urlmon: Added support for javascript URIs.
[wine.git] / dlls / urlmon / uri.c
blob4586f9d0af13de7e1d1d91ceba7db5bee768101e
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 inline BOOL is_slash(WCHAR c)
355 return c == '/' || c == '\\';
358 static BOOL is_default_port(URL_SCHEME scheme, DWORD port) {
359 DWORD i;
361 for(i = 0; i < sizeof(default_ports)/sizeof(default_ports[0]); ++i) {
362 if(default_ports[i].scheme == scheme && default_ports[i].port)
363 return TRUE;
366 return FALSE;
369 /* List of schemes types Windows seems to expect to be hierarchical. */
370 static inline BOOL is_hierarchical_scheme(URL_SCHEME type) {
371 return(type == URL_SCHEME_HTTP || type == URL_SCHEME_FTP ||
372 type == URL_SCHEME_GOPHER || type == URL_SCHEME_NNTP ||
373 type == URL_SCHEME_TELNET || type == URL_SCHEME_WAIS ||
374 type == URL_SCHEME_FILE || type == URL_SCHEME_HTTPS ||
375 type == URL_SCHEME_RES);
378 /* Checks if 'flags' contains an invalid combination of Uri_CREATE flags. */
379 static inline BOOL has_invalid_flag_combination(DWORD flags) {
380 return((flags & Uri_CREATE_DECODE_EXTRA_INFO && flags & Uri_CREATE_NO_DECODE_EXTRA_INFO) ||
381 (flags & Uri_CREATE_CANONICALIZE && flags & Uri_CREATE_NO_CANONICALIZE) ||
382 (flags & Uri_CREATE_CRACK_UNKNOWN_SCHEMES && flags & Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES) ||
383 (flags & Uri_CREATE_PRE_PROCESS_HTML_URI && flags & Uri_CREATE_NO_PRE_PROCESS_HTML_URI) ||
384 (flags & Uri_CREATE_IE_SETTINGS && flags & Uri_CREATE_NO_IE_SETTINGS));
387 /* Applies each default Uri_CREATE flags to 'flags' if it
388 * doesn't cause a flag conflict.
390 static void apply_default_flags(DWORD *flags) {
391 if(!(*flags & Uri_CREATE_NO_CANONICALIZE))
392 *flags |= Uri_CREATE_CANONICALIZE;
393 if(!(*flags & Uri_CREATE_NO_DECODE_EXTRA_INFO))
394 *flags |= Uri_CREATE_DECODE_EXTRA_INFO;
395 if(!(*flags & Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES))
396 *flags |= Uri_CREATE_CRACK_UNKNOWN_SCHEMES;
397 if(!(*flags & Uri_CREATE_NO_PRE_PROCESS_HTML_URI))
398 *flags |= Uri_CREATE_PRE_PROCESS_HTML_URI;
399 if(!(*flags & Uri_CREATE_IE_SETTINGS))
400 *flags |= Uri_CREATE_NO_IE_SETTINGS;
403 /* Determines if the URI is hierarchical using the information already parsed into
404 * data and using the current location of parsing in the URI string.
406 * Windows considers a URI hierarchical if on of the following is true:
407 * A.) It's a wildcard scheme.
408 * B.) It's an implicit file scheme.
409 * C.) It's a known hierarchical scheme and it has two '\\' after the scheme name.
410 * (the '\\' will be converted into "//" during canonicalization).
411 * D.) It's not a relative URI and "//" appears after the scheme name.
413 static inline BOOL is_hierarchical_uri(const WCHAR **ptr, const parse_data *data) {
414 const WCHAR *start = *ptr;
416 if(data->scheme_type == URL_SCHEME_WILDCARD)
417 return TRUE;
418 else if(data->scheme_type == URL_SCHEME_FILE && data->has_implicit_scheme)
419 return TRUE;
420 else if(is_hierarchical_scheme(data->scheme_type) && (*ptr)[0] == '\\' && (*ptr)[1] == '\\') {
421 *ptr += 2;
422 return TRUE;
423 } else if(!data->is_relative && check_hierarchical(ptr))
424 return TRUE;
426 *ptr = start;
427 return FALSE;
430 /* Checks if the two Uri's are logically equivalent. It's a simple
431 * comparison, since they are both of type Uri, and it can access
432 * the properties of each Uri directly without the need to go
433 * through the "IUri_Get*" interface calls.
435 static BOOL are_equal_simple(const Uri *a, const Uri *b) {
436 if(a->scheme_type == b->scheme_type) {
437 const BOOL known_scheme = a->scheme_type != URL_SCHEME_UNKNOWN;
438 const BOOL are_hierarchical =
439 (a->authority_start > -1 && b->authority_start > -1);
441 if(a->scheme_type == URL_SCHEME_FILE) {
442 if(a->canon_len == b->canon_len)
443 return !StrCmpIW(a->canon_uri, b->canon_uri);
446 /* Only compare the scheme names (if any) if their unknown scheme types. */
447 if(!known_scheme) {
448 if((a->scheme_start > -1 && b->scheme_start > -1) &&
449 (a->scheme_len == b->scheme_len)) {
450 /* Make sure the schemes are the same. */
451 if(StrCmpNW(a->canon_uri+a->scheme_start, b->canon_uri+b->scheme_start, a->scheme_len))
452 return FALSE;
453 } else if(a->scheme_len != b->scheme_len)
454 /* One of the Uri's has a scheme name, while the other doesn't. */
455 return FALSE;
458 /* If they have a userinfo component, perform case sensitive compare. */
459 if((a->userinfo_start > -1 && b->userinfo_start > -1) &&
460 (a->userinfo_len == b->userinfo_len)) {
461 if(StrCmpNW(a->canon_uri+a->userinfo_start, b->canon_uri+b->userinfo_start, a->userinfo_len))
462 return FALSE;
463 } else if(a->userinfo_len != b->userinfo_len)
464 /* One of the Uri's had a userinfo, while the other one doesn't. */
465 return FALSE;
467 /* Check if they have a host name. */
468 if((a->host_start > -1 && b->host_start > -1) &&
469 (a->host_len == b->host_len)) {
470 /* Perform a case insensitive compare if they are a known scheme type. */
471 if(known_scheme) {
472 if(StrCmpNIW(a->canon_uri+a->host_start, b->canon_uri+b->host_start, a->host_len))
473 return FALSE;
474 } else if(StrCmpNW(a->canon_uri+a->host_start, b->canon_uri+b->host_start, a->host_len))
475 return FALSE;
476 } else if(a->host_len != b->host_len)
477 /* One of the Uri's had a host, while the other one didn't. */
478 return FALSE;
480 if(a->has_port && b->has_port) {
481 if(a->port != b->port)
482 return FALSE;
483 } else if(a->has_port || b->has_port)
484 /* One had a port, while the other one didn't. */
485 return FALSE;
487 /* Windows is weird with how it handles paths. For example
488 * One URI could be "http://google.com" (after canonicalization)
489 * and one could be "http://google.com/" and the IsEqual function
490 * would still evaluate to TRUE, but, only if they are both hierarchical
491 * URIs.
493 if((a->path_start > -1 && b->path_start > -1) &&
494 (a->path_len == b->path_len)) {
495 if(StrCmpNW(a->canon_uri+a->path_start, b->canon_uri+b->path_start, a->path_len))
496 return FALSE;
497 } else if(are_hierarchical && a->path_len == -1 && b->path_len == 0) {
498 if(*(a->canon_uri+a->path_start) != '/')
499 return FALSE;
500 } else if(are_hierarchical && b->path_len == 1 && a->path_len == 0) {
501 if(*(b->canon_uri+b->path_start) != '/')
502 return FALSE;
503 } else if(a->path_len != b->path_len)
504 return FALSE;
506 /* Compare the query strings of the two URIs. */
507 if((a->query_start > -1 && b->query_start > -1) &&
508 (a->query_len == b->query_len)) {
509 if(StrCmpNW(a->canon_uri+a->query_start, b->canon_uri+b->query_start, a->query_len))
510 return FALSE;
511 } else if(a->query_len != b->query_len)
512 return FALSE;
514 if((a->fragment_start > -1 && b->fragment_start > -1) &&
515 (a->fragment_len == b->fragment_len)) {
516 if(StrCmpNW(a->canon_uri+a->fragment_start, b->canon_uri+b->fragment_start, a->fragment_len))
517 return FALSE;
518 } else if(a->fragment_len != b->fragment_len)
519 return FALSE;
521 /* If we get here, the two URIs are equivalent. */
522 return TRUE;
525 return FALSE;
528 /* Computes the size of the given IPv6 address.
529 * Each h16 component is 16bits, if there is an IPv4 address, it's
530 * 32bits. If there's an elision it can be 16bits to 128bits, depending
531 * on the number of other components.
533 * Modeled after google-url's CheckIPv6ComponentsSize function
535 static void compute_ipv6_comps_size(ipv6_address *address) {
536 address->components_size = address->h16_count * 2;
538 if(address->ipv4)
539 /* IPv4 address is 4 bytes. */
540 address->components_size += 4;
542 if(address->elision) {
543 /* An elision can be anywhere from 2 bytes up to 16 bytes.
544 * It size depends on the size of the h16 and IPv4 components.
546 address->elision_size = 16 - address->components_size;
547 if(address->elision_size < 2)
548 address->elision_size = 2;
549 } else
550 address->elision_size = 0;
553 /* Taken from dlls/jscript/lex.c */
554 static int hex_to_int(WCHAR val) {
555 if(val >= '0' && val <= '9')
556 return val - '0';
557 else if(val >= 'a' && val <= 'f')
558 return val - 'a' + 10;
559 else if(val >= 'A' && val <= 'F')
560 return val - 'A' + 10;
562 return -1;
565 /* Helper function for converting a percent encoded string
566 * representation of a WCHAR value into its actual WCHAR value. If
567 * the two characters following the '%' aren't valid hex values then
568 * this function returns the NULL character.
570 * Eg.
571 * "%2E" will result in '.' being returned by this function.
573 static WCHAR decode_pct_val(const WCHAR *ptr) {
574 WCHAR ret = '\0';
576 if(*ptr == '%' && is_hexdigit(*(ptr + 1)) && is_hexdigit(*(ptr + 2))) {
577 INT a = hex_to_int(*(ptr + 1));
578 INT b = hex_to_int(*(ptr + 2));
580 ret = a << 4;
581 ret += b;
584 return ret;
587 /* Helper function for percent encoding a given character
588 * and storing the encoded value into a given buffer (dest).
590 * It's up to the calling function to ensure that there is
591 * at least enough space in 'dest' for the percent encoded
592 * value to be stored (so dest + 3 spaces available).
594 static inline void pct_encode_val(WCHAR val, WCHAR *dest) {
595 dest[0] = '%';
596 dest[1] = hexDigits[(val >> 4) & 0xf];
597 dest[2] = hexDigits[val & 0xf];
600 /* Attempts to parse the domain name from the host.
602 * This function also includes the Top-level Domain (TLD) name
603 * of the host when it tries to find the domain name. If it finds
604 * a valid domain name it will assign 'domain_start' the offset
605 * into 'host' where the domain name starts.
607 * It's implied that if there is a domain name its range is:
608 * [host+domain_start, host+host_len).
610 void find_domain_name(const WCHAR *host, DWORD host_len,
611 INT *domain_start) {
612 const WCHAR *last_tld, *sec_last_tld, *end;
614 end = host+host_len-1;
616 *domain_start = -1;
618 /* There has to be at least enough room for a '.' followed by a
619 * 3 character TLD for a domain to even exist in the host name.
621 if(host_len < 4)
622 return;
624 last_tld = memrchrW(host, '.', host_len);
625 if(!last_tld)
626 /* http://hostname -> has no domain name. */
627 return;
629 sec_last_tld = memrchrW(host, '.', last_tld-host);
630 if(!sec_last_tld) {
631 /* If the '.' is at the beginning of the host there
632 * has to be at least 3 characters in the TLD for it
633 * to be valid.
634 * Ex: .com -> .com as the domain name.
635 * .co -> has no domain name.
637 if(last_tld-host == 0) {
638 if(end-(last_tld-1) < 3)
639 return;
640 } else if(last_tld-host == 3) {
641 DWORD i;
643 /* If there's three characters in front of last_tld and
644 * they are on the list of recognized TLDs, then this
645 * host doesn't have a domain (since the host only contains
646 * a TLD name.
647 * Ex: edu.uk -> has no domain name.
648 * foo.uk -> foo.uk as the domain name.
650 for(i = 0; i < sizeof(recognized_tlds)/sizeof(recognized_tlds[0]); ++i) {
651 if(!StrCmpNIW(host, recognized_tlds[i].tld_name, 3))
652 return;
654 } else if(last_tld-host < 3)
655 /* Anything less than 3 characters is considered part
656 * of the TLD name.
657 * Ex: ak.uk -> Has no domain name.
659 return;
661 /* Otherwise the domain name is the whole host name. */
662 *domain_start = 0;
663 } else if(end+1-last_tld > 3) {
664 /* If the last_tld has more than 3 characters, then it's automatically
665 * considered the TLD of the domain name.
666 * Ex: www.winehq.org.uk.test -> uk.test as the domain name.
668 *domain_start = (sec_last_tld+1)-host;
669 } else if(last_tld - (sec_last_tld+1) < 4) {
670 DWORD i;
671 /* If the sec_last_tld is 3 characters long it HAS to be on the list of
672 * recognized to still be considered part of the TLD name, otherwise
673 * its considered the domain name.
674 * Ex: www.google.com.uk -> google.com.uk as the domain name.
675 * www.google.foo.uk -> foo.uk as the domain name.
677 if(last_tld - (sec_last_tld+1) == 3) {
678 for(i = 0; i < sizeof(recognized_tlds)/sizeof(recognized_tlds[0]); ++i) {
679 if(!StrCmpNIW(sec_last_tld+1, recognized_tlds[i].tld_name, 3)) {
680 const WCHAR *domain = memrchrW(host, '.', sec_last_tld-host);
682 if(!domain)
683 *domain_start = 0;
684 else
685 *domain_start = (domain+1) - host;
686 TRACE("Found domain name %s\n", debugstr_wn(host+*domain_start,
687 (host+host_len)-(host+*domain_start)));
688 return;
692 *domain_start = (sec_last_tld+1)-host;
693 } else {
694 /* Since the sec_last_tld is less than 3 characters it's considered
695 * part of the TLD.
696 * Ex: www.google.fo.uk -> google.fo.uk as the domain name.
698 const WCHAR *domain = memrchrW(host, '.', sec_last_tld-host);
700 if(!domain)
701 *domain_start = 0;
702 else
703 *domain_start = (domain+1) - host;
705 } else {
706 /* The second to last TLD has more than 3 characters making it
707 * the domain name.
708 * Ex: www.google.test.us -> test.us as the domain name.
710 *domain_start = (sec_last_tld+1)-host;
713 TRACE("Found domain name %s\n", debugstr_wn(host+*domain_start,
714 (host+host_len)-(host+*domain_start)));
717 /* Removes the dot segments from a hierarchical URIs path component. This
718 * function performs the removal in place.
720 * This function returns the new length of the path string.
722 static DWORD remove_dot_segments(WCHAR *path, DWORD path_len) {
723 WCHAR *out = path;
724 const WCHAR *in = out;
725 const WCHAR *end = out + path_len;
726 DWORD len;
728 while(in < end) {
729 /* Move the first path segment in the input buffer to the end of
730 * the output buffer, and any subsequent characters up to, including
731 * the next "/" character (if any) or the end of the input buffer.
733 while(in < end && !is_slash(*in))
734 *out++ = *in++;
735 if(in == end)
736 break;
737 *out++ = *in++;
739 while(in < end) {
740 if(*in != '.')
741 break;
743 /* Handle ending "/." */
744 if(in + 1 == end) {
745 ++in;
746 break;
749 /* Handle "/./" */
750 if(is_slash(in[1])) {
751 in += 2;
752 continue;
755 /* If we don't have "/../" or ending "/.." */
756 if(in[1] != '.' || (in + 2 != end && !is_slash(in[2])))
757 break;
759 /* Find the slash preceding out pointer and move out pointer to it */
760 if(out > path+1 && is_slash(*--out))
761 --out;
762 while(out > path && !is_slash(*(--out)));
763 if(is_slash(*out))
764 ++out;
765 in += 2;
766 if(in != end)
767 ++in;
771 len = out - path;
772 TRACE("(%p %d): Path after dot segments removed %s len=%d\n", path, path_len,
773 debugstr_wn(path, len), len);
774 return len;
777 /* Attempts to find the file extension in a given path. */
778 static INT find_file_extension(const WCHAR *path, DWORD path_len) {
779 const WCHAR *end;
781 for(end = path+path_len-1; end >= path && *end != '/' && *end != '\\'; --end) {
782 if(*end == '.')
783 return end-path;
786 return -1;
789 /* Computes the location where the elision should occur in the IPv6
790 * address using the numerical values of each component stored in
791 * 'values'. If the address shouldn't contain an elision then 'index'
792 * is assigned -1 as it's value. Otherwise 'index' will contain the
793 * starting index (into values) where the elision should be, and 'count'
794 * will contain the number of cells the elision covers.
796 * NOTES:
797 * Windows will expand an elision if the elision only represents 1 h16
798 * component of the address.
800 * Ex: [1::2:3:4:5:6:7] -> [1:0:2:3:4:5:6:7]
802 * If the IPv6 address contains an IPv4 address, the IPv4 address is also
803 * considered for being included as part of an elision if all its components
804 * are zeros.
806 * Ex: [1:2:3:4:5:6:0.0.0.0] -> [1:2:3:4:5:6::]
808 static void compute_elision_location(const ipv6_address *address, const USHORT values[8],
809 INT *index, DWORD *count) {
810 DWORD i, max_len, cur_len;
811 INT max_index, cur_index;
813 max_len = cur_len = 0;
814 max_index = cur_index = -1;
815 for(i = 0; i < 8; ++i) {
816 BOOL check_ipv4 = (address->ipv4 && i == 6);
817 BOOL is_end = (check_ipv4 || i == 7);
819 if(check_ipv4) {
820 /* Check if the IPv4 address contains only zeros. */
821 if(values[i] == 0 && values[i+1] == 0) {
822 if(cur_index == -1)
823 cur_index = i;
825 cur_len += 2;
826 ++i;
828 } else if(values[i] == 0) {
829 if(cur_index == -1)
830 cur_index = i;
832 ++cur_len;
835 if(is_end || values[i] != 0) {
836 /* We only consider it for an elision if it's
837 * more than 1 component long.
839 if(cur_len > 1 && cur_len > max_len) {
840 /* Found the new elision location. */
841 max_len = cur_len;
842 max_index = cur_index;
845 /* Reset the current range for the next range of zeros. */
846 cur_index = -1;
847 cur_len = 0;
851 *index = max_index;
852 *count = max_len;
855 /* Removes all the leading and trailing white spaces or
856 * control characters from the URI and removes all control
857 * characters inside of the URI string.
859 static BSTR pre_process_uri(LPCWSTR uri) {
860 const WCHAR *start, *end, *ptr;
861 WCHAR *ptr2;
862 DWORD len;
863 BSTR ret;
865 start = uri;
866 /* Skip leading controls and whitespace. */
867 while(*start && (iscntrlW(*start) || isspaceW(*start))) ++start;
869 /* URI consisted only of control/whitespace. */
870 if(!*start)
871 return SysAllocStringLen(NULL, 0);
873 end = start + strlenW(start);
874 while(--end > start && (iscntrlW(*end) || isspaceW(*end)));
876 len = ++end - start;
877 for(ptr = start; ptr < end; ptr++) {
878 if(iscntrlW(*ptr))
879 len--;
882 ret = SysAllocStringLen(NULL, len);
883 if(!ret)
884 return NULL;
886 for(ptr = start, ptr2=ret; ptr < end; ptr++) {
887 if(!iscntrlW(*ptr))
888 *ptr2++ = *ptr;
891 return ret;
894 /* Converts the specified IPv4 address into an uint value.
896 * This function assumes that the IPv4 address has already been validated.
898 static UINT ipv4toui(const WCHAR *ip, DWORD len) {
899 UINT ret = 0;
900 DWORD comp_value = 0;
901 const WCHAR *ptr;
903 for(ptr = ip; ptr < ip+len; ++ptr) {
904 if(*ptr == '.') {
905 ret <<= 8;
906 ret += comp_value;
907 comp_value = 0;
908 } else
909 comp_value = comp_value*10 + (*ptr-'0');
912 ret <<= 8;
913 ret += comp_value;
915 return ret;
918 /* Converts an IPv4 address in numerical form into it's fully qualified
919 * string form. This function returns the number of characters written
920 * to 'dest'. If 'dest' is NULL this function will return the number of
921 * characters that would have been written.
923 * It's up to the caller to ensure there's enough space in 'dest' for the
924 * address.
926 static DWORD ui2ipv4(WCHAR *dest, UINT address) {
927 static const WCHAR formatW[] =
928 {'%','u','.','%','u','.','%','u','.','%','u',0};
929 DWORD ret = 0;
930 UCHAR digits[4];
932 digits[0] = (address >> 24) & 0xff;
933 digits[1] = (address >> 16) & 0xff;
934 digits[2] = (address >> 8) & 0xff;
935 digits[3] = address & 0xff;
937 if(!dest) {
938 WCHAR tmp[16];
939 ret = sprintfW(tmp, formatW, digits[0], digits[1], digits[2], digits[3]);
940 } else
941 ret = sprintfW(dest, formatW, digits[0], digits[1], digits[2], digits[3]);
943 return ret;
946 static DWORD ui2str(WCHAR *dest, UINT value) {
947 static const WCHAR formatW[] = {'%','u',0};
948 DWORD ret = 0;
950 if(!dest) {
951 WCHAR tmp[11];
952 ret = sprintfW(tmp, formatW, value);
953 } else
954 ret = sprintfW(dest, formatW, value);
956 return ret;
959 /* Converts an h16 component (from an IPv6 address) into it's
960 * numerical value.
962 * This function assumes that the h16 component has already been validated.
964 static USHORT h16tous(h16 component) {
965 DWORD i;
966 USHORT ret = 0;
968 for(i = 0; i < component.len; ++i) {
969 ret <<= 4;
970 ret += hex_to_int(component.str[i]);
973 return ret;
976 /* Converts an IPv6 address into its 128 bits (16 bytes) numerical value.
978 * This function assumes that the ipv6_address has already been validated.
980 static BOOL ipv6_to_number(const ipv6_address *address, USHORT number[8]) {
981 DWORD i, cur_component = 0;
982 BOOL already_passed_elision = FALSE;
984 for(i = 0; i < address->h16_count; ++i) {
985 if(address->elision) {
986 if(address->components[i].str > address->elision && !already_passed_elision) {
987 /* Means we just passed the elision and need to add its values to
988 * 'number' before we do anything else.
990 DWORD j = 0;
991 for(j = 0; j < address->elision_size; j+=2)
992 number[cur_component++] = 0;
994 already_passed_elision = TRUE;
998 number[cur_component++] = h16tous(address->components[i]);
1001 /* Case when the elision appears after the h16 components. */
1002 if(!already_passed_elision && address->elision) {
1003 for(i = 0; i < address->elision_size; i+=2)
1004 number[cur_component++] = 0;
1007 if(address->ipv4) {
1008 UINT value = ipv4toui(address->ipv4, address->ipv4_len);
1010 if(cur_component != 6) {
1011 ERR("(%p %p): Failed sanity check with %d\n", address, number, cur_component);
1012 return FALSE;
1015 number[cur_component++] = (value >> 16) & 0xffff;
1016 number[cur_component] = value & 0xffff;
1019 return TRUE;
1022 /* Checks if the characters pointed to by 'ptr' are
1023 * a percent encoded data octet.
1025 * pct-encoded = "%" HEXDIG HEXDIG
1027 static BOOL check_pct_encoded(const WCHAR **ptr) {
1028 const WCHAR *start = *ptr;
1030 if(**ptr != '%')
1031 return FALSE;
1033 ++(*ptr);
1034 if(!is_hexdigit(**ptr)) {
1035 *ptr = start;
1036 return FALSE;
1039 ++(*ptr);
1040 if(!is_hexdigit(**ptr)) {
1041 *ptr = start;
1042 return FALSE;
1045 ++(*ptr);
1046 return TRUE;
1049 /* dec-octet = DIGIT ; 0-9
1050 * / %x31-39 DIGIT ; 10-99
1051 * / "1" 2DIGIT ; 100-199
1052 * / "2" %x30-34 DIGIT ; 200-249
1053 * / "25" %x30-35 ; 250-255
1055 static BOOL check_dec_octet(const WCHAR **ptr) {
1056 const WCHAR *c1, *c2, *c3;
1058 c1 = *ptr;
1059 /* A dec-octet must be at least 1 digit long. */
1060 if(*c1 < '0' || *c1 > '9')
1061 return FALSE;
1063 ++(*ptr);
1065 c2 = *ptr;
1066 /* Since the 1 digit requirment was meet, it doesn't
1067 * matter if this is a DIGIT value, it's considered a
1068 * dec-octet.
1070 if(*c2 < '0' || *c2 > '9')
1071 return TRUE;
1073 ++(*ptr);
1075 c3 = *ptr;
1076 /* Same explanation as above. */
1077 if(*c3 < '0' || *c3 > '9')
1078 return TRUE;
1080 /* Anything > 255 isn't a valid IP dec-octet. */
1081 if(*c1 >= '2' && *c2 >= '5' && *c3 >= '5') {
1082 *ptr = c1;
1083 return FALSE;
1086 ++(*ptr);
1087 return TRUE;
1090 /* Checks if there is an implicit IPv4 address in the host component of the URI.
1091 * The max value of an implicit IPv4 address is UINT_MAX.
1093 * Ex:
1094 * "234567" would be considered an implicit IPv4 address.
1096 static BOOL check_implicit_ipv4(const WCHAR **ptr, UINT *val) {
1097 const WCHAR *start = *ptr;
1098 ULONGLONG ret = 0;
1099 *val = 0;
1101 while(is_num(**ptr)) {
1102 ret = ret*10 + (**ptr - '0');
1104 if(ret > UINT_MAX) {
1105 *ptr = start;
1106 return FALSE;
1108 ++(*ptr);
1111 if(*ptr == start)
1112 return FALSE;
1114 *val = ret;
1115 return TRUE;
1118 /* Checks if the string contains an IPv4 address.
1120 * This function has a strict mode or a non-strict mode of operation
1121 * When 'strict' is set to FALSE this function will return TRUE if
1122 * the string contains at least 'dec-octet "." dec-octet' since partial
1123 * IPv4 addresses will be normalized out into full IPv4 addresses. When
1124 * 'strict' is set this function expects there to be a full IPv4 address.
1126 * IPv4address = dec-octet "." dec-octet "." dec-octet "." dec-octet
1128 static BOOL check_ipv4address(const WCHAR **ptr, BOOL strict) {
1129 const WCHAR *start = *ptr;
1131 if(!check_dec_octet(ptr)) {
1132 *ptr = start;
1133 return FALSE;
1136 if(**ptr != '.') {
1137 *ptr = start;
1138 return FALSE;
1141 ++(*ptr);
1142 if(!check_dec_octet(ptr)) {
1143 *ptr = start;
1144 return FALSE;
1147 if(**ptr != '.') {
1148 if(strict) {
1149 *ptr = start;
1150 return FALSE;
1151 } else
1152 return TRUE;
1155 ++(*ptr);
1156 if(!check_dec_octet(ptr)) {
1157 *ptr = start;
1158 return FALSE;
1161 if(**ptr != '.') {
1162 if(strict) {
1163 *ptr = start;
1164 return FALSE;
1165 } else
1166 return TRUE;
1169 ++(*ptr);
1170 if(!check_dec_octet(ptr)) {
1171 *ptr = start;
1172 return FALSE;
1175 /* Found a four digit ip address. */
1176 return TRUE;
1178 /* Tries to parse the scheme name of the URI.
1180 * scheme = ALPHA *(ALPHA | NUM | '+' | '-' | '.') as defined by RFC 3896.
1181 * NOTE: Windows accepts a number as the first character of a scheme.
1183 static BOOL parse_scheme_name(const WCHAR **ptr, parse_data *data, DWORD extras) {
1184 const WCHAR *start = *ptr;
1186 data->scheme = NULL;
1187 data->scheme_len = 0;
1189 while(**ptr) {
1190 if(**ptr == '*' && *ptr == start) {
1191 /* Might have found a wildcard scheme. If it is the next
1192 * char has to be a ':' for it to be a valid URI
1194 ++(*ptr);
1195 break;
1196 } else if(!is_num(**ptr) && !is_alpha(**ptr) && **ptr != '+' &&
1197 **ptr != '-' && **ptr != '.')
1198 break;
1200 (*ptr)++;
1203 if(*ptr == start)
1204 return FALSE;
1206 /* Schemes must end with a ':' */
1207 if(**ptr != ':' && !((extras & ALLOW_NULL_TERM_SCHEME) && !**ptr)) {
1208 *ptr = start;
1209 return FALSE;
1212 data->scheme = start;
1213 data->scheme_len = *ptr - start;
1215 ++(*ptr);
1216 return TRUE;
1219 /* Tries to deduce the corresponding URL_SCHEME for the given URI. Stores
1220 * the deduced URL_SCHEME in data->scheme_type.
1222 static BOOL parse_scheme_type(parse_data *data) {
1223 /* If there's scheme data then see if it's a recognized scheme. */
1224 if(data->scheme && data->scheme_len) {
1225 DWORD i;
1227 for(i = 0; i < sizeof(recognized_schemes)/sizeof(recognized_schemes[0]); ++i) {
1228 if(lstrlenW(recognized_schemes[i].scheme_name) == data->scheme_len) {
1229 /* Has to be a case insensitive compare. */
1230 if(!StrCmpNIW(recognized_schemes[i].scheme_name, data->scheme, data->scheme_len)) {
1231 data->scheme_type = recognized_schemes[i].scheme;
1232 return TRUE;
1237 /* If we get here it means it's not a recognized scheme. */
1238 data->scheme_type = URL_SCHEME_UNKNOWN;
1239 return TRUE;
1240 } else if(data->is_relative) {
1241 /* Relative URI's have no scheme. */
1242 data->scheme_type = URL_SCHEME_UNKNOWN;
1243 return TRUE;
1244 } else {
1245 /* Should never reach here! what happened... */
1246 FIXME("(%p): Unable to determine scheme type for URI %s\n", data, debugstr_w(data->uri));
1247 return FALSE;
1251 /* Tries to parse (or deduce) the scheme_name of a URI. If it can't
1252 * parse a scheme from the URI it will try to deduce the scheme_name and scheme_type
1253 * using the flags specified in 'flags' (if any). Flags that affect how this function
1254 * operates are the Uri_CREATE_ALLOW_* flags.
1256 * All parsed/deduced information will be stored in 'data' when the function returns.
1258 * Returns TRUE if it was able to successfully parse the information.
1260 static BOOL parse_scheme(const WCHAR **ptr, parse_data *data, DWORD flags, DWORD extras) {
1261 static const WCHAR fileW[] = {'f','i','l','e',0};
1262 static const WCHAR wildcardW[] = {'*',0};
1264 /* First check to see if the uri could implicitly be a file path. */
1265 if(is_implicit_file_path(*ptr)) {
1266 if(flags & Uri_CREATE_ALLOW_IMPLICIT_FILE_SCHEME) {
1267 data->scheme = fileW;
1268 data->scheme_len = lstrlenW(fileW);
1269 data->has_implicit_scheme = TRUE;
1271 TRACE("(%p %p %x): URI is an implicit file path.\n", ptr, data, flags);
1272 } else {
1273 /* Window's does not consider anything that can implicitly be a file
1274 * path to be a valid URI if the ALLOW_IMPLICIT_FILE_SCHEME flag is not set...
1276 TRACE("(%p %p %x): URI is implicitly a file path, but, the ALLOW_IMPLICIT_FILE_SCHEME flag wasn't set.\n",
1277 ptr, data, flags);
1278 return FALSE;
1280 } else if(!parse_scheme_name(ptr, data, extras)) {
1281 /* No Scheme was found, this means it could be:
1282 * a) an implicit Wildcard scheme
1283 * b) a relative URI
1284 * c) a invalid URI.
1286 if(flags & Uri_CREATE_ALLOW_IMPLICIT_WILDCARD_SCHEME) {
1287 data->scheme = wildcardW;
1288 data->scheme_len = lstrlenW(wildcardW);
1289 data->has_implicit_scheme = TRUE;
1291 TRACE("(%p %p %x): URI is an implicit wildcard scheme.\n", ptr, data, flags);
1292 } else if (flags & Uri_CREATE_ALLOW_RELATIVE) {
1293 data->is_relative = TRUE;
1294 TRACE("(%p %p %x): URI is relative.\n", ptr, data, flags);
1295 } else {
1296 TRACE("(%p %p %x): Malformed URI found. Unable to deduce scheme name.\n", ptr, data, flags);
1297 return FALSE;
1301 if(!data->is_relative)
1302 TRACE("(%p %p %x): Found scheme=%s scheme_len=%d\n", ptr, data, flags,
1303 debugstr_wn(data->scheme, data->scheme_len), data->scheme_len);
1305 if(!parse_scheme_type(data))
1306 return FALSE;
1308 TRACE("(%p %p %x): Assigned %d as the URL_SCHEME.\n", ptr, data, flags, data->scheme_type);
1309 return TRUE;
1312 static BOOL parse_username(const WCHAR **ptr, parse_data *data, DWORD flags, DWORD extras) {
1313 data->username = *ptr;
1315 while(**ptr != ':' && **ptr != '@') {
1316 if(**ptr == '%') {
1317 if(!check_pct_encoded(ptr)) {
1318 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
1319 *ptr = data->username;
1320 data->username = NULL;
1321 return FALSE;
1323 } else
1324 continue;
1325 } else if(extras & ALLOW_NULL_TERM_USER_NAME && !**ptr)
1326 break;
1327 else if(is_auth_delim(**ptr, data->scheme_type != URL_SCHEME_UNKNOWN)) {
1328 *ptr = data->username;
1329 data->username = NULL;
1330 return FALSE;
1333 ++(*ptr);
1336 data->username_len = *ptr - data->username;
1337 return TRUE;
1340 static BOOL parse_password(const WCHAR **ptr, parse_data *data, DWORD flags, DWORD extras) {
1341 data->password = *ptr;
1343 while(**ptr != '@') {
1344 if(**ptr == '%') {
1345 if(!check_pct_encoded(ptr)) {
1346 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
1347 *ptr = data->password;
1348 data->password = NULL;
1349 return FALSE;
1351 } else
1352 continue;
1353 } else if(extras & ALLOW_NULL_TERM_PASSWORD && !**ptr)
1354 break;
1355 else if(is_auth_delim(**ptr, data->scheme_type != URL_SCHEME_UNKNOWN)) {
1356 *ptr = data->password;
1357 data->password = NULL;
1358 return FALSE;
1361 ++(*ptr);
1364 data->password_len = *ptr - data->password;
1365 return TRUE;
1368 /* Parses the userinfo part of the URI (if it exists). The userinfo field of
1369 * a URI can consist of "username:password@", or just "username@".
1371 * RFC def:
1372 * userinfo = *( unreserved / pct-encoded / sub-delims / ":" )
1374 * NOTES:
1375 * 1) If there is more than one ':' in the userinfo part of the URI Windows
1376 * uses the first occurrence of ':' to delimit the username and password
1377 * components.
1379 * ex:
1380 * ftp://user:pass:word@winehq.org
1382 * Would yield, "user" as the username and "pass:word" as the password.
1384 * 2) Windows allows any character to appear in the "userinfo" part of
1385 * a URI, as long as it's not an authority delimeter character set.
1387 static void parse_userinfo(const WCHAR **ptr, parse_data *data, DWORD flags) {
1388 const WCHAR *start = *ptr;
1390 if(!parse_username(ptr, data, flags, 0)) {
1391 TRACE("(%p %p %x): URI contained no userinfo.\n", ptr, data, flags);
1392 return;
1395 if(**ptr == ':') {
1396 ++(*ptr);
1397 if(!parse_password(ptr, data, flags, 0)) {
1398 *ptr = start;
1399 data->username = NULL;
1400 data->username_len = 0;
1401 TRACE("(%p %p %x): URI contained no userinfo.\n", ptr, data, flags);
1402 return;
1406 if(**ptr != '@') {
1407 *ptr = start;
1408 data->username = NULL;
1409 data->username_len = 0;
1410 data->password = NULL;
1411 data->password_len = 0;
1413 TRACE("(%p %p %x): URI contained no userinfo.\n", ptr, data, flags);
1414 return;
1417 if(data->username)
1418 TRACE("(%p %p %x): Found username %s len=%d.\n", ptr, data, flags,
1419 debugstr_wn(data->username, data->username_len), data->username_len);
1421 if(data->password)
1422 TRACE("(%p %p %x): Found password %s len=%d.\n", ptr, data, flags,
1423 debugstr_wn(data->password, data->password_len), data->password_len);
1425 ++(*ptr);
1428 /* Attempts to parse a port from the URI.
1430 * NOTES:
1431 * Windows seems to have a cap on what the maximum value
1432 * for a port can be. The max value is USHORT_MAX.
1434 * port = *DIGIT
1436 static BOOL parse_port(const WCHAR **ptr, parse_data *data, DWORD flags) {
1437 UINT port = 0;
1438 data->port = *ptr;
1440 while(!is_auth_delim(**ptr, data->scheme_type != URL_SCHEME_UNKNOWN)) {
1441 if(!is_num(**ptr)) {
1442 *ptr = data->port;
1443 data->port = NULL;
1444 return FALSE;
1447 port = port*10 + (**ptr-'0');
1449 if(port > USHORT_MAX) {
1450 *ptr = data->port;
1451 data->port = NULL;
1452 return FALSE;
1455 ++(*ptr);
1458 data->has_port = TRUE;
1459 data->port_value = port;
1460 data->port_len = *ptr - data->port;
1462 TRACE("(%p %p %x): Found port %s len=%d value=%u\n", ptr, data, flags,
1463 debugstr_wn(data->port, data->port_len), data->port_len, data->port_value);
1464 return TRUE;
1467 /* Attempts to parse a IPv4 address from the URI.
1469 * NOTES:
1470 * Window's normalizes IPv4 addresses, This means there's three
1471 * possibilities for the URI to contain an IPv4 address.
1472 * 1) A well formed address (ex. 192.2.2.2).
1473 * 2) A partially formed address. For example "192.0" would
1474 * normalize to "192.0.0.0" during canonicalization.
1475 * 3) An implicit IPv4 address. For example "256" would
1476 * normalize to "0.0.1.0" during canonicalization. Also
1477 * note that the maximum value for an implicit IP address
1478 * is UINT_MAX, if the value in the URI exceeds this then
1479 * it is not considered an IPv4 address.
1481 static BOOL parse_ipv4address(const WCHAR **ptr, parse_data *data, DWORD flags) {
1482 const BOOL is_unknown = data->scheme_type == URL_SCHEME_UNKNOWN;
1483 data->host = *ptr;
1485 if(!check_ipv4address(ptr, FALSE)) {
1486 if(!check_implicit_ipv4(ptr, &data->implicit_ipv4)) {
1487 TRACE("(%p %p %x): URI didn't contain anything looking like an IPv4 address.\n",
1488 ptr, data, flags);
1489 *ptr = data->host;
1490 data->host = NULL;
1491 return FALSE;
1492 } else
1493 data->has_implicit_ip = TRUE;
1496 /* Check if what we found is the only part of the host name (if it isn't
1497 * we don't have an IPv4 address).
1499 if(**ptr == ':') {
1500 ++(*ptr);
1501 if(!parse_port(ptr, data, flags)) {
1502 *ptr = data->host;
1503 data->host = NULL;
1504 return FALSE;
1506 } else if(!is_auth_delim(**ptr, !is_unknown)) {
1507 /* Found more data which belongs the host, so this isn't an IPv4. */
1508 *ptr = data->host;
1509 data->host = NULL;
1510 data->has_implicit_ip = FALSE;
1511 return FALSE;
1514 data->host_len = *ptr - data->host;
1515 data->host_type = Uri_HOST_IPV4;
1517 TRACE("(%p %p %x): IPv4 address found. host=%s host_len=%d host_type=%d\n",
1518 ptr, data, flags, debugstr_wn(data->host, data->host_len),
1519 data->host_len, data->host_type);
1520 return TRUE;
1523 /* Attempts to parse the reg-name from the URI.
1525 * Because of the way Windows handles ':' this function also
1526 * handles parsing the port.
1528 * reg-name = *( unreserved / pct-encoded / sub-delims )
1530 * NOTE:
1531 * Windows allows everything, but, the characters in "auth_delims" and ':'
1532 * to appear in a reg-name, unless it's an unknown scheme type then ':' is
1533 * allowed to appear (even if a valid port isn't after it).
1535 * Windows doesn't like host names which start with '[' and end with ']'
1536 * and don't contain a valid IP literal address in between them.
1538 * On Windows if an '[' is encountered in the host name the ':' no longer
1539 * counts as a delimiter until you reach the next ']' or an "authority delimeter".
1541 * A reg-name CAN be empty.
1543 static BOOL parse_reg_name(const WCHAR **ptr, parse_data *data, DWORD flags, DWORD extras) {
1544 const BOOL has_start_bracket = **ptr == '[';
1545 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
1546 const BOOL is_res = data->scheme_type == URL_SCHEME_RES;
1547 BOOL inside_brackets = has_start_bracket;
1549 /* res URIs don't have ports. */
1550 BOOL ignore_col = (extras & IGNORE_PORT_DELIMITER) || is_res;
1552 /* We have to be careful with file schemes. */
1553 if(data->scheme_type == URL_SCHEME_FILE) {
1554 /* This is because an implicit file scheme could be "C:\\test" and it
1555 * would trick this function into thinking the host is "C", when after
1556 * canonicalization the host would end up being an empty string. A drive
1557 * path can also have a '|' instead of a ':' after the drive letter.
1559 if(is_drive_path(*ptr)) {
1560 /* Regular old drive paths don't have a host type (or host name). */
1561 data->host_type = Uri_HOST_UNKNOWN;
1562 data->host = *ptr;
1563 data->host_len = 0;
1564 return TRUE;
1565 } else if(is_unc_path(*ptr))
1566 /* Skip past the "\\" of a UNC path. */
1567 *ptr += 2;
1570 data->host = *ptr;
1572 /* For res URIs, everything before the first '/' is
1573 * considered the host.
1575 while((!is_res && !is_auth_delim(**ptr, known_scheme)) ||
1576 (is_res && **ptr && **ptr != '/')) {
1577 if(**ptr == ':' && !ignore_col) {
1578 /* We can ignore ':' if were inside brackets.*/
1579 if(!inside_brackets) {
1580 const WCHAR *tmp = (*ptr)++;
1582 /* Attempt to parse the port. */
1583 if(!parse_port(ptr, data, flags)) {
1584 /* Windows expects there to be a valid port for known scheme types. */
1585 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
1586 *ptr = data->host;
1587 data->host = NULL;
1588 TRACE("(%p %p %x %x): Expected valid port\n", ptr, data, flags, extras);
1589 return FALSE;
1590 } else
1591 /* Windows gives up on trying to parse a port when it
1592 * encounters 1 invalid port.
1594 ignore_col = TRUE;
1595 } else {
1596 data->host_len = tmp - data->host;
1597 break;
1600 } else if(**ptr == '%' && (known_scheme && !is_res)) {
1601 /* Has to be a legit % encoded value. */
1602 if(!check_pct_encoded(ptr)) {
1603 *ptr = data->host;
1604 data->host = NULL;
1605 return FALSE;
1606 } else
1607 continue;
1608 } else if(is_res && is_forbidden_dos_path_char(**ptr)) {
1609 *ptr = data->host;
1610 data->host = NULL;
1611 return FALSE;
1612 } else if(**ptr == ']')
1613 inside_brackets = FALSE;
1614 else if(**ptr == '[')
1615 inside_brackets = TRUE;
1617 ++(*ptr);
1620 if(has_start_bracket) {
1621 /* Make sure the last character of the host wasn't a ']'. */
1622 if(*(*ptr-1) == ']') {
1623 TRACE("(%p %p %x %x): Expected an IP literal inside of the host\n",
1624 ptr, data, flags, extras);
1625 *ptr = data->host;
1626 data->host = NULL;
1627 return FALSE;
1631 /* Don't overwrite our length if we found a port earlier. */
1632 if(!data->port)
1633 data->host_len = *ptr - data->host;
1635 /* If the host is empty, then it's an unknown host type. */
1636 if(data->host_len == 0 || is_res)
1637 data->host_type = Uri_HOST_UNKNOWN;
1638 else
1639 data->host_type = Uri_HOST_DNS;
1641 TRACE("(%p %p %x %x): Parsed reg-name. host=%s len=%d\n", ptr, data, flags, extras,
1642 debugstr_wn(data->host, data->host_len), data->host_len);
1643 return TRUE;
1646 /* Attempts to parse an IPv6 address out of the URI.
1648 * IPv6address = 6( h16 ":" ) ls32
1649 * / "::" 5( h16 ":" ) ls32
1650 * / [ h16 ] "::" 4( h16 ":" ) ls32
1651 * / [ *1( h16 ":" ) h16 ] "::" 3( h16 ":" ) ls32
1652 * / [ *2( h16 ":" ) h16 ] "::" 2( h16 ":" ) ls32
1653 * / [ *3( h16 ":" ) h16 ] "::" h16 ":" ls32
1654 * / [ *4( h16 ":" ) h16 ] "::" ls32
1655 * / [ *5( h16 ":" ) h16 ] "::" h16
1656 * / [ *6( h16 ":" ) h16 ] "::"
1658 * ls32 = ( h16 ":" h16 ) / IPv4address
1659 * ; least-significant 32 bits of address.
1661 * h16 = 1*4HEXDIG
1662 * ; 16 bits of address represented in hexadecimal.
1664 * Modeled after google-url's 'DoParseIPv6' function.
1666 static BOOL parse_ipv6address(const WCHAR **ptr, parse_data *data, DWORD flags) {
1667 const WCHAR *start, *cur_start;
1668 ipv6_address ip;
1670 start = cur_start = *ptr;
1671 memset(&ip, 0, sizeof(ipv6_address));
1673 for(;; ++(*ptr)) {
1674 /* Check if we're on the last character of the host. */
1675 BOOL is_end = (is_auth_delim(**ptr, data->scheme_type != URL_SCHEME_UNKNOWN)
1676 || **ptr == ']');
1678 BOOL is_split = (**ptr == ':');
1679 BOOL is_elision = (is_split && !is_end && *(*ptr+1) == ':');
1681 /* Check if we're at the end of a component, or
1682 * if we're at the end of the IPv6 address.
1684 if(is_split || is_end) {
1685 DWORD cur_len = 0;
1687 cur_len = *ptr - cur_start;
1689 /* h16 can't have a length > 4. */
1690 if(cur_len > 4) {
1691 *ptr = start;
1693 TRACE("(%p %p %x): h16 component to long.\n",
1694 ptr, data, flags);
1695 return FALSE;
1698 if(cur_len == 0) {
1699 /* An h16 component can't have the length of 0 unless
1700 * the elision is at the beginning of the address, or
1701 * at the end of the address.
1703 if(!((*ptr == start && is_elision) ||
1704 (is_end && (*ptr-2) == ip.elision))) {
1705 *ptr = start;
1706 TRACE("(%p %p %x): IPv6 component cannot have a length of 0.\n",
1707 ptr, data, flags);
1708 return FALSE;
1712 if(cur_len > 0) {
1713 /* An IPv6 address can have no more than 8 h16 components. */
1714 if(ip.h16_count >= 8) {
1715 *ptr = start;
1716 TRACE("(%p %p %x): Not a IPv6 address, to many h16 components.\n",
1717 ptr, data, flags);
1718 return FALSE;
1721 ip.components[ip.h16_count].str = cur_start;
1722 ip.components[ip.h16_count].len = cur_len;
1724 TRACE("(%p %p %x): Found h16 component %s, len=%d, h16_count=%d\n",
1725 ptr, data, flags, debugstr_wn(cur_start, cur_len), cur_len,
1726 ip.h16_count);
1727 ++ip.h16_count;
1731 if(is_end)
1732 break;
1734 if(is_elision) {
1735 /* A IPv6 address can only have 1 elision ('::'). */
1736 if(ip.elision) {
1737 *ptr = start;
1739 TRACE("(%p %p %x): IPv6 address cannot have 2 elisions.\n",
1740 ptr, data, flags);
1741 return FALSE;
1744 ip.elision = *ptr;
1745 ++(*ptr);
1748 if(is_split)
1749 cur_start = *ptr+1;
1750 else {
1751 if(!check_ipv4address(ptr, TRUE)) {
1752 if(!is_hexdigit(**ptr)) {
1753 /* Not a valid character for an IPv6 address. */
1754 *ptr = start;
1755 return FALSE;
1757 } else {
1758 /* Found an IPv4 address. */
1759 ip.ipv4 = cur_start;
1760 ip.ipv4_len = *ptr - cur_start;
1762 TRACE("(%p %p %x): Found an attached IPv4 address %s len=%d.\n",
1763 ptr, data, flags, debugstr_wn(ip.ipv4, ip.ipv4_len),
1764 ip.ipv4_len);
1766 /* IPv4 addresses can only appear at the end of a IPv6. */
1767 break;
1772 compute_ipv6_comps_size(&ip);
1774 /* Make sure the IPv6 address adds up to 16 bytes. */
1775 if(ip.components_size + ip.elision_size != 16) {
1776 *ptr = start;
1777 TRACE("(%p %p %x): Invalid IPv6 address, did not add up to 16 bytes.\n",
1778 ptr, data, flags);
1779 return FALSE;
1782 if(ip.elision_size == 2) {
1783 /* For some reason on Windows if an elision that represents
1784 * only 1 h16 component is encountered at the very begin or
1785 * end of an IPv6 address, Windows does not consider it a
1786 * valid IPv6 address.
1788 * Ex: [::2:3:4:5:6:7] is not valid, even though the sum
1789 * of all the components == 128bits.
1791 if(ip.elision < ip.components[0].str ||
1792 ip.elision > ip.components[ip.h16_count-1].str) {
1793 *ptr = start;
1794 TRACE("(%p %p %x): Invalid IPv6 address. Detected elision of 2 bytes at the beginning or end of the address.\n",
1795 ptr, data, flags);
1796 return FALSE;
1800 data->host_type = Uri_HOST_IPV6;
1801 data->has_ipv6 = TRUE;
1802 data->ipv6_address = ip;
1804 TRACE("(%p %p %x): Found valid IPv6 literal %s len=%d\n",
1805 ptr, data, flags, debugstr_wn(start, *ptr-start),
1806 (int)(*ptr-start));
1807 return TRUE;
1810 /* IPvFuture = "v" 1*HEXDIG "." 1*( unreserved / sub-delims / ":" ) */
1811 static BOOL parse_ipvfuture(const WCHAR **ptr, parse_data *data, DWORD flags) {
1812 const WCHAR *start = *ptr;
1814 /* IPvFuture has to start with a 'v' or 'V'. */
1815 if(**ptr != 'v' && **ptr != 'V')
1816 return FALSE;
1818 /* Following the v there must be at least 1 hex digit. */
1819 ++(*ptr);
1820 if(!is_hexdigit(**ptr)) {
1821 *ptr = start;
1822 return FALSE;
1825 ++(*ptr);
1826 while(is_hexdigit(**ptr))
1827 ++(*ptr);
1829 /* End of the hexdigit sequence must be a '.' */
1830 if(**ptr != '.') {
1831 *ptr = start;
1832 return FALSE;
1835 ++(*ptr);
1836 if(!is_unreserved(**ptr) && !is_subdelim(**ptr) && **ptr != ':') {
1837 *ptr = start;
1838 return FALSE;
1841 ++(*ptr);
1842 while(is_unreserved(**ptr) || is_subdelim(**ptr) || **ptr == ':')
1843 ++(*ptr);
1845 data->host_type = Uri_HOST_UNKNOWN;
1847 TRACE("(%p %p %x): Parsed IPvFuture address %s len=%d\n", ptr, data, flags,
1848 debugstr_wn(start, *ptr-start), (int)(*ptr-start));
1850 return TRUE;
1853 /* IP-literal = "[" ( IPv6address / IPvFuture ) "]" */
1854 static BOOL parse_ip_literal(const WCHAR **ptr, parse_data *data, DWORD flags, DWORD extras) {
1855 data->host = *ptr;
1857 if(**ptr != '[' && !(extras & ALLOW_BRACKETLESS_IP_LITERAL)) {
1858 data->host = NULL;
1859 return FALSE;
1860 } else if(**ptr == '[')
1861 ++(*ptr);
1863 if(!parse_ipv6address(ptr, data, flags)) {
1864 if(extras & SKIP_IP_FUTURE_CHECK || !parse_ipvfuture(ptr, data, flags)) {
1865 *ptr = data->host;
1866 data->host = NULL;
1867 return FALSE;
1871 if(**ptr != ']' && !(extras & ALLOW_BRACKETLESS_IP_LITERAL)) {
1872 *ptr = data->host;
1873 data->host = NULL;
1874 return FALSE;
1875 } else if(!**ptr && extras & ALLOW_BRACKETLESS_IP_LITERAL) {
1876 /* The IP literal didn't contain brackets and was followed by
1877 * a NULL terminator, so no reason to even check the port.
1879 data->host_len = *ptr - data->host;
1880 return TRUE;
1883 ++(*ptr);
1884 if(**ptr == ':') {
1885 ++(*ptr);
1886 /* If a valid port is not found, then let it trickle down to
1887 * parse_reg_name.
1889 if(!parse_port(ptr, data, flags)) {
1890 *ptr = data->host;
1891 data->host = NULL;
1892 return FALSE;
1894 } else
1895 data->host_len = *ptr - data->host;
1897 return TRUE;
1900 /* Parses the host information from the URI.
1902 * host = IP-literal / IPv4address / reg-name
1904 static BOOL parse_host(const WCHAR **ptr, parse_data *data, DWORD flags, DWORD extras) {
1905 if(!parse_ip_literal(ptr, data, flags, extras)) {
1906 if(!parse_ipv4address(ptr, data, flags)) {
1907 if(!parse_reg_name(ptr, data, flags, extras)) {
1908 TRACE("(%p %p %x %x): Malformed URI, Unknown host type.\n",
1909 ptr, data, flags, extras);
1910 return FALSE;
1915 return TRUE;
1918 /* Parses the authority information from the URI.
1920 * authority = [ userinfo "@" ] host [ ":" port ]
1922 static BOOL parse_authority(const WCHAR **ptr, parse_data *data, DWORD flags) {
1923 parse_userinfo(ptr, data, flags);
1925 /* Parsing the port will happen during one of the host parsing
1926 * routines (if the URI has a port).
1928 if(!parse_host(ptr, data, flags, 0))
1929 return FALSE;
1931 return TRUE;
1934 /* Attempts to parse the path information of a hierarchical URI. */
1935 static BOOL parse_path_hierarchical(const WCHAR **ptr, parse_data *data, DWORD flags) {
1936 const WCHAR *start = *ptr;
1937 static const WCHAR slash[] = {'/',0};
1938 const BOOL is_file = data->scheme_type == URL_SCHEME_FILE;
1940 if(is_path_delim(**ptr)) {
1941 if(data->scheme_type == URL_SCHEME_WILDCARD) {
1942 /* Wildcard schemes don't get a '/' attached if their path is
1943 * empty.
1945 data->path = NULL;
1946 data->path_len = 0;
1947 } else if(!(flags & Uri_CREATE_NO_CANONICALIZE)) {
1948 /* If the path component is empty, then a '/' is added. */
1949 data->path = slash;
1950 data->path_len = 1;
1952 } else {
1953 while(!is_path_delim(**ptr)) {
1954 if(**ptr == '%' && data->scheme_type != URL_SCHEME_UNKNOWN && !is_file) {
1955 if(!check_pct_encoded(ptr)) {
1956 *ptr = start;
1957 return FALSE;
1958 } else
1959 continue;
1960 } else if(is_forbidden_dos_path_char(**ptr) && is_file &&
1961 (flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
1962 /* File schemes with USE_DOS_PATH set aren't allowed to have
1963 * a '<' or '>' or '\"' appear in them.
1965 *ptr = start;
1966 return FALSE;
1967 } else if(**ptr == '\\') {
1968 /* Not allowed to have a backslash if NO_CANONICALIZE is set
1969 * and the scheme is known type (but not a file scheme).
1971 if(flags & Uri_CREATE_NO_CANONICALIZE) {
1972 if(data->scheme_type != URL_SCHEME_FILE &&
1973 data->scheme_type != URL_SCHEME_UNKNOWN) {
1974 *ptr = start;
1975 return FALSE;
1980 ++(*ptr);
1983 /* The only time a URI doesn't have a path is when
1984 * the NO_CANONICALIZE flag is set and the raw URI
1985 * didn't contain one.
1987 if(*ptr == start) {
1988 data->path = NULL;
1989 data->path_len = 0;
1990 } else {
1991 data->path = start;
1992 data->path_len = *ptr - start;
1996 if(data->path)
1997 TRACE("(%p %p %x): Parsed path %s len=%d\n", ptr, data, flags,
1998 debugstr_wn(data->path, data->path_len), data->path_len);
1999 else
2000 TRACE("(%p %p %x): The URI contained no path\n", ptr, data, flags);
2002 return TRUE;
2005 /* Parses the path of a opaque URI (much less strict then the parser
2006 * for a hierarchical URI).
2008 * NOTE:
2009 * Windows allows invalid % encoded data to appear in opaque URI paths
2010 * for unknown scheme types.
2012 * File schemes with USE_DOS_PATH set aren't allowed to have '<', '>', or '\"'
2013 * appear in them.
2015 static BOOL parse_path_opaque(const WCHAR **ptr, parse_data *data, DWORD flags) {
2016 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
2017 const BOOL is_file = data->scheme_type == URL_SCHEME_FILE;
2019 data->path = *ptr;
2021 while(!is_path_delim(**ptr)) {
2022 if(**ptr == '%' && known_scheme) {
2023 if(!check_pct_encoded(ptr)) {
2024 *ptr = data->path;
2025 data->path = NULL;
2026 return FALSE;
2027 } else
2028 continue;
2029 } else if(is_forbidden_dos_path_char(**ptr) && is_file &&
2030 (flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
2031 *ptr = data->path;
2032 data->path = NULL;
2033 return FALSE;
2036 ++(*ptr);
2039 data->path_len = *ptr - data->path;
2040 TRACE("(%p %p %x): Parsed opaque URI path %s len=%d\n", ptr, data, flags,
2041 debugstr_wn(data->path, data->path_len), data->path_len);
2042 return TRUE;
2045 /* Determines how the URI should be parsed after the scheme information.
2047 * If the scheme is followed, by "//" then, it is treated as an hierarchical URI
2048 * which then the authority and path information will be parsed out. Otherwise, the
2049 * URI will be treated as an opaque URI which the authority information is not parsed
2050 * out.
2052 * RFC 3896 definition of hier-part:
2054 * hier-part = "//" authority path-abempty
2055 * / path-absolute
2056 * / path-rootless
2057 * / path-empty
2059 * MSDN opaque URI definition:
2060 * scheme ":" path [ "#" fragment ]
2062 * NOTES:
2063 * If the URI is of an unknown scheme type and has a "//" following the scheme then it
2064 * is treated as a hierarchical URI, but, if the CREATE_NO_CRACK_UNKNOWN_SCHEMES flag is
2065 * set then it is considered an opaque URI reguardless of what follows the scheme information
2066 * (per MSDN documentation).
2068 static BOOL parse_hierpart(const WCHAR **ptr, parse_data *data, DWORD flags) {
2069 const WCHAR *start = *ptr;
2071 /* For javascript: URIs, simply set everything as a path */
2072 if(data->scheme_type == URL_SCHEME_JAVASCRIPT) {
2073 data->path = *ptr;
2074 data->path_len = strlenW(*ptr);
2075 data->is_opaque = TRUE;
2076 *ptr += data->path_len;
2077 return TRUE;
2080 /* Checks if the authority information needs to be parsed. */
2081 if(is_hierarchical_uri(ptr, data)) {
2082 /* Only treat it as a hierarchical URI if the scheme_type is known or
2083 * the Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES flag is not set.
2085 if(data->scheme_type != URL_SCHEME_UNKNOWN ||
2086 !(flags & Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES)) {
2087 TRACE("(%p %p %x): Treating URI as an hierarchical URI.\n", ptr, data, flags);
2088 data->is_opaque = FALSE;
2090 /* TODO: Handle hierarchical URI's, parse authority then parse the path. */
2091 if(!parse_authority(ptr, data, flags))
2092 return FALSE;
2094 return parse_path_hierarchical(ptr, data, flags);
2095 } else
2096 /* Reset ptr to it's starting position so opaque path parsing
2097 * begins at the correct location.
2099 *ptr = start;
2102 /* If it reaches here, then the URI will be treated as an opaque
2103 * URI.
2106 TRACE("(%p %p %x): Treating URI as an opaque URI.\n", ptr, data, flags);
2108 data->is_opaque = TRUE;
2109 if(!parse_path_opaque(ptr, data, flags))
2110 return FALSE;
2112 return TRUE;
2115 /* Attempts to parse the query string from the URI.
2117 * NOTES:
2118 * If NO_DECODE_EXTRA_INFO flag is set, then invalid percent encoded
2119 * data is allowed appear in the query string. For unknown scheme types
2120 * invalid percent encoded data is allowed to appear reguardless.
2122 static BOOL parse_query(const WCHAR **ptr, parse_data *data, DWORD flags) {
2123 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
2125 if(**ptr != '?') {
2126 TRACE("(%p %p %x): URI didn't contain a query string.\n", ptr, data, flags);
2127 return TRUE;
2130 data->query = *ptr;
2132 ++(*ptr);
2133 while(**ptr && **ptr != '#') {
2134 if(**ptr == '%' && known_scheme &&
2135 !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
2136 if(!check_pct_encoded(ptr)) {
2137 *ptr = data->query;
2138 data->query = NULL;
2139 return FALSE;
2140 } else
2141 continue;
2144 ++(*ptr);
2147 data->query_len = *ptr - data->query;
2149 TRACE("(%p %p %x): Parsed query string %s len=%d\n", ptr, data, flags,
2150 debugstr_wn(data->query, data->query_len), data->query_len);
2151 return TRUE;
2154 /* Attempts to parse the fragment from the URI.
2156 * NOTES:
2157 * If NO_DECODE_EXTRA_INFO flag is set, then invalid percent encoded
2158 * data is allowed appear in the query string. For unknown scheme types
2159 * invalid percent encoded data is allowed to appear reguardless.
2161 static BOOL parse_fragment(const WCHAR **ptr, parse_data *data, DWORD flags) {
2162 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
2164 if(**ptr != '#') {
2165 TRACE("(%p %p %x): URI didn't contain a fragment.\n", ptr, data, flags);
2166 return TRUE;
2169 data->fragment = *ptr;
2171 ++(*ptr);
2172 while(**ptr) {
2173 if(**ptr == '%' && known_scheme &&
2174 !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
2175 if(!check_pct_encoded(ptr)) {
2176 *ptr = data->fragment;
2177 data->fragment = NULL;
2178 return FALSE;
2179 } else
2180 continue;
2183 ++(*ptr);
2186 data->fragment_len = *ptr - data->fragment;
2188 TRACE("(%p %p %x): Parsed fragment %s len=%d\n", ptr, data, flags,
2189 debugstr_wn(data->fragment, data->fragment_len), data->fragment_len);
2190 return TRUE;
2193 /* Parses and validates the components of the specified by data->uri
2194 * and stores the information it parses into 'data'.
2196 * Returns TRUE if it successfully parsed the URI. False otherwise.
2198 static BOOL parse_uri(parse_data *data, DWORD flags) {
2199 const WCHAR *ptr;
2200 const WCHAR **pptr;
2202 ptr = data->uri;
2203 pptr = &ptr;
2205 TRACE("(%p %x): BEGINNING TO PARSE URI %s.\n", data, flags, debugstr_w(data->uri));
2207 if(!parse_scheme(pptr, data, flags, 0))
2208 return FALSE;
2210 if(!parse_hierpart(pptr, data, flags))
2211 return FALSE;
2213 if(!parse_query(pptr, data, flags))
2214 return FALSE;
2216 if(!parse_fragment(pptr, data, flags))
2217 return FALSE;
2219 TRACE("(%p %x): FINISHED PARSING URI.\n", data, flags);
2220 return TRUE;
2223 static BOOL canonicalize_username(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2224 const WCHAR *ptr;
2226 if(!data->username) {
2227 uri->userinfo_start = -1;
2228 return TRUE;
2231 uri->userinfo_start = uri->canon_len;
2232 for(ptr = data->username; ptr < data->username+data->username_len; ++ptr) {
2233 if(*ptr == '%') {
2234 /* Only decode % encoded values for known scheme types. */
2235 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
2236 /* See if the value really needs decoded. */
2237 WCHAR val = decode_pct_val(ptr);
2238 if(is_unreserved(val)) {
2239 if(!computeOnly)
2240 uri->canon_uri[uri->canon_len] = val;
2242 ++uri->canon_len;
2244 /* Move pass the hex characters. */
2245 ptr += 2;
2246 continue;
2249 } else if(!is_reserved(*ptr) && !is_unreserved(*ptr) && *ptr != '\\') {
2250 /* Only percent encode forbidden characters if the NO_ENCODE_FORBIDDEN_CHARACTERS flag
2251 * is NOT set.
2253 if(!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS)) {
2254 if(!computeOnly)
2255 pct_encode_val(*ptr, uri->canon_uri + uri->canon_len);
2257 uri->canon_len += 3;
2258 continue;
2262 if(!computeOnly)
2263 /* Nothing special, so just copy the character over. */
2264 uri->canon_uri[uri->canon_len] = *ptr;
2265 ++uri->canon_len;
2268 return TRUE;
2271 static BOOL canonicalize_password(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2272 const WCHAR *ptr;
2274 if(!data->password) {
2275 uri->userinfo_split = -1;
2276 return TRUE;
2279 if(uri->userinfo_start == -1)
2280 /* Has a password, but, doesn't have a username. */
2281 uri->userinfo_start = uri->canon_len;
2283 uri->userinfo_split = uri->canon_len - uri->userinfo_start;
2285 /* Add the ':' to the userinfo component. */
2286 if(!computeOnly)
2287 uri->canon_uri[uri->canon_len] = ':';
2288 ++uri->canon_len;
2290 for(ptr = data->password; ptr < data->password+data->password_len; ++ptr) {
2291 if(*ptr == '%') {
2292 /* Only decode % encoded values for known scheme types. */
2293 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
2294 /* See if the value really needs decoded. */
2295 WCHAR val = decode_pct_val(ptr);
2296 if(is_unreserved(val)) {
2297 if(!computeOnly)
2298 uri->canon_uri[uri->canon_len] = val;
2300 ++uri->canon_len;
2302 /* Move pass the hex characters. */
2303 ptr += 2;
2304 continue;
2307 } else if(!is_reserved(*ptr) && !is_unreserved(*ptr) && *ptr != '\\') {
2308 /* Only percent encode forbidden characters if the NO_ENCODE_FORBIDDEN_CHARACTERS flag
2309 * is NOT set.
2311 if(!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS)) {
2312 if(!computeOnly)
2313 pct_encode_val(*ptr, uri->canon_uri + uri->canon_len);
2315 uri->canon_len += 3;
2316 continue;
2320 if(!computeOnly)
2321 /* Nothing special, so just copy the character over. */
2322 uri->canon_uri[uri->canon_len] = *ptr;
2323 ++uri->canon_len;
2326 return TRUE;
2329 /* Canonicalizes the userinfo of the URI represented by the parse_data.
2331 * Canonicalization of the userinfo is a simple process. If there are any percent
2332 * encoded characters that fall in the "unreserved" character set, they are decoded
2333 * to their actual value. If a character is not in the "unreserved" or "reserved" sets
2334 * then it is percent encoded. Other than that the characters are copied over without
2335 * change.
2337 static BOOL canonicalize_userinfo(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2338 uri->userinfo_start = uri->userinfo_split = -1;
2339 uri->userinfo_len = 0;
2341 if(!data->username && !data->password)
2342 /* URI doesn't have userinfo, so nothing to do here. */
2343 return TRUE;
2345 if(!canonicalize_username(data, uri, flags, computeOnly))
2346 return FALSE;
2348 if(!canonicalize_password(data, uri, flags, computeOnly))
2349 return FALSE;
2351 uri->userinfo_len = uri->canon_len - uri->userinfo_start;
2352 if(!computeOnly)
2353 TRACE("(%p %p %x %d): Canonicalized userinfo, userinfo_start=%d, userinfo=%s, userinfo_split=%d userinfo_len=%d.\n",
2354 data, uri, flags, computeOnly, uri->userinfo_start, debugstr_wn(uri->canon_uri + uri->userinfo_start, uri->userinfo_len),
2355 uri->userinfo_split, uri->userinfo_len);
2357 /* Now insert the '@' after the userinfo. */
2358 if(!computeOnly)
2359 uri->canon_uri[uri->canon_len] = '@';
2360 ++uri->canon_len;
2362 return TRUE;
2365 /* Attempts to canonicalize a reg_name.
2367 * Things that happen:
2368 * 1) If Uri_CREATE_NO_CANONICALIZE flag is not set, then the reg_name is
2369 * lower cased. Unless it's an unknown scheme type, which case it's
2370 * no lower cased reguardless.
2372 * 2) Unreserved % encoded characters are decoded for known
2373 * scheme types.
2375 * 3) Forbidden characters are % encoded as long as
2376 * Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS flag is not set and
2377 * it isn't an unknown scheme type.
2379 * 4) If it's a file scheme and the host is "localhost" it's removed.
2381 * 5) If it's a file scheme and Uri_CREATE_FILE_USE_DOS_PATH is set,
2382 * then the UNC path characters are added before the host name.
2384 static BOOL canonicalize_reg_name(const parse_data *data, Uri *uri,
2385 DWORD flags, BOOL computeOnly) {
2386 static const WCHAR localhostW[] =
2387 {'l','o','c','a','l','h','o','s','t',0};
2388 const WCHAR *ptr;
2389 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
2391 if(data->scheme_type == URL_SCHEME_FILE &&
2392 data->host_len == lstrlenW(localhostW)) {
2393 if(!StrCmpNIW(data->host, localhostW, data->host_len)) {
2394 uri->host_start = -1;
2395 uri->host_len = 0;
2396 uri->host_type = Uri_HOST_UNKNOWN;
2397 return TRUE;
2401 if(data->scheme_type == URL_SCHEME_FILE && flags & Uri_CREATE_FILE_USE_DOS_PATH) {
2402 if(!computeOnly) {
2403 uri->canon_uri[uri->canon_len] = '\\';
2404 uri->canon_uri[uri->canon_len+1] = '\\';
2406 uri->canon_len += 2;
2407 uri->authority_start = uri->canon_len;
2410 uri->host_start = uri->canon_len;
2412 for(ptr = data->host; ptr < data->host+data->host_len; ++ptr) {
2413 if(*ptr == '%' && known_scheme) {
2414 WCHAR val = decode_pct_val(ptr);
2415 if(is_unreserved(val)) {
2416 /* If NO_CANONICALZE is not set, then windows lower cases the
2417 * decoded value.
2419 if(!(flags & Uri_CREATE_NO_CANONICALIZE) && isupperW(val)) {
2420 if(!computeOnly)
2421 uri->canon_uri[uri->canon_len] = tolowerW(val);
2422 } else {
2423 if(!computeOnly)
2424 uri->canon_uri[uri->canon_len] = val;
2426 ++uri->canon_len;
2428 /* Skip past the % encoded character. */
2429 ptr += 2;
2430 continue;
2431 } else {
2432 /* Just copy the % over. */
2433 if(!computeOnly)
2434 uri->canon_uri[uri->canon_len] = *ptr;
2435 ++uri->canon_len;
2437 } else if(*ptr == '\\') {
2438 /* Only unknown scheme types could have made it here with a '\\' in the host name. */
2439 if(!computeOnly)
2440 uri->canon_uri[uri->canon_len] = *ptr;
2441 ++uri->canon_len;
2442 } else if(!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS) &&
2443 !is_unreserved(*ptr) && !is_reserved(*ptr) && known_scheme) {
2444 if(!computeOnly) {
2445 pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
2447 /* The percent encoded value gets lower cased also. */
2448 if(!(flags & Uri_CREATE_NO_CANONICALIZE)) {
2449 uri->canon_uri[uri->canon_len+1] = tolowerW(uri->canon_uri[uri->canon_len+1]);
2450 uri->canon_uri[uri->canon_len+2] = tolowerW(uri->canon_uri[uri->canon_len+2]);
2454 uri->canon_len += 3;
2455 } else {
2456 if(!computeOnly) {
2457 if(!(flags & Uri_CREATE_NO_CANONICALIZE) && known_scheme)
2458 uri->canon_uri[uri->canon_len] = tolowerW(*ptr);
2459 else
2460 uri->canon_uri[uri->canon_len] = *ptr;
2463 ++uri->canon_len;
2467 uri->host_len = uri->canon_len - uri->host_start;
2469 if(!computeOnly)
2470 TRACE("(%p %p %x %d): Canonicalize reg_name=%s len=%d\n", data, uri, flags,
2471 computeOnly, debugstr_wn(uri->canon_uri+uri->host_start, uri->host_len),
2472 uri->host_len);
2474 if(!computeOnly)
2475 find_domain_name(uri->canon_uri+uri->host_start, uri->host_len,
2476 &(uri->domain_offset));
2478 return TRUE;
2481 /* Attempts to canonicalize an implicit IPv4 address. */
2482 static BOOL canonicalize_implicit_ipv4address(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2483 uri->host_start = uri->canon_len;
2485 TRACE("%u\n", data->implicit_ipv4);
2486 /* For unknown scheme types Window's doesn't convert
2487 * the value into an IP address, but, it still considers
2488 * it an IPv4 address.
2490 if(data->scheme_type == URL_SCHEME_UNKNOWN) {
2491 if(!computeOnly)
2492 memcpy(uri->canon_uri+uri->canon_len, data->host, data->host_len*sizeof(WCHAR));
2493 uri->canon_len += data->host_len;
2494 } else {
2495 if(!computeOnly)
2496 uri->canon_len += ui2ipv4(uri->canon_uri+uri->canon_len, data->implicit_ipv4);
2497 else
2498 uri->canon_len += ui2ipv4(NULL, data->implicit_ipv4);
2501 uri->host_len = uri->canon_len - uri->host_start;
2502 uri->host_type = Uri_HOST_IPV4;
2504 if(!computeOnly)
2505 TRACE("%p %p %x %d): Canonicalized implicit IP address=%s len=%d\n",
2506 data, uri, flags, computeOnly,
2507 debugstr_wn(uri->canon_uri+uri->host_start, uri->host_len),
2508 uri->host_len);
2510 return TRUE;
2513 /* Attempts to canonicalize an IPv4 address.
2515 * If the parse_data represents a URI that has an implicit IPv4 address
2516 * (ex. http://256/, this function will convert 256 into 0.0.1.0). If
2517 * the implicit IP address exceeds the value of UINT_MAX (maximum value
2518 * for an IPv4 address) it's canonicalized as if were a reg-name.
2520 * If the parse_data contains a partial or full IPv4 address it normalizes it.
2521 * A partial IPv4 address is something like "192.0" and would be normalized to
2522 * "192.0.0.0". With a full (or partial) IPv4 address like "192.002.01.003" would
2523 * be normalized to "192.2.1.3".
2525 * NOTES:
2526 * Window's ONLY normalizes IPv4 address for known scheme types (one that isn't
2527 * URL_SCHEME_UNKNOWN). For unknown scheme types, it simply copies the data from
2528 * the original URI into the canonicalized URI, but, it still recognizes URI's
2529 * host type as HOST_IPV4.
2531 static BOOL canonicalize_ipv4address(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2532 if(data->has_implicit_ip)
2533 return canonicalize_implicit_ipv4address(data, uri, flags, computeOnly);
2534 else {
2535 uri->host_start = uri->canon_len;
2537 /* Windows only normalizes for known scheme types. */
2538 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
2539 /* parse_data contains a partial or full IPv4 address, so normalize it. */
2540 DWORD i, octetDigitCount = 0, octetCount = 0;
2541 BOOL octetHasDigit = FALSE;
2543 for(i = 0; i < data->host_len; ++i) {
2544 if(data->host[i] == '0' && !octetHasDigit) {
2545 /* Can ignore leading zeros if:
2546 * 1) It isn't the last digit of the octet.
2547 * 2) i+1 != data->host_len
2548 * 3) i+1 != '.'
2550 if(octetDigitCount == 2 ||
2551 i+1 == data->host_len ||
2552 data->host[i+1] == '.') {
2553 if(!computeOnly)
2554 uri->canon_uri[uri->canon_len] = data->host[i];
2555 ++uri->canon_len;
2556 TRACE("Adding zero\n");
2558 } else if(data->host[i] == '.') {
2559 if(!computeOnly)
2560 uri->canon_uri[uri->canon_len] = data->host[i];
2561 ++uri->canon_len;
2563 octetDigitCount = 0;
2564 octetHasDigit = FALSE;
2565 ++octetCount;
2566 } else {
2567 if(!computeOnly)
2568 uri->canon_uri[uri->canon_len] = data->host[i];
2569 ++uri->canon_len;
2571 ++octetDigitCount;
2572 octetHasDigit = TRUE;
2576 /* Make sure the canonicalized IP address has 4 dec-octets.
2577 * If doesn't add "0" ones until there is 4;
2579 for( ; octetCount < 3; ++octetCount) {
2580 if(!computeOnly) {
2581 uri->canon_uri[uri->canon_len] = '.';
2582 uri->canon_uri[uri->canon_len+1] = '0';
2585 uri->canon_len += 2;
2587 } else {
2588 /* Windows doesn't normalize addresses in unknown schemes. */
2589 if(!computeOnly)
2590 memcpy(uri->canon_uri+uri->canon_len, data->host, data->host_len*sizeof(WCHAR));
2591 uri->canon_len += data->host_len;
2594 uri->host_len = uri->canon_len - uri->host_start;
2595 if(!computeOnly)
2596 TRACE("(%p %p %x %d): Canonicalized IPv4 address, ip=%s len=%d\n",
2597 data, uri, flags, computeOnly,
2598 debugstr_wn(uri->canon_uri+uri->host_start, uri->host_len),
2599 uri->host_len);
2602 return TRUE;
2605 /* Attempts to canonicalize the IPv6 address of the URI.
2607 * Multiple things happen during the canonicalization of an IPv6 address:
2608 * 1) Any leading zero's in an h16 component are removed.
2609 * Ex: [0001:0022::] -> [1:22::]
2611 * 2) The longest sequence of zero h16 components are compressed
2612 * into a "::" (elision). If there's a tie, the first is choosen.
2614 * Ex: [0:0:0:0:1:6:7:8] -> [::1:6:7:8]
2615 * [0:0:0:0:1:2::] -> [::1:2:0:0]
2616 * [0:0:1:2:0:0:7:8] -> [::1:2:0:0:7:8]
2618 * 3) If an IPv4 address is attached to the IPv6 address, it's
2619 * also normalized.
2620 * Ex: [::001.002.022.000] -> [::1.2.22.0]
2622 * 4) If an elision is present, but, only represents 1 h16 component
2623 * it's expanded.
2625 * Ex: [1::2:3:4:5:6:7] -> [1:0:2:3:4:5:6:7]
2627 * 5) If the IPv6 address contains an IPv4 address and there exists
2628 * at least 1 non-zero h16 component the IPv4 address is converted
2629 * into two h16 components, otherwise it's normalized and kept as is.
2631 * Ex: [::192.200.003.4] -> [::192.200.3.4]
2632 * [ffff::192.200.003.4] -> [ffff::c0c8:3041]
2634 * NOTE:
2635 * For unknown scheme types Windows simply copies the address over without any
2636 * changes.
2638 * IPv4 address can be included in an elision if all its components are 0's.
2640 static BOOL canonicalize_ipv6address(const parse_data *data, Uri *uri,
2641 DWORD flags, BOOL computeOnly) {
2642 uri->host_start = uri->canon_len;
2644 if(data->scheme_type == URL_SCHEME_UNKNOWN) {
2645 if(!computeOnly)
2646 memcpy(uri->canon_uri+uri->canon_len, data->host, data->host_len*sizeof(WCHAR));
2647 uri->canon_len += data->host_len;
2648 } else {
2649 USHORT values[8];
2650 INT elision_start;
2651 DWORD i, elision_len;
2653 if(!ipv6_to_number(&(data->ipv6_address), values)) {
2654 TRACE("(%p %p %x %d): Failed to compute numerical value for IPv6 address.\n",
2655 data, uri, flags, computeOnly);
2656 return FALSE;
2659 if(!computeOnly)
2660 uri->canon_uri[uri->canon_len] = '[';
2661 ++uri->canon_len;
2663 /* Find where the elision should occur (if any). */
2664 compute_elision_location(&(data->ipv6_address), values, &elision_start, &elision_len);
2666 TRACE("%p %p %x %d): Elision starts at %d, len=%u\n", data, uri, flags,
2667 computeOnly, elision_start, elision_len);
2669 for(i = 0; i < 8; ++i) {
2670 BOOL in_elision = (elision_start > -1 && i >= elision_start &&
2671 i < elision_start+elision_len);
2672 BOOL do_ipv4 = (i == 6 && data->ipv6_address.ipv4 && !in_elision &&
2673 data->ipv6_address.h16_count == 0);
2675 if(i == elision_start) {
2676 if(!computeOnly) {
2677 uri->canon_uri[uri->canon_len] = ':';
2678 uri->canon_uri[uri->canon_len+1] = ':';
2680 uri->canon_len += 2;
2683 /* We can ignore the current component if we're in the elision. */
2684 if(in_elision)
2685 continue;
2687 /* We only add a ':' if we're not at i == 0, or when we're at
2688 * the very end of elision range since the ':' colon was handled
2689 * earlier. Otherwise we would end up with ":::" after elision.
2691 if(i != 0 && !(elision_start > -1 && i == elision_start+elision_len)) {
2692 if(!computeOnly)
2693 uri->canon_uri[uri->canon_len] = ':';
2694 ++uri->canon_len;
2697 if(do_ipv4) {
2698 UINT val;
2699 DWORD len;
2701 /* Combine the two parts of the IPv4 address values. */
2702 val = values[i];
2703 val <<= 16;
2704 val += values[i+1];
2706 if(!computeOnly)
2707 len = ui2ipv4(uri->canon_uri+uri->canon_len, val);
2708 else
2709 len = ui2ipv4(NULL, val);
2711 uri->canon_len += len;
2712 ++i;
2713 } else {
2714 /* Write a regular h16 component to the URI. */
2716 /* Short circuit for the trivial case. */
2717 if(values[i] == 0) {
2718 if(!computeOnly)
2719 uri->canon_uri[uri->canon_len] = '0';
2720 ++uri->canon_len;
2721 } else {
2722 static const WCHAR formatW[] = {'%','x',0};
2724 if(!computeOnly)
2725 uri->canon_len += sprintfW(uri->canon_uri+uri->canon_len,
2726 formatW, values[i]);
2727 else {
2728 WCHAR tmp[5];
2729 uri->canon_len += sprintfW(tmp, formatW, values[i]);
2735 /* Add the closing ']'. */
2736 if(!computeOnly)
2737 uri->canon_uri[uri->canon_len] = ']';
2738 ++uri->canon_len;
2741 uri->host_len = uri->canon_len - uri->host_start;
2743 if(!computeOnly)
2744 TRACE("(%p %p %x %d): Canonicalized IPv6 address %s, len=%d\n", data, uri, flags,
2745 computeOnly, debugstr_wn(uri->canon_uri+uri->host_start, uri->host_len),
2746 uri->host_len);
2748 return TRUE;
2751 /* Attempts to canonicalize the host of the URI (if any). */
2752 static BOOL canonicalize_host(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2753 uri->host_start = -1;
2754 uri->host_len = 0;
2755 uri->domain_offset = -1;
2757 if(data->host) {
2758 switch(data->host_type) {
2759 case Uri_HOST_DNS:
2760 uri->host_type = Uri_HOST_DNS;
2761 if(!canonicalize_reg_name(data, uri, flags, computeOnly))
2762 return FALSE;
2764 break;
2765 case Uri_HOST_IPV4:
2766 uri->host_type = Uri_HOST_IPV4;
2767 if(!canonicalize_ipv4address(data, uri, flags, computeOnly))
2768 return FALSE;
2770 break;
2771 case Uri_HOST_IPV6:
2772 if(!canonicalize_ipv6address(data, uri, flags, computeOnly))
2773 return FALSE;
2775 uri->host_type = Uri_HOST_IPV6;
2776 break;
2777 case Uri_HOST_UNKNOWN:
2778 if(data->host_len > 0 || data->scheme_type != URL_SCHEME_FILE) {
2779 uri->host_start = uri->canon_len;
2781 /* Nothing happens to unknown host types. */
2782 if(!computeOnly)
2783 memcpy(uri->canon_uri+uri->canon_len, data->host, data->host_len*sizeof(WCHAR));
2784 uri->canon_len += data->host_len;
2785 uri->host_len = data->host_len;
2788 uri->host_type = Uri_HOST_UNKNOWN;
2789 break;
2790 default:
2791 FIXME("(%p %p %x %d): Canonicalization for host type %d not supported.\n", data,
2792 uri, flags, computeOnly, data->host_type);
2793 return FALSE;
2797 return TRUE;
2800 static BOOL canonicalize_port(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2801 BOOL has_default_port = FALSE;
2802 USHORT default_port = 0;
2803 DWORD i;
2805 uri->port_offset = -1;
2807 /* Check if the scheme has a default port. */
2808 for(i = 0; i < sizeof(default_ports)/sizeof(default_ports[0]); ++i) {
2809 if(default_ports[i].scheme == data->scheme_type) {
2810 has_default_port = TRUE;
2811 default_port = default_ports[i].port;
2812 break;
2816 uri->has_port = data->has_port || has_default_port;
2818 /* Possible cases:
2819 * 1) Has a port which is the default port.
2820 * 2) Has a port (not the default).
2821 * 3) Doesn't have a port, but, scheme has a default port.
2822 * 4) No port.
2824 if(has_default_port && data->has_port && data->port_value == default_port) {
2825 /* If it's the default port and this flag isn't set, don't do anything. */
2826 if(flags & Uri_CREATE_NO_CANONICALIZE) {
2827 uri->port_offset = uri->canon_len-uri->authority_start;
2828 if(!computeOnly)
2829 uri->canon_uri[uri->canon_len] = ':';
2830 ++uri->canon_len;
2832 if(data->port) {
2833 /* Copy the original port over. */
2834 if(!computeOnly)
2835 memcpy(uri->canon_uri+uri->canon_len, data->port, data->port_len*sizeof(WCHAR));
2836 uri->canon_len += data->port_len;
2837 } else {
2838 if(!computeOnly)
2839 uri->canon_len += ui2str(uri->canon_uri+uri->canon_len, data->port_value);
2840 else
2841 uri->canon_len += ui2str(NULL, data->port_value);
2845 uri->port = default_port;
2846 } else if(data->has_port) {
2847 uri->port_offset = uri->canon_len-uri->authority_start;
2848 if(!computeOnly)
2849 uri->canon_uri[uri->canon_len] = ':';
2850 ++uri->canon_len;
2852 if(flags & Uri_CREATE_NO_CANONICALIZE && data->port) {
2853 /* Copy the original over without changes. */
2854 if(!computeOnly)
2855 memcpy(uri->canon_uri+uri->canon_len, data->port, data->port_len*sizeof(WCHAR));
2856 uri->canon_len += data->port_len;
2857 } else {
2858 if(!computeOnly)
2859 uri->canon_len += ui2str(uri->canon_uri+uri->canon_len, data->port_value);
2860 else
2861 uri->canon_len += ui2str(NULL, data->port_value);
2864 uri->port = data->port_value;
2865 } else if(has_default_port)
2866 uri->port = default_port;
2868 return TRUE;
2871 /* Canonicalizes the authority of the URI represented by the parse_data. */
2872 static BOOL canonicalize_authority(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2873 uri->authority_start = uri->canon_len;
2874 uri->authority_len = 0;
2876 if(!canonicalize_userinfo(data, uri, flags, computeOnly))
2877 return FALSE;
2879 if(!canonicalize_host(data, uri, flags, computeOnly))
2880 return FALSE;
2882 if(!canonicalize_port(data, uri, flags, computeOnly))
2883 return FALSE;
2885 if(uri->host_start != -1 || (data->is_relative && (data->password || data->username)))
2886 uri->authority_len = uri->canon_len - uri->authority_start;
2887 else
2888 uri->authority_start = -1;
2890 return TRUE;
2893 /* Attempts to canonicalize the path of a hierarchical URI.
2895 * Things that happen:
2896 * 1). Forbidden characters are percent encoded, unless the NO_ENCODE_FORBIDDEN
2897 * flag is set or it's a file URI. Forbidden characters are always encoded
2898 * for file schemes reguardless and forbidden characters are never encoded
2899 * for unknown scheme types.
2901 * 2). For known scheme types '\\' are changed to '/'.
2903 * 3). Percent encoded, unreserved characters are decoded to their actual values.
2904 * Unless the scheme type is unknown. For file schemes any percent encoded
2905 * character in the unreserved or reserved set is decoded.
2907 * 4). For File schemes if the path is starts with a drive letter and doesn't
2908 * start with a '/' then one is appended.
2909 * Ex: file://c:/test.mp3 -> file:///c:/test.mp3
2911 * 5). Dot segments are removed from the path for all scheme types
2912 * unless NO_CANONICALIZE flag is set. Dot segments aren't removed
2913 * for wildcard scheme types.
2915 * NOTES:
2916 * file://c:/test%20test -> file:///c:/test%2520test
2917 * file://c:/test%3Etest -> file:///c:/test%253Etest
2918 * if Uri_CREATE_FILE_USE_DOS_PATH is not set:
2919 * file:///c:/test%20test -> file:///c:/test%20test
2920 * file:///c:/test%test -> file:///c:/test%25test
2922 static BOOL canonicalize_path_hierarchical(const parse_data *data, Uri *uri,
2923 DWORD flags, BOOL computeOnly) {
2924 const WCHAR *ptr;
2925 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
2926 const BOOL is_file = data->scheme_type == URL_SCHEME_FILE;
2927 const BOOL is_res = data->scheme_type == URL_SCHEME_RES;
2929 BOOL escape_pct = FALSE;
2931 if(!data->path) {
2932 uri->path_start = -1;
2933 uri->path_len = 0;
2934 return TRUE;
2937 uri->path_start = uri->canon_len;
2938 ptr = data->path;
2940 if(is_file && uri->host_start == -1) {
2941 /* Check if a '/' needs to be appended for the file scheme. */
2942 if(data->path_len > 1 && is_drive_path(ptr) && !(flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
2943 if(!computeOnly)
2944 uri->canon_uri[uri->canon_len] = '/';
2945 uri->canon_len++;
2946 escape_pct = TRUE;
2947 } else if(*ptr == '/') {
2948 if(!(flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
2949 /* Copy the extra '/' over. */
2950 if(!computeOnly)
2951 uri->canon_uri[uri->canon_len] = '/';
2952 ++uri->canon_len;
2954 ++ptr;
2957 if(is_drive_path(ptr)) {
2958 if(!computeOnly) {
2959 uri->canon_uri[uri->canon_len] = *ptr;
2960 /* If theres a '|' after the drive letter, convert it to a ':'. */
2961 uri->canon_uri[uri->canon_len+1] = ':';
2963 ptr += 2;
2964 uri->canon_len += 2;
2968 if(!is_file && *(data->path) && *(data->path) != '/') {
2969 /* Prepend a '/' to the path if it doesn't have one. */
2970 if(!computeOnly)
2971 uri->canon_uri[uri->canon_len] = '/';
2972 ++uri->canon_len;
2975 for(; ptr < data->path+data->path_len; ++ptr) {
2976 BOOL do_default_action = TRUE;
2978 if(*ptr == '%' && !is_res) {
2979 const WCHAR *tmp = ptr;
2980 WCHAR val;
2982 /* Check if the % represents a valid encoded char, or if it needs encoded. */
2983 BOOL force_encode = !check_pct_encoded(&tmp) && is_file && !(flags&Uri_CREATE_FILE_USE_DOS_PATH);
2984 val = decode_pct_val(ptr);
2986 if(force_encode || escape_pct) {
2987 /* Escape the percent sign in the file URI. */
2988 if(!computeOnly)
2989 pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
2990 uri->canon_len += 3;
2991 do_default_action = FALSE;
2992 } else if((is_unreserved(val) && known_scheme) ||
2993 (is_file && (is_unreserved(val) || is_reserved(val) ||
2994 (val && flags&Uri_CREATE_FILE_USE_DOS_PATH && !is_forbidden_dos_path_char(val))))) {
2995 if(!computeOnly)
2996 uri->canon_uri[uri->canon_len] = val;
2997 ++uri->canon_len;
2999 ptr += 2;
3000 continue;
3002 } else if(*ptr == '/' && is_file && (flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
3003 /* Convert the '/' back to a '\\'. */
3004 if(!computeOnly)
3005 uri->canon_uri[uri->canon_len] = '\\';
3006 ++uri->canon_len;
3007 do_default_action = FALSE;
3008 } else if(*ptr == '\\' && known_scheme) {
3009 if(!(is_file && (flags & Uri_CREATE_FILE_USE_DOS_PATH))) {
3010 /* Convert '\\' into a '/'. */
3011 if(!computeOnly)
3012 uri->canon_uri[uri->canon_len] = '/';
3013 ++uri->canon_len;
3014 do_default_action = FALSE;
3016 } else if(known_scheme && !is_res && !is_unreserved(*ptr) && !is_reserved(*ptr) &&
3017 (!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS) || is_file)) {
3018 if(!(is_file && (flags & Uri_CREATE_FILE_USE_DOS_PATH))) {
3019 /* Escape the forbidden character. */
3020 if(!computeOnly)
3021 pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
3022 uri->canon_len += 3;
3023 do_default_action = FALSE;
3027 if(do_default_action) {
3028 if(!computeOnly)
3029 uri->canon_uri[uri->canon_len] = *ptr;
3030 ++uri->canon_len;
3034 uri->path_len = uri->canon_len - uri->path_start;
3036 /* Removing the dot segments only happens when it's not in
3037 * computeOnly mode and it's not a wildcard scheme. File schemes
3038 * with USE_DOS_PATH set don't get dot segments removed.
3040 if(!(is_file && (flags & Uri_CREATE_FILE_USE_DOS_PATH)) &&
3041 data->scheme_type != URL_SCHEME_WILDCARD) {
3042 if(!(flags & Uri_CREATE_NO_CANONICALIZE) && !computeOnly) {
3043 /* Remove the dot segments (if any) and reset everything to the new
3044 * correct length.
3046 DWORD new_len = remove_dot_segments(uri->canon_uri+uri->path_start, uri->path_len);
3047 uri->canon_len -= uri->path_len-new_len;
3048 uri->path_len = new_len;
3052 if(!computeOnly)
3053 TRACE("Canonicalized path %s len=%d\n",
3054 debugstr_wn(uri->canon_uri+uri->path_start, uri->path_len),
3055 uri->path_len);
3057 return TRUE;
3060 /* Attempts to canonicalize the path for an opaque URI.
3062 * For known scheme types:
3063 * 1) forbidden characters are percent encoded if
3064 * NO_ENCODE_FORBIDDEN_CHARACTERS isn't set.
3066 * 2) Percent encoded, unreserved characters are decoded
3067 * to their actual values, for known scheme types.
3069 * 3) '\\' are changed to '/' for known scheme types
3070 * except for mailto schemes.
3072 * 4) For file schemes, if USE_DOS_PATH is set all '/'
3073 * are converted to backslashes.
3075 * 5) For file schemes, if USE_DOS_PATH isn't set all '\'
3076 * are converted to forward slashes.
3078 static BOOL canonicalize_path_opaque(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
3079 const WCHAR *ptr;
3080 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
3081 const BOOL is_file = data->scheme_type == URL_SCHEME_FILE;
3083 if(!data->path) {
3084 uri->path_start = -1;
3085 uri->path_len = 0;
3086 return TRUE;
3089 uri->path_start = uri->canon_len;
3091 /* For javascript: URIs, simply copy path part withoutany canonicalization */
3092 if(data->scheme_type == URL_SCHEME_JAVASCRIPT) {
3093 if(!computeOnly)
3094 memcpy(uri->canon_uri+uri->canon_len, data->path, data->path_len*sizeof(WCHAR));
3095 uri->path_len = data->path_len;
3096 uri->canon_len += data->path_len;
3097 return TRUE;
3100 /* Windows doesn't allow a "//" to appear after the scheme
3101 * of a URI, if it's an opaque URI.
3103 if(data->scheme && *(data->path) == '/' && *(data->path+1) == '/') {
3104 /* So it inserts a "/." before the "//" if it exists. */
3105 if(!computeOnly) {
3106 uri->canon_uri[uri->canon_len] = '/';
3107 uri->canon_uri[uri->canon_len+1] = '.';
3110 uri->canon_len += 2;
3113 for(ptr = data->path; ptr < data->path+data->path_len; ++ptr) {
3114 BOOL do_default_action = TRUE;
3116 if(*ptr == '%' && known_scheme) {
3117 WCHAR val = decode_pct_val(ptr);
3119 if(is_unreserved(val)) {
3120 if(!computeOnly)
3121 uri->canon_uri[uri->canon_len] = val;
3122 ++uri->canon_len;
3124 ptr += 2;
3125 continue;
3127 } else if(*ptr == '/' && is_file && (flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
3128 if(!computeOnly)
3129 uri->canon_uri[uri->canon_len] = '\\';
3130 ++uri->canon_len;
3131 do_default_action = FALSE;
3132 } else if(*ptr == '\\') {
3133 if(is_file && !(flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
3134 /* Convert to a '/'. */
3135 if(!computeOnly)
3136 uri->canon_uri[uri->canon_len] = '/';
3137 ++uri->canon_len;
3138 do_default_action = FALSE;
3140 } else if(known_scheme && !is_unreserved(*ptr) && !is_reserved(*ptr) &&
3141 !(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS)) {
3142 if(!(is_file && (flags & Uri_CREATE_FILE_USE_DOS_PATH))) {
3143 if(!computeOnly)
3144 pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
3145 uri->canon_len += 3;
3146 do_default_action = FALSE;
3150 if(do_default_action) {
3151 if(!computeOnly)
3152 uri->canon_uri[uri->canon_len] = *ptr;
3153 ++uri->canon_len;
3157 if(data->scheme_type == URL_SCHEME_MK && !computeOnly && !(flags & Uri_CREATE_NO_CANONICALIZE)) {
3158 DWORD new_len = remove_dot_segments(uri->canon_uri + uri->path_start,
3159 uri->canon_len - uri->path_start);
3160 uri->canon_len = uri->path_start + new_len;
3163 uri->path_len = uri->canon_len - uri->path_start;
3165 if(!computeOnly)
3166 TRACE("(%p %p %x %d): Canonicalized opaque URI path %s len=%d\n", data, uri, flags, computeOnly,
3167 debugstr_wn(uri->canon_uri+uri->path_start, uri->path_len), uri->path_len);
3168 return TRUE;
3171 /* Determines how the URI represented by the parse_data should be canonicalized.
3173 * Essentially, if the parse_data represents an hierarchical URI then it calls
3174 * canonicalize_authority and the canonicalization functions for the path. If the
3175 * URI is opaque it canonicalizes the path of the URI.
3177 static BOOL canonicalize_hierpart(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
3178 if(!data->is_opaque || (data->is_relative && (data->password || data->username))) {
3179 /* "//" is only added for non-wildcard scheme types.
3181 * A "//" is only added to a relative URI if it has a
3182 * host or port component (this only happens if a IUriBuilder
3183 * is generating an IUri).
3185 if((data->is_relative && (data->host || data->has_port)) ||
3186 (!data->is_relative && data->scheme_type != URL_SCHEME_WILDCARD)) {
3187 if(!computeOnly) {
3188 INT pos = uri->canon_len;
3190 uri->canon_uri[pos] = '/';
3191 uri->canon_uri[pos+1] = '/';
3193 uri->canon_len += 2;
3196 if(!canonicalize_authority(data, uri, flags, computeOnly))
3197 return FALSE;
3199 if(data->is_relative && (data->password || data->username)) {
3200 if(!canonicalize_path_opaque(data, uri, flags, computeOnly))
3201 return FALSE;
3202 } else {
3203 if(!canonicalize_path_hierarchical(data, uri, flags, computeOnly))
3204 return FALSE;
3206 } else {
3207 /* Opaque URI's don't have an authority. */
3208 uri->userinfo_start = uri->userinfo_split = -1;
3209 uri->userinfo_len = 0;
3210 uri->host_start = -1;
3211 uri->host_len = 0;
3212 uri->host_type = Uri_HOST_UNKNOWN;
3213 uri->has_port = FALSE;
3214 uri->authority_start = -1;
3215 uri->authority_len = 0;
3216 uri->domain_offset = -1;
3217 uri->port_offset = -1;
3219 if(is_hierarchical_scheme(data->scheme_type)) {
3220 DWORD i;
3222 /* Absolute URIs aren't displayed for known scheme types
3223 * which should be hierarchical URIs.
3225 uri->display_modifiers |= URI_DISPLAY_NO_ABSOLUTE_URI;
3227 /* Windows also sets the port for these (if they have one). */
3228 for(i = 0; i < sizeof(default_ports)/sizeof(default_ports[0]); ++i) {
3229 if(data->scheme_type == default_ports[i].scheme) {
3230 uri->has_port = TRUE;
3231 uri->port = default_ports[i].port;
3232 break;
3237 if(!canonicalize_path_opaque(data, uri, flags, computeOnly))
3238 return FALSE;
3241 if(uri->path_start > -1 && !computeOnly)
3242 /* Finding file extensions happens for both types of URIs. */
3243 uri->extension_offset = find_file_extension(uri->canon_uri+uri->path_start, uri->path_len);
3244 else
3245 uri->extension_offset = -1;
3247 return TRUE;
3250 /* Attempts to canonicalize the query string of the URI.
3252 * Things that happen:
3253 * 1) For known scheme types forbidden characters
3254 * are percent encoded, unless the NO_DECODE_EXTRA_INFO flag is set
3255 * or NO_ENCODE_FORBIDDEN_CHARACTERS is set.
3257 * 2) For known scheme types, percent encoded, unreserved characters
3258 * are decoded as long as the NO_DECODE_EXTRA_INFO flag isn't set.
3260 static BOOL canonicalize_query(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
3261 const WCHAR *ptr, *end;
3262 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
3264 if(!data->query) {
3265 uri->query_start = -1;
3266 uri->query_len = 0;
3267 return TRUE;
3270 uri->query_start = uri->canon_len;
3272 end = data->query+data->query_len;
3273 for(ptr = data->query; ptr < end; ++ptr) {
3274 if(*ptr == '%') {
3275 if(known_scheme && !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
3276 WCHAR val = decode_pct_val(ptr);
3277 if(is_unreserved(val)) {
3278 if(!computeOnly)
3279 uri->canon_uri[uri->canon_len] = val;
3280 ++uri->canon_len;
3282 ptr += 2;
3283 continue;
3286 } else if(known_scheme && !is_unreserved(*ptr) && !is_reserved(*ptr)) {
3287 if(!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS) &&
3288 !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
3289 if(!computeOnly)
3290 pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
3291 uri->canon_len += 3;
3292 continue;
3296 if(!computeOnly)
3297 uri->canon_uri[uri->canon_len] = *ptr;
3298 ++uri->canon_len;
3301 uri->query_len = uri->canon_len - uri->query_start;
3303 if(!computeOnly)
3304 TRACE("(%p %p %x %d): Canonicalized query string %s len=%d\n", data, uri, flags,
3305 computeOnly, debugstr_wn(uri->canon_uri+uri->query_start, uri->query_len),
3306 uri->query_len);
3307 return TRUE;
3310 static BOOL canonicalize_fragment(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
3311 const WCHAR *ptr, *end;
3312 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
3314 if(!data->fragment) {
3315 uri->fragment_start = -1;
3316 uri->fragment_len = 0;
3317 return TRUE;
3320 uri->fragment_start = uri->canon_len;
3322 end = data->fragment + data->fragment_len;
3323 for(ptr = data->fragment; ptr < end; ++ptr) {
3324 if(*ptr == '%') {
3325 if(known_scheme && !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
3326 WCHAR val = decode_pct_val(ptr);
3327 if(is_unreserved(val)) {
3328 if(!computeOnly)
3329 uri->canon_uri[uri->canon_len] = val;
3330 ++uri->canon_len;
3332 ptr += 2;
3333 continue;
3336 } else if(known_scheme && !is_unreserved(*ptr) && !is_reserved(*ptr)) {
3337 if(!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS) &&
3338 !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
3339 if(!computeOnly)
3340 pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
3341 uri->canon_len += 3;
3342 continue;
3346 if(!computeOnly)
3347 uri->canon_uri[uri->canon_len] = *ptr;
3348 ++uri->canon_len;
3351 uri->fragment_len = uri->canon_len - uri->fragment_start;
3353 if(!computeOnly)
3354 TRACE("(%p %p %x %d): Canonicalized fragment %s len=%d\n", data, uri, flags,
3355 computeOnly, debugstr_wn(uri->canon_uri+uri->fragment_start, uri->fragment_len),
3356 uri->fragment_len);
3357 return TRUE;
3360 /* Canonicalizes the scheme information specified in the parse_data using the specified flags. */
3361 static BOOL canonicalize_scheme(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
3362 uri->scheme_start = -1;
3363 uri->scheme_len = 0;
3365 if(!data->scheme) {
3366 /* The only type of URI that doesn't have to have a scheme is a relative
3367 * URI.
3369 if(!data->is_relative) {
3370 FIXME("(%p %p %x): Unable to determine the scheme type of %s.\n", data,
3371 uri, flags, debugstr_w(data->uri));
3372 return FALSE;
3374 } else {
3375 if(!computeOnly) {
3376 DWORD i;
3377 INT pos = uri->canon_len;
3379 for(i = 0; i < data->scheme_len; ++i) {
3380 /* Scheme name must be lower case after canonicalization. */
3381 uri->canon_uri[i + pos] = tolowerW(data->scheme[i]);
3384 uri->canon_uri[i + pos] = ':';
3385 uri->scheme_start = pos;
3387 TRACE("(%p %p %x): Canonicalized scheme=%s, len=%d.\n", data, uri, flags,
3388 debugstr_wn(uri->canon_uri, uri->scheme_len), data->scheme_len);
3391 /* This happens in both computation modes. */
3392 uri->canon_len += data->scheme_len + 1;
3393 uri->scheme_len = data->scheme_len;
3395 return TRUE;
3398 /* Compute's what the length of the URI specified by the parse_data will be
3399 * after canonicalization occurs using the specified flags.
3401 * This function will return a non-zero value indicating the length of the canonicalized
3402 * URI, or -1 on error.
3404 static int compute_canonicalized_length(const parse_data *data, DWORD flags) {
3405 Uri uri;
3407 memset(&uri, 0, sizeof(Uri));
3409 TRACE("(%p %x): Beginning to compute canonicalized length for URI %s\n", data, flags,
3410 debugstr_w(data->uri));
3412 if(!canonicalize_scheme(data, &uri, flags, TRUE)) {
3413 ERR("(%p %x): Failed to compute URI scheme length.\n", data, flags);
3414 return -1;
3417 if(!canonicalize_hierpart(data, &uri, flags, TRUE)) {
3418 ERR("(%p %x): Failed to compute URI hierpart length.\n", data, flags);
3419 return -1;
3422 if(!canonicalize_query(data, &uri, flags, TRUE)) {
3423 ERR("(%p %x): Failed to compute query string length.\n", data, flags);
3424 return -1;
3427 if(!canonicalize_fragment(data, &uri, flags, TRUE)) {
3428 ERR("(%p %x): Failed to compute fragment length.\n", data, flags);
3429 return -1;
3432 TRACE("(%p %x): Finished computing canonicalized URI length. length=%d\n", data, flags, uri.canon_len);
3434 return uri.canon_len;
3437 /* Canonicalizes the URI data specified in the parse_data, using the given flags. If the
3438 * canonicalization succeededs it will store all the canonicalization information
3439 * in the pointer to the Uri.
3441 * To canonicalize a URI this function first computes what the length of the URI
3442 * specified by the parse_data will be. Once this is done it will then perfom the actual
3443 * canonicalization of the URI.
3445 static HRESULT canonicalize_uri(const parse_data *data, Uri *uri, DWORD flags) {
3446 INT len;
3448 uri->canon_uri = NULL;
3449 uri->canon_size = uri->canon_len = 0;
3451 TRACE("(%p %p %x): beginning to canonicalize URI %s.\n", data, uri, flags, debugstr_w(data->uri));
3453 /* First try to compute the length of the URI. */
3454 len = compute_canonicalized_length(data, flags);
3455 if(len == -1) {
3456 ERR("(%p %p %x): Could not compute the canonicalized length of %s.\n", data, uri, flags,
3457 debugstr_w(data->uri));
3458 return E_INVALIDARG;
3461 uri->canon_uri = heap_alloc((len+1)*sizeof(WCHAR));
3462 if(!uri->canon_uri)
3463 return E_OUTOFMEMORY;
3465 uri->canon_size = len;
3466 if(!canonicalize_scheme(data, uri, flags, FALSE)) {
3467 ERR("(%p %p %x): Unable to canonicalize the scheme of the URI.\n", data, uri, flags);
3468 return E_INVALIDARG;
3470 uri->scheme_type = data->scheme_type;
3472 if(!canonicalize_hierpart(data, uri, flags, FALSE)) {
3473 ERR("(%p %p %x): Unable to canonicalize the heirpart of the URI\n", data, uri, flags);
3474 return E_INVALIDARG;
3477 if(!canonicalize_query(data, uri, flags, FALSE)) {
3478 ERR("(%p %p %x): Unable to canonicalize query string of the URI.\n",
3479 data, uri, flags);
3480 return E_INVALIDARG;
3483 if(!canonicalize_fragment(data, uri, flags, FALSE)) {
3484 ERR("(%p %p %x): Unable to canonicalize fragment of the URI.\n",
3485 data, uri, flags);
3486 return E_INVALIDARG;
3489 /* There's a possibility we didn't use all the space we allocated
3490 * earlier.
3492 if(uri->canon_len < uri->canon_size) {
3493 /* This happens if the URI is hierarchical and dot
3494 * segments were removed from it's path.
3496 WCHAR *tmp = heap_realloc(uri->canon_uri, (uri->canon_len+1)*sizeof(WCHAR));
3497 if(!tmp)
3498 return E_OUTOFMEMORY;
3500 uri->canon_uri = tmp;
3501 uri->canon_size = uri->canon_len;
3504 uri->canon_uri[uri->canon_len] = '\0';
3505 TRACE("(%p %p %x): finished canonicalizing the URI. uri=%s\n", data, uri, flags, debugstr_w(uri->canon_uri));
3507 return S_OK;
3510 static HRESULT get_builder_component(LPWSTR *component, DWORD *component_len,
3511 LPCWSTR source, DWORD source_len,
3512 LPCWSTR *output, DWORD *output_len)
3514 if(!output_len) {
3515 if(output)
3516 *output = NULL;
3517 return E_POINTER;
3520 if(!output) {
3521 *output_len = 0;
3522 return E_POINTER;
3525 if(!(*component) && source) {
3526 /* Allocate 'component', and copy the contents from 'source'
3527 * into the new allocation.
3529 *component = heap_alloc((source_len+1)*sizeof(WCHAR));
3530 if(!(*component))
3531 return E_OUTOFMEMORY;
3533 memcpy(*component, source, source_len*sizeof(WCHAR));
3534 (*component)[source_len] = '\0';
3535 *component_len = source_len;
3538 *output = *component;
3539 *output_len = *component_len;
3540 return *output ? S_OK : S_FALSE;
3543 /* Allocates 'component' and copies the string from 'new_value' into 'component'.
3544 * If 'prefix' is set and 'new_value' isn't NULL, then it checks if 'new_value'
3545 * starts with 'prefix'. If it doesn't then 'prefix' is prepended to 'component'.
3547 * If everything is successful, then will set 'success_flag' in 'flags'.
3549 static HRESULT set_builder_component(LPWSTR *component, DWORD *component_len, LPCWSTR new_value,
3550 WCHAR prefix, DWORD *flags, DWORD success_flag)
3552 heap_free(*component);
3554 if(!new_value) {
3555 *component = NULL;
3556 *component_len = 0;
3557 } else {
3558 BOOL add_prefix = FALSE;
3559 DWORD len = lstrlenW(new_value);
3560 DWORD pos = 0;
3562 if(prefix && *new_value != prefix) {
3563 add_prefix = TRUE;
3564 *component = heap_alloc((len+2)*sizeof(WCHAR));
3565 } else
3566 *component = heap_alloc((len+1)*sizeof(WCHAR));
3568 if(!(*component))
3569 return E_OUTOFMEMORY;
3571 if(add_prefix)
3572 (*component)[pos++] = prefix;
3574 memcpy(*component+pos, new_value, (len+1)*sizeof(WCHAR));
3575 *component_len = len+pos;
3578 *flags |= success_flag;
3579 return S_OK;
3582 static void reset_builder(UriBuilder *builder) {
3583 if(builder->uri)
3584 IUri_Release(&builder->uri->IUri_iface);
3585 builder->uri = NULL;
3587 heap_free(builder->fragment);
3588 builder->fragment = NULL;
3589 builder->fragment_len = 0;
3591 heap_free(builder->host);
3592 builder->host = NULL;
3593 builder->host_len = 0;
3595 heap_free(builder->password);
3596 builder->password = NULL;
3597 builder->password_len = 0;
3599 heap_free(builder->path);
3600 builder->path = NULL;
3601 builder->path_len = 0;
3603 heap_free(builder->query);
3604 builder->query = NULL;
3605 builder->query_len = 0;
3607 heap_free(builder->scheme);
3608 builder->scheme = NULL;
3609 builder->scheme_len = 0;
3611 heap_free(builder->username);
3612 builder->username = NULL;
3613 builder->username_len = 0;
3615 builder->has_port = FALSE;
3616 builder->port = 0;
3617 builder->modified_props = 0;
3620 static HRESULT validate_scheme_name(const UriBuilder *builder, parse_data *data, DWORD flags) {
3621 const WCHAR *component;
3622 const WCHAR *ptr;
3623 const WCHAR **pptr;
3624 DWORD expected_len;
3626 if(builder->scheme) {
3627 ptr = builder->scheme;
3628 expected_len = builder->scheme_len;
3629 } else if(builder->uri && builder->uri->scheme_start > -1) {
3630 ptr = builder->uri->canon_uri+builder->uri->scheme_start;
3631 expected_len = builder->uri->scheme_len;
3632 } else {
3633 static const WCHAR nullW[] = {0};
3634 ptr = nullW;
3635 expected_len = 0;
3638 component = ptr;
3639 pptr = &ptr;
3640 if(parse_scheme(pptr, data, flags, ALLOW_NULL_TERM_SCHEME) &&
3641 data->scheme_len == expected_len) {
3642 if(data->scheme)
3643 TRACE("(%p %p %x): Found valid scheme component %s len=%d.\n", builder, data, flags,
3644 debugstr_wn(data->scheme, data->scheme_len), data->scheme_len);
3645 } else {
3646 TRACE("(%p %p %x): Invalid scheme component found %s.\n", builder, data, flags,
3647 debugstr_wn(component, expected_len));
3648 return INET_E_INVALID_URL;
3651 return S_OK;
3654 static HRESULT validate_username(const UriBuilder *builder, parse_data *data, DWORD flags) {
3655 const WCHAR *ptr;
3656 const WCHAR **pptr;
3657 DWORD expected_len;
3659 if(builder->username) {
3660 ptr = builder->username;
3661 expected_len = builder->username_len;
3662 } else if(!(builder->modified_props & Uri_HAS_USER_NAME) && builder->uri &&
3663 builder->uri->userinfo_start > -1 && builder->uri->userinfo_split != 0) {
3664 /* Just use the username from the base Uri. */
3665 data->username = builder->uri->canon_uri+builder->uri->userinfo_start;
3666 data->username_len = (builder->uri->userinfo_split > -1) ?
3667 builder->uri->userinfo_split : builder->uri->userinfo_len;
3668 ptr = NULL;
3669 } else {
3670 ptr = NULL;
3671 expected_len = 0;
3674 if(ptr) {
3675 const WCHAR *component = ptr;
3676 pptr = &ptr;
3677 if(parse_username(pptr, data, flags, ALLOW_NULL_TERM_USER_NAME) &&
3678 data->username_len == expected_len)
3679 TRACE("(%p %p %x): Found valid username component %s len=%d.\n", builder, data, flags,
3680 debugstr_wn(data->username, data->username_len), data->username_len);
3681 else {
3682 TRACE("(%p %p %x): Invalid username component found %s.\n", builder, data, flags,
3683 debugstr_wn(component, expected_len));
3684 return INET_E_INVALID_URL;
3688 return S_OK;
3691 static HRESULT validate_password(const UriBuilder *builder, parse_data *data, DWORD flags) {
3692 const WCHAR *ptr;
3693 const WCHAR **pptr;
3694 DWORD expected_len;
3696 if(builder->password) {
3697 ptr = builder->password;
3698 expected_len = builder->password_len;
3699 } else if(!(builder->modified_props & Uri_HAS_PASSWORD) && builder->uri &&
3700 builder->uri->userinfo_split > -1) {
3701 data->password = builder->uri->canon_uri+builder->uri->userinfo_start+builder->uri->userinfo_split+1;
3702 data->password_len = builder->uri->userinfo_len-builder->uri->userinfo_split-1;
3703 ptr = NULL;
3704 } else {
3705 ptr = NULL;
3706 expected_len = 0;
3709 if(ptr) {
3710 const WCHAR *component = ptr;
3711 pptr = &ptr;
3712 if(parse_password(pptr, data, flags, ALLOW_NULL_TERM_PASSWORD) &&
3713 data->password_len == expected_len)
3714 TRACE("(%p %p %x): Found valid password component %s len=%d.\n", builder, data, flags,
3715 debugstr_wn(data->password, data->password_len), data->password_len);
3716 else {
3717 TRACE("(%p %p %x): Invalid password component found %s.\n", builder, data, flags,
3718 debugstr_wn(component, expected_len));
3719 return INET_E_INVALID_URL;
3723 return S_OK;
3726 static HRESULT validate_userinfo(const UriBuilder *builder, parse_data *data, DWORD flags) {
3727 HRESULT hr;
3729 hr = validate_username(builder, data, flags);
3730 if(FAILED(hr))
3731 return hr;
3733 hr = validate_password(builder, data, flags);
3734 if(FAILED(hr))
3735 return hr;
3737 return S_OK;
3740 static HRESULT validate_host(const UriBuilder *builder, parse_data *data, DWORD flags) {
3741 const WCHAR *ptr;
3742 const WCHAR **pptr;
3743 DWORD expected_len;
3745 if(builder->host) {
3746 ptr = builder->host;
3747 expected_len = builder->host_len;
3748 } else if(!(builder->modified_props & Uri_HAS_HOST) && builder->uri && builder->uri->host_start > -1) {
3749 ptr = builder->uri->canon_uri + builder->uri->host_start;
3750 expected_len = builder->uri->host_len;
3751 } else
3752 ptr = NULL;
3754 if(ptr) {
3755 const WCHAR *component = ptr;
3756 DWORD extras = ALLOW_BRACKETLESS_IP_LITERAL|IGNORE_PORT_DELIMITER|SKIP_IP_FUTURE_CHECK;
3757 pptr = &ptr;
3759 if(parse_host(pptr, data, flags, extras) && data->host_len == expected_len)
3760 TRACE("(%p %p %x): Found valid host name %s len=%d type=%d.\n", builder, data, flags,
3761 debugstr_wn(data->host, data->host_len), data->host_len, data->host_type);
3762 else {
3763 TRACE("(%p %p %x): Invalid host name found %s.\n", builder, data, flags,
3764 debugstr_wn(component, expected_len));
3765 return INET_E_INVALID_URL;
3769 return S_OK;
3772 static void setup_port(const UriBuilder *builder, parse_data *data, DWORD flags) {
3773 if(builder->modified_props & Uri_HAS_PORT) {
3774 if(builder->has_port) {
3775 data->has_port = TRUE;
3776 data->port_value = builder->port;
3778 } else if(builder->uri && builder->uri->has_port) {
3779 data->has_port = TRUE;
3780 data->port_value = builder->uri->port;
3783 if(data->has_port)
3784 TRACE("(%p %p %x): Using %u as port for IUri.\n", builder, data, flags, data->port_value);
3787 static HRESULT validate_path(const UriBuilder *builder, parse_data *data, DWORD flags) {
3788 const WCHAR *ptr = NULL;
3789 const WCHAR *component;
3790 const WCHAR **pptr;
3791 DWORD expected_len;
3792 BOOL check_len = TRUE;
3793 BOOL valid = FALSE;
3795 if(builder->path) {
3796 ptr = builder->path;
3797 expected_len = builder->path_len;
3798 } else if(!(builder->modified_props & Uri_HAS_PATH) &&
3799 builder->uri && builder->uri->path_start > -1) {
3800 ptr = builder->uri->canon_uri+builder->uri->path_start;
3801 expected_len = builder->uri->path_len;
3802 } else {
3803 static const WCHAR nullW[] = {0};
3804 ptr = nullW;
3805 check_len = FALSE;
3806 expected_len = -1;
3809 component = ptr;
3810 pptr = &ptr;
3812 /* How the path is validated depends on what type of
3813 * URI it is.
3815 valid = data->is_opaque ?
3816 parse_path_opaque(pptr, data, flags) : parse_path_hierarchical(pptr, data, flags);
3818 if(!valid || (check_len && expected_len != data->path_len)) {
3819 TRACE("(%p %p %x): Invalid path component %s.\n", builder, data, flags,
3820 debugstr_wn(component, expected_len) );
3821 return INET_E_INVALID_URL;
3824 TRACE("(%p %p %x): Valid path component %s len=%d.\n", builder, data, flags,
3825 debugstr_wn(data->path, data->path_len), data->path_len);
3827 return S_OK;
3830 static HRESULT validate_query(const UriBuilder *builder, parse_data *data, DWORD flags) {
3831 const WCHAR *ptr = NULL;
3832 const WCHAR **pptr;
3833 DWORD expected_len;
3835 if(builder->query) {
3836 ptr = builder->query;
3837 expected_len = builder->query_len;
3838 } else if(!(builder->modified_props & Uri_HAS_QUERY) && builder->uri &&
3839 builder->uri->query_start > -1) {
3840 ptr = builder->uri->canon_uri+builder->uri->query_start;
3841 expected_len = builder->uri->query_len;
3844 if(ptr) {
3845 const WCHAR *component = ptr;
3846 pptr = &ptr;
3848 if(parse_query(pptr, data, flags) && expected_len == data->query_len)
3849 TRACE("(%p %p %x): Valid query component %s len=%d.\n", builder, data, flags,
3850 debugstr_wn(data->query, data->query_len), data->query_len);
3851 else {
3852 TRACE("(%p %p %x): Invalid query component %s.\n", builder, data, flags,
3853 debugstr_wn(component, expected_len));
3854 return INET_E_INVALID_URL;
3858 return S_OK;
3861 static HRESULT validate_fragment(const UriBuilder *builder, parse_data *data, DWORD flags) {
3862 const WCHAR *ptr = NULL;
3863 const WCHAR **pptr;
3864 DWORD expected_len;
3866 if(builder->fragment) {
3867 ptr = builder->fragment;
3868 expected_len = builder->fragment_len;
3869 } else if(!(builder->modified_props & Uri_HAS_FRAGMENT) && builder->uri &&
3870 builder->uri->fragment_start > -1) {
3871 ptr = builder->uri->canon_uri+builder->uri->fragment_start;
3872 expected_len = builder->uri->fragment_len;
3875 if(ptr) {
3876 const WCHAR *component = ptr;
3877 pptr = &ptr;
3879 if(parse_fragment(pptr, data, flags) && expected_len == data->fragment_len)
3880 TRACE("(%p %p %x): Valid fragment component %s len=%d.\n", builder, data, flags,
3881 debugstr_wn(data->fragment, data->fragment_len), data->fragment_len);
3882 else {
3883 TRACE("(%p %p %x): Invalid fragment component %s.\n", builder, data, flags,
3884 debugstr_wn(component, expected_len));
3885 return INET_E_INVALID_URL;
3889 return S_OK;
3892 static HRESULT validate_components(const UriBuilder *builder, parse_data *data, DWORD flags) {
3893 HRESULT hr;
3895 memset(data, 0, sizeof(parse_data));
3897 TRACE("(%p %p %x): Beginning to validate builder components.\n", builder, data, flags);
3899 hr = validate_scheme_name(builder, data, flags);
3900 if(FAILED(hr))
3901 return hr;
3903 /* Extra validation for file schemes. */
3904 if(data->scheme_type == URL_SCHEME_FILE) {
3905 if((builder->password || (builder->uri && builder->uri->userinfo_split > -1)) ||
3906 (builder->username || (builder->uri && builder->uri->userinfo_start > -1))) {
3907 TRACE("(%p %p %x): File schemes can't contain a username or password.\n",
3908 builder, data, flags);
3909 return INET_E_INVALID_URL;
3913 hr = validate_userinfo(builder, data, flags);
3914 if(FAILED(hr))
3915 return hr;
3917 hr = validate_host(builder, data, flags);
3918 if(FAILED(hr))
3919 return hr;
3921 setup_port(builder, data, flags);
3923 /* The URI is opaque if it doesn't have an authority component. */
3924 if(!data->is_relative)
3925 data->is_opaque = !data->username && !data->password && !data->host && !data->has_port;
3926 else
3927 data->is_opaque = !data->host && !data->has_port;
3929 hr = validate_path(builder, data, flags);
3930 if(FAILED(hr))
3931 return hr;
3933 hr = validate_query(builder, data, flags);
3934 if(FAILED(hr))
3935 return hr;
3937 hr = validate_fragment(builder, data, flags);
3938 if(FAILED(hr))
3939 return hr;
3941 TRACE("(%p %p %x): Finished validating builder components.\n", builder, data, flags);
3943 return S_OK;
3946 static void convert_to_dos_path(const WCHAR *path, DWORD path_len,
3947 WCHAR *output, DWORD *output_len)
3949 const WCHAR *ptr = path;
3951 if(path_len > 3 && *ptr == '/' && is_drive_path(path+1))
3952 /* Skip over the leading / before the drive path. */
3953 ++ptr;
3955 for(; ptr < path+path_len; ++ptr) {
3956 if(*ptr == '/') {
3957 if(output)
3958 *output++ = '\\';
3959 (*output_len)++;
3960 } else {
3961 if(output)
3962 *output++ = *ptr;
3963 (*output_len)++;
3968 /* Generates a raw uri string using the parse_data. */
3969 static DWORD generate_raw_uri(const parse_data *data, BSTR uri, DWORD flags) {
3970 DWORD length = 0;
3972 if(data->scheme) {
3973 if(uri) {
3974 memcpy(uri, data->scheme, data->scheme_len*sizeof(WCHAR));
3975 uri[data->scheme_len] = ':';
3977 length += data->scheme_len+1;
3980 if(!data->is_opaque) {
3981 /* For the "//" which appears before the authority component. */
3982 if(uri) {
3983 uri[length] = '/';
3984 uri[length+1] = '/';
3986 length += 2;
3988 /* Check if we need to add the "\\" before the host name
3989 * of a UNC server name in a DOS path.
3991 if(flags & RAW_URI_CONVERT_TO_DOS_PATH &&
3992 data->scheme_type == URL_SCHEME_FILE && data->host) {
3993 if(uri) {
3994 uri[length] = '\\';
3995 uri[length+1] = '\\';
3997 length += 2;
4001 if(data->username) {
4002 if(uri)
4003 memcpy(uri+length, data->username, data->username_len*sizeof(WCHAR));
4004 length += data->username_len;
4007 if(data->password) {
4008 if(uri) {
4009 uri[length] = ':';
4010 memcpy(uri+length+1, data->password, data->password_len*sizeof(WCHAR));
4012 length += data->password_len+1;
4015 if(data->password || data->username) {
4016 if(uri)
4017 uri[length] = '@';
4018 ++length;
4021 if(data->host) {
4022 /* IPv6 addresses get the brackets added around them if they don't already
4023 * have them.
4025 const BOOL add_brackets = data->host_type == Uri_HOST_IPV6 && *(data->host) != '[';
4026 if(add_brackets) {
4027 if(uri)
4028 uri[length] = '[';
4029 ++length;
4032 if(uri)
4033 memcpy(uri+length, data->host, data->host_len*sizeof(WCHAR));
4034 length += data->host_len;
4036 if(add_brackets) {
4037 if(uri)
4038 uri[length] = ']';
4039 length++;
4043 if(data->has_port) {
4044 /* The port isn't included in the raw uri if it's the default
4045 * port for the scheme type.
4047 DWORD i;
4048 BOOL is_default = FALSE;
4050 for(i = 0; i < sizeof(default_ports)/sizeof(default_ports[0]); ++i) {
4051 if(data->scheme_type == default_ports[i].scheme &&
4052 data->port_value == default_ports[i].port)
4053 is_default = TRUE;
4056 if(!is_default || flags & RAW_URI_FORCE_PORT_DISP) {
4057 if(uri)
4058 uri[length] = ':';
4059 ++length;
4061 if(uri)
4062 length += ui2str(uri+length, data->port_value);
4063 else
4064 length += ui2str(NULL, data->port_value);
4068 /* Check if a '/' should be added before the path for hierarchical URIs. */
4069 if(!data->is_opaque && data->path && *(data->path) != '/') {
4070 if(uri)
4071 uri[length] = '/';
4072 ++length;
4075 if(data->path) {
4076 if(!data->is_opaque && data->scheme_type == URL_SCHEME_FILE &&
4077 flags & RAW_URI_CONVERT_TO_DOS_PATH) {
4078 DWORD len = 0;
4080 if(uri)
4081 convert_to_dos_path(data->path, data->path_len, uri+length, &len);
4082 else
4083 convert_to_dos_path(data->path, data->path_len, NULL, &len);
4085 length += len;
4086 } else {
4087 if(uri)
4088 memcpy(uri+length, data->path, data->path_len*sizeof(WCHAR));
4089 length += data->path_len;
4093 if(data->query) {
4094 if(uri)
4095 memcpy(uri+length, data->query, data->query_len*sizeof(WCHAR));
4096 length += data->query_len;
4099 if(data->fragment) {
4100 if(uri)
4101 memcpy(uri+length, data->fragment, data->fragment_len*sizeof(WCHAR));
4102 length += data->fragment_len;
4105 if(uri)
4106 TRACE("(%p %p): Generated raw uri=%s len=%d\n", data, uri, debugstr_wn(uri, length), length);
4107 else
4108 TRACE("(%p %p): Computed raw uri len=%d\n", data, uri, length);
4110 return length;
4113 static HRESULT generate_uri(const UriBuilder *builder, const parse_data *data, Uri *uri, DWORD flags) {
4114 HRESULT hr;
4115 DWORD length = generate_raw_uri(data, NULL, 0);
4116 uri->raw_uri = SysAllocStringLen(NULL, length);
4117 if(!uri->raw_uri)
4118 return E_OUTOFMEMORY;
4120 generate_raw_uri(data, uri->raw_uri, 0);
4122 hr = canonicalize_uri(data, uri, flags);
4123 if(FAILED(hr)) {
4124 if(hr == E_INVALIDARG)
4125 return INET_E_INVALID_URL;
4126 return hr;
4129 uri->create_flags = flags;
4130 return S_OK;
4133 static inline Uri* impl_from_IUri(IUri *iface)
4135 return CONTAINING_RECORD(iface, Uri, IUri_iface);
4138 static inline void destory_uri_obj(Uri *This)
4140 SysFreeString(This->raw_uri);
4141 heap_free(This->canon_uri);
4142 heap_free(This);
4145 static HRESULT WINAPI Uri_QueryInterface(IUri *iface, REFIID riid, void **ppv)
4147 Uri *This = impl_from_IUri(iface);
4149 if(IsEqualGUID(&IID_IUnknown, riid)) {
4150 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
4151 *ppv = &This->IUri_iface;
4152 }else if(IsEqualGUID(&IID_IUri, riid)) {
4153 TRACE("(%p)->(IID_IUri %p)\n", This, ppv);
4154 *ppv = &This->IUri_iface;
4155 }else if(IsEqualGUID(&IID_IUriBuilderFactory, riid)) {
4156 TRACE("(%p)->(IID_IUriBuilderFactory %p)\n", This, riid);
4157 *ppv = &This->IUriBuilderFactory_iface;
4158 }else if(IsEqualGUID(&IID_IUriObj, riid)) {
4159 TRACE("(%p)->(IID_IUriObj %p)\n", This, ppv);
4160 *ppv = This;
4161 return S_OK;
4162 }else {
4163 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
4164 *ppv = NULL;
4165 return E_NOINTERFACE;
4168 IUnknown_AddRef((IUnknown*)*ppv);
4169 return S_OK;
4172 static ULONG WINAPI Uri_AddRef(IUri *iface)
4174 Uri *This = impl_from_IUri(iface);
4175 LONG ref = InterlockedIncrement(&This->ref);
4177 TRACE("(%p) ref=%d\n", This, ref);
4179 return ref;
4182 static ULONG WINAPI Uri_Release(IUri *iface)
4184 Uri *This = impl_from_IUri(iface);
4185 LONG ref = InterlockedDecrement(&This->ref);
4187 TRACE("(%p) ref=%d\n", This, ref);
4189 if(!ref)
4190 destory_uri_obj(This);
4192 return ref;
4195 static HRESULT WINAPI Uri_GetPropertyBSTR(IUri *iface, Uri_PROPERTY uriProp, BSTR *pbstrProperty, DWORD dwFlags)
4197 Uri *This = impl_from_IUri(iface);
4198 HRESULT hres;
4199 TRACE("(%p)->(%d %p %x)\n", This, uriProp, pbstrProperty, dwFlags);
4201 if(!pbstrProperty)
4202 return E_POINTER;
4204 if(uriProp > Uri_PROPERTY_STRING_LAST) {
4205 /* Windows allocates an empty BSTR for invalid Uri_PROPERTY's. */
4206 *pbstrProperty = SysAllocStringLen(NULL, 0);
4207 if(!(*pbstrProperty))
4208 return E_OUTOFMEMORY;
4210 /* It only returns S_FALSE for the ZONE property... */
4211 if(uriProp == Uri_PROPERTY_ZONE)
4212 return S_FALSE;
4213 else
4214 return S_OK;
4217 /* Don't have support for flags yet. */
4218 if(dwFlags) {
4219 FIXME("(%p)->(%d %p %x)\n", This, uriProp, pbstrProperty, dwFlags);
4220 return E_NOTIMPL;
4223 switch(uriProp) {
4224 case Uri_PROPERTY_ABSOLUTE_URI:
4225 if(This->display_modifiers & URI_DISPLAY_NO_ABSOLUTE_URI) {
4226 *pbstrProperty = SysAllocStringLen(NULL, 0);
4227 hres = S_FALSE;
4228 } else {
4229 if(This->scheme_type != URL_SCHEME_UNKNOWN && This->userinfo_start > -1) {
4230 if(This->userinfo_len == 0) {
4231 /* Don't include the '@' after the userinfo component. */
4232 *pbstrProperty = SysAllocStringLen(NULL, This->canon_len-1);
4233 hres = S_OK;
4234 if(*pbstrProperty) {
4235 /* Copy everything before it. */
4236 memcpy(*pbstrProperty, This->canon_uri, This->userinfo_start*sizeof(WCHAR));
4238 /* And everything after it. */
4239 memcpy(*pbstrProperty+This->userinfo_start, This->canon_uri+This->userinfo_start+1,
4240 (This->canon_len-This->userinfo_start-1)*sizeof(WCHAR));
4242 } else if(This->userinfo_split == 0 && This->userinfo_len == 1) {
4243 /* Don't include the ":@" */
4244 *pbstrProperty = SysAllocStringLen(NULL, This->canon_len-2);
4245 hres = S_OK;
4246 if(*pbstrProperty) {
4247 memcpy(*pbstrProperty, This->canon_uri, This->userinfo_start*sizeof(WCHAR));
4248 memcpy(*pbstrProperty+This->userinfo_start, This->canon_uri+This->userinfo_start+2,
4249 (This->canon_len-This->userinfo_start-2)*sizeof(WCHAR));
4251 } else {
4252 *pbstrProperty = SysAllocString(This->canon_uri);
4253 hres = S_OK;
4255 } else {
4256 *pbstrProperty = SysAllocString(This->canon_uri);
4257 hres = S_OK;
4261 if(!(*pbstrProperty))
4262 hres = E_OUTOFMEMORY;
4264 break;
4265 case Uri_PROPERTY_AUTHORITY:
4266 if(This->authority_start > -1) {
4267 if(This->port_offset > -1 && is_default_port(This->scheme_type, This->port) &&
4268 This->display_modifiers & URI_DISPLAY_NO_DEFAULT_PORT_AUTH)
4269 /* Don't include the port in the authority component. */
4270 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->authority_start, This->port_offset);
4271 else
4272 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->authority_start, This->authority_len);
4273 hres = S_OK;
4274 } else {
4275 *pbstrProperty = SysAllocStringLen(NULL, 0);
4276 hres = S_FALSE;
4279 if(!(*pbstrProperty))
4280 hres = E_OUTOFMEMORY;
4282 break;
4283 case Uri_PROPERTY_DISPLAY_URI:
4284 /* The Display URI contains everything except for the userinfo for known
4285 * scheme types.
4287 if(This->scheme_type != URL_SCHEME_UNKNOWN && This->userinfo_start > -1) {
4288 *pbstrProperty = SysAllocStringLen(NULL, This->canon_len-This->userinfo_len);
4290 if(*pbstrProperty) {
4291 /* Copy everything before the userinfo over. */
4292 memcpy(*pbstrProperty, This->canon_uri, This->userinfo_start*sizeof(WCHAR));
4293 /* Copy everything after the userinfo over. */
4294 memcpy(*pbstrProperty+This->userinfo_start,
4295 This->canon_uri+This->userinfo_start+This->userinfo_len+1,
4296 (This->canon_len-(This->userinfo_start+This->userinfo_len+1))*sizeof(WCHAR));
4298 } else
4299 *pbstrProperty = SysAllocString(This->canon_uri);
4301 if(!(*pbstrProperty))
4302 hres = E_OUTOFMEMORY;
4303 else
4304 hres = S_OK;
4306 break;
4307 case Uri_PROPERTY_DOMAIN:
4308 if(This->domain_offset > -1) {
4309 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->host_start+This->domain_offset,
4310 This->host_len-This->domain_offset);
4311 hres = S_OK;
4312 } else {
4313 *pbstrProperty = SysAllocStringLen(NULL, 0);
4314 hres = S_FALSE;
4317 if(!(*pbstrProperty))
4318 hres = E_OUTOFMEMORY;
4320 break;
4321 case Uri_PROPERTY_EXTENSION:
4322 if(This->extension_offset > -1) {
4323 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->path_start+This->extension_offset,
4324 This->path_len-This->extension_offset);
4325 hres = S_OK;
4326 } else {
4327 *pbstrProperty = SysAllocStringLen(NULL, 0);
4328 hres = S_FALSE;
4331 if(!(*pbstrProperty))
4332 hres = E_OUTOFMEMORY;
4334 break;
4335 case Uri_PROPERTY_FRAGMENT:
4336 if(This->fragment_start > -1) {
4337 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->fragment_start, This->fragment_len);
4338 hres = S_OK;
4339 } else {
4340 *pbstrProperty = SysAllocStringLen(NULL, 0);
4341 hres = S_FALSE;
4344 if(!(*pbstrProperty))
4345 hres = E_OUTOFMEMORY;
4347 break;
4348 case Uri_PROPERTY_HOST:
4349 if(This->host_start > -1) {
4350 /* The '[' and ']' aren't included for IPv6 addresses. */
4351 if(This->host_type == Uri_HOST_IPV6)
4352 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->host_start+1, This->host_len-2);
4353 else
4354 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->host_start, This->host_len);
4356 hres = S_OK;
4357 } else {
4358 *pbstrProperty = SysAllocStringLen(NULL, 0);
4359 hres = S_FALSE;
4362 if(!(*pbstrProperty))
4363 hres = E_OUTOFMEMORY;
4365 break;
4366 case Uri_PROPERTY_PASSWORD:
4367 if(This->userinfo_split > -1) {
4368 *pbstrProperty = SysAllocStringLen(
4369 This->canon_uri+This->userinfo_start+This->userinfo_split+1,
4370 This->userinfo_len-This->userinfo_split-1);
4371 hres = S_OK;
4372 } else {
4373 *pbstrProperty = SysAllocStringLen(NULL, 0);
4374 hres = S_FALSE;
4377 if(!(*pbstrProperty))
4378 return E_OUTOFMEMORY;
4380 break;
4381 case Uri_PROPERTY_PATH:
4382 if(This->path_start > -1) {
4383 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->path_start, This->path_len);
4384 hres = S_OK;
4385 } else {
4386 *pbstrProperty = SysAllocStringLen(NULL, 0);
4387 hres = S_FALSE;
4390 if(!(*pbstrProperty))
4391 hres = E_OUTOFMEMORY;
4393 break;
4394 case Uri_PROPERTY_PATH_AND_QUERY:
4395 if(This->path_start > -1) {
4396 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->path_start, This->path_len+This->query_len);
4397 hres = S_OK;
4398 } else if(This->query_start > -1) {
4399 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->query_start, This->query_len);
4400 hres = S_OK;
4401 } else {
4402 *pbstrProperty = SysAllocStringLen(NULL, 0);
4403 hres = S_FALSE;
4406 if(!(*pbstrProperty))
4407 hres = E_OUTOFMEMORY;
4409 break;
4410 case Uri_PROPERTY_QUERY:
4411 if(This->query_start > -1) {
4412 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->query_start, This->query_len);
4413 hres = S_OK;
4414 } else {
4415 *pbstrProperty = SysAllocStringLen(NULL, 0);
4416 hres = S_FALSE;
4419 if(!(*pbstrProperty))
4420 hres = E_OUTOFMEMORY;
4422 break;
4423 case Uri_PROPERTY_RAW_URI:
4424 *pbstrProperty = SysAllocString(This->raw_uri);
4425 if(!(*pbstrProperty))
4426 hres = E_OUTOFMEMORY;
4427 else
4428 hres = S_OK;
4429 break;
4430 case Uri_PROPERTY_SCHEME_NAME:
4431 if(This->scheme_start > -1) {
4432 *pbstrProperty = SysAllocStringLen(This->canon_uri + This->scheme_start, This->scheme_len);
4433 hres = S_OK;
4434 } else {
4435 *pbstrProperty = SysAllocStringLen(NULL, 0);
4436 hres = S_FALSE;
4439 if(!(*pbstrProperty))
4440 hres = E_OUTOFMEMORY;
4442 break;
4443 case Uri_PROPERTY_USER_INFO:
4444 if(This->userinfo_start > -1) {
4445 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->userinfo_start, This->userinfo_len);
4446 hres = S_OK;
4447 } else {
4448 *pbstrProperty = SysAllocStringLen(NULL, 0);
4449 hres = S_FALSE;
4452 if(!(*pbstrProperty))
4453 hres = E_OUTOFMEMORY;
4455 break;
4456 case Uri_PROPERTY_USER_NAME:
4457 if(This->userinfo_start > -1 && This->userinfo_split != 0) {
4458 /* If userinfo_split is set, that means a password exists
4459 * so the username is only from userinfo_start to userinfo_split.
4461 if(This->userinfo_split > -1) {
4462 *pbstrProperty = SysAllocStringLen(This->canon_uri + This->userinfo_start, This->userinfo_split);
4463 hres = S_OK;
4464 } else {
4465 *pbstrProperty = SysAllocStringLen(This->canon_uri + This->userinfo_start, This->userinfo_len);
4466 hres = S_OK;
4468 } else {
4469 *pbstrProperty = SysAllocStringLen(NULL, 0);
4470 hres = S_FALSE;
4473 if(!(*pbstrProperty))
4474 return E_OUTOFMEMORY;
4476 break;
4477 default:
4478 FIXME("(%p)->(%d %p %x)\n", This, uriProp, pbstrProperty, dwFlags);
4479 hres = E_NOTIMPL;
4482 return hres;
4485 static HRESULT WINAPI Uri_GetPropertyLength(IUri *iface, Uri_PROPERTY uriProp, DWORD *pcchProperty, DWORD dwFlags)
4487 Uri *This = impl_from_IUri(iface);
4488 HRESULT hres;
4489 TRACE("(%p)->(%d %p %x)\n", This, uriProp, pcchProperty, dwFlags);
4491 if(!pcchProperty)
4492 return E_INVALIDARG;
4494 /* Can only return a length for a property if it's a string. */
4495 if(uriProp > Uri_PROPERTY_STRING_LAST)
4496 return E_INVALIDARG;
4498 /* Don't have support for flags yet. */
4499 if(dwFlags) {
4500 FIXME("(%p)->(%d %p %x)\n", This, uriProp, pcchProperty, dwFlags);
4501 return E_NOTIMPL;
4504 switch(uriProp) {
4505 case Uri_PROPERTY_ABSOLUTE_URI:
4506 if(This->display_modifiers & URI_DISPLAY_NO_ABSOLUTE_URI) {
4507 *pcchProperty = 0;
4508 hres = S_FALSE;
4509 } else {
4510 if(This->scheme_type != URL_SCHEME_UNKNOWN) {
4511 if(This->userinfo_start > -1 && This->userinfo_len == 0)
4512 /* Don't include the '@' in the length. */
4513 *pcchProperty = This->canon_len-1;
4514 else if(This->userinfo_start > -1 && This->userinfo_len == 1 &&
4515 This->userinfo_split == 0)
4516 /* Don't include the ":@" in the length. */
4517 *pcchProperty = This->canon_len-2;
4518 else
4519 *pcchProperty = This->canon_len;
4520 } else
4521 *pcchProperty = This->canon_len;
4523 hres = S_OK;
4526 break;
4527 case Uri_PROPERTY_AUTHORITY:
4528 if(This->port_offset > -1 &&
4529 This->display_modifiers & URI_DISPLAY_NO_DEFAULT_PORT_AUTH &&
4530 is_default_port(This->scheme_type, This->port))
4531 /* Only count up until the port in the authority. */
4532 *pcchProperty = This->port_offset;
4533 else
4534 *pcchProperty = This->authority_len;
4535 hres = (This->authority_start > -1) ? S_OK : S_FALSE;
4536 break;
4537 case Uri_PROPERTY_DISPLAY_URI:
4538 if(This->scheme_type != URL_SCHEME_UNKNOWN && This->userinfo_start > -1)
4539 *pcchProperty = This->canon_len-This->userinfo_len-1;
4540 else
4541 *pcchProperty = This->canon_len;
4543 hres = S_OK;
4544 break;
4545 case Uri_PROPERTY_DOMAIN:
4546 if(This->domain_offset > -1)
4547 *pcchProperty = This->host_len - This->domain_offset;
4548 else
4549 *pcchProperty = 0;
4551 hres = (This->domain_offset > -1) ? S_OK : S_FALSE;
4552 break;
4553 case Uri_PROPERTY_EXTENSION:
4554 if(This->extension_offset > -1) {
4555 *pcchProperty = This->path_len - This->extension_offset;
4556 hres = S_OK;
4557 } else {
4558 *pcchProperty = 0;
4559 hres = S_FALSE;
4562 break;
4563 case Uri_PROPERTY_FRAGMENT:
4564 *pcchProperty = This->fragment_len;
4565 hres = (This->fragment_start > -1) ? S_OK : S_FALSE;
4566 break;
4567 case Uri_PROPERTY_HOST:
4568 *pcchProperty = This->host_len;
4570 /* '[' and ']' aren't included in the length. */
4571 if(This->host_type == Uri_HOST_IPV6)
4572 *pcchProperty -= 2;
4574 hres = (This->host_start > -1) ? S_OK : S_FALSE;
4575 break;
4576 case Uri_PROPERTY_PASSWORD:
4577 *pcchProperty = (This->userinfo_split > -1) ? This->userinfo_len-This->userinfo_split-1 : 0;
4578 hres = (This->userinfo_split > -1) ? S_OK : S_FALSE;
4579 break;
4580 case Uri_PROPERTY_PATH:
4581 *pcchProperty = This->path_len;
4582 hres = (This->path_start > -1) ? S_OK : S_FALSE;
4583 break;
4584 case Uri_PROPERTY_PATH_AND_QUERY:
4585 *pcchProperty = This->path_len+This->query_len;
4586 hres = (This->path_start > -1 || This->query_start > -1) ? S_OK : S_FALSE;
4587 break;
4588 case Uri_PROPERTY_QUERY:
4589 *pcchProperty = This->query_len;
4590 hres = (This->query_start > -1) ? S_OK : S_FALSE;
4591 break;
4592 case Uri_PROPERTY_RAW_URI:
4593 *pcchProperty = SysStringLen(This->raw_uri);
4594 hres = S_OK;
4595 break;
4596 case Uri_PROPERTY_SCHEME_NAME:
4597 *pcchProperty = This->scheme_len;
4598 hres = (This->scheme_start > -1) ? S_OK : S_FALSE;
4599 break;
4600 case Uri_PROPERTY_USER_INFO:
4601 *pcchProperty = This->userinfo_len;
4602 hres = (This->userinfo_start > -1) ? S_OK : S_FALSE;
4603 break;
4604 case Uri_PROPERTY_USER_NAME:
4605 *pcchProperty = (This->userinfo_split > -1) ? This->userinfo_split : This->userinfo_len;
4606 if(This->userinfo_split == 0)
4607 hres = S_FALSE;
4608 else
4609 hres = (This->userinfo_start > -1) ? S_OK : S_FALSE;
4610 break;
4611 default:
4612 FIXME("(%p)->(%d %p %x)\n", This, uriProp, pcchProperty, dwFlags);
4613 hres = E_NOTIMPL;
4616 return hres;
4619 static HRESULT WINAPI Uri_GetPropertyDWORD(IUri *iface, Uri_PROPERTY uriProp, DWORD *pcchProperty, DWORD dwFlags)
4621 Uri *This = impl_from_IUri(iface);
4622 HRESULT hres;
4624 TRACE("(%p)->(%d %p %x)\n", This, uriProp, pcchProperty, dwFlags);
4626 if(!pcchProperty)
4627 return E_INVALIDARG;
4629 /* Microsoft's implementation for the ZONE property of a URI seems to be lacking...
4630 * From what I can tell, instead of checking which URLZONE the URI belongs to it
4631 * simply assigns URLZONE_INVALID and returns E_NOTIMPL. This also applies to the GetZone
4632 * function.
4634 if(uriProp == Uri_PROPERTY_ZONE) {
4635 *pcchProperty = URLZONE_INVALID;
4636 return E_NOTIMPL;
4639 if(uriProp < Uri_PROPERTY_DWORD_START) {
4640 *pcchProperty = 0;
4641 return E_INVALIDARG;
4644 switch(uriProp) {
4645 case Uri_PROPERTY_HOST_TYPE:
4646 *pcchProperty = This->host_type;
4647 hres = S_OK;
4648 break;
4649 case Uri_PROPERTY_PORT:
4650 if(!This->has_port) {
4651 *pcchProperty = 0;
4652 hres = S_FALSE;
4653 } else {
4654 *pcchProperty = This->port;
4655 hres = S_OK;
4658 break;
4659 case Uri_PROPERTY_SCHEME:
4660 *pcchProperty = This->scheme_type;
4661 hres = S_OK;
4662 break;
4663 default:
4664 FIXME("(%p)->(%d %p %x)\n", This, uriProp, pcchProperty, dwFlags);
4665 hres = E_NOTIMPL;
4668 return hres;
4671 static HRESULT WINAPI Uri_HasProperty(IUri *iface, Uri_PROPERTY uriProp, BOOL *pfHasProperty)
4673 Uri *This = impl_from_IUri(iface);
4674 TRACE("(%p)->(%d %p)\n", This, uriProp, pfHasProperty);
4676 if(!pfHasProperty)
4677 return E_INVALIDARG;
4679 switch(uriProp) {
4680 case Uri_PROPERTY_ABSOLUTE_URI:
4681 *pfHasProperty = !(This->display_modifiers & URI_DISPLAY_NO_ABSOLUTE_URI);
4682 break;
4683 case Uri_PROPERTY_AUTHORITY:
4684 *pfHasProperty = This->authority_start > -1;
4685 break;
4686 case Uri_PROPERTY_DISPLAY_URI:
4687 *pfHasProperty = TRUE;
4688 break;
4689 case Uri_PROPERTY_DOMAIN:
4690 *pfHasProperty = This->domain_offset > -1;
4691 break;
4692 case Uri_PROPERTY_EXTENSION:
4693 *pfHasProperty = This->extension_offset > -1;
4694 break;
4695 case Uri_PROPERTY_FRAGMENT:
4696 *pfHasProperty = This->fragment_start > -1;
4697 break;
4698 case Uri_PROPERTY_HOST:
4699 *pfHasProperty = This->host_start > -1;
4700 break;
4701 case Uri_PROPERTY_PASSWORD:
4702 *pfHasProperty = This->userinfo_split > -1;
4703 break;
4704 case Uri_PROPERTY_PATH:
4705 *pfHasProperty = This->path_start > -1;
4706 break;
4707 case Uri_PROPERTY_PATH_AND_QUERY:
4708 *pfHasProperty = (This->path_start > -1 || This->query_start > -1);
4709 break;
4710 case Uri_PROPERTY_QUERY:
4711 *pfHasProperty = This->query_start > -1;
4712 break;
4713 case Uri_PROPERTY_RAW_URI:
4714 *pfHasProperty = TRUE;
4715 break;
4716 case Uri_PROPERTY_SCHEME_NAME:
4717 *pfHasProperty = This->scheme_start > -1;
4718 break;
4719 case Uri_PROPERTY_USER_INFO:
4720 *pfHasProperty = This->userinfo_start > -1;
4721 break;
4722 case Uri_PROPERTY_USER_NAME:
4723 if(This->userinfo_split == 0)
4724 *pfHasProperty = FALSE;
4725 else
4726 *pfHasProperty = This->userinfo_start > -1;
4727 break;
4728 case Uri_PROPERTY_HOST_TYPE:
4729 *pfHasProperty = TRUE;
4730 break;
4731 case Uri_PROPERTY_PORT:
4732 *pfHasProperty = This->has_port;
4733 break;
4734 case Uri_PROPERTY_SCHEME:
4735 *pfHasProperty = TRUE;
4736 break;
4737 case Uri_PROPERTY_ZONE:
4738 *pfHasProperty = FALSE;
4739 break;
4740 default:
4741 FIXME("(%p)->(%d %p): Unsupported property type.\n", This, uriProp, pfHasProperty);
4742 return E_NOTIMPL;
4745 return S_OK;
4748 static HRESULT WINAPI Uri_GetAbsoluteUri(IUri *iface, BSTR *pstrAbsoluteUri)
4750 TRACE("(%p)->(%p)\n", iface, pstrAbsoluteUri);
4751 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_ABSOLUTE_URI, pstrAbsoluteUri, 0);
4754 static HRESULT WINAPI Uri_GetAuthority(IUri *iface, BSTR *pstrAuthority)
4756 TRACE("(%p)->(%p)\n", iface, pstrAuthority);
4757 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_AUTHORITY, pstrAuthority, 0);
4760 static HRESULT WINAPI Uri_GetDisplayUri(IUri *iface, BSTR *pstrDisplayUri)
4762 TRACE("(%p)->(%p)\n", iface, pstrDisplayUri);
4763 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_DISPLAY_URI, pstrDisplayUri, 0);
4766 static HRESULT WINAPI Uri_GetDomain(IUri *iface, BSTR *pstrDomain)
4768 TRACE("(%p)->(%p)\n", iface, pstrDomain);
4769 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_DOMAIN, pstrDomain, 0);
4772 static HRESULT WINAPI Uri_GetExtension(IUri *iface, BSTR *pstrExtension)
4774 TRACE("(%p)->(%p)\n", iface, pstrExtension);
4775 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_EXTENSION, pstrExtension, 0);
4778 static HRESULT WINAPI Uri_GetFragment(IUri *iface, BSTR *pstrFragment)
4780 TRACE("(%p)->(%p)\n", iface, pstrFragment);
4781 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_FRAGMENT, pstrFragment, 0);
4784 static HRESULT WINAPI Uri_GetHost(IUri *iface, BSTR *pstrHost)
4786 TRACE("(%p)->(%p)\n", iface, pstrHost);
4787 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_HOST, pstrHost, 0);
4790 static HRESULT WINAPI Uri_GetPassword(IUri *iface, BSTR *pstrPassword)
4792 TRACE("(%p)->(%p)\n", iface, pstrPassword);
4793 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_PASSWORD, pstrPassword, 0);
4796 static HRESULT WINAPI Uri_GetPath(IUri *iface, BSTR *pstrPath)
4798 TRACE("(%p)->(%p)\n", iface, pstrPath);
4799 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_PATH, pstrPath, 0);
4802 static HRESULT WINAPI Uri_GetPathAndQuery(IUri *iface, BSTR *pstrPathAndQuery)
4804 TRACE("(%p)->(%p)\n", iface, pstrPathAndQuery);
4805 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_PATH_AND_QUERY, pstrPathAndQuery, 0);
4808 static HRESULT WINAPI Uri_GetQuery(IUri *iface, BSTR *pstrQuery)
4810 TRACE("(%p)->(%p)\n", iface, pstrQuery);
4811 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_QUERY, pstrQuery, 0);
4814 static HRESULT WINAPI Uri_GetRawUri(IUri *iface, BSTR *pstrRawUri)
4816 TRACE("(%p)->(%p)\n", iface, pstrRawUri);
4817 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_RAW_URI, pstrRawUri, 0);
4820 static HRESULT WINAPI Uri_GetSchemeName(IUri *iface, BSTR *pstrSchemeName)
4822 TRACE("(%p)->(%p)\n", iface, pstrSchemeName);
4823 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_SCHEME_NAME, pstrSchemeName, 0);
4826 static HRESULT WINAPI Uri_GetUserInfo(IUri *iface, BSTR *pstrUserInfo)
4828 TRACE("(%p)->(%p)\n", iface, pstrUserInfo);
4829 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_USER_INFO, pstrUserInfo, 0);
4832 static HRESULT WINAPI Uri_GetUserName(IUri *iface, BSTR *pstrUserName)
4834 TRACE("(%p)->(%p)\n", iface, pstrUserName);
4835 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_USER_NAME, pstrUserName, 0);
4838 static HRESULT WINAPI Uri_GetHostType(IUri *iface, DWORD *pdwHostType)
4840 TRACE("(%p)->(%p)\n", iface, pdwHostType);
4841 return IUri_GetPropertyDWORD(iface, Uri_PROPERTY_HOST_TYPE, pdwHostType, 0);
4844 static HRESULT WINAPI Uri_GetPort(IUri *iface, DWORD *pdwPort)
4846 TRACE("(%p)->(%p)\n", iface, pdwPort);
4847 return IUri_GetPropertyDWORD(iface, Uri_PROPERTY_PORT, pdwPort, 0);
4850 static HRESULT WINAPI Uri_GetScheme(IUri *iface, DWORD *pdwScheme)
4852 TRACE("(%p)->(%p)\n", iface, pdwScheme);
4853 return IUri_GetPropertyDWORD(iface, Uri_PROPERTY_SCHEME, pdwScheme, 0);
4856 static HRESULT WINAPI Uri_GetZone(IUri *iface, DWORD *pdwZone)
4858 TRACE("(%p)->(%p)\n", iface, pdwZone);
4859 return IUri_GetPropertyDWORD(iface, Uri_PROPERTY_ZONE,pdwZone, 0);
4862 static HRESULT WINAPI Uri_GetProperties(IUri *iface, DWORD *pdwProperties)
4864 Uri *This = impl_from_IUri(iface);
4865 TRACE("(%p)->(%p)\n", This, pdwProperties);
4867 if(!pdwProperties)
4868 return E_INVALIDARG;
4870 /* All URIs have these. */
4871 *pdwProperties = Uri_HAS_DISPLAY_URI|Uri_HAS_RAW_URI|Uri_HAS_SCHEME|Uri_HAS_HOST_TYPE;
4873 if(!(This->display_modifiers & URI_DISPLAY_NO_ABSOLUTE_URI))
4874 *pdwProperties |= Uri_HAS_ABSOLUTE_URI;
4876 if(This->scheme_start > -1)
4877 *pdwProperties |= Uri_HAS_SCHEME_NAME;
4879 if(This->authority_start > -1) {
4880 *pdwProperties |= Uri_HAS_AUTHORITY;
4881 if(This->userinfo_start > -1) {
4882 *pdwProperties |= Uri_HAS_USER_INFO;
4883 if(This->userinfo_split != 0)
4884 *pdwProperties |= Uri_HAS_USER_NAME;
4886 if(This->userinfo_split > -1)
4887 *pdwProperties |= Uri_HAS_PASSWORD;
4888 if(This->host_start > -1)
4889 *pdwProperties |= Uri_HAS_HOST;
4890 if(This->domain_offset > -1)
4891 *pdwProperties |= Uri_HAS_DOMAIN;
4894 if(This->has_port)
4895 *pdwProperties |= Uri_HAS_PORT;
4896 if(This->path_start > -1)
4897 *pdwProperties |= Uri_HAS_PATH|Uri_HAS_PATH_AND_QUERY;
4898 if(This->query_start > -1)
4899 *pdwProperties |= Uri_HAS_QUERY|Uri_HAS_PATH_AND_QUERY;
4901 if(This->extension_offset > -1)
4902 *pdwProperties |= Uri_HAS_EXTENSION;
4904 if(This->fragment_start > -1)
4905 *pdwProperties |= Uri_HAS_FRAGMENT;
4907 return S_OK;
4910 static HRESULT WINAPI Uri_IsEqual(IUri *iface, IUri *pUri, BOOL *pfEqual)
4912 Uri *This = impl_from_IUri(iface);
4913 Uri *other;
4915 TRACE("(%p)->(%p %p)\n", This, pUri, pfEqual);
4917 if(!pfEqual)
4918 return E_POINTER;
4920 if(!pUri) {
4921 *pfEqual = FALSE;
4923 /* For some reason Windows returns S_OK here... */
4924 return S_OK;
4927 /* Try to convert it to a Uri (allows for a more simple comparison). */
4928 if((other = get_uri_obj(pUri)))
4929 *pfEqual = are_equal_simple(This, other);
4930 else {
4931 /* Do it the hard way. */
4932 FIXME("(%p)->(%p %p) No support for unknown IUri's yet.\n", iface, pUri, pfEqual);
4933 return E_NOTIMPL;
4936 return S_OK;
4939 static const IUriVtbl UriVtbl = {
4940 Uri_QueryInterface,
4941 Uri_AddRef,
4942 Uri_Release,
4943 Uri_GetPropertyBSTR,
4944 Uri_GetPropertyLength,
4945 Uri_GetPropertyDWORD,
4946 Uri_HasProperty,
4947 Uri_GetAbsoluteUri,
4948 Uri_GetAuthority,
4949 Uri_GetDisplayUri,
4950 Uri_GetDomain,
4951 Uri_GetExtension,
4952 Uri_GetFragment,
4953 Uri_GetHost,
4954 Uri_GetPassword,
4955 Uri_GetPath,
4956 Uri_GetPathAndQuery,
4957 Uri_GetQuery,
4958 Uri_GetRawUri,
4959 Uri_GetSchemeName,
4960 Uri_GetUserInfo,
4961 Uri_GetUserName,
4962 Uri_GetHostType,
4963 Uri_GetPort,
4964 Uri_GetScheme,
4965 Uri_GetZone,
4966 Uri_GetProperties,
4967 Uri_IsEqual
4970 static inline Uri* impl_from_IUriBuilderFactory(IUriBuilderFactory *iface)
4972 return CONTAINING_RECORD(iface, Uri, IUriBuilderFactory_iface);
4975 static HRESULT WINAPI UriBuilderFactory_QueryInterface(IUriBuilderFactory *iface, REFIID riid, void **ppv)
4977 Uri *This = impl_from_IUriBuilderFactory(iface);
4979 if(IsEqualGUID(&IID_IUnknown, riid)) {
4980 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
4981 *ppv = &This->IUriBuilderFactory_iface;
4982 }else if(IsEqualGUID(&IID_IUriBuilderFactory, riid)) {
4983 TRACE("(%p)->(IID_IUriBuilderFactory %p)\n", This, ppv);
4984 *ppv = &This->IUriBuilderFactory_iface;
4985 }else if(IsEqualGUID(&IID_IUri, riid)) {
4986 TRACE("(%p)->(IID_IUri %p)\n", This, ppv);
4987 *ppv = &This->IUri_iface;
4988 }else {
4989 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
4990 *ppv = NULL;
4991 return E_NOINTERFACE;
4994 IUnknown_AddRef((IUnknown*)*ppv);
4995 return S_OK;
4998 static ULONG WINAPI UriBuilderFactory_AddRef(IUriBuilderFactory *iface)
5000 Uri *This = impl_from_IUriBuilderFactory(iface);
5001 LONG ref = InterlockedIncrement(&This->ref);
5003 TRACE("(%p) ref=%d\n", This, ref);
5005 return ref;
5008 static ULONG WINAPI UriBuilderFactory_Release(IUriBuilderFactory *iface)
5010 Uri *This = impl_from_IUriBuilderFactory(iface);
5011 LONG ref = InterlockedDecrement(&This->ref);
5013 TRACE("(%p) ref=%d\n", This, ref);
5015 if(!ref)
5016 destory_uri_obj(This);
5018 return ref;
5021 static HRESULT WINAPI UriBuilderFactory_CreateIUriBuilder(IUriBuilderFactory *iface,
5022 DWORD dwFlags,
5023 DWORD_PTR dwReserved,
5024 IUriBuilder **ppIUriBuilder)
5026 Uri *This = impl_from_IUriBuilderFactory(iface);
5027 TRACE("(%p)->(%08x %08x %p)\n", This, dwFlags, (DWORD)dwReserved, ppIUriBuilder);
5029 if(!ppIUriBuilder)
5030 return E_POINTER;
5032 if(dwFlags || dwReserved) {
5033 *ppIUriBuilder = NULL;
5034 return E_INVALIDARG;
5037 return CreateIUriBuilder(NULL, 0, 0, ppIUriBuilder);
5040 static HRESULT WINAPI UriBuilderFactory_CreateInitializedIUriBuilder(IUriBuilderFactory *iface,
5041 DWORD dwFlags,
5042 DWORD_PTR dwReserved,
5043 IUriBuilder **ppIUriBuilder)
5045 Uri *This = impl_from_IUriBuilderFactory(iface);
5046 TRACE("(%p)->(%08x %08x %p)\n", This, dwFlags, (DWORD)dwReserved, ppIUriBuilder);
5048 if(!ppIUriBuilder)
5049 return E_POINTER;
5051 if(dwFlags || dwReserved) {
5052 *ppIUriBuilder = NULL;
5053 return E_INVALIDARG;
5056 return CreateIUriBuilder(&This->IUri_iface, 0, 0, ppIUriBuilder);
5059 static const IUriBuilderFactoryVtbl UriBuilderFactoryVtbl = {
5060 UriBuilderFactory_QueryInterface,
5061 UriBuilderFactory_AddRef,
5062 UriBuilderFactory_Release,
5063 UriBuilderFactory_CreateIUriBuilder,
5064 UriBuilderFactory_CreateInitializedIUriBuilder
5067 static Uri* create_uri_obj(void) {
5068 Uri *ret = heap_alloc_zero(sizeof(Uri));
5069 if(ret) {
5070 ret->IUri_iface.lpVtbl = &UriVtbl;
5071 ret->IUriBuilderFactory_iface.lpVtbl = &UriBuilderFactoryVtbl;
5072 ret->ref = 1;
5075 return ret;
5078 /***********************************************************************
5079 * CreateUri (urlmon.@)
5081 * Creates a new IUri object using the URI represented by pwzURI. This function
5082 * parses and validates the components of pwzURI and then canonicalizes the
5083 * parsed components.
5085 * PARAMS
5086 * pwzURI [I] The URI to parse, validate, and canonicalize.
5087 * dwFlags [I] Flags which can affect how the parsing/canonicalization is performed.
5088 * dwReserved [I] Reserved (not used).
5089 * ppURI [O] The resulting IUri after parsing/canonicalization occurs.
5091 * RETURNS
5092 * Success: Returns S_OK. ppURI contains the pointer to the newly allocated IUri.
5093 * Failure: E_INVALIDARG if there's invalid flag combinations in dwFlags, or an
5094 * invalid parameters, or pwzURI doesn't represnt a valid URI.
5095 * E_OUTOFMEMORY if any memory allocation fails.
5097 * NOTES
5098 * Default flags:
5099 * Uri_CREATE_CANONICALIZE, Uri_CREATE_DECODE_EXTRA_INFO, Uri_CREATE_CRACK_UNKNOWN_SCHEMES,
5100 * Uri_CREATE_PRE_PROCESS_HTML_URI, Uri_CREATE_NO_IE_SETTINGS.
5102 HRESULT WINAPI CreateUri(LPCWSTR pwzURI, DWORD dwFlags, DWORD_PTR dwReserved, IUri **ppURI)
5104 const DWORD supported_flags = Uri_CREATE_ALLOW_RELATIVE|Uri_CREATE_ALLOW_IMPLICIT_WILDCARD_SCHEME|
5105 Uri_CREATE_ALLOW_IMPLICIT_FILE_SCHEME|Uri_CREATE_NO_CANONICALIZE|Uri_CREATE_CANONICALIZE|
5106 Uri_CREATE_DECODE_EXTRA_INFO|Uri_CREATE_NO_DECODE_EXTRA_INFO|Uri_CREATE_CRACK_UNKNOWN_SCHEMES|
5107 Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES|Uri_CREATE_PRE_PROCESS_HTML_URI|Uri_CREATE_NO_PRE_PROCESS_HTML_URI|
5108 Uri_CREATE_NO_IE_SETTINGS|Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS|Uri_CREATE_FILE_USE_DOS_PATH;
5109 Uri *ret;
5110 HRESULT hr;
5111 parse_data data;
5113 TRACE("(%s %x %x %p)\n", debugstr_w(pwzURI), dwFlags, (DWORD)dwReserved, ppURI);
5115 if(!ppURI)
5116 return E_INVALIDARG;
5118 if(!pwzURI) {
5119 *ppURI = NULL;
5120 return E_INVALIDARG;
5123 /* Check for invalid flags. */
5124 if(has_invalid_flag_combination(dwFlags)) {
5125 *ppURI = NULL;
5126 return E_INVALIDARG;
5129 /* Currently unsupported. */
5130 if(dwFlags & ~supported_flags)
5131 FIXME("Ignoring unsupported flag(s) %x\n", dwFlags & ~supported_flags);
5133 ret = create_uri_obj();
5134 if(!ret) {
5135 *ppURI = NULL;
5136 return E_OUTOFMEMORY;
5139 /* Explicitly set the default flags if it doesn't cause a flag conflict. */
5140 apply_default_flags(&dwFlags);
5142 /* Pre process the URI, unless told otherwise. */
5143 if(!(dwFlags & Uri_CREATE_NO_PRE_PROCESS_HTML_URI))
5144 ret->raw_uri = pre_process_uri(pwzURI);
5145 else
5146 ret->raw_uri = SysAllocString(pwzURI);
5148 if(!ret->raw_uri) {
5149 heap_free(ret);
5150 return E_OUTOFMEMORY;
5153 memset(&data, 0, sizeof(parse_data));
5154 data.uri = ret->raw_uri;
5156 /* Validate and parse the URI into it's components. */
5157 if(!parse_uri(&data, dwFlags)) {
5158 /* Encountered an unsupported or invalid URI */
5159 IUri_Release(&ret->IUri_iface);
5160 *ppURI = NULL;
5161 return E_INVALIDARG;
5164 /* Canonicalize the URI. */
5165 hr = canonicalize_uri(&data, ret, dwFlags);
5166 if(FAILED(hr)) {
5167 IUri_Release(&ret->IUri_iface);
5168 *ppURI = NULL;
5169 return hr;
5172 ret->create_flags = dwFlags;
5174 *ppURI = &ret->IUri_iface;
5175 return S_OK;
5178 /***********************************************************************
5179 * CreateUriWithFragment (urlmon.@)
5181 * Creates a new IUri object. This is almost the same as CreateUri, expect that
5182 * it allows you to explicitly specify a fragment (pwzFragment) for pwzURI.
5184 * PARAMS
5185 * pwzURI [I] The URI to parse and perform canonicalization on.
5186 * pwzFragment [I] The explict fragment string which should be added to pwzURI.
5187 * dwFlags [I] The flags which will be passed to CreateUri.
5188 * dwReserved [I] Reserved (not used).
5189 * ppURI [O] The resulting IUri after parsing/canonicalization.
5191 * RETURNS
5192 * Success: S_OK. ppURI contains the pointer to the newly allocated IUri.
5193 * Failure: E_INVALIDARG if pwzURI already contains a fragment and pwzFragment
5194 * isn't NULL. Will also return E_INVALIDARG for the same reasons as
5195 * CreateUri will. E_OUTOFMEMORY if any allocations fail.
5197 HRESULT WINAPI CreateUriWithFragment(LPCWSTR pwzURI, LPCWSTR pwzFragment, DWORD dwFlags,
5198 DWORD_PTR dwReserved, IUri **ppURI)
5200 HRESULT hres;
5201 TRACE("(%s %s %x %x %p)\n", debugstr_w(pwzURI), debugstr_w(pwzFragment), dwFlags, (DWORD)dwReserved, ppURI);
5203 if(!ppURI)
5204 return E_INVALIDARG;
5206 if(!pwzURI) {
5207 *ppURI = NULL;
5208 return E_INVALIDARG;
5211 /* Check if a fragment should be appended to the URI string. */
5212 if(pwzFragment) {
5213 WCHAR *uriW;
5214 DWORD uri_len, frag_len;
5215 BOOL add_pound;
5217 /* Check if the original URI already has a fragment component. */
5218 if(StrChrW(pwzURI, '#')) {
5219 *ppURI = NULL;
5220 return E_INVALIDARG;
5223 uri_len = lstrlenW(pwzURI);
5224 frag_len = lstrlenW(pwzFragment);
5226 /* If the fragment doesn't start with a '#', one will be added. */
5227 add_pound = *pwzFragment != '#';
5229 if(add_pound)
5230 uriW = heap_alloc((uri_len+frag_len+2)*sizeof(WCHAR));
5231 else
5232 uriW = heap_alloc((uri_len+frag_len+1)*sizeof(WCHAR));
5234 if(!uriW)
5235 return E_OUTOFMEMORY;
5237 memcpy(uriW, pwzURI, uri_len*sizeof(WCHAR));
5238 if(add_pound)
5239 uriW[uri_len++] = '#';
5240 memcpy(uriW+uri_len, pwzFragment, (frag_len+1)*sizeof(WCHAR));
5242 hres = CreateUri(uriW, dwFlags, 0, ppURI);
5244 heap_free(uriW);
5245 } else
5246 /* A fragment string wasn't specified, so just forward the call. */
5247 hres = CreateUri(pwzURI, dwFlags, 0, ppURI);
5249 return hres;
5252 static HRESULT build_uri(const UriBuilder *builder, IUri **uri, DWORD create_flags,
5253 DWORD use_orig_flags, DWORD encoding_mask)
5255 HRESULT hr;
5256 parse_data data;
5257 Uri *ret;
5259 if(!uri)
5260 return E_POINTER;
5262 if(encoding_mask && (!builder->uri || builder->modified_props)) {
5263 *uri = NULL;
5264 return E_NOTIMPL;
5267 /* Decide what flags should be used when creating the Uri. */
5268 if((use_orig_flags & UriBuilder_USE_ORIGINAL_FLAGS) && builder->uri)
5269 create_flags = builder->uri->create_flags;
5270 else {
5271 if(has_invalid_flag_combination(create_flags)) {
5272 *uri = NULL;
5273 return E_INVALIDARG;
5276 /* Set the default flags if they don't cause a conflict. */
5277 apply_default_flags(&create_flags);
5280 /* Return the base IUri if no changes have been made and the create_flags match. */
5281 if(builder->uri && !builder->modified_props && builder->uri->create_flags == create_flags) {
5282 *uri = &builder->uri->IUri_iface;
5283 IUri_AddRef(*uri);
5284 return S_OK;
5287 hr = validate_components(builder, &data, create_flags);
5288 if(FAILED(hr)) {
5289 *uri = NULL;
5290 return hr;
5293 ret = create_uri_obj();
5294 if(!ret) {
5295 *uri = NULL;
5296 return E_OUTOFMEMORY;
5299 hr = generate_uri(builder, &data, ret, create_flags);
5300 if(FAILED(hr)) {
5301 IUri_Release(&ret->IUri_iface);
5302 *uri = NULL;
5303 return hr;
5306 *uri = &ret->IUri_iface;
5307 return S_OK;
5310 static inline UriBuilder* impl_from_IUriBuilder(IUriBuilder *iface)
5312 return CONTAINING_RECORD(iface, UriBuilder, IUriBuilder_iface);
5315 static HRESULT WINAPI UriBuilder_QueryInterface(IUriBuilder *iface, REFIID riid, void **ppv)
5317 UriBuilder *This = impl_from_IUriBuilder(iface);
5319 if(IsEqualGUID(&IID_IUnknown, riid)) {
5320 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
5321 *ppv = &This->IUriBuilder_iface;
5322 }else if(IsEqualGUID(&IID_IUriBuilder, riid)) {
5323 TRACE("(%p)->(IID_IUriBuilder %p)\n", This, ppv);
5324 *ppv = &This->IUriBuilder_iface;
5325 }else {
5326 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
5327 *ppv = NULL;
5328 return E_NOINTERFACE;
5331 IUnknown_AddRef((IUnknown*)*ppv);
5332 return S_OK;
5335 static ULONG WINAPI UriBuilder_AddRef(IUriBuilder *iface)
5337 UriBuilder *This = impl_from_IUriBuilder(iface);
5338 LONG ref = InterlockedIncrement(&This->ref);
5340 TRACE("(%p) ref=%d\n", This, ref);
5342 return ref;
5345 static ULONG WINAPI UriBuilder_Release(IUriBuilder *iface)
5347 UriBuilder *This = impl_from_IUriBuilder(iface);
5348 LONG ref = InterlockedDecrement(&This->ref);
5350 TRACE("(%p) ref=%d\n", This, ref);
5352 if(!ref) {
5353 if(This->uri) IUri_Release(&This->uri->IUri_iface);
5354 heap_free(This->fragment);
5355 heap_free(This->host);
5356 heap_free(This->password);
5357 heap_free(This->path);
5358 heap_free(This->query);
5359 heap_free(This->scheme);
5360 heap_free(This->username);
5361 heap_free(This);
5364 return ref;
5367 static HRESULT WINAPI UriBuilder_CreateUriSimple(IUriBuilder *iface,
5368 DWORD dwAllowEncodingPropertyMask,
5369 DWORD_PTR dwReserved,
5370 IUri **ppIUri)
5372 UriBuilder *This = impl_from_IUriBuilder(iface);
5373 HRESULT hr;
5374 TRACE("(%p)->(%d %d %p)\n", This, dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
5376 hr = build_uri(This, ppIUri, 0, UriBuilder_USE_ORIGINAL_FLAGS, dwAllowEncodingPropertyMask);
5377 if(hr == E_NOTIMPL)
5378 FIXME("(%p)->(%d %d %p)\n", This, dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
5379 return hr;
5382 static HRESULT WINAPI UriBuilder_CreateUri(IUriBuilder *iface,
5383 DWORD dwCreateFlags,
5384 DWORD dwAllowEncodingPropertyMask,
5385 DWORD_PTR dwReserved,
5386 IUri **ppIUri)
5388 UriBuilder *This = impl_from_IUriBuilder(iface);
5389 HRESULT hr;
5390 TRACE("(%p)->(0x%08x %d %d %p)\n", This, dwCreateFlags, dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
5392 if(dwCreateFlags == -1)
5393 hr = build_uri(This, ppIUri, 0, UriBuilder_USE_ORIGINAL_FLAGS, dwAllowEncodingPropertyMask);
5394 else
5395 hr = build_uri(This, ppIUri, dwCreateFlags, 0, dwAllowEncodingPropertyMask);
5397 if(hr == E_NOTIMPL)
5398 FIXME("(%p)->(0x%08x %d %d %p)\n", This, dwCreateFlags, dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
5399 return hr;
5402 static HRESULT WINAPI UriBuilder_CreateUriWithFlags(IUriBuilder *iface,
5403 DWORD dwCreateFlags,
5404 DWORD dwUriBuilderFlags,
5405 DWORD dwAllowEncodingPropertyMask,
5406 DWORD_PTR dwReserved,
5407 IUri **ppIUri)
5409 UriBuilder *This = impl_from_IUriBuilder(iface);
5410 HRESULT hr;
5411 TRACE("(%p)->(0x%08x 0x%08x %d %d %p)\n", This, dwCreateFlags, dwUriBuilderFlags,
5412 dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
5414 hr = build_uri(This, ppIUri, dwCreateFlags, dwUriBuilderFlags, dwAllowEncodingPropertyMask);
5415 if(hr == E_NOTIMPL)
5416 FIXME("(%p)->(0x%08x 0x%08x %d %d %p)\n", This, dwCreateFlags, dwUriBuilderFlags,
5417 dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
5418 return hr;
5421 static HRESULT WINAPI UriBuilder_GetIUri(IUriBuilder *iface, IUri **ppIUri)
5423 UriBuilder *This = impl_from_IUriBuilder(iface);
5424 TRACE("(%p)->(%p)\n", This, ppIUri);
5426 if(!ppIUri)
5427 return E_POINTER;
5429 if(This->uri) {
5430 IUri *uri = &This->uri->IUri_iface;
5431 IUri_AddRef(uri);
5432 *ppIUri = uri;
5433 } else
5434 *ppIUri = NULL;
5436 return S_OK;
5439 static HRESULT WINAPI UriBuilder_SetIUri(IUriBuilder *iface, IUri *pIUri)
5441 UriBuilder *This = impl_from_IUriBuilder(iface);
5442 TRACE("(%p)->(%p)\n", This, pIUri);
5444 if(pIUri) {
5445 Uri *uri;
5447 if((uri = get_uri_obj(pIUri))) {
5448 /* Only reset the builder if it's Uri isn't the same as
5449 * the Uri passed to the function.
5451 if(This->uri != uri) {
5452 reset_builder(This);
5454 This->uri = uri;
5455 if(uri->has_port)
5456 This->port = uri->port;
5458 IUri_AddRef(pIUri);
5460 } else {
5461 FIXME("(%p)->(%p) Unknown IUri types not supported yet.\n", This, pIUri);
5462 return E_NOTIMPL;
5464 } else if(This->uri)
5465 /* Only reset the builder if it's Uri isn't NULL. */
5466 reset_builder(This);
5468 return S_OK;
5471 static HRESULT WINAPI UriBuilder_GetFragment(IUriBuilder *iface, DWORD *pcchFragment, LPCWSTR *ppwzFragment)
5473 UriBuilder *This = impl_from_IUriBuilder(iface);
5474 TRACE("(%p)->(%p %p)\n", This, pcchFragment, ppwzFragment);
5476 if(!This->uri || This->uri->fragment_start == -1 || This->modified_props & Uri_HAS_FRAGMENT)
5477 return get_builder_component(&This->fragment, &This->fragment_len, NULL, 0, ppwzFragment, pcchFragment);
5478 else
5479 return get_builder_component(&This->fragment, &This->fragment_len, This->uri->canon_uri+This->uri->fragment_start,
5480 This->uri->fragment_len, ppwzFragment, pcchFragment);
5483 static HRESULT WINAPI UriBuilder_GetHost(IUriBuilder *iface, DWORD *pcchHost, LPCWSTR *ppwzHost)
5485 UriBuilder *This = impl_from_IUriBuilder(iface);
5486 TRACE("(%p)->(%p %p)\n", This, pcchHost, ppwzHost);
5488 if(!This->uri || This->uri->host_start == -1 || This->modified_props & Uri_HAS_HOST)
5489 return get_builder_component(&This->host, &This->host_len, NULL, 0, ppwzHost, pcchHost);
5490 else {
5491 if(This->uri->host_type == Uri_HOST_IPV6)
5492 /* Don't include the '[' and ']' around the address. */
5493 return get_builder_component(&This->host, &This->host_len, This->uri->canon_uri+This->uri->host_start+1,
5494 This->uri->host_len-2, ppwzHost, pcchHost);
5495 else
5496 return get_builder_component(&This->host, &This->host_len, This->uri->canon_uri+This->uri->host_start,
5497 This->uri->host_len, ppwzHost, pcchHost);
5501 static HRESULT WINAPI UriBuilder_GetPassword(IUriBuilder *iface, DWORD *pcchPassword, LPCWSTR *ppwzPassword)
5503 UriBuilder *This = impl_from_IUriBuilder(iface);
5504 TRACE("(%p)->(%p %p)\n", This, pcchPassword, ppwzPassword);
5506 if(!This->uri || This->uri->userinfo_split == -1 || This->modified_props & Uri_HAS_PASSWORD)
5507 return get_builder_component(&This->password, &This->password_len, NULL, 0, ppwzPassword, pcchPassword);
5508 else {
5509 const WCHAR *start = This->uri->canon_uri+This->uri->userinfo_start+This->uri->userinfo_split+1;
5510 DWORD len = This->uri->userinfo_len-This->uri->userinfo_split-1;
5511 return get_builder_component(&This->password, &This->password_len, start, len, ppwzPassword, pcchPassword);
5515 static HRESULT WINAPI UriBuilder_GetPath(IUriBuilder *iface, DWORD *pcchPath, LPCWSTR *ppwzPath)
5517 UriBuilder *This = impl_from_IUriBuilder(iface);
5518 TRACE("(%p)->(%p %p)\n", This, pcchPath, ppwzPath);
5520 if(!This->uri || This->uri->path_start == -1 || This->modified_props & Uri_HAS_PATH)
5521 return get_builder_component(&This->path, &This->path_len, NULL, 0, ppwzPath, pcchPath);
5522 else
5523 return get_builder_component(&This->path, &This->path_len, This->uri->canon_uri+This->uri->path_start,
5524 This->uri->path_len, ppwzPath, pcchPath);
5527 static HRESULT WINAPI UriBuilder_GetPort(IUriBuilder *iface, BOOL *pfHasPort, DWORD *pdwPort)
5529 UriBuilder *This = impl_from_IUriBuilder(iface);
5530 TRACE("(%p)->(%p %p)\n", This, pfHasPort, pdwPort);
5532 if(!pfHasPort) {
5533 if(pdwPort)
5534 *pdwPort = 0;
5535 return E_POINTER;
5538 if(!pdwPort) {
5539 *pfHasPort = FALSE;
5540 return E_POINTER;
5543 *pfHasPort = This->has_port;
5544 *pdwPort = This->port;
5545 return S_OK;
5548 static HRESULT WINAPI UriBuilder_GetQuery(IUriBuilder *iface, DWORD *pcchQuery, LPCWSTR *ppwzQuery)
5550 UriBuilder *This = impl_from_IUriBuilder(iface);
5551 TRACE("(%p)->(%p %p)\n", This, pcchQuery, ppwzQuery);
5553 if(!This->uri || This->uri->query_start == -1 || This->modified_props & Uri_HAS_QUERY)
5554 return get_builder_component(&This->query, &This->query_len, NULL, 0, ppwzQuery, pcchQuery);
5555 else
5556 return get_builder_component(&This->query, &This->query_len, This->uri->canon_uri+This->uri->query_start,
5557 This->uri->query_len, ppwzQuery, pcchQuery);
5560 static HRESULT WINAPI UriBuilder_GetSchemeName(IUriBuilder *iface, DWORD *pcchSchemeName, LPCWSTR *ppwzSchemeName)
5562 UriBuilder *This = impl_from_IUriBuilder(iface);
5563 TRACE("(%p)->(%p %p)\n", This, pcchSchemeName, ppwzSchemeName);
5565 if(!This->uri || This->uri->scheme_start == -1 || This->modified_props & Uri_HAS_SCHEME_NAME)
5566 return get_builder_component(&This->scheme, &This->scheme_len, NULL, 0, ppwzSchemeName, pcchSchemeName);
5567 else
5568 return get_builder_component(&This->scheme, &This->scheme_len, This->uri->canon_uri+This->uri->scheme_start,
5569 This->uri->scheme_len, ppwzSchemeName, pcchSchemeName);
5572 static HRESULT WINAPI UriBuilder_GetUserName(IUriBuilder *iface, DWORD *pcchUserName, LPCWSTR *ppwzUserName)
5574 UriBuilder *This = impl_from_IUriBuilder(iface);
5575 TRACE("(%p)->(%p %p)\n", This, pcchUserName, ppwzUserName);
5577 if(!This->uri || This->uri->userinfo_start == -1 || This->uri->userinfo_split == 0 ||
5578 This->modified_props & Uri_HAS_USER_NAME)
5579 return get_builder_component(&This->username, &This->username_len, NULL, 0, ppwzUserName, pcchUserName);
5580 else {
5581 const WCHAR *start = This->uri->canon_uri+This->uri->userinfo_start;
5583 /* Check if there's a password in the userinfo section. */
5584 if(This->uri->userinfo_split > -1)
5585 /* Don't include the password. */
5586 return get_builder_component(&This->username, &This->username_len, start,
5587 This->uri->userinfo_split, ppwzUserName, pcchUserName);
5588 else
5589 return get_builder_component(&This->username, &This->username_len, start,
5590 This->uri->userinfo_len, ppwzUserName, pcchUserName);
5594 static HRESULT WINAPI UriBuilder_SetFragment(IUriBuilder *iface, LPCWSTR pwzNewValue)
5596 UriBuilder *This = impl_from_IUriBuilder(iface);
5597 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
5598 return set_builder_component(&This->fragment, &This->fragment_len, pwzNewValue, '#',
5599 &This->modified_props, Uri_HAS_FRAGMENT);
5602 static HRESULT WINAPI UriBuilder_SetHost(IUriBuilder *iface, LPCWSTR pwzNewValue)
5604 UriBuilder *This = impl_from_IUriBuilder(iface);
5605 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
5607 /* Host name can't be set to NULL. */
5608 if(!pwzNewValue)
5609 return E_INVALIDARG;
5611 return set_builder_component(&This->host, &This->host_len, pwzNewValue, 0,
5612 &This->modified_props, Uri_HAS_HOST);
5615 static HRESULT WINAPI UriBuilder_SetPassword(IUriBuilder *iface, LPCWSTR pwzNewValue)
5617 UriBuilder *This = impl_from_IUriBuilder(iface);
5618 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
5619 return set_builder_component(&This->password, &This->password_len, pwzNewValue, 0,
5620 &This->modified_props, Uri_HAS_PASSWORD);
5623 static HRESULT WINAPI UriBuilder_SetPath(IUriBuilder *iface, LPCWSTR pwzNewValue)
5625 UriBuilder *This = impl_from_IUriBuilder(iface);
5626 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
5627 return set_builder_component(&This->path, &This->path_len, pwzNewValue, 0,
5628 &This->modified_props, Uri_HAS_PATH);
5631 static HRESULT WINAPI UriBuilder_SetPort(IUriBuilder *iface, BOOL fHasPort, DWORD dwNewValue)
5633 UriBuilder *This = impl_from_IUriBuilder(iface);
5634 TRACE("(%p)->(%d %d)\n", This, fHasPort, dwNewValue);
5636 This->has_port = fHasPort;
5637 This->port = dwNewValue;
5638 This->modified_props |= Uri_HAS_PORT;
5639 return S_OK;
5642 static HRESULT WINAPI UriBuilder_SetQuery(IUriBuilder *iface, LPCWSTR pwzNewValue)
5644 UriBuilder *This = impl_from_IUriBuilder(iface);
5645 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
5646 return set_builder_component(&This->query, &This->query_len, pwzNewValue, '?',
5647 &This->modified_props, Uri_HAS_QUERY);
5650 static HRESULT WINAPI UriBuilder_SetSchemeName(IUriBuilder *iface, LPCWSTR pwzNewValue)
5652 UriBuilder *This = impl_from_IUriBuilder(iface);
5653 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
5655 /* Only set the scheme name if it's not NULL or empty. */
5656 if(!pwzNewValue || !*pwzNewValue)
5657 return E_INVALIDARG;
5659 return set_builder_component(&This->scheme, &This->scheme_len, pwzNewValue, 0,
5660 &This->modified_props, Uri_HAS_SCHEME_NAME);
5663 static HRESULT WINAPI UriBuilder_SetUserName(IUriBuilder *iface, LPCWSTR pwzNewValue)
5665 UriBuilder *This = impl_from_IUriBuilder(iface);
5666 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
5667 return set_builder_component(&This->username, &This->username_len, pwzNewValue, 0,
5668 &This->modified_props, Uri_HAS_USER_NAME);
5671 static HRESULT WINAPI UriBuilder_RemoveProperties(IUriBuilder *iface, DWORD dwPropertyMask)
5673 const DWORD accepted_flags = Uri_HAS_AUTHORITY|Uri_HAS_DOMAIN|Uri_HAS_EXTENSION|Uri_HAS_FRAGMENT|Uri_HAS_HOST|
5674 Uri_HAS_PASSWORD|Uri_HAS_PATH|Uri_HAS_PATH_AND_QUERY|Uri_HAS_QUERY|
5675 Uri_HAS_USER_INFO|Uri_HAS_USER_NAME;
5677 UriBuilder *This = impl_from_IUriBuilder(iface);
5678 TRACE("(%p)->(0x%08x)\n", This, dwPropertyMask);
5680 if(dwPropertyMask & ~accepted_flags)
5681 return E_INVALIDARG;
5683 if(dwPropertyMask & Uri_HAS_FRAGMENT)
5684 UriBuilder_SetFragment(iface, NULL);
5686 /* Even though you can't set the host name to NULL or an
5687 * empty string, you can still remove it... for some reason.
5689 if(dwPropertyMask & Uri_HAS_HOST)
5690 set_builder_component(&This->host, &This->host_len, NULL, 0,
5691 &This->modified_props, Uri_HAS_HOST);
5693 if(dwPropertyMask & Uri_HAS_PASSWORD)
5694 UriBuilder_SetPassword(iface, NULL);
5696 if(dwPropertyMask & Uri_HAS_PATH)
5697 UriBuilder_SetPath(iface, NULL);
5699 if(dwPropertyMask & Uri_HAS_PORT)
5700 UriBuilder_SetPort(iface, FALSE, 0);
5702 if(dwPropertyMask & Uri_HAS_QUERY)
5703 UriBuilder_SetQuery(iface, NULL);
5705 if(dwPropertyMask & Uri_HAS_USER_NAME)
5706 UriBuilder_SetUserName(iface, NULL);
5708 return S_OK;
5711 static HRESULT WINAPI UriBuilder_HasBeenModified(IUriBuilder *iface, BOOL *pfModified)
5713 UriBuilder *This = impl_from_IUriBuilder(iface);
5714 TRACE("(%p)->(%p)\n", This, pfModified);
5716 if(!pfModified)
5717 return E_POINTER;
5719 *pfModified = This->modified_props > 0;
5720 return S_OK;
5723 static const IUriBuilderVtbl UriBuilderVtbl = {
5724 UriBuilder_QueryInterface,
5725 UriBuilder_AddRef,
5726 UriBuilder_Release,
5727 UriBuilder_CreateUriSimple,
5728 UriBuilder_CreateUri,
5729 UriBuilder_CreateUriWithFlags,
5730 UriBuilder_GetIUri,
5731 UriBuilder_SetIUri,
5732 UriBuilder_GetFragment,
5733 UriBuilder_GetHost,
5734 UriBuilder_GetPassword,
5735 UriBuilder_GetPath,
5736 UriBuilder_GetPort,
5737 UriBuilder_GetQuery,
5738 UriBuilder_GetSchemeName,
5739 UriBuilder_GetUserName,
5740 UriBuilder_SetFragment,
5741 UriBuilder_SetHost,
5742 UriBuilder_SetPassword,
5743 UriBuilder_SetPath,
5744 UriBuilder_SetPort,
5745 UriBuilder_SetQuery,
5746 UriBuilder_SetSchemeName,
5747 UriBuilder_SetUserName,
5748 UriBuilder_RemoveProperties,
5749 UriBuilder_HasBeenModified,
5752 /***********************************************************************
5753 * CreateIUriBuilder (urlmon.@)
5755 HRESULT WINAPI CreateIUriBuilder(IUri *pIUri, DWORD dwFlags, DWORD_PTR dwReserved, IUriBuilder **ppIUriBuilder)
5757 UriBuilder *ret;
5759 TRACE("(%p %x %x %p)\n", pIUri, dwFlags, (DWORD)dwReserved, ppIUriBuilder);
5761 if(!ppIUriBuilder)
5762 return E_POINTER;
5764 ret = heap_alloc_zero(sizeof(UriBuilder));
5765 if(!ret)
5766 return E_OUTOFMEMORY;
5768 ret->IUriBuilder_iface.lpVtbl = &UriBuilderVtbl;
5769 ret->ref = 1;
5771 if(pIUri) {
5772 Uri *uri;
5774 if((uri = get_uri_obj(pIUri))) {
5775 IUri_AddRef(pIUri);
5776 ret->uri = uri;
5778 if(uri->has_port)
5779 /* Windows doesn't set 'has_port' to TRUE in this case. */
5780 ret->port = uri->port;
5782 } else {
5783 heap_free(ret);
5784 *ppIUriBuilder = NULL;
5785 FIXME("(%p %x %x %p): Unknown IUri types not supported yet.\n", pIUri, dwFlags,
5786 (DWORD)dwReserved, ppIUriBuilder);
5787 return E_NOTIMPL;
5791 *ppIUriBuilder = &ret->IUriBuilder_iface;
5792 return S_OK;
5795 /* Merges the base path with the relative path and stores the resulting path
5796 * and path len in 'result' and 'result_len'.
5798 static HRESULT merge_paths(parse_data *data, const WCHAR *base, DWORD base_len, const WCHAR *relative,
5799 DWORD relative_len, WCHAR **result, DWORD *result_len, DWORD flags)
5801 const WCHAR *end = NULL;
5802 DWORD base_copy_len = 0;
5803 WCHAR *ptr;
5805 if(base_len) {
5806 /* Find the characters the will be copied over from
5807 * the base path.
5809 end = memrchrW(base, '/', base_len);
5810 if(!end && data->scheme_type == URL_SCHEME_FILE)
5811 /* Try looking for a '\\'. */
5812 end = memrchrW(base, '\\', base_len);
5815 if(end) {
5816 base_copy_len = (end+1)-base;
5817 *result = heap_alloc((base_copy_len+relative_len+1)*sizeof(WCHAR));
5818 } else
5819 *result = heap_alloc((relative_len+1)*sizeof(WCHAR));
5821 if(!(*result)) {
5822 *result_len = 0;
5823 return E_OUTOFMEMORY;
5826 ptr = *result;
5827 if(end) {
5828 memcpy(ptr, base, base_copy_len*sizeof(WCHAR));
5829 ptr += base_copy_len;
5832 memcpy(ptr, relative, relative_len*sizeof(WCHAR));
5833 ptr += relative_len;
5834 *ptr = '\0';
5836 *result_len = (ptr-*result);
5837 return S_OK;
5840 static HRESULT combine_uri(Uri *base, Uri *relative, DWORD flags, IUri **result, DWORD extras) {
5841 Uri *ret;
5842 HRESULT hr;
5843 parse_data data;
5844 DWORD create_flags = 0, len = 0;
5846 memset(&data, 0, sizeof(parse_data));
5848 /* Base case is when the relative Uri has a scheme name,
5849 * if it does, then 'result' will contain the same data
5850 * as the relative Uri.
5852 if(relative->scheme_start > -1) {
5853 data.uri = SysAllocString(relative->raw_uri);
5854 if(!data.uri) {
5855 *result = NULL;
5856 return E_OUTOFMEMORY;
5859 parse_uri(&data, 0);
5861 ret = create_uri_obj();
5862 if(!ret) {
5863 *result = NULL;
5864 return E_OUTOFMEMORY;
5867 if(extras & COMBINE_URI_FORCE_FLAG_USE) {
5868 if(flags & URL_DONT_SIMPLIFY)
5869 create_flags |= Uri_CREATE_NO_CANONICALIZE;
5870 if(flags & URL_DONT_UNESCAPE_EXTRA_INFO)
5871 create_flags |= Uri_CREATE_NO_DECODE_EXTRA_INFO;
5874 ret->raw_uri = data.uri;
5875 hr = canonicalize_uri(&data, ret, create_flags);
5876 if(FAILED(hr)) {
5877 IUri_Release(&ret->IUri_iface);
5878 *result = NULL;
5879 return hr;
5882 apply_default_flags(&create_flags);
5883 ret->create_flags = create_flags;
5885 *result = &ret->IUri_iface;
5886 } else {
5887 WCHAR *path = NULL;
5888 DWORD raw_flags = 0;
5890 if(base->scheme_start > -1) {
5891 data.scheme = base->canon_uri+base->scheme_start;
5892 data.scheme_len = base->scheme_len;
5893 data.scheme_type = base->scheme_type;
5894 } else {
5895 data.is_relative = TRUE;
5896 data.scheme_type = URL_SCHEME_UNKNOWN;
5897 create_flags |= Uri_CREATE_ALLOW_RELATIVE;
5900 if(base->authority_start > -1) {
5901 if(base->userinfo_start > -1 && base->userinfo_split != 0) {
5902 data.username = base->canon_uri+base->userinfo_start;
5903 data.username_len = (base->userinfo_split > -1) ? base->userinfo_split : base->userinfo_len;
5906 if(base->userinfo_split > -1) {
5907 data.password = base->canon_uri+base->userinfo_start+base->userinfo_split+1;
5908 data.password_len = base->userinfo_len-base->userinfo_split-1;
5911 if(base->host_start > -1) {
5912 data.host = base->canon_uri+base->host_start;
5913 data.host_len = base->host_len;
5914 data.host_type = base->host_type;
5917 if(base->has_port) {
5918 data.has_port = TRUE;
5919 data.port_value = base->port;
5921 } else if(base->scheme_type != URL_SCHEME_FILE)
5922 data.is_opaque = TRUE;
5924 if(relative->path_start == -1 || !relative->path_len) {
5925 if(base->path_start > -1) {
5926 data.path = base->canon_uri+base->path_start;
5927 data.path_len = base->path_len;
5928 } else if((base->path_start == -1 || !base->path_len) && !data.is_opaque) {
5929 /* Just set the path as a '/' if the base didn't have
5930 * one and if it's an hierarchical URI.
5932 static const WCHAR slashW[] = {'/',0};
5933 data.path = slashW;
5934 data.path_len = 1;
5937 if(relative->query_start > -1) {
5938 data.query = relative->canon_uri+relative->query_start;
5939 data.query_len = relative->query_len;
5940 } else if(base->query_start > -1) {
5941 data.query = base->canon_uri+base->query_start;
5942 data.query_len = base->query_len;
5944 } else {
5945 const WCHAR *ptr, **pptr;
5946 DWORD path_offset = 0, path_len = 0;
5948 /* There's two possibilities on what will happen to the path component
5949 * of the result IUri. First, if the relative path begins with a '/'
5950 * then the resulting path will just be the relative path. Second, if
5951 * relative path doesn't begin with a '/' then the base path and relative
5952 * path are merged together.
5954 if(relative->path_len && *(relative->canon_uri+relative->path_start) == '/') {
5955 WCHAR *tmp = NULL;
5956 BOOL copy_drive_path = FALSE;
5958 /* If the relative IUri's path starts with a '/', then we
5959 * don't use the base IUri's path. Unless the base IUri
5960 * is a file URI, in which case it uses the drive path of
5961 * the base IUri (if it has any) in the new path.
5963 if(base->scheme_type == URL_SCHEME_FILE) {
5964 if(base->path_len > 3 && *(base->canon_uri+base->path_start) == '/' &&
5965 is_drive_path(base->canon_uri+base->path_start+1)) {
5966 path_len += 3;
5967 copy_drive_path = TRUE;
5971 path_len += relative->path_len;
5973 path = heap_alloc((path_len+1)*sizeof(WCHAR));
5974 if(!path) {
5975 *result = NULL;
5976 return E_OUTOFMEMORY;
5979 tmp = path;
5981 /* Copy the base paths, drive path over. */
5982 if(copy_drive_path) {
5983 memcpy(tmp, base->canon_uri+base->path_start, 3*sizeof(WCHAR));
5984 tmp += 3;
5987 memcpy(tmp, relative->canon_uri+relative->path_start, relative->path_len*sizeof(WCHAR));
5988 path[path_len] = '\0';
5989 } else {
5990 /* Merge the base path with the relative path. */
5991 hr = merge_paths(&data, base->canon_uri+base->path_start, base->path_len,
5992 relative->canon_uri+relative->path_start, relative->path_len,
5993 &path, &path_len, flags);
5994 if(FAILED(hr)) {
5995 *result = NULL;
5996 return hr;
5999 /* If the resulting IUri is a file URI, the drive path isn't
6000 * reduced out when the dot segments are removed.
6002 if(path_len >= 3 && data.scheme_type == URL_SCHEME_FILE && !data.host) {
6003 if(*path == '/' && is_drive_path(path+1))
6004 path_offset = 2;
6005 else if(is_drive_path(path))
6006 path_offset = 1;
6010 /* Check if the dot segments need to be removed from the path. */
6011 if(!(flags & URL_DONT_SIMPLIFY) && !data.is_opaque) {
6012 DWORD offset = (path_offset > 0) ? path_offset+1 : 0;
6013 DWORD new_len = remove_dot_segments(path+offset,path_len-offset);
6015 if(new_len != path_len) {
6016 WCHAR *tmp = heap_realloc(path, (offset+new_len+1)*sizeof(WCHAR));
6017 if(!tmp) {
6018 heap_free(path);
6019 *result = NULL;
6020 return E_OUTOFMEMORY;
6023 tmp[new_len+offset] = '\0';
6024 path = tmp;
6025 path_len = new_len+offset;
6029 if(relative->query_start > -1) {
6030 data.query = relative->canon_uri+relative->query_start;
6031 data.query_len = relative->query_len;
6034 /* Make sure the path component is valid. */
6035 ptr = path;
6036 pptr = &ptr;
6037 if((data.is_opaque && !parse_path_opaque(pptr, &data, 0)) ||
6038 (!data.is_opaque && !parse_path_hierarchical(pptr, &data, 0))) {
6039 heap_free(path);
6040 *result = NULL;
6041 return E_INVALIDARG;
6045 if(relative->fragment_start > -1) {
6046 data.fragment = relative->canon_uri+relative->fragment_start;
6047 data.fragment_len = relative->fragment_len;
6050 if(flags & URL_DONT_SIMPLIFY)
6051 raw_flags |= RAW_URI_FORCE_PORT_DISP;
6052 if(flags & URL_FILE_USE_PATHURL)
6053 raw_flags |= RAW_URI_CONVERT_TO_DOS_PATH;
6055 len = generate_raw_uri(&data, data.uri, raw_flags);
6056 data.uri = SysAllocStringLen(NULL, len);
6057 if(!data.uri) {
6058 heap_free(path);
6059 *result = NULL;
6060 return E_OUTOFMEMORY;
6063 generate_raw_uri(&data, data.uri, raw_flags);
6065 ret = create_uri_obj();
6066 if(!ret) {
6067 SysFreeString(data.uri);
6068 heap_free(path);
6069 *result = NULL;
6070 return E_OUTOFMEMORY;
6073 if(flags & URL_DONT_SIMPLIFY)
6074 create_flags |= Uri_CREATE_NO_CANONICALIZE;
6075 if(flags & URL_FILE_USE_PATHURL)
6076 create_flags |= Uri_CREATE_FILE_USE_DOS_PATH;
6078 ret->raw_uri = data.uri;
6079 hr = canonicalize_uri(&data, ret, create_flags);
6080 if(FAILED(hr)) {
6081 IUri_Release(&ret->IUri_iface);
6082 *result = NULL;
6083 return hr;
6086 if(flags & URL_DONT_SIMPLIFY)
6087 ret->display_modifiers |= URI_DISPLAY_NO_DEFAULT_PORT_AUTH;
6089 apply_default_flags(&create_flags);
6090 ret->create_flags = create_flags;
6091 *result = &ret->IUri_iface;
6093 heap_free(path);
6096 return S_OK;
6099 /***********************************************************************
6100 * CoInternetCombineIUri (urlmon.@)
6102 HRESULT WINAPI CoInternetCombineIUri(IUri *pBaseUri, IUri *pRelativeUri, DWORD dwCombineFlags,
6103 IUri **ppCombinedUri, DWORD_PTR dwReserved)
6105 HRESULT hr;
6106 IInternetProtocolInfo *info;
6107 Uri *relative, *base;
6108 TRACE("(%p %p %x %p %x)\n", pBaseUri, pRelativeUri, dwCombineFlags, ppCombinedUri, (DWORD)dwReserved);
6110 if(!ppCombinedUri)
6111 return E_INVALIDARG;
6113 if(!pBaseUri || !pRelativeUri) {
6114 *ppCombinedUri = NULL;
6115 return E_INVALIDARG;
6118 relative = get_uri_obj(pRelativeUri);
6119 base = get_uri_obj(pBaseUri);
6120 if(!relative || !base) {
6121 *ppCombinedUri = NULL;
6122 FIXME("(%p %p %x %p %x) Unknown IUri types not supported yet.\n",
6123 pBaseUri, pRelativeUri, dwCombineFlags, ppCombinedUri, (DWORD)dwReserved);
6124 return E_NOTIMPL;
6127 info = get_protocol_info(base->canon_uri);
6128 if(info) {
6129 WCHAR result[INTERNET_MAX_URL_LENGTH+1];
6130 DWORD result_len = 0;
6132 hr = IInternetProtocolInfo_CombineUrl(info, base->canon_uri, relative->canon_uri, dwCombineFlags,
6133 result, INTERNET_MAX_URL_LENGTH+1, &result_len, 0);
6134 IInternetProtocolInfo_Release(info);
6135 if(SUCCEEDED(hr)) {
6136 hr = CreateUri(result, Uri_CREATE_ALLOW_RELATIVE, 0, ppCombinedUri);
6137 if(SUCCEEDED(hr))
6138 return hr;
6142 return combine_uri(base, relative, dwCombineFlags, ppCombinedUri, 0);
6145 /***********************************************************************
6146 * CoInternetCombineUrlEx (urlmon.@)
6148 HRESULT WINAPI CoInternetCombineUrlEx(IUri *pBaseUri, LPCWSTR pwzRelativeUrl, DWORD dwCombineFlags,
6149 IUri **ppCombinedUri, DWORD_PTR dwReserved)
6151 IUri *relative;
6152 Uri *base;
6153 HRESULT hr;
6154 IInternetProtocolInfo *info;
6156 TRACE("(%p %s %x %p %x) stub\n", pBaseUri, debugstr_w(pwzRelativeUrl), dwCombineFlags,
6157 ppCombinedUri, (DWORD)dwReserved);
6159 if(!ppCombinedUri)
6160 return E_POINTER;
6162 if(!pwzRelativeUrl) {
6163 *ppCombinedUri = NULL;
6164 return E_UNEXPECTED;
6167 if(!pBaseUri) {
6168 *ppCombinedUri = NULL;
6169 return E_INVALIDARG;
6172 base = get_uri_obj(pBaseUri);
6173 if(!base) {
6174 *ppCombinedUri = NULL;
6175 FIXME("(%p %s %x %p %x) Unknown IUri's not supported yet.\n", pBaseUri, debugstr_w(pwzRelativeUrl),
6176 dwCombineFlags, ppCombinedUri, (DWORD)dwReserved);
6177 return E_NOTIMPL;
6180 info = get_protocol_info(base->canon_uri);
6181 if(info) {
6182 WCHAR result[INTERNET_MAX_URL_LENGTH+1];
6183 DWORD result_len = 0;
6185 hr = IInternetProtocolInfo_CombineUrl(info, base->canon_uri, pwzRelativeUrl, dwCombineFlags,
6186 result, INTERNET_MAX_URL_LENGTH+1, &result_len, 0);
6187 IInternetProtocolInfo_Release(info);
6188 if(SUCCEEDED(hr)) {
6189 hr = CreateUri(result, Uri_CREATE_ALLOW_RELATIVE, 0, ppCombinedUri);
6190 if(SUCCEEDED(hr))
6191 return hr;
6195 hr = CreateUri(pwzRelativeUrl, Uri_CREATE_ALLOW_RELATIVE, 0, &relative);
6196 if(FAILED(hr)) {
6197 *ppCombinedUri = NULL;
6198 return hr;
6201 hr = combine_uri(base, get_uri_obj(relative), dwCombineFlags, ppCombinedUri, COMBINE_URI_FORCE_FLAG_USE);
6203 IUri_Release(relative);
6204 return hr;
6207 static HRESULT parse_canonicalize(const Uri *uri, DWORD flags, LPWSTR output,
6208 DWORD output_len, DWORD *result_len)
6210 const WCHAR *ptr = NULL;
6211 WCHAR *path = NULL;
6212 const WCHAR **pptr;
6213 WCHAR buffer[INTERNET_MAX_URL_LENGTH+1];
6214 DWORD len = 0;
6215 BOOL reduce_path;
6217 /* URL_UNESCAPE only has effect if none of the URL_ESCAPE flags are set. */
6218 const BOOL allow_unescape = !(flags & URL_ESCAPE_UNSAFE) &&
6219 !(flags & URL_ESCAPE_SPACES_ONLY) &&
6220 !(flags & URL_ESCAPE_PERCENT);
6223 /* Check if the dot segments need to be removed from the
6224 * path component.
6226 if(uri->scheme_start > -1 && uri->path_start > -1) {
6227 ptr = uri->canon_uri+uri->scheme_start+uri->scheme_len+1;
6228 pptr = &ptr;
6230 reduce_path = !(flags & URL_NO_META) &&
6231 !(flags & URL_DONT_SIMPLIFY) &&
6232 ptr && check_hierarchical(pptr);
6234 for(ptr = uri->canon_uri; ptr < uri->canon_uri+uri->canon_len; ++ptr) {
6235 BOOL do_default_action = TRUE;
6237 /* Keep track of the path if we need to remove dot segments from
6238 * it later.
6240 if(reduce_path && !path && ptr == uri->canon_uri+uri->path_start)
6241 path = buffer+len;
6243 /* Check if it's time to reduce the path. */
6244 if(reduce_path && ptr == uri->canon_uri+uri->path_start+uri->path_len) {
6245 DWORD current_path_len = (buffer+len) - path;
6246 DWORD new_path_len = remove_dot_segments(path, current_path_len);
6248 /* Update the current length. */
6249 len -= (current_path_len-new_path_len);
6250 reduce_path = FALSE;
6253 if(*ptr == '%') {
6254 const WCHAR decoded = decode_pct_val(ptr);
6255 if(decoded) {
6256 if(allow_unescape && (flags & URL_UNESCAPE)) {
6257 buffer[len++] = decoded;
6258 ptr += 2;
6259 do_default_action = FALSE;
6263 /* See if %'s needed to encoded. */
6264 if(do_default_action && (flags & URL_ESCAPE_PERCENT)) {
6265 pct_encode_val(*ptr, buffer+len);
6266 len += 3;
6267 do_default_action = FALSE;
6269 } else if(*ptr == ' ') {
6270 if((flags & URL_ESCAPE_SPACES_ONLY) &&
6271 !(flags & URL_ESCAPE_UNSAFE)) {
6272 pct_encode_val(*ptr, buffer+len);
6273 len += 3;
6274 do_default_action = FALSE;
6276 } else if(!is_reserved(*ptr) && !is_unreserved(*ptr)) {
6277 if(flags & URL_ESCAPE_UNSAFE) {
6278 pct_encode_val(*ptr, buffer+len);
6279 len += 3;
6280 do_default_action = FALSE;
6284 if(do_default_action)
6285 buffer[len++] = *ptr;
6288 /* Sometimes the path is the very last component of the IUri, so
6289 * see if the dot segments need to be reduced now.
6291 if(reduce_path && path) {
6292 DWORD current_path_len = (buffer+len) - path;
6293 DWORD new_path_len = remove_dot_segments(path, current_path_len);
6295 /* Update the current length. */
6296 len -= (current_path_len-new_path_len);
6299 buffer[len++] = 0;
6301 /* The null terminator isn't included the length. */
6302 *result_len = len-1;
6303 if(len > output_len)
6304 return STRSAFE_E_INSUFFICIENT_BUFFER;
6305 else
6306 memcpy(output, buffer, len*sizeof(WCHAR));
6308 return S_OK;
6311 static HRESULT parse_friendly(IUri *uri, LPWSTR output, DWORD output_len,
6312 DWORD *result_len)
6314 HRESULT hr;
6315 DWORD display_len;
6316 BSTR display;
6318 hr = IUri_GetPropertyLength(uri, Uri_PROPERTY_DISPLAY_URI, &display_len, 0);
6319 if(FAILED(hr)) {
6320 *result_len = 0;
6321 return hr;
6324 *result_len = display_len;
6325 if(display_len+1 > output_len)
6326 return STRSAFE_E_INSUFFICIENT_BUFFER;
6328 hr = IUri_GetDisplayUri(uri, &display);
6329 if(FAILED(hr)) {
6330 *result_len = 0;
6331 return hr;
6334 memcpy(output, display, (display_len+1)*sizeof(WCHAR));
6335 SysFreeString(display);
6336 return S_OK;
6339 static HRESULT parse_rootdocument(const Uri *uri, LPWSTR output, DWORD output_len,
6340 DWORD *result_len)
6342 static const WCHAR colon_slashesW[] = {':','/','/'};
6344 WCHAR *ptr;
6345 DWORD len = 0;
6347 /* Windows only returns the root document if the URI has an authority
6348 * and it's not an unknown scheme type or a file scheme type.
6350 if(uri->authority_start == -1 ||
6351 uri->scheme_type == URL_SCHEME_UNKNOWN ||
6352 uri->scheme_type == URL_SCHEME_FILE) {
6353 *result_len = 0;
6354 if(!output_len)
6355 return STRSAFE_E_INSUFFICIENT_BUFFER;
6357 output[0] = 0;
6358 return S_OK;
6361 len = uri->scheme_len+uri->authority_len;
6362 /* For the "://" and '/' which will be added. */
6363 len += 4;
6365 if(len+1 > output_len) {
6366 *result_len = len;
6367 return STRSAFE_E_INSUFFICIENT_BUFFER;
6370 ptr = output;
6371 memcpy(ptr, uri->canon_uri+uri->scheme_start, uri->scheme_len*sizeof(WCHAR));
6373 /* Add the "://". */
6374 ptr += uri->scheme_len;
6375 memcpy(ptr, colon_slashesW, sizeof(colon_slashesW));
6377 /* Add the authority. */
6378 ptr += sizeof(colon_slashesW)/sizeof(WCHAR);
6379 memcpy(ptr, uri->canon_uri+uri->authority_start, uri->authority_len*sizeof(WCHAR));
6381 /* Add the '/' after the authority. */
6382 ptr += uri->authority_len;
6383 *ptr = '/';
6384 ptr[1] = 0;
6386 *result_len = len;
6387 return S_OK;
6390 static HRESULT parse_document(const Uri *uri, LPWSTR output, DWORD output_len,
6391 DWORD *result_len)
6393 DWORD len = 0;
6395 /* It has to be a known scheme type, but, it can't be a file
6396 * scheme. It also has to hierarchical.
6398 if(uri->scheme_type == URL_SCHEME_UNKNOWN ||
6399 uri->scheme_type == URL_SCHEME_FILE ||
6400 uri->authority_start == -1) {
6401 *result_len = 0;
6402 if(output_len < 1)
6403 return STRSAFE_E_INSUFFICIENT_BUFFER;
6405 output[0] = 0;
6406 return S_OK;
6409 if(uri->fragment_start > -1)
6410 len = uri->fragment_start;
6411 else
6412 len = uri->canon_len;
6414 *result_len = len;
6415 if(len+1 > output_len)
6416 return STRSAFE_E_INSUFFICIENT_BUFFER;
6418 memcpy(output, uri->canon_uri, len*sizeof(WCHAR));
6419 output[len] = 0;
6420 return S_OK;
6423 static HRESULT parse_path_from_url(const Uri *uri, LPWSTR output, DWORD output_len,
6424 DWORD *result_len)
6426 const WCHAR *path_ptr;
6427 WCHAR buffer[INTERNET_MAX_URL_LENGTH+1];
6428 WCHAR *ptr;
6430 if(uri->scheme_type != URL_SCHEME_FILE) {
6431 *result_len = 0;
6432 if(output_len > 0)
6433 output[0] = 0;
6434 return E_INVALIDARG;
6437 ptr = buffer;
6438 if(uri->host_start > -1) {
6439 static const WCHAR slash_slashW[] = {'\\','\\'};
6441 memcpy(ptr, slash_slashW, sizeof(slash_slashW));
6442 ptr += sizeof(slash_slashW)/sizeof(WCHAR);
6443 memcpy(ptr, uri->canon_uri+uri->host_start, uri->host_len*sizeof(WCHAR));
6444 ptr += uri->host_len;
6447 path_ptr = uri->canon_uri+uri->path_start;
6448 if(uri->path_len > 3 && *path_ptr == '/' && is_drive_path(path_ptr+1))
6449 /* Skip past the '/' in front of the drive path. */
6450 ++path_ptr;
6452 for(; path_ptr < uri->canon_uri+uri->path_start+uri->path_len; ++path_ptr, ++ptr) {
6453 BOOL do_default_action = TRUE;
6455 if(*path_ptr == '%') {
6456 const WCHAR decoded = decode_pct_val(path_ptr);
6457 if(decoded) {
6458 *ptr = decoded;
6459 path_ptr += 2;
6460 do_default_action = FALSE;
6462 } else if(*path_ptr == '/') {
6463 *ptr = '\\';
6464 do_default_action = FALSE;
6467 if(do_default_action)
6468 *ptr = *path_ptr;
6471 *ptr = 0;
6473 *result_len = ptr-buffer;
6474 if(*result_len+1 > output_len)
6475 return STRSAFE_E_INSUFFICIENT_BUFFER;
6477 memcpy(output, buffer, (*result_len+1)*sizeof(WCHAR));
6478 return S_OK;
6481 static HRESULT parse_url_from_path(IUri *uri, LPWSTR output, DWORD output_len,
6482 DWORD *result_len)
6484 HRESULT hr;
6485 BSTR received;
6486 DWORD len = 0;
6488 hr = IUri_GetPropertyLength(uri, Uri_PROPERTY_ABSOLUTE_URI, &len, 0);
6489 if(FAILED(hr)) {
6490 *result_len = 0;
6491 return hr;
6494 *result_len = len;
6495 if(len+1 > output_len)
6496 return STRSAFE_E_INSUFFICIENT_BUFFER;
6498 hr = IUri_GetAbsoluteUri(uri, &received);
6499 if(FAILED(hr)) {
6500 *result_len = 0;
6501 return hr;
6504 memcpy(output, received, (len+1)*sizeof(WCHAR));
6505 SysFreeString(received);
6507 return S_OK;
6510 static HRESULT parse_schema(IUri *uri, LPWSTR output, DWORD output_len,
6511 DWORD *result_len)
6513 HRESULT hr;
6514 DWORD len;
6515 BSTR received;
6517 hr = IUri_GetPropertyLength(uri, Uri_PROPERTY_SCHEME_NAME, &len, 0);
6518 if(FAILED(hr)) {
6519 *result_len = 0;
6520 return hr;
6523 *result_len = len;
6524 if(len+1 > output_len)
6525 return STRSAFE_E_INSUFFICIENT_BUFFER;
6527 hr = IUri_GetSchemeName(uri, &received);
6528 if(FAILED(hr)) {
6529 *result_len = 0;
6530 return hr;
6533 memcpy(output, received, (len+1)*sizeof(WCHAR));
6534 SysFreeString(received);
6536 return S_OK;
6539 static HRESULT parse_site(IUri *uri, LPWSTR output, DWORD output_len, DWORD *result_len)
6541 HRESULT hr;
6542 DWORD len;
6543 BSTR received;
6545 hr = IUri_GetPropertyLength(uri, Uri_PROPERTY_HOST, &len, 0);
6546 if(FAILED(hr)) {
6547 *result_len = 0;
6548 return hr;
6551 *result_len = len;
6552 if(len+1 > output_len)
6553 return STRSAFE_E_INSUFFICIENT_BUFFER;
6555 hr = IUri_GetHost(uri, &received);
6556 if(FAILED(hr)) {
6557 *result_len = 0;
6558 return hr;
6561 memcpy(output, received, (len+1)*sizeof(WCHAR));
6562 SysFreeString(received);
6564 return S_OK;
6567 static HRESULT parse_domain(IUri *uri, LPWSTR output, DWORD output_len, DWORD *result_len)
6569 HRESULT hr;
6570 DWORD len;
6571 BSTR received;
6573 hr = IUri_GetPropertyLength(uri, Uri_PROPERTY_DOMAIN, &len, 0);
6574 if(FAILED(hr)) {
6575 *result_len = 0;
6576 return hr;
6579 *result_len = len;
6580 if(len+1 > output_len)
6581 return STRSAFE_E_INSUFFICIENT_BUFFER;
6583 hr = IUri_GetDomain(uri, &received);
6584 if(FAILED(hr)) {
6585 *result_len = 0;
6586 return hr;
6589 memcpy(output, received, (len+1)*sizeof(WCHAR));
6590 SysFreeString(received);
6592 return S_OK;
6595 static HRESULT parse_anchor(IUri *uri, LPWSTR output, DWORD output_len, DWORD *result_len)
6597 HRESULT hr;
6598 DWORD len;
6599 BSTR received;
6601 hr = IUri_GetPropertyLength(uri, Uri_PROPERTY_FRAGMENT, &len, 0);
6602 if(FAILED(hr)) {
6603 *result_len = 0;
6604 return hr;
6607 *result_len = len;
6608 if(len+1 > output_len)
6609 return STRSAFE_E_INSUFFICIENT_BUFFER;
6611 hr = IUri_GetFragment(uri, &received);
6612 if(FAILED(hr)) {
6613 *result_len = 0;
6614 return hr;
6617 memcpy(output, received, (len+1)*sizeof(WCHAR));
6618 SysFreeString(received);
6620 return S_OK;
6623 /***********************************************************************
6624 * CoInternetParseIUri (urlmon.@)
6626 HRESULT WINAPI CoInternetParseIUri(IUri *pIUri, PARSEACTION ParseAction, DWORD dwFlags,
6627 LPWSTR pwzResult, DWORD cchResult, DWORD *pcchResult,
6628 DWORD_PTR dwReserved)
6630 HRESULT hr;
6631 Uri *uri;
6632 IInternetProtocolInfo *info;
6634 TRACE("(%p %d %x %p %d %p %x)\n", pIUri, ParseAction, dwFlags, pwzResult,
6635 cchResult, pcchResult, (DWORD)dwReserved);
6637 if(!pcchResult)
6638 return E_POINTER;
6640 if(!pwzResult || !pIUri) {
6641 *pcchResult = 0;
6642 return E_INVALIDARG;
6645 if(!(uri = get_uri_obj(pIUri))) {
6646 *pcchResult = 0;
6647 FIXME("(%p %d %x %p %d %p %x) Unknown IUri's not supported for this action.\n",
6648 pIUri, ParseAction, dwFlags, pwzResult, cchResult, pcchResult, (DWORD)dwReserved);
6649 return E_NOTIMPL;
6652 info = get_protocol_info(uri->canon_uri);
6653 if(info) {
6654 hr = IInternetProtocolInfo_ParseUrl(info, uri->canon_uri, ParseAction, dwFlags,
6655 pwzResult, cchResult, pcchResult, 0);
6656 IInternetProtocolInfo_Release(info);
6657 if(SUCCEEDED(hr)) return hr;
6660 switch(ParseAction) {
6661 case PARSE_CANONICALIZE:
6662 hr = parse_canonicalize(uri, dwFlags, pwzResult, cchResult, pcchResult);
6663 break;
6664 case PARSE_FRIENDLY:
6665 hr = parse_friendly(pIUri, pwzResult, cchResult, pcchResult);
6666 break;
6667 case PARSE_ROOTDOCUMENT:
6668 hr = parse_rootdocument(uri, pwzResult, cchResult, pcchResult);
6669 break;
6670 case PARSE_DOCUMENT:
6671 hr = parse_document(uri, pwzResult, cchResult, pcchResult);
6672 break;
6673 case PARSE_PATH_FROM_URL:
6674 hr = parse_path_from_url(uri, pwzResult, cchResult, pcchResult);
6675 break;
6676 case PARSE_URL_FROM_PATH:
6677 hr = parse_url_from_path(pIUri, pwzResult, cchResult, pcchResult);
6678 break;
6679 case PARSE_SCHEMA:
6680 hr = parse_schema(pIUri, pwzResult, cchResult, pcchResult);
6681 break;
6682 case PARSE_SITE:
6683 hr = parse_site(pIUri, pwzResult, cchResult, pcchResult);
6684 break;
6685 case PARSE_DOMAIN:
6686 hr = parse_domain(pIUri, pwzResult, cchResult, pcchResult);
6687 break;
6688 case PARSE_LOCATION:
6689 case PARSE_ANCHOR:
6690 hr = parse_anchor(pIUri, pwzResult, cchResult, pcchResult);
6691 break;
6692 case PARSE_SECURITY_URL:
6693 case PARSE_MIME:
6694 case PARSE_SERVER:
6695 case PARSE_SECURITY_DOMAIN:
6696 *pcchResult = 0;
6697 hr = E_FAIL;
6698 break;
6699 default:
6700 *pcchResult = 0;
6701 hr = E_NOTIMPL;
6702 FIXME("(%p %d %x %p %d %p %x) Partial stub.\n", pIUri, ParseAction, dwFlags,
6703 pwzResult, cchResult, pcchResult, (DWORD)dwReserved);
6706 return hr;