d3dx9/tests: Implemented CloneMesh test.
[wine/multimedia.git] / dlls / urlmon / uri.c
blob2b0b9ad68d67d9f5fb1c2fadaf0b78cf48043d18
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 #include "strsafe.h"
28 #define UINT_MAX 0xffffffff
29 #define USHORT_MAX 0xffff
31 #define URI_DISPLAY_NO_ABSOLUTE_URI 0x1
32 #define URI_DISPLAY_NO_DEFAULT_PORT_AUTH 0x2
34 #define ALLOW_NULL_TERM_SCHEME 0x01
35 #define ALLOW_NULL_TERM_USER_NAME 0x02
36 #define ALLOW_NULL_TERM_PASSWORD 0x04
37 #define ALLOW_BRACKETLESS_IP_LITERAL 0x08
38 #define SKIP_IP_FUTURE_CHECK 0x10
39 #define IGNORE_PORT_DELIMITER 0x20
41 #define RAW_URI_FORCE_PORT_DISP 0x1
42 #define RAW_URI_CONVERT_TO_DOS_PATH 0x2
44 #define COMBINE_URI_FORCE_FLAG_USE 0x1
46 WINE_DEFAULT_DEBUG_CHANNEL(urlmon);
48 static const IID IID_IUriObj = {0x4b364760,0x9f51,0x11df,{0x98,0x1c,0x08,0x00,0x20,0x0c,0x9a,0x66}};
50 typedef struct {
51 IUri IUri_iface;
52 IUriBuilderFactory IUriBuilderFactory_iface;
54 LONG ref;
56 BSTR raw_uri;
58 /* Information about the canonicalized URI's buffer. */
59 WCHAR *canon_uri;
60 DWORD canon_size;
61 DWORD canon_len;
62 BOOL display_modifiers;
63 DWORD create_flags;
65 INT scheme_start;
66 DWORD scheme_len;
67 URL_SCHEME scheme_type;
69 INT userinfo_start;
70 DWORD userinfo_len;
71 INT userinfo_split;
73 INT host_start;
74 DWORD host_len;
75 Uri_HOST_TYPE host_type;
77 INT port_offset;
78 DWORD port;
79 BOOL has_port;
81 INT authority_start;
82 DWORD authority_len;
84 INT domain_offset;
86 INT path_start;
87 DWORD path_len;
88 INT extension_offset;
90 INT query_start;
91 DWORD query_len;
93 INT fragment_start;
94 DWORD fragment_len;
95 } Uri;
97 typedef struct {
98 IUriBuilder IUriBuilder_iface;
99 LONG ref;
101 Uri *uri;
102 DWORD modified_props;
104 WCHAR *fragment;
105 DWORD fragment_len;
107 WCHAR *host;
108 DWORD host_len;
110 WCHAR *password;
111 DWORD password_len;
113 WCHAR *path;
114 DWORD path_len;
116 BOOL has_port;
117 DWORD port;
119 WCHAR *query;
120 DWORD query_len;
122 WCHAR *scheme;
123 DWORD scheme_len;
125 WCHAR *username;
126 DWORD username_len;
127 } UriBuilder;
129 typedef struct {
130 const WCHAR *str;
131 DWORD len;
132 } h16;
134 typedef struct {
135 /* IPv6 addresses can hold up to 8 h16 components. */
136 h16 components[8];
137 DWORD h16_count;
139 /* An IPv6 can have 1 elision ("::"). */
140 const WCHAR *elision;
142 /* An IPv6 can contain 1 IPv4 address as the last 32bits of the address. */
143 const WCHAR *ipv4;
144 DWORD ipv4_len;
146 INT components_size;
147 INT elision_size;
148 } ipv6_address;
150 typedef struct {
151 BSTR uri;
153 BOOL is_relative;
154 BOOL is_opaque;
155 BOOL has_implicit_scheme;
156 BOOL has_implicit_ip;
157 UINT implicit_ipv4;
158 BOOL must_have_path;
160 const WCHAR *scheme;
161 DWORD scheme_len;
162 URL_SCHEME scheme_type;
164 const WCHAR *username;
165 DWORD username_len;
167 const WCHAR *password;
168 DWORD password_len;
170 const WCHAR *host;
171 DWORD host_len;
172 Uri_HOST_TYPE host_type;
174 BOOL has_ipv6;
175 ipv6_address ipv6_address;
177 BOOL has_port;
178 const WCHAR *port;
179 DWORD port_len;
180 DWORD port_value;
182 const WCHAR *path;
183 DWORD path_len;
185 const WCHAR *query;
186 DWORD query_len;
188 const WCHAR *fragment;
189 DWORD fragment_len;
190 } parse_data;
192 static const CHAR hexDigits[] = "0123456789ABCDEF";
194 /* List of scheme types/scheme names that are recognized by the IUri interface as of IE 7. */
195 static const struct {
196 URL_SCHEME scheme;
197 WCHAR scheme_name[16];
198 } recognized_schemes[] = {
199 {URL_SCHEME_FTP, {'f','t','p',0}},
200 {URL_SCHEME_HTTP, {'h','t','t','p',0}},
201 {URL_SCHEME_GOPHER, {'g','o','p','h','e','r',0}},
202 {URL_SCHEME_MAILTO, {'m','a','i','l','t','o',0}},
203 {URL_SCHEME_NEWS, {'n','e','w','s',0}},
204 {URL_SCHEME_NNTP, {'n','n','t','p',0}},
205 {URL_SCHEME_TELNET, {'t','e','l','n','e','t',0}},
206 {URL_SCHEME_WAIS, {'w','a','i','s',0}},
207 {URL_SCHEME_FILE, {'f','i','l','e',0}},
208 {URL_SCHEME_MK, {'m','k',0}},
209 {URL_SCHEME_HTTPS, {'h','t','t','p','s',0}},
210 {URL_SCHEME_SHELL, {'s','h','e','l','l',0}},
211 {URL_SCHEME_SNEWS, {'s','n','e','w','s',0}},
212 {URL_SCHEME_LOCAL, {'l','o','c','a','l',0}},
213 {URL_SCHEME_JAVASCRIPT, {'j','a','v','a','s','c','r','i','p','t',0}},
214 {URL_SCHEME_VBSCRIPT, {'v','b','s','c','r','i','p','t',0}},
215 {URL_SCHEME_ABOUT, {'a','b','o','u','t',0}},
216 {URL_SCHEME_RES, {'r','e','s',0}},
217 {URL_SCHEME_MSSHELLROOTED, {'m','s','-','s','h','e','l','l','-','r','o','o','t','e','d',0}},
218 {URL_SCHEME_MSSHELLIDLIST, {'m','s','-','s','h','e','l','l','-','i','d','l','i','s','t',0}},
219 {URL_SCHEME_MSHELP, {'h','c','p',0}},
220 {URL_SCHEME_WILDCARD, {'*',0}}
223 /* List of default ports Windows recognizes. */
224 static const struct {
225 URL_SCHEME scheme;
226 USHORT port;
227 } default_ports[] = {
228 {URL_SCHEME_FTP, 21},
229 {URL_SCHEME_HTTP, 80},
230 {URL_SCHEME_GOPHER, 70},
231 {URL_SCHEME_NNTP, 119},
232 {URL_SCHEME_TELNET, 23},
233 {URL_SCHEME_WAIS, 210},
234 {URL_SCHEME_HTTPS, 443},
237 /* List of 3-character top level domain names Windows seems to recognize.
238 * There might be more, but, these are the only ones I've found so far.
240 static const struct {
241 WCHAR tld_name[4];
242 } recognized_tlds[] = {
243 {{'c','o','m',0}},
244 {{'e','d','u',0}},
245 {{'g','o','v',0}},
246 {{'i','n','t',0}},
247 {{'m','i','l',0}},
248 {{'n','e','t',0}},
249 {{'o','r','g',0}}
252 static Uri *get_uri_obj(IUri *uri)
254 Uri *ret;
255 HRESULT hres;
257 hres = IUri_QueryInterface(uri, &IID_IUriObj, (void**)&ret);
258 return SUCCEEDED(hres) ? ret : NULL;
261 static inline BOOL is_alpha(WCHAR val) {
262 return ((val >= 'a' && val <= 'z') || (val >= 'A' && val <= 'Z'));
265 static inline BOOL is_num(WCHAR val) {
266 return (val >= '0' && val <= '9');
269 static inline BOOL is_drive_path(const WCHAR *str) {
270 return (is_alpha(str[0]) && (str[1] == ':' || str[1] == '|'));
273 static inline BOOL is_unc_path(const WCHAR *str) {
274 return (str[0] == '\\' && str[0] == '\\');
277 static inline BOOL is_forbidden_dos_path_char(WCHAR val) {
278 return (val == '>' || val == '<' || val == '\"');
281 /* A URI is implicitly a file path if it begins with
282 * a drive letter (e.g. X:) or starts with "\\" (UNC path).
284 static inline BOOL is_implicit_file_path(const WCHAR *str) {
285 return (is_unc_path(str) || (is_alpha(str[0]) && str[1] == ':'));
288 /* Checks if the URI is a hierarchical URI. A hierarchical
289 * URI is one that has "//" after the scheme.
291 static BOOL check_hierarchical(const WCHAR **ptr) {
292 const WCHAR *start = *ptr;
294 if(**ptr != '/')
295 return FALSE;
297 ++(*ptr);
298 if(**ptr != '/') {
299 *ptr = start;
300 return FALSE;
303 ++(*ptr);
304 return TRUE;
307 /* unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~" */
308 static inline BOOL is_unreserved(WCHAR val) {
309 return (is_alpha(val) || is_num(val) || val == '-' || val == '.' ||
310 val == '_' || val == '~');
313 /* sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
314 * / "*" / "+" / "," / ";" / "="
316 static inline BOOL is_subdelim(WCHAR val) {
317 return (val == '!' || val == '$' || val == '&' ||
318 val == '\'' || val == '(' || val == ')' ||
319 val == '*' || val == '+' || val == ',' ||
320 val == ';' || val == '=');
323 /* gen-delims = ":" / "/" / "?" / "#" / "[" / "]" / "@" */
324 static inline BOOL is_gendelim(WCHAR val) {
325 return (val == ':' || val == '/' || val == '?' ||
326 val == '#' || val == '[' || val == ']' ||
327 val == '@');
330 /* Characters that delimit the end of the authority
331 * section of a URI. Sometimes a '\\' is considered
332 * an authority delimiter.
334 static inline BOOL is_auth_delim(WCHAR val, BOOL acceptSlash) {
335 return (val == '#' || val == '/' || val == '?' ||
336 val == '\0' || (acceptSlash && val == '\\'));
339 /* reserved = gen-delims / sub-delims */
340 static inline BOOL is_reserved(WCHAR val) {
341 return (is_subdelim(val) || is_gendelim(val));
344 static inline BOOL is_hexdigit(WCHAR val) {
345 return ((val >= 'a' && val <= 'f') ||
346 (val >= 'A' && val <= 'F') ||
347 (val >= '0' && val <= '9'));
350 static inline BOOL is_path_delim(WCHAR val) {
351 return (!val || val == '#' || val == '?');
354 static inline BOOL is_slash(WCHAR c)
356 return c == '/' || c == '\\';
359 static BOOL is_default_port(URL_SCHEME scheme, DWORD port) {
360 DWORD i;
362 for(i = 0; i < sizeof(default_ports)/sizeof(default_ports[0]); ++i) {
363 if(default_ports[i].scheme == scheme && default_ports[i].port)
364 return TRUE;
367 return FALSE;
370 /* List of schemes types Windows seems to expect to be hierarchical. */
371 static inline BOOL is_hierarchical_scheme(URL_SCHEME type) {
372 return(type == URL_SCHEME_HTTP || type == URL_SCHEME_FTP ||
373 type == URL_SCHEME_GOPHER || type == URL_SCHEME_NNTP ||
374 type == URL_SCHEME_TELNET || type == URL_SCHEME_WAIS ||
375 type == URL_SCHEME_FILE || type == URL_SCHEME_HTTPS ||
376 type == URL_SCHEME_RES);
379 /* Checks if 'flags' contains an invalid combination of Uri_CREATE flags. */
380 static inline BOOL has_invalid_flag_combination(DWORD flags) {
381 return((flags & Uri_CREATE_DECODE_EXTRA_INFO && flags & Uri_CREATE_NO_DECODE_EXTRA_INFO) ||
382 (flags & Uri_CREATE_CANONICALIZE && flags & Uri_CREATE_NO_CANONICALIZE) ||
383 (flags & Uri_CREATE_CRACK_UNKNOWN_SCHEMES && flags & Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES) ||
384 (flags & Uri_CREATE_PRE_PROCESS_HTML_URI && flags & Uri_CREATE_NO_PRE_PROCESS_HTML_URI) ||
385 (flags & Uri_CREATE_IE_SETTINGS && flags & Uri_CREATE_NO_IE_SETTINGS));
388 /* Applies each default Uri_CREATE flags to 'flags' if it
389 * doesn't cause a flag conflict.
391 static void apply_default_flags(DWORD *flags) {
392 if(!(*flags & Uri_CREATE_NO_CANONICALIZE))
393 *flags |= Uri_CREATE_CANONICALIZE;
394 if(!(*flags & Uri_CREATE_NO_DECODE_EXTRA_INFO))
395 *flags |= Uri_CREATE_DECODE_EXTRA_INFO;
396 if(!(*flags & Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES))
397 *flags |= Uri_CREATE_CRACK_UNKNOWN_SCHEMES;
398 if(!(*flags & Uri_CREATE_NO_PRE_PROCESS_HTML_URI))
399 *flags |= Uri_CREATE_PRE_PROCESS_HTML_URI;
400 if(!(*flags & Uri_CREATE_IE_SETTINGS))
401 *flags |= Uri_CREATE_NO_IE_SETTINGS;
404 /* Determines if the URI is hierarchical using the information already parsed into
405 * data and using the current location of parsing in the URI string.
407 * Windows considers a URI hierarchical if one of the following is true:
408 * A.) It's a wildcard scheme.
409 * B.) It's an implicit file scheme.
410 * C.) It's a known hierarchical scheme and it has two '\\' after the scheme name.
411 * (the '\\' will be converted into "//" during canonicalization).
412 * D.) It's not a relative URI and "//" appears after the scheme name.
414 static inline BOOL is_hierarchical_uri(const WCHAR **ptr, const parse_data *data) {
415 const WCHAR *start = *ptr;
417 if(data->scheme_type == URL_SCHEME_WILDCARD)
418 return TRUE;
419 else if(data->scheme_type == URL_SCHEME_FILE && data->has_implicit_scheme)
420 return TRUE;
421 else if(is_hierarchical_scheme(data->scheme_type) && (*ptr)[0] == '\\' && (*ptr)[1] == '\\') {
422 *ptr += 2;
423 return TRUE;
424 } else if(!data->is_relative && check_hierarchical(ptr))
425 return TRUE;
427 *ptr = start;
428 return FALSE;
431 /* Checks if the two Uri's are logically equivalent. It's a simple
432 * comparison, since they are both of type Uri, and it can access
433 * the properties of each Uri directly without the need to go
434 * through the "IUri_Get*" interface calls.
436 static BOOL are_equal_simple(const Uri *a, const Uri *b) {
437 if(a->scheme_type == b->scheme_type) {
438 const BOOL known_scheme = a->scheme_type != URL_SCHEME_UNKNOWN;
439 const BOOL are_hierarchical =
440 (a->authority_start > -1 && b->authority_start > -1);
442 if(a->scheme_type == URL_SCHEME_FILE) {
443 if(a->canon_len == b->canon_len)
444 return !StrCmpIW(a->canon_uri, b->canon_uri);
447 /* Only compare the scheme names (if any) if their unknown scheme types. */
448 if(!known_scheme) {
449 if((a->scheme_start > -1 && b->scheme_start > -1) &&
450 (a->scheme_len == b->scheme_len)) {
451 /* Make sure the schemes are the same. */
452 if(StrCmpNW(a->canon_uri+a->scheme_start, b->canon_uri+b->scheme_start, a->scheme_len))
453 return FALSE;
454 } else if(a->scheme_len != b->scheme_len)
455 /* One of the Uri's has a scheme name, while the other doesn't. */
456 return FALSE;
459 /* If they have a userinfo component, perform case sensitive compare. */
460 if((a->userinfo_start > -1 && b->userinfo_start > -1) &&
461 (a->userinfo_len == b->userinfo_len)) {
462 if(StrCmpNW(a->canon_uri+a->userinfo_start, b->canon_uri+b->userinfo_start, a->userinfo_len))
463 return FALSE;
464 } else if(a->userinfo_len != b->userinfo_len)
465 /* One of the Uri's had a userinfo, while the other one doesn't. */
466 return FALSE;
468 /* Check if they have a host name. */
469 if((a->host_start > -1 && b->host_start > -1) &&
470 (a->host_len == b->host_len)) {
471 /* Perform a case insensitive compare if they are a known scheme type. */
472 if(known_scheme) {
473 if(StrCmpNIW(a->canon_uri+a->host_start, b->canon_uri+b->host_start, a->host_len))
474 return FALSE;
475 } else if(StrCmpNW(a->canon_uri+a->host_start, b->canon_uri+b->host_start, a->host_len))
476 return FALSE;
477 } else if(a->host_len != b->host_len)
478 /* One of the Uri's had a host, while the other one didn't. */
479 return FALSE;
481 if(a->has_port && b->has_port) {
482 if(a->port != b->port)
483 return FALSE;
484 } else if(a->has_port || b->has_port)
485 /* One had a port, while the other one didn't. */
486 return FALSE;
488 /* Windows is weird with how it handles paths. For example
489 * One URI could be "http://google.com" (after canonicalization)
490 * and one could be "http://google.com/" and the IsEqual function
491 * would still evaluate to TRUE, but, only if they are both hierarchical
492 * URIs.
494 if((a->path_start > -1 && b->path_start > -1) &&
495 (a->path_len == b->path_len)) {
496 if(StrCmpNW(a->canon_uri+a->path_start, b->canon_uri+b->path_start, a->path_len))
497 return FALSE;
498 } else if(are_hierarchical && a->path_len == -1 && b->path_len == 0) {
499 if(*(a->canon_uri+a->path_start) != '/')
500 return FALSE;
501 } else if(are_hierarchical && b->path_len == 1 && a->path_len == 0) {
502 if(*(b->canon_uri+b->path_start) != '/')
503 return FALSE;
504 } else if(a->path_len != b->path_len)
505 return FALSE;
507 /* Compare the query strings of the two URIs. */
508 if((a->query_start > -1 && b->query_start > -1) &&
509 (a->query_len == b->query_len)) {
510 if(StrCmpNW(a->canon_uri+a->query_start, b->canon_uri+b->query_start, a->query_len))
511 return FALSE;
512 } else if(a->query_len != b->query_len)
513 return FALSE;
515 if((a->fragment_start > -1 && b->fragment_start > -1) &&
516 (a->fragment_len == b->fragment_len)) {
517 if(StrCmpNW(a->canon_uri+a->fragment_start, b->canon_uri+b->fragment_start, a->fragment_len))
518 return FALSE;
519 } else if(a->fragment_len != b->fragment_len)
520 return FALSE;
522 /* If we get here, the two URIs are equivalent. */
523 return TRUE;
526 return FALSE;
529 /* Computes the size of the given IPv6 address.
530 * Each h16 component is 16 bits. If there is an IPv4 address, it's
531 * 32 bits. If there's an elision it can be 16 to 128 bits, depending
532 * on the number of other components.
534 * Modeled after google-url's CheckIPv6ComponentsSize function
536 static void compute_ipv6_comps_size(ipv6_address *address) {
537 address->components_size = address->h16_count * 2;
539 if(address->ipv4)
540 /* IPv4 address is 4 bytes. */
541 address->components_size += 4;
543 if(address->elision) {
544 /* An elision can be anywhere from 2 bytes up to 16 bytes.
545 * Its size depends on the size of the h16 and IPv4 components.
547 address->elision_size = 16 - address->components_size;
548 if(address->elision_size < 2)
549 address->elision_size = 2;
550 } else
551 address->elision_size = 0;
554 /* Taken from dlls/jscript/lex.c */
555 static int hex_to_int(WCHAR val) {
556 if(val >= '0' && val <= '9')
557 return val - '0';
558 else if(val >= 'a' && val <= 'f')
559 return val - 'a' + 10;
560 else if(val >= 'A' && val <= 'F')
561 return val - 'A' + 10;
563 return -1;
566 /* Helper function for converting a percent encoded string
567 * representation of a WCHAR value into its actual WCHAR value. If
568 * the two characters following the '%' aren't valid hex values then
569 * this function returns the NULL character.
571 * E.g.
572 * "%2E" will result in '.' being returned by this function.
574 static WCHAR decode_pct_val(const WCHAR *ptr) {
575 WCHAR ret = '\0';
577 if(*ptr == '%' && is_hexdigit(*(ptr + 1)) && is_hexdigit(*(ptr + 2))) {
578 INT a = hex_to_int(*(ptr + 1));
579 INT b = hex_to_int(*(ptr + 2));
581 ret = a << 4;
582 ret += b;
585 return ret;
588 /* Helper function for percent encoding a given character
589 * and storing the encoded value into a given buffer (dest).
591 * It's up to the calling function to ensure that there is
592 * at least enough space in 'dest' for the percent encoded
593 * value to be stored (so dest + 3 spaces available).
595 static inline void pct_encode_val(WCHAR val, WCHAR *dest) {
596 dest[0] = '%';
597 dest[1] = hexDigits[(val >> 4) & 0xf];
598 dest[2] = hexDigits[val & 0xf];
601 /* Attempts to parse the domain name from the host.
603 * This function also includes the Top-level Domain (TLD) name
604 * of the host when it tries to find the domain name. If it finds
605 * a valid domain name it will assign 'domain_start' the offset
606 * into 'host' where the domain name starts.
608 * It's implied that if there is a domain name its range is:
609 * [host+domain_start, host+host_len).
611 void find_domain_name(const WCHAR *host, DWORD host_len,
612 INT *domain_start) {
613 const WCHAR *last_tld, *sec_last_tld, *end;
615 end = host+host_len-1;
617 *domain_start = -1;
619 /* There has to be at least enough room for a '.' followed by a
620 * 3-character TLD for a domain to even exist in the host name.
622 if(host_len < 4)
623 return;
625 last_tld = memrchrW(host, '.', host_len);
626 if(!last_tld)
627 /* http://hostname -> has no domain name. */
628 return;
630 sec_last_tld = memrchrW(host, '.', last_tld-host);
631 if(!sec_last_tld) {
632 /* If the '.' is at the beginning of the host there
633 * has to be at least 3 characters in the TLD for it
634 * to be valid.
635 * Ex: .com -> .com as the domain name.
636 * .co -> has no domain name.
638 if(last_tld-host == 0) {
639 if(end-(last_tld-1) < 3)
640 return;
641 } else if(last_tld-host == 3) {
642 DWORD i;
644 /* If there are three characters in front of last_tld and
645 * they are on the list of recognized TLDs, then this
646 * host doesn't have a domain (since the host only contains
647 * a TLD name.
648 * Ex: edu.uk -> has no domain name.
649 * foo.uk -> foo.uk as the domain name.
651 for(i = 0; i < sizeof(recognized_tlds)/sizeof(recognized_tlds[0]); ++i) {
652 if(!StrCmpNIW(host, recognized_tlds[i].tld_name, 3))
653 return;
655 } else if(last_tld-host < 3)
656 /* Anything less than 3 characters is considered part
657 * of the TLD name.
658 * Ex: ak.uk -> Has no domain name.
660 return;
662 /* Otherwise the domain name is the whole host name. */
663 *domain_start = 0;
664 } else if(end+1-last_tld > 3) {
665 /* If the last_tld has more than 3 characters, then it's automatically
666 * considered the TLD of the domain name.
667 * Ex: www.winehq.org.uk.test -> uk.test as the domain name.
669 *domain_start = (sec_last_tld+1)-host;
670 } else if(last_tld - (sec_last_tld+1) < 4) {
671 DWORD i;
672 /* If the sec_last_tld is 3 characters long it HAS to be on the list of
673 * recognized to still be considered part of the TLD name, otherwise
674 * its considered the domain name.
675 * Ex: www.google.com.uk -> google.com.uk as the domain name.
676 * www.google.foo.uk -> foo.uk as the domain name.
678 if(last_tld - (sec_last_tld+1) == 3) {
679 for(i = 0; i < sizeof(recognized_tlds)/sizeof(recognized_tlds[0]); ++i) {
680 if(!StrCmpNIW(sec_last_tld+1, recognized_tlds[i].tld_name, 3)) {
681 const WCHAR *domain = memrchrW(host, '.', sec_last_tld-host);
683 if(!domain)
684 *domain_start = 0;
685 else
686 *domain_start = (domain+1) - host;
687 TRACE("Found domain name %s\n", debugstr_wn(host+*domain_start,
688 (host+host_len)-(host+*domain_start)));
689 return;
693 *domain_start = (sec_last_tld+1)-host;
694 } else {
695 /* Since the sec_last_tld is less than 3 characters it's considered
696 * part of the TLD.
697 * Ex: www.google.fo.uk -> google.fo.uk as the domain name.
699 const WCHAR *domain = memrchrW(host, '.', sec_last_tld-host);
701 if(!domain)
702 *domain_start = 0;
703 else
704 *domain_start = (domain+1) - host;
706 } else {
707 /* The second to last TLD has more than 3 characters making it
708 * the domain name.
709 * Ex: www.google.test.us -> test.us as the domain name.
711 *domain_start = (sec_last_tld+1)-host;
714 TRACE("Found domain name %s\n", debugstr_wn(host+*domain_start,
715 (host+host_len)-(host+*domain_start)));
718 /* Removes the dot segments from a hierarchical URIs path component. This
719 * function performs the removal in place.
721 * This function returns the new length of the path string.
723 static DWORD remove_dot_segments(WCHAR *path, DWORD path_len) {
724 WCHAR *out = path;
725 const WCHAR *in = out;
726 const WCHAR *end = out + path_len;
727 DWORD len;
729 while(in < end) {
730 /* Move the first path segment in the input buffer to the end of
731 * the output buffer, and any subsequent characters up to, including
732 * the next "/" character (if any) or the end of the input buffer.
734 while(in < end && !is_slash(*in))
735 *out++ = *in++;
736 if(in == end)
737 break;
738 *out++ = *in++;
740 while(in < end) {
741 if(*in != '.')
742 break;
744 /* Handle ending "/." */
745 if(in + 1 == end) {
746 ++in;
747 break;
750 /* Handle "/./" */
751 if(is_slash(in[1])) {
752 in += 2;
753 continue;
756 /* If we don't have "/../" or ending "/.." */
757 if(in[1] != '.' || (in + 2 != end && !is_slash(in[2])))
758 break;
760 /* Find the slash preceding out pointer and move out pointer to it */
761 if(out > path+1 && is_slash(*--out))
762 --out;
763 while(out > path && !is_slash(*(--out)));
764 if(is_slash(*out))
765 ++out;
766 in += 2;
767 if(in != end)
768 ++in;
772 len = out - path;
773 TRACE("(%p %d): Path after dot segments removed %s len=%d\n", path, path_len,
774 debugstr_wn(path, len), len);
775 return len;
778 /* Attempts to find the file extension in a given path. */
779 static INT find_file_extension(const WCHAR *path, DWORD path_len) {
780 const WCHAR *end;
782 for(end = path+path_len-1; end >= path && *end != '/' && *end != '\\'; --end) {
783 if(*end == '.')
784 return end-path;
787 return -1;
790 /* Computes the location where the elision should occur in the IPv6
791 * address using the numerical values of each component stored in
792 * 'values'. If the address shouldn't contain an elision then 'index'
793 * is assigned -1 as its value. Otherwise 'index' will contain the
794 * starting index (into values) where the elision should be, and 'count'
795 * will contain the number of cells the elision covers.
797 * NOTES:
798 * Windows will expand an elision if the elision only represents one h16
799 * component of the address.
801 * Ex: [1::2:3:4:5:6:7] -> [1:0:2:3:4:5:6:7]
803 * If the IPv6 address contains an IPv4 address, the IPv4 address is also
804 * considered for being included as part of an elision if all its components
805 * are zeros.
807 * Ex: [1:2:3:4:5:6:0.0.0.0] -> [1:2:3:4:5:6::]
809 static void compute_elision_location(const ipv6_address *address, const USHORT values[8],
810 INT *index, DWORD *count) {
811 DWORD i, max_len, cur_len;
812 INT max_index, cur_index;
814 max_len = cur_len = 0;
815 max_index = cur_index = -1;
816 for(i = 0; i < 8; ++i) {
817 BOOL check_ipv4 = (address->ipv4 && i == 6);
818 BOOL is_end = (check_ipv4 || i == 7);
820 if(check_ipv4) {
821 /* Check if the IPv4 address contains only zeros. */
822 if(values[i] == 0 && values[i+1] == 0) {
823 if(cur_index == -1)
824 cur_index = i;
826 cur_len += 2;
827 ++i;
829 } else if(values[i] == 0) {
830 if(cur_index == -1)
831 cur_index = i;
833 ++cur_len;
836 if(is_end || values[i] != 0) {
837 /* We only consider it for an elision if it's
838 * more than 1 component long.
840 if(cur_len > 1 && cur_len > max_len) {
841 /* Found the new elision location. */
842 max_len = cur_len;
843 max_index = cur_index;
846 /* Reset the current range for the next range of zeros. */
847 cur_index = -1;
848 cur_len = 0;
852 *index = max_index;
853 *count = max_len;
856 /* Removes all the leading and trailing white spaces or
857 * control characters from the URI and removes all control
858 * characters inside of the URI string.
860 static BSTR pre_process_uri(LPCWSTR uri) {
861 const WCHAR *start, *end, *ptr;
862 WCHAR *ptr2;
863 DWORD len;
864 BSTR ret;
866 start = uri;
867 /* Skip leading controls and whitespace. */
868 while(*start && (iscntrlW(*start) || isspaceW(*start))) ++start;
870 /* URI consisted only of control/whitespace. */
871 if(!*start)
872 return SysAllocStringLen(NULL, 0);
874 end = start + strlenW(start);
875 while(--end > start && (iscntrlW(*end) || isspaceW(*end)));
877 len = ++end - start;
878 for(ptr = start; ptr < end; ptr++) {
879 if(iscntrlW(*ptr))
880 len--;
883 ret = SysAllocStringLen(NULL, len);
884 if(!ret)
885 return NULL;
887 for(ptr = start, ptr2=ret; ptr < end; ptr++) {
888 if(!iscntrlW(*ptr))
889 *ptr2++ = *ptr;
892 return ret;
895 /* Converts the specified IPv4 address into an uint value.
897 * This function assumes that the IPv4 address has already been validated.
899 static UINT ipv4toui(const WCHAR *ip, DWORD len) {
900 UINT ret = 0;
901 DWORD comp_value = 0;
902 const WCHAR *ptr;
904 for(ptr = ip; ptr < ip+len; ++ptr) {
905 if(*ptr == '.') {
906 ret <<= 8;
907 ret += comp_value;
908 comp_value = 0;
909 } else
910 comp_value = comp_value*10 + (*ptr-'0');
913 ret <<= 8;
914 ret += comp_value;
916 return ret;
919 /* Converts an IPv4 address in numerical form into its fully qualified
920 * string form. This function returns the number of characters written
921 * to 'dest'. If 'dest' is NULL this function will return the number of
922 * characters that would have been written.
924 * It's up to the caller to ensure there's enough space in 'dest' for the
925 * address.
927 static DWORD ui2ipv4(WCHAR *dest, UINT address) {
928 static const WCHAR formatW[] =
929 {'%','u','.','%','u','.','%','u','.','%','u',0};
930 DWORD ret = 0;
931 UCHAR digits[4];
933 digits[0] = (address >> 24) & 0xff;
934 digits[1] = (address >> 16) & 0xff;
935 digits[2] = (address >> 8) & 0xff;
936 digits[3] = address & 0xff;
938 if(!dest) {
939 WCHAR tmp[16];
940 ret = sprintfW(tmp, formatW, digits[0], digits[1], digits[2], digits[3]);
941 } else
942 ret = sprintfW(dest, formatW, digits[0], digits[1], digits[2], digits[3]);
944 return ret;
947 static DWORD ui2str(WCHAR *dest, UINT value) {
948 static const WCHAR formatW[] = {'%','u',0};
949 DWORD ret = 0;
951 if(!dest) {
952 WCHAR tmp[11];
953 ret = sprintfW(tmp, formatW, value);
954 } else
955 ret = sprintfW(dest, formatW, value);
957 return ret;
960 /* Converts a h16 component (from an IPv6 address) into its
961 * numerical value.
963 * This function assumes that the h16 component has already been validated.
965 static USHORT h16tous(h16 component) {
966 DWORD i;
967 USHORT ret = 0;
969 for(i = 0; i < component.len; ++i) {
970 ret <<= 4;
971 ret += hex_to_int(component.str[i]);
974 return ret;
977 /* Converts an IPv6 address into its 128 bits (16 bytes) numerical value.
979 * This function assumes that the ipv6_address has already been validated.
981 static BOOL ipv6_to_number(const ipv6_address *address, USHORT number[8]) {
982 DWORD i, cur_component = 0;
983 BOOL already_passed_elision = FALSE;
985 for(i = 0; i < address->h16_count; ++i) {
986 if(address->elision) {
987 if(address->components[i].str > address->elision && !already_passed_elision) {
988 /* Means we just passed the elision and need to add its values to
989 * 'number' before we do anything else.
991 DWORD j = 0;
992 for(j = 0; j < address->elision_size; j+=2)
993 number[cur_component++] = 0;
995 already_passed_elision = TRUE;
999 number[cur_component++] = h16tous(address->components[i]);
1002 /* Case when the elision appears after the h16 components. */
1003 if(!already_passed_elision && address->elision) {
1004 for(i = 0; i < address->elision_size; i+=2)
1005 number[cur_component++] = 0;
1008 if(address->ipv4) {
1009 UINT value = ipv4toui(address->ipv4, address->ipv4_len);
1011 if(cur_component != 6) {
1012 ERR("(%p %p): Failed sanity check with %d\n", address, number, cur_component);
1013 return FALSE;
1016 number[cur_component++] = (value >> 16) & 0xffff;
1017 number[cur_component] = value & 0xffff;
1020 return TRUE;
1023 /* Checks if the characters pointed to by 'ptr' are
1024 * a percent encoded data octet.
1026 * pct-encoded = "%" HEXDIG HEXDIG
1028 static BOOL check_pct_encoded(const WCHAR **ptr) {
1029 const WCHAR *start = *ptr;
1031 if(**ptr != '%')
1032 return FALSE;
1034 ++(*ptr);
1035 if(!is_hexdigit(**ptr)) {
1036 *ptr = start;
1037 return FALSE;
1040 ++(*ptr);
1041 if(!is_hexdigit(**ptr)) {
1042 *ptr = start;
1043 return FALSE;
1046 ++(*ptr);
1047 return TRUE;
1050 /* dec-octet = DIGIT ; 0-9
1051 * / %x31-39 DIGIT ; 10-99
1052 * / "1" 2DIGIT ; 100-199
1053 * / "2" %x30-34 DIGIT ; 200-249
1054 * / "25" %x30-35 ; 250-255
1056 static BOOL check_dec_octet(const WCHAR **ptr) {
1057 const WCHAR *c1, *c2, *c3;
1059 c1 = *ptr;
1060 /* A dec-octet must be at least 1 digit long. */
1061 if(*c1 < '0' || *c1 > '9')
1062 return FALSE;
1064 ++(*ptr);
1066 c2 = *ptr;
1067 /* Since the 1-digit requirement was met, it doesn't
1068 * matter if this is a DIGIT value, it's considered a
1069 * dec-octet.
1071 if(*c2 < '0' || *c2 > '9')
1072 return TRUE;
1074 ++(*ptr);
1076 c3 = *ptr;
1077 /* Same explanation as above. */
1078 if(*c3 < '0' || *c3 > '9')
1079 return TRUE;
1081 /* Anything > 255 isn't a valid IP dec-octet. */
1082 if(*c1 >= '2' && *c2 >= '5' && *c3 >= '5') {
1083 *ptr = c1;
1084 return FALSE;
1087 ++(*ptr);
1088 return TRUE;
1091 /* Checks if there is an implicit IPv4 address in the host component of the URI.
1092 * The max value of an implicit IPv4 address is UINT_MAX.
1094 * Ex:
1095 * "234567" would be considered an implicit IPv4 address.
1097 static BOOL check_implicit_ipv4(const WCHAR **ptr, UINT *val) {
1098 const WCHAR *start = *ptr;
1099 ULONGLONG ret = 0;
1100 *val = 0;
1102 while(is_num(**ptr)) {
1103 ret = ret*10 + (**ptr - '0');
1105 if(ret > UINT_MAX) {
1106 *ptr = start;
1107 return FALSE;
1109 ++(*ptr);
1112 if(*ptr == start)
1113 return FALSE;
1115 *val = ret;
1116 return TRUE;
1119 /* Checks if the string contains an IPv4 address.
1121 * This function has a strict mode or a non-strict mode of operation
1122 * When 'strict' is set to FALSE this function will return TRUE if
1123 * the string contains at least 'dec-octet "." dec-octet' since partial
1124 * IPv4 addresses will be normalized out into full IPv4 addresses. When
1125 * 'strict' is set this function expects there to be a full IPv4 address.
1127 * IPv4address = dec-octet "." dec-octet "." dec-octet "." dec-octet
1129 static BOOL check_ipv4address(const WCHAR **ptr, BOOL strict) {
1130 const WCHAR *start = *ptr;
1132 if(!check_dec_octet(ptr)) {
1133 *ptr = start;
1134 return FALSE;
1137 if(**ptr != '.') {
1138 *ptr = start;
1139 return FALSE;
1142 ++(*ptr);
1143 if(!check_dec_octet(ptr)) {
1144 *ptr = start;
1145 return FALSE;
1148 if(**ptr != '.') {
1149 if(strict) {
1150 *ptr = start;
1151 return FALSE;
1152 } else
1153 return TRUE;
1156 ++(*ptr);
1157 if(!check_dec_octet(ptr)) {
1158 *ptr = start;
1159 return FALSE;
1162 if(**ptr != '.') {
1163 if(strict) {
1164 *ptr = start;
1165 return FALSE;
1166 } else
1167 return TRUE;
1170 ++(*ptr);
1171 if(!check_dec_octet(ptr)) {
1172 *ptr = start;
1173 return FALSE;
1176 /* Found a four digit ip address. */
1177 return TRUE;
1179 /* Tries to parse the scheme name of the URI.
1181 * scheme = ALPHA *(ALPHA | NUM | '+' | '-' | '.') as defined by RFC 3896.
1182 * NOTE: Windows accepts a number as the first character of a scheme.
1184 static BOOL parse_scheme_name(const WCHAR **ptr, parse_data *data, DWORD extras) {
1185 const WCHAR *start = *ptr;
1187 data->scheme = NULL;
1188 data->scheme_len = 0;
1190 while(**ptr) {
1191 if(**ptr == '*' && *ptr == start) {
1192 /* Might have found a wildcard scheme. If it is the next
1193 * char has to be a ':' for it to be a valid URI
1195 ++(*ptr);
1196 break;
1197 } else if(!is_num(**ptr) && !is_alpha(**ptr) && **ptr != '+' &&
1198 **ptr != '-' && **ptr != '.')
1199 break;
1201 (*ptr)++;
1204 if(*ptr == start)
1205 return FALSE;
1207 /* Schemes must end with a ':' */
1208 if(**ptr != ':' && !((extras & ALLOW_NULL_TERM_SCHEME) && !**ptr)) {
1209 *ptr = start;
1210 return FALSE;
1213 data->scheme = start;
1214 data->scheme_len = *ptr - start;
1216 ++(*ptr);
1217 return TRUE;
1220 /* Tries to deduce the corresponding URL_SCHEME for the given URI. Stores
1221 * the deduced URL_SCHEME in data->scheme_type.
1223 static BOOL parse_scheme_type(parse_data *data) {
1224 /* If there's scheme data then see if it's a recognized scheme. */
1225 if(data->scheme && data->scheme_len) {
1226 DWORD i;
1228 for(i = 0; i < sizeof(recognized_schemes)/sizeof(recognized_schemes[0]); ++i) {
1229 if(lstrlenW(recognized_schemes[i].scheme_name) == data->scheme_len) {
1230 /* Has to be a case insensitive compare. */
1231 if(!StrCmpNIW(recognized_schemes[i].scheme_name, data->scheme, data->scheme_len)) {
1232 data->scheme_type = recognized_schemes[i].scheme;
1233 return TRUE;
1238 /* If we get here it means it's not a recognized scheme. */
1239 data->scheme_type = URL_SCHEME_UNKNOWN;
1240 return TRUE;
1241 } else if(data->is_relative) {
1242 /* Relative URI's have no scheme. */
1243 data->scheme_type = URL_SCHEME_UNKNOWN;
1244 return TRUE;
1245 } else {
1246 /* Should never reach here! what happened... */
1247 FIXME("(%p): Unable to determine scheme type for URI %s\n", data, debugstr_w(data->uri));
1248 return FALSE;
1252 /* Tries to parse (or deduce) the scheme_name of a URI. If it can't
1253 * parse a scheme from the URI it will try to deduce the scheme_name and scheme_type
1254 * using the flags specified in 'flags' (if any). Flags that affect how this function
1255 * operates are the Uri_CREATE_ALLOW_* flags.
1257 * All parsed/deduced information will be stored in 'data' when the function returns.
1259 * Returns TRUE if it was able to successfully parse the information.
1261 static BOOL parse_scheme(const WCHAR **ptr, parse_data *data, DWORD flags, DWORD extras) {
1262 static const WCHAR fileW[] = {'f','i','l','e',0};
1263 static const WCHAR wildcardW[] = {'*',0};
1265 /* First check to see if the uri could implicitly be a file path. */
1266 if(is_implicit_file_path(*ptr)) {
1267 if(flags & Uri_CREATE_ALLOW_IMPLICIT_FILE_SCHEME) {
1268 data->scheme = fileW;
1269 data->scheme_len = lstrlenW(fileW);
1270 data->has_implicit_scheme = TRUE;
1272 TRACE("(%p %p %x): URI is an implicit file path.\n", ptr, data, flags);
1273 } else {
1274 /* Windows does not consider anything that can implicitly be a file
1275 * path to be a valid URI if the ALLOW_IMPLICIT_FILE_SCHEME flag is not set...
1277 TRACE("(%p %p %x): URI is implicitly a file path, but, the ALLOW_IMPLICIT_FILE_SCHEME flag wasn't set.\n",
1278 ptr, data, flags);
1279 return FALSE;
1281 } else if(!parse_scheme_name(ptr, data, extras)) {
1282 /* No scheme was found, this means it could be:
1283 * a) an implicit Wildcard scheme
1284 * b) a relative URI
1285 * c) an invalid URI.
1287 if(flags & Uri_CREATE_ALLOW_IMPLICIT_WILDCARD_SCHEME) {
1288 data->scheme = wildcardW;
1289 data->scheme_len = lstrlenW(wildcardW);
1290 data->has_implicit_scheme = TRUE;
1292 TRACE("(%p %p %x): URI is an implicit wildcard scheme.\n", ptr, data, flags);
1293 } else if (flags & Uri_CREATE_ALLOW_RELATIVE) {
1294 data->is_relative = TRUE;
1295 TRACE("(%p %p %x): URI is relative.\n", ptr, data, flags);
1296 } else {
1297 TRACE("(%p %p %x): Malformed URI found. Unable to deduce scheme name.\n", ptr, data, flags);
1298 return FALSE;
1302 if(!data->is_relative)
1303 TRACE("(%p %p %x): Found scheme=%s scheme_len=%d\n", ptr, data, flags,
1304 debugstr_wn(data->scheme, data->scheme_len), data->scheme_len);
1306 if(!parse_scheme_type(data))
1307 return FALSE;
1309 TRACE("(%p %p %x): Assigned %d as the URL_SCHEME.\n", ptr, data, flags, data->scheme_type);
1310 return TRUE;
1313 static BOOL parse_username(const WCHAR **ptr, parse_data *data, DWORD flags, DWORD extras) {
1314 data->username = *ptr;
1316 while(**ptr != ':' && **ptr != '@') {
1317 if(**ptr == '%') {
1318 if(!check_pct_encoded(ptr)) {
1319 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
1320 *ptr = data->username;
1321 data->username = NULL;
1322 return FALSE;
1324 } else
1325 continue;
1326 } else if(extras & ALLOW_NULL_TERM_USER_NAME && !**ptr)
1327 break;
1328 else if(is_auth_delim(**ptr, data->scheme_type != URL_SCHEME_UNKNOWN)) {
1329 *ptr = data->username;
1330 data->username = NULL;
1331 return FALSE;
1334 ++(*ptr);
1337 data->username_len = *ptr - data->username;
1338 return TRUE;
1341 static BOOL parse_password(const WCHAR **ptr, parse_data *data, DWORD flags, DWORD extras) {
1342 data->password = *ptr;
1344 while(**ptr != '@') {
1345 if(**ptr == '%') {
1346 if(!check_pct_encoded(ptr)) {
1347 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
1348 *ptr = data->password;
1349 data->password = NULL;
1350 return FALSE;
1352 } else
1353 continue;
1354 } else if(extras & ALLOW_NULL_TERM_PASSWORD && !**ptr)
1355 break;
1356 else if(is_auth_delim(**ptr, data->scheme_type != URL_SCHEME_UNKNOWN)) {
1357 *ptr = data->password;
1358 data->password = NULL;
1359 return FALSE;
1362 ++(*ptr);
1365 data->password_len = *ptr - data->password;
1366 return TRUE;
1369 /* Parses the userinfo part of the URI (if it exists). The userinfo field of
1370 * a URI can consist of "username:password@", or just "username@".
1372 * RFC def:
1373 * userinfo = *( unreserved / pct-encoded / sub-delims / ":" )
1375 * NOTES:
1376 * 1) If there is more than one ':' in the userinfo part of the URI Windows
1377 * uses the first occurrence of ':' to delimit the username and password
1378 * components.
1380 * ex:
1381 * ftp://user:pass:word@winehq.org
1383 * would yield "user" as the username and "pass:word" as the password.
1385 * 2) Windows allows any character to appear in the "userinfo" part of
1386 * a URI, as long as it's not an authority delimiter character set.
1388 static void parse_userinfo(const WCHAR **ptr, parse_data *data, DWORD flags) {
1389 const WCHAR *start = *ptr;
1391 if(!parse_username(ptr, data, flags, 0)) {
1392 TRACE("(%p %p %x): URI contained no userinfo.\n", ptr, data, flags);
1393 return;
1396 if(**ptr == ':') {
1397 ++(*ptr);
1398 if(!parse_password(ptr, data, flags, 0)) {
1399 *ptr = start;
1400 data->username = NULL;
1401 data->username_len = 0;
1402 TRACE("(%p %p %x): URI contained no userinfo.\n", ptr, data, flags);
1403 return;
1407 if(**ptr != '@') {
1408 *ptr = start;
1409 data->username = NULL;
1410 data->username_len = 0;
1411 data->password = NULL;
1412 data->password_len = 0;
1414 TRACE("(%p %p %x): URI contained no userinfo.\n", ptr, data, flags);
1415 return;
1418 if(data->username)
1419 TRACE("(%p %p %x): Found username %s len=%d.\n", ptr, data, flags,
1420 debugstr_wn(data->username, data->username_len), data->username_len);
1422 if(data->password)
1423 TRACE("(%p %p %x): Found password %s len=%d.\n", ptr, data, flags,
1424 debugstr_wn(data->password, data->password_len), data->password_len);
1426 ++(*ptr);
1429 /* Attempts to parse a port from the URI.
1431 * NOTES:
1432 * Windows seems to have a cap on what the maximum value
1433 * for a port can be. The max value is USHORT_MAX.
1435 * port = *DIGIT
1437 static BOOL parse_port(const WCHAR **ptr, parse_data *data, DWORD flags) {
1438 UINT port = 0;
1439 data->port = *ptr;
1441 while(!is_auth_delim(**ptr, data->scheme_type != URL_SCHEME_UNKNOWN)) {
1442 if(!is_num(**ptr)) {
1443 *ptr = data->port;
1444 data->port = NULL;
1445 return FALSE;
1448 port = port*10 + (**ptr-'0');
1450 if(port > USHORT_MAX) {
1451 *ptr = data->port;
1452 data->port = NULL;
1453 return FALSE;
1456 ++(*ptr);
1459 data->has_port = TRUE;
1460 data->port_value = port;
1461 data->port_len = *ptr - data->port;
1463 TRACE("(%p %p %x): Found port %s len=%d value=%u\n", ptr, data, flags,
1464 debugstr_wn(data->port, data->port_len), data->port_len, data->port_value);
1465 return TRUE;
1468 /* Attempts to parse a IPv4 address from the URI.
1470 * NOTES:
1471 * Windows normalizes IPv4 addresses, This means there are three
1472 * possibilities for the URI to contain an IPv4 address.
1473 * 1) A well formed address (ex. 192.2.2.2).
1474 * 2) A partially formed address. For example "192.0" would
1475 * normalize to "192.0.0.0" during canonicalization.
1476 * 3) An implicit IPv4 address. For example "256" would
1477 * normalize to "0.0.1.0" during canonicalization. Also
1478 * note that the maximum value for an implicit IP address
1479 * is UINT_MAX, if the value in the URI exceeds this then
1480 * it is not considered an IPv4 address.
1482 static BOOL parse_ipv4address(const WCHAR **ptr, parse_data *data, DWORD flags) {
1483 const BOOL is_unknown = data->scheme_type == URL_SCHEME_UNKNOWN;
1484 data->host = *ptr;
1486 if(!check_ipv4address(ptr, FALSE)) {
1487 if(!check_implicit_ipv4(ptr, &data->implicit_ipv4)) {
1488 TRACE("(%p %p %x): URI didn't contain anything looking like an IPv4 address.\n",
1489 ptr, data, flags);
1490 *ptr = data->host;
1491 data->host = NULL;
1492 return FALSE;
1493 } else
1494 data->has_implicit_ip = TRUE;
1497 data->host_len = *ptr - data->host;
1498 data->host_type = Uri_HOST_IPV4;
1500 /* Check if what we found is the only part of the host name (if it isn't
1501 * we don't have an IPv4 address).
1503 if(**ptr == ':') {
1504 ++(*ptr);
1505 if(!parse_port(ptr, data, flags)) {
1506 *ptr = data->host;
1507 data->host = NULL;
1508 return FALSE;
1510 } else if(!is_auth_delim(**ptr, !is_unknown)) {
1511 /* Found more data which belongs to the host, so this isn't an IPv4. */
1512 *ptr = data->host;
1513 data->host = NULL;
1514 data->has_implicit_ip = FALSE;
1515 return FALSE;
1518 TRACE("(%p %p %x): IPv4 address found. host=%s host_len=%d host_type=%d\n",
1519 ptr, data, flags, debugstr_wn(data->host, data->host_len),
1520 data->host_len, data->host_type);
1521 return TRUE;
1524 /* Attempts to parse the reg-name from the URI.
1526 * Because of the way Windows handles ':' this function also
1527 * handles parsing the port.
1529 * reg-name = *( unreserved / pct-encoded / sub-delims )
1531 * NOTE:
1532 * Windows allows everything, but, the characters in "auth_delims" and ':'
1533 * to appear in a reg-name, unless it's an unknown scheme type then ':' is
1534 * allowed to appear (even if a valid port isn't after it).
1536 * Windows doesn't like host names which start with '[' and end with ']'
1537 * and don't contain a valid IP literal address in between them.
1539 * On Windows if a '[' is encountered in the host name the ':' no longer
1540 * counts as a delimiter until you reach the next ']' or an "authority delimiter".
1542 * A reg-name CAN be empty.
1544 static BOOL parse_reg_name(const WCHAR **ptr, parse_data *data, DWORD flags, DWORD extras) {
1545 const BOOL has_start_bracket = **ptr == '[';
1546 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
1547 const BOOL is_res = data->scheme_type == URL_SCHEME_RES;
1548 BOOL inside_brackets = has_start_bracket;
1550 /* res URIs don't have ports. */
1551 BOOL ignore_col = (extras & IGNORE_PORT_DELIMITER) || is_res;
1553 /* We have to be careful with file schemes. */
1554 if(data->scheme_type == URL_SCHEME_FILE) {
1555 /* This is because an implicit file scheme could be "C:\\test" and it
1556 * would trick this function into thinking the host is "C", when after
1557 * canonicalization the host would end up being an empty string. A drive
1558 * path can also have a '|' instead of a ':' after the drive letter.
1560 if(is_drive_path(*ptr)) {
1561 /* Regular old drive paths have no host type (or host name). */
1562 data->host_type = Uri_HOST_UNKNOWN;
1563 data->host = *ptr;
1564 data->host_len = 0;
1565 return TRUE;
1566 } else if(is_unc_path(*ptr))
1567 /* Skip past the "\\" of a UNC path. */
1568 *ptr += 2;
1571 data->host = *ptr;
1573 /* For res URIs, everything before the first '/' is
1574 * considered the host.
1576 while((!is_res && !is_auth_delim(**ptr, known_scheme)) ||
1577 (is_res && **ptr && **ptr != '/')) {
1578 if(**ptr == ':' && !ignore_col) {
1579 /* We can ignore ':' if were inside brackets.*/
1580 if(!inside_brackets) {
1581 const WCHAR *tmp = (*ptr)++;
1583 /* Attempt to parse the port. */
1584 if(!parse_port(ptr, data, flags)) {
1585 /* Windows expects there to be a valid port for known scheme types. */
1586 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
1587 *ptr = data->host;
1588 data->host = NULL;
1589 TRACE("(%p %p %x %x): Expected valid port\n", ptr, data, flags, extras);
1590 return FALSE;
1591 } else
1592 /* Windows gives up on trying to parse a port when it
1593 * encounters an invalid port.
1595 ignore_col = TRUE;
1596 } else {
1597 data->host_len = tmp - data->host;
1598 break;
1601 } else if(**ptr == '%' && (known_scheme && !is_res)) {
1602 /* Has to be a legit % encoded value. */
1603 if(!check_pct_encoded(ptr)) {
1604 *ptr = data->host;
1605 data->host = NULL;
1606 return FALSE;
1607 } else
1608 continue;
1609 } else if(is_res && is_forbidden_dos_path_char(**ptr)) {
1610 *ptr = data->host;
1611 data->host = NULL;
1612 return FALSE;
1613 } else if(**ptr == ']')
1614 inside_brackets = FALSE;
1615 else if(**ptr == '[')
1616 inside_brackets = TRUE;
1618 ++(*ptr);
1621 if(has_start_bracket) {
1622 /* Make sure the last character of the host wasn't a ']'. */
1623 if(*(*ptr-1) == ']') {
1624 TRACE("(%p %p %x %x): Expected an IP literal inside of the host\n",
1625 ptr, data, flags, extras);
1626 *ptr = data->host;
1627 data->host = NULL;
1628 return FALSE;
1632 /* Don't overwrite our length if we found a port earlier. */
1633 if(!data->port)
1634 data->host_len = *ptr - data->host;
1636 /* If the host is empty, then it's an unknown host type. */
1637 if(data->host_len == 0 || is_res)
1638 data->host_type = Uri_HOST_UNKNOWN;
1639 else
1640 data->host_type = Uri_HOST_DNS;
1642 TRACE("(%p %p %x %x): Parsed reg-name. host=%s len=%d\n", ptr, data, flags, extras,
1643 debugstr_wn(data->host, data->host_len), data->host_len);
1644 return TRUE;
1647 /* Attempts to parse an IPv6 address out of the URI.
1649 * IPv6address = 6( h16 ":" ) ls32
1650 * / "::" 5( h16 ":" ) ls32
1651 * / [ h16 ] "::" 4( h16 ":" ) ls32
1652 * / [ *1( h16 ":" ) h16 ] "::" 3( h16 ":" ) ls32
1653 * / [ *2( h16 ":" ) h16 ] "::" 2( h16 ":" ) ls32
1654 * / [ *3( h16 ":" ) h16 ] "::" h16 ":" ls32
1655 * / [ *4( h16 ":" ) h16 ] "::" ls32
1656 * / [ *5( h16 ":" ) h16 ] "::" h16
1657 * / [ *6( h16 ":" ) h16 ] "::"
1659 * ls32 = ( h16 ":" h16 ) / IPv4address
1660 * ; least-significant 32 bits of address.
1662 * h16 = 1*4HEXDIG
1663 * ; 16 bits of address represented in hexadecimal.
1665 * Modeled after google-url's 'DoParseIPv6' function.
1667 static BOOL parse_ipv6address(const WCHAR **ptr, parse_data *data, DWORD flags) {
1668 const WCHAR *start, *cur_start;
1669 ipv6_address ip;
1671 start = cur_start = *ptr;
1672 memset(&ip, 0, sizeof(ipv6_address));
1674 for(;; ++(*ptr)) {
1675 /* Check if we're on the last character of the host. */
1676 BOOL is_end = (is_auth_delim(**ptr, data->scheme_type != URL_SCHEME_UNKNOWN)
1677 || **ptr == ']');
1679 BOOL is_split = (**ptr == ':');
1680 BOOL is_elision = (is_split && !is_end && *(*ptr+1) == ':');
1682 /* Check if we're at the end of a component, or
1683 * if we're at the end of the IPv6 address.
1685 if(is_split || is_end) {
1686 DWORD cur_len = 0;
1688 cur_len = *ptr - cur_start;
1690 /* h16 can't have a length > 4. */
1691 if(cur_len > 4) {
1692 *ptr = start;
1694 TRACE("(%p %p %x): h16 component to long.\n",
1695 ptr, data, flags);
1696 return FALSE;
1699 if(cur_len == 0) {
1700 /* An h16 component can't have the length of 0 unless
1701 * the elision is at the beginning of the address, or
1702 * at the end of the address.
1704 if(!((*ptr == start && is_elision) ||
1705 (is_end && (*ptr-2) == ip.elision))) {
1706 *ptr = start;
1707 TRACE("(%p %p %x): IPv6 component cannot have a length of 0.\n",
1708 ptr, data, flags);
1709 return FALSE;
1713 if(cur_len > 0) {
1714 /* An IPv6 address can have no more than 8 h16 components. */
1715 if(ip.h16_count >= 8) {
1716 *ptr = start;
1717 TRACE("(%p %p %x): Not a IPv6 address, to many h16 components.\n",
1718 ptr, data, flags);
1719 return FALSE;
1722 ip.components[ip.h16_count].str = cur_start;
1723 ip.components[ip.h16_count].len = cur_len;
1725 TRACE("(%p %p %x): Found h16 component %s, len=%d, h16_count=%d\n",
1726 ptr, data, flags, debugstr_wn(cur_start, cur_len), cur_len,
1727 ip.h16_count);
1728 ++ip.h16_count;
1732 if(is_end)
1733 break;
1735 if(is_elision) {
1736 /* A IPv6 address can only have 1 elision ('::'). */
1737 if(ip.elision) {
1738 *ptr = start;
1740 TRACE("(%p %p %x): IPv6 address cannot have 2 elisions.\n",
1741 ptr, data, flags);
1742 return FALSE;
1745 ip.elision = *ptr;
1746 ++(*ptr);
1749 if(is_split)
1750 cur_start = *ptr+1;
1751 else {
1752 if(!check_ipv4address(ptr, TRUE)) {
1753 if(!is_hexdigit(**ptr)) {
1754 /* Not a valid character for an IPv6 address. */
1755 *ptr = start;
1756 return FALSE;
1758 } else {
1759 /* Found an IPv4 address. */
1760 ip.ipv4 = cur_start;
1761 ip.ipv4_len = *ptr - cur_start;
1763 TRACE("(%p %p %x): Found an attached IPv4 address %s len=%d.\n",
1764 ptr, data, flags, debugstr_wn(ip.ipv4, ip.ipv4_len),
1765 ip.ipv4_len);
1767 /* IPv4 addresses can only appear at the end of a IPv6. */
1768 break;
1773 compute_ipv6_comps_size(&ip);
1775 /* Make sure the IPv6 address adds up to 16 bytes. */
1776 if(ip.components_size + ip.elision_size != 16) {
1777 *ptr = start;
1778 TRACE("(%p %p %x): Invalid IPv6 address, did not add up to 16 bytes.\n",
1779 ptr, data, flags);
1780 return FALSE;
1783 if(ip.elision_size == 2) {
1784 /* For some reason on Windows if an elision that represents
1785 * only one h16 component is encountered at the very begin or
1786 * end of an IPv6 address, Windows does not consider it a
1787 * valid IPv6 address.
1789 * Ex: [::2:3:4:5:6:7] is not valid, even though the sum
1790 * of all the components == 128bits.
1792 if(ip.elision < ip.components[0].str ||
1793 ip.elision > ip.components[ip.h16_count-1].str) {
1794 *ptr = start;
1795 TRACE("(%p %p %x): Invalid IPv6 address. Detected elision of 2 bytes at the beginning or end of the address.\n",
1796 ptr, data, flags);
1797 return FALSE;
1801 data->host_type = Uri_HOST_IPV6;
1802 data->has_ipv6 = TRUE;
1803 data->ipv6_address = ip;
1805 TRACE("(%p %p %x): Found valid IPv6 literal %s len=%d\n",
1806 ptr, data, flags, debugstr_wn(start, *ptr-start),
1807 (int)(*ptr-start));
1808 return TRUE;
1811 /* IPvFuture = "v" 1*HEXDIG "." 1*( unreserved / sub-delims / ":" ) */
1812 static BOOL parse_ipvfuture(const WCHAR **ptr, parse_data *data, DWORD flags) {
1813 const WCHAR *start = *ptr;
1815 /* IPvFuture has to start with a 'v' or 'V'. */
1816 if(**ptr != 'v' && **ptr != 'V')
1817 return FALSE;
1819 /* Following the v there must be at least 1 hex digit. */
1820 ++(*ptr);
1821 if(!is_hexdigit(**ptr)) {
1822 *ptr = start;
1823 return FALSE;
1826 ++(*ptr);
1827 while(is_hexdigit(**ptr))
1828 ++(*ptr);
1830 /* End of the hexdigit sequence must be a '.' */
1831 if(**ptr != '.') {
1832 *ptr = start;
1833 return FALSE;
1836 ++(*ptr);
1837 if(!is_unreserved(**ptr) && !is_subdelim(**ptr) && **ptr != ':') {
1838 *ptr = start;
1839 return FALSE;
1842 ++(*ptr);
1843 while(is_unreserved(**ptr) || is_subdelim(**ptr) || **ptr == ':')
1844 ++(*ptr);
1846 data->host_type = Uri_HOST_UNKNOWN;
1848 TRACE("(%p %p %x): Parsed IPvFuture address %s len=%d\n", ptr, data, flags,
1849 debugstr_wn(start, *ptr-start), (int)(*ptr-start));
1851 return TRUE;
1854 /* IP-literal = "[" ( IPv6address / IPvFuture ) "]" */
1855 static BOOL parse_ip_literal(const WCHAR **ptr, parse_data *data, DWORD flags, DWORD extras) {
1856 data->host = *ptr;
1858 if(**ptr != '[' && !(extras & ALLOW_BRACKETLESS_IP_LITERAL)) {
1859 data->host = NULL;
1860 return FALSE;
1861 } else if(**ptr == '[')
1862 ++(*ptr);
1864 if(!parse_ipv6address(ptr, data, flags)) {
1865 if(extras & SKIP_IP_FUTURE_CHECK || !parse_ipvfuture(ptr, data, flags)) {
1866 *ptr = data->host;
1867 data->host = NULL;
1868 return FALSE;
1872 if(**ptr != ']' && !(extras & ALLOW_BRACKETLESS_IP_LITERAL)) {
1873 *ptr = data->host;
1874 data->host = NULL;
1875 return FALSE;
1876 } else if(!**ptr && extras & ALLOW_BRACKETLESS_IP_LITERAL) {
1877 /* The IP literal didn't contain brackets and was followed by
1878 * a NULL terminator, so no reason to even check the port.
1880 data->host_len = *ptr - data->host;
1881 return TRUE;
1884 ++(*ptr);
1885 if(**ptr == ':') {
1886 ++(*ptr);
1887 /* If a valid port is not found, then let it trickle down to
1888 * parse_reg_name.
1890 if(!parse_port(ptr, data, flags)) {
1891 *ptr = data->host;
1892 data->host = NULL;
1893 return FALSE;
1895 } else
1896 data->host_len = *ptr - data->host;
1898 return TRUE;
1901 /* Parses the host information from the URI.
1903 * host = IP-literal / IPv4address / reg-name
1905 static BOOL parse_host(const WCHAR **ptr, parse_data *data, DWORD flags, DWORD extras) {
1906 if(!parse_ip_literal(ptr, data, flags, extras)) {
1907 if(!parse_ipv4address(ptr, data, flags)) {
1908 if(!parse_reg_name(ptr, data, flags, extras)) {
1909 TRACE("(%p %p %x %x): Malformed URI, Unknown host type.\n",
1910 ptr, data, flags, extras);
1911 return FALSE;
1916 return TRUE;
1919 /* Parses the authority information from the URI.
1921 * authority = [ userinfo "@" ] host [ ":" port ]
1923 static BOOL parse_authority(const WCHAR **ptr, parse_data *data, DWORD flags) {
1924 parse_userinfo(ptr, data, flags);
1926 /* Parsing the port will happen during one of the host parsing
1927 * routines (if the URI has a port).
1929 if(!parse_host(ptr, data, flags, 0))
1930 return FALSE;
1932 return TRUE;
1935 /* Attempts to parse the path information of a hierarchical URI. */
1936 static BOOL parse_path_hierarchical(const WCHAR **ptr, parse_data *data, DWORD flags) {
1937 const WCHAR *start = *ptr;
1938 static const WCHAR slash[] = {'/',0};
1939 const BOOL is_file = data->scheme_type == URL_SCHEME_FILE;
1941 if(is_path_delim(**ptr)) {
1942 if(data->scheme_type == URL_SCHEME_WILDCARD && !data->must_have_path) {
1943 data->path = NULL;
1944 data->path_len = 0;
1945 } else if(!(flags & Uri_CREATE_NO_CANONICALIZE)) {
1946 /* If the path component is empty, then a '/' is added. */
1947 data->path = slash;
1948 data->path_len = 1;
1950 } else {
1951 while(!is_path_delim(**ptr)) {
1952 if(**ptr == '%' && data->scheme_type != URL_SCHEME_UNKNOWN && !is_file) {
1953 if(!check_pct_encoded(ptr)) {
1954 *ptr = start;
1955 return FALSE;
1956 } else
1957 continue;
1958 } else if(is_forbidden_dos_path_char(**ptr) && is_file &&
1959 (flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
1960 /* File schemes with USE_DOS_PATH set aren't allowed to have
1961 * a '<' or '>' or '\"' appear in them.
1963 *ptr = start;
1964 return FALSE;
1965 } else if(**ptr == '\\') {
1966 /* Not allowed to have a backslash if NO_CANONICALIZE is set
1967 * and the scheme is known type (but not a file scheme).
1969 if(flags & Uri_CREATE_NO_CANONICALIZE) {
1970 if(data->scheme_type != URL_SCHEME_FILE &&
1971 data->scheme_type != URL_SCHEME_UNKNOWN) {
1972 *ptr = start;
1973 return FALSE;
1978 ++(*ptr);
1981 /* The only time a URI doesn't have a path is when
1982 * the NO_CANONICALIZE flag is set and the raw URI
1983 * didn't contain one.
1985 if(*ptr == start) {
1986 data->path = NULL;
1987 data->path_len = 0;
1988 } else {
1989 data->path = start;
1990 data->path_len = *ptr - start;
1994 if(data->path)
1995 TRACE("(%p %p %x): Parsed path %s len=%d\n", ptr, data, flags,
1996 debugstr_wn(data->path, data->path_len), data->path_len);
1997 else
1998 TRACE("(%p %p %x): The URI contained no path\n", ptr, data, flags);
2000 return TRUE;
2003 /* Parses the path of an opaque URI (much less strict then the parser
2004 * for a hierarchical URI).
2006 * NOTE:
2007 * Windows allows invalid % encoded data to appear in opaque URI paths
2008 * for unknown scheme types.
2010 * File schemes with USE_DOS_PATH set aren't allowed to have '<', '>', or '\"'
2011 * appear in them.
2013 static BOOL parse_path_opaque(const WCHAR **ptr, parse_data *data, DWORD flags) {
2014 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
2015 const BOOL is_file = data->scheme_type == URL_SCHEME_FILE;
2017 data->path = *ptr;
2019 while(!is_path_delim(**ptr)) {
2020 if(**ptr == '%' && known_scheme) {
2021 if(!check_pct_encoded(ptr)) {
2022 *ptr = data->path;
2023 data->path = NULL;
2024 return FALSE;
2025 } else
2026 continue;
2027 } else if(is_forbidden_dos_path_char(**ptr) && is_file &&
2028 (flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
2029 *ptr = data->path;
2030 data->path = NULL;
2031 return FALSE;
2034 ++(*ptr);
2037 data->path_len = *ptr - data->path;
2038 TRACE("(%p %p %x): Parsed opaque URI path %s len=%d\n", ptr, data, flags,
2039 debugstr_wn(data->path, data->path_len), data->path_len);
2040 return TRUE;
2043 /* Determines how the URI should be parsed after the scheme information.
2045 * If the scheme is followed by "//", then it is treated as a hierarchical URI
2046 * which then the authority and path information will be parsed out. Otherwise, the
2047 * URI will be treated as an opaque URI which the authority information is not parsed
2048 * out.
2050 * RFC 3896 definition of hier-part:
2052 * hier-part = "//" authority path-abempty
2053 * / path-absolute
2054 * / path-rootless
2055 * / path-empty
2057 * MSDN opaque URI definition:
2058 * scheme ":" path [ "#" fragment ]
2060 * NOTES:
2061 * If the URI is of an unknown scheme type and has a "//" following the scheme then it
2062 * is treated as a hierarchical URI, but, if the CREATE_NO_CRACK_UNKNOWN_SCHEMES flag is
2063 * set then it is considered an opaque URI regardless of what follows the scheme information
2064 * (per MSDN documentation).
2066 static BOOL parse_hierpart(const WCHAR **ptr, parse_data *data, DWORD flags) {
2067 const WCHAR *start = *ptr;
2069 data->must_have_path = FALSE;
2071 /* For javascript: URIs, simply set everything as a path */
2072 if(data->scheme_type == URL_SCHEME_JAVASCRIPT) {
2073 data->path = *ptr;
2074 data->path_len = strlenW(*ptr);
2075 data->is_opaque = TRUE;
2076 *ptr += data->path_len;
2077 return TRUE;
2080 /* Checks if the authority information needs to be parsed. */
2081 if(is_hierarchical_uri(ptr, data)) {
2082 /* Only treat it as a hierarchical URI if the scheme_type is known or
2083 * the Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES flag is not set.
2085 if(data->scheme_type != URL_SCHEME_UNKNOWN ||
2086 !(flags & Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES)) {
2087 TRACE("(%p %p %x): Treating URI as an hierarchical URI.\n", ptr, data, flags);
2088 data->is_opaque = FALSE;
2090 if(data->scheme_type == URL_SCHEME_WILDCARD && !data->has_implicit_scheme) {
2091 if(**ptr == '/' && *(*ptr+1) == '/') {
2092 data->must_have_path = TRUE;
2093 *ptr += 2;
2097 /* TODO: Handle hierarchical URI's, parse authority then parse the path. */
2098 if(!parse_authority(ptr, data, flags))
2099 return FALSE;
2101 return parse_path_hierarchical(ptr, data, flags);
2102 } else
2103 /* Reset ptr to its starting position so opaque path parsing
2104 * begins at the correct location.
2106 *ptr = start;
2109 /* If it reaches here, then the URI will be treated as an opaque
2110 * URI.
2113 TRACE("(%p %p %x): Treating URI as an opaque URI.\n", ptr, data, flags);
2115 data->is_opaque = TRUE;
2116 if(!parse_path_opaque(ptr, data, flags))
2117 return FALSE;
2119 return TRUE;
2122 /* Attempts to parse the query string from the URI.
2124 * NOTES:
2125 * If NO_DECODE_EXTRA_INFO flag is set, then invalid percent encoded
2126 * data is allowed to appear in the query string. For unknown scheme types
2127 * invalid percent encoded data is allowed to appear regardless.
2129 static BOOL parse_query(const WCHAR **ptr, parse_data *data, DWORD flags) {
2130 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
2132 if(**ptr != '?') {
2133 TRACE("(%p %p %x): URI didn't contain a query string.\n", ptr, data, flags);
2134 return TRUE;
2137 data->query = *ptr;
2139 ++(*ptr);
2140 while(**ptr && **ptr != '#') {
2141 if(**ptr == '%' && known_scheme &&
2142 !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
2143 if(!check_pct_encoded(ptr)) {
2144 *ptr = data->query;
2145 data->query = NULL;
2146 return FALSE;
2147 } else
2148 continue;
2151 ++(*ptr);
2154 data->query_len = *ptr - data->query;
2156 TRACE("(%p %p %x): Parsed query string %s len=%d\n", ptr, data, flags,
2157 debugstr_wn(data->query, data->query_len), data->query_len);
2158 return TRUE;
2161 /* Attempts to parse the fragment from the URI.
2163 * NOTES:
2164 * If NO_DECODE_EXTRA_INFO flag is set, then invalid percent encoded
2165 * data is allowed to appear in the query string. For unknown scheme types
2166 * invalid percent encoded data is allowed to appear regardless.
2168 static BOOL parse_fragment(const WCHAR **ptr, parse_data *data, DWORD flags) {
2169 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
2171 if(**ptr != '#') {
2172 TRACE("(%p %p %x): URI didn't contain a fragment.\n", ptr, data, flags);
2173 return TRUE;
2176 data->fragment = *ptr;
2178 ++(*ptr);
2179 while(**ptr) {
2180 if(**ptr == '%' && known_scheme &&
2181 !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
2182 if(!check_pct_encoded(ptr)) {
2183 *ptr = data->fragment;
2184 data->fragment = NULL;
2185 return FALSE;
2186 } else
2187 continue;
2190 ++(*ptr);
2193 data->fragment_len = *ptr - data->fragment;
2195 TRACE("(%p %p %x): Parsed fragment %s len=%d\n", ptr, data, flags,
2196 debugstr_wn(data->fragment, data->fragment_len), data->fragment_len);
2197 return TRUE;
2200 /* Parses and validates the components of the specified by data->uri
2201 * and stores the information it parses into 'data'.
2203 * Returns TRUE if it successfully parsed the URI. False otherwise.
2205 static BOOL parse_uri(parse_data *data, DWORD flags) {
2206 const WCHAR *ptr;
2207 const WCHAR **pptr;
2209 ptr = data->uri;
2210 pptr = &ptr;
2212 TRACE("(%p %x): BEGINNING TO PARSE URI %s.\n", data, flags, debugstr_w(data->uri));
2214 if(!parse_scheme(pptr, data, flags, 0))
2215 return FALSE;
2217 if(!parse_hierpart(pptr, data, flags))
2218 return FALSE;
2220 if(!parse_query(pptr, data, flags))
2221 return FALSE;
2223 if(!parse_fragment(pptr, data, flags))
2224 return FALSE;
2226 TRACE("(%p %x): FINISHED PARSING URI.\n", data, flags);
2227 return TRUE;
2230 static BOOL canonicalize_username(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2231 const WCHAR *ptr;
2233 if(!data->username) {
2234 uri->userinfo_start = -1;
2235 return TRUE;
2238 uri->userinfo_start = uri->canon_len;
2239 for(ptr = data->username; ptr < data->username+data->username_len; ++ptr) {
2240 if(*ptr == '%') {
2241 /* Only decode % encoded values for known scheme types. */
2242 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
2243 /* See if the value really needs decoding. */
2244 WCHAR val = decode_pct_val(ptr);
2245 if(is_unreserved(val)) {
2246 if(!computeOnly)
2247 uri->canon_uri[uri->canon_len] = val;
2249 ++uri->canon_len;
2251 /* Move pass the hex characters. */
2252 ptr += 2;
2253 continue;
2256 } else if(!is_reserved(*ptr) && !is_unreserved(*ptr) && *ptr != '\\') {
2257 /* Only percent encode forbidden characters if the NO_ENCODE_FORBIDDEN_CHARACTERS flag
2258 * is NOT set.
2260 if(!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS)) {
2261 if(!computeOnly)
2262 pct_encode_val(*ptr, uri->canon_uri + uri->canon_len);
2264 uri->canon_len += 3;
2265 continue;
2269 if(!computeOnly)
2270 /* Nothing special, so just copy the character over. */
2271 uri->canon_uri[uri->canon_len] = *ptr;
2272 ++uri->canon_len;
2275 return TRUE;
2278 static BOOL canonicalize_password(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2279 const WCHAR *ptr;
2281 if(!data->password) {
2282 uri->userinfo_split = -1;
2283 return TRUE;
2286 if(uri->userinfo_start == -1)
2287 /* Has a password, but, doesn't have a username. */
2288 uri->userinfo_start = uri->canon_len;
2290 uri->userinfo_split = uri->canon_len - uri->userinfo_start;
2292 /* Add the ':' to the userinfo component. */
2293 if(!computeOnly)
2294 uri->canon_uri[uri->canon_len] = ':';
2295 ++uri->canon_len;
2297 for(ptr = data->password; ptr < data->password+data->password_len; ++ptr) {
2298 if(*ptr == '%') {
2299 /* Only decode % encoded values for known scheme types. */
2300 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
2301 /* See if the value really needs decoding. */
2302 WCHAR val = decode_pct_val(ptr);
2303 if(is_unreserved(val)) {
2304 if(!computeOnly)
2305 uri->canon_uri[uri->canon_len] = val;
2307 ++uri->canon_len;
2309 /* Move pass the hex characters. */
2310 ptr += 2;
2311 continue;
2314 } else if(!is_reserved(*ptr) && !is_unreserved(*ptr) && *ptr != '\\') {
2315 /* Only percent encode forbidden characters if the NO_ENCODE_FORBIDDEN_CHARACTERS flag
2316 * is NOT set.
2318 if(!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS)) {
2319 if(!computeOnly)
2320 pct_encode_val(*ptr, uri->canon_uri + uri->canon_len);
2322 uri->canon_len += 3;
2323 continue;
2327 if(!computeOnly)
2328 /* Nothing special, so just copy the character over. */
2329 uri->canon_uri[uri->canon_len] = *ptr;
2330 ++uri->canon_len;
2333 return TRUE;
2336 /* Canonicalizes the userinfo of the URI represented by the parse_data.
2338 * Canonicalization of the userinfo is a simple process. If there are any percent
2339 * encoded characters that fall in the "unreserved" character set, they are decoded
2340 * to their actual value. If a character is not in the "unreserved" or "reserved" sets
2341 * then it is percent encoded. Other than that the characters are copied over without
2342 * change.
2344 static BOOL canonicalize_userinfo(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2345 uri->userinfo_start = uri->userinfo_split = -1;
2346 uri->userinfo_len = 0;
2348 if(!data->username && !data->password)
2349 /* URI doesn't have userinfo, so nothing to do here. */
2350 return TRUE;
2352 if(!canonicalize_username(data, uri, flags, computeOnly))
2353 return FALSE;
2355 if(!canonicalize_password(data, uri, flags, computeOnly))
2356 return FALSE;
2358 uri->userinfo_len = uri->canon_len - uri->userinfo_start;
2359 if(!computeOnly)
2360 TRACE("(%p %p %x %d): Canonicalized userinfo, userinfo_start=%d, userinfo=%s, userinfo_split=%d userinfo_len=%d.\n",
2361 data, uri, flags, computeOnly, uri->userinfo_start, debugstr_wn(uri->canon_uri + uri->userinfo_start, uri->userinfo_len),
2362 uri->userinfo_split, uri->userinfo_len);
2364 /* Now insert the '@' after the userinfo. */
2365 if(!computeOnly)
2366 uri->canon_uri[uri->canon_len] = '@';
2367 ++uri->canon_len;
2369 return TRUE;
2372 /* Attempts to canonicalize a reg_name.
2374 * Things that happen:
2375 * 1) If Uri_CREATE_NO_CANONICALIZE flag is not set, then the reg_name is
2376 * lower cased. Unless it's an unknown scheme type, which case it's
2377 * no lower cased regardless.
2379 * 2) Unreserved % encoded characters are decoded for known
2380 * scheme types.
2382 * 3) Forbidden characters are % encoded as long as
2383 * Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS flag is not set and
2384 * it isn't an unknown scheme type.
2386 * 4) If it's a file scheme and the host is "localhost" it's removed.
2388 * 5) If it's a file scheme and Uri_CREATE_FILE_USE_DOS_PATH is set,
2389 * then the UNC path characters are added before the host name.
2391 static BOOL canonicalize_reg_name(const parse_data *data, Uri *uri,
2392 DWORD flags, BOOL computeOnly) {
2393 static const WCHAR localhostW[] =
2394 {'l','o','c','a','l','h','o','s','t',0};
2395 const WCHAR *ptr;
2396 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
2398 if(data->scheme_type == URL_SCHEME_FILE &&
2399 data->host_len == lstrlenW(localhostW)) {
2400 if(!StrCmpNIW(data->host, localhostW, data->host_len)) {
2401 uri->host_start = -1;
2402 uri->host_len = 0;
2403 uri->host_type = Uri_HOST_UNKNOWN;
2404 return TRUE;
2408 if(data->scheme_type == URL_SCHEME_FILE && flags & Uri_CREATE_FILE_USE_DOS_PATH) {
2409 if(!computeOnly) {
2410 uri->canon_uri[uri->canon_len] = '\\';
2411 uri->canon_uri[uri->canon_len+1] = '\\';
2413 uri->canon_len += 2;
2414 uri->authority_start = uri->canon_len;
2417 uri->host_start = uri->canon_len;
2419 for(ptr = data->host; ptr < data->host+data->host_len; ++ptr) {
2420 if(*ptr == '%' && known_scheme) {
2421 WCHAR val = decode_pct_val(ptr);
2422 if(is_unreserved(val)) {
2423 /* If NO_CANONICALIZE is not set, then windows lower cases the
2424 * decoded value.
2426 if(!(flags & Uri_CREATE_NO_CANONICALIZE) && isupperW(val)) {
2427 if(!computeOnly)
2428 uri->canon_uri[uri->canon_len] = tolowerW(val);
2429 } else {
2430 if(!computeOnly)
2431 uri->canon_uri[uri->canon_len] = val;
2433 ++uri->canon_len;
2435 /* Skip past the % encoded character. */
2436 ptr += 2;
2437 continue;
2438 } else {
2439 /* Just copy the % over. */
2440 if(!computeOnly)
2441 uri->canon_uri[uri->canon_len] = *ptr;
2442 ++uri->canon_len;
2444 } else if(*ptr == '\\') {
2445 /* Only unknown scheme types could have made it here with a '\\' in the host name. */
2446 if(!computeOnly)
2447 uri->canon_uri[uri->canon_len] = *ptr;
2448 ++uri->canon_len;
2449 } else if(!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS) &&
2450 !is_unreserved(*ptr) && !is_reserved(*ptr) && known_scheme) {
2451 if(!computeOnly) {
2452 pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
2454 /* The percent encoded value gets lower cased also. */
2455 if(!(flags & Uri_CREATE_NO_CANONICALIZE)) {
2456 uri->canon_uri[uri->canon_len+1] = tolowerW(uri->canon_uri[uri->canon_len+1]);
2457 uri->canon_uri[uri->canon_len+2] = tolowerW(uri->canon_uri[uri->canon_len+2]);
2461 uri->canon_len += 3;
2462 } else {
2463 if(!computeOnly) {
2464 if(!(flags & Uri_CREATE_NO_CANONICALIZE) && known_scheme)
2465 uri->canon_uri[uri->canon_len] = tolowerW(*ptr);
2466 else
2467 uri->canon_uri[uri->canon_len] = *ptr;
2470 ++uri->canon_len;
2474 uri->host_len = uri->canon_len - uri->host_start;
2476 if(!computeOnly)
2477 TRACE("(%p %p %x %d): Canonicalize reg_name=%s len=%d\n", data, uri, flags,
2478 computeOnly, debugstr_wn(uri->canon_uri+uri->host_start, uri->host_len),
2479 uri->host_len);
2481 if(!computeOnly)
2482 find_domain_name(uri->canon_uri+uri->host_start, uri->host_len,
2483 &(uri->domain_offset));
2485 return TRUE;
2488 /* Attempts to canonicalize an implicit IPv4 address. */
2489 static BOOL canonicalize_implicit_ipv4address(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2490 uri->host_start = uri->canon_len;
2492 TRACE("%u\n", data->implicit_ipv4);
2493 /* For unknown scheme types Windows doesn't convert
2494 * the value into an IP address, but it still considers
2495 * it an IPv4 address.
2497 if(data->scheme_type == URL_SCHEME_UNKNOWN) {
2498 if(!computeOnly)
2499 memcpy(uri->canon_uri+uri->canon_len, data->host, data->host_len*sizeof(WCHAR));
2500 uri->canon_len += data->host_len;
2501 } else {
2502 if(!computeOnly)
2503 uri->canon_len += ui2ipv4(uri->canon_uri+uri->canon_len, data->implicit_ipv4);
2504 else
2505 uri->canon_len += ui2ipv4(NULL, data->implicit_ipv4);
2508 uri->host_len = uri->canon_len - uri->host_start;
2509 uri->host_type = Uri_HOST_IPV4;
2511 if(!computeOnly)
2512 TRACE("%p %p %x %d): Canonicalized implicit IP address=%s len=%d\n",
2513 data, uri, flags, computeOnly,
2514 debugstr_wn(uri->canon_uri+uri->host_start, uri->host_len),
2515 uri->host_len);
2517 return TRUE;
2520 /* Attempts to canonicalize an IPv4 address.
2522 * If the parse_data represents a URI that has an implicit IPv4 address
2523 * (ex. http://256/, this function will convert 256 into 0.0.1.0). If
2524 * the implicit IP address exceeds the value of UINT_MAX (maximum value
2525 * for an IPv4 address) it's canonicalized as if it were a reg-name.
2527 * If the parse_data contains a partial or full IPv4 address it normalizes it.
2528 * A partial IPv4 address is something like "192.0" and would be normalized to
2529 * "192.0.0.0". With a full (or partial) IPv4 address like "192.002.01.003" would
2530 * be normalized to "192.2.1.3".
2532 * NOTES:
2533 * Windows ONLY normalizes IPv4 address for known scheme types (one that isn't
2534 * URL_SCHEME_UNKNOWN). For unknown scheme types, it simply copies the data from
2535 * the original URI into the canonicalized URI, but, it still recognizes URI's
2536 * host type as HOST_IPV4.
2538 static BOOL canonicalize_ipv4address(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2539 if(data->has_implicit_ip)
2540 return canonicalize_implicit_ipv4address(data, uri, flags, computeOnly);
2541 else {
2542 uri->host_start = uri->canon_len;
2544 /* Windows only normalizes for known scheme types. */
2545 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
2546 /* parse_data contains a partial or full IPv4 address, so normalize it. */
2547 DWORD i, octetDigitCount = 0, octetCount = 0;
2548 BOOL octetHasDigit = FALSE;
2550 for(i = 0; i < data->host_len; ++i) {
2551 if(data->host[i] == '0' && !octetHasDigit) {
2552 /* Can ignore leading zeros if:
2553 * 1) It isn't the last digit of the octet.
2554 * 2) i+1 != data->host_len
2555 * 3) i+1 != '.'
2557 if(octetDigitCount == 2 ||
2558 i+1 == data->host_len ||
2559 data->host[i+1] == '.') {
2560 if(!computeOnly)
2561 uri->canon_uri[uri->canon_len] = data->host[i];
2562 ++uri->canon_len;
2563 TRACE("Adding zero\n");
2565 } else if(data->host[i] == '.') {
2566 if(!computeOnly)
2567 uri->canon_uri[uri->canon_len] = data->host[i];
2568 ++uri->canon_len;
2570 octetDigitCount = 0;
2571 octetHasDigit = FALSE;
2572 ++octetCount;
2573 } else {
2574 if(!computeOnly)
2575 uri->canon_uri[uri->canon_len] = data->host[i];
2576 ++uri->canon_len;
2578 ++octetDigitCount;
2579 octetHasDigit = TRUE;
2583 /* Make sure the canonicalized IP address has 4 dec-octets.
2584 * If doesn't add "0" ones until there is 4;
2586 for( ; octetCount < 3; ++octetCount) {
2587 if(!computeOnly) {
2588 uri->canon_uri[uri->canon_len] = '.';
2589 uri->canon_uri[uri->canon_len+1] = '0';
2592 uri->canon_len += 2;
2594 } else {
2595 /* Windows doesn't normalize addresses in unknown schemes. */
2596 if(!computeOnly)
2597 memcpy(uri->canon_uri+uri->canon_len, data->host, data->host_len*sizeof(WCHAR));
2598 uri->canon_len += data->host_len;
2601 uri->host_len = uri->canon_len - uri->host_start;
2602 if(!computeOnly)
2603 TRACE("(%p %p %x %d): Canonicalized IPv4 address, ip=%s len=%d\n",
2604 data, uri, flags, computeOnly,
2605 debugstr_wn(uri->canon_uri+uri->host_start, uri->host_len),
2606 uri->host_len);
2609 return TRUE;
2612 /* Attempts to canonicalize the IPv6 address of the URI.
2614 * Multiple things happen during the canonicalization of an IPv6 address:
2615 * 1) Any leading zero's in a h16 component are removed.
2616 * Ex: [0001:0022::] -> [1:22::]
2618 * 2) The longest sequence of zero h16 components are compressed
2619 * into a "::" (elision). If there's a tie, the first is chosen.
2621 * Ex: [0:0:0:0:1:6:7:8] -> [::1:6:7:8]
2622 * [0:0:0:0:1:2::] -> [::1:2:0:0]
2623 * [0:0:1:2:0:0:7:8] -> [::1:2:0:0:7:8]
2625 * 3) If an IPv4 address is attached to the IPv6 address, it's
2626 * also normalized.
2627 * Ex: [::001.002.022.000] -> [::1.2.22.0]
2629 * 4) If an elision is present, but, only represents one h16 component
2630 * it's expanded.
2632 * Ex: [1::2:3:4:5:6:7] -> [1:0:2:3:4:5:6:7]
2634 * 5) If the IPv6 address contains an IPv4 address and there exists
2635 * at least 1 non-zero h16 component the IPv4 address is converted
2636 * into two h16 components, otherwise it's normalized and kept as is.
2638 * Ex: [::192.200.003.4] -> [::192.200.3.4]
2639 * [ffff::192.200.003.4] -> [ffff::c0c8:3041]
2641 * NOTE:
2642 * For unknown scheme types Windows simply copies the address over without any
2643 * changes.
2645 * IPv4 address can be included in an elision if all its components are 0's.
2647 static BOOL canonicalize_ipv6address(const parse_data *data, Uri *uri,
2648 DWORD flags, BOOL computeOnly) {
2649 uri->host_start = uri->canon_len;
2651 if(data->scheme_type == URL_SCHEME_UNKNOWN) {
2652 if(!computeOnly)
2653 memcpy(uri->canon_uri+uri->canon_len, data->host, data->host_len*sizeof(WCHAR));
2654 uri->canon_len += data->host_len;
2655 } else {
2656 USHORT values[8];
2657 INT elision_start;
2658 DWORD i, elision_len;
2660 if(!ipv6_to_number(&(data->ipv6_address), values)) {
2661 TRACE("(%p %p %x %d): Failed to compute numerical value for IPv6 address.\n",
2662 data, uri, flags, computeOnly);
2663 return FALSE;
2666 if(!computeOnly)
2667 uri->canon_uri[uri->canon_len] = '[';
2668 ++uri->canon_len;
2670 /* Find where the elision should occur (if any). */
2671 compute_elision_location(&(data->ipv6_address), values, &elision_start, &elision_len);
2673 TRACE("%p %p %x %d): Elision starts at %d, len=%u\n", data, uri, flags,
2674 computeOnly, elision_start, elision_len);
2676 for(i = 0; i < 8; ++i) {
2677 BOOL in_elision = (elision_start > -1 && i >= elision_start &&
2678 i < elision_start+elision_len);
2679 BOOL do_ipv4 = (i == 6 && data->ipv6_address.ipv4 && !in_elision &&
2680 data->ipv6_address.h16_count == 0);
2682 if(i == elision_start) {
2683 if(!computeOnly) {
2684 uri->canon_uri[uri->canon_len] = ':';
2685 uri->canon_uri[uri->canon_len+1] = ':';
2687 uri->canon_len += 2;
2690 /* We can ignore the current component if we're in the elision. */
2691 if(in_elision)
2692 continue;
2694 /* We only add a ':' if we're not at i == 0, or when we're at
2695 * the very end of elision range since the ':' colon was handled
2696 * earlier. Otherwise we would end up with ":::" after elision.
2698 if(i != 0 && !(elision_start > -1 && i == elision_start+elision_len)) {
2699 if(!computeOnly)
2700 uri->canon_uri[uri->canon_len] = ':';
2701 ++uri->canon_len;
2704 if(do_ipv4) {
2705 UINT val;
2706 DWORD len;
2708 /* Combine the two parts of the IPv4 address values. */
2709 val = values[i];
2710 val <<= 16;
2711 val += values[i+1];
2713 if(!computeOnly)
2714 len = ui2ipv4(uri->canon_uri+uri->canon_len, val);
2715 else
2716 len = ui2ipv4(NULL, val);
2718 uri->canon_len += len;
2719 ++i;
2720 } else {
2721 /* Write a regular h16 component to the URI. */
2723 /* Short circuit for the trivial case. */
2724 if(values[i] == 0) {
2725 if(!computeOnly)
2726 uri->canon_uri[uri->canon_len] = '0';
2727 ++uri->canon_len;
2728 } else {
2729 static const WCHAR formatW[] = {'%','x',0};
2731 if(!computeOnly)
2732 uri->canon_len += sprintfW(uri->canon_uri+uri->canon_len,
2733 formatW, values[i]);
2734 else {
2735 WCHAR tmp[5];
2736 uri->canon_len += sprintfW(tmp, formatW, values[i]);
2742 /* Add the closing ']'. */
2743 if(!computeOnly)
2744 uri->canon_uri[uri->canon_len] = ']';
2745 ++uri->canon_len;
2748 uri->host_len = uri->canon_len - uri->host_start;
2750 if(!computeOnly)
2751 TRACE("(%p %p %x %d): Canonicalized IPv6 address %s, len=%d\n", data, uri, flags,
2752 computeOnly, debugstr_wn(uri->canon_uri+uri->host_start, uri->host_len),
2753 uri->host_len);
2755 return TRUE;
2758 /* Attempts to canonicalize the host of the URI (if any). */
2759 static BOOL canonicalize_host(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2760 uri->host_start = -1;
2761 uri->host_len = 0;
2762 uri->domain_offset = -1;
2764 if(data->host) {
2765 switch(data->host_type) {
2766 case Uri_HOST_DNS:
2767 uri->host_type = Uri_HOST_DNS;
2768 if(!canonicalize_reg_name(data, uri, flags, computeOnly))
2769 return FALSE;
2771 break;
2772 case Uri_HOST_IPV4:
2773 uri->host_type = Uri_HOST_IPV4;
2774 if(!canonicalize_ipv4address(data, uri, flags, computeOnly))
2775 return FALSE;
2777 break;
2778 case Uri_HOST_IPV6:
2779 if(!canonicalize_ipv6address(data, uri, flags, computeOnly))
2780 return FALSE;
2782 uri->host_type = Uri_HOST_IPV6;
2783 break;
2784 case Uri_HOST_UNKNOWN:
2785 if(data->host_len > 0 || data->scheme_type != URL_SCHEME_FILE) {
2786 uri->host_start = uri->canon_len;
2788 /* Nothing happens to unknown host types. */
2789 if(!computeOnly)
2790 memcpy(uri->canon_uri+uri->canon_len, data->host, data->host_len*sizeof(WCHAR));
2791 uri->canon_len += data->host_len;
2792 uri->host_len = data->host_len;
2795 uri->host_type = Uri_HOST_UNKNOWN;
2796 break;
2797 default:
2798 FIXME("(%p %p %x %d): Canonicalization for host type %d not supported.\n", data,
2799 uri, flags, computeOnly, data->host_type);
2800 return FALSE;
2804 return TRUE;
2807 static BOOL canonicalize_port(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2808 BOOL has_default_port = FALSE;
2809 USHORT default_port = 0;
2810 DWORD i;
2812 uri->port_offset = -1;
2814 /* Check if the scheme has a default port. */
2815 for(i = 0; i < sizeof(default_ports)/sizeof(default_ports[0]); ++i) {
2816 if(default_ports[i].scheme == data->scheme_type) {
2817 has_default_port = TRUE;
2818 default_port = default_ports[i].port;
2819 break;
2823 uri->has_port = data->has_port || has_default_port;
2825 /* Possible cases:
2826 * 1) Has a port which is the default port.
2827 * 2) Has a port (not the default).
2828 * 3) Doesn't have a port, but, scheme has a default port.
2829 * 4) No port.
2831 if(has_default_port && data->has_port && data->port_value == default_port) {
2832 /* If it's the default port and this flag isn't set, don't do anything. */
2833 if(flags & Uri_CREATE_NO_CANONICALIZE) {
2834 uri->port_offset = uri->canon_len-uri->authority_start;
2835 if(!computeOnly)
2836 uri->canon_uri[uri->canon_len] = ':';
2837 ++uri->canon_len;
2839 if(data->port) {
2840 /* Copy the original port over. */
2841 if(!computeOnly)
2842 memcpy(uri->canon_uri+uri->canon_len, data->port, data->port_len*sizeof(WCHAR));
2843 uri->canon_len += data->port_len;
2844 } else {
2845 if(!computeOnly)
2846 uri->canon_len += ui2str(uri->canon_uri+uri->canon_len, data->port_value);
2847 else
2848 uri->canon_len += ui2str(NULL, data->port_value);
2852 uri->port = default_port;
2853 } else if(data->has_port) {
2854 uri->port_offset = uri->canon_len-uri->authority_start;
2855 if(!computeOnly)
2856 uri->canon_uri[uri->canon_len] = ':';
2857 ++uri->canon_len;
2859 if(flags & Uri_CREATE_NO_CANONICALIZE && data->port) {
2860 /* Copy the original over without changes. */
2861 if(!computeOnly)
2862 memcpy(uri->canon_uri+uri->canon_len, data->port, data->port_len*sizeof(WCHAR));
2863 uri->canon_len += data->port_len;
2864 } else {
2865 if(!computeOnly)
2866 uri->canon_len += ui2str(uri->canon_uri+uri->canon_len, data->port_value);
2867 else
2868 uri->canon_len += ui2str(NULL, data->port_value);
2871 uri->port = data->port_value;
2872 } else if(has_default_port)
2873 uri->port = default_port;
2875 return TRUE;
2878 /* Canonicalizes the authority of the URI represented by the parse_data. */
2879 static BOOL canonicalize_authority(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2880 uri->authority_start = uri->canon_len;
2881 uri->authority_len = 0;
2883 if(!canonicalize_userinfo(data, uri, flags, computeOnly))
2884 return FALSE;
2886 if(!canonicalize_host(data, uri, flags, computeOnly))
2887 return FALSE;
2889 if(!canonicalize_port(data, uri, flags, computeOnly))
2890 return FALSE;
2892 if(uri->host_start != -1 || (data->is_relative && (data->password || data->username)))
2893 uri->authority_len = uri->canon_len - uri->authority_start;
2894 else
2895 uri->authority_start = -1;
2897 return TRUE;
2900 /* Attempts to canonicalize the path of a hierarchical URI.
2902 * Things that happen:
2903 * 1). Forbidden characters are percent encoded, unless the NO_ENCODE_FORBIDDEN
2904 * flag is set or it's a file URI. Forbidden characters are always encoded
2905 * for file schemes regardless and forbidden characters are never encoded
2906 * for unknown scheme types.
2908 * 2). For known scheme types '\\' are changed to '/'.
2910 * 3). Percent encoded, unreserved characters are decoded to their actual values.
2911 * Unless the scheme type is unknown. For file schemes any percent encoded
2912 * character in the unreserved or reserved set is decoded.
2914 * 4). For File schemes if the path is starts with a drive letter and doesn't
2915 * start with a '/' then one is appended.
2916 * Ex: file://c:/test.mp3 -> file:///c:/test.mp3
2918 * 5). Dot segments are removed from the path for all scheme types
2919 * unless NO_CANONICALIZE flag is set. Dot segments aren't removed
2920 * for wildcard scheme types.
2922 * NOTES:
2923 * file://c:/test%20test -> file:///c:/test%2520test
2924 * file://c:/test%3Etest -> file:///c:/test%253Etest
2925 * if Uri_CREATE_FILE_USE_DOS_PATH is not set:
2926 * file:///c:/test%20test -> file:///c:/test%20test
2927 * file:///c:/test%test -> file:///c:/test%25test
2929 static BOOL canonicalize_path_hierarchical(const parse_data *data, Uri *uri,
2930 DWORD flags, BOOL computeOnly) {
2931 const WCHAR *ptr;
2932 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
2933 const BOOL is_file = data->scheme_type == URL_SCHEME_FILE;
2934 const BOOL is_res = data->scheme_type == URL_SCHEME_RES;
2936 BOOL escape_pct = FALSE;
2938 if(!data->path) {
2939 uri->path_start = -1;
2940 uri->path_len = 0;
2941 return TRUE;
2944 uri->path_start = uri->canon_len;
2945 ptr = data->path;
2947 if(is_file && uri->host_start == -1) {
2948 /* Check if a '/' needs to be appended for the file scheme. */
2949 if(data->path_len > 1 && is_drive_path(ptr) && !(flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
2950 if(!computeOnly)
2951 uri->canon_uri[uri->canon_len] = '/';
2952 uri->canon_len++;
2953 escape_pct = TRUE;
2954 } else if(*ptr == '/') {
2955 if(!(flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
2956 /* Copy the extra '/' over. */
2957 if(!computeOnly)
2958 uri->canon_uri[uri->canon_len] = '/';
2959 ++uri->canon_len;
2961 ++ptr;
2964 if(is_drive_path(ptr)) {
2965 if(!computeOnly) {
2966 uri->canon_uri[uri->canon_len] = *ptr;
2967 /* If there's a '|' after the drive letter, convert it to a ':'. */
2968 uri->canon_uri[uri->canon_len+1] = ':';
2970 ptr += 2;
2971 uri->canon_len += 2;
2975 if(!is_file && *(data->path) && *(data->path) != '/') {
2976 /* Prepend a '/' to the path if it doesn't have one. */
2977 if(!computeOnly)
2978 uri->canon_uri[uri->canon_len] = '/';
2979 ++uri->canon_len;
2982 for(; ptr < data->path+data->path_len; ++ptr) {
2983 BOOL do_default_action = TRUE;
2985 if(*ptr == '%' && !is_res) {
2986 const WCHAR *tmp = ptr;
2987 WCHAR val;
2989 /* Check if the % represents a valid encoded char, or if it needs encoding. */
2990 BOOL force_encode = !check_pct_encoded(&tmp) && is_file && !(flags&Uri_CREATE_FILE_USE_DOS_PATH);
2991 val = decode_pct_val(ptr);
2993 if(force_encode || escape_pct) {
2994 /* Escape the percent sign in the file URI. */
2995 if(!computeOnly)
2996 pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
2997 uri->canon_len += 3;
2998 do_default_action = FALSE;
2999 } else if((is_unreserved(val) && known_scheme) ||
3000 (is_file && (is_unreserved(val) || is_reserved(val) ||
3001 (val && flags&Uri_CREATE_FILE_USE_DOS_PATH && !is_forbidden_dos_path_char(val))))) {
3002 if(!computeOnly)
3003 uri->canon_uri[uri->canon_len] = val;
3004 ++uri->canon_len;
3006 ptr += 2;
3007 continue;
3009 } else if(*ptr == '/' && is_file && (flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
3010 /* Convert the '/' back to a '\\'. */
3011 if(!computeOnly)
3012 uri->canon_uri[uri->canon_len] = '\\';
3013 ++uri->canon_len;
3014 do_default_action = FALSE;
3015 } else if(*ptr == '\\' && known_scheme) {
3016 if(!(is_file && (flags & Uri_CREATE_FILE_USE_DOS_PATH))) {
3017 /* Convert '\\' into a '/'. */
3018 if(!computeOnly)
3019 uri->canon_uri[uri->canon_len] = '/';
3020 ++uri->canon_len;
3021 do_default_action = FALSE;
3023 } else if(known_scheme && !is_res && !is_unreserved(*ptr) && !is_reserved(*ptr) &&
3024 (!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS) || is_file)) {
3025 if(!(is_file && (flags & Uri_CREATE_FILE_USE_DOS_PATH))) {
3026 /* Escape the forbidden character. */
3027 if(!computeOnly)
3028 pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
3029 uri->canon_len += 3;
3030 do_default_action = FALSE;
3034 if(do_default_action) {
3035 if(!computeOnly)
3036 uri->canon_uri[uri->canon_len] = *ptr;
3037 ++uri->canon_len;
3041 uri->path_len = uri->canon_len - uri->path_start;
3043 /* Removing the dot segments only happens when it's not in
3044 * computeOnly mode and it's not a wildcard scheme. File schemes
3045 * with USE_DOS_PATH set don't get dot segments removed.
3047 if(!(is_file && (flags & Uri_CREATE_FILE_USE_DOS_PATH)) &&
3048 data->scheme_type != URL_SCHEME_WILDCARD) {
3049 if(!(flags & Uri_CREATE_NO_CANONICALIZE) && !computeOnly) {
3050 /* Remove the dot segments (if any) and reset everything to the new
3051 * correct length.
3053 DWORD new_len = remove_dot_segments(uri->canon_uri+uri->path_start, uri->path_len);
3054 uri->canon_len -= uri->path_len-new_len;
3055 uri->path_len = new_len;
3059 if(!computeOnly)
3060 TRACE("Canonicalized path %s len=%d\n",
3061 debugstr_wn(uri->canon_uri+uri->path_start, uri->path_len),
3062 uri->path_len);
3064 return TRUE;
3067 /* Attempts to canonicalize the path for an opaque URI.
3069 * For known scheme types:
3070 * 1) forbidden characters are percent encoded if
3071 * NO_ENCODE_FORBIDDEN_CHARACTERS isn't set.
3073 * 2) Percent encoded, unreserved characters are decoded
3074 * to their actual values, for known scheme types.
3076 * 3) '\\' are changed to '/' for known scheme types
3077 * except for mailto schemes.
3079 * 4) For file schemes, if USE_DOS_PATH is set all '/'
3080 * are converted to backslashes.
3082 * 5) For file schemes, if USE_DOS_PATH isn't set all '\'
3083 * are converted to forward slashes.
3085 static BOOL canonicalize_path_opaque(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
3086 const WCHAR *ptr;
3087 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
3088 const BOOL is_file = data->scheme_type == URL_SCHEME_FILE;
3090 if(!data->path) {
3091 uri->path_start = -1;
3092 uri->path_len = 0;
3093 return TRUE;
3096 uri->path_start = uri->canon_len;
3098 /* For javascript: URIs, simply copy path part without any canonicalization */
3099 if(data->scheme_type == URL_SCHEME_JAVASCRIPT) {
3100 if(!computeOnly)
3101 memcpy(uri->canon_uri+uri->canon_len, data->path, data->path_len*sizeof(WCHAR));
3102 uri->path_len = data->path_len;
3103 uri->canon_len += data->path_len;
3104 return TRUE;
3107 /* Windows doesn't allow a "//" to appear after the scheme
3108 * of a URI, if it's an opaque URI.
3110 if(data->scheme && *(data->path) == '/' && *(data->path+1) == '/') {
3111 /* So it inserts a "/." before the "//" if it exists. */
3112 if(!computeOnly) {
3113 uri->canon_uri[uri->canon_len] = '/';
3114 uri->canon_uri[uri->canon_len+1] = '.';
3117 uri->canon_len += 2;
3120 for(ptr = data->path; ptr < data->path+data->path_len; ++ptr) {
3121 BOOL do_default_action = TRUE;
3123 if(*ptr == '%' && known_scheme) {
3124 WCHAR val = decode_pct_val(ptr);
3126 if(is_unreserved(val)) {
3127 if(!computeOnly)
3128 uri->canon_uri[uri->canon_len] = val;
3129 ++uri->canon_len;
3131 ptr += 2;
3132 continue;
3134 } else if(*ptr == '/' && is_file && (flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
3135 if(!computeOnly)
3136 uri->canon_uri[uri->canon_len] = '\\';
3137 ++uri->canon_len;
3138 do_default_action = FALSE;
3139 } else if(*ptr == '\\') {
3140 if(is_file && !(flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
3141 /* Convert to a '/'. */
3142 if(!computeOnly)
3143 uri->canon_uri[uri->canon_len] = '/';
3144 ++uri->canon_len;
3145 do_default_action = FALSE;
3147 } else if(known_scheme && !is_unreserved(*ptr) && !is_reserved(*ptr) &&
3148 !(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS)) {
3149 if(!(is_file && (flags & Uri_CREATE_FILE_USE_DOS_PATH))) {
3150 if(!computeOnly)
3151 pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
3152 uri->canon_len += 3;
3153 do_default_action = FALSE;
3157 if(do_default_action) {
3158 if(!computeOnly)
3159 uri->canon_uri[uri->canon_len] = *ptr;
3160 ++uri->canon_len;
3164 if(data->scheme_type == URL_SCHEME_MK && !computeOnly && !(flags & Uri_CREATE_NO_CANONICALIZE)) {
3165 DWORD new_len = remove_dot_segments(uri->canon_uri + uri->path_start,
3166 uri->canon_len - uri->path_start);
3167 uri->canon_len = uri->path_start + new_len;
3170 uri->path_len = uri->canon_len - uri->path_start;
3172 if(!computeOnly)
3173 TRACE("(%p %p %x %d): Canonicalized opaque URI path %s len=%d\n", data, uri, flags, computeOnly,
3174 debugstr_wn(uri->canon_uri+uri->path_start, uri->path_len), uri->path_len);
3175 return TRUE;
3178 /* Determines how the URI represented by the parse_data should be canonicalized.
3180 * Essentially, if the parse_data represents an hierarchical URI then it calls
3181 * canonicalize_authority and the canonicalization functions for the path. If the
3182 * URI is opaque it canonicalizes the path of the URI.
3184 static BOOL canonicalize_hierpart(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
3185 if(!data->is_opaque || (data->is_relative && (data->password || data->username))) {
3186 /* "//" is only added for non-wildcard scheme types.
3188 * A "//" is only added to a relative URI if it has a
3189 * host or port component (this only happens if a IUriBuilder
3190 * is generating an IUri).
3192 if((data->is_relative && (data->host || data->has_port)) ||
3193 (!data->is_relative && data->scheme_type != URL_SCHEME_WILDCARD)) {
3194 if(data->scheme_type == URL_SCHEME_WILDCARD)
3195 FIXME("Here\n");
3197 if(!computeOnly) {
3198 INT pos = uri->canon_len;
3200 uri->canon_uri[pos] = '/';
3201 uri->canon_uri[pos+1] = '/';
3203 uri->canon_len += 2;
3206 if(!canonicalize_authority(data, uri, flags, computeOnly))
3207 return FALSE;
3209 if(data->is_relative && (data->password || data->username)) {
3210 if(!canonicalize_path_opaque(data, uri, flags, computeOnly))
3211 return FALSE;
3212 } else {
3213 if(!canonicalize_path_hierarchical(data, uri, flags, computeOnly))
3214 return FALSE;
3216 } else {
3217 /* Opaque URI's don't have an authority. */
3218 uri->userinfo_start = uri->userinfo_split = -1;
3219 uri->userinfo_len = 0;
3220 uri->host_start = -1;
3221 uri->host_len = 0;
3222 uri->host_type = Uri_HOST_UNKNOWN;
3223 uri->has_port = FALSE;
3224 uri->authority_start = -1;
3225 uri->authority_len = 0;
3226 uri->domain_offset = -1;
3227 uri->port_offset = -1;
3229 if(is_hierarchical_scheme(data->scheme_type)) {
3230 DWORD i;
3232 /* Absolute URIs aren't displayed for known scheme types
3233 * which should be hierarchical URIs.
3235 uri->display_modifiers |= URI_DISPLAY_NO_ABSOLUTE_URI;
3237 /* Windows also sets the port for these (if they have one). */
3238 for(i = 0; i < sizeof(default_ports)/sizeof(default_ports[0]); ++i) {
3239 if(data->scheme_type == default_ports[i].scheme) {
3240 uri->has_port = TRUE;
3241 uri->port = default_ports[i].port;
3242 break;
3247 if(!canonicalize_path_opaque(data, uri, flags, computeOnly))
3248 return FALSE;
3251 if(uri->path_start > -1 && !computeOnly)
3252 /* Finding file extensions happens for both types of URIs. */
3253 uri->extension_offset = find_file_extension(uri->canon_uri+uri->path_start, uri->path_len);
3254 else
3255 uri->extension_offset = -1;
3257 return TRUE;
3260 /* Attempts to canonicalize the query string of the URI.
3262 * Things that happen:
3263 * 1) For known scheme types forbidden characters
3264 * are percent encoded, unless the NO_DECODE_EXTRA_INFO flag is set
3265 * or NO_ENCODE_FORBIDDEN_CHARACTERS is set.
3267 * 2) For known scheme types, percent encoded, unreserved characters
3268 * are decoded as long as the NO_DECODE_EXTRA_INFO flag isn't set.
3270 static BOOL canonicalize_query(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
3271 const WCHAR *ptr, *end;
3272 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
3274 if(!data->query) {
3275 uri->query_start = -1;
3276 uri->query_len = 0;
3277 return TRUE;
3280 uri->query_start = uri->canon_len;
3282 end = data->query+data->query_len;
3283 for(ptr = data->query; ptr < end; ++ptr) {
3284 if(*ptr == '%') {
3285 if(known_scheme && !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
3286 WCHAR val = decode_pct_val(ptr);
3287 if(is_unreserved(val)) {
3288 if(!computeOnly)
3289 uri->canon_uri[uri->canon_len] = val;
3290 ++uri->canon_len;
3292 ptr += 2;
3293 continue;
3296 } else if(known_scheme && !is_unreserved(*ptr) && !is_reserved(*ptr)) {
3297 if(!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS) &&
3298 !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
3299 if(!computeOnly)
3300 pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
3301 uri->canon_len += 3;
3302 continue;
3306 if(!computeOnly)
3307 uri->canon_uri[uri->canon_len] = *ptr;
3308 ++uri->canon_len;
3311 uri->query_len = uri->canon_len - uri->query_start;
3313 if(!computeOnly)
3314 TRACE("(%p %p %x %d): Canonicalized query string %s len=%d\n", data, uri, flags,
3315 computeOnly, debugstr_wn(uri->canon_uri+uri->query_start, uri->query_len),
3316 uri->query_len);
3317 return TRUE;
3320 static BOOL canonicalize_fragment(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
3321 const WCHAR *ptr, *end;
3322 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
3324 if(!data->fragment) {
3325 uri->fragment_start = -1;
3326 uri->fragment_len = 0;
3327 return TRUE;
3330 uri->fragment_start = uri->canon_len;
3332 end = data->fragment + data->fragment_len;
3333 for(ptr = data->fragment; ptr < end; ++ptr) {
3334 if(*ptr == '%') {
3335 if(known_scheme && !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
3336 WCHAR val = decode_pct_val(ptr);
3337 if(is_unreserved(val)) {
3338 if(!computeOnly)
3339 uri->canon_uri[uri->canon_len] = val;
3340 ++uri->canon_len;
3342 ptr += 2;
3343 continue;
3346 } else if(known_scheme && !is_unreserved(*ptr) && !is_reserved(*ptr)) {
3347 if(!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS) &&
3348 !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
3349 if(!computeOnly)
3350 pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
3351 uri->canon_len += 3;
3352 continue;
3356 if(!computeOnly)
3357 uri->canon_uri[uri->canon_len] = *ptr;
3358 ++uri->canon_len;
3361 uri->fragment_len = uri->canon_len - uri->fragment_start;
3363 if(!computeOnly)
3364 TRACE("(%p %p %x %d): Canonicalized fragment %s len=%d\n", data, uri, flags,
3365 computeOnly, debugstr_wn(uri->canon_uri+uri->fragment_start, uri->fragment_len),
3366 uri->fragment_len);
3367 return TRUE;
3370 /* Canonicalizes the scheme information specified in the parse_data using the specified flags. */
3371 static BOOL canonicalize_scheme(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
3372 uri->scheme_start = -1;
3373 uri->scheme_len = 0;
3375 if(!data->scheme) {
3376 /* The only type of URI that doesn't have to have a scheme is a relative
3377 * URI.
3379 if(!data->is_relative) {
3380 FIXME("(%p %p %x): Unable to determine the scheme type of %s.\n", data,
3381 uri, flags, debugstr_w(data->uri));
3382 return FALSE;
3384 } else {
3385 if(!computeOnly) {
3386 DWORD i;
3387 INT pos = uri->canon_len;
3389 for(i = 0; i < data->scheme_len; ++i) {
3390 /* Scheme name must be lower case after canonicalization. */
3391 uri->canon_uri[i + pos] = tolowerW(data->scheme[i]);
3394 uri->canon_uri[i + pos] = ':';
3395 uri->scheme_start = pos;
3397 TRACE("(%p %p %x): Canonicalized scheme=%s, len=%d.\n", data, uri, flags,
3398 debugstr_wn(uri->canon_uri, uri->scheme_len), data->scheme_len);
3401 /* This happens in both computation modes. */
3402 uri->canon_len += data->scheme_len + 1;
3403 uri->scheme_len = data->scheme_len;
3405 return TRUE;
3408 /* Computes what the length of the URI specified by the parse_data will be
3409 * after canonicalization occurs using the specified flags.
3411 * This function will return a non-zero value indicating the length of the canonicalized
3412 * URI, or -1 on error.
3414 static int compute_canonicalized_length(const parse_data *data, DWORD flags) {
3415 Uri uri;
3417 memset(&uri, 0, sizeof(Uri));
3419 TRACE("(%p %x): Beginning to compute canonicalized length for URI %s\n", data, flags,
3420 debugstr_w(data->uri));
3422 if(!canonicalize_scheme(data, &uri, flags, TRUE)) {
3423 ERR("(%p %x): Failed to compute URI scheme length.\n", data, flags);
3424 return -1;
3427 if(!canonicalize_hierpart(data, &uri, flags, TRUE)) {
3428 ERR("(%p %x): Failed to compute URI hierpart length.\n", data, flags);
3429 return -1;
3432 if(!canonicalize_query(data, &uri, flags, TRUE)) {
3433 ERR("(%p %x): Failed to compute query string length.\n", data, flags);
3434 return -1;
3437 if(!canonicalize_fragment(data, &uri, flags, TRUE)) {
3438 ERR("(%p %x): Failed to compute fragment length.\n", data, flags);
3439 return -1;
3442 TRACE("(%p %x): Finished computing canonicalized URI length. length=%d\n", data, flags, uri.canon_len);
3444 return uri.canon_len;
3447 /* Canonicalizes the URI data specified in the parse_data, using the given flags. If the
3448 * canonicalization succeeds it will store all the canonicalization information
3449 * in the pointer to the Uri.
3451 * To canonicalize a URI this function first computes what the length of the URI
3452 * specified by the parse_data will be. Once this is done it will then perform the actual
3453 * canonicalization of the URI.
3455 static HRESULT canonicalize_uri(const parse_data *data, Uri *uri, DWORD flags) {
3456 INT len;
3458 uri->canon_uri = NULL;
3459 uri->canon_size = uri->canon_len = 0;
3461 TRACE("(%p %p %x): beginning to canonicalize URI %s.\n", data, uri, flags, debugstr_w(data->uri));
3463 /* First try to compute the length of the URI. */
3464 len = compute_canonicalized_length(data, flags);
3465 if(len == -1) {
3466 ERR("(%p %p %x): Could not compute the canonicalized length of %s.\n", data, uri, flags,
3467 debugstr_w(data->uri));
3468 return E_INVALIDARG;
3471 uri->canon_uri = heap_alloc((len+1)*sizeof(WCHAR));
3472 if(!uri->canon_uri)
3473 return E_OUTOFMEMORY;
3475 uri->canon_size = len;
3476 if(!canonicalize_scheme(data, uri, flags, FALSE)) {
3477 ERR("(%p %p %x): Unable to canonicalize the scheme of the URI.\n", data, uri, flags);
3478 return E_INVALIDARG;
3480 uri->scheme_type = data->scheme_type;
3482 if(!canonicalize_hierpart(data, uri, flags, FALSE)) {
3483 ERR("(%p %p %x): Unable to canonicalize the heirpart of the URI\n", data, uri, flags);
3484 return E_INVALIDARG;
3487 if(!canonicalize_query(data, uri, flags, FALSE)) {
3488 ERR("(%p %p %x): Unable to canonicalize query string of the URI.\n",
3489 data, uri, flags);
3490 return E_INVALIDARG;
3493 if(!canonicalize_fragment(data, uri, flags, FALSE)) {
3494 ERR("(%p %p %x): Unable to canonicalize fragment of the URI.\n",
3495 data, uri, flags);
3496 return E_INVALIDARG;
3499 /* There's a possibility we didn't use all the space we allocated
3500 * earlier.
3502 if(uri->canon_len < uri->canon_size) {
3503 /* This happens if the URI is hierarchical and dot
3504 * segments were removed from its path.
3506 WCHAR *tmp = heap_realloc(uri->canon_uri, (uri->canon_len+1)*sizeof(WCHAR));
3507 if(!tmp)
3508 return E_OUTOFMEMORY;
3510 uri->canon_uri = tmp;
3511 uri->canon_size = uri->canon_len;
3514 uri->canon_uri[uri->canon_len] = '\0';
3515 TRACE("(%p %p %x): finished canonicalizing the URI. uri=%s\n", data, uri, flags, debugstr_w(uri->canon_uri));
3517 return S_OK;
3520 static HRESULT get_builder_component(LPWSTR *component, DWORD *component_len,
3521 LPCWSTR source, DWORD source_len,
3522 LPCWSTR *output, DWORD *output_len)
3524 if(!output_len) {
3525 if(output)
3526 *output = NULL;
3527 return E_POINTER;
3530 if(!output) {
3531 *output_len = 0;
3532 return E_POINTER;
3535 if(!(*component) && source) {
3536 /* Allocate 'component', and copy the contents from 'source'
3537 * into the new allocation.
3539 *component = heap_alloc((source_len+1)*sizeof(WCHAR));
3540 if(!(*component))
3541 return E_OUTOFMEMORY;
3543 memcpy(*component, source, source_len*sizeof(WCHAR));
3544 (*component)[source_len] = '\0';
3545 *component_len = source_len;
3548 *output = *component;
3549 *output_len = *component_len;
3550 return *output ? S_OK : S_FALSE;
3553 /* Allocates 'component' and copies the string from 'new_value' into 'component'.
3554 * If 'prefix' is set and 'new_value' isn't NULL, then it checks if 'new_value'
3555 * starts with 'prefix'. If it doesn't then 'prefix' is prepended to 'component'.
3557 * If everything is successful, then will set 'success_flag' in 'flags'.
3559 static HRESULT set_builder_component(LPWSTR *component, DWORD *component_len, LPCWSTR new_value,
3560 WCHAR prefix, DWORD *flags, DWORD success_flag)
3562 heap_free(*component);
3564 if(!new_value) {
3565 *component = NULL;
3566 *component_len = 0;
3567 } else {
3568 BOOL add_prefix = FALSE;
3569 DWORD len = lstrlenW(new_value);
3570 DWORD pos = 0;
3572 if(prefix && *new_value != prefix) {
3573 add_prefix = TRUE;
3574 *component = heap_alloc((len+2)*sizeof(WCHAR));
3575 } else
3576 *component = heap_alloc((len+1)*sizeof(WCHAR));
3578 if(!(*component))
3579 return E_OUTOFMEMORY;
3581 if(add_prefix)
3582 (*component)[pos++] = prefix;
3584 memcpy(*component+pos, new_value, (len+1)*sizeof(WCHAR));
3585 *component_len = len+pos;
3588 *flags |= success_flag;
3589 return S_OK;
3592 static void reset_builder(UriBuilder *builder) {
3593 if(builder->uri)
3594 IUri_Release(&builder->uri->IUri_iface);
3595 builder->uri = NULL;
3597 heap_free(builder->fragment);
3598 builder->fragment = NULL;
3599 builder->fragment_len = 0;
3601 heap_free(builder->host);
3602 builder->host = NULL;
3603 builder->host_len = 0;
3605 heap_free(builder->password);
3606 builder->password = NULL;
3607 builder->password_len = 0;
3609 heap_free(builder->path);
3610 builder->path = NULL;
3611 builder->path_len = 0;
3613 heap_free(builder->query);
3614 builder->query = NULL;
3615 builder->query_len = 0;
3617 heap_free(builder->scheme);
3618 builder->scheme = NULL;
3619 builder->scheme_len = 0;
3621 heap_free(builder->username);
3622 builder->username = NULL;
3623 builder->username_len = 0;
3625 builder->has_port = FALSE;
3626 builder->port = 0;
3627 builder->modified_props = 0;
3630 static HRESULT validate_scheme_name(const UriBuilder *builder, parse_data *data, DWORD flags) {
3631 const WCHAR *component;
3632 const WCHAR *ptr;
3633 const WCHAR **pptr;
3634 DWORD expected_len;
3636 if(builder->scheme) {
3637 ptr = builder->scheme;
3638 expected_len = builder->scheme_len;
3639 } else if(builder->uri && builder->uri->scheme_start > -1) {
3640 ptr = builder->uri->canon_uri+builder->uri->scheme_start;
3641 expected_len = builder->uri->scheme_len;
3642 } else {
3643 static const WCHAR nullW[] = {0};
3644 ptr = nullW;
3645 expected_len = 0;
3648 component = ptr;
3649 pptr = &ptr;
3650 if(parse_scheme(pptr, data, flags, ALLOW_NULL_TERM_SCHEME) &&
3651 data->scheme_len == expected_len) {
3652 if(data->scheme)
3653 TRACE("(%p %p %x): Found valid scheme component %s len=%d.\n", builder, data, flags,
3654 debugstr_wn(data->scheme, data->scheme_len), data->scheme_len);
3655 } else {
3656 TRACE("(%p %p %x): Invalid scheme component found %s.\n", builder, data, flags,
3657 debugstr_wn(component, expected_len));
3658 return INET_E_INVALID_URL;
3661 return S_OK;
3664 static HRESULT validate_username(const UriBuilder *builder, parse_data *data, DWORD flags) {
3665 const WCHAR *ptr;
3666 const WCHAR **pptr;
3667 DWORD expected_len;
3669 if(builder->username) {
3670 ptr = builder->username;
3671 expected_len = builder->username_len;
3672 } else if(!(builder->modified_props & Uri_HAS_USER_NAME) && builder->uri &&
3673 builder->uri->userinfo_start > -1 && builder->uri->userinfo_split != 0) {
3674 /* Just use the username from the base Uri. */
3675 data->username = builder->uri->canon_uri+builder->uri->userinfo_start;
3676 data->username_len = (builder->uri->userinfo_split > -1) ?
3677 builder->uri->userinfo_split : builder->uri->userinfo_len;
3678 ptr = NULL;
3679 } else {
3680 ptr = NULL;
3681 expected_len = 0;
3684 if(ptr) {
3685 const WCHAR *component = ptr;
3686 pptr = &ptr;
3687 if(parse_username(pptr, data, flags, ALLOW_NULL_TERM_USER_NAME) &&
3688 data->username_len == expected_len)
3689 TRACE("(%p %p %x): Found valid username component %s len=%d.\n", builder, data, flags,
3690 debugstr_wn(data->username, data->username_len), data->username_len);
3691 else {
3692 TRACE("(%p %p %x): Invalid username component found %s.\n", builder, data, flags,
3693 debugstr_wn(component, expected_len));
3694 return INET_E_INVALID_URL;
3698 return S_OK;
3701 static HRESULT validate_password(const UriBuilder *builder, parse_data *data, DWORD flags) {
3702 const WCHAR *ptr;
3703 const WCHAR **pptr;
3704 DWORD expected_len;
3706 if(builder->password) {
3707 ptr = builder->password;
3708 expected_len = builder->password_len;
3709 } else if(!(builder->modified_props & Uri_HAS_PASSWORD) && builder->uri &&
3710 builder->uri->userinfo_split > -1) {
3711 data->password = builder->uri->canon_uri+builder->uri->userinfo_start+builder->uri->userinfo_split+1;
3712 data->password_len = builder->uri->userinfo_len-builder->uri->userinfo_split-1;
3713 ptr = NULL;
3714 } else {
3715 ptr = NULL;
3716 expected_len = 0;
3719 if(ptr) {
3720 const WCHAR *component = ptr;
3721 pptr = &ptr;
3722 if(parse_password(pptr, data, flags, ALLOW_NULL_TERM_PASSWORD) &&
3723 data->password_len == expected_len)
3724 TRACE("(%p %p %x): Found valid password component %s len=%d.\n", builder, data, flags,
3725 debugstr_wn(data->password, data->password_len), data->password_len);
3726 else {
3727 TRACE("(%p %p %x): Invalid password component found %s.\n", builder, data, flags,
3728 debugstr_wn(component, expected_len));
3729 return INET_E_INVALID_URL;
3733 return S_OK;
3736 static HRESULT validate_userinfo(const UriBuilder *builder, parse_data *data, DWORD flags) {
3737 HRESULT hr;
3739 hr = validate_username(builder, data, flags);
3740 if(FAILED(hr))
3741 return hr;
3743 hr = validate_password(builder, data, flags);
3744 if(FAILED(hr))
3745 return hr;
3747 return S_OK;
3750 static HRESULT validate_host(const UriBuilder *builder, parse_data *data, DWORD flags) {
3751 const WCHAR *ptr;
3752 const WCHAR **pptr;
3753 DWORD expected_len;
3755 if(builder->host) {
3756 ptr = builder->host;
3757 expected_len = builder->host_len;
3758 } else if(!(builder->modified_props & Uri_HAS_HOST) && builder->uri && builder->uri->host_start > -1) {
3759 ptr = builder->uri->canon_uri + builder->uri->host_start;
3760 expected_len = builder->uri->host_len;
3761 } else
3762 ptr = NULL;
3764 if(ptr) {
3765 const WCHAR *component = ptr;
3766 DWORD extras = ALLOW_BRACKETLESS_IP_LITERAL|IGNORE_PORT_DELIMITER|SKIP_IP_FUTURE_CHECK;
3767 pptr = &ptr;
3769 if(parse_host(pptr, data, flags, extras) && data->host_len == expected_len)
3770 TRACE("(%p %p %x): Found valid host name %s len=%d type=%d.\n", builder, data, flags,
3771 debugstr_wn(data->host, data->host_len), data->host_len, data->host_type);
3772 else {
3773 TRACE("(%p %p %x): Invalid host name found %s.\n", builder, data, flags,
3774 debugstr_wn(component, expected_len));
3775 return INET_E_INVALID_URL;
3779 return S_OK;
3782 static void setup_port(const UriBuilder *builder, parse_data *data, DWORD flags) {
3783 if(builder->modified_props & Uri_HAS_PORT) {
3784 if(builder->has_port) {
3785 data->has_port = TRUE;
3786 data->port_value = builder->port;
3788 } else if(builder->uri && builder->uri->has_port) {
3789 data->has_port = TRUE;
3790 data->port_value = builder->uri->port;
3793 if(data->has_port)
3794 TRACE("(%p %p %x): Using %u as port for IUri.\n", builder, data, flags, data->port_value);
3797 static HRESULT validate_path(const UriBuilder *builder, parse_data *data, DWORD flags) {
3798 const WCHAR *ptr = NULL;
3799 const WCHAR *component;
3800 const WCHAR **pptr;
3801 DWORD expected_len;
3802 BOOL check_len = TRUE;
3803 BOOL valid = FALSE;
3805 if(builder->path) {
3806 ptr = builder->path;
3807 expected_len = builder->path_len;
3808 } else if(!(builder->modified_props & Uri_HAS_PATH) &&
3809 builder->uri && builder->uri->path_start > -1) {
3810 ptr = builder->uri->canon_uri+builder->uri->path_start;
3811 expected_len = builder->uri->path_len;
3812 } else {
3813 static const WCHAR nullW[] = {0};
3814 ptr = nullW;
3815 check_len = FALSE;
3816 expected_len = -1;
3819 component = ptr;
3820 pptr = &ptr;
3822 /* How the path is validated depends on what type of
3823 * URI it is.
3825 valid = data->is_opaque ?
3826 parse_path_opaque(pptr, data, flags) : parse_path_hierarchical(pptr, data, flags);
3828 if(!valid || (check_len && expected_len != data->path_len)) {
3829 TRACE("(%p %p %x): Invalid path component %s.\n", builder, data, flags,
3830 debugstr_wn(component, expected_len) );
3831 return INET_E_INVALID_URL;
3834 TRACE("(%p %p %x): Valid path component %s len=%d.\n", builder, data, flags,
3835 debugstr_wn(data->path, data->path_len), data->path_len);
3837 return S_OK;
3840 static HRESULT validate_query(const UriBuilder *builder, parse_data *data, DWORD flags) {
3841 const WCHAR *ptr = NULL;
3842 const WCHAR **pptr;
3843 DWORD expected_len;
3845 if(builder->query) {
3846 ptr = builder->query;
3847 expected_len = builder->query_len;
3848 } else if(!(builder->modified_props & Uri_HAS_QUERY) && builder->uri &&
3849 builder->uri->query_start > -1) {
3850 ptr = builder->uri->canon_uri+builder->uri->query_start;
3851 expected_len = builder->uri->query_len;
3854 if(ptr) {
3855 const WCHAR *component = ptr;
3856 pptr = &ptr;
3858 if(parse_query(pptr, data, flags) && expected_len == data->query_len)
3859 TRACE("(%p %p %x): Valid query component %s len=%d.\n", builder, data, flags,
3860 debugstr_wn(data->query, data->query_len), data->query_len);
3861 else {
3862 TRACE("(%p %p %x): Invalid query component %s.\n", builder, data, flags,
3863 debugstr_wn(component, expected_len));
3864 return INET_E_INVALID_URL;
3868 return S_OK;
3871 static HRESULT validate_fragment(const UriBuilder *builder, parse_data *data, DWORD flags) {
3872 const WCHAR *ptr = NULL;
3873 const WCHAR **pptr;
3874 DWORD expected_len;
3876 if(builder->fragment) {
3877 ptr = builder->fragment;
3878 expected_len = builder->fragment_len;
3879 } else if(!(builder->modified_props & Uri_HAS_FRAGMENT) && builder->uri &&
3880 builder->uri->fragment_start > -1) {
3881 ptr = builder->uri->canon_uri+builder->uri->fragment_start;
3882 expected_len = builder->uri->fragment_len;
3885 if(ptr) {
3886 const WCHAR *component = ptr;
3887 pptr = &ptr;
3889 if(parse_fragment(pptr, data, flags) && expected_len == data->fragment_len)
3890 TRACE("(%p %p %x): Valid fragment component %s len=%d.\n", builder, data, flags,
3891 debugstr_wn(data->fragment, data->fragment_len), data->fragment_len);
3892 else {
3893 TRACE("(%p %p %x): Invalid fragment component %s.\n", builder, data, flags,
3894 debugstr_wn(component, expected_len));
3895 return INET_E_INVALID_URL;
3899 return S_OK;
3902 static HRESULT validate_components(const UriBuilder *builder, parse_data *data, DWORD flags) {
3903 HRESULT hr;
3905 memset(data, 0, sizeof(parse_data));
3907 TRACE("(%p %p %x): Beginning to validate builder components.\n", builder, data, flags);
3909 hr = validate_scheme_name(builder, data, flags);
3910 if(FAILED(hr))
3911 return hr;
3913 /* Extra validation for file schemes. */
3914 if(data->scheme_type == URL_SCHEME_FILE) {
3915 if((builder->password || (builder->uri && builder->uri->userinfo_split > -1)) ||
3916 (builder->username || (builder->uri && builder->uri->userinfo_start > -1))) {
3917 TRACE("(%p %p %x): File schemes can't contain a username or password.\n",
3918 builder, data, flags);
3919 return INET_E_INVALID_URL;
3923 hr = validate_userinfo(builder, data, flags);
3924 if(FAILED(hr))
3925 return hr;
3927 hr = validate_host(builder, data, flags);
3928 if(FAILED(hr))
3929 return hr;
3931 setup_port(builder, data, flags);
3933 /* The URI is opaque if it doesn't have an authority component. */
3934 if(!data->is_relative)
3935 data->is_opaque = !data->username && !data->password && !data->host && !data->has_port;
3936 else
3937 data->is_opaque = !data->host && !data->has_port;
3939 hr = validate_path(builder, data, flags);
3940 if(FAILED(hr))
3941 return hr;
3943 hr = validate_query(builder, data, flags);
3944 if(FAILED(hr))
3945 return hr;
3947 hr = validate_fragment(builder, data, flags);
3948 if(FAILED(hr))
3949 return hr;
3951 TRACE("(%p %p %x): Finished validating builder components.\n", builder, data, flags);
3953 return S_OK;
3956 static void convert_to_dos_path(const WCHAR *path, DWORD path_len,
3957 WCHAR *output, DWORD *output_len)
3959 const WCHAR *ptr = path;
3961 if(path_len > 3 && *ptr == '/' && is_drive_path(path+1))
3962 /* Skip over the leading / before the drive path. */
3963 ++ptr;
3965 for(; ptr < path+path_len; ++ptr) {
3966 if(*ptr == '/') {
3967 if(output)
3968 *output++ = '\\';
3969 (*output_len)++;
3970 } else {
3971 if(output)
3972 *output++ = *ptr;
3973 (*output_len)++;
3978 /* Generates a raw uri string using the parse_data. */
3979 static DWORD generate_raw_uri(const parse_data *data, BSTR uri, DWORD flags) {
3980 DWORD length = 0;
3982 if(data->scheme) {
3983 if(uri) {
3984 memcpy(uri, data->scheme, data->scheme_len*sizeof(WCHAR));
3985 uri[data->scheme_len] = ':';
3987 length += data->scheme_len+1;
3990 if(!data->is_opaque) {
3991 /* For the "//" which appears before the authority component. */
3992 if(uri) {
3993 uri[length] = '/';
3994 uri[length+1] = '/';
3996 length += 2;
3998 /* Check if we need to add the "\\" before the host name
3999 * of a UNC server name in a DOS path.
4001 if(flags & RAW_URI_CONVERT_TO_DOS_PATH &&
4002 data->scheme_type == URL_SCHEME_FILE && data->host) {
4003 if(uri) {
4004 uri[length] = '\\';
4005 uri[length+1] = '\\';
4007 length += 2;
4011 if(data->username) {
4012 if(uri)
4013 memcpy(uri+length, data->username, data->username_len*sizeof(WCHAR));
4014 length += data->username_len;
4017 if(data->password) {
4018 if(uri) {
4019 uri[length] = ':';
4020 memcpy(uri+length+1, data->password, data->password_len*sizeof(WCHAR));
4022 length += data->password_len+1;
4025 if(data->password || data->username) {
4026 if(uri)
4027 uri[length] = '@';
4028 ++length;
4031 if(data->host) {
4032 /* IPv6 addresses get the brackets added around them if they don't already
4033 * have them.
4035 const BOOL add_brackets = data->host_type == Uri_HOST_IPV6 && *(data->host) != '[';
4036 if(add_brackets) {
4037 if(uri)
4038 uri[length] = '[';
4039 ++length;
4042 if(uri)
4043 memcpy(uri+length, data->host, data->host_len*sizeof(WCHAR));
4044 length += data->host_len;
4046 if(add_brackets) {
4047 if(uri)
4048 uri[length] = ']';
4049 length++;
4053 if(data->has_port) {
4054 /* The port isn't included in the raw uri if it's the default
4055 * port for the scheme type.
4057 DWORD i;
4058 BOOL is_default = FALSE;
4060 for(i = 0; i < sizeof(default_ports)/sizeof(default_ports[0]); ++i) {
4061 if(data->scheme_type == default_ports[i].scheme &&
4062 data->port_value == default_ports[i].port)
4063 is_default = TRUE;
4066 if(!is_default || flags & RAW_URI_FORCE_PORT_DISP) {
4067 if(uri)
4068 uri[length] = ':';
4069 ++length;
4071 if(uri)
4072 length += ui2str(uri+length, data->port_value);
4073 else
4074 length += ui2str(NULL, data->port_value);
4078 /* Check if a '/' should be added before the path for hierarchical URIs. */
4079 if(!data->is_opaque && data->path && *(data->path) != '/') {
4080 if(uri)
4081 uri[length] = '/';
4082 ++length;
4085 if(data->path) {
4086 if(!data->is_opaque && data->scheme_type == URL_SCHEME_FILE &&
4087 flags & RAW_URI_CONVERT_TO_DOS_PATH) {
4088 DWORD len = 0;
4090 if(uri)
4091 convert_to_dos_path(data->path, data->path_len, uri+length, &len);
4092 else
4093 convert_to_dos_path(data->path, data->path_len, NULL, &len);
4095 length += len;
4096 } else {
4097 if(uri)
4098 memcpy(uri+length, data->path, data->path_len*sizeof(WCHAR));
4099 length += data->path_len;
4103 if(data->query) {
4104 if(uri)
4105 memcpy(uri+length, data->query, data->query_len*sizeof(WCHAR));
4106 length += data->query_len;
4109 if(data->fragment) {
4110 if(uri)
4111 memcpy(uri+length, data->fragment, data->fragment_len*sizeof(WCHAR));
4112 length += data->fragment_len;
4115 if(uri)
4116 TRACE("(%p %p): Generated raw uri=%s len=%d\n", data, uri, debugstr_wn(uri, length), length);
4117 else
4118 TRACE("(%p %p): Computed raw uri len=%d\n", data, uri, length);
4120 return length;
4123 static HRESULT generate_uri(const UriBuilder *builder, const parse_data *data, Uri *uri, DWORD flags) {
4124 HRESULT hr;
4125 DWORD length = generate_raw_uri(data, NULL, 0);
4126 uri->raw_uri = SysAllocStringLen(NULL, length);
4127 if(!uri->raw_uri)
4128 return E_OUTOFMEMORY;
4130 generate_raw_uri(data, uri->raw_uri, 0);
4132 hr = canonicalize_uri(data, uri, flags);
4133 if(FAILED(hr)) {
4134 if(hr == E_INVALIDARG)
4135 return INET_E_INVALID_URL;
4136 return hr;
4139 uri->create_flags = flags;
4140 return S_OK;
4143 static inline Uri* impl_from_IUri(IUri *iface)
4145 return CONTAINING_RECORD(iface, Uri, IUri_iface);
4148 static inline void destory_uri_obj(Uri *This)
4150 SysFreeString(This->raw_uri);
4151 heap_free(This->canon_uri);
4152 heap_free(This);
4155 static HRESULT WINAPI Uri_QueryInterface(IUri *iface, REFIID riid, void **ppv)
4157 Uri *This = impl_from_IUri(iface);
4159 if(IsEqualGUID(&IID_IUnknown, riid)) {
4160 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
4161 *ppv = &This->IUri_iface;
4162 }else if(IsEqualGUID(&IID_IUri, riid)) {
4163 TRACE("(%p)->(IID_IUri %p)\n", This, ppv);
4164 *ppv = &This->IUri_iface;
4165 }else if(IsEqualGUID(&IID_IUriBuilderFactory, riid)) {
4166 TRACE("(%p)->(IID_IUriBuilderFactory %p)\n", This, riid);
4167 *ppv = &This->IUriBuilderFactory_iface;
4168 }else if(IsEqualGUID(&IID_IUriObj, riid)) {
4169 TRACE("(%p)->(IID_IUriObj %p)\n", This, ppv);
4170 *ppv = This;
4171 return S_OK;
4172 }else {
4173 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
4174 *ppv = NULL;
4175 return E_NOINTERFACE;
4178 IUnknown_AddRef((IUnknown*)*ppv);
4179 return S_OK;
4182 static ULONG WINAPI Uri_AddRef(IUri *iface)
4184 Uri *This = impl_from_IUri(iface);
4185 LONG ref = InterlockedIncrement(&This->ref);
4187 TRACE("(%p) ref=%d\n", This, ref);
4189 return ref;
4192 static ULONG WINAPI Uri_Release(IUri *iface)
4194 Uri *This = impl_from_IUri(iface);
4195 LONG ref = InterlockedDecrement(&This->ref);
4197 TRACE("(%p) ref=%d\n", This, ref);
4199 if(!ref)
4200 destory_uri_obj(This);
4202 return ref;
4205 static HRESULT WINAPI Uri_GetPropertyBSTR(IUri *iface, Uri_PROPERTY uriProp, BSTR *pbstrProperty, DWORD dwFlags)
4207 Uri *This = impl_from_IUri(iface);
4208 HRESULT hres;
4209 TRACE("(%p)->(%d %p %x)\n", This, uriProp, pbstrProperty, dwFlags);
4211 if(!pbstrProperty)
4212 return E_POINTER;
4214 if(uriProp > Uri_PROPERTY_STRING_LAST) {
4215 /* Windows allocates an empty BSTR for invalid Uri_PROPERTY's. */
4216 *pbstrProperty = SysAllocStringLen(NULL, 0);
4217 if(!(*pbstrProperty))
4218 return E_OUTOFMEMORY;
4220 /* It only returns S_FALSE for the ZONE property... */
4221 if(uriProp == Uri_PROPERTY_ZONE)
4222 return S_FALSE;
4223 else
4224 return S_OK;
4227 /* Don't have support for flags yet. */
4228 if(dwFlags) {
4229 FIXME("(%p)->(%d %p %x)\n", This, uriProp, pbstrProperty, dwFlags);
4230 return E_NOTIMPL;
4233 switch(uriProp) {
4234 case Uri_PROPERTY_ABSOLUTE_URI:
4235 if(This->display_modifiers & URI_DISPLAY_NO_ABSOLUTE_URI) {
4236 *pbstrProperty = SysAllocStringLen(NULL, 0);
4237 hres = S_FALSE;
4238 } else {
4239 if(This->scheme_type != URL_SCHEME_UNKNOWN && This->userinfo_start > -1) {
4240 if(This->userinfo_len == 0) {
4241 /* Don't include the '@' after the userinfo component. */
4242 *pbstrProperty = SysAllocStringLen(NULL, This->canon_len-1);
4243 hres = S_OK;
4244 if(*pbstrProperty) {
4245 /* Copy everything before it. */
4246 memcpy(*pbstrProperty, This->canon_uri, This->userinfo_start*sizeof(WCHAR));
4248 /* And everything after it. */
4249 memcpy(*pbstrProperty+This->userinfo_start, This->canon_uri+This->userinfo_start+1,
4250 (This->canon_len-This->userinfo_start-1)*sizeof(WCHAR));
4252 } else if(This->userinfo_split == 0 && This->userinfo_len == 1) {
4253 /* Don't include the ":@" */
4254 *pbstrProperty = SysAllocStringLen(NULL, This->canon_len-2);
4255 hres = S_OK;
4256 if(*pbstrProperty) {
4257 memcpy(*pbstrProperty, This->canon_uri, This->userinfo_start*sizeof(WCHAR));
4258 memcpy(*pbstrProperty+This->userinfo_start, This->canon_uri+This->userinfo_start+2,
4259 (This->canon_len-This->userinfo_start-2)*sizeof(WCHAR));
4261 } else {
4262 *pbstrProperty = SysAllocString(This->canon_uri);
4263 hres = S_OK;
4265 } else {
4266 *pbstrProperty = SysAllocString(This->canon_uri);
4267 hres = S_OK;
4271 if(!(*pbstrProperty))
4272 hres = E_OUTOFMEMORY;
4274 break;
4275 case Uri_PROPERTY_AUTHORITY:
4276 if(This->authority_start > -1) {
4277 if(This->port_offset > -1 && is_default_port(This->scheme_type, This->port) &&
4278 This->display_modifiers & URI_DISPLAY_NO_DEFAULT_PORT_AUTH)
4279 /* Don't include the port in the authority component. */
4280 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->authority_start, This->port_offset);
4281 else
4282 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->authority_start, This->authority_len);
4283 hres = S_OK;
4284 } else {
4285 *pbstrProperty = SysAllocStringLen(NULL, 0);
4286 hres = S_FALSE;
4289 if(!(*pbstrProperty))
4290 hres = E_OUTOFMEMORY;
4292 break;
4293 case Uri_PROPERTY_DISPLAY_URI:
4294 /* The Display URI contains everything except for the userinfo for known
4295 * scheme types.
4297 if(This->scheme_type != URL_SCHEME_UNKNOWN && This->userinfo_start > -1) {
4298 *pbstrProperty = SysAllocStringLen(NULL, This->canon_len-This->userinfo_len);
4300 if(*pbstrProperty) {
4301 /* Copy everything before the userinfo over. */
4302 memcpy(*pbstrProperty, This->canon_uri, This->userinfo_start*sizeof(WCHAR));
4303 /* Copy everything after the userinfo over. */
4304 memcpy(*pbstrProperty+This->userinfo_start,
4305 This->canon_uri+This->userinfo_start+This->userinfo_len+1,
4306 (This->canon_len-(This->userinfo_start+This->userinfo_len+1))*sizeof(WCHAR));
4308 } else
4309 *pbstrProperty = SysAllocString(This->canon_uri);
4311 if(!(*pbstrProperty))
4312 hres = E_OUTOFMEMORY;
4313 else
4314 hres = S_OK;
4316 break;
4317 case Uri_PROPERTY_DOMAIN:
4318 if(This->domain_offset > -1) {
4319 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->host_start+This->domain_offset,
4320 This->host_len-This->domain_offset);
4321 hres = S_OK;
4322 } else {
4323 *pbstrProperty = SysAllocStringLen(NULL, 0);
4324 hres = S_FALSE;
4327 if(!(*pbstrProperty))
4328 hres = E_OUTOFMEMORY;
4330 break;
4331 case Uri_PROPERTY_EXTENSION:
4332 if(This->extension_offset > -1) {
4333 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->path_start+This->extension_offset,
4334 This->path_len-This->extension_offset);
4335 hres = S_OK;
4336 } else {
4337 *pbstrProperty = SysAllocStringLen(NULL, 0);
4338 hres = S_FALSE;
4341 if(!(*pbstrProperty))
4342 hres = E_OUTOFMEMORY;
4344 break;
4345 case Uri_PROPERTY_FRAGMENT:
4346 if(This->fragment_start > -1) {
4347 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->fragment_start, This->fragment_len);
4348 hres = S_OK;
4349 } else {
4350 *pbstrProperty = SysAllocStringLen(NULL, 0);
4351 hres = S_FALSE;
4354 if(!(*pbstrProperty))
4355 hres = E_OUTOFMEMORY;
4357 break;
4358 case Uri_PROPERTY_HOST:
4359 if(This->host_start > -1) {
4360 /* The '[' and ']' aren't included for IPv6 addresses. */
4361 if(This->host_type == Uri_HOST_IPV6)
4362 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->host_start+1, This->host_len-2);
4363 else
4364 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->host_start, This->host_len);
4366 hres = S_OK;
4367 } else {
4368 *pbstrProperty = SysAllocStringLen(NULL, 0);
4369 hres = S_FALSE;
4372 if(!(*pbstrProperty))
4373 hres = E_OUTOFMEMORY;
4375 break;
4376 case Uri_PROPERTY_PASSWORD:
4377 if(This->userinfo_split > -1) {
4378 *pbstrProperty = SysAllocStringLen(
4379 This->canon_uri+This->userinfo_start+This->userinfo_split+1,
4380 This->userinfo_len-This->userinfo_split-1);
4381 hres = S_OK;
4382 } else {
4383 *pbstrProperty = SysAllocStringLen(NULL, 0);
4384 hres = S_FALSE;
4387 if(!(*pbstrProperty))
4388 return E_OUTOFMEMORY;
4390 break;
4391 case Uri_PROPERTY_PATH:
4392 if(This->path_start > -1) {
4393 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->path_start, This->path_len);
4394 hres = S_OK;
4395 } else {
4396 *pbstrProperty = SysAllocStringLen(NULL, 0);
4397 hres = S_FALSE;
4400 if(!(*pbstrProperty))
4401 hres = E_OUTOFMEMORY;
4403 break;
4404 case Uri_PROPERTY_PATH_AND_QUERY:
4405 if(This->path_start > -1) {
4406 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->path_start, This->path_len+This->query_len);
4407 hres = S_OK;
4408 } else if(This->query_start > -1) {
4409 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->query_start, This->query_len);
4410 hres = S_OK;
4411 } else {
4412 *pbstrProperty = SysAllocStringLen(NULL, 0);
4413 hres = S_FALSE;
4416 if(!(*pbstrProperty))
4417 hres = E_OUTOFMEMORY;
4419 break;
4420 case Uri_PROPERTY_QUERY:
4421 if(This->query_start > -1) {
4422 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->query_start, This->query_len);
4423 hres = S_OK;
4424 } else {
4425 *pbstrProperty = SysAllocStringLen(NULL, 0);
4426 hres = S_FALSE;
4429 if(!(*pbstrProperty))
4430 hres = E_OUTOFMEMORY;
4432 break;
4433 case Uri_PROPERTY_RAW_URI:
4434 *pbstrProperty = SysAllocString(This->raw_uri);
4435 if(!(*pbstrProperty))
4436 hres = E_OUTOFMEMORY;
4437 else
4438 hres = S_OK;
4439 break;
4440 case Uri_PROPERTY_SCHEME_NAME:
4441 if(This->scheme_start > -1) {
4442 *pbstrProperty = SysAllocStringLen(This->canon_uri + This->scheme_start, This->scheme_len);
4443 hres = S_OK;
4444 } else {
4445 *pbstrProperty = SysAllocStringLen(NULL, 0);
4446 hres = S_FALSE;
4449 if(!(*pbstrProperty))
4450 hres = E_OUTOFMEMORY;
4452 break;
4453 case Uri_PROPERTY_USER_INFO:
4454 if(This->userinfo_start > -1) {
4455 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->userinfo_start, This->userinfo_len);
4456 hres = S_OK;
4457 } else {
4458 *pbstrProperty = SysAllocStringLen(NULL, 0);
4459 hres = S_FALSE;
4462 if(!(*pbstrProperty))
4463 hres = E_OUTOFMEMORY;
4465 break;
4466 case Uri_PROPERTY_USER_NAME:
4467 if(This->userinfo_start > -1 && This->userinfo_split != 0) {
4468 /* If userinfo_split is set, that means a password exists
4469 * so the username is only from userinfo_start to userinfo_split.
4471 if(This->userinfo_split > -1) {
4472 *pbstrProperty = SysAllocStringLen(This->canon_uri + This->userinfo_start, This->userinfo_split);
4473 hres = S_OK;
4474 } else {
4475 *pbstrProperty = SysAllocStringLen(This->canon_uri + This->userinfo_start, This->userinfo_len);
4476 hres = S_OK;
4478 } else {
4479 *pbstrProperty = SysAllocStringLen(NULL, 0);
4480 hres = S_FALSE;
4483 if(!(*pbstrProperty))
4484 return E_OUTOFMEMORY;
4486 break;
4487 default:
4488 FIXME("(%p)->(%d %p %x)\n", This, uriProp, pbstrProperty, dwFlags);
4489 hres = E_NOTIMPL;
4492 return hres;
4495 static HRESULT WINAPI Uri_GetPropertyLength(IUri *iface, Uri_PROPERTY uriProp, DWORD *pcchProperty, DWORD dwFlags)
4497 Uri *This = impl_from_IUri(iface);
4498 HRESULT hres;
4499 TRACE("(%p)->(%d %p %x)\n", This, uriProp, pcchProperty, dwFlags);
4501 if(!pcchProperty)
4502 return E_INVALIDARG;
4504 /* Can only return a length for a property if it's a string. */
4505 if(uriProp > Uri_PROPERTY_STRING_LAST)
4506 return E_INVALIDARG;
4508 /* Don't have support for flags yet. */
4509 if(dwFlags) {
4510 FIXME("(%p)->(%d %p %x)\n", This, uriProp, pcchProperty, dwFlags);
4511 return E_NOTIMPL;
4514 switch(uriProp) {
4515 case Uri_PROPERTY_ABSOLUTE_URI:
4516 if(This->display_modifiers & URI_DISPLAY_NO_ABSOLUTE_URI) {
4517 *pcchProperty = 0;
4518 hres = S_FALSE;
4519 } else {
4520 if(This->scheme_type != URL_SCHEME_UNKNOWN) {
4521 if(This->userinfo_start > -1 && This->userinfo_len == 0)
4522 /* Don't include the '@' in the length. */
4523 *pcchProperty = This->canon_len-1;
4524 else if(This->userinfo_start > -1 && This->userinfo_len == 1 &&
4525 This->userinfo_split == 0)
4526 /* Don't include the ":@" in the length. */
4527 *pcchProperty = This->canon_len-2;
4528 else
4529 *pcchProperty = This->canon_len;
4530 } else
4531 *pcchProperty = This->canon_len;
4533 hres = S_OK;
4536 break;
4537 case Uri_PROPERTY_AUTHORITY:
4538 if(This->port_offset > -1 &&
4539 This->display_modifiers & URI_DISPLAY_NO_DEFAULT_PORT_AUTH &&
4540 is_default_port(This->scheme_type, This->port))
4541 /* Only count up until the port in the authority. */
4542 *pcchProperty = This->port_offset;
4543 else
4544 *pcchProperty = This->authority_len;
4545 hres = (This->authority_start > -1) ? S_OK : S_FALSE;
4546 break;
4547 case Uri_PROPERTY_DISPLAY_URI:
4548 if(This->scheme_type != URL_SCHEME_UNKNOWN && This->userinfo_start > -1)
4549 *pcchProperty = This->canon_len-This->userinfo_len-1;
4550 else
4551 *pcchProperty = This->canon_len;
4553 hres = S_OK;
4554 break;
4555 case Uri_PROPERTY_DOMAIN:
4556 if(This->domain_offset > -1)
4557 *pcchProperty = This->host_len - This->domain_offset;
4558 else
4559 *pcchProperty = 0;
4561 hres = (This->domain_offset > -1) ? S_OK : S_FALSE;
4562 break;
4563 case Uri_PROPERTY_EXTENSION:
4564 if(This->extension_offset > -1) {
4565 *pcchProperty = This->path_len - This->extension_offset;
4566 hres = S_OK;
4567 } else {
4568 *pcchProperty = 0;
4569 hres = S_FALSE;
4572 break;
4573 case Uri_PROPERTY_FRAGMENT:
4574 *pcchProperty = This->fragment_len;
4575 hres = (This->fragment_start > -1) ? S_OK : S_FALSE;
4576 break;
4577 case Uri_PROPERTY_HOST:
4578 *pcchProperty = This->host_len;
4580 /* '[' and ']' aren't included in the length. */
4581 if(This->host_type == Uri_HOST_IPV6)
4582 *pcchProperty -= 2;
4584 hres = (This->host_start > -1) ? S_OK : S_FALSE;
4585 break;
4586 case Uri_PROPERTY_PASSWORD:
4587 *pcchProperty = (This->userinfo_split > -1) ? This->userinfo_len-This->userinfo_split-1 : 0;
4588 hres = (This->userinfo_split > -1) ? S_OK : S_FALSE;
4589 break;
4590 case Uri_PROPERTY_PATH:
4591 *pcchProperty = This->path_len;
4592 hres = (This->path_start > -1) ? S_OK : S_FALSE;
4593 break;
4594 case Uri_PROPERTY_PATH_AND_QUERY:
4595 *pcchProperty = This->path_len+This->query_len;
4596 hres = (This->path_start > -1 || This->query_start > -1) ? S_OK : S_FALSE;
4597 break;
4598 case Uri_PROPERTY_QUERY:
4599 *pcchProperty = This->query_len;
4600 hres = (This->query_start > -1) ? S_OK : S_FALSE;
4601 break;
4602 case Uri_PROPERTY_RAW_URI:
4603 *pcchProperty = SysStringLen(This->raw_uri);
4604 hres = S_OK;
4605 break;
4606 case Uri_PROPERTY_SCHEME_NAME:
4607 *pcchProperty = This->scheme_len;
4608 hres = (This->scheme_start > -1) ? S_OK : S_FALSE;
4609 break;
4610 case Uri_PROPERTY_USER_INFO:
4611 *pcchProperty = This->userinfo_len;
4612 hres = (This->userinfo_start > -1) ? S_OK : S_FALSE;
4613 break;
4614 case Uri_PROPERTY_USER_NAME:
4615 *pcchProperty = (This->userinfo_split > -1) ? This->userinfo_split : This->userinfo_len;
4616 if(This->userinfo_split == 0)
4617 hres = S_FALSE;
4618 else
4619 hres = (This->userinfo_start > -1) ? S_OK : S_FALSE;
4620 break;
4621 default:
4622 FIXME("(%p)->(%d %p %x)\n", This, uriProp, pcchProperty, dwFlags);
4623 hres = E_NOTIMPL;
4626 return hres;
4629 static HRESULT WINAPI Uri_GetPropertyDWORD(IUri *iface, Uri_PROPERTY uriProp, DWORD *pcchProperty, DWORD dwFlags)
4631 Uri *This = impl_from_IUri(iface);
4632 HRESULT hres;
4634 TRACE("(%p)->(%d %p %x)\n", This, uriProp, pcchProperty, dwFlags);
4636 if(!pcchProperty)
4637 return E_INVALIDARG;
4639 /* Microsoft's implementation for the ZONE property of a URI seems to be lacking...
4640 * From what I can tell, instead of checking which URLZONE the URI belongs to it
4641 * simply assigns URLZONE_INVALID and returns E_NOTIMPL. This also applies to the GetZone
4642 * function.
4644 if(uriProp == Uri_PROPERTY_ZONE) {
4645 *pcchProperty = URLZONE_INVALID;
4646 return E_NOTIMPL;
4649 if(uriProp < Uri_PROPERTY_DWORD_START) {
4650 *pcchProperty = 0;
4651 return E_INVALIDARG;
4654 switch(uriProp) {
4655 case Uri_PROPERTY_HOST_TYPE:
4656 *pcchProperty = This->host_type;
4657 hres = S_OK;
4658 break;
4659 case Uri_PROPERTY_PORT:
4660 if(!This->has_port) {
4661 *pcchProperty = 0;
4662 hres = S_FALSE;
4663 } else {
4664 *pcchProperty = This->port;
4665 hres = S_OK;
4668 break;
4669 case Uri_PROPERTY_SCHEME:
4670 *pcchProperty = This->scheme_type;
4671 hres = S_OK;
4672 break;
4673 default:
4674 FIXME("(%p)->(%d %p %x)\n", This, uriProp, pcchProperty, dwFlags);
4675 hres = E_NOTIMPL;
4678 return hres;
4681 static HRESULT WINAPI Uri_HasProperty(IUri *iface, Uri_PROPERTY uriProp, BOOL *pfHasProperty)
4683 Uri *This = impl_from_IUri(iface);
4684 TRACE("(%p)->(%d %p)\n", This, uriProp, pfHasProperty);
4686 if(!pfHasProperty)
4687 return E_INVALIDARG;
4689 switch(uriProp) {
4690 case Uri_PROPERTY_ABSOLUTE_URI:
4691 *pfHasProperty = !(This->display_modifiers & URI_DISPLAY_NO_ABSOLUTE_URI);
4692 break;
4693 case Uri_PROPERTY_AUTHORITY:
4694 *pfHasProperty = This->authority_start > -1;
4695 break;
4696 case Uri_PROPERTY_DISPLAY_URI:
4697 *pfHasProperty = TRUE;
4698 break;
4699 case Uri_PROPERTY_DOMAIN:
4700 *pfHasProperty = This->domain_offset > -1;
4701 break;
4702 case Uri_PROPERTY_EXTENSION:
4703 *pfHasProperty = This->extension_offset > -1;
4704 break;
4705 case Uri_PROPERTY_FRAGMENT:
4706 *pfHasProperty = This->fragment_start > -1;
4707 break;
4708 case Uri_PROPERTY_HOST:
4709 *pfHasProperty = This->host_start > -1;
4710 break;
4711 case Uri_PROPERTY_PASSWORD:
4712 *pfHasProperty = This->userinfo_split > -1;
4713 break;
4714 case Uri_PROPERTY_PATH:
4715 *pfHasProperty = This->path_start > -1;
4716 break;
4717 case Uri_PROPERTY_PATH_AND_QUERY:
4718 *pfHasProperty = (This->path_start > -1 || This->query_start > -1);
4719 break;
4720 case Uri_PROPERTY_QUERY:
4721 *pfHasProperty = This->query_start > -1;
4722 break;
4723 case Uri_PROPERTY_RAW_URI:
4724 *pfHasProperty = TRUE;
4725 break;
4726 case Uri_PROPERTY_SCHEME_NAME:
4727 *pfHasProperty = This->scheme_start > -1;
4728 break;
4729 case Uri_PROPERTY_USER_INFO:
4730 *pfHasProperty = This->userinfo_start > -1;
4731 break;
4732 case Uri_PROPERTY_USER_NAME:
4733 if(This->userinfo_split == 0)
4734 *pfHasProperty = FALSE;
4735 else
4736 *pfHasProperty = This->userinfo_start > -1;
4737 break;
4738 case Uri_PROPERTY_HOST_TYPE:
4739 *pfHasProperty = TRUE;
4740 break;
4741 case Uri_PROPERTY_PORT:
4742 *pfHasProperty = This->has_port;
4743 break;
4744 case Uri_PROPERTY_SCHEME:
4745 *pfHasProperty = TRUE;
4746 break;
4747 case Uri_PROPERTY_ZONE:
4748 *pfHasProperty = FALSE;
4749 break;
4750 default:
4751 FIXME("(%p)->(%d %p): Unsupported property type.\n", This, uriProp, pfHasProperty);
4752 return E_NOTIMPL;
4755 return S_OK;
4758 static HRESULT WINAPI Uri_GetAbsoluteUri(IUri *iface, BSTR *pstrAbsoluteUri)
4760 TRACE("(%p)->(%p)\n", iface, pstrAbsoluteUri);
4761 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_ABSOLUTE_URI, pstrAbsoluteUri, 0);
4764 static HRESULT WINAPI Uri_GetAuthority(IUri *iface, BSTR *pstrAuthority)
4766 TRACE("(%p)->(%p)\n", iface, pstrAuthority);
4767 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_AUTHORITY, pstrAuthority, 0);
4770 static HRESULT WINAPI Uri_GetDisplayUri(IUri *iface, BSTR *pstrDisplayUri)
4772 TRACE("(%p)->(%p)\n", iface, pstrDisplayUri);
4773 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_DISPLAY_URI, pstrDisplayUri, 0);
4776 static HRESULT WINAPI Uri_GetDomain(IUri *iface, BSTR *pstrDomain)
4778 TRACE("(%p)->(%p)\n", iface, pstrDomain);
4779 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_DOMAIN, pstrDomain, 0);
4782 static HRESULT WINAPI Uri_GetExtension(IUri *iface, BSTR *pstrExtension)
4784 TRACE("(%p)->(%p)\n", iface, pstrExtension);
4785 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_EXTENSION, pstrExtension, 0);
4788 static HRESULT WINAPI Uri_GetFragment(IUri *iface, BSTR *pstrFragment)
4790 TRACE("(%p)->(%p)\n", iface, pstrFragment);
4791 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_FRAGMENT, pstrFragment, 0);
4794 static HRESULT WINAPI Uri_GetHost(IUri *iface, BSTR *pstrHost)
4796 TRACE("(%p)->(%p)\n", iface, pstrHost);
4797 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_HOST, pstrHost, 0);
4800 static HRESULT WINAPI Uri_GetPassword(IUri *iface, BSTR *pstrPassword)
4802 TRACE("(%p)->(%p)\n", iface, pstrPassword);
4803 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_PASSWORD, pstrPassword, 0);
4806 static HRESULT WINAPI Uri_GetPath(IUri *iface, BSTR *pstrPath)
4808 TRACE("(%p)->(%p)\n", iface, pstrPath);
4809 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_PATH, pstrPath, 0);
4812 static HRESULT WINAPI Uri_GetPathAndQuery(IUri *iface, BSTR *pstrPathAndQuery)
4814 TRACE("(%p)->(%p)\n", iface, pstrPathAndQuery);
4815 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_PATH_AND_QUERY, pstrPathAndQuery, 0);
4818 static HRESULT WINAPI Uri_GetQuery(IUri *iface, BSTR *pstrQuery)
4820 TRACE("(%p)->(%p)\n", iface, pstrQuery);
4821 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_QUERY, pstrQuery, 0);
4824 static HRESULT WINAPI Uri_GetRawUri(IUri *iface, BSTR *pstrRawUri)
4826 TRACE("(%p)->(%p)\n", iface, pstrRawUri);
4827 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_RAW_URI, pstrRawUri, 0);
4830 static HRESULT WINAPI Uri_GetSchemeName(IUri *iface, BSTR *pstrSchemeName)
4832 TRACE("(%p)->(%p)\n", iface, pstrSchemeName);
4833 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_SCHEME_NAME, pstrSchemeName, 0);
4836 static HRESULT WINAPI Uri_GetUserInfo(IUri *iface, BSTR *pstrUserInfo)
4838 TRACE("(%p)->(%p)\n", iface, pstrUserInfo);
4839 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_USER_INFO, pstrUserInfo, 0);
4842 static HRESULT WINAPI Uri_GetUserName(IUri *iface, BSTR *pstrUserName)
4844 TRACE("(%p)->(%p)\n", iface, pstrUserName);
4845 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_USER_NAME, pstrUserName, 0);
4848 static HRESULT WINAPI Uri_GetHostType(IUri *iface, DWORD *pdwHostType)
4850 TRACE("(%p)->(%p)\n", iface, pdwHostType);
4851 return IUri_GetPropertyDWORD(iface, Uri_PROPERTY_HOST_TYPE, pdwHostType, 0);
4854 static HRESULT WINAPI Uri_GetPort(IUri *iface, DWORD *pdwPort)
4856 TRACE("(%p)->(%p)\n", iface, pdwPort);
4857 return IUri_GetPropertyDWORD(iface, Uri_PROPERTY_PORT, pdwPort, 0);
4860 static HRESULT WINAPI Uri_GetScheme(IUri *iface, DWORD *pdwScheme)
4862 TRACE("(%p)->(%p)\n", iface, pdwScheme);
4863 return IUri_GetPropertyDWORD(iface, Uri_PROPERTY_SCHEME, pdwScheme, 0);
4866 static HRESULT WINAPI Uri_GetZone(IUri *iface, DWORD *pdwZone)
4868 TRACE("(%p)->(%p)\n", iface, pdwZone);
4869 return IUri_GetPropertyDWORD(iface, Uri_PROPERTY_ZONE,pdwZone, 0);
4872 static HRESULT WINAPI Uri_GetProperties(IUri *iface, DWORD *pdwProperties)
4874 Uri *This = impl_from_IUri(iface);
4875 TRACE("(%p)->(%p)\n", This, pdwProperties);
4877 if(!pdwProperties)
4878 return E_INVALIDARG;
4880 /* All URIs have these. */
4881 *pdwProperties = Uri_HAS_DISPLAY_URI|Uri_HAS_RAW_URI|Uri_HAS_SCHEME|Uri_HAS_HOST_TYPE;
4883 if(!(This->display_modifiers & URI_DISPLAY_NO_ABSOLUTE_URI))
4884 *pdwProperties |= Uri_HAS_ABSOLUTE_URI;
4886 if(This->scheme_start > -1)
4887 *pdwProperties |= Uri_HAS_SCHEME_NAME;
4889 if(This->authority_start > -1) {
4890 *pdwProperties |= Uri_HAS_AUTHORITY;
4891 if(This->userinfo_start > -1) {
4892 *pdwProperties |= Uri_HAS_USER_INFO;
4893 if(This->userinfo_split != 0)
4894 *pdwProperties |= Uri_HAS_USER_NAME;
4896 if(This->userinfo_split > -1)
4897 *pdwProperties |= Uri_HAS_PASSWORD;
4898 if(This->host_start > -1)
4899 *pdwProperties |= Uri_HAS_HOST;
4900 if(This->domain_offset > -1)
4901 *pdwProperties |= Uri_HAS_DOMAIN;
4904 if(This->has_port)
4905 *pdwProperties |= Uri_HAS_PORT;
4906 if(This->path_start > -1)
4907 *pdwProperties |= Uri_HAS_PATH|Uri_HAS_PATH_AND_QUERY;
4908 if(This->query_start > -1)
4909 *pdwProperties |= Uri_HAS_QUERY|Uri_HAS_PATH_AND_QUERY;
4911 if(This->extension_offset > -1)
4912 *pdwProperties |= Uri_HAS_EXTENSION;
4914 if(This->fragment_start > -1)
4915 *pdwProperties |= Uri_HAS_FRAGMENT;
4917 return S_OK;
4920 static HRESULT WINAPI Uri_IsEqual(IUri *iface, IUri *pUri, BOOL *pfEqual)
4922 Uri *This = impl_from_IUri(iface);
4923 Uri *other;
4925 TRACE("(%p)->(%p %p)\n", This, pUri, pfEqual);
4927 if(!pfEqual)
4928 return E_POINTER;
4930 if(!pUri) {
4931 *pfEqual = FALSE;
4933 /* For some reason Windows returns S_OK here... */
4934 return S_OK;
4937 /* Try to convert it to a Uri (allows for a more simple comparison). */
4938 if((other = get_uri_obj(pUri)))
4939 *pfEqual = are_equal_simple(This, other);
4940 else {
4941 /* Do it the hard way. */
4942 FIXME("(%p)->(%p %p) No support for unknown IUri's yet.\n", iface, pUri, pfEqual);
4943 return E_NOTIMPL;
4946 return S_OK;
4949 static const IUriVtbl UriVtbl = {
4950 Uri_QueryInterface,
4951 Uri_AddRef,
4952 Uri_Release,
4953 Uri_GetPropertyBSTR,
4954 Uri_GetPropertyLength,
4955 Uri_GetPropertyDWORD,
4956 Uri_HasProperty,
4957 Uri_GetAbsoluteUri,
4958 Uri_GetAuthority,
4959 Uri_GetDisplayUri,
4960 Uri_GetDomain,
4961 Uri_GetExtension,
4962 Uri_GetFragment,
4963 Uri_GetHost,
4964 Uri_GetPassword,
4965 Uri_GetPath,
4966 Uri_GetPathAndQuery,
4967 Uri_GetQuery,
4968 Uri_GetRawUri,
4969 Uri_GetSchemeName,
4970 Uri_GetUserInfo,
4971 Uri_GetUserName,
4972 Uri_GetHostType,
4973 Uri_GetPort,
4974 Uri_GetScheme,
4975 Uri_GetZone,
4976 Uri_GetProperties,
4977 Uri_IsEqual
4980 static inline Uri* impl_from_IUriBuilderFactory(IUriBuilderFactory *iface)
4982 return CONTAINING_RECORD(iface, Uri, IUriBuilderFactory_iface);
4985 static HRESULT WINAPI UriBuilderFactory_QueryInterface(IUriBuilderFactory *iface, REFIID riid, void **ppv)
4987 Uri *This = impl_from_IUriBuilderFactory(iface);
4989 if(IsEqualGUID(&IID_IUnknown, riid)) {
4990 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
4991 *ppv = &This->IUriBuilderFactory_iface;
4992 }else if(IsEqualGUID(&IID_IUriBuilderFactory, riid)) {
4993 TRACE("(%p)->(IID_IUriBuilderFactory %p)\n", This, ppv);
4994 *ppv = &This->IUriBuilderFactory_iface;
4995 }else if(IsEqualGUID(&IID_IUri, riid)) {
4996 TRACE("(%p)->(IID_IUri %p)\n", This, ppv);
4997 *ppv = &This->IUri_iface;
4998 }else {
4999 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
5000 *ppv = NULL;
5001 return E_NOINTERFACE;
5004 IUnknown_AddRef((IUnknown*)*ppv);
5005 return S_OK;
5008 static ULONG WINAPI UriBuilderFactory_AddRef(IUriBuilderFactory *iface)
5010 Uri *This = impl_from_IUriBuilderFactory(iface);
5011 LONG ref = InterlockedIncrement(&This->ref);
5013 TRACE("(%p) ref=%d\n", This, ref);
5015 return ref;
5018 static ULONG WINAPI UriBuilderFactory_Release(IUriBuilderFactory *iface)
5020 Uri *This = impl_from_IUriBuilderFactory(iface);
5021 LONG ref = InterlockedDecrement(&This->ref);
5023 TRACE("(%p) ref=%d\n", This, ref);
5025 if(!ref)
5026 destory_uri_obj(This);
5028 return ref;
5031 static HRESULT WINAPI UriBuilderFactory_CreateIUriBuilder(IUriBuilderFactory *iface,
5032 DWORD dwFlags,
5033 DWORD_PTR dwReserved,
5034 IUriBuilder **ppIUriBuilder)
5036 Uri *This = impl_from_IUriBuilderFactory(iface);
5037 TRACE("(%p)->(%08x %08x %p)\n", This, dwFlags, (DWORD)dwReserved, ppIUriBuilder);
5039 if(!ppIUriBuilder)
5040 return E_POINTER;
5042 if(dwFlags || dwReserved) {
5043 *ppIUriBuilder = NULL;
5044 return E_INVALIDARG;
5047 return CreateIUriBuilder(NULL, 0, 0, ppIUriBuilder);
5050 static HRESULT WINAPI UriBuilderFactory_CreateInitializedIUriBuilder(IUriBuilderFactory *iface,
5051 DWORD dwFlags,
5052 DWORD_PTR dwReserved,
5053 IUriBuilder **ppIUriBuilder)
5055 Uri *This = impl_from_IUriBuilderFactory(iface);
5056 TRACE("(%p)->(%08x %08x %p)\n", This, dwFlags, (DWORD)dwReserved, ppIUriBuilder);
5058 if(!ppIUriBuilder)
5059 return E_POINTER;
5061 if(dwFlags || dwReserved) {
5062 *ppIUriBuilder = NULL;
5063 return E_INVALIDARG;
5066 return CreateIUriBuilder(&This->IUri_iface, 0, 0, ppIUriBuilder);
5069 static const IUriBuilderFactoryVtbl UriBuilderFactoryVtbl = {
5070 UriBuilderFactory_QueryInterface,
5071 UriBuilderFactory_AddRef,
5072 UriBuilderFactory_Release,
5073 UriBuilderFactory_CreateIUriBuilder,
5074 UriBuilderFactory_CreateInitializedIUriBuilder
5077 static Uri* create_uri_obj(void) {
5078 Uri *ret = heap_alloc_zero(sizeof(Uri));
5079 if(ret) {
5080 ret->IUri_iface.lpVtbl = &UriVtbl;
5081 ret->IUriBuilderFactory_iface.lpVtbl = &UriBuilderFactoryVtbl;
5082 ret->ref = 1;
5085 return ret;
5088 /***********************************************************************
5089 * CreateUri (urlmon.@)
5091 * Creates a new IUri object using the URI represented by pwzURI. This function
5092 * parses and validates the components of pwzURI and then canonicalizes the
5093 * parsed components.
5095 * PARAMS
5096 * pwzURI [I] The URI to parse, validate, and canonicalize.
5097 * dwFlags [I] Flags which can affect how the parsing/canonicalization is performed.
5098 * dwReserved [I] Reserved (not used).
5099 * ppURI [O] The resulting IUri after parsing/canonicalization occurs.
5101 * RETURNS
5102 * Success: Returns S_OK. ppURI contains the pointer to the newly allocated IUri.
5103 * Failure: E_INVALIDARG if there are invalid flag combinations in dwFlags, or an
5104 * invalid parameter, or pwzURI doesn't represent a valid URI.
5105 * E_OUTOFMEMORY if any memory allocation fails.
5107 * NOTES
5108 * Default flags:
5109 * Uri_CREATE_CANONICALIZE, Uri_CREATE_DECODE_EXTRA_INFO, Uri_CREATE_CRACK_UNKNOWN_SCHEMES,
5110 * Uri_CREATE_PRE_PROCESS_HTML_URI, Uri_CREATE_NO_IE_SETTINGS.
5112 HRESULT WINAPI CreateUri(LPCWSTR pwzURI, DWORD dwFlags, DWORD_PTR dwReserved, IUri **ppURI)
5114 const DWORD supported_flags = Uri_CREATE_ALLOW_RELATIVE|Uri_CREATE_ALLOW_IMPLICIT_WILDCARD_SCHEME|
5115 Uri_CREATE_ALLOW_IMPLICIT_FILE_SCHEME|Uri_CREATE_NO_CANONICALIZE|Uri_CREATE_CANONICALIZE|
5116 Uri_CREATE_DECODE_EXTRA_INFO|Uri_CREATE_NO_DECODE_EXTRA_INFO|Uri_CREATE_CRACK_UNKNOWN_SCHEMES|
5117 Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES|Uri_CREATE_PRE_PROCESS_HTML_URI|Uri_CREATE_NO_PRE_PROCESS_HTML_URI|
5118 Uri_CREATE_NO_IE_SETTINGS|Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS|Uri_CREATE_FILE_USE_DOS_PATH;
5119 Uri *ret;
5120 HRESULT hr;
5121 parse_data data;
5123 TRACE("(%s %x %x %p)\n", debugstr_w(pwzURI), dwFlags, (DWORD)dwReserved, ppURI);
5125 if(!ppURI)
5126 return E_INVALIDARG;
5128 if(!pwzURI) {
5129 *ppURI = NULL;
5130 return E_INVALIDARG;
5133 /* Check for invalid flags. */
5134 if(has_invalid_flag_combination(dwFlags)) {
5135 *ppURI = NULL;
5136 return E_INVALIDARG;
5139 /* Currently unsupported. */
5140 if(dwFlags & ~supported_flags)
5141 FIXME("Ignoring unsupported flag(s) %x\n", dwFlags & ~supported_flags);
5143 ret = create_uri_obj();
5144 if(!ret) {
5145 *ppURI = NULL;
5146 return E_OUTOFMEMORY;
5149 /* Explicitly set the default flags if it doesn't cause a flag conflict. */
5150 apply_default_flags(&dwFlags);
5152 /* Pre process the URI, unless told otherwise. */
5153 if(!(dwFlags & Uri_CREATE_NO_PRE_PROCESS_HTML_URI))
5154 ret->raw_uri = pre_process_uri(pwzURI);
5155 else
5156 ret->raw_uri = SysAllocString(pwzURI);
5158 if(!ret->raw_uri) {
5159 heap_free(ret);
5160 return E_OUTOFMEMORY;
5163 memset(&data, 0, sizeof(parse_data));
5164 data.uri = ret->raw_uri;
5166 /* Validate and parse the URI into it's components. */
5167 if(!parse_uri(&data, dwFlags)) {
5168 /* Encountered an unsupported or invalid URI */
5169 IUri_Release(&ret->IUri_iface);
5170 *ppURI = NULL;
5171 return E_INVALIDARG;
5174 /* Canonicalize the URI. */
5175 hr = canonicalize_uri(&data, ret, dwFlags);
5176 if(FAILED(hr)) {
5177 IUri_Release(&ret->IUri_iface);
5178 *ppURI = NULL;
5179 return hr;
5182 ret->create_flags = dwFlags;
5184 *ppURI = &ret->IUri_iface;
5185 return S_OK;
5188 /***********************************************************************
5189 * CreateUriWithFragment (urlmon.@)
5191 * Creates a new IUri object. This is almost the same as CreateUri, expect that
5192 * it allows you to explicitly specify a fragment (pwzFragment) for pwzURI.
5194 * PARAMS
5195 * pwzURI [I] The URI to parse and perform canonicalization on.
5196 * pwzFragment [I] The explicit fragment string which should be added to pwzURI.
5197 * dwFlags [I] The flags which will be passed to CreateUri.
5198 * dwReserved [I] Reserved (not used).
5199 * ppURI [O] The resulting IUri after parsing/canonicalization.
5201 * RETURNS
5202 * Success: S_OK. ppURI contains the pointer to the newly allocated IUri.
5203 * Failure: E_INVALIDARG if pwzURI already contains a fragment and pwzFragment
5204 * isn't NULL. Will also return E_INVALIDARG for the same reasons as
5205 * CreateUri will. E_OUTOFMEMORY if any allocation fails.
5207 HRESULT WINAPI CreateUriWithFragment(LPCWSTR pwzURI, LPCWSTR pwzFragment, DWORD dwFlags,
5208 DWORD_PTR dwReserved, IUri **ppURI)
5210 HRESULT hres;
5211 TRACE("(%s %s %x %x %p)\n", debugstr_w(pwzURI), debugstr_w(pwzFragment), dwFlags, (DWORD)dwReserved, ppURI);
5213 if(!ppURI)
5214 return E_INVALIDARG;
5216 if(!pwzURI) {
5217 *ppURI = NULL;
5218 return E_INVALIDARG;
5221 /* Check if a fragment should be appended to the URI string. */
5222 if(pwzFragment) {
5223 WCHAR *uriW;
5224 DWORD uri_len, frag_len;
5225 BOOL add_pound;
5227 /* Check if the original URI already has a fragment component. */
5228 if(StrChrW(pwzURI, '#')) {
5229 *ppURI = NULL;
5230 return E_INVALIDARG;
5233 uri_len = lstrlenW(pwzURI);
5234 frag_len = lstrlenW(pwzFragment);
5236 /* If the fragment doesn't start with a '#', one will be added. */
5237 add_pound = *pwzFragment != '#';
5239 if(add_pound)
5240 uriW = heap_alloc((uri_len+frag_len+2)*sizeof(WCHAR));
5241 else
5242 uriW = heap_alloc((uri_len+frag_len+1)*sizeof(WCHAR));
5244 if(!uriW)
5245 return E_OUTOFMEMORY;
5247 memcpy(uriW, pwzURI, uri_len*sizeof(WCHAR));
5248 if(add_pound)
5249 uriW[uri_len++] = '#';
5250 memcpy(uriW+uri_len, pwzFragment, (frag_len+1)*sizeof(WCHAR));
5252 hres = CreateUri(uriW, dwFlags, 0, ppURI);
5254 heap_free(uriW);
5255 } else
5256 /* A fragment string wasn't specified, so just forward the call. */
5257 hres = CreateUri(pwzURI, dwFlags, 0, ppURI);
5259 return hres;
5262 static HRESULT build_uri(const UriBuilder *builder, IUri **uri, DWORD create_flags,
5263 DWORD use_orig_flags, DWORD encoding_mask)
5265 HRESULT hr;
5266 parse_data data;
5267 Uri *ret;
5269 if(!uri)
5270 return E_POINTER;
5272 if(encoding_mask && (!builder->uri || builder->modified_props)) {
5273 *uri = NULL;
5274 return E_NOTIMPL;
5277 /* Decide what flags should be used when creating the Uri. */
5278 if((use_orig_flags & UriBuilder_USE_ORIGINAL_FLAGS) && builder->uri)
5279 create_flags = builder->uri->create_flags;
5280 else {
5281 if(has_invalid_flag_combination(create_flags)) {
5282 *uri = NULL;
5283 return E_INVALIDARG;
5286 /* Set the default flags if they don't cause a conflict. */
5287 apply_default_flags(&create_flags);
5290 /* Return the base IUri if no changes have been made and the create_flags match. */
5291 if(builder->uri && !builder->modified_props && builder->uri->create_flags == create_flags) {
5292 *uri = &builder->uri->IUri_iface;
5293 IUri_AddRef(*uri);
5294 return S_OK;
5297 hr = validate_components(builder, &data, create_flags);
5298 if(FAILED(hr)) {
5299 *uri = NULL;
5300 return hr;
5303 ret = create_uri_obj();
5304 if(!ret) {
5305 *uri = NULL;
5306 return E_OUTOFMEMORY;
5309 hr = generate_uri(builder, &data, ret, create_flags);
5310 if(FAILED(hr)) {
5311 IUri_Release(&ret->IUri_iface);
5312 *uri = NULL;
5313 return hr;
5316 *uri = &ret->IUri_iface;
5317 return S_OK;
5320 static inline UriBuilder* impl_from_IUriBuilder(IUriBuilder *iface)
5322 return CONTAINING_RECORD(iface, UriBuilder, IUriBuilder_iface);
5325 static HRESULT WINAPI UriBuilder_QueryInterface(IUriBuilder *iface, REFIID riid, void **ppv)
5327 UriBuilder *This = impl_from_IUriBuilder(iface);
5329 if(IsEqualGUID(&IID_IUnknown, riid)) {
5330 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
5331 *ppv = &This->IUriBuilder_iface;
5332 }else if(IsEqualGUID(&IID_IUriBuilder, riid)) {
5333 TRACE("(%p)->(IID_IUriBuilder %p)\n", This, ppv);
5334 *ppv = &This->IUriBuilder_iface;
5335 }else {
5336 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
5337 *ppv = NULL;
5338 return E_NOINTERFACE;
5341 IUnknown_AddRef((IUnknown*)*ppv);
5342 return S_OK;
5345 static ULONG WINAPI UriBuilder_AddRef(IUriBuilder *iface)
5347 UriBuilder *This = impl_from_IUriBuilder(iface);
5348 LONG ref = InterlockedIncrement(&This->ref);
5350 TRACE("(%p) ref=%d\n", This, ref);
5352 return ref;
5355 static ULONG WINAPI UriBuilder_Release(IUriBuilder *iface)
5357 UriBuilder *This = impl_from_IUriBuilder(iface);
5358 LONG ref = InterlockedDecrement(&This->ref);
5360 TRACE("(%p) ref=%d\n", This, ref);
5362 if(!ref) {
5363 if(This->uri) IUri_Release(&This->uri->IUri_iface);
5364 heap_free(This->fragment);
5365 heap_free(This->host);
5366 heap_free(This->password);
5367 heap_free(This->path);
5368 heap_free(This->query);
5369 heap_free(This->scheme);
5370 heap_free(This->username);
5371 heap_free(This);
5374 return ref;
5377 static HRESULT WINAPI UriBuilder_CreateUriSimple(IUriBuilder *iface,
5378 DWORD dwAllowEncodingPropertyMask,
5379 DWORD_PTR dwReserved,
5380 IUri **ppIUri)
5382 UriBuilder *This = impl_from_IUriBuilder(iface);
5383 HRESULT hr;
5384 TRACE("(%p)->(%d %d %p)\n", This, dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
5386 hr = build_uri(This, ppIUri, 0, UriBuilder_USE_ORIGINAL_FLAGS, dwAllowEncodingPropertyMask);
5387 if(hr == E_NOTIMPL)
5388 FIXME("(%p)->(%d %d %p)\n", This, dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
5389 return hr;
5392 static HRESULT WINAPI UriBuilder_CreateUri(IUriBuilder *iface,
5393 DWORD dwCreateFlags,
5394 DWORD dwAllowEncodingPropertyMask,
5395 DWORD_PTR dwReserved,
5396 IUri **ppIUri)
5398 UriBuilder *This = impl_from_IUriBuilder(iface);
5399 HRESULT hr;
5400 TRACE("(%p)->(0x%08x %d %d %p)\n", This, dwCreateFlags, dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
5402 if(dwCreateFlags == -1)
5403 hr = build_uri(This, ppIUri, 0, UriBuilder_USE_ORIGINAL_FLAGS, dwAllowEncodingPropertyMask);
5404 else
5405 hr = build_uri(This, ppIUri, dwCreateFlags, 0, dwAllowEncodingPropertyMask);
5407 if(hr == E_NOTIMPL)
5408 FIXME("(%p)->(0x%08x %d %d %p)\n", This, dwCreateFlags, dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
5409 return hr;
5412 static HRESULT WINAPI UriBuilder_CreateUriWithFlags(IUriBuilder *iface,
5413 DWORD dwCreateFlags,
5414 DWORD dwUriBuilderFlags,
5415 DWORD dwAllowEncodingPropertyMask,
5416 DWORD_PTR dwReserved,
5417 IUri **ppIUri)
5419 UriBuilder *This = impl_from_IUriBuilder(iface);
5420 HRESULT hr;
5421 TRACE("(%p)->(0x%08x 0x%08x %d %d %p)\n", This, dwCreateFlags, dwUriBuilderFlags,
5422 dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
5424 hr = build_uri(This, ppIUri, dwCreateFlags, dwUriBuilderFlags, dwAllowEncodingPropertyMask);
5425 if(hr == E_NOTIMPL)
5426 FIXME("(%p)->(0x%08x 0x%08x %d %d %p)\n", This, dwCreateFlags, dwUriBuilderFlags,
5427 dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
5428 return hr;
5431 static HRESULT WINAPI UriBuilder_GetIUri(IUriBuilder *iface, IUri **ppIUri)
5433 UriBuilder *This = impl_from_IUriBuilder(iface);
5434 TRACE("(%p)->(%p)\n", This, ppIUri);
5436 if(!ppIUri)
5437 return E_POINTER;
5439 if(This->uri) {
5440 IUri *uri = &This->uri->IUri_iface;
5441 IUri_AddRef(uri);
5442 *ppIUri = uri;
5443 } else
5444 *ppIUri = NULL;
5446 return S_OK;
5449 static HRESULT WINAPI UriBuilder_SetIUri(IUriBuilder *iface, IUri *pIUri)
5451 UriBuilder *This = impl_from_IUriBuilder(iface);
5452 TRACE("(%p)->(%p)\n", This, pIUri);
5454 if(pIUri) {
5455 Uri *uri;
5457 if((uri = get_uri_obj(pIUri))) {
5458 /* Only reset the builder if it's Uri isn't the same as
5459 * the Uri passed to the function.
5461 if(This->uri != uri) {
5462 reset_builder(This);
5464 This->uri = uri;
5465 if(uri->has_port)
5466 This->port = uri->port;
5468 IUri_AddRef(pIUri);
5470 } else {
5471 FIXME("(%p)->(%p) Unknown IUri types not supported yet.\n", This, pIUri);
5472 return E_NOTIMPL;
5474 } else if(This->uri)
5475 /* Only reset the builder if it's Uri isn't NULL. */
5476 reset_builder(This);
5478 return S_OK;
5481 static HRESULT WINAPI UriBuilder_GetFragment(IUriBuilder *iface, DWORD *pcchFragment, LPCWSTR *ppwzFragment)
5483 UriBuilder *This = impl_from_IUriBuilder(iface);
5484 TRACE("(%p)->(%p %p)\n", This, pcchFragment, ppwzFragment);
5486 if(!This->uri || This->uri->fragment_start == -1 || This->modified_props & Uri_HAS_FRAGMENT)
5487 return get_builder_component(&This->fragment, &This->fragment_len, NULL, 0, ppwzFragment, pcchFragment);
5488 else
5489 return get_builder_component(&This->fragment, &This->fragment_len, This->uri->canon_uri+This->uri->fragment_start,
5490 This->uri->fragment_len, ppwzFragment, pcchFragment);
5493 static HRESULT WINAPI UriBuilder_GetHost(IUriBuilder *iface, DWORD *pcchHost, LPCWSTR *ppwzHost)
5495 UriBuilder *This = impl_from_IUriBuilder(iface);
5496 TRACE("(%p)->(%p %p)\n", This, pcchHost, ppwzHost);
5498 if(!This->uri || This->uri->host_start == -1 || This->modified_props & Uri_HAS_HOST)
5499 return get_builder_component(&This->host, &This->host_len, NULL, 0, ppwzHost, pcchHost);
5500 else {
5501 if(This->uri->host_type == Uri_HOST_IPV6)
5502 /* Don't include the '[' and ']' around the address. */
5503 return get_builder_component(&This->host, &This->host_len, This->uri->canon_uri+This->uri->host_start+1,
5504 This->uri->host_len-2, ppwzHost, pcchHost);
5505 else
5506 return get_builder_component(&This->host, &This->host_len, This->uri->canon_uri+This->uri->host_start,
5507 This->uri->host_len, ppwzHost, pcchHost);
5511 static HRESULT WINAPI UriBuilder_GetPassword(IUriBuilder *iface, DWORD *pcchPassword, LPCWSTR *ppwzPassword)
5513 UriBuilder *This = impl_from_IUriBuilder(iface);
5514 TRACE("(%p)->(%p %p)\n", This, pcchPassword, ppwzPassword);
5516 if(!This->uri || This->uri->userinfo_split == -1 || This->modified_props & Uri_HAS_PASSWORD)
5517 return get_builder_component(&This->password, &This->password_len, NULL, 0, ppwzPassword, pcchPassword);
5518 else {
5519 const WCHAR *start = This->uri->canon_uri+This->uri->userinfo_start+This->uri->userinfo_split+1;
5520 DWORD len = This->uri->userinfo_len-This->uri->userinfo_split-1;
5521 return get_builder_component(&This->password, &This->password_len, start, len, ppwzPassword, pcchPassword);
5525 static HRESULT WINAPI UriBuilder_GetPath(IUriBuilder *iface, DWORD *pcchPath, LPCWSTR *ppwzPath)
5527 UriBuilder *This = impl_from_IUriBuilder(iface);
5528 TRACE("(%p)->(%p %p)\n", This, pcchPath, ppwzPath);
5530 if(!This->uri || This->uri->path_start == -1 || This->modified_props & Uri_HAS_PATH)
5531 return get_builder_component(&This->path, &This->path_len, NULL, 0, ppwzPath, pcchPath);
5532 else
5533 return get_builder_component(&This->path, &This->path_len, This->uri->canon_uri+This->uri->path_start,
5534 This->uri->path_len, ppwzPath, pcchPath);
5537 static HRESULT WINAPI UriBuilder_GetPort(IUriBuilder *iface, BOOL *pfHasPort, DWORD *pdwPort)
5539 UriBuilder *This = impl_from_IUriBuilder(iface);
5540 TRACE("(%p)->(%p %p)\n", This, pfHasPort, pdwPort);
5542 if(!pfHasPort) {
5543 if(pdwPort)
5544 *pdwPort = 0;
5545 return E_POINTER;
5548 if(!pdwPort) {
5549 *pfHasPort = FALSE;
5550 return E_POINTER;
5553 *pfHasPort = This->has_port;
5554 *pdwPort = This->port;
5555 return S_OK;
5558 static HRESULT WINAPI UriBuilder_GetQuery(IUriBuilder *iface, DWORD *pcchQuery, LPCWSTR *ppwzQuery)
5560 UriBuilder *This = impl_from_IUriBuilder(iface);
5561 TRACE("(%p)->(%p %p)\n", This, pcchQuery, ppwzQuery);
5563 if(!This->uri || This->uri->query_start == -1 || This->modified_props & Uri_HAS_QUERY)
5564 return get_builder_component(&This->query, &This->query_len, NULL, 0, ppwzQuery, pcchQuery);
5565 else
5566 return get_builder_component(&This->query, &This->query_len, This->uri->canon_uri+This->uri->query_start,
5567 This->uri->query_len, ppwzQuery, pcchQuery);
5570 static HRESULT WINAPI UriBuilder_GetSchemeName(IUriBuilder *iface, DWORD *pcchSchemeName, LPCWSTR *ppwzSchemeName)
5572 UriBuilder *This = impl_from_IUriBuilder(iface);
5573 TRACE("(%p)->(%p %p)\n", This, pcchSchemeName, ppwzSchemeName);
5575 if(!This->uri || This->uri->scheme_start == -1 || This->modified_props & Uri_HAS_SCHEME_NAME)
5576 return get_builder_component(&This->scheme, &This->scheme_len, NULL, 0, ppwzSchemeName, pcchSchemeName);
5577 else
5578 return get_builder_component(&This->scheme, &This->scheme_len, This->uri->canon_uri+This->uri->scheme_start,
5579 This->uri->scheme_len, ppwzSchemeName, pcchSchemeName);
5582 static HRESULT WINAPI UriBuilder_GetUserName(IUriBuilder *iface, DWORD *pcchUserName, LPCWSTR *ppwzUserName)
5584 UriBuilder *This = impl_from_IUriBuilder(iface);
5585 TRACE("(%p)->(%p %p)\n", This, pcchUserName, ppwzUserName);
5587 if(!This->uri || This->uri->userinfo_start == -1 || This->uri->userinfo_split == 0 ||
5588 This->modified_props & Uri_HAS_USER_NAME)
5589 return get_builder_component(&This->username, &This->username_len, NULL, 0, ppwzUserName, pcchUserName);
5590 else {
5591 const WCHAR *start = This->uri->canon_uri+This->uri->userinfo_start;
5593 /* Check if there's a password in the userinfo section. */
5594 if(This->uri->userinfo_split > -1)
5595 /* Don't include the password. */
5596 return get_builder_component(&This->username, &This->username_len, start,
5597 This->uri->userinfo_split, ppwzUserName, pcchUserName);
5598 else
5599 return get_builder_component(&This->username, &This->username_len, start,
5600 This->uri->userinfo_len, ppwzUserName, pcchUserName);
5604 static HRESULT WINAPI UriBuilder_SetFragment(IUriBuilder *iface, LPCWSTR pwzNewValue)
5606 UriBuilder *This = impl_from_IUriBuilder(iface);
5607 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
5608 return set_builder_component(&This->fragment, &This->fragment_len, pwzNewValue, '#',
5609 &This->modified_props, Uri_HAS_FRAGMENT);
5612 static HRESULT WINAPI UriBuilder_SetHost(IUriBuilder *iface, LPCWSTR pwzNewValue)
5614 UriBuilder *This = impl_from_IUriBuilder(iface);
5615 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
5617 /* Host name can't be set to NULL. */
5618 if(!pwzNewValue)
5619 return E_INVALIDARG;
5621 return set_builder_component(&This->host, &This->host_len, pwzNewValue, 0,
5622 &This->modified_props, Uri_HAS_HOST);
5625 static HRESULT WINAPI UriBuilder_SetPassword(IUriBuilder *iface, LPCWSTR pwzNewValue)
5627 UriBuilder *This = impl_from_IUriBuilder(iface);
5628 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
5629 return set_builder_component(&This->password, &This->password_len, pwzNewValue, 0,
5630 &This->modified_props, Uri_HAS_PASSWORD);
5633 static HRESULT WINAPI UriBuilder_SetPath(IUriBuilder *iface, LPCWSTR pwzNewValue)
5635 UriBuilder *This = impl_from_IUriBuilder(iface);
5636 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
5637 return set_builder_component(&This->path, &This->path_len, pwzNewValue, 0,
5638 &This->modified_props, Uri_HAS_PATH);
5641 static HRESULT WINAPI UriBuilder_SetPort(IUriBuilder *iface, BOOL fHasPort, DWORD dwNewValue)
5643 UriBuilder *This = impl_from_IUriBuilder(iface);
5644 TRACE("(%p)->(%d %d)\n", This, fHasPort, dwNewValue);
5646 This->has_port = fHasPort;
5647 This->port = dwNewValue;
5648 This->modified_props |= Uri_HAS_PORT;
5649 return S_OK;
5652 static HRESULT WINAPI UriBuilder_SetQuery(IUriBuilder *iface, LPCWSTR pwzNewValue)
5654 UriBuilder *This = impl_from_IUriBuilder(iface);
5655 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
5656 return set_builder_component(&This->query, &This->query_len, pwzNewValue, '?',
5657 &This->modified_props, Uri_HAS_QUERY);
5660 static HRESULT WINAPI UriBuilder_SetSchemeName(IUriBuilder *iface, LPCWSTR pwzNewValue)
5662 UriBuilder *This = impl_from_IUriBuilder(iface);
5663 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
5665 /* Only set the scheme name if it's not NULL or empty. */
5666 if(!pwzNewValue || !*pwzNewValue)
5667 return E_INVALIDARG;
5669 return set_builder_component(&This->scheme, &This->scheme_len, pwzNewValue, 0,
5670 &This->modified_props, Uri_HAS_SCHEME_NAME);
5673 static HRESULT WINAPI UriBuilder_SetUserName(IUriBuilder *iface, LPCWSTR pwzNewValue)
5675 UriBuilder *This = impl_from_IUriBuilder(iface);
5676 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
5677 return set_builder_component(&This->username, &This->username_len, pwzNewValue, 0,
5678 &This->modified_props, Uri_HAS_USER_NAME);
5681 static HRESULT WINAPI UriBuilder_RemoveProperties(IUriBuilder *iface, DWORD dwPropertyMask)
5683 const DWORD accepted_flags = Uri_HAS_AUTHORITY|Uri_HAS_DOMAIN|Uri_HAS_EXTENSION|Uri_HAS_FRAGMENT|Uri_HAS_HOST|
5684 Uri_HAS_PASSWORD|Uri_HAS_PATH|Uri_HAS_PATH_AND_QUERY|Uri_HAS_QUERY|
5685 Uri_HAS_USER_INFO|Uri_HAS_USER_NAME;
5687 UriBuilder *This = impl_from_IUriBuilder(iface);
5688 TRACE("(%p)->(0x%08x)\n", This, dwPropertyMask);
5690 if(dwPropertyMask & ~accepted_flags)
5691 return E_INVALIDARG;
5693 if(dwPropertyMask & Uri_HAS_FRAGMENT)
5694 UriBuilder_SetFragment(iface, NULL);
5696 /* Even though you can't set the host name to NULL or an
5697 * empty string, you can still remove it... for some reason.
5699 if(dwPropertyMask & Uri_HAS_HOST)
5700 set_builder_component(&This->host, &This->host_len, NULL, 0,
5701 &This->modified_props, Uri_HAS_HOST);
5703 if(dwPropertyMask & Uri_HAS_PASSWORD)
5704 UriBuilder_SetPassword(iface, NULL);
5706 if(dwPropertyMask & Uri_HAS_PATH)
5707 UriBuilder_SetPath(iface, NULL);
5709 if(dwPropertyMask & Uri_HAS_PORT)
5710 UriBuilder_SetPort(iface, FALSE, 0);
5712 if(dwPropertyMask & Uri_HAS_QUERY)
5713 UriBuilder_SetQuery(iface, NULL);
5715 if(dwPropertyMask & Uri_HAS_USER_NAME)
5716 UriBuilder_SetUserName(iface, NULL);
5718 return S_OK;
5721 static HRESULT WINAPI UriBuilder_HasBeenModified(IUriBuilder *iface, BOOL *pfModified)
5723 UriBuilder *This = impl_from_IUriBuilder(iface);
5724 TRACE("(%p)->(%p)\n", This, pfModified);
5726 if(!pfModified)
5727 return E_POINTER;
5729 *pfModified = This->modified_props > 0;
5730 return S_OK;
5733 static const IUriBuilderVtbl UriBuilderVtbl = {
5734 UriBuilder_QueryInterface,
5735 UriBuilder_AddRef,
5736 UriBuilder_Release,
5737 UriBuilder_CreateUriSimple,
5738 UriBuilder_CreateUri,
5739 UriBuilder_CreateUriWithFlags,
5740 UriBuilder_GetIUri,
5741 UriBuilder_SetIUri,
5742 UriBuilder_GetFragment,
5743 UriBuilder_GetHost,
5744 UriBuilder_GetPassword,
5745 UriBuilder_GetPath,
5746 UriBuilder_GetPort,
5747 UriBuilder_GetQuery,
5748 UriBuilder_GetSchemeName,
5749 UriBuilder_GetUserName,
5750 UriBuilder_SetFragment,
5751 UriBuilder_SetHost,
5752 UriBuilder_SetPassword,
5753 UriBuilder_SetPath,
5754 UriBuilder_SetPort,
5755 UriBuilder_SetQuery,
5756 UriBuilder_SetSchemeName,
5757 UriBuilder_SetUserName,
5758 UriBuilder_RemoveProperties,
5759 UriBuilder_HasBeenModified,
5762 /***********************************************************************
5763 * CreateIUriBuilder (urlmon.@)
5765 HRESULT WINAPI CreateIUriBuilder(IUri *pIUri, DWORD dwFlags, DWORD_PTR dwReserved, IUriBuilder **ppIUriBuilder)
5767 UriBuilder *ret;
5769 TRACE("(%p %x %x %p)\n", pIUri, dwFlags, (DWORD)dwReserved, ppIUriBuilder);
5771 if(!ppIUriBuilder)
5772 return E_POINTER;
5774 ret = heap_alloc_zero(sizeof(UriBuilder));
5775 if(!ret)
5776 return E_OUTOFMEMORY;
5778 ret->IUriBuilder_iface.lpVtbl = &UriBuilderVtbl;
5779 ret->ref = 1;
5781 if(pIUri) {
5782 Uri *uri;
5784 if((uri = get_uri_obj(pIUri))) {
5785 IUri_AddRef(pIUri);
5786 ret->uri = uri;
5788 if(uri->has_port)
5789 /* Windows doesn't set 'has_port' to TRUE in this case. */
5790 ret->port = uri->port;
5792 } else {
5793 heap_free(ret);
5794 *ppIUriBuilder = NULL;
5795 FIXME("(%p %x %x %p): Unknown IUri types not supported yet.\n", pIUri, dwFlags,
5796 (DWORD)dwReserved, ppIUriBuilder);
5797 return E_NOTIMPL;
5801 *ppIUriBuilder = &ret->IUriBuilder_iface;
5802 return S_OK;
5805 /* Merges the base path with the relative path and stores the resulting path
5806 * and path len in 'result' and 'result_len'.
5808 static HRESULT merge_paths(parse_data *data, const WCHAR *base, DWORD base_len, const WCHAR *relative,
5809 DWORD relative_len, WCHAR **result, DWORD *result_len, DWORD flags)
5811 const WCHAR *end = NULL;
5812 DWORD base_copy_len = 0;
5813 WCHAR *ptr;
5815 if(base_len) {
5816 /* Find the characters that will be copied over from
5817 * the base path.
5819 end = memrchrW(base, '/', base_len);
5820 if(!end && data->scheme_type == URL_SCHEME_FILE)
5821 /* Try looking for a '\\'. */
5822 end = memrchrW(base, '\\', base_len);
5825 if(end) {
5826 base_copy_len = (end+1)-base;
5827 *result = heap_alloc((base_copy_len+relative_len+1)*sizeof(WCHAR));
5828 } else
5829 *result = heap_alloc((relative_len+1)*sizeof(WCHAR));
5831 if(!(*result)) {
5832 *result_len = 0;
5833 return E_OUTOFMEMORY;
5836 ptr = *result;
5837 if(end) {
5838 memcpy(ptr, base, base_copy_len*sizeof(WCHAR));
5839 ptr += base_copy_len;
5842 memcpy(ptr, relative, relative_len*sizeof(WCHAR));
5843 ptr += relative_len;
5844 *ptr = '\0';
5846 *result_len = (ptr-*result);
5847 return S_OK;
5850 static HRESULT combine_uri(Uri *base, Uri *relative, DWORD flags, IUri **result, DWORD extras) {
5851 Uri *ret;
5852 HRESULT hr;
5853 parse_data data;
5854 DWORD create_flags = 0, len = 0;
5856 memset(&data, 0, sizeof(parse_data));
5858 /* Base case is when the relative Uri has a scheme name,
5859 * if it does, then 'result' will contain the same data
5860 * as the relative Uri.
5862 if(relative->scheme_start > -1) {
5863 data.uri = SysAllocString(relative->raw_uri);
5864 if(!data.uri) {
5865 *result = NULL;
5866 return E_OUTOFMEMORY;
5869 parse_uri(&data, 0);
5871 ret = create_uri_obj();
5872 if(!ret) {
5873 *result = NULL;
5874 return E_OUTOFMEMORY;
5877 if(extras & COMBINE_URI_FORCE_FLAG_USE) {
5878 if(flags & URL_DONT_SIMPLIFY)
5879 create_flags |= Uri_CREATE_NO_CANONICALIZE;
5880 if(flags & URL_DONT_UNESCAPE_EXTRA_INFO)
5881 create_flags |= Uri_CREATE_NO_DECODE_EXTRA_INFO;
5884 ret->raw_uri = data.uri;
5885 hr = canonicalize_uri(&data, ret, create_flags);
5886 if(FAILED(hr)) {
5887 IUri_Release(&ret->IUri_iface);
5888 *result = NULL;
5889 return hr;
5892 apply_default_flags(&create_flags);
5893 ret->create_flags = create_flags;
5895 *result = &ret->IUri_iface;
5896 } else {
5897 WCHAR *path = NULL;
5898 DWORD raw_flags = 0;
5900 if(base->scheme_start > -1) {
5901 data.scheme = base->canon_uri+base->scheme_start;
5902 data.scheme_len = base->scheme_len;
5903 data.scheme_type = base->scheme_type;
5904 } else {
5905 data.is_relative = TRUE;
5906 data.scheme_type = URL_SCHEME_UNKNOWN;
5907 create_flags |= Uri_CREATE_ALLOW_RELATIVE;
5910 if(base->authority_start > -1) {
5911 if(base->userinfo_start > -1 && base->userinfo_split != 0) {
5912 data.username = base->canon_uri+base->userinfo_start;
5913 data.username_len = (base->userinfo_split > -1) ? base->userinfo_split : base->userinfo_len;
5916 if(base->userinfo_split > -1) {
5917 data.password = base->canon_uri+base->userinfo_start+base->userinfo_split+1;
5918 data.password_len = base->userinfo_len-base->userinfo_split-1;
5921 if(base->host_start > -1) {
5922 data.host = base->canon_uri+base->host_start;
5923 data.host_len = base->host_len;
5924 data.host_type = base->host_type;
5927 if(base->has_port) {
5928 data.has_port = TRUE;
5929 data.port_value = base->port;
5931 } else if(base->scheme_type != URL_SCHEME_FILE)
5932 data.is_opaque = TRUE;
5934 if(relative->path_start == -1 || !relative->path_len) {
5935 if(base->path_start > -1) {
5936 data.path = base->canon_uri+base->path_start;
5937 data.path_len = base->path_len;
5938 } else if((base->path_start == -1 || !base->path_len) && !data.is_opaque) {
5939 /* Just set the path as a '/' if the base didn't have
5940 * one and if it's an hierarchical URI.
5942 static const WCHAR slashW[] = {'/',0};
5943 data.path = slashW;
5944 data.path_len = 1;
5947 if(relative->query_start > -1) {
5948 data.query = relative->canon_uri+relative->query_start;
5949 data.query_len = relative->query_len;
5950 } else if(base->query_start > -1) {
5951 data.query = base->canon_uri+base->query_start;
5952 data.query_len = base->query_len;
5954 } else {
5955 const WCHAR *ptr, **pptr;
5956 DWORD path_offset = 0, path_len = 0;
5958 /* There's two possibilities on what will happen to the path component
5959 * of the result IUri. First, if the relative path begins with a '/'
5960 * then the resulting path will just be the relative path. Second, if
5961 * relative path doesn't begin with a '/' then the base path and relative
5962 * path are merged together.
5964 if(relative->path_len && *(relative->canon_uri+relative->path_start) == '/') {
5965 WCHAR *tmp = NULL;
5966 BOOL copy_drive_path = FALSE;
5968 /* If the relative IUri's path starts with a '/', then we
5969 * don't use the base IUri's path. Unless the base IUri
5970 * is a file URI, in which case it uses the drive path of
5971 * the base IUri (if it has any) in the new path.
5973 if(base->scheme_type == URL_SCHEME_FILE) {
5974 if(base->path_len > 3 && *(base->canon_uri+base->path_start) == '/' &&
5975 is_drive_path(base->canon_uri+base->path_start+1)) {
5976 path_len += 3;
5977 copy_drive_path = TRUE;
5981 path_len += relative->path_len;
5983 path = heap_alloc((path_len+1)*sizeof(WCHAR));
5984 if(!path) {
5985 *result = NULL;
5986 return E_OUTOFMEMORY;
5989 tmp = path;
5991 /* Copy the base paths, drive path over. */
5992 if(copy_drive_path) {
5993 memcpy(tmp, base->canon_uri+base->path_start, 3*sizeof(WCHAR));
5994 tmp += 3;
5997 memcpy(tmp, relative->canon_uri+relative->path_start, relative->path_len*sizeof(WCHAR));
5998 path[path_len] = '\0';
5999 } else {
6000 /* Merge the base path with the relative path. */
6001 hr = merge_paths(&data, base->canon_uri+base->path_start, base->path_len,
6002 relative->canon_uri+relative->path_start, relative->path_len,
6003 &path, &path_len, flags);
6004 if(FAILED(hr)) {
6005 *result = NULL;
6006 return hr;
6009 /* If the resulting IUri is a file URI, the drive path isn't
6010 * reduced out when the dot segments are removed.
6012 if(path_len >= 3 && data.scheme_type == URL_SCHEME_FILE && !data.host) {
6013 if(*path == '/' && is_drive_path(path+1))
6014 path_offset = 2;
6015 else if(is_drive_path(path))
6016 path_offset = 1;
6020 /* Check if the dot segments need to be removed from the path. */
6021 if(!(flags & URL_DONT_SIMPLIFY) && !data.is_opaque) {
6022 DWORD offset = (path_offset > 0) ? path_offset+1 : 0;
6023 DWORD new_len = remove_dot_segments(path+offset,path_len-offset);
6025 if(new_len != path_len) {
6026 WCHAR *tmp = heap_realloc(path, (offset+new_len+1)*sizeof(WCHAR));
6027 if(!tmp) {
6028 heap_free(path);
6029 *result = NULL;
6030 return E_OUTOFMEMORY;
6033 tmp[new_len+offset] = '\0';
6034 path = tmp;
6035 path_len = new_len+offset;
6039 if(relative->query_start > -1) {
6040 data.query = relative->canon_uri+relative->query_start;
6041 data.query_len = relative->query_len;
6044 /* Make sure the path component is valid. */
6045 ptr = path;
6046 pptr = &ptr;
6047 if((data.is_opaque && !parse_path_opaque(pptr, &data, 0)) ||
6048 (!data.is_opaque && !parse_path_hierarchical(pptr, &data, 0))) {
6049 heap_free(path);
6050 *result = NULL;
6051 return E_INVALIDARG;
6055 if(relative->fragment_start > -1) {
6056 data.fragment = relative->canon_uri+relative->fragment_start;
6057 data.fragment_len = relative->fragment_len;
6060 if(flags & URL_DONT_SIMPLIFY)
6061 raw_flags |= RAW_URI_FORCE_PORT_DISP;
6062 if(flags & URL_FILE_USE_PATHURL)
6063 raw_flags |= RAW_URI_CONVERT_TO_DOS_PATH;
6065 len = generate_raw_uri(&data, data.uri, raw_flags);
6066 data.uri = SysAllocStringLen(NULL, len);
6067 if(!data.uri) {
6068 heap_free(path);
6069 *result = NULL;
6070 return E_OUTOFMEMORY;
6073 generate_raw_uri(&data, data.uri, raw_flags);
6075 ret = create_uri_obj();
6076 if(!ret) {
6077 SysFreeString(data.uri);
6078 heap_free(path);
6079 *result = NULL;
6080 return E_OUTOFMEMORY;
6083 if(flags & URL_DONT_SIMPLIFY)
6084 create_flags |= Uri_CREATE_NO_CANONICALIZE;
6085 if(flags & URL_FILE_USE_PATHURL)
6086 create_flags |= Uri_CREATE_FILE_USE_DOS_PATH;
6088 ret->raw_uri = data.uri;
6089 hr = canonicalize_uri(&data, ret, create_flags);
6090 if(FAILED(hr)) {
6091 IUri_Release(&ret->IUri_iface);
6092 *result = NULL;
6093 return hr;
6096 if(flags & URL_DONT_SIMPLIFY)
6097 ret->display_modifiers |= URI_DISPLAY_NO_DEFAULT_PORT_AUTH;
6099 apply_default_flags(&create_flags);
6100 ret->create_flags = create_flags;
6101 *result = &ret->IUri_iface;
6103 heap_free(path);
6106 return S_OK;
6109 /***********************************************************************
6110 * CoInternetCombineIUri (urlmon.@)
6112 HRESULT WINAPI CoInternetCombineIUri(IUri *pBaseUri, IUri *pRelativeUri, DWORD dwCombineFlags,
6113 IUri **ppCombinedUri, DWORD_PTR dwReserved)
6115 HRESULT hr;
6116 IInternetProtocolInfo *info;
6117 Uri *relative, *base;
6118 TRACE("(%p %p %x %p %x)\n", pBaseUri, pRelativeUri, dwCombineFlags, ppCombinedUri, (DWORD)dwReserved);
6120 if(!ppCombinedUri)
6121 return E_INVALIDARG;
6123 if(!pBaseUri || !pRelativeUri) {
6124 *ppCombinedUri = NULL;
6125 return E_INVALIDARG;
6128 relative = get_uri_obj(pRelativeUri);
6129 base = get_uri_obj(pBaseUri);
6130 if(!relative || !base) {
6131 *ppCombinedUri = NULL;
6132 FIXME("(%p %p %x %p %x) Unknown IUri types not supported yet.\n",
6133 pBaseUri, pRelativeUri, dwCombineFlags, ppCombinedUri, (DWORD)dwReserved);
6134 return E_NOTIMPL;
6137 info = get_protocol_info(base->canon_uri);
6138 if(info) {
6139 WCHAR result[INTERNET_MAX_URL_LENGTH+1];
6140 DWORD result_len = 0;
6142 hr = IInternetProtocolInfo_CombineUrl(info, base->canon_uri, relative->canon_uri, dwCombineFlags,
6143 result, INTERNET_MAX_URL_LENGTH+1, &result_len, 0);
6144 IInternetProtocolInfo_Release(info);
6145 if(SUCCEEDED(hr)) {
6146 hr = CreateUri(result, Uri_CREATE_ALLOW_RELATIVE, 0, ppCombinedUri);
6147 if(SUCCEEDED(hr))
6148 return hr;
6152 return combine_uri(base, relative, dwCombineFlags, ppCombinedUri, 0);
6155 /***********************************************************************
6156 * CoInternetCombineUrlEx (urlmon.@)
6158 HRESULT WINAPI CoInternetCombineUrlEx(IUri *pBaseUri, LPCWSTR pwzRelativeUrl, DWORD dwCombineFlags,
6159 IUri **ppCombinedUri, DWORD_PTR dwReserved)
6161 IUri *relative;
6162 Uri *base;
6163 HRESULT hr;
6164 IInternetProtocolInfo *info;
6166 TRACE("(%p %s %x %p %x) stub\n", pBaseUri, debugstr_w(pwzRelativeUrl), dwCombineFlags,
6167 ppCombinedUri, (DWORD)dwReserved);
6169 if(!ppCombinedUri)
6170 return E_POINTER;
6172 if(!pwzRelativeUrl) {
6173 *ppCombinedUri = NULL;
6174 return E_UNEXPECTED;
6177 if(!pBaseUri) {
6178 *ppCombinedUri = NULL;
6179 return E_INVALIDARG;
6182 base = get_uri_obj(pBaseUri);
6183 if(!base) {
6184 *ppCombinedUri = NULL;
6185 FIXME("(%p %s %x %p %x) Unknown IUri's not supported yet.\n", pBaseUri, debugstr_w(pwzRelativeUrl),
6186 dwCombineFlags, ppCombinedUri, (DWORD)dwReserved);
6187 return E_NOTIMPL;
6190 info = get_protocol_info(base->canon_uri);
6191 if(info) {
6192 WCHAR result[INTERNET_MAX_URL_LENGTH+1];
6193 DWORD result_len = 0;
6195 hr = IInternetProtocolInfo_CombineUrl(info, base->canon_uri, pwzRelativeUrl, dwCombineFlags,
6196 result, INTERNET_MAX_URL_LENGTH+1, &result_len, 0);
6197 IInternetProtocolInfo_Release(info);
6198 if(SUCCEEDED(hr)) {
6199 hr = CreateUri(result, Uri_CREATE_ALLOW_RELATIVE, 0, ppCombinedUri);
6200 if(SUCCEEDED(hr))
6201 return hr;
6205 hr = CreateUri(pwzRelativeUrl, Uri_CREATE_ALLOW_RELATIVE, 0, &relative);
6206 if(FAILED(hr)) {
6207 *ppCombinedUri = NULL;
6208 return hr;
6211 hr = combine_uri(base, get_uri_obj(relative), dwCombineFlags, ppCombinedUri, COMBINE_URI_FORCE_FLAG_USE);
6213 IUri_Release(relative);
6214 return hr;
6217 static HRESULT parse_canonicalize(const Uri *uri, DWORD flags, LPWSTR output,
6218 DWORD output_len, DWORD *result_len)
6220 const WCHAR *ptr = NULL;
6221 WCHAR *path = NULL;
6222 const WCHAR **pptr;
6223 WCHAR buffer[INTERNET_MAX_URL_LENGTH+1];
6224 DWORD len = 0;
6225 BOOL reduce_path;
6227 /* URL_UNESCAPE only has effect if none of the URL_ESCAPE flags are set. */
6228 const BOOL allow_unescape = !(flags & URL_ESCAPE_UNSAFE) &&
6229 !(flags & URL_ESCAPE_SPACES_ONLY) &&
6230 !(flags & URL_ESCAPE_PERCENT);
6233 /* Check if the dot segments need to be removed from the
6234 * path component.
6236 if(uri->scheme_start > -1 && uri->path_start > -1) {
6237 ptr = uri->canon_uri+uri->scheme_start+uri->scheme_len+1;
6238 pptr = &ptr;
6240 reduce_path = !(flags & URL_NO_META) &&
6241 !(flags & URL_DONT_SIMPLIFY) &&
6242 ptr && check_hierarchical(pptr);
6244 for(ptr = uri->canon_uri; ptr < uri->canon_uri+uri->canon_len; ++ptr) {
6245 BOOL do_default_action = TRUE;
6247 /* Keep track of the path if we need to remove dot segments from
6248 * it later.
6250 if(reduce_path && !path && ptr == uri->canon_uri+uri->path_start)
6251 path = buffer+len;
6253 /* Check if it's time to reduce the path. */
6254 if(reduce_path && ptr == uri->canon_uri+uri->path_start+uri->path_len) {
6255 DWORD current_path_len = (buffer+len) - path;
6256 DWORD new_path_len = remove_dot_segments(path, current_path_len);
6258 /* Update the current length. */
6259 len -= (current_path_len-new_path_len);
6260 reduce_path = FALSE;
6263 if(*ptr == '%') {
6264 const WCHAR decoded = decode_pct_val(ptr);
6265 if(decoded) {
6266 if(allow_unescape && (flags & URL_UNESCAPE)) {
6267 buffer[len++] = decoded;
6268 ptr += 2;
6269 do_default_action = FALSE;
6273 /* See if %'s needed to encoded. */
6274 if(do_default_action && (flags & URL_ESCAPE_PERCENT)) {
6275 pct_encode_val(*ptr, buffer+len);
6276 len += 3;
6277 do_default_action = FALSE;
6279 } else if(*ptr == ' ') {
6280 if((flags & URL_ESCAPE_SPACES_ONLY) &&
6281 !(flags & URL_ESCAPE_UNSAFE)) {
6282 pct_encode_val(*ptr, buffer+len);
6283 len += 3;
6284 do_default_action = FALSE;
6286 } else if(!is_reserved(*ptr) && !is_unreserved(*ptr)) {
6287 if(flags & URL_ESCAPE_UNSAFE) {
6288 pct_encode_val(*ptr, buffer+len);
6289 len += 3;
6290 do_default_action = FALSE;
6294 if(do_default_action)
6295 buffer[len++] = *ptr;
6298 /* Sometimes the path is the very last component of the IUri, so
6299 * see if the dot segments need to be reduced now.
6301 if(reduce_path && path) {
6302 DWORD current_path_len = (buffer+len) - path;
6303 DWORD new_path_len = remove_dot_segments(path, current_path_len);
6305 /* Update the current length. */
6306 len -= (current_path_len-new_path_len);
6309 buffer[len++] = 0;
6311 /* The null terminator isn't included in the length. */
6312 *result_len = len-1;
6313 if(len > output_len)
6314 return STRSAFE_E_INSUFFICIENT_BUFFER;
6315 else
6316 memcpy(output, buffer, len*sizeof(WCHAR));
6318 return S_OK;
6321 static HRESULT parse_friendly(IUri *uri, LPWSTR output, DWORD output_len,
6322 DWORD *result_len)
6324 HRESULT hr;
6325 DWORD display_len;
6326 BSTR display;
6328 hr = IUri_GetPropertyLength(uri, Uri_PROPERTY_DISPLAY_URI, &display_len, 0);
6329 if(FAILED(hr)) {
6330 *result_len = 0;
6331 return hr;
6334 *result_len = display_len;
6335 if(display_len+1 > output_len)
6336 return STRSAFE_E_INSUFFICIENT_BUFFER;
6338 hr = IUri_GetDisplayUri(uri, &display);
6339 if(FAILED(hr)) {
6340 *result_len = 0;
6341 return hr;
6344 memcpy(output, display, (display_len+1)*sizeof(WCHAR));
6345 SysFreeString(display);
6346 return S_OK;
6349 static HRESULT parse_rootdocument(const Uri *uri, LPWSTR output, DWORD output_len,
6350 DWORD *result_len)
6352 static const WCHAR colon_slashesW[] = {':','/','/'};
6354 WCHAR *ptr;
6355 DWORD len = 0;
6357 /* Windows only returns the root document if the URI has an authority
6358 * and it's not an unknown scheme type or a file scheme type.
6360 if(uri->authority_start == -1 ||
6361 uri->scheme_type == URL_SCHEME_UNKNOWN ||
6362 uri->scheme_type == URL_SCHEME_FILE) {
6363 *result_len = 0;
6364 if(!output_len)
6365 return STRSAFE_E_INSUFFICIENT_BUFFER;
6367 output[0] = 0;
6368 return S_OK;
6371 len = uri->scheme_len+uri->authority_len;
6372 /* For the "://" and '/' which will be added. */
6373 len += 4;
6375 if(len+1 > output_len) {
6376 *result_len = len;
6377 return STRSAFE_E_INSUFFICIENT_BUFFER;
6380 ptr = output;
6381 memcpy(ptr, uri->canon_uri+uri->scheme_start, uri->scheme_len*sizeof(WCHAR));
6383 /* Add the "://". */
6384 ptr += uri->scheme_len;
6385 memcpy(ptr, colon_slashesW, sizeof(colon_slashesW));
6387 /* Add the authority. */
6388 ptr += sizeof(colon_slashesW)/sizeof(WCHAR);
6389 memcpy(ptr, uri->canon_uri+uri->authority_start, uri->authority_len*sizeof(WCHAR));
6391 /* Add the '/' after the authority. */
6392 ptr += uri->authority_len;
6393 *ptr = '/';
6394 ptr[1] = 0;
6396 *result_len = len;
6397 return S_OK;
6400 static HRESULT parse_document(const Uri *uri, LPWSTR output, DWORD output_len,
6401 DWORD *result_len)
6403 DWORD len = 0;
6405 /* It has to be a known scheme type, but, it can't be a file
6406 * scheme. It also has to hierarchical.
6408 if(uri->scheme_type == URL_SCHEME_UNKNOWN ||
6409 uri->scheme_type == URL_SCHEME_FILE ||
6410 uri->authority_start == -1) {
6411 *result_len = 0;
6412 if(output_len < 1)
6413 return STRSAFE_E_INSUFFICIENT_BUFFER;
6415 output[0] = 0;
6416 return S_OK;
6419 if(uri->fragment_start > -1)
6420 len = uri->fragment_start;
6421 else
6422 len = uri->canon_len;
6424 *result_len = len;
6425 if(len+1 > output_len)
6426 return STRSAFE_E_INSUFFICIENT_BUFFER;
6428 memcpy(output, uri->canon_uri, len*sizeof(WCHAR));
6429 output[len] = 0;
6430 return S_OK;
6433 static HRESULT parse_path_from_url(const Uri *uri, LPWSTR output, DWORD output_len,
6434 DWORD *result_len)
6436 const WCHAR *path_ptr;
6437 WCHAR buffer[INTERNET_MAX_URL_LENGTH+1];
6438 WCHAR *ptr;
6440 if(uri->scheme_type != URL_SCHEME_FILE) {
6441 *result_len = 0;
6442 if(output_len > 0)
6443 output[0] = 0;
6444 return E_INVALIDARG;
6447 ptr = buffer;
6448 if(uri->host_start > -1) {
6449 static const WCHAR slash_slashW[] = {'\\','\\'};
6451 memcpy(ptr, slash_slashW, sizeof(slash_slashW));
6452 ptr += sizeof(slash_slashW)/sizeof(WCHAR);
6453 memcpy(ptr, uri->canon_uri+uri->host_start, uri->host_len*sizeof(WCHAR));
6454 ptr += uri->host_len;
6457 path_ptr = uri->canon_uri+uri->path_start;
6458 if(uri->path_len > 3 && *path_ptr == '/' && is_drive_path(path_ptr+1))
6459 /* Skip past the '/' in front of the drive path. */
6460 ++path_ptr;
6462 for(; path_ptr < uri->canon_uri+uri->path_start+uri->path_len; ++path_ptr, ++ptr) {
6463 BOOL do_default_action = TRUE;
6465 if(*path_ptr == '%') {
6466 const WCHAR decoded = decode_pct_val(path_ptr);
6467 if(decoded) {
6468 *ptr = decoded;
6469 path_ptr += 2;
6470 do_default_action = FALSE;
6472 } else if(*path_ptr == '/') {
6473 *ptr = '\\';
6474 do_default_action = FALSE;
6477 if(do_default_action)
6478 *ptr = *path_ptr;
6481 *ptr = 0;
6483 *result_len = ptr-buffer;
6484 if(*result_len+1 > output_len)
6485 return STRSAFE_E_INSUFFICIENT_BUFFER;
6487 memcpy(output, buffer, (*result_len+1)*sizeof(WCHAR));
6488 return S_OK;
6491 static HRESULT parse_url_from_path(IUri *uri, LPWSTR output, DWORD output_len,
6492 DWORD *result_len)
6494 HRESULT hr;
6495 BSTR received;
6496 DWORD len = 0;
6498 hr = IUri_GetPropertyLength(uri, Uri_PROPERTY_ABSOLUTE_URI, &len, 0);
6499 if(FAILED(hr)) {
6500 *result_len = 0;
6501 return hr;
6504 *result_len = len;
6505 if(len+1 > output_len)
6506 return STRSAFE_E_INSUFFICIENT_BUFFER;
6508 hr = IUri_GetAbsoluteUri(uri, &received);
6509 if(FAILED(hr)) {
6510 *result_len = 0;
6511 return hr;
6514 memcpy(output, received, (len+1)*sizeof(WCHAR));
6515 SysFreeString(received);
6517 return S_OK;
6520 static HRESULT parse_schema(IUri *uri, LPWSTR output, DWORD output_len,
6521 DWORD *result_len)
6523 HRESULT hr;
6524 DWORD len;
6525 BSTR received;
6527 hr = IUri_GetPropertyLength(uri, Uri_PROPERTY_SCHEME_NAME, &len, 0);
6528 if(FAILED(hr)) {
6529 *result_len = 0;
6530 return hr;
6533 *result_len = len;
6534 if(len+1 > output_len)
6535 return STRSAFE_E_INSUFFICIENT_BUFFER;
6537 hr = IUri_GetSchemeName(uri, &received);
6538 if(FAILED(hr)) {
6539 *result_len = 0;
6540 return hr;
6543 memcpy(output, received, (len+1)*sizeof(WCHAR));
6544 SysFreeString(received);
6546 return S_OK;
6549 static HRESULT parse_site(IUri *uri, LPWSTR output, DWORD output_len, DWORD *result_len)
6551 HRESULT hr;
6552 DWORD len;
6553 BSTR received;
6555 hr = IUri_GetPropertyLength(uri, Uri_PROPERTY_HOST, &len, 0);
6556 if(FAILED(hr)) {
6557 *result_len = 0;
6558 return hr;
6561 *result_len = len;
6562 if(len+1 > output_len)
6563 return STRSAFE_E_INSUFFICIENT_BUFFER;
6565 hr = IUri_GetHost(uri, &received);
6566 if(FAILED(hr)) {
6567 *result_len = 0;
6568 return hr;
6571 memcpy(output, received, (len+1)*sizeof(WCHAR));
6572 SysFreeString(received);
6574 return S_OK;
6577 static HRESULT parse_domain(IUri *uri, LPWSTR output, DWORD output_len, DWORD *result_len)
6579 HRESULT hr;
6580 DWORD len;
6581 BSTR received;
6583 hr = IUri_GetPropertyLength(uri, Uri_PROPERTY_DOMAIN, &len, 0);
6584 if(FAILED(hr)) {
6585 *result_len = 0;
6586 return hr;
6589 *result_len = len;
6590 if(len+1 > output_len)
6591 return STRSAFE_E_INSUFFICIENT_BUFFER;
6593 hr = IUri_GetDomain(uri, &received);
6594 if(FAILED(hr)) {
6595 *result_len = 0;
6596 return hr;
6599 memcpy(output, received, (len+1)*sizeof(WCHAR));
6600 SysFreeString(received);
6602 return S_OK;
6605 static HRESULT parse_anchor(IUri *uri, LPWSTR output, DWORD output_len, DWORD *result_len)
6607 HRESULT hr;
6608 DWORD len;
6609 BSTR received;
6611 hr = IUri_GetPropertyLength(uri, Uri_PROPERTY_FRAGMENT, &len, 0);
6612 if(FAILED(hr)) {
6613 *result_len = 0;
6614 return hr;
6617 *result_len = len;
6618 if(len+1 > output_len)
6619 return STRSAFE_E_INSUFFICIENT_BUFFER;
6621 hr = IUri_GetFragment(uri, &received);
6622 if(FAILED(hr)) {
6623 *result_len = 0;
6624 return hr;
6627 memcpy(output, received, (len+1)*sizeof(WCHAR));
6628 SysFreeString(received);
6630 return S_OK;
6633 /***********************************************************************
6634 * CoInternetParseIUri (urlmon.@)
6636 HRESULT WINAPI CoInternetParseIUri(IUri *pIUri, PARSEACTION ParseAction, DWORD dwFlags,
6637 LPWSTR pwzResult, DWORD cchResult, DWORD *pcchResult,
6638 DWORD_PTR dwReserved)
6640 HRESULT hr;
6641 Uri *uri;
6642 IInternetProtocolInfo *info;
6644 TRACE("(%p %d %x %p %d %p %x)\n", pIUri, ParseAction, dwFlags, pwzResult,
6645 cchResult, pcchResult, (DWORD)dwReserved);
6647 if(!pcchResult)
6648 return E_POINTER;
6650 if(!pwzResult || !pIUri) {
6651 *pcchResult = 0;
6652 return E_INVALIDARG;
6655 if(!(uri = get_uri_obj(pIUri))) {
6656 *pcchResult = 0;
6657 FIXME("(%p %d %x %p %d %p %x) Unknown IUri's not supported for this action.\n",
6658 pIUri, ParseAction, dwFlags, pwzResult, cchResult, pcchResult, (DWORD)dwReserved);
6659 return E_NOTIMPL;
6662 info = get_protocol_info(uri->canon_uri);
6663 if(info) {
6664 hr = IInternetProtocolInfo_ParseUrl(info, uri->canon_uri, ParseAction, dwFlags,
6665 pwzResult, cchResult, pcchResult, 0);
6666 IInternetProtocolInfo_Release(info);
6667 if(SUCCEEDED(hr)) return hr;
6670 switch(ParseAction) {
6671 case PARSE_CANONICALIZE:
6672 hr = parse_canonicalize(uri, dwFlags, pwzResult, cchResult, pcchResult);
6673 break;
6674 case PARSE_FRIENDLY:
6675 hr = parse_friendly(pIUri, pwzResult, cchResult, pcchResult);
6676 break;
6677 case PARSE_ROOTDOCUMENT:
6678 hr = parse_rootdocument(uri, pwzResult, cchResult, pcchResult);
6679 break;
6680 case PARSE_DOCUMENT:
6681 hr = parse_document(uri, pwzResult, cchResult, pcchResult);
6682 break;
6683 case PARSE_PATH_FROM_URL:
6684 hr = parse_path_from_url(uri, pwzResult, cchResult, pcchResult);
6685 break;
6686 case PARSE_URL_FROM_PATH:
6687 hr = parse_url_from_path(pIUri, pwzResult, cchResult, pcchResult);
6688 break;
6689 case PARSE_SCHEMA:
6690 hr = parse_schema(pIUri, pwzResult, cchResult, pcchResult);
6691 break;
6692 case PARSE_SITE:
6693 hr = parse_site(pIUri, pwzResult, cchResult, pcchResult);
6694 break;
6695 case PARSE_DOMAIN:
6696 hr = parse_domain(pIUri, pwzResult, cchResult, pcchResult);
6697 break;
6698 case PARSE_LOCATION:
6699 case PARSE_ANCHOR:
6700 hr = parse_anchor(pIUri, pwzResult, cchResult, pcchResult);
6701 break;
6702 case PARSE_SECURITY_URL:
6703 case PARSE_MIME:
6704 case PARSE_SERVER:
6705 case PARSE_SECURITY_DOMAIN:
6706 *pcchResult = 0;
6707 hr = E_FAIL;
6708 break;
6709 default:
6710 *pcchResult = 0;
6711 hr = E_NOTIMPL;
6712 FIXME("(%p %d %x %p %d %p %x) Partial stub.\n", pIUri, ParseAction, dwFlags,
6713 pwzResult, cchResult, pcchResult, (DWORD)dwReserved);
6716 return hr;