urlmon: Fixed memory leaks in tests.
[wine.git] / dlls / urlmon / uri.c
blob206fa6b15f7e4953fbd2cd59a1561ab835da7429
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;
159 const WCHAR *scheme;
160 DWORD scheme_len;
161 URL_SCHEME scheme_type;
163 const WCHAR *username;
164 DWORD username_len;
166 const WCHAR *password;
167 DWORD password_len;
169 const WCHAR *host;
170 DWORD host_len;
171 Uri_HOST_TYPE host_type;
173 BOOL has_ipv6;
174 ipv6_address ipv6_address;
176 BOOL has_port;
177 const WCHAR *port;
178 DWORD port_len;
179 DWORD port_value;
181 const WCHAR *path;
182 DWORD path_len;
184 const WCHAR *query;
185 DWORD query_len;
187 const WCHAR *fragment;
188 DWORD fragment_len;
189 } parse_data;
191 static const CHAR hexDigits[] = "0123456789ABCDEF";
193 /* List of scheme types/scheme names that are recognized by the IUri interface as of IE 7. */
194 static const struct {
195 URL_SCHEME scheme;
196 WCHAR scheme_name[16];
197 } recognized_schemes[] = {
198 {URL_SCHEME_FTP, {'f','t','p',0}},
199 {URL_SCHEME_HTTP, {'h','t','t','p',0}},
200 {URL_SCHEME_GOPHER, {'g','o','p','h','e','r',0}},
201 {URL_SCHEME_MAILTO, {'m','a','i','l','t','o',0}},
202 {URL_SCHEME_NEWS, {'n','e','w','s',0}},
203 {URL_SCHEME_NNTP, {'n','n','t','p',0}},
204 {URL_SCHEME_TELNET, {'t','e','l','n','e','t',0}},
205 {URL_SCHEME_WAIS, {'w','a','i','s',0}},
206 {URL_SCHEME_FILE, {'f','i','l','e',0}},
207 {URL_SCHEME_MK, {'m','k',0}},
208 {URL_SCHEME_HTTPS, {'h','t','t','p','s',0}},
209 {URL_SCHEME_SHELL, {'s','h','e','l','l',0}},
210 {URL_SCHEME_SNEWS, {'s','n','e','w','s',0}},
211 {URL_SCHEME_LOCAL, {'l','o','c','a','l',0}},
212 {URL_SCHEME_JAVASCRIPT, {'j','a','v','a','s','c','r','i','p','t',0}},
213 {URL_SCHEME_VBSCRIPT, {'v','b','s','c','r','i','p','t',0}},
214 {URL_SCHEME_ABOUT, {'a','b','o','u','t',0}},
215 {URL_SCHEME_RES, {'r','e','s',0}},
216 {URL_SCHEME_MSSHELLROOTED, {'m','s','-','s','h','e','l','l','-','r','o','o','t','e','d',0}},
217 {URL_SCHEME_MSSHELLIDLIST, {'m','s','-','s','h','e','l','l','-','i','d','l','i','s','t',0}},
218 {URL_SCHEME_MSHELP, {'h','c','p',0}},
219 {URL_SCHEME_WILDCARD, {'*',0}}
222 /* List of default ports Windows recognizes. */
223 static const struct {
224 URL_SCHEME scheme;
225 USHORT port;
226 } default_ports[] = {
227 {URL_SCHEME_FTP, 21},
228 {URL_SCHEME_HTTP, 80},
229 {URL_SCHEME_GOPHER, 70},
230 {URL_SCHEME_NNTP, 119},
231 {URL_SCHEME_TELNET, 23},
232 {URL_SCHEME_WAIS, 210},
233 {URL_SCHEME_HTTPS, 443},
236 /* List of 3 character top level domain names Windows seems to recognize.
237 * There might be more, but, these are the only ones I've found so far.
239 static const struct {
240 WCHAR tld_name[4];
241 } recognized_tlds[] = {
242 {{'c','o','m',0}},
243 {{'e','d','u',0}},
244 {{'g','o','v',0}},
245 {{'i','n','t',0}},
246 {{'m','i','l',0}},
247 {{'n','e','t',0}},
248 {{'o','r','g',0}}
251 static Uri *get_uri_obj(IUri *uri)
253 Uri *ret;
254 HRESULT hres;
256 hres = IUri_QueryInterface(uri, &IID_IUriObj, (void**)&ret);
257 return SUCCEEDED(hres) ? ret : NULL;
260 static inline BOOL is_alpha(WCHAR val) {
261 return ((val >= 'a' && val <= 'z') || (val >= 'A' && val <= 'Z'));
264 static inline BOOL is_num(WCHAR val) {
265 return (val >= '0' && val <= '9');
268 static inline BOOL is_drive_path(const WCHAR *str) {
269 return (is_alpha(str[0]) && (str[1] == ':' || str[1] == '|'));
272 static inline BOOL is_unc_path(const WCHAR *str) {
273 return (str[0] == '\\' && str[0] == '\\');
276 static inline BOOL is_forbidden_dos_path_char(WCHAR val) {
277 return (val == '>' || val == '<' || val == '\"');
280 /* A URI is implicitly a file path if it begins with
281 * a drive letter (eg X:) or starts with "\\" (UNC path).
283 static inline BOOL is_implicit_file_path(const WCHAR *str) {
284 return (is_unc_path(str) || (is_alpha(str[0]) && str[1] == ':'));
287 /* Checks if the URI is a hierarchical URI. A hierarchical
288 * URI is one that has "//" after the scheme.
290 static BOOL check_hierarchical(const WCHAR **ptr) {
291 const WCHAR *start = *ptr;
293 if(**ptr != '/')
294 return FALSE;
296 ++(*ptr);
297 if(**ptr != '/') {
298 *ptr = start;
299 return FALSE;
302 ++(*ptr);
303 return TRUE;
306 /* unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~" */
307 static inline BOOL is_unreserved(WCHAR val) {
308 return (is_alpha(val) || is_num(val) || val == '-' || val == '.' ||
309 val == '_' || val == '~');
312 /* sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
313 * / "*" / "+" / "," / ";" / "="
315 static inline BOOL is_subdelim(WCHAR val) {
316 return (val == '!' || val == '$' || val == '&' ||
317 val == '\'' || val == '(' || val == ')' ||
318 val == '*' || val == '+' || val == ',' ||
319 val == ';' || val == '=');
322 /* gen-delims = ":" / "/" / "?" / "#" / "[" / "]" / "@" */
323 static inline BOOL is_gendelim(WCHAR val) {
324 return (val == ':' || val == '/' || val == '?' ||
325 val == '#' || val == '[' || val == ']' ||
326 val == '@');
329 /* Characters that delimit the end of the authority
330 * section of a URI. Sometimes a '\\' is considered
331 * an authority delimeter.
333 static inline BOOL is_auth_delim(WCHAR val, BOOL acceptSlash) {
334 return (val == '#' || val == '/' || val == '?' ||
335 val == '\0' || (acceptSlash && val == '\\'));
338 /* reserved = gen-delims / sub-delims */
339 static inline BOOL is_reserved(WCHAR val) {
340 return (is_subdelim(val) || is_gendelim(val));
343 static inline BOOL is_hexdigit(WCHAR val) {
344 return ((val >= 'a' && val <= 'f') ||
345 (val >= 'A' && val <= 'F') ||
346 (val >= '0' && val <= '9'));
349 static inline BOOL is_path_delim(WCHAR val) {
350 return (!val || val == '#' || val == '?');
353 static BOOL is_default_port(URL_SCHEME scheme, DWORD port) {
354 DWORD i;
356 for(i = 0; i < sizeof(default_ports)/sizeof(default_ports[0]); ++i) {
357 if(default_ports[i].scheme == scheme && default_ports[i].port)
358 return TRUE;
361 return FALSE;
364 /* List of schemes types Windows seems to expect to be hierarchical. */
365 static inline BOOL is_hierarchical_scheme(URL_SCHEME type) {
366 return(type == URL_SCHEME_HTTP || type == URL_SCHEME_FTP ||
367 type == URL_SCHEME_GOPHER || type == URL_SCHEME_NNTP ||
368 type == URL_SCHEME_TELNET || type == URL_SCHEME_WAIS ||
369 type == URL_SCHEME_FILE || type == URL_SCHEME_HTTPS ||
370 type == URL_SCHEME_RES);
373 /* Checks if 'flags' contains an invalid combination of Uri_CREATE flags. */
374 static inline BOOL has_invalid_flag_combination(DWORD flags) {
375 return((flags & Uri_CREATE_DECODE_EXTRA_INFO && flags & Uri_CREATE_NO_DECODE_EXTRA_INFO) ||
376 (flags & Uri_CREATE_CANONICALIZE && flags & Uri_CREATE_NO_CANONICALIZE) ||
377 (flags & Uri_CREATE_CRACK_UNKNOWN_SCHEMES && flags & Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES) ||
378 (flags & Uri_CREATE_PRE_PROCESS_HTML_URI && flags & Uri_CREATE_NO_PRE_PROCESS_HTML_URI) ||
379 (flags & Uri_CREATE_IE_SETTINGS && flags & Uri_CREATE_NO_IE_SETTINGS));
382 /* Applies each default Uri_CREATE flags to 'flags' if it
383 * doesn't cause a flag conflict.
385 static void apply_default_flags(DWORD *flags) {
386 if(!(*flags & Uri_CREATE_NO_CANONICALIZE))
387 *flags |= Uri_CREATE_CANONICALIZE;
388 if(!(*flags & Uri_CREATE_NO_DECODE_EXTRA_INFO))
389 *flags |= Uri_CREATE_DECODE_EXTRA_INFO;
390 if(!(*flags & Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES))
391 *flags |= Uri_CREATE_CRACK_UNKNOWN_SCHEMES;
392 if(!(*flags & Uri_CREATE_NO_PRE_PROCESS_HTML_URI))
393 *flags |= Uri_CREATE_PRE_PROCESS_HTML_URI;
394 if(!(*flags & Uri_CREATE_IE_SETTINGS))
395 *flags |= Uri_CREATE_NO_IE_SETTINGS;
398 /* Determines if the URI is hierarchical using the information already parsed into
399 * data and using the current location of parsing in the URI string.
401 * Windows considers a URI hierarchical if on of the following is true:
402 * A.) It's a wildcard scheme.
403 * B.) It's an implicit file scheme.
404 * C.) It's a known hierarchical scheme and it has two '\\' after the scheme name.
405 * (the '\\' will be converted into "//" during canonicalization).
406 * D.) It's not a relative URI and "//" appears after the scheme name.
408 static inline BOOL is_hierarchical_uri(const WCHAR **ptr, const parse_data *data) {
409 const WCHAR *start = *ptr;
411 if(data->scheme_type == URL_SCHEME_WILDCARD)
412 return TRUE;
413 else if(data->scheme_type == URL_SCHEME_FILE && data->has_implicit_scheme)
414 return TRUE;
415 else if(is_hierarchical_scheme(data->scheme_type) && (*ptr)[0] == '\\' && (*ptr)[1] == '\\') {
416 *ptr += 2;
417 return TRUE;
418 } else if(!data->is_relative && check_hierarchical(ptr))
419 return TRUE;
421 *ptr = start;
422 return FALSE;
425 /* Checks if the two Uri's are logically equivalent. It's a simple
426 * comparison, since they are both of type Uri, and it can access
427 * the properties of each Uri directly without the need to go
428 * through the "IUri_Get*" interface calls.
430 static BOOL are_equal_simple(const Uri *a, const Uri *b) {
431 if(a->scheme_type == b->scheme_type) {
432 const BOOL known_scheme = a->scheme_type != URL_SCHEME_UNKNOWN;
433 const BOOL are_hierarchical =
434 (a->authority_start > -1 && b->authority_start > -1);
436 if(a->scheme_type == URL_SCHEME_FILE) {
437 if(a->canon_len == b->canon_len)
438 return !StrCmpIW(a->canon_uri, b->canon_uri);
441 /* Only compare the scheme names (if any) if their unknown scheme types. */
442 if(!known_scheme) {
443 if((a->scheme_start > -1 && b->scheme_start > -1) &&
444 (a->scheme_len == b->scheme_len)) {
445 /* Make sure the schemes are the same. */
446 if(StrCmpNW(a->canon_uri+a->scheme_start, b->canon_uri+b->scheme_start, a->scheme_len))
447 return FALSE;
448 } else if(a->scheme_len != b->scheme_len)
449 /* One of the Uri's has a scheme name, while the other doesn't. */
450 return FALSE;
453 /* If they have a userinfo component, perform case sensitive compare. */
454 if((a->userinfo_start > -1 && b->userinfo_start > -1) &&
455 (a->userinfo_len == b->userinfo_len)) {
456 if(StrCmpNW(a->canon_uri+a->userinfo_start, b->canon_uri+b->userinfo_start, a->userinfo_len))
457 return FALSE;
458 } else if(a->userinfo_len != b->userinfo_len)
459 /* One of the Uri's had a userinfo, while the other one doesn't. */
460 return FALSE;
462 /* Check if they have a host name. */
463 if((a->host_start > -1 && b->host_start > -1) &&
464 (a->host_len == b->host_len)) {
465 /* Perform a case insensitive compare if they are a known scheme type. */
466 if(known_scheme) {
467 if(StrCmpNIW(a->canon_uri+a->host_start, b->canon_uri+b->host_start, a->host_len))
468 return FALSE;
469 } else if(StrCmpNW(a->canon_uri+a->host_start, b->canon_uri+b->host_start, a->host_len))
470 return FALSE;
471 } else if(a->host_len != b->host_len)
472 /* One of the Uri's had a host, while the other one didn't. */
473 return FALSE;
475 if(a->has_port && b->has_port) {
476 if(a->port != b->port)
477 return FALSE;
478 } else if(a->has_port || b->has_port)
479 /* One had a port, while the other one didn't. */
480 return FALSE;
482 /* Windows is weird with how it handles paths. For example
483 * One URI could be "http://google.com" (after canonicalization)
484 * and one could be "http://google.com/" and the IsEqual function
485 * would still evaluate to TRUE, but, only if they are both hierarchical
486 * URIs.
488 if((a->path_start > -1 && b->path_start > -1) &&
489 (a->path_len == b->path_len)) {
490 if(StrCmpNW(a->canon_uri+a->path_start, b->canon_uri+b->path_start, a->path_len))
491 return FALSE;
492 } else if(are_hierarchical && a->path_len == -1 && b->path_len == 0) {
493 if(*(a->canon_uri+a->path_start) != '/')
494 return FALSE;
495 } else if(are_hierarchical && b->path_len == 1 && a->path_len == 0) {
496 if(*(b->canon_uri+b->path_start) != '/')
497 return FALSE;
498 } else if(a->path_len != b->path_len)
499 return FALSE;
501 /* Compare the query strings of the two URIs. */
502 if((a->query_start > -1 && b->query_start > -1) &&
503 (a->query_len == b->query_len)) {
504 if(StrCmpNW(a->canon_uri+a->query_start, b->canon_uri+b->query_start, a->query_len))
505 return FALSE;
506 } else if(a->query_len != b->query_len)
507 return FALSE;
509 if((a->fragment_start > -1 && b->fragment_start > -1) &&
510 (a->fragment_len == b->fragment_len)) {
511 if(StrCmpNW(a->canon_uri+a->fragment_start, b->canon_uri+b->fragment_start, a->fragment_len))
512 return FALSE;
513 } else if(a->fragment_len != b->fragment_len)
514 return FALSE;
516 /* If we get here, the two URIs are equivalent. */
517 return TRUE;
520 return FALSE;
523 /* Computes the size of the given IPv6 address.
524 * Each h16 component is 16bits, if there is an IPv4 address, it's
525 * 32bits. If there's an elision it can be 16bits to 128bits, depending
526 * on the number of other components.
528 * Modeled after google-url's CheckIPv6ComponentsSize function
530 static void compute_ipv6_comps_size(ipv6_address *address) {
531 address->components_size = address->h16_count * 2;
533 if(address->ipv4)
534 /* IPv4 address is 4 bytes. */
535 address->components_size += 4;
537 if(address->elision) {
538 /* An elision can be anywhere from 2 bytes up to 16 bytes.
539 * It size depends on the size of the h16 and IPv4 components.
541 address->elision_size = 16 - address->components_size;
542 if(address->elision_size < 2)
543 address->elision_size = 2;
544 } else
545 address->elision_size = 0;
548 /* Taken from dlls/jscript/lex.c */
549 static int hex_to_int(WCHAR val) {
550 if(val >= '0' && val <= '9')
551 return val - '0';
552 else if(val >= 'a' && val <= 'f')
553 return val - 'a' + 10;
554 else if(val >= 'A' && val <= 'F')
555 return val - 'A' + 10;
557 return -1;
560 /* Helper function for converting a percent encoded string
561 * representation of a WCHAR value into its actual WCHAR value. If
562 * the two characters following the '%' aren't valid hex values then
563 * this function returns the NULL character.
565 * Eg.
566 * "%2E" will result in '.' being returned by this function.
568 static WCHAR decode_pct_val(const WCHAR *ptr) {
569 WCHAR ret = '\0';
571 if(*ptr == '%' && is_hexdigit(*(ptr + 1)) && is_hexdigit(*(ptr + 2))) {
572 INT a = hex_to_int(*(ptr + 1));
573 INT b = hex_to_int(*(ptr + 2));
575 ret = a << 4;
576 ret += b;
579 return ret;
582 /* Helper function for percent encoding a given character
583 * and storing the encoded value into a given buffer (dest).
585 * It's up to the calling function to ensure that there is
586 * at least enough space in 'dest' for the percent encoded
587 * value to be stored (so dest + 3 spaces available).
589 static inline void pct_encode_val(WCHAR val, WCHAR *dest) {
590 dest[0] = '%';
591 dest[1] = hexDigits[(val >> 4) & 0xf];
592 dest[2] = hexDigits[val & 0xf];
595 /* Scans the range of characters [str, end] and returns the last occurrence
596 * of 'ch' or returns NULL.
598 static const WCHAR *str_last_of(const WCHAR *str, const WCHAR *end, WCHAR ch) {
599 const WCHAR *ptr = end;
601 while(ptr >= str) {
602 if(*ptr == ch)
603 return ptr;
604 --ptr;
607 return NULL;
610 /* Attempts to parse the domain name from the host.
612 * This function also includes the Top-level Domain (TLD) name
613 * of the host when it tries to find the domain name. If it finds
614 * a valid domain name it will assign 'domain_start' the offset
615 * into 'host' where the domain name starts.
617 * It's implied that if there is a domain name its range is:
618 * [host+domain_start, host+host_len).
620 static void find_domain_name(const WCHAR *host, DWORD host_len,
621 INT *domain_start) {
622 const WCHAR *last_tld, *sec_last_tld, *end;
624 end = host+host_len-1;
626 *domain_start = -1;
628 /* There has to be at least enough room for a '.' followed by a
629 * 3 character TLD for a domain to even exist in the host name.
631 if(host_len < 4)
632 return;
634 last_tld = str_last_of(host, end, '.');
635 if(!last_tld)
636 /* http://hostname -> has no domain name. */
637 return;
639 sec_last_tld = str_last_of(host, last_tld-1, '.');
640 if(!sec_last_tld) {
641 /* If the '.' is at the beginning of the host there
642 * has to be at least 3 characters in the TLD for it
643 * to be valid.
644 * Ex: .com -> .com as the domain name.
645 * .co -> has no domain name.
647 if(last_tld-host == 0) {
648 if(end-(last_tld-1) < 3)
649 return;
650 } else if(last_tld-host == 3) {
651 DWORD i;
653 /* If there's three characters in front of last_tld and
654 * they are on the list of recognized TLDs, then this
655 * host doesn't have a domain (since the host only contains
656 * a TLD name.
657 * Ex: edu.uk -> has no domain name.
658 * foo.uk -> foo.uk as the domain name.
660 for(i = 0; i < sizeof(recognized_tlds)/sizeof(recognized_tlds[0]); ++i) {
661 if(!StrCmpNIW(host, recognized_tlds[i].tld_name, 3))
662 return;
664 } else if(last_tld-host < 3)
665 /* Anything less than 3 characters is considered part
666 * of the TLD name.
667 * Ex: ak.uk -> Has no domain name.
669 return;
671 /* Otherwise the domain name is the whole host name. */
672 *domain_start = 0;
673 } else if(end+1-last_tld > 3) {
674 /* If the last_tld has more than 3 characters, then it's automatically
675 * considered the TLD of the domain name.
676 * Ex: www.winehq.org.uk.test -> uk.test as the domain name.
678 *domain_start = (sec_last_tld+1)-host;
679 } else if(last_tld - (sec_last_tld+1) < 4) {
680 DWORD i;
681 /* If the sec_last_tld is 3 characters long it HAS to be on the list of
682 * recognized to still be considered part of the TLD name, otherwise
683 * its considered the domain name.
684 * Ex: www.google.com.uk -> google.com.uk as the domain name.
685 * www.google.foo.uk -> foo.uk as the domain name.
687 if(last_tld - (sec_last_tld+1) == 3) {
688 for(i = 0; i < sizeof(recognized_tlds)/sizeof(recognized_tlds[0]); ++i) {
689 if(!StrCmpNIW(sec_last_tld+1, recognized_tlds[i].tld_name, 3)) {
690 const WCHAR *domain = str_last_of(host, sec_last_tld-1, '.');
692 if(!domain)
693 *domain_start = 0;
694 else
695 *domain_start = (domain+1) - host;
696 TRACE("Found domain name %s\n", debugstr_wn(host+*domain_start,
697 (host+host_len)-(host+*domain_start)));
698 return;
702 *domain_start = (sec_last_tld+1)-host;
703 } else {
704 /* Since the sec_last_tld is less than 3 characters it's considered
705 * part of the TLD.
706 * Ex: www.google.fo.uk -> google.fo.uk as the domain name.
708 const WCHAR *domain = str_last_of(host, sec_last_tld-1, '.');
710 if(!domain)
711 *domain_start = 0;
712 else
713 *domain_start = (domain+1) - host;
715 } else {
716 /* The second to last TLD has more than 3 characters making it
717 * the domain name.
718 * Ex: www.google.test.us -> test.us as the domain name.
720 *domain_start = (sec_last_tld+1)-host;
723 TRACE("Found domain name %s\n", debugstr_wn(host+*domain_start,
724 (host+host_len)-(host+*domain_start)));
727 /* Removes the dot segments from a hierarchical URIs path component. This
728 * function performs the removal in place.
730 * This is a modified version of Qt's QUrl function "removeDotsFromPath".
732 * This function returns the new length of the path string.
734 static DWORD remove_dot_segments(WCHAR *path, DWORD path_len) {
735 WCHAR *out = path;
736 const WCHAR *in = out;
737 const WCHAR *end = out + path_len;
738 DWORD len;
740 while(in < end) {
741 /* A. if the input buffer begins with a prefix of "/./" or "/.",
742 * where "." is a complete path segment, then replace that
743 * prefix with "/" in the input buffer; otherwise,
745 if(in <= end - 3 && in[0] == '/' && in[1] == '.' && in[2] == '/') {
746 in += 2;
747 continue;
748 } else if(in == end - 2 && in[0] == '/' && in[1] == '.') {
749 *out++ = '/';
750 in += 2;
751 break;
754 /* B. if the input buffer begins with a prefix of "/../" or "/..",
755 * where ".." is a complete path segment, then replace that
756 * prefix with "/" in the input buffer and remove the last
757 * segment and its preceding "/" (if any) from the output
758 * buffer; otherwise,
760 if(in <= end - 4 && in[0] == '/' && in[1] == '.' && in[2] == '.' && in[3] == '/') {
761 while(out > path && *(--out) != '/');
763 in += 3;
764 continue;
765 } else if(in == end - 3 && in[0] == '/' && in[1] == '.' && in[2] == '.') {
766 while(out > path && *(--out) != '/');
768 if(*out == '/')
769 ++out;
771 in += 3;
772 break;
775 /* C. move the first path segment in the input buffer to the end of
776 * the output buffer, including the initial "/" character (if
777 * any) and any subsequent characters up to, but not including,
778 * the next "/" character or the end of the input buffer.
780 *out++ = *in++;
781 while(in < end && *in != '/')
782 *out++ = *in++;
785 len = out - path;
786 TRACE("(%p %d): Path after dot segments removed %s len=%d\n", path, path_len,
787 debugstr_wn(path, len), len);
788 return len;
791 /* Attempts to find the file extension in a given path. */
792 static INT find_file_extension(const WCHAR *path, DWORD path_len) {
793 const WCHAR *end;
795 for(end = path+path_len-1; end >= path && *end != '/' && *end != '\\'; --end) {
796 if(*end == '.')
797 return end-path;
800 return -1;
803 /* Computes the location where the elision should occur in the IPv6
804 * address using the numerical values of each component stored in
805 * 'values'. If the address shouldn't contain an elision then 'index'
806 * is assigned -1 as it's value. Otherwise 'index' will contain the
807 * starting index (into values) where the elision should be, and 'count'
808 * will contain the number of cells the elision covers.
810 * NOTES:
811 * Windows will expand an elision if the elision only represents 1 h16
812 * component of the address.
814 * Ex: [1::2:3:4:5:6:7] -> [1:0:2:3:4:5:6:7]
816 * If the IPv6 address contains an IPv4 address, the IPv4 address is also
817 * considered for being included as part of an elision if all its components
818 * are zeros.
820 * Ex: [1:2:3:4:5:6:0.0.0.0] -> [1:2:3:4:5:6::]
822 static void compute_elision_location(const ipv6_address *address, const USHORT values[8],
823 INT *index, DWORD *count) {
824 DWORD i, max_len, cur_len;
825 INT max_index, cur_index;
827 max_len = cur_len = 0;
828 max_index = cur_index = -1;
829 for(i = 0; i < 8; ++i) {
830 BOOL check_ipv4 = (address->ipv4 && i == 6);
831 BOOL is_end = (check_ipv4 || i == 7);
833 if(check_ipv4) {
834 /* Check if the IPv4 address contains only zeros. */
835 if(values[i] == 0 && values[i+1] == 0) {
836 if(cur_index == -1)
837 cur_index = i;
839 cur_len += 2;
840 ++i;
842 } else if(values[i] == 0) {
843 if(cur_index == -1)
844 cur_index = i;
846 ++cur_len;
849 if(is_end || values[i] != 0) {
850 /* We only consider it for an elision if it's
851 * more than 1 component long.
853 if(cur_len > 1 && cur_len > max_len) {
854 /* Found the new elision location. */
855 max_len = cur_len;
856 max_index = cur_index;
859 /* Reset the current range for the next range of zeros. */
860 cur_index = -1;
861 cur_len = 0;
865 *index = max_index;
866 *count = max_len;
869 /* Removes all the leading and trailing white spaces or
870 * control characters from the URI and removes all control
871 * characters inside of the URI string.
873 static BSTR pre_process_uri(LPCWSTR uri) {
874 BSTR ret;
875 DWORD len;
876 const WCHAR *start, *end;
877 WCHAR *buf, *ptr;
879 len = lstrlenW(uri);
881 start = uri;
882 /* Skip leading controls and whitespace. */
883 while(iscntrlW(*start) || isspaceW(*start)) ++start;
885 end = uri+len-1;
886 if(start == end)
887 /* URI consisted only of control/whitespace. */
888 ret = SysAllocStringLen(NULL, 0);
889 else {
890 while(iscntrlW(*end) || isspaceW(*end)) --end;
892 buf = heap_alloc(((end+1)-start)*sizeof(WCHAR));
893 if(!buf)
894 return NULL;
896 for(ptr = buf; start < end+1; ++start) {
897 if(!iscntrlW(*start))
898 *ptr++ = *start;
901 ret = SysAllocStringLen(buf, ptr-buf);
902 heap_free(buf);
905 return ret;
908 /* Converts the specified IPv4 address into an uint value.
910 * This function assumes that the IPv4 address has already been validated.
912 static UINT ipv4toui(const WCHAR *ip, DWORD len) {
913 UINT ret = 0;
914 DWORD comp_value = 0;
915 const WCHAR *ptr;
917 for(ptr = ip; ptr < ip+len; ++ptr) {
918 if(*ptr == '.') {
919 ret <<= 8;
920 ret += comp_value;
921 comp_value = 0;
922 } else
923 comp_value = comp_value*10 + (*ptr-'0');
926 ret <<= 8;
927 ret += comp_value;
929 return ret;
932 /* Converts an IPv4 address in numerical form into it's fully qualified
933 * string form. This function returns the number of characters written
934 * to 'dest'. If 'dest' is NULL this function will return the number of
935 * characters that would have been written.
937 * It's up to the caller to ensure there's enough space in 'dest' for the
938 * address.
940 static DWORD ui2ipv4(WCHAR *dest, UINT address) {
941 static const WCHAR formatW[] =
942 {'%','u','.','%','u','.','%','u','.','%','u',0};
943 DWORD ret = 0;
944 UCHAR digits[4];
946 digits[0] = (address >> 24) & 0xff;
947 digits[1] = (address >> 16) & 0xff;
948 digits[2] = (address >> 8) & 0xff;
949 digits[3] = address & 0xff;
951 if(!dest) {
952 WCHAR tmp[16];
953 ret = sprintfW(tmp, formatW, digits[0], digits[1], digits[2], digits[3]);
954 } else
955 ret = sprintfW(dest, formatW, digits[0], digits[1], digits[2], digits[3]);
957 return ret;
960 static DWORD ui2str(WCHAR *dest, UINT value) {
961 static const WCHAR formatW[] = {'%','u',0};
962 DWORD ret = 0;
964 if(!dest) {
965 WCHAR tmp[11];
966 ret = sprintfW(tmp, formatW, value);
967 } else
968 ret = sprintfW(dest, formatW, value);
970 return ret;
973 /* Converts an h16 component (from an IPv6 address) into it's
974 * numerical value.
976 * This function assumes that the h16 component has already been validated.
978 static USHORT h16tous(h16 component) {
979 DWORD i;
980 USHORT ret = 0;
982 for(i = 0; i < component.len; ++i) {
983 ret <<= 4;
984 ret += hex_to_int(component.str[i]);
987 return ret;
990 /* Converts an IPv6 address into its 128 bits (16 bytes) numerical value.
992 * This function assumes that the ipv6_address has already been validated.
994 static BOOL ipv6_to_number(const ipv6_address *address, USHORT number[8]) {
995 DWORD i, cur_component = 0;
996 BOOL already_passed_elision = FALSE;
998 for(i = 0; i < address->h16_count; ++i) {
999 if(address->elision) {
1000 if(address->components[i].str > address->elision && !already_passed_elision) {
1001 /* Means we just passed the elision and need to add its values to
1002 * 'number' before we do anything else.
1004 DWORD j = 0;
1005 for(j = 0; j < address->elision_size; j+=2)
1006 number[cur_component++] = 0;
1008 already_passed_elision = TRUE;
1012 number[cur_component++] = h16tous(address->components[i]);
1015 /* Case when the elision appears after the h16 components. */
1016 if(!already_passed_elision && address->elision) {
1017 for(i = 0; i < address->elision_size; i+=2)
1018 number[cur_component++] = 0;
1019 already_passed_elision = TRUE;
1022 if(address->ipv4) {
1023 UINT value = ipv4toui(address->ipv4, address->ipv4_len);
1025 if(cur_component != 6) {
1026 ERR("(%p %p): Failed sanity check with %d\n", address, number, cur_component);
1027 return FALSE;
1030 number[cur_component++] = (value >> 16) & 0xffff;
1031 number[cur_component] = value & 0xffff;
1034 return TRUE;
1037 /* Checks if the characters pointed to by 'ptr' are
1038 * a percent encoded data octet.
1040 * pct-encoded = "%" HEXDIG HEXDIG
1042 static BOOL check_pct_encoded(const WCHAR **ptr) {
1043 const WCHAR *start = *ptr;
1045 if(**ptr != '%')
1046 return FALSE;
1048 ++(*ptr);
1049 if(!is_hexdigit(**ptr)) {
1050 *ptr = start;
1051 return FALSE;
1054 ++(*ptr);
1055 if(!is_hexdigit(**ptr)) {
1056 *ptr = start;
1057 return FALSE;
1060 ++(*ptr);
1061 return TRUE;
1064 /* dec-octet = DIGIT ; 0-9
1065 * / %x31-39 DIGIT ; 10-99
1066 * / "1" 2DIGIT ; 100-199
1067 * / "2" %x30-34 DIGIT ; 200-249
1068 * / "25" %x30-35 ; 250-255
1070 static BOOL check_dec_octet(const WCHAR **ptr) {
1071 const WCHAR *c1, *c2, *c3;
1073 c1 = *ptr;
1074 /* A dec-octet must be at least 1 digit long. */
1075 if(*c1 < '0' || *c1 > '9')
1076 return FALSE;
1078 ++(*ptr);
1080 c2 = *ptr;
1081 /* Since the 1 digit requirment was meet, it doesn't
1082 * matter if this is a DIGIT value, it's considered a
1083 * dec-octet.
1085 if(*c2 < '0' || *c2 > '9')
1086 return TRUE;
1088 ++(*ptr);
1090 c3 = *ptr;
1091 /* Same explanation as above. */
1092 if(*c3 < '0' || *c3 > '9')
1093 return TRUE;
1095 /* Anything > 255 isn't a valid IP dec-octet. */
1096 if(*c1 >= '2' && *c2 >= '5' && *c3 >= '5') {
1097 *ptr = c1;
1098 return FALSE;
1101 ++(*ptr);
1102 return TRUE;
1105 /* Checks if there is an implicit IPv4 address in the host component of the URI.
1106 * The max value of an implicit IPv4 address is UINT_MAX.
1108 * Ex:
1109 * "234567" would be considered an implicit IPv4 address.
1111 static BOOL check_implicit_ipv4(const WCHAR **ptr, UINT *val) {
1112 const WCHAR *start = *ptr;
1113 ULONGLONG ret = 0;
1114 *val = 0;
1116 while(is_num(**ptr)) {
1117 ret = ret*10 + (**ptr - '0');
1119 if(ret > UINT_MAX) {
1120 *ptr = start;
1121 return FALSE;
1123 ++(*ptr);
1126 if(*ptr == start)
1127 return FALSE;
1129 *val = ret;
1130 return TRUE;
1133 /* Checks if the string contains an IPv4 address.
1135 * This function has a strict mode or a non-strict mode of operation
1136 * When 'strict' is set to FALSE this function will return TRUE if
1137 * the string contains at least 'dec-octet "." dec-octet' since partial
1138 * IPv4 addresses will be normalized out into full IPv4 addresses. When
1139 * 'strict' is set this function expects there to be a full IPv4 address.
1141 * IPv4address = dec-octet "." dec-octet "." dec-octet "." dec-octet
1143 static BOOL check_ipv4address(const WCHAR **ptr, BOOL strict) {
1144 const WCHAR *start = *ptr;
1146 if(!check_dec_octet(ptr)) {
1147 *ptr = start;
1148 return FALSE;
1151 if(**ptr != '.') {
1152 *ptr = start;
1153 return FALSE;
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 if(**ptr != '.') {
1177 if(strict) {
1178 *ptr = start;
1179 return FALSE;
1180 } else
1181 return TRUE;
1184 ++(*ptr);
1185 if(!check_dec_octet(ptr)) {
1186 *ptr = start;
1187 return FALSE;
1190 /* Found a four digit ip address. */
1191 return TRUE;
1193 /* Tries to parse the scheme name of the URI.
1195 * scheme = ALPHA *(ALPHA | NUM | '+' | '-' | '.') as defined by RFC 3896.
1196 * NOTE: Windows accepts a number as the first character of a scheme.
1198 static BOOL parse_scheme_name(const WCHAR **ptr, parse_data *data, DWORD extras) {
1199 const WCHAR *start = *ptr;
1201 data->scheme = NULL;
1202 data->scheme_len = 0;
1204 while(**ptr) {
1205 if(**ptr == '*' && *ptr == start) {
1206 /* Might have found a wildcard scheme. If it is the next
1207 * char has to be a ':' for it to be a valid URI
1209 ++(*ptr);
1210 break;
1211 } else if(!is_num(**ptr) && !is_alpha(**ptr) && **ptr != '+' &&
1212 **ptr != '-' && **ptr != '.')
1213 break;
1215 (*ptr)++;
1218 if(*ptr == start)
1219 return FALSE;
1221 /* Schemes must end with a ':' */
1222 if(**ptr != ':' && !((extras & ALLOW_NULL_TERM_SCHEME) && !**ptr)) {
1223 *ptr = start;
1224 return FALSE;
1227 data->scheme = start;
1228 data->scheme_len = *ptr - start;
1230 ++(*ptr);
1231 return TRUE;
1234 /* Tries to deduce the corresponding URL_SCHEME for the given URI. Stores
1235 * the deduced URL_SCHEME in data->scheme_type.
1237 static BOOL parse_scheme_type(parse_data *data) {
1238 /* If there's scheme data then see if it's a recognized scheme. */
1239 if(data->scheme && data->scheme_len) {
1240 DWORD i;
1242 for(i = 0; i < sizeof(recognized_schemes)/sizeof(recognized_schemes[0]); ++i) {
1243 if(lstrlenW(recognized_schemes[i].scheme_name) == data->scheme_len) {
1244 /* Has to be a case insensitive compare. */
1245 if(!StrCmpNIW(recognized_schemes[i].scheme_name, data->scheme, data->scheme_len)) {
1246 data->scheme_type = recognized_schemes[i].scheme;
1247 return TRUE;
1252 /* If we get here it means it's not a recognized scheme. */
1253 data->scheme_type = URL_SCHEME_UNKNOWN;
1254 return TRUE;
1255 } else if(data->is_relative) {
1256 /* Relative URI's have no scheme. */
1257 data->scheme_type = URL_SCHEME_UNKNOWN;
1258 return TRUE;
1259 } else {
1260 /* Should never reach here! what happened... */
1261 FIXME("(%p): Unable to determine scheme type for URI %s\n", data, debugstr_w(data->uri));
1262 return FALSE;
1266 /* Tries to parse (or deduce) the scheme_name of a URI. If it can't
1267 * parse a scheme from the URI it will try to deduce the scheme_name and scheme_type
1268 * using the flags specified in 'flags' (if any). Flags that affect how this function
1269 * operates are the Uri_CREATE_ALLOW_* flags.
1271 * All parsed/deduced information will be stored in 'data' when the function returns.
1273 * Returns TRUE if it was able to successfully parse the information.
1275 static BOOL parse_scheme(const WCHAR **ptr, parse_data *data, DWORD flags, DWORD extras) {
1276 static const WCHAR fileW[] = {'f','i','l','e',0};
1277 static const WCHAR wildcardW[] = {'*',0};
1279 /* First check to see if the uri could implicitly be a file path. */
1280 if(is_implicit_file_path(*ptr)) {
1281 if(flags & Uri_CREATE_ALLOW_IMPLICIT_FILE_SCHEME) {
1282 data->scheme = fileW;
1283 data->scheme_len = lstrlenW(fileW);
1284 data->has_implicit_scheme = TRUE;
1286 TRACE("(%p %p %x): URI is an implicit file path.\n", ptr, data, flags);
1287 } else {
1288 /* Window's does not consider anything that can implicitly be a file
1289 * path to be a valid URI if the ALLOW_IMPLICIT_FILE_SCHEME flag is not set...
1291 TRACE("(%p %p %x): URI is implicitly a file path, but, the ALLOW_IMPLICIT_FILE_SCHEME flag wasn't set.\n",
1292 ptr, data, flags);
1293 return FALSE;
1295 } else if(!parse_scheme_name(ptr, data, extras)) {
1296 /* No Scheme was found, this means it could be:
1297 * a) an implicit Wildcard scheme
1298 * b) a relative URI
1299 * c) a invalid URI.
1301 if(flags & Uri_CREATE_ALLOW_IMPLICIT_WILDCARD_SCHEME) {
1302 data->scheme = wildcardW;
1303 data->scheme_len = lstrlenW(wildcardW);
1304 data->has_implicit_scheme = TRUE;
1306 TRACE("(%p %p %x): URI is an implicit wildcard scheme.\n", ptr, data, flags);
1307 } else if (flags & Uri_CREATE_ALLOW_RELATIVE) {
1308 data->is_relative = TRUE;
1309 TRACE("(%p %p %x): URI is relative.\n", ptr, data, flags);
1310 } else {
1311 TRACE("(%p %p %x): Malformed URI found. Unable to deduce scheme name.\n", ptr, data, flags);
1312 return FALSE;
1316 if(!data->is_relative)
1317 TRACE("(%p %p %x): Found scheme=%s scheme_len=%d\n", ptr, data, flags,
1318 debugstr_wn(data->scheme, data->scheme_len), data->scheme_len);
1320 if(!parse_scheme_type(data))
1321 return FALSE;
1323 TRACE("(%p %p %x): Assigned %d as the URL_SCHEME.\n", ptr, data, flags, data->scheme_type);
1324 return TRUE;
1327 static BOOL parse_username(const WCHAR **ptr, parse_data *data, DWORD flags, DWORD extras) {
1328 data->username = *ptr;
1330 while(**ptr != ':' && **ptr != '@') {
1331 if(**ptr == '%') {
1332 if(!check_pct_encoded(ptr)) {
1333 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
1334 *ptr = data->username;
1335 data->username = NULL;
1336 return FALSE;
1338 } else
1339 continue;
1340 } else if(extras & ALLOW_NULL_TERM_USER_NAME && !**ptr)
1341 break;
1342 else if(is_auth_delim(**ptr, data->scheme_type != URL_SCHEME_UNKNOWN)) {
1343 *ptr = data->username;
1344 data->username = NULL;
1345 return FALSE;
1348 ++(*ptr);
1351 data->username_len = *ptr - data->username;
1352 return TRUE;
1355 static BOOL parse_password(const WCHAR **ptr, parse_data *data, DWORD flags, DWORD extras) {
1356 data->password = *ptr;
1358 while(**ptr != '@') {
1359 if(**ptr == '%') {
1360 if(!check_pct_encoded(ptr)) {
1361 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
1362 *ptr = data->password;
1363 data->password = NULL;
1364 return FALSE;
1366 } else
1367 continue;
1368 } else if(extras & ALLOW_NULL_TERM_PASSWORD && !**ptr)
1369 break;
1370 else if(is_auth_delim(**ptr, data->scheme_type != URL_SCHEME_UNKNOWN)) {
1371 *ptr = data->password;
1372 data->password = NULL;
1373 return FALSE;
1376 ++(*ptr);
1379 data->password_len = *ptr - data->password;
1380 return TRUE;
1383 /* Parses the userinfo part of the URI (if it exists). The userinfo field of
1384 * a URI can consist of "username:password@", or just "username@".
1386 * RFC def:
1387 * userinfo = *( unreserved / pct-encoded / sub-delims / ":" )
1389 * NOTES:
1390 * 1) If there is more than one ':' in the userinfo part of the URI Windows
1391 * uses the first occurrence of ':' to delimit the username and password
1392 * components.
1394 * ex:
1395 * ftp://user:pass:word@winehq.org
1397 * Would yield, "user" as the username and "pass:word" as the password.
1399 * 2) Windows allows any character to appear in the "userinfo" part of
1400 * a URI, as long as it's not an authority delimeter character set.
1402 static void parse_userinfo(const WCHAR **ptr, parse_data *data, DWORD flags) {
1403 const WCHAR *start = *ptr;
1405 if(!parse_username(ptr, data, flags, 0)) {
1406 TRACE("(%p %p %x): URI contained no userinfo.\n", ptr, data, flags);
1407 return;
1410 if(**ptr == ':') {
1411 ++(*ptr);
1412 if(!parse_password(ptr, data, flags, 0)) {
1413 *ptr = start;
1414 data->username = NULL;
1415 data->username_len = 0;
1416 TRACE("(%p %p %x): URI contained no userinfo.\n", ptr, data, flags);
1417 return;
1421 if(**ptr != '@') {
1422 *ptr = start;
1423 data->username = NULL;
1424 data->username_len = 0;
1425 data->password = NULL;
1426 data->password_len = 0;
1428 TRACE("(%p %p %x): URI contained no userinfo.\n", ptr, data, flags);
1429 return;
1432 if(data->username)
1433 TRACE("(%p %p %x): Found username %s len=%d.\n", ptr, data, flags,
1434 debugstr_wn(data->username, data->username_len), data->username_len);
1436 if(data->password)
1437 TRACE("(%p %p %x): Found password %s len=%d.\n", ptr, data, flags,
1438 debugstr_wn(data->password, data->password_len), data->password_len);
1440 ++(*ptr);
1443 /* Attempts to parse a port from the URI.
1445 * NOTES:
1446 * Windows seems to have a cap on what the maximum value
1447 * for a port can be. The max value is USHORT_MAX.
1449 * port = *DIGIT
1451 static BOOL parse_port(const WCHAR **ptr, parse_data *data, DWORD flags) {
1452 UINT port = 0;
1453 data->port = *ptr;
1455 while(!is_auth_delim(**ptr, data->scheme_type != URL_SCHEME_UNKNOWN)) {
1456 if(!is_num(**ptr)) {
1457 *ptr = data->port;
1458 data->port = NULL;
1459 return FALSE;
1462 port = port*10 + (**ptr-'0');
1464 if(port > USHORT_MAX) {
1465 *ptr = data->port;
1466 data->port = NULL;
1467 return FALSE;
1470 ++(*ptr);
1473 data->has_port = TRUE;
1474 data->port_value = port;
1475 data->port_len = *ptr - data->port;
1477 TRACE("(%p %p %x): Found port %s len=%d value=%u\n", ptr, data, flags,
1478 debugstr_wn(data->port, data->port_len), data->port_len, data->port_value);
1479 return TRUE;
1482 /* Attempts to parse a IPv4 address from the URI.
1484 * NOTES:
1485 * Window's normalizes IPv4 addresses, This means there's three
1486 * possibilities for the URI to contain an IPv4 address.
1487 * 1) A well formed address (ex. 192.2.2.2).
1488 * 2) A partially formed address. For example "192.0" would
1489 * normalize to "192.0.0.0" during canonicalization.
1490 * 3) An implicit IPv4 address. For example "256" would
1491 * normalize to "0.0.1.0" during canonicalization. Also
1492 * note that the maximum value for an implicit IP address
1493 * is UINT_MAX, if the value in the URI exceeds this then
1494 * it is not considered an IPv4 address.
1496 static BOOL parse_ipv4address(const WCHAR **ptr, parse_data *data, DWORD flags) {
1497 const BOOL is_unknown = data->scheme_type == URL_SCHEME_UNKNOWN;
1498 data->host = *ptr;
1500 if(!check_ipv4address(ptr, FALSE)) {
1501 if(!check_implicit_ipv4(ptr, &data->implicit_ipv4)) {
1502 TRACE("(%p %p %x): URI didn't contain anything looking like an IPv4 address.\n",
1503 ptr, data, flags);
1504 *ptr = data->host;
1505 data->host = NULL;
1506 return FALSE;
1507 } else
1508 data->has_implicit_ip = TRUE;
1511 /* Check if what we found is the only part of the host name (if it isn't
1512 * we don't have an IPv4 address).
1514 if(**ptr == ':') {
1515 ++(*ptr);
1516 if(!parse_port(ptr, data, flags)) {
1517 *ptr = data->host;
1518 data->host = NULL;
1519 return FALSE;
1521 } else if(!is_auth_delim(**ptr, !is_unknown)) {
1522 /* Found more data which belongs the host, so this isn't an IPv4. */
1523 *ptr = data->host;
1524 data->host = NULL;
1525 data->has_implicit_ip = FALSE;
1526 return FALSE;
1529 data->host_len = *ptr - data->host;
1530 data->host_type = Uri_HOST_IPV4;
1532 TRACE("(%p %p %x): IPv4 address found. host=%s host_len=%d host_type=%d\n",
1533 ptr, data, flags, debugstr_wn(data->host, data->host_len),
1534 data->host_len, data->host_type);
1535 return TRUE;
1538 /* Attempts to parse the reg-name from the URI.
1540 * Because of the way Windows handles ':' this function also
1541 * handles parsing the port.
1543 * reg-name = *( unreserved / pct-encoded / sub-delims )
1545 * NOTE:
1546 * Windows allows everything, but, the characters in "auth_delims" and ':'
1547 * to appear in a reg-name, unless it's an unknown scheme type then ':' is
1548 * allowed to appear (even if a valid port isn't after it).
1550 * Windows doesn't like host names which start with '[' and end with ']'
1551 * and don't contain a valid IP literal address in between them.
1553 * On Windows if an '[' is encountered in the host name the ':' no longer
1554 * counts as a delimiter until you reach the next ']' or an "authority delimeter".
1556 * A reg-name CAN be empty.
1558 static BOOL parse_reg_name(const WCHAR **ptr, parse_data *data, DWORD flags, DWORD extras) {
1559 const BOOL has_start_bracket = **ptr == '[';
1560 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
1561 const BOOL is_res = data->scheme_type == URL_SCHEME_RES;
1562 BOOL inside_brackets = has_start_bracket;
1564 /* res URIs don't have ports. */
1565 BOOL ignore_col = (extras & IGNORE_PORT_DELIMITER) || is_res;
1567 /* We have to be careful with file schemes. */
1568 if(data->scheme_type == URL_SCHEME_FILE) {
1569 /* This is because an implicit file scheme could be "C:\\test" and it
1570 * would trick this function into thinking the host is "C", when after
1571 * canonicalization the host would end up being an empty string. A drive
1572 * path can also have a '|' instead of a ':' after the drive letter.
1574 if(is_drive_path(*ptr)) {
1575 /* Regular old drive paths don't have a host type (or host name). */
1576 data->host_type = Uri_HOST_UNKNOWN;
1577 data->host = *ptr;
1578 data->host_len = 0;
1579 return TRUE;
1580 } else if(is_unc_path(*ptr))
1581 /* Skip past the "\\" of a UNC path. */
1582 *ptr += 2;
1585 data->host = *ptr;
1587 /* For res URIs, everything before the first '/' is
1588 * considered the host.
1590 while((!is_res && !is_auth_delim(**ptr, known_scheme)) ||
1591 (is_res && **ptr && **ptr != '/')) {
1592 if(**ptr == ':' && !ignore_col) {
1593 /* We can ignore ':' if were inside brackets.*/
1594 if(!inside_brackets) {
1595 const WCHAR *tmp = (*ptr)++;
1597 /* Attempt to parse the port. */
1598 if(!parse_port(ptr, data, flags)) {
1599 /* Windows expects there to be a valid port for known scheme types. */
1600 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
1601 *ptr = data->host;
1602 data->host = NULL;
1603 TRACE("(%p %p %x %x): Expected valid port\n", ptr, data, flags, extras);
1604 return FALSE;
1605 } else
1606 /* Windows gives up on trying to parse a port when it
1607 * encounters 1 invalid port.
1609 ignore_col = TRUE;
1610 } else {
1611 data->host_len = tmp - data->host;
1612 break;
1615 } else if(**ptr == '%' && (known_scheme && !is_res)) {
1616 /* Has to be a legit % encoded value. */
1617 if(!check_pct_encoded(ptr)) {
1618 *ptr = data->host;
1619 data->host = NULL;
1620 return FALSE;
1621 } else
1622 continue;
1623 } else if(is_res && is_forbidden_dos_path_char(**ptr)) {
1624 *ptr = data->host;
1625 data->host = NULL;
1626 return FALSE;
1627 } else if(**ptr == ']')
1628 inside_brackets = FALSE;
1629 else if(**ptr == '[')
1630 inside_brackets = TRUE;
1632 ++(*ptr);
1635 if(has_start_bracket) {
1636 /* Make sure the last character of the host wasn't a ']'. */
1637 if(*(*ptr-1) == ']') {
1638 TRACE("(%p %p %x %x): Expected an IP literal inside of the host\n",
1639 ptr, data, flags, extras);
1640 *ptr = data->host;
1641 data->host = NULL;
1642 return FALSE;
1646 /* Don't overwrite our length if we found a port earlier. */
1647 if(!data->port)
1648 data->host_len = *ptr - data->host;
1650 /* If the host is empty, then it's an unknown host type. */
1651 if(data->host_len == 0 || is_res)
1652 data->host_type = Uri_HOST_UNKNOWN;
1653 else
1654 data->host_type = Uri_HOST_DNS;
1656 TRACE("(%p %p %x %x): Parsed reg-name. host=%s len=%d\n", ptr, data, flags, extras,
1657 debugstr_wn(data->host, data->host_len), data->host_len);
1658 return TRUE;
1661 /* Attempts to parse an IPv6 address out of the URI.
1663 * IPv6address = 6( h16 ":" ) ls32
1664 * / "::" 5( h16 ":" ) ls32
1665 * / [ h16 ] "::" 4( h16 ":" ) ls32
1666 * / [ *1( h16 ":" ) h16 ] "::" 3( h16 ":" ) ls32
1667 * / [ *2( h16 ":" ) h16 ] "::" 2( h16 ":" ) ls32
1668 * / [ *3( h16 ":" ) h16 ] "::" h16 ":" ls32
1669 * / [ *4( h16 ":" ) h16 ] "::" ls32
1670 * / [ *5( h16 ":" ) h16 ] "::" h16
1671 * / [ *6( h16 ":" ) h16 ] "::"
1673 * ls32 = ( h16 ":" h16 ) / IPv4address
1674 * ; least-significant 32 bits of address.
1676 * h16 = 1*4HEXDIG
1677 * ; 16 bits of address represented in hexadecimal.
1679 * Modeled after google-url's 'DoParseIPv6' function.
1681 static BOOL parse_ipv6address(const WCHAR **ptr, parse_data *data, DWORD flags) {
1682 const WCHAR *start, *cur_start;
1683 ipv6_address ip;
1685 start = cur_start = *ptr;
1686 memset(&ip, 0, sizeof(ipv6_address));
1688 for(;; ++(*ptr)) {
1689 /* Check if we're on the last character of the host. */
1690 BOOL is_end = (is_auth_delim(**ptr, data->scheme_type != URL_SCHEME_UNKNOWN)
1691 || **ptr == ']');
1693 BOOL is_split = (**ptr == ':');
1694 BOOL is_elision = (is_split && !is_end && *(*ptr+1) == ':');
1696 /* Check if we're at the end of a component, or
1697 * if we're at the end of the IPv6 address.
1699 if(is_split || is_end) {
1700 DWORD cur_len = 0;
1702 cur_len = *ptr - cur_start;
1704 /* h16 can't have a length > 4. */
1705 if(cur_len > 4) {
1706 *ptr = start;
1708 TRACE("(%p %p %x): h16 component to long.\n",
1709 ptr, data, flags);
1710 return FALSE;
1713 if(cur_len == 0) {
1714 /* An h16 component can't have the length of 0 unless
1715 * the elision is at the beginning of the address, or
1716 * at the end of the address.
1718 if(!((*ptr == start && is_elision) ||
1719 (is_end && (*ptr-2) == ip.elision))) {
1720 *ptr = start;
1721 TRACE("(%p %p %x): IPv6 component cannot have a length of 0.\n",
1722 ptr, data, flags);
1723 return FALSE;
1727 if(cur_len > 0) {
1728 /* An IPv6 address can have no more than 8 h16 components. */
1729 if(ip.h16_count >= 8) {
1730 *ptr = start;
1731 TRACE("(%p %p %x): Not a IPv6 address, to many h16 components.\n",
1732 ptr, data, flags);
1733 return FALSE;
1736 ip.components[ip.h16_count].str = cur_start;
1737 ip.components[ip.h16_count].len = cur_len;
1739 TRACE("(%p %p %x): Found h16 component %s, len=%d, h16_count=%d\n",
1740 ptr, data, flags, debugstr_wn(cur_start, cur_len), cur_len,
1741 ip.h16_count);
1742 ++ip.h16_count;
1746 if(is_end)
1747 break;
1749 if(is_elision) {
1750 /* A IPv6 address can only have 1 elision ('::'). */
1751 if(ip.elision) {
1752 *ptr = start;
1754 TRACE("(%p %p %x): IPv6 address cannot have 2 elisions.\n",
1755 ptr, data, flags);
1756 return FALSE;
1759 ip.elision = *ptr;
1760 ++(*ptr);
1763 if(is_split)
1764 cur_start = *ptr+1;
1765 else {
1766 if(!check_ipv4address(ptr, TRUE)) {
1767 if(!is_hexdigit(**ptr)) {
1768 /* Not a valid character for an IPv6 address. */
1769 *ptr = start;
1770 return FALSE;
1772 } else {
1773 /* Found an IPv4 address. */
1774 ip.ipv4 = cur_start;
1775 ip.ipv4_len = *ptr - cur_start;
1777 TRACE("(%p %p %x): Found an attached IPv4 address %s len=%d.\n",
1778 ptr, data, flags, debugstr_wn(ip.ipv4, ip.ipv4_len),
1779 ip.ipv4_len);
1781 /* IPv4 addresses can only appear at the end of a IPv6. */
1782 break;
1787 compute_ipv6_comps_size(&ip);
1789 /* Make sure the IPv6 address adds up to 16 bytes. */
1790 if(ip.components_size + ip.elision_size != 16) {
1791 *ptr = start;
1792 TRACE("(%p %p %x): Invalid IPv6 address, did not add up to 16 bytes.\n",
1793 ptr, data, flags);
1794 return FALSE;
1797 if(ip.elision_size == 2) {
1798 /* For some reason on Windows if an elision that represents
1799 * only 1 h16 component is encountered at the very begin or
1800 * end of an IPv6 address, Windows does not consider it a
1801 * valid IPv6 address.
1803 * Ex: [::2:3:4:5:6:7] is not valid, even though the sum
1804 * of all the components == 128bits.
1806 if(ip.elision < ip.components[0].str ||
1807 ip.elision > ip.components[ip.h16_count-1].str) {
1808 *ptr = start;
1809 TRACE("(%p %p %x): Invalid IPv6 address. Detected elision of 2 bytes at the beginning or end of the address.\n",
1810 ptr, data, flags);
1811 return FALSE;
1815 data->host_type = Uri_HOST_IPV6;
1816 data->has_ipv6 = TRUE;
1817 data->ipv6_address = ip;
1819 TRACE("(%p %p %x): Found valid IPv6 literal %s len=%d\n",
1820 ptr, data, flags, debugstr_wn(start, *ptr-start),
1821 (int)(*ptr-start));
1822 return TRUE;
1825 /* IPvFuture = "v" 1*HEXDIG "." 1*( unreserved / sub-delims / ":" ) */
1826 static BOOL parse_ipvfuture(const WCHAR **ptr, parse_data *data, DWORD flags) {
1827 const WCHAR *start = *ptr;
1829 /* IPvFuture has to start with a 'v' or 'V'. */
1830 if(**ptr != 'v' && **ptr != 'V')
1831 return FALSE;
1833 /* Following the v there must be at least 1 hex digit. */
1834 ++(*ptr);
1835 if(!is_hexdigit(**ptr)) {
1836 *ptr = start;
1837 return FALSE;
1840 ++(*ptr);
1841 while(is_hexdigit(**ptr))
1842 ++(*ptr);
1844 /* End of the hexdigit sequence must be a '.' */
1845 if(**ptr != '.') {
1846 *ptr = start;
1847 return FALSE;
1850 ++(*ptr);
1851 if(!is_unreserved(**ptr) && !is_subdelim(**ptr) && **ptr != ':') {
1852 *ptr = start;
1853 return FALSE;
1856 ++(*ptr);
1857 while(is_unreserved(**ptr) || is_subdelim(**ptr) || **ptr == ':')
1858 ++(*ptr);
1860 data->host_type = Uri_HOST_UNKNOWN;
1862 TRACE("(%p %p %x): Parsed IPvFuture address %s len=%d\n", ptr, data, flags,
1863 debugstr_wn(start, *ptr-start), (int)(*ptr-start));
1865 return TRUE;
1868 /* IP-literal = "[" ( IPv6address / IPvFuture ) "]" */
1869 static BOOL parse_ip_literal(const WCHAR **ptr, parse_data *data, DWORD flags, DWORD extras) {
1870 data->host = *ptr;
1872 if(**ptr != '[' && !(extras & ALLOW_BRACKETLESS_IP_LITERAL)) {
1873 data->host = NULL;
1874 return FALSE;
1875 } else if(**ptr == '[')
1876 ++(*ptr);
1878 if(!parse_ipv6address(ptr, data, flags)) {
1879 if(extras & SKIP_IP_FUTURE_CHECK || !parse_ipvfuture(ptr, data, flags)) {
1880 *ptr = data->host;
1881 data->host = NULL;
1882 return FALSE;
1886 if(**ptr != ']' && !(extras & ALLOW_BRACKETLESS_IP_LITERAL)) {
1887 *ptr = data->host;
1888 data->host = NULL;
1889 return FALSE;
1890 } else if(!**ptr && extras & ALLOW_BRACKETLESS_IP_LITERAL) {
1891 /* The IP literal didn't contain brackets and was followed by
1892 * a NULL terminator, so no reason to even check the port.
1894 data->host_len = *ptr - data->host;
1895 return TRUE;
1898 ++(*ptr);
1899 if(**ptr == ':') {
1900 ++(*ptr);
1901 /* If a valid port is not found, then let it trickle down to
1902 * parse_reg_name.
1904 if(!parse_port(ptr, data, flags)) {
1905 *ptr = data->host;
1906 data->host = NULL;
1907 return FALSE;
1909 } else
1910 data->host_len = *ptr - data->host;
1912 return TRUE;
1915 /* Parses the host information from the URI.
1917 * host = IP-literal / IPv4address / reg-name
1919 static BOOL parse_host(const WCHAR **ptr, parse_data *data, DWORD flags, DWORD extras) {
1920 if(!parse_ip_literal(ptr, data, flags, extras)) {
1921 if(!parse_ipv4address(ptr, data, flags)) {
1922 if(!parse_reg_name(ptr, data, flags, extras)) {
1923 TRACE("(%p %p %x %x): Malformed URI, Unknown host type.\n",
1924 ptr, data, flags, extras);
1925 return FALSE;
1930 return TRUE;
1933 /* Parses the authority information from the URI.
1935 * authority = [ userinfo "@" ] host [ ":" port ]
1937 static BOOL parse_authority(const WCHAR **ptr, parse_data *data, DWORD flags) {
1938 parse_userinfo(ptr, data, flags);
1940 /* Parsing the port will happen during one of the host parsing
1941 * routines (if the URI has a port).
1943 if(!parse_host(ptr, data, flags, 0))
1944 return FALSE;
1946 return TRUE;
1949 /* Attempts to parse the path information of a hierarchical URI. */
1950 static BOOL parse_path_hierarchical(const WCHAR **ptr, parse_data *data, DWORD flags) {
1951 const WCHAR *start = *ptr;
1952 static const WCHAR slash[] = {'/',0};
1953 const BOOL is_file = data->scheme_type == URL_SCHEME_FILE;
1955 if(is_path_delim(**ptr)) {
1956 if(data->scheme_type == URL_SCHEME_WILDCARD) {
1957 /* Wildcard schemes don't get a '/' attached if their path is
1958 * empty.
1960 data->path = NULL;
1961 data->path_len = 0;
1962 } else if(!(flags & Uri_CREATE_NO_CANONICALIZE)) {
1963 /* If the path component is empty, then a '/' is added. */
1964 data->path = slash;
1965 data->path_len = 1;
1967 } else {
1968 while(!is_path_delim(**ptr)) {
1969 if(**ptr == '%' && data->scheme_type != URL_SCHEME_UNKNOWN && !is_file) {
1970 if(!check_pct_encoded(ptr)) {
1971 *ptr = start;
1972 return FALSE;
1973 } else
1974 continue;
1975 } else if(is_forbidden_dos_path_char(**ptr) && is_file &&
1976 (flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
1977 /* File schemes with USE_DOS_PATH set aren't allowed to have
1978 * a '<' or '>' or '\"' appear in them.
1980 *ptr = start;
1981 return FALSE;
1982 } else if(**ptr == '\\') {
1983 /* Not allowed to have a backslash if NO_CANONICALIZE is set
1984 * and the scheme is known type (but not a file scheme).
1986 if(flags & Uri_CREATE_NO_CANONICALIZE) {
1987 if(data->scheme_type != URL_SCHEME_FILE &&
1988 data->scheme_type != URL_SCHEME_UNKNOWN) {
1989 *ptr = start;
1990 return FALSE;
1995 ++(*ptr);
1998 /* The only time a URI doesn't have a path is when
1999 * the NO_CANONICALIZE flag is set and the raw URI
2000 * didn't contain one.
2002 if(*ptr == start) {
2003 data->path = NULL;
2004 data->path_len = 0;
2005 } else {
2006 data->path = start;
2007 data->path_len = *ptr - start;
2011 if(data->path)
2012 TRACE("(%p %p %x): Parsed path %s len=%d\n", ptr, data, flags,
2013 debugstr_wn(data->path, data->path_len), data->path_len);
2014 else
2015 TRACE("(%p %p %x): The URI contained no path\n", ptr, data, flags);
2017 return TRUE;
2020 /* Parses the path of a opaque URI (much less strict then the parser
2021 * for a hierarchical URI).
2023 * NOTE:
2024 * Windows allows invalid % encoded data to appear in opaque URI paths
2025 * for unknown scheme types.
2027 * File schemes with USE_DOS_PATH set aren't allowed to have '<', '>', or '\"'
2028 * appear in them.
2030 static BOOL parse_path_opaque(const WCHAR **ptr, parse_data *data, DWORD flags) {
2031 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
2032 const BOOL is_file = data->scheme_type == URL_SCHEME_FILE;
2034 data->path = *ptr;
2036 while(!is_path_delim(**ptr)) {
2037 if(**ptr == '%' && known_scheme) {
2038 if(!check_pct_encoded(ptr)) {
2039 *ptr = data->path;
2040 data->path = NULL;
2041 return FALSE;
2042 } else
2043 continue;
2044 } else if(is_forbidden_dos_path_char(**ptr) && is_file &&
2045 (flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
2046 *ptr = data->path;
2047 data->path = NULL;
2048 return FALSE;
2051 ++(*ptr);
2054 data->path_len = *ptr - data->path;
2055 TRACE("(%p %p %x): Parsed opaque URI path %s len=%d\n", ptr, data, flags,
2056 debugstr_wn(data->path, data->path_len), data->path_len);
2057 return TRUE;
2060 /* Determines how the URI should be parsed after the scheme information.
2062 * If the scheme is followed, by "//" then, it is treated as an hierarchical URI
2063 * which then the authority and path information will be parsed out. Otherwise, the
2064 * URI will be treated as an opaque URI which the authority information is not parsed
2065 * out.
2067 * RFC 3896 definition of hier-part:
2069 * hier-part = "//" authority path-abempty
2070 * / path-absolute
2071 * / path-rootless
2072 * / path-empty
2074 * MSDN opaque URI definition:
2075 * scheme ":" path [ "#" fragment ]
2077 * NOTES:
2078 * If the URI is of an unknown scheme type and has a "//" following the scheme then it
2079 * is treated as a hierarchical URI, but, if the CREATE_NO_CRACK_UNKNOWN_SCHEMES flag is
2080 * set then it is considered an opaque URI reguardless of what follows the scheme information
2081 * (per MSDN documentation).
2083 static BOOL parse_hierpart(const WCHAR **ptr, parse_data *data, DWORD flags) {
2084 const WCHAR *start = *ptr;
2086 /* Checks if the authority information needs to be parsed. */
2087 if(is_hierarchical_uri(ptr, data)) {
2088 /* Only treat it as a hierarchical URI if the scheme_type is known or
2089 * the Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES flag is not set.
2091 if(data->scheme_type != URL_SCHEME_UNKNOWN ||
2092 !(flags & Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES)) {
2093 TRACE("(%p %p %x): Treating URI as an hierarchical URI.\n", ptr, data, flags);
2094 data->is_opaque = FALSE;
2096 /* TODO: Handle hierarchical URI's, parse authority then parse the path. */
2097 if(!parse_authority(ptr, data, flags))
2098 return FALSE;
2100 return parse_path_hierarchical(ptr, data, flags);
2101 } else
2102 /* Reset ptr to it's starting position so opaque path parsing
2103 * begins at the correct location.
2105 *ptr = start;
2108 /* If it reaches here, then the URI will be treated as an opaque
2109 * URI.
2112 TRACE("(%p %p %x): Treating URI as an opaque URI.\n", ptr, data, flags);
2114 data->is_opaque = TRUE;
2115 if(!parse_path_opaque(ptr, data, flags))
2116 return FALSE;
2118 return TRUE;
2121 /* Attempts to parse the query string from the URI.
2123 * NOTES:
2124 * If NO_DECODE_EXTRA_INFO flag is set, then invalid percent encoded
2125 * data is allowed appear in the query string. For unknown scheme types
2126 * invalid percent encoded data is allowed to appear reguardless.
2128 static BOOL parse_query(const WCHAR **ptr, parse_data *data, DWORD flags) {
2129 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
2131 if(**ptr != '?') {
2132 TRACE("(%p %p %x): URI didn't contain a query string.\n", ptr, data, flags);
2133 return TRUE;
2136 data->query = *ptr;
2138 ++(*ptr);
2139 while(**ptr && **ptr != '#') {
2140 if(**ptr == '%' && known_scheme &&
2141 !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
2142 if(!check_pct_encoded(ptr)) {
2143 *ptr = data->query;
2144 data->query = NULL;
2145 return FALSE;
2146 } else
2147 continue;
2150 ++(*ptr);
2153 data->query_len = *ptr - data->query;
2155 TRACE("(%p %p %x): Parsed query string %s len=%d\n", ptr, data, flags,
2156 debugstr_wn(data->query, data->query_len), data->query_len);
2157 return TRUE;
2160 /* Attempts to parse the fragment from the URI.
2162 * NOTES:
2163 * If NO_DECODE_EXTRA_INFO flag is set, then invalid percent encoded
2164 * data is allowed appear in the query string. For unknown scheme types
2165 * invalid percent encoded data is allowed to appear reguardless.
2167 static BOOL parse_fragment(const WCHAR **ptr, parse_data *data, DWORD flags) {
2168 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
2170 if(**ptr != '#') {
2171 TRACE("(%p %p %x): URI didn't contain a fragment.\n", ptr, data, flags);
2172 return TRUE;
2175 data->fragment = *ptr;
2177 ++(*ptr);
2178 while(**ptr) {
2179 if(**ptr == '%' && known_scheme &&
2180 !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
2181 if(!check_pct_encoded(ptr)) {
2182 *ptr = data->fragment;
2183 data->fragment = NULL;
2184 return FALSE;
2185 } else
2186 continue;
2189 ++(*ptr);
2192 data->fragment_len = *ptr - data->fragment;
2194 TRACE("(%p %p %x): Parsed fragment %s len=%d\n", ptr, data, flags,
2195 debugstr_wn(data->fragment, data->fragment_len), data->fragment_len);
2196 return TRUE;
2199 /* Parses and validates the components of the specified by data->uri
2200 * and stores the information it parses into 'data'.
2202 * Returns TRUE if it successfully parsed the URI. False otherwise.
2204 static BOOL parse_uri(parse_data *data, DWORD flags) {
2205 const WCHAR *ptr;
2206 const WCHAR **pptr;
2208 ptr = data->uri;
2209 pptr = &ptr;
2211 TRACE("(%p %x): BEGINNING TO PARSE URI %s.\n", data, flags, debugstr_w(data->uri));
2213 if(!parse_scheme(pptr, data, flags, 0))
2214 return FALSE;
2216 if(!parse_hierpart(pptr, data, flags))
2217 return FALSE;
2219 if(!parse_query(pptr, data, flags))
2220 return FALSE;
2222 if(!parse_fragment(pptr, data, flags))
2223 return FALSE;
2225 TRACE("(%p %x): FINISHED PARSING URI.\n", data, flags);
2226 return TRUE;
2229 static BOOL canonicalize_username(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2230 const WCHAR *ptr;
2232 if(!data->username) {
2233 uri->userinfo_start = -1;
2234 return TRUE;
2237 uri->userinfo_start = uri->canon_len;
2238 for(ptr = data->username; ptr < data->username+data->username_len; ++ptr) {
2239 if(*ptr == '%') {
2240 /* Only decode % encoded values for known scheme types. */
2241 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
2242 /* See if the value really needs decoded. */
2243 WCHAR val = decode_pct_val(ptr);
2244 if(is_unreserved(val)) {
2245 if(!computeOnly)
2246 uri->canon_uri[uri->canon_len] = val;
2248 ++uri->canon_len;
2250 /* Move pass the hex characters. */
2251 ptr += 2;
2252 continue;
2255 } else if(!is_reserved(*ptr) && !is_unreserved(*ptr) && *ptr != '\\') {
2256 /* Only percent encode forbidden characters if the NO_ENCODE_FORBIDDEN_CHARACTERS flag
2257 * is NOT set.
2259 if(!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS)) {
2260 if(!computeOnly)
2261 pct_encode_val(*ptr, uri->canon_uri + uri->canon_len);
2263 uri->canon_len += 3;
2264 continue;
2268 if(!computeOnly)
2269 /* Nothing special, so just copy the character over. */
2270 uri->canon_uri[uri->canon_len] = *ptr;
2271 ++uri->canon_len;
2274 return TRUE;
2277 static BOOL canonicalize_password(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2278 const WCHAR *ptr;
2280 if(!data->password) {
2281 uri->userinfo_split = -1;
2282 return TRUE;
2285 if(uri->userinfo_start == -1)
2286 /* Has a password, but, doesn't have a username. */
2287 uri->userinfo_start = uri->canon_len;
2289 uri->userinfo_split = uri->canon_len - uri->userinfo_start;
2291 /* Add the ':' to the userinfo component. */
2292 if(!computeOnly)
2293 uri->canon_uri[uri->canon_len] = ':';
2294 ++uri->canon_len;
2296 for(ptr = data->password; ptr < data->password+data->password_len; ++ptr) {
2297 if(*ptr == '%') {
2298 /* Only decode % encoded values for known scheme types. */
2299 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
2300 /* See if the value really needs decoded. */
2301 WCHAR val = decode_pct_val(ptr);
2302 if(is_unreserved(val)) {
2303 if(!computeOnly)
2304 uri->canon_uri[uri->canon_len] = val;
2306 ++uri->canon_len;
2308 /* Move pass the hex characters. */
2309 ptr += 2;
2310 continue;
2313 } else if(!is_reserved(*ptr) && !is_unreserved(*ptr) && *ptr != '\\') {
2314 /* Only percent encode forbidden characters if the NO_ENCODE_FORBIDDEN_CHARACTERS flag
2315 * is NOT set.
2317 if(!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS)) {
2318 if(!computeOnly)
2319 pct_encode_val(*ptr, uri->canon_uri + uri->canon_len);
2321 uri->canon_len += 3;
2322 continue;
2326 if(!computeOnly)
2327 /* Nothing special, so just copy the character over. */
2328 uri->canon_uri[uri->canon_len] = *ptr;
2329 ++uri->canon_len;
2332 return TRUE;
2335 /* Canonicalizes the userinfo of the URI represented by the parse_data.
2337 * Canonicalization of the userinfo is a simple process. If there are any percent
2338 * encoded characters that fall in the "unreserved" character set, they are decoded
2339 * to their actual value. If a character is not in the "unreserved" or "reserved" sets
2340 * then it is percent encoded. Other than that the characters are copied over without
2341 * change.
2343 static BOOL canonicalize_userinfo(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2344 uri->userinfo_start = uri->userinfo_split = -1;
2345 uri->userinfo_len = 0;
2347 if(!data->username && !data->password)
2348 /* URI doesn't have userinfo, so nothing to do here. */
2349 return TRUE;
2351 if(!canonicalize_username(data, uri, flags, computeOnly))
2352 return FALSE;
2354 if(!canonicalize_password(data, uri, flags, computeOnly))
2355 return FALSE;
2357 uri->userinfo_len = uri->canon_len - uri->userinfo_start;
2358 if(!computeOnly)
2359 TRACE("(%p %p %x %d): Canonicalized userinfo, userinfo_start=%d, userinfo=%s, userinfo_split=%d userinfo_len=%d.\n",
2360 data, uri, flags, computeOnly, uri->userinfo_start, debugstr_wn(uri->canon_uri + uri->userinfo_start, uri->userinfo_len),
2361 uri->userinfo_split, uri->userinfo_len);
2363 /* Now insert the '@' after the userinfo. */
2364 if(!computeOnly)
2365 uri->canon_uri[uri->canon_len] = '@';
2366 ++uri->canon_len;
2368 return TRUE;
2371 /* Attempts to canonicalize a reg_name.
2373 * Things that happen:
2374 * 1) If Uri_CREATE_NO_CANONICALIZE flag is not set, then the reg_name is
2375 * lower cased. Unless it's an unknown scheme type, which case it's
2376 * no lower cased reguardless.
2378 * 2) Unreserved % encoded characters are decoded for known
2379 * scheme types.
2381 * 3) Forbidden characters are % encoded as long as
2382 * Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS flag is not set and
2383 * it isn't an unknown scheme type.
2385 * 4) If it's a file scheme and the host is "localhost" it's removed.
2387 * 5) If it's a file scheme and Uri_CREATE_FILE_USE_DOS_PATH is set,
2388 * then the UNC path characters are added before the host name.
2390 static BOOL canonicalize_reg_name(const parse_data *data, Uri *uri,
2391 DWORD flags, BOOL computeOnly) {
2392 static const WCHAR localhostW[] =
2393 {'l','o','c','a','l','h','o','s','t',0};
2394 const WCHAR *ptr;
2395 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
2397 if(data->scheme_type == URL_SCHEME_FILE &&
2398 data->host_len == lstrlenW(localhostW)) {
2399 if(!StrCmpNIW(data->host, localhostW, data->host_len)) {
2400 uri->host_start = -1;
2401 uri->host_len = 0;
2402 uri->host_type = Uri_HOST_UNKNOWN;
2403 return TRUE;
2407 if(data->scheme_type == URL_SCHEME_FILE && flags & Uri_CREATE_FILE_USE_DOS_PATH) {
2408 if(!computeOnly) {
2409 uri->canon_uri[uri->canon_len] = '\\';
2410 uri->canon_uri[uri->canon_len+1] = '\\';
2412 uri->canon_len += 2;
2413 uri->authority_start = uri->canon_len;
2416 uri->host_start = uri->canon_len;
2418 for(ptr = data->host; ptr < data->host+data->host_len; ++ptr) {
2419 if(*ptr == '%' && known_scheme) {
2420 WCHAR val = decode_pct_val(ptr);
2421 if(is_unreserved(val)) {
2422 /* If NO_CANONICALZE is not set, then windows lower cases the
2423 * decoded value.
2425 if(!(flags & Uri_CREATE_NO_CANONICALIZE) && isupperW(val)) {
2426 if(!computeOnly)
2427 uri->canon_uri[uri->canon_len] = tolowerW(val);
2428 } else {
2429 if(!computeOnly)
2430 uri->canon_uri[uri->canon_len] = val;
2432 ++uri->canon_len;
2434 /* Skip past the % encoded character. */
2435 ptr += 2;
2436 continue;
2437 } else {
2438 /* Just copy the % over. */
2439 if(!computeOnly)
2440 uri->canon_uri[uri->canon_len] = *ptr;
2441 ++uri->canon_len;
2443 } else if(*ptr == '\\') {
2444 /* Only unknown scheme types could have made it here with a '\\' in the host name. */
2445 if(!computeOnly)
2446 uri->canon_uri[uri->canon_len] = *ptr;
2447 ++uri->canon_len;
2448 } else if(!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS) &&
2449 !is_unreserved(*ptr) && !is_reserved(*ptr) && known_scheme) {
2450 if(!computeOnly) {
2451 pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
2453 /* The percent encoded value gets lower cased also. */
2454 if(!(flags & Uri_CREATE_NO_CANONICALIZE)) {
2455 uri->canon_uri[uri->canon_len+1] = tolowerW(uri->canon_uri[uri->canon_len+1]);
2456 uri->canon_uri[uri->canon_len+2] = tolowerW(uri->canon_uri[uri->canon_len+2]);
2460 uri->canon_len += 3;
2461 } else {
2462 if(!computeOnly) {
2463 if(!(flags & Uri_CREATE_NO_CANONICALIZE) && known_scheme)
2464 uri->canon_uri[uri->canon_len] = tolowerW(*ptr);
2465 else
2466 uri->canon_uri[uri->canon_len] = *ptr;
2469 ++uri->canon_len;
2473 uri->host_len = uri->canon_len - uri->host_start;
2475 if(!computeOnly)
2476 TRACE("(%p %p %x %d): Canonicalize reg_name=%s len=%d\n", data, uri, flags,
2477 computeOnly, debugstr_wn(uri->canon_uri+uri->host_start, uri->host_len),
2478 uri->host_len);
2480 if(!computeOnly)
2481 find_domain_name(uri->canon_uri+uri->host_start, uri->host_len,
2482 &(uri->domain_offset));
2484 return TRUE;
2487 /* Attempts to canonicalize an implicit IPv4 address. */
2488 static BOOL canonicalize_implicit_ipv4address(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2489 uri->host_start = uri->canon_len;
2491 TRACE("%u\n", data->implicit_ipv4);
2492 /* For unknown scheme types Window's doesn't convert
2493 * the value into an IP address, but, it still considers
2494 * it an IPv4 address.
2496 if(data->scheme_type == URL_SCHEME_UNKNOWN) {
2497 if(!computeOnly)
2498 memcpy(uri->canon_uri+uri->canon_len, data->host, data->host_len*sizeof(WCHAR));
2499 uri->canon_len += data->host_len;
2500 } else {
2501 if(!computeOnly)
2502 uri->canon_len += ui2ipv4(uri->canon_uri+uri->canon_len, data->implicit_ipv4);
2503 else
2504 uri->canon_len += ui2ipv4(NULL, data->implicit_ipv4);
2507 uri->host_len = uri->canon_len - uri->host_start;
2508 uri->host_type = Uri_HOST_IPV4;
2510 if(!computeOnly)
2511 TRACE("%p %p %x %d): Canonicalized implicit IP address=%s len=%d\n",
2512 data, uri, flags, computeOnly,
2513 debugstr_wn(uri->canon_uri+uri->host_start, uri->host_len),
2514 uri->host_len);
2516 return TRUE;
2519 /* Attempts to canonicalize an IPv4 address.
2521 * If the parse_data represents a URI that has an implicit IPv4 address
2522 * (ex. http://256/, this function will convert 256 into 0.0.1.0). If
2523 * the implicit IP address exceeds the value of UINT_MAX (maximum value
2524 * for an IPv4 address) it's canonicalized as if were a reg-name.
2526 * If the parse_data contains a partial or full IPv4 address it normalizes it.
2527 * A partial IPv4 address is something like "192.0" and would be normalized to
2528 * "192.0.0.0". With a full (or partial) IPv4 address like "192.002.01.003" would
2529 * be normalized to "192.2.1.3".
2531 * NOTES:
2532 * Window's ONLY normalizes IPv4 address for known scheme types (one that isn't
2533 * URL_SCHEME_UNKNOWN). For unknown scheme types, it simply copies the data from
2534 * the original URI into the canonicalized URI, but, it still recognizes URI's
2535 * host type as HOST_IPV4.
2537 static BOOL canonicalize_ipv4address(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2538 if(data->has_implicit_ip)
2539 return canonicalize_implicit_ipv4address(data, uri, flags, computeOnly);
2540 else {
2541 uri->host_start = uri->canon_len;
2543 /* Windows only normalizes for known scheme types. */
2544 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
2545 /* parse_data contains a partial or full IPv4 address, so normalize it. */
2546 DWORD i, octetDigitCount = 0, octetCount = 0;
2547 BOOL octetHasDigit = FALSE;
2549 for(i = 0; i < data->host_len; ++i) {
2550 if(data->host[i] == '0' && !octetHasDigit) {
2551 /* Can ignore leading zeros if:
2552 * 1) It isn't the last digit of the octet.
2553 * 2) i+1 != data->host_len
2554 * 3) i+1 != '.'
2556 if(octetDigitCount == 2 ||
2557 i+1 == data->host_len ||
2558 data->host[i+1] == '.') {
2559 if(!computeOnly)
2560 uri->canon_uri[uri->canon_len] = data->host[i];
2561 ++uri->canon_len;
2562 TRACE("Adding zero\n");
2564 } else if(data->host[i] == '.') {
2565 if(!computeOnly)
2566 uri->canon_uri[uri->canon_len] = data->host[i];
2567 ++uri->canon_len;
2569 octetDigitCount = 0;
2570 octetHasDigit = FALSE;
2571 ++octetCount;
2572 } else {
2573 if(!computeOnly)
2574 uri->canon_uri[uri->canon_len] = data->host[i];
2575 ++uri->canon_len;
2577 ++octetDigitCount;
2578 octetHasDigit = TRUE;
2582 /* Make sure the canonicalized IP address has 4 dec-octets.
2583 * If doesn't add "0" ones until there is 4;
2585 for( ; octetCount < 3; ++octetCount) {
2586 if(!computeOnly) {
2587 uri->canon_uri[uri->canon_len] = '.';
2588 uri->canon_uri[uri->canon_len+1] = '0';
2591 uri->canon_len += 2;
2593 } else {
2594 /* Windows doesn't normalize addresses in unknown schemes. */
2595 if(!computeOnly)
2596 memcpy(uri->canon_uri+uri->canon_len, data->host, data->host_len*sizeof(WCHAR));
2597 uri->canon_len += data->host_len;
2600 uri->host_len = uri->canon_len - uri->host_start;
2601 if(!computeOnly)
2602 TRACE("(%p %p %x %d): Canonicalized IPv4 address, ip=%s len=%d\n",
2603 data, uri, flags, computeOnly,
2604 debugstr_wn(uri->canon_uri+uri->host_start, uri->host_len),
2605 uri->host_len);
2608 return TRUE;
2611 /* Attempts to canonicalize the IPv6 address of the URI.
2613 * Multiple things happen during the canonicalization of an IPv6 address:
2614 * 1) Any leading zero's in an h16 component are removed.
2615 * Ex: [0001:0022::] -> [1:22::]
2617 * 2) The longest sequence of zero h16 components are compressed
2618 * into a "::" (elision). If there's a tie, the first is choosen.
2620 * Ex: [0:0:0:0:1:6:7:8] -> [::1:6:7:8]
2621 * [0:0:0:0:1:2::] -> [::1:2:0:0]
2622 * [0:0:1:2:0:0:7:8] -> [::1:2:0:0:7:8]
2624 * 3) If an IPv4 address is attached to the IPv6 address, it's
2625 * also normalized.
2626 * Ex: [::001.002.022.000] -> [::1.2.22.0]
2628 * 4) If an elision is present, but, only represents 1 h16 component
2629 * it's expanded.
2631 * Ex: [1::2:3:4:5:6:7] -> [1:0:2:3:4:5:6:7]
2633 * 5) If the IPv6 address contains an IPv4 address and there exists
2634 * at least 1 non-zero h16 component the IPv4 address is converted
2635 * into two h16 components, otherwise it's normalized and kept as is.
2637 * Ex: [::192.200.003.4] -> [::192.200.3.4]
2638 * [ffff::192.200.003.4] -> [ffff::c0c8:3041]
2640 * NOTE:
2641 * For unknown scheme types Windows simply copies the address over without any
2642 * changes.
2644 * IPv4 address can be included in an elision if all its components are 0's.
2646 static BOOL canonicalize_ipv6address(const parse_data *data, Uri *uri,
2647 DWORD flags, BOOL computeOnly) {
2648 uri->host_start = uri->canon_len;
2650 if(data->scheme_type == URL_SCHEME_UNKNOWN) {
2651 if(!computeOnly)
2652 memcpy(uri->canon_uri+uri->canon_len, data->host, data->host_len*sizeof(WCHAR));
2653 uri->canon_len += data->host_len;
2654 } else {
2655 USHORT values[8];
2656 INT elision_start;
2657 DWORD i, elision_len;
2659 if(!ipv6_to_number(&(data->ipv6_address), values)) {
2660 TRACE("(%p %p %x %d): Failed to compute numerical value for IPv6 address.\n",
2661 data, uri, flags, computeOnly);
2662 return FALSE;
2665 if(!computeOnly)
2666 uri->canon_uri[uri->canon_len] = '[';
2667 ++uri->canon_len;
2669 /* Find where the elision should occur (if any). */
2670 compute_elision_location(&(data->ipv6_address), values, &elision_start, &elision_len);
2672 TRACE("%p %p %x %d): Elision starts at %d, len=%u\n", data, uri, flags,
2673 computeOnly, elision_start, elision_len);
2675 for(i = 0; i < 8; ++i) {
2676 BOOL in_elision = (elision_start > -1 && i >= elision_start &&
2677 i < elision_start+elision_len);
2678 BOOL do_ipv4 = (i == 6 && data->ipv6_address.ipv4 && !in_elision &&
2679 data->ipv6_address.h16_count == 0);
2681 if(i == elision_start) {
2682 if(!computeOnly) {
2683 uri->canon_uri[uri->canon_len] = ':';
2684 uri->canon_uri[uri->canon_len+1] = ':';
2686 uri->canon_len += 2;
2689 /* We can ignore the current component if we're in the elision. */
2690 if(in_elision)
2691 continue;
2693 /* We only add a ':' if we're not at i == 0, or when we're at
2694 * the very end of elision range since the ':' colon was handled
2695 * earlier. Otherwise we would end up with ":::" after elision.
2697 if(i != 0 && !(elision_start > -1 && i == elision_start+elision_len)) {
2698 if(!computeOnly)
2699 uri->canon_uri[uri->canon_len] = ':';
2700 ++uri->canon_len;
2703 if(do_ipv4) {
2704 UINT val;
2705 DWORD len;
2707 /* Combine the two parts of the IPv4 address values. */
2708 val = values[i];
2709 val <<= 16;
2710 val += values[i+1];
2712 if(!computeOnly)
2713 len = ui2ipv4(uri->canon_uri+uri->canon_len, val);
2714 else
2715 len = ui2ipv4(NULL, val);
2717 uri->canon_len += len;
2718 ++i;
2719 } else {
2720 /* Write a regular h16 component to the URI. */
2722 /* Short circuit for the trivial case. */
2723 if(values[i] == 0) {
2724 if(!computeOnly)
2725 uri->canon_uri[uri->canon_len] = '0';
2726 ++uri->canon_len;
2727 } else {
2728 static const WCHAR formatW[] = {'%','x',0};
2730 if(!computeOnly)
2731 uri->canon_len += sprintfW(uri->canon_uri+uri->canon_len,
2732 formatW, values[i]);
2733 else {
2734 WCHAR tmp[5];
2735 uri->canon_len += sprintfW(tmp, formatW, values[i]);
2741 /* Add the closing ']'. */
2742 if(!computeOnly)
2743 uri->canon_uri[uri->canon_len] = ']';
2744 ++uri->canon_len;
2747 uri->host_len = uri->canon_len - uri->host_start;
2749 if(!computeOnly)
2750 TRACE("(%p %p %x %d): Canonicalized IPv6 address %s, len=%d\n", data, uri, flags,
2751 computeOnly, debugstr_wn(uri->canon_uri+uri->host_start, uri->host_len),
2752 uri->host_len);
2754 return TRUE;
2757 /* Attempts to canonicalize the host of the URI (if any). */
2758 static BOOL canonicalize_host(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2759 uri->host_start = -1;
2760 uri->host_len = 0;
2761 uri->domain_offset = -1;
2763 if(data->host) {
2764 switch(data->host_type) {
2765 case Uri_HOST_DNS:
2766 uri->host_type = Uri_HOST_DNS;
2767 if(!canonicalize_reg_name(data, uri, flags, computeOnly))
2768 return FALSE;
2770 break;
2771 case Uri_HOST_IPV4:
2772 uri->host_type = Uri_HOST_IPV4;
2773 if(!canonicalize_ipv4address(data, uri, flags, computeOnly))
2774 return FALSE;
2776 break;
2777 case Uri_HOST_IPV6:
2778 if(!canonicalize_ipv6address(data, uri, flags, computeOnly))
2779 return FALSE;
2781 uri->host_type = Uri_HOST_IPV6;
2782 break;
2783 case Uri_HOST_UNKNOWN:
2784 if(data->host_len > 0 || data->scheme_type != URL_SCHEME_FILE) {
2785 uri->host_start = uri->canon_len;
2787 /* Nothing happens to unknown host types. */
2788 if(!computeOnly)
2789 memcpy(uri->canon_uri+uri->canon_len, data->host, data->host_len*sizeof(WCHAR));
2790 uri->canon_len += data->host_len;
2791 uri->host_len = data->host_len;
2794 uri->host_type = Uri_HOST_UNKNOWN;
2795 break;
2796 default:
2797 FIXME("(%p %p %x %d): Canonicalization for host type %d not supported.\n", data,
2798 uri, flags, computeOnly, data->host_type);
2799 return FALSE;
2803 return TRUE;
2806 static BOOL canonicalize_port(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2807 BOOL has_default_port = FALSE;
2808 USHORT default_port = 0;
2809 DWORD i;
2811 uri->port_offset = -1;
2813 /* Check if the scheme has a default port. */
2814 for(i = 0; i < sizeof(default_ports)/sizeof(default_ports[0]); ++i) {
2815 if(default_ports[i].scheme == data->scheme_type) {
2816 has_default_port = TRUE;
2817 default_port = default_ports[i].port;
2818 break;
2822 uri->has_port = data->has_port || has_default_port;
2824 /* Possible cases:
2825 * 1) Has a port which is the default port.
2826 * 2) Has a port (not the default).
2827 * 3) Doesn't have a port, but, scheme has a default port.
2828 * 4) No port.
2830 if(has_default_port && data->has_port && data->port_value == default_port) {
2831 /* If it's the default port and this flag isn't set, don't do anything. */
2832 if(flags & Uri_CREATE_NO_CANONICALIZE) {
2833 uri->port_offset = uri->canon_len-uri->authority_start;
2834 if(!computeOnly)
2835 uri->canon_uri[uri->canon_len] = ':';
2836 ++uri->canon_len;
2838 if(data->port) {
2839 /* Copy the original port over. */
2840 if(!computeOnly)
2841 memcpy(uri->canon_uri+uri->canon_len, data->port, data->port_len*sizeof(WCHAR));
2842 uri->canon_len += data->port_len;
2843 } else {
2844 if(!computeOnly)
2845 uri->canon_len += ui2str(uri->canon_uri+uri->canon_len, data->port_value);
2846 else
2847 uri->canon_len += ui2str(NULL, data->port_value);
2851 uri->port = default_port;
2852 } else if(data->has_port) {
2853 uri->port_offset = uri->canon_len-uri->authority_start;
2854 if(!computeOnly)
2855 uri->canon_uri[uri->canon_len] = ':';
2856 ++uri->canon_len;
2858 if(flags & Uri_CREATE_NO_CANONICALIZE && data->port) {
2859 /* Copy the original over without changes. */
2860 if(!computeOnly)
2861 memcpy(uri->canon_uri+uri->canon_len, data->port, data->port_len*sizeof(WCHAR));
2862 uri->canon_len += data->port_len;
2863 } else {
2864 if(!computeOnly)
2865 uri->canon_len += ui2str(uri->canon_uri+uri->canon_len, data->port_value);
2866 else
2867 uri->canon_len += ui2str(NULL, data->port_value);
2870 uri->port = data->port_value;
2871 } else if(has_default_port)
2872 uri->port = default_port;
2874 return TRUE;
2877 /* Canonicalizes the authority of the URI represented by the parse_data. */
2878 static BOOL canonicalize_authority(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2879 uri->authority_start = uri->canon_len;
2880 uri->authority_len = 0;
2882 if(!canonicalize_userinfo(data, uri, flags, computeOnly))
2883 return FALSE;
2885 if(!canonicalize_host(data, uri, flags, computeOnly))
2886 return FALSE;
2888 if(!canonicalize_port(data, uri, flags, computeOnly))
2889 return FALSE;
2891 if(uri->host_start != -1 || (data->is_relative && (data->password || data->username)))
2892 uri->authority_len = uri->canon_len - uri->authority_start;
2893 else
2894 uri->authority_start = -1;
2896 return TRUE;
2899 /* Attempts to canonicalize the path of a hierarchical URI.
2901 * Things that happen:
2902 * 1). Forbidden characters are percent encoded, unless the NO_ENCODE_FORBIDDEN
2903 * flag is set or it's a file URI. Forbidden characters are always encoded
2904 * for file schemes reguardless and forbidden characters are never encoded
2905 * for unknown scheme types.
2907 * 2). For known scheme types '\\' are changed to '/'.
2909 * 3). Percent encoded, unreserved characters are decoded to their actual values.
2910 * Unless the scheme type is unknown. For file schemes any percent encoded
2911 * character in the unreserved or reserved set is decoded.
2913 * 4). For File schemes if the path is starts with a drive letter and doesn't
2914 * start with a '/' then one is appended.
2915 * Ex: file://c:/test.mp3 -> file:///c:/test.mp3
2917 * 5). Dot segments are removed from the path for all scheme types
2918 * unless NO_CANONICALIZE flag is set. Dot segments aren't removed
2919 * for wildcard scheme types.
2921 * NOTES:
2922 * file://c:/test%20test -> file:///c:/test%2520test
2923 * file://c:/test%3Etest -> file:///c:/test%253Etest
2924 * if Uri_CREATE_FILE_USE_DOS_PATH is not set:
2925 * file:///c:/test%20test -> file:///c:/test%20test
2926 * file:///c:/test%test -> file:///c:/test%25test
2928 static BOOL canonicalize_path_hierarchical(const parse_data *data, Uri *uri,
2929 DWORD flags, BOOL computeOnly) {
2930 const WCHAR *ptr;
2931 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
2932 const BOOL is_file = data->scheme_type == URL_SCHEME_FILE;
2933 const BOOL is_res = data->scheme_type == URL_SCHEME_RES;
2935 BOOL escape_pct = FALSE;
2937 if(!data->path) {
2938 uri->path_start = -1;
2939 uri->path_len = 0;
2940 return TRUE;
2943 uri->path_start = uri->canon_len;
2944 ptr = data->path;
2946 if(is_file && uri->host_start == -1) {
2947 /* Check if a '/' needs to be appended for the file scheme. */
2948 if(data->path_len > 1 && is_drive_path(ptr) && !(flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
2949 if(!computeOnly)
2950 uri->canon_uri[uri->canon_len] = '/';
2951 uri->canon_len++;
2952 escape_pct = TRUE;
2953 } else if(*ptr == '/') {
2954 if(!(flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
2955 /* Copy the extra '/' over. */
2956 if(!computeOnly)
2957 uri->canon_uri[uri->canon_len] = '/';
2958 ++uri->canon_len;
2960 ++ptr;
2963 if(is_drive_path(ptr)) {
2964 if(!computeOnly) {
2965 uri->canon_uri[uri->canon_len] = *ptr;
2966 /* If theres a '|' after the drive letter, convert it to a ':'. */
2967 uri->canon_uri[uri->canon_len+1] = ':';
2969 ptr += 2;
2970 uri->canon_len += 2;
2974 if(!is_file && *(data->path) && *(data->path) != '/') {
2975 /* Prepend a '/' to the path if it doesn't have one. */
2976 if(!computeOnly)
2977 uri->canon_uri[uri->canon_len] = '/';
2978 ++uri->canon_len;
2981 for(; ptr < data->path+data->path_len; ++ptr) {
2982 BOOL do_default_action = TRUE;
2984 if(*ptr == '%' && !is_res) {
2985 const WCHAR *tmp = ptr;
2986 WCHAR val;
2988 /* Check if the % represents a valid encoded char, or if it needs encoded. */
2989 BOOL force_encode = !check_pct_encoded(&tmp) && is_file && !(flags&Uri_CREATE_FILE_USE_DOS_PATH);
2990 val = decode_pct_val(ptr);
2992 if(force_encode || escape_pct) {
2993 /* Escape the percent sign in the file URI. */
2994 if(!computeOnly)
2995 pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
2996 uri->canon_len += 3;
2997 do_default_action = FALSE;
2998 } else if((is_unreserved(val) && known_scheme) ||
2999 (is_file && (is_unreserved(val) || is_reserved(val) ||
3000 (val && flags&Uri_CREATE_FILE_USE_DOS_PATH && !is_forbidden_dos_path_char(val))))) {
3001 if(!computeOnly)
3002 uri->canon_uri[uri->canon_len] = val;
3003 ++uri->canon_len;
3005 ptr += 2;
3006 continue;
3008 } else if(*ptr == '/' && is_file && (flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
3009 /* Convert the '/' back to a '\\'. */
3010 if(!computeOnly)
3011 uri->canon_uri[uri->canon_len] = '\\';
3012 ++uri->canon_len;
3013 do_default_action = FALSE;
3014 } else if(*ptr == '\\' && known_scheme) {
3015 if(!(is_file && (flags & Uri_CREATE_FILE_USE_DOS_PATH))) {
3016 /* Convert '\\' into a '/'. */
3017 if(!computeOnly)
3018 uri->canon_uri[uri->canon_len] = '/';
3019 ++uri->canon_len;
3020 do_default_action = FALSE;
3022 } else if(known_scheme && !is_res && !is_unreserved(*ptr) && !is_reserved(*ptr) &&
3023 (!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS) || is_file)) {
3024 if(!(is_file && (flags & Uri_CREATE_FILE_USE_DOS_PATH))) {
3025 /* Escape the forbidden character. */
3026 if(!computeOnly)
3027 pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
3028 uri->canon_len += 3;
3029 do_default_action = FALSE;
3033 if(do_default_action) {
3034 if(!computeOnly)
3035 uri->canon_uri[uri->canon_len] = *ptr;
3036 ++uri->canon_len;
3040 uri->path_len = uri->canon_len - uri->path_start;
3042 /* Removing the dot segments only happens when it's not in
3043 * computeOnly mode and it's not a wildcard scheme. File schemes
3044 * with USE_DOS_PATH set don't get dot segments removed.
3046 if(!(is_file && (flags & Uri_CREATE_FILE_USE_DOS_PATH)) &&
3047 data->scheme_type != URL_SCHEME_WILDCARD) {
3048 if(!(flags & Uri_CREATE_NO_CANONICALIZE) && !computeOnly) {
3049 /* Remove the dot segments (if any) and reset everything to the new
3050 * correct length.
3052 DWORD new_len = remove_dot_segments(uri->canon_uri+uri->path_start, uri->path_len);
3053 uri->canon_len -= uri->path_len-new_len;
3054 uri->path_len = new_len;
3058 if(!computeOnly)
3059 TRACE("Canonicalized path %s len=%d\n",
3060 debugstr_wn(uri->canon_uri+uri->path_start, uri->path_len),
3061 uri->path_len);
3063 return TRUE;
3066 /* Attempts to canonicalize the path for an opaque URI.
3068 * For known scheme types:
3069 * 1) forbidden characters are percent encoded if
3070 * NO_ENCODE_FORBIDDEN_CHARACTERS isn't set.
3072 * 2) Percent encoded, unreserved characters are decoded
3073 * to their actual values, for known scheme types.
3075 * 3) '\\' are changed to '/' for known scheme types
3076 * except for mailto schemes.
3078 * 4) For file schemes, if USE_DOS_PATH is set all '/'
3079 * are converted to backslashes.
3081 * 5) For file schemes, if USE_DOS_PATH isn't set all '\'
3082 * are converted to forward slashes.
3084 static BOOL canonicalize_path_opaque(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
3085 const WCHAR *ptr;
3086 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
3087 const BOOL is_file = data->scheme_type == URL_SCHEME_FILE;
3089 if(!data->path) {
3090 uri->path_start = -1;
3091 uri->path_len = 0;
3092 return TRUE;
3095 uri->path_start = uri->canon_len;
3097 /* Windows doesn't allow a "//" to appear after the scheme
3098 * of a URI, if it's an opaque URI.
3100 if(data->scheme && *(data->path) == '/' && *(data->path+1) == '/') {
3101 /* So it inserts a "/." before the "//" if it exists. */
3102 if(!computeOnly) {
3103 uri->canon_uri[uri->canon_len] = '/';
3104 uri->canon_uri[uri->canon_len+1] = '.';
3107 uri->canon_len += 2;
3110 for(ptr = data->path; ptr < data->path+data->path_len; ++ptr) {
3111 BOOL do_default_action = TRUE;
3113 if(*ptr == '%' && known_scheme) {
3114 WCHAR val = decode_pct_val(ptr);
3116 if(is_unreserved(val)) {
3117 if(!computeOnly)
3118 uri->canon_uri[uri->canon_len] = val;
3119 ++uri->canon_len;
3121 ptr += 2;
3122 continue;
3124 } else if(*ptr == '/' && is_file && (flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
3125 if(!computeOnly)
3126 uri->canon_uri[uri->canon_len] = '\\';
3127 ++uri->canon_len;
3128 do_default_action = FALSE;
3129 } else if(*ptr == '\\') {
3130 if(is_file && !(flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
3131 /* Convert to a '/'. */
3132 if(!computeOnly)
3133 uri->canon_uri[uri->canon_len] = '/';
3134 ++uri->canon_len;
3135 do_default_action = FALSE;
3137 } else if(known_scheme && !is_unreserved(*ptr) && !is_reserved(*ptr) &&
3138 !(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS)) {
3139 if(!(is_file && (flags & Uri_CREATE_FILE_USE_DOS_PATH))) {
3140 if(!computeOnly)
3141 pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
3142 uri->canon_len += 3;
3143 do_default_action = FALSE;
3147 if(do_default_action) {
3148 if(!computeOnly)
3149 uri->canon_uri[uri->canon_len] = *ptr;
3150 ++uri->canon_len;
3154 uri->path_len = uri->canon_len - uri->path_start;
3156 TRACE("(%p %p %x %d): Canonicalized opaque URI path %s len=%d\n", data, uri, flags, computeOnly,
3157 debugstr_wn(uri->canon_uri+uri->path_start, uri->path_len), uri->path_len);
3158 return TRUE;
3161 /* Determines how the URI represented by the parse_data should be canonicalized.
3163 * Essentially, if the parse_data represents an hierarchical URI then it calls
3164 * canonicalize_authority and the canonicalization functions for the path. If the
3165 * URI is opaque it canonicalizes the path of the URI.
3167 static BOOL canonicalize_hierpart(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
3168 if(!data->is_opaque || (data->is_relative && (data->password || data->username))) {
3169 /* "//" is only added for non-wildcard scheme types.
3171 * A "//" is only added to a relative URI if it has a
3172 * host or port component (this only happens if a IUriBuilder
3173 * is generating an IUri).
3175 if((data->is_relative && (data->host || data->has_port)) ||
3176 (!data->is_relative && data->scheme_type != URL_SCHEME_WILDCARD)) {
3177 if(!computeOnly) {
3178 INT pos = uri->canon_len;
3180 uri->canon_uri[pos] = '/';
3181 uri->canon_uri[pos+1] = '/';
3183 uri->canon_len += 2;
3186 if(!canonicalize_authority(data, uri, flags, computeOnly))
3187 return FALSE;
3189 if(data->is_relative && (data->password || data->username)) {
3190 if(!canonicalize_path_opaque(data, uri, flags, computeOnly))
3191 return FALSE;
3192 } else {
3193 if(!canonicalize_path_hierarchical(data, uri, flags, computeOnly))
3194 return FALSE;
3196 } else {
3197 /* Opaque URI's don't have an authority. */
3198 uri->userinfo_start = uri->userinfo_split = -1;
3199 uri->userinfo_len = 0;
3200 uri->host_start = -1;
3201 uri->host_len = 0;
3202 uri->host_type = Uri_HOST_UNKNOWN;
3203 uri->has_port = FALSE;
3204 uri->authority_start = -1;
3205 uri->authority_len = 0;
3206 uri->domain_offset = -1;
3207 uri->port_offset = -1;
3209 if(is_hierarchical_scheme(data->scheme_type)) {
3210 DWORD i;
3212 /* Absolute URIs aren't displayed for known scheme types
3213 * which should be hierarchical URIs.
3215 uri->display_modifiers |= URI_DISPLAY_NO_ABSOLUTE_URI;
3217 /* Windows also sets the port for these (if they have one). */
3218 for(i = 0; i < sizeof(default_ports)/sizeof(default_ports[0]); ++i) {
3219 if(data->scheme_type == default_ports[i].scheme) {
3220 uri->has_port = TRUE;
3221 uri->port = default_ports[i].port;
3222 break;
3227 if(!canonicalize_path_opaque(data, uri, flags, computeOnly))
3228 return FALSE;
3231 if(uri->path_start > -1 && !computeOnly)
3232 /* Finding file extensions happens for both types of URIs. */
3233 uri->extension_offset = find_file_extension(uri->canon_uri+uri->path_start, uri->path_len);
3234 else
3235 uri->extension_offset = -1;
3237 return TRUE;
3240 /* Attempts to canonicalize the query string of the URI.
3242 * Things that happen:
3243 * 1) For known scheme types forbidden characters
3244 * are percent encoded, unless the NO_DECODE_EXTRA_INFO flag is set
3245 * or NO_ENCODE_FORBIDDEN_CHARACTERS is set.
3247 * 2) For known scheme types, percent encoded, unreserved characters
3248 * are decoded as long as the NO_DECODE_EXTRA_INFO flag isn't set.
3250 static BOOL canonicalize_query(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
3251 const WCHAR *ptr, *end;
3252 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
3254 if(!data->query) {
3255 uri->query_start = -1;
3256 uri->query_len = 0;
3257 return TRUE;
3260 uri->query_start = uri->canon_len;
3262 end = data->query+data->query_len;
3263 for(ptr = data->query; ptr < end; ++ptr) {
3264 if(*ptr == '%') {
3265 if(known_scheme && !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
3266 WCHAR val = decode_pct_val(ptr);
3267 if(is_unreserved(val)) {
3268 if(!computeOnly)
3269 uri->canon_uri[uri->canon_len] = val;
3270 ++uri->canon_len;
3272 ptr += 2;
3273 continue;
3276 } else if(known_scheme && !is_unreserved(*ptr) && !is_reserved(*ptr)) {
3277 if(!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS) &&
3278 !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
3279 if(!computeOnly)
3280 pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
3281 uri->canon_len += 3;
3282 continue;
3286 if(!computeOnly)
3287 uri->canon_uri[uri->canon_len] = *ptr;
3288 ++uri->canon_len;
3291 uri->query_len = uri->canon_len - uri->query_start;
3293 if(!computeOnly)
3294 TRACE("(%p %p %x %d): Canonicalized query string %s len=%d\n", data, uri, flags,
3295 computeOnly, debugstr_wn(uri->canon_uri+uri->query_start, uri->query_len),
3296 uri->query_len);
3297 return TRUE;
3300 static BOOL canonicalize_fragment(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
3301 const WCHAR *ptr, *end;
3302 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
3304 if(!data->fragment) {
3305 uri->fragment_start = -1;
3306 uri->fragment_len = 0;
3307 return TRUE;
3310 uri->fragment_start = uri->canon_len;
3312 end = data->fragment + data->fragment_len;
3313 for(ptr = data->fragment; ptr < end; ++ptr) {
3314 if(*ptr == '%') {
3315 if(known_scheme && !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
3316 WCHAR val = decode_pct_val(ptr);
3317 if(is_unreserved(val)) {
3318 if(!computeOnly)
3319 uri->canon_uri[uri->canon_len] = val;
3320 ++uri->canon_len;
3322 ptr += 2;
3323 continue;
3326 } else if(known_scheme && !is_unreserved(*ptr) && !is_reserved(*ptr)) {
3327 if(!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS) &&
3328 !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
3329 if(!computeOnly)
3330 pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
3331 uri->canon_len += 3;
3332 continue;
3336 if(!computeOnly)
3337 uri->canon_uri[uri->canon_len] = *ptr;
3338 ++uri->canon_len;
3341 uri->fragment_len = uri->canon_len - uri->fragment_start;
3343 if(!computeOnly)
3344 TRACE("(%p %p %x %d): Canonicalized fragment %s len=%d\n", data, uri, flags,
3345 computeOnly, debugstr_wn(uri->canon_uri+uri->fragment_start, uri->fragment_len),
3346 uri->fragment_len);
3347 return TRUE;
3350 /* Canonicalizes the scheme information specified in the parse_data using the specified flags. */
3351 static BOOL canonicalize_scheme(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
3352 uri->scheme_start = -1;
3353 uri->scheme_len = 0;
3355 if(!data->scheme) {
3356 /* The only type of URI that doesn't have to have a scheme is a relative
3357 * URI.
3359 if(!data->is_relative) {
3360 FIXME("(%p %p %x): Unable to determine the scheme type of %s.\n", data,
3361 uri, flags, debugstr_w(data->uri));
3362 return FALSE;
3364 } else {
3365 if(!computeOnly) {
3366 DWORD i;
3367 INT pos = uri->canon_len;
3369 for(i = 0; i < data->scheme_len; ++i) {
3370 /* Scheme name must be lower case after canonicalization. */
3371 uri->canon_uri[i + pos] = tolowerW(data->scheme[i]);
3374 uri->canon_uri[i + pos] = ':';
3375 uri->scheme_start = pos;
3377 TRACE("(%p %p %x): Canonicalized scheme=%s, len=%d.\n", data, uri, flags,
3378 debugstr_wn(uri->canon_uri, uri->scheme_len), data->scheme_len);
3381 /* This happens in both computation modes. */
3382 uri->canon_len += data->scheme_len + 1;
3383 uri->scheme_len = data->scheme_len;
3385 return TRUE;
3388 /* Compute's what the length of the URI specified by the parse_data will be
3389 * after canonicalization occurs using the specified flags.
3391 * This function will return a non-zero value indicating the length of the canonicalized
3392 * URI, or -1 on error.
3394 static int compute_canonicalized_length(const parse_data *data, DWORD flags) {
3395 Uri uri;
3397 memset(&uri, 0, sizeof(Uri));
3399 TRACE("(%p %x): Beginning to compute canonicalized length for URI %s\n", data, flags,
3400 debugstr_w(data->uri));
3402 if(!canonicalize_scheme(data, &uri, flags, TRUE)) {
3403 ERR("(%p %x): Failed to compute URI scheme length.\n", data, flags);
3404 return -1;
3407 if(!canonicalize_hierpart(data, &uri, flags, TRUE)) {
3408 ERR("(%p %x): Failed to compute URI hierpart length.\n", data, flags);
3409 return -1;
3412 if(!canonicalize_query(data, &uri, flags, TRUE)) {
3413 ERR("(%p %x): Failed to compute query string length.\n", data, flags);
3414 return -1;
3417 if(!canonicalize_fragment(data, &uri, flags, TRUE)) {
3418 ERR("(%p %x): Failed to compute fragment length.\n", data, flags);
3419 return -1;
3422 TRACE("(%p %x): Finished computing canonicalized URI length. length=%d\n", data, flags, uri.canon_len);
3424 return uri.canon_len;
3427 /* Canonicalizes the URI data specified in the parse_data, using the given flags. If the
3428 * canonicalization succeededs it will store all the canonicalization information
3429 * in the pointer to the Uri.
3431 * To canonicalize a URI this function first computes what the length of the URI
3432 * specified by the parse_data will be. Once this is done it will then perfom the actual
3433 * canonicalization of the URI.
3435 static HRESULT canonicalize_uri(const parse_data *data, Uri *uri, DWORD flags) {
3436 INT len;
3438 uri->canon_uri = NULL;
3439 len = uri->canon_size = uri->canon_len = 0;
3441 TRACE("(%p %p %x): beginning to canonicalize URI %s.\n", data, uri, flags, debugstr_w(data->uri));
3443 /* First try to compute the length of the URI. */
3444 len = compute_canonicalized_length(data, flags);
3445 if(len == -1) {
3446 ERR("(%p %p %x): Could not compute the canonicalized length of %s.\n", data, uri, flags,
3447 debugstr_w(data->uri));
3448 return E_INVALIDARG;
3451 uri->canon_uri = heap_alloc((len+1)*sizeof(WCHAR));
3452 if(!uri->canon_uri)
3453 return E_OUTOFMEMORY;
3455 uri->canon_size = len;
3456 if(!canonicalize_scheme(data, uri, flags, FALSE)) {
3457 ERR("(%p %p %x): Unable to canonicalize the scheme of the URI.\n", data, uri, flags);
3458 return E_INVALIDARG;
3460 uri->scheme_type = data->scheme_type;
3462 if(!canonicalize_hierpart(data, uri, flags, FALSE)) {
3463 ERR("(%p %p %x): Unable to canonicalize the heirpart of the URI\n", data, uri, flags);
3464 return E_INVALIDARG;
3467 if(!canonicalize_query(data, uri, flags, FALSE)) {
3468 ERR("(%p %p %x): Unable to canonicalize query string of the URI.\n",
3469 data, uri, flags);
3470 return E_INVALIDARG;
3473 if(!canonicalize_fragment(data, uri, flags, FALSE)) {
3474 ERR("(%p %p %x): Unable to canonicalize fragment of the URI.\n",
3475 data, uri, flags);
3476 return E_INVALIDARG;
3479 /* There's a possibility we didn't use all the space we allocated
3480 * earlier.
3482 if(uri->canon_len < uri->canon_size) {
3483 /* This happens if the URI is hierarchical and dot
3484 * segments were removed from it's path.
3486 WCHAR *tmp = heap_realloc(uri->canon_uri, (uri->canon_len+1)*sizeof(WCHAR));
3487 if(!tmp)
3488 return E_OUTOFMEMORY;
3490 uri->canon_uri = tmp;
3491 uri->canon_size = uri->canon_len;
3494 uri->canon_uri[uri->canon_len] = '\0';
3495 TRACE("(%p %p %x): finished canonicalizing the URI. uri=%s\n", data, uri, flags, debugstr_w(uri->canon_uri));
3497 return S_OK;
3500 static HRESULT get_builder_component(LPWSTR *component, DWORD *component_len,
3501 LPCWSTR source, DWORD source_len,
3502 LPCWSTR *output, DWORD *output_len)
3504 if(!output_len) {
3505 if(output)
3506 *output = NULL;
3507 return E_POINTER;
3510 if(!output) {
3511 *output_len = 0;
3512 return E_POINTER;
3515 if(!(*component) && source) {
3516 /* Allocate 'component', and copy the contents from 'source'
3517 * into the new allocation.
3519 *component = heap_alloc((source_len+1)*sizeof(WCHAR));
3520 if(!(*component))
3521 return E_OUTOFMEMORY;
3523 memcpy(*component, source, source_len*sizeof(WCHAR));
3524 (*component)[source_len] = '\0';
3525 *component_len = source_len;
3528 *output = *component;
3529 *output_len = *component_len;
3530 return *output ? S_OK : S_FALSE;
3533 /* Allocates 'component' and copies the string from 'new_value' into 'component'.
3534 * If 'prefix' is set and 'new_value' isn't NULL, then it checks if 'new_value'
3535 * starts with 'prefix'. If it doesn't then 'prefix' is prepended to 'component'.
3537 * If everything is successful, then will set 'success_flag' in 'flags'.
3539 static HRESULT set_builder_component(LPWSTR *component, DWORD *component_len, LPCWSTR new_value,
3540 WCHAR prefix, DWORD *flags, DWORD success_flag)
3542 heap_free(*component);
3544 if(!new_value) {
3545 *component = NULL;
3546 *component_len = 0;
3547 } else {
3548 BOOL add_prefix = FALSE;
3549 DWORD len = lstrlenW(new_value);
3550 DWORD pos = 0;
3552 if(prefix && *new_value != prefix) {
3553 add_prefix = TRUE;
3554 *component = heap_alloc((len+2)*sizeof(WCHAR));
3555 } else
3556 *component = heap_alloc((len+1)*sizeof(WCHAR));
3558 if(!(*component))
3559 return E_OUTOFMEMORY;
3561 if(add_prefix)
3562 (*component)[pos++] = prefix;
3564 memcpy(*component+pos, new_value, (len+1)*sizeof(WCHAR));
3565 *component_len = len+pos;
3568 *flags |= success_flag;
3569 return S_OK;
3572 static void reset_builder(UriBuilder *builder) {
3573 if(builder->uri)
3574 IUri_Release(&builder->uri->IUri_iface);
3575 builder->uri = NULL;
3577 heap_free(builder->fragment);
3578 builder->fragment = NULL;
3579 builder->fragment_len = 0;
3581 heap_free(builder->host);
3582 builder->host = NULL;
3583 builder->host_len = 0;
3585 heap_free(builder->password);
3586 builder->password = NULL;
3587 builder->password_len = 0;
3589 heap_free(builder->path);
3590 builder->path = NULL;
3591 builder->path_len = 0;
3593 heap_free(builder->query);
3594 builder->query = NULL;
3595 builder->query_len = 0;
3597 heap_free(builder->scheme);
3598 builder->scheme = NULL;
3599 builder->scheme_len = 0;
3601 heap_free(builder->username);
3602 builder->username = NULL;
3603 builder->username_len = 0;
3605 builder->has_port = FALSE;
3606 builder->port = 0;
3607 builder->modified_props = 0;
3610 static HRESULT validate_scheme_name(const UriBuilder *builder, parse_data *data, DWORD flags) {
3611 const WCHAR *component;
3612 const WCHAR *ptr;
3613 const WCHAR **pptr;
3614 DWORD expected_len;
3616 if(builder->scheme) {
3617 ptr = builder->scheme;
3618 expected_len = builder->scheme_len;
3619 } else if(builder->uri && builder->uri->scheme_start > -1) {
3620 ptr = builder->uri->canon_uri+builder->uri->scheme_start;
3621 expected_len = builder->uri->scheme_len;
3622 } else {
3623 static const WCHAR nullW[] = {0};
3624 ptr = nullW;
3625 expected_len = 0;
3628 component = ptr;
3629 pptr = &ptr;
3630 if(parse_scheme(pptr, data, flags, ALLOW_NULL_TERM_SCHEME) &&
3631 data->scheme_len == expected_len) {
3632 if(data->scheme)
3633 TRACE("(%p %p %x): Found valid scheme component %s len=%d.\n", builder, data, flags,
3634 debugstr_wn(data->scheme, data->scheme_len), data->scheme_len);
3635 } else {
3636 TRACE("(%p %p %x): Invalid scheme component found %s.\n", builder, data, flags,
3637 debugstr_wn(component, expected_len));
3638 return INET_E_INVALID_URL;
3641 return S_OK;
3644 static HRESULT validate_username(const UriBuilder *builder, parse_data *data, DWORD flags) {
3645 const WCHAR *ptr;
3646 const WCHAR **pptr;
3647 DWORD expected_len;
3649 if(builder->username) {
3650 ptr = builder->username;
3651 expected_len = builder->username_len;
3652 } else if(!(builder->modified_props & Uri_HAS_USER_NAME) && builder->uri &&
3653 builder->uri->userinfo_start > -1 && builder->uri->userinfo_split != 0) {
3654 /* Just use the username from the base Uri. */
3655 data->username = builder->uri->canon_uri+builder->uri->userinfo_start;
3656 data->username_len = (builder->uri->userinfo_split > -1) ?
3657 builder->uri->userinfo_split : builder->uri->userinfo_len;
3658 ptr = NULL;
3659 } else {
3660 ptr = NULL;
3661 expected_len = 0;
3664 if(ptr) {
3665 const WCHAR *component = ptr;
3666 pptr = &ptr;
3667 if(parse_username(pptr, data, flags, ALLOW_NULL_TERM_USER_NAME) &&
3668 data->username_len == expected_len)
3669 TRACE("(%p %p %x): Found valid username component %s len=%d.\n", builder, data, flags,
3670 debugstr_wn(data->username, data->username_len), data->username_len);
3671 else {
3672 TRACE("(%p %p %x): Invalid username component found %s.\n", builder, data, flags,
3673 debugstr_wn(component, expected_len));
3674 return INET_E_INVALID_URL;
3678 return S_OK;
3681 static HRESULT validate_password(const UriBuilder *builder, parse_data *data, DWORD flags) {
3682 const WCHAR *ptr;
3683 const WCHAR **pptr;
3684 DWORD expected_len;
3686 if(builder->password) {
3687 ptr = builder->password;
3688 expected_len = builder->password_len;
3689 } else if(!(builder->modified_props & Uri_HAS_PASSWORD) && builder->uri &&
3690 builder->uri->userinfo_split > -1) {
3691 data->password = builder->uri->canon_uri+builder->uri->userinfo_start+builder->uri->userinfo_split+1;
3692 data->password_len = builder->uri->userinfo_len-builder->uri->userinfo_split-1;
3693 ptr = NULL;
3694 } else {
3695 ptr = NULL;
3696 expected_len = 0;
3699 if(ptr) {
3700 const WCHAR *component = ptr;
3701 pptr = &ptr;
3702 if(parse_password(pptr, data, flags, ALLOW_NULL_TERM_PASSWORD) &&
3703 data->password_len == expected_len)
3704 TRACE("(%p %p %x): Found valid password component %s len=%d.\n", builder, data, flags,
3705 debugstr_wn(data->password, data->password_len), data->password_len);
3706 else {
3707 TRACE("(%p %p %x): Invalid password component found %s.\n", builder, data, flags,
3708 debugstr_wn(component, expected_len));
3709 return INET_E_INVALID_URL;
3713 return S_OK;
3716 static HRESULT validate_userinfo(const UriBuilder *builder, parse_data *data, DWORD flags) {
3717 HRESULT hr;
3719 hr = validate_username(builder, data, flags);
3720 if(FAILED(hr))
3721 return hr;
3723 hr = validate_password(builder, data, flags);
3724 if(FAILED(hr))
3725 return hr;
3727 return S_OK;
3730 static HRESULT validate_host(const UriBuilder *builder, parse_data *data, DWORD flags) {
3731 const WCHAR *ptr;
3732 const WCHAR **pptr;
3733 DWORD expected_len;
3735 if(builder->host) {
3736 ptr = builder->host;
3737 expected_len = builder->host_len;
3738 } else if(!(builder->modified_props & Uri_HAS_HOST) && builder->uri && builder->uri->host_start > -1) {
3739 ptr = builder->uri->canon_uri + builder->uri->host_start;
3740 expected_len = builder->uri->host_len;
3741 } else
3742 ptr = NULL;
3744 if(ptr) {
3745 const WCHAR *component = ptr;
3746 DWORD extras = ALLOW_BRACKETLESS_IP_LITERAL|IGNORE_PORT_DELIMITER|SKIP_IP_FUTURE_CHECK;
3747 pptr = &ptr;
3749 if(parse_host(pptr, data, flags, extras) && data->host_len == expected_len)
3750 TRACE("(%p %p %x): Found valid host name %s len=%d type=%d.\n", builder, data, flags,
3751 debugstr_wn(data->host, data->host_len), data->host_len, data->host_type);
3752 else {
3753 TRACE("(%p %p %x): Invalid host name found %s.\n", builder, data, flags,
3754 debugstr_wn(component, expected_len));
3755 return INET_E_INVALID_URL;
3759 return S_OK;
3762 static void setup_port(const UriBuilder *builder, parse_data *data, DWORD flags) {
3763 if(builder->modified_props & Uri_HAS_PORT) {
3764 if(builder->has_port) {
3765 data->has_port = TRUE;
3766 data->port_value = builder->port;
3768 } else if(builder->uri && builder->uri->has_port) {
3769 data->has_port = TRUE;
3770 data->port_value = builder->uri->port;
3773 if(data->has_port)
3774 TRACE("(%p %p %x): Using %u as port for IUri.\n", builder, data, flags, data->port_value);
3777 static HRESULT validate_path(const UriBuilder *builder, parse_data *data, DWORD flags) {
3778 const WCHAR *ptr = NULL;
3779 const WCHAR *component;
3780 const WCHAR **pptr;
3781 DWORD expected_len;
3782 BOOL check_len = TRUE;
3783 BOOL valid = FALSE;
3785 if(builder->path) {
3786 ptr = builder->path;
3787 expected_len = builder->path_len;
3788 } else if(!(builder->modified_props & Uri_HAS_PATH) &&
3789 builder->uri && builder->uri->path_start > -1) {
3790 ptr = builder->uri->canon_uri+builder->uri->path_start;
3791 expected_len = builder->uri->path_len;
3792 } else {
3793 static const WCHAR nullW[] = {0};
3794 ptr = nullW;
3795 check_len = FALSE;
3798 component = ptr;
3799 pptr = &ptr;
3801 /* How the path is validated depends on what type of
3802 * URI it is.
3804 valid = data->is_opaque ?
3805 parse_path_opaque(pptr, data, flags) : parse_path_hierarchical(pptr, data, flags);
3807 if(!valid || (check_len && expected_len != data->path_len)) {
3808 TRACE("(%p %p %x): Invalid path component %s.\n", builder, data, flags,
3809 debugstr_wn(component, check_len ? expected_len : -1) );
3810 return INET_E_INVALID_URL;
3813 TRACE("(%p %p %x): Valid path component %s len=%d.\n", builder, data, flags,
3814 debugstr_wn(data->path, data->path_len), data->path_len);
3816 return S_OK;
3819 static HRESULT validate_query(const UriBuilder *builder, parse_data *data, DWORD flags) {
3820 const WCHAR *ptr = NULL;
3821 const WCHAR **pptr;
3822 DWORD expected_len;
3824 if(builder->query) {
3825 ptr = builder->query;
3826 expected_len = builder->query_len;
3827 } else if(!(builder->modified_props & Uri_HAS_QUERY) && builder->uri &&
3828 builder->uri->query_start > -1) {
3829 ptr = builder->uri->canon_uri+builder->uri->query_start;
3830 expected_len = builder->uri->query_len;
3833 if(ptr) {
3834 const WCHAR *component = ptr;
3835 pptr = &ptr;
3837 if(parse_query(pptr, data, flags) && expected_len == data->query_len)
3838 TRACE("(%p %p %x): Valid query component %s len=%d.\n", builder, data, flags,
3839 debugstr_wn(data->query, data->query_len), data->query_len);
3840 else {
3841 TRACE("(%p %p %x): Invalid query component %s.\n", builder, data, flags,
3842 debugstr_wn(component, expected_len));
3843 return INET_E_INVALID_URL;
3847 return S_OK;
3850 static HRESULT validate_fragment(const UriBuilder *builder, parse_data *data, DWORD flags) {
3851 const WCHAR *ptr = NULL;
3852 const WCHAR **pptr;
3853 DWORD expected_len;
3855 if(builder->fragment) {
3856 ptr = builder->fragment;
3857 expected_len = builder->fragment_len;
3858 } else if(!(builder->modified_props & Uri_HAS_FRAGMENT) && builder->uri &&
3859 builder->uri->fragment_start > -1) {
3860 ptr = builder->uri->canon_uri+builder->uri->fragment_start;
3861 expected_len = builder->uri->fragment_len;
3864 if(ptr) {
3865 const WCHAR *component = ptr;
3866 pptr = &ptr;
3868 if(parse_fragment(pptr, data, flags) && expected_len == data->fragment_len)
3869 TRACE("(%p %p %x): Valid fragment component %s len=%d.\n", builder, data, flags,
3870 debugstr_wn(data->fragment, data->fragment_len), data->fragment_len);
3871 else {
3872 TRACE("(%p %p %x): Invalid fragment component %s.\n", builder, data, flags,
3873 debugstr_wn(component, expected_len));
3874 return INET_E_INVALID_URL;
3878 return S_OK;
3881 static HRESULT validate_components(const UriBuilder *builder, parse_data *data, DWORD flags) {
3882 HRESULT hr;
3884 memset(data, 0, sizeof(parse_data));
3886 TRACE("(%p %p %x): Beginning to validate builder components.\n", builder, data, flags);
3888 hr = validate_scheme_name(builder, data, flags);
3889 if(FAILED(hr))
3890 return hr;
3892 /* Extra validation for file schemes. */
3893 if(data->scheme_type == URL_SCHEME_FILE) {
3894 if((builder->password || (builder->uri && builder->uri->userinfo_split > -1)) ||
3895 (builder->username || (builder->uri && builder->uri->userinfo_start > -1))) {
3896 TRACE("(%p %p %x): File schemes can't contain a username or password.\n",
3897 builder, data, flags);
3898 return INET_E_INVALID_URL;
3902 hr = validate_userinfo(builder, data, flags);
3903 if(FAILED(hr))
3904 return hr;
3906 hr = validate_host(builder, data, flags);
3907 if(FAILED(hr))
3908 return hr;
3910 setup_port(builder, data, flags);
3912 /* The URI is opaque if it doesn't have an authority component. */
3913 if(!data->is_relative)
3914 data->is_opaque = !data->username && !data->password && !data->host && !data->has_port;
3915 else
3916 data->is_opaque = !data->host && !data->has_port;
3918 hr = validate_path(builder, data, flags);
3919 if(FAILED(hr))
3920 return hr;
3922 hr = validate_query(builder, data, flags);
3923 if(FAILED(hr))
3924 return hr;
3926 hr = validate_fragment(builder, data, flags);
3927 if(FAILED(hr))
3928 return hr;
3930 TRACE("(%p %p %x): Finished validating builder components.\n", builder, data, flags);
3932 return S_OK;
3935 static void convert_to_dos_path(const WCHAR *path, DWORD path_len,
3936 WCHAR *output, DWORD *output_len)
3938 const WCHAR *ptr = path;
3940 if(path_len > 3 && *ptr == '/' && is_drive_path(path+1))
3941 /* Skip over the leading / before the drive path. */
3942 ++ptr;
3944 for(; ptr < path+path_len; ++ptr) {
3945 if(*ptr == '/') {
3946 if(output)
3947 *output++ = '\\';
3948 (*output_len)++;
3949 } else {
3950 if(output)
3951 *output++ = *ptr;
3952 (*output_len)++;
3957 /* Generates a raw uri string using the parse_data. */
3958 static DWORD generate_raw_uri(const parse_data *data, BSTR uri, DWORD flags) {
3959 DWORD length = 0;
3961 if(data->scheme) {
3962 if(uri) {
3963 memcpy(uri, data->scheme, data->scheme_len*sizeof(WCHAR));
3964 uri[data->scheme_len] = ':';
3966 length += data->scheme_len+1;
3969 if(!data->is_opaque) {
3970 /* For the "//" which appears before the authority component. */
3971 if(uri) {
3972 uri[length] = '/';
3973 uri[length+1] = '/';
3975 length += 2;
3977 /* Check if we need to add the "\\" before the host name
3978 * of a UNC server name in a DOS path.
3980 if(flags & RAW_URI_CONVERT_TO_DOS_PATH &&
3981 data->scheme_type == URL_SCHEME_FILE && data->host) {
3982 if(uri) {
3983 uri[length] = '\\';
3984 uri[length+1] = '\\';
3986 length += 2;
3990 if(data->username) {
3991 if(uri)
3992 memcpy(uri+length, data->username, data->username_len*sizeof(WCHAR));
3993 length += data->username_len;
3996 if(data->password) {
3997 if(uri) {
3998 uri[length] = ':';
3999 memcpy(uri+length+1, data->password, data->password_len*sizeof(WCHAR));
4001 length += data->password_len+1;
4004 if(data->password || data->username) {
4005 if(uri)
4006 uri[length] = '@';
4007 ++length;
4010 if(data->host) {
4011 /* IPv6 addresses get the brackets added around them if they don't already
4012 * have them.
4014 const BOOL add_brackets = data->host_type == Uri_HOST_IPV6 && *(data->host) != '[';
4015 if(add_brackets) {
4016 if(uri)
4017 uri[length] = '[';
4018 ++length;
4021 if(uri)
4022 memcpy(uri+length, data->host, data->host_len*sizeof(WCHAR));
4023 length += data->host_len;
4025 if(add_brackets) {
4026 if(uri)
4027 uri[length] = ']';
4028 length++;
4032 if(data->has_port) {
4033 /* The port isn't included in the raw uri if it's the default
4034 * port for the scheme type.
4036 DWORD i;
4037 BOOL is_default = FALSE;
4039 for(i = 0; i < sizeof(default_ports)/sizeof(default_ports[0]); ++i) {
4040 if(data->scheme_type == default_ports[i].scheme &&
4041 data->port_value == default_ports[i].port)
4042 is_default = TRUE;
4045 if(!is_default || flags & RAW_URI_FORCE_PORT_DISP) {
4046 if(uri)
4047 uri[length] = ':';
4048 ++length;
4050 if(uri)
4051 length += ui2str(uri+length, data->port_value);
4052 else
4053 length += ui2str(NULL, data->port_value);
4057 /* Check if a '/' should be added before the path for hierarchical URIs. */
4058 if(!data->is_opaque && data->path && *(data->path) != '/') {
4059 if(uri)
4060 uri[length] = '/';
4061 ++length;
4064 if(data->path) {
4065 if(!data->is_opaque && data->scheme_type == URL_SCHEME_FILE &&
4066 flags & RAW_URI_CONVERT_TO_DOS_PATH) {
4067 DWORD len = 0;
4069 if(uri)
4070 convert_to_dos_path(data->path, data->path_len, uri+length, &len);
4071 else
4072 convert_to_dos_path(data->path, data->path_len, NULL, &len);
4074 length += len;
4075 } else {
4076 if(uri)
4077 memcpy(uri+length, data->path, data->path_len*sizeof(WCHAR));
4078 length += data->path_len;
4082 if(data->query) {
4083 if(uri)
4084 memcpy(uri+length, data->query, data->query_len*sizeof(WCHAR));
4085 length += data->query_len;
4088 if(data->fragment) {
4089 if(uri)
4090 memcpy(uri+length, data->fragment, data->fragment_len*sizeof(WCHAR));
4091 length += data->fragment_len;
4094 if(uri)
4095 TRACE("(%p %p): Generated raw uri=%s len=%d\n", data, uri, debugstr_wn(uri, length), length);
4096 else
4097 TRACE("(%p %p): Computed raw uri len=%d\n", data, uri, length);
4099 return length;
4102 static HRESULT generate_uri(const UriBuilder *builder, const parse_data *data, Uri *uri, DWORD flags) {
4103 HRESULT hr;
4104 DWORD length = generate_raw_uri(data, NULL, 0);
4105 uri->raw_uri = SysAllocStringLen(NULL, length);
4106 if(!uri->raw_uri)
4107 return E_OUTOFMEMORY;
4109 generate_raw_uri(data, uri->raw_uri, 0);
4111 hr = canonicalize_uri(data, uri, flags);
4112 if(FAILED(hr)) {
4113 if(hr == E_INVALIDARG)
4114 return INET_E_INVALID_URL;
4115 return hr;
4118 uri->create_flags = flags;
4119 return S_OK;
4122 static inline Uri* impl_from_IUri(IUri *iface)
4124 return CONTAINING_RECORD(iface, Uri, IUri_iface);
4127 static inline void destory_uri_obj(Uri *This)
4129 SysFreeString(This->raw_uri);
4130 heap_free(This->canon_uri);
4131 heap_free(This);
4134 static HRESULT WINAPI Uri_QueryInterface(IUri *iface, REFIID riid, void **ppv)
4136 Uri *This = impl_from_IUri(iface);
4138 if(IsEqualGUID(&IID_IUnknown, riid)) {
4139 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
4140 *ppv = &This->IUri_iface;
4141 }else if(IsEqualGUID(&IID_IUri, riid)) {
4142 TRACE("(%p)->(IID_IUri %p)\n", This, ppv);
4143 *ppv = &This->IUri_iface;
4144 }else if(IsEqualGUID(&IID_IUriBuilderFactory, riid)) {
4145 TRACE("(%p)->(IID_IUriBuilderFactory %p)\n", This, riid);
4146 *ppv = &This->IUriBuilderFactory_iface;
4147 }else if(IsEqualGUID(&IID_IUriObj, riid)) {
4148 TRACE("(%p)->(IID_IUriObj %p)\n", This, ppv);
4149 *ppv = This;
4150 return S_OK;
4151 }else {
4152 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
4153 *ppv = NULL;
4154 return E_NOINTERFACE;
4157 IUnknown_AddRef((IUnknown*)*ppv);
4158 return S_OK;
4161 static ULONG WINAPI Uri_AddRef(IUri *iface)
4163 Uri *This = impl_from_IUri(iface);
4164 LONG ref = InterlockedIncrement(&This->ref);
4166 TRACE("(%p) ref=%d\n", This, ref);
4168 return ref;
4171 static ULONG WINAPI Uri_Release(IUri *iface)
4173 Uri *This = impl_from_IUri(iface);
4174 LONG ref = InterlockedDecrement(&This->ref);
4176 TRACE("(%p) ref=%d\n", This, ref);
4178 if(!ref)
4179 destory_uri_obj(This);
4181 return ref;
4184 static HRESULT WINAPI Uri_GetPropertyBSTR(IUri *iface, Uri_PROPERTY uriProp, BSTR *pbstrProperty, DWORD dwFlags)
4186 Uri *This = impl_from_IUri(iface);
4187 HRESULT hres;
4188 TRACE("(%p)->(%d %p %x)\n", This, uriProp, pbstrProperty, dwFlags);
4190 if(!pbstrProperty)
4191 return E_POINTER;
4193 if(uriProp > Uri_PROPERTY_STRING_LAST) {
4194 /* Windows allocates an empty BSTR for invalid Uri_PROPERTY's. */
4195 *pbstrProperty = SysAllocStringLen(NULL, 0);
4196 if(!(*pbstrProperty))
4197 return E_OUTOFMEMORY;
4199 /* It only returns S_FALSE for the ZONE property... */
4200 if(uriProp == Uri_PROPERTY_ZONE)
4201 return S_FALSE;
4202 else
4203 return S_OK;
4206 /* Don't have support for flags yet. */
4207 if(dwFlags) {
4208 FIXME("(%p)->(%d %p %x)\n", This, uriProp, pbstrProperty, dwFlags);
4209 return E_NOTIMPL;
4212 switch(uriProp) {
4213 case Uri_PROPERTY_ABSOLUTE_URI:
4214 if(This->display_modifiers & URI_DISPLAY_NO_ABSOLUTE_URI) {
4215 *pbstrProperty = SysAllocStringLen(NULL, 0);
4216 hres = S_FALSE;
4217 } else {
4218 if(This->scheme_type != URL_SCHEME_UNKNOWN && This->userinfo_start > -1) {
4219 if(This->userinfo_len == 0) {
4220 /* Don't include the '@' after the userinfo component. */
4221 *pbstrProperty = SysAllocStringLen(NULL, This->canon_len-1);
4222 hres = S_OK;
4223 if(*pbstrProperty) {
4224 /* Copy everything before it. */
4225 memcpy(*pbstrProperty, This->canon_uri, This->userinfo_start*sizeof(WCHAR));
4227 /* And everything after it. */
4228 memcpy(*pbstrProperty+This->userinfo_start, This->canon_uri+This->userinfo_start+1,
4229 (This->canon_len-This->userinfo_start-1)*sizeof(WCHAR));
4231 } else if(This->userinfo_split == 0 && This->userinfo_len == 1) {
4232 /* Don't include the ":@" */
4233 *pbstrProperty = SysAllocStringLen(NULL, This->canon_len-2);
4234 hres = S_OK;
4235 if(*pbstrProperty) {
4236 memcpy(*pbstrProperty, This->canon_uri, This->userinfo_start*sizeof(WCHAR));
4237 memcpy(*pbstrProperty+This->userinfo_start, This->canon_uri+This->userinfo_start+2,
4238 (This->canon_len-This->userinfo_start-2)*sizeof(WCHAR));
4240 } else {
4241 *pbstrProperty = SysAllocString(This->canon_uri);
4242 hres = S_OK;
4244 } else {
4245 *pbstrProperty = SysAllocString(This->canon_uri);
4246 hres = S_OK;
4250 if(!(*pbstrProperty))
4251 hres = E_OUTOFMEMORY;
4253 break;
4254 case Uri_PROPERTY_AUTHORITY:
4255 if(This->authority_start > -1) {
4256 if(This->port_offset > -1 && is_default_port(This->scheme_type, This->port) &&
4257 This->display_modifiers & URI_DISPLAY_NO_DEFAULT_PORT_AUTH)
4258 /* Don't include the port in the authority component. */
4259 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->authority_start, This->port_offset);
4260 else
4261 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->authority_start, This->authority_len);
4262 hres = S_OK;
4263 } else {
4264 *pbstrProperty = SysAllocStringLen(NULL, 0);
4265 hres = S_FALSE;
4268 if(!(*pbstrProperty))
4269 hres = E_OUTOFMEMORY;
4271 break;
4272 case Uri_PROPERTY_DISPLAY_URI:
4273 /* The Display URI contains everything except for the userinfo for known
4274 * scheme types.
4276 if(This->scheme_type != URL_SCHEME_UNKNOWN && This->userinfo_start > -1) {
4277 *pbstrProperty = SysAllocStringLen(NULL, This->canon_len-This->userinfo_len);
4279 if(*pbstrProperty) {
4280 /* Copy everything before the userinfo over. */
4281 memcpy(*pbstrProperty, This->canon_uri, This->userinfo_start*sizeof(WCHAR));
4282 /* Copy everything after the userinfo over. */
4283 memcpy(*pbstrProperty+This->userinfo_start,
4284 This->canon_uri+This->userinfo_start+This->userinfo_len+1,
4285 (This->canon_len-(This->userinfo_start+This->userinfo_len+1))*sizeof(WCHAR));
4287 } else
4288 *pbstrProperty = SysAllocString(This->canon_uri);
4290 if(!(*pbstrProperty))
4291 hres = E_OUTOFMEMORY;
4292 else
4293 hres = S_OK;
4295 break;
4296 case Uri_PROPERTY_DOMAIN:
4297 if(This->domain_offset > -1) {
4298 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->host_start+This->domain_offset,
4299 This->host_len-This->domain_offset);
4300 hres = S_OK;
4301 } else {
4302 *pbstrProperty = SysAllocStringLen(NULL, 0);
4303 hres = S_FALSE;
4306 if(!(*pbstrProperty))
4307 hres = E_OUTOFMEMORY;
4309 break;
4310 case Uri_PROPERTY_EXTENSION:
4311 if(This->extension_offset > -1) {
4312 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->path_start+This->extension_offset,
4313 This->path_len-This->extension_offset);
4314 hres = S_OK;
4315 } else {
4316 *pbstrProperty = SysAllocStringLen(NULL, 0);
4317 hres = S_FALSE;
4320 if(!(*pbstrProperty))
4321 hres = E_OUTOFMEMORY;
4323 break;
4324 case Uri_PROPERTY_FRAGMENT:
4325 if(This->fragment_start > -1) {
4326 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->fragment_start, This->fragment_len);
4327 hres = S_OK;
4328 } else {
4329 *pbstrProperty = SysAllocStringLen(NULL, 0);
4330 hres = S_FALSE;
4333 if(!(*pbstrProperty))
4334 hres = E_OUTOFMEMORY;
4336 break;
4337 case Uri_PROPERTY_HOST:
4338 if(This->host_start > -1) {
4339 /* The '[' and ']' aren't included for IPv6 addresses. */
4340 if(This->host_type == Uri_HOST_IPV6)
4341 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->host_start+1, This->host_len-2);
4342 else
4343 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->host_start, This->host_len);
4345 hres = S_OK;
4346 } else {
4347 *pbstrProperty = SysAllocStringLen(NULL, 0);
4348 hres = S_FALSE;
4351 if(!(*pbstrProperty))
4352 hres = E_OUTOFMEMORY;
4354 break;
4355 case Uri_PROPERTY_PASSWORD:
4356 if(This->userinfo_split > -1) {
4357 *pbstrProperty = SysAllocStringLen(
4358 This->canon_uri+This->userinfo_start+This->userinfo_split+1,
4359 This->userinfo_len-This->userinfo_split-1);
4360 hres = S_OK;
4361 } else {
4362 *pbstrProperty = SysAllocStringLen(NULL, 0);
4363 hres = S_FALSE;
4366 if(!(*pbstrProperty))
4367 return E_OUTOFMEMORY;
4369 break;
4370 case Uri_PROPERTY_PATH:
4371 if(This->path_start > -1) {
4372 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->path_start, This->path_len);
4373 hres = S_OK;
4374 } else {
4375 *pbstrProperty = SysAllocStringLen(NULL, 0);
4376 hres = S_FALSE;
4379 if(!(*pbstrProperty))
4380 hres = E_OUTOFMEMORY;
4382 break;
4383 case Uri_PROPERTY_PATH_AND_QUERY:
4384 if(This->path_start > -1) {
4385 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->path_start, This->path_len+This->query_len);
4386 hres = S_OK;
4387 } else if(This->query_start > -1) {
4388 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->query_start, This->query_len);
4389 hres = S_OK;
4390 } else {
4391 *pbstrProperty = SysAllocStringLen(NULL, 0);
4392 hres = S_FALSE;
4395 if(!(*pbstrProperty))
4396 hres = E_OUTOFMEMORY;
4398 break;
4399 case Uri_PROPERTY_QUERY:
4400 if(This->query_start > -1) {
4401 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->query_start, This->query_len);
4402 hres = S_OK;
4403 } else {
4404 *pbstrProperty = SysAllocStringLen(NULL, 0);
4405 hres = S_FALSE;
4408 if(!(*pbstrProperty))
4409 hres = E_OUTOFMEMORY;
4411 break;
4412 case Uri_PROPERTY_RAW_URI:
4413 *pbstrProperty = SysAllocString(This->raw_uri);
4414 if(!(*pbstrProperty))
4415 hres = E_OUTOFMEMORY;
4416 else
4417 hres = S_OK;
4418 break;
4419 case Uri_PROPERTY_SCHEME_NAME:
4420 if(This->scheme_start > -1) {
4421 *pbstrProperty = SysAllocStringLen(This->canon_uri + This->scheme_start, This->scheme_len);
4422 hres = S_OK;
4423 } else {
4424 *pbstrProperty = SysAllocStringLen(NULL, 0);
4425 hres = S_FALSE;
4428 if(!(*pbstrProperty))
4429 hres = E_OUTOFMEMORY;
4431 break;
4432 case Uri_PROPERTY_USER_INFO:
4433 if(This->userinfo_start > -1) {
4434 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->userinfo_start, This->userinfo_len);
4435 hres = S_OK;
4436 } else {
4437 *pbstrProperty = SysAllocStringLen(NULL, 0);
4438 hres = S_FALSE;
4441 if(!(*pbstrProperty))
4442 hres = E_OUTOFMEMORY;
4444 break;
4445 case Uri_PROPERTY_USER_NAME:
4446 if(This->userinfo_start > -1 && This->userinfo_split != 0) {
4447 /* If userinfo_split is set, that means a password exists
4448 * so the username is only from userinfo_start to userinfo_split.
4450 if(This->userinfo_split > -1) {
4451 *pbstrProperty = SysAllocStringLen(This->canon_uri + This->userinfo_start, This->userinfo_split);
4452 hres = S_OK;
4453 } else {
4454 *pbstrProperty = SysAllocStringLen(This->canon_uri + This->userinfo_start, This->userinfo_len);
4455 hres = S_OK;
4457 } else {
4458 *pbstrProperty = SysAllocStringLen(NULL, 0);
4459 hres = S_FALSE;
4462 if(!(*pbstrProperty))
4463 return E_OUTOFMEMORY;
4465 break;
4466 default:
4467 FIXME("(%p)->(%d %p %x)\n", This, uriProp, pbstrProperty, dwFlags);
4468 hres = E_NOTIMPL;
4471 return hres;
4474 static HRESULT WINAPI Uri_GetPropertyLength(IUri *iface, Uri_PROPERTY uriProp, DWORD *pcchProperty, DWORD dwFlags)
4476 Uri *This = impl_from_IUri(iface);
4477 HRESULT hres;
4478 TRACE("(%p)->(%d %p %x)\n", This, uriProp, pcchProperty, dwFlags);
4480 if(!pcchProperty)
4481 return E_INVALIDARG;
4483 /* Can only return a length for a property if it's a string. */
4484 if(uriProp > Uri_PROPERTY_STRING_LAST)
4485 return E_INVALIDARG;
4487 /* Don't have support for flags yet. */
4488 if(dwFlags) {
4489 FIXME("(%p)->(%d %p %x)\n", This, uriProp, pcchProperty, dwFlags);
4490 return E_NOTIMPL;
4493 switch(uriProp) {
4494 case Uri_PROPERTY_ABSOLUTE_URI:
4495 if(This->display_modifiers & URI_DISPLAY_NO_ABSOLUTE_URI) {
4496 *pcchProperty = 0;
4497 hres = S_FALSE;
4498 } else {
4499 if(This->scheme_type != URL_SCHEME_UNKNOWN) {
4500 if(This->userinfo_start > -1 && This->userinfo_len == 0)
4501 /* Don't include the '@' in the length. */
4502 *pcchProperty = This->canon_len-1;
4503 else if(This->userinfo_start > -1 && This->userinfo_len == 1 &&
4504 This->userinfo_split == 0)
4505 /* Don't include the ":@" in the length. */
4506 *pcchProperty = This->canon_len-2;
4507 else
4508 *pcchProperty = This->canon_len;
4509 } else
4510 *pcchProperty = This->canon_len;
4512 hres = S_OK;
4515 break;
4516 case Uri_PROPERTY_AUTHORITY:
4517 if(This->port_offset > -1 &&
4518 This->display_modifiers & URI_DISPLAY_NO_DEFAULT_PORT_AUTH &&
4519 is_default_port(This->scheme_type, This->port))
4520 /* Only count up until the port in the authority. */
4521 *pcchProperty = This->port_offset;
4522 else
4523 *pcchProperty = This->authority_len;
4524 hres = (This->authority_start > -1) ? S_OK : S_FALSE;
4525 break;
4526 case Uri_PROPERTY_DISPLAY_URI:
4527 if(This->scheme_type != URL_SCHEME_UNKNOWN && This->userinfo_start > -1)
4528 *pcchProperty = This->canon_len-This->userinfo_len-1;
4529 else
4530 *pcchProperty = This->canon_len;
4532 hres = S_OK;
4533 break;
4534 case Uri_PROPERTY_DOMAIN:
4535 if(This->domain_offset > -1)
4536 *pcchProperty = This->host_len - This->domain_offset;
4537 else
4538 *pcchProperty = 0;
4540 hres = (This->domain_offset > -1) ? S_OK : S_FALSE;
4541 break;
4542 case Uri_PROPERTY_EXTENSION:
4543 if(This->extension_offset > -1) {
4544 *pcchProperty = This->path_len - This->extension_offset;
4545 hres = S_OK;
4546 } else {
4547 *pcchProperty = 0;
4548 hres = S_FALSE;
4551 break;
4552 case Uri_PROPERTY_FRAGMENT:
4553 *pcchProperty = This->fragment_len;
4554 hres = (This->fragment_start > -1) ? S_OK : S_FALSE;
4555 break;
4556 case Uri_PROPERTY_HOST:
4557 *pcchProperty = This->host_len;
4559 /* '[' and ']' aren't included in the length. */
4560 if(This->host_type == Uri_HOST_IPV6)
4561 *pcchProperty -= 2;
4563 hres = (This->host_start > -1) ? S_OK : S_FALSE;
4564 break;
4565 case Uri_PROPERTY_PASSWORD:
4566 *pcchProperty = (This->userinfo_split > -1) ? This->userinfo_len-This->userinfo_split-1 : 0;
4567 hres = (This->userinfo_split > -1) ? S_OK : S_FALSE;
4568 break;
4569 case Uri_PROPERTY_PATH:
4570 *pcchProperty = This->path_len;
4571 hres = (This->path_start > -1) ? S_OK : S_FALSE;
4572 break;
4573 case Uri_PROPERTY_PATH_AND_QUERY:
4574 *pcchProperty = This->path_len+This->query_len;
4575 hres = (This->path_start > -1 || This->query_start > -1) ? S_OK : S_FALSE;
4576 break;
4577 case Uri_PROPERTY_QUERY:
4578 *pcchProperty = This->query_len;
4579 hres = (This->query_start > -1) ? S_OK : S_FALSE;
4580 break;
4581 case Uri_PROPERTY_RAW_URI:
4582 *pcchProperty = SysStringLen(This->raw_uri);
4583 hres = S_OK;
4584 break;
4585 case Uri_PROPERTY_SCHEME_NAME:
4586 *pcchProperty = This->scheme_len;
4587 hres = (This->scheme_start > -1) ? S_OK : S_FALSE;
4588 break;
4589 case Uri_PROPERTY_USER_INFO:
4590 *pcchProperty = This->userinfo_len;
4591 hres = (This->userinfo_start > -1) ? S_OK : S_FALSE;
4592 break;
4593 case Uri_PROPERTY_USER_NAME:
4594 *pcchProperty = (This->userinfo_split > -1) ? This->userinfo_split : This->userinfo_len;
4595 if(This->userinfo_split == 0)
4596 hres = S_FALSE;
4597 else
4598 hres = (This->userinfo_start > -1) ? S_OK : S_FALSE;
4599 break;
4600 default:
4601 FIXME("(%p)->(%d %p %x)\n", This, uriProp, pcchProperty, dwFlags);
4602 hres = E_NOTIMPL;
4605 return hres;
4608 static HRESULT WINAPI Uri_GetPropertyDWORD(IUri *iface, Uri_PROPERTY uriProp, DWORD *pcchProperty, DWORD dwFlags)
4610 Uri *This = impl_from_IUri(iface);
4611 HRESULT hres;
4613 TRACE("(%p)->(%d %p %x)\n", This, uriProp, pcchProperty, dwFlags);
4615 if(!pcchProperty)
4616 return E_INVALIDARG;
4618 /* Microsoft's implementation for the ZONE property of a URI seems to be lacking...
4619 * From what I can tell, instead of checking which URLZONE the URI belongs to it
4620 * simply assigns URLZONE_INVALID and returns E_NOTIMPL. This also applies to the GetZone
4621 * function.
4623 if(uriProp == Uri_PROPERTY_ZONE) {
4624 *pcchProperty = URLZONE_INVALID;
4625 return E_NOTIMPL;
4628 if(uriProp < Uri_PROPERTY_DWORD_START) {
4629 *pcchProperty = 0;
4630 return E_INVALIDARG;
4633 switch(uriProp) {
4634 case Uri_PROPERTY_HOST_TYPE:
4635 *pcchProperty = This->host_type;
4636 hres = S_OK;
4637 break;
4638 case Uri_PROPERTY_PORT:
4639 if(!This->has_port) {
4640 *pcchProperty = 0;
4641 hres = S_FALSE;
4642 } else {
4643 *pcchProperty = This->port;
4644 hres = S_OK;
4647 break;
4648 case Uri_PROPERTY_SCHEME:
4649 *pcchProperty = This->scheme_type;
4650 hres = S_OK;
4651 break;
4652 default:
4653 FIXME("(%p)->(%d %p %x)\n", This, uriProp, pcchProperty, dwFlags);
4654 hres = E_NOTIMPL;
4657 return hres;
4660 static HRESULT WINAPI Uri_HasProperty(IUri *iface, Uri_PROPERTY uriProp, BOOL *pfHasProperty)
4662 Uri *This = impl_from_IUri(iface);
4663 TRACE("(%p)->(%d %p)\n", This, uriProp, pfHasProperty);
4665 if(!pfHasProperty)
4666 return E_INVALIDARG;
4668 switch(uriProp) {
4669 case Uri_PROPERTY_ABSOLUTE_URI:
4670 *pfHasProperty = !(This->display_modifiers & URI_DISPLAY_NO_ABSOLUTE_URI);
4671 break;
4672 case Uri_PROPERTY_AUTHORITY:
4673 *pfHasProperty = This->authority_start > -1;
4674 break;
4675 case Uri_PROPERTY_DISPLAY_URI:
4676 *pfHasProperty = TRUE;
4677 break;
4678 case Uri_PROPERTY_DOMAIN:
4679 *pfHasProperty = This->domain_offset > -1;
4680 break;
4681 case Uri_PROPERTY_EXTENSION:
4682 *pfHasProperty = This->extension_offset > -1;
4683 break;
4684 case Uri_PROPERTY_FRAGMENT:
4685 *pfHasProperty = This->fragment_start > -1;
4686 break;
4687 case Uri_PROPERTY_HOST:
4688 *pfHasProperty = This->host_start > -1;
4689 break;
4690 case Uri_PROPERTY_PASSWORD:
4691 *pfHasProperty = This->userinfo_split > -1;
4692 break;
4693 case Uri_PROPERTY_PATH:
4694 *pfHasProperty = This->path_start > -1;
4695 break;
4696 case Uri_PROPERTY_PATH_AND_QUERY:
4697 *pfHasProperty = (This->path_start > -1 || This->query_start > -1);
4698 break;
4699 case Uri_PROPERTY_QUERY:
4700 *pfHasProperty = This->query_start > -1;
4701 break;
4702 case Uri_PROPERTY_RAW_URI:
4703 *pfHasProperty = TRUE;
4704 break;
4705 case Uri_PROPERTY_SCHEME_NAME:
4706 *pfHasProperty = This->scheme_start > -1;
4707 break;
4708 case Uri_PROPERTY_USER_INFO:
4709 *pfHasProperty = This->userinfo_start > -1;
4710 break;
4711 case Uri_PROPERTY_USER_NAME:
4712 if(This->userinfo_split == 0)
4713 *pfHasProperty = FALSE;
4714 else
4715 *pfHasProperty = This->userinfo_start > -1;
4716 break;
4717 case Uri_PROPERTY_HOST_TYPE:
4718 *pfHasProperty = TRUE;
4719 break;
4720 case Uri_PROPERTY_PORT:
4721 *pfHasProperty = This->has_port;
4722 break;
4723 case Uri_PROPERTY_SCHEME:
4724 *pfHasProperty = TRUE;
4725 break;
4726 case Uri_PROPERTY_ZONE:
4727 *pfHasProperty = FALSE;
4728 break;
4729 default:
4730 FIXME("(%p)->(%d %p): Unsupported property type.\n", This, uriProp, pfHasProperty);
4731 return E_NOTIMPL;
4734 return S_OK;
4737 static HRESULT WINAPI Uri_GetAbsoluteUri(IUri *iface, BSTR *pstrAbsoluteUri)
4739 TRACE("(%p)->(%p)\n", iface, pstrAbsoluteUri);
4740 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_ABSOLUTE_URI, pstrAbsoluteUri, 0);
4743 static HRESULT WINAPI Uri_GetAuthority(IUri *iface, BSTR *pstrAuthority)
4745 TRACE("(%p)->(%p)\n", iface, pstrAuthority);
4746 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_AUTHORITY, pstrAuthority, 0);
4749 static HRESULT WINAPI Uri_GetDisplayUri(IUri *iface, BSTR *pstrDisplayUri)
4751 TRACE("(%p)->(%p)\n", iface, pstrDisplayUri);
4752 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_DISPLAY_URI, pstrDisplayUri, 0);
4755 static HRESULT WINAPI Uri_GetDomain(IUri *iface, BSTR *pstrDomain)
4757 TRACE("(%p)->(%p)\n", iface, pstrDomain);
4758 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_DOMAIN, pstrDomain, 0);
4761 static HRESULT WINAPI Uri_GetExtension(IUri *iface, BSTR *pstrExtension)
4763 TRACE("(%p)->(%p)\n", iface, pstrExtension);
4764 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_EXTENSION, pstrExtension, 0);
4767 static HRESULT WINAPI Uri_GetFragment(IUri *iface, BSTR *pstrFragment)
4769 TRACE("(%p)->(%p)\n", iface, pstrFragment);
4770 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_FRAGMENT, pstrFragment, 0);
4773 static HRESULT WINAPI Uri_GetHost(IUri *iface, BSTR *pstrHost)
4775 TRACE("(%p)->(%p)\n", iface, pstrHost);
4776 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_HOST, pstrHost, 0);
4779 static HRESULT WINAPI Uri_GetPassword(IUri *iface, BSTR *pstrPassword)
4781 TRACE("(%p)->(%p)\n", iface, pstrPassword);
4782 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_PASSWORD, pstrPassword, 0);
4785 static HRESULT WINAPI Uri_GetPath(IUri *iface, BSTR *pstrPath)
4787 TRACE("(%p)->(%p)\n", iface, pstrPath);
4788 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_PATH, pstrPath, 0);
4791 static HRESULT WINAPI Uri_GetPathAndQuery(IUri *iface, BSTR *pstrPathAndQuery)
4793 TRACE("(%p)->(%p)\n", iface, pstrPathAndQuery);
4794 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_PATH_AND_QUERY, pstrPathAndQuery, 0);
4797 static HRESULT WINAPI Uri_GetQuery(IUri *iface, BSTR *pstrQuery)
4799 TRACE("(%p)->(%p)\n", iface, pstrQuery);
4800 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_QUERY, pstrQuery, 0);
4803 static HRESULT WINAPI Uri_GetRawUri(IUri *iface, BSTR *pstrRawUri)
4805 TRACE("(%p)->(%p)\n", iface, pstrRawUri);
4806 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_RAW_URI, pstrRawUri, 0);
4809 static HRESULT WINAPI Uri_GetSchemeName(IUri *iface, BSTR *pstrSchemeName)
4811 TRACE("(%p)->(%p)\n", iface, pstrSchemeName);
4812 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_SCHEME_NAME, pstrSchemeName, 0);
4815 static HRESULT WINAPI Uri_GetUserInfo(IUri *iface, BSTR *pstrUserInfo)
4817 TRACE("(%p)->(%p)\n", iface, pstrUserInfo);
4818 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_USER_INFO, pstrUserInfo, 0);
4821 static HRESULT WINAPI Uri_GetUserName(IUri *iface, BSTR *pstrUserName)
4823 TRACE("(%p)->(%p)\n", iface, pstrUserName);
4824 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_USER_NAME, pstrUserName, 0);
4827 static HRESULT WINAPI Uri_GetHostType(IUri *iface, DWORD *pdwHostType)
4829 TRACE("(%p)->(%p)\n", iface, pdwHostType);
4830 return IUri_GetPropertyDWORD(iface, Uri_PROPERTY_HOST_TYPE, pdwHostType, 0);
4833 static HRESULT WINAPI Uri_GetPort(IUri *iface, DWORD *pdwPort)
4835 TRACE("(%p)->(%p)\n", iface, pdwPort);
4836 return IUri_GetPropertyDWORD(iface, Uri_PROPERTY_PORT, pdwPort, 0);
4839 static HRESULT WINAPI Uri_GetScheme(IUri *iface, DWORD *pdwScheme)
4841 TRACE("(%p)->(%p)\n", iface, pdwScheme);
4842 return IUri_GetPropertyDWORD(iface, Uri_PROPERTY_SCHEME, pdwScheme, 0);
4845 static HRESULT WINAPI Uri_GetZone(IUri *iface, DWORD *pdwZone)
4847 TRACE("(%p)->(%p)\n", iface, pdwZone);
4848 return IUri_GetPropertyDWORD(iface, Uri_PROPERTY_ZONE,pdwZone, 0);
4851 static HRESULT WINAPI Uri_GetProperties(IUri *iface, DWORD *pdwProperties)
4853 Uri *This = impl_from_IUri(iface);
4854 TRACE("(%p)->(%p)\n", This, pdwProperties);
4856 if(!pdwProperties)
4857 return E_INVALIDARG;
4859 /* All URIs have these. */
4860 *pdwProperties = Uri_HAS_DISPLAY_URI|Uri_HAS_RAW_URI|Uri_HAS_SCHEME|Uri_HAS_HOST_TYPE;
4862 if(!(This->display_modifiers & URI_DISPLAY_NO_ABSOLUTE_URI))
4863 *pdwProperties |= Uri_HAS_ABSOLUTE_URI;
4865 if(This->scheme_start > -1)
4866 *pdwProperties |= Uri_HAS_SCHEME_NAME;
4868 if(This->authority_start > -1) {
4869 *pdwProperties |= Uri_HAS_AUTHORITY;
4870 if(This->userinfo_start > -1) {
4871 *pdwProperties |= Uri_HAS_USER_INFO;
4872 if(This->userinfo_split != 0)
4873 *pdwProperties |= Uri_HAS_USER_NAME;
4875 if(This->userinfo_split > -1)
4876 *pdwProperties |= Uri_HAS_PASSWORD;
4877 if(This->host_start > -1)
4878 *pdwProperties |= Uri_HAS_HOST;
4879 if(This->domain_offset > -1)
4880 *pdwProperties |= Uri_HAS_DOMAIN;
4883 if(This->has_port)
4884 *pdwProperties |= Uri_HAS_PORT;
4885 if(This->path_start > -1)
4886 *pdwProperties |= Uri_HAS_PATH|Uri_HAS_PATH_AND_QUERY;
4887 if(This->query_start > -1)
4888 *pdwProperties |= Uri_HAS_QUERY|Uri_HAS_PATH_AND_QUERY;
4890 if(This->extension_offset > -1)
4891 *pdwProperties |= Uri_HAS_EXTENSION;
4893 if(This->fragment_start > -1)
4894 *pdwProperties |= Uri_HAS_FRAGMENT;
4896 return S_OK;
4899 static HRESULT WINAPI Uri_IsEqual(IUri *iface, IUri *pUri, BOOL *pfEqual)
4901 Uri *This = impl_from_IUri(iface);
4902 Uri *other;
4904 TRACE("(%p)->(%p %p)\n", This, pUri, pfEqual);
4906 if(!pfEqual)
4907 return E_POINTER;
4909 if(!pUri) {
4910 *pfEqual = FALSE;
4912 /* For some reason Windows returns S_OK here... */
4913 return S_OK;
4916 /* Try to convert it to a Uri (allows for a more simple comparison). */
4917 if((other = get_uri_obj(pUri)))
4918 *pfEqual = are_equal_simple(This, other);
4919 else {
4920 /* Do it the hard way. */
4921 FIXME("(%p)->(%p %p) No support for unknown IUri's yet.\n", iface, pUri, pfEqual);
4922 return E_NOTIMPL;
4925 return S_OK;
4928 static const IUriVtbl UriVtbl = {
4929 Uri_QueryInterface,
4930 Uri_AddRef,
4931 Uri_Release,
4932 Uri_GetPropertyBSTR,
4933 Uri_GetPropertyLength,
4934 Uri_GetPropertyDWORD,
4935 Uri_HasProperty,
4936 Uri_GetAbsoluteUri,
4937 Uri_GetAuthority,
4938 Uri_GetDisplayUri,
4939 Uri_GetDomain,
4940 Uri_GetExtension,
4941 Uri_GetFragment,
4942 Uri_GetHost,
4943 Uri_GetPassword,
4944 Uri_GetPath,
4945 Uri_GetPathAndQuery,
4946 Uri_GetQuery,
4947 Uri_GetRawUri,
4948 Uri_GetSchemeName,
4949 Uri_GetUserInfo,
4950 Uri_GetUserName,
4951 Uri_GetHostType,
4952 Uri_GetPort,
4953 Uri_GetScheme,
4954 Uri_GetZone,
4955 Uri_GetProperties,
4956 Uri_IsEqual
4959 static inline Uri* impl_from_IUriBuilderFactory(IUriBuilderFactory *iface)
4961 return CONTAINING_RECORD(iface, Uri, IUriBuilderFactory_iface);
4964 static HRESULT WINAPI UriBuilderFactory_QueryInterface(IUriBuilderFactory *iface, REFIID riid, void **ppv)
4966 Uri *This = impl_from_IUriBuilderFactory(iface);
4968 if(IsEqualGUID(&IID_IUnknown, riid)) {
4969 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
4970 *ppv = &This->IUriBuilderFactory_iface;
4971 }else if(IsEqualGUID(&IID_IUriBuilderFactory, riid)) {
4972 TRACE("(%p)->(IID_IUriBuilderFactory %p)\n", This, ppv);
4973 *ppv = &This->IUriBuilderFactory_iface;
4974 }else if(IsEqualGUID(&IID_IUri, riid)) {
4975 TRACE("(%p)->(IID_IUri %p)\n", This, ppv);
4976 *ppv = &This->IUri_iface;
4977 }else {
4978 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
4979 *ppv = NULL;
4980 return E_NOINTERFACE;
4983 IUnknown_AddRef((IUnknown*)*ppv);
4984 return S_OK;
4987 static ULONG WINAPI UriBuilderFactory_AddRef(IUriBuilderFactory *iface)
4989 Uri *This = impl_from_IUriBuilderFactory(iface);
4990 LONG ref = InterlockedIncrement(&This->ref);
4992 TRACE("(%p) ref=%d\n", This, ref);
4994 return ref;
4997 static ULONG WINAPI UriBuilderFactory_Release(IUriBuilderFactory *iface)
4999 Uri *This = impl_from_IUriBuilderFactory(iface);
5000 LONG ref = InterlockedDecrement(&This->ref);
5002 TRACE("(%p) ref=%d\n", This, ref);
5004 if(!ref)
5005 destory_uri_obj(This);
5007 return ref;
5010 static HRESULT WINAPI UriBuilderFactory_CreateIUriBuilder(IUriBuilderFactory *iface,
5011 DWORD dwFlags,
5012 DWORD_PTR dwReserved,
5013 IUriBuilder **ppIUriBuilder)
5015 Uri *This = impl_from_IUriBuilderFactory(iface);
5016 TRACE("(%p)->(%08x %08x %p)\n", This, dwFlags, (DWORD)dwReserved, ppIUriBuilder);
5018 if(!ppIUriBuilder)
5019 return E_POINTER;
5021 if(dwFlags || dwReserved) {
5022 *ppIUriBuilder = NULL;
5023 return E_INVALIDARG;
5026 return CreateIUriBuilder(NULL, 0, 0, ppIUriBuilder);
5029 static HRESULT WINAPI UriBuilderFactory_CreateInitializedIUriBuilder(IUriBuilderFactory *iface,
5030 DWORD dwFlags,
5031 DWORD_PTR dwReserved,
5032 IUriBuilder **ppIUriBuilder)
5034 Uri *This = impl_from_IUriBuilderFactory(iface);
5035 TRACE("(%p)->(%08x %08x %p)\n", This, dwFlags, (DWORD)dwReserved, ppIUriBuilder);
5037 if(!ppIUriBuilder)
5038 return E_POINTER;
5040 if(dwFlags || dwReserved) {
5041 *ppIUriBuilder = NULL;
5042 return E_INVALIDARG;
5045 return CreateIUriBuilder(&This->IUri_iface, 0, 0, ppIUriBuilder);
5048 static const IUriBuilderFactoryVtbl UriBuilderFactoryVtbl = {
5049 UriBuilderFactory_QueryInterface,
5050 UriBuilderFactory_AddRef,
5051 UriBuilderFactory_Release,
5052 UriBuilderFactory_CreateIUriBuilder,
5053 UriBuilderFactory_CreateInitializedIUriBuilder
5056 static Uri* create_uri_obj(void) {
5057 Uri *ret = heap_alloc_zero(sizeof(Uri));
5058 if(ret) {
5059 ret->IUri_iface.lpVtbl = &UriVtbl;
5060 ret->IUriBuilderFactory_iface.lpVtbl = &UriBuilderFactoryVtbl;
5061 ret->ref = 1;
5064 return ret;
5067 /***********************************************************************
5068 * CreateUri (urlmon.@)
5070 * Creates a new IUri object using the URI represented by pwzURI. This function
5071 * parses and validates the components of pwzURI and then canonicalizes the
5072 * parsed components.
5074 * PARAMS
5075 * pwzURI [I] The URI to parse, validate, and canonicalize.
5076 * dwFlags [I] Flags which can affect how the parsing/canonicalization is performed.
5077 * dwReserved [I] Reserved (not used).
5078 * ppURI [O] The resulting IUri after parsing/canonicalization occurs.
5080 * RETURNS
5081 * Success: Returns S_OK. ppURI contains the pointer to the newly allocated IUri.
5082 * Failure: E_INVALIDARG if there's invalid flag combinations in dwFlags, or an
5083 * invalid parameters, or pwzURI doesn't represnt a valid URI.
5084 * E_OUTOFMEMORY if any memory allocation fails.
5086 * NOTES
5087 * Default flags:
5088 * Uri_CREATE_CANONICALIZE, Uri_CREATE_DECODE_EXTRA_INFO, Uri_CREATE_CRACK_UNKNOWN_SCHEMES,
5089 * Uri_CREATE_PRE_PROCESS_HTML_URI, Uri_CREATE_NO_IE_SETTINGS.
5091 HRESULT WINAPI CreateUri(LPCWSTR pwzURI, DWORD dwFlags, DWORD_PTR dwReserved, IUri **ppURI)
5093 const DWORD supported_flags = Uri_CREATE_ALLOW_RELATIVE|Uri_CREATE_ALLOW_IMPLICIT_WILDCARD_SCHEME|
5094 Uri_CREATE_ALLOW_IMPLICIT_FILE_SCHEME|Uri_CREATE_NO_CANONICALIZE|Uri_CREATE_CANONICALIZE|
5095 Uri_CREATE_DECODE_EXTRA_INFO|Uri_CREATE_NO_DECODE_EXTRA_INFO|Uri_CREATE_CRACK_UNKNOWN_SCHEMES|
5096 Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES|Uri_CREATE_PRE_PROCESS_HTML_URI|Uri_CREATE_NO_PRE_PROCESS_HTML_URI|
5097 Uri_CREATE_NO_IE_SETTINGS|Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS|Uri_CREATE_FILE_USE_DOS_PATH;
5098 Uri *ret;
5099 HRESULT hr;
5100 parse_data data;
5102 TRACE("(%s %x %x %p)\n", debugstr_w(pwzURI), dwFlags, (DWORD)dwReserved, ppURI);
5104 if(!ppURI)
5105 return E_INVALIDARG;
5107 if(!pwzURI || !*pwzURI) {
5108 *ppURI = NULL;
5109 return E_INVALIDARG;
5112 /* Check for invalid flags. */
5113 if(has_invalid_flag_combination(dwFlags)) {
5114 *ppURI = NULL;
5115 return E_INVALIDARG;
5118 /* Currently unsupported. */
5119 if(dwFlags & ~supported_flags)
5120 FIXME("Ignoring unsupported flag(s) %x\n", dwFlags & ~supported_flags);
5122 ret = create_uri_obj();
5123 if(!ret) {
5124 *ppURI = NULL;
5125 return E_OUTOFMEMORY;
5128 /* Explicitly set the default flags if it doesn't cause a flag conflict. */
5129 apply_default_flags(&dwFlags);
5131 /* Pre process the URI, unless told otherwise. */
5132 if(!(dwFlags & Uri_CREATE_NO_PRE_PROCESS_HTML_URI))
5133 ret->raw_uri = pre_process_uri(pwzURI);
5134 else
5135 ret->raw_uri = SysAllocString(pwzURI);
5137 if(!ret->raw_uri) {
5138 heap_free(ret);
5139 return E_OUTOFMEMORY;
5142 memset(&data, 0, sizeof(parse_data));
5143 data.uri = ret->raw_uri;
5145 /* Validate and parse the URI into it's components. */
5146 if(!parse_uri(&data, dwFlags)) {
5147 /* Encountered an unsupported or invalid URI */
5148 IUri_Release(&ret->IUri_iface);
5149 *ppURI = NULL;
5150 return E_INVALIDARG;
5153 /* Canonicalize the URI. */
5154 hr = canonicalize_uri(&data, ret, dwFlags);
5155 if(FAILED(hr)) {
5156 IUri_Release(&ret->IUri_iface);
5157 *ppURI = NULL;
5158 return hr;
5161 ret->create_flags = dwFlags;
5163 *ppURI = &ret->IUri_iface;
5164 return S_OK;
5167 /***********************************************************************
5168 * CreateUriWithFragment (urlmon.@)
5170 * Creates a new IUri object. This is almost the same as CreateUri, expect that
5171 * it allows you to explicitly specify a fragment (pwzFragment) for pwzURI.
5173 * PARAMS
5174 * pwzURI [I] The URI to parse and perform canonicalization on.
5175 * pwzFragment [I] The explict fragment string which should be added to pwzURI.
5176 * dwFlags [I] The flags which will be passed to CreateUri.
5177 * dwReserved [I] Reserved (not used).
5178 * ppURI [O] The resulting IUri after parsing/canonicalization.
5180 * RETURNS
5181 * Success: S_OK. ppURI contains the pointer to the newly allocated IUri.
5182 * Failure: E_INVALIDARG if pwzURI already contains a fragment and pwzFragment
5183 * isn't NULL. Will also return E_INVALIDARG for the same reasons as
5184 * CreateUri will. E_OUTOFMEMORY if any allocations fail.
5186 HRESULT WINAPI CreateUriWithFragment(LPCWSTR pwzURI, LPCWSTR pwzFragment, DWORD dwFlags,
5187 DWORD_PTR dwReserved, IUri **ppURI)
5189 HRESULT hres;
5190 TRACE("(%s %s %x %x %p)\n", debugstr_w(pwzURI), debugstr_w(pwzFragment), dwFlags, (DWORD)dwReserved, ppURI);
5192 if(!ppURI)
5193 return E_INVALIDARG;
5195 if(!pwzURI) {
5196 *ppURI = NULL;
5197 return E_INVALIDARG;
5200 /* Check if a fragment should be appended to the URI string. */
5201 if(pwzFragment) {
5202 WCHAR *uriW;
5203 DWORD uri_len, frag_len;
5204 BOOL add_pound;
5206 /* Check if the original URI already has a fragment component. */
5207 if(StrChrW(pwzURI, '#')) {
5208 *ppURI = NULL;
5209 return E_INVALIDARG;
5212 uri_len = lstrlenW(pwzURI);
5213 frag_len = lstrlenW(pwzFragment);
5215 /* If the fragment doesn't start with a '#', one will be added. */
5216 add_pound = *pwzFragment != '#';
5218 if(add_pound)
5219 uriW = heap_alloc((uri_len+frag_len+2)*sizeof(WCHAR));
5220 else
5221 uriW = heap_alloc((uri_len+frag_len+1)*sizeof(WCHAR));
5223 if(!uriW)
5224 return E_OUTOFMEMORY;
5226 memcpy(uriW, pwzURI, uri_len*sizeof(WCHAR));
5227 if(add_pound)
5228 uriW[uri_len++] = '#';
5229 memcpy(uriW+uri_len, pwzFragment, (frag_len+1)*sizeof(WCHAR));
5231 hres = CreateUri(uriW, dwFlags, 0, ppURI);
5233 heap_free(uriW);
5234 } else
5235 /* A fragment string wasn't specified, so just forward the call. */
5236 hres = CreateUri(pwzURI, dwFlags, 0, ppURI);
5238 return hres;
5241 static HRESULT build_uri(const UriBuilder *builder, IUri **uri, DWORD create_flags,
5242 DWORD use_orig_flags, DWORD encoding_mask)
5244 HRESULT hr;
5245 parse_data data;
5246 Uri *ret;
5248 if(!uri)
5249 return E_POINTER;
5251 if(encoding_mask && (!builder->uri || builder->modified_props)) {
5252 *uri = NULL;
5253 return E_NOTIMPL;
5256 /* Decide what flags should be used when creating the Uri. */
5257 if((use_orig_flags & UriBuilder_USE_ORIGINAL_FLAGS) && builder->uri)
5258 create_flags = builder->uri->create_flags;
5259 else {
5260 if(has_invalid_flag_combination(create_flags)) {
5261 *uri = NULL;
5262 return E_INVALIDARG;
5265 /* Set the default flags if they don't cause a conflict. */
5266 apply_default_flags(&create_flags);
5269 /* Return the base IUri if no changes have been made and the create_flags match. */
5270 if(builder->uri && !builder->modified_props && builder->uri->create_flags == create_flags) {
5271 *uri = &builder->uri->IUri_iface;
5272 IUri_AddRef(*uri);
5273 return S_OK;
5276 hr = validate_components(builder, &data, create_flags);
5277 if(FAILED(hr)) {
5278 *uri = NULL;
5279 return hr;
5282 ret = create_uri_obj();
5283 if(!ret) {
5284 *uri = NULL;
5285 return E_OUTOFMEMORY;
5288 hr = generate_uri(builder, &data, ret, create_flags);
5289 if(FAILED(hr)) {
5290 IUri_Release(&ret->IUri_iface);
5291 *uri = NULL;
5292 return hr;
5295 *uri = &ret->IUri_iface;
5296 return S_OK;
5299 static inline UriBuilder* impl_from_IUriBuilder(IUriBuilder *iface)
5301 return CONTAINING_RECORD(iface, UriBuilder, IUriBuilder_iface);
5304 static HRESULT WINAPI UriBuilder_QueryInterface(IUriBuilder *iface, REFIID riid, void **ppv)
5306 UriBuilder *This = impl_from_IUriBuilder(iface);
5308 if(IsEqualGUID(&IID_IUnknown, riid)) {
5309 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
5310 *ppv = &This->IUriBuilder_iface;
5311 }else if(IsEqualGUID(&IID_IUriBuilder, riid)) {
5312 TRACE("(%p)->(IID_IUriBuilder %p)\n", This, ppv);
5313 *ppv = &This->IUriBuilder_iface;
5314 }else {
5315 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
5316 *ppv = NULL;
5317 return E_NOINTERFACE;
5320 IUnknown_AddRef((IUnknown*)*ppv);
5321 return S_OK;
5324 static ULONG WINAPI UriBuilder_AddRef(IUriBuilder *iface)
5326 UriBuilder *This = impl_from_IUriBuilder(iface);
5327 LONG ref = InterlockedIncrement(&This->ref);
5329 TRACE("(%p) ref=%d\n", This, ref);
5331 return ref;
5334 static ULONG WINAPI UriBuilder_Release(IUriBuilder *iface)
5336 UriBuilder *This = impl_from_IUriBuilder(iface);
5337 LONG ref = InterlockedDecrement(&This->ref);
5339 TRACE("(%p) ref=%d\n", This, ref);
5341 if(!ref) {
5342 if(This->uri) IUri_Release(&This->uri->IUri_iface);
5343 heap_free(This->fragment);
5344 heap_free(This->host);
5345 heap_free(This->password);
5346 heap_free(This->path);
5347 heap_free(This->query);
5348 heap_free(This->scheme);
5349 heap_free(This->username);
5350 heap_free(This);
5353 return ref;
5356 static HRESULT WINAPI UriBuilder_CreateUriSimple(IUriBuilder *iface,
5357 DWORD dwAllowEncodingPropertyMask,
5358 DWORD_PTR dwReserved,
5359 IUri **ppIUri)
5361 UriBuilder *This = impl_from_IUriBuilder(iface);
5362 HRESULT hr;
5363 TRACE("(%p)->(%d %d %p)\n", This, dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
5365 hr = build_uri(This, ppIUri, 0, UriBuilder_USE_ORIGINAL_FLAGS, dwAllowEncodingPropertyMask);
5366 if(hr == E_NOTIMPL)
5367 FIXME("(%p)->(%d %d %p)\n", This, dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
5368 return hr;
5371 static HRESULT WINAPI UriBuilder_CreateUri(IUriBuilder *iface,
5372 DWORD dwCreateFlags,
5373 DWORD dwAllowEncodingPropertyMask,
5374 DWORD_PTR dwReserved,
5375 IUri **ppIUri)
5377 UriBuilder *This = impl_from_IUriBuilder(iface);
5378 HRESULT hr;
5379 TRACE("(%p)->(0x%08x %d %d %p)\n", This, dwCreateFlags, dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
5381 if(dwCreateFlags == -1)
5382 hr = build_uri(This, ppIUri, 0, UriBuilder_USE_ORIGINAL_FLAGS, dwAllowEncodingPropertyMask);
5383 else
5384 hr = build_uri(This, ppIUri, dwCreateFlags, 0, dwAllowEncodingPropertyMask);
5386 if(hr == E_NOTIMPL)
5387 FIXME("(%p)->(0x%08x %d %d %p)\n", This, dwCreateFlags, dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
5388 return hr;
5391 static HRESULT WINAPI UriBuilder_CreateUriWithFlags(IUriBuilder *iface,
5392 DWORD dwCreateFlags,
5393 DWORD dwUriBuilderFlags,
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 0x%08x %d %d %p)\n", This, dwCreateFlags, dwUriBuilderFlags,
5401 dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
5403 hr = build_uri(This, ppIUri, dwCreateFlags, dwUriBuilderFlags, dwAllowEncodingPropertyMask);
5404 if(hr == E_NOTIMPL)
5405 FIXME("(%p)->(0x%08x 0x%08x %d %d %p)\n", This, dwCreateFlags, dwUriBuilderFlags,
5406 dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
5407 return hr;
5410 static HRESULT WINAPI UriBuilder_GetIUri(IUriBuilder *iface, IUri **ppIUri)
5412 UriBuilder *This = impl_from_IUriBuilder(iface);
5413 TRACE("(%p)->(%p)\n", This, ppIUri);
5415 if(!ppIUri)
5416 return E_POINTER;
5418 if(This->uri) {
5419 IUri *uri = &This->uri->IUri_iface;
5420 IUri_AddRef(uri);
5421 *ppIUri = uri;
5422 } else
5423 *ppIUri = NULL;
5425 return S_OK;
5428 static HRESULT WINAPI UriBuilder_SetIUri(IUriBuilder *iface, IUri *pIUri)
5430 UriBuilder *This = impl_from_IUriBuilder(iface);
5431 TRACE("(%p)->(%p)\n", This, pIUri);
5433 if(pIUri) {
5434 Uri *uri;
5436 if((uri = get_uri_obj(pIUri))) {
5437 /* Only reset the builder if it's Uri isn't the same as
5438 * the Uri passed to the function.
5440 if(This->uri != uri) {
5441 reset_builder(This);
5443 This->uri = uri;
5444 if(uri->has_port)
5445 This->port = uri->port;
5447 IUri_AddRef(pIUri);
5449 } else {
5450 FIXME("(%p)->(%p) Unknown IUri types not supported yet.\n", This, pIUri);
5451 return E_NOTIMPL;
5453 } else if(This->uri)
5454 /* Only reset the builder if it's Uri isn't NULL. */
5455 reset_builder(This);
5457 return S_OK;
5460 static HRESULT WINAPI UriBuilder_GetFragment(IUriBuilder *iface, DWORD *pcchFragment, LPCWSTR *ppwzFragment)
5462 UriBuilder *This = impl_from_IUriBuilder(iface);
5463 TRACE("(%p)->(%p %p)\n", This, pcchFragment, ppwzFragment);
5465 if(!This->uri || This->uri->fragment_start == -1 || This->modified_props & Uri_HAS_FRAGMENT)
5466 return get_builder_component(&This->fragment, &This->fragment_len, NULL, 0, ppwzFragment, pcchFragment);
5467 else
5468 return get_builder_component(&This->fragment, &This->fragment_len, This->uri->canon_uri+This->uri->fragment_start,
5469 This->uri->fragment_len, ppwzFragment, pcchFragment);
5472 static HRESULT WINAPI UriBuilder_GetHost(IUriBuilder *iface, DWORD *pcchHost, LPCWSTR *ppwzHost)
5474 UriBuilder *This = impl_from_IUriBuilder(iface);
5475 TRACE("(%p)->(%p %p)\n", This, pcchHost, ppwzHost);
5477 if(!This->uri || This->uri->host_start == -1 || This->modified_props & Uri_HAS_HOST)
5478 return get_builder_component(&This->host, &This->host_len, NULL, 0, ppwzHost, pcchHost);
5479 else {
5480 if(This->uri->host_type == Uri_HOST_IPV6)
5481 /* Don't include the '[' and ']' around the address. */
5482 return get_builder_component(&This->host, &This->host_len, This->uri->canon_uri+This->uri->host_start+1,
5483 This->uri->host_len-2, ppwzHost, pcchHost);
5484 else
5485 return get_builder_component(&This->host, &This->host_len, This->uri->canon_uri+This->uri->host_start,
5486 This->uri->host_len, ppwzHost, pcchHost);
5490 static HRESULT WINAPI UriBuilder_GetPassword(IUriBuilder *iface, DWORD *pcchPassword, LPCWSTR *ppwzPassword)
5492 UriBuilder *This = impl_from_IUriBuilder(iface);
5493 TRACE("(%p)->(%p %p)\n", This, pcchPassword, ppwzPassword);
5495 if(!This->uri || This->uri->userinfo_split == -1 || This->modified_props & Uri_HAS_PASSWORD)
5496 return get_builder_component(&This->password, &This->password_len, NULL, 0, ppwzPassword, pcchPassword);
5497 else {
5498 const WCHAR *start = This->uri->canon_uri+This->uri->userinfo_start+This->uri->userinfo_split+1;
5499 DWORD len = This->uri->userinfo_len-This->uri->userinfo_split-1;
5500 return get_builder_component(&This->password, &This->password_len, start, len, ppwzPassword, pcchPassword);
5504 static HRESULT WINAPI UriBuilder_GetPath(IUriBuilder *iface, DWORD *pcchPath, LPCWSTR *ppwzPath)
5506 UriBuilder *This = impl_from_IUriBuilder(iface);
5507 TRACE("(%p)->(%p %p)\n", This, pcchPath, ppwzPath);
5509 if(!This->uri || This->uri->path_start == -1 || This->modified_props & Uri_HAS_PATH)
5510 return get_builder_component(&This->path, &This->path_len, NULL, 0, ppwzPath, pcchPath);
5511 else
5512 return get_builder_component(&This->path, &This->path_len, This->uri->canon_uri+This->uri->path_start,
5513 This->uri->path_len, ppwzPath, pcchPath);
5516 static HRESULT WINAPI UriBuilder_GetPort(IUriBuilder *iface, BOOL *pfHasPort, DWORD *pdwPort)
5518 UriBuilder *This = impl_from_IUriBuilder(iface);
5519 TRACE("(%p)->(%p %p)\n", This, pfHasPort, pdwPort);
5521 if(!pfHasPort) {
5522 if(pdwPort)
5523 *pdwPort = 0;
5524 return E_POINTER;
5527 if(!pdwPort) {
5528 *pfHasPort = FALSE;
5529 return E_POINTER;
5532 *pfHasPort = This->has_port;
5533 *pdwPort = This->port;
5534 return S_OK;
5537 static HRESULT WINAPI UriBuilder_GetQuery(IUriBuilder *iface, DWORD *pcchQuery, LPCWSTR *ppwzQuery)
5539 UriBuilder *This = impl_from_IUriBuilder(iface);
5540 TRACE("(%p)->(%p %p)\n", This, pcchQuery, ppwzQuery);
5542 if(!This->uri || This->uri->query_start == -1 || This->modified_props & Uri_HAS_QUERY)
5543 return get_builder_component(&This->query, &This->query_len, NULL, 0, ppwzQuery, pcchQuery);
5544 else
5545 return get_builder_component(&This->query, &This->query_len, This->uri->canon_uri+This->uri->query_start,
5546 This->uri->query_len, ppwzQuery, pcchQuery);
5549 static HRESULT WINAPI UriBuilder_GetSchemeName(IUriBuilder *iface, DWORD *pcchSchemeName, LPCWSTR *ppwzSchemeName)
5551 UriBuilder *This = impl_from_IUriBuilder(iface);
5552 TRACE("(%p)->(%p %p)\n", This, pcchSchemeName, ppwzSchemeName);
5554 if(!This->uri || This->uri->scheme_start == -1 || This->modified_props & Uri_HAS_SCHEME_NAME)
5555 return get_builder_component(&This->scheme, &This->scheme_len, NULL, 0, ppwzSchemeName, pcchSchemeName);
5556 else
5557 return get_builder_component(&This->scheme, &This->scheme_len, This->uri->canon_uri+This->uri->scheme_start,
5558 This->uri->scheme_len, ppwzSchemeName, pcchSchemeName);
5561 static HRESULT WINAPI UriBuilder_GetUserName(IUriBuilder *iface, DWORD *pcchUserName, LPCWSTR *ppwzUserName)
5563 UriBuilder *This = impl_from_IUriBuilder(iface);
5564 TRACE("(%p)->(%p %p)\n", This, pcchUserName, ppwzUserName);
5566 if(!This->uri || This->uri->userinfo_start == -1 || This->uri->userinfo_split == 0 ||
5567 This->modified_props & Uri_HAS_USER_NAME)
5568 return get_builder_component(&This->username, &This->username_len, NULL, 0, ppwzUserName, pcchUserName);
5569 else {
5570 const WCHAR *start = This->uri->canon_uri+This->uri->userinfo_start;
5572 /* Check if there's a password in the userinfo section. */
5573 if(This->uri->userinfo_split > -1)
5574 /* Don't include the password. */
5575 return get_builder_component(&This->username, &This->username_len, start,
5576 This->uri->userinfo_split, ppwzUserName, pcchUserName);
5577 else
5578 return get_builder_component(&This->username, &This->username_len, start,
5579 This->uri->userinfo_len, ppwzUserName, pcchUserName);
5583 static HRESULT WINAPI UriBuilder_SetFragment(IUriBuilder *iface, LPCWSTR pwzNewValue)
5585 UriBuilder *This = impl_from_IUriBuilder(iface);
5586 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
5587 return set_builder_component(&This->fragment, &This->fragment_len, pwzNewValue, '#',
5588 &This->modified_props, Uri_HAS_FRAGMENT);
5591 static HRESULT WINAPI UriBuilder_SetHost(IUriBuilder *iface, LPCWSTR pwzNewValue)
5593 UriBuilder *This = impl_from_IUriBuilder(iface);
5594 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
5596 /* Host name can't be set to NULL. */
5597 if(!pwzNewValue)
5598 return E_INVALIDARG;
5600 return set_builder_component(&This->host, &This->host_len, pwzNewValue, 0,
5601 &This->modified_props, Uri_HAS_HOST);
5604 static HRESULT WINAPI UriBuilder_SetPassword(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->password, &This->password_len, pwzNewValue, 0,
5609 &This->modified_props, Uri_HAS_PASSWORD);
5612 static HRESULT WINAPI UriBuilder_SetPath(IUriBuilder *iface, LPCWSTR pwzNewValue)
5614 UriBuilder *This = impl_from_IUriBuilder(iface);
5615 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
5616 return set_builder_component(&This->path, &This->path_len, pwzNewValue, 0,
5617 &This->modified_props, Uri_HAS_PATH);
5620 static HRESULT WINAPI UriBuilder_SetPort(IUriBuilder *iface, BOOL fHasPort, DWORD dwNewValue)
5622 UriBuilder *This = impl_from_IUriBuilder(iface);
5623 TRACE("(%p)->(%d %d)\n", This, fHasPort, dwNewValue);
5625 This->has_port = fHasPort;
5626 This->port = dwNewValue;
5627 This->modified_props |= Uri_HAS_PORT;
5628 return S_OK;
5631 static HRESULT WINAPI UriBuilder_SetQuery(IUriBuilder *iface, LPCWSTR pwzNewValue)
5633 UriBuilder *This = impl_from_IUriBuilder(iface);
5634 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
5635 return set_builder_component(&This->query, &This->query_len, pwzNewValue, '?',
5636 &This->modified_props, Uri_HAS_QUERY);
5639 static HRESULT WINAPI UriBuilder_SetSchemeName(IUriBuilder *iface, LPCWSTR pwzNewValue)
5641 UriBuilder *This = impl_from_IUriBuilder(iface);
5642 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
5644 /* Only set the scheme name if it's not NULL or empty. */
5645 if(!pwzNewValue || !*pwzNewValue)
5646 return E_INVALIDARG;
5648 return set_builder_component(&This->scheme, &This->scheme_len, pwzNewValue, 0,
5649 &This->modified_props, Uri_HAS_SCHEME_NAME);
5652 static HRESULT WINAPI UriBuilder_SetUserName(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->username, &This->username_len, pwzNewValue, 0,
5657 &This->modified_props, Uri_HAS_USER_NAME);
5660 static HRESULT WINAPI UriBuilder_RemoveProperties(IUriBuilder *iface, DWORD dwPropertyMask)
5662 const DWORD accepted_flags = Uri_HAS_AUTHORITY|Uri_HAS_DOMAIN|Uri_HAS_EXTENSION|Uri_HAS_FRAGMENT|Uri_HAS_HOST|
5663 Uri_HAS_PASSWORD|Uri_HAS_PATH|Uri_HAS_PATH_AND_QUERY|Uri_HAS_QUERY|
5664 Uri_HAS_USER_INFO|Uri_HAS_USER_NAME;
5666 UriBuilder *This = impl_from_IUriBuilder(iface);
5667 TRACE("(%p)->(0x%08x)\n", This, dwPropertyMask);
5669 if(dwPropertyMask & ~accepted_flags)
5670 return E_INVALIDARG;
5672 if(dwPropertyMask & Uri_HAS_FRAGMENT)
5673 UriBuilder_SetFragment(iface, NULL);
5675 /* Even though you can't set the host name to NULL or an
5676 * empty string, you can still remove it... for some reason.
5678 if(dwPropertyMask & Uri_HAS_HOST)
5679 set_builder_component(&This->host, &This->host_len, NULL, 0,
5680 &This->modified_props, Uri_HAS_HOST);
5682 if(dwPropertyMask & Uri_HAS_PASSWORD)
5683 UriBuilder_SetPassword(iface, NULL);
5685 if(dwPropertyMask & Uri_HAS_PATH)
5686 UriBuilder_SetPath(iface, NULL);
5688 if(dwPropertyMask & Uri_HAS_PORT)
5689 UriBuilder_SetPort(iface, FALSE, 0);
5691 if(dwPropertyMask & Uri_HAS_QUERY)
5692 UriBuilder_SetQuery(iface, NULL);
5694 if(dwPropertyMask & Uri_HAS_USER_NAME)
5695 UriBuilder_SetUserName(iface, NULL);
5697 return S_OK;
5700 static HRESULT WINAPI UriBuilder_HasBeenModified(IUriBuilder *iface, BOOL *pfModified)
5702 UriBuilder *This = impl_from_IUriBuilder(iface);
5703 TRACE("(%p)->(%p)\n", This, pfModified);
5705 if(!pfModified)
5706 return E_POINTER;
5708 *pfModified = This->modified_props > 0;
5709 return S_OK;
5712 static const IUriBuilderVtbl UriBuilderVtbl = {
5713 UriBuilder_QueryInterface,
5714 UriBuilder_AddRef,
5715 UriBuilder_Release,
5716 UriBuilder_CreateUriSimple,
5717 UriBuilder_CreateUri,
5718 UriBuilder_CreateUriWithFlags,
5719 UriBuilder_GetIUri,
5720 UriBuilder_SetIUri,
5721 UriBuilder_GetFragment,
5722 UriBuilder_GetHost,
5723 UriBuilder_GetPassword,
5724 UriBuilder_GetPath,
5725 UriBuilder_GetPort,
5726 UriBuilder_GetQuery,
5727 UriBuilder_GetSchemeName,
5728 UriBuilder_GetUserName,
5729 UriBuilder_SetFragment,
5730 UriBuilder_SetHost,
5731 UriBuilder_SetPassword,
5732 UriBuilder_SetPath,
5733 UriBuilder_SetPort,
5734 UriBuilder_SetQuery,
5735 UriBuilder_SetSchemeName,
5736 UriBuilder_SetUserName,
5737 UriBuilder_RemoveProperties,
5738 UriBuilder_HasBeenModified,
5741 /***********************************************************************
5742 * CreateIUriBuilder (urlmon.@)
5744 HRESULT WINAPI CreateIUriBuilder(IUri *pIUri, DWORD dwFlags, DWORD_PTR dwReserved, IUriBuilder **ppIUriBuilder)
5746 UriBuilder *ret;
5748 TRACE("(%p %x %x %p)\n", pIUri, dwFlags, (DWORD)dwReserved, ppIUriBuilder);
5750 if(!ppIUriBuilder)
5751 return E_POINTER;
5753 ret = heap_alloc_zero(sizeof(UriBuilder));
5754 if(!ret)
5755 return E_OUTOFMEMORY;
5757 ret->IUriBuilder_iface.lpVtbl = &UriBuilderVtbl;
5758 ret->ref = 1;
5760 if(pIUri) {
5761 Uri *uri;
5763 if((uri = get_uri_obj(pIUri))) {
5764 IUri_AddRef(pIUri);
5765 ret->uri = uri;
5767 if(uri->has_port)
5768 /* Windows doesn't set 'has_port' to TRUE in this case. */
5769 ret->port = uri->port;
5771 } else {
5772 heap_free(ret);
5773 *ppIUriBuilder = NULL;
5774 FIXME("(%p %x %x %p): Unknown IUri types not supported yet.\n", pIUri, dwFlags,
5775 (DWORD)dwReserved, ppIUriBuilder);
5776 return E_NOTIMPL;
5780 *ppIUriBuilder = &ret->IUriBuilder_iface;
5781 return S_OK;
5784 /* Merges the base path with the relative path and stores the resulting path
5785 * and path len in 'result' and 'result_len'.
5787 static HRESULT merge_paths(parse_data *data, const WCHAR *base, DWORD base_len, const WCHAR *relative,
5788 DWORD relative_len, WCHAR **result, DWORD *result_len, DWORD flags)
5790 const WCHAR *end = NULL;
5791 DWORD base_copy_len = 0;
5792 WCHAR *ptr;
5794 if(base_len) {
5795 /* Find the characters the will be copied over from
5796 * the base path.
5798 end = str_last_of(base, base+(base_len-1), '/');
5799 if(!end && data->scheme_type == URL_SCHEME_FILE)
5800 /* Try looking for a '\\'. */
5801 end = str_last_of(base, base+(base_len-1), '\\');
5804 if(end) {
5805 base_copy_len = (end+1)-base;
5806 *result = heap_alloc((base_copy_len+relative_len+1)*sizeof(WCHAR));
5807 } else
5808 *result = heap_alloc((relative_len+1)*sizeof(WCHAR));
5810 if(!(*result)) {
5811 *result_len = 0;
5812 return E_OUTOFMEMORY;
5815 ptr = *result;
5816 if(end) {
5817 memcpy(ptr, base, base_copy_len*sizeof(WCHAR));
5818 ptr += base_copy_len;
5821 memcpy(ptr, relative, relative_len*sizeof(WCHAR));
5822 ptr += relative_len;
5823 *ptr = '\0';
5825 *result_len = (ptr-*result);
5826 return S_OK;
5829 static HRESULT combine_uri(Uri *base, Uri *relative, DWORD flags, IUri **result, DWORD extras) {
5830 Uri *ret;
5831 HRESULT hr;
5832 parse_data data;
5833 DWORD create_flags = 0, len = 0;
5835 memset(&data, 0, sizeof(parse_data));
5837 /* Base case is when the relative Uri has a scheme name,
5838 * if it does, then 'result' will contain the same data
5839 * as the relative Uri.
5841 if(relative->scheme_start > -1) {
5842 data.uri = SysAllocString(relative->raw_uri);
5843 if(!data.uri) {
5844 *result = NULL;
5845 return E_OUTOFMEMORY;
5848 parse_uri(&data, 0);
5850 ret = create_uri_obj();
5851 if(!ret) {
5852 *result = NULL;
5853 return E_OUTOFMEMORY;
5856 if(extras & COMBINE_URI_FORCE_FLAG_USE) {
5857 if(flags & URL_DONT_SIMPLIFY)
5858 create_flags |= Uri_CREATE_NO_CANONICALIZE;
5859 if(flags & URL_DONT_UNESCAPE_EXTRA_INFO)
5860 create_flags |= Uri_CREATE_NO_DECODE_EXTRA_INFO;
5863 ret->raw_uri = data.uri;
5864 hr = canonicalize_uri(&data, ret, create_flags);
5865 if(FAILED(hr)) {
5866 IUri_Release(&ret->IUri_iface);
5867 *result = NULL;
5868 return hr;
5871 apply_default_flags(&create_flags);
5872 ret->create_flags = create_flags;
5874 *result = &ret->IUri_iface;
5875 } else {
5876 WCHAR *path = NULL;
5877 DWORD raw_flags = 0;
5879 if(base->scheme_start > -1) {
5880 data.scheme = base->canon_uri+base->scheme_start;
5881 data.scheme_len = base->scheme_len;
5882 data.scheme_type = base->scheme_type;
5883 } else {
5884 data.is_relative = TRUE;
5885 data.scheme_type = URL_SCHEME_UNKNOWN;
5886 create_flags |= Uri_CREATE_ALLOW_RELATIVE;
5889 if(base->authority_start > -1) {
5890 if(base->userinfo_start > -1 && base->userinfo_split != 0) {
5891 data.username = base->canon_uri+base->userinfo_start;
5892 data.username_len = (base->userinfo_split > -1) ? base->userinfo_split : base->userinfo_len;
5895 if(base->userinfo_split > -1) {
5896 data.password = base->canon_uri+base->userinfo_start+base->userinfo_split+1;
5897 data.password_len = base->userinfo_len-base->userinfo_split-1;
5900 if(base->host_start > -1) {
5901 data.host = base->canon_uri+base->host_start;
5902 data.host_len = base->host_len;
5903 data.host_type = base->host_type;
5906 if(base->has_port) {
5907 data.has_port = TRUE;
5908 data.port_value = base->port;
5910 } else if(base->scheme_type != URL_SCHEME_FILE)
5911 data.is_opaque = TRUE;
5913 if(relative->path_start == -1 || !relative->path_len) {
5914 if(base->path_start > -1) {
5915 data.path = base->canon_uri+base->path_start;
5916 data.path_len = base->path_len;
5917 } else if((base->path_start == -1 || !base->path_len) && !data.is_opaque) {
5918 /* Just set the path as a '/' if the base didn't have
5919 * one and if it's an hierarchical URI.
5921 static const WCHAR slashW[] = {'/',0};
5922 data.path = slashW;
5923 data.path_len = 1;
5926 if(relative->query_start > -1) {
5927 data.query = relative->canon_uri+relative->query_start;
5928 data.query_len = relative->query_len;
5929 } else if(base->query_start > -1) {
5930 data.query = base->canon_uri+base->query_start;
5931 data.query_len = base->query_len;
5933 } else {
5934 const WCHAR *ptr, **pptr;
5935 DWORD path_offset = 0, path_len = 0;
5937 /* There's two possibilities on what will happen to the path component
5938 * of the result IUri. First, if the relative path begins with a '/'
5939 * then the resulting path will just be the relative path. Second, if
5940 * relative path doesn't begin with a '/' then the base path and relative
5941 * path are merged together.
5943 if(relative->path_len && *(relative->canon_uri+relative->path_start) == '/') {
5944 WCHAR *tmp = NULL;
5945 BOOL copy_drive_path = FALSE;
5947 /* If the relative IUri's path starts with a '/', then we
5948 * don't use the base IUri's path. Unless the base IUri
5949 * is a file URI, in which case it uses the drive path of
5950 * the base IUri (if it has any) in the new path.
5952 if(base->scheme_type == URL_SCHEME_FILE) {
5953 if(base->path_len > 3 && *(base->canon_uri+base->path_start) == '/' &&
5954 is_drive_path(base->canon_uri+base->path_start+1)) {
5955 path_len += 3;
5956 copy_drive_path = TRUE;
5960 path_len += relative->path_len;
5962 path = heap_alloc((path_len+1)*sizeof(WCHAR));
5963 if(!path) {
5964 *result = NULL;
5965 return E_OUTOFMEMORY;
5968 tmp = path;
5970 /* Copy the base paths, drive path over. */
5971 if(copy_drive_path) {
5972 memcpy(tmp, base->canon_uri+base->path_start, 3*sizeof(WCHAR));
5973 tmp += 3;
5976 memcpy(tmp, relative->canon_uri+relative->path_start, relative->path_len*sizeof(WCHAR));
5977 path[path_len] = '\0';
5978 } else {
5979 /* Merge the base path with the relative path. */
5980 hr = merge_paths(&data, base->canon_uri+base->path_start, base->path_len,
5981 relative->canon_uri+relative->path_start, relative->path_len,
5982 &path, &path_len, flags);
5983 if(FAILED(hr)) {
5984 *result = NULL;
5985 return hr;
5988 /* If the resulting IUri is a file URI, the drive path isn't
5989 * reduced out when the dot segments are removed.
5991 if(path_len >= 3 && data.scheme_type == URL_SCHEME_FILE && !data.host) {
5992 if(*path == '/' && is_drive_path(path+1))
5993 path_offset = 2;
5994 else if(is_drive_path(path))
5995 path_offset = 1;
5999 /* Check if the dot segments need to be removed from the path. */
6000 if(!(flags & URL_DONT_SIMPLIFY) && !data.is_opaque) {
6001 DWORD offset = (path_offset > 0) ? path_offset+1 : 0;
6002 DWORD new_len = remove_dot_segments(path+offset,path_len-offset);
6004 if(new_len != path_len) {
6005 WCHAR *tmp = heap_realloc(path, (path_offset+new_len+1)*sizeof(WCHAR));
6006 if(!tmp) {
6007 heap_free(path);
6008 *result = NULL;
6009 return E_OUTOFMEMORY;
6012 tmp[new_len+offset] = '\0';
6013 path = tmp;
6014 path_len = new_len+offset;
6018 /* Make sure the path component is valid. */
6019 ptr = path;
6020 pptr = &ptr;
6021 if((data.is_opaque && !parse_path_opaque(pptr, &data, 0)) ||
6022 (!data.is_opaque && !parse_path_hierarchical(pptr, &data, 0))) {
6023 heap_free(path);
6024 *result = NULL;
6025 return E_INVALIDARG;
6029 if(relative->fragment_start > -1) {
6030 data.fragment = relative->canon_uri+relative->fragment_start;
6031 data.fragment_len = relative->fragment_len;
6034 if(flags & URL_DONT_SIMPLIFY)
6035 raw_flags |= RAW_URI_FORCE_PORT_DISP;
6036 if(flags & URL_FILE_USE_PATHURL)
6037 raw_flags |= RAW_URI_CONVERT_TO_DOS_PATH;
6039 len = generate_raw_uri(&data, data.uri, raw_flags);
6040 data.uri = SysAllocStringLen(NULL, len);
6041 if(!data.uri) {
6042 heap_free(path);
6043 *result = NULL;
6044 return E_OUTOFMEMORY;
6047 generate_raw_uri(&data, data.uri, raw_flags);
6049 ret = create_uri_obj();
6050 if(!ret) {
6051 SysFreeString(data.uri);
6052 heap_free(path);
6053 *result = NULL;
6054 return E_OUTOFMEMORY;
6057 if(flags & URL_DONT_SIMPLIFY)
6058 create_flags |= Uri_CREATE_NO_CANONICALIZE;
6059 if(flags & URL_FILE_USE_PATHURL)
6060 create_flags |= Uri_CREATE_FILE_USE_DOS_PATH;
6062 ret->raw_uri = data.uri;
6063 hr = canonicalize_uri(&data, ret, create_flags);
6064 if(FAILED(hr)) {
6065 IUri_Release(&ret->IUri_iface);
6066 *result = NULL;
6067 return hr;
6070 if(flags & URL_DONT_SIMPLIFY)
6071 ret->display_modifiers |= URI_DISPLAY_NO_DEFAULT_PORT_AUTH;
6073 apply_default_flags(&create_flags);
6074 ret->create_flags = create_flags;
6075 *result = &ret->IUri_iface;
6077 heap_free(path);
6080 return S_OK;
6083 /***********************************************************************
6084 * CoInternetCombineIUri (urlmon.@)
6086 HRESULT WINAPI CoInternetCombineIUri(IUri *pBaseUri, IUri *pRelativeUri, DWORD dwCombineFlags,
6087 IUri **ppCombinedUri, DWORD_PTR dwReserved)
6089 HRESULT hr;
6090 IInternetProtocolInfo *info;
6091 Uri *relative, *base;
6092 TRACE("(%p %p %x %p %x)\n", pBaseUri, pRelativeUri, dwCombineFlags, ppCombinedUri, (DWORD)dwReserved);
6094 if(!ppCombinedUri)
6095 return E_INVALIDARG;
6097 if(!pBaseUri || !pRelativeUri) {
6098 *ppCombinedUri = NULL;
6099 return E_INVALIDARG;
6102 relative = get_uri_obj(pRelativeUri);
6103 base = get_uri_obj(pBaseUri);
6104 if(!relative || !base) {
6105 *ppCombinedUri = NULL;
6106 FIXME("(%p %p %x %p %x) Unknown IUri types not supported yet.\n",
6107 pBaseUri, pRelativeUri, dwCombineFlags, ppCombinedUri, (DWORD)dwReserved);
6108 return E_NOTIMPL;
6111 info = get_protocol_info(base->canon_uri);
6112 if(info) {
6113 WCHAR result[INTERNET_MAX_URL_LENGTH+1];
6114 DWORD result_len = 0;
6116 hr = IInternetProtocolInfo_CombineUrl(info, base->canon_uri, relative->canon_uri, dwCombineFlags,
6117 result, INTERNET_MAX_URL_LENGTH+1, &result_len, 0);
6118 IInternetProtocolInfo_Release(info);
6119 if(SUCCEEDED(hr)) {
6120 hr = CreateUri(result, Uri_CREATE_ALLOW_RELATIVE, 0, ppCombinedUri);
6121 if(SUCCEEDED(hr))
6122 return hr;
6126 return combine_uri(base, relative, dwCombineFlags, ppCombinedUri, 0);
6129 /***********************************************************************
6130 * CoInternetCombineUrlEx (urlmon.@)
6132 HRESULT WINAPI CoInternetCombineUrlEx(IUri *pBaseUri, LPCWSTR pwzRelativeUrl, DWORD dwCombineFlags,
6133 IUri **ppCombinedUri, DWORD_PTR dwReserved)
6135 IUri *relative;
6136 Uri *base;
6137 HRESULT hr;
6138 IInternetProtocolInfo *info;
6140 TRACE("(%p %s %x %p %x) stub\n", pBaseUri, debugstr_w(pwzRelativeUrl), dwCombineFlags,
6141 ppCombinedUri, (DWORD)dwReserved);
6143 if(!ppCombinedUri)
6144 return E_POINTER;
6146 if(!pwzRelativeUrl) {
6147 *ppCombinedUri = NULL;
6148 return E_UNEXPECTED;
6151 if(!pBaseUri) {
6152 *ppCombinedUri = NULL;
6153 return E_INVALIDARG;
6156 base = get_uri_obj(pBaseUri);
6157 if(!base) {
6158 *ppCombinedUri = NULL;
6159 FIXME("(%p %s %x %p %x) Unknown IUri's not supported yet.\n", pBaseUri, debugstr_w(pwzRelativeUrl),
6160 dwCombineFlags, ppCombinedUri, (DWORD)dwReserved);
6161 return E_NOTIMPL;
6164 info = get_protocol_info(base->canon_uri);
6165 if(info) {
6166 WCHAR result[INTERNET_MAX_URL_LENGTH+1];
6167 DWORD result_len = 0;
6169 hr = IInternetProtocolInfo_CombineUrl(info, base->canon_uri, pwzRelativeUrl, dwCombineFlags,
6170 result, INTERNET_MAX_URL_LENGTH+1, &result_len, 0);
6171 IInternetProtocolInfo_Release(info);
6172 if(SUCCEEDED(hr)) {
6173 hr = CreateUri(result, Uri_CREATE_ALLOW_RELATIVE, 0, ppCombinedUri);
6174 if(SUCCEEDED(hr))
6175 return hr;
6179 hr = CreateUri(pwzRelativeUrl, Uri_CREATE_ALLOW_RELATIVE, 0, &relative);
6180 if(FAILED(hr)) {
6181 *ppCombinedUri = NULL;
6182 return hr;
6185 hr = combine_uri(base, get_uri_obj(relative), dwCombineFlags, ppCombinedUri, COMBINE_URI_FORCE_FLAG_USE);
6187 IUri_Release(relative);
6188 return hr;
6191 static HRESULT parse_canonicalize(const Uri *uri, DWORD flags, LPWSTR output,
6192 DWORD output_len, DWORD *result_len)
6194 const WCHAR *ptr = NULL;
6195 WCHAR *path = NULL;
6196 const WCHAR **pptr;
6197 WCHAR buffer[INTERNET_MAX_URL_LENGTH+1];
6198 DWORD len = 0;
6199 BOOL reduce_path;
6201 /* URL_UNESCAPE only has effect if none of the URL_ESCAPE flags are set. */
6202 const BOOL allow_unescape = !(flags & URL_ESCAPE_UNSAFE) &&
6203 !(flags & URL_ESCAPE_SPACES_ONLY) &&
6204 !(flags & URL_ESCAPE_PERCENT);
6207 /* Check if the dot segments need to be removed from the
6208 * path component.
6210 if(uri->scheme_start > -1 && uri->path_start > -1) {
6211 ptr = uri->canon_uri+uri->scheme_start+uri->scheme_len+1;
6212 pptr = &ptr;
6214 reduce_path = !(flags & URL_NO_META) &&
6215 !(flags & URL_DONT_SIMPLIFY) &&
6216 ptr && check_hierarchical(pptr);
6218 for(ptr = uri->canon_uri; ptr < uri->canon_uri+uri->canon_len; ++ptr) {
6219 BOOL do_default_action = TRUE;
6221 /* Keep track of the path if we need to remove dot segments from
6222 * it later.
6224 if(reduce_path && !path && ptr == uri->canon_uri+uri->path_start)
6225 path = buffer+len;
6227 /* Check if it's time to reduce the path. */
6228 if(reduce_path && ptr == uri->canon_uri+uri->path_start+uri->path_len) {
6229 DWORD current_path_len = (buffer+len) - path;
6230 DWORD new_path_len = remove_dot_segments(path, current_path_len);
6232 /* Update the current length. */
6233 len -= (current_path_len-new_path_len);
6234 reduce_path = FALSE;
6237 if(*ptr == '%') {
6238 const WCHAR decoded = decode_pct_val(ptr);
6239 if(decoded) {
6240 if(allow_unescape && (flags & URL_UNESCAPE)) {
6241 buffer[len++] = decoded;
6242 ptr += 2;
6243 do_default_action = FALSE;
6247 /* See if %'s needed to encoded. */
6248 if(do_default_action && (flags & URL_ESCAPE_PERCENT)) {
6249 pct_encode_val(*ptr, buffer+len);
6250 len += 3;
6251 do_default_action = FALSE;
6253 } else if(*ptr == ' ') {
6254 if((flags & URL_ESCAPE_SPACES_ONLY) &&
6255 !(flags & URL_ESCAPE_UNSAFE)) {
6256 pct_encode_val(*ptr, buffer+len);
6257 len += 3;
6258 do_default_action = FALSE;
6260 } else if(!is_reserved(*ptr) && !is_unreserved(*ptr)) {
6261 if(flags & URL_ESCAPE_UNSAFE) {
6262 pct_encode_val(*ptr, buffer+len);
6263 len += 3;
6264 do_default_action = FALSE;
6268 if(do_default_action)
6269 buffer[len++] = *ptr;
6272 /* Sometimes the path is the very last component of the IUri, so
6273 * see if the dot segments need to be reduced now.
6275 if(reduce_path && path) {
6276 DWORD current_path_len = (buffer+len) - path;
6277 DWORD new_path_len = remove_dot_segments(path, current_path_len);
6279 /* Update the current length. */
6280 len -= (current_path_len-new_path_len);
6283 buffer[len++] = 0;
6285 /* The null terminator isn't included the length. */
6286 *result_len = len-1;
6287 if(len > output_len)
6288 return STRSAFE_E_INSUFFICIENT_BUFFER;
6289 else
6290 memcpy(output, buffer, len*sizeof(WCHAR));
6292 return S_OK;
6295 static HRESULT parse_friendly(IUri *uri, LPWSTR output, DWORD output_len,
6296 DWORD *result_len)
6298 HRESULT hr;
6299 DWORD display_len;
6300 BSTR display;
6302 hr = IUri_GetPropertyLength(uri, Uri_PROPERTY_DISPLAY_URI, &display_len, 0);
6303 if(FAILED(hr)) {
6304 *result_len = 0;
6305 return hr;
6308 *result_len = display_len;
6309 if(display_len+1 > output_len)
6310 return STRSAFE_E_INSUFFICIENT_BUFFER;
6312 hr = IUri_GetDisplayUri(uri, &display);
6313 if(FAILED(hr)) {
6314 *result_len = 0;
6315 return hr;
6318 memcpy(output, display, (display_len+1)*sizeof(WCHAR));
6319 SysFreeString(display);
6320 return S_OK;
6323 static HRESULT parse_rootdocument(const Uri *uri, LPWSTR output, DWORD output_len,
6324 DWORD *result_len)
6326 static const WCHAR colon_slashesW[] = {':','/','/'};
6328 WCHAR *ptr;
6329 DWORD len = 0;
6331 /* Windows only returns the root document if the URI has an authority
6332 * and it's not an unknown scheme type or a file scheme type.
6334 if(uri->authority_start == -1 ||
6335 uri->scheme_type == URL_SCHEME_UNKNOWN ||
6336 uri->scheme_type == URL_SCHEME_FILE) {
6337 *result_len = 0;
6338 if(!output_len)
6339 return STRSAFE_E_INSUFFICIENT_BUFFER;
6341 output[0] = 0;
6342 return S_OK;
6345 len = uri->scheme_len+uri->authority_len;
6346 /* For the "://" and '/' which will be added. */
6347 len += 4;
6349 if(len+1 > output_len) {
6350 *result_len = len;
6351 return STRSAFE_E_INSUFFICIENT_BUFFER;
6354 ptr = output;
6355 memcpy(ptr, uri->canon_uri+uri->scheme_start, uri->scheme_len*sizeof(WCHAR));
6357 /* Add the "://". */
6358 ptr += uri->scheme_len;
6359 memcpy(ptr, colon_slashesW, sizeof(colon_slashesW));
6361 /* Add the authority. */
6362 ptr += sizeof(colon_slashesW)/sizeof(WCHAR);
6363 memcpy(ptr, uri->canon_uri+uri->authority_start, uri->authority_len*sizeof(WCHAR));
6365 /* Add the '/' after the authority. */
6366 ptr += uri->authority_len;
6367 *ptr = '/';
6368 ptr[1] = 0;
6370 *result_len = len;
6371 return S_OK;
6374 static HRESULT parse_document(const Uri *uri, LPWSTR output, DWORD output_len,
6375 DWORD *result_len)
6377 DWORD len = 0;
6379 /* It has to be a known scheme type, but, it can't be a file
6380 * scheme. It also has to hierarchical.
6382 if(uri->scheme_type == URL_SCHEME_UNKNOWN ||
6383 uri->scheme_type == URL_SCHEME_FILE ||
6384 uri->authority_start == -1) {
6385 *result_len = 0;
6386 if(output_len < 1)
6387 return STRSAFE_E_INSUFFICIENT_BUFFER;
6389 output[0] = 0;
6390 return S_OK;
6393 if(uri->fragment_start > -1)
6394 len = uri->fragment_start;
6395 else
6396 len = uri->canon_len;
6398 *result_len = len;
6399 if(len+1 > output_len)
6400 return STRSAFE_E_INSUFFICIENT_BUFFER;
6402 memcpy(output, uri->canon_uri, len*sizeof(WCHAR));
6403 output[len] = 0;
6404 return S_OK;
6407 static HRESULT parse_path_from_url(const Uri *uri, LPWSTR output, DWORD output_len,
6408 DWORD *result_len)
6410 const WCHAR *path_ptr;
6411 WCHAR buffer[INTERNET_MAX_URL_LENGTH+1];
6412 WCHAR *ptr;
6414 if(uri->scheme_type != URL_SCHEME_FILE) {
6415 *result_len = 0;
6416 if(output_len > 0)
6417 output[0] = 0;
6418 return E_INVALIDARG;
6421 ptr = buffer;
6422 if(uri->host_start > -1) {
6423 static const WCHAR slash_slashW[] = {'\\','\\'};
6425 memcpy(ptr, slash_slashW, sizeof(slash_slashW));
6426 ptr += sizeof(slash_slashW)/sizeof(WCHAR);
6427 memcpy(ptr, uri->canon_uri+uri->host_start, uri->host_len*sizeof(WCHAR));
6428 ptr += uri->host_len;
6431 path_ptr = uri->canon_uri+uri->path_start;
6432 if(uri->path_len > 3 && *path_ptr == '/' && is_drive_path(path_ptr+1))
6433 /* Skip past the '/' in front of the drive path. */
6434 ++path_ptr;
6436 for(; path_ptr < uri->canon_uri+uri->path_start+uri->path_len; ++path_ptr, ++ptr) {
6437 BOOL do_default_action = TRUE;
6439 if(*path_ptr == '%') {
6440 const WCHAR decoded = decode_pct_val(path_ptr);
6441 if(decoded) {
6442 *ptr = decoded;
6443 path_ptr += 2;
6444 do_default_action = FALSE;
6446 } else if(*path_ptr == '/') {
6447 *ptr = '\\';
6448 do_default_action = FALSE;
6451 if(do_default_action)
6452 *ptr = *path_ptr;
6455 *ptr = 0;
6457 *result_len = ptr-buffer;
6458 if(*result_len+1 > output_len)
6459 return STRSAFE_E_INSUFFICIENT_BUFFER;
6461 memcpy(output, buffer, (*result_len+1)*sizeof(WCHAR));
6462 return S_OK;
6465 static HRESULT parse_url_from_path(IUri *uri, LPWSTR output, DWORD output_len,
6466 DWORD *result_len)
6468 HRESULT hr;
6469 BSTR received;
6470 DWORD len = 0;
6472 hr = IUri_GetPropertyLength(uri, Uri_PROPERTY_ABSOLUTE_URI, &len, 0);
6473 if(FAILED(hr)) {
6474 *result_len = 0;
6475 return hr;
6478 *result_len = len;
6479 if(len+1 > output_len)
6480 return STRSAFE_E_INSUFFICIENT_BUFFER;
6482 hr = IUri_GetAbsoluteUri(uri, &received);
6483 if(FAILED(hr)) {
6484 *result_len = 0;
6485 return hr;
6488 memcpy(output, received, (len+1)*sizeof(WCHAR));
6489 SysFreeString(received);
6491 return S_OK;
6494 static HRESULT parse_schema(IUri *uri, LPWSTR output, DWORD output_len,
6495 DWORD *result_len)
6497 HRESULT hr;
6498 DWORD len;
6499 BSTR received;
6501 hr = IUri_GetPropertyLength(uri, Uri_PROPERTY_SCHEME_NAME, &len, 0);
6502 if(FAILED(hr)) {
6503 *result_len = 0;
6504 return hr;
6507 *result_len = len;
6508 if(len+1 > output_len)
6509 return STRSAFE_E_INSUFFICIENT_BUFFER;
6511 hr = IUri_GetSchemeName(uri, &received);
6512 if(FAILED(hr)) {
6513 *result_len = 0;
6514 return hr;
6517 memcpy(output, received, (len+1)*sizeof(WCHAR));
6518 SysFreeString(received);
6520 return S_OK;
6523 static HRESULT parse_site(IUri *uri, LPWSTR output, DWORD output_len, DWORD *result_len)
6525 HRESULT hr;
6526 DWORD len;
6527 BSTR received;
6529 hr = IUri_GetPropertyLength(uri, Uri_PROPERTY_HOST, &len, 0);
6530 if(FAILED(hr)) {
6531 *result_len = 0;
6532 return hr;
6535 *result_len = len;
6536 if(len+1 > output_len)
6537 return STRSAFE_E_INSUFFICIENT_BUFFER;
6539 hr = IUri_GetHost(uri, &received);
6540 if(FAILED(hr)) {
6541 *result_len = 0;
6542 return hr;
6545 memcpy(output, received, (len+1)*sizeof(WCHAR));
6546 SysFreeString(received);
6548 return S_OK;
6551 static HRESULT parse_domain(IUri *uri, LPWSTR output, DWORD output_len, DWORD *result_len)
6553 HRESULT hr;
6554 DWORD len;
6555 BSTR received;
6557 hr = IUri_GetPropertyLength(uri, Uri_PROPERTY_DOMAIN, &len, 0);
6558 if(FAILED(hr)) {
6559 *result_len = 0;
6560 return hr;
6563 *result_len = len;
6564 if(len+1 > output_len)
6565 return STRSAFE_E_INSUFFICIENT_BUFFER;
6567 hr = IUri_GetDomain(uri, &received);
6568 if(FAILED(hr)) {
6569 *result_len = 0;
6570 return hr;
6573 memcpy(output, received, (len+1)*sizeof(WCHAR));
6574 SysFreeString(received);
6576 return S_OK;
6579 static HRESULT parse_anchor(IUri *uri, LPWSTR output, DWORD output_len, DWORD *result_len)
6581 HRESULT hr;
6582 DWORD len;
6583 BSTR received;
6585 hr = IUri_GetPropertyLength(uri, Uri_PROPERTY_FRAGMENT, &len, 0);
6586 if(FAILED(hr)) {
6587 *result_len = 0;
6588 return hr;
6591 *result_len = len;
6592 if(len+1 > output_len)
6593 return STRSAFE_E_INSUFFICIENT_BUFFER;
6595 hr = IUri_GetFragment(uri, &received);
6596 if(FAILED(hr)) {
6597 *result_len = 0;
6598 return hr;
6601 memcpy(output, received, (len+1)*sizeof(WCHAR));
6602 SysFreeString(received);
6604 return S_OK;
6607 /***********************************************************************
6608 * CoInternetParseIUri (urlmon.@)
6610 HRESULT WINAPI CoInternetParseIUri(IUri *pIUri, PARSEACTION ParseAction, DWORD dwFlags,
6611 LPWSTR pwzResult, DWORD cchResult, DWORD *pcchResult,
6612 DWORD_PTR dwReserved)
6614 HRESULT hr;
6615 Uri *uri;
6616 IInternetProtocolInfo *info;
6618 TRACE("(%p %d %x %p %d %p %x)\n", pIUri, ParseAction, dwFlags, pwzResult,
6619 cchResult, pcchResult, (DWORD)dwReserved);
6621 if(!pcchResult)
6622 return E_POINTER;
6624 if(!pwzResult || !pIUri) {
6625 *pcchResult = 0;
6626 return E_INVALIDARG;
6629 if(!(uri = get_uri_obj(pIUri))) {
6630 *pcchResult = 0;
6631 FIXME("(%p %d %x %p %d %p %x) Unknown IUri's not supported for this action.\n",
6632 pIUri, ParseAction, dwFlags, pwzResult, cchResult, pcchResult, (DWORD)dwReserved);
6633 return E_NOTIMPL;
6636 info = get_protocol_info(uri->canon_uri);
6637 if(info) {
6638 hr = IInternetProtocolInfo_ParseUrl(info, uri->canon_uri, ParseAction, dwFlags,
6639 pwzResult, cchResult, pcchResult, 0);
6640 IInternetProtocolInfo_Release(info);
6641 if(SUCCEEDED(hr)) return hr;
6644 switch(ParseAction) {
6645 case PARSE_CANONICALIZE:
6646 hr = parse_canonicalize(uri, dwFlags, pwzResult, cchResult, pcchResult);
6647 break;
6648 case PARSE_FRIENDLY:
6649 hr = parse_friendly(pIUri, pwzResult, cchResult, pcchResult);
6650 break;
6651 case PARSE_ROOTDOCUMENT:
6652 hr = parse_rootdocument(uri, pwzResult, cchResult, pcchResult);
6653 break;
6654 case PARSE_DOCUMENT:
6655 hr = parse_document(uri, pwzResult, cchResult, pcchResult);
6656 break;
6657 case PARSE_PATH_FROM_URL:
6658 hr = parse_path_from_url(uri, pwzResult, cchResult, pcchResult);
6659 break;
6660 case PARSE_URL_FROM_PATH:
6661 hr = parse_url_from_path(pIUri, pwzResult, cchResult, pcchResult);
6662 break;
6663 case PARSE_SCHEMA:
6664 hr = parse_schema(pIUri, pwzResult, cchResult, pcchResult);
6665 break;
6666 case PARSE_SITE:
6667 hr = parse_site(pIUri, pwzResult, cchResult, pcchResult);
6668 break;
6669 case PARSE_DOMAIN:
6670 hr = parse_domain(pIUri, pwzResult, cchResult, pcchResult);
6671 break;
6672 case PARSE_LOCATION:
6673 case PARSE_ANCHOR:
6674 hr = parse_anchor(pIUri, pwzResult, cchResult, pcchResult);
6675 break;
6676 case PARSE_SECURITY_URL:
6677 case PARSE_MIME:
6678 case PARSE_SERVER:
6679 case PARSE_SECURITY_DOMAIN:
6680 *pcchResult = 0;
6681 hr = E_FAIL;
6682 break;
6683 default:
6684 *pcchResult = 0;
6685 hr = E_NOTIMPL;
6686 FIXME("(%p %d %x %p %d %p %x) Partial stub.\n", pIUri, ParseAction, dwFlags,
6687 pwzResult, cchResult, pcchResult, (DWORD)dwReserved);
6690 return hr;