urlmon: Implemented a fragment parser.
[wine.git] / dlls / urlmon / uri.c
blobecf69223c457cfd61ad8c95a1f1384bfa8936f96
1 /*
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
24 #include "shlwapi.h"
26 #define UINT_MAX 0xffffffff
27 #define USHORT_MAX 0xffff
29 WINE_DEFAULT_DEBUG_CHANNEL(urlmon);
31 typedef struct {
32 const IUriVtbl *lpIUriVtbl;
33 LONG ref;
35 BSTR raw_uri;
37 /* Information about the canonicalized URI's buffer. */
38 WCHAR *canon_uri;
39 DWORD canon_size;
40 DWORD canon_len;
42 INT scheme_start;
43 DWORD scheme_len;
44 URL_SCHEME scheme_type;
46 INT userinfo_start;
47 DWORD userinfo_len;
48 INT userinfo_split;
50 INT host_start;
51 DWORD host_len;
52 Uri_HOST_TYPE host_type;
54 USHORT port;
55 BOOL has_port;
57 INT authority_start;
58 DWORD authority_len;
60 INT domain_offset;
62 INT path_start;
63 DWORD path_len;
64 INT extension_offset;
66 INT query_start;
67 DWORD query_len;
68 } Uri;
70 typedef struct {
71 const IUriBuilderVtbl *lpIUriBuilderVtbl;
72 LONG ref;
73 } UriBuilder;
75 typedef struct {
76 const WCHAR *str;
77 DWORD len;
78 } h16;
80 typedef struct {
81 /* IPv6 addresses can hold up to 8 h16 components. */
82 h16 components[8];
83 DWORD h16_count;
85 /* An IPv6 can have 1 elision ("::"). */
86 const WCHAR *elision;
88 /* An IPv6 can contain 1 IPv4 address as the last 32bits of the address. */
89 const WCHAR *ipv4;
90 DWORD ipv4_len;
92 INT components_size;
93 INT elision_size;
94 } ipv6_address;
96 typedef struct {
97 BSTR uri;
99 BOOL is_relative;
100 BOOL is_opaque;
101 BOOL has_implicit_scheme;
102 BOOL has_implicit_ip;
103 UINT implicit_ipv4;
105 const WCHAR *scheme;
106 DWORD scheme_len;
107 URL_SCHEME scheme_type;
109 const WCHAR *userinfo;
110 DWORD userinfo_len;
111 INT userinfo_split;
113 const WCHAR *host;
114 DWORD host_len;
115 Uri_HOST_TYPE host_type;
117 BOOL has_ipv6;
118 ipv6_address ipv6_address;
120 const WCHAR *port;
121 DWORD port_len;
122 USHORT port_value;
124 const WCHAR *path;
125 DWORD path_len;
127 const WCHAR *query;
128 DWORD query_len;
130 const WCHAR *fragment;
131 DWORD fragment_len;
132 } parse_data;
134 static const CHAR hexDigits[] = "0123456789ABCDEF";
136 /* List of scheme types/scheme names that are recognized by the IUri interface as of IE 7. */
137 static const struct {
138 URL_SCHEME scheme;
139 WCHAR scheme_name[16];
140 } recognized_schemes[] = {
141 {URL_SCHEME_FTP, {'f','t','p',0}},
142 {URL_SCHEME_HTTP, {'h','t','t','p',0}},
143 {URL_SCHEME_GOPHER, {'g','o','p','h','e','r',0}},
144 {URL_SCHEME_MAILTO, {'m','a','i','l','t','o',0}},
145 {URL_SCHEME_NEWS, {'n','e','w','s',0}},
146 {URL_SCHEME_NNTP, {'n','n','t','p',0}},
147 {URL_SCHEME_TELNET, {'t','e','l','n','e','t',0}},
148 {URL_SCHEME_WAIS, {'w','a','i','s',0}},
149 {URL_SCHEME_FILE, {'f','i','l','e',0}},
150 {URL_SCHEME_MK, {'m','k',0}},
151 {URL_SCHEME_HTTPS, {'h','t','t','p','s',0}},
152 {URL_SCHEME_SHELL, {'s','h','e','l','l',0}},
153 {URL_SCHEME_SNEWS, {'s','n','e','w','s',0}},
154 {URL_SCHEME_LOCAL, {'l','o','c','a','l',0}},
155 {URL_SCHEME_JAVASCRIPT, {'j','a','v','a','s','c','r','i','p','t',0}},
156 {URL_SCHEME_VBSCRIPT, {'v','b','s','c','r','i','p','t',0}},
157 {URL_SCHEME_ABOUT, {'a','b','o','u','t',0}},
158 {URL_SCHEME_RES, {'r','e','s',0}},
159 {URL_SCHEME_MSSHELLROOTED, {'m','s','-','s','h','e','l','l','-','r','o','o','t','e','d',0}},
160 {URL_SCHEME_MSSHELLIDLIST, {'m','s','-','s','h','e','l','l','-','i','d','l','i','s','t',0}},
161 {URL_SCHEME_MSHELP, {'h','c','p',0}},
162 {URL_SCHEME_WILDCARD, {'*',0}}
165 /* List of default ports Windows recognizes. */
166 static const struct {
167 URL_SCHEME scheme;
168 USHORT port;
169 } default_ports[] = {
170 {URL_SCHEME_FTP, 21},
171 {URL_SCHEME_HTTP, 80},
172 {URL_SCHEME_GOPHER, 70},
173 {URL_SCHEME_NNTP, 119},
174 {URL_SCHEME_TELNET, 23},
175 {URL_SCHEME_WAIS, 210},
176 {URL_SCHEME_HTTPS, 443},
179 /* List of 3 character top level domain names Windows seems to recognize.
180 * There might be more, but, these are the only ones I've found so far.
182 static const struct {
183 WCHAR tld_name[4];
184 } recognized_tlds[] = {
185 {{'c','o','m',0}},
186 {{'e','d','u',0}},
187 {{'g','o','v',0}},
188 {{'i','n','t',0}},
189 {{'m','i','l',0}},
190 {{'n','e','t',0}},
191 {{'o','r','g',0}}
194 static inline BOOL is_alpha(WCHAR val) {
195 return ((val >= 'a' && val <= 'z') || (val >= 'A' && val <= 'Z'));
198 static inline BOOL is_num(WCHAR val) {
199 return (val >= '0' && val <= '9');
202 /* A URI is implicitly a file path if it begins with
203 * a drive letter (eg X:) or starts with "\\" (UNC path).
205 static inline BOOL is_implicit_file_path(const WCHAR *str) {
206 if(is_alpha(str[0]) && str[1] == ':')
207 return TRUE;
208 else if(str[0] == '\\' && str[1] == '\\')
209 return TRUE;
211 return FALSE;
214 /* Checks if the URI is a hierarchical URI. A hierarchical
215 * URI is one that has "//" after the scheme.
217 static BOOL check_hierarchical(const WCHAR **ptr) {
218 const WCHAR *start = *ptr;
220 if(**ptr != '/')
221 return FALSE;
223 ++(*ptr);
224 if(**ptr != '/') {
225 *ptr = start;
226 return FALSE;
229 ++(*ptr);
230 return TRUE;
233 /* unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~" */
234 static inline BOOL is_unreserved(WCHAR val) {
235 return (is_alpha(val) || is_num(val) || val == '-' || val == '.' ||
236 val == '_' || val == '~');
239 /* sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
240 * / "*" / "+" / "," / ";" / "="
242 static inline BOOL is_subdelim(WCHAR val) {
243 return (val == '!' || val == '$' || val == '&' ||
244 val == '\'' || val == '(' || val == ')' ||
245 val == '*' || val == '+' || val == ',' ||
246 val == ';' || val == '=');
249 /* gen-delims = ":" / "/" / "?" / "#" / "[" / "]" / "@" */
250 static inline BOOL is_gendelim(WCHAR val) {
251 return (val == ':' || val == '/' || val == '?' ||
252 val == '#' || val == '[' || val == ']' ||
253 val == '@');
256 /* Characters that delimit the end of the authority
257 * section of a URI. Sometimes a '\\' is considered
258 * an authority delimeter.
260 static inline BOOL is_auth_delim(WCHAR val, BOOL acceptSlash) {
261 return (val == '#' || val == '/' || val == '?' ||
262 val == '\0' || (acceptSlash && val == '\\'));
265 /* reserved = gen-delims / sub-delims */
266 static inline BOOL is_reserved(WCHAR val) {
267 return (is_subdelim(val) || is_gendelim(val));
270 static inline BOOL is_hexdigit(WCHAR val) {
271 return ((val >= 'a' && val <= 'f') ||
272 (val >= 'A' && val <= 'F') ||
273 (val >= '0' && val <= '9'));
276 static inline BOOL is_path_delim(WCHAR val) {
277 return (!val || val == '#' || val == '?');
280 /* Computes the size of the given IPv6 address.
281 * Each h16 component is 16bits, if there is an IPv4 address, it's
282 * 32bits. If there's an elision it can be 16bits to 128bits, depending
283 * on the number of other components.
285 * Modeled after google-url's CheckIPv6ComponentsSize function
287 static void compute_ipv6_comps_size(ipv6_address *address) {
288 address->components_size = address->h16_count * 2;
290 if(address->ipv4)
291 /* IPv4 address is 4 bytes. */
292 address->components_size += 4;
294 if(address->elision) {
295 /* An elision can be anywhere from 2 bytes up to 16 bytes.
296 * It size depends on the size of the h16 and IPv4 components.
298 address->elision_size = 16 - address->components_size;
299 if(address->elision_size < 2)
300 address->elision_size = 2;
301 } else
302 address->elision_size = 0;
305 /* Taken from dlls/jscript/lex.c */
306 static int hex_to_int(WCHAR val) {
307 if(val >= '0' && val <= '9')
308 return val - '0';
309 else if(val >= 'a' && val <= 'f')
310 return val - 'a' + 10;
311 else if(val >= 'A' && val <= 'F')
312 return val - 'A' + 10;
314 return -1;
317 /* Helper function for converting a percent encoded string
318 * representation of a WCHAR value into its actual WCHAR value. If
319 * the two characters following the '%' aren't valid hex values then
320 * this function returns the NULL character.
322 * Eg.
323 * "%2E" will result in '.' being returned by this function.
325 static WCHAR decode_pct_val(const WCHAR *ptr) {
326 WCHAR ret = '\0';
328 if(*ptr == '%' && is_hexdigit(*(ptr + 1)) && is_hexdigit(*(ptr + 2))) {
329 INT a = hex_to_int(*(ptr + 1));
330 INT b = hex_to_int(*(ptr + 2));
332 ret = a << 4;
333 ret += b;
336 return ret;
339 /* Helper function for percent encoding a given character
340 * and storing the encoded value into a given buffer (dest).
342 * It's up to the calling function to ensure that there is
343 * at least enough space in 'dest' for the percent encoded
344 * value to be stored (so dest + 3 spaces available).
346 static inline void pct_encode_val(WCHAR val, WCHAR *dest) {
347 dest[0] = '%';
348 dest[1] = hexDigits[(val >> 4) & 0xf];
349 dest[2] = hexDigits[val & 0xf];
352 /* Scans the range of characters [str, end] and returns the last occurence
353 * of 'ch' or returns NULL.
355 static const WCHAR *str_last_of(const WCHAR *str, const WCHAR *end, WCHAR ch) {
356 const WCHAR *ptr = end;
358 while(ptr >= str) {
359 if(*ptr == ch)
360 return ptr;
361 --ptr;
364 return NULL;
367 /* Attempts to parse the domain name from the host.
369 * This function also includes the Top-level Domain (TLD) name
370 * of the host when it tries to find the domain name. If it finds
371 * a valid domain name it will assign 'domain_start' the offset
372 * into 'host' where the domain name starts.
374 * It's implied that if a domain name its range is implied to be
375 * [host+domain_start, host+host_len).
377 static void find_domain_name(const WCHAR *host, DWORD host_len,
378 INT *domain_start) {
379 const WCHAR *last_tld, *sec_last_tld, *end;
381 end = host+host_len-1;
383 *domain_start = -1;
385 /* There has to be at least enough room for a '.' followed by a
386 * 3 character TLD for a domain to even exist in the host name.
388 if(host_len < 4)
389 return;
391 last_tld = str_last_of(host, end, '.');
392 if(!last_tld)
393 /* http://hostname -> has no domain name. */
394 return;
396 sec_last_tld = str_last_of(host, last_tld-1, '.');
397 if(!sec_last_tld) {
398 /* If the '.' is at the beginning of the host there
399 * has to be at least 3 characters in the TLD for it
400 * to be valid.
401 * Ex: .com -> .com as the domain name.
402 * .co -> has no domain name.
404 if(last_tld-host == 0) {
405 if(end-(last_tld-1) < 3)
406 return;
407 } else if(last_tld-host == 3) {
408 DWORD i;
410 /* If there's three characters in front of last_tld and
411 * they are on the list of recognized TLDs, then this
412 * host doesn't have a domain (since the host only contains
413 * a TLD name.
414 * Ex: edu.uk -> has no domain name.
415 * foo.uk -> foo.uk as the domain name.
417 for(i = 0; i < sizeof(recognized_tlds)/sizeof(recognized_tlds[0]); ++i) {
418 if(!StrCmpNIW(host, recognized_tlds[i].tld_name, 3))
419 return;
421 } else if(last_tld-host < 3)
422 /* Anything less then 3 characters is considered part
423 * of the TLD name.
424 * Ex: ak.uk -> Has no domain name.
426 return;
428 /* Otherwise the domain name is the whole host name. */
429 *domain_start = 0;
430 } else if(end+1-last_tld > 3) {
431 /* If the last_tld has more then 3 characters then it's automatically
432 * considered the TLD of the domain name.
433 * Ex: www.winehq.org.uk.test -> uk.test as the domain name.
435 *domain_start = (sec_last_tld+1)-host;
436 } else if(last_tld - (sec_last_tld+1) < 4) {
437 DWORD i;
438 /* If the sec_last_tld is 3 characters long it HAS to be on the list of
439 * recognized to still be considered part of the TLD name, otherwise
440 * its considered the domain name.
441 * Ex: www.google.com.uk -> google.com.uk as the domain name.
442 * www.google.foo.uk -> foo.uk as the domain name.
444 if(last_tld - (sec_last_tld+1) == 3) {
445 for(i = 0; i < sizeof(recognized_tlds)/sizeof(recognized_tlds[0]); ++i) {
446 if(!StrCmpNIW(sec_last_tld+1, recognized_tlds[i].tld_name, 3)) {
447 const WCHAR *domain = str_last_of(host, sec_last_tld-1, '.');
449 if(!domain)
450 *domain_start = 0;
451 else
452 *domain_start = (domain+1) - host;
453 TRACE("Found domain name %s\n", debugstr_wn(host+*domain_start,
454 (host+host_len)-(host+*domain_start)));
455 return;
459 *domain_start = (sec_last_tld+1)-host;
460 } else {
461 /* Since the sec_last_tld is less then 3 characters it's considered
462 * part of the TLD.
463 * Ex: www.google.fo.uk -> google.fo.uk as the domain name.
465 const WCHAR *domain = str_last_of(host, sec_last_tld-1, '.');
467 if(!domain)
468 *domain_start = 0;
469 else
470 *domain_start = (domain+1) - host;
472 } else {
473 /* The second to last TLD has more then 3 characters making it
474 * the domain name.
475 * Ex: www.google.test.us -> test.us as the domain name.
477 *domain_start = (sec_last_tld+1)-host;
480 TRACE("Found domain name %s\n", debugstr_wn(host+*domain_start,
481 (host+host_len)-(host+*domain_start)));
484 /* Removes the dot segments from a heirarchical URIs path component. This
485 * function performs the removal in place.
487 * This is a modified version of Qt's QUrl function "removeDotsFromPath".
489 * This function returns the new length of the path string.
491 static DWORD remove_dot_segments(WCHAR *path, DWORD path_len) {
492 WCHAR *out = path;
493 const WCHAR *in = out;
494 const WCHAR *end = out + path_len;
495 DWORD len;
497 while(in < end) {
498 /* A. if the input buffer begins with a prefix of "/./" or "/.",
499 * where "." is a complete path segment, then replace that
500 * prefix with "/" in the input buffer; otherwise,
502 if(in <= end - 3 && in[0] == '/' && in[1] == '.' && in[2] == '/') {
503 in += 2;
504 continue;
505 } else if(in == end - 2 && in[0] == '/' && in[1] == '.') {
506 *out++ = '/';
507 in += 2;
508 break;
511 /* B. if the input buffer begins with a prefix of "/../" or "/..",
512 * where ".." is a complete path segment, then replace that
513 * prefix with "/" in the input buffer and remove the last
514 * segment and its preceding "/" (if any) from the output
515 * buffer; otherwise,
517 if(in <= end - 4 && in[0] == '/' && in[1] == '.' && in[2] == '.' && in[3] == '/') {
518 while(out > path && *(--out) != '/');
520 in += 3;
521 continue;
522 } else if(in == end - 3 && in[0] == '/' && in[1] == '.' && in[2] == '.') {
523 while(out > path && *(--out) != '/');
525 if(*out == '/')
526 ++out;
528 in += 3;
529 break;
532 /* C. move the first path segment in the input buffer to the end of
533 * the output buffer, including the initial "/" character (if
534 * any) and any subsequent characters up to, but not including,
535 * the next "/" character or the end of the input buffer.
537 *out++ = *in++;
538 while(in < end && *in != '/')
539 *out++ = *in++;
542 len = out - path;
543 TRACE("(%p %d): Path after dot segments removed %s len=%d\n", path, path_len,
544 debugstr_wn(path, len), len);
545 return len;
548 /* Attempts to find the file extension in a given path. */
549 static INT find_file_extension(const WCHAR *path, DWORD path_len) {
550 const WCHAR *end;
552 for(end = path+path_len-1; end >= path && *end != '/' && *end != '\\'; --end) {
553 if(*end == '.')
554 return end-path;
557 return -1;
560 /* Computes the location where the elision should occur in the IPv6
561 * address using the numerical values of each component stored in
562 * 'values'. If the address shouldn't contain an elision then 'index'
563 * is assigned -1 as it's value. Otherwise 'index' will contain the
564 * starting index (into values) where the elision should be, and 'count'
565 * will contain the number of cells the elision covers.
567 * NOTES:
568 * Windows will expand an elision if the elision only represents 1 h16
569 * component of the URI.
571 * Ex: [1::2:3:4:5:6:7] -> [1:0:2:3:4:5:6:7]
573 * If the IPv6 address contains an IPv4 address, the IPv4 address is also
574 * considered for being included as part of an elision if all it's components
575 * are zeros.
577 * Ex: [1:2:3:4:5:6:0.0.0.0] -> [1:2:3:4:5:6::]
579 static void compute_elision_location(const ipv6_address *address, const USHORT values[8],
580 INT *index, DWORD *count) {
581 DWORD i, max_len, cur_len;
582 INT max_index, cur_index;
584 max_len = cur_len = 0;
585 max_index = cur_index = -1;
586 for(i = 0; i < 8; ++i) {
587 BOOL check_ipv4 = (address->ipv4 && i == 6);
588 BOOL is_end = (check_ipv4 || i == 7);
590 if(check_ipv4) {
591 /* Check if the IPv4 address contains only zeros. */
592 if(values[i] == 0 && values[i+1] == 0) {
593 if(cur_index == -1)
594 cur_index = i;
596 cur_len += 2;
597 ++i;
599 } else if(values[i] == 0) {
600 if(cur_index == -1)
601 cur_index = i;
603 ++cur_len;
606 if(is_end || values[i] != 0) {
607 /* We only consider it for an elision if it's
608 * more then 1 component long.
610 if(cur_len > 1 && cur_len > max_len) {
611 /* Found the new elision location. */
612 max_len = cur_len;
613 max_index = cur_index;
616 /* Reset the current range for the next range of zeros. */
617 cur_index = -1;
618 cur_len = 0;
622 *index = max_index;
623 *count = max_len;
626 /* Converts the specified IPv4 address into an uint value.
628 * This function assumes that the IPv4 address has already been validated.
630 static UINT ipv4toui(const WCHAR *ip, DWORD len) {
631 UINT ret = 0;
632 DWORD comp_value = 0;
633 const WCHAR *ptr;
635 for(ptr = ip; ptr < ip+len; ++ptr) {
636 if(*ptr == '.') {
637 ret <<= 8;
638 ret += comp_value;
639 comp_value = 0;
640 } else
641 comp_value = comp_value*10 + (*ptr-'0');
644 ret <<= 8;
645 ret += comp_value;
647 return ret;
650 /* Converts an IPv4 address in numerical form into it's fully qualified
651 * string form. This function returns the number of characters written
652 * to 'dest'. If 'dest' is NULL this function will return the number of
653 * characters that would have been written.
655 * It's up to the caller to ensure there's enough space in 'dest' for the
656 * address.
658 static DWORD ui2ipv4(WCHAR *dest, UINT address) {
659 static const WCHAR formatW[] =
660 {'%','u','.','%','u','.','%','u','.','%','u',0};
661 DWORD ret = 0;
662 UCHAR digits[4];
664 digits[0] = (address >> 24) & 0xff;
665 digits[1] = (address >> 16) & 0xff;
666 digits[2] = (address >> 8) & 0xff;
667 digits[3] = address & 0xff;
669 if(!dest) {
670 WCHAR tmp[16];
671 ret = sprintfW(tmp, formatW, digits[0], digits[1], digits[2], digits[3]);
672 } else
673 ret = sprintfW(dest, formatW, digits[0], digits[1], digits[2], digits[3]);
675 return ret;
678 /* Converts an h16 component (from an IPv6 address) into it's
679 * numerical value.
681 * This function assumes that the h16 component has already been validated.
683 static USHORT h16tous(h16 component) {
684 DWORD i;
685 USHORT ret = 0;
687 for(i = 0; i < component.len; ++i) {
688 ret <<= 4;
689 ret += hex_to_int(component.str[i]);
692 return ret;
695 /* Converts an IPv6 address into it's 128 bits (16 bytes) numerical value.
697 * This function assumes that the ipv6_address has already been validated.
699 static BOOL ipv6_to_number(const ipv6_address *address, USHORT number[8]) {
700 DWORD i, cur_component = 0;
701 BOOL already_passed_elision = FALSE;
703 for(i = 0; i < address->h16_count; ++i) {
704 if(address->elision) {
705 if(address->components[i].str > address->elision && !already_passed_elision) {
706 /* Means we just passed the elision and need to add it's values to
707 * 'number' before we do anything else.
709 DWORD j = 0;
710 for(j = 0; j < address->elision_size; j+=2)
711 number[cur_component++] = 0;
713 already_passed_elision = TRUE;
717 number[cur_component++] = h16tous(address->components[i]);
720 /* Case when the elision appears after the h16 components. */
721 if(!already_passed_elision && address->elision) {
722 for(i = 0; i < address->elision_size; i+=2)
723 number[cur_component++] = 0;
724 already_passed_elision = TRUE;
727 if(address->ipv4) {
728 UINT value = ipv4toui(address->ipv4, address->ipv4_len);
730 if(cur_component != 6) {
731 ERR("(%p %p): Failed sanity check with %d\n", address, number, cur_component);
732 return FALSE;
735 number[cur_component++] = (value >> 16) & 0xffff;
736 number[cur_component] = value & 0xffff;
739 return TRUE;
742 /* Checks if the characters pointed to by 'ptr' are
743 * a percent encoded data octet.
745 * pct-encoded = "%" HEXDIG HEXDIG
747 static BOOL check_pct_encoded(const WCHAR **ptr) {
748 const WCHAR *start = *ptr;
750 if(**ptr != '%')
751 return FALSE;
753 ++(*ptr);
754 if(!is_hexdigit(**ptr)) {
755 *ptr = start;
756 return FALSE;
759 ++(*ptr);
760 if(!is_hexdigit(**ptr)) {
761 *ptr = start;
762 return FALSE;
765 ++(*ptr);
766 return TRUE;
769 /* dec-octet = DIGIT ; 0-9
770 * / %x31-39 DIGIT ; 10-99
771 * / "1" 2DIGIT ; 100-199
772 * / "2" %x30-34 DIGIT ; 200-249
773 * / "25" %x30-35 ; 250-255
775 static BOOL check_dec_octet(const WCHAR **ptr) {
776 const WCHAR *c1, *c2, *c3;
778 c1 = *ptr;
779 /* A dec-octet must be at least 1 digit long. */
780 if(*c1 < '0' || *c1 > '9')
781 return FALSE;
783 ++(*ptr);
785 c2 = *ptr;
786 /* Since the 1 digit requirment was meet, it doesn't
787 * matter if this is a DIGIT value, it's considered a
788 * dec-octet.
790 if(*c2 < '0' || *c2 > '9')
791 return TRUE;
793 ++(*ptr);
795 c3 = *ptr;
796 /* Same explanation as above. */
797 if(*c3 < '0' || *c3 > '9')
798 return TRUE;
800 /* Anything > 255 isn't a valid IP dec-octet. */
801 if(*c1 >= '2' && *c2 >= '5' && *c3 >= '5') {
802 *ptr = c1;
803 return FALSE;
806 ++(*ptr);
807 return TRUE;
810 /* Checks if there is an implicit IPv4 address in the host component of the URI.
811 * The max value of an implicit IPv4 address is UINT_MAX.
813 * Ex:
814 * "234567" would be considered an implicit IPv4 address.
816 static BOOL check_implicit_ipv4(const WCHAR **ptr, UINT *val) {
817 const WCHAR *start = *ptr;
818 ULONGLONG ret = 0;
819 *val = 0;
821 while(is_num(**ptr)) {
822 ret = ret*10 + (**ptr - '0');
824 if(ret > UINT_MAX) {
825 *ptr = start;
826 return FALSE;
828 ++(*ptr);
831 if(*ptr == start)
832 return FALSE;
834 *val = ret;
835 return TRUE;
838 /* Checks if the string contains an IPv4 address.
840 * This function has a strict mode or a non-strict mode of operation
841 * When 'strict' is set to FALSE this function will return TRUE if
842 * the string contains at least 'dec-octet "." dec-octet' since partial
843 * IPv4 addresses will be normalized out into full IPv4 addresses. When
844 * 'strict' is set this function expects there to be a full IPv4 address.
846 * IPv4address = dec-octet "." dec-octet "." dec-octet "." dec-octet
848 static BOOL check_ipv4address(const WCHAR **ptr, BOOL strict) {
849 const WCHAR *start = *ptr;
851 if(!check_dec_octet(ptr)) {
852 *ptr = start;
853 return FALSE;
856 if(**ptr != '.') {
857 *ptr = start;
858 return FALSE;
861 ++(*ptr);
862 if(!check_dec_octet(ptr)) {
863 *ptr = start;
864 return FALSE;
867 if(**ptr != '.') {
868 if(strict) {
869 *ptr = start;
870 return FALSE;
871 } else
872 return TRUE;
875 ++(*ptr);
876 if(!check_dec_octet(ptr)) {
877 *ptr = start;
878 return FALSE;
881 if(**ptr != '.') {
882 if(strict) {
883 *ptr = start;
884 return FALSE;
885 } else
886 return TRUE;
889 ++(*ptr);
890 if(!check_dec_octet(ptr)) {
891 *ptr = start;
892 return FALSE;
895 /* Found a four digit ip address. */
896 return TRUE;
898 /* Tries to parse the scheme name of the URI.
900 * scheme = ALPHA *(ALPHA | NUM | '+' | '-' | '.') as defined by RFC 3896.
901 * NOTE: Windows accepts a number as the first character of a scheme.
903 static BOOL parse_scheme_name(const WCHAR **ptr, parse_data *data) {
904 const WCHAR *start = *ptr;
906 data->scheme = NULL;
907 data->scheme_len = 0;
909 while(**ptr) {
910 if(**ptr == '*' && *ptr == start) {
911 /* Might have found a wildcard scheme. If it is the next
912 * char has to be a ':' for it to be a valid URI
914 ++(*ptr);
915 break;
916 } else if(!is_num(**ptr) && !is_alpha(**ptr) && **ptr != '+' &&
917 **ptr != '-' && **ptr != '.')
918 break;
920 (*ptr)++;
923 if(*ptr == start)
924 return FALSE;
926 /* Schemes must end with a ':' */
927 if(**ptr != ':') {
928 *ptr = start;
929 return FALSE;
932 data->scheme = start;
933 data->scheme_len = *ptr - start;
935 ++(*ptr);
936 return TRUE;
939 /* Tries to deduce the corresponding URL_SCHEME for the given URI. Stores
940 * the deduced URL_SCHEME in data->scheme_type.
942 static BOOL parse_scheme_type(parse_data *data) {
943 /* If there's scheme data then see if it's a recognized scheme. */
944 if(data->scheme && data->scheme_len) {
945 DWORD i;
947 for(i = 0; i < sizeof(recognized_schemes)/sizeof(recognized_schemes[0]); ++i) {
948 if(lstrlenW(recognized_schemes[i].scheme_name) == data->scheme_len) {
949 /* Has to be a case insensitive compare. */
950 if(!StrCmpNIW(recognized_schemes[i].scheme_name, data->scheme, data->scheme_len)) {
951 data->scheme_type = recognized_schemes[i].scheme;
952 return TRUE;
957 /* If we get here it means it's not a recognized scheme. */
958 data->scheme_type = URL_SCHEME_UNKNOWN;
959 return TRUE;
960 } else if(data->is_relative) {
961 /* Relative URI's have no scheme. */
962 data->scheme_type = URL_SCHEME_UNKNOWN;
963 return TRUE;
964 } else {
965 /* Should never reach here! what happened... */
966 FIXME("(%p): Unable to determine scheme type for URI %s\n", data, debugstr_w(data->uri));
967 return FALSE;
971 /* Tries to parse (or deduce) the scheme_name of a URI. If it can't
972 * parse a scheme from the URI it will try to deduce the scheme_name and scheme_type
973 * using the flags specified in 'flags' (if any). Flags that affect how this function
974 * operates are the Uri_CREATE_ALLOW_* flags.
976 * All parsed/deduced information will be stored in 'data' when the function returns.
978 * Returns TRUE if it was able to successfully parse the information.
980 static BOOL parse_scheme(const WCHAR **ptr, parse_data *data, DWORD flags) {
981 static const WCHAR fileW[] = {'f','i','l','e',0};
982 static const WCHAR wildcardW[] = {'*',0};
984 /* First check to see if the uri could implicitly be a file path. */
985 if(is_implicit_file_path(*ptr)) {
986 if(flags & Uri_CREATE_ALLOW_IMPLICIT_FILE_SCHEME) {
987 data->scheme = fileW;
988 data->scheme_len = lstrlenW(fileW);
989 data->has_implicit_scheme = TRUE;
991 TRACE("(%p %p %x): URI is an implicit file path.\n", ptr, data, flags);
992 } else {
993 /* Window's does not consider anything that can implicitly be a file
994 * path to be a valid URI if the ALLOW_IMPLICIT_FILE_SCHEME flag is not set...
996 TRACE("(%p %p %x): URI is implicitly a file path, but, the ALLOW_IMPLICIT_FILE_SCHEME flag wasn't set.\n",
997 ptr, data, flags);
998 return FALSE;
1000 } else if(!parse_scheme_name(ptr, data)) {
1001 /* No Scheme was found, this means it could be:
1002 * a) an implicit Wildcard scheme
1003 * b) a relative URI
1004 * c) a invalid URI.
1006 if(flags & Uri_CREATE_ALLOW_IMPLICIT_WILDCARD_SCHEME) {
1007 data->scheme = wildcardW;
1008 data->scheme_len = lstrlenW(wildcardW);
1009 data->has_implicit_scheme = TRUE;
1011 TRACE("(%p %p %x): URI is an implicit wildcard scheme.\n", ptr, data, flags);
1012 } else if (flags & Uri_CREATE_ALLOW_RELATIVE) {
1013 data->is_relative = TRUE;
1014 TRACE("(%p %p %x): URI is relative.\n", ptr, data, flags);
1015 } else {
1016 TRACE("(%p %p %x): Malformed URI found. Unable to deduce scheme name.\n", ptr, data, flags);
1017 return FALSE;
1021 if(!data->is_relative)
1022 TRACE("(%p %p %x): Found scheme=%s scheme_len=%d\n", ptr, data, flags,
1023 debugstr_wn(data->scheme, data->scheme_len), data->scheme_len);
1025 if(!parse_scheme_type(data))
1026 return FALSE;
1028 TRACE("(%p %p %x): Assigned %d as the URL_SCHEME.\n", ptr, data, flags, data->scheme_type);
1029 return TRUE;
1032 /* Parses the userinfo part of the URI (if it exists). The userinfo field of
1033 * a URI can consist of "username:password@", or just "username@".
1035 * RFC def:
1036 * userinfo = *( unreserved / pct-encoded / sub-delims / ":" )
1038 * NOTES:
1039 * 1) If there is more than one ':' in the userinfo part of the URI Windows
1040 * uses the first occurence of ':' to delimit the username and password
1041 * components.
1043 * ex:
1044 * ftp://user:pass:word@winehq.org
1046 * Would yield, "user" as the username and "pass:word" as the password.
1048 * 2) Windows allows any character to appear in the "userinfo" part of
1049 * a URI, as long as it's not an authority delimeter character set.
1051 static void parse_userinfo(const WCHAR **ptr, parse_data *data, DWORD flags) {
1052 data->userinfo = *ptr;
1053 data->userinfo_split = -1;
1055 while(**ptr != '@') {
1056 if(**ptr == ':' && data->userinfo_split == -1)
1057 data->userinfo_split = *ptr - data->userinfo;
1058 else if(**ptr == '%') {
1059 /* If it's a known scheme type, it has to be a valid percent
1060 * encoded value.
1062 if(!check_pct_encoded(ptr)) {
1063 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
1064 *ptr = data->userinfo;
1065 data->userinfo = NULL;
1066 data->userinfo_split = -1;
1068 TRACE("(%p %p %x): URI contained no userinfo.\n", ptr, data, flags);
1069 return;
1071 } else
1072 continue;
1073 } else if(is_auth_delim(**ptr, data->scheme_type != URL_SCHEME_UNKNOWN))
1074 break;
1076 ++(*ptr);
1079 if(**ptr != '@') {
1080 *ptr = data->userinfo;
1081 data->userinfo = NULL;
1082 data->userinfo_split = -1;
1084 TRACE("(%p %p %x): URI contained no userinfo.\n", ptr, data, flags);
1085 return;
1088 data->userinfo_len = *ptr - data->userinfo;
1089 TRACE("(%p %p %x): Found userinfo=%s userinfo_len=%d split=%d.\n", ptr, data, flags,
1090 debugstr_wn(data->userinfo, data->userinfo_len), data->userinfo_len, data->userinfo_split);
1091 ++(*ptr);
1094 /* Attempts to parse a port from the URI.
1096 * NOTES:
1097 * Windows seems to have a cap on what the maximum value
1098 * for a port can be. The max value is USHORT_MAX.
1100 * port = *DIGIT
1102 static BOOL parse_port(const WCHAR **ptr, parse_data *data, DWORD flags) {
1103 UINT port = 0;
1104 data->port = *ptr;
1106 while(!is_auth_delim(**ptr, data->scheme_type != URL_SCHEME_UNKNOWN)) {
1107 if(!is_num(**ptr)) {
1108 *ptr = data->port;
1109 data->port = NULL;
1110 return FALSE;
1113 port = port*10 + (**ptr-'0');
1115 if(port > USHORT_MAX) {
1116 *ptr = data->port;
1117 data->port = NULL;
1118 return FALSE;
1121 ++(*ptr);
1124 data->port_value = port;
1125 data->port_len = *ptr - data->port;
1127 TRACE("(%p %p %x): Found port %s len=%d value=%u\n", ptr, data, flags,
1128 debugstr_wn(data->port, data->port_len), data->port_len, data->port_value);
1129 return TRUE;
1132 /* Attempts to parse a IPv4 address from the URI.
1134 * NOTES:
1135 * Window's normalizes IPv4 addresses, This means there's three
1136 * possibilities for the URI to contain an IPv4 address.
1137 * 1) A well formed address (ex. 192.2.2.2).
1138 * 2) A partially formed address. For example "192.0" would
1139 * normalize to "192.0.0.0" during canonicalization.
1140 * 3) An implicit IPv4 address. For example "256" would
1141 * normalize to "0.0.1.0" during canonicalization. Also
1142 * note that the maximum value for an implicit IP address
1143 * is UINT_MAX, if the value in the URI exceeds this then
1144 * it is not considered an IPv4 address.
1146 static BOOL parse_ipv4address(const WCHAR **ptr, parse_data *data, DWORD flags) {
1147 const BOOL is_unknown = data->scheme_type == URL_SCHEME_UNKNOWN;
1148 data->host = *ptr;
1150 if(!check_ipv4address(ptr, FALSE)) {
1151 if(!check_implicit_ipv4(ptr, &data->implicit_ipv4)) {
1152 TRACE("(%p %p %x): URI didn't contain anything looking like an IPv4 address.\n",
1153 ptr, data, flags);
1154 *ptr = data->host;
1155 data->host = NULL;
1156 return FALSE;
1157 } else
1158 data->has_implicit_ip = TRUE;
1161 /* Check if what we found is the only part of the host name (if it isn't
1162 * we don't have an IPv4 address).
1164 if(**ptr == ':') {
1165 ++(*ptr);
1166 if(!parse_port(ptr, data, flags)) {
1167 *ptr = data->host;
1168 data->host = NULL;
1169 return FALSE;
1171 } else if(!is_auth_delim(**ptr, !is_unknown)) {
1172 /* Found more data which belongs the host, so this isn't an IPv4. */
1173 *ptr = data->host;
1174 data->host = NULL;
1175 data->has_implicit_ip = FALSE;
1176 return FALSE;
1179 data->host_len = *ptr - data->host;
1180 data->host_type = Uri_HOST_IPV4;
1182 TRACE("(%p %p %x): IPv4 address found. host=%s host_len=%d host_type=%d\n",
1183 ptr, data, flags, debugstr_wn(data->host, data->host_len),
1184 data->host_len, data->host_type);
1185 return TRUE;
1188 /* Attempts to parse the reg-name from the URI.
1190 * Because of the way Windows handles ':' this function also
1191 * handles parsing the port.
1193 * reg-name = *( unreserved / pct-encoded / sub-delims )
1195 * NOTE:
1196 * Windows allows everything, but, the characters in "auth_delims" and ':'
1197 * to appear in a reg-name, unless it's an unknown scheme type then ':' is
1198 * allowed to appear (even if a valid port isn't after it).
1200 * Windows doesn't like host names which start with '[' and end with ']'
1201 * and don't contain a valid IP literal address in between them.
1203 * On Windows if an '[' is encountered in the host name the ':' no longer
1204 * counts as a delimiter until you reach the next ']' or an "authority delimeter".
1206 * A reg-name CAN be empty.
1208 static BOOL parse_reg_name(const WCHAR **ptr, parse_data *data, DWORD flags) {
1209 const BOOL has_start_bracket = **ptr == '[';
1210 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
1211 BOOL inside_brackets = has_start_bracket;
1212 BOOL ignore_col = FALSE;
1214 /* We have to be careful with file schemes. */
1215 if(data->scheme_type == URL_SCHEME_FILE) {
1216 /* This is because an implicit file scheme could be "C:\\test" and it
1217 * would trick this function into thinking the host is "C", when after
1218 * canonicalization the host would end up being an empty string.
1220 if(is_alpha(**ptr) && *(*ptr+1) == ':') {
1221 /* Regular old drive paths don't have a host type (or host name). */
1222 data->host_type = Uri_HOST_UNKNOWN;
1223 data->host = *ptr;
1224 data->host_len = 0;
1225 return TRUE;
1226 } else if(**ptr == '\\' && *(*ptr+1) == '\\')
1227 /* Skip past the "\\" of a UNC path. */
1228 *ptr += 2;
1231 data->host = *ptr;
1233 while(!is_auth_delim(**ptr, known_scheme)) {
1234 if(**ptr == ':' && !ignore_col) {
1235 /* We can ignore ':' if were inside brackets.*/
1236 if(!inside_brackets) {
1237 const WCHAR *tmp = (*ptr)++;
1239 /* Attempt to parse the port. */
1240 if(!parse_port(ptr, data, flags)) {
1241 /* Windows expects there to be a valid port for known scheme types. */
1242 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
1243 *ptr = data->host;
1244 data->host = NULL;
1245 TRACE("(%p %p %x): Expected valid port\n", ptr, data, flags);
1246 return FALSE;
1247 } else
1248 /* Windows gives up on trying to parse a port when it
1249 * encounters 1 invalid port.
1251 ignore_col = TRUE;
1252 } else {
1253 data->host_len = tmp - data->host;
1254 break;
1257 } else if(**ptr == '%' && known_scheme) {
1258 /* Has to be a legit % encoded value. */
1259 if(!check_pct_encoded(ptr)) {
1260 *ptr = data->host;
1261 data->host = NULL;
1262 return FALSE;
1263 } else
1264 continue;
1265 } else if(**ptr == ']')
1266 inside_brackets = FALSE;
1267 else if(**ptr == '[')
1268 inside_brackets = TRUE;
1270 ++(*ptr);
1273 if(has_start_bracket) {
1274 /* Make sure the last character of the host wasn't a ']'. */
1275 if(*(*ptr-1) == ']') {
1276 TRACE("(%p %p %x): Expected an IP literal inside of the host\n",
1277 ptr, data, flags);
1278 *ptr = data->host;
1279 data->host = NULL;
1280 return FALSE;
1284 /* Don't overwrite our length if we found a port earlier. */
1285 if(!data->port)
1286 data->host_len = *ptr - data->host;
1288 /* If the host is empty, then it's an unknown host type. */
1289 if(data->host_len == 0)
1290 data->host_type = Uri_HOST_UNKNOWN;
1291 else
1292 data->host_type = Uri_HOST_DNS;
1294 TRACE("(%p %p %x): Parsed reg-name. host=%s len=%d\n", ptr, data, flags,
1295 debugstr_wn(data->host, data->host_len), data->host_len);
1296 return TRUE;
1299 /* Attempts to parse an IPv6 address out of the URI.
1301 * IPv6address = 6( h16 ":" ) ls32
1302 * / "::" 5( h16 ":" ) ls32
1303 * / [ h16 ] "::" 4( h16 ":" ) ls32
1304 * / [ *1( h16 ":" ) h16 ] "::" 3( h16 ":" ) ls32
1305 * / [ *2( h16 ":" ) h16 ] "::" 2( h16 ":" ) ls32
1306 * / [ *3( h16 ":" ) h16 ] "::" h16 ":" ls32
1307 * / [ *4( h16 ":" ) h16 ] "::" ls32
1308 * / [ *5( h16 ":" ) h16 ] "::" h16
1309 * / [ *6( h16 ":" ) h16 ] "::"
1311 * ls32 = ( h16 ":" h16 ) / IPv4address
1312 * ; least-significant 32 bits of address.
1314 * h16 = 1*4HEXDIG
1315 * ; 16 bits of address represented in hexadecimal.
1317 * Modeled after google-url's 'DoParseIPv6' function.
1319 static BOOL parse_ipv6address(const WCHAR **ptr, parse_data *data, DWORD flags) {
1320 const WCHAR *start, *cur_start;
1321 ipv6_address ip;
1323 start = cur_start = *ptr;
1324 memset(&ip, 0, sizeof(ipv6_address));
1326 for(;; ++(*ptr)) {
1327 /* Check if we're on the last character of the host. */
1328 BOOL is_end = (is_auth_delim(**ptr, data->scheme_type != URL_SCHEME_UNKNOWN)
1329 || **ptr == ']');
1331 BOOL is_split = (**ptr == ':');
1332 BOOL is_elision = (is_split && !is_end && *(*ptr+1) == ':');
1334 /* Check if we're at the end of of the a component, or
1335 * if we're at the end of the IPv6 address.
1337 if(is_split || is_end) {
1338 DWORD cur_len = 0;
1340 cur_len = *ptr - cur_start;
1342 /* h16 can't have a length > 4. */
1343 if(cur_len > 4) {
1344 *ptr = start;
1346 TRACE("(%p %p %x): h16 component to long.\n",
1347 ptr, data, flags);
1348 return FALSE;
1351 if(cur_len == 0) {
1352 /* An h16 component can't have the length of 0 unless
1353 * the elision is at the beginning of the address, or
1354 * at the end of the address.
1356 if(!((*ptr == start && is_elision) ||
1357 (is_end && (*ptr-2) == ip.elision))) {
1358 *ptr = start;
1359 TRACE("(%p %p %x): IPv6 component can not have a length of 0.\n",
1360 ptr, data, flags);
1361 return FALSE;
1365 if(cur_len > 0) {
1366 /* An IPv6 address can have no more than 8 h16 components. */
1367 if(ip.h16_count >= 8) {
1368 *ptr = start;
1369 TRACE("(%p %p %x): Not a IPv6 address, to many h16 components.\n",
1370 ptr, data, flags);
1371 return FALSE;
1374 ip.components[ip.h16_count].str = cur_start;
1375 ip.components[ip.h16_count].len = cur_len;
1377 TRACE("(%p %p %x): Found h16 component %s, len=%d, h16_count=%d\n",
1378 ptr, data, flags, debugstr_wn(cur_start, cur_len), cur_len,
1379 ip.h16_count);
1380 ++ip.h16_count;
1384 if(is_end)
1385 break;
1387 if(is_elision) {
1388 /* A IPv6 address can only have 1 elision ('::'). */
1389 if(ip.elision) {
1390 *ptr = start;
1392 TRACE("(%p %p %x): IPv6 address cannot have 2 elisions.\n",
1393 ptr, data, flags);
1394 return FALSE;
1397 ip.elision = *ptr;
1398 ++(*ptr);
1401 if(is_split)
1402 cur_start = *ptr+1;
1403 else {
1404 if(!check_ipv4address(ptr, TRUE)) {
1405 if(!is_hexdigit(**ptr)) {
1406 /* Not a valid character for an IPv6 address. */
1407 *ptr = start;
1408 return FALSE;
1410 } else {
1411 /* Found an IPv4 address. */
1412 ip.ipv4 = cur_start;
1413 ip.ipv4_len = *ptr - cur_start;
1415 TRACE("(%p %p %x): Found an attached IPv4 address %s len=%d.\n",
1416 ptr, data, flags, debugstr_wn(ip.ipv4, ip.ipv4_len),
1417 ip.ipv4_len);
1419 /* IPv4 addresses can only appear at the end of a IPv6. */
1420 break;
1425 compute_ipv6_comps_size(&ip);
1427 /* Make sure the IPv6 address adds up to 16 bytes. */
1428 if(ip.components_size + ip.elision_size != 16) {
1429 *ptr = start;
1430 TRACE("(%p %p %x): Invalid IPv6 address, did not add up to 16 bytes.\n",
1431 ptr, data, flags);
1432 return FALSE;
1435 if(ip.elision_size == 2) {
1436 /* For some reason on Windows if an elision that represents
1437 * only 1 h16 component is encountered at the very begin or
1438 * end of an IPv6 address, Windows does not consider it a
1439 * valid IPv6 address.
1441 * Ex: [::2:3:4:5:6:7] is not valid, even though the sum
1442 * of all the components == 128bits.
1444 if(ip.elision < ip.components[0].str ||
1445 ip.elision > ip.components[ip.h16_count-1].str) {
1446 *ptr = start;
1447 TRACE("(%p %p %x): Invalid IPv6 address. Detected elision of 2 bytes at the beginning or end of the address.\n",
1448 ptr, data, flags);
1449 return FALSE;
1453 data->host_type = Uri_HOST_IPV6;
1454 data->has_ipv6 = TRUE;
1455 data->ipv6_address = ip;
1457 TRACE("(%p %p %x): Found valid IPv6 literal %s len=%d\n",
1458 ptr, data, flags, debugstr_wn(start, *ptr-start),
1459 *ptr-start);
1460 return TRUE;
1463 /* IPvFuture = "v" 1*HEXDIG "." 1*( unreserved / sub-delims / ":" ) */
1464 static BOOL parse_ipvfuture(const WCHAR **ptr, parse_data *data, DWORD flags) {
1465 const WCHAR *start = *ptr;
1467 /* IPvFuture has to start with a 'v' or 'V'. */
1468 if(**ptr != 'v' && **ptr != 'V')
1469 return FALSE;
1471 /* Following the v their must be atleast 1 hexdigit. */
1472 ++(*ptr);
1473 if(!is_hexdigit(**ptr)) {
1474 *ptr = start;
1475 return FALSE;
1478 ++(*ptr);
1479 while(is_hexdigit(**ptr))
1480 ++(*ptr);
1482 /* End of the hexdigit sequence must be a '.' */
1483 if(**ptr != '.') {
1484 *ptr = start;
1485 return FALSE;
1488 ++(*ptr);
1489 if(!is_unreserved(**ptr) && !is_subdelim(**ptr) && **ptr != ':') {
1490 *ptr = start;
1491 return FALSE;
1494 ++(*ptr);
1495 while(is_unreserved(**ptr) || is_subdelim(**ptr) || **ptr == ':')
1496 ++(*ptr);
1498 data->host_type = Uri_HOST_UNKNOWN;
1500 TRACE("(%p %p %x): Parsed IPvFuture address %s len=%d\n", ptr, data, flags,
1501 debugstr_wn(start, *ptr-start), *ptr-start);
1503 return TRUE;
1506 /* IP-literal = "[" ( IPv6address / IPvFuture ) "]" */
1507 static BOOL parse_ip_literal(const WCHAR **ptr, parse_data *data, DWORD flags) {
1508 data->host = *ptr;
1510 if(**ptr != '[') {
1511 data->host = NULL;
1512 return FALSE;
1515 ++(*ptr);
1516 if(!parse_ipv6address(ptr, data, flags)) {
1517 if(!parse_ipvfuture(ptr, data, flags)) {
1518 *ptr = data->host;
1519 data->host = NULL;
1520 return FALSE;
1524 if(**ptr != ']') {
1525 *ptr = data->host;
1526 data->host = NULL;
1527 return FALSE;
1530 ++(*ptr);
1531 if(**ptr == ':') {
1532 ++(*ptr);
1533 /* If a valid port is not found, then let it trickle down to
1534 * parse_reg_name.
1536 if(!parse_port(ptr, data, flags)) {
1537 *ptr = data->host;
1538 data->host = NULL;
1539 return FALSE;
1541 } else
1542 data->host_len = *ptr - data->host;
1544 return TRUE;
1547 /* Parses the host information from the URI.
1549 * host = IP-literal / IPv4address / reg-name
1551 static BOOL parse_host(const WCHAR **ptr, parse_data *data, DWORD flags) {
1552 if(!parse_ip_literal(ptr, data, flags)) {
1553 if(!parse_ipv4address(ptr, data, flags)) {
1554 if(!parse_reg_name(ptr, data, flags)) {
1555 TRACE("(%p %p %x): Malformed URI, Unknown host type.\n",
1556 ptr, data, flags);
1557 return FALSE;
1562 return TRUE;
1565 /* Parses the authority information from the URI.
1567 * authority = [ userinfo "@" ] host [ ":" port ]
1569 static BOOL parse_authority(const WCHAR **ptr, parse_data *data, DWORD flags) {
1570 parse_userinfo(ptr, data, flags);
1572 /* Parsing the port will happen during one of the host parsing
1573 * routines (if the URI has a port).
1575 if(!parse_host(ptr, data, flags))
1576 return FALSE;
1578 return TRUE;
1581 /* Attempts to parse the path information of a hierarchical URI. */
1582 static BOOL parse_path_hierarchical(const WCHAR **ptr, parse_data *data, DWORD flags) {
1583 const WCHAR *start = *ptr;
1584 static const WCHAR slash[] = {'/',0};
1586 if(is_path_delim(**ptr)) {
1587 if(data->scheme_type == URL_SCHEME_WILDCARD) {
1588 /* Wildcard schemes don't get a '/' attached if their path is
1589 * empty.
1591 data->path = NULL;
1592 data->path_len = 0;
1593 } else if(!(flags & Uri_CREATE_NO_CANONICALIZE)) {
1594 /* If the path component is empty, then a '/' is added. */
1595 data->path = slash;
1596 data->path_len = 1;
1598 } else {
1599 while(!is_path_delim(**ptr)) {
1600 if(**ptr == '%' && data->scheme_type != URL_SCHEME_UNKNOWN &&
1601 data->scheme_type != URL_SCHEME_FILE) {
1602 if(!check_pct_encoded(ptr)) {
1603 *ptr = start;
1604 return FALSE;
1605 } else
1606 continue;
1607 } else if(**ptr == '\\') {
1608 /* Not allowed to have a backslash if NO_CANONICALIZE is set
1609 * and the scheme is known type (but not a file scheme).
1611 if(flags & Uri_CREATE_NO_CANONICALIZE) {
1612 if(data->scheme_type != URL_SCHEME_FILE &&
1613 data->scheme_type != URL_SCHEME_UNKNOWN) {
1614 *ptr = start;
1615 return FALSE;
1620 ++(*ptr);
1623 /* The only time a URI doesn't have a path is when
1624 * the NO_CANONICALIZE flag is set and the raw URI
1625 * didn't contain one.
1627 if(*ptr == start) {
1628 data->path = NULL;
1629 data->path_len = 0;
1630 } else {
1631 data->path = start;
1632 data->path_len = *ptr - start;
1636 if(data->path)
1637 TRACE("(%p %p %x): Parsed path %s len=%d\n", ptr, data, flags,
1638 debugstr_wn(data->path, data->path_len), data->path_len);
1639 else
1640 TRACE("(%p %p %x): The URI contained no path\n", ptr, data, flags);
1642 return TRUE;
1645 /* Parses the path of a opaque URI (much less strict then the parser
1646 * for a hierarchical URI).
1648 * NOTE:
1649 * Windows allows invalid % encoded data to appear in opaque URI paths
1650 * for unknown scheme types.
1652 static BOOL parse_path_opaque(const WCHAR **ptr, parse_data *data, DWORD flags) {
1653 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
1655 data->path = *ptr;
1657 while(!is_path_delim(**ptr)) {
1658 if(**ptr == '%' && known_scheme) {
1659 if(!check_pct_encoded(ptr)) {
1660 *ptr = data->path;
1661 data->path = NULL;
1662 return FALSE;
1663 } else
1664 continue;
1667 ++(*ptr);
1670 data->path_len = *ptr - data->path;
1671 TRACE("(%p %p %x): Parsed opaque URI path %s len=%d\n", ptr, data, flags,
1672 debugstr_wn(data->path, data->path_len), data->path_len);
1673 return TRUE;
1676 /* Determines how the URI should be parsed after the scheme information.
1678 * If the scheme is followed, by "//" then, it is treated as an hierarchical URI
1679 * which then the authority and path information will be parsed out. Otherwise, the
1680 * URI will be treated as an opaque URI which the authority information is not parsed
1681 * out.
1683 * RFC 3896 definition of hier-part:
1685 * hier-part = "//" authority path-abempty
1686 * / path-absolute
1687 * / path-rootless
1688 * / path-empty
1690 * MSDN opaque URI definition:
1691 * scheme ":" path [ "#" fragment ]
1693 * NOTES:
1694 * If the URI is of an unknown scheme type and has a "//" following the scheme then it
1695 * is treated as a hierarchical URI, but, if the CREATE_NO_CRACK_UNKNOWN_SCHEMES flag is
1696 * set then it is considered an opaque URI reguardless of what follows the scheme information
1697 * (per MSDN documentation).
1699 static BOOL parse_hierpart(const WCHAR **ptr, parse_data *data, DWORD flags) {
1700 const WCHAR *start = *ptr;
1702 /* Checks if the authority information needs to be parsed.
1704 * Relative URI's aren't hierarchical URI's, but, they could trick
1705 * "check_hierarchical" into thinking it is, so we need to explicitly
1706 * make sure it's not relative. Also, if the URI is an implicit file
1707 * scheme it might not contain a "//", but, it's considered hierarchical
1708 * anyways. Wildcard Schemes are always considered hierarchical
1710 if(data->scheme_type == URL_SCHEME_WILDCARD ||
1711 data->scheme_type == URL_SCHEME_FILE ||
1712 (!data->is_relative && check_hierarchical(ptr))) {
1713 /* Only treat it as a hierarchical URI if the scheme_type is known or
1714 * the Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES flag is not set.
1716 if(data->scheme_type != URL_SCHEME_UNKNOWN ||
1717 !(flags & Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES)) {
1718 TRACE("(%p %p %x): Treating URI as an hierarchical URI.\n", ptr, data, flags);
1719 data->is_opaque = FALSE;
1721 if(data->scheme_type == URL_SCHEME_FILE)
1722 /* Skip past the "//" after the scheme (if any). */
1723 check_hierarchical(ptr);
1725 /* TODO: Handle hierarchical URI's, parse authority then parse the path. */
1726 if(!parse_authority(ptr, data, flags))
1727 return FALSE;
1729 return parse_path_hierarchical(ptr, data, flags);
1730 } else
1731 /* Reset ptr to it's starting position so opaque path parsing
1732 * begins at the correct location.
1734 *ptr = start;
1737 /* If it reaches here, then the URI will be treated as an opaque
1738 * URI.
1741 TRACE("(%p %p %x): Treating URI as an opaque URI.\n", ptr, data, flags);
1743 data->is_opaque = TRUE;
1744 if(!parse_path_opaque(ptr, data, flags))
1745 return FALSE;
1747 return TRUE;
1750 /* Attempts to parse the query string from the URI.
1752 * NOTES:
1753 * If NO_DECODE_EXTRA_INFO flag is set, then invalid percent encoded
1754 * data is allowed appear in the query string. For unknown scheme types
1755 * invalid percent encoded data is allowed to appear reguardless.
1757 static BOOL parse_query(const WCHAR **ptr, parse_data *data, DWORD flags) {
1758 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
1760 if(**ptr != '?') {
1761 TRACE("(%p %p %x): URI didn't contain a query string.\n", ptr, data, flags);
1762 return TRUE;
1765 data->query = *ptr;
1767 ++(*ptr);
1768 while(**ptr && **ptr != '#') {
1769 if(**ptr == '%' && known_scheme &&
1770 !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
1771 if(!check_pct_encoded(ptr)) {
1772 *ptr = data->query;
1773 data->query = NULL;
1774 return FALSE;
1775 } else
1776 continue;
1779 ++(*ptr);
1782 data->query_len = *ptr - data->query;
1784 TRACE("(%p %p %x): Parsed query string %s len=%d\n", ptr, data, flags,
1785 debugstr_wn(data->query, data->query_len), data->query_len);
1786 return TRUE;
1789 /* Attempts to parse the fragment from the URI.
1791 * NOTES:
1792 * If NO_DECODE_EXTRA_INFO flag is set, then invalid percent encoded
1793 * data is allowed appear in the query string. For unknown scheme types
1794 * invalid percent encoded data is allowed to appear reguardless.
1796 static BOOL parse_fragment(const WCHAR **ptr, parse_data *data, DWORD flags) {
1797 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
1799 if(**ptr != '#') {
1800 TRACE("(%p %p %x): URI didn't contain a fragment.\n", ptr, data, flags);
1801 return TRUE;
1804 data->fragment = *ptr;
1806 ++(*ptr);
1807 while(**ptr) {
1808 if(**ptr == '%' && known_scheme &&
1809 !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
1810 if(!check_pct_encoded(ptr)) {
1811 *ptr = data->fragment;
1812 data->fragment = NULL;
1813 return FALSE;
1814 } else
1815 continue;
1818 ++(*ptr);
1821 data->fragment_len = *ptr - data->fragment;
1823 TRACE("(%p %p %x): Parsed fragment %s len=%d\n", ptr, data, flags,
1824 debugstr_wn(data->fragment, data->fragment_len), data->fragment_len);
1825 return TRUE;
1828 /* Parses and validates the components of the specified by data->uri
1829 * and stores the information it parses into 'data'.
1831 * Returns TRUE if it successfully parsed the URI. False otherwise.
1833 static BOOL parse_uri(parse_data *data, DWORD flags) {
1834 const WCHAR *ptr;
1835 const WCHAR **pptr;
1837 ptr = data->uri;
1838 pptr = &ptr;
1840 TRACE("(%p %x): BEGINNING TO PARSE URI %s.\n", data, flags, debugstr_w(data->uri));
1842 if(!parse_scheme(pptr, data, flags))
1843 return FALSE;
1845 if(!parse_hierpart(pptr, data, flags))
1846 return FALSE;
1848 if(!parse_query(pptr, data, flags))
1849 return FALSE;
1851 if(!parse_fragment(pptr, data, flags))
1852 return FALSE;
1854 TRACE("(%p %x): FINISHED PARSING URI.\n", data, flags);
1855 return TRUE;
1858 /* Canonicalizes the userinfo of the URI represented by the parse_data.
1860 * Canonicalization of the userinfo is a simple process. If there are any percent
1861 * encoded characters that fall in the "unreserved" character set, they are decoded
1862 * to their actual value. If a character is not in the "unreserved" or "reserved" sets
1863 * then it is percent encoded. Other than that the characters are copied over without
1864 * change.
1866 static BOOL canonicalize_userinfo(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
1867 DWORD i = 0;
1869 uri->userinfo_start = uri->userinfo_split = -1;
1870 uri->userinfo_len = 0;
1872 if(!data->userinfo)
1873 /* URI doesn't have userinfo, so nothing to do here. */
1874 return TRUE;
1876 uri->userinfo_start = uri->canon_len;
1878 while(i < data->userinfo_len) {
1879 if(data->userinfo[i] == ':' && uri->userinfo_split == -1)
1880 /* Windows only considers the first ':' as the delimiter. */
1881 uri->userinfo_split = uri->canon_len - uri->userinfo_start;
1882 else if(data->userinfo[i] == '%') {
1883 /* Only decode % encoded values for known scheme types. */
1884 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
1885 /* See if the value really needs decoded. */
1886 WCHAR val = decode_pct_val(data->userinfo + i);
1887 if(is_unreserved(val)) {
1888 if(!computeOnly)
1889 uri->canon_uri[uri->canon_len] = val;
1891 ++uri->canon_len;
1893 /* Move pass the hex characters. */
1894 i += 3;
1895 continue;
1898 } else if(!is_reserved(data->userinfo[i]) && !is_unreserved(data->userinfo[i]) &&
1899 data->userinfo[i] != '\\') {
1900 /* Only percent encode forbidden characters if the NO_ENCODE_FORBIDDEN_CHARACTERS flag
1901 * is NOT set.
1903 if(!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS)) {
1904 if(!computeOnly)
1905 pct_encode_val(data->userinfo[i], uri->canon_uri + uri->canon_len);
1907 uri->canon_len += 3;
1908 ++i;
1909 continue;
1913 if(!computeOnly)
1914 /* Nothing special, so just copy the character over. */
1915 uri->canon_uri[uri->canon_len] = data->userinfo[i];
1917 ++uri->canon_len;
1918 ++i;
1921 uri->userinfo_len = uri->canon_len - uri->userinfo_start;
1922 if(!computeOnly)
1923 TRACE("(%p %p %x %d): Canonicalized userinfo, userinfo_start=%d, userinfo=%s, userinfo_split=%d userinfo_len=%d.\n",
1924 data, uri, flags, computeOnly, uri->userinfo_start, debugstr_wn(uri->canon_uri + uri->userinfo_start, uri->userinfo_len),
1925 uri->userinfo_split, uri->userinfo_len);
1927 /* Now insert the '@' after the userinfo. */
1928 if(!computeOnly)
1929 uri->canon_uri[uri->canon_len] = '@';
1931 ++uri->canon_len;
1932 return TRUE;
1935 /* Attempts to canonicalize a reg_name.
1937 * Things that happen:
1938 * 1) If Uri_CREATE_NO_CANONICALIZE flag is not set, then the reg_name is
1939 * lower cased. Unless it's an unknown scheme type, which case it's
1940 * no lower cased reguardless.
1942 * 2) Unreserved % encoded characters are decoded for known
1943 * scheme types.
1945 * 3) Forbidden characters are % encoded as long as
1946 * Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS flag is not set and
1947 * it isn't an unknown scheme type.
1949 * 4) If it's a file scheme and the host is "localhost" it's removed.
1951 static BOOL canonicalize_reg_name(const parse_data *data, Uri *uri,
1952 DWORD flags, BOOL computeOnly) {
1953 static const WCHAR localhostW[] =
1954 {'l','o','c','a','l','h','o','s','t',0};
1955 const WCHAR *ptr;
1956 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
1958 uri->host_start = uri->canon_len;
1960 if(data->scheme_type == URL_SCHEME_FILE &&
1961 data->host_len == lstrlenW(localhostW)) {
1962 if(!StrCmpNIW(data->host, localhostW, data->host_len)) {
1963 uri->host_start = -1;
1964 uri->host_len = 0;
1965 uri->host_type = Uri_HOST_UNKNOWN;
1966 return TRUE;
1970 for(ptr = data->host; ptr < data->host+data->host_len; ++ptr) {
1971 if(*ptr == '%' && known_scheme) {
1972 WCHAR val = decode_pct_val(ptr);
1973 if(is_unreserved(val)) {
1974 /* If NO_CANONICALZE is not set, then windows lower cases the
1975 * decoded value.
1977 if(!(flags & Uri_CREATE_NO_CANONICALIZE) && isupperW(val)) {
1978 if(!computeOnly)
1979 uri->canon_uri[uri->canon_len] = tolowerW(val);
1980 } else {
1981 if(!computeOnly)
1982 uri->canon_uri[uri->canon_len] = val;
1984 ++uri->canon_len;
1986 /* Skip past the % encoded character. */
1987 ptr += 2;
1988 continue;
1989 } else {
1990 /* Just copy the % over. */
1991 if(!computeOnly)
1992 uri->canon_uri[uri->canon_len] = *ptr;
1993 ++uri->canon_len;
1995 } else if(*ptr == '\\') {
1996 /* Only unknown scheme types could have made it here with a '\\' in the host name. */
1997 if(!computeOnly)
1998 uri->canon_uri[uri->canon_len] = *ptr;
1999 ++uri->canon_len;
2000 } else if(!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS) &&
2001 !is_unreserved(*ptr) && !is_reserved(*ptr) && known_scheme) {
2002 if(!computeOnly) {
2003 pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
2005 /* The percent encoded value gets lower cased also. */
2006 if(!(flags & Uri_CREATE_NO_CANONICALIZE)) {
2007 uri->canon_uri[uri->canon_len+1] = tolowerW(uri->canon_uri[uri->canon_len+1]);
2008 uri->canon_uri[uri->canon_len+2] = tolowerW(uri->canon_uri[uri->canon_len+2]);
2012 uri->canon_len += 3;
2013 } else {
2014 if(!computeOnly) {
2015 if(!(flags & Uri_CREATE_NO_CANONICALIZE) && known_scheme)
2016 uri->canon_uri[uri->canon_len] = tolowerW(*ptr);
2017 else
2018 uri->canon_uri[uri->canon_len] = *ptr;
2021 ++uri->canon_len;
2025 uri->host_len = uri->canon_len - uri->host_start;
2027 if(!computeOnly)
2028 TRACE("(%p %p %x %d): Canonicalize reg_name=%s len=%d\n", data, uri, flags,
2029 computeOnly, debugstr_wn(uri->canon_uri+uri->host_start, uri->host_len),
2030 uri->host_len);
2032 if(!computeOnly)
2033 find_domain_name(uri->canon_uri+uri->host_start, uri->host_len,
2034 &(uri->domain_offset));
2036 return TRUE;
2039 /* Attempts to canonicalize an implicit IPv4 address. */
2040 static BOOL canonicalize_implicit_ipv4address(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2041 uri->host_start = uri->canon_len;
2043 TRACE("%u\n", data->implicit_ipv4);
2044 /* For unknown scheme types Window's doesn't convert
2045 * the value into an IP address, but, it still considers
2046 * it an IPv4 address.
2048 if(data->scheme_type == URL_SCHEME_UNKNOWN) {
2049 if(!computeOnly)
2050 memcpy(uri->canon_uri+uri->canon_len, data->host, data->host_len*sizeof(WCHAR));
2051 uri->canon_len += data->host_len;
2052 } else {
2053 if(!computeOnly)
2054 uri->canon_len += ui2ipv4(uri->canon_uri+uri->canon_len, data->implicit_ipv4);
2055 else
2056 uri->canon_len += ui2ipv4(NULL, data->implicit_ipv4);
2059 uri->host_len = uri->canon_len - uri->host_start;
2060 uri->host_type = Uri_HOST_IPV4;
2062 if(!computeOnly)
2063 TRACE("%p %p %x %d): Canonicalized implicit IP address=%s len=%d\n",
2064 data, uri, flags, computeOnly,
2065 debugstr_wn(uri->canon_uri+uri->host_start, uri->host_len),
2066 uri->host_len);
2068 return TRUE;
2071 /* Attempts to canonicalize an IPv4 address.
2073 * If the parse_data represents a URI that has an implicit IPv4 address
2074 * (ex. http://256/, this function will convert 256 into 0.0.1.0). If
2075 * the implicit IP address exceeds the value of UINT_MAX (maximum value
2076 * for an IPv4 address) it's canonicalized as if were a reg-name.
2078 * If the parse_data contains a partial or full IPv4 address it normalizes it.
2079 * A partial IPv4 address is something like "192.0" and would be normalized to
2080 * "192.0.0.0". With a full (or partial) IPv4 address like "192.002.01.003" would
2081 * be normalized to "192.2.1.3".
2083 * NOTES:
2084 * Window's ONLY normalizes IPv4 address for known scheme types (one that isn't
2085 * URL_SCHEME_UNKNOWN). For unknown scheme types, it simply copies the data from
2086 * the original URI into the canonicalized URI, but, it still recognizes URI's
2087 * host type as HOST_IPV4.
2089 static BOOL canonicalize_ipv4address(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2090 if(data->has_implicit_ip)
2091 return canonicalize_implicit_ipv4address(data, uri, flags, computeOnly);
2092 else {
2093 uri->host_start = uri->canon_len;
2095 /* Windows only normalizes for known scheme types. */
2096 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
2097 /* parse_data contains a partial or full IPv4 address, so normalize it. */
2098 DWORD i, octetDigitCount = 0, octetCount = 0;
2099 BOOL octetHasDigit = FALSE;
2101 for(i = 0; i < data->host_len; ++i) {
2102 if(data->host[i] == '0' && !octetHasDigit) {
2103 /* Can ignore leading zeros if:
2104 * 1) It isn't the last digit of the octet.
2105 * 2) i+1 != data->host_len
2106 * 3) i+1 != '.'
2108 if(octetDigitCount == 2 ||
2109 i+1 == data->host_len ||
2110 data->host[i+1] == '.') {
2111 if(!computeOnly)
2112 uri->canon_uri[uri->canon_len] = data->host[i];
2113 ++uri->canon_len;
2114 TRACE("Adding zero\n");
2116 } else if(data->host[i] == '.') {
2117 if(!computeOnly)
2118 uri->canon_uri[uri->canon_len] = data->host[i];
2119 ++uri->canon_len;
2121 octetDigitCount = 0;
2122 octetHasDigit = FALSE;
2123 ++octetCount;
2124 } else {
2125 if(!computeOnly)
2126 uri->canon_uri[uri->canon_len] = data->host[i];
2127 ++uri->canon_len;
2129 ++octetDigitCount;
2130 octetHasDigit = TRUE;
2134 /* Make sure the canonicalized IP address has 4 dec-octets.
2135 * If doesn't add "0" ones until there is 4;
2137 for( ; octetCount < 3; ++octetCount) {
2138 if(!computeOnly) {
2139 uri->canon_uri[uri->canon_len] = '.';
2140 uri->canon_uri[uri->canon_len+1] = '0';
2143 uri->canon_len += 2;
2145 } else {
2146 /* Windows doesn't normalize addresses in unknown schemes. */
2147 if(!computeOnly)
2148 memcpy(uri->canon_uri+uri->canon_len, data->host, data->host_len*sizeof(WCHAR));
2149 uri->canon_len += data->host_len;
2152 uri->host_len = uri->canon_len - uri->host_start;
2153 if(!computeOnly)
2154 TRACE("(%p %p %x %d): Canonicalized IPv4 address, ip=%s len=%d\n",
2155 data, uri, flags, computeOnly,
2156 debugstr_wn(uri->canon_uri+uri->host_start, uri->host_len),
2157 uri->host_len);
2160 return TRUE;
2163 /* Attempts to canonicalize the IPv6 address of the URI.
2165 * Multiple things happen during the canonicalization of an IPv6 address:
2166 * 1) Any leading zero's in an h16 component are removed.
2167 * Ex: [0001:0022::] -> [1:22::]
2169 * 2) The longest sequence of zero h16 components are compressed
2170 * into a "::" (elision). If there's a tie, the first is choosen.
2172 * Ex: [0:0:0:0:1:6:7:8] -> [::1:6:7:8]
2173 * [0:0:0:0:1:2::] -> [::1:2:0:0]
2174 * [0:0:1:2:0:0:7:8] -> [::1:2:0:0:7:8]
2176 * 3) If an IPv4 address is attached to the IPv6 address, it's
2177 * also normalized.
2178 * Ex: [::001.002.022.000] -> [::1.2.22.0]
2180 * 4) If an elision is present, but, only represents 1 h16 component
2181 * it's expanded.
2183 * Ex: [1::2:3:4:5:6:7] -> [1:0:2:3:4:5:6:7]
2185 * 5) If the IPv6 address contains an IPv4 address and there exists
2186 * at least 1 non-zero h16 component the IPv4 address is converted
2187 * into two h16 components, otherwise it's normalized and kept as is.
2189 * Ex: [::192.200.003.4] -> [::192.200.3.4]
2190 * [ffff::192.200.003.4] -> [ffff::c0c8:3041]
2192 * NOTE:
2193 * For unknown scheme types Windows simply copies the address over without any
2194 * changes.
2196 * IPv4 address can be included in an elision if all its components are 0's.
2198 static BOOL canonicalize_ipv6address(const parse_data *data, Uri *uri,
2199 DWORD flags, BOOL computeOnly) {
2200 uri->host_start = uri->canon_len;
2202 if(data->scheme_type == URL_SCHEME_UNKNOWN) {
2203 if(!computeOnly)
2204 memcpy(uri->canon_uri+uri->canon_len, data->host, data->host_len*sizeof(WCHAR));
2205 uri->canon_len += data->host_len;
2206 } else {
2207 USHORT values[8];
2208 INT elision_start;
2209 DWORD i, elision_len;
2211 if(!ipv6_to_number(&(data->ipv6_address), values)) {
2212 TRACE("(%p %p %x %d): Failed to compute numerical value for IPv6 address.\n",
2213 data, uri, flags, computeOnly);
2214 return FALSE;
2217 if(!computeOnly)
2218 uri->canon_uri[uri->canon_len] = '[';
2219 ++uri->canon_len;
2221 /* Find where the elision should occur (if any). */
2222 compute_elision_location(&(data->ipv6_address), values, &elision_start, &elision_len);
2224 TRACE("%p %p %x %d): Elision starts at %d, len=%u\n", data, uri, flags,
2225 computeOnly, elision_start, elision_len);
2227 for(i = 0; i < 8; ++i) {
2228 BOOL in_elision = (elision_start > -1 && i >= elision_start &&
2229 i < elision_start+elision_len);
2230 BOOL do_ipv4 = (i == 6 && data->ipv6_address.ipv4 && !in_elision &&
2231 data->ipv6_address.h16_count == 0);
2233 if(i == elision_start) {
2234 if(!computeOnly) {
2235 uri->canon_uri[uri->canon_len] = ':';
2236 uri->canon_uri[uri->canon_len+1] = ':';
2238 uri->canon_len += 2;
2241 /* We can ignore the current component if we're in the elision. */
2242 if(in_elision)
2243 continue;
2245 /* We only add a ':' if we're not at i == 0, or when we're at
2246 * the very end of elision range since the ':' colon was handled
2247 * earlier. Otherwise we would end up with ":::" after elision.
2249 if(i != 0 && !(elision_start > -1 && i == elision_start+elision_len)) {
2250 if(!computeOnly)
2251 uri->canon_uri[uri->canon_len] = ':';
2252 ++uri->canon_len;
2255 if(do_ipv4) {
2256 UINT val;
2257 DWORD len;
2259 /* Combine the two parts of the IPv4 address values. */
2260 val = values[i];
2261 val <<= 16;
2262 val += values[i+1];
2264 if(!computeOnly)
2265 len = ui2ipv4(uri->canon_uri+uri->canon_len, val);
2266 else
2267 len = ui2ipv4(NULL, val);
2269 uri->canon_len += len;
2270 ++i;
2271 } else {
2272 /* Write a regular h16 component to the URI. */
2274 /* Short circuit for the trivial case. */
2275 if(values[i] == 0) {
2276 if(!computeOnly)
2277 uri->canon_uri[uri->canon_len] = '0';
2278 ++uri->canon_len;
2279 } else {
2280 static const WCHAR formatW[] = {'%','x',0};
2282 if(!computeOnly)
2283 uri->canon_len += sprintfW(uri->canon_uri+uri->canon_len,
2284 formatW, values[i]);
2285 else {
2286 WCHAR tmp[5];
2287 uri->canon_len += sprintfW(tmp, formatW, values[i]);
2293 /* Add the closing ']'. */
2294 if(!computeOnly)
2295 uri->canon_uri[uri->canon_len] = ']';
2296 ++uri->canon_len;
2299 uri->host_len = uri->canon_len - uri->host_start;
2301 if(!computeOnly)
2302 TRACE("(%p %p %x %d): Canonicalized IPv6 address %s, len=%d\n", data, uri, flags,
2303 computeOnly, debugstr_wn(uri->canon_uri+uri->host_start, uri->host_len),
2304 uri->host_len);
2306 return TRUE;
2309 /* Attempts to canonicalize the host of the URI (if any). */
2310 static BOOL canonicalize_host(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2311 uri->host_start = -1;
2312 uri->host_len = 0;
2313 uri->domain_offset = -1;
2315 if(data->host) {
2316 switch(data->host_type) {
2317 case Uri_HOST_DNS:
2318 uri->host_type = Uri_HOST_DNS;
2319 if(!canonicalize_reg_name(data, uri, flags, computeOnly))
2320 return FALSE;
2322 break;
2323 case Uri_HOST_IPV4:
2324 uri->host_type = Uri_HOST_IPV4;
2325 if(!canonicalize_ipv4address(data, uri, flags, computeOnly))
2326 return FALSE;
2328 break;
2329 case Uri_HOST_IPV6:
2330 if(!canonicalize_ipv6address(data, uri, flags, computeOnly))
2331 return FALSE;
2333 uri->host_type = Uri_HOST_IPV6;
2334 break;
2335 case Uri_HOST_UNKNOWN:
2336 if(data->host_len > 0 || data->scheme_type != URL_SCHEME_FILE) {
2337 uri->host_start = uri->canon_len;
2339 /* Nothing happens to unknown host types. */
2340 if(!computeOnly)
2341 memcpy(uri->canon_uri+uri->canon_len, data->host, data->host_len*sizeof(WCHAR));
2342 uri->canon_len += data->host_len;
2343 uri->host_len = data->host_len;
2346 uri->host_type = Uri_HOST_UNKNOWN;
2347 break;
2348 default:
2349 FIXME("(%p %p %x %d): Canonicalization for host type %d not supported.\n", data,
2350 uri, flags, computeOnly, data->host_type);
2351 return FALSE;
2355 return TRUE;
2358 static BOOL canonicalize_port(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2359 BOOL has_default_port = FALSE;
2360 USHORT default_port = 0;
2361 DWORD i;
2363 uri->has_port = FALSE;
2365 /* Check if the scheme has a default port. */
2366 for(i = 0; i < sizeof(default_ports)/sizeof(default_ports[0]); ++i) {
2367 if(default_ports[i].scheme == data->scheme_type) {
2368 has_default_port = TRUE;
2369 default_port = default_ports[i].port;
2370 break;
2374 if(data->port || has_default_port)
2375 uri->has_port = TRUE;
2377 /* Possible cases:
2378 * 1) Has a port which is the default port.
2379 * 2) Has a port (not the default).
2380 * 3) Doesn't have a port, but, scheme has a default port.
2381 * 4) No port.
2383 if(has_default_port && data->port && data->port_value == default_port) {
2384 /* If it's the default port and this flag isn't set, don't do anything. */
2385 if(flags & Uri_CREATE_NO_CANONICALIZE) {
2386 /* Copy the original port over. */
2387 if(!computeOnly) {
2388 uri->canon_uri[uri->canon_len] = ':';
2389 memcpy(uri->canon_uri+uri->canon_len+1, data->port, data->port_len*sizeof(WCHAR));
2391 uri->canon_len += data->port_len+1;
2394 uri->port = default_port;
2395 } else if(data->port) {
2396 if(!computeOnly)
2397 uri->canon_uri[uri->canon_len] = ':';
2398 ++uri->canon_len;
2400 if(flags & Uri_CREATE_NO_CANONICALIZE) {
2401 /* Copy the original over without changes. */
2402 if(!computeOnly)
2403 memcpy(uri->canon_uri+uri->canon_len, data->port, data->port_len*sizeof(WCHAR));
2404 uri->canon_len += data->port_len;
2405 } else {
2406 const WCHAR formatW[] = {'%','u',0};
2407 INT len = 0;
2408 if(!computeOnly)
2409 len = sprintfW(uri->canon_uri+uri->canon_len, formatW, data->port_value);
2410 else {
2411 WCHAR tmp[6];
2412 len = sprintfW(tmp, formatW, data->port_value);
2414 uri->canon_len += len;
2417 uri->port = data->port_value;
2418 } else if(has_default_port)
2419 uri->port = default_port;
2421 return TRUE;
2424 /* Canonicalizes the authority of the URI represented by the parse_data. */
2425 static BOOL canonicalize_authority(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2426 uri->authority_start = uri->canon_len;
2427 uri->authority_len = 0;
2429 if(!canonicalize_userinfo(data, uri, flags, computeOnly))
2430 return FALSE;
2432 if(!canonicalize_host(data, uri, flags, computeOnly))
2433 return FALSE;
2435 if(!canonicalize_port(data, uri, flags, computeOnly))
2436 return FALSE;
2438 if(uri->host_start != -1)
2439 uri->authority_len = uri->canon_len - uri->authority_start;
2440 else
2441 uri->authority_start = -1;
2443 return TRUE;
2446 /* Attempts to canonicalize the path of a hierarchical URI.
2448 * Things that happen:
2449 * 1). Forbidden characters are percent encoded, unless the NO_ENCODE_FORBIDDEN
2450 * flag is set or it's a file URI. Forbidden characters are always encoded
2451 * for file schemes reguardless and forbidden characters are never encoded
2452 * for unknown scheme types.
2454 * 2). For known scheme types '\\' are changed to '/'.
2456 * 3). Percent encoded, unreserved characters are decoded to their actual values.
2457 * Unless the scheme type is unknown. For file schemes any percent encoded
2458 * character in the unreserved or reserved set is decoded.
2460 * 4). For File schemes if the path is starts with a drive letter and doesn't
2461 * start with a '/' then one is appended.
2462 * Ex: file://c:/test.mp3 -> file:///c:/test.mp3
2464 * 5). Dot segments are removed from the path for all scheme types
2465 * unless NO_CANONICALIZE flag is set. Dot segments aren't removed
2466 * for wildcard scheme types.
2468 * NOTES:
2469 * file://c:/test%20test -> file:///c:/test%2520test
2470 * file://c:/test%3Etest -> file:///c:/test%253Etest
2471 * file:///c:/test%20test -> file:///c:/test%20test
2472 * file:///c:/test%test -> file:///c:/test%25test
2474 static BOOL canonicalize_path_hierarchical(const parse_data *data, Uri *uri,
2475 DWORD flags, BOOL computeOnly) {
2476 const WCHAR *ptr;
2477 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
2478 const BOOL is_file = data->scheme_type == URL_SCHEME_FILE;
2480 BOOL escape_pct = FALSE;
2482 if(!data->path) {
2483 uri->path_start = -1;
2484 uri->path_len = 0;
2485 return TRUE;
2488 uri->path_start = uri->canon_len;
2490 /* Check if a '/' needs to be appended for the file scheme. */
2491 if(is_file) {
2492 if(data->path_len > 1 && is_alpha(*(data->path)) &&
2493 *(data->path+1) == ':') {
2494 if(!computeOnly)
2495 uri->canon_uri[uri->canon_len] = '/';
2496 uri->canon_len++;
2497 escape_pct = TRUE;
2501 for(ptr = data->path; ptr < data->path+data->path_len; ++ptr) {
2502 if(*ptr == '%') {
2503 const WCHAR *tmp = ptr;
2504 WCHAR val;
2506 /* Check if the % represents a valid encoded char, or if it needs encoded. */
2507 BOOL force_encode = !check_pct_encoded(&tmp) && is_file;
2508 val = decode_pct_val(ptr);
2510 if(force_encode || escape_pct) {
2511 /* Escape the percent sign in the file URI. */
2512 if(!computeOnly)
2513 pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
2514 uri->canon_len += 3;
2515 } else if((is_unreserved(val) && known_scheme) ||
2516 (is_file && (is_unreserved(val) || is_reserved(val)))) {
2517 if(!computeOnly)
2518 uri->canon_uri[uri->canon_len] = val;
2519 ++uri->canon_len;
2521 ptr += 2;
2522 continue;
2523 } else {
2524 if(!computeOnly)
2525 uri->canon_uri[uri->canon_len] = *ptr;
2526 ++uri->canon_len;
2528 } else if(*ptr == '\\' && known_scheme) {
2529 if(!computeOnly)
2530 uri->canon_uri[uri->canon_len] = '/';
2531 ++uri->canon_len;
2532 } else if(known_scheme && !is_unreserved(*ptr) && !is_reserved(*ptr) &&
2533 (!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS) || is_file)) {
2534 /* Escape the forbidden character. */
2535 if(!computeOnly)
2536 pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
2537 uri->canon_len += 3;
2538 } else {
2539 if(!computeOnly)
2540 uri->canon_uri[uri->canon_len] = *ptr;
2541 ++uri->canon_len;
2545 uri->path_len = uri->canon_len - uri->path_start;
2547 /* Removing the dot segments only happens when it's not in
2548 * computeOnly mode and it's not a wildcard scheme.
2550 if(!computeOnly && data->scheme_type != URL_SCHEME_WILDCARD) {
2551 if(!(flags & Uri_CREATE_NO_CANONICALIZE)) {
2552 /* Remove the dot segments (if any) and reset everything to the new
2553 * correct length.
2555 DWORD new_len = remove_dot_segments(uri->canon_uri+uri->path_start, uri->path_len);
2556 uri->canon_len -= uri->path_len-new_len;
2557 uri->path_len = new_len;
2561 if(!computeOnly)
2562 TRACE("Canonicalized path %s len=%d\n",
2563 debugstr_wn(uri->canon_uri+uri->path_start, uri->path_len),
2564 uri->path_len);
2566 return TRUE;
2569 /* Attempts to canonicalize the path for an opaque URI.
2571 * For known scheme types:
2572 * 1) forbidden characters are percent encoded if
2573 * NO_ENCODE_FORBIDDEN_CHARACTERS isn't set.
2575 * 2) Percent encoded, unreserved characters are decoded
2576 * to their actual values, for known scheme types.
2578 * 3) '\\' are changed to '/' for known scheme types
2579 * except for mailto schemes.
2581 static BOOL canonicalize_path_opaque(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2582 const WCHAR *ptr;
2583 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
2585 if(!data->path) {
2586 uri->path_start = -1;
2587 uri->path_len = 0;
2588 return TRUE;
2591 uri->path_start = uri->canon_len;
2593 /* Windows doesn't allow a "//" to appear after the scheme
2594 * of a URI, if it's an opaque URI.
2596 if(data->scheme && *(data->path) == '/' && *(data->path+1) == '/') {
2597 /* So it inserts a "/." before the "//" if it exists. */
2598 if(!computeOnly) {
2599 uri->canon_uri[uri->canon_len] = '/';
2600 uri->canon_uri[uri->canon_len+1] = '.';
2603 uri->canon_len += 2;
2606 for(ptr = data->path; ptr < data->path+data->path_len; ++ptr) {
2607 if(*ptr == '%' && known_scheme) {
2608 WCHAR val = decode_pct_val(ptr);
2610 if(is_unreserved(val)) {
2611 if(!computeOnly)
2612 uri->canon_uri[uri->canon_len] = val;
2613 ++uri->canon_len;
2615 ptr += 2;
2616 continue;
2617 } else {
2618 if(!computeOnly)
2619 uri->canon_uri[uri->canon_len] = *ptr;
2620 ++uri->canon_len;
2622 } else if(known_scheme && !is_unreserved(*ptr) && !is_reserved(*ptr) &&
2623 !(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS)) {
2624 if(!computeOnly)
2625 pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
2626 uri->canon_len += 3;
2627 } else {
2628 if(!computeOnly)
2629 uri->canon_uri[uri->canon_len] = *ptr;
2630 ++uri->canon_len;
2634 uri->path_len = uri->canon_len - uri->path_start;
2636 TRACE("(%p %p %x %d): Canonicalized opaque URI path %s len=%d\n", data, uri, flags, computeOnly,
2637 debugstr_wn(uri->canon_uri+uri->path_start, uri->path_len), uri->path_len);
2638 return TRUE;
2641 /* Determines how the URI represented by the parse_data should be canonicalized.
2643 * Essentially, if the parse_data represents an hierarchical URI then it calls
2644 * canonicalize_authority and the canonicalization functions for the path. If the
2645 * URI is opaque it canonicalizes the path of the URI.
2647 static BOOL canonicalize_hierpart(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2648 if(!data->is_opaque) {
2649 /* "//" is only added for non-wildcard scheme types. */
2650 if(data->scheme_type != URL_SCHEME_WILDCARD) {
2651 if(!computeOnly) {
2652 INT pos = uri->canon_len;
2654 uri->canon_uri[pos] = '/';
2655 uri->canon_uri[pos+1] = '/';
2657 uri->canon_len += 2;
2660 if(!canonicalize_authority(data, uri, flags, computeOnly))
2661 return FALSE;
2663 /* TODO: Canonicalize the path of the URI. */
2664 if(!canonicalize_path_hierarchical(data, uri, flags, computeOnly))
2665 return FALSE;
2667 } else {
2668 /* Opaque URI's don't have an authority. */
2669 uri->userinfo_start = uri->userinfo_split = -1;
2670 uri->userinfo_len = 0;
2671 uri->host_start = -1;
2672 uri->host_len = 0;
2673 uri->host_type = Uri_HOST_UNKNOWN;
2674 uri->has_port = FALSE;
2675 uri->authority_start = -1;
2676 uri->authority_len = 0;
2677 uri->domain_offset = -1;
2679 if(!canonicalize_path_opaque(data, uri, flags, computeOnly))
2680 return FALSE;
2683 if(uri->path_start > -1 && !computeOnly)
2684 /* Finding file extensions happens for both types of URIs. */
2685 uri->extension_offset = find_file_extension(uri->canon_uri+uri->path_start, uri->path_len);
2686 else
2687 uri->extension_offset = -1;
2689 return TRUE;
2692 /* Attempts to canonicalize the query string of the URI.
2694 * Things that happen:
2695 * 1) For known scheme types forbidden characters
2696 * are percent encoded, unless the NO_DECODE_EXTRA_INFO flag is set
2697 * or NO_ENCODE_FORBIDDEN_CHARACTERS is set.
2699 * 2) For known scheme types, percent encoded, unreserved characters
2700 * are decoded as long as the NO_DECODE_EXTRA_INFO flag isn't set.
2702 static BOOL canonicalize_query(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2703 const WCHAR *ptr, *end;
2704 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
2706 if(!data->query) {
2707 uri->query_start = -1;
2708 uri->query_len = 0;
2709 return TRUE;
2712 uri->query_start = uri->canon_len;
2714 end = data->query+data->query_len;
2715 for(ptr = data->query; ptr < end; ++ptr) {
2716 if(*ptr == '%') {
2717 if(known_scheme && !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
2718 WCHAR val = decode_pct_val(ptr);
2719 if(is_unreserved(val)) {
2720 if(!computeOnly)
2721 uri->canon_uri[uri->canon_len] = val;
2722 ++uri->canon_len;
2724 ptr += 2;
2725 continue;
2728 } else if(known_scheme && !is_unreserved(*ptr) && !is_reserved(*ptr)) {
2729 if(!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS) &&
2730 !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
2731 if(!computeOnly)
2732 pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
2733 uri->canon_len += 3;
2734 continue;
2738 if(!computeOnly)
2739 uri->canon_uri[uri->canon_len] = *ptr;
2740 ++uri->canon_len;
2743 uri->query_len = uri->canon_len - uri->query_start;
2745 if(!computeOnly)
2746 TRACE("(%p %p %x %d): Canonicalized query string %s len=%d\n", data, uri, flags,
2747 computeOnly, debugstr_wn(uri->canon_uri+uri->query_start, uri->query_len),
2748 uri->query_len);
2749 return TRUE;
2752 /* Canonicalizes the scheme information specified in the parse_data using the specified flags. */
2753 static BOOL canonicalize_scheme(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2754 uri->scheme_start = -1;
2755 uri->scheme_len = 0;
2757 if(!data->scheme) {
2758 /* The only type of URI that doesn't have to have a scheme is a relative
2759 * URI.
2761 if(!data->is_relative) {
2762 FIXME("(%p %p %x): Unable to determine the scheme type of %s.\n", data,
2763 uri, flags, debugstr_w(data->uri));
2764 return FALSE;
2766 } else {
2767 if(!computeOnly) {
2768 DWORD i;
2769 INT pos = uri->canon_len;
2771 for(i = 0; i < data->scheme_len; ++i) {
2772 /* Scheme name must be lower case after canonicalization. */
2773 uri->canon_uri[i + pos] = tolowerW(data->scheme[i]);
2776 uri->canon_uri[i + pos] = ':';
2777 uri->scheme_start = pos;
2779 TRACE("(%p %p %x): Canonicalized scheme=%s, len=%d.\n", data, uri, flags,
2780 debugstr_wn(uri->canon_uri, uri->scheme_len), data->scheme_len);
2783 /* This happens in both computation modes. */
2784 uri->canon_len += data->scheme_len + 1;
2785 uri->scheme_len = data->scheme_len;
2787 return TRUE;
2790 /* Compute's what the length of the URI specified by the parse_data will be
2791 * after canonicalization occurs using the specified flags.
2793 * This function will return a non-zero value indicating the length of the canonicalized
2794 * URI, or -1 on error.
2796 static int compute_canonicalized_length(const parse_data *data, DWORD flags) {
2797 Uri uri;
2799 memset(&uri, 0, sizeof(Uri));
2801 TRACE("(%p %x): Beginning to compute canonicalized length for URI %s\n", data, flags,
2802 debugstr_w(data->uri));
2804 if(!canonicalize_scheme(data, &uri, flags, TRUE)) {
2805 ERR("(%p %x): Failed to compute URI scheme length.\n", data, flags);
2806 return -1;
2809 if(!canonicalize_hierpart(data, &uri, flags, TRUE)) {
2810 ERR("(%p %x): Failed to compute URI hierpart length.\n", data, flags);
2811 return -1;
2814 if(!canonicalize_query(data, &uri, flags, TRUE)) {
2815 ERR("(%p %x): Failed to compute query string length.\n", data, flags);
2816 return -1;
2819 TRACE("(%p %x): Finished computing canonicalized URI length. length=%d\n", data, flags, uri.canon_len);
2821 return uri.canon_len;
2824 /* Canonicalizes the URI data specified in the parse_data, using the given flags. If the
2825 * canonicalization succeededs it will store all the canonicalization information
2826 * in the pointer to the Uri.
2828 * To canonicalize a URI this function first computes what the length of the URI
2829 * specified by the parse_data will be. Once this is done it will then perfom the actual
2830 * canonicalization of the URI.
2832 static HRESULT canonicalize_uri(const parse_data *data, Uri *uri, DWORD flags) {
2833 INT len;
2835 uri->canon_uri = NULL;
2836 len = uri->canon_size = uri->canon_len = 0;
2838 TRACE("(%p %p %x): beginning to canonicalize URI %s.\n", data, uri, flags, debugstr_w(data->uri));
2840 /* First try to compute the length of the URI. */
2841 len = compute_canonicalized_length(data, flags);
2842 if(len == -1) {
2843 ERR("(%p %p %x): Could not compute the canonicalized length of %s.\n", data, uri, flags,
2844 debugstr_w(data->uri));
2845 return E_INVALIDARG;
2848 uri->canon_uri = heap_alloc((len+1)*sizeof(WCHAR));
2849 if(!uri->canon_uri)
2850 return E_OUTOFMEMORY;
2852 uri->canon_size = len;
2853 if(!canonicalize_scheme(data, uri, flags, FALSE)) {
2854 ERR("(%p %p %x): Unable to canonicalize the scheme of the URI.\n", data, uri, flags);
2855 heap_free(uri->canon_uri);
2856 return E_INVALIDARG;
2858 uri->scheme_type = data->scheme_type;
2860 if(!canonicalize_hierpart(data, uri, flags, FALSE)) {
2861 ERR("(%p %p %x): Unable to canonicalize the heirpart of the URI\n", data, uri, flags);
2862 heap_free(uri->canon_uri);
2863 return E_INVALIDARG;
2866 if(!canonicalize_query(data, uri, flags, FALSE)) {
2867 ERR("(%p %p %x): Unable to canonicalize query string of the URI.\n",
2868 data, uri, flags);
2869 return E_INVALIDARG;
2872 /* There's a possibility we didn't use all the space we allocated
2873 * earlier.
2875 if(uri->canon_len < uri->canon_size) {
2876 /* This happens if the URI is hierarchical and dot
2877 * segments were removed from it's path.
2879 WCHAR *tmp = heap_realloc(uri->canon_uri, (uri->canon_len+1)*sizeof(WCHAR));
2880 if(!tmp)
2881 return E_OUTOFMEMORY;
2883 uri->canon_uri = tmp;
2884 uri->canon_size = uri->canon_len;
2887 uri->canon_uri[uri->canon_len] = '\0';
2888 TRACE("(%p %p %x): finished canonicalizing the URI. uri=%s\n", data, uri, flags, debugstr_w(uri->canon_uri));
2890 return S_OK;
2893 #define URI(x) ((IUri*) &(x)->lpIUriVtbl)
2894 #define URIBUILDER(x) ((IUriBuilder*) &(x)->lpIUriBuilderVtbl)
2896 #define URI_THIS(iface) DEFINE_THIS(Uri, IUri, iface)
2898 static HRESULT WINAPI Uri_QueryInterface(IUri *iface, REFIID riid, void **ppv)
2900 Uri *This = URI_THIS(iface);
2902 if(IsEqualGUID(&IID_IUnknown, riid)) {
2903 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
2904 *ppv = URI(This);
2905 }else if(IsEqualGUID(&IID_IUri, riid)) {
2906 TRACE("(%p)->(IID_IUri %p)\n", This, ppv);
2907 *ppv = URI(This);
2908 }else {
2909 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
2910 *ppv = NULL;
2911 return E_NOINTERFACE;
2914 IUnknown_AddRef((IUnknown*)*ppv);
2915 return S_OK;
2918 static ULONG WINAPI Uri_AddRef(IUri *iface)
2920 Uri *This = URI_THIS(iface);
2921 LONG ref = InterlockedIncrement(&This->ref);
2923 TRACE("(%p) ref=%d\n", This, ref);
2925 return ref;
2928 static ULONG WINAPI Uri_Release(IUri *iface)
2930 Uri *This = URI_THIS(iface);
2931 LONG ref = InterlockedDecrement(&This->ref);
2933 TRACE("(%p) ref=%d\n", This, ref);
2935 if(!ref) {
2936 SysFreeString(This->raw_uri);
2937 heap_free(This->canon_uri);
2938 heap_free(This);
2941 return ref;
2944 static HRESULT WINAPI Uri_GetPropertyBSTR(IUri *iface, Uri_PROPERTY uriProp, BSTR *pbstrProperty, DWORD dwFlags)
2946 Uri *This = URI_THIS(iface);
2947 HRESULT hres;
2948 TRACE("(%p)->(%d %p %x)\n", This, uriProp, pbstrProperty, dwFlags);
2950 if(!pbstrProperty)
2951 return E_POINTER;
2953 if(uriProp > Uri_PROPERTY_STRING_LAST) {
2954 /* Windows allocates an empty BSTR for invalid Uri_PROPERTY's. */
2955 *pbstrProperty = SysAllocStringLen(NULL, 0);
2956 if(!(*pbstrProperty))
2957 return E_OUTOFMEMORY;
2959 /* It only returns S_FALSE for the ZONE property... */
2960 if(uriProp == Uri_PROPERTY_ZONE)
2961 return S_FALSE;
2962 else
2963 return S_OK;
2966 /* Don't have support for flags yet. */
2967 if(dwFlags) {
2968 FIXME("(%p)->(%d %p %x)\n", This, uriProp, pbstrProperty, dwFlags);
2969 return E_NOTIMPL;
2972 switch(uriProp) {
2973 case Uri_PROPERTY_AUTHORITY:
2974 if(This->authority_start > -1) {
2975 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->authority_start, This->authority_len);
2976 hres = S_OK;
2977 } else {
2978 *pbstrProperty = SysAllocStringLen(NULL, 0);
2979 hres = S_FALSE;
2982 if(!(*pbstrProperty))
2983 hres = E_OUTOFMEMORY;
2985 break;
2986 case Uri_PROPERTY_DOMAIN:
2987 if(This->domain_offset > -1) {
2988 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->host_start+This->domain_offset,
2989 This->host_len-This->domain_offset);
2990 hres = S_OK;
2991 } else {
2992 *pbstrProperty = SysAllocStringLen(NULL, 0);
2993 hres = S_FALSE;
2996 if(!(*pbstrProperty))
2997 hres = E_OUTOFMEMORY;
2999 break;
3000 case Uri_PROPERTY_EXTENSION:
3001 if(This->extension_offset > -1) {
3002 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->path_start+This->extension_offset,
3003 This->path_len-This->extension_offset);
3004 hres = S_OK;
3005 } else {
3006 *pbstrProperty = SysAllocStringLen(NULL, 0);
3007 hres = S_FALSE;
3010 if(!(*pbstrProperty))
3011 hres = E_OUTOFMEMORY;
3013 break;
3014 case Uri_PROPERTY_HOST:
3015 if(This->host_start > -1) {
3016 /* The '[' and ']' aren't included for IPv6 addresses. */
3017 if(This->host_type == Uri_HOST_IPV6)
3018 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->host_start+1, This->host_len-2);
3019 else
3020 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->host_start, This->host_len);
3022 hres = S_OK;
3023 } else {
3024 *pbstrProperty = SysAllocStringLen(NULL, 0);
3025 hres = S_FALSE;
3028 if(!(*pbstrProperty))
3029 hres = E_OUTOFMEMORY;
3031 break;
3032 case Uri_PROPERTY_PASSWORD:
3033 if(This->userinfo_split > -1) {
3034 *pbstrProperty = SysAllocStringLen(
3035 This->canon_uri+This->userinfo_start+This->userinfo_split+1,
3036 This->userinfo_len-This->userinfo_split-1);
3037 hres = S_OK;
3038 } else {
3039 *pbstrProperty = SysAllocStringLen(NULL, 0);
3040 hres = S_FALSE;
3043 if(!(*pbstrProperty))
3044 return E_OUTOFMEMORY;
3046 break;
3047 case Uri_PROPERTY_PATH:
3048 if(This->path_start > -1) {
3049 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->path_start, This->path_len);
3050 hres = S_OK;
3051 } else {
3052 *pbstrProperty = SysAllocStringLen(NULL, 0);
3053 hres = S_FALSE;
3056 if(!(*pbstrProperty))
3057 hres = E_OUTOFMEMORY;
3059 break;
3060 case Uri_PROPERTY_PATH_AND_QUERY:
3061 if(This->path_start > -1) {
3062 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->path_start, This->path_len+This->query_len);
3063 hres = S_OK;
3064 } else if(This->query_start > -1) {
3065 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->query_start, This->query_len);
3066 hres = S_OK;
3067 } else {
3068 *pbstrProperty = SysAllocStringLen(NULL, 0);
3069 hres = S_FALSE;
3072 if(!(*pbstrProperty))
3073 hres = E_OUTOFMEMORY;
3075 break;
3076 case Uri_PROPERTY_QUERY:
3077 if(This->query_start > -1) {
3078 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->query_start, This->query_len);
3079 hres = S_OK;
3080 } else {
3081 *pbstrProperty = SysAllocStringLen(NULL, 0);
3082 hres = S_FALSE;
3085 if(!(*pbstrProperty))
3086 hres = E_OUTOFMEMORY;
3088 break;
3089 case Uri_PROPERTY_RAW_URI:
3090 *pbstrProperty = SysAllocString(This->raw_uri);
3091 if(!(*pbstrProperty))
3092 hres = E_OUTOFMEMORY;
3093 else
3094 hres = S_OK;
3095 break;
3096 case Uri_PROPERTY_SCHEME_NAME:
3097 if(This->scheme_start > -1) {
3098 *pbstrProperty = SysAllocStringLen(This->canon_uri + This->scheme_start, This->scheme_len);
3099 hres = S_OK;
3100 } else {
3101 *pbstrProperty = SysAllocStringLen(NULL, 0);
3102 hres = S_FALSE;
3105 if(!(*pbstrProperty))
3106 hres = E_OUTOFMEMORY;
3108 break;
3109 case Uri_PROPERTY_USER_INFO:
3110 if(This->userinfo_start > -1) {
3111 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->userinfo_start, This->userinfo_len);
3112 hres = S_OK;
3113 } else {
3114 *pbstrProperty = SysAllocStringLen(NULL, 0);
3115 hres = S_FALSE;
3118 if(!(*pbstrProperty))
3119 hres = E_OUTOFMEMORY;
3121 break;
3122 case Uri_PROPERTY_USER_NAME:
3123 if(This->userinfo_start > -1) {
3124 /* If userinfo_split is set, that means a password exists
3125 * so the username is only from userinfo_start to userinfo_split.
3127 if(This->userinfo_split > -1) {
3128 *pbstrProperty = SysAllocStringLen(This->canon_uri + This->userinfo_start, This->userinfo_split);
3129 hres = S_OK;
3130 } else {
3131 *pbstrProperty = SysAllocStringLen(This->canon_uri + This->userinfo_start, This->userinfo_len);
3132 hres = S_OK;
3134 } else {
3135 *pbstrProperty = SysAllocStringLen(NULL, 0);
3136 hres = S_FALSE;
3139 if(!(*pbstrProperty))
3140 return E_OUTOFMEMORY;
3142 break;
3143 default:
3144 FIXME("(%p)->(%d %p %x)\n", This, uriProp, pbstrProperty, dwFlags);
3145 hres = E_NOTIMPL;
3148 return hres;
3151 static HRESULT WINAPI Uri_GetPropertyLength(IUri *iface, Uri_PROPERTY uriProp, DWORD *pcchProperty, DWORD dwFlags)
3153 Uri *This = URI_THIS(iface);
3154 HRESULT hres;
3155 TRACE("(%p)->(%d %p %x)\n", This, uriProp, pcchProperty, dwFlags);
3157 if(!pcchProperty)
3158 return E_INVALIDARG;
3160 /* Can only return a length for a property if it's a string. */
3161 if(uriProp > Uri_PROPERTY_STRING_LAST)
3162 return E_INVALIDARG;
3164 /* Don't have support for flags yet. */
3165 if(dwFlags) {
3166 FIXME("(%p)->(%d %p %x)\n", This, uriProp, pcchProperty, dwFlags);
3167 return E_NOTIMPL;
3170 switch(uriProp) {
3171 case Uri_PROPERTY_AUTHORITY:
3172 *pcchProperty = This->authority_len;
3173 hres = (This->authority_start > -1) ? S_OK : S_FALSE;
3174 break;
3175 case Uri_PROPERTY_DOMAIN:
3176 if(This->domain_offset > -1)
3177 *pcchProperty = This->host_len - This->domain_offset;
3178 else
3179 *pcchProperty = 0;
3181 hres = (This->domain_offset > -1) ? S_OK : S_FALSE;
3182 break;
3183 case Uri_PROPERTY_EXTENSION:
3184 if(This->extension_offset > -1) {
3185 *pcchProperty = This->path_len - This->extension_offset;
3186 hres = S_OK;
3187 } else {
3188 *pcchProperty = 0;
3189 hres = S_FALSE;
3192 break;
3193 case Uri_PROPERTY_HOST:
3194 *pcchProperty = This->host_len;
3196 /* '[' and ']' aren't included in the length. */
3197 if(This->host_type == Uri_HOST_IPV6)
3198 *pcchProperty -= 2;
3200 hres = (This->host_start > -1) ? S_OK : S_FALSE;
3201 break;
3202 case Uri_PROPERTY_PASSWORD:
3203 *pcchProperty = (This->userinfo_split > -1) ? This->userinfo_len-This->userinfo_split-1 : 0;
3204 hres = (This->userinfo_split > -1) ? S_OK : S_FALSE;
3205 break;
3206 case Uri_PROPERTY_PATH:
3207 *pcchProperty = This->path_len;
3208 hres = (This->path_start > -1) ? S_OK : S_FALSE;
3209 break;
3210 case Uri_PROPERTY_PATH_AND_QUERY:
3211 *pcchProperty = This->path_len+This->query_len;
3212 hres = (This->path_start > -1 || This->query_start > -1) ? S_OK : S_FALSE;
3213 break;
3214 case Uri_PROPERTY_QUERY:
3215 *pcchProperty = This->query_len;
3216 hres = (This->query_start > -1) ? S_OK : S_FALSE;
3217 break;
3218 case Uri_PROPERTY_RAW_URI:
3219 *pcchProperty = SysStringLen(This->raw_uri);
3220 hres = S_OK;
3221 break;
3222 case Uri_PROPERTY_SCHEME_NAME:
3223 *pcchProperty = This->scheme_len;
3224 hres = (This->scheme_start > -1) ? S_OK : S_FALSE;
3225 break;
3226 case Uri_PROPERTY_USER_INFO:
3227 *pcchProperty = This->userinfo_len;
3228 hres = (This->userinfo_start > -1) ? S_OK : S_FALSE;
3229 break;
3230 case Uri_PROPERTY_USER_NAME:
3231 *pcchProperty = (This->userinfo_split > -1) ? This->userinfo_split : This->userinfo_len;
3232 hres = (This->userinfo_start > -1) ? S_OK : S_FALSE;
3233 break;
3234 default:
3235 FIXME("(%p)->(%d %p %x)\n", This, uriProp, pcchProperty, dwFlags);
3236 hres = E_NOTIMPL;
3239 return hres;
3242 static HRESULT WINAPI Uri_GetPropertyDWORD(IUri *iface, Uri_PROPERTY uriProp, DWORD *pcchProperty, DWORD dwFlags)
3244 Uri *This = URI_THIS(iface);
3245 HRESULT hres;
3247 TRACE("(%p)->(%d %p %x)\n", This, uriProp, pcchProperty, dwFlags);
3249 if(!pcchProperty)
3250 return E_INVALIDARG;
3252 /* Microsoft's implementation for the ZONE property of a URI seems to be lacking...
3253 * From what I can tell, instead of checking which URLZONE the URI belongs to it
3254 * simply assigns URLZONE_INVALID and returns E_NOTIMPL. This also applies to the GetZone
3255 * function.
3257 if(uriProp == Uri_PROPERTY_ZONE) {
3258 *pcchProperty = URLZONE_INVALID;
3259 return E_NOTIMPL;
3262 if(uriProp < Uri_PROPERTY_DWORD_START) {
3263 *pcchProperty = 0;
3264 return E_INVALIDARG;
3267 switch(uriProp) {
3268 case Uri_PROPERTY_HOST_TYPE:
3269 *pcchProperty = This->host_type;
3270 hres = S_OK;
3271 break;
3272 case Uri_PROPERTY_PORT:
3273 if(!This->has_port) {
3274 *pcchProperty = 0;
3275 hres = S_FALSE;
3276 } else {
3277 *pcchProperty = This->port;
3278 hres = S_OK;
3281 break;
3282 case Uri_PROPERTY_SCHEME:
3283 *pcchProperty = This->scheme_type;
3284 hres = S_OK;
3285 break;
3286 default:
3287 FIXME("(%p)->(%d %p %x)\n", This, uriProp, pcchProperty, dwFlags);
3288 hres = E_NOTIMPL;
3291 return hres;
3294 static HRESULT WINAPI Uri_HasProperty(IUri *iface, Uri_PROPERTY uriProp, BOOL *pfHasProperty)
3296 Uri *This = URI_THIS(iface);
3297 FIXME("(%p)->(%d %p)\n", This, uriProp, pfHasProperty);
3299 if(!pfHasProperty)
3300 return E_INVALIDARG;
3302 return E_NOTIMPL;
3305 static HRESULT WINAPI Uri_GetAbsoluteUri(IUri *iface, BSTR *pstrAbsoluteUri)
3307 Uri *This = URI_THIS(iface);
3308 FIXME("(%p)->(%p)\n", This, pstrAbsoluteUri);
3310 if(!pstrAbsoluteUri)
3311 return E_POINTER;
3313 return E_NOTIMPL;
3316 static HRESULT WINAPI Uri_GetAuthority(IUri *iface, BSTR *pstrAuthority)
3318 TRACE("(%p)->(%p)\n", iface, pstrAuthority);
3319 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_AUTHORITY, pstrAuthority, 0);
3322 static HRESULT WINAPI Uri_GetDisplayUri(IUri *iface, BSTR *pstrDisplayUri)
3324 Uri *This = URI_THIS(iface);
3325 FIXME("(%p)->(%p)\n", This, pstrDisplayUri);
3327 if(!pstrDisplayUri)
3328 return E_POINTER;
3330 return E_NOTIMPL;
3333 static HRESULT WINAPI Uri_GetDomain(IUri *iface, BSTR *pstrDomain)
3335 TRACE("(%p)->(%p)\n", iface, pstrDomain);
3336 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_DOMAIN, pstrDomain, 0);
3339 static HRESULT WINAPI Uri_GetExtension(IUri *iface, BSTR *pstrExtension)
3341 TRACE("(%p)->(%p)\n", iface, pstrExtension);
3342 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_EXTENSION, pstrExtension, 0);
3345 static HRESULT WINAPI Uri_GetFragment(IUri *iface, BSTR *pstrFragment)
3347 Uri *This = URI_THIS(iface);
3348 FIXME("(%p)->(%p)\n", This, pstrFragment);
3350 if(!pstrFragment)
3351 return E_POINTER;
3353 return E_NOTIMPL;
3356 static HRESULT WINAPI Uri_GetHost(IUri *iface, BSTR *pstrHost)
3358 TRACE("(%p)->(%p)\n", iface, pstrHost);
3359 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_HOST, pstrHost, 0);
3362 static HRESULT WINAPI Uri_GetPassword(IUri *iface, BSTR *pstrPassword)
3364 TRACE("(%p)->(%p)\n", iface, pstrPassword);
3365 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_PASSWORD, pstrPassword, 0);
3368 static HRESULT WINAPI Uri_GetPath(IUri *iface, BSTR *pstrPath)
3370 TRACE("(%p)->(%p)\n", iface, pstrPath);
3371 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_PATH, pstrPath, 0);
3374 static HRESULT WINAPI Uri_GetPathAndQuery(IUri *iface, BSTR *pstrPathAndQuery)
3376 TRACE("(%p)->(%p)\n", iface, pstrPathAndQuery);
3377 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_PATH_AND_QUERY, pstrPathAndQuery, 0);
3380 static HRESULT WINAPI Uri_GetQuery(IUri *iface, BSTR *pstrQuery)
3382 TRACE("(%p)->(%p)\n", iface, pstrQuery);
3383 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_QUERY, pstrQuery, 0);
3386 static HRESULT WINAPI Uri_GetRawUri(IUri *iface, BSTR *pstrRawUri)
3388 Uri *This = URI_THIS(iface);
3389 TRACE("(%p)->(%p)\n", This, pstrRawUri);
3391 /* Just forward the call to GetPropertyBSTR. */
3392 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_RAW_URI, pstrRawUri, 0);
3395 static HRESULT WINAPI Uri_GetSchemeName(IUri *iface, BSTR *pstrSchemeName)
3397 Uri *This = URI_THIS(iface);
3398 TRACE("(%p)->(%p)\n", This, pstrSchemeName);
3399 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_SCHEME_NAME, pstrSchemeName, 0);
3402 static HRESULT WINAPI Uri_GetUserInfo(IUri *iface, BSTR *pstrUserInfo)
3404 TRACE("(%p)->(%p)\n", iface, pstrUserInfo);
3405 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_USER_INFO, pstrUserInfo, 0);
3408 static HRESULT WINAPI Uri_GetUserName(IUri *iface, BSTR *pstrUserName)
3410 TRACE("(%p)->(%p)\n", iface, pstrUserName);
3411 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_USER_NAME, pstrUserName, 0);
3414 static HRESULT WINAPI Uri_GetHostType(IUri *iface, DWORD *pdwHostType)
3416 TRACE("(%p)->(%p)\n", iface, pdwHostType);
3417 return Uri_GetPropertyDWORD(iface, Uri_PROPERTY_HOST_TYPE, pdwHostType, 0);
3420 static HRESULT WINAPI Uri_GetPort(IUri *iface, DWORD *pdwPort)
3422 TRACE("(%p)->(%p)\n", iface, pdwPort);
3423 return Uri_GetPropertyDWORD(iface, Uri_PROPERTY_PORT, pdwPort, 0);
3426 static HRESULT WINAPI Uri_GetScheme(IUri *iface, DWORD *pdwScheme)
3428 Uri *This = URI_THIS(iface);
3429 TRACE("(%p)->(%p)\n", This, pdwScheme);
3430 return Uri_GetPropertyDWORD(iface, Uri_PROPERTY_SCHEME, pdwScheme, 0);
3433 static HRESULT WINAPI Uri_GetZone(IUri *iface, DWORD *pdwZone)
3435 TRACE("(%p)->(%p)\n", iface, pdwZone);
3436 return Uri_GetPropertyDWORD(iface, Uri_PROPERTY_ZONE,pdwZone, 0);
3439 static HRESULT WINAPI Uri_GetProperties(IUri *iface, DWORD *pdwProperties)
3441 Uri *This = URI_THIS(iface);
3442 FIXME("(%p)->(%p)\n", This, pdwProperties);
3444 if(!pdwProperties)
3445 return E_INVALIDARG;
3447 return E_NOTIMPL;
3450 static HRESULT WINAPI Uri_IsEqual(IUri *iface, IUri *pUri, BOOL *pfEqual)
3452 Uri *This = URI_THIS(iface);
3453 TRACE("(%p)->(%p %p)\n", This, pUri, pfEqual);
3455 if(!pfEqual)
3456 return E_POINTER;
3458 if(!pUri) {
3459 *pfEqual = FALSE;
3461 /* For some reason Windows returns S_OK here... */
3462 return S_OK;
3465 FIXME("(%p)->(%p %p)\n", This, pUri, pfEqual);
3466 return E_NOTIMPL;
3469 #undef URI_THIS
3471 static const IUriVtbl UriVtbl = {
3472 Uri_QueryInterface,
3473 Uri_AddRef,
3474 Uri_Release,
3475 Uri_GetPropertyBSTR,
3476 Uri_GetPropertyLength,
3477 Uri_GetPropertyDWORD,
3478 Uri_HasProperty,
3479 Uri_GetAbsoluteUri,
3480 Uri_GetAuthority,
3481 Uri_GetDisplayUri,
3482 Uri_GetDomain,
3483 Uri_GetExtension,
3484 Uri_GetFragment,
3485 Uri_GetHost,
3486 Uri_GetPassword,
3487 Uri_GetPath,
3488 Uri_GetPathAndQuery,
3489 Uri_GetQuery,
3490 Uri_GetRawUri,
3491 Uri_GetSchemeName,
3492 Uri_GetUserInfo,
3493 Uri_GetUserName,
3494 Uri_GetHostType,
3495 Uri_GetPort,
3496 Uri_GetScheme,
3497 Uri_GetZone,
3498 Uri_GetProperties,
3499 Uri_IsEqual
3502 /***********************************************************************
3503 * CreateUri (urlmon.@)
3505 HRESULT WINAPI CreateUri(LPCWSTR pwzURI, DWORD dwFlags, DWORD_PTR dwReserved, IUri **ppURI)
3507 Uri *ret;
3508 HRESULT hr;
3509 parse_data data;
3511 TRACE("(%s %x %x %p)\n", debugstr_w(pwzURI), dwFlags, (DWORD)dwReserved, ppURI);
3513 if(!ppURI)
3514 return E_INVALIDARG;
3516 if(!pwzURI) {
3517 *ppURI = NULL;
3518 return E_INVALIDARG;
3521 ret = heap_alloc(sizeof(Uri));
3522 if(!ret)
3523 return E_OUTOFMEMORY;
3525 ret->lpIUriVtbl = &UriVtbl;
3526 ret->ref = 1;
3528 /* Create a copy of pwzURI and store it as the raw_uri. */
3529 ret->raw_uri = SysAllocString(pwzURI);
3530 if(!ret->raw_uri) {
3531 heap_free(ret);
3532 return E_OUTOFMEMORY;
3535 memset(&data, 0, sizeof(parse_data));
3536 data.uri = ret->raw_uri;
3538 /* Validate and parse the URI into it's components. */
3539 if(!parse_uri(&data, dwFlags)) {
3540 /* Encountered an unsupported or invalid URI */
3541 SysFreeString(ret->raw_uri);
3542 heap_free(ret);
3543 *ppURI = NULL;
3544 return E_INVALIDARG;
3547 /* Canonicalize the URI. */
3548 hr = canonicalize_uri(&data, ret, dwFlags);
3549 if(FAILED(hr)) {
3550 SysFreeString(ret->raw_uri);
3551 heap_free(ret);
3552 *ppURI = NULL;
3553 return hr;
3556 *ppURI = URI(ret);
3557 return S_OK;
3560 #define URIBUILDER_THIS(iface) DEFINE_THIS(UriBuilder, IUriBuilder, iface)
3562 static HRESULT WINAPI UriBuilder_QueryInterface(IUriBuilder *iface, REFIID riid, void **ppv)
3564 UriBuilder *This = URIBUILDER_THIS(iface);
3566 if(IsEqualGUID(&IID_IUnknown, riid)) {
3567 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
3568 *ppv = URIBUILDER(This);
3569 }else if(IsEqualGUID(&IID_IUriBuilder, riid)) {
3570 TRACE("(%p)->(IID_IUri %p)\n", This, ppv);
3571 *ppv = URIBUILDER(This);
3572 }else {
3573 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
3574 *ppv = NULL;
3575 return E_NOINTERFACE;
3578 IUnknown_AddRef((IUnknown*)*ppv);
3579 return S_OK;
3582 static ULONG WINAPI UriBuilder_AddRef(IUriBuilder *iface)
3584 UriBuilder *This = URIBUILDER_THIS(iface);
3585 LONG ref = InterlockedIncrement(&This->ref);
3587 TRACE("(%p) ref=%d\n", This, ref);
3589 return ref;
3592 static ULONG WINAPI UriBuilder_Release(IUriBuilder *iface)
3594 UriBuilder *This = URIBUILDER_THIS(iface);
3595 LONG ref = InterlockedDecrement(&This->ref);
3597 TRACE("(%p) ref=%d\n", This, ref);
3599 if(!ref)
3600 heap_free(This);
3602 return ref;
3605 static HRESULT WINAPI UriBuilder_CreateUriSimple(IUriBuilder *iface,
3606 DWORD dwAllowEncodingPropertyMask,
3607 DWORD_PTR dwReserved,
3608 IUri **ppIUri)
3610 UriBuilder *This = URIBUILDER_THIS(iface);
3611 FIXME("(%p)->(%d %d %p)\n", This, dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
3612 return E_NOTIMPL;
3615 static HRESULT WINAPI UriBuilder_CreateUri(IUriBuilder *iface,
3616 DWORD dwCreateFlags,
3617 DWORD dwAllowEncodingPropertyMask,
3618 DWORD_PTR dwReserved,
3619 IUri **ppIUri)
3621 UriBuilder *This = URIBUILDER_THIS(iface);
3622 FIXME("(%p)->(0x%08x %d %d %p)\n", This, dwCreateFlags, dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
3623 return E_NOTIMPL;
3626 static HRESULT WINAPI UriBuilder_CreateUriWithFlags(IUriBuilder *iface,
3627 DWORD dwCreateFlags,
3628 DWORD dwUriBuilderFlags,
3629 DWORD dwAllowEncodingPropertyMask,
3630 DWORD_PTR dwReserved,
3631 IUri **ppIUri)
3633 UriBuilder *This = URIBUILDER_THIS(iface);
3634 FIXME("(%p)->(0x%08x 0x%08x %d %d %p)\n", This, dwCreateFlags, dwUriBuilderFlags,
3635 dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
3636 return E_NOTIMPL;
3639 static HRESULT WINAPI UriBuilder_GetIUri(IUriBuilder *iface, IUri **ppIUri)
3641 UriBuilder *This = URIBUILDER_THIS(iface);
3642 FIXME("(%p)->(%p)\n", This, ppIUri);
3643 return E_NOTIMPL;
3646 static HRESULT WINAPI UriBuilder_SetIUri(IUriBuilder *iface, IUri *pIUri)
3648 UriBuilder *This = URIBUILDER_THIS(iface);
3649 FIXME("(%p)->(%p)\n", This, pIUri);
3650 return E_NOTIMPL;
3653 static HRESULT WINAPI UriBuilder_GetFragment(IUriBuilder *iface, DWORD *pcchFragment, LPCWSTR *ppwzFragment)
3655 UriBuilder *This = URIBUILDER_THIS(iface);
3656 FIXME("(%p)->(%p %p)\n", This, pcchFragment, ppwzFragment);
3657 return E_NOTIMPL;
3660 static HRESULT WINAPI UriBuilder_GetHost(IUriBuilder *iface, DWORD *pcchHost, LPCWSTR *ppwzHost)
3662 UriBuilder *This = URIBUILDER_THIS(iface);
3663 FIXME("(%p)->(%p %p)\n", This, pcchHost, ppwzHost);
3664 return E_NOTIMPL;
3667 static HRESULT WINAPI UriBuilder_GetPassword(IUriBuilder *iface, DWORD *pcchPassword, LPCWSTR *ppwzPassword)
3669 UriBuilder *This = URIBUILDER_THIS(iface);
3670 FIXME("(%p)->(%p %p)\n", This, pcchPassword, ppwzPassword);
3671 return E_NOTIMPL;
3674 static HRESULT WINAPI UriBuilder_GetPath(IUriBuilder *iface, DWORD *pcchPath, LPCWSTR *ppwzPath)
3676 UriBuilder *This = URIBUILDER_THIS(iface);
3677 FIXME("(%p)->(%p %p)\n", This, pcchPath, ppwzPath);
3678 return E_NOTIMPL;
3681 static HRESULT WINAPI UriBuilder_GetPort(IUriBuilder *iface, BOOL *pfHasPort, DWORD *pdwPort)
3683 UriBuilder *This = URIBUILDER_THIS(iface);
3684 FIXME("(%p)->(%p %p)\n", This, pfHasPort, pdwPort);
3685 return E_NOTIMPL;
3688 static HRESULT WINAPI UriBuilder_GetQuery(IUriBuilder *iface, DWORD *pcchQuery, LPCWSTR *ppwzQuery)
3690 UriBuilder *This = URIBUILDER_THIS(iface);
3691 FIXME("(%p)->(%p %p)\n", This, pcchQuery, ppwzQuery);
3692 return E_NOTIMPL;
3695 static HRESULT WINAPI UriBuilder_GetSchemeName(IUriBuilder *iface, DWORD *pcchSchemeName, LPCWSTR *ppwzSchemeName)
3697 UriBuilder *This = URIBUILDER_THIS(iface);
3698 FIXME("(%p)->(%p %p)\n", This, pcchSchemeName, ppwzSchemeName);
3699 return E_NOTIMPL;
3702 static HRESULT WINAPI UriBuilder_GetUserName(IUriBuilder *iface, DWORD *pcchUserName, LPCWSTR *ppwzUserName)
3704 UriBuilder *This = URIBUILDER_THIS(iface);
3705 FIXME("(%p)->(%p %p)\n", This, pcchUserName, ppwzUserName);
3706 return E_NOTIMPL;
3709 static HRESULT WINAPI UriBuilder_SetFragment(IUriBuilder *iface, LPCWSTR pwzNewValue)
3711 UriBuilder *This = URIBUILDER_THIS(iface);
3712 FIXME("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
3713 return E_NOTIMPL;
3716 static HRESULT WINAPI UriBuilder_SetHost(IUriBuilder *iface, LPCWSTR pwzNewValue)
3718 UriBuilder *This = URIBUILDER_THIS(iface);
3719 FIXME("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
3720 return E_NOTIMPL;
3723 static HRESULT WINAPI UriBuilder_SetPassword(IUriBuilder *iface, LPCWSTR pwzNewValue)
3725 UriBuilder *This = URIBUILDER_THIS(iface);
3726 FIXME("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
3727 return E_NOTIMPL;
3730 static HRESULT WINAPI UriBuilder_SetPath(IUriBuilder *iface, LPCWSTR pwzNewValue)
3732 UriBuilder *This = URIBUILDER_THIS(iface);
3733 FIXME("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
3734 return E_NOTIMPL;
3737 static HRESULT WINAPI UriBuilder_SetPort(IUriBuilder *iface, BOOL fHasPort, DWORD dwNewValue)
3739 UriBuilder *This = URIBUILDER_THIS(iface);
3740 FIXME("(%p)->(%d %d)\n", This, fHasPort, dwNewValue);
3741 return E_NOTIMPL;
3744 static HRESULT WINAPI UriBuilder_SetQuery(IUriBuilder *iface, LPCWSTR pwzNewValue)
3746 UriBuilder *This = URIBUILDER_THIS(iface);
3747 FIXME("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
3748 return E_NOTIMPL;
3751 static HRESULT WINAPI UriBuilder_SetSchemeName(IUriBuilder *iface, LPCWSTR pwzNewValue)
3753 UriBuilder *This = URIBUILDER_THIS(iface);
3754 FIXME("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
3755 return E_NOTIMPL;
3758 static HRESULT WINAPI UriBuilder_SetUserName(IUriBuilder *iface, LPCWSTR pwzNewValue)
3760 UriBuilder *This = URIBUILDER_THIS(iface);
3761 FIXME("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
3762 return E_NOTIMPL;
3765 static HRESULT WINAPI UriBuilder_RemoveProperties(IUriBuilder *iface, DWORD dwPropertyMask)
3767 UriBuilder *This = URIBUILDER_THIS(iface);
3768 FIXME("(%p)->(0x%08x)\n", This, dwPropertyMask);
3769 return E_NOTIMPL;
3772 static HRESULT WINAPI UriBuilder_HasBeenModified(IUriBuilder *iface, BOOL *pfModified)
3774 UriBuilder *This = URIBUILDER_THIS(iface);
3775 FIXME("(%p)->(%p)\n", This, pfModified);
3776 return E_NOTIMPL;
3779 #undef URIBUILDER_THIS
3781 static const IUriBuilderVtbl UriBuilderVtbl = {
3782 UriBuilder_QueryInterface,
3783 UriBuilder_AddRef,
3784 UriBuilder_Release,
3785 UriBuilder_CreateUriSimple,
3786 UriBuilder_CreateUri,
3787 UriBuilder_CreateUriWithFlags,
3788 UriBuilder_GetIUri,
3789 UriBuilder_SetIUri,
3790 UriBuilder_GetFragment,
3791 UriBuilder_GetHost,
3792 UriBuilder_GetPassword,
3793 UriBuilder_GetPath,
3794 UriBuilder_GetPort,
3795 UriBuilder_GetQuery,
3796 UriBuilder_GetSchemeName,
3797 UriBuilder_GetUserName,
3798 UriBuilder_SetFragment,
3799 UriBuilder_SetHost,
3800 UriBuilder_SetPassword,
3801 UriBuilder_SetPath,
3802 UriBuilder_SetPort,
3803 UriBuilder_SetQuery,
3804 UriBuilder_SetSchemeName,
3805 UriBuilder_SetUserName,
3806 UriBuilder_RemoveProperties,
3807 UriBuilder_HasBeenModified,
3810 /***********************************************************************
3811 * CreateIUriBuilder (urlmon.@)
3813 HRESULT WINAPI CreateIUriBuilder(IUri *pIUri, DWORD dwFlags, DWORD_PTR dwReserved, IUriBuilder **ppIUriBuilder)
3815 UriBuilder *ret;
3817 TRACE("(%p %x %x %p)\n", pIUri, dwFlags, (DWORD)dwReserved, ppIUriBuilder);
3819 ret = heap_alloc(sizeof(UriBuilder));
3820 if(!ret)
3821 return E_OUTOFMEMORY;
3823 ret->lpIUriBuilderVtbl = &UriBuilderVtbl;
3824 ret->ref = 1;
3826 *ppIUriBuilder = URIBUILDER(ret);
3827 return S_OK;