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"
24 #define URI_DISPLAY_NO_ABSOLUTE_URI 0x1
25 #define URI_DISPLAY_NO_DEFAULT_PORT_AUTH 0x2
27 #define ALLOW_NULL_TERM_SCHEME 0x01
28 #define ALLOW_NULL_TERM_USER_NAME 0x02
29 #define ALLOW_NULL_TERM_PASSWORD 0x04
30 #define ALLOW_BRACKETLESS_IP_LITERAL 0x08
31 #define SKIP_IP_FUTURE_CHECK 0x10
32 #define IGNORE_PORT_DELIMITER 0x20
34 #define RAW_URI_FORCE_PORT_DISP 0x1
35 #define RAW_URI_CONVERT_TO_DOS_PATH 0x2
37 #define COMBINE_URI_FORCE_FLAG_USE 0x1
39 static const IID IID_IUriObj
= {0x4b364760,0x9f51,0x11df,{0x98,0x1c,0x08,0x00,0x20,0x0c,0x9a,0x66}};
43 IUriBuilderFactory IUriBuilderFactory_iface
;
44 IPersistStream IPersistStream_iface
;
45 IMarshal IMarshal_iface
;
51 /* Information about the canonicalized URI's buffer. */
55 BOOL display_modifiers
;
60 URL_SCHEME scheme_type
;
68 Uri_HOST_TYPE host_type
;
91 IUriBuilder IUriBuilder_iface
;
128 /* IPv6 addresses can hold up to 8 h16 components. */
132 /* An IPv6 can have 1 elision ("::"). */
133 const WCHAR
*elision
;
135 /* An IPv6 can contain 1 IPv4 address as the last 32bits of the address. */
148 BOOL has_implicit_scheme
;
149 BOOL has_implicit_ip
;
155 URL_SCHEME scheme_type
;
157 const WCHAR
*username
;
160 const WCHAR
*password
;
165 Uri_HOST_TYPE host_type
;
168 ipv6_address ipv6_address
;
181 const WCHAR
*fragment
;
185 static const CHAR hexDigits
[] = "0123456789ABCDEF";
187 /* List of scheme types/scheme names that are recognized by the IUri interface as of IE 7. */
188 static const struct {
190 WCHAR scheme_name
[16];
191 } recognized_schemes
[] = {
192 {URL_SCHEME_FTP
, {'f','t','p',0}},
193 {URL_SCHEME_HTTP
, {'h','t','t','p',0}},
194 {URL_SCHEME_GOPHER
, {'g','o','p','h','e','r',0}},
195 {URL_SCHEME_MAILTO
, {'m','a','i','l','t','o',0}},
196 {URL_SCHEME_NEWS
, {'n','e','w','s',0}},
197 {URL_SCHEME_NNTP
, {'n','n','t','p',0}},
198 {URL_SCHEME_TELNET
, {'t','e','l','n','e','t',0}},
199 {URL_SCHEME_WAIS
, {'w','a','i','s',0}},
200 {URL_SCHEME_FILE
, {'f','i','l','e',0}},
201 {URL_SCHEME_MK
, {'m','k',0}},
202 {URL_SCHEME_HTTPS
, {'h','t','t','p','s',0}},
203 {URL_SCHEME_SHELL
, {'s','h','e','l','l',0}},
204 {URL_SCHEME_SNEWS
, {'s','n','e','w','s',0}},
205 {URL_SCHEME_LOCAL
, {'l','o','c','a','l',0}},
206 {URL_SCHEME_JAVASCRIPT
, {'j','a','v','a','s','c','r','i','p','t',0}},
207 {URL_SCHEME_VBSCRIPT
, {'v','b','s','c','r','i','p','t',0}},
208 {URL_SCHEME_ABOUT
, {'a','b','o','u','t',0}},
209 {URL_SCHEME_RES
, {'r','e','s',0}},
210 {URL_SCHEME_MSSHELLROOTED
, {'m','s','-','s','h','e','l','l','-','r','o','o','t','e','d',0}},
211 {URL_SCHEME_MSSHELLIDLIST
, {'m','s','-','s','h','e','l','l','-','i','d','l','i','s','t',0}},
212 {URL_SCHEME_MSHELP
, {'h','c','p',0}},
213 {URL_SCHEME_WILDCARD
, {'*',0}}
216 /* List of default ports Windows recognizes. */
217 static const struct {
220 } default_ports
[] = {
221 {URL_SCHEME_FTP
, 21},
222 {URL_SCHEME_HTTP
, 80},
223 {URL_SCHEME_GOPHER
, 70},
224 {URL_SCHEME_NNTP
, 119},
225 {URL_SCHEME_TELNET
, 23},
226 {URL_SCHEME_WAIS
, 210},
227 {URL_SCHEME_HTTPS
, 443},
230 /* List of 3-character top level domain names Windows seems to recognize.
231 * There might be more, but, these are the only ones I've found so far.
233 static const struct {
235 } recognized_tlds
[] = {
245 static Uri
*get_uri_obj(IUri
*uri
)
250 hres
= IUri_QueryInterface(uri
, &IID_IUriObj
, (void**)&ret
);
251 return SUCCEEDED(hres
) ? ret
: NULL
;
254 static inline BOOL
is_alpha(WCHAR val
) {
255 return ((val
>= 'a' && val
<= 'z') || (val
>= 'A' && val
<= 'Z'));
258 static inline BOOL
is_num(WCHAR val
) {
259 return (val
>= '0' && val
<= '9');
262 static inline BOOL
is_drive_path(const WCHAR
*str
) {
263 return (is_alpha(str
[0]) && (str
[1] == ':' || str
[1] == '|'));
266 static inline BOOL
is_unc_path(const WCHAR
*str
) {
267 return (str
[0] == '\\' && str
[1] == '\\');
270 static inline BOOL
is_forbidden_dos_path_char(WCHAR val
) {
271 return (val
== '>' || val
== '<' || val
== '\"');
274 /* A URI is implicitly a file path if it begins with
275 * a drive letter (e.g. X:) or starts with "\\" (UNC path).
277 static inline BOOL
is_implicit_file_path(const WCHAR
*str
) {
278 return (is_unc_path(str
) || (is_alpha(str
[0]) && str
[1] == ':'));
281 /* Checks if the URI is a hierarchical URI. A hierarchical
282 * URI is one that has "//" after the scheme.
284 static BOOL
check_hierarchical(const WCHAR
**ptr
) {
285 const WCHAR
*start
= *ptr
;
300 /* unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~" */
301 static inline BOOL
is_unreserved(WCHAR val
) {
302 return (is_alpha(val
) || is_num(val
) || val
== '-' || val
== '.' ||
303 val
== '_' || val
== '~');
306 /* sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
307 * / "*" / "+" / "," / ";" / "="
309 static inline BOOL
is_subdelim(WCHAR val
) {
310 return (val
== '!' || val
== '$' || val
== '&' ||
311 val
== '\'' || val
== '(' || val
== ')' ||
312 val
== '*' || val
== '+' || val
== ',' ||
313 val
== ';' || val
== '=');
316 /* gen-delims = ":" / "/" / "?" / "#" / "[" / "]" / "@" */
317 static inline BOOL
is_gendelim(WCHAR val
) {
318 return (val
== ':' || val
== '/' || val
== '?' ||
319 val
== '#' || val
== '[' || val
== ']' ||
323 /* Characters that delimit the end of the authority
324 * section of a URI. Sometimes a '\\' is considered
325 * an authority delimiter.
327 static inline BOOL
is_auth_delim(WCHAR val
, BOOL acceptSlash
) {
328 return (val
== '#' || val
== '/' || val
== '?' ||
329 val
== '\0' || (acceptSlash
&& val
== '\\'));
332 /* reserved = gen-delims / sub-delims */
333 static inline BOOL
is_reserved(WCHAR val
) {
334 return (is_subdelim(val
) || is_gendelim(val
));
337 static inline BOOL
is_hexdigit(WCHAR val
) {
338 return ((val
>= 'a' && val
<= 'f') ||
339 (val
>= 'A' && val
<= 'F') ||
340 (val
>= '0' && val
<= '9'));
343 static inline BOOL
is_path_delim(URL_SCHEME scheme
, WCHAR val
) {
344 return (!val
|| (val
== '#' && scheme
!= URL_SCHEME_FILE
) || val
== '?');
347 static inline BOOL
is_slash(WCHAR c
)
349 return c
== '/' || c
== '\\';
352 static BOOL
is_default_port(URL_SCHEME scheme
, DWORD port
) {
355 for(i
= 0; i
< sizeof(default_ports
)/sizeof(default_ports
[0]); ++i
) {
356 if(default_ports
[i
].scheme
== scheme
&& default_ports
[i
].port
)
363 /* List of schemes types Windows seems to expect to be hierarchical. */
364 static inline BOOL
is_hierarchical_scheme(URL_SCHEME type
) {
365 return(type
== URL_SCHEME_HTTP
|| type
== URL_SCHEME_FTP
||
366 type
== URL_SCHEME_GOPHER
|| type
== URL_SCHEME_NNTP
||
367 type
== URL_SCHEME_TELNET
|| type
== URL_SCHEME_WAIS
||
368 type
== URL_SCHEME_FILE
|| type
== URL_SCHEME_HTTPS
||
369 type
== URL_SCHEME_RES
);
372 /* Checks if 'flags' contains an invalid combination of Uri_CREATE flags. */
373 static inline BOOL
has_invalid_flag_combination(DWORD flags
) {
374 return((flags
& Uri_CREATE_DECODE_EXTRA_INFO
&& flags
& Uri_CREATE_NO_DECODE_EXTRA_INFO
) ||
375 (flags
& Uri_CREATE_CANONICALIZE
&& flags
& Uri_CREATE_NO_CANONICALIZE
) ||
376 (flags
& Uri_CREATE_CRACK_UNKNOWN_SCHEMES
&& flags
& Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES
) ||
377 (flags
& Uri_CREATE_PRE_PROCESS_HTML_URI
&& flags
& Uri_CREATE_NO_PRE_PROCESS_HTML_URI
) ||
378 (flags
& Uri_CREATE_IE_SETTINGS
&& flags
& Uri_CREATE_NO_IE_SETTINGS
));
381 /* Applies each default Uri_CREATE flags to 'flags' if it
382 * doesn't cause a flag conflict.
384 static void apply_default_flags(DWORD
*flags
) {
385 if(!(*flags
& Uri_CREATE_NO_CANONICALIZE
))
386 *flags
|= Uri_CREATE_CANONICALIZE
;
387 if(!(*flags
& Uri_CREATE_NO_DECODE_EXTRA_INFO
))
388 *flags
|= Uri_CREATE_DECODE_EXTRA_INFO
;
389 if(!(*flags
& Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES
))
390 *flags
|= Uri_CREATE_CRACK_UNKNOWN_SCHEMES
;
391 if(!(*flags
& Uri_CREATE_NO_PRE_PROCESS_HTML_URI
))
392 *flags
|= Uri_CREATE_PRE_PROCESS_HTML_URI
;
393 if(!(*flags
& Uri_CREATE_IE_SETTINGS
))
394 *flags
|= Uri_CREATE_NO_IE_SETTINGS
;
397 /* Determines if the URI is hierarchical using the information already parsed into
398 * data and using the current location of parsing in the URI string.
400 * Windows considers a URI hierarchical if one of the following is true:
401 * A.) It's a wildcard scheme.
402 * B.) It's an implicit file scheme.
403 * C.) It's a known hierarchical scheme and it has two '\\' after the scheme name.
404 * (the '\\' will be converted into "//" during canonicalization).
405 * D.) "//" appears after the scheme name (or at the beginning if no scheme is given).
407 static inline BOOL
is_hierarchical_uri(const WCHAR
**ptr
, const parse_data
*data
) {
408 const WCHAR
*start
= *ptr
;
410 if(data
->scheme_type
== URL_SCHEME_WILDCARD
)
412 else if(data
->scheme_type
== URL_SCHEME_FILE
&& data
->has_implicit_scheme
)
414 else if(is_hierarchical_scheme(data
->scheme_type
) && (*ptr
)[0] == '\\' && (*ptr
)[1] == '\\') {
417 } else if(data
->scheme_type
!= URL_SCHEME_MAILTO
&& check_hierarchical(ptr
))
424 /* Computes the size of the given IPv6 address.
425 * Each h16 component is 16 bits. If there is an IPv4 address, it's
426 * 32 bits. If there's an elision it can be 16 to 128 bits, depending
427 * on the number of other components.
429 * Modeled after google-url's CheckIPv6ComponentsSize function
431 static void compute_ipv6_comps_size(ipv6_address
*address
) {
432 address
->components_size
= address
->h16_count
* 2;
435 /* IPv4 address is 4 bytes. */
436 address
->components_size
+= 4;
438 if(address
->elision
) {
439 /* An elision can be anywhere from 2 bytes up to 16 bytes.
440 * Its size depends on the size of the h16 and IPv4 components.
442 address
->elision_size
= 16 - address
->components_size
;
443 if(address
->elision_size
< 2)
444 address
->elision_size
= 2;
446 address
->elision_size
= 0;
449 /* Taken from dlls/jscript/lex.c */
450 static int hex_to_int(WCHAR val
) {
451 if(val
>= '0' && val
<= '9')
453 else if(val
>= 'a' && val
<= 'f')
454 return val
- 'a' + 10;
455 else if(val
>= 'A' && val
<= 'F')
456 return val
- 'A' + 10;
461 /* Helper function for converting a percent encoded string
462 * representation of a WCHAR value into its actual WCHAR value. If
463 * the two characters following the '%' aren't valid hex values then
464 * this function returns the NULL character.
467 * "%2E" will result in '.' being returned by this function.
469 static WCHAR
decode_pct_val(const WCHAR
*ptr
) {
472 if(*ptr
== '%' && is_hexdigit(*(ptr
+ 1)) && is_hexdigit(*(ptr
+ 2))) {
473 INT a
= hex_to_int(*(ptr
+ 1));
474 INT b
= hex_to_int(*(ptr
+ 2));
483 /* Helper function for percent encoding a given character
484 * and storing the encoded value into a given buffer (dest).
486 * It's up to the calling function to ensure that there is
487 * at least enough space in 'dest' for the percent encoded
488 * value to be stored (so dest + 3 spaces available).
490 static inline void pct_encode_val(WCHAR val
, WCHAR
*dest
) {
492 dest
[1] = hexDigits
[(val
>> 4) & 0xf];
493 dest
[2] = hexDigits
[val
& 0xf];
496 /* Attempts to parse the domain name from the host.
498 * This function also includes the Top-level Domain (TLD) name
499 * of the host when it tries to find the domain name. If it finds
500 * a valid domain name it will assign 'domain_start' the offset
501 * into 'host' where the domain name starts.
503 * It's implied that if there is a domain name its range is:
504 * [host+domain_start, host+host_len).
506 void find_domain_name(const WCHAR
*host
, DWORD host_len
,
508 const WCHAR
*last_tld
, *sec_last_tld
, *end
;
510 end
= host
+host_len
-1;
514 /* There has to be at least enough room for a '.' followed by a
515 * 3-character TLD for a domain to even exist in the host name.
520 last_tld
= memrchrW(host
, '.', host_len
);
522 /* http://hostname -> has no domain name. */
525 sec_last_tld
= memrchrW(host
, '.', last_tld
-host
);
527 /* If the '.' is at the beginning of the host there
528 * has to be at least 3 characters in the TLD for it
530 * Ex: .com -> .com as the domain name.
531 * .co -> has no domain name.
533 if(last_tld
-host
== 0) {
534 if(end
-(last_tld
-1) < 3)
536 } else if(last_tld
-host
== 3) {
539 /* If there are three characters in front of last_tld and
540 * they are on the list of recognized TLDs, then this
541 * host doesn't have a domain (since the host only contains
543 * Ex: edu.uk -> has no domain name.
544 * foo.uk -> foo.uk as the domain name.
546 for(i
= 0; i
< sizeof(recognized_tlds
)/sizeof(recognized_tlds
[0]); ++i
) {
547 if(!StrCmpNIW(host
, recognized_tlds
[i
].tld_name
, 3))
550 } else if(last_tld
-host
< 3)
551 /* Anything less than 3 characters is considered part
553 * Ex: ak.uk -> Has no domain name.
557 /* Otherwise the domain name is the whole host name. */
559 } else if(end
+1-last_tld
> 3) {
560 /* If the last_tld has more than 3 characters, then it's automatically
561 * considered the TLD of the domain name.
562 * Ex: www.winehq.org.uk.test -> uk.test as the domain name.
564 *domain_start
= (sec_last_tld
+1)-host
;
565 } else if(last_tld
- (sec_last_tld
+1) < 4) {
567 /* If the sec_last_tld is 3 characters long it HAS to be on the list of
568 * recognized to still be considered part of the TLD name, otherwise
569 * it's considered the domain name.
570 * Ex: www.google.com.uk -> google.com.uk as the domain name.
571 * www.google.foo.uk -> foo.uk as the domain name.
573 if(last_tld
- (sec_last_tld
+1) == 3) {
574 for(i
= 0; i
< sizeof(recognized_tlds
)/sizeof(recognized_tlds
[0]); ++i
) {
575 if(!StrCmpNIW(sec_last_tld
+1, recognized_tlds
[i
].tld_name
, 3)) {
576 const WCHAR
*domain
= memrchrW(host
, '.', sec_last_tld
-host
);
581 *domain_start
= (domain
+1) - host
;
582 TRACE("Found domain name %s\n", debugstr_wn(host
+*domain_start
,
583 (host
+host_len
)-(host
+*domain_start
)));
588 *domain_start
= (sec_last_tld
+1)-host
;
590 /* Since the sec_last_tld is less than 3 characters it's considered
592 * Ex: www.google.fo.uk -> google.fo.uk as the domain name.
594 const WCHAR
*domain
= memrchrW(host
, '.', sec_last_tld
-host
);
599 *domain_start
= (domain
+1) - host
;
602 /* The second to last TLD has more than 3 characters making it
604 * Ex: www.google.test.us -> test.us as the domain name.
606 *domain_start
= (sec_last_tld
+1)-host
;
609 TRACE("Found domain name %s\n", debugstr_wn(host
+*domain_start
,
610 (host
+host_len
)-(host
+*domain_start
)));
613 /* Removes the dot segments from a hierarchical URIs path component. This
614 * function performs the removal in place.
616 * This function returns the new length of the path string.
618 static DWORD
remove_dot_segments(WCHAR
*path
, DWORD path_len
) {
620 const WCHAR
*in
= out
;
621 const WCHAR
*end
= out
+ path_len
;
625 /* Move the first path segment in the input buffer to the end of
626 * the output buffer, and any subsequent characters up to, including
627 * the next "/" character (if any) or the end of the input buffer.
629 while(in
< end
&& !is_slash(*in
))
639 /* Handle ending "/." */
646 if(is_slash(in
[1])) {
651 /* If we don't have "/../" or ending "/.." */
652 if(in
[1] != '.' || (in
+ 2 != end
&& !is_slash(in
[2])))
655 /* Find the slash preceding out pointer and move out pointer to it */
656 if(out
> path
+1 && is_slash(*--out
))
658 while(out
> path
&& !is_slash(*(--out
)));
668 TRACE("(%p %d): Path after dot segments removed %s len=%d\n", path
, path_len
,
669 debugstr_wn(path
, len
), len
);
673 /* Attempts to find the file extension in a given path. */
674 static INT
find_file_extension(const WCHAR
*path
, DWORD path_len
) {
677 for(end
= path
+path_len
-1; end
>= path
&& *end
!= '/' && *end
!= '\\'; --end
) {
685 /* Computes the location where the elision should occur in the IPv6
686 * address using the numerical values of each component stored in
687 * 'values'. If the address shouldn't contain an elision then 'index'
688 * is assigned -1 as its value. Otherwise 'index' will contain the
689 * starting index (into values) where the elision should be, and 'count'
690 * will contain the number of cells the elision covers.
693 * Windows will expand an elision if the elision only represents one h16
694 * component of the address.
696 * Ex: [1::2:3:4:5:6:7] -> [1:0:2:3:4:5:6:7]
698 * If the IPv6 address contains an IPv4 address, the IPv4 address is also
699 * considered for being included as part of an elision if all its components
702 * Ex: [1:2:3:4:5:6:0.0.0.0] -> [1:2:3:4:5:6::]
704 static void compute_elision_location(const ipv6_address
*address
, const USHORT values
[8],
705 INT
*index
, DWORD
*count
) {
706 DWORD i
, max_len
, cur_len
;
707 INT max_index
, cur_index
;
709 max_len
= cur_len
= 0;
710 max_index
= cur_index
= -1;
711 for(i
= 0; i
< 8; ++i
) {
712 BOOL check_ipv4
= (address
->ipv4
&& i
== 6);
713 BOOL is_end
= (check_ipv4
|| i
== 7);
716 /* Check if the IPv4 address contains only zeros. */
717 if(values
[i
] == 0 && values
[i
+1] == 0) {
724 } else if(values
[i
] == 0) {
731 if(is_end
|| values
[i
] != 0) {
732 /* We only consider it for an elision if it's
733 * more than 1 component long.
735 if(cur_len
> 1 && cur_len
> max_len
) {
736 /* Found the new elision location. */
738 max_index
= cur_index
;
741 /* Reset the current range for the next range of zeros. */
751 /* Removes all the leading and trailing white spaces or
752 * control characters from the URI and removes all control
753 * characters inside of the URI string.
755 static BSTR
pre_process_uri(LPCWSTR uri
) {
756 const WCHAR
*start
, *end
, *ptr
;
762 /* Skip leading controls and whitespace. */
763 while(*start
&& (iscntrlW(*start
) || isspaceW(*start
))) ++start
;
765 /* URI consisted only of control/whitespace. */
767 return SysAllocStringLen(NULL
, 0);
769 end
= start
+ strlenW(start
);
770 while(--end
> start
&& (iscntrlW(*end
) || isspaceW(*end
)));
773 for(ptr
= start
; ptr
< end
; ptr
++) {
778 ret
= SysAllocStringLen(NULL
, len
);
782 for(ptr
= start
, ptr2
=ret
; ptr
< end
; ptr
++) {
790 /* Converts the specified IPv4 address into an uint value.
792 * This function assumes that the IPv4 address has already been validated.
794 static UINT
ipv4toui(const WCHAR
*ip
, DWORD len
) {
796 DWORD comp_value
= 0;
799 for(ptr
= ip
; ptr
< ip
+len
; ++ptr
) {
805 comp_value
= comp_value
*10 + (*ptr
-'0');
814 /* Converts an IPv4 address in numerical form into its fully qualified
815 * string form. This function returns the number of characters written
816 * to 'dest'. If 'dest' is NULL this function will return the number of
817 * characters that would have been written.
819 * It's up to the caller to ensure there's enough space in 'dest' for the
822 static DWORD
ui2ipv4(WCHAR
*dest
, UINT address
) {
823 static const WCHAR formatW
[] =
824 {'%','u','.','%','u','.','%','u','.','%','u',0};
828 digits
[0] = (address
>> 24) & 0xff;
829 digits
[1] = (address
>> 16) & 0xff;
830 digits
[2] = (address
>> 8) & 0xff;
831 digits
[3] = address
& 0xff;
835 ret
= sprintfW(tmp
, formatW
, digits
[0], digits
[1], digits
[2], digits
[3]);
837 ret
= sprintfW(dest
, formatW
, digits
[0], digits
[1], digits
[2], digits
[3]);
842 static DWORD
ui2str(WCHAR
*dest
, UINT value
) {
843 static const WCHAR formatW
[] = {'%','u',0};
848 ret
= sprintfW(tmp
, formatW
, value
);
850 ret
= sprintfW(dest
, formatW
, value
);
855 /* Converts a h16 component (from an IPv6 address) into its
858 * This function assumes that the h16 component has already been validated.
860 static USHORT
h16tous(h16 component
) {
864 for(i
= 0; i
< component
.len
; ++i
) {
866 ret
+= hex_to_int(component
.str
[i
]);
872 /* Converts an IPv6 address into its 128 bits (16 bytes) numerical value.
874 * This function assumes that the ipv6_address has already been validated.
876 static BOOL
ipv6_to_number(const ipv6_address
*address
, USHORT number
[8]) {
877 DWORD i
, cur_component
= 0;
878 BOOL already_passed_elision
= FALSE
;
880 for(i
= 0; i
< address
->h16_count
; ++i
) {
881 if(address
->elision
) {
882 if(address
->components
[i
].str
> address
->elision
&& !already_passed_elision
) {
883 /* Means we just passed the elision and need to add its values to
884 * 'number' before we do anything else.
887 for(j
= 0; j
< address
->elision_size
; j
+=2)
888 number
[cur_component
++] = 0;
890 already_passed_elision
= TRUE
;
894 number
[cur_component
++] = h16tous(address
->components
[i
]);
897 /* Case when the elision appears after the h16 components. */
898 if(!already_passed_elision
&& address
->elision
) {
900 for(j
= 0; j
< address
->elision_size
; j
+=2)
901 number
[cur_component
++] = 0;
905 UINT value
= ipv4toui(address
->ipv4
, address
->ipv4_len
);
907 if(cur_component
!= 6) {
908 ERR("(%p %p): Failed sanity check with %d\n", address
, number
, cur_component
);
912 number
[cur_component
++] = (value
>> 16) & 0xffff;
913 number
[cur_component
] = value
& 0xffff;
919 /* Checks if the characters pointed to by 'ptr' are
920 * a percent encoded data octet.
922 * pct-encoded = "%" HEXDIG HEXDIG
924 static BOOL
check_pct_encoded(const WCHAR
**ptr
) {
925 const WCHAR
*start
= *ptr
;
931 if(!is_hexdigit(**ptr
)) {
937 if(!is_hexdigit(**ptr
)) {
946 /* dec-octet = DIGIT ; 0-9
947 * / %x31-39 DIGIT ; 10-99
948 * / "1" 2DIGIT ; 100-199
949 * / "2" %x30-34 DIGIT ; 200-249
950 * / "25" %x30-35 ; 250-255
952 static BOOL
check_dec_octet(const WCHAR
**ptr
) {
953 const WCHAR
*c1
, *c2
, *c3
;
956 /* A dec-octet must be at least 1 digit long. */
957 if(*c1
< '0' || *c1
> '9')
963 /* Since the 1-digit requirement was met, it doesn't
964 * matter if this is a DIGIT value, it's considered a
967 if(*c2
< '0' || *c2
> '9')
973 /* Same explanation as above. */
974 if(*c3
< '0' || *c3
> '9')
977 /* Anything > 255 isn't a valid IP dec-octet. */
978 if(*c1
>= '2' && *c2
>= '5' && *c3
>= '5') {
987 /* Checks if there is an implicit IPv4 address in the host component of the URI.
988 * The max value of an implicit IPv4 address is UINT_MAX.
991 * "234567" would be considered an implicit IPv4 address.
993 static BOOL
check_implicit_ipv4(const WCHAR
**ptr
, UINT
*val
) {
994 const WCHAR
*start
= *ptr
;
998 while(is_num(**ptr
)) {
999 ret
= ret
*10 + (**ptr
- '0');
1001 if(ret
> UINT_MAX
) {
1015 /* Checks if the string contains an IPv4 address.
1017 * This function has a strict mode or a non-strict mode of operation
1018 * When 'strict' is set to FALSE this function will return TRUE if
1019 * the string contains at least 'dec-octet "." dec-octet' since partial
1020 * IPv4 addresses will be normalized out into full IPv4 addresses. When
1021 * 'strict' is set this function expects there to be a full IPv4 address.
1023 * IPv4address = dec-octet "." dec-octet "." dec-octet "." dec-octet
1025 static BOOL
check_ipv4address(const WCHAR
**ptr
, BOOL strict
) {
1026 const WCHAR
*start
= *ptr
;
1028 if(!check_dec_octet(ptr
)) {
1039 if(!check_dec_octet(ptr
)) {
1053 if(!check_dec_octet(ptr
)) {
1067 if(!check_dec_octet(ptr
)) {
1072 /* Found a four digit ip address. */
1075 /* Tries to parse the scheme name of the URI.
1077 * scheme = ALPHA *(ALPHA | NUM | '+' | '-' | '.') as defined by RFC 3896.
1078 * NOTE: Windows accepts a number as the first character of a scheme.
1080 static BOOL
parse_scheme_name(const WCHAR
**ptr
, parse_data
*data
, DWORD extras
) {
1081 const WCHAR
*start
= *ptr
;
1083 data
->scheme
= NULL
;
1084 data
->scheme_len
= 0;
1087 if(**ptr
== '*' && *ptr
== start
) {
1088 /* Might have found a wildcard scheme. If it is the next
1089 * char has to be a ':' for it to be a valid URI
1093 } else if(!is_num(**ptr
) && !is_alpha(**ptr
) && **ptr
!= '+' &&
1094 **ptr
!= '-' && **ptr
!= '.')
1103 /* Schemes must end with a ':' */
1104 if(**ptr
!= ':' && !((extras
& ALLOW_NULL_TERM_SCHEME
) && !**ptr
)) {
1109 data
->scheme
= start
;
1110 data
->scheme_len
= *ptr
- start
;
1116 /* Tries to deduce the corresponding URL_SCHEME for the given URI. Stores
1117 * the deduced URL_SCHEME in data->scheme_type.
1119 static BOOL
parse_scheme_type(parse_data
*data
) {
1120 /* If there's scheme data then see if it's a recognized scheme. */
1121 if(data
->scheme
&& data
->scheme_len
) {
1124 for(i
= 0; i
< sizeof(recognized_schemes
)/sizeof(recognized_schemes
[0]); ++i
) {
1125 if(lstrlenW(recognized_schemes
[i
].scheme_name
) == data
->scheme_len
) {
1126 /* Has to be a case insensitive compare. */
1127 if(!StrCmpNIW(recognized_schemes
[i
].scheme_name
, data
->scheme
, data
->scheme_len
)) {
1128 data
->scheme_type
= recognized_schemes
[i
].scheme
;
1134 /* If we get here it means it's not a recognized scheme. */
1135 data
->scheme_type
= URL_SCHEME_UNKNOWN
;
1137 } else if(data
->is_relative
) {
1138 /* Relative URI's have no scheme. */
1139 data
->scheme_type
= URL_SCHEME_UNKNOWN
;
1142 /* Should never reach here! what happened... */
1143 FIXME("(%p): Unable to determine scheme type for URI %s\n", data
, debugstr_w(data
->uri
));
1148 /* Tries to parse (or deduce) the scheme_name of a URI. If it can't
1149 * parse a scheme from the URI it will try to deduce the scheme_name and scheme_type
1150 * using the flags specified in 'flags' (if any). Flags that affect how this function
1151 * operates are the Uri_CREATE_ALLOW_* flags.
1153 * All parsed/deduced information will be stored in 'data' when the function returns.
1155 * Returns TRUE if it was able to successfully parse the information.
1157 static BOOL
parse_scheme(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
, DWORD extras
) {
1158 static const WCHAR fileW
[] = {'f','i','l','e',0};
1159 static const WCHAR wildcardW
[] = {'*',0};
1161 /* First check to see if the uri could implicitly be a file path. */
1162 if(is_implicit_file_path(*ptr
)) {
1163 if(flags
& Uri_CREATE_ALLOW_IMPLICIT_FILE_SCHEME
) {
1164 data
->scheme
= fileW
;
1165 data
->scheme_len
= lstrlenW(fileW
);
1166 data
->has_implicit_scheme
= TRUE
;
1168 TRACE("(%p %p %x): URI is an implicit file path.\n", ptr
, data
, flags
);
1170 /* Windows does not consider anything that can implicitly be a file
1171 * path to be a valid URI if the ALLOW_IMPLICIT_FILE_SCHEME flag is not set...
1173 TRACE("(%p %p %x): URI is implicitly a file path, but, the ALLOW_IMPLICIT_FILE_SCHEME flag wasn't set.\n",
1177 } else if(!parse_scheme_name(ptr
, data
, extras
)) {
1178 /* No scheme was found, this means it could be:
1179 * a) an implicit Wildcard scheme
1181 * c) an invalid URI.
1183 if(flags
& Uri_CREATE_ALLOW_IMPLICIT_WILDCARD_SCHEME
) {
1184 data
->scheme
= wildcardW
;
1185 data
->scheme_len
= lstrlenW(wildcardW
);
1186 data
->has_implicit_scheme
= TRUE
;
1188 TRACE("(%p %p %x): URI is an implicit wildcard scheme.\n", ptr
, data
, flags
);
1189 } else if (flags
& Uri_CREATE_ALLOW_RELATIVE
) {
1190 data
->is_relative
= TRUE
;
1191 TRACE("(%p %p %x): URI is relative.\n", ptr
, data
, flags
);
1193 TRACE("(%p %p %x): Malformed URI found. Unable to deduce scheme name.\n", ptr
, data
, flags
);
1198 if(!data
->is_relative
)
1199 TRACE("(%p %p %x): Found scheme=%s scheme_len=%d\n", ptr
, data
, flags
,
1200 debugstr_wn(data
->scheme
, data
->scheme_len
), data
->scheme_len
);
1202 if(!parse_scheme_type(data
))
1205 TRACE("(%p %p %x): Assigned %d as the URL_SCHEME.\n", ptr
, data
, flags
, data
->scheme_type
);
1209 static BOOL
parse_username(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
, DWORD extras
) {
1210 data
->username
= *ptr
;
1212 while(**ptr
!= ':' && **ptr
!= '@') {
1214 if(!check_pct_encoded(ptr
)) {
1215 if(data
->scheme_type
!= URL_SCHEME_UNKNOWN
) {
1216 *ptr
= data
->username
;
1217 data
->username
= NULL
;
1222 } else if(extras
& ALLOW_NULL_TERM_USER_NAME
&& !**ptr
)
1224 else if(is_auth_delim(**ptr
, data
->scheme_type
!= URL_SCHEME_UNKNOWN
)) {
1225 *ptr
= data
->username
;
1226 data
->username
= NULL
;
1233 data
->username_len
= *ptr
- data
->username
;
1237 static BOOL
parse_password(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
, DWORD extras
) {
1238 data
->password
= *ptr
;
1240 while(**ptr
!= '@') {
1242 if(!check_pct_encoded(ptr
)) {
1243 if(data
->scheme_type
!= URL_SCHEME_UNKNOWN
) {
1244 *ptr
= data
->password
;
1245 data
->password
= NULL
;
1250 } else if(extras
& ALLOW_NULL_TERM_PASSWORD
&& !**ptr
)
1252 else if(is_auth_delim(**ptr
, data
->scheme_type
!= URL_SCHEME_UNKNOWN
)) {
1253 *ptr
= data
->password
;
1254 data
->password
= NULL
;
1261 data
->password_len
= *ptr
- data
->password
;
1265 /* Parses the userinfo part of the URI (if it exists). The userinfo field of
1266 * a URI can consist of "username:password@", or just "username@".
1269 * userinfo = *( unreserved / pct-encoded / sub-delims / ":" )
1272 * 1) If there is more than one ':' in the userinfo part of the URI Windows
1273 * uses the first occurrence of ':' to delimit the username and password
1277 * ftp://user:pass:word@winehq.org
1279 * would yield "user" as the username and "pass:word" as the password.
1281 * 2) Windows allows any character to appear in the "userinfo" part of
1282 * a URI, as long as it's not an authority delimiter character set.
1284 static void parse_userinfo(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
) {
1285 const WCHAR
*start
= *ptr
;
1287 if(!parse_username(ptr
, data
, flags
, 0)) {
1288 TRACE("(%p %p %x): URI contained no userinfo.\n", ptr
, data
, flags
);
1294 if(!parse_password(ptr
, data
, flags
, 0)) {
1296 data
->username
= NULL
;
1297 data
->username_len
= 0;
1298 TRACE("(%p %p %x): URI contained no userinfo.\n", ptr
, data
, flags
);
1305 data
->username
= NULL
;
1306 data
->username_len
= 0;
1307 data
->password
= NULL
;
1308 data
->password_len
= 0;
1310 TRACE("(%p %p %x): URI contained no userinfo.\n", ptr
, data
, flags
);
1315 TRACE("(%p %p %x): Found username %s len=%d.\n", ptr
, data
, flags
,
1316 debugstr_wn(data
->username
, data
->username_len
), data
->username_len
);
1319 TRACE("(%p %p %x): Found password %s len=%d.\n", ptr
, data
, flags
,
1320 debugstr_wn(data
->password
, data
->password_len
), data
->password_len
);
1325 /* Attempts to parse a port from the URI.
1328 * Windows seems to have a cap on what the maximum value
1329 * for a port can be. The max value is USHORT_MAX.
1333 static BOOL
parse_port(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
) {
1337 while(!is_auth_delim(**ptr
, data
->scheme_type
!= URL_SCHEME_UNKNOWN
)) {
1338 if(!is_num(**ptr
)) {
1344 port
= port
*10 + (**ptr
-'0');
1346 if(port
> USHRT_MAX
) {
1355 data
->has_port
= TRUE
;
1356 data
->port_value
= port
;
1357 data
->port_len
= *ptr
- data
->port
;
1359 TRACE("(%p %p %x): Found port %s len=%d value=%u\n", ptr
, data
, flags
,
1360 debugstr_wn(data
->port
, data
->port_len
), data
->port_len
, data
->port_value
);
1364 /* Attempts to parse a IPv4 address from the URI.
1367 * Windows normalizes IPv4 addresses, This means there are three
1368 * possibilities for the URI to contain an IPv4 address.
1369 * 1) A well formed address (ex. 192.2.2.2).
1370 * 2) A partially formed address. For example "192.0" would
1371 * normalize to "192.0.0.0" during canonicalization.
1372 * 3) An implicit IPv4 address. For example "256" would
1373 * normalize to "0.0.1.0" during canonicalization. Also
1374 * note that the maximum value for an implicit IP address
1375 * is UINT_MAX, if the value in the URI exceeds this then
1376 * it is not considered an IPv4 address.
1378 static BOOL
parse_ipv4address(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
) {
1379 const BOOL is_unknown
= data
->scheme_type
== URL_SCHEME_UNKNOWN
;
1382 if(!check_ipv4address(ptr
, FALSE
)) {
1383 if(!check_implicit_ipv4(ptr
, &data
->implicit_ipv4
)) {
1384 TRACE("(%p %p %x): URI didn't contain anything looking like an IPv4 address.\n",
1390 data
->has_implicit_ip
= TRUE
;
1393 data
->host_len
= *ptr
- data
->host
;
1394 data
->host_type
= Uri_HOST_IPV4
;
1396 /* Check if what we found is the only part of the host name (if it isn't
1397 * we don't have an IPv4 address).
1401 if(!parse_port(ptr
, data
, flags
)) {
1406 } else if(!is_auth_delim(**ptr
, !is_unknown
)) {
1407 /* Found more data which belongs to the host, so this isn't an IPv4. */
1410 data
->has_implicit_ip
= FALSE
;
1414 TRACE("(%p %p %x): IPv4 address found. host=%s host_len=%d host_type=%d\n",
1415 ptr
, data
, flags
, debugstr_wn(data
->host
, data
->host_len
),
1416 data
->host_len
, data
->host_type
);
1420 /* Attempts to parse the reg-name from the URI.
1422 * Because of the way Windows handles ':' this function also
1423 * handles parsing the port.
1425 * reg-name = *( unreserved / pct-encoded / sub-delims )
1428 * Windows allows everything, but, the characters in "auth_delims" and ':'
1429 * to appear in a reg-name, unless it's an unknown scheme type then ':' is
1430 * allowed to appear (even if a valid port isn't after it).
1432 * Windows doesn't like host names which start with '[' and end with ']'
1433 * and don't contain a valid IP literal address in between them.
1435 * On Windows if a '[' is encountered in the host name the ':' no longer
1436 * counts as a delimiter until you reach the next ']' or an "authority delimiter".
1438 * A reg-name CAN be empty.
1440 static BOOL
parse_reg_name(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
, DWORD extras
) {
1441 const BOOL has_start_bracket
= **ptr
== '[';
1442 const BOOL known_scheme
= data
->scheme_type
!= URL_SCHEME_UNKNOWN
;
1443 const BOOL is_res
= data
->scheme_type
== URL_SCHEME_RES
;
1444 BOOL inside_brackets
= has_start_bracket
;
1446 /* res URIs don't have ports. */
1447 BOOL ignore_col
= (extras
& IGNORE_PORT_DELIMITER
) || is_res
;
1449 /* We have to be careful with file schemes. */
1450 if(data
->scheme_type
== URL_SCHEME_FILE
) {
1451 /* This is because an implicit file scheme could be "C:\\test" and it
1452 * would trick this function into thinking the host is "C", when after
1453 * canonicalization the host would end up being an empty string. A drive
1454 * path can also have a '|' instead of a ':' after the drive letter.
1456 if(is_drive_path(*ptr
)) {
1457 /* Regular old drive paths have no host type (or host name). */
1458 data
->host_type
= Uri_HOST_UNKNOWN
;
1462 } else if(is_unc_path(*ptr
))
1463 /* Skip past the "\\" of a UNC path. */
1469 /* For res URIs, everything before the first '/' is
1470 * considered the host.
1472 while((!is_res
&& !is_auth_delim(**ptr
, known_scheme
)) ||
1473 (is_res
&& **ptr
&& **ptr
!= '/')) {
1474 if(**ptr
== ':' && !ignore_col
) {
1475 /* We can ignore ':' if we are inside brackets.*/
1476 if(!inside_brackets
) {
1477 const WCHAR
*tmp
= (*ptr
)++;
1479 /* Attempt to parse the port. */
1480 if(!parse_port(ptr
, data
, flags
)) {
1481 /* Windows expects there to be a valid port for known scheme types. */
1482 if(data
->scheme_type
!= URL_SCHEME_UNKNOWN
) {
1485 TRACE("(%p %p %x %x): Expected valid port\n", ptr
, data
, flags
, extras
);
1488 /* Windows gives up on trying to parse a port when it
1489 * encounters an invalid port.
1493 data
->host_len
= tmp
- data
->host
;
1497 } else if(**ptr
== '%' && (known_scheme
&& !is_res
)) {
1498 /* Has to be a legit % encoded value. */
1499 if(!check_pct_encoded(ptr
)) {
1505 } else if(is_res
&& is_forbidden_dos_path_char(**ptr
)) {
1509 } else if(**ptr
== ']')
1510 inside_brackets
= FALSE
;
1511 else if(**ptr
== '[')
1512 inside_brackets
= TRUE
;
1517 if(has_start_bracket
) {
1518 /* Make sure the last character of the host wasn't a ']'. */
1519 if(*(*ptr
-1) == ']') {
1520 TRACE("(%p %p %x %x): Expected an IP literal inside of the host\n",
1521 ptr
, data
, flags
, extras
);
1528 /* Don't overwrite our length if we found a port earlier. */
1530 data
->host_len
= *ptr
- data
->host
;
1532 /* If the host is empty, then it's an unknown host type. */
1533 if(data
->host_len
== 0 || is_res
)
1534 data
->host_type
= Uri_HOST_UNKNOWN
;
1536 data
->host_type
= Uri_HOST_DNS
;
1538 TRACE("(%p %p %x %x): Parsed reg-name. host=%s len=%d\n", ptr
, data
, flags
, extras
,
1539 debugstr_wn(data
->host
, data
->host_len
), data
->host_len
);
1543 /* Attempts to parse an IPv6 address out of the URI.
1545 * IPv6address = 6( h16 ":" ) ls32
1546 * / "::" 5( h16 ":" ) ls32
1547 * / [ h16 ] "::" 4( h16 ":" ) ls32
1548 * / [ *1( h16 ":" ) h16 ] "::" 3( h16 ":" ) ls32
1549 * / [ *2( h16 ":" ) h16 ] "::" 2( h16 ":" ) ls32
1550 * / [ *3( h16 ":" ) h16 ] "::" h16 ":" ls32
1551 * / [ *4( h16 ":" ) h16 ] "::" ls32
1552 * / [ *5( h16 ":" ) h16 ] "::" h16
1553 * / [ *6( h16 ":" ) h16 ] "::"
1555 * ls32 = ( h16 ":" h16 ) / IPv4address
1556 * ; least-significant 32 bits of address.
1559 * ; 16 bits of address represented in hexadecimal.
1561 * Modeled after google-url's 'DoParseIPv6' function.
1563 static BOOL
parse_ipv6address(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
) {
1564 const WCHAR
*start
, *cur_start
;
1567 start
= cur_start
= *ptr
;
1568 memset(&ip
, 0, sizeof(ipv6_address
));
1571 /* Check if we're on the last character of the host. */
1572 BOOL is_end
= (is_auth_delim(**ptr
, data
->scheme_type
!= URL_SCHEME_UNKNOWN
)
1575 BOOL is_split
= (**ptr
== ':');
1576 BOOL is_elision
= (is_split
&& !is_end
&& *(*ptr
+1) == ':');
1578 /* Check if we're at the end of a component, or
1579 * if we're at the end of the IPv6 address.
1581 if(is_split
|| is_end
) {
1584 cur_len
= *ptr
- cur_start
;
1586 /* h16 can't have a length > 4. */
1590 TRACE("(%p %p %x): h16 component to long.\n",
1596 /* An h16 component can't have the length of 0 unless
1597 * the elision is at the beginning of the address, or
1598 * at the end of the address.
1600 if(!((*ptr
== start
&& is_elision
) ||
1601 (is_end
&& (*ptr
-2) == ip
.elision
))) {
1603 TRACE("(%p %p %x): IPv6 component cannot have a length of 0.\n",
1610 /* An IPv6 address can have no more than 8 h16 components. */
1611 if(ip
.h16_count
>= 8) {
1613 TRACE("(%p %p %x): Not a IPv6 address, too many h16 components.\n",
1618 ip
.components
[ip
.h16_count
].str
= cur_start
;
1619 ip
.components
[ip
.h16_count
].len
= cur_len
;
1621 TRACE("(%p %p %x): Found h16 component %s, len=%d, h16_count=%d\n",
1622 ptr
, data
, flags
, debugstr_wn(cur_start
, cur_len
), cur_len
,
1632 /* A IPv6 address can only have 1 elision ('::'). */
1636 TRACE("(%p %p %x): IPv6 address cannot have 2 elisions.\n",
1648 if(!check_ipv4address(ptr
, TRUE
)) {
1649 if(!is_hexdigit(**ptr
)) {
1650 /* Not a valid character for an IPv6 address. */
1655 /* Found an IPv4 address. */
1656 ip
.ipv4
= cur_start
;
1657 ip
.ipv4_len
= *ptr
- cur_start
;
1659 TRACE("(%p %p %x): Found an attached IPv4 address %s len=%d.\n",
1660 ptr
, data
, flags
, debugstr_wn(ip
.ipv4
, ip
.ipv4_len
),
1663 /* IPv4 addresses can only appear at the end of a IPv6. */
1669 compute_ipv6_comps_size(&ip
);
1671 /* Make sure the IPv6 address adds up to 16 bytes. */
1672 if(ip
.components_size
+ ip
.elision_size
!= 16) {
1674 TRACE("(%p %p %x): Invalid IPv6 address, did not add up to 16 bytes.\n",
1679 if(ip
.elision_size
== 2) {
1680 /* For some reason on Windows if an elision that represents
1681 * only one h16 component is encountered at the very begin or
1682 * end of an IPv6 address, Windows does not consider it a
1683 * valid IPv6 address.
1685 * Ex: [::2:3:4:5:6:7] is not valid, even though the sum
1686 * of all the components == 128bits.
1688 if(ip
.elision
< ip
.components
[0].str
||
1689 ip
.elision
> ip
.components
[ip
.h16_count
-1].str
) {
1691 TRACE("(%p %p %x): Invalid IPv6 address. Detected elision of 2 bytes at the beginning or end of the address.\n",
1697 data
->host_type
= Uri_HOST_IPV6
;
1698 data
->has_ipv6
= TRUE
;
1699 data
->ipv6_address
= ip
;
1701 TRACE("(%p %p %x): Found valid IPv6 literal %s len=%d\n",
1702 ptr
, data
, flags
, debugstr_wn(start
, *ptr
-start
),
1707 /* IPvFuture = "v" 1*HEXDIG "." 1*( unreserved / sub-delims / ":" ) */
1708 static BOOL
parse_ipvfuture(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
) {
1709 const WCHAR
*start
= *ptr
;
1711 /* IPvFuture has to start with a 'v' or 'V'. */
1712 if(**ptr
!= 'v' && **ptr
!= 'V')
1715 /* Following the v there must be at least 1 hex digit. */
1717 if(!is_hexdigit(**ptr
)) {
1723 while(is_hexdigit(**ptr
))
1726 /* End of the hexdigit sequence must be a '.' */
1733 if(!is_unreserved(**ptr
) && !is_subdelim(**ptr
) && **ptr
!= ':') {
1739 while(is_unreserved(**ptr
) || is_subdelim(**ptr
) || **ptr
== ':')
1742 data
->host_type
= Uri_HOST_UNKNOWN
;
1744 TRACE("(%p %p %x): Parsed IPvFuture address %s len=%d\n", ptr
, data
, flags
,
1745 debugstr_wn(start
, *ptr
-start
), (int)(*ptr
-start
));
1750 /* IP-literal = "[" ( IPv6address / IPvFuture ) "]" */
1751 static BOOL
parse_ip_literal(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
, DWORD extras
) {
1754 if(**ptr
!= '[' && !(extras
& ALLOW_BRACKETLESS_IP_LITERAL
)) {
1757 } else if(**ptr
== '[')
1760 if(!parse_ipv6address(ptr
, data
, flags
)) {
1761 if(extras
& SKIP_IP_FUTURE_CHECK
|| !parse_ipvfuture(ptr
, data
, flags
)) {
1768 if(**ptr
!= ']' && !(extras
& ALLOW_BRACKETLESS_IP_LITERAL
)) {
1772 } else if(!**ptr
&& extras
& ALLOW_BRACKETLESS_IP_LITERAL
) {
1773 /* The IP literal didn't contain brackets and was followed by
1774 * a NULL terminator, so no reason to even check the port.
1776 data
->host_len
= *ptr
- data
->host
;
1783 /* If a valid port is not found, then let it trickle down to
1786 if(!parse_port(ptr
, data
, flags
)) {
1792 data
->host_len
= *ptr
- data
->host
;
1797 /* Parses the host information from the URI.
1799 * host = IP-literal / IPv4address / reg-name
1801 static BOOL
parse_host(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
, DWORD extras
) {
1802 if(!parse_ip_literal(ptr
, data
, flags
, extras
)) {
1803 if(!parse_ipv4address(ptr
, data
, flags
)) {
1804 if(!parse_reg_name(ptr
, data
, flags
, extras
)) {
1805 TRACE("(%p %p %x %x): Malformed URI, Unknown host type.\n",
1806 ptr
, data
, flags
, extras
);
1815 /* Parses the authority information from the URI.
1817 * authority = [ userinfo "@" ] host [ ":" port ]
1819 static BOOL
parse_authority(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
) {
1820 parse_userinfo(ptr
, data
, flags
);
1822 /* Parsing the port will happen during one of the host parsing
1823 * routines (if the URI has a port).
1825 if(!parse_host(ptr
, data
, flags
, 0))
1831 /* Attempts to parse the path information of a hierarchical URI. */
1832 static BOOL
parse_path_hierarchical(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
) {
1833 const WCHAR
*start
= *ptr
;
1834 static const WCHAR slash
[] = {'/',0};
1835 const BOOL is_file
= data
->scheme_type
== URL_SCHEME_FILE
;
1837 if(is_path_delim(data
->scheme_type
, **ptr
)) {
1838 if(data
->scheme_type
== URL_SCHEME_WILDCARD
&& !data
->must_have_path
) {
1841 } else if(!(flags
& Uri_CREATE_NO_CANONICALIZE
)) {
1842 /* If the path component is empty, then a '/' is added. */
1847 while(!is_path_delim(data
->scheme_type
, **ptr
)) {
1848 if(**ptr
== '%' && data
->scheme_type
!= URL_SCHEME_UNKNOWN
&& !is_file
) {
1849 if(!check_pct_encoded(ptr
)) {
1854 } else if(is_forbidden_dos_path_char(**ptr
) && is_file
&&
1855 (flags
& Uri_CREATE_FILE_USE_DOS_PATH
)) {
1856 /* File schemes with USE_DOS_PATH set aren't allowed to have
1857 * a '<' or '>' or '\"' appear in them.
1861 } else if(**ptr
== '\\') {
1862 /* Not allowed to have a backslash if NO_CANONICALIZE is set
1863 * and the scheme is known type (but not a file scheme).
1865 if(flags
& Uri_CREATE_NO_CANONICALIZE
) {
1866 if(data
->scheme_type
!= URL_SCHEME_FILE
&&
1867 data
->scheme_type
!= URL_SCHEME_UNKNOWN
) {
1877 /* The only time a URI doesn't have a path is when
1878 * the NO_CANONICALIZE flag is set and the raw URI
1879 * didn't contain one.
1886 data
->path_len
= *ptr
- start
;
1891 TRACE("(%p %p %x): Parsed path %s len=%d\n", ptr
, data
, flags
,
1892 debugstr_wn(data
->path
, data
->path_len
), data
->path_len
);
1894 TRACE("(%p %p %x): The URI contained no path\n", ptr
, data
, flags
);
1899 /* Parses the path of an opaque URI (much less strict than the parser
1900 * for a hierarchical URI).
1903 * Windows allows invalid % encoded data to appear in opaque URI paths
1904 * for unknown scheme types.
1906 * File schemes with USE_DOS_PATH set aren't allowed to have '<', '>', or '\"'
1909 static BOOL
parse_path_opaque(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
) {
1910 const BOOL known_scheme
= data
->scheme_type
!= URL_SCHEME_UNKNOWN
;
1911 const BOOL is_file
= data
->scheme_type
== URL_SCHEME_FILE
;
1912 const BOOL is_mailto
= data
->scheme_type
== URL_SCHEME_MAILTO
;
1914 if (is_mailto
&& (*ptr
)[0] == '/' && (*ptr
)[1] == '/')
1916 if ((*ptr
)[2]) data
->path
= *ptr
+ 2;
1917 else data
->path
= NULL
;
1922 while(!is_path_delim(data
->scheme_type
, **ptr
)) {
1923 if(**ptr
== '%' && known_scheme
) {
1924 if(!check_pct_encoded(ptr
)) {
1930 } else if(is_forbidden_dos_path_char(**ptr
) && is_file
&&
1931 (flags
& Uri_CREATE_FILE_USE_DOS_PATH
)) {
1940 if (data
->path
) data
->path_len
= *ptr
- data
->path
;
1941 TRACE("(%p %p %x): Parsed opaque URI path %s len=%d\n", ptr
, data
, flags
,
1942 debugstr_wn(data
->path
, data
->path_len
), data
->path_len
);
1946 /* Determines how the URI should be parsed after the scheme information.
1948 * If the scheme is followed by "//", then it is treated as a hierarchical URI
1949 * which then the authority and path information will be parsed out. Otherwise, the
1950 * URI will be treated as an opaque URI which the authority information is not parsed
1953 * RFC 3896 definition of hier-part:
1955 * hier-part = "//" authority path-abempty
1960 * MSDN opaque URI definition:
1961 * scheme ":" path [ "#" fragment ]
1964 * If the URI is of an unknown scheme type and has a "//" following the scheme then it
1965 * is treated as a hierarchical URI, but, if the CREATE_NO_CRACK_UNKNOWN_SCHEMES flag is
1966 * set then it is considered an opaque URI regardless of what follows the scheme information
1967 * (per MSDN documentation).
1969 static BOOL
parse_hierpart(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
) {
1970 const WCHAR
*start
= *ptr
;
1972 data
->must_have_path
= FALSE
;
1974 /* For javascript: URIs, simply set everything as a path */
1975 if(data
->scheme_type
== URL_SCHEME_JAVASCRIPT
) {
1977 data
->path_len
= strlenW(*ptr
);
1978 data
->is_opaque
= TRUE
;
1979 *ptr
+= data
->path_len
;
1983 /* Checks if the authority information needs to be parsed. */
1984 if(is_hierarchical_uri(ptr
, data
)) {
1985 /* Only treat it as a hierarchical URI if the scheme_type is known or
1986 * the Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES flag is not set.
1988 if(data
->scheme_type
!= URL_SCHEME_UNKNOWN
||
1989 !(flags
& Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES
)) {
1990 TRACE("(%p %p %x): Treating URI as an hierarchical URI.\n", ptr
, data
, flags
);
1991 data
->is_opaque
= FALSE
;
1993 if(data
->scheme_type
== URL_SCHEME_WILDCARD
&& !data
->has_implicit_scheme
) {
1994 if(**ptr
== '/' && *(*ptr
+1) == '/') {
1995 data
->must_have_path
= TRUE
;
2000 /* TODO: Handle hierarchical URI's, parse authority then parse the path. */
2001 if(!parse_authority(ptr
, data
, flags
))
2004 return parse_path_hierarchical(ptr
, data
, flags
);
2006 /* Reset ptr to its starting position so opaque path parsing
2007 * begins at the correct location.
2012 /* If it reaches here, then the URI will be treated as an opaque
2016 TRACE("(%p %p %x): Treating URI as an opaque URI.\n", ptr
, data
, flags
);
2018 data
->is_opaque
= TRUE
;
2019 if(!parse_path_opaque(ptr
, data
, flags
))
2025 /* Attempts to parse the query string from the URI.
2028 * If NO_DECODE_EXTRA_INFO flag is set, then invalid percent encoded
2029 * data is allowed to appear in the query string. For unknown scheme types
2030 * invalid percent encoded data is allowed to appear regardless.
2032 static BOOL
parse_query(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
) {
2033 const BOOL known_scheme
= data
->scheme_type
!= URL_SCHEME_UNKNOWN
;
2036 TRACE("(%p %p %x): URI didn't contain a query string.\n", ptr
, data
, flags
);
2043 while(**ptr
&& **ptr
!= '#') {
2044 if(**ptr
== '%' && known_scheme
&&
2045 !(flags
& Uri_CREATE_NO_DECODE_EXTRA_INFO
)) {
2046 if(!check_pct_encoded(ptr
)) {
2057 data
->query_len
= *ptr
- data
->query
;
2059 TRACE("(%p %p %x): Parsed query string %s len=%d\n", ptr
, data
, flags
,
2060 debugstr_wn(data
->query
, data
->query_len
), data
->query_len
);
2064 /* Attempts to parse the fragment from the URI.
2067 * If NO_DECODE_EXTRA_INFO flag is set, then invalid percent encoded
2068 * data is allowed to appear in the query string. For unknown scheme types
2069 * invalid percent encoded data is allowed to appear regardless.
2071 static BOOL
parse_fragment(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
) {
2072 const BOOL known_scheme
= data
->scheme_type
!= URL_SCHEME_UNKNOWN
;
2075 TRACE("(%p %p %x): URI didn't contain a fragment.\n", ptr
, data
, flags
);
2079 data
->fragment
= *ptr
;
2083 if(**ptr
== '%' && known_scheme
&&
2084 !(flags
& Uri_CREATE_NO_DECODE_EXTRA_INFO
)) {
2085 if(!check_pct_encoded(ptr
)) {
2086 *ptr
= data
->fragment
;
2087 data
->fragment
= NULL
;
2096 data
->fragment_len
= *ptr
- data
->fragment
;
2098 TRACE("(%p %p %x): Parsed fragment %s len=%d\n", ptr
, data
, flags
,
2099 debugstr_wn(data
->fragment
, data
->fragment_len
), data
->fragment_len
);
2103 /* Parses and validates the components of the specified by data->uri
2104 * and stores the information it parses into 'data'.
2106 * Returns TRUE if it successfully parsed the URI. False otherwise.
2108 static BOOL
parse_uri(parse_data
*data
, DWORD flags
) {
2115 TRACE("(%p %x): BEGINNING TO PARSE URI %s.\n", data
, flags
, debugstr_w(data
->uri
));
2117 if(!parse_scheme(pptr
, data
, flags
, 0))
2120 if(!parse_hierpart(pptr
, data
, flags
))
2123 if(!parse_query(pptr
, data
, flags
))
2126 if(!parse_fragment(pptr
, data
, flags
))
2129 TRACE("(%p %x): FINISHED PARSING URI.\n", data
, flags
);
2133 static BOOL
canonicalize_username(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
2136 if(!data
->username
) {
2137 uri
->userinfo_start
= -1;
2141 uri
->userinfo_start
= uri
->canon_len
;
2142 for(ptr
= data
->username
; ptr
< data
->username
+data
->username_len
; ++ptr
) {
2144 /* Only decode % encoded values for known scheme types. */
2145 if(data
->scheme_type
!= URL_SCHEME_UNKNOWN
) {
2146 /* See if the value really needs decoding. */
2147 WCHAR val
= decode_pct_val(ptr
);
2148 if(is_unreserved(val
)) {
2150 uri
->canon_uri
[uri
->canon_len
] = val
;
2154 /* Move pass the hex characters. */
2159 } else if(!is_reserved(*ptr
) && !is_unreserved(*ptr
) && *ptr
!= '\\') {
2160 /* Only percent encode forbidden characters if the NO_ENCODE_FORBIDDEN_CHARACTERS flag
2163 if(!(flags
& Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS
)) {
2165 pct_encode_val(*ptr
, uri
->canon_uri
+ uri
->canon_len
);
2167 uri
->canon_len
+= 3;
2173 /* Nothing special, so just copy the character over. */
2174 uri
->canon_uri
[uri
->canon_len
] = *ptr
;
2181 static BOOL
canonicalize_password(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
2184 if(!data
->password
) {
2185 uri
->userinfo_split
= -1;
2189 if(uri
->userinfo_start
== -1)
2190 /* Has a password, but, doesn't have a username. */
2191 uri
->userinfo_start
= uri
->canon_len
;
2193 uri
->userinfo_split
= uri
->canon_len
- uri
->userinfo_start
;
2195 /* Add the ':' to the userinfo component. */
2197 uri
->canon_uri
[uri
->canon_len
] = ':';
2200 for(ptr
= data
->password
; ptr
< data
->password
+data
->password_len
; ++ptr
) {
2202 /* Only decode % encoded values for known scheme types. */
2203 if(data
->scheme_type
!= URL_SCHEME_UNKNOWN
) {
2204 /* See if the value really needs decoding. */
2205 WCHAR val
= decode_pct_val(ptr
);
2206 if(is_unreserved(val
)) {
2208 uri
->canon_uri
[uri
->canon_len
] = val
;
2212 /* Move pass the hex characters. */
2217 } else if(!is_reserved(*ptr
) && !is_unreserved(*ptr
) && *ptr
!= '\\') {
2218 /* Only percent encode forbidden characters if the NO_ENCODE_FORBIDDEN_CHARACTERS flag
2221 if(!(flags
& Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS
)) {
2223 pct_encode_val(*ptr
, uri
->canon_uri
+ uri
->canon_len
);
2225 uri
->canon_len
+= 3;
2231 /* Nothing special, so just copy the character over. */
2232 uri
->canon_uri
[uri
->canon_len
] = *ptr
;
2239 /* Canonicalizes the userinfo of the URI represented by the parse_data.
2241 * Canonicalization of the userinfo is a simple process. If there are any percent
2242 * encoded characters that fall in the "unreserved" character set, they are decoded
2243 * to their actual value. If a character is not in the "unreserved" or "reserved" sets
2244 * then it is percent encoded. Other than that the characters are copied over without
2247 static BOOL
canonicalize_userinfo(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
2248 uri
->userinfo_start
= uri
->userinfo_split
= -1;
2249 uri
->userinfo_len
= 0;
2251 if(!data
->username
&& !data
->password
)
2252 /* URI doesn't have userinfo, so nothing to do here. */
2255 if(!canonicalize_username(data
, uri
, flags
, computeOnly
))
2258 if(!canonicalize_password(data
, uri
, flags
, computeOnly
))
2261 uri
->userinfo_len
= uri
->canon_len
- uri
->userinfo_start
;
2263 TRACE("(%p %p %x %d): Canonicalized userinfo, userinfo_start=%d, userinfo=%s, userinfo_split=%d userinfo_len=%d.\n",
2264 data
, uri
, flags
, computeOnly
, uri
->userinfo_start
, debugstr_wn(uri
->canon_uri
+ uri
->userinfo_start
, uri
->userinfo_len
),
2265 uri
->userinfo_split
, uri
->userinfo_len
);
2267 /* Now insert the '@' after the userinfo. */
2269 uri
->canon_uri
[uri
->canon_len
] = '@';
2275 /* Attempts to canonicalize a reg_name.
2277 * Things that happen:
2278 * 1) If Uri_CREATE_NO_CANONICALIZE flag is not set, then the reg_name is
2279 * lower cased. Unless it's an unknown scheme type, which case it's
2280 * no lower cased regardless.
2282 * 2) Unreserved % encoded characters are decoded for known
2285 * 3) Forbidden characters are % encoded as long as
2286 * Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS flag is not set and
2287 * it isn't an unknown scheme type.
2289 * 4) If it's a file scheme and the host is "localhost" it's removed.
2291 * 5) If it's a file scheme and Uri_CREATE_FILE_USE_DOS_PATH is set,
2292 * then the UNC path characters are added before the host name.
2294 static BOOL
canonicalize_reg_name(const parse_data
*data
, Uri
*uri
,
2295 DWORD flags
, BOOL computeOnly
) {
2296 static const WCHAR localhostW
[] =
2297 {'l','o','c','a','l','h','o','s','t',0};
2299 const BOOL known_scheme
= data
->scheme_type
!= URL_SCHEME_UNKNOWN
;
2301 if(data
->scheme_type
== URL_SCHEME_FILE
&&
2302 data
->host_len
== lstrlenW(localhostW
)) {
2303 if(!StrCmpNIW(data
->host
, localhostW
, data
->host_len
)) {
2304 uri
->host_start
= -1;
2306 uri
->host_type
= Uri_HOST_UNKNOWN
;
2311 if(data
->scheme_type
== URL_SCHEME_FILE
&& flags
& Uri_CREATE_FILE_USE_DOS_PATH
) {
2313 uri
->canon_uri
[uri
->canon_len
] = '\\';
2314 uri
->canon_uri
[uri
->canon_len
+1] = '\\';
2316 uri
->canon_len
+= 2;
2317 uri
->authority_start
= uri
->canon_len
;
2320 uri
->host_start
= uri
->canon_len
;
2322 for(ptr
= data
->host
; ptr
< data
->host
+data
->host_len
; ++ptr
) {
2323 if(*ptr
== '%' && known_scheme
) {
2324 WCHAR val
= decode_pct_val(ptr
);
2325 if(is_unreserved(val
)) {
2326 /* If NO_CANONICALIZE is not set, then windows lower cases the
2329 if(!(flags
& Uri_CREATE_NO_CANONICALIZE
) && isupperW(val
)) {
2331 uri
->canon_uri
[uri
->canon_len
] = tolowerW(val
);
2334 uri
->canon_uri
[uri
->canon_len
] = val
;
2338 /* Skip past the % encoded character. */
2342 /* Just copy the % over. */
2344 uri
->canon_uri
[uri
->canon_len
] = *ptr
;
2347 } else if(*ptr
== '\\') {
2348 /* Only unknown scheme types could have made it here with a '\\' in the host name. */
2350 uri
->canon_uri
[uri
->canon_len
] = *ptr
;
2352 } else if(!(flags
& Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS
) &&
2353 !is_unreserved(*ptr
) && !is_reserved(*ptr
) && known_scheme
) {
2355 pct_encode_val(*ptr
, uri
->canon_uri
+uri
->canon_len
);
2357 /* The percent encoded value gets lower cased also. */
2358 if(!(flags
& Uri_CREATE_NO_CANONICALIZE
)) {
2359 uri
->canon_uri
[uri
->canon_len
+1] = tolowerW(uri
->canon_uri
[uri
->canon_len
+1]);
2360 uri
->canon_uri
[uri
->canon_len
+2] = tolowerW(uri
->canon_uri
[uri
->canon_len
+2]);
2364 uri
->canon_len
+= 3;
2367 if(!(flags
& Uri_CREATE_NO_CANONICALIZE
) && known_scheme
)
2368 uri
->canon_uri
[uri
->canon_len
] = tolowerW(*ptr
);
2370 uri
->canon_uri
[uri
->canon_len
] = *ptr
;
2377 uri
->host_len
= uri
->canon_len
- uri
->host_start
;
2380 TRACE("(%p %p %x %d): Canonicalize reg_name=%s len=%d\n", data
, uri
, flags
,
2381 computeOnly
, debugstr_wn(uri
->canon_uri
+uri
->host_start
, uri
->host_len
),
2385 find_domain_name(uri
->canon_uri
+uri
->host_start
, uri
->host_len
,
2386 &(uri
->domain_offset
));
2391 /* Attempts to canonicalize an implicit IPv4 address. */
2392 static BOOL
canonicalize_implicit_ipv4address(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
2393 uri
->host_start
= uri
->canon_len
;
2395 TRACE("%u\n", data
->implicit_ipv4
);
2396 /* For unknown scheme types Windows doesn't convert
2397 * the value into an IP address, but it still considers
2398 * it an IPv4 address.
2400 if(data
->scheme_type
== URL_SCHEME_UNKNOWN
) {
2402 memcpy(uri
->canon_uri
+uri
->canon_len
, data
->host
, data
->host_len
*sizeof(WCHAR
));
2403 uri
->canon_len
+= data
->host_len
;
2406 uri
->canon_len
+= ui2ipv4(uri
->canon_uri
+uri
->canon_len
, data
->implicit_ipv4
);
2408 uri
->canon_len
+= ui2ipv4(NULL
, data
->implicit_ipv4
);
2411 uri
->host_len
= uri
->canon_len
- uri
->host_start
;
2412 uri
->host_type
= Uri_HOST_IPV4
;
2415 TRACE("%p %p %x %d): Canonicalized implicit IP address=%s len=%d\n",
2416 data
, uri
, flags
, computeOnly
,
2417 debugstr_wn(uri
->canon_uri
+uri
->host_start
, uri
->host_len
),
2423 /* Attempts to canonicalize an IPv4 address.
2425 * If the parse_data represents a URI that has an implicit IPv4 address
2426 * (ex. http://256/, this function will convert 256 into 0.0.1.0). If
2427 * the implicit IP address exceeds the value of UINT_MAX (maximum value
2428 * for an IPv4 address) it's canonicalized as if it were a reg-name.
2430 * If the parse_data contains a partial or full IPv4 address it normalizes it.
2431 * A partial IPv4 address is something like "192.0" and would be normalized to
2432 * "192.0.0.0". With a full (or partial) IPv4 address like "192.002.01.003" would
2433 * be normalized to "192.2.1.3".
2436 * Windows ONLY normalizes IPv4 address for known scheme types (one that isn't
2437 * URL_SCHEME_UNKNOWN). For unknown scheme types, it simply copies the data from
2438 * the original URI into the canonicalized URI, but, it still recognizes URI's
2439 * host type as HOST_IPV4.
2441 static BOOL
canonicalize_ipv4address(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
2442 if(data
->has_implicit_ip
)
2443 return canonicalize_implicit_ipv4address(data
, uri
, flags
, computeOnly
);
2445 uri
->host_start
= uri
->canon_len
;
2447 /* Windows only normalizes for known scheme types. */
2448 if(data
->scheme_type
!= URL_SCHEME_UNKNOWN
) {
2449 /* parse_data contains a partial or full IPv4 address, so normalize it. */
2450 DWORD i
, octetDigitCount
= 0, octetCount
= 0;
2451 BOOL octetHasDigit
= FALSE
;
2453 for(i
= 0; i
< data
->host_len
; ++i
) {
2454 if(data
->host
[i
] == '0' && !octetHasDigit
) {
2455 /* Can ignore leading zeros if:
2456 * 1) It isn't the last digit of the octet.
2457 * 2) i+1 != data->host_len
2460 if(octetDigitCount
== 2 ||
2461 i
+1 == data
->host_len
||
2462 data
->host
[i
+1] == '.') {
2464 uri
->canon_uri
[uri
->canon_len
] = data
->host
[i
];
2466 TRACE("Adding zero\n");
2468 } else if(data
->host
[i
] == '.') {
2470 uri
->canon_uri
[uri
->canon_len
] = data
->host
[i
];
2473 octetDigitCount
= 0;
2474 octetHasDigit
= FALSE
;
2478 uri
->canon_uri
[uri
->canon_len
] = data
->host
[i
];
2482 octetHasDigit
= TRUE
;
2486 /* Make sure the canonicalized IP address has 4 dec-octets.
2487 * If doesn't add "0" ones until there is 4;
2489 for( ; octetCount
< 3; ++octetCount
) {
2491 uri
->canon_uri
[uri
->canon_len
] = '.';
2492 uri
->canon_uri
[uri
->canon_len
+1] = '0';
2495 uri
->canon_len
+= 2;
2498 /* Windows doesn't normalize addresses in unknown schemes. */
2500 memcpy(uri
->canon_uri
+uri
->canon_len
, data
->host
, data
->host_len
*sizeof(WCHAR
));
2501 uri
->canon_len
+= data
->host_len
;
2504 uri
->host_len
= uri
->canon_len
- uri
->host_start
;
2506 TRACE("(%p %p %x %d): Canonicalized IPv4 address, ip=%s len=%d\n",
2507 data
, uri
, flags
, computeOnly
,
2508 debugstr_wn(uri
->canon_uri
+uri
->host_start
, uri
->host_len
),
2515 /* Attempts to canonicalize the IPv6 address of the URI.
2517 * Multiple things happen during the canonicalization of an IPv6 address:
2518 * 1) Any leading zero's in a h16 component are removed.
2519 * Ex: [0001:0022::] -> [1:22::]
2521 * 2) The longest sequence of zero h16 components are compressed
2522 * into a "::" (elision). If there's a tie, the first is chosen.
2524 * Ex: [0:0:0:0:1:6:7:8] -> [::1:6:7:8]
2525 * [0:0:0:0:1:2::] -> [::1:2:0:0]
2526 * [0:0:1:2:0:0:7:8] -> [::1:2:0:0:7:8]
2528 * 3) If an IPv4 address is attached to the IPv6 address, it's
2530 * Ex: [::001.002.022.000] -> [::1.2.22.0]
2532 * 4) If an elision is present, but, only represents one h16 component
2535 * Ex: [1::2:3:4:5:6:7] -> [1:0:2:3:4:5:6:7]
2537 * 5) If the IPv6 address contains an IPv4 address and there exists
2538 * at least 1 non-zero h16 component the IPv4 address is converted
2539 * into two h16 components, otherwise it's normalized and kept as is.
2541 * Ex: [::192.200.003.4] -> [::192.200.3.4]
2542 * [ffff::192.200.003.4] -> [ffff::c0c8:3041]
2545 * For unknown scheme types Windows simply copies the address over without any
2548 * IPv4 address can be included in an elision if all its components are 0's.
2550 static BOOL
canonicalize_ipv6address(const parse_data
*data
, Uri
*uri
,
2551 DWORD flags
, BOOL computeOnly
) {
2552 uri
->host_start
= uri
->canon_len
;
2554 if(data
->scheme_type
== URL_SCHEME_UNKNOWN
) {
2556 memcpy(uri
->canon_uri
+uri
->canon_len
, data
->host
, data
->host_len
*sizeof(WCHAR
));
2557 uri
->canon_len
+= data
->host_len
;
2561 DWORD i
, elision_len
;
2563 if(!ipv6_to_number(&(data
->ipv6_address
), values
)) {
2564 TRACE("(%p %p %x %d): Failed to compute numerical value for IPv6 address.\n",
2565 data
, uri
, flags
, computeOnly
);
2570 uri
->canon_uri
[uri
->canon_len
] = '[';
2573 /* Find where the elision should occur (if any). */
2574 compute_elision_location(&(data
->ipv6_address
), values
, &elision_start
, &elision_len
);
2576 TRACE("%p %p %x %d): Elision starts at %d, len=%u\n", data
, uri
, flags
,
2577 computeOnly
, elision_start
, elision_len
);
2579 for(i
= 0; i
< 8; ++i
) {
2580 BOOL in_elision
= (elision_start
> -1 && i
>= elision_start
&&
2581 i
< elision_start
+elision_len
);
2582 BOOL do_ipv4
= (i
== 6 && data
->ipv6_address
.ipv4
&& !in_elision
&&
2583 data
->ipv6_address
.h16_count
== 0);
2585 if(i
== elision_start
) {
2587 uri
->canon_uri
[uri
->canon_len
] = ':';
2588 uri
->canon_uri
[uri
->canon_len
+1] = ':';
2590 uri
->canon_len
+= 2;
2593 /* We can ignore the current component if we're in the elision. */
2597 /* We only add a ':' if we're not at i == 0, or when we're at
2598 * the very end of elision range since the ':' colon was handled
2599 * earlier. Otherwise we would end up with ":::" after elision.
2601 if(i
!= 0 && !(elision_start
> -1 && i
== elision_start
+elision_len
)) {
2603 uri
->canon_uri
[uri
->canon_len
] = ':';
2611 /* Combine the two parts of the IPv4 address values. */
2617 len
= ui2ipv4(uri
->canon_uri
+uri
->canon_len
, val
);
2619 len
= ui2ipv4(NULL
, val
);
2621 uri
->canon_len
+= len
;
2624 /* Write a regular h16 component to the URI. */
2626 /* Short circuit for the trivial case. */
2627 if(values
[i
] == 0) {
2629 uri
->canon_uri
[uri
->canon_len
] = '0';
2632 static const WCHAR formatW
[] = {'%','x',0};
2635 uri
->canon_len
+= sprintfW(uri
->canon_uri
+uri
->canon_len
,
2636 formatW
, values
[i
]);
2639 uri
->canon_len
+= sprintfW(tmp
, formatW
, values
[i
]);
2645 /* Add the closing ']'. */
2647 uri
->canon_uri
[uri
->canon_len
] = ']';
2651 uri
->host_len
= uri
->canon_len
- uri
->host_start
;
2654 TRACE("(%p %p %x %d): Canonicalized IPv6 address %s, len=%d\n", data
, uri
, flags
,
2655 computeOnly
, debugstr_wn(uri
->canon_uri
+uri
->host_start
, uri
->host_len
),
2661 /* Attempts to canonicalize the host of the URI (if any). */
2662 static BOOL
canonicalize_host(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
2663 uri
->host_start
= -1;
2665 uri
->domain_offset
= -1;
2668 switch(data
->host_type
) {
2670 uri
->host_type
= Uri_HOST_DNS
;
2671 if(!canonicalize_reg_name(data
, uri
, flags
, computeOnly
))
2676 uri
->host_type
= Uri_HOST_IPV4
;
2677 if(!canonicalize_ipv4address(data
, uri
, flags
, computeOnly
))
2682 if(!canonicalize_ipv6address(data
, uri
, flags
, computeOnly
))
2685 uri
->host_type
= Uri_HOST_IPV6
;
2687 case Uri_HOST_UNKNOWN
:
2688 if(data
->host_len
> 0 || data
->scheme_type
!= URL_SCHEME_FILE
) {
2689 uri
->host_start
= uri
->canon_len
;
2691 /* Nothing happens to unknown host types. */
2693 memcpy(uri
->canon_uri
+uri
->canon_len
, data
->host
, data
->host_len
*sizeof(WCHAR
));
2694 uri
->canon_len
+= data
->host_len
;
2695 uri
->host_len
= data
->host_len
;
2698 uri
->host_type
= Uri_HOST_UNKNOWN
;
2701 FIXME("(%p %p %x %d): Canonicalization for host type %d not supported.\n", data
,
2702 uri
, flags
, computeOnly
, data
->host_type
);
2710 static BOOL
canonicalize_port(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
2711 BOOL has_default_port
= FALSE
;
2712 USHORT default_port
= 0;
2715 uri
->port_offset
= -1;
2717 /* Check if the scheme has a default port. */
2718 for(i
= 0; i
< sizeof(default_ports
)/sizeof(default_ports
[0]); ++i
) {
2719 if(default_ports
[i
].scheme
== data
->scheme_type
) {
2720 has_default_port
= TRUE
;
2721 default_port
= default_ports
[i
].port
;
2726 uri
->has_port
= data
->has_port
|| has_default_port
;
2729 * 1) Has a port which is the default port.
2730 * 2) Has a port (not the default).
2731 * 3) Doesn't have a port, but, scheme has a default port.
2734 if(has_default_port
&& data
->has_port
&& data
->port_value
== default_port
) {
2735 /* If it's the default port and this flag isn't set, don't do anything. */
2736 if(flags
& Uri_CREATE_NO_CANONICALIZE
) {
2737 uri
->port_offset
= uri
->canon_len
-uri
->authority_start
;
2739 uri
->canon_uri
[uri
->canon_len
] = ':';
2743 /* Copy the original port over. */
2745 memcpy(uri
->canon_uri
+uri
->canon_len
, data
->port
, data
->port_len
*sizeof(WCHAR
));
2746 uri
->canon_len
+= data
->port_len
;
2749 uri
->canon_len
+= ui2str(uri
->canon_uri
+uri
->canon_len
, data
->port_value
);
2751 uri
->canon_len
+= ui2str(NULL
, data
->port_value
);
2755 uri
->port
= default_port
;
2756 } else if(data
->has_port
) {
2757 uri
->port_offset
= uri
->canon_len
-uri
->authority_start
;
2759 uri
->canon_uri
[uri
->canon_len
] = ':';
2762 if(flags
& Uri_CREATE_NO_CANONICALIZE
&& data
->port
) {
2763 /* Copy the original over without changes. */
2765 memcpy(uri
->canon_uri
+uri
->canon_len
, data
->port
, data
->port_len
*sizeof(WCHAR
));
2766 uri
->canon_len
+= data
->port_len
;
2769 uri
->canon_len
+= ui2str(uri
->canon_uri
+uri
->canon_len
, data
->port_value
);
2771 uri
->canon_len
+= ui2str(NULL
, data
->port_value
);
2774 uri
->port
= data
->port_value
;
2775 } else if(has_default_port
)
2776 uri
->port
= default_port
;
2781 /* Canonicalizes the authority of the URI represented by the parse_data. */
2782 static BOOL
canonicalize_authority(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
2783 uri
->authority_start
= uri
->canon_len
;
2784 uri
->authority_len
= 0;
2786 if(!canonicalize_userinfo(data
, uri
, flags
, computeOnly
))
2789 if(!canonicalize_host(data
, uri
, flags
, computeOnly
))
2792 if(!canonicalize_port(data
, uri
, flags
, computeOnly
))
2795 if(uri
->host_start
!= -1 || (data
->is_relative
&& (data
->password
|| data
->username
)))
2796 uri
->authority_len
= uri
->canon_len
- uri
->authority_start
;
2798 uri
->authority_start
= -1;
2803 /* Attempts to canonicalize the path of a hierarchical URI.
2805 * Things that happen:
2806 * 1). Forbidden characters are percent encoded, unless the NO_ENCODE_FORBIDDEN
2807 * flag is set or it's a file URI. Forbidden characters are always encoded
2808 * for file schemes regardless and forbidden characters are never encoded
2809 * for unknown scheme types.
2811 * 2). For known scheme types '\\' are changed to '/'.
2813 * 3). Percent encoded, unreserved characters are decoded to their actual values.
2814 * Unless the scheme type is unknown. For file schemes any percent encoded
2815 * character in the unreserved or reserved set is decoded.
2817 * 4). For File schemes if the path is starts with a drive letter and doesn't
2818 * start with a '/' then one is appended.
2819 * Ex: file://c:/test.mp3 -> file:///c:/test.mp3
2821 * 5). Dot segments are removed from the path for all scheme types
2822 * unless NO_CANONICALIZE flag is set. Dot segments aren't removed
2823 * for wildcard scheme types.
2826 * file://c:/test%20test -> file:///c:/test%2520test
2827 * file://c:/test%3Etest -> file:///c:/test%253Etest
2828 * if Uri_CREATE_FILE_USE_DOS_PATH is not set:
2829 * file:///c:/test%20test -> file:///c:/test%20test
2830 * file:///c:/test%test -> file:///c:/test%25test
2832 static DWORD
canonicalize_path_hierarchical(const WCHAR
*path
, DWORD path_len
, URL_SCHEME scheme_type
, BOOL has_host
, DWORD flags
,
2833 BOOL is_implicit_scheme
, WCHAR
*ret_path
) {
2834 const BOOL known_scheme
= scheme_type
!= URL_SCHEME_UNKNOWN
;
2835 const BOOL is_file
= scheme_type
== URL_SCHEME_FILE
;
2836 const BOOL is_res
= scheme_type
== URL_SCHEME_RES
;
2838 BOOL escape_pct
= FALSE
;
2846 if(is_file
&& !has_host
) {
2847 /* Check if a '/' needs to be appended for the file scheme. */
2848 if(path_len
> 1 && is_drive_path(ptr
) && !(flags
& Uri_CREATE_FILE_USE_DOS_PATH
)) {
2850 ret_path
[len
] = '/';
2853 } else if(*ptr
== '/') {
2854 if(!(flags
& Uri_CREATE_FILE_USE_DOS_PATH
)) {
2855 /* Copy the extra '/' over. */
2857 ret_path
[len
] = '/';
2863 if(is_drive_path(ptr
)) {
2865 ret_path
[len
] = *ptr
;
2866 /* If there's a '|' after the drive letter, convert it to a ':'. */
2867 ret_path
[len
+1] = ':';
2874 if(!is_file
&& *path
&& *path
!= '/') {
2875 /* Prepend a '/' to the path if it doesn't have one. */
2877 ret_path
[len
] = '/';
2881 for(; ptr
< path
+path_len
; ++ptr
) {
2882 BOOL do_default_action
= TRUE
;
2884 if(*ptr
== '%' && !is_res
) {
2885 const WCHAR
*tmp
= ptr
;
2888 /* Check if the % represents a valid encoded char, or if it needs encoding. */
2889 BOOL force_encode
= !check_pct_encoded(&tmp
) && is_file
&& !(flags
&Uri_CREATE_FILE_USE_DOS_PATH
);
2890 val
= decode_pct_val(ptr
);
2892 if(force_encode
|| escape_pct
) {
2893 /* Escape the percent sign in the file URI. */
2895 pct_encode_val(*ptr
, ret_path
+len
);
2897 do_default_action
= FALSE
;
2898 } else if((is_unreserved(val
) && known_scheme
) ||
2899 (is_file
&& !is_implicit_scheme
&& (is_unreserved(val
) || is_reserved(val
) ||
2900 (val
&& flags
&Uri_CREATE_FILE_USE_DOS_PATH
&& !is_forbidden_dos_path_char(val
))))) {
2902 ret_path
[len
] = val
;
2908 } else if(*ptr
== '/' && is_file
&& (flags
& Uri_CREATE_FILE_USE_DOS_PATH
)) {
2909 /* Convert the '/' back to a '\\'. */
2911 ret_path
[len
] = '\\';
2913 do_default_action
= FALSE
;
2914 } else if(*ptr
== '\\' && known_scheme
) {
2915 if(!(is_file
&& (flags
& Uri_CREATE_FILE_USE_DOS_PATH
))) {
2916 /* Convert '\\' into a '/'. */
2918 ret_path
[len
] = '/';
2920 do_default_action
= FALSE
;
2922 } else if(known_scheme
&& !is_res
&& !is_unreserved(*ptr
) && !is_reserved(*ptr
) &&
2923 (!(flags
& Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS
) || is_file
)) {
2924 if(!is_file
|| !(flags
& Uri_CREATE_FILE_USE_DOS_PATH
)) {
2925 /* Escape the forbidden character. */
2927 pct_encode_val(*ptr
, ret_path
+len
);
2929 do_default_action
= FALSE
;
2933 if(do_default_action
) {
2935 ret_path
[len
] = *ptr
;
2940 /* Removing the dot segments only happens when it's not in
2941 * computeOnly mode and it's not a wildcard scheme. File schemes
2942 * with USE_DOS_PATH set don't get dot segments removed.
2944 if(!(is_file
&& (flags
& Uri_CREATE_FILE_USE_DOS_PATH
)) &&
2945 scheme_type
!= URL_SCHEME_WILDCARD
) {
2946 if(!(flags
& Uri_CREATE_NO_CANONICALIZE
) && ret_path
) {
2947 /* Remove the dot segments (if any) and reset everything to the new
2950 len
= remove_dot_segments(ret_path
, len
);
2955 TRACE("Canonicalized path %s len=%d\n", debugstr_wn(ret_path
, len
), len
);
2959 /* Attempts to canonicalize the path for an opaque URI.
2961 * For known scheme types:
2962 * 1) forbidden characters are percent encoded if
2963 * NO_ENCODE_FORBIDDEN_CHARACTERS isn't set.
2965 * 2) Percent encoded, unreserved characters are decoded
2966 * to their actual values, for known scheme types.
2968 * 3) '\\' are changed to '/' for known scheme types
2969 * except for mailto schemes.
2971 * 4) For file schemes, if USE_DOS_PATH is set all '/'
2972 * are converted to backslashes.
2974 * 5) For file schemes, if USE_DOS_PATH isn't set all '\'
2975 * are converted to forward slashes.
2977 static BOOL
canonicalize_path_opaque(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
2979 const BOOL known_scheme
= data
->scheme_type
!= URL_SCHEME_UNKNOWN
;
2980 const BOOL is_file
= data
->scheme_type
== URL_SCHEME_FILE
;
2981 const BOOL is_mk
= data
->scheme_type
== URL_SCHEME_MK
;
2984 uri
->path_start
= -1;
2989 uri
->path_start
= uri
->canon_len
;
2992 /* hijack this flag for SCHEME_MK to tell the function when to start
2993 * converting slashes */
2994 flags
|= Uri_CREATE_FILE_USE_DOS_PATH
;
2997 /* For javascript: URIs, simply copy path part without any canonicalization */
2998 if(data
->scheme_type
== URL_SCHEME_JAVASCRIPT
) {
3000 memcpy(uri
->canon_uri
+uri
->canon_len
, data
->path
, data
->path_len
*sizeof(WCHAR
));
3001 uri
->path_len
= data
->path_len
;
3002 uri
->canon_len
+= data
->path_len
;
3006 /* Windows doesn't allow a "//" to appear after the scheme
3007 * of a URI, if it's an opaque URI.
3009 if(data
->scheme
&& *(data
->path
) == '/' && *(data
->path
+1) == '/') {
3010 /* So it inserts a "/." before the "//" if it exists. */
3012 uri
->canon_uri
[uri
->canon_len
] = '/';
3013 uri
->canon_uri
[uri
->canon_len
+1] = '.';
3016 uri
->canon_len
+= 2;
3019 for(ptr
= data
->path
; ptr
< data
->path
+data
->path_len
; ++ptr
) {
3020 BOOL do_default_action
= TRUE
;
3022 if(*ptr
== '%' && known_scheme
) {
3023 WCHAR val
= decode_pct_val(ptr
);
3025 if(is_unreserved(val
)) {
3027 uri
->canon_uri
[uri
->canon_len
] = val
;
3033 } else if(*ptr
== '/' && is_file
&& (flags
& Uri_CREATE_FILE_USE_DOS_PATH
)) {
3035 uri
->canon_uri
[uri
->canon_len
] = '\\';
3037 do_default_action
= FALSE
;
3038 } else if(*ptr
== '\\') {
3039 if((data
->is_relative
|| is_mk
|| is_file
) && !(flags
& Uri_CREATE_FILE_USE_DOS_PATH
)) {
3040 /* Convert to a '/'. */
3042 uri
->canon_uri
[uri
->canon_len
] = '/';
3044 do_default_action
= FALSE
;
3046 } else if(is_mk
&& *ptr
== ':' && ptr
+ 1 < data
->path
+ data
->path_len
&& *(ptr
+ 1) == ':') {
3047 flags
&= ~Uri_CREATE_FILE_USE_DOS_PATH
;
3048 } else if(known_scheme
&& !is_unreserved(*ptr
) && !is_reserved(*ptr
) &&
3049 !(flags
& Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS
)) {
3050 if(!(is_file
&& (flags
& Uri_CREATE_FILE_USE_DOS_PATH
))) {
3052 pct_encode_val(*ptr
, uri
->canon_uri
+uri
->canon_len
);
3053 uri
->canon_len
+= 3;
3054 do_default_action
= FALSE
;
3058 if(do_default_action
) {
3060 uri
->canon_uri
[uri
->canon_len
] = *ptr
;
3065 if(is_mk
&& !computeOnly
&& !(flags
& Uri_CREATE_NO_CANONICALIZE
)) {
3066 DWORD new_len
= remove_dot_segments(uri
->canon_uri
+ uri
->path_start
,
3067 uri
->canon_len
- uri
->path_start
);
3068 uri
->canon_len
= uri
->path_start
+ new_len
;
3071 uri
->path_len
= uri
->canon_len
- uri
->path_start
;
3074 TRACE("(%p %p %x %d): Canonicalized opaque URI path %s len=%d\n", data
, uri
, flags
, computeOnly
,
3075 debugstr_wn(uri
->canon_uri
+uri
->path_start
, uri
->path_len
), uri
->path_len
);
3079 /* Determines how the URI represented by the parse_data should be canonicalized.
3081 * Essentially, if the parse_data represents an hierarchical URI then it calls
3082 * canonicalize_authority and the canonicalization functions for the path. If the
3083 * URI is opaque it canonicalizes the path of the URI.
3085 static BOOL
canonicalize_hierpart(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
3086 if(!data
->is_opaque
|| (data
->is_relative
&& (data
->password
|| data
->username
))) {
3087 /* "//" is only added for non-wildcard scheme types.
3089 * A "//" is only added to a relative URI if it has a
3090 * host or port component (this only happens if a IUriBuilder
3091 * is generating an IUri).
3093 if((data
->is_relative
&& (data
->host
|| data
->has_port
)) ||
3094 (!data
->is_relative
&& data
->scheme_type
!= URL_SCHEME_WILDCARD
)) {
3095 if(data
->scheme_type
== URL_SCHEME_WILDCARD
)
3099 INT pos
= uri
->canon_len
;
3101 uri
->canon_uri
[pos
] = '/';
3102 uri
->canon_uri
[pos
+1] = '/';
3104 uri
->canon_len
+= 2;
3107 if(!canonicalize_authority(data
, uri
, flags
, computeOnly
))
3110 if(data
->is_relative
&& (data
->password
|| data
->username
)) {
3111 if(!canonicalize_path_opaque(data
, uri
, flags
, computeOnly
))
3115 uri
->path_start
= uri
->canon_len
;
3116 uri
->path_len
= canonicalize_path_hierarchical(data
->path
, data
->path_len
, data
->scheme_type
, data
->host_len
!= 0,
3117 flags
, data
->has_implicit_scheme
, computeOnly
? NULL
: uri
->canon_uri
+uri
->canon_len
);
3118 uri
->canon_len
+= uri
->path_len
;
3119 if(!computeOnly
&& !uri
->path_len
)
3120 uri
->path_start
= -1;
3123 /* Opaque URI's don't have an authority. */
3124 uri
->userinfo_start
= uri
->userinfo_split
= -1;
3125 uri
->userinfo_len
= 0;
3126 uri
->host_start
= -1;
3128 uri
->host_type
= Uri_HOST_UNKNOWN
;
3129 uri
->has_port
= FALSE
;
3130 uri
->authority_start
= -1;
3131 uri
->authority_len
= 0;
3132 uri
->domain_offset
= -1;
3133 uri
->port_offset
= -1;
3135 if(is_hierarchical_scheme(data
->scheme_type
)) {
3138 /* Absolute URIs aren't displayed for known scheme types
3139 * which should be hierarchical URIs.
3141 uri
->display_modifiers
|= URI_DISPLAY_NO_ABSOLUTE_URI
;
3143 /* Windows also sets the port for these (if they have one). */
3144 for(i
= 0; i
< sizeof(default_ports
)/sizeof(default_ports
[0]); ++i
) {
3145 if(data
->scheme_type
== default_ports
[i
].scheme
) {