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
40 #define RAW_URI_CONVERT_TO_DOS_PATH 0x2
42 WINE_DEFAULT_DEBUG_CHANNEL(urlmon
);
44 static const IID IID_IUriObj
= {0x4b364760,0x9f51,0x11df,{0x98,0x1c,0x08,0x00,0x20,0x0c,0x9a,0x66}};
47 const IUriVtbl
*lpIUriVtbl
;
52 /* Information about the canonicalized URI's buffer. */
56 BOOL display_modifiers
;
61 URL_SCHEME scheme_type
;
69 Uri_HOST_TYPE host_type
;
92 const IUriBuilderVtbl
*lpIUriBuilderVtbl
;
129 /* IPv6 addresses can hold up to 8 h16 components. */
133 /* An IPv6 can have 1 elision ("::"). */
134 const WCHAR
*elision
;
136 /* An IPv6 can contain 1 IPv4 address as the last 32bits of the address. */
149 BOOL has_implicit_scheme
;
150 BOOL has_implicit_ip
;
155 URL_SCHEME scheme_type
;
157 const WCHAR
*username
;
160 const WCHAR
*password
;
165 Uri_HOST_TYPE host_type
;
168 ipv6_address ipv6_address
;
181 const WCHAR
*fragment
;
185 static const CHAR hexDigits
[] = "0123456789ABCDEF";
187 /* List of scheme types/scheme names that are recognized by the IUri interface as of IE 7. */
188 static const struct {
190 WCHAR scheme_name
[16];
191 } recognized_schemes
[] = {
192 {URL_SCHEME_FTP
, {'f','t','p',0}},
193 {URL_SCHEME_HTTP
, {'h','t','t','p',0}},
194 {URL_SCHEME_GOPHER
, {'g','o','p','h','e','r',0}},
195 {URL_SCHEME_MAILTO
, {'m','a','i','l','t','o',0}},
196 {URL_SCHEME_NEWS
, {'n','e','w','s',0}},
197 {URL_SCHEME_NNTP
, {'n','n','t','p',0}},
198 {URL_SCHEME_TELNET
, {'t','e','l','n','e','t',0}},
199 {URL_SCHEME_WAIS
, {'w','a','i','s',0}},
200 {URL_SCHEME_FILE
, {'f','i','l','e',0}},
201 {URL_SCHEME_MK
, {'m','k',0}},
202 {URL_SCHEME_HTTPS
, {'h','t','t','p','s',0}},
203 {URL_SCHEME_SHELL
, {'s','h','e','l','l',0}},
204 {URL_SCHEME_SNEWS
, {'s','n','e','w','s',0}},
205 {URL_SCHEME_LOCAL
, {'l','o','c','a','l',0}},
206 {URL_SCHEME_JAVASCRIPT
, {'j','a','v','a','s','c','r','i','p','t',0}},
207 {URL_SCHEME_VBSCRIPT
, {'v','b','s','c','r','i','p','t',0}},
208 {URL_SCHEME_ABOUT
, {'a','b','o','u','t',0}},
209 {URL_SCHEME_RES
, {'r','e','s',0}},
210 {URL_SCHEME_MSSHELLROOTED
, {'m','s','-','s','h','e','l','l','-','r','o','o','t','e','d',0}},
211 {URL_SCHEME_MSSHELLIDLIST
, {'m','s','-','s','h','e','l','l','-','i','d','l','i','s','t',0}},
212 {URL_SCHEME_MSHELP
, {'h','c','p',0}},
213 {URL_SCHEME_WILDCARD
, {'*',0}}
216 /* List of default ports Windows recognizes. */
217 static const struct {
220 } default_ports
[] = {
221 {URL_SCHEME_FTP
, 21},
222 {URL_SCHEME_HTTP
, 80},
223 {URL_SCHEME_GOPHER
, 70},
224 {URL_SCHEME_NNTP
, 119},
225 {URL_SCHEME_TELNET
, 23},
226 {URL_SCHEME_WAIS
, 210},
227 {URL_SCHEME_HTTPS
, 443},
230 /* List of 3 character top level domain names Windows seems to recognize.
231 * There might be more, but, these are the only ones I've found so far.
233 static const struct {
235 } recognized_tlds
[] = {
245 static Uri
*get_uri_obj(IUri
*uri
)
250 hres
= IUri_QueryInterface(uri
, &IID_IUriObj
, (void**)&ret
);
251 return SUCCEEDED(hres
) ? ret
: NULL
;
254 static inline BOOL
is_alpha(WCHAR val
) {
255 return ((val
>= 'a' && val
<= 'z') || (val
>= 'A' && val
<= 'Z'));
258 static inline BOOL
is_num(WCHAR val
) {
259 return (val
>= '0' && val
<= '9');
262 static inline BOOL
is_drive_path(const WCHAR
*str
) {
263 return (is_alpha(str
[0]) && (str
[1] == ':' || str
[1] == '|'));
266 static inline BOOL
is_unc_path(const WCHAR
*str
) {
267 return (str
[0] == '\\' && str
[0] == '\\');
270 static inline BOOL
is_forbidden_dos_path_char(WCHAR val
) {
271 return (val
== '>' || val
== '<' || val
== '\"');
274 /* A URI is implicitly a file path if it begins with
275 * a drive letter (eg X:) or starts with "\\" (UNC path).
277 static inline BOOL
is_implicit_file_path(const WCHAR
*str
) {
278 return (is_unc_path(str
) || (is_alpha(str
[0]) && str
[1] == ':'));
281 /* Checks if the URI is a hierarchical URI. A hierarchical
282 * URI is one that has "//" after the scheme.
284 static BOOL
check_hierarchical(const WCHAR
**ptr
) {
285 const WCHAR
*start
= *ptr
;
300 /* unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~" */
301 static inline BOOL
is_unreserved(WCHAR val
) {
302 return (is_alpha(val
) || is_num(val
) || val
== '-' || val
== '.' ||
303 val
== '_' || val
== '~');
306 /* sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
307 * / "*" / "+" / "," / ";" / "="
309 static inline BOOL
is_subdelim(WCHAR val
) {
310 return (val
== '!' || val
== '$' || val
== '&' ||
311 val
== '\'' || val
== '(' || val
== ')' ||
312 val
== '*' || val
== '+' || val
== ',' ||
313 val
== ';' || val
== '=');
316 /* gen-delims = ":" / "/" / "?" / "#" / "[" / "]" / "@" */
317 static inline BOOL
is_gendelim(WCHAR val
) {
318 return (val
== ':' || val
== '/' || val
== '?' ||
319 val
== '#' || val
== '[' || val
== ']' ||
323 /* Characters that delimit the end of the authority
324 * section of a URI. Sometimes a '\\' is considered
325 * an authority delimeter.
327 static inline BOOL
is_auth_delim(WCHAR val
, BOOL acceptSlash
) {
328 return (val
== '#' || val
== '/' || val
== '?' ||
329 val
== '\0' || (acceptSlash
&& val
== '\\'));
332 /* reserved = gen-delims / sub-delims */
333 static inline BOOL
is_reserved(WCHAR val
) {
334 return (is_subdelim(val
) || is_gendelim(val
));
337 static inline BOOL
is_hexdigit(WCHAR val
) {
338 return ((val
>= 'a' && val
<= 'f') ||
339 (val
>= 'A' && val
<= 'F') ||
340 (val
>= '0' && val
<= '9'));
343 static inline BOOL
is_path_delim(WCHAR val
) {
344 return (!val
|| val
== '#' || val
== '?');
347 static BOOL
is_default_port(URL_SCHEME scheme
, DWORD port
) {
350 for(i
= 0; i
< sizeof(default_ports
)/sizeof(default_ports
[0]); ++i
) {
351 if(default_ports
[i
].scheme
== scheme
&& default_ports
[i
].port
)
358 /* List of schemes types Windows seems to expect to be hierarchical. */
359 static inline BOOL
is_hierarchical_scheme(URL_SCHEME type
) {
360 return(type
== URL_SCHEME_HTTP
|| type
== URL_SCHEME_FTP
||
361 type
== URL_SCHEME_GOPHER
|| type
== URL_SCHEME_NNTP
||
362 type
== URL_SCHEME_TELNET
|| type
== URL_SCHEME_WAIS
||
363 type
== URL_SCHEME_FILE
|| type
== URL_SCHEME_HTTPS
||
364 type
== URL_SCHEME_RES
);
367 /* Checks if 'flags' contains an invalid combination of Uri_CREATE flags. */
368 static inline BOOL
has_invalid_flag_combination(DWORD flags
) {
369 return((flags
& Uri_CREATE_DECODE_EXTRA_INFO
&& flags
& Uri_CREATE_NO_DECODE_EXTRA_INFO
) ||
370 (flags
& Uri_CREATE_CANONICALIZE
&& flags
& Uri_CREATE_NO_CANONICALIZE
) ||
371 (flags
& Uri_CREATE_CRACK_UNKNOWN_SCHEMES
&& flags
& Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES
) ||
372 (flags
& Uri_CREATE_PRE_PROCESS_HTML_URI
&& flags
& Uri_CREATE_NO_PRE_PROCESS_HTML_URI
) ||
373 (flags
& Uri_CREATE_IE_SETTINGS
&& flags
& Uri_CREATE_NO_IE_SETTINGS
));
376 /* Applies each default Uri_CREATE flags to 'flags' if it
377 * doesn't cause a flag conflict.
379 static void apply_default_flags(DWORD
*flags
) {
380 if(!(*flags
& Uri_CREATE_NO_CANONICALIZE
))
381 *flags
|= Uri_CREATE_CANONICALIZE
;
382 if(!(*flags
& Uri_CREATE_NO_DECODE_EXTRA_INFO
))
383 *flags
|= Uri_CREATE_DECODE_EXTRA_INFO
;
384 if(!(*flags
& Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES
))
385 *flags
|= Uri_CREATE_CRACK_UNKNOWN_SCHEMES
;
386 if(!(*flags
& Uri_CREATE_NO_PRE_PROCESS_HTML_URI
))
387 *flags
|= Uri_CREATE_PRE_PROCESS_HTML_URI
;
388 if(!(*flags
& Uri_CREATE_IE_SETTINGS
))
389 *flags
|= Uri_CREATE_NO_IE_SETTINGS
;
392 /* Determines if the URI is hierarchical using the information already parsed into
393 * data and using the current location of parsing in the URI string.
395 * Windows considers a URI hierarchical if on of the following is true:
396 * A.) It's a wildcard scheme.
397 * B.) It's an implicit file scheme.
398 * C.) It's a known hierarchical scheme and it has two '\\' after the scheme name.
399 * (the '\\' will be converted into "//" during canonicalization).
400 * D.) It's not a relative URI and "//" appears after the scheme name.
402 static inline BOOL
is_hierarchical_uri(const WCHAR
**ptr
, const parse_data
*data
) {
403 const WCHAR
*start
= *ptr
;
405 if(data
->scheme_type
== URL_SCHEME_WILDCARD
)
407 else if(data
->scheme_type
== URL_SCHEME_FILE
&& data
->has_implicit_scheme
)
409 else if(is_hierarchical_scheme(data
->scheme_type
) && (*ptr
)[0] == '\\' && (*ptr
)[1] == '\\') {
412 } else if(!data
->is_relative
&& check_hierarchical(ptr
))
419 /* Checks if the two Uri's are logically equivalent. It's a simple
420 * comparison, since they are both of type Uri, and it can access
421 * the properties of each Uri directly without the need to go
422 * through the "IUri_Get*" interface calls.
424 static BOOL
are_equal_simple(const Uri
*a
, const Uri
*b
) {
425 if(a
->scheme_type
== b
->scheme_type
) {
426 const BOOL known_scheme
= a
->scheme_type
!= URL_SCHEME_UNKNOWN
;
427 const BOOL are_hierarchical
=
428 (a
->authority_start
> -1 && b
->authority_start
> -1);
430 if(a
->scheme_type
== URL_SCHEME_FILE
) {
431 if(a
->canon_len
== b
->canon_len
)
432 return !StrCmpIW(a
->canon_uri
, b
->canon_uri
);
435 /* Only compare the scheme names (if any) if their unknown scheme types. */
437 if((a
->scheme_start
> -1 && b
->scheme_start
> -1) &&
438 (a
->scheme_len
== b
->scheme_len
)) {
439 /* Make sure the schemes are the same. */
440 if(StrCmpNW(a
->canon_uri
+a
->scheme_start
, b
->canon_uri
+b
->scheme_start
, a
->scheme_len
))
442 } else if(a
->scheme_len
!= b
->scheme_len
)
443 /* One of the Uri's has a scheme name, while the other doesn't. */
447 /* If they have a userinfo component, perform case sensitive compare. */
448 if((a
->userinfo_start
> -1 && b
->userinfo_start
> -1) &&
449 (a
->userinfo_len
== b
->userinfo_len
)) {
450 if(StrCmpNW(a
->canon_uri
+a
->userinfo_start
, b
->canon_uri
+b
->userinfo_start
, a
->userinfo_len
))
452 } else if(a
->userinfo_len
!= b
->userinfo_len
)
453 /* One of the Uri's had a userinfo, while the other one doesn't. */
456 /* Check if they have a host name. */
457 if((a
->host_start
> -1 && b
->host_start
> -1) &&
458 (a
->host_len
== b
->host_len
)) {
459 /* Perform a case insensitive compare if they are a known scheme type. */
461 if(StrCmpNIW(a
->canon_uri
+a
->host_start
, b
->canon_uri
+b
->host_start
, a
->host_len
))
463 } else if(StrCmpNW(a
->canon_uri
+a
->host_start
, b
->canon_uri
+b
->host_start
, a
->host_len
))
465 } else if(a
->host_len
!= b
->host_len
)
466 /* One of the Uri's had a host, while the other one didn't. */
469 if(a
->has_port
&& b
->has_port
) {
470 if(a
->port
!= b
->port
)
472 } else if(a
->has_port
|| b
->has_port
)
473 /* One had a port, while the other one didn't. */
476 /* Windows is weird with how it handles paths. For example
477 * One URI could be "http://google.com" (after canonicalization)
478 * and one could be "http://google.com/" and the IsEqual function
479 * would still evaluate to TRUE, but, only if they are both hierarchical
482 if((a
->path_start
> -1 && b
->path_start
> -1) &&
483 (a
->path_len
== b
->path_len
)) {
484 if(StrCmpNW(a
->canon_uri
+a
->path_start
, b
->canon_uri
+b
->path_start
, a
->path_len
))
486 } else if(are_hierarchical
&& a
->path_len
== -1 && b
->path_len
== 0) {
487 if(*(a
->canon_uri
+a
->path_start
) != '/')
489 } else if(are_hierarchical
&& b
->path_len
== 1 && a
->path_len
== 0) {
490 if(*(b
->canon_uri
+b
->path_start
) != '/')
492 } else if(a
->path_len
!= b
->path_len
)
495 /* Compare the query strings of the two URIs. */
496 if((a
->query_start
> -1 && b
->query_start
> -1) &&
497 (a
->query_len
== b
->query_len
)) {
498 if(StrCmpNW(a
->canon_uri
+a
->query_start
, b
->canon_uri
+b
->query_start
, a
->query_len
))
500 } else if(a
->query_len
!= b
->query_len
)
503 if((a
->fragment_start
> -1 && b
->fragment_start
> -1) &&
504 (a
->fragment_len
== b
->fragment_len
)) {
505 if(StrCmpNW(a
->canon_uri
+a
->fragment_start
, b
->canon_uri
+b
->fragment_start
, a
->fragment_len
))
507 } else if(a
->fragment_len
!= b
->fragment_len
)
510 /* If we get here, the two URIs are equivalent. */
517 /* Computes the size of the given IPv6 address.
518 * Each h16 component is 16bits, if there is an IPv4 address, it's
519 * 32bits. If there's an elision it can be 16bits to 128bits, depending
520 * on the number of other components.
522 * Modeled after google-url's CheckIPv6ComponentsSize function
524 static void compute_ipv6_comps_size(ipv6_address
*address
) {
525 address
->components_size
= address
->h16_count
* 2;
528 /* IPv4 address is 4 bytes. */
529 address
->components_size
+= 4;
531 if(address
->elision
) {
532 /* An elision can be anywhere from 2 bytes up to 16 bytes.
533 * It size depends on the size of the h16 and IPv4 components.
535 address
->elision_size
= 16 - address
->components_size
;
536 if(address
->elision_size
< 2)
537 address
->elision_size
= 2;
539 address
->elision_size
= 0;
542 /* Taken from dlls/jscript/lex.c */
543 static int hex_to_int(WCHAR val
) {
544 if(val
>= '0' && val
<= '9')
546 else if(val
>= 'a' && val
<= 'f')
547 return val
- 'a' + 10;
548 else if(val
>= 'A' && val
<= 'F')
549 return val
- 'A' + 10;
554 /* Helper function for converting a percent encoded string
555 * representation of a WCHAR value into its actual WCHAR value. If
556 * the two characters following the '%' aren't valid hex values then
557 * this function returns the NULL character.
560 * "%2E" will result in '.' being returned by this function.
562 static WCHAR
decode_pct_val(const WCHAR
*ptr
) {
565 if(*ptr
== '%' && is_hexdigit(*(ptr
+ 1)) && is_hexdigit(*(ptr
+ 2))) {
566 INT a
= hex_to_int(*(ptr
+ 1));
567 INT b
= hex_to_int(*(ptr
+ 2));
576 /* Helper function for percent encoding a given character
577 * and storing the encoded value into a given buffer (dest).
579 * It's up to the calling function to ensure that there is
580 * at least enough space in 'dest' for the percent encoded
581 * value to be stored (so dest + 3 spaces available).
583 static inline void pct_encode_val(WCHAR val
, WCHAR
*dest
) {
585 dest
[1] = hexDigits
[(val
>> 4) & 0xf];
586 dest
[2] = hexDigits
[val
& 0xf];
589 /* Scans the range of characters [str, end] and returns the last occurrence
590 * of 'ch' or returns NULL.
592 static const WCHAR
*str_last_of(const WCHAR
*str
, const WCHAR
*end
, WCHAR ch
) {
593 const WCHAR
*ptr
= end
;
604 /* Attempts to parse the domain name from the host.
606 * This function also includes the Top-level Domain (TLD) name
607 * of the host when it tries to find the domain name. If it finds
608 * a valid domain name it will assign 'domain_start' the offset
609 * into 'host' where the domain name starts.
611 * It's implied that if a domain name its range is implied to be
612 * [host+domain_start, host+host_len).
614 static void find_domain_name(const WCHAR
*host
, DWORD host_len
,
616 const WCHAR
*last_tld
, *sec_last_tld
, *end
;
618 end
= host
+host_len
-1;
622 /* There has to be at least enough room for a '.' followed by a
623 * 3 character TLD for a domain to even exist in the host name.
628 last_tld
= str_last_of(host
, end
, '.');
630 /* http://hostname -> has no domain name. */
633 sec_last_tld
= str_last_of(host
, last_tld
-1, '.');
635 /* If the '.' is at the beginning of the host there
636 * has to be at least 3 characters in the TLD for it
638 * Ex: .com -> .com as the domain name.
639 * .co -> has no domain name.
641 if(last_tld
-host
== 0) {
642 if(end
-(last_tld
-1) < 3)
644 } else if(last_tld
-host
== 3) {
647 /* If there's three characters in front of last_tld and
648 * they are on the list of recognized TLDs, then this
649 * host doesn't have a domain (since the host only contains
651 * Ex: edu.uk -> has no domain name.
652 * foo.uk -> foo.uk as the domain name.
654 for(i
= 0; i
< sizeof(recognized_tlds
)/sizeof(recognized_tlds
[0]); ++i
) {
655 if(!StrCmpNIW(host
, recognized_tlds
[i
].tld_name
, 3))
658 } else if(last_tld
-host
< 3)
659 /* Anything less than 3 characters is considered part
661 * Ex: ak.uk -> Has no domain name.
665 /* Otherwise the domain name is the whole host name. */
667 } else if(end
+1-last_tld
> 3) {
668 /* If the last_tld has more than 3 characters, then it's automatically
669 * considered the TLD of the domain name.
670 * Ex: www.winehq.org.uk.test -> uk.test as the domain name.
672 *domain_start
= (sec_last_tld
+1)-host
;
673 } else if(last_tld
- (sec_last_tld
+1) < 4) {
675 /* If the sec_last_tld is 3 characters long it HAS to be on the list of
676 * recognized to still be considered part of the TLD name, otherwise
677 * its considered the domain name.
678 * Ex: www.google.com.uk -> google.com.uk as the domain name.
679 * www.google.foo.uk -> foo.uk as the domain name.
681 if(last_tld
- (sec_last_tld
+1) == 3) {
682 for(i
= 0; i
< sizeof(recognized_tlds
)/sizeof(recognized_tlds
[0]); ++i
) {
683 if(!StrCmpNIW(sec_last_tld
+1, recognized_tlds
[i
].tld_name
, 3)) {
684 const WCHAR
*domain
= str_last_of(host
, sec_last_tld
-1, '.');
689 *domain_start
= (domain
+1) - host
;
690 TRACE("Found domain name %s\n", debugstr_wn(host
+*domain_start
,
691 (host
+host_len
)-(host
+*domain_start
)));
696 *domain_start
= (sec_last_tld
+1)-host
;
698 /* Since the sec_last_tld is less than 3 characters it's considered
700 * Ex: www.google.fo.uk -> google.fo.uk as the domain name.
702 const WCHAR
*domain
= str_last_of(host
, sec_last_tld
-1, '.');
707 *domain_start
= (domain
+1) - host
;
710 /* The second to last TLD has more than 3 characters making it
712 * Ex: www.google.test.us -> test.us as the domain name.
714 *domain_start
= (sec_last_tld
+1)-host
;
717 TRACE("Found domain name %s\n", debugstr_wn(host
+*domain_start
,
718 (host
+host_len
)-(host
+*domain_start
)));
721 /* Removes the dot segments from a hierarchical URIs path component. This
722 * function performs the removal in place.
724 * This is a modified version of Qt's QUrl function "removeDotsFromPath".
726 * This function returns the new length of the path string.
728 static DWORD
remove_dot_segments(WCHAR
*path
, DWORD path_len
) {
730 const WCHAR
*in
= out
;
731 const WCHAR
*end
= out
+ path_len
;
735 /* A. if the input buffer begins with a prefix of "/./" or "/.",
736 * where "." is a complete path segment, then replace that
737 * prefix with "/" in the input buffer; otherwise,
739 if(in
<= end
- 3 && in
[0] == '/' && in
[1] == '.' && in
[2] == '/') {
742 } else if(in
== end
- 2 && in
[0] == '/' && in
[1] == '.') {
748 /* B. if the input buffer begins with a prefix of "/../" or "/..",
749 * where ".." is a complete path segment, then replace that
750 * prefix with "/" in the input buffer and remove the last
751 * segment and its preceding "/" (if any) from the output
754 if(in
<= end
- 4 && in
[0] == '/' && in
[1] == '.' && in
[2] == '.' && in
[3] == '/') {
755 while(out
> path
&& *(--out
) != '/');
759 } else if(in
== end
- 3 && in
[0] == '/' && in
[1] == '.' && in
[2] == '.') {
760 while(out
> path
&& *(--out
) != '/');
769 /* C. move the first path segment in the input buffer to the end of
770 * the output buffer, including the initial "/" character (if
771 * any) and any subsequent characters up to, but not including,
772 * the next "/" character or the end of the input buffer.
775 while(in
< end
&& *in
!= '/')
780 TRACE("(%p %d): Path after dot segments removed %s len=%d\n", path
, path_len
,
781 debugstr_wn(path
, len
), len
);
785 /* Attempts to find the file extension in a given path. */
786 static INT
find_file_extension(const WCHAR
*path
, DWORD path_len
) {
789 for(end
= path
+path_len
-1; end
>= path
&& *end
!= '/' && *end
!= '\\'; --end
) {
797 /* Computes the location where the elision should occur in the IPv6
798 * address using the numerical values of each component stored in
799 * 'values'. If the address shouldn't contain an elision then 'index'
800 * is assigned -1 as it's value. Otherwise 'index' will contain the
801 * starting index (into values) where the elision should be, and 'count'
802 * will contain the number of cells the elision covers.
805 * Windows will expand an elision if the elision only represents 1 h16
806 * component of the URI.
808 * Ex: [1::2:3:4:5:6:7] -> [1:0:2:3:4:5:6:7]
810 * If the IPv6 address contains an IPv4 address, the IPv4 address is also
811 * considered for being included as part of an elision if all it's components
814 * Ex: [1:2:3:4:5:6:0.0.0.0] -> [1:2:3:4:5:6::]
816 static void compute_elision_location(const ipv6_address
*address
, const USHORT values
[8],
817 INT
*index
, DWORD
*count
) {
818 DWORD i
, max_len
, cur_len
;
819 INT max_index
, cur_index
;
821 max_len
= cur_len
= 0;
822 max_index
= cur_index
= -1;
823 for(i
= 0; i
< 8; ++i
) {
824 BOOL check_ipv4
= (address
->ipv4
&& i
== 6);
825 BOOL is_end
= (check_ipv4
|| i
== 7);
828 /* Check if the IPv4 address contains only zeros. */
829 if(values
[i
] == 0 && values
[i
+1] == 0) {
836 } else if(values
[i
] == 0) {
843 if(is_end
|| values
[i
] != 0) {
844 /* We only consider it for an elision if it's
845 * more than 1 component long.
847 if(cur_len
> 1 && cur_len
> max_len
) {
848 /* Found the new elision location. */
850 max_index
= cur_index
;
853 /* Reset the current range for the next range of zeros. */
863 /* Removes all the leading and trailing white spaces or
864 * control characters from the URI and removes all control
865 * characters inside of the URI string.
867 static BSTR
pre_process_uri(LPCWSTR uri
) {
870 const WCHAR
*start
, *end
;
876 /* Skip leading controls and whitespace. */
877 while(iscntrlW(*start
) || isspaceW(*start
)) ++start
;
881 /* URI consisted only of control/whitespace. */
882 ret
= SysAllocStringLen(NULL
, 0);
884 while(iscntrlW(*end
) || isspaceW(*end
)) --end
;
886 buf
= heap_alloc(((end
+1)-start
)*sizeof(WCHAR
));
890 for(ptr
= buf
; start
< end
+1; ++start
) {
891 if(!iscntrlW(*start
))
895 ret
= SysAllocStringLen(buf
, ptr
-buf
);
902 /* Converts the specified IPv4 address into an uint value.
904 * This function assumes that the IPv4 address has already been validated.
906 static UINT
ipv4toui(const WCHAR
*ip
, DWORD len
) {
908 DWORD comp_value
= 0;
911 for(ptr
= ip
; ptr
< ip
+len
; ++ptr
) {
917 comp_value
= comp_value
*10 + (*ptr
-'0');
926 /* Converts an IPv4 address in numerical form into it's fully qualified
927 * string form. This function returns the number of characters written
928 * to 'dest'. If 'dest' is NULL this function will return the number of
929 * characters that would have been written.
931 * It's up to the caller to ensure there's enough space in 'dest' for the
934 static DWORD
ui2ipv4(WCHAR
*dest
, UINT address
) {
935 static const WCHAR formatW
[] =
936 {'%','u','.','%','u','.','%','u','.','%','u',0};
940 digits
[0] = (address
>> 24) & 0xff;
941 digits
[1] = (address
>> 16) & 0xff;
942 digits
[2] = (address
>> 8) & 0xff;
943 digits
[3] = address
& 0xff;
947 ret
= sprintfW(tmp
, formatW
, digits
[0], digits
[1], digits
[2], digits
[3]);
949 ret
= sprintfW(dest
, formatW
, digits
[0], digits
[1], digits
[2], digits
[3]);
954 static DWORD
ui2str(WCHAR
*dest
, UINT value
) {
955 static const WCHAR formatW
[] = {'%','u',0};
960 ret
= sprintfW(tmp
, formatW
, value
);
962 ret
= sprintfW(dest
, formatW
, value
);
967 /* Converts an h16 component (from an IPv6 address) into it's
970 * This function assumes that the h16 component has already been validated.
972 static USHORT
h16tous(h16 component
) {
976 for(i
= 0; i
< component
.len
; ++i
) {
978 ret
+= hex_to_int(component
.str
[i
]);
984 /* Converts an IPv6 address into it's 128 bits (16 bytes) numerical value.
986 * This function assumes that the ipv6_address has already been validated.
988 static BOOL
ipv6_to_number(const ipv6_address
*address
, USHORT number
[8]) {
989 DWORD i
, cur_component
= 0;
990 BOOL already_passed_elision
= FALSE
;
992 for(i
= 0; i
< address
->h16_count
; ++i
) {
993 if(address
->elision
) {
994 if(address
->components
[i
].str
> address
->elision
&& !already_passed_elision
) {
995 /* Means we just passed the elision and need to add it's values to
996 * 'number' before we do anything else.
999 for(j
= 0; j
< address
->elision_size
; j
+=2)
1000 number
[cur_component
++] = 0;
1002 already_passed_elision
= TRUE
;
1006 number
[cur_component
++] = h16tous(address
->components
[i
]);
1009 /* Case when the elision appears after the h16 components. */
1010 if(!already_passed_elision
&& address
->elision
) {
1011 for(i
= 0; i
< address
->elision_size
; i
+=2)
1012 number
[cur_component
++] = 0;
1013 already_passed_elision
= TRUE
;
1017 UINT value
= ipv4toui(address
->ipv4
, address
->ipv4_len
);
1019 if(cur_component
!= 6) {
1020 ERR("(%p %p): Failed sanity check with %d\n", address
, number
, cur_component
);
1024 number
[cur_component
++] = (value
>> 16) & 0xffff;
1025 number
[cur_component
] = value
& 0xffff;
1031 /* Checks if the characters pointed to by 'ptr' are
1032 * a percent encoded data octet.
1034 * pct-encoded = "%" HEXDIG HEXDIG
1036 static BOOL
check_pct_encoded(const WCHAR
**ptr
) {
1037 const WCHAR
*start
= *ptr
;
1043 if(!is_hexdigit(**ptr
)) {
1049 if(!is_hexdigit(**ptr
)) {
1058 /* dec-octet = DIGIT ; 0-9
1059 * / %x31-39 DIGIT ; 10-99
1060 * / "1" 2DIGIT ; 100-199
1061 * / "2" %x30-34 DIGIT ; 200-249
1062 * / "25" %x30-35 ; 250-255
1064 static BOOL
check_dec_octet(const WCHAR
**ptr
) {
1065 const WCHAR
*c1
, *c2
, *c3
;
1068 /* A dec-octet must be at least 1 digit long. */
1069 if(*c1
< '0' || *c1
> '9')
1075 /* Since the 1 digit requirment was meet, it doesn't
1076 * matter if this is a DIGIT value, it's considered a
1079 if(*c2
< '0' || *c2
> '9')
1085 /* Same explanation as above. */
1086 if(*c3
< '0' || *c3
> '9')
1089 /* Anything > 255 isn't a valid IP dec-octet. */
1090 if(*c1
>= '2' && *c2
>= '5' && *c3
>= '5') {
1099 /* Checks if there is an implicit IPv4 address in the host component of the URI.
1100 * The max value of an implicit IPv4 address is UINT_MAX.
1103 * "234567" would be considered an implicit IPv4 address.
1105 static BOOL
check_implicit_ipv4(const WCHAR
**ptr
, UINT
*val
) {
1106 const WCHAR
*start
= *ptr
;
1110 while(is_num(**ptr
)) {
1111 ret
= ret
*10 + (**ptr
- '0');
1113 if(ret
> UINT_MAX
) {
1127 /* Checks if the string contains an IPv4 address.
1129 * This function has a strict mode or a non-strict mode of operation
1130 * When 'strict' is set to FALSE this function will return TRUE if
1131 * the string contains at least 'dec-octet "." dec-octet' since partial
1132 * IPv4 addresses will be normalized out into full IPv4 addresses. When
1133 * 'strict' is set this function expects there to be a full IPv4 address.
1135 * IPv4address = dec-octet "." dec-octet "." dec-octet "." dec-octet
1137 static BOOL
check_ipv4address(const WCHAR
**ptr
, BOOL strict
) {
1138 const WCHAR
*start
= *ptr
;
1140 if(!check_dec_octet(ptr
)) {
1151 if(!check_dec_octet(ptr
)) {
1165 if(!check_dec_octet(ptr
)) {
1179 if(!check_dec_octet(ptr
)) {
1184 /* Found a four digit ip address. */
1187 /* Tries to parse the scheme name of the URI.
1189 * scheme = ALPHA *(ALPHA | NUM | '+' | '-' | '.') as defined by RFC 3896.
1190 * NOTE: Windows accepts a number as the first character of a scheme.
1192 static BOOL
parse_scheme_name(const WCHAR
**ptr
, parse_data
*data
, DWORD extras
) {
1193 const WCHAR
*start
= *ptr
;
1195 data
->scheme
= NULL
;
1196 data
->scheme_len
= 0;
1199 if(**ptr
== '*' && *ptr
== start
) {
1200 /* Might have found a wildcard scheme. If it is the next
1201 * char has to be a ':' for it to be a valid URI
1205 } else if(!is_num(**ptr
) && !is_alpha(**ptr
) && **ptr
!= '+' &&
1206 **ptr
!= '-' && **ptr
!= '.')
1215 /* Schemes must end with a ':' */
1216 if(**ptr
!= ':' && !((extras
& ALLOW_NULL_TERM_SCHEME
) && !**ptr
)) {
1221 data
->scheme
= start
;
1222 data
->scheme_len
= *ptr
- start
;
1228 /* Tries to deduce the corresponding URL_SCHEME for the given URI. Stores
1229 * the deduced URL_SCHEME in data->scheme_type.
1231 static BOOL
parse_scheme_type(parse_data
*data
) {
1232 /* If there's scheme data then see if it's a recognized scheme. */
1233 if(data
->scheme
&& data
->scheme_len
) {
1236 for(i
= 0; i
< sizeof(recognized_schemes
)/sizeof(recognized_schemes
[0]); ++i
) {
1237 if(lstrlenW(recognized_schemes
[i
].scheme_name
) == data
->scheme_len
) {
1238 /* Has to be a case insensitive compare. */
1239 if(!StrCmpNIW(recognized_schemes
[i
].scheme_name
, data
->scheme
, data
->scheme_len
)) {
1240 data
->scheme_type
= recognized_schemes
[i
].scheme
;
1246 /* If we get here it means it's not a recognized scheme. */
1247 data
->scheme_type
= URL_SCHEME_UNKNOWN
;
1249 } else if(data
->is_relative
) {
1250 /* Relative URI's have no scheme. */
1251 data
->scheme_type
= URL_SCHEME_UNKNOWN
;
1254 /* Should never reach here! what happened... */
1255 FIXME("(%p): Unable to determine scheme type for URI %s\n", data
, debugstr_w(data
->uri
));
1260 /* Tries to parse (or deduce) the scheme_name of a URI. If it can't
1261 * parse a scheme from the URI it will try to deduce the scheme_name and scheme_type
1262 * using the flags specified in 'flags' (if any). Flags that affect how this function
1263 * operates are the Uri_CREATE_ALLOW_* flags.
1265 * All parsed/deduced information will be stored in 'data' when the function returns.
1267 * Returns TRUE if it was able to successfully parse the information.
1269 static BOOL
parse_scheme(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
, DWORD extras
) {
1270 static const WCHAR fileW
[] = {'f','i','l','e',0};
1271 static const WCHAR wildcardW
[] = {'*',0};
1273 /* First check to see if the uri could implicitly be a file path. */
1274 if(is_implicit_file_path(*ptr
)) {
1275 if(flags
& Uri_CREATE_ALLOW_IMPLICIT_FILE_SCHEME
) {
1276 data
->scheme
= fileW
;
1277 data
->scheme_len
= lstrlenW(fileW
);
1278 data
->has_implicit_scheme
= TRUE
;
1280 TRACE("(%p %p %x): URI is an implicit file path.\n", ptr
, data
, flags
);
1282 /* Window's does not consider anything that can implicitly be a file
1283 * path to be a valid URI if the ALLOW_IMPLICIT_FILE_SCHEME flag is not set...
1285 TRACE("(%p %p %x): URI is implicitly a file path, but, the ALLOW_IMPLICIT_FILE_SCHEME flag wasn't set.\n",
1289 } else if(!parse_scheme_name(ptr
, data
, extras
)) {
1290 /* No Scheme was found, this means it could be:
1291 * a) an implicit Wildcard scheme
1295 if(flags
& Uri_CREATE_ALLOW_IMPLICIT_WILDCARD_SCHEME
) {
1296 data
->scheme
= wildcardW
;
1297 data
->scheme_len
= lstrlenW(wildcardW
);
1298 data
->has_implicit_scheme
= TRUE
;
1300 TRACE("(%p %p %x): URI is an implicit wildcard scheme.\n", ptr
, data
, flags
);
1301 } else if (flags
& Uri_CREATE_ALLOW_RELATIVE
) {
1302 data
->is_relative
= TRUE
;
1303 TRACE("(%p %p %x): URI is relative.\n", ptr
, data
, flags
);
1305 TRACE("(%p %p %x): Malformed URI found. Unable to deduce scheme name.\n", ptr
, data
, flags
);
1310 if(!data
->is_relative
)
1311 TRACE("(%p %p %x): Found scheme=%s scheme_len=%d\n", ptr
, data
, flags
,
1312 debugstr_wn(data
->scheme
, data
->scheme_len
), data
->scheme_len
);
1314 if(!parse_scheme_type(data
))
1317 TRACE("(%p %p %x): Assigned %d as the URL_SCHEME.\n", ptr
, data
, flags
, data
->scheme_type
);
1321 static BOOL
parse_username(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
, DWORD extras
) {
1322 data
->username
= *ptr
;
1324 while(**ptr
!= ':' && **ptr
!= '@') {
1326 if(!check_pct_encoded(ptr
)) {
1327 if(data
->scheme_type
!= URL_SCHEME_UNKNOWN
) {
1328 *ptr
= data
->username
;
1329 data
->username
= NULL
;
1334 } else if(extras
& ALLOW_NULL_TERM_USER_NAME
&& !**ptr
)
1336 else if(is_auth_delim(**ptr
, data
->scheme_type
!= URL_SCHEME_UNKNOWN
)) {
1337 *ptr
= data
->username
;
1338 data
->username
= NULL
;
1345 data
->username_len
= *ptr
- data
->username
;
1349 static BOOL
parse_password(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
, DWORD extras
) {
1350 data
->password
= *ptr
;
1352 while(**ptr
!= '@') {
1354 if(!check_pct_encoded(ptr
)) {
1355 if(data
->scheme_type
!= URL_SCHEME_UNKNOWN
) {
1356 *ptr
= data
->password
;
1357 data
->password
= NULL
;
1362 } else if(extras
& ALLOW_NULL_TERM_PASSWORD
&& !**ptr
)
1364 else if(is_auth_delim(**ptr
, data
->scheme_type
!= URL_SCHEME_UNKNOWN
)) {
1365 *ptr
= data
->password
;
1366 data
->password
= NULL
;
1373 data
->password_len
= *ptr
- data
->password
;
1377 /* Parses the userinfo part of the URI (if it exists). The userinfo field of
1378 * a URI can consist of "username:password@", or just "username@".
1381 * userinfo = *( unreserved / pct-encoded / sub-delims / ":" )
1384 * 1) If there is more than one ':' in the userinfo part of the URI Windows
1385 * uses the first occurrence of ':' to delimit the username and password
1389 * ftp://user:pass:word@winehq.org
1391 * Would yield, "user" as the username and "pass:word" as the password.
1393 * 2) Windows allows any character to appear in the "userinfo" part of
1394 * a URI, as long as it's not an authority delimeter character set.
1396 static void parse_userinfo(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
) {
1397 const WCHAR
*start
= *ptr
;
1399 if(!parse_username(ptr
, data
, flags
, 0)) {
1400 TRACE("(%p %p %x): URI contained no userinfo.\n", ptr
, data
, flags
);
1406 if(!parse_password(ptr
, data
, flags
, 0)) {
1408 data
->username
= NULL
;
1409 data
->username_len
= 0;
1410 TRACE("(%p %p %x): URI contained no userinfo.\n", ptr
, data
, flags
);
1417 data
->username
= NULL
;
1418 data
->username_len
= 0;
1419 data
->password
= NULL
;
1420 data
->password_len
= 0;
1422 TRACE("(%p %p %x): URI contained no userinfo.\n", ptr
, data
, flags
);
1427 TRACE("(%p %p %x): Found username %s len=%d.\n", ptr
, data
, flags
,
1428 debugstr_wn(data
->username
, data
->username_len
), data
->username_len
);
1431 TRACE("(%p %p %x): Found password %s len=%d.\n", ptr
, data
, flags
,
1432 debugstr_wn(data
->password
, data
->password_len
), data
->password_len
);
1437 /* Attempts to parse a port from the URI.
1440 * Windows seems to have a cap on what the maximum value
1441 * for a port can be. The max value is USHORT_MAX.
1445 static BOOL
parse_port(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
) {
1449 while(!is_auth_delim(**ptr
, data
->scheme_type
!= URL_SCHEME_UNKNOWN
)) {
1450 if(!is_num(**ptr
)) {
1456 port
= port
*10 + (**ptr
-'0');
1458 if(port
> USHORT_MAX
) {
1467 data
->has_port
= TRUE
;
1468 data
->port_value
= port
;
1469 data
->port_len
= *ptr
- data
->port
;
1471 TRACE("(%p %p %x): Found port %s len=%d value=%u\n", ptr
, data
, flags
,
1472 debugstr_wn(data
->port
, data
->port_len
), data
->port_len
, data
->port_value
);
1476 /* Attempts to parse a IPv4 address from the URI.
1479 * Window's normalizes IPv4 addresses, This means there's three
1480 * possibilities for the URI to contain an IPv4 address.
1481 * 1) A well formed address (ex. 192.2.2.2).
1482 * 2) A partially formed address. For example "192.0" would
1483 * normalize to "192.0.0.0" during canonicalization.
1484 * 3) An implicit IPv4 address. For example "256" would
1485 * normalize to "0.0.1.0" during canonicalization. Also
1486 * note that the maximum value for an implicit IP address
1487 * is UINT_MAX, if the value in the URI exceeds this then
1488 * it is not considered an IPv4 address.
1490 static BOOL
parse_ipv4address(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
) {
1491 const BOOL is_unknown
= data
->scheme_type
== URL_SCHEME_UNKNOWN
;
1494 if(!check_ipv4address(ptr
, FALSE
)) {
1495 if(!check_implicit_ipv4(ptr
, &data
->implicit_ipv4
)) {
1496 TRACE("(%p %p %x): URI didn't contain anything looking like an IPv4 address.\n",
1502 data
->has_implicit_ip
= TRUE
;
1505 /* Check if what we found is the only part of the host name (if it isn't
1506 * we don't have an IPv4 address).
1510 if(!parse_port(ptr
, data
, flags
)) {
1515 } else if(!is_auth_delim(**ptr
, !is_unknown
)) {
1516 /* Found more data which belongs the host, so this isn't an IPv4. */
1519 data
->has_implicit_ip
= FALSE
;
1523 data
->host_len
= *ptr
- data
->host
;
1524 data
->host_type
= Uri_HOST_IPV4
;
1526 TRACE("(%p %p %x): IPv4 address found. host=%s host_len=%d host_type=%d\n",
1527 ptr
, data
, flags
, debugstr_wn(data
->host
, data
->host_len
),
1528 data
->host_len
, data
->host_type
);
1532 /* Attempts to parse the reg-name from the URI.
1534 * Because of the way Windows handles ':' this function also
1535 * handles parsing the port.
1537 * reg-name = *( unreserved / pct-encoded / sub-delims )
1540 * Windows allows everything, but, the characters in "auth_delims" and ':'
1541 * to appear in a reg-name, unless it's an unknown scheme type then ':' is
1542 * allowed to appear (even if a valid port isn't after it).
1544 * Windows doesn't like host names which start with '[' and end with ']'
1545 * and don't contain a valid IP literal address in between them.
1547 * On Windows if an '[' is encountered in the host name the ':' no longer
1548 * counts as a delimiter until you reach the next ']' or an "authority delimeter".
1550 * A reg-name CAN be empty.
1552 static BOOL
parse_reg_name(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
, DWORD extras
) {
1553 const BOOL has_start_bracket
= **ptr
== '[';
1554 const BOOL known_scheme
= data
->scheme_type
!= URL_SCHEME_UNKNOWN
;
1555 const BOOL is_res
= data
->scheme_type
== URL_SCHEME_RES
;
1556 BOOL inside_brackets
= has_start_bracket
;
1558 /* res URIs don't have ports. */
1559 BOOL ignore_col
= (extras
& IGNORE_PORT_DELIMITER
) || is_res
;
1561 /* We have to be careful with file schemes. */
1562 if(data
->scheme_type
== URL_SCHEME_FILE
) {
1563 /* This is because an implicit file scheme could be "C:\\test" and it
1564 * would trick this function into thinking the host is "C", when after
1565 * canonicalization the host would end up being an empty string. A drive
1566 * path can also have a '|' instead of a ':' after the drive letter.
1568 if(is_drive_path(*ptr
)) {
1569 /* Regular old drive paths don't have a host type (or host name). */
1570 data
->host_type
= Uri_HOST_UNKNOWN
;
1574 } else if(is_unc_path(*ptr
))
1575 /* Skip past the "\\" of a UNC path. */
1581 /* For res URIs, everything before the first '/' is
1582 * considered the host.
1584 while((!is_res
&& !is_auth_delim(**ptr
, known_scheme
)) ||
1585 (is_res
&& **ptr
&& **ptr
!= '/')) {
1586 if(**ptr
== ':' && !ignore_col
) {
1587 /* We can ignore ':' if were inside brackets.*/
1588 if(!inside_brackets
) {
1589 const WCHAR
*tmp
= (*ptr
)++;
1591 /* Attempt to parse the port. */
1592 if(!parse_port(ptr
, data
, flags
)) {
1593 /* Windows expects there to be a valid port for known scheme types. */
1594 if(data
->scheme_type
!= URL_SCHEME_UNKNOWN
) {
1597 TRACE("(%p %p %x %x): Expected valid port\n", ptr
, data
, flags
, extras
);
1600 /* Windows gives up on trying to parse a port when it
1601 * encounters 1 invalid port.
1605 data
->host_len
= tmp
- data
->host
;
1609 } else if(**ptr
== '%' && (known_scheme
&& !is_res
)) {
1610 /* Has to be a legit % encoded value. */
1611 if(!check_pct_encoded(ptr
)) {
1617 } else if(is_res
&& is_forbidden_dos_path_char(**ptr
)) {
1621 } else if(**ptr
== ']')
1622 inside_brackets
= FALSE
;
1623 else if(**ptr
== '[')
1624 inside_brackets
= TRUE
;
1629 if(has_start_bracket
) {
1630 /* Make sure the last character of the host wasn't a ']'. */
1631 if(*(*ptr
-1) == ']') {
1632 TRACE("(%p %p %x %x): Expected an IP literal inside of the host\n",
1633 ptr
, data
, flags
, extras
);
1640 /* Don't overwrite our length if we found a port earlier. */
1642 data
->host_len
= *ptr
- data
->host
;
1644 /* If the host is empty, then it's an unknown host type. */
1645 if(data
->host_len
== 0 || is_res
)
1646 data
->host_type
= Uri_HOST_UNKNOWN
;
1648 data
->host_type
= Uri_HOST_DNS
;
1650 TRACE("(%p %p %x %x): Parsed reg-name. host=%s len=%d\n", ptr
, data
, flags
, extras
,
1651 debugstr_wn(data
->host
, data
->host_len
), data
->host_len
);
1655 /* Attempts to parse an IPv6 address out of the URI.
1657 * IPv6address = 6( h16 ":" ) ls32
1658 * / "::" 5( h16 ":" ) ls32
1659 * / [ h16 ] "::" 4( h16 ":" ) ls32
1660 * / [ *1( h16 ":" ) h16 ] "::" 3( h16 ":" ) ls32
1661 * / [ *2( h16 ":" ) h16 ] "::" 2( h16 ":" ) ls32
1662 * / [ *3( h16 ":" ) h16 ] "::" h16 ":" ls32
1663 * / [ *4( h16 ":" ) h16 ] "::" ls32
1664 * / [ *5( h16 ":" ) h16 ] "::" h16
1665 * / [ *6( h16 ":" ) h16 ] "::"
1667 * ls32 = ( h16 ":" h16 ) / IPv4address
1668 * ; least-significant 32 bits of address.
1671 * ; 16 bits of address represented in hexadecimal.
1673 * Modeled after google-url's 'DoParseIPv6' function.
1675 static BOOL
parse_ipv6address(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
) {
1676 const WCHAR
*start
, *cur_start
;
1679 start
= cur_start
= *ptr
;
1680 memset(&ip
, 0, sizeof(ipv6_address
));
1683 /* Check if we're on the last character of the host. */
1684 BOOL is_end
= (is_auth_delim(**ptr
, data
->scheme_type
!= URL_SCHEME_UNKNOWN
)
1687 BOOL is_split
= (**ptr
== ':');
1688 BOOL is_elision
= (is_split
&& !is_end
&& *(*ptr
+1) == ':');
1690 /* Check if we're at the end of a component, or
1691 * if we're at the end of the IPv6 address.
1693 if(is_split
|| is_end
) {
1696 cur_len
= *ptr
- cur_start
;
1698 /* h16 can't have a length > 4. */
1702 TRACE("(%p %p %x): h16 component to long.\n",
1708 /* An h16 component can't have the length of 0 unless
1709 * the elision is at the beginning of the address, or
1710 * at the end of the address.
1712 if(!((*ptr
== start
&& is_elision
) ||
1713 (is_end
&& (*ptr
-2) == ip
.elision
))) {
1715 TRACE("(%p %p %x): IPv6 component cannot have a length of 0.\n",
1722 /* An IPv6 address can have no more than 8 h16 components. */
1723 if(ip
.h16_count
>= 8) {
1725 TRACE("(%p %p %x): Not a IPv6 address, to many h16 components.\n",
1730 ip
.components
[ip
.h16_count
].str
= cur_start
;
1731 ip
.components
[ip
.h16_count
].len
= cur_len
;
1733 TRACE("(%p %p %x): Found h16 component %s, len=%d, h16_count=%d\n",
1734 ptr
, data
, flags
, debugstr_wn(cur_start
, cur_len
), cur_len
,
1744 /* A IPv6 address can only have 1 elision ('::'). */
1748 TRACE("(%p %p %x): IPv6 address cannot have 2 elisions.\n",
1760 if(!check_ipv4address(ptr
, TRUE
)) {
1761 if(!is_hexdigit(**ptr
)) {
1762 /* Not a valid character for an IPv6 address. */
1767 /* Found an IPv4 address. */
1768 ip
.ipv4
= cur_start
;
1769 ip
.ipv4_len
= *ptr
- cur_start
;
1771 TRACE("(%p %p %x): Found an attached IPv4 address %s len=%d.\n",
1772 ptr
, data
, flags
, debugstr_wn(ip
.ipv4
, ip
.ipv4_len
),
1775 /* IPv4 addresses can only appear at the end of a IPv6. */
1781 compute_ipv6_comps_size(&ip
);
1783 /* Make sure the IPv6 address adds up to 16 bytes. */
1784 if(ip
.components_size
+ ip
.elision_size
!= 16) {
1786 TRACE("(%p %p %x): Invalid IPv6 address, did not add up to 16 bytes.\n",
1791 if(ip
.elision_size
== 2) {
1792 /* For some reason on Windows if an elision that represents
1793 * only 1 h16 component is encountered at the very begin or
1794 * end of an IPv6 address, Windows does not consider it a
1795 * valid IPv6 address.
1797 * Ex: [::2:3:4:5:6:7] is not valid, even though the sum
1798 * of all the components == 128bits.
1800 if(ip
.elision
< ip
.components
[0].str
||
1801 ip
.elision
> ip
.components
[ip
.h16_count
-1].str
) {
1803 TRACE("(%p %p %x): Invalid IPv6 address. Detected elision of 2 bytes at the beginning or end of the address.\n",
1809 data
->host_type
= Uri_HOST_IPV6
;
1810 data
->has_ipv6
= TRUE
;
1811 data
->ipv6_address
= ip
;
1813 TRACE("(%p %p %x): Found valid IPv6 literal %s len=%d\n",
1814 ptr
, data
, flags
, debugstr_wn(start
, *ptr
-start
),
1819 /* IPvFuture = "v" 1*HEXDIG "." 1*( unreserved / sub-delims / ":" ) */
1820 static BOOL
parse_ipvfuture(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
) {
1821 const WCHAR
*start
= *ptr
;
1823 /* IPvFuture has to start with a 'v' or 'V'. */
1824 if(**ptr
!= 'v' && **ptr
!= 'V')
1827 /* Following the v there must be at least 1 hex digit. */
1829 if(!is_hexdigit(**ptr
)) {
1835 while(is_hexdigit(**ptr
))
1838 /* End of the hexdigit sequence must be a '.' */
1845 if(!is_unreserved(**ptr
) && !is_subdelim(**ptr
) && **ptr
!= ':') {
1851 while(is_unreserved(**ptr
) || is_subdelim(**ptr
) || **ptr
== ':')
1854 data
->host_type
= Uri_HOST_UNKNOWN
;
1856 TRACE("(%p %p %x): Parsed IPvFuture address %s len=%d\n", ptr
, data
, flags
,
1857 debugstr_wn(start
, *ptr
-start
), *ptr
-start
);
1862 /* IP-literal = "[" ( IPv6address / IPvFuture ) "]" */
1863 static BOOL
parse_ip_literal(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
, DWORD extras
) {
1866 if(**ptr
!= '[' && !(extras
& ALLOW_BRACKETLESS_IP_LITERAL
)) {
1869 } else if(**ptr
== '[')
1872 if(!parse_ipv6address(ptr
, data
, flags
)) {
1873 if(extras
& SKIP_IP_FUTURE_CHECK
|| !parse_ipvfuture(ptr
, data
, flags
)) {
1880 if(**ptr
!= ']' && !(extras
& ALLOW_BRACKETLESS_IP_LITERAL
)) {
1884 } else if(!**ptr
&& extras
& ALLOW_BRACKETLESS_IP_LITERAL
) {
1885 /* The IP literal didn't contain brackets and was followed by
1886 * a NULL terminator, so no reason to even check the port.
1888 data
->host_len
= *ptr
- data
->host
;
1895 /* If a valid port is not found, then let it trickle down to
1898 if(!parse_port(ptr
, data
, flags
)) {
1904 data
->host_len
= *ptr
- data
->host
;
1909 /* Parses the host information from the URI.
1911 * host = IP-literal / IPv4address / reg-name
1913 static BOOL
parse_host(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
, DWORD extras
) {
1914 if(!parse_ip_literal(ptr
, data
, flags
, extras
)) {
1915 if(!parse_ipv4address(ptr
, data
, flags
)) {
1916 if(!parse_reg_name(ptr
, data
, flags
, extras
)) {
1917 TRACE("(%p %p %x %x): Malformed URI, Unknown host type.\n",
1918 ptr
, data
, flags
, extras
);
1927 /* Parses the authority information from the URI.
1929 * authority = [ userinfo "@" ] host [ ":" port ]
1931 static BOOL
parse_authority(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
) {
1932 parse_userinfo(ptr
, data
, flags
);
1934 /* Parsing the port will happen during one of the host parsing
1935 * routines (if the URI has a port).
1937 if(!parse_host(ptr
, data
, flags
, 0))
1943 /* Attempts to parse the path information of a hierarchical URI. */
1944 static BOOL
parse_path_hierarchical(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
) {
1945 const WCHAR
*start
= *ptr
;
1946 static const WCHAR slash
[] = {'/',0};
1947 const BOOL is_file
= data
->scheme_type
== URL_SCHEME_FILE
;
1949 if(is_path_delim(**ptr
)) {
1950 if(data
->scheme_type
== URL_SCHEME_WILDCARD
) {
1951 /* Wildcard schemes don't get a '/' attached if their path is
1956 } else if(!(flags
& Uri_CREATE_NO_CANONICALIZE
)) {
1957 /* If the path component is empty, then a '/' is added. */
1962 while(!is_path_delim(**ptr
)) {
1963 if(**ptr
== '%' && data
->scheme_type
!= URL_SCHEME_UNKNOWN
&& !is_file
) {
1964 if(!check_pct_encoded(ptr
)) {
1969 } else if(is_forbidden_dos_path_char(**ptr
) && is_file
&&
1970 (flags
& Uri_CREATE_FILE_USE_DOS_PATH
)) {
1971 /* File schemes with USE_DOS_PATH set aren't allowed to have
1972 * a '<' or '>' or '\"' appear in them.
1976 } else if(**ptr
== '\\') {
1977 /* Not allowed to have a backslash if NO_CANONICALIZE is set
1978 * and the scheme is known type (but not a file scheme).
1980 if(flags
& Uri_CREATE_NO_CANONICALIZE
) {
1981 if(data
->scheme_type
!= URL_SCHEME_FILE
&&
1982 data
->scheme_type
!= URL_SCHEME_UNKNOWN
) {
1992 /* The only time a URI doesn't have a path is when
1993 * the NO_CANONICALIZE flag is set and the raw URI
1994 * didn't contain one.
2001 data
->path_len
= *ptr
- start
;
2006 TRACE("(%p %p %x): Parsed path %s len=%d\n", ptr
, data
, flags
,
2007 debugstr_wn(data
->path
, data
->path_len
), data
->path_len
);
2009 TRACE("(%p %p %x): The URI contained no path\n", ptr
, data
, flags
);
2014 /* Parses the path of a opaque URI (much less strict then the parser
2015 * for a hierarchical URI).
2018 * Windows allows invalid % encoded data to appear in opaque URI paths
2019 * for unknown scheme types.
2021 * File schemes with USE_DOS_PATH set aren't allowed to have '<', '>', or '\"'
2024 static BOOL
parse_path_opaque(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
) {
2025 const BOOL known_scheme
= data
->scheme_type
!= URL_SCHEME_UNKNOWN
;
2026 const BOOL is_file
= data
->scheme_type
== URL_SCHEME_FILE
;
2030 while(!is_path_delim(**ptr
)) {
2031 if(**ptr
== '%' && known_scheme
) {
2032 if(!check_pct_encoded(ptr
)) {
2038 } else if(is_forbidden_dos_path_char(**ptr
) && is_file
&&
2039 (flags
& Uri_CREATE_FILE_USE_DOS_PATH
)) {
2048 data
->path_len
= *ptr
- data
->path
;
2049 TRACE("(%p %p %x): Parsed opaque URI path %s len=%d\n", ptr
, data
, flags
,
2050 debugstr_wn(data
->path
, data
->path_len
), data
->path_len
);
2054 /* Determines how the URI should be parsed after the scheme information.
2056 * If the scheme is followed, by "//" then, it is treated as an hierarchical URI
2057 * which then the authority and path information will be parsed out. Otherwise, the
2058 * URI will be treated as an opaque URI which the authority information is not parsed
2061 * RFC 3896 definition of hier-part:
2063 * hier-part = "//" authority path-abempty
2068 * MSDN opaque URI definition:
2069 * scheme ":" path [ "#" fragment ]
2072 * If the URI is of an unknown scheme type and has a "//" following the scheme then it
2073 * is treated as a hierarchical URI, but, if the CREATE_NO_CRACK_UNKNOWN_SCHEMES flag is
2074 * set then it is considered an opaque URI reguardless of what follows the scheme information
2075 * (per MSDN documentation).
2077 static BOOL
parse_hierpart(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
) {
2078 const WCHAR
*start
= *ptr
;
2080 /* Checks if the authority information needs to be parsed. */
2081 if(is_hierarchical_uri(ptr
, data
)) {
2082 /* Only treat it as a hierarchical URI if the scheme_type is known or
2083 * the Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES flag is not set.
2085 if(data
->scheme_type
!= URL_SCHEME_UNKNOWN
||
2086 !(flags
& Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES
)) {
2087 TRACE("(%p %p %x): Treating URI as an hierarchical URI.\n", ptr
, data
, flags
);
2088 data
->is_opaque
= FALSE
;
2090 /* TODO: Handle hierarchical URI's, parse authority then parse the path. */
2091 if(!parse_authority(ptr
, data
, flags
))
2094 return parse_path_hierarchical(ptr
, data
, flags
);
2096 /* Reset ptr to it's starting position so opaque path parsing
2097 * begins at the correct location.
2102 /* If it reaches here, then the URI will be treated as an opaque
2106 TRACE("(%p %p %x): Treating URI as an opaque URI.\n", ptr
, data
, flags
);
2108 data
->is_opaque
= TRUE
;
2109 if(!parse_path_opaque(ptr
, data
, flags
))
2115 /* Attempts to parse the query string from the URI.
2118 * If NO_DECODE_EXTRA_INFO flag is set, then invalid percent encoded
2119 * data is allowed appear in the query string. For unknown scheme types
2120 * invalid percent encoded data is allowed to appear reguardless.
2122 static BOOL
parse_query(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
) {
2123 const BOOL known_scheme
= data
->scheme_type
!= URL_SCHEME_UNKNOWN
;
2126 TRACE("(%p %p %x): URI didn't contain a query string.\n", ptr
, data
, flags
);
2133 while(**ptr
&& **ptr
!= '#') {
2134 if(**ptr
== '%' && known_scheme
&&
2135 !(flags
& Uri_CREATE_NO_DECODE_EXTRA_INFO
)) {
2136 if(!check_pct_encoded(ptr
)) {
2147 data
->query_len
= *ptr
- data
->query
;
2149 TRACE("(%p %p %x): Parsed query string %s len=%d\n", ptr
, data
, flags
,
2150 debugstr_wn(data
->query
, data
->query_len
), data
->query_len
);
2154 /* Attempts to parse the fragment from the URI.
2157 * If NO_DECODE_EXTRA_INFO flag is set, then invalid percent encoded
2158 * data is allowed appear in the query string. For unknown scheme types
2159 * invalid percent encoded data is allowed to appear reguardless.
2161 static BOOL
parse_fragment(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
) {
2162 const BOOL known_scheme
= data
->scheme_type
!= URL_SCHEME_UNKNOWN
;
2165 TRACE("(%p %p %x): URI didn't contain a fragment.\n", ptr
, data
, flags
);
2169 data
->fragment
= *ptr
;
2173 if(**ptr
== '%' && known_scheme
&&
2174 !(flags
& Uri_CREATE_NO_DECODE_EXTRA_INFO
)) {
2175 if(!check_pct_encoded(ptr
)) {
2176 *ptr
= data
->fragment
;
2177 data
->fragment
= NULL
;
2186 data
->fragment_len
= *ptr
- data
->fragment
;
2188 TRACE("(%p %p %x): Parsed fragment %s len=%d\n", ptr
, data
, flags
,
2189 debugstr_wn(data
->fragment
, data
->fragment_len
), data
->fragment_len
);
2193 /* Parses and validates the components of the specified by data->uri
2194 * and stores the information it parses into 'data'.
2196 * Returns TRUE if it successfully parsed the URI. False otherwise.
2198 static BOOL
parse_uri(parse_data
*data
, DWORD flags
) {
2205 TRACE("(%p %x): BEGINNING TO PARSE URI %s.\n", data
, flags
, debugstr_w(data
->uri
));
2207 if(!parse_scheme(pptr
, data
, flags
, 0))
2210 if(!parse_hierpart(pptr
, data
, flags
))
2213 if(!parse_query(pptr
, data
, flags
))
2216 if(!parse_fragment(pptr
, data
, flags
))
2219 TRACE("(%p %x): FINISHED PARSING URI.\n", data
, flags
);
2223 static BOOL
canonicalize_username(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
2226 if(!data
->username
) {
2227 uri
->userinfo_start
= -1;
2231 uri
->userinfo_start
= uri
->canon_len
;
2232 for(ptr
= data
->username
; ptr
< data
->username
+data
->username_len
; ++ptr
) {
2234 /* Only decode % encoded values for known scheme types. */
2235 if(data
->scheme_type
!= URL_SCHEME_UNKNOWN
) {
2236 /* See if the value really needs decoded. */
2237 WCHAR val
= decode_pct_val(ptr
);
2238 if(is_unreserved(val
)) {
2240 uri
->canon_uri
[uri
->canon_len
] = val
;
2244 /* Move pass the hex characters. */
2249 } else if(!is_reserved(*ptr
) && !is_unreserved(*ptr
) && *ptr
!= '\\') {
2250 /* Only percent encode forbidden characters if the NO_ENCODE_FORBIDDEN_CHARACTERS flag
2253 if(!(flags
& Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS
)) {
2255 pct_encode_val(*ptr
, uri
->canon_uri
+ uri
->canon_len
);
2257 uri
->canon_len
+= 3;
2263 /* Nothing special, so just copy the character over. */
2264 uri
->canon_uri
[uri
->canon_len
] = *ptr
;
2271 static BOOL
canonicalize_password(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
2274 if(!data
->password
) {
2275 uri
->userinfo_split
= -1;
2279 if(uri
->userinfo_start
== -1)
2280 /* Has a password, but, doesn't have a username. */
2281 uri
->userinfo_start
= uri
->canon_len
;
2283 uri
->userinfo_split
= uri
->canon_len
- uri
->userinfo_start
;
2285 /* Add the ':' to the userinfo component. */
2287 uri
->canon_uri
[uri
->canon_len
] = ':';
2290 for(ptr
= data
->password
; ptr
< data
->password
+data
->password_len
; ++ptr
) {
2292 /* Only decode % encoded values for known scheme types. */
2293 if(data
->scheme_type
!= URL_SCHEME_UNKNOWN
) {
2294 /* See if the value really needs decoded. */
2295 WCHAR val
= decode_pct_val(ptr
);
2296 if(is_unreserved(val
)) {
2298 uri
->canon_uri
[uri
->canon_len
] = val
;
2302 /* Move pass the hex characters. */
2307 } else if(!is_reserved(*ptr
) && !is_unreserved(*ptr
) && *ptr
!= '\\') {
2308 /* Only percent encode forbidden characters if the NO_ENCODE_FORBIDDEN_CHARACTERS flag
2311 if(!(flags
& Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS
)) {
2313 pct_encode_val(*ptr
, uri
->canon_uri
+ uri
->canon_len
);
2315 uri
->canon_len
+= 3;
2321 /* Nothing special, so just copy the character over. */
2322 uri
->canon_uri
[uri
->canon_len
] = *ptr
;
2329 /* Canonicalizes the userinfo of the URI represented by the parse_data.
2331 * Canonicalization of the userinfo is a simple process. If there are any percent
2332 * encoded characters that fall in the "unreserved" character set, they are decoded
2333 * to their actual value. If a character is not in the "unreserved" or "reserved" sets
2334 * then it is percent encoded. Other than that the characters are copied over without
2337 static BOOL
canonicalize_userinfo(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
2338 uri
->userinfo_start
= uri
->userinfo_split
= -1;
2339 uri
->userinfo_len
= 0;
2341 if(!data
->username
&& !data
->password
)
2342 /* URI doesn't have userinfo, so nothing to do here. */
2345 if(!canonicalize_username(data
, uri
, flags
, computeOnly
))
2348 if(!canonicalize_password(data
, uri
, flags
, computeOnly
))
2351 uri
->userinfo_len
= uri
->canon_len
- uri
->userinfo_start
;
2353 TRACE("(%p %p %x %d): Canonicalized userinfo, userinfo_start=%d, userinfo=%s, userinfo_split=%d userinfo_len=%d.\n",
2354 data
, uri
, flags
, computeOnly
, uri
->userinfo_start
, debugstr_wn(uri
->canon_uri
+ uri
->userinfo_start
, uri
->userinfo_len
),
2355 uri
->userinfo_split
, uri
->userinfo_len
);
2357 /* Now insert the '@' after the userinfo. */
2359 uri
->canon_uri
[uri
->canon_len
] = '@';
2365 /* Attempts to canonicalize a reg_name.
2367 * Things that happen:
2368 * 1) If Uri_CREATE_NO_CANONICALIZE flag is not set, then the reg_name is
2369 * lower cased. Unless it's an unknown scheme type, which case it's
2370 * no lower cased reguardless.
2372 * 2) Unreserved % encoded characters are decoded for known
2375 * 3) Forbidden characters are % encoded as long as
2376 * Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS flag is not set and
2377 * it isn't an unknown scheme type.
2379 * 4) If it's a file scheme and the host is "localhost" it's removed.
2381 * 5) If it's a file scheme and Uri_CREATE_FILE_USE_DOS_PATH is set,
2382 * then the UNC path characters are added before the host name.
2384 static BOOL
canonicalize_reg_name(const parse_data
*data
, Uri
*uri
,
2385 DWORD flags
, BOOL computeOnly
) {
2386 static const WCHAR localhostW
[] =
2387 {'l','o','c','a','l','h','o','s','t',0};
2389 const BOOL known_scheme
= data
->scheme_type
!= URL_SCHEME_UNKNOWN
;
2391 if(data
->scheme_type
== URL_SCHEME_FILE
&&
2392 data
->host_len
== lstrlenW(localhostW
)) {
2393 if(!StrCmpNIW(data
->host
, localhostW
, data
->host_len
)) {
2394 uri
->host_start
= -1;
2396 uri
->host_type
= Uri_HOST_UNKNOWN
;
2401 if(data
->scheme_type
== URL_SCHEME_FILE
&& flags
& Uri_CREATE_FILE_USE_DOS_PATH
) {
2403 uri
->canon_uri
[uri
->canon_len
] = '\\';
2404 uri
->canon_uri
[uri
->canon_len
+1] = '\\';
2406 uri
->canon_len
+= 2;
2407 uri
->authority_start
= uri
->canon_len
;
2410 uri
->host_start
= uri
->canon_len
;
2412 for(ptr
= data
->host
; ptr
< data
->host
+data
->host_len
; ++ptr
) {
2413 if(*ptr
== '%' && known_scheme
) {
2414 WCHAR val
= decode_pct_val(ptr
);
2415 if(is_unreserved(val
)) {
2416 /* If NO_CANONICALZE is not set, then windows lower cases the
2419 if(!(flags
& Uri_CREATE_NO_CANONICALIZE
) && isupperW(val
)) {
2421 uri
->canon_uri
[uri
->canon_len
] = tolowerW(val
);
2424 uri
->canon_uri
[uri
->canon_len
] = val
;
2428 /* Skip past the % encoded character. */
2432 /* Just copy the % over. */
2434 uri
->canon_uri
[uri
->canon_len
] = *ptr
;
2437 } else if(*ptr
== '\\') {
2438 /* Only unknown scheme types could have made it here with a '\\' in the host name. */
2440 uri
->canon_uri
[uri
->canon_len
] = *ptr
;
2442 } else if(!(flags
& Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS
) &&
2443 !is_unreserved(*ptr
) && !is_reserved(*ptr
) && known_scheme
) {
2445 pct_encode_val(*ptr
, uri
->canon_uri
+uri
->canon_len
);
2447 /* The percent encoded value gets lower cased also. */
2448 if(!(flags
& Uri_CREATE_NO_CANONICALIZE
)) {
2449 uri
->canon_uri
[uri
->canon_len
+1] = tolowerW(uri
->canon_uri
[uri
->canon_len
+1]);
2450 uri
->canon_uri
[uri
->canon_len
+2] = tolowerW(uri
->canon_uri
[uri
->canon_len
+2]);
2454 uri
->canon_len
+= 3;
2457 if(!(flags
& Uri_CREATE_NO_CANONICALIZE
) && known_scheme
)
2458 uri
->canon_uri
[uri
->canon_len
] = tolowerW(*ptr
);
2460 uri
->canon_uri
[uri
->canon_len
] = *ptr
;
2467 uri
->host_len
= uri
->canon_len
- uri
->host_start
;
2470 TRACE("(%p %p %x %d): Canonicalize reg_name=%s len=%d\n", data
, uri
, flags
,
2471 computeOnly
, debugstr_wn(uri
->canon_uri
+uri
->host_start
, uri
->host_len
),
2475 find_domain_name(uri
->canon_uri
+uri
->host_start
, uri
->host_len
,
2476 &(uri
->domain_offset
));
2481 /* Attempts to canonicalize an implicit IPv4 address. */
2482 static BOOL
canonicalize_implicit_ipv4address(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
2483 uri
->host_start
= uri
->canon_len
;
2485 TRACE("%u\n", data
->implicit_ipv4
);
2486 /* For unknown scheme types Window's doesn't convert
2487 * the value into an IP address, but, it still considers
2488 * it an IPv4 address.
2490 if(data
->scheme_type
== URL_SCHEME_UNKNOWN
) {
2492 memcpy(uri
->canon_uri
+uri
->canon_len
, data
->host
, data
->host_len
*sizeof(WCHAR
));
2493 uri
->canon_len
+= data
->host_len
;
2496 uri
->canon_len
+= ui2ipv4(uri
->canon_uri
+uri
->canon_len
, data
->implicit_ipv4
);
2498 uri
->canon_len
+= ui2ipv4(NULL
, data
->implicit_ipv4
);
2501 uri
->host_len
= uri
->canon_len
- uri
->host_start
;
2502 uri
->host_type
= Uri_HOST_IPV4
;
2505 TRACE("%p %p %x %d): Canonicalized implicit IP address=%s len=%d\n",
2506 data
, uri
, flags
, computeOnly
,
2507 debugstr_wn(uri
->canon_uri
+uri
->host_start
, uri
->host_len
),
2513 /* Attempts to canonicalize an IPv4 address.
2515 * If the parse_data represents a URI that has an implicit IPv4 address
2516 * (ex. http://256/, this function will convert 256 into 0.0.1.0). If
2517 * the implicit IP address exceeds the value of UINT_MAX (maximum value
2518 * for an IPv4 address) it's canonicalized as if were a reg-name.
2520 * If the parse_data contains a partial or full IPv4 address it normalizes it.
2521 * A partial IPv4 address is something like "192.0" and would be normalized to
2522 * "192.0.0.0". With a full (or partial) IPv4 address like "192.002.01.003" would
2523 * be normalized to "192.2.1.3".
2526 * Window's ONLY normalizes IPv4 address for known scheme types (one that isn't
2527 * URL_SCHEME_UNKNOWN). For unknown scheme types, it simply copies the data from
2528 * the original URI into the canonicalized URI, but, it still recognizes URI's
2529 * host type as HOST_IPV4.
2531 static BOOL
canonicalize_ipv4address(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
2532 if(data
->has_implicit_ip
)
2533 return canonicalize_implicit_ipv4address(data
, uri
, flags
, computeOnly
);
2535 uri
->host_start
= uri
->canon_len
;
2537 /* Windows only normalizes for known scheme types. */
2538 if(data
->scheme_type
!= URL_SCHEME_UNKNOWN
) {
2539 /* parse_data contains a partial or full IPv4 address, so normalize it. */
2540 DWORD i
, octetDigitCount
= 0, octetCount
= 0;
2541 BOOL octetHasDigit
= FALSE
;
2543 for(i
= 0; i
< data
->host_len
; ++i
) {
2544 if(data
->host
[i
] == '0' && !octetHasDigit
) {
2545 /* Can ignore leading zeros if:
2546 * 1) It isn't the last digit of the octet.
2547 * 2) i+1 != data->host_len
2550 if(octetDigitCount
== 2 ||
2551 i
+1 == data
->host_len
||
2552 data
->host
[i
+1] == '.') {
2554 uri
->canon_uri
[uri
->canon_len
] = data
->host
[i
];
2556 TRACE("Adding zero\n");
2558 } else if(data
->host
[i
] == '.') {
2560 uri
->canon_uri
[uri
->canon_len
] = data
->host
[i
];
2563 octetDigitCount
= 0;
2564 octetHasDigit
= FALSE
;
2568 uri
->canon_uri
[uri
->canon_len
] = data
->host
[i
];
2572 octetHasDigit
= TRUE
;
2576 /* Make sure the canonicalized IP address has 4 dec-octets.
2577 * If doesn't add "0" ones until there is 4;
2579 for( ; octetCount
< 3; ++octetCount
) {
2581 uri
->canon_uri
[uri
->canon_len
] = '.';
2582 uri
->canon_uri
[uri
->canon_len
+1] = '0';
2585 uri
->canon_len
+= 2;
2588 /* Windows doesn't normalize addresses in unknown schemes. */
2590 memcpy(uri
->canon_uri
+uri
->canon_len
, data
->host
, data
->host_len
*sizeof(WCHAR
));
2591 uri
->canon_len
+= data
->host_len
;
2594 uri
->host_len
= uri
->canon_len
- uri
->host_start
;
2596 TRACE("(%p %p %x %d): Canonicalized IPv4 address, ip=%s len=%d\n",
2597 data
, uri
, flags
, computeOnly
,
2598 debugstr_wn(uri
->canon_uri
+uri
->host_start
, uri
->host_len
),
2605 /* Attempts to canonicalize the IPv6 address of the URI.
2607 * Multiple things happen during the canonicalization of an IPv6 address:
2608 * 1) Any leading zero's in an h16 component are removed.
2609 * Ex: [0001:0022::] -> [1:22::]
2611 * 2) The longest sequence of zero h16 components are compressed
2612 * into a "::" (elision). If there's a tie, the first is choosen.
2614 * Ex: [0:0:0:0:1:6:7:8] -> [::1:6:7:8]
2615 * [0:0:0:0:1:2::] -> [::1:2:0:0]
2616 * [0:0:1:2:0:0:7:8] -> [::1:2:0:0:7:8]
2618 * 3) If an IPv4 address is attached to the IPv6 address, it's
2620 * Ex: [::001.002.022.000] -> [::1.2.22.0]
2622 * 4) If an elision is present, but, only represents 1 h16 component
2625 * Ex: [1::2:3:4:5:6:7] -> [1:0:2:3:4:5:6:7]
2627 * 5) If the IPv6 address contains an IPv4 address and there exists
2628 * at least 1 non-zero h16 component the IPv4 address is converted
2629 * into two h16 components, otherwise it's normalized and kept as is.
2631 * Ex: [::192.200.003.4] -> [::192.200.3.4]
2632 * [ffff::192.200.003.4] -> [ffff::c0c8:3041]
2635 * For unknown scheme types Windows simply copies the address over without any
2638 * IPv4 address can be included in an elision if all its components are 0's.
2640 static BOOL
canonicalize_ipv6address(const parse_data
*data
, Uri
*uri
,
2641 DWORD flags
, BOOL computeOnly
) {
2642 uri
->host_start
= uri
->canon_len
;
2644 if(data
->scheme_type
== URL_SCHEME_UNKNOWN
) {
2646 memcpy(uri
->canon_uri
+uri
->canon_len
, data
->host
, data
->host_len
*sizeof(WCHAR
));
2647 uri
->canon_len
+= data
->host_len
;
2651 DWORD i
, elision_len
;
2653 if(!ipv6_to_number(&(data
->ipv6_address
), values
)) {
2654 TRACE("(%p %p %x %d): Failed to compute numerical value for IPv6 address.\n",
2655 data
, uri
, flags
, computeOnly
);
2660 uri
->canon_uri
[uri
->canon_len
] = '[';
2663 /* Find where the elision should occur (if any). */
2664 compute_elision_location(&(data
->ipv6_address
), values
, &elision_start
, &elision_len
);
2666 TRACE("%p %p %x %d): Elision starts at %d, len=%u\n", data
, uri
, flags
,
2667 computeOnly
, elision_start
, elision_len
);
2669 for(i
= 0; i
< 8; ++i
) {
2670 BOOL in_elision
= (elision_start
> -1 && i
>= elision_start
&&
2671 i
< elision_start
+elision_len
);
2672 BOOL do_ipv4
= (i
== 6 && data
->ipv6_address
.ipv4
&& !in_elision
&&
2673 data
->ipv6_address
.h16_count
== 0);
2675 if(i
== elision_start
) {
2677 uri
->canon_uri
[uri
->canon_len
] = ':';
2678 uri
->canon_uri
[uri
->canon_len
+1] = ':';
2680 uri
->canon_len
+= 2;
2683 /* We can ignore the current component if we're in the elision. */
2687 /* We only add a ':' if we're not at i == 0, or when we're at
2688 * the very end of elision range since the ':' colon was handled
2689 * earlier. Otherwise we would end up with ":::" after elision.
2691 if(i
!= 0 && !(elision_start
> -1 && i
== elision_start
+elision_len
)) {
2693 uri
->canon_uri
[uri
->canon_len
] = ':';
2701 /* Combine the two parts of the IPv4 address values. */
2707 len
= ui2ipv4(uri
->canon_uri
+uri
->canon_len
, val
);
2709 len
= ui2ipv4(NULL
, val
);
2711 uri
->canon_len
+= len
;
2714 /* Write a regular h16 component to the URI. */
2716 /* Short circuit for the trivial case. */
2717 if(values
[i
] == 0) {
2719 uri
->canon_uri
[uri
->canon_len
] = '0';
2722 static const WCHAR formatW
[] = {'%','x',0};
2725 uri
->canon_len
+= sprintfW(uri
->canon_uri
+uri
->canon_len
,
2726 formatW
, values
[i
]);
2729 uri
->canon_len
+= sprintfW(tmp
, formatW
, values
[i
]);
2735 /* Add the closing ']'. */
2737 uri
->canon_uri
[uri
->canon_len
] = ']';
2741 uri
->host_len
= uri
->canon_len
- uri
->host_start
;
2744 TRACE("(%p %p %x %d): Canonicalized IPv6 address %s, len=%d\n", data
, uri
, flags
,
2745 computeOnly
, debugstr_wn(uri
->canon_uri
+uri
->host_start
, uri
->host_len
),
2751 /* Attempts to canonicalize the host of the URI (if any). */
2752 static BOOL
canonicalize_host(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
2753 uri
->host_start
= -1;
2755 uri
->domain_offset
= -1;
2758 switch(data
->host_type
) {
2760 uri
->host_type
= Uri_HOST_DNS
;
2761 if(!canonicalize_reg_name(data
, uri
, flags
, computeOnly
))
2766 uri
->host_type
= Uri_HOST_IPV4
;
2767 if(!canonicalize_ipv4address(data
, uri
, flags
, computeOnly
))
2772 if(!canonicalize_ipv6address(data
, uri
, flags
, computeOnly
))
2775 uri
->host_type
= Uri_HOST_IPV6
;
2777 case Uri_HOST_UNKNOWN
:
2778 if(data
->host_len
> 0 || data
->scheme_type
!= URL_SCHEME_FILE
) {
2779 uri
->host_start
= uri
->canon_len
;
2781 /* Nothing happens to unknown host types. */
2783 memcpy(uri
->canon_uri
+uri
->canon_len
, data
->host
, data
->host_len
*sizeof(WCHAR
));
2784 uri
->canon_len
+= data
->host_len
;
2785 uri
->host_len
= data
->host_len
;
2788 uri
->host_type
= Uri_HOST_UNKNOWN
;
2791 FIXME("(%p %p %x %d): Canonicalization for host type %d not supported.\n", data
,
2792 uri
, flags
, computeOnly
, data
->host_type
);
2800 static BOOL
canonicalize_port(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
2801 BOOL has_default_port
= FALSE
;
2802 USHORT default_port
= 0;
2805 uri
->port_offset
= -1;
2807 /* Check if the scheme has a default port. */
2808 for(i
= 0; i
< sizeof(default_ports
)/sizeof(default_ports
[0]); ++i
) {
2809 if(default_ports
[i
].scheme
== data
->scheme_type
) {
2810 has_default_port
= TRUE
;
2811 default_port
= default_ports
[i
].port
;
2816 uri
->has_port
= data
->has_port
|| has_default_port
;
2819 * 1) Has a port which is the default port.
2820 * 2) Has a port (not the default).
2821 * 3) Doesn't have a port, but, scheme has a default port.
2824 if(has_default_port
&& data
->has_port
&& data
->port_value
== default_port
) {
2825 /* If it's the default port and this flag isn't set, don't do anything. */
2826 if(flags
& Uri_CREATE_NO_CANONICALIZE
) {
2827 uri
->port_offset
= uri
->canon_len
-uri
->authority_start
;
2829 uri
->canon_uri
[uri
->canon_len
] = ':';
2833 /* Copy the original port over. */
2835 memcpy(uri
->canon_uri
+uri
->canon_len
, data
->port
, data
->port_len
*sizeof(WCHAR
));
2836 uri
->canon_len
+= data
->port_len
;
2839 uri
->canon_len
+= ui2str(uri
->canon_uri
+uri
->canon_len
, data
->port_value
);
2841 uri
->canon_len
+= ui2str(NULL
, data
->port_value
);
2845 uri
->port
= default_port
;
2846 } else if(data
->has_port
) {
2847 uri
->port_offset
= uri
->canon_len
-uri
->authority_start
;
2849 uri
->canon_uri
[uri
->canon_len
] = ':';
2852 if(flags
& Uri_CREATE_NO_CANONICALIZE
&& data
->port
) {
2853 /* Copy the original over without changes. */
2855 memcpy(uri
->canon_uri
+uri
->canon_len
, data
->port
, data
->port_len
*sizeof(WCHAR
));
2856 uri
->canon_len
+= data
->port_len
;
2859 uri
->canon_len
+= ui2str(uri
->canon_uri
+uri
->canon_len
, data
->port_value
);
2861 uri
->canon_len
+= ui2str(NULL
, data
->port_value
);
2864 uri
->port
= data
->port_value
;
2865 } else if(has_default_port
)
2866 uri
->port
= default_port
;
2871 /* Canonicalizes the authority of the URI represented by the parse_data. */
2872 static BOOL
canonicalize_authority(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
2873 uri
->authority_start
= uri
->canon_len
;
2874 uri
->authority_len
= 0;
2876 if(!canonicalize_userinfo(data
, uri
, flags
, computeOnly
))
2879 if(!canonicalize_host(data
, uri
, flags
, computeOnly
))
2882 if(!canonicalize_port(data
, uri
, flags
, computeOnly
))
2885 if(uri
->host_start
!= -1 || (data
->is_relative
&& (data
->password
|| data
->username
)))
2886 uri
->authority_len
= uri
->canon_len
- uri
->authority_start
;
2888 uri
->authority_start
= -1;
2893 /* Attempts to canonicalize the path of a hierarchical URI.
2895 * Things that happen:
2896 * 1). Forbidden characters are percent encoded, unless the NO_ENCODE_FORBIDDEN
2897 * flag is set or it's a file URI. Forbidden characters are always encoded
2898 * for file schemes reguardless and forbidden characters are never encoded
2899 * for unknown scheme types.
2901 * 2). For known scheme types '\\' are changed to '/'.
2903 * 3). Percent encoded, unreserved characters are decoded to their actual values.
2904 * Unless the scheme type is unknown. For file schemes any percent encoded
2905 * character in the unreserved or reserved set is decoded.
2907 * 4). For File schemes if the path is starts with a drive letter and doesn't
2908 * start with a '/' then one is appended.
2909 * Ex: file://c:/test.mp3 -> file:///c:/test.mp3
2911 * 5). Dot segments are removed from the path for all scheme types
2912 * unless NO_CANONICALIZE flag is set. Dot segments aren't removed
2913 * for wildcard scheme types.
2916 * file://c:/test%20test -> file:///c:/test%2520test
2917 * file://c:/test%3Etest -> file:///c:/test%253Etest
2918 * if Uri_CREATE_FILE_USE_DOS_PATH is not set:
2919 * file:///c:/test%20test -> file:///c:/test%20test
2920 * file:///c:/test%test -> file:///c:/test%25test
2922 static BOOL
canonicalize_path_hierarchical(const parse_data
*data
, Uri
*uri
,
2923 DWORD flags
, BOOL computeOnly
) {
2925 const BOOL known_scheme
= data
->scheme_type
!= URL_SCHEME_UNKNOWN
;
2926 const BOOL is_file
= data
->scheme_type
== URL_SCHEME_FILE
;
2927 const BOOL is_res
= data
->scheme_type
== URL_SCHEME_RES
;
2929 BOOL escape_pct
= FALSE
;
2932 uri
->path_start
= -1;
2937 uri
->path_start
= uri
->canon_len
;
2940 if(is_file
&& uri
->host_start
== -1) {
2941 /* Check if a '/' needs to be appended for the file scheme. */
2942 if(data
->path_len
> 1 && is_drive_path(ptr
) && !(flags
& Uri_CREATE_FILE_USE_DOS_PATH
)) {
2944 uri
->canon_uri
[uri
->canon_len
] = '/';
2947 } else if(*ptr
== '/') {
2948 if(!(flags
& Uri_CREATE_FILE_USE_DOS_PATH
)) {
2949 /* Copy the extra '/' over. */
2951 uri
->canon_uri
[uri
->canon_len
] = '/';
2957 if(is_drive_path(ptr
)) {
2959 uri
->canon_uri
[uri
->canon_len
] = *ptr
;
2960 /* If theres a '|' after the drive letter, convert it to a ':'. */
2961 uri
->canon_uri
[uri
->canon_len
+1] = ':';
2964 uri
->canon_len
+= 2;
2968 if(!is_file
&& *(data
->path
) && *(data
->path
) != '/') {
2969 /* Prepend a '/' to the path if it doesn't have one. */
2971 uri
->canon_uri
[uri
->canon_len
] = '/';
2975 for(; ptr
< data
->path
+data
->path_len
; ++ptr
) {
2976 BOOL do_default_action
= TRUE
;
2978 if(*ptr
== '%' && !is_res
) {
2979 const WCHAR
*tmp
= ptr
;
2982 /* Check if the % represents a valid encoded char, or if it needs encoded. */
2983 BOOL force_encode
= !check_pct_encoded(&tmp
) && is_file
&& !(flags
&Uri_CREATE_FILE_USE_DOS_PATH
);
2984 val
= decode_pct_val(ptr
);
2986 if(force_encode
|| escape_pct
) {
2987 /* Escape the percent sign in the file URI. */
2989 pct_encode_val(*ptr
, uri
->canon_uri
+uri
->canon_len
);
2990 uri
->canon_len
+= 3;
2991 do_default_action
= FALSE
;
2992 } else if((is_unreserved(val
) && known_scheme
) ||
2993 (is_file
&& (is_unreserved(val
) || is_reserved(val
) ||
2994 (val
&& flags
&Uri_CREATE_FILE_USE_DOS_PATH
&& !is_forbidden_dos_path_char(val
))))) {
2996 uri
->canon_uri
[uri
->canon_len
] = val
;
3002 } else if(*ptr
== '/' && is_file
&& (flags
& Uri_CREATE_FILE_USE_DOS_PATH
)) {
3003 /* Convert the '/' back to a '\\'. */
3005 uri
->canon_uri
[uri
->canon_len
] = '\\';
3007 do_default_action
= FALSE
;
3008 } else if(*ptr
== '\\' && known_scheme
) {
3009 if(!(is_file
&& (flags
& Uri_CREATE_FILE_USE_DOS_PATH
))) {
3010 /* Convert '\\' into a '/'. */
3012 uri
->canon_uri
[uri
->canon_len
] = '/';
3014 do_default_action
= FALSE
;
3016 } else if(known_scheme
&& !is_res
&& !is_unreserved(*ptr
) && !is_reserved(*ptr
) &&
3017 (!(flags
& Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS
) || is_file
)) {
3018 if(!(is_file
&& (flags
& Uri_CREATE_FILE_USE_DOS_PATH
))) {
3019 /* Escape the forbidden character. */
3021 pct_encode_val(*ptr
, uri
->canon_uri
+uri
->canon_len
);
3022 uri
->canon_len
+= 3;
3023 do_default_action
= FALSE
;
3027 if(do_default_action
) {
3029 uri
->canon_uri
[uri
->canon_len
] = *ptr
;
3034 uri
->path_len
= uri
->canon_len
- uri
->path_start
;
3036 /* Removing the dot segments only happens when it's not in
3037 * computeOnly mode and it's not a wildcard scheme. File schemes
3038 * with USE_DOS_PATH set don't get dot segments removed.
3040 if(!(is_file
&& (flags
& Uri_CREATE_FILE_USE_DOS_PATH
)) &&
3041 data
->scheme_type
!= URL_SCHEME_WILDCARD
) {
3042 if(!(flags
& Uri_CREATE_NO_CANONICALIZE
) && !computeOnly
) {
3043 /* Remove the dot segments (if any) and reset everything to the new
3046 DWORD new_len
= remove_dot_segments(uri
->canon_uri
+uri
->path_start
, uri
->path_len
);
3047 uri
->canon_len
-= uri
->path_len
-new_len
;
3048 uri
->path_len
= new_len
;
3053 TRACE("Canonicalized path %s len=%d\n",
3054 debugstr_wn(uri
->canon_uri
+uri
->path_start
, uri
->path_len
),
3060 /* Attempts to canonicalize the path for an opaque URI.
3062 * For known scheme types:
3063 * 1) forbidden characters are percent encoded if
3064 * NO_ENCODE_FORBIDDEN_CHARACTERS isn't set.
3066 * 2) Percent encoded, unreserved characters are decoded
3067 * to their actual values, for known scheme types.
3069 * 3) '\\' are changed to '/' for known scheme types
3070 * except for mailto schemes.
3072 * 4) For file schemes, if USE_DOS_PATH is set all '/'
3073 * are converted to backslashes.
3075 * 5) For file schemes, if USE_DOS_PATH isn't set all '\'
3076 * are converted to forward slashes.
3078 static BOOL
canonicalize_path_opaque(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
3080 const BOOL known_scheme
= data
->scheme_type
!= URL_SCHEME_UNKNOWN
;
3081 const BOOL is_file
= data
->scheme_type
== URL_SCHEME_FILE
;
3084 uri
->path_start
= -1;
3089 uri
->path_start
= uri
->canon_len
;
3091 /* Windows doesn't allow a "//" to appear after the scheme
3092 * of a URI, if it's an opaque URI.
3094 if(data
->scheme
&& *(data
->path
) == '/' && *(data
->path
+1) == '/') {
3095 /* So it inserts a "/." before the "//" if it exists. */
3097 uri
->canon_uri
[uri
->canon_len
] = '/';
3098 uri
->canon_uri
[uri
->canon_len
+1] = '.';
3101 uri
->canon_len
+= 2;
3104 for(ptr
= data
->path
; ptr
< data
->path
+data
->path_len
; ++ptr
) {
3105 BOOL do_default_action
= TRUE
;
3107 if(*ptr
== '%' && known_scheme
) {
3108 WCHAR val
= decode_pct_val(ptr
);
3110 if(is_unreserved(val
)) {
3112 uri
->canon_uri
[uri
->canon_len
] = val
;
3118 } else if(*ptr
== '/' && is_file
&& (flags
& Uri_CREATE_FILE_USE_DOS_PATH
)) {
3120 uri
->canon_uri
[uri
->canon_len
] = '\\';
3122 do_default_action
= FALSE
;
3123 } else if(*ptr
== '\\') {
3124 if(is_file
&& !(flags
& Uri_CREATE_FILE_USE_DOS_PATH
)) {
3125 /* Convert to a '/'. */
3127 uri
->canon_uri
[uri
->canon_len
] = '/';
3129 do_default_action
= FALSE
;
3131 } else if(known_scheme
&& !is_unreserved(*ptr
) && !is_reserved(*ptr
) &&
3132 !(flags
& Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS
)) {
3133 if(!(is_file
&& (flags
& Uri_CREATE_FILE_USE_DOS_PATH
))) {
3135 pct_encode_val(*ptr
, uri
->canon_uri
+uri
->canon_len
);
3136 uri
->canon_len
+= 3;
3137 do_default_action
= FALSE
;
3141 if(do_default_action
) {
3143 uri
->canon_uri
[uri
->canon_len
] = *ptr
;
3148 uri
->path_len
= uri
->canon_len
- uri
->path_start
;
3150 TRACE("(%p %p %x %d): Canonicalized opaque URI path %s len=%d\n", data
, uri
, flags
, computeOnly
,
3151 debugstr_wn(uri
->canon_uri
+uri
->path_start
, uri
->path_len
), uri
->path_len
);
3155 /* Determines how the URI represented by the parse_data should be canonicalized.
3157 * Essentially, if the parse_data represents an hierarchical URI then it calls
3158 * canonicalize_authority and the canonicalization functions for the path. If the
3159 * URI is opaque it canonicalizes the path of the URI.
3161 static BOOL
canonicalize_hierpart(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
3162 if(!data
->is_opaque
|| (data
->is_relative
&& (data
->password
|| data
->username
))) {
3163 /* "//" is only added for non-wildcard scheme types.
3165 * A "//" is only added to a relative URI if it has a
3166 * host or port component (this only happens if a IUriBuilder
3167 * is generating an IUri).
3169 if((data
->is_relative
&& (data
->host
|| data
->has_port
)) ||
3170 (!data
->is_relative
&& data
->scheme_type
!= URL_SCHEME_WILDCARD
)) {
3172 INT pos
= uri
->canon_len
;
3174 uri
->canon_uri
[pos
] = '/';
3175 uri
->canon_uri
[pos
+1] = '/';
3177 uri
->canon_len
+= 2;
3180 if(!canonicalize_authority(data
, uri
, flags
, computeOnly
))
3183 if(data
->is_relative
&& (data
->password
|| data
->username
)) {
3184 if(!canonicalize_path_opaque(data
, uri
, flags
, computeOnly
))
3187 if(!canonicalize_path_hierarchical(data
, uri
, flags
, computeOnly
))
3191 /* Opaque URI's don't have an authority. */
3192 uri
->userinfo_start
= uri
->userinfo_split
= -1;
3193 uri
->userinfo_len
= 0;
3194 uri
->host_start
= -1;
3196 uri
->host_type
= Uri_HOST_UNKNOWN
;
3197 uri
->has_port
= FALSE
;
3198 uri
->authority_start
= -1;
3199 uri
->authority_len
= 0;
3200 uri
->domain_offset
= -1;
3201 uri
->port_offset
= -1;
3203 if(is_hierarchical_scheme(data
->scheme_type
)) {
3206 /* Absolute URIs aren't displayed for known scheme types
3207 * which should be hierarchical URIs.
3209 uri
->display_modifiers
|= URI_DISPLAY_NO_ABSOLUTE_URI
;
3211 /* Windows also sets the port for these (if they have one). */
3212 for(i
= 0; i
< sizeof(default_ports
)/sizeof(default_ports
[0]); ++i
) {
3213 if(data
->scheme_type
== default_ports
[i
].scheme
) {
3214 uri
->has_port
= TRUE
;
3215 uri
->port
= default_ports
[i
].port
;
3221 if(!canonicalize_path_opaque(data
, uri
, flags
, computeOnly
))
3225 if(uri
->path_start
> -1 && !computeOnly
)
3226 /* Finding file extensions happens for both types of URIs. */
3227 uri
->extension_offset
= find_file_extension(uri
->canon_uri
+uri
->path_start
, uri
->path_len
);
3229 uri
->extension_offset
= -1;
3234 /* Attempts to canonicalize the query string of the URI.
3236 * Things that happen:
3237 * 1) For known scheme types forbidden characters
3238 * are percent encoded, unless the NO_DECODE_EXTRA_INFO flag is set
3239 * or NO_ENCODE_FORBIDDEN_CHARACTERS is set.
3241 * 2) For known scheme types, percent encoded, unreserved characters
3242 * are decoded as long as the NO_DECODE_EXTRA_INFO flag isn't set.
3244 static BOOL
canonicalize_query(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
3245 const WCHAR
*ptr
, *end
;
3246 const BOOL known_scheme
= data
->scheme_type
!= URL_SCHEME_UNKNOWN
;
3249 uri
->query_start
= -1;
3254 uri
->query_start
= uri
->canon_len
;
3256 end
= data
->query
+data
->query_len
;
3257 for(ptr
= data
->query
; ptr
< end
; ++ptr
) {
3259 if(known_scheme
&& !(flags
& Uri_CREATE_NO_DECODE_EXTRA_INFO
)) {
3260 WCHAR val
= decode_pct_val(ptr
);
3261 if(is_unreserved(val
)) {
3263 uri
->canon_uri
[uri
->canon_len
] = val
;
3270 } else if(known_scheme
&& !is_unreserved(*ptr
) && !is_reserved(*ptr
)) {
3271 if(!(flags
& Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS
) &&
3272 !(flags
& Uri_CREATE_NO_DECODE_EXTRA_INFO
)) {
3274 pct_encode_val(*ptr
, uri
->canon_uri
+uri
->canon_len
);
3275 uri
->canon_len
+= 3;
3281 uri
->canon_uri
[uri
->canon_len
] = *ptr
;
3285 uri
->query_len
= uri
->canon_len
- uri
->query_start
;
3288 TRACE("(%p %p %x %d): Canonicalized query string %s len=%d\n", data
, uri
, flags
,
3289 computeOnly
, debugstr_wn(uri
->canon_uri
+uri
->query_start
, uri
->query_len
),
3294 static BOOL
canonicalize_fragment(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
3295 const WCHAR
*ptr
, *end
;
3296 const BOOL known_scheme
= data
->scheme_type
!= URL_SCHEME_UNKNOWN
;
3298 if(!data
->fragment
) {
3299 uri
->fragment_start
= -1;
3300 uri
->fragment_len
= 0;
3304 uri
->fragment_start
= uri
->canon_len
;
3306 end
= data
->fragment
+ data
->fragment_len
;
3307 for(ptr
= data
->fragment
; ptr
< end
; ++ptr
) {
3309 if(known_scheme
&& !(flags
& Uri_CREATE_NO_DECODE_EXTRA_INFO
)) {
3310 WCHAR val
= decode_pct_val(ptr
);
3311 if(is_unreserved(val
)) {
3313 uri
->canon_uri
[uri
->canon_len
] = val
;
3320 } else if(known_scheme
&& !is_unreserved(*ptr
) && !is_reserved(*ptr
)) {
3321 if(!(flags
& Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS
) &&
3322 !(flags
& Uri_CREATE_NO_DECODE_EXTRA_INFO
)) {
3324 pct_encode_val(*ptr
, uri
->canon_uri
+uri
->canon_len
);
3325 uri
->canon_len
+= 3;
3331 uri
->canon_uri
[uri
->canon_len
] = *ptr
;
3335 uri
->fragment_len
= uri
->canon_len
- uri
->fragment_start
;
3338 TRACE("(%p %p %x %d): Canonicalized fragment %s len=%d\n", data
, uri
, flags
,
3339 computeOnly
, debugstr_wn(uri
->canon_uri
+uri
->fragment_start
, uri
->fragment_len
),
3344 /* Canonicalizes the scheme information specified in the parse_data using the specified flags. */
3345 static BOOL
canonicalize_scheme(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
3346 uri
->scheme_start
= -1;
3347 uri
->scheme_len
= 0;
3350 /* The only type of URI that doesn't have to have a scheme is a relative
3353 if(!data
->is_relative
) {
3354 FIXME("(%p %p %x): Unable to determine the scheme type of %s.\n", data
,
3355 uri
, flags
, debugstr_w(data
->uri
));
3361 INT pos
= uri
->canon_len
;
3363 for(i
= 0; i
< data
->scheme_len
; ++i
) {
3364 /* Scheme name must be lower case after canonicalization. */
3365 uri
->canon_uri
[i
+ pos
] = tolowerW(data
->scheme
[i
]);
3368 uri
->canon_uri
[i
+ pos
] = ':';
3369 uri
->scheme_start
= pos
;
3371 TRACE("(%p %p %x): Canonicalized scheme=%s, len=%d.\n", data
, uri
, flags
,
3372 debugstr_wn(uri
->canon_uri
, uri
->scheme_len
), data
->scheme_len
);
3375 /* This happens in both computation modes. */
3376 uri
->canon_len
+= data
->scheme_len
+ 1;
3377 uri
->scheme_len
= data
->scheme_len
;
3382 /* Compute's what the length of the URI specified by the parse_data will be
3383 * after canonicalization occurs using the specified flags.
3385 * This function will return a non-zero value indicating the length of the canonicalized
3386 * URI, or -1 on error.
3388 static int compute_canonicalized_length(const parse_data
*data
, DWORD flags
) {
3391 memset(&uri
, 0, sizeof(Uri
));
3393 TRACE("(%p %x): Beginning to compute canonicalized length for URI %s\n", data
, flags
,
3394 debugstr_w(data
->uri
));
3396 if(!canonicalize_scheme(data
, &uri
, flags
, TRUE
)) {
3397 ERR("(%p %x): Failed to compute URI scheme length.\n", data
, flags
);
3401 if(!canonicalize_hierpart(data
, &uri
, flags
, TRUE
)) {
3402 ERR("(%p %x): Failed to compute URI hierpart length.\n", data
, flags
);
3406 if(!canonicalize_query(data
, &uri
, flags
, TRUE
)) {
3407 ERR("(%p %x): Failed to compute query string length.\n", data
, flags
);
3411 if(!canonicalize_fragment(data
, &uri
, flags
, TRUE
)) {
3412 ERR("(%p %x): Failed to compute fragment length.\n", data
, flags
);
3416 TRACE("(%p %x): Finished computing canonicalized URI length. length=%d\n", data
, flags
, uri
.canon_len
);
3418 return uri
.canon_len
;
3421 /* Canonicalizes the URI data specified in the parse_data, using the given flags. If the
3422 * canonicalization succeededs it will store all the canonicalization information
3423 * in the pointer to the Uri.
3425 * To canonicalize a URI this function first computes what the length of the URI
3426 * specified by the parse_data will be. Once this is done it will then perfom the actual
3427 * canonicalization of the URI.
3429 static HRESULT
canonicalize_uri(const parse_data
*data
, Uri
*uri
, DWORD flags
) {
3432 uri
->canon_uri
= NULL
;
3433 len
= uri
->canon_size
= uri
->canon_len
= 0;
3435 TRACE("(%p %p %x): beginning to canonicalize URI %s.\n", data
, uri
, flags
, debugstr_w(data
->uri
));
3437 /* First try to compute the length of the URI. */
3438 len
= compute_canonicalized_length(data
, flags
);
3440 ERR("(%p %p %x): Could not compute the canonicalized length of %s.\n", data
, uri
, flags
,
3441 debugstr_w(data
->uri
));
3442 return E_INVALIDARG
;
3445 uri
->canon_uri
= heap_alloc((len
+1)*sizeof(WCHAR
));
3447 return E_OUTOFMEMORY
;
3449 uri
->canon_size
= len
;
3450 if(!canonicalize_scheme(data
, uri
, flags
, FALSE
)) {
3451 ERR("(%p %p %x): Unable to canonicalize the scheme of the URI.\n", data
, uri
, flags
);
3452 return E_INVALIDARG
;
3454 uri
->scheme_type
= data
->scheme_type
;
3456 if(!canonicalize_hierpart(data
, uri
, flags
, FALSE
)) {
3457 ERR("(%p %p %x): Unable to canonicalize the heirpart of the URI\n", data
, uri
, flags
);
3458 return E_INVALIDARG
;
3461 if(!canonicalize_query(data
, uri
, flags
, FALSE
)) {
3462 ERR("(%p %p %x): Unable to canonicalize query string of the URI.\n",
3464 return E_INVALIDARG
;
3467 if(!canonicalize_fragment(data
, uri
, flags
, FALSE
)) {
3468 ERR("(%p %p %x): Unable to canonicalize fragment of the URI.\n",
3470 return E_INVALIDARG
;
3473 /* There's a possibility we didn't use all the space we allocated
3476 if(uri
->canon_len
< uri
->canon_size
) {
3477 /* This happens if the URI is hierarchical and dot
3478 * segments were removed from it's path.
3480 WCHAR
*tmp
= heap_realloc(uri
->canon_uri
, (uri
->canon_len
+1)*sizeof(WCHAR
));
3482 return E_OUTOFMEMORY
;
3484 uri
->canon_uri
= tmp
;
3485 uri
->canon_size
= uri
->canon_len
;
3488 uri
->canon_uri
[uri
->canon_len
] = '\0';
3489 TRACE("(%p %p %x): finished canonicalizing the URI. uri=%s\n", data
, uri
, flags
, debugstr_w(uri
->canon_uri
));
3494 static HRESULT
get_builder_component(LPWSTR
*component
, DWORD
*component_len
,
3495 LPCWSTR source
, DWORD source_len
,
3496 LPCWSTR
*output
, DWORD
*output_len
)
3509 if(!(*component
) && source
) {
3510 /* Allocate 'component', and copy the contents from 'source'
3511 * into the new allocation.
3513 *component
= heap_alloc((source_len
+1)*sizeof(WCHAR
));
3515 return E_OUTOFMEMORY
;
3517 memcpy(*component
, source
, source_len
*sizeof(WCHAR
));
3518 (*component
)[source_len
] = '\0';
3519 *component_len
= source_len
;
3522 *output
= *component
;
3523 *output_len
= *component_len
;
3524 return *output
? S_OK
: S_FALSE
;
3527 /* Allocates 'component' and copies the string from 'new_value' into 'component'.
3528 * If 'prefix' is set and 'new_value' isn't NULL, then it checks if 'new_value'
3529 * starts with 'prefix'. If it doesn't then 'prefix' is prepended to 'component'.
3531 * If everything is successful, then will set 'success_flag' in 'flags'.
3533 static HRESULT
set_builder_component(LPWSTR
*component
, DWORD
*component_len
, LPCWSTR new_value
,
3534 WCHAR prefix
, DWORD
*flags
, DWORD success_flag
)
3536 heap_free(*component
);
3542 BOOL add_prefix
= FALSE
;
3543 DWORD len
= lstrlenW(new_value
);
3546 if(prefix
&& *new_value
!= prefix
) {
3548 *component
= heap_alloc((len
+2)*sizeof(WCHAR
));
3550 *component
= heap_alloc((len
+1)*sizeof(WCHAR
));
3553 return E_OUTOFMEMORY
;
3556 (*component
)[pos
++] = prefix
;
3558 memcpy(*component
+pos
, new_value
, (len
+1)*sizeof(WCHAR
));
3559 *component_len
= len
+pos
;
3562 *flags
|= success_flag
;
3566 #define URI(x) ((IUri*) &(x)->lpIUriVtbl)
3567 #define URIBUILDER(x) ((IUriBuilder*) &(x)->lpIUriBuilderVtbl)
3569 static void reset_builder(UriBuilder
*builder
) {
3571 IUri_Release(URI(builder
->uri
));
3572 builder
->uri
= NULL
;
3574 heap_free(builder
->fragment
);
3575 builder
->fragment
= NULL
;
3576 builder
->fragment_len
= 0;
3578 heap_free(builder
->host
);
3579 builder
->host
= NULL
;
3580 builder
->host_len
= 0;
3582 heap_free(builder
->password
);
3583 builder
->password
= NULL
;
3584 builder
->password_len
= 0;
3586 heap_free(builder
->path
);
3587 builder
->path
= NULL
;
3588 builder
->path_len
= 0;
3590 heap_free(builder
->query
);
3591 builder
->query
= NULL
;
3592 builder
->query_len
= 0;
3594 heap_free(builder
->scheme
);
3595 builder
->scheme
= NULL
;
3596 builder
->scheme_len
= 0;
3598 heap_free(builder
->username
);
3599 builder
->username
= NULL
;
3600 builder
->username_len
= 0;
3602 builder
->has_port
= FALSE
;
3604 builder
->modified_props
= 0;
3607 static HRESULT
validate_scheme_name(const UriBuilder
*builder
, parse_data
*data
, DWORD flags
) {
3608 const WCHAR
*component
;
3613 if(builder
->scheme
) {
3614 ptr
= builder
->scheme
;
3615 expected_len
= builder
->scheme_len
;
3616 } else if(builder
->uri
&& builder
->uri
->scheme_start
> -1) {
3617 ptr
= builder
->uri
->canon_uri
+builder
->uri
->scheme_start
;
3618 expected_len
= builder
->uri
->scheme_len
;
3620 static const WCHAR nullW
[] = {0};
3627 if(parse_scheme(pptr
, data
, flags
, ALLOW_NULL_TERM_SCHEME
) &&
3628 data
->scheme_len
== expected_len
) {
3630 TRACE("(%p %p %x): Found valid scheme component %s len=%d.\n", builder
, data
, flags
,
3631 debugstr_wn(data
->scheme
, data
->scheme_len
), data
->scheme_len
);
3633 TRACE("(%p %p %x): Invalid scheme component found %s.\n", builder
, data
, flags
,
3634 debugstr_wn(component
, expected_len
));
3635 return INET_E_INVALID_URL
;
3641 static HRESULT
validate_username(const UriBuilder
*builder
, parse_data
*data
, DWORD flags
) {
3646 if(builder
->username
) {
3647 ptr
= builder
->username
;
3648 expected_len
= builder
->username_len
;
3649 } else if(!(builder
->modified_props
& Uri_HAS_USER_NAME
) && builder
->uri
&&
3650 builder
->uri
->userinfo_start
> -1 && builder
->uri
->userinfo_split
!= 0) {
3651 /* Just use the username from the base Uri. */
3652 data
->username
= builder
->uri
->canon_uri
+builder
->uri
->userinfo_start
;
3653 data
->username_len
= (builder
->uri
->userinfo_split
> -1) ?
3654 builder
->uri
->userinfo_split
: builder
->uri
->userinfo_len
;
3662 const WCHAR
*component
= ptr
;
3664 if(parse_username(pptr
, data
, flags
, ALLOW_NULL_TERM_USER_NAME
) &&
3665 data
->username_len
== expected_len
)
3666 TRACE("(%p %p %x): Found valid username component %s len=%d.\n", builder
, data
, flags
,
3667 debugstr_wn(data
->username
, data
->username_len
), data
->username_len
);
3669 TRACE("(%p %p %x): Invalid username component found %s.\n", builder
, data
, flags
,
3670 debugstr_wn(component
, expected_len
));
3671 return INET_E_INVALID_URL
;
3678 static HRESULT
validate_password(const UriBuilder
*builder
, parse_data
*data
, DWORD flags
) {
3683 if(builder
->password
) {
3684 ptr
= builder
->password
;
3685 expected_len
= builder
->password_len
;
3686 } else if(!(builder
->modified_props
& Uri_HAS_PASSWORD
) && builder
->uri
&&
3687 builder
->uri
->userinfo_split
> -1) {
3688 data
->password
= builder
->uri
->canon_uri
+builder
->uri
->userinfo_start
+builder
->uri
->userinfo_split
+1;
3689 data
->password_len
= builder
->uri
->userinfo_len
-builder
->uri
->userinfo_split
-1;
3697 const WCHAR
*component
= ptr
;
3699 if(parse_password(pptr
, data
, flags
, ALLOW_NULL_TERM_PASSWORD
) &&
3700 data
->password_len
== expected_len
)
3701 TRACE("(%p %p %x): Found valid password component %s len=%d.\n", builder
, data
, flags
,
3702 debugstr_wn(data
->password
, data
->password_len
), data
->password_len
);
3704 TRACE("(%p %p %x): Invalid password component found %s.\n", builder
, data
, flags
,
3705 debugstr_wn(component
, expected_len
));
3706 return INET_E_INVALID_URL
;
3713 static HRESULT
validate_userinfo(const UriBuilder
*builder
, parse_data
*data
, DWORD flags
) {
3716 hr
= validate_username(builder
, data
, flags
);
3720 hr
= validate_password(builder
, data
, flags
);
3727 static HRESULT
validate_host(const UriBuilder
*builder
, parse_data
*data
, DWORD flags
) {
3733 ptr
= builder
->host
;
3734 expected_len
= builder
->host_len
;
3735 } else if(!(builder
->modified_props
& Uri_HAS_HOST
) && builder
->uri
&& builder
->uri
->host_start
> -1) {
3736 ptr
= builder
->uri
->canon_uri
+ builder
->uri
->host_start
;
3737 expected_len
= builder
->uri
->host_len
;
3742 const WCHAR
*component
= ptr
;
3743 DWORD extras
= ALLOW_BRACKETLESS_IP_LITERAL
|IGNORE_PORT_DELIMITER
|SKIP_IP_FUTURE_CHECK
;
3746 if(parse_host(pptr
, data
, flags
, extras
) && data
->host_len
== expected_len
)
3747 TRACE("(%p %p %x): Found valid host name %s len=%d type=%d.\n", builder
, data
, flags
,
3748 debugstr_wn(data
->host
, data
->host_len
), data
->host_len
, data
->host_type
);
3750 TRACE("(%p %p %x): Invalid host name found %s.\n", builder
, data
, flags
,
3751 debugstr_wn(component
, expected_len
));
3752 return INET_E_INVALID_URL
;
3759 static void setup_port(const UriBuilder
*builder
, parse_data
*data
, DWORD flags
) {
3760 if(builder
->modified_props
& Uri_HAS_PORT
) {
3761 if(builder
->has_port
) {
3762 data
->has_port
= TRUE
;
3763 data
->port_value
= builder
->port
;
3765 } else if(builder
->uri
&& builder
->uri
->has_port
) {
3766 data
->has_port
= TRUE
;
3767 data
->port_value
= builder
->uri
->port
;
3771 TRACE("(%p %p %x): Using %u as port for IUri.\n", builder
, data
, flags
, data
->port_value
);
3774 static HRESULT
validate_path(const UriBuilder
*builder
, parse_data
*data
, DWORD flags
) {
3775 const WCHAR
*ptr
= NULL
;
3776 const WCHAR
*component
;
3779 BOOL check_len
= TRUE
;
3783 ptr
= builder
->path
;
3784 expected_len
= builder
->path_len
;
3785 } else if(!(builder
->modified_props
& Uri_HAS_PATH
) &&
3786 builder
->uri
&& builder
->uri
->path_start
> -1) {
3787 ptr
= builder
->uri
->canon_uri
+builder
->uri
->path_start
;
3788 expected_len
= builder
->uri
->path_len
;
3790 static const WCHAR nullW
[] = {0};
3798 /* How the path is validated depends on what type of
3801 valid
= data
->is_opaque
?
3802 parse_path_opaque(pptr
, data
, flags
) : parse_path_hierarchical(pptr
, data
, flags
);
3804 if(!valid
|| (check_len
&& expected_len
!= data
->path_len
)) {
3805 TRACE("(%p %p %x): Invalid path component %s.\n", builder
, data
, flags
,
3806 debugstr_wn(component
, check_len
? expected_len
: -1) );
3807 return INET_E_INVALID_URL
;
3810 TRACE("(%p %p %x): Valid path component %s len=%d.\n", builder
, data
, flags
,
3811 debugstr_wn(data
->path
, data
->path_len
), data
->path_len
);
3816 static HRESULT
validate_query(const UriBuilder
*builder
, parse_data
*data
, DWORD flags
) {
3817 const WCHAR
*ptr
= NULL
;
3821 if(builder
->query
) {
3822 ptr
= builder
->query
;
3823 expected_len
= builder
->query_len
;
3824 } else if(!(builder
->modified_props
& Uri_HAS_QUERY
) && builder
->uri
&&
3825 builder
->uri
->query_start
> -1) {
3826 ptr
= builder
->uri
->canon_uri
+builder
->uri
->query_start
;
3827 expected_len
= builder
->uri
->query_len
;
3831 const WCHAR
*component
= ptr
;
3834 if(parse_query(pptr
, data
, flags
) && expected_len
== data
->query_len
)
3835 TRACE("(%p %p %x): Valid query component %s len=%d.\n", builder
, data
, flags
,
3836 debugstr_wn(data
->query
, data
->query_len
), data
->query_len
);
3838 TRACE("(%p %p %x): Invalid query component %s.\n", builder
, data
, flags
,
3839 debugstr_wn(component
, expected_len
));
3840 return INET_E_INVALID_URL
;
3847 static HRESULT
validate_fragment(const UriBuilder
*builder
, parse_data
*data
, DWORD flags
) {
3848 const WCHAR
*ptr
= NULL
;
3852 if(builder
->fragment
) {
3853 ptr
= builder
->fragment
;
3854 expected_len
= builder
->fragment_len
;
3855 } else if(!(builder
->modified_props
& Uri_HAS_FRAGMENT
) && builder
->uri
&&
3856 builder
->uri
->fragment_start
> -1) {
3857 ptr
= builder
->uri
->canon_uri
+builder
->uri
->fragment_start
;
3858 expected_len
= builder
->uri
->fragment_len
;
3862 const WCHAR
*component
= ptr
;
3865 if(parse_fragment(pptr
, data
, flags
) && expected_len
== data
->fragment_len
)
3866 TRACE("(%p %p %x): Valid fragment component %s len=%d.\n", builder
, data
, flags
,
3867 debugstr_wn(data
->fragment
, data
->fragment_len
), data
->fragment_len
);
3869 TRACE("(%p %p %x): Invalid fragment component %s.\n", builder
, data
, flags
,
3870 debugstr_wn(component
, expected_len
));
3871 return INET_E_INVALID_URL
;
3878 static HRESULT
validate_components(const UriBuilder
*builder
, parse_data
*data
, DWORD flags
) {
3881 memset(data
, 0, sizeof(parse_data
));
3883 TRACE("(%p %p %x): Beginning to validate builder components.\n", builder
, data
, flags
);
3885 hr
= validate_scheme_name(builder
, data
, flags
);
3889 /* Extra validation for file schemes. */
3890 if(data
->scheme_type
== URL_SCHEME_FILE
) {
3891 if((builder
->password
|| (builder
->uri
&& builder
->uri
->userinfo_split
> -1)) ||
3892 (builder
->username
|| (builder
->uri
&& builder
->uri
->userinfo_start
> -1))) {
3893 TRACE("(%p %p %x): File schemes can't contain a username or password.\n",
3894 builder
, data
, flags
);
3895 return INET_E_INVALID_URL
;
3899 hr
= validate_userinfo(builder
, data
, flags
);
3903 hr
= validate_host(builder
, data
, flags
);
3907 setup_port(builder
, data
, flags
);
3909 /* The URI is opaque if it doesn't have an authority component. */
3910 if(!data
->is_relative
)
3911 data
->is_opaque
= !data
->username
&& !data
->password
&& !data
->host
&& !data
->has_port
;
3913 data
->is_opaque
= !data
->host
&& !data
->has_port
;
3915 hr
= validate_path(builder
, data
, flags
);
3919 hr
= validate_query(builder
, data
, flags
);
3923 hr
= validate_fragment(builder
, data
, flags
);
3927 TRACE("(%p %p %x): Finished validating builder components.\n", builder
, data
, flags
);
3932 static void convert_to_dos_path(const WCHAR
*path
, DWORD path_len
,
3933 WCHAR
*output
, DWORD
*output_len
)
3935 const WCHAR
*ptr
= path
;
3937 if(path_len
> 3 && *ptr
== '/' && is_drive_path(path
+1))
3938 /* Skip over the leading / before the drive path. */
3941 for(; ptr
< path
+path_len
; ++ptr
) {
3954 /* Generates a raw uri string using the parse_data. */
3955 static DWORD
generate_raw_uri(const parse_data
*data
, BSTR uri
, DWORD flags
) {
3960 memcpy(uri
, data
->scheme
, data
->scheme_len
*sizeof(WCHAR
));
3961 uri
[data
->scheme_len
] = ':';
3963 length
+= data
->scheme_len
+1;
3966 if(!data
->is_opaque
) {
3967 /* For the "//" which appears before the authority component. */
3970 uri
[length
+1] = '/';
3974 /* Check if we need to add the "\\" before the host name
3975 * of a UNC server name in a DOS path.
3977 if(flags
& RAW_URI_CONVERT_TO_DOS_PATH
&&
3978 data
->scheme_type
== URL_SCHEME_FILE
&& data
->host
) {
3981 uri
[length
+1] = '\\';
3987 if(data
->username
) {
3989 memcpy(uri
+length
, data
->username
, data
->username_len
*sizeof(WCHAR
));
3990 length
+= data
->username_len
;
3993 if(data
->password
) {
3996 memcpy(uri
+length
+1, data
->password
, data
->password_len
*sizeof(WCHAR
));
3998 length
+= data
->password_len
+1;
4001 if(data
->password
|| data
->username
) {
4008 /* IPv6 addresses get the brackets added around them if they don't already
4011 const BOOL add_brackets
= data
->host_type
== Uri_HOST_IPV6
&& *(data
->host
) != '[';
4019 memcpy(uri
+length
, data
->host
, data
->host_len
*sizeof(WCHAR
));
4020 length
+= data
->host_len
;
4029 if(data
->has_port
) {
4030 /* The port isn't included in the raw uri if it's the default
4031 * port for the scheme type.
4034 BOOL is_default
= FALSE
;
4036 for(i
= 0; i
< sizeof(default_ports
)/sizeof(default_ports
[0]); ++i
) {
4037 if(data
->scheme_type
== default_ports
[i
].scheme
&&
4038 data
->port_value
== default_ports
[i
].port
)
4042 if(!is_default
|| flags
& RAW_URI_FORCE_PORT_DISP
) {
4048 length
+= ui2str(uri
+length
, data
->port_value
);
4050 length
+= ui2str(NULL
, data
->port_value
);
4054 /* Check if a '/' should be added before the path for hierarchical URIs. */
4055 if(!data
->is_opaque
&& data
->path
&& *(data
->path
) != '/') {
4062 if(!data
->is_opaque
&& data
->scheme_type
== URL_SCHEME_FILE
&&
4063 flags
& RAW_URI_CONVERT_TO_DOS_PATH
) {
4067 convert_to_dos_path(data
->path
, data
->path_len
, uri
+length
, &len
);
4069 convert_to_dos_path(data
->path
, data
->path_len
, NULL
, &len
);
4074 memcpy(uri
+length
, data
->path
, data
->path_len
*sizeof(WCHAR
));
4075 length
+= data
->path_len
;
4081 memcpy(uri
+length
, data
->query
, data
->query_len
*sizeof(WCHAR
));
4082 length
+= data
->query_len
;
4085 if(data
->fragment
) {
4087 memcpy(uri
+length
, data
->fragment
, data
->fragment_len
*sizeof(WCHAR
));
4088 length
+= data
->fragment_len
;
4092 TRACE("(%p %p): Generated raw uri=%s len=%d\n", data
, uri
, debugstr_wn(uri
, length
), length
);
4094 TRACE("(%p %p): Computed raw uri len=%d\n", data
, uri
, length
);
4099 static HRESULT
generate_uri(const UriBuilder
*builder
, const parse_data
*data
, Uri
*uri
, DWORD flags
) {
4101 DWORD length
= generate_raw_uri(data
, NULL
, 0);
4102 uri
->raw_uri
= SysAllocStringLen(NULL
, length
);
4104 return E_OUTOFMEMORY
;
4106 generate_raw_uri(data
, uri
->raw_uri
, 0);
4108 hr
= canonicalize_uri(data
, uri
, flags
);
4110 if(hr
== E_INVALIDARG
)
4111 return INET_E_INVALID_URL
;
4115 uri
->create_flags
= flags
;
4119 #define URI_THIS(iface) DEFINE_THIS(Uri, IUri, iface)
4121 static HRESULT WINAPI
Uri_QueryInterface(IUri
*iface
, REFIID riid
, void **ppv
)
4123 Uri
*This
= URI_THIS(iface
);
4125 if(IsEqualGUID(&IID_IUnknown
, riid
)) {
4126 TRACE("(%p)->(IID_IUnknown %p)\n", This
, ppv
);
4128 }else if(IsEqualGUID(&IID_IUri
, riid
)) {
4129 TRACE("(%p)->(IID_IUri %p)\n", This
, ppv
);
4131 }else if(IsEqualGUID(&IID_IUriObj
, riid
)) {
4132 TRACE("(%p)->(IID_IUriObj %p)\n", This
, ppv
);
4136 TRACE("(%p)->(%s %p)\n", This
, debugstr_guid(riid
), ppv
);
4138 return E_NOINTERFACE
;
4141 IUnknown_AddRef((IUnknown
*)*ppv
);
4145 static ULONG WINAPI
Uri_AddRef(IUri
*iface
)
4147 Uri
*This
= URI_THIS(iface
);
4148 LONG ref
= InterlockedIncrement(&This
->ref
);
4150 TRACE("(%p) ref=%d\n", This
, ref
);
4155 static ULONG WINAPI
Uri_Release(IUri
*iface
)
4157 Uri
*This
= URI_THIS(iface
);
4158 LONG ref
= InterlockedDecrement(&This
->ref
);
4160 TRACE("(%p) ref=%d\n", This
, ref
);
4163 SysFreeString(This
->raw_uri
);
4164 heap_free(This
->canon_uri
);
4171 static HRESULT WINAPI
Uri_GetPropertyBSTR(IUri
*iface
, Uri_PROPERTY uriProp
, BSTR
*pbstrProperty
, DWORD dwFlags
)
4173 Uri
*This
= URI_THIS(iface
);
4175 TRACE("(%p)->(%d %p %x)\n", This
, uriProp
, pbstrProperty
, dwFlags
);
4180 if(uriProp
> Uri_PROPERTY_STRING_LAST
) {
4181 /* Windows allocates an empty BSTR for invalid Uri_PROPERTY's. */
4182 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
4183 if(!(*pbstrProperty
))
4184 return E_OUTOFMEMORY
;
4186 /* It only returns S_FALSE for the ZONE property... */
4187 if(uriProp
== Uri_PROPERTY_ZONE
)
4193 /* Don't have support for flags yet. */
4195 FIXME("(%p)->(%d %p %x)\n", This
, uriProp
, pbstrProperty
, dwFlags
);
4200 case Uri_PROPERTY_ABSOLUTE_URI
:
4201 if(This
->display_modifiers
& URI_DISPLAY_NO_ABSOLUTE_URI
) {
4202 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
4205 if(This
->scheme_type
!= URL_SCHEME_UNKNOWN
&& This
->userinfo_start
> -1) {
4206 if(This
->userinfo_len
== 0) {
4207 /* Don't include the '@' after the userinfo component. */
4208 *pbstrProperty
= SysAllocStringLen(NULL
, This
->canon_len
-1);
4210 if(*pbstrProperty
) {
4211 /* Copy everything before it. */
4212 memcpy(*pbstrProperty
, This
->canon_uri
, This
->userinfo_start
*sizeof(WCHAR
));
4214 /* And everything after it. */
4215 memcpy(*pbstrProperty
+This
->userinfo_start
, This
->canon_uri
+This
->userinfo_start
+1,
4216 (This
->canon_len
-This
->userinfo_start
-1)*sizeof(WCHAR
));
4218 } else if(This
->userinfo_split
== 0 && This
->userinfo_len
== 1) {
4219 /* Don't include the ":@" */
4220 *pbstrProperty
= SysAllocStringLen(NULL
, This
->canon_len
-2);
4222 if(*pbstrProperty
) {
4223 memcpy(*pbstrProperty
, This
->canon_uri
, This
->userinfo_start
*sizeof(WCHAR
));
4224 memcpy(*pbstrProperty
+This
->userinfo_start
, This
->canon_uri
+This
->userinfo_start
+2,
4225 (This
->canon_len
-This
->userinfo_start
-2)*sizeof(WCHAR
));
4228 *pbstrProperty
= SysAllocString(This
->canon_uri
);
4232 *pbstrProperty
= SysAllocString(This
->canon_uri
);
4237 if(!(*pbstrProperty
))
4238 hres
= E_OUTOFMEMORY
;
4241 case Uri_PROPERTY_AUTHORITY
:
4242 if(This
->authority_start
> -1) {
4243 if(This
->port_offset
> -1 && is_default_port(This
->scheme_type
, This
->port
) &&
4244 This
->display_modifiers
& URI_DISPLAY_NO_DEFAULT_PORT_AUTH
)
4245 /* Don't include the port in the authority component. */
4246 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+This
->authority_start
, This
->port_offset
);
4248 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+This
->authority_start
, This
->authority_len
);
4251 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
4255 if(!(*pbstrProperty
))
4256 hres
= E_OUTOFMEMORY
;
4259 case Uri_PROPERTY_DISPLAY_URI
:
4260 /* The Display URI contains everything except for the userinfo for known
4263 if(This
->scheme_type
!= URL_SCHEME_UNKNOWN
&& This
->userinfo_start
> -1) {
4264 *pbstrProperty
= SysAllocStringLen(NULL
, This
->canon_len
-This
->userinfo_len
);
4266 if(*pbstrProperty
) {
4267 /* Copy everything before the userinfo over. */
4268 memcpy(*pbstrProperty
, This
->canon_uri
, This
->userinfo_start
*sizeof(WCHAR
));
4269 /* Copy everything after the userinfo over. */
4270 memcpy(*pbstrProperty
+This
->userinfo_start
,
4271 This
->canon_uri
+This
->userinfo_start
+This
->userinfo_len
+1,
4272 (This
->canon_len
-(This
->userinfo_start
+This
->userinfo_len
+1))*sizeof(WCHAR
));
4275 *pbstrProperty
= SysAllocString(This
->canon_uri
);
4277 if(!(*pbstrProperty
))
4278 hres
= E_OUTOFMEMORY
;
4283 case Uri_PROPERTY_DOMAIN
:
4284 if(This
->domain_offset
> -1) {
4285 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+This
->host_start
+This
->domain_offset
,
4286 This
->host_len
-This
->domain_offset
);
4289 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
4293 if(!(*pbstrProperty
))
4294 hres
= E_OUTOFMEMORY
;
4297 case Uri_PROPERTY_EXTENSION
:
4298 if(This
->extension_offset
> -1) {
4299 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+This
->path_start
+This
->extension_offset
,
4300 This
->path_len
-This
->extension_offset
);
4303 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
4307 if(!(*pbstrProperty
))
4308 hres
= E_OUTOFMEMORY
;
4311 case Uri_PROPERTY_FRAGMENT
:
4312 if(This
->fragment_start
> -1) {
4313 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+This
->fragment_start
, This
->fragment_len
);
4316 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
4320 if(!(*pbstrProperty
))
4321 hres
= E_OUTOFMEMORY
;
4324 case Uri_PROPERTY_HOST
:
4325 if(This
->host_start
> -1) {
4326 /* The '[' and ']' aren't included for IPv6 addresses. */
4327 if(This
->host_type
== Uri_HOST_IPV6
)
4328 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+This
->host_start
+1, This
->host_len
-2);
4330 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+This
->host_start
, This
->host_len
);
4334 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
4338 if(!(*pbstrProperty
))
4339 hres
= E_OUTOFMEMORY
;
4342 case Uri_PROPERTY_PASSWORD
:
4343 if(This
->userinfo_split
> -1) {
4344 *pbstrProperty
= SysAllocStringLen(
4345 This
->canon_uri
+This
->userinfo_start
+This
->userinfo_split
+1,
4346 This
->userinfo_len
-This
->userinfo_split
-1);
4349 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
4353 if(!(*pbstrProperty
))
4354 return E_OUTOFMEMORY
;
4357 case Uri_PROPERTY_PATH
:
4358 if(This
->path_start
> -1) {
4359 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+This
->path_start
, This
->path_len
);
4362 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
4366 if(!(*pbstrProperty
))
4367 hres
= E_OUTOFMEMORY
;
4370 case Uri_PROPERTY_PATH_AND_QUERY
:
4371 if(This
->path_start
> -1) {
4372 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+This
->path_start
, This
->path_len
+This
->query_len
);
4374 } else if(This
->query_start
> -1) {
4375 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+This
->query_start
, This
->query_len
);
4378 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
4382 if(!(*pbstrProperty
))
4383 hres
= E_OUTOFMEMORY
;
4386 case Uri_PROPERTY_QUERY
:
4387 if(This
->query_start
> -1) {
4388 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+This
->query_start
, This
->query_len
);
4391 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
4395 if(!(*pbstrProperty
))
4396 hres
= E_OUTOFMEMORY
;
4399 case Uri_PROPERTY_RAW_URI
:
4400 *pbstrProperty
= SysAllocString(This
->raw_uri
);
4401 if(!(*pbstrProperty
))
4402 hres
= E_OUTOFMEMORY
;
4406 case Uri_PROPERTY_SCHEME_NAME
:
4407 if(This
->scheme_start
> -1) {
4408 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+ This
->scheme_start
, This
->scheme_len
);
4411 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
4415 if(!(*pbstrProperty
))
4416 hres
= E_OUTOFMEMORY
;
4419 case Uri_PROPERTY_USER_INFO
:
4420 if(This
->userinfo_start
> -1) {
4421 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+This
->userinfo_start
, This
->userinfo_len
);
4424 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
4428 if(!(*pbstrProperty
))
4429 hres
= E_OUTOFMEMORY
;
4432 case Uri_PROPERTY_USER_NAME
:
4433 if(This
->userinfo_start
> -1 && This
->userinfo_split
!= 0) {
4434 /* If userinfo_split is set, that means a password exists
4435 * so the username is only from userinfo_start to userinfo_split.
4437 if(This
->userinfo_split
> -1) {
4438 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+ This
->userinfo_start
, This
->userinfo_split
);
4441 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+ This
->userinfo_start
, This
->userinfo_len
);
4445 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
4449 if(!(*pbstrProperty
))
4450 return E_OUTOFMEMORY
;
4454 FIXME("(%p)->(%d %p %x)\n", This
, uriProp
, pbstrProperty
, dwFlags
);
4461 static HRESULT WINAPI
Uri_GetPropertyLength(IUri
*iface
, Uri_PROPERTY uriProp
, DWORD
*pcchProperty
, DWORD dwFlags
)
4463 Uri
*This
= URI_THIS(iface
);
4465 TRACE("(%p)->(%d %p %x)\n", This
, uriProp
, pcchProperty
, dwFlags
);
4468 return E_INVALIDARG
;
4470 /* Can only return a length for a property if it's a string. */
4471 if(uriProp
> Uri_PROPERTY_STRING_LAST
)
4472 return E_INVALIDARG
;
4474 /* Don't have support for flags yet. */
4476 FIXME("(%p)->(%d %p %x)\n", This
, uriProp
, pcchProperty
, dwFlags
);
4481 case Uri_PROPERTY_ABSOLUTE_URI
:
4482 if(This
->display_modifiers
& URI_DISPLAY_NO_ABSOLUTE_URI
) {
4486 if(This
->scheme_type
!= URL_SCHEME_UNKNOWN
) {
4487 if(This
->userinfo_start
> -1 && This
->userinfo_len
== 0)
4488 /* Don't include the '@' in the length. */
4489 *pcchProperty
= This
->canon_len
-1;
4490 else if(This
->userinfo_start
> -1 && This
->userinfo_len
== 1 &&
4491 This
->userinfo_split
== 0)
4492 /* Don't include the ":@" in the length. */
4493 *pcchProperty
= This
->canon_len
-2;
4495 *pcchProperty
= This
->canon_len
;
4497 *pcchProperty
= This
->canon_len
;
4503 case Uri_PROPERTY_AUTHORITY
:
4504 if(This
->port_offset
> -1 &&
4505 This
->display_modifiers
& URI_DISPLAY_NO_DEFAULT_PORT_AUTH
&&
4506 is_default_port(This
->scheme_type
, This
->port
))
4507 /* Only count up until the port in the authority. */
4508 *pcchProperty
= This
->port_offset
;
4510 *pcchProperty
= This
->authority_len
;
4511 hres
= (This
->authority_start
> -1) ? S_OK
: S_FALSE
;
4513 case Uri_PROPERTY_DISPLAY_URI
:
4514 if(This
->scheme_type
!= URL_SCHEME_UNKNOWN
&& This
->userinfo_start
> -1)
4515 *pcchProperty
= This
->canon_len
-This
->userinfo_len
-1;
4517 *pcchProperty
= This
->canon_len
;
4521 case Uri_PROPERTY_DOMAIN
:
4522 if(This
->domain_offset
> -1)
4523 *pcchProperty
= This
->host_len
- This
->domain_offset
;
4527 hres
= (This
->domain_offset
> -1) ? S_OK
: S_FALSE
;
4529 case Uri_PROPERTY_EXTENSION
:
4530 if(This
->extension_offset
> -1) {
4531 *pcchProperty
= This
->path_len
- This
->extension_offset
;
4539 case Uri_PROPERTY_FRAGMENT
:
4540 *pcchProperty
= This
->fragment_len
;
4541 hres
= (This
->fragment_start
> -1) ? S_OK
: S_FALSE
;
4543 case Uri_PROPERTY_HOST
:
4544 *pcchProperty
= This
->host_len
;
4546 /* '[' and ']' aren't included in the length. */
4547 if(This
->host_type
== Uri_HOST_IPV6
)
4550 hres
= (This
->host_start
> -1) ? S_OK
: S_FALSE
;
4552 case Uri_PROPERTY_PASSWORD
:
4553 *pcchProperty
= (This
->userinfo_split
> -1) ? This
->userinfo_len
-This
->userinfo_split
-1 : 0;
4554 hres
= (This
->userinfo_split
> -1) ? S_OK
: S_FALSE
;
4556 case Uri_PROPERTY_PATH
:
4557 *pcchProperty
= This
->path_len
;
4558 hres
= (This
->path_start
> -1) ? S_OK
: S_FALSE
;
4560 case Uri_PROPERTY_PATH_AND_QUERY
:
4561 *pcchProperty
= This
->path_len
+This
->query_len
;
4562 hres
= (This
->path_start
> -1 || This
->query_start
> -1) ? S_OK
: S_FALSE
;
4564 case Uri_PROPERTY_QUERY
:
4565 *pcchProperty
= This
->query_len
;
4566 hres
= (This
->query_start
> -1) ? S_OK
: S_FALSE
;
4568 case Uri_PROPERTY_RAW_URI
:
4569 *pcchProperty
= SysStringLen(This
->raw_uri
);
4572 case Uri_PROPERTY_SCHEME_NAME
:
4573 *pcchProperty
= This
->scheme_len
;
4574 hres
= (This
->scheme_start
> -1) ? S_OK
: S_FALSE
;
4576 case Uri_PROPERTY_USER_INFO
:
4577 *pcchProperty
= This
->userinfo_len
;
4578 hres
= (This
->userinfo_start
> -1) ? S_OK
: S_FALSE
;
4580 case Uri_PROPERTY_USER_NAME
:
4581 *pcchProperty
= (This
->userinfo_split
> -1) ? This
->userinfo_split
: This
->userinfo_len
;
4582 if(This
->userinfo_split
== 0)
4585 hres
= (This
->userinfo_start
> -1) ? S_OK
: S_FALSE
;
4588 FIXME("(%p)->(%d %p %x)\n", This
, uriProp
, pcchProperty
, dwFlags
);
4595 static HRESULT WINAPI
Uri_GetPropertyDWORD(IUri
*iface
, Uri_PROPERTY uriProp
, DWORD
*pcchProperty
, DWORD dwFlags
)
4597 Uri
*This
= URI_THIS(iface
);
4600 TRACE("(%p)->(%d %p %x)\n", This
, uriProp
, pcchProperty
, dwFlags
);
4603 return E_INVALIDARG
;
4605 /* Microsoft's implementation for the ZONE property of a URI seems to be lacking...
4606 * From what I can tell, instead of checking which URLZONE the URI belongs to it
4607 * simply assigns URLZONE_INVALID and returns E_NOTIMPL. This also applies to the GetZone
4610 if(uriProp
== Uri_PROPERTY_ZONE
) {
4611 *pcchProperty
= URLZONE_INVALID
;
4615 if(uriProp
< Uri_PROPERTY_DWORD_START
) {
4617 return E_INVALIDARG
;
4621 case Uri_PROPERTY_HOST_TYPE
:
4622 *pcchProperty
= This
->host_type
;
4625 case Uri_PROPERTY_PORT
:
4626 if(!This
->has_port
) {
4630 *pcchProperty
= This
->port
;
4635 case Uri_PROPERTY_SCHEME
:
4636 *pcchProperty
= This
->scheme_type
;
4640 FIXME("(%p)->(%d %p %x)\n", This
, uriProp
, pcchProperty
, dwFlags
);
4647 static HRESULT WINAPI
Uri_HasProperty(IUri
*iface
, Uri_PROPERTY uriProp
, BOOL
*pfHasProperty
)
4649 Uri
*This
= URI_THIS(iface
);
4650 TRACE("(%p)->(%d %p)\n", This
, uriProp
, pfHasProperty
);
4653 return E_INVALIDARG
;
4656 case Uri_PROPERTY_ABSOLUTE_URI
:
4657 *pfHasProperty
= !(This
->display_modifiers
& URI_DISPLAY_NO_ABSOLUTE_URI
);
4659 case Uri_PROPERTY_AUTHORITY
:
4660 *pfHasProperty
= This
->authority_start
> -1;
4662 case Uri_PROPERTY_DISPLAY_URI
:
4663 *pfHasProperty
= TRUE
;
4665 case Uri_PROPERTY_DOMAIN
:
4666 *pfHasProperty
= This
->domain_offset
> -1;
4668 case Uri_PROPERTY_EXTENSION
:
4669 *pfHasProperty
= This
->extension_offset
> -1;
4671 case Uri_PROPERTY_FRAGMENT
:
4672 *pfHasProperty
= This
->fragment_start
> -1;
4674 case Uri_PROPERTY_HOST
:
4675 *pfHasProperty
= This
->host_start
> -1;
4677 case Uri_PROPERTY_PASSWORD
:
4678 *pfHasProperty
= This
->userinfo_split
> -1;
4680 case Uri_PROPERTY_PATH
:
4681 *pfHasProperty
= This
->path_start
> -1;
4683 case Uri_PROPERTY_PATH_AND_QUERY
:
4684 *pfHasProperty
= (This
->path_start
> -1 || This
->query_start
> -1);
4686 case Uri_PROPERTY_QUERY
:
4687 *pfHasProperty
= This
->query_start
> -1;
4689 case Uri_PROPERTY_RAW_URI
:
4690 *pfHasProperty
= TRUE
;
4692 case Uri_PROPERTY_SCHEME_NAME
:
4693 *pfHasProperty
= This
->scheme_start
> -1;
4695 case Uri_PROPERTY_USER_INFO
:
4696 *pfHasProperty
= This
->userinfo_start
> -1;
4698 case Uri_PROPERTY_USER_NAME
:
4699 if(This
->userinfo_split
== 0)
4700 *pfHasProperty
= FALSE
;
4702 *pfHasProperty
= This
->userinfo_start
> -1;
4704 case Uri_PROPERTY_HOST_TYPE
:
4705 *pfHasProperty
= TRUE
;
4707 case Uri_PROPERTY_PORT
:
4708 *pfHasProperty
= This
->has_port
;
4710 case Uri_PROPERTY_SCHEME
:
4711 *pfHasProperty
= TRUE
;
4713 case Uri_PROPERTY_ZONE
:
4714 *pfHasProperty
= FALSE
;
4717 FIXME("(%p)->(%d %p): Unsupported property type.\n", This
, uriProp
, pfHasProperty
);
4724 static HRESULT WINAPI
Uri_GetAbsoluteUri(IUri
*iface
, BSTR
*pstrAbsoluteUri
)
4726 TRACE("(%p)->(%p)\n", iface
, pstrAbsoluteUri
);
4727 return IUri_GetPropertyBSTR(iface
, Uri_PROPERTY_ABSOLUTE_URI
, pstrAbsoluteUri
, 0);
4730 static HRESULT WINAPI
Uri_GetAuthority(IUri
*iface
, BSTR
*pstrAuthority
)
4732 TRACE("(%p)->(%p)\n", iface
, pstrAuthority
);
4733 return IUri_GetPropertyBSTR(iface
, Uri_PROPERTY_AUTHORITY
, pstrAuthority
, 0);
4736 static HRESULT WINAPI
Uri_GetDisplayUri(IUri
*iface
, BSTR
*pstrDisplayUri
)
4738 TRACE("(%p)->(%p)\n", iface
, pstrDisplayUri
);
4739 return IUri_GetPropertyBSTR(iface
, Uri_PROPERTY_DISPLAY_URI
, pstrDisplayUri
, 0);
4742 static HRESULT WINAPI
Uri_GetDomain(IUri
*iface
, BSTR
*pstrDomain
)
4744 TRACE("(%p)->(%p)\n", iface
, pstrDomain
);
4745 return IUri_GetPropertyBSTR(iface
, Uri_PROPERTY_DOMAIN
, pstrDomain
, 0);
4748 static HRESULT WINAPI
Uri_GetExtension(IUri
*iface
, BSTR
*pstrExtension
)
4750 TRACE("(%p)->(%p)\n", iface
, pstrExtension
);
4751 return IUri_GetPropertyBSTR(iface
, Uri_PROPERTY_EXTENSION
, pstrExtension
, 0);
4754 static HRESULT WINAPI
Uri_GetFragment(IUri
*iface
, BSTR
*pstrFragment
)
4756 TRACE("(%p)->(%p)\n", iface
, pstrFragment
);
4757 return IUri_GetPropertyBSTR(iface
, Uri_PROPERTY_FRAGMENT
, pstrFragment
, 0);
4760 static HRESULT WINAPI
Uri_GetHost(IUri
*iface
, BSTR
*pstrHost
)
4762 TRACE("(%p)->(%p)\n", iface
, pstrHost
);
4763 return IUri_GetPropertyBSTR(iface
, Uri_PROPERTY_HOST
, pstrHost
, 0);
4766 static HRESULT WINAPI
Uri_GetPassword(IUri
*iface
, BSTR
*pstrPassword
)
4768 TRACE("(%p)->(%p)\n", iface
, pstrPassword
);
4769 return IUri_GetPropertyBSTR(iface
, Uri_PROPERTY_PASSWORD
, pstrPassword
, 0);
4772 static HRESULT WINAPI
Uri_GetPath(IUri
*iface
, BSTR
*pstrPath
)
4774 TRACE("(%p)->(%p)\n", iface
, pstrPath
);
4775 return IUri_GetPropertyBSTR(iface
, Uri_PROPERTY_PATH
, pstrPath
, 0);
4778 static HRESULT WINAPI
Uri_GetPathAndQuery(IUri
*iface
, BSTR
*pstrPathAndQuery
)
4780 TRACE("(%p)->(%p)\n", iface
, pstrPathAndQuery
);
4781 return IUri_GetPropertyBSTR(iface
, Uri_PROPERTY_PATH_AND_QUERY
, pstrPathAndQuery
, 0);
4784 static HRESULT WINAPI
Uri_GetQuery(IUri
*iface
, BSTR
*pstrQuery
)
4786 TRACE("(%p)->(%p)\n", iface
, pstrQuery
);
4787 return IUri_GetPropertyBSTR(iface
, Uri_PROPERTY_QUERY
, pstrQuery
, 0);
4790 static HRESULT WINAPI
Uri_GetRawUri(IUri
*iface
, BSTR
*pstrRawUri
)
4792 TRACE("(%p)->(%p)\n", iface
, pstrRawUri
);
4793 return IUri_GetPropertyBSTR(iface
, Uri_PROPERTY_RAW_URI
, pstrRawUri
, 0);
4796 static HRESULT WINAPI
Uri_GetSchemeName(IUri
*iface
, BSTR
*pstrSchemeName
)
4798 TRACE("(%p)->(%p)\n", iface
, pstrSchemeName
);
4799 return IUri_GetPropertyBSTR(iface
, Uri_PROPERTY_SCHEME_NAME
, pstrSchemeName
, 0);
4802 static HRESULT WINAPI
Uri_GetUserInfo(IUri
*iface
, BSTR
*pstrUserInfo
)
4804 TRACE("(%p)->(%p)\n", iface
, pstrUserInfo
);
4805 return IUri_GetPropertyBSTR(iface
, Uri_PROPERTY_USER_INFO
, pstrUserInfo
, 0);
4808 static HRESULT WINAPI
Uri_GetUserName(IUri
*iface
, BSTR
*pstrUserName
)
4810 TRACE("(%p)->(%p)\n", iface
, pstrUserName
);
4811 return IUri_GetPropertyBSTR(iface
, Uri_PROPERTY_USER_NAME
, pstrUserName
, 0);
4814 static HRESULT WINAPI
Uri_GetHostType(IUri
*iface
, DWORD
*pdwHostType
)
4816 TRACE("(%p)->(%p)\n", iface
, pdwHostType
);
4817 return IUri_GetPropertyDWORD(iface
, Uri_PROPERTY_HOST_TYPE
, pdwHostType
, 0);
4820 static HRESULT WINAPI
Uri_GetPort(IUri
*iface
, DWORD
*pdwPort
)
4822 TRACE("(%p)->(%p)\n", iface
, pdwPort
);
4823 return IUri_GetPropertyDWORD(iface
, Uri_PROPERTY_PORT
, pdwPort
, 0);
4826 static HRESULT WINAPI
Uri_GetScheme(IUri
*iface
, DWORD
*pdwScheme
)
4828 Uri
*This
= URI_THIS(iface
);
4829 TRACE("(%p)->(%p)\n", This
, pdwScheme
);
4830 return IUri_GetPropertyDWORD(iface
, Uri_PROPERTY_SCHEME
, pdwScheme
, 0);
4833 static HRESULT WINAPI
Uri_GetZone(IUri
*iface
, DWORD
*pdwZone
)
4835 TRACE("(%p)->(%p)\n", iface
, pdwZone
);
4836 return IUri_GetPropertyDWORD(iface
, Uri_PROPERTY_ZONE
,pdwZone
, 0);
4839 static HRESULT WINAPI
Uri_GetProperties(IUri
*iface
, DWORD
*pdwProperties
)
4841 Uri
*This
= URI_THIS(iface
);
4842 TRACE("(%p)->(%p)\n", This
, pdwProperties
);
4845 return E_INVALIDARG
;
4847 /* All URIs have these. */
4848 *pdwProperties
= Uri_HAS_DISPLAY_URI
|Uri_HAS_RAW_URI
|Uri_HAS_SCHEME
|Uri_HAS_HOST_TYPE
;
4850 if(!(This
->display_modifiers
& URI_DISPLAY_NO_ABSOLUTE_URI
))
4851 *pdwProperties
|= Uri_HAS_ABSOLUTE_URI
;
4853 if(This
->scheme_start
> -1)
4854 *pdwProperties
|= Uri_HAS_SCHEME_NAME
;
4856 if(This
->authority_start
> -1) {
4857 *pdwProperties
|= Uri_HAS_AUTHORITY
;
4858 if(This
->userinfo_start
> -1) {
4859 *pdwProperties
|= Uri_HAS_USER_INFO
;
4860 if(This
->userinfo_split
!= 0)
4861 *pdwProperties
|= Uri_HAS_USER_NAME
;
4863 if(This
->userinfo_split
> -1)
4864 *pdwProperties
|= Uri_HAS_PASSWORD
;
4865 if(This
->host_start
> -1)
4866 *pdwProperties
|= Uri_HAS_HOST
;
4867 if(This
->domain_offset
> -1)
4868 *pdwProperties
|= Uri_HAS_DOMAIN
;
4872 *pdwProperties
|= Uri_HAS_PORT
;
4873 if(This
->path_start
> -1)
4874 *pdwProperties
|= Uri_HAS_PATH
|Uri_HAS_PATH_AND_QUERY
;
4875 if(This
->query_start
> -1)
4876 *pdwProperties
|= Uri_HAS_QUERY
|Uri_HAS_PATH_AND_QUERY
;
4878 if(This
->extension_offset
> -1)
4879 *pdwProperties
|= Uri_HAS_EXTENSION
;
4881 if(This
->fragment_start
> -1)
4882 *pdwProperties
|= Uri_HAS_FRAGMENT
;
4887 static HRESULT WINAPI
Uri_IsEqual(IUri
*iface
, IUri
*pUri
, BOOL
*pfEqual
)
4889 Uri
*This
= URI_THIS(iface
);
4892 TRACE("(%p)->(%p %p)\n", This
, pUri
, pfEqual
);
4900 /* For some reason Windows returns S_OK here... */
4904 /* Try to convert it to a Uri (allows for a more simple comparison). */
4905 if((other
= get_uri_obj(pUri
)))
4906 *pfEqual
= are_equal_simple(This
, other
);
4908 /* Do it the hard way. */
4909 FIXME("(%p)->(%p %p) No support for unknown IUri's yet.\n", iface
, pUri
, pfEqual
);
4918 static const IUriVtbl UriVtbl
= {
4922 Uri_GetPropertyBSTR
,
4923 Uri_GetPropertyLength
,
4924 Uri_GetPropertyDWORD
,
4935 Uri_GetPathAndQuery
,
4949 static Uri
* create_uri_obj(void) {
4950 Uri
*ret
= heap_alloc_zero(sizeof(Uri
));
4952 ret
->lpIUriVtbl
= &UriVtbl
;
4959 /***********************************************************************
4960 * CreateUri (urlmon.@)
4962 * Creates a new IUri object using the URI represented by pwzURI. This function
4963 * parses and validates the components of pwzURI and then canonicalizes the
4964 * parsed components.
4967 * pwzURI [I] The URI to parse, validate, and canonicalize.
4968 * dwFlags [I] Flags which can affect how the parsing/canonicalization is performed.
4969 * dwReserved [I] Reserved (not used).
4970 * ppURI [O] The resulting IUri after parsing/canonicalization occurs.
4973 * Success: Returns S_OK. ppURI contains the pointer to the newly allocated IUri.
4974 * Failure: E_INVALIDARG if there's invalid flag combinations in dwFlags, or an
4975 * invalid parameters, or pwzURI doesn't represnt a valid URI.
4976 * E_OUTOFMEMORY if any memory allocation fails.
4980 * Uri_CREATE_CANONICALIZE, Uri_CREATE_DECODE_EXTRA_INFO, Uri_CREATE_CRACK_UNKNOWN_SCHEMES,
4981 * Uri_CREATE_PRE_PROCESS_HTML_URI, Uri_CREATE_NO_IE_SETTINGS.
4983 HRESULT WINAPI
CreateUri(LPCWSTR pwzURI
, DWORD dwFlags
, DWORD_PTR dwReserved
, IUri
**ppURI
)
4985 const DWORD supported_flags
= Uri_CREATE_ALLOW_RELATIVE
|Uri_CREATE_ALLOW_IMPLICIT_WILDCARD_SCHEME
|
4986 Uri_CREATE_ALLOW_IMPLICIT_FILE_SCHEME
|Uri_CREATE_NO_CANONICALIZE
|Uri_CREATE_CANONICALIZE
|
4987 Uri_CREATE_DECODE_EXTRA_INFO
|Uri_CREATE_NO_DECODE_EXTRA_INFO
|Uri_CREATE_CRACK_UNKNOWN_SCHEMES
|
4988 Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES
|Uri_CREATE_PRE_PROCESS_HTML_URI
|Uri_CREATE_NO_PRE_PROCESS_HTML_URI
|
4989 Uri_CREATE_NO_IE_SETTINGS
|Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS
|Uri_CREATE_FILE_USE_DOS_PATH
;
4994 TRACE("(%s %x %x %p)\n", debugstr_w(pwzURI
), dwFlags
, (DWORD
)dwReserved
, ppURI
);
4997 return E_INVALIDARG
;
4999 if(!pwzURI
|| !*pwzURI
) {
5001 return E_INVALIDARG
;
5004 /* Check for invalid flags. */
5005 if(has_invalid_flag_combination(dwFlags
)) {
5007 return E_INVALIDARG
;
5010 /* Currently unsupported. */
5011 if(dwFlags
& ~supported_flags
)
5012 FIXME("Ignoring unsupported flag(s) %x\n", dwFlags
& ~supported_flags
);
5014 ret
= create_uri_obj();
5017 return E_OUTOFMEMORY
;
5020 /* Explicitly set the default flags if it doesn't cause a flag conflict. */
5021 apply_default_flags(&dwFlags
);
5023 /* Pre process the URI, unless told otherwise. */
5024 if(!(dwFlags
& Uri_CREATE_NO_PRE_PROCESS_HTML_URI
))
5025 ret
->raw_uri
= pre_process_uri(pwzURI
);
5027 ret
->raw_uri
= SysAllocString(pwzURI
);
5031 return E_OUTOFMEMORY
;
5034 memset(&data
, 0, sizeof(parse_data
));
5035 data
.uri
= ret
->raw_uri
;
5037 /* Validate and parse the URI into it's components. */
5038 if(!parse_uri(&data
, dwFlags
)) {
5039 /* Encountered an unsupported or invalid URI */
5040 IUri_Release(URI(ret
));
5042 return E_INVALIDARG
;
5045 /* Canonicalize the URI. */
5046 hr
= canonicalize_uri(&data
, ret
, dwFlags
);
5048 IUri_Release(URI(ret
));
5053 ret
->create_flags
= dwFlags
;
5059 /***********************************************************************
5060 * CreateUriWithFragment (urlmon.@)
5062 * Creates a new IUri object. This is almost the same as CreateUri, expect that
5063 * it allows you to explicitly specify a fragment (pwzFragment) for pwzURI.
5066 * pwzURI [I] The URI to parse and perform canonicalization on.
5067 * pwzFragment [I] The explict fragment string which should be added to pwzURI.
5068 * dwFlags [I] The flags which will be passed to CreateUri.
5069 * dwReserved [I] Reserved (not used).
5070 * ppURI [O] The resulting IUri after parsing/canonicalization.
5073 * Success: S_OK. ppURI contains the pointer to the newly allocated IUri.
5074 * Failure: E_INVALIDARG if pwzURI already contains a fragment and pwzFragment
5075 * isn't NULL. Will also return E_INVALIDARG for the same reasons as
5076 * CreateUri will. E_OUTOFMEMORY if any allocations fail.
5078 HRESULT WINAPI
CreateUriWithFragment(LPCWSTR pwzURI
, LPCWSTR pwzFragment
, DWORD dwFlags
,
5079 DWORD_PTR dwReserved
, IUri
**ppURI
)
5082 TRACE("(%s %s %x %x %p)\n", debugstr_w(pwzURI
), debugstr_w(pwzFragment
), dwFlags
, (DWORD
)dwReserved
, ppURI
);
5085 return E_INVALIDARG
;
5089 return E_INVALIDARG
;
5092 /* Check if a fragment should be appended to the URI string. */
5095 DWORD uri_len
, frag_len
;
5098 /* Check if the original URI already has a fragment component. */
5099 if(StrChrW(pwzURI
, '#')) {
5101 return E_INVALIDARG
;
5104 uri_len
= lstrlenW(pwzURI
);
5105 frag_len
= lstrlenW(pwzFragment
);
5107 /* If the fragment doesn't start with a '#', one will be added. */
5108 add_pound
= *pwzFragment
!= '#';
5111 uriW
= heap_alloc((uri_len
+frag_len
+2)*sizeof(WCHAR
));
5113 uriW
= heap_alloc((uri_len
+frag_len
+1)*sizeof(WCHAR
));
5116 return E_OUTOFMEMORY
;
5118 memcpy(uriW
, pwzURI
, uri_len
*sizeof(WCHAR
));
5120 uriW
[uri_len
++] = '#';
5121 memcpy(uriW
+uri_len
, pwzFragment
, (frag_len
+1)*sizeof(WCHAR
));
5123 hres
= CreateUri(uriW
, dwFlags
, 0, ppURI
);
5127 /* A fragment string wasn't specified, so just forward the call. */
5128 hres
= CreateUri(pwzURI
, dwFlags
, 0, ppURI
);
5133 static HRESULT
build_uri(const UriBuilder
*builder
, IUri
**uri
, DWORD create_flags
,
5134 DWORD use_orig_flags
, DWORD encoding_mask
)
5143 if(encoding_mask
&& (!builder
->uri
|| builder
->modified_props
)) {
5148 /* Decide what flags should be used when creating the Uri. */
5149 if((use_orig_flags
& UriBuilder_USE_ORIGINAL_FLAGS
) && builder
->uri
)
5150 create_flags
= builder
->uri
->create_flags
;
5152 if(has_invalid_flag_combination(create_flags
)) {
5154 return E_INVALIDARG
;
5157 /* Set the default flags if they don't cause a conflict. */
5158 apply_default_flags(&create_flags
);
5161 /* Return the base IUri if no changes have been made and the create_flags match. */
5162 if(builder
->uri
&& !builder
->modified_props
&& builder
->uri
->create_flags
== create_flags
) {
5163 *uri
= URI(builder
->uri
);
5168 hr
= validate_components(builder
, &data
, create_flags
);
5174 ret
= create_uri_obj();
5177 return E_OUTOFMEMORY
;
5180 hr
= generate_uri(builder
, &data
, ret
, create_flags
);
5182 IUri_Release(URI(ret
));
5191 #define URIBUILDER_THIS(iface) DEFINE_THIS(UriBuilder, IUriBuilder, iface)
5193 static HRESULT WINAPI
UriBuilder_QueryInterface(IUriBuilder
*iface
, REFIID riid
, void **ppv
)
5195 UriBuilder
*This
= URIBUILDER_THIS(iface
);
5197 if(IsEqualGUID(&IID_IUnknown
, riid
)) {
5198 TRACE("(%p)->(IID_IUnknown %p)\n", This
, ppv
);
5199 *ppv
= URIBUILDER(This
);
5200 }else if(IsEqualGUID(&IID_IUriBuilder
, riid
)) {
5201 TRACE("(%p)->(IID_IUri %p)\n", This
, ppv
);
5202 *ppv
= URIBUILDER(This
);
5204 TRACE("(%p)->(%s %p)\n", This
, debugstr_guid(riid
), ppv
);
5206 return E_NOINTERFACE
;
5209 IUnknown_AddRef((IUnknown
*)*ppv
);
5213 static ULONG WINAPI
UriBuilder_AddRef(IUriBuilder
*iface
)
5215 UriBuilder
*This
= URIBUILDER_THIS(iface
);
5216 LONG ref
= InterlockedIncrement(&This
->ref
);
5218 TRACE("(%p) ref=%d\n", This
, ref
);
5223 static ULONG WINAPI
UriBuilder_Release(IUriBuilder
*iface
)
5225 UriBuilder
*This
= URIBUILDER_THIS(iface
);
5226 LONG ref
= InterlockedDecrement(&This
->ref
);
5228 TRACE("(%p) ref=%d\n", This
, ref
);
5231 if(This
->uri
) IUri_Release(URI(This
->uri
));
5232 heap_free(This
->fragment
);
5233 heap_free(This
->host
);
5234 heap_free(This
->password
);
5235 heap_free(This
->path
);
5236 heap_free(This
->query
);
5237 heap_free(This
->scheme
);
5238 heap_free(This
->username
);
5245 static HRESULT WINAPI
UriBuilder_CreateUriSimple(IUriBuilder
*iface
,
5246 DWORD dwAllowEncodingPropertyMask
,
5247 DWORD_PTR dwReserved
,
5250 UriBuilder
*This
= URIBUILDER_THIS(iface
);
5252 TRACE("(%p)->(%d %d %p)\n", This
, dwAllowEncodingPropertyMask
, (DWORD
)dwReserved
, ppIUri
);
5254 hr
= build_uri(This
, ppIUri
, 0, UriBuilder_USE_ORIGINAL_FLAGS
, dwAllowEncodingPropertyMask
);
5256 FIXME("(%p)->(%d %d %p)\n", This
, dwAllowEncodingPropertyMask
, (DWORD
)dwReserved
, ppIUri
);
5260 static HRESULT WINAPI
UriBuilder_CreateUri(IUriBuilder
*iface
,
5261 DWORD dwCreateFlags
,
5262 DWORD dwAllowEncodingPropertyMask
,
5263 DWORD_PTR dwReserved
,
5266 UriBuilder
*This
= URIBUILDER_THIS(iface
);
5268 TRACE("(%p)->(0x%08x %d %d %p)\n", This
, dwCreateFlags
, dwAllowEncodingPropertyMask
, (DWORD
)dwReserved
, ppIUri
);
5270 if(dwCreateFlags
== -1)
5271 hr
= build_uri(This
, ppIUri
, 0, UriBuilder_USE_ORIGINAL_FLAGS
, dwAllowEncodingPropertyMask
);
5273 hr
= build_uri(This
, ppIUri
, dwCreateFlags
, 0, dwAllowEncodingPropertyMask
);
5276 FIXME("(%p)->(0x%08x %d %d %p)\n", This
, dwCreateFlags
, dwAllowEncodingPropertyMask
, (DWORD
)dwReserved
, ppIUri
);
5280 static HRESULT WINAPI
UriBuilder_CreateUriWithFlags(IUriBuilder
*iface
,
5281 DWORD dwCreateFlags
,
5282 DWORD dwUriBuilderFlags
,
5283 DWORD dwAllowEncodingPropertyMask
,
5284 DWORD_PTR dwReserved
,
5287 UriBuilder
*This
= URIBUILDER_THIS(iface
);
5289 TRACE("(%p)->(0x%08x 0x%08x %d %d %p)\n", This
, dwCreateFlags
, dwUriBuilderFlags
,
5290 dwAllowEncodingPropertyMask
, (DWORD
)dwReserved
, ppIUri
);
5292 hr
= build_uri(This
, ppIUri
, dwCreateFlags
, dwUriBuilderFlags
, dwAllowEncodingPropertyMask
);
5294 FIXME("(%p)->(0x%08x 0x%08x %d %d %p)\n", This
, dwCreateFlags
, dwUriBuilderFlags
,
5295 dwAllowEncodingPropertyMask
, (DWORD
)dwReserved
, ppIUri
);
5299 static HRESULT WINAPI
UriBuilder_GetIUri(IUriBuilder
*iface
, IUri
**ppIUri
)
5301 UriBuilder
*This
= URIBUILDER_THIS(iface
);
5302 TRACE("(%p)->(%p)\n", This
, ppIUri
);
5308 IUri
*uri
= URI(This
->uri
);
5317 static HRESULT WINAPI
UriBuilder_SetIUri(IUriBuilder
*iface
, IUri
*pIUri
)
5319 UriBuilder
*This
= URIBUILDER_THIS(iface
);
5320 TRACE("(%p)->(%p)\n", This
, pIUri
);
5325 if((uri
= get_uri_obj(pIUri
))) {
5326 /* Only reset the builder if it's Uri isn't the same as
5327 * the Uri passed to the function.
5329 if(This
->uri
!= uri
) {
5330 reset_builder(This
);
5334 This
->port
= uri
->port
;
5339 FIXME("(%p)->(%p) Unknown IUri types not supported yet.\n", This
, pIUri
);
5342 } else if(This
->uri
)
5343 /* Only reset the builder if it's Uri isn't NULL. */
5344 reset_builder(This
);
5349 static HRESULT WINAPI
UriBuilder_GetFragment(IUriBuilder
*iface
, DWORD
*pcchFragment
, LPCWSTR
*ppwzFragment
)
5351 UriBuilder
*This
= URIBUILDER_THIS(iface
);
5352 TRACE("(%p)->(%p %p)\n", This
, pcchFragment
, ppwzFragment
);
5354 if(!This
->uri
|| This
->uri
->fragment_start
== -1 || This
->modified_props
& Uri_HAS_FRAGMENT
)
5355 return get_builder_component(&This
->fragment
, &This
->fragment_len
, NULL
, 0, ppwzFragment
, pcchFragment
);
5357 return get_builder_component(&This
->fragment
, &This
->fragment_len
, This
->uri
->canon_uri
+This
->uri
->fragment_start
,
5358 This
->uri
->fragment_len
, ppwzFragment
, pcchFragment
);
5361 static HRESULT WINAPI
UriBuilder_GetHost(IUriBuilder
*iface
, DWORD
*pcchHost
, LPCWSTR
*ppwzHost
)
5363 UriBuilder
*This
= URIBUILDER_THIS(iface
);
5364 TRACE("(%p)->(%p %p)\n", This
, pcchHost
, ppwzHost
);
5366 if(!This
->uri
|| This
->uri
->host_start
== -1 || This
->modified_props
& Uri_HAS_HOST
)
5367 return get_builder_component(&This
->host
, &This
->host_len
, NULL
, 0, ppwzHost
, pcchHost
);
5369 if(This
->uri
->host_type
== Uri_HOST_IPV6
)
5370 /* Don't include the '[' and ']' around the address. */
5371 return get_builder_component(&This
->host
, &This
->host_len
, This
->uri
->canon_uri
+This
->uri
->host_start
+1,
5372 This
->uri
->host_len
-2, ppwzHost
, pcchHost
);
5374 return get_builder_component(&This
->host
, &This
->host_len
, This
->uri
->canon_uri
+This
->uri
->host_start
,
5375 This
->uri
->host_len
, ppwzHost
, pcchHost
);
5379 static HRESULT WINAPI
UriBuilder_GetPassword(IUriBuilder
*iface
, DWORD
*pcchPassword
, LPCWSTR
*ppwzPassword
)
5381 UriBuilder
*This
= URIBUILDER_THIS(iface
);
5382 TRACE("(%p)->(%p %p)\n", This
, pcchPassword
, ppwzPassword
);
5384 if(!This
->uri
|| This
->uri
->userinfo_split
== -1 || This
->modified_props
& Uri_HAS_PASSWORD
)
5385 return get_builder_component(&This
->password
, &This
->password_len
, NULL
, 0, ppwzPassword
, pcchPassword
);
5387 const WCHAR
*start
= This
->uri
->canon_uri
+This
->uri
->userinfo_start
+This
->uri
->userinfo_split
+1;
5388 DWORD len
= This
->uri
->userinfo_len
-This
->uri
->userinfo_split
-1;
5389 return get_builder_component(&This
->password
, &This
->password_len
, start
, len
, ppwzPassword
, pcchPassword
);
5393 static HRESULT WINAPI
UriBuilder_GetPath(IUriBuilder
*iface
, DWORD
*pcchPath
, LPCWSTR
*ppwzPath
)
5395 UriBuilder
*This
= URIBUILDER_THIS(iface
);
5396 TRACE("(%p)->(%p %p)\n", This
, pcchPath
, ppwzPath
);
5398 if(!This
->uri
|| This
->uri
->path_start
== -1 || This
->modified_props
& Uri_HAS_PATH
)
5399 return get_builder_component(&This
->path
, &This
->path_len
, NULL
, 0, ppwzPath
, pcchPath
);
5401 return get_builder_component(&This
->path
, &This
->path_len
, This
->uri
->canon_uri
+This
->uri
->path_start
,
5402 This
->uri
->path_len
, ppwzPath
, pcchPath
);
5405 static HRESULT WINAPI
UriBuilder_GetPort(IUriBuilder
*iface
, BOOL
*pfHasPort
, DWORD
*pdwPort
)
5407 UriBuilder
*This
= URIBUILDER_THIS(iface
);
5408 TRACE("(%p)->(%p %p)\n", This
, pfHasPort
, pdwPort
);
5421 *pfHasPort
= This
->has_port
;
5422 *pdwPort
= This
->port
;
5426 static HRESULT WINAPI
UriBuilder_GetQuery(IUriBuilder
*iface
, DWORD
*pcchQuery
, LPCWSTR
*ppwzQuery
)
5428 UriBuilder
*This
= URIBUILDER_THIS(iface
);
5429 TRACE("(%p)->(%p %p)\n", This
, pcchQuery
, ppwzQuery
);
5431 if(!This
->uri
|| This
->uri
->query_start
== -1 || This
->modified_props
& Uri_HAS_QUERY
)
5432 return get_builder_component(&This
->query
, &This
->query_len
, NULL
, 0, ppwzQuery
, pcchQuery
);
5434 return get_builder_component(&This
->query
, &This
->query_len
, This
->uri
->canon_uri
+This
->uri
->query_start
,
5435 This
->uri
->query_len
, ppwzQuery
, pcchQuery
);
5438 static HRESULT WINAPI
UriBuilder_GetSchemeName(IUriBuilder
*iface
, DWORD
*pcchSchemeName
, LPCWSTR
*ppwzSchemeName
)
5440 UriBuilder
*This
= URIBUILDER_THIS(iface
);
5441 TRACE("(%p)->(%p %p)\n", This
, pcchSchemeName
, ppwzSchemeName
);
5443 if(!This
->uri
|| This
->uri
->scheme_start
== -1 || This
->modified_props
& Uri_HAS_SCHEME_NAME
)
5444 return get_builder_component(&This
->scheme
, &This
->scheme_len
, NULL
, 0, ppwzSchemeName
, pcchSchemeName
);
5446 return get_builder_component(&This
->scheme
, &This
->scheme_len
, This
->uri
->canon_uri
+This
->uri
->scheme_start
,
5447 This
->uri
->scheme_len
, ppwzSchemeName
, pcchSchemeName
);
5450 static HRESULT WINAPI
UriBuilder_GetUserName(IUriBuilder
*iface
, DWORD
*pcchUserName
, LPCWSTR
*ppwzUserName
)
5452 UriBuilder
*This
= URIBUILDER_THIS(iface
);
5453 TRACE("(%p)->(%p %p)\n", This
, pcchUserName
, ppwzUserName
);
5455 if(!This
->uri
|| This
->uri
->userinfo_start
== -1 || This
->uri
->userinfo_split
== 0 ||
5456 This
->modified_props
& Uri_HAS_USER_NAME
)
5457 return get_builder_component(&This
->username
, &This
->username_len
, NULL
, 0, ppwzUserName
, pcchUserName
);
5459 const WCHAR
*start
= This
->uri
->canon_uri
+This
->uri
->userinfo_start
;
5461 /* Check if there's a password in the userinfo section. */
5462 if(This
->uri
->userinfo_split
> -1)
5463 /* Don't include the password. */
5464 return get_builder_component(&This
->username
, &This
->username_len
, start
,
5465 This
->uri
->userinfo_split
, ppwzUserName
, pcchUserName
);
5467 return get_builder_component(&This
->username
, &This
->username_len
, start
,
5468 This
->uri
->userinfo_len
, ppwzUserName
, pcchUserName
);
5472 static HRESULT WINAPI
UriBuilder_SetFragment(IUriBuilder
*iface
, LPCWSTR pwzNewValue
)
5474 UriBuilder
*This
= URIBUILDER_THIS(iface
);
5475 TRACE("(%p)->(%s)\n", This
, debugstr_w(pwzNewValue
));
5476 return set_builder_component(&This
->fragment
, &This
->fragment_len
, pwzNewValue
, '#',
5477 &This
->modified_props
, Uri_HAS_FRAGMENT
);
5480 static HRESULT WINAPI
UriBuilder_SetHost(IUriBuilder
*iface
, LPCWSTR pwzNewValue
)
5482 UriBuilder
*This
= URIBUILDER_THIS(iface
);
5483 TRACE("(%p)->(%s)\n", This
, debugstr_w(pwzNewValue
));
5485 /* Host name can't be set to NULL. */
5487 return E_INVALIDARG
;
5489 return set_builder_component(&This
->host
, &This
->host_len
, pwzNewValue
, 0,
5490 &This
->modified_props
, Uri_HAS_HOST
);
5493 static HRESULT WINAPI
UriBuilder_SetPassword(IUriBuilder
*iface
, LPCWSTR pwzNewValue
)
5495 UriBuilder
*This
= URIBUILDER_THIS(iface
);
5496 TRACE("(%p)->(%s)\n", This
, debugstr_w(pwzNewValue
));
5497 return set_builder_component(&This
->password
, &This
->password_len
, pwzNewValue
, 0,
5498 &This
->modified_props
, Uri_HAS_PASSWORD
);
5501 static HRESULT WINAPI
UriBuilder_SetPath(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
->path
, &This
->path_len
, pwzNewValue
, 0,
5506 &This
->modified_props
, Uri_HAS_PATH
);
5509 static HRESULT WINAPI
UriBuilder_SetPort(IUriBuilder
*iface
, BOOL fHasPort
, DWORD dwNewValue
)
5511 UriBuilder
*This
= URIBUILDER_THIS(iface
);
5512 TRACE("(%p)->(%d %d)\n", This
, fHasPort
, dwNewValue
);
5514 This
->has_port
= fHasPort
;
5515 This
->port
= dwNewValue
;
5516 This
->modified_props
|= Uri_HAS_PORT
;
5520 static HRESULT WINAPI
UriBuilder_SetQuery(IUriBuilder
*iface
, LPCWSTR pwzNewValue
)
5522 UriBuilder
*This
= URIBUILDER_THIS(iface
);
5523 TRACE("(%p)->(%s)\n", This
, debugstr_w(pwzNewValue
));
5524 return set_builder_component(&This
->query
, &This
->query_len
, pwzNewValue
, '?',
5525 &This
->modified_props
, Uri_HAS_QUERY
);
5528 static HRESULT WINAPI
UriBuilder_SetSchemeName(IUriBuilder
*iface
, LPCWSTR pwzNewValue
)
5530 UriBuilder
*This
= URIBUILDER_THIS(iface
);
5531 TRACE("(%p)->(%s)\n", This
, debugstr_w(pwzNewValue
));
5533 /* Only set the scheme name if it's not NULL or empty. */
5534 if(!pwzNewValue
|| !*pwzNewValue
)
5535 return E_INVALIDARG
;
5537 return set_builder_component(&This
->scheme
, &This
->scheme_len
, pwzNewValue
, 0,
5538 &This
->modified_props
, Uri_HAS_SCHEME_NAME
);
5541 static HRESULT WINAPI
UriBuilder_SetUserName(IUriBuilder
*iface
, LPCWSTR pwzNewValue
)
5543 UriBuilder
*This
= URIBUILDER_THIS(iface
);
5544 TRACE("(%p)->(%s)\n", This
, debugstr_w(pwzNewValue
));
5545 return set_builder_component(&This
->username
, &This
->username_len
, pwzNewValue
, 0,
5546 &This
->modified_props
, Uri_HAS_USER_NAME
);
5549 static HRESULT WINAPI
UriBuilder_RemoveProperties(IUriBuilder
*iface
, DWORD dwPropertyMask
)
5551 const DWORD accepted_flags
= Uri_HAS_AUTHORITY
|Uri_HAS_DOMAIN
|Uri_HAS_EXTENSION
|Uri_HAS_FRAGMENT
|Uri_HAS_HOST
|
5552 Uri_HAS_PASSWORD
|Uri_HAS_PATH
|Uri_HAS_PATH_AND_QUERY
|Uri_HAS_QUERY
|
5553 Uri_HAS_USER_INFO
|Uri_HAS_USER_NAME
;
5555 UriBuilder
*This
= URIBUILDER_THIS(iface
);
5556 TRACE("(%p)->(0x%08x)\n", This
, dwPropertyMask
);
5558 if(dwPropertyMask
& ~accepted_flags
)
5559 return E_INVALIDARG
;
5561 if(dwPropertyMask
& Uri_HAS_FRAGMENT
)
5562 UriBuilder_SetFragment(iface
, NULL
);
5564 /* Even though you can't set the host name to NULL or an
5565 * empty string, you can still remove it... for some reason.
5567 if(dwPropertyMask
& Uri_HAS_HOST
)
5568 set_builder_component(&This
->host
, &This
->host_len
, NULL
, 0,
5569 &This
->modified_props
, Uri_HAS_HOST
);
5571 if(dwPropertyMask
& Uri_HAS_PASSWORD
)
5572 UriBuilder_SetPassword(iface
, NULL
);
5574 if(dwPropertyMask
& Uri_HAS_PATH
)
5575 UriBuilder_SetPath(iface
, NULL
);
5577 if(dwPropertyMask
& Uri_HAS_PORT
)
5578 UriBuilder_SetPort(iface
, FALSE
, 0);
5580 if(dwPropertyMask
& Uri_HAS_QUERY
)
5581 UriBuilder_SetQuery(iface
, NULL
);
5583 if(dwPropertyMask
& Uri_HAS_USER_NAME
)
5584 UriBuilder_SetUserName(iface
, NULL
);
5589 static HRESULT WINAPI
UriBuilder_HasBeenModified(IUriBuilder
*iface
, BOOL
*pfModified
)
5591 UriBuilder
*This
= URIBUILDER_THIS(iface
);
5592 TRACE("(%p)->(%p)\n", This
, pfModified
);
5597 *pfModified
= This
->modified_props
> 0;
5601 #undef URIBUILDER_THIS
5603 static const IUriBuilderVtbl UriBuilderVtbl
= {
5604 UriBuilder_QueryInterface
,
5607 UriBuilder_CreateUriSimple
,
5608 UriBuilder_CreateUri
,
5609 UriBuilder_CreateUriWithFlags
,
5612 UriBuilder_GetFragment
,
5614 UriBuilder_GetPassword
,
5617 UriBuilder_GetQuery
,
5618 UriBuilder_GetSchemeName
,
5619 UriBuilder_GetUserName
,
5620 UriBuilder_SetFragment
,
5622 UriBuilder_SetPassword
,
5625 UriBuilder_SetQuery
,
5626 UriBuilder_SetSchemeName
,
5627 UriBuilder_SetUserName
,
5628 UriBuilder_RemoveProperties
,
5629 UriBuilder_HasBeenModified
,
5632 /***********************************************************************
5633 * CreateIUriBuilder (urlmon.@)
5635 HRESULT WINAPI
CreateIUriBuilder(IUri
*pIUri
, DWORD dwFlags
, DWORD_PTR dwReserved
, IUriBuilder
**ppIUriBuilder
)
5639 TRACE("(%p %x %x %p)\n", pIUri
, dwFlags
, (DWORD
)dwReserved
, ppIUriBuilder
);
5644 ret
= heap_alloc_zero(sizeof(UriBuilder
));
5646 return E_OUTOFMEMORY
;
5648 ret
->lpIUriBuilderVtbl
= &UriBuilderVtbl
;
5654 if((uri
= get_uri_obj(pIUri
))) {
5659 /* Windows doesn't set 'has_port' to TRUE in this case. */
5660 ret
->port
= uri
->port
;
5664 *ppIUriBuilder
= NULL
;
5665 FIXME("(%p %x %x %p): Unknown IUri types not supported yet.\n", pIUri
, dwFlags
,
5666 (DWORD
)dwReserved
, ppIUriBuilder
);
5671 *ppIUriBuilder
= URIBUILDER(ret
);
5675 /* Merges the base path with the relative path and stores the resulting path
5676 * and path len in 'result' and 'result_len'.
5678 static HRESULT
merge_paths(parse_data
*data
, const WCHAR
*base
, DWORD base_len
, const WCHAR
*relative
,
5679 DWORD relative_len
, WCHAR
**result
, DWORD
*result_len
, DWORD flags
)
5681 const WCHAR
*end
= NULL
;
5682 DWORD base_copy_len
= 0;
5686 /* Find the characters the will be copied over from
5689 end
= str_last_of(base
, base
+(base_len
-1), '/');
5690 if(!end
&& data
->scheme_type
== URL_SCHEME_FILE
)
5691 /* Try looking for a '\\'. */
5692 end
= str_last_of(base
, base
+(base_len
-1), '\\');
5696 base_copy_len
= (end
+1)-base
;
5697 *result
= heap_alloc((base_copy_len
+relative_len
+1)*sizeof(WCHAR
));
5699 *result
= heap_alloc((relative_len
+1)*sizeof(WCHAR
));
5703 return E_OUTOFMEMORY
;
5708 memcpy(ptr
, base
, base_copy_len
*sizeof(WCHAR
));
5709 ptr
+= base_copy_len
;
5712 memcpy(ptr
, relative
, relative_len
*sizeof(WCHAR
));
5713 ptr
+= relative_len
;
5716 *result_len
= (ptr
-*result
);
5720 static HRESULT
combine_uri(Uri
*base
, Uri
*relative
, DWORD flags
, IUri
**result
) {
5724 DWORD create_flags
= 0, len
= 0;
5726 memset(&data
, 0, sizeof(parse_data
));
5728 /* Base case is when the relative Uri has a scheme name,
5729 * if it does, then 'result' will contain the same data
5730 * as the relative Uri.
5732 if(relative
->scheme_start
> -1) {
5733 data
.uri
= SysAllocString(relative
->raw_uri
);
5736 return E_OUTOFMEMORY
;
5739 parse_uri(&data
, 0);
5741 ret
= create_uri_obj();
5744 return E_OUTOFMEMORY
;
5747 ret
->raw_uri
= data
.uri
;
5748 hr
= canonicalize_uri(&data
, ret
, 0);
5750 IUri_Release(URI(ret
));
5755 apply_default_flags(&create_flags
);
5756 ret
->create_flags
= create_flags
;
5761 DWORD raw_flags
= 0;
5763 if(base
->scheme_start
> -1) {
5764 data
.scheme
= base
->canon_uri
+base
->scheme_start
;
5765 data
.scheme_len
= base
->scheme_len
;
5766 data
.scheme_type
= base
->scheme_type
;
5768 data
.is_relative
= TRUE
;
5769 data
.scheme_type
= URL_SCHEME_UNKNOWN
;
5770 create_flags
|= Uri_CREATE_ALLOW_RELATIVE
;
5773 if(base
->authority_start
> -1) {
5774 if(base
->userinfo_start
> -1 && base
->userinfo_split
!= 0) {
5775 data
.username
= base
->canon_uri
+base
->userinfo_start
;
5776 data
.username_len
= (base
->userinfo_split
> -1) ? base
->userinfo_split
: base
->userinfo_len
;
5779 if(base
->userinfo_split
> -1) {
5780 data
.password
= base
->canon_uri
+base
->userinfo_start
+base
->userinfo_split
+1;
5781 data
.password_len
= base
->userinfo_len
-base
->userinfo_split
-1;
5784 if(base
->host_start
> -1) {
5785 data
.host
= base
->canon_uri
+base
->host_start
;
5786 data
.host_len
= base
->host_len
;
5787 data
.host_type
= base
->host_type
;
5790 if(base
->has_port
) {
5791 data
.has_port
= TRUE
;
5792 data
.port_value
= base
->port
;
5794 } else if(base
->scheme_type
!= URL_SCHEME_FILE
)
5795 data
.is_opaque
= TRUE
;
5797 if(relative
->path_start
== -1 || !relative
->path_len
) {
5798 if(base
->path_start
> -1) {
5799 data
.path
= base
->canon_uri
+base
->path_start
;
5800 data
.path_len
= base
->path_len
;
5801 } else if((base
->path_start
== -1 || !base
->path_len
) && !data
.is_opaque
) {
5802 /* Just set the path as a '/' if the base didn't have
5803 * one and if it's an hierarchical URI.
5805 static const WCHAR slashW
[] = {'/',0};
5810 if(relative
->query_start
> -1) {
5811 data
.query
= relative
->canon_uri
+relative
->query_start
;
5812 data
.query_len
= relative
->query_len
;
5813 } else if(base
->query_start
> -1) {
5814 data
.query
= base
->canon_uri
+base
->query_start
;
5815 data
.query_len
= base
->query_len
;
5818 const WCHAR
*ptr
, **pptr
;
5819 DWORD path_offset
= 0, path_len
= 0;
5821 /* There's two possibilities on what will happen to the path component
5822 * of the result IUri. First, if the relative path begins with a '/'
5823 * then the resulting path will just be the relative path. Second, if
5824 * relative path doesn't begin with a '/' then the base path and relative
5825 * path are merged together.
5827 if(relative
->path_len
&& *(relative
->canon_uri
+relative
->path_start
) == '/') {
5829 BOOL copy_drive_path
= FALSE
;
5831 /* If the relative IUri's path starts with a '/', then we
5832 * don't use the base IUri's path. Unless the base IUri
5833 * is a file URI, in which case it uses the drive path of
5834 * the base IUri (if it has any) in the new path.
5836 if(base
->scheme_type
== URL_SCHEME_FILE
) {
5837 if(base
->path_len
> 3 && *(base
->canon_uri
+base
->path_start
) == '/' &&
5838 is_drive_path(base
->canon_uri
+base
->path_start
+1)) {
5840 copy_drive_path
= TRUE
;
5844 path_len
+= relative
->path_len
;
5846 path
= heap_alloc((path_len
+1)*sizeof(WCHAR
));
5849 return E_OUTOFMEMORY
;
5854 /* Copy the base paths, drive path over. */
5855 if(copy_drive_path
) {
5856 memcpy(tmp
, base
->canon_uri
+base
->path_start
, 3*sizeof(WCHAR
));
5860 memcpy(tmp
, relative
->canon_uri
+relative
->path_start
, relative
->path_len
*sizeof(WCHAR
));
5861 path
[path_len
] = '\0';
5863 /* Merge the base path with the relative path. */
5864 hr
= merge_paths(&data
, base
->canon_uri
+base
->path_start
, base
->path_len
,
5865 relative
->canon_uri
+relative
->path_start
, relative
->path_len
,
5866 &path
, &path_len
, flags
);
5872 /* If the resulting IUri is a file URI, the drive path isn't
5873 * reduced out when the dot segments are removed.
5875 if(path_len
>= 3 && data
.scheme_type
== URL_SCHEME_FILE
&& !data
.host
) {
5876 if(*path
== '/' && is_drive_path(path
+1))
5878 else if(is_drive_path(path
))
5883 /* Check if the dot segments need to be removed from the path. */
5884 if(!(flags
& URL_DONT_SIMPLIFY
) && !data
.is_opaque
) {
5885 DWORD offset
= (path_offset
> 0) ? path_offset
+1 : 0;
5886 DWORD new_len
= remove_dot_segments(path
+offset
,path_len
-offset
);
5888 if(new_len
!= path_len
) {
5889 WCHAR
*tmp
= heap_realloc(path
, (path_offset
+new_len
+1)*sizeof(WCHAR
));
5893 return E_OUTOFMEMORY
;
5896 tmp
[new_len
+offset
] = '\0';
5898 path_len
= new_len
+offset
;
5902 /* Make sure the path component is valid. */
5905 if((data
.is_opaque
&& !parse_path_opaque(pptr
, &data
, 0)) ||
5906 (!data
.is_opaque
&& !parse_path_hierarchical(pptr
, &data
, 0))) {
5909 return E_INVALIDARG
;
5913 if(relative
->fragment_start
> -1) {
5914 data
.fragment
= relative
->canon_uri
+relative
->fragment_start
;
5915 data
.fragment_len
= relative
->fragment_len
;
5918 if(flags
& URL_DONT_SIMPLIFY
)
5919 raw_flags
|= RAW_URI_FORCE_PORT_DISP
;
5920 if(flags
& URL_FILE_USE_PATHURL
)
5921 raw_flags
|= RAW_URI_CONVERT_TO_DOS_PATH
;
5923 len
= generate_raw_uri(&data
, data
.uri
, raw_flags
);
5924 data
.uri
= SysAllocStringLen(NULL
, len
);
5928 return E_OUTOFMEMORY
;
5931 generate_raw_uri(&data
, data
.uri
, raw_flags
);
5933 ret
= create_uri_obj();
5935 SysFreeString(data
.uri
);
5938 return E_OUTOFMEMORY
;
5941 if(flags
& URL_DONT_SIMPLIFY
)
5942 create_flags
|= Uri_CREATE_NO_CANONICALIZE
;
5943 if(flags
& URL_FILE_USE_PATHURL
)
5944 create_flags
|= Uri_CREATE_FILE_USE_DOS_PATH
;
5946 ret
->raw_uri
= data
.uri
;
5947 hr
= canonicalize_uri(&data
, ret
, create_flags
);
5949 IUri_Release(URI(ret
));
5954 if(flags
& URL_DONT_SIMPLIFY
)
5955 ret
->display_modifiers
|= URI_DISPLAY_NO_DEFAULT_PORT_AUTH
;
5957 apply_default_flags(&create_flags
);
5958 ret
->create_flags
= create_flags
;
5967 /***********************************************************************
5968 * CoInternetCombineIUri (urlmon.@)
5970 HRESULT WINAPI
CoInternetCombineIUri(IUri
*pBaseUri
, IUri
*pRelativeUri
, DWORD dwCombineFlags
,
5971 IUri
**ppCombinedUri
, DWORD_PTR dwReserved
)
5974 IInternetProtocolInfo
*info
;
5975 Uri
*relative
, *base
;
5976 TRACE("(%p %p %x %p %x)\n", pBaseUri
, pRelativeUri
, dwCombineFlags
, ppCombinedUri
, (DWORD
)dwReserved
);
5979 return E_INVALIDARG
;
5981 if(!pBaseUri
|| !pRelativeUri
) {
5982 *ppCombinedUri
= NULL
;
5983 return E_INVALIDARG
;
5986 relative
= get_uri_obj(pRelativeUri
);
5987 base
= get_uri_obj(pBaseUri
);
5988 if(!relative
|| !base
) {
5989 *ppCombinedUri
= NULL
;
5990 FIXME("(%p %p %x %p %x) Unknown IUri types not supported yet.\n",
5991 pBaseUri
, pRelativeUri
, dwCombineFlags
, ppCombinedUri
, (DWORD
)dwReserved
);
5995 info
= get_protocol_info(base
->canon_uri
);
5997 WCHAR result
[INTERNET_MAX_URL_LENGTH
+1];
5998 DWORD result_len
= 0;
6000 hr
= IInternetProtocolInfo_CombineUrl(info
, base
->canon_uri
, relative
->canon_uri
, dwCombineFlags
,
6001 result
, INTERNET_MAX_URL_LENGTH
+1, &result_len
, 0);
6002 IInternetProtocolInfo_Release(info
);
6004 hr
= CreateUri(result
, Uri_CREATE_ALLOW_RELATIVE
, 0, ppCombinedUri
);
6010 return combine_uri(base
, relative
, dwCombineFlags
, ppCombinedUri
);