urlmon: Implemented support for determining what port value to use in IUriBuilder.
[wine/multimedia.git] / dlls / urlmon / uri.c
blob44e37b1e989030dc657cb8e7f56d346f306fc78d
1 /*
2 * Copyright 2010 Jacek Caban for CodeWeavers
3 * Copyright 2010 Thomas Mullaly
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #include "urlmon_main.h"
21 #include "wine/debug.h"
23 #define NO_SHLWAPI_REG
24 #include "shlwapi.h"
26 #define UINT_MAX 0xffffffff
27 #define USHORT_MAX 0xffff
29 #define ALLOW_NULL_TERM_SCHEME 0x01
30 #define ALLOW_NULL_TERM_USER_NAME 0x02
31 #define ALLOW_NULL_TERM_PASSWORD 0x04
32 #define ALLOW_BRACKETLESS_IP_LITERAL 0x08
33 #define SKIP_IP_FUTURE_CHECK 0x10
34 #define IGNORE_PORT_DELIMITER 0x20
36 WINE_DEFAULT_DEBUG_CHANNEL(urlmon);
38 static const IID IID_IUriObj = {0x4b364760,0x9f51,0x11df,{0x98,0x1c,0x08,0x00,0x20,0x0c,0x9a,0x66}};
40 typedef struct {
41 const IUriVtbl *lpIUriVtbl;
42 LONG ref;
44 BSTR raw_uri;
46 /* Information about the canonicalized URI's buffer. */
47 WCHAR *canon_uri;
48 DWORD canon_size;
49 DWORD canon_len;
50 BOOL display_absolute;
51 DWORD create_flags;
53 INT scheme_start;
54 DWORD scheme_len;
55 URL_SCHEME scheme_type;
57 INT userinfo_start;
58 DWORD userinfo_len;
59 INT userinfo_split;
61 INT host_start;
62 DWORD host_len;
63 Uri_HOST_TYPE host_type;
65 USHORT port;
66 BOOL has_port;
68 INT authority_start;
69 DWORD authority_len;
71 INT domain_offset;
73 INT path_start;
74 DWORD path_len;
75 INT extension_offset;
77 INT query_start;
78 DWORD query_len;
80 INT fragment_start;
81 DWORD fragment_len;
82 } Uri;
84 typedef struct {
85 const IUriBuilderVtbl *lpIUriBuilderVtbl;
86 LONG ref;
88 Uri *uri;
89 DWORD modified_props;
91 WCHAR *fragment;
92 DWORD fragment_len;
94 WCHAR *host;
95 DWORD host_len;
97 WCHAR *password;
98 DWORD password_len;
100 WCHAR *path;
101 DWORD path_len;
103 BOOL has_port;
104 DWORD port;
106 WCHAR *query;
107 DWORD query_len;
109 WCHAR *scheme;
110 DWORD scheme_len;
112 WCHAR *username;
113 DWORD username_len;
114 } UriBuilder;
116 typedef struct {
117 const WCHAR *str;
118 DWORD len;
119 } h16;
121 typedef struct {
122 /* IPv6 addresses can hold up to 8 h16 components. */
123 h16 components[8];
124 DWORD h16_count;
126 /* An IPv6 can have 1 elision ("::"). */
127 const WCHAR *elision;
129 /* An IPv6 can contain 1 IPv4 address as the last 32bits of the address. */
130 const WCHAR *ipv4;
131 DWORD ipv4_len;
133 INT components_size;
134 INT elision_size;
135 } ipv6_address;
137 typedef struct {
138 BSTR uri;
140 BOOL is_relative;
141 BOOL is_opaque;
142 BOOL has_implicit_scheme;
143 BOOL has_implicit_ip;
144 UINT implicit_ipv4;
146 const WCHAR *scheme;
147 DWORD scheme_len;
148 URL_SCHEME scheme_type;
150 const WCHAR *username;
151 DWORD username_len;
153 const WCHAR *password;
154 DWORD password_len;
156 const WCHAR *host;
157 DWORD host_len;
158 Uri_HOST_TYPE host_type;
160 BOOL has_ipv6;
161 ipv6_address ipv6_address;
163 BOOL has_port;
164 const WCHAR *port;
165 DWORD port_len;
166 DWORD port_value;
168 const WCHAR *path;
169 DWORD path_len;
171 const WCHAR *query;
172 DWORD query_len;
174 const WCHAR *fragment;
175 DWORD fragment_len;
176 } parse_data;
178 static const CHAR hexDigits[] = "0123456789ABCDEF";
180 /* List of scheme types/scheme names that are recognized by the IUri interface as of IE 7. */
181 static const struct {
182 URL_SCHEME scheme;
183 WCHAR scheme_name[16];
184 } recognized_schemes[] = {
185 {URL_SCHEME_FTP, {'f','t','p',0}},
186 {URL_SCHEME_HTTP, {'h','t','t','p',0}},
187 {URL_SCHEME_GOPHER, {'g','o','p','h','e','r',0}},
188 {URL_SCHEME_MAILTO, {'m','a','i','l','t','o',0}},
189 {URL_SCHEME_NEWS, {'n','e','w','s',0}},
190 {URL_SCHEME_NNTP, {'n','n','t','p',0}},
191 {URL_SCHEME_TELNET, {'t','e','l','n','e','t',0}},
192 {URL_SCHEME_WAIS, {'w','a','i','s',0}},
193 {URL_SCHEME_FILE, {'f','i','l','e',0}},
194 {URL_SCHEME_MK, {'m','k',0}},
195 {URL_SCHEME_HTTPS, {'h','t','t','p','s',0}},
196 {URL_SCHEME_SHELL, {'s','h','e','l','l',0}},
197 {URL_SCHEME_SNEWS, {'s','n','e','w','s',0}},
198 {URL_SCHEME_LOCAL, {'l','o','c','a','l',0}},
199 {URL_SCHEME_JAVASCRIPT, {'j','a','v','a','s','c','r','i','p','t',0}},
200 {URL_SCHEME_VBSCRIPT, {'v','b','s','c','r','i','p','t',0}},
201 {URL_SCHEME_ABOUT, {'a','b','o','u','t',0}},
202 {URL_SCHEME_RES, {'r','e','s',0}},
203 {URL_SCHEME_MSSHELLROOTED, {'m','s','-','s','h','e','l','l','-','r','o','o','t','e','d',0}},
204 {URL_SCHEME_MSSHELLIDLIST, {'m','s','-','s','h','e','l','l','-','i','d','l','i','s','t',0}},
205 {URL_SCHEME_MSHELP, {'h','c','p',0}},
206 {URL_SCHEME_WILDCARD, {'*',0}}
209 /* List of default ports Windows recognizes. */
210 static const struct {
211 URL_SCHEME scheme;
212 USHORT port;
213 } default_ports[] = {
214 {URL_SCHEME_FTP, 21},
215 {URL_SCHEME_HTTP, 80},
216 {URL_SCHEME_GOPHER, 70},
217 {URL_SCHEME_NNTP, 119},
218 {URL_SCHEME_TELNET, 23},
219 {URL_SCHEME_WAIS, 210},
220 {URL_SCHEME_HTTPS, 443},
223 /* List of 3 character top level domain names Windows seems to recognize.
224 * There might be more, but, these are the only ones I've found so far.
226 static const struct {
227 WCHAR tld_name[4];
228 } recognized_tlds[] = {
229 {{'c','o','m',0}},
230 {{'e','d','u',0}},
231 {{'g','o','v',0}},
232 {{'i','n','t',0}},
233 {{'m','i','l',0}},
234 {{'n','e','t',0}},
235 {{'o','r','g',0}}
238 static Uri *get_uri_obj(IUri *uri)
240 Uri *ret;
241 HRESULT hres;
243 hres = IUri_QueryInterface(uri, &IID_IUriObj, (void**)&ret);
244 return SUCCEEDED(hres) ? ret : NULL;
247 static inline BOOL is_alpha(WCHAR val) {
248 return ((val >= 'a' && val <= 'z') || (val >= 'A' && val <= 'Z'));
251 static inline BOOL is_num(WCHAR val) {
252 return (val >= '0' && val <= '9');
255 static inline BOOL is_drive_path(const WCHAR *str) {
256 return (is_alpha(str[0]) && (str[1] == ':' || str[1] == '|'));
259 static inline BOOL is_unc_path(const WCHAR *str) {
260 return (str[0] == '\\' && str[0] == '\\');
263 static inline BOOL is_forbidden_dos_path_char(WCHAR val) {
264 return (val == '>' || val == '<' || val == '\"');
267 /* A URI is implicitly a file path if it begins with
268 * a drive letter (eg X:) or starts with "\\" (UNC path).
270 static inline BOOL is_implicit_file_path(const WCHAR *str) {
271 return (is_unc_path(str) || (is_alpha(str[0]) && str[1] == ':'));
274 /* Checks if the URI is a hierarchical URI. A hierarchical
275 * URI is one that has "//" after the scheme.
277 static BOOL check_hierarchical(const WCHAR **ptr) {
278 const WCHAR *start = *ptr;
280 if(**ptr != '/')
281 return FALSE;
283 ++(*ptr);
284 if(**ptr != '/') {
285 *ptr = start;
286 return FALSE;
289 ++(*ptr);
290 return TRUE;
293 /* unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~" */
294 static inline BOOL is_unreserved(WCHAR val) {
295 return (is_alpha(val) || is_num(val) || val == '-' || val == '.' ||
296 val == '_' || val == '~');
299 /* sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
300 * / "*" / "+" / "," / ";" / "="
302 static inline BOOL is_subdelim(WCHAR val) {
303 return (val == '!' || val == '$' || val == '&' ||
304 val == '\'' || val == '(' || val == ')' ||
305 val == '*' || val == '+' || val == ',' ||
306 val == ';' || val == '=');
309 /* gen-delims = ":" / "/" / "?" / "#" / "[" / "]" / "@" */
310 static inline BOOL is_gendelim(WCHAR val) {
311 return (val == ':' || val == '/' || val == '?' ||
312 val == '#' || val == '[' || val == ']' ||
313 val == '@');
316 /* Characters that delimit the end of the authority
317 * section of a URI. Sometimes a '\\' is considered
318 * an authority delimeter.
320 static inline BOOL is_auth_delim(WCHAR val, BOOL acceptSlash) {
321 return (val == '#' || val == '/' || val == '?' ||
322 val == '\0' || (acceptSlash && val == '\\'));
325 /* reserved = gen-delims / sub-delims */
326 static inline BOOL is_reserved(WCHAR val) {
327 return (is_subdelim(val) || is_gendelim(val));
330 static inline BOOL is_hexdigit(WCHAR val) {
331 return ((val >= 'a' && val <= 'f') ||
332 (val >= 'A' && val <= 'F') ||
333 (val >= '0' && val <= '9'));
336 static inline BOOL is_path_delim(WCHAR val) {
337 return (!val || val == '#' || val == '?');
340 /* List of schemes types Windows seems to expect to be hierarchical. */
341 static inline BOOL is_hierarchical_scheme(URL_SCHEME type) {
342 return(type == URL_SCHEME_HTTP || type == URL_SCHEME_FTP ||
343 type == URL_SCHEME_GOPHER || type == URL_SCHEME_NNTP ||
344 type == URL_SCHEME_TELNET || type == URL_SCHEME_WAIS ||
345 type == URL_SCHEME_FILE || type == URL_SCHEME_HTTPS ||
346 type == URL_SCHEME_RES);
349 /* Checks if 'flags' contains an invalid combination of Uri_CREATE flags. */
350 static inline BOOL has_invalid_flag_combination(DWORD flags) {
351 return((flags & Uri_CREATE_DECODE_EXTRA_INFO && flags & Uri_CREATE_NO_DECODE_EXTRA_INFO) ||
352 (flags & Uri_CREATE_CANONICALIZE && flags & Uri_CREATE_NO_CANONICALIZE) ||
353 (flags & Uri_CREATE_CRACK_UNKNOWN_SCHEMES && flags & Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES) ||
354 (flags & Uri_CREATE_PRE_PROCESS_HTML_URI && flags & Uri_CREATE_NO_PRE_PROCESS_HTML_URI) ||
355 (flags & Uri_CREATE_IE_SETTINGS && flags & Uri_CREATE_NO_IE_SETTINGS));
358 /* Applies each default Uri_CREATE flags to 'flags' if it
359 * doesn't cause a flag conflict.
361 static void apply_default_flags(DWORD *flags) {
362 if(!(*flags & Uri_CREATE_NO_CANONICALIZE))
363 *flags |= Uri_CREATE_CANONICALIZE;
364 if(!(*flags & Uri_CREATE_NO_DECODE_EXTRA_INFO))
365 *flags |= Uri_CREATE_DECODE_EXTRA_INFO;
366 if(!(*flags & Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES))
367 *flags |= Uri_CREATE_CRACK_UNKNOWN_SCHEMES;
368 if(!(*flags & Uri_CREATE_NO_PRE_PROCESS_HTML_URI))
369 *flags |= Uri_CREATE_PRE_PROCESS_HTML_URI;
370 if(!(*flags & Uri_CREATE_IE_SETTINGS))
371 *flags |= Uri_CREATE_NO_IE_SETTINGS;
374 /* Determines if the URI is hierarchical using the information already parsed into
375 * data and using the current location of parsing in the URI string.
377 * Windows considers a URI hierarchical if on of the following is true:
378 * A.) It's a wildcard scheme.
379 * B.) It's an implicit file scheme.
380 * C.) It's a known hierarchical scheme and it has two '\\' after the scheme name.
381 * (the '\\' will be converted into "//" during canonicalization).
382 * D.) It's not a relative URI and "//" appears after the scheme name.
384 static inline BOOL is_hierarchical_uri(const WCHAR **ptr, const parse_data *data) {
385 const WCHAR *start = *ptr;
387 if(data->scheme_type == URL_SCHEME_WILDCARD)
388 return TRUE;
389 else if(data->scheme_type == URL_SCHEME_FILE && data->has_implicit_scheme)
390 return TRUE;
391 else if(is_hierarchical_scheme(data->scheme_type) && (*ptr)[0] == '\\' && (*ptr)[1] == '\\') {
392 *ptr += 2;
393 return TRUE;
394 } else if(!data->is_relative && check_hierarchical(ptr))
395 return TRUE;
397 *ptr = start;
398 return FALSE;
401 /* Checks if the two Uri's are logically equivalent. It's a simple
402 * comparison, since they are both of type Uri, and it can access
403 * the properties of each Uri directly without the need to go
404 * through the "IUri_Get*" interface calls.
406 static BOOL are_equal_simple(const Uri *a, const Uri *b) {
407 if(a->scheme_type == b->scheme_type) {
408 const BOOL known_scheme = a->scheme_type != URL_SCHEME_UNKNOWN;
409 const BOOL are_hierarchical =
410 (a->authority_start > -1 && b->authority_start > -1);
412 if(a->scheme_type == URL_SCHEME_FILE) {
413 if(a->canon_len == b->canon_len)
414 return !StrCmpIW(a->canon_uri, b->canon_uri);
417 /* Only compare the scheme names (if any) if their unknown scheme types. */
418 if(!known_scheme) {
419 if((a->scheme_start > -1 && b->scheme_start > -1) &&
420 (a->scheme_len == b->scheme_len)) {
421 /* Make sure the schemes are the same. */
422 if(StrCmpNW(a->canon_uri+a->scheme_start, b->canon_uri+b->scheme_start, a->scheme_len))
423 return FALSE;
424 } else if(a->scheme_len != b->scheme_len)
425 /* One of the Uri's has a scheme name, while the other doesn't. */
426 return FALSE;
429 /* If they have a userinfo component, perform case sensitive compare. */
430 if((a->userinfo_start > -1 && b->userinfo_start > -1) &&
431 (a->userinfo_len == b->userinfo_len)) {
432 if(StrCmpNW(a->canon_uri+a->userinfo_start, b->canon_uri+b->userinfo_start, a->userinfo_len))
433 return FALSE;
434 } else if(a->userinfo_len != b->userinfo_len)
435 /* One of the Uri's had a userinfo, while the other one doesn't. */
436 return FALSE;
438 /* Check if they have a host name. */
439 if((a->host_start > -1 && b->host_start > -1) &&
440 (a->host_len == b->host_len)) {
441 /* Perform a case insensitive compare if they are a known scheme type. */
442 if(known_scheme) {
443 if(StrCmpNIW(a->canon_uri+a->host_start, b->canon_uri+b->host_start, a->host_len))
444 return FALSE;
445 } else if(StrCmpNW(a->canon_uri+a->host_start, b->canon_uri+b->host_start, a->host_len))
446 return FALSE;
447 } else if(a->host_len != b->host_len)
448 /* One of the Uri's had a host, while the other one didn't. */
449 return FALSE;
451 if(a->has_port && b->has_port) {
452 if(a->port != b->port)
453 return FALSE;
454 } else if(a->has_port || b->has_port)
455 /* One had a port, while the other one didn't. */
456 return FALSE;
458 /* Windows is weird with how it handles paths. For example
459 * One URI could be "http://google.com" (after canonicalization)
460 * and one could be "http://google.com/" and the IsEqual function
461 * would still evaluate to TRUE, but, only if they are both hierarchical
462 * URIs.
464 if((a->path_start > -1 && b->path_start > -1) &&
465 (a->path_len == b->path_len)) {
466 if(StrCmpNW(a->canon_uri+a->path_start, b->canon_uri+b->path_start, a->path_len))
467 return FALSE;
468 } else if(are_hierarchical && a->path_len == -1 && b->path_len == 0) {
469 if(*(a->canon_uri+a->path_start) != '/')
470 return FALSE;
471 } else if(are_hierarchical && b->path_len == 1 && a->path_len == 0) {
472 if(*(b->canon_uri+b->path_start) != '/')
473 return FALSE;
474 } else if(a->path_len != b->path_len)
475 return FALSE;
477 /* Compare the query strings of the two URIs. */
478 if((a->query_start > -1 && b->query_start > -1) &&
479 (a->query_len == b->query_len)) {
480 if(StrCmpNW(a->canon_uri+a->query_start, b->canon_uri+b->query_start, a->query_len))
481 return FALSE;
482 } else if(a->query_len != b->query_len)
483 return FALSE;
485 if((a->fragment_start > -1 && b->fragment_start > -1) &&
486 (a->fragment_len == b->fragment_len)) {
487 if(StrCmpNW(a->canon_uri+a->fragment_start, b->canon_uri+b->fragment_start, a->fragment_len))
488 return FALSE;
489 } else if(a->fragment_len != b->fragment_len)
490 return FALSE;
492 /* If we get here, the two URIs are equivalent. */
493 return TRUE;
496 return FALSE;
499 /* Computes the size of the given IPv6 address.
500 * Each h16 component is 16bits, if there is an IPv4 address, it's
501 * 32bits. If there's an elision it can be 16bits to 128bits, depending
502 * on the number of other components.
504 * Modeled after google-url's CheckIPv6ComponentsSize function
506 static void compute_ipv6_comps_size(ipv6_address *address) {
507 address->components_size = address->h16_count * 2;
509 if(address->ipv4)
510 /* IPv4 address is 4 bytes. */
511 address->components_size += 4;
513 if(address->elision) {
514 /* An elision can be anywhere from 2 bytes up to 16 bytes.
515 * It size depends on the size of the h16 and IPv4 components.
517 address->elision_size = 16 - address->components_size;
518 if(address->elision_size < 2)
519 address->elision_size = 2;
520 } else
521 address->elision_size = 0;
524 /* Taken from dlls/jscript/lex.c */
525 static int hex_to_int(WCHAR val) {
526 if(val >= '0' && val <= '9')
527 return val - '0';
528 else if(val >= 'a' && val <= 'f')
529 return val - 'a' + 10;
530 else if(val >= 'A' && val <= 'F')
531 return val - 'A' + 10;
533 return -1;
536 /* Helper function for converting a percent encoded string
537 * representation of a WCHAR value into its actual WCHAR value. If
538 * the two characters following the '%' aren't valid hex values then
539 * this function returns the NULL character.
541 * Eg.
542 * "%2E" will result in '.' being returned by this function.
544 static WCHAR decode_pct_val(const WCHAR *ptr) {
545 WCHAR ret = '\0';
547 if(*ptr == '%' && is_hexdigit(*(ptr + 1)) && is_hexdigit(*(ptr + 2))) {
548 INT a = hex_to_int(*(ptr + 1));
549 INT b = hex_to_int(*(ptr + 2));
551 ret = a << 4;
552 ret += b;
555 return ret;
558 /* Helper function for percent encoding a given character
559 * and storing the encoded value into a given buffer (dest).
561 * It's up to the calling function to ensure that there is
562 * at least enough space in 'dest' for the percent encoded
563 * value to be stored (so dest + 3 spaces available).
565 static inline void pct_encode_val(WCHAR val, WCHAR *dest) {
566 dest[0] = '%';
567 dest[1] = hexDigits[(val >> 4) & 0xf];
568 dest[2] = hexDigits[val & 0xf];
571 /* Scans the range of characters [str, end] and returns the last occurrence
572 * of 'ch' or returns NULL.
574 static const WCHAR *str_last_of(const WCHAR *str, const WCHAR *end, WCHAR ch) {
575 const WCHAR *ptr = end;
577 while(ptr >= str) {
578 if(*ptr == ch)
579 return ptr;
580 --ptr;
583 return NULL;
586 /* Attempts to parse the domain name from the host.
588 * This function also includes the Top-level Domain (TLD) name
589 * of the host when it tries to find the domain name. If it finds
590 * a valid domain name it will assign 'domain_start' the offset
591 * into 'host' where the domain name starts.
593 * It's implied that if a domain name its range is implied to be
594 * [host+domain_start, host+host_len).
596 static void find_domain_name(const WCHAR *host, DWORD host_len,
597 INT *domain_start) {
598 const WCHAR *last_tld, *sec_last_tld, *end;
600 end = host+host_len-1;
602 *domain_start = -1;
604 /* There has to be at least enough room for a '.' followed by a
605 * 3 character TLD for a domain to even exist in the host name.
607 if(host_len < 4)
608 return;
610 last_tld = str_last_of(host, end, '.');
611 if(!last_tld)
612 /* http://hostname -> has no domain name. */
613 return;
615 sec_last_tld = str_last_of(host, last_tld-1, '.');
616 if(!sec_last_tld) {
617 /* If the '.' is at the beginning of the host there
618 * has to be at least 3 characters in the TLD for it
619 * to be valid.
620 * Ex: .com -> .com as the domain name.
621 * .co -> has no domain name.
623 if(last_tld-host == 0) {
624 if(end-(last_tld-1) < 3)
625 return;
626 } else if(last_tld-host == 3) {
627 DWORD i;
629 /* If there's three characters in front of last_tld and
630 * they are on the list of recognized TLDs, then this
631 * host doesn't have a domain (since the host only contains
632 * a TLD name.
633 * Ex: edu.uk -> has no domain name.
634 * foo.uk -> foo.uk as the domain name.
636 for(i = 0; i < sizeof(recognized_tlds)/sizeof(recognized_tlds[0]); ++i) {
637 if(!StrCmpNIW(host, recognized_tlds[i].tld_name, 3))
638 return;
640 } else if(last_tld-host < 3)
641 /* Anything less than 3 characters is considered part
642 * of the TLD name.
643 * Ex: ak.uk -> Has no domain name.
645 return;
647 /* Otherwise the domain name is the whole host name. */
648 *domain_start = 0;
649 } else if(end+1-last_tld > 3) {
650 /* If the last_tld has more than 3 characters, then it's automatically
651 * considered the TLD of the domain name.
652 * Ex: www.winehq.org.uk.test -> uk.test as the domain name.
654 *domain_start = (sec_last_tld+1)-host;
655 } else if(last_tld - (sec_last_tld+1) < 4) {
656 DWORD i;
657 /* If the sec_last_tld is 3 characters long it HAS to be on the list of
658 * recognized to still be considered part of the TLD name, otherwise
659 * its considered the domain name.
660 * Ex: www.google.com.uk -> google.com.uk as the domain name.
661 * www.google.foo.uk -> foo.uk as the domain name.
663 if(last_tld - (sec_last_tld+1) == 3) {
664 for(i = 0; i < sizeof(recognized_tlds)/sizeof(recognized_tlds[0]); ++i) {
665 if(!StrCmpNIW(sec_last_tld+1, recognized_tlds[i].tld_name, 3)) {
666 const WCHAR *domain = str_last_of(host, sec_last_tld-1, '.');
668 if(!domain)
669 *domain_start = 0;
670 else
671 *domain_start = (domain+1) - host;
672 TRACE("Found domain name %s\n", debugstr_wn(host+*domain_start,
673 (host+host_len)-(host+*domain_start)));
674 return;
678 *domain_start = (sec_last_tld+1)-host;
679 } else {
680 /* Since the sec_last_tld is less than 3 characters it's considered
681 * part of the TLD.
682 * Ex: www.google.fo.uk -> google.fo.uk as the domain name.
684 const WCHAR *domain = str_last_of(host, sec_last_tld-1, '.');
686 if(!domain)
687 *domain_start = 0;
688 else
689 *domain_start = (domain+1) - host;
691 } else {
692 /* The second to last TLD has more than 3 characters making it
693 * the domain name.
694 * Ex: www.google.test.us -> test.us as the domain name.
696 *domain_start = (sec_last_tld+1)-host;
699 TRACE("Found domain name %s\n", debugstr_wn(host+*domain_start,
700 (host+host_len)-(host+*domain_start)));
703 /* Removes the dot segments from a hierarchical URIs path component. This
704 * function performs the removal in place.
706 * This is a modified version of Qt's QUrl function "removeDotsFromPath".
708 * This function returns the new length of the path string.
710 static DWORD remove_dot_segments(WCHAR *path, DWORD path_len) {
711 WCHAR *out = path;
712 const WCHAR *in = out;
713 const WCHAR *end = out + path_len;
714 DWORD len;
716 while(in < end) {
717 /* A. if the input buffer begins with a prefix of "/./" or "/.",
718 * where "." is a complete path segment, then replace that
719 * prefix with "/" in the input buffer; otherwise,
721 if(in <= end - 3 && in[0] == '/' && in[1] == '.' && in[2] == '/') {
722 in += 2;
723 continue;
724 } else if(in == end - 2 && in[0] == '/' && in[1] == '.') {
725 *out++ = '/';
726 in += 2;
727 break;
730 /* B. if the input buffer begins with a prefix of "/../" or "/..",
731 * where ".." is a complete path segment, then replace that
732 * prefix with "/" in the input buffer and remove the last
733 * segment and its preceding "/" (if any) from the output
734 * buffer; otherwise,
736 if(in <= end - 4 && in[0] == '/' && in[1] == '.' && in[2] == '.' && in[3] == '/') {
737 while(out > path && *(--out) != '/');
739 in += 3;
740 continue;
741 } else if(in == end - 3 && in[0] == '/' && in[1] == '.' && in[2] == '.') {
742 while(out > path && *(--out) != '/');
744 if(*out == '/')
745 ++out;
747 in += 3;
748 break;
751 /* C. move the first path segment in the input buffer to the end of
752 * the output buffer, including the initial "/" character (if
753 * any) and any subsequent characters up to, but not including,
754 * the next "/" character or the end of the input buffer.
756 *out++ = *in++;
757 while(in < end && *in != '/')
758 *out++ = *in++;
761 len = out - path;
762 TRACE("(%p %d): Path after dot segments removed %s len=%d\n", path, path_len,
763 debugstr_wn(path, len), len);
764 return len;
767 /* Attempts to find the file extension in a given path. */
768 static INT find_file_extension(const WCHAR *path, DWORD path_len) {
769 const WCHAR *end;
771 for(end = path+path_len-1; end >= path && *end != '/' && *end != '\\'; --end) {
772 if(*end == '.')
773 return end-path;
776 return -1;
779 /* Computes the location where the elision should occur in the IPv6
780 * address using the numerical values of each component stored in
781 * 'values'. If the address shouldn't contain an elision then 'index'
782 * is assigned -1 as it's value. Otherwise 'index' will contain the
783 * starting index (into values) where the elision should be, and 'count'
784 * will contain the number of cells the elision covers.
786 * NOTES:
787 * Windows will expand an elision if the elision only represents 1 h16
788 * component of the URI.
790 * Ex: [1::2:3:4:5:6:7] -> [1:0:2:3:4:5:6:7]
792 * If the IPv6 address contains an IPv4 address, the IPv4 address is also
793 * considered for being included as part of an elision if all it's components
794 * are zeros.
796 * Ex: [1:2:3:4:5:6:0.0.0.0] -> [1:2:3:4:5:6::]
798 static void compute_elision_location(const ipv6_address *address, const USHORT values[8],
799 INT *index, DWORD *count) {
800 DWORD i, max_len, cur_len;
801 INT max_index, cur_index;
803 max_len = cur_len = 0;
804 max_index = cur_index = -1;
805 for(i = 0; i < 8; ++i) {
806 BOOL check_ipv4 = (address->ipv4 && i == 6);
807 BOOL is_end = (check_ipv4 || i == 7);
809 if(check_ipv4) {
810 /* Check if the IPv4 address contains only zeros. */
811 if(values[i] == 0 && values[i+1] == 0) {
812 if(cur_index == -1)
813 cur_index = i;
815 cur_len += 2;
816 ++i;
818 } else if(values[i] == 0) {
819 if(cur_index == -1)
820 cur_index = i;
822 ++cur_len;
825 if(is_end || values[i] != 0) {
826 /* We only consider it for an elision if it's
827 * more than 1 component long.
829 if(cur_len > 1 && cur_len > max_len) {
830 /* Found the new elision location. */
831 max_len = cur_len;
832 max_index = cur_index;
835 /* Reset the current range for the next range of zeros. */
836 cur_index = -1;
837 cur_len = 0;
841 *index = max_index;
842 *count = max_len;
845 /* Removes all the leading and trailing white spaces or
846 * control characters from the URI and removes all control
847 * characters inside of the URI string.
849 static BSTR pre_process_uri(LPCWSTR uri) {
850 BSTR ret;
851 DWORD len;
852 const WCHAR *start, *end;
853 WCHAR *buf, *ptr;
855 len = lstrlenW(uri);
857 start = uri;
858 /* Skip leading controls and whitespace. */
859 while(iscntrlW(*start) || isspaceW(*start)) ++start;
861 end = uri+len-1;
862 if(start == end)
863 /* URI consisted only of control/whitespace. */
864 ret = SysAllocStringLen(NULL, 0);
865 else {
866 while(iscntrlW(*end) || isspaceW(*end)) --end;
868 buf = heap_alloc(((end+1)-start)*sizeof(WCHAR));
869 if(!buf)
870 return NULL;
872 for(ptr = buf; start < end+1; ++start) {
873 if(!iscntrlW(*start))
874 *ptr++ = *start;
877 ret = SysAllocStringLen(buf, ptr-buf);
878 heap_free(buf);
881 return ret;
884 /* Converts the specified IPv4 address into an uint value.
886 * This function assumes that the IPv4 address has already been validated.
888 static UINT ipv4toui(const WCHAR *ip, DWORD len) {
889 UINT ret = 0;
890 DWORD comp_value = 0;
891 const WCHAR *ptr;
893 for(ptr = ip; ptr < ip+len; ++ptr) {
894 if(*ptr == '.') {
895 ret <<= 8;
896 ret += comp_value;
897 comp_value = 0;
898 } else
899 comp_value = comp_value*10 + (*ptr-'0');
902 ret <<= 8;
903 ret += comp_value;
905 return ret;
908 /* Converts an IPv4 address in numerical form into it's fully qualified
909 * string form. This function returns the number of characters written
910 * to 'dest'. If 'dest' is NULL this function will return the number of
911 * characters that would have been written.
913 * It's up to the caller to ensure there's enough space in 'dest' for the
914 * address.
916 static DWORD ui2ipv4(WCHAR *dest, UINT address) {
917 static const WCHAR formatW[] =
918 {'%','u','.','%','u','.','%','u','.','%','u',0};
919 DWORD ret = 0;
920 UCHAR digits[4];
922 digits[0] = (address >> 24) & 0xff;
923 digits[1] = (address >> 16) & 0xff;
924 digits[2] = (address >> 8) & 0xff;
925 digits[3] = address & 0xff;
927 if(!dest) {
928 WCHAR tmp[16];
929 ret = sprintfW(tmp, formatW, digits[0], digits[1], digits[2], digits[3]);
930 } else
931 ret = sprintfW(dest, formatW, digits[0], digits[1], digits[2], digits[3]);
933 return ret;
936 /* Converts an h16 component (from an IPv6 address) into it's
937 * numerical value.
939 * This function assumes that the h16 component has already been validated.
941 static USHORT h16tous(h16 component) {
942 DWORD i;
943 USHORT ret = 0;
945 for(i = 0; i < component.len; ++i) {
946 ret <<= 4;
947 ret += hex_to_int(component.str[i]);
950 return ret;
953 /* Converts an IPv6 address into it's 128 bits (16 bytes) numerical value.
955 * This function assumes that the ipv6_address has already been validated.
957 static BOOL ipv6_to_number(const ipv6_address *address, USHORT number[8]) {
958 DWORD i, cur_component = 0;
959 BOOL already_passed_elision = FALSE;
961 for(i = 0; i < address->h16_count; ++i) {
962 if(address->elision) {
963 if(address->components[i].str > address->elision && !already_passed_elision) {
964 /* Means we just passed the elision and need to add it's values to
965 * 'number' before we do anything else.
967 DWORD j = 0;
968 for(j = 0; j < address->elision_size; j+=2)
969 number[cur_component++] = 0;
971 already_passed_elision = TRUE;
975 number[cur_component++] = h16tous(address->components[i]);
978 /* Case when the elision appears after the h16 components. */
979 if(!already_passed_elision && address->elision) {
980 for(i = 0; i < address->elision_size; i+=2)
981 number[cur_component++] = 0;
982 already_passed_elision = TRUE;
985 if(address->ipv4) {
986 UINT value = ipv4toui(address->ipv4, address->ipv4_len);
988 if(cur_component != 6) {
989 ERR("(%p %p): Failed sanity check with %d\n", address, number, cur_component);
990 return FALSE;
993 number[cur_component++] = (value >> 16) & 0xffff;
994 number[cur_component] = value & 0xffff;
997 return TRUE;
1000 /* Checks if the characters pointed to by 'ptr' are
1001 * a percent encoded data octet.
1003 * pct-encoded = "%" HEXDIG HEXDIG
1005 static BOOL check_pct_encoded(const WCHAR **ptr) {
1006 const WCHAR *start = *ptr;
1008 if(**ptr != '%')
1009 return FALSE;
1011 ++(*ptr);
1012 if(!is_hexdigit(**ptr)) {
1013 *ptr = start;
1014 return FALSE;
1017 ++(*ptr);
1018 if(!is_hexdigit(**ptr)) {
1019 *ptr = start;
1020 return FALSE;
1023 ++(*ptr);
1024 return TRUE;
1027 /* dec-octet = DIGIT ; 0-9
1028 * / %x31-39 DIGIT ; 10-99
1029 * / "1" 2DIGIT ; 100-199
1030 * / "2" %x30-34 DIGIT ; 200-249
1031 * / "25" %x30-35 ; 250-255
1033 static BOOL check_dec_octet(const WCHAR **ptr) {
1034 const WCHAR *c1, *c2, *c3;
1036 c1 = *ptr;
1037 /* A dec-octet must be at least 1 digit long. */
1038 if(*c1 < '0' || *c1 > '9')
1039 return FALSE;
1041 ++(*ptr);
1043 c2 = *ptr;
1044 /* Since the 1 digit requirment was meet, it doesn't
1045 * matter if this is a DIGIT value, it's considered a
1046 * dec-octet.
1048 if(*c2 < '0' || *c2 > '9')
1049 return TRUE;
1051 ++(*ptr);
1053 c3 = *ptr;
1054 /* Same explanation as above. */
1055 if(*c3 < '0' || *c3 > '9')
1056 return TRUE;
1058 /* Anything > 255 isn't a valid IP dec-octet. */
1059 if(*c1 >= '2' && *c2 >= '5' && *c3 >= '5') {
1060 *ptr = c1;
1061 return FALSE;
1064 ++(*ptr);
1065 return TRUE;
1068 /* Checks if there is an implicit IPv4 address in the host component of the URI.
1069 * The max value of an implicit IPv4 address is UINT_MAX.
1071 * Ex:
1072 * "234567" would be considered an implicit IPv4 address.
1074 static BOOL check_implicit_ipv4(const WCHAR **ptr, UINT *val) {
1075 const WCHAR *start = *ptr;
1076 ULONGLONG ret = 0;
1077 *val = 0;
1079 while(is_num(**ptr)) {
1080 ret = ret*10 + (**ptr - '0');
1082 if(ret > UINT_MAX) {
1083 *ptr = start;
1084 return FALSE;
1086 ++(*ptr);
1089 if(*ptr == start)
1090 return FALSE;
1092 *val = ret;
1093 return TRUE;
1096 /* Checks if the string contains an IPv4 address.
1098 * This function has a strict mode or a non-strict mode of operation
1099 * When 'strict' is set to FALSE this function will return TRUE if
1100 * the string contains at least 'dec-octet "." dec-octet' since partial
1101 * IPv4 addresses will be normalized out into full IPv4 addresses. When
1102 * 'strict' is set this function expects there to be a full IPv4 address.
1104 * IPv4address = dec-octet "." dec-octet "." dec-octet "." dec-octet
1106 static BOOL check_ipv4address(const WCHAR **ptr, BOOL strict) {
1107 const WCHAR *start = *ptr;
1109 if(!check_dec_octet(ptr)) {
1110 *ptr = start;
1111 return FALSE;
1114 if(**ptr != '.') {
1115 *ptr = start;
1116 return FALSE;
1119 ++(*ptr);
1120 if(!check_dec_octet(ptr)) {
1121 *ptr = start;
1122 return FALSE;
1125 if(**ptr != '.') {
1126 if(strict) {
1127 *ptr = start;
1128 return FALSE;
1129 } else
1130 return TRUE;
1133 ++(*ptr);
1134 if(!check_dec_octet(ptr)) {
1135 *ptr = start;
1136 return FALSE;
1139 if(**ptr != '.') {
1140 if(strict) {
1141 *ptr = start;
1142 return FALSE;
1143 } else
1144 return TRUE;
1147 ++(*ptr);
1148 if(!check_dec_octet(ptr)) {
1149 *ptr = start;
1150 return FALSE;
1153 /* Found a four digit ip address. */
1154 return TRUE;
1156 /* Tries to parse the scheme name of the URI.
1158 * scheme = ALPHA *(ALPHA | NUM | '+' | '-' | '.') as defined by RFC 3896.
1159 * NOTE: Windows accepts a number as the first character of a scheme.
1161 static BOOL parse_scheme_name(const WCHAR **ptr, parse_data *data, DWORD extras) {
1162 const WCHAR *start = *ptr;
1164 data->scheme = NULL;
1165 data->scheme_len = 0;
1167 while(**ptr) {
1168 if(**ptr == '*' && *ptr == start) {
1169 /* Might have found a wildcard scheme. If it is the next
1170 * char has to be a ':' for it to be a valid URI
1172 ++(*ptr);
1173 break;
1174 } else if(!is_num(**ptr) && !is_alpha(**ptr) && **ptr != '+' &&
1175 **ptr != '-' && **ptr != '.')
1176 break;
1178 (*ptr)++;
1181 if(*ptr == start)
1182 return FALSE;
1184 /* Schemes must end with a ':' */
1185 if(**ptr != ':' && !((extras & ALLOW_NULL_TERM_SCHEME) && !**ptr)) {
1186 *ptr = start;
1187 return FALSE;
1190 data->scheme = start;
1191 data->scheme_len = *ptr - start;
1193 ++(*ptr);
1194 return TRUE;
1197 /* Tries to deduce the corresponding URL_SCHEME for the given URI. Stores
1198 * the deduced URL_SCHEME in data->scheme_type.
1200 static BOOL parse_scheme_type(parse_data *data) {
1201 /* If there's scheme data then see if it's a recognized scheme. */
1202 if(data->scheme && data->scheme_len) {
1203 DWORD i;
1205 for(i = 0; i < sizeof(recognized_schemes)/sizeof(recognized_schemes[0]); ++i) {
1206 if(lstrlenW(recognized_schemes[i].scheme_name) == data->scheme_len) {
1207 /* Has to be a case insensitive compare. */
1208 if(!StrCmpNIW(recognized_schemes[i].scheme_name, data->scheme, data->scheme_len)) {
1209 data->scheme_type = recognized_schemes[i].scheme;
1210 return TRUE;
1215 /* If we get here it means it's not a recognized scheme. */
1216 data->scheme_type = URL_SCHEME_UNKNOWN;
1217 return TRUE;
1218 } else if(data->is_relative) {
1219 /* Relative URI's have no scheme. */
1220 data->scheme_type = URL_SCHEME_UNKNOWN;
1221 return TRUE;
1222 } else {
1223 /* Should never reach here! what happened... */
1224 FIXME("(%p): Unable to determine scheme type for URI %s\n", data, debugstr_w(data->uri));
1225 return FALSE;
1229 /* Tries to parse (or deduce) the scheme_name of a URI. If it can't
1230 * parse a scheme from the URI it will try to deduce the scheme_name and scheme_type
1231 * using the flags specified in 'flags' (if any). Flags that affect how this function
1232 * operates are the Uri_CREATE_ALLOW_* flags.
1234 * All parsed/deduced information will be stored in 'data' when the function returns.
1236 * Returns TRUE if it was able to successfully parse the information.
1238 static BOOL parse_scheme(const WCHAR **ptr, parse_data *data, DWORD flags, DWORD extras) {
1239 static const WCHAR fileW[] = {'f','i','l','e',0};
1240 static const WCHAR wildcardW[] = {'*',0};
1242 /* First check to see if the uri could implicitly be a file path. */
1243 if(is_implicit_file_path(*ptr)) {
1244 if(flags & Uri_CREATE_ALLOW_IMPLICIT_FILE_SCHEME) {
1245 data->scheme = fileW;
1246 data->scheme_len = lstrlenW(fileW);
1247 data->has_implicit_scheme = TRUE;
1249 TRACE("(%p %p %x): URI is an implicit file path.\n", ptr, data, flags);
1250 } else {
1251 /* Window's does not consider anything that can implicitly be a file
1252 * path to be a valid URI if the ALLOW_IMPLICIT_FILE_SCHEME flag is not set...
1254 TRACE("(%p %p %x): URI is implicitly a file path, but, the ALLOW_IMPLICIT_FILE_SCHEME flag wasn't set.\n",
1255 ptr, data, flags);
1256 return FALSE;
1258 } else if(!parse_scheme_name(ptr, data, extras)) {
1259 /* No Scheme was found, this means it could be:
1260 * a) an implicit Wildcard scheme
1261 * b) a relative URI
1262 * c) a invalid URI.
1264 if(flags & Uri_CREATE_ALLOW_IMPLICIT_WILDCARD_SCHEME) {
1265 data->scheme = wildcardW;
1266 data->scheme_len = lstrlenW(wildcardW);
1267 data->has_implicit_scheme = TRUE;
1269 TRACE("(%p %p %x): URI is an implicit wildcard scheme.\n", ptr, data, flags);
1270 } else if (flags & Uri_CREATE_ALLOW_RELATIVE) {
1271 data->is_relative = TRUE;
1272 TRACE("(%p %p %x): URI is relative.\n", ptr, data, flags);
1273 } else {
1274 TRACE("(%p %p %x): Malformed URI found. Unable to deduce scheme name.\n", ptr, data, flags);
1275 return FALSE;
1279 if(!data->is_relative)
1280 TRACE("(%p %p %x): Found scheme=%s scheme_len=%d\n", ptr, data, flags,
1281 debugstr_wn(data->scheme, data->scheme_len), data->scheme_len);
1283 if(!parse_scheme_type(data))
1284 return FALSE;
1286 TRACE("(%p %p %x): Assigned %d as the URL_SCHEME.\n", ptr, data, flags, data->scheme_type);
1287 return TRUE;
1290 static BOOL parse_username(const WCHAR **ptr, parse_data *data, DWORD flags, DWORD extras) {
1291 data->username = *ptr;
1293 while(**ptr != ':' && **ptr != '@') {
1294 if(**ptr == '%') {
1295 if(!check_pct_encoded(ptr)) {
1296 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
1297 *ptr = data->username;
1298 data->username = NULL;
1299 return FALSE;
1301 } else
1302 continue;
1303 } else if(extras & ALLOW_NULL_TERM_USER_NAME && !**ptr)
1304 break;
1305 else if(is_auth_delim(**ptr, data->scheme_type != URL_SCHEME_UNKNOWN)) {
1306 *ptr = data->username;
1307 data->username = NULL;
1308 return FALSE;
1311 ++(*ptr);
1314 data->username_len = *ptr - data->username;
1315 return TRUE;
1318 static BOOL parse_password(const WCHAR **ptr, parse_data *data, DWORD flags, DWORD extras) {
1319 const WCHAR *start = *ptr;
1321 if(**ptr != ':')
1322 return TRUE;
1324 ++(*ptr);
1325 data->password = *ptr;
1327 while(**ptr != '@') {
1328 if(**ptr == '%') {
1329 if(!check_pct_encoded(ptr)) {
1330 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
1331 *ptr = start;
1332 data->password = NULL;
1333 return FALSE;
1335 } else
1336 continue;
1337 } else if(extras & ALLOW_NULL_TERM_PASSWORD && !**ptr)
1338 break;
1339 else if(is_auth_delim(**ptr, data->scheme_type != URL_SCHEME_UNKNOWN)) {
1340 *ptr = start;
1341 data->password = NULL;
1342 return FALSE;
1345 ++(*ptr);
1348 data->password_len = *ptr - data->password;
1349 return TRUE;
1352 /* Parses the userinfo part of the URI (if it exists). The userinfo field of
1353 * a URI can consist of "username:password@", or just "username@".
1355 * RFC def:
1356 * userinfo = *( unreserved / pct-encoded / sub-delims / ":" )
1358 * NOTES:
1359 * 1) If there is more than one ':' in the userinfo part of the URI Windows
1360 * uses the first occurrence of ':' to delimit the username and password
1361 * components.
1363 * ex:
1364 * ftp://user:pass:word@winehq.org
1366 * Would yield, "user" as the username and "pass:word" as the password.
1368 * 2) Windows allows any character to appear in the "userinfo" part of
1369 * a URI, as long as it's not an authority delimeter character set.
1371 static void parse_userinfo(const WCHAR **ptr, parse_data *data, DWORD flags) {
1372 const WCHAR *start = *ptr;
1374 if(!parse_username(ptr, data, flags, 0)) {
1375 TRACE("(%p %p %x): URI contained no userinfo.\n", ptr, data, flags);
1376 return;
1379 if(!parse_password(ptr, data, flags, 0)) {
1380 *ptr = start;
1381 data->username = NULL;
1382 data->username_len = 0;
1383 TRACE("(%p %p %x): URI contained no userinfo.\n", ptr, data, flags);
1384 return;
1387 if(**ptr != '@') {
1388 *ptr = start;
1389 data->username = NULL;
1390 data->username_len = 0;
1391 data->password = NULL;
1392 data->password_len = 0;
1394 TRACE("(%p %p %x): URI contained no userinfo.\n", ptr, data, flags);
1395 return;
1398 if(data->username)
1399 TRACE("(%p %p %x): Found username %s len=%d.\n", ptr, data, flags,
1400 debugstr_wn(data->username, data->username_len), data->username_len);
1402 if(data->password)
1403 TRACE("(%p %p %x): Found password %s len=%d.\n", ptr, data, flags,
1404 debugstr_wn(data->password, data->password_len), data->password_len);
1406 ++(*ptr);
1409 /* Attempts to parse a port from the URI.
1411 * NOTES:
1412 * Windows seems to have a cap on what the maximum value
1413 * for a port can be. The max value is USHORT_MAX.
1415 * port = *DIGIT
1417 static BOOL parse_port(const WCHAR **ptr, parse_data *data, DWORD flags) {
1418 UINT port = 0;
1419 data->port = *ptr;
1421 while(!is_auth_delim(**ptr, data->scheme_type != URL_SCHEME_UNKNOWN)) {
1422 if(!is_num(**ptr)) {
1423 *ptr = data->port;
1424 data->port = NULL;
1425 return FALSE;
1428 port = port*10 + (**ptr-'0');
1430 if(port > USHORT_MAX) {
1431 *ptr = data->port;
1432 data->port = NULL;
1433 return FALSE;
1436 ++(*ptr);
1439 data->has_port = TRUE;
1440 data->port_value = port;
1441 data->port_len = *ptr - data->port;
1443 TRACE("(%p %p %x): Found port %s len=%d value=%u\n", ptr, data, flags,
1444 debugstr_wn(data->port, data->port_len), data->port_len, data->port_value);
1445 return TRUE;
1448 /* Attempts to parse a IPv4 address from the URI.
1450 * NOTES:
1451 * Window's normalizes IPv4 addresses, This means there's three
1452 * possibilities for the URI to contain an IPv4 address.
1453 * 1) A well formed address (ex. 192.2.2.2).
1454 * 2) A partially formed address. For example "192.0" would
1455 * normalize to "192.0.0.0" during canonicalization.
1456 * 3) An implicit IPv4 address. For example "256" would
1457 * normalize to "0.0.1.0" during canonicalization. Also
1458 * note that the maximum value for an implicit IP address
1459 * is UINT_MAX, if the value in the URI exceeds this then
1460 * it is not considered an IPv4 address.
1462 static BOOL parse_ipv4address(const WCHAR **ptr, parse_data *data, DWORD flags) {
1463 const BOOL is_unknown = data->scheme_type == URL_SCHEME_UNKNOWN;
1464 data->host = *ptr;
1466 if(!check_ipv4address(ptr, FALSE)) {
1467 if(!check_implicit_ipv4(ptr, &data->implicit_ipv4)) {
1468 TRACE("(%p %p %x): URI didn't contain anything looking like an IPv4 address.\n",
1469 ptr, data, flags);
1470 *ptr = data->host;
1471 data->host = NULL;
1472 return FALSE;
1473 } else
1474 data->has_implicit_ip = TRUE;
1477 /* Check if what we found is the only part of the host name (if it isn't
1478 * we don't have an IPv4 address).
1480 if(**ptr == ':') {
1481 ++(*ptr);
1482 if(!parse_port(ptr, data, flags)) {
1483 *ptr = data->host;
1484 data->host = NULL;
1485 return FALSE;
1487 } else if(!is_auth_delim(**ptr, !is_unknown)) {
1488 /* Found more data which belongs the host, so this isn't an IPv4. */
1489 *ptr = data->host;
1490 data->host = NULL;
1491 data->has_implicit_ip = FALSE;
1492 return FALSE;
1495 data->host_len = *ptr - data->host;
1496 data->host_type = Uri_HOST_IPV4;
1498 TRACE("(%p %p %x): IPv4 address found. host=%s host_len=%d host_type=%d\n",
1499 ptr, data, flags, debugstr_wn(data->host, data->host_len),
1500 data->host_len, data->host_type);
1501 return TRUE;
1504 /* Attempts to parse the reg-name from the URI.
1506 * Because of the way Windows handles ':' this function also
1507 * handles parsing the port.
1509 * reg-name = *( unreserved / pct-encoded / sub-delims )
1511 * NOTE:
1512 * Windows allows everything, but, the characters in "auth_delims" and ':'
1513 * to appear in a reg-name, unless it's an unknown scheme type then ':' is
1514 * allowed to appear (even if a valid port isn't after it).
1516 * Windows doesn't like host names which start with '[' and end with ']'
1517 * and don't contain a valid IP literal address in between them.
1519 * On Windows if an '[' is encountered in the host name the ':' no longer
1520 * counts as a delimiter until you reach the next ']' or an "authority delimeter".
1522 * A reg-name CAN be empty.
1524 static BOOL parse_reg_name(const WCHAR **ptr, parse_data *data, DWORD flags, DWORD extras) {
1525 const BOOL has_start_bracket = **ptr == '[';
1526 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
1527 BOOL inside_brackets = has_start_bracket;
1528 BOOL ignore_col = extras & IGNORE_PORT_DELIMITER;
1530 /* We have to be careful with file schemes. */
1531 if(data->scheme_type == URL_SCHEME_FILE) {
1532 /* This is because an implicit file scheme could be "C:\\test" and it
1533 * would trick this function into thinking the host is "C", when after
1534 * canonicalization the host would end up being an empty string. A drive
1535 * path can also have a '|' instead of a ':' after the drive letter.
1537 if(is_drive_path(*ptr)) {
1538 /* Regular old drive paths don't have a host type (or host name). */
1539 data->host_type = Uri_HOST_UNKNOWN;
1540 data->host = *ptr;
1541 data->host_len = 0;
1542 return TRUE;
1543 } else if(is_unc_path(*ptr))
1544 /* Skip past the "\\" of a UNC path. */
1545 *ptr += 2;
1548 data->host = *ptr;
1550 while(!is_auth_delim(**ptr, known_scheme)) {
1551 if(**ptr == ':' && !ignore_col) {
1552 /* We can ignore ':' if were inside brackets.*/
1553 if(!inside_brackets) {
1554 const WCHAR *tmp = (*ptr)++;
1556 /* Attempt to parse the port. */
1557 if(!parse_port(ptr, data, flags)) {
1558 /* Windows expects there to be a valid port for known scheme types. */
1559 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
1560 *ptr = data->host;
1561 data->host = NULL;
1562 TRACE("(%p %p %x %x): Expected valid port\n", ptr, data, flags, extras);
1563 return FALSE;
1564 } else
1565 /* Windows gives up on trying to parse a port when it
1566 * encounters 1 invalid port.
1568 ignore_col = TRUE;
1569 } else {
1570 data->host_len = tmp - data->host;
1571 break;
1574 } else if(**ptr == '%' && known_scheme) {
1575 /* Has to be a legit % encoded value. */
1576 if(!check_pct_encoded(ptr)) {
1577 *ptr = data->host;
1578 data->host = NULL;
1579 return FALSE;
1580 } else
1581 continue;
1582 } else if(**ptr == ']')
1583 inside_brackets = FALSE;
1584 else if(**ptr == '[')
1585 inside_brackets = TRUE;
1587 ++(*ptr);
1590 if(has_start_bracket) {
1591 /* Make sure the last character of the host wasn't a ']'. */
1592 if(*(*ptr-1) == ']') {
1593 TRACE("(%p %p %x %x): Expected an IP literal inside of the host\n",
1594 ptr, data, flags, extras);
1595 *ptr = data->host;
1596 data->host = NULL;
1597 return FALSE;
1601 /* Don't overwrite our length if we found a port earlier. */
1602 if(!data->port)
1603 data->host_len = *ptr - data->host;
1605 /* If the host is empty, then it's an unknown host type. */
1606 if(data->host_len == 0)
1607 data->host_type = Uri_HOST_UNKNOWN;
1608 else
1609 data->host_type = Uri_HOST_DNS;
1611 TRACE("(%p %p %x %x): Parsed reg-name. host=%s len=%d\n", ptr, data, flags, extras,
1612 debugstr_wn(data->host, data->host_len), data->host_len);
1613 return TRUE;
1616 /* Attempts to parse an IPv6 address out of the URI.
1618 * IPv6address = 6( h16 ":" ) ls32
1619 * / "::" 5( h16 ":" ) ls32
1620 * / [ h16 ] "::" 4( h16 ":" ) ls32
1621 * / [ *1( h16 ":" ) h16 ] "::" 3( h16 ":" ) ls32
1622 * / [ *2( h16 ":" ) h16 ] "::" 2( h16 ":" ) ls32
1623 * / [ *3( h16 ":" ) h16 ] "::" h16 ":" ls32
1624 * / [ *4( h16 ":" ) h16 ] "::" ls32
1625 * / [ *5( h16 ":" ) h16 ] "::" h16
1626 * / [ *6( h16 ":" ) h16 ] "::"
1628 * ls32 = ( h16 ":" h16 ) / IPv4address
1629 * ; least-significant 32 bits of address.
1631 * h16 = 1*4HEXDIG
1632 * ; 16 bits of address represented in hexadecimal.
1634 * Modeled after google-url's 'DoParseIPv6' function.
1636 static BOOL parse_ipv6address(const WCHAR **ptr, parse_data *data, DWORD flags) {
1637 const WCHAR *start, *cur_start;
1638 ipv6_address ip;
1640 start = cur_start = *ptr;
1641 memset(&ip, 0, sizeof(ipv6_address));
1643 for(;; ++(*ptr)) {
1644 /* Check if we're on the last character of the host. */
1645 BOOL is_end = (is_auth_delim(**ptr, data->scheme_type != URL_SCHEME_UNKNOWN)
1646 || **ptr == ']');
1648 BOOL is_split = (**ptr == ':');
1649 BOOL is_elision = (is_split && !is_end && *(*ptr+1) == ':');
1651 /* Check if we're at the end of a component, or
1652 * if we're at the end of the IPv6 address.
1654 if(is_split || is_end) {
1655 DWORD cur_len = 0;
1657 cur_len = *ptr - cur_start;
1659 /* h16 can't have a length > 4. */
1660 if(cur_len > 4) {
1661 *ptr = start;
1663 TRACE("(%p %p %x): h16 component to long.\n",
1664 ptr, data, flags);
1665 return FALSE;
1668 if(cur_len == 0) {
1669 /* An h16 component can't have the length of 0 unless
1670 * the elision is at the beginning of the address, or
1671 * at the end of the address.
1673 if(!((*ptr == start && is_elision) ||
1674 (is_end && (*ptr-2) == ip.elision))) {
1675 *ptr = start;
1676 TRACE("(%p %p %x): IPv6 component cannot have a length of 0.\n",
1677 ptr, data, flags);
1678 return FALSE;
1682 if(cur_len > 0) {
1683 /* An IPv6 address can have no more than 8 h16 components. */
1684 if(ip.h16_count >= 8) {
1685 *ptr = start;
1686 TRACE("(%p %p %x): Not a IPv6 address, to many h16 components.\n",
1687 ptr, data, flags);
1688 return FALSE;
1691 ip.components[ip.h16_count].str = cur_start;
1692 ip.components[ip.h16_count].len = cur_len;
1694 TRACE("(%p %p %x): Found h16 component %s, len=%d, h16_count=%d\n",
1695 ptr, data, flags, debugstr_wn(cur_start, cur_len), cur_len,
1696 ip.h16_count);
1697 ++ip.h16_count;
1701 if(is_end)
1702 break;
1704 if(is_elision) {
1705 /* A IPv6 address can only have 1 elision ('::'). */
1706 if(ip.elision) {
1707 *ptr = start;
1709 TRACE("(%p %p %x): IPv6 address cannot have 2 elisions.\n",
1710 ptr, data, flags);
1711 return FALSE;
1714 ip.elision = *ptr;
1715 ++(*ptr);
1718 if(is_split)
1719 cur_start = *ptr+1;
1720 else {
1721 if(!check_ipv4address(ptr, TRUE)) {
1722 if(!is_hexdigit(**ptr)) {
1723 /* Not a valid character for an IPv6 address. */
1724 *ptr = start;
1725 return FALSE;
1727 } else {
1728 /* Found an IPv4 address. */
1729 ip.ipv4 = cur_start;
1730 ip.ipv4_len = *ptr - cur_start;
1732 TRACE("(%p %p %x): Found an attached IPv4 address %s len=%d.\n",
1733 ptr, data, flags, debugstr_wn(ip.ipv4, ip.ipv4_len),
1734 ip.ipv4_len);
1736 /* IPv4 addresses can only appear at the end of a IPv6. */
1737 break;
1742 compute_ipv6_comps_size(&ip);
1744 /* Make sure the IPv6 address adds up to 16 bytes. */
1745 if(ip.components_size + ip.elision_size != 16) {
1746 *ptr = start;
1747 TRACE("(%p %p %x): Invalid IPv6 address, did not add up to 16 bytes.\n",
1748 ptr, data, flags);
1749 return FALSE;
1752 if(ip.elision_size == 2) {
1753 /* For some reason on Windows if an elision that represents
1754 * only 1 h16 component is encountered at the very begin or
1755 * end of an IPv6 address, Windows does not consider it a
1756 * valid IPv6 address.
1758 * Ex: [::2:3:4:5:6:7] is not valid, even though the sum
1759 * of all the components == 128bits.
1761 if(ip.elision < ip.components[0].str ||
1762 ip.elision > ip.components[ip.h16_count-1].str) {
1763 *ptr = start;
1764 TRACE("(%p %p %x): Invalid IPv6 address. Detected elision of 2 bytes at the beginning or end of the address.\n",
1765 ptr, data, flags);
1766 return FALSE;
1770 data->host_type = Uri_HOST_IPV6;
1771 data->has_ipv6 = TRUE;
1772 data->ipv6_address = ip;
1774 TRACE("(%p %p %x): Found valid IPv6 literal %s len=%d\n",
1775 ptr, data, flags, debugstr_wn(start, *ptr-start),
1776 *ptr-start);
1777 return TRUE;
1780 /* IPvFuture = "v" 1*HEXDIG "." 1*( unreserved / sub-delims / ":" ) */
1781 static BOOL parse_ipvfuture(const WCHAR **ptr, parse_data *data, DWORD flags) {
1782 const WCHAR *start = *ptr;
1784 /* IPvFuture has to start with a 'v' or 'V'. */
1785 if(**ptr != 'v' && **ptr != 'V')
1786 return FALSE;
1788 /* Following the v there must be at least 1 hex digit. */
1789 ++(*ptr);
1790 if(!is_hexdigit(**ptr)) {
1791 *ptr = start;
1792 return FALSE;
1795 ++(*ptr);
1796 while(is_hexdigit(**ptr))
1797 ++(*ptr);
1799 /* End of the hexdigit sequence must be a '.' */
1800 if(**ptr != '.') {
1801 *ptr = start;
1802 return FALSE;
1805 ++(*ptr);
1806 if(!is_unreserved(**ptr) && !is_subdelim(**ptr) && **ptr != ':') {
1807 *ptr = start;
1808 return FALSE;
1811 ++(*ptr);
1812 while(is_unreserved(**ptr) || is_subdelim(**ptr) || **ptr == ':')
1813 ++(*ptr);
1815 data->host_type = Uri_HOST_UNKNOWN;
1817 TRACE("(%p %p %x): Parsed IPvFuture address %s len=%d\n", ptr, data, flags,
1818 debugstr_wn(start, *ptr-start), *ptr-start);
1820 return TRUE;
1823 /* IP-literal = "[" ( IPv6address / IPvFuture ) "]" */
1824 static BOOL parse_ip_literal(const WCHAR **ptr, parse_data *data, DWORD flags, DWORD extras) {
1825 data->host = *ptr;
1827 if(**ptr != '[' && !(extras & ALLOW_BRACKETLESS_IP_LITERAL)) {
1828 data->host = NULL;
1829 return FALSE;
1830 } else if(**ptr == '[')
1831 ++(*ptr);
1833 if(!parse_ipv6address(ptr, data, flags)) {
1834 if(extras & SKIP_IP_FUTURE_CHECK || !parse_ipvfuture(ptr, data, flags)) {
1835 *ptr = data->host;
1836 data->host = NULL;
1837 return FALSE;
1841 if(**ptr != ']' && !(extras & ALLOW_BRACKETLESS_IP_LITERAL)) {
1842 *ptr = data->host;
1843 data->host = NULL;
1844 return FALSE;
1845 } else if(!**ptr && extras & ALLOW_BRACKETLESS_IP_LITERAL) {
1846 /* The IP literal didn't contain brackets and was followed by
1847 * a NULL terminator, so no reason to even check the port.
1849 data->host_len = *ptr - data->host;
1850 return TRUE;
1853 ++(*ptr);
1854 if(**ptr == ':') {
1855 ++(*ptr);
1856 /* If a valid port is not found, then let it trickle down to
1857 * parse_reg_name.
1859 if(!parse_port(ptr, data, flags)) {
1860 *ptr = data->host;
1861 data->host = NULL;
1862 return FALSE;
1864 } else
1865 data->host_len = *ptr - data->host;
1867 return TRUE;
1870 /* Parses the host information from the URI.
1872 * host = IP-literal / IPv4address / reg-name
1874 static BOOL parse_host(const WCHAR **ptr, parse_data *data, DWORD flags, DWORD extras) {
1875 if(!parse_ip_literal(ptr, data, flags, extras)) {
1876 if(!parse_ipv4address(ptr, data, flags)) {
1877 if(!parse_reg_name(ptr, data, flags, extras)) {
1878 TRACE("(%p %p %x %x): Malformed URI, Unknown host type.\n",
1879 ptr, data, flags, extras);
1880 return FALSE;
1885 return TRUE;
1888 /* Parses the authority information from the URI.
1890 * authority = [ userinfo "@" ] host [ ":" port ]
1892 static BOOL parse_authority(const WCHAR **ptr, parse_data *data, DWORD flags) {
1893 parse_userinfo(ptr, data, flags);
1895 /* Parsing the port will happen during one of the host parsing
1896 * routines (if the URI has a port).
1898 if(!parse_host(ptr, data, flags, 0))
1899 return FALSE;
1901 return TRUE;
1904 /* Attempts to parse the path information of a hierarchical URI. */
1905 static BOOL parse_path_hierarchical(const WCHAR **ptr, parse_data *data, DWORD flags) {
1906 const WCHAR *start = *ptr;
1907 static const WCHAR slash[] = {'/',0};
1908 const BOOL is_file = data->scheme_type == URL_SCHEME_FILE;
1910 if(is_path_delim(**ptr)) {
1911 if(data->scheme_type == URL_SCHEME_WILDCARD) {
1912 /* Wildcard schemes don't get a '/' attached if their path is
1913 * empty.
1915 data->path = NULL;
1916 data->path_len = 0;
1917 } else if(!(flags & Uri_CREATE_NO_CANONICALIZE)) {
1918 /* If the path component is empty, then a '/' is added. */
1919 data->path = slash;
1920 data->path_len = 1;
1922 } else {
1923 while(!is_path_delim(**ptr)) {
1924 if(**ptr == '%' && data->scheme_type != URL_SCHEME_UNKNOWN && !is_file) {
1925 if(!check_pct_encoded(ptr)) {
1926 *ptr = start;
1927 return FALSE;
1928 } else
1929 continue;
1930 } else if(is_forbidden_dos_path_char(**ptr) && is_file &&
1931 (flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
1932 /* File schemes with USE_DOS_PATH set aren't allowed to have
1933 * a '<' or '>' or '\"' appear in them.
1935 *ptr = start;
1936 return FALSE;
1937 } else if(**ptr == '\\') {
1938 /* Not allowed to have a backslash if NO_CANONICALIZE is set
1939 * and the scheme is known type (but not a file scheme).
1941 if(flags & Uri_CREATE_NO_CANONICALIZE) {
1942 if(data->scheme_type != URL_SCHEME_FILE &&
1943 data->scheme_type != URL_SCHEME_UNKNOWN) {
1944 *ptr = start;
1945 return FALSE;
1950 ++(*ptr);
1953 /* The only time a URI doesn't have a path is when
1954 * the NO_CANONICALIZE flag is set and the raw URI
1955 * didn't contain one.
1957 if(*ptr == start) {
1958 data->path = NULL;
1959 data->path_len = 0;
1960 } else {
1961 data->path = start;
1962 data->path_len = *ptr - start;
1966 if(data->path)
1967 TRACE("(%p %p %x): Parsed path %s len=%d\n", ptr, data, flags,
1968 debugstr_wn(data->path, data->path_len), data->path_len);
1969 else
1970 TRACE("(%p %p %x): The URI contained no path\n", ptr, data, flags);
1972 return TRUE;
1975 /* Parses the path of a opaque URI (much less strict then the parser
1976 * for a hierarchical URI).
1978 * NOTE:
1979 * Windows allows invalid % encoded data to appear in opaque URI paths
1980 * for unknown scheme types.
1982 * File schemes with USE_DOS_PATH set aren't allowed to have '<', '>', or '\"'
1983 * appear in them.
1985 static BOOL parse_path_opaque(const WCHAR **ptr, parse_data *data, DWORD flags) {
1986 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
1987 const BOOL is_file = data->scheme_type == URL_SCHEME_FILE;
1989 data->path = *ptr;
1991 while(!is_path_delim(**ptr)) {
1992 if(**ptr == '%' && known_scheme) {
1993 if(!check_pct_encoded(ptr)) {
1994 *ptr = data->path;
1995 data->path = NULL;
1996 return FALSE;
1997 } else
1998 continue;
1999 } else if(is_forbidden_dos_path_char(**ptr) && is_file &&
2000 (flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
2001 *ptr = data->path;
2002 data->path = NULL;
2003 return FALSE;
2006 ++(*ptr);
2009 data->path_len = *ptr - data->path;
2010 TRACE("(%p %p %x): Parsed opaque URI path %s len=%d\n", ptr, data, flags,
2011 debugstr_wn(data->path, data->path_len), data->path_len);
2012 return TRUE;
2015 /* Determines how the URI should be parsed after the scheme information.
2017 * If the scheme is followed, by "//" then, it is treated as an hierarchical URI
2018 * which then the authority and path information will be parsed out. Otherwise, the
2019 * URI will be treated as an opaque URI which the authority information is not parsed
2020 * out.
2022 * RFC 3896 definition of hier-part:
2024 * hier-part = "//" authority path-abempty
2025 * / path-absolute
2026 * / path-rootless
2027 * / path-empty
2029 * MSDN opaque URI definition:
2030 * scheme ":" path [ "#" fragment ]
2032 * NOTES:
2033 * If the URI is of an unknown scheme type and has a "//" following the scheme then it
2034 * is treated as a hierarchical URI, but, if the CREATE_NO_CRACK_UNKNOWN_SCHEMES flag is
2035 * set then it is considered an opaque URI reguardless of what follows the scheme information
2036 * (per MSDN documentation).
2038 static BOOL parse_hierpart(const WCHAR **ptr, parse_data *data, DWORD flags) {
2039 const WCHAR *start = *ptr;
2041 /* Checks if the authority information needs to be parsed. */
2042 if(is_hierarchical_uri(ptr, data)) {
2043 /* Only treat it as a hierarchical URI if the scheme_type is known or
2044 * the Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES flag is not set.
2046 if(data->scheme_type != URL_SCHEME_UNKNOWN ||
2047 !(flags & Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES)) {
2048 TRACE("(%p %p %x): Treating URI as an hierarchical URI.\n", ptr, data, flags);
2049 data->is_opaque = FALSE;
2051 /* TODO: Handle hierarchical URI's, parse authority then parse the path. */
2052 if(!parse_authority(ptr, data, flags))
2053 return FALSE;
2055 return parse_path_hierarchical(ptr, data, flags);
2056 } else
2057 /* Reset ptr to it's starting position so opaque path parsing
2058 * begins at the correct location.
2060 *ptr = start;
2063 /* If it reaches here, then the URI will be treated as an opaque
2064 * URI.
2067 TRACE("(%p %p %x): Treating URI as an opaque URI.\n", ptr, data, flags);
2069 data->is_opaque = TRUE;
2070 if(!parse_path_opaque(ptr, data, flags))
2071 return FALSE;
2073 return TRUE;
2076 /* Attempts to parse the query string from the URI.
2078 * NOTES:
2079 * If NO_DECODE_EXTRA_INFO flag is set, then invalid percent encoded
2080 * data is allowed appear in the query string. For unknown scheme types
2081 * invalid percent encoded data is allowed to appear reguardless.
2083 static BOOL parse_query(const WCHAR **ptr, parse_data *data, DWORD flags) {
2084 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
2086 if(**ptr != '?') {
2087 TRACE("(%p %p %x): URI didn't contain a query string.\n", ptr, data, flags);
2088 return TRUE;
2091 data->query = *ptr;
2093 ++(*ptr);
2094 while(**ptr && **ptr != '#') {
2095 if(**ptr == '%' && known_scheme &&
2096 !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
2097 if(!check_pct_encoded(ptr)) {
2098 *ptr = data->query;
2099 data->query = NULL;
2100 return FALSE;
2101 } else
2102 continue;
2105 ++(*ptr);
2108 data->query_len = *ptr - data->query;
2110 TRACE("(%p %p %x): Parsed query string %s len=%d\n", ptr, data, flags,
2111 debugstr_wn(data->query, data->query_len), data->query_len);
2112 return TRUE;
2115 /* Attempts to parse the fragment 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_fragment(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 fragment.\n", ptr, data, flags);
2127 return TRUE;
2130 data->fragment = *ptr;
2132 ++(*ptr);
2133 while(**ptr) {
2134 if(**ptr == '%' && known_scheme &&
2135 !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
2136 if(!check_pct_encoded(ptr)) {
2137 *ptr = data->fragment;
2138 data->fragment = NULL;
2139 return FALSE;
2140 } else
2141 continue;
2144 ++(*ptr);
2147 data->fragment_len = *ptr - data->fragment;
2149 TRACE("(%p %p %x): Parsed fragment %s len=%d\n", ptr, data, flags,
2150 debugstr_wn(data->fragment, data->fragment_len), data->fragment_len);
2151 return TRUE;
2154 /* Parses and validates the components of the specified by data->uri
2155 * and stores the information it parses into 'data'.
2157 * Returns TRUE if it successfully parsed the URI. False otherwise.
2159 static BOOL parse_uri(parse_data *data, DWORD flags) {
2160 const WCHAR *ptr;
2161 const WCHAR **pptr;
2163 ptr = data->uri;
2164 pptr = &ptr;
2166 TRACE("(%p %x): BEGINNING TO PARSE URI %s.\n", data, flags, debugstr_w(data->uri));
2168 if(!parse_scheme(pptr, data, flags, 0))
2169 return FALSE;
2171 if(!parse_hierpart(pptr, data, flags))
2172 return FALSE;
2174 if(!parse_query(pptr, data, flags))
2175 return FALSE;
2177 if(!parse_fragment(pptr, data, flags))
2178 return FALSE;
2180 TRACE("(%p %x): FINISHED PARSING URI.\n", data, flags);
2181 return TRUE;
2184 static BOOL canonicalize_username(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2185 const WCHAR *ptr;
2187 if(!data->username) {
2188 uri->userinfo_start = -1;
2189 return TRUE;
2192 uri->userinfo_start = uri->canon_len;
2193 for(ptr = data->username; ptr < data->username+data->username_len; ++ptr) {
2194 if(*ptr == '%') {
2195 /* Only decode % encoded values for known scheme types. */
2196 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
2197 /* See if the value really needs decoded. */
2198 WCHAR val = decode_pct_val(ptr);
2199 if(is_unreserved(val)) {
2200 if(!computeOnly)
2201 uri->canon_uri[uri->canon_len] = val;
2203 ++uri->canon_len;
2205 /* Move pass the hex characters. */
2206 ptr += 2;
2207 continue;
2210 } else if(!is_reserved(*ptr) && !is_unreserved(*ptr) && *ptr != '\\') {
2211 /* Only percent encode forbidden characters if the NO_ENCODE_FORBIDDEN_CHARACTERS flag
2212 * is NOT set.
2214 if(!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS)) {
2215 if(!computeOnly)
2216 pct_encode_val(*ptr, uri->canon_uri + uri->canon_len);
2218 uri->canon_len += 3;
2219 continue;
2223 if(!computeOnly)
2224 /* Nothing special, so just copy the character over. */
2225 uri->canon_uri[uri->canon_len] = *ptr;
2226 ++uri->canon_len;
2229 return TRUE;
2232 static BOOL canonicalize_password(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2233 const WCHAR *ptr;
2235 if(!data->password) {
2236 uri->userinfo_split = -1;
2237 return TRUE;
2240 if(uri->userinfo_start == -1)
2241 /* Has a password, but, doesn't have a username. */
2242 uri->userinfo_start = uri->canon_len;
2244 uri->userinfo_split = uri->canon_len - uri->userinfo_start;
2246 /* Add the ':' to the userinfo component. */
2247 if(!computeOnly)
2248 uri->canon_uri[uri->canon_len] = ':';
2249 ++uri->canon_len;
2251 for(ptr = data->password; ptr < data->password+data->password_len; ++ptr) {
2252 if(*ptr == '%') {
2253 /* Only decode % encoded values for known scheme types. */
2254 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
2255 /* See if the value really needs decoded. */
2256 WCHAR val = decode_pct_val(ptr);
2257 if(is_unreserved(val)) {
2258 if(!computeOnly)
2259 uri->canon_uri[uri->canon_len] = val;
2261 ++uri->canon_len;
2263 /* Move pass the hex characters. */
2264 ptr += 2;
2265 continue;
2268 } else if(!is_reserved(*ptr) && !is_unreserved(*ptr) && *ptr != '\\') {
2269 /* Only percent encode forbidden characters if the NO_ENCODE_FORBIDDEN_CHARACTERS flag
2270 * is NOT set.
2272 if(!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS)) {
2273 if(!computeOnly)
2274 pct_encode_val(*ptr, uri->canon_uri + uri->canon_len);
2276 uri->canon_len += 3;
2277 continue;
2281 if(!computeOnly)
2282 /* Nothing special, so just copy the character over. */
2283 uri->canon_uri[uri->canon_len] = *ptr;
2284 ++uri->canon_len;
2287 return TRUE;
2290 /* Canonicalizes the userinfo of the URI represented by the parse_data.
2292 * Canonicalization of the userinfo is a simple process. If there are any percent
2293 * encoded characters that fall in the "unreserved" character set, they are decoded
2294 * to their actual value. If a character is not in the "unreserved" or "reserved" sets
2295 * then it is percent encoded. Other than that the characters are copied over without
2296 * change.
2298 static BOOL canonicalize_userinfo(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2299 uri->userinfo_start = uri->userinfo_split = -1;
2300 uri->userinfo_len = 0;
2302 if(!data->username && !data->password)
2303 /* URI doesn't have userinfo, so nothing to do here. */
2304 return TRUE;
2306 if(!canonicalize_username(data, uri, flags, computeOnly))
2307 return FALSE;
2309 if(!canonicalize_password(data, uri, flags, computeOnly))
2310 return FALSE;
2312 uri->userinfo_len = uri->canon_len - uri->userinfo_start;
2313 if(!computeOnly)
2314 TRACE("(%p %p %x %d): Canonicalized userinfo, userinfo_start=%d, userinfo=%s, userinfo_split=%d userinfo_len=%d.\n",
2315 data, uri, flags, computeOnly, uri->userinfo_start, debugstr_wn(uri->canon_uri + uri->userinfo_start, uri->userinfo_len),
2316 uri->userinfo_split, uri->userinfo_len);
2318 /* Now insert the '@' after the userinfo. */
2319 if(!computeOnly)
2320 uri->canon_uri[uri->canon_len] = '@';
2321 ++uri->canon_len;
2323 return TRUE;
2326 /* Attempts to canonicalize a reg_name.
2328 * Things that happen:
2329 * 1) If Uri_CREATE_NO_CANONICALIZE flag is not set, then the reg_name is
2330 * lower cased. Unless it's an unknown scheme type, which case it's
2331 * no lower cased reguardless.
2333 * 2) Unreserved % encoded characters are decoded for known
2334 * scheme types.
2336 * 3) Forbidden characters are % encoded as long as
2337 * Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS flag is not set and
2338 * it isn't an unknown scheme type.
2340 * 4) If it's a file scheme and the host is "localhost" it's removed.
2342 static BOOL canonicalize_reg_name(const parse_data *data, Uri *uri,
2343 DWORD flags, BOOL computeOnly) {
2344 static const WCHAR localhostW[] =
2345 {'l','o','c','a','l','h','o','s','t',0};
2346 const WCHAR *ptr;
2347 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
2349 uri->host_start = uri->canon_len;
2351 if(data->scheme_type == URL_SCHEME_FILE &&
2352 data->host_len == lstrlenW(localhostW)) {
2353 if(!StrCmpNIW(data->host, localhostW, data->host_len)) {
2354 uri->host_start = -1;
2355 uri->host_len = 0;
2356 uri->host_type = Uri_HOST_UNKNOWN;
2357 return TRUE;
2361 for(ptr = data->host; ptr < data->host+data->host_len; ++ptr) {
2362 if(*ptr == '%' && known_scheme) {
2363 WCHAR val = decode_pct_val(ptr);
2364 if(is_unreserved(val)) {
2365 /* If NO_CANONICALZE is not set, then windows lower cases the
2366 * decoded value.
2368 if(!(flags & Uri_CREATE_NO_CANONICALIZE) && isupperW(val)) {
2369 if(!computeOnly)
2370 uri->canon_uri[uri->canon_len] = tolowerW(val);
2371 } else {
2372 if(!computeOnly)
2373 uri->canon_uri[uri->canon_len] = val;
2375 ++uri->canon_len;
2377 /* Skip past the % encoded character. */
2378 ptr += 2;
2379 continue;
2380 } else {
2381 /* Just copy the % over. */
2382 if(!computeOnly)
2383 uri->canon_uri[uri->canon_len] = *ptr;
2384 ++uri->canon_len;
2386 } else if(*ptr == '\\') {
2387 /* Only unknown scheme types could have made it here with a '\\' in the host name. */
2388 if(!computeOnly)
2389 uri->canon_uri[uri->canon_len] = *ptr;
2390 ++uri->canon_len;
2391 } else if(!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS) &&
2392 !is_unreserved(*ptr) && !is_reserved(*ptr) && known_scheme) {
2393 if(!computeOnly) {
2394 pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
2396 /* The percent encoded value gets lower cased also. */
2397 if(!(flags & Uri_CREATE_NO_CANONICALIZE)) {
2398 uri->canon_uri[uri->canon_len+1] = tolowerW(uri->canon_uri[uri->canon_len+1]);
2399 uri->canon_uri[uri->canon_len+2] = tolowerW(uri->canon_uri[uri->canon_len+2]);
2403 uri->canon_len += 3;
2404 } else {
2405 if(!computeOnly) {
2406 if(!(flags & Uri_CREATE_NO_CANONICALIZE) && known_scheme)
2407 uri->canon_uri[uri->canon_len] = tolowerW(*ptr);
2408 else
2409 uri->canon_uri[uri->canon_len] = *ptr;
2412 ++uri->canon_len;
2416 uri->host_len = uri->canon_len - uri->host_start;
2418 if(!computeOnly)
2419 TRACE("(%p %p %x %d): Canonicalize reg_name=%s len=%d\n", data, uri, flags,
2420 computeOnly, debugstr_wn(uri->canon_uri+uri->host_start, uri->host_len),
2421 uri->host_len);
2423 if(!computeOnly)
2424 find_domain_name(uri->canon_uri+uri->host_start, uri->host_len,
2425 &(uri->domain_offset));
2427 return TRUE;
2430 /* Attempts to canonicalize an implicit IPv4 address. */
2431 static BOOL canonicalize_implicit_ipv4address(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2432 uri->host_start = uri->canon_len;
2434 TRACE("%u\n", data->implicit_ipv4);
2435 /* For unknown scheme types Window's doesn't convert
2436 * the value into an IP address, but, it still considers
2437 * it an IPv4 address.
2439 if(data->scheme_type == URL_SCHEME_UNKNOWN) {
2440 if(!computeOnly)
2441 memcpy(uri->canon_uri+uri->canon_len, data->host, data->host_len*sizeof(WCHAR));
2442 uri->canon_len += data->host_len;
2443 } else {
2444 if(!computeOnly)
2445 uri->canon_len += ui2ipv4(uri->canon_uri+uri->canon_len, data->implicit_ipv4);
2446 else
2447 uri->canon_len += ui2ipv4(NULL, data->implicit_ipv4);
2450 uri->host_len = uri->canon_len - uri->host_start;
2451 uri->host_type = Uri_HOST_IPV4;
2453 if(!computeOnly)
2454 TRACE("%p %p %x %d): Canonicalized implicit IP address=%s len=%d\n",
2455 data, uri, flags, computeOnly,
2456 debugstr_wn(uri->canon_uri+uri->host_start, uri->host_len),
2457 uri->host_len);
2459 return TRUE;
2462 /* Attempts to canonicalize an IPv4 address.
2464 * If the parse_data represents a URI that has an implicit IPv4 address
2465 * (ex. http://256/, this function will convert 256 into 0.0.1.0). If
2466 * the implicit IP address exceeds the value of UINT_MAX (maximum value
2467 * for an IPv4 address) it's canonicalized as if were a reg-name.
2469 * If the parse_data contains a partial or full IPv4 address it normalizes it.
2470 * A partial IPv4 address is something like "192.0" and would be normalized to
2471 * "192.0.0.0". With a full (or partial) IPv4 address like "192.002.01.003" would
2472 * be normalized to "192.2.1.3".
2474 * NOTES:
2475 * Window's ONLY normalizes IPv4 address for known scheme types (one that isn't
2476 * URL_SCHEME_UNKNOWN). For unknown scheme types, it simply copies the data from
2477 * the original URI into the canonicalized URI, but, it still recognizes URI's
2478 * host type as HOST_IPV4.
2480 static BOOL canonicalize_ipv4address(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2481 if(data->has_implicit_ip)
2482 return canonicalize_implicit_ipv4address(data, uri, flags, computeOnly);
2483 else {
2484 uri->host_start = uri->canon_len;
2486 /* Windows only normalizes for known scheme types. */
2487 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
2488 /* parse_data contains a partial or full IPv4 address, so normalize it. */
2489 DWORD i, octetDigitCount = 0, octetCount = 0;
2490 BOOL octetHasDigit = FALSE;
2492 for(i = 0; i < data->host_len; ++i) {
2493 if(data->host[i] == '0' && !octetHasDigit) {
2494 /* Can ignore leading zeros if:
2495 * 1) It isn't the last digit of the octet.
2496 * 2) i+1 != data->host_len
2497 * 3) i+1 != '.'
2499 if(octetDigitCount == 2 ||
2500 i+1 == data->host_len ||
2501 data->host[i+1] == '.') {
2502 if(!computeOnly)
2503 uri->canon_uri[uri->canon_len] = data->host[i];
2504 ++uri->canon_len;
2505 TRACE("Adding zero\n");
2507 } else if(data->host[i] == '.') {
2508 if(!computeOnly)
2509 uri->canon_uri[uri->canon_len] = data->host[i];
2510 ++uri->canon_len;
2512 octetDigitCount = 0;
2513 octetHasDigit = FALSE;
2514 ++octetCount;
2515 } else {
2516 if(!computeOnly)
2517 uri->canon_uri[uri->canon_len] = data->host[i];
2518 ++uri->canon_len;
2520 ++octetDigitCount;
2521 octetHasDigit = TRUE;
2525 /* Make sure the canonicalized IP address has 4 dec-octets.
2526 * If doesn't add "0" ones until there is 4;
2528 for( ; octetCount < 3; ++octetCount) {
2529 if(!computeOnly) {
2530 uri->canon_uri[uri->canon_len] = '.';
2531 uri->canon_uri[uri->canon_len+1] = '0';
2534 uri->canon_len += 2;
2536 } else {
2537 /* Windows doesn't normalize addresses in unknown schemes. */
2538 if(!computeOnly)
2539 memcpy(uri->canon_uri+uri->canon_len, data->host, data->host_len*sizeof(WCHAR));
2540 uri->canon_len += data->host_len;
2543 uri->host_len = uri->canon_len - uri->host_start;
2544 if(!computeOnly)
2545 TRACE("(%p %p %x %d): Canonicalized IPv4 address, ip=%s len=%d\n",
2546 data, uri, flags, computeOnly,
2547 debugstr_wn(uri->canon_uri+uri->host_start, uri->host_len),
2548 uri->host_len);
2551 return TRUE;
2554 /* Attempts to canonicalize the IPv6 address of the URI.
2556 * Multiple things happen during the canonicalization of an IPv6 address:
2557 * 1) Any leading zero's in an h16 component are removed.
2558 * Ex: [0001:0022::] -> [1:22::]
2560 * 2) The longest sequence of zero h16 components are compressed
2561 * into a "::" (elision). If there's a tie, the first is choosen.
2563 * Ex: [0:0:0:0:1:6:7:8] -> [::1:6:7:8]
2564 * [0:0:0:0:1:2::] -> [::1:2:0:0]
2565 * [0:0:1:2:0:0:7:8] -> [::1:2:0:0:7:8]
2567 * 3) If an IPv4 address is attached to the IPv6 address, it's
2568 * also normalized.
2569 * Ex: [::001.002.022.000] -> [::1.2.22.0]
2571 * 4) If an elision is present, but, only represents 1 h16 component
2572 * it's expanded.
2574 * Ex: [1::2:3:4:5:6:7] -> [1:0:2:3:4:5:6:7]
2576 * 5) If the IPv6 address contains an IPv4 address and there exists
2577 * at least 1 non-zero h16 component the IPv4 address is converted
2578 * into two h16 components, otherwise it's normalized and kept as is.
2580 * Ex: [::192.200.003.4] -> [::192.200.3.4]
2581 * [ffff::192.200.003.4] -> [ffff::c0c8:3041]
2583 * NOTE:
2584 * For unknown scheme types Windows simply copies the address over without any
2585 * changes.
2587 * IPv4 address can be included in an elision if all its components are 0's.
2589 static BOOL canonicalize_ipv6address(const parse_data *data, Uri *uri,
2590 DWORD flags, BOOL computeOnly) {
2591 uri->host_start = uri->canon_len;
2593 if(data->scheme_type == URL_SCHEME_UNKNOWN) {
2594 if(!computeOnly)
2595 memcpy(uri->canon_uri+uri->canon_len, data->host, data->host_len*sizeof(WCHAR));
2596 uri->canon_len += data->host_len;
2597 } else {
2598 USHORT values[8];
2599 INT elision_start;
2600 DWORD i, elision_len;
2602 if(!ipv6_to_number(&(data->ipv6_address), values)) {
2603 TRACE("(%p %p %x %d): Failed to compute numerical value for IPv6 address.\n",
2604 data, uri, flags, computeOnly);
2605 return FALSE;
2608 if(!computeOnly)
2609 uri->canon_uri[uri->canon_len] = '[';
2610 ++uri->canon_len;
2612 /* Find where the elision should occur (if any). */
2613 compute_elision_location(&(data->ipv6_address), values, &elision_start, &elision_len);
2615 TRACE("%p %p %x %d): Elision starts at %d, len=%u\n", data, uri, flags,
2616 computeOnly, elision_start, elision_len);
2618 for(i = 0; i < 8; ++i) {
2619 BOOL in_elision = (elision_start > -1 && i >= elision_start &&
2620 i < elision_start+elision_len);
2621 BOOL do_ipv4 = (i == 6 && data->ipv6_address.ipv4 && !in_elision &&
2622 data->ipv6_address.h16_count == 0);
2624 if(i == elision_start) {
2625 if(!computeOnly) {
2626 uri->canon_uri[uri->canon_len] = ':';
2627 uri->canon_uri[uri->canon_len+1] = ':';
2629 uri->canon_len += 2;
2632 /* We can ignore the current component if we're in the elision. */
2633 if(in_elision)
2634 continue;
2636 /* We only add a ':' if we're not at i == 0, or when we're at
2637 * the very end of elision range since the ':' colon was handled
2638 * earlier. Otherwise we would end up with ":::" after elision.
2640 if(i != 0 && !(elision_start > -1 && i == elision_start+elision_len)) {
2641 if(!computeOnly)
2642 uri->canon_uri[uri->canon_len] = ':';
2643 ++uri->canon_len;
2646 if(do_ipv4) {
2647 UINT val;
2648 DWORD len;
2650 /* Combine the two parts of the IPv4 address values. */
2651 val = values[i];
2652 val <<= 16;
2653 val += values[i+1];
2655 if(!computeOnly)
2656 len = ui2ipv4(uri->canon_uri+uri->canon_len, val);
2657 else
2658 len = ui2ipv4(NULL, val);
2660 uri->canon_len += len;
2661 ++i;
2662 } else {
2663 /* Write a regular h16 component to the URI. */
2665 /* Short circuit for the trivial case. */
2666 if(values[i] == 0) {
2667 if(!computeOnly)
2668 uri->canon_uri[uri->canon_len] = '0';
2669 ++uri->canon_len;
2670 } else {
2671 static const WCHAR formatW[] = {'%','x',0};
2673 if(!computeOnly)
2674 uri->canon_len += sprintfW(uri->canon_uri+uri->canon_len,
2675 formatW, values[i]);
2676 else {
2677 WCHAR tmp[5];
2678 uri->canon_len += sprintfW(tmp, formatW, values[i]);
2684 /* Add the closing ']'. */
2685 if(!computeOnly)
2686 uri->canon_uri[uri->canon_len] = ']';
2687 ++uri->canon_len;
2690 uri->host_len = uri->canon_len - uri->host_start;
2692 if(!computeOnly)
2693 TRACE("(%p %p %x %d): Canonicalized IPv6 address %s, len=%d\n", data, uri, flags,
2694 computeOnly, debugstr_wn(uri->canon_uri+uri->host_start, uri->host_len),
2695 uri->host_len);
2697 return TRUE;
2700 /* Attempts to canonicalize the host of the URI (if any). */
2701 static BOOL canonicalize_host(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2702 uri->host_start = -1;
2703 uri->host_len = 0;
2704 uri->domain_offset = -1;
2706 if(data->host) {
2707 switch(data->host_type) {
2708 case Uri_HOST_DNS:
2709 uri->host_type = Uri_HOST_DNS;
2710 if(!canonicalize_reg_name(data, uri, flags, computeOnly))
2711 return FALSE;
2713 break;
2714 case Uri_HOST_IPV4:
2715 uri->host_type = Uri_HOST_IPV4;
2716 if(!canonicalize_ipv4address(data, uri, flags, computeOnly))
2717 return FALSE;
2719 break;
2720 case Uri_HOST_IPV6:
2721 if(!canonicalize_ipv6address(data, uri, flags, computeOnly))
2722 return FALSE;
2724 uri->host_type = Uri_HOST_IPV6;
2725 break;
2726 case Uri_HOST_UNKNOWN:
2727 if(data->host_len > 0 || data->scheme_type != URL_SCHEME_FILE) {
2728 uri->host_start = uri->canon_len;
2730 /* Nothing happens to unknown host types. */
2731 if(!computeOnly)
2732 memcpy(uri->canon_uri+uri->canon_len, data->host, data->host_len*sizeof(WCHAR));
2733 uri->canon_len += data->host_len;
2734 uri->host_len = data->host_len;
2737 uri->host_type = Uri_HOST_UNKNOWN;
2738 break;
2739 default:
2740 FIXME("(%p %p %x %d): Canonicalization for host type %d not supported.\n", data,
2741 uri, flags, computeOnly, data->host_type);
2742 return FALSE;
2746 return TRUE;
2749 static BOOL canonicalize_port(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2750 BOOL has_default_port = FALSE;
2751 USHORT default_port = 0;
2752 DWORD i;
2754 uri->has_port = FALSE;
2756 /* Check if the scheme has a default port. */
2757 for(i = 0; i < sizeof(default_ports)/sizeof(default_ports[0]); ++i) {
2758 if(default_ports[i].scheme == data->scheme_type) {
2759 has_default_port = TRUE;
2760 default_port = default_ports[i].port;
2761 break;
2765 if(data->port || has_default_port)
2766 uri->has_port = TRUE;
2768 /* Possible cases:
2769 * 1) Has a port which is the default port.
2770 * 2) Has a port (not the default).
2771 * 3) Doesn't have a port, but, scheme has a default port.
2772 * 4) No port.
2774 if(has_default_port && data->port && data->port_value == default_port) {
2775 /* If it's the default port and this flag isn't set, don't do anything. */
2776 if(flags & Uri_CREATE_NO_CANONICALIZE) {
2777 /* Copy the original port over. */
2778 if(!computeOnly) {
2779 uri->canon_uri[uri->canon_len] = ':';
2780 memcpy(uri->canon_uri+uri->canon_len+1, data->port, data->port_len*sizeof(WCHAR));
2782 uri->canon_len += data->port_len+1;
2785 uri->port = default_port;
2786 } else if(data->port) {
2787 if(!computeOnly)
2788 uri->canon_uri[uri->canon_len] = ':';
2789 ++uri->canon_len;
2791 if(flags & Uri_CREATE_NO_CANONICALIZE) {
2792 /* Copy the original over without changes. */
2793 if(!computeOnly)
2794 memcpy(uri->canon_uri+uri->canon_len, data->port, data->port_len*sizeof(WCHAR));
2795 uri->canon_len += data->port_len;
2796 } else {
2797 const WCHAR formatW[] = {'%','u',0};
2798 INT len = 0;
2799 if(!computeOnly)
2800 len = sprintfW(uri->canon_uri+uri->canon_len, formatW, data->port_value);
2801 else {
2802 WCHAR tmp[6];
2803 len = sprintfW(tmp, formatW, data->port_value);
2805 uri->canon_len += len;
2808 uri->port = data->port_value;
2809 } else if(has_default_port)
2810 uri->port = default_port;
2812 return TRUE;
2815 /* Canonicalizes the authority of the URI represented by the parse_data. */
2816 static BOOL canonicalize_authority(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2817 uri->authority_start = uri->canon_len;
2818 uri->authority_len = 0;
2820 if(!canonicalize_userinfo(data, uri, flags, computeOnly))
2821 return FALSE;
2823 if(!canonicalize_host(data, uri, flags, computeOnly))
2824 return FALSE;
2826 if(!canonicalize_port(data, uri, flags, computeOnly))
2827 return FALSE;
2829 if(uri->host_start != -1)
2830 uri->authority_len = uri->canon_len - uri->authority_start;
2831 else
2832 uri->authority_start = -1;
2834 return TRUE;
2837 /* Attempts to canonicalize the path of a hierarchical URI.
2839 * Things that happen:
2840 * 1). Forbidden characters are percent encoded, unless the NO_ENCODE_FORBIDDEN
2841 * flag is set or it's a file URI. Forbidden characters are always encoded
2842 * for file schemes reguardless and forbidden characters are never encoded
2843 * for unknown scheme types.
2845 * 2). For known scheme types '\\' are changed to '/'.
2847 * 3). Percent encoded, unreserved characters are decoded to their actual values.
2848 * Unless the scheme type is unknown. For file schemes any percent encoded
2849 * character in the unreserved or reserved set is decoded.
2851 * 4). For File schemes if the path is starts with a drive letter and doesn't
2852 * start with a '/' then one is appended.
2853 * Ex: file://c:/test.mp3 -> file:///c:/test.mp3
2855 * 5). Dot segments are removed from the path for all scheme types
2856 * unless NO_CANONICALIZE flag is set. Dot segments aren't removed
2857 * for wildcard scheme types.
2859 * NOTES:
2860 * file://c:/test%20test -> file:///c:/test%2520test
2861 * file://c:/test%3Etest -> file:///c:/test%253Etest
2862 * file:///c:/test%20test -> file:///c:/test%20test
2863 * file:///c:/test%test -> file:///c:/test%25test
2865 static BOOL canonicalize_path_hierarchical(const parse_data *data, Uri *uri,
2866 DWORD flags, BOOL computeOnly) {
2867 const WCHAR *ptr;
2868 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
2869 const BOOL is_file = data->scheme_type == URL_SCHEME_FILE;
2871 BOOL escape_pct = FALSE;
2873 if(!data->path) {
2874 uri->path_start = -1;
2875 uri->path_len = 0;
2876 return TRUE;
2879 uri->path_start = uri->canon_len;
2880 ptr = data->path;
2882 if(is_file && uri->host_start == -1) {
2883 /* Check if a '/' needs to be appended for the file scheme. */
2884 if(data->path_len > 1 && is_drive_path(ptr) && !(flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
2885 if(!computeOnly)
2886 uri->canon_uri[uri->canon_len] = '/';
2887 uri->canon_len++;
2888 escape_pct = TRUE;
2889 } else if(*ptr == '/') {
2890 if(!(flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
2891 /* Copy the extra '/' over. */
2892 if(!computeOnly)
2893 uri->canon_uri[uri->canon_len] = '/';
2894 ++uri->canon_len;
2896 ++ptr;
2899 if(is_drive_path(ptr)) {
2900 if(!computeOnly) {
2901 uri->canon_uri[uri->canon_len] = *ptr;
2902 /* If theres a '|' after the drive letter, convert it to a ':'. */
2903 uri->canon_uri[uri->canon_len+1] = ':';
2905 ptr += 2;
2906 uri->canon_len += 2;
2910 for(; ptr < data->path+data->path_len; ++ptr) {
2911 if(*ptr == '%') {
2912 const WCHAR *tmp = ptr;
2913 WCHAR val;
2915 /* Check if the % represents a valid encoded char, or if it needs encoded. */
2916 BOOL force_encode = !check_pct_encoded(&tmp) && is_file;
2917 val = decode_pct_val(ptr);
2919 if(force_encode || escape_pct) {
2920 /* Escape the percent sign in the file URI. */
2921 if(!computeOnly)
2922 pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
2923 uri->canon_len += 3;
2924 } else if((is_unreserved(val) && known_scheme) ||
2925 (is_file && (is_unreserved(val) || is_reserved(val)))) {
2926 if(!computeOnly)
2927 uri->canon_uri[uri->canon_len] = val;
2928 ++uri->canon_len;
2930 ptr += 2;
2931 continue;
2932 } else {
2933 if(!computeOnly)
2934 uri->canon_uri[uri->canon_len] = *ptr;
2935 ++uri->canon_len;
2937 } else if(*ptr == '/' && is_file && (flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
2938 /* Convert the '/' back to a '\\'. */
2939 if(!computeOnly)
2940 uri->canon_uri[uri->canon_len] = '\\';
2941 ++uri->canon_len;
2942 } else if(*ptr == '\\' && known_scheme) {
2943 if(is_file && (flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
2944 /* Don't convert the '\\' to a '/'. */
2945 if(!computeOnly)
2946 uri->canon_uri[uri->canon_len] = *ptr;
2947 ++uri->canon_len;
2948 } else {
2949 if(!computeOnly)
2950 uri->canon_uri[uri->canon_len] = '/';
2951 ++uri->canon_len;
2953 } else if(known_scheme && !is_unreserved(*ptr) && !is_reserved(*ptr) &&
2954 (!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS) || is_file)) {
2955 if(is_file && (flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
2956 /* Don't escape the character. */
2957 if(!computeOnly)
2958 uri->canon_uri[uri->canon_len] = *ptr;
2959 ++uri->canon_len;
2960 } else {
2961 /* Escape the forbidden character. */
2962 if(!computeOnly)
2963 pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
2964 uri->canon_len += 3;
2966 } else {
2967 if(!computeOnly)
2968 uri->canon_uri[uri->canon_len] = *ptr;
2969 ++uri->canon_len;
2973 uri->path_len = uri->canon_len - uri->path_start;
2975 /* Removing the dot segments only happens when it's not in
2976 * computeOnly mode and it's not a wildcard scheme. File schemes
2977 * with USE_DOS_PATH set don't get dot segments removed.
2979 if(!(is_file && (flags & Uri_CREATE_FILE_USE_DOS_PATH)) &&
2980 data->scheme_type != URL_SCHEME_WILDCARD) {
2981 if(!(flags & Uri_CREATE_NO_CANONICALIZE) && !computeOnly) {
2982 /* Remove the dot segments (if any) and reset everything to the new
2983 * correct length.
2985 DWORD new_len = remove_dot_segments(uri->canon_uri+uri->path_start, uri->path_len);
2986 uri->canon_len -= uri->path_len-new_len;
2987 uri->path_len = new_len;
2991 if(!computeOnly)
2992 TRACE("Canonicalized path %s len=%d\n",
2993 debugstr_wn(uri->canon_uri+uri->path_start, uri->path_len),
2994 uri->path_len);
2996 return TRUE;
2999 /* Attempts to canonicalize the path for an opaque URI.
3001 * For known scheme types:
3002 * 1) forbidden characters are percent encoded if
3003 * NO_ENCODE_FORBIDDEN_CHARACTERS isn't set.
3005 * 2) Percent encoded, unreserved characters are decoded
3006 * to their actual values, for known scheme types.
3008 * 3) '\\' are changed to '/' for known scheme types
3009 * except for mailto schemes.
3011 * 4) For file schemes, if USE_DOS_PATH is set all '/'
3012 * are converted to backslashes.
3014 * 5) For file schemes, if USE_DOS_PATH isn't set all '\'
3015 * are converted to forward slashes.
3017 static BOOL canonicalize_path_opaque(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
3018 const WCHAR *ptr;
3019 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
3020 const BOOL is_file = data->scheme_type == URL_SCHEME_FILE;
3022 if(!data->path) {
3023 uri->path_start = -1;
3024 uri->path_len = 0;
3025 return TRUE;
3028 uri->path_start = uri->canon_len;
3030 /* Windows doesn't allow a "//" to appear after the scheme
3031 * of a URI, if it's an opaque URI.
3033 if(data->scheme && *(data->path) == '/' && *(data->path+1) == '/') {
3034 /* So it inserts a "/." before the "//" if it exists. */
3035 if(!computeOnly) {
3036 uri->canon_uri[uri->canon_len] = '/';
3037 uri->canon_uri[uri->canon_len+1] = '.';
3040 uri->canon_len += 2;
3043 for(ptr = data->path; ptr < data->path+data->path_len; ++ptr) {
3044 if(*ptr == '%' && known_scheme) {
3045 WCHAR val = decode_pct_val(ptr);
3047 if(is_unreserved(val)) {
3048 if(!computeOnly)
3049 uri->canon_uri[uri->canon_len] = val;
3050 ++uri->canon_len;
3052 ptr += 2;
3053 continue;
3054 } else {
3055 if(!computeOnly)
3056 uri->canon_uri[uri->canon_len] = *ptr;
3057 ++uri->canon_len;
3059 } else if(*ptr == '/' && is_file && (flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
3060 if(!computeOnly)
3061 uri->canon_uri[uri->canon_len] = '\\';
3062 ++uri->canon_len;
3063 } else if(*ptr == '\\' && is_file) {
3064 if(!(flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
3065 /* Convert to a '/'. */
3066 if(!computeOnly)
3067 uri->canon_uri[uri->canon_len] = '/';
3068 ++uri->canon_len;
3069 } else {
3070 /* Just copy it over. */
3071 if(!computeOnly)
3072 uri->canon_uri[uri->canon_len] = *ptr;
3073 ++uri->canon_len;
3075 } else if(known_scheme && !is_unreserved(*ptr) && !is_reserved(*ptr) &&
3076 !(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS)) {
3077 if(is_file && (flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
3078 /* Forbidden characters aren't percent encoded for file schemes
3079 * with USE_DOS_PATH set.
3081 if(!computeOnly)
3082 uri->canon_uri[uri->canon_len] = *ptr;
3083 ++uri->canon_len;
3084 } else {
3085 if(!computeOnly)
3086 pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
3087 uri->canon_len += 3;
3089 } else {
3090 if(!computeOnly)
3091 uri->canon_uri[uri->canon_len] = *ptr;
3092 ++uri->canon_len;
3096 uri->path_len = uri->canon_len - uri->path_start;
3098 TRACE("(%p %p %x %d): Canonicalized opaque URI path %s len=%d\n", data, uri, flags, computeOnly,
3099 debugstr_wn(uri->canon_uri+uri->path_start, uri->path_len), uri->path_len);
3100 return TRUE;
3103 /* Determines how the URI represented by the parse_data should be canonicalized.
3105 * Essentially, if the parse_data represents an hierarchical URI then it calls
3106 * canonicalize_authority and the canonicalization functions for the path. If the
3107 * URI is opaque it canonicalizes the path of the URI.
3109 static BOOL canonicalize_hierpart(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
3110 uri->display_absolute = TRUE;
3112 if(!data->is_opaque) {
3113 /* "//" is only added for non-wildcard scheme types. */
3114 if(data->scheme_type != URL_SCHEME_WILDCARD) {
3115 if(!computeOnly) {
3116 INT pos = uri->canon_len;
3118 uri->canon_uri[pos] = '/';
3119 uri->canon_uri[pos+1] = '/';
3121 uri->canon_len += 2;
3124 if(!canonicalize_authority(data, uri, flags, computeOnly))
3125 return FALSE;
3127 /* TODO: Canonicalize the path of the URI. */
3128 if(!canonicalize_path_hierarchical(data, uri, flags, computeOnly))
3129 return FALSE;
3131 } else {
3132 /* Opaque URI's don't have an authority. */
3133 uri->userinfo_start = uri->userinfo_split = -1;
3134 uri->userinfo_len = 0;
3135 uri->host_start = -1;
3136 uri->host_len = 0;
3137 uri->host_type = Uri_HOST_UNKNOWN;
3138 uri->has_port = FALSE;
3139 uri->authority_start = -1;
3140 uri->authority_len = 0;
3141 uri->domain_offset = -1;
3143 if(is_hierarchical_scheme(data->scheme_type)) {
3144 DWORD i;
3146 /* Absolute URIs aren't displayed for known scheme types
3147 * which should be hierarchical URIs.
3149 uri->display_absolute = FALSE;
3151 /* Windows also sets the port for these (if they have one). */
3152 for(i = 0; i < sizeof(default_ports)/sizeof(default_ports[0]); ++i) {
3153 if(data->scheme_type == default_ports[i].scheme) {
3154 uri->has_port = TRUE;
3155 uri->port = default_ports[i].port;
3156 break;
3161 if(!canonicalize_path_opaque(data, uri, flags, computeOnly))
3162 return FALSE;
3165 if(uri->path_start > -1 && !computeOnly)
3166 /* Finding file extensions happens for both types of URIs. */
3167 uri->extension_offset = find_file_extension(uri->canon_uri+uri->path_start, uri->path_len);
3168 else
3169 uri->extension_offset = -1;
3171 return TRUE;
3174 /* Attempts to canonicalize the query string of the URI.
3176 * Things that happen:
3177 * 1) For known scheme types forbidden characters
3178 * are percent encoded, unless the NO_DECODE_EXTRA_INFO flag is set
3179 * or NO_ENCODE_FORBIDDEN_CHARACTERS is set.
3181 * 2) For known scheme types, percent encoded, unreserved characters
3182 * are decoded as long as the NO_DECODE_EXTRA_INFO flag isn't set.
3184 static BOOL canonicalize_query(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
3185 const WCHAR *ptr, *end;
3186 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
3188 if(!data->query) {
3189 uri->query_start = -1;
3190 uri->query_len = 0;
3191 return TRUE;
3194 uri->query_start = uri->canon_len;
3196 end = data->query+data->query_len;
3197 for(ptr = data->query; ptr < end; ++ptr) {
3198 if(*ptr == '%') {
3199 if(known_scheme && !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
3200 WCHAR val = decode_pct_val(ptr);
3201 if(is_unreserved(val)) {
3202 if(!computeOnly)
3203 uri->canon_uri[uri->canon_len] = val;
3204 ++uri->canon_len;
3206 ptr += 2;
3207 continue;
3210 } else if(known_scheme && !is_unreserved(*ptr) && !is_reserved(*ptr)) {
3211 if(!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS) &&
3212 !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
3213 if(!computeOnly)
3214 pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
3215 uri->canon_len += 3;
3216 continue;
3220 if(!computeOnly)
3221 uri->canon_uri[uri->canon_len] = *ptr;
3222 ++uri->canon_len;
3225 uri->query_len = uri->canon_len - uri->query_start;
3227 if(!computeOnly)
3228 TRACE("(%p %p %x %d): Canonicalized query string %s len=%d\n", data, uri, flags,
3229 computeOnly, debugstr_wn(uri->canon_uri+uri->query_start, uri->query_len),
3230 uri->query_len);
3231 return TRUE;
3234 static BOOL canonicalize_fragment(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
3235 const WCHAR *ptr, *end;
3236 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
3238 if(!data->fragment) {
3239 uri->fragment_start = -1;
3240 uri->fragment_len = 0;
3241 return TRUE;
3244 uri->fragment_start = uri->canon_len;
3246 end = data->fragment + data->fragment_len;
3247 for(ptr = data->fragment; ptr < end; ++ptr) {
3248 if(*ptr == '%') {
3249 if(known_scheme && !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
3250 WCHAR val = decode_pct_val(ptr);
3251 if(is_unreserved(val)) {
3252 if(!computeOnly)
3253 uri->canon_uri[uri->canon_len] = val;
3254 ++uri->canon_len;
3256 ptr += 2;
3257 continue;
3260 } else if(known_scheme && !is_unreserved(*ptr) && !is_reserved(*ptr)) {
3261 if(!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS) &&
3262 !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
3263 if(!computeOnly)
3264 pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
3265 uri->canon_len += 3;
3266 continue;
3270 if(!computeOnly)
3271 uri->canon_uri[uri->canon_len] = *ptr;
3272 ++uri->canon_len;
3275 uri->fragment_len = uri->canon_len - uri->fragment_start;
3277 if(!computeOnly)
3278 TRACE("(%p %p %x %d): Canonicalized fragment %s len=%d\n", data, uri, flags,
3279 computeOnly, debugstr_wn(uri->canon_uri+uri->fragment_start, uri->fragment_len),
3280 uri->fragment_len);
3281 return TRUE;
3284 /* Canonicalizes the scheme information specified in the parse_data using the specified flags. */
3285 static BOOL canonicalize_scheme(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
3286 uri->scheme_start = -1;
3287 uri->scheme_len = 0;
3289 if(!data->scheme) {
3290 /* The only type of URI that doesn't have to have a scheme is a relative
3291 * URI.
3293 if(!data->is_relative) {
3294 FIXME("(%p %p %x): Unable to determine the scheme type of %s.\n", data,
3295 uri, flags, debugstr_w(data->uri));
3296 return FALSE;
3298 } else {
3299 if(!computeOnly) {
3300 DWORD i;
3301 INT pos = uri->canon_len;
3303 for(i = 0; i < data->scheme_len; ++i) {
3304 /* Scheme name must be lower case after canonicalization. */
3305 uri->canon_uri[i + pos] = tolowerW(data->scheme[i]);
3308 uri->canon_uri[i + pos] = ':';
3309 uri->scheme_start = pos;
3311 TRACE("(%p %p %x): Canonicalized scheme=%s, len=%d.\n", data, uri, flags,
3312 debugstr_wn(uri->canon_uri, uri->scheme_len), data->scheme_len);
3315 /* This happens in both computation modes. */
3316 uri->canon_len += data->scheme_len + 1;
3317 uri->scheme_len = data->scheme_len;
3319 return TRUE;
3322 /* Compute's what the length of the URI specified by the parse_data will be
3323 * after canonicalization occurs using the specified flags.
3325 * This function will return a non-zero value indicating the length of the canonicalized
3326 * URI, or -1 on error.
3328 static int compute_canonicalized_length(const parse_data *data, DWORD flags) {
3329 Uri uri;
3331 memset(&uri, 0, sizeof(Uri));
3333 TRACE("(%p %x): Beginning to compute canonicalized length for URI %s\n", data, flags,
3334 debugstr_w(data->uri));
3336 if(!canonicalize_scheme(data, &uri, flags, TRUE)) {
3337 ERR("(%p %x): Failed to compute URI scheme length.\n", data, flags);
3338 return -1;
3341 if(!canonicalize_hierpart(data, &uri, flags, TRUE)) {
3342 ERR("(%p %x): Failed to compute URI hierpart length.\n", data, flags);
3343 return -1;
3346 if(!canonicalize_query(data, &uri, flags, TRUE)) {
3347 ERR("(%p %x): Failed to compute query string length.\n", data, flags);
3348 return -1;
3351 if(!canonicalize_fragment(data, &uri, flags, TRUE)) {
3352 ERR("(%p %x): Failed to compute fragment length.\n", data, flags);
3353 return -1;
3356 TRACE("(%p %x): Finished computing canonicalized URI length. length=%d\n", data, flags, uri.canon_len);
3358 return uri.canon_len;
3361 /* Canonicalizes the URI data specified in the parse_data, using the given flags. If the
3362 * canonicalization succeededs it will store all the canonicalization information
3363 * in the pointer to the Uri.
3365 * To canonicalize a URI this function first computes what the length of the URI
3366 * specified by the parse_data will be. Once this is done it will then perfom the actual
3367 * canonicalization of the URI.
3369 static HRESULT canonicalize_uri(const parse_data *data, Uri *uri, DWORD flags) {
3370 INT len;
3372 uri->canon_uri = NULL;
3373 len = uri->canon_size = uri->canon_len = 0;
3375 TRACE("(%p %p %x): beginning to canonicalize URI %s.\n", data, uri, flags, debugstr_w(data->uri));
3377 /* First try to compute the length of the URI. */
3378 len = compute_canonicalized_length(data, flags);
3379 if(len == -1) {
3380 ERR("(%p %p %x): Could not compute the canonicalized length of %s.\n", data, uri, flags,
3381 debugstr_w(data->uri));
3382 return E_INVALIDARG;
3385 uri->canon_uri = heap_alloc((len+1)*sizeof(WCHAR));
3386 if(!uri->canon_uri)
3387 return E_OUTOFMEMORY;
3389 uri->canon_size = len;
3390 if(!canonicalize_scheme(data, uri, flags, FALSE)) {
3391 ERR("(%p %p %x): Unable to canonicalize the scheme of the URI.\n", data, uri, flags);
3392 heap_free(uri->canon_uri);
3393 return E_INVALIDARG;
3395 uri->scheme_type = data->scheme_type;
3397 if(!canonicalize_hierpart(data, uri, flags, FALSE)) {
3398 ERR("(%p %p %x): Unable to canonicalize the heirpart of the URI\n", data, uri, flags);
3399 heap_free(uri->canon_uri);
3400 return E_INVALIDARG;
3403 if(!canonicalize_query(data, uri, flags, FALSE)) {
3404 ERR("(%p %p %x): Unable to canonicalize query string of the URI.\n",
3405 data, uri, flags);
3406 return E_INVALIDARG;
3409 if(!canonicalize_fragment(data, uri, flags, FALSE)) {
3410 ERR("(%p %p %x): Unable to canonicalize fragment of the URI.\n",
3411 data, uri, flags);
3412 return E_INVALIDARG;
3415 /* There's a possibility we didn't use all the space we allocated
3416 * earlier.
3418 if(uri->canon_len < uri->canon_size) {
3419 /* This happens if the URI is hierarchical and dot
3420 * segments were removed from it's path.
3422 WCHAR *tmp = heap_realloc(uri->canon_uri, (uri->canon_len+1)*sizeof(WCHAR));
3423 if(!tmp)
3424 return E_OUTOFMEMORY;
3426 uri->canon_uri = tmp;
3427 uri->canon_size = uri->canon_len;
3430 uri->canon_uri[uri->canon_len] = '\0';
3431 TRACE("(%p %p %x): finished canonicalizing the URI. uri=%s\n", data, uri, flags, debugstr_w(uri->canon_uri));
3433 return S_OK;
3436 static HRESULT get_builder_component(LPWSTR *component, DWORD *component_len,
3437 LPCWSTR source, DWORD source_len,
3438 LPCWSTR *output, DWORD *output_len)
3440 if(!output_len) {
3441 if(output)
3442 *output = NULL;
3443 return E_POINTER;
3446 if(!output) {
3447 *output_len = 0;
3448 return E_POINTER;
3451 if(!(*component) && source) {
3452 /* Allocate 'component', and copy the contents from 'source'
3453 * into the new allocation.
3455 *component = heap_alloc((source_len+1)*sizeof(WCHAR));
3456 if(!(*component))
3457 return E_OUTOFMEMORY;
3459 memcpy(*component, source, source_len*sizeof(WCHAR));
3460 (*component)[source_len] = '\0';
3461 *component_len = source_len;
3464 *output = *component;
3465 *output_len = *component_len;
3466 return *output ? S_OK : S_FALSE;
3469 /* Allocates 'component' and copies the string from 'new_value' into 'component'.
3470 * If 'prefix' is set and 'new_value' isn't NULL, then it checks if 'new_value'
3471 * starts with 'prefix'. If it doesn't then 'prefix' is prepended to 'component'.
3473 * If everything is successful, then will set 'success_flag' in 'flags'.
3475 static HRESULT set_builder_component(LPWSTR *component, DWORD *component_len, LPCWSTR new_value,
3476 WCHAR prefix, DWORD *flags, DWORD success_flag)
3478 heap_free(*component);
3480 if(!new_value) {
3481 *component = NULL;
3482 *component_len = 0;
3483 } else {
3484 BOOL add_prefix = FALSE;
3485 DWORD len = lstrlenW(new_value);
3486 DWORD pos = 0;
3488 if(prefix && *new_value != prefix) {
3489 add_prefix = TRUE;
3490 *component = heap_alloc((len+2)*sizeof(WCHAR));
3491 } else
3492 *component = heap_alloc((len+1)*sizeof(WCHAR));
3494 if(!(*component))
3495 return E_OUTOFMEMORY;
3497 if(add_prefix)
3498 (*component)[pos++] = prefix;
3500 memcpy(*component+pos, new_value, (len+1)*sizeof(WCHAR));
3501 *component_len = len+pos;
3504 *flags |= success_flag;
3505 return S_OK;
3508 #define URI(x) ((IUri*) &(x)->lpIUriVtbl)
3509 #define URIBUILDER(x) ((IUriBuilder*) &(x)->lpIUriBuilderVtbl)
3511 static void reset_builder(UriBuilder *builder) {
3512 if(builder->uri)
3513 IUri_Release(URI(builder->uri));
3514 builder->uri = NULL;
3516 heap_free(builder->fragment);
3517 builder->fragment = NULL;
3518 builder->fragment_len = 0;
3520 heap_free(builder->host);
3521 builder->host = NULL;
3522 builder->host_len = 0;
3524 heap_free(builder->password);
3525 builder->password = NULL;
3526 builder->password_len = 0;
3528 heap_free(builder->path);
3529 builder->path = NULL;
3530 builder->path_len = 0;
3532 heap_free(builder->query);
3533 builder->query = NULL;
3534 builder->query_len = 0;
3536 heap_free(builder->scheme);
3537 builder->scheme = NULL;
3538 builder->scheme_len = 0;
3540 heap_free(builder->username);
3541 builder->username = NULL;
3542 builder->username_len = 0;
3544 builder->has_port = FALSE;
3545 builder->port = 0;
3546 builder->modified_props = 0;
3549 static HRESULT validate_scheme_name(const UriBuilder *builder, parse_data *data, DWORD flags) {
3550 const WCHAR *component;
3551 const WCHAR *ptr;
3552 const WCHAR **pptr;
3553 DWORD expected_len;
3555 if(builder->scheme) {
3556 ptr = builder->scheme;
3557 expected_len = builder->scheme_len;
3558 } else if(builder->uri && builder->uri->scheme_start > -1) {
3559 ptr = builder->uri->canon_uri+builder->uri->scheme_start;
3560 expected_len = builder->uri->scheme_len;
3561 } else {
3562 static const WCHAR nullW[] = {0};
3563 ptr = nullW;
3564 expected_len = 0;
3567 component = ptr;
3568 pptr = &ptr;
3569 if(parse_scheme(pptr, data, flags, ALLOW_NULL_TERM_SCHEME) &&
3570 data->scheme_len == expected_len) {
3571 if(data->scheme)
3572 TRACE("(%p %p %x): Found valid scheme component %s len=%d.\n", builder, data, flags,
3573 debugstr_wn(data->scheme, data->scheme_len), data->scheme_len);
3574 } else {
3575 TRACE("(%p %p %x): Invalid scheme component found %s.\n", builder, data, flags,
3576 debugstr_wn(component, expected_len));
3577 return INET_E_INVALID_URL;
3580 return S_OK;
3583 static HRESULT validate_username(const UriBuilder *builder, parse_data *data, DWORD flags) {
3584 const WCHAR *ptr;
3585 const WCHAR **pptr;
3586 DWORD expected_len;
3588 if(builder->username) {
3589 ptr = builder->username;
3590 expected_len = builder->username_len;
3591 } else if(!(builder->modified_props & Uri_HAS_USER_NAME) && builder->uri &&
3592 builder->uri->userinfo_start > -1 && builder->uri->userinfo_split != 0) {
3593 /* Just use the username from the base Uri. */
3594 data->username = builder->uri->canon_uri+builder->uri->userinfo_start;
3595 data->username_len = (builder->uri->userinfo_split > -1) ?
3596 builder->uri->userinfo_split : builder->uri->userinfo_len;
3597 ptr = NULL;
3598 } else {
3599 ptr = NULL;
3600 expected_len = 0;
3603 if(ptr) {
3604 const WCHAR *component = ptr;
3605 pptr = &ptr;
3606 if(parse_username(pptr, data, flags, ALLOW_NULL_TERM_USER_NAME) &&
3607 data->username_len == expected_len)
3608 TRACE("(%p %p %x): Found valid username component %s len=%d.\n", builder, data, flags,
3609 debugstr_wn(data->username, data->username_len), data->username_len);
3610 else {
3611 TRACE("(%p %p %x): Invalid username component found %s.\n", builder, data, flags,
3612 debugstr_wn(component, expected_len));
3613 return INET_E_INVALID_URL;
3617 return S_OK;
3620 static HRESULT validate_password(const UriBuilder *builder, parse_data *data, DWORD flags) {
3621 const WCHAR *ptr;
3622 const WCHAR **pptr;
3623 DWORD expected_len;
3625 if(builder->password) {
3626 ptr = builder->password;
3627 expected_len = builder->password_len;
3628 } else if(!(builder->modified_props & Uri_HAS_PASSWORD) && builder->uri &&
3629 builder->uri->userinfo_split > -1) {
3630 data->password = builder->uri->canon_uri+builder->uri->userinfo_start+builder->uri->userinfo_split+1;
3631 data->password_len = builder->uri->userinfo_len-builder->uri->userinfo_split-1;
3632 ptr = NULL;
3633 } else {
3634 ptr = NULL;
3635 expected_len = 0;
3638 if(ptr) {
3639 const WCHAR *component = ptr;
3640 pptr = &ptr;
3641 if(parse_password(pptr, data, flags, ALLOW_NULL_TERM_PASSWORD) &&
3642 data->password_len == expected_len)
3643 TRACE("(%p %p %x): Found valid password component %s len=%d.\n", builder, data, flags,
3644 debugstr_wn(data->password, data->password_len), data->password_len);
3645 else {
3646 TRACE("(%p %p %x): Invalid password component found %s.\n", builder, data, flags,
3647 debugstr_wn(component, expected_len));
3648 return INET_E_INVALID_URL;
3652 return S_OK;
3655 static HRESULT validate_userinfo(const UriBuilder *builder, parse_data *data, DWORD flags) {
3656 HRESULT hr;
3658 hr = validate_username(builder, data, flags);
3659 if(FAILED(hr))
3660 return hr;
3662 hr = validate_password(builder, data, flags);
3663 if(FAILED(hr))
3664 return hr;
3666 return S_OK;
3669 static HRESULT validate_host(const UriBuilder *builder, parse_data *data, DWORD flags) {
3670 const WCHAR *ptr;
3671 const WCHAR **pptr;
3672 DWORD expected_len;
3674 if(builder->host) {
3675 ptr = builder->host;
3676 expected_len = builder->host_len;
3677 } else if(!(builder->modified_props & Uri_HAS_HOST) && builder->uri && builder->uri->host_start > -1) {
3678 ptr = builder->uri->canon_uri + builder->uri->host_start;
3679 expected_len = builder->uri->host_len;
3680 } else
3681 ptr = NULL;
3683 if(ptr) {
3684 const WCHAR *component = ptr;
3685 DWORD extras = ALLOW_BRACKETLESS_IP_LITERAL|IGNORE_PORT_DELIMITER|SKIP_IP_FUTURE_CHECK;
3686 pptr = &ptr;
3688 if(parse_host(pptr, data, flags, extras) && data->host_len == expected_len)
3689 TRACE("(%p %p %x): Found valid host name %s len=%d type=%d.\n", builder, data, flags,
3690 debugstr_wn(data->host, data->host_len), data->host_len, data->host_type);
3691 else {
3692 TRACE("(%p %p %x): Invalid host name found %s.\n", builder, data, flags,
3693 debugstr_wn(component, expected_len));
3694 return INET_E_INVALID_URL;
3698 return S_OK;
3701 static void setup_port(const UriBuilder *builder, parse_data *data, DWORD flags) {
3702 if(builder->modified_props & Uri_HAS_PORT) {
3703 if(builder->has_port) {
3704 data->has_port = TRUE;
3705 data->port_value = builder->port;
3707 } else if(builder->uri && builder->uri->has_port) {
3708 data->has_port = TRUE;
3709 data->port_value = builder->uri->port;
3712 if(data->has_port)
3713 TRACE("(%p %p %x): Using %u as port for IUri.\n", builder, data, flags, data->port_value);
3716 static HRESULT validate_components(const UriBuilder *builder, parse_data *data, DWORD flags) {
3717 HRESULT hr;
3719 memset(data, 0, sizeof(parse_data));
3721 TRACE("(%p %p %x): Beginning to validate builder components.\n", builder, data, flags);
3723 hr = validate_scheme_name(builder, data, flags);
3724 if(FAILED(hr))
3725 return hr;
3727 /* Extra validation for file schemes. */
3728 if(data->scheme_type == URL_SCHEME_FILE) {
3729 if((builder->password || (builder->uri && builder->uri->userinfo_split > -1)) ||
3730 (builder->username || (builder->uri && builder->uri->userinfo_start > -1))) {
3731 TRACE("(%p %p %x): File schemes can't contain a username or password.\n",
3732 builder, data, flags);
3733 return INET_E_INVALID_URL;
3737 hr = validate_userinfo(builder, data, flags);
3738 if(FAILED(hr))
3739 return hr;
3741 hr = validate_host(builder, data, flags);
3742 if(FAILED(hr))
3743 return hr;
3745 /* The URI is opaque if it doesn't have an authority component. */
3746 data->is_opaque = !data->username && !data->password && !data->host;
3748 setup_port(builder, data, flags);
3750 return E_NOTIMPL;
3753 static HRESULT build_uri(const UriBuilder *builder, IUri **uri, DWORD create_flags,
3754 DWORD use_orig_flags, DWORD encoding_mask)
3756 HRESULT hr;
3757 parse_data data;
3759 if(!uri)
3760 return E_POINTER;
3762 if(encoding_mask && (!builder->uri || builder->modified_props)) {
3763 *uri = NULL;
3764 return E_NOTIMPL;
3767 /* Decide what flags should be used when creating the Uri. */
3768 if((use_orig_flags & UriBuilder_USE_ORIGINAL_FLAGS) && builder->uri)
3769 create_flags = builder->uri->create_flags;
3770 else {
3771 if(has_invalid_flag_combination(create_flags)) {
3772 *uri = NULL;
3773 return E_INVALIDARG;
3776 /* Set the default flags if they don't cause a conflict. */
3777 apply_default_flags(&create_flags);
3780 /* Return the base IUri if no changes have been made and the create_flags match. */
3781 if(builder->uri && !builder->modified_props && builder->uri->create_flags == create_flags) {
3782 *uri = URI(builder->uri);
3783 IUri_AddRef(*uri);
3784 return S_OK;
3787 hr = validate_components(builder, &data, create_flags);
3788 if(FAILED(hr)) {
3789 *uri = NULL;
3790 return hr;
3793 return S_OK;
3796 #define URI_THIS(iface) DEFINE_THIS(Uri, IUri, iface)
3798 static HRESULT WINAPI Uri_QueryInterface(IUri *iface, REFIID riid, void **ppv)
3800 Uri *This = URI_THIS(iface);
3802 if(IsEqualGUID(&IID_IUnknown, riid)) {
3803 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
3804 *ppv = URI(This);
3805 }else if(IsEqualGUID(&IID_IUri, riid)) {
3806 TRACE("(%p)->(IID_IUri %p)\n", This, ppv);
3807 *ppv = URI(This);
3808 }else if(IsEqualGUID(&IID_IUriObj, riid)) {
3809 TRACE("(%p)->(IID_IUriObj %p)\n", This, ppv);
3810 *ppv = This;
3811 return S_OK;
3812 }else {
3813 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
3814 *ppv = NULL;
3815 return E_NOINTERFACE;
3818 IUnknown_AddRef((IUnknown*)*ppv);
3819 return S_OK;
3822 static ULONG WINAPI Uri_AddRef(IUri *iface)
3824 Uri *This = URI_THIS(iface);
3825 LONG ref = InterlockedIncrement(&This->ref);
3827 TRACE("(%p) ref=%d\n", This, ref);
3829 return ref;
3832 static ULONG WINAPI Uri_Release(IUri *iface)
3834 Uri *This = URI_THIS(iface);
3835 LONG ref = InterlockedDecrement(&This->ref);
3837 TRACE("(%p) ref=%d\n", This, ref);
3839 if(!ref) {
3840 SysFreeString(This->raw_uri);
3841 heap_free(This->canon_uri);
3842 heap_free(This);
3845 return ref;
3848 static HRESULT WINAPI Uri_GetPropertyBSTR(IUri *iface, Uri_PROPERTY uriProp, BSTR *pbstrProperty, DWORD dwFlags)
3850 Uri *This = URI_THIS(iface);
3851 HRESULT hres;
3852 TRACE("(%p)->(%d %p %x)\n", This, uriProp, pbstrProperty, dwFlags);
3854 if(!pbstrProperty)
3855 return E_POINTER;
3857 if(uriProp > Uri_PROPERTY_STRING_LAST) {
3858 /* Windows allocates an empty BSTR for invalid Uri_PROPERTY's. */
3859 *pbstrProperty = SysAllocStringLen(NULL, 0);
3860 if(!(*pbstrProperty))
3861 return E_OUTOFMEMORY;
3863 /* It only returns S_FALSE for the ZONE property... */
3864 if(uriProp == Uri_PROPERTY_ZONE)
3865 return S_FALSE;
3866 else
3867 return S_OK;
3870 /* Don't have support for flags yet. */
3871 if(dwFlags) {
3872 FIXME("(%p)->(%d %p %x)\n", This, uriProp, pbstrProperty, dwFlags);
3873 return E_NOTIMPL;
3876 switch(uriProp) {
3877 case Uri_PROPERTY_ABSOLUTE_URI:
3878 if(!This->display_absolute) {
3879 *pbstrProperty = SysAllocStringLen(NULL, 0);
3880 hres = S_FALSE;
3881 } else {
3882 if(This->scheme_type != URL_SCHEME_UNKNOWN && This->userinfo_start > -1) {
3883 if(This->userinfo_len == 0) {
3884 /* Don't include the '@' after the userinfo component. */
3885 *pbstrProperty = SysAllocStringLen(NULL, This->canon_len-1);
3886 hres = S_OK;
3887 if(*pbstrProperty) {
3888 /* Copy everything before it. */
3889 memcpy(*pbstrProperty, This->canon_uri, This->userinfo_start*sizeof(WCHAR));
3891 /* And everything after it. */
3892 memcpy(*pbstrProperty+This->userinfo_start, This->canon_uri+This->userinfo_start+1,
3893 (This->canon_len-This->userinfo_start-1)*sizeof(WCHAR));
3895 } else if(This->userinfo_split == 0 && This->userinfo_len == 1) {
3896 /* Don't include the ":@" */
3897 *pbstrProperty = SysAllocStringLen(NULL, This->canon_len-2);
3898 hres = S_OK;
3899 if(*pbstrProperty) {
3900 memcpy(*pbstrProperty, This->canon_uri, This->userinfo_start*sizeof(WCHAR));
3901 memcpy(*pbstrProperty+This->userinfo_start, This->canon_uri+This->userinfo_start+2,
3902 (This->canon_len-This->userinfo_start-2)*sizeof(WCHAR));
3904 } else {
3905 *pbstrProperty = SysAllocString(This->canon_uri);
3906 hres = S_OK;
3908 } else {
3909 *pbstrProperty = SysAllocString(This->canon_uri);
3910 hres = S_OK;
3914 if(!(*pbstrProperty))
3915 hres = E_OUTOFMEMORY;
3917 break;
3918 case Uri_PROPERTY_AUTHORITY:
3919 if(This->authority_start > -1) {
3920 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->authority_start, This->authority_len);
3921 hres = S_OK;
3922 } else {
3923 *pbstrProperty = SysAllocStringLen(NULL, 0);
3924 hres = S_FALSE;
3927 if(!(*pbstrProperty))
3928 hres = E_OUTOFMEMORY;
3930 break;
3931 case Uri_PROPERTY_DISPLAY_URI:
3932 /* The Display URI contains everything except for the userinfo for known
3933 * scheme types.
3935 if(This->scheme_type != URL_SCHEME_UNKNOWN && This->userinfo_start > -1) {
3936 *pbstrProperty = SysAllocStringLen(NULL, This->canon_len-This->userinfo_len);
3938 if(*pbstrProperty) {
3939 /* Copy everything before the userinfo over. */
3940 memcpy(*pbstrProperty, This->canon_uri, This->userinfo_start*sizeof(WCHAR));
3941 /* Copy everything after the userinfo over. */
3942 memcpy(*pbstrProperty+This->userinfo_start,
3943 This->canon_uri+This->userinfo_start+This->userinfo_len+1,
3944 (This->canon_len-(This->userinfo_start+This->userinfo_len+1))*sizeof(WCHAR));
3946 } else
3947 *pbstrProperty = SysAllocString(This->canon_uri);
3949 if(!(*pbstrProperty))
3950 hres = E_OUTOFMEMORY;
3951 else
3952 hres = S_OK;
3954 break;
3955 case Uri_PROPERTY_DOMAIN:
3956 if(This->domain_offset > -1) {
3957 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->host_start+This->domain_offset,
3958 This->host_len-This->domain_offset);
3959 hres = S_OK;
3960 } else {
3961 *pbstrProperty = SysAllocStringLen(NULL, 0);
3962 hres = S_FALSE;
3965 if(!(*pbstrProperty))
3966 hres = E_OUTOFMEMORY;
3968 break;
3969 case Uri_PROPERTY_EXTENSION:
3970 if(This->extension_offset > -1) {
3971 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->path_start+This->extension_offset,
3972 This->path_len-This->extension_offset);
3973 hres = S_OK;
3974 } else {
3975 *pbstrProperty = SysAllocStringLen(NULL, 0);
3976 hres = S_FALSE;
3979 if(!(*pbstrProperty))
3980 hres = E_OUTOFMEMORY;
3982 break;
3983 case Uri_PROPERTY_FRAGMENT:
3984 if(This->fragment_start > -1) {
3985 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->fragment_start, This->fragment_len);
3986 hres = S_OK;
3987 } else {
3988 *pbstrProperty = SysAllocStringLen(NULL, 0);
3989 hres = S_FALSE;
3992 if(!(*pbstrProperty))
3993 hres = E_OUTOFMEMORY;
3995 break;
3996 case Uri_PROPERTY_HOST:
3997 if(This->host_start > -1) {
3998 /* The '[' and ']' aren't included for IPv6 addresses. */
3999 if(This->host_type == Uri_HOST_IPV6)
4000 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->host_start+1, This->host_len-2);
4001 else
4002 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->host_start, This->host_len);
4004 hres = S_OK;
4005 } else {
4006 *pbstrProperty = SysAllocStringLen(NULL, 0);
4007 hres = S_FALSE;
4010 if(!(*pbstrProperty))
4011 hres = E_OUTOFMEMORY;
4013 break;
4014 case Uri_PROPERTY_PASSWORD:
4015 if(This->userinfo_split > -1) {
4016 *pbstrProperty = SysAllocStringLen(
4017 This->canon_uri+This->userinfo_start+This->userinfo_split+1,
4018 This->userinfo_len-This->userinfo_split-1);
4019 hres = S_OK;
4020 } else {
4021 *pbstrProperty = SysAllocStringLen(NULL, 0);
4022 hres = S_FALSE;
4025 if(!(*pbstrProperty))
4026 return E_OUTOFMEMORY;
4028 break;
4029 case Uri_PROPERTY_PATH:
4030 if(This->path_start > -1) {
4031 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->path_start, This->path_len);
4032 hres = S_OK;
4033 } else {
4034 *pbstrProperty = SysAllocStringLen(NULL, 0);
4035 hres = S_FALSE;
4038 if(!(*pbstrProperty))
4039 hres = E_OUTOFMEMORY;
4041 break;
4042 case Uri_PROPERTY_PATH_AND_QUERY:
4043 if(This->path_start > -1) {
4044 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->path_start, This->path_len+This->query_len);
4045 hres = S_OK;
4046 } else if(This->query_start > -1) {
4047 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->query_start, This->query_len);
4048 hres = S_OK;
4049 } else {
4050 *pbstrProperty = SysAllocStringLen(NULL, 0);
4051 hres = S_FALSE;
4054 if(!(*pbstrProperty))
4055 hres = E_OUTOFMEMORY;
4057 break;
4058 case Uri_PROPERTY_QUERY:
4059 if(This->query_start > -1) {
4060 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->query_start, This->query_len);
4061 hres = S_OK;
4062 } else {
4063 *pbstrProperty = SysAllocStringLen(NULL, 0);
4064 hres = S_FALSE;
4067 if(!(*pbstrProperty))
4068 hres = E_OUTOFMEMORY;
4070 break;
4071 case Uri_PROPERTY_RAW_URI:
4072 *pbstrProperty = SysAllocString(This->raw_uri);
4073 if(!(*pbstrProperty))
4074 hres = E_OUTOFMEMORY;
4075 else
4076 hres = S_OK;
4077 break;
4078 case Uri_PROPERTY_SCHEME_NAME:
4079 if(This->scheme_start > -1) {
4080 *pbstrProperty = SysAllocStringLen(This->canon_uri + This->scheme_start, This->scheme_len);
4081 hres = S_OK;
4082 } else {
4083 *pbstrProperty = SysAllocStringLen(NULL, 0);
4084 hres = S_FALSE;
4087 if(!(*pbstrProperty))
4088 hres = E_OUTOFMEMORY;
4090 break;
4091 case Uri_PROPERTY_USER_INFO:
4092 if(This->userinfo_start > -1) {
4093 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->userinfo_start, This->userinfo_len);
4094 hres = S_OK;
4095 } else {
4096 *pbstrProperty = SysAllocStringLen(NULL, 0);
4097 hres = S_FALSE;
4100 if(!(*pbstrProperty))
4101 hres = E_OUTOFMEMORY;
4103 break;
4104 case Uri_PROPERTY_USER_NAME:
4105 if(This->userinfo_start > -1 && This->userinfo_split != 0) {
4106 /* If userinfo_split is set, that means a password exists
4107 * so the username is only from userinfo_start to userinfo_split.
4109 if(This->userinfo_split > -1) {
4110 *pbstrProperty = SysAllocStringLen(This->canon_uri + This->userinfo_start, This->userinfo_split);
4111 hres = S_OK;
4112 } else {
4113 *pbstrProperty = SysAllocStringLen(This->canon_uri + This->userinfo_start, This->userinfo_len);
4114 hres = S_OK;
4116 } else {
4117 *pbstrProperty = SysAllocStringLen(NULL, 0);
4118 hres = S_FALSE;
4121 if(!(*pbstrProperty))
4122 return E_OUTOFMEMORY;
4124 break;
4125 default:
4126 FIXME("(%p)->(%d %p %x)\n", This, uriProp, pbstrProperty, dwFlags);
4127 hres = E_NOTIMPL;
4130 return hres;
4133 static HRESULT WINAPI Uri_GetPropertyLength(IUri *iface, Uri_PROPERTY uriProp, DWORD *pcchProperty, DWORD dwFlags)
4135 Uri *This = URI_THIS(iface);
4136 HRESULT hres;
4137 TRACE("(%p)->(%d %p %x)\n", This, uriProp, pcchProperty, dwFlags);
4139 if(!pcchProperty)
4140 return E_INVALIDARG;
4142 /* Can only return a length for a property if it's a string. */
4143 if(uriProp > Uri_PROPERTY_STRING_LAST)
4144 return E_INVALIDARG;
4146 /* Don't have support for flags yet. */
4147 if(dwFlags) {
4148 FIXME("(%p)->(%d %p %x)\n", This, uriProp, pcchProperty, dwFlags);
4149 return E_NOTIMPL;
4152 switch(uriProp) {
4153 case Uri_PROPERTY_ABSOLUTE_URI:
4154 if(!This->display_absolute) {
4155 *pcchProperty = 0;
4156 hres = S_FALSE;
4157 } else {
4158 if(This->scheme_type != URL_SCHEME_UNKNOWN) {
4159 if(This->userinfo_start > -1 && This->userinfo_len == 0)
4160 /* Don't include the '@' in the length. */
4161 *pcchProperty = This->canon_len-1;
4162 else if(This->userinfo_start > -1 && This->userinfo_len == 1 &&
4163 This->userinfo_split == 0)
4164 /* Don't include the ":@" in the length. */
4165 *pcchProperty = This->canon_len-2;
4166 else
4167 *pcchProperty = This->canon_len;
4168 } else
4169 *pcchProperty = This->canon_len;
4171 hres = S_OK;
4174 break;
4175 case Uri_PROPERTY_AUTHORITY:
4176 *pcchProperty = This->authority_len;
4177 hres = (This->authority_start > -1) ? S_OK : S_FALSE;
4178 break;
4179 case Uri_PROPERTY_DISPLAY_URI:
4180 if(This->scheme_type != URL_SCHEME_UNKNOWN && This->userinfo_start > -1)
4181 *pcchProperty = This->canon_len-This->userinfo_len-1;
4182 else
4183 *pcchProperty = This->canon_len;
4185 hres = S_OK;
4186 break;
4187 case Uri_PROPERTY_DOMAIN:
4188 if(This->domain_offset > -1)
4189 *pcchProperty = This->host_len - This->domain_offset;
4190 else
4191 *pcchProperty = 0;
4193 hres = (This->domain_offset > -1) ? S_OK : S_FALSE;
4194 break;
4195 case Uri_PROPERTY_EXTENSION:
4196 if(This->extension_offset > -1) {
4197 *pcchProperty = This->path_len - This->extension_offset;
4198 hres = S_OK;
4199 } else {
4200 *pcchProperty = 0;
4201 hres = S_FALSE;
4204 break;
4205 case Uri_PROPERTY_FRAGMENT:
4206 *pcchProperty = This->fragment_len;
4207 hres = (This->fragment_start > -1) ? S_OK : S_FALSE;
4208 break;
4209 case Uri_PROPERTY_HOST:
4210 *pcchProperty = This->host_len;
4212 /* '[' and ']' aren't included in the length. */
4213 if(This->host_type == Uri_HOST_IPV6)
4214 *pcchProperty -= 2;
4216 hres = (This->host_start > -1) ? S_OK : S_FALSE;
4217 break;
4218 case Uri_PROPERTY_PASSWORD:
4219 *pcchProperty = (This->userinfo_split > -1) ? This->userinfo_len-This->userinfo_split-1 : 0;
4220 hres = (This->userinfo_split > -1) ? S_OK : S_FALSE;
4221 break;
4222 case Uri_PROPERTY_PATH:
4223 *pcchProperty = This->path_len;
4224 hres = (This->path_start > -1) ? S_OK : S_FALSE;
4225 break;
4226 case Uri_PROPERTY_PATH_AND_QUERY:
4227 *pcchProperty = This->path_len+This->query_len;
4228 hres = (This->path_start > -1 || This->query_start > -1) ? S_OK : S_FALSE;
4229 break;
4230 case Uri_PROPERTY_QUERY:
4231 *pcchProperty = This->query_len;
4232 hres = (This->query_start > -1) ? S_OK : S_FALSE;
4233 break;
4234 case Uri_PROPERTY_RAW_URI:
4235 *pcchProperty = SysStringLen(This->raw_uri);
4236 hres = S_OK;
4237 break;
4238 case Uri_PROPERTY_SCHEME_NAME:
4239 *pcchProperty = This->scheme_len;
4240 hres = (This->scheme_start > -1) ? S_OK : S_FALSE;
4241 break;
4242 case Uri_PROPERTY_USER_INFO:
4243 *pcchProperty = This->userinfo_len;
4244 hres = (This->userinfo_start > -1) ? S_OK : S_FALSE;
4245 break;
4246 case Uri_PROPERTY_USER_NAME:
4247 *pcchProperty = (This->userinfo_split > -1) ? This->userinfo_split : This->userinfo_len;
4248 if(This->userinfo_split == 0)
4249 hres = S_FALSE;
4250 else
4251 hres = (This->userinfo_start > -1) ? S_OK : S_FALSE;
4252 break;
4253 default:
4254 FIXME("(%p)->(%d %p %x)\n", This, uriProp, pcchProperty, dwFlags);
4255 hres = E_NOTIMPL;
4258 return hres;
4261 static HRESULT WINAPI Uri_GetPropertyDWORD(IUri *iface, Uri_PROPERTY uriProp, DWORD *pcchProperty, DWORD dwFlags)
4263 Uri *This = URI_THIS(iface);
4264 HRESULT hres;
4266 TRACE("(%p)->(%d %p %x)\n", This, uriProp, pcchProperty, dwFlags);
4268 if(!pcchProperty)
4269 return E_INVALIDARG;
4271 /* Microsoft's implementation for the ZONE property of a URI seems to be lacking...
4272 * From what I can tell, instead of checking which URLZONE the URI belongs to it
4273 * simply assigns URLZONE_INVALID and returns E_NOTIMPL. This also applies to the GetZone
4274 * function.
4276 if(uriProp == Uri_PROPERTY_ZONE) {
4277 *pcchProperty = URLZONE_INVALID;
4278 return E_NOTIMPL;
4281 if(uriProp < Uri_PROPERTY_DWORD_START) {
4282 *pcchProperty = 0;
4283 return E_INVALIDARG;
4286 switch(uriProp) {
4287 case Uri_PROPERTY_HOST_TYPE:
4288 *pcchProperty = This->host_type;
4289 hres = S_OK;
4290 break;
4291 case Uri_PROPERTY_PORT:
4292 if(!This->has_port) {
4293 *pcchProperty = 0;
4294 hres = S_FALSE;
4295 } else {
4296 *pcchProperty = This->port;
4297 hres = S_OK;
4300 break;
4301 case Uri_PROPERTY_SCHEME:
4302 *pcchProperty = This->scheme_type;
4303 hres = S_OK;
4304 break;
4305 default:
4306 FIXME("(%p)->(%d %p %x)\n", This, uriProp, pcchProperty, dwFlags);
4307 hres = E_NOTIMPL;
4310 return hres;
4313 static HRESULT WINAPI Uri_HasProperty(IUri *iface, Uri_PROPERTY uriProp, BOOL *pfHasProperty)
4315 Uri *This = URI_THIS(iface);
4316 TRACE("(%p)->(%d %p)\n", This, uriProp, pfHasProperty);
4318 if(!pfHasProperty)
4319 return E_INVALIDARG;
4321 switch(uriProp) {
4322 case Uri_PROPERTY_ABSOLUTE_URI:
4323 *pfHasProperty = This->display_absolute;
4324 break;
4325 case Uri_PROPERTY_AUTHORITY:
4326 *pfHasProperty = This->authority_start > -1;
4327 break;
4328 case Uri_PROPERTY_DISPLAY_URI:
4329 *pfHasProperty = TRUE;
4330 break;
4331 case Uri_PROPERTY_DOMAIN:
4332 *pfHasProperty = This->domain_offset > -1;
4333 break;
4334 case Uri_PROPERTY_EXTENSION:
4335 *pfHasProperty = This->extension_offset > -1;
4336 break;
4337 case Uri_PROPERTY_FRAGMENT:
4338 *pfHasProperty = This->fragment_start > -1;
4339 break;
4340 case Uri_PROPERTY_HOST:
4341 *pfHasProperty = This->host_start > -1;
4342 break;
4343 case Uri_PROPERTY_PASSWORD:
4344 *pfHasProperty = This->userinfo_split > -1;
4345 break;
4346 case Uri_PROPERTY_PATH:
4347 *pfHasProperty = This->path_start > -1;
4348 break;
4349 case Uri_PROPERTY_PATH_AND_QUERY:
4350 *pfHasProperty = (This->path_start > -1 || This->query_start > -1);
4351 break;
4352 case Uri_PROPERTY_QUERY:
4353 *pfHasProperty = This->query_start > -1;
4354 break;
4355 case Uri_PROPERTY_RAW_URI:
4356 *pfHasProperty = TRUE;
4357 break;
4358 case Uri_PROPERTY_SCHEME_NAME:
4359 *pfHasProperty = This->scheme_start > -1;
4360 break;
4361 case Uri_PROPERTY_USER_INFO:
4362 *pfHasProperty = This->userinfo_start > -1;
4363 break;
4364 case Uri_PROPERTY_USER_NAME:
4365 if(This->userinfo_split == 0)
4366 *pfHasProperty = FALSE;
4367 else
4368 *pfHasProperty = This->userinfo_start > -1;
4369 break;
4370 case Uri_PROPERTY_HOST_TYPE:
4371 *pfHasProperty = TRUE;
4372 break;
4373 case Uri_PROPERTY_PORT:
4374 *pfHasProperty = This->has_port;
4375 break;
4376 case Uri_PROPERTY_SCHEME:
4377 *pfHasProperty = TRUE;
4378 break;
4379 case Uri_PROPERTY_ZONE:
4380 *pfHasProperty = FALSE;
4381 break;
4382 default:
4383 FIXME("(%p)->(%d %p): Unsupported property type.\n", This, uriProp, pfHasProperty);
4384 return E_NOTIMPL;
4387 return S_OK;
4390 static HRESULT WINAPI Uri_GetAbsoluteUri(IUri *iface, BSTR *pstrAbsoluteUri)
4392 TRACE("(%p)->(%p)\n", iface, pstrAbsoluteUri);
4393 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_ABSOLUTE_URI, pstrAbsoluteUri, 0);
4396 static HRESULT WINAPI Uri_GetAuthority(IUri *iface, BSTR *pstrAuthority)
4398 TRACE("(%p)->(%p)\n", iface, pstrAuthority);
4399 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_AUTHORITY, pstrAuthority, 0);
4402 static HRESULT WINAPI Uri_GetDisplayUri(IUri *iface, BSTR *pstrDisplayUri)
4404 TRACE("(%p)->(%p)\n", iface, pstrDisplayUri);
4405 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_DISPLAY_URI, pstrDisplayUri, 0);
4408 static HRESULT WINAPI Uri_GetDomain(IUri *iface, BSTR *pstrDomain)
4410 TRACE("(%p)->(%p)\n", iface, pstrDomain);
4411 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_DOMAIN, pstrDomain, 0);
4414 static HRESULT WINAPI Uri_GetExtension(IUri *iface, BSTR *pstrExtension)
4416 TRACE("(%p)->(%p)\n", iface, pstrExtension);
4417 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_EXTENSION, pstrExtension, 0);
4420 static HRESULT WINAPI Uri_GetFragment(IUri *iface, BSTR *pstrFragment)
4422 TRACE("(%p)->(%p)\n", iface, pstrFragment);
4423 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_FRAGMENT, pstrFragment, 0);
4426 static HRESULT WINAPI Uri_GetHost(IUri *iface, BSTR *pstrHost)
4428 TRACE("(%p)->(%p)\n", iface, pstrHost);
4429 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_HOST, pstrHost, 0);
4432 static HRESULT WINAPI Uri_GetPassword(IUri *iface, BSTR *pstrPassword)
4434 TRACE("(%p)->(%p)\n", iface, pstrPassword);
4435 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_PASSWORD, pstrPassword, 0);
4438 static HRESULT WINAPI Uri_GetPath(IUri *iface, BSTR *pstrPath)
4440 TRACE("(%p)->(%p)\n", iface, pstrPath);
4441 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_PATH, pstrPath, 0);
4444 static HRESULT WINAPI Uri_GetPathAndQuery(IUri *iface, BSTR *pstrPathAndQuery)
4446 TRACE("(%p)->(%p)\n", iface, pstrPathAndQuery);
4447 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_PATH_AND_QUERY, pstrPathAndQuery, 0);
4450 static HRESULT WINAPI Uri_GetQuery(IUri *iface, BSTR *pstrQuery)
4452 TRACE("(%p)->(%p)\n", iface, pstrQuery);
4453 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_QUERY, pstrQuery, 0);
4456 static HRESULT WINAPI Uri_GetRawUri(IUri *iface, BSTR *pstrRawUri)
4458 TRACE("(%p)->(%p)\n", iface, pstrRawUri);
4459 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_RAW_URI, pstrRawUri, 0);
4462 static HRESULT WINAPI Uri_GetSchemeName(IUri *iface, BSTR *pstrSchemeName)
4464 TRACE("(%p)->(%p)\n", iface, pstrSchemeName);
4465 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_SCHEME_NAME, pstrSchemeName, 0);
4468 static HRESULT WINAPI Uri_GetUserInfo(IUri *iface, BSTR *pstrUserInfo)
4470 TRACE("(%p)->(%p)\n", iface, pstrUserInfo);
4471 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_USER_INFO, pstrUserInfo, 0);
4474 static HRESULT WINAPI Uri_GetUserName(IUri *iface, BSTR *pstrUserName)
4476 TRACE("(%p)->(%p)\n", iface, pstrUserName);
4477 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_USER_NAME, pstrUserName, 0);
4480 static HRESULT WINAPI Uri_GetHostType(IUri *iface, DWORD *pdwHostType)
4482 TRACE("(%p)->(%p)\n", iface, pdwHostType);
4483 return Uri_GetPropertyDWORD(iface, Uri_PROPERTY_HOST_TYPE, pdwHostType, 0);
4486 static HRESULT WINAPI Uri_GetPort(IUri *iface, DWORD *pdwPort)
4488 TRACE("(%p)->(%p)\n", iface, pdwPort);
4489 return Uri_GetPropertyDWORD(iface, Uri_PROPERTY_PORT, pdwPort, 0);
4492 static HRESULT WINAPI Uri_GetScheme(IUri *iface, DWORD *pdwScheme)
4494 Uri *This = URI_THIS(iface);
4495 TRACE("(%p)->(%p)\n", This, pdwScheme);
4496 return Uri_GetPropertyDWORD(iface, Uri_PROPERTY_SCHEME, pdwScheme, 0);
4499 static HRESULT WINAPI Uri_GetZone(IUri *iface, DWORD *pdwZone)
4501 TRACE("(%p)->(%p)\n", iface, pdwZone);
4502 return Uri_GetPropertyDWORD(iface, Uri_PROPERTY_ZONE,pdwZone, 0);
4505 static HRESULT WINAPI Uri_GetProperties(IUri *iface, DWORD *pdwProperties)
4507 Uri *This = URI_THIS(iface);
4508 TRACE("(%p)->(%p)\n", This, pdwProperties);
4510 if(!pdwProperties)
4511 return E_INVALIDARG;
4513 /* All URIs have these. */
4514 *pdwProperties = Uri_HAS_DISPLAY_URI|Uri_HAS_RAW_URI|Uri_HAS_SCHEME|Uri_HAS_HOST_TYPE;
4516 if(This->display_absolute)
4517 *pdwProperties |= Uri_HAS_ABSOLUTE_URI;
4519 if(This->scheme_start > -1)
4520 *pdwProperties |= Uri_HAS_SCHEME_NAME;
4522 if(This->authority_start > -1) {
4523 *pdwProperties |= Uri_HAS_AUTHORITY;
4524 if(This->userinfo_start > -1) {
4525 *pdwProperties |= Uri_HAS_USER_INFO;
4526 if(This->userinfo_split != 0)
4527 *pdwProperties |= Uri_HAS_USER_NAME;
4529 if(This->userinfo_split > -1)
4530 *pdwProperties |= Uri_HAS_PASSWORD;
4531 if(This->host_start > -1)
4532 *pdwProperties |= Uri_HAS_HOST;
4533 if(This->domain_offset > -1)
4534 *pdwProperties |= Uri_HAS_DOMAIN;
4537 if(This->has_port)
4538 *pdwProperties |= Uri_HAS_PORT;
4539 if(This->path_start > -1)
4540 *pdwProperties |= Uri_HAS_PATH|Uri_HAS_PATH_AND_QUERY;
4541 if(This->query_start > -1)
4542 *pdwProperties |= Uri_HAS_QUERY|Uri_HAS_PATH_AND_QUERY;
4544 if(This->extension_offset > -1)
4545 *pdwProperties |= Uri_HAS_EXTENSION;
4547 if(This->fragment_start > -1)
4548 *pdwProperties |= Uri_HAS_FRAGMENT;
4550 return S_OK;
4553 static HRESULT WINAPI Uri_IsEqual(IUri *iface, IUri *pUri, BOOL *pfEqual)
4555 Uri *This = URI_THIS(iface);
4556 Uri *other;
4558 TRACE("(%p)->(%p %p)\n", This, pUri, pfEqual);
4560 if(!pfEqual)
4561 return E_POINTER;
4563 if(!pUri) {
4564 *pfEqual = FALSE;
4566 /* For some reason Windows returns S_OK here... */
4567 return S_OK;
4570 /* Try to convert it to a Uri (allows for a more simple comparison). */
4571 if((other = get_uri_obj(pUri)))
4572 *pfEqual = are_equal_simple(This, other);
4573 else {
4574 /* Do it the hard way. */
4575 FIXME("(%p)->(%p %p) No support for unknown IUri's yet.\n", iface, pUri, pfEqual);
4576 return E_NOTIMPL;
4579 return S_OK;
4582 #undef URI_THIS
4584 static const IUriVtbl UriVtbl = {
4585 Uri_QueryInterface,
4586 Uri_AddRef,
4587 Uri_Release,
4588 Uri_GetPropertyBSTR,
4589 Uri_GetPropertyLength,
4590 Uri_GetPropertyDWORD,
4591 Uri_HasProperty,
4592 Uri_GetAbsoluteUri,
4593 Uri_GetAuthority,
4594 Uri_GetDisplayUri,
4595 Uri_GetDomain,
4596 Uri_GetExtension,
4597 Uri_GetFragment,
4598 Uri_GetHost,
4599 Uri_GetPassword,
4600 Uri_GetPath,
4601 Uri_GetPathAndQuery,
4602 Uri_GetQuery,
4603 Uri_GetRawUri,
4604 Uri_GetSchemeName,
4605 Uri_GetUserInfo,
4606 Uri_GetUserName,
4607 Uri_GetHostType,
4608 Uri_GetPort,
4609 Uri_GetScheme,
4610 Uri_GetZone,
4611 Uri_GetProperties,
4612 Uri_IsEqual
4615 /***********************************************************************
4616 * CreateUri (urlmon.@)
4618 * Creates a new IUri object using the URI represented by pwzURI. This function
4619 * parses and validates the components of pwzURI and then canonicalizes the
4620 * parsed components.
4622 * PARAMS
4623 * pwzURI [I] The URI to parse, validate, and canonicalize.
4624 * dwFlags [I] Flags which can affect how the parsing/canonicalization is performed.
4625 * dwReserved [I] Reserved (not used).
4626 * ppURI [O] The resulting IUri after parsing/canonicalization occurs.
4628 * RETURNS
4629 * Success: Returns S_OK. ppURI contains the pointer to the newly allocated IUri.
4630 * Failure: E_INVALIDARG if there's invalid flag combinations in dwFlags, or an
4631 * invalid parameters, or pwzURI doesn't represnt a valid URI.
4632 * E_OUTOFMEMORY if any memory allocation fails.
4634 * NOTES
4635 * Default flags:
4636 * Uri_CREATE_CANONICALIZE, Uri_CREATE_DECODE_EXTRA_INFO, Uri_CREATE_CRACK_UNKNOWN_SCHEMES,
4637 * Uri_CREATE_PRE_PROCESS_HTML_URI, Uri_CREATE_NO_IE_SETTINGS.
4639 HRESULT WINAPI CreateUri(LPCWSTR pwzURI, DWORD dwFlags, DWORD_PTR dwReserved, IUri **ppURI)
4641 const DWORD supported_flags = Uri_CREATE_ALLOW_RELATIVE|Uri_CREATE_ALLOW_IMPLICIT_WILDCARD_SCHEME|
4642 Uri_CREATE_ALLOW_IMPLICIT_FILE_SCHEME|Uri_CREATE_NO_CANONICALIZE|Uri_CREATE_CANONICALIZE|
4643 Uri_CREATE_DECODE_EXTRA_INFO|Uri_CREATE_NO_DECODE_EXTRA_INFO|Uri_CREATE_CRACK_UNKNOWN_SCHEMES|
4644 Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES|Uri_CREATE_PRE_PROCESS_HTML_URI|Uri_CREATE_NO_PRE_PROCESS_HTML_URI|
4645 Uri_CREATE_NO_IE_SETTINGS|Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS|Uri_CREATE_FILE_USE_DOS_PATH;
4646 Uri *ret;
4647 HRESULT hr;
4648 parse_data data;
4650 TRACE("(%s %x %x %p)\n", debugstr_w(pwzURI), dwFlags, (DWORD)dwReserved, ppURI);
4652 if(!ppURI)
4653 return E_INVALIDARG;
4655 if(!pwzURI || !*pwzURI) {
4656 *ppURI = NULL;
4657 return E_INVALIDARG;
4660 /* Check for invalid flags. */
4661 if(has_invalid_flag_combination(dwFlags)) {
4662 *ppURI = NULL;
4663 return E_INVALIDARG;
4666 /* Currently unsupported. */
4667 if(dwFlags & ~supported_flags)
4668 FIXME("Ignoring unsupported flag(s) %x\n", dwFlags & ~supported_flags);
4670 ret = heap_alloc(sizeof(Uri));
4671 if(!ret)
4672 return E_OUTOFMEMORY;
4674 ret->lpIUriVtbl = &UriVtbl;
4675 ret->ref = 1;
4677 /* Explicitly set the default flags if it doesn't cause a flag conflict. */
4678 apply_default_flags(&dwFlags);
4680 /* Pre process the URI, unless told otherwise. */
4681 if(!(dwFlags & Uri_CREATE_NO_PRE_PROCESS_HTML_URI))
4682 ret->raw_uri = pre_process_uri(pwzURI);
4683 else
4684 ret->raw_uri = SysAllocString(pwzURI);
4686 if(!ret->raw_uri) {
4687 heap_free(ret);
4688 return E_OUTOFMEMORY;
4691 memset(&data, 0, sizeof(parse_data));
4692 data.uri = ret->raw_uri;
4694 /* Validate and parse the URI into it's components. */
4695 if(!parse_uri(&data, dwFlags)) {
4696 /* Encountered an unsupported or invalid URI */
4697 SysFreeString(ret->raw_uri);
4698 heap_free(ret);
4699 *ppURI = NULL;
4700 return E_INVALIDARG;
4703 /* Canonicalize the URI. */
4704 hr = canonicalize_uri(&data, ret, dwFlags);
4705 if(FAILED(hr)) {
4706 SysFreeString(ret->raw_uri);
4707 heap_free(ret);
4708 *ppURI = NULL;
4709 return hr;
4712 ret->create_flags = dwFlags;
4714 *ppURI = URI(ret);
4715 return S_OK;
4718 /***********************************************************************
4719 * CreateUriWithFragment (urlmon.@)
4721 * Creates a new IUri object. This is almost the same as CreateUri, expect that
4722 * it allows you to explicitly specify a fragment (pwzFragment) for pwzURI.
4724 * PARAMS
4725 * pwzURI [I] The URI to parse and perform canonicalization on.
4726 * pwzFragment [I] The explict fragment string which should be added to pwzURI.
4727 * dwFlags [I] The flags which will be passed to CreateUri.
4728 * dwReserved [I] Reserved (not used).
4729 * ppURI [O] The resulting IUri after parsing/canonicalization.
4731 * RETURNS
4732 * Success: S_OK. ppURI contains the pointer to the newly allocated IUri.
4733 * Failure: E_INVALIDARG if pwzURI already contains a fragment and pwzFragment
4734 * isn't NULL. Will also return E_INVALIDARG for the same reasons as
4735 * CreateUri will. E_OUTOFMEMORY if any allocations fail.
4737 HRESULT WINAPI CreateUriWithFragment(LPCWSTR pwzURI, LPCWSTR pwzFragment, DWORD dwFlags,
4738 DWORD_PTR dwReserved, IUri **ppURI)
4740 HRESULT hres;
4741 TRACE("(%s %s %x %x %p)\n", debugstr_w(pwzURI), debugstr_w(pwzFragment), dwFlags, (DWORD)dwReserved, ppURI);
4743 if(!ppURI)
4744 return E_INVALIDARG;
4746 if(!pwzURI) {
4747 *ppURI = NULL;
4748 return E_INVALIDARG;
4751 /* Check if a fragment should be appended to the URI string. */
4752 if(pwzFragment) {
4753 WCHAR *uriW;
4754 DWORD uri_len, frag_len;
4755 BOOL add_pound;
4757 /* Check if the original URI already has a fragment component. */
4758 if(StrChrW(pwzURI, '#')) {
4759 *ppURI = NULL;
4760 return E_INVALIDARG;
4763 uri_len = lstrlenW(pwzURI);
4764 frag_len = lstrlenW(pwzFragment);
4766 /* If the fragment doesn't start with a '#', one will be added. */
4767 add_pound = *pwzFragment != '#';
4769 if(add_pound)
4770 uriW = heap_alloc((uri_len+frag_len+2)*sizeof(WCHAR));
4771 else
4772 uriW = heap_alloc((uri_len+frag_len+1)*sizeof(WCHAR));
4774 if(!uriW)
4775 return E_OUTOFMEMORY;
4777 memcpy(uriW, pwzURI, uri_len*sizeof(WCHAR));
4778 if(add_pound)
4779 uriW[uri_len++] = '#';
4780 memcpy(uriW+uri_len, pwzFragment, (frag_len+1)*sizeof(WCHAR));
4782 hres = CreateUri(uriW, dwFlags, 0, ppURI);
4784 heap_free(uriW);
4785 } else
4786 /* A fragment string wasn't specified, so just forward the call. */
4787 hres = CreateUri(pwzURI, dwFlags, 0, ppURI);
4789 return hres;
4792 #define URIBUILDER_THIS(iface) DEFINE_THIS(UriBuilder, IUriBuilder, iface)
4794 static HRESULT WINAPI UriBuilder_QueryInterface(IUriBuilder *iface, REFIID riid, void **ppv)
4796 UriBuilder *This = URIBUILDER_THIS(iface);
4798 if(IsEqualGUID(&IID_IUnknown, riid)) {
4799 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
4800 *ppv = URIBUILDER(This);
4801 }else if(IsEqualGUID(&IID_IUriBuilder, riid)) {
4802 TRACE("(%p)->(IID_IUri %p)\n", This, ppv);
4803 *ppv = URIBUILDER(This);
4804 }else {
4805 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
4806 *ppv = NULL;
4807 return E_NOINTERFACE;
4810 IUnknown_AddRef((IUnknown*)*ppv);
4811 return S_OK;
4814 static ULONG WINAPI UriBuilder_AddRef(IUriBuilder *iface)
4816 UriBuilder *This = URIBUILDER_THIS(iface);
4817 LONG ref = InterlockedIncrement(&This->ref);
4819 TRACE("(%p) ref=%d\n", This, ref);
4821 return ref;
4824 static ULONG WINAPI UriBuilder_Release(IUriBuilder *iface)
4826 UriBuilder *This = URIBUILDER_THIS(iface);
4827 LONG ref = InterlockedDecrement(&This->ref);
4829 TRACE("(%p) ref=%d\n", This, ref);
4831 if(!ref) {
4832 if(This->uri) IUri_Release(URI(This->uri));
4833 heap_free(This->fragment);
4834 heap_free(This->host);
4835 heap_free(This->password);
4836 heap_free(This->path);
4837 heap_free(This->query);
4838 heap_free(This->scheme);
4839 heap_free(This->username);
4840 heap_free(This);
4843 return ref;
4846 static HRESULT WINAPI UriBuilder_CreateUriSimple(IUriBuilder *iface,
4847 DWORD dwAllowEncodingPropertyMask,
4848 DWORD_PTR dwReserved,
4849 IUri **ppIUri)
4851 UriBuilder *This = URIBUILDER_THIS(iface);
4852 HRESULT hr;
4853 TRACE("(%p)->(%d %d %p)\n", This, dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
4855 hr = build_uri(This, ppIUri, 0, UriBuilder_USE_ORIGINAL_FLAGS, dwAllowEncodingPropertyMask);
4856 if(hr == E_NOTIMPL)
4857 FIXME("(%p)->(%d %d %p)\n", This, dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
4858 return hr;
4861 static HRESULT WINAPI UriBuilder_CreateUri(IUriBuilder *iface,
4862 DWORD dwCreateFlags,
4863 DWORD dwAllowEncodingPropertyMask,
4864 DWORD_PTR dwReserved,
4865 IUri **ppIUri)
4867 UriBuilder *This = URIBUILDER_THIS(iface);
4868 HRESULT hr;
4869 TRACE("(%p)->(0x%08x %d %d %p)\n", This, dwCreateFlags, dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
4871 if(dwCreateFlags == -1)
4872 hr = build_uri(This, ppIUri, 0, UriBuilder_USE_ORIGINAL_FLAGS, dwAllowEncodingPropertyMask);
4873 else
4874 hr = build_uri(This, ppIUri, dwCreateFlags, 0, dwAllowEncodingPropertyMask);
4876 if(hr == E_NOTIMPL)
4877 FIXME("(%p)->(0x%08x %d %d %p)\n", This, dwCreateFlags, dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
4878 return hr;
4881 static HRESULT WINAPI UriBuilder_CreateUriWithFlags(IUriBuilder *iface,
4882 DWORD dwCreateFlags,
4883 DWORD dwUriBuilderFlags,
4884 DWORD dwAllowEncodingPropertyMask,
4885 DWORD_PTR dwReserved,
4886 IUri **ppIUri)
4888 UriBuilder *This = URIBUILDER_THIS(iface);
4889 HRESULT hr;
4890 TRACE("(%p)->(0x%08x 0x%08x %d %d %p)\n", This, dwCreateFlags, dwUriBuilderFlags,
4891 dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
4893 hr = build_uri(This, ppIUri, dwCreateFlags, dwUriBuilderFlags, dwAllowEncodingPropertyMask);
4894 if(hr == E_NOTIMPL)
4895 FIXME("(%p)->(0x%08x 0x%08x %d %d %p)\n", This, dwCreateFlags, dwUriBuilderFlags,
4896 dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
4897 return hr;
4900 static HRESULT WINAPI UriBuilder_GetIUri(IUriBuilder *iface, IUri **ppIUri)
4902 UriBuilder *This = URIBUILDER_THIS(iface);
4903 TRACE("(%p)->(%p)\n", This, ppIUri);
4905 if(!ppIUri)
4906 return E_POINTER;
4908 if(This->uri) {
4909 IUri *uri = URI(This->uri);
4910 IUri_AddRef(uri);
4911 *ppIUri = uri;
4912 } else
4913 *ppIUri = NULL;
4915 return S_OK;
4918 static HRESULT WINAPI UriBuilder_SetIUri(IUriBuilder *iface, IUri *pIUri)
4920 UriBuilder *This = URIBUILDER_THIS(iface);
4921 TRACE("(%p)->(%p)\n", This, pIUri);
4923 if(pIUri) {
4924 Uri *uri;
4926 if((uri = get_uri_obj(pIUri))) {
4927 /* Only reset the builder if it's Uri isn't the same as
4928 * the Uri passed to the function.
4930 if(This->uri != uri) {
4931 reset_builder(This);
4933 This->uri = uri;
4934 if(uri->has_port)
4935 This->port = uri->port;
4937 IUri_AddRef(pIUri);
4939 } else {
4940 FIXME("(%p)->(%p) Unknown IUri types not supported yet.\n", This, pIUri);
4941 return E_NOTIMPL;
4943 } else if(This->uri)
4944 /* Only reset the builder if it's Uri isn't NULL. */
4945 reset_builder(This);
4947 return S_OK;
4950 static HRESULT WINAPI UriBuilder_GetFragment(IUriBuilder *iface, DWORD *pcchFragment, LPCWSTR *ppwzFragment)
4952 UriBuilder *This = URIBUILDER_THIS(iface);
4953 TRACE("(%p)->(%p %p)\n", This, pcchFragment, ppwzFragment);
4955 if(!This->uri || This->uri->fragment_start == -1 || This->modified_props & Uri_HAS_FRAGMENT)
4956 return get_builder_component(&This->fragment, &This->fragment_len, NULL, 0, ppwzFragment, pcchFragment);
4957 else
4958 return get_builder_component(&This->fragment, &This->fragment_len, This->uri->canon_uri+This->uri->fragment_start,
4959 This->uri->fragment_len, ppwzFragment, pcchFragment);
4962 static HRESULT WINAPI UriBuilder_GetHost(IUriBuilder *iface, DWORD *pcchHost, LPCWSTR *ppwzHost)
4964 UriBuilder *This = URIBUILDER_THIS(iface);
4965 TRACE("(%p)->(%p %p)\n", This, pcchHost, ppwzHost);
4967 if(!This->uri || This->uri->host_start == -1 || This->modified_props & Uri_HAS_HOST)
4968 return get_builder_component(&This->host, &This->host_len, NULL, 0, ppwzHost, pcchHost);
4969 else {
4970 if(This->uri->host_type == Uri_HOST_IPV6)
4971 /* Don't include the '[' and ']' around the address. */
4972 return get_builder_component(&This->host, &This->host_len, This->uri->canon_uri+This->uri->host_start+1,
4973 This->uri->host_len-2, ppwzHost, pcchHost);
4974 else
4975 return get_builder_component(&This->host, &This->host_len, This->uri->canon_uri+This->uri->host_start,
4976 This->uri->host_len, ppwzHost, pcchHost);
4980 static HRESULT WINAPI UriBuilder_GetPassword(IUriBuilder *iface, DWORD *pcchPassword, LPCWSTR *ppwzPassword)
4982 UriBuilder *This = URIBUILDER_THIS(iface);
4983 TRACE("(%p)->(%p %p)\n", This, pcchPassword, ppwzPassword);
4985 if(!This->uri || This->uri->userinfo_split == -1 || This->modified_props & Uri_HAS_PASSWORD)
4986 return get_builder_component(&This->password, &This->password_len, NULL, 0, ppwzPassword, pcchPassword);
4987 else {
4988 const WCHAR *start = This->uri->canon_uri+This->uri->userinfo_start+This->uri->userinfo_split+1;
4989 DWORD len = This->uri->userinfo_len-This->uri->userinfo_split-1;
4990 return get_builder_component(&This->password, &This->password_len, start, len, ppwzPassword, pcchPassword);
4994 static HRESULT WINAPI UriBuilder_GetPath(IUriBuilder *iface, DWORD *pcchPath, LPCWSTR *ppwzPath)
4996 UriBuilder *This = URIBUILDER_THIS(iface);
4997 TRACE("(%p)->(%p %p)\n", This, pcchPath, ppwzPath);
4999 if(!This->uri || This->uri->path_start == -1 || This->modified_props & Uri_HAS_PATH)
5000 return get_builder_component(&This->path, &This->path_len, NULL, 0, ppwzPath, pcchPath);
5001 else
5002 return get_builder_component(&This->path, &This->path_len, This->uri->canon_uri+This->uri->path_start,
5003 This->uri->path_len, ppwzPath, pcchPath);
5006 static HRESULT WINAPI UriBuilder_GetPort(IUriBuilder *iface, BOOL *pfHasPort, DWORD *pdwPort)
5008 UriBuilder *This = URIBUILDER_THIS(iface);
5009 TRACE("(%p)->(%p %p)\n", This, pfHasPort, pdwPort);
5011 if(!pfHasPort) {
5012 if(pdwPort)
5013 *pdwPort = 0;
5014 return E_POINTER;
5017 if(!pdwPort) {
5018 *pfHasPort = FALSE;
5019 return E_POINTER;
5022 *pfHasPort = This->has_port;
5023 *pdwPort = This->port;
5024 return S_OK;
5027 static HRESULT WINAPI UriBuilder_GetQuery(IUriBuilder *iface, DWORD *pcchQuery, LPCWSTR *ppwzQuery)
5029 UriBuilder *This = URIBUILDER_THIS(iface);
5030 TRACE("(%p)->(%p %p)\n", This, pcchQuery, ppwzQuery);
5032 if(!This->uri || This->uri->query_start == -1 || This->modified_props & Uri_HAS_QUERY)
5033 return get_builder_component(&This->query, &This->query_len, NULL, 0, ppwzQuery, pcchQuery);
5034 else
5035 return get_builder_component(&This->query, &This->query_len, This->uri->canon_uri+This->uri->query_start,
5036 This->uri->query_len, ppwzQuery, pcchQuery);
5039 static HRESULT WINAPI UriBuilder_GetSchemeName(IUriBuilder *iface, DWORD *pcchSchemeName, LPCWSTR *ppwzSchemeName)
5041 UriBuilder *This = URIBUILDER_THIS(iface);
5042 TRACE("(%p)->(%p %p)\n", This, pcchSchemeName, ppwzSchemeName);
5044 if(!This->uri || This->uri->scheme_start == -1 || This->modified_props & Uri_HAS_SCHEME_NAME)
5045 return get_builder_component(&This->scheme, &This->scheme_len, NULL, 0, ppwzSchemeName, pcchSchemeName);
5046 else
5047 return get_builder_component(&This->scheme, &This->scheme_len, This->uri->canon_uri+This->uri->scheme_start,
5048 This->uri->scheme_len, ppwzSchemeName, pcchSchemeName);
5051 static HRESULT WINAPI UriBuilder_GetUserName(IUriBuilder *iface, DWORD *pcchUserName, LPCWSTR *ppwzUserName)
5053 UriBuilder *This = URIBUILDER_THIS(iface);
5054 TRACE("(%p)->(%p %p)\n", This, pcchUserName, ppwzUserName);
5056 if(!This->uri || This->uri->userinfo_start == -1 || This->uri->userinfo_split == 0 ||
5057 This->modified_props & Uri_HAS_USER_NAME)
5058 return get_builder_component(&This->username, &This->username_len, NULL, 0, ppwzUserName, pcchUserName);
5059 else {
5060 const WCHAR *start = This->uri->canon_uri+This->uri->userinfo_start;
5062 /* Check if there's a password in the userinfo section. */
5063 if(This->uri->userinfo_split > -1)
5064 /* Don't include the password. */
5065 return get_builder_component(&This->username, &This->username_len, start,
5066 This->uri->userinfo_split, ppwzUserName, pcchUserName);
5067 else
5068 return get_builder_component(&This->username, &This->username_len, start,
5069 This->uri->userinfo_len, ppwzUserName, pcchUserName);
5073 static HRESULT WINAPI UriBuilder_SetFragment(IUriBuilder *iface, LPCWSTR pwzNewValue)
5075 UriBuilder *This = URIBUILDER_THIS(iface);
5076 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
5077 return set_builder_component(&This->fragment, &This->fragment_len, pwzNewValue, '#',
5078 &This->modified_props, Uri_HAS_FRAGMENT);
5081 static HRESULT WINAPI UriBuilder_SetHost(IUriBuilder *iface, LPCWSTR pwzNewValue)
5083 UriBuilder *This = URIBUILDER_THIS(iface);
5084 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
5085 return set_builder_component(&This->host, &This->host_len, pwzNewValue, 0,
5086 &This->modified_props, Uri_HAS_HOST);
5089 static HRESULT WINAPI UriBuilder_SetPassword(IUriBuilder *iface, LPCWSTR pwzNewValue)
5091 UriBuilder *This = URIBUILDER_THIS(iface);
5092 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
5093 return set_builder_component(&This->password, &This->password_len, pwzNewValue, 0,
5094 &This->modified_props, Uri_HAS_PASSWORD);
5097 static HRESULT WINAPI UriBuilder_SetPath(IUriBuilder *iface, LPCWSTR pwzNewValue)
5099 UriBuilder *This = URIBUILDER_THIS(iface);
5100 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
5101 return set_builder_component(&This->path, &This->path_len, pwzNewValue, 0,
5102 &This->modified_props, Uri_HAS_PATH);
5105 static HRESULT WINAPI UriBuilder_SetPort(IUriBuilder *iface, BOOL fHasPort, DWORD dwNewValue)
5107 UriBuilder *This = URIBUILDER_THIS(iface);
5108 TRACE("(%p)->(%d %d)\n", This, fHasPort, dwNewValue);
5110 This->has_port = fHasPort;
5111 This->port = dwNewValue;
5112 This->modified_props |= Uri_HAS_PORT;
5113 return S_OK;
5116 static HRESULT WINAPI UriBuilder_SetQuery(IUriBuilder *iface, LPCWSTR pwzNewValue)
5118 UriBuilder *This = URIBUILDER_THIS(iface);
5119 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
5120 return set_builder_component(&This->query, &This->query_len, pwzNewValue, '?',
5121 &This->modified_props, Uri_HAS_QUERY);
5124 static HRESULT WINAPI UriBuilder_SetSchemeName(IUriBuilder *iface, LPCWSTR pwzNewValue)
5126 UriBuilder *This = URIBUILDER_THIS(iface);
5127 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
5129 /* Only set the scheme name if it's not NULL or empty. */
5130 if(pwzNewValue && *pwzNewValue)
5131 return set_builder_component(&This->scheme, &This->scheme_len, pwzNewValue, 0,
5132 &This->modified_props, Uri_HAS_SCHEME_NAME);
5133 else
5134 return E_INVALIDARG;
5137 static HRESULT WINAPI UriBuilder_SetUserName(IUriBuilder *iface, LPCWSTR pwzNewValue)
5139 UriBuilder *This = URIBUILDER_THIS(iface);
5140 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
5141 return set_builder_component(&This->username, &This->username_len, pwzNewValue, 0,
5142 &This->modified_props, Uri_HAS_USER_NAME);
5145 static HRESULT WINAPI UriBuilder_RemoveProperties(IUriBuilder *iface, DWORD dwPropertyMask)
5147 const DWORD accepted_flags = Uri_HAS_AUTHORITY|Uri_HAS_DOMAIN|Uri_HAS_EXTENSION|Uri_HAS_FRAGMENT|Uri_HAS_HOST|
5148 Uri_HAS_PASSWORD|Uri_HAS_PATH|Uri_HAS_PATH_AND_QUERY|Uri_HAS_QUERY|
5149 Uri_HAS_USER_INFO|Uri_HAS_USER_NAME;
5151 UriBuilder *This = URIBUILDER_THIS(iface);
5152 TRACE("(%p)->(0x%08x)\n", This, dwPropertyMask);
5154 if(dwPropertyMask & ~accepted_flags)
5155 return E_INVALIDARG;
5157 if(dwPropertyMask & Uri_HAS_FRAGMENT)
5158 UriBuilder_SetFragment(iface, NULL);
5160 if(dwPropertyMask & Uri_HAS_HOST)
5161 UriBuilder_SetHost(iface, NULL);
5163 if(dwPropertyMask & Uri_HAS_PASSWORD)
5164 UriBuilder_SetPassword(iface, NULL);
5166 if(dwPropertyMask & Uri_HAS_PATH)
5167 UriBuilder_SetPath(iface, NULL);
5169 if(dwPropertyMask & Uri_HAS_PORT)
5170 UriBuilder_SetPort(iface, FALSE, 0);
5172 if(dwPropertyMask & Uri_HAS_QUERY)
5173 UriBuilder_SetQuery(iface, NULL);
5175 if(dwPropertyMask & Uri_HAS_USER_NAME)
5176 UriBuilder_SetUserName(iface, NULL);
5178 return S_OK;
5181 static HRESULT WINAPI UriBuilder_HasBeenModified(IUriBuilder *iface, BOOL *pfModified)
5183 UriBuilder *This = URIBUILDER_THIS(iface);
5184 TRACE("(%p)->(%p)\n", This, pfModified);
5186 if(!pfModified)
5187 return E_POINTER;
5189 *pfModified = This->modified_props > 0;
5190 return S_OK;
5193 #undef URIBUILDER_THIS
5195 static const IUriBuilderVtbl UriBuilderVtbl = {
5196 UriBuilder_QueryInterface,
5197 UriBuilder_AddRef,
5198 UriBuilder_Release,
5199 UriBuilder_CreateUriSimple,
5200 UriBuilder_CreateUri,
5201 UriBuilder_CreateUriWithFlags,
5202 UriBuilder_GetIUri,
5203 UriBuilder_SetIUri,
5204 UriBuilder_GetFragment,
5205 UriBuilder_GetHost,
5206 UriBuilder_GetPassword,
5207 UriBuilder_GetPath,
5208 UriBuilder_GetPort,
5209 UriBuilder_GetQuery,
5210 UriBuilder_GetSchemeName,
5211 UriBuilder_GetUserName,
5212 UriBuilder_SetFragment,
5213 UriBuilder_SetHost,
5214 UriBuilder_SetPassword,
5215 UriBuilder_SetPath,
5216 UriBuilder_SetPort,
5217 UriBuilder_SetQuery,
5218 UriBuilder_SetSchemeName,
5219 UriBuilder_SetUserName,
5220 UriBuilder_RemoveProperties,
5221 UriBuilder_HasBeenModified,
5224 /***********************************************************************
5225 * CreateIUriBuilder (urlmon.@)
5227 HRESULT WINAPI CreateIUriBuilder(IUri *pIUri, DWORD dwFlags, DWORD_PTR dwReserved, IUriBuilder **ppIUriBuilder)
5229 UriBuilder *ret;
5231 TRACE("(%p %x %x %p)\n", pIUri, dwFlags, (DWORD)dwReserved, ppIUriBuilder);
5233 if(!ppIUriBuilder)
5234 return E_POINTER;
5236 ret = heap_alloc_zero(sizeof(UriBuilder));
5237 if(!ret)
5238 return E_OUTOFMEMORY;
5240 ret->lpIUriBuilderVtbl = &UriBuilderVtbl;
5241 ret->ref = 1;
5243 if(pIUri) {
5244 Uri *uri;
5246 if((uri = get_uri_obj(pIUri))) {
5247 IUri_AddRef(pIUri);
5248 ret->uri = uri;
5250 if(uri->has_port)
5251 /* Windows doesn't set 'has_port' to TRUE in this case. */
5252 ret->port = uri->port;
5254 } else {
5255 heap_free(ret);
5256 *ppIUriBuilder = NULL;
5257 FIXME("(%p %x %x %p): Unknown IUri types not supported yet.\n", pIUri, dwFlags,
5258 (DWORD)dwReserved, ppIUriBuilder);
5259 return E_NOTIMPL;
5263 *ppIUriBuilder = URIBUILDER(ret);
5264 return S_OK;