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 #define URI_DISPLAY_NO_ABSOLUTE_URI 0x1
30 #define URI_DISPLAY_NO_DEFAULT_PORT_AUTH 0x2
32 #define ALLOW_NULL_TERM_SCHEME 0x01
33 #define ALLOW_NULL_TERM_USER_NAME 0x02
34 #define ALLOW_NULL_TERM_PASSWORD 0x04
35 #define ALLOW_BRACKETLESS_IP_LITERAL 0x08
36 #define SKIP_IP_FUTURE_CHECK 0x10
37 #define IGNORE_PORT_DELIMITER 0x20
39 #define RAW_URI_FORCE_PORT_DISP 0x1
41 WINE_DEFAULT_DEBUG_CHANNEL(urlmon
);
43 static const IID IID_IUriObj
= {0x4b364760,0x9f51,0x11df,{0x98,0x1c,0x08,0x00,0x20,0x0c,0x9a,0x66}};
46 const IUriVtbl
*lpIUriVtbl
;
51 /* Information about the canonicalized URI's buffer. */
55 BOOL display_modifiers
;
60 URL_SCHEME scheme_type
;
68 Uri_HOST_TYPE host_type
;
91 const IUriBuilderVtbl
*lpIUriBuilderVtbl
;
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
;
154 URL_SCHEME scheme_type
;
156 const WCHAR
*username
;
159 const WCHAR
*password
;
164 Uri_HOST_TYPE host_type
;
167 ipv6_address ipv6_address
;
180 const WCHAR
*fragment
;
184 static const CHAR hexDigits
[] = "0123456789ABCDEF";
186 /* List of scheme types/scheme names that are recognized by the IUri interface as of IE 7. */
187 static const struct {
189 WCHAR scheme_name
[16];
190 } recognized_schemes
[] = {
191 {URL_SCHEME_FTP
, {'f','t','p',0}},
192 {URL_SCHEME_HTTP
, {'h','t','t','p',0}},
193 {URL_SCHEME_GOPHER
, {'g','o','p','h','e','r',0}},
194 {URL_SCHEME_MAILTO
, {'m','a','i','l','t','o',0}},
195 {URL_SCHEME_NEWS
, {'n','e','w','s',0}},
196 {URL_SCHEME_NNTP
, {'n','n','t','p',0}},
197 {URL_SCHEME_TELNET
, {'t','e','l','n','e','t',0}},
198 {URL_SCHEME_WAIS
, {'w','a','i','s',0}},
199 {URL_SCHEME_FILE
, {'f','i','l','e',0}},
200 {URL_SCHEME_MK
, {'m','k',0}},
201 {URL_SCHEME_HTTPS
, {'h','t','t','p','s',0}},
202 {URL_SCHEME_SHELL
, {'s','h','e','l','l',0}},
203 {URL_SCHEME_SNEWS
, {'s','n','e','w','s',0}},
204 {URL_SCHEME_LOCAL
, {'l','o','c','a','l',0}},
205 {URL_SCHEME_JAVASCRIPT
, {'j','a','v','a','s','c','r','i','p','t',0}},
206 {URL_SCHEME_VBSCRIPT
, {'v','b','s','c','r','i','p','t',0}},
207 {URL_SCHEME_ABOUT
, {'a','b','o','u','t',0}},
208 {URL_SCHEME_RES
, {'r','e','s',0}},
209 {URL_SCHEME_MSSHELLROOTED
, {'m','s','-','s','h','e','l','l','-','r','o','o','t','e','d',0}},
210 {URL_SCHEME_MSSHELLIDLIST
, {'m','s','-','s','h','e','l','l','-','i','d','l','i','s','t',0}},
211 {URL_SCHEME_MSHELP
, {'h','c','p',0}},
212 {URL_SCHEME_WILDCARD
, {'*',0}}
215 /* List of default ports Windows recognizes. */
216 static const struct {
219 } default_ports
[] = {
220 {URL_SCHEME_FTP
, 21},
221 {URL_SCHEME_HTTP
, 80},
222 {URL_SCHEME_GOPHER
, 70},
223 {URL_SCHEME_NNTP
, 119},
224 {URL_SCHEME_TELNET
, 23},
225 {URL_SCHEME_WAIS
, 210},
226 {URL_SCHEME_HTTPS
, 443},
229 /* List of 3 character top level domain names Windows seems to recognize.
230 * There might be more, but, these are the only ones I've found so far.
232 static const struct {
234 } recognized_tlds
[] = {
244 static Uri
*get_uri_obj(IUri
*uri
)
249 hres
= IUri_QueryInterface(uri
, &IID_IUriObj
, (void**)&ret
);
250 return SUCCEEDED(hres
) ? ret
: NULL
;
253 static inline BOOL
is_alpha(WCHAR val
) {
254 return ((val
>= 'a' && val
<= 'z') || (val
>= 'A' && val
<= 'Z'));
257 static inline BOOL
is_num(WCHAR val
) {
258 return (val
>= '0' && val
<= '9');
261 static inline BOOL
is_drive_path(const WCHAR
*str
) {
262 return (is_alpha(str
[0]) && (str
[1] == ':' || str
[1] == '|'));
265 static inline BOOL
is_unc_path(const WCHAR
*str
) {
266 return (str
[0] == '\\' && str
[0] == '\\');
269 static inline BOOL
is_forbidden_dos_path_char(WCHAR val
) {
270 return (val
== '>' || val
== '<' || val
== '\"');
273 /* A URI is implicitly a file path if it begins with
274 * a drive letter (eg X:) or starts with "\\" (UNC path).
276 static inline BOOL
is_implicit_file_path(const WCHAR
*str
) {
277 return (is_unc_path(str
) || (is_alpha(str
[0]) && str
[1] == ':'));
280 /* Checks if the URI is a hierarchical URI. A hierarchical
281 * URI is one that has "//" after the scheme.
283 static BOOL
check_hierarchical(const WCHAR
**ptr
) {
284 const WCHAR
*start
= *ptr
;
299 /* unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~" */
300 static inline BOOL
is_unreserved(WCHAR val
) {
301 return (is_alpha(val
) || is_num(val
) || val
== '-' || val
== '.' ||
302 val
== '_' || val
== '~');
305 /* sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
306 * / "*" / "+" / "," / ";" / "="
308 static inline BOOL
is_subdelim(WCHAR val
) {
309 return (val
== '!' || val
== '$' || val
== '&' ||
310 val
== '\'' || val
== '(' || val
== ')' ||
311 val
== '*' || val
== '+' || val
== ',' ||
312 val
== ';' || val
== '=');
315 /* gen-delims = ":" / "/" / "?" / "#" / "[" / "]" / "@" */
316 static inline BOOL
is_gendelim(WCHAR val
) {
317 return (val
== ':' || val
== '/' || val
== '?' ||
318 val
== '#' || val
== '[' || val
== ']' ||
322 /* Characters that delimit the end of the authority
323 * section of a URI. Sometimes a '\\' is considered
324 * an authority delimeter.
326 static inline BOOL
is_auth_delim(WCHAR val
, BOOL acceptSlash
) {
327 return (val
== '#' || val
== '/' || val
== '?' ||
328 val
== '\0' || (acceptSlash
&& val
== '\\'));
331 /* reserved = gen-delims / sub-delims */
332 static inline BOOL
is_reserved(WCHAR val
) {
333 return (is_subdelim(val
) || is_gendelim(val
));
336 static inline BOOL
is_hexdigit(WCHAR val
) {
337 return ((val
>= 'a' && val
<= 'f') ||
338 (val
>= 'A' && val
<= 'F') ||
339 (val
>= '0' && val
<= '9'));
342 static inline BOOL
is_path_delim(WCHAR val
) {
343 return (!val
|| val
== '#' || val
== '?');
346 static BOOL
is_default_port(URL_SCHEME scheme
, DWORD port
) {
349 for(i
= 0; i
< sizeof(default_ports
)/sizeof(default_ports
[0]); ++i
) {
350 if(default_ports
[i
].scheme
== scheme
&& default_ports
[i
].port
)
357 /* List of schemes types Windows seems to expect to be hierarchical. */
358 static inline BOOL
is_hierarchical_scheme(URL_SCHEME type
) {
359 return(type
== URL_SCHEME_HTTP
|| type
== URL_SCHEME_FTP
||
360 type
== URL_SCHEME_GOPHER
|| type
== URL_SCHEME_NNTP
||
361 type
== URL_SCHEME_TELNET
|| type
== URL_SCHEME_WAIS
||
362 type
== URL_SCHEME_FILE
|| type
== URL_SCHEME_HTTPS
||
363 type
== URL_SCHEME_RES
);
366 /* Checks if 'flags' contains an invalid combination of Uri_CREATE flags. */
367 static inline BOOL
has_invalid_flag_combination(DWORD flags
) {
368 return((flags
& Uri_CREATE_DECODE_EXTRA_INFO
&& flags
& Uri_CREATE_NO_DECODE_EXTRA_INFO
) ||
369 (flags
& Uri_CREATE_CANONICALIZE
&& flags
& Uri_CREATE_NO_CANONICALIZE
) ||
370 (flags
& Uri_CREATE_CRACK_UNKNOWN_SCHEMES
&& flags
& Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES
) ||
371 (flags
& Uri_CREATE_PRE_PROCESS_HTML_URI
&& flags
& Uri_CREATE_NO_PRE_PROCESS_HTML_URI
) ||
372 (flags
& Uri_CREATE_IE_SETTINGS
&& flags
& Uri_CREATE_NO_IE_SETTINGS
));
375 /* Applies each default Uri_CREATE flags to 'flags' if it
376 * doesn't cause a flag conflict.
378 static void apply_default_flags(DWORD
*flags
) {
379 if(!(*flags
& Uri_CREATE_NO_CANONICALIZE
))
380 *flags
|= Uri_CREATE_CANONICALIZE
;
381 if(!(*flags
& Uri_CREATE_NO_DECODE_EXTRA_INFO
))
382 *flags
|= Uri_CREATE_DECODE_EXTRA_INFO
;
383 if(!(*flags
& Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES
))
384 *flags
|= Uri_CREATE_CRACK_UNKNOWN_SCHEMES
;
385 if(!(*flags
& Uri_CREATE_NO_PRE_PROCESS_HTML_URI
))
386 *flags
|= Uri_CREATE_PRE_PROCESS_HTML_URI
;
387 if(!(*flags
& Uri_CREATE_IE_SETTINGS
))
388 *flags
|= Uri_CREATE_NO_IE_SETTINGS
;
391 /* Determines if the URI is hierarchical using the information already parsed into
392 * data and using the current location of parsing in the URI string.
394 * Windows considers a URI hierarchical if on of the following is true:
395 * A.) It's a wildcard scheme.
396 * B.) It's an implicit file scheme.
397 * C.) It's a known hierarchical scheme and it has two '\\' after the scheme name.
398 * (the '\\' will be converted into "//" during canonicalization).
399 * D.) It's not a relative URI and "//" appears after the scheme name.
401 static inline BOOL
is_hierarchical_uri(const WCHAR
**ptr
, const parse_data
*data
) {
402 const WCHAR
*start
= *ptr
;
404 if(data
->scheme_type
== URL_SCHEME_WILDCARD
)
406 else if(data
->scheme_type
== URL_SCHEME_FILE
&& data
->has_implicit_scheme
)
408 else if(is_hierarchical_scheme(data
->scheme_type
) && (*ptr
)[0] == '\\' && (*ptr
)[1] == '\\') {
411 } else if(!data
->is_relative
&& check_hierarchical(ptr
))
418 /* Checks if the two Uri's are logically equivalent. It's a simple
419 * comparison, since they are both of type Uri, and it can access
420 * the properties of each Uri directly without the need to go
421 * through the "IUri_Get*" interface calls.
423 static BOOL
are_equal_simple(const Uri
*a
, const Uri
*b
) {
424 if(a
->scheme_type
== b
->scheme_type
) {
425 const BOOL known_scheme
= a
->scheme_type
!= URL_SCHEME_UNKNOWN
;
426 const BOOL are_hierarchical
=
427 (a
->authority_start
> -1 && b
->authority_start
> -1);
429 if(a
->scheme_type
== URL_SCHEME_FILE
) {
430 if(a
->canon_len
== b
->canon_len
)
431 return !StrCmpIW(a
->canon_uri
, b
->canon_uri
);
434 /* Only compare the scheme names (if any) if their unknown scheme types. */
436 if((a
->scheme_start
> -1 && b
->scheme_start
> -1) &&
437 (a
->scheme_len
== b
->scheme_len
)) {
438 /* Make sure the schemes are the same. */
439 if(StrCmpNW(a
->canon_uri
+a
->scheme_start
, b
->canon_uri
+b
->scheme_start
, a
->scheme_len
))
441 } else if(a
->scheme_len
!= b
->scheme_len
)
442 /* One of the Uri's has a scheme name, while the other doesn't. */
446 /* If they have a userinfo component, perform case sensitive compare. */
447 if((a
->userinfo_start
> -1 && b
->userinfo_start
> -1) &&
448 (a
->userinfo_len
== b
->userinfo_len
)) {
449 if(StrCmpNW(a
->canon_uri
+a
->userinfo_start
, b
->canon_uri
+b
->userinfo_start
, a
->userinfo_len
))
451 } else if(a
->userinfo_len
!= b
->userinfo_len
)
452 /* One of the Uri's had a userinfo, while the other one doesn't. */
455 /* Check if they have a host name. */
456 if((a
->host_start
> -1 && b
->host_start
> -1) &&
457 (a
->host_len
== b
->host_len
)) {
458 /* Perform a case insensitive compare if they are a known scheme type. */
460 if(StrCmpNIW(a
->canon_uri
+a
->host_start
, b
->canon_uri
+b
->host_start
, a
->host_len
))
462 } else if(StrCmpNW(a
->canon_uri
+a
->host_start
, b
->canon_uri
+b
->host_start
, a
->host_len
))
464 } else if(a
->host_len
!= b
->host_len
)
465 /* One of the Uri's had a host, while the other one didn't. */
468 if(a
->has_port
&& b
->has_port
) {
469 if(a
->port
!= b
->port
)
471 } else if(a
->has_port
|| b
->has_port
)
472 /* One had a port, while the other one didn't. */
475 /* Windows is weird with how it handles paths. For example
476 * One URI could be "http://google.com" (after canonicalization)
477 * and one could be "http://google.com/" and the IsEqual function
478 * would still evaluate to TRUE, but, only if they are both hierarchical
481 if((a
->path_start
> -1 && b
->path_start
> -1) &&
482 (a
->path_len
== b
->path_len
)) {
483 if(StrCmpNW(a
->canon_uri
+a
->path_start
, b
->canon_uri
+b
->path_start
, a
->path_len
))
485 } else if(are_hierarchical
&& a
->path_len
== -1 && b
->path_len
== 0) {
486 if(*(a
->canon_uri
+a
->path_start
) != '/')
488 } else if(are_hierarchical
&& b
->path_len
== 1 && a
->path_len
== 0) {
489 if(*(b
->canon_uri
+b
->path_start
) != '/')
491 } else if(a
->path_len
!= b
->path_len
)
494 /* Compare the query strings of the two URIs. */
495 if((a
->query_start
> -1 && b
->query_start
> -1) &&
496 (a
->query_len
== b
->query_len
)) {
497 if(StrCmpNW(a
->canon_uri
+a
->query_start
, b
->canon_uri
+b
->query_start
, a
->query_len
))
499 } else if(a
->query_len
!= b
->query_len
)
502 if((a
->fragment_start
> -1 && b
->fragment_start
> -1) &&
503 (a
->fragment_len
== b
->fragment_len
)) {
504 if(StrCmpNW(a
->canon_uri
+a
->fragment_start
, b
->canon_uri
+b
->fragment_start
, a
->fragment_len
))
506 } else if(a
->fragment_len
!= b
->fragment_len
)
509 /* If we get here, the two URIs are equivalent. */
516 /* Computes the size of the given IPv6 address.
517 * Each h16 component is 16bits, if there is an IPv4 address, it's
518 * 32bits. If there's an elision it can be 16bits to 128bits, depending
519 * on the number of other components.
521 * Modeled after google-url's CheckIPv6ComponentsSize function
523 static void compute_ipv6_comps_size(ipv6_address
*address
) {
524 address
->components_size
= address
->h16_count
* 2;
527 /* IPv4 address is 4 bytes. */
528 address
->components_size
+= 4;
530 if(address
->elision
) {
531 /* An elision can be anywhere from 2 bytes up to 16 bytes.
532 * It size depends on the size of the h16 and IPv4 components.
534 address
->elision_size
= 16 - address
->components_size
;
535 if(address
->elision_size
< 2)
536 address
->elision_size
= 2;
538 address
->elision_size
= 0;
541 /* Taken from dlls/jscript/lex.c */
542 static int hex_to_int(WCHAR val
) {
543 if(val
>= '0' && val
<= '9')
545 else if(val
>= 'a' && val
<= 'f')
546 return val
- 'a' + 10;
547 else if(val
>= 'A' && val
<= 'F')
548 return val
- 'A' + 10;
553 /* Helper function for converting a percent encoded string
554 * representation of a WCHAR value into its actual WCHAR value. If
555 * the two characters following the '%' aren't valid hex values then
556 * this function returns the NULL character.
559 * "%2E" will result in '.' being returned by this function.
561 static WCHAR
decode_pct_val(const WCHAR
*ptr
) {
564 if(*ptr
== '%' && is_hexdigit(*(ptr
+ 1)) && is_hexdigit(*(ptr
+ 2))) {
565 INT a
= hex_to_int(*(ptr
+ 1));
566 INT b
= hex_to_int(*(ptr
+ 2));
575 /* Helper function for percent encoding a given character
576 * and storing the encoded value into a given buffer (dest).
578 * It's up to the calling function to ensure that there is
579 * at least enough space in 'dest' for the percent encoded
580 * value to be stored (so dest + 3 spaces available).
582 static inline void pct_encode_val(WCHAR val
, WCHAR
*dest
) {
584 dest
[1] = hexDigits
[(val
>> 4) & 0xf];
585 dest
[2] = hexDigits
[val
& 0xf];
588 /* Scans the range of characters [str, end] and returns the last occurrence
589 * of 'ch' or returns NULL.
591 static const WCHAR
*str_last_of(const WCHAR
*str
, const WCHAR
*end
, WCHAR ch
) {
592 const WCHAR
*ptr
= end
;
603 /* Attempts to parse the domain name from the host.
605 * This function also includes the Top-level Domain (TLD) name
606 * of the host when it tries to find the domain name. If it finds
607 * a valid domain name it will assign 'domain_start' the offset
608 * into 'host' where the domain name starts.
610 * It's implied that if a domain name its range is implied to be
611 * [host+domain_start, host+host_len).
613 static void find_domain_name(const WCHAR
*host
, DWORD host_len
,
615 const WCHAR
*last_tld
, *sec_last_tld
, *end
;
617 end
= host
+host_len
-1;
621 /* There has to be at least enough room for a '.' followed by a
622 * 3 character TLD for a domain to even exist in the host name.
627 last_tld
= str_last_of(host
, end
, '.');
629 /* http://hostname -> has no domain name. */
632 sec_last_tld
= str_last_of(host
, last_tld
-1, '.');
634 /* If the '.' is at the beginning of the host there
635 * has to be at least 3 characters in the TLD for it
637 * Ex: .com -> .com as the domain name.
638 * .co -> has no domain name.
640 if(last_tld
-host
== 0) {
641 if(end
-(last_tld
-1) < 3)
643 } else if(last_tld
-host
== 3) {
646 /* If there's three characters in front of last_tld and
647 * they are on the list of recognized TLDs, then this
648 * host doesn't have a domain (since the host only contains
650 * Ex: edu.uk -> has no domain name.
651 * foo.uk -> foo.uk as the domain name.
653 for(i
= 0; i
< sizeof(recognized_tlds
)/sizeof(recognized_tlds
[0]); ++i
) {
654 if(!StrCmpNIW(host
, recognized_tlds
[i
].tld_name
, 3))
657 } else if(last_tld
-host
< 3)
658 /* Anything less than 3 characters is considered part
660 * Ex: ak.uk -> Has no domain name.
664 /* Otherwise the domain name is the whole host name. */
666 } else if(end
+1-last_tld
> 3) {
667 /* If the last_tld has more than 3 characters, then it's automatically
668 * considered the TLD of the domain name.
669 * Ex: www.winehq.org.uk.test -> uk.test as the domain name.
671 *domain_start
= (sec_last_tld
+1)-host
;
672 } else if(last_tld
- (sec_last_tld
+1) < 4) {
674 /* If the sec_last_tld is 3 characters long it HAS to be on the list of
675 * recognized to still be considered part of the TLD name, otherwise
676 * its considered the domain name.
677 * Ex: www.google.com.uk -> google.com.uk as the domain name.
678 * www.google.foo.uk -> foo.uk as the domain name.
680 if(last_tld
- (sec_last_tld
+1) == 3) {
681 for(i
= 0; i
< sizeof(recognized_tlds
)/sizeof(recognized_tlds
[0]); ++i
) {
682 if(!StrCmpNIW(sec_last_tld
+1, recognized_tlds
[i
].tld_name
, 3)) {
683 const WCHAR
*domain
= str_last_of(host
, sec_last_tld
-1, '.');
688 *domain_start
= (domain
+1) - host
;
689 TRACE("Found domain name %s\n", debugstr_wn(host
+*domain_start
,
690 (host
+host_len
)-(host
+*domain_start
)));
695 *domain_start
= (sec_last_tld
+1)-host
;
697 /* Since the sec_last_tld is less than 3 characters it's considered
699 * Ex: www.google.fo.uk -> google.fo.uk as the domain name.
701 const WCHAR
*domain
= str_last_of(host
, sec_last_tld
-1, '.');
706 *domain_start
= (domain
+1) - host
;
709 /* The second to last TLD has more than 3 characters making it
711 * Ex: www.google.test.us -> test.us as the domain name.
713 *domain_start
= (sec_last_tld
+1)-host
;
716 TRACE("Found domain name %s\n", debugstr_wn(host
+*domain_start
,
717 (host
+host_len
)-(host
+*domain_start
)));
720 /* Removes the dot segments from a hierarchical URIs path component. This
721 * function performs the removal in place.
723 * This is a modified version of Qt's QUrl function "removeDotsFromPath".
725 * This function returns the new length of the path string.
727 static DWORD
remove_dot_segments(WCHAR
*path
, DWORD path_len
) {
729 const WCHAR
*in
= out
;
730 const WCHAR
*end
= out
+ path_len
;
734 /* A. if the input buffer begins with a prefix of "/./" or "/.",
735 * where "." is a complete path segment, then replace that
736 * prefix with "/" in the input buffer; otherwise,
738 if(in
<= end
- 3 && in
[0] == '/' && in
[1] == '.' && in
[2] == '/') {
741 } else if(in
== end
- 2 && in
[0] == '/' && in
[1] == '.') {
747 /* B. if the input buffer begins with a prefix of "/../" or "/..",
748 * where ".." is a complete path segment, then replace that
749 * prefix with "/" in the input buffer and remove the last
750 * segment and its preceding "/" (if any) from the output
753 if(in
<= end
- 4 && in
[0] == '/' && in
[1] == '.' && in
[2] == '.' && in
[3] == '/') {
754 while(out
> path
&& *(--out
) != '/');
758 } else if(in
== end
- 3 && in
[0] == '/' && in
[1] == '.' && in
[2] == '.') {
759 while(out
> path
&& *(--out
) != '/');
768 /* C. move the first path segment in the input buffer to the end of
769 * the output buffer, including the initial "/" character (if
770 * any) and any subsequent characters up to, but not including,
771 * the next "/" character or the end of the input buffer.
774 while(in
< end
&& *in
!= '/')
779 TRACE("(%p %d): Path after dot segments removed %s len=%d\n", path
, path_len
,
780 debugstr_wn(path
, len
), len
);
784 /* Attempts to find the file extension in a given path. */
785 static INT
find_file_extension(const WCHAR
*path
, DWORD path_len
) {
788 for(end
= path
+path_len
-1; end
>= path
&& *end
!= '/' && *end
!= '\\'; --end
) {
796 /* Computes the location where the elision should occur in the IPv6
797 * address using the numerical values of each component stored in
798 * 'values'. If the address shouldn't contain an elision then 'index'
799 * is assigned -1 as it's value. Otherwise 'index' will contain the
800 * starting index (into values) where the elision should be, and 'count'
801 * will contain the number of cells the elision covers.
804 * Windows will expand an elision if the elision only represents 1 h16
805 * component of the URI.
807 * Ex: [1::2:3:4:5:6:7] -> [1:0:2:3:4:5:6:7]
809 * If the IPv6 address contains an IPv4 address, the IPv4 address is also
810 * considered for being included as part of an elision if all it's components
813 * Ex: [1:2:3:4:5:6:0.0.0.0] -> [1:2:3:4:5:6::]
815 static void compute_elision_location(const ipv6_address
*address
, const USHORT values
[8],
816 INT
*index
, DWORD
*count
) {
817 DWORD i
, max_len
, cur_len
;
818 INT max_index
, cur_index
;
820 max_len
= cur_len
= 0;
821 max_index
= cur_index
= -1;
822 for(i
= 0; i
< 8; ++i
) {
823 BOOL check_ipv4
= (address
->ipv4
&& i
== 6);
824 BOOL is_end
= (check_ipv4
|| i
== 7);
827 /* Check if the IPv4 address contains only zeros. */
828 if(values
[i
] == 0 && values
[i
+1] == 0) {
835 } else if(values
[i
] == 0) {
842 if(is_end
|| values
[i
] != 0) {
843 /* We only consider it for an elision if it's
844 * more than 1 component long.
846 if(cur_len
> 1 && cur_len
> max_len
) {
847 /* Found the new elision location. */
849 max_index
= cur_index
;
852 /* Reset the current range for the next range of zeros. */
862 /* Removes all the leading and trailing white spaces or
863 * control characters from the URI and removes all control
864 * characters inside of the URI string.
866 static BSTR
pre_process_uri(LPCWSTR uri
) {
869 const WCHAR
*start
, *end
;
875 /* Skip leading controls and whitespace. */
876 while(iscntrlW(*start
) || isspaceW(*start
)) ++start
;
880 /* URI consisted only of control/whitespace. */
881 ret
= SysAllocStringLen(NULL
, 0);
883 while(iscntrlW(*end
) || isspaceW(*end
)) --end
;
885 buf
= heap_alloc(((end
+1)-start
)*sizeof(WCHAR
));
889 for(ptr
= buf
; start
< end
+1; ++start
) {
890 if(!iscntrlW(*start
))
894 ret
= SysAllocStringLen(buf
, ptr
-buf
);
901 /* Converts the specified IPv4 address into an uint value.
903 * This function assumes that the IPv4 address has already been validated.
905 static UINT
ipv4toui(const WCHAR
*ip
, DWORD len
) {
907 DWORD comp_value
= 0;
910 for(ptr
= ip
; ptr
< ip
+len
; ++ptr
) {
916 comp_value
= comp_value
*10 + (*ptr
-'0');
925 /* Converts an IPv4 address in numerical form into it's fully qualified
926 * string form. This function returns the number of characters written
927 * to 'dest'. If 'dest' is NULL this function will return the number of
928 * characters that would have been written.
930 * It's up to the caller to ensure there's enough space in 'dest' for the
933 static DWORD
ui2ipv4(WCHAR
*dest
, UINT address
) {
934 static const WCHAR formatW
[] =
935 {'%','u','.','%','u','.','%','u','.','%','u',0};
939 digits
[0] = (address
>> 24) & 0xff;
940 digits
[1] = (address
>> 16) & 0xff;
941 digits
[2] = (address
>> 8) & 0xff;
942 digits
[3] = address
& 0xff;
946 ret
= sprintfW(tmp
, formatW
, digits
[0], digits
[1], digits
[2], digits
[3]);
948 ret
= sprintfW(dest
, formatW
, digits
[0], digits
[1], digits
[2], digits
[3]);
953 static DWORD
ui2str(WCHAR
*dest
, UINT value
) {
954 static const WCHAR formatW
[] = {'%','u',0};
959 ret
= sprintfW(tmp
, formatW
, value
);
961 ret
= sprintfW(dest
, formatW
, value
);
966 /* Converts an h16 component (from an IPv6 address) into it's
969 * This function assumes that the h16 component has already been validated.
971 static USHORT
h16tous(h16 component
) {
975 for(i
= 0; i
< component
.len
; ++i
) {
977 ret
+= hex_to_int(component
.str
[i
]);
983 /* Converts an IPv6 address into it's 128 bits (16 bytes) numerical value.
985 * This function assumes that the ipv6_address has already been validated.
987 static BOOL
ipv6_to_number(const ipv6_address
*address
, USHORT number
[8]) {
988 DWORD i
, cur_component
= 0;
989 BOOL already_passed_elision
= FALSE
;
991 for(i
= 0; i
< address
->h16_count
; ++i
) {
992 if(address
->elision
) {
993 if(address
->components
[i
].str
> address
->elision
&& !already_passed_elision
) {
994 /* Means we just passed the elision and need to add it's values to
995 * 'number' before we do anything else.
998 for(j
= 0; j
< address
->elision_size
; j
+=2)
999 number
[cur_component
++] = 0;
1001 already_passed_elision
= TRUE
;
1005 number
[cur_component
++] = h16tous(address
->components
[i
]);
1008 /* Case when the elision appears after the h16 components. */
1009 if(!already_passed_elision
&& address
->elision
) {
1010 for(i
= 0; i
< address
->elision_size
; i
+=2)
1011 number
[cur_component
++] = 0;
1012 already_passed_elision
= TRUE
;
1016 UINT value
= ipv4toui(address
->ipv4
, address
->ipv4_len
);
1018 if(cur_component
!= 6) {
1019 ERR("(%p %p): Failed sanity check with %d\n", address
, number
, cur_component
);
1023 number
[cur_component
++] = (value
>> 16) & 0xffff;
1024 number
[cur_component
] = value
& 0xffff;
1030 /* Checks if the characters pointed to by 'ptr' are
1031 * a percent encoded data octet.
1033 * pct-encoded = "%" HEXDIG HEXDIG
1035 static BOOL
check_pct_encoded(const WCHAR
**ptr
) {
1036 const WCHAR
*start
= *ptr
;
1042 if(!is_hexdigit(**ptr
)) {
1048 if(!is_hexdigit(**ptr
)) {
1057 /* dec-octet = DIGIT ; 0-9
1058 * / %x31-39 DIGIT ; 10-99
1059 * / "1" 2DIGIT ; 100-199
1060 * / "2" %x30-34 DIGIT ; 200-249
1061 * / "25" %x30-35 ; 250-255
1063 static BOOL
check_dec_octet(const WCHAR
**ptr
) {
1064 const WCHAR
*c1
, *c2
, *c3
;
1067 /* A dec-octet must be at least 1 digit long. */
1068 if(*c1
< '0' || *c1
> '9')
1074 /* Since the 1 digit requirment was meet, it doesn't
1075 * matter if this is a DIGIT value, it's considered a
1078 if(*c2
< '0' || *c2
> '9')
1084 /* Same explanation as above. */
1085 if(*c3
< '0' || *c3
> '9')
1088 /* Anything > 255 isn't a valid IP dec-octet. */
1089 if(*c1
>= '2' && *c2
>= '5' && *c3
>= '5') {
1098 /* Checks if there is an implicit IPv4 address in the host component of the URI.
1099 * The max value of an implicit IPv4 address is UINT_MAX.
1102 * "234567" would be considered an implicit IPv4 address.
1104 static BOOL
check_implicit_ipv4(const WCHAR
**ptr
, UINT
*val
) {
1105 const WCHAR
*start
= *ptr
;
1109 while(is_num(**ptr
)) {
1110 ret
= ret
*10 + (**ptr
- '0');
1112 if(ret
> UINT_MAX
) {
1126 /* Checks if the string contains an IPv4 address.
1128 * This function has a strict mode or a non-strict mode of operation
1129 * When 'strict' is set to FALSE this function will return TRUE if
1130 * the string contains at least 'dec-octet "." dec-octet' since partial
1131 * IPv4 addresses will be normalized out into full IPv4 addresses. When
1132 * 'strict' is set this function expects there to be a full IPv4 address.
1134 * IPv4address = dec-octet "." dec-octet "." dec-octet "." dec-octet
1136 static BOOL
check_ipv4address(const WCHAR
**ptr
, BOOL strict
) {
1137 const WCHAR
*start
= *ptr
;
1139 if(!check_dec_octet(ptr
)) {
1150 if(!check_dec_octet(ptr
)) {
1164 if(!check_dec_octet(ptr
)) {
1178 if(!check_dec_octet(ptr
)) {
1183 /* Found a four digit ip address. */
1186 /* Tries to parse the scheme name of the URI.
1188 * scheme = ALPHA *(ALPHA | NUM | '+' | '-' | '.') as defined by RFC 3896.
1189 * NOTE: Windows accepts a number as the first character of a scheme.
1191 static BOOL
parse_scheme_name(const WCHAR
**ptr
, parse_data
*data
, DWORD extras
) {
1192 const WCHAR
*start
= *ptr
;
1194 data
->scheme
= NULL
;
1195 data
->scheme_len
= 0;
1198 if(**ptr
== '*' && *ptr
== start
) {
1199 /* Might have found a wildcard scheme. If it is the next
1200 * char has to be a ':' for it to be a valid URI
1204 } else if(!is_num(**ptr
) && !is_alpha(**ptr
) && **ptr
!= '+' &&
1205 **ptr
!= '-' && **ptr
!= '.')
1214 /* Schemes must end with a ':' */
1215 if(**ptr
!= ':' && !((extras
& ALLOW_NULL_TERM_SCHEME
) && !**ptr
)) {
1220 data
->scheme
= start
;
1221 data
->scheme_len
= *ptr
- start
;
1227 /* Tries to deduce the corresponding URL_SCHEME for the given URI. Stores
1228 * the deduced URL_SCHEME in data->scheme_type.
1230 static BOOL
parse_scheme_type(parse_data
*data
) {
1231 /* If there's scheme data then see if it's a recognized scheme. */
1232 if(data
->scheme
&& data
->scheme_len
) {
1235 for(i
= 0; i
< sizeof(recognized_schemes
)/sizeof(recognized_schemes
[0]); ++i
) {
1236 if(lstrlenW(recognized_schemes
[i
].scheme_name
) == data
->scheme_len
) {
1237 /* Has to be a case insensitive compare. */
1238 if(!StrCmpNIW(recognized_schemes
[i
].scheme_name
, data
->scheme
, data
->scheme_len
)) {
1239 data
->scheme_type
= recognized_schemes
[i
].scheme
;
1245 /* If we get here it means it's not a recognized scheme. */
1246 data
->scheme_type
= URL_SCHEME_UNKNOWN
;
1248 } else if(data
->is_relative
) {
1249 /* Relative URI's have no scheme. */
1250 data
->scheme_type
= URL_SCHEME_UNKNOWN
;
1253 /* Should never reach here! what happened... */
1254 FIXME("(%p): Unable to determine scheme type for URI %s\n", data
, debugstr_w(data
->uri
));
1259 /* Tries to parse (or deduce) the scheme_name of a URI. If it can't
1260 * parse a scheme from the URI it will try to deduce the scheme_name and scheme_type
1261 * using the flags specified in 'flags' (if any). Flags that affect how this function
1262 * operates are the Uri_CREATE_ALLOW_* flags.
1264 * All parsed/deduced information will be stored in 'data' when the function returns.
1266 * Returns TRUE if it was able to successfully parse the information.
1268 static BOOL
parse_scheme(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
, DWORD extras
) {
1269 static const WCHAR fileW
[] = {'f','i','l','e',0};
1270 static const WCHAR wildcardW
[] = {'*',0};
1272 /* First check to see if the uri could implicitly be a file path. */
1273 if(is_implicit_file_path(*ptr
)) {
1274 if(flags
& Uri_CREATE_ALLOW_IMPLICIT_FILE_SCHEME
) {
1275 data
->scheme
= fileW
;
1276 data
->scheme_len
= lstrlenW(fileW
);
1277 data
->has_implicit_scheme
= TRUE
;
1279 TRACE("(%p %p %x): URI is an implicit file path.\n", ptr
, data
, flags
);
1281 /* Window's does not consider anything that can implicitly be a file
1282 * path to be a valid URI if the ALLOW_IMPLICIT_FILE_SCHEME flag is not set...
1284 TRACE("(%p %p %x): URI is implicitly a file path, but, the ALLOW_IMPLICIT_FILE_SCHEME flag wasn't set.\n",
1288 } else if(!parse_scheme_name(ptr
, data
, extras
)) {
1289 /* No Scheme was found, this means it could be:
1290 * a) an implicit Wildcard scheme
1294 if(flags
& Uri_CREATE_ALLOW_IMPLICIT_WILDCARD_SCHEME
) {
1295 data
->scheme
= wildcardW
;
1296 data
->scheme_len
= lstrlenW(wildcardW
);
1297 data
->has_implicit_scheme
= TRUE
;
1299 TRACE("(%p %p %x): URI is an implicit wildcard scheme.\n", ptr
, data
, flags
);
1300 } else if (flags
& Uri_CREATE_ALLOW_RELATIVE
) {
1301 data
->is_relative
= TRUE
;
1302 TRACE("(%p %p %x): URI is relative.\n", ptr
, data
, flags
);
1304 TRACE("(%p %p %x): Malformed URI found. Unable to deduce scheme name.\n", ptr
, data
, flags
);
1309 if(!data
->is_relative
)
1310 TRACE("(%p %p %x): Found scheme=%s scheme_len=%d\n", ptr
, data
, flags
,
1311 debugstr_wn(data
->scheme
, data
->scheme_len
), data
->scheme_len
);
1313 if(!parse_scheme_type(data
))
1316 TRACE("(%p %p %x): Assigned %d as the URL_SCHEME.\n", ptr
, data
, flags
, data
->scheme_type
);
1320 static BOOL
parse_username(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
, DWORD extras
) {
1321 data
->username
= *ptr
;
1323 while(**ptr
!= ':' && **ptr
!= '@') {
1325 if(!check_pct_encoded(ptr
)) {
1326 if(data
->scheme_type
!= URL_SCHEME_UNKNOWN
) {
1327 *ptr
= data
->username
;
1328 data
->username
= NULL
;
1333 } else if(extras
& ALLOW_NULL_TERM_USER_NAME
&& !**ptr
)
1335 else if(is_auth_delim(**ptr
, data
->scheme_type
!= URL_SCHEME_UNKNOWN
)) {
1336 *ptr
= data
->username
;
1337 data
->username
= NULL
;
1344 data
->username_len
= *ptr
- data
->username
;
1348 static BOOL
parse_password(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
, DWORD extras
) {
1349 data
->password
= *ptr
;
1351 while(**ptr
!= '@') {
1353 if(!check_pct_encoded(ptr
)) {
1354 if(data
->scheme_type
!= URL_SCHEME_UNKNOWN
) {
1355 *ptr
= data
->password
;
1356 data
->password
= NULL
;
1361 } else if(extras
& ALLOW_NULL_TERM_PASSWORD
&& !**ptr
)
1363 else if(is_auth_delim(**ptr
, data
->scheme_type
!= URL_SCHEME_UNKNOWN
)) {
1364 *ptr
= data
->password
;
1365 data
->password
= NULL
;
1372 data
->password_len
= *ptr
- data
->password
;
1376 /* Parses the userinfo part of the URI (if it exists). The userinfo field of
1377 * a URI can consist of "username:password@", or just "username@".
1380 * userinfo = *( unreserved / pct-encoded / sub-delims / ":" )
1383 * 1) If there is more than one ':' in the userinfo part of the URI Windows
1384 * uses the first occurrence of ':' to delimit the username and password
1388 * ftp://user:pass:word@winehq.org
1390 * Would yield, "user" as the username and "pass:word" as the password.
1392 * 2) Windows allows any character to appear in the "userinfo" part of
1393 * a URI, as long as it's not an authority delimeter character set.
1395 static void parse_userinfo(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
) {
1396 const WCHAR
*start
= *ptr
;
1398 if(!parse_username(ptr
, data
, flags
, 0)) {
1399 TRACE("(%p %p %x): URI contained no userinfo.\n", ptr
, data
, flags
);
1405 if(!parse_password(ptr
, data
, flags
, 0)) {
1407 data
->username
= NULL
;
1408 data
->username_len
= 0;
1409 TRACE("(%p %p %x): URI contained no userinfo.\n", ptr
, data
, flags
);
1416 data
->username
= NULL
;
1417 data
->username_len
= 0;
1418 data
->password
= NULL
;
1419 data
->password_len
= 0;
1421 TRACE("(%p %p %x): URI contained no userinfo.\n", ptr
, data
, flags
);
1426 TRACE("(%p %p %x): Found username %s len=%d.\n", ptr
, data
, flags
,
1427 debugstr_wn(data
->username
, data
->username_len
), data
->username_len
);
1430 TRACE("(%p %p %x): Found password %s len=%d.\n", ptr
, data
, flags
,
1431 debugstr_wn(data
->password
, data
->password_len
), data
->password_len
);
1436 /* Attempts to parse a port from the URI.
1439 * Windows seems to have a cap on what the maximum value
1440 * for a port can be. The max value is USHORT_MAX.
1444 static BOOL
parse_port(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
) {
1448 while(!is_auth_delim(**ptr
, data
->scheme_type
!= URL_SCHEME_UNKNOWN
)) {
1449 if(!is_num(**ptr
)) {
1455 port
= port
*10 + (**ptr
-'0');
1457 if(port
> USHORT_MAX
) {
1466 data
->has_port
= TRUE
;
1467 data
->port_value
= port
;
1468 data
->port_len
= *ptr
- data
->port
;
1470 TRACE("(%p %p %x): Found port %s len=%d value=%u\n", ptr
, data
, flags
,
1471 debugstr_wn(data
->port
, data
->port_len
), data
->port_len
, data
->port_value
);
1475 /* Attempts to parse a IPv4 address from the URI.
1478 * Window's normalizes IPv4 addresses, This means there's three
1479 * possibilities for the URI to contain an IPv4 address.
1480 * 1) A well formed address (ex. 192.2.2.2).
1481 * 2) A partially formed address. For example "192.0" would
1482 * normalize to "192.0.0.0" during canonicalization.
1483 * 3) An implicit IPv4 address. For example "256" would
1484 * normalize to "0.0.1.0" during canonicalization. Also
1485 * note that the maximum value for an implicit IP address
1486 * is UINT_MAX, if the value in the URI exceeds this then
1487 * it is not considered an IPv4 address.
1489 static BOOL
parse_ipv4address(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
) {
1490 const BOOL is_unknown
= data
->scheme_type
== URL_SCHEME_UNKNOWN
;
1493 if(!check_ipv4address(ptr
, FALSE
)) {
1494 if(!check_implicit_ipv4(ptr
, &data
->implicit_ipv4
)) {
1495 TRACE("(%p %p %x): URI didn't contain anything looking like an IPv4 address.\n",
1501 data
->has_implicit_ip
= TRUE
;
1504 /* Check if what we found is the only part of the host name (if it isn't
1505 * we don't have an IPv4 address).
1509 if(!parse_port(ptr
, data
, flags
)) {
1514 } else if(!is_auth_delim(**ptr
, !is_unknown
)) {
1515 /* Found more data which belongs the host, so this isn't an IPv4. */
1518 data
->has_implicit_ip
= FALSE
;
1522 data
->host_len
= *ptr
- data
->host
;
1523 data
->host_type
= Uri_HOST_IPV4
;
1525 TRACE("(%p %p %x): IPv4 address found. host=%s host_len=%d host_type=%d\n",
1526 ptr
, data
, flags
, debugstr_wn(data
->host
, data
->host_len
),
1527 data
->host_len
, data
->host_type
);
1531 /* Attempts to parse the reg-name from the URI.
1533 * Because of the way Windows handles ':' this function also
1534 * handles parsing the port.
1536 * reg-name = *( unreserved / pct-encoded / sub-delims )
1539 * Windows allows everything, but, the characters in "auth_delims" and ':'
1540 * to appear in a reg-name, unless it's an unknown scheme type then ':' is
1541 * allowed to appear (even if a valid port isn't after it).
1543 * Windows doesn't like host names which start with '[' and end with ']'
1544 * and don't contain a valid IP literal address in between them.
1546 * On Windows if an '[' is encountered in the host name the ':' no longer
1547 * counts as a delimiter until you reach the next ']' or an "authority delimeter".
1549 * A reg-name CAN be empty.
1551 static BOOL
parse_reg_name(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
, DWORD extras
) {
1552 const BOOL has_start_bracket
= **ptr
== '[';
1553 const BOOL known_scheme
= data
->scheme_type
!= URL_SCHEME_UNKNOWN
;
1554 BOOL inside_brackets
= has_start_bracket
;
1555 BOOL ignore_col
= extras
& IGNORE_PORT_DELIMITER
;
1557 /* We have to be careful with file schemes. */
1558 if(data
->scheme_type
== URL_SCHEME_FILE
) {
1559 /* This is because an implicit file scheme could be "C:\\test" and it
1560 * would trick this function into thinking the host is "C", when after
1561 * canonicalization the host would end up being an empty string. A drive
1562 * path can also have a '|' instead of a ':' after the drive letter.
1564 if(is_drive_path(*ptr
)) {
1565 /* Regular old drive paths don't have a host type (or host name). */
1566 data
->host_type
= Uri_HOST_UNKNOWN
;
1570 } else if(is_unc_path(*ptr
))
1571 /* Skip past the "\\" of a UNC path. */
1577 while(!is_auth_delim(**ptr
, known_scheme
)) {
1578 if(**ptr
== ':' && !ignore_col
) {
1579 /* We can ignore ':' if were inside brackets.*/
1580 if(!inside_brackets
) {
1581 const WCHAR
*tmp
= (*ptr
)++;
1583 /* Attempt to parse the port. */
1584 if(!parse_port(ptr
, data
, flags
)) {
1585 /* Windows expects there to be a valid port for known scheme types. */
1586 if(data
->scheme_type
!= URL_SCHEME_UNKNOWN
) {
1589 TRACE("(%p %p %x %x): Expected valid port\n", ptr
, data
, flags
, extras
);
1592 /* Windows gives up on trying to parse a port when it
1593 * encounters 1 invalid port.
1597 data
->host_len
= tmp
- data
->host
;
1601 } else if(**ptr
== '%' && known_scheme
) {
1602 /* Has to be a legit % encoded value. */
1603 if(!check_pct_encoded(ptr
)) {
1609 } else if(**ptr
== ']')
1610 inside_brackets
= FALSE
;
1611 else if(**ptr
== '[')
1612 inside_brackets
= TRUE
;
1617 if(has_start_bracket
) {
1618 /* Make sure the last character of the host wasn't a ']'. */
1619 if(*(*ptr
-1) == ']') {
1620 TRACE("(%p %p %x %x): Expected an IP literal inside of the host\n",
1621 ptr
, data
, flags
, extras
);
1628 /* Don't overwrite our length if we found a port earlier. */
1630 data
->host_len
= *ptr
- data
->host
;
1632 /* If the host is empty, then it's an unknown host type. */
1633 if(data
->host_len
== 0)
1634 data
->host_type
= Uri_HOST_UNKNOWN
;
1636 data
->host_type
= Uri_HOST_DNS
;
1638 TRACE("(%p %p %x %x): Parsed reg-name. host=%s len=%d\n", ptr
, data
, flags
, extras
,
1639 debugstr_wn(data
->host
, data
->host_len
), data
->host_len
);
1643 /* Attempts to parse an IPv6 address out of the URI.
1645 * IPv6address = 6( h16 ":" ) ls32
1646 * / "::" 5( h16 ":" ) ls32
1647 * / [ h16 ] "::" 4( h16 ":" ) ls32
1648 * / [ *1( h16 ":" ) h16 ] "::" 3( h16 ":" ) ls32
1649 * / [ *2( h16 ":" ) h16 ] "::" 2( h16 ":" ) ls32
1650 * / [ *3( h16 ":" ) h16 ] "::" h16 ":" ls32
1651 * / [ *4( h16 ":" ) h16 ] "::" ls32
1652 * / [ *5( h16 ":" ) h16 ] "::" h16
1653 * / [ *6( h16 ":" ) h16 ] "::"
1655 * ls32 = ( h16 ":" h16 ) / IPv4address
1656 * ; least-significant 32 bits of address.
1659 * ; 16 bits of address represented in hexadecimal.
1661 * Modeled after google-url's 'DoParseIPv6' function.
1663 static BOOL
parse_ipv6address(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
) {
1664 const WCHAR
*start
, *cur_start
;
1667 start
= cur_start
= *ptr
;
1668 memset(&ip
, 0, sizeof(ipv6_address
));
1671 /* Check if we're on the last character of the host. */
1672 BOOL is_end
= (is_auth_delim(**ptr
, data
->scheme_type
!= URL_SCHEME_UNKNOWN
)
1675 BOOL is_split
= (**ptr
== ':');
1676 BOOL is_elision
= (is_split
&& !is_end
&& *(*ptr
+1) == ':');
1678 /* Check if we're at the end of a component, or
1679 * if we're at the end of the IPv6 address.
1681 if(is_split
|| is_end
) {
1684 cur_len
= *ptr
- cur_start
;
1686 /* h16 can't have a length > 4. */
1690 TRACE("(%p %p %x): h16 component to long.\n",
1696 /* An h16 component can't have the length of 0 unless
1697 * the elision is at the beginning of the address, or
1698 * at the end of the address.
1700 if(!((*ptr
== start
&& is_elision
) ||
1701 (is_end
&& (*ptr
-2) == ip
.elision
))) {
1703 TRACE("(%p %p %x): IPv6 component cannot have a length of 0.\n",
1710 /* An IPv6 address can have no more than 8 h16 components. */
1711 if(ip
.h16_count
>= 8) {
1713 TRACE("(%p %p %x): Not a IPv6 address, to many h16 components.\n",
1718 ip
.components
[ip
.h16_count
].str
= cur_start
;
1719 ip
.components
[ip
.h16_count
].len
= cur_len
;
1721 TRACE("(%p %p %x): Found h16 component %s, len=%d, h16_count=%d\n",
1722 ptr
, data
, flags
, debugstr_wn(cur_start
, cur_len
), cur_len
,
1732 /* A IPv6 address can only have 1 elision ('::'). */
1736 TRACE("(%p %p %x): IPv6 address cannot have 2 elisions.\n",
1748 if(!check_ipv4address(ptr
, TRUE
)) {
1749 if(!is_hexdigit(**ptr
)) {
1750 /* Not a valid character for an IPv6 address. */
1755 /* Found an IPv4 address. */
1756 ip
.ipv4
= cur_start
;
1757 ip
.ipv4_len
= *ptr
- cur_start
;
1759 TRACE("(%p %p %x): Found an attached IPv4 address %s len=%d.\n",
1760 ptr
, data
, flags
, debugstr_wn(ip
.ipv4
, ip
.ipv4_len
),
1763 /* IPv4 addresses can only appear at the end of a IPv6. */
1769 compute_ipv6_comps_size(&ip
);
1771 /* Make sure the IPv6 address adds up to 16 bytes. */
1772 if(ip
.components_size
+ ip
.elision_size
!= 16) {
1774 TRACE("(%p %p %x): Invalid IPv6 address, did not add up to 16 bytes.\n",
1779 if(ip
.elision_size
== 2) {
1780 /* For some reason on Windows if an elision that represents
1781 * only 1 h16 component is encountered at the very begin or
1782 * end of an IPv6 address, Windows does not consider it a
1783 * valid IPv6 address.
1785 * Ex: [::2:3:4:5:6:7] is not valid, even though the sum
1786 * of all the components == 128bits.
1788 if(ip
.elision
< ip
.components
[0].str
||
1789 ip
.elision
> ip
.components
[ip
.h16_count
-1].str
) {
1791 TRACE("(%p %p %x): Invalid IPv6 address. Detected elision of 2 bytes at the beginning or end of the address.\n",
1797 data
->host_type
= Uri_HOST_IPV6
;
1798 data
->has_ipv6
= TRUE
;
1799 data
->ipv6_address
= ip
;
1801 TRACE("(%p %p %x): Found valid IPv6 literal %s len=%d\n",
1802 ptr
, data
, flags
, debugstr_wn(start
, *ptr
-start
),
1807 /* IPvFuture = "v" 1*HEXDIG "." 1*( unreserved / sub-delims / ":" ) */
1808 static BOOL
parse_ipvfuture(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
) {
1809 const WCHAR
*start
= *ptr
;
1811 /* IPvFuture has to start with a 'v' or 'V'. */
1812 if(**ptr
!= 'v' && **ptr
!= 'V')
1815 /* Following the v there must be at least 1 hex digit. */
1817 if(!is_hexdigit(**ptr
)) {
1823 while(is_hexdigit(**ptr
))
1826 /* End of the hexdigit sequence must be a '.' */
1833 if(!is_unreserved(**ptr
) && !is_subdelim(**ptr
) && **ptr
!= ':') {
1839 while(is_unreserved(**ptr
) || is_subdelim(**ptr
) || **ptr
== ':')
1842 data
->host_type
= Uri_HOST_UNKNOWN
;
1844 TRACE("(%p %p %x): Parsed IPvFuture address %s len=%d\n", ptr
, data
, flags
,
1845 debugstr_wn(start
, *ptr
-start
), *ptr
-start
);
1850 /* IP-literal = "[" ( IPv6address / IPvFuture ) "]" */
1851 static BOOL
parse_ip_literal(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
, DWORD extras
) {
1854 if(**ptr
!= '[' && !(extras
& ALLOW_BRACKETLESS_IP_LITERAL
)) {
1857 } else if(**ptr
== '[')
1860 if(!parse_ipv6address(ptr
, data
, flags
)) {
1861 if(extras
& SKIP_IP_FUTURE_CHECK
|| !parse_ipvfuture(ptr
, data
, flags
)) {
1868 if(**ptr
!= ']' && !(extras
& ALLOW_BRACKETLESS_IP_LITERAL
)) {
1872 } else if(!**ptr
&& extras
& ALLOW_BRACKETLESS_IP_LITERAL
) {
1873 /* The IP literal didn't contain brackets and was followed by
1874 * a NULL terminator, so no reason to even check the port.
1876 data
->host_len
= *ptr
- data
->host
;
1883 /* If a valid port is not found, then let it trickle down to
1886 if(!parse_port(ptr
, data
, flags
)) {
1892 data
->host_len
= *ptr
- data
->host
;
1897 /* Parses the host information from the URI.
1899 * host = IP-literal / IPv4address / reg-name
1901 static BOOL
parse_host(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
, DWORD extras
) {
1902 if(!parse_ip_literal(ptr
, data
, flags
, extras
)) {
1903 if(!parse_ipv4address(ptr
, data
, flags
)) {
1904 if(!parse_reg_name(ptr
, data
, flags
, extras
)) {
1905 TRACE("(%p %p %x %x): Malformed URI, Unknown host type.\n",
1906 ptr
, data
, flags
, extras
);
1915 /* Parses the authority information from the URI.
1917 * authority = [ userinfo "@" ] host [ ":" port ]
1919 static BOOL
parse_authority(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
) {
1920 parse_userinfo(ptr
, data
, flags
);
1922 /* Parsing the port will happen during one of the host parsing
1923 * routines (if the URI has a port).
1925 if(!parse_host(ptr
, data
, flags
, 0))
1931 /* Attempts to parse the path information of a hierarchical URI. */
1932 static BOOL
parse_path_hierarchical(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
) {
1933 const WCHAR
*start
= *ptr
;
1934 static const WCHAR slash
[] = {'/',0};
1935 const BOOL is_file
= data
->scheme_type
== URL_SCHEME_FILE
;
1937 if(is_path_delim(**ptr
)) {
1938 if(data
->scheme_type
== URL_SCHEME_WILDCARD
) {
1939 /* Wildcard schemes don't get a '/' attached if their path is
1944 } else if(!(flags
& Uri_CREATE_NO_CANONICALIZE
)) {
1945 /* If the path component is empty, then a '/' is added. */
1950 while(!is_path_delim(**ptr
)) {
1951 if(**ptr
== '%' && data
->scheme_type
!= URL_SCHEME_UNKNOWN
&& !is_file
) {
1952 if(!check_pct_encoded(ptr
)) {
1957 } else if(is_forbidden_dos_path_char(**ptr
) && is_file
&&
1958 (flags
& Uri_CREATE_FILE_USE_DOS_PATH
)) {
1959 /* File schemes with USE_DOS_PATH set aren't allowed to have
1960 * a '<' or '>' or '\"' appear in them.
1964 } else if(**ptr
== '\\') {
1965 /* Not allowed to have a backslash if NO_CANONICALIZE is set
1966 * and the scheme is known type (but not a file scheme).
1968 if(flags
& Uri_CREATE_NO_CANONICALIZE
) {
1969 if(data
->scheme_type
!= URL_SCHEME_FILE
&&
1970 data
->scheme_type
!= URL_SCHEME_UNKNOWN
) {
1980 /* The only time a URI doesn't have a path is when
1981 * the NO_CANONICALIZE flag is set and the raw URI
1982 * didn't contain one.
1989 data
->path_len
= *ptr
- start
;
1994 TRACE("(%p %p %x): Parsed path %s len=%d\n", ptr
, data
, flags
,
1995 debugstr_wn(data
->path
, data
->path_len
), data
->path_len
);
1997 TRACE("(%p %p %x): The URI contained no path\n", ptr
, data
, flags
);
2002 /* Parses the path of a opaque URI (much less strict then the parser
2003 * for a hierarchical URI).
2006 * Windows allows invalid % encoded data to appear in opaque URI paths
2007 * for unknown scheme types.
2009 * File schemes with USE_DOS_PATH set aren't allowed to have '<', '>', or '\"'
2012 static BOOL
parse_path_opaque(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
) {
2013 const BOOL known_scheme
= data
->scheme_type
!= URL_SCHEME_UNKNOWN
;
2014 const BOOL is_file
= data
->scheme_type
== URL_SCHEME_FILE
;
2018 while(!is_path_delim(**ptr
)) {
2019 if(**ptr
== '%' && known_scheme
) {
2020 if(!check_pct_encoded(ptr
)) {
2026 } else if(is_forbidden_dos_path_char(**ptr
) && is_file
&&
2027 (flags
& Uri_CREATE_FILE_USE_DOS_PATH
)) {
2036 data
->path_len
= *ptr
- data
->path
;
2037 TRACE("(%p %p %x): Parsed opaque URI path %s len=%d\n", ptr
, data
, flags
,
2038 debugstr_wn(data
->path
, data
->path_len
), data
->path_len
);
2042 /* Determines how the URI should be parsed after the scheme information.
2044 * If the scheme is followed, by "//" then, it is treated as an hierarchical URI
2045 * which then the authority and path information will be parsed out. Otherwise, the
2046 * URI will be treated as an opaque URI which the authority information is not parsed
2049 * RFC 3896 definition of hier-part:
2051 * hier-part = "//" authority path-abempty
2056 * MSDN opaque URI definition:
2057 * scheme ":" path [ "#" fragment ]
2060 * If the URI is of an unknown scheme type and has a "//" following the scheme then it
2061 * is treated as a hierarchical URI, but, if the CREATE_NO_CRACK_UNKNOWN_SCHEMES flag is
2062 * set then it is considered an opaque URI reguardless of what follows the scheme information
2063 * (per MSDN documentation).
2065 static BOOL
parse_hierpart(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
) {
2066 const WCHAR
*start
= *ptr
;
2068 /* Checks if the authority information needs to be parsed. */
2069 if(is_hierarchical_uri(ptr
, data
)) {
2070 /* Only treat it as a hierarchical URI if the scheme_type is known or
2071 * the Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES flag is not set.
2073 if(data
->scheme_type
!= URL_SCHEME_UNKNOWN
||
2074 !(flags
& Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES
)) {
2075 TRACE("(%p %p %x): Treating URI as an hierarchical URI.\n", ptr
, data
, flags
);
2076 data
->is_opaque
= FALSE
;
2078 /* TODO: Handle hierarchical URI's, parse authority then parse the path. */
2079 if(!parse_authority(ptr
, data
, flags
))
2082 return parse_path_hierarchical(ptr
, data
, flags
);
2084 /* Reset ptr to it's starting position so opaque path parsing
2085 * begins at the correct location.
2090 /* If it reaches here, then the URI will be treated as an opaque
2094 TRACE("(%p %p %x): Treating URI as an opaque URI.\n", ptr
, data
, flags
);
2096 data
->is_opaque
= TRUE
;
2097 if(!parse_path_opaque(ptr
, data
, flags
))
2103 /* Attempts to parse the query string from the URI.
2106 * If NO_DECODE_EXTRA_INFO flag is set, then invalid percent encoded
2107 * data is allowed appear in the query string. For unknown scheme types
2108 * invalid percent encoded data is allowed to appear reguardless.
2110 static BOOL
parse_query(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
) {
2111 const BOOL known_scheme
= data
->scheme_type
!= URL_SCHEME_UNKNOWN
;
2114 TRACE("(%p %p %x): URI didn't contain a query string.\n", ptr
, data
, flags
);
2121 while(**ptr
&& **ptr
!= '#') {
2122 if(**ptr
== '%' && known_scheme
&&
2123 !(flags
& Uri_CREATE_NO_DECODE_EXTRA_INFO
)) {
2124 if(!check_pct_encoded(ptr
)) {
2135 data
->query_len
= *ptr
- data
->query
;
2137 TRACE("(%p %p %x): Parsed query string %s len=%d\n", ptr
, data
, flags
,
2138 debugstr_wn(data
->query
, data
->query_len
), data
->query_len
);
2142 /* Attempts to parse the fragment from the URI.
2145 * If NO_DECODE_EXTRA_INFO flag is set, then invalid percent encoded
2146 * data is allowed appear in the query string. For unknown scheme types
2147 * invalid percent encoded data is allowed to appear reguardless.
2149 static BOOL
parse_fragment(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
) {
2150 const BOOL known_scheme
= data
->scheme_type
!= URL_SCHEME_UNKNOWN
;
2153 TRACE("(%p %p %x): URI didn't contain a fragment.\n", ptr
, data
, flags
);
2157 data
->fragment
= *ptr
;
2161 if(**ptr
== '%' && known_scheme
&&
2162 !(flags
& Uri_CREATE_NO_DECODE_EXTRA_INFO
)) {
2163 if(!check_pct_encoded(ptr
)) {
2164 *ptr
= data
->fragment
;
2165 data
->fragment
= NULL
;
2174 data
->fragment_len
= *ptr
- data
->fragment
;
2176 TRACE("(%p %p %x): Parsed fragment %s len=%d\n", ptr
, data
, flags
,
2177 debugstr_wn(data
->fragment
, data
->fragment_len
), data
->fragment_len
);
2181 /* Parses and validates the components of the specified by data->uri
2182 * and stores the information it parses into 'data'.
2184 * Returns TRUE if it successfully parsed the URI. False otherwise.
2186 static BOOL
parse_uri(parse_data
*data
, DWORD flags
) {
2193 TRACE("(%p %x): BEGINNING TO PARSE URI %s.\n", data
, flags
, debugstr_w(data
->uri
));
2195 if(!parse_scheme(pptr
, data
, flags
, 0))
2198 if(!parse_hierpart(pptr
, data
, flags
))
2201 if(!parse_query(pptr
, data
, flags
))
2204 if(!parse_fragment(pptr
, data
, flags
))
2207 TRACE("(%p %x): FINISHED PARSING URI.\n", data
, flags
);
2211 static BOOL
canonicalize_username(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
2214 if(!data
->username
) {
2215 uri
->userinfo_start
= -1;
2219 uri
->userinfo_start
= uri
->canon_len
;
2220 for(ptr
= data
->username
; ptr
< data
->username
+data
->username_len
; ++ptr
) {
2222 /* Only decode % encoded values for known scheme types. */
2223 if(data
->scheme_type
!= URL_SCHEME_UNKNOWN
) {
2224 /* See if the value really needs decoded. */
2225 WCHAR val
= decode_pct_val(ptr
);
2226 if(is_unreserved(val
)) {
2228 uri
->canon_uri
[uri
->canon_len
] = val
;
2232 /* Move pass the hex characters. */
2237 } else if(!is_reserved(*ptr
) && !is_unreserved(*ptr
) && *ptr
!= '\\') {
2238 /* Only percent encode forbidden characters if the NO_ENCODE_FORBIDDEN_CHARACTERS flag
2241 if(!(flags
& Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS
)) {
2243 pct_encode_val(*ptr
, uri
->canon_uri
+ uri
->canon_len
);
2245 uri
->canon_len
+= 3;
2251 /* Nothing special, so just copy the character over. */
2252 uri
->canon_uri
[uri
->canon_len
] = *ptr
;
2259 static BOOL
canonicalize_password(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
2262 if(!data
->password
) {
2263 uri
->userinfo_split
= -1;
2267 if(uri
->userinfo_start
== -1)
2268 /* Has a password, but, doesn't have a username. */
2269 uri
->userinfo_start
= uri
->canon_len
;
2271 uri
->userinfo_split
= uri
->canon_len
- uri
->userinfo_start
;
2273 /* Add the ':' to the userinfo component. */
2275 uri
->canon_uri
[uri
->canon_len
] = ':';
2278 for(ptr
= data
->password
; ptr
< data
->password
+data
->password_len
; ++ptr
) {
2280 /* Only decode % encoded values for known scheme types. */
2281 if(data
->scheme_type
!= URL_SCHEME_UNKNOWN
) {
2282 /* See if the value really needs decoded. */
2283 WCHAR val
= decode_pct_val(ptr
);
2284 if(is_unreserved(val
)) {
2286 uri
->canon_uri
[uri
->canon_len
] = val
;
2290 /* Move pass the hex characters. */
2295 } else if(!is_reserved(*ptr
) && !is_unreserved(*ptr
) && *ptr
!= '\\') {
2296 /* Only percent encode forbidden characters if the NO_ENCODE_FORBIDDEN_CHARACTERS flag
2299 if(!(flags
& Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS
)) {
2301 pct_encode_val(*ptr
, uri
->canon_uri
+ uri
->canon_len
);
2303 uri
->canon_len
+= 3;
2309 /* Nothing special, so just copy the character over. */
2310 uri
->canon_uri
[uri
->canon_len
] = *ptr
;
2317 /* Canonicalizes the userinfo of the URI represented by the parse_data.
2319 * Canonicalization of the userinfo is a simple process. If there are any percent
2320 * encoded characters that fall in the "unreserved" character set, they are decoded
2321 * to their actual value. If a character is not in the "unreserved" or "reserved" sets
2322 * then it is percent encoded. Other than that the characters are copied over without
2325 static BOOL
canonicalize_userinfo(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
2326 uri
->userinfo_start
= uri
->userinfo_split
= -1;
2327 uri
->userinfo_len
= 0;
2329 if(!data
->username
&& !data
->password
)
2330 /* URI doesn't have userinfo, so nothing to do here. */
2333 if(!canonicalize_username(data
, uri
, flags
, computeOnly
))
2336 if(!canonicalize_password(data
, uri
, flags
, computeOnly
))
2339 uri
->userinfo_len
= uri
->canon_len
- uri
->userinfo_start
;
2341 TRACE("(%p %p %x %d): Canonicalized userinfo, userinfo_start=%d, userinfo=%s, userinfo_split=%d userinfo_len=%d.\n",
2342 data
, uri
, flags
, computeOnly
, uri
->userinfo_start
, debugstr_wn(uri
->canon_uri
+ uri
->userinfo_start
, uri
->userinfo_len
),
2343 uri
->userinfo_split
, uri
->userinfo_len
);
2345 /* Now insert the '@' after the userinfo. */
2347 uri
->canon_uri
[uri
->canon_len
] = '@';
2353 /* Attempts to canonicalize a reg_name.
2355 * Things that happen:
2356 * 1) If Uri_CREATE_NO_CANONICALIZE flag is not set, then the reg_name is
2357 * lower cased. Unless it's an unknown scheme type, which case it's
2358 * no lower cased reguardless.
2360 * 2) Unreserved % encoded characters are decoded for known
2363 * 3) Forbidden characters are % encoded as long as
2364 * Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS flag is not set and
2365 * it isn't an unknown scheme type.
2367 * 4) If it's a file scheme and the host is "localhost" it's removed.
2369 * 5) If it's a file scheme and Uri_CREATE_FILE_USE_DOS_PATH is set,
2370 * then the UNC path characters are added before the host name.
2372 static BOOL
canonicalize_reg_name(const parse_data
*data
, Uri
*uri
,
2373 DWORD flags
, BOOL computeOnly
) {
2374 static const WCHAR localhostW
[] =
2375 {'l','o','c','a','l','h','o','s','t',0};
2377 const BOOL known_scheme
= data
->scheme_type
!= URL_SCHEME_UNKNOWN
;
2379 if(data
->scheme_type
== URL_SCHEME_FILE
&&
2380 data
->host_len
== lstrlenW(localhostW
)) {
2381 if(!StrCmpNIW(data
->host
, localhostW
, data
->host_len
)) {
2382 uri
->host_start
= -1;
2384 uri
->host_type
= Uri_HOST_UNKNOWN
;
2389 if(data
->scheme_type
== URL_SCHEME_FILE
&& flags
& Uri_CREATE_FILE_USE_DOS_PATH
) {
2391 uri
->canon_uri
[uri
->canon_len
] = '\\';
2392 uri
->canon_uri
[uri
->canon_len
+1] = '\\';
2394 uri
->canon_len
+= 2;
2395 uri
->authority_start
= uri
->canon_len
;
2398 uri
->host_start
= uri
->canon_len
;
2400 for(ptr
= data
->host
; ptr
< data
->host
+data
->host_len
; ++ptr
) {
2401 if(*ptr
== '%' && known_scheme
) {
2402 WCHAR val
= decode_pct_val(ptr
);
2403 if(is_unreserved(val
)) {
2404 /* If NO_CANONICALZE is not set, then windows lower cases the
2407 if(!(flags
& Uri_CREATE_NO_CANONICALIZE
) && isupperW(val
)) {
2409 uri
->canon_uri
[uri
->canon_len
] = tolowerW(val
);
2412 uri
->canon_uri
[uri
->canon_len
] = val
;
2416 /* Skip past the % encoded character. */
2420 /* Just copy the % over. */
2422 uri
->canon_uri
[uri
->canon_len
] = *ptr
;
2425 } else if(*ptr
== '\\') {
2426 /* Only unknown scheme types could have made it here with a '\\' in the host name. */
2428 uri
->canon_uri
[uri
->canon_len
] = *ptr
;
2430 } else if(!(flags
& Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS
) &&
2431 !is_unreserved(*ptr
) && !is_reserved(*ptr
) && known_scheme
) {
2433 pct_encode_val(*ptr
, uri
->canon_uri
+uri
->canon_len
);
2435 /* The percent encoded value gets lower cased also. */
2436 if(!(flags
& Uri_CREATE_NO_CANONICALIZE
)) {
2437 uri
->canon_uri
[uri
->canon_len
+1] = tolowerW(uri
->canon_uri
[uri
->canon_len
+1]);
2438 uri
->canon_uri
[uri
->canon_len
+2] = tolowerW(uri
->canon_uri
[uri
->canon_len
+2]);
2442 uri
->canon_len
+= 3;
2445 if(!(flags
& Uri_CREATE_NO_CANONICALIZE
) && known_scheme
)
2446 uri
->canon_uri
[uri
->canon_len
] = tolowerW(*ptr
);
2448 uri
->canon_uri
[uri
->canon_len
] = *ptr
;
2455 uri
->host_len
= uri
->canon_len
- uri
->host_start
;
2458 TRACE("(%p %p %x %d): Canonicalize reg_name=%s len=%d\n", data
, uri
, flags
,
2459 computeOnly
, debugstr_wn(uri
->canon_uri
+uri
->host_start
, uri
->host_len
),
2463 find_domain_name(uri
->canon_uri
+uri
->host_start
, uri
->host_len
,
2464 &(uri
->domain_offset
));
2469 /* Attempts to canonicalize an implicit IPv4 address. */
2470 static BOOL
canonicalize_implicit_ipv4address(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
2471 uri
->host_start
= uri
->canon_len
;
2473 TRACE("%u\n", data
->implicit_ipv4
);
2474 /* For unknown scheme types Window's doesn't convert
2475 * the value into an IP address, but, it still considers
2476 * it an IPv4 address.
2478 if(data
->scheme_type
== URL_SCHEME_UNKNOWN
) {
2480 memcpy(uri
->canon_uri
+uri
->canon_len
, data
->host
, data
->host_len
*sizeof(WCHAR
));
2481 uri
->canon_len
+= data
->host_len
;
2484 uri
->canon_len
+= ui2ipv4(uri
->canon_uri
+uri
->canon_len
, data
->implicit_ipv4
);
2486 uri
->canon_len
+= ui2ipv4(NULL
, data
->implicit_ipv4
);
2489 uri
->host_len
= uri
->canon_len
- uri
->host_start
;
2490 uri
->host_type
= Uri_HOST_IPV4
;
2493 TRACE("%p %p %x %d): Canonicalized implicit IP address=%s len=%d\n",
2494 data
, uri
, flags
, computeOnly
,
2495 debugstr_wn(uri
->canon_uri
+uri
->host_start
, uri
->host_len
),
2501 /* Attempts to canonicalize an IPv4 address.
2503 * If the parse_data represents a URI that has an implicit IPv4 address
2504 * (ex. http://256/, this function will convert 256 into 0.0.1.0). If
2505 * the implicit IP address exceeds the value of UINT_MAX (maximum value
2506 * for an IPv4 address) it's canonicalized as if were a reg-name.
2508 * If the parse_data contains a partial or full IPv4 address it normalizes it.
2509 * A partial IPv4 address is something like "192.0" and would be normalized to
2510 * "192.0.0.0". With a full (or partial) IPv4 address like "192.002.01.003" would
2511 * be normalized to "192.2.1.3".
2514 * Window's ONLY normalizes IPv4 address for known scheme types (one that isn't
2515 * URL_SCHEME_UNKNOWN). For unknown scheme types, it simply copies the data from
2516 * the original URI into the canonicalized URI, but, it still recognizes URI's
2517 * host type as HOST_IPV4.
2519 static BOOL
canonicalize_ipv4address(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
2520 if(data
->has_implicit_ip
)
2521 return canonicalize_implicit_ipv4address(data
, uri
, flags
, computeOnly
);
2523 uri
->host_start
= uri
->canon_len
;
2525 /* Windows only normalizes for known scheme types. */
2526 if(data
->scheme_type
!= URL_SCHEME_UNKNOWN
) {
2527 /* parse_data contains a partial or full IPv4 address, so normalize it. */
2528 DWORD i
, octetDigitCount
= 0, octetCount
= 0;
2529 BOOL octetHasDigit
= FALSE
;
2531 for(i
= 0; i
< data
->host_len
; ++i
) {
2532 if(data
->host
[i
] == '0' && !octetHasDigit
) {
2533 /* Can ignore leading zeros if:
2534 * 1) It isn't the last digit of the octet.
2535 * 2) i+1 != data->host_len
2538 if(octetDigitCount
== 2 ||
2539 i
+1 == data
->host_len
||
2540 data
->host
[i
+1] == '.') {
2542 uri
->canon_uri
[uri
->canon_len
] = data
->host
[i
];
2544 TRACE("Adding zero\n");
2546 } else if(data
->host
[i
] == '.') {
2548 uri
->canon_uri
[uri
->canon_len
] = data
->host
[i
];
2551 octetDigitCount
= 0;
2552 octetHasDigit
= FALSE
;
2556 uri
->canon_uri
[uri
->canon_len
] = data
->host
[i
];
2560 octetHasDigit
= TRUE
;
2564 /* Make sure the canonicalized IP address has 4 dec-octets.
2565 * If doesn't add "0" ones until there is 4;
2567 for( ; octetCount
< 3; ++octetCount
) {
2569 uri
->canon_uri
[uri
->canon_len
] = '.';
2570 uri
->canon_uri
[uri
->canon_len
+1] = '0';
2573 uri
->canon_len
+= 2;
2576 /* Windows doesn't normalize addresses in unknown schemes. */
2578 memcpy(uri
->canon_uri
+uri
->canon_len
, data
->host
, data
->host_len
*sizeof(WCHAR
));
2579 uri
->canon_len
+= data
->host_len
;
2582 uri
->host_len
= uri
->canon_len
- uri
->host_start
;
2584 TRACE("(%p %p %x %d): Canonicalized IPv4 address, ip=%s len=%d\n",
2585 data
, uri
, flags
, computeOnly
,
2586 debugstr_wn(uri
->canon_uri
+uri
->host_start
, uri
->host_len
),
2593 /* Attempts to canonicalize the IPv6 address of the URI.
2595 * Multiple things happen during the canonicalization of an IPv6 address:
2596 * 1) Any leading zero's in an h16 component are removed.
2597 * Ex: [0001:0022::] -> [1:22::]
2599 * 2) The longest sequence of zero h16 components are compressed
2600 * into a "::" (elision). If there's a tie, the first is choosen.
2602 * Ex: [0:0:0:0:1:6:7:8] -> [::1:6:7:8]
2603 * [0:0:0:0:1:2::] -> [::1:2:0:0]
2604 * [0:0:1:2:0:0:7:8] -> [::1:2:0:0:7:8]
2606 * 3) If an IPv4 address is attached to the IPv6 address, it's
2608 * Ex: [::001.002.022.000] -> [::1.2.22.0]
2610 * 4) If an elision is present, but, only represents 1 h16 component
2613 * Ex: [1::2:3:4:5:6:7] -> [1:0:2:3:4:5:6:7]
2615 * 5) If the IPv6 address contains an IPv4 address and there exists
2616 * at least 1 non-zero h16 component the IPv4 address is converted
2617 * into two h16 components, otherwise it's normalized and kept as is.
2619 * Ex: [::192.200.003.4] -> [::192.200.3.4]
2620 * [ffff::192.200.003.4] -> [ffff::c0c8:3041]
2623 * For unknown scheme types Windows simply copies the address over without any
2626 * IPv4 address can be included in an elision if all its components are 0's.
2628 static BOOL
canonicalize_ipv6address(const parse_data
*data
, Uri
*uri
,
2629 DWORD flags
, BOOL computeOnly
) {
2630 uri
->host_start
= uri
->canon_len
;
2632 if(data
->scheme_type
== URL_SCHEME_UNKNOWN
) {
2634 memcpy(uri
->canon_uri
+uri
->canon_len
, data
->host
, data
->host_len
*sizeof(WCHAR
));
2635 uri
->canon_len
+= data
->host_len
;
2639 DWORD i
, elision_len
;
2641 if(!ipv6_to_number(&(data
->ipv6_address
), values
)) {
2642 TRACE("(%p %p %x %d): Failed to compute numerical value for IPv6 address.\n",
2643 data
, uri
, flags
, computeOnly
);
2648 uri
->canon_uri
[uri
->canon_len
] = '[';
2651 /* Find where the elision should occur (if any). */
2652 compute_elision_location(&(data
->ipv6_address
), values
, &elision_start
, &elision_len
);
2654 TRACE("%p %p %x %d): Elision starts at %d, len=%u\n", data
, uri
, flags
,
2655 computeOnly
, elision_start
, elision_len
);
2657 for(i
= 0; i
< 8; ++i
) {
2658 BOOL in_elision
= (elision_start
> -1 && i
>= elision_start
&&
2659 i
< elision_start
+elision_len
);
2660 BOOL do_ipv4
= (i
== 6 && data
->ipv6_address
.ipv4
&& !in_elision
&&
2661 data
->ipv6_address
.h16_count
== 0);
2663 if(i
== elision_start
) {
2665 uri
->canon_uri
[uri
->canon_len
] = ':';
2666 uri
->canon_uri
[uri
->canon_len
+1] = ':';
2668 uri
->canon_len
+= 2;
2671 /* We can ignore the current component if we're in the elision. */
2675 /* We only add a ':' if we're not at i == 0, or when we're at
2676 * the very end of elision range since the ':' colon was handled
2677 * earlier. Otherwise we would end up with ":::" after elision.
2679 if(i
!= 0 && !(elision_start
> -1 && i
== elision_start
+elision_len
)) {
2681 uri
->canon_uri
[uri
->canon_len
] = ':';
2689 /* Combine the two parts of the IPv4 address values. */
2695 len
= ui2ipv4(uri
->canon_uri
+uri
->canon_len
, val
);
2697 len
= ui2ipv4(NULL
, val
);
2699 uri
->canon_len
+= len
;
2702 /* Write a regular h16 component to the URI. */
2704 /* Short circuit for the trivial case. */
2705 if(values
[i
] == 0) {
2707 uri
->canon_uri
[uri
->canon_len
] = '0';
2710 static const WCHAR formatW
[] = {'%','x',0};
2713 uri
->canon_len
+= sprintfW(uri
->canon_uri
+uri
->canon_len
,
2714 formatW
, values
[i
]);
2717 uri
->canon_len
+= sprintfW(tmp
, formatW
, values
[i
]);
2723 /* Add the closing ']'. */
2725 uri
->canon_uri
[uri
->canon_len
] = ']';
2729 uri
->host_len
= uri
->canon_len
- uri
->host_start
;
2732 TRACE("(%p %p %x %d): Canonicalized IPv6 address %s, len=%d\n", data
, uri
, flags
,
2733 computeOnly
, debugstr_wn(uri
->canon_uri
+uri
->host_start
, uri
->host_len
),
2739 /* Attempts to canonicalize the host of the URI (if any). */
2740 static BOOL
canonicalize_host(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
2741 uri
->host_start
= -1;
2743 uri
->domain_offset
= -1;
2746 switch(data
->host_type
) {
2748 uri
->host_type
= Uri_HOST_DNS
;
2749 if(!canonicalize_reg_name(data
, uri
, flags
, computeOnly
))
2754 uri
->host_type
= Uri_HOST_IPV4
;
2755 if(!canonicalize_ipv4address(data
, uri
, flags
, computeOnly
))
2760 if(!canonicalize_ipv6address(data
, uri
, flags
, computeOnly
))
2763 uri
->host_type
= Uri_HOST_IPV6
;
2765 case Uri_HOST_UNKNOWN
:
2766 if(data
->host_len
> 0 || data
->scheme_type
!= URL_SCHEME_FILE
) {
2767 uri
->host_start
= uri
->canon_len
;
2769 /* Nothing happens to unknown host types. */
2771 memcpy(uri
->canon_uri
+uri
->canon_len
, data
->host
, data
->host_len
*sizeof(WCHAR
));
2772 uri
->canon_len
+= data
->host_len
;
2773 uri
->host_len
= data
->host_len
;
2776 uri
->host_type
= Uri_HOST_UNKNOWN
;
2779 FIXME("(%p %p %x %d): Canonicalization for host type %d not supported.\n", data
,
2780 uri
, flags
, computeOnly
, data
->host_type
);
2788 static BOOL
canonicalize_port(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
2789 BOOL has_default_port
= FALSE
;
2790 USHORT default_port
= 0;
2793 uri
->port_offset
= -1;
2795 /* Check if the scheme has a default port. */
2796 for(i
= 0; i
< sizeof(default_ports
)/sizeof(default_ports
[0]); ++i
) {
2797 if(default_ports
[i
].scheme
== data
->scheme_type
) {
2798 has_default_port
= TRUE
;
2799 default_port
= default_ports
[i
].port
;
2804 uri
->has_port
= data
->has_port
|| has_default_port
;
2807 * 1) Has a port which is the default port.
2808 * 2) Has a port (not the default).
2809 * 3) Doesn't have a port, but, scheme has a default port.
2812 if(has_default_port
&& data
->has_port
&& data
->port_value
== default_port
) {
2813 /* If it's the default port and this flag isn't set, don't do anything. */
2814 if(flags
& Uri_CREATE_NO_CANONICALIZE
) {
2815 uri
->port_offset
= uri
->canon_len
-uri
->authority_start
;
2817 uri
->canon_uri
[uri
->canon_len
] = ':';
2821 /* Copy the original port over. */
2823 memcpy(uri
->canon_uri
+uri
->canon_len
, data
->port
, data
->port_len
*sizeof(WCHAR
));
2824 uri
->canon_len
+= data
->port_len
;
2827 uri
->canon_len
+= ui2str(uri
->canon_uri
+uri
->canon_len
, data
->port_value
);
2829 uri
->canon_len
+= ui2str(NULL
, data
->port_value
);
2833 uri
->port
= default_port
;
2834 } else if(data
->has_port
) {
2835 uri
->port_offset
= uri
->canon_len
-uri
->authority_start
;
2837 uri
->canon_uri
[uri
->canon_len
] = ':';
2840 if(flags
& Uri_CREATE_NO_CANONICALIZE
&& data
->port
) {
2841 /* Copy the original over without changes. */
2843 memcpy(uri
->canon_uri
+uri
->canon_len
, data
->port
, data
->port_len
*sizeof(WCHAR
));
2844 uri
->canon_len
+= data
->port_len
;
2847 uri
->canon_len
+= ui2str(uri
->canon_uri
+uri
->canon_len
, data
->port_value
);
2849 uri
->canon_len
+= ui2str(NULL
, data
->port_value
);
2852 uri
->port
= data
->port_value
;
2853 } else if(has_default_port
)
2854 uri
->port
= default_port
;
2859 /* Canonicalizes the authority of the URI represented by the parse_data. */
2860 static BOOL
canonicalize_authority(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
2861 uri
->authority_start
= uri
->canon_len
;
2862 uri
->authority_len
= 0;
2864 if(!canonicalize_userinfo(data
, uri
, flags
, computeOnly
))
2867 if(!canonicalize_host(data
, uri
, flags
, computeOnly
))
2870 if(!canonicalize_port(data
, uri
, flags
, computeOnly
))
2873 if(uri
->host_start
!= -1 || (data
->is_relative
&& (data
->password
|| data
->username
)))
2874 uri
->authority_len
= uri
->canon_len
- uri
->authority_start
;
2876 uri
->authority_start
= -1;
2881 /* Attempts to canonicalize the path of a hierarchical URI.
2883 * Things that happen:
2884 * 1). Forbidden characters are percent encoded, unless the NO_ENCODE_FORBIDDEN
2885 * flag is set or it's a file URI. Forbidden characters are always encoded
2886 * for file schemes reguardless and forbidden characters are never encoded
2887 * for unknown scheme types.
2889 * 2). For known scheme types '\\' are changed to '/'.
2891 * 3). Percent encoded, unreserved characters are decoded to their actual values.
2892 * Unless the scheme type is unknown. For file schemes any percent encoded
2893 * character in the unreserved or reserved set is decoded.
2895 * 4). For File schemes if the path is starts with a drive letter and doesn't
2896 * start with a '/' then one is appended.
2897 * Ex: file://c:/test.mp3 -> file:///c:/test.mp3
2899 * 5). Dot segments are removed from the path for all scheme types
2900 * unless NO_CANONICALIZE flag is set. Dot segments aren't removed
2901 * for wildcard scheme types.
2904 * file://c:/test%20test -> file:///c:/test%2520test
2905 * file://c:/test%3Etest -> file:///c:/test%253Etest
2906 * if Uri_CREATE_FILE_USE_DOS_PATH is not set:
2907 * file:///c:/test%20test -> file:///c:/test%20test
2908 * file:///c:/test%test -> file:///c:/test%25test
2910 static BOOL
canonicalize_path_hierarchical(const parse_data
*data
, Uri
*uri
,
2911 DWORD flags
, BOOL computeOnly
) {
2913 const BOOL known_scheme
= data
->scheme_type
!= URL_SCHEME_UNKNOWN
;
2914 const BOOL is_file
= data
->scheme_type
== URL_SCHEME_FILE
;
2916 BOOL escape_pct
= FALSE
;
2919 uri
->path_start
= -1;
2924 uri
->path_start
= uri
->canon_len
;
2927 if(is_file
&& uri
->host_start
== -1) {
2928 /* Check if a '/' needs to be appended for the file scheme. */
2929 if(data
->path_len
> 1 && is_drive_path(ptr
) && !(flags
& Uri_CREATE_FILE_USE_DOS_PATH
)) {
2931 uri
->canon_uri
[uri
->canon_len
] = '/';
2934 } else if(*ptr
== '/') {
2935 if(!(flags
& Uri_CREATE_FILE_USE_DOS_PATH
)) {
2936 /* Copy the extra '/' over. */
2938 uri
->canon_uri
[uri
->canon_len
] = '/';
2944 if(is_drive_path(ptr
)) {
2946 uri
->canon_uri
[uri
->canon_len
] = *ptr
;
2947 /* If theres a '|' after the drive letter, convert it to a ':'. */
2948 uri
->canon_uri
[uri
->canon_len
+1] = ':';
2951 uri
->canon_len
+= 2;
2955 if(!is_file
&& *(data
->path
) && *(data
->path
) != '/') {
2956 /* Prepend a '/' to the path if it doesn't have one. */
2958 uri
->canon_uri
[uri
->canon_len
] = '/';
2962 for(; ptr
< data
->path
+data
->path_len
; ++ptr
) {
2964 const WCHAR
*tmp
= ptr
;
2967 /* Check if the % represents a valid encoded char, or if it needs encoded. */
2968 BOOL force_encode
= !check_pct_encoded(&tmp
) && is_file
&& !(flags
&Uri_CREATE_FILE_USE_DOS_PATH
);
2969 val
= decode_pct_val(ptr
);
2971 if(force_encode
|| escape_pct
) {
2972 /* Escape the percent sign in the file URI. */
2974 pct_encode_val(*ptr
, uri
->canon_uri
+uri
->canon_len
);
2975 uri
->canon_len
+= 3;
2976 } else if((is_unreserved(val
) && known_scheme
) ||
2977 (is_file
&& (is_unreserved(val
) || is_reserved(val
) ||
2978 (val
&& flags
&Uri_CREATE_FILE_USE_DOS_PATH
&& !is_forbidden_dos_path_char(val
))))) {
2980 uri
->canon_uri
[uri
->canon_len
] = val
;
2987 uri
->canon_uri
[uri
->canon_len
] = *ptr
;
2990 } else if(*ptr
== '/' && is_file
&& (flags
& Uri_CREATE_FILE_USE_DOS_PATH
)) {
2991 /* Convert the '/' back to a '\\'. */
2993 uri
->canon_uri
[uri
->canon_len
] = '\\';
2995 } else if(*ptr
== '\\' && known_scheme
) {
2996 if(is_file
&& (flags
& Uri_CREATE_FILE_USE_DOS_PATH
)) {
2997 /* Don't convert the '\\' to a '/'. */
2999 uri
->canon_uri
[uri
->canon_len
] = *ptr
;
3003 uri
->canon_uri
[uri
->canon_len
] = '/';
3006 } else if(known_scheme
&& !is_unreserved(*ptr
) && !is_reserved(*ptr
) &&
3007 (!(flags
& Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS
) || is_file
)) {
3008 if(is_file
&& (flags
& Uri_CREATE_FILE_USE_DOS_PATH
)) {
3009 /* Don't escape the character. */
3011 uri
->canon_uri
[uri
->canon_len
] = *ptr
;
3014 /* Escape the forbidden character. */
3016 pct_encode_val(*ptr
, uri
->canon_uri
+uri
->canon_len
);
3017 uri
->canon_len
+= 3;
3021 uri
->canon_uri
[uri
->canon_len
] = *ptr
;
3026 uri
->path_len
= uri
->canon_len
- uri
->path_start
;
3028 /* Removing the dot segments only happens when it's not in
3029 * computeOnly mode and it's not a wildcard scheme. File schemes
3030 * with USE_DOS_PATH set don't get dot segments removed.
3032 if(!(is_file
&& (flags
& Uri_CREATE_FILE_USE_DOS_PATH
)) &&
3033 data
->scheme_type
!= URL_SCHEME_WILDCARD
) {
3034 if(!(flags
& Uri_CREATE_NO_CANONICALIZE
) && !computeOnly
) {
3035 /* Remove the dot segments (if any) and reset everything to the new
3038 DWORD new_len
= remove_dot_segments(uri
->canon_uri
+uri
->path_start
, uri
->path_len
);
3039 uri
->canon_len
-= uri
->path_len
-new_len
;
3040 uri
->path_len
= new_len
;
3045 TRACE("Canonicalized path %s len=%d\n",
3046 debugstr_wn(uri
->canon_uri
+uri
->path_start
, uri
->path_len
),
3052 /* Attempts to canonicalize the path for an opaque URI.
3054 * For known scheme types:
3055 * 1) forbidden characters are percent encoded if
3056 * NO_ENCODE_FORBIDDEN_CHARACTERS isn't set.
3058 * 2) Percent encoded, unreserved characters are decoded
3059 * to their actual values, for known scheme types.
3061 * 3) '\\' are changed to '/' for known scheme types
3062 * except for mailto schemes.
3064 * 4) For file schemes, if USE_DOS_PATH is set all '/'
3065 * are converted to backslashes.
3067 * 5) For file schemes, if USE_DOS_PATH isn't set all '\'
3068 * are converted to forward slashes.
3070 static BOOL
canonicalize_path_opaque(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
3072 const BOOL known_scheme
= data
->scheme_type
!= URL_SCHEME_UNKNOWN
;
3073 const BOOL is_file
= data
->scheme_type
== URL_SCHEME_FILE
;
3076 uri
->path_start
= -1;
3081 uri
->path_start
= uri
->canon_len
;
3083 /* Windows doesn't allow a "//" to appear after the scheme
3084 * of a URI, if it's an opaque URI.
3086 if(data
->scheme
&& *(data
->path
) == '/' && *(data
->path
+1) == '/') {
3087 /* So it inserts a "/." before the "//" if it exists. */
3089 uri
->canon_uri
[uri
->canon_len
] = '/';
3090 uri
->canon_uri
[uri
->canon_len
+1] = '.';
3093 uri
->canon_len
+= 2;
3096 for(ptr
= data
->path
; ptr
< data
->path
+data
->path_len
; ++ptr
) {
3097 if(*ptr
== '%' && known_scheme
) {
3098 WCHAR val
= decode_pct_val(ptr
);
3100 if(is_unreserved(val
)) {
3102 uri
->canon_uri
[uri
->canon_len
] = val
;
3109 uri
->canon_uri
[uri
->canon_len
] = *ptr
;
3112 } else if(*ptr
== '/' && is_file
&& (flags
& Uri_CREATE_FILE_USE_DOS_PATH
)) {
3114 uri
->canon_uri
[uri
->canon_len
] = '\\';
3116 } else if(*ptr
== '\\' && is_file
) {
3117 if(!(flags
& Uri_CREATE_FILE_USE_DOS_PATH
)) {
3118 /* Convert to a '/'. */
3120 uri
->canon_uri
[uri
->canon_len
] = '/';
3123 /* Just copy it over. */
3125 uri
->canon_uri
[uri
->canon_len
] = *ptr
;
3128 } else if(known_scheme
&& !is_unreserved(*ptr
) && !is_reserved(*ptr
) &&
3129 !(flags
& Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS
)) {
3130 if(is_file
&& (flags
& Uri_CREATE_FILE_USE_DOS_PATH
)) {
3131 /* Forbidden characters aren't percent encoded for file schemes
3132 * with USE_DOS_PATH set.
3135 uri
->canon_uri
[uri
->canon_len
] = *ptr
;
3137 } else if(data
->scheme_type
== URL_SCHEME_MK
&& *ptr
== '\\') {
3138 /* MK URIs don't get '\\' percent encoded. */
3140 uri
->canon_uri
[uri
->canon_len
] = *ptr
;
3144 pct_encode_val(*ptr
, uri
->canon_uri
+uri
->canon_len
);
3145 uri
->canon_len
+= 3;
3149 uri
->canon_uri
[uri
->canon_len
] = *ptr
;
3154 uri
->path_len
= uri
->canon_len
- uri
->path_start
;
3156 TRACE("(%p %p %x %d): Canonicalized opaque URI path %s len=%d\n", data
, uri
, flags
, computeOnly
,
3157 debugstr_wn(uri
->canon_uri
+uri
->path_start
, uri
->path_len
), uri
->path_len
);
3161 /* Determines how the URI represented by the parse_data should be canonicalized.
3163 * Essentially, if the parse_data represents an hierarchical URI then it calls
3164 * canonicalize_authority and the canonicalization functions for the path. If the
3165 * URI is opaque it canonicalizes the path of the URI.
3167 static BOOL
canonicalize_hierpart(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
3168 if(!data
->is_opaque
|| (data
->is_relative
&& (data
->password
|| data
->username
))) {
3169 /* "//" is only added for non-wildcard scheme types.
3171 * A "//" is only added to a relative URI if it has a
3172 * host or port component (this only happens if a IUriBuilder
3173 * is generating an IUri).
3175 if((data
->is_relative
&& (data
->host
|| data
->has_port
)) ||
3176 (!data
->is_relative
&& data
->scheme_type
!= URL_SCHEME_WILDCARD
)) {
3178 INT pos
= uri
->canon_len
;
3180 uri
->canon_uri
[pos
] = '/';
3181 uri
->canon_uri
[pos
+1] = '/';
3183 uri
->canon_len
+= 2;
3186 if(!canonicalize_authority(data
, uri
, flags
, computeOnly
))
3189 if(data
->is_relative
&& (data
->password
|| data
->username
)) {
3190 if(!canonicalize_path_opaque(data
, uri
, flags
, computeOnly
))
3193 if(!canonicalize_path_hierarchical(data
, uri
, flags
, computeOnly
))
3197 /* Opaque URI's don't have an authority. */
3198 uri
->userinfo_start
= uri
->userinfo_split
= -1;
3199 uri
->userinfo_len
= 0;
3200 uri
->host_start
= -1;
3202 uri
->host_type
= Uri_HOST_UNKNOWN
;
3203 uri
->has_port
= FALSE
;
3204 uri
->authority_start
= -1;
3205 uri
->authority_len
= 0;
3206 uri
->domain_offset
= -1;
3207 uri
->port_offset
= -1;
3209 if(is_hierarchical_scheme(data
->scheme_type
)) {
3212 /* Absolute URIs aren't displayed for known scheme types
3213 * which should be hierarchical URIs.
3215 uri
->display_modifiers
|= URI_DISPLAY_NO_ABSOLUTE_URI
;
3217 /* Windows also sets the port for these (if they have one). */
3218 for(i
= 0; i
< sizeof(default_ports
)/sizeof(default_ports
[0]); ++i
) {
3219 if(data
->scheme_type
== default_ports
[i
].scheme
) {
3220 uri
->has_port
= TRUE
;
3221 uri
->port
= default_ports
[i
].port
;
3227 if(!canonicalize_path_opaque(data
, uri
, flags
, computeOnly
))
3231 if(uri
->path_start
> -1 && !computeOnly
)
3232 /* Finding file extensions happens for both types of URIs. */
3233 uri
->extension_offset
= find_file_extension(uri
->canon_uri
+uri
->path_start
, uri
->path_len
);
3235 uri
->extension_offset
= -1;
3240 /* Attempts to canonicalize the query string of the URI.
3242 * Things that happen:
3243 * 1) For known scheme types forbidden characters
3244 * are percent encoded, unless the NO_DECODE_EXTRA_INFO flag is set
3245 * or NO_ENCODE_FORBIDDEN_CHARACTERS is set.
3247 * 2) For known scheme types, percent encoded, unreserved characters
3248 * are decoded as long as the NO_DECODE_EXTRA_INFO flag isn't set.
3250 static BOOL
canonicalize_query(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
3251 const WCHAR
*ptr
, *end
;
3252 const BOOL known_scheme
= data
->scheme_type
!= URL_SCHEME_UNKNOWN
;
3255 uri
->query_start
= -1;
3260 uri
->query_start
= uri
->canon_len
;
3262 end
= data
->query
+data
->query_len
;
3263 for(ptr
= data
->query
; ptr
< end
; ++ptr
) {
3265 if(known_scheme
&& !(flags
& Uri_CREATE_NO_DECODE_EXTRA_INFO
)) {
3266 WCHAR val
= decode_pct_val(ptr
);
3267 if(is_unreserved(val
)) {
3269 uri
->canon_uri
[uri
->canon_len
] = val
;
3276 } else if(known_scheme
&& !is_unreserved(*ptr
) && !is_reserved(*ptr
)) {
3277 if(!(flags
& Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS
) &&
3278 !(flags
& Uri_CREATE_NO_DECODE_EXTRA_INFO
)) {
3280 pct_encode_val(*ptr
, uri
->canon_uri
+uri
->canon_len
);
3281 uri
->canon_len
+= 3;
3287 uri
->canon_uri
[uri
->canon_len
] = *ptr
;
3291 uri
->query_len
= uri
->canon_len
- uri
->query_start
;
3294 TRACE("(%p %p %x %d): Canonicalized query string %s len=%d\n", data
, uri
, flags
,
3295 computeOnly
, debugstr_wn(uri
->canon_uri
+uri
->query_start
, uri
->query_len
),
3300 static BOOL
canonicalize_fragment(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
3301 const WCHAR
*ptr
, *end
;
3302 const BOOL known_scheme
= data
->scheme_type
!= URL_SCHEME_UNKNOWN
;
3304 if(!data
->fragment
) {
3305 uri
->fragment_start
= -1;
3306 uri
->fragment_len
= 0;
3310 uri
->fragment_start
= uri
->canon_len
;
3312 end
= data
->fragment
+ data
->fragment_len
;
3313 for(ptr
= data
->fragment
; ptr
< end
; ++ptr
) {
3315 if(known_scheme
&& !(flags
& Uri_CREATE_NO_DECODE_EXTRA_INFO
)) {
3316 WCHAR val
= decode_pct_val(ptr
);
3317 if(is_unreserved(val
)) {
3319 uri
->canon_uri
[uri
->canon_len
] = val
;
3326 } else if(known_scheme
&& !is_unreserved(*ptr
) && !is_reserved(*ptr
)) {
3327 if(!(flags
& Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS
) &&
3328 !(flags
& Uri_CREATE_NO_DECODE_EXTRA_INFO
)) {
3330 pct_encode_val(*ptr
, uri
->canon_uri
+uri
->canon_len
);
3331 uri
->canon_len
+= 3;
3337 uri
->canon_uri
[uri
->canon_len
] = *ptr
;
3341 uri
->fragment_len
= uri
->canon_len
- uri
->fragment_start
;
3344 TRACE("(%p %p %x %d): Canonicalized fragment %s len=%d\n", data
, uri
, flags
,
3345 computeOnly
, debugstr_wn(uri
->canon_uri
+uri
->fragment_start
, uri
->fragment_len
),
3350 /* Canonicalizes the scheme information specified in the parse_data using the specified flags. */
3351 static BOOL
canonicalize_scheme(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
3352 uri
->scheme_start
= -1;
3353 uri
->scheme_len
= 0;
3356 /* The only type of URI that doesn't have to have a scheme is a relative
3359 if(!data
->is_relative
) {
3360 FIXME("(%p %p %x): Unable to determine the scheme type of %s.\n", data
,
3361 uri
, flags
, debugstr_w(data
->uri
));
3367 INT pos
= uri
->canon_len
;
3369 for(i
= 0; i
< data
->scheme_len
; ++i
) {
3370 /* Scheme name must be lower case after canonicalization. */
3371 uri
->canon_uri
[i
+ pos
] = tolowerW(data
->scheme
[i
]);
3374 uri
->canon_uri
[i
+ pos
] = ':';
3375 uri
->scheme_start
= pos
;
3377 TRACE("(%p %p %x): Canonicalized scheme=%s, len=%d.\n", data
, uri
, flags
,
3378 debugstr_wn(uri
->canon_uri
, uri
->scheme_len
), data
->scheme_len
);
3381 /* This happens in both computation modes. */
3382 uri
->canon_len
+= data
->scheme_len
+ 1;
3383 uri
->scheme_len
= data
->scheme_len
;
3388 /* Compute's what the length of the URI specified by the parse_data will be
3389 * after canonicalization occurs using the specified flags.
3391 * This function will return a non-zero value indicating the length of the canonicalized
3392 * URI, or -1 on error.
3394 static int compute_canonicalized_length(const parse_data
*data
, DWORD flags
) {
3397 memset(&uri
, 0, sizeof(Uri
));
3399 TRACE("(%p %x): Beginning to compute canonicalized length for URI %s\n", data
, flags
,
3400 debugstr_w(data
->uri
));
3402 if(!canonicalize_scheme(data
, &uri
, flags
, TRUE
)) {
3403 ERR("(%p %x): Failed to compute URI scheme length.\n", data
, flags
);
3407 if(!canonicalize_hierpart(data
, &uri
, flags
, TRUE
)) {
3408 ERR("(%p %x): Failed to compute URI hierpart length.\n", data
, flags
);
3412 if(!canonicalize_query(data
, &uri
, flags
, TRUE
)) {
3413 ERR("(%p %x): Failed to compute query string length.\n", data
, flags
);
3417 if(!canonicalize_fragment(data
, &uri
, flags
, TRUE
)) {
3418 ERR("(%p %x): Failed to compute fragment length.\n", data
, flags
);
3422 TRACE("(%p %x): Finished computing canonicalized URI length. length=%d\n", data
, flags
, uri
.canon_len
);
3424 return uri
.canon_len
;
3427 /* Canonicalizes the URI data specified in the parse_data, using the given flags. If the
3428 * canonicalization succeededs it will store all the canonicalization information
3429 * in the pointer to the Uri.
3431 * To canonicalize a URI this function first computes what the length of the URI
3432 * specified by the parse_data will be. Once this is done it will then perfom the actual
3433 * canonicalization of the URI.
3435 static HRESULT
canonicalize_uri(const parse_data
*data
, Uri
*uri
, DWORD flags
) {
3438 uri
->canon_uri
= NULL
;
3439 len
= uri
->canon_size
= uri
->canon_len
= 0;
3441 TRACE("(%p %p %x): beginning to canonicalize URI %s.\n", data
, uri
, flags
, debugstr_w(data
->uri
));
3443 /* First try to compute the length of the URI. */
3444 len
= compute_canonicalized_length(data
, flags
);
3446 ERR("(%p %p %x): Could not compute the canonicalized length of %s.\n", data
, uri
, flags
,
3447 debugstr_w(data
->uri
));
3448 return E_INVALIDARG
;
3451 uri
->canon_uri
= heap_alloc((len
+1)*sizeof(WCHAR
));
3453 return E_OUTOFMEMORY
;
3455 uri
->canon_size
= len
;
3456 if(!canonicalize_scheme(data
, uri
, flags
, FALSE
)) {
3457 ERR("(%p %p %x): Unable to canonicalize the scheme of the URI.\n", data
, uri
, flags
);
3458 return E_INVALIDARG
;
3460 uri
->scheme_type
= data
->scheme_type
;
3462 if(!canonicalize_hierpart(data
, uri
, flags
, FALSE
)) {
3463 ERR("(%p %p %x): Unable to canonicalize the heirpart of the URI\n", data
, uri
, flags
);
3464 return E_INVALIDARG
;
3467 if(!canonicalize_query(data
, uri
, flags
, FALSE
)) {
3468 ERR("(%p %p %x): Unable to canonicalize query string of the URI.\n",
3470 return E_INVALIDARG
;
3473 if(!canonicalize_fragment(data
, uri
, flags
, FALSE
)) {
3474 ERR("(%p %p %x): Unable to canonicalize fragment of the URI.\n",
3476 return E_INVALIDARG
;
3479 /* There's a possibility we didn't use all the space we allocated
3482 if(uri
->canon_len
< uri
->canon_size
) {
3483 /* This happens if the URI is hierarchical and dot
3484 * segments were removed from it's path.
3486 WCHAR
*tmp
= heap_realloc(uri
->canon_uri
, (uri
->canon_len
+1)*sizeof(WCHAR
));
3488 return E_OUTOFMEMORY
;
3490 uri
->canon_uri
= tmp
;
3491 uri
->canon_size
= uri
->canon_len
;
3494 uri
->canon_uri
[uri
->canon_len
] = '\0';
3495 TRACE("(%p %p %x): finished canonicalizing the URI. uri=%s\n", data
, uri
, flags
, debugstr_w(uri
->canon_uri
));
3500 static HRESULT
get_builder_component(LPWSTR
*component
, DWORD
*component_len
,
3501 LPCWSTR source
, DWORD source_len
,
3502 LPCWSTR
*output
, DWORD
*output_len
)
3515 if(!(*component
) && source
) {
3516 /* Allocate 'component', and copy the contents from 'source'
3517 * into the new allocation.
3519 *component
= heap_alloc((source_len
+1)*sizeof(WCHAR
));
3521 return E_OUTOFMEMORY
;
3523 memcpy(*component
, source
, source_len
*sizeof(WCHAR
));
3524 (*component
)[source_len
] = '\0';
3525 *component_len
= source_len
;
3528 *output
= *component
;
3529 *output_len
= *component_len
;
3530 return *output
? S_OK
: S_FALSE
;
3533 /* Allocates 'component' and copies the string from 'new_value' into 'component'.
3534 * If 'prefix' is set and 'new_value' isn't NULL, then it checks if 'new_value'
3535 * starts with 'prefix'. If it doesn't then 'prefix' is prepended to 'component'.
3537 * If everything is successful, then will set 'success_flag' in 'flags'.
3539 static HRESULT
set_builder_component(LPWSTR
*component
, DWORD
*component_len
, LPCWSTR new_value
,
3540 WCHAR prefix
, DWORD
*flags
, DWORD success_flag
)
3542 heap_free(*component
);
3548 BOOL add_prefix
= FALSE
;
3549 DWORD len
= lstrlenW(new_value
);
3552 if(prefix
&& *new_value
!= prefix
) {
3554 *component
= heap_alloc((len
+2)*sizeof(WCHAR
));
3556 *component
= heap_alloc((len
+1)*sizeof(WCHAR
));
3559 return E_OUTOFMEMORY
;
3562 (*component
)[pos
++] = prefix
;
3564 memcpy(*component
+pos
, new_value
, (len
+1)*sizeof(WCHAR
));
3565 *component_len
= len
+pos
;
3568 *flags
|= success_flag
;
3572 #define URI(x) ((IUri*) &(x)->lpIUriVtbl)
3573 #define URIBUILDER(x) ((IUriBuilder*) &(x)->lpIUriBuilderVtbl)
3575 static void reset_builder(UriBuilder
*builder
) {
3577 IUri_Release(URI(builder
->uri
));
3578 builder
->uri
= NULL
;
3580 heap_free(builder
->fragment
);
3581 builder
->fragment
= NULL
;
3582 builder
->fragment_len
= 0;
3584 heap_free(builder
->host
);
3585 builder
->host
= NULL
;
3586 builder
->host_len
= 0;
3588 heap_free(builder
->password
);
3589 builder
->password
= NULL
;
3590 builder
->password_len
= 0;
3592 heap_free(builder
->path
);
3593 builder
->path
= NULL
;
3594 builder
->path_len
= 0;
3596 heap_free(builder
->query
);
3597 builder
->query
= NULL
;
3598 builder
->query_len
= 0;
3600 heap_free(builder
->scheme
);
3601 builder
->scheme
= NULL
;
3602 builder
->scheme_len
= 0;
3604 heap_free(builder
->username
);
3605 builder
->username
= NULL
;
3606 builder
->username_len
= 0;
3608 builder
->has_port
= FALSE
;
3610 builder
->modified_props
= 0;
3613 static HRESULT
validate_scheme_name(const UriBuilder
*builder
, parse_data
*data
, DWORD flags
) {
3614 const WCHAR
*component
;
3619 if(builder
->scheme
) {
3620 ptr
= builder
->scheme
;
3621 expected_len
= builder
->scheme_len
;
3622 } else if(builder
->uri
&& builder
->uri
->scheme_start
> -1) {
3623 ptr
= builder
->uri
->canon_uri
+builder
->uri
->scheme_start
;
3624 expected_len
= builder
->uri
->scheme_len
;
3626 static const WCHAR nullW
[] = {0};
3633 if(parse_scheme(pptr
, data
, flags
, ALLOW_NULL_TERM_SCHEME
) &&
3634 data
->scheme_len
== expected_len
) {
3636 TRACE("(%p %p %x): Found valid scheme component %s len=%d.\n", builder
, data
, flags
,
3637 debugstr_wn(data
->scheme
, data
->scheme_len
), data
->scheme_len
);
3639 TRACE("(%p %p %x): Invalid scheme component found %s.\n", builder
, data
, flags
,
3640 debugstr_wn(component
, expected_len
));
3641 return INET_E_INVALID_URL
;
3647 static HRESULT
validate_username(const UriBuilder
*builder
, parse_data
*data
, DWORD flags
) {
3652 if(builder
->username
) {
3653 ptr
= builder
->username
;
3654 expected_len
= builder
->username_len
;
3655 } else if(!(builder
->modified_props
& Uri_HAS_USER_NAME
) && builder
->uri
&&
3656 builder
->uri
->userinfo_start
> -1 && builder
->uri
->userinfo_split
!= 0) {
3657 /* Just use the username from the base Uri. */
3658 data
->username
= builder
->uri
->canon_uri
+builder
->uri
->userinfo_start
;
3659 data
->username_len
= (builder
->uri
->userinfo_split
> -1) ?
3660 builder
->uri
->userinfo_split
: builder
->uri
->userinfo_len
;
3668 const WCHAR
*component
= ptr
;
3670 if(parse_username(pptr
, data
, flags
, ALLOW_NULL_TERM_USER_NAME
) &&
3671 data
->username_len
== expected_len
)
3672 TRACE("(%p %p %x): Found valid username component %s len=%d.\n", builder
, data
, flags
,
3673 debugstr_wn(data
->username
, data
->username_len
), data
->username_len
);
3675 TRACE("(%p %p %x): Invalid username component found %s.\n", builder
, data
, flags
,
3676 debugstr_wn(component
, expected_len
));
3677 return INET_E_INVALID_URL
;
3684 static HRESULT
validate_password(const UriBuilder
*builder
, parse_data
*data
, DWORD flags
) {
3689 if(builder
->password
) {
3690 ptr
= builder
->password
;
3691 expected_len
= builder
->password_len
;
3692 } else if(!(builder
->modified_props
& Uri_HAS_PASSWORD
) && builder
->uri
&&
3693 builder
->uri
->userinfo_split
> -1) {
3694 data
->password
= builder
->uri
->canon_uri
+builder
->uri
->userinfo_start
+builder
->uri
->userinfo_split
+1;
3695 data
->password_len
= builder
->uri
->userinfo_len
-builder
->uri
->userinfo_split
-1;
3703 const WCHAR
*component
= ptr
;
3705 if(parse_password(pptr
, data
, flags
, ALLOW_NULL_TERM_PASSWORD
) &&
3706 data
->password_len
== expected_len
)
3707 TRACE("(%p %p %x): Found valid password component %s len=%d.\n", builder
, data
, flags
,
3708 debugstr_wn(data
->password
, data
->password_len
), data
->password_len
);
3710 TRACE("(%p %p %x): Invalid password component found %s.\n", builder
, data
, flags
,
3711 debugstr_wn(component
, expected_len
));
3712 return INET_E_INVALID_URL
;
3719 static HRESULT
validate_userinfo(const UriBuilder
*builder
, parse_data
*data
, DWORD flags
) {
3722 hr
= validate_username(builder
, data
, flags
);
3726 hr
= validate_password(builder
, data
, flags
);
3733 static HRESULT
validate_host(const UriBuilder
*builder
, parse_data
*data
, DWORD flags
) {
3739 ptr
= builder
->host
;
3740 expected_len
= builder
->host_len
;
3741 } else if(!(builder
->modified_props
& Uri_HAS_HOST
) && builder
->uri
&& builder
->uri
->host_start
> -1) {
3742 ptr
= builder
->uri
->canon_uri
+ builder
->uri
->host_start
;
3743 expected_len
= builder
->uri
->host_len
;
3748 const WCHAR
*component
= ptr
;
3749 DWORD extras
= ALLOW_BRACKETLESS_IP_LITERAL
|IGNORE_PORT_DELIMITER
|SKIP_IP_FUTURE_CHECK
;
3752 if(parse_host(pptr
, data
, flags
, extras
) && data
->host_len
== expected_len
)
3753 TRACE("(%p %p %x): Found valid host name %s len=%d type=%d.\n", builder
, data
, flags
,
3754 debugstr_wn(data
->host
, data
->host_len
), data
->host_len
, data
->host_type
);
3756 TRACE("(%p %p %x): Invalid host name found %s.\n", builder
, data
, flags
,
3757 debugstr_wn(component
, expected_len
));
3758 return INET_E_INVALID_URL
;
3765 static void setup_port(const UriBuilder
*builder
, parse_data
*data
, DWORD flags
) {
3766 if(builder
->modified_props
& Uri_HAS_PORT
) {
3767 if(builder
->has_port
) {
3768 data
->has_port
= TRUE
;
3769 data
->port_value
= builder
->port
;
3771 } else if(builder
->uri
&& builder
->uri
->has_port
) {
3772 data
->has_port
= TRUE
;
3773 data
->port_value
= builder
->uri
->port
;
3777 TRACE("(%p %p %x): Using %u as port for IUri.\n", builder
, data
, flags
, data
->port_value
);
3780 static HRESULT
validate_path(const UriBuilder
*builder
, parse_data
*data
, DWORD flags
) {
3781 const WCHAR
*ptr
= NULL
;
3782 const WCHAR
*component
;
3785 BOOL check_len
= TRUE
;
3789 ptr
= builder
->path
;
3790 expected_len
= builder
->path_len
;
3791 } else if(!(builder
->modified_props
& Uri_HAS_PATH
) &&
3792 builder
->uri
&& builder
->uri
->path_start
> -1) {
3793 ptr
= builder
->uri
->canon_uri
+builder
->uri
->path_start
;
3794 expected_len
= builder
->uri
->path_len
;
3796 static const WCHAR nullW
[] = {0};
3804 /* How the path is validated depends on what type of
3807 valid
= data
->is_opaque
?
3808 parse_path_opaque(pptr
, data
, flags
) : parse_path_hierarchical(pptr
, data
, flags
);
3810 if(!valid
|| (check_len
&& expected_len
!= data
->path_len
)) {
3811 TRACE("(%p %p %x): Invalid path component %s.\n", builder
, data
, flags
,
3812 debugstr_wn(component
, expected_len
));
3813 return INET_E_INVALID_URL
;
3816 TRACE("(%p %p %x): Valid path component %s len=%d.\n", builder
, data
, flags
,
3817 debugstr_wn(data
->path
, data
->path_len
), data
->path_len
);
3822 static HRESULT
validate_query(const UriBuilder
*builder
, parse_data
*data
, DWORD flags
) {
3823 const WCHAR
*ptr
= NULL
;
3827 if(builder
->query
) {
3828 ptr
= builder
->query
;
3829 expected_len
= builder
->query_len
;
3830 } else if(!(builder
->modified_props
& Uri_HAS_QUERY
) && builder
->uri
&&
3831 builder
->uri
->query_start
> -1) {
3832 ptr
= builder
->uri
->canon_uri
+builder
->uri
->query_start
;
3833 expected_len
= builder
->uri
->query_len
;
3837 const WCHAR
*component
= ptr
;
3840 if(parse_query(pptr
, data
, flags
) && expected_len
== data
->query_len
)
3841 TRACE("(%p %p %x): Valid query component %s len=%d.\n", builder
, data
, flags
,
3842 debugstr_wn(data
->query
, data
->query_len
), data
->query_len
);
3844 TRACE("(%p %p %x): Invalid query component %s.\n", builder
, data
, flags
,
3845 debugstr_wn(component
, expected_len
));
3846 return INET_E_INVALID_URL
;
3853 static HRESULT
validate_fragment(const UriBuilder
*builder
, parse_data
*data
, DWORD flags
) {
3854 const WCHAR
*ptr
= NULL
;
3858 if(builder
->fragment
) {
3859 ptr
= builder
->fragment
;
3860 expected_len
= builder
->fragment_len
;
3861 } else if(!(builder
->modified_props
& Uri_HAS_FRAGMENT
) && builder
->uri
&&
3862 builder
->uri
->fragment_start
> -1) {
3863 ptr
= builder
->uri
->canon_uri
+builder
->uri
->fragment_start
;
3864 expected_len
= builder
->uri
->fragment_len
;
3868 const WCHAR
*component
= ptr
;
3871 if(parse_fragment(pptr
, data
, flags
) && expected_len
== data
->fragment_len
)
3872 TRACE("(%p %p %x): Valid fragment component %s len=%d.\n", builder
, data
, flags
,
3873 debugstr_wn(data
->fragment
, data
->fragment_len
), data
->fragment_len
);
3875 TRACE("(%p %p %x): Invalid fragment component %s.\n", builder
, data
, flags
,
3876 debugstr_wn(component
, expected_len
));
3877 return INET_E_INVALID_URL
;
3884 static HRESULT
validate_components(const UriBuilder
*builder
, parse_data
*data
, DWORD flags
) {
3887 memset(data
, 0, sizeof(parse_data
));
3889 TRACE("(%p %p %x): Beginning to validate builder components.\n", builder
, data
, flags
);
3891 hr
= validate_scheme_name(builder
, data
, flags
);
3895 /* Extra validation for file schemes. */
3896 if(data
->scheme_type
== URL_SCHEME_FILE
) {
3897 if((builder
->password
|| (builder
->uri
&& builder
->uri
->userinfo_split
> -1)) ||
3898 (builder
->username
|| (builder
->uri
&& builder
->uri
->userinfo_start
> -1))) {
3899 TRACE("(%p %p %x): File schemes can't contain a username or password.\n",
3900 builder
, data
, flags
);
3901 return INET_E_INVALID_URL
;
3905 hr
= validate_userinfo(builder
, data
, flags
);
3909 hr
= validate_host(builder
, data
, flags
);
3913 setup_port(builder
, data
, flags
);
3915 /* The URI is opaque if it doesn't have an authority component. */
3916 if(!data
->is_relative
)
3917 data
->is_opaque
= !data
->username
&& !data
->password
&& !data
->host
&& !data
->has_port
;
3919 data
->is_opaque
= !data
->host
&& !data
->has_port
;
3921 hr
= validate_path(builder
, data
, flags
);
3925 hr
= validate_query(builder
, data
, flags
);
3929 hr
= validate_fragment(builder
, data
, flags
);
3933 TRACE("(%p %p %x): Finished validating builder components.\n", builder
, data
, flags
);
3938 /* Generates a raw uri string using the parse_data. */
3939 static DWORD
generate_raw_uri(const parse_data
*data
, BSTR uri
, DWORD flags
) {
3944 memcpy(uri
, data
->scheme
, data
->scheme_len
*sizeof(WCHAR
));
3945 uri
[data
->scheme_len
] = ':';
3947 length
+= data
->scheme_len
+1;
3950 if(!data
->is_opaque
) {
3951 /* For the "//" which appears before the authority component. */
3954 uri
[length
+1] = '/';
3959 if(data
->username
) {
3961 memcpy(uri
+length
, data
->username
, data
->username_len
*sizeof(WCHAR
));
3962 length
+= data
->username_len
;
3965 if(data
->password
) {
3968 memcpy(uri
+length
+1, data
->password
, data
->password_len
*sizeof(WCHAR
));
3970 length
+= data
->password_len
+1;
3973 if(data
->password
|| data
->username
) {
3980 /* IPv6 addresses get the brackets added around them if they don't already
3983 const BOOL add_brackets
= data
->host_type
== Uri_HOST_IPV6
&& *(data
->host
) != '[';
3991 memcpy(uri
+length
, data
->host
, data
->host_len
*sizeof(WCHAR
));
3992 length
+= data
->host_len
;
4001 if(data
->has_port
) {
4002 /* The port isn't included in the raw uri if it's the default
4003 * port for the scheme type.
4006 BOOL is_default
= FALSE
;
4008 for(i
= 0; i
< sizeof(default_ports
)/sizeof(default_ports
[0]); ++i
) {
4009 if(data
->scheme_type
== default_ports
[i
].scheme
&&
4010 data
->port_value
== default_ports
[i
].port
)
4014 if(!is_default
|| flags
& RAW_URI_FORCE_PORT_DISP
) {
4020 length
+= ui2str(uri
+length
, data
->port_value
);
4022 length
+= ui2str(NULL
, data
->port_value
);
4026 /* Check if a '/' should be added before the path for hierarchical URIs. */
4027 if(!data
->is_opaque
&& data
->path
&& *(data
->path
) != '/') {
4035 memcpy(uri
+length
, data
->path
, data
->path_len
*sizeof(WCHAR
));
4036 length
+= data
->path_len
;
4041 memcpy(uri
+length
, data
->query
, data
->query_len
*sizeof(WCHAR
));
4042 length
+= data
->query_len
;
4045 if(data
->fragment
) {
4047 memcpy(uri
+length
, data
->fragment
, data
->fragment_len
*sizeof(WCHAR
));
4048 length
+= data
->fragment_len
;
4052 TRACE("(%p %p): Generated raw uri=%s len=%d\n", data
, uri
, debugstr_wn(uri
, length
), length
);
4054 TRACE("(%p %p): Computed raw uri len=%d\n", data
, uri
, length
);
4059 static HRESULT
generate_uri(const UriBuilder
*builder
, const parse_data
*data
, Uri
*uri
, DWORD flags
) {
4061 DWORD length
= generate_raw_uri(data
, NULL
, 0);
4062 uri
->raw_uri
= SysAllocStringLen(NULL
, length
);
4064 return E_OUTOFMEMORY
;
4066 generate_raw_uri(data
, uri
->raw_uri
, 0);
4068 hr
= canonicalize_uri(data
, uri
, flags
);
4070 if(hr
== E_INVALIDARG
)
4071 return INET_E_INVALID_URL
;
4075 uri
->create_flags
= flags
;
4079 #define URI_THIS(iface) DEFINE_THIS(Uri, IUri, iface)
4081 static HRESULT WINAPI
Uri_QueryInterface(IUri
*iface
, REFIID riid
, void **ppv
)
4083 Uri
*This
= URI_THIS(iface
);
4085 if(IsEqualGUID(&IID_IUnknown
, riid
)) {
4086 TRACE("(%p)->(IID_IUnknown %p)\n", This
, ppv
);
4088 }else if(IsEqualGUID(&IID_IUri
, riid
)) {
4089 TRACE("(%p)->(IID_IUri %p)\n", This
, ppv
);
4091 }else if(IsEqualGUID(&IID_IUriObj
, riid
)) {
4092 TRACE("(%p)->(IID_IUriObj %p)\n", This
, ppv
);
4096 TRACE("(%p)->(%s %p)\n", This
, debugstr_guid(riid
), ppv
);
4098 return E_NOINTERFACE
;
4101 IUnknown_AddRef((IUnknown
*)*ppv
);
4105 static ULONG WINAPI
Uri_AddRef(IUri
*iface
)
4107 Uri
*This
= URI_THIS(iface
);
4108 LONG ref
= InterlockedIncrement(&This
->ref
);
4110 TRACE("(%p) ref=%d\n", This
, ref
);
4115 static ULONG WINAPI
Uri_Release(IUri
*iface
)
4117 Uri
*This
= URI_THIS(iface
);
4118 LONG ref
= InterlockedDecrement(&This
->ref
);
4120 TRACE("(%p) ref=%d\n", This
, ref
);
4123 SysFreeString(This
->raw_uri
);
4124 heap_free(This
->canon_uri
);
4131 static HRESULT WINAPI
Uri_GetPropertyBSTR(IUri
*iface
, Uri_PROPERTY uriProp
, BSTR
*pbstrProperty
, DWORD dwFlags
)
4133 Uri
*This
= URI_THIS(iface
);
4135 TRACE("(%p)->(%d %p %x)\n", This
, uriProp
, pbstrProperty
, dwFlags
);
4140 if(uriProp
> Uri_PROPERTY_STRING_LAST
) {
4141 /* Windows allocates an empty BSTR for invalid Uri_PROPERTY's. */
4142 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
4143 if(!(*pbstrProperty
))
4144 return E_OUTOFMEMORY
;
4146 /* It only returns S_FALSE for the ZONE property... */
4147 if(uriProp
== Uri_PROPERTY_ZONE
)
4153 /* Don't have support for flags yet. */
4155 FIXME("(%p)->(%d %p %x)\n", This
, uriProp
, pbstrProperty
, dwFlags
);
4160 case Uri_PROPERTY_ABSOLUTE_URI
:
4161 if(This
->display_modifiers
& URI_DISPLAY_NO_ABSOLUTE_URI
) {
4162 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
4165 if(This
->scheme_type
!= URL_SCHEME_UNKNOWN
&& This
->userinfo_start
> -1) {
4166 if(This
->userinfo_len
== 0) {
4167 /* Don't include the '@' after the userinfo component. */
4168 *pbstrProperty
= SysAllocStringLen(NULL
, This
->canon_len
-1);
4170 if(*pbstrProperty
) {
4171 /* Copy everything before it. */
4172 memcpy(*pbstrProperty
, This
->canon_uri
, This
->userinfo_start
*sizeof(WCHAR
));
4174 /* And everything after it. */
4175 memcpy(*pbstrProperty
+This
->userinfo_start
, This
->canon_uri
+This
->userinfo_start
+1,
4176 (This
->canon_len
-This
->userinfo_start
-1)*sizeof(WCHAR
));
4178 } else if(This
->userinfo_split
== 0 && This
->userinfo_len
== 1) {
4179 /* Don't include the ":@" */
4180 *pbstrProperty
= SysAllocStringLen(NULL
, This
->canon_len
-2);
4182 if(*pbstrProperty
) {
4183 memcpy(*pbstrProperty
, This
->canon_uri
, This
->userinfo_start
*sizeof(WCHAR
));
4184 memcpy(*pbstrProperty
+This
->userinfo_start
, This
->canon_uri
+This
->userinfo_start
+2,
4185 (This
->canon_len
-This
->userinfo_start
-2)*sizeof(WCHAR
));
4188 *pbstrProperty
= SysAllocString(This
->canon_uri
);
4192 *pbstrProperty
= SysAllocString(This
->canon_uri
);
4197 if(!(*pbstrProperty
))
4198 hres
= E_OUTOFMEMORY
;
4201 case Uri_PROPERTY_AUTHORITY
:
4202 if(This
->authority_start
> -1) {
4203 if(This
->port_offset
> -1 && is_default_port(This
->scheme_type
, This
->port
) &&
4204 This
->display_modifiers
& URI_DISPLAY_NO_DEFAULT_PORT_AUTH
)
4205 /* Don't include the port in the authority component. */
4206 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+This
->authority_start
, This
->port_offset
);
4208 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+This
->authority_start
, This
->authority_len
);
4211 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
4215 if(!(*pbstrProperty
))
4216 hres
= E_OUTOFMEMORY
;
4219 case Uri_PROPERTY_DISPLAY_URI
:
4220 /* The Display URI contains everything except for the userinfo for known
4223 if(This
->scheme_type
!= URL_SCHEME_UNKNOWN
&& This
->userinfo_start
> -1) {
4224 *pbstrProperty
= SysAllocStringLen(NULL
, This
->canon_len
-This
->userinfo_len
);
4226 if(*pbstrProperty
) {
4227 /* Copy everything before the userinfo over. */
4228 memcpy(*pbstrProperty
, This
->canon_uri
, This
->userinfo_start
*sizeof(WCHAR
));
4229 /* Copy everything after the userinfo over. */
4230 memcpy(*pbstrProperty
+This
->userinfo_start
,
4231 This
->canon_uri
+This
->userinfo_start
+This
->userinfo_len
+1,
4232 (This
->canon_len
-(This
->userinfo_start
+This
->userinfo_len
+1))*sizeof(WCHAR
));
4235 *pbstrProperty
= SysAllocString(This
->canon_uri
);
4237 if(!(*pbstrProperty
))
4238 hres
= E_OUTOFMEMORY
;
4243 case Uri_PROPERTY_DOMAIN
:
4244 if(This
->domain_offset
> -1) {
4245 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+This
->host_start
+This
->domain_offset
,
4246 This
->host_len
-This
->domain_offset
);
4249 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
4253 if(!(*pbstrProperty
))
4254 hres
= E_OUTOFMEMORY
;
4257 case Uri_PROPERTY_EXTENSION
:
4258 if(This
->extension_offset
> -1) {
4259 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+This
->path_start
+This
->extension_offset
,
4260 This
->path_len
-This
->extension_offset
);
4263 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
4267 if(!(*pbstrProperty
))
4268 hres
= E_OUTOFMEMORY
;
4271 case Uri_PROPERTY_FRAGMENT
:
4272 if(This
->fragment_start
> -1) {
4273 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+This
->fragment_start
, This
->fragment_len
);
4276 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
4280 if(!(*pbstrProperty
))
4281 hres
= E_OUTOFMEMORY
;
4284 case Uri_PROPERTY_HOST
:
4285 if(This
->host_start
> -1) {
4286 /* The '[' and ']' aren't included for IPv6 addresses. */
4287 if(This
->host_type
== Uri_HOST_IPV6
)
4288 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+This
->host_start
+1, This
->host_len
-2);
4290 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+This
->host_start
, This
->host_len
);
4294 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
4298 if(!(*pbstrProperty
))
4299 hres
= E_OUTOFMEMORY
;
4302 case Uri_PROPERTY_PASSWORD
:
4303 if(This
->userinfo_split
> -1) {
4304 *pbstrProperty
= SysAllocStringLen(
4305 This
->canon_uri
+This
->userinfo_start
+This
->userinfo_split
+1,
4306 This
->userinfo_len
-This
->userinfo_split
-1);
4309 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
4313 if(!(*pbstrProperty
))
4314 return E_OUTOFMEMORY
;
4317 case Uri_PROPERTY_PATH
:
4318 if(This
->path_start
> -1) {
4319 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+This
->path_start
, This
->path_len
);
4322 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
4326 if(!(*pbstrProperty
))
4327 hres
= E_OUTOFMEMORY
;
4330 case Uri_PROPERTY_PATH_AND_QUERY
:
4331 if(This
->path_start
> -1) {
4332 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+This
->path_start
, This
->path_len
+This
->query_len
);
4334 } else if(This
->query_start
> -1) {
4335 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+This
->query_start
, This
->query_len
);
4338 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
4342 if(!(*pbstrProperty
))
4343 hres
= E_OUTOFMEMORY
;
4346 case Uri_PROPERTY_QUERY
:
4347 if(This
->query_start
> -1) {
4348 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+This
->query_start
, This
->query_len
);
4351 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
4355 if(!(*pbstrProperty
))
4356 hres
= E_OUTOFMEMORY
;
4359 case Uri_PROPERTY_RAW_URI
:
4360 *pbstrProperty
= SysAllocString(This
->raw_uri
);
4361 if(!(*pbstrProperty
))
4362 hres
= E_OUTOFMEMORY
;
4366 case Uri_PROPERTY_SCHEME_NAME
:
4367 if(This
->scheme_start
> -1) {
4368 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+ This
->scheme_start
, This
->scheme_len
);
4371 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
4375 if(!(*pbstrProperty
))
4376 hres
= E_OUTOFMEMORY
;
4379 case Uri_PROPERTY_USER_INFO
:
4380 if(This
->userinfo_start
> -1) {
4381 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+This
->userinfo_start
, This
->userinfo_len
);
4384 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
4388 if(!(*pbstrProperty
))
4389 hres
= E_OUTOFMEMORY
;
4392 case Uri_PROPERTY_USER_NAME
:
4393 if(This
->userinfo_start
> -1 && This
->userinfo_split
!= 0) {
4394 /* If userinfo_split is set, that means a password exists
4395 * so the username is only from userinfo_start to userinfo_split.
4397 if(This
->userinfo_split
> -1) {
4398 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+ This
->userinfo_start
, This
->userinfo_split
);
4401 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+ This
->userinfo_start
, This
->userinfo_len
);
4405 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
4409 if(!(*pbstrProperty
))
4410 return E_OUTOFMEMORY
;
4414 FIXME("(%p)->(%d %p %x)\n", This
, uriProp
, pbstrProperty
, dwFlags
);
4421 static HRESULT WINAPI
Uri_GetPropertyLength(IUri
*iface
, Uri_PROPERTY uriProp
, DWORD
*pcchProperty
, DWORD dwFlags
)
4423 Uri
*This
= URI_THIS(iface
);
4425 TRACE("(%p)->(%d %p %x)\n", This
, uriProp
, pcchProperty
, dwFlags
);
4428 return E_INVALIDARG
;
4430 /* Can only return a length for a property if it's a string. */
4431 if(uriProp
> Uri_PROPERTY_STRING_LAST
)
4432 return E_INVALIDARG
;
4434 /* Don't have support for flags yet. */
4436 FIXME("(%p)->(%d %p %x)\n", This
, uriProp
, pcchProperty
, dwFlags
);
4441 case Uri_PROPERTY_ABSOLUTE_URI
:
4442 if(This
->display_modifiers
& URI_DISPLAY_NO_ABSOLUTE_URI
) {
4446 if(This
->scheme_type
!= URL_SCHEME_UNKNOWN
) {
4447 if(This
->userinfo_start
> -1 && This
->userinfo_len
== 0)
4448 /* Don't include the '@' in the length. */
4449 *pcchProperty
= This
->canon_len
-1;
4450 else if(This
->userinfo_start
> -1 && This
->userinfo_len
== 1 &&
4451 This
->userinfo_split
== 0)
4452 /* Don't include the ":@" in the length. */
4453 *pcchProperty
= This
->canon_len
-2;
4455 *pcchProperty
= This
->canon_len
;
4457 *pcchProperty
= This
->canon_len
;
4463 case Uri_PROPERTY_AUTHORITY
:
4464 if(This
->port_offset
> -1 &&
4465 This
->display_modifiers
& URI_DISPLAY_NO_DEFAULT_PORT_AUTH
&&
4466 is_default_port(This
->scheme_type
, This
->port
))
4467 /* Only count up until the port in the authority. */
4468 *pcchProperty
= This
->port_offset
;
4470 *pcchProperty
= This
->authority_len
;
4471 hres
= (This
->authority_start
> -1) ? S_OK
: S_FALSE
;
4473 case Uri_PROPERTY_DISPLAY_URI
:
4474 if(This
->scheme_type
!= URL_SCHEME_UNKNOWN
&& This
->userinfo_start
> -1)
4475 *pcchProperty
= This
->canon_len
-This
->userinfo_len
-1;
4477 *pcchProperty
= This
->canon_len
;
4481 case Uri_PROPERTY_DOMAIN
:
4482 if(This
->domain_offset
> -1)
4483 *pcchProperty
= This
->host_len
- This
->domain_offset
;
4487 hres
= (This
->domain_offset
> -1) ? S_OK
: S_FALSE
;
4489 case Uri_PROPERTY_EXTENSION
:
4490 if(This
->extension_offset
> -1) {
4491 *pcchProperty
= This
->path_len
- This
->extension_offset
;
4499 case Uri_PROPERTY_FRAGMENT
:
4500 *pcchProperty
= This
->fragment_len
;
4501 hres
= (This
->fragment_start
> -1) ? S_OK
: S_FALSE
;
4503 case Uri_PROPERTY_HOST
:
4504 *pcchProperty
= This
->host_len
;
4506 /* '[' and ']' aren't included in the length. */
4507 if(This
->host_type
== Uri_HOST_IPV6
)
4510 hres
= (This
->host_start
> -1) ? S_OK
: S_FALSE
;
4512 case Uri_PROPERTY_PASSWORD
:
4513 *pcchProperty
= (This
->userinfo_split
> -1) ? This
->userinfo_len
-This
->userinfo_split
-1 : 0;
4514 hres
= (This
->userinfo_split
> -1) ? S_OK
: S_FALSE
;
4516 case Uri_PROPERTY_PATH
:
4517 *pcchProperty
= This
->path_len
;
4518 hres
= (This
->path_start
> -1) ? S_OK
: S_FALSE
;
4520 case Uri_PROPERTY_PATH_AND_QUERY
:
4521 *pcchProperty
= This
->path_len
+This
->query_len
;
4522 hres
= (This
->path_start
> -1 || This
->query_start
> -1) ? S_OK
: S_FALSE
;
4524 case Uri_PROPERTY_QUERY
:
4525 *pcchProperty
= This
->query_len
;
4526 hres
= (This
->query_start
> -1) ? S_OK
: S_FALSE
;
4528 case Uri_PROPERTY_RAW_URI
:
4529 *pcchProperty
= SysStringLen(This
->raw_uri
);
4532 case Uri_PROPERTY_SCHEME_NAME
:
4533 *pcchProperty
= This
->scheme_len
;
4534 hres
= (This
->scheme_start
> -1) ? S_OK
: S_FALSE
;
4536 case Uri_PROPERTY_USER_INFO
:
4537 *pcchProperty
= This
->userinfo_len
;
4538 hres
= (This
->userinfo_start
> -1) ? S_OK
: S_FALSE
;
4540 case Uri_PROPERTY_USER_NAME
:
4541 *pcchProperty
= (This
->userinfo_split
> -1) ? This
->userinfo_split
: This
->userinfo_len
;
4542 if(This
->userinfo_split
== 0)
4545 hres
= (This
->userinfo_start
> -1) ? S_OK
: S_FALSE
;
4548 FIXME("(%p)->(%d %p %x)\n", This
, uriProp
, pcchProperty
, dwFlags
);
4555 static HRESULT WINAPI
Uri_GetPropertyDWORD(IUri
*iface
, Uri_PROPERTY uriProp
, DWORD
*pcchProperty
, DWORD dwFlags
)
4557 Uri
*This
= URI_THIS(iface
);
4560 TRACE("(%p)->(%d %p %x)\n", This
, uriProp
, pcchProperty
, dwFlags
);
4563 return E_INVALIDARG
;
4565 /* Microsoft's implementation for the ZONE property of a URI seems to be lacking...
4566 * From what I can tell, instead of checking which URLZONE the URI belongs to it
4567 * simply assigns URLZONE_INVALID and returns E_NOTIMPL. This also applies to the GetZone
4570 if(uriProp
== Uri_PROPERTY_ZONE
) {
4571 *pcchProperty
= URLZONE_INVALID
;
4575 if(uriProp
< Uri_PROPERTY_DWORD_START
) {
4577 return E_INVALIDARG
;
4581 case Uri_PROPERTY_HOST_TYPE
:
4582 *pcchProperty
= This
->host_type
;
4585 case Uri_PROPERTY_PORT
:
4586 if(!This
->has_port
) {
4590 *pcchProperty
= This
->port
;
4595 case Uri_PROPERTY_SCHEME
:
4596 *pcchProperty
= This
->scheme_type
;
4600 FIXME("(%p)->(%d %p %x)\n", This
, uriProp
, pcchProperty
, dwFlags
);
4607 static HRESULT WINAPI
Uri_HasProperty(IUri
*iface
, Uri_PROPERTY uriProp
, BOOL
*pfHasProperty
)
4609 Uri
*This
= URI_THIS(iface
);
4610 TRACE("(%p)->(%d %p)\n", This
, uriProp
, pfHasProperty
);
4613 return E_INVALIDARG
;
4616 case Uri_PROPERTY_ABSOLUTE_URI
:
4617 *pfHasProperty
= !(This
->display_modifiers
& URI_DISPLAY_NO_ABSOLUTE_URI
);
4619 case Uri_PROPERTY_AUTHORITY
:
4620 *pfHasProperty
= This
->authority_start
> -1;
4622 case Uri_PROPERTY_DISPLAY_URI
:
4623 *pfHasProperty
= TRUE
;
4625 case Uri_PROPERTY_DOMAIN
:
4626 *pfHasProperty
= This
->domain_offset
> -1;
4628 case Uri_PROPERTY_EXTENSION
:
4629 *pfHasProperty
= This
->extension_offset
> -1;
4631 case Uri_PROPERTY_FRAGMENT
:
4632 *pfHasProperty
= This
->fragment_start
> -1;
4634 case Uri_PROPERTY_HOST
:
4635 *pfHasProperty
= This
->host_start
> -1;
4637 case Uri_PROPERTY_PASSWORD
:
4638 *pfHasProperty
= This
->userinfo_split
> -1;
4640 case Uri_PROPERTY_PATH
:
4641 *pfHasProperty
= This
->path_start
> -1;
4643 case Uri_PROPERTY_PATH_AND_QUERY
:
4644 *pfHasProperty
= (This
->path_start
> -1 || This
->query_start
> -1);
4646 case Uri_PROPERTY_QUERY
:
4647 *pfHasProperty
= This
->query_start
> -1;
4649 case Uri_PROPERTY_RAW_URI
:
4650 *pfHasProperty
= TRUE
;
4652 case Uri_PROPERTY_SCHEME_NAME
:
4653 *pfHasProperty
= This
->scheme_start
> -1;
4655 case Uri_PROPERTY_USER_INFO
:
4656 *pfHasProperty
= This
->userinfo_start
> -1;
4658 case Uri_PROPERTY_USER_NAME
:
4659 if(This
->userinfo_split
== 0)
4660 *pfHasProperty
= FALSE
;
4662 *pfHasProperty
= This
->userinfo_start
> -1;
4664 case Uri_PROPERTY_HOST_TYPE
:
4665 *pfHasProperty
= TRUE
;
4667 case Uri_PROPERTY_PORT
:
4668 *pfHasProperty
= This
->has_port
;
4670 case Uri_PROPERTY_SCHEME
:
4671 *pfHasProperty
= TRUE
;
4673 case Uri_PROPERTY_ZONE
:
4674 *pfHasProperty
= FALSE
;
4677 FIXME("(%p)->(%d %p): Unsupported property type.\n", This
, uriProp
, pfHasProperty
);
4684 static HRESULT WINAPI
Uri_GetAbsoluteUri(IUri
*iface
, BSTR
*pstrAbsoluteUri
)
4686 TRACE("(%p)->(%p)\n", iface
, pstrAbsoluteUri
);
4687 return Uri_GetPropertyBSTR(iface
, Uri_PROPERTY_ABSOLUTE_URI
, pstrAbsoluteUri
, 0);
4690 static HRESULT WINAPI
Uri_GetAuthority(IUri
*iface
, BSTR
*pstrAuthority
)
4692 TRACE("(%p)->(%p)\n", iface
, pstrAuthority
);
4693 return Uri_GetPropertyBSTR(iface
, Uri_PROPERTY_AUTHORITY
, pstrAuthority
, 0);
4696 static HRESULT WINAPI
Uri_GetDisplayUri(IUri
*iface
, BSTR
*pstrDisplayUri
)
4698 TRACE("(%p)->(%p)\n", iface
, pstrDisplayUri
);
4699 return Uri_GetPropertyBSTR(iface
, Uri_PROPERTY_DISPLAY_URI
, pstrDisplayUri
, 0);
4702 static HRESULT WINAPI
Uri_GetDomain(IUri
*iface
, BSTR
*pstrDomain
)
4704 TRACE("(%p)->(%p)\n", iface
, pstrDomain
);
4705 return Uri_GetPropertyBSTR(iface
, Uri_PROPERTY_DOMAIN
, pstrDomain
, 0);
4708 static HRESULT WINAPI
Uri_GetExtension(IUri
*iface
, BSTR
*pstrExtension
)
4710 TRACE("(%p)->(%p)\n", iface
, pstrExtension
);
4711 return Uri_GetPropertyBSTR(iface
, Uri_PROPERTY_EXTENSION
, pstrExtension
, 0);
4714 static HRESULT WINAPI
Uri_GetFragment(IUri
*iface
, BSTR
*pstrFragment
)
4716 TRACE("(%p)->(%p)\n", iface
, pstrFragment
);
4717 return Uri_GetPropertyBSTR(iface
, Uri_PROPERTY_FRAGMENT
, pstrFragment
, 0);
4720 static HRESULT WINAPI
Uri_GetHost(IUri
*iface
, BSTR
*pstrHost
)
4722 TRACE("(%p)->(%p)\n", iface
, pstrHost
);
4723 return Uri_GetPropertyBSTR(iface
, Uri_PROPERTY_HOST
, pstrHost
, 0);
4726 static HRESULT WINAPI
Uri_GetPassword(IUri
*iface
, BSTR
*pstrPassword
)
4728 TRACE("(%p)->(%p)\n", iface
, pstrPassword
);
4729 return Uri_GetPropertyBSTR(iface
, Uri_PROPERTY_PASSWORD
, pstrPassword
, 0);
4732 static HRESULT WINAPI
Uri_GetPath(IUri
*iface
, BSTR
*pstrPath
)
4734 TRACE("(%p)->(%p)\n", iface
, pstrPath
);
4735 return Uri_GetPropertyBSTR(iface
, Uri_PROPERTY_PATH
, pstrPath
, 0);
4738 static HRESULT WINAPI
Uri_GetPathAndQuery(IUri
*iface
, BSTR
*pstrPathAndQuery
)
4740 TRACE("(%p)->(%p)\n", iface
, pstrPathAndQuery
);
4741 return Uri_GetPropertyBSTR(iface
, Uri_PROPERTY_PATH_AND_QUERY
, pstrPathAndQuery
, 0);
4744 static HRESULT WINAPI
Uri_GetQuery(IUri
*iface
, BSTR
*pstrQuery
)
4746 TRACE("(%p)->(%p)\n", iface
, pstrQuery
);
4747 return Uri_GetPropertyBSTR(iface
, Uri_PROPERTY_QUERY
, pstrQuery
, 0);
4750 static HRESULT WINAPI
Uri_GetRawUri(IUri
*iface
, BSTR
*pstrRawUri
)
4752 TRACE("(%p)->(%p)\n", iface
, pstrRawUri
);
4753 return Uri_GetPropertyBSTR(iface
, Uri_PROPERTY_RAW_URI
, pstrRawUri
, 0);
4756 static HRESULT WINAPI
Uri_GetSchemeName(IUri
*iface
, BSTR
*pstrSchemeName
)
4758 TRACE("(%p)->(%p)\n", iface
, pstrSchemeName
);
4759 return Uri_GetPropertyBSTR(iface
, Uri_PROPERTY_SCHEME_NAME
, pstrSchemeName
, 0);
4762 static HRESULT WINAPI
Uri_GetUserInfo(IUri
*iface
, BSTR
*pstrUserInfo
)
4764 TRACE("(%p)->(%p)\n", iface
, pstrUserInfo
);
4765 return Uri_GetPropertyBSTR(iface
, Uri_PROPERTY_USER_INFO
, pstrUserInfo
, 0);
4768 static HRESULT WINAPI
Uri_GetUserName(IUri
*iface
, BSTR
*pstrUserName
)
4770 TRACE("(%p)->(%p)\n", iface
, pstrUserName
);
4771 return Uri_GetPropertyBSTR(iface
, Uri_PROPERTY_USER_NAME
, pstrUserName
, 0);
4774 static HRESULT WINAPI
Uri_GetHostType(IUri
*iface
, DWORD
*pdwHostType
)
4776 TRACE("(%p)->(%p)\n", iface
, pdwHostType
);
4777 return Uri_GetPropertyDWORD(iface
, Uri_PROPERTY_HOST_TYPE
, pdwHostType
, 0);
4780 static HRESULT WINAPI
Uri_GetPort(IUri
*iface
, DWORD
*pdwPort
)
4782 TRACE("(%p)->(%p)\n", iface
, pdwPort
);
4783 return Uri_GetPropertyDWORD(iface
, Uri_PROPERTY_PORT
, pdwPort
, 0);
4786 static HRESULT WINAPI
Uri_GetScheme(IUri
*iface
, DWORD
*pdwScheme
)
4788 Uri
*This
= URI_THIS(iface
);
4789 TRACE("(%p)->(%p)\n", This
, pdwScheme
);
4790 return Uri_GetPropertyDWORD(iface
, Uri_PROPERTY_SCHEME
, pdwScheme
, 0);
4793 static HRESULT WINAPI
Uri_GetZone(IUri
*iface
, DWORD
*pdwZone
)
4795 TRACE("(%p)->(%p)\n", iface
, pdwZone
);
4796 return Uri_GetPropertyDWORD(iface
, Uri_PROPERTY_ZONE
,pdwZone
, 0);
4799 static HRESULT WINAPI
Uri_GetProperties(IUri
*iface
, DWORD
*pdwProperties
)
4801 Uri
*This
= URI_THIS(iface
);
4802 TRACE("(%p)->(%p)\n", This
, pdwProperties
);
4805 return E_INVALIDARG
;
4807 /* All URIs have these. */
4808 *pdwProperties
= Uri_HAS_DISPLAY_URI
|Uri_HAS_RAW_URI
|Uri_HAS_SCHEME
|Uri_HAS_HOST_TYPE
;
4810 if(!(This
->display_modifiers
& URI_DISPLAY_NO_ABSOLUTE_URI
))
4811 *pdwProperties
|= Uri_HAS_ABSOLUTE_URI
;
4813 if(This
->scheme_start
> -1)
4814 *pdwProperties
|= Uri_HAS_SCHEME_NAME
;
4816 if(This
->authority_start
> -1) {
4817 *pdwProperties
|= Uri_HAS_AUTHORITY
;
4818 if(This
->userinfo_start
> -1) {
4819 *pdwProperties
|= Uri_HAS_USER_INFO
;
4820 if(This
->userinfo_split
!= 0)
4821 *pdwProperties
|= Uri_HAS_USER_NAME
;
4823 if(This
->userinfo_split
> -1)
4824 *pdwProperties
|= Uri_HAS_PASSWORD
;
4825 if(This
->host_start
> -1)
4826 *pdwProperties
|= Uri_HAS_HOST
;
4827 if(This
->domain_offset
> -1)
4828 *pdwProperties
|= Uri_HAS_DOMAIN
;
4832 *pdwProperties
|= Uri_HAS_PORT
;
4833 if(This
->path_start
> -1)
4834 *pdwProperties
|= Uri_HAS_PATH
|Uri_HAS_PATH_AND_QUERY
;
4835 if(This
->query_start
> -1)
4836 *pdwProperties
|= Uri_HAS_QUERY
|Uri_HAS_PATH_AND_QUERY
;
4838 if(This
->extension_offset
> -1)
4839 *pdwProperties
|= Uri_HAS_EXTENSION
;
4841 if(This
->fragment_start
> -1)
4842 *pdwProperties
|= Uri_HAS_FRAGMENT
;
4847 static HRESULT WINAPI
Uri_IsEqual(IUri
*iface
, IUri
*pUri
, BOOL
*pfEqual
)
4849 Uri
*This
= URI_THIS(iface
);
4852 TRACE("(%p)->(%p %p)\n", This
, pUri
, pfEqual
);
4860 /* For some reason Windows returns S_OK here... */
4864 /* Try to convert it to a Uri (allows for a more simple comparison). */
4865 if((other
= get_uri_obj(pUri
)))
4866 *pfEqual
= are_equal_simple(This
, other
);
4868 /* Do it the hard way. */
4869 FIXME("(%p)->(%p %p) No support for unknown IUri's yet.\n", iface
, pUri
, pfEqual
);
4878 static const IUriVtbl UriVtbl
= {
4882 Uri_GetPropertyBSTR
,
4883 Uri_GetPropertyLength
,
4884 Uri_GetPropertyDWORD
,
4895 Uri_GetPathAndQuery
,
4909 static Uri
* create_uri_obj(void) {
4910 Uri
*ret
= heap_alloc_zero(sizeof(Uri
));
4912 ret
->lpIUriVtbl
= &UriVtbl
;
4919 /***********************************************************************
4920 * CreateUri (urlmon.@)
4922 * Creates a new IUri object using the URI represented by pwzURI. This function
4923 * parses and validates the components of pwzURI and then canonicalizes the
4924 * parsed components.
4927 * pwzURI [I] The URI to parse, validate, and canonicalize.
4928 * dwFlags [I] Flags which can affect how the parsing/canonicalization is performed.
4929 * dwReserved [I] Reserved (not used).
4930 * ppURI [O] The resulting IUri after parsing/canonicalization occurs.
4933 * Success: Returns S_OK. ppURI contains the pointer to the newly allocated IUri.
4934 * Failure: E_INVALIDARG if there's invalid flag combinations in dwFlags, or an
4935 * invalid parameters, or pwzURI doesn't represnt a valid URI.
4936 * E_OUTOFMEMORY if any memory allocation fails.
4940 * Uri_CREATE_CANONICALIZE, Uri_CREATE_DECODE_EXTRA_INFO, Uri_CREATE_CRACK_UNKNOWN_SCHEMES,
4941 * Uri_CREATE_PRE_PROCESS_HTML_URI, Uri_CREATE_NO_IE_SETTINGS.
4943 HRESULT WINAPI
CreateUri(LPCWSTR pwzURI
, DWORD dwFlags
, DWORD_PTR dwReserved
, IUri
**ppURI
)
4945 const DWORD supported_flags
= Uri_CREATE_ALLOW_RELATIVE
|Uri_CREATE_ALLOW_IMPLICIT_WILDCARD_SCHEME
|
4946 Uri_CREATE_ALLOW_IMPLICIT_FILE_SCHEME
|Uri_CREATE_NO_CANONICALIZE
|Uri_CREATE_CANONICALIZE
|
4947 Uri_CREATE_DECODE_EXTRA_INFO
|Uri_CREATE_NO_DECODE_EXTRA_INFO
|Uri_CREATE_CRACK_UNKNOWN_SCHEMES
|
4948 Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES
|Uri_CREATE_PRE_PROCESS_HTML_URI
|Uri_CREATE_NO_PRE_PROCESS_HTML_URI
|
4949 Uri_CREATE_NO_IE_SETTINGS
|Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS
|Uri_CREATE_FILE_USE_DOS_PATH
;
4954 TRACE("(%s %x %x %p)\n", debugstr_w(pwzURI
), dwFlags
, (DWORD
)dwReserved
, ppURI
);
4957 return E_INVALIDARG
;
4959 if(!pwzURI
|| !*pwzURI
) {
4961 return E_INVALIDARG
;
4964 /* Check for invalid flags. */
4965 if(has_invalid_flag_combination(dwFlags
)) {
4967 return E_INVALIDARG
;
4970 /* Currently unsupported. */
4971 if(dwFlags
& ~supported_flags
)
4972 FIXME("Ignoring unsupported flag(s) %x\n", dwFlags
& ~supported_flags
);
4974 ret
= create_uri_obj();
4977 return E_OUTOFMEMORY
;
4980 /* Explicitly set the default flags if it doesn't cause a flag conflict. */
4981 apply_default_flags(&dwFlags
);
4983 /* Pre process the URI, unless told otherwise. */
4984 if(!(dwFlags
& Uri_CREATE_NO_PRE_PROCESS_HTML_URI
))
4985 ret
->raw_uri
= pre_process_uri(pwzURI
);
4987 ret
->raw_uri
= SysAllocString(pwzURI
);
4991 return E_OUTOFMEMORY
;
4994 memset(&data
, 0, sizeof(parse_data
));
4995 data
.uri
= ret
->raw_uri
;
4997 /* Validate and parse the URI into it's components. */
4998 if(!parse_uri(&data
, dwFlags
)) {
4999 /* Encountered an unsupported or invalid URI */
5000 IUri_Release(URI(ret
));
5002 return E_INVALIDARG
;
5005 /* Canonicalize the URI. */
5006 hr
= canonicalize_uri(&data
, ret
, dwFlags
);
5008 IUri_Release(URI(ret
));
5013 ret
->create_flags
= dwFlags
;
5019 /***********************************************************************
5020 * CreateUriWithFragment (urlmon.@)
5022 * Creates a new IUri object. This is almost the same as CreateUri, expect that
5023 * it allows you to explicitly specify a fragment (pwzFragment) for pwzURI.
5026 * pwzURI [I] The URI to parse and perform canonicalization on.
5027 * pwzFragment [I] The explict fragment string which should be added to pwzURI.
5028 * dwFlags [I] The flags which will be passed to CreateUri.
5029 * dwReserved [I] Reserved (not used).
5030 * ppURI [O] The resulting IUri after parsing/canonicalization.
5033 * Success: S_OK. ppURI contains the pointer to the newly allocated IUri.
5034 * Failure: E_INVALIDARG if pwzURI already contains a fragment and pwzFragment
5035 * isn't NULL. Will also return E_INVALIDARG for the same reasons as
5036 * CreateUri will. E_OUTOFMEMORY if any allocations fail.
5038 HRESULT WINAPI
CreateUriWithFragment(LPCWSTR pwzURI
, LPCWSTR pwzFragment
, DWORD dwFlags
,
5039 DWORD_PTR dwReserved
, IUri
**ppURI
)
5042 TRACE("(%s %s %x %x %p)\n", debugstr_w(pwzURI
), debugstr_w(pwzFragment
), dwFlags
, (DWORD
)dwReserved
, ppURI
);
5045 return E_INVALIDARG
;
5049 return E_INVALIDARG
;
5052 /* Check if a fragment should be appended to the URI string. */
5055 DWORD uri_len
, frag_len
;
5058 /* Check if the original URI already has a fragment component. */
5059 if(StrChrW(pwzURI
, '#')) {
5061 return E_INVALIDARG
;
5064 uri_len
= lstrlenW(pwzURI
);
5065 frag_len
= lstrlenW(pwzFragment
);
5067 /* If the fragment doesn't start with a '#', one will be added. */
5068 add_pound
= *pwzFragment
!= '#';
5071 uriW
= heap_alloc((uri_len
+frag_len
+2)*sizeof(WCHAR
));
5073 uriW
= heap_alloc((uri_len
+frag_len
+1)*sizeof(WCHAR
));
5076 return E_OUTOFMEMORY
;
5078 memcpy(uriW
, pwzURI
, uri_len
*sizeof(WCHAR
));
5080 uriW
[uri_len
++] = '#';
5081 memcpy(uriW
+uri_len
, pwzFragment
, (frag_len
+1)*sizeof(WCHAR
));
5083 hres
= CreateUri(uriW
, dwFlags
, 0, ppURI
);
5087 /* A fragment string wasn't specified, so just forward the call. */
5088 hres
= CreateUri(pwzURI
, dwFlags
, 0, ppURI
);
5093 static HRESULT
build_uri(const UriBuilder
*builder
, IUri
**uri
, DWORD create_flags
,
5094 DWORD use_orig_flags
, DWORD encoding_mask
)
5103 if(encoding_mask
&& (!builder
->uri
|| builder
->modified_props
)) {
5108 /* Decide what flags should be used when creating the Uri. */
5109 if((use_orig_flags
& UriBuilder_USE_ORIGINAL_FLAGS
) && builder
->uri
)
5110 create_flags
= builder
->uri
->create_flags
;
5112 if(has_invalid_flag_combination(create_flags
)) {
5114 return E_INVALIDARG
;
5117 /* Set the default flags if they don't cause a conflict. */
5118 apply_default_flags(&create_flags
);
5121 /* Return the base IUri if no changes have been made and the create_flags match. */
5122 if(builder
->uri
&& !builder
->modified_props
&& builder
->uri
->create_flags
== create_flags
) {
5123 *uri
= URI(builder
->uri
);
5128 hr
= validate_components(builder
, &data
, create_flags
);
5134 ret
= create_uri_obj();
5137 return E_OUTOFMEMORY
;
5140 hr
= generate_uri(builder
, &data
, ret
, create_flags
);
5142 IUri_Release(URI(ret
));
5151 #define URIBUILDER_THIS(iface) DEFINE_THIS(UriBuilder, IUriBuilder, iface)
5153 static HRESULT WINAPI
UriBuilder_QueryInterface(IUriBuilder
*iface
, REFIID riid
, void **ppv
)
5155 UriBuilder
*This
= URIBUILDER_THIS(iface
);
5157 if(IsEqualGUID(&IID_IUnknown
, riid
)) {
5158 TRACE("(%p)->(IID_IUnknown %p)\n", This
, ppv
);
5159 *ppv
= URIBUILDER(This
);
5160 }else if(IsEqualGUID(&IID_IUriBuilder
, riid
)) {
5161 TRACE("(%p)->(IID_IUri %p)\n", This
, ppv
);
5162 *ppv
= URIBUILDER(This
);
5164 TRACE("(%p)->(%s %p)\n", This
, debugstr_guid(riid
), ppv
);
5166 return E_NOINTERFACE
;
5169 IUnknown_AddRef((IUnknown
*)*ppv
);
5173 static ULONG WINAPI
UriBuilder_AddRef(IUriBuilder
*iface
)
5175 UriBuilder
*This
= URIBUILDER_THIS(iface
);
5176 LONG ref
= InterlockedIncrement(&This
->ref
);
5178 TRACE("(%p) ref=%d\n", This
, ref
);
5183 static ULONG WINAPI
UriBuilder_Release(IUriBuilder
*iface
)
5185 UriBuilder
*This
= URIBUILDER_THIS(iface
);
5186 LONG ref
= InterlockedDecrement(&This
->ref
);
5188 TRACE("(%p) ref=%d\n", This
, ref
);
5191 if(This
->uri
) IUri_Release(URI(This
->uri
));
5192 heap_free(This
->fragment
);
5193 heap_free(This
->host
);
5194 heap_free(This
->password
);
5195 heap_free(This
->path
);
5196 heap_free(This
->query
);
5197 heap_free(This
->scheme
);
5198 heap_free(This
->username
);
5205 static HRESULT WINAPI
UriBuilder_CreateUriSimple(IUriBuilder
*iface
,
5206 DWORD dwAllowEncodingPropertyMask
,
5207 DWORD_PTR dwReserved
,
5210 UriBuilder
*This
= URIBUILDER_THIS(iface
);
5212 TRACE("(%p)->(%d %d %p)\n", This
, dwAllowEncodingPropertyMask
, (DWORD
)dwReserved
, ppIUri
);
5214 hr
= build_uri(This
, ppIUri
, 0, UriBuilder_USE_ORIGINAL_FLAGS
, dwAllowEncodingPropertyMask
);
5216 FIXME("(%p)->(%d %d %p)\n", This
, dwAllowEncodingPropertyMask
, (DWORD
)dwReserved
, ppIUri
);
5220 static HRESULT WINAPI
UriBuilder_CreateUri(IUriBuilder
*iface
,
5221 DWORD dwCreateFlags
,
5222 DWORD dwAllowEncodingPropertyMask
,
5223 DWORD_PTR dwReserved
,
5226 UriBuilder
*This
= URIBUILDER_THIS(iface
);
5228 TRACE("(%p)->(0x%08x %d %d %p)\n", This
, dwCreateFlags
, dwAllowEncodingPropertyMask
, (DWORD
)dwReserved
, ppIUri
);
5230 if(dwCreateFlags
== -1)
5231 hr
= build_uri(This
, ppIUri
, 0, UriBuilder_USE_ORIGINAL_FLAGS
, dwAllowEncodingPropertyMask
);
5233 hr
= build_uri(This
, ppIUri
, dwCreateFlags
, 0, dwAllowEncodingPropertyMask
);
5236 FIXME("(%p)->(0x%08x %d %d %p)\n", This
, dwCreateFlags
, dwAllowEncodingPropertyMask
, (DWORD
)dwReserved
, ppIUri
);
5240 static HRESULT WINAPI
UriBuilder_CreateUriWithFlags(IUriBuilder
*iface
,
5241 DWORD dwCreateFlags
,
5242 DWORD dwUriBuilderFlags
,
5243 DWORD dwAllowEncodingPropertyMask
,
5244 DWORD_PTR dwReserved
,
5247 UriBuilder
*This
= URIBUILDER_THIS(iface
);
5249 TRACE("(%p)->(0x%08x 0x%08x %d %d %p)\n", This
, dwCreateFlags
, dwUriBuilderFlags
,
5250 dwAllowEncodingPropertyMask
, (DWORD
)dwReserved
, ppIUri
);
5252 hr
= build_uri(This
, ppIUri
, dwCreateFlags
, dwUriBuilderFlags
, dwAllowEncodingPropertyMask
);
5254 FIXME("(%p)->(0x%08x 0x%08x %d %d %p)\n", This
, dwCreateFlags
, dwUriBuilderFlags
,
5255 dwAllowEncodingPropertyMask
, (DWORD
)dwReserved
, ppIUri
);
5259 static HRESULT WINAPI
UriBuilder_GetIUri(IUriBuilder
*iface
, IUri
**ppIUri
)
5261 UriBuilder
*This
= URIBUILDER_THIS(iface
);
5262 TRACE("(%p)->(%p)\n", This
, ppIUri
);
5268 IUri
*uri
= URI(This
->uri
);
5277 static HRESULT WINAPI
UriBuilder_SetIUri(IUriBuilder
*iface
, IUri
*pIUri
)
5279 UriBuilder
*This
= URIBUILDER_THIS(iface
);
5280 TRACE("(%p)->(%p)\n", This
, pIUri
);
5285 if((uri
= get_uri_obj(pIUri
))) {
5286 /* Only reset the builder if it's Uri isn't the same as
5287 * the Uri passed to the function.
5289 if(This
->uri
!= uri
) {
5290 reset_builder(This
);
5294 This
->port
= uri
->port
;
5299 FIXME("(%p)->(%p) Unknown IUri types not supported yet.\n", This
, pIUri
);
5302 } else if(This
->uri
)
5303 /* Only reset the builder if it's Uri isn't NULL. */
5304 reset_builder(This
);
5309 static HRESULT WINAPI
UriBuilder_GetFragment(IUriBuilder
*iface
, DWORD
*pcchFragment
, LPCWSTR
*ppwzFragment
)
5311 UriBuilder
*This
= URIBUILDER_THIS(iface
);
5312 TRACE("(%p)->(%p %p)\n", This
, pcchFragment
, ppwzFragment
);
5314 if(!This
->uri
|| This
->uri
->fragment_start
== -1 || This
->modified_props
& Uri_HAS_FRAGMENT
)
5315 return get_builder_component(&This
->fragment
, &This
->fragment_len
, NULL
, 0, ppwzFragment
, pcchFragment
);
5317 return get_builder_component(&This
->fragment
, &This
->fragment_len
, This
->uri
->canon_uri
+This
->uri
->fragment_start
,
5318 This
->uri
->fragment_len
, ppwzFragment
, pcchFragment
);
5321 static HRESULT WINAPI
UriBuilder_GetHost(IUriBuilder
*iface
, DWORD
*pcchHost
, LPCWSTR
*ppwzHost
)
5323 UriBuilder
*This
= URIBUILDER_THIS(iface
);
5324 TRACE("(%p)->(%p %p)\n", This
, pcchHost
, ppwzHost
);
5326 if(!This
->uri
|| This
->uri
->host_start
== -1 || This
->modified_props
& Uri_HAS_HOST
)
5327 return get_builder_component(&This
->host
, &This
->host_len
, NULL
, 0, ppwzHost
, pcchHost
);
5329 if(This
->uri
->host_type
== Uri_HOST_IPV6
)
5330 /* Don't include the '[' and ']' around the address. */
5331 return get_builder_component(&This
->host
, &This
->host_len
, This
->uri
->canon_uri
+This
->uri
->host_start
+1,
5332 This
->uri
->host_len
-2, ppwzHost
, pcchHost
);
5334 return get_builder_component(&This
->host
, &This
->host_len
, This
->uri
->canon_uri
+This
->uri
->host_start
,
5335 This
->uri
->host_len
, ppwzHost
, pcchHost
);
5339 static HRESULT WINAPI
UriBuilder_GetPassword(IUriBuilder
*iface
, DWORD
*pcchPassword
, LPCWSTR
*ppwzPassword
)
5341 UriBuilder
*This
= URIBUILDER_THIS(iface
);
5342 TRACE("(%p)->(%p %p)\n", This
, pcchPassword
, ppwzPassword
);
5344 if(!This
->uri
|| This
->uri
->userinfo_split
== -1 || This
->modified_props
& Uri_HAS_PASSWORD
)
5345 return get_builder_component(&This
->password
, &This
->password_len
, NULL
, 0, ppwzPassword
, pcchPassword
);
5347 const WCHAR
*start
= This
->uri
->canon_uri
+This
->uri
->userinfo_start
+This
->uri
->userinfo_split
+1;
5348 DWORD len
= This
->uri
->userinfo_len
-This
->uri
->userinfo_split
-1;
5349 return get_builder_component(&This
->password
, &This
->password_len
, start
, len
, ppwzPassword
, pcchPassword
);
5353 static HRESULT WINAPI
UriBuilder_GetPath(IUriBuilder
*iface
, DWORD
*pcchPath
, LPCWSTR
*ppwzPath
)
5355 UriBuilder
*This
= URIBUILDER_THIS(iface
);
5356 TRACE("(%p)->(%p %p)\n", This
, pcchPath
, ppwzPath
);
5358 if(!This
->uri
|| This
->uri
->path_start
== -1 || This
->modified_props
& Uri_HAS_PATH
)
5359 return get_builder_component(&This
->path
, &This
->path_len
, NULL
, 0, ppwzPath
, pcchPath
);
5361 return get_builder_component(&This
->path
, &This
->path_len
, This
->uri
->canon_uri
+This
->uri
->path_start
,
5362 This
->uri
->path_len
, ppwzPath
, pcchPath
);
5365 static HRESULT WINAPI
UriBuilder_GetPort(IUriBuilder
*iface
, BOOL
*pfHasPort
, DWORD
*pdwPort
)
5367 UriBuilder
*This
= URIBUILDER_THIS(iface
);
5368 TRACE("(%p)->(%p %p)\n", This
, pfHasPort
, pdwPort
);
5381 *pfHasPort
= This
->has_port
;
5382 *pdwPort
= This
->port
;
5386 static HRESULT WINAPI
UriBuilder_GetQuery(IUriBuilder
*iface
, DWORD
*pcchQuery
, LPCWSTR
*ppwzQuery
)
5388 UriBuilder
*This
= URIBUILDER_THIS(iface
);
5389 TRACE("(%p)->(%p %p)\n", This
, pcchQuery
, ppwzQuery
);
5391 if(!This
->uri
|| This
->uri
->query_start
== -1 || This
->modified_props
& Uri_HAS_QUERY
)
5392 return get_builder_component(&This
->query
, &This
->query_len
, NULL
, 0, ppwzQuery
, pcchQuery
);
5394 return get_builder_component(&This
->query
, &This
->query_len
, This
->uri
->canon_uri
+This
->uri
->query_start
,
5395 This
->uri
->query_len
, ppwzQuery
, pcchQuery
);
5398 static HRESULT WINAPI
UriBuilder_GetSchemeName(IUriBuilder
*iface
, DWORD
*pcchSchemeName
, LPCWSTR
*ppwzSchemeName
)
5400 UriBuilder
*This
= URIBUILDER_THIS(iface
);
5401 TRACE("(%p)->(%p %p)\n", This
, pcchSchemeName
, ppwzSchemeName
);
5403 if(!This
->uri
|| This
->uri
->scheme_start
== -1 || This
->modified_props
& Uri_HAS_SCHEME_NAME
)
5404 return get_builder_component(&This
->scheme
, &This
->scheme_len
, NULL
, 0, ppwzSchemeName
, pcchSchemeName
);
5406 return get_builder_component(&This
->scheme
, &This
->scheme_len
, This
->uri
->canon_uri
+This
->uri
->scheme_start
,
5407 This
->uri
->scheme_len
, ppwzSchemeName
, pcchSchemeName
);
5410 static HRESULT WINAPI
UriBuilder_GetUserName(IUriBuilder
*iface
, DWORD
*pcchUserName
, LPCWSTR
*ppwzUserName
)
5412 UriBuilder
*This
= URIBUILDER_THIS(iface
);
5413 TRACE("(%p)->(%p %p)\n", This
, pcchUserName
, ppwzUserName
);
5415 if(!This
->uri
|| This
->uri
->userinfo_start
== -1 || This
->uri
->userinfo_split
== 0 ||
5416 This
->modified_props
& Uri_HAS_USER_NAME
)
5417 return get_builder_component(&This
->username
, &This
->username_len
, NULL
, 0, ppwzUserName
, pcchUserName
);
5419 const WCHAR
*start
= This
->uri
->canon_uri
+This
->uri
->userinfo_start
;
5421 /* Check if there's a password in the userinfo section. */
5422 if(This
->uri
->userinfo_split
> -1)
5423 /* Don't include the password. */
5424 return get_builder_component(&This
->username
, &This
->username_len
, start
,
5425 This
->uri
->userinfo_split
, ppwzUserName
, pcchUserName
);
5427 return get_builder_component(&This
->username
, &This
->username_len
, start
,
5428 This
->uri
->userinfo_len
, ppwzUserName
, pcchUserName
);
5432 static HRESULT WINAPI
UriBuilder_SetFragment(IUriBuilder
*iface
, LPCWSTR pwzNewValue
)
5434 UriBuilder
*This
= URIBUILDER_THIS(iface
);
5435 TRACE("(%p)->(%s)\n", This
, debugstr_w(pwzNewValue
));
5436 return set_builder_component(&This
->fragment
, &This
->fragment_len
, pwzNewValue
, '#',
5437 &This
->modified_props
, Uri_HAS_FRAGMENT
);
5440 static HRESULT WINAPI
UriBuilder_SetHost(IUriBuilder
*iface
, LPCWSTR pwzNewValue
)
5442 UriBuilder
*This
= URIBUILDER_THIS(iface
);
5443 TRACE("(%p)->(%s)\n", This
, debugstr_w(pwzNewValue
));
5445 /* Host name can't be set to NULL. */
5447 return E_INVALIDARG
;
5449 return set_builder_component(&This
->host
, &This
->host_len
, pwzNewValue
, 0,
5450 &This
->modified_props
, Uri_HAS_HOST
);
5453 static HRESULT WINAPI
UriBuilder_SetPassword(IUriBuilder
*iface
, LPCWSTR pwzNewValue
)
5455 UriBuilder
*This
= URIBUILDER_THIS(iface
);
5456 TRACE("(%p)->(%s)\n", This
, debugstr_w(pwzNewValue
));
5457 return set_builder_component(&This
->password
, &This
->password_len
, pwzNewValue
, 0,
5458 &This
->modified_props
, Uri_HAS_PASSWORD
);
5461 static HRESULT WINAPI
UriBuilder_SetPath(IUriBuilder
*iface
, LPCWSTR pwzNewValue
)
5463 UriBuilder
*This
= URIBUILDER_THIS(iface
);
5464 TRACE("(%p)->(%s)\n", This
, debugstr_w(pwzNewValue
));
5465 return set_builder_component(&This
->path
, &This
->path_len
, pwzNewValue
, 0,
5466 &This
->modified_props
, Uri_HAS_PATH
);
5469 static HRESULT WINAPI
UriBuilder_SetPort(IUriBuilder
*iface
, BOOL fHasPort
, DWORD dwNewValue
)
5471 UriBuilder
*This
= URIBUILDER_THIS(iface
);
5472 TRACE("(%p)->(%d %d)\n", This
, fHasPort
, dwNewValue
);
5474 This
->has_port
= fHasPort
;
5475 This
->port
= dwNewValue
;
5476 This
->modified_props
|= Uri_HAS_PORT
;
5480 static HRESULT WINAPI
UriBuilder_SetQuery(IUriBuilder
*iface
, LPCWSTR pwzNewValue
)
5482 UriBuilder
*This
= URIBUILDER_THIS(iface
);
5483 TRACE("(%p)->(%s)\n", This
, debugstr_w(pwzNewValue
));
5484 return set_builder_component(&This
->query
, &This
->query_len
, pwzNewValue
, '?',
5485 &This
->modified_props
, Uri_HAS_QUERY
);
5488 static HRESULT WINAPI
UriBuilder_SetSchemeName(IUriBuilder
*iface
, LPCWSTR pwzNewValue
)
5490 UriBuilder
*This
= URIBUILDER_THIS(iface
);
5491 TRACE("(%p)->(%s)\n", This
, debugstr_w(pwzNewValue
));
5493 /* Only set the scheme name if it's not NULL or empty. */
5494 if(!pwzNewValue
|| !*pwzNewValue
)
5495 return E_INVALIDARG
;
5497 return set_builder_component(&This
->scheme
, &This
->scheme_len
, pwzNewValue
, 0,
5498 &This
->modified_props
, Uri_HAS_SCHEME_NAME
);
5501 static HRESULT WINAPI
UriBuilder_SetUserName(IUriBuilder
*iface
, LPCWSTR pwzNewValue
)
5503 UriBuilder
*This
= URIBUILDER_THIS(iface
);
5504 TRACE("(%p)->(%s)\n", This
, debugstr_w(pwzNewValue
));
5505 return set_builder_component(&This
->username
, &This
->username_len
, pwzNewValue
, 0,
5506 &This
->modified_props
, Uri_HAS_USER_NAME
);
5509 static HRESULT WINAPI
UriBuilder_RemoveProperties(IUriBuilder
*iface
, DWORD dwPropertyMask
)
5511 const DWORD accepted_flags
= Uri_HAS_AUTHORITY
|Uri_HAS_DOMAIN
|Uri_HAS_EXTENSION
|Uri_HAS_FRAGMENT
|Uri_HAS_HOST
|
5512 Uri_HAS_PASSWORD
|Uri_HAS_PATH
|Uri_HAS_PATH_AND_QUERY
|Uri_HAS_QUERY
|
5513 Uri_HAS_USER_INFO
|Uri_HAS_USER_NAME
;
5515 UriBuilder
*This
= URIBUILDER_THIS(iface
);
5516 TRACE("(%p)->(0x%08x)\n", This
, dwPropertyMask
);
5518 if(dwPropertyMask
& ~accepted_flags
)
5519 return E_INVALIDARG
;
5521 if(dwPropertyMask
& Uri_HAS_FRAGMENT
)
5522 UriBuilder_SetFragment(iface
, NULL
);
5524 /* Even though you can't set the host name to NULL or an
5525 * empty string, you can still remove it... for some reason.
5527 if(dwPropertyMask
& Uri_HAS_HOST
)
5528 set_builder_component(&This
->host
, &This
->host_len
, NULL
, 0,
5529 &This
->modified_props
, Uri_HAS_HOST
);
5531 if(dwPropertyMask
& Uri_HAS_PASSWORD
)
5532 UriBuilder_SetPassword(iface
, NULL
);
5534 if(dwPropertyMask
& Uri_HAS_PATH
)
5535 UriBuilder_SetPath(iface
, NULL
);
5537 if(dwPropertyMask
& Uri_HAS_PORT
)
5538 UriBuilder_SetPort(iface
, FALSE
, 0);
5540 if(dwPropertyMask
& Uri_HAS_QUERY
)
5541 UriBuilder_SetQuery(iface
, NULL
);
5543 if(dwPropertyMask
& Uri_HAS_USER_NAME
)
5544 UriBuilder_SetUserName(iface
, NULL
);
5549 static HRESULT WINAPI
UriBuilder_HasBeenModified(IUriBuilder
*iface
, BOOL
*pfModified
)
5551 UriBuilder
*This
= URIBUILDER_THIS(iface
);
5552 TRACE("(%p)->(%p)\n", This
, pfModified
);
5557 *pfModified
= This
->modified_props
> 0;
5561 #undef URIBUILDER_THIS
5563 static const IUriBuilderVtbl UriBuilderVtbl
= {
5564 UriBuilder_QueryInterface
,
5567 UriBuilder_CreateUriSimple
,
5568 UriBuilder_CreateUri
,
5569 UriBuilder_CreateUriWithFlags
,
5572 UriBuilder_GetFragment
,
5574 UriBuilder_GetPassword
,
5577 UriBuilder_GetQuery
,
5578 UriBuilder_GetSchemeName
,
5579 UriBuilder_GetUserName
,
5580 UriBuilder_SetFragment
,
5582 UriBuilder_SetPassword
,
5585 UriBuilder_SetQuery
,
5586 UriBuilder_SetSchemeName
,
5587 UriBuilder_SetUserName
,
5588 UriBuilder_RemoveProperties
,
5589 UriBuilder_HasBeenModified
,
5592 /***********************************************************************
5593 * CreateIUriBuilder (urlmon.@)
5595 HRESULT WINAPI
CreateIUriBuilder(IUri
*pIUri
, DWORD dwFlags
, DWORD_PTR dwReserved
, IUriBuilder
**ppIUriBuilder
)
5599 TRACE("(%p %x %x %p)\n", pIUri
, dwFlags
, (DWORD
)dwReserved
, ppIUriBuilder
);
5604 ret
= heap_alloc_zero(sizeof(UriBuilder
));
5606 return E_OUTOFMEMORY
;
5608 ret
->lpIUriBuilderVtbl
= &UriBuilderVtbl
;
5614 if((uri
= get_uri_obj(pIUri
))) {
5619 /* Windows doesn't set 'has_port' to TRUE in this case. */
5620 ret
->port
= uri
->port
;
5624 *ppIUriBuilder
= NULL
;
5625 FIXME("(%p %x %x %p): Unknown IUri types not supported yet.\n", pIUri
, dwFlags
,
5626 (DWORD
)dwReserved
, ppIUriBuilder
);
5631 *ppIUriBuilder
= URIBUILDER(ret
);
5635 static HRESULT
combine_uri(Uri
*base
, Uri
*relative
, DWORD flags
, IUri
**result
) {
5639 DWORD create_flags
= 0, len
= 0;
5641 memset(&data
, 0, sizeof(parse_data
));
5643 /* Base case is when the relative Uri has a scheme name,
5644 * if it does, then 'result' will contain the same data
5645 * as the relative Uri.
5647 if(relative
->scheme_start
> -1) {
5648 data
.uri
= SysAllocString(relative
->raw_uri
);
5651 return E_OUTOFMEMORY
;
5654 parse_uri(&data
, 0);
5656 ret
= create_uri_obj();
5659 return E_OUTOFMEMORY
;
5662 ret
->raw_uri
= data
.uri
;
5663 hr
= canonicalize_uri(&data
, ret
, 0);
5665 IUri_Release(URI(ret
));
5670 apply_default_flags(&create_flags
);
5671 ret
->create_flags
= create_flags
;
5675 DWORD raw_flags
= 0;
5677 if(base
->scheme_start
> -1) {
5678 data
.scheme
= base
->canon_uri
+base
->scheme_start
;
5679 data
.scheme_len
= base
->scheme_len
;
5680 data
.scheme_type
= base
->scheme_type
;
5682 data
.is_relative
= TRUE
;
5683 data
.scheme_type
= URL_SCHEME_UNKNOWN
;
5684 create_flags
|= Uri_CREATE_ALLOW_RELATIVE
;
5687 if(base
->authority_start
> -1) {
5688 if(base
->userinfo_start
> -1 && base
->userinfo_split
!= 0) {
5689 data
.username
= base
->canon_uri
+base
->userinfo_start
;
5690 data
.username_len
= (base
->userinfo_split
> -1) ? base
->userinfo_split
: base
->userinfo_len
;
5693 if(base
->userinfo_split
> -1) {
5694 data
.password
= base
->canon_uri
+base
->userinfo_start
+base
->userinfo_split
+1;
5695 data
.password_len
= base
->userinfo_len
-base
->userinfo_split
-1;
5698 if(base
->host_start
> -1) {
5699 data
.host
= base
->canon_uri
+base
->host_start
;
5700 data
.host_len
= base
->host_len
;
5701 data
.host_type
= base
->host_type
;
5704 if(base
->has_port
) {
5705 data
.has_port
= TRUE
;
5706 data
.port_value
= base
->port
;
5709 data
.is_opaque
= TRUE
;
5711 if(relative
->path_start
== -1 || !relative
->path_len
) {
5712 if(base
->path_start
> -1) {
5713 data
.path
= base
->canon_uri
+base
->path_start
;
5714 data
.path_len
= base
->path_len
;
5715 } else if((base
->path_start
== -1 || !base
->path_len
) && !data
.is_opaque
) {
5716 /* Just set the path as a '/' if the base didn't have
5717 * one and if it's an hierarchical URI.
5719 static const WCHAR slashW
[] = {'/',0};
5724 if(relative
->query_start
> -1) {
5725 data
.query
= relative
->canon_uri
+relative
->query_start
;
5726 data
.query_len
= relative
->query_len
;
5727 } else if(base
->query_start
> -1) {
5728 data
.query
= base
->canon_uri
+base
->query_start
;
5729 data
.query_len
= base
->query_len
;
5732 FIXME("Path merging not implemented yet!\n");
5737 if(relative
->fragment_start
> -1) {
5738 data
.fragment
= relative
->canon_uri
+relative
->fragment_start
;
5739 data
.fragment_len
= relative
->fragment_len
;
5742 if(flags
& URL_DONT_SIMPLIFY
)
5743 raw_flags
|= RAW_URI_FORCE_PORT_DISP
;
5745 len
= generate_raw_uri(&data
, data
.uri
, raw_flags
);
5746 data
.uri
= SysAllocStringLen(NULL
, len
);
5749 return E_OUTOFMEMORY
;
5752 generate_raw_uri(&data
, data
.uri
, raw_flags
);
5754 ret
= create_uri_obj();
5756 SysFreeString(data
.uri
);
5758 return E_OUTOFMEMORY
;
5761 ret
->raw_uri
= data
.uri
;
5762 hr
= canonicalize_uri(&data
, ret
, create_flags
);
5764 IUri_Release(URI(ret
));
5769 apply_default_flags(&create_flags
);
5770 ret
->create_flags
= create_flags
;
5777 /***********************************************************************
5778 * CoInternetCombineIUri (urlmon.@)
5780 HRESULT WINAPI
CoInternetCombineIUri(IUri
*pBaseUri
, IUri
*pRelativeUri
, DWORD dwCombineFlags
,
5781 IUri
**ppCombinedUri
, DWORD_PTR dwReserved
)
5784 Uri
*relative
, *base
;
5785 TRACE("(%p %p %x %p %x)\n", pBaseUri
, pRelativeUri
, dwCombineFlags
, ppCombinedUri
, (DWORD
)dwReserved
);
5788 return E_INVALIDARG
;
5790 if(!pBaseUri
|| !pRelativeUri
) {
5791 *ppCombinedUri
= NULL
;
5792 return E_INVALIDARG
;
5795 relative
= get_uri_obj(pRelativeUri
);
5796 base
= get_uri_obj(pBaseUri
);
5797 if(!relative
|| !base
) {
5798 *ppCombinedUri
= NULL
;
5799 FIXME("(%p %p %x %p %x) Unknown IUri types not supported yet.\n",
5800 pBaseUri
, pRelativeUri
, dwCombineFlags
, ppCombinedUri
, (DWORD
)dwReserved
);
5804 hr
= combine_uri(base
, relative
, dwCombineFlags
, ppCombinedUri
);
5806 FIXME("(%p %p %x %p %x): stub\n", pBaseUri
, pRelativeUri
, dwCombineFlags
, ppCombinedUri
, (DWORD
)dwReserved
);