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;
68 const IUriBuilderVtbl *lpIUriBuilderVtbl;
78 /* IPv6 addresses can hold up to 8 h16 components. */
82 /* An IPv6 can have 1 elision ("::"). */
85 /* An IPv6 can contain 1 IPv4 address as the last 32bits of the address. */
98 BOOL has_implicit_scheme;
104 URL_SCHEME scheme_type;
106 const WCHAR *userinfo;
112 Uri_HOST_TYPE host_type;
115 ipv6_address ipv6_address;
128 static const CHAR hexDigits[] = "0123456789ABCDEF";
130 /* List of scheme types/scheme names that are recognized by the IUri interface as of IE 7. */
131 static const struct {
133 WCHAR scheme_name[16];
134 } recognized_schemes[] = {
135 {URL_SCHEME_FTP, {'f','t','p',0}},
136 {URL_SCHEME_HTTP, {'h','t','t','p',0}},
137 {URL_SCHEME_GOPHER, {'g','o','p','h','e','r',0}},
138 {URL_SCHEME_MAILTO, {'m','a','i','l','t','o',0}},
139 {URL_SCHEME_NEWS, {'n','e','w','s',0}},
140 {URL_SCHEME_NNTP, {'n','n','t','p',0}},
141 {URL_SCHEME_TELNET, {'t','e','l','n','e','t',0}},
142 {URL_SCHEME_WAIS, {'w','a','i','s',0}},
143 {URL_SCHEME_FILE, {'f','i','l','e',0}},
144 {URL_SCHEME_MK, {'m','k',0}},
145 {URL_SCHEME_HTTPS, {'h','t','t','p','s',0}},
146 {URL_SCHEME_SHELL, {'s','h','e','l','l',0}},
147 {URL_SCHEME_SNEWS, {'s','n','e','w','s',0}},
148 {URL_SCHEME_LOCAL, {'l','o','c','a','l',0}},
149 {URL_SCHEME_JAVASCRIPT, {'j','a','v','a','s','c','r','i','p','t',0}},
150 {URL_SCHEME_VBSCRIPT, {'v','b','s','c','r','i','p','t',0}},
151 {URL_SCHEME_ABOUT, {'a','b','o','u','t',0}},
152 {URL_SCHEME_RES, {'r','e','s',0}},
153 {URL_SCHEME_MSSHELLROOTED, {'m','s','-','s','h','e','l','l','-','r','o','o','t','e','d',0}},
154 {URL_SCHEME_MSSHELLIDLIST, {'m','s','-','s','h','e','l','l','-','i','d','l','i','s','t',0}},
155 {URL_SCHEME_MSHELP, {'h','c','p',0}},
156 {URL_SCHEME_WILDCARD, {'*',0}}
159 /* List of default ports Windows recognizes. */
160 static const struct {
163 } default_ports[] = {
164 {URL_SCHEME_FTP, 21},
165 {URL_SCHEME_HTTP, 80},
166 {URL_SCHEME_GOPHER, 70},
167 {URL_SCHEME_NNTP, 119},
168 {URL_SCHEME_TELNET, 23},
169 {URL_SCHEME_WAIS, 210},
170 {URL_SCHEME_HTTPS, 443},
173 /* List of 3 character top level domain names Windows seems to recognize.
174 * There might be more, but, these are the only ones I've found so far.
176 static const struct {
178 } recognized_tlds[] = {
188 static inline BOOL is_alpha(WCHAR val) {
189 return ((val >= 'a' && val <= 'z') || (val >= 'A' && val <= 'Z'));
192 static inline BOOL is_num(WCHAR val) {
193 return (val >= '0' && val <= '9');
196 /* A URI is implicitly a file path if it begins with
197 * a drive letter (eg X:) or starts with "\\" (UNC path).
199 static inline BOOL is_implicit_file_path(const WCHAR *str) {
200 if(is_alpha(str[0]) && str[1] == ':')
202 else if(str[0] == '\\' && str[1] == '\\')
208 /* Checks if the URI is a hierarchical URI. A hierarchical
209 * URI is one that has "//" after the scheme.
211 static BOOL check_hierarchical(const WCHAR **ptr) {
212 const WCHAR *start = *ptr;
227 /* unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~" */
228 static inline BOOL is_unreserved(WCHAR val) {
229 return (is_alpha(val) || is_num(val) || val == '-' || val == '.' ||
230 val == '_' || val == '~');
233 /* sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
234 * / "*" / "+" / "," / ";" / "="
236 static inline BOOL is_subdelim(WCHAR val) {
237 return (val == '!' || val == '$' || val == '&' ||
238 val == '\'' || val == '(' || val == ')' ||
239 val == '*' || val == '+' || val == ',' ||
240 val == ';' || val == '=');
243 /* gen-delims = ":" / "/" / "?" / "#" / "[" / "]" / "@" */
244 static inline BOOL is_gendelim(WCHAR val) {
245 return (val == ':' || val == '/' || val == '?' ||
246 val == '#' || val == '[' || val == ']' ||
250 /* Characters that delimit the end of the authority
251 * section of a URI. Sometimes a '\\' is considered
252 * an authority delimeter.
254 static inline BOOL is_auth_delim(WCHAR val, BOOL acceptSlash) {
255 return (val == '#' || val == '/' || val == '?' ||
256 val == '\0' || (acceptSlash && val == '\\'));
259 /* reserved = gen-delims / sub-delims */
260 static inline BOOL is_reserved(WCHAR val) {
261 return (is_subdelim(val) || is_gendelim(val));
264 static inline BOOL is_hexdigit(WCHAR val) {
265 return ((val >= 'a' && val <= 'f') ||
266 (val >= 'A' && val <= 'F') ||
267 (val >= '0' && val <= '9'));
270 static inline BOOL is_path_delim(WCHAR val) {
271 return (!val || val == '#' || val == '?');
274 /* Computes the size of the given IPv6 address.
275 * Each h16 component is 16bits, if there is an IPv4 address, it's
276 * 32bits. If there's an elision it can be 16bits to 128bits, depending
277 * on the number of other components.
279 * Modeled after google-url's CheckIPv6ComponentsSize function
281 static void compute_ipv6_comps_size(ipv6_address *address) {
282 address->components_size = address->h16_count * 2;
285 /* IPv4 address is 4 bytes. */
286 address->components_size += 4;
288 if(address->elision) {
289 /* An elision can be anywhere from 2 bytes up to 16 bytes.
290 * It size depends on the size of the h16 and IPv4 components.
292 address->elision_size = 16 - address->components_size;
293 if(address->elision_size < 2)
294 address->elision_size = 2;
296 address->elision_size = 0;
299 /* Taken from dlls/jscript/lex.c */
300 static int hex_to_int(WCHAR val) {
301 if(val >= '0' && val <= '9')
303 else if(val >= 'a' && val <= 'f')
304 return val - 'a' + 10;
305 else if(val >= 'A' && val <= 'F')
306 return val - 'A' + 10;
311 /* Helper function for converting a percent encoded string
312 * representation of a WCHAR value into its actual WCHAR value. If
313 * the two characters following the '%' aren't valid hex values then
314 * this function returns the NULL character.
317 * "%2E" will result in '.' being returned by this function.
319 static WCHAR decode_pct_val(const WCHAR *ptr) {
322 if(*ptr == '%' && is_hexdigit(*(ptr + 1)) && is_hexdigit(*(ptr + 2))) {
323 INT a = hex_to_int(*(ptr + 1));
324 INT b = hex_to_int(*(ptr + 2));
333 /* Helper function for percent encoding a given character
334 * and storing the encoded value into a given buffer (dest).
336 * It's up to the calling function to ensure that there is
337 * at least enough space in 'dest' for the percent encoded
338 * value to be stored (so dest + 3 spaces available).
340 static inline void pct_encode_val(WCHAR val, WCHAR *dest) {
342 dest[1] = hexDigits[(val >> 4) & 0xf];
343 dest[2] = hexDigits[val & 0xf];
346 /* Scans the range of characters [str, end] and returns the last occurence
347 * of 'ch' or returns NULL.
349 static const WCHAR *str_last_of(const WCHAR *str, const WCHAR *end, WCHAR ch) {
350 const WCHAR *ptr = end;
361 /* Attempts to parse the domain name from the host.
363 * This function also includes the Top-level Domain (TLD) name
364 * of the host when it tries to find the domain name. If it finds
365 * a valid domain name it will assign 'domain_start' the offset
366 * into 'host' where the domain name starts.
368 * It's implied that if a domain name its range is implied to be
369 * [host+domain_start, host+host_len).
371 static void find_domain_name(const WCHAR *host, DWORD host_len,
373 const WCHAR *last_tld, *sec_last_tld, *end;
375 end = host+host_len-1;
379 /* There has to be at least enough room for a '.' followed by a
380 * 3 character TLD for a domain to even exist in the host name.
385 last_tld = str_last_of(host, end, '.');
387 /* http://hostname -> has no domain name. */
390 sec_last_tld = str_last_of(host, last_tld-1, '.');
392 /* If the '.' is at the beginning of the host there
393 * has to be at least 3 characters in the TLD for it
395 * Ex: .com -> .com as the domain name.
396 * .co -> has no domain name.
398 if(last_tld-host == 0) {
399 if(end-(last_tld-1) < 3)
401 } else if(last_tld-host == 3) {
404 /* If there's three characters in front of last_tld and
405 * they are on the list of recognized TLDs, then this
406 * host doesn't have a domain (since the host only contains
408 * Ex: edu.uk -> has no domain name.
409 * foo.uk -> foo.uk as the domain name.
411 for(i = 0; i < sizeof(recognized_tlds)/sizeof(recognized_tlds[0]); ++i) {
412 if(!StrCmpNIW(host, recognized_tlds[i].tld_name, 3))
415 } else if(last_tld-host < 3)
416 /* Anything less then 3 characters is considered part
418 * Ex: ak.uk -> Has no domain name.
422 /* Otherwise the domain name is the whole host name. */
424 } else if(end+1-last_tld > 3) {
425 /* If the last_tld has more then 3 characters then it's automatically
426 * considered the TLD of the domain name.
427 * Ex: www.winehq.org.uk.test -> uk.test as the domain name.
429 *domain_start = (sec_last_tld+1)-host;
430 } else if(last_tld - (sec_last_tld+1) < 4) {
432 /* If the sec_last_tld is 3 characters long it HAS to be on the list of
433 * recognized to still be considered part of the TLD name, otherwise
434 * its considered the domain name.
435 * Ex: www.google.com.uk -> google.com.uk as the domain name.
436 * www.google.foo.uk -> foo.uk as the domain name.
438 if(last_tld - (sec_last_tld+1) == 3) {
439 for(i = 0; i < sizeof(recognized_tlds)/sizeof(recognized_tlds[0]); ++i) {
440 if(!StrCmpNIW(sec_last_tld+1, recognized_tlds[i].tld_name, 3)) {
441 const WCHAR *domain = str_last_of(host, sec_last_tld-1, '.');
446 *domain_start = (domain+1) - host;
447 TRACE("Found domain name %s\n", debugstr_wn(host+*domain_start,
448 (host+host_len)-(host+*domain_start)));
453 *domain_start = (sec_last_tld+1)-host;
455 /* Since the sec_last_tld is less then 3 characters it's considered
457 * Ex: www.google.fo.uk -> google.fo.uk as the domain name.
459 const WCHAR *domain = str_last_of(host, sec_last_tld-1, '.');
464 *domain_start = (domain+1) - host;
467 /* The second to last TLD has more then 3 characters making it
469 * Ex: www.google.test.us -> test.us as the domain name.
471 *domain_start = (sec_last_tld+1)-host;
474 TRACE("Found domain name %s\n", debugstr_wn(host+*domain_start,
475 (host+host_len)-(host+*domain_start)));
478 /* Removes the dot segments from a heirarchical URIs path component. This
479 * function performs the removal in place.
481 * This is a modified version of Qt's QUrl function "removeDotsFromPath".
483 * This function returns the new length of the path string.
485 static DWORD remove_dot_segments(WCHAR *path, DWORD path_len) {
487 const WCHAR *in = out;
488 const WCHAR *end = out + path_len;
492 /* A. if the input buffer begins with a prefix of "/./" or "/.",
493 * where "." is a complete path segment, then replace that
494 * prefix with "/" in the input buffer; otherwise,
496 if(in <= end - 3 && in[0] == '/' && in[1] == '.' && in[2] == '/') {
499 } else if(in == end - 2 && in[0] == '/' && in[1] == '.') {
505 /* B. if the input buffer begins with a prefix of "/../" or "/..",
506 * where ".." is a complete path segment, then replace that
507 * prefix with "/" in the input buffer and remove the last
508 * segment and its preceding "/" (if any) from the output
511 if(in <= end - 4 && in[0] == '/' && in[1] == '.' && in[2] == '.' && in[3] == '/') {
512 while(out > path && *(--out) != '/');
516 } else if(in == end - 3 && in[0] == '/' && in[1] == '.' && in[2] == '.') {
517 while(out > path && *(--out) != '/');
526 /* C. move the first path segment in the input buffer to the end of
527 * the output buffer, including the initial "/" character (if
528 * any) and any subsequent characters up to, but not including,
529 * the next "/" character or the end of the input buffer.
532 while(in < end && *in != '/')
537 TRACE("(%p %d): Path after dot segments removed %s len=%d\n", path, path_len,
538 debugstr_wn(path, len), len);
542 /* Attempts to find the file extension in a given path. */
543 static INT find_file_extension(const WCHAR *path, DWORD path_len) {
546 for(end = path+path_len-1; end >= path && *end != '/' && *end != '\\'; --end) {
554 /* Computes the location where the elision should occur in the IPv6
555 * address using the numerical values of each component stored in
556 * 'values'. If the address shouldn't contain an elision then 'index'
557 * is assigned -1 as it's value. Otherwise 'index' will contain the
558 * starting index (into values) where the elision should be, and 'count'
559 * will contain the number of cells the elision covers.
562 * Windows will expand an elision if the elision only represents 1 h16
563 * component of the URI.
565 * Ex: [1::2:3:4:5:6:7] -> [1:0:2:3:4:5:6:7]
567 * If the IPv6 address contains an IPv4 address, the IPv4 address is also
568 * considered for being included as part of an elision if all it's components
571 * Ex: [1:2:3:4:5:6:0.0.0.0] -> [1:2:3:4:5:6::]
573 static void compute_elision_location(const ipv6_address *address, const USHORT values[8],
574 INT *index, DWORD *count) {
575 DWORD i, max_len, cur_len;
576 INT max_index, cur_index;
578 max_len = cur_len = 0;
579 max_index = cur_index = -1;
580 for(i = 0; i < 8; ++i) {
581 BOOL check_ipv4 = (address->ipv4 && i == 6);
582 BOOL is_end = (check_ipv4 || i == 7);
585 /* Check if the IPv4 address contains only zeros. */
586 if(values[i] == 0 && values[i+1] == 0) {
593 } else if(values[i] == 0) {
600 if(is_end || values[i] != 0) {
601 /* We only consider it for an elision if it's
602 * more then 1 component long.
604 if(cur_len > 1 && cur_len > max_len) {
605 /* Found the new elision location. */
607 max_index = cur_index;
610 /* Reset the current range for the next range of zeros. */
620 /* Converts the specified IPv4 address into an uint value.
622 * This function assumes that the IPv4 address has already been validated.
624 static UINT ipv4toui(const WCHAR *ip, DWORD len) {
626 DWORD comp_value = 0;
629 for(ptr = ip; ptr < ip+len; ++ptr) {
635 comp_value = comp_value*10 + (*ptr-'0');
644 /* Converts an IPv4 address in numerical form into it's fully qualified
645 * string form. This function returns the number of characters written
646 * to 'dest'. If 'dest' is NULL this function will return the number of
647 * characters that would have been written.
649 * It's up to the caller to ensure there's enough space in 'dest' for the
652 static DWORD ui2ipv4(WCHAR *dest, UINT address) {
653 static const WCHAR formatW[] =
654 {'%','u','.','%','u','.','%','u','.','%','u',0};
658 digits[0] = (address >> 24) & 0xff;
659 digits[1] = (address >> 16) & 0xff;
660 digits[2] = (address >> 8) & 0xff;
661 digits[3] = address & 0xff;
665 ret = sprintfW(tmp, formatW, digits[0], digits[1], digits[2], digits[3]);
667 ret = sprintfW(dest, formatW, digits[0], digits[1], digits[2], digits[3]);
672 /* Converts an h16 component (from an IPv6 address) into it's
675 * This function assumes that the h16 component has already been validated.
677 static USHORT h16tous(h16 component) {
681 for(i = 0; i < component.len; ++i) {
683 ret += hex_to_int(component.str[i]);
689 /* Converts an IPv6 address into it's 128 bits (16 bytes) numerical value.
691 * This function assumes that the ipv6_address has already been validated.
693 static BOOL ipv6_to_number(const ipv6_address *address, USHORT number[8]) {
694 DWORD i, cur_component = 0;
695 BOOL already_passed_elision = FALSE;
697 for(i = 0; i < address->h16_count; ++i) {
698 if(address->elision) {
699 if(address->components[i].str > address->elision && !already_passed_elision) {
700 /* Means we just passed the elision and need to add it's values to
701 * 'number' before we do anything else.
704 for(j = 0; j < address->elision_size; j+=2)
705 number[cur_component++] = 0;
707 already_passed_elision = TRUE;
711 number[cur_component++] = h16tous(address->components[i]);
714 /* Case when the elision appears after the h16 components. */
715 if(!already_passed_elision && address->elision) {
716 for(i = 0; i < address->elision_size; i+=2)
717 number[cur_component++] = 0;
718 already_passed_elision = TRUE;
722 UINT value = ipv4toui(address->ipv4, address->ipv4_len);
724 if(cur_component != 6) {
725 ERR("(%p %p): Failed sanity check with %d\n", address, number, cur_component);
729 number[cur_component++] = (value >> 16) & 0xffff;
730 number[cur_component] = value & 0xffff;
736 /* Checks if the characters pointed to by 'ptr' are
737 * a percent encoded data octet.
739 * pct-encoded = "%" HEXDIG HEXDIG
741 static BOOL check_pct_encoded(const WCHAR **ptr) {
742 const WCHAR *start = *ptr;
748 if(!is_hexdigit(**ptr)) {
754 if(!is_hexdigit(**ptr)) {
763 /* dec-octet = DIGIT ; 0-9
764 * / %x31-39 DIGIT ; 10-99
765 * / "1" 2DIGIT ; 100-199
766 * / "2" %x30-34 DIGIT ; 200-249
767 * / "25" %x30-35 ; 250-255
769 static BOOL check_dec_octet(const WCHAR **ptr) {
770 const WCHAR *c1, *c2, *c3;
773 /* A dec-octet must be at least 1 digit long. */
774 if(*c1 < '0' || *c1 > '9')
780 /* Since the 1 digit requirment was meet, it doesn't
781 * matter if this is a DIGIT value, it's considered a
784 if(*c2 < '0' || *c2 > '9')
790 /* Same explanation as above. */
791 if(*c3 < '0' || *c3 > '9')
794 /* Anything > 255 isn't a valid IP dec-octet. */
795 if(*c1 >= '2' && *c2 >= '5' && *c3 >= '5') {
804 /* Checks if there is an implicit IPv4 address in the host component of the URI.
805 * The max value of an implicit IPv4 address is UINT_MAX.
808 * "234567" would be considered an implicit IPv4 address.
810 static BOOL check_implicit_ipv4(const WCHAR **ptr, UINT *val) {
811 const WCHAR *start = *ptr;
815 while(is_num(**ptr)) {
816 ret = ret*10 + (**ptr - '0');
832 /* Checks if the string contains an IPv4 address.
834 * This function has a strict mode or a non-strict mode of operation
835 * When 'strict' is set to FALSE this function will return TRUE if
836 * the string contains at least 'dec-octet "." dec-octet' since partial
837 * IPv4 addresses will be normalized out into full IPv4 addresses. When
838 * 'strict' is set this function expects there to be a full IPv4 address.
840 * IPv4address = dec-octet "." dec-octet "." dec-octet "." dec-octet
842 static BOOL check_ipv4address(const WCHAR **ptr, BOOL strict) {
843 const WCHAR *start = *ptr;
845 if(!check_dec_octet(ptr)) {
856 if(!check_dec_octet(ptr)) {
870 if(!check_dec_octet(ptr)) {
884 if(!check_dec_octet(ptr)) {
889 /* Found a four digit ip address. */
892 /* Tries to parse the scheme name of the URI.
894 * scheme = ALPHA *(ALPHA | NUM | '+' | '-' | '.') as defined by RFC 3896.
895 * NOTE: Windows accepts a number as the first character of a scheme.
897 static BOOL parse_scheme_name(const WCHAR **ptr, parse_data *data) {
898 const WCHAR *start = *ptr;
901 data->scheme_len = 0;
904 if(**ptr == '*' && *ptr == start) {
905 /* Might have found a wildcard scheme. If it is the next
906 * char has to be a ':' for it to be a valid URI
910 } else if(!is_num(**ptr) && !is_alpha(**ptr) && **ptr != '+' &&
911 **ptr != '-' && **ptr != '.')
920 /* Schemes must end with a ':' */
926 data->scheme = start;
927 data->scheme_len = *ptr - start;
933 /* Tries to deduce the corresponding URL_SCHEME for the given URI. Stores
934 * the deduced URL_SCHEME in data->scheme_type.
936 static BOOL parse_scheme_type(parse_data *data) {
937 /* If there's scheme data then see if it's a recognized scheme. */
938 if(data->scheme && data->scheme_len) {
941 for(i = 0; i < sizeof(recognized_schemes)/sizeof(recognized_schemes[0]); ++i) {
942 if(lstrlenW(recognized_schemes[i].scheme_name) == data->scheme_len) {
943 /* Has to be a case insensitive compare. */
944 if(!StrCmpNIW(recognized_schemes[i].scheme_name, data->scheme, data->scheme_len)) {
945 data->scheme_type = recognized_schemes[i].scheme;
951 /* If we get here it means it's not a recognized scheme. */
952 data->scheme_type = URL_SCHEME_UNKNOWN;
954 } else if(data->is_relative) {
955 /* Relative URI's have no scheme. */
956 data->scheme_type = URL_SCHEME_UNKNOWN;
959 /* Should never reach here! what happened... */
960 FIXME("(%p): Unable to determine scheme type for URI %s\n", data, debugstr_w(data->uri));
965 /* Tries to parse (or deduce) the scheme_name of a URI. If it can't
966 * parse a scheme from the URI it will try to deduce the scheme_name and scheme_type
967 * using the flags specified in 'flags' (if any). Flags that affect how this function
968 * operates are the Uri_CREATE_ALLOW_* flags.
970 * All parsed/deduced information will be stored in 'data' when the function returns.
972 * Returns TRUE if it was able to successfully parse the information.
974 static BOOL parse_scheme(const WCHAR **ptr, parse_data *data, DWORD flags) {
975 static const WCHAR fileW[] = {'f','i','l','e',0};
976 static const WCHAR wildcardW[] = {'*',0};
978 /* First check to see if the uri could implicitly be a file path. */
979 if(is_implicit_file_path(*ptr)) {
980 if(flags & Uri_CREATE_ALLOW_IMPLICIT_FILE_SCHEME) {
981 data->scheme = fileW;
982 data->scheme_len = lstrlenW(fileW);
983 data->has_implicit_scheme = TRUE;
985 TRACE("(%p %p %x): URI is an implicit file path.\n", ptr, data, flags);
987 /* Window's does not consider anything that can implicitly be a file
988 * path to be a valid URI if the ALLOW_IMPLICIT_FILE_SCHEME flag is not set...
990 TRACE("(%p %p %x): URI is implicitly a file path, but, the ALLOW_IMPLICIT_FILE_SCHEME flag wasn't set.\n",
994 } else if(!parse_scheme_name(ptr, data)) {
995 /* No Scheme was found, this means it could be:
996 * a) an implicit Wildcard scheme
1000 if(flags & Uri_CREATE_ALLOW_IMPLICIT_WILDCARD_SCHEME) {
1001 data->scheme = wildcardW;
1002 data->scheme_len = lstrlenW(wildcardW);
1003 data->has_implicit_scheme = TRUE;
1005 TRACE("(%p %p %x): URI is an implicit wildcard scheme.\n", ptr, data, flags);
1006 } else if (flags & Uri_CREATE_ALLOW_RELATIVE) {
1007 data->is_relative = TRUE;
1008 TRACE("(%p %p %x): URI is relative.\n", ptr, data, flags);
1010 TRACE("(%p %p %x): Malformed URI found. Unable to deduce scheme name.\n", ptr, data, flags);
1015 if(!data->is_relative)
1016 TRACE("(%p %p %x): Found scheme=%s scheme_len=%d\n", ptr, data, flags,
1017 debugstr_wn(data->scheme, data->scheme_len), data->scheme_len);
1019 if(!parse_scheme_type(data))
1022 TRACE("(%p %p %x): Assigned %d as the URL_SCHEME.\n", ptr, data, flags, data->scheme_type);
1026 /* Parses the userinfo part of the URI (if it exists). The userinfo field of
1027 * a URI can consist of "username:password@", or just "username@".
1030 * userinfo = *( unreserved / pct-encoded / sub-delims / ":" )
1033 * 1) If there is more than one ':' in the userinfo part of the URI Windows
1034 * uses the first occurence of ':' to delimit the username and password
1038 * ftp://user:pass:word@winehq.org
1040 * Would yield, "user" as the username and "pass:word" as the password.
1042 * 2) Windows allows any character to appear in the "userinfo" part of
1043 * a URI, as long as it's not an authority delimeter character set.
1045 static void parse_userinfo(const WCHAR **ptr, parse_data *data, DWORD flags) {
1046 data->userinfo = *ptr;
1047 data->userinfo_split = -1;
1049 while(**ptr != '@') {
1050 if(**ptr == ':' && data->userinfo_split == -1)
1051 data->userinfo_split = *ptr - data->userinfo;
1052 else if(**ptr == '%') {
1053 /* If it's a known scheme type, it has to be a valid percent
1056 if(!check_pct_encoded(ptr)) {
1057 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
1058 *ptr = data->userinfo;
1059 data->userinfo = NULL;
1060 data->userinfo_split = -1;
1062 TRACE("(%p %p %x): URI contained no userinfo.\n", ptr, data, flags);
1067 } else if(is_auth_delim(**ptr, data->scheme_type != URL_SCHEME_UNKNOWN))
1074 *ptr = data->userinfo;
1075 data->userinfo = NULL;
1076 data->userinfo_split = -1;
1078 TRACE("(%p %p %x): URI contained no userinfo.\n", ptr, data, flags);
1082 data->userinfo_len = *ptr - data->userinfo;
1083 TRACE("(%p %p %x): Found userinfo=%s userinfo_len=%d split=%d.\n", ptr, data, flags,
1084 debugstr_wn(data->userinfo, data->userinfo_len), data->userinfo_len, data->userinfo_split);
1088 /* Attempts to parse a port from the URI.
1091 * Windows seems to have a cap on what the maximum value
1092 * for a port can be. The max value is USHORT_MAX.
1096 static BOOL parse_port(const WCHAR **ptr, parse_data *data, DWORD flags) {
1100 while(!is_auth_delim(**ptr, data->scheme_type != URL_SCHEME_UNKNOWN)) {
1101 if(!is_num(**ptr)) {
1107 port = port*10 + (**ptr-'0');
1109 if(port > USHORT_MAX) {
1118 data->port_value = port;
1119 data->port_len = *ptr - data->port;
1121 TRACE("(%p %p %x): Found port %s len=%d value=%u\n", ptr, data, flags,
1122 debugstr_wn(data->port, data->port_len), data->port_len, data->port_value);
1126 /* Attempts to parse a IPv4 address from the URI.
1129 * Window's normalizes IPv4 addresses, This means there's three
1130 * possibilities for the URI to contain an IPv4 address.
1131 * 1) A well formed address (ex. 192.2.2.2).
1132 * 2) A partially formed address. For example "192.0" would
1133 * normalize to "192.0.0.0" during canonicalization.
1134 * 3) An implicit IPv4 address. For example "256" would
1135 * normalize to "0.0.1.0" during canonicalization. Also
1136 * note that the maximum value for an implicit IP address
1137 * is UINT_MAX, if the value in the URI exceeds this then
1138 * it is not considered an IPv4 address.
1140 static BOOL parse_ipv4address(const WCHAR **ptr, parse_data *data, DWORD flags) {
1141 const BOOL is_unknown = data->scheme_type == URL_SCHEME_UNKNOWN;
1144 if(!check_ipv4address(ptr, FALSE)) {
1145 if(!check_implicit_ipv4(ptr, &data->implicit_ipv4)) {
1146 TRACE("(%p %p %x): URI didn't contain anything looking like an IPv4 address.\n",
1152 data->has_implicit_ip = TRUE;
1155 /* Check if what we found is the only part of the host name (if it isn't
1156 * we don't have an IPv4 address).
1160 if(!parse_port(ptr, data, flags)) {
1165 } else if(!is_auth_delim(**ptr, !is_unknown)) {
1166 /* Found more data which belongs the host, so this isn't an IPv4. */
1169 data->has_implicit_ip = FALSE;
1173 data->host_len = *ptr - data->host;
1174 data->host_type = Uri_HOST_IPV4;
1176 TRACE("(%p %p %x): IPv4 address found. host=%s host_len=%d host_type=%d\n",
1177 ptr, data, flags, debugstr_wn(data->host, data->host_len),
1178 data->host_len, data->host_type);
1182 /* Attempts to parse the reg-name from the URI.
1184 * Because of the way Windows handles ':' this function also
1185 * handles parsing the port.
1187 * reg-name = *( unreserved / pct-encoded / sub-delims )
1190 * Windows allows everything, but, the characters in "auth_delims" and ':'
1191 * to appear in a reg-name, unless it's an unknown scheme type then ':' is
1192 * allowed to appear (even if a valid port isn't after it).
1194 * Windows doesn't like host names which start with '[' and end with ']'
1195 * and don't contain a valid IP literal address in between them.
1197 * On Windows if an '[' is encountered in the host name the ':' no longer
1198 * counts as a delimiter until you reach the next ']' or an "authority delimeter".
1200 * A reg-name CAN be empty.
1202 static BOOL parse_reg_name(const WCHAR **ptr, parse_data *data, DWORD flags) {
1203 const BOOL has_start_bracket = **ptr == '[';
1204 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
1205 BOOL inside_brackets = has_start_bracket;
1206 BOOL ignore_col = FALSE;
1208 /* We have to be careful with file schemes. */
1209 if(data->scheme_type == URL_SCHEME_FILE) {
1210 /* This is because an implicit file scheme could be "C:\\test" and it
1211 * would trick this function into thinking the host is "C", when after
1212 * canonicalization the host would end up being an empty string.
1214 if(is_alpha(**ptr) && *(*ptr+1) == ':') {
1215 /* Regular old drive paths don't have a host type (or host name). */
1216 data->host_type = Uri_HOST_UNKNOWN;
1220 } else if(**ptr == '\\' && *(*ptr+1) == '\\')
1221 /* Skip past the "\\" of a UNC path. */
1227 while(!is_auth_delim(**ptr, known_scheme)) {
1228 if(**ptr == ':' && !ignore_col) {
1229 /* We can ignore ':' if were inside brackets.*/
1230 if(!inside_brackets) {
1231 const WCHAR *tmp = (*ptr)++;
1233 /* Attempt to parse the port. */
1234 if(!parse_port(ptr, data, flags)) {
1235 /* Windows expects there to be a valid port for known scheme types. */
1236 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
1239 TRACE("(%p %p %x): Expected valid port\n", ptr, data, flags);
1242 /* Windows gives up on trying to parse a port when it
1243 * encounters 1 invalid port.
1247 data->host_len = tmp - data->host;
1251 } else if(**ptr == '%' && known_scheme) {
1252 /* Has to be a legit % encoded value. */
1253 if(!check_pct_encoded(ptr)) {
1259 } else if(**ptr == ']')
1260 inside_brackets = FALSE;
1261 else if(**ptr == '[')
1262 inside_brackets = TRUE;
1267 if(has_start_bracket) {
1268 /* Make sure the last character of the host wasn't a ']'. */
1269 if(*(*ptr-1) == ']') {
1270 TRACE("(%p %p %x): Expected an IP literal inside of the host\n",
1278 /* Don't overwrite our length if we found a port earlier. */
1280 data->host_len = *ptr - data->host;
1282 /* If the host is empty, then it's an unknown host type. */
1283 if(data->host_len == 0)
1284 data->host_type = Uri_HOST_UNKNOWN;
1286 data->host_type = Uri_HOST_DNS;
1288 TRACE("(%p %p %x): Parsed reg-name. host=%s len=%d\n", ptr, data, flags,
1289 debugstr_wn(data->host, data->host_len), data->host_len);
1293 /* Attempts to parse an IPv6 address out of the URI.
1295 * IPv6address = 6( h16 ":" ) ls32
1296 * / "::" 5( h16 ":" ) ls32
1297 * / [ h16 ] "::" 4( h16 ":" ) ls32
1298 * / [ *1( h16 ":" ) h16 ] "::" 3( h16 ":" ) ls32
1299 * / [ *2( h16 ":" ) h16 ] "::" 2( h16 ":" ) ls32
1300 * / [ *3( h16 ":" ) h16 ] "::" h16 ":" ls32
1301 * / [ *4( h16 ":" ) h16 ] "::" ls32
1302 * / [ *5( h16 ":" ) h16 ] "::" h16
1303 * / [ *6( h16 ":" ) h16 ] "::"
1305 * ls32 = ( h16 ":" h16 ) / IPv4address
1306 * ; least-significant 32 bits of address.
1309 * ; 16 bits of address represented in hexadecimal.
1311 * Modeled after google-url's 'DoParseIPv6' function.
1313 static BOOL parse_ipv6address(const WCHAR **ptr, parse_data *data, DWORD flags) {
1314 const WCHAR *start, *cur_start;
1317 start = cur_start = *ptr;
1318 memset(&ip, 0, sizeof(ipv6_address));
1321 /* Check if we're on the last character of the host. */
1322 BOOL is_end = (is_auth_delim(**ptr, data->scheme_type != URL_SCHEME_UNKNOWN)
1325 BOOL is_split = (**ptr == ':');
1326 BOOL is_elision = (is_split && !is_end && *(*ptr+1) == ':');
1328 /* Check if we're at the end of of the a component, or
1329 * if we're at the end of the IPv6 address.
1331 if(is_split || is_end) {
1334 cur_len = *ptr - cur_start;
1336 /* h16 can't have a length > 4. */
1340 TRACE("(%p %p %x): h16 component to long.\n",
1346 /* An h16 component can't have the length of 0 unless
1347 * the elision is at the beginning of the address, or
1348 * at the end of the address.
1350 if(!((*ptr == start && is_elision) ||
1351 (is_end && (*ptr-2) == ip.elision))) {
1353 TRACE("(%p %p %x): IPv6 component can not have a length of 0.\n",
1360 /* An IPv6 address can have no more than 8 h16 components. */
1361 if(ip.h16_count >= 8) {
1363 TRACE("(%p %p %x): Not a IPv6 address, to many h16 components.\n",
1368 ip.components[ip.h16_count].str = cur_start;
1369 ip.components[ip.h16_count].len = cur_len;
1371 TRACE("(%p %p %x): Found h16 component %s, len=%d, h16_count=%d\n",
1372 ptr, data, flags, debugstr_wn(cur_start, cur_len), cur_len,
1382 /* A IPv6 address can only have 1 elision ('::'). */
1386 TRACE("(%p %p %x): IPv6 address cannot have 2 elisions.\n",
1398 if(!check_ipv4address(ptr, TRUE)) {
1399 if(!is_hexdigit(**ptr)) {
1400 /* Not a valid character for an IPv6 address. */
1405 /* Found an IPv4 address. */
1406 ip.ipv4 = cur_start;
1407 ip.ipv4_len = *ptr - cur_start;
1409 TRACE("(%p %p %x): Found an attached IPv4 address %s len=%d.\n",
1410 ptr, data, flags, debugstr_wn(ip.ipv4, ip.ipv4_len),
1413 /* IPv4 addresses can only appear at the end of a IPv6. */
1419 compute_ipv6_comps_size(&ip);
1421 /* Make sure the IPv6 address adds up to 16 bytes. */
1422 if(ip.components_size + ip.elision_size != 16) {
1424 TRACE("(%p %p %x): Invalid IPv6 address, did not add up to 16 bytes.\n",
1429 if(ip.elision_size == 2) {
1430 /* For some reason on Windows if an elision that represents
1431 * only 1 h16 component is encountered at the very begin or
1432 * end of an IPv6 address, Windows does not consider it a
1433 * valid IPv6 address.
1435 * Ex: [::2:3:4:5:6:7] is not valid, even though the sum
1436 * of all the components == 128bits.
1438 if(ip.elision < ip.components[0].str ||
1439 ip.elision > ip.components[ip.h16_count-1].str) {
1441 TRACE("(%p %p %x): Invalid IPv6 address. Detected elision of 2 bytes at the beginning or end of the address.\n",
1447 data->host_type = Uri_HOST_IPV6;
1448 data->has_ipv6 = TRUE;
1449 data->ipv6_address = ip;
1451 TRACE("(%p %p %x): Found valid IPv6 literal %s len=%d\n",
1452 ptr, data, flags, debugstr_wn(start, *ptr-start),
1457 /* IPvFuture = "v" 1*HEXDIG "." 1*( unreserved / sub-delims / ":" ) */
1458 static BOOL parse_ipvfuture(const WCHAR **ptr, parse_data *data, DWORD flags) {
1459 const WCHAR *start = *ptr;
1461 /* IPvFuture has to start with a 'v' or 'V'. */
1462 if(**ptr != 'v' && **ptr != 'V')
1465 /* Following the v their must be atleast 1 hexdigit. */
1467 if(!is_hexdigit(**ptr)) {
1473 while(is_hexdigit(**ptr))
1476 /* End of the hexdigit sequence must be a '.' */
1483 if(!is_unreserved(**ptr) && !is_subdelim(**ptr) && **ptr != ':') {
1489 while(is_unreserved(**ptr) || is_subdelim(**ptr) || **ptr == ':')
1492 data->host_type = Uri_HOST_UNKNOWN;
1494 TRACE("(%p %p %x): Parsed IPvFuture address %s len=%d\n", ptr, data, flags,
1495 debugstr_wn(start, *ptr-start), *ptr-start);
1500 /* IP-literal = "[" ( IPv6address / IPvFuture ) "]" */
1501 static BOOL parse_ip_literal(const WCHAR **ptr, parse_data *data, DWORD flags) {
1510 if(!parse_ipv6address(ptr, data, flags)) {
1511 if(!parse_ipvfuture(ptr, data, flags)) {
1527 /* If a valid port is not found, then let it trickle down to
1530 if(!parse_port(ptr, data, flags)) {
1536 data->host_len = *ptr - data->host;
1541 /* Parses the host information from the URI.
1543 * host = IP-literal / IPv4address / reg-name
1545 static BOOL parse_host(const WCHAR **ptr, parse_data *data, DWORD flags) {
1546 if(!parse_ip_literal(ptr, data, flags)) {
1547 if(!parse_ipv4address(ptr, data, flags)) {
1548 if(!parse_reg_name(ptr, data, flags)) {
1549 TRACE("(%p %p %x): Malformed URI, Unknown host type.\n",
1559 /* Parses the authority information from the URI.
1561 * authority = [ userinfo "@" ] host [ ":" port ]
1563 static BOOL parse_authority(const WCHAR **ptr, parse_data *data, DWORD flags) {
1564 parse_userinfo(ptr, data, flags);
1566 /* Parsing the port will happen during one of the host parsing
1567 * routines (if the URI has a port).
1569 if(!parse_host(ptr, data, flags))
1575 /* Attempts to parse the path information of a hierarchical URI. */
1576 static BOOL parse_path_hierarchical(const WCHAR **ptr, parse_data *data, DWORD flags) {
1577 const WCHAR *start = *ptr;
1578 static const WCHAR slash[] = {'/',0};
1580 if(is_path_delim(**ptr)) {
1581 if(data->scheme_type == URL_SCHEME_WILDCARD) {
1582 /* Wildcard schemes don't get a '/' attached if their path is
1587 } else if(!(flags & Uri_CREATE_NO_CANONICALIZE)) {
1588 /* If the path component is empty, then a '/' is added. */
1593 while(!is_path_delim(**ptr)) {
1594 if(**ptr == '%' && data->scheme_type != URL_SCHEME_UNKNOWN &&
1595 data->scheme_type != URL_SCHEME_FILE) {
1596 if(!check_pct_encoded(ptr)) {
1601 } else if(**ptr == '\\') {
1602 /* Not allowed to have a backslash if NO_CANONICALIZE is set
1603 * and the scheme is known type (but not a file scheme).
1605 if(flags & Uri_CREATE_NO_CANONICALIZE) {
1606 if(data->scheme_type != URL_SCHEME_FILE &&
1607 data->scheme_type != URL_SCHEME_UNKNOWN) {
1617 /* The only time a URI doesn't have a path is when
1618 * the NO_CANONICALIZE flag is set and the raw URI
1619 * didn't contain one.
1626 data->path_len = *ptr - start;
1631 TRACE("(%p %p %x): Parsed path %s len=%d\n", ptr, data, flags,
1632 debugstr_wn(data->path, data->path_len), data->path_len);
1634 TRACE("(%p %p %x): The URI contained no path\n", ptr, data, flags);
1639 /* Parses the path of a opaque URI (much less strict then the parser
1640 * for a hierarchical URI).
1643 * Windows allows invalid % encoded data to appear in opaque URI paths
1644 * for unknown scheme types.
1646 static BOOL parse_path_opaque(const WCHAR **ptr, parse_data *data, DWORD flags) {
1647 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
1651 while(!is_path_delim(**ptr)) {
1652 if(**ptr == '%' && known_scheme) {
1653 if(!check_pct_encoded(ptr)) {
1664 data->path_len = *ptr - data->path;
1665 TRACE("(%p %p %x): Parsed opaque URI path %s len=%d\n", ptr, data, flags,
1666 debugstr_wn(data->path, data->path_len), data->path_len);
1670 /* Determines how the URI should be parsed after the scheme information.
1672 * If the scheme is followed, by "//" then, it is treated as an hierarchical URI
1673 * which then the authority and path information will be parsed out. Otherwise, the
1674 * URI will be treated as an opaque URI which the authority information is not parsed
1677 * RFC 3896 definition of hier-part:
1679 * hier-part = "//" authority path-abempty
1684 * MSDN opaque URI definition:
1685 * scheme ":" path [ "#" fragment ]
1688 * If the URI is of an unknown scheme type and has a "//" following the scheme then it
1689 * is treated as a hierarchical URI, but, if the CREATE_NO_CRACK_UNKNOWN_SCHEMES flag is
1690 * set then it is considered an opaque URI reguardless of what follows the scheme information
1691 * (per MSDN documentation).
1693 static BOOL parse_hierpart(const WCHAR **ptr, parse_data *data, DWORD flags) {
1694 const WCHAR *start = *ptr;
1696 /* Checks if the authority information needs to be parsed.
1698 * Relative URI's aren't hierarchical URI's, but, they could trick
1699 * "check_hierarchical" into thinking it is, so we need to explicitly
1700 * make sure it's not relative. Also, if the URI is an implicit file
1701 * scheme it might not contain a "//", but, it's considered hierarchical
1702 * anyways. Wildcard Schemes are always considered hierarchical
1704 if(data->scheme_type == URL_SCHEME_WILDCARD ||
1705 data->scheme_type == URL_SCHEME_FILE ||
1706 (!data->is_relative && check_hierarchical(ptr))) {
1707 /* Only treat it as a hierarchical URI if the scheme_type is known or
1708 * the Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES flag is not set.
1710 if(data->scheme_type != URL_SCHEME_UNKNOWN ||
1711 !(flags & Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES)) {
1712 TRACE("(%p %p %x): Treating URI as an hierarchical URI.\n", ptr, data, flags);
1713 data->is_opaque = FALSE;
1715 if(data->scheme_type == URL_SCHEME_FILE)
1716 /* Skip past the "//" after the scheme (if any). */
1717 check_hierarchical(ptr);
1719 /* TODO: Handle hierarchical URI's, parse authority then parse the path. */
1720 if(!parse_authority(ptr, data, flags))
1723 return parse_path_hierarchical(ptr, data, flags);
1725 /* Reset ptr to it's starting position so opaque path parsing
1726 * begins at the correct location.
1731 /* If it reaches here, then the URI will be treated as an opaque
1735 TRACE("(%p %p %x): Treating URI as an opaque URI.\n", ptr, data, flags);
1737 data->is_opaque = TRUE;
1738 if(!parse_path_opaque(ptr, data, flags))
1744 /* Attempts to parse the query string from the URI.
1747 * If NO_DECODE_EXTRA_INFO flag is set, then invalid percent encoded
1748 * data is allowed appear in the query string. For unknown scheme types
1749 * invalid percent encoded data is allowed to appear reguardless.
1751 static BOOL parse_query(const WCHAR **ptr, parse_data *data, DWORD flags) {
1752 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
1755 TRACE("(%p %p %x): URI didn't contain a query string.\n", ptr, data, flags);
1762 while(**ptr && **ptr != '#') {
1763 if(**ptr == '%' && known_scheme &&
1764 !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
1765 if(!check_pct_encoded(ptr)) {
1776 data->query_len = *ptr - data->query;
1778 TRACE("(%p %p %x): Parsed query string %s len=%d\n", ptr, data, flags,
1779 debugstr_wn(data->query, data->query_len), data->query_len);
1783 /* Parses and validates the components of the specified by data->uri
1784 * and stores the information it parses into 'data'.
1786 * Returns TRUE if it successfully parsed the URI. False otherwise.
1788 static BOOL parse_uri(parse_data *data, DWORD flags) {
1795 TRACE("(%p %x): BEGINNING TO PARSE URI %s.\n", data, flags, debugstr_w(data->uri));
1797 if(!parse_scheme(pptr, data, flags))
1800 if(!parse_hierpart(pptr, data, flags))
1803 if(!parse_query(pptr, data, flags))
1806 /* TODO: Parse fragment (if the URI has one). */
1808 TRACE("(%p %x): FINISHED PARSING URI.\n", data, flags);
1812 /* Canonicalizes the userinfo of the URI represented by the parse_data.
1814 * Canonicalization of the userinfo is a simple process. If there are any percent
1815 * encoded characters that fall in the "unreserved" character set, they are decoded
1816 * to their actual value. If a character is not in the "unreserved" or "reserved" sets
1817 * then it is percent encoded. Other than that the characters are copied over without
1820 static BOOL canonicalize_userinfo(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
1823 uri->userinfo_start = uri->userinfo_split = -1;
1824 uri->userinfo_len = 0;
1827 /* URI doesn't have userinfo, so nothing to do here. */
1830 uri->userinfo_start = uri->canon_len;
1832 while(i < data->userinfo_len) {
1833 if(data->userinfo[i] == ':' && uri->userinfo_split == -1)
1834 /* Windows only considers the first ':' as the delimiter. */
1835 uri->userinfo_split = uri->canon_len - uri->userinfo_start;
1836 else if(data->userinfo[i] == '%') {
1837 /* Only decode % encoded values for known scheme types. */
1838 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
1839 /* See if the value really needs decoded. */
1840 WCHAR val = decode_pct_val(data->userinfo + i);
1841 if(is_unreserved(val)) {
1843 uri->canon_uri[uri->canon_len] = val;
1847 /* Move pass the hex characters. */
1852 } else if(!is_reserved(data->userinfo[i]) && !is_unreserved(data->userinfo[i]) &&
1853 data->userinfo[i] != '\\') {
1854 /* Only percent encode forbidden characters if the NO_ENCODE_FORBIDDEN_CHARACTERS flag
1857 if(!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS)) {
1859 pct_encode_val(data->userinfo[i], uri->canon_uri + uri->canon_len);
1861 uri->canon_len += 3;
1868 /* Nothing special, so just copy the character over. */
1869 uri->canon_uri[uri->canon_len] = data->userinfo[i];
1875 uri->userinfo_len = uri->canon_len - uri->userinfo_start;
1877 TRACE("(%p %p %x %d): Canonicalized userinfo, userinfo_start=%d, userinfo=%s, userinfo_split=%d userinfo_len=%d.\n",
1878 data, uri, flags, computeOnly, uri->userinfo_start, debugstr_wn(uri->canon_uri + uri->userinfo_start, uri->userinfo_len),
1879 uri->userinfo_split, uri->userinfo_len);
1881 /* Now insert the '@' after the userinfo. */
1883 uri->canon_uri[uri->canon_len] = '@';
1889 /* Attempts to canonicalize a reg_name.
1891 * Things that happen:
1892 * 1) If Uri_CREATE_NO_CANONICALIZE flag is not set, then the reg_name is
1893 * lower cased. Unless it's an unknown scheme type, which case it's
1894 * no lower cased reguardless.
1896 * 2) Unreserved % encoded characters are decoded for known
1899 * 3) Forbidden characters are % encoded as long as
1900 * Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS flag is not set and
1901 * it isn't an unknown scheme type.
1903 * 4) If it's a file scheme and the host is "localhost" it's removed.
1905 static BOOL canonicalize_reg_name(const parse_data *data, Uri *uri,
1906 DWORD flags, BOOL computeOnly) {
1907 static const WCHAR localhostW[] =
1908 {'l','o','c','a','l','h','o','s','t',0};
1910 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
1912 uri->host_start = uri->canon_len;
1914 if(data->scheme_type == URL_SCHEME_FILE &&
1915 data->host_len == lstrlenW(localhostW)) {
1916 if(!StrCmpNIW(data->host, localhostW, data->host_len)) {
1917 uri->host_start = -1;
1919 uri->host_type = Uri_HOST_UNKNOWN;
1924 for(ptr = data->host; ptr < data->host+data->host_len; ++ptr) {
1925 if(*ptr == '%' && known_scheme) {
1926 WCHAR val = decode_pct_val(ptr);
1927 if(is_unreserved(val)) {
1928 /* If NO_CANONICALZE is not set, then windows lower cases the
1931 if(!(flags & Uri_CREATE_NO_CANONICALIZE) && isupperW(val)) {
1933 uri->canon_uri[uri->canon_len] = tolowerW(val);
1936 uri->canon_uri[uri->canon_len] = val;
1940 /* Skip past the % encoded character. */
1944 /* Just copy the % over. */
1946 uri->canon_uri[uri->canon_len] = *ptr;
1949 } else if(*ptr == '\\') {
1950 /* Only unknown scheme types could have made it here with a '\\' in the host name. */
1952 uri->canon_uri[uri->canon_len] = *ptr;
1954 } else if(!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS) &&
1955 !is_unreserved(*ptr) && !is_reserved(*ptr) && known_scheme) {
1957 pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
1959 /* The percent encoded value gets lower cased also. */
1960 if(!(flags & Uri_CREATE_NO_CANONICALIZE)) {
1961 uri->canon_uri[uri->canon_len+1] = tolowerW(uri->canon_uri[uri->canon_len+1]);
1962 uri->canon_uri[uri->canon_len+2] = tolowerW(uri->canon_uri[uri->canon_len+2]);
1966 uri->canon_len += 3;
1969 if(!(flags & Uri_CREATE_NO_CANONICALIZE) && known_scheme)
1970 uri->canon_uri[uri->canon_len] = tolowerW(*ptr);
1972 uri->canon_uri[uri->canon_len] = *ptr;
1979 uri->host_len = uri->canon_len - uri->host_start;
1982 TRACE("(%p %p %x %d): Canonicalize reg_name=%s len=%d\n", data, uri, flags,
1983 computeOnly, debugstr_wn(uri->canon_uri+uri->host_start, uri->host_len),
1987 find_domain_name(uri->canon_uri+uri->host_start, uri->host_len,
1988 &(uri->domain_offset));
1993 /* Attempts to canonicalize an implicit IPv4 address. */
1994 static BOOL canonicalize_implicit_ipv4address(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
1995 uri->host_start = uri->canon_len;
1997 TRACE("%u\n", data->implicit_ipv4);
1998 /* For unknown scheme types Window's doesn't convert
1999 * the value into an IP address, but, it still considers
2000 * it an IPv4 address.
2002 if(data->scheme_type == URL_SCHEME_UNKNOWN) {
2004 memcpy(uri->canon_uri+uri->canon_len, data->host, data->host_len*sizeof(WCHAR));
2005 uri->canon_len += data->host_len;
2008 uri->canon_len += ui2ipv4(uri->canon_uri+uri->canon_len, data->implicit_ipv4);
2010 uri->canon_len += ui2ipv4(NULL, data->implicit_ipv4);
2013 uri->host_len = uri->canon_len - uri->host_start;
2014 uri->host_type = Uri_HOST_IPV4;
2017 TRACE("%p %p %x %d): Canonicalized implicit IP address=%s len=%d\n",
2018 data, uri, flags, computeOnly,
2019 debugstr_wn(uri->canon_uri+uri->host_start, uri->host_len),
2025 /* Attempts to canonicalize an IPv4 address.
2027 * If the parse_data represents a URI that has an implicit IPv4 address
2028 * (ex. http://256/, this function will convert 256 into 0.0.1.0). If
2029 * the implicit IP address exceeds the value of UINT_MAX (maximum value
2030 * for an IPv4 address) it's canonicalized as if were a reg-name.
2032 * If the parse_data contains a partial or full IPv4 address it normalizes it.
2033 * A partial IPv4 address is something like "192.0" and would be normalized to
2034 * "192.0.0.0". With a full (or partial) IPv4 address like "192.002.01.003" would
2035 * be normalized to "192.2.1.3".
2038 * Window's ONLY normalizes IPv4 address for known scheme types (one that isn't
2039 * URL_SCHEME_UNKNOWN). For unknown scheme types, it simply copies the data from
2040 * the original URI into the canonicalized URI, but, it still recognizes URI's
2041 * host type as HOST_IPV4.
2043 static BOOL canonicalize_ipv4address(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2044 if(data->has_implicit_ip)
2045 return canonicalize_implicit_ipv4address(data, uri, flags, computeOnly);
2047 uri->host_start = uri->canon_len;
2049 /* Windows only normalizes for known scheme types. */
2050 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
2051 /* parse_data contains a partial or full IPv4 address, so normalize it. */
2052 DWORD i, octetDigitCount = 0, octetCount = 0;
2053 BOOL octetHasDigit = FALSE;
2055 for(i = 0; i < data->host_len; ++i) {
2056 if(data->host[i] == '0' && !octetHasDigit) {
2057 /* Can ignore leading zeros if:
2058 * 1) It isn't the last digit of the octet.
2059 * 2) i+1 != data->host_len
2062 if(octetDigitCount == 2 ||
2063 i+1 == data->host_len ||
2064 data->host[i+1] == '.') {
2066 uri->canon_uri[uri->canon_len] = data->host[i];
2068 TRACE("Adding zero\n");
2070 } else if(data->host[i] == '.') {
2072 uri->canon_uri[uri->canon_len] = data->host[i];
2075 octetDigitCount = 0;
2076 octetHasDigit = FALSE;
2080 uri->canon_uri[uri->canon_len] = data->host[i];
2084 octetHasDigit = TRUE;
2088 /* Make sure the canonicalized IP address has 4 dec-octets.
2089 * If doesn't add "0" ones until there is 4;
2091 for( ; octetCount < 3; ++octetCount) {
2093 uri->canon_uri[uri->canon_len] = '.';
2094 uri->canon_uri[uri->canon_len+1] = '0';
2097 uri->canon_len += 2;
2100 /* Windows doesn't normalize addresses in unknown schemes. */
2102 memcpy(uri->canon_uri+uri->canon_len, data->host, data->host_len*sizeof(WCHAR));
2103 uri->canon_len += data->host_len;
2106 uri->host_len = uri->canon_len - uri->host_start;
2108 TRACE("(%p %p %x %d): Canonicalized IPv4 address, ip=%s len=%d\n",
2109 data, uri, flags, computeOnly,
2110 debugstr_wn(uri->canon_uri+uri->host_start, uri->host_len),
2117 /* Attempts to canonicalize the IPv6 address of the URI.
2119 * Multiple things happen during the canonicalization of an IPv6 address:
2120 * 1) Any leading zero's in an h16 component are removed.
2121 * Ex: [0001:0022::] -> [1:22::]
2123 * 2) The longest sequence of zero h16 components are compressed
2124 * into a "::" (elision). If there's a tie, the first is choosen.
2126 * Ex: [0:0:0:0:1:6:7:8] -> [::1:6:7:8]
2127 * [0:0:0:0:1:2::] -> [::1:2:0:0]
2128 * [0:0:1:2:0:0:7:8] -> [::1:2:0:0:7:8]
2130 * 3) If an IPv4 address is attached to the IPv6 address, it's
2132 * Ex: [::001.002.022.000] -> [::1.2.22.0]
2134 * 4) If an elision is present, but, only represents 1 h16 component
2137 * Ex: [1::2:3:4:5:6:7] -> [1:0:2:3:4:5:6:7]
2139 * 5) If the IPv6 address contains an IPv4 address and there exists
2140 * at least 1 non-zero h16 component the IPv4 address is converted
2141 * into two h16 components, otherwise it's normalized and kept as is.
2143 * Ex: [::192.200.003.4] -> [::192.200.3.4]
2144 * [ffff::192.200.003.4] -> [ffff::c0c8:3041]
2147 * For unknown scheme types Windows simply copies the address over without any
2150 * IPv4 address can be included in an elision if all its components are 0's.
2152 static BOOL canonicalize_ipv6address(const parse_data *data, Uri *uri,
2153 DWORD flags, BOOL computeOnly) {
2154 uri->host_start = uri->canon_len;
2156 if(data->scheme_type == URL_SCHEME_UNKNOWN) {
2158 memcpy(uri->canon_uri+uri->canon_len, data->host, data->host_len*sizeof(WCHAR));
2159 uri->canon_len += data->host_len;
2163 DWORD i, elision_len;
2165 if(!ipv6_to_number(&(data->ipv6_address), values)) {
2166 TRACE("(%p %p %x %d): Failed to compute numerical value for IPv6 address.\n",
2167 data, uri, flags, computeOnly);
2172 uri->canon_uri[uri->canon_len] = '[';
2175 /* Find where the elision should occur (if any). */
2176 compute_elision_location(&(data->ipv6_address), values, &elision_start, &elision_len);
2178 TRACE("%p %p %x %d): Elision starts at %d, len=%u\n", data, uri, flags,
2179 computeOnly, elision_start, elision_len);
2181 for(i = 0; i < 8; ++i) {
2182 BOOL in_elision = (elision_start > -1 && i >= elision_start &&
2183 i < elision_start+elision_len);
2184 BOOL do_ipv4 = (i == 6 && data->ipv6_address.ipv4 && !in_elision &&
2185 data->ipv6_address.h16_count == 0);
2187 if(i == elision_start) {
2189 uri->canon_uri[uri->canon_len] = ':';
2190 uri->canon_uri[uri->canon_len+1] = ':';
2192 uri->canon_len += 2;
2195 /* We can ignore the current component if we're in the elision. */
2199 /* We only add a ':' if we're not at i == 0, or when we're at
2200 * the very end of elision range since the ':' colon was handled
2201 * earlier. Otherwise we would end up with ":::" after elision.
2203 if(i != 0 && !(elision_start > -1 && i == elision_start+elision_len)) {
2205 uri->canon_uri[uri->canon_len] = ':';
2213 /* Combine the two parts of the IPv4 address values. */
2219 len = ui2ipv4(uri->canon_uri+uri->canon_len, val);
2221 len = ui2ipv4(NULL, val);
2223 uri->canon_len += len;
2226 /* Write a regular h16 component to the URI. */
2228 /* Short circuit for the trivial case. */
2229 if(values[i] == 0) {
2231 uri->canon_uri[uri->canon_len] = '0';
2234 static const WCHAR formatW[] = {'%','x',0};
2237 uri->canon_len += sprintfW(uri->canon_uri+uri->canon_len,
2238 formatW, values[i]);
2241 uri->canon_len += sprintfW(tmp, formatW, values[i]);
2247 /* Add the closing ']'. */
2249 uri->canon_uri[uri->canon_len] = ']';
2253 uri->host_len = uri->canon_len - uri->host_start;
2256 TRACE("(%p %p %x %d): Canonicalized IPv6 address %s, len=%d\n", data, uri, flags,
2257 computeOnly, debugstr_wn(uri->canon_uri+uri->host_start, uri->host_len),
2263 /* Attempts to canonicalize the host of the URI (if any). */
2264 static BOOL canonicalize_host(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2265 uri->host_start = -1;
2267 uri->domain_offset = -1;
2270 switch(data->host_type) {
2272 uri->host_type = Uri_HOST_DNS;
2273 if(!canonicalize_reg_name(data, uri, flags, computeOnly))
2278 uri->host_type = Uri_HOST_IPV4;
2279 if(!canonicalize_ipv4address(data, uri, flags, computeOnly))
2284 if(!canonicalize_ipv6address(data, uri, flags, computeOnly))
2287 uri->host_type = Uri_HOST_IPV6;
2289 case Uri_HOST_UNKNOWN:
2290 if(data->host_len > 0 || data->scheme_type != URL_SCHEME_FILE) {
2291 uri->host_start = uri->canon_len;
2293 /* Nothing happens to unknown host types. */
2295 memcpy(uri->canon_uri+uri->canon_len, data->host, data->host_len*sizeof(WCHAR));
2296 uri->canon_len += data->host_len;
2297 uri->host_len = data->host_len;
2300 uri->host_type = Uri_HOST_UNKNOWN;
2303 FIXME("(%p %p %x %d): Canonicalization for host type %d not supported.\n", data,
2304 uri, flags, computeOnly, data->host_type);
2312 static BOOL canonicalize_port(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2313 BOOL has_default_port = FALSE;
2314 USHORT default_port = 0;
2317 uri->has_port = FALSE;
2319 /* Check if the scheme has a default port. */
2320 for(i = 0; i < sizeof(default_ports)/sizeof(default_ports[0]); ++i) {
2321 if(default_ports[i].scheme == data->scheme_type) {
2322 has_default_port = TRUE;
2323 default_port = default_ports[i].port;
2328 if(data->port || has_default_port)
2329 uri->has_port = TRUE;
2332 * 1) Has a port which is the default port.
2333 * 2) Has a port (not the default).
2334 * 3) Doesn't have a port, but, scheme has a default port.
2337 if(has_default_port && data->port && data->port_value == default_port) {
2338 /* If it's the default port and this flag isn't set, don't do anything. */
2339 if(flags & Uri_CREATE_NO_CANONICALIZE) {
2340 /* Copy the original port over. */
2342 uri->canon_uri[uri->canon_len] = ':';
2343 memcpy(uri->canon_uri+uri->canon_len+1, data->port, data->port_len*sizeof(WCHAR));
2345 uri->canon_len += data->port_len+1;
2348 uri->port = default_port;
2349 } else if(data->port) {
2351 uri->canon_uri[uri->canon_len] = ':';
2354 if(flags & Uri_CREATE_NO_CANONICALIZE) {
2355 /* Copy the original over without changes. */
2357 memcpy(uri->canon_uri+uri->canon_len, data->port, data->port_len*sizeof(WCHAR));
2358 uri->canon_len += data->port_len;
2360 const WCHAR formatW[] = {'%','u',0};
2363 len = sprintfW(uri->canon_uri+uri->canon_len, formatW, data->port_value);
2366 len = sprintfW(tmp, formatW, data->port_value);
2368 uri->canon_len += len;
2371 uri->port = data->port_value;
2372 } else if(has_default_port)
2373 uri->port = default_port;
2378 /* Canonicalizes the authority of the URI represented by the parse_data. */
2379 static BOOL canonicalize_authority(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2380 uri->authority_start = uri->canon_len;
2381 uri->authority_len = 0;
2383 if(!canonicalize_userinfo(data, uri, flags, computeOnly))
2386 if(!canonicalize_host(data, uri, flags, computeOnly))
2389 if(!canonicalize_port(data, uri, flags, computeOnly))
2392 if(uri->host_start != -1)
2393 uri->authority_len = uri->canon_len - uri->authority_start;
2395 uri->authority_start = -1;
2400 /* Attempts to canonicalize the path of a hierarchical URI.
2402 * Things that happen:
2403 * 1). Forbidden characters are percent encoded, unless the NO_ENCODE_FORBIDDEN
2404 * flag is set or it's a file URI. Forbidden characters are always encoded
2405 * for file schemes reguardless and forbidden characters are never encoded
2406 * for unknown scheme types.
2408 * 2). For known scheme types '\\' are changed to '/'.
2410 * 3). Percent encoded, unreserved characters are decoded to their actual values.
2411 * Unless the scheme type is unknown. For file schemes any percent encoded
2412 * character in the unreserved or reserved set is decoded.
2414 * 4). For File schemes if the path is starts with a drive letter and doesn't
2415 * start with a '/' then one is appended.
2416 * Ex: file://c:/test.mp3 -> file:///c:/test.mp3
2418 * 5). Dot segments are removed from the path for all scheme types
2419 * unless NO_CANONICALIZE flag is set. Dot segments aren't removed
2420 * for wildcard scheme types.
2423 * file://c:/test%20test -> file:///c:/test%2520test
2424 * file://c:/test%3Etest -> file:///c:/test%253Etest
2425 * file:///c:/test%20test -> file:///c:/test%20test
2426 * file:///c:/test%test -> file:///c:/test%25test
2428 static BOOL canonicalize_path_hierarchical(const parse_data *data, Uri *uri,
2429 DWORD flags, BOOL computeOnly) {
2431 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
2432 const BOOL is_file = data->scheme_type == URL_SCHEME_FILE;
2434 BOOL escape_pct = FALSE;
2437 uri->path_start = -1;
2442 uri->path_start = uri->canon_len;
2444 /* Check if a '/' needs to be appended for the file scheme. */
2446 if(data->path_len > 1 && is_alpha(*(data->path)) &&
2447 *(data->path+1) == ':') {
2449 uri->canon_uri[uri->canon_len] = '/';
2455 for(ptr = data->path; ptr < data->path+data->path_len; ++ptr) {
2457 const WCHAR *tmp = ptr;
2460 /* Check if the % represents a valid encoded char, or if it needs encoded. */
2461 BOOL force_encode = !check_pct_encoded(&tmp) && is_file;
2462 val = decode_pct_val(ptr);
2464 if(force_encode || escape_pct) {
2465 /* Escape the percent sign in the file URI. */
2467 pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
2468 uri->canon_len += 3;
2469 } else if((is_unreserved(val) && known_scheme) ||
2470 (is_file && (is_unreserved(val) || is_reserved(val)))) {
2472 uri->canon_uri[uri->canon_len] = val;
2479 uri->canon_uri[uri->canon_len] = *ptr;
2482 } else if(*ptr == '\\' && known_scheme) {
2484 uri->canon_uri[uri->canon_len] = '/';
2486 } else if(known_scheme && !is_unreserved(*ptr) && !is_reserved(*ptr) &&
2487 (!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS) || is_file)) {
2488 /* Escape the forbidden character. */
2490 pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
2491 uri->canon_len += 3;
2494 uri->canon_uri[uri->canon_len] = *ptr;
2499 uri->path_len = uri->canon_len - uri->path_start;
2501 /* Removing the dot segments only happens when it's not in
2502 * computeOnly mode and it's not a wildcard scheme.
2504 if(!computeOnly && data->scheme_type != URL_SCHEME_WILDCARD) {
2505 if(!(flags & Uri_CREATE_NO_CANONICALIZE)) {
2506 /* Remove the dot segments (if any) and reset everything to the new
2509 DWORD new_len = remove_dot_segments(uri->canon_uri+uri->path_start, uri->path_len);
2510 uri->canon_len -= uri->path_len-new_len;
2511 uri->path_len = new_len;
2516 TRACE("Canonicalized path %s len=%d\n",
2517 debugstr_wn(uri->canon_uri+uri->path_start, uri->path_len),
2523 /* Attempts to canonicalize the path for an opaque URI.
2525 * For known scheme types:
2526 * 1) forbidden characters are percent encoded if
2527 * NO_ENCODE_FORBIDDEN_CHARACTERS isn't set.
2529 * 2) Percent encoded, unreserved characters are decoded
2530 * to their actual values, for known scheme types.
2532 * 3) '\\' are changed to '/' for known scheme types
2533 * except for mailto schemes.
2535 static BOOL canonicalize_path_opaque(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2537 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
2540 uri->path_start = -1;
2545 uri->path_start = uri->canon_len;
2547 /* Windows doesn't allow a "//" to appear after the scheme
2548 * of a URI, if it's an opaque URI.
2550 if(data->scheme && *(data->path) == '/' && *(data->path+1) == '/') {
2551 /* So it inserts a "/." before the "//" if it exists. */
2553 uri->canon_uri[uri->canon_len] = '/';
2554 uri->canon_uri[uri->canon_len+1] = '.';
2557 uri->canon_len += 2;
2560 for(ptr = data->path; ptr < data->path+data->path_len; ++ptr) {
2561 if(*ptr == '%' && known_scheme) {
2562 WCHAR val = decode_pct_val(ptr);
2564 if(is_unreserved(val)) {
2566 uri->canon_uri[uri->canon_len] = val;
2573 uri->canon_uri[uri->canon_len] = *ptr;
2576 } else if(known_scheme && !is_unreserved(*ptr) && !is_reserved(*ptr) &&
2577 !(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS)) {
2579 pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
2580 uri->canon_len += 3;
2583 uri->canon_uri[uri->canon_len] = *ptr;
2588 uri->path_len = uri->canon_len - uri->path_start;
2590 TRACE("(%p %p %x %d): Canonicalized opaque URI path %s len=%d\n", data, uri, flags, computeOnly,
2591 debugstr_wn(uri->canon_uri+uri->path_start, uri->path_len), uri->path_len);
2595 /* Determines how the URI represented by the parse_data should be canonicalized.
2597 * Essentially, if the parse_data represents an hierarchical URI then it calls
2598 * canonicalize_authority and the canonicalization functions for the path. If the
2599 * URI is opaque it canonicalizes the path of the URI.
2601 static BOOL canonicalize_hierpart(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2602 if(!data->is_opaque) {
2603 /* "//" is only added for non-wildcard scheme types. */
2604 if(data->scheme_type != URL_SCHEME_WILDCARD) {
2606 INT pos = uri->canon_len;
2608 uri->canon_uri[pos] = '/';
2609 uri->canon_uri[pos+1] = '/';
2611 uri->canon_len += 2;
2614 if(!canonicalize_authority(data, uri, flags, computeOnly))
2617 /* TODO: Canonicalize the path of the URI. */
2618 if(!canonicalize_path_hierarchical(data, uri, flags, computeOnly))
2622 /* Opaque URI's don't have an authority. */
2623 uri->userinfo_start = uri->userinfo_split = -1;
2624 uri->userinfo_len = 0;
2625 uri->host_start = -1;
2627 uri->host_type = Uri_HOST_UNKNOWN;
2628 uri->has_port = FALSE;
2629 uri->authority_start = -1;
2630 uri->authority_len = 0;
2631 uri->domain_offset = -1;
2633 if(!canonicalize_path_opaque(data, uri, flags, computeOnly))
2637 if(uri->path_start > -1 && !computeOnly)
2638 /* Finding file extensions happens for both types of URIs. */
2639 uri->extension_offset = find_file_extension(uri->canon_uri+uri->path_start, uri->path_len);
2641 uri->extension_offset = -1;
2646 /* Canonicalizes the scheme information specified in the parse_data using the specified flags. */
2647 static BOOL canonicalize_scheme(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2648 uri->scheme_start = -1;
2649 uri->scheme_len = 0;
2652 /* The only type of URI that doesn't have to have a scheme is a relative
2655 if(!data->is_relative) {
2656 FIXME("(%p %p %x): Unable to determine the scheme type of %s.\n", data,
2657 uri, flags, debugstr_w(data->uri));
2663 INT pos = uri->canon_len;
2665 for(i = 0; i < data->scheme_len; ++i) {
2666 /* Scheme name must be lower case after canonicalization. */
2667 uri->canon_uri[i + pos] = tolowerW(data->scheme[i]);
2670 uri->canon_uri[i + pos] = ':';
2671 uri->scheme_start = pos;
2673 TRACE("(%p %p %x): Canonicalized scheme=%s, len=%d.\n", data, uri, flags,
2674 debugstr_wn(uri->canon_uri, uri->scheme_len), data->scheme_len);
2677 /* This happens in both computation modes. */
2678 uri->canon_len += data->scheme_len + 1;
2679 uri->scheme_len = data->scheme_len;
2684 /* Compute's what the length of the URI specified by the parse_data will be
2685 * after canonicalization occurs using the specified flags.
2687 * This function will return a non-zero value indicating the length of the canonicalized
2688 * URI, or -1 on error.
2690 static int compute_canonicalized_length(const parse_data *data, DWORD flags) {
2693 memset(&uri, 0, sizeof(Uri));
2695 TRACE("(%p %x): Beginning to compute canonicalized length for URI %s\n", data, flags,
2696 debugstr_w(data->uri));
2698 if(!canonicalize_scheme(data, &uri, flags, TRUE)) {
2699 ERR("(%p %x): Failed to compute URI scheme length.\n", data, flags);
2703 if(!canonicalize_hierpart(data, &uri, flags, TRUE)) {
2704 ERR("(%p %x): Failed to compute URI hierpart length.\n", data, flags);
2708 TRACE("(%p %x): Finished computing canonicalized URI length. length=%d\n", data, flags, uri.canon_len);
2710 return uri.canon_len;
2713 /* Canonicalizes the URI data specified in the parse_data, using the given flags. If the
2714 * canonicalization succeededs it will store all the canonicalization information
2715 * in the pointer to the Uri.
2717 * To canonicalize a URI this function first computes what the length of the URI
2718 * specified by the parse_data will be. Once this is done it will then perfom the actual
2719 * canonicalization of the URI.
2721 static HRESULT canonicalize_uri(const parse_data *data, Uri *uri, DWORD flags) {
2724 uri->canon_uri = NULL;
2725 len = uri->canon_size = uri->canon_len = 0;
2727 TRACE("(%p %p %x): beginning to canonicalize URI %s.\n", data, uri, flags, debugstr_w(data->uri));
2729 /* First try to compute the length of the URI. */
2730 len = compute_canonicalized_length(data, flags);
2732 ERR("(%p %p %x): Could not compute the canonicalized length of %s.\n", data, uri, flags,
2733 debugstr_w(data->uri));
2734 return E_INVALIDARG;
2737 uri->canon_uri = heap_alloc((len+1)*sizeof(WCHAR));
2739 return E_OUTOFMEMORY;
2741 uri->canon_size = len;
2742 if(!canonicalize_scheme(data, uri, flags, FALSE)) {
2743 ERR("(%p %p %x): Unable to canonicalize the scheme of the URI.\n", data, uri, flags);
2744 heap_free(uri->canon_uri);
2745 return E_INVALIDARG;
2747 uri->scheme_type = data->scheme_type;
2749 if(!canonicalize_hierpart(data, uri, flags, FALSE)) {
2750 ERR("(%p %p %x): Unable to canonicalize the heirpart of the URI\n", data, uri, flags);
2751 heap_free(uri->canon_uri);
2752 return E_INVALIDARG;
2755 /* There's a possibility we didn't use all the space we allocated
2758 if(uri->canon_len < uri->canon_size) {
2759 /* This happens if the URI is hierarchical and dot
2760 * segments were removed from it's path.
2762 WCHAR *tmp = heap_realloc(uri->canon_uri, (uri->canon_len+1)*sizeof(WCHAR));
2764 return E_OUTOFMEMORY;
2766 uri->canon_uri = tmp;
2767 uri->canon_size = uri->canon_len;
2770 uri->canon_uri[uri->canon_len] = '\0';
2771 TRACE("(%p %p %x): finished canonicalizing the URI. uri=%s\n", data, uri, flags, debugstr_w(uri->canon_uri));
2776 #define URI(x) ((IUri*) &(x)->lpIUriVtbl)
2777 #define URIBUILDER(x) ((IUriBuilder*) &(x)->lpIUriBuilderVtbl)
2779 #define URI_THIS(iface) DEFINE_THIS(Uri, IUri, iface)
2781 static HRESULT WINAPI Uri_QueryInterface(IUri *iface, REFIID riid, void **ppv)
2783 Uri *This = URI_THIS(iface);
2785 if(IsEqualGUID(&IID_IUnknown, riid)) {
2786 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
2788 }else if(IsEqualGUID(&IID_IUri, riid)) {
2789 TRACE("(%p)->(IID_IUri %p)\n", This, ppv);
2792 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
2794 return E_NOINTERFACE;
2797 IUnknown_AddRef((IUnknown*)*ppv);
2801 static ULONG WINAPI Uri_AddRef(IUri *iface)
2803 Uri *This = URI_THIS(iface);
2804 LONG ref = InterlockedIncrement(&This->ref);
2806 TRACE("(%p) ref=%d\n", This, ref);
2811 static ULONG WINAPI Uri_Release(IUri *iface)
2813 Uri *This = URI_THIS(iface);
2814 LONG ref = InterlockedDecrement(&This->ref);
2816 TRACE("(%p) ref=%d\n", This, ref);
2819 SysFreeString(This->raw_uri);
2820 heap_free(This->canon_uri);
2827 static HRESULT WINAPI Uri_GetPropertyBSTR(IUri *iface, Uri_PROPERTY uriProp, BSTR *pbstrProperty, DWORD dwFlags)
2829 Uri *This = URI_THIS(iface);
2831 TRACE("(%p)->(%d %p %x)\n", This, uriProp, pbstrProperty, dwFlags);
2836 if(uriProp > Uri_PROPERTY_STRING_LAST) {
2837 /* Windows allocates an empty BSTR for invalid Uri_PROPERTY's. */
2838 *pbstrProperty = SysAllocStringLen(NULL, 0);
2839 if(!(*pbstrProperty))
2840 return E_OUTOFMEMORY;
2842 /* It only returns S_FALSE for the ZONE property... */
2843 if(uriProp == Uri_PROPERTY_ZONE)
2849 /* Don't have support for flags yet. */
2851 FIXME("(%p)->(%d %p %x)\n", This, uriProp, pbstrProperty, dwFlags);
2856 case Uri_PROPERTY_AUTHORITY:
2857 if(This->authority_start > -1) {
2858 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->authority_start, This->authority_len);
2861 *pbstrProperty = SysAllocStringLen(NULL, 0);
2865 if(!(*pbstrProperty))
2866 hres = E_OUTOFMEMORY;
2869 case Uri_PROPERTY_DOMAIN:
2870 if(This->domain_offset > -1) {
2871 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->host_start+This->domain_offset,
2872 This->host_len-This->domain_offset);
2875 *pbstrProperty = SysAllocStringLen(NULL, 0);
2879 if(!(*pbstrProperty))
2880 hres = E_OUTOFMEMORY;
2883 case Uri_PROPERTY_EXTENSION:
2884 if(This->extension_offset > -1) {
2885 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->path_start+This->extension_offset,
2886 This->path_len-This->extension_offset);
2889 *pbstrProperty = SysAllocStringLen(NULL, 0);
2893 if(!(*pbstrProperty))
2894 hres = E_OUTOFMEMORY;
2897 case Uri_PROPERTY_HOST:
2898 if(This->host_start > -1) {
2899 /* The '[' and ']' aren't included for IPv6 addresses. */
2900 if(This->host_type == Uri_HOST_IPV6)
2901 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->host_start+1, This->host_len-2);
2903 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->host_start, This->host_len);
2907 *pbstrProperty = SysAllocStringLen(NULL, 0);
2911 if(!(*pbstrProperty))
2912 hres = E_OUTOFMEMORY;
2915 case Uri_PROPERTY_PASSWORD:
2916 if(This->userinfo_split > -1) {
2917 *pbstrProperty = SysAllocStringLen(
2918 This->canon_uri+This->userinfo_start+This->userinfo_split+1,
2919 This->userinfo_len-This->userinfo_split-1);
2922 *pbstrProperty = SysAllocStringLen(NULL, 0);
2926 if(!(*pbstrProperty))
2927 return E_OUTOFMEMORY;
2930 case Uri_PROPERTY_PATH:
2931 if(This->path_start > -1) {
2932 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->path_start, This->path_len);
2935 *pbstrProperty = SysAllocStringLen(NULL, 0);
2939 if(!(*pbstrProperty))
2940 hres = E_OUTOFMEMORY;
2943 case Uri_PROPERTY_RAW_URI:
2944 *pbstrProperty = SysAllocString(This->raw_uri);
2945 if(!(*pbstrProperty))
2946 hres = E_OUTOFMEMORY;
2950 case Uri_PROPERTY_SCHEME_NAME:
2951 if(This->scheme_start > -1) {
2952 *pbstrProperty = SysAllocStringLen(This->canon_uri + This->scheme_start, This->scheme_len);
2955 *pbstrProperty = SysAllocStringLen(NULL, 0);
2959 if(!(*pbstrProperty))
2960 hres = E_OUTOFMEMORY;
2963 case Uri_PROPERTY_USER_INFO:
2964 if(This->userinfo_start > -1) {
2965 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->userinfo_start, This->userinfo_len);
2968 *pbstrProperty = SysAllocStringLen(NULL, 0);
2972 if(!(*pbstrProperty))
2973 hres = E_OUTOFMEMORY;
2976 case Uri_PROPERTY_USER_NAME:
2977 if(This->userinfo_start > -1) {
2978 /* If userinfo_split is set, that means a password exists
2979 * so the username is only from userinfo_start to userinfo_split.
2981 if(This->userinfo_split > -1) {
2982 *pbstrProperty = SysAllocStringLen(This->canon_uri + This->userinfo_start, This->userinfo_split);
2985 *pbstrProperty = SysAllocStringLen(This->canon_uri + This->userinfo_start, This->userinfo_len);
2989 *pbstrProperty = SysAllocStringLen(NULL, 0);
2993 if(!(*pbstrProperty))
2994 return E_OUTOFMEMORY;
2998 FIXME("(%p)->(%d %p %x)\n", This, uriProp, pbstrProperty, dwFlags);
3005 static HRESULT WINAPI Uri_GetPropertyLength(IUri *iface, Uri_PROPERTY uriProp, DWORD *pcchProperty, DWORD dwFlags)
3007 Uri *This = URI_THIS(iface);
3009 TRACE("(%p)->(%d %p %x)\n", This, uriProp, pcchProperty, dwFlags);
3012 return E_INVALIDARG;
3014 /* Can only return a length for a property if it's a string. */
3015 if(uriProp > Uri_PROPERTY_STRING_LAST)
3016 return E_INVALIDARG;
3018 /* Don't have support for flags yet. */
3020 FIXME("(%p)->(%d %p %x)\n", This, uriProp, pcchProperty, dwFlags);
3025 case Uri_PROPERTY_AUTHORITY:
3026 *pcchProperty = This->authority_len;
3027 hres = (This->authority_start > -1) ? S_OK : S_FALSE;
3029 case Uri_PROPERTY_DOMAIN:
3030 if(This->domain_offset > -1)
3031 *pcchProperty = This->host_len - This->domain_offset;
3035 hres = (This->domain_offset > -1) ? S_OK : S_FALSE;
3037 case Uri_PROPERTY_EXTENSION:
3038 if(This->extension_offset > -1) {
3039 *pcchProperty = This->path_len - This->extension_offset;
3047 case Uri_PROPERTY_HOST:
3048 *pcchProperty = This->host_len;
3050 /* '[' and ']' aren't included in the length. */
3051 if(This->host_type == Uri_HOST_IPV6)
3054 hres = (This->host_start > -1) ? S_OK : S_FALSE;
3056 case Uri_PROPERTY_PASSWORD:
3057 *pcchProperty = (This->userinfo_split > -1) ? This->userinfo_len-This->userinfo_split-1 : 0;
3058 hres = (This->userinfo_split > -1) ? S_OK : S_FALSE;
3060 case Uri_PROPERTY_PATH:
3061 *pcchProperty = This->path_len;
3062 hres = (This->path_start > -1) ? S_OK : S_FALSE;
3064 case Uri_PROPERTY_RAW_URI:
3065 *pcchProperty = SysStringLen(This->raw_uri);
3068 case Uri_PROPERTY_SCHEME_NAME:
3069 *pcchProperty = This->scheme_len;
3070 hres = (This->scheme_start > -1) ? S_OK : S_FALSE;
3072 case Uri_PROPERTY_USER_INFO:
3073 *pcchProperty = This->userinfo_len;
3074 hres = (This->userinfo_start > -1) ? S_OK : S_FALSE;
3076 case Uri_PROPERTY_USER_NAME:
3077 *pcchProperty = (This->userinfo_split > -1) ? This->userinfo_split : This->userinfo_len;
3078 hres = (This->userinfo_start > -1) ? S_OK : S_FALSE;
3081 FIXME("(%p)->(%d %p %x)\n", This, uriProp, pcchProperty, dwFlags);
3088 static HRESULT WINAPI Uri_GetPropertyDWORD(IUri *iface, Uri_PROPERTY uriProp, DWORD *pcchProperty, DWORD dwFlags)
3090 Uri *This = URI_THIS(iface);
3093 TRACE("(%p)->(%d %p %x)\n", This, uriProp, pcchProperty, dwFlags);
3096 return E_INVALIDARG;
3098 /* Microsoft's implementation for the ZONE property of a URI seems to be lacking...
3099 * From what I can tell, instead of checking which URLZONE the URI belongs to it
3100 * simply assigns URLZONE_INVALID and returns E_NOTIMPL. This also applies to the GetZone
3103 if(uriProp == Uri_PROPERTY_ZONE) {
3104 *pcchProperty = URLZONE_INVALID;
3108 if(uriProp < Uri_PROPERTY_DWORD_START) {
3110 return E_INVALIDARG;
3114 case Uri_PROPERTY_HOST_TYPE:
3115 *pcchProperty = This->host_type;
3118 case Uri_PROPERTY_PORT:
3119 if(!This->has_port) {
3123 *pcchProperty = This->port;
3128 case Uri_PROPERTY_SCHEME:
3129 *pcchProperty = This->scheme_type;
3133 FIXME("(%p)->(%d %p %x)\n", This, uriProp, pcchProperty, dwFlags);
3140 static HRESULT WINAPI Uri_HasProperty(IUri *iface, Uri_PROPERTY uriProp, BOOL *pfHasProperty)
3142 Uri *This = URI_THIS(iface);
3143 FIXME("(%p)->(%d %p)\n", This, uriProp, pfHasProperty);
3146 return E_INVALIDARG;
3151 static HRESULT WINAPI Uri_GetAbsoluteUri(IUri *iface, BSTR *pstrAbsoluteUri)
3153 Uri *This = URI_THIS(iface);
3154 FIXME("(%p)->(%p)\n", This, pstrAbsoluteUri);
3156 if(!pstrAbsoluteUri)
3162 static HRESULT WINAPI Uri_GetAuthority(IUri *iface, BSTR *pstrAuthority)
3164 TRACE("(%p)->(%p)\n", iface, pstrAuthority);
3165 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_AUTHORITY, pstrAuthority, 0);
3168 static HRESULT WINAPI Uri_GetDisplayUri(IUri *iface, BSTR *pstrDisplayUri)
3170 Uri *This = URI_THIS(iface);
3171 FIXME("(%p)->(%p)\n", This, pstrDisplayUri);
3179 static HRESULT WINAPI Uri_GetDomain(IUri *iface, BSTR *pstrDomain)
3181 TRACE("(%p)->(%p)\n", iface, pstrDomain);
3182 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_DOMAIN, pstrDomain, 0);
3185 static HRESULT WINAPI Uri_GetExtension(IUri *iface, BSTR *pstrExtension)
3187 TRACE("(%p)->(%p)\n", iface, pstrExtension);
3188 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_EXTENSION, pstrExtension, 0);
3191 static HRESULT WINAPI Uri_GetFragment(IUri *iface, BSTR *pstrFragment)
3193 Uri *This = URI_THIS(iface);
3194 FIXME("(%p)->(%p)\n", This, pstrFragment);
3202 static HRESULT WINAPI Uri_GetHost(IUri *iface, BSTR *pstrHost)
3204 TRACE("(%p)->(%p)\n", iface, pstrHost);
3205 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_HOST, pstrHost, 0);
3208 static HRESULT WINAPI Uri_GetPassword(IUri *iface, BSTR *pstrPassword)
3210 TRACE("(%p)->(%p)\n", iface, pstrPassword);
3211 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_PASSWORD, pstrPassword, 0);
3214 static HRESULT WINAPI Uri_GetPath(IUri *iface, BSTR *pstrPath)
3216 TRACE("(%p)->(%p)\n", iface, pstrPath);
3217 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_PATH, pstrPath, 0);
3220 static HRESULT WINAPI Uri_GetPathAndQuery(IUri *iface, BSTR *pstrPathAndQuery)
3222 Uri *This = URI_THIS(iface);
3223 FIXME("(%p)->(%p)\n", This, pstrPathAndQuery);
3225 if(!pstrPathAndQuery)
3231 static HRESULT WINAPI Uri_GetQuery(IUri *iface, BSTR *pstrQuery)
3233 Uri *This = URI_THIS(iface);
3234 FIXME("(%p)->(%p)\n", This, pstrQuery);
3242 static HRESULT WINAPI Uri_GetRawUri(IUri *iface, BSTR *pstrRawUri)
3244 Uri *This = URI_THIS(iface);
3245 TRACE("(%p)->(%p)\n", This, pstrRawUri);
3247 /* Just forward the call to GetPropertyBSTR. */
3248 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_RAW_URI, pstrRawUri, 0);
3251 static HRESULT WINAPI Uri_GetSchemeName(IUri *iface, BSTR *pstrSchemeName)
3253 Uri *This = URI_THIS(iface);
3254 TRACE("(%p)->(%p)\n", This, pstrSchemeName);
3255 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_SCHEME_NAME, pstrSchemeName, 0);
3258 static HRESULT WINAPI Uri_GetUserInfo(IUri *iface, BSTR *pstrUserInfo)
3260 TRACE("(%p)->(%p)\n", iface, pstrUserInfo);
3261 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_USER_INFO, pstrUserInfo, 0);
3264 static HRESULT WINAPI Uri_GetUserName(IUri *iface, BSTR *pstrUserName)
3266 TRACE("(%p)->(%p)\n", iface, pstrUserName);
3267 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_USER_NAME, pstrUserName, 0);
3270 static HRESULT WINAPI Uri_GetHostType(IUri *iface, DWORD *pdwHostType)
3272 TRACE("(%p)->(%p)\n", iface, pdwHostType);
3273 return Uri_GetPropertyDWORD(iface, Uri_PROPERTY_HOST_TYPE, pdwHostType, 0);
3276 static HRESULT WINAPI Uri_GetPort(IUri *iface, DWORD *pdwPort)
3278 TRACE("(%p)->(%p)\n", iface, pdwPort);
3279 return Uri_GetPropertyDWORD(iface, Uri_PROPERTY_PORT, pdwPort, 0);
3282 static HRESULT WINAPI Uri_GetScheme(IUri *iface, DWORD *pdwScheme)
3284 Uri *This = URI_THIS(iface);
3285 TRACE("(%p)->(%p)\n", This, pdwScheme);
3286 return Uri_GetPropertyDWORD(iface, Uri_PROPERTY_SCHEME, pdwScheme, 0);
3289 static HRESULT WINAPI Uri_GetZone(IUri *iface, DWORD *pdwZone)
3291 TRACE("(%p)->(%p)\n", iface, pdwZone);
3292 return Uri_GetPropertyDWORD(iface, Uri_PROPERTY_ZONE,pdwZone, 0);
3295 static HRESULT WINAPI Uri_GetProperties(IUri *iface, DWORD *pdwProperties)
3297 Uri *This = URI_THIS(iface);
3298 FIXME("(%p)->(%p)\n", This, pdwProperties);
3301 return E_INVALIDARG;
3306 static HRESULT WINAPI Uri_IsEqual(IUri *iface, IUri *pUri, BOOL *pfEqual)
3308 Uri *This = URI_THIS(iface);
3309 TRACE("(%p)->(%p %p)\n", This, pUri, pfEqual);
3317 /* For some reason Windows returns S_OK here... */
3321 FIXME("(%p)->(%p %p)\n", This, pUri, pfEqual);
3327 static const IUriVtbl UriVtbl = {
3331 Uri_GetPropertyBSTR,
3332 Uri_GetPropertyLength,
3333 Uri_GetPropertyDWORD,
3344 Uri_GetPathAndQuery,
3358 /***********************************************************************
3359 * CreateUri (urlmon.@)
3361 HRESULT WINAPI CreateUri(LPCWSTR pwzURI, DWORD dwFlags, DWORD_PTR dwReserved, IUri **ppURI)
3367 TRACE("(%s %x %x %p)\n", debugstr_w(pwzURI), dwFlags, (DWORD)dwReserved, ppURI);
3370 return E_INVALIDARG;
3374 return E_INVALIDARG;
3377 ret = heap_alloc(sizeof(Uri));
3379 return E_OUTOFMEMORY;
3381 ret->lpIUriVtbl = &UriVtbl;
3384 /* Create a copy of pwzURI and store it as the raw_uri. */
3385 ret->raw_uri = SysAllocString(pwzURI);
3388 return E_OUTOFMEMORY;
3391 memset(&data, 0, sizeof(parse_data));
3392 data.uri = ret->raw_uri;
3394 /* Validate and parse the URI into it's components. */
3395 if(!parse_uri(&data, dwFlags)) {
3396 /* Encountered an unsupported or invalid URI */
3397 SysFreeString(ret->raw_uri);
3400 return E_INVALIDARG;
3403 /* Canonicalize the URI. */
3404 hr = canonicalize_uri(&data, ret, dwFlags);
3406 SysFreeString(ret->raw_uri);
3416 #define URIBUILDER_THIS(iface) DEFINE_THIS(UriBuilder, IUriBuilder, iface)
3418 static HRESULT WINAPI UriBuilder_QueryInterface(IUriBuilder *iface, REFIID riid, void **ppv)
3420 UriBuilder *This = URIBUILDER_THIS(iface);
3422 if(IsEqualGUID(&IID_IUnknown, riid)) {
3423 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
3424 *ppv = URIBUILDER(This);
3425 }else if(IsEqualGUID(&IID_IUriBuilder, riid)) {
3426 TRACE("(%p)->(IID_IUri %p)\n", This, ppv);
3427 *ppv = URIBUILDER(This);
3429 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
3431 return E_NOINTERFACE;
3434 IUnknown_AddRef((IUnknown*)*ppv);
3438 static ULONG WINAPI UriBuilder_AddRef(IUriBuilder *iface)
3440 UriBuilder *This = URIBUILDER_THIS(iface);
3441 LONG ref = InterlockedIncrement(&This->ref);
3443 TRACE("(%p) ref=%d\n", This, ref);
3448 static ULONG WINAPI UriBuilder_Release(IUriBuilder *iface)
3450 UriBuilder *This = URIBUILDER_THIS(iface);
3451 LONG ref = InterlockedDecrement(&This->ref);
3453 TRACE("(%p) ref=%d\n", This, ref);
3461 static HRESULT WINAPI UriBuilder_CreateUriSimple(IUriBuilder *iface,
3462 DWORD dwAllowEncodingPropertyMask,
3463 DWORD_PTR dwReserved,
3466 UriBuilder *This = URIBUILDER_THIS(iface);
3467 FIXME("(%p)->(%d %d %p)\n", This, dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
3471 static HRESULT WINAPI UriBuilder_CreateUri(IUriBuilder *iface,
3472 DWORD dwCreateFlags,
3473 DWORD dwAllowEncodingPropertyMask,
3474 DWORD_PTR dwReserved,
3477 UriBuilder *This = URIBUILDER_THIS(iface);
3478 FIXME("(%p)->(0x%08x %d %d %p)\n", This, dwCreateFlags, dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
3482 static HRESULT WINAPI UriBuilder_CreateUriWithFlags(IUriBuilder *iface,
3483 DWORD dwCreateFlags,
3484 DWORD dwUriBuilderFlags,
3485 DWORD dwAllowEncodingPropertyMask,
3486 DWORD_PTR dwReserved,
3489 UriBuilder *This = URIBUILDER_THIS(iface);
3490 FIXME("(%p)->(0x%08x 0x%08x %d %d %p)\n", This, dwCreateFlags, dwUriBuilderFlags,
3491 dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
3495 static HRESULT WINAPI UriBuilder_GetIUri(IUriBuilder *iface, IUri **ppIUri)
3497 UriBuilder *This = URIBUILDER_THIS(iface);
3498 FIXME("(%p)->(%p)\n", This, ppIUri);
3502 static HRESULT WINAPI UriBuilder_SetIUri(IUriBuilder *iface, IUri *pIUri)
3504 UriBuilder *This = URIBUILDER_THIS(iface);
3505 FIXME("(%p)->(%p)\n", This, pIUri);
3509 static HRESULT WINAPI UriBuilder_GetFragment(IUriBuilder *iface, DWORD *pcchFragment, LPCWSTR *ppwzFragment)
3511 UriBuilder *This = URIBUILDER_THIS(iface);
3512 FIXME("(%p)->(%p %p)\n", This, pcchFragment, ppwzFragment);
3516 static HRESULT WINAPI UriBuilder_GetHost(IUriBuilder *iface, DWORD *pcchHost, LPCWSTR *ppwzHost)
3518 UriBuilder *This = URIBUILDER_THIS(iface);
3519 FIXME("(%p)->(%p %p)\n", This, pcchHost, ppwzHost);
3523 static HRESULT WINAPI UriBuilder_GetPassword(IUriBuilder *iface, DWORD *pcchPassword, LPCWSTR *ppwzPassword)
3525 UriBuilder *This = URIBUILDER_THIS(iface);
3526 FIXME("(%p)->(%p %p)\n", This, pcchPassword, ppwzPassword);
3530 static HRESULT WINAPI UriBuilder_GetPath(IUriBuilder *iface, DWORD *pcchPath, LPCWSTR *ppwzPath)
3532 UriBuilder *This = URIBUILDER_THIS(iface);
3533 FIXME("(%p)->(%p %p)\n", This, pcchPath, ppwzPath);
3537 static HRESULT WINAPI UriBuilder_GetPort(IUriBuilder *iface, BOOL *pfHasPort, DWORD *pdwPort)
3539 UriBuilder *This = URIBUILDER_THIS(iface);
3540 FIXME("(%p)->(%p %p)\n", This, pfHasPort, pdwPort);
3544 static HRESULT WINAPI UriBuilder_GetQuery(IUriBuilder *iface, DWORD *pcchQuery, LPCWSTR *ppwzQuery)
3546 UriBuilder *This = URIBUILDER_THIS(iface);
3547 FIXME("(%p)->(%p %p)\n", This, pcchQuery, ppwzQuery);
3551 static HRESULT WINAPI UriBuilder_GetSchemeName(IUriBuilder *iface, DWORD *pcchSchemeName, LPCWSTR *ppwzSchemeName)
3553 UriBuilder *This = URIBUILDER_THIS(iface);
3554 FIXME("(%p)->(%p %p)\n", This, pcchSchemeName, ppwzSchemeName);
3558 static HRESULT WINAPI UriBuilder_GetUserName(IUriBuilder *iface, DWORD *pcchUserName, LPCWSTR *ppwzUserName)
3560 UriBuilder *This = URIBUILDER_THIS(iface);
3561 FIXME("(%p)->(%p %p)\n", This, pcchUserName, ppwzUserName);
3565 static HRESULT WINAPI UriBuilder_SetFragment(IUriBuilder *iface, LPCWSTR pwzNewValue)
3567 UriBuilder *This = URIBUILDER_THIS(iface);
3568 FIXME("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
3572 static HRESULT WINAPI UriBuilder_SetHost(IUriBuilder *iface, LPCWSTR pwzNewValue)
3574 UriBuilder *This = URIBUILDER_THIS(iface);
3575 FIXME("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
3579 static HRESULT WINAPI UriBuilder_SetPassword(IUriBuilder *iface, LPCWSTR pwzNewValue)
3581 UriBuilder *This = URIBUILDER_THIS(iface);
3582 FIXME("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
3586 static HRESULT WINAPI UriBuilder_SetPath(IUriBuilder *iface, LPCWSTR pwzNewValue)
3588 UriBuilder *This = URIBUILDER_THIS(iface);
3589 FIXME("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
3593 static HRESULT WINAPI UriBuilder_SetPort(IUriBuilder *iface, BOOL fHasPort, DWORD dwNewValue)
3595 UriBuilder *This = URIBUILDER_THIS(iface);
3596 FIXME("(%p)->(%d %d)\n", This, fHasPort, dwNewValue);
3600 static HRESULT WINAPI UriBuilder_SetQuery(IUriBuilder *iface, LPCWSTR pwzNewValue)
3602 UriBuilder *This = URIBUILDER_THIS(iface);
3603 FIXME("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
3607 static HRESULT WINAPI UriBuilder_SetSchemeName(IUriBuilder *iface, LPCWSTR pwzNewValue)
3609 UriBuilder *This = URIBUILDER_THIS(iface);
3610 FIXME("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
3614 static HRESULT WINAPI UriBuilder_SetUserName(IUriBuilder *iface, LPCWSTR pwzNewValue)
3616 UriBuilder *This = URIBUILDER_THIS(iface);
3617 FIXME("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
3621 static HRESULT WINAPI UriBuilder_RemoveProperties(IUriBuilder *iface, DWORD dwPropertyMask)
3623 UriBuilder *This = URIBUILDER_THIS(iface);
3624 FIXME("(%p)->(0x%08x)\n", This, dwPropertyMask);
3628 static HRESULT WINAPI UriBuilder_HasBeenModified(IUriBuilder *iface, BOOL *pfModified)
3630 UriBuilder *This = URIBUILDER_THIS(iface);
3631 FIXME("(%p)->(%p)\n", This, pfModified);
3635 #undef URIBUILDER_THIS
3637 static const IUriBuilderVtbl UriBuilderVtbl = {
3638 UriBuilder_QueryInterface,
3641 UriBuilder_CreateUriSimple,
3642 UriBuilder_CreateUri,
3643 UriBuilder_CreateUriWithFlags,
3646 UriBuilder_GetFragment,
3648 UriBuilder_GetPassword,
3651 UriBuilder_GetQuery,
3652 UriBuilder_GetSchemeName,
3653 UriBuilder_GetUserName,
3654 UriBuilder_SetFragment,
3656 UriBuilder_SetPassword,
3659 UriBuilder_SetQuery,
3660 UriBuilder_SetSchemeName,
3661 UriBuilder_SetUserName,
3662 UriBuilder_RemoveProperties,
3663 UriBuilder_HasBeenModified,
3666 /***********************************************************************
3667 * CreateIUriBuilder (urlmon.@)
3669 HRESULT WINAPI CreateIUriBuilder(IUri *pIUri, DWORD dwFlags, DWORD_PTR dwReserved, IUriBuilder **ppIUriBuilder)
3673 TRACE("(%p %x %x %p)\n", pIUri, dwFlags, (DWORD)dwReserved, ppIUriBuilder);
3675 ret = heap_alloc(sizeof(UriBuilder));
3677 return E_OUTOFMEMORY;
3679 ret->lpIUriBuilderVtbl = &UriBuilderVtbl;
3682 *ppIUriBuilder = URIBUILDER(ret);