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 #include "urlmon_main.h"
21 #include "wine/debug.h"
23 #define NO_SHLWAPI_REG
26 #define UINT_MAX 0xffffffff
27 #define USHORT_MAX 0xffff
29 WINE_DEFAULT_DEBUG_CHANNEL(urlmon
);
32 const IUriVtbl
*lpIUriVtbl
;
37 /* Information about the canonicalized URI's buffer. */
44 URL_SCHEME scheme_type
;
52 Uri_HOST_TYPE host_type
;
71 const IUriBuilderVtbl
*lpIUriBuilderVtbl
;
81 /* IPv6 addresses can hold up to 8 h16 components. */
85 /* An IPv6 can have 1 elision ("::"). */
88 /* An IPv6 can contain 1 IPv4 address as the last 32bits of the address. */
101 BOOL has_implicit_scheme
;
102 BOOL has_implicit_ip
;
107 URL_SCHEME scheme_type
;
109 const WCHAR
*userinfo
;
115 Uri_HOST_TYPE host_type
;
118 ipv6_address ipv6_address
;
130 const WCHAR
*fragment
;
134 static const CHAR hexDigits
[] = "0123456789ABCDEF";
136 /* List of scheme types/scheme names that are recognized by the IUri interface as of IE 7. */
137 static const struct {
139 WCHAR scheme_name
[16];
140 } recognized_schemes
[] = {
141 {URL_SCHEME_FTP
, {'f','t','p',0}},
142 {URL_SCHEME_HTTP
, {'h','t','t','p',0}},
143 {URL_SCHEME_GOPHER
, {'g','o','p','h','e','r',0}},
144 {URL_SCHEME_MAILTO
, {'m','a','i','l','t','o',0}},
145 {URL_SCHEME_NEWS
, {'n','e','w','s',0}},
146 {URL_SCHEME_NNTP
, {'n','n','t','p',0}},
147 {URL_SCHEME_TELNET
, {'t','e','l','n','e','t',0}},
148 {URL_SCHEME_WAIS
, {'w','a','i','s',0}},
149 {URL_SCHEME_FILE
, {'f','i','l','e',0}},
150 {URL_SCHEME_MK
, {'m','k',0}},
151 {URL_SCHEME_HTTPS
, {'h','t','t','p','s',0}},
152 {URL_SCHEME_SHELL
, {'s','h','e','l','l',0}},
153 {URL_SCHEME_SNEWS
, {'s','n','e','w','s',0}},
154 {URL_SCHEME_LOCAL
, {'l','o','c','a','l',0}},
155 {URL_SCHEME_JAVASCRIPT
, {'j','a','v','a','s','c','r','i','p','t',0}},
156 {URL_SCHEME_VBSCRIPT
, {'v','b','s','c','r','i','p','t',0}},
157 {URL_SCHEME_ABOUT
, {'a','b','o','u','t',0}},
158 {URL_SCHEME_RES
, {'r','e','s',0}},
159 {URL_SCHEME_MSSHELLROOTED
, {'m','s','-','s','h','e','l','l','-','r','o','o','t','e','d',0}},
160 {URL_SCHEME_MSSHELLIDLIST
, {'m','s','-','s','h','e','l','l','-','i','d','l','i','s','t',0}},
161 {URL_SCHEME_MSHELP
, {'h','c','p',0}},
162 {URL_SCHEME_WILDCARD
, {'*',0}}
165 /* List of default ports Windows recognizes. */
166 static const struct {
169 } default_ports
[] = {
170 {URL_SCHEME_FTP
, 21},
171 {URL_SCHEME_HTTP
, 80},
172 {URL_SCHEME_GOPHER
, 70},
173 {URL_SCHEME_NNTP
, 119},
174 {URL_SCHEME_TELNET
, 23},
175 {URL_SCHEME_WAIS
, 210},
176 {URL_SCHEME_HTTPS
, 443},
179 /* List of 3 character top level domain names Windows seems to recognize.
180 * There might be more, but, these are the only ones I've found so far.
182 static const struct {
184 } recognized_tlds
[] = {
194 static inline BOOL
is_alpha(WCHAR val
) {
195 return ((val
>= 'a' && val
<= 'z') || (val
>= 'A' && val
<= 'Z'));
198 static inline BOOL
is_num(WCHAR val
) {
199 return (val
>= '0' && val
<= '9');
202 /* A URI is implicitly a file path if it begins with
203 * a drive letter (eg X:) or starts with "\\" (UNC path).
205 static inline BOOL
is_implicit_file_path(const WCHAR
*str
) {
206 if(is_alpha(str
[0]) && str
[1] == ':')
208 else if(str
[0] == '\\' && str
[1] == '\\')
214 /* Checks if the URI is a hierarchical URI. A hierarchical
215 * URI is one that has "//" after the scheme.
217 static BOOL
check_hierarchical(const WCHAR
**ptr
) {
218 const WCHAR
*start
= *ptr
;
233 /* unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~" */
234 static inline BOOL
is_unreserved(WCHAR val
) {
235 return (is_alpha(val
) || is_num(val
) || val
== '-' || val
== '.' ||
236 val
== '_' || val
== '~');
239 /* sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
240 * / "*" / "+" / "," / ";" / "="
242 static inline BOOL
is_subdelim(WCHAR val
) {
243 return (val
== '!' || val
== '$' || val
== '&' ||
244 val
== '\'' || val
== '(' || val
== ')' ||
245 val
== '*' || val
== '+' || val
== ',' ||
246 val
== ';' || val
== '=');
249 /* gen-delims = ":" / "/" / "?" / "#" / "[" / "]" / "@" */
250 static inline BOOL
is_gendelim(WCHAR val
) {
251 return (val
== ':' || val
== '/' || val
== '?' ||
252 val
== '#' || val
== '[' || val
== ']' ||
256 /* Characters that delimit the end of the authority
257 * section of a URI. Sometimes a '\\' is considered
258 * an authority delimeter.
260 static inline BOOL
is_auth_delim(WCHAR val
, BOOL acceptSlash
) {
261 return (val
== '#' || val
== '/' || val
== '?' ||
262 val
== '\0' || (acceptSlash
&& val
== '\\'));
265 /* reserved = gen-delims / sub-delims */
266 static inline BOOL
is_reserved(WCHAR val
) {
267 return (is_subdelim(val
) || is_gendelim(val
));
270 static inline BOOL
is_hexdigit(WCHAR val
) {
271 return ((val
>= 'a' && val
<= 'f') ||
272 (val
>= 'A' && val
<= 'F') ||
273 (val
>= '0' && val
<= '9'));
276 static inline BOOL
is_path_delim(WCHAR val
) {
277 return (!val
|| val
== '#' || val
== '?');
280 /* Computes the size of the given IPv6 address.
281 * Each h16 component is 16bits, if there is an IPv4 address, it's
282 * 32bits. If there's an elision it can be 16bits to 128bits, depending
283 * on the number of other components.
285 * Modeled after google-url's CheckIPv6ComponentsSize function
287 static void compute_ipv6_comps_size(ipv6_address
*address
) {
288 address
->components_size
= address
->h16_count
* 2;
291 /* IPv4 address is 4 bytes. */
292 address
->components_size
+= 4;
294 if(address
->elision
) {
295 /* An elision can be anywhere from 2 bytes up to 16 bytes.
296 * It size depends on the size of the h16 and IPv4 components.
298 address
->elision_size
= 16 - address
->components_size
;
299 if(address
->elision_size
< 2)
300 address
->elision_size
= 2;
302 address
->elision_size
= 0;
305 /* Taken from dlls/jscript/lex.c */
306 static int hex_to_int(WCHAR val
) {
307 if(val
>= '0' && val
<= '9')
309 else if(val
>= 'a' && val
<= 'f')
310 return val
- 'a' + 10;
311 else if(val
>= 'A' && val
<= 'F')
312 return val
- 'A' + 10;
317 /* Helper function for converting a percent encoded string
318 * representation of a WCHAR value into its actual WCHAR value. If
319 * the two characters following the '%' aren't valid hex values then
320 * this function returns the NULL character.
323 * "%2E" will result in '.' being returned by this function.
325 static WCHAR
decode_pct_val(const WCHAR
*ptr
) {
328 if(*ptr
== '%' && is_hexdigit(*(ptr
+ 1)) && is_hexdigit(*(ptr
+ 2))) {
329 INT a
= hex_to_int(*(ptr
+ 1));
330 INT b
= hex_to_int(*(ptr
+ 2));
339 /* Helper function for percent encoding a given character
340 * and storing the encoded value into a given buffer (dest).
342 * It's up to the calling function to ensure that there is
343 * at least enough space in 'dest' for the percent encoded
344 * value to be stored (so dest + 3 spaces available).
346 static inline void pct_encode_val(WCHAR val
, WCHAR
*dest
) {
348 dest
[1] = hexDigits
[(val
>> 4) & 0xf];
349 dest
[2] = hexDigits
[val
& 0xf];
352 /* Scans the range of characters [str, end] and returns the last occurence
353 * of 'ch' or returns NULL.
355 static const WCHAR
*str_last_of(const WCHAR
*str
, const WCHAR
*end
, WCHAR ch
) {
356 const WCHAR
*ptr
= end
;
367 /* Attempts to parse the domain name from the host.
369 * This function also includes the Top-level Domain (TLD) name
370 * of the host when it tries to find the domain name. If it finds
371 * a valid domain name it will assign 'domain_start' the offset
372 * into 'host' where the domain name starts.
374 * It's implied that if a domain name its range is implied to be
375 * [host+domain_start, host+host_len).
377 static void find_domain_name(const WCHAR
*host
, DWORD host_len
,
379 const WCHAR
*last_tld
, *sec_last_tld
, *end
;
381 end
= host
+host_len
-1;
385 /* There has to be at least enough room for a '.' followed by a
386 * 3 character TLD for a domain to even exist in the host name.
391 last_tld
= str_last_of(host
, end
, '.');
393 /* http://hostname -> has no domain name. */
396 sec_last_tld
= str_last_of(host
, last_tld
-1, '.');
398 /* If the '.' is at the beginning of the host there
399 * has to be at least 3 characters in the TLD for it
401 * Ex: .com -> .com as the domain name.
402 * .co -> has no domain name.
404 if(last_tld
-host
== 0) {
405 if(end
-(last_tld
-1) < 3)
407 } else if(last_tld
-host
== 3) {
410 /* If there's three characters in front of last_tld and
411 * they are on the list of recognized TLDs, then this
412 * host doesn't have a domain (since the host only contains
414 * Ex: edu.uk -> has no domain name.
415 * foo.uk -> foo.uk as the domain name.
417 for(i
= 0; i
< sizeof(recognized_tlds
)/sizeof(recognized_tlds
[0]); ++i
) {
418 if(!StrCmpNIW(host
, recognized_tlds
[i
].tld_name
, 3))
421 } else if(last_tld
-host
< 3)
422 /* Anything less then 3 characters is considered part
424 * Ex: ak.uk -> Has no domain name.
428 /* Otherwise the domain name is the whole host name. */
430 } else if(end
+1-last_tld
> 3) {
431 /* If the last_tld has more then 3 characters then it's automatically
432 * considered the TLD of the domain name.
433 * Ex: www.winehq.org.uk.test -> uk.test as the domain name.
435 *domain_start
= (sec_last_tld
+1)-host
;
436 } else if(last_tld
- (sec_last_tld
+1) < 4) {
438 /* If the sec_last_tld is 3 characters long it HAS to be on the list of
439 * recognized to still be considered part of the TLD name, otherwise
440 * its considered the domain name.
441 * Ex: www.google.com.uk -> google.com.uk as the domain name.
442 * www.google.foo.uk -> foo.uk as the domain name.
444 if(last_tld
- (sec_last_tld
+1) == 3) {
445 for(i
= 0; i
< sizeof(recognized_tlds
)/sizeof(recognized_tlds
[0]); ++i
) {
446 if(!StrCmpNIW(sec_last_tld
+1, recognized_tlds
[i
].tld_name
, 3)) {
447 const WCHAR
*domain
= str_last_of(host
, sec_last_tld
-1, '.');
452 *domain_start
= (domain
+1) - host
;
453 TRACE("Found domain name %s\n", debugstr_wn(host
+*domain_start
,
454 (host
+host_len
)-(host
+*domain_start
)));
459 *domain_start
= (sec_last_tld
+1)-host
;
461 /* Since the sec_last_tld is less then 3 characters it's considered
463 * Ex: www.google.fo.uk -> google.fo.uk as the domain name.
465 const WCHAR
*domain
= str_last_of(host
, sec_last_tld
-1, '.');
470 *domain_start
= (domain
+1) - host
;
473 /* The second to last TLD has more then 3 characters making it
475 * Ex: www.google.test.us -> test.us as the domain name.
477 *domain_start
= (sec_last_tld
+1)-host
;
480 TRACE("Found domain name %s\n", debugstr_wn(host
+*domain_start
,
481 (host
+host_len
)-(host
+*domain_start
)));
484 /* Removes the dot segments from a heirarchical URIs path component. This
485 * function performs the removal in place.
487 * This is a modified version of Qt's QUrl function "removeDotsFromPath".
489 * This function returns the new length of the path string.
491 static DWORD
remove_dot_segments(WCHAR
*path
, DWORD path_len
) {
493 const WCHAR
*in
= out
;
494 const WCHAR
*end
= out
+ path_len
;
498 /* A. if the input buffer begins with a prefix of "/./" or "/.",
499 * where "." is a complete path segment, then replace that
500 * prefix with "/" in the input buffer; otherwise,
502 if(in
<= end
- 3 && in
[0] == '/' && in
[1] == '.' && in
[2] == '/') {
505 } else if(in
== end
- 2 && in
[0] == '/' && in
[1] == '.') {
511 /* B. if the input buffer begins with a prefix of "/../" or "/..",
512 * where ".." is a complete path segment, then replace that
513 * prefix with "/" in the input buffer and remove the last
514 * segment and its preceding "/" (if any) from the output
517 if(in
<= end
- 4 && in
[0] == '/' && in
[1] == '.' && in
[2] == '.' && in
[3] == '/') {
518 while(out
> path
&& *(--out
) != '/');
522 } else if(in
== end
- 3 && in
[0] == '/' && in
[1] == '.' && in
[2] == '.') {
523 while(out
> path
&& *(--out
) != '/');
532 /* C. move the first path segment in the input buffer to the end of
533 * the output buffer, including the initial "/" character (if
534 * any) and any subsequent characters up to, but not including,
535 * the next "/" character or the end of the input buffer.
538 while(in
< end
&& *in
!= '/')
543 TRACE("(%p %d): Path after dot segments removed %s len=%d\n", path
, path_len
,
544 debugstr_wn(path
, len
), len
);
548 /* Attempts to find the file extension in a given path. */
549 static INT
find_file_extension(const WCHAR
*path
, DWORD path_len
) {
552 for(end
= path
+path_len
-1; end
>= path
&& *end
!= '/' && *end
!= '\\'; --end
) {
560 /* Computes the location where the elision should occur in the IPv6
561 * address using the numerical values of each component stored in
562 * 'values'. If the address shouldn't contain an elision then 'index'
563 * is assigned -1 as it's value. Otherwise 'index' will contain the
564 * starting index (into values) where the elision should be, and 'count'
565 * will contain the number of cells the elision covers.
568 * Windows will expand an elision if the elision only represents 1 h16
569 * component of the URI.
571 * Ex: [1::2:3:4:5:6:7] -> [1:0:2:3:4:5:6:7]
573 * If the IPv6 address contains an IPv4 address, the IPv4 address is also
574 * considered for being included as part of an elision if all it's components
577 * Ex: [1:2:3:4:5:6:0.0.0.0] -> [1:2:3:4:5:6::]
579 static void compute_elision_location(const ipv6_address
*address
, const USHORT values
[8],
580 INT
*index
, DWORD
*count
) {
581 DWORD i
, max_len
, cur_len
;
582 INT max_index
, cur_index
;
584 max_len
= cur_len
= 0;
585 max_index
= cur_index
= -1;
586 for(i
= 0; i
< 8; ++i
) {
587 BOOL check_ipv4
= (address
->ipv4
&& i
== 6);
588 BOOL is_end
= (check_ipv4
|| i
== 7);
591 /* Check if the IPv4 address contains only zeros. */
592 if(values
[i
] == 0 && values
[i
+1] == 0) {
599 } else if(values
[i
] == 0) {
606 if(is_end
|| values
[i
] != 0) {
607 /* We only consider it for an elision if it's
608 * more then 1 component long.
610 if(cur_len
> 1 && cur_len
> max_len
) {
611 /* Found the new elision location. */
613 max_index
= cur_index
;
616 /* Reset the current range for the next range of zeros. */
626 /* Converts the specified IPv4 address into an uint value.
628 * This function assumes that the IPv4 address has already been validated.
630 static UINT
ipv4toui(const WCHAR
*ip
, DWORD len
) {
632 DWORD comp_value
= 0;
635 for(ptr
= ip
; ptr
< ip
+len
; ++ptr
) {
641 comp_value
= comp_value
*10 + (*ptr
-'0');
650 /* Converts an IPv4 address in numerical form into it's fully qualified
651 * string form. This function returns the number of characters written
652 * to 'dest'. If 'dest' is NULL this function will return the number of
653 * characters that would have been written.
655 * It's up to the caller to ensure there's enough space in 'dest' for the
658 static DWORD
ui2ipv4(WCHAR
*dest
, UINT address
) {
659 static const WCHAR formatW
[] =
660 {'%','u','.','%','u','.','%','u','.','%','u',0};
664 digits
[0] = (address
>> 24) & 0xff;
665 digits
[1] = (address
>> 16) & 0xff;
666 digits
[2] = (address
>> 8) & 0xff;
667 digits
[3] = address
& 0xff;
671 ret
= sprintfW(tmp
, formatW
, digits
[0], digits
[1], digits
[2], digits
[3]);
673 ret
= sprintfW(dest
, formatW
, digits
[0], digits
[1], digits
[2], digits
[3]);
678 /* Converts an h16 component (from an IPv6 address) into it's
681 * This function assumes that the h16 component has already been validated.
683 static USHORT
h16tous(h16 component
) {
687 for(i
= 0; i
< component
.len
; ++i
) {
689 ret
+= hex_to_int(component
.str
[i
]);
695 /* Converts an IPv6 address into it's 128 bits (16 bytes) numerical value.
697 * This function assumes that the ipv6_address has already been validated.
699 static BOOL
ipv6_to_number(const ipv6_address
*address
, USHORT number
[8]) {
700 DWORD i
, cur_component
= 0;
701 BOOL already_passed_elision
= FALSE
;
703 for(i
= 0; i
< address
->h16_count
; ++i
) {
704 if(address
->elision
) {
705 if(address
->components
[i
].str
> address
->elision
&& !already_passed_elision
) {
706 /* Means we just passed the elision and need to add it's values to
707 * 'number' before we do anything else.
710 for(j
= 0; j
< address
->elision_size
; j
+=2)
711 number
[cur_component
++] = 0;
713 already_passed_elision
= TRUE
;
717 number
[cur_component
++] = h16tous(address
->components
[i
]);
720 /* Case when the elision appears after the h16 components. */
721 if(!already_passed_elision
&& address
->elision
) {
722 for(i
= 0; i
< address
->elision_size
; i
+=2)
723 number
[cur_component
++] = 0;
724 already_passed_elision
= TRUE
;
728 UINT value
= ipv4toui(address
->ipv4
, address
->ipv4_len
);
730 if(cur_component
!= 6) {
731 ERR("(%p %p): Failed sanity check with %d\n", address
, number
, cur_component
);
735 number
[cur_component
++] = (value
>> 16) & 0xffff;
736 number
[cur_component
] = value
& 0xffff;
742 /* Checks if the characters pointed to by 'ptr' are
743 * a percent encoded data octet.
745 * pct-encoded = "%" HEXDIG HEXDIG
747 static BOOL
check_pct_encoded(const WCHAR
**ptr
) {
748 const WCHAR
*start
= *ptr
;
754 if(!is_hexdigit(**ptr
)) {
760 if(!is_hexdigit(**ptr
)) {
769 /* dec-octet = DIGIT ; 0-9
770 * / %x31-39 DIGIT ; 10-99
771 * / "1" 2DIGIT ; 100-199
772 * / "2" %x30-34 DIGIT ; 200-249
773 * / "25" %x30-35 ; 250-255
775 static BOOL
check_dec_octet(const WCHAR
**ptr
) {
776 const WCHAR
*c1
, *c2
, *c3
;
779 /* A dec-octet must be at least 1 digit long. */
780 if(*c1
< '0' || *c1
> '9')
786 /* Since the 1 digit requirment was meet, it doesn't
787 * matter if this is a DIGIT value, it's considered a
790 if(*c2
< '0' || *c2
> '9')
796 /* Same explanation as above. */
797 if(*c3
< '0' || *c3
> '9')
800 /* Anything > 255 isn't a valid IP dec-octet. */
801 if(*c1
>= '2' && *c2
>= '5' && *c3
>= '5') {
810 /* Checks if there is an implicit IPv4 address in the host component of the URI.
811 * The max value of an implicit IPv4 address is UINT_MAX.
814 * "234567" would be considered an implicit IPv4 address.
816 static BOOL
check_implicit_ipv4(const WCHAR
**ptr
, UINT
*val
) {
817 const WCHAR
*start
= *ptr
;
821 while(is_num(**ptr
)) {
822 ret
= ret
*10 + (**ptr
- '0');
838 /* Checks if the string contains an IPv4 address.
840 * This function has a strict mode or a non-strict mode of operation
841 * When 'strict' is set to FALSE this function will return TRUE if
842 * the string contains at least 'dec-octet "." dec-octet' since partial
843 * IPv4 addresses will be normalized out into full IPv4 addresses. When
844 * 'strict' is set this function expects there to be a full IPv4 address.
846 * IPv4address = dec-octet "." dec-octet "." dec-octet "." dec-octet
848 static BOOL
check_ipv4address(const WCHAR
**ptr
, BOOL strict
) {
849 const WCHAR
*start
= *ptr
;
851 if(!check_dec_octet(ptr
)) {
862 if(!check_dec_octet(ptr
)) {
876 if(!check_dec_octet(ptr
)) {
890 if(!check_dec_octet(ptr
)) {
895 /* Found a four digit ip address. */
898 /* Tries to parse the scheme name of the URI.
900 * scheme = ALPHA *(ALPHA | NUM | '+' | '-' | '.') as defined by RFC 3896.
901 * NOTE: Windows accepts a number as the first character of a scheme.
903 static BOOL
parse_scheme_name(const WCHAR
**ptr
, parse_data
*data
) {
904 const WCHAR
*start
= *ptr
;
907 data
->scheme_len
= 0;
910 if(**ptr
== '*' && *ptr
== start
) {
911 /* Might have found a wildcard scheme. If it is the next
912 * char has to be a ':' for it to be a valid URI
916 } else if(!is_num(**ptr
) && !is_alpha(**ptr
) && **ptr
!= '+' &&
917 **ptr
!= '-' && **ptr
!= '.')
926 /* Schemes must end with a ':' */
932 data
->scheme
= start
;
933 data
->scheme_len
= *ptr
- start
;
939 /* Tries to deduce the corresponding URL_SCHEME for the given URI. Stores
940 * the deduced URL_SCHEME in data->scheme_type.
942 static BOOL
parse_scheme_type(parse_data
*data
) {
943 /* If there's scheme data then see if it's a recognized scheme. */
944 if(data
->scheme
&& data
->scheme_len
) {
947 for(i
= 0; i
< sizeof(recognized_schemes
)/sizeof(recognized_schemes
[0]); ++i
) {
948 if(lstrlenW(recognized_schemes
[i
].scheme_name
) == data
->scheme_len
) {
949 /* Has to be a case insensitive compare. */
950 if(!StrCmpNIW(recognized_schemes
[i
].scheme_name
, data
->scheme
, data
->scheme_len
)) {
951 data
->scheme_type
= recognized_schemes
[i
].scheme
;
957 /* If we get here it means it's not a recognized scheme. */
958 data
->scheme_type
= URL_SCHEME_UNKNOWN
;
960 } else if(data
->is_relative
) {
961 /* Relative URI's have no scheme. */
962 data
->scheme_type
= URL_SCHEME_UNKNOWN
;
965 /* Should never reach here! what happened... */
966 FIXME("(%p): Unable to determine scheme type for URI %s\n", data
, debugstr_w(data
->uri
));
971 /* Tries to parse (or deduce) the scheme_name of a URI. If it can't
972 * parse a scheme from the URI it will try to deduce the scheme_name and scheme_type
973 * using the flags specified in 'flags' (if any). Flags that affect how this function
974 * operates are the Uri_CREATE_ALLOW_* flags.
976 * All parsed/deduced information will be stored in 'data' when the function returns.
978 * Returns TRUE if it was able to successfully parse the information.
980 static BOOL
parse_scheme(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
) {
981 static const WCHAR fileW
[] = {'f','i','l','e',0};
982 static const WCHAR wildcardW
[] = {'*',0};
984 /* First check to see if the uri could implicitly be a file path. */
985 if(is_implicit_file_path(*ptr
)) {
986 if(flags
& Uri_CREATE_ALLOW_IMPLICIT_FILE_SCHEME
) {
987 data
->scheme
= fileW
;
988 data
->scheme_len
= lstrlenW(fileW
);
989 data
->has_implicit_scheme
= TRUE
;
991 TRACE("(%p %p %x): URI is an implicit file path.\n", ptr
, data
, flags
);
993 /* Window's does not consider anything that can implicitly be a file
994 * path to be a valid URI if the ALLOW_IMPLICIT_FILE_SCHEME flag is not set...
996 TRACE("(%p %p %x): URI is implicitly a file path, but, the ALLOW_IMPLICIT_FILE_SCHEME flag wasn't set.\n",
1000 } else if(!parse_scheme_name(ptr
, data
)) {
1001 /* No Scheme was found, this means it could be:
1002 * a) an implicit Wildcard scheme
1006 if(flags
& Uri_CREATE_ALLOW_IMPLICIT_WILDCARD_SCHEME
) {
1007 data
->scheme
= wildcardW
;
1008 data
->scheme_len
= lstrlenW(wildcardW
);
1009 data
->has_implicit_scheme
= TRUE
;
1011 TRACE("(%p %p %x): URI is an implicit wildcard scheme.\n", ptr
, data
, flags
);
1012 } else if (flags
& Uri_CREATE_ALLOW_RELATIVE
) {
1013 data
->is_relative
= TRUE
;
1014 TRACE("(%p %p %x): URI is relative.\n", ptr
, data
, flags
);
1016 TRACE("(%p %p %x): Malformed URI found. Unable to deduce scheme name.\n", ptr
, data
, flags
);
1021 if(!data
->is_relative
)
1022 TRACE("(%p %p %x): Found scheme=%s scheme_len=%d\n", ptr
, data
, flags
,
1023 debugstr_wn(data
->scheme
, data
->scheme_len
), data
->scheme_len
);
1025 if(!parse_scheme_type(data
))
1028 TRACE("(%p %p %x): Assigned %d as the URL_SCHEME.\n", ptr
, data
, flags
, data
->scheme_type
);
1032 /* Parses the userinfo part of the URI (if it exists). The userinfo field of
1033 * a URI can consist of "username:password@", or just "username@".
1036 * userinfo = *( unreserved / pct-encoded / sub-delims / ":" )
1039 * 1) If there is more than one ':' in the userinfo part of the URI Windows
1040 * uses the first occurence of ':' to delimit the username and password
1044 * ftp://user:pass:word@winehq.org
1046 * Would yield, "user" as the username and "pass:word" as the password.
1048 * 2) Windows allows any character to appear in the "userinfo" part of
1049 * a URI, as long as it's not an authority delimeter character set.
1051 static void parse_userinfo(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
) {
1052 data
->userinfo
= *ptr
;
1053 data
->userinfo_split
= -1;
1055 while(**ptr
!= '@') {
1056 if(**ptr
== ':' && data
->userinfo_split
== -1)
1057 data
->userinfo_split
= *ptr
- data
->userinfo
;
1058 else if(**ptr
== '%') {
1059 /* If it's a known scheme type, it has to be a valid percent
1062 if(!check_pct_encoded(ptr
)) {
1063 if(data
->scheme_type
!= URL_SCHEME_UNKNOWN
) {
1064 *ptr
= data
->userinfo
;
1065 data
->userinfo
= NULL
;
1066 data
->userinfo_split
= -1;
1068 TRACE("(%p %p %x): URI contained no userinfo.\n", ptr
, data
, flags
);
1073 } else if(is_auth_delim(**ptr
, data
->scheme_type
!= URL_SCHEME_UNKNOWN
))
1080 *ptr
= data
->userinfo
;
1081 data
->userinfo
= NULL
;
1082 data
->userinfo_split
= -1;
1084 TRACE("(%p %p %x): URI contained no userinfo.\n", ptr
, data
, flags
);
1088 data
->userinfo_len
= *ptr
- data
->userinfo
;
1089 TRACE("(%p %p %x): Found userinfo=%s userinfo_len=%d split=%d.\n", ptr
, data
, flags
,
1090 debugstr_wn(data
->userinfo
, data
->userinfo_len
), data
->userinfo_len
, data
->userinfo_split
);
1094 /* Attempts to parse a port from the URI.
1097 * Windows seems to have a cap on what the maximum value
1098 * for a port can be. The max value is USHORT_MAX.
1102 static BOOL
parse_port(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
) {
1106 while(!is_auth_delim(**ptr
, data
->scheme_type
!= URL_SCHEME_UNKNOWN
)) {
1107 if(!is_num(**ptr
)) {
1113 port
= port
*10 + (**ptr
-'0');
1115 if(port
> USHORT_MAX
) {
1124 data
->port_value
= port
;
1125 data
->port_len
= *ptr
- data
->port
;
1127 TRACE("(%p %p %x): Found port %s len=%d value=%u\n", ptr
, data
, flags
,
1128 debugstr_wn(data
->port
, data
->port_len
), data
->port_len
, data
->port_value
);
1132 /* Attempts to parse a IPv4 address from the URI.
1135 * Window's normalizes IPv4 addresses, This means there's three
1136 * possibilities for the URI to contain an IPv4 address.
1137 * 1) A well formed address (ex. 192.2.2.2).
1138 * 2) A partially formed address. For example "192.0" would
1139 * normalize to "192.0.0.0" during canonicalization.
1140 * 3) An implicit IPv4 address. For example "256" would
1141 * normalize to "0.0.1.0" during canonicalization. Also
1142 * note that the maximum value for an implicit IP address
1143 * is UINT_MAX, if the value in the URI exceeds this then
1144 * it is not considered an IPv4 address.
1146 static BOOL
parse_ipv4address(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
) {
1147 const BOOL is_unknown
= data
->scheme_type
== URL_SCHEME_UNKNOWN
;
1150 if(!check_ipv4address(ptr
, FALSE
)) {
1151 if(!check_implicit_ipv4(ptr
, &data
->implicit_ipv4
)) {
1152 TRACE("(%p %p %x): URI didn't contain anything looking like an IPv4 address.\n",
1158 data
->has_implicit_ip
= TRUE
;
1161 /* Check if what we found is the only part of the host name (if it isn't
1162 * we don't have an IPv4 address).
1166 if(!parse_port(ptr
, data
, flags
)) {
1171 } else if(!is_auth_delim(**ptr
, !is_unknown
)) {
1172 /* Found more data which belongs the host, so this isn't an IPv4. */
1175 data
->has_implicit_ip
= FALSE
;
1179 data
->host_len
= *ptr
- data
->host
;
1180 data
->host_type
= Uri_HOST_IPV4
;
1182 TRACE("(%p %p %x): IPv4 address found. host=%s host_len=%d host_type=%d\n",
1183 ptr
, data
, flags
, debugstr_wn(data
->host
, data
->host_len
),
1184 data
->host_len
, data
->host_type
);
1188 /* Attempts to parse the reg-name from the URI.
1190 * Because of the way Windows handles ':' this function also
1191 * handles parsing the port.
1193 * reg-name = *( unreserved / pct-encoded / sub-delims )
1196 * Windows allows everything, but, the characters in "auth_delims" and ':'
1197 * to appear in a reg-name, unless it's an unknown scheme type then ':' is
1198 * allowed to appear (even if a valid port isn't after it).
1200 * Windows doesn't like host names which start with '[' and end with ']'
1201 * and don't contain a valid IP literal address in between them.
1203 * On Windows if an '[' is encountered in the host name the ':' no longer
1204 * counts as a delimiter until you reach the next ']' or an "authority delimeter".
1206 * A reg-name CAN be empty.
1208 static BOOL
parse_reg_name(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
) {
1209 const BOOL has_start_bracket
= **ptr
== '[';
1210 const BOOL known_scheme
= data
->scheme_type
!= URL_SCHEME_UNKNOWN
;
1211 BOOL inside_brackets
= has_start_bracket
;
1212 BOOL ignore_col
= FALSE
;
1214 /* We have to be careful with file schemes. */
1215 if(data
->scheme_type
== URL_SCHEME_FILE
) {
1216 /* This is because an implicit file scheme could be "C:\\test" and it
1217 * would trick this function into thinking the host is "C", when after
1218 * canonicalization the host would end up being an empty string.
1220 if(is_alpha(**ptr
) && *(*ptr
+1) == ':') {
1221 /* Regular old drive paths don't have a host type (or host name). */
1222 data
->host_type
= Uri_HOST_UNKNOWN
;
1226 } else if(**ptr
== '\\' && *(*ptr
+1) == '\\')
1227 /* Skip past the "\\" of a UNC path. */
1233 while(!is_auth_delim(**ptr
, known_scheme
)) {
1234 if(**ptr
== ':' && !ignore_col
) {
1235 /* We can ignore ':' if were inside brackets.*/
1236 if(!inside_brackets
) {
1237 const WCHAR
*tmp
= (*ptr
)++;
1239 /* Attempt to parse the port. */
1240 if(!parse_port(ptr
, data
, flags
)) {
1241 /* Windows expects there to be a valid port for known scheme types. */
1242 if(data
->scheme_type
!= URL_SCHEME_UNKNOWN
) {
1245 TRACE("(%p %p %x): Expected valid port\n", ptr
, data
, flags
);
1248 /* Windows gives up on trying to parse a port when it
1249 * encounters 1 invalid port.
1253 data
->host_len
= tmp
- data
->host
;
1257 } else if(**ptr
== '%' && known_scheme
) {
1258 /* Has to be a legit % encoded value. */
1259 if(!check_pct_encoded(ptr
)) {
1265 } else if(**ptr
== ']')
1266 inside_brackets
= FALSE
;
1267 else if(**ptr
== '[')
1268 inside_brackets
= TRUE
;
1273 if(has_start_bracket
) {
1274 /* Make sure the last character of the host wasn't a ']'. */
1275 if(*(*ptr
-1) == ']') {
1276 TRACE("(%p %p %x): Expected an IP literal inside of the host\n",
1284 /* Don't overwrite our length if we found a port earlier. */
1286 data
->host_len
= *ptr
- data
->host
;
1288 /* If the host is empty, then it's an unknown host type. */
1289 if(data
->host_len
== 0)
1290 data
->host_type
= Uri_HOST_UNKNOWN
;
1292 data
->host_type
= Uri_HOST_DNS
;
1294 TRACE("(%p %p %x): Parsed reg-name. host=%s len=%d\n", ptr
, data
, flags
,
1295 debugstr_wn(data
->host
, data
->host_len
), data
->host_len
);
1299 /* Attempts to parse an IPv6 address out of the URI.
1301 * IPv6address = 6( h16 ":" ) ls32
1302 * / "::" 5( h16 ":" ) ls32
1303 * / [ h16 ] "::" 4( h16 ":" ) ls32
1304 * / [ *1( h16 ":" ) h16 ] "::" 3( h16 ":" ) ls32
1305 * / [ *2( h16 ":" ) h16 ] "::" 2( h16 ":" ) ls32
1306 * / [ *3( h16 ":" ) h16 ] "::" h16 ":" ls32
1307 * / [ *4( h16 ":" ) h16 ] "::" ls32
1308 * / [ *5( h16 ":" ) h16 ] "::" h16
1309 * / [ *6( h16 ":" ) h16 ] "::"
1311 * ls32 = ( h16 ":" h16 ) / IPv4address
1312 * ; least-significant 32 bits of address.
1315 * ; 16 bits of address represented in hexadecimal.
1317 * Modeled after google-url's 'DoParseIPv6' function.
1319 static BOOL
parse_ipv6address(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
) {
1320 const WCHAR
*start
, *cur_start
;
1323 start
= cur_start
= *ptr
;
1324 memset(&ip
, 0, sizeof(ipv6_address
));
1327 /* Check if we're on the last character of the host. */
1328 BOOL is_end
= (is_auth_delim(**ptr
, data
->scheme_type
!= URL_SCHEME_UNKNOWN
)
1331 BOOL is_split
= (**ptr
== ':');
1332 BOOL is_elision
= (is_split
&& !is_end
&& *(*ptr
+1) == ':');
1334 /* Check if we're at the end of of the a component, or
1335 * if we're at the end of the IPv6 address.
1337 if(is_split
|| is_end
) {
1340 cur_len
= *ptr
- cur_start
;
1342 /* h16 can't have a length > 4. */
1346 TRACE("(%p %p %x): h16 component to long.\n",
1352 /* An h16 component can't have the length of 0 unless
1353 * the elision is at the beginning of the address, or
1354 * at the end of the address.
1356 if(!((*ptr
== start
&& is_elision
) ||
1357 (is_end
&& (*ptr
-2) == ip
.elision
))) {
1359 TRACE("(%p %p %x): IPv6 component can not have a length of 0.\n",
1366 /* An IPv6 address can have no more than 8 h16 components. */
1367 if(ip
.h16_count
>= 8) {
1369 TRACE("(%p %p %x): Not a IPv6 address, to many h16 components.\n",
1374 ip
.components
[ip
.h16_count
].str
= cur_start
;
1375 ip
.components
[ip
.h16_count
].len
= cur_len
;
1377 TRACE("(%p %p %x): Found h16 component %s, len=%d, h16_count=%d\n",
1378 ptr
, data
, flags
, debugstr_wn(cur_start
, cur_len
), cur_len
,
1388 /* A IPv6 address can only have 1 elision ('::'). */
1392 TRACE("(%p %p %x): IPv6 address cannot have 2 elisions.\n",
1404 if(!check_ipv4address(ptr
, TRUE
)) {
1405 if(!is_hexdigit(**ptr
)) {
1406 /* Not a valid character for an IPv6 address. */
1411 /* Found an IPv4 address. */
1412 ip
.ipv4
= cur_start
;
1413 ip
.ipv4_len
= *ptr
- cur_start
;
1415 TRACE("(%p %p %x): Found an attached IPv4 address %s len=%d.\n",
1416 ptr
, data
, flags
, debugstr_wn(ip
.ipv4
, ip
.ipv4_len
),
1419 /* IPv4 addresses can only appear at the end of a IPv6. */
1425 compute_ipv6_comps_size(&ip
);
1427 /* Make sure the IPv6 address adds up to 16 bytes. */
1428 if(ip
.components_size
+ ip
.elision_size
!= 16) {
1430 TRACE("(%p %p %x): Invalid IPv6 address, did not add up to 16 bytes.\n",
1435 if(ip
.elision_size
== 2) {
1436 /* For some reason on Windows if an elision that represents
1437 * only 1 h16 component is encountered at the very begin or
1438 * end of an IPv6 address, Windows does not consider it a
1439 * valid IPv6 address.
1441 * Ex: [::2:3:4:5:6:7] is not valid, even though the sum
1442 * of all the components == 128bits.
1444 if(ip
.elision
< ip
.components
[0].str
||
1445 ip
.elision
> ip
.components
[ip
.h16_count
-1].str
) {
1447 TRACE("(%p %p %x): Invalid IPv6 address. Detected elision of 2 bytes at the beginning or end of the address.\n",
1453 data
->host_type
= Uri_HOST_IPV6
;
1454 data
->has_ipv6
= TRUE
;
1455 data
->ipv6_address
= ip
;
1457 TRACE("(%p %p %x): Found valid IPv6 literal %s len=%d\n",
1458 ptr
, data
, flags
, debugstr_wn(start
, *ptr
-start
),
1463 /* IPvFuture = "v" 1*HEXDIG "." 1*( unreserved / sub-delims / ":" ) */
1464 static BOOL
parse_ipvfuture(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
) {
1465 const WCHAR
*start
= *ptr
;
1467 /* IPvFuture has to start with a 'v' or 'V'. */
1468 if(**ptr
!= 'v' && **ptr
!= 'V')
1471 /* Following the v their must be atleast 1 hexdigit. */
1473 if(!is_hexdigit(**ptr
)) {
1479 while(is_hexdigit(**ptr
))
1482 /* End of the hexdigit sequence must be a '.' */
1489 if(!is_unreserved(**ptr
) && !is_subdelim(**ptr
) && **ptr
!= ':') {
1495 while(is_unreserved(**ptr
) || is_subdelim(**ptr
) || **ptr
== ':')
1498 data
->host_type
= Uri_HOST_UNKNOWN
;
1500 TRACE("(%p %p %x): Parsed IPvFuture address %s len=%d\n", ptr
, data
, flags
,
1501 debugstr_wn(start
, *ptr
-start
), *ptr
-start
);
1506 /* IP-literal = "[" ( IPv6address / IPvFuture ) "]" */
1507 static BOOL
parse_ip_literal(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
) {
1516 if(!parse_ipv6address(ptr
, data
, flags
)) {
1517 if(!parse_ipvfuture(ptr
, data
, flags
)) {
1533 /* If a valid port is not found, then let it trickle down to
1536 if(!parse_port(ptr
, data
, flags
)) {
1542 data
->host_len
= *ptr
- data
->host
;
1547 /* Parses the host information from the URI.
1549 * host = IP-literal / IPv4address / reg-name
1551 static BOOL
parse_host(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
) {
1552 if(!parse_ip_literal(ptr
, data
, flags
)) {
1553 if(!parse_ipv4address(ptr
, data
, flags
)) {
1554 if(!parse_reg_name(ptr
, data
, flags
)) {
1555 TRACE("(%p %p %x): Malformed URI, Unknown host type.\n",
1565 /* Parses the authority information from the URI.
1567 * authority = [ userinfo "@" ] host [ ":" port ]
1569 static BOOL
parse_authority(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
) {
1570 parse_userinfo(ptr
, data
, flags
);
1572 /* Parsing the port will happen during one of the host parsing
1573 * routines (if the URI has a port).
1575 if(!parse_host(ptr
, data
, flags
))
1581 /* Attempts to parse the path information of a hierarchical URI. */
1582 static BOOL
parse_path_hierarchical(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
) {
1583 const WCHAR
*start
= *ptr
;
1584 static const WCHAR slash
[] = {'/',0};
1586 if(is_path_delim(**ptr
)) {
1587 if(data
->scheme_type
== URL_SCHEME_WILDCARD
) {
1588 /* Wildcard schemes don't get a '/' attached if their path is
1593 } else if(!(flags
& Uri_CREATE_NO_CANONICALIZE
)) {
1594 /* If the path component is empty, then a '/' is added. */
1599 while(!is_path_delim(**ptr
)) {
1600 if(**ptr
== '%' && data
->scheme_type
!= URL_SCHEME_UNKNOWN
&&
1601 data
->scheme_type
!= URL_SCHEME_FILE
) {
1602 if(!check_pct_encoded(ptr
)) {
1607 } else if(**ptr
== '\\') {
1608 /* Not allowed to have a backslash if NO_CANONICALIZE is set
1609 * and the scheme is known type (but not a file scheme).
1611 if(flags
& Uri_CREATE_NO_CANONICALIZE
) {
1612 if(data
->scheme_type
!= URL_SCHEME_FILE
&&
1613 data
->scheme_type
!= URL_SCHEME_UNKNOWN
) {
1623 /* The only time a URI doesn't have a path is when
1624 * the NO_CANONICALIZE flag is set and the raw URI
1625 * didn't contain one.
1632 data
->path_len
= *ptr
- start
;
1637 TRACE("(%p %p %x): Parsed path %s len=%d\n", ptr
, data
, flags
,
1638 debugstr_wn(data
->path
, data
->path_len
), data
->path_len
);
1640 TRACE("(%p %p %x): The URI contained no path\n", ptr
, data
, flags
);
1645 /* Parses the path of a opaque URI (much less strict then the parser
1646 * for a hierarchical URI).
1649 * Windows allows invalid % encoded data to appear in opaque URI paths
1650 * for unknown scheme types.
1652 static BOOL
parse_path_opaque(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
) {
1653 const BOOL known_scheme
= data
->scheme_type
!= URL_SCHEME_UNKNOWN
;
1657 while(!is_path_delim(**ptr
)) {
1658 if(**ptr
== '%' && known_scheme
) {
1659 if(!check_pct_encoded(ptr
)) {
1670 data
->path_len
= *ptr
- data
->path
;
1671 TRACE("(%p %p %x): Parsed opaque URI path %s len=%d\n", ptr
, data
, flags
,
1672 debugstr_wn(data
->path
, data
->path_len
), data
->path_len
);
1676 /* Determines how the URI should be parsed after the scheme information.
1678 * If the scheme is followed, by "//" then, it is treated as an hierarchical URI
1679 * which then the authority and path information will be parsed out. Otherwise, the
1680 * URI will be treated as an opaque URI which the authority information is not parsed
1683 * RFC 3896 definition of hier-part:
1685 * hier-part = "//" authority path-abempty
1690 * MSDN opaque URI definition:
1691 * scheme ":" path [ "#" fragment ]
1694 * If the URI is of an unknown scheme type and has a "//" following the scheme then it
1695 * is treated as a hierarchical URI, but, if the CREATE_NO_CRACK_UNKNOWN_SCHEMES flag is
1696 * set then it is considered an opaque URI reguardless of what follows the scheme information
1697 * (per MSDN documentation).
1699 static BOOL
parse_hierpart(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
) {
1700 const WCHAR
*start
= *ptr
;
1702 /* Checks if the authority information needs to be parsed.
1704 * Relative URI's aren't hierarchical URI's, but, they could trick
1705 * "check_hierarchical" into thinking it is, so we need to explicitly
1706 * make sure it's not relative. Also, if the URI is an implicit file
1707 * scheme it might not contain a "//", but, it's considered hierarchical
1708 * anyways. Wildcard Schemes are always considered hierarchical
1710 if(data
->scheme_type
== URL_SCHEME_WILDCARD
||
1711 data
->scheme_type
== URL_SCHEME_FILE
||
1712 (!data
->is_relative
&& check_hierarchical(ptr
))) {
1713 /* Only treat it as a hierarchical URI if the scheme_type is known or
1714 * the Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES flag is not set.
1716 if(data
->scheme_type
!= URL_SCHEME_UNKNOWN
||
1717 !(flags
& Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES
)) {
1718 TRACE("(%p %p %x): Treating URI as an hierarchical URI.\n", ptr
, data
, flags
);
1719 data
->is_opaque
= FALSE
;
1721 if(data
->scheme_type
== URL_SCHEME_FILE
)
1722 /* Skip past the "//" after the scheme (if any). */
1723 check_hierarchical(ptr
);
1725 /* TODO: Handle hierarchical URI's, parse authority then parse the path. */
1726 if(!parse_authority(ptr
, data
, flags
))
1729 return parse_path_hierarchical(ptr
, data
, flags
);
1731 /* Reset ptr to it's starting position so opaque path parsing
1732 * begins at the correct location.
1737 /* If it reaches here, then the URI will be treated as an opaque
1741 TRACE("(%p %p %x): Treating URI as an opaque URI.\n", ptr
, data
, flags
);
1743 data
->is_opaque
= TRUE
;
1744 if(!parse_path_opaque(ptr
, data
, flags
))
1750 /* Attempts to parse the query string from the URI.
1753 * If NO_DECODE_EXTRA_INFO flag is set, then invalid percent encoded
1754 * data is allowed appear in the query string. For unknown scheme types
1755 * invalid percent encoded data is allowed to appear reguardless.
1757 static BOOL
parse_query(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
) {
1758 const BOOL known_scheme
= data
->scheme_type
!= URL_SCHEME_UNKNOWN
;
1761 TRACE("(%p %p %x): URI didn't contain a query string.\n", ptr
, data
, flags
);
1768 while(**ptr
&& **ptr
!= '#') {
1769 if(**ptr
== '%' && known_scheme
&&
1770 !(flags
& Uri_CREATE_NO_DECODE_EXTRA_INFO
)) {
1771 if(!check_pct_encoded(ptr
)) {
1782 data
->query_len
= *ptr
- data
->query
;
1784 TRACE("(%p %p %x): Parsed query string %s len=%d\n", ptr
, data
, flags
,
1785 debugstr_wn(data
->query
, data
->query_len
), data
->query_len
);
1789 /* Attempts to parse the fragment from the URI.
1792 * If NO_DECODE_EXTRA_INFO flag is set, then invalid percent encoded
1793 * data is allowed appear in the query string. For unknown scheme types
1794 * invalid percent encoded data is allowed to appear reguardless.
1796 static BOOL
parse_fragment(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
) {
1797 const BOOL known_scheme
= data
->scheme_type
!= URL_SCHEME_UNKNOWN
;
1800 TRACE("(%p %p %x): URI didn't contain a fragment.\n", ptr
, data
, flags
);
1804 data
->fragment
= *ptr
;
1808 if(**ptr
== '%' && known_scheme
&&
1809 !(flags
& Uri_CREATE_NO_DECODE_EXTRA_INFO
)) {
1810 if(!check_pct_encoded(ptr
)) {
1811 *ptr
= data
->fragment
;
1812 data
->fragment
= NULL
;
1821 data
->fragment_len
= *ptr
- data
->fragment
;
1823 TRACE("(%p %p %x): Parsed fragment %s len=%d\n", ptr
, data
, flags
,
1824 debugstr_wn(data
->fragment
, data
->fragment_len
), data
->fragment_len
);
1828 /* Parses and validates the components of the specified by data->uri
1829 * and stores the information it parses into 'data'.
1831 * Returns TRUE if it successfully parsed the URI. False otherwise.
1833 static BOOL
parse_uri(parse_data
*data
, DWORD flags
) {
1840 TRACE("(%p %x): BEGINNING TO PARSE URI %s.\n", data
, flags
, debugstr_w(data
->uri
));
1842 if(!parse_scheme(pptr
, data
, flags
))
1845 if(!parse_hierpart(pptr
, data
, flags
))
1848 if(!parse_query(pptr
, data
, flags
))
1851 if(!parse_fragment(pptr
, data
, flags
))
1854 TRACE("(%p %x): FINISHED PARSING URI.\n", data
, flags
);
1858 /* Canonicalizes the userinfo of the URI represented by the parse_data.
1860 * Canonicalization of the userinfo is a simple process. If there are any percent
1861 * encoded characters that fall in the "unreserved" character set, they are decoded
1862 * to their actual value. If a character is not in the "unreserved" or "reserved" sets
1863 * then it is percent encoded. Other than that the characters are copied over without
1866 static BOOL
canonicalize_userinfo(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
1869 uri
->userinfo_start
= uri
->userinfo_split
= -1;
1870 uri
->userinfo_len
= 0;
1873 /* URI doesn't have userinfo, so nothing to do here. */
1876 uri
->userinfo_start
= uri
->canon_len
;
1878 while(i
< data
->userinfo_len
) {
1879 if(data
->userinfo
[i
] == ':' && uri
->userinfo_split
== -1)
1880 /* Windows only considers the first ':' as the delimiter. */
1881 uri
->userinfo_split
= uri
->canon_len
- uri
->userinfo_start
;
1882 else if(data
->userinfo
[i
] == '%') {
1883 /* Only decode % encoded values for known scheme types. */
1884 if(data
->scheme_type
!= URL_SCHEME_UNKNOWN
) {
1885 /* See if the value really needs decoded. */
1886 WCHAR val
= decode_pct_val(data
->userinfo
+ i
);
1887 if(is_unreserved(val
)) {
1889 uri
->canon_uri
[uri
->canon_len
] = val
;
1893 /* Move pass the hex characters. */
1898 } else if(!is_reserved(data
->userinfo
[i
]) && !is_unreserved(data
->userinfo
[i
]) &&
1899 data
->userinfo
[i
] != '\\') {
1900 /* Only percent encode forbidden characters if the NO_ENCODE_FORBIDDEN_CHARACTERS flag
1903 if(!(flags
& Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS
)) {
1905 pct_encode_val(data
->userinfo
[i
], uri
->canon_uri
+ uri
->canon_len
);
1907 uri
->canon_len
+= 3;
1914 /* Nothing special, so just copy the character over. */
1915 uri
->canon_uri
[uri
->canon_len
] = data
->userinfo
[i
];
1921 uri
->userinfo_len
= uri
->canon_len
- uri
->userinfo_start
;
1923 TRACE("(%p %p %x %d): Canonicalized userinfo, userinfo_start=%d, userinfo=%s, userinfo_split=%d userinfo_len=%d.\n",
1924 data
, uri
, flags
, computeOnly
, uri
->userinfo_start
, debugstr_wn(uri
->canon_uri
+ uri
->userinfo_start
, uri
->userinfo_len
),
1925 uri
->userinfo_split
, uri
->userinfo_len
);
1927 /* Now insert the '@' after the userinfo. */
1929 uri
->canon_uri
[uri
->canon_len
] = '@';
1935 /* Attempts to canonicalize a reg_name.
1937 * Things that happen:
1938 * 1) If Uri_CREATE_NO_CANONICALIZE flag is not set, then the reg_name is
1939 * lower cased. Unless it's an unknown scheme type, which case it's
1940 * no lower cased reguardless.
1942 * 2) Unreserved % encoded characters are decoded for known
1945 * 3) Forbidden characters are % encoded as long as
1946 * Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS flag is not set and
1947 * it isn't an unknown scheme type.
1949 * 4) If it's a file scheme and the host is "localhost" it's removed.
1951 static BOOL
canonicalize_reg_name(const parse_data
*data
, Uri
*uri
,
1952 DWORD flags
, BOOL computeOnly
) {
1953 static const WCHAR localhostW
[] =
1954 {'l','o','c','a','l','h','o','s','t',0};
1956 const BOOL known_scheme
= data
->scheme_type
!= URL_SCHEME_UNKNOWN
;
1958 uri
->host_start
= uri
->canon_len
;
1960 if(data
->scheme_type
== URL_SCHEME_FILE
&&
1961 data
->host_len
== lstrlenW(localhostW
)) {
1962 if(!StrCmpNIW(data
->host
, localhostW
, data
->host_len
)) {
1963 uri
->host_start
= -1;
1965 uri
->host_type
= Uri_HOST_UNKNOWN
;
1970 for(ptr
= data
->host
; ptr
< data
->host
+data
->host_len
; ++ptr
) {
1971 if(*ptr
== '%' && known_scheme
) {
1972 WCHAR val
= decode_pct_val(ptr
);
1973 if(is_unreserved(val
)) {
1974 /* If NO_CANONICALZE is not set, then windows lower cases the
1977 if(!(flags
& Uri_CREATE_NO_CANONICALIZE
) && isupperW(val
)) {
1979 uri
->canon_uri
[uri
->canon_len
] = tolowerW(val
);
1982 uri
->canon_uri
[uri
->canon_len
] = val
;
1986 /* Skip past the % encoded character. */
1990 /* Just copy the % over. */
1992 uri
->canon_uri
[uri
->canon_len
] = *ptr
;
1995 } else if(*ptr
== '\\') {
1996 /* Only unknown scheme types could have made it here with a '\\' in the host name. */
1998 uri
->canon_uri
[uri
->canon_len
] = *ptr
;
2000 } else if(!(flags
& Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS
) &&
2001 !is_unreserved(*ptr
) && !is_reserved(*ptr
) && known_scheme
) {
2003 pct_encode_val(*ptr
, uri
->canon_uri
+uri
->canon_len
);
2005 /* The percent encoded value gets lower cased also. */
2006 if(!(flags
& Uri_CREATE_NO_CANONICALIZE
)) {
2007 uri
->canon_uri
[uri
->canon_len
+1] = tolowerW(uri
->canon_uri
[uri
->canon_len
+1]);
2008 uri
->canon_uri
[uri
->canon_len
+2] = tolowerW(uri
->canon_uri
[uri
->canon_len
+2]);
2012 uri
->canon_len
+= 3;
2015 if(!(flags
& Uri_CREATE_NO_CANONICALIZE
) && known_scheme
)
2016 uri
->canon_uri
[uri
->canon_len
] = tolowerW(*ptr
);
2018 uri
->canon_uri
[uri
->canon_len
] = *ptr
;
2025 uri
->host_len
= uri
->canon_len
- uri
->host_start
;
2028 TRACE("(%p %p %x %d): Canonicalize reg_name=%s len=%d\n", data
, uri
, flags
,
2029 computeOnly
, debugstr_wn(uri
->canon_uri
+uri
->host_start
, uri
->host_len
),
2033 find_domain_name(uri
->canon_uri
+uri
->host_start
, uri
->host_len
,
2034 &(uri
->domain_offset
));
2039 /* Attempts to canonicalize an implicit IPv4 address. */
2040 static BOOL
canonicalize_implicit_ipv4address(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
2041 uri
->host_start
= uri
->canon_len
;
2043 TRACE("%u\n", data
->implicit_ipv4
);
2044 /* For unknown scheme types Window's doesn't convert
2045 * the value into an IP address, but, it still considers
2046 * it an IPv4 address.
2048 if(data
->scheme_type
== URL_SCHEME_UNKNOWN
) {
2050 memcpy(uri
->canon_uri
+uri
->canon_len
, data
->host
, data
->host_len
*sizeof(WCHAR
));
2051 uri
->canon_len
+= data
->host_len
;
2054 uri
->canon_len
+= ui2ipv4(uri
->canon_uri
+uri
->canon_len
, data
->implicit_ipv4
);
2056 uri
->canon_len
+= ui2ipv4(NULL
, data
->implicit_ipv4
);
2059 uri
->host_len
= uri
->canon_len
- uri
->host_start
;
2060 uri
->host_type
= Uri_HOST_IPV4
;
2063 TRACE("%p %p %x %d): Canonicalized implicit IP address=%s len=%d\n",
2064 data
, uri
, flags
, computeOnly
,
2065 debugstr_wn(uri
->canon_uri
+uri
->host_start
, uri
->host_len
),
2071 /* Attempts to canonicalize an IPv4 address.
2073 * If the parse_data represents a URI that has an implicit IPv4 address
2074 * (ex. http://256/, this function will convert 256 into 0.0.1.0). If
2075 * the implicit IP address exceeds the value of UINT_MAX (maximum value
2076 * for an IPv4 address) it's canonicalized as if were a reg-name.
2078 * If the parse_data contains a partial or full IPv4 address it normalizes it.
2079 * A partial IPv4 address is something like "192.0" and would be normalized to
2080 * "192.0.0.0". With a full (or partial) IPv4 address like "192.002.01.003" would
2081 * be normalized to "192.2.1.3".
2084 * Window's ONLY normalizes IPv4 address for known scheme types (one that isn't
2085 * URL_SCHEME_UNKNOWN). For unknown scheme types, it simply copies the data from
2086 * the original URI into the canonicalized URI, but, it still recognizes URI's
2087 * host type as HOST_IPV4.
2089 static BOOL
canonicalize_ipv4address(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
2090 if(data
->has_implicit_ip
)
2091 return canonicalize_implicit_ipv4address(data
, uri
, flags
, computeOnly
);
2093 uri
->host_start
= uri
->canon_len
;
2095 /* Windows only normalizes for known scheme types. */
2096 if(data
->scheme_type
!= URL_SCHEME_UNKNOWN
) {
2097 /* parse_data contains a partial or full IPv4 address, so normalize it. */
2098 DWORD i
, octetDigitCount
= 0, octetCount
= 0;
2099 BOOL octetHasDigit
= FALSE
;
2101 for(i
= 0; i
< data
->host_len
; ++i
) {
2102 if(data
->host
[i
] == '0' && !octetHasDigit
) {
2103 /* Can ignore leading zeros if:
2104 * 1) It isn't the last digit of the octet.
2105 * 2) i+1 != data->host_len
2108 if(octetDigitCount
== 2 ||
2109 i
+1 == data
->host_len
||
2110 data
->host
[i
+1] == '.') {
2112 uri
->canon_uri
[uri
->canon_len
] = data
->host
[i
];
2114 TRACE("Adding zero\n");
2116 } else if(data
->host
[i
] == '.') {
2118 uri
->canon_uri
[uri
->canon_len
] = data
->host
[i
];
2121 octetDigitCount
= 0;
2122 octetHasDigit
= FALSE
;
2126 uri
->canon_uri
[uri
->canon_len
] = data
->host
[i
];
2130 octetHasDigit
= TRUE
;
2134 /* Make sure the canonicalized IP address has 4 dec-octets.
2135 * If doesn't add "0" ones until there is 4;
2137 for( ; octetCount
< 3; ++octetCount
) {
2139 uri
->canon_uri
[uri
->canon_len
] = '.';
2140 uri
->canon_uri
[uri
->canon_len
+1] = '0';
2143 uri
->canon_len
+= 2;
2146 /* Windows doesn't normalize addresses in unknown schemes. */
2148 memcpy(uri
->canon_uri
+uri
->canon_len
, data
->host
, data
->host_len
*sizeof(WCHAR
));
2149 uri
->canon_len
+= data
->host_len
;
2152 uri
->host_len
= uri
->canon_len
- uri
->host_start
;
2154 TRACE("(%p %p %x %d): Canonicalized IPv4 address, ip=%s len=%d\n",
2155 data
, uri
, flags
, computeOnly
,
2156 debugstr_wn(uri
->canon_uri
+uri
->host_start
, uri
->host_len
),
2163 /* Attempts to canonicalize the IPv6 address of the URI.
2165 * Multiple things happen during the canonicalization of an IPv6 address:
2166 * 1) Any leading zero's in an h16 component are removed.
2167 * Ex: [0001:0022::] -> [1:22::]
2169 * 2) The longest sequence of zero h16 components are compressed
2170 * into a "::" (elision). If there's a tie, the first is choosen.
2172 * Ex: [0:0:0:0:1:6:7:8] -> [::1:6:7:8]
2173 * [0:0:0:0:1:2::] -> [::1:2:0:0]
2174 * [0:0:1:2:0:0:7:8] -> [::1:2:0:0:7:8]
2176 * 3) If an IPv4 address is attached to the IPv6 address, it's
2178 * Ex: [::001.002.022.000] -> [::1.2.22.0]
2180 * 4) If an elision is present, but, only represents 1 h16 component
2183 * Ex: [1::2:3:4:5:6:7] -> [1:0:2:3:4:5:6:7]
2185 * 5) If the IPv6 address contains an IPv4 address and there exists
2186 * at least 1 non-zero h16 component the IPv4 address is converted
2187 * into two h16 components, otherwise it's normalized and kept as is.
2189 * Ex: [::192.200.003.4] -> [::192.200.3.4]
2190 * [ffff::192.200.003.4] -> [ffff::c0c8:3041]
2193 * For unknown scheme types Windows simply copies the address over without any
2196 * IPv4 address can be included in an elision if all its components are 0's.
2198 static BOOL
canonicalize_ipv6address(const parse_data
*data
, Uri
*uri
,
2199 DWORD flags
, BOOL computeOnly
) {
2200 uri
->host_start
= uri
->canon_len
;
2202 if(data
->scheme_type
== URL_SCHEME_UNKNOWN
) {
2204 memcpy(uri
->canon_uri
+uri
->canon_len
, data
->host
, data
->host_len
*sizeof(WCHAR
));
2205 uri
->canon_len
+= data
->host_len
;
2209 DWORD i
, elision_len
;
2211 if(!ipv6_to_number(&(data
->ipv6_address
), values
)) {
2212 TRACE("(%p %p %x %d): Failed to compute numerical value for IPv6 address.\n",
2213 data
, uri
, flags
, computeOnly
);
2218 uri
->canon_uri
[uri
->canon_len
] = '[';
2221 /* Find where the elision should occur (if any). */
2222 compute_elision_location(&(data
->ipv6_address
), values
, &elision_start
, &elision_len
);
2224 TRACE("%p %p %x %d): Elision starts at %d, len=%u\n", data
, uri
, flags
,
2225 computeOnly
, elision_start
, elision_len
);
2227 for(i
= 0; i
< 8; ++i
) {
2228 BOOL in_elision
= (elision_start
> -1 && i
>= elision_start
&&
2229 i
< elision_start
+elision_len
);
2230 BOOL do_ipv4
= (i
== 6 && data
->ipv6_address
.ipv4
&& !in_elision
&&
2231 data
->ipv6_address
.h16_count
== 0);
2233 if(i
== elision_start
) {
2235 uri
->canon_uri
[uri
->canon_len
] = ':';
2236 uri
->canon_uri
[uri
->canon_len
+1] = ':';
2238 uri
->canon_len
+= 2;
2241 /* We can ignore the current component if we're in the elision. */
2245 /* We only add a ':' if we're not at i == 0, or when we're at
2246 * the very end of elision range since the ':' colon was handled
2247 * earlier. Otherwise we would end up with ":::" after elision.
2249 if(i
!= 0 && !(elision_start
> -1 && i
== elision_start
+elision_len
)) {
2251 uri
->canon_uri
[uri
->canon_len
] = ':';
2259 /* Combine the two parts of the IPv4 address values. */
2265 len
= ui2ipv4(uri
->canon_uri
+uri
->canon_len
, val
);
2267 len
= ui2ipv4(NULL
, val
);
2269 uri
->canon_len
+= len
;
2272 /* Write a regular h16 component to the URI. */
2274 /* Short circuit for the trivial case. */
2275 if(values
[i
] == 0) {
2277 uri
->canon_uri
[uri
->canon_len
] = '0';
2280 static const WCHAR formatW
[] = {'%','x',0};
2283 uri
->canon_len
+= sprintfW(uri
->canon_uri
+uri
->canon_len
,
2284 formatW
, values
[i
]);
2287 uri
->canon_len
+= sprintfW(tmp
, formatW
, values
[i
]);
2293 /* Add the closing ']'. */
2295 uri
->canon_uri
[uri
->canon_len
] = ']';
2299 uri
->host_len
= uri
->canon_len
- uri
->host_start
;
2302 TRACE("(%p %p %x %d): Canonicalized IPv6 address %s, len=%d\n", data
, uri
, flags
,
2303 computeOnly
, debugstr_wn(uri
->canon_uri
+uri
->host_start
, uri
->host_len
),
2309 /* Attempts to canonicalize the host of the URI (if any). */
2310 static BOOL
canonicalize_host(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
2311 uri
->host_start
= -1;
2313 uri
->domain_offset
= -1;
2316 switch(data
->host_type
) {
2318 uri
->host_type
= Uri_HOST_DNS
;
2319 if(!canonicalize_reg_name(data
, uri
, flags
, computeOnly
))
2324 uri
->host_type
= Uri_HOST_IPV4
;
2325 if(!canonicalize_ipv4address(data
, uri
, flags
, computeOnly
))
2330 if(!canonicalize_ipv6address(data
, uri
, flags
, computeOnly
))
2333 uri
->host_type
= Uri_HOST_IPV6
;
2335 case Uri_HOST_UNKNOWN
:
2336 if(data
->host_len
> 0 || data
->scheme_type
!= URL_SCHEME_FILE
) {
2337 uri
->host_start
= uri
->canon_len
;
2339 /* Nothing happens to unknown host types. */
2341 memcpy(uri
->canon_uri
+uri
->canon_len
, data
->host
, data
->host_len
*sizeof(WCHAR
));
2342 uri
->canon_len
+= data
->host_len
;
2343 uri
->host_len
= data
->host_len
;
2346 uri
->host_type
= Uri_HOST_UNKNOWN
;
2349 FIXME("(%p %p %x %d): Canonicalization for host type %d not supported.\n", data
,
2350 uri
, flags
, computeOnly
, data
->host_type
);
2358 static BOOL
canonicalize_port(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
2359 BOOL has_default_port
= FALSE
;
2360 USHORT default_port
= 0;
2363 uri
->has_port
= FALSE
;
2365 /* Check if the scheme has a default port. */
2366 for(i
= 0; i
< sizeof(default_ports
)/sizeof(default_ports
[0]); ++i
) {
2367 if(default_ports
[i
].scheme
== data
->scheme_type
) {
2368 has_default_port
= TRUE
;
2369 default_port
= default_ports
[i
].port
;
2374 if(data
->port
|| has_default_port
)
2375 uri
->has_port
= TRUE
;
2378 * 1) Has a port which is the default port.
2379 * 2) Has a port (not the default).
2380 * 3) Doesn't have a port, but, scheme has a default port.
2383 if(has_default_port
&& data
->port
&& data
->port_value
== default_port
) {
2384 /* If it's the default port and this flag isn't set, don't do anything. */
2385 if(flags
& Uri_CREATE_NO_CANONICALIZE
) {
2386 /* Copy the original port over. */
2388 uri
->canon_uri
[uri
->canon_len
] = ':';
2389 memcpy(uri
->canon_uri
+uri
->canon_len
+1, data
->port
, data
->port_len
*sizeof(WCHAR
));
2391 uri
->canon_len
+= data
->port_len
+1;
2394 uri
->port
= default_port
;
2395 } else if(data
->port
) {
2397 uri
->canon_uri
[uri
->canon_len
] = ':';
2400 if(flags
& Uri_CREATE_NO_CANONICALIZE
) {
2401 /* Copy the original over without changes. */
2403 memcpy(uri
->canon_uri
+uri
->canon_len
, data
->port
, data
->port_len
*sizeof(WCHAR
));
2404 uri
->canon_len
+= data
->port_len
;
2406 const WCHAR formatW
[] = {'%','u',0};
2409 len
= sprintfW(uri
->canon_uri
+uri
->canon_len
, formatW
, data
->port_value
);
2412 len
= sprintfW(tmp
, formatW
, data
->port_value
);
2414 uri
->canon_len
+= len
;
2417 uri
->port
= data
->port_value
;
2418 } else if(has_default_port
)
2419 uri
->port
= default_port
;
2424 /* Canonicalizes the authority of the URI represented by the parse_data. */
2425 static BOOL
canonicalize_authority(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
2426 uri
->authority_start
= uri
->canon_len
;
2427 uri
->authority_len
= 0;
2429 if(!canonicalize_userinfo(data
, uri
, flags
, computeOnly
))
2432 if(!canonicalize_host(data
, uri
, flags
, computeOnly
))
2435 if(!canonicalize_port(data
, uri
, flags
, computeOnly
))
2438 if(uri
->host_start
!= -1)
2439 uri
->authority_len
= uri
->canon_len
- uri
->authority_start
;
2441 uri
->authority_start
= -1;
2446 /* Attempts to canonicalize the path of a hierarchical URI.
2448 * Things that happen:
2449 * 1). Forbidden characters are percent encoded, unless the NO_ENCODE_FORBIDDEN
2450 * flag is set or it's a file URI. Forbidden characters are always encoded
2451 * for file schemes reguardless and forbidden characters are never encoded
2452 * for unknown scheme types.
2454 * 2). For known scheme types '\\' are changed to '/'.
2456 * 3). Percent encoded, unreserved characters are decoded to their actual values.
2457 * Unless the scheme type is unknown. For file schemes any percent encoded
2458 * character in the unreserved or reserved set is decoded.
2460 * 4). For File schemes if the path is starts with a drive letter and doesn't
2461 * start with a '/' then one is appended.
2462 * Ex: file://c:/test.mp3 -> file:///c:/test.mp3
2464 * 5). Dot segments are removed from the path for all scheme types
2465 * unless NO_CANONICALIZE flag is set. Dot segments aren't removed
2466 * for wildcard scheme types.
2469 * file://c:/test%20test -> file:///c:/test%2520test
2470 * file://c:/test%3Etest -> file:///c:/test%253Etest
2471 * file:///c:/test%20test -> file:///c:/test%20test
2472 * file:///c:/test%test -> file:///c:/test%25test
2474 static BOOL
canonicalize_path_hierarchical(const parse_data
*data
, Uri
*uri
,
2475 DWORD flags
, BOOL computeOnly
) {
2477 const BOOL known_scheme
= data
->scheme_type
!= URL_SCHEME_UNKNOWN
;
2478 const BOOL is_file
= data
->scheme_type
== URL_SCHEME_FILE
;
2480 BOOL escape_pct
= FALSE
;
2483 uri
->path_start
= -1;
2488 uri
->path_start
= uri
->canon_len
;
2490 /* Check if a '/' needs to be appended for the file scheme. */
2492 if(data
->path_len
> 1 && is_alpha(*(data
->path
)) &&
2493 *(data
->path
+1) == ':') {
2495 uri
->canon_uri
[uri
->canon_len
] = '/';
2501 for(ptr
= data
->path
; ptr
< data
->path
+data
->path_len
; ++ptr
) {
2503 const WCHAR
*tmp
= ptr
;
2506 /* Check if the % represents a valid encoded char, or if it needs encoded. */
2507 BOOL force_encode
= !check_pct_encoded(&tmp
) && is_file
;
2508 val
= decode_pct_val(ptr
);
2510 if(force_encode
|| escape_pct
) {
2511 /* Escape the percent sign in the file URI. */
2513 pct_encode_val(*ptr
, uri
->canon_uri
+uri
->canon_len
);
2514 uri
->canon_len
+= 3;
2515 } else if((is_unreserved(val
) && known_scheme
) ||
2516 (is_file
&& (is_unreserved(val
) || is_reserved(val
)))) {
2518 uri
->canon_uri
[uri
->canon_len
] = val
;
2525 uri
->canon_uri
[uri
->canon_len
] = *ptr
;
2528 } else if(*ptr
== '\\' && known_scheme
) {
2530 uri
->canon_uri
[uri
->canon_len
] = '/';
2532 } else if(known_scheme
&& !is_unreserved(*ptr
) && !is_reserved(*ptr
) &&
2533 (!(flags
& Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS
) || is_file
)) {
2534 /* Escape the forbidden character. */
2536 pct_encode_val(*ptr
, uri
->canon_uri
+uri
->canon_len
);
2537 uri
->canon_len
+= 3;
2540 uri
->canon_uri
[uri
->canon_len
] = *ptr
;
2545 uri
->path_len
= uri
->canon_len
- uri
->path_start
;
2547 /* Removing the dot segments only happens when it's not in
2548 * computeOnly mode and it's not a wildcard scheme.
2550 if(!computeOnly
&& data
->scheme_type
!= URL_SCHEME_WILDCARD
) {
2551 if(!(flags
& Uri_CREATE_NO_CANONICALIZE
)) {
2552 /* Remove the dot segments (if any) and reset everything to the new
2555 DWORD new_len
= remove_dot_segments(uri
->canon_uri
+uri
->path_start
, uri
->path_len
);
2556 uri
->canon_len
-= uri
->path_len
-new_len
;
2557 uri
->path_len
= new_len
;
2562 TRACE("Canonicalized path %s len=%d\n",
2563 debugstr_wn(uri
->canon_uri
+uri
->path_start
, uri
->path_len
),
2569 /* Attempts to canonicalize the path for an opaque URI.
2571 * For known scheme types:
2572 * 1) forbidden characters are percent encoded if
2573 * NO_ENCODE_FORBIDDEN_CHARACTERS isn't set.
2575 * 2) Percent encoded, unreserved characters are decoded
2576 * to their actual values, for known scheme types.
2578 * 3) '\\' are changed to '/' for known scheme types
2579 * except for mailto schemes.
2581 static BOOL
canonicalize_path_opaque(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
2583 const BOOL known_scheme
= data
->scheme_type
!= URL_SCHEME_UNKNOWN
;
2586 uri
->path_start
= -1;
2591 uri
->path_start
= uri
->canon_len
;
2593 /* Windows doesn't allow a "//" to appear after the scheme
2594 * of a URI, if it's an opaque URI.
2596 if(data
->scheme
&& *(data
->path
) == '/' && *(data
->path
+1) == '/') {
2597 /* So it inserts a "/." before the "//" if it exists. */
2599 uri
->canon_uri
[uri
->canon_len
] = '/';
2600 uri
->canon_uri
[uri
->canon_len
+1] = '.';
2603 uri
->canon_len
+= 2;
2606 for(ptr
= data
->path
; ptr
< data
->path
+data
->path_len
; ++ptr
) {
2607 if(*ptr
== '%' && known_scheme
) {
2608 WCHAR val
= decode_pct_val(ptr
);
2610 if(is_unreserved(val
)) {
2612 uri
->canon_uri
[uri
->canon_len
] = val
;
2619 uri
->canon_uri
[uri
->canon_len
] = *ptr
;
2622 } else if(known_scheme
&& !is_unreserved(*ptr
) && !is_reserved(*ptr
) &&
2623 !(flags
& Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS
)) {
2625 pct_encode_val(*ptr
, uri
->canon_uri
+uri
->canon_len
);
2626 uri
->canon_len
+= 3;
2629 uri
->canon_uri
[uri
->canon_len
] = *ptr
;
2634 uri
->path_len
= uri
->canon_len
- uri
->path_start
;
2636 TRACE("(%p %p %x %d): Canonicalized opaque URI path %s len=%d\n", data
, uri
, flags
, computeOnly
,
2637 debugstr_wn(uri
->canon_uri
+uri
->path_start
, uri
->path_len
), uri
->path_len
);
2641 /* Determines how the URI represented by the parse_data should be canonicalized.
2643 * Essentially, if the parse_data represents an hierarchical URI then it calls
2644 * canonicalize_authority and the canonicalization functions for the path. If the
2645 * URI is opaque it canonicalizes the path of the URI.
2647 static BOOL
canonicalize_hierpart(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
2648 if(!data
->is_opaque
) {
2649 /* "//" is only added for non-wildcard scheme types. */
2650 if(data
->scheme_type
!= URL_SCHEME_WILDCARD
) {
2652 INT pos
= uri
->canon_len
;
2654 uri
->canon_uri
[pos
] = '/';
2655 uri
->canon_uri
[pos
+1] = '/';
2657 uri
->canon_len
+= 2;
2660 if(!canonicalize_authority(data
, uri
, flags
, computeOnly
))
2663 /* TODO: Canonicalize the path of the URI. */
2664 if(!canonicalize_path_hierarchical(data
, uri
, flags
, computeOnly
))
2668 /* Opaque URI's don't have an authority. */
2669 uri
->userinfo_start
= uri
->userinfo_split
= -1;
2670 uri
->userinfo_len
= 0;
2671 uri
->host_start
= -1;
2673 uri
->host_type
= Uri_HOST_UNKNOWN
;
2674 uri
->has_port
= FALSE
;
2675 uri
->authority_start
= -1;
2676 uri
->authority_len
= 0;
2677 uri
->domain_offset
= -1;
2679 if(!canonicalize_path_opaque(data
, uri
, flags
, computeOnly
))
2683 if(uri
->path_start
> -1 && !computeOnly
)
2684 /* Finding file extensions happens for both types of URIs. */
2685 uri
->extension_offset
= find_file_extension(uri
->canon_uri
+uri
->path_start
, uri
->path_len
);
2687 uri
->extension_offset
= -1;
2692 /* Attempts to canonicalize the query string of the URI.
2694 * Things that happen:
2695 * 1) For known scheme types forbidden characters
2696 * are percent encoded, unless the NO_DECODE_EXTRA_INFO flag is set
2697 * or NO_ENCODE_FORBIDDEN_CHARACTERS is set.
2699 * 2) For known scheme types, percent encoded, unreserved characters
2700 * are decoded as long as the NO_DECODE_EXTRA_INFO flag isn't set.
2702 static BOOL
canonicalize_query(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
2703 const WCHAR
*ptr
, *end
;
2704 const BOOL known_scheme
= data
->scheme_type
!= URL_SCHEME_UNKNOWN
;
2707 uri
->query_start
= -1;
2712 uri
->query_start
= uri
->canon_len
;
2714 end
= data
->query
+data
->query_len
;
2715 for(ptr
= data
->query
; ptr
< end
; ++ptr
) {
2717 if(known_scheme
&& !(flags
& Uri_CREATE_NO_DECODE_EXTRA_INFO
)) {
2718 WCHAR val
= decode_pct_val(ptr
);
2719 if(is_unreserved(val
)) {
2721 uri
->canon_uri
[uri
->canon_len
] = val
;
2728 } else if(known_scheme
&& !is_unreserved(*ptr
) && !is_reserved(*ptr
)) {
2729 if(!(flags
& Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS
) &&
2730 !(flags
& Uri_CREATE_NO_DECODE_EXTRA_INFO
)) {
2732 pct_encode_val(*ptr
, uri
->canon_uri
+uri
->canon_len
);
2733 uri
->canon_len
+= 3;
2739 uri
->canon_uri
[uri
->canon_len
] = *ptr
;
2743 uri
->query_len
= uri
->canon_len
- uri
->query_start
;
2746 TRACE("(%p %p %x %d): Canonicalized query string %s len=%d\n", data
, uri
, flags
,
2747 computeOnly
, debugstr_wn(uri
->canon_uri
+uri
->query_start
, uri
->query_len
),
2752 /* Canonicalizes the scheme information specified in the parse_data using the specified flags. */
2753 static BOOL
canonicalize_scheme(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
2754 uri
->scheme_start
= -1;
2755 uri
->scheme_len
= 0;
2758 /* The only type of URI that doesn't have to have a scheme is a relative
2761 if(!data
->is_relative
) {
2762 FIXME("(%p %p %x): Unable to determine the scheme type of %s.\n", data
,
2763 uri
, flags
, debugstr_w(data
->uri
));
2769 INT pos
= uri
->canon_len
;
2771 for(i
= 0; i
< data
->scheme_len
; ++i
) {
2772 /* Scheme name must be lower case after canonicalization. */
2773 uri
->canon_uri
[i
+ pos
] = tolowerW(data
->scheme
[i
]);
2776 uri
->canon_uri
[i
+ pos
] = ':';
2777 uri
->scheme_start
= pos
;
2779 TRACE("(%p %p %x): Canonicalized scheme=%s, len=%d.\n", data
, uri
, flags
,
2780 debugstr_wn(uri
->canon_uri
, uri
->scheme_len
), data
->scheme_len
);
2783 /* This happens in both computation modes. */
2784 uri
->canon_len
+= data
->scheme_len
+ 1;
2785 uri
->scheme_len
= data
->scheme_len
;
2790 /* Compute's what the length of the URI specified by the parse_data will be
2791 * after canonicalization occurs using the specified flags.
2793 * This function will return a non-zero value indicating the length of the canonicalized
2794 * URI, or -1 on error.
2796 static int compute_canonicalized_length(const parse_data
*data
, DWORD flags
) {
2799 memset(&uri
, 0, sizeof(Uri
));
2801 TRACE("(%p %x): Beginning to compute canonicalized length for URI %s\n", data
, flags
,
2802 debugstr_w(data
->uri
));
2804 if(!canonicalize_scheme(data
, &uri
, flags
, TRUE
)) {
2805 ERR("(%p %x): Failed to compute URI scheme length.\n", data
, flags
);
2809 if(!canonicalize_hierpart(data
, &uri
, flags
, TRUE
)) {
2810 ERR("(%p %x): Failed to compute URI hierpart length.\n", data
, flags
);
2814 if(!canonicalize_query(data
, &uri
, flags
, TRUE
)) {
2815 ERR("(%p %x): Failed to compute query string length.\n", data
, flags
);
2819 TRACE("(%p %x): Finished computing canonicalized URI length. length=%d\n", data
, flags
, uri
.canon_len
);
2821 return uri
.canon_len
;
2824 /* Canonicalizes the URI data specified in the parse_data, using the given flags. If the
2825 * canonicalization succeededs it will store all the canonicalization information
2826 * in the pointer to the Uri.
2828 * To canonicalize a URI this function first computes what the length of the URI
2829 * specified by the parse_data will be. Once this is done it will then perfom the actual
2830 * canonicalization of the URI.
2832 static HRESULT
canonicalize_uri(const parse_data
*data
, Uri
*uri
, DWORD flags
) {
2835 uri
->canon_uri
= NULL
;
2836 len
= uri
->canon_size
= uri
->canon_len
= 0;
2838 TRACE("(%p %p %x): beginning to canonicalize URI %s.\n", data
, uri
, flags
, debugstr_w(data
->uri
));
2840 /* First try to compute the length of the URI. */
2841 len
= compute_canonicalized_length(data
, flags
);
2843 ERR("(%p %p %x): Could not compute the canonicalized length of %s.\n", data
, uri
, flags
,
2844 debugstr_w(data
->uri
));
2845 return E_INVALIDARG
;
2848 uri
->canon_uri
= heap_alloc((len
+1)*sizeof(WCHAR
));
2850 return E_OUTOFMEMORY
;
2852 uri
->canon_size
= len
;
2853 if(!canonicalize_scheme(data
, uri
, flags
, FALSE
)) {
2854 ERR("(%p %p %x): Unable to canonicalize the scheme of the URI.\n", data
, uri
, flags
);
2855 heap_free(uri
->canon_uri
);
2856 return E_INVALIDARG
;
2858 uri
->scheme_type
= data
->scheme_type
;
2860 if(!canonicalize_hierpart(data
, uri
, flags
, FALSE
)) {
2861 ERR("(%p %p %x): Unable to canonicalize the heirpart of the URI\n", data
, uri
, flags
);
2862 heap_free(uri
->canon_uri
);
2863 return E_INVALIDARG
;
2866 if(!canonicalize_query(data
, uri
, flags
, FALSE
)) {
2867 ERR("(%p %p %x): Unable to canonicalize query string of the URI.\n",
2869 return E_INVALIDARG
;
2872 /* There's a possibility we didn't use all the space we allocated
2875 if(uri
->canon_len
< uri
->canon_size
) {
2876 /* This happens if the URI is hierarchical and dot
2877 * segments were removed from it's path.
2879 WCHAR
*tmp
= heap_realloc(uri
->canon_uri
, (uri
->canon_len
+1)*sizeof(WCHAR
));
2881 return E_OUTOFMEMORY
;
2883 uri
->canon_uri
= tmp
;
2884 uri
->canon_size
= uri
->canon_len
;
2887 uri
->canon_uri
[uri
->canon_len
] = '\0';
2888 TRACE("(%p %p %x): finished canonicalizing the URI. uri=%s\n", data
, uri
, flags
, debugstr_w(uri
->canon_uri
));
2893 #define URI(x) ((IUri*) &(x)->lpIUriVtbl)
2894 #define URIBUILDER(x) ((IUriBuilder*) &(x)->lpIUriBuilderVtbl)
2896 #define URI_THIS(iface) DEFINE_THIS(Uri, IUri, iface)
2898 static HRESULT WINAPI
Uri_QueryInterface(IUri
*iface
, REFIID riid
, void **ppv
)
2900 Uri
*This
= URI_THIS(iface
);
2902 if(IsEqualGUID(&IID_IUnknown
, riid
)) {
2903 TRACE("(%p)->(IID_IUnknown %p)\n", This
, ppv
);
2905 }else if(IsEqualGUID(&IID_IUri
, riid
)) {
2906 TRACE("(%p)->(IID_IUri %p)\n", This
, ppv
);
2909 TRACE("(%p)->(%s %p)\n", This
, debugstr_guid(riid
), ppv
);
2911 return E_NOINTERFACE
;
2914 IUnknown_AddRef((IUnknown
*)*ppv
);
2918 static ULONG WINAPI
Uri_AddRef(IUri
*iface
)
2920 Uri
*This
= URI_THIS(iface
);
2921 LONG ref
= InterlockedIncrement(&This
->ref
);
2923 TRACE("(%p) ref=%d\n", This
, ref
);
2928 static ULONG WINAPI
Uri_Release(IUri
*iface
)
2930 Uri
*This
= URI_THIS(iface
);
2931 LONG ref
= InterlockedDecrement(&This
->ref
);
2933 TRACE("(%p) ref=%d\n", This
, ref
);
2936 SysFreeString(This
->raw_uri
);
2937 heap_free(This
->canon_uri
);
2944 static HRESULT WINAPI
Uri_GetPropertyBSTR(IUri
*iface
, Uri_PROPERTY uriProp
, BSTR
*pbstrProperty
, DWORD dwFlags
)
2946 Uri
*This
= URI_THIS(iface
);
2948 TRACE("(%p)->(%d %p %x)\n", This
, uriProp
, pbstrProperty
, dwFlags
);
2953 if(uriProp
> Uri_PROPERTY_STRING_LAST
) {
2954 /* Windows allocates an empty BSTR for invalid Uri_PROPERTY's. */
2955 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
2956 if(!(*pbstrProperty
))
2957 return E_OUTOFMEMORY
;
2959 /* It only returns S_FALSE for the ZONE property... */
2960 if(uriProp
== Uri_PROPERTY_ZONE
)
2966 /* Don't have support for flags yet. */
2968 FIXME("(%p)->(%d %p %x)\n", This
, uriProp
, pbstrProperty
, dwFlags
);
2973 case Uri_PROPERTY_AUTHORITY
:
2974 if(This
->authority_start
> -1) {
2975 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+This
->authority_start
, This
->authority_len
);
2978 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
2982 if(!(*pbstrProperty
))
2983 hres
= E_OUTOFMEMORY
;
2986 case Uri_PROPERTY_DOMAIN
:
2987 if(This
->domain_offset
> -1) {
2988 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+This
->host_start
+This
->domain_offset
,
2989 This
->host_len
-This
->domain_offset
);
2992 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
2996 if(!(*pbstrProperty
))
2997 hres
= E_OUTOFMEMORY
;
3000 case Uri_PROPERTY_EXTENSION
:
3001 if(This
->extension_offset
> -1) {
3002 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+This
->path_start
+This
->extension_offset
,
3003 This
->path_len
-This
->extension_offset
);
3006 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
3010 if(!(*pbstrProperty
))
3011 hres
= E_OUTOFMEMORY
;
3014 case Uri_PROPERTY_HOST
:
3015 if(This
->host_start
> -1) {
3016 /* The '[' and ']' aren't included for IPv6 addresses. */
3017 if(This
->host_type
== Uri_HOST_IPV6
)
3018 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+This
->host_start
+1, This
->host_len
-2);
3020 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+This
->host_start
, This
->host_len
);
3024 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
3028 if(!(*pbstrProperty
))
3029 hres
= E_OUTOFMEMORY
;
3032 case Uri_PROPERTY_PASSWORD
:
3033 if(This
->userinfo_split
> -1) {
3034 *pbstrProperty
= SysAllocStringLen(
3035 This
->canon_uri
+This
->userinfo_start
+This
->userinfo_split
+1,
3036 This
->userinfo_len
-This
->userinfo_split
-1);
3039 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
3043 if(!(*pbstrProperty
))
3044 return E_OUTOFMEMORY
;
3047 case Uri_PROPERTY_PATH
:
3048 if(This
->path_start
> -1) {
3049 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+This
->path_start
, This
->path_len
);
3052 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
3056 if(!(*pbstrProperty
))
3057 hres
= E_OUTOFMEMORY
;
3060 case Uri_PROPERTY_PATH_AND_QUERY
:
3061 if(This
->path_start
> -1) {
3062 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+This
->path_start
, This
->path_len
+This
->query_len
);
3064 } else if(This
->query_start
> -1) {
3065 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+This
->query_start
, This
->query_len
);
3068 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
3072 if(!(*pbstrProperty
))
3073 hres
= E_OUTOFMEMORY
;
3076 case Uri_PROPERTY_QUERY
:
3077 if(This
->query_start
> -1) {
3078 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+This
->query_start
, This
->query_len
);
3081 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
3085 if(!(*pbstrProperty
))
3086 hres
= E_OUTOFMEMORY
;
3089 case Uri_PROPERTY_RAW_URI
:
3090 *pbstrProperty
= SysAllocString(This
->raw_uri
);
3091 if(!(*pbstrProperty
))
3092 hres
= E_OUTOFMEMORY
;
3096 case Uri_PROPERTY_SCHEME_NAME
:
3097 if(This
->scheme_start
> -1) {
3098 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+ This
->scheme_start
, This
->scheme_len
);
3101 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
3105 if(!(*pbstrProperty
))
3106 hres
= E_OUTOFMEMORY
;
3109 case Uri_PROPERTY_USER_INFO
:
3110 if(This
->userinfo_start
> -1) {
3111 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+This
->userinfo_start
, This
->userinfo_len
);
3114 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
3118 if(!(*pbstrProperty
))
3119 hres
= E_OUTOFMEMORY
;
3122 case Uri_PROPERTY_USER_NAME
:
3123 if(This
->userinfo_start
> -1) {
3124 /* If userinfo_split is set, that means a password exists
3125 * so the username is only from userinfo_start to userinfo_split.
3127 if(This
->userinfo_split
> -1) {
3128 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+ This
->userinfo_start
, This
->userinfo_split
);
3131 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+ This
->userinfo_start
, This
->userinfo_len
);
3135 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
3139 if(!(*pbstrProperty
))
3140 return E_OUTOFMEMORY
;
3144 FIXME("(%p)->(%d %p %x)\n", This
, uriProp
, pbstrProperty
, dwFlags
);
3151 static HRESULT WINAPI
Uri_GetPropertyLength(IUri
*iface
, Uri_PROPERTY uriProp
, DWORD
*pcchProperty
, DWORD dwFlags
)
3153 Uri
*This
= URI_THIS(iface
);
3155 TRACE("(%p)->(%d %p %x)\n", This
, uriProp
, pcchProperty
, dwFlags
);
3158 return E_INVALIDARG
;
3160 /* Can only return a length for a property if it's a string. */
3161 if(uriProp
> Uri_PROPERTY_STRING_LAST
)
3162 return E_INVALIDARG
;
3164 /* Don't have support for flags yet. */
3166 FIXME("(%p)->(%d %p %x)\n", This
, uriProp
, pcchProperty
, dwFlags
);
3171 case Uri_PROPERTY_AUTHORITY
:
3172 *pcchProperty
= This
->authority_len
;
3173 hres
= (This
->authority_start
> -1) ? S_OK
: S_FALSE
;
3175 case Uri_PROPERTY_DOMAIN
:
3176 if(This
->domain_offset
> -1)
3177 *pcchProperty
= This
->host_len
- This
->domain_offset
;
3181 hres
= (This
->domain_offset
> -1) ? S_OK
: S_FALSE
;
3183 case Uri_PROPERTY_EXTENSION
:
3184 if(This
->extension_offset
> -1) {
3185 *pcchProperty
= This
->path_len
- This
->extension_offset
;
3193 case Uri_PROPERTY_HOST
:
3194 *pcchProperty
= This
->host_len
;
3196 /* '[' and ']' aren't included in the length. */
3197 if(This
->host_type
== Uri_HOST_IPV6
)
3200 hres
= (This
->host_start
> -1) ? S_OK
: S_FALSE
;
3202 case Uri_PROPERTY_PASSWORD
:
3203 *pcchProperty
= (This
->userinfo_split
> -1) ? This
->userinfo_len
-This
->userinfo_split
-1 : 0;
3204 hres
= (This
->userinfo_split
> -1) ? S_OK
: S_FALSE
;
3206 case Uri_PROPERTY_PATH
:
3207 *pcchProperty
= This
->path_len
;
3208 hres
= (This
->path_start
> -1) ? S_OK
: S_FALSE
;
3210 case Uri_PROPERTY_PATH_AND_QUERY
:
3211 *pcchProperty
= This
->path_len
+This
->query_len
;
3212 hres
= (This
->path_start
> -1 || This
->query_start
> -1) ? S_OK
: S_FALSE
;
3214 case Uri_PROPERTY_QUERY
:
3215 *pcchProperty
= This
->query_len
;
3216 hres
= (This
->query_start
> -1) ? S_OK
: S_FALSE
;
3218 case Uri_PROPERTY_RAW_URI
:
3219 *pcchProperty
= SysStringLen(This
->raw_uri
);
3222 case Uri_PROPERTY_SCHEME_NAME
:
3223 *pcchProperty
= This
->scheme_len
;
3224 hres
= (This
->scheme_start
> -1) ? S_OK
: S_FALSE
;
3226 case Uri_PROPERTY_USER_INFO
:
3227 *pcchProperty
= This
->userinfo_len
;
3228 hres
= (This
->userinfo_start
> -1) ? S_OK
: S_FALSE
;
3230 case Uri_PROPERTY_USER_NAME
:
3231 *pcchProperty
= (This
->userinfo_split
> -1) ? This
->userinfo_split
: This
->userinfo_len
;
3232 hres
= (This
->userinfo_start
> -1) ? S_OK
: S_FALSE
;
3235 FIXME("(%p)->(%d %p %x)\n", This
, uriProp
, pcchProperty
, dwFlags
);
3242 static HRESULT WINAPI
Uri_GetPropertyDWORD(IUri
*iface
, Uri_PROPERTY uriProp
, DWORD
*pcchProperty
, DWORD dwFlags
)
3244 Uri
*This
= URI_THIS(iface
);
3247 TRACE("(%p)->(%d %p %x)\n", This
, uriProp
, pcchProperty
, dwFlags
);
3250 return E_INVALIDARG
;
3252 /* Microsoft's implementation for the ZONE property of a URI seems to be lacking...
3253 * From what I can tell, instead of checking which URLZONE the URI belongs to it
3254 * simply assigns URLZONE_INVALID and returns E_NOTIMPL. This also applies to the GetZone
3257 if(uriProp
== Uri_PROPERTY_ZONE
) {
3258 *pcchProperty
= URLZONE_INVALID
;
3262 if(uriProp
< Uri_PROPERTY_DWORD_START
) {
3264 return E_INVALIDARG
;
3268 case Uri_PROPERTY_HOST_TYPE
:
3269 *pcchProperty
= This
->host_type
;
3272 case Uri_PROPERTY_PORT
:
3273 if(!This
->has_port
) {
3277 *pcchProperty
= This
->port
;
3282 case Uri_PROPERTY_SCHEME
:
3283 *pcchProperty
= This
->scheme_type
;
3287 FIXME("(%p)->(%d %p %x)\n", This
, uriProp
, pcchProperty
, dwFlags
);
3294 static HRESULT WINAPI
Uri_HasProperty(IUri
*iface
, Uri_PROPERTY uriProp
, BOOL
*pfHasProperty
)
3296 Uri
*This
= URI_THIS(iface
);
3297 FIXME("(%p)->(%d %p)\n", This
, uriProp
, pfHasProperty
);
3300 return E_INVALIDARG
;
3305 static HRESULT WINAPI
Uri_GetAbsoluteUri(IUri
*iface
, BSTR
*pstrAbsoluteUri
)
3307 Uri
*This
= URI_THIS(iface
);
3308 FIXME("(%p)->(%p)\n", This
, pstrAbsoluteUri
);
3310 if(!pstrAbsoluteUri
)
3316 static HRESULT WINAPI
Uri_GetAuthority(IUri
*iface
, BSTR
*pstrAuthority
)
3318 TRACE("(%p)->(%p)\n", iface
, pstrAuthority
);
3319 return Uri_GetPropertyBSTR(iface
, Uri_PROPERTY_AUTHORITY
, pstrAuthority
, 0);
3322 static HRESULT WINAPI
Uri_GetDisplayUri(IUri
*iface
, BSTR
*pstrDisplayUri
)
3324 Uri
*This
= URI_THIS(iface
);
3325 FIXME("(%p)->(%p)\n", This
, pstrDisplayUri
);
3333 static HRESULT WINAPI
Uri_GetDomain(IUri
*iface
, BSTR
*pstrDomain
)
3335 TRACE("(%p)->(%p)\n", iface
, pstrDomain
);
3336 return Uri_GetPropertyBSTR(iface
, Uri_PROPERTY_DOMAIN
, pstrDomain
, 0);
3339 static HRESULT WINAPI
Uri_GetExtension(IUri
*iface
, BSTR
*pstrExtension
)
3341 TRACE("(%p)->(%p)\n", iface
, pstrExtension
);
3342 return Uri_GetPropertyBSTR(iface
, Uri_PROPERTY_EXTENSION
, pstrExtension
, 0);
3345 static HRESULT WINAPI
Uri_GetFragment(IUri
*iface
, BSTR
*pstrFragment
)
3347 Uri
*This
= URI_THIS(iface
);
3348 FIXME("(%p)->(%p)\n", This
, pstrFragment
);
3356 static HRESULT WINAPI
Uri_GetHost(IUri
*iface
, BSTR
*pstrHost
)
3358 TRACE("(%p)->(%p)\n", iface
, pstrHost
);
3359 return Uri_GetPropertyBSTR(iface
, Uri_PROPERTY_HOST
, pstrHost
, 0);
3362 static HRESULT WINAPI
Uri_GetPassword(IUri
*iface
, BSTR
*pstrPassword
)
3364 TRACE("(%p)->(%p)\n", iface
, pstrPassword
);
3365 return Uri_GetPropertyBSTR(iface
, Uri_PROPERTY_PASSWORD
, pstrPassword
, 0);
3368 static HRESULT WINAPI
Uri_GetPath(IUri
*iface
, BSTR
*pstrPath
)
3370 TRACE("(%p)->(%p)\n", iface
, pstrPath
);
3371 return Uri_GetPropertyBSTR(iface
, Uri_PROPERTY_PATH
, pstrPath
, 0);
3374 static HRESULT WINAPI
Uri_GetPathAndQuery(IUri
*iface
, BSTR
*pstrPathAndQuery
)
3376 TRACE("(%p)->(%p)\n", iface
, pstrPathAndQuery
);
3377 return Uri_GetPropertyBSTR(iface
, Uri_PROPERTY_PATH_AND_QUERY
, pstrPathAndQuery
, 0);
3380 static HRESULT WINAPI
Uri_GetQuery(IUri
*iface
, BSTR
*pstrQuery
)
3382 TRACE("(%p)->(%p)\n", iface
, pstrQuery
);
3383 return Uri_GetPropertyBSTR(iface
, Uri_PROPERTY_QUERY
, pstrQuery
, 0);
3386 static HRESULT WINAPI
Uri_GetRawUri(IUri
*iface
, BSTR
*pstrRawUri
)
3388 Uri
*This
= URI_THIS(iface
);
3389 TRACE("(%p)->(%p)\n", This
, pstrRawUri
);
3391 /* Just forward the call to GetPropertyBSTR. */
3392 return Uri_GetPropertyBSTR(iface
, Uri_PROPERTY_RAW_URI
, pstrRawUri
, 0);
3395 static HRESULT WINAPI
Uri_GetSchemeName(IUri
*iface
, BSTR
*pstrSchemeName
)
3397 Uri
*This
= URI_THIS(iface
);
3398 TRACE("(%p)->(%p)\n", This
, pstrSchemeName
);
3399 return Uri_GetPropertyBSTR(iface
, Uri_PROPERTY_SCHEME_NAME
, pstrSchemeName
, 0);
3402 static HRESULT WINAPI
Uri_GetUserInfo(IUri
*iface
, BSTR
*pstrUserInfo
)
3404 TRACE("(%p)->(%p)\n", iface
, pstrUserInfo
);
3405 return Uri_GetPropertyBSTR(iface
, Uri_PROPERTY_USER_INFO
, pstrUserInfo
, 0);
3408 static HRESULT WINAPI
Uri_GetUserName(IUri
*iface
, BSTR
*pstrUserName
)
3410 TRACE("(%p)->(%p)\n", iface
, pstrUserName
);
3411 return Uri_GetPropertyBSTR(iface
, Uri_PROPERTY_USER_NAME
, pstrUserName
, 0);
3414 static HRESULT WINAPI
Uri_GetHostType(IUri
*iface
, DWORD
*pdwHostType
)
3416 TRACE("(%p)->(%p)\n", iface
, pdwHostType
);
3417 return Uri_GetPropertyDWORD(iface
, Uri_PROPERTY_HOST_TYPE
, pdwHostType
, 0);
3420 static HRESULT WINAPI
Uri_GetPort(IUri
*iface
, DWORD
*pdwPort
)
3422 TRACE("(%p)->(%p)\n", iface
, pdwPort
);
3423 return Uri_GetPropertyDWORD(iface
, Uri_PROPERTY_PORT
, pdwPort
, 0);
3426 static HRESULT WINAPI
Uri_GetScheme(IUri
*iface
, DWORD
*pdwScheme
)
3428 Uri
*This
= URI_THIS(iface
);
3429 TRACE("(%p)->(%p)\n", This
, pdwScheme
);
3430 return Uri_GetPropertyDWORD(iface
, Uri_PROPERTY_SCHEME
, pdwScheme
, 0);
3433 static HRESULT WINAPI
Uri_GetZone(IUri
*iface
, DWORD
*pdwZone
)
3435 TRACE("(%p)->(%p)\n", iface
, pdwZone
);
3436 return Uri_GetPropertyDWORD(iface
, Uri_PROPERTY_ZONE
,pdwZone
, 0);
3439 static HRESULT WINAPI
Uri_GetProperties(IUri
*iface
, DWORD
*pdwProperties
)
3441 Uri
*This
= URI_THIS(iface
);
3442 FIXME("(%p)->(%p)\n", This
, pdwProperties
);
3445 return E_INVALIDARG
;
3450 static HRESULT WINAPI
Uri_IsEqual(IUri
*iface
, IUri
*pUri
, BOOL
*pfEqual
)
3452 Uri
*This
= URI_THIS(iface
);
3453 TRACE("(%p)->(%p %p)\n", This
, pUri
, pfEqual
);
3461 /* For some reason Windows returns S_OK here... */
3465 FIXME("(%p)->(%p %p)\n", This
, pUri
, pfEqual
);
3471 static const IUriVtbl UriVtbl
= {
3475 Uri_GetPropertyBSTR
,
3476 Uri_GetPropertyLength
,
3477 Uri_GetPropertyDWORD
,
3488 Uri_GetPathAndQuery
,
3502 /***********************************************************************
3503 * CreateUri (urlmon.@)
3505 HRESULT WINAPI
CreateUri(LPCWSTR pwzURI
, DWORD dwFlags
, DWORD_PTR dwReserved
, IUri
**ppURI
)
3511 TRACE("(%s %x %x %p)\n", debugstr_w(pwzURI
), dwFlags
, (DWORD
)dwReserved
, ppURI
);
3514 return E_INVALIDARG
;
3518 return E_INVALIDARG
;
3521 ret
= heap_alloc(sizeof(Uri
));
3523 return E_OUTOFMEMORY
;
3525 ret
->lpIUriVtbl
= &UriVtbl
;
3528 /* Create a copy of pwzURI and store it as the raw_uri. */
3529 ret
->raw_uri
= SysAllocString(pwzURI
);
3532 return E_OUTOFMEMORY
;
3535 memset(&data
, 0, sizeof(parse_data
));
3536 data
.uri
= ret
->raw_uri
;
3538 /* Validate and parse the URI into it's components. */
3539 if(!parse_uri(&data
, dwFlags
)) {
3540 /* Encountered an unsupported or invalid URI */
3541 SysFreeString(ret
->raw_uri
);
3544 return E_INVALIDARG
;
3547 /* Canonicalize the URI. */
3548 hr
= canonicalize_uri(&data
, ret
, dwFlags
);
3550 SysFreeString(ret
->raw_uri
);
3560 #define URIBUILDER_THIS(iface) DEFINE_THIS(UriBuilder, IUriBuilder, iface)
3562 static HRESULT WINAPI
UriBuilder_QueryInterface(IUriBuilder
*iface
, REFIID riid
, void **ppv
)
3564 UriBuilder
*This
= URIBUILDER_THIS(iface
);
3566 if(IsEqualGUID(&IID_IUnknown
, riid
)) {
3567 TRACE("(%p)->(IID_IUnknown %p)\n", This
, ppv
);
3568 *ppv
= URIBUILDER(This
);
3569 }else if(IsEqualGUID(&IID_IUriBuilder
, riid
)) {
3570 TRACE("(%p)->(IID_IUri %p)\n", This
, ppv
);
3571 *ppv
= URIBUILDER(This
);
3573 TRACE("(%p)->(%s %p)\n", This
, debugstr_guid(riid
), ppv
);
3575 return E_NOINTERFACE
;
3578 IUnknown_AddRef((IUnknown
*)*ppv
);
3582 static ULONG WINAPI
UriBuilder_AddRef(IUriBuilder
*iface
)
3584 UriBuilder
*This
= URIBUILDER_THIS(iface
);
3585 LONG ref
= InterlockedIncrement(&This
->ref
);
3587 TRACE("(%p) ref=%d\n", This
, ref
);
3592 static ULONG WINAPI
UriBuilder_Release(IUriBuilder
*iface
)
3594 UriBuilder
*This
= URIBUILDER_THIS(iface
);
3595 LONG ref
= InterlockedDecrement(&This
->ref
);
3597 TRACE("(%p) ref=%d\n", This
, ref
);
3605 static HRESULT WINAPI
UriBuilder_CreateUriSimple(IUriBuilder
*iface
,
3606 DWORD dwAllowEncodingPropertyMask
,
3607 DWORD_PTR dwReserved
,
3610 UriBuilder
*This
= URIBUILDER_THIS(iface
);
3611 FIXME("(%p)->(%d %d %p)\n", This
, dwAllowEncodingPropertyMask
, (DWORD
)dwReserved
, ppIUri
);
3615 static HRESULT WINAPI
UriBuilder_CreateUri(IUriBuilder
*iface
,
3616 DWORD dwCreateFlags
,
3617 DWORD dwAllowEncodingPropertyMask
,
3618 DWORD_PTR dwReserved
,
3621 UriBuilder
*This
= URIBUILDER_THIS(iface
);
3622 FIXME("(%p)->(0x%08x %d %d %p)\n", This
, dwCreateFlags
, dwAllowEncodingPropertyMask
, (DWORD
)dwReserved
, ppIUri
);
3626 static HRESULT WINAPI
UriBuilder_CreateUriWithFlags(IUriBuilder
*iface
,
3627 DWORD dwCreateFlags
,
3628 DWORD dwUriBuilderFlags
,
3629 DWORD dwAllowEncodingPropertyMask
,
3630 DWORD_PTR dwReserved
,
3633 UriBuilder
*This
= URIBUILDER_THIS(iface
);
3634 FIXME("(%p)->(0x%08x 0x%08x %d %d %p)\n", This
, dwCreateFlags
, dwUriBuilderFlags
,
3635 dwAllowEncodingPropertyMask
, (DWORD
)dwReserved
, ppIUri
);
3639 static HRESULT WINAPI
UriBuilder_GetIUri(IUriBuilder
*iface
, IUri
**ppIUri
)
3641 UriBuilder
*This
= URIBUILDER_THIS(iface
);
3642 FIXME("(%p)->(%p)\n", This
, ppIUri
);
3646 static HRESULT WINAPI
UriBuilder_SetIUri(IUriBuilder
*iface
, IUri
*pIUri
)
3648 UriBuilder
*This
= URIBUILDER_THIS(iface
);
3649 FIXME("(%p)->(%p)\n", This
, pIUri
);
3653 static HRESULT WINAPI
UriBuilder_GetFragment(IUriBuilder
*iface
, DWORD
*pcchFragment
, LPCWSTR
*ppwzFragment
)
3655 UriBuilder
*This
= URIBUILDER_THIS(iface
);
3656 FIXME("(%p)->(%p %p)\n", This
, pcchFragment
, ppwzFragment
);
3660 static HRESULT WINAPI
UriBuilder_GetHost(IUriBuilder
*iface
, DWORD
*pcchHost
, LPCWSTR
*ppwzHost
)
3662 UriBuilder
*This
= URIBUILDER_THIS(iface
);
3663 FIXME("(%p)->(%p %p)\n", This
, pcchHost
, ppwzHost
);
3667 static HRESULT WINAPI
UriBuilder_GetPassword(IUriBuilder
*iface
, DWORD
*pcchPassword
, LPCWSTR
*ppwzPassword
)
3669 UriBuilder
*This
= URIBUILDER_THIS(iface
);
3670 FIXME("(%p)->(%p %p)\n", This
, pcchPassword
, ppwzPassword
);
3674 static HRESULT WINAPI
UriBuilder_GetPath(IUriBuilder
*iface
, DWORD
*pcchPath
, LPCWSTR
*ppwzPath
)
3676 UriBuilder
*This
= URIBUILDER_THIS(iface
);
3677 FIXME("(%p)->(%p %p)\n", This
, pcchPath
, ppwzPath
);
3681 static HRESULT WINAPI
UriBuilder_GetPort(IUriBuilder
*iface
, BOOL
*pfHasPort
, DWORD
*pdwPort
)
3683 UriBuilder
*This
= URIBUILDER_THIS(iface
);
3684 FIXME("(%p)->(%p %p)\n", This
, pfHasPort
, pdwPort
);
3688 static HRESULT WINAPI
UriBuilder_GetQuery(IUriBuilder
*iface
, DWORD
*pcchQuery
, LPCWSTR
*ppwzQuery
)
3690 UriBuilder
*This
= URIBUILDER_THIS(iface
);
3691 FIXME("(%p)->(%p %p)\n", This
, pcchQuery
, ppwzQuery
);
3695 static HRESULT WINAPI
UriBuilder_GetSchemeName(IUriBuilder
*iface
, DWORD
*pcchSchemeName
, LPCWSTR
*ppwzSchemeName
)
3697 UriBuilder
*This
= URIBUILDER_THIS(iface
);
3698 FIXME("(%p)->(%p %p)\n", This
, pcchSchemeName
, ppwzSchemeName
);
3702 static HRESULT WINAPI
UriBuilder_GetUserName(IUriBuilder
*iface
, DWORD
*pcchUserName
, LPCWSTR
*ppwzUserName
)
3704 UriBuilder
*This
= URIBUILDER_THIS(iface
);
3705 FIXME("(%p)->(%p %p)\n", This
, pcchUserName
, ppwzUserName
);
3709 static HRESULT WINAPI
UriBuilder_SetFragment(IUriBuilder
*iface
, LPCWSTR pwzNewValue
)
3711 UriBuilder
*This
= URIBUILDER_THIS(iface
);
3712 FIXME("(%p)->(%s)\n", This
, debugstr_w(pwzNewValue
));
3716 static HRESULT WINAPI
UriBuilder_SetHost(IUriBuilder
*iface
, LPCWSTR pwzNewValue
)
3718 UriBuilder
*This
= URIBUILDER_THIS(iface
);
3719 FIXME("(%p)->(%s)\n", This
, debugstr_w(pwzNewValue
));
3723 static HRESULT WINAPI
UriBuilder_SetPassword(IUriBuilder
*iface
, LPCWSTR pwzNewValue
)
3725 UriBuilder
*This
= URIBUILDER_THIS(iface
);
3726 FIXME("(%p)->(%s)\n", This
, debugstr_w(pwzNewValue
));
3730 static HRESULT WINAPI
UriBuilder_SetPath(IUriBuilder
*iface
, LPCWSTR pwzNewValue
)
3732 UriBuilder
*This
= URIBUILDER_THIS(iface
);
3733 FIXME("(%p)->(%s)\n", This
, debugstr_w(pwzNewValue
));
3737 static HRESULT WINAPI
UriBuilder_SetPort(IUriBuilder
*iface
, BOOL fHasPort
, DWORD dwNewValue
)
3739 UriBuilder
*This
= URIBUILDER_THIS(iface
);
3740 FIXME("(%p)->(%d %d)\n", This
, fHasPort
, dwNewValue
);
3744 static HRESULT WINAPI
UriBuilder_SetQuery(IUriBuilder
*iface
, LPCWSTR pwzNewValue
)
3746 UriBuilder
*This
= URIBUILDER_THIS(iface
);
3747 FIXME("(%p)->(%s)\n", This
, debugstr_w(pwzNewValue
));
3751 static HRESULT WINAPI
UriBuilder_SetSchemeName(IUriBuilder
*iface
, LPCWSTR pwzNewValue
)
3753 UriBuilder
*This
= URIBUILDER_THIS(iface
);
3754 FIXME("(%p)->(%s)\n", This
, debugstr_w(pwzNewValue
));
3758 static HRESULT WINAPI
UriBuilder_SetUserName(IUriBuilder
*iface
, LPCWSTR pwzNewValue
)
3760 UriBuilder
*This
= URIBUILDER_THIS(iface
);
3761 FIXME("(%p)->(%s)\n", This
, debugstr_w(pwzNewValue
));
3765 static HRESULT WINAPI
UriBuilder_RemoveProperties(IUriBuilder
*iface
, DWORD dwPropertyMask
)
3767 UriBuilder
*This
= URIBUILDER_THIS(iface
);
3768 FIXME("(%p)->(0x%08x)\n", This
, dwPropertyMask
);
3772 static HRESULT WINAPI
UriBuilder_HasBeenModified(IUriBuilder
*iface
, BOOL
*pfModified
)
3774 UriBuilder
*This
= URIBUILDER_THIS(iface
);
3775 FIXME("(%p)->(%p)\n", This
, pfModified
);
3779 #undef URIBUILDER_THIS
3781 static const IUriBuilderVtbl UriBuilderVtbl
= {
3782 UriBuilder_QueryInterface
,
3785 UriBuilder_CreateUriSimple
,
3786 UriBuilder_CreateUri
,
3787 UriBuilder_CreateUriWithFlags
,
3790 UriBuilder_GetFragment
,
3792 UriBuilder_GetPassword
,
3795 UriBuilder_GetQuery
,
3796 UriBuilder_GetSchemeName
,
3797 UriBuilder_GetUserName
,
3798 UriBuilder_SetFragment
,
3800 UriBuilder_SetPassword
,
3803 UriBuilder_SetQuery
,
3804 UriBuilder_SetSchemeName
,
3805 UriBuilder_SetUserName
,
3806 UriBuilder_RemoveProperties
,
3807 UriBuilder_HasBeenModified
,
3810 /***********************************************************************
3811 * CreateIUriBuilder (urlmon.@)
3813 HRESULT WINAPI
CreateIUriBuilder(IUri
*pIUri
, DWORD dwFlags
, DWORD_PTR dwReserved
, IUriBuilder
**ppIUriBuilder
)
3817 TRACE("(%p %x %x %p)\n", pIUri
, dwFlags
, (DWORD
)dwReserved
, ppIUriBuilder
);
3819 ret
= heap_alloc(sizeof(UriBuilder
));
3821 return E_OUTOFMEMORY
;
3823 ret
->lpIUriBuilderVtbl
= &UriBuilderVtbl
;
3826 *ppIUriBuilder
= URIBUILDER(ret
);