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. */
43 BOOL display_absolute;
47 URL_SCHEME scheme_type;
55 Uri_HOST_TYPE host_type;
77 const IUriBuilderVtbl *lpIUriBuilderVtbl;
102 /* IPv6 addresses can hold up to 8 h16 components. */
106 /* An IPv6 can have 1 elision ("::"). */
107 const WCHAR *elision;
109 /* An IPv6 can contain 1 IPv4 address as the last 32bits of the address. */
122 BOOL has_implicit_scheme;
123 BOOL has_implicit_ip;
128 URL_SCHEME scheme_type;
130 const WCHAR *userinfo;
136 Uri_HOST_TYPE host_type;
139 ipv6_address ipv6_address;
151 const WCHAR *fragment;
155 static const CHAR hexDigits[] = "0123456789ABCDEF";
157 /* List of scheme types/scheme names that are recognized by the IUri interface as of IE 7. */
158 static const struct {
160 WCHAR scheme_name[16];
161 } recognized_schemes[] = {
162 {URL_SCHEME_FTP, {'f','t','p',0}},
163 {URL_SCHEME_HTTP, {'h','t','t','p',0}},
164 {URL_SCHEME_GOPHER, {'g','o','p','h','e','r',0}},
165 {URL_SCHEME_MAILTO, {'m','a','i','l','t','o',0}},
166 {URL_SCHEME_NEWS, {'n','e','w','s',0}},
167 {URL_SCHEME_NNTP, {'n','n','t','p',0}},
168 {URL_SCHEME_TELNET, {'t','e','l','n','e','t',0}},
169 {URL_SCHEME_WAIS, {'w','a','i','s',0}},
170 {URL_SCHEME_FILE, {'f','i','l','e',0}},
171 {URL_SCHEME_MK, {'m','k',0}},
172 {URL_SCHEME_HTTPS, {'h','t','t','p','s',0}},
173 {URL_SCHEME_SHELL, {'s','h','e','l','l',0}},
174 {URL_SCHEME_SNEWS, {'s','n','e','w','s',0}},
175 {URL_SCHEME_LOCAL, {'l','o','c','a','l',0}},
176 {URL_SCHEME_JAVASCRIPT, {'j','a','v','a','s','c','r','i','p','t',0}},
177 {URL_SCHEME_VBSCRIPT, {'v','b','s','c','r','i','p','t',0}},
178 {URL_SCHEME_ABOUT, {'a','b','o','u','t',0}},
179 {URL_SCHEME_RES, {'r','e','s',0}},
180 {URL_SCHEME_MSSHELLROOTED, {'m','s','-','s','h','e','l','l','-','r','o','o','t','e','d',0}},
181 {URL_SCHEME_MSSHELLIDLIST, {'m','s','-','s','h','e','l','l','-','i','d','l','i','s','t',0}},
182 {URL_SCHEME_MSHELP, {'h','c','p',0}},
183 {URL_SCHEME_WILDCARD, {'*',0}}
186 /* List of default ports Windows recognizes. */
187 static const struct {
190 } default_ports[] = {
191 {URL_SCHEME_FTP, 21},
192 {URL_SCHEME_HTTP, 80},
193 {URL_SCHEME_GOPHER, 70},
194 {URL_SCHEME_NNTP, 119},
195 {URL_SCHEME_TELNET, 23},
196 {URL_SCHEME_WAIS, 210},
197 {URL_SCHEME_HTTPS, 443},
200 /* List of 3 character top level domain names Windows seems to recognize.
201 * There might be more, but, these are the only ones I've found so far.
203 static const struct {
205 } recognized_tlds[] = {
215 static Uri *get_uri_obj(IUri *uri)
220 hres = IUri_QueryInterface(uri, &IID_IUriObj, (void**)&ret);
221 return SUCCEEDED(hres) ? ret : NULL;
224 static inline BOOL is_alpha(WCHAR val) {
225 return ((val >= 'a' && val <= 'z') || (val >= 'A' && val <= 'Z'));
228 static inline BOOL is_num(WCHAR val) {
229 return (val >= '0' && val <= '9');
232 static inline BOOL is_drive_path(const WCHAR *str) {
233 return (is_alpha(str[0]) && (str[1] == ':' || str[1] == '|'));
236 static inline BOOL is_unc_path(const WCHAR *str) {
237 return (str[0] == '\\' && str[0] == '\\');
240 static inline BOOL is_forbidden_dos_path_char(WCHAR val) {
241 return (val == '>' || val == '<' || val == '\"');
244 /* A URI is implicitly a file path if it begins with
245 * a drive letter (eg X:) or starts with "\\" (UNC path).
247 static inline BOOL is_implicit_file_path(const WCHAR *str) {
248 return (is_unc_path(str) || (is_alpha(str[0]) && str[1] == ':'));
251 /* Checks if the URI is a hierarchical URI. A hierarchical
252 * URI is one that has "//" after the scheme.
254 static BOOL check_hierarchical(const WCHAR **ptr) {
255 const WCHAR *start = *ptr;
270 /* unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~" */
271 static inline BOOL is_unreserved(WCHAR val) {
272 return (is_alpha(val) || is_num(val) || val == '-' || val == '.' ||
273 val == '_' || val == '~');
276 /* sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
277 * / "*" / "+" / "," / ";" / "="
279 static inline BOOL is_subdelim(WCHAR val) {
280 return (val == '!' || val == '$' || val == '&' ||
281 val == '\'' || val == '(' || val == ')' ||
282 val == '*' || val == '+' || val == ',' ||
283 val == ';' || val == '=');
286 /* gen-delims = ":" / "/" / "?" / "#" / "[" / "]" / "@" */
287 static inline BOOL is_gendelim(WCHAR val) {
288 return (val == ':' || val == '/' || val == '?' ||
289 val == '#' || val == '[' || val == ']' ||
293 /* Characters that delimit the end of the authority
294 * section of a URI. Sometimes a '\\' is considered
295 * an authority delimeter.
297 static inline BOOL is_auth_delim(WCHAR val, BOOL acceptSlash) {
298 return (val == '#' || val == '/' || val == '?' ||
299 val == '\0' || (acceptSlash && val == '\\'));
302 /* reserved = gen-delims / sub-delims */
303 static inline BOOL is_reserved(WCHAR val) {
304 return (is_subdelim(val) || is_gendelim(val));
307 static inline BOOL is_hexdigit(WCHAR val) {
308 return ((val >= 'a' && val <= 'f') ||
309 (val >= 'A' && val <= 'F') ||
310 (val >= '0' && val <= '9'));
313 static inline BOOL is_path_delim(WCHAR val) {
314 return (!val || val == '#' || val == '?');
317 /* List of schemes types Windows seems to expect to be hierarchical. */
318 static inline BOOL is_hierarchical_scheme(URL_SCHEME type) {
319 return(type == URL_SCHEME_HTTP || type == URL_SCHEME_FTP ||
320 type == URL_SCHEME_GOPHER || type == URL_SCHEME_NNTP ||
321 type == URL_SCHEME_TELNET || type == URL_SCHEME_WAIS ||
322 type == URL_SCHEME_FILE || type == URL_SCHEME_HTTPS ||
323 type == URL_SCHEME_RES);
326 /* Determines if the URI is hierarchical using the information already parsed into
327 * data and using the current location of parsing in the URI string.
329 * Windows considers a URI hierarchical if on of the following is true:
330 * A.) It's a wildcard scheme.
331 * B.) It's an implicit file scheme.
332 * C.) It's a known hierarchical scheme and it has two '\\' after the scheme name.
333 * (the '\\' will be converted into "//" during canonicalization).
334 * D.) It's not a relative URI and "//" appears after the scheme name.
336 static inline BOOL is_hierarchical_uri(const WCHAR **ptr, const parse_data *data) {
337 const WCHAR *start = *ptr;
339 if(data->scheme_type == URL_SCHEME_WILDCARD)
341 else if(data->scheme_type == URL_SCHEME_FILE && data->has_implicit_scheme)
343 else if(is_hierarchical_scheme(data->scheme_type) && (*ptr)[0] == '\\' && (*ptr)[1] == '\\') {
346 } else if(!data->is_relative && check_hierarchical(ptr))
353 /* Checks if the two Uri's are logically equivalent. It's a simple
354 * comparison, since they are both of type Uri, and it can access
355 * the properties of each Uri directly without the need to go
356 * through the "IUri_Get*" interface calls.
358 static BOOL are_equal_simple(const Uri *a, const Uri *b) {
359 if(a->scheme_type == b->scheme_type) {
360 const BOOL known_scheme = a->scheme_type != URL_SCHEME_UNKNOWN;
361 const BOOL are_hierarchical =
362 (a->authority_start > -1 && b->authority_start > -1);
364 if(a->scheme_type == URL_SCHEME_FILE) {
365 if(a->canon_len == b->canon_len)
366 return !StrCmpIW(a->canon_uri, b->canon_uri);
369 /* Only compare the scheme names (if any) if their unknown scheme types. */
371 if((a->scheme_start > -1 && b->scheme_start > -1) &&
372 (a->scheme_len == b->scheme_len)) {
373 /* Make sure the schemes are the same. */
374 if(StrCmpNW(a->canon_uri+a->scheme_start, b->canon_uri+b->scheme_start, a->scheme_len))
376 } else if(a->scheme_len != b->scheme_len)
377 /* One of the Uri's has a scheme name, while the other doesn't. */
381 /* If they have a userinfo component, perform case sensitive compare. */
382 if((a->userinfo_start > -1 && b->userinfo_start > -1) &&
383 (a->userinfo_len == b->userinfo_len)) {
384 if(StrCmpNW(a->canon_uri+a->userinfo_start, b->canon_uri+b->userinfo_start, a->userinfo_len))
386 } else if(a->userinfo_len != b->userinfo_len)
387 /* One of the Uri's had a userinfo, while the other one doesn't. */
390 /* Check if they have a host name. */
391 if((a->host_start > -1 && b->host_start > -1) &&
392 (a->host_len == b->host_len)) {
393 /* Perform a case insensitive compare if they are a known scheme type. */
395 if(StrCmpNIW(a->canon_uri+a->host_start, b->canon_uri+b->host_start, a->host_len))
397 } else if(StrCmpNW(a->canon_uri+a->host_start, b->canon_uri+b->host_start, a->host_len))
399 } else if(a->host_len != b->host_len)
400 /* One of the Uri's had a host, while the other one didn't. */
403 if(a->has_port && b->has_port) {
404 if(a->port != b->port)
406 } else if(a->has_port || b->has_port)
407 /* One had a port, while the other one didn't. */
410 /* Windows is weird with how it handles paths. For example
411 * One URI could be "http://google.com" (after canonicalization)
412 * and one could be "http://google.com/" and the IsEqual function
413 * would still evaluate to TRUE, but, only if they are both hierarchical
416 if((a->path_start > -1 && b->path_start > -1) &&
417 (a->path_len == b->path_len)) {
418 if(StrCmpNW(a->canon_uri+a->path_start, b->canon_uri+b->path_start, a->path_len))
420 } else if(are_hierarchical && a->path_len == -1 && b->path_len == 0) {
421 if(*(a->canon_uri+a->path_start) != '/')
423 } else if(are_hierarchical && b->path_len == 1 && a->path_len == 0) {
424 if(*(b->canon_uri+b->path_start) != '/')
426 } else if(a->path_len != b->path_len)
429 /* Compare the query strings of the two URIs. */
430 if((a->query_start > -1 && b->query_start > -1) &&
431 (a->query_len == b->query_len)) {
432 if(StrCmpNW(a->canon_uri+a->query_start, b->canon_uri+b->query_start, a->query_len))
434 } else if(a->query_len != b->query_len)
437 if((a->fragment_start > -1 && b->fragment_start > -1) &&
438 (a->fragment_len == b->fragment_len)) {
439 if(StrCmpNW(a->canon_uri+a->fragment_start, b->canon_uri+b->fragment_start, a->fragment_len))
441 } else if(a->fragment_len != b->fragment_len)
444 /* If we get here, the two URIs are equivalent. */
451 /* Computes the size of the given IPv6 address.
452 * Each h16 component is 16bits, if there is an IPv4 address, it's
453 * 32bits. If there's an elision it can be 16bits to 128bits, depending
454 * on the number of other components.
456 * Modeled after google-url's CheckIPv6ComponentsSize function
458 static void compute_ipv6_comps_size(ipv6_address *address) {
459 address->components_size = address->h16_count * 2;
462 /* IPv4 address is 4 bytes. */
463 address->components_size += 4;
465 if(address->elision) {
466 /* An elision can be anywhere from 2 bytes up to 16 bytes.
467 * It size depends on the size of the h16 and IPv4 components.
469 address->elision_size = 16 - address->components_size;
470 if(address->elision_size < 2)
471 address->elision_size = 2;
473 address->elision_size = 0;
476 /* Taken from dlls/jscript/lex.c */
477 static int hex_to_int(WCHAR val) {
478 if(val >= '0' && val <= '9')
480 else if(val >= 'a' && val <= 'f')
481 return val - 'a' + 10;
482 else if(val >= 'A' && val <= 'F')
483 return val - 'A' + 10;
488 /* Helper function for converting a percent encoded string
489 * representation of a WCHAR value into its actual WCHAR value. If
490 * the two characters following the '%' aren't valid hex values then
491 * this function returns the NULL character.
494 * "%2E" will result in '.' being returned by this function.
496 static WCHAR decode_pct_val(const WCHAR *ptr) {
499 if(*ptr == '%' && is_hexdigit(*(ptr + 1)) && is_hexdigit(*(ptr + 2))) {
500 INT a = hex_to_int(*(ptr + 1));
501 INT b = hex_to_int(*(ptr + 2));
510 /* Helper function for percent encoding a given character
511 * and storing the encoded value into a given buffer (dest).
513 * It's up to the calling function to ensure that there is
514 * at least enough space in 'dest' for the percent encoded
515 * value to be stored (so dest + 3 spaces available).
517 static inline void pct_encode_val(WCHAR val, WCHAR *dest) {
519 dest[1] = hexDigits[(val >> 4) & 0xf];
520 dest[2] = hexDigits[val & 0xf];
523 /* Scans the range of characters [str, end] and returns the last occurrence
524 * of 'ch' or returns NULL.
526 static const WCHAR *str_last_of(const WCHAR *str, const WCHAR *end, WCHAR ch) {
527 const WCHAR *ptr = end;
538 /* Attempts to parse the domain name from the host.
540 * This function also includes the Top-level Domain (TLD) name
541 * of the host when it tries to find the domain name. If it finds
542 * a valid domain name it will assign 'domain_start' the offset
543 * into 'host' where the domain name starts.
545 * It's implied that if a domain name its range is implied to be
546 * [host+domain_start, host+host_len).
548 static void find_domain_name(const WCHAR *host, DWORD host_len,
550 const WCHAR *last_tld, *sec_last_tld, *end;
552 end = host+host_len-1;
556 /* There has to be at least enough room for a '.' followed by a
557 * 3 character TLD for a domain to even exist in the host name.
562 last_tld = str_last_of(host, end, '.');
564 /* http://hostname -> has no domain name. */
567 sec_last_tld = str_last_of(host, last_tld-1, '.');
569 /* If the '.' is at the beginning of the host there
570 * has to be at least 3 characters in the TLD for it
572 * Ex: .com -> .com as the domain name.
573 * .co -> has no domain name.
575 if(last_tld-host == 0) {
576 if(end-(last_tld-1) < 3)
578 } else if(last_tld-host == 3) {
581 /* If there's three characters in front of last_tld and
582 * they are on the list of recognized TLDs, then this
583 * host doesn't have a domain (since the host only contains
585 * Ex: edu.uk -> has no domain name.
586 * foo.uk -> foo.uk as the domain name.
588 for(i = 0; i < sizeof(recognized_tlds)/sizeof(recognized_tlds[0]); ++i) {
589 if(!StrCmpNIW(host, recognized_tlds[i].tld_name, 3))
592 } else if(last_tld-host < 3)
593 /* Anything less than 3 characters is considered part
595 * Ex: ak.uk -> Has no domain name.
599 /* Otherwise the domain name is the whole host name. */
601 } else if(end+1-last_tld > 3) {
602 /* If the last_tld has more than 3 characters, then it's automatically
603 * considered the TLD of the domain name.
604 * Ex: www.winehq.org.uk.test -> uk.test as the domain name.
606 *domain_start = (sec_last_tld+1)-host;
607 } else if(last_tld - (sec_last_tld+1) < 4) {
609 /* If the sec_last_tld is 3 characters long it HAS to be on the list of
610 * recognized to still be considered part of the TLD name, otherwise
611 * its considered the domain name.
612 * Ex: www.google.com.uk -> google.com.uk as the domain name.
613 * www.google.foo.uk -> foo.uk as the domain name.
615 if(last_tld - (sec_last_tld+1) == 3) {
616 for(i = 0; i < sizeof(recognized_tlds)/sizeof(recognized_tlds[0]); ++i) {
617 if(!StrCmpNIW(sec_last_tld+1, recognized_tlds[i].tld_name, 3)) {
618 const WCHAR *domain = str_last_of(host, sec_last_tld-1, '.');
623 *domain_start = (domain+1) - host;
624 TRACE("Found domain name %s\n", debugstr_wn(host+*domain_start,
625 (host+host_len)-(host+*domain_start)));
630 *domain_start = (sec_last_tld+1)-host;
632 /* Since the sec_last_tld is less than 3 characters it's considered
634 * Ex: www.google.fo.uk -> google.fo.uk as the domain name.
636 const WCHAR *domain = str_last_of(host, sec_last_tld-1, '.');
641 *domain_start = (domain+1) - host;
644 /* The second to last TLD has more than 3 characters making it
646 * Ex: www.google.test.us -> test.us as the domain name.
648 *domain_start = (sec_last_tld+1)-host;
651 TRACE("Found domain name %s\n", debugstr_wn(host+*domain_start,
652 (host+host_len)-(host+*domain_start)));
655 /* Removes the dot segments from a hierarchical URIs path component. This
656 * function performs the removal in place.
658 * This is a modified version of Qt's QUrl function "removeDotsFromPath".
660 * This function returns the new length of the path string.
662 static DWORD remove_dot_segments(WCHAR *path, DWORD path_len) {
664 const WCHAR *in = out;
665 const WCHAR *end = out + path_len;
669 /* A. if the input buffer begins with a prefix of "/./" or "/.",
670 * where "." is a complete path segment, then replace that
671 * prefix with "/" in the input buffer; otherwise,
673 if(in <= end - 3 && in[0] == '/' && in[1] == '.' && in[2] == '/') {
676 } else if(in == end - 2 && in[0] == '/' && in[1] == '.') {
682 /* B. if the input buffer begins with a prefix of "/../" or "/..",
683 * where ".." is a complete path segment, then replace that
684 * prefix with "/" in the input buffer and remove the last
685 * segment and its preceding "/" (if any) from the output
688 if(in <= end - 4 && in[0] == '/' && in[1] == '.' && in[2] == '.' && in[3] == '/') {
689 while(out > path && *(--out) != '/');
693 } else if(in == end - 3 && in[0] == '/' && in[1] == '.' && in[2] == '.') {
694 while(out > path && *(--out) != '/');
703 /* C. move the first path segment in the input buffer to the end of
704 * the output buffer, including the initial "/" character (if
705 * any) and any subsequent characters up to, but not including,
706 * the next "/" character or the end of the input buffer.
709 while(in < end && *in != '/')
714 TRACE("(%p %d): Path after dot segments removed %s len=%d\n", path, path_len,
715 debugstr_wn(path, len), len);
719 /* Attempts to find the file extension in a given path. */
720 static INT find_file_extension(const WCHAR *path, DWORD path_len) {
723 for(end = path+path_len-1; end >= path && *end != '/' && *end != '\\'; --end) {
731 /* Computes the location where the elision should occur in the IPv6
732 * address using the numerical values of each component stored in
733 * 'values'. If the address shouldn't contain an elision then 'index'
734 * is assigned -1 as it's value. Otherwise 'index' will contain the
735 * starting index (into values) where the elision should be, and 'count'
736 * will contain the number of cells the elision covers.
739 * Windows will expand an elision if the elision only represents 1 h16
740 * component of the URI.
742 * Ex: [1::2:3:4:5:6:7] -> [1:0:2:3:4:5:6:7]
744 * If the IPv6 address contains an IPv4 address, the IPv4 address is also
745 * considered for being included as part of an elision if all it's components
748 * Ex: [1:2:3:4:5:6:0.0.0.0] -> [1:2:3:4:5:6::]
750 static void compute_elision_location(const ipv6_address *address, const USHORT values[8],
751 INT *index, DWORD *count) {
752 DWORD i, max_len, cur_len;
753 INT max_index, cur_index;
755 max_len = cur_len = 0;
756 max_index = cur_index = -1;
757 for(i = 0; i < 8; ++i) {
758 BOOL check_ipv4 = (address->ipv4 && i == 6);
759 BOOL is_end = (check_ipv4 || i == 7);
762 /* Check if the IPv4 address contains only zeros. */
763 if(values[i] == 0 && values[i+1] == 0) {
770 } else if(values[i] == 0) {
777 if(is_end || values[i] != 0) {
778 /* We only consider it for an elision if it's
779 * more than 1 component long.
781 if(cur_len > 1 && cur_len > max_len) {
782 /* Found the new elision location. */
784 max_index = cur_index;
787 /* Reset the current range for the next range of zeros. */
797 /* Removes all the leading and trailing white spaces or
798 * control characters from the URI and removes all control
799 * characters inside of the URI string.
801 static BSTR pre_process_uri(LPCWSTR uri) {
804 const WCHAR *start, *end;
810 /* Skip leading controls and whitespace. */
811 while(iscntrlW(*start) || isspaceW(*start)) ++start;
815 /* URI consisted only of control/whitespace. */
816 ret = SysAllocStringLen(NULL, 0);
818 while(iscntrlW(*end) || isspaceW(*end)) --end;
820 buf = heap_alloc(((end+1)-start)*sizeof(WCHAR));
824 for(ptr = buf; start < end+1; ++start) {
825 if(!iscntrlW(*start))
829 ret = SysAllocStringLen(buf, ptr-buf);
836 /* Converts the specified IPv4 address into an uint value.
838 * This function assumes that the IPv4 address has already been validated.
840 static UINT ipv4toui(const WCHAR *ip, DWORD len) {
842 DWORD comp_value = 0;
845 for(ptr = ip; ptr < ip+len; ++ptr) {
851 comp_value = comp_value*10 + (*ptr-'0');
860 /* Converts an IPv4 address in numerical form into it's fully qualified
861 * string form. This function returns the number of characters written
862 * to 'dest'. If 'dest' is NULL this function will return the number of
863 * characters that would have been written.
865 * It's up to the caller to ensure there's enough space in 'dest' for the
868 static DWORD ui2ipv4(WCHAR *dest, UINT address) {
869 static const WCHAR formatW[] =
870 {'%','u','.','%','u','.','%','u','.','%','u',0};
874 digits[0] = (address >> 24) & 0xff;
875 digits[1] = (address >> 16) & 0xff;
876 digits[2] = (address >> 8) & 0xff;
877 digits[3] = address & 0xff;
881 ret = sprintfW(tmp, formatW, digits[0], digits[1], digits[2], digits[3]);
883 ret = sprintfW(dest, formatW, digits[0], digits[1], digits[2], digits[3]);
888 /* Converts an h16 component (from an IPv6 address) into it's
891 * This function assumes that the h16 component has already been validated.
893 static USHORT h16tous(h16 component) {
897 for(i = 0; i < component.len; ++i) {
899 ret += hex_to_int(component.str[i]);
905 /* Converts an IPv6 address into it's 128 bits (16 bytes) numerical value.
907 * This function assumes that the ipv6_address has already been validated.
909 static BOOL ipv6_to_number(const ipv6_address *address, USHORT number[8]) {
910 DWORD i, cur_component = 0;
911 BOOL already_passed_elision = FALSE;
913 for(i = 0; i < address->h16_count; ++i) {
914 if(address->elision) {
915 if(address->components[i].str > address->elision && !already_passed_elision) {
916 /* Means we just passed the elision and need to add it's values to
917 * 'number' before we do anything else.
920 for(j = 0; j < address->elision_size; j+=2)
921 number[cur_component++] = 0;
923 already_passed_elision = TRUE;
927 number[cur_component++] = h16tous(address->components[i]);
930 /* Case when the elision appears after the h16 components. */
931 if(!already_passed_elision && address->elision) {
932 for(i = 0; i < address->elision_size; i+=2)
933 number[cur_component++] = 0;
934 already_passed_elision = TRUE;
938 UINT value = ipv4toui(address->ipv4, address->ipv4_len);
940 if(cur_component != 6) {
941 ERR("(%p %p): Failed sanity check with %d\n", address, number, cur_component);
945 number[cur_component++] = (value >> 16) & 0xffff;
946 number[cur_component] = value & 0xffff;
952 /* Checks if the characters pointed to by 'ptr' are
953 * a percent encoded data octet.
955 * pct-encoded = "%" HEXDIG HEXDIG
957 static BOOL check_pct_encoded(const WCHAR **ptr) {
958 const WCHAR *start = *ptr;
964 if(!is_hexdigit(**ptr)) {
970 if(!is_hexdigit(**ptr)) {
979 /* dec-octet = DIGIT ; 0-9
980 * / %x31-39 DIGIT ; 10-99
981 * / "1" 2DIGIT ; 100-199
982 * / "2" %x30-34 DIGIT ; 200-249
983 * / "25" %x30-35 ; 250-255
985 static BOOL check_dec_octet(const WCHAR **ptr) {
986 const WCHAR *c1, *c2, *c3;
989 /* A dec-octet must be at least 1 digit long. */
990 if(*c1 < '0' || *c1 > '9')
996 /* Since the 1 digit requirment was meet, it doesn't
997 * matter if this is a DIGIT value, it's considered a
1000 if(*c2 < '0' || *c2 > '9')
1006 /* Same explanation as above. */
1007 if(*c3 < '0' || *c3 > '9')
1010 /* Anything > 255 isn't a valid IP dec-octet. */
1011 if(*c1 >= '2' && *c2 >= '5' && *c3 >= '5') {
1020 /* Checks if there is an implicit IPv4 address in the host component of the URI.
1021 * The max value of an implicit IPv4 address is UINT_MAX.
1024 * "234567" would be considered an implicit IPv4 address.
1026 static BOOL check_implicit_ipv4(const WCHAR **ptr, UINT *val) {
1027 const WCHAR *start = *ptr;
1031 while(is_num(**ptr)) {
1032 ret = ret*10 + (**ptr - '0');
1034 if(ret > UINT_MAX) {
1048 /* Checks if the string contains an IPv4 address.
1050 * This function has a strict mode or a non-strict mode of operation
1051 * When 'strict' is set to FALSE this function will return TRUE if
1052 * the string contains at least 'dec-octet "." dec-octet' since partial
1053 * IPv4 addresses will be normalized out into full IPv4 addresses. When
1054 * 'strict' is set this function expects there to be a full IPv4 address.
1056 * IPv4address = dec-octet "." dec-octet "." dec-octet "." dec-octet
1058 static BOOL check_ipv4address(const WCHAR **ptr, BOOL strict) {
1059 const WCHAR *start = *ptr;
1061 if(!check_dec_octet(ptr)) {
1072 if(!check_dec_octet(ptr)) {
1086 if(!check_dec_octet(ptr)) {
1100 if(!check_dec_octet(ptr)) {
1105 /* Found a four digit ip address. */
1108 /* Tries to parse the scheme name of the URI.
1110 * scheme = ALPHA *(ALPHA | NUM | '+' | '-' | '.') as defined by RFC 3896.
1111 * NOTE: Windows accepts a number as the first character of a scheme.
1113 static BOOL parse_scheme_name(const WCHAR **ptr, parse_data *data) {
1114 const WCHAR *start = *ptr;
1116 data->scheme = NULL;
1117 data->scheme_len = 0;
1120 if(**ptr == '*' && *ptr == start) {
1121 /* Might have found a wildcard scheme. If it is the next
1122 * char has to be a ':' for it to be a valid URI
1126 } else if(!is_num(**ptr) && !is_alpha(**ptr) && **ptr != '+' &&
1127 **ptr != '-' && **ptr != '.')
1136 /* Schemes must end with a ':' */
1142 data->scheme = start;
1143 data->scheme_len = *ptr - start;
1149 /* Tries to deduce the corresponding URL_SCHEME for the given URI. Stores
1150 * the deduced URL_SCHEME in data->scheme_type.
1152 static BOOL parse_scheme_type(parse_data *data) {
1153 /* If there's scheme data then see if it's a recognized scheme. */
1154 if(data->scheme && data->scheme_len) {
1157 for(i = 0; i < sizeof(recognized_schemes)/sizeof(recognized_schemes[0]); ++i) {
1158 if(lstrlenW(recognized_schemes[i].scheme_name) == data->scheme_len) {
1159 /* Has to be a case insensitive compare. */
1160 if(!StrCmpNIW(recognized_schemes[i].scheme_name, data->scheme, data->scheme_len)) {
1161 data->scheme_type = recognized_schemes[i].scheme;
1167 /* If we get here it means it's not a recognized scheme. */
1168 data->scheme_type = URL_SCHEME_UNKNOWN;
1170 } else if(data->is_relative) {
1171 /* Relative URI's have no scheme. */
1172 data->scheme_type = URL_SCHEME_UNKNOWN;
1175 /* Should never reach here! what happened... */
1176 FIXME("(%p): Unable to determine scheme type for URI %s\n", data, debugstr_w(data->uri));
1181 /* Tries to parse (or deduce) the scheme_name of a URI. If it can't
1182 * parse a scheme from the URI it will try to deduce the scheme_name and scheme_type
1183 * using the flags specified in 'flags' (if any). Flags that affect how this function
1184 * operates are the Uri_CREATE_ALLOW_* flags.
1186 * All parsed/deduced information will be stored in 'data' when the function returns.
1188 * Returns TRUE if it was able to successfully parse the information.
1190 static BOOL parse_scheme(const WCHAR **ptr, parse_data *data, DWORD flags) {
1191 static const WCHAR fileW[] = {'f','i','l','e',0};
1192 static const WCHAR wildcardW[] = {'*',0};
1194 /* First check to see if the uri could implicitly be a file path. */
1195 if(is_implicit_file_path(*ptr)) {
1196 if(flags & Uri_CREATE_ALLOW_IMPLICIT_FILE_SCHEME) {
1197 data->scheme = fileW;
1198 data->scheme_len = lstrlenW(fileW);
1199 data->has_implicit_scheme = TRUE;
1201 TRACE("(%p %p %x): URI is an implicit file path.\n", ptr, data, flags);
1203 /* Window's does not consider anything that can implicitly be a file
1204 * path to be a valid URI if the ALLOW_IMPLICIT_FILE_SCHEME flag is not set...
1206 TRACE("(%p %p %x): URI is implicitly a file path, but, the ALLOW_IMPLICIT_FILE_SCHEME flag wasn't set.\n",
1210 } else if(!parse_scheme_name(ptr, data)) {
1211 /* No Scheme was found, this means it could be:
1212 * a) an implicit Wildcard scheme
1216 if(flags & Uri_CREATE_ALLOW_IMPLICIT_WILDCARD_SCHEME) {
1217 data->scheme = wildcardW;
1218 data->scheme_len = lstrlenW(wildcardW);
1219 data->has_implicit_scheme = TRUE;
1221 TRACE("(%p %p %x): URI is an implicit wildcard scheme.\n", ptr, data, flags);
1222 } else if (flags & Uri_CREATE_ALLOW_RELATIVE) {
1223 data->is_relative = TRUE;
1224 TRACE("(%p %p %x): URI is relative.\n", ptr, data, flags);
1226 TRACE("(%p %p %x): Malformed URI found. Unable to deduce scheme name.\n", ptr, data, flags);
1231 if(!data->is_relative)
1232 TRACE("(%p %p %x): Found scheme=%s scheme_len=%d\n", ptr, data, flags,
1233 debugstr_wn(data->scheme, data->scheme_len), data->scheme_len);
1235 if(!parse_scheme_type(data))
1238 TRACE("(%p %p %x): Assigned %d as the URL_SCHEME.\n", ptr, data, flags, data->scheme_type);
1242 /* Parses the userinfo part of the URI (if it exists). The userinfo field of
1243 * a URI can consist of "username:password@", or just "username@".
1246 * userinfo = *( unreserved / pct-encoded / sub-delims / ":" )
1249 * 1) If there is more than one ':' in the userinfo part of the URI Windows
1250 * uses the first occurrence of ':' to delimit the username and password
1254 * ftp://user:pass:word@winehq.org
1256 * Would yield, "user" as the username and "pass:word" as the password.
1258 * 2) Windows allows any character to appear in the "userinfo" part of
1259 * a URI, as long as it's not an authority delimeter character set.
1261 static void parse_userinfo(const WCHAR **ptr, parse_data *data, DWORD flags) {
1262 data->userinfo = *ptr;
1263 data->userinfo_split = -1;
1265 while(**ptr != '@') {
1266 if(**ptr == ':' && data->userinfo_split == -1)
1267 data->userinfo_split = *ptr - data->userinfo;
1268 else if(**ptr == '%') {
1269 /* If it's a known scheme type, it has to be a valid percent
1272 if(!check_pct_encoded(ptr)) {
1273 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
1274 *ptr = data->userinfo;
1275 data->userinfo = NULL;
1276 data->userinfo_split = -1;
1278 TRACE("(%p %p %x): URI contained no userinfo.\n", ptr, data, flags);
1283 } else if(is_auth_delim(**ptr, data->scheme_type != URL_SCHEME_UNKNOWN))
1290 *ptr = data->userinfo;
1291 data->userinfo = NULL;
1292 data->userinfo_split = -1;
1294 TRACE("(%p %p %x): URI contained no userinfo.\n", ptr, data, flags);
1298 data->userinfo_len = *ptr - data->userinfo;
1299 TRACE("(%p %p %x): Found userinfo=%s userinfo_len=%d split=%d.\n", ptr, data, flags,
1300 debugstr_wn(data->userinfo, data->userinfo_len), data->userinfo_len, data->userinfo_split);
1304 /* Attempts to parse a port from the URI.
1307 * Windows seems to have a cap on what the maximum value
1308 * for a port can be. The max value is USHORT_MAX.
1312 static BOOL parse_port(const WCHAR **ptr, parse_data *data, DWORD flags) {
1316 while(!is_auth_delim(**ptr, data->scheme_type != URL_SCHEME_UNKNOWN)) {
1317 if(!is_num(**ptr)) {
1323 port = port*10 + (**ptr-'0');
1325 if(port > USHORT_MAX) {
1334 data->port_value = port;
1335 data->port_len = *ptr - data->port;
1337 TRACE("(%p %p %x): Found port %s len=%d value=%u\n", ptr, data, flags,
1338 debugstr_wn(data->port, data->port_len), data->port_len, data->port_value);
1342 /* Attempts to parse a IPv4 address from the URI.
1345 * Window's normalizes IPv4 addresses, This means there's three
1346 * possibilities for the URI to contain an IPv4 address.
1347 * 1) A well formed address (ex. 192.2.2.2).
1348 * 2) A partially formed address. For example "192.0" would
1349 * normalize to "192.0.0.0" during canonicalization.
1350 * 3) An implicit IPv4 address. For example "256" would
1351 * normalize to "0.0.1.0" during canonicalization. Also
1352 * note that the maximum value for an implicit IP address
1353 * is UINT_MAX, if the value in the URI exceeds this then
1354 * it is not considered an IPv4 address.
1356 static BOOL parse_ipv4address(const WCHAR **ptr, parse_data *data, DWORD flags) {
1357 const BOOL is_unknown = data->scheme_type == URL_SCHEME_UNKNOWN;
1360 if(!check_ipv4address(ptr, FALSE)) {
1361 if(!check_implicit_ipv4(ptr, &data->implicit_ipv4)) {
1362 TRACE("(%p %p %x): URI didn't contain anything looking like an IPv4 address.\n",
1368 data->has_implicit_ip = TRUE;
1371 /* Check if what we found is the only part of the host name (if it isn't
1372 * we don't have an IPv4 address).
1376 if(!parse_port(ptr, data, flags)) {
1381 } else if(!is_auth_delim(**ptr, !is_unknown)) {
1382 /* Found more data which belongs the host, so this isn't an IPv4. */
1385 data->has_implicit_ip = FALSE;
1389 data->host_len = *ptr - data->host;
1390 data->host_type = Uri_HOST_IPV4;
1392 TRACE("(%p %p %x): IPv4 address found. host=%s host_len=%d host_type=%d\n",
1393 ptr, data, flags, debugstr_wn(data->host, data->host_len),
1394 data->host_len, data->host_type);
1398 /* Attempts to parse the reg-name from the URI.
1400 * Because of the way Windows handles ':' this function also
1401 * handles parsing the port.
1403 * reg-name = *( unreserved / pct-encoded / sub-delims )
1406 * Windows allows everything, but, the characters in "auth_delims" and ':'
1407 * to appear in a reg-name, unless it's an unknown scheme type then ':' is
1408 * allowed to appear (even if a valid port isn't after it).
1410 * Windows doesn't like host names which start with '[' and end with ']'
1411 * and don't contain a valid IP literal address in between them.
1413 * On Windows if an '[' is encountered in the host name the ':' no longer
1414 * counts as a delimiter until you reach the next ']' or an "authority delimeter".
1416 * A reg-name CAN be empty.
1418 static BOOL parse_reg_name(const WCHAR **ptr, parse_data *data, DWORD flags) {
1419 const BOOL has_start_bracket = **ptr == '[';
1420 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
1421 BOOL inside_brackets = has_start_bracket;
1422 BOOL ignore_col = FALSE;
1424 /* We have to be careful with file schemes. */
1425 if(data->scheme_type == URL_SCHEME_FILE) {
1426 /* This is because an implicit file scheme could be "C:\\test" and it
1427 * would trick this function into thinking the host is "C", when after
1428 * canonicalization the host would end up being an empty string. A drive
1429 * path can also have a '|' instead of a ':' after the drive letter.
1431 if(is_drive_path(*ptr)) {
1432 /* Regular old drive paths don't have a host type (or host name). */
1433 data->host_type = Uri_HOST_UNKNOWN;
1437 } else if(is_unc_path(*ptr))
1438 /* Skip past the "\\" of a UNC path. */
1444 while(!is_auth_delim(**ptr, known_scheme)) {
1445 if(**ptr == ':' && !ignore_col) {
1446 /* We can ignore ':' if were inside brackets.*/
1447 if(!inside_brackets) {
1448 const WCHAR *tmp = (*ptr)++;
1450 /* Attempt to parse the port. */
1451 if(!parse_port(ptr, data, flags)) {
1452 /* Windows expects there to be a valid port for known scheme types. */
1453 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
1456 TRACE("(%p %p %x): Expected valid port\n", ptr, data, flags);
1459 /* Windows gives up on trying to parse a port when it
1460 * encounters 1 invalid port.
1464 data->host_len = tmp - data->host;
1468 } else if(**ptr == '%' && known_scheme) {
1469 /* Has to be a legit % encoded value. */
1470 if(!check_pct_encoded(ptr)) {
1476 } else if(**ptr == ']')
1477 inside_brackets = FALSE;
1478 else if(**ptr == '[')
1479 inside_brackets = TRUE;
1484 if(has_start_bracket) {
1485 /* Make sure the last character of the host wasn't a ']'. */
1486 if(*(*ptr-1) == ']') {
1487 TRACE("(%p %p %x): Expected an IP literal inside of the host\n",
1495 /* Don't overwrite our length if we found a port earlier. */
1497 data->host_len = *ptr - data->host;
1499 /* If the host is empty, then it's an unknown host type. */
1500 if(data->host_len == 0)
1501 data->host_type = Uri_HOST_UNKNOWN;
1503 data->host_type = Uri_HOST_DNS;
1505 TRACE("(%p %p %x): Parsed reg-name. host=%s len=%d\n", ptr, data, flags,
1506 debugstr_wn(data->host, data->host_len), data->host_len);
1510 /* Attempts to parse an IPv6 address out of the URI.
1512 * IPv6address = 6( h16 ":" ) ls32
1513 * / "::" 5( h16 ":" ) ls32
1514 * / [ h16 ] "::" 4( h16 ":" ) ls32
1515 * / [ *1( h16 ":" ) h16 ] "::" 3( h16 ":" ) ls32
1516 * / [ *2( h16 ":" ) h16 ] "::" 2( h16 ":" ) ls32
1517 * / [ *3( h16 ":" ) h16 ] "::" h16 ":" ls32
1518 * / [ *4( h16 ":" ) h16 ] "::" ls32
1519 * / [ *5( h16 ":" ) h16 ] "::" h16
1520 * / [ *6( h16 ":" ) h16 ] "::"
1522 * ls32 = ( h16 ":" h16 ) / IPv4address
1523 * ; least-significant 32 bits of address.
1526 * ; 16 bits of address represented in hexadecimal.
1528 * Modeled after google-url's 'DoParseIPv6' function.
1530 static BOOL parse_ipv6address(const WCHAR **ptr, parse_data *data, DWORD flags) {
1531 const WCHAR *start, *cur_start;
1534 start = cur_start = *ptr;
1535 memset(&ip, 0, sizeof(ipv6_address));
1538 /* Check if we're on the last character of the host. */
1539 BOOL is_end = (is_auth_delim(**ptr, data->scheme_type != URL_SCHEME_UNKNOWN)
1542 BOOL is_split = (**ptr == ':');
1543 BOOL is_elision = (is_split && !is_end && *(*ptr+1) == ':');
1545 /* Check if we're at the end of a component, or
1546 * if we're at the end of the IPv6 address.
1548 if(is_split || is_end) {
1551 cur_len = *ptr - cur_start;
1553 /* h16 can't have a length > 4. */
1557 TRACE("(%p %p %x): h16 component to long.\n",
1563 /* An h16 component can't have the length of 0 unless
1564 * the elision is at the beginning of the address, or
1565 * at the end of the address.
1567 if(!((*ptr == start && is_elision) ||
1568 (is_end && (*ptr-2) == ip.elision))) {
1570 TRACE("(%p %p %x): IPv6 component cannot have a length of 0.\n",
1577 /* An IPv6 address can have no more than 8 h16 components. */
1578 if(ip.h16_count >= 8) {
1580 TRACE("(%p %p %x): Not a IPv6 address, to many h16 components.\n",
1585 ip.components[ip.h16_count].str = cur_start;
1586 ip.components[ip.h16_count].len = cur_len;
1588 TRACE("(%p %p %x): Found h16 component %s, len=%d, h16_count=%d\n",
1589 ptr, data, flags, debugstr_wn(cur_start, cur_len), cur_len,
1599 /* A IPv6 address can only have 1 elision ('::'). */
1603 TRACE("(%p %p %x): IPv6 address cannot have 2 elisions.\n",
1615 if(!check_ipv4address(ptr, TRUE)) {
1616 if(!is_hexdigit(**ptr)) {
1617 /* Not a valid character for an IPv6 address. */
1622 /* Found an IPv4 address. */
1623 ip.ipv4 = cur_start;
1624 ip.ipv4_len = *ptr - cur_start;
1626 TRACE("(%p %p %x): Found an attached IPv4 address %s len=%d.\n",
1627 ptr, data, flags, debugstr_wn(ip.ipv4, ip.ipv4_len),
1630 /* IPv4 addresses can only appear at the end of a IPv6. */
1636 compute_ipv6_comps_size(&ip);
1638 /* Make sure the IPv6 address adds up to 16 bytes. */
1639 if(ip.components_size + ip.elision_size != 16) {
1641 TRACE("(%p %p %x): Invalid IPv6 address, did not add up to 16 bytes.\n",
1646 if(ip.elision_size == 2) {
1647 /* For some reason on Windows if an elision that represents
1648 * only 1 h16 component is encountered at the very begin or
1649 * end of an IPv6 address, Windows does not consider it a
1650 * valid IPv6 address.
1652 * Ex: [::2:3:4:5:6:7] is not valid, even though the sum
1653 * of all the components == 128bits.
1655 if(ip.elision < ip.components[0].str ||
1656 ip.elision > ip.components[ip.h16_count-1].str) {
1658 TRACE("(%p %p %x): Invalid IPv6 address. Detected elision of 2 bytes at the beginning or end of the address.\n",
1664 data->host_type = Uri_HOST_IPV6;
1665 data->has_ipv6 = TRUE;
1666 data->ipv6_address = ip;
1668 TRACE("(%p %p %x): Found valid IPv6 literal %s len=%d\n",
1669 ptr, data, flags, debugstr_wn(start, *ptr-start),
1674 /* IPvFuture = "v" 1*HEXDIG "." 1*( unreserved / sub-delims / ":" ) */
1675 static BOOL parse_ipvfuture(const WCHAR **ptr, parse_data *data, DWORD flags) {
1676 const WCHAR *start = *ptr;
1678 /* IPvFuture has to start with a 'v' or 'V'. */
1679 if(**ptr != 'v' && **ptr != 'V')
1682 /* Following the v there must be at least 1 hex digit. */
1684 if(!is_hexdigit(**ptr)) {
1690 while(is_hexdigit(**ptr))
1693 /* End of the hexdigit sequence must be a '.' */
1700 if(!is_unreserved(**ptr) && !is_subdelim(**ptr) && **ptr != ':') {
1706 while(is_unreserved(**ptr) || is_subdelim(**ptr) || **ptr == ':')
1709 data->host_type = Uri_HOST_UNKNOWN;
1711 TRACE("(%p %p %x): Parsed IPvFuture address %s len=%d\n", ptr, data, flags,
1712 debugstr_wn(start, *ptr-start), *ptr-start);
1717 /* IP-literal = "[" ( IPv6address / IPvFuture ) "]" */
1718 static BOOL parse_ip_literal(const WCHAR **ptr, parse_data *data, DWORD flags) {
1727 if(!parse_ipv6address(ptr, data, flags)) {
1728 if(!parse_ipvfuture(ptr, data, flags)) {
1744 /* If a valid port is not found, then let it trickle down to
1747 if(!parse_port(ptr, data, flags)) {
1753 data->host_len = *ptr - data->host;
1758 /* Parses the host information from the URI.
1760 * host = IP-literal / IPv4address / reg-name
1762 static BOOL parse_host(const WCHAR **ptr, parse_data *data, DWORD flags) {
1763 if(!parse_ip_literal(ptr, data, flags)) {
1764 if(!parse_ipv4address(ptr, data, flags)) {
1765 if(!parse_reg_name(ptr, data, flags)) {
1766 TRACE("(%p %p %x): Malformed URI, Unknown host type.\n",
1776 /* Parses the authority information from the URI.
1778 * authority = [ userinfo "@" ] host [ ":" port ]
1780 static BOOL parse_authority(const WCHAR **ptr, parse_data *data, DWORD flags) {
1781 parse_userinfo(ptr, data, flags);
1783 /* Parsing the port will happen during one of the host parsing
1784 * routines (if the URI has a port).
1786 if(!parse_host(ptr, data, flags))
1792 /* Attempts to parse the path information of a hierarchical URI. */
1793 static BOOL parse_path_hierarchical(const WCHAR **ptr, parse_data *data, DWORD flags) {
1794 const WCHAR *start = *ptr;
1795 static const WCHAR slash[] = {'/',0};
1796 const BOOL is_file = data->scheme_type == URL_SCHEME_FILE;
1798 if(is_path_delim(**ptr)) {
1799 if(data->scheme_type == URL_SCHEME_WILDCARD) {
1800 /* Wildcard schemes don't get a '/' attached if their path is
1805 } else if(!(flags & Uri_CREATE_NO_CANONICALIZE)) {
1806 /* If the path component is empty, then a '/' is added. */
1811 while(!is_path_delim(**ptr)) {
1812 if(**ptr == '%' && data->scheme_type != URL_SCHEME_UNKNOWN && !is_file) {
1813 if(!check_pct_encoded(ptr)) {
1818 } else if(is_forbidden_dos_path_char(**ptr) && is_file &&
1819 (flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
1820 /* File schemes with USE_DOS_PATH set aren't allowed to have
1821 * a '<' or '>' or '\"' appear in them.
1825 } else if(**ptr == '\\') {
1826 /* Not allowed to have a backslash if NO_CANONICALIZE is set
1827 * and the scheme is known type (but not a file scheme).
1829 if(flags & Uri_CREATE_NO_CANONICALIZE) {
1830 if(data->scheme_type != URL_SCHEME_FILE &&
1831 data->scheme_type != URL_SCHEME_UNKNOWN) {
1841 /* The only time a URI doesn't have a path is when
1842 * the NO_CANONICALIZE flag is set and the raw URI
1843 * didn't contain one.
1850 data->path_len = *ptr - start;
1855 TRACE("(%p %p %x): Parsed path %s len=%d\n", ptr, data, flags,
1856 debugstr_wn(data->path, data->path_len), data->path_len);
1858 TRACE("(%p %p %x): The URI contained no path\n", ptr, data, flags);
1863 /* Parses the path of a opaque URI (much less strict then the parser
1864 * for a hierarchical URI).
1867 * Windows allows invalid % encoded data to appear in opaque URI paths
1868 * for unknown scheme types.
1870 * File schemes with USE_DOS_PATH set aren't allowed to have '<', '>', or '\"'
1873 static BOOL parse_path_opaque(const WCHAR **ptr, parse_data *data, DWORD flags) {
1874 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
1875 const BOOL is_file = data->scheme_type == URL_SCHEME_FILE;
1879 while(!is_path_delim(**ptr)) {
1880 if(**ptr == '%' && known_scheme) {
1881 if(!check_pct_encoded(ptr)) {
1887 } else if(is_forbidden_dos_path_char(**ptr) && is_file &&
1888 (flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
1897 data->path_len = *ptr - data->path;
1898 TRACE("(%p %p %x): Parsed opaque URI path %s len=%d\n", ptr, data, flags,
1899 debugstr_wn(data->path, data->path_len), data->path_len);
1903 /* Determines how the URI should be parsed after the scheme information.
1905 * If the scheme is followed, by "//" then, it is treated as an hierarchical URI
1906 * which then the authority and path information will be parsed out. Otherwise, the
1907 * URI will be treated as an opaque URI which the authority information is not parsed
1910 * RFC 3896 definition of hier-part:
1912 * hier-part = "//" authority path-abempty
1917 * MSDN opaque URI definition:
1918 * scheme ":" path [ "#" fragment ]
1921 * If the URI is of an unknown scheme type and has a "//" following the scheme then it
1922 * is treated as a hierarchical URI, but, if the CREATE_NO_CRACK_UNKNOWN_SCHEMES flag is
1923 * set then it is considered an opaque URI reguardless of what follows the scheme information
1924 * (per MSDN documentation).
1926 static BOOL parse_hierpart(const WCHAR **ptr, parse_data *data, DWORD flags) {
1927 const WCHAR *start = *ptr;
1929 /* Checks if the authority information needs to be parsed. */
1930 if(is_hierarchical_uri(ptr, data)) {
1931 /* Only treat it as a hierarchical URI if the scheme_type is known or
1932 * the Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES flag is not set.
1934 if(data->scheme_type != URL_SCHEME_UNKNOWN ||
1935 !(flags & Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES)) {
1936 TRACE("(%p %p %x): Treating URI as an hierarchical URI.\n", ptr, data, flags);
1937 data->is_opaque = FALSE;
1939 /* TODO: Handle hierarchical URI's, parse authority then parse the path. */
1940 if(!parse_authority(ptr, data, flags))
1943 return parse_path_hierarchical(ptr, data, flags);
1945 /* Reset ptr to it's starting position so opaque path parsing
1946 * begins at the correct location.
1951 /* If it reaches here, then the URI will be treated as an opaque
1955 TRACE("(%p %p %x): Treating URI as an opaque URI.\n", ptr, data, flags);
1957 data->is_opaque = TRUE;
1958 if(!parse_path_opaque(ptr, data, flags))
1964 /* Attempts to parse the query string from the URI.
1967 * If NO_DECODE_EXTRA_INFO flag is set, then invalid percent encoded
1968 * data is allowed appear in the query string. For unknown scheme types
1969 * invalid percent encoded data is allowed to appear reguardless.
1971 static BOOL parse_query(const WCHAR **ptr, parse_data *data, DWORD flags) {
1972 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
1975 TRACE("(%p %p %x): URI didn't contain a query string.\n", ptr, data, flags);
1982 while(**ptr && **ptr != '#') {
1983 if(**ptr == '%' && known_scheme &&
1984 !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
1985 if(!check_pct_encoded(ptr)) {
1996 data->query_len = *ptr - data->query;
1998 TRACE("(%p %p %x): Parsed query string %s len=%d\n", ptr, data, flags,
1999 debugstr_wn(data->query, data->query_len), data->query_len);
2003 /* Attempts to parse the fragment from the URI.
2006 * If NO_DECODE_EXTRA_INFO flag is set, then invalid percent encoded
2007 * data is allowed appear in the query string. For unknown scheme types
2008 * invalid percent encoded data is allowed to appear reguardless.
2010 static BOOL parse_fragment(const WCHAR **ptr, parse_data *data, DWORD flags) {
2011 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
2014 TRACE("(%p %p %x): URI didn't contain a fragment.\n", ptr, data, flags);
2018 data->fragment = *ptr;
2022 if(**ptr == '%' && known_scheme &&
2023 !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
2024 if(!check_pct_encoded(ptr)) {
2025 *ptr = data->fragment;
2026 data->fragment = NULL;
2035 data->fragment_len = *ptr - data->fragment;
2037 TRACE("(%p %p %x): Parsed fragment %s len=%d\n", ptr, data, flags,
2038 debugstr_wn(data->fragment, data->fragment_len), data->fragment_len);
2042 /* Parses and validates the components of the specified by data->uri
2043 * and stores the information it parses into 'data'.
2045 * Returns TRUE if it successfully parsed the URI. False otherwise.
2047 static BOOL parse_uri(parse_data *data, DWORD flags) {
2054 TRACE("(%p %x): BEGINNING TO PARSE URI %s.\n", data, flags, debugstr_w(data->uri));
2056 if(!parse_scheme(pptr, data, flags))
2059 if(!parse_hierpart(pptr, data, flags))
2062 if(!parse_query(pptr, data, flags))
2065 if(!parse_fragment(pptr, data, flags))
2068 TRACE("(%p %x): FINISHED PARSING URI.\n", data, flags);
2072 /* Canonicalizes the userinfo of the URI represented by the parse_data.
2074 * Canonicalization of the userinfo is a simple process. If there are any percent
2075 * encoded characters that fall in the "unreserved" character set, they are decoded
2076 * to their actual value. If a character is not in the "unreserved" or "reserved" sets
2077 * then it is percent encoded. Other than that the characters are copied over without
2080 static BOOL canonicalize_userinfo(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2083 uri->userinfo_start = uri->userinfo_split = -1;
2084 uri->userinfo_len = 0;
2087 /* URI doesn't have userinfo, so nothing to do here. */
2090 uri->userinfo_start = uri->canon_len;
2092 while(i < data->userinfo_len) {
2093 if(data->userinfo[i] == ':' && uri->userinfo_split == -1)
2094 /* Windows only considers the first ':' as the delimiter. */
2095 uri->userinfo_split = uri->canon_len - uri->userinfo_start;
2096 else if(data->userinfo[i] == '%') {
2097 /* Only decode % encoded values for known scheme types. */
2098 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
2099 /* See if the value really needs decoded. */
2100 WCHAR val = decode_pct_val(data->userinfo + i);
2101 if(is_unreserved(val)) {
2103 uri->canon_uri[uri->canon_len] = val;
2107 /* Move pass the hex characters. */
2112 } else if(!is_reserved(data->userinfo[i]) && !is_unreserved(data->userinfo[i]) &&
2113 data->userinfo[i] != '\\') {
2114 /* Only percent encode forbidden characters if the NO_ENCODE_FORBIDDEN_CHARACTERS flag
2117 if(!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS)) {
2119 pct_encode_val(data->userinfo[i], uri->canon_uri + uri->canon_len);
2121 uri->canon_len += 3;
2128 /* Nothing special, so just copy the character over. */
2129 uri->canon_uri[uri->canon_len] = data->userinfo[i];
2135 uri->userinfo_len = uri->canon_len - uri->userinfo_start;
2137 TRACE("(%p %p %x %d): Canonicalized userinfo, userinfo_start=%d, userinfo=%s, userinfo_split=%d userinfo_len=%d.\n",
2138 data, uri, flags, computeOnly, uri->userinfo_start, debugstr_wn(uri->canon_uri + uri->userinfo_start, uri->userinfo_len),
2139 uri->userinfo_split, uri->userinfo_len);
2141 /* Now insert the '@' after the userinfo. */
2143 uri->canon_uri[uri->canon_len] = '@';
2149 /* Attempts to canonicalize a reg_name.
2151 * Things that happen:
2152 * 1) If Uri_CREATE_NO_CANONICALIZE flag is not set, then the reg_name is
2153 * lower cased. Unless it's an unknown scheme type, which case it's
2154 * no lower cased reguardless.
2156 * 2) Unreserved % encoded characters are decoded for known
2159 * 3) Forbidden characters are % encoded as long as
2160 * Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS flag is not set and
2161 * it isn't an unknown scheme type.
2163 * 4) If it's a file scheme and the host is "localhost" it's removed.
2165 static BOOL canonicalize_reg_name(const parse_data *data, Uri *uri,
2166 DWORD flags, BOOL computeOnly) {
2167 static const WCHAR localhostW[] =
2168 {'l','o','c','a','l','h','o','s','t',0};
2170 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
2172 uri->host_start = uri->canon_len;
2174 if(data->scheme_type == URL_SCHEME_FILE &&
2175 data->host_len == lstrlenW(localhostW)) {
2176 if(!StrCmpNIW(data->host, localhostW, data->host_len)) {
2177 uri->host_start = -1;
2179 uri->host_type = Uri_HOST_UNKNOWN;
2184 for(ptr = data->host; ptr < data->host+data->host_len; ++ptr) {
2185 if(*ptr == '%' && known_scheme) {
2186 WCHAR val = decode_pct_val(ptr);
2187 if(is_unreserved(val)) {
2188 /* If NO_CANONICALZE is not set, then windows lower cases the
2191 if(!(flags & Uri_CREATE_NO_CANONICALIZE) && isupperW(val)) {
2193 uri->canon_uri[uri->canon_len] = tolowerW(val);
2196 uri->canon_uri[uri->canon_len] = val;
2200 /* Skip past the % encoded character. */
2204 /* Just copy the % over. */
2206 uri->canon_uri[uri->canon_len] = *ptr;
2209 } else if(*ptr == '\\') {
2210 /* Only unknown scheme types could have made it here with a '\\' in the host name. */
2212 uri->canon_uri[uri->canon_len] = *ptr;
2214 } else if(!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS) &&
2215 !is_unreserved(*ptr) && !is_reserved(*ptr) && known_scheme) {
2217 pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
2219 /* The percent encoded value gets lower cased also. */
2220 if(!(flags & Uri_CREATE_NO_CANONICALIZE)) {
2221 uri->canon_uri[uri->canon_len+1] = tolowerW(uri->canon_uri[uri->canon_len+1]);
2222 uri->canon_uri[uri->canon_len+2] = tolowerW(uri->canon_uri[uri->canon_len+2]);
2226 uri->canon_len += 3;
2229 if(!(flags & Uri_CREATE_NO_CANONICALIZE) && known_scheme)
2230 uri->canon_uri[uri->canon_len] = tolowerW(*ptr);
2232 uri->canon_uri[uri->canon_len] = *ptr;
2239 uri->host_len = uri->canon_len - uri->host_start;
2242 TRACE("(%p %p %x %d): Canonicalize reg_name=%s len=%d\n", data, uri, flags,
2243 computeOnly, debugstr_wn(uri->canon_uri+uri->host_start, uri->host_len),
2247 find_domain_name(uri->canon_uri+uri->host_start, uri->host_len,
2248 &(uri->domain_offset));
2253 /* Attempts to canonicalize an implicit IPv4 address. */
2254 static BOOL canonicalize_implicit_ipv4address(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2255 uri->host_start = uri->canon_len;
2257 TRACE("%u\n", data->implicit_ipv4);
2258 /* For unknown scheme types Window's doesn't convert
2259 * the value into an IP address, but, it still considers
2260 * it an IPv4 address.
2262 if(data->scheme_type == URL_SCHEME_UNKNOWN) {
2264 memcpy(uri->canon_uri+uri->canon_len, data->host, data->host_len*sizeof(WCHAR));
2265 uri->canon_len += data->host_len;
2268 uri->canon_len += ui2ipv4(uri->canon_uri+uri->canon_len, data->implicit_ipv4);
2270 uri->canon_len += ui2ipv4(NULL, data->implicit_ipv4);
2273 uri->host_len = uri->canon_len - uri->host_start;
2274 uri->host_type = Uri_HOST_IPV4;
2277 TRACE("%p %p %x %d): Canonicalized implicit IP address=%s len=%d\n",
2278 data, uri, flags, computeOnly,
2279 debugstr_wn(uri->canon_uri+uri->host_start, uri->host_len),
2285 /* Attempts to canonicalize an IPv4 address.
2287 * If the parse_data represents a URI that has an implicit IPv4 address
2288 * (ex. http://256/, this function will convert 256 into 0.0.1.0). If
2289 * the implicit IP address exceeds the value of UINT_MAX (maximum value
2290 * for an IPv4 address) it's canonicalized as if were a reg-name.
2292 * If the parse_data contains a partial or full IPv4 address it normalizes it.
2293 * A partial IPv4 address is something like "192.0" and would be normalized to
2294 * "192.0.0.0". With a full (or partial) IPv4 address like "192.002.01.003" would
2295 * be normalized to "192.2.1.3".
2298 * Window's ONLY normalizes IPv4 address for known scheme types (one that isn't
2299 * URL_SCHEME_UNKNOWN). For unknown scheme types, it simply copies the data from
2300 * the original URI into the canonicalized URI, but, it still recognizes URI's
2301 * host type as HOST_IPV4.
2303 static BOOL canonicalize_ipv4address(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2304 if(data->has_implicit_ip)
2305 return canonicalize_implicit_ipv4address(data, uri, flags, computeOnly);
2307 uri->host_start = uri->canon_len;
2309 /* Windows only normalizes for known scheme types. */
2310 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
2311 /* parse_data contains a partial or full IPv4 address, so normalize it. */
2312 DWORD i, octetDigitCount = 0, octetCount = 0;
2313 BOOL octetHasDigit = FALSE;
2315 for(i = 0; i < data->host_len; ++i) {
2316 if(data->host[i] == '0' && !octetHasDigit) {
2317 /* Can ignore leading zeros if:
2318 * 1) It isn't the last digit of the octet.
2319 * 2) i+1 != data->host_len
2322 if(octetDigitCount == 2 ||
2323 i+1 == data->host_len ||
2324 data->host[i+1] == '.') {
2326 uri->canon_uri[uri->canon_len] = data->host[i];
2328 TRACE("Adding zero\n");
2330 } else if(data->host[i] == '.') {
2332 uri->canon_uri[uri->canon_len] = data->host[i];
2335 octetDigitCount = 0;
2336 octetHasDigit = FALSE;
2340 uri->canon_uri[uri->canon_len] = data->host[i];
2344 octetHasDigit = TRUE;
2348 /* Make sure the canonicalized IP address has 4 dec-octets.
2349 * If doesn't add "0" ones until there is 4;
2351 for( ; octetCount < 3; ++octetCount) {
2353 uri->canon_uri[uri->canon_len] = '.';
2354 uri->canon_uri[uri->canon_len+1] = '0';
2357 uri->canon_len += 2;
2360 /* Windows doesn't normalize addresses in unknown schemes. */
2362 memcpy(uri->canon_uri+uri->canon_len, data->host, data->host_len*sizeof(WCHAR));
2363 uri->canon_len += data->host_len;
2366 uri->host_len = uri->canon_len - uri->host_start;
2368 TRACE("(%p %p %x %d): Canonicalized IPv4 address, ip=%s len=%d\n",
2369 data, uri, flags, computeOnly,
2370 debugstr_wn(uri->canon_uri+uri->host_start, uri->host_len),
2377 /* Attempts to canonicalize the IPv6 address of the URI.
2379 * Multiple things happen during the canonicalization of an IPv6 address:
2380 * 1) Any leading zero's in an h16 component are removed.
2381 * Ex: [0001:0022::] -> [1:22::]
2383 * 2) The longest sequence of zero h16 components are compressed
2384 * into a "::" (elision). If there's a tie, the first is choosen.
2386 * Ex: [0:0:0:0:1:6:7:8] -> [::1:6:7:8]
2387 * [0:0:0:0:1:2::] -> [::1:2:0:0]
2388 * [0:0:1:2:0:0:7:8] -> [::1:2:0:0:7:8]
2390 * 3) If an IPv4 address is attached to the IPv6 address, it's
2392 * Ex: [::001.002.022.000] -> [::1.2.22.0]
2394 * 4) If an elision is present, but, only represents 1 h16 component
2397 * Ex: [1::2:3:4:5:6:7] -> [1:0:2:3:4:5:6:7]
2399 * 5) If the IPv6 address contains an IPv4 address and there exists
2400 * at least 1 non-zero h16 component the IPv4 address is converted
2401 * into two h16 components, otherwise it's normalized and kept as is.
2403 * Ex: [::192.200.003.4] -> [::192.200.3.4]
2404 * [ffff::192.200.003.4] -> [ffff::c0c8:3041]
2407 * For unknown scheme types Windows simply copies the address over without any
2410 * IPv4 address can be included in an elision if all its components are 0's.
2412 static BOOL canonicalize_ipv6address(const parse_data *data, Uri *uri,
2413 DWORD flags, BOOL computeOnly) {
2414 uri->host_start = uri->canon_len;
2416 if(data->scheme_type == URL_SCHEME_UNKNOWN) {
2418 memcpy(uri->canon_uri+uri->canon_len, data->host, data->host_len*sizeof(WCHAR));
2419 uri->canon_len += data->host_len;
2423 DWORD i, elision_len;
2425 if(!ipv6_to_number(&(data->ipv6_address), values)) {
2426 TRACE("(%p %p %x %d): Failed to compute numerical value for IPv6 address.\n",
2427 data, uri, flags, computeOnly);
2432 uri->canon_uri[uri->canon_len] = '[';
2435 /* Find where the elision should occur (if any). */
2436 compute_elision_location(&(data->ipv6_address), values, &elision_start, &elision_len);
2438 TRACE("%p %p %x %d): Elision starts at %d, len=%u\n", data, uri, flags,
2439 computeOnly, elision_start, elision_len);
2441 for(i = 0; i < 8; ++i) {
2442 BOOL in_elision = (elision_start > -1 && i >= elision_start &&
2443 i < elision_start+elision_len);
2444 BOOL do_ipv4 = (i == 6 && data->ipv6_address.ipv4 && !in_elision &&
2445 data->ipv6_address.h16_count == 0);
2447 if(i == elision_start) {
2449 uri->canon_uri[uri->canon_len] = ':';
2450 uri->canon_uri[uri->canon_len+1] = ':';
2452 uri->canon_len += 2;
2455 /* We can ignore the current component if we're in the elision. */
2459 /* We only add a ':' if we're not at i == 0, or when we're at
2460 * the very end of elision range since the ':' colon was handled
2461 * earlier. Otherwise we would end up with ":::" after elision.
2463 if(i != 0 && !(elision_start > -1 && i == elision_start+elision_len)) {
2465 uri->canon_uri[uri->canon_len] = ':';
2473 /* Combine the two parts of the IPv4 address values. */
2479 len = ui2ipv4(uri->canon_uri+uri->canon_len, val);
2481 len = ui2ipv4(NULL, val);
2483 uri->canon_len += len;
2486 /* Write a regular h16 component to the URI. */
2488 /* Short circuit for the trivial case. */
2489 if(values[i] == 0) {
2491 uri->canon_uri[uri->canon_len] = '0';
2494 static const WCHAR formatW[] = {'%','x',0};
2497 uri->canon_len += sprintfW(uri->canon_uri+uri->canon_len,
2498 formatW, values[i]);
2501 uri->canon_len += sprintfW(tmp, formatW, values[i]);
2507 /* Add the closing ']'. */
2509 uri->canon_uri[uri->canon_len] = ']';
2513 uri->host_len = uri->canon_len - uri->host_start;
2516 TRACE("(%p %p %x %d): Canonicalized IPv6 address %s, len=%d\n", data, uri, flags,
2517 computeOnly, debugstr_wn(uri->canon_uri+uri->host_start, uri->host_len),
2523 /* Attempts to canonicalize the host of the URI (if any). */
2524 static BOOL canonicalize_host(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2525 uri->host_start = -1;
2527 uri->domain_offset = -1;
2530 switch(data->host_type) {
2532 uri->host_type = Uri_HOST_DNS;
2533 if(!canonicalize_reg_name(data, uri, flags, computeOnly))
2538 uri->host_type = Uri_HOST_IPV4;
2539 if(!canonicalize_ipv4address(data, uri, flags, computeOnly))
2544 if(!canonicalize_ipv6address(data, uri, flags, computeOnly))
2547 uri->host_type = Uri_HOST_IPV6;
2549 case Uri_HOST_UNKNOWN:
2550 if(data->host_len > 0 || data->scheme_type != URL_SCHEME_FILE) {
2551 uri->host_start = uri->canon_len;
2553 /* Nothing happens to unknown host types. */
2555 memcpy(uri->canon_uri+uri->canon_len, data->host, data->host_len*sizeof(WCHAR));
2556 uri->canon_len += data->host_len;
2557 uri->host_len = data->host_len;
2560 uri->host_type = Uri_HOST_UNKNOWN;
2563 FIXME("(%p %p %x %d): Canonicalization for host type %d not supported.\n", data,
2564 uri, flags, computeOnly, data->host_type);
2572 static BOOL canonicalize_port(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2573 BOOL has_default_port = FALSE;
2574 USHORT default_port = 0;
2577 uri->has_port = FALSE;
2579 /* Check if the scheme has a default port. */
2580 for(i = 0; i < sizeof(default_ports)/sizeof(default_ports[0]); ++i) {
2581 if(default_ports[i].scheme == data->scheme_type) {
2582 has_default_port = TRUE;
2583 default_port = default_ports[i].port;
2588 if(data->port || has_default_port)
2589 uri->has_port = TRUE;
2592 * 1) Has a port which is the default port.
2593 * 2) Has a port (not the default).
2594 * 3) Doesn't have a port, but, scheme has a default port.
2597 if(has_default_port && data->port && data->port_value == default_port) {
2598 /* If it's the default port and this flag isn't set, don't do anything. */
2599 if(flags & Uri_CREATE_NO_CANONICALIZE) {
2600 /* Copy the original port over. */
2602 uri->canon_uri[uri->canon_len] = ':';
2603 memcpy(uri->canon_uri+uri->canon_len+1, data->port, data->port_len*sizeof(WCHAR));
2605 uri->canon_len += data->port_len+1;
2608 uri->port = default_port;
2609 } else if(data->port) {
2611 uri->canon_uri[uri->canon_len] = ':';
2614 if(flags & Uri_CREATE_NO_CANONICALIZE) {
2615 /* Copy the original over without changes. */
2617 memcpy(uri->canon_uri+uri->canon_len, data->port, data->port_len*sizeof(WCHAR));
2618 uri->canon_len += data->port_len;
2620 const WCHAR formatW[] = {'%','u',0};
2623 len = sprintfW(uri->canon_uri+uri->canon_len, formatW, data->port_value);
2626 len = sprintfW(tmp, formatW, data->port_value);
2628 uri->canon_len += len;
2631 uri->port = data->port_value;
2632 } else if(has_default_port)
2633 uri->port = default_port;
2638 /* Canonicalizes the authority of the URI represented by the parse_data. */
2639 static BOOL canonicalize_authority(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2640 uri->authority_start = uri->canon_len;
2641 uri->authority_len = 0;
2643 if(!canonicalize_userinfo(data, uri, flags, computeOnly))
2646 if(!canonicalize_host(data, uri, flags, computeOnly))
2649 if(!canonicalize_port(data, uri, flags, computeOnly))
2652 if(uri->host_start != -1)
2653 uri->authority_len = uri->canon_len - uri->authority_start;
2655 uri->authority_start = -1;
2660 /* Attempts to canonicalize the path of a hierarchical URI.
2662 * Things that happen:
2663 * 1). Forbidden characters are percent encoded, unless the NO_ENCODE_FORBIDDEN
2664 * flag is set or it's a file URI. Forbidden characters are always encoded
2665 * for file schemes reguardless and forbidden characters are never encoded
2666 * for unknown scheme types.
2668 * 2). For known scheme types '\\' are changed to '/'.
2670 * 3). Percent encoded, unreserved characters are decoded to their actual values.
2671 * Unless the scheme type is unknown. For file schemes any percent encoded
2672 * character in the unreserved or reserved set is decoded.
2674 * 4). For File schemes if the path is starts with a drive letter and doesn't
2675 * start with a '/' then one is appended.
2676 * Ex: file://c:/test.mp3 -> file:///c:/test.mp3
2678 * 5). Dot segments are removed from the path for all scheme types
2679 * unless NO_CANONICALIZE flag is set. Dot segments aren't removed
2680 * for wildcard scheme types.
2683 * file://c:/test%20test -> file:///c:/test%2520test
2684 * file://c:/test%3Etest -> file:///c:/test%253Etest
2685 * file:///c:/test%20test -> file:///c:/test%20test
2686 * file:///c:/test%test -> file:///c:/test%25test
2688 static BOOL canonicalize_path_hierarchical(const parse_data *data, Uri *uri,
2689 DWORD flags, BOOL computeOnly) {
2691 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
2692 const BOOL is_file = data->scheme_type == URL_SCHEME_FILE;
2694 BOOL escape_pct = FALSE;
2697 uri->path_start = -1;
2702 uri->path_start = uri->canon_len;
2705 if(is_file && uri->host_start == -1) {
2706 /* Check if a '/' needs to be appended for the file scheme. */
2707 if(data->path_len > 1 && is_drive_path(ptr) && !(flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
2709 uri->canon_uri[uri->canon_len] = '/';
2712 } else if(*ptr == '/') {
2713 if(!(flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
2714 /* Copy the extra '/' over. */
2716 uri->canon_uri[uri->canon_len] = '/';
2722 if(is_drive_path(ptr)) {
2724 uri->canon_uri[uri->canon_len] = *ptr;
2725 /* If theres a '|' after the drive letter, convert it to a ':'. */
2726 uri->canon_uri[uri->canon_len+1] = ':';
2729 uri->canon_len += 2;
2733 for(; ptr < data->path+data->path_len; ++ptr) {
2735 const WCHAR *tmp = ptr;
2738 /* Check if the % represents a valid encoded char, or if it needs encoded. */
2739 BOOL force_encode = !check_pct_encoded(&tmp) && is_file;
2740 val = decode_pct_val(ptr);
2742 if(force_encode || escape_pct) {
2743 /* Escape the percent sign in the file URI. */
2745 pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
2746 uri->canon_len += 3;
2747 } else if((is_unreserved(val) && known_scheme) ||
2748 (is_file && (is_unreserved(val) || is_reserved(val)))) {
2750 uri->canon_uri[uri->canon_len] = val;
2757 uri->canon_uri[uri->canon_len] = *ptr;
2760 } else if(*ptr == '/' && is_file && (flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
2761 /* Convert the '/' back to a '\\'. */
2763 uri->canon_uri[uri->canon_len] = '\\';
2765 } else if(*ptr == '\\' && known_scheme) {
2766 if(is_file && (flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
2767 /* Don't convert the '\\' to a '/'. */
2769 uri->canon_uri[uri->canon_len] = *ptr;
2773 uri->canon_uri[uri->canon_len] = '/';
2776 } else if(known_scheme && !is_unreserved(*ptr) && !is_reserved(*ptr) &&
2777 (!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS) || is_file)) {
2778 if(is_file && (flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
2779 /* Don't escape the character. */
2781 uri->canon_uri[uri->canon_len] = *ptr;
2784 /* Escape the forbidden character. */
2786 pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
2787 uri->canon_len += 3;
2791 uri->canon_uri[uri->canon_len] = *ptr;
2796 uri->path_len = uri->canon_len - uri->path_start;
2798 /* Removing the dot segments only happens when it's not in
2799 * computeOnly mode and it's not a wildcard scheme. File schemes
2800 * with USE_DOS_PATH set don't get dot segments removed.
2802 if(!(is_file && (flags & Uri_CREATE_FILE_USE_DOS_PATH)) &&
2803 data->scheme_type != URL_SCHEME_WILDCARD) {
2804 if(!(flags & Uri_CREATE_NO_CANONICALIZE) && !computeOnly) {
2805 /* Remove the dot segments (if any) and reset everything to the new
2808 DWORD new_len = remove_dot_segments(uri->canon_uri+uri->path_start, uri->path_len);
2809 uri->canon_len -= uri->path_len-new_len;
2810 uri->path_len = new_len;
2815 TRACE("Canonicalized path %s len=%d\n",
2816 debugstr_wn(uri->canon_uri+uri->path_start, uri->path_len),
2822 /* Attempts to canonicalize the path for an opaque URI.
2824 * For known scheme types:
2825 * 1) forbidden characters are percent encoded if
2826 * NO_ENCODE_FORBIDDEN_CHARACTERS isn't set.
2828 * 2) Percent encoded, unreserved characters are decoded
2829 * to their actual values, for known scheme types.
2831 * 3) '\\' are changed to '/' for known scheme types
2832 * except for mailto schemes.
2834 * 4) For file schemes, if USE_DOS_PATH is set all '/'
2835 * are converted to backslashes.
2837 * 5) For file schemes, if USE_DOS_PATH isn't set all '\'
2838 * are converted to forward slashes.
2840 static BOOL canonicalize_path_opaque(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2842 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
2843 const BOOL is_file = data->scheme_type == URL_SCHEME_FILE;
2846 uri->path_start = -1;
2851 uri->path_start = uri->canon_len;
2853 /* Windows doesn't allow a "//" to appear after the scheme
2854 * of a URI, if it's an opaque URI.
2856 if(data->scheme && *(data->path) == '/' && *(data->path+1) == '/') {
2857 /* So it inserts a "/." before the "//" if it exists. */
2859 uri->canon_uri[uri->canon_len] = '/';
2860 uri->canon_uri[uri->canon_len+1] = '.';
2863 uri->canon_len += 2;
2866 for(ptr = data->path; ptr < data->path+data->path_len; ++ptr) {
2867 if(*ptr == '%' && known_scheme) {
2868 WCHAR val = decode_pct_val(ptr);
2870 if(is_unreserved(val)) {
2872 uri->canon_uri[uri->canon_len] = val;
2879 uri->canon_uri[uri->canon_len] = *ptr;
2882 } else if(*ptr == '/' && is_file && (flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
2884 uri->canon_uri[uri->canon_len] = '\\';
2886 } else if(*ptr == '\\' && is_file) {
2887 if(!(flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
2888 /* Convert to a '/'. */
2890 uri->canon_uri[uri->canon_len] = '/';
2893 /* Just copy it over. */
2895 uri->canon_uri[uri->canon_len] = *ptr;
2898 } else if(known_scheme && !is_unreserved(*ptr) && !is_reserved(*ptr) &&
2899 !(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS)) {
2900 if(is_file && (flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
2901 /* Forbidden characters aren't percent encoded for file schemes
2902 * with USE_DOS_PATH set.
2905 uri->canon_uri[uri->canon_len] = *ptr;
2909 pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
2910 uri->canon_len += 3;
2914 uri->canon_uri[uri->canon_len] = *ptr;
2919 uri->path_len = uri->canon_len - uri->path_start;
2921 TRACE("(%p %p %x %d): Canonicalized opaque URI path %s len=%d\n", data, uri, flags, computeOnly,
2922 debugstr_wn(uri->canon_uri+uri->path_start, uri->path_len), uri->path_len);
2926 /* Determines how the URI represented by the parse_data should be canonicalized.
2928 * Essentially, if the parse_data represents an hierarchical URI then it calls
2929 * canonicalize_authority and the canonicalization functions for the path. If the
2930 * URI is opaque it canonicalizes the path of the URI.
2932 static BOOL canonicalize_hierpart(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2933 uri->display_absolute = TRUE;
2935 if(!data->is_opaque) {
2936 /* "//" is only added for non-wildcard scheme types. */
2937 if(data->scheme_type != URL_SCHEME_WILDCARD) {
2939 INT pos = uri->canon_len;
2941 uri->canon_uri[pos] = '/';
2942 uri->canon_uri[pos+1] = '/';
2944 uri->canon_len += 2;
2947 if(!canonicalize_authority(data, uri, flags, computeOnly))
2950 /* TODO: Canonicalize the path of the URI. */
2951 if(!canonicalize_path_hierarchical(data, uri, flags, computeOnly))
2955 /* Opaque URI's don't have an authority. */
2956 uri->userinfo_start = uri->userinfo_split = -1;
2957 uri->userinfo_len = 0;
2958 uri->host_start = -1;
2960 uri->host_type = Uri_HOST_UNKNOWN;
2961 uri->has_port = FALSE;
2962 uri->authority_start = -1;
2963 uri->authority_len = 0;
2964 uri->domain_offset = -1;
2966 if(is_hierarchical_scheme(data->scheme_type)) {
2969 /* Absolute URIs aren't displayed for known scheme types
2970 * which should be hierarchical URIs.
2972 uri->display_absolute = FALSE;
2974 /* Windows also sets the port for these (if they have one). */
2975 for(i = 0; i < sizeof(default_ports)/sizeof(default_ports[0]); ++i) {
2976 if(data->scheme_type == default_ports[i].scheme) {
2977 uri->has_port = TRUE;
2978 uri->port = default_ports[i].port;
2984 if(!canonicalize_path_opaque(data, uri, flags, computeOnly))
2988 if(uri->path_start > -1 && !computeOnly)
2989 /* Finding file extensions happens for both types of URIs. */
2990 uri->extension_offset = find_file_extension(uri->canon_uri+uri->path_start, uri->path_len);
2992 uri->extension_offset = -1;
2997 /* Attempts to canonicalize the query string of the URI.
2999 * Things that happen:
3000 * 1) For known scheme types forbidden characters
3001 * are percent encoded, unless the NO_DECODE_EXTRA_INFO flag is set
3002 * or NO_ENCODE_FORBIDDEN_CHARACTERS is set.
3004 * 2) For known scheme types, percent encoded, unreserved characters
3005 * are decoded as long as the NO_DECODE_EXTRA_INFO flag isn't set.
3007 static BOOL canonicalize_query(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
3008 const WCHAR *ptr, *end;
3009 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
3012 uri->query_start = -1;
3017 uri->query_start = uri->canon_len;
3019 end = data->query+data->query_len;
3020 for(ptr = data->query; ptr < end; ++ptr) {
3022 if(known_scheme && !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
3023 WCHAR val = decode_pct_val(ptr);
3024 if(is_unreserved(val)) {
3026 uri->canon_uri[uri->canon_len] = val;
3033 } else if(known_scheme && !is_unreserved(*ptr) && !is_reserved(*ptr)) {
3034 if(!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS) &&
3035 !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
3037 pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
3038 uri->canon_len += 3;
3044 uri->canon_uri[uri->canon_len] = *ptr;
3048 uri->query_len = uri->canon_len - uri->query_start;
3051 TRACE("(%p %p %x %d): Canonicalized query string %s len=%d\n", data, uri, flags,
3052 computeOnly, debugstr_wn(uri->canon_uri+uri->query_start, uri->query_len),
3057 static BOOL canonicalize_fragment(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
3058 const WCHAR *ptr, *end;
3059 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
3061 if(!data->fragment) {
3062 uri->fragment_start = -1;
3063 uri->fragment_len = 0;
3067 uri->fragment_start = uri->canon_len;
3069 end = data->fragment + data->fragment_len;
3070 for(ptr = data->fragment; ptr < end; ++ptr) {
3072 if(known_scheme && !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
3073 WCHAR val = decode_pct_val(ptr);
3074 if(is_unreserved(val)) {
3076 uri->canon_uri[uri->canon_len] = val;
3083 } else if(known_scheme && !is_unreserved(*ptr) && !is_reserved(*ptr)) {
3084 if(!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS) &&
3085 !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
3087 pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
3088 uri->canon_len += 3;
3094 uri->canon_uri[uri->canon_len] = *ptr;
3098 uri->fragment_len = uri->canon_len - uri->fragment_start;
3101 TRACE("(%p %p %x %d): Canonicalized fragment %s len=%d\n", data, uri, flags,
3102 computeOnly, debugstr_wn(uri->canon_uri+uri->fragment_start, uri->fragment_len),
3107 /* Canonicalizes the scheme information specified in the parse_data using the specified flags. */
3108 static BOOL canonicalize_scheme(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
3109 uri->scheme_start = -1;
3110 uri->scheme_len = 0;
3113 /* The only type of URI that doesn't have to have a scheme is a relative
3116 if(!data->is_relative) {
3117 FIXME("(%p %p %x): Unable to determine the scheme type of %s.\n", data,
3118 uri, flags, debugstr_w(data->uri));
3124 INT pos = uri->canon_len;
3126 for(i = 0; i < data->scheme_len; ++i) {
3127 /* Scheme name must be lower case after canonicalization. */
3128 uri->canon_uri[i + pos] = tolowerW(data->scheme[i]);
3131 uri->canon_uri[i + pos] = ':';
3132 uri->scheme_start = pos;
3134 TRACE("(%p %p %x): Canonicalized scheme=%s, len=%d.\n", data, uri, flags,
3135 debugstr_wn(uri->canon_uri, uri->scheme_len), data->scheme_len);
3138 /* This happens in both computation modes. */
3139 uri->canon_len += data->scheme_len + 1;
3140 uri->scheme_len = data->scheme_len;
3145 /* Compute's what the length of the URI specified by the parse_data will be
3146 * after canonicalization occurs using the specified flags.
3148 * This function will return a non-zero value indicating the length of the canonicalized
3149 * URI, or -1 on error.
3151 static int compute_canonicalized_length(const parse_data *data, DWORD flags) {
3154 memset(&uri, 0, sizeof(Uri));
3156 TRACE("(%p %x): Beginning to compute canonicalized length for URI %s\n", data, flags,
3157 debugstr_w(data->uri));
3159 if(!canonicalize_scheme(data, &uri, flags, TRUE)) {
3160 ERR("(%p %x): Failed to compute URI scheme length.\n", data, flags);
3164 if(!canonicalize_hierpart(data, &uri, flags, TRUE)) {
3165 ERR("(%p %x): Failed to compute URI hierpart length.\n", data, flags);
3169 if(!canonicalize_query(data, &uri, flags, TRUE)) {
3170 ERR("(%p %x): Failed to compute query string length.\n", data, flags);
3174 if(!canonicalize_fragment(data, &uri, flags, TRUE)) {
3175 ERR("(%p %x): Failed to compute fragment length.\n", data, flags);
3179 TRACE("(%p %x): Finished computing canonicalized URI length. length=%d\n", data, flags, uri.canon_len);
3181 return uri.canon_len;
3184 /* Canonicalizes the URI data specified in the parse_data, using the given flags. If the
3185 * canonicalization succeededs it will store all the canonicalization information
3186 * in the pointer to the Uri.
3188 * To canonicalize a URI this function first computes what the length of the URI
3189 * specified by the parse_data will be. Once this is done it will then perfom the actual
3190 * canonicalization of the URI.
3192 static HRESULT canonicalize_uri(const parse_data *data, Uri *uri, DWORD flags) {
3195 uri->canon_uri = NULL;
3196 len = uri->canon_size = uri->canon_len = 0;
3198 TRACE("(%p %p %x): beginning to canonicalize URI %s.\n", data, uri, flags, debugstr_w(data->uri));
3200 /* First try to compute the length of the URI. */
3201 len = compute_canonicalized_length(data, flags);
3203 ERR("(%p %p %x): Could not compute the canonicalized length of %s.\n", data, uri, flags,
3204 debugstr_w(data->uri));
3205 return E_INVALIDARG;
3208 uri->canon_uri = heap_alloc((len+1)*sizeof(WCHAR));
3210 return E_OUTOFMEMORY;
3212 uri->canon_size = len;
3213 if(!canonicalize_scheme(data, uri, flags, FALSE)) {
3214 ERR("(%p %p %x): Unable to canonicalize the scheme of the URI.\n", data, uri, flags);
3215 heap_free(uri->canon_uri);
3216 return E_INVALIDARG;
3218 uri->scheme_type = data->scheme_type;
3220 if(!canonicalize_hierpart(data, uri, flags, FALSE)) {
3221 ERR("(%p %p %x): Unable to canonicalize the heirpart of the URI\n", data, uri, flags);
3222 heap_free(uri->canon_uri);
3223 return E_INVALIDARG;
3226 if(!canonicalize_query(data, uri, flags, FALSE)) {
3227 ERR("(%p %p %x): Unable to canonicalize query string of the URI.\n",
3229 return E_INVALIDARG;
3232 if(!canonicalize_fragment(data, uri, flags, FALSE)) {
3233 ERR("(%p %p %x): Unable to canonicalize fragment of the URI.\n",
3235 return E_INVALIDARG;
3238 /* There's a possibility we didn't use all the space we allocated
3241 if(uri->canon_len < uri->canon_size) {
3242 /* This happens if the URI is hierarchical and dot
3243 * segments were removed from it's path.
3245 WCHAR *tmp = heap_realloc(uri->canon_uri, (uri->canon_len+1)*sizeof(WCHAR));
3247 return E_OUTOFMEMORY;
3249 uri->canon_uri = tmp;
3250 uri->canon_size = uri->canon_len;
3253 uri->canon_uri[uri->canon_len] = '\0';
3254 TRACE("(%p %p %x): finished canonicalizing the URI. uri=%s\n", data, uri, flags, debugstr_w(uri->canon_uri));
3259 static HRESULT get_builder_component(LPWSTR *component, DWORD *component_len,
3260 LPCWSTR source, DWORD source_len,
3261 LPCWSTR *output, DWORD *output_len)
3274 if(!(*component) && source) {
3275 /* Allocate 'component', and copy the contents from 'source'
3276 * into the new allocation.
3278 *component = heap_alloc((source_len+1)*sizeof(WCHAR));
3280 return E_OUTOFMEMORY;
3282 memcpy(*component, source, source_len*sizeof(WCHAR));
3283 (*component)[source_len] = '\0';
3284 *component_len = source_len;
3287 *output = *component;
3288 *output_len = *component_len;
3289 return *output ? S_OK : S_FALSE;
3292 /* Allocates 'component' and copies the string from 'new_value' into 'component'.
3293 * If 'prefix' is set and 'new_value' isn't NULL, then it checks if 'new_value'
3294 * starts with 'prefix'. If it doesn't then 'prefix' is prepended to 'component'.
3296 static HRESULT set_builder_component(LPWSTR *component, DWORD *component_len, LPCWSTR new_value,
3300 heap_free(*component);
3306 BOOL add_prefix = FALSE;
3307 DWORD len = lstrlenW(new_value);
3310 if(prefix && *new_value != prefix) {
3312 *component = heap_alloc((len+2)*sizeof(WCHAR));
3314 *component = heap_alloc((len+1)*sizeof(WCHAR));
3317 return E_OUTOFMEMORY;
3320 (*component)[pos++] = prefix;
3322 memcpy(*component+pos, new_value, (len+1)*sizeof(WCHAR));
3323 *component_len = len+pos;
3329 #define URI(x) ((IUri*) &(x)->lpIUriVtbl)
3330 #define URIBUILDER(x) ((IUriBuilder*) &(x)->lpIUriBuilderVtbl)
3332 #define URI_THIS(iface) DEFINE_THIS(Uri, IUri, iface)
3334 static HRESULT WINAPI Uri_QueryInterface(IUri *iface, REFIID riid, void **ppv)
3336 Uri *This = URI_THIS(iface);
3338 if(IsEqualGUID(&IID_IUnknown, riid)) {
3339 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
3341 }else if(IsEqualGUID(&IID_IUri, riid)) {
3342 TRACE("(%p)->(IID_IUri %p)\n", This, ppv);
3344 }else if(IsEqualGUID(&IID_IUriObj, riid)) {
3345 TRACE("(%p)->(IID_IUriObj %p)\n", This, ppv);
3349 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
3351 return E_NOINTERFACE;
3354 IUnknown_AddRef((IUnknown*)*ppv);
3358 static ULONG WINAPI Uri_AddRef(IUri *iface)
3360 Uri *This = URI_THIS(iface);
3361 LONG ref = InterlockedIncrement(&This->ref);
3363 TRACE("(%p) ref=%d\n", This, ref);
3368 static ULONG WINAPI Uri_Release(IUri *iface)
3370 Uri *This = URI_THIS(iface);
3371 LONG ref = InterlockedDecrement(&This->ref);
3373 TRACE("(%p) ref=%d\n", This, ref);
3376 SysFreeString(This->raw_uri);
3377 heap_free(This->canon_uri);
3384 static HRESULT WINAPI Uri_GetPropertyBSTR(IUri *iface, Uri_PROPERTY uriProp, BSTR *pbstrProperty, DWORD dwFlags)
3386 Uri *This = URI_THIS(iface);
3388 TRACE("(%p)->(%d %p %x)\n", This, uriProp, pbstrProperty, dwFlags);
3393 if(uriProp > Uri_PROPERTY_STRING_LAST) {
3394 /* Windows allocates an empty BSTR for invalid Uri_PROPERTY's. */
3395 *pbstrProperty = SysAllocStringLen(NULL, 0);
3396 if(!(*pbstrProperty))
3397 return E_OUTOFMEMORY;
3399 /* It only returns S_FALSE for the ZONE property... */
3400 if(uriProp == Uri_PROPERTY_ZONE)
3406 /* Don't have support for flags yet. */
3408 FIXME("(%p)->(%d %p %x)\n", This, uriProp, pbstrProperty, dwFlags);
3413 case Uri_PROPERTY_ABSOLUTE_URI:
3414 if(!This->display_absolute) {
3415 *pbstrProperty = SysAllocStringLen(NULL, 0);
3418 *pbstrProperty = SysAllocString(This->canon_uri);
3422 if(!(*pbstrProperty))
3423 hres = E_OUTOFMEMORY;
3426 case Uri_PROPERTY_AUTHORITY:
3427 if(This->authority_start > -1) {
3428 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->authority_start, This->authority_len);
3431 *pbstrProperty = SysAllocStringLen(NULL, 0);
3435 if(!(*pbstrProperty))
3436 hres = E_OUTOFMEMORY;
3439 case Uri_PROPERTY_DISPLAY_URI:
3440 /* The Display URI contains everything except for the userinfo for known
3443 if(This->scheme_type != URL_SCHEME_UNKNOWN && This->userinfo_start > -1) {
3444 *pbstrProperty = SysAllocStringLen(NULL, This->canon_len-This->userinfo_len);
3446 if(*pbstrProperty) {
3447 /* Copy everything before the userinfo over. */
3448 memcpy(*pbstrProperty, This->canon_uri, This->userinfo_start*sizeof(WCHAR));
3449 /* Copy everything after the userinfo over. */
3450 memcpy(*pbstrProperty+This->userinfo_start,
3451 This->canon_uri+This->userinfo_start+This->userinfo_len+1,
3452 (This->canon_len-(This->userinfo_start+This->userinfo_len+1))*sizeof(WCHAR));
3455 *pbstrProperty = SysAllocString(This->canon_uri);
3457 if(!(*pbstrProperty))
3458 hres = E_OUTOFMEMORY;
3463 case Uri_PROPERTY_DOMAIN:
3464 if(This->domain_offset > -1) {
3465 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->host_start+This->domain_offset,
3466 This->host_len-This->domain_offset);
3469 *pbstrProperty = SysAllocStringLen(NULL, 0);
3473 if(!(*pbstrProperty))
3474 hres = E_OUTOFMEMORY;
3477 case Uri_PROPERTY_EXTENSION:
3478 if(This->extension_offset > -1) {
3479 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->path_start+This->extension_offset,
3480 This->path_len-This->extension_offset);
3483 *pbstrProperty = SysAllocStringLen(NULL, 0);
3487 if(!(*pbstrProperty))
3488 hres = E_OUTOFMEMORY;
3491 case Uri_PROPERTY_FRAGMENT:
3492 if(This->fragment_start > -1) {
3493 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->fragment_start, This->fragment_len);
3496 *pbstrProperty = SysAllocStringLen(NULL, 0);
3500 if(!(*pbstrProperty))
3501 hres = E_OUTOFMEMORY;
3504 case Uri_PROPERTY_HOST:
3505 if(This->host_start > -1) {
3506 /* The '[' and ']' aren't included for IPv6 addresses. */
3507 if(This->host_type == Uri_HOST_IPV6)
3508 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->host_start+1, This->host_len-2);
3510 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->host_start, This->host_len);
3514 *pbstrProperty = SysAllocStringLen(NULL, 0);
3518 if(!(*pbstrProperty))
3519 hres = E_OUTOFMEMORY;
3522 case Uri_PROPERTY_PASSWORD:
3523 if(This->userinfo_split > -1) {
3524 *pbstrProperty = SysAllocStringLen(
3525 This->canon_uri+This->userinfo_start+This->userinfo_split+1,
3526 This->userinfo_len-This->userinfo_split-1);
3529 *pbstrProperty = SysAllocStringLen(NULL, 0);
3533 if(!(*pbstrProperty))
3534 return E_OUTOFMEMORY;
3537 case Uri_PROPERTY_PATH:
3538 if(This->path_start > -1) {
3539 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->path_start, This->path_len);
3542 *pbstrProperty = SysAllocStringLen(NULL, 0);
3546 if(!(*pbstrProperty))
3547 hres = E_OUTOFMEMORY;
3550 case Uri_PROPERTY_PATH_AND_QUERY:
3551 if(This->path_start > -1) {
3552 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->path_start, This->path_len+This->query_len);
3554 } else if(This->query_start > -1) {
3555 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->query_start, This->query_len);
3558 *pbstrProperty = SysAllocStringLen(NULL, 0);
3562 if(!(*pbstrProperty))
3563 hres = E_OUTOFMEMORY;
3566 case Uri_PROPERTY_QUERY:
3567 if(This->query_start > -1) {
3568 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->query_start, This->query_len);
3571 *pbstrProperty = SysAllocStringLen(NULL, 0);
3575 if(!(*pbstrProperty))
3576 hres = E_OUTOFMEMORY;
3579 case Uri_PROPERTY_RAW_URI:
3580 *pbstrProperty = SysAllocString(This->raw_uri);
3581 if(!(*pbstrProperty))
3582 hres = E_OUTOFMEMORY;
3586 case Uri_PROPERTY_SCHEME_NAME:
3587 if(This->scheme_start > -1) {
3588 *pbstrProperty = SysAllocStringLen(This->canon_uri + This->scheme_start, This->scheme_len);
3591 *pbstrProperty = SysAllocStringLen(NULL, 0);
3595 if(!(*pbstrProperty))
3596 hres = E_OUTOFMEMORY;
3599 case Uri_PROPERTY_USER_INFO:
3600 if(This->userinfo_start > -1) {
3601 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->userinfo_start, This->userinfo_len);
3604 *pbstrProperty = SysAllocStringLen(NULL, 0);
3608 if(!(*pbstrProperty))
3609 hres = E_OUTOFMEMORY;
3612 case Uri_PROPERTY_USER_NAME:
3613 if(This->userinfo_start > -1) {
3614 /* If userinfo_split is set, that means a password exists
3615 * so the username is only from userinfo_start to userinfo_split.
3617 if(This->userinfo_split > -1) {
3618 *pbstrProperty = SysAllocStringLen(This->canon_uri + This->userinfo_start, This->userinfo_split);
3621 *pbstrProperty = SysAllocStringLen(This->canon_uri + This->userinfo_start, This->userinfo_len);
3625 *pbstrProperty = SysAllocStringLen(NULL, 0);
3629 if(!(*pbstrProperty))
3630 return E_OUTOFMEMORY;
3634 FIXME("(%p)->(%d %p %x)\n", This, uriProp, pbstrProperty, dwFlags);
3641 static HRESULT WINAPI Uri_GetPropertyLength(IUri *iface, Uri_PROPERTY uriProp, DWORD *pcchProperty, DWORD dwFlags)
3643 Uri *This = URI_THIS(iface);
3645 TRACE("(%p)->(%d %p %x)\n", This, uriProp, pcchProperty, dwFlags);
3648 return E_INVALIDARG;
3650 /* Can only return a length for a property if it's a string. */
3651 if(uriProp > Uri_PROPERTY_STRING_LAST)
3652 return E_INVALIDARG;
3654 /* Don't have support for flags yet. */
3656 FIXME("(%p)->(%d %p %x)\n", This, uriProp, pcchProperty, dwFlags);
3661 case Uri_PROPERTY_ABSOLUTE_URI:
3662 if(!This->display_absolute) {
3666 *pcchProperty = This->canon_len;
3671 case Uri_PROPERTY_AUTHORITY:
3672 *pcchProperty = This->authority_len;
3673 hres = (This->authority_start > -1) ? S_OK : S_FALSE;
3675 case Uri_PROPERTY_DISPLAY_URI:
3676 if(This->scheme_type != URL_SCHEME_UNKNOWN && This->userinfo_start > -1)
3677 *pcchProperty = This->canon_len-This->userinfo_len-1;
3679 *pcchProperty = This->canon_len;
3683 case Uri_PROPERTY_DOMAIN:
3684 if(This->domain_offset > -1)
3685 *pcchProperty = This->host_len - This->domain_offset;
3689 hres = (This->domain_offset > -1) ? S_OK : S_FALSE;
3691 case Uri_PROPERTY_EXTENSION:
3692 if(This->extension_offset > -1) {
3693 *pcchProperty = This->path_len - This->extension_offset;
3701 case Uri_PROPERTY_FRAGMENT:
3702 *pcchProperty = This->fragment_len;
3703 hres = (This->fragment_start > -1) ? S_OK : S_FALSE;
3705 case Uri_PROPERTY_HOST:
3706 *pcchProperty = This->host_len;
3708 /* '[' and ']' aren't included in the length. */
3709 if(This->host_type == Uri_HOST_IPV6)
3712 hres = (This->host_start > -1) ? S_OK : S_FALSE;
3714 case Uri_PROPERTY_PASSWORD:
3715 *pcchProperty = (This->userinfo_split > -1) ? This->userinfo_len-This->userinfo_split-1 : 0;
3716 hres = (This->userinfo_split > -1) ? S_OK : S_FALSE;
3718 case Uri_PROPERTY_PATH:
3719 *pcchProperty = This->path_len;
3720 hres = (This->path_start > -1) ? S_OK : S_FALSE;
3722 case Uri_PROPERTY_PATH_AND_QUERY:
3723 *pcchProperty = This->path_len+This->query_len;
3724 hres = (This->path_start > -1 || This->query_start > -1) ? S_OK : S_FALSE;
3726 case Uri_PROPERTY_QUERY:
3727 *pcchProperty = This->query_len;
3728 hres = (This->query_start > -1) ? S_OK : S_FALSE;
3730 case Uri_PROPERTY_RAW_URI:
3731 *pcchProperty = SysStringLen(This->raw_uri);
3734 case Uri_PROPERTY_SCHEME_NAME:
3735 *pcchProperty = This->scheme_len;
3736 hres = (This->scheme_start > -1) ? S_OK : S_FALSE;
3738 case Uri_PROPERTY_USER_INFO:
3739 *pcchProperty = This->userinfo_len;
3740 hres = (This->userinfo_start > -1) ? S_OK : S_FALSE;
3742 case Uri_PROPERTY_USER_NAME:
3743 *pcchProperty = (This->userinfo_split > -1) ? This->userinfo_split : This->userinfo_len;
3744 hres = (This->userinfo_start > -1) ? S_OK : S_FALSE;
3747 FIXME("(%p)->(%d %p %x)\n", This, uriProp, pcchProperty, dwFlags);
3754 static HRESULT WINAPI Uri_GetPropertyDWORD(IUri *iface, Uri_PROPERTY uriProp, DWORD *pcchProperty, DWORD dwFlags)
3756 Uri *This = URI_THIS(iface);
3759 TRACE("(%p)->(%d %p %x)\n", This, uriProp, pcchProperty, dwFlags);
3762 return E_INVALIDARG;
3764 /* Microsoft's implementation for the ZONE property of a URI seems to be lacking...
3765 * From what I can tell, instead of checking which URLZONE the URI belongs to it
3766 * simply assigns URLZONE_INVALID and returns E_NOTIMPL. This also applies to the GetZone
3769 if(uriProp == Uri_PROPERTY_ZONE) {
3770 *pcchProperty = URLZONE_INVALID;
3774 if(uriProp < Uri_PROPERTY_DWORD_START) {
3776 return E_INVALIDARG;
3780 case Uri_PROPERTY_HOST_TYPE:
3781 *pcchProperty = This->host_type;
3784 case Uri_PROPERTY_PORT:
3785 if(!This->has_port) {
3789 *pcchProperty = This->port;
3794 case Uri_PROPERTY_SCHEME:
3795 *pcchProperty = This->scheme_type;
3799 FIXME("(%p)->(%d %p %x)\n", This, uriProp, pcchProperty, dwFlags);
3806 static HRESULT WINAPI Uri_HasProperty(IUri *iface, Uri_PROPERTY uriProp, BOOL *pfHasProperty)
3808 Uri *This = URI_THIS(iface);
3809 TRACE("(%p)->(%d %p)\n", This, uriProp, pfHasProperty);
3812 return E_INVALIDARG;
3815 case Uri_PROPERTY_ABSOLUTE_URI:
3816 *pfHasProperty = This->display_absolute;
3818 case Uri_PROPERTY_AUTHORITY:
3819 *pfHasProperty = This->authority_start > -1;
3821 case Uri_PROPERTY_DISPLAY_URI:
3822 *pfHasProperty = TRUE;
3824 case Uri_PROPERTY_DOMAIN:
3825 *pfHasProperty = This->domain_offset > -1;
3827 case Uri_PROPERTY_EXTENSION:
3828 *pfHasProperty = This->extension_offset > -1;
3830 case Uri_PROPERTY_FRAGMENT:
3831 *pfHasProperty = This->fragment_start > -1;
3833 case Uri_PROPERTY_HOST:
3834 *pfHasProperty = This->host_start > -1;
3836 case Uri_PROPERTY_PASSWORD:
3837 *pfHasProperty = This->userinfo_split > -1;
3839 case Uri_PROPERTY_PATH:
3840 *pfHasProperty = This->path_start > -1;
3842 case Uri_PROPERTY_PATH_AND_QUERY:
3843 *pfHasProperty = (This->path_start > -1 || This->query_start > -1);
3845 case Uri_PROPERTY_QUERY:
3846 *pfHasProperty = This->query_start > -1;
3848 case Uri_PROPERTY_RAW_URI:
3849 *pfHasProperty = TRUE;
3851 case Uri_PROPERTY_SCHEME_NAME:
3852 *pfHasProperty = This->scheme_start > -1;
3854 case Uri_PROPERTY_USER_INFO:
3855 case Uri_PROPERTY_USER_NAME:
3856 *pfHasProperty = This->userinfo_start > -1;
3858 case Uri_PROPERTY_HOST_TYPE:
3859 *pfHasProperty = TRUE;
3861 case Uri_PROPERTY_PORT:
3862 *pfHasProperty = This->has_port;
3864 case Uri_PROPERTY_SCHEME:
3865 *pfHasProperty = TRUE;
3867 case Uri_PROPERTY_ZONE:
3868 *pfHasProperty = FALSE;
3871 FIXME("(%p)->(%d %p): Unsupported property type.\n", This, uriProp, pfHasProperty);
3878 static HRESULT WINAPI Uri_GetAbsoluteUri(IUri *iface, BSTR *pstrAbsoluteUri)
3880 TRACE("(%p)->(%p)\n", iface, pstrAbsoluteUri);
3881 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_ABSOLUTE_URI, pstrAbsoluteUri, 0);
3884 static HRESULT WINAPI Uri_GetAuthority(IUri *iface, BSTR *pstrAuthority)
3886 TRACE("(%p)->(%p)\n", iface, pstrAuthority);
3887 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_AUTHORITY, pstrAuthority, 0);
3890 static HRESULT WINAPI Uri_GetDisplayUri(IUri *iface, BSTR *pstrDisplayUri)
3892 TRACE("(%p)->(%p)\n", iface, pstrDisplayUri);
3893 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_DISPLAY_URI, pstrDisplayUri, 0);
3896 static HRESULT WINAPI Uri_GetDomain(IUri *iface, BSTR *pstrDomain)
3898 TRACE("(%p)->(%p)\n", iface, pstrDomain);
3899 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_DOMAIN, pstrDomain, 0);
3902 static HRESULT WINAPI Uri_GetExtension(IUri *iface, BSTR *pstrExtension)
3904 TRACE("(%p)->(%p)\n", iface, pstrExtension);
3905 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_EXTENSION, pstrExtension, 0);
3908 static HRESULT WINAPI Uri_GetFragment(IUri *iface, BSTR *pstrFragment)
3910 TRACE("(%p)->(%p)\n", iface, pstrFragment);
3911 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_FRAGMENT, pstrFragment, 0);
3914 static HRESULT WINAPI Uri_GetHost(IUri *iface, BSTR *pstrHost)
3916 TRACE("(%p)->(%p)\n", iface, pstrHost);
3917 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_HOST, pstrHost, 0);
3920 static HRESULT WINAPI Uri_GetPassword(IUri *iface, BSTR *pstrPassword)
3922 TRACE("(%p)->(%p)\n", iface, pstrPassword);
3923 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_PASSWORD, pstrPassword, 0);
3926 static HRESULT WINAPI Uri_GetPath(IUri *iface, BSTR *pstrPath)
3928 TRACE("(%p)->(%p)\n", iface, pstrPath);
3929 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_PATH, pstrPath, 0);
3932 static HRESULT WINAPI Uri_GetPathAndQuery(IUri *iface, BSTR *pstrPathAndQuery)
3934 TRACE("(%p)->(%p)\n", iface, pstrPathAndQuery);
3935 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_PATH_AND_QUERY, pstrPathAndQuery, 0);
3938 static HRESULT WINAPI Uri_GetQuery(IUri *iface, BSTR *pstrQuery)
3940 TRACE("(%p)->(%p)\n", iface, pstrQuery);
3941 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_QUERY, pstrQuery, 0);
3944 static HRESULT WINAPI Uri_GetRawUri(IUri *iface, BSTR *pstrRawUri)
3946 Uri *This = URI_THIS(iface);
3947 TRACE("(%p)->(%p)\n", This, pstrRawUri);
3949 /* Just forward the call to GetPropertyBSTR. */
3950 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_RAW_URI, pstrRawUri, 0);
3953 static HRESULT WINAPI Uri_GetSchemeName(IUri *iface, BSTR *pstrSchemeName)
3955 Uri *This = URI_THIS(iface);
3956 TRACE("(%p)->(%p)\n", This, pstrSchemeName);
3957 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_SCHEME_NAME, pstrSchemeName, 0);
3960 static HRESULT WINAPI Uri_GetUserInfo(IUri *iface, BSTR *pstrUserInfo)
3962 TRACE("(%p)->(%p)\n", iface, pstrUserInfo);
3963 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_USER_INFO, pstrUserInfo, 0);
3966 static HRESULT WINAPI Uri_GetUserName(IUri *iface, BSTR *pstrUserName)
3968 TRACE("(%p)->(%p)\n", iface, pstrUserName);
3969 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_USER_NAME, pstrUserName, 0);
3972 static HRESULT WINAPI Uri_GetHostType(IUri *iface, DWORD *pdwHostType)
3974 TRACE("(%p)->(%p)\n", iface, pdwHostType);
3975 return Uri_GetPropertyDWORD(iface, Uri_PROPERTY_HOST_TYPE, pdwHostType, 0);
3978 static HRESULT WINAPI Uri_GetPort(IUri *iface, DWORD *pdwPort)
3980 TRACE("(%p)->(%p)\n", iface, pdwPort);
3981 return Uri_GetPropertyDWORD(iface, Uri_PROPERTY_PORT, pdwPort, 0);
3984 static HRESULT WINAPI Uri_GetScheme(IUri *iface, DWORD *pdwScheme)
3986 Uri *This = URI_THIS(iface);
3987 TRACE("(%p)->(%p)\n", This, pdwScheme);
3988 return Uri_GetPropertyDWORD(iface, Uri_PROPERTY_SCHEME, pdwScheme, 0);
3991 static HRESULT WINAPI Uri_GetZone(IUri *iface, DWORD *pdwZone)
3993 TRACE("(%p)->(%p)\n", iface, pdwZone);
3994 return Uri_GetPropertyDWORD(iface, Uri_PROPERTY_ZONE,pdwZone, 0);
3997 static HRESULT WINAPI Uri_GetProperties(IUri *iface, DWORD *pdwProperties)
3999 Uri *This = URI_THIS(iface);
4000 TRACE("(%p)->(%p)\n", This, pdwProperties);
4003 return E_INVALIDARG;
4005 /* All URIs have these. */
4006 *pdwProperties = Uri_HAS_DISPLAY_URI|Uri_HAS_RAW_URI|Uri_HAS_SCHEME|Uri_HAS_HOST_TYPE;
4008 if(This->display_absolute)
4009 *pdwProperties |= Uri_HAS_ABSOLUTE_URI;
4011 if(This->scheme_start > -1)
4012 *pdwProperties |= Uri_HAS_SCHEME_NAME;
4014 if(This->authority_start > -1) {
4015 *pdwProperties |= Uri_HAS_AUTHORITY;
4016 if(This->userinfo_start > -1)
4017 *pdwProperties |= Uri_HAS_USER_INFO|Uri_HAS_USER_NAME;
4018 if(This->userinfo_split > -1)
4019 *pdwProperties |= Uri_HAS_PASSWORD;
4020 if(This->host_start > -1)
4021 *pdwProperties |= Uri_HAS_HOST;
4022 if(This->domain_offset > -1)
4023 *pdwProperties |= Uri_HAS_DOMAIN;
4027 *pdwProperties |= Uri_HAS_PORT;
4028 if(This->path_start > -1)
4029 *pdwProperties |= Uri_HAS_PATH|Uri_HAS_PATH_AND_QUERY;
4030 if(This->query_start > -1)
4031 *pdwProperties |= Uri_HAS_QUERY|Uri_HAS_PATH_AND_QUERY;
4033 if(This->extension_offset > -1)
4034 *pdwProperties |= Uri_HAS_EXTENSION;
4036 if(This->fragment_start > -1)
4037 *pdwProperties |= Uri_HAS_FRAGMENT;
4042 static HRESULT WINAPI Uri_IsEqual(IUri *iface, IUri *pUri, BOOL *pfEqual)
4044 Uri *This = URI_THIS(iface);
4047 TRACE("(%p)->(%p %p)\n", This, pUri, pfEqual);
4055 /* For some reason Windows returns S_OK here... */
4059 /* Try to convert it to a Uri (allows for a more simple comparison). */
4060 if((other = get_uri_obj(pUri)))
4061 *pfEqual = are_equal_simple(This, other);
4063 /* Do it the hard way. */
4064 FIXME("(%p)->(%p %p) No support for unknown IUri's yet.\n", iface, pUri, pfEqual);
4073 static const IUriVtbl UriVtbl = {
4077 Uri_GetPropertyBSTR,
4078 Uri_GetPropertyLength,
4079 Uri_GetPropertyDWORD,
4090 Uri_GetPathAndQuery,
4104 /***********************************************************************
4105 * CreateUri (urlmon.@)
4107 * Creates a new IUri object using the URI represented by pwzURI. This function
4108 * parses and validates the components of pwzURI and then canonicalizes the
4109 * parsed components.
4112 * pwzURI [I] The URI to parse, validate, and canonicalize.
4113 * dwFlags [I] Flags which can affect how the parsing/canonicalization is performed.
4114 * dwReserved [I] Reserved (not used).
4115 * ppURI [O] The resulting IUri after parsing/canonicalization occurs.
4118 * Success: Returns S_OK. ppURI contains the pointer to the newly allocated IUri.
4119 * Failure: E_INVALIDARG if there's invalid flag combinations in dwFlags, or an
4120 * invalid parameters, or pwzURI doesn't represnt a valid URI.
4121 * E_OUTOFMEMORY if any memory allocation fails.
4125 * Uri_CREATE_CANONICALIZE, Uri_CREATE_DECODE_EXTRA_INFO, Uri_CREATE_CRACK_UNKNOWN_SCHEMES,
4126 * Uri_CREATE_PRE_PROCESS_HTML_URI, Uri_CREATE_NO_IE_SETTINGS.
4128 HRESULT WINAPI CreateUri(LPCWSTR pwzURI, DWORD dwFlags, DWORD_PTR dwReserved, IUri **ppURI)
4130 const DWORD supported_flags = Uri_CREATE_ALLOW_RELATIVE|Uri_CREATE_ALLOW_IMPLICIT_WILDCARD_SCHEME|
4131 Uri_CREATE_ALLOW_IMPLICIT_FILE_SCHEME|Uri_CREATE_NO_CANONICALIZE|Uri_CREATE_CANONICALIZE|
4132 Uri_CREATE_DECODE_EXTRA_INFO|Uri_CREATE_NO_DECODE_EXTRA_INFO|Uri_CREATE_CRACK_UNKNOWN_SCHEMES|
4133 Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES|Uri_CREATE_PRE_PROCESS_HTML_URI|Uri_CREATE_NO_PRE_PROCESS_HTML_URI|
4134 Uri_CREATE_NO_IE_SETTINGS|Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS|Uri_CREATE_FILE_USE_DOS_PATH;
4139 TRACE("(%s %x %x %p)\n", debugstr_w(pwzURI), dwFlags, (DWORD)dwReserved, ppURI);
4142 return E_INVALIDARG;
4144 if(!pwzURI || !*pwzURI) {
4146 return E_INVALIDARG;
4149 /* Check for invalid flags. */
4150 if((dwFlags & Uri_CREATE_DECODE_EXTRA_INFO && dwFlags & Uri_CREATE_NO_DECODE_EXTRA_INFO) ||
4151 (dwFlags & Uri_CREATE_CANONICALIZE && dwFlags & Uri_CREATE_NO_CANONICALIZE) ||
4152 (dwFlags & Uri_CREATE_CRACK_UNKNOWN_SCHEMES && dwFlags & Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES) ||
4153 (dwFlags & Uri_CREATE_PRE_PROCESS_HTML_URI && dwFlags & Uri_CREATE_NO_PRE_PROCESS_HTML_URI) ||
4154 (dwFlags & Uri_CREATE_IE_SETTINGS && dwFlags & Uri_CREATE_NO_IE_SETTINGS)) {
4156 return E_INVALIDARG;
4159 /* Currently unsupported. */
4160 if(dwFlags & ~supported_flags)
4161 FIXME("Ignoring unsupported flag(s) %x\n", dwFlags & ~supported_flags);
4163 ret = heap_alloc(sizeof(Uri));
4165 return E_OUTOFMEMORY;
4167 ret->lpIUriVtbl = &UriVtbl;
4170 /* Pre process the URI, unless told otherwise. */
4171 if(!(dwFlags & Uri_CREATE_NO_PRE_PROCESS_HTML_URI))
4172 ret->raw_uri = pre_process_uri(pwzURI);
4174 ret->raw_uri = SysAllocString(pwzURI);
4178 return E_OUTOFMEMORY;
4181 memset(&data, 0, sizeof(parse_data));
4182 data.uri = ret->raw_uri;
4184 /* Validate and parse the URI into it's components. */
4185 if(!parse_uri(&data, dwFlags)) {
4186 /* Encountered an unsupported or invalid URI */
4187 SysFreeString(ret->raw_uri);
4190 return E_INVALIDARG;
4193 /* Canonicalize the URI. */
4194 hr = canonicalize_uri(&data, ret, dwFlags);
4196 SysFreeString(ret->raw_uri);
4206 /***********************************************************************
4207 * CreateUriWithFragment (urlmon.@)
4209 * Creates a new IUri object. This is almost the same as CreateUri, expect that
4210 * it allows you to explicitly specify a fragment (pwzFragment) for pwzURI.
4213 * pwzURI [I] The URI to parse and perform canonicalization on.
4214 * pwzFragment [I] The explict fragment string which should be added to pwzURI.
4215 * dwFlags [I] The flags which will be passed to CreateUri.
4216 * dwReserved [I] Reserved (not used).
4217 * ppURI [O] The resulting IUri after parsing/canonicalization.
4220 * Success: S_OK. ppURI contains the pointer to the newly allocated IUri.
4221 * Failure: E_INVALIDARG if pwzURI already contains a fragment and pwzFragment
4222 * isn't NULL. Will also return E_INVALIDARG for the same reasons as
4223 * CreateUri will. E_OUTOFMEMORY if any allocations fail.
4225 HRESULT WINAPI CreateUriWithFragment(LPCWSTR pwzURI, LPCWSTR pwzFragment, DWORD dwFlags,
4226 DWORD_PTR dwReserved, IUri **ppURI)
4229 TRACE("(%s %s %x %x %p)\n", debugstr_w(pwzURI), debugstr_w(pwzFragment), dwFlags, (DWORD)dwReserved, ppURI);
4232 return E_INVALIDARG;
4236 return E_INVALIDARG;
4239 /* Check if a fragment should be appended to the URI string. */
4242 DWORD uri_len, frag_len;
4245 /* Check if the original URI already has a fragment component. */
4246 if(StrChrW(pwzURI, '#')) {
4248 return E_INVALIDARG;
4251 uri_len = lstrlenW(pwzURI);
4252 frag_len = lstrlenW(pwzFragment);
4254 /* If the fragment doesn't start with a '#', one will be added. */
4255 add_pound = *pwzFragment != '#';
4258 uriW = heap_alloc((uri_len+frag_len+2)*sizeof(WCHAR));
4260 uriW = heap_alloc((uri_len+frag_len+1)*sizeof(WCHAR));
4263 return E_OUTOFMEMORY;
4265 memcpy(uriW, pwzURI, uri_len*sizeof(WCHAR));
4267 uriW[uri_len++] = '#';
4268 memcpy(uriW+uri_len, pwzFragment, (frag_len+1)*sizeof(WCHAR));
4270 hres = CreateUri(uriW, dwFlags, 0, ppURI);
4274 /* A fragment string wasn't specified, so just forward the call. */
4275 hres = CreateUri(pwzURI, dwFlags, 0, ppURI);
4280 #define URIBUILDER_THIS(iface) DEFINE_THIS(UriBuilder, IUriBuilder, iface)
4282 static HRESULT WINAPI UriBuilder_QueryInterface(IUriBuilder *iface, REFIID riid, void **ppv)
4284 UriBuilder *This = URIBUILDER_THIS(iface);
4286 if(IsEqualGUID(&IID_IUnknown, riid)) {
4287 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
4288 *ppv = URIBUILDER(This);
4289 }else if(IsEqualGUID(&IID_IUriBuilder, riid)) {
4290 TRACE("(%p)->(IID_IUri %p)\n", This, ppv);
4291 *ppv = URIBUILDER(This);
4293 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
4295 return E_NOINTERFACE;
4298 IUnknown_AddRef((IUnknown*)*ppv);
4302 static ULONG WINAPI UriBuilder_AddRef(IUriBuilder *iface)
4304 UriBuilder *This = URIBUILDER_THIS(iface);
4305 LONG ref = InterlockedIncrement(&This->ref);
4307 TRACE("(%p) ref=%d\n", This, ref);
4312 static ULONG WINAPI UriBuilder_Release(IUriBuilder *iface)
4314 UriBuilder *This = URIBUILDER_THIS(iface);
4315 LONG ref = InterlockedDecrement(&This->ref);
4317 TRACE("(%p) ref=%d\n", This, ref);
4320 if(This->uri) IUri_Release(URI(This->uri));
4321 heap_free(This->fragment);
4322 heap_free(This->host);
4323 heap_free(This->password);
4324 heap_free(This->path);
4331 static HRESULT WINAPI UriBuilder_CreateUriSimple(IUriBuilder *iface,
4332 DWORD dwAllowEncodingPropertyMask,
4333 DWORD_PTR dwReserved,
4336 UriBuilder *This = URIBUILDER_THIS(iface);
4337 TRACE("(%p)->(%d %d %p)\n", This, dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
4342 /* Acts the same way as CreateUri. */
4343 if(dwAllowEncodingPropertyMask && !This->uri) {
4350 return INET_E_INVALID_URL;
4353 FIXME("(%p)->(%d %d %p)\n", This, dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
4357 static HRESULT WINAPI UriBuilder_CreateUri(IUriBuilder *iface,
4358 DWORD dwCreateFlags,
4359 DWORD dwAllowEncodingPropertyMask,
4360 DWORD_PTR dwReserved,
4363 UriBuilder *This = URIBUILDER_THIS(iface);
4364 TRACE("(%p)->(0x%08x %d %d %p)\n", This, dwCreateFlags, dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
4369 /* The only time it doesn't return E_NOTIMPL when the dwAllow parameter
4370 * has flags set, is when the IUriBuilder has a IUri set and it hasn't
4371 * been modified (a call to a "Set*" hasn't been performed).
4373 * TODO: Check if the IUriBuilder's properties have been modified.
4375 if(dwAllowEncodingPropertyMask && !This->uri) {
4382 return INET_E_INVALID_URL;
4385 FIXME("(%p)->(0x%08x %d %d %p)\n", This, dwCreateFlags, dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
4389 static HRESULT WINAPI UriBuilder_CreateUriWithFlags(IUriBuilder *iface,
4390 DWORD dwCreateFlags,
4391 DWORD dwUriBuilderFlags,
4392 DWORD dwAllowEncodingPropertyMask,
4393 DWORD_PTR dwReserved,
4396 UriBuilder *This = URIBUILDER_THIS(iface);
4397 TRACE("(%p)->(0x%08x 0x%08x %d %d %p)\n", This, dwCreateFlags, dwUriBuilderFlags,
4398 dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
4403 /* Same as CreateUri. */
4404 if(dwAllowEncodingPropertyMask && !This->uri) {
4411 return INET_E_INVALID_URL;
4414 FIXME("(%p)->(0x%08x 0x%08x %d %d %p)\n", This, dwCreateFlags, dwUriBuilderFlags,
4415 dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
4419 static HRESULT WINAPI UriBuilder_GetIUri(IUriBuilder *iface, IUri **ppIUri)
4421 UriBuilder *This = URIBUILDER_THIS(iface);
4422 TRACE("(%p)->(%p)\n", This, ppIUri);
4427 FIXME("(%p)->(%p)\n", This, ppIUri);
4431 static HRESULT WINAPI UriBuilder_SetIUri(IUriBuilder *iface, IUri *pIUri)
4433 UriBuilder *This = URIBUILDER_THIS(iface);
4434 FIXME("(%p)->(%p)\n", This, pIUri);
4438 static HRESULT WINAPI UriBuilder_GetFragment(IUriBuilder *iface, DWORD *pcchFragment, LPCWSTR *ppwzFragment)
4440 UriBuilder *This = URIBUILDER_THIS(iface);
4441 TRACE("(%p)->(%p %p)\n", This, pcchFragment, ppwzFragment);
4443 if(!This->uri || This->uri->fragment_start == -1 || This->modified_props & Uri_HAS_FRAGMENT)
4444 return get_builder_component(&This->fragment, &This->fragment_len, NULL, 0, ppwzFragment, pcchFragment);
4446 return get_builder_component(&This->fragment, &This->fragment_len, This->uri->canon_uri+This->uri->fragment_start,
4447 This->uri->fragment_len, ppwzFragment, pcchFragment);
4450 static HRESULT WINAPI UriBuilder_GetHost(IUriBuilder *iface, DWORD *pcchHost, LPCWSTR *ppwzHost)
4452 UriBuilder *This = URIBUILDER_THIS(iface);
4453 TRACE("(%p)->(%p %p)\n", This, pcchHost, ppwzHost);
4455 if(!This->uri || This->uri->host_start == -1 || This->modified_props & Uri_HAS_HOST)
4456 return get_builder_component(&This->host, &This->host_len, NULL, 0, ppwzHost, pcchHost);
4458 if(This->uri->host_type == Uri_HOST_IPV6)
4459 /* Don't include the '[' and ']' around the address. */
4460 return get_builder_component(&This->host, &This->host_len, This->uri->canon_uri+This->uri->host_start+1,
4461 This->uri->host_len-2, ppwzHost, pcchHost);
4463 return get_builder_component(&This->host, &This->host_len, This->uri->canon_uri+This->uri->host_start,
4464 This->uri->host_len, ppwzHost, pcchHost);
4468 static HRESULT WINAPI UriBuilder_GetPassword(IUriBuilder *iface, DWORD *pcchPassword, LPCWSTR *ppwzPassword)
4470 UriBuilder *This = URIBUILDER_THIS(iface);
4471 TRACE("(%p)->(%p %p)\n", This, pcchPassword, ppwzPassword);
4473 if(!This->uri || This->uri->userinfo_split == -1 || This->modified_props & Uri_HAS_PASSWORD)
4474 return get_builder_component(&This->password, &This->password_len, NULL, 0, ppwzPassword, pcchPassword);
4476 const WCHAR *start = This->uri->canon_uri+This->uri->userinfo_start+This->uri->userinfo_split+1;
4477 DWORD len = This->uri->userinfo_len-This->uri->userinfo_split-1;
4478 return get_builder_component(&This->password, &This->password_len, start, len, ppwzPassword, pcchPassword);
4482 static HRESULT WINAPI UriBuilder_GetPath(IUriBuilder *iface, DWORD *pcchPath, LPCWSTR *ppwzPath)
4484 UriBuilder *This = URIBUILDER_THIS(iface);
4485 TRACE("(%p)->(%p %p)\n", This, pcchPath, ppwzPath);
4487 if(!This->uri || This->uri->path_start == -1 || This->modified_props & Uri_HAS_PATH)
4488 return get_builder_component(&This->path, &This->path_len, NULL, 0, ppwzPath, pcchPath);
4490 return get_builder_component(&This->path, &This->path_len, This->uri->canon_uri+This->uri->path_start,
4491 This->uri->path_len, ppwzPath, pcchPath);
4494 static HRESULT WINAPI UriBuilder_GetPort(IUriBuilder *iface, BOOL *pfHasPort, DWORD *pdwPort)
4496 UriBuilder *This = URIBUILDER_THIS(iface);
4497 TRACE("(%p)->(%p %p)\n", This, pfHasPort, pdwPort);
4510 FIXME("(%p)->(%p %p)\n", This, pfHasPort, pdwPort);
4514 static HRESULT WINAPI UriBuilder_GetQuery(IUriBuilder *iface, DWORD *pcchQuery, LPCWSTR *ppwzQuery)
4516 UriBuilder *This = URIBUILDER_THIS(iface);
4517 TRACE("(%p)->(%p %p)\n", This, pcchQuery, ppwzQuery);
4530 FIXME("(%p)->(%p %p)\n", This, pcchQuery, ppwzQuery);
4534 static HRESULT WINAPI UriBuilder_GetSchemeName(IUriBuilder *iface, DWORD *pcchSchemeName, LPCWSTR *ppwzSchemeName)
4536 UriBuilder *This = URIBUILDER_THIS(iface);
4537 TRACE("(%p)->(%p %p)\n", This, pcchSchemeName, ppwzSchemeName);
4539 if(!pcchSchemeName) {
4541 *ppwzSchemeName = NULL;
4545 if(!ppwzSchemeName) {
4546 *pcchSchemeName = 0;
4550 FIXME("(%p)->(%p %p)\n", This, pcchSchemeName, ppwzSchemeName);
4554 static HRESULT WINAPI UriBuilder_GetUserName(IUriBuilder *iface, DWORD *pcchUserName, LPCWSTR *ppwzUserName)
4556 UriBuilder *This = URIBUILDER_THIS(iface);
4557 TRACE("(%p)->(%p %p)\n", This, pcchUserName, ppwzUserName);
4561 *ppwzUserName = NULL;
4570 FIXME("(%p)->(%p %p)\n", This, pcchUserName, ppwzUserName);
4574 static HRESULT WINAPI UriBuilder_SetFragment(IUriBuilder *iface, LPCWSTR pwzNewValue)
4576 UriBuilder *This = URIBUILDER_THIS(iface);
4577 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
4579 This->modified_props |= Uri_HAS_FRAGMENT;
4580 return set_builder_component(&This->fragment, &This->fragment_len, pwzNewValue, '#');
4583 static HRESULT WINAPI UriBuilder_SetHost(IUriBuilder *iface, LPCWSTR pwzNewValue)
4585 UriBuilder *This = URIBUILDER_THIS(iface);
4586 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
4588 This->modified_props |= Uri_HAS_HOST;
4589 return set_builder_component(&This->host, &This->host_len, pwzNewValue, 0);
4592 static HRESULT WINAPI UriBuilder_SetPassword(IUriBuilder *iface, LPCWSTR pwzNewValue)
4594 UriBuilder *This = URIBUILDER_THIS(iface);
4595 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
4597 This->modified_props |= Uri_HAS_PASSWORD;
4598 return set_builder_component(&This->password, &This->password_len, pwzNewValue, 0);
4601 static HRESULT WINAPI UriBuilder_SetPath(IUriBuilder *iface, LPCWSTR pwzNewValue)
4603 UriBuilder *This = URIBUILDER_THIS(iface);
4604 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
4606 This->modified_props |= Uri_HAS_PATH;
4607 return set_builder_component(&This->path, &This->path_len, pwzNewValue, 0);
4610 static HRESULT WINAPI UriBuilder_SetPort(IUriBuilder *iface, BOOL fHasPort, DWORD dwNewValue)
4612 UriBuilder *This = URIBUILDER_THIS(iface);
4613 FIXME("(%p)->(%d %d)\n", This, fHasPort, dwNewValue);
4617 static HRESULT WINAPI UriBuilder_SetQuery(IUriBuilder *iface, LPCWSTR pwzNewValue)
4619 UriBuilder *This = URIBUILDER_THIS(iface);
4620 FIXME("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
4624 static HRESULT WINAPI UriBuilder_SetSchemeName(IUriBuilder *iface, LPCWSTR pwzNewValue)
4626 UriBuilder *This = URIBUILDER_THIS(iface);
4627 FIXME("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
4631 static HRESULT WINAPI UriBuilder_SetUserName(IUriBuilder *iface, LPCWSTR pwzNewValue)
4633 UriBuilder *This = URIBUILDER_THIS(iface);
4634 FIXME("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
4638 static HRESULT WINAPI UriBuilder_RemoveProperties(IUriBuilder *iface, DWORD dwPropertyMask)
4640 UriBuilder *This = URIBUILDER_THIS(iface);
4641 FIXME("(%p)->(0x%08x)\n", This, dwPropertyMask);
4645 static HRESULT WINAPI UriBuilder_HasBeenModified(IUriBuilder *iface, BOOL *pfModified)
4647 UriBuilder *This = URIBUILDER_THIS(iface);
4648 TRACE("(%p)->(%p)\n", This, pfModified);
4653 FIXME("(%p)->(%p)\n", This, pfModified);
4657 #undef URIBUILDER_THIS
4659 static const IUriBuilderVtbl UriBuilderVtbl = {
4660 UriBuilder_QueryInterface,
4663 UriBuilder_CreateUriSimple,
4664 UriBuilder_CreateUri,
4665 UriBuilder_CreateUriWithFlags,
4668 UriBuilder_GetFragment,
4670 UriBuilder_GetPassword,
4673 UriBuilder_GetQuery,
4674 UriBuilder_GetSchemeName,
4675 UriBuilder_GetUserName,
4676 UriBuilder_SetFragment,
4678 UriBuilder_SetPassword,
4681 UriBuilder_SetQuery,
4682 UriBuilder_SetSchemeName,
4683 UriBuilder_SetUserName,
4684 UriBuilder_RemoveProperties,
4685 UriBuilder_HasBeenModified,
4688 /***********************************************************************
4689 * CreateIUriBuilder (urlmon.@)
4691 HRESULT WINAPI CreateIUriBuilder(IUri *pIUri, DWORD dwFlags, DWORD_PTR dwReserved, IUriBuilder **ppIUriBuilder)
4695 TRACE("(%p %x %x %p)\n", pIUri, dwFlags, (DWORD)dwReserved, ppIUriBuilder);
4700 ret = heap_alloc_zero(sizeof(UriBuilder));
4702 return E_OUTOFMEMORY;
4704 ret->lpIUriBuilderVtbl = &UriBuilderVtbl;
4710 if((uri = get_uri_obj(pIUri))) {
4715 *ppIUriBuilder = NULL;
4716 FIXME("(%p %x %x %p): Unknown IUri types not supported yet.\n", pIUri, dwFlags,
4717 (DWORD)dwReserved, ppIUriBuilder);
4722 *ppIUriBuilder = URIBUILDER(ret);