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 BOOL inside_brackets
= has_start_bracket
;
1556 BOOL ignore_col
= extras
& IGNORE_PORT_DELIMITER
;
1558 /* We have to be careful with file schemes. */
1559 if(data
->scheme_type
== URL_SCHEME_FILE
) {
1560 /* This is because an implicit file scheme could be "C:\\test" and it
1561 * would trick this function into thinking the host is "C", when after
1562 * canonicalization the host would end up being an empty string. A drive
1563 * path can also have a '|' instead of a ':' after the drive letter.
1565 if(is_drive_path(*ptr
)) {
1566 /* Regular old drive paths don't have a host type (or host name). */
1567 data
->host_type
= Uri_HOST_UNKNOWN
;
1571 } else if(is_unc_path(*ptr
))
1572 /* Skip past the "\\" of a UNC path. */
1578 while(!is_auth_delim(**ptr
, known_scheme
)) {
1579 if(**ptr
== ':' && !ignore_col
) {
1580 /* We can ignore ':' if were inside brackets.*/
1581 if(!inside_brackets
) {
1582 const WCHAR
*tmp
= (*ptr
)++;
1584 /* Attempt to parse the port. */
1585 if(!parse_port(ptr
, data
, flags
)) {
1586 /* Windows expects there to be a valid port for known scheme types. */
1587 if(data
->scheme_type
!= URL_SCHEME_UNKNOWN
) {
1590 TRACE("(%p %p %x %x): Expected valid port\n", ptr
, data
, flags
, extras
);
1593 /* Windows gives up on trying to parse a port when it
1594 * encounters 1 invalid port.
1598 data
->host_len
= tmp
- data
->host
;
1602 } else if(**ptr
== '%' && known_scheme
) {
1603 /* Has to be a legit % encoded value. */
1604 if(!check_pct_encoded(ptr
)) {
1610 } else if(**ptr
== ']')
1611 inside_brackets
= FALSE
;
1612 else if(**ptr
== '[')
1613 inside_brackets
= TRUE
;
1618 if(has_start_bracket
) {
1619 /* Make sure the last character of the host wasn't a ']'. */
1620 if(*(*ptr
-1) == ']') {
1621 TRACE("(%p %p %x %x): Expected an IP literal inside of the host\n",
1622 ptr
, data
, flags
, extras
);
1629 /* Don't overwrite our length if we found a port earlier. */
1631 data
->host_len
= *ptr
- data
->host
;
1633 /* If the host is empty, then it's an unknown host type. */
1634 if(data
->host_len
== 0)
1635 data
->host_type
= Uri_HOST_UNKNOWN
;
1637 data
->host_type
= Uri_HOST_DNS
;
1639 TRACE("(%p %p %x %x): Parsed reg-name. host=%s len=%d\n", ptr
, data
, flags
, extras
,
1640 debugstr_wn(data
->host
, data
->host_len
), data
->host_len
);
1644 /* Attempts to parse an IPv6 address out of the URI.
1646 * IPv6address = 6( h16 ":" ) ls32
1647 * / "::" 5( h16 ":" ) ls32
1648 * / [ h16 ] "::" 4( h16 ":" ) ls32
1649 * / [ *1( h16 ":" ) h16 ] "::" 3( h16 ":" ) ls32
1650 * / [ *2( h16 ":" ) h16 ] "::" 2( h16 ":" ) ls32
1651 * / [ *3( h16 ":" ) h16 ] "::" h16 ":" ls32
1652 * / [ *4( h16 ":" ) h16 ] "::" ls32
1653 * / [ *5( h16 ":" ) h16 ] "::" h16
1654 * / [ *6( h16 ":" ) h16 ] "::"
1656 * ls32 = ( h16 ":" h16 ) / IPv4address
1657 * ; least-significant 32 bits of address.
1660 * ; 16 bits of address represented in hexadecimal.
1662 * Modeled after google-url's 'DoParseIPv6' function.
1664 static BOOL
parse_ipv6address(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
) {
1665 const WCHAR
*start
, *cur_start
;
1668 start
= cur_start
= *ptr
;
1669 memset(&ip
, 0, sizeof(ipv6_address
));
1672 /* Check if we're on the last character of the host. */
1673 BOOL is_end
= (is_auth_delim(**ptr
, data
->scheme_type
!= URL_SCHEME_UNKNOWN
)
1676 BOOL is_split
= (**ptr
== ':');
1677 BOOL is_elision
= (is_split
&& !is_end
&& *(*ptr
+1) == ':');
1679 /* Check if we're at the end of a component, or
1680 * if we're at the end of the IPv6 address.
1682 if(is_split
|| is_end
) {
1685 cur_len
= *ptr
- cur_start
;
1687 /* h16 can't have a length > 4. */
1691 TRACE("(%p %p %x): h16 component to long.\n",
1697 /* An h16 component can't have the length of 0 unless
1698 * the elision is at the beginning of the address, or
1699 * at the end of the address.
1701 if(!((*ptr
== start
&& is_elision
) ||
1702 (is_end
&& (*ptr
-2) == ip
.elision
))) {
1704 TRACE("(%p %p %x): IPv6 component cannot have a length of 0.\n",
1711 /* An IPv6 address can have no more than 8 h16 components. */
1712 if(ip
.h16_count
>= 8) {
1714 TRACE("(%p %p %x): Not a IPv6 address, to many h16 components.\n",
1719 ip
.components
[ip
.h16_count
].str
= cur_start
;
1720 ip
.components
[ip
.h16_count
].len
= cur_len
;
1722 TRACE("(%p %p %x): Found h16 component %s, len=%d, h16_count=%d\n",
1723 ptr
, data
, flags
, debugstr_wn(cur_start
, cur_len
), cur_len
,
1733 /* A IPv6 address can only have 1 elision ('::'). */
1737 TRACE("(%p %p %x): IPv6 address cannot have 2 elisions.\n",
1749 if(!check_ipv4address(ptr
, TRUE
)) {
1750 if(!is_hexdigit(**ptr
)) {
1751 /* Not a valid character for an IPv6 address. */
1756 /* Found an IPv4 address. */
1757 ip
.ipv4
= cur_start
;
1758 ip
.ipv4_len
= *ptr
- cur_start
;
1760 TRACE("(%p %p %x): Found an attached IPv4 address %s len=%d.\n",
1761 ptr
, data
, flags
, debugstr_wn(ip
.ipv4
, ip
.ipv4_len
),
1764 /* IPv4 addresses can only appear at the end of a IPv6. */
1770 compute_ipv6_comps_size(&ip
);
1772 /* Make sure the IPv6 address adds up to 16 bytes. */
1773 if(ip
.components_size
+ ip
.elision_size
!= 16) {
1775 TRACE("(%p %p %x): Invalid IPv6 address, did not add up to 16 bytes.\n",
1780 if(ip
.elision_size
== 2) {
1781 /* For some reason on Windows if an elision that represents
1782 * only 1 h16 component is encountered at the very begin or
1783 * end of an IPv6 address, Windows does not consider it a
1784 * valid IPv6 address.
1786 * Ex: [::2:3:4:5:6:7] is not valid, even though the sum
1787 * of all the components == 128bits.
1789 if(ip
.elision
< ip
.components
[0].str
||
1790 ip
.elision
> ip
.components
[ip
.h16_count
-1].str
) {
1792 TRACE("(%p %p %x): Invalid IPv6 address. Detected elision of 2 bytes at the beginning or end of the address.\n",
1798 data
->host_type
= Uri_HOST_IPV6
;
1799 data
->has_ipv6
= TRUE
;
1800 data
->ipv6_address
= ip
;
1802 TRACE("(%p %p %x): Found valid IPv6 literal %s len=%d\n",
1803 ptr
, data
, flags
, debugstr_wn(start
, *ptr
-start
),
1808 /* IPvFuture = "v" 1*HEXDIG "." 1*( unreserved / sub-delims / ":" ) */
1809 static BOOL
parse_ipvfuture(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
) {
1810 const WCHAR
*start
= *ptr
;
1812 /* IPvFuture has to start with a 'v' or 'V'. */
1813 if(**ptr
!= 'v' && **ptr
!= 'V')
1816 /* Following the v there must be at least 1 hex digit. */
1818 if(!is_hexdigit(**ptr
)) {
1824 while(is_hexdigit(**ptr
))
1827 /* End of the hexdigit sequence must be a '.' */
1834 if(!is_unreserved(**ptr
) && !is_subdelim(**ptr
) && **ptr
!= ':') {
1840 while(is_unreserved(**ptr
) || is_subdelim(**ptr
) || **ptr
== ':')
1843 data
->host_type
= Uri_HOST_UNKNOWN
;
1845 TRACE("(%p %p %x): Parsed IPvFuture address %s len=%d\n", ptr
, data
, flags
,
1846 debugstr_wn(start
, *ptr
-start
), *ptr
-start
);
1851 /* IP-literal = "[" ( IPv6address / IPvFuture ) "]" */
1852 static BOOL
parse_ip_literal(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
, DWORD extras
) {
1855 if(**ptr
!= '[' && !(extras
& ALLOW_BRACKETLESS_IP_LITERAL
)) {
1858 } else if(**ptr
== '[')
1861 if(!parse_ipv6address(ptr
, data
, flags
)) {
1862 if(extras
& SKIP_IP_FUTURE_CHECK
|| !parse_ipvfuture(ptr
, data
, flags
)) {
1869 if(**ptr
!= ']' && !(extras
& ALLOW_BRACKETLESS_IP_LITERAL
)) {
1873 } else if(!**ptr
&& extras
& ALLOW_BRACKETLESS_IP_LITERAL
) {
1874 /* The IP literal didn't contain brackets and was followed by
1875 * a NULL terminator, so no reason to even check the port.
1877 data
->host_len
= *ptr
- data
->host
;
1884 /* If a valid port is not found, then let it trickle down to
1887 if(!parse_port(ptr
, data
, flags
)) {
1893 data
->host_len
= *ptr
- data
->host
;
1898 /* Parses the host information from the URI.
1900 * host = IP-literal / IPv4address / reg-name
1902 static BOOL
parse_host(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
, DWORD extras
) {
1903 if(!parse_ip_literal(ptr
, data
, flags
, extras
)) {
1904 if(!parse_ipv4address(ptr
, data
, flags
)) {
1905 if(!parse_reg_name(ptr
, data
, flags
, extras
)) {
1906 TRACE("(%p %p %x %x): Malformed URI, Unknown host type.\n",
1907 ptr
, data
, flags
, extras
);
1916 /* Parses the authority information from the URI.
1918 * authority = [ userinfo "@" ] host [ ":" port ]
1920 static BOOL
parse_authority(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
) {
1921 parse_userinfo(ptr
, data
, flags
);
1923 /* Parsing the port will happen during one of the host parsing
1924 * routines (if the URI has a port).
1926 if(!parse_host(ptr
, data
, flags
, 0))
1932 /* Attempts to parse the path information of a hierarchical URI. */
1933 static BOOL
parse_path_hierarchical(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
) {
1934 const WCHAR
*start
= *ptr
;
1935 static const WCHAR slash
[] = {'/',0};
1936 const BOOL is_file
= data
->scheme_type
== URL_SCHEME_FILE
;
1938 if(is_path_delim(**ptr
)) {
1939 if(data
->scheme_type
== URL_SCHEME_WILDCARD
) {
1940 /* Wildcard schemes don't get a '/' attached if their path is
1945 } else if(!(flags
& Uri_CREATE_NO_CANONICALIZE
)) {
1946 /* If the path component is empty, then a '/' is added. */
1951 while(!is_path_delim(**ptr
)) {
1952 if(**ptr
== '%' && data
->scheme_type
!= URL_SCHEME_UNKNOWN
&& !is_file
) {
1953 if(!check_pct_encoded(ptr
)) {
1958 } else if(is_forbidden_dos_path_char(**ptr
) && is_file
&&
1959 (flags
& Uri_CREATE_FILE_USE_DOS_PATH
)) {
1960 /* File schemes with USE_DOS_PATH set aren't allowed to have
1961 * a '<' or '>' or '\"' appear in them.
1965 } else if(**ptr
== '\\') {
1966 /* Not allowed to have a backslash if NO_CANONICALIZE is set
1967 * and the scheme is known type (but not a file scheme).
1969 if(flags
& Uri_CREATE_NO_CANONICALIZE
) {
1970 if(data
->scheme_type
!= URL_SCHEME_FILE
&&
1971 data
->scheme_type
!= URL_SCHEME_UNKNOWN
) {
1981 /* The only time a URI doesn't have a path is when
1982 * the NO_CANONICALIZE flag is set and the raw URI
1983 * didn't contain one.
1990 data
->path_len
= *ptr
- start
;
1995 TRACE("(%p %p %x): Parsed path %s len=%d\n", ptr
, data
, flags
,
1996 debugstr_wn(data
->path
, data
->path_len
), data
->path_len
);
1998 TRACE("(%p %p %x): The URI contained no path\n", ptr
, data
, flags
);
2003 /* Parses the path of a opaque URI (much less strict then the parser
2004 * for a hierarchical URI).
2007 * Windows allows invalid % encoded data to appear in opaque URI paths
2008 * for unknown scheme types.
2010 * File schemes with USE_DOS_PATH set aren't allowed to have '<', '>', or '\"'
2013 static BOOL
parse_path_opaque(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
) {
2014 const BOOL known_scheme
= data
->scheme_type
!= URL_SCHEME_UNKNOWN
;
2015 const BOOL is_file
= data
->scheme_type
== URL_SCHEME_FILE
;
2019 while(!is_path_delim(**ptr
)) {
2020 if(**ptr
== '%' && known_scheme
) {
2021 if(!check_pct_encoded(ptr
)) {
2027 } else if(is_forbidden_dos_path_char(**ptr
) && is_file
&&
2028 (flags
& Uri_CREATE_FILE_USE_DOS_PATH
)) {
2037 data
->path_len
= *ptr
- data
->path
;
2038 TRACE("(%p %p %x): Parsed opaque URI path %s len=%d\n", ptr
, data
, flags
,
2039 debugstr_wn(data
->path
, data
->path_len
), data
->path_len
);
2043 /* Determines how the URI should be parsed after the scheme information.
2045 * If the scheme is followed, by "//" then, it is treated as an hierarchical URI
2046 * which then the authority and path information will be parsed out. Otherwise, the
2047 * URI will be treated as an opaque URI which the authority information is not parsed
2050 * RFC 3896 definition of hier-part:
2052 * hier-part = "//" authority path-abempty
2057 * MSDN opaque URI definition:
2058 * scheme ":" path [ "#" fragment ]
2061 * If the URI is of an unknown scheme type and has a "//" following the scheme then it
2062 * is treated as a hierarchical URI, but, if the CREATE_NO_CRACK_UNKNOWN_SCHEMES flag is
2063 * set then it is considered an opaque URI reguardless of what follows the scheme information
2064 * (per MSDN documentation).
2066 static BOOL
parse_hierpart(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
) {
2067 const WCHAR
*start
= *ptr
;
2069 /* Checks if the authority information needs to be parsed. */
2070 if(is_hierarchical_uri(ptr
, data
)) {
2071 /* Only treat it as a hierarchical URI if the scheme_type is known or
2072 * the Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES flag is not set.
2074 if(data
->scheme_type
!= URL_SCHEME_UNKNOWN
||
2075 !(flags
& Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES
)) {
2076 TRACE("(%p %p %x): Treating URI as an hierarchical URI.\n", ptr
, data
, flags
);
2077 data
->is_opaque
= FALSE
;
2079 /* TODO: Handle hierarchical URI's, parse authority then parse the path. */
2080 if(!parse_authority(ptr
, data
, flags
))
2083 return parse_path_hierarchical(ptr
, data
, flags
);
2085 /* Reset ptr to it's starting position so opaque path parsing
2086 * begins at the correct location.
2091 /* If it reaches here, then the URI will be treated as an opaque
2095 TRACE("(%p %p %x): Treating URI as an opaque URI.\n", ptr
, data
, flags
);
2097 data
->is_opaque
= TRUE
;
2098 if(!parse_path_opaque(ptr
, data
, flags
))
2104 /* Attempts to parse the query string from the URI.
2107 * If NO_DECODE_EXTRA_INFO flag is set, then invalid percent encoded
2108 * data is allowed appear in the query string. For unknown scheme types
2109 * invalid percent encoded data is allowed to appear reguardless.
2111 static BOOL
parse_query(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
) {
2112 const BOOL known_scheme
= data
->scheme_type
!= URL_SCHEME_UNKNOWN
;
2115 TRACE("(%p %p %x): URI didn't contain a query string.\n", ptr
, data
, flags
);
2122 while(**ptr
&& **ptr
!= '#') {
2123 if(**ptr
== '%' && known_scheme
&&
2124 !(flags
& Uri_CREATE_NO_DECODE_EXTRA_INFO
)) {
2125 if(!check_pct_encoded(ptr
)) {
2136 data
->query_len
= *ptr
- data
->query
;
2138 TRACE("(%p %p %x): Parsed query string %s len=%d\n", ptr
, data
, flags
,
2139 debugstr_wn(data
->query
, data
->query_len
), data
->query_len
);
2143 /* Attempts to parse the fragment from the URI.
2146 * If NO_DECODE_EXTRA_INFO flag is set, then invalid percent encoded
2147 * data is allowed appear in the query string. For unknown scheme types
2148 * invalid percent encoded data is allowed to appear reguardless.
2150 static BOOL
parse_fragment(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
) {
2151 const BOOL known_scheme
= data
->scheme_type
!= URL_SCHEME_UNKNOWN
;
2154 TRACE("(%p %p %x): URI didn't contain a fragment.\n", ptr
, data
, flags
);
2158 data
->fragment
= *ptr
;
2162 if(**ptr
== '%' && known_scheme
&&
2163 !(flags
& Uri_CREATE_NO_DECODE_EXTRA_INFO
)) {
2164 if(!check_pct_encoded(ptr
)) {
2165 *ptr
= data
->fragment
;
2166 data
->fragment
= NULL
;
2175 data
->fragment_len
= *ptr
- data
->fragment
;
2177 TRACE("(%p %p %x): Parsed fragment %s len=%d\n", ptr
, data
, flags
,
2178 debugstr_wn(data
->fragment
, data
->fragment_len
), data
->fragment_len
);
2182 /* Parses and validates the components of the specified by data->uri
2183 * and stores the information it parses into 'data'.
2185 * Returns TRUE if it successfully parsed the URI. False otherwise.
2187 static BOOL
parse_uri(parse_data
*data
, DWORD flags
) {
2194 TRACE("(%p %x): BEGINNING TO PARSE URI %s.\n", data
, flags
, debugstr_w(data
->uri
));
2196 if(!parse_scheme(pptr
, data
, flags
, 0))
2199 if(!parse_hierpart(pptr
, data
, flags
))
2202 if(!parse_query(pptr
, data
, flags
))
2205 if(!parse_fragment(pptr
, data
, flags
))
2208 TRACE("(%p %x): FINISHED PARSING URI.\n", data
, flags
);
2212 static BOOL
canonicalize_username(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
2215 if(!data
->username
) {
2216 uri
->userinfo_start
= -1;
2220 uri
->userinfo_start
= uri
->canon_len
;
2221 for(ptr
= data
->username
; ptr
< data
->username
+data
->username_len
; ++ptr
) {
2223 /* Only decode % encoded values for known scheme types. */
2224 if(data
->scheme_type
!= URL_SCHEME_UNKNOWN
) {
2225 /* See if the value really needs decoded. */
2226 WCHAR val
= decode_pct_val(ptr
);
2227 if(is_unreserved(val
)) {
2229 uri
->canon_uri
[uri
->canon_len
] = val
;
2233 /* Move pass the hex characters. */
2238 } else if(!is_reserved(*ptr
) && !is_unreserved(*ptr
) && *ptr
!= '\\') {
2239 /* Only percent encode forbidden characters if the NO_ENCODE_FORBIDDEN_CHARACTERS flag
2242 if(!(flags
& Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS
)) {
2244 pct_encode_val(*ptr
, uri
->canon_uri
+ uri
->canon_len
);
2246 uri
->canon_len
+= 3;
2252 /* Nothing special, so just copy the character over. */
2253 uri
->canon_uri
[uri
->canon_len
] = *ptr
;
2260 static BOOL
canonicalize_password(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
2263 if(!data
->password
) {
2264 uri
->userinfo_split
= -1;
2268 if(uri
->userinfo_start
== -1)
2269 /* Has a password, but, doesn't have a username. */
2270 uri
->userinfo_start
= uri
->canon_len
;
2272 uri
->userinfo_split
= uri
->canon_len
- uri
->userinfo_start
;
2274 /* Add the ':' to the userinfo component. */
2276 uri
->canon_uri
[uri
->canon_len
] = ':';
2279 for(ptr
= data
->password
; ptr
< data
->password
+data
->password_len
; ++ptr
) {
2281 /* Only decode % encoded values for known scheme types. */
2282 if(data
->scheme_type
!= URL_SCHEME_UNKNOWN
) {
2283 /* See if the value really needs decoded. */
2284 WCHAR val
= decode_pct_val(ptr
);
2285 if(is_unreserved(val
)) {
2287 uri
->canon_uri
[uri
->canon_len
] = val
;
2291 /* Move pass the hex characters. */
2296 } else if(!is_reserved(*ptr
) && !is_unreserved(*ptr
) && *ptr
!= '\\') {
2297 /* Only percent encode forbidden characters if the NO_ENCODE_FORBIDDEN_CHARACTERS flag
2300 if(!(flags
& Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS
)) {
2302 pct_encode_val(*ptr
, uri
->canon_uri
+ uri
->canon_len
);
2304 uri
->canon_len
+= 3;
2310 /* Nothing special, so just copy the character over. */
2311 uri
->canon_uri
[uri
->canon_len
] = *ptr
;
2318 /* Canonicalizes the userinfo of the URI represented by the parse_data.
2320 * Canonicalization of the userinfo is a simple process. If there are any percent
2321 * encoded characters that fall in the "unreserved" character set, they are decoded
2322 * to their actual value. If a character is not in the "unreserved" or "reserved" sets
2323 * then it is percent encoded. Other than that the characters are copied over without
2326 static BOOL
canonicalize_userinfo(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
2327 uri
->userinfo_start
= uri
->userinfo_split
= -1;
2328 uri
->userinfo_len
= 0;
2330 if(!data
->username
&& !data
->password
)
2331 /* URI doesn't have userinfo, so nothing to do here. */
2334 if(!canonicalize_username(data
, uri
, flags
, computeOnly
))
2337 if(!canonicalize_password(data
, uri
, flags
, computeOnly
))
2340 uri
->userinfo_len
= uri
->canon_len
- uri
->userinfo_start
;
2342 TRACE("(%p %p %x %d): Canonicalized userinfo, userinfo_start=%d, userinfo=%s, userinfo_split=%d userinfo_len=%d.\n",
2343 data
, uri
, flags
, computeOnly
, uri
->userinfo_start
, debugstr_wn(uri
->canon_uri
+ uri
->userinfo_start
, uri
->userinfo_len
),
2344 uri
->userinfo_split
, uri
->userinfo_len
);
2346 /* Now insert the '@' after the userinfo. */
2348 uri
->canon_uri
[uri
->canon_len
] = '@';
2354 /* Attempts to canonicalize a reg_name.
2356 * Things that happen:
2357 * 1) If Uri_CREATE_NO_CANONICALIZE flag is not set, then the reg_name is
2358 * lower cased. Unless it's an unknown scheme type, which case it's
2359 * no lower cased reguardless.
2361 * 2) Unreserved % encoded characters are decoded for known
2364 * 3) Forbidden characters are % encoded as long as
2365 * Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS flag is not set and
2366 * it isn't an unknown scheme type.
2368 * 4) If it's a file scheme and the host is "localhost" it's removed.
2370 * 5) If it's a file scheme and Uri_CREATE_FILE_USE_DOS_PATH is set,
2371 * then the UNC path characters are added before the host name.
2373 static BOOL
canonicalize_reg_name(const parse_data
*data
, Uri
*uri
,
2374 DWORD flags
, BOOL computeOnly
) {
2375 static const WCHAR localhostW
[] =
2376 {'l','o','c','a','l','h','o','s','t',0};
2378 const BOOL known_scheme
= data
->scheme_type
!= URL_SCHEME_UNKNOWN
;
2380 if(data
->scheme_type
== URL_SCHEME_FILE
&&
2381 data
->host_len
== lstrlenW(localhostW
)) {
2382 if(!StrCmpNIW(data
->host
, localhostW
, data
->host_len
)) {
2383 uri
->host_start
= -1;
2385 uri
->host_type
= Uri_HOST_UNKNOWN
;
2390 if(data
->scheme_type
== URL_SCHEME_FILE
&& flags
& Uri_CREATE_FILE_USE_DOS_PATH
) {
2392 uri
->canon_uri
[uri
->canon_len
] = '\\';
2393 uri
->canon_uri
[uri
->canon_len
+1] = '\\';
2395 uri
->canon_len
+= 2;
2396 uri
->authority_start
= uri
->canon_len
;
2399 uri
->host_start
= uri
->canon_len
;
2401 for(ptr
= data
->host
; ptr
< data
->host
+data
->host_len
; ++ptr
) {
2402 if(*ptr
== '%' && known_scheme
) {
2403 WCHAR val
= decode_pct_val(ptr
);
2404 if(is_unreserved(val
)) {
2405 /* If NO_CANONICALZE is not set, then windows lower cases the
2408 if(!(flags
& Uri_CREATE_NO_CANONICALIZE
) && isupperW(val
)) {
2410 uri
->canon_uri
[uri
->canon_len
] = tolowerW(val
);
2413 uri
->canon_uri
[uri
->canon_len
] = val
;
2417 /* Skip past the % encoded character. */
2421 /* Just copy the % over. */
2423 uri
->canon_uri
[uri
->canon_len
] = *ptr
;
2426 } else if(*ptr
== '\\') {
2427 /* Only unknown scheme types could have made it here with a '\\' in the host name. */
2429 uri
->canon_uri
[uri
->canon_len
] = *ptr
;
2431 } else if(!(flags
& Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS
) &&
2432 !is_unreserved(*ptr
) && !is_reserved(*ptr
) && known_scheme
) {
2434 pct_encode_val(*ptr
, uri
->canon_uri
+uri
->canon_len
);
2436 /* The percent encoded value gets lower cased also. */
2437 if(!(flags
& Uri_CREATE_NO_CANONICALIZE
)) {
2438 uri
->canon_uri
[uri
->canon_len
+1] = tolowerW(uri
->canon_uri
[uri
->canon_len
+1]);
2439 uri
->canon_uri
[uri
->canon_len
+2] = tolowerW(uri
->canon_uri
[uri
->canon_len
+2]);
2443 uri
->canon_len
+= 3;
2446 if(!(flags
& Uri_CREATE_NO_CANONICALIZE
) && known_scheme
)
2447 uri
->canon_uri
[uri
->canon_len
] = tolowerW(*ptr
);
2449 uri
->canon_uri
[uri
->canon_len
] = *ptr
;
2456 uri
->host_len
= uri
->canon_len
- uri
->host_start
;
2459 TRACE("(%p %p %x %d): Canonicalize reg_name=%s len=%d\n", data
, uri
, flags
,
2460 computeOnly
, debugstr_wn(uri
->canon_uri
+uri
->host_start
, uri
->host_len
),
2464 find_domain_name(uri
->canon_uri
+uri
->host_start
, uri
->host_len
,
2465 &(uri
->domain_offset
));
2470 /* Attempts to canonicalize an implicit IPv4 address. */
2471 static BOOL
canonicalize_implicit_ipv4address(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
2472 uri
->host_start
= uri
->canon_len
;
2474 TRACE("%u\n", data
->implicit_ipv4
);
2475 /* For unknown scheme types Window's doesn't convert
2476 * the value into an IP address, but, it still considers
2477 * it an IPv4 address.
2479 if(data
->scheme_type
== URL_SCHEME_UNKNOWN
) {
2481 memcpy(uri
->canon_uri
+uri
->canon_len
, data
->host
, data
->host_len
*sizeof(WCHAR
));
2482 uri
->canon_len
+= data
->host_len
;
2485 uri
->canon_len
+= ui2ipv4(uri
->canon_uri
+uri
->canon_len
, data
->implicit_ipv4
);
2487 uri
->canon_len
+= ui2ipv4(NULL
, data
->implicit_ipv4
);
2490 uri
->host_len
= uri
->canon_len
- uri
->host_start
;
2491 uri
->host_type
= Uri_HOST_IPV4
;
2494 TRACE("%p %p %x %d): Canonicalized implicit IP address=%s len=%d\n",
2495 data
, uri
, flags
, computeOnly
,
2496 debugstr_wn(uri
->canon_uri
+uri
->host_start
, uri
->host_len
),
2502 /* Attempts to canonicalize an IPv4 address.
2504 * If the parse_data represents a URI that has an implicit IPv4 address
2505 * (ex. http://256/, this function will convert 256 into 0.0.1.0). If
2506 * the implicit IP address exceeds the value of UINT_MAX (maximum value
2507 * for an IPv4 address) it's canonicalized as if were a reg-name.
2509 * If the parse_data contains a partial or full IPv4 address it normalizes it.
2510 * A partial IPv4 address is something like "192.0" and would be normalized to
2511 * "192.0.0.0". With a full (or partial) IPv4 address like "192.002.01.003" would
2512 * be normalized to "192.2.1.3".
2515 * Window's ONLY normalizes IPv4 address for known scheme types (one that isn't
2516 * URL_SCHEME_UNKNOWN). For unknown scheme types, it simply copies the data from
2517 * the original URI into the canonicalized URI, but, it still recognizes URI's
2518 * host type as HOST_IPV4.
2520 static BOOL
canonicalize_ipv4address(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
2521 if(data
->has_implicit_ip
)
2522 return canonicalize_implicit_ipv4address(data
, uri
, flags
, computeOnly
);
2524 uri
->host_start
= uri
->canon_len
;
2526 /* Windows only normalizes for known scheme types. */
2527 if(data
->scheme_type
!= URL_SCHEME_UNKNOWN
) {
2528 /* parse_data contains a partial or full IPv4 address, so normalize it. */
2529 DWORD i
, octetDigitCount
= 0, octetCount
= 0;
2530 BOOL octetHasDigit
= FALSE
;
2532 for(i
= 0; i
< data
->host_len
; ++i
) {
2533 if(data
->host
[i
] == '0' && !octetHasDigit
) {
2534 /* Can ignore leading zeros if:
2535 * 1) It isn't the last digit of the octet.
2536 * 2) i+1 != data->host_len
2539 if(octetDigitCount
== 2 ||
2540 i
+1 == data
->host_len
||
2541 data
->host
[i
+1] == '.') {
2543 uri
->canon_uri
[uri
->canon_len
] = data
->host
[i
];
2545 TRACE("Adding zero\n");
2547 } else if(data
->host
[i
] == '.') {
2549 uri
->canon_uri
[uri
->canon_len
] = data
->host
[i
];
2552 octetDigitCount
= 0;
2553 octetHasDigit
= FALSE
;
2557 uri
->canon_uri
[uri
->canon_len
] = data
->host
[i
];
2561 octetHasDigit
= TRUE
;
2565 /* Make sure the canonicalized IP address has 4 dec-octets.
2566 * If doesn't add "0" ones until there is 4;
2568 for( ; octetCount
< 3; ++octetCount
) {
2570 uri
->canon_uri
[uri
->canon_len
] = '.';
2571 uri
->canon_uri
[uri
->canon_len
+1] = '0';
2574 uri
->canon_len
+= 2;
2577 /* Windows doesn't normalize addresses in unknown schemes. */
2579 memcpy(uri
->canon_uri
+uri
->canon_len
, data
->host
, data
->host_len
*sizeof(WCHAR
));
2580 uri
->canon_len
+= data
->host_len
;
2583 uri
->host_len
= uri
->canon_len
- uri
->host_start
;
2585 TRACE("(%p %p %x %d): Canonicalized IPv4 address, ip=%s len=%d\n",
2586 data
, uri
, flags
, computeOnly
,
2587 debugstr_wn(uri
->canon_uri
+uri
->host_start
, uri
->host_len
),
2594 /* Attempts to canonicalize the IPv6 address of the URI.
2596 * Multiple things happen during the canonicalization of an IPv6 address:
2597 * 1) Any leading zero's in an h16 component are removed.
2598 * Ex: [0001:0022::] -> [1:22::]
2600 * 2) The longest sequence of zero h16 components are compressed
2601 * into a "::" (elision). If there's a tie, the first is choosen.
2603 * Ex: [0:0:0:0:1:6:7:8] -> [::1:6:7:8]
2604 * [0:0:0:0:1:2::] -> [::1:2:0:0]
2605 * [0:0:1:2:0:0:7:8] -> [::1:2:0:0:7:8]
2607 * 3) If an IPv4 address is attached to the IPv6 address, it's
2609 * Ex: [::001.002.022.000] -> [::1.2.22.0]
2611 * 4) If an elision is present, but, only represents 1 h16 component
2614 * Ex: [1::2:3:4:5:6:7] -> [1:0:2:3:4:5:6:7]
2616 * 5) If the IPv6 address contains an IPv4 address and there exists
2617 * at least 1 non-zero h16 component the IPv4 address is converted
2618 * into two h16 components, otherwise it's normalized and kept as is.
2620 * Ex: [::192.200.003.4] -> [::192.200.3.4]
2621 * [ffff::192.200.003.4] -> [ffff::c0c8:3041]
2624 * For unknown scheme types Windows simply copies the address over without any
2627 * IPv4 address can be included in an elision if all its components are 0's.
2629 static BOOL
canonicalize_ipv6address(const parse_data
*data
, Uri
*uri
,
2630 DWORD flags
, BOOL computeOnly
) {
2631 uri
->host_start
= uri
->canon_len
;
2633 if(data
->scheme_type
== URL_SCHEME_UNKNOWN
) {
2635 memcpy(uri
->canon_uri
+uri
->canon_len
, data
->host
, data
->host_len
*sizeof(WCHAR
));
2636 uri
->canon_len
+= data
->host_len
;
2640 DWORD i
, elision_len
;
2642 if(!ipv6_to_number(&(data
->ipv6_address
), values
)) {
2643 TRACE("(%p %p %x %d): Failed to compute numerical value for IPv6 address.\n",
2644 data
, uri
, flags
, computeOnly
);
2649 uri
->canon_uri
[uri
->canon_len
] = '[';
2652 /* Find where the elision should occur (if any). */
2653 compute_elision_location(&(data
->ipv6_address
), values
, &elision_start
, &elision_len
);
2655 TRACE("%p %p %x %d): Elision starts at %d, len=%u\n", data
, uri
, flags
,
2656 computeOnly
, elision_start
, elision_len
);
2658 for(i
= 0; i
< 8; ++i
) {
2659 BOOL in_elision
= (elision_start
> -1 && i
>= elision_start
&&
2660 i
< elision_start
+elision_len
);
2661 BOOL do_ipv4
= (i
== 6 && data
->ipv6_address
.ipv4
&& !in_elision
&&
2662 data
->ipv6_address
.h16_count
== 0);
2664 if(i
== elision_start
) {
2666 uri
->canon_uri
[uri
->canon_len
] = ':';
2667 uri
->canon_uri
[uri
->canon_len
+1] = ':';
2669 uri
->canon_len
+= 2;
2672 /* We can ignore the current component if we're in the elision. */
2676 /* We only add a ':' if we're not at i == 0, or when we're at
2677 * the very end of elision range since the ':' colon was handled
2678 * earlier. Otherwise we would end up with ":::" after elision.
2680 if(i
!= 0 && !(elision_start
> -1 && i
== elision_start
+elision_len
)) {
2682 uri
->canon_uri
[uri
->canon_len
] = ':';
2690 /* Combine the two parts of the IPv4 address values. */
2696 len
= ui2ipv4(uri
->canon_uri
+uri
->canon_len
, val
);
2698 len
= ui2ipv4(NULL
, val
);
2700 uri
->canon_len
+= len
;
2703 /* Write a regular h16 component to the URI. */
2705 /* Short circuit for the trivial case. */
2706 if(values
[i
] == 0) {
2708 uri
->canon_uri
[uri
->canon_len
] = '0';
2711 static const WCHAR formatW
[] = {'%','x',0};
2714 uri
->canon_len
+= sprintfW(uri
->canon_uri
+uri
->canon_len
,
2715 formatW
, values
[i
]);
2718 uri
->canon_len
+= sprintfW(tmp
, formatW
, values
[i
]);
2724 /* Add the closing ']'. */
2726 uri
->canon_uri
[uri
->canon_len
] = ']';
2730 uri
->host_len
= uri
->canon_len
- uri
->host_start
;
2733 TRACE("(%p %p %x %d): Canonicalized IPv6 address %s, len=%d\n", data
, uri
, flags
,
2734 computeOnly
, debugstr_wn(uri
->canon_uri
+uri
->host_start
, uri
->host_len
),
2740 /* Attempts to canonicalize the host of the URI (if any). */
2741 static BOOL
canonicalize_host(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
2742 uri
->host_start
= -1;
2744 uri
->domain_offset
= -1;
2747 switch(data
->host_type
) {
2749 uri
->host_type
= Uri_HOST_DNS
;
2750 if(!canonicalize_reg_name(data
, uri
, flags
, computeOnly
))
2755 uri
->host_type
= Uri_HOST_IPV4
;
2756 if(!canonicalize_ipv4address(data
, uri
, flags
, computeOnly
))
2761 if(!canonicalize_ipv6address(data
, uri
, flags
, computeOnly
))
2764 uri
->host_type
= Uri_HOST_IPV6
;
2766 case Uri_HOST_UNKNOWN
:
2767 if(data
->host_len
> 0 || data
->scheme_type
!= URL_SCHEME_FILE
) {
2768 uri
->host_start
= uri
->canon_len
;
2770 /* Nothing happens to unknown host types. */
2772 memcpy(uri
->canon_uri
+uri
->canon_len
, data
->host
, data
->host_len
*sizeof(WCHAR
));
2773 uri
->canon_len
+= data
->host_len
;
2774 uri
->host_len
= data
->host_len
;
2777 uri
->host_type
= Uri_HOST_UNKNOWN
;
2780 FIXME("(%p %p %x %d): Canonicalization for host type %d not supported.\n", data
,
2781 uri
, flags
, computeOnly
, data
->host_type
);
2789 static BOOL
canonicalize_port(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
2790 BOOL has_default_port
= FALSE
;
2791 USHORT default_port
= 0;
2794 uri
->port_offset
= -1;
2796 /* Check if the scheme has a default port. */
2797 for(i
= 0; i
< sizeof(default_ports
)/sizeof(default_ports
[0]); ++i
) {
2798 if(default_ports
[i
].scheme
== data
->scheme_type
) {
2799 has_default_port
= TRUE
;
2800 default_port
= default_ports
[i
].port
;
2805 uri
->has_port
= data
->has_port
|| has_default_port
;
2808 * 1) Has a port which is the default port.
2809 * 2) Has a port (not the default).
2810 * 3) Doesn't have a port, but, scheme has a default port.
2813 if(has_default_port
&& data
->has_port
&& data
->port_value
== default_port
) {
2814 /* If it's the default port and this flag isn't set, don't do anything. */
2815 if(flags
& Uri_CREATE_NO_CANONICALIZE
) {
2816 uri
->port_offset
= uri
->canon_len
-uri
->authority_start
;
2818 uri
->canon_uri
[uri
->canon_len
] = ':';
2822 /* Copy the original port over. */
2824 memcpy(uri
->canon_uri
+uri
->canon_len
, data
->port
, data
->port_len
*sizeof(WCHAR
));
2825 uri
->canon_len
+= data
->port_len
;
2828 uri
->canon_len
+= ui2str(uri
->canon_uri
+uri
->canon_len
, data
->port_value
);
2830 uri
->canon_len
+= ui2str(NULL
, data
->port_value
);
2834 uri
->port
= default_port
;
2835 } else if(data
->has_port
) {
2836 uri
->port_offset
= uri
->canon_len
-uri
->authority_start
;
2838 uri
->canon_uri
[uri
->canon_len
] = ':';
2841 if(flags
& Uri_CREATE_NO_CANONICALIZE
&& data
->port
) {
2842 /* Copy the original over without changes. */
2844 memcpy(uri
->canon_uri
+uri
->canon_len
, data
->port
, data
->port_len
*sizeof(WCHAR
));
2845 uri
->canon_len
+= data
->port_len
;
2848 uri
->canon_len
+= ui2str(uri
->canon_uri
+uri
->canon_len
, data
->port_value
);
2850 uri
->canon_len
+= ui2str(NULL
, data
->port_value
);
2853 uri
->port
= data
->port_value
;
2854 } else if(has_default_port
)
2855 uri
->port
= default_port
;
2860 /* Canonicalizes the authority of the URI represented by the parse_data. */
2861 static BOOL
canonicalize_authority(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
2862 uri
->authority_start
= uri
->canon_len
;
2863 uri
->authority_len
= 0;
2865 if(!canonicalize_userinfo(data
, uri
, flags
, computeOnly
))
2868 if(!canonicalize_host(data
, uri
, flags
, computeOnly
))
2871 if(!canonicalize_port(data
, uri
, flags
, computeOnly
))
2874 if(uri
->host_start
!= -1 || (data
->is_relative
&& (data
->password
|| data
->username
)))
2875 uri
->authority_len
= uri
->canon_len
- uri
->authority_start
;
2877 uri
->authority_start
= -1;
2882 /* Attempts to canonicalize the path of a hierarchical URI.
2884 * Things that happen:
2885 * 1). Forbidden characters are percent encoded, unless the NO_ENCODE_FORBIDDEN
2886 * flag is set or it's a file URI. Forbidden characters are always encoded
2887 * for file schemes reguardless and forbidden characters are never encoded
2888 * for unknown scheme types.
2890 * 2). For known scheme types '\\' are changed to '/'.
2892 * 3). Percent encoded, unreserved characters are decoded to their actual values.
2893 * Unless the scheme type is unknown. For file schemes any percent encoded
2894 * character in the unreserved or reserved set is decoded.
2896 * 4). For File schemes if the path is starts with a drive letter and doesn't
2897 * start with a '/' then one is appended.
2898 * Ex: file://c:/test.mp3 -> file:///c:/test.mp3
2900 * 5). Dot segments are removed from the path for all scheme types
2901 * unless NO_CANONICALIZE flag is set. Dot segments aren't removed
2902 * for wildcard scheme types.
2905 * file://c:/test%20test -> file:///c:/test%2520test
2906 * file://c:/test%3Etest -> file:///c:/test%253Etest
2907 * if Uri_CREATE_FILE_USE_DOS_PATH is not set:
2908 * file:///c:/test%20test -> file:///c:/test%20test
2909 * file:///c:/test%test -> file:///c:/test%25test
2911 static BOOL
canonicalize_path_hierarchical(const parse_data
*data
, Uri
*uri
,
2912 DWORD flags
, BOOL computeOnly
) {
2914 const BOOL known_scheme
= data
->scheme_type
!= URL_SCHEME_UNKNOWN
;
2915 const BOOL is_file
= data
->scheme_type
== URL_SCHEME_FILE
;
2917 BOOL escape_pct
= FALSE
;
2920 uri
->path_start
= -1;
2925 uri
->path_start
= uri
->canon_len
;
2928 if(is_file
&& uri
->host_start
== -1) {
2929 /* Check if a '/' needs to be appended for the file scheme. */
2930 if(data
->path_len
> 1 && is_drive_path(ptr
) && !(flags
& Uri_CREATE_FILE_USE_DOS_PATH
)) {
2932 uri
->canon_uri
[uri
->canon_len
] = '/';
2935 } else if(*ptr
== '/') {
2936 if(!(flags
& Uri_CREATE_FILE_USE_DOS_PATH
)) {
2937 /* Copy the extra '/' over. */
2939 uri
->canon_uri
[uri
->canon_len
] = '/';
2945 if(is_drive_path(ptr
)) {
2947 uri
->canon_uri
[uri
->canon_len
] = *ptr
;
2948 /* If theres a '|' after the drive letter, convert it to a ':'. */
2949 uri
->canon_uri
[uri
->canon_len
+1] = ':';
2952 uri
->canon_len
+= 2;
2956 if(!is_file
&& *(data
->path
) && *(data
->path
) != '/') {
2957 /* Prepend a '/' to the path if it doesn't have one. */
2959 uri
->canon_uri
[uri
->canon_len
] = '/';
2963 for(; ptr
< data
->path
+data
->path_len
; ++ptr
) {
2965 const WCHAR
*tmp
= ptr
;
2968 /* Check if the % represents a valid encoded char, or if it needs encoded. */
2969 BOOL force_encode
= !check_pct_encoded(&tmp
) && is_file
&& !(flags
&Uri_CREATE_FILE_USE_DOS_PATH
);
2970 val
= decode_pct_val(ptr
);
2972 if(force_encode
|| escape_pct
) {
2973 /* Escape the percent sign in the file URI. */
2975 pct_encode_val(*ptr
, uri
->canon_uri
+uri
->canon_len
);
2976 uri
->canon_len
+= 3;
2977 } else if((is_unreserved(val
) && known_scheme
) ||
2978 (is_file
&& (is_unreserved(val
) || is_reserved(val
) ||
2979 (val
&& flags
&Uri_CREATE_FILE_USE_DOS_PATH
&& !is_forbidden_dos_path_char(val
))))) {
2981 uri
->canon_uri
[uri
->canon_len
] = val
;
2988 uri
->canon_uri
[uri
->canon_len
] = *ptr
;
2991 } else if(*ptr
== '/' && is_file
&& (flags
& Uri_CREATE_FILE_USE_DOS_PATH
)) {
2992 /* Convert the '/' back to a '\\'. */
2994 uri
->canon_uri
[uri
->canon_len
] = '\\';
2996 } else if(*ptr
== '\\' && known_scheme
) {
2997 if(is_file
&& (flags
& Uri_CREATE_FILE_USE_DOS_PATH
)) {
2998 /* Don't convert the '\\' to a '/'. */
3000 uri
->canon_uri
[uri
->canon_len
] = *ptr
;
3004 uri
->canon_uri
[uri
->canon_len
] = '/';
3007 } else if(known_scheme
&& !is_unreserved(*ptr
) && !is_reserved(*ptr
) &&
3008 (!(flags
& Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS
) || is_file
)) {
3009 if(is_file
&& (flags
& Uri_CREATE_FILE_USE_DOS_PATH
)) {
3010 /* Don't escape the character. */
3012 uri
->canon_uri
[uri
->canon_len
] = *ptr
;
3015 /* Escape the forbidden character. */
3017 pct_encode_val(*ptr
, uri
->canon_uri
+uri
->canon_len
);
3018 uri
->canon_len
+= 3;
3022 uri
->canon_uri
[uri
->canon_len
] = *ptr
;
3027 uri
->path_len
= uri
->canon_len
- uri
->path_start
;
3029 /* Removing the dot segments only happens when it's not in
3030 * computeOnly mode and it's not a wildcard scheme. File schemes
3031 * with USE_DOS_PATH set don't get dot segments removed.
3033 if(!(is_file
&& (flags
& Uri_CREATE_FILE_USE_DOS_PATH
)) &&
3034 data
->scheme_type
!= URL_SCHEME_WILDCARD
) {
3035 if(!(flags
& Uri_CREATE_NO_CANONICALIZE
) && !computeOnly
) {
3036 /* Remove the dot segments (if any) and reset everything to the new
3039 DWORD new_len
= remove_dot_segments(uri
->canon_uri
+uri
->path_start
, uri
->path_len
);
3040 uri
->canon_len
-= uri
->path_len
-new_len
;
3041 uri
->path_len
= new_len
;
3046 TRACE("Canonicalized path %s len=%d\n",
3047 debugstr_wn(uri
->canon_uri
+uri
->path_start
, uri
->path_len
),
3053 /* Attempts to canonicalize the path for an opaque URI.
3055 * For known scheme types:
3056 * 1) forbidden characters are percent encoded if
3057 * NO_ENCODE_FORBIDDEN_CHARACTERS isn't set.
3059 * 2) Percent encoded, unreserved characters are decoded
3060 * to their actual values, for known scheme types.
3062 * 3) '\\' are changed to '/' for known scheme types
3063 * except for mailto schemes.
3065 * 4) For file schemes, if USE_DOS_PATH is set all '/'
3066 * are converted to backslashes.
3068 * 5) For file schemes, if USE_DOS_PATH isn't set all '\'
3069 * are converted to forward slashes.
3071 static BOOL
canonicalize_path_opaque(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
3073 const BOOL known_scheme
= data
->scheme_type
!= URL_SCHEME_UNKNOWN
;
3074 const BOOL is_file
= data
->scheme_type
== URL_SCHEME_FILE
;
3077 uri
->path_start
= -1;
3082 uri
->path_start
= uri
->canon_len
;
3084 /* Windows doesn't allow a "//" to appear after the scheme
3085 * of a URI, if it's an opaque URI.
3087 if(data
->scheme
&& *(data
->path
) == '/' && *(data
->path
+1) == '/') {
3088 /* So it inserts a "/." before the "//" if it exists. */
3090 uri
->canon_uri
[uri
->canon_len
] = '/';
3091 uri
->canon_uri
[uri
->canon_len
+1] = '.';
3094 uri
->canon_len
+= 2;
3097 for(ptr
= data
->path
; ptr
< data
->path
+data
->path_len
; ++ptr
) {
3098 if(*ptr
== '%' && known_scheme
) {
3099 WCHAR val
= decode_pct_val(ptr
);
3101 if(is_unreserved(val
)) {
3103 uri
->canon_uri
[uri
->canon_len
] = val
;
3110 uri
->canon_uri
[uri
->canon_len
] = *ptr
;
3113 } else if(*ptr
== '/' && is_file
&& (flags
& Uri_CREATE_FILE_USE_DOS_PATH
)) {
3115 uri
->canon_uri
[uri
->canon_len
] = '\\';
3117 } else if(*ptr
== '\\' && is_file
) {
3118 if(!(flags
& Uri_CREATE_FILE_USE_DOS_PATH
)) {
3119 /* Convert to a '/'. */
3121 uri
->canon_uri
[uri
->canon_len
] = '/';
3124 /* Just copy it over. */
3126 uri
->canon_uri
[uri
->canon_len
] = *ptr
;
3129 } else if(known_scheme
&& !is_unreserved(*ptr
) && !is_reserved(*ptr
) &&
3130 !(flags
& Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS
)) {
3131 if(is_file
&& (flags
& Uri_CREATE_FILE_USE_DOS_PATH
)) {
3132 /* Forbidden characters aren't percent encoded for file schemes
3133 * with USE_DOS_PATH set.
3136 uri
->canon_uri
[uri
->canon_len
] = *ptr
;
3138 } else if(data
->scheme_type
== URL_SCHEME_MK
&& *ptr
== '\\') {
3139 /* MK URIs don't get '\\' percent encoded. */
3141 uri
->canon_uri
[uri
->canon_len
] = *ptr
;
3145 pct_encode_val(*ptr
, uri
->canon_uri
+uri
->canon_len
);
3146 uri
->canon_len
+= 3;
3150 uri
->canon_uri
[uri
->canon_len
] = *ptr
;
3155 uri
->path_len
= uri
->canon_len
- uri
->path_start
;
3157 TRACE("(%p %p %x %d): Canonicalized opaque URI path %s len=%d\n", data
, uri
, flags
, computeOnly
,
3158 debugstr_wn(uri
->canon_uri
+uri
->path_start
, uri
->path_len
), uri
->path_len
);
3162 /* Determines how the URI represented by the parse_data should be canonicalized.
3164 * Essentially, if the parse_data represents an hierarchical URI then it calls
3165 * canonicalize_authority and the canonicalization functions for the path. If the
3166 * URI is opaque it canonicalizes the path of the URI.
3168 static BOOL
canonicalize_hierpart(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
3169 if(!data
->is_opaque
|| (data
->is_relative
&& (data
->password
|| data
->username
))) {
3170 /* "//" is only added for non-wildcard scheme types.
3172 * A "//" is only added to a relative URI if it has a
3173 * host or port component (this only happens if a IUriBuilder
3174 * is generating an IUri).
3176 if((data
->is_relative
&& (data
->host
|| data
->has_port
)) ||
3177 (!data
->is_relative
&& data
->scheme_type
!= URL_SCHEME_WILDCARD
)) {
3179 INT pos
= uri
->canon_len
;
3181 uri
->canon_uri
[pos
] = '/';
3182 uri
->canon_uri
[pos
+1] = '/';
3184 uri
->canon_len
+= 2;
3187 if(!canonicalize_authority(data
, uri
, flags
, computeOnly
))
3190 if(data
->is_relative
&& (data
->password
|| data
->username
)) {
3191 if(!canonicalize_path_opaque(data
, uri
, flags
, computeOnly
))
3194 if(!canonicalize_path_hierarchical(data
, uri
, flags
, computeOnly
))
3198 /* Opaque URI's don't have an authority. */
3199 uri
->userinfo_start
= uri
->userinfo_split
= -1;
3200 uri
->userinfo_len
= 0;
3201 uri
->host_start
= -1;
3203 uri
->host_type
= Uri_HOST_UNKNOWN
;
3204 uri
->has_port
= FALSE
;
3205 uri
->authority_start
= -1;
3206 uri
->authority_len
= 0;
3207 uri
->domain_offset
= -1;
3208 uri
->port_offset
= -1;
3210 if(is_hierarchical_scheme(data
->scheme_type
)) {
3213 /* Absolute URIs aren't displayed for known scheme types
3214 * which should be hierarchical URIs.
3216 uri
->display_modifiers
|= URI_DISPLAY_NO_ABSOLUTE_URI
;
3218 /* Windows also sets the port for these (if they have one). */
3219 for(i
= 0; i
< sizeof(default_ports
)/sizeof(default_ports
[0]); ++i
) {
3220 if(data
->scheme_type
== default_ports
[i
].scheme
) {
3221 uri
->has_port
= TRUE
;
3222 uri
->port
= default_ports
[i
].port
;
3228 if(!canonicalize_path_opaque(data
, uri
, flags
, computeOnly
))
3232 if(uri
->path_start
> -1 && !computeOnly
)
3233 /* Finding file extensions happens for both types of URIs. */
3234 uri
->extension_offset
= find_file_extension(uri
->canon_uri
+uri
->path_start
, uri
->path_len
);
3236 uri
->extension_offset
= -1;
3241 /* Attempts to canonicalize the query string of the URI.
3243 * Things that happen:
3244 * 1) For known scheme types forbidden characters
3245 * are percent encoded, unless the NO_DECODE_EXTRA_INFO flag is set
3246 * or NO_ENCODE_FORBIDDEN_CHARACTERS is set.
3248 * 2) For known scheme types, percent encoded, unreserved characters
3249 * are decoded as long as the NO_DECODE_EXTRA_INFO flag isn't set.
3251 static BOOL
canonicalize_query(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
3252 const WCHAR
*ptr
, *end
;
3253 const BOOL known_scheme
= data
->scheme_type
!= URL_SCHEME_UNKNOWN
;
3256 uri
->query_start
= -1;
3261 uri
->query_start
= uri
->canon_len
;
3263 end
= data
->query
+data
->query_len
;
3264 for(ptr
= data
->query
; ptr
< end
; ++ptr
) {
3266 if(known_scheme
&& !(flags
& Uri_CREATE_NO_DECODE_EXTRA_INFO
)) {
3267 WCHAR val
= decode_pct_val(ptr
);
3268 if(is_unreserved(val
)) {
3270 uri
->canon_uri
[uri
->canon_len
] = val
;
3277 } else if(known_scheme
&& !is_unreserved(*ptr
) && !is_reserved(*ptr
)) {
3278 if(!(flags
& Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS
) &&
3279 !(flags
& Uri_CREATE_NO_DECODE_EXTRA_INFO
)) {
3281 pct_encode_val(*ptr
, uri
->canon_uri
+uri
->canon_len
);
3282 uri
->canon_len
+= 3;
3288 uri
->canon_uri
[uri
->canon_len
] = *ptr
;
3292 uri
->query_len
= uri
->canon_len
- uri
->query_start
;
3295 TRACE("(%p %p %x %d): Canonicalized query string %s len=%d\n", data
, uri
, flags
,
3296 computeOnly
, debugstr_wn(uri
->canon_uri
+uri
->query_start
, uri
->query_len
),
3301 static BOOL
canonicalize_fragment(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
3302 const WCHAR
*ptr
, *end
;
3303 const BOOL known_scheme
= data
->scheme_type
!= URL_SCHEME_UNKNOWN
;
3305 if(!data
->fragment
) {
3306 uri
->fragment_start
= -1;
3307 uri
->fragment_len
= 0;
3311 uri
->fragment_start
= uri
->canon_len
;
3313 end
= data
->fragment
+ data
->fragment_len
;
3314 for(ptr
= data
->fragment
; ptr
< end
; ++ptr
) {
3316 if(known_scheme
&& !(flags
& Uri_CREATE_NO_DECODE_EXTRA_INFO
)) {
3317 WCHAR val
= decode_pct_val(ptr
);
3318 if(is_unreserved(val
)) {
3320 uri
->canon_uri
[uri
->canon_len
] = val
;
3327 } else if(known_scheme
&& !is_unreserved(*ptr
) && !is_reserved(*ptr
)) {
3328 if(!(flags
& Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS
) &&
3329 !(flags
& Uri_CREATE_NO_DECODE_EXTRA_INFO
)) {
3331 pct_encode_val(*ptr
, uri
->canon_uri
+uri
->canon_len
);
3332 uri
->canon_len
+= 3;
3338 uri
->canon_uri
[uri
->canon_len
] = *ptr
;
3342 uri
->fragment_len
= uri
->canon_len
- uri
->fragment_start
;
3345 TRACE("(%p %p %x %d): Canonicalized fragment %s len=%d\n", data
, uri
, flags
,
3346 computeOnly
, debugstr_wn(uri
->canon_uri
+uri
->fragment_start
, uri
->fragment_len
),
3351 /* Canonicalizes the scheme information specified in the parse_data using the specified flags. */
3352 static BOOL
canonicalize_scheme(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
3353 uri
->scheme_start
= -1;
3354 uri
->scheme_len
= 0;
3357 /* The only type of URI that doesn't have to have a scheme is a relative
3360 if(!data
->is_relative
) {
3361 FIXME("(%p %p %x): Unable to determine the scheme type of %s.\n", data
,
3362 uri
, flags
, debugstr_w(data
->uri
));
3368 INT pos
= uri
->canon_len
;
3370 for(i
= 0; i
< data
->scheme_len
; ++i
) {
3371 /* Scheme name must be lower case after canonicalization. */
3372 uri
->canon_uri
[i
+ pos
] = tolowerW(data
->scheme
[i
]);
3375 uri
->canon_uri
[i
+ pos
] = ':';
3376 uri
->scheme_start
= pos
;
3378 TRACE("(%p %p %x): Canonicalized scheme=%s, len=%d.\n", data
, uri
, flags
,
3379 debugstr_wn(uri
->canon_uri
, uri
->scheme_len
), data
->scheme_len
);
3382 /* This happens in both computation modes. */
3383 uri
->canon_len
+= data
->scheme_len
+ 1;
3384 uri
->scheme_len
= data
->scheme_len
;
3389 /* Compute's what the length of the URI specified by the parse_data will be
3390 * after canonicalization occurs using the specified flags.
3392 * This function will return a non-zero value indicating the length of the canonicalized
3393 * URI, or -1 on error.
3395 static int compute_canonicalized_length(const parse_data
*data
, DWORD flags
) {
3398 memset(&uri
, 0, sizeof(Uri
));
3400 TRACE("(%p %x): Beginning to compute canonicalized length for URI %s\n", data
, flags
,
3401 debugstr_w(data
->uri
));
3403 if(!canonicalize_scheme(data
, &uri
, flags
, TRUE
)) {
3404 ERR("(%p %x): Failed to compute URI scheme length.\n", data
, flags
);
3408 if(!canonicalize_hierpart(data
, &uri
, flags
, TRUE
)) {
3409 ERR("(%p %x): Failed to compute URI hierpart length.\n", data
, flags
);
3413 if(!canonicalize_query(data
, &uri
, flags
, TRUE
)) {
3414 ERR("(%p %x): Failed to compute query string length.\n", data
, flags
);
3418 if(!canonicalize_fragment(data
, &uri
, flags
, TRUE
)) {
3419 ERR("(%p %x): Failed to compute fragment length.\n", data
, flags
);
3423 TRACE("(%p %x): Finished computing canonicalized URI length. length=%d\n", data
, flags
, uri
.canon_len
);
3425 return uri
.canon_len
;
3428 /* Canonicalizes the URI data specified in the parse_data, using the given flags. If the
3429 * canonicalization succeededs it will store all the canonicalization information
3430 * in the pointer to the Uri.
3432 * To canonicalize a URI this function first computes what the length of the URI
3433 * specified by the parse_data will be. Once this is done it will then perfom the actual
3434 * canonicalization of the URI.
3436 static HRESULT
canonicalize_uri(const parse_data
*data
, Uri
*uri
, DWORD flags
) {
3439 uri
->canon_uri
= NULL
;
3440 len
= uri
->canon_size
= uri
->canon_len
= 0;
3442 TRACE("(%p %p %x): beginning to canonicalize URI %s.\n", data
, uri
, flags
, debugstr_w(data
->uri
));
3444 /* First try to compute the length of the URI. */
3445 len
= compute_canonicalized_length(data
, flags
);
3447 ERR("(%p %p %x): Could not compute the canonicalized length of %s.\n", data
, uri
, flags
,
3448 debugstr_w(data
->uri
));
3449 return E_INVALIDARG
;
3452 uri
->canon_uri
= heap_alloc((len
+1)*sizeof(WCHAR
));
3454 return E_OUTOFMEMORY
;
3456 uri
->canon_size
= len
;
3457 if(!canonicalize_scheme(data
, uri
, flags
, FALSE
)) {
3458 ERR("(%p %p %x): Unable to canonicalize the scheme of the URI.\n", data
, uri
, flags
);
3459 return E_INVALIDARG
;
3461 uri
->scheme_type
= data
->scheme_type
;
3463 if(!canonicalize_hierpart(data
, uri
, flags
, FALSE
)) {
3464 ERR("(%p %p %x): Unable to canonicalize the heirpart of the URI\n", data
, uri
, flags
);
3465 return E_INVALIDARG
;
3468 if(!canonicalize_query(data
, uri
, flags
, FALSE
)) {
3469 ERR("(%p %p %x): Unable to canonicalize query string of the URI.\n",
3471 return E_INVALIDARG
;
3474 if(!canonicalize_fragment(data
, uri
, flags
, FALSE
)) {
3475 ERR("(%p %p %x): Unable to canonicalize fragment of the URI.\n",
3477 return E_INVALIDARG
;
3480 /* There's a possibility we didn't use all the space we allocated
3483 if(uri
->canon_len
< uri
->canon_size
) {
3484 /* This happens if the URI is hierarchical and dot
3485 * segments were removed from it's path.
3487 WCHAR
*tmp
= heap_realloc(uri
->canon_uri
, (uri
->canon_len
+1)*sizeof(WCHAR
));
3489 return E_OUTOFMEMORY
;
3491 uri
->canon_uri
= tmp
;
3492 uri
->canon_size
= uri
->canon_len
;
3495 uri
->canon_uri
[uri
->canon_len
] = '\0';
3496 TRACE("(%p %p %x): finished canonicalizing the URI. uri=%s\n", data
, uri
, flags
, debugstr_w(uri
->canon_uri
));
3501 static HRESULT
get_builder_component(LPWSTR
*component
, DWORD
*component_len
,
3502 LPCWSTR source
, DWORD source_len
,
3503 LPCWSTR
*output
, DWORD
*output_len
)
3516 if(!(*component
) && source
) {
3517 /* Allocate 'component', and copy the contents from 'source'
3518 * into the new allocation.
3520 *component
= heap_alloc((source_len
+1)*sizeof(WCHAR
));
3522 return E_OUTOFMEMORY
;
3524 memcpy(*component
, source
, source_len
*sizeof(WCHAR
));
3525 (*component
)[source_len
] = '\0';
3526 *component_len
= source_len
;
3529 *output
= *component
;
3530 *output_len
= *component_len
;
3531 return *output
? S_OK
: S_FALSE
;
3534 /* Allocates 'component' and copies the string from 'new_value' into 'component'.
3535 * If 'prefix' is set and 'new_value' isn't NULL, then it checks if 'new_value'
3536 * starts with 'prefix'. If it doesn't then 'prefix' is prepended to 'component'.
3538 * If everything is successful, then will set 'success_flag' in 'flags'.
3540 static HRESULT
set_builder_component(LPWSTR
*component
, DWORD
*component_len
, LPCWSTR new_value
,
3541 WCHAR prefix
, DWORD
*flags
, DWORD success_flag
)
3543 heap_free(*component
);
3549 BOOL add_prefix
= FALSE
;
3550 DWORD len
= lstrlenW(new_value
);
3553 if(prefix
&& *new_value
!= prefix
) {
3555 *component
= heap_alloc((len
+2)*sizeof(WCHAR
));
3557 *component
= heap_alloc((len
+1)*sizeof(WCHAR
));
3560 return E_OUTOFMEMORY
;
3563 (*component
)[pos
++] = prefix
;
3565 memcpy(*component
+pos
, new_value
, (len
+1)*sizeof(WCHAR
));
3566 *component_len
= len
+pos
;
3569 *flags
|= success_flag
;
3573 #define URI(x) ((IUri*) &(x)->lpIUriVtbl)
3574 #define URIBUILDER(x) ((IUriBuilder*) &(x)->lpIUriBuilderVtbl)
3576 static void reset_builder(UriBuilder
*builder
) {
3578 IUri_Release(URI(builder
->uri
));
3579 builder
->uri
= NULL
;
3581 heap_free(builder
->fragment
);
3582 builder
->fragment
= NULL
;
3583 builder
->fragment_len
= 0;
3585 heap_free(builder
->host
);
3586 builder
->host
= NULL
;
3587 builder
->host_len
= 0;
3589 heap_free(builder
->password
);
3590 builder
->password
= NULL
;
3591 builder
->password_len
= 0;
3593 heap_free(builder
->path
);
3594 builder
->path
= NULL
;
3595 builder
->path_len
= 0;
3597 heap_free(builder
->query
);
3598 builder
->query
= NULL
;
3599 builder
->query_len
= 0;
3601 heap_free(builder
->scheme
);
3602 builder
->scheme
= NULL
;
3603 builder
->scheme_len
= 0;
3605 heap_free(builder
->username
);
3606 builder
->username
= NULL
;
3607 builder
->username_len
= 0;
3609 builder
->has_port
= FALSE
;
3611 builder
->modified_props
= 0;
3614 static HRESULT
validate_scheme_name(const UriBuilder
*builder
, parse_data
*data
, DWORD flags
) {
3615 const WCHAR
*component
;
3620 if(builder
->scheme
) {
3621 ptr
= builder
->scheme
;
3622 expected_len
= builder
->scheme_len
;
3623 } else if(builder
->uri
&& builder
->uri
->scheme_start
> -1) {
3624 ptr
= builder
->uri
->canon_uri
+builder
->uri
->scheme_start
;
3625 expected_len
= builder
->uri
->scheme_len
;
3627 static const WCHAR nullW
[] = {0};
3634 if(parse_scheme(pptr
, data
, flags
, ALLOW_NULL_TERM_SCHEME
) &&
3635 data
->scheme_len
== expected_len
) {
3637 TRACE("(%p %p %x): Found valid scheme component %s len=%d.\n", builder
, data
, flags
,
3638 debugstr_wn(data
->scheme
, data
->scheme_len
), data
->scheme_len
);
3640 TRACE("(%p %p %x): Invalid scheme component found %s.\n", builder
, data
, flags
,
3641 debugstr_wn(component
, expected_len
));
3642 return INET_E_INVALID_URL
;
3648 static HRESULT
validate_username(const UriBuilder
*builder
, parse_data
*data
, DWORD flags
) {
3653 if(builder
->username
) {
3654 ptr
= builder
->username
;
3655 expected_len
= builder
->username_len
;
3656 } else if(!(builder
->modified_props
& Uri_HAS_USER_NAME
) && builder
->uri
&&
3657 builder
->uri
->userinfo_start
> -1 && builder
->uri
->userinfo_split
!= 0) {
3658 /* Just use the username from the base Uri. */
3659 data
->username
= builder
->uri
->canon_uri
+builder
->uri
->userinfo_start
;
3660 data
->username_len
= (builder
->uri
->userinfo_split
> -1) ?
3661 builder
->uri
->userinfo_split
: builder
->uri
->userinfo_len
;
3669 const WCHAR
*component
= ptr
;
3671 if(parse_username(pptr
, data
, flags
, ALLOW_NULL_TERM_USER_NAME
) &&
3672 data
->username_len
== expected_len
)
3673 TRACE("(%p %p %x): Found valid username component %s len=%d.\n", builder
, data
, flags
,
3674 debugstr_wn(data
->username
, data
->username_len
), data
->username_len
);
3676 TRACE("(%p %p %x): Invalid username component found %s.\n", builder
, data
, flags
,
3677 debugstr_wn(component
, expected_len
));
3678 return INET_E_INVALID_URL
;
3685 static HRESULT
validate_password(const UriBuilder
*builder
, parse_data
*data
, DWORD flags
) {
3690 if(builder
->password
) {
3691 ptr
= builder
->password
;
3692 expected_len
= builder
->password_len
;
3693 } else if(!(builder
->modified_props
& Uri_HAS_PASSWORD
) && builder
->uri
&&
3694 builder
->uri
->userinfo_split
> -1) {
3695 data
->password
= builder
->uri
->canon_uri
+builder
->uri
->userinfo_start
+builder
->uri
->userinfo_split
+1;
3696 data
->password_len
= builder
->uri
->userinfo_len
-builder
->uri
->userinfo_split
-1;
3704 const WCHAR
*component
= ptr
;
3706 if(parse_password(pptr
, data
, flags
, ALLOW_NULL_TERM_PASSWORD
) &&
3707 data
->password_len
== expected_len
)
3708 TRACE("(%p %p %x): Found valid password component %s len=%d.\n", builder
, data
, flags
,
3709 debugstr_wn(data
->password
, data
->password_len
), data
->password_len
);
3711 TRACE("(%p %p %x): Invalid password component found %s.\n", builder
, data
, flags
,
3712 debugstr_wn(component
, expected_len
));
3713 return INET_E_INVALID_URL
;
3720 static HRESULT
validate_userinfo(const UriBuilder
*builder
, parse_data
*data
, DWORD flags
) {
3723 hr
= validate_username(builder
, data
, flags
);
3727 hr
= validate_password(builder
, data
, flags
);
3734 static HRESULT
validate_host(const UriBuilder
*builder
, parse_data
*data
, DWORD flags
) {
3740 ptr
= builder
->host
;
3741 expected_len
= builder
->host_len
;
3742 } else if(!(builder
->modified_props
& Uri_HAS_HOST
) && builder
->uri
&& builder
->uri
->host_start
> -1) {
3743 ptr
= builder
->uri
->canon_uri
+ builder
->uri
->host_start
;
3744 expected_len
= builder
->uri
->host_len
;
3749 const WCHAR
*component
= ptr
;
3750 DWORD extras
= ALLOW_BRACKETLESS_IP_LITERAL
|IGNORE_PORT_DELIMITER
|SKIP_IP_FUTURE_CHECK
;
3753 if(parse_host(pptr
, data
, flags
, extras
) && data
->host_len
== expected_len
)
3754 TRACE("(%p %p %x): Found valid host name %s len=%d type=%d.\n", builder
, data
, flags
,
3755 debugstr_wn(data
->host
, data
->host_len
), data
->host_len
, data
->host_type
);
3757 TRACE("(%p %p %x): Invalid host name found %s.\n", builder
, data
, flags
,
3758 debugstr_wn(component
, expected_len
));
3759 return INET_E_INVALID_URL
;
3766 static void setup_port(const UriBuilder
*builder
, parse_data
*data
, DWORD flags
) {
3767 if(builder
->modified_props
& Uri_HAS_PORT
) {
3768 if(builder
->has_port
) {
3769 data
->has_port
= TRUE
;
3770 data
->port_value
= builder
->port
;
3772 } else if(builder
->uri
&& builder
->uri
->has_port
) {
3773 data
->has_port
= TRUE
;
3774 data
->port_value
= builder
->uri
->port
;
3778 TRACE("(%p %p %x): Using %u as port for IUri.\n", builder
, data
, flags
, data
->port_value
);
3781 static HRESULT
validate_path(const UriBuilder
*builder
, parse_data
*data
, DWORD flags
) {
3782 const WCHAR
*ptr
= NULL
;
3783 const WCHAR
*component
;
3786 BOOL check_len
= TRUE
;
3790 ptr
= builder
->path
;
3791 expected_len
= builder
->path_len
;
3792 } else if(!(builder
->modified_props
& Uri_HAS_PATH
) &&
3793 builder
->uri
&& builder
->uri
->path_start
> -1) {
3794 ptr
= builder
->uri
->canon_uri
+builder
->uri
->path_start
;
3795 expected_len
= builder
->uri
->path_len
;
3797 static const WCHAR nullW
[] = {0};
3805 /* How the path is validated depends on what type of
3808 valid
= data
->is_opaque
?
3809 parse_path_opaque(pptr
, data
, flags
) : parse_path_hierarchical(pptr
, data
, flags
);
3811 if(!valid
|| (check_len
&& expected_len
!= data
->path_len
)) {
3812 TRACE("(%p %p %x): Invalid path component %s.\n", builder
, data
, flags
,
3813 debugstr_wn(component
, expected_len
));
3814 return INET_E_INVALID_URL
;
3817 TRACE("(%p %p %x): Valid path component %s len=%d.\n", builder
, data
, flags
,
3818 debugstr_wn(data
->path
, data
->path_len
), data
->path_len
);
3823 static HRESULT
validate_query(const UriBuilder
*builder
, parse_data
*data
, DWORD flags
) {
3824 const WCHAR
*ptr
= NULL
;
3828 if(builder
->query
) {
3829 ptr
= builder
->query
;
3830 expected_len
= builder
->query_len
;
3831 } else if(!(builder
->modified_props
& Uri_HAS_QUERY
) && builder
->uri
&&
3832 builder
->uri
->query_start
> -1) {
3833 ptr
= builder
->uri
->canon_uri
+builder
->uri
->query_start
;
3834 expected_len
= builder
->uri
->query_len
;
3838 const WCHAR
*component
= ptr
;
3841 if(parse_query(pptr
, data
, flags
) && expected_len
== data
->query_len
)
3842 TRACE("(%p %p %x): Valid query component %s len=%d.\n", builder
, data
, flags
,
3843 debugstr_wn(data
->query
, data
->query_len
), data
->query_len
);
3845 TRACE("(%p %p %x): Invalid query component %s.\n", builder
, data
, flags
,
3846 debugstr_wn(component
, expected_len
));
3847 return INET_E_INVALID_URL
;
3854 static HRESULT
validate_fragment(const UriBuilder
*builder
, parse_data
*data
, DWORD flags
) {
3855 const WCHAR
*ptr
= NULL
;
3859 if(builder
->fragment
) {
3860 ptr
= builder
->fragment
;
3861 expected_len
= builder
->fragment_len
;
3862 } else if(!(builder
->modified_props
& Uri_HAS_FRAGMENT
) && builder
->uri
&&
3863 builder
->uri
->fragment_start
> -1) {
3864 ptr
= builder
->uri
->canon_uri
+builder
->uri
->fragment_start
;
3865 expected_len
= builder
->uri
->fragment_len
;
3869 const WCHAR
*component
= ptr
;
3872 if(parse_fragment(pptr
, data
, flags
) && expected_len
== data
->fragment_len
)
3873 TRACE("(%p %p %x): Valid fragment component %s len=%d.\n", builder
, data
, flags
,
3874 debugstr_wn(data
->fragment
, data
->fragment_len
), data
->fragment_len
);
3876 TRACE("(%p %p %x): Invalid fragment component %s.\n", builder
, data
, flags
,
3877 debugstr_wn(component
, expected_len
));
3878 return INET_E_INVALID_URL
;
3885 static HRESULT
validate_components(const UriBuilder
*builder
, parse_data
*data
, DWORD flags
) {
3888 memset(data
, 0, sizeof(parse_data
));
3890 TRACE("(%p %p %x): Beginning to validate builder components.\n", builder
, data
, flags
);
3892 hr
= validate_scheme_name(builder
, data
, flags
);
3896 /* Extra validation for file schemes. */
3897 if(data
->scheme_type
== URL_SCHEME_FILE
) {
3898 if((builder
->password
|| (builder
->uri
&& builder
->uri
->userinfo_split
> -1)) ||
3899 (builder
->username
|| (builder
->uri
&& builder
->uri
->userinfo_start
> -1))) {
3900 TRACE("(%p %p %x): File schemes can't contain a username or password.\n",
3901 builder
, data
, flags
);
3902 return INET_E_INVALID_URL
;
3906 hr
= validate_userinfo(builder
, data
, flags
);
3910 hr
= validate_host(builder
, data
, flags
);
3914 setup_port(builder
, data
, flags
);
3916 /* The URI is opaque if it doesn't have an authority component. */
3917 if(!data
->is_relative
)
3918 data
->is_opaque
= !data
->username
&& !data
->password
&& !data
->host
&& !data
->has_port
;
3920 data
->is_opaque
= !data
->host
&& !data
->has_port
;
3922 hr
= validate_path(builder
, data
, flags
);
3926 hr
= validate_query(builder
, data
, flags
);
3930 hr
= validate_fragment(builder
, data
, flags
);
3934 TRACE("(%p %p %x): Finished validating builder components.\n", builder
, data
, flags
);
3939 static void convert_to_dos_path(const WCHAR
*path
, DWORD path_len
,
3940 WCHAR
*output
, DWORD
*output_len
)
3942 const WCHAR
*ptr
= path
;
3944 if(path_len
> 3 && *ptr
== '/' && is_drive_path(path
+1))
3945 /* Skip over the leading / before the drive path. */
3948 for(; ptr
< path
+path_len
; ++ptr
) {
3961 /* Generates a raw uri string using the parse_data. */
3962 static DWORD
generate_raw_uri(const parse_data
*data
, BSTR uri
, DWORD flags
) {
3967 memcpy(uri
, data
->scheme
, data
->scheme_len
*sizeof(WCHAR
));
3968 uri
[data
->scheme_len
] = ':';
3970 length
+= data
->scheme_len
+1;
3973 if(!data
->is_opaque
) {
3974 /* For the "//" which appears before the authority component. */
3977 uri
[length
+1] = '/';
3981 /* Check if we need to add the "\\" before the host name
3982 * of a UNC server name in a DOS path.
3984 if(flags
& RAW_URI_CONVERT_TO_DOS_PATH
&&
3985 data
->scheme_type
== URL_SCHEME_FILE
&& data
->host
) {
3988 uri
[length
+1] = '\\';
3994 if(data
->username
) {
3996 memcpy(uri
+length
, data
->username
, data
->username_len
*sizeof(WCHAR
));
3997 length
+= data
->username_len
;
4000 if(data
->password
) {
4003 memcpy(uri
+length
+1, data
->password
, data
->password_len
*sizeof(WCHAR
));
4005 length
+= data
->password_len
+1;
4008 if(data
->password
|| data
->username
) {
4015 /* IPv6 addresses get the brackets added around them if they don't already
4018 const BOOL add_brackets
= data
->host_type
== Uri_HOST_IPV6
&& *(data
->host
) != '[';
4026 memcpy(uri
+length
, data
->host
, data
->host_len
*sizeof(WCHAR
));
4027 length
+= data
->host_len
;
4036 if(data
->has_port
) {
4037 /* The port isn't included in the raw uri if it's the default
4038 * port for the scheme type.
4041 BOOL is_default
= FALSE
;
4043 for(i
= 0; i
< sizeof(default_ports
)/sizeof(default_ports
[0]); ++i
) {
4044 if(data
->scheme_type
== default_ports
[i
].scheme
&&
4045 data
->port_value
== default_ports
[i
].port
)
4049 if(!is_default
|| flags
& RAW_URI_FORCE_PORT_DISP
) {
4055 length
+= ui2str(uri
+length
, data
->port_value
);
4057 length
+= ui2str(NULL
, data
->port_value
);
4061 /* Check if a '/' should be added before the path for hierarchical URIs. */
4062 if(!data
->is_opaque
&& data
->path
&& *(data
->path
) != '/') {
4069 if(!data
->is_opaque
&& data
->scheme_type
== URL_SCHEME_FILE
&&
4070 flags
& RAW_URI_CONVERT_TO_DOS_PATH
) {
4074 convert_to_dos_path(data
->path
, data
->path_len
, uri
+length
, &len
);
4076 convert_to_dos_path(data
->path
, data
->path_len
, NULL
, &len
);
4081 memcpy(uri
+length
, data
->path
, data
->path_len
*sizeof(WCHAR
));
4082 length
+= data
->path_len
;
4088 memcpy(uri
+length
, data
->query
, data
->query_len
*sizeof(WCHAR
));
4089 length
+= data
->query_len
;
4092 if(data
->fragment
) {
4094 memcpy(uri
+length
, data
->fragment
, data
->fragment_len
*sizeof(WCHAR
));
4095 length
+= data
->fragment_len
;
4099 TRACE("(%p %p): Generated raw uri=%s len=%d\n", data
, uri
, debugstr_wn(uri
, length
), length
);
4101 TRACE("(%p %p): Computed raw uri len=%d\n", data
, uri
, length
);
4106 static HRESULT
generate_uri(const UriBuilder
*builder
, const parse_data
*data
, Uri
*uri
, DWORD flags
) {
4108 DWORD length
= generate_raw_uri(data
, NULL
, 0);
4109 uri
->raw_uri
= SysAllocStringLen(NULL
, length
);
4111 return E_OUTOFMEMORY
;
4113 generate_raw_uri(data
, uri
->raw_uri
, 0);
4115 hr
= canonicalize_uri(data
, uri
, flags
);
4117 if(hr
== E_INVALIDARG
)
4118 return INET_E_INVALID_URL
;
4122 uri
->create_flags
= flags
;
4126 #define URI_THIS(iface) DEFINE_THIS(Uri, IUri, iface)
4128 static HRESULT WINAPI
Uri_QueryInterface(IUri
*iface
, REFIID riid
, void **ppv
)
4130 Uri
*This
= URI_THIS(iface
);
4132 if(IsEqualGUID(&IID_IUnknown
, riid
)) {
4133 TRACE("(%p)->(IID_IUnknown %p)\n", This
, ppv
);
4135 }else if(IsEqualGUID(&IID_IUri
, riid
)) {
4136 TRACE("(%p)->(IID_IUri %p)\n", This
, ppv
);
4138 }else if(IsEqualGUID(&IID_IUriObj
, riid
)) {
4139 TRACE("(%p)->(IID_IUriObj %p)\n", This
, ppv
);
4143 TRACE("(%p)->(%s %p)\n", This
, debugstr_guid(riid
), ppv
);
4145 return E_NOINTERFACE
;
4148 IUnknown_AddRef((IUnknown
*)*ppv
);
4152 static ULONG WINAPI
Uri_AddRef(IUri
*iface
)
4154 Uri
*This
= URI_THIS(iface
);
4155 LONG ref
= InterlockedIncrement(&This
->ref
);
4157 TRACE("(%p) ref=%d\n", This
, ref
);
4162 static ULONG WINAPI
Uri_Release(IUri
*iface
)
4164 Uri
*This
= URI_THIS(iface
);
4165 LONG ref
= InterlockedDecrement(&This
->ref
);
4167 TRACE("(%p) ref=%d\n", This
, ref
);
4170 SysFreeString(This
->raw_uri
);
4171 heap_free(This
->canon_uri
);
4178 static HRESULT WINAPI
Uri_GetPropertyBSTR(IUri
*iface
, Uri_PROPERTY uriProp
, BSTR
*pbstrProperty
, DWORD dwFlags
)
4180 Uri
*This
= URI_THIS(iface
);
4182 TRACE("(%p)->(%d %p %x)\n", This
, uriProp
, pbstrProperty
, dwFlags
);
4187 if(uriProp
> Uri_PROPERTY_STRING_LAST
) {
4188 /* Windows allocates an empty BSTR for invalid Uri_PROPERTY's. */
4189 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
4190 if(!(*pbstrProperty
))
4191 return E_OUTOFMEMORY
;
4193 /* It only returns S_FALSE for the ZONE property... */
4194 if(uriProp
== Uri_PROPERTY_ZONE
)
4200 /* Don't have support for flags yet. */
4202 FIXME("(%p)->(%d %p %x)\n", This
, uriProp
, pbstrProperty
, dwFlags
);
4207 case Uri_PROPERTY_ABSOLUTE_URI
:
4208 if(This
->display_modifiers
& URI_DISPLAY_NO_ABSOLUTE_URI
) {
4209 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
4212 if(This
->scheme_type
!= URL_SCHEME_UNKNOWN
&& This
->userinfo_start
> -1) {
4213 if(This
->userinfo_len
== 0) {
4214 /* Don't include the '@' after the userinfo component. */
4215 *pbstrProperty
= SysAllocStringLen(NULL
, This
->canon_len
-1);
4217 if(*pbstrProperty
) {
4218 /* Copy everything before it. */
4219 memcpy(*pbstrProperty
, This
->canon_uri
, This
->userinfo_start
*sizeof(WCHAR
));
4221 /* And everything after it. */
4222 memcpy(*pbstrProperty
+This
->userinfo_start
, This
->canon_uri
+This
->userinfo_start
+1,
4223 (This
->canon_len
-This
->userinfo_start
-1)*sizeof(WCHAR
));
4225 } else if(This
->userinfo_split
== 0 && This
->userinfo_len
== 1) {
4226 /* Don't include the ":@" */
4227 *pbstrProperty
= SysAllocStringLen(NULL
, This
->canon_len
-2);
4229 if(*pbstrProperty
) {
4230 memcpy(*pbstrProperty
, This
->canon_uri
, This
->userinfo_start
*sizeof(WCHAR
));
4231 memcpy(*pbstrProperty
+This
->userinfo_start
, This
->canon_uri
+This
->userinfo_start
+2,
4232 (This
->canon_len
-This
->userinfo_start
-2)*sizeof(WCHAR
));
4235 *pbstrProperty
= SysAllocString(This
->canon_uri
);
4239 *pbstrProperty
= SysAllocString(This
->canon_uri
);
4244 if(!(*pbstrProperty
))
4245 hres
= E_OUTOFMEMORY
;
4248 case Uri_PROPERTY_AUTHORITY
:
4249 if(This
->authority_start
> -1) {
4250 if(This
->port_offset
> -1 && is_default_port(This
->scheme_type
, This
->port
) &&
4251 This
->display_modifiers
& URI_DISPLAY_NO_DEFAULT_PORT_AUTH
)
4252 /* Don't include the port in the authority component. */
4253 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+This
->authority_start
, This
->port_offset
);
4255 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+This
->authority_start
, This
->authority_len
);
4258 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
4262 if(!(*pbstrProperty
))
4263 hres
= E_OUTOFMEMORY
;
4266 case Uri_PROPERTY_DISPLAY_URI
:
4267 /* The Display URI contains everything except for the userinfo for known
4270 if(This
->scheme_type
!= URL_SCHEME_UNKNOWN
&& This
->userinfo_start
> -1) {
4271 *pbstrProperty
= SysAllocStringLen(NULL
, This
->canon_len
-This
->userinfo_len
);
4273 if(*pbstrProperty
) {
4274 /* Copy everything before the userinfo over. */
4275 memcpy(*pbstrProperty
, This
->canon_uri
, This
->userinfo_start
*sizeof(WCHAR
));
4276 /* Copy everything after the userinfo over. */
4277 memcpy(*pbstrProperty
+This
->userinfo_start
,
4278 This
->canon_uri
+This
->userinfo_start
+This
->userinfo_len
+1,
4279 (This
->canon_len
-(This
->userinfo_start
+This
->userinfo_len
+1))*sizeof(WCHAR
));
4282 *pbstrProperty
= SysAllocString(This
->canon_uri
);
4284 if(!(*pbstrProperty
))
4285 hres
= E_OUTOFMEMORY
;
4290 case Uri_PROPERTY_DOMAIN
:
4291 if(This
->domain_offset
> -1) {
4292 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+This
->host_start
+This
->domain_offset
,
4293 This
->host_len
-This
->domain_offset
);
4296 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
4300 if(!(*pbstrProperty
))
4301 hres
= E_OUTOFMEMORY
;
4304 case Uri_PROPERTY_EXTENSION
:
4305 if(This
->extension_offset
> -1) {
4306 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+This
->path_start
+This
->extension_offset
,
4307 This
->path_len
-This
->extension_offset
);
4310 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
4314 if(!(*pbstrProperty
))
4315 hres
= E_OUTOFMEMORY
;
4318 case Uri_PROPERTY_FRAGMENT
:
4319 if(This
->fragment_start
> -1) {
4320 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+This
->fragment_start
, This
->fragment_len
);
4323 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
4327 if(!(*pbstrProperty
))
4328 hres
= E_OUTOFMEMORY
;
4331 case Uri_PROPERTY_HOST
:
4332 if(This
->host_start
> -1) {
4333 /* The '[' and ']' aren't included for IPv6 addresses. */
4334 if(This
->host_type
== Uri_HOST_IPV6
)
4335 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+This
->host_start
+1, This
->host_len
-2);
4337 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+This
->host_start
, This
->host_len
);
4341 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
4345 if(!(*pbstrProperty
))
4346 hres
= E_OUTOFMEMORY
;
4349 case Uri_PROPERTY_PASSWORD
:
4350 if(This
->userinfo_split
> -1) {
4351 *pbstrProperty
= SysAllocStringLen(
4352 This
->canon_uri
+This
->userinfo_start
+This
->userinfo_split
+1,
4353 This
->userinfo_len
-This
->userinfo_split
-1);
4356 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
4360 if(!(*pbstrProperty
))
4361 return E_OUTOFMEMORY
;
4364 case Uri_PROPERTY_PATH
:
4365 if(This
->path_start
> -1) {
4366 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+This
->path_start
, This
->path_len
);
4369 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
4373 if(!(*pbstrProperty
))
4374 hres
= E_OUTOFMEMORY
;
4377 case Uri_PROPERTY_PATH_AND_QUERY
:
4378 if(This
->path_start
> -1) {
4379 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+This
->path_start
, This
->path_len
+This
->query_len
);
4381 } else if(This
->query_start
> -1) {
4382 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+This
->query_start
, This
->query_len
);
4385 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
4389 if(!(*pbstrProperty
))
4390 hres
= E_OUTOFMEMORY
;
4393 case Uri_PROPERTY_QUERY
:
4394 if(This
->query_start
> -1) {
4395 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+This
->query_start
, This
->query_len
);
4398 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
4402 if(!(*pbstrProperty
))
4403 hres
= E_OUTOFMEMORY
;
4406 case Uri_PROPERTY_RAW_URI
:
4407 *pbstrProperty
= SysAllocString(This
->raw_uri
);
4408 if(!(*pbstrProperty
))
4409 hres
= E_OUTOFMEMORY
;
4413 case Uri_PROPERTY_SCHEME_NAME
:
4414 if(This
->scheme_start
> -1) {
4415 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+ This
->scheme_start
, This
->scheme_len
);
4418 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
4422 if(!(*pbstrProperty
))
4423 hres
= E_OUTOFMEMORY
;
4426 case Uri_PROPERTY_USER_INFO
:
4427 if(This
->userinfo_start
> -1) {
4428 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+This
->userinfo_start
, This
->userinfo_len
);
4431 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
4435 if(!(*pbstrProperty
))
4436 hres
= E_OUTOFMEMORY
;
4439 case Uri_PROPERTY_USER_NAME
:
4440 if(This
->userinfo_start
> -1 && This
->userinfo_split
!= 0) {
4441 /* If userinfo_split is set, that means a password exists
4442 * so the username is only from userinfo_start to userinfo_split.
4444 if(This
->userinfo_split
> -1) {
4445 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+ This
->userinfo_start
, This
->userinfo_split
);
4448 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+ This
->userinfo_start
, This
->userinfo_len
);
4452 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
4456 if(!(*pbstrProperty
))
4457 return E_OUTOFMEMORY
;
4461 FIXME("(%p)->(%d %p %x)\n", This
, uriProp
, pbstrProperty
, dwFlags
);
4468 static HRESULT WINAPI
Uri_GetPropertyLength(IUri
*iface
, Uri_PROPERTY uriProp
, DWORD
*pcchProperty
, DWORD dwFlags
)
4470 Uri
*This
= URI_THIS(iface
);
4472 TRACE("(%p)->(%d %p %x)\n", This
, uriProp
, pcchProperty
, dwFlags
);
4475 return E_INVALIDARG
;
4477 /* Can only return a length for a property if it's a string. */
4478 if(uriProp
> Uri_PROPERTY_STRING_LAST
)
4479 return E_INVALIDARG
;
4481 /* Don't have support for flags yet. */
4483 FIXME("(%p)->(%d %p %x)\n", This
, uriProp
, pcchProperty
, dwFlags
);
4488 case Uri_PROPERTY_ABSOLUTE_URI
:
4489 if(This
->display_modifiers
& URI_DISPLAY_NO_ABSOLUTE_URI
) {
4493 if(This
->scheme_type
!= URL_SCHEME_UNKNOWN
) {
4494 if(This
->userinfo_start
> -1 && This
->userinfo_len
== 0)
4495 /* Don't include the '@' in the length. */
4496 *pcchProperty
= This
->canon_len
-1;
4497 else if(This
->userinfo_start
> -1 && This
->userinfo_len
== 1 &&
4498 This
->userinfo_split
== 0)
4499 /* Don't include the ":@" in the length. */
4500 *pcchProperty
= This
->canon_len
-2;
4502 *pcchProperty
= This
->canon_len
;
4504 *pcchProperty
= This
->canon_len
;
4510 case Uri_PROPERTY_AUTHORITY
:
4511 if(This
->port_offset
> -1 &&
4512 This
->display_modifiers
& URI_DISPLAY_NO_DEFAULT_PORT_AUTH
&&
4513 is_default_port(This
->scheme_type
, This
->port
))
4514 /* Only count up until the port in the authority. */
4515 *pcchProperty
= This
->port_offset
;
4517 *pcchProperty
= This
->authority_len
;
4518 hres
= (This
->authority_start
> -1) ? S_OK
: S_FALSE
;
4520 case Uri_PROPERTY_DISPLAY_URI
:
4521 if(This
->scheme_type
!= URL_SCHEME_UNKNOWN
&& This
->userinfo_start
> -1)
4522 *pcchProperty
= This
->canon_len
-This
->userinfo_len
-1;
4524 *pcchProperty
= This
->canon_len
;
4528 case Uri_PROPERTY_DOMAIN
:
4529 if(This
->domain_offset
> -1)
4530 *pcchProperty
= This
->host_len
- This
->domain_offset
;
4534 hres
= (This
->domain_offset
> -1) ? S_OK
: S_FALSE
;
4536 case Uri_PROPERTY_EXTENSION
:
4537 if(This
->extension_offset
> -1) {
4538 *pcchProperty
= This
->path_len
- This
->extension_offset
;
4546 case Uri_PROPERTY_FRAGMENT
:
4547 *pcchProperty
= This
->fragment_len
;
4548 hres
= (This
->fragment_start
> -1) ? S_OK
: S_FALSE
;
4550 case Uri_PROPERTY_HOST
:
4551 *pcchProperty
= This
->host_len
;
4553 /* '[' and ']' aren't included in the length. */
4554 if(This
->host_type
== Uri_HOST_IPV6
)
4557 hres
= (This
->host_start
> -1) ? S_OK
: S_FALSE
;
4559 case Uri_PROPERTY_PASSWORD
:
4560 *pcchProperty
= (This
->userinfo_split
> -1) ? This
->userinfo_len
-This
->userinfo_split
-1 : 0;
4561 hres
= (This
->userinfo_split
> -1) ? S_OK
: S_FALSE
;
4563 case Uri_PROPERTY_PATH
:
4564 *pcchProperty
= This
->path_len
;
4565 hres
= (This
->path_start
> -1) ? S_OK
: S_FALSE
;
4567 case Uri_PROPERTY_PATH_AND_QUERY
:
4568 *pcchProperty
= This
->path_len
+This
->query_len
;
4569 hres
= (This
->path_start
> -1 || This
->query_start
> -1) ? S_OK
: S_FALSE
;
4571 case Uri_PROPERTY_QUERY
:
4572 *pcchProperty
= This
->query_len
;
4573 hres
= (This
->query_start
> -1) ? S_OK
: S_FALSE
;
4575 case Uri_PROPERTY_RAW_URI
:
4576 *pcchProperty
= SysStringLen(This
->raw_uri
);
4579 case Uri_PROPERTY_SCHEME_NAME
:
4580 *pcchProperty
= This
->scheme_len
;
4581 hres
= (This
->scheme_start
> -1) ? S_OK
: S_FALSE
;
4583 case Uri_PROPERTY_USER_INFO
:
4584 *pcchProperty
= This
->userinfo_len
;
4585 hres
= (This
->userinfo_start
> -1) ? S_OK
: S_FALSE
;
4587 case Uri_PROPERTY_USER_NAME
:
4588 *pcchProperty
= (This
->userinfo_split
> -1) ? This
->userinfo_split
: This
->userinfo_len
;
4589 if(This
->userinfo_split
== 0)
4592 hres
= (This
->userinfo_start
> -1) ? S_OK
: S_FALSE
;
4595 FIXME("(%p)->(%d %p %x)\n", This
, uriProp
, pcchProperty
, dwFlags
);
4602 static HRESULT WINAPI
Uri_GetPropertyDWORD(IUri
*iface
, Uri_PROPERTY uriProp
, DWORD
*pcchProperty
, DWORD dwFlags
)
4604 Uri
*This
= URI_THIS(iface
);
4607 TRACE("(%p)->(%d %p %x)\n", This
, uriProp
, pcchProperty
, dwFlags
);
4610 return E_INVALIDARG
;
4612 /* Microsoft's implementation for the ZONE property of a URI seems to be lacking...
4613 * From what I can tell, instead of checking which URLZONE the URI belongs to it
4614 * simply assigns URLZONE_INVALID and returns E_NOTIMPL. This also applies to the GetZone
4617 if(uriProp
== Uri_PROPERTY_ZONE
) {
4618 *pcchProperty
= URLZONE_INVALID
;
4622 if(uriProp
< Uri_PROPERTY_DWORD_START
) {
4624 return E_INVALIDARG
;
4628 case Uri_PROPERTY_HOST_TYPE
:
4629 *pcchProperty
= This
->host_type
;
4632 case Uri_PROPERTY_PORT
:
4633 if(!This
->has_port
) {
4637 *pcchProperty
= This
->port
;
4642 case Uri_PROPERTY_SCHEME
:
4643 *pcchProperty
= This
->scheme_type
;
4647 FIXME("(%p)->(%d %p %x)\n", This
, uriProp
, pcchProperty
, dwFlags
);
4654 static HRESULT WINAPI
Uri_HasProperty(IUri
*iface
, Uri_PROPERTY uriProp
, BOOL
*pfHasProperty
)
4656 Uri
*This
= URI_THIS(iface
);
4657 TRACE("(%p)->(%d %p)\n", This
, uriProp
, pfHasProperty
);
4660 return E_INVALIDARG
;
4663 case Uri_PROPERTY_ABSOLUTE_URI
:
4664 *pfHasProperty
= !(This
->display_modifiers
& URI_DISPLAY_NO_ABSOLUTE_URI
);
4666 case Uri_PROPERTY_AUTHORITY
:
4667 *pfHasProperty
= This
->authority_start
> -1;
4669 case Uri_PROPERTY_DISPLAY_URI
:
4670 *pfHasProperty
= TRUE
;
4672 case Uri_PROPERTY_DOMAIN
:
4673 *pfHasProperty
= This
->domain_offset
> -1;
4675 case Uri_PROPERTY_EXTENSION
:
4676 *pfHasProperty
= This
->extension_offset
> -1;
4678 case Uri_PROPERTY_FRAGMENT
:
4679 *pfHasProperty
= This
->fragment_start
> -1;
4681 case Uri_PROPERTY_HOST
:
4682 *pfHasProperty
= This
->host_start
> -1;
4684 case Uri_PROPERTY_PASSWORD
:
4685 *pfHasProperty
= This
->userinfo_split
> -1;
4687 case Uri_PROPERTY_PATH
:
4688 *pfHasProperty
= This
->path_start
> -1;
4690 case Uri_PROPERTY_PATH_AND_QUERY
:
4691 *pfHasProperty
= (This
->path_start
> -1 || This
->query_start
> -1);
4693 case Uri_PROPERTY_QUERY
:
4694 *pfHasProperty
= This
->query_start
> -1;
4696 case Uri_PROPERTY_RAW_URI
:
4697 *pfHasProperty
= TRUE
;
4699 case Uri_PROPERTY_SCHEME_NAME
:
4700 *pfHasProperty
= This
->scheme_start
> -1;
4702 case Uri_PROPERTY_USER_INFO
:
4703 *pfHasProperty
= This
->userinfo_start
> -1;
4705 case Uri_PROPERTY_USER_NAME
:
4706 if(This
->userinfo_split
== 0)
4707 *pfHasProperty
= FALSE
;
4709 *pfHasProperty
= This
->userinfo_start
> -1;
4711 case Uri_PROPERTY_HOST_TYPE
:
4712 *pfHasProperty
= TRUE
;
4714 case Uri_PROPERTY_PORT
:
4715 *pfHasProperty
= This
->has_port
;
4717 case Uri_PROPERTY_SCHEME
:
4718 *pfHasProperty
= TRUE
;
4720 case Uri_PROPERTY_ZONE
:
4721 *pfHasProperty
= FALSE
;
4724 FIXME("(%p)->(%d %p): Unsupported property type.\n", This
, uriProp
, pfHasProperty
);
4731 static HRESULT WINAPI
Uri_GetAbsoluteUri(IUri
*iface
, BSTR
*pstrAbsoluteUri
)
4733 TRACE("(%p)->(%p)\n", iface
, pstrAbsoluteUri
);
4734 return Uri_GetPropertyBSTR(iface
, Uri_PROPERTY_ABSOLUTE_URI
, pstrAbsoluteUri
, 0);
4737 static HRESULT WINAPI
Uri_GetAuthority(IUri
*iface
, BSTR
*pstrAuthority
)
4739 TRACE("(%p)->(%p)\n", iface
, pstrAuthority
);
4740 return Uri_GetPropertyBSTR(iface
, Uri_PROPERTY_AUTHORITY
, pstrAuthority
, 0);
4743 static HRESULT WINAPI
Uri_GetDisplayUri(IUri
*iface
, BSTR
*pstrDisplayUri
)
4745 TRACE("(%p)->(%p)\n", iface
, pstrDisplayUri
);
4746 return Uri_GetPropertyBSTR(iface
, Uri_PROPERTY_DISPLAY_URI
, pstrDisplayUri
, 0);
4749 static HRESULT WINAPI
Uri_GetDomain(IUri
*iface
, BSTR
*pstrDomain
)
4751 TRACE("(%p)->(%p)\n", iface
, pstrDomain
);
4752 return Uri_GetPropertyBSTR(iface
, Uri_PROPERTY_DOMAIN
, pstrDomain
, 0);
4755 static HRESULT WINAPI
Uri_GetExtension(IUri
*iface
, BSTR
*pstrExtension
)
4757 TRACE("(%p)->(%p)\n", iface
, pstrExtension
);
4758 return Uri_GetPropertyBSTR(iface
, Uri_PROPERTY_EXTENSION
, pstrExtension
, 0);
4761 static HRESULT WINAPI
Uri_GetFragment(IUri
*iface
, BSTR
*pstrFragment
)
4763 TRACE("(%p)->(%p)\n", iface
, pstrFragment
);
4764 return Uri_GetPropertyBSTR(iface
, Uri_PROPERTY_FRAGMENT
, pstrFragment
, 0);
4767 static HRESULT WINAPI
Uri_GetHost(IUri
*iface
, BSTR
*pstrHost
)
4769 TRACE("(%p)->(%p)\n", iface
, pstrHost
);
4770 return Uri_GetPropertyBSTR(iface
, Uri_PROPERTY_HOST
, pstrHost
, 0);
4773 static HRESULT WINAPI
Uri_GetPassword(IUri
*iface
, BSTR
*pstrPassword
)
4775 TRACE("(%p)->(%p)\n", iface
, pstrPassword
);
4776 return Uri_GetPropertyBSTR(iface
, Uri_PROPERTY_PASSWORD
, pstrPassword
, 0);
4779 static HRESULT WINAPI
Uri_GetPath(IUri
*iface
, BSTR
*pstrPath
)
4781 TRACE("(%p)->(%p)\n", iface
, pstrPath
);
4782 return Uri_GetPropertyBSTR(iface
, Uri_PROPERTY_PATH
, pstrPath
, 0);
4785 static HRESULT WINAPI
Uri_GetPathAndQuery(IUri
*iface
, BSTR
*pstrPathAndQuery
)
4787 TRACE("(%p)->(%p)\n", iface
, pstrPathAndQuery
);
4788 return Uri_GetPropertyBSTR(iface
, Uri_PROPERTY_PATH_AND_QUERY
, pstrPathAndQuery
, 0);
4791 static HRESULT WINAPI
Uri_GetQuery(IUri
*iface
, BSTR
*pstrQuery
)
4793 TRACE("(%p)->(%p)\n", iface
, pstrQuery
);
4794 return Uri_GetPropertyBSTR(iface
, Uri_PROPERTY_QUERY
, pstrQuery
, 0);
4797 static HRESULT WINAPI
Uri_GetRawUri(IUri
*iface
, BSTR
*pstrRawUri
)
4799 TRACE("(%p)->(%p)\n", iface
, pstrRawUri
);
4800 return Uri_GetPropertyBSTR(iface
, Uri_PROPERTY_RAW_URI
, pstrRawUri
, 0);
4803 static HRESULT WINAPI
Uri_GetSchemeName(IUri
*iface
, BSTR
*pstrSchemeName
)
4805 TRACE("(%p)->(%p)\n", iface
, pstrSchemeName
);
4806 return Uri_GetPropertyBSTR(iface
, Uri_PROPERTY_SCHEME_NAME
, pstrSchemeName
, 0);
4809 static HRESULT WINAPI
Uri_GetUserInfo(IUri
*iface
, BSTR
*pstrUserInfo
)
4811 TRACE("(%p)->(%p)\n", iface
, pstrUserInfo
);
4812 return Uri_GetPropertyBSTR(iface
, Uri_PROPERTY_USER_INFO
, pstrUserInfo
, 0);
4815 static HRESULT WINAPI
Uri_GetUserName(IUri
*iface
, BSTR
*pstrUserName
)
4817 TRACE("(%p)->(%p)\n", iface
, pstrUserName
);
4818 return Uri_GetPropertyBSTR(iface
, Uri_PROPERTY_USER_NAME
, pstrUserName
, 0);
4821 static HRESULT WINAPI
Uri_GetHostType(IUri
*iface
, DWORD
*pdwHostType
)
4823 TRACE("(%p)->(%p)\n", iface
, pdwHostType
);
4824 return Uri_GetPropertyDWORD(iface
, Uri_PROPERTY_HOST_TYPE
, pdwHostType
, 0);
4827 static HRESULT WINAPI
Uri_GetPort(IUri
*iface
, DWORD
*pdwPort
)
4829 TRACE("(%p)->(%p)\n", iface
, pdwPort
);
4830 return Uri_GetPropertyDWORD(iface
, Uri_PROPERTY_PORT
, pdwPort
, 0);
4833 static HRESULT WINAPI
Uri_GetScheme(IUri
*iface
, DWORD
*pdwScheme
)
4835 Uri
*This
= URI_THIS(iface
);
4836 TRACE("(%p)->(%p)\n", This
, pdwScheme
);
4837 return Uri_GetPropertyDWORD(iface
, Uri_PROPERTY_SCHEME
, pdwScheme
, 0);
4840 static HRESULT WINAPI
Uri_GetZone(IUri
*iface
, DWORD
*pdwZone
)
4842 TRACE("(%p)->(%p)\n", iface
, pdwZone
);
4843 return Uri_GetPropertyDWORD(iface
, Uri_PROPERTY_ZONE
,pdwZone
, 0);
4846 static HRESULT WINAPI
Uri_GetProperties(IUri
*iface
, DWORD
*pdwProperties
)
4848 Uri
*This
= URI_THIS(iface
);
4849 TRACE("(%p)->(%p)\n", This
, pdwProperties
);
4852 return E_INVALIDARG
;
4854 /* All URIs have these. */
4855 *pdwProperties
= Uri_HAS_DISPLAY_URI
|Uri_HAS_RAW_URI
|Uri_HAS_SCHEME
|Uri_HAS_HOST_TYPE
;
4857 if(!(This
->display_modifiers
& URI_DISPLAY_NO_ABSOLUTE_URI
))
4858 *pdwProperties
|= Uri_HAS_ABSOLUTE_URI
;
4860 if(This
->scheme_start
> -1)
4861 *pdwProperties
|= Uri_HAS_SCHEME_NAME
;
4863 if(This
->authority_start
> -1) {
4864 *pdwProperties
|= Uri_HAS_AUTHORITY
;
4865 if(This
->userinfo_start
> -1) {
4866 *pdwProperties
|= Uri_HAS_USER_INFO
;
4867 if(This
->userinfo_split
!= 0)
4868 *pdwProperties
|= Uri_HAS_USER_NAME
;
4870 if(This
->userinfo_split
> -1)
4871 *pdwProperties
|= Uri_HAS_PASSWORD
;
4872 if(This
->host_start
> -1)
4873 *pdwProperties
|= Uri_HAS_HOST
;
4874 if(This
->domain_offset
> -1)
4875 *pdwProperties
|= Uri_HAS_DOMAIN
;
4879 *pdwProperties
|= Uri_HAS_PORT
;
4880 if(This
->path_start
> -1)
4881 *pdwProperties
|= Uri_HAS_PATH
|Uri_HAS_PATH_AND_QUERY
;
4882 if(This
->query_start
> -1)
4883 *pdwProperties
|= Uri_HAS_QUERY
|Uri_HAS_PATH_AND_QUERY
;
4885 if(This
->extension_offset
> -1)
4886 *pdwProperties
|= Uri_HAS_EXTENSION
;
4888 if(This
->fragment_start
> -1)
4889 *pdwProperties
|= Uri_HAS_FRAGMENT
;
4894 static HRESULT WINAPI
Uri_IsEqual(IUri
*iface
, IUri
*pUri
, BOOL
*pfEqual
)
4896 Uri
*This
= URI_THIS(iface
);
4899 TRACE("(%p)->(%p %p)\n", This
, pUri
, pfEqual
);
4907 /* For some reason Windows returns S_OK here... */
4911 /* Try to convert it to a Uri (allows for a more simple comparison). */
4912 if((other
= get_uri_obj(pUri
)))
4913 *pfEqual
= are_equal_simple(This
, other
);
4915 /* Do it the hard way. */
4916 FIXME("(%p)->(%p %p) No support for unknown IUri's yet.\n", iface
, pUri
, pfEqual
);
4925 static const IUriVtbl UriVtbl
= {
4929 Uri_GetPropertyBSTR
,
4930 Uri_GetPropertyLength
,
4931 Uri_GetPropertyDWORD
,
4942 Uri_GetPathAndQuery
,
4956 static Uri
* create_uri_obj(void) {
4957 Uri
*ret
= heap_alloc_zero(sizeof(Uri
));
4959 ret
->lpIUriVtbl
= &UriVtbl
;
4966 /***********************************************************************
4967 * CreateUri (urlmon.@)
4969 * Creates a new IUri object using the URI represented by pwzURI. This function
4970 * parses and validates the components of pwzURI and then canonicalizes the
4971 * parsed components.
4974 * pwzURI [I] The URI to parse, validate, and canonicalize.
4975 * dwFlags [I] Flags which can affect how the parsing/canonicalization is performed.
4976 * dwReserved [I] Reserved (not used).
4977 * ppURI [O] The resulting IUri after parsing/canonicalization occurs.
4980 * Success: Returns S_OK. ppURI contains the pointer to the newly allocated IUri.
4981 * Failure: E_INVALIDARG if there's invalid flag combinations in dwFlags, or an
4982 * invalid parameters, or pwzURI doesn't represnt a valid URI.
4983 * E_OUTOFMEMORY if any memory allocation fails.
4987 * Uri_CREATE_CANONICALIZE, Uri_CREATE_DECODE_EXTRA_INFO, Uri_CREATE_CRACK_UNKNOWN_SCHEMES,
4988 * Uri_CREATE_PRE_PROCESS_HTML_URI, Uri_CREATE_NO_IE_SETTINGS.
4990 HRESULT WINAPI
CreateUri(LPCWSTR pwzURI
, DWORD dwFlags
, DWORD_PTR dwReserved
, IUri
**ppURI
)
4992 const DWORD supported_flags
= Uri_CREATE_ALLOW_RELATIVE
|Uri_CREATE_ALLOW_IMPLICIT_WILDCARD_SCHEME
|
4993 Uri_CREATE_ALLOW_IMPLICIT_FILE_SCHEME
|Uri_CREATE_NO_CANONICALIZE
|Uri_CREATE_CANONICALIZE
|
4994 Uri_CREATE_DECODE_EXTRA_INFO
|Uri_CREATE_NO_DECODE_EXTRA_INFO
|Uri_CREATE_CRACK_UNKNOWN_SCHEMES
|
4995 Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES
|Uri_CREATE_PRE_PROCESS_HTML_URI
|Uri_CREATE_NO_PRE_PROCESS_HTML_URI
|
4996 Uri_CREATE_NO_IE_SETTINGS
|Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS
|Uri_CREATE_FILE_USE_DOS_PATH
;
5001 TRACE("(%s %x %x %p)\n", debugstr_w(pwzURI
), dwFlags
, (DWORD
)dwReserved
, ppURI
);
5004 return E_INVALIDARG
;
5006 if(!pwzURI
|| !*pwzURI
) {
5008 return E_INVALIDARG
;
5011 /* Check for invalid flags. */
5012 if(has_invalid_flag_combination(dwFlags
)) {
5014 return E_INVALIDARG
;
5017 /* Currently unsupported. */
5018 if(dwFlags
& ~supported_flags
)
5019 FIXME("Ignoring unsupported flag(s) %x\n", dwFlags
& ~supported_flags
);
5021 ret
= create_uri_obj();
5024 return E_OUTOFMEMORY
;
5027 /* Explicitly set the default flags if it doesn't cause a flag conflict. */
5028 apply_default_flags(&dwFlags
);
5030 /* Pre process the URI, unless told otherwise. */
5031 if(!(dwFlags
& Uri_CREATE_NO_PRE_PROCESS_HTML_URI
))
5032 ret
->raw_uri
= pre_process_uri(pwzURI
);
5034 ret
->raw_uri
= SysAllocString(pwzURI
);
5038 return E_OUTOFMEMORY
;
5041 memset(&data
, 0, sizeof(parse_data
));
5042 data
.uri
= ret
->raw_uri
;
5044 /* Validate and parse the URI into it's components. */
5045 if(!parse_uri(&data
, dwFlags
)) {
5046 /* Encountered an unsupported or invalid URI */
5047 IUri_Release(URI(ret
));
5049 return E_INVALIDARG
;
5052 /* Canonicalize the URI. */
5053 hr
= canonicalize_uri(&data
, ret
, dwFlags
);
5055 IUri_Release(URI(ret
));
5060 ret
->create_flags
= dwFlags
;
5066 /***********************************************************************
5067 * CreateUriWithFragment (urlmon.@)
5069 * Creates a new IUri object. This is almost the same as CreateUri, expect that
5070 * it allows you to explicitly specify a fragment (pwzFragment) for pwzURI.
5073 * pwzURI [I] The URI to parse and perform canonicalization on.
5074 * pwzFragment [I] The explict fragment string which should be added to pwzURI.
5075 * dwFlags [I] The flags which will be passed to CreateUri.
5076 * dwReserved [I] Reserved (not used).
5077 * ppURI [O] The resulting IUri after parsing/canonicalization.
5080 * Success: S_OK. ppURI contains the pointer to the newly allocated IUri.
5081 * Failure: E_INVALIDARG if pwzURI already contains a fragment and pwzFragment
5082 * isn't NULL. Will also return E_INVALIDARG for the same reasons as
5083 * CreateUri will. E_OUTOFMEMORY if any allocations fail.
5085 HRESULT WINAPI
CreateUriWithFragment(LPCWSTR pwzURI
, LPCWSTR pwzFragment
, DWORD dwFlags
,
5086 DWORD_PTR dwReserved
, IUri
**ppURI
)
5089 TRACE("(%s %s %x %x %p)\n", debugstr_w(pwzURI
), debugstr_w(pwzFragment
), dwFlags
, (DWORD
)dwReserved
, ppURI
);
5092 return E_INVALIDARG
;
5096 return E_INVALIDARG
;
5099 /* Check if a fragment should be appended to the URI string. */
5102 DWORD uri_len
, frag_len
;
5105 /* Check if the original URI already has a fragment component. */
5106 if(StrChrW(pwzURI
, '#')) {
5108 return E_INVALIDARG
;
5111 uri_len
= lstrlenW(pwzURI
);
5112 frag_len
= lstrlenW(pwzFragment
);
5114 /* If the fragment doesn't start with a '#', one will be added. */
5115 add_pound
= *pwzFragment
!= '#';
5118 uriW
= heap_alloc((uri_len
+frag_len
+2)*sizeof(WCHAR
));
5120 uriW
= heap_alloc((uri_len
+frag_len
+1)*sizeof(WCHAR
));
5123 return E_OUTOFMEMORY
;
5125 memcpy(uriW
, pwzURI
, uri_len
*sizeof(WCHAR
));
5127 uriW
[uri_len
++] = '#';
5128 memcpy(uriW
+uri_len
, pwzFragment
, (frag_len
+1)*sizeof(WCHAR
));
5130 hres
= CreateUri(uriW
, dwFlags
, 0, ppURI
);
5134 /* A fragment string wasn't specified, so just forward the call. */
5135 hres
= CreateUri(pwzURI
, dwFlags
, 0, ppURI
);
5140 static HRESULT
build_uri(const UriBuilder
*builder
, IUri
**uri
, DWORD create_flags
,
5141 DWORD use_orig_flags
, DWORD encoding_mask
)
5150 if(encoding_mask
&& (!builder
->uri
|| builder
->modified_props
)) {
5155 /* Decide what flags should be used when creating the Uri. */
5156 if((use_orig_flags
& UriBuilder_USE_ORIGINAL_FLAGS
) && builder
->uri
)
5157 create_flags
= builder
->uri
->create_flags
;
5159 if(has_invalid_flag_combination(create_flags
)) {
5161 return E_INVALIDARG
;
5164 /* Set the default flags if they don't cause a conflict. */
5165 apply_default_flags(&create_flags
);
5168 /* Return the base IUri if no changes have been made and the create_flags match. */
5169 if(builder
->uri
&& !builder
->modified_props
&& builder
->uri
->create_flags
== create_flags
) {
5170 *uri
= URI(builder
->uri
);
5175 hr
= validate_components(builder
, &data
, create_flags
);
5181 ret
= create_uri_obj();
5184 return E_OUTOFMEMORY
;
5187 hr
= generate_uri(builder
, &data
, ret
, create_flags
);
5189 IUri_Release(URI(ret
));
5198 #define URIBUILDER_THIS(iface) DEFINE_THIS(UriBuilder, IUriBuilder, iface)
5200 static HRESULT WINAPI
UriBuilder_QueryInterface(IUriBuilder
*iface
, REFIID riid
, void **ppv
)
5202 UriBuilder
*This
= URIBUILDER_THIS(iface
);
5204 if(IsEqualGUID(&IID_IUnknown
, riid
)) {
5205 TRACE("(%p)->(IID_IUnknown %p)\n", This
, ppv
);
5206 *ppv
= URIBUILDER(This
);
5207 }else if(IsEqualGUID(&IID_IUriBuilder
, riid
)) {
5208 TRACE("(%p)->(IID_IUri %p)\n", This
, ppv
);
5209 *ppv
= URIBUILDER(This
);
5211 TRACE("(%p)->(%s %p)\n", This
, debugstr_guid(riid
), ppv
);
5213 return E_NOINTERFACE
;
5216 IUnknown_AddRef((IUnknown
*)*ppv
);
5220 static ULONG WINAPI
UriBuilder_AddRef(IUriBuilder
*iface
)
5222 UriBuilder
*This
= URIBUILDER_THIS(iface
);
5223 LONG ref
= InterlockedIncrement(&This
->ref
);
5225 TRACE("(%p) ref=%d\n", This
, ref
);
5230 static ULONG WINAPI
UriBuilder_Release(IUriBuilder
*iface
)
5232 UriBuilder
*This
= URIBUILDER_THIS(iface
);
5233 LONG ref
= InterlockedDecrement(&This
->ref
);
5235 TRACE("(%p) ref=%d\n", This
, ref
);
5238 if(This
->uri
) IUri_Release(URI(This
->uri
));
5239 heap_free(This
->fragment
);
5240 heap_free(This
->host
);
5241 heap_free(This
->password
);
5242 heap_free(This
->path
);
5243 heap_free(This
->query
);
5244 heap_free(This
->scheme
);
5245 heap_free(This
->username
);
5252 static HRESULT WINAPI
UriBuilder_CreateUriSimple(IUriBuilder
*iface
,
5253 DWORD dwAllowEncodingPropertyMask
,
5254 DWORD_PTR dwReserved
,
5257 UriBuilder
*This
= URIBUILDER_THIS(iface
);
5259 TRACE("(%p)->(%d %d %p)\n", This
, dwAllowEncodingPropertyMask
, (DWORD
)dwReserved
, ppIUri
);
5261 hr
= build_uri(This
, ppIUri
, 0, UriBuilder_USE_ORIGINAL_FLAGS
, dwAllowEncodingPropertyMask
);
5263 FIXME("(%p)->(%d %d %p)\n", This
, dwAllowEncodingPropertyMask
, (DWORD
)dwReserved
, ppIUri
);
5267 static HRESULT WINAPI
UriBuilder_CreateUri(IUriBuilder
*iface
,
5268 DWORD dwCreateFlags
,
5269 DWORD dwAllowEncodingPropertyMask
,
5270 DWORD_PTR dwReserved
,
5273 UriBuilder
*This
= URIBUILDER_THIS(iface
);
5275 TRACE("(%p)->(0x%08x %d %d %p)\n", This
, dwCreateFlags
, dwAllowEncodingPropertyMask
, (DWORD
)dwReserved
, ppIUri
);
5277 if(dwCreateFlags
== -1)
5278 hr
= build_uri(This
, ppIUri
, 0, UriBuilder_USE_ORIGINAL_FLAGS
, dwAllowEncodingPropertyMask
);
5280 hr
= build_uri(This
, ppIUri
, dwCreateFlags
, 0, dwAllowEncodingPropertyMask
);
5283 FIXME("(%p)->(0x%08x %d %d %p)\n", This
, dwCreateFlags
, dwAllowEncodingPropertyMask
, (DWORD
)dwReserved
, ppIUri
);
5287 static HRESULT WINAPI
UriBuilder_CreateUriWithFlags(IUriBuilder
*iface
,
5288 DWORD dwCreateFlags
,
5289 DWORD dwUriBuilderFlags
,
5290 DWORD dwAllowEncodingPropertyMask
,
5291 DWORD_PTR dwReserved
,
5294 UriBuilder
*This
= URIBUILDER_THIS(iface
);
5296 TRACE("(%p)->(0x%08x 0x%08x %d %d %p)\n", This
, dwCreateFlags
, dwUriBuilderFlags
,
5297 dwAllowEncodingPropertyMask
, (DWORD
)dwReserved
, ppIUri
);
5299 hr
= build_uri(This
, ppIUri
, dwCreateFlags
, dwUriBuilderFlags
, dwAllowEncodingPropertyMask
);
5301 FIXME("(%p)->(0x%08x 0x%08x %d %d %p)\n", This
, dwCreateFlags
, dwUriBuilderFlags
,
5302 dwAllowEncodingPropertyMask
, (DWORD
)dwReserved
, ppIUri
);
5306 static HRESULT WINAPI
UriBuilder_GetIUri(IUriBuilder
*iface
, IUri
**ppIUri
)
5308 UriBuilder
*This
= URIBUILDER_THIS(iface
);
5309 TRACE("(%p)->(%p)\n", This
, ppIUri
);
5315 IUri
*uri
= URI(This
->uri
);
5324 static HRESULT WINAPI
UriBuilder_SetIUri(IUriBuilder
*iface
, IUri
*pIUri
)
5326 UriBuilder
*This
= URIBUILDER_THIS(iface
);
5327 TRACE("(%p)->(%p)\n", This
, pIUri
);
5332 if((uri
= get_uri_obj(pIUri
))) {
5333 /* Only reset the builder if it's Uri isn't the same as
5334 * the Uri passed to the function.
5336 if(This
->uri
!= uri
) {
5337 reset_builder(This
);
5341 This
->port
= uri
->port
;
5346 FIXME("(%p)->(%p) Unknown IUri types not supported yet.\n", This
, pIUri
);
5349 } else if(This
->uri
)
5350 /* Only reset the builder if it's Uri isn't NULL. */
5351 reset_builder(This
);
5356 static HRESULT WINAPI
UriBuilder_GetFragment(IUriBuilder
*iface
, DWORD
*pcchFragment
, LPCWSTR
*ppwzFragment
)
5358 UriBuilder
*This
= URIBUILDER_THIS(iface
);
5359 TRACE("(%p)->(%p %p)\n", This
, pcchFragment
, ppwzFragment
);
5361 if(!This
->uri
|| This
->uri
->fragment_start
== -1 || This
->modified_props
& Uri_HAS_FRAGMENT
)
5362 return get_builder_component(&This
->fragment
, &This
->fragment_len
, NULL
, 0, ppwzFragment
, pcchFragment
);
5364 return get_builder_component(&This
->fragment
, &This
->fragment_len
, This
->uri
->canon_uri
+This
->uri
->fragment_start
,
5365 This
->uri
->fragment_len
, ppwzFragment
, pcchFragment
);
5368 static HRESULT WINAPI
UriBuilder_GetHost(IUriBuilder
*iface
, DWORD
*pcchHost
, LPCWSTR
*ppwzHost
)
5370 UriBuilder
*This
= URIBUILDER_THIS(iface
);
5371 TRACE("(%p)->(%p %p)\n", This
, pcchHost
, ppwzHost
);
5373 if(!This
->uri
|| This
->uri
->host_start
== -1 || This
->modified_props
& Uri_HAS_HOST
)
5374 return get_builder_component(&This
->host
, &This
->host_len
, NULL
, 0, ppwzHost
, pcchHost
);
5376 if(This
->uri
->host_type
== Uri_HOST_IPV6
)
5377 /* Don't include the '[' and ']' around the address. */
5378 return get_builder_component(&This
->host
, &This
->host_len
, This
->uri
->canon_uri
+This
->uri
->host_start
+1,
5379 This
->uri
->host_len
-2, ppwzHost
, pcchHost
);
5381 return get_builder_component(&This
->host
, &This
->host_len
, This
->uri
->canon_uri
+This
->uri
->host_start
,
5382 This
->uri
->host_len
, ppwzHost
, pcchHost
);
5386 static HRESULT WINAPI
UriBuilder_GetPassword(IUriBuilder
*iface
, DWORD
*pcchPassword
, LPCWSTR
*ppwzPassword
)
5388 UriBuilder
*This
= URIBUILDER_THIS(iface
);
5389 TRACE("(%p)->(%p %p)\n", This
, pcchPassword
, ppwzPassword
);
5391 if(!This
->uri
|| This
->uri
->userinfo_split
== -1 || This
->modified_props
& Uri_HAS_PASSWORD
)
5392 return get_builder_component(&This
->password
, &This
->password_len
, NULL
, 0, ppwzPassword
, pcchPassword
);
5394 const WCHAR
*start
= This
->uri
->canon_uri
+This
->uri
->userinfo_start
+This
->uri
->userinfo_split
+1;
5395 DWORD len
= This
->uri
->userinfo_len
-This
->uri
->userinfo_split
-1;
5396 return get_builder_component(&This
->password
, &This
->password_len
, start
, len
, ppwzPassword
, pcchPassword
);
5400 static HRESULT WINAPI
UriBuilder_GetPath(IUriBuilder
*iface
, DWORD
*pcchPath
, LPCWSTR
*ppwzPath
)
5402 UriBuilder
*This
= URIBUILDER_THIS(iface
);
5403 TRACE("(%p)->(%p %p)\n", This
, pcchPath
, ppwzPath
);
5405 if(!This
->uri
|| This
->uri
->path_start
== -1 || This
->modified_props
& Uri_HAS_PATH
)
5406 return get_builder_component(&This
->path
, &This
->path_len
, NULL
, 0, ppwzPath
, pcchPath
);
5408 return get_builder_component(&This
->path
, &This
->path_len
, This
->uri
->canon_uri
+This
->uri
->path_start
,
5409 This
->uri
->path_len
, ppwzPath
, pcchPath
);
5412 static HRESULT WINAPI
UriBuilder_GetPort(IUriBuilder
*iface
, BOOL
*pfHasPort
, DWORD
*pdwPort
)
5414 UriBuilder
*This
= URIBUILDER_THIS(iface
);
5415 TRACE("(%p)->(%p %p)\n", This
, pfHasPort
, pdwPort
);
5428 *pfHasPort
= This
->has_port
;
5429 *pdwPort
= This
->port
;
5433 static HRESULT WINAPI
UriBuilder_GetQuery(IUriBuilder
*iface
, DWORD
*pcchQuery
, LPCWSTR
*ppwzQuery
)
5435 UriBuilder
*This
= URIBUILDER_THIS(iface
);
5436 TRACE("(%p)->(%p %p)\n", This
, pcchQuery
, ppwzQuery
);
5438 if(!This
->uri
|| This
->uri
->query_start
== -1 || This
->modified_props
& Uri_HAS_QUERY
)
5439 return get_builder_component(&This
->query
, &This
->query_len
, NULL
, 0, ppwzQuery
, pcchQuery
);
5441 return get_builder_component(&This
->query
, &This
->query_len
, This
->uri
->canon_uri
+This
->uri
->query_start
,
5442 This
->uri
->query_len
, ppwzQuery
, pcchQuery
);
5445 static HRESULT WINAPI
UriBuilder_GetSchemeName(IUriBuilder
*iface
, DWORD
*pcchSchemeName
, LPCWSTR
*ppwzSchemeName
)
5447 UriBuilder
*This
= URIBUILDER_THIS(iface
);
5448 TRACE("(%p)->(%p %p)\n", This
, pcchSchemeName
, ppwzSchemeName
);
5450 if(!This
->uri
|| This
->uri
->scheme_start
== -1 || This
->modified_props
& Uri_HAS_SCHEME_NAME
)
5451 return get_builder_component(&This
->scheme
, &This
->scheme_len
, NULL
, 0, ppwzSchemeName
, pcchSchemeName
);
5453 return get_builder_component(&This
->scheme
, &This
->scheme_len
, This
->uri
->canon_uri
+This
->uri
->scheme_start
,
5454 This
->uri
->scheme_len
, ppwzSchemeName
, pcchSchemeName
);
5457 static HRESULT WINAPI
UriBuilder_GetUserName(IUriBuilder
*iface
, DWORD
*pcchUserName
, LPCWSTR
*ppwzUserName
)
5459 UriBuilder
*This
= URIBUILDER_THIS(iface
);
5460 TRACE("(%p)->(%p %p)\n", This
, pcchUserName
, ppwzUserName
);
5462 if(!This
->uri
|| This
->uri
->userinfo_start
== -1 || This
->uri
->userinfo_split
== 0 ||
5463 This
->modified_props
& Uri_HAS_USER_NAME
)
5464 return get_builder_component(&This
->username
, &This
->username_len
, NULL
, 0, ppwzUserName
, pcchUserName
);
5466 const WCHAR
*start
= This
->uri
->canon_uri
+This
->uri
->userinfo_start
;
5468 /* Check if there's a password in the userinfo section. */
5469 if(This
->uri
->userinfo_split
> -1)
5470 /* Don't include the password. */
5471 return get_builder_component(&This
->username
, &This
->username_len
, start
,
5472 This
->uri
->userinfo_split
, ppwzUserName
, pcchUserName
);
5474 return get_builder_component(&This
->username
, &This
->username_len
, start
,
5475 This
->uri
->userinfo_len
, ppwzUserName
, pcchUserName
);
5479 static HRESULT WINAPI
UriBuilder_SetFragment(IUriBuilder
*iface
, LPCWSTR pwzNewValue
)
5481 UriBuilder
*This
= URIBUILDER_THIS(iface
);
5482 TRACE("(%p)->(%s)\n", This
, debugstr_w(pwzNewValue
));
5483 return set_builder_component(&This
->fragment
, &This
->fragment_len
, pwzNewValue
, '#',
5484 &This
->modified_props
, Uri_HAS_FRAGMENT
);
5487 static HRESULT WINAPI
UriBuilder_SetHost(IUriBuilder
*iface
, LPCWSTR pwzNewValue
)
5489 UriBuilder
*This
= URIBUILDER_THIS(iface
);
5490 TRACE("(%p)->(%s)\n", This
, debugstr_w(pwzNewValue
));
5492 /* Host name can't be set to NULL. */
5494 return E_INVALIDARG
;
5496 return set_builder_component(&This
->host
, &This
->host_len
, pwzNewValue
, 0,
5497 &This
->modified_props
, Uri_HAS_HOST
);
5500 static HRESULT WINAPI
UriBuilder_SetPassword(IUriBuilder
*iface
, LPCWSTR pwzNewValue
)
5502 UriBuilder
*This
= URIBUILDER_THIS(iface
);
5503 TRACE("(%p)->(%s)\n", This
, debugstr_w(pwzNewValue
));
5504 return set_builder_component(&This
->password
, &This
->password_len
, pwzNewValue
, 0,
5505 &This
->modified_props
, Uri_HAS_PASSWORD
);
5508 static HRESULT WINAPI
UriBuilder_SetPath(IUriBuilder
*iface
, LPCWSTR pwzNewValue
)
5510 UriBuilder
*This
= URIBUILDER_THIS(iface
);
5511 TRACE("(%p)->(%s)\n", This
, debugstr_w(pwzNewValue
));
5512 return set_builder_component(&This
->path
, &This
->path_len
, pwzNewValue
, 0,
5513 &This
->modified_props
, Uri_HAS_PATH
);
5516 static HRESULT WINAPI
UriBuilder_SetPort(IUriBuilder
*iface
, BOOL fHasPort
, DWORD dwNewValue
)
5518 UriBuilder
*This
= URIBUILDER_THIS(iface
);
5519 TRACE("(%p)->(%d %d)\n", This
, fHasPort
, dwNewValue
);
5521 This
->has_port
= fHasPort
;
5522 This
->port
= dwNewValue
;
5523 This
->modified_props
|= Uri_HAS_PORT
;
5527 static HRESULT WINAPI
UriBuilder_SetQuery(IUriBuilder
*iface
, LPCWSTR pwzNewValue
)
5529 UriBuilder
*This
= URIBUILDER_THIS(iface
);
5530 TRACE("(%p)->(%s)\n", This
, debugstr_w(pwzNewValue
));
5531 return set_builder_component(&This
->query
, &This
->query_len
, pwzNewValue
, '?',
5532 &This
->modified_props
, Uri_HAS_QUERY
);
5535 static HRESULT WINAPI
UriBuilder_SetSchemeName(IUriBuilder
*iface
, LPCWSTR pwzNewValue
)
5537 UriBuilder
*This
= URIBUILDER_THIS(iface
);
5538 TRACE("(%p)->(%s)\n", This
, debugstr_w(pwzNewValue
));
5540 /* Only set the scheme name if it's not NULL or empty. */
5541 if(!pwzNewValue
|| !*pwzNewValue
)
5542 return E_INVALIDARG
;
5544 return set_builder_component(&This
->scheme
, &This
->scheme_len
, pwzNewValue
, 0,
5545 &This
->modified_props
, Uri_HAS_SCHEME_NAME
);
5548 static HRESULT WINAPI
UriBuilder_SetUserName(IUriBuilder
*iface
, LPCWSTR pwzNewValue
)
5550 UriBuilder
*This
= URIBUILDER_THIS(iface
);
5551 TRACE("(%p)->(%s)\n", This
, debugstr_w(pwzNewValue
));
5552 return set_builder_component(&This
->username
, &This
->username_len
, pwzNewValue
, 0,
5553 &This
->modified_props
, Uri_HAS_USER_NAME
);
5556 static HRESULT WINAPI
UriBuilder_RemoveProperties(IUriBuilder
*iface
, DWORD dwPropertyMask
)
5558 const DWORD accepted_flags
= Uri_HAS_AUTHORITY
|Uri_HAS_DOMAIN
|Uri_HAS_EXTENSION
|Uri_HAS_FRAGMENT
|Uri_HAS_HOST
|
5559 Uri_HAS_PASSWORD
|Uri_HAS_PATH
|Uri_HAS_PATH_AND_QUERY
|Uri_HAS_QUERY
|
5560 Uri_HAS_USER_INFO
|Uri_HAS_USER_NAME
;
5562 UriBuilder
*This
= URIBUILDER_THIS(iface
);
5563 TRACE("(%p)->(0x%08x)\n", This
, dwPropertyMask
);
5565 if(dwPropertyMask
& ~accepted_flags
)
5566 return E_INVALIDARG
;
5568 if(dwPropertyMask
& Uri_HAS_FRAGMENT
)
5569 UriBuilder_SetFragment(iface
, NULL
);
5571 /* Even though you can't set the host name to NULL or an
5572 * empty string, you can still remove it... for some reason.
5574 if(dwPropertyMask
& Uri_HAS_HOST
)
5575 set_builder_component(&This
->host
, &This
->host_len
, NULL
, 0,
5576 &This
->modified_props
, Uri_HAS_HOST
);
5578 if(dwPropertyMask
& Uri_HAS_PASSWORD
)
5579 UriBuilder_SetPassword(iface
, NULL
);
5581 if(dwPropertyMask
& Uri_HAS_PATH
)
5582 UriBuilder_SetPath(iface
, NULL
);
5584 if(dwPropertyMask
& Uri_HAS_PORT
)
5585 UriBuilder_SetPort(iface
, FALSE
, 0);
5587 if(dwPropertyMask
& Uri_HAS_QUERY
)
5588 UriBuilder_SetQuery(iface
, NULL
);
5590 if(dwPropertyMask
& Uri_HAS_USER_NAME
)
5591 UriBuilder_SetUserName(iface
, NULL
);
5596 static HRESULT WINAPI
UriBuilder_HasBeenModified(IUriBuilder
*iface
, BOOL
*pfModified
)
5598 UriBuilder
*This
= URIBUILDER_THIS(iface
);
5599 TRACE("(%p)->(%p)\n", This
, pfModified
);
5604 *pfModified
= This
->modified_props
> 0;
5608 #undef URIBUILDER_THIS
5610 static const IUriBuilderVtbl UriBuilderVtbl
= {
5611 UriBuilder_QueryInterface
,
5614 UriBuilder_CreateUriSimple
,
5615 UriBuilder_CreateUri
,
5616 UriBuilder_CreateUriWithFlags
,
5619 UriBuilder_GetFragment
,
5621 UriBuilder_GetPassword
,
5624 UriBuilder_GetQuery
,
5625 UriBuilder_GetSchemeName
,
5626 UriBuilder_GetUserName
,
5627 UriBuilder_SetFragment
,
5629 UriBuilder_SetPassword
,
5632 UriBuilder_SetQuery
,
5633 UriBuilder_SetSchemeName
,
5634 UriBuilder_SetUserName
,
5635 UriBuilder_RemoveProperties
,
5636 UriBuilder_HasBeenModified
,
5639 /***********************************************************************
5640 * CreateIUriBuilder (urlmon.@)
5642 HRESULT WINAPI
CreateIUriBuilder(IUri
*pIUri
, DWORD dwFlags
, DWORD_PTR dwReserved
, IUriBuilder
**ppIUriBuilder
)
5646 TRACE("(%p %x %x %p)\n", pIUri
, dwFlags
, (DWORD
)dwReserved
, ppIUriBuilder
);
5651 ret
= heap_alloc_zero(sizeof(UriBuilder
));
5653 return E_OUTOFMEMORY
;
5655 ret
->lpIUriBuilderVtbl
= &UriBuilderVtbl
;
5661 if((uri
= get_uri_obj(pIUri
))) {
5666 /* Windows doesn't set 'has_port' to TRUE in this case. */
5667 ret
->port
= uri
->port
;
5671 *ppIUriBuilder
= NULL
;
5672 FIXME("(%p %x %x %p): Unknown IUri types not supported yet.\n", pIUri
, dwFlags
,
5673 (DWORD
)dwReserved
, ppIUriBuilder
);
5678 *ppIUriBuilder
= URIBUILDER(ret
);
5682 /* Merges the base path with the relative path and stores the resulting path
5683 * and path len in 'result' and 'result_len'.
5685 static HRESULT
merge_paths(parse_data
*data
, const WCHAR
*base
, DWORD base_len
, const WCHAR
*relative
,
5686 DWORD relative_len
, WCHAR
**result
, DWORD
*result_len
, DWORD flags
)
5688 const WCHAR
*end
= NULL
;
5689 DWORD base_copy_len
= 0;
5693 /* Find the characters the will be copied over from
5696 end
= str_last_of(base
, base
+(base_len
-1), '/');
5697 if(!end
&& data
->scheme_type
== URL_SCHEME_FILE
)
5698 /* Try looking for a '\\'. */
5699 end
= str_last_of(base
, base
+(base_len
-1), '\\');
5703 base_copy_len
= (end
+1)-base
;
5704 *result
= heap_alloc((base_copy_len
+relative_len
+1)*sizeof(WCHAR
));
5706 *result
= heap_alloc((relative_len
+1)*sizeof(WCHAR
));
5710 return E_OUTOFMEMORY
;
5715 memcpy(ptr
, base
, base_copy_len
*sizeof(WCHAR
));
5716 ptr
+= base_copy_len
;
5719 memcpy(ptr
, relative
, relative_len
*sizeof(WCHAR
));
5720 ptr
+= relative_len
;
5723 *result_len
= (ptr
-*result
);
5727 static HRESULT
combine_uri(Uri
*base
, Uri
*relative
, DWORD flags
, IUri
**result
) {
5731 DWORD create_flags
= 0, len
= 0;
5733 memset(&data
, 0, sizeof(parse_data
));
5735 /* Base case is when the relative Uri has a scheme name,
5736 * if it does, then 'result' will contain the same data
5737 * as the relative Uri.
5739 if(relative
->scheme_start
> -1) {
5740 data
.uri
= SysAllocString(relative
->raw_uri
);
5743 return E_OUTOFMEMORY
;
5746 parse_uri(&data
, 0);
5748 ret
= create_uri_obj();
5751 return E_OUTOFMEMORY
;
5754 ret
->raw_uri
= data
.uri
;
5755 hr
= canonicalize_uri(&data
, ret
, 0);
5757 IUri_Release(URI(ret
));
5762 apply_default_flags(&create_flags
);
5763 ret
->create_flags
= create_flags
;
5768 DWORD raw_flags
= 0;
5770 if(base
->scheme_start
> -1) {
5771 data
.scheme
= base
->canon_uri
+base
->scheme_start
;
5772 data
.scheme_len
= base
->scheme_len
;
5773 data
.scheme_type
= base
->scheme_type
;
5775 data
.is_relative
= TRUE
;
5776 data
.scheme_type
= URL_SCHEME_UNKNOWN
;
5777 create_flags
|= Uri_CREATE_ALLOW_RELATIVE
;
5780 if(base
->authority_start
> -1) {
5781 if(base
->userinfo_start
> -1 && base
->userinfo_split
!= 0) {
5782 data
.username
= base
->canon_uri
+base
->userinfo_start
;
5783 data
.username_len
= (base
->userinfo_split
> -1) ? base
->userinfo_split
: base
->userinfo_len
;
5786 if(base
->userinfo_split
> -1) {
5787 data
.password
= base
->canon_uri
+base
->userinfo_start
+base
->userinfo_split
+1;
5788 data
.password_len
= base
->userinfo_len
-base
->userinfo_split
-1;
5791 if(base
->host_start
> -1) {
5792 data
.host
= base
->canon_uri
+base
->host_start
;
5793 data
.host_len
= base
->host_len
;
5794 data
.host_type
= base
->host_type
;
5797 if(base
->has_port
) {
5798 data
.has_port
= TRUE
;
5799 data
.port_value
= base
->port
;
5801 } else if(base
->scheme_type
!= URL_SCHEME_FILE
)
5802 data
.is_opaque
= TRUE
;
5804 if(relative
->path_start
== -1 || !relative
->path_len
) {
5805 if(base
->path_start
> -1) {
5806 data
.path
= base
->canon_uri
+base
->path_start
;
5807 data
.path_len
= base
->path_len
;
5808 } else if((base
->path_start
== -1 || !base
->path_len
) && !data
.is_opaque
) {
5809 /* Just set the path as a '/' if the base didn't have
5810 * one and if it's an hierarchical URI.
5812 static const WCHAR slashW
[] = {'/',0};
5817 if(relative
->query_start
> -1) {
5818 data
.query
= relative
->canon_uri
+relative
->query_start
;
5819 data
.query_len
= relative
->query_len
;
5820 } else if(base
->query_start
> -1) {
5821 data
.query
= base
->canon_uri
+base
->query_start
;
5822 data
.query_len
= base
->query_len
;
5825 const WCHAR
*ptr
, **pptr
;
5826 DWORD path_offset
= 0, path_len
= 0;
5828 /* There's two possibilities on what will happen to the path component
5829 * of the result IUri. First, if the relative path begins with a '/'
5830 * then the resulting path will just be the relative path. Second, if
5831 * relative path doesn't begin with a '/' then the base path and relative
5832 * path are merged together.
5834 if(relative
->path_len
&& *(relative
->canon_uri
+relative
->path_start
) == '/') {
5836 BOOL copy_drive_path
= FALSE
;
5838 /* If the relative IUri's path starts with a '/', then we
5839 * don't use the base IUri's path. Unless the base IUri
5840 * is a file URI, in which case it uses the drive path of
5841 * the base IUri (if it has any) in the new path.
5843 if(base
->scheme_type
== URL_SCHEME_FILE
) {
5844 if(base
->path_len
> 3 && *(base
->canon_uri
+base
->path_start
) == '/' &&
5845 is_drive_path(base
->canon_uri
+base
->path_start
+1)) {
5847 copy_drive_path
= TRUE
;
5851 path_len
+= relative
->path_len
;
5853 path
= heap_alloc((path_len
+1)*sizeof(WCHAR
));
5856 return E_OUTOFMEMORY
;
5861 /* Copy the base paths, drive path over. */
5862 if(copy_drive_path
) {
5863 memcpy(tmp
, base
->canon_uri
+base
->path_start
, 3*sizeof(WCHAR
));
5867 memcpy(tmp
, relative
->canon_uri
+relative
->path_start
, relative
->path_len
*sizeof(WCHAR
));
5868 path
[path_len
] = '\0';
5870 /* Merge the base path with the relative path. */
5871 hr
= merge_paths(&data
, base
->canon_uri
+base
->path_start
, base
->path_len
,
5872 relative
->canon_uri
+relative
->path_start
, relative
->path_len
,
5873 &path
, &path_len
, flags
);
5879 /* If the resulting IUri is a file URI, the drive path isn't
5880 * reduced out when the dot segments are removed.
5882 if(path_len
>= 3 && data
.scheme_type
== URL_SCHEME_FILE
&& !data
.host
) {
5883 if(*path
== '/' && is_drive_path(path
+1))
5885 else if(is_drive_path(path
))
5890 /* Check if the dot segments need to be removed from the path. */
5891 if(!(flags
& URL_DONT_SIMPLIFY
) && !data
.is_opaque
) {
5892 DWORD offset
= (path_offset
> 0) ? path_offset
+1 : 0;
5893 DWORD new_len
= remove_dot_segments(path
+offset
,path_len
-offset
);
5895 if(new_len
!= path_len
) {
5896 WCHAR
*tmp
= heap_realloc(path
, (path_offset
+new_len
+1)*sizeof(WCHAR
));
5900 return E_OUTOFMEMORY
;
5903 tmp
[new_len
+offset
] = '\0';
5905 path_len
= new_len
+offset
;
5909 /* Make sure the path component is valid. */
5912 if((data
.is_opaque
&& !parse_path_opaque(pptr
, &data
, 0)) ||
5913 (!data
.is_opaque
&& !parse_path_hierarchical(pptr
, &data
, 0))) {
5916 return E_INVALIDARG
;
5920 if(relative
->fragment_start
> -1) {
5921 data
.fragment
= relative
->canon_uri
+relative
->fragment_start
;
5922 data
.fragment_len
= relative
->fragment_len
;
5925 if(flags
& URL_DONT_SIMPLIFY
)
5926 raw_flags
|= RAW_URI_FORCE_PORT_DISP
;
5927 if(flags
& URL_FILE_USE_PATHURL
)
5928 raw_flags
|= RAW_URI_CONVERT_TO_DOS_PATH
;
5930 len
= generate_raw_uri(&data
, data
.uri
, raw_flags
);
5931 data
.uri
= SysAllocStringLen(NULL
, len
);
5935 return E_OUTOFMEMORY
;
5938 generate_raw_uri(&data
, data
.uri
, raw_flags
);
5940 ret
= create_uri_obj();
5942 SysFreeString(data
.uri
);
5945 return E_OUTOFMEMORY
;
5948 if(flags
& URL_DONT_SIMPLIFY
)
5949 create_flags
|= Uri_CREATE_NO_CANONICALIZE
;
5950 if(flags
& URL_FILE_USE_PATHURL
)
5951 create_flags
|= Uri_CREATE_FILE_USE_DOS_PATH
;
5953 ret
->raw_uri
= data
.uri
;
5954 hr
= canonicalize_uri(&data
, ret
, create_flags
);
5956 IUri_Release(URI(ret
));
5961 if(flags
& URL_DONT_SIMPLIFY
)
5962 ret
->display_modifiers
|= URI_DISPLAY_NO_DEFAULT_PORT_AUTH
;
5964 apply_default_flags(&create_flags
);
5965 ret
->create_flags
= create_flags
;
5974 /***********************************************************************
5975 * CoInternetCombineIUri (urlmon.@)
5977 HRESULT WINAPI
CoInternetCombineIUri(IUri
*pBaseUri
, IUri
*pRelativeUri
, DWORD dwCombineFlags
,
5978 IUri
**ppCombinedUri
, DWORD_PTR dwReserved
)
5981 Uri
*relative
, *base
;
5982 TRACE("(%p %p %x %p %x)\n", pBaseUri
, pRelativeUri
, dwCombineFlags
, ppCombinedUri
, (DWORD
)dwReserved
);
5985 return E_INVALIDARG
;
5987 if(!pBaseUri
|| !pRelativeUri
) {
5988 *ppCombinedUri
= NULL
;
5989 return E_INVALIDARG
;
5992 relative
= get_uri_obj(pRelativeUri
);
5993 base
= get_uri_obj(pBaseUri
);
5994 if(!relative
|| !base
) {
5995 *ppCombinedUri
= NULL
;
5996 FIXME("(%p %p %x %p %x) Unknown IUri types not supported yet.\n",
5997 pBaseUri
, pRelativeUri
, dwCombineFlags
, ppCombinedUri
, (DWORD
)dwReserved
);
6001 hr
= combine_uri(base
, relative
, dwCombineFlags
, ppCombinedUri
);
6003 FIXME("(%p %p %x %p %x): stub\n", pBaseUri
, pRelativeUri
, dwCombineFlags
, ppCombinedUri
, (DWORD
)dwReserved
);