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);
32 const IUriVtbl *lpIUriVtbl;
37 /* Information about the canonicalized URI's buffer. */
44 URL_SCHEME scheme_type;
52 Uri_HOST_TYPE host_type;
71 const IUriBuilderVtbl *lpIUriBuilderVtbl;
81 /* IPv6 addresses can hold up to 8 h16 components. */
85 /* An IPv6 can have 1 elision ("::"). */
88 /* An IPv6 can contain 1 IPv4 address as the last 32bits of the address. */
101 BOOL has_implicit_scheme;
102 BOOL has_implicit_ip;
107 URL_SCHEME scheme_type;
109 const WCHAR *userinfo;
115 Uri_HOST_TYPE host_type;
118 ipv6_address ipv6_address;
131 static const CHAR hexDigits[] = "0123456789ABCDEF";
133 /* List of scheme types/scheme names that are recognized by the IUri interface as of IE 7. */
134 static const struct {
136 WCHAR scheme_name[16];
137 } recognized_schemes[] = {
138 {URL_SCHEME_FTP, {'f','t','p',0}},
139 {URL_SCHEME_HTTP, {'h','t','t','p',0}},
140 {URL_SCHEME_GOPHER, {'g','o','p','h','e','r',0}},
141 {URL_SCHEME_MAILTO, {'m','a','i','l','t','o',0}},
142 {URL_SCHEME_NEWS, {'n','e','w','s',0}},
143 {URL_SCHEME_NNTP, {'n','n','t','p',0}},
144 {URL_SCHEME_TELNET, {'t','e','l','n','e','t',0}},
145 {URL_SCHEME_WAIS, {'w','a','i','s',0}},
146 {URL_SCHEME_FILE, {'f','i','l','e',0}},
147 {URL_SCHEME_MK, {'m','k',0}},
148 {URL_SCHEME_HTTPS, {'h','t','t','p','s',0}},
149 {URL_SCHEME_SHELL, {'s','h','e','l','l',0}},
150 {URL_SCHEME_SNEWS, {'s','n','e','w','s',0}},
151 {URL_SCHEME_LOCAL, {'l','o','c','a','l',0}},
152 {URL_SCHEME_JAVASCRIPT, {'j','a','v','a','s','c','r','i','p','t',0}},
153 {URL_SCHEME_VBSCRIPT, {'v','b','s','c','r','i','p','t',0}},
154 {URL_SCHEME_ABOUT, {'a','b','o','u','t',0}},
155 {URL_SCHEME_RES, {'r','e','s',0}},
156 {URL_SCHEME_MSSHELLROOTED, {'m','s','-','s','h','e','l','l','-','r','o','o','t','e','d',0}},
157 {URL_SCHEME_MSSHELLIDLIST, {'m','s','-','s','h','e','l','l','-','i','d','l','i','s','t',0}},
158 {URL_SCHEME_MSHELP, {'h','c','p',0}},
159 {URL_SCHEME_WILDCARD, {'*',0}}
162 /* List of default ports Windows recognizes. */
163 static const struct {
166 } default_ports[] = {
167 {URL_SCHEME_FTP, 21},
168 {URL_SCHEME_HTTP, 80},
169 {URL_SCHEME_GOPHER, 70},
170 {URL_SCHEME_NNTP, 119},
171 {URL_SCHEME_TELNET, 23},
172 {URL_SCHEME_WAIS, 210},
173 {URL_SCHEME_HTTPS, 443},
176 /* List of 3 character top level domain names Windows seems to recognize.
177 * There might be more, but, these are the only ones I've found so far.
179 static const struct {
181 } recognized_tlds[] = {
191 static inline BOOL is_alpha(WCHAR val) {
192 return ((val >= 'a' && val <= 'z') || (val >= 'A' && val <= 'Z'));
195 static inline BOOL is_num(WCHAR val) {
196 return (val >= '0' && val <= '9');
199 /* A URI is implicitly a file path if it begins with
200 * a drive letter (eg X:) or starts with "\\" (UNC path).
202 static inline BOOL is_implicit_file_path(const WCHAR *str) {
203 if(is_alpha(str[0]) && str[1] == ':')
205 else if(str[0] == '\\' && str[1] == '\\')
211 /* Checks if the URI is a hierarchical URI. A hierarchical
212 * URI is one that has "//" after the scheme.
214 static BOOL check_hierarchical(const WCHAR **ptr) {
215 const WCHAR *start = *ptr;
230 /* unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~" */
231 static inline BOOL is_unreserved(WCHAR val) {
232 return (is_alpha(val) || is_num(val) || val == '-' || val == '.' ||
233 val == '_' || val == '~');
236 /* sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
237 * / "*" / "+" / "," / ";" / "="
239 static inline BOOL is_subdelim(WCHAR val) {
240 return (val == '!' || val == '$' || val == '&' ||
241 val == '\'' || val == '(' || val == ')' ||
242 val == '*' || val == '+' || val == ',' ||
243 val == ';' || val == '=');
246 /* gen-delims = ":" / "/" / "?" / "#" / "[" / "]" / "@" */
247 static inline BOOL is_gendelim(WCHAR val) {
248 return (val == ':' || val == '/' || val == '?' ||
249 val == '#' || val == '[' || val == ']' ||
253 /* Characters that delimit the end of the authority
254 * section of a URI. Sometimes a '\\' is considered
255 * an authority delimeter.
257 static inline BOOL is_auth_delim(WCHAR val, BOOL acceptSlash) {
258 return (val == '#' || val == '/' || val == '?' ||
259 val == '\0' || (acceptSlash && val == '\\'));
262 /* reserved = gen-delims / sub-delims */
263 static inline BOOL is_reserved(WCHAR val) {
264 return (is_subdelim(val) || is_gendelim(val));
267 static inline BOOL is_hexdigit(WCHAR val) {
268 return ((val >= 'a' && val <= 'f') ||
269 (val >= 'A' && val <= 'F') ||
270 (val >= '0' && val <= '9'));
273 static inline BOOL is_path_delim(WCHAR val) {
274 return (!val || val == '#' || val == '?');
277 /* Computes the size of the given IPv6 address.
278 * Each h16 component is 16bits, if there is an IPv4 address, it's
279 * 32bits. If there's an elision it can be 16bits to 128bits, depending
280 * on the number of other components.
282 * Modeled after google-url's CheckIPv6ComponentsSize function
284 static void compute_ipv6_comps_size(ipv6_address *address) {
285 address->components_size = address->h16_count * 2;
288 /* IPv4 address is 4 bytes. */
289 address->components_size += 4;
291 if(address->elision) {
292 /* An elision can be anywhere from 2 bytes up to 16 bytes.
293 * It size depends on the size of the h16 and IPv4 components.
295 address->elision_size = 16 - address->components_size;
296 if(address->elision_size < 2)
297 address->elision_size = 2;
299 address->elision_size = 0;
302 /* Taken from dlls/jscript/lex.c */
303 static int hex_to_int(WCHAR val) {
304 if(val >= '0' && val <= '9')
306 else if(val >= 'a' && val <= 'f')
307 return val - 'a' + 10;
308 else if(val >= 'A' && val <= 'F')
309 return val - 'A' + 10;
314 /* Helper function for converting a percent encoded string
315 * representation of a WCHAR value into its actual WCHAR value. If
316 * the two characters following the '%' aren't valid hex values then
317 * this function returns the NULL character.
320 * "%2E" will result in '.' being returned by this function.
322 static WCHAR decode_pct_val(const WCHAR *ptr) {
325 if(*ptr == '%' && is_hexdigit(*(ptr + 1)) && is_hexdigit(*(ptr + 2))) {
326 INT a = hex_to_int(*(ptr + 1));
327 INT b = hex_to_int(*(ptr + 2));
336 /* Helper function for percent encoding a given character
337 * and storing the encoded value into a given buffer (dest).
339 * It's up to the calling function to ensure that there is
340 * at least enough space in 'dest' for the percent encoded
341 * value to be stored (so dest + 3 spaces available).
343 static inline void pct_encode_val(WCHAR val, WCHAR *dest) {
345 dest[1] = hexDigits[(val >> 4) & 0xf];
346 dest[2] = hexDigits[val & 0xf];
349 /* Scans the range of characters [str, end] and returns the last occurence
350 * of 'ch' or returns NULL.
352 static const WCHAR *str_last_of(const WCHAR *str, const WCHAR *end, WCHAR ch) {
353 const WCHAR *ptr = end;
364 /* Attempts to parse the domain name from the host.
366 * This function also includes the Top-level Domain (TLD) name
367 * of the host when it tries to find the domain name. If it finds
368 * a valid domain name it will assign 'domain_start' the offset
369 * into 'host' where the domain name starts.
371 * It's implied that if a domain name its range is implied to be
372 * [host+domain_start, host+host_len).
374 static void find_domain_name(const WCHAR *host, DWORD host_len,
376 const WCHAR *last_tld, *sec_last_tld, *end;
378 end = host+host_len-1;
382 /* There has to be at least enough room for a '.' followed by a
383 * 3 character TLD for a domain to even exist in the host name.
388 last_tld = str_last_of(host, end, '.');
390 /* http://hostname -> has no domain name. */
393 sec_last_tld = str_last_of(host, last_tld-1, '.');
395 /* If the '.' is at the beginning of the host there
396 * has to be at least 3 characters in the TLD for it
398 * Ex: .com -> .com as the domain name.
399 * .co -> has no domain name.
401 if(last_tld-host == 0) {
402 if(end-(last_tld-1) < 3)
404 } else if(last_tld-host == 3) {
407 /* If there's three characters in front of last_tld and
408 * they are on the list of recognized TLDs, then this
409 * host doesn't have a domain (since the host only contains
411 * Ex: edu.uk -> has no domain name.
412 * foo.uk -> foo.uk as the domain name.
414 for(i = 0; i < sizeof(recognized_tlds)/sizeof(recognized_tlds[0]); ++i) {
415 if(!StrCmpNIW(host, recognized_tlds[i].tld_name, 3))
418 } else if(last_tld-host < 3)
419 /* Anything less then 3 characters is considered part
421 * Ex: ak.uk -> Has no domain name.
425 /* Otherwise the domain name is the whole host name. */
427 } else if(end+1-last_tld > 3) {
428 /* If the last_tld has more then 3 characters then it's automatically
429 * considered the TLD of the domain name.
430 * Ex: www.winehq.org.uk.test -> uk.test as the domain name.
432 *domain_start = (sec_last_tld+1)-host;
433 } else if(last_tld - (sec_last_tld+1) < 4) {
435 /* If the sec_last_tld is 3 characters long it HAS to be on the list of
436 * recognized to still be considered part of the TLD name, otherwise
437 * its considered the domain name.
438 * Ex: www.google.com.uk -> google.com.uk as the domain name.
439 * www.google.foo.uk -> foo.uk as the domain name.
441 if(last_tld - (sec_last_tld+1) == 3) {
442 for(i = 0; i < sizeof(recognized_tlds)/sizeof(recognized_tlds[0]); ++i) {
443 if(!StrCmpNIW(sec_last_tld+1, recognized_tlds[i].tld_name, 3)) {
444 const WCHAR *domain = str_last_of(host, sec_last_tld-1, '.');
449 *domain_start = (domain+1) - host;
450 TRACE("Found domain name %s\n", debugstr_wn(host+*domain_start,
451 (host+host_len)-(host+*domain_start)));
456 *domain_start = (sec_last_tld+1)-host;
458 /* Since the sec_last_tld is less then 3 characters it's considered
460 * Ex: www.google.fo.uk -> google.fo.uk as the domain name.
462 const WCHAR *domain = str_last_of(host, sec_last_tld-1, '.');
467 *domain_start = (domain+1) - host;
470 /* The second to last TLD has more then 3 characters making it
472 * Ex: www.google.test.us -> test.us as the domain name.
474 *domain_start = (sec_last_tld+1)-host;
477 TRACE("Found domain name %s\n", debugstr_wn(host+*domain_start,
478 (host+host_len)-(host+*domain_start)));
481 /* Removes the dot segments from a heirarchical URIs path component. This
482 * function performs the removal in place.
484 * This is a modified version of Qt's QUrl function "removeDotsFromPath".
486 * This function returns the new length of the path string.
488 static DWORD remove_dot_segments(WCHAR *path, DWORD path_len) {
490 const WCHAR *in = out;
491 const WCHAR *end = out + path_len;
495 /* A. if the input buffer begins with a prefix of "/./" or "/.",
496 * where "." is a complete path segment, then replace that
497 * prefix with "/" in the input buffer; otherwise,
499 if(in <= end - 3 && in[0] == '/' && in[1] == '.' && in[2] == '/') {
502 } else if(in == end - 2 && in[0] == '/' && in[1] == '.') {
508 /* B. if the input buffer begins with a prefix of "/../" or "/..",
509 * where ".." is a complete path segment, then replace that
510 * prefix with "/" in the input buffer and remove the last
511 * segment and its preceding "/" (if any) from the output
514 if(in <= end - 4 && in[0] == '/' && in[1] == '.' && in[2] == '.' && in[3] == '/') {
515 while(out > path && *(--out) != '/');
519 } else if(in == end - 3 && in[0] == '/' && in[1] == '.' && in[2] == '.') {
520 while(out > path && *(--out) != '/');
529 /* C. move the first path segment in the input buffer to the end of
530 * the output buffer, including the initial "/" character (if
531 * any) and any subsequent characters up to, but not including,
532 * the next "/" character or the end of the input buffer.
535 while(in < end && *in != '/')
540 TRACE("(%p %d): Path after dot segments removed %s len=%d\n", path, path_len,
541 debugstr_wn(path, len), len);
545 /* Attempts to find the file extension in a given path. */
546 static INT find_file_extension(const WCHAR *path, DWORD path_len) {
549 for(end = path+path_len-1; end >= path && *end != '/' && *end != '\\'; --end) {
557 /* Computes the location where the elision should occur in the IPv6
558 * address using the numerical values of each component stored in
559 * 'values'. If the address shouldn't contain an elision then 'index'
560 * is assigned -1 as it's value. Otherwise 'index' will contain the
561 * starting index (into values) where the elision should be, and 'count'
562 * will contain the number of cells the elision covers.
565 * Windows will expand an elision if the elision only represents 1 h16
566 * component of the URI.
568 * Ex: [1::2:3:4:5:6:7] -> [1:0:2:3:4:5:6:7]
570 * If the IPv6 address contains an IPv4 address, the IPv4 address is also
571 * considered for being included as part of an elision if all it's components
574 * Ex: [1:2:3:4:5:6:0.0.0.0] -> [1:2:3:4:5:6::]
576 static void compute_elision_location(const ipv6_address *address, const USHORT values[8],
577 INT *index, DWORD *count) {
578 DWORD i, max_len, cur_len;
579 INT max_index, cur_index;
581 max_len = cur_len = 0;
582 max_index = cur_index = -1;
583 for(i = 0; i < 8; ++i) {
584 BOOL check_ipv4 = (address->ipv4 && i == 6);
585 BOOL is_end = (check_ipv4 || i == 7);
588 /* Check if the IPv4 address contains only zeros. */
589 if(values[i] == 0 && values[i+1] == 0) {
596 } else if(values[i] == 0) {
603 if(is_end || values[i] != 0) {
604 /* We only consider it for an elision if it's
605 * more then 1 component long.
607 if(cur_len > 1 && cur_len > max_len) {
608 /* Found the new elision location. */
610 max_index = cur_index;
613 /* Reset the current range for the next range of zeros. */
623 /* Converts the specified IPv4 address into an uint value.
625 * This function assumes that the IPv4 address has already been validated.
627 static UINT ipv4toui(const WCHAR *ip, DWORD len) {
629 DWORD comp_value = 0;
632 for(ptr = ip; ptr < ip+len; ++ptr) {
638 comp_value = comp_value*10 + (*ptr-'0');
647 /* Converts an IPv4 address in numerical form into it's fully qualified
648 * string form. This function returns the number of characters written
649 * to 'dest'. If 'dest' is NULL this function will return the number of
650 * characters that would have been written.
652 * It's up to the caller to ensure there's enough space in 'dest' for the
655 static DWORD ui2ipv4(WCHAR *dest, UINT address) {
656 static const WCHAR formatW[] =
657 {'%','u','.','%','u','.','%','u','.','%','u',0};
661 digits[0] = (address >> 24) & 0xff;
662 digits[1] = (address >> 16) & 0xff;
663 digits[2] = (address >> 8) & 0xff;
664 digits[3] = address & 0xff;
668 ret = sprintfW(tmp, formatW, digits[0], digits[1], digits[2], digits[3]);
670 ret = sprintfW(dest, formatW, digits[0], digits[1], digits[2], digits[3]);
675 /* Converts an h16 component (from an IPv6 address) into it's
678 * This function assumes that the h16 component has already been validated.
680 static USHORT h16tous(h16 component) {
684 for(i = 0; i < component.len; ++i) {
686 ret += hex_to_int(component.str[i]);
692 /* Converts an IPv6 address into it's 128 bits (16 bytes) numerical value.
694 * This function assumes that the ipv6_address has already been validated.
696 static BOOL ipv6_to_number(const ipv6_address *address, USHORT number[8]) {
697 DWORD i, cur_component = 0;
698 BOOL already_passed_elision = FALSE;
700 for(i = 0; i < address->h16_count; ++i) {
701 if(address->elision) {
702 if(address->components[i].str > address->elision && !already_passed_elision) {
703 /* Means we just passed the elision and need to add it's values to
704 * 'number' before we do anything else.
707 for(j = 0; j < address->elision_size; j+=2)
708 number[cur_component++] = 0;
710 already_passed_elision = TRUE;
714 number[cur_component++] = h16tous(address->components[i]);
717 /* Case when the elision appears after the h16 components. */
718 if(!already_passed_elision && address->elision) {
719 for(i = 0; i < address->elision_size; i+=2)
720 number[cur_component++] = 0;
721 already_passed_elision = TRUE;
725 UINT value = ipv4toui(address->ipv4, address->ipv4_len);
727 if(cur_component != 6) {
728 ERR("(%p %p): Failed sanity check with %d\n", address, number, cur_component);
732 number[cur_component++] = (value >> 16) & 0xffff;
733 number[cur_component] = value & 0xffff;
739 /* Checks if the characters pointed to by 'ptr' are
740 * a percent encoded data octet.
742 * pct-encoded = "%" HEXDIG HEXDIG
744 static BOOL check_pct_encoded(const WCHAR **ptr) {
745 const WCHAR *start = *ptr;
751 if(!is_hexdigit(**ptr)) {
757 if(!is_hexdigit(**ptr)) {
766 /* dec-octet = DIGIT ; 0-9
767 * / %x31-39 DIGIT ; 10-99
768 * / "1" 2DIGIT ; 100-199
769 * / "2" %x30-34 DIGIT ; 200-249
770 * / "25" %x30-35 ; 250-255
772 static BOOL check_dec_octet(const WCHAR **ptr) {
773 const WCHAR *c1, *c2, *c3;
776 /* A dec-octet must be at least 1 digit long. */
777 if(*c1 < '0' || *c1 > '9')
783 /* Since the 1 digit requirment was meet, it doesn't
784 * matter if this is a DIGIT value, it's considered a
787 if(*c2 < '0' || *c2 > '9')
793 /* Same explanation as above. */
794 if(*c3 < '0' || *c3 > '9')
797 /* Anything > 255 isn't a valid IP dec-octet. */
798 if(*c1 >= '2' && *c2 >= '5' && *c3 >= '5') {
807 /* Checks if there is an implicit IPv4 address in the host component of the URI.
808 * The max value of an implicit IPv4 address is UINT_MAX.
811 * "234567" would be considered an implicit IPv4 address.
813 static BOOL check_implicit_ipv4(const WCHAR **ptr, UINT *val) {
814 const WCHAR *start = *ptr;
818 while(is_num(**ptr)) {
819 ret = ret*10 + (**ptr - '0');
835 /* Checks if the string contains an IPv4 address.
837 * This function has a strict mode or a non-strict mode of operation
838 * When 'strict' is set to FALSE this function will return TRUE if
839 * the string contains at least 'dec-octet "." dec-octet' since partial
840 * IPv4 addresses will be normalized out into full IPv4 addresses. When
841 * 'strict' is set this function expects there to be a full IPv4 address.
843 * IPv4address = dec-octet "." dec-octet "." dec-octet "." dec-octet
845 static BOOL check_ipv4address(const WCHAR **ptr, BOOL strict) {
846 const WCHAR *start = *ptr;
848 if(!check_dec_octet(ptr)) {
859 if(!check_dec_octet(ptr)) {
873 if(!check_dec_octet(ptr)) {
887 if(!check_dec_octet(ptr)) {
892 /* Found a four digit ip address. */
895 /* Tries to parse the scheme name of the URI.
897 * scheme = ALPHA *(ALPHA | NUM | '+' | '-' | '.') as defined by RFC 3896.
898 * NOTE: Windows accepts a number as the first character of a scheme.
900 static BOOL parse_scheme_name(const WCHAR **ptr, parse_data *data) {
901 const WCHAR *start = *ptr;
904 data->scheme_len = 0;
907 if(**ptr == '*' && *ptr == start) {
908 /* Might have found a wildcard scheme. If it is the next
909 * char has to be a ':' for it to be a valid URI
913 } else if(!is_num(**ptr) && !is_alpha(**ptr) && **ptr != '+' &&
914 **ptr != '-' && **ptr != '.')
923 /* Schemes must end with a ':' */
929 data->scheme = start;
930 data->scheme_len = *ptr - start;
936 /* Tries to deduce the corresponding URL_SCHEME for the given URI. Stores
937 * the deduced URL_SCHEME in data->scheme_type.
939 static BOOL parse_scheme_type(parse_data *data) {
940 /* If there's scheme data then see if it's a recognized scheme. */
941 if(data->scheme && data->scheme_len) {
944 for(i = 0; i < sizeof(recognized_schemes)/sizeof(recognized_schemes[0]); ++i) {
945 if(lstrlenW(recognized_schemes[i].scheme_name) == data->scheme_len) {
946 /* Has to be a case insensitive compare. */
947 if(!StrCmpNIW(recognized_schemes[i].scheme_name, data->scheme, data->scheme_len)) {
948 data->scheme_type = recognized_schemes[i].scheme;
954 /* If we get here it means it's not a recognized scheme. */
955 data->scheme_type = URL_SCHEME_UNKNOWN;
957 } else if(data->is_relative) {
958 /* Relative URI's have no scheme. */
959 data->scheme_type = URL_SCHEME_UNKNOWN;
962 /* Should never reach here! what happened... */
963 FIXME("(%p): Unable to determine scheme type for URI %s\n", data, debugstr_w(data->uri));
968 /* Tries to parse (or deduce) the scheme_name of a URI. If it can't
969 * parse a scheme from the URI it will try to deduce the scheme_name and scheme_type
970 * using the flags specified in 'flags' (if any). Flags that affect how this function
971 * operates are the Uri_CREATE_ALLOW_* flags.
973 * All parsed/deduced information will be stored in 'data' when the function returns.
975 * Returns TRUE if it was able to successfully parse the information.
977 static BOOL parse_scheme(const WCHAR **ptr, parse_data *data, DWORD flags) {
978 static const WCHAR fileW[] = {'f','i','l','e',0};
979 static const WCHAR wildcardW[] = {'*',0};
981 /* First check to see if the uri could implicitly be a file path. */
982 if(is_implicit_file_path(*ptr)) {
983 if(flags & Uri_CREATE_ALLOW_IMPLICIT_FILE_SCHEME) {
984 data->scheme = fileW;
985 data->scheme_len = lstrlenW(fileW);
986 data->has_implicit_scheme = TRUE;
988 TRACE("(%p %p %x): URI is an implicit file path.\n", ptr, data, flags);
990 /* Window's does not consider anything that can implicitly be a file
991 * path to be a valid URI if the ALLOW_IMPLICIT_FILE_SCHEME flag is not set...
993 TRACE("(%p %p %x): URI is implicitly a file path, but, the ALLOW_IMPLICIT_FILE_SCHEME flag wasn't set.\n",
997 } else if(!parse_scheme_name(ptr, data)) {
998 /* No Scheme was found, this means it could be:
999 * a) an implicit Wildcard scheme
1003 if(flags & Uri_CREATE_ALLOW_IMPLICIT_WILDCARD_SCHEME) {
1004 data->scheme = wildcardW;
1005 data->scheme_len = lstrlenW(wildcardW);
1006 data->has_implicit_scheme = TRUE;
1008 TRACE("(%p %p %x): URI is an implicit wildcard scheme.\n", ptr, data, flags);
1009 } else if (flags & Uri_CREATE_ALLOW_RELATIVE) {
1010 data->is_relative = TRUE;
1011 TRACE("(%p %p %x): URI is relative.\n", ptr, data, flags);
1013 TRACE("(%p %p %x): Malformed URI found. Unable to deduce scheme name.\n", ptr, data, flags);
1018 if(!data->is_relative)
1019 TRACE("(%p %p %x): Found scheme=%s scheme_len=%d\n", ptr, data, flags,
1020 debugstr_wn(data->scheme, data->scheme_len), data->scheme_len);
1022 if(!parse_scheme_type(data))
1025 TRACE("(%p %p %x): Assigned %d as the URL_SCHEME.\n", ptr, data, flags, data->scheme_type);
1029 /* Parses the userinfo part of the URI (if it exists). The userinfo field of
1030 * a URI can consist of "username:password@", or just "username@".
1033 * userinfo = *( unreserved / pct-encoded / sub-delims / ":" )
1036 * 1) If there is more than one ':' in the userinfo part of the URI Windows
1037 * uses the first occurence of ':' to delimit the username and password
1041 * ftp://user:pass:word@winehq.org
1043 * Would yield, "user" as the username and "pass:word" as the password.
1045 * 2) Windows allows any character to appear in the "userinfo" part of
1046 * a URI, as long as it's not an authority delimeter character set.
1048 static void parse_userinfo(const WCHAR **ptr, parse_data *data, DWORD flags) {
1049 data->userinfo = *ptr;
1050 data->userinfo_split = -1;
1052 while(**ptr != '@') {
1053 if(**ptr == ':' && data->userinfo_split == -1)
1054 data->userinfo_split = *ptr - data->userinfo;
1055 else if(**ptr == '%') {
1056 /* If it's a known scheme type, it has to be a valid percent
1059 if(!check_pct_encoded(ptr)) {
1060 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
1061 *ptr = data->userinfo;
1062 data->userinfo = NULL;
1063 data->userinfo_split = -1;
1065 TRACE("(%p %p %x): URI contained no userinfo.\n", ptr, data, flags);
1070 } else if(is_auth_delim(**ptr, data->scheme_type != URL_SCHEME_UNKNOWN))
1077 *ptr = data->userinfo;
1078 data->userinfo = NULL;
1079 data->userinfo_split = -1;
1081 TRACE("(%p %p %x): URI contained no userinfo.\n", ptr, data, flags);
1085 data->userinfo_len = *ptr - data->userinfo;
1086 TRACE("(%p %p %x): Found userinfo=%s userinfo_len=%d split=%d.\n", ptr, data, flags,
1087 debugstr_wn(data->userinfo, data->userinfo_len), data->userinfo_len, data->userinfo_split);
1091 /* Attempts to parse a port from the URI.
1094 * Windows seems to have a cap on what the maximum value
1095 * for a port can be. The max value is USHORT_MAX.
1099 static BOOL parse_port(const WCHAR **ptr, parse_data *data, DWORD flags) {
1103 while(!is_auth_delim(**ptr, data->scheme_type != URL_SCHEME_UNKNOWN)) {
1104 if(!is_num(**ptr)) {
1110 port = port*10 + (**ptr-'0');
1112 if(port > USHORT_MAX) {
1121 data->port_value = port;
1122 data->port_len = *ptr - data->port;
1124 TRACE("(%p %p %x): Found port %s len=%d value=%u\n", ptr, data, flags,
1125 debugstr_wn(data->port, data->port_len), data->port_len, data->port_value);
1129 /* Attempts to parse a IPv4 address from the URI.
1132 * Window's normalizes IPv4 addresses, This means there's three
1133 * possibilities for the URI to contain an IPv4 address.
1134 * 1) A well formed address (ex. 192.2.2.2).
1135 * 2) A partially formed address. For example "192.0" would
1136 * normalize to "192.0.0.0" during canonicalization.
1137 * 3) An implicit IPv4 address. For example "256" would
1138 * normalize to "0.0.1.0" during canonicalization. Also
1139 * note that the maximum value for an implicit IP address
1140 * is UINT_MAX, if the value in the URI exceeds this then
1141 * it is not considered an IPv4 address.
1143 static BOOL parse_ipv4address(const WCHAR **ptr, parse_data *data, DWORD flags) {
1144 const BOOL is_unknown = data->scheme_type == URL_SCHEME_UNKNOWN;
1147 if(!check_ipv4address(ptr, FALSE)) {
1148 if(!check_implicit_ipv4(ptr, &data->implicit_ipv4)) {
1149 TRACE("(%p %p %x): URI didn't contain anything looking like an IPv4 address.\n",
1155 data->has_implicit_ip = TRUE;
1158 /* Check if what we found is the only part of the host name (if it isn't
1159 * we don't have an IPv4 address).
1163 if(!parse_port(ptr, data, flags)) {
1168 } else if(!is_auth_delim(**ptr, !is_unknown)) {
1169 /* Found more data which belongs the host, so this isn't an IPv4. */
1172 data->has_implicit_ip = FALSE;
1176 data->host_len = *ptr - data->host;
1177 data->host_type = Uri_HOST_IPV4;
1179 TRACE("(%p %p %x): IPv4 address found. host=%s host_len=%d host_type=%d\n",
1180 ptr, data, flags, debugstr_wn(data->host, data->host_len),
1181 data->host_len, data->host_type);
1185 /* Attempts to parse the reg-name from the URI.
1187 * Because of the way Windows handles ':' this function also
1188 * handles parsing the port.
1190 * reg-name = *( unreserved / pct-encoded / sub-delims )
1193 * Windows allows everything, but, the characters in "auth_delims" and ':'
1194 * to appear in a reg-name, unless it's an unknown scheme type then ':' is
1195 * allowed to appear (even if a valid port isn't after it).
1197 * Windows doesn't like host names which start with '[' and end with ']'
1198 * and don't contain a valid IP literal address in between them.
1200 * On Windows if an '[' is encountered in the host name the ':' no longer
1201 * counts as a delimiter until you reach the next ']' or an "authority delimeter".
1203 * A reg-name CAN be empty.
1205 static BOOL parse_reg_name(const WCHAR **ptr, parse_data *data, DWORD flags) {
1206 const BOOL has_start_bracket = **ptr == '[';
1207 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
1208 BOOL inside_brackets = has_start_bracket;
1209 BOOL ignore_col = FALSE;
1211 /* We have to be careful with file schemes. */
1212 if(data->scheme_type == URL_SCHEME_FILE) {
1213 /* This is because an implicit file scheme could be "C:\\test" and it
1214 * would trick this function into thinking the host is "C", when after
1215 * canonicalization the host would end up being an empty string.
1217 if(is_alpha(**ptr) && *(*ptr+1) == ':') {
1218 /* Regular old drive paths don't have a host type (or host name). */
1219 data->host_type = Uri_HOST_UNKNOWN;
1223 } else if(**ptr == '\\' && *(*ptr+1) == '\\')
1224 /* Skip past the "\\" of a UNC path. */
1230 while(!is_auth_delim(**ptr, known_scheme)) {
1231 if(**ptr == ':' && !ignore_col) {
1232 /* We can ignore ':' if were inside brackets.*/
1233 if(!inside_brackets) {
1234 const WCHAR *tmp = (*ptr)++;
1236 /* Attempt to parse the port. */
1237 if(!parse_port(ptr, data, flags)) {
1238 /* Windows expects there to be a valid port for known scheme types. */
1239 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
1242 TRACE("(%p %p %x): Expected valid port\n", ptr, data, flags);
1245 /* Windows gives up on trying to parse a port when it
1246 * encounters 1 invalid port.
1250 data->host_len = tmp - data->host;
1254 } else if(**ptr == '%' && known_scheme) {
1255 /* Has to be a legit % encoded value. */
1256 if(!check_pct_encoded(ptr)) {
1262 } else if(**ptr == ']')
1263 inside_brackets = FALSE;
1264 else if(**ptr == '[')
1265 inside_brackets = TRUE;
1270 if(has_start_bracket) {
1271 /* Make sure the last character of the host wasn't a ']'. */
1272 if(*(*ptr-1) == ']') {
1273 TRACE("(%p %p %x): Expected an IP literal inside of the host\n",
1281 /* Don't overwrite our length if we found a port earlier. */
1283 data->host_len = *ptr - data->host;
1285 /* If the host is empty, then it's an unknown host type. */
1286 if(data->host_len == 0)
1287 data->host_type = Uri_HOST_UNKNOWN;
1289 data->host_type = Uri_HOST_DNS;
1291 TRACE("(%p %p %x): Parsed reg-name. host=%s len=%d\n", ptr, data, flags,
1292 debugstr_wn(data->host, data->host_len), data->host_len);
1296 /* Attempts to parse an IPv6 address out of the URI.
1298 * IPv6address = 6( h16 ":" ) ls32
1299 * / "::" 5( h16 ":" ) ls32
1300 * / [ h16 ] "::" 4( h16 ":" ) ls32
1301 * / [ *1( h16 ":" ) h16 ] "::" 3( h16 ":" ) ls32
1302 * / [ *2( h16 ":" ) h16 ] "::" 2( h16 ":" ) ls32
1303 * / [ *3( h16 ":" ) h16 ] "::" h16 ":" ls32
1304 * / [ *4( h16 ":" ) h16 ] "::" ls32
1305 * / [ *5( h16 ":" ) h16 ] "::" h16
1306 * / [ *6( h16 ":" ) h16 ] "::"
1308 * ls32 = ( h16 ":" h16 ) / IPv4address
1309 * ; least-significant 32 bits of address.
1312 * ; 16 bits of address represented in hexadecimal.
1314 * Modeled after google-url's 'DoParseIPv6' function.
1316 static BOOL parse_ipv6address(const WCHAR **ptr, parse_data *data, DWORD flags) {
1317 const WCHAR *start, *cur_start;
1320 start = cur_start = *ptr;
1321 memset(&ip, 0, sizeof(ipv6_address));
1324 /* Check if we're on the last character of the host. */
1325 BOOL is_end = (is_auth_delim(**ptr, data->scheme_type != URL_SCHEME_UNKNOWN)
1328 BOOL is_split = (**ptr == ':');
1329 BOOL is_elision = (is_split && !is_end && *(*ptr+1) == ':');
1331 /* Check if we're at the end of of the a component, or
1332 * if we're at the end of the IPv6 address.
1334 if(is_split || is_end) {
1337 cur_len = *ptr - cur_start;
1339 /* h16 can't have a length > 4. */
1343 TRACE("(%p %p %x): h16 component to long.\n",
1349 /* An h16 component can't have the length of 0 unless
1350 * the elision is at the beginning of the address, or
1351 * at the end of the address.
1353 if(!((*ptr == start && is_elision) ||
1354 (is_end && (*ptr-2) == ip.elision))) {
1356 TRACE("(%p %p %x): IPv6 component can not have a length of 0.\n",
1363 /* An IPv6 address can have no more than 8 h16 components. */
1364 if(ip.h16_count >= 8) {
1366 TRACE("(%p %p %x): Not a IPv6 address, to many h16 components.\n",
1371 ip.components[ip.h16_count].str = cur_start;
1372 ip.components[ip.h16_count].len = cur_len;
1374 TRACE("(%p %p %x): Found h16 component %s, len=%d, h16_count=%d\n",
1375 ptr, data, flags, debugstr_wn(cur_start, cur_len), cur_len,
1385 /* A IPv6 address can only have 1 elision ('::'). */
1389 TRACE("(%p %p %x): IPv6 address cannot have 2 elisions.\n",
1401 if(!check_ipv4address(ptr, TRUE)) {
1402 if(!is_hexdigit(**ptr)) {
1403 /* Not a valid character for an IPv6 address. */
1408 /* Found an IPv4 address. */
1409 ip.ipv4 = cur_start;
1410 ip.ipv4_len = *ptr - cur_start;
1412 TRACE("(%p %p %x): Found an attached IPv4 address %s len=%d.\n",
1413 ptr, data, flags, debugstr_wn(ip.ipv4, ip.ipv4_len),
1416 /* IPv4 addresses can only appear at the end of a IPv6. */
1422 compute_ipv6_comps_size(&ip);
1424 /* Make sure the IPv6 address adds up to 16 bytes. */
1425 if(ip.components_size + ip.elision_size != 16) {
1427 TRACE("(%p %p %x): Invalid IPv6 address, did not add up to 16 bytes.\n",
1432 if(ip.elision_size == 2) {
1433 /* For some reason on Windows if an elision that represents
1434 * only 1 h16 component is encountered at the very begin or
1435 * end of an IPv6 address, Windows does not consider it a
1436 * valid IPv6 address.
1438 * Ex: [::2:3:4:5:6:7] is not valid, even though the sum
1439 * of all the components == 128bits.
1441 if(ip.elision < ip.components[0].str ||
1442 ip.elision > ip.components[ip.h16_count-1].str) {
1444 TRACE("(%p %p %x): Invalid IPv6 address. Detected elision of 2 bytes at the beginning or end of the address.\n",
1450 data->host_type = Uri_HOST_IPV6;
1451 data->has_ipv6 = TRUE;
1452 data->ipv6_address = ip;
1454 TRACE("(%p %p %x): Found valid IPv6 literal %s len=%d\n",
1455 ptr, data, flags, debugstr_wn(start, *ptr-start),
1460 /* IPvFuture = "v" 1*HEXDIG "." 1*( unreserved / sub-delims / ":" ) */
1461 static BOOL parse_ipvfuture(const WCHAR **ptr, parse_data *data, DWORD flags) {
1462 const WCHAR *start = *ptr;
1464 /* IPvFuture has to start with a 'v' or 'V'. */
1465 if(**ptr != 'v' && **ptr != 'V')
1468 /* Following the v their must be atleast 1 hexdigit. */
1470 if(!is_hexdigit(**ptr)) {
1476 while(is_hexdigit(**ptr))
1479 /* End of the hexdigit sequence must be a '.' */
1486 if(!is_unreserved(**ptr) && !is_subdelim(**ptr) && **ptr != ':') {
1492 while(is_unreserved(**ptr) || is_subdelim(**ptr) || **ptr == ':')
1495 data->host_type = Uri_HOST_UNKNOWN;
1497 TRACE("(%p %p %x): Parsed IPvFuture address %s len=%d\n", ptr, data, flags,
1498 debugstr_wn(start, *ptr-start), *ptr-start);
1503 /* IP-literal = "[" ( IPv6address / IPvFuture ) "]" */
1504 static BOOL parse_ip_literal(const WCHAR **ptr, parse_data *data, DWORD flags) {
1513 if(!parse_ipv6address(ptr, data, flags)) {
1514 if(!parse_ipvfuture(ptr, data, flags)) {
1530 /* If a valid port is not found, then let it trickle down to
1533 if(!parse_port(ptr, data, flags)) {
1539 data->host_len = *ptr - data->host;
1544 /* Parses the host information from the URI.
1546 * host = IP-literal / IPv4address / reg-name
1548 static BOOL parse_host(const WCHAR **ptr, parse_data *data, DWORD flags) {
1549 if(!parse_ip_literal(ptr, data, flags)) {
1550 if(!parse_ipv4address(ptr, data, flags)) {
1551 if(!parse_reg_name(ptr, data, flags)) {
1552 TRACE("(%p %p %x): Malformed URI, Unknown host type.\n",
1562 /* Parses the authority information from the URI.
1564 * authority = [ userinfo "@" ] host [ ":" port ]
1566 static BOOL parse_authority(const WCHAR **ptr, parse_data *data, DWORD flags) {
1567 parse_userinfo(ptr, data, flags);
1569 /* Parsing the port will happen during one of the host parsing
1570 * routines (if the URI has a port).
1572 if(!parse_host(ptr, data, flags))
1578 /* Attempts to parse the path information of a hierarchical URI. */
1579 static BOOL parse_path_hierarchical(const WCHAR **ptr, parse_data *data, DWORD flags) {
1580 const WCHAR *start = *ptr;
1581 static const WCHAR slash[] = {'/',0};
1583 if(is_path_delim(**ptr)) {
1584 if(data->scheme_type == URL_SCHEME_WILDCARD) {
1585 /* Wildcard schemes don't get a '/' attached if their path is
1590 } else if(!(flags & Uri_CREATE_NO_CANONICALIZE)) {
1591 /* If the path component is empty, then a '/' is added. */
1596 while(!is_path_delim(**ptr)) {
1597 if(**ptr == '%' && data->scheme_type != URL_SCHEME_UNKNOWN &&
1598 data->scheme_type != URL_SCHEME_FILE) {
1599 if(!check_pct_encoded(ptr)) {
1604 } else if(**ptr == '\\') {
1605 /* Not allowed to have a backslash if NO_CANONICALIZE is set
1606 * and the scheme is known type (but not a file scheme).
1608 if(flags & Uri_CREATE_NO_CANONICALIZE) {
1609 if(data->scheme_type != URL_SCHEME_FILE &&
1610 data->scheme_type != URL_SCHEME_UNKNOWN) {
1620 /* The only time a URI doesn't have a path is when
1621 * the NO_CANONICALIZE flag is set and the raw URI
1622 * didn't contain one.
1629 data->path_len = *ptr - start;
1634 TRACE("(%p %p %x): Parsed path %s len=%d\n", ptr, data, flags,
1635 debugstr_wn(data->path, data->path_len), data->path_len);
1637 TRACE("(%p %p %x): The URI contained no path\n", ptr, data, flags);
1642 /* Parses the path of a opaque URI (much less strict then the parser
1643 * for a hierarchical URI).
1646 * Windows allows invalid % encoded data to appear in opaque URI paths
1647 * for unknown scheme types.
1649 static BOOL parse_path_opaque(const WCHAR **ptr, parse_data *data, DWORD flags) {
1650 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
1654 while(!is_path_delim(**ptr)) {
1655 if(**ptr == '%' && known_scheme) {
1656 if(!check_pct_encoded(ptr)) {
1667 data->path_len = *ptr - data->path;
1668 TRACE("(%p %p %x): Parsed opaque URI path %s len=%d\n", ptr, data, flags,
1669 debugstr_wn(data->path, data->path_len), data->path_len);
1673 /* Determines how the URI should be parsed after the scheme information.
1675 * If the scheme is followed, by "//" then, it is treated as an hierarchical URI
1676 * which then the authority and path information will be parsed out. Otherwise, the
1677 * URI will be treated as an opaque URI which the authority information is not parsed
1680 * RFC 3896 definition of hier-part:
1682 * hier-part = "//" authority path-abempty
1687 * MSDN opaque URI definition:
1688 * scheme ":" path [ "#" fragment ]
1691 * If the URI is of an unknown scheme type and has a "//" following the scheme then it
1692 * is treated as a hierarchical URI, but, if the CREATE_NO_CRACK_UNKNOWN_SCHEMES flag is
1693 * set then it is considered an opaque URI reguardless of what follows the scheme information
1694 * (per MSDN documentation).
1696 static BOOL parse_hierpart(const WCHAR **ptr, parse_data *data, DWORD flags) {
1697 const WCHAR *start = *ptr;
1699 /* Checks if the authority information needs to be parsed.
1701 * Relative URI's aren't hierarchical URI's, but, they could trick
1702 * "check_hierarchical" into thinking it is, so we need to explicitly
1703 * make sure it's not relative. Also, if the URI is an implicit file
1704 * scheme it might not contain a "//", but, it's considered hierarchical
1705 * anyways. Wildcard Schemes are always considered hierarchical
1707 if(data->scheme_type == URL_SCHEME_WILDCARD ||
1708 data->scheme_type == URL_SCHEME_FILE ||
1709 (!data->is_relative && check_hierarchical(ptr))) {
1710 /* Only treat it as a hierarchical URI if the scheme_type is known or
1711 * the Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES flag is not set.
1713 if(data->scheme_type != URL_SCHEME_UNKNOWN ||
1714 !(flags & Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES)) {
1715 TRACE("(%p %p %x): Treating URI as an hierarchical URI.\n", ptr, data, flags);
1716 data->is_opaque = FALSE;
1718 if(data->scheme_type == URL_SCHEME_FILE)
1719 /* Skip past the "//" after the scheme (if any). */
1720 check_hierarchical(ptr);
1722 /* TODO: Handle hierarchical URI's, parse authority then parse the path. */
1723 if(!parse_authority(ptr, data, flags))
1726 return parse_path_hierarchical(ptr, data, flags);
1728 /* Reset ptr to it's starting position so opaque path parsing
1729 * begins at the correct location.
1734 /* If it reaches here, then the URI will be treated as an opaque
1738 TRACE("(%p %p %x): Treating URI as an opaque URI.\n", ptr, data, flags);
1740 data->is_opaque = TRUE;
1741 if(!parse_path_opaque(ptr, data, flags))
1747 /* Attempts to parse the query string from the URI.
1750 * If NO_DECODE_EXTRA_INFO flag is set, then invalid percent encoded
1751 * data is allowed appear in the query string. For unknown scheme types
1752 * invalid percent encoded data is allowed to appear reguardless.
1754 static BOOL parse_query(const WCHAR **ptr, parse_data *data, DWORD flags) {
1755 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
1758 TRACE("(%p %p %x): URI didn't contain a query string.\n", ptr, data, flags);
1765 while(**ptr && **ptr != '#') {
1766 if(**ptr == '%' && known_scheme &&
1767 !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
1768 if(!check_pct_encoded(ptr)) {
1779 data->query_len = *ptr - data->query;
1781 TRACE("(%p %p %x): Parsed query string %s len=%d\n", ptr, data, flags,
1782 debugstr_wn(data->query, data->query_len), data->query_len);
1786 /* Parses and validates the components of the specified by data->uri
1787 * and stores the information it parses into 'data'.
1789 * Returns TRUE if it successfully parsed the URI. False otherwise.
1791 static BOOL parse_uri(parse_data *data, DWORD flags) {
1798 TRACE("(%p %x): BEGINNING TO PARSE URI %s.\n", data, flags, debugstr_w(data->uri));
1800 if(!parse_scheme(pptr, data, flags))
1803 if(!parse_hierpart(pptr, data, flags))
1806 if(!parse_query(pptr, data, flags))
1809 /* TODO: Parse fragment (if the URI has one). */
1811 TRACE("(%p %x): FINISHED PARSING URI.\n", data, flags);
1815 /* Canonicalizes the userinfo of the URI represented by the parse_data.
1817 * Canonicalization of the userinfo is a simple process. If there are any percent
1818 * encoded characters that fall in the "unreserved" character set, they are decoded
1819 * to their actual value. If a character is not in the "unreserved" or "reserved" sets
1820 * then it is percent encoded. Other than that the characters are copied over without
1823 static BOOL canonicalize_userinfo(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
1826 uri->userinfo_start = uri->userinfo_split = -1;
1827 uri->userinfo_len = 0;
1830 /* URI doesn't have userinfo, so nothing to do here. */
1833 uri->userinfo_start = uri->canon_len;
1835 while(i < data->userinfo_len) {
1836 if(data->userinfo[i] == ':' && uri->userinfo_split == -1)
1837 /* Windows only considers the first ':' as the delimiter. */
1838 uri->userinfo_split = uri->canon_len - uri->userinfo_start;
1839 else if(data->userinfo[i] == '%') {
1840 /* Only decode % encoded values for known scheme types. */
1841 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
1842 /* See if the value really needs decoded. */
1843 WCHAR val = decode_pct_val(data->userinfo + i);
1844 if(is_unreserved(val)) {
1846 uri->canon_uri[uri->canon_len] = val;
1850 /* Move pass the hex characters. */
1855 } else if(!is_reserved(data->userinfo[i]) && !is_unreserved(data->userinfo[i]) &&
1856 data->userinfo[i] != '\\') {
1857 /* Only percent encode forbidden characters if the NO_ENCODE_FORBIDDEN_CHARACTERS flag
1860 if(!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS)) {
1862 pct_encode_val(data->userinfo[i], uri->canon_uri + uri->canon_len);
1864 uri->canon_len += 3;
1871 /* Nothing special, so just copy the character over. */
1872 uri->canon_uri[uri->canon_len] = data->userinfo[i];
1878 uri->userinfo_len = uri->canon_len - uri->userinfo_start;
1880 TRACE("(%p %p %x %d): Canonicalized userinfo, userinfo_start=%d, userinfo=%s, userinfo_split=%d userinfo_len=%d.\n",
1881 data, uri, flags, computeOnly, uri->userinfo_start, debugstr_wn(uri->canon_uri + uri->userinfo_start, uri->userinfo_len),
1882 uri->userinfo_split, uri->userinfo_len);
1884 /* Now insert the '@' after the userinfo. */
1886 uri->canon_uri[uri->canon_len] = '@';
1892 /* Attempts to canonicalize a reg_name.
1894 * Things that happen:
1895 * 1) If Uri_CREATE_NO_CANONICALIZE flag is not set, then the reg_name is
1896 * lower cased. Unless it's an unknown scheme type, which case it's
1897 * no lower cased reguardless.
1899 * 2) Unreserved % encoded characters are decoded for known
1902 * 3) Forbidden characters are % encoded as long as
1903 * Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS flag is not set and
1904 * it isn't an unknown scheme type.
1906 * 4) If it's a file scheme and the host is "localhost" it's removed.
1908 static BOOL canonicalize_reg_name(const parse_data *data, Uri *uri,
1909 DWORD flags, BOOL computeOnly) {
1910 static const WCHAR localhostW[] =
1911 {'l','o','c','a','l','h','o','s','t',0};
1913 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
1915 uri->host_start = uri->canon_len;
1917 if(data->scheme_type == URL_SCHEME_FILE &&
1918 data->host_len == lstrlenW(localhostW)) {
1919 if(!StrCmpNIW(data->host, localhostW, data->host_len)) {
1920 uri->host_start = -1;
1922 uri->host_type = Uri_HOST_UNKNOWN;
1927 for(ptr = data->host; ptr < data->host+data->host_len; ++ptr) {
1928 if(*ptr == '%' && known_scheme) {
1929 WCHAR val = decode_pct_val(ptr);
1930 if(is_unreserved(val)) {
1931 /* If NO_CANONICALZE is not set, then windows lower cases the
1934 if(!(flags & Uri_CREATE_NO_CANONICALIZE) && isupperW(val)) {
1936 uri->canon_uri[uri->canon_len] = tolowerW(val);
1939 uri->canon_uri[uri->canon_len] = val;
1943 /* Skip past the % encoded character. */
1947 /* Just copy the % over. */
1949 uri->canon_uri[uri->canon_len] = *ptr;
1952 } else if(*ptr == '\\') {
1953 /* Only unknown scheme types could have made it here with a '\\' in the host name. */
1955 uri->canon_uri[uri->canon_len] = *ptr;
1957 } else if(!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS) &&
1958 !is_unreserved(*ptr) && !is_reserved(*ptr) && known_scheme) {
1960 pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
1962 /* The percent encoded value gets lower cased also. */
1963 if(!(flags & Uri_CREATE_NO_CANONICALIZE)) {
1964 uri->canon_uri[uri->canon_len+1] = tolowerW(uri->canon_uri[uri->canon_len+1]);
1965 uri->canon_uri[uri->canon_len+2] = tolowerW(uri->canon_uri[uri->canon_len+2]);
1969 uri->canon_len += 3;
1972 if(!(flags & Uri_CREATE_NO_CANONICALIZE) && known_scheme)
1973 uri->canon_uri[uri->canon_len] = tolowerW(*ptr);
1975 uri->canon_uri[uri->canon_len] = *ptr;
1982 uri->host_len = uri->canon_len - uri->host_start;
1985 TRACE("(%p %p %x %d): Canonicalize reg_name=%s len=%d\n", data, uri, flags,
1986 computeOnly, debugstr_wn(uri->canon_uri+uri->host_start, uri->host_len),
1990 find_domain_name(uri->canon_uri+uri->host_start, uri->host_len,
1991 &(uri->domain_offset));
1996 /* Attempts to canonicalize an implicit IPv4 address. */
1997 static BOOL canonicalize_implicit_ipv4address(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
1998 uri->host_start = uri->canon_len;
2000 TRACE("%u\n", data->implicit_ipv4);
2001 /* For unknown scheme types Window's doesn't convert
2002 * the value into an IP address, but, it still considers
2003 * it an IPv4 address.
2005 if(data->scheme_type == URL_SCHEME_UNKNOWN) {
2007 memcpy(uri->canon_uri+uri->canon_len, data->host, data->host_len*sizeof(WCHAR));
2008 uri->canon_len += data->host_len;
2011 uri->canon_len += ui2ipv4(uri->canon_uri+uri->canon_len, data->implicit_ipv4);
2013 uri->canon_len += ui2ipv4(NULL, data->implicit_ipv4);
2016 uri->host_len = uri->canon_len - uri->host_start;
2017 uri->host_type = Uri_HOST_IPV4;
2020 TRACE("%p %p %x %d): Canonicalized implicit IP address=%s len=%d\n",
2021 data, uri, flags, computeOnly,
2022 debugstr_wn(uri->canon_uri+uri->host_start, uri->host_len),
2028 /* Attempts to canonicalize an IPv4 address.
2030 * If the parse_data represents a URI that has an implicit IPv4 address
2031 * (ex. http://256/, this function will convert 256 into 0.0.1.0). If
2032 * the implicit IP address exceeds the value of UINT_MAX (maximum value
2033 * for an IPv4 address) it's canonicalized as if were a reg-name.
2035 * If the parse_data contains a partial or full IPv4 address it normalizes it.
2036 * A partial IPv4 address is something like "192.0" and would be normalized to
2037 * "192.0.0.0". With a full (or partial) IPv4 address like "192.002.01.003" would
2038 * be normalized to "192.2.1.3".
2041 * Window's ONLY normalizes IPv4 address for known scheme types (one that isn't
2042 * URL_SCHEME_UNKNOWN). For unknown scheme types, it simply copies the data from
2043 * the original URI into the canonicalized URI, but, it still recognizes URI's
2044 * host type as HOST_IPV4.
2046 static BOOL canonicalize_ipv4address(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2047 if(data->has_implicit_ip)
2048 return canonicalize_implicit_ipv4address(data, uri, flags, computeOnly);
2050 uri->host_start = uri->canon_len;
2052 /* Windows only normalizes for known scheme types. */
2053 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
2054 /* parse_data contains a partial or full IPv4 address, so normalize it. */
2055 DWORD i, octetDigitCount = 0, octetCount = 0;
2056 BOOL octetHasDigit = FALSE;
2058 for(i = 0; i < data->host_len; ++i) {
2059 if(data->host[i] == '0' && !octetHasDigit) {
2060 /* Can ignore leading zeros if:
2061 * 1) It isn't the last digit of the octet.
2062 * 2) i+1 != data->host_len
2065 if(octetDigitCount == 2 ||
2066 i+1 == data->host_len ||
2067 data->host[i+1] == '.') {
2069 uri->canon_uri[uri->canon_len] = data->host[i];
2071 TRACE("Adding zero\n");
2073 } else if(data->host[i] == '.') {
2075 uri->canon_uri[uri->canon_len] = data->host[i];
2078 octetDigitCount = 0;
2079 octetHasDigit = FALSE;
2083 uri->canon_uri[uri->canon_len] = data->host[i];
2087 octetHasDigit = TRUE;
2091 /* Make sure the canonicalized IP address has 4 dec-octets.
2092 * If doesn't add "0" ones until there is 4;
2094 for( ; octetCount < 3; ++octetCount) {
2096 uri->canon_uri[uri->canon_len] = '.';
2097 uri->canon_uri[uri->canon_len+1] = '0';
2100 uri->canon_len += 2;
2103 /* Windows doesn't normalize addresses in unknown schemes. */
2105 memcpy(uri->canon_uri+uri->canon_len, data->host, data->host_len*sizeof(WCHAR));
2106 uri->canon_len += data->host_len;
2109 uri->host_len = uri->canon_len - uri->host_start;
2111 TRACE("(%p %p %x %d): Canonicalized IPv4 address, ip=%s len=%d\n",
2112 data, uri, flags, computeOnly,
2113 debugstr_wn(uri->canon_uri+uri->host_start, uri->host_len),
2120 /* Attempts to canonicalize the IPv6 address of the URI.
2122 * Multiple things happen during the canonicalization of an IPv6 address:
2123 * 1) Any leading zero's in an h16 component are removed.
2124 * Ex: [0001:0022::] -> [1:22::]
2126 * 2) The longest sequence of zero h16 components are compressed
2127 * into a "::" (elision). If there's a tie, the first is choosen.
2129 * Ex: [0:0:0:0:1:6:7:8] -> [::1:6:7:8]
2130 * [0:0:0:0:1:2::] -> [::1:2:0:0]
2131 * [0:0:1:2:0:0:7:8] -> [::1:2:0:0:7:8]
2133 * 3) If an IPv4 address is attached to the IPv6 address, it's
2135 * Ex: [::001.002.022.000] -> [::1.2.22.0]
2137 * 4) If an elision is present, but, only represents 1 h16 component
2140 * Ex: [1::2:3:4:5:6:7] -> [1:0:2:3:4:5:6:7]
2142 * 5) If the IPv6 address contains an IPv4 address and there exists
2143 * at least 1 non-zero h16 component the IPv4 address is converted
2144 * into two h16 components, otherwise it's normalized and kept as is.
2146 * Ex: [::192.200.003.4] -> [::192.200.3.4]
2147 * [ffff::192.200.003.4] -> [ffff::c0c8:3041]
2150 * For unknown scheme types Windows simply copies the address over without any
2153 * IPv4 address can be included in an elision if all its components are 0's.
2155 static BOOL canonicalize_ipv6address(const parse_data *data, Uri *uri,
2156 DWORD flags, BOOL computeOnly) {
2157 uri->host_start = uri->canon_len;
2159 if(data->scheme_type == URL_SCHEME_UNKNOWN) {
2161 memcpy(uri->canon_uri+uri->canon_len, data->host, data->host_len*sizeof(WCHAR));
2162 uri->canon_len += data->host_len;
2166 DWORD i, elision_len;
2168 if(!ipv6_to_number(&(data->ipv6_address), values)) {
2169 TRACE("(%p %p %x %d): Failed to compute numerical value for IPv6 address.\n",
2170 data, uri, flags, computeOnly);
2175 uri->canon_uri[uri->canon_len] = '[';
2178 /* Find where the elision should occur (if any). */
2179 compute_elision_location(&(data->ipv6_address), values, &elision_start, &elision_len);
2181 TRACE("%p %p %x %d): Elision starts at %d, len=%u\n", data, uri, flags,
2182 computeOnly, elision_start, elision_len);
2184 for(i = 0; i < 8; ++i) {
2185 BOOL in_elision = (elision_start > -1 && i >= elision_start &&
2186 i < elision_start+elision_len);
2187 BOOL do_ipv4 = (i == 6 && data->ipv6_address.ipv4 && !in_elision &&
2188 data->ipv6_address.h16_count == 0);
2190 if(i == elision_start) {
2192 uri->canon_uri[uri->canon_len] = ':';
2193 uri->canon_uri[uri->canon_len+1] = ':';
2195 uri->canon_len += 2;
2198 /* We can ignore the current component if we're in the elision. */
2202 /* We only add a ':' if we're not at i == 0, or when we're at
2203 * the very end of elision range since the ':' colon was handled
2204 * earlier. Otherwise we would end up with ":::" after elision.
2206 if(i != 0 && !(elision_start > -1 && i == elision_start+elision_len)) {
2208 uri->canon_uri[uri->canon_len] = ':';
2216 /* Combine the two parts of the IPv4 address values. */
2222 len = ui2ipv4(uri->canon_uri+uri->canon_len, val);
2224 len = ui2ipv4(NULL, val);
2226 uri->canon_len += len;
2229 /* Write a regular h16 component to the URI. */
2231 /* Short circuit for the trivial case. */
2232 if(values[i] == 0) {
2234 uri->canon_uri[uri->canon_len] = '0';
2237 static const WCHAR formatW[] = {'%','x',0};
2240 uri->canon_len += sprintfW(uri->canon_uri+uri->canon_len,
2241 formatW, values[i]);
2244 uri->canon_len += sprintfW(tmp, formatW, values[i]);
2250 /* Add the closing ']'. */
2252 uri->canon_uri[uri->canon_len] = ']';
2256 uri->host_len = uri->canon_len - uri->host_start;
2259 TRACE("(%p %p %x %d): Canonicalized IPv6 address %s, len=%d\n", data, uri, flags,
2260 computeOnly, debugstr_wn(uri->canon_uri+uri->host_start, uri->host_len),
2266 /* Attempts to canonicalize the host of the URI (if any). */
2267 static BOOL canonicalize_host(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2268 uri->host_start = -1;
2270 uri->domain_offset = -1;
2273 switch(data->host_type) {
2275 uri->host_type = Uri_HOST_DNS;
2276 if(!canonicalize_reg_name(data, uri, flags, computeOnly))
2281 uri->host_type = Uri_HOST_IPV4;
2282 if(!canonicalize_ipv4address(data, uri, flags, computeOnly))
2287 if(!canonicalize_ipv6address(data, uri, flags, computeOnly))
2290 uri->host_type = Uri_HOST_IPV6;
2292 case Uri_HOST_UNKNOWN:
2293 if(data->host_len > 0 || data->scheme_type != URL_SCHEME_FILE) {
2294 uri->host_start = uri->canon_len;
2296 /* Nothing happens to unknown host types. */
2298 memcpy(uri->canon_uri+uri->canon_len, data->host, data->host_len*sizeof(WCHAR));
2299 uri->canon_len += data->host_len;
2300 uri->host_len = data->host_len;
2303 uri->host_type = Uri_HOST_UNKNOWN;
2306 FIXME("(%p %p %x %d): Canonicalization for host type %d not supported.\n", data,
2307 uri, flags, computeOnly, data->host_type);
2315 static BOOL canonicalize_port(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2316 BOOL has_default_port = FALSE;
2317 USHORT default_port = 0;
2320 uri->has_port = FALSE;
2322 /* Check if the scheme has a default port. */
2323 for(i = 0; i < sizeof(default_ports)/sizeof(default_ports[0]); ++i) {
2324 if(default_ports[i].scheme == data->scheme_type) {
2325 has_default_port = TRUE;
2326 default_port = default_ports[i].port;
2331 if(data->port || has_default_port)
2332 uri->has_port = TRUE;
2335 * 1) Has a port which is the default port.
2336 * 2) Has a port (not the default).
2337 * 3) Doesn't have a port, but, scheme has a default port.
2340 if(has_default_port && data->port && data->port_value == default_port) {
2341 /* If it's the default port and this flag isn't set, don't do anything. */
2342 if(flags & Uri_CREATE_NO_CANONICALIZE) {
2343 /* Copy the original port over. */
2345 uri->canon_uri[uri->canon_len] = ':';
2346 memcpy(uri->canon_uri+uri->canon_len+1, data->port, data->port_len*sizeof(WCHAR));
2348 uri->canon_len += data->port_len+1;
2351 uri->port = default_port;
2352 } else if(data->port) {
2354 uri->canon_uri[uri->canon_len] = ':';
2357 if(flags & Uri_CREATE_NO_CANONICALIZE) {
2358 /* Copy the original over without changes. */
2360 memcpy(uri->canon_uri+uri->canon_len, data->port, data->port_len*sizeof(WCHAR));
2361 uri->canon_len += data->port_len;
2363 const WCHAR formatW[] = {'%','u',0};
2366 len = sprintfW(uri->canon_uri+uri->canon_len, formatW, data->port_value);
2369 len = sprintfW(tmp, formatW, data->port_value);
2371 uri->canon_len += len;
2374 uri->port = data->port_value;
2375 } else if(has_default_port)
2376 uri->port = default_port;
2381 /* Canonicalizes the authority of the URI represented by the parse_data. */
2382 static BOOL canonicalize_authority(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2383 uri->authority_start = uri->canon_len;
2384 uri->authority_len = 0;
2386 if(!canonicalize_userinfo(data, uri, flags, computeOnly))
2389 if(!canonicalize_host(data, uri, flags, computeOnly))
2392 if(!canonicalize_port(data, uri, flags, computeOnly))
2395 if(uri->host_start != -1)
2396 uri->authority_len = uri->canon_len - uri->authority_start;
2398 uri->authority_start = -1;
2403 /* Attempts to canonicalize the path of a hierarchical URI.
2405 * Things that happen:
2406 * 1). Forbidden characters are percent encoded, unless the NO_ENCODE_FORBIDDEN
2407 * flag is set or it's a file URI. Forbidden characters are always encoded
2408 * for file schemes reguardless and forbidden characters are never encoded
2409 * for unknown scheme types.
2411 * 2). For known scheme types '\\' are changed to '/'.
2413 * 3). Percent encoded, unreserved characters are decoded to their actual values.
2414 * Unless the scheme type is unknown. For file schemes any percent encoded
2415 * character in the unreserved or reserved set is decoded.
2417 * 4). For File schemes if the path is starts with a drive letter and doesn't
2418 * start with a '/' then one is appended.
2419 * Ex: file://c:/test.mp3 -> file:///c:/test.mp3
2421 * 5). Dot segments are removed from the path for all scheme types
2422 * unless NO_CANONICALIZE flag is set. Dot segments aren't removed
2423 * for wildcard scheme types.
2426 * file://c:/test%20test -> file:///c:/test%2520test
2427 * file://c:/test%3Etest -> file:///c:/test%253Etest
2428 * file:///c:/test%20test -> file:///c:/test%20test
2429 * file:///c:/test%test -> file:///c:/test%25test
2431 static BOOL canonicalize_path_hierarchical(const parse_data *data, Uri *uri,
2432 DWORD flags, BOOL computeOnly) {
2434 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
2435 const BOOL is_file = data->scheme_type == URL_SCHEME_FILE;
2437 BOOL escape_pct = FALSE;
2440 uri->path_start = -1;
2445 uri->path_start = uri->canon_len;
2447 /* Check if a '/' needs to be appended for the file scheme. */
2449 if(data->path_len > 1 && is_alpha(*(data->path)) &&
2450 *(data->path+1) == ':') {
2452 uri->canon_uri[uri->canon_len] = '/';
2458 for(ptr = data->path; ptr < data->path+data->path_len; ++ptr) {
2460 const WCHAR *tmp = ptr;
2463 /* Check if the % represents a valid encoded char, or if it needs encoded. */
2464 BOOL force_encode = !check_pct_encoded(&tmp) && is_file;
2465 val = decode_pct_val(ptr);
2467 if(force_encode || escape_pct) {
2468 /* Escape the percent sign in the file URI. */
2470 pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
2471 uri->canon_len += 3;
2472 } else if((is_unreserved(val) && known_scheme) ||
2473 (is_file && (is_unreserved(val) || is_reserved(val)))) {
2475 uri->canon_uri[uri->canon_len] = val;
2482 uri->canon_uri[uri->canon_len] = *ptr;
2485 } else if(*ptr == '\\' && known_scheme) {
2487 uri->canon_uri[uri->canon_len] = '/';
2489 } else if(known_scheme && !is_unreserved(*ptr) && !is_reserved(*ptr) &&
2490 (!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS) || is_file)) {
2491 /* Escape the forbidden character. */
2493 pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
2494 uri->canon_len += 3;
2497 uri->canon_uri[uri->canon_len] = *ptr;
2502 uri->path_len = uri->canon_len - uri->path_start;
2504 /* Removing the dot segments only happens when it's not in
2505 * computeOnly mode and it's not a wildcard scheme.
2507 if(!computeOnly && data->scheme_type != URL_SCHEME_WILDCARD) {
2508 if(!(flags & Uri_CREATE_NO_CANONICALIZE)) {
2509 /* Remove the dot segments (if any) and reset everything to the new
2512 DWORD new_len = remove_dot_segments(uri->canon_uri+uri->path_start, uri->path_len);
2513 uri->canon_len -= uri->path_len-new_len;
2514 uri->path_len = new_len;
2519 TRACE("Canonicalized path %s len=%d\n",
2520 debugstr_wn(uri->canon_uri+uri->path_start, uri->path_len),
2526 /* Attempts to canonicalize the path for an opaque URI.
2528 * For known scheme types:
2529 * 1) forbidden characters are percent encoded if
2530 * NO_ENCODE_FORBIDDEN_CHARACTERS isn't set.
2532 * 2) Percent encoded, unreserved characters are decoded
2533 * to their actual values, for known scheme types.
2535 * 3) '\\' are changed to '/' for known scheme types
2536 * except for mailto schemes.
2538 static BOOL canonicalize_path_opaque(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2540 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
2543 uri->path_start = -1;
2548 uri->path_start = uri->canon_len;
2550 /* Windows doesn't allow a "//" to appear after the scheme
2551 * of a URI, if it's an opaque URI.
2553 if(data->scheme && *(data->path) == '/' && *(data->path+1) == '/') {
2554 /* So it inserts a "/." before the "//" if it exists. */
2556 uri->canon_uri[uri->canon_len] = '/';
2557 uri->canon_uri[uri->canon_len+1] = '.';
2560 uri->canon_len += 2;
2563 for(ptr = data->path; ptr < data->path+data->path_len; ++ptr) {
2564 if(*ptr == '%' && known_scheme) {
2565 WCHAR val = decode_pct_val(ptr);
2567 if(is_unreserved(val)) {
2569 uri->canon_uri[uri->canon_len] = val;
2576 uri->canon_uri[uri->canon_len] = *ptr;
2579 } else if(known_scheme && !is_unreserved(*ptr) && !is_reserved(*ptr) &&
2580 !(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS)) {
2582 pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
2583 uri->canon_len += 3;
2586 uri->canon_uri[uri->canon_len] = *ptr;
2591 uri->path_len = uri->canon_len - uri->path_start;
2593 TRACE("(%p %p %x %d): Canonicalized opaque URI path %s len=%d\n", data, uri, flags, computeOnly,
2594 debugstr_wn(uri->canon_uri+uri->path_start, uri->path_len), uri->path_len);
2598 /* Determines how the URI represented by the parse_data should be canonicalized.
2600 * Essentially, if the parse_data represents an hierarchical URI then it calls
2601 * canonicalize_authority and the canonicalization functions for the path. If the
2602 * URI is opaque it canonicalizes the path of the URI.
2604 static BOOL canonicalize_hierpart(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2605 if(!data->is_opaque) {
2606 /* "//" is only added for non-wildcard scheme types. */
2607 if(data->scheme_type != URL_SCHEME_WILDCARD) {
2609 INT pos = uri->canon_len;
2611 uri->canon_uri[pos] = '/';
2612 uri->canon_uri[pos+1] = '/';
2614 uri->canon_len += 2;
2617 if(!canonicalize_authority(data, uri, flags, computeOnly))
2620 /* TODO: Canonicalize the path of the URI. */
2621 if(!canonicalize_path_hierarchical(data, uri, flags, computeOnly))
2625 /* Opaque URI's don't have an authority. */
2626 uri->userinfo_start = uri->userinfo_split = -1;
2627 uri->userinfo_len = 0;
2628 uri->host_start = -1;
2630 uri->host_type = Uri_HOST_UNKNOWN;
2631 uri->has_port = FALSE;
2632 uri->authority_start = -1;
2633 uri->authority_len = 0;
2634 uri->domain_offset = -1;
2636 if(!canonicalize_path_opaque(data, uri, flags, computeOnly))
2640 if(uri->path_start > -1 && !computeOnly)
2641 /* Finding file extensions happens for both types of URIs. */
2642 uri->extension_offset = find_file_extension(uri->canon_uri+uri->path_start, uri->path_len);
2644 uri->extension_offset = -1;
2649 /* Attempts to canonicalize the query string of the URI.
2651 * Things that happen:
2652 * 1) For known scheme types forbidden characters
2653 * are percent encoded, unless the NO_DECODE_EXTRA_INFO flag is set
2654 * or NO_ENCODE_FORBIDDEN_CHARACTERS is set.
2656 * 2) For known scheme types, percent encoded, unreserved characters
2657 * are decoded as long as the NO_DECODE_EXTRA_INFO flag isn't set.
2659 static BOOL canonicalize_query(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2660 const WCHAR *ptr, *end;
2661 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
2664 uri->query_start = -1;
2669 uri->query_start = uri->canon_len;
2671 end = data->query+data->query_len;
2672 for(ptr = data->query; ptr < end; ++ptr) {
2674 if(known_scheme && !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
2675 WCHAR val = decode_pct_val(ptr);
2676 if(is_unreserved(val)) {
2678 uri->canon_uri[uri->canon_len] = val;
2685 } else if(known_scheme && !is_unreserved(*ptr) && !is_reserved(*ptr)) {
2686 if(!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS) &&
2687 !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
2689 pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
2690 uri->canon_len += 3;
2696 uri->canon_uri[uri->canon_len] = *ptr;
2700 uri->query_len = uri->canon_len - uri->query_start;
2703 TRACE("(%p %p %x %d): Canonicalized query string %s len=%d\n", data, uri, flags,
2704 computeOnly, debugstr_wn(uri->canon_uri+uri->query_start, uri->query_len),
2709 /* Canonicalizes the scheme information specified in the parse_data using the specified flags. */
2710 static BOOL canonicalize_scheme(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2711 uri->scheme_start = -1;
2712 uri->scheme_len = 0;
2715 /* The only type of URI that doesn't have to have a scheme is a relative
2718 if(!data->is_relative) {
2719 FIXME("(%p %p %x): Unable to determine the scheme type of %s.\n", data,
2720 uri, flags, debugstr_w(data->uri));
2726 INT pos = uri->canon_len;
2728 for(i = 0; i < data->scheme_len; ++i) {
2729 /* Scheme name must be lower case after canonicalization. */
2730 uri->canon_uri[i + pos] = tolowerW(data->scheme[i]);
2733 uri->canon_uri[i + pos] = ':';
2734 uri->scheme_start = pos;
2736 TRACE("(%p %p %x): Canonicalized scheme=%s, len=%d.\n", data, uri, flags,
2737 debugstr_wn(uri->canon_uri, uri->scheme_len), data->scheme_len);
2740 /* This happens in both computation modes. */
2741 uri->canon_len += data->scheme_len + 1;
2742 uri->scheme_len = data->scheme_len;
2747 /* Compute's what the length of the URI specified by the parse_data will be
2748 * after canonicalization occurs using the specified flags.
2750 * This function will return a non-zero value indicating the length of the canonicalized
2751 * URI, or -1 on error.
2753 static int compute_canonicalized_length(const parse_data *data, DWORD flags) {
2756 memset(&uri, 0, sizeof(Uri));
2758 TRACE("(%p %x): Beginning to compute canonicalized length for URI %s\n", data, flags,
2759 debugstr_w(data->uri));
2761 if(!canonicalize_scheme(data, &uri, flags, TRUE)) {
2762 ERR("(%p %x): Failed to compute URI scheme length.\n", data, flags);
2766 if(!canonicalize_hierpart(data, &uri, flags, TRUE)) {
2767 ERR("(%p %x): Failed to compute URI hierpart length.\n", data, flags);
2771 if(!canonicalize_query(data, &uri, flags, TRUE)) {
2772 ERR("(%p %x): Failed to compute query string length.\n", data, flags);
2776 TRACE("(%p %x): Finished computing canonicalized URI length. length=%d\n", data, flags, uri.canon_len);
2778 return uri.canon_len;
2781 /* Canonicalizes the URI data specified in the parse_data, using the given flags. If the
2782 * canonicalization succeededs it will store all the canonicalization information
2783 * in the pointer to the Uri.
2785 * To canonicalize a URI this function first computes what the length of the URI
2786 * specified by the parse_data will be. Once this is done it will then perfom the actual
2787 * canonicalization of the URI.
2789 static HRESULT canonicalize_uri(const parse_data *data, Uri *uri, DWORD flags) {
2792 uri->canon_uri = NULL;
2793 len = uri->canon_size = uri->canon_len = 0;
2795 TRACE("(%p %p %x): beginning to canonicalize URI %s.\n", data, uri, flags, debugstr_w(data->uri));
2797 /* First try to compute the length of the URI. */
2798 len = compute_canonicalized_length(data, flags);
2800 ERR("(%p %p %x): Could not compute the canonicalized length of %s.\n", data, uri, flags,
2801 debugstr_w(data->uri));
2802 return E_INVALIDARG;
2805 uri->canon_uri = heap_alloc((len+1)*sizeof(WCHAR));
2807 return E_OUTOFMEMORY;
2809 uri->canon_size = len;
2810 if(!canonicalize_scheme(data, uri, flags, FALSE)) {
2811 ERR("(%p %p %x): Unable to canonicalize the scheme of the URI.\n", data, uri, flags);
2812 heap_free(uri->canon_uri);
2813 return E_INVALIDARG;
2815 uri->scheme_type = data->scheme_type;
2817 if(!canonicalize_hierpart(data, uri, flags, FALSE)) {
2818 ERR("(%p %p %x): Unable to canonicalize the heirpart of the URI\n", data, uri, flags);
2819 heap_free(uri->canon_uri);
2820 return E_INVALIDARG;
2823 if(!canonicalize_query(data, uri, flags, FALSE)) {
2824 ERR("(%p %p %x): Unable to canonicalize query string of the URI.\n",
2826 return E_INVALIDARG;
2829 /* There's a possibility we didn't use all the space we allocated
2832 if(uri->canon_len < uri->canon_size) {
2833 /* This happens if the URI is hierarchical and dot
2834 * segments were removed from it's path.
2836 WCHAR *tmp = heap_realloc(uri->canon_uri, (uri->canon_len+1)*sizeof(WCHAR));
2838 return E_OUTOFMEMORY;
2840 uri->canon_uri = tmp;
2841 uri->canon_size = uri->canon_len;
2844 uri->canon_uri[uri->canon_len] = '\0';
2845 TRACE("(%p %p %x): finished canonicalizing the URI. uri=%s\n", data, uri, flags, debugstr_w(uri->canon_uri));
2850 #define URI(x) ((IUri*) &(x)->lpIUriVtbl)
2851 #define URIBUILDER(x) ((IUriBuilder*) &(x)->lpIUriBuilderVtbl)
2853 #define URI_THIS(iface) DEFINE_THIS(Uri, IUri, iface)
2855 static HRESULT WINAPI Uri_QueryInterface(IUri *iface, REFIID riid, void **ppv)
2857 Uri *This = URI_THIS(iface);
2859 if(IsEqualGUID(&IID_IUnknown, riid)) {
2860 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
2862 }else if(IsEqualGUID(&IID_IUri, riid)) {
2863 TRACE("(%p)->(IID_IUri %p)\n", This, ppv);
2866 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
2868 return E_NOINTERFACE;
2871 IUnknown_AddRef((IUnknown*)*ppv);
2875 static ULONG WINAPI Uri_AddRef(IUri *iface)
2877 Uri *This = URI_THIS(iface);
2878 LONG ref = InterlockedIncrement(&This->ref);
2880 TRACE("(%p) ref=%d\n", This, ref);
2885 static ULONG WINAPI Uri_Release(IUri *iface)
2887 Uri *This = URI_THIS(iface);
2888 LONG ref = InterlockedDecrement(&This->ref);
2890 TRACE("(%p) ref=%d\n", This, ref);
2893 SysFreeString(This->raw_uri);
2894 heap_free(This->canon_uri);
2901 static HRESULT WINAPI Uri_GetPropertyBSTR(IUri *iface, Uri_PROPERTY uriProp, BSTR *pbstrProperty, DWORD dwFlags)
2903 Uri *This = URI_THIS(iface);
2905 TRACE("(%p)->(%d %p %x)\n", This, uriProp, pbstrProperty, dwFlags);
2910 if(uriProp > Uri_PROPERTY_STRING_LAST) {
2911 /* Windows allocates an empty BSTR for invalid Uri_PROPERTY's. */
2912 *pbstrProperty = SysAllocStringLen(NULL, 0);
2913 if(!(*pbstrProperty))
2914 return E_OUTOFMEMORY;
2916 /* It only returns S_FALSE for the ZONE property... */
2917 if(uriProp == Uri_PROPERTY_ZONE)
2923 /* Don't have support for flags yet. */
2925 FIXME("(%p)->(%d %p %x)\n", This, uriProp, pbstrProperty, dwFlags);
2930 case Uri_PROPERTY_AUTHORITY:
2931 if(This->authority_start > -1) {
2932 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->authority_start, This->authority_len);
2935 *pbstrProperty = SysAllocStringLen(NULL, 0);
2939 if(!(*pbstrProperty))
2940 hres = E_OUTOFMEMORY;
2943 case Uri_PROPERTY_DOMAIN:
2944 if(This->domain_offset > -1) {
2945 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->host_start+This->domain_offset,
2946 This->host_len-This->domain_offset);
2949 *pbstrProperty = SysAllocStringLen(NULL, 0);
2953 if(!(*pbstrProperty))
2954 hres = E_OUTOFMEMORY;
2957 case Uri_PROPERTY_EXTENSION:
2958 if(This->extension_offset > -1) {
2959 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->path_start+This->extension_offset,
2960 This->path_len-This->extension_offset);
2963 *pbstrProperty = SysAllocStringLen(NULL, 0);
2967 if(!(*pbstrProperty))
2968 hres = E_OUTOFMEMORY;
2971 case Uri_PROPERTY_HOST:
2972 if(This->host_start > -1) {
2973 /* The '[' and ']' aren't included for IPv6 addresses. */
2974 if(This->host_type == Uri_HOST_IPV6)
2975 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->host_start+1, This->host_len-2);
2977 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->host_start, This->host_len);
2981 *pbstrProperty = SysAllocStringLen(NULL, 0);
2985 if(!(*pbstrProperty))
2986 hres = E_OUTOFMEMORY;
2989 case Uri_PROPERTY_PASSWORD:
2990 if(This->userinfo_split > -1) {
2991 *pbstrProperty = SysAllocStringLen(
2992 This->canon_uri+This->userinfo_start+This->userinfo_split+1,
2993 This->userinfo_len-This->userinfo_split-1);
2996 *pbstrProperty = SysAllocStringLen(NULL, 0);
3000 if(!(*pbstrProperty))
3001 return E_OUTOFMEMORY;
3004 case Uri_PROPERTY_PATH:
3005 if(This->path_start > -1) {
3006 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->path_start, This->path_len);
3009 *pbstrProperty = SysAllocStringLen(NULL, 0);
3013 if(!(*pbstrProperty))
3014 hres = E_OUTOFMEMORY;
3017 case Uri_PROPERTY_RAW_URI:
3018 *pbstrProperty = SysAllocString(This->raw_uri);
3019 if(!(*pbstrProperty))
3020 hres = E_OUTOFMEMORY;
3024 case Uri_PROPERTY_SCHEME_NAME:
3025 if(This->scheme_start > -1) {
3026 *pbstrProperty = SysAllocStringLen(This->canon_uri + This->scheme_start, This->scheme_len);
3029 *pbstrProperty = SysAllocStringLen(NULL, 0);
3033 if(!(*pbstrProperty))
3034 hres = E_OUTOFMEMORY;
3037 case Uri_PROPERTY_USER_INFO:
3038 if(This->userinfo_start > -1) {
3039 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->userinfo_start, This->userinfo_len);
3042 *pbstrProperty = SysAllocStringLen(NULL, 0);
3046 if(!(*pbstrProperty))
3047 hres = E_OUTOFMEMORY;
3050 case Uri_PROPERTY_USER_NAME:
3051 if(This->userinfo_start > -1) {
3052 /* If userinfo_split is set, that means a password exists
3053 * so the username is only from userinfo_start to userinfo_split.
3055 if(This->userinfo_split > -1) {
3056 *pbstrProperty = SysAllocStringLen(This->canon_uri + This->userinfo_start, This->userinfo_split);
3059 *pbstrProperty = SysAllocStringLen(This->canon_uri + This->userinfo_start, This->userinfo_len);
3063 *pbstrProperty = SysAllocStringLen(NULL, 0);
3067 if(!(*pbstrProperty))
3068 return E_OUTOFMEMORY;
3072 FIXME("(%p)->(%d %p %x)\n", This, uriProp, pbstrProperty, dwFlags);
3079 static HRESULT WINAPI Uri_GetPropertyLength(IUri *iface, Uri_PROPERTY uriProp, DWORD *pcchProperty, DWORD dwFlags)
3081 Uri *This = URI_THIS(iface);
3083 TRACE("(%p)->(%d %p %x)\n", This, uriProp, pcchProperty, dwFlags);
3086 return E_INVALIDARG;
3088 /* Can only return a length for a property if it's a string. */
3089 if(uriProp > Uri_PROPERTY_STRING_LAST)
3090 return E_INVALIDARG;
3092 /* Don't have support for flags yet. */
3094 FIXME("(%p)->(%d %p %x)\n", This, uriProp, pcchProperty, dwFlags);
3099 case Uri_PROPERTY_AUTHORITY:
3100 *pcchProperty = This->authority_len;
3101 hres = (This->authority_start > -1) ? S_OK : S_FALSE;
3103 case Uri_PROPERTY_DOMAIN:
3104 if(This->domain_offset > -1)
3105 *pcchProperty = This->host_len - This->domain_offset;
3109 hres = (This->domain_offset > -1) ? S_OK : S_FALSE;
3111 case Uri_PROPERTY_EXTENSION:
3112 if(This->extension_offset > -1) {
3113 *pcchProperty = This->path_len - This->extension_offset;
3121 case Uri_PROPERTY_HOST:
3122 *pcchProperty = This->host_len;
3124 /* '[' and ']' aren't included in the length. */
3125 if(This->host_type == Uri_HOST_IPV6)
3128 hres = (This->host_start > -1) ? S_OK : S_FALSE;
3130 case Uri_PROPERTY_PASSWORD:
3131 *pcchProperty = (This->userinfo_split > -1) ? This->userinfo_len-This->userinfo_split-1 : 0;
3132 hres = (This->userinfo_split > -1) ? S_OK : S_FALSE;
3134 case Uri_PROPERTY_PATH:
3135 *pcchProperty = This->path_len;
3136 hres = (This->path_start > -1) ? S_OK : S_FALSE;
3138 case Uri_PROPERTY_RAW_URI:
3139 *pcchProperty = SysStringLen(This->raw_uri);
3142 case Uri_PROPERTY_SCHEME_NAME:
3143 *pcchProperty = This->scheme_len;
3144 hres = (This->scheme_start > -1) ? S_OK : S_FALSE;
3146 case Uri_PROPERTY_USER_INFO:
3147 *pcchProperty = This->userinfo_len;
3148 hres = (This->userinfo_start > -1) ? S_OK : S_FALSE;
3150 case Uri_PROPERTY_USER_NAME:
3151 *pcchProperty = (This->userinfo_split > -1) ? This->userinfo_split : This->userinfo_len;
3152 hres = (This->userinfo_start > -1) ? S_OK : S_FALSE;
3155 FIXME("(%p)->(%d %p %x)\n", This, uriProp, pcchProperty, dwFlags);
3162 static HRESULT WINAPI Uri_GetPropertyDWORD(IUri *iface, Uri_PROPERTY uriProp, DWORD *pcchProperty, DWORD dwFlags)
3164 Uri *This = URI_THIS(iface);
3167 TRACE("(%p)->(%d %p %x)\n", This, uriProp, pcchProperty, dwFlags);
3170 return E_INVALIDARG;
3172 /* Microsoft's implementation for the ZONE property of a URI seems to be lacking...
3173 * From what I can tell, instead of checking which URLZONE the URI belongs to it
3174 * simply assigns URLZONE_INVALID and returns E_NOTIMPL. This also applies to the GetZone
3177 if(uriProp == Uri_PROPERTY_ZONE) {
3178 *pcchProperty = URLZONE_INVALID;
3182 if(uriProp < Uri_PROPERTY_DWORD_START) {
3184 return E_INVALIDARG;
3188 case Uri_PROPERTY_HOST_TYPE:
3189 *pcchProperty = This->host_type;
3192 case Uri_PROPERTY_PORT:
3193 if(!This->has_port) {
3197 *pcchProperty = This->port;
3202 case Uri_PROPERTY_SCHEME:
3203 *pcchProperty = This->scheme_type;
3207 FIXME("(%p)->(%d %p %x)\n", This, uriProp, pcchProperty, dwFlags);
3214 static HRESULT WINAPI Uri_HasProperty(IUri *iface, Uri_PROPERTY uriProp, BOOL *pfHasProperty)
3216 Uri *This = URI_THIS(iface);
3217 FIXME("(%p)->(%d %p)\n", This, uriProp, pfHasProperty);
3220 return E_INVALIDARG;
3225 static HRESULT WINAPI Uri_GetAbsoluteUri(IUri *iface, BSTR *pstrAbsoluteUri)
3227 Uri *This = URI_THIS(iface);
3228 FIXME("(%p)->(%p)\n", This, pstrAbsoluteUri);
3230 if(!pstrAbsoluteUri)
3236 static HRESULT WINAPI Uri_GetAuthority(IUri *iface, BSTR *pstrAuthority)
3238 TRACE("(%p)->(%p)\n", iface, pstrAuthority);
3239 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_AUTHORITY, pstrAuthority, 0);
3242 static HRESULT WINAPI Uri_GetDisplayUri(IUri *iface, BSTR *pstrDisplayUri)
3244 Uri *This = URI_THIS(iface);
3245 FIXME("(%p)->(%p)\n", This, pstrDisplayUri);
3253 static HRESULT WINAPI Uri_GetDomain(IUri *iface, BSTR *pstrDomain)
3255 TRACE("(%p)->(%p)\n", iface, pstrDomain);
3256 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_DOMAIN, pstrDomain, 0);
3259 static HRESULT WINAPI Uri_GetExtension(IUri *iface, BSTR *pstrExtension)
3261 TRACE("(%p)->(%p)\n", iface, pstrExtension);
3262 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_EXTENSION, pstrExtension, 0);
3265 static HRESULT WINAPI Uri_GetFragment(IUri *iface, BSTR *pstrFragment)
3267 Uri *This = URI_THIS(iface);
3268 FIXME("(%p)->(%p)\n", This, pstrFragment);
3276 static HRESULT WINAPI Uri_GetHost(IUri *iface, BSTR *pstrHost)
3278 TRACE("(%p)->(%p)\n", iface, pstrHost);
3279 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_HOST, pstrHost, 0);
3282 static HRESULT WINAPI Uri_GetPassword(IUri *iface, BSTR *pstrPassword)
3284 TRACE("(%p)->(%p)\n", iface, pstrPassword);
3285 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_PASSWORD, pstrPassword, 0);
3288 static HRESULT WINAPI Uri_GetPath(IUri *iface, BSTR *pstrPath)
3290 TRACE("(%p)->(%p)\n", iface, pstrPath);
3291 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_PATH, pstrPath, 0);
3294 static HRESULT WINAPI Uri_GetPathAndQuery(IUri *iface, BSTR *pstrPathAndQuery)
3296 Uri *This = URI_THIS(iface);
3297 FIXME("(%p)->(%p)\n", This, pstrPathAndQuery);
3299 if(!pstrPathAndQuery)
3305 static HRESULT WINAPI Uri_GetQuery(IUri *iface, BSTR *pstrQuery)
3307 Uri *This = URI_THIS(iface);
3308 FIXME("(%p)->(%p)\n", This, pstrQuery);
3316 static HRESULT WINAPI Uri_GetRawUri(IUri *iface, BSTR *pstrRawUri)
3318 Uri *This = URI_THIS(iface);
3319 TRACE("(%p)->(%p)\n", This, pstrRawUri);
3321 /* Just forward the call to GetPropertyBSTR. */
3322 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_RAW_URI, pstrRawUri, 0);
3325 static HRESULT WINAPI Uri_GetSchemeName(IUri *iface, BSTR *pstrSchemeName)
3327 Uri *This = URI_THIS(iface);
3328 TRACE("(%p)->(%p)\n", This, pstrSchemeName);
3329 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_SCHEME_NAME, pstrSchemeName, 0);
3332 static HRESULT WINAPI Uri_GetUserInfo(IUri *iface, BSTR *pstrUserInfo)
3334 TRACE("(%p)->(%p)\n", iface, pstrUserInfo);
3335 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_USER_INFO, pstrUserInfo, 0);
3338 static HRESULT WINAPI Uri_GetUserName(IUri *iface, BSTR *pstrUserName)
3340 TRACE("(%p)->(%p)\n", iface, pstrUserName);
3341 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_USER_NAME, pstrUserName, 0);
3344 static HRESULT WINAPI Uri_GetHostType(IUri *iface, DWORD *pdwHostType)
3346 TRACE("(%p)->(%p)\n", iface, pdwHostType);
3347 return Uri_GetPropertyDWORD(iface, Uri_PROPERTY_HOST_TYPE, pdwHostType, 0);
3350 static HRESULT WINAPI Uri_GetPort(IUri *iface, DWORD *pdwPort)
3352 TRACE("(%p)->(%p)\n", iface, pdwPort);
3353 return Uri_GetPropertyDWORD(iface, Uri_PROPERTY_PORT, pdwPort, 0);
3356 static HRESULT WINAPI Uri_GetScheme(IUri *iface, DWORD *pdwScheme)
3358 Uri *This = URI_THIS(iface);
3359 TRACE("(%p)->(%p)\n", This, pdwScheme);
3360 return Uri_GetPropertyDWORD(iface, Uri_PROPERTY_SCHEME, pdwScheme, 0);
3363 static HRESULT WINAPI Uri_GetZone(IUri *iface, DWORD *pdwZone)
3365 TRACE("(%p)->(%p)\n", iface, pdwZone);
3366 return Uri_GetPropertyDWORD(iface, Uri_PROPERTY_ZONE,pdwZone, 0);
3369 static HRESULT WINAPI Uri_GetProperties(IUri *iface, DWORD *pdwProperties)
3371 Uri *This = URI_THIS(iface);
3372 FIXME("(%p)->(%p)\n", This, pdwProperties);
3375 return E_INVALIDARG;
3380 static HRESULT WINAPI Uri_IsEqual(IUri *iface, IUri *pUri, BOOL *pfEqual)
3382 Uri *This = URI_THIS(iface);
3383 TRACE("(%p)->(%p %p)\n", This, pUri, pfEqual);
3391 /* For some reason Windows returns S_OK here... */
3395 FIXME("(%p)->(%p %p)\n", This, pUri, pfEqual);
3401 static const IUriVtbl UriVtbl = {
3405 Uri_GetPropertyBSTR,
3406 Uri_GetPropertyLength,
3407 Uri_GetPropertyDWORD,
3418 Uri_GetPathAndQuery,
3432 /***********************************************************************
3433 * CreateUri (urlmon.@)
3435 HRESULT WINAPI CreateUri(LPCWSTR pwzURI, DWORD dwFlags, DWORD_PTR dwReserved, IUri **ppURI)
3441 TRACE("(%s %x %x %p)\n", debugstr_w(pwzURI), dwFlags, (DWORD)dwReserved, ppURI);
3444 return E_INVALIDARG;
3448 return E_INVALIDARG;
3451 ret = heap_alloc(sizeof(Uri));
3453 return E_OUTOFMEMORY;
3455 ret->lpIUriVtbl = &UriVtbl;
3458 /* Create a copy of pwzURI and store it as the raw_uri. */
3459 ret->raw_uri = SysAllocString(pwzURI);
3462 return E_OUTOFMEMORY;
3465 memset(&data, 0, sizeof(parse_data));
3466 data.uri = ret->raw_uri;
3468 /* Validate and parse the URI into it's components. */
3469 if(!parse_uri(&data, dwFlags)) {
3470 /* Encountered an unsupported or invalid URI */
3471 SysFreeString(ret->raw_uri);
3474 return E_INVALIDARG;
3477 /* Canonicalize the URI. */
3478 hr = canonicalize_uri(&data, ret, dwFlags);
3480 SysFreeString(ret->raw_uri);
3490 #define URIBUILDER_THIS(iface) DEFINE_THIS(UriBuilder, IUriBuilder, iface)
3492 static HRESULT WINAPI UriBuilder_QueryInterface(IUriBuilder *iface, REFIID riid, void **ppv)
3494 UriBuilder *This = URIBUILDER_THIS(iface);
3496 if(IsEqualGUID(&IID_IUnknown, riid)) {
3497 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
3498 *ppv = URIBUILDER(This);
3499 }else if(IsEqualGUID(&IID_IUriBuilder, riid)) {
3500 TRACE("(%p)->(IID_IUri %p)\n", This, ppv);
3501 *ppv = URIBUILDER(This);
3503 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
3505 return E_NOINTERFACE;
3508 IUnknown_AddRef((IUnknown*)*ppv);
3512 static ULONG WINAPI UriBuilder_AddRef(IUriBuilder *iface)
3514 UriBuilder *This = URIBUILDER_THIS(iface);
3515 LONG ref = InterlockedIncrement(&This->ref);
3517 TRACE("(%p) ref=%d\n", This, ref);
3522 static ULONG WINAPI UriBuilder_Release(IUriBuilder *iface)
3524 UriBuilder *This = URIBUILDER_THIS(iface);
3525 LONG ref = InterlockedDecrement(&This->ref);
3527 TRACE("(%p) ref=%d\n", This, ref);
3535 static HRESULT WINAPI UriBuilder_CreateUriSimple(IUriBuilder *iface,
3536 DWORD dwAllowEncodingPropertyMask,
3537 DWORD_PTR dwReserved,
3540 UriBuilder *This = URIBUILDER_THIS(iface);
3541 FIXME("(%p)->(%d %d %p)\n", This, dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
3545 static HRESULT WINAPI UriBuilder_CreateUri(IUriBuilder *iface,
3546 DWORD dwCreateFlags,
3547 DWORD dwAllowEncodingPropertyMask,
3548 DWORD_PTR dwReserved,
3551 UriBuilder *This = URIBUILDER_THIS(iface);
3552 FIXME("(%p)->(0x%08x %d %d %p)\n", This, dwCreateFlags, dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
3556 static HRESULT WINAPI UriBuilder_CreateUriWithFlags(IUriBuilder *iface,
3557 DWORD dwCreateFlags,
3558 DWORD dwUriBuilderFlags,
3559 DWORD dwAllowEncodingPropertyMask,
3560 DWORD_PTR dwReserved,
3563 UriBuilder *This = URIBUILDER_THIS(iface);
3564 FIXME("(%p)->(0x%08x 0x%08x %d %d %p)\n", This, dwCreateFlags, dwUriBuilderFlags,
3565 dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
3569 static HRESULT WINAPI UriBuilder_GetIUri(IUriBuilder *iface, IUri **ppIUri)
3571 UriBuilder *This = URIBUILDER_THIS(iface);
3572 FIXME("(%p)->(%p)\n", This, ppIUri);
3576 static HRESULT WINAPI UriBuilder_SetIUri(IUriBuilder *iface, IUri *pIUri)
3578 UriBuilder *This = URIBUILDER_THIS(iface);
3579 FIXME("(%p)->(%p)\n", This, pIUri);
3583 static HRESULT WINAPI UriBuilder_GetFragment(IUriBuilder *iface, DWORD *pcchFragment, LPCWSTR *ppwzFragment)
3585 UriBuilder *This = URIBUILDER_THIS(iface);
3586 FIXME("(%p)->(%p %p)\n", This, pcchFragment, ppwzFragment);
3590 static HRESULT WINAPI UriBuilder_GetHost(IUriBuilder *iface, DWORD *pcchHost, LPCWSTR *ppwzHost)
3592 UriBuilder *This = URIBUILDER_THIS(iface);
3593 FIXME("(%p)->(%p %p)\n", This, pcchHost, ppwzHost);
3597 static HRESULT WINAPI UriBuilder_GetPassword(IUriBuilder *iface, DWORD *pcchPassword, LPCWSTR *ppwzPassword)
3599 UriBuilder *This = URIBUILDER_THIS(iface);
3600 FIXME("(%p)->(%p %p)\n", This, pcchPassword, ppwzPassword);
3604 static HRESULT WINAPI UriBuilder_GetPath(IUriBuilder *iface, DWORD *pcchPath, LPCWSTR *ppwzPath)
3606 UriBuilder *This = URIBUILDER_THIS(iface);
3607 FIXME("(%p)->(%p %p)\n", This, pcchPath, ppwzPath);
3611 static HRESULT WINAPI UriBuilder_GetPort(IUriBuilder *iface, BOOL *pfHasPort, DWORD *pdwPort)
3613 UriBuilder *This = URIBUILDER_THIS(iface);
3614 FIXME("(%p)->(%p %p)\n", This, pfHasPort, pdwPort);
3618 static HRESULT WINAPI UriBuilder_GetQuery(IUriBuilder *iface, DWORD *pcchQuery, LPCWSTR *ppwzQuery)
3620 UriBuilder *This = URIBUILDER_THIS(iface);
3621 FIXME("(%p)->(%p %p)\n", This, pcchQuery, ppwzQuery);
3625 static HRESULT WINAPI UriBuilder_GetSchemeName(IUriBuilder *iface, DWORD *pcchSchemeName, LPCWSTR *ppwzSchemeName)
3627 UriBuilder *This = URIBUILDER_THIS(iface);
3628 FIXME("(%p)->(%p %p)\n", This, pcchSchemeName, ppwzSchemeName);
3632 static HRESULT WINAPI UriBuilder_GetUserName(IUriBuilder *iface, DWORD *pcchUserName, LPCWSTR *ppwzUserName)
3634 UriBuilder *This = URIBUILDER_THIS(iface);
3635 FIXME("(%p)->(%p %p)\n", This, pcchUserName, ppwzUserName);
3639 static HRESULT WINAPI UriBuilder_SetFragment(IUriBuilder *iface, LPCWSTR pwzNewValue)
3641 UriBuilder *This = URIBUILDER_THIS(iface);
3642 FIXME("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
3646 static HRESULT WINAPI UriBuilder_SetHost(IUriBuilder *iface, LPCWSTR pwzNewValue)
3648 UriBuilder *This = URIBUILDER_THIS(iface);
3649 FIXME("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
3653 static HRESULT WINAPI UriBuilder_SetPassword(IUriBuilder *iface, LPCWSTR pwzNewValue)
3655 UriBuilder *This = URIBUILDER_THIS(iface);
3656 FIXME("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
3660 static HRESULT WINAPI UriBuilder_SetPath(IUriBuilder *iface, LPCWSTR pwzNewValue)
3662 UriBuilder *This = URIBUILDER_THIS(iface);
3663 FIXME("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
3667 static HRESULT WINAPI UriBuilder_SetPort(IUriBuilder *iface, BOOL fHasPort, DWORD dwNewValue)
3669 UriBuilder *This = URIBUILDER_THIS(iface);
3670 FIXME("(%p)->(%d %d)\n", This, fHasPort, dwNewValue);
3674 static HRESULT WINAPI UriBuilder_SetQuery(IUriBuilder *iface, LPCWSTR pwzNewValue)
3676 UriBuilder *This = URIBUILDER_THIS(iface);
3677 FIXME("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
3681 static HRESULT WINAPI UriBuilder_SetSchemeName(IUriBuilder *iface, LPCWSTR pwzNewValue)
3683 UriBuilder *This = URIBUILDER_THIS(iface);
3684 FIXME("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
3688 static HRESULT WINAPI UriBuilder_SetUserName(IUriBuilder *iface, LPCWSTR pwzNewValue)
3690 UriBuilder *This = URIBUILDER_THIS(iface);
3691 FIXME("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
3695 static HRESULT WINAPI UriBuilder_RemoveProperties(IUriBuilder *iface, DWORD dwPropertyMask)
3697 UriBuilder *This = URIBUILDER_THIS(iface);
3698 FIXME("(%p)->(0x%08x)\n", This, dwPropertyMask);
3702 static HRESULT WINAPI UriBuilder_HasBeenModified(IUriBuilder *iface, BOOL *pfModified)
3704 UriBuilder *This = URIBUILDER_THIS(iface);
3705 FIXME("(%p)->(%p)\n", This, pfModified);
3709 #undef URIBUILDER_THIS
3711 static const IUriBuilderVtbl UriBuilderVtbl = {
3712 UriBuilder_QueryInterface,
3715 UriBuilder_CreateUriSimple,
3716 UriBuilder_CreateUri,
3717 UriBuilder_CreateUriWithFlags,
3720 UriBuilder_GetFragment,
3722 UriBuilder_GetPassword,
3725 UriBuilder_GetQuery,
3726 UriBuilder_GetSchemeName,
3727 UriBuilder_GetUserName,
3728 UriBuilder_SetFragment,
3730 UriBuilder_SetPassword,
3733 UriBuilder_SetQuery,
3734 UriBuilder_SetSchemeName,
3735 UriBuilder_SetUserName,
3736 UriBuilder_RemoveProperties,
3737 UriBuilder_HasBeenModified,
3740 /***********************************************************************
3741 * CreateIUriBuilder (urlmon.@)
3743 HRESULT WINAPI CreateIUriBuilder(IUri *pIUri, DWORD dwFlags, DWORD_PTR dwReserved, IUriBuilder **ppIUriBuilder)
3747 TRACE("(%p %x %x %p)\n", pIUri, dwFlags, (DWORD)dwReserved, ppIUriBuilder);
3749 ret = heap_alloc(sizeof(UriBuilder));
3751 return E_OUTOFMEMORY;
3753 ret->lpIUriBuilderVtbl = &UriBuilderVtbl;
3756 *ppIUriBuilder = URIBUILDER(ret);