urlmon: Some IUri's don't display the default port in the authority.
[wine/multimedia.git] / dlls / urlmon / uri.c
blob9b25f69b6d7132cf07fb3f96e2a97eea898efb76
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 URI_DISPLAY_NO_ABSOLUTE_URI 0x1
30 #define URI_DISPLAY_NO_DEFAULT_PORT_AUTH 0x2
32 #define ALLOW_NULL_TERM_SCHEME 0x01
33 #define ALLOW_NULL_TERM_USER_NAME 0x02
34 #define ALLOW_NULL_TERM_PASSWORD 0x04
35 #define ALLOW_BRACKETLESS_IP_LITERAL 0x08
36 #define SKIP_IP_FUTURE_CHECK 0x10
37 #define IGNORE_PORT_DELIMITER 0x20
39 #define RAW_URI_FORCE_PORT_DISP 0x1
41 WINE_DEFAULT_DEBUG_CHANNEL(urlmon);
43 static const IID IID_IUriObj = {0x4b364760,0x9f51,0x11df,{0x98,0x1c,0x08,0x00,0x20,0x0c,0x9a,0x66}};
45 typedef struct {
46 const IUriVtbl *lpIUriVtbl;
47 LONG ref;
49 BSTR raw_uri;
51 /* Information about the canonicalized URI's buffer. */
52 WCHAR *canon_uri;
53 DWORD canon_size;
54 DWORD canon_len;
55 BOOL display_modifiers;
56 DWORD create_flags;
58 INT scheme_start;
59 DWORD scheme_len;
60 URL_SCHEME scheme_type;
62 INT userinfo_start;
63 DWORD userinfo_len;
64 INT userinfo_split;
66 INT host_start;
67 DWORD host_len;
68 Uri_HOST_TYPE host_type;
70 INT port_offset;
71 DWORD port;
72 BOOL has_port;
74 INT authority_start;
75 DWORD authority_len;
77 INT domain_offset;
79 INT path_start;
80 DWORD path_len;
81 INT extension_offset;
83 INT query_start;
84 DWORD query_len;
86 INT fragment_start;
87 DWORD fragment_len;
88 } Uri;
90 typedef struct {
91 const IUriBuilderVtbl *lpIUriBuilderVtbl;
92 LONG ref;
94 Uri *uri;
95 DWORD modified_props;
97 WCHAR *fragment;
98 DWORD fragment_len;
100 WCHAR *host;
101 DWORD host_len;
103 WCHAR *password;
104 DWORD password_len;
106 WCHAR *path;
107 DWORD path_len;
109 BOOL has_port;
110 DWORD port;
112 WCHAR *query;
113 DWORD query_len;
115 WCHAR *scheme;
116 DWORD scheme_len;
118 WCHAR *username;
119 DWORD username_len;
120 } UriBuilder;
122 typedef struct {
123 const WCHAR *str;
124 DWORD len;
125 } h16;
127 typedef struct {
128 /* IPv6 addresses can hold up to 8 h16 components. */
129 h16 components[8];
130 DWORD h16_count;
132 /* An IPv6 can have 1 elision ("::"). */
133 const WCHAR *elision;
135 /* An IPv6 can contain 1 IPv4 address as the last 32bits of the address. */
136 const WCHAR *ipv4;
137 DWORD ipv4_len;
139 INT components_size;
140 INT elision_size;
141 } ipv6_address;
143 typedef struct {
144 BSTR uri;
146 BOOL is_relative;
147 BOOL is_opaque;
148 BOOL has_implicit_scheme;
149 BOOL has_implicit_ip;
150 UINT implicit_ipv4;
152 const WCHAR *scheme;
153 DWORD scheme_len;
154 URL_SCHEME scheme_type;
156 const WCHAR *username;
157 DWORD username_len;
159 const WCHAR *password;
160 DWORD password_len;
162 const WCHAR *host;
163 DWORD host_len;
164 Uri_HOST_TYPE host_type;
166 BOOL has_ipv6;
167 ipv6_address ipv6_address;
169 BOOL has_port;
170 const WCHAR *port;
171 DWORD port_len;
172 DWORD port_value;
174 const WCHAR *path;
175 DWORD path_len;
177 const WCHAR *query;
178 DWORD query_len;
180 const WCHAR *fragment;
181 DWORD fragment_len;
182 } parse_data;
184 static const CHAR hexDigits[] = "0123456789ABCDEF";
186 /* List of scheme types/scheme names that are recognized by the IUri interface as of IE 7. */
187 static const struct {
188 URL_SCHEME scheme;
189 WCHAR scheme_name[16];
190 } recognized_schemes[] = {
191 {URL_SCHEME_FTP, {'f','t','p',0}},
192 {URL_SCHEME_HTTP, {'h','t','t','p',0}},
193 {URL_SCHEME_GOPHER, {'g','o','p','h','e','r',0}},
194 {URL_SCHEME_MAILTO, {'m','a','i','l','t','o',0}},
195 {URL_SCHEME_NEWS, {'n','e','w','s',0}},
196 {URL_SCHEME_NNTP, {'n','n','t','p',0}},
197 {URL_SCHEME_TELNET, {'t','e','l','n','e','t',0}},
198 {URL_SCHEME_WAIS, {'w','a','i','s',0}},
199 {URL_SCHEME_FILE, {'f','i','l','e',0}},
200 {URL_SCHEME_MK, {'m','k',0}},
201 {URL_SCHEME_HTTPS, {'h','t','t','p','s',0}},
202 {URL_SCHEME_SHELL, {'s','h','e','l','l',0}},
203 {URL_SCHEME_SNEWS, {'s','n','e','w','s',0}},
204 {URL_SCHEME_LOCAL, {'l','o','c','a','l',0}},
205 {URL_SCHEME_JAVASCRIPT, {'j','a','v','a','s','c','r','i','p','t',0}},
206 {URL_SCHEME_VBSCRIPT, {'v','b','s','c','r','i','p','t',0}},
207 {URL_SCHEME_ABOUT, {'a','b','o','u','t',0}},
208 {URL_SCHEME_RES, {'r','e','s',0}},
209 {URL_SCHEME_MSSHELLROOTED, {'m','s','-','s','h','e','l','l','-','r','o','o','t','e','d',0}},
210 {URL_SCHEME_MSSHELLIDLIST, {'m','s','-','s','h','e','l','l','-','i','d','l','i','s','t',0}},
211 {URL_SCHEME_MSHELP, {'h','c','p',0}},
212 {URL_SCHEME_WILDCARD, {'*',0}}
215 /* List of default ports Windows recognizes. */
216 static const struct {
217 URL_SCHEME scheme;
218 USHORT port;
219 } default_ports[] = {
220 {URL_SCHEME_FTP, 21},
221 {URL_SCHEME_HTTP, 80},
222 {URL_SCHEME_GOPHER, 70},
223 {URL_SCHEME_NNTP, 119},
224 {URL_SCHEME_TELNET, 23},
225 {URL_SCHEME_WAIS, 210},
226 {URL_SCHEME_HTTPS, 443},
229 /* List of 3 character top level domain names Windows seems to recognize.
230 * There might be more, but, these are the only ones I've found so far.
232 static const struct {
233 WCHAR tld_name[4];
234 } recognized_tlds[] = {
235 {{'c','o','m',0}},
236 {{'e','d','u',0}},
237 {{'g','o','v',0}},
238 {{'i','n','t',0}},
239 {{'m','i','l',0}},
240 {{'n','e','t',0}},
241 {{'o','r','g',0}}
244 static Uri *get_uri_obj(IUri *uri)
246 Uri *ret;
247 HRESULT hres;
249 hres = IUri_QueryInterface(uri, &IID_IUriObj, (void**)&ret);
250 return SUCCEEDED(hres) ? ret : NULL;
253 static inline BOOL is_alpha(WCHAR val) {
254 return ((val >= 'a' && val <= 'z') || (val >= 'A' && val <= 'Z'));
257 static inline BOOL is_num(WCHAR val) {
258 return (val >= '0' && val <= '9');
261 static inline BOOL is_drive_path(const WCHAR *str) {
262 return (is_alpha(str[0]) && (str[1] == ':' || str[1] == '|'));
265 static inline BOOL is_unc_path(const WCHAR *str) {
266 return (str[0] == '\\' && str[0] == '\\');
269 static inline BOOL is_forbidden_dos_path_char(WCHAR val) {
270 return (val == '>' || val == '<' || val == '\"');
273 /* A URI is implicitly a file path if it begins with
274 * a drive letter (eg X:) or starts with "\\" (UNC path).
276 static inline BOOL is_implicit_file_path(const WCHAR *str) {
277 return (is_unc_path(str) || (is_alpha(str[0]) && str[1] == ':'));
280 /* Checks if the URI is a hierarchical URI. A hierarchical
281 * URI is one that has "//" after the scheme.
283 static BOOL check_hierarchical(const WCHAR **ptr) {
284 const WCHAR *start = *ptr;
286 if(**ptr != '/')
287 return FALSE;
289 ++(*ptr);
290 if(**ptr != '/') {
291 *ptr = start;
292 return FALSE;
295 ++(*ptr);
296 return TRUE;
299 /* unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~" */
300 static inline BOOL is_unreserved(WCHAR val) {
301 return (is_alpha(val) || is_num(val) || val == '-' || val == '.' ||
302 val == '_' || val == '~');
305 /* sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
306 * / "*" / "+" / "," / ";" / "="
308 static inline BOOL is_subdelim(WCHAR val) {
309 return (val == '!' || val == '$' || val == '&' ||
310 val == '\'' || val == '(' || val == ')' ||
311 val == '*' || val == '+' || val == ',' ||
312 val == ';' || val == '=');
315 /* gen-delims = ":" / "/" / "?" / "#" / "[" / "]" / "@" */
316 static inline BOOL is_gendelim(WCHAR val) {
317 return (val == ':' || val == '/' || val == '?' ||
318 val == '#' || val == '[' || val == ']' ||
319 val == '@');
322 /* Characters that delimit the end of the authority
323 * section of a URI. Sometimes a '\\' is considered
324 * an authority delimeter.
326 static inline BOOL is_auth_delim(WCHAR val, BOOL acceptSlash) {
327 return (val == '#' || val == '/' || val == '?' ||
328 val == '\0' || (acceptSlash && val == '\\'));
331 /* reserved = gen-delims / sub-delims */
332 static inline BOOL is_reserved(WCHAR val) {
333 return (is_subdelim(val) || is_gendelim(val));
336 static inline BOOL is_hexdigit(WCHAR val) {
337 return ((val >= 'a' && val <= 'f') ||
338 (val >= 'A' && val <= 'F') ||
339 (val >= '0' && val <= '9'));
342 static inline BOOL is_path_delim(WCHAR val) {
343 return (!val || val == '#' || val == '?');
346 static BOOL is_default_port(URL_SCHEME scheme, DWORD port) {
347 DWORD i;
349 for(i = 0; i < sizeof(default_ports)/sizeof(default_ports[0]); ++i) {
350 if(default_ports[i].scheme == scheme && default_ports[i].port)
351 return TRUE;
354 return FALSE;
357 /* List of schemes types Windows seems to expect to be hierarchical. */
358 static inline BOOL is_hierarchical_scheme(URL_SCHEME type) {
359 return(type == URL_SCHEME_HTTP || type == URL_SCHEME_FTP ||
360 type == URL_SCHEME_GOPHER || type == URL_SCHEME_NNTP ||
361 type == URL_SCHEME_TELNET || type == URL_SCHEME_WAIS ||
362 type == URL_SCHEME_FILE || type == URL_SCHEME_HTTPS ||
363 type == URL_SCHEME_RES);
366 /* Checks if 'flags' contains an invalid combination of Uri_CREATE flags. */
367 static inline BOOL has_invalid_flag_combination(DWORD flags) {
368 return((flags & Uri_CREATE_DECODE_EXTRA_INFO && flags & Uri_CREATE_NO_DECODE_EXTRA_INFO) ||
369 (flags & Uri_CREATE_CANONICALIZE && flags & Uri_CREATE_NO_CANONICALIZE) ||
370 (flags & Uri_CREATE_CRACK_UNKNOWN_SCHEMES && flags & Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES) ||
371 (flags & Uri_CREATE_PRE_PROCESS_HTML_URI && flags & Uri_CREATE_NO_PRE_PROCESS_HTML_URI) ||
372 (flags & Uri_CREATE_IE_SETTINGS && flags & Uri_CREATE_NO_IE_SETTINGS));
375 /* Applies each default Uri_CREATE flags to 'flags' if it
376 * doesn't cause a flag conflict.
378 static void apply_default_flags(DWORD *flags) {
379 if(!(*flags & Uri_CREATE_NO_CANONICALIZE))
380 *flags |= Uri_CREATE_CANONICALIZE;
381 if(!(*flags & Uri_CREATE_NO_DECODE_EXTRA_INFO))
382 *flags |= Uri_CREATE_DECODE_EXTRA_INFO;
383 if(!(*flags & Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES))
384 *flags |= Uri_CREATE_CRACK_UNKNOWN_SCHEMES;
385 if(!(*flags & Uri_CREATE_NO_PRE_PROCESS_HTML_URI))
386 *flags |= Uri_CREATE_PRE_PROCESS_HTML_URI;
387 if(!(*flags & Uri_CREATE_IE_SETTINGS))
388 *flags |= Uri_CREATE_NO_IE_SETTINGS;
391 /* Determines if the URI is hierarchical using the information already parsed into
392 * data and using the current location of parsing in the URI string.
394 * Windows considers a URI hierarchical if on of the following is true:
395 * A.) It's a wildcard scheme.
396 * B.) It's an implicit file scheme.
397 * C.) It's a known hierarchical scheme and it has two '\\' after the scheme name.
398 * (the '\\' will be converted into "//" during canonicalization).
399 * D.) It's not a relative URI and "//" appears after the scheme name.
401 static inline BOOL is_hierarchical_uri(const WCHAR **ptr, const parse_data *data) {
402 const WCHAR *start = *ptr;
404 if(data->scheme_type == URL_SCHEME_WILDCARD)
405 return TRUE;
406 else if(data->scheme_type == URL_SCHEME_FILE && data->has_implicit_scheme)
407 return TRUE;
408 else if(is_hierarchical_scheme(data->scheme_type) && (*ptr)[0] == '\\' && (*ptr)[1] == '\\') {
409 *ptr += 2;
410 return TRUE;
411 } else if(!data->is_relative && check_hierarchical(ptr))
412 return TRUE;
414 *ptr = start;
415 return FALSE;
418 /* Checks if the two Uri's are logically equivalent. It's a simple
419 * comparison, since they are both of type Uri, and it can access
420 * the properties of each Uri directly without the need to go
421 * through the "IUri_Get*" interface calls.
423 static BOOL are_equal_simple(const Uri *a, const Uri *b) {
424 if(a->scheme_type == b->scheme_type) {
425 const BOOL known_scheme = a->scheme_type != URL_SCHEME_UNKNOWN;
426 const BOOL are_hierarchical =
427 (a->authority_start > -1 && b->authority_start > -1);
429 if(a->scheme_type == URL_SCHEME_FILE) {
430 if(a->canon_len == b->canon_len)
431 return !StrCmpIW(a->canon_uri, b->canon_uri);
434 /* Only compare the scheme names (if any) if their unknown scheme types. */
435 if(!known_scheme) {
436 if((a->scheme_start > -1 && b->scheme_start > -1) &&
437 (a->scheme_len == b->scheme_len)) {
438 /* Make sure the schemes are the same. */
439 if(StrCmpNW(a->canon_uri+a->scheme_start, b->canon_uri+b->scheme_start, a->scheme_len))
440 return FALSE;
441 } else if(a->scheme_len != b->scheme_len)
442 /* One of the Uri's has a scheme name, while the other doesn't. */
443 return FALSE;
446 /* If they have a userinfo component, perform case sensitive compare. */
447 if((a->userinfo_start > -1 && b->userinfo_start > -1) &&
448 (a->userinfo_len == b->userinfo_len)) {
449 if(StrCmpNW(a->canon_uri+a->userinfo_start, b->canon_uri+b->userinfo_start, a->userinfo_len))
450 return FALSE;
451 } else if(a->userinfo_len != b->userinfo_len)
452 /* One of the Uri's had a userinfo, while the other one doesn't. */
453 return FALSE;
455 /* Check if they have a host name. */
456 if((a->host_start > -1 && b->host_start > -1) &&
457 (a->host_len == b->host_len)) {
458 /* Perform a case insensitive compare if they are a known scheme type. */
459 if(known_scheme) {
460 if(StrCmpNIW(a->canon_uri+a->host_start, b->canon_uri+b->host_start, a->host_len))
461 return FALSE;
462 } else if(StrCmpNW(a->canon_uri+a->host_start, b->canon_uri+b->host_start, a->host_len))
463 return FALSE;
464 } else if(a->host_len != b->host_len)
465 /* One of the Uri's had a host, while the other one didn't. */
466 return FALSE;
468 if(a->has_port && b->has_port) {
469 if(a->port != b->port)
470 return FALSE;
471 } else if(a->has_port || b->has_port)
472 /* One had a port, while the other one didn't. */
473 return FALSE;
475 /* Windows is weird with how it handles paths. For example
476 * One URI could be "http://google.com" (after canonicalization)
477 * and one could be "http://google.com/" and the IsEqual function
478 * would still evaluate to TRUE, but, only if they are both hierarchical
479 * URIs.
481 if((a->path_start > -1 && b->path_start > -1) &&
482 (a->path_len == b->path_len)) {
483 if(StrCmpNW(a->canon_uri+a->path_start, b->canon_uri+b->path_start, a->path_len))
484 return FALSE;
485 } else if(are_hierarchical && a->path_len == -1 && b->path_len == 0) {
486 if(*(a->canon_uri+a->path_start) != '/')
487 return FALSE;
488 } else if(are_hierarchical && b->path_len == 1 && a->path_len == 0) {
489 if(*(b->canon_uri+b->path_start) != '/')
490 return FALSE;
491 } else if(a->path_len != b->path_len)
492 return FALSE;
494 /* Compare the query strings of the two URIs. */
495 if((a->query_start > -1 && b->query_start > -1) &&
496 (a->query_len == b->query_len)) {
497 if(StrCmpNW(a->canon_uri+a->query_start, b->canon_uri+b->query_start, a->query_len))
498 return FALSE;
499 } else if(a->query_len != b->query_len)
500 return FALSE;
502 if((a->fragment_start > -1 && b->fragment_start > -1) &&
503 (a->fragment_len == b->fragment_len)) {
504 if(StrCmpNW(a->canon_uri+a->fragment_start, b->canon_uri+b->fragment_start, a->fragment_len))
505 return FALSE;
506 } else if(a->fragment_len != b->fragment_len)
507 return FALSE;
509 /* If we get here, the two URIs are equivalent. */
510 return TRUE;
513 return FALSE;
516 /* Computes the size of the given IPv6 address.
517 * Each h16 component is 16bits, if there is an IPv4 address, it's
518 * 32bits. If there's an elision it can be 16bits to 128bits, depending
519 * on the number of other components.
521 * Modeled after google-url's CheckIPv6ComponentsSize function
523 static void compute_ipv6_comps_size(ipv6_address *address) {
524 address->components_size = address->h16_count * 2;
526 if(address->ipv4)
527 /* IPv4 address is 4 bytes. */
528 address->components_size += 4;
530 if(address->elision) {
531 /* An elision can be anywhere from 2 bytes up to 16 bytes.
532 * It size depends on the size of the h16 and IPv4 components.
534 address->elision_size = 16 - address->components_size;
535 if(address->elision_size < 2)
536 address->elision_size = 2;
537 } else
538 address->elision_size = 0;
541 /* Taken from dlls/jscript/lex.c */
542 static int hex_to_int(WCHAR val) {
543 if(val >= '0' && val <= '9')
544 return val - '0';
545 else if(val >= 'a' && val <= 'f')
546 return val - 'a' + 10;
547 else if(val >= 'A' && val <= 'F')
548 return val - 'A' + 10;
550 return -1;
553 /* Helper function for converting a percent encoded string
554 * representation of a WCHAR value into its actual WCHAR value. If
555 * the two characters following the '%' aren't valid hex values then
556 * this function returns the NULL character.
558 * Eg.
559 * "%2E" will result in '.' being returned by this function.
561 static WCHAR decode_pct_val(const WCHAR *ptr) {
562 WCHAR ret = '\0';
564 if(*ptr == '%' && is_hexdigit(*(ptr + 1)) && is_hexdigit(*(ptr + 2))) {
565 INT a = hex_to_int(*(ptr + 1));
566 INT b = hex_to_int(*(ptr + 2));
568 ret = a << 4;
569 ret += b;
572 return ret;
575 /* Helper function for percent encoding a given character
576 * and storing the encoded value into a given buffer (dest).
578 * It's up to the calling function to ensure that there is
579 * at least enough space in 'dest' for the percent encoded
580 * value to be stored (so dest + 3 spaces available).
582 static inline void pct_encode_val(WCHAR val, WCHAR *dest) {
583 dest[0] = '%';
584 dest[1] = hexDigits[(val >> 4) & 0xf];
585 dest[2] = hexDigits[val & 0xf];
588 /* Scans the range of characters [str, end] and returns the last occurrence
589 * of 'ch' or returns NULL.
591 static const WCHAR *str_last_of(const WCHAR *str, const WCHAR *end, WCHAR ch) {
592 const WCHAR *ptr = end;
594 while(ptr >= str) {
595 if(*ptr == ch)
596 return ptr;
597 --ptr;
600 return NULL;
603 /* Attempts to parse the domain name from the host.
605 * This function also includes the Top-level Domain (TLD) name
606 * of the host when it tries to find the domain name. If it finds
607 * a valid domain name it will assign 'domain_start' the offset
608 * into 'host' where the domain name starts.
610 * It's implied that if a domain name its range is implied to be
611 * [host+domain_start, host+host_len).
613 static void find_domain_name(const WCHAR *host, DWORD host_len,
614 INT *domain_start) {
615 const WCHAR *last_tld, *sec_last_tld, *end;
617 end = host+host_len-1;
619 *domain_start = -1;
621 /* There has to be at least enough room for a '.' followed by a
622 * 3 character TLD for a domain to even exist in the host name.
624 if(host_len < 4)
625 return;
627 last_tld = str_last_of(host, end, '.');
628 if(!last_tld)
629 /* http://hostname -> has no domain name. */
630 return;
632 sec_last_tld = str_last_of(host, last_tld-1, '.');
633 if(!sec_last_tld) {
634 /* If the '.' is at the beginning of the host there
635 * has to be at least 3 characters in the TLD for it
636 * to be valid.
637 * Ex: .com -> .com as the domain name.
638 * .co -> has no domain name.
640 if(last_tld-host == 0) {
641 if(end-(last_tld-1) < 3)
642 return;
643 } else if(last_tld-host == 3) {
644 DWORD i;
646 /* If there's three characters in front of last_tld and
647 * they are on the list of recognized TLDs, then this
648 * host doesn't have a domain (since the host only contains
649 * a TLD name.
650 * Ex: edu.uk -> has no domain name.
651 * foo.uk -> foo.uk as the domain name.
653 for(i = 0; i < sizeof(recognized_tlds)/sizeof(recognized_tlds[0]); ++i) {
654 if(!StrCmpNIW(host, recognized_tlds[i].tld_name, 3))
655 return;
657 } else if(last_tld-host < 3)
658 /* Anything less than 3 characters is considered part
659 * of the TLD name.
660 * Ex: ak.uk -> Has no domain name.
662 return;
664 /* Otherwise the domain name is the whole host name. */
665 *domain_start = 0;
666 } else if(end+1-last_tld > 3) {
667 /* If the last_tld has more than 3 characters, then it's automatically
668 * considered the TLD of the domain name.
669 * Ex: www.winehq.org.uk.test -> uk.test as the domain name.
671 *domain_start = (sec_last_tld+1)-host;
672 } else if(last_tld - (sec_last_tld+1) < 4) {
673 DWORD i;
674 /* If the sec_last_tld is 3 characters long it HAS to be on the list of
675 * recognized to still be considered part of the TLD name, otherwise
676 * its considered the domain name.
677 * Ex: www.google.com.uk -> google.com.uk as the domain name.
678 * www.google.foo.uk -> foo.uk as the domain name.
680 if(last_tld - (sec_last_tld+1) == 3) {
681 for(i = 0; i < sizeof(recognized_tlds)/sizeof(recognized_tlds[0]); ++i) {
682 if(!StrCmpNIW(sec_last_tld+1, recognized_tlds[i].tld_name, 3)) {
683 const WCHAR *domain = str_last_of(host, sec_last_tld-1, '.');
685 if(!domain)
686 *domain_start = 0;
687 else
688 *domain_start = (domain+1) - host;
689 TRACE("Found domain name %s\n", debugstr_wn(host+*domain_start,
690 (host+host_len)-(host+*domain_start)));
691 return;
695 *domain_start = (sec_last_tld+1)-host;
696 } else {
697 /* Since the sec_last_tld is less than 3 characters it's considered
698 * part of the TLD.
699 * Ex: www.google.fo.uk -> google.fo.uk as the domain name.
701 const WCHAR *domain = str_last_of(host, sec_last_tld-1, '.');
703 if(!domain)
704 *domain_start = 0;
705 else
706 *domain_start = (domain+1) - host;
708 } else {
709 /* The second to last TLD has more than 3 characters making it
710 * the domain name.
711 * Ex: www.google.test.us -> test.us as the domain name.
713 *domain_start = (sec_last_tld+1)-host;
716 TRACE("Found domain name %s\n", debugstr_wn(host+*domain_start,
717 (host+host_len)-(host+*domain_start)));
720 /* Removes the dot segments from a hierarchical URIs path component. This
721 * function performs the removal in place.
723 * This is a modified version of Qt's QUrl function "removeDotsFromPath".
725 * This function returns the new length of the path string.
727 static DWORD remove_dot_segments(WCHAR *path, DWORD path_len) {
728 WCHAR *out = path;
729 const WCHAR *in = out;
730 const WCHAR *end = out + path_len;
731 DWORD len;
733 while(in < end) {
734 /* A. if the input buffer begins with a prefix of "/./" or "/.",
735 * where "." is a complete path segment, then replace that
736 * prefix with "/" in the input buffer; otherwise,
738 if(in <= end - 3 && in[0] == '/' && in[1] == '.' && in[2] == '/') {
739 in += 2;
740 continue;
741 } else if(in == end - 2 && in[0] == '/' && in[1] == '.') {
742 *out++ = '/';
743 in += 2;
744 break;
747 /* B. if the input buffer begins with a prefix of "/../" or "/..",
748 * where ".." is a complete path segment, then replace that
749 * prefix with "/" in the input buffer and remove the last
750 * segment and its preceding "/" (if any) from the output
751 * buffer; otherwise,
753 if(in <= end - 4 && in[0] == '/' && in[1] == '.' && in[2] == '.' && in[3] == '/') {
754 while(out > path && *(--out) != '/');
756 in += 3;
757 continue;
758 } else if(in == end - 3 && in[0] == '/' && in[1] == '.' && in[2] == '.') {
759 while(out > path && *(--out) != '/');
761 if(*out == '/')
762 ++out;
764 in += 3;
765 break;
768 /* C. move the first path segment in the input buffer to the end of
769 * the output buffer, including the initial "/" character (if
770 * any) and any subsequent characters up to, but not including,
771 * the next "/" character or the end of the input buffer.
773 *out++ = *in++;
774 while(in < end && *in != '/')
775 *out++ = *in++;
778 len = out - path;
779 TRACE("(%p %d): Path after dot segments removed %s len=%d\n", path, path_len,
780 debugstr_wn(path, len), len);
781 return len;
784 /* Attempts to find the file extension in a given path. */
785 static INT find_file_extension(const WCHAR *path, DWORD path_len) {
786 const WCHAR *end;
788 for(end = path+path_len-1; end >= path && *end != '/' && *end != '\\'; --end) {
789 if(*end == '.')
790 return end-path;
793 return -1;
796 /* Computes the location where the elision should occur in the IPv6
797 * address using the numerical values of each component stored in
798 * 'values'. If the address shouldn't contain an elision then 'index'
799 * is assigned -1 as it's value. Otherwise 'index' will contain the
800 * starting index (into values) where the elision should be, and 'count'
801 * will contain the number of cells the elision covers.
803 * NOTES:
804 * Windows will expand an elision if the elision only represents 1 h16
805 * component of the URI.
807 * Ex: [1::2:3:4:5:6:7] -> [1:0:2:3:4:5:6:7]
809 * If the IPv6 address contains an IPv4 address, the IPv4 address is also
810 * considered for being included as part of an elision if all it's components
811 * are zeros.
813 * Ex: [1:2:3:4:5:6:0.0.0.0] -> [1:2:3:4:5:6::]
815 static void compute_elision_location(const ipv6_address *address, const USHORT values[8],
816 INT *index, DWORD *count) {
817 DWORD i, max_len, cur_len;
818 INT max_index, cur_index;
820 max_len = cur_len = 0;
821 max_index = cur_index = -1;
822 for(i = 0; i < 8; ++i) {
823 BOOL check_ipv4 = (address->ipv4 && i == 6);
824 BOOL is_end = (check_ipv4 || i == 7);
826 if(check_ipv4) {
827 /* Check if the IPv4 address contains only zeros. */
828 if(values[i] == 0 && values[i+1] == 0) {
829 if(cur_index == -1)
830 cur_index = i;
832 cur_len += 2;
833 ++i;
835 } else if(values[i] == 0) {
836 if(cur_index == -1)
837 cur_index = i;
839 ++cur_len;
842 if(is_end || values[i] != 0) {
843 /* We only consider it for an elision if it's
844 * more than 1 component long.
846 if(cur_len > 1 && cur_len > max_len) {
847 /* Found the new elision location. */
848 max_len = cur_len;
849 max_index = cur_index;
852 /* Reset the current range for the next range of zeros. */
853 cur_index = -1;
854 cur_len = 0;
858 *index = max_index;
859 *count = max_len;
862 /* Removes all the leading and trailing white spaces or
863 * control characters from the URI and removes all control
864 * characters inside of the URI string.
866 static BSTR pre_process_uri(LPCWSTR uri) {
867 BSTR ret;
868 DWORD len;
869 const WCHAR *start, *end;
870 WCHAR *buf, *ptr;
872 len = lstrlenW(uri);
874 start = uri;
875 /* Skip leading controls and whitespace. */
876 while(iscntrlW(*start) || isspaceW(*start)) ++start;
878 end = uri+len-1;
879 if(start == end)
880 /* URI consisted only of control/whitespace. */
881 ret = SysAllocStringLen(NULL, 0);
882 else {
883 while(iscntrlW(*end) || isspaceW(*end)) --end;
885 buf = heap_alloc(((end+1)-start)*sizeof(WCHAR));
886 if(!buf)
887 return NULL;
889 for(ptr = buf; start < end+1; ++start) {
890 if(!iscntrlW(*start))
891 *ptr++ = *start;
894 ret = SysAllocStringLen(buf, ptr-buf);
895 heap_free(buf);
898 return ret;
901 /* Converts the specified IPv4 address into an uint value.
903 * This function assumes that the IPv4 address has already been validated.
905 static UINT ipv4toui(const WCHAR *ip, DWORD len) {
906 UINT ret = 0;
907 DWORD comp_value = 0;
908 const WCHAR *ptr;
910 for(ptr = ip; ptr < ip+len; ++ptr) {
911 if(*ptr == '.') {
912 ret <<= 8;
913 ret += comp_value;
914 comp_value = 0;
915 } else
916 comp_value = comp_value*10 + (*ptr-'0');
919 ret <<= 8;
920 ret += comp_value;
922 return ret;
925 /* Converts an IPv4 address in numerical form into it's fully qualified
926 * string form. This function returns the number of characters written
927 * to 'dest'. If 'dest' is NULL this function will return the number of
928 * characters that would have been written.
930 * It's up to the caller to ensure there's enough space in 'dest' for the
931 * address.
933 static DWORD ui2ipv4(WCHAR *dest, UINT address) {
934 static const WCHAR formatW[] =
935 {'%','u','.','%','u','.','%','u','.','%','u',0};
936 DWORD ret = 0;
937 UCHAR digits[4];
939 digits[0] = (address >> 24) & 0xff;
940 digits[1] = (address >> 16) & 0xff;
941 digits[2] = (address >> 8) & 0xff;
942 digits[3] = address & 0xff;
944 if(!dest) {
945 WCHAR tmp[16];
946 ret = sprintfW(tmp, formatW, digits[0], digits[1], digits[2], digits[3]);
947 } else
948 ret = sprintfW(dest, formatW, digits[0], digits[1], digits[2], digits[3]);
950 return ret;
953 static DWORD ui2str(WCHAR *dest, UINT value) {
954 static const WCHAR formatW[] = {'%','u',0};
955 DWORD ret = 0;
957 if(!dest) {
958 WCHAR tmp[11];
959 ret = sprintfW(tmp, formatW, value);
960 } else
961 ret = sprintfW(dest, formatW, value);
963 return ret;
966 /* Converts an h16 component (from an IPv6 address) into it's
967 * numerical value.
969 * This function assumes that the h16 component has already been validated.
971 static USHORT h16tous(h16 component) {
972 DWORD i;
973 USHORT ret = 0;
975 for(i = 0; i < component.len; ++i) {
976 ret <<= 4;
977 ret += hex_to_int(component.str[i]);
980 return ret;
983 /* Converts an IPv6 address into it's 128 bits (16 bytes) numerical value.
985 * This function assumes that the ipv6_address has already been validated.
987 static BOOL ipv6_to_number(const ipv6_address *address, USHORT number[8]) {
988 DWORD i, cur_component = 0;
989 BOOL already_passed_elision = FALSE;
991 for(i = 0; i < address->h16_count; ++i) {
992 if(address->elision) {
993 if(address->components[i].str > address->elision && !already_passed_elision) {
994 /* Means we just passed the elision and need to add it's values to
995 * 'number' before we do anything else.
997 DWORD j = 0;
998 for(j = 0; j < address->elision_size; j+=2)
999 number[cur_component++] = 0;
1001 already_passed_elision = TRUE;
1005 number[cur_component++] = h16tous(address->components[i]);
1008 /* Case when the elision appears after the h16 components. */
1009 if(!already_passed_elision && address->elision) {
1010 for(i = 0; i < address->elision_size; i+=2)
1011 number[cur_component++] = 0;
1012 already_passed_elision = TRUE;
1015 if(address->ipv4) {
1016 UINT value = ipv4toui(address->ipv4, address->ipv4_len);
1018 if(cur_component != 6) {
1019 ERR("(%p %p): Failed sanity check with %d\n", address, number, cur_component);
1020 return FALSE;
1023 number[cur_component++] = (value >> 16) & 0xffff;
1024 number[cur_component] = value & 0xffff;
1027 return TRUE;
1030 /* Checks if the characters pointed to by 'ptr' are
1031 * a percent encoded data octet.
1033 * pct-encoded = "%" HEXDIG HEXDIG
1035 static BOOL check_pct_encoded(const WCHAR **ptr) {
1036 const WCHAR *start = *ptr;
1038 if(**ptr != '%')
1039 return FALSE;
1041 ++(*ptr);
1042 if(!is_hexdigit(**ptr)) {
1043 *ptr = start;
1044 return FALSE;
1047 ++(*ptr);
1048 if(!is_hexdigit(**ptr)) {
1049 *ptr = start;
1050 return FALSE;
1053 ++(*ptr);
1054 return TRUE;
1057 /* dec-octet = DIGIT ; 0-9
1058 * / %x31-39 DIGIT ; 10-99
1059 * / "1" 2DIGIT ; 100-199
1060 * / "2" %x30-34 DIGIT ; 200-249
1061 * / "25" %x30-35 ; 250-255
1063 static BOOL check_dec_octet(const WCHAR **ptr) {
1064 const WCHAR *c1, *c2, *c3;
1066 c1 = *ptr;
1067 /* A dec-octet must be at least 1 digit long. */
1068 if(*c1 < '0' || *c1 > '9')
1069 return FALSE;
1071 ++(*ptr);
1073 c2 = *ptr;
1074 /* Since the 1 digit requirment was meet, it doesn't
1075 * matter if this is a DIGIT value, it's considered a
1076 * dec-octet.
1078 if(*c2 < '0' || *c2 > '9')
1079 return TRUE;
1081 ++(*ptr);
1083 c3 = *ptr;
1084 /* Same explanation as above. */
1085 if(*c3 < '0' || *c3 > '9')
1086 return TRUE;
1088 /* Anything > 255 isn't a valid IP dec-octet. */
1089 if(*c1 >= '2' && *c2 >= '5' && *c3 >= '5') {
1090 *ptr = c1;
1091 return FALSE;
1094 ++(*ptr);
1095 return TRUE;
1098 /* Checks if there is an implicit IPv4 address in the host component of the URI.
1099 * The max value of an implicit IPv4 address is UINT_MAX.
1101 * Ex:
1102 * "234567" would be considered an implicit IPv4 address.
1104 static BOOL check_implicit_ipv4(const WCHAR **ptr, UINT *val) {
1105 const WCHAR *start = *ptr;
1106 ULONGLONG ret = 0;
1107 *val = 0;
1109 while(is_num(**ptr)) {
1110 ret = ret*10 + (**ptr - '0');
1112 if(ret > UINT_MAX) {
1113 *ptr = start;
1114 return FALSE;
1116 ++(*ptr);
1119 if(*ptr == start)
1120 return FALSE;
1122 *val = ret;
1123 return TRUE;
1126 /* Checks if the string contains an IPv4 address.
1128 * This function has a strict mode or a non-strict mode of operation
1129 * When 'strict' is set to FALSE this function will return TRUE if
1130 * the string contains at least 'dec-octet "." dec-octet' since partial
1131 * IPv4 addresses will be normalized out into full IPv4 addresses. When
1132 * 'strict' is set this function expects there to be a full IPv4 address.
1134 * IPv4address = dec-octet "." dec-octet "." dec-octet "." dec-octet
1136 static BOOL check_ipv4address(const WCHAR **ptr, BOOL strict) {
1137 const WCHAR *start = *ptr;
1139 if(!check_dec_octet(ptr)) {
1140 *ptr = start;
1141 return FALSE;
1144 if(**ptr != '.') {
1145 *ptr = start;
1146 return FALSE;
1149 ++(*ptr);
1150 if(!check_dec_octet(ptr)) {
1151 *ptr = start;
1152 return FALSE;
1155 if(**ptr != '.') {
1156 if(strict) {
1157 *ptr = start;
1158 return FALSE;
1159 } else
1160 return TRUE;
1163 ++(*ptr);
1164 if(!check_dec_octet(ptr)) {
1165 *ptr = start;
1166 return FALSE;
1169 if(**ptr != '.') {
1170 if(strict) {
1171 *ptr = start;
1172 return FALSE;
1173 } else
1174 return TRUE;
1177 ++(*ptr);
1178 if(!check_dec_octet(ptr)) {
1179 *ptr = start;
1180 return FALSE;
1183 /* Found a four digit ip address. */
1184 return TRUE;
1186 /* Tries to parse the scheme name of the URI.
1188 * scheme = ALPHA *(ALPHA | NUM | '+' | '-' | '.') as defined by RFC 3896.
1189 * NOTE: Windows accepts a number as the first character of a scheme.
1191 static BOOL parse_scheme_name(const WCHAR **ptr, parse_data *data, DWORD extras) {
1192 const WCHAR *start = *ptr;
1194 data->scheme = NULL;
1195 data->scheme_len = 0;
1197 while(**ptr) {
1198 if(**ptr == '*' && *ptr == start) {
1199 /* Might have found a wildcard scheme. If it is the next
1200 * char has to be a ':' for it to be a valid URI
1202 ++(*ptr);
1203 break;
1204 } else if(!is_num(**ptr) && !is_alpha(**ptr) && **ptr != '+' &&
1205 **ptr != '-' && **ptr != '.')
1206 break;
1208 (*ptr)++;
1211 if(*ptr == start)
1212 return FALSE;
1214 /* Schemes must end with a ':' */
1215 if(**ptr != ':' && !((extras & ALLOW_NULL_TERM_SCHEME) && !**ptr)) {
1216 *ptr = start;
1217 return FALSE;
1220 data->scheme = start;
1221 data->scheme_len = *ptr - start;
1223 ++(*ptr);
1224 return TRUE;
1227 /* Tries to deduce the corresponding URL_SCHEME for the given URI. Stores
1228 * the deduced URL_SCHEME in data->scheme_type.
1230 static BOOL parse_scheme_type(parse_data *data) {
1231 /* If there's scheme data then see if it's a recognized scheme. */
1232 if(data->scheme && data->scheme_len) {
1233 DWORD i;
1235 for(i = 0; i < sizeof(recognized_schemes)/sizeof(recognized_schemes[0]); ++i) {
1236 if(lstrlenW(recognized_schemes[i].scheme_name) == data->scheme_len) {
1237 /* Has to be a case insensitive compare. */
1238 if(!StrCmpNIW(recognized_schemes[i].scheme_name, data->scheme, data->scheme_len)) {
1239 data->scheme_type = recognized_schemes[i].scheme;
1240 return TRUE;
1245 /* If we get here it means it's not a recognized scheme. */
1246 data->scheme_type = URL_SCHEME_UNKNOWN;
1247 return TRUE;
1248 } else if(data->is_relative) {
1249 /* Relative URI's have no scheme. */
1250 data->scheme_type = URL_SCHEME_UNKNOWN;
1251 return TRUE;
1252 } else {
1253 /* Should never reach here! what happened... */
1254 FIXME("(%p): Unable to determine scheme type for URI %s\n", data, debugstr_w(data->uri));
1255 return FALSE;
1259 /* Tries to parse (or deduce) the scheme_name of a URI. If it can't
1260 * parse a scheme from the URI it will try to deduce the scheme_name and scheme_type
1261 * using the flags specified in 'flags' (if any). Flags that affect how this function
1262 * operates are the Uri_CREATE_ALLOW_* flags.
1264 * All parsed/deduced information will be stored in 'data' when the function returns.
1266 * Returns TRUE if it was able to successfully parse the information.
1268 static BOOL parse_scheme(const WCHAR **ptr, parse_data *data, DWORD flags, DWORD extras) {
1269 static const WCHAR fileW[] = {'f','i','l','e',0};
1270 static const WCHAR wildcardW[] = {'*',0};
1272 /* First check to see if the uri could implicitly be a file path. */
1273 if(is_implicit_file_path(*ptr)) {
1274 if(flags & Uri_CREATE_ALLOW_IMPLICIT_FILE_SCHEME) {
1275 data->scheme = fileW;
1276 data->scheme_len = lstrlenW(fileW);
1277 data->has_implicit_scheme = TRUE;
1279 TRACE("(%p %p %x): URI is an implicit file path.\n", ptr, data, flags);
1280 } else {
1281 /* Window's does not consider anything that can implicitly be a file
1282 * path to be a valid URI if the ALLOW_IMPLICIT_FILE_SCHEME flag is not set...
1284 TRACE("(%p %p %x): URI is implicitly a file path, but, the ALLOW_IMPLICIT_FILE_SCHEME flag wasn't set.\n",
1285 ptr, data, flags);
1286 return FALSE;
1288 } else if(!parse_scheme_name(ptr, data, extras)) {
1289 /* No Scheme was found, this means it could be:
1290 * a) an implicit Wildcard scheme
1291 * b) a relative URI
1292 * c) a invalid URI.
1294 if(flags & Uri_CREATE_ALLOW_IMPLICIT_WILDCARD_SCHEME) {
1295 data->scheme = wildcardW;
1296 data->scheme_len = lstrlenW(wildcardW);
1297 data->has_implicit_scheme = TRUE;
1299 TRACE("(%p %p %x): URI is an implicit wildcard scheme.\n", ptr, data, flags);
1300 } else if (flags & Uri_CREATE_ALLOW_RELATIVE) {
1301 data->is_relative = TRUE;
1302 TRACE("(%p %p %x): URI is relative.\n", ptr, data, flags);
1303 } else {
1304 TRACE("(%p %p %x): Malformed URI found. Unable to deduce scheme name.\n", ptr, data, flags);
1305 return FALSE;
1309 if(!data->is_relative)
1310 TRACE("(%p %p %x): Found scheme=%s scheme_len=%d\n", ptr, data, flags,
1311 debugstr_wn(data->scheme, data->scheme_len), data->scheme_len);
1313 if(!parse_scheme_type(data))
1314 return FALSE;
1316 TRACE("(%p %p %x): Assigned %d as the URL_SCHEME.\n", ptr, data, flags, data->scheme_type);
1317 return TRUE;
1320 static BOOL parse_username(const WCHAR **ptr, parse_data *data, DWORD flags, DWORD extras) {
1321 data->username = *ptr;
1323 while(**ptr != ':' && **ptr != '@') {
1324 if(**ptr == '%') {
1325 if(!check_pct_encoded(ptr)) {
1326 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
1327 *ptr = data->username;
1328 data->username = NULL;
1329 return FALSE;
1331 } else
1332 continue;
1333 } else if(extras & ALLOW_NULL_TERM_USER_NAME && !**ptr)
1334 break;
1335 else if(is_auth_delim(**ptr, data->scheme_type != URL_SCHEME_UNKNOWN)) {
1336 *ptr = data->username;
1337 data->username = NULL;
1338 return FALSE;
1341 ++(*ptr);
1344 data->username_len = *ptr - data->username;
1345 return TRUE;
1348 static BOOL parse_password(const WCHAR **ptr, parse_data *data, DWORD flags, DWORD extras) {
1349 data->password = *ptr;
1351 while(**ptr != '@') {
1352 if(**ptr == '%') {
1353 if(!check_pct_encoded(ptr)) {
1354 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
1355 *ptr = data->password;
1356 data->password = NULL;
1357 return FALSE;
1359 } else
1360 continue;
1361 } else if(extras & ALLOW_NULL_TERM_PASSWORD && !**ptr)
1362 break;
1363 else if(is_auth_delim(**ptr, data->scheme_type != URL_SCHEME_UNKNOWN)) {
1364 *ptr = data->password;
1365 data->password = NULL;
1366 return FALSE;
1369 ++(*ptr);
1372 data->password_len = *ptr - data->password;
1373 return TRUE;
1376 /* Parses the userinfo part of the URI (if it exists). The userinfo field of
1377 * a URI can consist of "username:password@", or just "username@".
1379 * RFC def:
1380 * userinfo = *( unreserved / pct-encoded / sub-delims / ":" )
1382 * NOTES:
1383 * 1) If there is more than one ':' in the userinfo part of the URI Windows
1384 * uses the first occurrence of ':' to delimit the username and password
1385 * components.
1387 * ex:
1388 * ftp://user:pass:word@winehq.org
1390 * Would yield, "user" as the username and "pass:word" as the password.
1392 * 2) Windows allows any character to appear in the "userinfo" part of
1393 * a URI, as long as it's not an authority delimeter character set.
1395 static void parse_userinfo(const WCHAR **ptr, parse_data *data, DWORD flags) {
1396 const WCHAR *start = *ptr;
1398 if(!parse_username(ptr, data, flags, 0)) {
1399 TRACE("(%p %p %x): URI contained no userinfo.\n", ptr, data, flags);
1400 return;
1403 if(**ptr == ':') {
1404 ++(*ptr);
1405 if(!parse_password(ptr, data, flags, 0)) {
1406 *ptr = start;
1407 data->username = NULL;
1408 data->username_len = 0;
1409 TRACE("(%p %p %x): URI contained no userinfo.\n", ptr, data, flags);
1410 return;
1414 if(**ptr != '@') {
1415 *ptr = start;
1416 data->username = NULL;
1417 data->username_len = 0;
1418 data->password = NULL;
1419 data->password_len = 0;
1421 TRACE("(%p %p %x): URI contained no userinfo.\n", ptr, data, flags);
1422 return;
1425 if(data->username)
1426 TRACE("(%p %p %x): Found username %s len=%d.\n", ptr, data, flags,
1427 debugstr_wn(data->username, data->username_len), data->username_len);
1429 if(data->password)
1430 TRACE("(%p %p %x): Found password %s len=%d.\n", ptr, data, flags,
1431 debugstr_wn(data->password, data->password_len), data->password_len);
1433 ++(*ptr);
1436 /* Attempts to parse a port from the URI.
1438 * NOTES:
1439 * Windows seems to have a cap on what the maximum value
1440 * for a port can be. The max value is USHORT_MAX.
1442 * port = *DIGIT
1444 static BOOL parse_port(const WCHAR **ptr, parse_data *data, DWORD flags) {
1445 UINT port = 0;
1446 data->port = *ptr;
1448 while(!is_auth_delim(**ptr, data->scheme_type != URL_SCHEME_UNKNOWN)) {
1449 if(!is_num(**ptr)) {
1450 *ptr = data->port;
1451 data->port = NULL;
1452 return FALSE;
1455 port = port*10 + (**ptr-'0');
1457 if(port > USHORT_MAX) {
1458 *ptr = data->port;
1459 data->port = NULL;
1460 return FALSE;
1463 ++(*ptr);
1466 data->has_port = TRUE;
1467 data->port_value = port;
1468 data->port_len = *ptr - data->port;
1470 TRACE("(%p %p %x): Found port %s len=%d value=%u\n", ptr, data, flags,
1471 debugstr_wn(data->port, data->port_len), data->port_len, data->port_value);
1472 return TRUE;
1475 /* Attempts to parse a IPv4 address from the URI.
1477 * NOTES:
1478 * Window's normalizes IPv4 addresses, This means there's three
1479 * possibilities for the URI to contain an IPv4 address.
1480 * 1) A well formed address (ex. 192.2.2.2).
1481 * 2) A partially formed address. For example "192.0" would
1482 * normalize to "192.0.0.0" during canonicalization.
1483 * 3) An implicit IPv4 address. For example "256" would
1484 * normalize to "0.0.1.0" during canonicalization. Also
1485 * note that the maximum value for an implicit IP address
1486 * is UINT_MAX, if the value in the URI exceeds this then
1487 * it is not considered an IPv4 address.
1489 static BOOL parse_ipv4address(const WCHAR **ptr, parse_data *data, DWORD flags) {
1490 const BOOL is_unknown = data->scheme_type == URL_SCHEME_UNKNOWN;
1491 data->host = *ptr;
1493 if(!check_ipv4address(ptr, FALSE)) {
1494 if(!check_implicit_ipv4(ptr, &data->implicit_ipv4)) {
1495 TRACE("(%p %p %x): URI didn't contain anything looking like an IPv4 address.\n",
1496 ptr, data, flags);
1497 *ptr = data->host;
1498 data->host = NULL;
1499 return FALSE;
1500 } else
1501 data->has_implicit_ip = TRUE;
1504 /* Check if what we found is the only part of the host name (if it isn't
1505 * we don't have an IPv4 address).
1507 if(**ptr == ':') {
1508 ++(*ptr);
1509 if(!parse_port(ptr, data, flags)) {
1510 *ptr = data->host;
1511 data->host = NULL;
1512 return FALSE;
1514 } else if(!is_auth_delim(**ptr, !is_unknown)) {
1515 /* Found more data which belongs the host, so this isn't an IPv4. */
1516 *ptr = data->host;
1517 data->host = NULL;
1518 data->has_implicit_ip = FALSE;
1519 return FALSE;
1522 data->host_len = *ptr - data->host;
1523 data->host_type = Uri_HOST_IPV4;
1525 TRACE("(%p %p %x): IPv4 address found. host=%s host_len=%d host_type=%d\n",
1526 ptr, data, flags, debugstr_wn(data->host, data->host_len),
1527 data->host_len, data->host_type);
1528 return TRUE;
1531 /* Attempts to parse the reg-name from the URI.
1533 * Because of the way Windows handles ':' this function also
1534 * handles parsing the port.
1536 * reg-name = *( unreserved / pct-encoded / sub-delims )
1538 * NOTE:
1539 * Windows allows everything, but, the characters in "auth_delims" and ':'
1540 * to appear in a reg-name, unless it's an unknown scheme type then ':' is
1541 * allowed to appear (even if a valid port isn't after it).
1543 * Windows doesn't like host names which start with '[' and end with ']'
1544 * and don't contain a valid IP literal address in between them.
1546 * On Windows if an '[' is encountered in the host name the ':' no longer
1547 * counts as a delimiter until you reach the next ']' or an "authority delimeter".
1549 * A reg-name CAN be empty.
1551 static BOOL parse_reg_name(const WCHAR **ptr, parse_data *data, DWORD flags, DWORD extras) {
1552 const BOOL has_start_bracket = **ptr == '[';
1553 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
1554 BOOL inside_brackets = has_start_bracket;
1555 BOOL ignore_col = extras & IGNORE_PORT_DELIMITER;
1557 /* We have to be careful with file schemes. */
1558 if(data->scheme_type == URL_SCHEME_FILE) {
1559 /* This is because an implicit file scheme could be "C:\\test" and it
1560 * would trick this function into thinking the host is "C", when after
1561 * canonicalization the host would end up being an empty string. A drive
1562 * path can also have a '|' instead of a ':' after the drive letter.
1564 if(is_drive_path(*ptr)) {
1565 /* Regular old drive paths don't have a host type (or host name). */
1566 data->host_type = Uri_HOST_UNKNOWN;
1567 data->host = *ptr;
1568 data->host_len = 0;
1569 return TRUE;
1570 } else if(is_unc_path(*ptr))
1571 /* Skip past the "\\" of a UNC path. */
1572 *ptr += 2;
1575 data->host = *ptr;
1577 while(!is_auth_delim(**ptr, known_scheme)) {
1578 if(**ptr == ':' && !ignore_col) {
1579 /* We can ignore ':' if were inside brackets.*/
1580 if(!inside_brackets) {
1581 const WCHAR *tmp = (*ptr)++;
1583 /* Attempt to parse the port. */
1584 if(!parse_port(ptr, data, flags)) {
1585 /* Windows expects there to be a valid port for known scheme types. */
1586 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
1587 *ptr = data->host;
1588 data->host = NULL;
1589 TRACE("(%p %p %x %x): Expected valid port\n", ptr, data, flags, extras);
1590 return FALSE;
1591 } else
1592 /* Windows gives up on trying to parse a port when it
1593 * encounters 1 invalid port.
1595 ignore_col = TRUE;
1596 } else {
1597 data->host_len = tmp - data->host;
1598 break;
1601 } else if(**ptr == '%' && known_scheme) {
1602 /* Has to be a legit % encoded value. */
1603 if(!check_pct_encoded(ptr)) {
1604 *ptr = data->host;
1605 data->host = NULL;
1606 return FALSE;
1607 } else
1608 continue;
1609 } else if(**ptr == ']')
1610 inside_brackets = FALSE;
1611 else if(**ptr == '[')
1612 inside_brackets = TRUE;
1614 ++(*ptr);
1617 if(has_start_bracket) {
1618 /* Make sure the last character of the host wasn't a ']'. */
1619 if(*(*ptr-1) == ']') {
1620 TRACE("(%p %p %x %x): Expected an IP literal inside of the host\n",
1621 ptr, data, flags, extras);
1622 *ptr = data->host;
1623 data->host = NULL;
1624 return FALSE;
1628 /* Don't overwrite our length if we found a port earlier. */
1629 if(!data->port)
1630 data->host_len = *ptr - data->host;
1632 /* If the host is empty, then it's an unknown host type. */
1633 if(data->host_len == 0)
1634 data->host_type = Uri_HOST_UNKNOWN;
1635 else
1636 data->host_type = Uri_HOST_DNS;
1638 TRACE("(%p %p %x %x): Parsed reg-name. host=%s len=%d\n", ptr, data, flags, extras,
1639 debugstr_wn(data->host, data->host_len), data->host_len);
1640 return TRUE;
1643 /* Attempts to parse an IPv6 address out of the URI.
1645 * IPv6address = 6( h16 ":" ) ls32
1646 * / "::" 5( h16 ":" ) ls32
1647 * / [ h16 ] "::" 4( h16 ":" ) ls32
1648 * / [ *1( h16 ":" ) h16 ] "::" 3( h16 ":" ) ls32
1649 * / [ *2( h16 ":" ) h16 ] "::" 2( h16 ":" ) ls32
1650 * / [ *3( h16 ":" ) h16 ] "::" h16 ":" ls32
1651 * / [ *4( h16 ":" ) h16 ] "::" ls32
1652 * / [ *5( h16 ":" ) h16 ] "::" h16
1653 * / [ *6( h16 ":" ) h16 ] "::"
1655 * ls32 = ( h16 ":" h16 ) / IPv4address
1656 * ; least-significant 32 bits of address.
1658 * h16 = 1*4HEXDIG
1659 * ; 16 bits of address represented in hexadecimal.
1661 * Modeled after google-url's 'DoParseIPv6' function.
1663 static BOOL parse_ipv6address(const WCHAR **ptr, parse_data *data, DWORD flags) {
1664 const WCHAR *start, *cur_start;
1665 ipv6_address ip;
1667 start = cur_start = *ptr;
1668 memset(&ip, 0, sizeof(ipv6_address));
1670 for(;; ++(*ptr)) {
1671 /* Check if we're on the last character of the host. */
1672 BOOL is_end = (is_auth_delim(**ptr, data->scheme_type != URL_SCHEME_UNKNOWN)
1673 || **ptr == ']');
1675 BOOL is_split = (**ptr == ':');
1676 BOOL is_elision = (is_split && !is_end && *(*ptr+1) == ':');
1678 /* Check if we're at the end of a component, or
1679 * if we're at the end of the IPv6 address.
1681 if(is_split || is_end) {
1682 DWORD cur_len = 0;
1684 cur_len = *ptr - cur_start;
1686 /* h16 can't have a length > 4. */
1687 if(cur_len > 4) {
1688 *ptr = start;
1690 TRACE("(%p %p %x): h16 component to long.\n",
1691 ptr, data, flags);
1692 return FALSE;
1695 if(cur_len == 0) {
1696 /* An h16 component can't have the length of 0 unless
1697 * the elision is at the beginning of the address, or
1698 * at the end of the address.
1700 if(!((*ptr == start && is_elision) ||
1701 (is_end && (*ptr-2) == ip.elision))) {
1702 *ptr = start;
1703 TRACE("(%p %p %x): IPv6 component cannot have a length of 0.\n",
1704 ptr, data, flags);
1705 return FALSE;
1709 if(cur_len > 0) {
1710 /* An IPv6 address can have no more than 8 h16 components. */
1711 if(ip.h16_count >= 8) {
1712 *ptr = start;
1713 TRACE("(%p %p %x): Not a IPv6 address, to many h16 components.\n",
1714 ptr, data, flags);
1715 return FALSE;
1718 ip.components[ip.h16_count].str = cur_start;
1719 ip.components[ip.h16_count].len = cur_len;
1721 TRACE("(%p %p %x): Found h16 component %s, len=%d, h16_count=%d\n",
1722 ptr, data, flags, debugstr_wn(cur_start, cur_len), cur_len,
1723 ip.h16_count);
1724 ++ip.h16_count;
1728 if(is_end)
1729 break;
1731 if(is_elision) {
1732 /* A IPv6 address can only have 1 elision ('::'). */
1733 if(ip.elision) {
1734 *ptr = start;
1736 TRACE("(%p %p %x): IPv6 address cannot have 2 elisions.\n",
1737 ptr, data, flags);
1738 return FALSE;
1741 ip.elision = *ptr;
1742 ++(*ptr);
1745 if(is_split)
1746 cur_start = *ptr+1;
1747 else {
1748 if(!check_ipv4address(ptr, TRUE)) {
1749 if(!is_hexdigit(**ptr)) {
1750 /* Not a valid character for an IPv6 address. */
1751 *ptr = start;
1752 return FALSE;
1754 } else {
1755 /* Found an IPv4 address. */
1756 ip.ipv4 = cur_start;
1757 ip.ipv4_len = *ptr - cur_start;
1759 TRACE("(%p %p %x): Found an attached IPv4 address %s len=%d.\n",
1760 ptr, data, flags, debugstr_wn(ip.ipv4, ip.ipv4_len),
1761 ip.ipv4_len);
1763 /* IPv4 addresses can only appear at the end of a IPv6. */
1764 break;
1769 compute_ipv6_comps_size(&ip);
1771 /* Make sure the IPv6 address adds up to 16 bytes. */
1772 if(ip.components_size + ip.elision_size != 16) {
1773 *ptr = start;
1774 TRACE("(%p %p %x): Invalid IPv6 address, did not add up to 16 bytes.\n",
1775 ptr, data, flags);
1776 return FALSE;
1779 if(ip.elision_size == 2) {
1780 /* For some reason on Windows if an elision that represents
1781 * only 1 h16 component is encountered at the very begin or
1782 * end of an IPv6 address, Windows does not consider it a
1783 * valid IPv6 address.
1785 * Ex: [::2:3:4:5:6:7] is not valid, even though the sum
1786 * of all the components == 128bits.
1788 if(ip.elision < ip.components[0].str ||
1789 ip.elision > ip.components[ip.h16_count-1].str) {
1790 *ptr = start;
1791 TRACE("(%p %p %x): Invalid IPv6 address. Detected elision of 2 bytes at the beginning or end of the address.\n",
1792 ptr, data, flags);
1793 return FALSE;
1797 data->host_type = Uri_HOST_IPV6;
1798 data->has_ipv6 = TRUE;
1799 data->ipv6_address = ip;
1801 TRACE("(%p %p %x): Found valid IPv6 literal %s len=%d\n",
1802 ptr, data, flags, debugstr_wn(start, *ptr-start),
1803 *ptr-start);
1804 return TRUE;
1807 /* IPvFuture = "v" 1*HEXDIG "." 1*( unreserved / sub-delims / ":" ) */
1808 static BOOL parse_ipvfuture(const WCHAR **ptr, parse_data *data, DWORD flags) {
1809 const WCHAR *start = *ptr;
1811 /* IPvFuture has to start with a 'v' or 'V'. */
1812 if(**ptr != 'v' && **ptr != 'V')
1813 return FALSE;
1815 /* Following the v there must be at least 1 hex digit. */
1816 ++(*ptr);
1817 if(!is_hexdigit(**ptr)) {
1818 *ptr = start;
1819 return FALSE;
1822 ++(*ptr);
1823 while(is_hexdigit(**ptr))
1824 ++(*ptr);
1826 /* End of the hexdigit sequence must be a '.' */
1827 if(**ptr != '.') {
1828 *ptr = start;
1829 return FALSE;
1832 ++(*ptr);
1833 if(!is_unreserved(**ptr) && !is_subdelim(**ptr) && **ptr != ':') {
1834 *ptr = start;
1835 return FALSE;
1838 ++(*ptr);
1839 while(is_unreserved(**ptr) || is_subdelim(**ptr) || **ptr == ':')
1840 ++(*ptr);
1842 data->host_type = Uri_HOST_UNKNOWN;
1844 TRACE("(%p %p %x): Parsed IPvFuture address %s len=%d\n", ptr, data, flags,
1845 debugstr_wn(start, *ptr-start), *ptr-start);
1847 return TRUE;
1850 /* IP-literal = "[" ( IPv6address / IPvFuture ) "]" */
1851 static BOOL parse_ip_literal(const WCHAR **ptr, parse_data *data, DWORD flags, DWORD extras) {
1852 data->host = *ptr;
1854 if(**ptr != '[' && !(extras & ALLOW_BRACKETLESS_IP_LITERAL)) {
1855 data->host = NULL;
1856 return FALSE;
1857 } else if(**ptr == '[')
1858 ++(*ptr);
1860 if(!parse_ipv6address(ptr, data, flags)) {
1861 if(extras & SKIP_IP_FUTURE_CHECK || !parse_ipvfuture(ptr, data, flags)) {
1862 *ptr = data->host;
1863 data->host = NULL;
1864 return FALSE;
1868 if(**ptr != ']' && !(extras & ALLOW_BRACKETLESS_IP_LITERAL)) {
1869 *ptr = data->host;
1870 data->host = NULL;
1871 return FALSE;
1872 } else if(!**ptr && extras & ALLOW_BRACKETLESS_IP_LITERAL) {
1873 /* The IP literal didn't contain brackets and was followed by
1874 * a NULL terminator, so no reason to even check the port.
1876 data->host_len = *ptr - data->host;
1877 return TRUE;
1880 ++(*ptr);
1881 if(**ptr == ':') {
1882 ++(*ptr);
1883 /* If a valid port is not found, then let it trickle down to
1884 * parse_reg_name.
1886 if(!parse_port(ptr, data, flags)) {
1887 *ptr = data->host;
1888 data->host = NULL;
1889 return FALSE;
1891 } else
1892 data->host_len = *ptr - data->host;
1894 return TRUE;
1897 /* Parses the host information from the URI.
1899 * host = IP-literal / IPv4address / reg-name
1901 static BOOL parse_host(const WCHAR **ptr, parse_data *data, DWORD flags, DWORD extras) {
1902 if(!parse_ip_literal(ptr, data, flags, extras)) {
1903 if(!parse_ipv4address(ptr, data, flags)) {
1904 if(!parse_reg_name(ptr, data, flags, extras)) {
1905 TRACE("(%p %p %x %x): Malformed URI, Unknown host type.\n",
1906 ptr, data, flags, extras);
1907 return FALSE;
1912 return TRUE;
1915 /* Parses the authority information from the URI.
1917 * authority = [ userinfo "@" ] host [ ":" port ]
1919 static BOOL parse_authority(const WCHAR **ptr, parse_data *data, DWORD flags) {
1920 parse_userinfo(ptr, data, flags);
1922 /* Parsing the port will happen during one of the host parsing
1923 * routines (if the URI has a port).
1925 if(!parse_host(ptr, data, flags, 0))
1926 return FALSE;
1928 return TRUE;
1931 /* Attempts to parse the path information of a hierarchical URI. */
1932 static BOOL parse_path_hierarchical(const WCHAR **ptr, parse_data *data, DWORD flags) {
1933 const WCHAR *start = *ptr;
1934 static const WCHAR slash[] = {'/',0};
1935 const BOOL is_file = data->scheme_type == URL_SCHEME_FILE;
1937 if(is_path_delim(**ptr)) {
1938 if(data->scheme_type == URL_SCHEME_WILDCARD) {
1939 /* Wildcard schemes don't get a '/' attached if their path is
1940 * empty.
1942 data->path = NULL;
1943 data->path_len = 0;
1944 } else if(!(flags & Uri_CREATE_NO_CANONICALIZE)) {
1945 /* If the path component is empty, then a '/' is added. */
1946 data->path = slash;
1947 data->path_len = 1;
1949 } else {
1950 while(!is_path_delim(**ptr)) {
1951 if(**ptr == '%' && data->scheme_type != URL_SCHEME_UNKNOWN && !is_file) {
1952 if(!check_pct_encoded(ptr)) {
1953 *ptr = start;
1954 return FALSE;
1955 } else
1956 continue;
1957 } else if(is_forbidden_dos_path_char(**ptr) && is_file &&
1958 (flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
1959 /* File schemes with USE_DOS_PATH set aren't allowed to have
1960 * a '<' or '>' or '\"' appear in them.
1962 *ptr = start;
1963 return FALSE;
1964 } else if(**ptr == '\\') {
1965 /* Not allowed to have a backslash if NO_CANONICALIZE is set
1966 * and the scheme is known type (but not a file scheme).
1968 if(flags & Uri_CREATE_NO_CANONICALIZE) {
1969 if(data->scheme_type != URL_SCHEME_FILE &&
1970 data->scheme_type != URL_SCHEME_UNKNOWN) {
1971 *ptr = start;
1972 return FALSE;
1977 ++(*ptr);
1980 /* The only time a URI doesn't have a path is when
1981 * the NO_CANONICALIZE flag is set and the raw URI
1982 * didn't contain one.
1984 if(*ptr == start) {
1985 data->path = NULL;
1986 data->path_len = 0;
1987 } else {
1988 data->path = start;
1989 data->path_len = *ptr - start;
1993 if(data->path)
1994 TRACE("(%p %p %x): Parsed path %s len=%d\n", ptr, data, flags,
1995 debugstr_wn(data->path, data->path_len), data->path_len);
1996 else
1997 TRACE("(%p %p %x): The URI contained no path\n", ptr, data, flags);
1999 return TRUE;
2002 /* Parses the path of a opaque URI (much less strict then the parser
2003 * for a hierarchical URI).
2005 * NOTE:
2006 * Windows allows invalid % encoded data to appear in opaque URI paths
2007 * for unknown scheme types.
2009 * File schemes with USE_DOS_PATH set aren't allowed to have '<', '>', or '\"'
2010 * appear in them.
2012 static BOOL parse_path_opaque(const WCHAR **ptr, parse_data *data, DWORD flags) {
2013 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
2014 const BOOL is_file = data->scheme_type == URL_SCHEME_FILE;
2016 data->path = *ptr;
2018 while(!is_path_delim(**ptr)) {
2019 if(**ptr == '%' && known_scheme) {
2020 if(!check_pct_encoded(ptr)) {
2021 *ptr = data->path;
2022 data->path = NULL;
2023 return FALSE;
2024 } else
2025 continue;
2026 } else if(is_forbidden_dos_path_char(**ptr) && is_file &&
2027 (flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
2028 *ptr = data->path;
2029 data->path = NULL;
2030 return FALSE;
2033 ++(*ptr);
2036 data->path_len = *ptr - data->path;
2037 TRACE("(%p %p %x): Parsed opaque URI path %s len=%d\n", ptr, data, flags,
2038 debugstr_wn(data->path, data->path_len), data->path_len);
2039 return TRUE;
2042 /* Determines how the URI should be parsed after the scheme information.
2044 * If the scheme is followed, by "//" then, it is treated as an hierarchical URI
2045 * which then the authority and path information will be parsed out. Otherwise, the
2046 * URI will be treated as an opaque URI which the authority information is not parsed
2047 * out.
2049 * RFC 3896 definition of hier-part:
2051 * hier-part = "//" authority path-abempty
2052 * / path-absolute
2053 * / path-rootless
2054 * / path-empty
2056 * MSDN opaque URI definition:
2057 * scheme ":" path [ "#" fragment ]
2059 * NOTES:
2060 * If the URI is of an unknown scheme type and has a "//" following the scheme then it
2061 * is treated as a hierarchical URI, but, if the CREATE_NO_CRACK_UNKNOWN_SCHEMES flag is
2062 * set then it is considered an opaque URI reguardless of what follows the scheme information
2063 * (per MSDN documentation).
2065 static BOOL parse_hierpart(const WCHAR **ptr, parse_data *data, DWORD flags) {
2066 const WCHAR *start = *ptr;
2068 /* Checks if the authority information needs to be parsed. */
2069 if(is_hierarchical_uri(ptr, data)) {
2070 /* Only treat it as a hierarchical URI if the scheme_type is known or
2071 * the Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES flag is not set.
2073 if(data->scheme_type != URL_SCHEME_UNKNOWN ||
2074 !(flags & Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES)) {
2075 TRACE("(%p %p %x): Treating URI as an hierarchical URI.\n", ptr, data, flags);
2076 data->is_opaque = FALSE;
2078 /* TODO: Handle hierarchical URI's, parse authority then parse the path. */
2079 if(!parse_authority(ptr, data, flags))
2080 return FALSE;
2082 return parse_path_hierarchical(ptr, data, flags);
2083 } else
2084 /* Reset ptr to it's starting position so opaque path parsing
2085 * begins at the correct location.
2087 *ptr = start;
2090 /* If it reaches here, then the URI will be treated as an opaque
2091 * URI.
2094 TRACE("(%p %p %x): Treating URI as an opaque URI.\n", ptr, data, flags);
2096 data->is_opaque = TRUE;
2097 if(!parse_path_opaque(ptr, data, flags))
2098 return FALSE;
2100 return TRUE;
2103 /* Attempts to parse the query string from the URI.
2105 * NOTES:
2106 * If NO_DECODE_EXTRA_INFO flag is set, then invalid percent encoded
2107 * data is allowed appear in the query string. For unknown scheme types
2108 * invalid percent encoded data is allowed to appear reguardless.
2110 static BOOL parse_query(const WCHAR **ptr, parse_data *data, DWORD flags) {
2111 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
2113 if(**ptr != '?') {
2114 TRACE("(%p %p %x): URI didn't contain a query string.\n", ptr, data, flags);
2115 return TRUE;
2118 data->query = *ptr;
2120 ++(*ptr);
2121 while(**ptr && **ptr != '#') {
2122 if(**ptr == '%' && known_scheme &&
2123 !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
2124 if(!check_pct_encoded(ptr)) {
2125 *ptr = data->query;
2126 data->query = NULL;
2127 return FALSE;
2128 } else
2129 continue;
2132 ++(*ptr);
2135 data->query_len = *ptr - data->query;
2137 TRACE("(%p %p %x): Parsed query string %s len=%d\n", ptr, data, flags,
2138 debugstr_wn(data->query, data->query_len), data->query_len);
2139 return TRUE;
2142 /* Attempts to parse the fragment from the URI.
2144 * NOTES:
2145 * If NO_DECODE_EXTRA_INFO flag is set, then invalid percent encoded
2146 * data is allowed appear in the query string. For unknown scheme types
2147 * invalid percent encoded data is allowed to appear reguardless.
2149 static BOOL parse_fragment(const WCHAR **ptr, parse_data *data, DWORD flags) {
2150 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
2152 if(**ptr != '#') {
2153 TRACE("(%p %p %x): URI didn't contain a fragment.\n", ptr, data, flags);
2154 return TRUE;
2157 data->fragment = *ptr;
2159 ++(*ptr);
2160 while(**ptr) {
2161 if(**ptr == '%' && known_scheme &&
2162 !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
2163 if(!check_pct_encoded(ptr)) {
2164 *ptr = data->fragment;
2165 data->fragment = NULL;
2166 return FALSE;
2167 } else
2168 continue;
2171 ++(*ptr);
2174 data->fragment_len = *ptr - data->fragment;
2176 TRACE("(%p %p %x): Parsed fragment %s len=%d\n", ptr, data, flags,
2177 debugstr_wn(data->fragment, data->fragment_len), data->fragment_len);
2178 return TRUE;
2181 /* Parses and validates the components of the specified by data->uri
2182 * and stores the information it parses into 'data'.
2184 * Returns TRUE if it successfully parsed the URI. False otherwise.
2186 static BOOL parse_uri(parse_data *data, DWORD flags) {
2187 const WCHAR *ptr;
2188 const WCHAR **pptr;
2190 ptr = data->uri;
2191 pptr = &ptr;
2193 TRACE("(%p %x): BEGINNING TO PARSE URI %s.\n", data, flags, debugstr_w(data->uri));
2195 if(!parse_scheme(pptr, data, flags, 0))
2196 return FALSE;
2198 if(!parse_hierpart(pptr, data, flags))
2199 return FALSE;
2201 if(!parse_query(pptr, data, flags))
2202 return FALSE;
2204 if(!parse_fragment(pptr, data, flags))
2205 return FALSE;
2207 TRACE("(%p %x): FINISHED PARSING URI.\n", data, flags);
2208 return TRUE;
2211 static BOOL canonicalize_username(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2212 const WCHAR *ptr;
2214 if(!data->username) {
2215 uri->userinfo_start = -1;
2216 return TRUE;
2219 uri->userinfo_start = uri->canon_len;
2220 for(ptr = data->username; ptr < data->username+data->username_len; ++ptr) {
2221 if(*ptr == '%') {
2222 /* Only decode % encoded values for known scheme types. */
2223 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
2224 /* See if the value really needs decoded. */
2225 WCHAR val = decode_pct_val(ptr);
2226 if(is_unreserved(val)) {
2227 if(!computeOnly)
2228 uri->canon_uri[uri->canon_len] = val;
2230 ++uri->canon_len;
2232 /* Move pass the hex characters. */
2233 ptr += 2;
2234 continue;
2237 } else if(!is_reserved(*ptr) && !is_unreserved(*ptr) && *ptr != '\\') {
2238 /* Only percent encode forbidden characters if the NO_ENCODE_FORBIDDEN_CHARACTERS flag
2239 * is NOT set.
2241 if(!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS)) {
2242 if(!computeOnly)
2243 pct_encode_val(*ptr, uri->canon_uri + uri->canon_len);
2245 uri->canon_len += 3;
2246 continue;
2250 if(!computeOnly)
2251 /* Nothing special, so just copy the character over. */
2252 uri->canon_uri[uri->canon_len] = *ptr;
2253 ++uri->canon_len;
2256 return TRUE;
2259 static BOOL canonicalize_password(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2260 const WCHAR *ptr;
2262 if(!data->password) {
2263 uri->userinfo_split = -1;
2264 return TRUE;
2267 if(uri->userinfo_start == -1)
2268 /* Has a password, but, doesn't have a username. */
2269 uri->userinfo_start = uri->canon_len;
2271 uri->userinfo_split = uri->canon_len - uri->userinfo_start;
2273 /* Add the ':' to the userinfo component. */
2274 if(!computeOnly)
2275 uri->canon_uri[uri->canon_len] = ':';
2276 ++uri->canon_len;
2278 for(ptr = data->password; ptr < data->password+data->password_len; ++ptr) {
2279 if(*ptr == '%') {
2280 /* Only decode % encoded values for known scheme types. */
2281 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
2282 /* See if the value really needs decoded. */
2283 WCHAR val = decode_pct_val(ptr);
2284 if(is_unreserved(val)) {
2285 if(!computeOnly)
2286 uri->canon_uri[uri->canon_len] = val;
2288 ++uri->canon_len;
2290 /* Move pass the hex characters. */
2291 ptr += 2;
2292 continue;
2295 } else if(!is_reserved(*ptr) && !is_unreserved(*ptr) && *ptr != '\\') {
2296 /* Only percent encode forbidden characters if the NO_ENCODE_FORBIDDEN_CHARACTERS flag
2297 * is NOT set.
2299 if(!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS)) {
2300 if(!computeOnly)
2301 pct_encode_val(*ptr, uri->canon_uri + uri->canon_len);
2303 uri->canon_len += 3;
2304 continue;
2308 if(!computeOnly)
2309 /* Nothing special, so just copy the character over. */
2310 uri->canon_uri[uri->canon_len] = *ptr;
2311 ++uri->canon_len;
2314 return TRUE;
2317 /* Canonicalizes the userinfo of the URI represented by the parse_data.
2319 * Canonicalization of the userinfo is a simple process. If there are any percent
2320 * encoded characters that fall in the "unreserved" character set, they are decoded
2321 * to their actual value. If a character is not in the "unreserved" or "reserved" sets
2322 * then it is percent encoded. Other than that the characters are copied over without
2323 * change.
2325 static BOOL canonicalize_userinfo(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2326 uri->userinfo_start = uri->userinfo_split = -1;
2327 uri->userinfo_len = 0;
2329 if(!data->username && !data->password)
2330 /* URI doesn't have userinfo, so nothing to do here. */
2331 return TRUE;
2333 if(!canonicalize_username(data, uri, flags, computeOnly))
2334 return FALSE;
2336 if(!canonicalize_password(data, uri, flags, computeOnly))
2337 return FALSE;
2339 uri->userinfo_len = uri->canon_len - uri->userinfo_start;
2340 if(!computeOnly)
2341 TRACE("(%p %p %x %d): Canonicalized userinfo, userinfo_start=%d, userinfo=%s, userinfo_split=%d userinfo_len=%d.\n",
2342 data, uri, flags, computeOnly, uri->userinfo_start, debugstr_wn(uri->canon_uri + uri->userinfo_start, uri->userinfo_len),
2343 uri->userinfo_split, uri->userinfo_len);
2345 /* Now insert the '@' after the userinfo. */
2346 if(!computeOnly)
2347 uri->canon_uri[uri->canon_len] = '@';
2348 ++uri->canon_len;
2350 return TRUE;
2353 /* Attempts to canonicalize a reg_name.
2355 * Things that happen:
2356 * 1) If Uri_CREATE_NO_CANONICALIZE flag is not set, then the reg_name is
2357 * lower cased. Unless it's an unknown scheme type, which case it's
2358 * no lower cased reguardless.
2360 * 2) Unreserved % encoded characters are decoded for known
2361 * scheme types.
2363 * 3) Forbidden characters are % encoded as long as
2364 * Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS flag is not set and
2365 * it isn't an unknown scheme type.
2367 * 4) If it's a file scheme and the host is "localhost" it's removed.
2369 * 5) If it's a file scheme and Uri_CREATE_FILE_USE_DOS_PATH is set,
2370 * then the UNC path characters are added before the host name.
2372 static BOOL canonicalize_reg_name(const parse_data *data, Uri *uri,
2373 DWORD flags, BOOL computeOnly) {
2374 static const WCHAR localhostW[] =
2375 {'l','o','c','a','l','h','o','s','t',0};
2376 const WCHAR *ptr;
2377 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
2379 if(data->scheme_type == URL_SCHEME_FILE &&
2380 data->host_len == lstrlenW(localhostW)) {
2381 if(!StrCmpNIW(data->host, localhostW, data->host_len)) {
2382 uri->host_start = -1;
2383 uri->host_len = 0;
2384 uri->host_type = Uri_HOST_UNKNOWN;
2385 return TRUE;
2389 if(data->scheme_type == URL_SCHEME_FILE && flags & Uri_CREATE_FILE_USE_DOS_PATH) {
2390 if(!computeOnly) {
2391 uri->canon_uri[uri->canon_len] = '\\';
2392 uri->canon_uri[uri->canon_len+1] = '\\';
2394 uri->canon_len += 2;
2395 uri->authority_start = uri->canon_len;
2398 uri->host_start = uri->canon_len;
2400 for(ptr = data->host; ptr < data->host+data->host_len; ++ptr) {
2401 if(*ptr == '%' && known_scheme) {
2402 WCHAR val = decode_pct_val(ptr);
2403 if(is_unreserved(val)) {
2404 /* If NO_CANONICALZE is not set, then windows lower cases the
2405 * decoded value.
2407 if(!(flags & Uri_CREATE_NO_CANONICALIZE) && isupperW(val)) {
2408 if(!computeOnly)
2409 uri->canon_uri[uri->canon_len] = tolowerW(val);
2410 } else {
2411 if(!computeOnly)
2412 uri->canon_uri[uri->canon_len] = val;
2414 ++uri->canon_len;
2416 /* Skip past the % encoded character. */
2417 ptr += 2;
2418 continue;
2419 } else {
2420 /* Just copy the % over. */
2421 if(!computeOnly)
2422 uri->canon_uri[uri->canon_len] = *ptr;
2423 ++uri->canon_len;
2425 } else if(*ptr == '\\') {
2426 /* Only unknown scheme types could have made it here with a '\\' in the host name. */
2427 if(!computeOnly)
2428 uri->canon_uri[uri->canon_len] = *ptr;
2429 ++uri->canon_len;
2430 } else if(!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS) &&
2431 !is_unreserved(*ptr) && !is_reserved(*ptr) && known_scheme) {
2432 if(!computeOnly) {
2433 pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
2435 /* The percent encoded value gets lower cased also. */
2436 if(!(flags & Uri_CREATE_NO_CANONICALIZE)) {
2437 uri->canon_uri[uri->canon_len+1] = tolowerW(uri->canon_uri[uri->canon_len+1]);
2438 uri->canon_uri[uri->canon_len+2] = tolowerW(uri->canon_uri[uri->canon_len+2]);
2442 uri->canon_len += 3;
2443 } else {
2444 if(!computeOnly) {
2445 if(!(flags & Uri_CREATE_NO_CANONICALIZE) && known_scheme)
2446 uri->canon_uri[uri->canon_len] = tolowerW(*ptr);
2447 else
2448 uri->canon_uri[uri->canon_len] = *ptr;
2451 ++uri->canon_len;
2455 uri->host_len = uri->canon_len - uri->host_start;
2457 if(!computeOnly)
2458 TRACE("(%p %p %x %d): Canonicalize reg_name=%s len=%d\n", data, uri, flags,
2459 computeOnly, debugstr_wn(uri->canon_uri+uri->host_start, uri->host_len),
2460 uri->host_len);
2462 if(!computeOnly)
2463 find_domain_name(uri->canon_uri+uri->host_start, uri->host_len,
2464 &(uri->domain_offset));
2466 return TRUE;
2469 /* Attempts to canonicalize an implicit IPv4 address. */
2470 static BOOL canonicalize_implicit_ipv4address(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2471 uri->host_start = uri->canon_len;
2473 TRACE("%u\n", data->implicit_ipv4);
2474 /* For unknown scheme types Window's doesn't convert
2475 * the value into an IP address, but, it still considers
2476 * it an IPv4 address.
2478 if(data->scheme_type == URL_SCHEME_UNKNOWN) {
2479 if(!computeOnly)
2480 memcpy(uri->canon_uri+uri->canon_len, data->host, data->host_len*sizeof(WCHAR));
2481 uri->canon_len += data->host_len;
2482 } else {
2483 if(!computeOnly)
2484 uri->canon_len += ui2ipv4(uri->canon_uri+uri->canon_len, data->implicit_ipv4);
2485 else
2486 uri->canon_len += ui2ipv4(NULL, data->implicit_ipv4);
2489 uri->host_len = uri->canon_len - uri->host_start;
2490 uri->host_type = Uri_HOST_IPV4;
2492 if(!computeOnly)
2493 TRACE("%p %p %x %d): Canonicalized implicit IP address=%s len=%d\n",
2494 data, uri, flags, computeOnly,
2495 debugstr_wn(uri->canon_uri+uri->host_start, uri->host_len),
2496 uri->host_len);
2498 return TRUE;
2501 /* Attempts to canonicalize an IPv4 address.
2503 * If the parse_data represents a URI that has an implicit IPv4 address
2504 * (ex. http://256/, this function will convert 256 into 0.0.1.0). If
2505 * the implicit IP address exceeds the value of UINT_MAX (maximum value
2506 * for an IPv4 address) it's canonicalized as if were a reg-name.
2508 * If the parse_data contains a partial or full IPv4 address it normalizes it.
2509 * A partial IPv4 address is something like "192.0" and would be normalized to
2510 * "192.0.0.0". With a full (or partial) IPv4 address like "192.002.01.003" would
2511 * be normalized to "192.2.1.3".
2513 * NOTES:
2514 * Window's ONLY normalizes IPv4 address for known scheme types (one that isn't
2515 * URL_SCHEME_UNKNOWN). For unknown scheme types, it simply copies the data from
2516 * the original URI into the canonicalized URI, but, it still recognizes URI's
2517 * host type as HOST_IPV4.
2519 static BOOL canonicalize_ipv4address(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2520 if(data->has_implicit_ip)
2521 return canonicalize_implicit_ipv4address(data, uri, flags, computeOnly);
2522 else {
2523 uri->host_start = uri->canon_len;
2525 /* Windows only normalizes for known scheme types. */
2526 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
2527 /* parse_data contains a partial or full IPv4 address, so normalize it. */
2528 DWORD i, octetDigitCount = 0, octetCount = 0;
2529 BOOL octetHasDigit = FALSE;
2531 for(i = 0; i < data->host_len; ++i) {
2532 if(data->host[i] == '0' && !octetHasDigit) {
2533 /* Can ignore leading zeros if:
2534 * 1) It isn't the last digit of the octet.
2535 * 2) i+1 != data->host_len
2536 * 3) i+1 != '.'
2538 if(octetDigitCount == 2 ||
2539 i+1 == data->host_len ||
2540 data->host[i+1] == '.') {
2541 if(!computeOnly)
2542 uri->canon_uri[uri->canon_len] = data->host[i];
2543 ++uri->canon_len;
2544 TRACE("Adding zero\n");
2546 } else if(data->host[i] == '.') {
2547 if(!computeOnly)
2548 uri->canon_uri[uri->canon_len] = data->host[i];
2549 ++uri->canon_len;
2551 octetDigitCount = 0;
2552 octetHasDigit = FALSE;
2553 ++octetCount;
2554 } else {
2555 if(!computeOnly)
2556 uri->canon_uri[uri->canon_len] = data->host[i];
2557 ++uri->canon_len;
2559 ++octetDigitCount;
2560 octetHasDigit = TRUE;
2564 /* Make sure the canonicalized IP address has 4 dec-octets.
2565 * If doesn't add "0" ones until there is 4;
2567 for( ; octetCount < 3; ++octetCount) {
2568 if(!computeOnly) {
2569 uri->canon_uri[uri->canon_len] = '.';
2570 uri->canon_uri[uri->canon_len+1] = '0';
2573 uri->canon_len += 2;
2575 } else {
2576 /* Windows doesn't normalize addresses in unknown schemes. */
2577 if(!computeOnly)
2578 memcpy(uri->canon_uri+uri->canon_len, data->host, data->host_len*sizeof(WCHAR));
2579 uri->canon_len += data->host_len;
2582 uri->host_len = uri->canon_len - uri->host_start;
2583 if(!computeOnly)
2584 TRACE("(%p %p %x %d): Canonicalized IPv4 address, ip=%s len=%d\n",
2585 data, uri, flags, computeOnly,
2586 debugstr_wn(uri->canon_uri+uri->host_start, uri->host_len),
2587 uri->host_len);
2590 return TRUE;
2593 /* Attempts to canonicalize the IPv6 address of the URI.
2595 * Multiple things happen during the canonicalization of an IPv6 address:
2596 * 1) Any leading zero's in an h16 component are removed.
2597 * Ex: [0001:0022::] -> [1:22::]
2599 * 2) The longest sequence of zero h16 components are compressed
2600 * into a "::" (elision). If there's a tie, the first is choosen.
2602 * Ex: [0:0:0:0:1:6:7:8] -> [::1:6:7:8]
2603 * [0:0:0:0:1:2::] -> [::1:2:0:0]
2604 * [0:0:1:2:0:0:7:8] -> [::1:2:0:0:7:8]
2606 * 3) If an IPv4 address is attached to the IPv6 address, it's
2607 * also normalized.
2608 * Ex: [::001.002.022.000] -> [::1.2.22.0]
2610 * 4) If an elision is present, but, only represents 1 h16 component
2611 * it's expanded.
2613 * Ex: [1::2:3:4:5:6:7] -> [1:0:2:3:4:5:6:7]
2615 * 5) If the IPv6 address contains an IPv4 address and there exists
2616 * at least 1 non-zero h16 component the IPv4 address is converted
2617 * into two h16 components, otherwise it's normalized and kept as is.
2619 * Ex: [::192.200.003.4] -> [::192.200.3.4]
2620 * [ffff::192.200.003.4] -> [ffff::c0c8:3041]
2622 * NOTE:
2623 * For unknown scheme types Windows simply copies the address over without any
2624 * changes.
2626 * IPv4 address can be included in an elision if all its components are 0's.
2628 static BOOL canonicalize_ipv6address(const parse_data *data, Uri *uri,
2629 DWORD flags, BOOL computeOnly) {
2630 uri->host_start = uri->canon_len;
2632 if(data->scheme_type == URL_SCHEME_UNKNOWN) {
2633 if(!computeOnly)
2634 memcpy(uri->canon_uri+uri->canon_len, data->host, data->host_len*sizeof(WCHAR));
2635 uri->canon_len += data->host_len;
2636 } else {
2637 USHORT values[8];
2638 INT elision_start;
2639 DWORD i, elision_len;
2641 if(!ipv6_to_number(&(data->ipv6_address), values)) {
2642 TRACE("(%p %p %x %d): Failed to compute numerical value for IPv6 address.\n",
2643 data, uri, flags, computeOnly);
2644 return FALSE;
2647 if(!computeOnly)
2648 uri->canon_uri[uri->canon_len] = '[';
2649 ++uri->canon_len;
2651 /* Find where the elision should occur (if any). */
2652 compute_elision_location(&(data->ipv6_address), values, &elision_start, &elision_len);
2654 TRACE("%p %p %x %d): Elision starts at %d, len=%u\n", data, uri, flags,
2655 computeOnly, elision_start, elision_len);
2657 for(i = 0; i < 8; ++i) {
2658 BOOL in_elision = (elision_start > -1 && i >= elision_start &&
2659 i < elision_start+elision_len);
2660 BOOL do_ipv4 = (i == 6 && data->ipv6_address.ipv4 && !in_elision &&
2661 data->ipv6_address.h16_count == 0);
2663 if(i == elision_start) {
2664 if(!computeOnly) {
2665 uri->canon_uri[uri->canon_len] = ':';
2666 uri->canon_uri[uri->canon_len+1] = ':';
2668 uri->canon_len += 2;
2671 /* We can ignore the current component if we're in the elision. */
2672 if(in_elision)
2673 continue;
2675 /* We only add a ':' if we're not at i == 0, or when we're at
2676 * the very end of elision range since the ':' colon was handled
2677 * earlier. Otherwise we would end up with ":::" after elision.
2679 if(i != 0 && !(elision_start > -1 && i == elision_start+elision_len)) {
2680 if(!computeOnly)
2681 uri->canon_uri[uri->canon_len] = ':';
2682 ++uri->canon_len;
2685 if(do_ipv4) {
2686 UINT val;
2687 DWORD len;
2689 /* Combine the two parts of the IPv4 address values. */
2690 val = values[i];
2691 val <<= 16;
2692 val += values[i+1];
2694 if(!computeOnly)
2695 len = ui2ipv4(uri->canon_uri+uri->canon_len, val);
2696 else
2697 len = ui2ipv4(NULL, val);
2699 uri->canon_len += len;
2700 ++i;
2701 } else {
2702 /* Write a regular h16 component to the URI. */
2704 /* Short circuit for the trivial case. */
2705 if(values[i] == 0) {
2706 if(!computeOnly)
2707 uri->canon_uri[uri->canon_len] = '0';
2708 ++uri->canon_len;
2709 } else {
2710 static const WCHAR formatW[] = {'%','x',0};
2712 if(!computeOnly)
2713 uri->canon_len += sprintfW(uri->canon_uri+uri->canon_len,
2714 formatW, values[i]);
2715 else {
2716 WCHAR tmp[5];
2717 uri->canon_len += sprintfW(tmp, formatW, values[i]);
2723 /* Add the closing ']'. */
2724 if(!computeOnly)
2725 uri->canon_uri[uri->canon_len] = ']';
2726 ++uri->canon_len;
2729 uri->host_len = uri->canon_len - uri->host_start;
2731 if(!computeOnly)
2732 TRACE("(%p %p %x %d): Canonicalized IPv6 address %s, len=%d\n", data, uri, flags,
2733 computeOnly, debugstr_wn(uri->canon_uri+uri->host_start, uri->host_len),
2734 uri->host_len);
2736 return TRUE;
2739 /* Attempts to canonicalize the host of the URI (if any). */
2740 static BOOL canonicalize_host(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2741 uri->host_start = -1;
2742 uri->host_len = 0;
2743 uri->domain_offset = -1;
2745 if(data->host) {
2746 switch(data->host_type) {
2747 case Uri_HOST_DNS:
2748 uri->host_type = Uri_HOST_DNS;
2749 if(!canonicalize_reg_name(data, uri, flags, computeOnly))
2750 return FALSE;
2752 break;
2753 case Uri_HOST_IPV4:
2754 uri->host_type = Uri_HOST_IPV4;
2755 if(!canonicalize_ipv4address(data, uri, flags, computeOnly))
2756 return FALSE;
2758 break;
2759 case Uri_HOST_IPV6:
2760 if(!canonicalize_ipv6address(data, uri, flags, computeOnly))
2761 return FALSE;
2763 uri->host_type = Uri_HOST_IPV6;
2764 break;
2765 case Uri_HOST_UNKNOWN:
2766 if(data->host_len > 0 || data->scheme_type != URL_SCHEME_FILE) {
2767 uri->host_start = uri->canon_len;
2769 /* Nothing happens to unknown host types. */
2770 if(!computeOnly)
2771 memcpy(uri->canon_uri+uri->canon_len, data->host, data->host_len*sizeof(WCHAR));
2772 uri->canon_len += data->host_len;
2773 uri->host_len = data->host_len;
2776 uri->host_type = Uri_HOST_UNKNOWN;
2777 break;
2778 default:
2779 FIXME("(%p %p %x %d): Canonicalization for host type %d not supported.\n", data,
2780 uri, flags, computeOnly, data->host_type);
2781 return FALSE;
2785 return TRUE;
2788 static BOOL canonicalize_port(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2789 BOOL has_default_port = FALSE;
2790 USHORT default_port = 0;
2791 DWORD i;
2793 uri->port_offset = -1;
2795 /* Check if the scheme has a default port. */
2796 for(i = 0; i < sizeof(default_ports)/sizeof(default_ports[0]); ++i) {
2797 if(default_ports[i].scheme == data->scheme_type) {
2798 has_default_port = TRUE;
2799 default_port = default_ports[i].port;
2800 break;
2804 uri->has_port = data->has_port || has_default_port;
2806 /* Possible cases:
2807 * 1) Has a port which is the default port.
2808 * 2) Has a port (not the default).
2809 * 3) Doesn't have a port, but, scheme has a default port.
2810 * 4) No port.
2812 if(has_default_port && data->has_port && data->port_value == default_port) {
2813 /* If it's the default port and this flag isn't set, don't do anything. */
2814 if(flags & Uri_CREATE_NO_CANONICALIZE) {
2815 uri->port_offset = uri->canon_len-uri->authority_start;
2816 if(!computeOnly)
2817 uri->canon_uri[uri->canon_len] = ':';
2818 ++uri->canon_len;
2820 if(data->port) {
2821 /* Copy the original port over. */
2822 if(!computeOnly)
2823 memcpy(uri->canon_uri+uri->canon_len, data->port, data->port_len*sizeof(WCHAR));
2824 uri->canon_len += data->port_len;
2825 } else {
2826 if(!computeOnly)
2827 uri->canon_len += ui2str(uri->canon_uri+uri->canon_len, data->port_value);
2828 else
2829 uri->canon_len += ui2str(NULL, data->port_value);
2833 uri->port = default_port;
2834 } else if(data->has_port) {
2835 uri->port_offset = uri->canon_len-uri->authority_start;
2836 if(!computeOnly)
2837 uri->canon_uri[uri->canon_len] = ':';
2838 ++uri->canon_len;
2840 if(flags & Uri_CREATE_NO_CANONICALIZE && data->port) {
2841 /* Copy the original over without changes. */
2842 if(!computeOnly)
2843 memcpy(uri->canon_uri+uri->canon_len, data->port, data->port_len*sizeof(WCHAR));
2844 uri->canon_len += data->port_len;
2845 } else {
2846 if(!computeOnly)
2847 uri->canon_len += ui2str(uri->canon_uri+uri->canon_len, data->port_value);
2848 else
2849 uri->canon_len += ui2str(NULL, data->port_value);
2852 uri->port = data->port_value;
2853 } else if(has_default_port)
2854 uri->port = default_port;
2856 return TRUE;
2859 /* Canonicalizes the authority of the URI represented by the parse_data. */
2860 static BOOL canonicalize_authority(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2861 uri->authority_start = uri->canon_len;
2862 uri->authority_len = 0;
2864 if(!canonicalize_userinfo(data, uri, flags, computeOnly))
2865 return FALSE;
2867 if(!canonicalize_host(data, uri, flags, computeOnly))
2868 return FALSE;
2870 if(!canonicalize_port(data, uri, flags, computeOnly))
2871 return FALSE;
2873 if(uri->host_start != -1 || (data->is_relative && (data->password || data->username)))
2874 uri->authority_len = uri->canon_len - uri->authority_start;
2875 else
2876 uri->authority_start = -1;
2878 return TRUE;
2881 /* Attempts to canonicalize the path of a hierarchical URI.
2883 * Things that happen:
2884 * 1). Forbidden characters are percent encoded, unless the NO_ENCODE_FORBIDDEN
2885 * flag is set or it's a file URI. Forbidden characters are always encoded
2886 * for file schemes reguardless and forbidden characters are never encoded
2887 * for unknown scheme types.
2889 * 2). For known scheme types '\\' are changed to '/'.
2891 * 3). Percent encoded, unreserved characters are decoded to their actual values.
2892 * Unless the scheme type is unknown. For file schemes any percent encoded
2893 * character in the unreserved or reserved set is decoded.
2895 * 4). For File schemes if the path is starts with a drive letter and doesn't
2896 * start with a '/' then one is appended.
2897 * Ex: file://c:/test.mp3 -> file:///c:/test.mp3
2899 * 5). Dot segments are removed from the path for all scheme types
2900 * unless NO_CANONICALIZE flag is set. Dot segments aren't removed
2901 * for wildcard scheme types.
2903 * NOTES:
2904 * file://c:/test%20test -> file:///c:/test%2520test
2905 * file://c:/test%3Etest -> file:///c:/test%253Etest
2906 * if Uri_CREATE_FILE_USE_DOS_PATH is not set:
2907 * file:///c:/test%20test -> file:///c:/test%20test
2908 * file:///c:/test%test -> file:///c:/test%25test
2910 static BOOL canonicalize_path_hierarchical(const parse_data *data, Uri *uri,
2911 DWORD flags, BOOL computeOnly) {
2912 const WCHAR *ptr;
2913 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
2914 const BOOL is_file = data->scheme_type == URL_SCHEME_FILE;
2916 BOOL escape_pct = FALSE;
2918 if(!data->path) {
2919 uri->path_start = -1;
2920 uri->path_len = 0;
2921 return TRUE;
2924 uri->path_start = uri->canon_len;
2925 ptr = data->path;
2927 if(is_file && uri->host_start == -1) {
2928 /* Check if a '/' needs to be appended for the file scheme. */
2929 if(data->path_len > 1 && is_drive_path(ptr) && !(flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
2930 if(!computeOnly)
2931 uri->canon_uri[uri->canon_len] = '/';
2932 uri->canon_len++;
2933 escape_pct = TRUE;
2934 } else if(*ptr == '/') {
2935 if(!(flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
2936 /* Copy the extra '/' over. */
2937 if(!computeOnly)
2938 uri->canon_uri[uri->canon_len] = '/';
2939 ++uri->canon_len;
2941 ++ptr;
2944 if(is_drive_path(ptr)) {
2945 if(!computeOnly) {
2946 uri->canon_uri[uri->canon_len] = *ptr;
2947 /* If theres a '|' after the drive letter, convert it to a ':'. */
2948 uri->canon_uri[uri->canon_len+1] = ':';
2950 ptr += 2;
2951 uri->canon_len += 2;
2955 if(!is_file && *(data->path) && *(data->path) != '/') {
2956 /* Prepend a '/' to the path if it doesn't have one. */
2957 if(!computeOnly)
2958 uri->canon_uri[uri->canon_len] = '/';
2959 ++uri->canon_len;
2962 for(; ptr < data->path+data->path_len; ++ptr) {
2963 if(*ptr == '%') {
2964 const WCHAR *tmp = ptr;
2965 WCHAR val;
2967 /* Check if the % represents a valid encoded char, or if it needs encoded. */
2968 BOOL force_encode = !check_pct_encoded(&tmp) && is_file && !(flags&Uri_CREATE_FILE_USE_DOS_PATH);
2969 val = decode_pct_val(ptr);
2971 if(force_encode || escape_pct) {
2972 /* Escape the percent sign in the file URI. */
2973 if(!computeOnly)
2974 pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
2975 uri->canon_len += 3;
2976 } else if((is_unreserved(val) && known_scheme) ||
2977 (is_file && (is_unreserved(val) || is_reserved(val) ||
2978 (val && flags&Uri_CREATE_FILE_USE_DOS_PATH && !is_forbidden_dos_path_char(val))))) {
2979 if(!computeOnly)
2980 uri->canon_uri[uri->canon_len] = val;
2981 ++uri->canon_len;
2983 ptr += 2;
2984 continue;
2985 } else {
2986 if(!computeOnly)
2987 uri->canon_uri[uri->canon_len] = *ptr;
2988 ++uri->canon_len;
2990 } else if(*ptr == '/' && is_file && (flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
2991 /* Convert the '/' back to a '\\'. */
2992 if(!computeOnly)
2993 uri->canon_uri[uri->canon_len] = '\\';
2994 ++uri->canon_len;
2995 } else if(*ptr == '\\' && known_scheme) {
2996 if(is_file && (flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
2997 /* Don't convert the '\\' to a '/'. */
2998 if(!computeOnly)
2999 uri->canon_uri[uri->canon_len] = *ptr;
3000 ++uri->canon_len;
3001 } else {
3002 if(!computeOnly)
3003 uri->canon_uri[uri->canon_len] = '/';
3004 ++uri->canon_len;
3006 } else if(known_scheme && !is_unreserved(*ptr) && !is_reserved(*ptr) &&
3007 (!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS) || is_file)) {
3008 if(is_file && (flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
3009 /* Don't escape the character. */
3010 if(!computeOnly)
3011 uri->canon_uri[uri->canon_len] = *ptr;
3012 ++uri->canon_len;
3013 } else {
3014 /* Escape the forbidden character. */
3015 if(!computeOnly)
3016 pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
3017 uri->canon_len += 3;
3019 } else {
3020 if(!computeOnly)
3021 uri->canon_uri[uri->canon_len] = *ptr;
3022 ++uri->canon_len;
3026 uri->path_len = uri->canon_len - uri->path_start;
3028 /* Removing the dot segments only happens when it's not in
3029 * computeOnly mode and it's not a wildcard scheme. File schemes
3030 * with USE_DOS_PATH set don't get dot segments removed.
3032 if(!(is_file && (flags & Uri_CREATE_FILE_USE_DOS_PATH)) &&
3033 data->scheme_type != URL_SCHEME_WILDCARD) {
3034 if(!(flags & Uri_CREATE_NO_CANONICALIZE) && !computeOnly) {
3035 /* Remove the dot segments (if any) and reset everything to the new
3036 * correct length.
3038 DWORD new_len = remove_dot_segments(uri->canon_uri+uri->path_start, uri->path_len);
3039 uri->canon_len -= uri->path_len-new_len;
3040 uri->path_len = new_len;
3044 if(!computeOnly)
3045 TRACE("Canonicalized path %s len=%d\n",
3046 debugstr_wn(uri->canon_uri+uri->path_start, uri->path_len),
3047 uri->path_len);
3049 return TRUE;
3052 /* Attempts to canonicalize the path for an opaque URI.
3054 * For known scheme types:
3055 * 1) forbidden characters are percent encoded if
3056 * NO_ENCODE_FORBIDDEN_CHARACTERS isn't set.
3058 * 2) Percent encoded, unreserved characters are decoded
3059 * to their actual values, for known scheme types.
3061 * 3) '\\' are changed to '/' for known scheme types
3062 * except for mailto schemes.
3064 * 4) For file schemes, if USE_DOS_PATH is set all '/'
3065 * are converted to backslashes.
3067 * 5) For file schemes, if USE_DOS_PATH isn't set all '\'
3068 * are converted to forward slashes.
3070 static BOOL canonicalize_path_opaque(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
3071 const WCHAR *ptr;
3072 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
3073 const BOOL is_file = data->scheme_type == URL_SCHEME_FILE;
3075 if(!data->path) {
3076 uri->path_start = -1;
3077 uri->path_len = 0;
3078 return TRUE;
3081 uri->path_start = uri->canon_len;
3083 /* Windows doesn't allow a "//" to appear after the scheme
3084 * of a URI, if it's an opaque URI.
3086 if(data->scheme && *(data->path) == '/' && *(data->path+1) == '/') {
3087 /* So it inserts a "/." before the "//" if it exists. */
3088 if(!computeOnly) {
3089 uri->canon_uri[uri->canon_len] = '/';
3090 uri->canon_uri[uri->canon_len+1] = '.';
3093 uri->canon_len += 2;
3096 for(ptr = data->path; ptr < data->path+data->path_len; ++ptr) {
3097 if(*ptr == '%' && known_scheme) {
3098 WCHAR val = decode_pct_val(ptr);
3100 if(is_unreserved(val)) {
3101 if(!computeOnly)
3102 uri->canon_uri[uri->canon_len] = val;
3103 ++uri->canon_len;
3105 ptr += 2;
3106 continue;
3107 } else {
3108 if(!computeOnly)
3109 uri->canon_uri[uri->canon_len] = *ptr;
3110 ++uri->canon_len;
3112 } else if(*ptr == '/' && is_file && (flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
3113 if(!computeOnly)
3114 uri->canon_uri[uri->canon_len] = '\\';
3115 ++uri->canon_len;
3116 } else if(*ptr == '\\' && is_file) {
3117 if(!(flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
3118 /* Convert to a '/'. */
3119 if(!computeOnly)
3120 uri->canon_uri[uri->canon_len] = '/';
3121 ++uri->canon_len;
3122 } else {
3123 /* Just copy it over. */
3124 if(!computeOnly)
3125 uri->canon_uri[uri->canon_len] = *ptr;
3126 ++uri->canon_len;
3128 } else if(known_scheme && !is_unreserved(*ptr) && !is_reserved(*ptr) &&
3129 !(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS)) {
3130 if(is_file && (flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
3131 /* Forbidden characters aren't percent encoded for file schemes
3132 * with USE_DOS_PATH set.
3134 if(!computeOnly)
3135 uri->canon_uri[uri->canon_len] = *ptr;
3136 ++uri->canon_len;
3137 } else if(data->scheme_type == URL_SCHEME_MK && *ptr == '\\') {
3138 /* MK URIs don't get '\\' percent encoded. */
3139 if(!computeOnly)
3140 uri->canon_uri[uri->canon_len] = *ptr;
3141 ++uri->canon_len;
3142 } else {
3143 if(!computeOnly)
3144 pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
3145 uri->canon_len += 3;
3147 } else {
3148 if(!computeOnly)
3149 uri->canon_uri[uri->canon_len] = *ptr;
3150 ++uri->canon_len;
3154 uri->path_len = uri->canon_len - uri->path_start;
3156 TRACE("(%p %p %x %d): Canonicalized opaque URI path %s len=%d\n", data, uri, flags, computeOnly,
3157 debugstr_wn(uri->canon_uri+uri->path_start, uri->path_len), uri->path_len);
3158 return TRUE;
3161 /* Determines how the URI represented by the parse_data should be canonicalized.
3163 * Essentially, if the parse_data represents an hierarchical URI then it calls
3164 * canonicalize_authority and the canonicalization functions for the path. If the
3165 * URI is opaque it canonicalizes the path of the URI.
3167 static BOOL canonicalize_hierpart(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
3168 if(!data->is_opaque || (data->is_relative && (data->password || data->username))) {
3169 /* "//" is only added for non-wildcard scheme types.
3171 * A "//" is only added to a relative URI if it has a
3172 * host or port component (this only happens if a IUriBuilder
3173 * is generating an IUri).
3175 if((data->is_relative && (data->host || data->has_port)) ||
3176 (!data->is_relative && data->scheme_type != URL_SCHEME_WILDCARD)) {
3177 if(!computeOnly) {
3178 INT pos = uri->canon_len;
3180 uri->canon_uri[pos] = '/';
3181 uri->canon_uri[pos+1] = '/';
3183 uri->canon_len += 2;
3186 if(!canonicalize_authority(data, uri, flags, computeOnly))
3187 return FALSE;
3189 if(data->is_relative && (data->password || data->username)) {
3190 if(!canonicalize_path_opaque(data, uri, flags, computeOnly))
3191 return FALSE;
3192 } else {
3193 if(!canonicalize_path_hierarchical(data, uri, flags, computeOnly))
3194 return FALSE;
3196 } else {
3197 /* Opaque URI's don't have an authority. */
3198 uri->userinfo_start = uri->userinfo_split = -1;
3199 uri->userinfo_len = 0;
3200 uri->host_start = -1;
3201 uri->host_len = 0;
3202 uri->host_type = Uri_HOST_UNKNOWN;
3203 uri->has_port = FALSE;
3204 uri->authority_start = -1;
3205 uri->authority_len = 0;
3206 uri->domain_offset = -1;
3207 uri->port_offset = -1;
3209 if(is_hierarchical_scheme(data->scheme_type)) {
3210 DWORD i;
3212 /* Absolute URIs aren't displayed for known scheme types
3213 * which should be hierarchical URIs.
3215 uri->display_modifiers |= URI_DISPLAY_NO_ABSOLUTE_URI;
3217 /* Windows also sets the port for these (if they have one). */
3218 for(i = 0; i < sizeof(default_ports)/sizeof(default_ports[0]); ++i) {
3219 if(data->scheme_type == default_ports[i].scheme) {
3220 uri->has_port = TRUE;
3221 uri->port = default_ports[i].port;
3222 break;
3227 if(!canonicalize_path_opaque(data, uri, flags, computeOnly))
3228 return FALSE;
3231 if(uri->path_start > -1 && !computeOnly)
3232 /* Finding file extensions happens for both types of URIs. */
3233 uri->extension_offset = find_file_extension(uri->canon_uri+uri->path_start, uri->path_len);
3234 else
3235 uri->extension_offset = -1;
3237 return TRUE;
3240 /* Attempts to canonicalize the query string of the URI.
3242 * Things that happen:
3243 * 1) For known scheme types forbidden characters
3244 * are percent encoded, unless the NO_DECODE_EXTRA_INFO flag is set
3245 * or NO_ENCODE_FORBIDDEN_CHARACTERS is set.
3247 * 2) For known scheme types, percent encoded, unreserved characters
3248 * are decoded as long as the NO_DECODE_EXTRA_INFO flag isn't set.
3250 static BOOL canonicalize_query(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
3251 const WCHAR *ptr, *end;
3252 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
3254 if(!data->query) {
3255 uri->query_start = -1;
3256 uri->query_len = 0;
3257 return TRUE;
3260 uri->query_start = uri->canon_len;
3262 end = data->query+data->query_len;
3263 for(ptr = data->query; ptr < end; ++ptr) {
3264 if(*ptr == '%') {
3265 if(known_scheme && !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
3266 WCHAR val = decode_pct_val(ptr);
3267 if(is_unreserved(val)) {
3268 if(!computeOnly)
3269 uri->canon_uri[uri->canon_len] = val;
3270 ++uri->canon_len;
3272 ptr += 2;
3273 continue;
3276 } else if(known_scheme && !is_unreserved(*ptr) && !is_reserved(*ptr)) {
3277 if(!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS) &&
3278 !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
3279 if(!computeOnly)
3280 pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
3281 uri->canon_len += 3;
3282 continue;
3286 if(!computeOnly)
3287 uri->canon_uri[uri->canon_len] = *ptr;
3288 ++uri->canon_len;
3291 uri->query_len = uri->canon_len - uri->query_start;
3293 if(!computeOnly)
3294 TRACE("(%p %p %x %d): Canonicalized query string %s len=%d\n", data, uri, flags,
3295 computeOnly, debugstr_wn(uri->canon_uri+uri->query_start, uri->query_len),
3296 uri->query_len);
3297 return TRUE;
3300 static BOOL canonicalize_fragment(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
3301 const WCHAR *ptr, *end;
3302 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
3304 if(!data->fragment) {
3305 uri->fragment_start = -1;
3306 uri->fragment_len = 0;
3307 return TRUE;
3310 uri->fragment_start = uri->canon_len;
3312 end = data->fragment + data->fragment_len;
3313 for(ptr = data->fragment; ptr < end; ++ptr) {
3314 if(*ptr == '%') {
3315 if(known_scheme && !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
3316 WCHAR val = decode_pct_val(ptr);
3317 if(is_unreserved(val)) {
3318 if(!computeOnly)
3319 uri->canon_uri[uri->canon_len] = val;
3320 ++uri->canon_len;
3322 ptr += 2;
3323 continue;
3326 } else if(known_scheme && !is_unreserved(*ptr) && !is_reserved(*ptr)) {
3327 if(!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS) &&
3328 !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
3329 if(!computeOnly)
3330 pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
3331 uri->canon_len += 3;
3332 continue;
3336 if(!computeOnly)
3337 uri->canon_uri[uri->canon_len] = *ptr;
3338 ++uri->canon_len;
3341 uri->fragment_len = uri->canon_len - uri->fragment_start;
3343 if(!computeOnly)
3344 TRACE("(%p %p %x %d): Canonicalized fragment %s len=%d\n", data, uri, flags,
3345 computeOnly, debugstr_wn(uri->canon_uri+uri->fragment_start, uri->fragment_len),
3346 uri->fragment_len);
3347 return TRUE;
3350 /* Canonicalizes the scheme information specified in the parse_data using the specified flags. */
3351 static BOOL canonicalize_scheme(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
3352 uri->scheme_start = -1;
3353 uri->scheme_len = 0;
3355 if(!data->scheme) {
3356 /* The only type of URI that doesn't have to have a scheme is a relative
3357 * URI.
3359 if(!data->is_relative) {
3360 FIXME("(%p %p %x): Unable to determine the scheme type of %s.\n", data,
3361 uri, flags, debugstr_w(data->uri));
3362 return FALSE;
3364 } else {
3365 if(!computeOnly) {
3366 DWORD i;
3367 INT pos = uri->canon_len;
3369 for(i = 0; i < data->scheme_len; ++i) {
3370 /* Scheme name must be lower case after canonicalization. */
3371 uri->canon_uri[i + pos] = tolowerW(data->scheme[i]);
3374 uri->canon_uri[i + pos] = ':';
3375 uri->scheme_start = pos;
3377 TRACE("(%p %p %x): Canonicalized scheme=%s, len=%d.\n", data, uri, flags,
3378 debugstr_wn(uri->canon_uri, uri->scheme_len), data->scheme_len);
3381 /* This happens in both computation modes. */
3382 uri->canon_len += data->scheme_len + 1;
3383 uri->scheme_len = data->scheme_len;
3385 return TRUE;
3388 /* Compute's what the length of the URI specified by the parse_data will be
3389 * after canonicalization occurs using the specified flags.
3391 * This function will return a non-zero value indicating the length of the canonicalized
3392 * URI, or -1 on error.
3394 static int compute_canonicalized_length(const parse_data *data, DWORD flags) {
3395 Uri uri;
3397 memset(&uri, 0, sizeof(Uri));
3399 TRACE("(%p %x): Beginning to compute canonicalized length for URI %s\n", data, flags,
3400 debugstr_w(data->uri));
3402 if(!canonicalize_scheme(data, &uri, flags, TRUE)) {
3403 ERR("(%p %x): Failed to compute URI scheme length.\n", data, flags);
3404 return -1;
3407 if(!canonicalize_hierpart(data, &uri, flags, TRUE)) {
3408 ERR("(%p %x): Failed to compute URI hierpart length.\n", data, flags);
3409 return -1;
3412 if(!canonicalize_query(data, &uri, flags, TRUE)) {
3413 ERR("(%p %x): Failed to compute query string length.\n", data, flags);
3414 return -1;
3417 if(!canonicalize_fragment(data, &uri, flags, TRUE)) {
3418 ERR("(%p %x): Failed to compute fragment length.\n", data, flags);
3419 return -1;
3422 TRACE("(%p %x): Finished computing canonicalized URI length. length=%d\n", data, flags, uri.canon_len);
3424 return uri.canon_len;
3427 /* Canonicalizes the URI data specified in the parse_data, using the given flags. If the
3428 * canonicalization succeededs it will store all the canonicalization information
3429 * in the pointer to the Uri.
3431 * To canonicalize a URI this function first computes what the length of the URI
3432 * specified by the parse_data will be. Once this is done it will then perfom the actual
3433 * canonicalization of the URI.
3435 static HRESULT canonicalize_uri(const parse_data *data, Uri *uri, DWORD flags) {
3436 INT len;
3438 uri->canon_uri = NULL;
3439 len = uri->canon_size = uri->canon_len = 0;
3441 TRACE("(%p %p %x): beginning to canonicalize URI %s.\n", data, uri, flags, debugstr_w(data->uri));
3443 /* First try to compute the length of the URI. */
3444 len = compute_canonicalized_length(data, flags);
3445 if(len == -1) {
3446 ERR("(%p %p %x): Could not compute the canonicalized length of %s.\n", data, uri, flags,
3447 debugstr_w(data->uri));
3448 return E_INVALIDARG;
3451 uri->canon_uri = heap_alloc((len+1)*sizeof(WCHAR));
3452 if(!uri->canon_uri)
3453 return E_OUTOFMEMORY;
3455 uri->canon_size = len;
3456 if(!canonicalize_scheme(data, uri, flags, FALSE)) {
3457 ERR("(%p %p %x): Unable to canonicalize the scheme of the URI.\n", data, uri, flags);
3458 return E_INVALIDARG;
3460 uri->scheme_type = data->scheme_type;
3462 if(!canonicalize_hierpart(data, uri, flags, FALSE)) {
3463 ERR("(%p %p %x): Unable to canonicalize the heirpart of the URI\n", data, uri, flags);
3464 return E_INVALIDARG;
3467 if(!canonicalize_query(data, uri, flags, FALSE)) {
3468 ERR("(%p %p %x): Unable to canonicalize query string of the URI.\n",
3469 data, uri, flags);
3470 return E_INVALIDARG;
3473 if(!canonicalize_fragment(data, uri, flags, FALSE)) {
3474 ERR("(%p %p %x): Unable to canonicalize fragment of the URI.\n",
3475 data, uri, flags);
3476 return E_INVALIDARG;
3479 /* There's a possibility we didn't use all the space we allocated
3480 * earlier.
3482 if(uri->canon_len < uri->canon_size) {
3483 /* This happens if the URI is hierarchical and dot
3484 * segments were removed from it's path.
3486 WCHAR *tmp = heap_realloc(uri->canon_uri, (uri->canon_len+1)*sizeof(WCHAR));
3487 if(!tmp)
3488 return E_OUTOFMEMORY;
3490 uri->canon_uri = tmp;
3491 uri->canon_size = uri->canon_len;
3494 uri->canon_uri[uri->canon_len] = '\0';
3495 TRACE("(%p %p %x): finished canonicalizing the URI. uri=%s\n", data, uri, flags, debugstr_w(uri->canon_uri));
3497 return S_OK;
3500 static HRESULT get_builder_component(LPWSTR *component, DWORD *component_len,
3501 LPCWSTR source, DWORD source_len,
3502 LPCWSTR *output, DWORD *output_len)
3504 if(!output_len) {
3505 if(output)
3506 *output = NULL;
3507 return E_POINTER;
3510 if(!output) {
3511 *output_len = 0;
3512 return E_POINTER;
3515 if(!(*component) && source) {
3516 /* Allocate 'component', and copy the contents from 'source'
3517 * into the new allocation.
3519 *component = heap_alloc((source_len+1)*sizeof(WCHAR));
3520 if(!(*component))
3521 return E_OUTOFMEMORY;
3523 memcpy(*component, source, source_len*sizeof(WCHAR));
3524 (*component)[source_len] = '\0';
3525 *component_len = source_len;
3528 *output = *component;
3529 *output_len = *component_len;
3530 return *output ? S_OK : S_FALSE;
3533 /* Allocates 'component' and copies the string from 'new_value' into 'component'.
3534 * If 'prefix' is set and 'new_value' isn't NULL, then it checks if 'new_value'
3535 * starts with 'prefix'. If it doesn't then 'prefix' is prepended to 'component'.
3537 * If everything is successful, then will set 'success_flag' in 'flags'.
3539 static HRESULT set_builder_component(LPWSTR *component, DWORD *component_len, LPCWSTR new_value,
3540 WCHAR prefix, DWORD *flags, DWORD success_flag)
3542 heap_free(*component);
3544 if(!new_value) {
3545 *component = NULL;
3546 *component_len = 0;
3547 } else {
3548 BOOL add_prefix = FALSE;
3549 DWORD len = lstrlenW(new_value);
3550 DWORD pos = 0;
3552 if(prefix && *new_value != prefix) {
3553 add_prefix = TRUE;
3554 *component = heap_alloc((len+2)*sizeof(WCHAR));
3555 } else
3556 *component = heap_alloc((len+1)*sizeof(WCHAR));
3558 if(!(*component))
3559 return E_OUTOFMEMORY;
3561 if(add_prefix)
3562 (*component)[pos++] = prefix;
3564 memcpy(*component+pos, new_value, (len+1)*sizeof(WCHAR));
3565 *component_len = len+pos;
3568 *flags |= success_flag;
3569 return S_OK;
3572 #define URI(x) ((IUri*) &(x)->lpIUriVtbl)
3573 #define URIBUILDER(x) ((IUriBuilder*) &(x)->lpIUriBuilderVtbl)
3575 static void reset_builder(UriBuilder *builder) {
3576 if(builder->uri)
3577 IUri_Release(URI(builder->uri));
3578 builder->uri = NULL;
3580 heap_free(builder->fragment);
3581 builder->fragment = NULL;
3582 builder->fragment_len = 0;
3584 heap_free(builder->host);
3585 builder->host = NULL;
3586 builder->host_len = 0;
3588 heap_free(builder->password);
3589 builder->password = NULL;
3590 builder->password_len = 0;
3592 heap_free(builder->path);
3593 builder->path = NULL;
3594 builder->path_len = 0;
3596 heap_free(builder->query);
3597 builder->query = NULL;
3598 builder->query_len = 0;
3600 heap_free(builder->scheme);
3601 builder->scheme = NULL;
3602 builder->scheme_len = 0;
3604 heap_free(builder->username);
3605 builder->username = NULL;
3606 builder->username_len = 0;
3608 builder->has_port = FALSE;
3609 builder->port = 0;
3610 builder->modified_props = 0;
3613 static HRESULT validate_scheme_name(const UriBuilder *builder, parse_data *data, DWORD flags) {
3614 const WCHAR *component;
3615 const WCHAR *ptr;
3616 const WCHAR **pptr;
3617 DWORD expected_len;
3619 if(builder->scheme) {
3620 ptr = builder->scheme;
3621 expected_len = builder->scheme_len;
3622 } else if(builder->uri && builder->uri->scheme_start > -1) {
3623 ptr = builder->uri->canon_uri+builder->uri->scheme_start;
3624 expected_len = builder->uri->scheme_len;
3625 } else {
3626 static const WCHAR nullW[] = {0};
3627 ptr = nullW;
3628 expected_len = 0;
3631 component = ptr;
3632 pptr = &ptr;
3633 if(parse_scheme(pptr, data, flags, ALLOW_NULL_TERM_SCHEME) &&
3634 data->scheme_len == expected_len) {
3635 if(data->scheme)
3636 TRACE("(%p %p %x): Found valid scheme component %s len=%d.\n", builder, data, flags,
3637 debugstr_wn(data->scheme, data->scheme_len), data->scheme_len);
3638 } else {
3639 TRACE("(%p %p %x): Invalid scheme component found %s.\n", builder, data, flags,
3640 debugstr_wn(component, expected_len));
3641 return INET_E_INVALID_URL;
3644 return S_OK;
3647 static HRESULT validate_username(const UriBuilder *builder, parse_data *data, DWORD flags) {
3648 const WCHAR *ptr;
3649 const WCHAR **pptr;
3650 DWORD expected_len;
3652 if(builder->username) {
3653 ptr = builder->username;
3654 expected_len = builder->username_len;
3655 } else if(!(builder->modified_props & Uri_HAS_USER_NAME) && builder->uri &&
3656 builder->uri->userinfo_start > -1 && builder->uri->userinfo_split != 0) {
3657 /* Just use the username from the base Uri. */
3658 data->username = builder->uri->canon_uri+builder->uri->userinfo_start;
3659 data->username_len = (builder->uri->userinfo_split > -1) ?
3660 builder->uri->userinfo_split : builder->uri->userinfo_len;
3661 ptr = NULL;
3662 } else {
3663 ptr = NULL;
3664 expected_len = 0;
3667 if(ptr) {
3668 const WCHAR *component = ptr;
3669 pptr = &ptr;
3670 if(parse_username(pptr, data, flags, ALLOW_NULL_TERM_USER_NAME) &&
3671 data->username_len == expected_len)
3672 TRACE("(%p %p %x): Found valid username component %s len=%d.\n", builder, data, flags,
3673 debugstr_wn(data->username, data->username_len), data->username_len);
3674 else {
3675 TRACE("(%p %p %x): Invalid username component found %s.\n", builder, data, flags,
3676 debugstr_wn(component, expected_len));
3677 return INET_E_INVALID_URL;
3681 return S_OK;
3684 static HRESULT validate_password(const UriBuilder *builder, parse_data *data, DWORD flags) {
3685 const WCHAR *ptr;
3686 const WCHAR **pptr;
3687 DWORD expected_len;
3689 if(builder->password) {
3690 ptr = builder->password;
3691 expected_len = builder->password_len;
3692 } else if(!(builder->modified_props & Uri_HAS_PASSWORD) && builder->uri &&
3693 builder->uri->userinfo_split > -1) {
3694 data->password = builder->uri->canon_uri+builder->uri->userinfo_start+builder->uri->userinfo_split+1;
3695 data->password_len = builder->uri->userinfo_len-builder->uri->userinfo_split-1;
3696 ptr = NULL;
3697 } else {
3698 ptr = NULL;
3699 expected_len = 0;
3702 if(ptr) {
3703 const WCHAR *component = ptr;
3704 pptr = &ptr;
3705 if(parse_password(pptr, data, flags, ALLOW_NULL_TERM_PASSWORD) &&
3706 data->password_len == expected_len)
3707 TRACE("(%p %p %x): Found valid password component %s len=%d.\n", builder, data, flags,
3708 debugstr_wn(data->password, data->password_len), data->password_len);
3709 else {
3710 TRACE("(%p %p %x): Invalid password component found %s.\n", builder, data, flags,
3711 debugstr_wn(component, expected_len));
3712 return INET_E_INVALID_URL;
3716 return S_OK;
3719 static HRESULT validate_userinfo(const UriBuilder *builder, parse_data *data, DWORD flags) {
3720 HRESULT hr;
3722 hr = validate_username(builder, data, flags);
3723 if(FAILED(hr))
3724 return hr;
3726 hr = validate_password(builder, data, flags);
3727 if(FAILED(hr))
3728 return hr;
3730 return S_OK;
3733 static HRESULT validate_host(const UriBuilder *builder, parse_data *data, DWORD flags) {
3734 const WCHAR *ptr;
3735 const WCHAR **pptr;
3736 DWORD expected_len;
3738 if(builder->host) {
3739 ptr = builder->host;
3740 expected_len = builder->host_len;
3741 } else if(!(builder->modified_props & Uri_HAS_HOST) && builder->uri && builder->uri->host_start > -1) {
3742 ptr = builder->uri->canon_uri + builder->uri->host_start;
3743 expected_len = builder->uri->host_len;
3744 } else
3745 ptr = NULL;
3747 if(ptr) {
3748 const WCHAR *component = ptr;
3749 DWORD extras = ALLOW_BRACKETLESS_IP_LITERAL|IGNORE_PORT_DELIMITER|SKIP_IP_FUTURE_CHECK;
3750 pptr = &ptr;
3752 if(parse_host(pptr, data, flags, extras) && data->host_len == expected_len)
3753 TRACE("(%p %p %x): Found valid host name %s len=%d type=%d.\n", builder, data, flags,
3754 debugstr_wn(data->host, data->host_len), data->host_len, data->host_type);
3755 else {
3756 TRACE("(%p %p %x): Invalid host name found %s.\n", builder, data, flags,
3757 debugstr_wn(component, expected_len));
3758 return INET_E_INVALID_URL;
3762 return S_OK;
3765 static void setup_port(const UriBuilder *builder, parse_data *data, DWORD flags) {
3766 if(builder->modified_props & Uri_HAS_PORT) {
3767 if(builder->has_port) {
3768 data->has_port = TRUE;
3769 data->port_value = builder->port;
3771 } else if(builder->uri && builder->uri->has_port) {
3772 data->has_port = TRUE;
3773 data->port_value = builder->uri->port;
3776 if(data->has_port)
3777 TRACE("(%p %p %x): Using %u as port for IUri.\n", builder, data, flags, data->port_value);
3780 static HRESULT validate_path(const UriBuilder *builder, parse_data *data, DWORD flags) {
3781 const WCHAR *ptr = NULL;
3782 const WCHAR *component;
3783 const WCHAR **pptr;
3784 DWORD expected_len;
3785 BOOL check_len = TRUE;
3786 BOOL valid = FALSE;
3788 if(builder->path) {
3789 ptr = builder->path;
3790 expected_len = builder->path_len;
3791 } else if(!(builder->modified_props & Uri_HAS_PATH) &&
3792 builder->uri && builder->uri->path_start > -1) {
3793 ptr = builder->uri->canon_uri+builder->uri->path_start;
3794 expected_len = builder->uri->path_len;
3795 } else {
3796 static const WCHAR nullW[] = {0};
3797 ptr = nullW;
3798 check_len = FALSE;
3801 component = ptr;
3802 pptr = &ptr;
3804 /* How the path is validated depends on what type of
3805 * URI it is.
3807 valid = data->is_opaque ?
3808 parse_path_opaque(pptr, data, flags) : parse_path_hierarchical(pptr, data, flags);
3810 if(!valid || (check_len && expected_len != data->path_len)) {
3811 TRACE("(%p %p %x): Invalid path component %s.\n", builder, data, flags,
3812 debugstr_wn(component, expected_len));
3813 return INET_E_INVALID_URL;
3816 TRACE("(%p %p %x): Valid path component %s len=%d.\n", builder, data, flags,
3817 debugstr_wn(data->path, data->path_len), data->path_len);
3819 return S_OK;
3822 static HRESULT validate_query(const UriBuilder *builder, parse_data *data, DWORD flags) {
3823 const WCHAR *ptr = NULL;
3824 const WCHAR **pptr;
3825 DWORD expected_len;
3827 if(builder->query) {
3828 ptr = builder->query;
3829 expected_len = builder->query_len;
3830 } else if(!(builder->modified_props & Uri_HAS_QUERY) && builder->uri &&
3831 builder->uri->query_start > -1) {
3832 ptr = builder->uri->canon_uri+builder->uri->query_start;
3833 expected_len = builder->uri->query_len;
3836 if(ptr) {
3837 const WCHAR *component = ptr;
3838 pptr = &ptr;
3840 if(parse_query(pptr, data, flags) && expected_len == data->query_len)
3841 TRACE("(%p %p %x): Valid query component %s len=%d.\n", builder, data, flags,
3842 debugstr_wn(data->query, data->query_len), data->query_len);
3843 else {
3844 TRACE("(%p %p %x): Invalid query component %s.\n", builder, data, flags,
3845 debugstr_wn(component, expected_len));
3846 return INET_E_INVALID_URL;
3850 return S_OK;
3853 static HRESULT validate_fragment(const UriBuilder *builder, parse_data *data, DWORD flags) {
3854 const WCHAR *ptr = NULL;
3855 const WCHAR **pptr;
3856 DWORD expected_len;
3858 if(builder->fragment) {
3859 ptr = builder->fragment;
3860 expected_len = builder->fragment_len;
3861 } else if(!(builder->modified_props & Uri_HAS_FRAGMENT) && builder->uri &&
3862 builder->uri->fragment_start > -1) {
3863 ptr = builder->uri->canon_uri+builder->uri->fragment_start;
3864 expected_len = builder->uri->fragment_len;
3867 if(ptr) {
3868 const WCHAR *component = ptr;
3869 pptr = &ptr;
3871 if(parse_fragment(pptr, data, flags) && expected_len == data->fragment_len)
3872 TRACE("(%p %p %x): Valid fragment component %s len=%d.\n", builder, data, flags,
3873 debugstr_wn(data->fragment, data->fragment_len), data->fragment_len);
3874 else {
3875 TRACE("(%p %p %x): Invalid fragment component %s.\n", builder, data, flags,
3876 debugstr_wn(component, expected_len));
3877 return INET_E_INVALID_URL;
3881 return S_OK;
3884 static HRESULT validate_components(const UriBuilder *builder, parse_data *data, DWORD flags) {
3885 HRESULT hr;
3887 memset(data, 0, sizeof(parse_data));
3889 TRACE("(%p %p %x): Beginning to validate builder components.\n", builder, data, flags);
3891 hr = validate_scheme_name(builder, data, flags);
3892 if(FAILED(hr))
3893 return hr;
3895 /* Extra validation for file schemes. */
3896 if(data->scheme_type == URL_SCHEME_FILE) {
3897 if((builder->password || (builder->uri && builder->uri->userinfo_split > -1)) ||
3898 (builder->username || (builder->uri && builder->uri->userinfo_start > -1))) {
3899 TRACE("(%p %p %x): File schemes can't contain a username or password.\n",
3900 builder, data, flags);
3901 return INET_E_INVALID_URL;
3905 hr = validate_userinfo(builder, data, flags);
3906 if(FAILED(hr))
3907 return hr;
3909 hr = validate_host(builder, data, flags);
3910 if(FAILED(hr))
3911 return hr;
3913 setup_port(builder, data, flags);
3915 /* The URI is opaque if it doesn't have an authority component. */
3916 if(!data->is_relative)
3917 data->is_opaque = !data->username && !data->password && !data->host && !data->has_port;
3918 else
3919 data->is_opaque = !data->host && !data->has_port;
3921 hr = validate_path(builder, data, flags);
3922 if(FAILED(hr))
3923 return hr;
3925 hr = validate_query(builder, data, flags);
3926 if(FAILED(hr))
3927 return hr;
3929 hr = validate_fragment(builder, data, flags);
3930 if(FAILED(hr))
3931 return hr;
3933 TRACE("(%p %p %x): Finished validating builder components.\n", builder, data, flags);
3935 return S_OK;
3938 /* Generates a raw uri string using the parse_data. */
3939 static DWORD generate_raw_uri(const parse_data *data, BSTR uri, DWORD flags) {
3940 DWORD length = 0;
3942 if(data->scheme) {
3943 if(uri) {
3944 memcpy(uri, data->scheme, data->scheme_len*sizeof(WCHAR));
3945 uri[data->scheme_len] = ':';
3947 length += data->scheme_len+1;
3950 if(!data->is_opaque) {
3951 /* For the "//" which appears before the authority component. */
3952 if(uri) {
3953 uri[length] = '/';
3954 uri[length+1] = '/';
3956 length += 2;
3959 if(data->username) {
3960 if(uri)
3961 memcpy(uri+length, data->username, data->username_len*sizeof(WCHAR));
3962 length += data->username_len;
3965 if(data->password) {
3966 if(uri) {
3967 uri[length] = ':';
3968 memcpy(uri+length+1, data->password, data->password_len*sizeof(WCHAR));
3970 length += data->password_len+1;
3973 if(data->password || data->username) {
3974 if(uri)
3975 uri[length] = '@';
3976 ++length;
3979 if(data->host) {
3980 /* IPv6 addresses get the brackets added around them if they don't already
3981 * have them.
3983 const BOOL add_brackets = data->host_type == Uri_HOST_IPV6 && *(data->host) != '[';
3984 if(add_brackets) {
3985 if(uri)
3986 uri[length] = '[';
3987 ++length;
3990 if(uri)
3991 memcpy(uri+length, data->host, data->host_len*sizeof(WCHAR));
3992 length += data->host_len;
3994 if(add_brackets) {
3995 if(uri)
3996 uri[length] = ']';
3997 length++;
4001 if(data->has_port) {
4002 /* The port isn't included in the raw uri if it's the default
4003 * port for the scheme type.
4005 DWORD i;
4006 BOOL is_default = FALSE;
4008 for(i = 0; i < sizeof(default_ports)/sizeof(default_ports[0]); ++i) {
4009 if(data->scheme_type == default_ports[i].scheme &&
4010 data->port_value == default_ports[i].port)
4011 is_default = TRUE;
4014 if(!is_default || flags & RAW_URI_FORCE_PORT_DISP) {
4015 if(uri)
4016 uri[length] = ':';
4017 ++length;
4019 if(uri)
4020 length += ui2str(uri+length, data->port_value);
4021 else
4022 length += ui2str(NULL, data->port_value);
4026 /* Check if a '/' should be added before the path for hierarchical URIs. */
4027 if(!data->is_opaque && data->path && *(data->path) != '/') {
4028 if(uri)
4029 uri[length] = '/';
4030 ++length;
4033 if(data->path) {
4034 if(uri)
4035 memcpy(uri+length, data->path, data->path_len*sizeof(WCHAR));
4036 length += data->path_len;
4039 if(data->query) {
4040 if(uri)
4041 memcpy(uri+length, data->query, data->query_len*sizeof(WCHAR));
4042 length += data->query_len;
4045 if(data->fragment) {
4046 if(uri)
4047 memcpy(uri+length, data->fragment, data->fragment_len*sizeof(WCHAR));
4048 length += data->fragment_len;
4051 if(uri)
4052 TRACE("(%p %p): Generated raw uri=%s len=%d\n", data, uri, debugstr_wn(uri, length), length);
4053 else
4054 TRACE("(%p %p): Computed raw uri len=%d\n", data, uri, length);
4056 return length;
4059 static HRESULT generate_uri(const UriBuilder *builder, const parse_data *data, Uri *uri, DWORD flags) {
4060 HRESULT hr;
4061 DWORD length = generate_raw_uri(data, NULL, 0);
4062 uri->raw_uri = SysAllocStringLen(NULL, length);
4063 if(!uri->raw_uri)
4064 return E_OUTOFMEMORY;
4066 generate_raw_uri(data, uri->raw_uri, 0);
4068 hr = canonicalize_uri(data, uri, flags);
4069 if(FAILED(hr)) {
4070 if(hr == E_INVALIDARG)
4071 return INET_E_INVALID_URL;
4072 return hr;
4075 uri->create_flags = flags;
4076 return S_OK;
4079 #define URI_THIS(iface) DEFINE_THIS(Uri, IUri, iface)
4081 static HRESULT WINAPI Uri_QueryInterface(IUri *iface, REFIID riid, void **ppv)
4083 Uri *This = URI_THIS(iface);
4085 if(IsEqualGUID(&IID_IUnknown, riid)) {
4086 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
4087 *ppv = URI(This);
4088 }else if(IsEqualGUID(&IID_IUri, riid)) {
4089 TRACE("(%p)->(IID_IUri %p)\n", This, ppv);
4090 *ppv = URI(This);
4091 }else if(IsEqualGUID(&IID_IUriObj, riid)) {
4092 TRACE("(%p)->(IID_IUriObj %p)\n", This, ppv);
4093 *ppv = This;
4094 return S_OK;
4095 }else {
4096 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
4097 *ppv = NULL;
4098 return E_NOINTERFACE;
4101 IUnknown_AddRef((IUnknown*)*ppv);
4102 return S_OK;
4105 static ULONG WINAPI Uri_AddRef(IUri *iface)
4107 Uri *This = URI_THIS(iface);
4108 LONG ref = InterlockedIncrement(&This->ref);
4110 TRACE("(%p) ref=%d\n", This, ref);
4112 return ref;
4115 static ULONG WINAPI Uri_Release(IUri *iface)
4117 Uri *This = URI_THIS(iface);
4118 LONG ref = InterlockedDecrement(&This->ref);
4120 TRACE("(%p) ref=%d\n", This, ref);
4122 if(!ref) {
4123 SysFreeString(This->raw_uri);
4124 heap_free(This->canon_uri);
4125 heap_free(This);
4128 return ref;
4131 static HRESULT WINAPI Uri_GetPropertyBSTR(IUri *iface, Uri_PROPERTY uriProp, BSTR *pbstrProperty, DWORD dwFlags)
4133 Uri *This = URI_THIS(iface);
4134 HRESULT hres;
4135 TRACE("(%p)->(%d %p %x)\n", This, uriProp, pbstrProperty, dwFlags);
4137 if(!pbstrProperty)
4138 return E_POINTER;
4140 if(uriProp > Uri_PROPERTY_STRING_LAST) {
4141 /* Windows allocates an empty BSTR for invalid Uri_PROPERTY's. */
4142 *pbstrProperty = SysAllocStringLen(NULL, 0);
4143 if(!(*pbstrProperty))
4144 return E_OUTOFMEMORY;
4146 /* It only returns S_FALSE for the ZONE property... */
4147 if(uriProp == Uri_PROPERTY_ZONE)
4148 return S_FALSE;
4149 else
4150 return S_OK;
4153 /* Don't have support for flags yet. */
4154 if(dwFlags) {
4155 FIXME("(%p)->(%d %p %x)\n", This, uriProp, pbstrProperty, dwFlags);
4156 return E_NOTIMPL;
4159 switch(uriProp) {
4160 case Uri_PROPERTY_ABSOLUTE_URI:
4161 if(This->display_modifiers & URI_DISPLAY_NO_ABSOLUTE_URI) {
4162 *pbstrProperty = SysAllocStringLen(NULL, 0);
4163 hres = S_FALSE;
4164 } else {
4165 if(This->scheme_type != URL_SCHEME_UNKNOWN && This->userinfo_start > -1) {
4166 if(This->userinfo_len == 0) {
4167 /* Don't include the '@' after the userinfo component. */
4168 *pbstrProperty = SysAllocStringLen(NULL, This->canon_len-1);
4169 hres = S_OK;
4170 if(*pbstrProperty) {
4171 /* Copy everything before it. */
4172 memcpy(*pbstrProperty, This->canon_uri, This->userinfo_start*sizeof(WCHAR));
4174 /* And everything after it. */
4175 memcpy(*pbstrProperty+This->userinfo_start, This->canon_uri+This->userinfo_start+1,
4176 (This->canon_len-This->userinfo_start-1)*sizeof(WCHAR));
4178 } else if(This->userinfo_split == 0 && This->userinfo_len == 1) {
4179 /* Don't include the ":@" */
4180 *pbstrProperty = SysAllocStringLen(NULL, This->canon_len-2);
4181 hres = S_OK;
4182 if(*pbstrProperty) {
4183 memcpy(*pbstrProperty, This->canon_uri, This->userinfo_start*sizeof(WCHAR));
4184 memcpy(*pbstrProperty+This->userinfo_start, This->canon_uri+This->userinfo_start+2,
4185 (This->canon_len-This->userinfo_start-2)*sizeof(WCHAR));
4187 } else {
4188 *pbstrProperty = SysAllocString(This->canon_uri);
4189 hres = S_OK;
4191 } else {
4192 *pbstrProperty = SysAllocString(This->canon_uri);
4193 hres = S_OK;
4197 if(!(*pbstrProperty))
4198 hres = E_OUTOFMEMORY;
4200 break;
4201 case Uri_PROPERTY_AUTHORITY:
4202 if(This->authority_start > -1) {
4203 if(This->port_offset > -1 && is_default_port(This->scheme_type, This->port) &&
4204 This->display_modifiers & URI_DISPLAY_NO_DEFAULT_PORT_AUTH)
4205 /* Don't include the port in the authority component. */
4206 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->authority_start, This->port_offset);
4207 else
4208 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->authority_start, This->authority_len);
4209 hres = S_OK;
4210 } else {
4211 *pbstrProperty = SysAllocStringLen(NULL, 0);
4212 hres = S_FALSE;
4215 if(!(*pbstrProperty))
4216 hres = E_OUTOFMEMORY;
4218 break;
4219 case Uri_PROPERTY_DISPLAY_URI:
4220 /* The Display URI contains everything except for the userinfo for known
4221 * scheme types.
4223 if(This->scheme_type != URL_SCHEME_UNKNOWN && This->userinfo_start > -1) {
4224 *pbstrProperty = SysAllocStringLen(NULL, This->canon_len-This->userinfo_len);
4226 if(*pbstrProperty) {
4227 /* Copy everything before the userinfo over. */
4228 memcpy(*pbstrProperty, This->canon_uri, This->userinfo_start*sizeof(WCHAR));
4229 /* Copy everything after the userinfo over. */
4230 memcpy(*pbstrProperty+This->userinfo_start,
4231 This->canon_uri+This->userinfo_start+This->userinfo_len+1,
4232 (This->canon_len-(This->userinfo_start+This->userinfo_len+1))*sizeof(WCHAR));
4234 } else
4235 *pbstrProperty = SysAllocString(This->canon_uri);
4237 if(!(*pbstrProperty))
4238 hres = E_OUTOFMEMORY;
4239 else
4240 hres = S_OK;
4242 break;
4243 case Uri_PROPERTY_DOMAIN:
4244 if(This->domain_offset > -1) {
4245 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->host_start+This->domain_offset,
4246 This->host_len-This->domain_offset);
4247 hres = S_OK;
4248 } else {
4249 *pbstrProperty = SysAllocStringLen(NULL, 0);
4250 hres = S_FALSE;
4253 if(!(*pbstrProperty))
4254 hres = E_OUTOFMEMORY;
4256 break;
4257 case Uri_PROPERTY_EXTENSION:
4258 if(This->extension_offset > -1) {
4259 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->path_start+This->extension_offset,
4260 This->path_len-This->extension_offset);
4261 hres = S_OK;
4262 } else {
4263 *pbstrProperty = SysAllocStringLen(NULL, 0);
4264 hres = S_FALSE;
4267 if(!(*pbstrProperty))
4268 hres = E_OUTOFMEMORY;
4270 break;
4271 case Uri_PROPERTY_FRAGMENT:
4272 if(This->fragment_start > -1) {
4273 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->fragment_start, This->fragment_len);
4274 hres = S_OK;
4275 } else {
4276 *pbstrProperty = SysAllocStringLen(NULL, 0);
4277 hres = S_FALSE;
4280 if(!(*pbstrProperty))
4281 hres = E_OUTOFMEMORY;
4283 break;
4284 case Uri_PROPERTY_HOST:
4285 if(This->host_start > -1) {
4286 /* The '[' and ']' aren't included for IPv6 addresses. */
4287 if(This->host_type == Uri_HOST_IPV6)
4288 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->host_start+1, This->host_len-2);
4289 else
4290 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->host_start, This->host_len);
4292 hres = S_OK;
4293 } else {
4294 *pbstrProperty = SysAllocStringLen(NULL, 0);
4295 hres = S_FALSE;
4298 if(!(*pbstrProperty))
4299 hres = E_OUTOFMEMORY;
4301 break;
4302 case Uri_PROPERTY_PASSWORD:
4303 if(This->userinfo_split > -1) {
4304 *pbstrProperty = SysAllocStringLen(
4305 This->canon_uri+This->userinfo_start+This->userinfo_split+1,
4306 This->userinfo_len-This->userinfo_split-1);
4307 hres = S_OK;
4308 } else {
4309 *pbstrProperty = SysAllocStringLen(NULL, 0);
4310 hres = S_FALSE;
4313 if(!(*pbstrProperty))
4314 return E_OUTOFMEMORY;
4316 break;
4317 case Uri_PROPERTY_PATH:
4318 if(This->path_start > -1) {
4319 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->path_start, This->path_len);
4320 hres = S_OK;
4321 } else {
4322 *pbstrProperty = SysAllocStringLen(NULL, 0);
4323 hres = S_FALSE;
4326 if(!(*pbstrProperty))
4327 hres = E_OUTOFMEMORY;
4329 break;
4330 case Uri_PROPERTY_PATH_AND_QUERY:
4331 if(This->path_start > -1) {
4332 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->path_start, This->path_len+This->query_len);
4333 hres = S_OK;
4334 } else if(This->query_start > -1) {
4335 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->query_start, This->query_len);
4336 hres = S_OK;
4337 } else {
4338 *pbstrProperty = SysAllocStringLen(NULL, 0);
4339 hres = S_FALSE;
4342 if(!(*pbstrProperty))
4343 hres = E_OUTOFMEMORY;
4345 break;
4346 case Uri_PROPERTY_QUERY:
4347 if(This->query_start > -1) {
4348 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->query_start, This->query_len);
4349 hres = S_OK;
4350 } else {
4351 *pbstrProperty = SysAllocStringLen(NULL, 0);
4352 hres = S_FALSE;
4355 if(!(*pbstrProperty))
4356 hres = E_OUTOFMEMORY;
4358 break;
4359 case Uri_PROPERTY_RAW_URI:
4360 *pbstrProperty = SysAllocString(This->raw_uri);
4361 if(!(*pbstrProperty))
4362 hres = E_OUTOFMEMORY;
4363 else
4364 hres = S_OK;
4365 break;
4366 case Uri_PROPERTY_SCHEME_NAME:
4367 if(This->scheme_start > -1) {
4368 *pbstrProperty = SysAllocStringLen(This->canon_uri + This->scheme_start, This->scheme_len);
4369 hres = S_OK;
4370 } else {
4371 *pbstrProperty = SysAllocStringLen(NULL, 0);
4372 hres = S_FALSE;
4375 if(!(*pbstrProperty))
4376 hres = E_OUTOFMEMORY;
4378 break;
4379 case Uri_PROPERTY_USER_INFO:
4380 if(This->userinfo_start > -1) {
4381 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->userinfo_start, This->userinfo_len);
4382 hres = S_OK;
4383 } else {
4384 *pbstrProperty = SysAllocStringLen(NULL, 0);
4385 hres = S_FALSE;
4388 if(!(*pbstrProperty))
4389 hres = E_OUTOFMEMORY;
4391 break;
4392 case Uri_PROPERTY_USER_NAME:
4393 if(This->userinfo_start > -1 && This->userinfo_split != 0) {
4394 /* If userinfo_split is set, that means a password exists
4395 * so the username is only from userinfo_start to userinfo_split.
4397 if(This->userinfo_split > -1) {
4398 *pbstrProperty = SysAllocStringLen(This->canon_uri + This->userinfo_start, This->userinfo_split);
4399 hres = S_OK;
4400 } else {
4401 *pbstrProperty = SysAllocStringLen(This->canon_uri + This->userinfo_start, This->userinfo_len);
4402 hres = S_OK;
4404 } else {
4405 *pbstrProperty = SysAllocStringLen(NULL, 0);
4406 hres = S_FALSE;
4409 if(!(*pbstrProperty))
4410 return E_OUTOFMEMORY;
4412 break;
4413 default:
4414 FIXME("(%p)->(%d %p %x)\n", This, uriProp, pbstrProperty, dwFlags);
4415 hres = E_NOTIMPL;
4418 return hres;
4421 static HRESULT WINAPI Uri_GetPropertyLength(IUri *iface, Uri_PROPERTY uriProp, DWORD *pcchProperty, DWORD dwFlags)
4423 Uri *This = URI_THIS(iface);
4424 HRESULT hres;
4425 TRACE("(%p)->(%d %p %x)\n", This, uriProp, pcchProperty, dwFlags);
4427 if(!pcchProperty)
4428 return E_INVALIDARG;
4430 /* Can only return a length for a property if it's a string. */
4431 if(uriProp > Uri_PROPERTY_STRING_LAST)
4432 return E_INVALIDARG;
4434 /* Don't have support for flags yet. */
4435 if(dwFlags) {
4436 FIXME("(%p)->(%d %p %x)\n", This, uriProp, pcchProperty, dwFlags);
4437 return E_NOTIMPL;
4440 switch(uriProp) {
4441 case Uri_PROPERTY_ABSOLUTE_URI:
4442 if(This->display_modifiers & URI_DISPLAY_NO_ABSOLUTE_URI) {
4443 *pcchProperty = 0;
4444 hres = S_FALSE;
4445 } else {
4446 if(This->scheme_type != URL_SCHEME_UNKNOWN) {
4447 if(This->userinfo_start > -1 && This->userinfo_len == 0)
4448 /* Don't include the '@' in the length. */
4449 *pcchProperty = This->canon_len-1;
4450 else if(This->userinfo_start > -1 && This->userinfo_len == 1 &&
4451 This->userinfo_split == 0)
4452 /* Don't include the ":@" in the length. */
4453 *pcchProperty = This->canon_len-2;
4454 else
4455 *pcchProperty = This->canon_len;
4456 } else
4457 *pcchProperty = This->canon_len;
4459 hres = S_OK;
4462 break;
4463 case Uri_PROPERTY_AUTHORITY:
4464 if(This->port_offset > -1 &&
4465 This->display_modifiers & URI_DISPLAY_NO_DEFAULT_PORT_AUTH &&
4466 is_default_port(This->scheme_type, This->port))
4467 /* Only count up until the port in the authority. */
4468 *pcchProperty = This->port_offset;
4469 else
4470 *pcchProperty = This->authority_len;
4471 hres = (This->authority_start > -1) ? S_OK : S_FALSE;
4472 break;
4473 case Uri_PROPERTY_DISPLAY_URI:
4474 if(This->scheme_type != URL_SCHEME_UNKNOWN && This->userinfo_start > -1)
4475 *pcchProperty = This->canon_len-This->userinfo_len-1;
4476 else
4477 *pcchProperty = This->canon_len;
4479 hres = S_OK;
4480 break;
4481 case Uri_PROPERTY_DOMAIN:
4482 if(This->domain_offset > -1)
4483 *pcchProperty = This->host_len - This->domain_offset;
4484 else
4485 *pcchProperty = 0;
4487 hres = (This->domain_offset > -1) ? S_OK : S_FALSE;
4488 break;
4489 case Uri_PROPERTY_EXTENSION:
4490 if(This->extension_offset > -1) {
4491 *pcchProperty = This->path_len - This->extension_offset;
4492 hres = S_OK;
4493 } else {
4494 *pcchProperty = 0;
4495 hres = S_FALSE;
4498 break;
4499 case Uri_PROPERTY_FRAGMENT:
4500 *pcchProperty = This->fragment_len;
4501 hres = (This->fragment_start > -1) ? S_OK : S_FALSE;
4502 break;
4503 case Uri_PROPERTY_HOST:
4504 *pcchProperty = This->host_len;
4506 /* '[' and ']' aren't included in the length. */
4507 if(This->host_type == Uri_HOST_IPV6)
4508 *pcchProperty -= 2;
4510 hres = (This->host_start > -1) ? S_OK : S_FALSE;
4511 break;
4512 case Uri_PROPERTY_PASSWORD:
4513 *pcchProperty = (This->userinfo_split > -1) ? This->userinfo_len-This->userinfo_split-1 : 0;
4514 hres = (This->userinfo_split > -1) ? S_OK : S_FALSE;
4515 break;
4516 case Uri_PROPERTY_PATH:
4517 *pcchProperty = This->path_len;
4518 hres = (This->path_start > -1) ? S_OK : S_FALSE;
4519 break;
4520 case Uri_PROPERTY_PATH_AND_QUERY:
4521 *pcchProperty = This->path_len+This->query_len;
4522 hres = (This->path_start > -1 || This->query_start > -1) ? S_OK : S_FALSE;
4523 break;
4524 case Uri_PROPERTY_QUERY:
4525 *pcchProperty = This->query_len;
4526 hres = (This->query_start > -1) ? S_OK : S_FALSE;
4527 break;
4528 case Uri_PROPERTY_RAW_URI:
4529 *pcchProperty = SysStringLen(This->raw_uri);
4530 hres = S_OK;
4531 break;
4532 case Uri_PROPERTY_SCHEME_NAME:
4533 *pcchProperty = This->scheme_len;
4534 hres = (This->scheme_start > -1) ? S_OK : S_FALSE;
4535 break;
4536 case Uri_PROPERTY_USER_INFO:
4537 *pcchProperty = This->userinfo_len;
4538 hres = (This->userinfo_start > -1) ? S_OK : S_FALSE;
4539 break;
4540 case Uri_PROPERTY_USER_NAME:
4541 *pcchProperty = (This->userinfo_split > -1) ? This->userinfo_split : This->userinfo_len;
4542 if(This->userinfo_split == 0)
4543 hres = S_FALSE;
4544 else
4545 hres = (This->userinfo_start > -1) ? S_OK : S_FALSE;
4546 break;
4547 default:
4548 FIXME("(%p)->(%d %p %x)\n", This, uriProp, pcchProperty, dwFlags);
4549 hres = E_NOTIMPL;
4552 return hres;
4555 static HRESULT WINAPI Uri_GetPropertyDWORD(IUri *iface, Uri_PROPERTY uriProp, DWORD *pcchProperty, DWORD dwFlags)
4557 Uri *This = URI_THIS(iface);
4558 HRESULT hres;
4560 TRACE("(%p)->(%d %p %x)\n", This, uriProp, pcchProperty, dwFlags);
4562 if(!pcchProperty)
4563 return E_INVALIDARG;
4565 /* Microsoft's implementation for the ZONE property of a URI seems to be lacking...
4566 * From what I can tell, instead of checking which URLZONE the URI belongs to it
4567 * simply assigns URLZONE_INVALID and returns E_NOTIMPL. This also applies to the GetZone
4568 * function.
4570 if(uriProp == Uri_PROPERTY_ZONE) {
4571 *pcchProperty = URLZONE_INVALID;
4572 return E_NOTIMPL;
4575 if(uriProp < Uri_PROPERTY_DWORD_START) {
4576 *pcchProperty = 0;
4577 return E_INVALIDARG;
4580 switch(uriProp) {
4581 case Uri_PROPERTY_HOST_TYPE:
4582 *pcchProperty = This->host_type;
4583 hres = S_OK;
4584 break;
4585 case Uri_PROPERTY_PORT:
4586 if(!This->has_port) {
4587 *pcchProperty = 0;
4588 hres = S_FALSE;
4589 } else {
4590 *pcchProperty = This->port;
4591 hres = S_OK;
4594 break;
4595 case Uri_PROPERTY_SCHEME:
4596 *pcchProperty = This->scheme_type;
4597 hres = S_OK;
4598 break;
4599 default:
4600 FIXME("(%p)->(%d %p %x)\n", This, uriProp, pcchProperty, dwFlags);
4601 hres = E_NOTIMPL;
4604 return hres;
4607 static HRESULT WINAPI Uri_HasProperty(IUri *iface, Uri_PROPERTY uriProp, BOOL *pfHasProperty)
4609 Uri *This = URI_THIS(iface);
4610 TRACE("(%p)->(%d %p)\n", This, uriProp, pfHasProperty);
4612 if(!pfHasProperty)
4613 return E_INVALIDARG;
4615 switch(uriProp) {
4616 case Uri_PROPERTY_ABSOLUTE_URI:
4617 *pfHasProperty = !(This->display_modifiers & URI_DISPLAY_NO_ABSOLUTE_URI);
4618 break;
4619 case Uri_PROPERTY_AUTHORITY:
4620 *pfHasProperty = This->authority_start > -1;
4621 break;
4622 case Uri_PROPERTY_DISPLAY_URI:
4623 *pfHasProperty = TRUE;
4624 break;
4625 case Uri_PROPERTY_DOMAIN:
4626 *pfHasProperty = This->domain_offset > -1;
4627 break;
4628 case Uri_PROPERTY_EXTENSION:
4629 *pfHasProperty = This->extension_offset > -1;
4630 break;
4631 case Uri_PROPERTY_FRAGMENT:
4632 *pfHasProperty = This->fragment_start > -1;
4633 break;
4634 case Uri_PROPERTY_HOST:
4635 *pfHasProperty = This->host_start > -1;
4636 break;
4637 case Uri_PROPERTY_PASSWORD:
4638 *pfHasProperty = This->userinfo_split > -1;
4639 break;
4640 case Uri_PROPERTY_PATH:
4641 *pfHasProperty = This->path_start > -1;
4642 break;
4643 case Uri_PROPERTY_PATH_AND_QUERY:
4644 *pfHasProperty = (This->path_start > -1 || This->query_start > -1);
4645 break;
4646 case Uri_PROPERTY_QUERY:
4647 *pfHasProperty = This->query_start > -1;
4648 break;
4649 case Uri_PROPERTY_RAW_URI:
4650 *pfHasProperty = TRUE;
4651 break;
4652 case Uri_PROPERTY_SCHEME_NAME:
4653 *pfHasProperty = This->scheme_start > -1;
4654 break;
4655 case Uri_PROPERTY_USER_INFO:
4656 *pfHasProperty = This->userinfo_start > -1;
4657 break;
4658 case Uri_PROPERTY_USER_NAME:
4659 if(This->userinfo_split == 0)
4660 *pfHasProperty = FALSE;
4661 else
4662 *pfHasProperty = This->userinfo_start > -1;
4663 break;
4664 case Uri_PROPERTY_HOST_TYPE:
4665 *pfHasProperty = TRUE;
4666 break;
4667 case Uri_PROPERTY_PORT:
4668 *pfHasProperty = This->has_port;
4669 break;
4670 case Uri_PROPERTY_SCHEME:
4671 *pfHasProperty = TRUE;
4672 break;
4673 case Uri_PROPERTY_ZONE:
4674 *pfHasProperty = FALSE;
4675 break;
4676 default:
4677 FIXME("(%p)->(%d %p): Unsupported property type.\n", This, uriProp, pfHasProperty);
4678 return E_NOTIMPL;
4681 return S_OK;
4684 static HRESULT WINAPI Uri_GetAbsoluteUri(IUri *iface, BSTR *pstrAbsoluteUri)
4686 TRACE("(%p)->(%p)\n", iface, pstrAbsoluteUri);
4687 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_ABSOLUTE_URI, pstrAbsoluteUri, 0);
4690 static HRESULT WINAPI Uri_GetAuthority(IUri *iface, BSTR *pstrAuthority)
4692 TRACE("(%p)->(%p)\n", iface, pstrAuthority);
4693 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_AUTHORITY, pstrAuthority, 0);
4696 static HRESULT WINAPI Uri_GetDisplayUri(IUri *iface, BSTR *pstrDisplayUri)
4698 TRACE("(%p)->(%p)\n", iface, pstrDisplayUri);
4699 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_DISPLAY_URI, pstrDisplayUri, 0);
4702 static HRESULT WINAPI Uri_GetDomain(IUri *iface, BSTR *pstrDomain)
4704 TRACE("(%p)->(%p)\n", iface, pstrDomain);
4705 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_DOMAIN, pstrDomain, 0);
4708 static HRESULT WINAPI Uri_GetExtension(IUri *iface, BSTR *pstrExtension)
4710 TRACE("(%p)->(%p)\n", iface, pstrExtension);
4711 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_EXTENSION, pstrExtension, 0);
4714 static HRESULT WINAPI Uri_GetFragment(IUri *iface, BSTR *pstrFragment)
4716 TRACE("(%p)->(%p)\n", iface, pstrFragment);
4717 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_FRAGMENT, pstrFragment, 0);
4720 static HRESULT WINAPI Uri_GetHost(IUri *iface, BSTR *pstrHost)
4722 TRACE("(%p)->(%p)\n", iface, pstrHost);
4723 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_HOST, pstrHost, 0);
4726 static HRESULT WINAPI Uri_GetPassword(IUri *iface, BSTR *pstrPassword)
4728 TRACE("(%p)->(%p)\n", iface, pstrPassword);
4729 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_PASSWORD, pstrPassword, 0);
4732 static HRESULT WINAPI Uri_GetPath(IUri *iface, BSTR *pstrPath)
4734 TRACE("(%p)->(%p)\n", iface, pstrPath);
4735 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_PATH, pstrPath, 0);
4738 static HRESULT WINAPI Uri_GetPathAndQuery(IUri *iface, BSTR *pstrPathAndQuery)
4740 TRACE("(%p)->(%p)\n", iface, pstrPathAndQuery);
4741 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_PATH_AND_QUERY, pstrPathAndQuery, 0);
4744 static HRESULT WINAPI Uri_GetQuery(IUri *iface, BSTR *pstrQuery)
4746 TRACE("(%p)->(%p)\n", iface, pstrQuery);
4747 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_QUERY, pstrQuery, 0);
4750 static HRESULT WINAPI Uri_GetRawUri(IUri *iface, BSTR *pstrRawUri)
4752 TRACE("(%p)->(%p)\n", iface, pstrRawUri);
4753 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_RAW_URI, pstrRawUri, 0);
4756 static HRESULT WINAPI Uri_GetSchemeName(IUri *iface, BSTR *pstrSchemeName)
4758 TRACE("(%p)->(%p)\n", iface, pstrSchemeName);
4759 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_SCHEME_NAME, pstrSchemeName, 0);
4762 static HRESULT WINAPI Uri_GetUserInfo(IUri *iface, BSTR *pstrUserInfo)
4764 TRACE("(%p)->(%p)\n", iface, pstrUserInfo);
4765 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_USER_INFO, pstrUserInfo, 0);
4768 static HRESULT WINAPI Uri_GetUserName(IUri *iface, BSTR *pstrUserName)
4770 TRACE("(%p)->(%p)\n", iface, pstrUserName);
4771 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_USER_NAME, pstrUserName, 0);
4774 static HRESULT WINAPI Uri_GetHostType(IUri *iface, DWORD *pdwHostType)
4776 TRACE("(%p)->(%p)\n", iface, pdwHostType);
4777 return Uri_GetPropertyDWORD(iface, Uri_PROPERTY_HOST_TYPE, pdwHostType, 0);
4780 static HRESULT WINAPI Uri_GetPort(IUri *iface, DWORD *pdwPort)
4782 TRACE("(%p)->(%p)\n", iface, pdwPort);
4783 return Uri_GetPropertyDWORD(iface, Uri_PROPERTY_PORT, pdwPort, 0);
4786 static HRESULT WINAPI Uri_GetScheme(IUri *iface, DWORD *pdwScheme)
4788 Uri *This = URI_THIS(iface);
4789 TRACE("(%p)->(%p)\n", This, pdwScheme);
4790 return Uri_GetPropertyDWORD(iface, Uri_PROPERTY_SCHEME, pdwScheme, 0);
4793 static HRESULT WINAPI Uri_GetZone(IUri *iface, DWORD *pdwZone)
4795 TRACE("(%p)->(%p)\n", iface, pdwZone);
4796 return Uri_GetPropertyDWORD(iface, Uri_PROPERTY_ZONE,pdwZone, 0);
4799 static HRESULT WINAPI Uri_GetProperties(IUri *iface, DWORD *pdwProperties)
4801 Uri *This = URI_THIS(iface);
4802 TRACE("(%p)->(%p)\n", This, pdwProperties);
4804 if(!pdwProperties)
4805 return E_INVALIDARG;
4807 /* All URIs have these. */
4808 *pdwProperties = Uri_HAS_DISPLAY_URI|Uri_HAS_RAW_URI|Uri_HAS_SCHEME|Uri_HAS_HOST_TYPE;
4810 if(!(This->display_modifiers & URI_DISPLAY_NO_ABSOLUTE_URI))
4811 *pdwProperties |= Uri_HAS_ABSOLUTE_URI;
4813 if(This->scheme_start > -1)
4814 *pdwProperties |= Uri_HAS_SCHEME_NAME;
4816 if(This->authority_start > -1) {
4817 *pdwProperties |= Uri_HAS_AUTHORITY;
4818 if(This->userinfo_start > -1) {
4819 *pdwProperties |= Uri_HAS_USER_INFO;
4820 if(This->userinfo_split != 0)
4821 *pdwProperties |= Uri_HAS_USER_NAME;
4823 if(This->userinfo_split > -1)
4824 *pdwProperties |= Uri_HAS_PASSWORD;
4825 if(This->host_start > -1)
4826 *pdwProperties |= Uri_HAS_HOST;
4827 if(This->domain_offset > -1)
4828 *pdwProperties |= Uri_HAS_DOMAIN;
4831 if(This->has_port)
4832 *pdwProperties |= Uri_HAS_PORT;
4833 if(This->path_start > -1)
4834 *pdwProperties |= Uri_HAS_PATH|Uri_HAS_PATH_AND_QUERY;
4835 if(This->query_start > -1)
4836 *pdwProperties |= Uri_HAS_QUERY|Uri_HAS_PATH_AND_QUERY;
4838 if(This->extension_offset > -1)
4839 *pdwProperties |= Uri_HAS_EXTENSION;
4841 if(This->fragment_start > -1)
4842 *pdwProperties |= Uri_HAS_FRAGMENT;
4844 return S_OK;
4847 static HRESULT WINAPI Uri_IsEqual(IUri *iface, IUri *pUri, BOOL *pfEqual)
4849 Uri *This = URI_THIS(iface);
4850 Uri *other;
4852 TRACE("(%p)->(%p %p)\n", This, pUri, pfEqual);
4854 if(!pfEqual)
4855 return E_POINTER;
4857 if(!pUri) {
4858 *pfEqual = FALSE;
4860 /* For some reason Windows returns S_OK here... */
4861 return S_OK;
4864 /* Try to convert it to a Uri (allows for a more simple comparison). */
4865 if((other = get_uri_obj(pUri)))
4866 *pfEqual = are_equal_simple(This, other);
4867 else {
4868 /* Do it the hard way. */
4869 FIXME("(%p)->(%p %p) No support for unknown IUri's yet.\n", iface, pUri, pfEqual);
4870 return E_NOTIMPL;
4873 return S_OK;
4876 #undef URI_THIS
4878 static const IUriVtbl UriVtbl = {
4879 Uri_QueryInterface,
4880 Uri_AddRef,
4881 Uri_Release,
4882 Uri_GetPropertyBSTR,
4883 Uri_GetPropertyLength,
4884 Uri_GetPropertyDWORD,
4885 Uri_HasProperty,
4886 Uri_GetAbsoluteUri,
4887 Uri_GetAuthority,
4888 Uri_GetDisplayUri,
4889 Uri_GetDomain,
4890 Uri_GetExtension,
4891 Uri_GetFragment,
4892 Uri_GetHost,
4893 Uri_GetPassword,
4894 Uri_GetPath,
4895 Uri_GetPathAndQuery,
4896 Uri_GetQuery,
4897 Uri_GetRawUri,
4898 Uri_GetSchemeName,
4899 Uri_GetUserInfo,
4900 Uri_GetUserName,
4901 Uri_GetHostType,
4902 Uri_GetPort,
4903 Uri_GetScheme,
4904 Uri_GetZone,
4905 Uri_GetProperties,
4906 Uri_IsEqual
4909 static Uri* create_uri_obj(void) {
4910 Uri *ret = heap_alloc_zero(sizeof(Uri));
4911 if(ret) {
4912 ret->lpIUriVtbl = &UriVtbl;
4913 ret->ref = 1;
4916 return ret;
4919 /***********************************************************************
4920 * CreateUri (urlmon.@)
4922 * Creates a new IUri object using the URI represented by pwzURI. This function
4923 * parses and validates the components of pwzURI and then canonicalizes the
4924 * parsed components.
4926 * PARAMS
4927 * pwzURI [I] The URI to parse, validate, and canonicalize.
4928 * dwFlags [I] Flags which can affect how the parsing/canonicalization is performed.
4929 * dwReserved [I] Reserved (not used).
4930 * ppURI [O] The resulting IUri after parsing/canonicalization occurs.
4932 * RETURNS
4933 * Success: Returns S_OK. ppURI contains the pointer to the newly allocated IUri.
4934 * Failure: E_INVALIDARG if there's invalid flag combinations in dwFlags, or an
4935 * invalid parameters, or pwzURI doesn't represnt a valid URI.
4936 * E_OUTOFMEMORY if any memory allocation fails.
4938 * NOTES
4939 * Default flags:
4940 * Uri_CREATE_CANONICALIZE, Uri_CREATE_DECODE_EXTRA_INFO, Uri_CREATE_CRACK_UNKNOWN_SCHEMES,
4941 * Uri_CREATE_PRE_PROCESS_HTML_URI, Uri_CREATE_NO_IE_SETTINGS.
4943 HRESULT WINAPI CreateUri(LPCWSTR pwzURI, DWORD dwFlags, DWORD_PTR dwReserved, IUri **ppURI)
4945 const DWORD supported_flags = Uri_CREATE_ALLOW_RELATIVE|Uri_CREATE_ALLOW_IMPLICIT_WILDCARD_SCHEME|
4946 Uri_CREATE_ALLOW_IMPLICIT_FILE_SCHEME|Uri_CREATE_NO_CANONICALIZE|Uri_CREATE_CANONICALIZE|
4947 Uri_CREATE_DECODE_EXTRA_INFO|Uri_CREATE_NO_DECODE_EXTRA_INFO|Uri_CREATE_CRACK_UNKNOWN_SCHEMES|
4948 Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES|Uri_CREATE_PRE_PROCESS_HTML_URI|Uri_CREATE_NO_PRE_PROCESS_HTML_URI|
4949 Uri_CREATE_NO_IE_SETTINGS|Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS|Uri_CREATE_FILE_USE_DOS_PATH;
4950 Uri *ret;
4951 HRESULT hr;
4952 parse_data data;
4954 TRACE("(%s %x %x %p)\n", debugstr_w(pwzURI), dwFlags, (DWORD)dwReserved, ppURI);
4956 if(!ppURI)
4957 return E_INVALIDARG;
4959 if(!pwzURI || !*pwzURI) {
4960 *ppURI = NULL;
4961 return E_INVALIDARG;
4964 /* Check for invalid flags. */
4965 if(has_invalid_flag_combination(dwFlags)) {
4966 *ppURI = NULL;
4967 return E_INVALIDARG;
4970 /* Currently unsupported. */
4971 if(dwFlags & ~supported_flags)
4972 FIXME("Ignoring unsupported flag(s) %x\n", dwFlags & ~supported_flags);
4974 ret = create_uri_obj();
4975 if(!ret) {
4976 *ppURI = NULL;
4977 return E_OUTOFMEMORY;
4980 /* Explicitly set the default flags if it doesn't cause a flag conflict. */
4981 apply_default_flags(&dwFlags);
4983 /* Pre process the URI, unless told otherwise. */
4984 if(!(dwFlags & Uri_CREATE_NO_PRE_PROCESS_HTML_URI))
4985 ret->raw_uri = pre_process_uri(pwzURI);
4986 else
4987 ret->raw_uri = SysAllocString(pwzURI);
4989 if(!ret->raw_uri) {
4990 heap_free(ret);
4991 return E_OUTOFMEMORY;
4994 memset(&data, 0, sizeof(parse_data));
4995 data.uri = ret->raw_uri;
4997 /* Validate and parse the URI into it's components. */
4998 if(!parse_uri(&data, dwFlags)) {
4999 /* Encountered an unsupported or invalid URI */
5000 IUri_Release(URI(ret));
5001 *ppURI = NULL;
5002 return E_INVALIDARG;
5005 /* Canonicalize the URI. */
5006 hr = canonicalize_uri(&data, ret, dwFlags);
5007 if(FAILED(hr)) {
5008 IUri_Release(URI(ret));
5009 *ppURI = NULL;
5010 return hr;
5013 ret->create_flags = dwFlags;
5015 *ppURI = URI(ret);
5016 return S_OK;
5019 /***********************************************************************
5020 * CreateUriWithFragment (urlmon.@)
5022 * Creates a new IUri object. This is almost the same as CreateUri, expect that
5023 * it allows you to explicitly specify a fragment (pwzFragment) for pwzURI.
5025 * PARAMS
5026 * pwzURI [I] The URI to parse and perform canonicalization on.
5027 * pwzFragment [I] The explict fragment string which should be added to pwzURI.
5028 * dwFlags [I] The flags which will be passed to CreateUri.
5029 * dwReserved [I] Reserved (not used).
5030 * ppURI [O] The resulting IUri after parsing/canonicalization.
5032 * RETURNS
5033 * Success: S_OK. ppURI contains the pointer to the newly allocated IUri.
5034 * Failure: E_INVALIDARG if pwzURI already contains a fragment and pwzFragment
5035 * isn't NULL. Will also return E_INVALIDARG for the same reasons as
5036 * CreateUri will. E_OUTOFMEMORY if any allocations fail.
5038 HRESULT WINAPI CreateUriWithFragment(LPCWSTR pwzURI, LPCWSTR pwzFragment, DWORD dwFlags,
5039 DWORD_PTR dwReserved, IUri **ppURI)
5041 HRESULT hres;
5042 TRACE("(%s %s %x %x %p)\n", debugstr_w(pwzURI), debugstr_w(pwzFragment), dwFlags, (DWORD)dwReserved, ppURI);
5044 if(!ppURI)
5045 return E_INVALIDARG;
5047 if(!pwzURI) {
5048 *ppURI = NULL;
5049 return E_INVALIDARG;
5052 /* Check if a fragment should be appended to the URI string. */
5053 if(pwzFragment) {
5054 WCHAR *uriW;
5055 DWORD uri_len, frag_len;
5056 BOOL add_pound;
5058 /* Check if the original URI already has a fragment component. */
5059 if(StrChrW(pwzURI, '#')) {
5060 *ppURI = NULL;
5061 return E_INVALIDARG;
5064 uri_len = lstrlenW(pwzURI);
5065 frag_len = lstrlenW(pwzFragment);
5067 /* If the fragment doesn't start with a '#', one will be added. */
5068 add_pound = *pwzFragment != '#';
5070 if(add_pound)
5071 uriW = heap_alloc((uri_len+frag_len+2)*sizeof(WCHAR));
5072 else
5073 uriW = heap_alloc((uri_len+frag_len+1)*sizeof(WCHAR));
5075 if(!uriW)
5076 return E_OUTOFMEMORY;
5078 memcpy(uriW, pwzURI, uri_len*sizeof(WCHAR));
5079 if(add_pound)
5080 uriW[uri_len++] = '#';
5081 memcpy(uriW+uri_len, pwzFragment, (frag_len+1)*sizeof(WCHAR));
5083 hres = CreateUri(uriW, dwFlags, 0, ppURI);
5085 heap_free(uriW);
5086 } else
5087 /* A fragment string wasn't specified, so just forward the call. */
5088 hres = CreateUri(pwzURI, dwFlags, 0, ppURI);
5090 return hres;
5093 static HRESULT build_uri(const UriBuilder *builder, IUri **uri, DWORD create_flags,
5094 DWORD use_orig_flags, DWORD encoding_mask)
5096 HRESULT hr;
5097 parse_data data;
5098 Uri *ret;
5100 if(!uri)
5101 return E_POINTER;
5103 if(encoding_mask && (!builder->uri || builder->modified_props)) {
5104 *uri = NULL;
5105 return E_NOTIMPL;
5108 /* Decide what flags should be used when creating the Uri. */
5109 if((use_orig_flags & UriBuilder_USE_ORIGINAL_FLAGS) && builder->uri)
5110 create_flags = builder->uri->create_flags;
5111 else {
5112 if(has_invalid_flag_combination(create_flags)) {
5113 *uri = NULL;
5114 return E_INVALIDARG;
5117 /* Set the default flags if they don't cause a conflict. */
5118 apply_default_flags(&create_flags);
5121 /* Return the base IUri if no changes have been made and the create_flags match. */
5122 if(builder->uri && !builder->modified_props && builder->uri->create_flags == create_flags) {
5123 *uri = URI(builder->uri);
5124 IUri_AddRef(*uri);
5125 return S_OK;
5128 hr = validate_components(builder, &data, create_flags);
5129 if(FAILED(hr)) {
5130 *uri = NULL;
5131 return hr;
5134 ret = create_uri_obj();
5135 if(!ret) {
5136 *uri = NULL;
5137 return E_OUTOFMEMORY;
5140 hr = generate_uri(builder, &data, ret, create_flags);
5141 if(FAILED(hr)) {
5142 IUri_Release(URI(ret));
5143 *uri = NULL;
5144 return hr;
5147 *uri = URI(ret);
5148 return S_OK;
5151 #define URIBUILDER_THIS(iface) DEFINE_THIS(UriBuilder, IUriBuilder, iface)
5153 static HRESULT WINAPI UriBuilder_QueryInterface(IUriBuilder *iface, REFIID riid, void **ppv)
5155 UriBuilder *This = URIBUILDER_THIS(iface);
5157 if(IsEqualGUID(&IID_IUnknown, riid)) {
5158 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
5159 *ppv = URIBUILDER(This);
5160 }else if(IsEqualGUID(&IID_IUriBuilder, riid)) {
5161 TRACE("(%p)->(IID_IUri %p)\n", This, ppv);
5162 *ppv = URIBUILDER(This);
5163 }else {
5164 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
5165 *ppv = NULL;
5166 return E_NOINTERFACE;
5169 IUnknown_AddRef((IUnknown*)*ppv);
5170 return S_OK;
5173 static ULONG WINAPI UriBuilder_AddRef(IUriBuilder *iface)
5175 UriBuilder *This = URIBUILDER_THIS(iface);
5176 LONG ref = InterlockedIncrement(&This->ref);
5178 TRACE("(%p) ref=%d\n", This, ref);
5180 return ref;
5183 static ULONG WINAPI UriBuilder_Release(IUriBuilder *iface)
5185 UriBuilder *This = URIBUILDER_THIS(iface);
5186 LONG ref = InterlockedDecrement(&This->ref);
5188 TRACE("(%p) ref=%d\n", This, ref);
5190 if(!ref) {
5191 if(This->uri) IUri_Release(URI(This->uri));
5192 heap_free(This->fragment);
5193 heap_free(This->host);
5194 heap_free(This->password);
5195 heap_free(This->path);
5196 heap_free(This->query);
5197 heap_free(This->scheme);
5198 heap_free(This->username);
5199 heap_free(This);
5202 return ref;
5205 static HRESULT WINAPI UriBuilder_CreateUriSimple(IUriBuilder *iface,
5206 DWORD dwAllowEncodingPropertyMask,
5207 DWORD_PTR dwReserved,
5208 IUri **ppIUri)
5210 UriBuilder *This = URIBUILDER_THIS(iface);
5211 HRESULT hr;
5212 TRACE("(%p)->(%d %d %p)\n", This, dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
5214 hr = build_uri(This, ppIUri, 0, UriBuilder_USE_ORIGINAL_FLAGS, dwAllowEncodingPropertyMask);
5215 if(hr == E_NOTIMPL)
5216 FIXME("(%p)->(%d %d %p)\n", This, dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
5217 return hr;
5220 static HRESULT WINAPI UriBuilder_CreateUri(IUriBuilder *iface,
5221 DWORD dwCreateFlags,
5222 DWORD dwAllowEncodingPropertyMask,
5223 DWORD_PTR dwReserved,
5224 IUri **ppIUri)
5226 UriBuilder *This = URIBUILDER_THIS(iface);
5227 HRESULT hr;
5228 TRACE("(%p)->(0x%08x %d %d %p)\n", This, dwCreateFlags, dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
5230 if(dwCreateFlags == -1)
5231 hr = build_uri(This, ppIUri, 0, UriBuilder_USE_ORIGINAL_FLAGS, dwAllowEncodingPropertyMask);
5232 else
5233 hr = build_uri(This, ppIUri, dwCreateFlags, 0, dwAllowEncodingPropertyMask);
5235 if(hr == E_NOTIMPL)
5236 FIXME("(%p)->(0x%08x %d %d %p)\n", This, dwCreateFlags, dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
5237 return hr;
5240 static HRESULT WINAPI UriBuilder_CreateUriWithFlags(IUriBuilder *iface,
5241 DWORD dwCreateFlags,
5242 DWORD dwUriBuilderFlags,
5243 DWORD dwAllowEncodingPropertyMask,
5244 DWORD_PTR dwReserved,
5245 IUri **ppIUri)
5247 UriBuilder *This = URIBUILDER_THIS(iface);
5248 HRESULT hr;
5249 TRACE("(%p)->(0x%08x 0x%08x %d %d %p)\n", This, dwCreateFlags, dwUriBuilderFlags,
5250 dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
5252 hr = build_uri(This, ppIUri, dwCreateFlags, dwUriBuilderFlags, dwAllowEncodingPropertyMask);
5253 if(hr == E_NOTIMPL)
5254 FIXME("(%p)->(0x%08x 0x%08x %d %d %p)\n", This, dwCreateFlags, dwUriBuilderFlags,
5255 dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
5256 return hr;
5259 static HRESULT WINAPI UriBuilder_GetIUri(IUriBuilder *iface, IUri **ppIUri)
5261 UriBuilder *This = URIBUILDER_THIS(iface);
5262 TRACE("(%p)->(%p)\n", This, ppIUri);
5264 if(!ppIUri)
5265 return E_POINTER;
5267 if(This->uri) {
5268 IUri *uri = URI(This->uri);
5269 IUri_AddRef(uri);
5270 *ppIUri = uri;
5271 } else
5272 *ppIUri = NULL;
5274 return S_OK;
5277 static HRESULT WINAPI UriBuilder_SetIUri(IUriBuilder *iface, IUri *pIUri)
5279 UriBuilder *This = URIBUILDER_THIS(iface);
5280 TRACE("(%p)->(%p)\n", This, pIUri);
5282 if(pIUri) {
5283 Uri *uri;
5285 if((uri = get_uri_obj(pIUri))) {
5286 /* Only reset the builder if it's Uri isn't the same as
5287 * the Uri passed to the function.
5289 if(This->uri != uri) {
5290 reset_builder(This);
5292 This->uri = uri;
5293 if(uri->has_port)
5294 This->port = uri->port;
5296 IUri_AddRef(pIUri);
5298 } else {
5299 FIXME("(%p)->(%p) Unknown IUri types not supported yet.\n", This, pIUri);
5300 return E_NOTIMPL;
5302 } else if(This->uri)
5303 /* Only reset the builder if it's Uri isn't NULL. */
5304 reset_builder(This);
5306 return S_OK;
5309 static HRESULT WINAPI UriBuilder_GetFragment(IUriBuilder *iface, DWORD *pcchFragment, LPCWSTR *ppwzFragment)
5311 UriBuilder *This = URIBUILDER_THIS(iface);
5312 TRACE("(%p)->(%p %p)\n", This, pcchFragment, ppwzFragment);
5314 if(!This->uri || This->uri->fragment_start == -1 || This->modified_props & Uri_HAS_FRAGMENT)
5315 return get_builder_component(&This->fragment, &This->fragment_len, NULL, 0, ppwzFragment, pcchFragment);
5316 else
5317 return get_builder_component(&This->fragment, &This->fragment_len, This->uri->canon_uri+This->uri->fragment_start,
5318 This->uri->fragment_len, ppwzFragment, pcchFragment);
5321 static HRESULT WINAPI UriBuilder_GetHost(IUriBuilder *iface, DWORD *pcchHost, LPCWSTR *ppwzHost)
5323 UriBuilder *This = URIBUILDER_THIS(iface);
5324 TRACE("(%p)->(%p %p)\n", This, pcchHost, ppwzHost);
5326 if(!This->uri || This->uri->host_start == -1 || This->modified_props & Uri_HAS_HOST)
5327 return get_builder_component(&This->host, &This->host_len, NULL, 0, ppwzHost, pcchHost);
5328 else {
5329 if(This->uri->host_type == Uri_HOST_IPV6)
5330 /* Don't include the '[' and ']' around the address. */
5331 return get_builder_component(&This->host, &This->host_len, This->uri->canon_uri+This->uri->host_start+1,
5332 This->uri->host_len-2, ppwzHost, pcchHost);
5333 else
5334 return get_builder_component(&This->host, &This->host_len, This->uri->canon_uri+This->uri->host_start,
5335 This->uri->host_len, ppwzHost, pcchHost);
5339 static HRESULT WINAPI UriBuilder_GetPassword(IUriBuilder *iface, DWORD *pcchPassword, LPCWSTR *ppwzPassword)
5341 UriBuilder *This = URIBUILDER_THIS(iface);
5342 TRACE("(%p)->(%p %p)\n", This, pcchPassword, ppwzPassword);
5344 if(!This->uri || This->uri->userinfo_split == -1 || This->modified_props & Uri_HAS_PASSWORD)
5345 return get_builder_component(&This->password, &This->password_len, NULL, 0, ppwzPassword, pcchPassword);
5346 else {
5347 const WCHAR *start = This->uri->canon_uri+This->uri->userinfo_start+This->uri->userinfo_split+1;
5348 DWORD len = This->uri->userinfo_len-This->uri->userinfo_split-1;
5349 return get_builder_component(&This->password, &This->password_len, start, len, ppwzPassword, pcchPassword);
5353 static HRESULT WINAPI UriBuilder_GetPath(IUriBuilder *iface, DWORD *pcchPath, LPCWSTR *ppwzPath)
5355 UriBuilder *This = URIBUILDER_THIS(iface);
5356 TRACE("(%p)->(%p %p)\n", This, pcchPath, ppwzPath);
5358 if(!This->uri || This->uri->path_start == -1 || This->modified_props & Uri_HAS_PATH)
5359 return get_builder_component(&This->path, &This->path_len, NULL, 0, ppwzPath, pcchPath);
5360 else
5361 return get_builder_component(&This->path, &This->path_len, This->uri->canon_uri+This->uri->path_start,
5362 This->uri->path_len, ppwzPath, pcchPath);
5365 static HRESULT WINAPI UriBuilder_GetPort(IUriBuilder *iface, BOOL *pfHasPort, DWORD *pdwPort)
5367 UriBuilder *This = URIBUILDER_THIS(iface);
5368 TRACE("(%p)->(%p %p)\n", This, pfHasPort, pdwPort);
5370 if(!pfHasPort) {
5371 if(pdwPort)
5372 *pdwPort = 0;
5373 return E_POINTER;
5376 if(!pdwPort) {
5377 *pfHasPort = FALSE;
5378 return E_POINTER;
5381 *pfHasPort = This->has_port;
5382 *pdwPort = This->port;
5383 return S_OK;
5386 static HRESULT WINAPI UriBuilder_GetQuery(IUriBuilder *iface, DWORD *pcchQuery, LPCWSTR *ppwzQuery)
5388 UriBuilder *This = URIBUILDER_THIS(iface);
5389 TRACE("(%p)->(%p %p)\n", This, pcchQuery, ppwzQuery);
5391 if(!This->uri || This->uri->query_start == -1 || This->modified_props & Uri_HAS_QUERY)
5392 return get_builder_component(&This->query, &This->query_len, NULL, 0, ppwzQuery, pcchQuery);
5393 else
5394 return get_builder_component(&This->query, &This->query_len, This->uri->canon_uri+This->uri->query_start,
5395 This->uri->query_len, ppwzQuery, pcchQuery);
5398 static HRESULT WINAPI UriBuilder_GetSchemeName(IUriBuilder *iface, DWORD *pcchSchemeName, LPCWSTR *ppwzSchemeName)
5400 UriBuilder *This = URIBUILDER_THIS(iface);
5401 TRACE("(%p)->(%p %p)\n", This, pcchSchemeName, ppwzSchemeName);
5403 if(!This->uri || This->uri->scheme_start == -1 || This->modified_props & Uri_HAS_SCHEME_NAME)
5404 return get_builder_component(&This->scheme, &This->scheme_len, NULL, 0, ppwzSchemeName, pcchSchemeName);
5405 else
5406 return get_builder_component(&This->scheme, &This->scheme_len, This->uri->canon_uri+This->uri->scheme_start,
5407 This->uri->scheme_len, ppwzSchemeName, pcchSchemeName);
5410 static HRESULT WINAPI UriBuilder_GetUserName(IUriBuilder *iface, DWORD *pcchUserName, LPCWSTR *ppwzUserName)
5412 UriBuilder *This = URIBUILDER_THIS(iface);
5413 TRACE("(%p)->(%p %p)\n", This, pcchUserName, ppwzUserName);
5415 if(!This->uri || This->uri->userinfo_start == -1 || This->uri->userinfo_split == 0 ||
5416 This->modified_props & Uri_HAS_USER_NAME)
5417 return get_builder_component(&This->username, &This->username_len, NULL, 0, ppwzUserName, pcchUserName);
5418 else {
5419 const WCHAR *start = This->uri->canon_uri+This->uri->userinfo_start;
5421 /* Check if there's a password in the userinfo section. */
5422 if(This->uri->userinfo_split > -1)
5423 /* Don't include the password. */
5424 return get_builder_component(&This->username, &This->username_len, start,
5425 This->uri->userinfo_split, ppwzUserName, pcchUserName);
5426 else
5427 return get_builder_component(&This->username, &This->username_len, start,
5428 This->uri->userinfo_len, ppwzUserName, pcchUserName);
5432 static HRESULT WINAPI UriBuilder_SetFragment(IUriBuilder *iface, LPCWSTR pwzNewValue)
5434 UriBuilder *This = URIBUILDER_THIS(iface);
5435 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
5436 return set_builder_component(&This->fragment, &This->fragment_len, pwzNewValue, '#',
5437 &This->modified_props, Uri_HAS_FRAGMENT);
5440 static HRESULT WINAPI UriBuilder_SetHost(IUriBuilder *iface, LPCWSTR pwzNewValue)
5442 UriBuilder *This = URIBUILDER_THIS(iface);
5443 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
5445 /* Host name can't be set to NULL. */
5446 if(!pwzNewValue)
5447 return E_INVALIDARG;
5449 return set_builder_component(&This->host, &This->host_len, pwzNewValue, 0,
5450 &This->modified_props, Uri_HAS_HOST);
5453 static HRESULT WINAPI UriBuilder_SetPassword(IUriBuilder *iface, LPCWSTR pwzNewValue)
5455 UriBuilder *This = URIBUILDER_THIS(iface);
5456 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
5457 return set_builder_component(&This->password, &This->password_len, pwzNewValue, 0,
5458 &This->modified_props, Uri_HAS_PASSWORD);
5461 static HRESULT WINAPI UriBuilder_SetPath(IUriBuilder *iface, LPCWSTR pwzNewValue)
5463 UriBuilder *This = URIBUILDER_THIS(iface);
5464 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
5465 return set_builder_component(&This->path, &This->path_len, pwzNewValue, 0,
5466 &This->modified_props, Uri_HAS_PATH);
5469 static HRESULT WINAPI UriBuilder_SetPort(IUriBuilder *iface, BOOL fHasPort, DWORD dwNewValue)
5471 UriBuilder *This = URIBUILDER_THIS(iface);
5472 TRACE("(%p)->(%d %d)\n", This, fHasPort, dwNewValue);
5474 This->has_port = fHasPort;
5475 This->port = dwNewValue;
5476 This->modified_props |= Uri_HAS_PORT;
5477 return S_OK;
5480 static HRESULT WINAPI UriBuilder_SetQuery(IUriBuilder *iface, LPCWSTR pwzNewValue)
5482 UriBuilder *This = URIBUILDER_THIS(iface);
5483 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
5484 return set_builder_component(&This->query, &This->query_len, pwzNewValue, '?',
5485 &This->modified_props, Uri_HAS_QUERY);
5488 static HRESULT WINAPI UriBuilder_SetSchemeName(IUriBuilder *iface, LPCWSTR pwzNewValue)
5490 UriBuilder *This = URIBUILDER_THIS(iface);
5491 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
5493 /* Only set the scheme name if it's not NULL or empty. */
5494 if(!pwzNewValue || !*pwzNewValue)
5495 return E_INVALIDARG;
5497 return set_builder_component(&This->scheme, &This->scheme_len, pwzNewValue, 0,
5498 &This->modified_props, Uri_HAS_SCHEME_NAME);
5501 static HRESULT WINAPI UriBuilder_SetUserName(IUriBuilder *iface, LPCWSTR pwzNewValue)
5503 UriBuilder *This = URIBUILDER_THIS(iface);
5504 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
5505 return set_builder_component(&This->username, &This->username_len, pwzNewValue, 0,
5506 &This->modified_props, Uri_HAS_USER_NAME);
5509 static HRESULT WINAPI UriBuilder_RemoveProperties(IUriBuilder *iface, DWORD dwPropertyMask)
5511 const DWORD accepted_flags = Uri_HAS_AUTHORITY|Uri_HAS_DOMAIN|Uri_HAS_EXTENSION|Uri_HAS_FRAGMENT|Uri_HAS_HOST|
5512 Uri_HAS_PASSWORD|Uri_HAS_PATH|Uri_HAS_PATH_AND_QUERY|Uri_HAS_QUERY|
5513 Uri_HAS_USER_INFO|Uri_HAS_USER_NAME;
5515 UriBuilder *This = URIBUILDER_THIS(iface);
5516 TRACE("(%p)->(0x%08x)\n", This, dwPropertyMask);
5518 if(dwPropertyMask & ~accepted_flags)
5519 return E_INVALIDARG;
5521 if(dwPropertyMask & Uri_HAS_FRAGMENT)
5522 UriBuilder_SetFragment(iface, NULL);
5524 /* Even though you can't set the host name to NULL or an
5525 * empty string, you can still remove it... for some reason.
5527 if(dwPropertyMask & Uri_HAS_HOST)
5528 set_builder_component(&This->host, &This->host_len, NULL, 0,
5529 &This->modified_props, Uri_HAS_HOST);
5531 if(dwPropertyMask & Uri_HAS_PASSWORD)
5532 UriBuilder_SetPassword(iface, NULL);
5534 if(dwPropertyMask & Uri_HAS_PATH)
5535 UriBuilder_SetPath(iface, NULL);
5537 if(dwPropertyMask & Uri_HAS_PORT)
5538 UriBuilder_SetPort(iface, FALSE, 0);
5540 if(dwPropertyMask & Uri_HAS_QUERY)
5541 UriBuilder_SetQuery(iface, NULL);
5543 if(dwPropertyMask & Uri_HAS_USER_NAME)
5544 UriBuilder_SetUserName(iface, NULL);
5546 return S_OK;
5549 static HRESULT WINAPI UriBuilder_HasBeenModified(IUriBuilder *iface, BOOL *pfModified)
5551 UriBuilder *This = URIBUILDER_THIS(iface);
5552 TRACE("(%p)->(%p)\n", This, pfModified);
5554 if(!pfModified)
5555 return E_POINTER;
5557 *pfModified = This->modified_props > 0;
5558 return S_OK;
5561 #undef URIBUILDER_THIS
5563 static const IUriBuilderVtbl UriBuilderVtbl = {
5564 UriBuilder_QueryInterface,
5565 UriBuilder_AddRef,
5566 UriBuilder_Release,
5567 UriBuilder_CreateUriSimple,
5568 UriBuilder_CreateUri,
5569 UriBuilder_CreateUriWithFlags,
5570 UriBuilder_GetIUri,
5571 UriBuilder_SetIUri,
5572 UriBuilder_GetFragment,
5573 UriBuilder_GetHost,
5574 UriBuilder_GetPassword,
5575 UriBuilder_GetPath,
5576 UriBuilder_GetPort,
5577 UriBuilder_GetQuery,
5578 UriBuilder_GetSchemeName,
5579 UriBuilder_GetUserName,
5580 UriBuilder_SetFragment,
5581 UriBuilder_SetHost,
5582 UriBuilder_SetPassword,
5583 UriBuilder_SetPath,
5584 UriBuilder_SetPort,
5585 UriBuilder_SetQuery,
5586 UriBuilder_SetSchemeName,
5587 UriBuilder_SetUserName,
5588 UriBuilder_RemoveProperties,
5589 UriBuilder_HasBeenModified,
5592 /***********************************************************************
5593 * CreateIUriBuilder (urlmon.@)
5595 HRESULT WINAPI CreateIUriBuilder(IUri *pIUri, DWORD dwFlags, DWORD_PTR dwReserved, IUriBuilder **ppIUriBuilder)
5597 UriBuilder *ret;
5599 TRACE("(%p %x %x %p)\n", pIUri, dwFlags, (DWORD)dwReserved, ppIUriBuilder);
5601 if(!ppIUriBuilder)
5602 return E_POINTER;
5604 ret = heap_alloc_zero(sizeof(UriBuilder));
5605 if(!ret)
5606 return E_OUTOFMEMORY;
5608 ret->lpIUriBuilderVtbl = &UriBuilderVtbl;
5609 ret->ref = 1;
5611 if(pIUri) {
5612 Uri *uri;
5614 if((uri = get_uri_obj(pIUri))) {
5615 IUri_AddRef(pIUri);
5616 ret->uri = uri;
5618 if(uri->has_port)
5619 /* Windows doesn't set 'has_port' to TRUE in this case. */
5620 ret->port = uri->port;
5622 } else {
5623 heap_free(ret);
5624 *ppIUriBuilder = NULL;
5625 FIXME("(%p %x %x %p): Unknown IUri types not supported yet.\n", pIUri, dwFlags,
5626 (DWORD)dwReserved, ppIUriBuilder);
5627 return E_NOTIMPL;
5631 *ppIUriBuilder = URIBUILDER(ret);
5632 return S_OK;
5635 static HRESULT combine_uri(Uri *base, Uri *relative, DWORD flags, IUri **result) {
5636 Uri *ret;
5637 HRESULT hr;
5638 parse_data data;
5639 DWORD create_flags = 0, len = 0;
5641 memset(&data, 0, sizeof(parse_data));
5643 /* Base case is when the relative Uri has a scheme name,
5644 * if it does, then 'result' will contain the same data
5645 * as the relative Uri.
5647 if(relative->scheme_start > -1) {
5648 data.uri = SysAllocString(relative->raw_uri);
5649 if(!data.uri) {
5650 *result = NULL;
5651 return E_OUTOFMEMORY;
5654 parse_uri(&data, 0);
5656 ret = create_uri_obj();
5657 if(!ret) {
5658 *result = NULL;
5659 return E_OUTOFMEMORY;
5662 ret->raw_uri = data.uri;
5663 hr = canonicalize_uri(&data, ret, 0);
5664 if(FAILED(hr)) {
5665 IUri_Release(URI(ret));
5666 *result = NULL;
5667 return hr;
5670 apply_default_flags(&create_flags);
5671 ret->create_flags = create_flags;
5673 *result = URI(ret);
5674 } else {
5675 DWORD raw_flags = 0;
5677 if(base->scheme_start > -1) {
5678 data.scheme = base->canon_uri+base->scheme_start;
5679 data.scheme_len = base->scheme_len;
5680 data.scheme_type = base->scheme_type;
5681 } else {
5682 data.is_relative = TRUE;
5683 data.scheme_type = URL_SCHEME_UNKNOWN;
5684 create_flags |= Uri_CREATE_ALLOW_RELATIVE;
5687 if(base->authority_start > -1) {
5688 if(base->userinfo_start > -1 && base->userinfo_split != 0) {
5689 data.username = base->canon_uri+base->userinfo_start;
5690 data.username_len = (base->userinfo_split > -1) ? base->userinfo_split : base->userinfo_len;
5693 if(base->userinfo_split > -1) {
5694 data.password = base->canon_uri+base->userinfo_start+base->userinfo_split+1;
5695 data.password_len = base->userinfo_len-base->userinfo_split-1;
5698 if(base->host_start > -1) {
5699 data.host = base->canon_uri+base->host_start;
5700 data.host_len = base->host_len;
5701 data.host_type = base->host_type;
5704 if(base->has_port) {
5705 data.has_port = TRUE;
5706 data.port_value = base->port;
5708 } else
5709 data.is_opaque = TRUE;
5711 if(relative->path_start == -1 || !relative->path_len) {
5712 if(base->path_start > -1) {
5713 data.path = base->canon_uri+base->path_start;
5714 data.path_len = base->path_len;
5715 } else if((base->path_start == -1 || !base->path_len) && !data.is_opaque) {
5716 /* Just set the path as a '/' if the base didn't have
5717 * one and if it's an hierarchical URI.
5719 static const WCHAR slashW[] = {'/',0};
5720 data.path = slashW;
5721 data.path_len = 1;
5724 if(relative->query_start > -1) {
5725 data.query = relative->canon_uri+relative->query_start;
5726 data.query_len = relative->query_len;
5727 } else if(base->query_start > -1) {
5728 data.query = base->canon_uri+base->query_start;
5729 data.query_len = base->query_len;
5731 } else {
5732 FIXME("Path merging not implemented yet!\n");
5733 *result = NULL;
5734 return E_NOTIMPL;
5737 if(relative->fragment_start > -1) {
5738 data.fragment = relative->canon_uri+relative->fragment_start;
5739 data.fragment_len = relative->fragment_len;
5742 if(flags & URL_DONT_SIMPLIFY)
5743 raw_flags |= RAW_URI_FORCE_PORT_DISP;
5745 len = generate_raw_uri(&data, data.uri, raw_flags);
5746 data.uri = SysAllocStringLen(NULL, len);
5747 if(!data.uri) {
5748 *result = NULL;
5749 return E_OUTOFMEMORY;
5752 generate_raw_uri(&data, data.uri, raw_flags);
5754 ret = create_uri_obj();
5755 if(!ret) {
5756 SysFreeString(data.uri);
5757 *result = NULL;
5758 return E_OUTOFMEMORY;
5761 ret->raw_uri = data.uri;
5762 hr = canonicalize_uri(&data, ret, create_flags);
5763 if(FAILED(hr)) {
5764 IUri_Release(URI(ret));
5765 *result = NULL;
5766 return hr;
5769 apply_default_flags(&create_flags);
5770 ret->create_flags = create_flags;
5771 *result = URI(ret);
5774 return S_OK;
5777 /***********************************************************************
5778 * CoInternetCombineIUri (urlmon.@)
5780 HRESULT WINAPI CoInternetCombineIUri(IUri *pBaseUri, IUri *pRelativeUri, DWORD dwCombineFlags,
5781 IUri **ppCombinedUri, DWORD_PTR dwReserved)
5783 HRESULT hr;
5784 Uri *relative, *base;
5785 TRACE("(%p %p %x %p %x)\n", pBaseUri, pRelativeUri, dwCombineFlags, ppCombinedUri, (DWORD)dwReserved);
5787 if(!ppCombinedUri)
5788 return E_INVALIDARG;
5790 if(!pBaseUri || !pRelativeUri) {
5791 *ppCombinedUri = NULL;
5792 return E_INVALIDARG;
5795 relative = get_uri_obj(pRelativeUri);
5796 base = get_uri_obj(pBaseUri);
5797 if(!relative || !base) {
5798 *ppCombinedUri = NULL;
5799 FIXME("(%p %p %x %p %x) Unknown IUri types not supported yet.\n",
5800 pBaseUri, pRelativeUri, dwCombineFlags, ppCombinedUri, (DWORD)dwReserved);
5801 return E_NOTIMPL;
5804 hr = combine_uri(base, relative, dwCombineFlags, ppCombinedUri);
5805 if(hr == E_NOTIMPL)
5806 FIXME("(%p %p %x %p %x): stub\n", pBaseUri, pRelativeUri, dwCombineFlags, ppCombinedUri, (DWORD)dwReserved);
5807 return hr;