2 * Copyright 2010 Jacek Caban for CodeWeavers
3 * Copyright 2010 Thomas Mullaly
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #define NONAMELESSUNION
22 #include "urlmon_main.h"
23 #include "wine/debug.h"
25 #define NO_SHLWAPI_REG
30 #define UINT_MAX 0xffffffff
31 #define USHORT_MAX 0xffff
33 #define URI_DISPLAY_NO_ABSOLUTE_URI 0x1
34 #define URI_DISPLAY_NO_DEFAULT_PORT_AUTH 0x2
36 #define ALLOW_NULL_TERM_SCHEME 0x01
37 #define ALLOW_NULL_TERM_USER_NAME 0x02
38 #define ALLOW_NULL_TERM_PASSWORD 0x04
39 #define ALLOW_BRACKETLESS_IP_LITERAL 0x08
40 #define SKIP_IP_FUTURE_CHECK 0x10
41 #define IGNORE_PORT_DELIMITER 0x20
43 #define RAW_URI_FORCE_PORT_DISP 0x1
44 #define RAW_URI_CONVERT_TO_DOS_PATH 0x2
46 #define COMBINE_URI_FORCE_FLAG_USE 0x1
48 WINE_DEFAULT_DEBUG_CHANNEL(urlmon
);
50 static const IID IID_IUriObj
= {0x4b364760,0x9f51,0x11df,{0x98,0x1c,0x08,0x00,0x20,0x0c,0x9a,0x66}};
54 IUriBuilderFactory IUriBuilderFactory_iface
;
55 IPersistStream IPersistStream_iface
;
61 /* Information about the canonicalized URI's buffer. */
65 BOOL display_modifiers
;
70 URL_SCHEME scheme_type
;
78 Uri_HOST_TYPE host_type
;
101 IUriBuilder IUriBuilder_iface
;
105 DWORD modified_props
;
138 /* IPv6 addresses can hold up to 8 h16 components. */
142 /* An IPv6 can have 1 elision ("::"). */
143 const WCHAR
*elision
;
145 /* An IPv6 can contain 1 IPv4 address as the last 32bits of the address. */
158 BOOL has_implicit_scheme
;
159 BOOL has_implicit_ip
;
165 URL_SCHEME scheme_type
;
167 const WCHAR
*username
;
170 const WCHAR
*password
;
175 Uri_HOST_TYPE host_type
;
178 ipv6_address ipv6_address
;
191 const WCHAR
*fragment
;
195 static const CHAR hexDigits
[] = "0123456789ABCDEF";
197 /* List of scheme types/scheme names that are recognized by the IUri interface as of IE 7. */
198 static const struct {
200 WCHAR scheme_name
[16];
201 } recognized_schemes
[] = {
202 {URL_SCHEME_FTP
, {'f','t','p',0}},
203 {URL_SCHEME_HTTP
, {'h','t','t','p',0}},
204 {URL_SCHEME_GOPHER
, {'g','o','p','h','e','r',0}},
205 {URL_SCHEME_MAILTO
, {'m','a','i','l','t','o',0}},
206 {URL_SCHEME_NEWS
, {'n','e','w','s',0}},
207 {URL_SCHEME_NNTP
, {'n','n','t','p',0}},
208 {URL_SCHEME_TELNET
, {'t','e','l','n','e','t',0}},
209 {URL_SCHEME_WAIS
, {'w','a','i','s',0}},
210 {URL_SCHEME_FILE
, {'f','i','l','e',0}},
211 {URL_SCHEME_MK
, {'m','k',0}},
212 {URL_SCHEME_HTTPS
, {'h','t','t','p','s',0}},
213 {URL_SCHEME_SHELL
, {'s','h','e','l','l',0}},
214 {URL_SCHEME_SNEWS
, {'s','n','e','w','s',0}},
215 {URL_SCHEME_LOCAL
, {'l','o','c','a','l',0}},
216 {URL_SCHEME_JAVASCRIPT
, {'j','a','v','a','s','c','r','i','p','t',0}},
217 {URL_SCHEME_VBSCRIPT
, {'v','b','s','c','r','i','p','t',0}},
218 {URL_SCHEME_ABOUT
, {'a','b','o','u','t',0}},
219 {URL_SCHEME_RES
, {'r','e','s',0}},
220 {URL_SCHEME_MSSHELLROOTED
, {'m','s','-','s','h','e','l','l','-','r','o','o','t','e','d',0}},
221 {URL_SCHEME_MSSHELLIDLIST
, {'m','s','-','s','h','e','l','l','-','i','d','l','i','s','t',0}},
222 {URL_SCHEME_MSHELP
, {'h','c','p',0}},
223 {URL_SCHEME_WILDCARD
, {'*',0}}
226 /* List of default ports Windows recognizes. */
227 static const struct {
230 } default_ports
[] = {
231 {URL_SCHEME_FTP
, 21},
232 {URL_SCHEME_HTTP
, 80},
233 {URL_SCHEME_GOPHER
, 70},
234 {URL_SCHEME_NNTP
, 119},
235 {URL_SCHEME_TELNET
, 23},
236 {URL_SCHEME_WAIS
, 210},
237 {URL_SCHEME_HTTPS
, 443},
240 /* List of 3-character top level domain names Windows seems to recognize.
241 * There might be more, but, these are the only ones I've found so far.
243 static const struct {
245 } recognized_tlds
[] = {
255 static Uri
*get_uri_obj(IUri
*uri
)
260 hres
= IUri_QueryInterface(uri
, &IID_IUriObj
, (void**)&ret
);
261 return SUCCEEDED(hres
) ? ret
: NULL
;
264 static inline BOOL
is_alpha(WCHAR val
) {
265 return ((val
>= 'a' && val
<= 'z') || (val
>= 'A' && val
<= 'Z'));
268 static inline BOOL
is_num(WCHAR val
) {
269 return (val
>= '0' && val
<= '9');
272 static inline BOOL
is_drive_path(const WCHAR
*str
) {
273 return (is_alpha(str
[0]) && (str
[1] == ':' || str
[1] == '|'));
276 static inline BOOL
is_unc_path(const WCHAR
*str
) {
277 return (str
[0] == '\\' && str
[0] == '\\');
280 static inline BOOL
is_forbidden_dos_path_char(WCHAR val
) {
281 return (val
== '>' || val
== '<' || val
== '\"');
284 /* A URI is implicitly a file path if it begins with
285 * a drive letter (e.g. X:) or starts with "\\" (UNC path).
287 static inline BOOL
is_implicit_file_path(const WCHAR
*str
) {
288 return (is_unc_path(str
) || (is_alpha(str
[0]) && str
[1] == ':'));
291 /* Checks if the URI is a hierarchical URI. A hierarchical
292 * URI is one that has "//" after the scheme.
294 static BOOL
check_hierarchical(const WCHAR
**ptr
) {
295 const WCHAR
*start
= *ptr
;
310 /* unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~" */
311 static inline BOOL
is_unreserved(WCHAR val
) {
312 return (is_alpha(val
) || is_num(val
) || val
== '-' || val
== '.' ||
313 val
== '_' || val
== '~');
316 /* sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
317 * / "*" / "+" / "," / ";" / "="
319 static inline BOOL
is_subdelim(WCHAR val
) {
320 return (val
== '!' || val
== '$' || val
== '&' ||
321 val
== '\'' || val
== '(' || val
== ')' ||
322 val
== '*' || val
== '+' || val
== ',' ||
323 val
== ';' || val
== '=');
326 /* gen-delims = ":" / "/" / "?" / "#" / "[" / "]" / "@" */
327 static inline BOOL
is_gendelim(WCHAR val
) {
328 return (val
== ':' || val
== '/' || val
== '?' ||
329 val
== '#' || val
== '[' || val
== ']' ||
333 /* Characters that delimit the end of the authority
334 * section of a URI. Sometimes a '\\' is considered
335 * an authority delimiter.
337 static inline BOOL
is_auth_delim(WCHAR val
, BOOL acceptSlash
) {
338 return (val
== '#' || val
== '/' || val
== '?' ||
339 val
== '\0' || (acceptSlash
&& val
== '\\'));
342 /* reserved = gen-delims / sub-delims */
343 static inline BOOL
is_reserved(WCHAR val
) {
344 return (is_subdelim(val
) || is_gendelim(val
));
347 static inline BOOL
is_hexdigit(WCHAR val
) {
348 return ((val
>= 'a' && val
<= 'f') ||
349 (val
>= 'A' && val
<= 'F') ||
350 (val
>= '0' && val
<= '9'));
353 static inline BOOL
is_path_delim(WCHAR val
) {
354 return (!val
|| val
== '#' || val
== '?');
357 static inline BOOL
is_slash(WCHAR c
)
359 return c
== '/' || c
== '\\';
362 static BOOL
is_default_port(URL_SCHEME scheme
, DWORD port
) {
365 for(i
= 0; i
< sizeof(default_ports
)/sizeof(default_ports
[0]); ++i
) {
366 if(default_ports
[i
].scheme
== scheme
&& default_ports
[i
].port
)
373 /* List of schemes types Windows seems to expect to be hierarchical. */
374 static inline BOOL
is_hierarchical_scheme(URL_SCHEME type
) {
375 return(type
== URL_SCHEME_HTTP
|| type
== URL_SCHEME_FTP
||
376 type
== URL_SCHEME_GOPHER
|| type
== URL_SCHEME_NNTP
||
377 type
== URL_SCHEME_TELNET
|| type
== URL_SCHEME_WAIS
||
378 type
== URL_SCHEME_FILE
|| type
== URL_SCHEME_HTTPS
||
379 type
== URL_SCHEME_RES
);
382 /* Checks if 'flags' contains an invalid combination of Uri_CREATE flags. */
383 static inline BOOL
has_invalid_flag_combination(DWORD flags
) {
384 return((flags
& Uri_CREATE_DECODE_EXTRA_INFO
&& flags
& Uri_CREATE_NO_DECODE_EXTRA_INFO
) ||
385 (flags
& Uri_CREATE_CANONICALIZE
&& flags
& Uri_CREATE_NO_CANONICALIZE
) ||
386 (flags
& Uri_CREATE_CRACK_UNKNOWN_SCHEMES
&& flags
& Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES
) ||
387 (flags
& Uri_CREATE_PRE_PROCESS_HTML_URI
&& flags
& Uri_CREATE_NO_PRE_PROCESS_HTML_URI
) ||
388 (flags
& Uri_CREATE_IE_SETTINGS
&& flags
& Uri_CREATE_NO_IE_SETTINGS
));
391 /* Applies each default Uri_CREATE flags to 'flags' if it
392 * doesn't cause a flag conflict.
394 static void apply_default_flags(DWORD
*flags
) {
395 if(!(*flags
& Uri_CREATE_NO_CANONICALIZE
))
396 *flags
|= Uri_CREATE_CANONICALIZE
;
397 if(!(*flags
& Uri_CREATE_NO_DECODE_EXTRA_INFO
))
398 *flags
|= Uri_CREATE_DECODE_EXTRA_INFO
;
399 if(!(*flags
& Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES
))
400 *flags
|= Uri_CREATE_CRACK_UNKNOWN_SCHEMES
;
401 if(!(*flags
& Uri_CREATE_NO_PRE_PROCESS_HTML_URI
))
402 *flags
|= Uri_CREATE_PRE_PROCESS_HTML_URI
;
403 if(!(*flags
& Uri_CREATE_IE_SETTINGS
))
404 *flags
|= Uri_CREATE_NO_IE_SETTINGS
;
407 /* Determines if the URI is hierarchical using the information already parsed into
408 * data and using the current location of parsing in the URI string.
410 * Windows considers a URI hierarchical if one of the following is true:
411 * A.) It's a wildcard scheme.
412 * B.) It's an implicit file scheme.
413 * C.) It's a known hierarchical scheme and it has two '\\' after the scheme name.
414 * (the '\\' will be converted into "//" during canonicalization).
415 * D.) It's not a relative URI and "//" appears after the scheme name.
417 static inline BOOL
is_hierarchical_uri(const WCHAR
**ptr
, const parse_data
*data
) {
418 const WCHAR
*start
= *ptr
;
420 if(data
->scheme_type
== URL_SCHEME_WILDCARD
)
422 else if(data
->scheme_type
== URL_SCHEME_FILE
&& data
->has_implicit_scheme
)
424 else if(is_hierarchical_scheme(data
->scheme_type
) && (*ptr
)[0] == '\\' && (*ptr
)[1] == '\\') {
427 } else if(!data
->is_relative
&& check_hierarchical(ptr
))
434 /* Computes the size of the given IPv6 address.
435 * Each h16 component is 16 bits. If there is an IPv4 address, it's
436 * 32 bits. If there's an elision it can be 16 to 128 bits, depending
437 * on the number of other components.
439 * Modeled after google-url's CheckIPv6ComponentsSize function
441 static void compute_ipv6_comps_size(ipv6_address
*address
) {
442 address
->components_size
= address
->h16_count
* 2;
445 /* IPv4 address is 4 bytes. */
446 address
->components_size
+= 4;
448 if(address
->elision
) {
449 /* An elision can be anywhere from 2 bytes up to 16 bytes.
450 * Its size depends on the size of the h16 and IPv4 components.
452 address
->elision_size
= 16 - address
->components_size
;
453 if(address
->elision_size
< 2)
454 address
->elision_size
= 2;
456 address
->elision_size
= 0;
459 /* Taken from dlls/jscript/lex.c */
460 static int hex_to_int(WCHAR val
) {
461 if(val
>= '0' && val
<= '9')
463 else if(val
>= 'a' && val
<= 'f')
464 return val
- 'a' + 10;
465 else if(val
>= 'A' && val
<= 'F')
466 return val
- 'A' + 10;
471 /* Helper function for converting a percent encoded string
472 * representation of a WCHAR value into its actual WCHAR value. If
473 * the two characters following the '%' aren't valid hex values then
474 * this function returns the NULL character.
477 * "%2E" will result in '.' being returned by this function.
479 static WCHAR
decode_pct_val(const WCHAR
*ptr
) {
482 if(*ptr
== '%' && is_hexdigit(*(ptr
+ 1)) && is_hexdigit(*(ptr
+ 2))) {
483 INT a
= hex_to_int(*(ptr
+ 1));
484 INT b
= hex_to_int(*(ptr
+ 2));
493 /* Helper function for percent encoding a given character
494 * and storing the encoded value into a given buffer (dest).
496 * It's up to the calling function to ensure that there is
497 * at least enough space in 'dest' for the percent encoded
498 * value to be stored (so dest + 3 spaces available).
500 static inline void pct_encode_val(WCHAR val
, WCHAR
*dest
) {
502 dest
[1] = hexDigits
[(val
>> 4) & 0xf];
503 dest
[2] = hexDigits
[val
& 0xf];
506 /* Attempts to parse the domain name from the host.
508 * This function also includes the Top-level Domain (TLD) name
509 * of the host when it tries to find the domain name. If it finds
510 * a valid domain name it will assign 'domain_start' the offset
511 * into 'host' where the domain name starts.
513 * It's implied that if there is a domain name its range is:
514 * [host+domain_start, host+host_len).
516 void find_domain_name(const WCHAR
*host
, DWORD host_len
,
518 const WCHAR
*last_tld
, *sec_last_tld
, *end
;
520 end
= host
+host_len
-1;
524 /* There has to be at least enough room for a '.' followed by a
525 * 3-character TLD for a domain to even exist in the host name.
530 last_tld
= memrchrW(host
, '.', host_len
);
532 /* http://hostname -> has no domain name. */
535 sec_last_tld
= memrchrW(host
, '.', last_tld
-host
);
537 /* If the '.' is at the beginning of the host there
538 * has to be at least 3 characters in the TLD for it
540 * Ex: .com -> .com as the domain name.
541 * .co -> has no domain name.
543 if(last_tld
-host
== 0) {
544 if(end
-(last_tld
-1) < 3)
546 } else if(last_tld
-host
== 3) {
549 /* If there are three characters in front of last_tld and
550 * they are on the list of recognized TLDs, then this
551 * host doesn't have a domain (since the host only contains
553 * Ex: edu.uk -> has no domain name.
554 * foo.uk -> foo.uk as the domain name.
556 for(i
= 0; i
< sizeof(recognized_tlds
)/sizeof(recognized_tlds
[0]); ++i
) {
557 if(!StrCmpNIW(host
, recognized_tlds
[i
].tld_name
, 3))
560 } else if(last_tld
-host
< 3)
561 /* Anything less than 3 characters is considered part
563 * Ex: ak.uk -> Has no domain name.
567 /* Otherwise the domain name is the whole host name. */
569 } else if(end
+1-last_tld
> 3) {
570 /* If the last_tld has more than 3 characters, then it's automatically
571 * considered the TLD of the domain name.
572 * Ex: www.winehq.org.uk.test -> uk.test as the domain name.
574 *domain_start
= (sec_last_tld
+1)-host
;
575 } else if(last_tld
- (sec_last_tld
+1) < 4) {
577 /* If the sec_last_tld is 3 characters long it HAS to be on the list of
578 * recognized to still be considered part of the TLD name, otherwise
579 * its considered the domain name.
580 * Ex: www.google.com.uk -> google.com.uk as the domain name.
581 * www.google.foo.uk -> foo.uk as the domain name.
583 if(last_tld
- (sec_last_tld
+1) == 3) {
584 for(i
= 0; i
< sizeof(recognized_tlds
)/sizeof(recognized_tlds
[0]); ++i
) {
585 if(!StrCmpNIW(sec_last_tld
+1, recognized_tlds
[i
].tld_name
, 3)) {
586 const WCHAR
*domain
= memrchrW(host
, '.', sec_last_tld
-host
);
591 *domain_start
= (domain
+1) - host
;
592 TRACE("Found domain name %s\n", debugstr_wn(host
+*domain_start
,
593 (host
+host_len
)-(host
+*domain_start
)));
598 *domain_start
= (sec_last_tld
+1)-host
;
600 /* Since the sec_last_tld is less than 3 characters it's considered
602 * Ex: www.google.fo.uk -> google.fo.uk as the domain name.
604 const WCHAR
*domain
= memrchrW(host
, '.', sec_last_tld
-host
);
609 *domain_start
= (domain
+1) - host
;
612 /* The second to last TLD has more than 3 characters making it
614 * Ex: www.google.test.us -> test.us as the domain name.
616 *domain_start
= (sec_last_tld
+1)-host
;
619 TRACE("Found domain name %s\n", debugstr_wn(host
+*domain_start
,
620 (host
+host_len
)-(host
+*domain_start
)));
623 /* Removes the dot segments from a hierarchical URIs path component. This
624 * function performs the removal in place.
626 * This function returns the new length of the path string.
628 static DWORD
remove_dot_segments(WCHAR
*path
, DWORD path_len
) {
630 const WCHAR
*in
= out
;
631 const WCHAR
*end
= out
+ path_len
;
635 /* Move the first path segment in the input buffer to the end of
636 * the output buffer, and any subsequent characters up to, including
637 * the next "/" character (if any) or the end of the input buffer.
639 while(in
< end
&& !is_slash(*in
))
649 /* Handle ending "/." */
656 if(is_slash(in
[1])) {
661 /* If we don't have "/../" or ending "/.." */
662 if(in
[1] != '.' || (in
+ 2 != end
&& !is_slash(in
[2])))
665 /* Find the slash preceding out pointer and move out pointer to it */
666 if(out
> path
+1 && is_slash(*--out
))
668 while(out
> path
&& !is_slash(*(--out
)));
678 TRACE("(%p %d): Path after dot segments removed %s len=%d\n", path
, path_len
,
679 debugstr_wn(path
, len
), len
);
683 /* Attempts to find the file extension in a given path. */
684 static INT
find_file_extension(const WCHAR
*path
, DWORD path_len
) {
687 for(end
= path
+path_len
-1; end
>= path
&& *end
!= '/' && *end
!= '\\'; --end
) {
695 /* Computes the location where the elision should occur in the IPv6
696 * address using the numerical values of each component stored in
697 * 'values'. If the address shouldn't contain an elision then 'index'
698 * is assigned -1 as its value. Otherwise 'index' will contain the
699 * starting index (into values) where the elision should be, and 'count'
700 * will contain the number of cells the elision covers.
703 * Windows will expand an elision if the elision only represents one h16
704 * component of the address.
706 * Ex: [1::2:3:4:5:6:7] -> [1:0:2:3:4:5:6:7]
708 * If the IPv6 address contains an IPv4 address, the IPv4 address is also
709 * considered for being included as part of an elision if all its components
712 * Ex: [1:2:3:4:5:6:0.0.0.0] -> [1:2:3:4:5:6::]
714 static void compute_elision_location(const ipv6_address
*address
, const USHORT values
[8],
715 INT
*index
, DWORD
*count
) {
716 DWORD i
, max_len
, cur_len
;
717 INT max_index
, cur_index
;
719 max_len
= cur_len
= 0;
720 max_index
= cur_index
= -1;
721 for(i
= 0; i
< 8; ++i
) {
722 BOOL check_ipv4
= (address
->ipv4
&& i
== 6);
723 BOOL is_end
= (check_ipv4
|| i
== 7);
726 /* Check if the IPv4 address contains only zeros. */
727 if(values
[i
] == 0 && values
[i
+1] == 0) {
734 } else if(values
[i
] == 0) {
741 if(is_end
|| values
[i
] != 0) {
742 /* We only consider it for an elision if it's
743 * more than 1 component long.
745 if(cur_len
> 1 && cur_len
> max_len
) {
746 /* Found the new elision location. */
748 max_index
= cur_index
;
751 /* Reset the current range for the next range of zeros. */
761 /* Removes all the leading and trailing white spaces or
762 * control characters from the URI and removes all control
763 * characters inside of the URI string.
765 static BSTR
pre_process_uri(LPCWSTR uri
) {
766 const WCHAR
*start
, *end
, *ptr
;
772 /* Skip leading controls and whitespace. */
773 while(*start
&& (iscntrlW(*start
) || isspaceW(*start
))) ++start
;
775 /* URI consisted only of control/whitespace. */
777 return SysAllocStringLen(NULL
, 0);
779 end
= start
+ strlenW(start
);
780 while(--end
> start
&& (iscntrlW(*end
) || isspaceW(*end
)));
783 for(ptr
= start
; ptr
< end
; ptr
++) {
788 ret
= SysAllocStringLen(NULL
, len
);
792 for(ptr
= start
, ptr2
=ret
; ptr
< end
; ptr
++) {
800 /* Converts the specified IPv4 address into an uint value.
802 * This function assumes that the IPv4 address has already been validated.
804 static UINT
ipv4toui(const WCHAR
*ip
, DWORD len
) {
806 DWORD comp_value
= 0;
809 for(ptr
= ip
; ptr
< ip
+len
; ++ptr
) {
815 comp_value
= comp_value
*10 + (*ptr
-'0');
824 /* Converts an IPv4 address in numerical form into its fully qualified
825 * string form. This function returns the number of characters written
826 * to 'dest'. If 'dest' is NULL this function will return the number of
827 * characters that would have been written.
829 * It's up to the caller to ensure there's enough space in 'dest' for the
832 static DWORD
ui2ipv4(WCHAR
*dest
, UINT address
) {
833 static const WCHAR formatW
[] =
834 {'%','u','.','%','u','.','%','u','.','%','u',0};
838 digits
[0] = (address
>> 24) & 0xff;
839 digits
[1] = (address
>> 16) & 0xff;
840 digits
[2] = (address
>> 8) & 0xff;
841 digits
[3] = address
& 0xff;
845 ret
= sprintfW(tmp
, formatW
, digits
[0], digits
[1], digits
[2], digits
[3]);
847 ret
= sprintfW(dest
, formatW
, digits
[0], digits
[1], digits
[2], digits
[3]);
852 static DWORD
ui2str(WCHAR
*dest
, UINT value
) {
853 static const WCHAR formatW
[] = {'%','u',0};
858 ret
= sprintfW(tmp
, formatW
, value
);
860 ret
= sprintfW(dest
, formatW
, value
);
865 /* Converts a h16 component (from an IPv6 address) into its
868 * This function assumes that the h16 component has already been validated.
870 static USHORT
h16tous(h16 component
) {
874 for(i
= 0; i
< component
.len
; ++i
) {
876 ret
+= hex_to_int(component
.str
[i
]);
882 /* Converts an IPv6 address into its 128 bits (16 bytes) numerical value.
884 * This function assumes that the ipv6_address has already been validated.
886 static BOOL
ipv6_to_number(const ipv6_address
*address
, USHORT number
[8]) {
887 DWORD i
, cur_component
= 0;
888 BOOL already_passed_elision
= FALSE
;
890 for(i
= 0; i
< address
->h16_count
; ++i
) {
891 if(address
->elision
) {
892 if(address
->components
[i
].str
> address
->elision
&& !already_passed_elision
) {
893 /* Means we just passed the elision and need to add its values to
894 * 'number' before we do anything else.
897 for(j
= 0; j
< address
->elision_size
; j
+=2)
898 number
[cur_component
++] = 0;
900 already_passed_elision
= TRUE
;
904 number
[cur_component
++] = h16tous(address
->components
[i
]);
907 /* Case when the elision appears after the h16 components. */
908 if(!already_passed_elision
&& address
->elision
) {
909 for(i
= 0; i
< address
->elision_size
; i
+=2)
910 number
[cur_component
++] = 0;
914 UINT value
= ipv4toui(address
->ipv4
, address
->ipv4_len
);
916 if(cur_component
!= 6) {
917 ERR("(%p %p): Failed sanity check with %d\n", address
, number
, cur_component
);
921 number
[cur_component
++] = (value
>> 16) & 0xffff;
922 number
[cur_component
] = value
& 0xffff;
928 /* Checks if the characters pointed to by 'ptr' are
929 * a percent encoded data octet.
931 * pct-encoded = "%" HEXDIG HEXDIG
933 static BOOL
check_pct_encoded(const WCHAR
**ptr
) {
934 const WCHAR
*start
= *ptr
;
940 if(!is_hexdigit(**ptr
)) {
946 if(!is_hexdigit(**ptr
)) {
955 /* dec-octet = DIGIT ; 0-9
956 * / %x31-39 DIGIT ; 10-99
957 * / "1" 2DIGIT ; 100-199
958 * / "2" %x30-34 DIGIT ; 200-249
959 * / "25" %x30-35 ; 250-255
961 static BOOL
check_dec_octet(const WCHAR
**ptr
) {
962 const WCHAR
*c1
, *c2
, *c3
;
965 /* A dec-octet must be at least 1 digit long. */
966 if(*c1
< '0' || *c1
> '9')
972 /* Since the 1-digit requirement was met, it doesn't
973 * matter if this is a DIGIT value, it's considered a
976 if(*c2
< '0' || *c2
> '9')
982 /* Same explanation as above. */
983 if(*c3
< '0' || *c3
> '9')
986 /* Anything > 255 isn't a valid IP dec-octet. */
987 if(*c1
>= '2' && *c2
>= '5' && *c3
>= '5') {
996 /* Checks if there is an implicit IPv4 address in the host component of the URI.
997 * The max value of an implicit IPv4 address is UINT_MAX.
1000 * "234567" would be considered an implicit IPv4 address.
1002 static BOOL
check_implicit_ipv4(const WCHAR
**ptr
, UINT
*val
) {
1003 const WCHAR
*start
= *ptr
;
1007 while(is_num(**ptr
)) {
1008 ret
= ret
*10 + (**ptr
- '0');
1010 if(ret
> UINT_MAX
) {
1024 /* Checks if the string contains an IPv4 address.
1026 * This function has a strict mode or a non-strict mode of operation
1027 * When 'strict' is set to FALSE this function will return TRUE if
1028 * the string contains at least 'dec-octet "." dec-octet' since partial
1029 * IPv4 addresses will be normalized out into full IPv4 addresses. When
1030 * 'strict' is set this function expects there to be a full IPv4 address.
1032 * IPv4address = dec-octet "." dec-octet "." dec-octet "." dec-octet
1034 static BOOL
check_ipv4address(const WCHAR
**ptr
, BOOL strict
) {
1035 const WCHAR
*start
= *ptr
;
1037 if(!check_dec_octet(ptr
)) {
1048 if(!check_dec_octet(ptr
)) {
1062 if(!check_dec_octet(ptr
)) {
1076 if(!check_dec_octet(ptr
)) {
1081 /* Found a four digit ip address. */
1084 /* Tries to parse the scheme name of the URI.
1086 * scheme = ALPHA *(ALPHA | NUM | '+' | '-' | '.') as defined by RFC 3896.
1087 * NOTE: Windows accepts a number as the first character of a scheme.
1089 static BOOL
parse_scheme_name(const WCHAR
**ptr
, parse_data
*data
, DWORD extras
) {
1090 const WCHAR
*start
= *ptr
;
1092 data
->scheme
= NULL
;
1093 data
->scheme_len
= 0;
1096 if(**ptr
== '*' && *ptr
== start
) {
1097 /* Might have found a wildcard scheme. If it is the next
1098 * char has to be a ':' for it to be a valid URI
1102 } else if(!is_num(**ptr
) && !is_alpha(**ptr
) && **ptr
!= '+' &&
1103 **ptr
!= '-' && **ptr
!= '.')
1112 /* Schemes must end with a ':' */
1113 if(**ptr
!= ':' && !((extras
& ALLOW_NULL_TERM_SCHEME
) && !**ptr
)) {
1118 data
->scheme
= start
;
1119 data
->scheme_len
= *ptr
- start
;
1125 /* Tries to deduce the corresponding URL_SCHEME for the given URI. Stores
1126 * the deduced URL_SCHEME in data->scheme_type.
1128 static BOOL
parse_scheme_type(parse_data
*data
) {
1129 /* If there's scheme data then see if it's a recognized scheme. */
1130 if(data
->scheme
&& data
->scheme_len
) {
1133 for(i
= 0; i
< sizeof(recognized_schemes
)/sizeof(recognized_schemes
[0]); ++i
) {
1134 if(lstrlenW(recognized_schemes
[i
].scheme_name
) == data
->scheme_len
) {
1135 /* Has to be a case insensitive compare. */
1136 if(!StrCmpNIW(recognized_schemes
[i
].scheme_name
, data
->scheme
, data
->scheme_len
)) {
1137 data
->scheme_type
= recognized_schemes
[i
].scheme
;
1143 /* If we get here it means it's not a recognized scheme. */
1144 data
->scheme_type
= URL_SCHEME_UNKNOWN
;
1146 } else if(data
->is_relative
) {
1147 /* Relative URI's have no scheme. */
1148 data
->scheme_type
= URL_SCHEME_UNKNOWN
;
1151 /* Should never reach here! what happened... */
1152 FIXME("(%p): Unable to determine scheme type for URI %s\n", data
, debugstr_w(data
->uri
));
1157 /* Tries to parse (or deduce) the scheme_name of a URI. If it can't
1158 * parse a scheme from the URI it will try to deduce the scheme_name and scheme_type
1159 * using the flags specified in 'flags' (if any). Flags that affect how this function
1160 * operates are the Uri_CREATE_ALLOW_* flags.
1162 * All parsed/deduced information will be stored in 'data' when the function returns.
1164 * Returns TRUE if it was able to successfully parse the information.
1166 static BOOL
parse_scheme(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
, DWORD extras
) {
1167 static const WCHAR fileW
[] = {'f','i','l','e',0};
1168 static const WCHAR wildcardW
[] = {'*',0};
1170 /* First check to see if the uri could implicitly be a file path. */
1171 if(is_implicit_file_path(*ptr
)) {
1172 if(flags
& Uri_CREATE_ALLOW_IMPLICIT_FILE_SCHEME
) {
1173 data
->scheme
= fileW
;
1174 data
->scheme_len
= lstrlenW(fileW
);
1175 data
->has_implicit_scheme
= TRUE
;
1177 TRACE("(%p %p %x): URI is an implicit file path.\n", ptr
, data
, flags
);
1179 /* Windows does not consider anything that can implicitly be a file
1180 * path to be a valid URI if the ALLOW_IMPLICIT_FILE_SCHEME flag is not set...
1182 TRACE("(%p %p %x): URI is implicitly a file path, but, the ALLOW_IMPLICIT_FILE_SCHEME flag wasn't set.\n",
1186 } else if(!parse_scheme_name(ptr
, data
, extras
)) {
1187 /* No scheme was found, this means it could be:
1188 * a) an implicit Wildcard scheme
1190 * c) an invalid URI.
1192 if(flags
& Uri_CREATE_ALLOW_IMPLICIT_WILDCARD_SCHEME
) {
1193 data
->scheme
= wildcardW
;
1194 data
->scheme_len
= lstrlenW(wildcardW
);
1195 data
->has_implicit_scheme
= TRUE
;
1197 TRACE("(%p %p %x): URI is an implicit wildcard scheme.\n", ptr
, data
, flags
);
1198 } else if (flags
& Uri_CREATE_ALLOW_RELATIVE
) {
1199 data
->is_relative
= TRUE
;
1200 TRACE("(%p %p %x): URI is relative.\n", ptr
, data
, flags
);
1202 TRACE("(%p %p %x): Malformed URI found. Unable to deduce scheme name.\n", ptr
, data
, flags
);
1207 if(!data
->is_relative
)
1208 TRACE("(%p %p %x): Found scheme=%s scheme_len=%d\n", ptr
, data
, flags
,
1209 debugstr_wn(data
->scheme
, data
->scheme_len
), data
->scheme_len
);
1211 if(!parse_scheme_type(data
))
1214 TRACE("(%p %p %x): Assigned %d as the URL_SCHEME.\n", ptr
, data
, flags
, data
->scheme_type
);
1218 static BOOL
parse_username(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
, DWORD extras
) {
1219 data
->username
= *ptr
;
1221 while(**ptr
!= ':' && **ptr
!= '@') {
1223 if(!check_pct_encoded(ptr
)) {
1224 if(data
->scheme_type
!= URL_SCHEME_UNKNOWN
) {
1225 *ptr
= data
->username
;
1226 data
->username
= NULL
;
1231 } else if(extras
& ALLOW_NULL_TERM_USER_NAME
&& !**ptr
)
1233 else if(is_auth_delim(**ptr
, data
->scheme_type
!= URL_SCHEME_UNKNOWN
)) {
1234 *ptr
= data
->username
;
1235 data
->username
= NULL
;
1242 data
->username_len
= *ptr
- data
->username
;
1246 static BOOL
parse_password(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
, DWORD extras
) {
1247 data
->password
= *ptr
;
1249 while(**ptr
!= '@') {
1251 if(!check_pct_encoded(ptr
)) {
1252 if(data
->scheme_type
!= URL_SCHEME_UNKNOWN
) {
1253 *ptr
= data
->password
;
1254 data
->password
= NULL
;
1259 } else if(extras
& ALLOW_NULL_TERM_PASSWORD
&& !**ptr
)
1261 else if(is_auth_delim(**ptr
, data
->scheme_type
!= URL_SCHEME_UNKNOWN
)) {
1262 *ptr
= data
->password
;
1263 data
->password
= NULL
;
1270 data
->password_len
= *ptr
- data
->password
;
1274 /* Parses the userinfo part of the URI (if it exists). The userinfo field of
1275 * a URI can consist of "username:password@", or just "username@".
1278 * userinfo = *( unreserved / pct-encoded / sub-delims / ":" )
1281 * 1) If there is more than one ':' in the userinfo part of the URI Windows
1282 * uses the first occurrence of ':' to delimit the username and password
1286 * ftp://user:pass:word@winehq.org
1288 * would yield "user" as the username and "pass:word" as the password.
1290 * 2) Windows allows any character to appear in the "userinfo" part of
1291 * a URI, as long as it's not an authority delimiter character set.
1293 static void parse_userinfo(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
) {
1294 const WCHAR
*start
= *ptr
;
1296 if(!parse_username(ptr
, data
, flags
, 0)) {
1297 TRACE("(%p %p %x): URI contained no userinfo.\n", ptr
, data
, flags
);
1303 if(!parse_password(ptr
, data
, flags
, 0)) {
1305 data
->username
= NULL
;
1306 data
->username_len
= 0;
1307 TRACE("(%p %p %x): URI contained no userinfo.\n", ptr
, data
, flags
);
1314 data
->username
= NULL
;
1315 data
->username_len
= 0;
1316 data
->password
= NULL
;
1317 data
->password_len
= 0;
1319 TRACE("(%p %p %x): URI contained no userinfo.\n", ptr
, data
, flags
);
1324 TRACE("(%p %p %x): Found username %s len=%d.\n", ptr
, data
, flags
,
1325 debugstr_wn(data
->username
, data
->username_len
), data
->username_len
);
1328 TRACE("(%p %p %x): Found password %s len=%d.\n", ptr
, data
, flags
,
1329 debugstr_wn(data
->password
, data
->password_len
), data
->password_len
);
1334 /* Attempts to parse a port from the URI.
1337 * Windows seems to have a cap on what the maximum value
1338 * for a port can be. The max value is USHORT_MAX.
1342 static BOOL
parse_port(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
) {
1346 while(!is_auth_delim(**ptr
, data
->scheme_type
!= URL_SCHEME_UNKNOWN
)) {
1347 if(!is_num(**ptr
)) {
1353 port
= port
*10 + (**ptr
-'0');
1355 if(port
> USHORT_MAX
) {
1364 data
->has_port
= TRUE
;
1365 data
->port_value
= port
;
1366 data
->port_len
= *ptr
- data
->port
;
1368 TRACE("(%p %p %x): Found port %s len=%d value=%u\n", ptr
, data
, flags
,
1369 debugstr_wn(data
->port
, data
->port_len
), data
->port_len
, data
->port_value
);
1373 /* Attempts to parse a IPv4 address from the URI.
1376 * Windows normalizes IPv4 addresses, This means there are three
1377 * possibilities for the URI to contain an IPv4 address.
1378 * 1) A well formed address (ex. 192.2.2.2).
1379 * 2) A partially formed address. For example "192.0" would
1380 * normalize to "192.0.0.0" during canonicalization.
1381 * 3) An implicit IPv4 address. For example "256" would
1382 * normalize to "0.0.1.0" during canonicalization. Also
1383 * note that the maximum value for an implicit IP address
1384 * is UINT_MAX, if the value in the URI exceeds this then
1385 * it is not considered an IPv4 address.
1387 static BOOL
parse_ipv4address(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
) {
1388 const BOOL is_unknown
= data
->scheme_type
== URL_SCHEME_UNKNOWN
;
1391 if(!check_ipv4address(ptr
, FALSE
)) {
1392 if(!check_implicit_ipv4(ptr
, &data
->implicit_ipv4
)) {
1393 TRACE("(%p %p %x): URI didn't contain anything looking like an IPv4 address.\n",
1399 data
->has_implicit_ip
= TRUE
;
1402 data
->host_len
= *ptr
- data
->host
;
1403 data
->host_type
= Uri_HOST_IPV4
;
1405 /* Check if what we found is the only part of the host name (if it isn't
1406 * we don't have an IPv4 address).
1410 if(!parse_port(ptr
, data
, flags
)) {
1415 } else if(!is_auth_delim(**ptr
, !is_unknown
)) {
1416 /* Found more data which belongs to the host, so this isn't an IPv4. */
1419 data
->has_implicit_ip
= FALSE
;
1423 TRACE("(%p %p %x): IPv4 address found. host=%s host_len=%d host_type=%d\n",
1424 ptr
, data
, flags
, debugstr_wn(data
->host
, data
->host_len
),
1425 data
->host_len
, data
->host_type
);
1429 /* Attempts to parse the reg-name from the URI.
1431 * Because of the way Windows handles ':' this function also
1432 * handles parsing the port.
1434 * reg-name = *( unreserved / pct-encoded / sub-delims )
1437 * Windows allows everything, but, the characters in "auth_delims" and ':'
1438 * to appear in a reg-name, unless it's an unknown scheme type then ':' is
1439 * allowed to appear (even if a valid port isn't after it).
1441 * Windows doesn't like host names which start with '[' and end with ']'
1442 * and don't contain a valid IP literal address in between them.
1444 * On Windows if a '[' is encountered in the host name the ':' no longer
1445 * counts as a delimiter until you reach the next ']' or an "authority delimiter".
1447 * A reg-name CAN be empty.
1449 static BOOL
parse_reg_name(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
, DWORD extras
) {
1450 const BOOL has_start_bracket
= **ptr
== '[';
1451 const BOOL known_scheme
= data
->scheme_type
!= URL_SCHEME_UNKNOWN
;
1452 const BOOL is_res
= data
->scheme_type
== URL_SCHEME_RES
;
1453 BOOL inside_brackets
= has_start_bracket
;
1455 /* res URIs don't have ports. */
1456 BOOL ignore_col
= (extras
& IGNORE_PORT_DELIMITER
) || is_res
;
1458 /* We have to be careful with file schemes. */
1459 if(data
->scheme_type
== URL_SCHEME_FILE
) {
1460 /* This is because an implicit file scheme could be "C:\\test" and it
1461 * would trick this function into thinking the host is "C", when after
1462 * canonicalization the host would end up being an empty string. A drive
1463 * path can also have a '|' instead of a ':' after the drive letter.
1465 if(is_drive_path(*ptr
)) {
1466 /* Regular old drive paths have no host type (or host name). */
1467 data
->host_type
= Uri_HOST_UNKNOWN
;
1471 } else if(is_unc_path(*ptr
))
1472 /* Skip past the "\\" of a UNC path. */
1478 /* For res URIs, everything before the first '/' is
1479 * considered the host.
1481 while((!is_res
&& !is_auth_delim(**ptr
, known_scheme
)) ||
1482 (is_res
&& **ptr
&& **ptr
!= '/')) {
1483 if(**ptr
== ':' && !ignore_col
) {
1484 /* We can ignore ':' if were inside brackets.*/
1485 if(!inside_brackets
) {
1486 const WCHAR
*tmp
= (*ptr
)++;
1488 /* Attempt to parse the port. */
1489 if(!parse_port(ptr
, data
, flags
)) {
1490 /* Windows expects there to be a valid port for known scheme types. */
1491 if(data
->scheme_type
!= URL_SCHEME_UNKNOWN
) {
1494 TRACE("(%p %p %x %x): Expected valid port\n", ptr
, data
, flags
, extras
);
1497 /* Windows gives up on trying to parse a port when it
1498 * encounters an invalid port.
1502 data
->host_len
= tmp
- data
->host
;
1506 } else if(**ptr
== '%' && (known_scheme
&& !is_res
)) {
1507 /* Has to be a legit % encoded value. */
1508 if(!check_pct_encoded(ptr
)) {
1514 } else if(is_res
&& is_forbidden_dos_path_char(**ptr
)) {
1518 } else if(**ptr
== ']')
1519 inside_brackets
= FALSE
;
1520 else if(**ptr
== '[')
1521 inside_brackets
= TRUE
;
1526 if(has_start_bracket
) {
1527 /* Make sure the last character of the host wasn't a ']'. */
1528 if(*(*ptr
-1) == ']') {
1529 TRACE("(%p %p %x %x): Expected an IP literal inside of the host\n",
1530 ptr
, data
, flags
, extras
);
1537 /* Don't overwrite our length if we found a port earlier. */
1539 data
->host_len
= *ptr
- data
->host
;
1541 /* If the host is empty, then it's an unknown host type. */
1542 if(data
->host_len
== 0 || is_res
)
1543 data
->host_type
= Uri_HOST_UNKNOWN
;
1545 data
->host_type
= Uri_HOST_DNS
;
1547 TRACE("(%p %p %x %x): Parsed reg-name. host=%s len=%d\n", ptr
, data
, flags
, extras
,
1548 debugstr_wn(data
->host
, data
->host_len
), data
->host_len
);
1552 /* Attempts to parse an IPv6 address out of the URI.
1554 * IPv6address = 6( h16 ":" ) ls32
1555 * / "::" 5( h16 ":" ) ls32
1556 * / [ h16 ] "::" 4( h16 ":" ) ls32
1557 * / [ *1( h16 ":" ) h16 ] "::" 3( h16 ":" ) ls32
1558 * / [ *2( h16 ":" ) h16 ] "::" 2( h16 ":" ) ls32
1559 * / [ *3( h16 ":" ) h16 ] "::" h16 ":" ls32
1560 * / [ *4( h16 ":" ) h16 ] "::" ls32
1561 * / [ *5( h16 ":" ) h16 ] "::" h16
1562 * / [ *6( h16 ":" ) h16 ] "::"
1564 * ls32 = ( h16 ":" h16 ) / IPv4address
1565 * ; least-significant 32 bits of address.
1568 * ; 16 bits of address represented in hexadecimal.
1570 * Modeled after google-url's 'DoParseIPv6' function.
1572 static BOOL
parse_ipv6address(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
) {
1573 const WCHAR
*start
, *cur_start
;
1576 start
= cur_start
= *ptr
;
1577 memset(&ip
, 0, sizeof(ipv6_address
));
1580 /* Check if we're on the last character of the host. */
1581 BOOL is_end
= (is_auth_delim(**ptr
, data
->scheme_type
!= URL_SCHEME_UNKNOWN
)
1584 BOOL is_split
= (**ptr
== ':');
1585 BOOL is_elision
= (is_split
&& !is_end
&& *(*ptr
+1) == ':');
1587 /* Check if we're at the end of a component, or
1588 * if we're at the end of the IPv6 address.
1590 if(is_split
|| is_end
) {
1593 cur_len
= *ptr
- cur_start
;
1595 /* h16 can't have a length > 4. */
1599 TRACE("(%p %p %x): h16 component to long.\n",
1605 /* An h16 component can't have the length of 0 unless
1606 * the elision is at the beginning of the address, or
1607 * at the end of the address.
1609 if(!((*ptr
== start
&& is_elision
) ||
1610 (is_end
&& (*ptr
-2) == ip
.elision
))) {
1612 TRACE("(%p %p %x): IPv6 component cannot have a length of 0.\n",
1619 /* An IPv6 address can have no more than 8 h16 components. */
1620 if(ip
.h16_count
>= 8) {
1622 TRACE("(%p %p %x): Not a IPv6 address, to many h16 components.\n",
1627 ip
.components
[ip
.h16_count
].str
= cur_start
;
1628 ip
.components
[ip
.h16_count
].len
= cur_len
;
1630 TRACE("(%p %p %x): Found h16 component %s, len=%d, h16_count=%d\n",
1631 ptr
, data
, flags
, debugstr_wn(cur_start
, cur_len
), cur_len
,
1641 /* A IPv6 address can only have 1 elision ('::'). */
1645 TRACE("(%p %p %x): IPv6 address cannot have 2 elisions.\n",
1657 if(!check_ipv4address(ptr
, TRUE
)) {
1658 if(!is_hexdigit(**ptr
)) {
1659 /* Not a valid character for an IPv6 address. */
1664 /* Found an IPv4 address. */
1665 ip
.ipv4
= cur_start
;
1666 ip
.ipv4_len
= *ptr
- cur_start
;
1668 TRACE("(%p %p %x): Found an attached IPv4 address %s len=%d.\n",
1669 ptr
, data
, flags
, debugstr_wn(ip
.ipv4
, ip
.ipv4_len
),
1672 /* IPv4 addresses can only appear at the end of a IPv6. */
1678 compute_ipv6_comps_size(&ip
);
1680 /* Make sure the IPv6 address adds up to 16 bytes. */
1681 if(ip
.components_size
+ ip
.elision_size
!= 16) {
1683 TRACE("(%p %p %x): Invalid IPv6 address, did not add up to 16 bytes.\n",
1688 if(ip
.elision_size
== 2) {
1689 /* For some reason on Windows if an elision that represents
1690 * only one h16 component is encountered at the very begin or
1691 * end of an IPv6 address, Windows does not consider it a
1692 * valid IPv6 address.
1694 * Ex: [::2:3:4:5:6:7] is not valid, even though the sum
1695 * of all the components == 128bits.
1697 if(ip
.elision
< ip
.components
[0].str
||
1698 ip
.elision
> ip
.components
[ip
.h16_count
-1].str
) {
1700 TRACE("(%p %p %x): Invalid IPv6 address. Detected elision of 2 bytes at the beginning or end of the address.\n",
1706 data
->host_type
= Uri_HOST_IPV6
;
1707 data
->has_ipv6
= TRUE
;
1708 data
->ipv6_address
= ip
;
1710 TRACE("(%p %p %x): Found valid IPv6 literal %s len=%d\n",
1711 ptr
, data
, flags
, debugstr_wn(start
, *ptr
-start
),
1716 /* IPvFuture = "v" 1*HEXDIG "." 1*( unreserved / sub-delims / ":" ) */
1717 static BOOL
parse_ipvfuture(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
) {
1718 const WCHAR
*start
= *ptr
;
1720 /* IPvFuture has to start with a 'v' or 'V'. */
1721 if(**ptr
!= 'v' && **ptr
!= 'V')
1724 /* Following the v there must be at least 1 hex digit. */
1726 if(!is_hexdigit(**ptr
)) {
1732 while(is_hexdigit(**ptr
))
1735 /* End of the hexdigit sequence must be a '.' */
1742 if(!is_unreserved(**ptr
) && !is_subdelim(**ptr
) && **ptr
!= ':') {
1748 while(is_unreserved(**ptr
) || is_subdelim(**ptr
) || **ptr
== ':')
1751 data
->host_type
= Uri_HOST_UNKNOWN
;
1753 TRACE("(%p %p %x): Parsed IPvFuture address %s len=%d\n", ptr
, data
, flags
,
1754 debugstr_wn(start
, *ptr
-start
), (int)(*ptr
-start
));
1759 /* IP-literal = "[" ( IPv6address / IPvFuture ) "]" */
1760 static BOOL
parse_ip_literal(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
, DWORD extras
) {
1763 if(**ptr
!= '[' && !(extras
& ALLOW_BRACKETLESS_IP_LITERAL
)) {
1766 } else if(**ptr
== '[')
1769 if(!parse_ipv6address(ptr
, data
, flags
)) {
1770 if(extras
& SKIP_IP_FUTURE_CHECK
|| !parse_ipvfuture(ptr
, data
, flags
)) {
1777 if(**ptr
!= ']' && !(extras
& ALLOW_BRACKETLESS_IP_LITERAL
)) {
1781 } else if(!**ptr
&& extras
& ALLOW_BRACKETLESS_IP_LITERAL
) {
1782 /* The IP literal didn't contain brackets and was followed by
1783 * a NULL terminator, so no reason to even check the port.
1785 data
->host_len
= *ptr
- data
->host
;
1792 /* If a valid port is not found, then let it trickle down to
1795 if(!parse_port(ptr
, data
, flags
)) {
1801 data
->host_len
= *ptr
- data
->host
;
1806 /* Parses the host information from the URI.
1808 * host = IP-literal / IPv4address / reg-name
1810 static BOOL
parse_host(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
, DWORD extras
) {
1811 if(!parse_ip_literal(ptr
, data
, flags
, extras
)) {
1812 if(!parse_ipv4address(ptr
, data
, flags
)) {
1813 if(!parse_reg_name(ptr
, data
, flags
, extras
)) {
1814 TRACE("(%p %p %x %x): Malformed URI, Unknown host type.\n",
1815 ptr
, data
, flags
, extras
);
1824 /* Parses the authority information from the URI.
1826 * authority = [ userinfo "@" ] host [ ":" port ]
1828 static BOOL
parse_authority(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
) {
1829 parse_userinfo(ptr
, data
, flags
);
1831 /* Parsing the port will happen during one of the host parsing
1832 * routines (if the URI has a port).
1834 if(!parse_host(ptr
, data
, flags
, 0))
1840 /* Attempts to parse the path information of a hierarchical URI. */
1841 static BOOL
parse_path_hierarchical(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
) {
1842 const WCHAR
*start
= *ptr
;
1843 static const WCHAR slash
[] = {'/',0};
1844 const BOOL is_file
= data
->scheme_type
== URL_SCHEME_FILE
;
1846 if(is_path_delim(**ptr
)) {
1847 if(data
->scheme_type
== URL_SCHEME_WILDCARD
&& !data
->must_have_path
) {
1850 } else if(!(flags
& Uri_CREATE_NO_CANONICALIZE
)) {
1851 /* If the path component is empty, then a '/' is added. */
1856 while(!is_path_delim(**ptr
)) {
1857 if(**ptr
== '%' && data
->scheme_type
!= URL_SCHEME_UNKNOWN
&& !is_file
) {
1858 if(!check_pct_encoded(ptr
)) {
1863 } else if(is_forbidden_dos_path_char(**ptr
) && is_file
&&
1864 (flags
& Uri_CREATE_FILE_USE_DOS_PATH
)) {
1865 /* File schemes with USE_DOS_PATH set aren't allowed to have
1866 * a '<' or '>' or '\"' appear in them.
1870 } else if(**ptr
== '\\') {
1871 /* Not allowed to have a backslash if NO_CANONICALIZE is set
1872 * and the scheme is known type (but not a file scheme).
1874 if(flags
& Uri_CREATE_NO_CANONICALIZE
) {
1875 if(data
->scheme_type
!= URL_SCHEME_FILE
&&
1876 data
->scheme_type
!= URL_SCHEME_UNKNOWN
) {
1886 /* The only time a URI doesn't have a path is when
1887 * the NO_CANONICALIZE flag is set and the raw URI
1888 * didn't contain one.
1895 data
->path_len
= *ptr
- start
;
1900 TRACE("(%p %p %x): Parsed path %s len=%d\n", ptr
, data
, flags
,
1901 debugstr_wn(data
->path
, data
->path_len
), data
->path_len
);
1903 TRACE("(%p %p %x): The URI contained no path\n", ptr
, data
, flags
);
1908 /* Parses the path of an opaque URI (much less strict then the parser
1909 * for a hierarchical URI).
1912 * Windows allows invalid % encoded data to appear in opaque URI paths
1913 * for unknown scheme types.
1915 * File schemes with USE_DOS_PATH set aren't allowed to have '<', '>', or '\"'
1918 static BOOL
parse_path_opaque(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
) {
1919 const BOOL known_scheme
= data
->scheme_type
!= URL_SCHEME_UNKNOWN
;
1920 const BOOL is_file
= data
->scheme_type
== URL_SCHEME_FILE
;
1924 while(!is_path_delim(**ptr
)) {
1925 if(**ptr
== '%' && known_scheme
) {
1926 if(!check_pct_encoded(ptr
)) {
1932 } else if(is_forbidden_dos_path_char(**ptr
) && is_file
&&
1933 (flags
& Uri_CREATE_FILE_USE_DOS_PATH
)) {
1942 data
->path_len
= *ptr
- data
->path
;
1943 TRACE("(%p %p %x): Parsed opaque URI path %s len=%d\n", ptr
, data
, flags
,
1944 debugstr_wn(data
->path
, data
->path_len
), data
->path_len
);
1948 /* Determines how the URI should be parsed after the scheme information.
1950 * If the scheme is followed by "//", then it is treated as a hierarchical URI
1951 * which then the authority and path information will be parsed out. Otherwise, the
1952 * URI will be treated as an opaque URI which the authority information is not parsed
1955 * RFC 3896 definition of hier-part:
1957 * hier-part = "//" authority path-abempty
1962 * MSDN opaque URI definition:
1963 * scheme ":" path [ "#" fragment ]
1966 * If the URI is of an unknown scheme type and has a "//" following the scheme then it
1967 * is treated as a hierarchical URI, but, if the CREATE_NO_CRACK_UNKNOWN_SCHEMES flag is
1968 * set then it is considered an opaque URI regardless of what follows the scheme information
1969 * (per MSDN documentation).
1971 static BOOL
parse_hierpart(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
) {
1972 const WCHAR
*start
= *ptr
;
1974 data
->must_have_path
= FALSE
;
1976 /* For javascript: URIs, simply set everything as a path */
1977 if(data
->scheme_type
== URL_SCHEME_JAVASCRIPT
) {
1979 data
->path_len
= strlenW(*ptr
);
1980 data
->is_opaque
= TRUE
;
1981 *ptr
+= data
->path_len
;
1985 /* Checks if the authority information needs to be parsed. */
1986 if(is_hierarchical_uri(ptr
, data
)) {
1987 /* Only treat it as a hierarchical URI if the scheme_type is known or
1988 * the Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES flag is not set.
1990 if(data
->scheme_type
!= URL_SCHEME_UNKNOWN
||
1991 !(flags
& Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES
)) {
1992 TRACE("(%p %p %x): Treating URI as an hierarchical URI.\n", ptr
, data
, flags
);
1993 data
->is_opaque
= FALSE
;
1995 if(data
->scheme_type
== URL_SCHEME_WILDCARD
&& !data
->has_implicit_scheme
) {
1996 if(**ptr
== '/' && *(*ptr
+1) == '/') {
1997 data
->must_have_path
= TRUE
;
2002 /* TODO: Handle hierarchical URI's, parse authority then parse the path. */
2003 if(!parse_authority(ptr
, data
, flags
))
2006 return parse_path_hierarchical(ptr
, data
, flags
);
2008 /* Reset ptr to its starting position so opaque path parsing
2009 * begins at the correct location.
2014 /* If it reaches here, then the URI will be treated as an opaque
2018 TRACE("(%p %p %x): Treating URI as an opaque URI.\n", ptr
, data
, flags
);
2020 data
->is_opaque
= TRUE
;
2021 if(!parse_path_opaque(ptr
, data
, flags
))
2027 /* Attempts to parse the query string from the URI.
2030 * If NO_DECODE_EXTRA_INFO flag is set, then invalid percent encoded
2031 * data is allowed to appear in the query string. For unknown scheme types
2032 * invalid percent encoded data is allowed to appear regardless.
2034 static BOOL
parse_query(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
) {
2035 const BOOL known_scheme
= data
->scheme_type
!= URL_SCHEME_UNKNOWN
;
2038 TRACE("(%p %p %x): URI didn't contain a query string.\n", ptr
, data
, flags
);
2045 while(**ptr
&& **ptr
!= '#') {
2046 if(**ptr
== '%' && known_scheme
&&
2047 !(flags
& Uri_CREATE_NO_DECODE_EXTRA_INFO
)) {
2048 if(!check_pct_encoded(ptr
)) {
2059 data
->query_len
= *ptr
- data
->query
;
2061 TRACE("(%p %p %x): Parsed query string %s len=%d\n", ptr
, data
, flags
,
2062 debugstr_wn(data
->query
, data
->query_len
), data
->query_len
);
2066 /* Attempts to parse the fragment from the URI.
2069 * If NO_DECODE_EXTRA_INFO flag is set, then invalid percent encoded
2070 * data is allowed to appear in the query string. For unknown scheme types
2071 * invalid percent encoded data is allowed to appear regardless.
2073 static BOOL
parse_fragment(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
) {
2074 const BOOL known_scheme
= data
->scheme_type
!= URL_SCHEME_UNKNOWN
;
2077 TRACE("(%p %p %x): URI didn't contain a fragment.\n", ptr
, data
, flags
);
2081 data
->fragment
= *ptr
;
2085 if(**ptr
== '%' && known_scheme
&&
2086 !(flags
& Uri_CREATE_NO_DECODE_EXTRA_INFO
)) {
2087 if(!check_pct_encoded(ptr
)) {
2088 *ptr
= data
->fragment
;
2089 data
->fragment
= NULL
;
2098 data
->fragment_len
= *ptr
- data
->fragment
;
2100 TRACE("(%p %p %x): Parsed fragment %s len=%d\n", ptr
, data
, flags
,
2101 debugstr_wn(data
->fragment
, data
->fragment_len
), data
->fragment_len
);
2105 /* Parses and validates the components of the specified by data->uri
2106 * and stores the information it parses into 'data'.
2108 * Returns TRUE if it successfully parsed the URI. False otherwise.
2110 static BOOL
parse_uri(parse_data
*data
, DWORD flags
) {
2117 TRACE("(%p %x): BEGINNING TO PARSE URI %s.\n", data
, flags
, debugstr_w(data
->uri
));
2119 if(!parse_scheme(pptr
, data
, flags
, 0))
2122 if(!parse_hierpart(pptr
, data
, flags
))
2125 if(!parse_query(pptr
, data
, flags
))
2128 if(!parse_fragment(pptr
, data
, flags
))
2131 TRACE("(%p %x): FINISHED PARSING URI.\n", data
, flags
);
2135 static BOOL
canonicalize_username(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
2138 if(!data
->username
) {
2139 uri
->userinfo_start
= -1;
2143 uri
->userinfo_start
= uri
->canon_len
;
2144 for(ptr
= data
->username
; ptr
< data
->username
+data
->username_len
; ++ptr
) {
2146 /* Only decode % encoded values for known scheme types. */
2147 if(data
->scheme_type
!= URL_SCHEME_UNKNOWN
) {
2148 /* See if the value really needs decoding. */
2149 WCHAR val
= decode_pct_val(ptr
);
2150 if(is_unreserved(val
)) {
2152 uri
->canon_uri
[uri
->canon_len
] = val
;
2156 /* Move pass the hex characters. */
2161 } else if(!is_reserved(*ptr
) && !is_unreserved(*ptr
) && *ptr
!= '\\') {
2162 /* Only percent encode forbidden characters if the NO_ENCODE_FORBIDDEN_CHARACTERS flag
2165 if(!(flags
& Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS
)) {
2167 pct_encode_val(*ptr
, uri
->canon_uri
+ uri
->canon_len
);
2169 uri
->canon_len
+= 3;
2175 /* Nothing special, so just copy the character over. */
2176 uri
->canon_uri
[uri
->canon_len
] = *ptr
;
2183 static BOOL
canonicalize_password(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
2186 if(!data
->password
) {
2187 uri
->userinfo_split
= -1;
2191 if(uri
->userinfo_start
== -1)
2192 /* Has a password, but, doesn't have a username. */
2193 uri
->userinfo_start
= uri
->canon_len
;
2195 uri
->userinfo_split
= uri
->canon_len
- uri
->userinfo_start
;
2197 /* Add the ':' to the userinfo component. */
2199 uri
->canon_uri
[uri
->canon_len
] = ':';
2202 for(ptr
= data
->password
; ptr
< data
->password
+data
->password_len
; ++ptr
) {
2204 /* Only decode % encoded values for known scheme types. */
2205 if(data
->scheme_type
!= URL_SCHEME_UNKNOWN
) {
2206 /* See if the value really needs decoding. */
2207 WCHAR val
= decode_pct_val(ptr
);
2208 if(is_unreserved(val
)) {
2210 uri
->canon_uri
[uri
->canon_len
] = val
;
2214 /* Move pass the hex characters. */
2219 } else if(!is_reserved(*ptr
) && !is_unreserved(*ptr
) && *ptr
!= '\\') {
2220 /* Only percent encode forbidden characters if the NO_ENCODE_FORBIDDEN_CHARACTERS flag
2223 if(!(flags
& Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS
)) {
2225 pct_encode_val(*ptr
, uri
->canon_uri
+ uri
->canon_len
);
2227 uri
->canon_len
+= 3;
2233 /* Nothing special, so just copy the character over. */
2234 uri
->canon_uri
[uri
->canon_len
] = *ptr
;
2241 /* Canonicalizes the userinfo of the URI represented by the parse_data.
2243 * Canonicalization of the userinfo is a simple process. If there are any percent
2244 * encoded characters that fall in the "unreserved" character set, they are decoded
2245 * to their actual value. If a character is not in the "unreserved" or "reserved" sets
2246 * then it is percent encoded. Other than that the characters are copied over without
2249 static BOOL
canonicalize_userinfo(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
2250 uri
->userinfo_start
= uri
->userinfo_split
= -1;
2251 uri
->userinfo_len
= 0;
2253 if(!data
->username
&& !data
->password
)
2254 /* URI doesn't have userinfo, so nothing to do here. */
2257 if(!canonicalize_username(data
, uri
, flags
, computeOnly
))
2260 if(!canonicalize_password(data
, uri
, flags
, computeOnly
))
2263 uri
->userinfo_len
= uri
->canon_len
- uri
->userinfo_start
;
2265 TRACE("(%p %p %x %d): Canonicalized userinfo, userinfo_start=%d, userinfo=%s, userinfo_split=%d userinfo_len=%d.\n",
2266 data
, uri
, flags
, computeOnly
, uri
->userinfo_start
, debugstr_wn(uri
->canon_uri
+ uri
->userinfo_start
, uri
->userinfo_len
),
2267 uri
->userinfo_split
, uri
->userinfo_len
);
2269 /* Now insert the '@' after the userinfo. */
2271 uri
->canon_uri
[uri
->canon_len
] = '@';
2277 /* Attempts to canonicalize a reg_name.
2279 * Things that happen:
2280 * 1) If Uri_CREATE_NO_CANONICALIZE flag is not set, then the reg_name is
2281 * lower cased. Unless it's an unknown scheme type, which case it's
2282 * no lower cased regardless.
2284 * 2) Unreserved % encoded characters are decoded for known
2287 * 3) Forbidden characters are % encoded as long as
2288 * Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS flag is not set and
2289 * it isn't an unknown scheme type.
2291 * 4) If it's a file scheme and the host is "localhost" it's removed.
2293 * 5) If it's a file scheme and Uri_CREATE_FILE_USE_DOS_PATH is set,
2294 * then the UNC path characters are added before the host name.
2296 static BOOL
canonicalize_reg_name(const parse_data
*data
, Uri
*uri
,
2297 DWORD flags
, BOOL computeOnly
) {
2298 static const WCHAR localhostW
[] =
2299 {'l','o','c','a','l','h','o','s','t',0};
2301 const BOOL known_scheme
= data
->scheme_type
!= URL_SCHEME_UNKNOWN
;
2303 if(data
->scheme_type
== URL_SCHEME_FILE
&&
2304 data
->host_len
== lstrlenW(localhostW
)) {
2305 if(!StrCmpNIW(data
->host
, localhostW
, data
->host_len
)) {
2306 uri
->host_start
= -1;
2308 uri
->host_type
= Uri_HOST_UNKNOWN
;
2313 if(data
->scheme_type
== URL_SCHEME_FILE
&& flags
& Uri_CREATE_FILE_USE_DOS_PATH
) {
2315 uri
->canon_uri
[uri
->canon_len
] = '\\';
2316 uri
->canon_uri
[uri
->canon_len
+1] = '\\';
2318 uri
->canon_len
+= 2;
2319 uri
->authority_start
= uri
->canon_len
;
2322 uri
->host_start
= uri
->canon_len
;
2324 for(ptr
= data
->host
; ptr
< data
->host
+data
->host_len
; ++ptr
) {
2325 if(*ptr
== '%' && known_scheme
) {
2326 WCHAR val
= decode_pct_val(ptr
);
2327 if(is_unreserved(val
)) {
2328 /* If NO_CANONICALIZE is not set, then windows lower cases the
2331 if(!(flags
& Uri_CREATE_NO_CANONICALIZE
) && isupperW(val
)) {
2333 uri
->canon_uri
[uri
->canon_len
] = tolowerW(val
);
2336 uri
->canon_uri
[uri
->canon_len
] = val
;
2340 /* Skip past the % encoded character. */
2344 /* Just copy the % over. */
2346 uri
->canon_uri
[uri
->canon_len
] = *ptr
;
2349 } else if(*ptr
== '\\') {
2350 /* Only unknown scheme types could have made it here with a '\\' in the host name. */
2352 uri
->canon_uri
[uri
->canon_len
] = *ptr
;
2354 } else if(!(flags
& Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS
) &&
2355 !is_unreserved(*ptr
) && !is_reserved(*ptr
) && known_scheme
) {
2357 pct_encode_val(*ptr
, uri
->canon_uri
+uri
->canon_len
);
2359 /* The percent encoded value gets lower cased also. */
2360 if(!(flags
& Uri_CREATE_NO_CANONICALIZE
)) {
2361 uri
->canon_uri
[uri
->canon_len
+1] = tolowerW(uri
->canon_uri
[uri
->canon_len
+1]);
2362 uri
->canon_uri
[uri
->canon_len
+2] = tolowerW(uri
->canon_uri
[uri
->canon_len
+2]);
2366 uri
->canon_len
+= 3;
2369 if(!(flags
& Uri_CREATE_NO_CANONICALIZE
) && known_scheme
)
2370 uri
->canon_uri
[uri
->canon_len
] = tolowerW(*ptr
);
2372 uri
->canon_uri
[uri
->canon_len
] = *ptr
;
2379 uri
->host_len
= uri
->canon_len
- uri
->host_start
;
2382 TRACE("(%p %p %x %d): Canonicalize reg_name=%s len=%d\n", data
, uri
, flags
,
2383 computeOnly
, debugstr_wn(uri
->canon_uri
+uri
->host_start
, uri
->host_len
),
2387 find_domain_name(uri
->canon_uri
+uri
->host_start
, uri
->host_len
,
2388 &(uri
->domain_offset
));
2393 /* Attempts to canonicalize an implicit IPv4 address. */
2394 static BOOL
canonicalize_implicit_ipv4address(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
2395 uri
->host_start
= uri
->canon_len
;
2397 TRACE("%u\n", data
->implicit_ipv4
);
2398 /* For unknown scheme types Windows doesn't convert
2399 * the value into an IP address, but it still considers
2400 * it an IPv4 address.
2402 if(data
->scheme_type
== URL_SCHEME_UNKNOWN
) {
2404 memcpy(uri
->canon_uri
+uri
->canon_len
, data
->host
, data
->host_len
*sizeof(WCHAR
));
2405 uri
->canon_len
+= data
->host_len
;
2408 uri
->canon_len
+= ui2ipv4(uri
->canon_uri
+uri
->canon_len
, data
->implicit_ipv4
);
2410 uri
->canon_len
+= ui2ipv4(NULL
, data
->implicit_ipv4
);
2413 uri
->host_len
= uri
->canon_len
- uri
->host_start
;
2414 uri
->host_type
= Uri_HOST_IPV4
;
2417 TRACE("%p %p %x %d): Canonicalized implicit IP address=%s len=%d\n",
2418 data
, uri
, flags
, computeOnly
,
2419 debugstr_wn(uri
->canon_uri
+uri
->host_start
, uri
->host_len
),
2425 /* Attempts to canonicalize an IPv4 address.
2427 * If the parse_data represents a URI that has an implicit IPv4 address
2428 * (ex. http://256/, this function will convert 256 into 0.0.1.0). If
2429 * the implicit IP address exceeds the value of UINT_MAX (maximum value
2430 * for an IPv4 address) it's canonicalized as if it were a reg-name.
2432 * If the parse_data contains a partial or full IPv4 address it normalizes it.
2433 * A partial IPv4 address is something like "192.0" and would be normalized to
2434 * "192.0.0.0". With a full (or partial) IPv4 address like "192.002.01.003" would
2435 * be normalized to "192.2.1.3".
2438 * Windows ONLY normalizes IPv4 address for known scheme types (one that isn't
2439 * URL_SCHEME_UNKNOWN). For unknown scheme types, it simply copies the data from
2440 * the original URI into the canonicalized URI, but, it still recognizes URI's
2441 * host type as HOST_IPV4.
2443 static BOOL
canonicalize_ipv4address(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
2444 if(data
->has_implicit_ip
)
2445 return canonicalize_implicit_ipv4address(data
, uri
, flags
, computeOnly
);
2447 uri
->host_start
= uri
->canon_len
;
2449 /* Windows only normalizes for known scheme types. */
2450 if(data
->scheme_type
!= URL_SCHEME_UNKNOWN
) {
2451 /* parse_data contains a partial or full IPv4 address, so normalize it. */
2452 DWORD i
, octetDigitCount
= 0, octetCount
= 0;
2453 BOOL octetHasDigit
= FALSE
;
2455 for(i
= 0; i
< data
->host_len
; ++i
) {
2456 if(data
->host
[i
] == '0' && !octetHasDigit
) {
2457 /* Can ignore leading zeros if:
2458 * 1) It isn't the last digit of the octet.
2459 * 2) i+1 != data->host_len
2462 if(octetDigitCount
== 2 ||
2463 i
+1 == data
->host_len
||
2464 data
->host
[i
+1] == '.') {
2466 uri
->canon_uri
[uri
->canon_len
] = data
->host
[i
];
2468 TRACE("Adding zero\n");
2470 } else if(data
->host
[i
] == '.') {
2472 uri
->canon_uri
[uri
->canon_len
] = data
->host
[i
];
2475 octetDigitCount
= 0;
2476 octetHasDigit
= FALSE
;
2480 uri
->canon_uri
[uri
->canon_len
] = data
->host
[i
];
2484 octetHasDigit
= TRUE
;
2488 /* Make sure the canonicalized IP address has 4 dec-octets.
2489 * If doesn't add "0" ones until there is 4;
2491 for( ; octetCount
< 3; ++octetCount
) {
2493 uri
->canon_uri
[uri
->canon_len
] = '.';
2494 uri
->canon_uri
[uri
->canon_len
+1] = '0';
2497 uri
->canon_len
+= 2;
2500 /* Windows doesn't normalize addresses in unknown schemes. */
2502 memcpy(uri
->canon_uri
+uri
->canon_len
, data
->host
, data
->host_len
*sizeof(WCHAR
));
2503 uri
->canon_len
+= data
->host_len
;
2506 uri
->host_len
= uri
->canon_len
- uri
->host_start
;
2508 TRACE("(%p %p %x %d): Canonicalized IPv4 address, ip=%s len=%d\n",
2509 data
, uri
, flags
, computeOnly
,
2510 debugstr_wn(uri
->canon_uri
+uri
->host_start
, uri
->host_len
),
2517 /* Attempts to canonicalize the IPv6 address of the URI.
2519 * Multiple things happen during the canonicalization of an IPv6 address:
2520 * 1) Any leading zero's in a h16 component are removed.
2521 * Ex: [0001:0022::] -> [1:22::]
2523 * 2) The longest sequence of zero h16 components are compressed
2524 * into a "::" (elision). If there's a tie, the first is chosen.
2526 * Ex: [0:0:0:0:1:6:7:8] -> [::1:6:7:8]
2527 * [0:0:0:0:1:2::] -> [::1:2:0:0]
2528 * [0:0:1:2:0:0:7:8] -> [::1:2:0:0:7:8]
2530 * 3) If an IPv4 address is attached to the IPv6 address, it's
2532 * Ex: [::001.002.022.000] -> [::1.2.22.0]
2534 * 4) If an elision is present, but, only represents one h16 component
2537 * Ex: [1::2:3:4:5:6:7] -> [1:0:2:3:4:5:6:7]
2539 * 5) If the IPv6 address contains an IPv4 address and there exists
2540 * at least 1 non-zero h16 component the IPv4 address is converted
2541 * into two h16 components, otherwise it's normalized and kept as is.
2543 * Ex: [::192.200.003.4] -> [::192.200.3.4]
2544 * [ffff::192.200.003.4] -> [ffff::c0c8:3041]
2547 * For unknown scheme types Windows simply copies the address over without any
2550 * IPv4 address can be included in an elision if all its components are 0's.
2552 static BOOL
canonicalize_ipv6address(const parse_data
*data
, Uri
*uri
,
2553 DWORD flags
, BOOL computeOnly
) {
2554 uri
->host_start
= uri
->canon_len
;
2556 if(data
->scheme_type
== URL_SCHEME_UNKNOWN
) {
2558 memcpy(uri
->canon_uri
+uri
->canon_len
, data
->host
, data
->host_len
*sizeof(WCHAR
));
2559 uri
->canon_len
+= data
->host_len
;
2563 DWORD i
, elision_len
;
2565 if(!ipv6_to_number(&(data
->ipv6_address
), values
)) {
2566 TRACE("(%p %p %x %d): Failed to compute numerical value for IPv6 address.\n",
2567 data
, uri
, flags
, computeOnly
);
2572 uri
->canon_uri
[uri
->canon_len
] = '[';
2575 /* Find where the elision should occur (if any). */
2576 compute_elision_location(&(data
->ipv6_address
), values
, &elision_start
, &elision_len
);
2578 TRACE("%p %p %x %d): Elision starts at %d, len=%u\n", data
, uri
, flags
,
2579 computeOnly
, elision_start
, elision_len
);
2581 for(i
= 0; i
< 8; ++i
) {
2582 BOOL in_elision
= (elision_start
> -1 && i
>= elision_start
&&
2583 i
< elision_start
+elision_len
);
2584 BOOL do_ipv4
= (i
== 6 && data
->ipv6_address
.ipv4
&& !in_elision
&&
2585 data
->ipv6_address
.h16_count
== 0);
2587 if(i
== elision_start
) {
2589 uri
->canon_uri
[uri
->canon_len
] = ':';
2590 uri
->canon_uri
[uri
->canon_len
+1] = ':';
2592 uri
->canon_len
+= 2;
2595 /* We can ignore the current component if we're in the elision. */
2599 /* We only add a ':' if we're not at i == 0, or when we're at
2600 * the very end of elision range since the ':' colon was handled
2601 * earlier. Otherwise we would end up with ":::" after elision.
2603 if(i
!= 0 && !(elision_start
> -1 && i
== elision_start
+elision_len
)) {
2605 uri
->canon_uri
[uri
->canon_len
] = ':';
2613 /* Combine the two parts of the IPv4 address values. */
2619 len
= ui2ipv4(uri
->canon_uri
+uri
->canon_len
, val
);
2621 len
= ui2ipv4(NULL
, val
);
2623 uri
->canon_len
+= len
;
2626 /* Write a regular h16 component to the URI. */
2628 /* Short circuit for the trivial case. */
2629 if(values
[i
] == 0) {
2631 uri
->canon_uri
[uri
->canon_len
] = '0';
2634 static const WCHAR formatW
[] = {'%','x',0};
2637 uri
->canon_len
+= sprintfW(uri
->canon_uri
+uri
->canon_len
,
2638 formatW
, values
[i
]);
2641 uri
->canon_len
+= sprintfW(tmp
, formatW
, values
[i
]);
2647 /* Add the closing ']'. */
2649 uri
->canon_uri
[uri
->canon_len
] = ']';
2653 uri
->host_len
= uri
->canon_len
- uri
->host_start
;
2656 TRACE("(%p %p %x %d): Canonicalized IPv6 address %s, len=%d\n", data
, uri
, flags
,
2657 computeOnly
, debugstr_wn(uri
->canon_uri
+uri
->host_start
, uri
->host_len
),
2663 /* Attempts to canonicalize the host of the URI (if any). */
2664 static BOOL
canonicalize_host(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
2665 uri
->host_start
= -1;
2667 uri
->domain_offset
= -1;
2670 switch(data
->host_type
) {
2672 uri
->host_type
= Uri_HOST_DNS
;
2673 if(!canonicalize_reg_name(data
, uri
, flags
, computeOnly
))
2678 uri
->host_type
= Uri_HOST_IPV4
;
2679 if(!canonicalize_ipv4address(data
, uri
, flags
, computeOnly
))
2684 if(!canonicalize_ipv6address(data
, uri
, flags
, computeOnly
))
2687 uri
->host_type
= Uri_HOST_IPV6
;
2689 case Uri_HOST_UNKNOWN
:
2690 if(data
->host_len
> 0 || data
->scheme_type
!= URL_SCHEME_FILE
) {
2691 uri
->host_start
= uri
->canon_len
;
2693 /* Nothing happens to unknown host types. */
2695 memcpy(uri
->canon_uri
+uri
->canon_len
, data
->host
, data
->host_len
*sizeof(WCHAR
));
2696 uri
->canon_len
+= data
->host_len
;
2697 uri
->host_len
= data
->host_len
;
2700 uri
->host_type
= Uri_HOST_UNKNOWN
;
2703 FIXME("(%p %p %x %d): Canonicalization for host type %d not supported.\n", data
,
2704 uri
, flags
, computeOnly
, data
->host_type
);
2712 static BOOL
canonicalize_port(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
2713 BOOL has_default_port
= FALSE
;
2714 USHORT default_port
= 0;
2717 uri
->port_offset
= -1;
2719 /* Check if the scheme has a default port. */
2720 for(i
= 0; i
< sizeof(default_ports
)/sizeof(default_ports
[0]); ++i
) {
2721 if(default_ports
[i
].scheme
== data
->scheme_type
) {
2722 has_default_port
= TRUE
;
2723 default_port
= default_ports
[i
].port
;
2728 uri
->has_port
= data
->has_port
|| has_default_port
;
2731 * 1) Has a port which is the default port.
2732 * 2) Has a port (not the default).
2733 * 3) Doesn't have a port, but, scheme has a default port.
2736 if(has_default_port
&& data
->has_port
&& data
->port_value
== default_port
) {
2737 /* If it's the default port and this flag isn't set, don't do anything. */
2738 if(flags
& Uri_CREATE_NO_CANONICALIZE
) {
2739 uri
->port_offset
= uri
->canon_len
-uri
->authority_start
;
2741 uri
->canon_uri
[uri
->canon_len
] = ':';
2745 /* Copy the original port over. */
2747 memcpy(uri
->canon_uri
+uri
->canon_len
, data
->port
, data
->port_len
*sizeof(WCHAR
));
2748 uri
->canon_len
+= data
->port_len
;
2751 uri
->canon_len
+= ui2str(uri
->canon_uri
+uri
->canon_len
, data
->port_value
);
2753 uri
->canon_len
+= ui2str(NULL
, data
->port_value
);
2757 uri
->port
= default_port
;
2758 } else if(data
->has_port
) {
2759 uri
->port_offset
= uri
->canon_len
-uri
->authority_start
;
2761 uri
->canon_uri
[uri
->canon_len
] = ':';
2764 if(flags
& Uri_CREATE_NO_CANONICALIZE
&& data
->port
) {
2765 /* Copy the original over without changes. */
2767 memcpy(uri
->canon_uri
+uri
->canon_len
, data
->port
, data
->port_len
*sizeof(WCHAR
));
2768 uri
->canon_len
+= data
->port_len
;
2771 uri
->canon_len
+= ui2str(uri
->canon_uri
+uri
->canon_len
, data
->port_value
);
2773 uri
->canon_len
+= ui2str(NULL
, data
->port_value
);
2776 uri
->port
= data
->port_value
;
2777 } else if(has_default_port
)
2778 uri
->port
= default_port
;
2783 /* Canonicalizes the authority of the URI represented by the parse_data. */
2784 static BOOL
canonicalize_authority(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
2785 uri
->authority_start
= uri
->canon_len
;
2786 uri
->authority_len
= 0;
2788 if(!canonicalize_userinfo(data
, uri
, flags
, computeOnly
))
2791 if(!canonicalize_host(data
, uri
, flags
, computeOnly
))
2794 if(!canonicalize_port(data
, uri
, flags
, computeOnly
))
2797 if(uri
->host_start
!= -1 || (data
->is_relative
&& (data
->password
|| data
->username
)))
2798 uri
->authority_len
= uri
->canon_len
- uri
->authority_start
;
2800 uri
->authority_start
= -1;
2805 /* Attempts to canonicalize the path of a hierarchical URI.
2807 * Things that happen:
2808 * 1). Forbidden characters are percent encoded, unless the NO_ENCODE_FORBIDDEN
2809 * flag is set or it's a file URI. Forbidden characters are always encoded
2810 * for file schemes regardless and forbidden characters are never encoded
2811 * for unknown scheme types.
2813 * 2). For known scheme types '\\' are changed to '/'.
2815 * 3). Percent encoded, unreserved characters are decoded to their actual values.
2816 * Unless the scheme type is unknown. For file schemes any percent encoded
2817 * character in the unreserved or reserved set is decoded.
2819 * 4). For File schemes if the path is starts with a drive letter and doesn't
2820 * start with a '/' then one is appended.
2821 * Ex: file://c:/test.mp3 -> file:///c:/test.mp3
2823 * 5). Dot segments are removed from the path for all scheme types
2824 * unless NO_CANONICALIZE flag is set. Dot segments aren't removed
2825 * for wildcard scheme types.
2828 * file://c:/test%20test -> file:///c:/test%2520test
2829 * file://c:/test%3Etest -> file:///c:/test%253Etest
2830 * if Uri_CREATE_FILE_USE_DOS_PATH is not set:
2831 * file:///c:/test%20test -> file:///c:/test%20test
2832 * file:///c:/test%test -> file:///c:/test%25test
2834 static DWORD
canonicalize_path_hierarchical(const WCHAR
*path
, DWORD path_len
, URL_SCHEME scheme_type
, BOOL has_host
, DWORD flags
,
2836 const BOOL known_scheme
= scheme_type
!= URL_SCHEME_UNKNOWN
;
2837 const BOOL is_file
= scheme_type
== URL_SCHEME_FILE
;
2838 const BOOL is_res
= scheme_type
== URL_SCHEME_RES
;
2840 BOOL escape_pct
= FALSE
;
2848 if(is_file
&& !has_host
) {
2849 /* Check if a '/' needs to be appended for the file scheme. */
2850 if(path_len
> 1 && is_drive_path(ptr
) && !(flags
& Uri_CREATE_FILE_USE_DOS_PATH
)) {
2852 ret_path
[len
] = '/';
2855 } else if(*ptr
== '/') {
2856 if(!(flags
& Uri_CREATE_FILE_USE_DOS_PATH
)) {
2857 /* Copy the extra '/' over. */
2859 ret_path
[len
] = '/';
2865 if(is_drive_path(ptr
)) {
2867 ret_path
[len
] = *ptr
;
2868 /* If there's a '|' after the drive letter, convert it to a ':'. */
2869 ret_path
[len
+1] = ':';
2876 if(!is_file
&& *path
&& *path
!= '/') {
2877 /* Prepend a '/' to the path if it doesn't have one. */
2879 ret_path
[len
] = '/';
2883 for(; ptr
< path
+path_len
; ++ptr
) {
2884 BOOL do_default_action
= TRUE
;
2886 if(*ptr
== '%' && !is_res
) {
2887 const WCHAR
*tmp
= ptr
;
2890 /* Check if the % represents a valid encoded char, or if it needs encoding. */
2891 BOOL force_encode
= !check_pct_encoded(&tmp
) && is_file
&& !(flags
&Uri_CREATE_FILE_USE_DOS_PATH
);
2892 val
= decode_pct_val(ptr
);
2894 if(force_encode
|| escape_pct
) {
2895 /* Escape the percent sign in the file URI. */
2897 pct_encode_val(*ptr
, ret_path
+len
);
2899 do_default_action
= FALSE
;
2900 } else if((is_unreserved(val
) && known_scheme
) ||
2901 (is_file
&& (is_unreserved(val
) || is_reserved(val
) ||
2902 (val
&& flags
&Uri_CREATE_FILE_USE_DOS_PATH
&& !is_forbidden_dos_path_char(val
))))) {
2904 ret_path
[len
] = val
;
2910 } else if(*ptr
== '/' && is_file
&& (flags
& Uri_CREATE_FILE_USE_DOS_PATH
)) {
2911 /* Convert the '/' back to a '\\'. */
2913 ret_path
[len
] = '\\';
2915 do_default_action
= FALSE
;
2916 } else if(*ptr
== '\\' && known_scheme
) {
2917 if(!(is_file
&& (flags
& Uri_CREATE_FILE_USE_DOS_PATH
))) {
2918 /* Convert '\\' into a '/'. */
2920 ret_path
[len
] = '/';
2922 do_default_action
= FALSE
;
2924 } else if(known_scheme
&& !is_res
&& !is_unreserved(*ptr
) && !is_reserved(*ptr
) &&
2925 (!(flags
& Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS
) || is_file
)) {
2926 if(!(is_file
&& (flags
& Uri_CREATE_FILE_USE_DOS_PATH
))) {
2927 /* Escape the forbidden character. */
2929 pct_encode_val(*ptr
, ret_path
+len
);
2931 do_default_action
= FALSE
;
2935 if(do_default_action
) {
2937 ret_path
[len
] = *ptr
;
2942 /* Removing the dot segments only happens when it's not in
2943 * computeOnly mode and it's not a wildcard scheme. File schemes
2944 * with USE_DOS_PATH set don't get dot segments removed.
2946 if(!(is_file
&& (flags
& Uri_CREATE_FILE_USE_DOS_PATH
)) &&
2947 scheme_type
!= URL_SCHEME_WILDCARD
) {
2948 if(!(flags
& Uri_CREATE_NO_CANONICALIZE
) && ret_path
) {
2949 /* Remove the dot segments (if any) and reset everything to the new
2952 len
= remove_dot_segments(ret_path
, len
);
2957 TRACE("Canonicalized path %s len=%d\n", debugstr_wn(ret_path
, len
), len
);
2961 /* Attempts to canonicalize the path for an opaque URI.
2963 * For known scheme types:
2964 * 1) forbidden characters are percent encoded if
2965 * NO_ENCODE_FORBIDDEN_CHARACTERS isn't set.
2967 * 2) Percent encoded, unreserved characters are decoded
2968 * to their actual values, for known scheme types.
2970 * 3) '\\' are changed to '/' for known scheme types
2971 * except for mailto schemes.
2973 * 4) For file schemes, if USE_DOS_PATH is set all '/'
2974 * are converted to backslashes.
2976 * 5) For file schemes, if USE_DOS_PATH isn't set all '\'
2977 * are converted to forward slashes.
2979 static BOOL
canonicalize_path_opaque(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
2981 const BOOL known_scheme
= data
->scheme_type
!= URL_SCHEME_UNKNOWN
;
2982 const BOOL is_file
= data
->scheme_type
== URL_SCHEME_FILE
;
2983 const BOOL is_mk
= data
->scheme_type
== URL_SCHEME_MK
;
2986 uri
->path_start
= -1;
2991 uri
->path_start
= uri
->canon_len
;
2994 /* hijack this flag for SCHEME_MK to tell the function when to start
2995 * converting slashes */
2996 flags
|= Uri_CREATE_FILE_USE_DOS_PATH
;
2999 /* For javascript: URIs, simply copy path part without any canonicalization */
3000 if(data
->scheme_type
== URL_SCHEME_JAVASCRIPT
) {
3002 memcpy(uri
->canon_uri
+uri
->canon_len
, data
->path
, data
->path_len
*sizeof(WCHAR
));
3003 uri
->path_len
= data
->path_len
;
3004 uri
->canon_len
+= data
->path_len
;
3008 /* Windows doesn't allow a "//" to appear after the scheme
3009 * of a URI, if it's an opaque URI.
3011 if(data
->scheme
&& *(data
->path
) == '/' && *(data
->path
+1) == '/') {
3012 /* So it inserts a "/." before the "//" if it exists. */
3014 uri
->canon_uri
[uri
->canon_len
] = '/';
3015 uri
->canon_uri
[uri
->canon_len
+1] = '.';
3018 uri
->canon_len
+= 2;
3021 for(ptr
= data
->path
; ptr
< data
->path
+data
->path_len
; ++ptr
) {
3022 BOOL do_default_action
= TRUE
;
3024 if(*ptr
== '%' && known_scheme
) {
3025 WCHAR val
= decode_pct_val(ptr
);
3027 if(is_unreserved(val
)) {
3029 uri
->canon_uri
[uri
->canon_len
] = val
;
3035 } else if(*ptr
== '/' && is_file
&& (flags
& Uri_CREATE_FILE_USE_DOS_PATH
)) {
3037 uri
->canon_uri
[uri
->canon_len
] = '\\';
3039 do_default_action
= FALSE
;
3040 } else if(*ptr
== '\\') {
3041 if((data
->is_relative
|| is_mk
|| is_file
) && !(flags
& Uri_CREATE_FILE_USE_DOS_PATH
)) {
3042 /* Convert to a '/'. */
3044 uri
->canon_uri
[uri
->canon_len
] = '/';
3046 do_default_action
= FALSE
;
3048 } else if(is_mk
&& *ptr
== ':' && ptr
+ 1 < data
->path
+ data
->path_len
&& *(ptr
+ 1) == ':') {
3049 flags
&= ~Uri_CREATE_FILE_USE_DOS_PATH
;
3050 } else if(known_scheme
&& !is_unreserved(*ptr
) && !is_reserved(*ptr
) &&
3051 !(flags
& Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS
)) {
3052 if(!(is_file
&& (flags
& Uri_CREATE_FILE_USE_DOS_PATH
))) {
3054 pct_encode_val(*ptr
, uri
->canon_uri
+uri
->canon_len
);
3055 uri
->canon_len
+= 3;
3056 do_default_action
= FALSE
;
3060 if(do_default_action
) {
3062 uri
->canon_uri
[uri
->canon_len
] = *ptr
;
3067 if(is_mk
&& !computeOnly
&& !(flags
& Uri_CREATE_NO_CANONICALIZE
)) {
3068 DWORD new_len
= remove_dot_segments(uri
->canon_uri
+ uri
->path_start
,
3069 uri
->canon_len
- uri
->path_start
);
3070 uri
->canon_len
= uri
->path_start
+ new_len
;
3073 uri
->path_len
= uri
->canon_len
- uri
->path_start
;
3076 TRACE("(%p %p %x %d): Canonicalized opaque URI path %s len=%d\n", data
, uri
, flags
, computeOnly
,
3077 debugstr_wn(uri
->canon_uri
+uri
->path_start
, uri
->path_len
), uri
->path_len
);
3081 /* Determines how the URI represented by the parse_data should be canonicalized.
3083 * Essentially, if the parse_data represents an hierarchical URI then it calls
3084 * canonicalize_authority and the canonicalization functions for the path. If the
3085 * URI is opaque it canonicalizes the path of the URI.
3087 static BOOL
canonicalize_hierpart(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
3088 if(!data
->is_opaque
|| (data
->is_relative
&& (data
->password
|| data
->username
))) {
3089 /* "//" is only added for non-wildcard scheme types.
3091 * A "//" is only added to a relative URI if it has a
3092 * host or port component (this only happens if a IUriBuilder
3093 * is generating an IUri).
3095 if((data
->is_relative
&& (data
->host
|| data
->has_port
)) ||
3096 (!data
->is_relative
&& data
->scheme_type
!= URL_SCHEME_WILDCARD
)) {
3097 if(data
->scheme_type
== URL_SCHEME_WILDCARD
)
3101 INT pos
= uri
->canon_len
;
3103 uri
->canon_uri
[pos
] = '/';
3104 uri
->canon_uri
[pos
+1] = '/';
3106 uri
->canon_len
+= 2;
3109 if(!canonicalize_authority(data
, uri
, flags
, computeOnly
))
3112 if(data
->is_relative
&& (data
->password
|| data
->username
)) {
3113 if(!canonicalize_path_opaque(data
, uri
, flags
, computeOnly
))
3117 uri
->path_start
= uri
->canon_len
;
3118 uri
->path_len
= canonicalize_path_hierarchical(data
->path
, data
->path_len
, data
->scheme_type
, data
->host_len
!= 0,
3119 flags
, computeOnly
? NULL
: uri
->canon_uri
+uri
->canon_len
);
3120 uri
->canon_len
+= uri
->path_len
;
3121 if(!computeOnly
&& !uri
->path_len
)
3122 uri
->path_start
= -1;
3125 /* Opaque URI's don't have an authority. */
3126 uri
->userinfo_start
= uri
->userinfo_split
= -1;
3127 uri
->userinfo_len
= 0;
3128 uri
->host_start
= -1;
3130 uri
->host_type
= Uri_HOST_UNKNOWN
;
3131 uri
->has_port
= FALSE
;
3132 uri
->authority_start
= -1;
3133 uri
->authority_len
= 0;
3134 uri
->domain_offset
= -1;
3135 uri
->port_offset
= -1;
3137 if(is_hierarchical_scheme(data
->scheme_type
)) {
3140 /* Absolute URIs aren't displayed for known scheme types
3141 * which should be hierarchical URIs.
3143 uri
->display_modifiers
|= URI_DISPLAY_NO_ABSOLUTE_URI
;
3145 /* Windows also sets the port for these (if they have one). */
3146 for(i
= 0; i
< sizeof(default_ports
)/sizeof(default_ports
[0]); ++i
) {
3147 if(data
->scheme_type
== default_ports
[i
].scheme
) {
3148 uri
->has_port
= TRUE
;
3149 uri
->port
= default_ports
[i
].port
;
3155 if(!canonicalize_path_opaque(data
, uri
, flags
, computeOnly
))
3159 if(uri
->path_start
> -1 && !computeOnly
)
3160 /* Finding file extensions happens for both types of URIs. */
3161 uri
->extension_offset
= find_file_extension(uri
->canon_uri
+uri
->path_start
, uri
->path_len
);
3163 uri
->extension_offset
= -1;
3168 /* Attempts to canonicalize the query string of the URI.
3170 * Things that happen:
3171 * 1) For known scheme types forbidden characters
3172 * are percent encoded, unless the NO_DECODE_EXTRA_INFO flag is set
3173 * or NO_ENCODE_FORBIDDEN_CHARACTERS is set.
3175 * 2) For known scheme types, percent encoded, unreserved characters
3176 * are decoded as long as the NO_DECODE_EXTRA_INFO flag isn't set.
3178 static BOOL
canonicalize_query(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
3179 const WCHAR
*ptr
, *end
;
3180 const BOOL known_scheme
= data
->scheme_type
!= URL_SCHEME_UNKNOWN
;
3183 uri
->query_start
= -1;
3188 uri
->query_start
= uri
->canon_len
;
3190 end
= data
->query
+data
->query_len
;
3191 for(ptr
= data
->query
; ptr
< end
; ++ptr
) {
3193 if(known_scheme
&& !(flags
& Uri_CREATE_NO_DECODE_EXTRA_INFO
)) {
3194 WCHAR val
= decode_pct_val(ptr
);
3195 if(is_unreserved(val
)) {
3197 uri
->canon_uri
[uri
->canon_len
] = val
;
3204 } else if(known_scheme
&& !is_unreserved(*ptr
) && !is_reserved(*ptr
)) {
3205 if(!(flags
& Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS
) &&
3206 !(flags
& Uri_CREATE_NO_DECODE_EXTRA_INFO
)) {
3208 pct_encode_val(*ptr
, uri
->canon_uri
+uri
->canon_len
);
3209 uri
->canon_len
+= 3;
3215 uri
->canon_uri
[uri
->canon_len
] = *ptr
;
3219 uri
->query_len
= uri
->canon_len
- uri
->query_start
;
3222 TRACE("(%p %p %x %d): Canonicalized query string %s len=%d\n", data
, uri
, flags
,
3223 computeOnly
, debugstr_wn(uri
->canon_uri
+uri
->query_start
, uri
->query_len
),
3228 static BOOL
canonicalize_fragment(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
3229 const WCHAR
*ptr
, *end
;
3230 const BOOL known_scheme
= data
->scheme_type
!= URL_SCHEME_UNKNOWN
;
3232 if(!data
->fragment
) {
3233 uri
->fragment_start
= -1;
3234 uri
->fragment_len
= 0;
3238 uri
->fragment_start
= uri
->canon_len
;
3240 end
= data
->fragment
+ data
->fragment_len
;
3241 for(ptr
= data
->fragment
; ptr
< end
; ++ptr
) {
3243 if(known_scheme
&& !(flags
& Uri_CREATE_NO_DECODE_EXTRA_INFO
)) {
3244 WCHAR val
= decode_pct_val(ptr
);
3245 if(is_unreserved(val
)) {
3247 uri
->canon_uri
[uri
->canon_len
] = val
;
3254 } else if(known_scheme
&& !is_unreserved(*ptr
) && !is_reserved(*ptr
)) {
3255 if(!(flags
& Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS
) &&
3256 !(flags
& Uri_CREATE_NO_DECODE_EXTRA_INFO
)) {
3258 pct_encode_val(*ptr
, uri
->canon_uri
+uri
->canon_len
);
3259 uri
->canon_len
+= 3;
3265 uri
->canon_uri
[uri
->canon_len
] = *ptr
;
3269 uri
->fragment_len
= uri
->canon_len
- uri
->fragment_start
;
3272 TRACE("(%p %p %x %d): Canonicalized fragment %s len=%d\n", data
, uri
, flags
,
3273 computeOnly
, debugstr_wn(uri
->canon_uri
+uri
->fragment_start
, uri
->fragment_len
),
3278 /* Canonicalizes the scheme information specified in the parse_data using the specified flags. */
3279 static BOOL
canonicalize_scheme(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
3280 uri
->scheme_start
= -1;
3281 uri
->scheme_len
= 0;
3284 /* The only type of URI that doesn't have to have a scheme is a relative
3287 if(!data
->is_relative
) {
3288 FIXME("(%p %p %x): Unable to determine the scheme type of %s.\n", data
,
3289 uri
, flags
, debugstr_w(data
->uri
));
3295 INT pos
= uri
->canon_len
;
3297 for(i
= 0; i
< data
->scheme_len
; ++i
) {
3298 /* Scheme name must be lower case after canonicalization. */
3299 uri
->canon_uri
[i
+ pos
] = tolowerW(data
->scheme
[i
]);
3302 uri
->canon_uri
[i
+ pos
] = ':';
3303 uri
->scheme_start
= pos
;
3305 TRACE("(%p %p %x): Canonicalized scheme=%s, len=%d.\n", data
, uri
, flags
,
3306 debugstr_wn(uri
->canon_uri
, uri
->scheme_len
), data
->scheme_len
);
3309 /* This happens in both computation modes. */
3310 uri
->canon_len
+= data
->scheme_len
+ 1;
3311 uri
->scheme_len
= data
->scheme_len
;
3316 /* Computes what the length of the URI specified by the parse_data will be
3317 * after canonicalization occurs using the specified flags.
3319 * This function will return a non-zero value indicating the length of the canonicalized
3320 * URI, or -1 on error.
3322 static int compute_canonicalized_length(const parse_data
*data
, DWORD flags
) {
3325 memset(&uri
, 0, sizeof(Uri
));
3327 TRACE("(%p %x): Beginning to compute canonicalized length for URI %s\n", data
, flags
,
3328 debugstr_w(data
->uri
));
3330 if(!canonicalize_scheme(data
, &uri
, flags
, TRUE
)) {
3331 ERR("(%p %x): Failed to compute URI scheme length.\n", data
, flags
);
3335 if(!canonicalize_hierpart(data
, &uri
, flags
, TRUE
)) {
3336 ERR("(%p %x): Failed to compute URI hierpart length.\n", data
, flags
);
3340 if(!canonicalize_query(data
, &uri
, flags
, TRUE
)) {
3341 ERR("(%p %x): Failed to compute query string length.\n", data
, flags
);
3345 if(!canonicalize_fragment(data
, &uri
, flags
, TRUE
)) {
3346 ERR("(%p %x): Failed to compute fragment length.\n", data
, flags
);
3350 TRACE("(%p %x): Finished computing canonicalized URI length. length=%d\n", data
, flags
, uri
.canon_len
);
3352 return uri
.canon_len
;
3355 /* Canonicalizes the URI data specified in the parse_data, using the given flags. If the
3356 * canonicalization succeeds it will store all the canonicalization information
3357 * in the pointer to the Uri.
3359 * To canonicalize a URI this function first computes what the length of the URI
3360 * specified by the parse_data will be. Once this is done it will then perform the actual
3361 * canonicalization of the URI.
3363 static HRESULT
canonicalize_uri(const parse_data
*data
, Uri
*uri
, DWORD flags
) {
3366 uri
->canon_uri
= NULL
;
3367 uri
->canon_size
= uri
->canon_len
= 0;
3369 TRACE("(%p %p %x): beginning to canonicalize URI %s.\n", data
, uri
, flags
, debugstr_w(data
->uri
));
3371 /* First try to compute the length of the URI. */
3372 len
= compute_canonicalized_length(data
, flags
);
3374 ERR("(%p %p %x): Could not compute the canonicalized length of %s.\n", data
, uri
, flags
,
3375 debugstr_w(data
->uri
));
3376 return E_INVALIDARG
;
3379 uri
->canon_uri
= heap_alloc((len
+1)*sizeof(WCHAR
));
3381 return E_OUTOFMEMORY
;
3383 uri
->canon_size
= len
;
3384 if(!canonicalize_scheme(data
, uri
, flags
, FALSE
)) {
3385 ERR("(%p %p %x): Unable to canonicalize the scheme of the URI.\n", data
, uri
, flags
);
3386 return E_INVALIDARG
;
3388 uri
->scheme_type
= data
->scheme_type
;
3390 if(!canonicalize_hierpart(data
, uri
, flags
, FALSE
)) {
3391 ERR("(%p %p %x): Unable to canonicalize the heirpart of the URI\n", data
, uri
, flags
);
3392 return E_INVALIDARG
;
3395 if(!canonicalize_query(data
, uri
, flags
, FALSE
)) {
3396 ERR("(%p %p %x): Unable to canonicalize query string of the URI.\n",
3398 return E_INVALIDARG
;
3401 if(!canonicalize_fragment(data
, uri
, flags
, FALSE
)) {
3402 ERR("(%p %p %x): Unable to canonicalize fragment of the URI.\n",
3404 return E_INVALIDARG
;
3407 /* There's a possibility we didn't use all the space we allocated
3410 if(uri
->canon_len
< uri
->canon_size
) {
3411 /* This happens if the URI is hierarchical and dot
3412 * segments were removed from its path.
3414 WCHAR
*tmp
= heap_realloc(uri
->canon_uri
, (uri
->canon_len
+1)*sizeof(WCHAR
));
3416 return E_OUTOFMEMORY
;
3418 uri
->canon_uri
= tmp
;
3419 uri
->canon_size
= uri
->canon_len
;
3422 uri
->canon_uri
[uri
->canon_len
] = '\0';
3423 TRACE("(%p %p %x): finished canonicalizing the URI. uri=%s\n", data
, uri
, flags
, debugstr_w(uri
->canon_uri
));
3428 static HRESULT
get_builder_component(LPWSTR
*component
, DWORD
*component_len
,
3429 LPCWSTR source
, DWORD source_len
,
3430 LPCWSTR
*output
, DWORD
*output_len
)
3443 if(!(*component
) && source
) {
3444 /* Allocate 'component', and copy the contents from 'source'
3445 * into the new allocation.
3447 *component
= heap_alloc((source_len
+1)*sizeof(WCHAR
));
3449 return E_OUTOFMEMORY
;
3451 memcpy(*component
, source
, source_len
*sizeof(WCHAR
));
3452 (*component
)[source_len
] = '\0';
3453 *component_len
= source_len
;
3456 *output
= *component
;
3457 *output_len
= *component_len
;
3458 return *output
? S_OK
: S_FALSE
;
3461 /* Allocates 'component' and copies the string from 'new_value' into 'component'.
3462 * If 'prefix' is set and 'new_value' isn't NULL, then it checks if 'new_value'
3463 * starts with 'prefix'. If it doesn't then 'prefix' is prepended to 'component'.
3465 * If everything is successful, then will set 'success_flag' in 'flags'.
3467 static HRESULT
set_builder_component(LPWSTR
*component
, DWORD
*component_len
, LPCWSTR new_value
,
3468 WCHAR prefix
, DWORD
*flags
, DWORD success_flag
)
3470 heap_free(*component
);
3476 BOOL add_prefix
= FALSE
;
3477 DWORD len
= lstrlenW(new_value
);
3480 if(prefix
&& *new_value
!= prefix
) {
3482 *component
= heap_alloc((len
+2)*sizeof(WCHAR
));
3484 *component
= heap_alloc((len
+1)*sizeof(WCHAR
));
3487 return E_OUTOFMEMORY
;
3490 (*component
)[pos
++] = prefix
;
3492 memcpy(*component
+pos
, new_value
, (len
+1)*sizeof(WCHAR
));
3493 *component_len
= len
+pos
;
3496 *flags
|= success_flag
;
3500 static void reset_builder(UriBuilder
*builder
) {
3502 IUri_Release(&builder
->uri
->IUri_iface
);
3503 builder
->uri
= NULL
;
3505 heap_free(builder
->fragment
);
3506 builder
->fragment
= NULL
;
3507 builder
->fragment_len
= 0;
3509 heap_free(builder
->host
);
3510 builder
->host
= NULL
;
3511 builder
->host_len
= 0;
3513 heap_free(builder
->password
);
3514 builder
->password
= NULL
;
3515 builder
->password_len
= 0;
3517 heap_free(builder
->path
);
3518 builder
->path
= NULL
;
3519 builder
->path_len
= 0;
3521 heap_free(builder
->query
);
3522 builder
->query
= NULL
;
3523 builder
->query_len
= 0;
3525 heap_free(builder
->scheme
);
3526 builder
->scheme
= NULL
;
3527 builder
->scheme_len
= 0;
3529 heap_free(builder
->username
);
3530 builder
->username
= NULL
;
3531 builder
->username_len
= 0;
3533 builder
->has_port
= FALSE
;
3535 builder
->modified_props
= 0;
3538 static HRESULT
validate_scheme_name(const UriBuilder
*builder
, parse_data
*data
, DWORD flags
) {
3539 const WCHAR
*component
;
3544 if(builder
->scheme
) {
3545 ptr
= builder
->scheme
;
3546 expected_len
= builder
->scheme_len
;
3547 } else if(builder
->uri
&& builder
->uri
->scheme_start
> -1) {
3548 ptr
= builder
->uri
->canon_uri
+builder
->uri
->scheme_start
;
3549 expected_len
= builder
->uri
->scheme_len
;
3551 static const WCHAR nullW
[] = {0};
3558 if(parse_scheme(pptr
, data
, flags
, ALLOW_NULL_TERM_SCHEME
) &&
3559 data
->scheme_len
== expected_len
) {
3561 TRACE("(%p %p %x): Found valid scheme component %s len=%d.\n", builder
, data
, flags
,
3562 debugstr_wn(data
->scheme
, data
->scheme_len
), data
->scheme_len
);
3564 TRACE("(%p %p %x): Invalid scheme component found %s.\n", builder
, data
, flags
,
3565 debugstr_wn(component
, expected_len
));
3566 return INET_E_INVALID_URL
;
3572 static HRESULT
validate_username(const UriBuilder
*builder
, parse_data
*data
, DWORD flags
) {
3577 if(builder
->username
) {
3578 ptr
= builder
->username
;
3579 expected_len
= builder
->username_len
;
3580 } else if(!(builder
->modified_props
& Uri_HAS_USER_NAME
) && builder
->uri
&&
3581 builder
->uri
->userinfo_start
> -1 && builder
->uri
->userinfo_split
!= 0) {
3582 /* Just use the username from the base Uri. */
3583 data
->username
= builder
->uri
->canon_uri
+builder
->uri
->userinfo_start
;
3584 data
->username_len
= (builder
->uri
->userinfo_split
> -1) ?
3585 builder
->uri
->userinfo_split
: builder
->uri
->userinfo_len
;
3593 const WCHAR
*component
= ptr
;
3595 if(parse_username(pptr
, data
, flags
, ALLOW_NULL_TERM_USER_NAME
) &&
3596 data
->username_len
== expected_len
)
3597 TRACE("(%p %p %x): Found valid username component %s len=%d.\n", builder
, data
, flags
,
3598 debugstr_wn(data
->username
, data
->username_len
), data
->username_len
);
3600 TRACE("(%p %p %x): Invalid username component found %s.\n", builder
, data
, flags
,
3601 debugstr_wn(component
, expected_len
));
3602 return INET_E_INVALID_URL
;
3609 static HRESULT
validate_password(const UriBuilder
*builder
, parse_data
*data
, DWORD flags
) {
3614 if(builder
->password
) {
3615 ptr
= builder
->password
;
3616 expected_len
= builder
->password_len
;
3617 } else if(!(builder
->modified_props
& Uri_HAS_PASSWORD
) && builder
->uri
&&
3618 builder
->uri
->userinfo_split
> -1) {
3619 data
->password
= builder
->uri
->canon_uri
+builder
->uri
->userinfo_start
+builder
->uri
->userinfo_split
+1;
3620 data
->password_len
= builder
->uri
->userinfo_len
-builder
->uri
->userinfo_split
-1;
3628 const WCHAR
*component
= ptr
;
3630 if(parse_password(pptr
, data
, flags
, ALLOW_NULL_TERM_PASSWORD
) &&
3631 data
->password_len
== expected_len
)
3632 TRACE("(%p %p %x): Found valid password component %s len=%d.\n", builder
, data
, flags
,
3633 debugstr_wn(data
->password
, data
->password_len
), data
->password_len
);
3635 TRACE("(%p %p %x): Invalid password component found %s.\n", builder
, data
, flags
,
3636 debugstr_wn(component
, expected_len
));
3637 return INET_E_INVALID_URL
;
3644 static HRESULT
validate_userinfo(const UriBuilder
*builder
, parse_data
*data
, DWORD flags
) {
3647 hr
= validate_username(builder
, data
, flags
);
3651 hr
= validate_password(builder
, data
, flags
);
3658 static HRESULT
validate_host(const UriBuilder
*builder
, parse_data
*data
, DWORD flags
) {
3664 ptr
= builder
->host
;
3665 expected_len
= builder
->host_len
;
3666 } else if(!(builder
->modified_props
& Uri_HAS_HOST
) && builder
->uri
&& builder
->uri
->host_start
> -1) {
3667 ptr
= builder
->uri
->canon_uri
+ builder
->uri
->host_start
;
3668 expected_len
= builder
->uri
->host_len
;
3673 const WCHAR
*component
= ptr
;
3674 DWORD extras
= ALLOW_BRACKETLESS_IP_LITERAL
|IGNORE_PORT_DELIMITER
|SKIP_IP_FUTURE_CHECK
;
3677 if(parse_host(pptr
, data
, flags
, extras
) && data
->host_len
== expected_len
)
3678 TRACE("(%p %p %x): Found valid host name %s len=%d type=%d.\n", builder
, data
, flags
,
3679 debugstr_wn(data
->host
, data
->host_len
), data
->host_len
, data
->host_type
);
3681 TRACE("(%p %p %x): Invalid host name found %s.\n", builder
, data
, flags
,
3682 debugstr_wn(component
, expected_len
));
3683 return INET_E_INVALID_URL
;
3690 static void setup_port(const UriBuilder
*builder
, parse_data
*data
, DWORD flags
) {
3691 if(builder
->modified_props
& Uri_HAS_PORT
) {
3692 if(builder
->has_port
) {
3693 data
->has_port
= TRUE
;
3694 data
->port_value
= builder
->port
;
3696 } else if(builder
->uri
&& builder
->uri
->has_port
) {
3697 data
->has_port
= TRUE
;
3698 data
->port_value
= builder
->uri
->port
;
3702 TRACE("(%p %p %x): Using %u as port for IUri.\n", builder
, data
, flags
, data
->port_value
);
3705 static HRESULT
validate_path(const UriBuilder
*builder
, parse_data
*data
, DWORD flags
) {
3706 const WCHAR
*ptr
= NULL
;
3707 const WCHAR
*component
;
3710 BOOL check_len
= TRUE
;
3714 ptr
= builder
->path
;
3715 expected_len
= builder
->path_len
;
3716 } else if(!(builder
->modified_props
& Uri_HAS_PATH
) &&
3717 builder
->uri
&& builder
->uri
->path_start
> -1) {
3718 ptr
= builder
->uri
->canon_uri
+builder
->uri
->path_start
;
3719 expected_len
= builder
->uri
->path_len
;
3721 static const WCHAR nullW
[] = {0};
3730 /* How the path is validated depends on what type of
3733 valid
= data
->is_opaque
?
3734 parse_path_opaque(pptr
, data
, flags
) : parse_path_hierarchical(pptr
, data
, flags
);
3736 if(!valid
|| (check_len
&& expected_len
!= data
->path_len
)) {
3737 TRACE("(%p %p %x): Invalid path component %s.\n", builder
, data
, flags
,
3738 debugstr_wn(component
, expected_len
) );
3739 return INET_E_INVALID_URL
;
3742 TRACE("(%p %p %x): Valid path component %s len=%d.\n", builder
, data
, flags
,
3743 debugstr_wn(data
->path
, data
->path_len
), data
->path_len
);
3748 static HRESULT
validate_query(const UriBuilder
*builder
, parse_data
*data
, DWORD flags
) {
3749 const WCHAR
*ptr
= NULL
;
3753 if(builder
->query
) {
3754 ptr
= builder
->query
;
3755 expected_len
= builder
->query_len
;
3756 } else if(!(builder
->modified_props
& Uri_HAS_QUERY
) && builder
->uri
&&
3757 builder
->uri
->query_start
> -1) {
3758 ptr
= builder
->uri
->canon_uri
+builder
->uri
->query_start
;
3759 expected_len
= builder
->uri
->query_len
;
3763 const WCHAR
*component
= ptr
;
3766 if(parse_query(pptr
, data
, flags
) && expected_len
== data
->query_len
)
3767 TRACE("(%p %p %x): Valid query component %s len=%d.\n", builder
, data
, flags
,
3768 debugstr_wn(data
->query
, data
->query_len
), data
->query_len
);
3770 TRACE("(%p %p %x): Invalid query component %s.\n", builder
, data
, flags
,
3771 debugstr_wn(component
, expected_len
));
3772 return INET_E_INVALID_URL
;
3779 static HRESULT
validate_fragment(const UriBuilder
*builder
, parse_data
*data
, DWORD flags
) {
3780 const WCHAR
*ptr
= NULL
;
3784 if(builder
->fragment
) {
3785 ptr
= builder
->fragment
;
3786 expected_len
= builder
->fragment_len
;
3787 } else if(!(builder
->modified_props
& Uri_HAS_FRAGMENT
) && builder
->uri
&&
3788 builder
->uri
->fragment_start
> -1) {
3789 ptr
= builder
->uri
->canon_uri
+builder
->uri
->fragment_start
;
3790 expected_len
= builder
->uri
->fragment_len
;
3794 const WCHAR
*component
= ptr
;
3797 if(parse_fragment(pptr
, data
, flags
) && expected_len
== data
->fragment_len
)
3798 TRACE("(%p %p %x): Valid fragment component %s len=%d.\n", builder
, data
, flags
,
3799 debugstr_wn(data
->fragment
, data
->fragment_len
), data
->fragment_len
);
3801 TRACE("(%p %p %x): Invalid fragment component %s.\n", builder
, data
, flags
,
3802 debugstr_wn(component
, expected_len
));
3803 return INET_E_INVALID_URL
;
3810 static HRESULT
validate_components(const UriBuilder
*builder
, parse_data
*data
, DWORD flags
) {
3813 memset(data
, 0, sizeof(parse_data
));
3815 TRACE("(%p %p %x): Beginning to validate builder components.\n", builder
, data
, flags
);
3817 hr
= validate_scheme_name(builder
, data
, flags
);
3821 /* Extra validation for file schemes. */
3822 if(data
->scheme_type
== URL_SCHEME_FILE
) {
3823 if((builder
->password
|| (builder
->uri
&& builder
->uri
->userinfo_split
> -1)) ||
3824 (builder
->username
|| (builder
->uri
&& builder
->uri
->userinfo_start
> -1))) {
3825 TRACE("(%p %p %x): File schemes can't contain a username or password.\n",
3826 builder
, data
, flags
);
3827 return INET_E_INVALID_URL
;
3831 hr
= validate_userinfo(builder
, data
, flags
);
3835 hr
= validate_host(builder
, data
, flags
);
3839 setup_port(builder
, data
, flags
);
3841 /* The URI is opaque if it doesn't have an authority component. */
3842 if(!data
->is_relative
)
3843 data
->is_opaque
= !data
->username
&& !data
->password
&& !data
->host
&& !data
->has_port
3844 && data
->scheme_type
!= URL_SCHEME_FILE
;
3846 data
->is_opaque
= !data
->host
&& !data
->has_port
;
3848 hr
= validate_path(builder
, data
, flags
);
3852 hr
= validate_query(builder
, data
, flags
);
3856 hr
= validate_fragment(builder
, data
, flags
);
3860 TRACE("(%p %p %x): Finished validating builder components.\n", builder
, data
, flags
);
3865 static HRESULT
compare_file_paths(const Uri
*a
, const Uri
*b
, BOOL
*ret
)
3867 WCHAR
*canon_path_a
, *canon_path_b
;
3871 *ret
= !b
->path_len
;
3881 if(a
->path_len
== b
->path_len
&& !memicmpW(a
->canon_uri
+a
->path_start
, b
->canon_uri
+b
->path_start
, a
->path_len
)) {
3886 len_a
= canonicalize_path_hierarchical(a
->canon_uri
+a
->path_start
, a
->path_len
, a
->scheme_type
, FALSE
, 0, NULL
);
3887 len_b
= canonicalize_path_hierarchical(b
->canon_uri
+b
->path_start
, b
->path_len
, b
->scheme_type
, FALSE
, 0, NULL
);
3889 canon_path_a
= heap_alloc(len_a
*sizeof(WCHAR
));
3891 return E_OUTOFMEMORY
;
3892 canon_path_b
= heap_alloc(len_b
*sizeof(WCHAR
));
3894 heap_free(canon_path_a
);
3895 return E_OUTOFMEMORY
;
3898 len_a
= canonicalize_path_hierarchical(a
->canon_uri
+a
->path_start
, a
->path_len
, a
->scheme_type
, FALSE
, 0, canon_path_a
);
3899 len_b
= canonicalize_path_hierarchical(b
->canon_uri
+b
->path_start
, b
->path_len
, b
->scheme_type
, FALSE
, 0, canon_path_b
);
3901 *ret
= len_a
== len_b
&& !memicmpW(canon_path_a
, canon_path_b
, len_a
);
3903 heap_free(canon_path_a
);
3904 heap_free(canon_path_b
);
3908 /* Checks if the two Uri's are logically equivalent. It's a simple
3909 * comparison, since they are both of type Uri, and it can access
3910 * the properties of each Uri directly without the need to go
3911 * through the "IUri_Get*" interface calls.
3913 static HRESULT
compare_uris(const Uri
*a
, const Uri
*b
, BOOL
*ret
) {
3914 const BOOL known_scheme
= a
->scheme_type
!= URL_SCHEME_UNKNOWN
;
3915 const BOOL are_hierarchical
= a
->authority_start
> -1 && b
->authority_start
> -1;
3920 if(a
->scheme_type
!= b
->scheme_type
)
3923 /* Only compare the scheme names (if any) if their unknown scheme types. */
3925 if((a
->scheme_start
> -1 && b
->scheme_start
> -1) &&
3926 (a
->scheme_len
== b
->scheme_len
)) {
3927 /* Make sure the schemes are the same. */
3928 if(StrCmpNW(a
->canon_uri
+a
->scheme_start
, b
->canon_uri
+b
->scheme_start
, a
->scheme_len
))
3930 } else if(a
->scheme_len
!= b
->scheme_len
)
3931 /* One of the Uri's has a scheme name, while the other doesn't. */
3935 /* If they have a userinfo component, perform case sensitive compare. */
3936 if((a
->userinfo_start
> -1 && b
->userinfo_start
> -1) &&
3937 (a
->userinfo_len
== b
->userinfo_len
)) {
3938 if(StrCmpNW(a
->canon_uri
+a
->userinfo_start
, b
->canon_uri
+b
->userinfo_start
, a
->userinfo_len
))
3940 } else if(a
->userinfo_len
!= b
->userinfo_len
)
3941 /* One of the Uri's had a userinfo, while the other one doesn't. */
3944 /* Check if they have a host name. */
3945 if((a
->host_start
> -1 && b
->host_start
> -1) &&
3946 (a
->host_len
== b
->host_len
)) {
3947 /* Perform a case insensitive compare if they are a known scheme type. */
3949 if(StrCmpNIW(a
->canon_uri
+a
->host_start
, b
->canon_uri
+b
->host_start
, a
->host_len
))
3951 } else if(StrCmpNW(a
->canon_uri
+a
->host_start
, b
->canon_uri
+b
->host_start
, a
->host_len
))
3953 } else if(a
->host_len
!= b
->host_len
)
3954 /* One of the Uri's had a host, while the other one didn't. */
3957 if(a
->has_port
&& b
->has_port
) {
3958 if(a
->port
!= b
->port
)
3960 } else if(a
->has_port
|| b
->has_port
)
3961 /* One had a port, while the other one didn't. */
3964 /* Windows is weird with how it handles paths. For example
3965 * One URI could be "http://google.com" (after canonicalization)
3966 * and one could be "http://google.com/" and the IsEqual function
3967 * would still evaluate to TRUE, but, only if they are both hierarchical
3970 if(a
->scheme_type
== URL_SCHEME_FILE
) {
3973 hres
= compare_file_paths(a
, b
, &cmp
);
3974 if(FAILED(hres
) || !cmp
)
3976 } else if((a
->path_start
> -1 && b
->path_start
> -1) &&
3977 (a
->path_len
== b
->path_len
)) {
3978 if(StrCmpNW(a
->canon_uri
+a
->path_start
, b
->canon_uri
+b
->path_start
, a
->path_len
))
3980 } else if(are_hierarchical
&& a
->path_len
== -1 && b
->path_len
== 0) {
3981 if(*(a
->canon_uri
+a
->path_start
) != '/')
3983 } else if(are_hierarchical
&& b
->path_len
== 1 && a
->path_len
== 0) {
3984 if(*(b
->canon_uri
+b
->path_start
) != '/')
3986 } else if(a
->path_len
!= b
->path_len
)
3989 /* Compare the query strings of the two URIs. */
3990 if((a
->query_start
> -1 && b
->query_start
> -1) &&
3991 (a
->query_len
== b
->query_len
)) {
3992 if(StrCmpNW(a
->canon_uri
+a
->query_start
, b
->canon_uri
+b
->query_start
, a
->query_len
))
3994 } else if(a
->query_len
!= b
->query_len
)
3997 if((a
->fragment_start
> -1 && b
->fragment_start
> -1) &&
3998 (a
->fragment_len
== b
->fragment_len
)) {
3999 if(StrCmpNW(a
->canon_uri
+a
->fragment_start
, b
->canon_uri
+b
->fragment_start
, a
->fragment_len
))
4001 } else if(a
->fragment_len
!= b
->fragment_len
)
4004 /* If we get here, the two URIs are equivalent. */
4009 static void convert_to_dos_path(const WCHAR
*path
, DWORD path_len
,
4010 WCHAR
*output
, DWORD
*output_len
)
4012 const WCHAR
*ptr
= path
;
4014 if(path_len
> 3 && *ptr
== '/' && is_drive_path(path
+1))
4015 /* Skip over the leading / before the drive path. */
4018 for(; ptr
< path
+path_len
; ++ptr
) {
4031 /* Generates a raw uri string using the parse_data. */
4032 static DWORD
generate_raw_uri(const parse_data
*data
, BSTR uri
, DWORD flags
) {
4037 memcpy(uri
, data
->scheme
, data
->scheme_len
*sizeof(WCHAR
));
4038 uri
[data
->scheme_len
] = ':';
4040 length
+= data
->scheme_len
+1;
4043 if(!data
->is_opaque
) {
4044 /* For the "//" which appears before the authority component. */
4047 uri
[length
+1] = '/';
4051 /* Check if we need to add the "\\" before the host name
4052 * of a UNC server name in a DOS path.
4054 if(flags
& RAW_URI_CONVERT_TO_DOS_PATH
&&
4055 data
->scheme_type
== URL_SCHEME_FILE
&& data
->host
) {
4058 uri
[length
+1] = '\\';
4064 if(data
->username
) {
4066 memcpy(uri
+length
, data
->username
, data
->username_len
*sizeof(WCHAR
));
4067 length
+= data
->username_len
;
4070 if(data
->password
) {
4073 memcpy(uri
+length
+1, data
->password
, data
->password_len
*sizeof(WCHAR
));
4075 length
+= data
->password_len
+1;
4078 if(data
->password
|| data
->username
) {
4085 /* IPv6 addresses get the brackets added around them if they don't already
4088 const BOOL add_brackets
= data
->host_type
== Uri_HOST_IPV6
&& *(data
->host
) != '[';
4096 memcpy(uri
+length
, data
->host
, data
->host_len
*sizeof(WCHAR
));
4097 length
+= data
->host_len
;
4106 if(data
->has_port
) {
4107 /* The port isn't included in the raw uri if it's the default
4108 * port for the scheme type.
4111 BOOL is_default
= FALSE
;
4113 for(i
= 0; i
< sizeof(default_ports
)/sizeof(default_ports
[0]); ++i
) {
4114 if(data
->scheme_type
== default_ports
[i
].scheme
&&
4115 data
->port_value
== default_ports
[i
].port
)
4119 if(!is_default
|| flags
& RAW_URI_FORCE_PORT_DISP
) {
4125 length
+= ui2str(uri
+length
, data
->port_value
);
4127 length
+= ui2str(NULL
, data
->port_value
);
4131 /* Check if a '/' should be added before the path for hierarchical URIs. */
4132 if(!data
->is_opaque
&& data
->path
&& *(data
->path
) != '/') {
4139 if(!data
->is_opaque
&& data
->scheme_type
== URL_SCHEME_FILE
&&
4140 flags
& RAW_URI_CONVERT_TO_DOS_PATH
) {
4144 convert_to_dos_path(data
->path
, data
->path_len
, uri
+length
, &len
);
4146 convert_to_dos_path(data
->path
, data
->path_len
, NULL
, &len
);
4151 memcpy(uri
+length
, data
->path
, data
->path_len
*sizeof(WCHAR
));
4152 length
+= data
->path_len
;
4158 memcpy(uri
+length
, data
->query
, data
->query_len
*sizeof(WCHAR
));
4159 length
+= data
->query_len
;
4162 if(data
->fragment
) {
4164 memcpy(uri
+length
, data
->fragment
, data
->fragment_len
*sizeof(WCHAR
));
4165 length
+= data
->fragment_len
;
4169 TRACE("(%p %p): Generated raw uri=%s len=%d\n", data
, uri
, debugstr_wn(uri
, length
), length
);
4171 TRACE("(%p %p): Computed raw uri len=%d\n", data
, uri
, length
);
4176 static HRESULT
generate_uri(const UriBuilder
*builder
, const parse_data
*data
, Uri
*uri
, DWORD flags
) {
4178 DWORD length
= generate_raw_uri(data
, NULL
, 0);
4179 uri
->raw_uri
= SysAllocStringLen(NULL
, length
);
4181 return E_OUTOFMEMORY
;
4183 generate_raw_uri(data
, uri
->raw_uri
, 0);
4185 hr
= canonicalize_uri(data
, uri
, flags
);
4187 if(hr
== E_INVALIDARG
)
4188 return INET_E_INVALID_URL
;
4192 uri
->create_flags
= flags
;
4196 static inline Uri
* impl_from_IUri(IUri
*iface
)
4198 return CONTAINING_RECORD(iface
, Uri
, IUri_iface
);
4201 static inline void destory_uri_obj(Uri
*This
)
4203 SysFreeString(This
->raw_uri
);
4204 heap_free(This
->canon_uri
);
4208 static HRESULT WINAPI
Uri_QueryInterface(IUri
*iface
, REFIID riid
, void **ppv
)
4210 Uri
*This
= impl_from_IUri(iface
);
4212 if(IsEqualGUID(&IID_IUnknown
, riid
)) {
4213 TRACE("(%p)->(IID_IUnknown %p)\n", This
, ppv
);
4214 *ppv
= &This
->IUri_iface
;
4215 }else if(IsEqualGUID(&IID_IUri
, riid
)) {
4216 TRACE("(%p)->(IID_IUri %p)\n", This
, ppv
);
4217 *ppv
= &This
->IUri_iface
;
4218 }else if(IsEqualGUID(&IID_IUriBuilderFactory
, riid
)) {
4219 TRACE("(%p)->(IID_IUriBuilderFactory %p)\n", This
, ppv
);
4220 *ppv
= &This
->IUriBuilderFactory_iface
;
4221 }else if(IsEqualGUID(&IID_IPersistStream
, riid
)) {
4222 TRACE("(%p)->(IID_IPersistStream %p)\n", This
, ppv
);
4223 *ppv
= &This
->IPersistStream_iface
;
4224 }else if(IsEqualGUID(&IID_IUriObj
, riid
)) {
4225 TRACE("(%p)->(IID_IUriObj %p)\n", This
, ppv
);
4229 TRACE("(%p)->(%s %p)\n", This
, debugstr_guid(riid
), ppv
);
4231 return E_NOINTERFACE
;
4234 IUnknown_AddRef((IUnknown
*)*ppv
);
4238 static ULONG WINAPI
Uri_AddRef(IUri
*iface
)
4240 Uri
*This
= impl_from_IUri(iface
);
4241 LONG ref
= InterlockedIncrement(&This
->ref
);
4243 TRACE("(%p) ref=%d\n", This
, ref
);
4248 static ULONG WINAPI
Uri_Release(IUri
*iface
)
4250 Uri
*This
= impl_from_IUri(iface
);
4251 LONG ref
= InterlockedDecrement(&This
->ref
);
4253 TRACE("(%p) ref=%d\n", This
, ref
);
4256 destory_uri_obj(This
);
4261 static HRESULT WINAPI
Uri_GetPropertyBSTR(IUri
*iface
, Uri_PROPERTY uriProp
, BSTR
*pbstrProperty
, DWORD dwFlags
)
4263 Uri
*This
= impl_from_IUri(iface
);
4265 TRACE("(%p %s)->(%d %p %x)\n", This
, debugstr_w(This
->canon_uri
), uriProp
, pbstrProperty
, dwFlags
);
4267 if(!This
->create_flags
)
4268 return E_UNEXPECTED
;
4272 if(uriProp
> Uri_PROPERTY_STRING_LAST
) {
4273 /* Windows allocates an empty BSTR for invalid Uri_PROPERTY's. */
4274 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
4275 if(!(*pbstrProperty
))
4276 return E_OUTOFMEMORY
;
4278 /* It only returns S_FALSE for the ZONE property... */
4279 if(uriProp
== Uri_PROPERTY_ZONE
)
4285 /* Don't have support for flags yet. */
4287 FIXME("(%p)->(%d %p %x)\n", This
, uriProp
, pbstrProperty
, dwFlags
);
4292 case Uri_PROPERTY_ABSOLUTE_URI
:
4293 if(This
->display_modifiers
& URI_DISPLAY_NO_ABSOLUTE_URI
) {
4294 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
4297 if(This
->scheme_type
!= URL_SCHEME_UNKNOWN
&& This
->userinfo_start
> -1) {
4298 if(This
->userinfo_len
== 0) {
4299 /* Don't include the '@' after the userinfo component. */
4300 *pbstrProperty
= SysAllocStringLen(NULL
, This
->canon_len
-1);
4302 if(*pbstrProperty
) {
4303 /* Copy everything before it. */
4304 memcpy(*pbstrProperty
, This
->canon_uri
, This
->userinfo_start
*sizeof(WCHAR
));
4306 /* And everything after it. */
4307 memcpy(*pbstrProperty
+This
->userinfo_start
, This
->canon_uri
+This
->userinfo_start
+1,
4308 (This
->canon_len
-This
->userinfo_start
-1)*sizeof(WCHAR
));
4310 } else if(This
->userinfo_split
== 0 && This
->userinfo_len
== 1) {
4311 /* Don't include the ":@" */
4312 *pbstrProperty
= SysAllocStringLen(NULL
, This
->canon_len
-2);
4314 if(*pbstrProperty
) {
4315 memcpy(*pbstrProperty
, This
->canon_uri
, This
->userinfo_start
*sizeof(WCHAR
));
4316 memcpy(*pbstrProperty
+This
->userinfo_start
, This
->canon_uri
+This
->userinfo_start
+2,
4317 (This
->canon_len
-This
->userinfo_start
-2)*sizeof(WCHAR
));
4320 *pbstrProperty
= SysAllocString(This
->canon_uri
);
4324 *pbstrProperty
= SysAllocString(This
->canon_uri
);
4329 if(!(*pbstrProperty
))
4330 hres
= E_OUTOFMEMORY
;
4333 case Uri_PROPERTY_AUTHORITY
:
4334 if(This
->authority_start
> -1) {
4335 if(This
->port_offset
> -1 && is_default_port(This
->scheme_type
, This
->port
) &&
4336 This
->display_modifiers
& URI_DISPLAY_NO_DEFAULT_PORT_AUTH
)
4337 /* Don't include the port in the authority component. */
4338 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+This
->authority_start
, This
->port_offset
);
4340 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+This
->authority_start
, This
->authority_len
);
4343 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
4347 if(!(*pbstrProperty
))
4348 hres
= E_OUTOFMEMORY
;
4351 case Uri_PROPERTY_DISPLAY_URI
:
4352 /* The Display URI contains everything except for the userinfo for known
4355 if(This
->scheme_type
!= URL_SCHEME_UNKNOWN
&& This
->userinfo_start
> -1) {
4356 *pbstrProperty
= SysAllocStringLen(NULL
, This
->canon_len
-This
->userinfo_len
);
4358 if(*pbstrProperty
) {
4359 /* Copy everything before the userinfo over. */
4360 memcpy(*pbstrProperty
, This
->canon_uri
, This
->userinfo_start
*sizeof(WCHAR
));
4361 /* Copy everything after the userinfo over. */
4362 memcpy(*pbstrProperty
+This
->userinfo_start
,
4363 This
->canon_uri
+This
->userinfo_start
+This
->userinfo_len
+1,
4364 (This
->canon_len
-(This
->userinfo_start
+This
->userinfo_len
+1))*sizeof(WCHAR
));
4367 *pbstrProperty
= SysAllocString(This
->canon_uri
);
4369 if(!(*pbstrProperty
))
4370 hres
= E_OUTOFMEMORY
;
4375 case Uri_PROPERTY_DOMAIN
:
4376 if(This
->domain_offset
> -1) {
4377 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+This
->host_start
+This
->domain_offset
,
4378 This
->host_len
-This
->domain_offset
);
4381 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
4385 if(!(*pbstrProperty
))
4386 hres
= E_OUTOFMEMORY
;
4389 case Uri_PROPERTY_EXTENSION
:
4390 if(This
->extension_offset
> -1) {
4391 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+This
->path_start
+This
->extension_offset
,
4392 This
->path_len
-This
->extension_offset
);
4395 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
4399 if(!(*pbstrProperty
))
4400 hres
= E_OUTOFMEMORY
;
4403 case Uri_PROPERTY_FRAGMENT
:
4404 if(This
->fragment_start
> -1) {
4405 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+This
->fragment_start
, This
->fragment_len
);
4408 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
4412 if(!(*pbstrProperty
))
4413 hres
= E_OUTOFMEMORY
;
4416 case Uri_PROPERTY_HOST
:
4417 if(This
->host_start
> -1) {
4418 /* The '[' and ']' aren't included for IPv6 addresses. */
4419 if(This
->host_type
== Uri_HOST_IPV6
)
4420 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+This
->host_start
+1, This
->host_len
-2);
4422 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+This
->host_start
, This
->host_len
);
4426 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
4430 if(!(*pbstrProperty
))
4431 hres
= E_OUTOFMEMORY
;
4434 case Uri_PROPERTY_PASSWORD
:
4435 if(This
->userinfo_split
> -1) {
4436 *pbstrProperty
= SysAllocStringLen(
4437 This
->canon_uri
+This
->userinfo_start
+This
->userinfo_split
+1,
4438 This
->userinfo_len
-This
->userinfo_split
-1);
4441 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
4445 if(!(*pbstrProperty
))
4446 return E_OUTOFMEMORY
;
4449 case Uri_PROPERTY_PATH
:
4450 if(This
->path_start
> -1) {
4451 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+This
->path_start
, This
->path_len
);
4454 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
4458 if(!(*pbstrProperty
))
4459 hres
= E_OUTOFMEMORY
;
4462 case Uri_PROPERTY_PATH_AND_QUERY
:
4463 if(This
->path_start
> -1) {
4464 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+This
->path_start
, This
->path_len
+This
->query_len
);
4466 } else if(This
->query_start
> -1) {
4467 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+This
->query_start
, This
->query_len
);
4470 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
4474 if(!(*pbstrProperty
))
4475 hres
= E_OUTOFMEMORY
;
4478 case Uri_PROPERTY_QUERY
:
4479 if(This
->query_start
> -1) {
4480 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+This
->query_start
, This
->query_len
);
4483 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
4487 if(!(*pbstrProperty
))
4488 hres
= E_OUTOFMEMORY
;
4491 case Uri_PROPERTY_RAW_URI
:
4492 *pbstrProperty
= SysAllocString(This
->raw_uri
);
4493 if(!(*pbstrProperty
))
4494 hres
= E_OUTOFMEMORY
;
4498 case Uri_PROPERTY_SCHEME_NAME
:
4499 if(This
->scheme_start
> -1) {
4500 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+ This
->scheme_start
, This
->scheme_len
);
4503 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
4507 if(!(*pbstrProperty
))
4508 hres
= E_OUTOFMEMORY
;
4511 case Uri_PROPERTY_USER_INFO
:
4512 if(This
->userinfo_start
> -1) {
4513 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+This
->userinfo_start
, This
->userinfo_len
);
4516 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
4520 if(!(*pbstrProperty
))
4521 hres
= E_OUTOFMEMORY
;
4524 case Uri_PROPERTY_USER_NAME
:
4525 if(This
->userinfo_start
> -1 && This
->userinfo_split
!= 0) {
4526 /* If userinfo_split is set, that means a password exists
4527 * so the username is only from userinfo_start to userinfo_split.
4529 if(This
->userinfo_split
> -1) {
4530 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+ This
->userinfo_start
, This
->userinfo_split
);
4533 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+ This
->userinfo_start
, This
->userinfo_len
);
4537 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
4541 if(!(*pbstrProperty
))
4542 return E_OUTOFMEMORY
;
4546 FIXME("(%p)->(%d %p %x)\n", This
, uriProp
, pbstrProperty
, dwFlags
);
4553 static HRESULT WINAPI
Uri_GetPropertyLength(IUri
*iface
, Uri_PROPERTY uriProp
, DWORD
*pcchProperty
, DWORD dwFlags
)
4555 Uri
*This
= impl_from_IUri(iface
);
4557 TRACE("(%p %s)->(%d %p %x)\n", This
, debugstr_w(This
->canon_uri
), uriProp
, pcchProperty
, dwFlags
);
4559 if(!This
->create_flags
)
4560 return E_UNEXPECTED
;
4562 return E_INVALIDARG
;
4564 /* Can only return a length for a property if it's a string. */
4565 if(uriProp
> Uri_PROPERTY_STRING_LAST
)
4566 return E_INVALIDARG
;
4568 /* Don't have support for flags yet. */
4570 FIXME("(%p)->(%d %p %x)\n", This
, uriProp
, pcchProperty
, dwFlags
);
4575 case Uri_PROPERTY_ABSOLUTE_URI
:
4576 if(This
->display_modifiers
& URI_DISPLAY_NO_ABSOLUTE_URI
) {
4580 if(This
->scheme_type
!= URL_SCHEME_UNKNOWN
) {
4581 if(This
->userinfo_start
> -1 && This
->userinfo_len
== 0)
4582 /* Don't include the '@' in the length. */
4583 *pcchProperty
= This
->canon_len
-1;
4584 else if(This
->userinfo_start
> -1 && This
->userinfo_len
== 1 &&
4585 This
->userinfo_split
== 0)
4586 /* Don't include the ":@" in the length. */
4587 *pcchProperty
= This
->canon_len
-2;
4589 *pcchProperty
= This
->canon_len
;
4591 *pcchProperty
= This
->canon_len
;
4597 case Uri_PROPERTY_AUTHORITY
:
4598 if(This
->port_offset
> -1 &&
4599 This
->display_modifiers
& URI_DISPLAY_NO_DEFAULT_PORT_AUTH
&&
4600 is_default_port(This
->scheme_type
, This
->port
))
4601 /* Only count up until the port in the authority. */
4602 *pcchProperty
= This
->port_offset
;
4604 *pcchProperty
= This
->authority_len
;
4605 hres
= (This
->authority_start
> -1) ? S_OK
: S_FALSE
;
4607 case Uri_PROPERTY_DISPLAY_URI
:
4608 if(This
->scheme_type
!= URL_SCHEME_UNKNOWN
&& This
->userinfo_start
> -1)
4609 *pcchProperty
= This
->canon_len
-This
->userinfo_len
-1;
4611 *pcchProperty
= This
->canon_len
;
4615 case Uri_PROPERTY_DOMAIN
:
4616 if(This
->domain_offset
> -1)
4617 *pcchProperty
= This
->host_len
- This
->domain_offset
;
4621 hres
= (This
->domain_offset
> -1) ? S_OK
: S_FALSE
;
4623 case Uri_PROPERTY_EXTENSION
:
4624 if(This
->extension_offset
> -1) {
4625 *pcchProperty
= This
->path_len
- This
->extension_offset
;
4633 case Uri_PROPERTY_FRAGMENT
:
4634 *pcchProperty
= This
->fragment_len
;
4635 hres
= (This
->fragment_start
> -1) ? S_OK
: S_FALSE
;
4637 case Uri_PROPERTY_HOST
:
4638 *pcchProperty
= This
->host_len
;
4640 /* '[' and ']' aren't included in the length. */
4641 if(This
->host_type
== Uri_HOST_IPV6
)
4644 hres
= (This
->host_start
> -1) ? S_OK
: S_FALSE
;
4646 case Uri_PROPERTY_PASSWORD
:
4647 *pcchProperty
= (This
->userinfo_split
> -1) ? This
->userinfo_len
-This
->userinfo_split
-1 : 0;
4648 hres
= (This
->userinfo_split
> -1) ? S_OK
: S_FALSE
;
4650 case Uri_PROPERTY_PATH
:
4651 *pcchProperty
= This
->path_len
;
4652 hres
= (This
->path_start
> -1) ? S_OK
: S_FALSE
;
4654 case Uri_PROPERTY_PATH_AND_QUERY
:
4655 *pcchProperty
= This
->path_len
+This
->query_len
;
4656 hres
= (This
->path_start
> -1 || This
->query_start
> -1) ? S_OK
: S_FALSE
;
4658 case Uri_PROPERTY_QUERY
:
4659 *pcchProperty
= This
->query_len
;
4660 hres
= (This
->query_start
> -1) ? S_OK
: S_FALSE
;
4662 case Uri_PROPERTY_RAW_URI
:
4663 *pcchProperty
= SysStringLen(This
->raw_uri
);
4666 case Uri_PROPERTY_SCHEME_NAME
:
4667 *pcchProperty
= This
->scheme_len
;
4668 hres
= (This
->scheme_start
> -1) ? S_OK
: S_FALSE
;
4670 case Uri_PROPERTY_USER_INFO
:
4671 *pcchProperty
= This
->userinfo_len
;
4672 hres
= (This
->userinfo_start
> -1) ? S_OK
: S_FALSE
;
4674 case Uri_PROPERTY_USER_NAME
:
4675 *pcchProperty
= (This
->userinfo_split
> -1) ? This
->userinfo_split
: This
->userinfo_len
;
4676 if(This
->userinfo_split
== 0)
4679 hres
= (This
->userinfo_start
> -1) ? S_OK
: S_FALSE
;
4682 FIXME("(%p)->(%d %p %x)\n", This
, uriProp
, pcchProperty
, dwFlags
);
4689 static HRESULT WINAPI
Uri_GetPropertyDWORD(IUri
*iface
, Uri_PROPERTY uriProp
, DWORD
*pcchProperty
, DWORD dwFlags
)
4691 Uri
*This
= impl_from_IUri(iface
);
4694 TRACE("(%p %s)->(%d %p %x)\n", This
, debugstr_w(This
->canon_uri
), uriProp
, pcchProperty
, dwFlags
);
4696 if(!This
->create_flags
)
4697 return E_UNEXPECTED
;
4699 return E_INVALIDARG
;
4701 /* Microsoft's implementation for the ZONE property of a URI seems to be lacking...
4702 * From what I can tell, instead of checking which URLZONE the URI belongs to it
4703 * simply assigns URLZONE_INVALID and returns E_NOTIMPL. This also applies to the GetZone
4706 if(uriProp
== Uri_PROPERTY_ZONE
) {
4707 *pcchProperty
= URLZONE_INVALID
;
4711 if(uriProp
< Uri_PROPERTY_DWORD_START
) {
4713 return E_INVALIDARG
;
4717 case Uri_PROPERTY_HOST_TYPE
:
4718 *pcchProperty
= This
->host_type
;
4721 case Uri_PROPERTY_PORT
:
4722 if(!This
->has_port
) {
4726 *pcchProperty
= This
->port
;
4731 case Uri_PROPERTY_SCHEME
:
4732 *pcchProperty
= This
->scheme_type
;
4736 FIXME("(%p)->(%d %p %x)\n", This
, uriProp
, pcchProperty
, dwFlags
);
4743 static HRESULT WINAPI
Uri_HasProperty(IUri
*iface
, Uri_PROPERTY uriProp
, BOOL
*pfHasProperty
)
4745 Uri
*This
= impl_from_IUri(iface
);
4747 TRACE("(%p %s)->(%d %p)\n", This
, debugstr_w(This
->canon_uri
), uriProp
, pfHasProperty
);
4750 return E_INVALIDARG
;
4753 case Uri_PROPERTY_ABSOLUTE_URI
:
4754 *pfHasProperty
= !(This
->display_modifiers
& URI_DISPLAY_NO_ABSOLUTE_URI
);
4756 case Uri_PROPERTY_AUTHORITY
:
4757 *pfHasProperty
= This
->authority_start
> -1;
4759 case Uri_PROPERTY_DISPLAY_URI
:
4760 *pfHasProperty
= TRUE
;
4762 case Uri_PROPERTY_DOMAIN
:
4763 *pfHasProperty
= This
->domain_offset
> -1;
4765 case Uri_PROPERTY_EXTENSION
:
4766 *pfHasProperty
= This
->extension_offset
> -1;
4768 case Uri_PROPERTY_FRAGMENT
:
4769 *pfHasProperty
= This
->fragment_start
> -1;
4771 case Uri_PROPERTY_HOST
:
4772 *pfHasProperty
= This
->host_start
> -1;
4774 case Uri_PROPERTY_PASSWORD
:
4775 *pfHasProperty
= This
->userinfo_split
> -1;
4777 case Uri_PROPERTY_PATH
:
4778 *pfHasProperty
= This
->path_start
> -1;
4780 case Uri_PROPERTY_PATH_AND_QUERY
:
4781 *pfHasProperty
= (This
->path_start
> -1 || This
->query_start
> -1);
4783 case Uri_PROPERTY_QUERY
:
4784 *pfHasProperty
= This
->query_start
> -1;
4786 case Uri_PROPERTY_RAW_URI
:
4787 *pfHasProperty
= TRUE
;
4789 case Uri_PROPERTY_SCHEME_NAME
:
4790 *pfHasProperty
= This
->scheme_start
> -1;
4792 case Uri_PROPERTY_USER_INFO
:
4793 *pfHasProperty
= This
->userinfo_start
> -1;
4795 case Uri_PROPERTY_USER_NAME
:
4796 if(This
->userinfo_split
== 0)
4797 *pfHasProperty
= FALSE
;
4799 *pfHasProperty
= This
->userinfo_start
> -1;
4801 case Uri_PROPERTY_HOST_TYPE
:
4802 *pfHasProperty
= TRUE
;
4804 case Uri_PROPERTY_PORT
:
4805 *pfHasProperty
= This
->has_port
;
4807 case Uri_PROPERTY_SCHEME
:
4808 *pfHasProperty
= TRUE
;
4810 case Uri_PROPERTY_ZONE
:
4811 *pfHasProperty
= FALSE
;
4814 FIXME("(%p)->(%d %p): Unsupported property type.\n", This
, uriProp
, pfHasProperty
);
4821 static HRESULT WINAPI
Uri_GetAbsoluteUri(IUri
*iface
, BSTR
*pstrAbsoluteUri
)
4823 TRACE("(%p)->(%p)\n", iface
, pstrAbsoluteUri
);
4824 return IUri_GetPropertyBSTR(iface
, Uri_PROPERTY_ABSOLUTE_URI
, pstrAbsoluteUri
, 0);
4827 static HRESULT WINAPI
Uri_GetAuthority(IUri
*iface
, BSTR
*pstrAuthority
)
4829 TRACE("(%p)->(%p)\n", iface
, pstrAuthority
);
4830 return IUri_GetPropertyBSTR(iface
, Uri_PROPERTY_AUTHORITY
, pstrAuthority
, 0);
4833 static HRESULT WINAPI
Uri_GetDisplayUri(IUri
*iface
, BSTR
*pstrDisplayUri
)
4835 TRACE("(%p)->(%p)\n", iface
, pstrDisplayUri
);
4836 return IUri_GetPropertyBSTR(iface
, Uri_PROPERTY_DISPLAY_URI
, pstrDisplayUri
, 0);
4839 static HRESULT WINAPI
Uri_GetDomain(IUri
*iface
, BSTR
*pstrDomain
)
4841 TRACE("(%p)->(%p)\n", iface
, pstrDomain
);
4842 return IUri_GetPropertyBSTR(iface
, Uri_PROPERTY_DOMAIN
, pstrDomain
, 0);
4845 static HRESULT WINAPI
Uri_GetExtension(IUri
*iface
, BSTR
*pstrExtension
)
4847 TRACE("(%p)->(%p)\n", iface
, pstrExtension
);
4848 return IUri_GetPropertyBSTR(iface
, Uri_PROPERTY_EXTENSION
, pstrExtension
, 0);
4851 static HRESULT WINAPI
Uri_GetFragment(IUri
*iface
, BSTR
*pstrFragment
)
4853 TRACE("(%p)->(%p)\n", iface
, pstrFragment
);
4854 return IUri_GetPropertyBSTR(iface
, Uri_PROPERTY_FRAGMENT
, pstrFragment
, 0);
4857 static HRESULT WINAPI
Uri_GetHost(IUri
*iface
, BSTR
*pstrHost
)
4859 TRACE("(%p)->(%p)\n", iface
, pstrHost
);
4860 return IUri_GetPropertyBSTR(iface
, Uri_PROPERTY_HOST
, pstrHost
, 0);
4863 static HRESULT WINAPI
Uri_GetPassword(IUri
*iface
, BSTR
*pstrPassword
)
4865 TRACE("(%p)->(%p)\n", iface
, pstrPassword
);
4866 return IUri_GetPropertyBSTR(iface
, Uri_PROPERTY_PASSWORD
, pstrPassword
, 0);
4869 static HRESULT WINAPI
Uri_GetPath(IUri
*iface
, BSTR
*pstrPath
)
4871 TRACE("(%p)->(%p)\n", iface
, pstrPath
);
4872 return IUri_GetPropertyBSTR(iface
, Uri_PROPERTY_PATH
, pstrPath
, 0);
4875 static HRESULT WINAPI
Uri_GetPathAndQuery(IUri
*iface
, BSTR
*pstrPathAndQuery
)
4877 TRACE("(%p)->(%p)\n", iface
, pstrPathAndQuery
);
4878 return IUri_GetPropertyBSTR(iface
, Uri_PROPERTY_PATH_AND_QUERY
, pstrPathAndQuery
, 0);
4881 static HRESULT WINAPI
Uri_GetQuery(IUri
*iface
, BSTR
*pstrQuery
)
4883 TRACE("(%p)->(%p)\n", iface
, pstrQuery
);
4884 return IUri_GetPropertyBSTR(iface
, Uri_PROPERTY_QUERY
, pstrQuery
, 0);
4887 static HRESULT WINAPI
Uri_GetRawUri(IUri
*iface
, BSTR
*pstrRawUri
)
4889 TRACE("(%p)->(%p)\n", iface
, pstrRawUri
);
4890 return IUri_GetPropertyBSTR(iface
, Uri_PROPERTY_RAW_URI
, pstrRawUri
, 0);
4893 static HRESULT WINAPI
Uri_GetSchemeName(IUri
*iface
, BSTR
*pstrSchemeName
)
4895 TRACE("(%p)->(%p)\n", iface
, pstrSchemeName
);
4896 return IUri_GetPropertyBSTR(iface
, Uri_PROPERTY_SCHEME_NAME
, pstrSchemeName
, 0);
4899 static HRESULT WINAPI
Uri_GetUserInfo(IUri
*iface
, BSTR
*pstrUserInfo
)
4901 TRACE("(%p)->(%p)\n", iface
, pstrUserInfo
);
4902 return IUri_GetPropertyBSTR(iface
, Uri_PROPERTY_USER_INFO
, pstrUserInfo
, 0);
4905 static HRESULT WINAPI
Uri_GetUserName(IUri
*iface
, BSTR
*pstrUserName
)
4907 TRACE("(%p)->(%p)\n", iface
, pstrUserName
);
4908 return IUri_GetPropertyBSTR(iface
, Uri_PROPERTY_USER_NAME
, pstrUserName
, 0);
4911 static HRESULT WINAPI
Uri_GetHostType(IUri
*iface
, DWORD
*pdwHostType
)
4913 TRACE("(%p)->(%p)\n", iface
, pdwHostType
);
4914 return IUri_GetPropertyDWORD(iface
, Uri_PROPERTY_HOST_TYPE
, pdwHostType
, 0);
4917 static HRESULT WINAPI
Uri_GetPort(IUri
*iface
, DWORD
*pdwPort
)
4919 TRACE("(%p)->(%p)\n", iface
, pdwPort
);
4920 return IUri_GetPropertyDWORD(iface
, Uri_PROPERTY_PORT
, pdwPort
, 0);
4923 static HRESULT WINAPI
Uri_GetScheme(IUri
*iface
, DWORD
*pdwScheme
)
4925 TRACE("(%p)->(%p)\n", iface
, pdwScheme
);
4926 return IUri_GetPropertyDWORD(iface
, Uri_PROPERTY_SCHEME
, pdwScheme
, 0);
4929 static HRESULT WINAPI
Uri_GetZone(IUri
*iface
, DWORD
*pdwZone
)
4931 TRACE("(%p)->(%p)\n", iface
, pdwZone
);
4932 return IUri_GetPropertyDWORD(iface
, Uri_PROPERTY_ZONE
,pdwZone
, 0);
4935 static HRESULT WINAPI
Uri_GetProperties(IUri
*iface
, DWORD
*pdwProperties
)
4937 Uri
*This
= impl_from_IUri(iface
);
4938 TRACE("(%p %s)->(%p)\n", This
, debugstr_w(This
->canon_uri
), pdwProperties
);
4940 if(!This
->create_flags
)
4941 return E_UNEXPECTED
;
4943 return E_INVALIDARG
;
4945 /* All URIs have these. */
4946 *pdwProperties
= Uri_HAS_DISPLAY_URI
|Uri_HAS_RAW_URI
|Uri_HAS_SCHEME
|Uri_HAS_HOST_TYPE
;
4948 if(!(This
->display_modifiers
& URI_DISPLAY_NO_ABSOLUTE_URI
))
4949 *pdwProperties
|= Uri_HAS_ABSOLUTE_URI
;
4951 if(This
->scheme_start
> -1)
4952 *pdwProperties
|= Uri_HAS_SCHEME_NAME
;
4954 if(This
->authority_start
> -1) {
4955 *pdwProperties
|= Uri_HAS_AUTHORITY
;
4956 if(This
->userinfo_start
> -1) {
4957 *pdwProperties
|= Uri_HAS_USER_INFO
;
4958 if(This
->userinfo_split
!= 0)
4959 *pdwProperties
|= Uri_HAS_USER_NAME
;
4961 if(This
->userinfo_split
> -1)
4962 *pdwProperties
|= Uri_HAS_PASSWORD
;
4963 if(This
->host_start
> -1)
4964 *pdwProperties
|= Uri_HAS_HOST
;
4965 if(This
->domain_offset
> -1)
4966 *pdwProperties
|= Uri_HAS_DOMAIN
;
4970 *pdwProperties
|= Uri_HAS_PORT
;
4971 if(This
->path_start
> -1)
4972 *pdwProperties
|= Uri_HAS_PATH
|Uri_HAS_PATH_AND_QUERY
;
4973 if(This
->query_start
> -1)
4974 *pdwProperties
|= Uri_HAS_QUERY
|Uri_HAS_PATH_AND_QUERY
;
4976 if(This
->extension_offset
> -1)
4977 *pdwProperties
|= Uri_HAS_EXTENSION
;
4979 if(This
->fragment_start
> -1)
4980 *pdwProperties
|= Uri_HAS_FRAGMENT
;
4985 static HRESULT WINAPI
Uri_IsEqual(IUri
*iface
, IUri
*pUri
, BOOL
*pfEqual
)
4987 Uri
*This
= impl_from_IUri(iface
);
4990 TRACE("(%p %s)->(%p %p)\n", This
, debugstr_w(This
->canon_uri
), pUri
, pfEqual
);
4992 if(!This
->create_flags
)
4993 return E_UNEXPECTED
;
5000 /* For some reason Windows returns S_OK here... */
5004 /* Try to convert it to a Uri (allows for a more simple comparison). */
5005 if(!(other
= get_uri_obj(pUri
))) {
5006 FIXME("(%p)->(%p %p) No support for unknown IUri's yet.\n", iface
, pUri
, pfEqual
);
5010 TRACE("comparing to %s\n", debugstr_w(other
->canon_uri
));
5011 return compare_uris(This
, other
, pfEqual
);
5014 static const IUriVtbl UriVtbl
= {
5018 Uri_GetPropertyBSTR
,
5019 Uri_GetPropertyLength
,
5020 Uri_GetPropertyDWORD
,
5031 Uri_GetPathAndQuery
,
5045 static inline Uri
* impl_from_IUriBuilderFactory(IUriBuilderFactory
*iface
)
5047 return CONTAINING_RECORD(iface
, Uri
, IUriBuilderFactory_iface
);
5050 static HRESULT WINAPI
UriBuilderFactory_QueryInterface(IUriBuilderFactory
*iface
, REFIID riid
, void **ppv
)
5052 Uri
*This
= impl_from_IUriBuilderFactory(iface
);
5053 return IUri_QueryInterface(&This
->IUri_iface
, riid
, ppv
);
5056 static ULONG WINAPI
UriBuilderFactory_AddRef(IUriBuilderFactory
*iface
)
5058 Uri
*This
= impl_from_IUriBuilderFactory(iface
);
5059 return IUri_AddRef(&This
->IUri_iface
);
5062 static ULONG WINAPI
UriBuilderFactory_Release(IUriBuilderFactory
*iface
)
5064 Uri
*This
= impl_from_IUriBuilderFactory(iface
);
5065 return IUri_Release(&This
->IUri_iface
);
5068 static HRESULT WINAPI
UriBuilderFactory_CreateIUriBuilder(IUriBuilderFactory
*iface
,
5070 DWORD_PTR dwReserved
,
5071 IUriBuilder
**ppIUriBuilder
)
5073 Uri
*This
= impl_from_IUriBuilderFactory(iface
);
5074 TRACE("(%p)->(%08x %08x %p)\n", This
, dwFlags
, (DWORD
)dwReserved
, ppIUriBuilder
);
5079 if(dwFlags
|| dwReserved
) {
5080 *ppIUriBuilder
= NULL
;
5081 return E_INVALIDARG
;
5084 return CreateIUriBuilder(NULL
, 0, 0, ppIUriBuilder
);
5087 static HRESULT WINAPI
UriBuilderFactory_CreateInitializedIUriBuilder(IUriBuilderFactory
*iface
,
5089 DWORD_PTR dwReserved
,
5090 IUriBuilder
**ppIUriBuilder
)
5092 Uri
*This
= impl_from_IUriBuilderFactory(iface
);
5093 TRACE("(%p)->(%08x %08x %p)\n", This
, dwFlags
, (DWORD
)dwReserved
, ppIUriBuilder
);
5098 if(dwFlags
|| dwReserved
) {
5099 *ppIUriBuilder
= NULL
;
5100 return E_INVALIDARG
;
5103 return CreateIUriBuilder(&This
->IUri_iface
, 0, 0, ppIUriBuilder
);
5106 static const IUriBuilderFactoryVtbl UriBuilderFactoryVtbl
= {
5107 UriBuilderFactory_QueryInterface
,
5108 UriBuilderFactory_AddRef
,
5109 UriBuilderFactory_Release
,
5110 UriBuilderFactory_CreateIUriBuilder
,
5111 UriBuilderFactory_CreateInitializedIUriBuilder
5114 static inline Uri
* impl_from_IPersistStream(IPersistStream
*iface
)
5116 return CONTAINING_RECORD(iface
, Uri
, IPersistStream_iface
);
5119 static HRESULT WINAPI
PersistStream_QueryInterface(IPersistStream
*iface
, REFIID riid
, void **ppvObject
)
5121 Uri
*This
= impl_from_IPersistStream(iface
);
5122 return IUri_QueryInterface(&This
->IUri_iface
, riid
, ppvObject
);
5125 static ULONG WINAPI
PersistStream_AddRef(IPersistStream
*iface
)
5127 Uri
*This
= impl_from_IPersistStream(iface
);
5128 return IUri_AddRef(&This
->IUri_iface
);
5131 static ULONG WINAPI
PersistStream_Release(IPersistStream
*iface
)
5133 Uri
*This
= impl_from_IPersistStream(iface
);
5134 return IUri_Release(&This
->IUri_iface
);
5137 static HRESULT WINAPI
PersistStream_GetClassID(IPersistStream
*iface
, CLSID
*pClassID
)
5139 Uri
*This
= impl_from_IPersistStream(iface
);
5140 FIXME("(%p)->(%p)\n", This
, pClassID
);
5144 static HRESULT WINAPI
PersistStream_IsDirty(IPersistStream
*iface
)
5146 Uri
*This
= impl_from_IPersistStream(iface
);
5147 TRACE("(%p)\n", This
);
5151 static HRESULT WINAPI
PersistStream_Load(IPersistStream
*iface
, IStream
*pStm
)
5153 Uri
*This
= impl_from_IPersistStream(iface
);
5154 FIXME("(%p)->(%p)\n", This
, pStm
);
5158 struct persist_uri
{
5167 static inline BYTE
* persist_stream_add_strprop(Uri
*This
, BYTE
*p
, DWORD type
, DWORD len
, WCHAR
*data
)
5169 len
*= sizeof(WCHAR
);
5172 *(DWORD
*)p
= len
+sizeof(WCHAR
);
5174 memcpy(p
, data
, len
);
5177 return p
+sizeof(WCHAR
);
5180 static HRESULT WINAPI
PersistStream_Save(IPersistStream
*iface
, IStream
*pStm
, BOOL fClearDirty
)
5182 Uri
*This
= impl_from_IPersistStream(iface
);
5183 ULARGE_INTEGER size
;
5184 struct persist_uri
*data
;
5188 TRACE("(%p)->(%p %x)\n", This
, pStm
, fClearDirty
);
5191 return E_INVALIDARG
;
5193 hres
= IPersistStream_GetSizeMax(&This
->IPersistStream_iface
, &size
);
5197 data
= heap_alloc_zero(size
.u
.LowPart
);
5199 return E_OUTOFMEMORY
;
5201 data
->size
= size
.u
.LowPart
;
5202 data
->create_flags
= This
->create_flags
;
5204 if(This
->create_flags
) {
5205 data
->fields_no
= 1;
5206 p
= persist_stream_add_strprop(This
, data
->data
, Uri_PROPERTY_RAW_URI
,
5207 SysStringLen(This
->raw_uri
), This
->raw_uri
);
5209 if(This
->scheme_type
!=URL_SCHEME_HTTP
&& This
->scheme_type
!=URL_SCHEME_HTTPS
5210 && This
->scheme_type
!=URL_SCHEME_FTP
) {
5211 hres
= IStream_Write(pStm
, data
, data
->size
-2, NULL
);
5216 if(This
->fragment_len
) {
5218 p
= persist_stream_add_strprop(This
, p
, Uri_PROPERTY_FRAGMENT
,
5219 This
->fragment_len
, This
->canon_uri
+This
->fragment_start
);
5222 if(This
->host_len
) {
5224 if(This
->host_type
== Uri_HOST_IPV6
)
5225 p
= persist_stream_add_strprop(This
, p
, Uri_PROPERTY_HOST
,
5226 This
->host_len
-2, This
->canon_uri
+This
->host_start
+1);
5228 p
= persist_stream_add_strprop(This
, p
, Uri_PROPERTY_HOST
,
5229 This
->host_len
, This
->canon_uri
+This
->host_start
);
5232 if(This
->userinfo_split
> -1) {
5234 p
= persist_stream_add_strprop(This
, p
, Uri_PROPERTY_PASSWORD
,
5235 This
->userinfo_len
-This
->userinfo_split
-1,
5236 This
->canon_uri
+This
->userinfo_start
+This
->userinfo_split
+1);
5239 if(This
->path_len
) {
5241 p
= persist_stream_add_strprop(This
, p
, Uri_PROPERTY_PATH
,
5242 This
->path_len
, This
->canon_uri
+This
->path_start
);
5245 if(This
->has_port
) {
5247 *(DWORD
*)p
= Uri_PROPERTY_PORT
;
5249 *(DWORD
*)p
= sizeof(DWORD
);
5251 *(DWORD
*)p
= This
->port
;
5255 if(This
->query_len
) {
5257 p
= persist_stream_add_strprop(This
, p
, Uri_PROPERTY_QUERY
,
5258 This
->query_len
, This
->canon_uri
+This
->query_start
);
5261 if(This
->scheme_len
) {
5263 p
= persist_stream_add_strprop(This
, p
, Uri_PROPERTY_SCHEME_NAME
,
5264 This
->scheme_len
, This
->canon_uri
+This
->scheme_start
);
5267 if(This
->userinfo_start
>-1 && This
->userinfo_split
!=0) {
5269 if(This
->userinfo_split
> -1)
5270 p
= persist_stream_add_strprop(This
, p
, Uri_PROPERTY_USER_NAME
,
5271 This
->userinfo_split
, This
->canon_uri
+This
->userinfo_start
);
5273 p
= persist_stream_add_strprop(This
, p
, Uri_PROPERTY_USER_NAME
,
5274 This
->userinfo_len
, This
->canon_uri
+This
->userinfo_start
);
5277 hres
= IStream_Write(pStm
, data
, data
->size
-2, NULL
);
5282 static HRESULT WINAPI
PersistStream_GetSizeMax(IPersistStream
*iface
, ULARGE_INTEGER
*pcbSize
)
5284 Uri
*This
= impl_from_IPersistStream(iface
);
5285 TRACE("(%p)->(%p)\n", This
, pcbSize
);
5288 return E_INVALIDARG
;
5290 pcbSize
->u
.LowPart
= 2+sizeof(struct persist_uri
);
5291 pcbSize
->u
.HighPart
= 0;
5292 if(This
->create_flags
)
5293 pcbSize
->u
.LowPart
+= (SysStringLen(This
->raw_uri
)+1)*sizeof(WCHAR
) + 2*sizeof(DWORD
);
5294 else /* there's no place for fields no */
5295 pcbSize
->u
.LowPart
-= sizeof(DWORD
);
5296 if(This
->scheme_type
!=URL_SCHEME_HTTP
&& This
->scheme_type
!=URL_SCHEME_HTTPS
5297 && This
->scheme_type
!=URL_SCHEME_FTP
)
5300 if(This
->fragment_len
)
5301 pcbSize
->u
.LowPart
+= (This
->fragment_len
+1)*sizeof(WCHAR
) + 2*sizeof(DWORD
);
5302 if(This
->host_len
) {
5303 if(This
->host_type
== Uri_HOST_IPV6
)
5304 pcbSize
->u
.LowPart
+= (This
->host_len
-1)*sizeof(WCHAR
) + 2*sizeof(DWORD
);
5306 pcbSize
->u
.LowPart
+= (This
->host_len
+1)*sizeof(WCHAR
) + 2*sizeof(DWORD
);
5308 if(This
->userinfo_split
> -1)
5309 pcbSize
->u
.LowPart
+= (This
->userinfo_len
-This
->userinfo_split
)*sizeof(WCHAR
) + 2*sizeof(DWORD
);
5311 pcbSize
->u
.LowPart
+= (This
->path_len
+1)*sizeof(WCHAR
) + 2*sizeof(DWORD
);
5313 pcbSize
->u
.LowPart
+= 3*sizeof(DWORD
);
5315 pcbSize
->u
.LowPart
+= (This
->query_len
+1)*sizeof(WCHAR
) + 2*sizeof(DWORD
);
5316 if(This
->scheme_len
)
5317 pcbSize
->u
.LowPart
+= (This
->scheme_len
+1)*sizeof(WCHAR
) + 2*sizeof(DWORD
);
5318 if(This
->userinfo_start
>-1 && This
->userinfo_split
!=0) {
5319 if(This
->userinfo_split
> -1)
5320 pcbSize
->u
.LowPart
+= (This
->userinfo_split
+1)*sizeof(WCHAR
) + 2*sizeof(DWORD
);
5322 pcbSize
->u
.LowPart
+= (This
->userinfo_len
+1)*sizeof(WCHAR
) + 2*sizeof(DWORD
);
5327 static const IPersistStreamVtbl PersistStreamVtbl
= {
5328 PersistStream_QueryInterface
,
5329 PersistStream_AddRef
,
5330 PersistStream_Release
,
5331 PersistStream_GetClassID
,
5332 PersistStream_IsDirty
,
5335 PersistStream_GetSizeMax
5338 HRESULT
Uri_Construct(IUnknown
*pUnkOuter
, LPVOID
*ppobj
)
5340 Uri
*ret
= heap_alloc_zero(sizeof(Uri
));
5342 TRACE("(%p %p)\n", pUnkOuter
, ppobj
);
5346 return E_OUTOFMEMORY
;
5348 ret
->IUri_iface
.lpVtbl
= &UriVtbl
;
5349 ret
->IUriBuilderFactory_iface
.lpVtbl
= &UriBuilderFactoryVtbl
;
5350 ret
->IPersistStream_iface
.lpVtbl
= &PersistStreamVtbl
;
5353 *ppobj
= &ret
->IUri_iface
;
5357 /***********************************************************************
5358 * CreateUri (urlmon.@)
5360 * Creates a new IUri object using the URI represented by pwzURI. This function
5361 * parses and validates the components of pwzURI and then canonicalizes the
5362 * parsed components.
5365 * pwzURI [I] The URI to parse, validate, and canonicalize.
5366 * dwFlags [I] Flags which can affect how the parsing/canonicalization is performed.
5367 * dwReserved [I] Reserved (not used).
5368 * ppURI [O] The resulting IUri after parsing/canonicalization occurs.
5371 * Success: Returns S_OK. ppURI contains the pointer to the newly allocated IUri.
5372 * Failure: E_INVALIDARG if there are invalid flag combinations in dwFlags, or an
5373 * invalid parameter, or pwzURI doesn't represent a valid URI.
5374 * E_OUTOFMEMORY if any memory allocation fails.
5378 * Uri_CREATE_CANONICALIZE, Uri_CREATE_DECODE_EXTRA_INFO, Uri_CREATE_CRACK_UNKNOWN_SCHEMES,
5379 * Uri_CREATE_PRE_PROCESS_HTML_URI, Uri_CREATE_NO_IE_SETTINGS.
5381 HRESULT WINAPI
CreateUri(LPCWSTR pwzURI
, DWORD dwFlags
, DWORD_PTR dwReserved
, IUri
**ppURI
)
5383 const DWORD supported_flags
= Uri_CREATE_ALLOW_RELATIVE
|Uri_CREATE_ALLOW_IMPLICIT_WILDCARD_SCHEME
|
5384 Uri_CREATE_ALLOW_IMPLICIT_FILE_SCHEME
|Uri_CREATE_NO_CANONICALIZE
|Uri_CREATE_CANONICALIZE
|
5385 Uri_CREATE_DECODE_EXTRA_INFO
|Uri_CREATE_NO_DECODE_EXTRA_INFO
|Uri_CREATE_CRACK_UNKNOWN_SCHEMES
|
5386 Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES
|Uri_CREATE_PRE_PROCESS_HTML_URI
|Uri_CREATE_NO_PRE_PROCESS_HTML_URI
|
5387 Uri_CREATE_NO_IE_SETTINGS
|Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS
|Uri_CREATE_FILE_USE_DOS_PATH
;
5392 TRACE("(%s %x %x %p)\n", debugstr_w(pwzURI
), dwFlags
, (DWORD
)dwReserved
, ppURI
);
5395 return E_INVALIDARG
;
5399 return E_INVALIDARG
;
5402 /* Check for invalid flags. */
5403 if(has_invalid_flag_combination(dwFlags
)) {
5405 return E_INVALIDARG
;
5408 /* Currently unsupported. */
5409 if(dwFlags
& ~supported_flags
)
5410 FIXME("Ignoring unsupported flag(s) %x\n", dwFlags
& ~supported_flags
);
5412 hr
= Uri_Construct(NULL
, (void**)&ret
);
5418 /* Explicitly set the default flags if it doesn't cause a flag conflict. */
5419 apply_default_flags(&dwFlags
);
5421 /* Pre process the URI, unless told otherwise. */
5422 if(!(dwFlags
& Uri_CREATE_NO_PRE_PROCESS_HTML_URI
))
5423 ret
->raw_uri
= pre_process_uri(pwzURI
);
5425 ret
->raw_uri
= SysAllocString(pwzURI
);
5429 return E_OUTOFMEMORY
;
5432 memset(&data
, 0, sizeof(parse_data
));
5433 data
.uri
= ret
->raw_uri
;
5435 /* Validate and parse the URI into its components. */
5436 if(!parse_uri(&data
, dwFlags
)) {
5437 /* Encountered an unsupported or invalid URI */
5438 IUri_Release(&ret
->IUri_iface
);
5440 return E_INVALIDARG
;
5443 /* Canonicalize the URI. */
5444 hr
= canonicalize_uri(&data
, ret
, dwFlags
);
5446 IUri_Release(&ret
->IUri_iface
);
5451 ret
->create_flags
= dwFlags
;
5453 *ppURI
= &ret
->IUri_iface
;
5457 /***********************************************************************
5458 * CreateUriWithFragment (urlmon.@)
5460 * Creates a new IUri object. This is almost the same as CreateUri, expect that
5461 * it allows you to explicitly specify a fragment (pwzFragment) for pwzURI.
5464 * pwzURI [I] The URI to parse and perform canonicalization on.
5465 * pwzFragment [I] The explicit fragment string which should be added to pwzURI.
5466 * dwFlags [I] The flags which will be passed to CreateUri.
5467 * dwReserved [I] Reserved (not used).
5468 * ppURI [O] The resulting IUri after parsing/canonicalization.
5471 * Success: S_OK. ppURI contains the pointer to the newly allocated IUri.
5472 * Failure: E_INVALIDARG if pwzURI already contains a fragment and pwzFragment
5473 * isn't NULL. Will also return E_INVALIDARG for the same reasons as
5474 * CreateUri will. E_OUTOFMEMORY if any allocation fails.
5476 HRESULT WINAPI
CreateUriWithFragment(LPCWSTR pwzURI
, LPCWSTR pwzFragment
, DWORD dwFlags
,
5477 DWORD_PTR dwReserved
, IUri
**ppURI
)
5480 TRACE("(%s %s %x %x %p)\n", debugstr_w(pwzURI
), debugstr_w(pwzFragment
), dwFlags
, (DWORD
)dwReserved
, ppURI
);
5483 return E_INVALIDARG
;
5487 return E_INVALIDARG
;
5490 /* Check if a fragment should be appended to the URI string. */
5493 DWORD uri_len
, frag_len
;
5496 /* Check if the original URI already has a fragment component. */
5497 if(StrChrW(pwzURI
, '#')) {
5499 return E_INVALIDARG
;
5502 uri_len
= lstrlenW(pwzURI
);
5503 frag_len
= lstrlenW(pwzFragment
);
5505 /* If the fragment doesn't start with a '#', one will be added. */
5506 add_pound
= *pwzFragment
!= '#';
5509 uriW
= heap_alloc((uri_len
+frag_len
+2)*sizeof(WCHAR
));
5511 uriW
= heap_alloc((uri_len
+frag_len
+1)*sizeof(WCHAR
));
5514 return E_OUTOFMEMORY
;
5516 memcpy(uriW
, pwzURI
, uri_len
*sizeof(WCHAR
));
5518 uriW
[uri_len
++] = '#';
5519 memcpy(uriW
+uri_len
, pwzFragment
, (frag_len
+1)*sizeof(WCHAR
));
5521 hres
= CreateUri(uriW
, dwFlags
, 0, ppURI
);
5525 /* A fragment string wasn't specified, so just forward the call. */
5526 hres
= CreateUri(pwzURI
, dwFlags
, 0, ppURI
);
5531 static HRESULT
build_uri(const UriBuilder
*builder
, IUri
**uri
, DWORD create_flags
,
5532 DWORD use_orig_flags
, DWORD encoding_mask
)
5541 if(encoding_mask
&& (!builder
->uri
|| builder
->modified_props
)) {
5546 /* Decide what flags should be used when creating the Uri. */
5547 if((use_orig_flags
& UriBuilder_USE_ORIGINAL_FLAGS
) && builder
->uri
)
5548 create_flags
= builder
->uri
->create_flags
;
5550 if(has_invalid_flag_combination(create_flags
)) {
5552 return E_INVALIDARG
;
5555 /* Set the default flags if they don't cause a conflict. */
5556 apply_default_flags(&create_flags
);
5559 /* Return the base IUri if no changes have been made and the create_flags match. */
5560 if(builder
->uri
&& !builder
->modified_props
&& builder
->uri
->create_flags
== create_flags
) {
5561 *uri
= &builder
->uri
->IUri_iface
;
5566 hr
= validate_components(builder
, &data
, create_flags
);
5572 hr
= Uri_Construct(NULL
, (void**)&ret
);
5578 hr
= generate_uri(builder
, &data
, ret
, create_flags
);
5580 IUri_Release(&ret
->IUri_iface
);
5585 *uri
= &ret
->IUri_iface
;
5589 static inline UriBuilder
* impl_from_IUriBuilder(IUriBuilder
*iface
)
5591 return CONTAINING_RECORD(iface
, UriBuilder
, IUriBuilder_iface
);
5594 static HRESULT WINAPI
UriBuilder_QueryInterface(IUriBuilder
*iface
, REFIID riid
, void **ppv
)
5596 UriBuilder
*This
= impl_from_IUriBuilder(iface
);
5598 if(IsEqualGUID(&IID_IUnknown
, riid
)) {
5599 TRACE("(%p)->(IID_IUnknown %p)\n", This
, ppv
);
5600 *ppv
= &This
->IUriBuilder_iface
;
5601 }else if(IsEqualGUID(&IID_IUriBuilder
, riid
)) {
5602 TRACE("(%p)->(IID_IUriBuilder %p)\n", This
, ppv
);
5603 *ppv
= &This
->IUriBuilder_iface
;
5605 TRACE("(%p)->(%s %p)\n", This
, debugstr_guid(riid
), ppv
);
5607 return E_NOINTERFACE
;
5610 IUnknown_AddRef((IUnknown
*)*ppv
);
5614 static ULONG WINAPI
UriBuilder_AddRef(IUriBuilder
*iface
)
5616 UriBuilder
*This
= impl_from_IUriBuilder(iface
);
5617 LONG ref
= InterlockedIncrement(&This
->ref
);
5619 TRACE("(%p) ref=%d\n", This
, ref
);
5624 static ULONG WINAPI
UriBuilder_Release(IUriBuilder
*iface
)
5626 UriBuilder
*This
= impl_from_IUriBuilder(iface
);
5627 LONG ref
= InterlockedDecrement(&This
->ref
);
5629 TRACE("(%p) ref=%d\n", This
, ref
);
5632 if(This
->uri
) IUri_Release(&This
->uri
->IUri_iface
);
5633 heap_free(This
->fragment
);
5634 heap_free(This
->host
);
5635 heap_free(This
->password
);
5636 heap_free(This
->path
);
5637 heap_free(This
->query
);
5638 heap_free(This
->scheme
);
5639 heap_free(This
->username
);
5646 static HRESULT WINAPI
UriBuilder_CreateUriSimple(IUriBuilder
*iface
,
5647 DWORD dwAllowEncodingPropertyMask
,
5648 DWORD_PTR dwReserved
,
5651 UriBuilder
*This
= impl_from_IUriBuilder(iface
);
5653 TRACE("(%p)->(%d %d %p)\n", This
, dwAllowEncodingPropertyMask
, (DWORD
)dwReserved
, ppIUri
);
5655 hr
= build_uri(This
, ppIUri
, 0, UriBuilder_USE_ORIGINAL_FLAGS
, dwAllowEncodingPropertyMask
);
5657 FIXME("(%p)->(%d %d %p)\n", This
, dwAllowEncodingPropertyMask
, (DWORD
)dwReserved
, ppIUri
);
5661 static HRESULT WINAPI
UriBuilder_CreateUri(IUriBuilder
*iface
,
5662 DWORD dwCreateFlags
,
5663 DWORD dwAllowEncodingPropertyMask
,
5664 DWORD_PTR dwReserved
,
5667 UriBuilder
*This
= impl_from_IUriBuilder(iface
);
5669 TRACE("(%p)->(0x%08x %d %d %p)\n", This
, dwCreateFlags
, dwAllowEncodingPropertyMask
, (DWORD
)dwReserved
, ppIUri
);
5671 if(dwCreateFlags
== -1)
5672 hr
= build_uri(This
, ppIUri
, 0, UriBuilder_USE_ORIGINAL_FLAGS
, dwAllowEncodingPropertyMask
);
5674 hr
= build_uri(This
, ppIUri
, dwCreateFlags
, 0, dwAllowEncodingPropertyMask
);
5677 FIXME("(%p)->(0x%08x %d %d %p)\n", This
, dwCreateFlags
, dwAllowEncodingPropertyMask
, (DWORD
)dwReserved
, ppIUri
);
5681 static HRESULT WINAPI
UriBuilder_CreateUriWithFlags(IUriBuilder
*iface
,
5682 DWORD dwCreateFlags
,
5683 DWORD dwUriBuilderFlags
,
5684 DWORD dwAllowEncodingPropertyMask
,
5685 DWORD_PTR dwReserved
,
5688 UriBuilder
*This
= impl_from_IUriBuilder(iface
);
5690 TRACE("(%p)->(0x%08x 0x%08x %d %d %p)\n", This
, dwCreateFlags
, dwUriBuilderFlags
,
5691 dwAllowEncodingPropertyMask
, (DWORD
)dwReserved
, ppIUri
);
5693 hr
= build_uri(This
, ppIUri
, dwCreateFlags
, dwUriBuilderFlags
, dwAllowEncodingPropertyMask
);
5695 FIXME("(%p)->(0x%08x 0x%08x %d %d %p)\n", This
, dwCreateFlags
, dwUriBuilderFlags
,
5696 dwAllowEncodingPropertyMask
, (DWORD
)dwReserved
, ppIUri
);
5700 static HRESULT WINAPI
UriBuilder_GetIUri(IUriBuilder
*iface
, IUri
**ppIUri
)
5702 UriBuilder
*This
= impl_from_IUriBuilder(iface
);
5703 TRACE("(%p)->(%p)\n", This
, ppIUri
);
5709 IUri
*uri
= &This
->uri
->IUri_iface
;
5718 static HRESULT WINAPI
UriBuilder_SetIUri(IUriBuilder
*iface
, IUri
*pIUri
)
5720 UriBuilder
*This
= impl_from_IUriBuilder(iface
);
5721 TRACE("(%p)->(%p)\n", This
, pIUri
);
5726 if((uri
= get_uri_obj(pIUri
))) {
5727 /* Only reset the builder if it's Uri isn't the same as
5728 * the Uri passed to the function.
5730 if(This
->uri
!= uri
) {
5731 reset_builder(This
);
5735 This
->port
= uri
->port
;
5740 FIXME("(%p)->(%p) Unknown IUri types not supported yet.\n", This
, pIUri
);
5743 } else if(This
->uri
)
5744 /* Only reset the builder if it's Uri isn't NULL. */
5745 reset_builder(This
);
5750 static HRESULT WINAPI
UriBuilder_GetFragment(IUriBuilder
*iface
, DWORD
*pcchFragment
, LPCWSTR
*ppwzFragment
)
5752 UriBuilder
*This
= impl_from_IUriBuilder(iface
);
5753 TRACE("(%p)->(%p %p)\n", This
, pcchFragment
, ppwzFragment
);
5755 if(!This
->uri
|| This
->uri
->fragment_start
== -1 || This
->modified_props
& Uri_HAS_FRAGMENT
)
5756 return get_builder_component(&This
->fragment
, &This
->fragment_len
, NULL
, 0, ppwzFragment
, pcchFragment
);
5758 return get_builder_component(&This
->fragment
, &This
->fragment_len
, This
->uri
->canon_uri
+This
->uri
->fragment_start
,
5759 This
->uri
->fragment_len
, ppwzFragment
, pcchFragment
);
5762 static HRESULT WINAPI
UriBuilder_GetHost(IUriBuilder
*iface
, DWORD
*pcchHost
, LPCWSTR
*ppwzHost
)
5764 UriBuilder
*This
= impl_from_IUriBuilder(iface
);
5765 TRACE("(%p)->(%p %p)\n", This
, pcchHost
, ppwzHost
);
5767 if(!This
->uri
|| This
->uri
->host_start
== -1 || This
->modified_props
& Uri_HAS_HOST
)
5768 return get_builder_component(&This
->host
, &This
->host_len
, NULL
, 0, ppwzHost
, pcchHost
);
5770 if(This
->uri
->host_type
== Uri_HOST_IPV6
)
5771 /* Don't include the '[' and ']' around the address. */
5772 return get_builder_component(&This
->host
, &This
->host_len
, This
->uri
->canon_uri
+This
->uri
->host_start
+1,
5773 This
->uri
->host_len
-2, ppwzHost
, pcchHost
);
5775 return get_builder_component(&This
->host
, &This
->host_len
, This
->uri
->canon_uri
+This
->uri
->host_start
,
5776 This
->uri
->host_len
, ppwzHost
, pcchHost
);
5780 static HRESULT WINAPI
UriBuilder_GetPassword(IUriBuilder
*iface
, DWORD
*pcchPassword
, LPCWSTR
*ppwzPassword
)
5782 UriBuilder
*This
= impl_from_IUriBuilder(iface
);
5783 TRACE("(%p)->(%p %p)\n", This
, pcchPassword
, ppwzPassword
);
5785 if(!This
->uri
|| This
->uri
->userinfo_split
== -1 || This
->modified_props
& Uri_HAS_PASSWORD
)
5786 return get_builder_component(&This
->password
, &This
->password_len
, NULL
, 0, ppwzPassword
, pcchPassword
);
5788 const WCHAR
*start
= This
->uri
->canon_uri
+This
->uri
->userinfo_start
+This
->uri
->userinfo_split
+1;
5789 DWORD len
= This
->uri
->userinfo_len
-This
->uri
->userinfo_split
-1;
5790 return get_builder_component(&This
->password
, &This
->password_len
, start
, len
, ppwzPassword
, pcchPassword
);
5794 static HRESULT WINAPI
UriBuilder_GetPath(IUriBuilder
*iface
, DWORD
*pcchPath
, LPCWSTR
*ppwzPath
)
5796 UriBuilder
*This
= impl_from_IUriBuilder(iface
);
5797 TRACE("(%p)->(%p %p)\n", This
, pcchPath
, ppwzPath
);
5799 if(!This
->uri
|| This
->uri
->path_start
== -1 || This
->modified_props
& Uri_HAS_PATH
)
5800 return get_builder_component(&This
->path
, &This
->path_len
, NULL
, 0, ppwzPath
, pcchPath
);
5802 return get_builder_component(&This
->path
, &This
->path_len
, This
->uri
->canon_uri
+This
->uri
->path_start
,
5803 This
->uri
->path_len
, ppwzPath
, pcchPath
);
5806 static HRESULT WINAPI
UriBuilder_GetPort(IUriBuilder
*iface
, BOOL
*pfHasPort
, DWORD
*pdwPort
)
5808 UriBuilder
*This
= impl_from_IUriBuilder(iface
);
5809 TRACE("(%p)->(%p %p)\n", This
, pfHasPort
, pdwPort
);
5822 *pfHasPort
= This
->has_port
;
5823 *pdwPort
= This
->port
;
5827 static HRESULT WINAPI
UriBuilder_GetQuery(IUriBuilder
*iface
, DWORD
*pcchQuery
, LPCWSTR
*ppwzQuery
)
5829 UriBuilder
*This
= impl_from_IUriBuilder(iface
);
5830 TRACE("(%p)->(%p %p)\n", This
, pcchQuery
, ppwzQuery
);
5832 if(!This
->uri
|| This
->uri
->query_start
== -1 || This
->modified_props
& Uri_HAS_QUERY
)
5833 return get_builder_component(&This
->query
, &This
->query_len
, NULL
, 0, ppwzQuery
, pcchQuery
);
5835 return get_builder_component(&This
->query
, &This
->query_len
, This
->uri
->canon_uri
+This
->uri
->query_start
,
5836 This
->uri
->query_len
, ppwzQuery
, pcchQuery
);
5839 static HRESULT WINAPI
UriBuilder_GetSchemeName(IUriBuilder
*iface
, DWORD
*pcchSchemeName
, LPCWSTR
*ppwzSchemeName
)
5841 UriBuilder
*This
= impl_from_IUriBuilder(iface
);
5842 TRACE("(%p)->(%p %p)\n", This
, pcchSchemeName
, ppwzSchemeName
);
5844 if(!This
->uri
|| This
->uri
->scheme_start
== -1 || This
->modified_props
& Uri_HAS_SCHEME_NAME
)
5845 return get_builder_component(&This
->scheme
, &This
->scheme_len
, NULL
, 0, ppwzSchemeName
, pcchSchemeName
);
5847 return get_builder_component(&This
->scheme
, &This
->scheme_len
, This
->uri
->canon_uri
+This
->uri
->scheme_start
,
5848 This
->uri
->scheme_len
, ppwzSchemeName
, pcchSchemeName
);
5851 static HRESULT WINAPI
UriBuilder_GetUserName(IUriBuilder
*iface
, DWORD
*pcchUserName
, LPCWSTR
*ppwzUserName
)
5853 UriBuilder
*This
= impl_from_IUriBuilder(iface
);
5854 TRACE("(%p)->(%p %p)\n", This
, pcchUserName
, ppwzUserName
);
5856 if(!This
->uri
|| This
->uri
->userinfo_start
== -1 || This
->uri
->userinfo_split
== 0 ||
5857 This
->modified_props
& Uri_HAS_USER_NAME
)
5858 return get_builder_component(&This
->username
, &This
->username_len
, NULL
, 0, ppwzUserName
, pcchUserName
);
5860 const WCHAR
*start
= This
->uri
->canon_uri
+This
->uri
->userinfo_start
;
5862 /* Check if there's a password in the userinfo section. */
5863 if(This
->uri
->userinfo_split
> -1)
5864 /* Don't include the password. */
5865 return get_builder_component(&This
->username
, &This
->username_len
, start
,
5866 This
->uri
->userinfo_split
, ppwzUserName
, pcchUserName
);
5868 return get_builder_component(&This
->username
, &This
->username_len
, start
,
5869 This
->uri
->userinfo_len
, ppwzUserName
, pcchUserName
);
5873 static HRESULT WINAPI
UriBuilder_SetFragment(IUriBuilder
*iface
, LPCWSTR pwzNewValue
)
5875 UriBuilder
*This
= impl_from_IUriBuilder(iface
);
5876 TRACE("(%p)->(%s)\n", This
, debugstr_w(pwzNewValue
));
5877 return set_builder_component(&This
->fragment
, &This
->fragment_len
, pwzNewValue
, '#',
5878 &This
->modified_props
, Uri_HAS_FRAGMENT
);
5881 static HRESULT WINAPI
UriBuilder_SetHost(IUriBuilder
*iface
, LPCWSTR pwzNewValue
)
5883 UriBuilder
*This
= impl_from_IUriBuilder(iface
);
5884 TRACE("(%p)->(%s)\n", This
, debugstr_w(pwzNewValue
));
5886 /* Host name can't be set to NULL. */
5888 return E_INVALIDARG
;
5890 return set_builder_component(&This
->host
, &This
->host_len
, pwzNewValue
, 0,
5891 &This
->modified_props
, Uri_HAS_HOST
);
5894 static HRESULT WINAPI
UriBuilder_SetPassword(IUriBuilder
*iface
, LPCWSTR pwzNewValue
)
5896 UriBuilder
*This
= impl_from_IUriBuilder(iface
);
5897 TRACE("(%p)->(%s)\n", This
, debugstr_w(pwzNewValue
));
5898 return set_builder_component(&This
->password
, &This
->password_len
, pwzNewValue
, 0,
5899 &This
->modified_props
, Uri_HAS_PASSWORD
);
5902 static HRESULT WINAPI
UriBuilder_SetPath(IUriBuilder
*iface
, LPCWSTR pwzNewValue
)
5904 UriBuilder
*This
= impl_from_IUriBuilder(iface
);
5905 TRACE("(%p)->(%s)\n", This
, debugstr_w(pwzNewValue
));
5906 return set_builder_component(&This
->path
, &This
->path_len
, pwzNewValue
, 0,
5907 &This
->modified_props
, Uri_HAS_PATH
);
5910 static HRESULT WINAPI
UriBuilder_SetPort(IUriBuilder
*iface
, BOOL fHasPort
, DWORD dwNewValue
)
5912 UriBuilder
*This
= impl_from_IUriBuilder(iface
);
5913 TRACE("(%p)->(%d %d)\n", This
, fHasPort
, dwNewValue
);
5915 This
->has_port
= fHasPort
;
5916 This
->port
= dwNewValue
;
5917 This
->modified_props
|= Uri_HAS_PORT
;
5921 static HRESULT WINAPI
UriBuilder_SetQuery(IUriBuilder
*iface
, LPCWSTR pwzNewValue
)
5923 UriBuilder
*This
= impl_from_IUriBuilder(iface
);
5924 TRACE("(%p)->(%s)\n", This
, debugstr_w(pwzNewValue
));
5925 return set_builder_component(&This
->query
, &This
->query_len
, pwzNewValue
, '?',
5926 &This
->modified_props
, Uri_HAS_QUERY
);
5929 static HRESULT WINAPI
UriBuilder_SetSchemeName(IUriBuilder
*iface
, LPCWSTR pwzNewValue
)
5931 UriBuilder
*This
= impl_from_IUriBuilder(iface
);
5932 TRACE("(%p)->(%s)\n", This
, debugstr_w(pwzNewValue
));
5934 /* Only set the scheme name if it's not NULL or empty. */
5935 if(!pwzNewValue
|| !*pwzNewValue
)
5936 return E_INVALIDARG
;
5938 return set_builder_component(&This
->scheme
, &This
->scheme_len
, pwzNewValue
, 0,
5939 &This
->modified_props
, Uri_HAS_SCHEME_NAME
);
5942 static HRESULT WINAPI
UriBuilder_SetUserName(IUriBuilder
*iface
, LPCWSTR pwzNewValue
)
5944 UriBuilder
*This
= impl_from_IUriBuilder(iface
);
5945 TRACE("(%p)->(%s)\n", This
, debugstr_w(pwzNewValue
));
5946 return set_builder_component(&This
->username
, &This
->username_len
, pwzNewValue
, 0,
5947 &This
->modified_props
, Uri_HAS_USER_NAME
);
5950 static HRESULT WINAPI
UriBuilder_RemoveProperties(IUriBuilder
*iface
, DWORD dwPropertyMask
)
5952 const DWORD accepted_flags
= Uri_HAS_AUTHORITY
|Uri_HAS_DOMAIN
|Uri_HAS_EXTENSION
|Uri_HAS_FRAGMENT
|Uri_HAS_HOST
|
5953 Uri_HAS_PASSWORD
|Uri_HAS_PATH
|Uri_HAS_PATH_AND_QUERY
|Uri_HAS_QUERY
|
5954 Uri_HAS_USER_INFO
|Uri_HAS_USER_NAME
;
5956 UriBuilder
*This
= impl_from_IUriBuilder(iface
);
5957 TRACE("(%p)->(0x%08x)\n", This
, dwPropertyMask
);
5959 if(dwPropertyMask
& ~accepted_flags
)
5960 return E_INVALIDARG
;
5962 if(dwPropertyMask
& Uri_HAS_FRAGMENT
)
5963 UriBuilder_SetFragment(iface
, NULL
);
5965 /* Even though you can't set the host name to NULL or an
5966 * empty string, you can still remove it... for some reason.
5968 if(dwPropertyMask
& Uri_HAS_HOST
)
5969 set_builder_component(&This
->host
, &This
->host_len
, NULL
, 0,
5970 &This
->modified_props
, Uri_HAS_HOST
);
5972 if(dwPropertyMask
& Uri_HAS_PASSWORD
)
5973 UriBuilder_SetPassword(iface
, NULL
);
5975 if(dwPropertyMask
& Uri_HAS_PATH
)
5976 UriBuilder_SetPath(iface
, NULL
);
5978 if(dwPropertyMask
& Uri_HAS_PORT
)
5979 UriBuilder_SetPort(iface
, FALSE
, 0);
5981 if(dwPropertyMask
& Uri_HAS_QUERY
)
5982 UriBuilder_SetQuery(iface
, NULL
);
5984 if(dwPropertyMask
& Uri_HAS_USER_NAME
)
5985 UriBuilder_SetUserName(iface
, NULL
);
5990 static HRESULT WINAPI
UriBuilder_HasBeenModified(IUriBuilder
*iface
, BOOL
*pfModified
)
5992 UriBuilder
*This
= impl_from_IUriBuilder(iface
);
5993 TRACE("(%p)->(%p)\n", This
, pfModified
);
5998 *pfModified
= This
->modified_props
> 0;
6002 static const IUriBuilderVtbl UriBuilderVtbl
= {
6003 UriBuilder_QueryInterface
,
6006 UriBuilder_CreateUriSimple
,
6007 UriBuilder_CreateUri
,
6008 UriBuilder_CreateUriWithFlags
,
6011 UriBuilder_GetFragment
,
6013 UriBuilder_GetPassword
,
6016 UriBuilder_GetQuery
,
6017 UriBuilder_GetSchemeName
,
6018 UriBuilder_GetUserName
,
6019 UriBuilder_SetFragment
,
6021 UriBuilder_SetPassword
,
6024 UriBuilder_SetQuery
,
6025 UriBuilder_SetSchemeName
,
6026 UriBuilder_SetUserName
,
6027 UriBuilder_RemoveProperties
,
6028 UriBuilder_HasBeenModified
,
6031 /***********************************************************************
6032 * CreateIUriBuilder (urlmon.@)
6034 HRESULT WINAPI
CreateIUriBuilder(IUri
*pIUri
, DWORD dwFlags
, DWORD_PTR dwReserved
, IUriBuilder
**ppIUriBuilder
)
6038 TRACE("(%p %x %x %p)\n", pIUri
, dwFlags
, (DWORD
)dwReserved
, ppIUriBuilder
);
6043 ret
= heap_alloc_zero(sizeof(UriBuilder
));
6045 return E_OUTOFMEMORY
;
6047 ret
->IUriBuilder_iface
.lpVtbl
= &UriBuilderVtbl
;
6053 if((uri
= get_uri_obj(pIUri
))) {
6054 if(!uri
->create_flags
)
6055 return E_UNEXPECTED
;
6060 /* Windows doesn't set 'has_port' to TRUE in this case. */
6061 ret
->port
= uri
->port
;
6065 *ppIUriBuilder
= NULL
;
6066 FIXME("(%p %x %x %p): Unknown IUri types not supported yet.\n", pIUri
, dwFlags
,
6067 (DWORD
)dwReserved
, ppIUriBuilder
);
6072 *ppIUriBuilder
= &ret
->IUriBuilder_iface
;
6076 /* Merges the base path with the relative path and stores the resulting path
6077 * and path len in 'result' and 'result_len'.
6079 static HRESULT
merge_paths(parse_data
*data
, const WCHAR
*base
, DWORD base_len
, const WCHAR
*relative
,
6080 DWORD relative_len
, WCHAR
**result
, DWORD
*result_len
, DWORD flags
)
6082 const WCHAR
*end
= NULL
;
6083 DWORD base_copy_len
= 0;
6087 /* Find the characters that will be copied over from
6090 end
= memrchrW(base
, '/', base_len
);
6091 if(!end
&& data
->scheme_type
== URL_SCHEME_FILE
)
6092 /* Try looking for a '\\'. */
6093 end
= memrchrW(base
, '\\', base_len
);
6097 base_copy_len
= (end
+1)-base
;
6098 *result
= heap_alloc((base_copy_len
+relative_len
+1)*sizeof(WCHAR
));
6100 *result
= heap_alloc((relative_len
+1)*sizeof(WCHAR
));
6104 return E_OUTOFMEMORY
;
6109 memcpy(ptr
, base
, base_copy_len
*sizeof(WCHAR
));
6110 ptr
+= base_copy_len
;
6113 memcpy(ptr
, relative
, relative_len
*sizeof(WCHAR
));
6114 ptr
+= relative_len
;
6117 *result_len
= (ptr
-*result
);
6121 static HRESULT
combine_uri(Uri
*base
, Uri
*relative
, DWORD flags
, IUri
**result
, DWORD extras
) {
6125 DWORD create_flags
= 0, len
= 0;
6127 memset(&data
, 0, sizeof(parse_data
));
6129 /* Base case is when the relative Uri has a scheme name,
6130 * if it does, then 'result' will contain the same data
6131 * as the relative Uri.
6133 if(relative
->scheme_start
> -1) {
6134 data
.uri
= SysAllocString(relative
->raw_uri
);
6137 return E_OUTOFMEMORY
;
6140 parse_uri(&data
, 0);
6142 hr
= Uri_Construct(NULL
, (void**)&ret
);
6148 if(extras
& COMBINE_URI_FORCE_FLAG_USE
) {
6149 if(flags
& URL_DONT_SIMPLIFY
)
6150 create_flags
|= Uri_CREATE_NO_CANONICALIZE
;
6151 if(flags
& URL_DONT_UNESCAPE_EXTRA_INFO
)
6152 create_flags
|= Uri_CREATE_NO_DECODE_EXTRA_INFO
;
6155 ret
->raw_uri
= data
.uri
;
6156 hr
= canonicalize_uri(&data
, ret
, create_flags
);
6158 IUri_Release(&ret
->IUri_iface
);
6163 apply_default_flags(&create_flags
);
6164 ret
->create_flags
= create_flags
;
6166 *result
= &ret
->IUri_iface
;
6169 DWORD raw_flags
= 0;
6171 if(base
->scheme_start
> -1) {
6172 data
.scheme
= base
->canon_uri
+base
->scheme_start
;
6173 data
.scheme_len
= base
->scheme_len
;
6174 data
.scheme_type
= base
->scheme_type
;
6176 data
.is_relative
= TRUE
;
6177 data
.scheme_type
= URL_SCHEME_UNKNOWN
;
6178 create_flags
|= Uri_CREATE_ALLOW_RELATIVE
;
6181 if(base
->authority_start
> -1) {
6182 if(base
->userinfo_start
> -1 && base
->userinfo_split
!= 0) {
6183 data
.username
= base
->canon_uri
+base
->userinfo_start
;
6184 data
.username_len
= (base
->userinfo_split
> -1) ? base
->userinfo_split
: base
->userinfo_len
;
6187 if(base
->userinfo_split
> -1) {
6188 data
.password
= base
->canon_uri
+base
->userinfo_start
+base
->userinfo_split
+1;
6189 data
.password_len
= base
->userinfo_len
-base
->userinfo_split
-1;
6192 if(base
->host_start
> -1) {
6193 data
.host
= base
->canon_uri
+base
->host_start
;
6194 data
.host_len
= base
->host_len
;
6195 data
.host_type
= base
->host_type
;
6198 if(base
->has_port
) {
6199 data
.has_port
= TRUE
;
6200 data
.port_value
= base
->port
;
6202 } else if(base
->scheme_type
!= URL_SCHEME_FILE
)
6203 data
.is_opaque
= TRUE
;
6205 if(relative
->path_start
== -1 || !relative
->path_len
) {
6206 if(base
->path_start
> -1) {
6207 data
.path
= base
->canon_uri
+base
->path_start
;
6208 data
.path_len
= base
->path_len
;
6209 } else if((base
->path_start
== -1 || !base
->path_len
) && !data
.is_opaque
) {
6210 /* Just set the path as a '/' if the base didn't have
6211 * one and if it's an hierarchical URI.
6213 static const WCHAR slashW
[] = {'/',0};
6218 if(relative
->query_start
> -1) {
6219 data
.query
= relative
->canon_uri
+relative
->query_start
;
6220 data
.query_len
= relative
->query_len
;
6221 } else if(base
->query_start
> -1) {
6222 data
.query
= base
->canon_uri
+base
->query_start
;
6223 data
.query_len
= base
->query_len
;
6226 const WCHAR
*ptr
, **pptr
;
6227 DWORD path_offset
= 0, path_len
= 0;
6229 /* There's two possibilities on what will happen to the path component
6230 * of the result IUri. First, if the relative path begins with a '/'
6231 * then the resulting path will just be the relative path. Second, if
6232 * relative path doesn't begin with a '/' then the base path and relative
6233 * path are merged together.
6235 if(relative
->path_len
&& *(relative
->canon_uri
+relative
->path_start
) == '/') {
6237 BOOL copy_drive_path
= FALSE
;
6239 /* If the relative IUri's path starts with a '/', then we
6240 * don't use the base IUri's path. Unless the base IUri
6241 * is a file URI, in which case it uses the drive path of
6242 * the base IUri (if it has any) in the new path.
6244 if(base
->scheme_type
== URL_SCHEME_FILE
) {
6245 if(base
->path_len
> 3 && *(base
->canon_uri
+base
->path_start
) == '/' &&
6246 is_drive_path(base
->canon_uri
+base
->path_start
+1)) {
6248 copy_drive_path
= TRUE
;
6252 path_len
+= relative
->path_len
;
6254 path
= heap_alloc((path_len
+1)*sizeof(WCHAR
));
6257 return E_OUTOFMEMORY
;
6262 /* Copy the base paths, drive path over. */
6263 if(copy_drive_path
) {
6264 memcpy(tmp
, base
->canon_uri
+base
->path_start
, 3*sizeof(WCHAR
));
6268 memcpy(tmp
, relative
->canon_uri
+relative
->path_start
, relative
->path_len
*sizeof(WCHAR
));
6269 path
[path_len
] = '\0';
6271 /* Merge the base path with the relative path. */
6272 hr
= merge_paths(&data
, base
->canon_uri
+base
->path_start
, base
->path_len
,
6273 relative
->canon_uri
+relative
->path_start
, relative
->path_len
,
6274 &path
, &path_len
, flags
);
6280 /* If the resulting IUri is a file URI, the drive path isn't
6281 * reduced out when the dot segments are removed.
6283 if(path_len
>= 3 && data
.scheme_type
== URL_SCHEME_FILE
&& !data
.host
) {
6284 if(*path
== '/' && is_drive_path(path
+1))
6286 else if(is_drive_path(path
))
6291 /* Check if the dot segments need to be removed from the path. */
6292 if(!(flags
& URL_DONT_SIMPLIFY
) && !data
.is_opaque
) {
6293 DWORD offset
= (path_offset
> 0) ? path_offset
+1 : 0;
6294 DWORD new_len
= remove_dot_segments(path
+offset
,path_len
-offset
);
6296 if(new_len
!= path_len
) {
6297 WCHAR
*tmp
= heap_realloc(path
, (offset
+new_len
+1)*sizeof(WCHAR
));
6301 return E_OUTOFMEMORY
;
6304 tmp
[new_len
+offset
] = '\0';
6306 path_len
= new_len
+offset
;
6310 if(relative
->query_start
> -1) {
6311 data
.query
= relative
->canon_uri
+relative
->query_start
;
6312 data
.query_len
= relative
->query_len
;
6315 /* Make sure the path component is valid. */
6318 if((data
.is_opaque
&& !parse_path_opaque(pptr
, &data
, 0)) ||
6319 (!data
.is_opaque
&& !parse_path_hierarchical(pptr
, &data
, 0))) {
6322 return E_INVALIDARG
;
6326 if(relative
->fragment_start
> -1) {
6327 data
.fragment
= relative
->canon_uri
+relative
->fragment_start
;
6328 data
.fragment_len
= relative
->fragment_len
;
6331 if(flags
& URL_DONT_SIMPLIFY
)
6332 raw_flags
|= RAW_URI_FORCE_PORT_DISP
;
6333 if(flags
& URL_FILE_USE_PATHURL
)
6334 raw_flags
|= RAW_URI_CONVERT_TO_DOS_PATH
;
6336 len
= generate_raw_uri(&data
, data
.uri
, raw_flags
);
6337 data
.uri
= SysAllocStringLen(NULL
, len
);
6341 return E_OUTOFMEMORY
;
6344 generate_raw_uri(&data
, data
.uri
, raw_flags
);
6346 hr
= Uri_Construct(NULL
, (void**)&ret
);
6348 SysFreeString(data
.uri
);
6354 if(flags
& URL_DONT_SIMPLIFY
)
6355 create_flags
|= Uri_CREATE_NO_CANONICALIZE
;
6356 if(flags
& URL_FILE_USE_PATHURL
)
6357 create_flags
|= Uri_CREATE_FILE_USE_DOS_PATH
;
6359 ret
->raw_uri
= data
.uri
;
6360 hr
= canonicalize_uri(&data
, ret
, create_flags
);
6362 IUri_Release(&ret
->IUri_iface
);
6367 if(flags
& URL_DONT_SIMPLIFY
)
6368 ret
->display_modifiers
|= URI_DISPLAY_NO_DEFAULT_PORT_AUTH
;
6370 apply_default_flags(&create_flags
);
6371 ret
->create_flags
= create_flags
;
6372 *result
= &ret
->IUri_iface
;
6380 /***********************************************************************
6381 * CoInternetCombineIUri (urlmon.@)
6383 HRESULT WINAPI
CoInternetCombineIUri(IUri
*pBaseUri
, IUri
*pRelativeUri
, DWORD dwCombineFlags
,
6384 IUri
**ppCombinedUri
, DWORD_PTR dwReserved
)
6387 IInternetProtocolInfo
*info
;
6388 Uri
*relative
, *base
;
6389 TRACE("(%p %p %x %p %x)\n", pBaseUri
, pRelativeUri
, dwCombineFlags
, ppCombinedUri
, (DWORD
)dwReserved
);
6392 return E_INVALIDARG
;
6394 if(!pBaseUri
|| !pRelativeUri
) {
6395 *ppCombinedUri
= NULL
;
6396 return E_INVALIDARG
;
6399 relative
= get_uri_obj(pRelativeUri
);
6400 base
= get_uri_obj(pBaseUri
);
6401 if(!relative
|| !base
) {
6402 *ppCombinedUri
= NULL
;
6403 FIXME("(%p %p %x %p %x) Unknown IUri types not supported yet.\n",
6404 pBaseUri
, pRelativeUri
, dwCombineFlags
, ppCombinedUri
, (DWORD
)dwReserved
);
6408 info
= get_protocol_info(base
->canon_uri
);
6410 WCHAR result
[INTERNET_MAX_URL_LENGTH
+1];
6411 DWORD result_len
= 0;
6413 hr
= IInternetProtocolInfo_CombineUrl(info
, base
->canon_uri
, relative
->canon_uri
, dwCombineFlags
,
6414 result
, INTERNET_MAX_URL_LENGTH
+1, &result_len
, 0);
6415 IInternetProtocolInfo_Release(info
);
6417 hr
= CreateUri(result
, Uri_CREATE_ALLOW_RELATIVE
, 0, ppCombinedUri
);
6423 return combine_uri(base
, relative
, dwCombineFlags
, ppCombinedUri
, 0);
6426 /***********************************************************************
6427 * CoInternetCombineUrlEx (urlmon.@)
6429 HRESULT WINAPI
CoInternetCombineUrlEx(IUri
*pBaseUri
, LPCWSTR pwzRelativeUrl
, DWORD dwCombineFlags
,
6430 IUri
**ppCombinedUri
, DWORD_PTR dwReserved
)
6435 IInternetProtocolInfo
*info
;
6437 TRACE("(%p %s %x %p %x) stub\n", pBaseUri
, debugstr_w(pwzRelativeUrl
), dwCombineFlags
,
6438 ppCombinedUri
, (DWORD
)dwReserved
);
6443 if(!pwzRelativeUrl
) {
6444 *ppCombinedUri
= NULL
;
6445 return E_UNEXPECTED
;
6449 *ppCombinedUri
= NULL
;
6450 return E_INVALIDARG
;
6453 base
= get_uri_obj(pBaseUri
);
6455 *ppCombinedUri
= NULL
;
6456 FIXME("(%p %s %x %p %x) Unknown IUri's not supported yet.\n", pBaseUri
, debugstr_w(pwzRelativeUrl
),
6457 dwCombineFlags
, ppCombinedUri
, (DWORD
)dwReserved
);
6461 info
= get_protocol_info(base
->canon_uri
);
6463 WCHAR result
[INTERNET_MAX_URL_LENGTH
+1];
6464 DWORD result_len
= 0;
6466 hr
= IInternetProtocolInfo_CombineUrl(info
, base
->canon_uri
, pwzRelativeUrl
, dwCombineFlags
,
6467 result
, INTERNET_MAX_URL_LENGTH
+1, &result_len
, 0);
6468 IInternetProtocolInfo_Release(info
);
6470 hr
= CreateUri(result
, Uri_CREATE_ALLOW_RELATIVE
, 0, ppCombinedUri
);
6476 hr
= CreateUri(pwzRelativeUrl
, Uri_CREATE_ALLOW_RELATIVE
, 0, &relative
);
6478 *ppCombinedUri
= NULL
;
6482 hr
= combine_uri(base
, get_uri_obj(relative
), dwCombineFlags
, ppCombinedUri
, COMBINE_URI_FORCE_FLAG_USE
);
6484 IUri_Release(relative
);
6488 static HRESULT
parse_canonicalize(const Uri
*uri
, DWORD flags
, LPWSTR output
,
6489 DWORD output_len
, DWORD
*result_len
)
6491 const WCHAR
*ptr
= NULL
;
6494 WCHAR buffer
[INTERNET_MAX_URL_LENGTH
+1];
6498 /* URL_UNESCAPE only has effect if none of the URL_ESCAPE flags are set. */
6499 const BOOL allow_unescape
= !(flags
& URL_ESCAPE_UNSAFE
) &&
6500 !(flags
& URL_ESCAPE_SPACES_ONLY
) &&
6501 !(flags
& URL_ESCAPE_PERCENT
);
6504 /* Check if the dot segments need to be removed from the
6507 if(uri
->scheme_start
> -1 && uri
->path_start
> -1) {
6508 ptr
= uri
->canon_uri
+uri
->scheme_start
+uri
->scheme_len
+1;
6511 reduce_path
= !(flags
& URL_NO_META
) &&
6512 !(flags
& URL_DONT_SIMPLIFY
) &&
6513 ptr
&& check_hierarchical(pptr
);
6515 for(ptr
= uri
->canon_uri
; ptr
< uri
->canon_uri
+uri
->canon_len
; ++ptr
) {
6516 BOOL do_default_action
= TRUE
;
6518 /* Keep track of the path if we need to remove dot segments from
6521 if(reduce_path
&& !path
&& ptr
== uri
->canon_uri
+uri
->path_start
)
6524 /* Check if it's time to reduce the path. */
6525 if(reduce_path
&& ptr
== uri
->canon_uri
+uri
->path_start
+uri
->path_len
) {
6526 DWORD current_path_len
= (buffer
+len
) - path
;
6527 DWORD new_path_len
= remove_dot_segments(path
, current_path_len
);
6529 /* Update the current length. */
6530 len
-= (current_path_len
-new_path_len
);
6531 reduce_path
= FALSE
;
6535 const WCHAR decoded
= decode_pct_val(ptr
);
6537 if(allow_unescape
&& (flags
& URL_UNESCAPE
)) {
6538 buffer
[len
++] = decoded
;
6540 do_default_action
= FALSE
;
6544 /* See if %'s needed to encoded. */
6545 if(do_default_action
&& (flags
& URL_ESCAPE_PERCENT
)) {
6546 pct_encode_val(*ptr
, buffer
+len
);
6548 do_default_action
= FALSE
;
6550 } else if(*ptr
== ' ') {
6551 if((flags
& URL_ESCAPE_SPACES_ONLY
) &&
6552 !(flags
& URL_ESCAPE_UNSAFE
)) {
6553 pct_encode_val(*ptr
, buffer
+len
);
6555 do_default_action
= FALSE
;
6557 } else if(!is_reserved(*ptr
) && !is_unreserved(*ptr
)) {
6558 if(flags
& URL_ESCAPE_UNSAFE
) {
6559 pct_encode_val(*ptr
, buffer
+len
);
6561 do_default_action
= FALSE
;
6565 if(do_default_action
)
6566 buffer
[len
++] = *ptr
;
6569 /* Sometimes the path is the very last component of the IUri, so
6570 * see if the dot segments need to be reduced now.
6572 if(reduce_path
&& path
) {
6573 DWORD current_path_len
= (buffer
+len
) - path
;
6574 DWORD new_path_len
= remove_dot_segments(path
, current_path_len
);
6576 /* Update the current length. */
6577 len
-= (current_path_len
-new_path_len
);
6582 /* The null terminator isn't included in the length. */
6583 *result_len
= len
-1;
6584 if(len
> output_len
)
6585 return STRSAFE_E_INSUFFICIENT_BUFFER
;
6587 memcpy(output
, buffer
, len
*sizeof(WCHAR
));
6592 static HRESULT
parse_friendly(IUri
*uri
, LPWSTR output
, DWORD output_len
,
6599 hr
= IUri_GetPropertyLength(uri
, Uri_PROPERTY_DISPLAY_URI
, &display_len
, 0);
6605 *result_len
= display_len
;
6606 if(display_len
+1 > output_len
)
6607 return STRSAFE_E_INSUFFICIENT_BUFFER
;
6609 hr
= IUri_GetDisplayUri(uri
, &display
);
6615 memcpy(output
, display
, (display_len
+1)*sizeof(WCHAR
));
6616 SysFreeString(display
);
6620 static HRESULT
parse_rootdocument(const Uri
*uri
, LPWSTR output
, DWORD output_len
,
6623 static const WCHAR colon_slashesW
[] = {':','/','/'};
6628 /* Windows only returns the root document if the URI has an authority
6629 * and it's not an unknown scheme type or a file scheme type.
6631 if(uri
->authority_start
== -1 ||
6632 uri
->scheme_type
== URL_SCHEME_UNKNOWN
||
6633 uri
->scheme_type
== URL_SCHEME_FILE
) {
6636 return STRSAFE_E_INSUFFICIENT_BUFFER
;
6642 len
= uri
->scheme_len
+uri
->authority_len
;
6643 /* For the "://" and '/' which will be added. */
6646 if(len
+1 > output_len
) {
6648 return STRSAFE_E_INSUFFICIENT_BUFFER
;
6652 memcpy(ptr
, uri
->canon_uri
+uri
->scheme_start
, uri
->scheme_len
*sizeof(WCHAR
));
6654 /* Add the "://". */
6655 ptr
+= uri
->scheme_len
;
6656 memcpy(ptr
, colon_slashesW
, sizeof(colon_slashesW
));
6658 /* Add the authority. */
6659 ptr
+= sizeof(colon_slashesW
)/sizeof(WCHAR
);
6660 memcpy(ptr
, uri
->canon_uri
+uri
->authority_start
, uri
->authority_len
*sizeof(WCHAR
));
6662 /* Add the '/' after the authority. */
6663 ptr
+= uri
->authority_len
;
6671 static HRESULT
parse_document(const Uri
*uri
, LPWSTR output
, DWORD output_len
,
6676 /* It has to be a known scheme type, but, it can't be a file
6677 * scheme. It also has to hierarchical.
6679 if(uri
->scheme_type
== URL_SCHEME_UNKNOWN
||
6680 uri
->scheme_type
== URL_SCHEME_FILE
||
6681 uri
->authority_start
== -1) {
6684 return STRSAFE_E_INSUFFICIENT_BUFFER
;
6690 if(uri
->fragment_start
> -1)
6691 len
= uri
->fragment_start
;
6693 len
= uri
->canon_len
;
6696 if(len
+1 > output_len
)
6697 return STRSAFE_E_INSUFFICIENT_BUFFER
;
6699 memcpy(output
, uri
->canon_uri
, len
*sizeof(WCHAR
));
6704 static HRESULT
parse_path_from_url(const Uri
*uri
, LPWSTR output
, DWORD output_len
,
6707 const WCHAR
*path_ptr
;
6708 WCHAR buffer
[INTERNET_MAX_URL_LENGTH
+1];
6711 if(uri
->scheme_type
!= URL_SCHEME_FILE
) {
6715 return E_INVALIDARG
;
6719 if(uri
->host_start
> -1) {
6720 static const WCHAR slash_slashW
[] = {'\\','\\'};
6722 memcpy(ptr
, slash_slashW
, sizeof(slash_slashW
));
6723 ptr
+= sizeof(slash_slashW
)/sizeof(WCHAR
);
6724 memcpy(ptr
, uri
->canon_uri
+uri
->host_start
, uri
->host_len
*sizeof(WCHAR
));
6725 ptr
+= uri
->host_len
;
6728 path_ptr
= uri
->canon_uri
+uri
->path_start
;
6729 if(uri
->path_len
> 3 && *path_ptr
== '/' && is_drive_path(path_ptr
+1))
6730 /* Skip past the '/' in front of the drive path. */
6733 for(; path_ptr
< uri
->canon_uri
+uri
->path_start
+uri
->path_len
; ++path_ptr
, ++ptr
) {
6734 BOOL do_default_action
= TRUE
;
6736 if(*path_ptr
== '%') {
6737 const WCHAR decoded
= decode_pct_val(path_ptr
);
6741 do_default_action
= FALSE
;
6743 } else if(*path_ptr
== '/') {
6745 do_default_action
= FALSE
;
6748 if(do_default_action
)
6754 *result_len
= ptr
-buffer
;
6755 if(*result_len
+1 > output_len
)
6756 return STRSAFE_E_INSUFFICIENT_BUFFER
;
6758 memcpy(output
, buffer
, (*result_len
+1)*sizeof(WCHAR
));
6762 static HRESULT
parse_url_from_path(IUri
*uri
, LPWSTR output
, DWORD output_len
,
6769 hr
= IUri_GetPropertyLength(uri
, Uri_PROPERTY_ABSOLUTE_URI
, &len
, 0);
6776 if(len
+1 > output_len
)
6777 return STRSAFE_E_INSUFFICIENT_BUFFER
;
6779 hr
= IUri_GetAbsoluteUri(uri
, &received
);
6785 memcpy(output
, received
, (len
+1)*sizeof(WCHAR
));
6786 SysFreeString(received
);
6791 static HRESULT
parse_schema(IUri
*uri
, LPWSTR output
, DWORD output_len
,
6798 hr
= IUri_GetPropertyLength(uri
, Uri_PROPERTY_SCHEME_NAME
, &len
, 0);
6805 if(len
+1 > output_len
)
6806 return STRSAFE_E_INSUFFICIENT_BUFFER
;
6808 hr
= IUri_GetSchemeName(uri
, &received
);
6814 memcpy(output
, received
, (len
+1)*sizeof(WCHAR
));
6815 SysFreeString(received
);
6820 static HRESULT
parse_site(IUri
*uri
, LPWSTR output
, DWORD output_len
, DWORD
*result_len
)
6826 hr
= IUri_GetPropertyLength(uri
, Uri_PROPERTY_HOST
, &len
, 0);
6833 if(len
+1 > output_len
)
6834 return STRSAFE_E_INSUFFICIENT_BUFFER
;
6836 hr
= IUri_GetHost(uri
, &received
);
6842 memcpy(output
, received
, (len
+1)*sizeof(WCHAR
));
6843 SysFreeString(received
);
6848 static HRESULT
parse_domain(IUri
*uri
, LPWSTR output
, DWORD output_len
, DWORD
*result_len
)
6854 hr
= IUri_GetPropertyLength(uri
, Uri_PROPERTY_DOMAIN
, &len
, 0);
6861 if(len
+1 > output_len
)
6862 return STRSAFE_E_INSUFFICIENT_BUFFER
;
6864 hr
= IUri_GetDomain(uri
, &received
);
6870 memcpy(output
, received
, (len
+1)*sizeof(WCHAR
));
6871 SysFreeString(received
);
6876 static HRESULT
parse_anchor(IUri
*uri
, LPWSTR output
, DWORD output_len
, DWORD
*result_len
)
6882 hr
= IUri_GetPropertyLength(uri
, Uri_PROPERTY_FRAGMENT
, &len
, 0);
6889 if(len
+1 > output_len
)
6890 return STRSAFE_E_INSUFFICIENT_BUFFER
;
6892 hr
= IUri_GetFragment(uri
, &received
);
6898 memcpy(output
, received
, (len
+1)*sizeof(WCHAR
));
6899 SysFreeString(received
);
6904 /***********************************************************************
6905 * CoInternetParseIUri (urlmon.@)
6907 HRESULT WINAPI
CoInternetParseIUri(IUri
*pIUri
, PARSEACTION ParseAction
, DWORD dwFlags
,
6908 LPWSTR pwzResult
, DWORD cchResult
, DWORD
*pcchResult
,
6909 DWORD_PTR dwReserved
)
6913 IInternetProtocolInfo
*info
;
6915 TRACE("(%p %d %x %p %d %p %x)\n", pIUri
, ParseAction
, dwFlags
, pwzResult
,
6916 cchResult
, pcchResult
, (DWORD
)dwReserved
);
6921 if(!pwzResult
|| !pIUri
) {
6923 return E_INVALIDARG
;
6926 if(!(uri
= get_uri_obj(pIUri
))) {
6928 FIXME("(%p %d %x %p %d %p %x) Unknown IUri's not supported for this action.\n",
6929 pIUri
, ParseAction
, dwFlags
, pwzResult
, cchResult
, pcchResult
, (DWORD
)dwReserved
);
6933 info
= get_protocol_info(uri
->canon_uri
);
6935 hr
= IInternetProtocolInfo_ParseUrl(info
, uri
->canon_uri
, ParseAction
, dwFlags
,
6936 pwzResult
, cchResult
, pcchResult
, 0);
6937 IInternetProtocolInfo_Release(info
);
6938 if(SUCCEEDED(hr
)) return hr
;
6941 switch(ParseAction
) {
6942 case PARSE_CANONICALIZE
:
6943 hr
= parse_canonicalize(uri
, dwFlags
, pwzResult
, cchResult
, pcchResult
);
6945 case PARSE_FRIENDLY
:
6946 hr
= parse_friendly(pIUri
, pwzResult
, cchResult
, pcchResult
);
6948 case PARSE_ROOTDOCUMENT
:
6949 hr
= parse_rootdocument(uri
, pwzResult
, cchResult
, pcchResult
);
6951 case PARSE_DOCUMENT
:
6952 hr
= parse_document(uri
, pwzResult
, cchResult
, pcchResult
);
6954 case PARSE_PATH_FROM_URL
:
6955 hr
= parse_path_from_url(uri
, pwzResult
, cchResult
, pcchResult
);
6957 case PARSE_URL_FROM_PATH
:
6958 hr
= parse_url_from_path(pIUri
, pwzResult
, cchResult
, pcchResult
);
6961 hr
= parse_schema(pIUri
, pwzResult
, cchResult
, pcchResult
);
6964 hr
= parse_site(pIUri
, pwzResult
, cchResult
, pcchResult
);
6967 hr
= parse_domain(pIUri
, pwzResult
, cchResult
, pcchResult
);
6969 case PARSE_LOCATION
:
6971 hr
= parse_anchor(pIUri
, pwzResult
, cchResult
, pcchResult
);
6973 case PARSE_SECURITY_URL
:
6976 case PARSE_SECURITY_DOMAIN
:
6983 FIXME("(%p %d %x %p %d %p %x) Partial stub.\n", pIUri
, ParseAction
, dwFlags
,
6984 pwzResult
, cchResult
, pcchResult
, (DWORD
)dwReserved
);