2 * Copyright 2010 Jacek Caban for CodeWeavers
3 * Copyright 2010 Thomas Mullaly
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #include "urlmon_main.h"
21 #include "wine/debug.h"
23 #define NO_SHLWAPI_REG
26 #define UINT_MAX 0xffffffff
27 #define USHORT_MAX 0xffff
29 WINE_DEFAULT_DEBUG_CHANNEL(urlmon
);
31 static const IID IID_IUriObj
= {0x4b364760,0x9f51,0x11df,{0x98,0x1c,0x08,0x00,0x20,0x0c,0x9a,0x66}};
34 const IUriVtbl
*lpIUriVtbl
;
39 /* Information about the canonicalized URI's buffer. */
46 URL_SCHEME scheme_type
;
54 Uri_HOST_TYPE host_type
;
76 const IUriBuilderVtbl
*lpIUriBuilderVtbl
;
88 /* IPv6 addresses can hold up to 8 h16 components. */
92 /* An IPv6 can have 1 elision ("::"). */
95 /* An IPv6 can contain 1 IPv4 address as the last 32bits of the address. */
108 BOOL has_implicit_scheme
;
109 BOOL has_implicit_ip
;
114 URL_SCHEME scheme_type
;
116 const WCHAR
*userinfo
;
122 Uri_HOST_TYPE host_type
;
125 ipv6_address ipv6_address
;
137 const WCHAR
*fragment
;
141 static const CHAR hexDigits
[] = "0123456789ABCDEF";
143 /* List of scheme types/scheme names that are recognized by the IUri interface as of IE 7. */
144 static const struct {
146 WCHAR scheme_name
[16];
147 } recognized_schemes
[] = {
148 {URL_SCHEME_FTP
, {'f','t','p',0}},
149 {URL_SCHEME_HTTP
, {'h','t','t','p',0}},
150 {URL_SCHEME_GOPHER
, {'g','o','p','h','e','r',0}},
151 {URL_SCHEME_MAILTO
, {'m','a','i','l','t','o',0}},
152 {URL_SCHEME_NEWS
, {'n','e','w','s',0}},
153 {URL_SCHEME_NNTP
, {'n','n','t','p',0}},
154 {URL_SCHEME_TELNET
, {'t','e','l','n','e','t',0}},
155 {URL_SCHEME_WAIS
, {'w','a','i','s',0}},
156 {URL_SCHEME_FILE
, {'f','i','l','e',0}},
157 {URL_SCHEME_MK
, {'m','k',0}},
158 {URL_SCHEME_HTTPS
, {'h','t','t','p','s',0}},
159 {URL_SCHEME_SHELL
, {'s','h','e','l','l',0}},
160 {URL_SCHEME_SNEWS
, {'s','n','e','w','s',0}},
161 {URL_SCHEME_LOCAL
, {'l','o','c','a','l',0}},
162 {URL_SCHEME_JAVASCRIPT
, {'j','a','v','a','s','c','r','i','p','t',0}},
163 {URL_SCHEME_VBSCRIPT
, {'v','b','s','c','r','i','p','t',0}},
164 {URL_SCHEME_ABOUT
, {'a','b','o','u','t',0}},
165 {URL_SCHEME_RES
, {'r','e','s',0}},
166 {URL_SCHEME_MSSHELLROOTED
, {'m','s','-','s','h','e','l','l','-','r','o','o','t','e','d',0}},
167 {URL_SCHEME_MSSHELLIDLIST
, {'m','s','-','s','h','e','l','l','-','i','d','l','i','s','t',0}},
168 {URL_SCHEME_MSHELP
, {'h','c','p',0}},
169 {URL_SCHEME_WILDCARD
, {'*',0}}
172 /* List of default ports Windows recognizes. */
173 static const struct {
176 } default_ports
[] = {
177 {URL_SCHEME_FTP
, 21},
178 {URL_SCHEME_HTTP
, 80},
179 {URL_SCHEME_GOPHER
, 70},
180 {URL_SCHEME_NNTP
, 119},
181 {URL_SCHEME_TELNET
, 23},
182 {URL_SCHEME_WAIS
, 210},
183 {URL_SCHEME_HTTPS
, 443},
186 /* List of 3 character top level domain names Windows seems to recognize.
187 * There might be more, but, these are the only ones I've found so far.
189 static const struct {
191 } recognized_tlds
[] = {
201 static Uri
*get_uri_obj(IUri
*uri
)
206 hres
= IUri_QueryInterface(uri
, &IID_IUriObj
, (void**)&ret
);
207 return SUCCEEDED(hres
) ? ret
: NULL
;
210 static inline BOOL
is_alpha(WCHAR val
) {
211 return ((val
>= 'a' && val
<= 'z') || (val
>= 'A' && val
<= 'Z'));
214 static inline BOOL
is_num(WCHAR val
) {
215 return (val
>= '0' && val
<= '9');
218 /* A URI is implicitly a file path if it begins with
219 * a drive letter (eg X:) or starts with "\\" (UNC path).
221 static inline BOOL
is_implicit_file_path(const WCHAR
*str
) {
222 if(is_alpha(str
[0]) && str
[1] == ':')
224 else if(str
[0] == '\\' && str
[1] == '\\')
230 /* Checks if the URI is a hierarchical URI. A hierarchical
231 * URI is one that has "//" after the scheme.
233 static BOOL
check_hierarchical(const WCHAR
**ptr
) {
234 const WCHAR
*start
= *ptr
;
249 /* unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~" */
250 static inline BOOL
is_unreserved(WCHAR val
) {
251 return (is_alpha(val
) || is_num(val
) || val
== '-' || val
== '.' ||
252 val
== '_' || val
== '~');
255 /* sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
256 * / "*" / "+" / "," / ";" / "="
258 static inline BOOL
is_subdelim(WCHAR val
) {
259 return (val
== '!' || val
== '$' || val
== '&' ||
260 val
== '\'' || val
== '(' || val
== ')' ||
261 val
== '*' || val
== '+' || val
== ',' ||
262 val
== ';' || val
== '=');
265 /* gen-delims = ":" / "/" / "?" / "#" / "[" / "]" / "@" */
266 static inline BOOL
is_gendelim(WCHAR val
) {
267 return (val
== ':' || val
== '/' || val
== '?' ||
268 val
== '#' || val
== '[' || val
== ']' ||
272 /* Characters that delimit the end of the authority
273 * section of a URI. Sometimes a '\\' is considered
274 * an authority delimeter.
276 static inline BOOL
is_auth_delim(WCHAR val
, BOOL acceptSlash
) {
277 return (val
== '#' || val
== '/' || val
== '?' ||
278 val
== '\0' || (acceptSlash
&& val
== '\\'));
281 /* reserved = gen-delims / sub-delims */
282 static inline BOOL
is_reserved(WCHAR val
) {
283 return (is_subdelim(val
) || is_gendelim(val
));
286 static inline BOOL
is_hexdigit(WCHAR val
) {
287 return ((val
>= 'a' && val
<= 'f') ||
288 (val
>= 'A' && val
<= 'F') ||
289 (val
>= '0' && val
<= '9'));
292 static inline BOOL
is_path_delim(WCHAR val
) {
293 return (!val
|| val
== '#' || val
== '?');
296 /* Checks if the two Uri's are logically equivalent. It's a simple
297 * comparison, since they are both of type Uri, and it can access
298 * the properties of each Uri directly without the need to go
299 * through the "IUri_Get*" interface calls.
301 static BOOL
are_equal_simple(const Uri
*a
, const Uri
*b
) {
302 if(a
->scheme_type
== b
->scheme_type
) {
303 const BOOL known_scheme
= a
->scheme_type
!= URL_SCHEME_UNKNOWN
;
304 const BOOL are_hierarchical
=
305 (a
->authority_start
> -1 && b
->authority_start
> -1);
307 if(a
->scheme_type
== URL_SCHEME_FILE
) {
308 if(a
->canon_len
== b
->canon_len
)
309 return !StrCmpIW(a
->canon_uri
, b
->canon_uri
);
312 /* Only compare the scheme names (if any) if their unknown scheme types. */
314 if((a
->scheme_start
> -1 && b
->scheme_start
> -1) &&
315 (a
->scheme_len
== b
->scheme_len
)) {
316 /* Make sure the schemes are the same. */
317 if(StrCmpNW(a
->canon_uri
+a
->scheme_start
, b
->canon_uri
+b
->scheme_start
, a
->scheme_len
))
319 } else if(a
->scheme_len
!= b
->scheme_len
)
320 /* One of the Uri's has a scheme name, while the other doesn't. */
324 /* If they have a userinfo component, perform case sensitive compare. */
325 if((a
->userinfo_start
> -1 && b
->userinfo_start
> -1) &&
326 (a
->userinfo_len
== b
->userinfo_len
)) {
327 if(StrCmpNW(a
->canon_uri
+a
->userinfo_start
, b
->canon_uri
+b
->userinfo_start
, a
->userinfo_len
))
329 } else if(a
->userinfo_len
!= b
->userinfo_len
)
330 /* One of the Uri's had a userinfo, while the other one doesn't. */
333 /* Check if they have a host name. */
334 if((a
->host_start
> -1 && b
->host_start
> -1) &&
335 (a
->host_len
== b
->host_len
)) {
336 /* Perform a case insensitive compare if they are a known scheme type. */
338 if(StrCmpNIW(a
->canon_uri
+a
->host_start
, b
->canon_uri
+b
->host_start
, a
->host_len
))
340 } else if(StrCmpNW(a
->canon_uri
+a
->host_start
, b
->canon_uri
+b
->host_start
, a
->host_len
))
342 } else if(a
->host_len
!= b
->host_len
)
343 /* One of the Uri's had a host, while the other one didn't. */
346 if(a
->has_port
&& b
->has_port
) {
347 if(a
->port
!= b
->port
)
349 } else if(a
->has_port
|| b
->has_port
)
350 /* One had a port, while the other one didn't. */
353 /* Windows is weird with how it handles paths. For example
354 * One URI could be "http://google.com" (after canonicalization)
355 * and one could be "http://google.com/" and the IsEqual function
356 * would still evaluate to TRUE, but, only if they are both hierarchical
359 if((a
->path_start
> -1 && b
->path_start
> -1) &&
360 (a
->path_len
== b
->path_len
)) {
361 if(StrCmpNW(a
->canon_uri
+a
->path_start
, b
->canon_uri
+b
->path_start
, a
->path_len
))
363 } else if(are_hierarchical
&& a
->path_len
== -1 && b
->path_len
== 0) {
364 if(*(a
->canon_uri
+a
->path_start
) != '/')
366 } else if(are_hierarchical
&& b
->path_len
== 1 && a
->path_len
== 0) {
367 if(*(b
->canon_uri
+b
->path_start
) != '/')
369 } else if(a
->path_len
!= b
->path_len
)
372 /* Compare the query strings of the two URIs. */
373 if((a
->query_start
> -1 && b
->query_start
> -1) &&
374 (a
->query_len
== b
->query_len
)) {
375 if(StrCmpNW(a
->canon_uri
+a
->query_start
, b
->canon_uri
+b
->query_start
, a
->query_len
))
377 } else if(a
->query_len
!= b
->query_len
)
380 if((a
->fragment_start
> -1 && b
->fragment_start
> -1) &&
381 (a
->fragment_len
== b
->fragment_len
)) {
382 if(StrCmpNW(a
->canon_uri
+a
->fragment_start
, b
->canon_uri
+b
->fragment_start
, a
->fragment_len
))
384 } else if(a
->fragment_len
!= b
->fragment_len
)
387 /* If we get here, the two URIs are equivalent. */
394 /* Computes the size of the given IPv6 address.
395 * Each h16 component is 16bits, if there is an IPv4 address, it's
396 * 32bits. If there's an elision it can be 16bits to 128bits, depending
397 * on the number of other components.
399 * Modeled after google-url's CheckIPv6ComponentsSize function
401 static void compute_ipv6_comps_size(ipv6_address
*address
) {
402 address
->components_size
= address
->h16_count
* 2;
405 /* IPv4 address is 4 bytes. */
406 address
->components_size
+= 4;
408 if(address
->elision
) {
409 /* An elision can be anywhere from 2 bytes up to 16 bytes.
410 * It size depends on the size of the h16 and IPv4 components.
412 address
->elision_size
= 16 - address
->components_size
;
413 if(address
->elision_size
< 2)
414 address
->elision_size
= 2;
416 address
->elision_size
= 0;
419 /* Taken from dlls/jscript/lex.c */
420 static int hex_to_int(WCHAR val
) {
421 if(val
>= '0' && val
<= '9')
423 else if(val
>= 'a' && val
<= 'f')
424 return val
- 'a' + 10;
425 else if(val
>= 'A' && val
<= 'F')
426 return val
- 'A' + 10;
431 /* Helper function for converting a percent encoded string
432 * representation of a WCHAR value into its actual WCHAR value. If
433 * the two characters following the '%' aren't valid hex values then
434 * this function returns the NULL character.
437 * "%2E" will result in '.' being returned by this function.
439 static WCHAR
decode_pct_val(const WCHAR
*ptr
) {
442 if(*ptr
== '%' && is_hexdigit(*(ptr
+ 1)) && is_hexdigit(*(ptr
+ 2))) {
443 INT a
= hex_to_int(*(ptr
+ 1));
444 INT b
= hex_to_int(*(ptr
+ 2));
453 /* Helper function for percent encoding a given character
454 * and storing the encoded value into a given buffer (dest).
456 * It's up to the calling function to ensure that there is
457 * at least enough space in 'dest' for the percent encoded
458 * value to be stored (so dest + 3 spaces available).
460 static inline void pct_encode_val(WCHAR val
, WCHAR
*dest
) {
462 dest
[1] = hexDigits
[(val
>> 4) & 0xf];
463 dest
[2] = hexDigits
[val
& 0xf];
466 /* Scans the range of characters [str, end] and returns the last occurence
467 * of 'ch' or returns NULL.
469 static const WCHAR
*str_last_of(const WCHAR
*str
, const WCHAR
*end
, WCHAR ch
) {
470 const WCHAR
*ptr
= end
;
481 /* Attempts to parse the domain name from the host.
483 * This function also includes the Top-level Domain (TLD) name
484 * of the host when it tries to find the domain name. If it finds
485 * a valid domain name it will assign 'domain_start' the offset
486 * into 'host' where the domain name starts.
488 * It's implied that if a domain name its range is implied to be
489 * [host+domain_start, host+host_len).
491 static void find_domain_name(const WCHAR
*host
, DWORD host_len
,
493 const WCHAR
*last_tld
, *sec_last_tld
, *end
;
495 end
= host
+host_len
-1;
499 /* There has to be at least enough room for a '.' followed by a
500 * 3 character TLD for a domain to even exist in the host name.
505 last_tld
= str_last_of(host
, end
, '.');
507 /* http://hostname -> has no domain name. */
510 sec_last_tld
= str_last_of(host
, last_tld
-1, '.');
512 /* If the '.' is at the beginning of the host there
513 * has to be at least 3 characters in the TLD for it
515 * Ex: .com -> .com as the domain name.
516 * .co -> has no domain name.
518 if(last_tld
-host
== 0) {
519 if(end
-(last_tld
-1) < 3)
521 } else if(last_tld
-host
== 3) {
524 /* If there's three characters in front of last_tld and
525 * they are on the list of recognized TLDs, then this
526 * host doesn't have a domain (since the host only contains
528 * Ex: edu.uk -> has no domain name.
529 * foo.uk -> foo.uk as the domain name.
531 for(i
= 0; i
< sizeof(recognized_tlds
)/sizeof(recognized_tlds
[0]); ++i
) {
532 if(!StrCmpNIW(host
, recognized_tlds
[i
].tld_name
, 3))
535 } else if(last_tld
-host
< 3)
536 /* Anything less then 3 characters is considered part
538 * Ex: ak.uk -> Has no domain name.
542 /* Otherwise the domain name is the whole host name. */
544 } else if(end
+1-last_tld
> 3) {
545 /* If the last_tld has more then 3 characters then it's automatically
546 * considered the TLD of the domain name.
547 * Ex: www.winehq.org.uk.test -> uk.test as the domain name.
549 *domain_start
= (sec_last_tld
+1)-host
;
550 } else if(last_tld
- (sec_last_tld
+1) < 4) {
552 /* If the sec_last_tld is 3 characters long it HAS to be on the list of
553 * recognized to still be considered part of the TLD name, otherwise
554 * its considered the domain name.
555 * Ex: www.google.com.uk -> google.com.uk as the domain name.
556 * www.google.foo.uk -> foo.uk as the domain name.
558 if(last_tld
- (sec_last_tld
+1) == 3) {
559 for(i
= 0; i
< sizeof(recognized_tlds
)/sizeof(recognized_tlds
[0]); ++i
) {
560 if(!StrCmpNIW(sec_last_tld
+1, recognized_tlds
[i
].tld_name
, 3)) {
561 const WCHAR
*domain
= str_last_of(host
, sec_last_tld
-1, '.');
566 *domain_start
= (domain
+1) - host
;
567 TRACE("Found domain name %s\n", debugstr_wn(host
+*domain_start
,
568 (host
+host_len
)-(host
+*domain_start
)));
573 *domain_start
= (sec_last_tld
+1)-host
;
575 /* Since the sec_last_tld is less then 3 characters it's considered
577 * Ex: www.google.fo.uk -> google.fo.uk as the domain name.
579 const WCHAR
*domain
= str_last_of(host
, sec_last_tld
-1, '.');
584 *domain_start
= (domain
+1) - host
;
587 /* The second to last TLD has more then 3 characters making it
589 * Ex: www.google.test.us -> test.us as the domain name.
591 *domain_start
= (sec_last_tld
+1)-host
;
594 TRACE("Found domain name %s\n", debugstr_wn(host
+*domain_start
,
595 (host
+host_len
)-(host
+*domain_start
)));
598 /* Removes the dot segments from a heirarchical URIs path component. This
599 * function performs the removal in place.
601 * This is a modified version of Qt's QUrl function "removeDotsFromPath".
603 * This function returns the new length of the path string.
605 static DWORD
remove_dot_segments(WCHAR
*path
, DWORD path_len
) {
607 const WCHAR
*in
= out
;
608 const WCHAR
*end
= out
+ path_len
;
612 /* A. if the input buffer begins with a prefix of "/./" or "/.",
613 * where "." is a complete path segment, then replace that
614 * prefix with "/" in the input buffer; otherwise,
616 if(in
<= end
- 3 && in
[0] == '/' && in
[1] == '.' && in
[2] == '/') {
619 } else if(in
== end
- 2 && in
[0] == '/' && in
[1] == '.') {
625 /* B. if the input buffer begins with a prefix of "/../" or "/..",
626 * where ".." is a complete path segment, then replace that
627 * prefix with "/" in the input buffer and remove the last
628 * segment and its preceding "/" (if any) from the output
631 if(in
<= end
- 4 && in
[0] == '/' && in
[1] == '.' && in
[2] == '.' && in
[3] == '/') {
632 while(out
> path
&& *(--out
) != '/');
636 } else if(in
== end
- 3 && in
[0] == '/' && in
[1] == '.' && in
[2] == '.') {
637 while(out
> path
&& *(--out
) != '/');
646 /* C. move the first path segment in the input buffer to the end of
647 * the output buffer, including the initial "/" character (if
648 * any) and any subsequent characters up to, but not including,
649 * the next "/" character or the end of the input buffer.
652 while(in
< end
&& *in
!= '/')
657 TRACE("(%p %d): Path after dot segments removed %s len=%d\n", path
, path_len
,
658 debugstr_wn(path
, len
), len
);
662 /* Attempts to find the file extension in a given path. */
663 static INT
find_file_extension(const WCHAR
*path
, DWORD path_len
) {
666 for(end
= path
+path_len
-1; end
>= path
&& *end
!= '/' && *end
!= '\\'; --end
) {
674 /* Computes the location where the elision should occur in the IPv6
675 * address using the numerical values of each component stored in
676 * 'values'. If the address shouldn't contain an elision then 'index'
677 * is assigned -1 as it's value. Otherwise 'index' will contain the
678 * starting index (into values) where the elision should be, and 'count'
679 * will contain the number of cells the elision covers.
682 * Windows will expand an elision if the elision only represents 1 h16
683 * component of the URI.
685 * Ex: [1::2:3:4:5:6:7] -> [1:0:2:3:4:5:6:7]
687 * If the IPv6 address contains an IPv4 address, the IPv4 address is also
688 * considered for being included as part of an elision if all it's components
691 * Ex: [1:2:3:4:5:6:0.0.0.0] -> [1:2:3:4:5:6::]
693 static void compute_elision_location(const ipv6_address
*address
, const USHORT values
[8],
694 INT
*index
, DWORD
*count
) {
695 DWORD i
, max_len
, cur_len
;
696 INT max_index
, cur_index
;
698 max_len
= cur_len
= 0;
699 max_index
= cur_index
= -1;
700 for(i
= 0; i
< 8; ++i
) {
701 BOOL check_ipv4
= (address
->ipv4
&& i
== 6);
702 BOOL is_end
= (check_ipv4
|| i
== 7);
705 /* Check if the IPv4 address contains only zeros. */
706 if(values
[i
] == 0 && values
[i
+1] == 0) {
713 } else if(values
[i
] == 0) {
720 if(is_end
|| values
[i
] != 0) {
721 /* We only consider it for an elision if it's
722 * more then 1 component long.
724 if(cur_len
> 1 && cur_len
> max_len
) {
725 /* Found the new elision location. */
727 max_index
= cur_index
;
730 /* Reset the current range for the next range of zeros. */
740 /* Removes all the leading and trailing white spaces or
741 * control characters from the URI and removes all control
742 * characters inside of the URI string.
744 static BSTR
pre_process_uri(LPCWSTR uri
) {
747 const WCHAR
*start
, *end
;
753 /* Skip leading controls and whitespace. */
754 while(iscntrlW(*start
) || isspaceW(*start
)) ++start
;
758 /* URI consisted only of control/whitespace. */
759 ret
= SysAllocStringLen(NULL
, 0);
761 while(iscntrlW(*end
) || isspaceW(*end
)) --end
;
763 buf
= heap_alloc(((end
+1)-start
)*sizeof(WCHAR
));
767 for(ptr
= buf
; start
< end
+1; ++start
) {
768 if(!iscntrlW(*start
))
772 ret
= SysAllocStringLen(buf
, ptr
-buf
);
779 /* Converts the specified IPv4 address into an uint value.
781 * This function assumes that the IPv4 address has already been validated.
783 static UINT
ipv4toui(const WCHAR
*ip
, DWORD len
) {
785 DWORD comp_value
= 0;
788 for(ptr
= ip
; ptr
< ip
+len
; ++ptr
) {
794 comp_value
= comp_value
*10 + (*ptr
-'0');
803 /* Converts an IPv4 address in numerical form into it's fully qualified
804 * string form. This function returns the number of characters written
805 * to 'dest'. If 'dest' is NULL this function will return the number of
806 * characters that would have been written.
808 * It's up to the caller to ensure there's enough space in 'dest' for the
811 static DWORD
ui2ipv4(WCHAR
*dest
, UINT address
) {
812 static const WCHAR formatW
[] =
813 {'%','u','.','%','u','.','%','u','.','%','u',0};
817 digits
[0] = (address
>> 24) & 0xff;
818 digits
[1] = (address
>> 16) & 0xff;
819 digits
[2] = (address
>> 8) & 0xff;
820 digits
[3] = address
& 0xff;
824 ret
= sprintfW(tmp
, formatW
, digits
[0], digits
[1], digits
[2], digits
[3]);
826 ret
= sprintfW(dest
, formatW
, digits
[0], digits
[1], digits
[2], digits
[3]);
831 /* Converts an h16 component (from an IPv6 address) into it's
834 * This function assumes that the h16 component has already been validated.
836 static USHORT
h16tous(h16 component
) {
840 for(i
= 0; i
< component
.len
; ++i
) {
842 ret
+= hex_to_int(component
.str
[i
]);
848 /* Converts an IPv6 address into it's 128 bits (16 bytes) numerical value.
850 * This function assumes that the ipv6_address has already been validated.
852 static BOOL
ipv6_to_number(const ipv6_address
*address
, USHORT number
[8]) {
853 DWORD i
, cur_component
= 0;
854 BOOL already_passed_elision
= FALSE
;
856 for(i
= 0; i
< address
->h16_count
; ++i
) {
857 if(address
->elision
) {
858 if(address
->components
[i
].str
> address
->elision
&& !already_passed_elision
) {
859 /* Means we just passed the elision and need to add it's values to
860 * 'number' before we do anything else.
863 for(j
= 0; j
< address
->elision_size
; j
+=2)
864 number
[cur_component
++] = 0;
866 already_passed_elision
= TRUE
;
870 number
[cur_component
++] = h16tous(address
->components
[i
]);
873 /* Case when the elision appears after the h16 components. */
874 if(!already_passed_elision
&& address
->elision
) {
875 for(i
= 0; i
< address
->elision_size
; i
+=2)
876 number
[cur_component
++] = 0;
877 already_passed_elision
= TRUE
;
881 UINT value
= ipv4toui(address
->ipv4
, address
->ipv4_len
);
883 if(cur_component
!= 6) {
884 ERR("(%p %p): Failed sanity check with %d\n", address
, number
, cur_component
);
888 number
[cur_component
++] = (value
>> 16) & 0xffff;
889 number
[cur_component
] = value
& 0xffff;
895 /* Checks if the characters pointed to by 'ptr' are
896 * a percent encoded data octet.
898 * pct-encoded = "%" HEXDIG HEXDIG
900 static BOOL
check_pct_encoded(const WCHAR
**ptr
) {
901 const WCHAR
*start
= *ptr
;
907 if(!is_hexdigit(**ptr
)) {
913 if(!is_hexdigit(**ptr
)) {
922 /* dec-octet = DIGIT ; 0-9
923 * / %x31-39 DIGIT ; 10-99
924 * / "1" 2DIGIT ; 100-199
925 * / "2" %x30-34 DIGIT ; 200-249
926 * / "25" %x30-35 ; 250-255
928 static BOOL
check_dec_octet(const WCHAR
**ptr
) {
929 const WCHAR
*c1
, *c2
, *c3
;
932 /* A dec-octet must be at least 1 digit long. */
933 if(*c1
< '0' || *c1
> '9')
939 /* Since the 1 digit requirment was meet, it doesn't
940 * matter if this is a DIGIT value, it's considered a
943 if(*c2
< '0' || *c2
> '9')
949 /* Same explanation as above. */
950 if(*c3
< '0' || *c3
> '9')
953 /* Anything > 255 isn't a valid IP dec-octet. */
954 if(*c1
>= '2' && *c2
>= '5' && *c3
>= '5') {
963 /* Checks if there is an implicit IPv4 address in the host component of the URI.
964 * The max value of an implicit IPv4 address is UINT_MAX.
967 * "234567" would be considered an implicit IPv4 address.
969 static BOOL
check_implicit_ipv4(const WCHAR
**ptr
, UINT
*val
) {
970 const WCHAR
*start
= *ptr
;
974 while(is_num(**ptr
)) {
975 ret
= ret
*10 + (**ptr
- '0');
991 /* Checks if the string contains an IPv4 address.
993 * This function has a strict mode or a non-strict mode of operation
994 * When 'strict' is set to FALSE this function will return TRUE if
995 * the string contains at least 'dec-octet "." dec-octet' since partial
996 * IPv4 addresses will be normalized out into full IPv4 addresses. When
997 * 'strict' is set this function expects there to be a full IPv4 address.
999 * IPv4address = dec-octet "." dec-octet "." dec-octet "." dec-octet
1001 static BOOL
check_ipv4address(const WCHAR
**ptr
, BOOL strict
) {
1002 const WCHAR
*start
= *ptr
;
1004 if(!check_dec_octet(ptr
)) {
1015 if(!check_dec_octet(ptr
)) {
1029 if(!check_dec_octet(ptr
)) {
1043 if(!check_dec_octet(ptr
)) {
1048 /* Found a four digit ip address. */
1051 /* Tries to parse the scheme name of the URI.
1053 * scheme = ALPHA *(ALPHA | NUM | '+' | '-' | '.') as defined by RFC 3896.
1054 * NOTE: Windows accepts a number as the first character of a scheme.
1056 static BOOL
parse_scheme_name(const WCHAR
**ptr
, parse_data
*data
) {
1057 const WCHAR
*start
= *ptr
;
1059 data
->scheme
= NULL
;
1060 data
->scheme_len
= 0;
1063 if(**ptr
== '*' && *ptr
== start
) {
1064 /* Might have found a wildcard scheme. If it is the next
1065 * char has to be a ':' for it to be a valid URI
1069 } else if(!is_num(**ptr
) && !is_alpha(**ptr
) && **ptr
!= '+' &&
1070 **ptr
!= '-' && **ptr
!= '.')
1079 /* Schemes must end with a ':' */
1085 data
->scheme
= start
;
1086 data
->scheme_len
= *ptr
- start
;
1092 /* Tries to deduce the corresponding URL_SCHEME for the given URI. Stores
1093 * the deduced URL_SCHEME in data->scheme_type.
1095 static BOOL
parse_scheme_type(parse_data
*data
) {
1096 /* If there's scheme data then see if it's a recognized scheme. */
1097 if(data
->scheme
&& data
->scheme_len
) {
1100 for(i
= 0; i
< sizeof(recognized_schemes
)/sizeof(recognized_schemes
[0]); ++i
) {
1101 if(lstrlenW(recognized_schemes
[i
].scheme_name
) == data
->scheme_len
) {
1102 /* Has to be a case insensitive compare. */
1103 if(!StrCmpNIW(recognized_schemes
[i
].scheme_name
, data
->scheme
, data
->scheme_len
)) {
1104 data
->scheme_type
= recognized_schemes
[i
].scheme
;
1110 /* If we get here it means it's not a recognized scheme. */
1111 data
->scheme_type
= URL_SCHEME_UNKNOWN
;
1113 } else if(data
->is_relative
) {
1114 /* Relative URI's have no scheme. */
1115 data
->scheme_type
= URL_SCHEME_UNKNOWN
;
1118 /* Should never reach here! what happened... */
1119 FIXME("(%p): Unable to determine scheme type for URI %s\n", data
, debugstr_w(data
->uri
));
1124 /* Tries to parse (or deduce) the scheme_name of a URI. If it can't
1125 * parse a scheme from the URI it will try to deduce the scheme_name and scheme_type
1126 * using the flags specified in 'flags' (if any). Flags that affect how this function
1127 * operates are the Uri_CREATE_ALLOW_* flags.
1129 * All parsed/deduced information will be stored in 'data' when the function returns.
1131 * Returns TRUE if it was able to successfully parse the information.
1133 static BOOL
parse_scheme(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
) {
1134 static const WCHAR fileW
[] = {'f','i','l','e',0};
1135 static const WCHAR wildcardW
[] = {'*',0};
1137 /* First check to see if the uri could implicitly be a file path. */
1138 if(is_implicit_file_path(*ptr
)) {
1139 if(flags
& Uri_CREATE_ALLOW_IMPLICIT_FILE_SCHEME
) {
1140 data
->scheme
= fileW
;
1141 data
->scheme_len
= lstrlenW(fileW
);
1142 data
->has_implicit_scheme
= TRUE
;
1144 TRACE("(%p %p %x): URI is an implicit file path.\n", ptr
, data
, flags
);
1146 /* Window's does not consider anything that can implicitly be a file
1147 * path to be a valid URI if the ALLOW_IMPLICIT_FILE_SCHEME flag is not set...
1149 TRACE("(%p %p %x): URI is implicitly a file path, but, the ALLOW_IMPLICIT_FILE_SCHEME flag wasn't set.\n",
1153 } else if(!parse_scheme_name(ptr
, data
)) {
1154 /* No Scheme was found, this means it could be:
1155 * a) an implicit Wildcard scheme
1159 if(flags
& Uri_CREATE_ALLOW_IMPLICIT_WILDCARD_SCHEME
) {
1160 data
->scheme
= wildcardW
;
1161 data
->scheme_len
= lstrlenW(wildcardW
);
1162 data
->has_implicit_scheme
= TRUE
;
1164 TRACE("(%p %p %x): URI is an implicit wildcard scheme.\n", ptr
, data
, flags
);
1165 } else if (flags
& Uri_CREATE_ALLOW_RELATIVE
) {
1166 data
->is_relative
= TRUE
;
1167 TRACE("(%p %p %x): URI is relative.\n", ptr
, data
, flags
);
1169 TRACE("(%p %p %x): Malformed URI found. Unable to deduce scheme name.\n", ptr
, data
, flags
);
1174 if(!data
->is_relative
)
1175 TRACE("(%p %p %x): Found scheme=%s scheme_len=%d\n", ptr
, data
, flags
,
1176 debugstr_wn(data
->scheme
, data
->scheme_len
), data
->scheme_len
);
1178 if(!parse_scheme_type(data
))
1181 TRACE("(%p %p %x): Assigned %d as the URL_SCHEME.\n", ptr
, data
, flags
, data
->scheme_type
);
1185 /* Parses the userinfo part of the URI (if it exists). The userinfo field of
1186 * a URI can consist of "username:password@", or just "username@".
1189 * userinfo = *( unreserved / pct-encoded / sub-delims / ":" )
1192 * 1) If there is more than one ':' in the userinfo part of the URI Windows
1193 * uses the first occurence of ':' to delimit the username and password
1197 * ftp://user:pass:word@winehq.org
1199 * Would yield, "user" as the username and "pass:word" as the password.
1201 * 2) Windows allows any character to appear in the "userinfo" part of
1202 * a URI, as long as it's not an authority delimeter character set.
1204 static void parse_userinfo(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
) {
1205 data
->userinfo
= *ptr
;
1206 data
->userinfo_split
= -1;
1208 while(**ptr
!= '@') {
1209 if(**ptr
== ':' && data
->userinfo_split
== -1)
1210 data
->userinfo_split
= *ptr
- data
->userinfo
;
1211 else if(**ptr
== '%') {
1212 /* If it's a known scheme type, it has to be a valid percent
1215 if(!check_pct_encoded(ptr
)) {
1216 if(data
->scheme_type
!= URL_SCHEME_UNKNOWN
) {
1217 *ptr
= data
->userinfo
;
1218 data
->userinfo
= NULL
;
1219 data
->userinfo_split
= -1;
1221 TRACE("(%p %p %x): URI contained no userinfo.\n", ptr
, data
, flags
);
1226 } else if(is_auth_delim(**ptr
, data
->scheme_type
!= URL_SCHEME_UNKNOWN
))
1233 *ptr
= data
->userinfo
;
1234 data
->userinfo
= NULL
;
1235 data
->userinfo_split
= -1;
1237 TRACE("(%p %p %x): URI contained no userinfo.\n", ptr
, data
, flags
);
1241 data
->userinfo_len
= *ptr
- data
->userinfo
;
1242 TRACE("(%p %p %x): Found userinfo=%s userinfo_len=%d split=%d.\n", ptr
, data
, flags
,
1243 debugstr_wn(data
->userinfo
, data
->userinfo_len
), data
->userinfo_len
, data
->userinfo_split
);
1247 /* Attempts to parse a port from the URI.
1250 * Windows seems to have a cap on what the maximum value
1251 * for a port can be. The max value is USHORT_MAX.
1255 static BOOL
parse_port(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
) {
1259 while(!is_auth_delim(**ptr
, data
->scheme_type
!= URL_SCHEME_UNKNOWN
)) {
1260 if(!is_num(**ptr
)) {
1266 port
= port
*10 + (**ptr
-'0');
1268 if(port
> USHORT_MAX
) {
1277 data
->port_value
= port
;
1278 data
->port_len
= *ptr
- data
->port
;
1280 TRACE("(%p %p %x): Found port %s len=%d value=%u\n", ptr
, data
, flags
,
1281 debugstr_wn(data
->port
, data
->port_len
), data
->port_len
, data
->port_value
);
1285 /* Attempts to parse a IPv4 address from the URI.
1288 * Window's normalizes IPv4 addresses, This means there's three
1289 * possibilities for the URI to contain an IPv4 address.
1290 * 1) A well formed address (ex. 192.2.2.2).
1291 * 2) A partially formed address. For example "192.0" would
1292 * normalize to "192.0.0.0" during canonicalization.
1293 * 3) An implicit IPv4 address. For example "256" would
1294 * normalize to "0.0.1.0" during canonicalization. Also
1295 * note that the maximum value for an implicit IP address
1296 * is UINT_MAX, if the value in the URI exceeds this then
1297 * it is not considered an IPv4 address.
1299 static BOOL
parse_ipv4address(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
) {
1300 const BOOL is_unknown
= data
->scheme_type
== URL_SCHEME_UNKNOWN
;
1303 if(!check_ipv4address(ptr
, FALSE
)) {
1304 if(!check_implicit_ipv4(ptr
, &data
->implicit_ipv4
)) {
1305 TRACE("(%p %p %x): URI didn't contain anything looking like an IPv4 address.\n",
1311 data
->has_implicit_ip
= TRUE
;
1314 /* Check if what we found is the only part of the host name (if it isn't
1315 * we don't have an IPv4 address).
1319 if(!parse_port(ptr
, data
, flags
)) {
1324 } else if(!is_auth_delim(**ptr
, !is_unknown
)) {
1325 /* Found more data which belongs the host, so this isn't an IPv4. */
1328 data
->has_implicit_ip
= FALSE
;
1332 data
->host_len
= *ptr
- data
->host
;
1333 data
->host_type
= Uri_HOST_IPV4
;
1335 TRACE("(%p %p %x): IPv4 address found. host=%s host_len=%d host_type=%d\n",
1336 ptr
, data
, flags
, debugstr_wn(data
->host
, data
->host_len
),
1337 data
->host_len
, data
->host_type
);
1341 /* Attempts to parse the reg-name from the URI.
1343 * Because of the way Windows handles ':' this function also
1344 * handles parsing the port.
1346 * reg-name = *( unreserved / pct-encoded / sub-delims )
1349 * Windows allows everything, but, the characters in "auth_delims" and ':'
1350 * to appear in a reg-name, unless it's an unknown scheme type then ':' is
1351 * allowed to appear (even if a valid port isn't after it).
1353 * Windows doesn't like host names which start with '[' and end with ']'
1354 * and don't contain a valid IP literal address in between them.
1356 * On Windows if an '[' is encountered in the host name the ':' no longer
1357 * counts as a delimiter until you reach the next ']' or an "authority delimeter".
1359 * A reg-name CAN be empty.
1361 static BOOL
parse_reg_name(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
) {
1362 const BOOL has_start_bracket
= **ptr
== '[';
1363 const BOOL known_scheme
= data
->scheme_type
!= URL_SCHEME_UNKNOWN
;
1364 BOOL inside_brackets
= has_start_bracket
;
1365 BOOL ignore_col
= FALSE
;
1367 /* We have to be careful with file schemes. */
1368 if(data
->scheme_type
== URL_SCHEME_FILE
) {
1369 /* This is because an implicit file scheme could be "C:\\test" and it
1370 * would trick this function into thinking the host is "C", when after
1371 * canonicalization the host would end up being an empty string.
1373 if(is_alpha(**ptr
) && *(*ptr
+1) == ':') {
1374 /* Regular old drive paths don't have a host type (or host name). */
1375 data
->host_type
= Uri_HOST_UNKNOWN
;
1379 } else if(**ptr
== '\\' && *(*ptr
+1) == '\\')
1380 /* Skip past the "\\" of a UNC path. */
1386 while(!is_auth_delim(**ptr
, known_scheme
)) {
1387 if(**ptr
== ':' && !ignore_col
) {
1388 /* We can ignore ':' if were inside brackets.*/
1389 if(!inside_brackets
) {
1390 const WCHAR
*tmp
= (*ptr
)++;
1392 /* Attempt to parse the port. */
1393 if(!parse_port(ptr
, data
, flags
)) {
1394 /* Windows expects there to be a valid port for known scheme types. */
1395 if(data
->scheme_type
!= URL_SCHEME_UNKNOWN
) {
1398 TRACE("(%p %p %x): Expected valid port\n", ptr
, data
, flags
);
1401 /* Windows gives up on trying to parse a port when it
1402 * encounters 1 invalid port.
1406 data
->host_len
= tmp
- data
->host
;
1410 } else if(**ptr
== '%' && known_scheme
) {
1411 /* Has to be a legit % encoded value. */
1412 if(!check_pct_encoded(ptr
)) {
1418 } else if(**ptr
== ']')
1419 inside_brackets
= FALSE
;
1420 else if(**ptr
== '[')
1421 inside_brackets
= TRUE
;
1426 if(has_start_bracket
) {
1427 /* Make sure the last character of the host wasn't a ']'. */
1428 if(*(*ptr
-1) == ']') {
1429 TRACE("(%p %p %x): Expected an IP literal inside of the host\n",
1437 /* Don't overwrite our length if we found a port earlier. */
1439 data
->host_len
= *ptr
- data
->host
;
1441 /* If the host is empty, then it's an unknown host type. */
1442 if(data
->host_len
== 0)
1443 data
->host_type
= Uri_HOST_UNKNOWN
;
1445 data
->host_type
= Uri_HOST_DNS
;
1447 TRACE("(%p %p %x): Parsed reg-name. host=%s len=%d\n", ptr
, data
, flags
,
1448 debugstr_wn(data
->host
, data
->host_len
), data
->host_len
);
1452 /* Attempts to parse an IPv6 address out of the URI.
1454 * IPv6address = 6( h16 ":" ) ls32
1455 * / "::" 5( h16 ":" ) ls32
1456 * / [ h16 ] "::" 4( h16 ":" ) ls32
1457 * / [ *1( h16 ":" ) h16 ] "::" 3( h16 ":" ) ls32
1458 * / [ *2( h16 ":" ) h16 ] "::" 2( h16 ":" ) ls32
1459 * / [ *3( h16 ":" ) h16 ] "::" h16 ":" ls32
1460 * / [ *4( h16 ":" ) h16 ] "::" ls32
1461 * / [ *5( h16 ":" ) h16 ] "::" h16
1462 * / [ *6( h16 ":" ) h16 ] "::"
1464 * ls32 = ( h16 ":" h16 ) / IPv4address
1465 * ; least-significant 32 bits of address.
1468 * ; 16 bits of address represented in hexadecimal.
1470 * Modeled after google-url's 'DoParseIPv6' function.
1472 static BOOL
parse_ipv6address(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
) {
1473 const WCHAR
*start
, *cur_start
;
1476 start
= cur_start
= *ptr
;
1477 memset(&ip
, 0, sizeof(ipv6_address
));
1480 /* Check if we're on the last character of the host. */
1481 BOOL is_end
= (is_auth_delim(**ptr
, data
->scheme_type
!= URL_SCHEME_UNKNOWN
)
1484 BOOL is_split
= (**ptr
== ':');
1485 BOOL is_elision
= (is_split
&& !is_end
&& *(*ptr
+1) == ':');
1487 /* Check if we're at the end of of the a component, or
1488 * if we're at the end of the IPv6 address.
1490 if(is_split
|| is_end
) {
1493 cur_len
= *ptr
- cur_start
;
1495 /* h16 can't have a length > 4. */
1499 TRACE("(%p %p %x): h16 component to long.\n",
1505 /* An h16 component can't have the length of 0 unless
1506 * the elision is at the beginning of the address, or
1507 * at the end of the address.
1509 if(!((*ptr
== start
&& is_elision
) ||
1510 (is_end
&& (*ptr
-2) == ip
.elision
))) {
1512 TRACE("(%p %p %x): IPv6 component can not have a length of 0.\n",
1519 /* An IPv6 address can have no more than 8 h16 components. */
1520 if(ip
.h16_count
>= 8) {
1522 TRACE("(%p %p %x): Not a IPv6 address, to many h16 components.\n",
1527 ip
.components
[ip
.h16_count
].str
= cur_start
;
1528 ip
.components
[ip
.h16_count
].len
= cur_len
;
1530 TRACE("(%p %p %x): Found h16 component %s, len=%d, h16_count=%d\n",
1531 ptr
, data
, flags
, debugstr_wn(cur_start
, cur_len
), cur_len
,
1541 /* A IPv6 address can only have 1 elision ('::'). */
1545 TRACE("(%p %p %x): IPv6 address cannot have 2 elisions.\n",
1557 if(!check_ipv4address(ptr
, TRUE
)) {
1558 if(!is_hexdigit(**ptr
)) {
1559 /* Not a valid character for an IPv6 address. */
1564 /* Found an IPv4 address. */
1565 ip
.ipv4
= cur_start
;
1566 ip
.ipv4_len
= *ptr
- cur_start
;
1568 TRACE("(%p %p %x): Found an attached IPv4 address %s len=%d.\n",
1569 ptr
, data
, flags
, debugstr_wn(ip
.ipv4
, ip
.ipv4_len
),
1572 /* IPv4 addresses can only appear at the end of a IPv6. */
1578 compute_ipv6_comps_size(&ip
);
1580 /* Make sure the IPv6 address adds up to 16 bytes. */
1581 if(ip
.components_size
+ ip
.elision_size
!= 16) {
1583 TRACE("(%p %p %x): Invalid IPv6 address, did not add up to 16 bytes.\n",
1588 if(ip
.elision_size
== 2) {
1589 /* For some reason on Windows if an elision that represents
1590 * only 1 h16 component is encountered at the very begin or
1591 * end of an IPv6 address, Windows does not consider it a
1592 * valid IPv6 address.
1594 * Ex: [::2:3:4:5:6:7] is not valid, even though the sum
1595 * of all the components == 128bits.
1597 if(ip
.elision
< ip
.components
[0].str
||
1598 ip
.elision
> ip
.components
[ip
.h16_count
-1].str
) {
1600 TRACE("(%p %p %x): Invalid IPv6 address. Detected elision of 2 bytes at the beginning or end of the address.\n",
1606 data
->host_type
= Uri_HOST_IPV6
;
1607 data
->has_ipv6
= TRUE
;
1608 data
->ipv6_address
= ip
;
1610 TRACE("(%p %p %x): Found valid IPv6 literal %s len=%d\n",
1611 ptr
, data
, flags
, debugstr_wn(start
, *ptr
-start
),
1616 /* IPvFuture = "v" 1*HEXDIG "." 1*( unreserved / sub-delims / ":" ) */
1617 static BOOL
parse_ipvfuture(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
) {
1618 const WCHAR
*start
= *ptr
;
1620 /* IPvFuture has to start with a 'v' or 'V'. */
1621 if(**ptr
!= 'v' && **ptr
!= 'V')
1624 /* Following the v their must be atleast 1 hexdigit. */
1626 if(!is_hexdigit(**ptr
)) {
1632 while(is_hexdigit(**ptr
))
1635 /* End of the hexdigit sequence must be a '.' */
1642 if(!is_unreserved(**ptr
) && !is_subdelim(**ptr
) && **ptr
!= ':') {
1648 while(is_unreserved(**ptr
) || is_subdelim(**ptr
) || **ptr
== ':')
1651 data
->host_type
= Uri_HOST_UNKNOWN
;
1653 TRACE("(%p %p %x): Parsed IPvFuture address %s len=%d\n", ptr
, data
, flags
,
1654 debugstr_wn(start
, *ptr
-start
), *ptr
-start
);
1659 /* IP-literal = "[" ( IPv6address / IPvFuture ) "]" */
1660 static BOOL
parse_ip_literal(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
) {
1669 if(!parse_ipv6address(ptr
, data
, flags
)) {
1670 if(!parse_ipvfuture(ptr
, data
, flags
)) {
1686 /* If a valid port is not found, then let it trickle down to
1689 if(!parse_port(ptr
, data
, flags
)) {
1695 data
->host_len
= *ptr
- data
->host
;
1700 /* Parses the host information from the URI.
1702 * host = IP-literal / IPv4address / reg-name
1704 static BOOL
parse_host(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
) {
1705 if(!parse_ip_literal(ptr
, data
, flags
)) {
1706 if(!parse_ipv4address(ptr
, data
, flags
)) {
1707 if(!parse_reg_name(ptr
, data
, flags
)) {
1708 TRACE("(%p %p %x): Malformed URI, Unknown host type.\n",
1718 /* Parses the authority information from the URI.
1720 * authority = [ userinfo "@" ] host [ ":" port ]
1722 static BOOL
parse_authority(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
) {
1723 parse_userinfo(ptr
, data
, flags
);
1725 /* Parsing the port will happen during one of the host parsing
1726 * routines (if the URI has a port).
1728 if(!parse_host(ptr
, data
, flags
))
1734 /* Attempts to parse the path information of a hierarchical URI. */
1735 static BOOL
parse_path_hierarchical(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
) {
1736 const WCHAR
*start
= *ptr
;
1737 static const WCHAR slash
[] = {'/',0};
1739 if(is_path_delim(**ptr
)) {
1740 if(data
->scheme_type
== URL_SCHEME_WILDCARD
) {
1741 /* Wildcard schemes don't get a '/' attached if their path is
1746 } else if(!(flags
& Uri_CREATE_NO_CANONICALIZE
)) {
1747 /* If the path component is empty, then a '/' is added. */
1752 while(!is_path_delim(**ptr
)) {
1753 if(**ptr
== '%' && data
->scheme_type
!= URL_SCHEME_UNKNOWN
&&
1754 data
->scheme_type
!= URL_SCHEME_FILE
) {
1755 if(!check_pct_encoded(ptr
)) {
1760 } else if(**ptr
== '\\') {
1761 /* Not allowed to have a backslash if NO_CANONICALIZE is set
1762 * and the scheme is known type (but not a file scheme).
1764 if(flags
& Uri_CREATE_NO_CANONICALIZE
) {
1765 if(data
->scheme_type
!= URL_SCHEME_FILE
&&
1766 data
->scheme_type
!= URL_SCHEME_UNKNOWN
) {
1776 /* The only time a URI doesn't have a path is when
1777 * the NO_CANONICALIZE flag is set and the raw URI
1778 * didn't contain one.
1785 data
->path_len
= *ptr
- start
;
1790 TRACE("(%p %p %x): Parsed path %s len=%d\n", ptr
, data
, flags
,
1791 debugstr_wn(data
->path
, data
->path_len
), data
->path_len
);
1793 TRACE("(%p %p %x): The URI contained no path\n", ptr
, data
, flags
);
1798 /* Parses the path of a opaque URI (much less strict then the parser
1799 * for a hierarchical URI).
1802 * Windows allows invalid % encoded data to appear in opaque URI paths
1803 * for unknown scheme types.
1805 static BOOL
parse_path_opaque(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
) {
1806 const BOOL known_scheme
= data
->scheme_type
!= URL_SCHEME_UNKNOWN
;
1810 while(!is_path_delim(**ptr
)) {
1811 if(**ptr
== '%' && known_scheme
) {
1812 if(!check_pct_encoded(ptr
)) {
1823 data
->path_len
= *ptr
- data
->path
;
1824 TRACE("(%p %p %x): Parsed opaque URI path %s len=%d\n", ptr
, data
, flags
,
1825 debugstr_wn(data
->path
, data
->path_len
), data
->path_len
);
1829 /* Determines how the URI should be parsed after the scheme information.
1831 * If the scheme is followed, by "//" then, it is treated as an hierarchical URI
1832 * which then the authority and path information will be parsed out. Otherwise, the
1833 * URI will be treated as an opaque URI which the authority information is not parsed
1836 * RFC 3896 definition of hier-part:
1838 * hier-part = "//" authority path-abempty
1843 * MSDN opaque URI definition:
1844 * scheme ":" path [ "#" fragment ]
1847 * If the URI is of an unknown scheme type and has a "//" following the scheme then it
1848 * is treated as a hierarchical URI, but, if the CREATE_NO_CRACK_UNKNOWN_SCHEMES flag is
1849 * set then it is considered an opaque URI reguardless of what follows the scheme information
1850 * (per MSDN documentation).
1852 static BOOL
parse_hierpart(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
) {
1853 const WCHAR
*start
= *ptr
;
1855 /* Checks if the authority information needs to be parsed.
1857 * Relative URI's aren't hierarchical URI's, but, they could trick
1858 * "check_hierarchical" into thinking it is, so we need to explicitly
1859 * make sure it's not relative. Also, if the URI is an implicit file
1860 * scheme it might not contain a "//", but, it's considered hierarchical
1861 * anyways. Wildcard Schemes are always considered hierarchical
1863 if(data
->scheme_type
== URL_SCHEME_WILDCARD
||
1864 data
->scheme_type
== URL_SCHEME_FILE
||
1865 (!data
->is_relative
&& check_hierarchical(ptr
))) {
1866 /* Only treat it as a hierarchical URI if the scheme_type is known or
1867 * the Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES flag is not set.
1869 if(data
->scheme_type
!= URL_SCHEME_UNKNOWN
||
1870 !(flags
& Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES
)) {
1871 TRACE("(%p %p %x): Treating URI as an hierarchical URI.\n", ptr
, data
, flags
);
1872 data
->is_opaque
= FALSE
;
1874 if(data
->scheme_type
== URL_SCHEME_FILE
)
1875 /* Skip past the "//" after the scheme (if any). */
1876 check_hierarchical(ptr
);
1878 /* TODO: Handle hierarchical URI's, parse authority then parse the path. */
1879 if(!parse_authority(ptr
, data
, flags
))
1882 return parse_path_hierarchical(ptr
, data
, flags
);
1884 /* Reset ptr to it's starting position so opaque path parsing
1885 * begins at the correct location.
1890 /* If it reaches here, then the URI will be treated as an opaque
1894 TRACE("(%p %p %x): Treating URI as an opaque URI.\n", ptr
, data
, flags
);
1896 data
->is_opaque
= TRUE
;
1897 if(!parse_path_opaque(ptr
, data
, flags
))
1903 /* Attempts to parse the query string from the URI.
1906 * If NO_DECODE_EXTRA_INFO flag is set, then invalid percent encoded
1907 * data is allowed appear in the query string. For unknown scheme types
1908 * invalid percent encoded data is allowed to appear reguardless.
1910 static BOOL
parse_query(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
) {
1911 const BOOL known_scheme
= data
->scheme_type
!= URL_SCHEME_UNKNOWN
;
1914 TRACE("(%p %p %x): URI didn't contain a query string.\n", ptr
, data
, flags
);
1921 while(**ptr
&& **ptr
!= '#') {
1922 if(**ptr
== '%' && known_scheme
&&
1923 !(flags
& Uri_CREATE_NO_DECODE_EXTRA_INFO
)) {
1924 if(!check_pct_encoded(ptr
)) {
1935 data
->query_len
= *ptr
- data
->query
;
1937 TRACE("(%p %p %x): Parsed query string %s len=%d\n", ptr
, data
, flags
,
1938 debugstr_wn(data
->query
, data
->query_len
), data
->query_len
);
1942 /* Attempts to parse the fragment from the URI.
1945 * If NO_DECODE_EXTRA_INFO flag is set, then invalid percent encoded
1946 * data is allowed appear in the query string. For unknown scheme types
1947 * invalid percent encoded data is allowed to appear reguardless.
1949 static BOOL
parse_fragment(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
) {
1950 const BOOL known_scheme
= data
->scheme_type
!= URL_SCHEME_UNKNOWN
;
1953 TRACE("(%p %p %x): URI didn't contain a fragment.\n", ptr
, data
, flags
);
1957 data
->fragment
= *ptr
;
1961 if(**ptr
== '%' && known_scheme
&&
1962 !(flags
& Uri_CREATE_NO_DECODE_EXTRA_INFO
)) {
1963 if(!check_pct_encoded(ptr
)) {
1964 *ptr
= data
->fragment
;
1965 data
->fragment
= NULL
;
1974 data
->fragment_len
= *ptr
- data
->fragment
;
1976 TRACE("(%p %p %x): Parsed fragment %s len=%d\n", ptr
, data
, flags
,
1977 debugstr_wn(data
->fragment
, data
->fragment_len
), data
->fragment_len
);
1981 /* Parses and validates the components of the specified by data->uri
1982 * and stores the information it parses into 'data'.
1984 * Returns TRUE if it successfully parsed the URI. False otherwise.
1986 static BOOL
parse_uri(parse_data
*data
, DWORD flags
) {
1993 TRACE("(%p %x): BEGINNING TO PARSE URI %s.\n", data
, flags
, debugstr_w(data
->uri
));
1995 if(!parse_scheme(pptr
, data
, flags
))
1998 if(!parse_hierpart(pptr
, data
, flags
))
2001 if(!parse_query(pptr
, data
, flags
))
2004 if(!parse_fragment(pptr
, data
, flags
))
2007 TRACE("(%p %x): FINISHED PARSING URI.\n", data
, flags
);
2011 /* Canonicalizes the userinfo of the URI represented by the parse_data.
2013 * Canonicalization of the userinfo is a simple process. If there are any percent
2014 * encoded characters that fall in the "unreserved" character set, they are decoded
2015 * to their actual value. If a character is not in the "unreserved" or "reserved" sets
2016 * then it is percent encoded. Other than that the characters are copied over without
2019 static BOOL
canonicalize_userinfo(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
2022 uri
->userinfo_start
= uri
->userinfo_split
= -1;
2023 uri
->userinfo_len
= 0;
2026 /* URI doesn't have userinfo, so nothing to do here. */
2029 uri
->userinfo_start
= uri
->canon_len
;
2031 while(i
< data
->userinfo_len
) {
2032 if(data
->userinfo
[i
] == ':' && uri
->userinfo_split
== -1)
2033 /* Windows only considers the first ':' as the delimiter. */
2034 uri
->userinfo_split
= uri
->canon_len
- uri
->userinfo_start
;
2035 else if(data
->userinfo
[i
] == '%') {
2036 /* Only decode % encoded values for known scheme types. */
2037 if(data
->scheme_type
!= URL_SCHEME_UNKNOWN
) {
2038 /* See if the value really needs decoded. */
2039 WCHAR val
= decode_pct_val(data
->userinfo
+ i
);
2040 if(is_unreserved(val
)) {
2042 uri
->canon_uri
[uri
->canon_len
] = val
;
2046 /* Move pass the hex characters. */
2051 } else if(!is_reserved(data
->userinfo
[i
]) && !is_unreserved(data
->userinfo
[i
]) &&
2052 data
->userinfo
[i
] != '\\') {
2053 /* Only percent encode forbidden characters if the NO_ENCODE_FORBIDDEN_CHARACTERS flag
2056 if(!(flags
& Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS
)) {
2058 pct_encode_val(data
->userinfo
[i
], uri
->canon_uri
+ uri
->canon_len
);
2060 uri
->canon_len
+= 3;
2067 /* Nothing special, so just copy the character over. */
2068 uri
->canon_uri
[uri
->canon_len
] = data
->userinfo
[i
];
2074 uri
->userinfo_len
= uri
->canon_len
- uri
->userinfo_start
;
2076 TRACE("(%p %p %x %d): Canonicalized userinfo, userinfo_start=%d, userinfo=%s, userinfo_split=%d userinfo_len=%d.\n",
2077 data
, uri
, flags
, computeOnly
, uri
->userinfo_start
, debugstr_wn(uri
->canon_uri
+ uri
->userinfo_start
, uri
->userinfo_len
),
2078 uri
->userinfo_split
, uri
->userinfo_len
);
2080 /* Now insert the '@' after the userinfo. */
2082 uri
->canon_uri
[uri
->canon_len
] = '@';
2088 /* Attempts to canonicalize a reg_name.
2090 * Things that happen:
2091 * 1) If Uri_CREATE_NO_CANONICALIZE flag is not set, then the reg_name is
2092 * lower cased. Unless it's an unknown scheme type, which case it's
2093 * no lower cased reguardless.
2095 * 2) Unreserved % encoded characters are decoded for known
2098 * 3) Forbidden characters are % encoded as long as
2099 * Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS flag is not set and
2100 * it isn't an unknown scheme type.
2102 * 4) If it's a file scheme and the host is "localhost" it's removed.
2104 static BOOL
canonicalize_reg_name(const parse_data
*data
, Uri
*uri
,
2105 DWORD flags
, BOOL computeOnly
) {
2106 static const WCHAR localhostW
[] =
2107 {'l','o','c','a','l','h','o','s','t',0};
2109 const BOOL known_scheme
= data
->scheme_type
!= URL_SCHEME_UNKNOWN
;
2111 uri
->host_start
= uri
->canon_len
;
2113 if(data
->scheme_type
== URL_SCHEME_FILE
&&
2114 data
->host_len
== lstrlenW(localhostW
)) {
2115 if(!StrCmpNIW(data
->host
, localhostW
, data
->host_len
)) {
2116 uri
->host_start
= -1;
2118 uri
->host_type
= Uri_HOST_UNKNOWN
;
2123 for(ptr
= data
->host
; ptr
< data
->host
+data
->host_len
; ++ptr
) {
2124 if(*ptr
== '%' && known_scheme
) {
2125 WCHAR val
= decode_pct_val(ptr
);
2126 if(is_unreserved(val
)) {
2127 /* If NO_CANONICALZE is not set, then windows lower cases the
2130 if(!(flags
& Uri_CREATE_NO_CANONICALIZE
) && isupperW(val
)) {
2132 uri
->canon_uri
[uri
->canon_len
] = tolowerW(val
);
2135 uri
->canon_uri
[uri
->canon_len
] = val
;
2139 /* Skip past the % encoded character. */
2143 /* Just copy the % over. */
2145 uri
->canon_uri
[uri
->canon_len
] = *ptr
;
2148 } else if(*ptr
== '\\') {
2149 /* Only unknown scheme types could have made it here with a '\\' in the host name. */
2151 uri
->canon_uri
[uri
->canon_len
] = *ptr
;
2153 } else if(!(flags
& Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS
) &&
2154 !is_unreserved(*ptr
) && !is_reserved(*ptr
) && known_scheme
) {
2156 pct_encode_val(*ptr
, uri
->canon_uri
+uri
->canon_len
);
2158 /* The percent encoded value gets lower cased also. */
2159 if(!(flags
& Uri_CREATE_NO_CANONICALIZE
)) {
2160 uri
->canon_uri
[uri
->canon_len
+1] = tolowerW(uri
->canon_uri
[uri
->canon_len
+1]);
2161 uri
->canon_uri
[uri
->canon_len
+2] = tolowerW(uri
->canon_uri
[uri
->canon_len
+2]);
2165 uri
->canon_len
+= 3;
2168 if(!(flags
& Uri_CREATE_NO_CANONICALIZE
) && known_scheme
)
2169 uri
->canon_uri
[uri
->canon_len
] = tolowerW(*ptr
);
2171 uri
->canon_uri
[uri
->canon_len
] = *ptr
;
2178 uri
->host_len
= uri
->canon_len
- uri
->host_start
;
2181 TRACE("(%p %p %x %d): Canonicalize reg_name=%s len=%d\n", data
, uri
, flags
,
2182 computeOnly
, debugstr_wn(uri
->canon_uri
+uri
->host_start
, uri
->host_len
),
2186 find_domain_name(uri
->canon_uri
+uri
->host_start
, uri
->host_len
,
2187 &(uri
->domain_offset
));
2192 /* Attempts to canonicalize an implicit IPv4 address. */
2193 static BOOL
canonicalize_implicit_ipv4address(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
2194 uri
->host_start
= uri
->canon_len
;
2196 TRACE("%u\n", data
->implicit_ipv4
);
2197 /* For unknown scheme types Window's doesn't convert
2198 * the value into an IP address, but, it still considers
2199 * it an IPv4 address.
2201 if(data
->scheme_type
== URL_SCHEME_UNKNOWN
) {
2203 memcpy(uri
->canon_uri
+uri
->canon_len
, data
->host
, data
->host_len
*sizeof(WCHAR
));
2204 uri
->canon_len
+= data
->host_len
;
2207 uri
->canon_len
+= ui2ipv4(uri
->canon_uri
+uri
->canon_len
, data
->implicit_ipv4
);
2209 uri
->canon_len
+= ui2ipv4(NULL
, data
->implicit_ipv4
);
2212 uri
->host_len
= uri
->canon_len
- uri
->host_start
;
2213 uri
->host_type
= Uri_HOST_IPV4
;
2216 TRACE("%p %p %x %d): Canonicalized implicit IP address=%s len=%d\n",
2217 data
, uri
, flags
, computeOnly
,
2218 debugstr_wn(uri
->canon_uri
+uri
->host_start
, uri
->host_len
),
2224 /* Attempts to canonicalize an IPv4 address.
2226 * If the parse_data represents a URI that has an implicit IPv4 address
2227 * (ex. http://256/, this function will convert 256 into 0.0.1.0). If
2228 * the implicit IP address exceeds the value of UINT_MAX (maximum value
2229 * for an IPv4 address) it's canonicalized as if were a reg-name.
2231 * If the parse_data contains a partial or full IPv4 address it normalizes it.
2232 * A partial IPv4 address is something like "192.0" and would be normalized to
2233 * "192.0.0.0". With a full (or partial) IPv4 address like "192.002.01.003" would
2234 * be normalized to "192.2.1.3".
2237 * Window's ONLY normalizes IPv4 address for known scheme types (one that isn't
2238 * URL_SCHEME_UNKNOWN). For unknown scheme types, it simply copies the data from
2239 * the original URI into the canonicalized URI, but, it still recognizes URI's
2240 * host type as HOST_IPV4.
2242 static BOOL
canonicalize_ipv4address(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
2243 if(data
->has_implicit_ip
)
2244 return canonicalize_implicit_ipv4address(data
, uri
, flags
, computeOnly
);
2246 uri
->host_start
= uri
->canon_len
;
2248 /* Windows only normalizes for known scheme types. */
2249 if(data
->scheme_type
!= URL_SCHEME_UNKNOWN
) {
2250 /* parse_data contains a partial or full IPv4 address, so normalize it. */
2251 DWORD i
, octetDigitCount
= 0, octetCount
= 0;
2252 BOOL octetHasDigit
= FALSE
;
2254 for(i
= 0; i
< data
->host_len
; ++i
) {
2255 if(data
->host
[i
] == '0' && !octetHasDigit
) {
2256 /* Can ignore leading zeros if:
2257 * 1) It isn't the last digit of the octet.
2258 * 2) i+1 != data->host_len
2261 if(octetDigitCount
== 2 ||
2262 i
+1 == data
->host_len
||
2263 data
->host
[i
+1] == '.') {
2265 uri
->canon_uri
[uri
->canon_len
] = data
->host
[i
];
2267 TRACE("Adding zero\n");
2269 } else if(data
->host
[i
] == '.') {
2271 uri
->canon_uri
[uri
->canon_len
] = data
->host
[i
];
2274 octetDigitCount
= 0;
2275 octetHasDigit
= FALSE
;
2279 uri
->canon_uri
[uri
->canon_len
] = data
->host
[i
];
2283 octetHasDigit
= TRUE
;
2287 /* Make sure the canonicalized IP address has 4 dec-octets.
2288 * If doesn't add "0" ones until there is 4;
2290 for( ; octetCount
< 3; ++octetCount
) {
2292 uri
->canon_uri
[uri
->canon_len
] = '.';
2293 uri
->canon_uri
[uri
->canon_len
+1] = '0';
2296 uri
->canon_len
+= 2;
2299 /* Windows doesn't normalize addresses in unknown schemes. */
2301 memcpy(uri
->canon_uri
+uri
->canon_len
, data
->host
, data
->host_len
*sizeof(WCHAR
));
2302 uri
->canon_len
+= data
->host_len
;
2305 uri
->host_len
= uri
->canon_len
- uri
->host_start
;
2307 TRACE("(%p %p %x %d): Canonicalized IPv4 address, ip=%s len=%d\n",
2308 data
, uri
, flags
, computeOnly
,
2309 debugstr_wn(uri
->canon_uri
+uri
->host_start
, uri
->host_len
),
2316 /* Attempts to canonicalize the IPv6 address of the URI.
2318 * Multiple things happen during the canonicalization of an IPv6 address:
2319 * 1) Any leading zero's in an h16 component are removed.
2320 * Ex: [0001:0022::] -> [1:22::]
2322 * 2) The longest sequence of zero h16 components are compressed
2323 * into a "::" (elision). If there's a tie, the first is choosen.
2325 * Ex: [0:0:0:0:1:6:7:8] -> [::1:6:7:8]
2326 * [0:0:0:0:1:2::] -> [::1:2:0:0]
2327 * [0:0:1:2:0:0:7:8] -> [::1:2:0:0:7:8]
2329 * 3) If an IPv4 address is attached to the IPv6 address, it's
2331 * Ex: [::001.002.022.000] -> [::1.2.22.0]
2333 * 4) If an elision is present, but, only represents 1 h16 component
2336 * Ex: [1::2:3:4:5:6:7] -> [1:0:2:3:4:5:6:7]
2338 * 5) If the IPv6 address contains an IPv4 address and there exists
2339 * at least 1 non-zero h16 component the IPv4 address is converted
2340 * into two h16 components, otherwise it's normalized and kept as is.
2342 * Ex: [::192.200.003.4] -> [::192.200.3.4]
2343 * [ffff::192.200.003.4] -> [ffff::c0c8:3041]
2346 * For unknown scheme types Windows simply copies the address over without any
2349 * IPv4 address can be included in an elision if all its components are 0's.
2351 static BOOL
canonicalize_ipv6address(const parse_data
*data
, Uri
*uri
,
2352 DWORD flags
, BOOL computeOnly
) {
2353 uri
->host_start
= uri
->canon_len
;
2355 if(data
->scheme_type
== URL_SCHEME_UNKNOWN
) {
2357 memcpy(uri
->canon_uri
+uri
->canon_len
, data
->host
, data
->host_len
*sizeof(WCHAR
));
2358 uri
->canon_len
+= data
->host_len
;
2362 DWORD i
, elision_len
;
2364 if(!ipv6_to_number(&(data
->ipv6_address
), values
)) {
2365 TRACE("(%p %p %x %d): Failed to compute numerical value for IPv6 address.\n",
2366 data
, uri
, flags
, computeOnly
);
2371 uri
->canon_uri
[uri
->canon_len
] = '[';
2374 /* Find where the elision should occur (if any). */
2375 compute_elision_location(&(data
->ipv6_address
), values
, &elision_start
, &elision_len
);
2377 TRACE("%p %p %x %d): Elision starts at %d, len=%u\n", data
, uri
, flags
,
2378 computeOnly
, elision_start
, elision_len
);
2380 for(i
= 0; i
< 8; ++i
) {
2381 BOOL in_elision
= (elision_start
> -1 && i
>= elision_start
&&
2382 i
< elision_start
+elision_len
);
2383 BOOL do_ipv4
= (i
== 6 && data
->ipv6_address
.ipv4
&& !in_elision
&&
2384 data
->ipv6_address
.h16_count
== 0);
2386 if(i
== elision_start
) {
2388 uri
->canon_uri
[uri
->canon_len
] = ':';
2389 uri
->canon_uri
[uri
->canon_len
+1] = ':';
2391 uri
->canon_len
+= 2;
2394 /* We can ignore the current component if we're in the elision. */
2398 /* We only add a ':' if we're not at i == 0, or when we're at
2399 * the very end of elision range since the ':' colon was handled
2400 * earlier. Otherwise we would end up with ":::" after elision.
2402 if(i
!= 0 && !(elision_start
> -1 && i
== elision_start
+elision_len
)) {
2404 uri
->canon_uri
[uri
->canon_len
] = ':';
2412 /* Combine the two parts of the IPv4 address values. */
2418 len
= ui2ipv4(uri
->canon_uri
+uri
->canon_len
, val
);
2420 len
= ui2ipv4(NULL
, val
);
2422 uri
->canon_len
+= len
;
2425 /* Write a regular h16 component to the URI. */
2427 /* Short circuit for the trivial case. */
2428 if(values
[i
] == 0) {
2430 uri
->canon_uri
[uri
->canon_len
] = '0';
2433 static const WCHAR formatW
[] = {'%','x',0};
2436 uri
->canon_len
+= sprintfW(uri
->canon_uri
+uri
->canon_len
,
2437 formatW
, values
[i
]);
2440 uri
->canon_len
+= sprintfW(tmp
, formatW
, values
[i
]);
2446 /* Add the closing ']'. */
2448 uri
->canon_uri
[uri
->canon_len
] = ']';
2452 uri
->host_len
= uri
->canon_len
- uri
->host_start
;
2455 TRACE("(%p %p %x %d): Canonicalized IPv6 address %s, len=%d\n", data
, uri
, flags
,
2456 computeOnly
, debugstr_wn(uri
->canon_uri
+uri
->host_start
, uri
->host_len
),
2462 /* Attempts to canonicalize the host of the URI (if any). */
2463 static BOOL
canonicalize_host(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
2464 uri
->host_start
= -1;
2466 uri
->domain_offset
= -1;
2469 switch(data
->host_type
) {
2471 uri
->host_type
= Uri_HOST_DNS
;
2472 if(!canonicalize_reg_name(data
, uri
, flags
, computeOnly
))
2477 uri
->host_type
= Uri_HOST_IPV4
;
2478 if(!canonicalize_ipv4address(data
, uri
, flags
, computeOnly
))
2483 if(!canonicalize_ipv6address(data
, uri
, flags
, computeOnly
))
2486 uri
->host_type
= Uri_HOST_IPV6
;
2488 case Uri_HOST_UNKNOWN
:
2489 if(data
->host_len
> 0 || data
->scheme_type
!= URL_SCHEME_FILE
) {
2490 uri
->host_start
= uri
->canon_len
;
2492 /* Nothing happens to unknown host types. */
2494 memcpy(uri
->canon_uri
+uri
->canon_len
, data
->host
, data
->host_len
*sizeof(WCHAR
));
2495 uri
->canon_len
+= data
->host_len
;
2496 uri
->host_len
= data
->host_len
;
2499 uri
->host_type
= Uri_HOST_UNKNOWN
;
2502 FIXME("(%p %p %x %d): Canonicalization for host type %d not supported.\n", data
,
2503 uri
, flags
, computeOnly
, data
->host_type
);
2511 static BOOL
canonicalize_port(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
2512 BOOL has_default_port
= FALSE
;
2513 USHORT default_port
= 0;
2516 uri
->has_port
= FALSE
;
2518 /* Check if the scheme has a default port. */
2519 for(i
= 0; i
< sizeof(default_ports
)/sizeof(default_ports
[0]); ++i
) {
2520 if(default_ports
[i
].scheme
== data
->scheme_type
) {
2521 has_default_port
= TRUE
;
2522 default_port
= default_ports
[i
].port
;
2527 if(data
->port
|| has_default_port
)
2528 uri
->has_port
= TRUE
;
2531 * 1) Has a port which is the default port.
2532 * 2) Has a port (not the default).
2533 * 3) Doesn't have a port, but, scheme has a default port.
2536 if(has_default_port
&& data
->port
&& data
->port_value
== default_port
) {
2537 /* If it's the default port and this flag isn't set, don't do anything. */
2538 if(flags
& Uri_CREATE_NO_CANONICALIZE
) {
2539 /* Copy the original port over. */
2541 uri
->canon_uri
[uri
->canon_len
] = ':';
2542 memcpy(uri
->canon_uri
+uri
->canon_len
+1, data
->port
, data
->port_len
*sizeof(WCHAR
));
2544 uri
->canon_len
+= data
->port_len
+1;
2547 uri
->port
= default_port
;
2548 } else if(data
->port
) {
2550 uri
->canon_uri
[uri
->canon_len
] = ':';
2553 if(flags
& Uri_CREATE_NO_CANONICALIZE
) {
2554 /* Copy the original over without changes. */
2556 memcpy(uri
->canon_uri
+uri
->canon_len
, data
->port
, data
->port_len
*sizeof(WCHAR
));
2557 uri
->canon_len
+= data
->port_len
;
2559 const WCHAR formatW
[] = {'%','u',0};
2562 len
= sprintfW(uri
->canon_uri
+uri
->canon_len
, formatW
, data
->port_value
);
2565 len
= sprintfW(tmp
, formatW
, data
->port_value
);
2567 uri
->canon_len
+= len
;
2570 uri
->port
= data
->port_value
;
2571 } else if(has_default_port
)
2572 uri
->port
= default_port
;
2577 /* Canonicalizes the authority of the URI represented by the parse_data. */
2578 static BOOL
canonicalize_authority(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
2579 uri
->authority_start
= uri
->canon_len
;
2580 uri
->authority_len
= 0;
2582 if(!canonicalize_userinfo(data
, uri
, flags
, computeOnly
))
2585 if(!canonicalize_host(data
, uri
, flags
, computeOnly
))
2588 if(!canonicalize_port(data
, uri
, flags
, computeOnly
))
2591 if(uri
->host_start
!= -1)
2592 uri
->authority_len
= uri
->canon_len
- uri
->authority_start
;
2594 uri
->authority_start
= -1;
2599 /* Attempts to canonicalize the path of a hierarchical URI.
2601 * Things that happen:
2602 * 1). Forbidden characters are percent encoded, unless the NO_ENCODE_FORBIDDEN
2603 * flag is set or it's a file URI. Forbidden characters are always encoded
2604 * for file schemes reguardless and forbidden characters are never encoded
2605 * for unknown scheme types.
2607 * 2). For known scheme types '\\' are changed to '/'.
2609 * 3). Percent encoded, unreserved characters are decoded to their actual values.
2610 * Unless the scheme type is unknown. For file schemes any percent encoded
2611 * character in the unreserved or reserved set is decoded.
2613 * 4). For File schemes if the path is starts with a drive letter and doesn't
2614 * start with a '/' then one is appended.
2615 * Ex: file://c:/test.mp3 -> file:///c:/test.mp3
2617 * 5). Dot segments are removed from the path for all scheme types
2618 * unless NO_CANONICALIZE flag is set. Dot segments aren't removed
2619 * for wildcard scheme types.
2622 * file://c:/test%20test -> file:///c:/test%2520test
2623 * file://c:/test%3Etest -> file:///c:/test%253Etest
2624 * file:///c:/test%20test -> file:///c:/test%20test
2625 * file:///c:/test%test -> file:///c:/test%25test
2627 static BOOL
canonicalize_path_hierarchical(const parse_data
*data
, Uri
*uri
,
2628 DWORD flags
, BOOL computeOnly
) {
2630 const BOOL known_scheme
= data
->scheme_type
!= URL_SCHEME_UNKNOWN
;
2631 const BOOL is_file
= data
->scheme_type
== URL_SCHEME_FILE
;
2633 BOOL escape_pct
= FALSE
;
2636 uri
->path_start
= -1;
2641 uri
->path_start
= uri
->canon_len
;
2643 /* Check if a '/' needs to be appended for the file scheme. */
2645 if(data
->path_len
> 1 && is_alpha(*(data
->path
)) &&
2646 *(data
->path
+1) == ':') {
2648 uri
->canon_uri
[uri
->canon_len
] = '/';
2654 for(ptr
= data
->path
; ptr
< data
->path
+data
->path_len
; ++ptr
) {
2656 const WCHAR
*tmp
= ptr
;
2659 /* Check if the % represents a valid encoded char, or if it needs encoded. */
2660 BOOL force_encode
= !check_pct_encoded(&tmp
) && is_file
;
2661 val
= decode_pct_val(ptr
);
2663 if(force_encode
|| escape_pct
) {
2664 /* Escape the percent sign in the file URI. */
2666 pct_encode_val(*ptr
, uri
->canon_uri
+uri
->canon_len
);
2667 uri
->canon_len
+= 3;
2668 } else if((is_unreserved(val
) && known_scheme
) ||
2669 (is_file
&& (is_unreserved(val
) || is_reserved(val
)))) {
2671 uri
->canon_uri
[uri
->canon_len
] = val
;
2678 uri
->canon_uri
[uri
->canon_len
] = *ptr
;
2681 } else if(*ptr
== '\\' && known_scheme
) {
2683 uri
->canon_uri
[uri
->canon_len
] = '/';
2685 } else if(known_scheme
&& !is_unreserved(*ptr
) && !is_reserved(*ptr
) &&
2686 (!(flags
& Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS
) || is_file
)) {
2687 /* Escape the forbidden character. */
2689 pct_encode_val(*ptr
, uri
->canon_uri
+uri
->canon_len
);
2690 uri
->canon_len
+= 3;
2693 uri
->canon_uri
[uri
->canon_len
] = *ptr
;
2698 uri
->path_len
= uri
->canon_len
- uri
->path_start
;
2700 /* Removing the dot segments only happens when it's not in
2701 * computeOnly mode and it's not a wildcard scheme.
2703 if(!computeOnly
&& data
->scheme_type
!= URL_SCHEME_WILDCARD
) {
2704 if(!(flags
& Uri_CREATE_NO_CANONICALIZE
)) {
2705 /* Remove the dot segments (if any) and reset everything to the new
2708 DWORD new_len
= remove_dot_segments(uri
->canon_uri
+uri
->path_start
, uri
->path_len
);
2709 uri
->canon_len
-= uri
->path_len
-new_len
;
2710 uri
->path_len
= new_len
;
2715 TRACE("Canonicalized path %s len=%d\n",
2716 debugstr_wn(uri
->canon_uri
+uri
->path_start
, uri
->path_len
),
2722 /* Attempts to canonicalize the path for an opaque URI.
2724 * For known scheme types:
2725 * 1) forbidden characters are percent encoded if
2726 * NO_ENCODE_FORBIDDEN_CHARACTERS isn't set.
2728 * 2) Percent encoded, unreserved characters are decoded
2729 * to their actual values, for known scheme types.
2731 * 3) '\\' are changed to '/' for known scheme types
2732 * except for mailto schemes.
2734 static BOOL
canonicalize_path_opaque(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
2736 const BOOL known_scheme
= data
->scheme_type
!= URL_SCHEME_UNKNOWN
;
2739 uri
->path_start
= -1;
2744 uri
->path_start
= uri
->canon_len
;
2746 /* Windows doesn't allow a "//" to appear after the scheme
2747 * of a URI, if it's an opaque URI.
2749 if(data
->scheme
&& *(data
->path
) == '/' && *(data
->path
+1) == '/') {
2750 /* So it inserts a "/." before the "//" if it exists. */
2752 uri
->canon_uri
[uri
->canon_len
] = '/';
2753 uri
->canon_uri
[uri
->canon_len
+1] = '.';
2756 uri
->canon_len
+= 2;
2759 for(ptr
= data
->path
; ptr
< data
->path
+data
->path_len
; ++ptr
) {
2760 if(*ptr
== '%' && known_scheme
) {
2761 WCHAR val
= decode_pct_val(ptr
);
2763 if(is_unreserved(val
)) {
2765 uri
->canon_uri
[uri
->canon_len
] = val
;
2772 uri
->canon_uri
[uri
->canon_len
] = *ptr
;
2775 } else if(known_scheme
&& !is_unreserved(*ptr
) && !is_reserved(*ptr
) &&
2776 !(flags
& Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS
)) {
2778 pct_encode_val(*ptr
, uri
->canon_uri
+uri
->canon_len
);
2779 uri
->canon_len
+= 3;
2782 uri
->canon_uri
[uri
->canon_len
] = *ptr
;
2787 uri
->path_len
= uri
->canon_len
- uri
->path_start
;
2789 TRACE("(%p %p %x %d): Canonicalized opaque URI path %s len=%d\n", data
, uri
, flags
, computeOnly
,
2790 debugstr_wn(uri
->canon_uri
+uri
->path_start
, uri
->path_len
), uri
->path_len
);
2794 /* Determines how the URI represented by the parse_data should be canonicalized.
2796 * Essentially, if the parse_data represents an hierarchical URI then it calls
2797 * canonicalize_authority and the canonicalization functions for the path. If the
2798 * URI is opaque it canonicalizes the path of the URI.
2800 static BOOL
canonicalize_hierpart(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
2801 if(!data
->is_opaque
) {
2802 /* "//" is only added for non-wildcard scheme types. */
2803 if(data
->scheme_type
!= URL_SCHEME_WILDCARD
) {
2805 INT pos
= uri
->canon_len
;
2807 uri
->canon_uri
[pos
] = '/';
2808 uri
->canon_uri
[pos
+1] = '/';
2810 uri
->canon_len
+= 2;
2813 if(!canonicalize_authority(data
, uri
, flags
, computeOnly
))
2816 /* TODO: Canonicalize the path of the URI. */
2817 if(!canonicalize_path_hierarchical(data
, uri
, flags
, computeOnly
))
2821 /* Opaque URI's don't have an authority. */
2822 uri
->userinfo_start
= uri
->userinfo_split
= -1;
2823 uri
->userinfo_len
= 0;
2824 uri
->host_start
= -1;
2826 uri
->host_type
= Uri_HOST_UNKNOWN
;
2827 uri
->has_port
= FALSE
;
2828 uri
->authority_start
= -1;
2829 uri
->authority_len
= 0;
2830 uri
->domain_offset
= -1;
2832 if(!canonicalize_path_opaque(data
, uri
, flags
, computeOnly
))
2836 if(uri
->path_start
> -1 && !computeOnly
)
2837 /* Finding file extensions happens for both types of URIs. */
2838 uri
->extension_offset
= find_file_extension(uri
->canon_uri
+uri
->path_start
, uri
->path_len
);
2840 uri
->extension_offset
= -1;
2845 /* Attempts to canonicalize the query string of the URI.
2847 * Things that happen:
2848 * 1) For known scheme types forbidden characters
2849 * are percent encoded, unless the NO_DECODE_EXTRA_INFO flag is set
2850 * or NO_ENCODE_FORBIDDEN_CHARACTERS is set.
2852 * 2) For known scheme types, percent encoded, unreserved characters
2853 * are decoded as long as the NO_DECODE_EXTRA_INFO flag isn't set.
2855 static BOOL
canonicalize_query(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
2856 const WCHAR
*ptr
, *end
;
2857 const BOOL known_scheme
= data
->scheme_type
!= URL_SCHEME_UNKNOWN
;
2860 uri
->query_start
= -1;
2865 uri
->query_start
= uri
->canon_len
;
2867 end
= data
->query
+data
->query_len
;
2868 for(ptr
= data
->query
; ptr
< end
; ++ptr
) {
2870 if(known_scheme
&& !(flags
& Uri_CREATE_NO_DECODE_EXTRA_INFO
)) {
2871 WCHAR val
= decode_pct_val(ptr
);
2872 if(is_unreserved(val
)) {
2874 uri
->canon_uri
[uri
->canon_len
] = val
;
2881 } else if(known_scheme
&& !is_unreserved(*ptr
) && !is_reserved(*ptr
)) {
2882 if(!(flags
& Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS
) &&
2883 !(flags
& Uri_CREATE_NO_DECODE_EXTRA_INFO
)) {
2885 pct_encode_val(*ptr
, uri
->canon_uri
+uri
->canon_len
);
2886 uri
->canon_len
+= 3;
2892 uri
->canon_uri
[uri
->canon_len
] = *ptr
;
2896 uri
->query_len
= uri
->canon_len
- uri
->query_start
;
2899 TRACE("(%p %p %x %d): Canonicalized query string %s len=%d\n", data
, uri
, flags
,
2900 computeOnly
, debugstr_wn(uri
->canon_uri
+uri
->query_start
, uri
->query_len
),
2905 static BOOL
canonicalize_fragment(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
2906 const WCHAR
*ptr
, *end
;
2907 const BOOL known_scheme
= data
->scheme_type
!= URL_SCHEME_UNKNOWN
;
2909 if(!data
->fragment
) {
2910 uri
->fragment_start
= -1;
2911 uri
->fragment_len
= 0;
2915 uri
->fragment_start
= uri
->canon_len
;
2917 end
= data
->fragment
+ data
->fragment_len
;
2918 for(ptr
= data
->fragment
; ptr
< end
; ++ptr
) {
2920 if(known_scheme
&& !(flags
& Uri_CREATE_NO_DECODE_EXTRA_INFO
)) {
2921 WCHAR val
= decode_pct_val(ptr
);
2922 if(is_unreserved(val
)) {
2924 uri
->canon_uri
[uri
->canon_len
] = val
;
2931 } else if(known_scheme
&& !is_unreserved(*ptr
) && !is_reserved(*ptr
)) {
2932 if(!(flags
& Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS
) &&
2933 !(flags
& Uri_CREATE_NO_DECODE_EXTRA_INFO
)) {
2935 pct_encode_val(*ptr
, uri
->canon_uri
+uri
->canon_len
);
2936 uri
->canon_len
+= 3;
2942 uri
->canon_uri
[uri
->canon_len
] = *ptr
;
2946 uri
->fragment_len
= uri
->canon_len
- uri
->fragment_start
;
2949 TRACE("(%p %p %x %d): Canonicalized fragment %s len=%d\n", data
, uri
, flags
,
2950 computeOnly
, debugstr_wn(uri
->canon_uri
+uri
->fragment_start
, uri
->fragment_len
),
2955 /* Canonicalizes the scheme information specified in the parse_data using the specified flags. */
2956 static BOOL
canonicalize_scheme(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
2957 uri
->scheme_start
= -1;
2958 uri
->scheme_len
= 0;
2961 /* The only type of URI that doesn't have to have a scheme is a relative
2964 if(!data
->is_relative
) {
2965 FIXME("(%p %p %x): Unable to determine the scheme type of %s.\n", data
,
2966 uri
, flags
, debugstr_w(data
->uri
));
2972 INT pos
= uri
->canon_len
;
2974 for(i
= 0; i
< data
->scheme_len
; ++i
) {
2975 /* Scheme name must be lower case after canonicalization. */
2976 uri
->canon_uri
[i
+ pos
] = tolowerW(data
->scheme
[i
]);
2979 uri
->canon_uri
[i
+ pos
] = ':';
2980 uri
->scheme_start
= pos
;
2982 TRACE("(%p %p %x): Canonicalized scheme=%s, len=%d.\n", data
, uri
, flags
,
2983 debugstr_wn(uri
->canon_uri
, uri
->scheme_len
), data
->scheme_len
);
2986 /* This happens in both computation modes. */
2987 uri
->canon_len
+= data
->scheme_len
+ 1;
2988 uri
->scheme_len
= data
->scheme_len
;
2993 /* Compute's what the length of the URI specified by the parse_data will be
2994 * after canonicalization occurs using the specified flags.
2996 * This function will return a non-zero value indicating the length of the canonicalized
2997 * URI, or -1 on error.
2999 static int compute_canonicalized_length(const parse_data
*data
, DWORD flags
) {
3002 memset(&uri
, 0, sizeof(Uri
));
3004 TRACE("(%p %x): Beginning to compute canonicalized length for URI %s\n", data
, flags
,
3005 debugstr_w(data
->uri
));
3007 if(!canonicalize_scheme(data
, &uri
, flags
, TRUE
)) {
3008 ERR("(%p %x): Failed to compute URI scheme length.\n", data
, flags
);
3012 if(!canonicalize_hierpart(data
, &uri
, flags
, TRUE
)) {
3013 ERR("(%p %x): Failed to compute URI hierpart length.\n", data
, flags
);
3017 if(!canonicalize_query(data
, &uri
, flags
, TRUE
)) {
3018 ERR("(%p %x): Failed to compute query string length.\n", data
, flags
);
3022 if(!canonicalize_fragment(data
, &uri
, flags
, TRUE
)) {
3023 ERR("(%p %x): Failed to compute fragment length.\n", data
, flags
);
3027 TRACE("(%p %x): Finished computing canonicalized URI length. length=%d\n", data
, flags
, uri
.canon_len
);
3029 return uri
.canon_len
;
3032 /* Canonicalizes the URI data specified in the parse_data, using the given flags. If the
3033 * canonicalization succeededs it will store all the canonicalization information
3034 * in the pointer to the Uri.
3036 * To canonicalize a URI this function first computes what the length of the URI
3037 * specified by the parse_data will be. Once this is done it will then perfom the actual
3038 * canonicalization of the URI.
3040 static HRESULT
canonicalize_uri(const parse_data
*data
, Uri
*uri
, DWORD flags
) {
3043 uri
->canon_uri
= NULL
;
3044 len
= uri
->canon_size
= uri
->canon_len
= 0;
3046 TRACE("(%p %p %x): beginning to canonicalize URI %s.\n", data
, uri
, flags
, debugstr_w(data
->uri
));
3048 /* First try to compute the length of the URI. */
3049 len
= compute_canonicalized_length(data
, flags
);
3051 ERR("(%p %p %x): Could not compute the canonicalized length of %s.\n", data
, uri
, flags
,
3052 debugstr_w(data
->uri
));
3053 return E_INVALIDARG
;
3056 uri
->canon_uri
= heap_alloc((len
+1)*sizeof(WCHAR
));
3058 return E_OUTOFMEMORY
;
3060 uri
->canon_size
= len
;
3061 if(!canonicalize_scheme(data
, uri
, flags
, FALSE
)) {
3062 ERR("(%p %p %x): Unable to canonicalize the scheme of the URI.\n", data
, uri
, flags
);
3063 heap_free(uri
->canon_uri
);
3064 return E_INVALIDARG
;
3066 uri
->scheme_type
= data
->scheme_type
;
3068 if(!canonicalize_hierpart(data
, uri
, flags
, FALSE
)) {
3069 ERR("(%p %p %x): Unable to canonicalize the heirpart of the URI\n", data
, uri
, flags
);
3070 heap_free(uri
->canon_uri
);
3071 return E_INVALIDARG
;
3074 if(!canonicalize_query(data
, uri
, flags
, FALSE
)) {
3075 ERR("(%p %p %x): Unable to canonicalize query string of the URI.\n",
3077 return E_INVALIDARG
;
3080 if(!canonicalize_fragment(data
, uri
, flags
, FALSE
)) {
3081 ERR("(%p %p %x): Unable to canonicalize fragment of the URI.\n",
3083 return E_INVALIDARG
;
3086 /* There's a possibility we didn't use all the space we allocated
3089 if(uri
->canon_len
< uri
->canon_size
) {
3090 /* This happens if the URI is hierarchical and dot
3091 * segments were removed from it's path.
3093 WCHAR
*tmp
= heap_realloc(uri
->canon_uri
, (uri
->canon_len
+1)*sizeof(WCHAR
));
3095 return E_OUTOFMEMORY
;
3097 uri
->canon_uri
= tmp
;
3098 uri
->canon_size
= uri
->canon_len
;
3101 uri
->canon_uri
[uri
->canon_len
] = '\0';
3102 TRACE("(%p %p %x): finished canonicalizing the URI. uri=%s\n", data
, uri
, flags
, debugstr_w(uri
->canon_uri
));
3107 #define URI(x) ((IUri*) &(x)->lpIUriVtbl)
3108 #define URIBUILDER(x) ((IUriBuilder*) &(x)->lpIUriBuilderVtbl)
3110 #define URI_THIS(iface) DEFINE_THIS(Uri, IUri, iface)
3112 static HRESULT WINAPI
Uri_QueryInterface(IUri
*iface
, REFIID riid
, void **ppv
)
3114 Uri
*This
= URI_THIS(iface
);
3116 if(IsEqualGUID(&IID_IUnknown
, riid
)) {
3117 TRACE("(%p)->(IID_IUnknown %p)\n", This
, ppv
);
3119 }else if(IsEqualGUID(&IID_IUri
, riid
)) {
3120 TRACE("(%p)->(IID_IUri %p)\n", This
, ppv
);
3122 }else if(IsEqualGUID(&IID_IUriObj
, riid
)) {
3123 TRACE("(%p)->(IID_IUriObj %p)\n", This
, ppv
);
3127 TRACE("(%p)->(%s %p)\n", This
, debugstr_guid(riid
), ppv
);
3129 return E_NOINTERFACE
;
3132 IUnknown_AddRef((IUnknown
*)*ppv
);
3136 static ULONG WINAPI
Uri_AddRef(IUri
*iface
)
3138 Uri
*This
= URI_THIS(iface
);
3139 LONG ref
= InterlockedIncrement(&This
->ref
);
3141 TRACE("(%p) ref=%d\n", This
, ref
);
3146 static ULONG WINAPI
Uri_Release(IUri
*iface
)
3148 Uri
*This
= URI_THIS(iface
);
3149 LONG ref
= InterlockedDecrement(&This
->ref
);
3151 TRACE("(%p) ref=%d\n", This
, ref
);
3154 SysFreeString(This
->raw_uri
);
3155 heap_free(This
->canon_uri
);
3162 static HRESULT WINAPI
Uri_GetPropertyBSTR(IUri
*iface
, Uri_PROPERTY uriProp
, BSTR
*pbstrProperty
, DWORD dwFlags
)
3164 Uri
*This
= URI_THIS(iface
);
3166 TRACE("(%p)->(%d %p %x)\n", This
, uriProp
, pbstrProperty
, dwFlags
);
3171 if(uriProp
> Uri_PROPERTY_STRING_LAST
) {
3172 /* Windows allocates an empty BSTR for invalid Uri_PROPERTY's. */
3173 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
3174 if(!(*pbstrProperty
))
3175 return E_OUTOFMEMORY
;
3177 /* It only returns S_FALSE for the ZONE property... */
3178 if(uriProp
== Uri_PROPERTY_ZONE
)
3184 /* Don't have support for flags yet. */
3186 FIXME("(%p)->(%d %p %x)\n", This
, uriProp
, pbstrProperty
, dwFlags
);
3191 case Uri_PROPERTY_ABSOLUTE_URI
:
3192 *pbstrProperty
= SysAllocString(This
->canon_uri
);
3194 if(!(*pbstrProperty
))
3195 hres
= E_OUTOFMEMORY
;
3200 case Uri_PROPERTY_AUTHORITY
:
3201 if(This
->authority_start
> -1) {
3202 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+This
->authority_start
, This
->authority_len
);
3205 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
3209 if(!(*pbstrProperty
))
3210 hres
= E_OUTOFMEMORY
;
3213 case Uri_PROPERTY_DISPLAY_URI
:
3214 /* The Display URI contains everything except for the userinfo for known
3217 if(This
->scheme_type
!= URL_SCHEME_UNKNOWN
&& This
->userinfo_start
> -1) {
3218 *pbstrProperty
= SysAllocStringLen(NULL
, This
->canon_len
-This
->userinfo_len
);
3220 if(*pbstrProperty
) {
3221 /* Copy everything before the userinfo over. */
3222 memcpy(*pbstrProperty
, This
->canon_uri
, This
->userinfo_start
*sizeof(WCHAR
));
3223 /* Copy everything after the userinfo over. */
3224 memcpy(*pbstrProperty
+This
->userinfo_start
,
3225 This
->canon_uri
+This
->userinfo_start
+This
->userinfo_len
+1,
3226 (This
->canon_len
-(This
->userinfo_start
+This
->userinfo_len
+1))*sizeof(WCHAR
));
3229 *pbstrProperty
= SysAllocString(This
->canon_uri
);
3231 if(!(*pbstrProperty
))
3232 hres
= E_OUTOFMEMORY
;
3237 case Uri_PROPERTY_DOMAIN
:
3238 if(This
->domain_offset
> -1) {
3239 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+This
->host_start
+This
->domain_offset
,
3240 This
->host_len
-This
->domain_offset
);
3243 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
3247 if(!(*pbstrProperty
))
3248 hres
= E_OUTOFMEMORY
;
3251 case Uri_PROPERTY_EXTENSION
:
3252 if(This
->extension_offset
> -1) {
3253 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+This
->path_start
+This
->extension_offset
,
3254 This
->path_len
-This
->extension_offset
);
3257 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
3261 if(!(*pbstrProperty
))
3262 hres
= E_OUTOFMEMORY
;
3265 case Uri_PROPERTY_FRAGMENT
:
3266 if(This
->fragment_start
> -1) {
3267 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+This
->fragment_start
, This
->fragment_len
);
3270 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
3274 if(!(*pbstrProperty
))
3275 hres
= E_OUTOFMEMORY
;
3278 case Uri_PROPERTY_HOST
:
3279 if(This
->host_start
> -1) {
3280 /* The '[' and ']' aren't included for IPv6 addresses. */
3281 if(This
->host_type
== Uri_HOST_IPV6
)
3282 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+This
->host_start
+1, This
->host_len
-2);
3284 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+This
->host_start
, This
->host_len
);
3288 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
3292 if(!(*pbstrProperty
))
3293 hres
= E_OUTOFMEMORY
;
3296 case Uri_PROPERTY_PASSWORD
:
3297 if(This
->userinfo_split
> -1) {
3298 *pbstrProperty
= SysAllocStringLen(
3299 This
->canon_uri
+This
->userinfo_start
+This
->userinfo_split
+1,
3300 This
->userinfo_len
-This
->userinfo_split
-1);
3303 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
3307 if(!(*pbstrProperty
))
3308 return E_OUTOFMEMORY
;
3311 case Uri_PROPERTY_PATH
:
3312 if(This
->path_start
> -1) {
3313 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+This
->path_start
, This
->path_len
);
3316 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
3320 if(!(*pbstrProperty
))
3321 hres
= E_OUTOFMEMORY
;
3324 case Uri_PROPERTY_PATH_AND_QUERY
:
3325 if(This
->path_start
> -1) {
3326 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+This
->path_start
, This
->path_len
+This
->query_len
);
3328 } else if(This
->query_start
> -1) {
3329 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+This
->query_start
, This
->query_len
);
3332 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
3336 if(!(*pbstrProperty
))
3337 hres
= E_OUTOFMEMORY
;
3340 case Uri_PROPERTY_QUERY
:
3341 if(This
->query_start
> -1) {
3342 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+This
->query_start
, This
->query_len
);
3345 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
3349 if(!(*pbstrProperty
))
3350 hres
= E_OUTOFMEMORY
;
3353 case Uri_PROPERTY_RAW_URI
:
3354 *pbstrProperty
= SysAllocString(This
->raw_uri
);
3355 if(!(*pbstrProperty
))
3356 hres
= E_OUTOFMEMORY
;
3360 case Uri_PROPERTY_SCHEME_NAME
:
3361 if(This
->scheme_start
> -1) {
3362 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+ This
->scheme_start
, This
->scheme_len
);
3365 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
3369 if(!(*pbstrProperty
))
3370 hres
= E_OUTOFMEMORY
;
3373 case Uri_PROPERTY_USER_INFO
:
3374 if(This
->userinfo_start
> -1) {
3375 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+This
->userinfo_start
, This
->userinfo_len
);
3378 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
3382 if(!(*pbstrProperty
))
3383 hres
= E_OUTOFMEMORY
;
3386 case Uri_PROPERTY_USER_NAME
:
3387 if(This
->userinfo_start
> -1) {
3388 /* If userinfo_split is set, that means a password exists
3389 * so the username is only from userinfo_start to userinfo_split.
3391 if(This
->userinfo_split
> -1) {
3392 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+ This
->userinfo_start
, This
->userinfo_split
);
3395 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+ This
->userinfo_start
, This
->userinfo_len
);
3399 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
3403 if(!(*pbstrProperty
))
3404 return E_OUTOFMEMORY
;
3408 FIXME("(%p)->(%d %p %x)\n", This
, uriProp
, pbstrProperty
, dwFlags
);
3415 static HRESULT WINAPI
Uri_GetPropertyLength(IUri
*iface
, Uri_PROPERTY uriProp
, DWORD
*pcchProperty
, DWORD dwFlags
)
3417 Uri
*This
= URI_THIS(iface
);
3419 TRACE("(%p)->(%d %p %x)\n", This
, uriProp
, pcchProperty
, dwFlags
);
3422 return E_INVALIDARG
;
3424 /* Can only return a length for a property if it's a string. */
3425 if(uriProp
> Uri_PROPERTY_STRING_LAST
)
3426 return E_INVALIDARG
;
3428 /* Don't have support for flags yet. */
3430 FIXME("(%p)->(%d %p %x)\n", This
, uriProp
, pcchProperty
, dwFlags
);
3435 case Uri_PROPERTY_ABSOLUTE_URI
:
3436 *pcchProperty
= This
->canon_len
;
3439 case Uri_PROPERTY_AUTHORITY
:
3440 *pcchProperty
= This
->authority_len
;
3441 hres
= (This
->authority_start
> -1) ? S_OK
: S_FALSE
;
3443 case Uri_PROPERTY_DISPLAY_URI
:
3444 if(This
->scheme_type
!= URL_SCHEME_UNKNOWN
&& This
->userinfo_start
> -1)
3445 *pcchProperty
= This
->canon_len
-This
->userinfo_len
-1;
3447 *pcchProperty
= This
->canon_len
;
3451 case Uri_PROPERTY_DOMAIN
:
3452 if(This
->domain_offset
> -1)
3453 *pcchProperty
= This
->host_len
- This
->domain_offset
;
3457 hres
= (This
->domain_offset
> -1) ? S_OK
: S_FALSE
;
3459 case Uri_PROPERTY_EXTENSION
:
3460 if(This
->extension_offset
> -1) {
3461 *pcchProperty
= This
->path_len
- This
->extension_offset
;
3469 case Uri_PROPERTY_FRAGMENT
:
3470 *pcchProperty
= This
->fragment_len
;
3471 hres
= (This
->fragment_start
> -1) ? S_OK
: S_FALSE
;
3473 case Uri_PROPERTY_HOST
:
3474 *pcchProperty
= This
->host_len
;
3476 /* '[' and ']' aren't included in the length. */
3477 if(This
->host_type
== Uri_HOST_IPV6
)
3480 hres
= (This
->host_start
> -1) ? S_OK
: S_FALSE
;
3482 case Uri_PROPERTY_PASSWORD
:
3483 *pcchProperty
= (This
->userinfo_split
> -1) ? This
->userinfo_len
-This
->userinfo_split
-1 : 0;
3484 hres
= (This
->userinfo_split
> -1) ? S_OK
: S_FALSE
;
3486 case Uri_PROPERTY_PATH
:
3487 *pcchProperty
= This
->path_len
;
3488 hres
= (This
->path_start
> -1) ? S_OK
: S_FALSE
;
3490 case Uri_PROPERTY_PATH_AND_QUERY
:
3491 *pcchProperty
= This
->path_len
+This
->query_len
;
3492 hres
= (This
->path_start
> -1 || This
->query_start
> -1) ? S_OK
: S_FALSE
;
3494 case Uri_PROPERTY_QUERY
:
3495 *pcchProperty
= This
->query_len
;
3496 hres
= (This
->query_start
> -1) ? S_OK
: S_FALSE
;
3498 case Uri_PROPERTY_RAW_URI
:
3499 *pcchProperty
= SysStringLen(This
->raw_uri
);
3502 case Uri_PROPERTY_SCHEME_NAME
:
3503 *pcchProperty
= This
->scheme_len
;
3504 hres
= (This
->scheme_start
> -1) ? S_OK
: S_FALSE
;
3506 case Uri_PROPERTY_USER_INFO
:
3507 *pcchProperty
= This
->userinfo_len
;
3508 hres
= (This
->userinfo_start
> -1) ? S_OK
: S_FALSE
;
3510 case Uri_PROPERTY_USER_NAME
:
3511 *pcchProperty
= (This
->userinfo_split
> -1) ? This
->userinfo_split
: This
->userinfo_len
;
3512 hres
= (This
->userinfo_start
> -1) ? S_OK
: S_FALSE
;
3515 FIXME("(%p)->(%d %p %x)\n", This
, uriProp
, pcchProperty
, dwFlags
);
3522 static HRESULT WINAPI
Uri_GetPropertyDWORD(IUri
*iface
, Uri_PROPERTY uriProp
, DWORD
*pcchProperty
, DWORD dwFlags
)
3524 Uri
*This
= URI_THIS(iface
);
3527 TRACE("(%p)->(%d %p %x)\n", This
, uriProp
, pcchProperty
, dwFlags
);
3530 return E_INVALIDARG
;
3532 /* Microsoft's implementation for the ZONE property of a URI seems to be lacking...
3533 * From what I can tell, instead of checking which URLZONE the URI belongs to it
3534 * simply assigns URLZONE_INVALID and returns E_NOTIMPL. This also applies to the GetZone
3537 if(uriProp
== Uri_PROPERTY_ZONE
) {
3538 *pcchProperty
= URLZONE_INVALID
;
3542 if(uriProp
< Uri_PROPERTY_DWORD_START
) {
3544 return E_INVALIDARG
;
3548 case Uri_PROPERTY_HOST_TYPE
:
3549 *pcchProperty
= This
->host_type
;
3552 case Uri_PROPERTY_PORT
:
3553 if(!This
->has_port
) {
3557 *pcchProperty
= This
->port
;
3562 case Uri_PROPERTY_SCHEME
:
3563 *pcchProperty
= This
->scheme_type
;
3567 FIXME("(%p)->(%d %p %x)\n", This
, uriProp
, pcchProperty
, dwFlags
);
3574 static HRESULT WINAPI
Uri_HasProperty(IUri
*iface
, Uri_PROPERTY uriProp
, BOOL
*pfHasProperty
)
3576 Uri
*This
= URI_THIS(iface
);
3577 TRACE("(%p)->(%d %p)\n", This
, uriProp
, pfHasProperty
);
3580 return E_INVALIDARG
;
3583 case Uri_PROPERTY_ABSOLUTE_URI
:
3584 *pfHasProperty
= TRUE
;
3586 case Uri_PROPERTY_AUTHORITY
:
3587 *pfHasProperty
= This
->authority_start
> -1;
3589 case Uri_PROPERTY_DISPLAY_URI
:
3590 *pfHasProperty
= TRUE
;
3592 case Uri_PROPERTY_DOMAIN
:
3593 *pfHasProperty
= This
->domain_offset
> -1;
3595 case Uri_PROPERTY_EXTENSION
:
3596 *pfHasProperty
= This
->extension_offset
> -1;
3598 case Uri_PROPERTY_FRAGMENT
:
3599 *pfHasProperty
= This
->fragment_start
> -1;
3601 case Uri_PROPERTY_HOST
:
3602 *pfHasProperty
= This
->host_start
> -1;
3604 case Uri_PROPERTY_PASSWORD
:
3605 *pfHasProperty
= This
->userinfo_split
> -1;
3607 case Uri_PROPERTY_PATH
:
3608 *pfHasProperty
= This
->path_start
> -1;
3610 case Uri_PROPERTY_PATH_AND_QUERY
:
3611 *pfHasProperty
= (This
->path_start
> -1 || This
->query_start
> -1);
3613 case Uri_PROPERTY_QUERY
:
3614 *pfHasProperty
= This
->query_start
> -1;
3616 case Uri_PROPERTY_RAW_URI
:
3617 *pfHasProperty
= TRUE
;
3619 case Uri_PROPERTY_SCHEME_NAME
:
3620 *pfHasProperty
= This
->scheme_start
> -1;
3622 case Uri_PROPERTY_USER_INFO
:
3623 case Uri_PROPERTY_USER_NAME
:
3624 *pfHasProperty
= This
->userinfo_start
> -1;
3626 case Uri_PROPERTY_HOST_TYPE
:
3627 *pfHasProperty
= TRUE
;
3629 case Uri_PROPERTY_PORT
:
3630 *pfHasProperty
= This
->has_port
;
3632 case Uri_PROPERTY_SCHEME
:
3633 *pfHasProperty
= TRUE
;
3635 case Uri_PROPERTY_ZONE
:
3636 *pfHasProperty
= FALSE
;
3639 FIXME("(%p)->(%d %p): Unsupported property type.\n", This
, uriProp
, pfHasProperty
);
3646 static HRESULT WINAPI
Uri_GetAbsoluteUri(IUri
*iface
, BSTR
*pstrAbsoluteUri
)
3648 TRACE("(%p)->(%p)\n", iface
, pstrAbsoluteUri
);
3649 return Uri_GetPropertyBSTR(iface
, Uri_PROPERTY_ABSOLUTE_URI
, pstrAbsoluteUri
, 0);
3652 static HRESULT WINAPI
Uri_GetAuthority(IUri
*iface
, BSTR
*pstrAuthority
)
3654 TRACE("(%p)->(%p)\n", iface
, pstrAuthority
);
3655 return Uri_GetPropertyBSTR(iface
, Uri_PROPERTY_AUTHORITY
, pstrAuthority
, 0);
3658 static HRESULT WINAPI
Uri_GetDisplayUri(IUri
*iface
, BSTR
*pstrDisplayUri
)
3660 TRACE("(%p)->(%p)\n", iface
, pstrDisplayUri
);
3661 return Uri_GetPropertyBSTR(iface
, Uri_PROPERTY_DISPLAY_URI
, pstrDisplayUri
, 0);
3664 static HRESULT WINAPI
Uri_GetDomain(IUri
*iface
, BSTR
*pstrDomain
)
3666 TRACE("(%p)->(%p)\n", iface
, pstrDomain
);
3667 return Uri_GetPropertyBSTR(iface
, Uri_PROPERTY_DOMAIN
, pstrDomain
, 0);
3670 static HRESULT WINAPI
Uri_GetExtension(IUri
*iface
, BSTR
*pstrExtension
)
3672 TRACE("(%p)->(%p)\n", iface
, pstrExtension
);
3673 return Uri_GetPropertyBSTR(iface
, Uri_PROPERTY_EXTENSION
, pstrExtension
, 0);
3676 static HRESULT WINAPI
Uri_GetFragment(IUri
*iface
, BSTR
*pstrFragment
)
3678 TRACE("(%p)->(%p)\n", iface
, pstrFragment
);
3679 return Uri_GetPropertyBSTR(iface
, Uri_PROPERTY_FRAGMENT
, pstrFragment
, 0);
3682 static HRESULT WINAPI
Uri_GetHost(IUri
*iface
, BSTR
*pstrHost
)
3684 TRACE("(%p)->(%p)\n", iface
, pstrHost
);
3685 return Uri_GetPropertyBSTR(iface
, Uri_PROPERTY_HOST
, pstrHost
, 0);
3688 static HRESULT WINAPI
Uri_GetPassword(IUri
*iface
, BSTR
*pstrPassword
)
3690 TRACE("(%p)->(%p)\n", iface
, pstrPassword
);
3691 return Uri_GetPropertyBSTR(iface
, Uri_PROPERTY_PASSWORD
, pstrPassword
, 0);
3694 static HRESULT WINAPI
Uri_GetPath(IUri
*iface
, BSTR
*pstrPath
)
3696 TRACE("(%p)->(%p)\n", iface
, pstrPath
);
3697 return Uri_GetPropertyBSTR(iface
, Uri_PROPERTY_PATH
, pstrPath
, 0);
3700 static HRESULT WINAPI
Uri_GetPathAndQuery(IUri
*iface
, BSTR
*pstrPathAndQuery
)
3702 TRACE("(%p)->(%p)\n", iface
, pstrPathAndQuery
);
3703 return Uri_GetPropertyBSTR(iface
, Uri_PROPERTY_PATH_AND_QUERY
, pstrPathAndQuery
, 0);
3706 static HRESULT WINAPI
Uri_GetQuery(IUri
*iface
, BSTR
*pstrQuery
)
3708 TRACE("(%p)->(%p)\n", iface
, pstrQuery
);
3709 return Uri_GetPropertyBSTR(iface
, Uri_PROPERTY_QUERY
, pstrQuery
, 0);
3712 static HRESULT WINAPI
Uri_GetRawUri(IUri
*iface
, BSTR
*pstrRawUri
)
3714 Uri
*This
= URI_THIS(iface
);
3715 TRACE("(%p)->(%p)\n", This
, pstrRawUri
);
3717 /* Just forward the call to GetPropertyBSTR. */
3718 return Uri_GetPropertyBSTR(iface
, Uri_PROPERTY_RAW_URI
, pstrRawUri
, 0);
3721 static HRESULT WINAPI
Uri_GetSchemeName(IUri
*iface
, BSTR
*pstrSchemeName
)
3723 Uri
*This
= URI_THIS(iface
);
3724 TRACE("(%p)->(%p)\n", This
, pstrSchemeName
);
3725 return Uri_GetPropertyBSTR(iface
, Uri_PROPERTY_SCHEME_NAME
, pstrSchemeName
, 0);
3728 static HRESULT WINAPI
Uri_GetUserInfo(IUri
*iface
, BSTR
*pstrUserInfo
)
3730 TRACE("(%p)->(%p)\n", iface
, pstrUserInfo
);
3731 return Uri_GetPropertyBSTR(iface
, Uri_PROPERTY_USER_INFO
, pstrUserInfo
, 0);
3734 static HRESULT WINAPI
Uri_GetUserName(IUri
*iface
, BSTR
*pstrUserName
)
3736 TRACE("(%p)->(%p)\n", iface
, pstrUserName
);
3737 return Uri_GetPropertyBSTR(iface
, Uri_PROPERTY_USER_NAME
, pstrUserName
, 0);
3740 static HRESULT WINAPI
Uri_GetHostType(IUri
*iface
, DWORD
*pdwHostType
)
3742 TRACE("(%p)->(%p)\n", iface
, pdwHostType
);
3743 return Uri_GetPropertyDWORD(iface
, Uri_PROPERTY_HOST_TYPE
, pdwHostType
, 0);
3746 static HRESULT WINAPI
Uri_GetPort(IUri
*iface
, DWORD
*pdwPort
)
3748 TRACE("(%p)->(%p)\n", iface
, pdwPort
);
3749 return Uri_GetPropertyDWORD(iface
, Uri_PROPERTY_PORT
, pdwPort
, 0);
3752 static HRESULT WINAPI
Uri_GetScheme(IUri
*iface
, DWORD
*pdwScheme
)
3754 Uri
*This
= URI_THIS(iface
);
3755 TRACE("(%p)->(%p)\n", This
, pdwScheme
);
3756 return Uri_GetPropertyDWORD(iface
, Uri_PROPERTY_SCHEME
, pdwScheme
, 0);
3759 static HRESULT WINAPI
Uri_GetZone(IUri
*iface
, DWORD
*pdwZone
)
3761 TRACE("(%p)->(%p)\n", iface
, pdwZone
);
3762 return Uri_GetPropertyDWORD(iface
, Uri_PROPERTY_ZONE
,pdwZone
, 0);
3765 static HRESULT WINAPI
Uri_GetProperties(IUri
*iface
, DWORD
*pdwProperties
)
3767 Uri
*This
= URI_THIS(iface
);
3768 TRACE("(%p)->(%p)\n", This
, pdwProperties
);
3771 return E_INVALIDARG
;
3773 /* All URIs have these. */
3774 *pdwProperties
= Uri_HAS_ABSOLUTE_URI
|Uri_HAS_DISPLAY_URI
|Uri_HAS_RAW_URI
|
3775 Uri_HAS_SCHEME
|Uri_HAS_HOST_TYPE
;
3777 if(This
->scheme_start
> -1)
3778 *pdwProperties
|= Uri_HAS_SCHEME_NAME
;
3780 if(This
->authority_start
> -1) {
3781 *pdwProperties
|= Uri_HAS_AUTHORITY
;
3782 if(This
->userinfo_start
> -1)
3783 *pdwProperties
|= Uri_HAS_USER_INFO
|Uri_HAS_USER_NAME
;
3784 if(This
->userinfo_split
> -1)
3785 *pdwProperties
|= Uri_HAS_PASSWORD
;
3786 if(This
->host_start
> -1)
3787 *pdwProperties
|= Uri_HAS_HOST
;
3788 if(This
->domain_offset
> -1)
3789 *pdwProperties
|= Uri_HAS_DOMAIN
;
3791 *pdwProperties
|= Uri_HAS_PORT
;
3794 if(This
->path_start
> -1)
3795 *pdwProperties
|= Uri_HAS_PATH
|Uri_HAS_PATH_AND_QUERY
;
3796 if(This
->query_start
> -1)
3797 *pdwProperties
|= Uri_HAS_QUERY
|Uri_HAS_PATH_AND_QUERY
;
3799 if(This
->extension_offset
> -1)
3800 *pdwProperties
|= Uri_HAS_EXTENSION
;
3802 if(This
->fragment_start
> -1)
3803 *pdwProperties
|= Uri_HAS_FRAGMENT
;
3808 static HRESULT WINAPI
Uri_IsEqual(IUri
*iface
, IUri
*pUri
, BOOL
*pfEqual
)
3810 Uri
*This
= URI_THIS(iface
);
3813 TRACE("(%p)->(%p %p)\n", This
, pUri
, pfEqual
);
3821 /* For some reason Windows returns S_OK here... */
3825 /* Try to convert it to a Uri (allows for a more simple comparison). */
3826 if((other
= get_uri_obj(pUri
)))
3827 *pfEqual
= are_equal_simple(This
, other
);
3829 /* Do it the hard way. */
3830 FIXME("(%p)->(%p %p) No support for unknown IUri's yet.\n", iface
, pUri
, pfEqual
);
3839 static const IUriVtbl UriVtbl
= {
3843 Uri_GetPropertyBSTR
,
3844 Uri_GetPropertyLength
,
3845 Uri_GetPropertyDWORD
,
3856 Uri_GetPathAndQuery
,
3870 /***********************************************************************
3871 * CreateUri (urlmon.@)
3873 * Creates a new IUri object using the URI represented by pwzURI. This function
3874 * parses and validates the components of pwzURI and then canonicalizes the
3875 * parsed components.
3878 * pwzURI [I] The URI to parse, validate, and canonicalize.
3879 * dwFlags [I] Flags which can affect how the parsing/canonicalization is performed.
3880 * dwReserved [I] Reserved (not used).
3881 * ppURI [O] The resulting IUri after parsing/canonicalization occurs.
3884 * Success: Returns S_OK. ppURI contains the pointer to the newly allocated IUri.
3885 * Failure: E_INVALIDARG if there's invalid flag combinations in dwFlags, or an
3886 * invalid parameters, or pwzURI doesn't represnt a valid URI.
3887 * E_OUTOFMEMORY if any memory allocation fails.
3891 * Uri_CREATE_CANONICALIZE, Uri_CREATE_DECODE_EXTRA_INFO, Uri_CREATE_CRACK_UNKNOWN_SCHEMES,
3892 * Uri_CREATE_PRE_PROCESS_HTML_URI, Uri_CREATE_NO_IE_SETTINGS.
3894 HRESULT WINAPI
CreateUri(LPCWSTR pwzURI
, DWORD dwFlags
, DWORD_PTR dwReserved
, IUri
**ppURI
)
3896 const DWORD supported_flags
= Uri_CREATE_ALLOW_RELATIVE
|Uri_CREATE_ALLOW_IMPLICIT_WILDCARD_SCHEME
|
3897 Uri_CREATE_ALLOW_IMPLICIT_FILE_SCHEME
|Uri_CREATE_NO_CANONICALIZE
|Uri_CREATE_CANONICALIZE
|
3898 Uri_CREATE_DECODE_EXTRA_INFO
|Uri_CREATE_NO_DECODE_EXTRA_INFO
|Uri_CREATE_CRACK_UNKNOWN_SCHEMES
|
3899 Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES
|Uri_CREATE_PRE_PROCESS_HTML_URI
|Uri_CREATE_NO_PRE_PROCESS_HTML_URI
|
3900 Uri_CREATE_NO_IE_SETTINGS
|Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS
;
3905 TRACE("(%s %x %x %p)\n", debugstr_w(pwzURI
), dwFlags
, (DWORD
)dwReserved
, ppURI
);
3908 return E_INVALIDARG
;
3912 return E_INVALIDARG
;
3915 /* Check for invalid flags. */
3916 if((dwFlags
& Uri_CREATE_DECODE_EXTRA_INFO
&& dwFlags
& Uri_CREATE_NO_DECODE_EXTRA_INFO
) ||
3917 (dwFlags
& Uri_CREATE_CANONICALIZE
&& dwFlags
& Uri_CREATE_NO_CANONICALIZE
) ||
3918 (dwFlags
& Uri_CREATE_CRACK_UNKNOWN_SCHEMES
&& dwFlags
& Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES
) ||
3919 (dwFlags
& Uri_CREATE_PRE_PROCESS_HTML_URI
&& dwFlags
& Uri_CREATE_NO_PRE_PROCESS_HTML_URI
) ||
3920 (dwFlags
& Uri_CREATE_IE_SETTINGS
&& dwFlags
& Uri_CREATE_NO_IE_SETTINGS
)) {
3922 return E_INVALIDARG
;
3925 /* Currently unsupported. */
3926 if(dwFlags
& ~supported_flags
)
3927 FIXME("Ignoring unsupported flags %x\n", dwFlags
& ~supported_flags
);
3929 ret
= heap_alloc(sizeof(Uri
));
3931 return E_OUTOFMEMORY
;
3933 ret
->lpIUriVtbl
= &UriVtbl
;
3936 /* Pre process the URI, unless told otherwise. */
3937 if(!(dwFlags
& Uri_CREATE_NO_PRE_PROCESS_HTML_URI
))
3938 ret
->raw_uri
= pre_process_uri(pwzURI
);
3940 ret
->raw_uri
= SysAllocString(pwzURI
);
3944 return E_OUTOFMEMORY
;
3947 memset(&data
, 0, sizeof(parse_data
));
3948 data
.uri
= ret
->raw_uri
;
3950 /* Validate and parse the URI into it's components. */
3951 if(!parse_uri(&data
, dwFlags
)) {
3952 /* Encountered an unsupported or invalid URI */
3953 SysFreeString(ret
->raw_uri
);
3956 return E_INVALIDARG
;
3959 /* Canonicalize the URI. */
3960 hr
= canonicalize_uri(&data
, ret
, dwFlags
);
3962 SysFreeString(ret
->raw_uri
);
3972 /***********************************************************************
3973 * CreateUriWithFragment (urlmon.@)
3975 * Creates a new IUri object. This is almost the same as CreateUri, expect that
3976 * it allows you to explicitly specify a fragment (pwzFragment) for pwzURI.
3979 * pwzURI [I] The URI to parse and perform canonicalization on.
3980 * pwzFragment [I] The explict fragment string which should be added to pwzURI.
3981 * dwFlags [I] The flags which will be passed to CreateUri.
3982 * dwReserved [I] Reserved (not used).
3983 * ppURI [O] The resulting IUri after parsing/canonicalization.
3986 * Success: S_OK. ppURI contains the pointer to the newly allocated IUri.
3987 * Failure: E_INVALIDARG if pwzURI already contains a fragment and pwzFragment
3988 * isn't NULL. Will also return E_INVALIDARG for the same reasons as
3989 * CreateUri will. E_OUTOFMEMORY if any allocations fail.
3991 HRESULT WINAPI
CreateUriWithFragment(LPCWSTR pwzURI
, LPCWSTR pwzFragment
, DWORD dwFlags
,
3992 DWORD_PTR dwReserved
, IUri
**ppURI
)
3995 TRACE("(%s %s %x %x %p)\n", debugstr_w(pwzURI
), debugstr_w(pwzFragment
), dwFlags
, (DWORD
)dwReserved
, ppURI
);
3998 return E_INVALIDARG
;
4002 return E_INVALIDARG
;
4005 /* Check if a fragment should be appended to the URI string. */
4008 DWORD uri_len
, frag_len
;
4011 /* Check if the original URI already has a fragment component. */
4012 if(StrChrW(pwzURI
, '#')) {
4014 return E_INVALIDARG
;
4017 uri_len
= lstrlenW(pwzURI
);
4018 frag_len
= lstrlenW(pwzFragment
);
4020 /* If the fragment doesn't start with a '#', one will be added. */
4021 add_pound
= *pwzFragment
!= '#';
4024 uriW
= heap_alloc((uri_len
+frag_len
+2)*sizeof(WCHAR
));
4026 uriW
= heap_alloc((uri_len
+frag_len
+1)*sizeof(WCHAR
));
4029 return E_OUTOFMEMORY
;
4031 memcpy(uriW
, pwzURI
, uri_len
*sizeof(WCHAR
));
4033 uriW
[uri_len
++] = '#';
4034 memcpy(uriW
+uri_len
, pwzFragment
, (frag_len
+1)*sizeof(WCHAR
));
4036 hres
= CreateUri(uriW
, dwFlags
, 0, ppURI
);
4040 /* A fragment string wasn't specified, so just forward the call. */
4041 hres
= CreateUri(pwzURI
, dwFlags
, 0, ppURI
);
4046 #define URIBUILDER_THIS(iface) DEFINE_THIS(UriBuilder, IUriBuilder, iface)
4048 static HRESULT WINAPI
UriBuilder_QueryInterface(IUriBuilder
*iface
, REFIID riid
, void **ppv
)
4050 UriBuilder
*This
= URIBUILDER_THIS(iface
);
4052 if(IsEqualGUID(&IID_IUnknown
, riid
)) {
4053 TRACE("(%p)->(IID_IUnknown %p)\n", This
, ppv
);
4054 *ppv
= URIBUILDER(This
);
4055 }else if(IsEqualGUID(&IID_IUriBuilder
, riid
)) {
4056 TRACE("(%p)->(IID_IUri %p)\n", This
, ppv
);
4057 *ppv
= URIBUILDER(This
);
4059 TRACE("(%p)->(%s %p)\n", This
, debugstr_guid(riid
), ppv
);
4061 return E_NOINTERFACE
;
4064 IUnknown_AddRef((IUnknown
*)*ppv
);
4068 static ULONG WINAPI
UriBuilder_AddRef(IUriBuilder
*iface
)
4070 UriBuilder
*This
= URIBUILDER_THIS(iface
);
4071 LONG ref
= InterlockedIncrement(&This
->ref
);
4073 TRACE("(%p) ref=%d\n", This
, ref
);
4078 static ULONG WINAPI
UriBuilder_Release(IUriBuilder
*iface
)
4080 UriBuilder
*This
= URIBUILDER_THIS(iface
);
4081 LONG ref
= InterlockedDecrement(&This
->ref
);
4083 TRACE("(%p) ref=%d\n", This
, ref
);
4086 if(This
->uri
) IUri_Release(This
->uri
);
4093 static HRESULT WINAPI
UriBuilder_CreateUriSimple(IUriBuilder
*iface
,
4094 DWORD dwAllowEncodingPropertyMask
,
4095 DWORD_PTR dwReserved
,
4098 UriBuilder
*This
= URIBUILDER_THIS(iface
);
4099 TRACE("(%p)->(%d %d %p)\n", This
, dwAllowEncodingPropertyMask
, (DWORD
)dwReserved
, ppIUri
);
4104 /* Acts the same way as CreateUri. */
4105 if(dwAllowEncodingPropertyMask
&& !This
->uri
) {
4112 return INET_E_INVALID_URL
;
4115 FIXME("(%p)->(%d %d %p)\n", This
, dwAllowEncodingPropertyMask
, (DWORD
)dwReserved
, ppIUri
);
4119 static HRESULT WINAPI
UriBuilder_CreateUri(IUriBuilder
*iface
,
4120 DWORD dwCreateFlags
,
4121 DWORD dwAllowEncodingPropertyMask
,
4122 DWORD_PTR dwReserved
,
4125 UriBuilder
*This
= URIBUILDER_THIS(iface
);
4126 TRACE("(%p)->(0x%08x %d %d %p)\n", This
, dwCreateFlags
, dwAllowEncodingPropertyMask
, (DWORD
)dwReserved
, ppIUri
);
4131 /* The only time it doesn't return E_NOTIMPL when the dwAllow parameter
4132 * has flags set, is when the IUriBuilder has a IUri set and it hasn't
4133 * been modified (a call to a "Set*" hasn't been performed).
4135 * TODO: Check if the IUriBuilder's properties have been modified.
4137 if(dwAllowEncodingPropertyMask
&& !This
->uri
) {
4144 return INET_E_INVALID_URL
;
4147 FIXME("(%p)->(0x%08x %d %d %p)\n", This
, dwCreateFlags
, dwAllowEncodingPropertyMask
, (DWORD
)dwReserved
, ppIUri
);
4151 static HRESULT WINAPI
UriBuilder_CreateUriWithFlags(IUriBuilder
*iface
,
4152 DWORD dwCreateFlags
,
4153 DWORD dwUriBuilderFlags
,
4154 DWORD dwAllowEncodingPropertyMask
,
4155 DWORD_PTR dwReserved
,
4158 UriBuilder
*This
= URIBUILDER_THIS(iface
);
4159 FIXME("(%p)->(0x%08x 0x%08x %d %d %p)\n", This
, dwCreateFlags
, dwUriBuilderFlags
,
4160 dwAllowEncodingPropertyMask
, (DWORD
)dwReserved
, ppIUri
);
4164 static HRESULT WINAPI
UriBuilder_GetIUri(IUriBuilder
*iface
, IUri
**ppIUri
)
4166 UriBuilder
*This
= URIBUILDER_THIS(iface
);
4167 FIXME("(%p)->(%p)\n", This
, ppIUri
);
4171 static HRESULT WINAPI
UriBuilder_SetIUri(IUriBuilder
*iface
, IUri
*pIUri
)
4173 UriBuilder
*This
= URIBUILDER_THIS(iface
);
4174 FIXME("(%p)->(%p)\n", This
, pIUri
);
4178 static HRESULT WINAPI
UriBuilder_GetFragment(IUriBuilder
*iface
, DWORD
*pcchFragment
, LPCWSTR
*ppwzFragment
)
4180 UriBuilder
*This
= URIBUILDER_THIS(iface
);
4181 FIXME("(%p)->(%p %p)\n", This
, pcchFragment
, ppwzFragment
);
4185 static HRESULT WINAPI
UriBuilder_GetHost(IUriBuilder
*iface
, DWORD
*pcchHost
, LPCWSTR
*ppwzHost
)
4187 UriBuilder
*This
= URIBUILDER_THIS(iface
);
4188 FIXME("(%p)->(%p %p)\n", This
, pcchHost
, ppwzHost
);
4192 static HRESULT WINAPI
UriBuilder_GetPassword(IUriBuilder
*iface
, DWORD
*pcchPassword
, LPCWSTR
*ppwzPassword
)
4194 UriBuilder
*This
= URIBUILDER_THIS(iface
);
4195 FIXME("(%p)->(%p %p)\n", This
, pcchPassword
, ppwzPassword
);
4199 static HRESULT WINAPI
UriBuilder_GetPath(IUriBuilder
*iface
, DWORD
*pcchPath
, LPCWSTR
*ppwzPath
)
4201 UriBuilder
*This
= URIBUILDER_THIS(iface
);
4202 FIXME("(%p)->(%p %p)\n", This
, pcchPath
, ppwzPath
);
4206 static HRESULT WINAPI
UriBuilder_GetPort(IUriBuilder
*iface
, BOOL
*pfHasPort
, DWORD
*pdwPort
)
4208 UriBuilder
*This
= URIBUILDER_THIS(iface
);
4209 FIXME("(%p)->(%p %p)\n", This
, pfHasPort
, pdwPort
);
4213 static HRESULT WINAPI
UriBuilder_GetQuery(IUriBuilder
*iface
, DWORD
*pcchQuery
, LPCWSTR
*ppwzQuery
)
4215 UriBuilder
*This
= URIBUILDER_THIS(iface
);
4216 FIXME("(%p)->(%p %p)\n", This
, pcchQuery
, ppwzQuery
);
4220 static HRESULT WINAPI
UriBuilder_GetSchemeName(IUriBuilder
*iface
, DWORD
*pcchSchemeName
, LPCWSTR
*ppwzSchemeName
)
4222 UriBuilder
*This
= URIBUILDER_THIS(iface
);
4223 FIXME("(%p)->(%p %p)\n", This
, pcchSchemeName
, ppwzSchemeName
);
4227 static HRESULT WINAPI
UriBuilder_GetUserName(IUriBuilder
*iface
, DWORD
*pcchUserName
, LPCWSTR
*ppwzUserName
)
4229 UriBuilder
*This
= URIBUILDER_THIS(iface
);
4230 FIXME("(%p)->(%p %p)\n", This
, pcchUserName
, ppwzUserName
);
4234 static HRESULT WINAPI
UriBuilder_SetFragment(IUriBuilder
*iface
, LPCWSTR pwzNewValue
)
4236 UriBuilder
*This
= URIBUILDER_THIS(iface
);
4237 FIXME("(%p)->(%s)\n", This
, debugstr_w(pwzNewValue
));
4241 static HRESULT WINAPI
UriBuilder_SetHost(IUriBuilder
*iface
, LPCWSTR pwzNewValue
)
4243 UriBuilder
*This
= URIBUILDER_THIS(iface
);
4244 FIXME("(%p)->(%s)\n", This
, debugstr_w(pwzNewValue
));
4248 static HRESULT WINAPI
UriBuilder_SetPassword(IUriBuilder
*iface
, LPCWSTR pwzNewValue
)
4250 UriBuilder
*This
= URIBUILDER_THIS(iface
);
4251 FIXME("(%p)->(%s)\n", This
, debugstr_w(pwzNewValue
));
4255 static HRESULT WINAPI
UriBuilder_SetPath(IUriBuilder
*iface
, LPCWSTR pwzNewValue
)
4257 UriBuilder
*This
= URIBUILDER_THIS(iface
);
4258 FIXME("(%p)->(%s)\n", This
, debugstr_w(pwzNewValue
));
4262 static HRESULT WINAPI
UriBuilder_SetPort(IUriBuilder
*iface
, BOOL fHasPort
, DWORD dwNewValue
)
4264 UriBuilder
*This
= URIBUILDER_THIS(iface
);
4265 FIXME("(%p)->(%d %d)\n", This
, fHasPort
, dwNewValue
);
4269 static HRESULT WINAPI
UriBuilder_SetQuery(IUriBuilder
*iface
, LPCWSTR pwzNewValue
)
4271 UriBuilder
*This
= URIBUILDER_THIS(iface
);
4272 FIXME("(%p)->(%s)\n", This
, debugstr_w(pwzNewValue
));
4276 static HRESULT WINAPI
UriBuilder_SetSchemeName(IUriBuilder
*iface
, LPCWSTR pwzNewValue
)
4278 UriBuilder
*This
= URIBUILDER_THIS(iface
);
4279 FIXME("(%p)->(%s)\n", This
, debugstr_w(pwzNewValue
));
4283 static HRESULT WINAPI
UriBuilder_SetUserName(IUriBuilder
*iface
, LPCWSTR pwzNewValue
)
4285 UriBuilder
*This
= URIBUILDER_THIS(iface
);
4286 FIXME("(%p)->(%s)\n", This
, debugstr_w(pwzNewValue
));
4290 static HRESULT WINAPI
UriBuilder_RemoveProperties(IUriBuilder
*iface
, DWORD dwPropertyMask
)
4292 UriBuilder
*This
= URIBUILDER_THIS(iface
);
4293 FIXME("(%p)->(0x%08x)\n", This
, dwPropertyMask
);
4297 static HRESULT WINAPI
UriBuilder_HasBeenModified(IUriBuilder
*iface
, BOOL
*pfModified
)
4299 UriBuilder
*This
= URIBUILDER_THIS(iface
);
4300 FIXME("(%p)->(%p)\n", This
, pfModified
);
4304 #undef URIBUILDER_THIS
4306 static const IUriBuilderVtbl UriBuilderVtbl
= {
4307 UriBuilder_QueryInterface
,
4310 UriBuilder_CreateUriSimple
,
4311 UriBuilder_CreateUri
,
4312 UriBuilder_CreateUriWithFlags
,
4315 UriBuilder_GetFragment
,
4317 UriBuilder_GetPassword
,
4320 UriBuilder_GetQuery
,
4321 UriBuilder_GetSchemeName
,
4322 UriBuilder_GetUserName
,
4323 UriBuilder_SetFragment
,
4325 UriBuilder_SetPassword
,
4328 UriBuilder_SetQuery
,
4329 UriBuilder_SetSchemeName
,
4330 UriBuilder_SetUserName
,
4331 UriBuilder_RemoveProperties
,
4332 UriBuilder_HasBeenModified
,
4335 /***********************************************************************
4336 * CreateIUriBuilder (urlmon.@)
4338 HRESULT WINAPI
CreateIUriBuilder(IUri
*pIUri
, DWORD dwFlags
, DWORD_PTR dwReserved
, IUriBuilder
**ppIUriBuilder
)
4342 TRACE("(%p %x %x %p)\n", pIUri
, dwFlags
, (DWORD
)dwReserved
, ppIUriBuilder
);
4347 ret
= heap_alloc(sizeof(UriBuilder
));
4349 return E_OUTOFMEMORY
;
4351 ret
->lpIUriBuilderVtbl
= &UriBuilderVtbl
;
4358 *ppIUriBuilder
= URIBUILDER(ret
);