Release 1.3.7.
[wine/gsoc-2012-control.git] / dlls / urlmon / uri.c
blob6ed71e120153d1be2ae43558d8558784959a56f9
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
40 #define RAW_URI_CONVERT_TO_DOS_PATH 0x2
42 WINE_DEFAULT_DEBUG_CHANNEL(urlmon);
44 static const IID IID_IUriObj = {0x4b364760,0x9f51,0x11df,{0x98,0x1c,0x08,0x00,0x20,0x0c,0x9a,0x66}};
46 typedef struct {
47 const IUriVtbl *lpIUriVtbl;
48 LONG ref;
50 BSTR raw_uri;
52 /* Information about the canonicalized URI's buffer. */
53 WCHAR *canon_uri;
54 DWORD canon_size;
55 DWORD canon_len;
56 BOOL display_modifiers;
57 DWORD create_flags;
59 INT scheme_start;
60 DWORD scheme_len;
61 URL_SCHEME scheme_type;
63 INT userinfo_start;
64 DWORD userinfo_len;
65 INT userinfo_split;
67 INT host_start;
68 DWORD host_len;
69 Uri_HOST_TYPE host_type;
71 INT port_offset;
72 DWORD port;
73 BOOL has_port;
75 INT authority_start;
76 DWORD authority_len;
78 INT domain_offset;
80 INT path_start;
81 DWORD path_len;
82 INT extension_offset;
84 INT query_start;
85 DWORD query_len;
87 INT fragment_start;
88 DWORD fragment_len;
89 } Uri;
91 typedef struct {
92 const IUriBuilderVtbl *lpIUriBuilderVtbl;
93 LONG ref;
95 Uri *uri;
96 DWORD modified_props;
98 WCHAR *fragment;
99 DWORD fragment_len;
101 WCHAR *host;
102 DWORD host_len;
104 WCHAR *password;
105 DWORD password_len;
107 WCHAR *path;
108 DWORD path_len;
110 BOOL has_port;
111 DWORD port;
113 WCHAR *query;
114 DWORD query_len;
116 WCHAR *scheme;
117 DWORD scheme_len;
119 WCHAR *username;
120 DWORD username_len;
121 } UriBuilder;
123 typedef struct {
124 const WCHAR *str;
125 DWORD len;
126 } h16;
128 typedef struct {
129 /* IPv6 addresses can hold up to 8 h16 components. */
130 h16 components[8];
131 DWORD h16_count;
133 /* An IPv6 can have 1 elision ("::"). */
134 const WCHAR *elision;
136 /* An IPv6 can contain 1 IPv4 address as the last 32bits of the address. */
137 const WCHAR *ipv4;
138 DWORD ipv4_len;
140 INT components_size;
141 INT elision_size;
142 } ipv6_address;
144 typedef struct {
145 BSTR uri;
147 BOOL is_relative;
148 BOOL is_opaque;
149 BOOL has_implicit_scheme;
150 BOOL has_implicit_ip;
151 UINT implicit_ipv4;
153 const WCHAR *scheme;
154 DWORD scheme_len;
155 URL_SCHEME scheme_type;
157 const WCHAR *username;
158 DWORD username_len;
160 const WCHAR *password;
161 DWORD password_len;
163 const WCHAR *host;
164 DWORD host_len;
165 Uri_HOST_TYPE host_type;
167 BOOL has_ipv6;
168 ipv6_address ipv6_address;
170 BOOL has_port;
171 const WCHAR *port;
172 DWORD port_len;
173 DWORD port_value;
175 const WCHAR *path;
176 DWORD path_len;
178 const WCHAR *query;
179 DWORD query_len;
181 const WCHAR *fragment;
182 DWORD fragment_len;
183 } parse_data;
185 static const CHAR hexDigits[] = "0123456789ABCDEF";
187 /* List of scheme types/scheme names that are recognized by the IUri interface as of IE 7. */
188 static const struct {
189 URL_SCHEME scheme;
190 WCHAR scheme_name[16];
191 } recognized_schemes[] = {
192 {URL_SCHEME_FTP, {'f','t','p',0}},
193 {URL_SCHEME_HTTP, {'h','t','t','p',0}},
194 {URL_SCHEME_GOPHER, {'g','o','p','h','e','r',0}},
195 {URL_SCHEME_MAILTO, {'m','a','i','l','t','o',0}},
196 {URL_SCHEME_NEWS, {'n','e','w','s',0}},
197 {URL_SCHEME_NNTP, {'n','n','t','p',0}},
198 {URL_SCHEME_TELNET, {'t','e','l','n','e','t',0}},
199 {URL_SCHEME_WAIS, {'w','a','i','s',0}},
200 {URL_SCHEME_FILE, {'f','i','l','e',0}},
201 {URL_SCHEME_MK, {'m','k',0}},
202 {URL_SCHEME_HTTPS, {'h','t','t','p','s',0}},
203 {URL_SCHEME_SHELL, {'s','h','e','l','l',0}},
204 {URL_SCHEME_SNEWS, {'s','n','e','w','s',0}},
205 {URL_SCHEME_LOCAL, {'l','o','c','a','l',0}},
206 {URL_SCHEME_JAVASCRIPT, {'j','a','v','a','s','c','r','i','p','t',0}},
207 {URL_SCHEME_VBSCRIPT, {'v','b','s','c','r','i','p','t',0}},
208 {URL_SCHEME_ABOUT, {'a','b','o','u','t',0}},
209 {URL_SCHEME_RES, {'r','e','s',0}},
210 {URL_SCHEME_MSSHELLROOTED, {'m','s','-','s','h','e','l','l','-','r','o','o','t','e','d',0}},
211 {URL_SCHEME_MSSHELLIDLIST, {'m','s','-','s','h','e','l','l','-','i','d','l','i','s','t',0}},
212 {URL_SCHEME_MSHELP, {'h','c','p',0}},
213 {URL_SCHEME_WILDCARD, {'*',0}}
216 /* List of default ports Windows recognizes. */
217 static const struct {
218 URL_SCHEME scheme;
219 USHORT port;
220 } default_ports[] = {
221 {URL_SCHEME_FTP, 21},
222 {URL_SCHEME_HTTP, 80},
223 {URL_SCHEME_GOPHER, 70},
224 {URL_SCHEME_NNTP, 119},
225 {URL_SCHEME_TELNET, 23},
226 {URL_SCHEME_WAIS, 210},
227 {URL_SCHEME_HTTPS, 443},
230 /* List of 3 character top level domain names Windows seems to recognize.
231 * There might be more, but, these are the only ones I've found so far.
233 static const struct {
234 WCHAR tld_name[4];
235 } recognized_tlds[] = {
236 {{'c','o','m',0}},
237 {{'e','d','u',0}},
238 {{'g','o','v',0}},
239 {{'i','n','t',0}},
240 {{'m','i','l',0}},
241 {{'n','e','t',0}},
242 {{'o','r','g',0}}
245 static Uri *get_uri_obj(IUri *uri)
247 Uri *ret;
248 HRESULT hres;
250 hres = IUri_QueryInterface(uri, &IID_IUriObj, (void**)&ret);
251 return SUCCEEDED(hres) ? ret : NULL;
254 static inline BOOL is_alpha(WCHAR val) {
255 return ((val >= 'a' && val <= 'z') || (val >= 'A' && val <= 'Z'));
258 static inline BOOL is_num(WCHAR val) {
259 return (val >= '0' && val <= '9');
262 static inline BOOL is_drive_path(const WCHAR *str) {
263 return (is_alpha(str[0]) && (str[1] == ':' || str[1] == '|'));
266 static inline BOOL is_unc_path(const WCHAR *str) {
267 return (str[0] == '\\' && str[0] == '\\');
270 static inline BOOL is_forbidden_dos_path_char(WCHAR val) {
271 return (val == '>' || val == '<' || val == '\"');
274 /* A URI is implicitly a file path if it begins with
275 * a drive letter (eg X:) or starts with "\\" (UNC path).
277 static inline BOOL is_implicit_file_path(const WCHAR *str) {
278 return (is_unc_path(str) || (is_alpha(str[0]) && str[1] == ':'));
281 /* Checks if the URI is a hierarchical URI. A hierarchical
282 * URI is one that has "//" after the scheme.
284 static BOOL check_hierarchical(const WCHAR **ptr) {
285 const WCHAR *start = *ptr;
287 if(**ptr != '/')
288 return FALSE;
290 ++(*ptr);
291 if(**ptr != '/') {
292 *ptr = start;
293 return FALSE;
296 ++(*ptr);
297 return TRUE;
300 /* unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~" */
301 static inline BOOL is_unreserved(WCHAR val) {
302 return (is_alpha(val) || is_num(val) || val == '-' || val == '.' ||
303 val == '_' || val == '~');
306 /* sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
307 * / "*" / "+" / "," / ";" / "="
309 static inline BOOL is_subdelim(WCHAR val) {
310 return (val == '!' || val == '$' || val == '&' ||
311 val == '\'' || val == '(' || val == ')' ||
312 val == '*' || val == '+' || val == ',' ||
313 val == ';' || val == '=');
316 /* gen-delims = ":" / "/" / "?" / "#" / "[" / "]" / "@" */
317 static inline BOOL is_gendelim(WCHAR val) {
318 return (val == ':' || val == '/' || val == '?' ||
319 val == '#' || val == '[' || val == ']' ||
320 val == '@');
323 /* Characters that delimit the end of the authority
324 * section of a URI. Sometimes a '\\' is considered
325 * an authority delimeter.
327 static inline BOOL is_auth_delim(WCHAR val, BOOL acceptSlash) {
328 return (val == '#' || val == '/' || val == '?' ||
329 val == '\0' || (acceptSlash && val == '\\'));
332 /* reserved = gen-delims / sub-delims */
333 static inline BOOL is_reserved(WCHAR val) {
334 return (is_subdelim(val) || is_gendelim(val));
337 static inline BOOL is_hexdigit(WCHAR val) {
338 return ((val >= 'a' && val <= 'f') ||
339 (val >= 'A' && val <= 'F') ||
340 (val >= '0' && val <= '9'));
343 static inline BOOL is_path_delim(WCHAR val) {
344 return (!val || val == '#' || val == '?');
347 static BOOL is_default_port(URL_SCHEME scheme, DWORD port) {
348 DWORD i;
350 for(i = 0; i < sizeof(default_ports)/sizeof(default_ports[0]); ++i) {
351 if(default_ports[i].scheme == scheme && default_ports[i].port)
352 return TRUE;
355 return FALSE;
358 /* List of schemes types Windows seems to expect to be hierarchical. */
359 static inline BOOL is_hierarchical_scheme(URL_SCHEME type) {
360 return(type == URL_SCHEME_HTTP || type == URL_SCHEME_FTP ||
361 type == URL_SCHEME_GOPHER || type == URL_SCHEME_NNTP ||
362 type == URL_SCHEME_TELNET || type == URL_SCHEME_WAIS ||
363 type == URL_SCHEME_FILE || type == URL_SCHEME_HTTPS ||
364 type == URL_SCHEME_RES);
367 /* Checks if 'flags' contains an invalid combination of Uri_CREATE flags. */
368 static inline BOOL has_invalid_flag_combination(DWORD flags) {
369 return((flags & Uri_CREATE_DECODE_EXTRA_INFO && flags & Uri_CREATE_NO_DECODE_EXTRA_INFO) ||
370 (flags & Uri_CREATE_CANONICALIZE && flags & Uri_CREATE_NO_CANONICALIZE) ||
371 (flags & Uri_CREATE_CRACK_UNKNOWN_SCHEMES && flags & Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES) ||
372 (flags & Uri_CREATE_PRE_PROCESS_HTML_URI && flags & Uri_CREATE_NO_PRE_PROCESS_HTML_URI) ||
373 (flags & Uri_CREATE_IE_SETTINGS && flags & Uri_CREATE_NO_IE_SETTINGS));
376 /* Applies each default Uri_CREATE flags to 'flags' if it
377 * doesn't cause a flag conflict.
379 static void apply_default_flags(DWORD *flags) {
380 if(!(*flags & Uri_CREATE_NO_CANONICALIZE))
381 *flags |= Uri_CREATE_CANONICALIZE;
382 if(!(*flags & Uri_CREATE_NO_DECODE_EXTRA_INFO))
383 *flags |= Uri_CREATE_DECODE_EXTRA_INFO;
384 if(!(*flags & Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES))
385 *flags |= Uri_CREATE_CRACK_UNKNOWN_SCHEMES;
386 if(!(*flags & Uri_CREATE_NO_PRE_PROCESS_HTML_URI))
387 *flags |= Uri_CREATE_PRE_PROCESS_HTML_URI;
388 if(!(*flags & Uri_CREATE_IE_SETTINGS))
389 *flags |= Uri_CREATE_NO_IE_SETTINGS;
392 /* Determines if the URI is hierarchical using the information already parsed into
393 * data and using the current location of parsing in the URI string.
395 * Windows considers a URI hierarchical if on of the following is true:
396 * A.) It's a wildcard scheme.
397 * B.) It's an implicit file scheme.
398 * C.) It's a known hierarchical scheme and it has two '\\' after the scheme name.
399 * (the '\\' will be converted into "//" during canonicalization).
400 * D.) It's not a relative URI and "//" appears after the scheme name.
402 static inline BOOL is_hierarchical_uri(const WCHAR **ptr, const parse_data *data) {
403 const WCHAR *start = *ptr;
405 if(data->scheme_type == URL_SCHEME_WILDCARD)
406 return TRUE;
407 else if(data->scheme_type == URL_SCHEME_FILE && data->has_implicit_scheme)
408 return TRUE;
409 else if(is_hierarchical_scheme(data->scheme_type) && (*ptr)[0] == '\\' && (*ptr)[1] == '\\') {
410 *ptr += 2;
411 return TRUE;
412 } else if(!data->is_relative && check_hierarchical(ptr))
413 return TRUE;
415 *ptr = start;
416 return FALSE;
419 /* Checks if the two Uri's are logically equivalent. It's a simple
420 * comparison, since they are both of type Uri, and it can access
421 * the properties of each Uri directly without the need to go
422 * through the "IUri_Get*" interface calls.
424 static BOOL are_equal_simple(const Uri *a, const Uri *b) {
425 if(a->scheme_type == b->scheme_type) {
426 const BOOL known_scheme = a->scheme_type != URL_SCHEME_UNKNOWN;
427 const BOOL are_hierarchical =
428 (a->authority_start > -1 && b->authority_start > -1);
430 if(a->scheme_type == URL_SCHEME_FILE) {
431 if(a->canon_len == b->canon_len)
432 return !StrCmpIW(a->canon_uri, b->canon_uri);
435 /* Only compare the scheme names (if any) if their unknown scheme types. */
436 if(!known_scheme) {
437 if((a->scheme_start > -1 && b->scheme_start > -1) &&
438 (a->scheme_len == b->scheme_len)) {
439 /* Make sure the schemes are the same. */
440 if(StrCmpNW(a->canon_uri+a->scheme_start, b->canon_uri+b->scheme_start, a->scheme_len))
441 return FALSE;
442 } else if(a->scheme_len != b->scheme_len)
443 /* One of the Uri's has a scheme name, while the other doesn't. */
444 return FALSE;
447 /* If they have a userinfo component, perform case sensitive compare. */
448 if((a->userinfo_start > -1 && b->userinfo_start > -1) &&
449 (a->userinfo_len == b->userinfo_len)) {
450 if(StrCmpNW(a->canon_uri+a->userinfo_start, b->canon_uri+b->userinfo_start, a->userinfo_len))
451 return FALSE;
452 } else if(a->userinfo_len != b->userinfo_len)
453 /* One of the Uri's had a userinfo, while the other one doesn't. */
454 return FALSE;
456 /* Check if they have a host name. */
457 if((a->host_start > -1 && b->host_start > -1) &&
458 (a->host_len == b->host_len)) {
459 /* Perform a case insensitive compare if they are a known scheme type. */
460 if(known_scheme) {
461 if(StrCmpNIW(a->canon_uri+a->host_start, b->canon_uri+b->host_start, a->host_len))
462 return FALSE;
463 } else if(StrCmpNW(a->canon_uri+a->host_start, b->canon_uri+b->host_start, a->host_len))
464 return FALSE;
465 } else if(a->host_len != b->host_len)
466 /* One of the Uri's had a host, while the other one didn't. */
467 return FALSE;
469 if(a->has_port && b->has_port) {
470 if(a->port != b->port)
471 return FALSE;
472 } else if(a->has_port || b->has_port)
473 /* One had a port, while the other one didn't. */
474 return FALSE;
476 /* Windows is weird with how it handles paths. For example
477 * One URI could be "http://google.com" (after canonicalization)
478 * and one could be "http://google.com/" and the IsEqual function
479 * would still evaluate to TRUE, but, only if they are both hierarchical
480 * URIs.
482 if((a->path_start > -1 && b->path_start > -1) &&
483 (a->path_len == b->path_len)) {
484 if(StrCmpNW(a->canon_uri+a->path_start, b->canon_uri+b->path_start, a->path_len))
485 return FALSE;
486 } else if(are_hierarchical && a->path_len == -1 && b->path_len == 0) {
487 if(*(a->canon_uri+a->path_start) != '/')
488 return FALSE;
489 } else if(are_hierarchical && b->path_len == 1 && a->path_len == 0) {
490 if(*(b->canon_uri+b->path_start) != '/')
491 return FALSE;
492 } else if(a->path_len != b->path_len)
493 return FALSE;
495 /* Compare the query strings of the two URIs. */
496 if((a->query_start > -1 && b->query_start > -1) &&
497 (a->query_len == b->query_len)) {
498 if(StrCmpNW(a->canon_uri+a->query_start, b->canon_uri+b->query_start, a->query_len))
499 return FALSE;
500 } else if(a->query_len != b->query_len)
501 return FALSE;
503 if((a->fragment_start > -1 && b->fragment_start > -1) &&
504 (a->fragment_len == b->fragment_len)) {
505 if(StrCmpNW(a->canon_uri+a->fragment_start, b->canon_uri+b->fragment_start, a->fragment_len))
506 return FALSE;
507 } else if(a->fragment_len != b->fragment_len)
508 return FALSE;
510 /* If we get here, the two URIs are equivalent. */
511 return TRUE;
514 return FALSE;
517 /* Computes the size of the given IPv6 address.
518 * Each h16 component is 16bits, if there is an IPv4 address, it's
519 * 32bits. If there's an elision it can be 16bits to 128bits, depending
520 * on the number of other components.
522 * Modeled after google-url's CheckIPv6ComponentsSize function
524 static void compute_ipv6_comps_size(ipv6_address *address) {
525 address->components_size = address->h16_count * 2;
527 if(address->ipv4)
528 /* IPv4 address is 4 bytes. */
529 address->components_size += 4;
531 if(address->elision) {
532 /* An elision can be anywhere from 2 bytes up to 16 bytes.
533 * It size depends on the size of the h16 and IPv4 components.
535 address->elision_size = 16 - address->components_size;
536 if(address->elision_size < 2)
537 address->elision_size = 2;
538 } else
539 address->elision_size = 0;
542 /* Taken from dlls/jscript/lex.c */
543 static int hex_to_int(WCHAR val) {
544 if(val >= '0' && val <= '9')
545 return val - '0';
546 else if(val >= 'a' && val <= 'f')
547 return val - 'a' + 10;
548 else if(val >= 'A' && val <= 'F')
549 return val - 'A' + 10;
551 return -1;
554 /* Helper function for converting a percent encoded string
555 * representation of a WCHAR value into its actual WCHAR value. If
556 * the two characters following the '%' aren't valid hex values then
557 * this function returns the NULL character.
559 * Eg.
560 * "%2E" will result in '.' being returned by this function.
562 static WCHAR decode_pct_val(const WCHAR *ptr) {
563 WCHAR ret = '\0';
565 if(*ptr == '%' && is_hexdigit(*(ptr + 1)) && is_hexdigit(*(ptr + 2))) {
566 INT a = hex_to_int(*(ptr + 1));
567 INT b = hex_to_int(*(ptr + 2));
569 ret = a << 4;
570 ret += b;
573 return ret;
576 /* Helper function for percent encoding a given character
577 * and storing the encoded value into a given buffer (dest).
579 * It's up to the calling function to ensure that there is
580 * at least enough space in 'dest' for the percent encoded
581 * value to be stored (so dest + 3 spaces available).
583 static inline void pct_encode_val(WCHAR val, WCHAR *dest) {
584 dest[0] = '%';
585 dest[1] = hexDigits[(val >> 4) & 0xf];
586 dest[2] = hexDigits[val & 0xf];
589 /* Scans the range of characters [str, end] and returns the last occurrence
590 * of 'ch' or returns NULL.
592 static const WCHAR *str_last_of(const WCHAR *str, const WCHAR *end, WCHAR ch) {
593 const WCHAR *ptr = end;
595 while(ptr >= str) {
596 if(*ptr == ch)
597 return ptr;
598 --ptr;
601 return NULL;
604 /* Attempts to parse the domain name from the host.
606 * This function also includes the Top-level Domain (TLD) name
607 * of the host when it tries to find the domain name. If it finds
608 * a valid domain name it will assign 'domain_start' the offset
609 * into 'host' where the domain name starts.
611 * It's implied that if a domain name its range is implied to be
612 * [host+domain_start, host+host_len).
614 static void find_domain_name(const WCHAR *host, DWORD host_len,
615 INT *domain_start) {
616 const WCHAR *last_tld, *sec_last_tld, *end;
618 end = host+host_len-1;
620 *domain_start = -1;
622 /* There has to be at least enough room for a '.' followed by a
623 * 3 character TLD for a domain to even exist in the host name.
625 if(host_len < 4)
626 return;
628 last_tld = str_last_of(host, end, '.');
629 if(!last_tld)
630 /* http://hostname -> has no domain name. */
631 return;
633 sec_last_tld = str_last_of(host, last_tld-1, '.');
634 if(!sec_last_tld) {
635 /* If the '.' is at the beginning of the host there
636 * has to be at least 3 characters in the TLD for it
637 * to be valid.
638 * Ex: .com -> .com as the domain name.
639 * .co -> has no domain name.
641 if(last_tld-host == 0) {
642 if(end-(last_tld-1) < 3)
643 return;
644 } else if(last_tld-host == 3) {
645 DWORD i;
647 /* If there's three characters in front of last_tld and
648 * they are on the list of recognized TLDs, then this
649 * host doesn't have a domain (since the host only contains
650 * a TLD name.
651 * Ex: edu.uk -> has no domain name.
652 * foo.uk -> foo.uk as the domain name.
654 for(i = 0; i < sizeof(recognized_tlds)/sizeof(recognized_tlds[0]); ++i) {
655 if(!StrCmpNIW(host, recognized_tlds[i].tld_name, 3))
656 return;
658 } else if(last_tld-host < 3)
659 /* Anything less than 3 characters is considered part
660 * of the TLD name.
661 * Ex: ak.uk -> Has no domain name.
663 return;
665 /* Otherwise the domain name is the whole host name. */
666 *domain_start = 0;
667 } else if(end+1-last_tld > 3) {
668 /* If the last_tld has more than 3 characters, then it's automatically
669 * considered the TLD of the domain name.
670 * Ex: www.winehq.org.uk.test -> uk.test as the domain name.
672 *domain_start = (sec_last_tld+1)-host;
673 } else if(last_tld - (sec_last_tld+1) < 4) {
674 DWORD i;
675 /* If the sec_last_tld is 3 characters long it HAS to be on the list of
676 * recognized to still be considered part of the TLD name, otherwise
677 * its considered the domain name.
678 * Ex: www.google.com.uk -> google.com.uk as the domain name.
679 * www.google.foo.uk -> foo.uk as the domain name.
681 if(last_tld - (sec_last_tld+1) == 3) {
682 for(i = 0; i < sizeof(recognized_tlds)/sizeof(recognized_tlds[0]); ++i) {
683 if(!StrCmpNIW(sec_last_tld+1, recognized_tlds[i].tld_name, 3)) {
684 const WCHAR *domain = str_last_of(host, sec_last_tld-1, '.');
686 if(!domain)
687 *domain_start = 0;
688 else
689 *domain_start = (domain+1) - host;
690 TRACE("Found domain name %s\n", debugstr_wn(host+*domain_start,
691 (host+host_len)-(host+*domain_start)));
692 return;
696 *domain_start = (sec_last_tld+1)-host;
697 } else {
698 /* Since the sec_last_tld is less than 3 characters it's considered
699 * part of the TLD.
700 * Ex: www.google.fo.uk -> google.fo.uk as the domain name.
702 const WCHAR *domain = str_last_of(host, sec_last_tld-1, '.');
704 if(!domain)
705 *domain_start = 0;
706 else
707 *domain_start = (domain+1) - host;
709 } else {
710 /* The second to last TLD has more than 3 characters making it
711 * the domain name.
712 * Ex: www.google.test.us -> test.us as the domain name.
714 *domain_start = (sec_last_tld+1)-host;
717 TRACE("Found domain name %s\n", debugstr_wn(host+*domain_start,
718 (host+host_len)-(host+*domain_start)));
721 /* Removes the dot segments from a hierarchical URIs path component. This
722 * function performs the removal in place.
724 * This is a modified version of Qt's QUrl function "removeDotsFromPath".
726 * This function returns the new length of the path string.
728 static DWORD remove_dot_segments(WCHAR *path, DWORD path_len) {
729 WCHAR *out = path;
730 const WCHAR *in = out;
731 const WCHAR *end = out + path_len;
732 DWORD len;
734 while(in < end) {
735 /* A. if the input buffer begins with a prefix of "/./" or "/.",
736 * where "." is a complete path segment, then replace that
737 * prefix with "/" in the input buffer; otherwise,
739 if(in <= end - 3 && in[0] == '/' && in[1] == '.' && in[2] == '/') {
740 in += 2;
741 continue;
742 } else if(in == end - 2 && in[0] == '/' && in[1] == '.') {
743 *out++ = '/';
744 in += 2;
745 break;
748 /* B. if the input buffer begins with a prefix of "/../" or "/..",
749 * where ".." is a complete path segment, then replace that
750 * prefix with "/" in the input buffer and remove the last
751 * segment and its preceding "/" (if any) from the output
752 * buffer; otherwise,
754 if(in <= end - 4 && in[0] == '/' && in[1] == '.' && in[2] == '.' && in[3] == '/') {
755 while(out > path && *(--out) != '/');
757 in += 3;
758 continue;
759 } else if(in == end - 3 && in[0] == '/' && in[1] == '.' && in[2] == '.') {
760 while(out > path && *(--out) != '/');
762 if(*out == '/')
763 ++out;
765 in += 3;
766 break;
769 /* C. move the first path segment in the input buffer to the end of
770 * the output buffer, including the initial "/" character (if
771 * any) and any subsequent characters up to, but not including,
772 * the next "/" character or the end of the input buffer.
774 *out++ = *in++;
775 while(in < end && *in != '/')
776 *out++ = *in++;
779 len = out - path;
780 TRACE("(%p %d): Path after dot segments removed %s len=%d\n", path, path_len,
781 debugstr_wn(path, len), len);
782 return len;
785 /* Attempts to find the file extension in a given path. */
786 static INT find_file_extension(const WCHAR *path, DWORD path_len) {
787 const WCHAR *end;
789 for(end = path+path_len-1; end >= path && *end != '/' && *end != '\\'; --end) {
790 if(*end == '.')
791 return end-path;
794 return -1;
797 /* Computes the location where the elision should occur in the IPv6
798 * address using the numerical values of each component stored in
799 * 'values'. If the address shouldn't contain an elision then 'index'
800 * is assigned -1 as it's value. Otherwise 'index' will contain the
801 * starting index (into values) where the elision should be, and 'count'
802 * will contain the number of cells the elision covers.
804 * NOTES:
805 * Windows will expand an elision if the elision only represents 1 h16
806 * component of the URI.
808 * Ex: [1::2:3:4:5:6:7] -> [1:0:2:3:4:5:6:7]
810 * If the IPv6 address contains an IPv4 address, the IPv4 address is also
811 * considered for being included as part of an elision if all it's components
812 * are zeros.
814 * Ex: [1:2:3:4:5:6:0.0.0.0] -> [1:2:3:4:5:6::]
816 static void compute_elision_location(const ipv6_address *address, const USHORT values[8],
817 INT *index, DWORD *count) {
818 DWORD i, max_len, cur_len;
819 INT max_index, cur_index;
821 max_len = cur_len = 0;
822 max_index = cur_index = -1;
823 for(i = 0; i < 8; ++i) {
824 BOOL check_ipv4 = (address->ipv4 && i == 6);
825 BOOL is_end = (check_ipv4 || i == 7);
827 if(check_ipv4) {
828 /* Check if the IPv4 address contains only zeros. */
829 if(values[i] == 0 && values[i+1] == 0) {
830 if(cur_index == -1)
831 cur_index = i;
833 cur_len += 2;
834 ++i;
836 } else if(values[i] == 0) {
837 if(cur_index == -1)
838 cur_index = i;
840 ++cur_len;
843 if(is_end || values[i] != 0) {
844 /* We only consider it for an elision if it's
845 * more than 1 component long.
847 if(cur_len > 1 && cur_len > max_len) {
848 /* Found the new elision location. */
849 max_len = cur_len;
850 max_index = cur_index;
853 /* Reset the current range for the next range of zeros. */
854 cur_index = -1;
855 cur_len = 0;
859 *index = max_index;
860 *count = max_len;
863 /* Removes all the leading and trailing white spaces or
864 * control characters from the URI and removes all control
865 * characters inside of the URI string.
867 static BSTR pre_process_uri(LPCWSTR uri) {
868 BSTR ret;
869 DWORD len;
870 const WCHAR *start, *end;
871 WCHAR *buf, *ptr;
873 len = lstrlenW(uri);
875 start = uri;
876 /* Skip leading controls and whitespace. */
877 while(iscntrlW(*start) || isspaceW(*start)) ++start;
879 end = uri+len-1;
880 if(start == end)
881 /* URI consisted only of control/whitespace. */
882 ret = SysAllocStringLen(NULL, 0);
883 else {
884 while(iscntrlW(*end) || isspaceW(*end)) --end;
886 buf = heap_alloc(((end+1)-start)*sizeof(WCHAR));
887 if(!buf)
888 return NULL;
890 for(ptr = buf; start < end+1; ++start) {
891 if(!iscntrlW(*start))
892 *ptr++ = *start;
895 ret = SysAllocStringLen(buf, ptr-buf);
896 heap_free(buf);
899 return ret;
902 /* Converts the specified IPv4 address into an uint value.
904 * This function assumes that the IPv4 address has already been validated.
906 static UINT ipv4toui(const WCHAR *ip, DWORD len) {
907 UINT ret = 0;
908 DWORD comp_value = 0;
909 const WCHAR *ptr;
911 for(ptr = ip; ptr < ip+len; ++ptr) {
912 if(*ptr == '.') {
913 ret <<= 8;
914 ret += comp_value;
915 comp_value = 0;
916 } else
917 comp_value = comp_value*10 + (*ptr-'0');
920 ret <<= 8;
921 ret += comp_value;
923 return ret;
926 /* Converts an IPv4 address in numerical form into it's fully qualified
927 * string form. This function returns the number of characters written
928 * to 'dest'. If 'dest' is NULL this function will return the number of
929 * characters that would have been written.
931 * It's up to the caller to ensure there's enough space in 'dest' for the
932 * address.
934 static DWORD ui2ipv4(WCHAR *dest, UINT address) {
935 static const WCHAR formatW[] =
936 {'%','u','.','%','u','.','%','u','.','%','u',0};
937 DWORD ret = 0;
938 UCHAR digits[4];
940 digits[0] = (address >> 24) & 0xff;
941 digits[1] = (address >> 16) & 0xff;
942 digits[2] = (address >> 8) & 0xff;
943 digits[3] = address & 0xff;
945 if(!dest) {
946 WCHAR tmp[16];
947 ret = sprintfW(tmp, formatW, digits[0], digits[1], digits[2], digits[3]);
948 } else
949 ret = sprintfW(dest, formatW, digits[0], digits[1], digits[2], digits[3]);
951 return ret;
954 static DWORD ui2str(WCHAR *dest, UINT value) {
955 static const WCHAR formatW[] = {'%','u',0};
956 DWORD ret = 0;
958 if(!dest) {
959 WCHAR tmp[11];
960 ret = sprintfW(tmp, formatW, value);
961 } else
962 ret = sprintfW(dest, formatW, value);
964 return ret;
967 /* Converts an h16 component (from an IPv6 address) into it's
968 * numerical value.
970 * This function assumes that the h16 component has already been validated.
972 static USHORT h16tous(h16 component) {
973 DWORD i;
974 USHORT ret = 0;
976 for(i = 0; i < component.len; ++i) {
977 ret <<= 4;
978 ret += hex_to_int(component.str[i]);
981 return ret;
984 /* Converts an IPv6 address into it's 128 bits (16 bytes) numerical value.
986 * This function assumes that the ipv6_address has already been validated.
988 static BOOL ipv6_to_number(const ipv6_address *address, USHORT number[8]) {
989 DWORD i, cur_component = 0;
990 BOOL already_passed_elision = FALSE;
992 for(i = 0; i < address->h16_count; ++i) {
993 if(address->elision) {
994 if(address->components[i].str > address->elision && !already_passed_elision) {
995 /* Means we just passed the elision and need to add it's values to
996 * 'number' before we do anything else.
998 DWORD j = 0;
999 for(j = 0; j < address->elision_size; j+=2)
1000 number[cur_component++] = 0;
1002 already_passed_elision = TRUE;
1006 number[cur_component++] = h16tous(address->components[i]);
1009 /* Case when the elision appears after the h16 components. */
1010 if(!already_passed_elision && address->elision) {
1011 for(i = 0; i < address->elision_size; i+=2)
1012 number[cur_component++] = 0;
1013 already_passed_elision = TRUE;
1016 if(address->ipv4) {
1017 UINT value = ipv4toui(address->ipv4, address->ipv4_len);
1019 if(cur_component != 6) {
1020 ERR("(%p %p): Failed sanity check with %d\n", address, number, cur_component);
1021 return FALSE;
1024 number[cur_component++] = (value >> 16) & 0xffff;
1025 number[cur_component] = value & 0xffff;
1028 return TRUE;
1031 /* Checks if the characters pointed to by 'ptr' are
1032 * a percent encoded data octet.
1034 * pct-encoded = "%" HEXDIG HEXDIG
1036 static BOOL check_pct_encoded(const WCHAR **ptr) {
1037 const WCHAR *start = *ptr;
1039 if(**ptr != '%')
1040 return FALSE;
1042 ++(*ptr);
1043 if(!is_hexdigit(**ptr)) {
1044 *ptr = start;
1045 return FALSE;
1048 ++(*ptr);
1049 if(!is_hexdigit(**ptr)) {
1050 *ptr = start;
1051 return FALSE;
1054 ++(*ptr);
1055 return TRUE;
1058 /* dec-octet = DIGIT ; 0-9
1059 * / %x31-39 DIGIT ; 10-99
1060 * / "1" 2DIGIT ; 100-199
1061 * / "2" %x30-34 DIGIT ; 200-249
1062 * / "25" %x30-35 ; 250-255
1064 static BOOL check_dec_octet(const WCHAR **ptr) {
1065 const WCHAR *c1, *c2, *c3;
1067 c1 = *ptr;
1068 /* A dec-octet must be at least 1 digit long. */
1069 if(*c1 < '0' || *c1 > '9')
1070 return FALSE;
1072 ++(*ptr);
1074 c2 = *ptr;
1075 /* Since the 1 digit requirment was meet, it doesn't
1076 * matter if this is a DIGIT value, it's considered a
1077 * dec-octet.
1079 if(*c2 < '0' || *c2 > '9')
1080 return TRUE;
1082 ++(*ptr);
1084 c3 = *ptr;
1085 /* Same explanation as above. */
1086 if(*c3 < '0' || *c3 > '9')
1087 return TRUE;
1089 /* Anything > 255 isn't a valid IP dec-octet. */
1090 if(*c1 >= '2' && *c2 >= '5' && *c3 >= '5') {
1091 *ptr = c1;
1092 return FALSE;
1095 ++(*ptr);
1096 return TRUE;
1099 /* Checks if there is an implicit IPv4 address in the host component of the URI.
1100 * The max value of an implicit IPv4 address is UINT_MAX.
1102 * Ex:
1103 * "234567" would be considered an implicit IPv4 address.
1105 static BOOL check_implicit_ipv4(const WCHAR **ptr, UINT *val) {
1106 const WCHAR *start = *ptr;
1107 ULONGLONG ret = 0;
1108 *val = 0;
1110 while(is_num(**ptr)) {
1111 ret = ret*10 + (**ptr - '0');
1113 if(ret > UINT_MAX) {
1114 *ptr = start;
1115 return FALSE;
1117 ++(*ptr);
1120 if(*ptr == start)
1121 return FALSE;
1123 *val = ret;
1124 return TRUE;
1127 /* Checks if the string contains an IPv4 address.
1129 * This function has a strict mode or a non-strict mode of operation
1130 * When 'strict' is set to FALSE this function will return TRUE if
1131 * the string contains at least 'dec-octet "." dec-octet' since partial
1132 * IPv4 addresses will be normalized out into full IPv4 addresses. When
1133 * 'strict' is set this function expects there to be a full IPv4 address.
1135 * IPv4address = dec-octet "." dec-octet "." dec-octet "." dec-octet
1137 static BOOL check_ipv4address(const WCHAR **ptr, BOOL strict) {
1138 const WCHAR *start = *ptr;
1140 if(!check_dec_octet(ptr)) {
1141 *ptr = start;
1142 return FALSE;
1145 if(**ptr != '.') {
1146 *ptr = start;
1147 return FALSE;
1150 ++(*ptr);
1151 if(!check_dec_octet(ptr)) {
1152 *ptr = start;
1153 return FALSE;
1156 if(**ptr != '.') {
1157 if(strict) {
1158 *ptr = start;
1159 return FALSE;
1160 } else
1161 return TRUE;
1164 ++(*ptr);
1165 if(!check_dec_octet(ptr)) {
1166 *ptr = start;
1167 return FALSE;
1170 if(**ptr != '.') {
1171 if(strict) {
1172 *ptr = start;
1173 return FALSE;
1174 } else
1175 return TRUE;
1178 ++(*ptr);
1179 if(!check_dec_octet(ptr)) {
1180 *ptr = start;
1181 return FALSE;
1184 /* Found a four digit ip address. */
1185 return TRUE;
1187 /* Tries to parse the scheme name of the URI.
1189 * scheme = ALPHA *(ALPHA | NUM | '+' | '-' | '.') as defined by RFC 3896.
1190 * NOTE: Windows accepts a number as the first character of a scheme.
1192 static BOOL parse_scheme_name(const WCHAR **ptr, parse_data *data, DWORD extras) {
1193 const WCHAR *start = *ptr;
1195 data->scheme = NULL;
1196 data->scheme_len = 0;
1198 while(**ptr) {
1199 if(**ptr == '*' && *ptr == start) {
1200 /* Might have found a wildcard scheme. If it is the next
1201 * char has to be a ':' for it to be a valid URI
1203 ++(*ptr);
1204 break;
1205 } else if(!is_num(**ptr) && !is_alpha(**ptr) && **ptr != '+' &&
1206 **ptr != '-' && **ptr != '.')
1207 break;
1209 (*ptr)++;
1212 if(*ptr == start)
1213 return FALSE;
1215 /* Schemes must end with a ':' */
1216 if(**ptr != ':' && !((extras & ALLOW_NULL_TERM_SCHEME) && !**ptr)) {
1217 *ptr = start;
1218 return FALSE;
1221 data->scheme = start;
1222 data->scheme_len = *ptr - start;
1224 ++(*ptr);
1225 return TRUE;
1228 /* Tries to deduce the corresponding URL_SCHEME for the given URI. Stores
1229 * the deduced URL_SCHEME in data->scheme_type.
1231 static BOOL parse_scheme_type(parse_data *data) {
1232 /* If there's scheme data then see if it's a recognized scheme. */
1233 if(data->scheme && data->scheme_len) {
1234 DWORD i;
1236 for(i = 0; i < sizeof(recognized_schemes)/sizeof(recognized_schemes[0]); ++i) {
1237 if(lstrlenW(recognized_schemes[i].scheme_name) == data->scheme_len) {
1238 /* Has to be a case insensitive compare. */
1239 if(!StrCmpNIW(recognized_schemes[i].scheme_name, data->scheme, data->scheme_len)) {
1240 data->scheme_type = recognized_schemes[i].scheme;
1241 return TRUE;
1246 /* If we get here it means it's not a recognized scheme. */
1247 data->scheme_type = URL_SCHEME_UNKNOWN;
1248 return TRUE;
1249 } else if(data->is_relative) {
1250 /* Relative URI's have no scheme. */
1251 data->scheme_type = URL_SCHEME_UNKNOWN;
1252 return TRUE;
1253 } else {
1254 /* Should never reach here! what happened... */
1255 FIXME("(%p): Unable to determine scheme type for URI %s\n", data, debugstr_w(data->uri));
1256 return FALSE;
1260 /* Tries to parse (or deduce) the scheme_name of a URI. If it can't
1261 * parse a scheme from the URI it will try to deduce the scheme_name and scheme_type
1262 * using the flags specified in 'flags' (if any). Flags that affect how this function
1263 * operates are the Uri_CREATE_ALLOW_* flags.
1265 * All parsed/deduced information will be stored in 'data' when the function returns.
1267 * Returns TRUE if it was able to successfully parse the information.
1269 static BOOL parse_scheme(const WCHAR **ptr, parse_data *data, DWORD flags, DWORD extras) {
1270 static const WCHAR fileW[] = {'f','i','l','e',0};
1271 static const WCHAR wildcardW[] = {'*',0};
1273 /* First check to see if the uri could implicitly be a file path. */
1274 if(is_implicit_file_path(*ptr)) {
1275 if(flags & Uri_CREATE_ALLOW_IMPLICIT_FILE_SCHEME) {
1276 data->scheme = fileW;
1277 data->scheme_len = lstrlenW(fileW);
1278 data->has_implicit_scheme = TRUE;
1280 TRACE("(%p %p %x): URI is an implicit file path.\n", ptr, data, flags);
1281 } else {
1282 /* Window's does not consider anything that can implicitly be a file
1283 * path to be a valid URI if the ALLOW_IMPLICIT_FILE_SCHEME flag is not set...
1285 TRACE("(%p %p %x): URI is implicitly a file path, but, the ALLOW_IMPLICIT_FILE_SCHEME flag wasn't set.\n",
1286 ptr, data, flags);
1287 return FALSE;
1289 } else if(!parse_scheme_name(ptr, data, extras)) {
1290 /* No Scheme was found, this means it could be:
1291 * a) an implicit Wildcard scheme
1292 * b) a relative URI
1293 * c) a invalid URI.
1295 if(flags & Uri_CREATE_ALLOW_IMPLICIT_WILDCARD_SCHEME) {
1296 data->scheme = wildcardW;
1297 data->scheme_len = lstrlenW(wildcardW);
1298 data->has_implicit_scheme = TRUE;
1300 TRACE("(%p %p %x): URI is an implicit wildcard scheme.\n", ptr, data, flags);
1301 } else if (flags & Uri_CREATE_ALLOW_RELATIVE) {
1302 data->is_relative = TRUE;
1303 TRACE("(%p %p %x): URI is relative.\n", ptr, data, flags);
1304 } else {
1305 TRACE("(%p %p %x): Malformed URI found. Unable to deduce scheme name.\n", ptr, data, flags);
1306 return FALSE;
1310 if(!data->is_relative)
1311 TRACE("(%p %p %x): Found scheme=%s scheme_len=%d\n", ptr, data, flags,
1312 debugstr_wn(data->scheme, data->scheme_len), data->scheme_len);
1314 if(!parse_scheme_type(data))
1315 return FALSE;
1317 TRACE("(%p %p %x): Assigned %d as the URL_SCHEME.\n", ptr, data, flags, data->scheme_type);
1318 return TRUE;
1321 static BOOL parse_username(const WCHAR **ptr, parse_data *data, DWORD flags, DWORD extras) {
1322 data->username = *ptr;
1324 while(**ptr != ':' && **ptr != '@') {
1325 if(**ptr == '%') {
1326 if(!check_pct_encoded(ptr)) {
1327 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
1328 *ptr = data->username;
1329 data->username = NULL;
1330 return FALSE;
1332 } else
1333 continue;
1334 } else if(extras & ALLOW_NULL_TERM_USER_NAME && !**ptr)
1335 break;
1336 else if(is_auth_delim(**ptr, data->scheme_type != URL_SCHEME_UNKNOWN)) {
1337 *ptr = data->username;
1338 data->username = NULL;
1339 return FALSE;
1342 ++(*ptr);
1345 data->username_len = *ptr - data->username;
1346 return TRUE;
1349 static BOOL parse_password(const WCHAR **ptr, parse_data *data, DWORD flags, DWORD extras) {
1350 data->password = *ptr;
1352 while(**ptr != '@') {
1353 if(**ptr == '%') {
1354 if(!check_pct_encoded(ptr)) {
1355 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
1356 *ptr = data->password;
1357 data->password = NULL;
1358 return FALSE;
1360 } else
1361 continue;
1362 } else if(extras & ALLOW_NULL_TERM_PASSWORD && !**ptr)
1363 break;
1364 else if(is_auth_delim(**ptr, data->scheme_type != URL_SCHEME_UNKNOWN)) {
1365 *ptr = data->password;
1366 data->password = NULL;
1367 return FALSE;
1370 ++(*ptr);
1373 data->password_len = *ptr - data->password;
1374 return TRUE;
1377 /* Parses the userinfo part of the URI (if it exists). The userinfo field of
1378 * a URI can consist of "username:password@", or just "username@".
1380 * RFC def:
1381 * userinfo = *( unreserved / pct-encoded / sub-delims / ":" )
1383 * NOTES:
1384 * 1) If there is more than one ':' in the userinfo part of the URI Windows
1385 * uses the first occurrence of ':' to delimit the username and password
1386 * components.
1388 * ex:
1389 * ftp://user:pass:word@winehq.org
1391 * Would yield, "user" as the username and "pass:word" as the password.
1393 * 2) Windows allows any character to appear in the "userinfo" part of
1394 * a URI, as long as it's not an authority delimeter character set.
1396 static void parse_userinfo(const WCHAR **ptr, parse_data *data, DWORD flags) {
1397 const WCHAR *start = *ptr;
1399 if(!parse_username(ptr, data, flags, 0)) {
1400 TRACE("(%p %p %x): URI contained no userinfo.\n", ptr, data, flags);
1401 return;
1404 if(**ptr == ':') {
1405 ++(*ptr);
1406 if(!parse_password(ptr, data, flags, 0)) {
1407 *ptr = start;
1408 data->username = NULL;
1409 data->username_len = 0;
1410 TRACE("(%p %p %x): URI contained no userinfo.\n", ptr, data, flags);
1411 return;
1415 if(**ptr != '@') {
1416 *ptr = start;
1417 data->username = NULL;
1418 data->username_len = 0;
1419 data->password = NULL;
1420 data->password_len = 0;
1422 TRACE("(%p %p %x): URI contained no userinfo.\n", ptr, data, flags);
1423 return;
1426 if(data->username)
1427 TRACE("(%p %p %x): Found username %s len=%d.\n", ptr, data, flags,
1428 debugstr_wn(data->username, data->username_len), data->username_len);
1430 if(data->password)
1431 TRACE("(%p %p %x): Found password %s len=%d.\n", ptr, data, flags,
1432 debugstr_wn(data->password, data->password_len), data->password_len);
1434 ++(*ptr);
1437 /* Attempts to parse a port from the URI.
1439 * NOTES:
1440 * Windows seems to have a cap on what the maximum value
1441 * for a port can be. The max value is USHORT_MAX.
1443 * port = *DIGIT
1445 static BOOL parse_port(const WCHAR **ptr, parse_data *data, DWORD flags) {
1446 UINT port = 0;
1447 data->port = *ptr;
1449 while(!is_auth_delim(**ptr, data->scheme_type != URL_SCHEME_UNKNOWN)) {
1450 if(!is_num(**ptr)) {
1451 *ptr = data->port;
1452 data->port = NULL;
1453 return FALSE;
1456 port = port*10 + (**ptr-'0');
1458 if(port > USHORT_MAX) {
1459 *ptr = data->port;
1460 data->port = NULL;
1461 return FALSE;
1464 ++(*ptr);
1467 data->has_port = TRUE;
1468 data->port_value = port;
1469 data->port_len = *ptr - data->port;
1471 TRACE("(%p %p %x): Found port %s len=%d value=%u\n", ptr, data, flags,
1472 debugstr_wn(data->port, data->port_len), data->port_len, data->port_value);
1473 return TRUE;
1476 /* Attempts to parse a IPv4 address from the URI.
1478 * NOTES:
1479 * Window's normalizes IPv4 addresses, This means there's three
1480 * possibilities for the URI to contain an IPv4 address.
1481 * 1) A well formed address (ex. 192.2.2.2).
1482 * 2) A partially formed address. For example "192.0" would
1483 * normalize to "192.0.0.0" during canonicalization.
1484 * 3) An implicit IPv4 address. For example "256" would
1485 * normalize to "0.0.1.0" during canonicalization. Also
1486 * note that the maximum value for an implicit IP address
1487 * is UINT_MAX, if the value in the URI exceeds this then
1488 * it is not considered an IPv4 address.
1490 static BOOL parse_ipv4address(const WCHAR **ptr, parse_data *data, DWORD flags) {
1491 const BOOL is_unknown = data->scheme_type == URL_SCHEME_UNKNOWN;
1492 data->host = *ptr;
1494 if(!check_ipv4address(ptr, FALSE)) {
1495 if(!check_implicit_ipv4(ptr, &data->implicit_ipv4)) {
1496 TRACE("(%p %p %x): URI didn't contain anything looking like an IPv4 address.\n",
1497 ptr, data, flags);
1498 *ptr = data->host;
1499 data->host = NULL;
1500 return FALSE;
1501 } else
1502 data->has_implicit_ip = TRUE;
1505 /* Check if what we found is the only part of the host name (if it isn't
1506 * we don't have an IPv4 address).
1508 if(**ptr == ':') {
1509 ++(*ptr);
1510 if(!parse_port(ptr, data, flags)) {
1511 *ptr = data->host;
1512 data->host = NULL;
1513 return FALSE;
1515 } else if(!is_auth_delim(**ptr, !is_unknown)) {
1516 /* Found more data which belongs the host, so this isn't an IPv4. */
1517 *ptr = data->host;
1518 data->host = NULL;
1519 data->has_implicit_ip = FALSE;
1520 return FALSE;
1523 data->host_len = *ptr - data->host;
1524 data->host_type = Uri_HOST_IPV4;
1526 TRACE("(%p %p %x): IPv4 address found. host=%s host_len=%d host_type=%d\n",
1527 ptr, data, flags, debugstr_wn(data->host, data->host_len),
1528 data->host_len, data->host_type);
1529 return TRUE;
1532 /* Attempts to parse the reg-name from the URI.
1534 * Because of the way Windows handles ':' this function also
1535 * handles parsing the port.
1537 * reg-name = *( unreserved / pct-encoded / sub-delims )
1539 * NOTE:
1540 * Windows allows everything, but, the characters in "auth_delims" and ':'
1541 * to appear in a reg-name, unless it's an unknown scheme type then ':' is
1542 * allowed to appear (even if a valid port isn't after it).
1544 * Windows doesn't like host names which start with '[' and end with ']'
1545 * and don't contain a valid IP literal address in between them.
1547 * On Windows if an '[' is encountered in the host name the ':' no longer
1548 * counts as a delimiter until you reach the next ']' or an "authority delimeter".
1550 * A reg-name CAN be empty.
1552 static BOOL parse_reg_name(const WCHAR **ptr, parse_data *data, DWORD flags, DWORD extras) {
1553 const BOOL has_start_bracket = **ptr == '[';
1554 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
1555 const BOOL is_res = data->scheme_type == URL_SCHEME_RES;
1556 BOOL inside_brackets = has_start_bracket;
1558 /* res URIs don't have ports. */
1559 BOOL ignore_col = (extras & IGNORE_PORT_DELIMITER) || is_res;
1561 /* We have to be careful with file schemes. */
1562 if(data->scheme_type == URL_SCHEME_FILE) {
1563 /* This is because an implicit file scheme could be "C:\\test" and it
1564 * would trick this function into thinking the host is "C", when after
1565 * canonicalization the host would end up being an empty string. A drive
1566 * path can also have a '|' instead of a ':' after the drive letter.
1568 if(is_drive_path(*ptr)) {
1569 /* Regular old drive paths don't have a host type (or host name). */
1570 data->host_type = Uri_HOST_UNKNOWN;
1571 data->host = *ptr;
1572 data->host_len = 0;
1573 return TRUE;
1574 } else if(is_unc_path(*ptr))
1575 /* Skip past the "\\" of a UNC path. */
1576 *ptr += 2;
1579 data->host = *ptr;
1581 /* For res URIs, everything before the first '/' is
1582 * considered the host.
1584 while((!is_res && !is_auth_delim(**ptr, known_scheme)) ||
1585 (is_res && **ptr && **ptr != '/')) {
1586 if(**ptr == ':' && !ignore_col) {
1587 /* We can ignore ':' if were inside brackets.*/
1588 if(!inside_brackets) {
1589 const WCHAR *tmp = (*ptr)++;
1591 /* Attempt to parse the port. */
1592 if(!parse_port(ptr, data, flags)) {
1593 /* Windows expects there to be a valid port for known scheme types. */
1594 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
1595 *ptr = data->host;
1596 data->host = NULL;
1597 TRACE("(%p %p %x %x): Expected valid port\n", ptr, data, flags, extras);
1598 return FALSE;
1599 } else
1600 /* Windows gives up on trying to parse a port when it
1601 * encounters 1 invalid port.
1603 ignore_col = TRUE;
1604 } else {
1605 data->host_len = tmp - data->host;
1606 break;
1609 } else if(**ptr == '%' && (known_scheme && !is_res)) {
1610 /* Has to be a legit % encoded value. */
1611 if(!check_pct_encoded(ptr)) {
1612 *ptr = data->host;
1613 data->host = NULL;
1614 return FALSE;
1615 } else
1616 continue;
1617 } else if(is_res && is_forbidden_dos_path_char(**ptr)) {
1618 *ptr = data->host;
1619 data->host = NULL;
1620 return FALSE;
1621 } else if(**ptr == ']')
1622 inside_brackets = FALSE;
1623 else if(**ptr == '[')
1624 inside_brackets = TRUE;
1626 ++(*ptr);
1629 if(has_start_bracket) {
1630 /* Make sure the last character of the host wasn't a ']'. */
1631 if(*(*ptr-1) == ']') {
1632 TRACE("(%p %p %x %x): Expected an IP literal inside of the host\n",
1633 ptr, data, flags, extras);
1634 *ptr = data->host;
1635 data->host = NULL;
1636 return FALSE;
1640 /* Don't overwrite our length if we found a port earlier. */
1641 if(!data->port)
1642 data->host_len = *ptr - data->host;
1644 /* If the host is empty, then it's an unknown host type. */
1645 if(data->host_len == 0 || is_res)
1646 data->host_type = Uri_HOST_UNKNOWN;
1647 else
1648 data->host_type = Uri_HOST_DNS;
1650 TRACE("(%p %p %x %x): Parsed reg-name. host=%s len=%d\n", ptr, data, flags, extras,
1651 debugstr_wn(data->host, data->host_len), data->host_len);
1652 return TRUE;
1655 /* Attempts to parse an IPv6 address out of the URI.
1657 * IPv6address = 6( h16 ":" ) ls32
1658 * / "::" 5( h16 ":" ) ls32
1659 * / [ h16 ] "::" 4( h16 ":" ) ls32
1660 * / [ *1( h16 ":" ) h16 ] "::" 3( h16 ":" ) ls32
1661 * / [ *2( h16 ":" ) h16 ] "::" 2( h16 ":" ) ls32
1662 * / [ *3( h16 ":" ) h16 ] "::" h16 ":" ls32
1663 * / [ *4( h16 ":" ) h16 ] "::" ls32
1664 * / [ *5( h16 ":" ) h16 ] "::" h16
1665 * / [ *6( h16 ":" ) h16 ] "::"
1667 * ls32 = ( h16 ":" h16 ) / IPv4address
1668 * ; least-significant 32 bits of address.
1670 * h16 = 1*4HEXDIG
1671 * ; 16 bits of address represented in hexadecimal.
1673 * Modeled after google-url's 'DoParseIPv6' function.
1675 static BOOL parse_ipv6address(const WCHAR **ptr, parse_data *data, DWORD flags) {
1676 const WCHAR *start, *cur_start;
1677 ipv6_address ip;
1679 start = cur_start = *ptr;
1680 memset(&ip, 0, sizeof(ipv6_address));
1682 for(;; ++(*ptr)) {
1683 /* Check if we're on the last character of the host. */
1684 BOOL is_end = (is_auth_delim(**ptr, data->scheme_type != URL_SCHEME_UNKNOWN)
1685 || **ptr == ']');
1687 BOOL is_split = (**ptr == ':');
1688 BOOL is_elision = (is_split && !is_end && *(*ptr+1) == ':');
1690 /* Check if we're at the end of a component, or
1691 * if we're at the end of the IPv6 address.
1693 if(is_split || is_end) {
1694 DWORD cur_len = 0;
1696 cur_len = *ptr - cur_start;
1698 /* h16 can't have a length > 4. */
1699 if(cur_len > 4) {
1700 *ptr = start;
1702 TRACE("(%p %p %x): h16 component to long.\n",
1703 ptr, data, flags);
1704 return FALSE;
1707 if(cur_len == 0) {
1708 /* An h16 component can't have the length of 0 unless
1709 * the elision is at the beginning of the address, or
1710 * at the end of the address.
1712 if(!((*ptr == start && is_elision) ||
1713 (is_end && (*ptr-2) == ip.elision))) {
1714 *ptr = start;
1715 TRACE("(%p %p %x): IPv6 component cannot have a length of 0.\n",
1716 ptr, data, flags);
1717 return FALSE;
1721 if(cur_len > 0) {
1722 /* An IPv6 address can have no more than 8 h16 components. */
1723 if(ip.h16_count >= 8) {
1724 *ptr = start;
1725 TRACE("(%p %p %x): Not a IPv6 address, to many h16 components.\n",
1726 ptr, data, flags);
1727 return FALSE;
1730 ip.components[ip.h16_count].str = cur_start;
1731 ip.components[ip.h16_count].len = cur_len;
1733 TRACE("(%p %p %x): Found h16 component %s, len=%d, h16_count=%d\n",
1734 ptr, data, flags, debugstr_wn(cur_start, cur_len), cur_len,
1735 ip.h16_count);
1736 ++ip.h16_count;
1740 if(is_end)
1741 break;
1743 if(is_elision) {
1744 /* A IPv6 address can only have 1 elision ('::'). */
1745 if(ip.elision) {
1746 *ptr = start;
1748 TRACE("(%p %p %x): IPv6 address cannot have 2 elisions.\n",
1749 ptr, data, flags);
1750 return FALSE;
1753 ip.elision = *ptr;
1754 ++(*ptr);
1757 if(is_split)
1758 cur_start = *ptr+1;
1759 else {
1760 if(!check_ipv4address(ptr, TRUE)) {
1761 if(!is_hexdigit(**ptr)) {
1762 /* Not a valid character for an IPv6 address. */
1763 *ptr = start;
1764 return FALSE;
1766 } else {
1767 /* Found an IPv4 address. */
1768 ip.ipv4 = cur_start;
1769 ip.ipv4_len = *ptr - cur_start;
1771 TRACE("(%p %p %x): Found an attached IPv4 address %s len=%d.\n",
1772 ptr, data, flags, debugstr_wn(ip.ipv4, ip.ipv4_len),
1773 ip.ipv4_len);
1775 /* IPv4 addresses can only appear at the end of a IPv6. */
1776 break;
1781 compute_ipv6_comps_size(&ip);
1783 /* Make sure the IPv6 address adds up to 16 bytes. */
1784 if(ip.components_size + ip.elision_size != 16) {
1785 *ptr = start;
1786 TRACE("(%p %p %x): Invalid IPv6 address, did not add up to 16 bytes.\n",
1787 ptr, data, flags);
1788 return FALSE;
1791 if(ip.elision_size == 2) {
1792 /* For some reason on Windows if an elision that represents
1793 * only 1 h16 component is encountered at the very begin or
1794 * end of an IPv6 address, Windows does not consider it a
1795 * valid IPv6 address.
1797 * Ex: [::2:3:4:5:6:7] is not valid, even though the sum
1798 * of all the components == 128bits.
1800 if(ip.elision < ip.components[0].str ||
1801 ip.elision > ip.components[ip.h16_count-1].str) {
1802 *ptr = start;
1803 TRACE("(%p %p %x): Invalid IPv6 address. Detected elision of 2 bytes at the beginning or end of the address.\n",
1804 ptr, data, flags);
1805 return FALSE;
1809 data->host_type = Uri_HOST_IPV6;
1810 data->has_ipv6 = TRUE;
1811 data->ipv6_address = ip;
1813 TRACE("(%p %p %x): Found valid IPv6 literal %s len=%d\n",
1814 ptr, data, flags, debugstr_wn(start, *ptr-start),
1815 *ptr-start);
1816 return TRUE;
1819 /* IPvFuture = "v" 1*HEXDIG "." 1*( unreserved / sub-delims / ":" ) */
1820 static BOOL parse_ipvfuture(const WCHAR **ptr, parse_data *data, DWORD flags) {
1821 const WCHAR *start = *ptr;
1823 /* IPvFuture has to start with a 'v' or 'V'. */
1824 if(**ptr != 'v' && **ptr != 'V')
1825 return FALSE;
1827 /* Following the v there must be at least 1 hex digit. */
1828 ++(*ptr);
1829 if(!is_hexdigit(**ptr)) {
1830 *ptr = start;
1831 return FALSE;
1834 ++(*ptr);
1835 while(is_hexdigit(**ptr))
1836 ++(*ptr);
1838 /* End of the hexdigit sequence must be a '.' */
1839 if(**ptr != '.') {
1840 *ptr = start;
1841 return FALSE;
1844 ++(*ptr);
1845 if(!is_unreserved(**ptr) && !is_subdelim(**ptr) && **ptr != ':') {
1846 *ptr = start;
1847 return FALSE;
1850 ++(*ptr);
1851 while(is_unreserved(**ptr) || is_subdelim(**ptr) || **ptr == ':')
1852 ++(*ptr);
1854 data->host_type = Uri_HOST_UNKNOWN;
1856 TRACE("(%p %p %x): Parsed IPvFuture address %s len=%d\n", ptr, data, flags,
1857 debugstr_wn(start, *ptr-start), *ptr-start);
1859 return TRUE;
1862 /* IP-literal = "[" ( IPv6address / IPvFuture ) "]" */
1863 static BOOL parse_ip_literal(const WCHAR **ptr, parse_data *data, DWORD flags, DWORD extras) {
1864 data->host = *ptr;
1866 if(**ptr != '[' && !(extras & ALLOW_BRACKETLESS_IP_LITERAL)) {
1867 data->host = NULL;
1868 return FALSE;
1869 } else if(**ptr == '[')
1870 ++(*ptr);
1872 if(!parse_ipv6address(ptr, data, flags)) {
1873 if(extras & SKIP_IP_FUTURE_CHECK || !parse_ipvfuture(ptr, data, flags)) {
1874 *ptr = data->host;
1875 data->host = NULL;
1876 return FALSE;
1880 if(**ptr != ']' && !(extras & ALLOW_BRACKETLESS_IP_LITERAL)) {
1881 *ptr = data->host;
1882 data->host = NULL;
1883 return FALSE;
1884 } else if(!**ptr && extras & ALLOW_BRACKETLESS_IP_LITERAL) {
1885 /* The IP literal didn't contain brackets and was followed by
1886 * a NULL terminator, so no reason to even check the port.
1888 data->host_len = *ptr - data->host;
1889 return TRUE;
1892 ++(*ptr);
1893 if(**ptr == ':') {
1894 ++(*ptr);
1895 /* If a valid port is not found, then let it trickle down to
1896 * parse_reg_name.
1898 if(!parse_port(ptr, data, flags)) {
1899 *ptr = data->host;
1900 data->host = NULL;
1901 return FALSE;
1903 } else
1904 data->host_len = *ptr - data->host;
1906 return TRUE;
1909 /* Parses the host information from the URI.
1911 * host = IP-literal / IPv4address / reg-name
1913 static BOOL parse_host(const WCHAR **ptr, parse_data *data, DWORD flags, DWORD extras) {
1914 if(!parse_ip_literal(ptr, data, flags, extras)) {
1915 if(!parse_ipv4address(ptr, data, flags)) {
1916 if(!parse_reg_name(ptr, data, flags, extras)) {
1917 TRACE("(%p %p %x %x): Malformed URI, Unknown host type.\n",
1918 ptr, data, flags, extras);
1919 return FALSE;
1924 return TRUE;
1927 /* Parses the authority information from the URI.
1929 * authority = [ userinfo "@" ] host [ ":" port ]
1931 static BOOL parse_authority(const WCHAR **ptr, parse_data *data, DWORD flags) {
1932 parse_userinfo(ptr, data, flags);
1934 /* Parsing the port will happen during one of the host parsing
1935 * routines (if the URI has a port).
1937 if(!parse_host(ptr, data, flags, 0))
1938 return FALSE;
1940 return TRUE;
1943 /* Attempts to parse the path information of a hierarchical URI. */
1944 static BOOL parse_path_hierarchical(const WCHAR **ptr, parse_data *data, DWORD flags) {
1945 const WCHAR *start = *ptr;
1946 static const WCHAR slash[] = {'/',0};
1947 const BOOL is_file = data->scheme_type == URL_SCHEME_FILE;
1949 if(is_path_delim(**ptr)) {
1950 if(data->scheme_type == URL_SCHEME_WILDCARD) {
1951 /* Wildcard schemes don't get a '/' attached if their path is
1952 * empty.
1954 data->path = NULL;
1955 data->path_len = 0;
1956 } else if(!(flags & Uri_CREATE_NO_CANONICALIZE)) {
1957 /* If the path component is empty, then a '/' is added. */
1958 data->path = slash;
1959 data->path_len = 1;
1961 } else {
1962 while(!is_path_delim(**ptr)) {
1963 if(**ptr == '%' && data->scheme_type != URL_SCHEME_UNKNOWN && !is_file) {
1964 if(!check_pct_encoded(ptr)) {
1965 *ptr = start;
1966 return FALSE;
1967 } else
1968 continue;
1969 } else if(is_forbidden_dos_path_char(**ptr) && is_file &&
1970 (flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
1971 /* File schemes with USE_DOS_PATH set aren't allowed to have
1972 * a '<' or '>' or '\"' appear in them.
1974 *ptr = start;
1975 return FALSE;
1976 } else if(**ptr == '\\') {
1977 /* Not allowed to have a backslash if NO_CANONICALIZE is set
1978 * and the scheme is known type (but not a file scheme).
1980 if(flags & Uri_CREATE_NO_CANONICALIZE) {
1981 if(data->scheme_type != URL_SCHEME_FILE &&
1982 data->scheme_type != URL_SCHEME_UNKNOWN) {
1983 *ptr = start;
1984 return FALSE;
1989 ++(*ptr);
1992 /* The only time a URI doesn't have a path is when
1993 * the NO_CANONICALIZE flag is set and the raw URI
1994 * didn't contain one.
1996 if(*ptr == start) {
1997 data->path = NULL;
1998 data->path_len = 0;
1999 } else {
2000 data->path = start;
2001 data->path_len = *ptr - start;
2005 if(data->path)
2006 TRACE("(%p %p %x): Parsed path %s len=%d\n", ptr, data, flags,
2007 debugstr_wn(data->path, data->path_len), data->path_len);
2008 else
2009 TRACE("(%p %p %x): The URI contained no path\n", ptr, data, flags);
2011 return TRUE;
2014 /* Parses the path of a opaque URI (much less strict then the parser
2015 * for a hierarchical URI).
2017 * NOTE:
2018 * Windows allows invalid % encoded data to appear in opaque URI paths
2019 * for unknown scheme types.
2021 * File schemes with USE_DOS_PATH set aren't allowed to have '<', '>', or '\"'
2022 * appear in them.
2024 static BOOL parse_path_opaque(const WCHAR **ptr, parse_data *data, DWORD flags) {
2025 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
2026 const BOOL is_file = data->scheme_type == URL_SCHEME_FILE;
2028 data->path = *ptr;
2030 while(!is_path_delim(**ptr)) {
2031 if(**ptr == '%' && known_scheme) {
2032 if(!check_pct_encoded(ptr)) {
2033 *ptr = data->path;
2034 data->path = NULL;
2035 return FALSE;
2036 } else
2037 continue;
2038 } else if(is_forbidden_dos_path_char(**ptr) && is_file &&
2039 (flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
2040 *ptr = data->path;
2041 data->path = NULL;
2042 return FALSE;
2045 ++(*ptr);
2048 data->path_len = *ptr - data->path;
2049 TRACE("(%p %p %x): Parsed opaque URI path %s len=%d\n", ptr, data, flags,
2050 debugstr_wn(data->path, data->path_len), data->path_len);
2051 return TRUE;
2054 /* Determines how the URI should be parsed after the scheme information.
2056 * If the scheme is followed, by "//" then, it is treated as an hierarchical URI
2057 * which then the authority and path information will be parsed out. Otherwise, the
2058 * URI will be treated as an opaque URI which the authority information is not parsed
2059 * out.
2061 * RFC 3896 definition of hier-part:
2063 * hier-part = "//" authority path-abempty
2064 * / path-absolute
2065 * / path-rootless
2066 * / path-empty
2068 * MSDN opaque URI definition:
2069 * scheme ":" path [ "#" fragment ]
2071 * NOTES:
2072 * If the URI is of an unknown scheme type and has a "//" following the scheme then it
2073 * is treated as a hierarchical URI, but, if the CREATE_NO_CRACK_UNKNOWN_SCHEMES flag is
2074 * set then it is considered an opaque URI reguardless of what follows the scheme information
2075 * (per MSDN documentation).
2077 static BOOL parse_hierpart(const WCHAR **ptr, parse_data *data, DWORD flags) {
2078 const WCHAR *start = *ptr;
2080 /* Checks if the authority information needs to be parsed. */
2081 if(is_hierarchical_uri(ptr, data)) {
2082 /* Only treat it as a hierarchical URI if the scheme_type is known or
2083 * the Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES flag is not set.
2085 if(data->scheme_type != URL_SCHEME_UNKNOWN ||
2086 !(flags & Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES)) {
2087 TRACE("(%p %p %x): Treating URI as an hierarchical URI.\n", ptr, data, flags);
2088 data->is_opaque = FALSE;
2090 /* TODO: Handle hierarchical URI's, parse authority then parse the path. */
2091 if(!parse_authority(ptr, data, flags))
2092 return FALSE;
2094 return parse_path_hierarchical(ptr, data, flags);
2095 } else
2096 /* Reset ptr to it's starting position so opaque path parsing
2097 * begins at the correct location.
2099 *ptr = start;
2102 /* If it reaches here, then the URI will be treated as an opaque
2103 * URI.
2106 TRACE("(%p %p %x): Treating URI as an opaque URI.\n", ptr, data, flags);
2108 data->is_opaque = TRUE;
2109 if(!parse_path_opaque(ptr, data, flags))
2110 return FALSE;
2112 return TRUE;
2115 /* Attempts to parse the query string from the URI.
2117 * NOTES:
2118 * If NO_DECODE_EXTRA_INFO flag is set, then invalid percent encoded
2119 * data is allowed appear in the query string. For unknown scheme types
2120 * invalid percent encoded data is allowed to appear reguardless.
2122 static BOOL parse_query(const WCHAR **ptr, parse_data *data, DWORD flags) {
2123 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
2125 if(**ptr != '?') {
2126 TRACE("(%p %p %x): URI didn't contain a query string.\n", ptr, data, flags);
2127 return TRUE;
2130 data->query = *ptr;
2132 ++(*ptr);
2133 while(**ptr && **ptr != '#') {
2134 if(**ptr == '%' && known_scheme &&
2135 !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
2136 if(!check_pct_encoded(ptr)) {
2137 *ptr = data->query;
2138 data->query = NULL;
2139 return FALSE;
2140 } else
2141 continue;
2144 ++(*ptr);
2147 data->query_len = *ptr - data->query;
2149 TRACE("(%p %p %x): Parsed query string %s len=%d\n", ptr, data, flags,
2150 debugstr_wn(data->query, data->query_len), data->query_len);
2151 return TRUE;
2154 /* Attempts to parse the fragment from the URI.
2156 * NOTES:
2157 * If NO_DECODE_EXTRA_INFO flag is set, then invalid percent encoded
2158 * data is allowed appear in the query string. For unknown scheme types
2159 * invalid percent encoded data is allowed to appear reguardless.
2161 static BOOL parse_fragment(const WCHAR **ptr, parse_data *data, DWORD flags) {
2162 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
2164 if(**ptr != '#') {
2165 TRACE("(%p %p %x): URI didn't contain a fragment.\n", ptr, data, flags);
2166 return TRUE;
2169 data->fragment = *ptr;
2171 ++(*ptr);
2172 while(**ptr) {
2173 if(**ptr == '%' && known_scheme &&
2174 !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
2175 if(!check_pct_encoded(ptr)) {
2176 *ptr = data->fragment;
2177 data->fragment = NULL;
2178 return FALSE;
2179 } else
2180 continue;
2183 ++(*ptr);
2186 data->fragment_len = *ptr - data->fragment;
2188 TRACE("(%p %p %x): Parsed fragment %s len=%d\n", ptr, data, flags,
2189 debugstr_wn(data->fragment, data->fragment_len), data->fragment_len);
2190 return TRUE;
2193 /* Parses and validates the components of the specified by data->uri
2194 * and stores the information it parses into 'data'.
2196 * Returns TRUE if it successfully parsed the URI. False otherwise.
2198 static BOOL parse_uri(parse_data *data, DWORD flags) {
2199 const WCHAR *ptr;
2200 const WCHAR **pptr;
2202 ptr = data->uri;
2203 pptr = &ptr;
2205 TRACE("(%p %x): BEGINNING TO PARSE URI %s.\n", data, flags, debugstr_w(data->uri));
2207 if(!parse_scheme(pptr, data, flags, 0))
2208 return FALSE;
2210 if(!parse_hierpart(pptr, data, flags))
2211 return FALSE;
2213 if(!parse_query(pptr, data, flags))
2214 return FALSE;
2216 if(!parse_fragment(pptr, data, flags))
2217 return FALSE;
2219 TRACE("(%p %x): FINISHED PARSING URI.\n", data, flags);
2220 return TRUE;
2223 static BOOL canonicalize_username(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2224 const WCHAR *ptr;
2226 if(!data->username) {
2227 uri->userinfo_start = -1;
2228 return TRUE;
2231 uri->userinfo_start = uri->canon_len;
2232 for(ptr = data->username; ptr < data->username+data->username_len; ++ptr) {
2233 if(*ptr == '%') {
2234 /* Only decode % encoded values for known scheme types. */
2235 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
2236 /* See if the value really needs decoded. */
2237 WCHAR val = decode_pct_val(ptr);
2238 if(is_unreserved(val)) {
2239 if(!computeOnly)
2240 uri->canon_uri[uri->canon_len] = val;
2242 ++uri->canon_len;
2244 /* Move pass the hex characters. */
2245 ptr += 2;
2246 continue;
2249 } else if(!is_reserved(*ptr) && !is_unreserved(*ptr) && *ptr != '\\') {
2250 /* Only percent encode forbidden characters if the NO_ENCODE_FORBIDDEN_CHARACTERS flag
2251 * is NOT set.
2253 if(!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS)) {
2254 if(!computeOnly)
2255 pct_encode_val(*ptr, uri->canon_uri + uri->canon_len);
2257 uri->canon_len += 3;
2258 continue;
2262 if(!computeOnly)
2263 /* Nothing special, so just copy the character over. */
2264 uri->canon_uri[uri->canon_len] = *ptr;
2265 ++uri->canon_len;
2268 return TRUE;
2271 static BOOL canonicalize_password(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2272 const WCHAR *ptr;
2274 if(!data->password) {
2275 uri->userinfo_split = -1;
2276 return TRUE;
2279 if(uri->userinfo_start == -1)
2280 /* Has a password, but, doesn't have a username. */
2281 uri->userinfo_start = uri->canon_len;
2283 uri->userinfo_split = uri->canon_len - uri->userinfo_start;
2285 /* Add the ':' to the userinfo component. */
2286 if(!computeOnly)
2287 uri->canon_uri[uri->canon_len] = ':';
2288 ++uri->canon_len;
2290 for(ptr = data->password; ptr < data->password+data->password_len; ++ptr) {
2291 if(*ptr == '%') {
2292 /* Only decode % encoded values for known scheme types. */
2293 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
2294 /* See if the value really needs decoded. */
2295 WCHAR val = decode_pct_val(ptr);
2296 if(is_unreserved(val)) {
2297 if(!computeOnly)
2298 uri->canon_uri[uri->canon_len] = val;
2300 ++uri->canon_len;
2302 /* Move pass the hex characters. */
2303 ptr += 2;
2304 continue;
2307 } else if(!is_reserved(*ptr) && !is_unreserved(*ptr) && *ptr != '\\') {
2308 /* Only percent encode forbidden characters if the NO_ENCODE_FORBIDDEN_CHARACTERS flag
2309 * is NOT set.
2311 if(!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS)) {
2312 if(!computeOnly)
2313 pct_encode_val(*ptr, uri->canon_uri + uri->canon_len);
2315 uri->canon_len += 3;
2316 continue;
2320 if(!computeOnly)
2321 /* Nothing special, so just copy the character over. */
2322 uri->canon_uri[uri->canon_len] = *ptr;
2323 ++uri->canon_len;
2326 return TRUE;
2329 /* Canonicalizes the userinfo of the URI represented by the parse_data.
2331 * Canonicalization of the userinfo is a simple process. If there are any percent
2332 * encoded characters that fall in the "unreserved" character set, they are decoded
2333 * to their actual value. If a character is not in the "unreserved" or "reserved" sets
2334 * then it is percent encoded. Other than that the characters are copied over without
2335 * change.
2337 static BOOL canonicalize_userinfo(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2338 uri->userinfo_start = uri->userinfo_split = -1;
2339 uri->userinfo_len = 0;
2341 if(!data->username && !data->password)
2342 /* URI doesn't have userinfo, so nothing to do here. */
2343 return TRUE;
2345 if(!canonicalize_username(data, uri, flags, computeOnly))
2346 return FALSE;
2348 if(!canonicalize_password(data, uri, flags, computeOnly))
2349 return FALSE;
2351 uri->userinfo_len = uri->canon_len - uri->userinfo_start;
2352 if(!computeOnly)
2353 TRACE("(%p %p %x %d): Canonicalized userinfo, userinfo_start=%d, userinfo=%s, userinfo_split=%d userinfo_len=%d.\n",
2354 data, uri, flags, computeOnly, uri->userinfo_start, debugstr_wn(uri->canon_uri + uri->userinfo_start, uri->userinfo_len),
2355 uri->userinfo_split, uri->userinfo_len);
2357 /* Now insert the '@' after the userinfo. */
2358 if(!computeOnly)
2359 uri->canon_uri[uri->canon_len] = '@';
2360 ++uri->canon_len;
2362 return TRUE;
2365 /* Attempts to canonicalize a reg_name.
2367 * Things that happen:
2368 * 1) If Uri_CREATE_NO_CANONICALIZE flag is not set, then the reg_name is
2369 * lower cased. Unless it's an unknown scheme type, which case it's
2370 * no lower cased reguardless.
2372 * 2) Unreserved % encoded characters are decoded for known
2373 * scheme types.
2375 * 3) Forbidden characters are % encoded as long as
2376 * Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS flag is not set and
2377 * it isn't an unknown scheme type.
2379 * 4) If it's a file scheme and the host is "localhost" it's removed.
2381 * 5) If it's a file scheme and Uri_CREATE_FILE_USE_DOS_PATH is set,
2382 * then the UNC path characters are added before the host name.
2384 static BOOL canonicalize_reg_name(const parse_data *data, Uri *uri,
2385 DWORD flags, BOOL computeOnly) {
2386 static const WCHAR localhostW[] =
2387 {'l','o','c','a','l','h','o','s','t',0};
2388 const WCHAR *ptr;
2389 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
2391 if(data->scheme_type == URL_SCHEME_FILE &&
2392 data->host_len == lstrlenW(localhostW)) {
2393 if(!StrCmpNIW(data->host, localhostW, data->host_len)) {
2394 uri->host_start = -1;
2395 uri->host_len = 0;
2396 uri->host_type = Uri_HOST_UNKNOWN;
2397 return TRUE;
2401 if(data->scheme_type == URL_SCHEME_FILE && flags & Uri_CREATE_FILE_USE_DOS_PATH) {
2402 if(!computeOnly) {
2403 uri->canon_uri[uri->canon_len] = '\\';
2404 uri->canon_uri[uri->canon_len+1] = '\\';
2406 uri->canon_len += 2;
2407 uri->authority_start = uri->canon_len;
2410 uri->host_start = uri->canon_len;
2412 for(ptr = data->host; ptr < data->host+data->host_len; ++ptr) {
2413 if(*ptr == '%' && known_scheme) {
2414 WCHAR val = decode_pct_val(ptr);
2415 if(is_unreserved(val)) {
2416 /* If NO_CANONICALZE is not set, then windows lower cases the
2417 * decoded value.
2419 if(!(flags & Uri_CREATE_NO_CANONICALIZE) && isupperW(val)) {
2420 if(!computeOnly)
2421 uri->canon_uri[uri->canon_len] = tolowerW(val);
2422 } else {
2423 if(!computeOnly)
2424 uri->canon_uri[uri->canon_len] = val;
2426 ++uri->canon_len;
2428 /* Skip past the % encoded character. */
2429 ptr += 2;
2430 continue;
2431 } else {
2432 /* Just copy the % over. */
2433 if(!computeOnly)
2434 uri->canon_uri[uri->canon_len] = *ptr;
2435 ++uri->canon_len;
2437 } else if(*ptr == '\\') {
2438 /* Only unknown scheme types could have made it here with a '\\' in the host name. */
2439 if(!computeOnly)
2440 uri->canon_uri[uri->canon_len] = *ptr;
2441 ++uri->canon_len;
2442 } else if(!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS) &&
2443 !is_unreserved(*ptr) && !is_reserved(*ptr) && known_scheme) {
2444 if(!computeOnly) {
2445 pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
2447 /* The percent encoded value gets lower cased also. */
2448 if(!(flags & Uri_CREATE_NO_CANONICALIZE)) {
2449 uri->canon_uri[uri->canon_len+1] = tolowerW(uri->canon_uri[uri->canon_len+1]);
2450 uri->canon_uri[uri->canon_len+2] = tolowerW(uri->canon_uri[uri->canon_len+2]);
2454 uri->canon_len += 3;
2455 } else {
2456 if(!computeOnly) {
2457 if(!(flags & Uri_CREATE_NO_CANONICALIZE) && known_scheme)
2458 uri->canon_uri[uri->canon_len] = tolowerW(*ptr);
2459 else
2460 uri->canon_uri[uri->canon_len] = *ptr;
2463 ++uri->canon_len;
2467 uri->host_len = uri->canon_len - uri->host_start;
2469 if(!computeOnly)
2470 TRACE("(%p %p %x %d): Canonicalize reg_name=%s len=%d\n", data, uri, flags,
2471 computeOnly, debugstr_wn(uri->canon_uri+uri->host_start, uri->host_len),
2472 uri->host_len);
2474 if(!computeOnly)
2475 find_domain_name(uri->canon_uri+uri->host_start, uri->host_len,
2476 &(uri->domain_offset));
2478 return TRUE;
2481 /* Attempts to canonicalize an implicit IPv4 address. */
2482 static BOOL canonicalize_implicit_ipv4address(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2483 uri->host_start = uri->canon_len;
2485 TRACE("%u\n", data->implicit_ipv4);
2486 /* For unknown scheme types Window's doesn't convert
2487 * the value into an IP address, but, it still considers
2488 * it an IPv4 address.
2490 if(data->scheme_type == URL_SCHEME_UNKNOWN) {
2491 if(!computeOnly)
2492 memcpy(uri->canon_uri+uri->canon_len, data->host, data->host_len*sizeof(WCHAR));
2493 uri->canon_len += data->host_len;
2494 } else {
2495 if(!computeOnly)
2496 uri->canon_len += ui2ipv4(uri->canon_uri+uri->canon_len, data->implicit_ipv4);
2497 else
2498 uri->canon_len += ui2ipv4(NULL, data->implicit_ipv4);
2501 uri->host_len = uri->canon_len - uri->host_start;
2502 uri->host_type = Uri_HOST_IPV4;
2504 if(!computeOnly)
2505 TRACE("%p %p %x %d): Canonicalized implicit IP address=%s len=%d\n",
2506 data, uri, flags, computeOnly,
2507 debugstr_wn(uri->canon_uri+uri->host_start, uri->host_len),
2508 uri->host_len);
2510 return TRUE;
2513 /* Attempts to canonicalize an IPv4 address.
2515 * If the parse_data represents a URI that has an implicit IPv4 address
2516 * (ex. http://256/, this function will convert 256 into 0.0.1.0). If
2517 * the implicit IP address exceeds the value of UINT_MAX (maximum value
2518 * for an IPv4 address) it's canonicalized as if were a reg-name.
2520 * If the parse_data contains a partial or full IPv4 address it normalizes it.
2521 * A partial IPv4 address is something like "192.0" and would be normalized to
2522 * "192.0.0.0". With a full (or partial) IPv4 address like "192.002.01.003" would
2523 * be normalized to "192.2.1.3".
2525 * NOTES:
2526 * Window's ONLY normalizes IPv4 address for known scheme types (one that isn't
2527 * URL_SCHEME_UNKNOWN). For unknown scheme types, it simply copies the data from
2528 * the original URI into the canonicalized URI, but, it still recognizes URI's
2529 * host type as HOST_IPV4.
2531 static BOOL canonicalize_ipv4address(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2532 if(data->has_implicit_ip)
2533 return canonicalize_implicit_ipv4address(data, uri, flags, computeOnly);
2534 else {
2535 uri->host_start = uri->canon_len;
2537 /* Windows only normalizes for known scheme types. */
2538 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
2539 /* parse_data contains a partial or full IPv4 address, so normalize it. */
2540 DWORD i, octetDigitCount = 0, octetCount = 0;
2541 BOOL octetHasDigit = FALSE;
2543 for(i = 0; i < data->host_len; ++i) {
2544 if(data->host[i] == '0' && !octetHasDigit) {
2545 /* Can ignore leading zeros if:
2546 * 1) It isn't the last digit of the octet.
2547 * 2) i+1 != data->host_len
2548 * 3) i+1 != '.'
2550 if(octetDigitCount == 2 ||
2551 i+1 == data->host_len ||
2552 data->host[i+1] == '.') {
2553 if(!computeOnly)
2554 uri->canon_uri[uri->canon_len] = data->host[i];
2555 ++uri->canon_len;
2556 TRACE("Adding zero\n");
2558 } else if(data->host[i] == '.') {
2559 if(!computeOnly)
2560 uri->canon_uri[uri->canon_len] = data->host[i];
2561 ++uri->canon_len;
2563 octetDigitCount = 0;
2564 octetHasDigit = FALSE;
2565 ++octetCount;
2566 } else {
2567 if(!computeOnly)
2568 uri->canon_uri[uri->canon_len] = data->host[i];
2569 ++uri->canon_len;
2571 ++octetDigitCount;
2572 octetHasDigit = TRUE;
2576 /* Make sure the canonicalized IP address has 4 dec-octets.
2577 * If doesn't add "0" ones until there is 4;
2579 for( ; octetCount < 3; ++octetCount) {
2580 if(!computeOnly) {
2581 uri->canon_uri[uri->canon_len] = '.';
2582 uri->canon_uri[uri->canon_len+1] = '0';
2585 uri->canon_len += 2;
2587 } else {
2588 /* Windows doesn't normalize addresses in unknown schemes. */
2589 if(!computeOnly)
2590 memcpy(uri->canon_uri+uri->canon_len, data->host, data->host_len*sizeof(WCHAR));
2591 uri->canon_len += data->host_len;
2594 uri->host_len = uri->canon_len - uri->host_start;
2595 if(!computeOnly)
2596 TRACE("(%p %p %x %d): Canonicalized IPv4 address, ip=%s len=%d\n",
2597 data, uri, flags, computeOnly,
2598 debugstr_wn(uri->canon_uri+uri->host_start, uri->host_len),
2599 uri->host_len);
2602 return TRUE;
2605 /* Attempts to canonicalize the IPv6 address of the URI.
2607 * Multiple things happen during the canonicalization of an IPv6 address:
2608 * 1) Any leading zero's in an h16 component are removed.
2609 * Ex: [0001:0022::] -> [1:22::]
2611 * 2) The longest sequence of zero h16 components are compressed
2612 * into a "::" (elision). If there's a tie, the first is choosen.
2614 * Ex: [0:0:0:0:1:6:7:8] -> [::1:6:7:8]
2615 * [0:0:0:0:1:2::] -> [::1:2:0:0]
2616 * [0:0:1:2:0:0:7:8] -> [::1:2:0:0:7:8]
2618 * 3) If an IPv4 address is attached to the IPv6 address, it's
2619 * also normalized.
2620 * Ex: [::001.002.022.000] -> [::1.2.22.0]
2622 * 4) If an elision is present, but, only represents 1 h16 component
2623 * it's expanded.
2625 * Ex: [1::2:3:4:5:6:7] -> [1:0:2:3:4:5:6:7]
2627 * 5) If the IPv6 address contains an IPv4 address and there exists
2628 * at least 1 non-zero h16 component the IPv4 address is converted
2629 * into two h16 components, otherwise it's normalized and kept as is.
2631 * Ex: [::192.200.003.4] -> [::192.200.3.4]
2632 * [ffff::192.200.003.4] -> [ffff::c0c8:3041]
2634 * NOTE:
2635 * For unknown scheme types Windows simply copies the address over without any
2636 * changes.
2638 * IPv4 address can be included in an elision if all its components are 0's.
2640 static BOOL canonicalize_ipv6address(const parse_data *data, Uri *uri,
2641 DWORD flags, BOOL computeOnly) {
2642 uri->host_start = uri->canon_len;
2644 if(data->scheme_type == URL_SCHEME_UNKNOWN) {
2645 if(!computeOnly)
2646 memcpy(uri->canon_uri+uri->canon_len, data->host, data->host_len*sizeof(WCHAR));
2647 uri->canon_len += data->host_len;
2648 } else {
2649 USHORT values[8];
2650 INT elision_start;
2651 DWORD i, elision_len;
2653 if(!ipv6_to_number(&(data->ipv6_address), values)) {
2654 TRACE("(%p %p %x %d): Failed to compute numerical value for IPv6 address.\n",
2655 data, uri, flags, computeOnly);
2656 return FALSE;
2659 if(!computeOnly)
2660 uri->canon_uri[uri->canon_len] = '[';
2661 ++uri->canon_len;
2663 /* Find where the elision should occur (if any). */
2664 compute_elision_location(&(data->ipv6_address), values, &elision_start, &elision_len);
2666 TRACE("%p %p %x %d): Elision starts at %d, len=%u\n", data, uri, flags,
2667 computeOnly, elision_start, elision_len);
2669 for(i = 0; i < 8; ++i) {
2670 BOOL in_elision = (elision_start > -1 && i >= elision_start &&
2671 i < elision_start+elision_len);
2672 BOOL do_ipv4 = (i == 6 && data->ipv6_address.ipv4 && !in_elision &&
2673 data->ipv6_address.h16_count == 0);
2675 if(i == elision_start) {
2676 if(!computeOnly) {
2677 uri->canon_uri[uri->canon_len] = ':';
2678 uri->canon_uri[uri->canon_len+1] = ':';
2680 uri->canon_len += 2;
2683 /* We can ignore the current component if we're in the elision. */
2684 if(in_elision)
2685 continue;
2687 /* We only add a ':' if we're not at i == 0, or when we're at
2688 * the very end of elision range since the ':' colon was handled
2689 * earlier. Otherwise we would end up with ":::" after elision.
2691 if(i != 0 && !(elision_start > -1 && i == elision_start+elision_len)) {
2692 if(!computeOnly)
2693 uri->canon_uri[uri->canon_len] = ':';
2694 ++uri->canon_len;
2697 if(do_ipv4) {
2698 UINT val;
2699 DWORD len;
2701 /* Combine the two parts of the IPv4 address values. */
2702 val = values[i];
2703 val <<= 16;
2704 val += values[i+1];
2706 if(!computeOnly)
2707 len = ui2ipv4(uri->canon_uri+uri->canon_len, val);
2708 else
2709 len = ui2ipv4(NULL, val);
2711 uri->canon_len += len;
2712 ++i;
2713 } else {
2714 /* Write a regular h16 component to the URI. */
2716 /* Short circuit for the trivial case. */
2717 if(values[i] == 0) {
2718 if(!computeOnly)
2719 uri->canon_uri[uri->canon_len] = '0';
2720 ++uri->canon_len;
2721 } else {
2722 static const WCHAR formatW[] = {'%','x',0};
2724 if(!computeOnly)
2725 uri->canon_len += sprintfW(uri->canon_uri+uri->canon_len,
2726 formatW, values[i]);
2727 else {
2728 WCHAR tmp[5];
2729 uri->canon_len += sprintfW(tmp, formatW, values[i]);
2735 /* Add the closing ']'. */
2736 if(!computeOnly)
2737 uri->canon_uri[uri->canon_len] = ']';
2738 ++uri->canon_len;
2741 uri->host_len = uri->canon_len - uri->host_start;
2743 if(!computeOnly)
2744 TRACE("(%p %p %x %d): Canonicalized IPv6 address %s, len=%d\n", data, uri, flags,
2745 computeOnly, debugstr_wn(uri->canon_uri+uri->host_start, uri->host_len),
2746 uri->host_len);
2748 return TRUE;
2751 /* Attempts to canonicalize the host of the URI (if any). */
2752 static BOOL canonicalize_host(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2753 uri->host_start = -1;
2754 uri->host_len = 0;
2755 uri->domain_offset = -1;
2757 if(data->host) {
2758 switch(data->host_type) {
2759 case Uri_HOST_DNS:
2760 uri->host_type = Uri_HOST_DNS;
2761 if(!canonicalize_reg_name(data, uri, flags, computeOnly))
2762 return FALSE;
2764 break;
2765 case Uri_HOST_IPV4:
2766 uri->host_type = Uri_HOST_IPV4;
2767 if(!canonicalize_ipv4address(data, uri, flags, computeOnly))
2768 return FALSE;
2770 break;
2771 case Uri_HOST_IPV6:
2772 if(!canonicalize_ipv6address(data, uri, flags, computeOnly))
2773 return FALSE;
2775 uri->host_type = Uri_HOST_IPV6;
2776 break;
2777 case Uri_HOST_UNKNOWN:
2778 if(data->host_len > 0 || data->scheme_type != URL_SCHEME_FILE) {
2779 uri->host_start = uri->canon_len;
2781 /* Nothing happens to unknown host types. */
2782 if(!computeOnly)
2783 memcpy(uri->canon_uri+uri->canon_len, data->host, data->host_len*sizeof(WCHAR));
2784 uri->canon_len += data->host_len;
2785 uri->host_len = data->host_len;
2788 uri->host_type = Uri_HOST_UNKNOWN;
2789 break;
2790 default:
2791 FIXME("(%p %p %x %d): Canonicalization for host type %d not supported.\n", data,
2792 uri, flags, computeOnly, data->host_type);
2793 return FALSE;
2797 return TRUE;
2800 static BOOL canonicalize_port(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2801 BOOL has_default_port = FALSE;
2802 USHORT default_port = 0;
2803 DWORD i;
2805 uri->port_offset = -1;
2807 /* Check if the scheme has a default port. */
2808 for(i = 0; i < sizeof(default_ports)/sizeof(default_ports[0]); ++i) {
2809 if(default_ports[i].scheme == data->scheme_type) {
2810 has_default_port = TRUE;
2811 default_port = default_ports[i].port;
2812 break;
2816 uri->has_port = data->has_port || has_default_port;
2818 /* Possible cases:
2819 * 1) Has a port which is the default port.
2820 * 2) Has a port (not the default).
2821 * 3) Doesn't have a port, but, scheme has a default port.
2822 * 4) No port.
2824 if(has_default_port && data->has_port && data->port_value == default_port) {
2825 /* If it's the default port and this flag isn't set, don't do anything. */
2826 if(flags & Uri_CREATE_NO_CANONICALIZE) {
2827 uri->port_offset = uri->canon_len-uri->authority_start;
2828 if(!computeOnly)
2829 uri->canon_uri[uri->canon_len] = ':';
2830 ++uri->canon_len;
2832 if(data->port) {
2833 /* Copy the original port over. */
2834 if(!computeOnly)
2835 memcpy(uri->canon_uri+uri->canon_len, data->port, data->port_len*sizeof(WCHAR));
2836 uri->canon_len += data->port_len;
2837 } else {
2838 if(!computeOnly)
2839 uri->canon_len += ui2str(uri->canon_uri+uri->canon_len, data->port_value);
2840 else
2841 uri->canon_len += ui2str(NULL, data->port_value);
2845 uri->port = default_port;
2846 } else if(data->has_port) {
2847 uri->port_offset = uri->canon_len-uri->authority_start;
2848 if(!computeOnly)
2849 uri->canon_uri[uri->canon_len] = ':';
2850 ++uri->canon_len;
2852 if(flags & Uri_CREATE_NO_CANONICALIZE && data->port) {
2853 /* Copy the original over without changes. */
2854 if(!computeOnly)
2855 memcpy(uri->canon_uri+uri->canon_len, data->port, data->port_len*sizeof(WCHAR));
2856 uri->canon_len += data->port_len;
2857 } else {
2858 if(!computeOnly)
2859 uri->canon_len += ui2str(uri->canon_uri+uri->canon_len, data->port_value);
2860 else
2861 uri->canon_len += ui2str(NULL, data->port_value);
2864 uri->port = data->port_value;
2865 } else if(has_default_port)
2866 uri->port = default_port;
2868 return TRUE;
2871 /* Canonicalizes the authority of the URI represented by the parse_data. */
2872 static BOOL canonicalize_authority(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2873 uri->authority_start = uri->canon_len;
2874 uri->authority_len = 0;
2876 if(!canonicalize_userinfo(data, uri, flags, computeOnly))
2877 return FALSE;
2879 if(!canonicalize_host(data, uri, flags, computeOnly))
2880 return FALSE;
2882 if(!canonicalize_port(data, uri, flags, computeOnly))
2883 return FALSE;
2885 if(uri->host_start != -1 || (data->is_relative && (data->password || data->username)))
2886 uri->authority_len = uri->canon_len - uri->authority_start;
2887 else
2888 uri->authority_start = -1;
2890 return TRUE;
2893 /* Attempts to canonicalize the path of a hierarchical URI.
2895 * Things that happen:
2896 * 1). Forbidden characters are percent encoded, unless the NO_ENCODE_FORBIDDEN
2897 * flag is set or it's a file URI. Forbidden characters are always encoded
2898 * for file schemes reguardless and forbidden characters are never encoded
2899 * for unknown scheme types.
2901 * 2). For known scheme types '\\' are changed to '/'.
2903 * 3). Percent encoded, unreserved characters are decoded to their actual values.
2904 * Unless the scheme type is unknown. For file schemes any percent encoded
2905 * character in the unreserved or reserved set is decoded.
2907 * 4). For File schemes if the path is starts with a drive letter and doesn't
2908 * start with a '/' then one is appended.
2909 * Ex: file://c:/test.mp3 -> file:///c:/test.mp3
2911 * 5). Dot segments are removed from the path for all scheme types
2912 * unless NO_CANONICALIZE flag is set. Dot segments aren't removed
2913 * for wildcard scheme types.
2915 * NOTES:
2916 * file://c:/test%20test -> file:///c:/test%2520test
2917 * file://c:/test%3Etest -> file:///c:/test%253Etest
2918 * if Uri_CREATE_FILE_USE_DOS_PATH is not set:
2919 * file:///c:/test%20test -> file:///c:/test%20test
2920 * file:///c:/test%test -> file:///c:/test%25test
2922 static BOOL canonicalize_path_hierarchical(const parse_data *data, Uri *uri,
2923 DWORD flags, BOOL computeOnly) {
2924 const WCHAR *ptr;
2925 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
2926 const BOOL is_file = data->scheme_type == URL_SCHEME_FILE;
2927 const BOOL is_res = data->scheme_type == URL_SCHEME_RES;
2929 BOOL escape_pct = FALSE;
2931 if(!data->path) {
2932 uri->path_start = -1;
2933 uri->path_len = 0;
2934 return TRUE;
2937 uri->path_start = uri->canon_len;
2938 ptr = data->path;
2940 if(is_file && uri->host_start == -1) {
2941 /* Check if a '/' needs to be appended for the file scheme. */
2942 if(data->path_len > 1 && is_drive_path(ptr) && !(flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
2943 if(!computeOnly)
2944 uri->canon_uri[uri->canon_len] = '/';
2945 uri->canon_len++;
2946 escape_pct = TRUE;
2947 } else if(*ptr == '/') {
2948 if(!(flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
2949 /* Copy the extra '/' over. */
2950 if(!computeOnly)
2951 uri->canon_uri[uri->canon_len] = '/';
2952 ++uri->canon_len;
2954 ++ptr;
2957 if(is_drive_path(ptr)) {
2958 if(!computeOnly) {
2959 uri->canon_uri[uri->canon_len] = *ptr;
2960 /* If theres a '|' after the drive letter, convert it to a ':'. */
2961 uri->canon_uri[uri->canon_len+1] = ':';
2963 ptr += 2;
2964 uri->canon_len += 2;
2968 if(!is_file && *(data->path) && *(data->path) != '/') {
2969 /* Prepend a '/' to the path if it doesn't have one. */
2970 if(!computeOnly)
2971 uri->canon_uri[uri->canon_len] = '/';
2972 ++uri->canon_len;
2975 for(; ptr < data->path+data->path_len; ++ptr) {
2976 BOOL do_default_action = TRUE;
2978 if(*ptr == '%' && !is_res) {
2979 const WCHAR *tmp = ptr;
2980 WCHAR val;
2982 /* Check if the % represents a valid encoded char, or if it needs encoded. */
2983 BOOL force_encode = !check_pct_encoded(&tmp) && is_file && !(flags&Uri_CREATE_FILE_USE_DOS_PATH);
2984 val = decode_pct_val(ptr);
2986 if(force_encode || escape_pct) {
2987 /* Escape the percent sign in the file URI. */
2988 if(!computeOnly)
2989 pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
2990 uri->canon_len += 3;
2991 do_default_action = FALSE;
2992 } else if((is_unreserved(val) && known_scheme) ||
2993 (is_file && (is_unreserved(val) || is_reserved(val) ||
2994 (val && flags&Uri_CREATE_FILE_USE_DOS_PATH && !is_forbidden_dos_path_char(val))))) {
2995 if(!computeOnly)
2996 uri->canon_uri[uri->canon_len] = val;
2997 ++uri->canon_len;
2999 ptr += 2;
3000 continue;
3002 } else if(*ptr == '/' && is_file && (flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
3003 /* Convert the '/' back to a '\\'. */
3004 if(!computeOnly)
3005 uri->canon_uri[uri->canon_len] = '\\';
3006 ++uri->canon_len;
3007 do_default_action = FALSE;
3008 } else if(*ptr == '\\' && known_scheme) {
3009 if(!(is_file && (flags & Uri_CREATE_FILE_USE_DOS_PATH))) {
3010 /* Convert '\\' into a '/'. */
3011 if(!computeOnly)
3012 uri->canon_uri[uri->canon_len] = '/';
3013 ++uri->canon_len;
3014 do_default_action = FALSE;
3016 } else if(known_scheme && !is_res && !is_unreserved(*ptr) && !is_reserved(*ptr) &&
3017 (!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS) || is_file)) {
3018 if(!(is_file && (flags & Uri_CREATE_FILE_USE_DOS_PATH))) {
3019 /* Escape the forbidden character. */
3020 if(!computeOnly)
3021 pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
3022 uri->canon_len += 3;
3023 do_default_action = FALSE;
3027 if(do_default_action) {
3028 if(!computeOnly)
3029 uri->canon_uri[uri->canon_len] = *ptr;
3030 ++uri->canon_len;
3034 uri->path_len = uri->canon_len - uri->path_start;
3036 /* Removing the dot segments only happens when it's not in
3037 * computeOnly mode and it's not a wildcard scheme. File schemes
3038 * with USE_DOS_PATH set don't get dot segments removed.
3040 if(!(is_file && (flags & Uri_CREATE_FILE_USE_DOS_PATH)) &&
3041 data->scheme_type != URL_SCHEME_WILDCARD) {
3042 if(!(flags & Uri_CREATE_NO_CANONICALIZE) && !computeOnly) {
3043 /* Remove the dot segments (if any) and reset everything to the new
3044 * correct length.
3046 DWORD new_len = remove_dot_segments(uri->canon_uri+uri->path_start, uri->path_len);
3047 uri->canon_len -= uri->path_len-new_len;
3048 uri->path_len = new_len;
3052 if(!computeOnly)
3053 TRACE("Canonicalized path %s len=%d\n",
3054 debugstr_wn(uri->canon_uri+uri->path_start, uri->path_len),
3055 uri->path_len);
3057 return TRUE;
3060 /* Attempts to canonicalize the path for an opaque URI.
3062 * For known scheme types:
3063 * 1) forbidden characters are percent encoded if
3064 * NO_ENCODE_FORBIDDEN_CHARACTERS isn't set.
3066 * 2) Percent encoded, unreserved characters are decoded
3067 * to their actual values, for known scheme types.
3069 * 3) '\\' are changed to '/' for known scheme types
3070 * except for mailto schemes.
3072 * 4) For file schemes, if USE_DOS_PATH is set all '/'
3073 * are converted to backslashes.
3075 * 5) For file schemes, if USE_DOS_PATH isn't set all '\'
3076 * are converted to forward slashes.
3078 static BOOL canonicalize_path_opaque(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
3079 const WCHAR *ptr;
3080 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
3081 const BOOL is_file = data->scheme_type == URL_SCHEME_FILE;
3083 if(!data->path) {
3084 uri->path_start = -1;
3085 uri->path_len = 0;
3086 return TRUE;
3089 uri->path_start = uri->canon_len;
3091 /* Windows doesn't allow a "//" to appear after the scheme
3092 * of a URI, if it's an opaque URI.
3094 if(data->scheme && *(data->path) == '/' && *(data->path+1) == '/') {
3095 /* So it inserts a "/." before the "//" if it exists. */
3096 if(!computeOnly) {
3097 uri->canon_uri[uri->canon_len] = '/';
3098 uri->canon_uri[uri->canon_len+1] = '.';
3101 uri->canon_len += 2;
3104 for(ptr = data->path; ptr < data->path+data->path_len; ++ptr) {
3105 BOOL do_default_action = TRUE;
3107 if(*ptr == '%' && known_scheme) {
3108 WCHAR val = decode_pct_val(ptr);
3110 if(is_unreserved(val)) {
3111 if(!computeOnly)
3112 uri->canon_uri[uri->canon_len] = val;
3113 ++uri->canon_len;
3115 ptr += 2;
3116 continue;
3118 } else if(*ptr == '/' && is_file && (flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
3119 if(!computeOnly)
3120 uri->canon_uri[uri->canon_len] = '\\';
3121 ++uri->canon_len;
3122 do_default_action = FALSE;
3123 } else if(*ptr == '\\') {
3124 if(is_file && !(flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
3125 /* Convert to a '/'. */
3126 if(!computeOnly)
3127 uri->canon_uri[uri->canon_len] = '/';
3128 ++uri->canon_len;
3129 do_default_action = FALSE;
3131 } else if(known_scheme && !is_unreserved(*ptr) && !is_reserved(*ptr) &&
3132 !(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS)) {
3133 if(!(is_file && (flags & Uri_CREATE_FILE_USE_DOS_PATH))) {
3134 if(!computeOnly)
3135 pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
3136 uri->canon_len += 3;
3137 do_default_action = FALSE;
3141 if(do_default_action) {
3142 if(!computeOnly)
3143 uri->canon_uri[uri->canon_len] = *ptr;
3144 ++uri->canon_len;
3148 uri->path_len = uri->canon_len - uri->path_start;
3150 TRACE("(%p %p %x %d): Canonicalized opaque URI path %s len=%d\n", data, uri, flags, computeOnly,
3151 debugstr_wn(uri->canon_uri+uri->path_start, uri->path_len), uri->path_len);
3152 return TRUE;
3155 /* Determines how the URI represented by the parse_data should be canonicalized.
3157 * Essentially, if the parse_data represents an hierarchical URI then it calls
3158 * canonicalize_authority and the canonicalization functions for the path. If the
3159 * URI is opaque it canonicalizes the path of the URI.
3161 static BOOL canonicalize_hierpart(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
3162 if(!data->is_opaque || (data->is_relative && (data->password || data->username))) {
3163 /* "//" is only added for non-wildcard scheme types.
3165 * A "//" is only added to a relative URI if it has a
3166 * host or port component (this only happens if a IUriBuilder
3167 * is generating an IUri).
3169 if((data->is_relative && (data->host || data->has_port)) ||
3170 (!data->is_relative && data->scheme_type != URL_SCHEME_WILDCARD)) {
3171 if(!computeOnly) {
3172 INT pos = uri->canon_len;
3174 uri->canon_uri[pos] = '/';
3175 uri->canon_uri[pos+1] = '/';
3177 uri->canon_len += 2;
3180 if(!canonicalize_authority(data, uri, flags, computeOnly))
3181 return FALSE;
3183 if(data->is_relative && (data->password || data->username)) {
3184 if(!canonicalize_path_opaque(data, uri, flags, computeOnly))
3185 return FALSE;
3186 } else {
3187 if(!canonicalize_path_hierarchical(data, uri, flags, computeOnly))
3188 return FALSE;
3190 } else {
3191 /* Opaque URI's don't have an authority. */
3192 uri->userinfo_start = uri->userinfo_split = -1;
3193 uri->userinfo_len = 0;
3194 uri->host_start = -1;
3195 uri->host_len = 0;
3196 uri->host_type = Uri_HOST_UNKNOWN;
3197 uri->has_port = FALSE;
3198 uri->authority_start = -1;
3199 uri->authority_len = 0;
3200 uri->domain_offset = -1;
3201 uri->port_offset = -1;
3203 if(is_hierarchical_scheme(data->scheme_type)) {
3204 DWORD i;
3206 /* Absolute URIs aren't displayed for known scheme types
3207 * which should be hierarchical URIs.
3209 uri->display_modifiers |= URI_DISPLAY_NO_ABSOLUTE_URI;
3211 /* Windows also sets the port for these (if they have one). */
3212 for(i = 0; i < sizeof(default_ports)/sizeof(default_ports[0]); ++i) {
3213 if(data->scheme_type == default_ports[i].scheme) {
3214 uri->has_port = TRUE;
3215 uri->port = default_ports[i].port;
3216 break;
3221 if(!canonicalize_path_opaque(data, uri, flags, computeOnly))
3222 return FALSE;
3225 if(uri->path_start > -1 && !computeOnly)
3226 /* Finding file extensions happens for both types of URIs. */
3227 uri->extension_offset = find_file_extension(uri->canon_uri+uri->path_start, uri->path_len);
3228 else
3229 uri->extension_offset = -1;
3231 return TRUE;
3234 /* Attempts to canonicalize the query string of the URI.
3236 * Things that happen:
3237 * 1) For known scheme types forbidden characters
3238 * are percent encoded, unless the NO_DECODE_EXTRA_INFO flag is set
3239 * or NO_ENCODE_FORBIDDEN_CHARACTERS is set.
3241 * 2) For known scheme types, percent encoded, unreserved characters
3242 * are decoded as long as the NO_DECODE_EXTRA_INFO flag isn't set.
3244 static BOOL canonicalize_query(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
3245 const WCHAR *ptr, *end;
3246 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
3248 if(!data->query) {
3249 uri->query_start = -1;
3250 uri->query_len = 0;
3251 return TRUE;
3254 uri->query_start = uri->canon_len;
3256 end = data->query+data->query_len;
3257 for(ptr = data->query; ptr < end; ++ptr) {
3258 if(*ptr == '%') {
3259 if(known_scheme && !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
3260 WCHAR val = decode_pct_val(ptr);
3261 if(is_unreserved(val)) {
3262 if(!computeOnly)
3263 uri->canon_uri[uri->canon_len] = val;
3264 ++uri->canon_len;
3266 ptr += 2;
3267 continue;
3270 } else if(known_scheme && !is_unreserved(*ptr) && !is_reserved(*ptr)) {
3271 if(!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS) &&
3272 !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
3273 if(!computeOnly)
3274 pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
3275 uri->canon_len += 3;
3276 continue;
3280 if(!computeOnly)
3281 uri->canon_uri[uri->canon_len] = *ptr;
3282 ++uri->canon_len;
3285 uri->query_len = uri->canon_len - uri->query_start;
3287 if(!computeOnly)
3288 TRACE("(%p %p %x %d): Canonicalized query string %s len=%d\n", data, uri, flags,
3289 computeOnly, debugstr_wn(uri->canon_uri+uri->query_start, uri->query_len),
3290 uri->query_len);
3291 return TRUE;
3294 static BOOL canonicalize_fragment(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
3295 const WCHAR *ptr, *end;
3296 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
3298 if(!data->fragment) {
3299 uri->fragment_start = -1;
3300 uri->fragment_len = 0;
3301 return TRUE;
3304 uri->fragment_start = uri->canon_len;
3306 end = data->fragment + data->fragment_len;
3307 for(ptr = data->fragment; ptr < end; ++ptr) {
3308 if(*ptr == '%') {
3309 if(known_scheme && !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
3310 WCHAR val = decode_pct_val(ptr);
3311 if(is_unreserved(val)) {
3312 if(!computeOnly)
3313 uri->canon_uri[uri->canon_len] = val;
3314 ++uri->canon_len;
3316 ptr += 2;
3317 continue;
3320 } else if(known_scheme && !is_unreserved(*ptr) && !is_reserved(*ptr)) {
3321 if(!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS) &&
3322 !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
3323 if(!computeOnly)
3324 pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
3325 uri->canon_len += 3;
3326 continue;
3330 if(!computeOnly)
3331 uri->canon_uri[uri->canon_len] = *ptr;
3332 ++uri->canon_len;
3335 uri->fragment_len = uri->canon_len - uri->fragment_start;
3337 if(!computeOnly)
3338 TRACE("(%p %p %x %d): Canonicalized fragment %s len=%d\n", data, uri, flags,
3339 computeOnly, debugstr_wn(uri->canon_uri+uri->fragment_start, uri->fragment_len),
3340 uri->fragment_len);
3341 return TRUE;
3344 /* Canonicalizes the scheme information specified in the parse_data using the specified flags. */
3345 static BOOL canonicalize_scheme(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
3346 uri->scheme_start = -1;
3347 uri->scheme_len = 0;
3349 if(!data->scheme) {
3350 /* The only type of URI that doesn't have to have a scheme is a relative
3351 * URI.
3353 if(!data->is_relative) {
3354 FIXME("(%p %p %x): Unable to determine the scheme type of %s.\n", data,
3355 uri, flags, debugstr_w(data->uri));
3356 return FALSE;
3358 } else {
3359 if(!computeOnly) {
3360 DWORD i;
3361 INT pos = uri->canon_len;
3363 for(i = 0; i < data->scheme_len; ++i) {
3364 /* Scheme name must be lower case after canonicalization. */
3365 uri->canon_uri[i + pos] = tolowerW(data->scheme[i]);
3368 uri->canon_uri[i + pos] = ':';
3369 uri->scheme_start = pos;
3371 TRACE("(%p %p %x): Canonicalized scheme=%s, len=%d.\n", data, uri, flags,
3372 debugstr_wn(uri->canon_uri, uri->scheme_len), data->scheme_len);
3375 /* This happens in both computation modes. */
3376 uri->canon_len += data->scheme_len + 1;
3377 uri->scheme_len = data->scheme_len;
3379 return TRUE;
3382 /* Compute's what the length of the URI specified by the parse_data will be
3383 * after canonicalization occurs using the specified flags.
3385 * This function will return a non-zero value indicating the length of the canonicalized
3386 * URI, or -1 on error.
3388 static int compute_canonicalized_length(const parse_data *data, DWORD flags) {
3389 Uri uri;
3391 memset(&uri, 0, sizeof(Uri));
3393 TRACE("(%p %x): Beginning to compute canonicalized length for URI %s\n", data, flags,
3394 debugstr_w(data->uri));
3396 if(!canonicalize_scheme(data, &uri, flags, TRUE)) {
3397 ERR("(%p %x): Failed to compute URI scheme length.\n", data, flags);
3398 return -1;
3401 if(!canonicalize_hierpart(data, &uri, flags, TRUE)) {
3402 ERR("(%p %x): Failed to compute URI hierpart length.\n", data, flags);
3403 return -1;
3406 if(!canonicalize_query(data, &uri, flags, TRUE)) {
3407 ERR("(%p %x): Failed to compute query string length.\n", data, flags);
3408 return -1;
3411 if(!canonicalize_fragment(data, &uri, flags, TRUE)) {
3412 ERR("(%p %x): Failed to compute fragment length.\n", data, flags);
3413 return -1;
3416 TRACE("(%p %x): Finished computing canonicalized URI length. length=%d\n", data, flags, uri.canon_len);
3418 return uri.canon_len;
3421 /* Canonicalizes the URI data specified in the parse_data, using the given flags. If the
3422 * canonicalization succeededs it will store all the canonicalization information
3423 * in the pointer to the Uri.
3425 * To canonicalize a URI this function first computes what the length of the URI
3426 * specified by the parse_data will be. Once this is done it will then perfom the actual
3427 * canonicalization of the URI.
3429 static HRESULT canonicalize_uri(const parse_data *data, Uri *uri, DWORD flags) {
3430 INT len;
3432 uri->canon_uri = NULL;
3433 len = uri->canon_size = uri->canon_len = 0;
3435 TRACE("(%p %p %x): beginning to canonicalize URI %s.\n", data, uri, flags, debugstr_w(data->uri));
3437 /* First try to compute the length of the URI. */
3438 len = compute_canonicalized_length(data, flags);
3439 if(len == -1) {
3440 ERR("(%p %p %x): Could not compute the canonicalized length of %s.\n", data, uri, flags,
3441 debugstr_w(data->uri));
3442 return E_INVALIDARG;
3445 uri->canon_uri = heap_alloc((len+1)*sizeof(WCHAR));
3446 if(!uri->canon_uri)
3447 return E_OUTOFMEMORY;
3449 uri->canon_size = len;
3450 if(!canonicalize_scheme(data, uri, flags, FALSE)) {
3451 ERR("(%p %p %x): Unable to canonicalize the scheme of the URI.\n", data, uri, flags);
3452 return E_INVALIDARG;
3454 uri->scheme_type = data->scheme_type;
3456 if(!canonicalize_hierpart(data, uri, flags, FALSE)) {
3457 ERR("(%p %p %x): Unable to canonicalize the heirpart of the URI\n", data, uri, flags);
3458 return E_INVALIDARG;
3461 if(!canonicalize_query(data, uri, flags, FALSE)) {
3462 ERR("(%p %p %x): Unable to canonicalize query string of the URI.\n",
3463 data, uri, flags);
3464 return E_INVALIDARG;
3467 if(!canonicalize_fragment(data, uri, flags, FALSE)) {
3468 ERR("(%p %p %x): Unable to canonicalize fragment of the URI.\n",
3469 data, uri, flags);
3470 return E_INVALIDARG;
3473 /* There's a possibility we didn't use all the space we allocated
3474 * earlier.
3476 if(uri->canon_len < uri->canon_size) {
3477 /* This happens if the URI is hierarchical and dot
3478 * segments were removed from it's path.
3480 WCHAR *tmp = heap_realloc(uri->canon_uri, (uri->canon_len+1)*sizeof(WCHAR));
3481 if(!tmp)
3482 return E_OUTOFMEMORY;
3484 uri->canon_uri = tmp;
3485 uri->canon_size = uri->canon_len;
3488 uri->canon_uri[uri->canon_len] = '\0';
3489 TRACE("(%p %p %x): finished canonicalizing the URI. uri=%s\n", data, uri, flags, debugstr_w(uri->canon_uri));
3491 return S_OK;
3494 static HRESULT get_builder_component(LPWSTR *component, DWORD *component_len,
3495 LPCWSTR source, DWORD source_len,
3496 LPCWSTR *output, DWORD *output_len)
3498 if(!output_len) {
3499 if(output)
3500 *output = NULL;
3501 return E_POINTER;
3504 if(!output) {
3505 *output_len = 0;
3506 return E_POINTER;
3509 if(!(*component) && source) {
3510 /* Allocate 'component', and copy the contents from 'source'
3511 * into the new allocation.
3513 *component = heap_alloc((source_len+1)*sizeof(WCHAR));
3514 if(!(*component))
3515 return E_OUTOFMEMORY;
3517 memcpy(*component, source, source_len*sizeof(WCHAR));
3518 (*component)[source_len] = '\0';
3519 *component_len = source_len;
3522 *output = *component;
3523 *output_len = *component_len;
3524 return *output ? S_OK : S_FALSE;
3527 /* Allocates 'component' and copies the string from 'new_value' into 'component'.
3528 * If 'prefix' is set and 'new_value' isn't NULL, then it checks if 'new_value'
3529 * starts with 'prefix'. If it doesn't then 'prefix' is prepended to 'component'.
3531 * If everything is successful, then will set 'success_flag' in 'flags'.
3533 static HRESULT set_builder_component(LPWSTR *component, DWORD *component_len, LPCWSTR new_value,
3534 WCHAR prefix, DWORD *flags, DWORD success_flag)
3536 heap_free(*component);
3538 if(!new_value) {
3539 *component = NULL;
3540 *component_len = 0;
3541 } else {
3542 BOOL add_prefix = FALSE;
3543 DWORD len = lstrlenW(new_value);
3544 DWORD pos = 0;
3546 if(prefix && *new_value != prefix) {
3547 add_prefix = TRUE;
3548 *component = heap_alloc((len+2)*sizeof(WCHAR));
3549 } else
3550 *component = heap_alloc((len+1)*sizeof(WCHAR));
3552 if(!(*component))
3553 return E_OUTOFMEMORY;
3555 if(add_prefix)
3556 (*component)[pos++] = prefix;
3558 memcpy(*component+pos, new_value, (len+1)*sizeof(WCHAR));
3559 *component_len = len+pos;
3562 *flags |= success_flag;
3563 return S_OK;
3566 #define URI(x) ((IUri*) &(x)->lpIUriVtbl)
3567 #define URIBUILDER(x) ((IUriBuilder*) &(x)->lpIUriBuilderVtbl)
3569 static void reset_builder(UriBuilder *builder) {
3570 if(builder->uri)
3571 IUri_Release(URI(builder->uri));
3572 builder->uri = NULL;
3574 heap_free(builder->fragment);
3575 builder->fragment = NULL;
3576 builder->fragment_len = 0;
3578 heap_free(builder->host);
3579 builder->host = NULL;
3580 builder->host_len = 0;
3582 heap_free(builder->password);
3583 builder->password = NULL;
3584 builder->password_len = 0;
3586 heap_free(builder->path);
3587 builder->path = NULL;
3588 builder->path_len = 0;
3590 heap_free(builder->query);
3591 builder->query = NULL;
3592 builder->query_len = 0;
3594 heap_free(builder->scheme);
3595 builder->scheme = NULL;
3596 builder->scheme_len = 0;
3598 heap_free(builder->username);
3599 builder->username = NULL;
3600 builder->username_len = 0;
3602 builder->has_port = FALSE;
3603 builder->port = 0;
3604 builder->modified_props = 0;
3607 static HRESULT validate_scheme_name(const UriBuilder *builder, parse_data *data, DWORD flags) {
3608 const WCHAR *component;
3609 const WCHAR *ptr;
3610 const WCHAR **pptr;
3611 DWORD expected_len;
3613 if(builder->scheme) {
3614 ptr = builder->scheme;
3615 expected_len = builder->scheme_len;
3616 } else if(builder->uri && builder->uri->scheme_start > -1) {
3617 ptr = builder->uri->canon_uri+builder->uri->scheme_start;
3618 expected_len = builder->uri->scheme_len;
3619 } else {
3620 static const WCHAR nullW[] = {0};
3621 ptr = nullW;
3622 expected_len = 0;
3625 component = ptr;
3626 pptr = &ptr;
3627 if(parse_scheme(pptr, data, flags, ALLOW_NULL_TERM_SCHEME) &&
3628 data->scheme_len == expected_len) {
3629 if(data->scheme)
3630 TRACE("(%p %p %x): Found valid scheme component %s len=%d.\n", builder, data, flags,
3631 debugstr_wn(data->scheme, data->scheme_len), data->scheme_len);
3632 } else {
3633 TRACE("(%p %p %x): Invalid scheme component found %s.\n", builder, data, flags,
3634 debugstr_wn(component, expected_len));
3635 return INET_E_INVALID_URL;
3638 return S_OK;
3641 static HRESULT validate_username(const UriBuilder *builder, parse_data *data, DWORD flags) {
3642 const WCHAR *ptr;
3643 const WCHAR **pptr;
3644 DWORD expected_len;
3646 if(builder->username) {
3647 ptr = builder->username;
3648 expected_len = builder->username_len;
3649 } else if(!(builder->modified_props & Uri_HAS_USER_NAME) && builder->uri &&
3650 builder->uri->userinfo_start > -1 && builder->uri->userinfo_split != 0) {
3651 /* Just use the username from the base Uri. */
3652 data->username = builder->uri->canon_uri+builder->uri->userinfo_start;
3653 data->username_len = (builder->uri->userinfo_split > -1) ?
3654 builder->uri->userinfo_split : builder->uri->userinfo_len;
3655 ptr = NULL;
3656 } else {
3657 ptr = NULL;
3658 expected_len = 0;
3661 if(ptr) {
3662 const WCHAR *component = ptr;
3663 pptr = &ptr;
3664 if(parse_username(pptr, data, flags, ALLOW_NULL_TERM_USER_NAME) &&
3665 data->username_len == expected_len)
3666 TRACE("(%p %p %x): Found valid username component %s len=%d.\n", builder, data, flags,
3667 debugstr_wn(data->username, data->username_len), data->username_len);
3668 else {
3669 TRACE("(%p %p %x): Invalid username component found %s.\n", builder, data, flags,
3670 debugstr_wn(component, expected_len));
3671 return INET_E_INVALID_URL;
3675 return S_OK;
3678 static HRESULT validate_password(const UriBuilder *builder, parse_data *data, DWORD flags) {
3679 const WCHAR *ptr;
3680 const WCHAR **pptr;
3681 DWORD expected_len;
3683 if(builder->password) {
3684 ptr = builder->password;
3685 expected_len = builder->password_len;
3686 } else if(!(builder->modified_props & Uri_HAS_PASSWORD) && builder->uri &&
3687 builder->uri->userinfo_split > -1) {
3688 data->password = builder->uri->canon_uri+builder->uri->userinfo_start+builder->uri->userinfo_split+1;
3689 data->password_len = builder->uri->userinfo_len-builder->uri->userinfo_split-1;
3690 ptr = NULL;
3691 } else {
3692 ptr = NULL;
3693 expected_len = 0;
3696 if(ptr) {
3697 const WCHAR *component = ptr;
3698 pptr = &ptr;
3699 if(parse_password(pptr, data, flags, ALLOW_NULL_TERM_PASSWORD) &&
3700 data->password_len == expected_len)
3701 TRACE("(%p %p %x): Found valid password component %s len=%d.\n", builder, data, flags,
3702 debugstr_wn(data->password, data->password_len), data->password_len);
3703 else {
3704 TRACE("(%p %p %x): Invalid password component found %s.\n", builder, data, flags,
3705 debugstr_wn(component, expected_len));
3706 return INET_E_INVALID_URL;
3710 return S_OK;
3713 static HRESULT validate_userinfo(const UriBuilder *builder, parse_data *data, DWORD flags) {
3714 HRESULT hr;
3716 hr = validate_username(builder, data, flags);
3717 if(FAILED(hr))
3718 return hr;
3720 hr = validate_password(builder, data, flags);
3721 if(FAILED(hr))
3722 return hr;
3724 return S_OK;
3727 static HRESULT validate_host(const UriBuilder *builder, parse_data *data, DWORD flags) {
3728 const WCHAR *ptr;
3729 const WCHAR **pptr;
3730 DWORD expected_len;
3732 if(builder->host) {
3733 ptr = builder->host;
3734 expected_len = builder->host_len;
3735 } else if(!(builder->modified_props & Uri_HAS_HOST) && builder->uri && builder->uri->host_start > -1) {
3736 ptr = builder->uri->canon_uri + builder->uri->host_start;
3737 expected_len = builder->uri->host_len;
3738 } else
3739 ptr = NULL;
3741 if(ptr) {
3742 const WCHAR *component = ptr;
3743 DWORD extras = ALLOW_BRACKETLESS_IP_LITERAL|IGNORE_PORT_DELIMITER|SKIP_IP_FUTURE_CHECK;
3744 pptr = &ptr;
3746 if(parse_host(pptr, data, flags, extras) && data->host_len == expected_len)
3747 TRACE("(%p %p %x): Found valid host name %s len=%d type=%d.\n", builder, data, flags,
3748 debugstr_wn(data->host, data->host_len), data->host_len, data->host_type);
3749 else {
3750 TRACE("(%p %p %x): Invalid host name found %s.\n", builder, data, flags,
3751 debugstr_wn(component, expected_len));
3752 return INET_E_INVALID_URL;
3756 return S_OK;
3759 static void setup_port(const UriBuilder *builder, parse_data *data, DWORD flags) {
3760 if(builder->modified_props & Uri_HAS_PORT) {
3761 if(builder->has_port) {
3762 data->has_port = TRUE;
3763 data->port_value = builder->port;
3765 } else if(builder->uri && builder->uri->has_port) {
3766 data->has_port = TRUE;
3767 data->port_value = builder->uri->port;
3770 if(data->has_port)
3771 TRACE("(%p %p %x): Using %u as port for IUri.\n", builder, data, flags, data->port_value);
3774 static HRESULT validate_path(const UriBuilder *builder, parse_data *data, DWORD flags) {
3775 const WCHAR *ptr = NULL;
3776 const WCHAR *component;
3777 const WCHAR **pptr;
3778 DWORD expected_len;
3779 BOOL check_len = TRUE;
3780 BOOL valid = FALSE;
3782 if(builder->path) {
3783 ptr = builder->path;
3784 expected_len = builder->path_len;
3785 } else if(!(builder->modified_props & Uri_HAS_PATH) &&
3786 builder->uri && builder->uri->path_start > -1) {
3787 ptr = builder->uri->canon_uri+builder->uri->path_start;
3788 expected_len = builder->uri->path_len;
3789 } else {
3790 static const WCHAR nullW[] = {0};
3791 ptr = nullW;
3792 check_len = FALSE;
3795 component = ptr;
3796 pptr = &ptr;
3798 /* How the path is validated depends on what type of
3799 * URI it is.
3801 valid = data->is_opaque ?
3802 parse_path_opaque(pptr, data, flags) : parse_path_hierarchical(pptr, data, flags);
3804 if(!valid || (check_len && expected_len != data->path_len)) {
3805 TRACE("(%p %p %x): Invalid path component %s.\n", builder, data, flags,
3806 debugstr_wn(component, check_len ? expected_len : -1) );
3807 return INET_E_INVALID_URL;
3810 TRACE("(%p %p %x): Valid path component %s len=%d.\n", builder, data, flags,
3811 debugstr_wn(data->path, data->path_len), data->path_len);
3813 return S_OK;
3816 static HRESULT validate_query(const UriBuilder *builder, parse_data *data, DWORD flags) {
3817 const WCHAR *ptr = NULL;
3818 const WCHAR **pptr;
3819 DWORD expected_len;
3821 if(builder->query) {
3822 ptr = builder->query;
3823 expected_len = builder->query_len;
3824 } else if(!(builder->modified_props & Uri_HAS_QUERY) && builder->uri &&
3825 builder->uri->query_start > -1) {
3826 ptr = builder->uri->canon_uri+builder->uri->query_start;
3827 expected_len = builder->uri->query_len;
3830 if(ptr) {
3831 const WCHAR *component = ptr;
3832 pptr = &ptr;
3834 if(parse_query(pptr, data, flags) && expected_len == data->query_len)
3835 TRACE("(%p %p %x): Valid query component %s len=%d.\n", builder, data, flags,
3836 debugstr_wn(data->query, data->query_len), data->query_len);
3837 else {
3838 TRACE("(%p %p %x): Invalid query component %s.\n", builder, data, flags,
3839 debugstr_wn(component, expected_len));
3840 return INET_E_INVALID_URL;
3844 return S_OK;
3847 static HRESULT validate_fragment(const UriBuilder *builder, parse_data *data, DWORD flags) {
3848 const WCHAR *ptr = NULL;
3849 const WCHAR **pptr;
3850 DWORD expected_len;
3852 if(builder->fragment) {
3853 ptr = builder->fragment;
3854 expected_len = builder->fragment_len;
3855 } else if(!(builder->modified_props & Uri_HAS_FRAGMENT) && builder->uri &&
3856 builder->uri->fragment_start > -1) {
3857 ptr = builder->uri->canon_uri+builder->uri->fragment_start;
3858 expected_len = builder->uri->fragment_len;
3861 if(ptr) {
3862 const WCHAR *component = ptr;
3863 pptr = &ptr;
3865 if(parse_fragment(pptr, data, flags) && expected_len == data->fragment_len)
3866 TRACE("(%p %p %x): Valid fragment component %s len=%d.\n", builder, data, flags,
3867 debugstr_wn(data->fragment, data->fragment_len), data->fragment_len);
3868 else {
3869 TRACE("(%p %p %x): Invalid fragment component %s.\n", builder, data, flags,
3870 debugstr_wn(component, expected_len));
3871 return INET_E_INVALID_URL;
3875 return S_OK;
3878 static HRESULT validate_components(const UriBuilder *builder, parse_data *data, DWORD flags) {
3879 HRESULT hr;
3881 memset(data, 0, sizeof(parse_data));
3883 TRACE("(%p %p %x): Beginning to validate builder components.\n", builder, data, flags);
3885 hr = validate_scheme_name(builder, data, flags);
3886 if(FAILED(hr))
3887 return hr;
3889 /* Extra validation for file schemes. */
3890 if(data->scheme_type == URL_SCHEME_FILE) {
3891 if((builder->password || (builder->uri && builder->uri->userinfo_split > -1)) ||
3892 (builder->username || (builder->uri && builder->uri->userinfo_start > -1))) {
3893 TRACE("(%p %p %x): File schemes can't contain a username or password.\n",
3894 builder, data, flags);
3895 return INET_E_INVALID_URL;
3899 hr = validate_userinfo(builder, data, flags);
3900 if(FAILED(hr))
3901 return hr;
3903 hr = validate_host(builder, data, flags);
3904 if(FAILED(hr))
3905 return hr;
3907 setup_port(builder, data, flags);
3909 /* The URI is opaque if it doesn't have an authority component. */
3910 if(!data->is_relative)
3911 data->is_opaque = !data->username && !data->password && !data->host && !data->has_port;
3912 else
3913 data->is_opaque = !data->host && !data->has_port;
3915 hr = validate_path(builder, data, flags);
3916 if(FAILED(hr))
3917 return hr;
3919 hr = validate_query(builder, data, flags);
3920 if(FAILED(hr))
3921 return hr;
3923 hr = validate_fragment(builder, data, flags);
3924 if(FAILED(hr))
3925 return hr;
3927 TRACE("(%p %p %x): Finished validating builder components.\n", builder, data, flags);
3929 return S_OK;
3932 static void convert_to_dos_path(const WCHAR *path, DWORD path_len,
3933 WCHAR *output, DWORD *output_len)
3935 const WCHAR *ptr = path;
3937 if(path_len > 3 && *ptr == '/' && is_drive_path(path+1))
3938 /* Skip over the leading / before the drive path. */
3939 ++ptr;
3941 for(; ptr < path+path_len; ++ptr) {
3942 if(*ptr == '/') {
3943 if(output)
3944 *output++ = '\\';
3945 (*output_len)++;
3946 } else {
3947 if(output)
3948 *output++ = *ptr;
3949 (*output_len)++;
3954 /* Generates a raw uri string using the parse_data. */
3955 static DWORD generate_raw_uri(const parse_data *data, BSTR uri, DWORD flags) {
3956 DWORD length = 0;
3958 if(data->scheme) {
3959 if(uri) {
3960 memcpy(uri, data->scheme, data->scheme_len*sizeof(WCHAR));
3961 uri[data->scheme_len] = ':';
3963 length += data->scheme_len+1;
3966 if(!data->is_opaque) {
3967 /* For the "//" which appears before the authority component. */
3968 if(uri) {
3969 uri[length] = '/';
3970 uri[length+1] = '/';
3972 length += 2;
3974 /* Check if we need to add the "\\" before the host name
3975 * of a UNC server name in a DOS path.
3977 if(flags & RAW_URI_CONVERT_TO_DOS_PATH &&
3978 data->scheme_type == URL_SCHEME_FILE && data->host) {
3979 if(uri) {
3980 uri[length] = '\\';
3981 uri[length+1] = '\\';
3983 length += 2;
3987 if(data->username) {
3988 if(uri)
3989 memcpy(uri+length, data->username, data->username_len*sizeof(WCHAR));
3990 length += data->username_len;
3993 if(data->password) {
3994 if(uri) {
3995 uri[length] = ':';
3996 memcpy(uri+length+1, data->password, data->password_len*sizeof(WCHAR));
3998 length += data->password_len+1;
4001 if(data->password || data->username) {
4002 if(uri)
4003 uri[length] = '@';
4004 ++length;
4007 if(data->host) {
4008 /* IPv6 addresses get the brackets added around them if they don't already
4009 * have them.
4011 const BOOL add_brackets = data->host_type == Uri_HOST_IPV6 && *(data->host) != '[';
4012 if(add_brackets) {
4013 if(uri)
4014 uri[length] = '[';
4015 ++length;
4018 if(uri)
4019 memcpy(uri+length, data->host, data->host_len*sizeof(WCHAR));
4020 length += data->host_len;
4022 if(add_brackets) {
4023 if(uri)
4024 uri[length] = ']';
4025 length++;
4029 if(data->has_port) {
4030 /* The port isn't included in the raw uri if it's the default
4031 * port for the scheme type.
4033 DWORD i;
4034 BOOL is_default = FALSE;
4036 for(i = 0; i < sizeof(default_ports)/sizeof(default_ports[0]); ++i) {
4037 if(data->scheme_type == default_ports[i].scheme &&
4038 data->port_value == default_ports[i].port)
4039 is_default = TRUE;
4042 if(!is_default || flags & RAW_URI_FORCE_PORT_DISP) {
4043 if(uri)
4044 uri[length] = ':';
4045 ++length;
4047 if(uri)
4048 length += ui2str(uri+length, data->port_value);
4049 else
4050 length += ui2str(NULL, data->port_value);
4054 /* Check if a '/' should be added before the path for hierarchical URIs. */
4055 if(!data->is_opaque && data->path && *(data->path) != '/') {
4056 if(uri)
4057 uri[length] = '/';
4058 ++length;
4061 if(data->path) {
4062 if(!data->is_opaque && data->scheme_type == URL_SCHEME_FILE &&
4063 flags & RAW_URI_CONVERT_TO_DOS_PATH) {
4064 DWORD len = 0;
4066 if(uri)
4067 convert_to_dos_path(data->path, data->path_len, uri+length, &len);
4068 else
4069 convert_to_dos_path(data->path, data->path_len, NULL, &len);
4071 length += len;
4072 } else {
4073 if(uri)
4074 memcpy(uri+length, data->path, data->path_len*sizeof(WCHAR));
4075 length += data->path_len;
4079 if(data->query) {
4080 if(uri)
4081 memcpy(uri+length, data->query, data->query_len*sizeof(WCHAR));
4082 length += data->query_len;
4085 if(data->fragment) {
4086 if(uri)
4087 memcpy(uri+length, data->fragment, data->fragment_len*sizeof(WCHAR));
4088 length += data->fragment_len;
4091 if(uri)
4092 TRACE("(%p %p): Generated raw uri=%s len=%d\n", data, uri, debugstr_wn(uri, length), length);
4093 else
4094 TRACE("(%p %p): Computed raw uri len=%d\n", data, uri, length);
4096 return length;
4099 static HRESULT generate_uri(const UriBuilder *builder, const parse_data *data, Uri *uri, DWORD flags) {
4100 HRESULT hr;
4101 DWORD length = generate_raw_uri(data, NULL, 0);
4102 uri->raw_uri = SysAllocStringLen(NULL, length);
4103 if(!uri->raw_uri)
4104 return E_OUTOFMEMORY;
4106 generate_raw_uri(data, uri->raw_uri, 0);
4108 hr = canonicalize_uri(data, uri, flags);
4109 if(FAILED(hr)) {
4110 if(hr == E_INVALIDARG)
4111 return INET_E_INVALID_URL;
4112 return hr;
4115 uri->create_flags = flags;
4116 return S_OK;
4119 #define URI_THIS(iface) DEFINE_THIS(Uri, IUri, iface)
4121 static HRESULT WINAPI Uri_QueryInterface(IUri *iface, REFIID riid, void **ppv)
4123 Uri *This = URI_THIS(iface);
4125 if(IsEqualGUID(&IID_IUnknown, riid)) {
4126 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
4127 *ppv = URI(This);
4128 }else if(IsEqualGUID(&IID_IUri, riid)) {
4129 TRACE("(%p)->(IID_IUri %p)\n", This, ppv);
4130 *ppv = URI(This);
4131 }else if(IsEqualGUID(&IID_IUriObj, riid)) {
4132 TRACE("(%p)->(IID_IUriObj %p)\n", This, ppv);
4133 *ppv = This;
4134 return S_OK;
4135 }else {
4136 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
4137 *ppv = NULL;
4138 return E_NOINTERFACE;
4141 IUnknown_AddRef((IUnknown*)*ppv);
4142 return S_OK;
4145 static ULONG WINAPI Uri_AddRef(IUri *iface)
4147 Uri *This = URI_THIS(iface);
4148 LONG ref = InterlockedIncrement(&This->ref);
4150 TRACE("(%p) ref=%d\n", This, ref);
4152 return ref;
4155 static ULONG WINAPI Uri_Release(IUri *iface)
4157 Uri *This = URI_THIS(iface);
4158 LONG ref = InterlockedDecrement(&This->ref);
4160 TRACE("(%p) ref=%d\n", This, ref);
4162 if(!ref) {
4163 SysFreeString(This->raw_uri);
4164 heap_free(This->canon_uri);
4165 heap_free(This);
4168 return ref;
4171 static HRESULT WINAPI Uri_GetPropertyBSTR(IUri *iface, Uri_PROPERTY uriProp, BSTR *pbstrProperty, DWORD dwFlags)
4173 Uri *This = URI_THIS(iface);
4174 HRESULT hres;
4175 TRACE("(%p)->(%d %p %x)\n", This, uriProp, pbstrProperty, dwFlags);
4177 if(!pbstrProperty)
4178 return E_POINTER;
4180 if(uriProp > Uri_PROPERTY_STRING_LAST) {
4181 /* Windows allocates an empty BSTR for invalid Uri_PROPERTY's. */
4182 *pbstrProperty = SysAllocStringLen(NULL, 0);
4183 if(!(*pbstrProperty))
4184 return E_OUTOFMEMORY;
4186 /* It only returns S_FALSE for the ZONE property... */
4187 if(uriProp == Uri_PROPERTY_ZONE)
4188 return S_FALSE;
4189 else
4190 return S_OK;
4193 /* Don't have support for flags yet. */
4194 if(dwFlags) {
4195 FIXME("(%p)->(%d %p %x)\n", This, uriProp, pbstrProperty, dwFlags);
4196 return E_NOTIMPL;
4199 switch(uriProp) {
4200 case Uri_PROPERTY_ABSOLUTE_URI:
4201 if(This->display_modifiers & URI_DISPLAY_NO_ABSOLUTE_URI) {
4202 *pbstrProperty = SysAllocStringLen(NULL, 0);
4203 hres = S_FALSE;
4204 } else {
4205 if(This->scheme_type != URL_SCHEME_UNKNOWN && This->userinfo_start > -1) {
4206 if(This->userinfo_len == 0) {
4207 /* Don't include the '@' after the userinfo component. */
4208 *pbstrProperty = SysAllocStringLen(NULL, This->canon_len-1);
4209 hres = S_OK;
4210 if(*pbstrProperty) {
4211 /* Copy everything before it. */
4212 memcpy(*pbstrProperty, This->canon_uri, This->userinfo_start*sizeof(WCHAR));
4214 /* And everything after it. */
4215 memcpy(*pbstrProperty+This->userinfo_start, This->canon_uri+This->userinfo_start+1,
4216 (This->canon_len-This->userinfo_start-1)*sizeof(WCHAR));
4218 } else if(This->userinfo_split == 0 && This->userinfo_len == 1) {
4219 /* Don't include the ":@" */
4220 *pbstrProperty = SysAllocStringLen(NULL, This->canon_len-2);
4221 hres = S_OK;
4222 if(*pbstrProperty) {
4223 memcpy(*pbstrProperty, This->canon_uri, This->userinfo_start*sizeof(WCHAR));
4224 memcpy(*pbstrProperty+This->userinfo_start, This->canon_uri+This->userinfo_start+2,
4225 (This->canon_len-This->userinfo_start-2)*sizeof(WCHAR));
4227 } else {
4228 *pbstrProperty = SysAllocString(This->canon_uri);
4229 hres = S_OK;
4231 } else {
4232 *pbstrProperty = SysAllocString(This->canon_uri);
4233 hres = S_OK;
4237 if(!(*pbstrProperty))
4238 hres = E_OUTOFMEMORY;
4240 break;
4241 case Uri_PROPERTY_AUTHORITY:
4242 if(This->authority_start > -1) {
4243 if(This->port_offset > -1 && is_default_port(This->scheme_type, This->port) &&
4244 This->display_modifiers & URI_DISPLAY_NO_DEFAULT_PORT_AUTH)
4245 /* Don't include the port in the authority component. */
4246 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->authority_start, This->port_offset);
4247 else
4248 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->authority_start, This->authority_len);
4249 hres = S_OK;
4250 } else {
4251 *pbstrProperty = SysAllocStringLen(NULL, 0);
4252 hres = S_FALSE;
4255 if(!(*pbstrProperty))
4256 hres = E_OUTOFMEMORY;
4258 break;
4259 case Uri_PROPERTY_DISPLAY_URI:
4260 /* The Display URI contains everything except for the userinfo for known
4261 * scheme types.
4263 if(This->scheme_type != URL_SCHEME_UNKNOWN && This->userinfo_start > -1) {
4264 *pbstrProperty = SysAllocStringLen(NULL, This->canon_len-This->userinfo_len);
4266 if(*pbstrProperty) {
4267 /* Copy everything before the userinfo over. */
4268 memcpy(*pbstrProperty, This->canon_uri, This->userinfo_start*sizeof(WCHAR));
4269 /* Copy everything after the userinfo over. */
4270 memcpy(*pbstrProperty+This->userinfo_start,
4271 This->canon_uri+This->userinfo_start+This->userinfo_len+1,
4272 (This->canon_len-(This->userinfo_start+This->userinfo_len+1))*sizeof(WCHAR));
4274 } else
4275 *pbstrProperty = SysAllocString(This->canon_uri);
4277 if(!(*pbstrProperty))
4278 hres = E_OUTOFMEMORY;
4279 else
4280 hres = S_OK;
4282 break;
4283 case Uri_PROPERTY_DOMAIN:
4284 if(This->domain_offset > -1) {
4285 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->host_start+This->domain_offset,
4286 This->host_len-This->domain_offset);
4287 hres = S_OK;
4288 } else {
4289 *pbstrProperty = SysAllocStringLen(NULL, 0);
4290 hres = S_FALSE;
4293 if(!(*pbstrProperty))
4294 hres = E_OUTOFMEMORY;
4296 break;
4297 case Uri_PROPERTY_EXTENSION:
4298 if(This->extension_offset > -1) {
4299 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->path_start+This->extension_offset,
4300 This->path_len-This->extension_offset);
4301 hres = S_OK;
4302 } else {
4303 *pbstrProperty = SysAllocStringLen(NULL, 0);
4304 hres = S_FALSE;
4307 if(!(*pbstrProperty))
4308 hres = E_OUTOFMEMORY;
4310 break;
4311 case Uri_PROPERTY_FRAGMENT:
4312 if(This->fragment_start > -1) {
4313 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->fragment_start, This->fragment_len);
4314 hres = S_OK;
4315 } else {
4316 *pbstrProperty = SysAllocStringLen(NULL, 0);
4317 hres = S_FALSE;
4320 if(!(*pbstrProperty))
4321 hres = E_OUTOFMEMORY;
4323 break;
4324 case Uri_PROPERTY_HOST:
4325 if(This->host_start > -1) {
4326 /* The '[' and ']' aren't included for IPv6 addresses. */
4327 if(This->host_type == Uri_HOST_IPV6)
4328 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->host_start+1, This->host_len-2);
4329 else
4330 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->host_start, This->host_len);
4332 hres = S_OK;
4333 } else {
4334 *pbstrProperty = SysAllocStringLen(NULL, 0);
4335 hres = S_FALSE;
4338 if(!(*pbstrProperty))
4339 hres = E_OUTOFMEMORY;
4341 break;
4342 case Uri_PROPERTY_PASSWORD:
4343 if(This->userinfo_split > -1) {
4344 *pbstrProperty = SysAllocStringLen(
4345 This->canon_uri+This->userinfo_start+This->userinfo_split+1,
4346 This->userinfo_len-This->userinfo_split-1);
4347 hres = S_OK;
4348 } else {
4349 *pbstrProperty = SysAllocStringLen(NULL, 0);
4350 hres = S_FALSE;
4353 if(!(*pbstrProperty))
4354 return E_OUTOFMEMORY;
4356 break;
4357 case Uri_PROPERTY_PATH:
4358 if(This->path_start > -1) {
4359 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->path_start, This->path_len);
4360 hres = S_OK;
4361 } else {
4362 *pbstrProperty = SysAllocStringLen(NULL, 0);
4363 hres = S_FALSE;
4366 if(!(*pbstrProperty))
4367 hres = E_OUTOFMEMORY;
4369 break;
4370 case Uri_PROPERTY_PATH_AND_QUERY:
4371 if(This->path_start > -1) {
4372 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->path_start, This->path_len+This->query_len);
4373 hres = S_OK;
4374 } else if(This->query_start > -1) {
4375 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->query_start, This->query_len);
4376 hres = S_OK;
4377 } else {
4378 *pbstrProperty = SysAllocStringLen(NULL, 0);
4379 hres = S_FALSE;
4382 if(!(*pbstrProperty))
4383 hres = E_OUTOFMEMORY;
4385 break;
4386 case Uri_PROPERTY_QUERY:
4387 if(This->query_start > -1) {
4388 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->query_start, This->query_len);
4389 hres = S_OK;
4390 } else {
4391 *pbstrProperty = SysAllocStringLen(NULL, 0);
4392 hres = S_FALSE;
4395 if(!(*pbstrProperty))
4396 hres = E_OUTOFMEMORY;
4398 break;
4399 case Uri_PROPERTY_RAW_URI:
4400 *pbstrProperty = SysAllocString(This->raw_uri);
4401 if(!(*pbstrProperty))
4402 hres = E_OUTOFMEMORY;
4403 else
4404 hres = S_OK;
4405 break;
4406 case Uri_PROPERTY_SCHEME_NAME:
4407 if(This->scheme_start > -1) {
4408 *pbstrProperty = SysAllocStringLen(This->canon_uri + This->scheme_start, This->scheme_len);
4409 hres = S_OK;
4410 } else {
4411 *pbstrProperty = SysAllocStringLen(NULL, 0);
4412 hres = S_FALSE;
4415 if(!(*pbstrProperty))
4416 hres = E_OUTOFMEMORY;
4418 break;
4419 case Uri_PROPERTY_USER_INFO:
4420 if(This->userinfo_start > -1) {
4421 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->userinfo_start, This->userinfo_len);
4422 hres = S_OK;
4423 } else {
4424 *pbstrProperty = SysAllocStringLen(NULL, 0);
4425 hres = S_FALSE;
4428 if(!(*pbstrProperty))
4429 hres = E_OUTOFMEMORY;
4431 break;
4432 case Uri_PROPERTY_USER_NAME:
4433 if(This->userinfo_start > -1 && This->userinfo_split != 0) {
4434 /* If userinfo_split is set, that means a password exists
4435 * so the username is only from userinfo_start to userinfo_split.
4437 if(This->userinfo_split > -1) {
4438 *pbstrProperty = SysAllocStringLen(This->canon_uri + This->userinfo_start, This->userinfo_split);
4439 hres = S_OK;
4440 } else {
4441 *pbstrProperty = SysAllocStringLen(This->canon_uri + This->userinfo_start, This->userinfo_len);
4442 hres = S_OK;
4444 } else {
4445 *pbstrProperty = SysAllocStringLen(NULL, 0);
4446 hres = S_FALSE;
4449 if(!(*pbstrProperty))
4450 return E_OUTOFMEMORY;
4452 break;
4453 default:
4454 FIXME("(%p)->(%d %p %x)\n", This, uriProp, pbstrProperty, dwFlags);
4455 hres = E_NOTIMPL;
4458 return hres;
4461 static HRESULT WINAPI Uri_GetPropertyLength(IUri *iface, Uri_PROPERTY uriProp, DWORD *pcchProperty, DWORD dwFlags)
4463 Uri *This = URI_THIS(iface);
4464 HRESULT hres;
4465 TRACE("(%p)->(%d %p %x)\n", This, uriProp, pcchProperty, dwFlags);
4467 if(!pcchProperty)
4468 return E_INVALIDARG;
4470 /* Can only return a length for a property if it's a string. */
4471 if(uriProp > Uri_PROPERTY_STRING_LAST)
4472 return E_INVALIDARG;
4474 /* Don't have support for flags yet. */
4475 if(dwFlags) {
4476 FIXME("(%p)->(%d %p %x)\n", This, uriProp, pcchProperty, dwFlags);
4477 return E_NOTIMPL;
4480 switch(uriProp) {
4481 case Uri_PROPERTY_ABSOLUTE_URI:
4482 if(This->display_modifiers & URI_DISPLAY_NO_ABSOLUTE_URI) {
4483 *pcchProperty = 0;
4484 hres = S_FALSE;
4485 } else {
4486 if(This->scheme_type != URL_SCHEME_UNKNOWN) {
4487 if(This->userinfo_start > -1 && This->userinfo_len == 0)
4488 /* Don't include the '@' in the length. */
4489 *pcchProperty = This->canon_len-1;
4490 else if(This->userinfo_start > -1 && This->userinfo_len == 1 &&
4491 This->userinfo_split == 0)
4492 /* Don't include the ":@" in the length. */
4493 *pcchProperty = This->canon_len-2;
4494 else
4495 *pcchProperty = This->canon_len;
4496 } else
4497 *pcchProperty = This->canon_len;
4499 hres = S_OK;
4502 break;
4503 case Uri_PROPERTY_AUTHORITY:
4504 if(This->port_offset > -1 &&
4505 This->display_modifiers & URI_DISPLAY_NO_DEFAULT_PORT_AUTH &&
4506 is_default_port(This->scheme_type, This->port))
4507 /* Only count up until the port in the authority. */
4508 *pcchProperty = This->port_offset;
4509 else
4510 *pcchProperty = This->authority_len;
4511 hres = (This->authority_start > -1) ? S_OK : S_FALSE;
4512 break;
4513 case Uri_PROPERTY_DISPLAY_URI:
4514 if(This->scheme_type != URL_SCHEME_UNKNOWN && This->userinfo_start > -1)
4515 *pcchProperty = This->canon_len-This->userinfo_len-1;
4516 else
4517 *pcchProperty = This->canon_len;
4519 hres = S_OK;
4520 break;
4521 case Uri_PROPERTY_DOMAIN:
4522 if(This->domain_offset > -1)
4523 *pcchProperty = This->host_len - This->domain_offset;
4524 else
4525 *pcchProperty = 0;
4527 hres = (This->domain_offset > -1) ? S_OK : S_FALSE;
4528 break;
4529 case Uri_PROPERTY_EXTENSION:
4530 if(This->extension_offset > -1) {
4531 *pcchProperty = This->path_len - This->extension_offset;
4532 hres = S_OK;
4533 } else {
4534 *pcchProperty = 0;
4535 hres = S_FALSE;
4538 break;
4539 case Uri_PROPERTY_FRAGMENT:
4540 *pcchProperty = This->fragment_len;
4541 hres = (This->fragment_start > -1) ? S_OK : S_FALSE;
4542 break;
4543 case Uri_PROPERTY_HOST:
4544 *pcchProperty = This->host_len;
4546 /* '[' and ']' aren't included in the length. */
4547 if(This->host_type == Uri_HOST_IPV6)
4548 *pcchProperty -= 2;
4550 hres = (This->host_start > -1) ? S_OK : S_FALSE;
4551 break;
4552 case Uri_PROPERTY_PASSWORD:
4553 *pcchProperty = (This->userinfo_split > -1) ? This->userinfo_len-This->userinfo_split-1 : 0;
4554 hres = (This->userinfo_split > -1) ? S_OK : S_FALSE;
4555 break;
4556 case Uri_PROPERTY_PATH:
4557 *pcchProperty = This->path_len;
4558 hres = (This->path_start > -1) ? S_OK : S_FALSE;
4559 break;
4560 case Uri_PROPERTY_PATH_AND_QUERY:
4561 *pcchProperty = This->path_len+This->query_len;
4562 hres = (This->path_start > -1 || This->query_start > -1) ? S_OK : S_FALSE;
4563 break;
4564 case Uri_PROPERTY_QUERY:
4565 *pcchProperty = This->query_len;
4566 hres = (This->query_start > -1) ? S_OK : S_FALSE;
4567 break;
4568 case Uri_PROPERTY_RAW_URI:
4569 *pcchProperty = SysStringLen(This->raw_uri);
4570 hres = S_OK;
4571 break;
4572 case Uri_PROPERTY_SCHEME_NAME:
4573 *pcchProperty = This->scheme_len;
4574 hres = (This->scheme_start > -1) ? S_OK : S_FALSE;
4575 break;
4576 case Uri_PROPERTY_USER_INFO:
4577 *pcchProperty = This->userinfo_len;
4578 hres = (This->userinfo_start > -1) ? S_OK : S_FALSE;
4579 break;
4580 case Uri_PROPERTY_USER_NAME:
4581 *pcchProperty = (This->userinfo_split > -1) ? This->userinfo_split : This->userinfo_len;
4582 if(This->userinfo_split == 0)
4583 hres = S_FALSE;
4584 else
4585 hres = (This->userinfo_start > -1) ? S_OK : S_FALSE;
4586 break;
4587 default:
4588 FIXME("(%p)->(%d %p %x)\n", This, uriProp, pcchProperty, dwFlags);
4589 hres = E_NOTIMPL;
4592 return hres;
4595 static HRESULT WINAPI Uri_GetPropertyDWORD(IUri *iface, Uri_PROPERTY uriProp, DWORD *pcchProperty, DWORD dwFlags)
4597 Uri *This = URI_THIS(iface);
4598 HRESULT hres;
4600 TRACE("(%p)->(%d %p %x)\n", This, uriProp, pcchProperty, dwFlags);
4602 if(!pcchProperty)
4603 return E_INVALIDARG;
4605 /* Microsoft's implementation for the ZONE property of a URI seems to be lacking...
4606 * From what I can tell, instead of checking which URLZONE the URI belongs to it
4607 * simply assigns URLZONE_INVALID and returns E_NOTIMPL. This also applies to the GetZone
4608 * function.
4610 if(uriProp == Uri_PROPERTY_ZONE) {
4611 *pcchProperty = URLZONE_INVALID;
4612 return E_NOTIMPL;
4615 if(uriProp < Uri_PROPERTY_DWORD_START) {
4616 *pcchProperty = 0;
4617 return E_INVALIDARG;
4620 switch(uriProp) {
4621 case Uri_PROPERTY_HOST_TYPE:
4622 *pcchProperty = This->host_type;
4623 hres = S_OK;
4624 break;
4625 case Uri_PROPERTY_PORT:
4626 if(!This->has_port) {
4627 *pcchProperty = 0;
4628 hres = S_FALSE;
4629 } else {
4630 *pcchProperty = This->port;
4631 hres = S_OK;
4634 break;
4635 case Uri_PROPERTY_SCHEME:
4636 *pcchProperty = This->scheme_type;
4637 hres = S_OK;
4638 break;
4639 default:
4640 FIXME("(%p)->(%d %p %x)\n", This, uriProp, pcchProperty, dwFlags);
4641 hres = E_NOTIMPL;
4644 return hres;
4647 static HRESULT WINAPI Uri_HasProperty(IUri *iface, Uri_PROPERTY uriProp, BOOL *pfHasProperty)
4649 Uri *This = URI_THIS(iface);
4650 TRACE("(%p)->(%d %p)\n", This, uriProp, pfHasProperty);
4652 if(!pfHasProperty)
4653 return E_INVALIDARG;
4655 switch(uriProp) {
4656 case Uri_PROPERTY_ABSOLUTE_URI:
4657 *pfHasProperty = !(This->display_modifiers & URI_DISPLAY_NO_ABSOLUTE_URI);
4658 break;
4659 case Uri_PROPERTY_AUTHORITY:
4660 *pfHasProperty = This->authority_start > -1;
4661 break;
4662 case Uri_PROPERTY_DISPLAY_URI:
4663 *pfHasProperty = TRUE;
4664 break;
4665 case Uri_PROPERTY_DOMAIN:
4666 *pfHasProperty = This->domain_offset > -1;
4667 break;
4668 case Uri_PROPERTY_EXTENSION:
4669 *pfHasProperty = This->extension_offset > -1;
4670 break;
4671 case Uri_PROPERTY_FRAGMENT:
4672 *pfHasProperty = This->fragment_start > -1;
4673 break;
4674 case Uri_PROPERTY_HOST:
4675 *pfHasProperty = This->host_start > -1;
4676 break;
4677 case Uri_PROPERTY_PASSWORD:
4678 *pfHasProperty = This->userinfo_split > -1;
4679 break;
4680 case Uri_PROPERTY_PATH:
4681 *pfHasProperty = This->path_start > -1;
4682 break;
4683 case Uri_PROPERTY_PATH_AND_QUERY:
4684 *pfHasProperty = (This->path_start > -1 || This->query_start > -1);
4685 break;
4686 case Uri_PROPERTY_QUERY:
4687 *pfHasProperty = This->query_start > -1;
4688 break;
4689 case Uri_PROPERTY_RAW_URI:
4690 *pfHasProperty = TRUE;
4691 break;
4692 case Uri_PROPERTY_SCHEME_NAME:
4693 *pfHasProperty = This->scheme_start > -1;
4694 break;
4695 case Uri_PROPERTY_USER_INFO:
4696 *pfHasProperty = This->userinfo_start > -1;
4697 break;
4698 case Uri_PROPERTY_USER_NAME:
4699 if(This->userinfo_split == 0)
4700 *pfHasProperty = FALSE;
4701 else
4702 *pfHasProperty = This->userinfo_start > -1;
4703 break;
4704 case Uri_PROPERTY_HOST_TYPE:
4705 *pfHasProperty = TRUE;
4706 break;
4707 case Uri_PROPERTY_PORT:
4708 *pfHasProperty = This->has_port;
4709 break;
4710 case Uri_PROPERTY_SCHEME:
4711 *pfHasProperty = TRUE;
4712 break;
4713 case Uri_PROPERTY_ZONE:
4714 *pfHasProperty = FALSE;
4715 break;
4716 default:
4717 FIXME("(%p)->(%d %p): Unsupported property type.\n", This, uriProp, pfHasProperty);
4718 return E_NOTIMPL;
4721 return S_OK;
4724 static HRESULT WINAPI Uri_GetAbsoluteUri(IUri *iface, BSTR *pstrAbsoluteUri)
4726 TRACE("(%p)->(%p)\n", iface, pstrAbsoluteUri);
4727 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_ABSOLUTE_URI, pstrAbsoluteUri, 0);
4730 static HRESULT WINAPI Uri_GetAuthority(IUri *iface, BSTR *pstrAuthority)
4732 TRACE("(%p)->(%p)\n", iface, pstrAuthority);
4733 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_AUTHORITY, pstrAuthority, 0);
4736 static HRESULT WINAPI Uri_GetDisplayUri(IUri *iface, BSTR *pstrDisplayUri)
4738 TRACE("(%p)->(%p)\n", iface, pstrDisplayUri);
4739 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_DISPLAY_URI, pstrDisplayUri, 0);
4742 static HRESULT WINAPI Uri_GetDomain(IUri *iface, BSTR *pstrDomain)
4744 TRACE("(%p)->(%p)\n", iface, pstrDomain);
4745 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_DOMAIN, pstrDomain, 0);
4748 static HRESULT WINAPI Uri_GetExtension(IUri *iface, BSTR *pstrExtension)
4750 TRACE("(%p)->(%p)\n", iface, pstrExtension);
4751 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_EXTENSION, pstrExtension, 0);
4754 static HRESULT WINAPI Uri_GetFragment(IUri *iface, BSTR *pstrFragment)
4756 TRACE("(%p)->(%p)\n", iface, pstrFragment);
4757 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_FRAGMENT, pstrFragment, 0);
4760 static HRESULT WINAPI Uri_GetHost(IUri *iface, BSTR *pstrHost)
4762 TRACE("(%p)->(%p)\n", iface, pstrHost);
4763 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_HOST, pstrHost, 0);
4766 static HRESULT WINAPI Uri_GetPassword(IUri *iface, BSTR *pstrPassword)
4768 TRACE("(%p)->(%p)\n", iface, pstrPassword);
4769 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_PASSWORD, pstrPassword, 0);
4772 static HRESULT WINAPI Uri_GetPath(IUri *iface, BSTR *pstrPath)
4774 TRACE("(%p)->(%p)\n", iface, pstrPath);
4775 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_PATH, pstrPath, 0);
4778 static HRESULT WINAPI Uri_GetPathAndQuery(IUri *iface, BSTR *pstrPathAndQuery)
4780 TRACE("(%p)->(%p)\n", iface, pstrPathAndQuery);
4781 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_PATH_AND_QUERY, pstrPathAndQuery, 0);
4784 static HRESULT WINAPI Uri_GetQuery(IUri *iface, BSTR *pstrQuery)
4786 TRACE("(%p)->(%p)\n", iface, pstrQuery);
4787 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_QUERY, pstrQuery, 0);
4790 static HRESULT WINAPI Uri_GetRawUri(IUri *iface, BSTR *pstrRawUri)
4792 TRACE("(%p)->(%p)\n", iface, pstrRawUri);
4793 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_RAW_URI, pstrRawUri, 0);
4796 static HRESULT WINAPI Uri_GetSchemeName(IUri *iface, BSTR *pstrSchemeName)
4798 TRACE("(%p)->(%p)\n", iface, pstrSchemeName);
4799 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_SCHEME_NAME, pstrSchemeName, 0);
4802 static HRESULT WINAPI Uri_GetUserInfo(IUri *iface, BSTR *pstrUserInfo)
4804 TRACE("(%p)->(%p)\n", iface, pstrUserInfo);
4805 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_USER_INFO, pstrUserInfo, 0);
4808 static HRESULT WINAPI Uri_GetUserName(IUri *iface, BSTR *pstrUserName)
4810 TRACE("(%p)->(%p)\n", iface, pstrUserName);
4811 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_USER_NAME, pstrUserName, 0);
4814 static HRESULT WINAPI Uri_GetHostType(IUri *iface, DWORD *pdwHostType)
4816 TRACE("(%p)->(%p)\n", iface, pdwHostType);
4817 return IUri_GetPropertyDWORD(iface, Uri_PROPERTY_HOST_TYPE, pdwHostType, 0);
4820 static HRESULT WINAPI Uri_GetPort(IUri *iface, DWORD *pdwPort)
4822 TRACE("(%p)->(%p)\n", iface, pdwPort);
4823 return IUri_GetPropertyDWORD(iface, Uri_PROPERTY_PORT, pdwPort, 0);
4826 static HRESULT WINAPI Uri_GetScheme(IUri *iface, DWORD *pdwScheme)
4828 Uri *This = URI_THIS(iface);
4829 TRACE("(%p)->(%p)\n", This, pdwScheme);
4830 return IUri_GetPropertyDWORD(iface, Uri_PROPERTY_SCHEME, pdwScheme, 0);
4833 static HRESULT WINAPI Uri_GetZone(IUri *iface, DWORD *pdwZone)
4835 TRACE("(%p)->(%p)\n", iface, pdwZone);
4836 return IUri_GetPropertyDWORD(iface, Uri_PROPERTY_ZONE,pdwZone, 0);
4839 static HRESULT WINAPI Uri_GetProperties(IUri *iface, DWORD *pdwProperties)
4841 Uri *This = URI_THIS(iface);
4842 TRACE("(%p)->(%p)\n", This, pdwProperties);
4844 if(!pdwProperties)
4845 return E_INVALIDARG;
4847 /* All URIs have these. */
4848 *pdwProperties = Uri_HAS_DISPLAY_URI|Uri_HAS_RAW_URI|Uri_HAS_SCHEME|Uri_HAS_HOST_TYPE;
4850 if(!(This->display_modifiers & URI_DISPLAY_NO_ABSOLUTE_URI))
4851 *pdwProperties |= Uri_HAS_ABSOLUTE_URI;
4853 if(This->scheme_start > -1)
4854 *pdwProperties |= Uri_HAS_SCHEME_NAME;
4856 if(This->authority_start > -1) {
4857 *pdwProperties |= Uri_HAS_AUTHORITY;
4858 if(This->userinfo_start > -1) {
4859 *pdwProperties |= Uri_HAS_USER_INFO;
4860 if(This->userinfo_split != 0)
4861 *pdwProperties |= Uri_HAS_USER_NAME;
4863 if(This->userinfo_split > -1)
4864 *pdwProperties |= Uri_HAS_PASSWORD;
4865 if(This->host_start > -1)
4866 *pdwProperties |= Uri_HAS_HOST;
4867 if(This->domain_offset > -1)
4868 *pdwProperties |= Uri_HAS_DOMAIN;
4871 if(This->has_port)
4872 *pdwProperties |= Uri_HAS_PORT;
4873 if(This->path_start > -1)
4874 *pdwProperties |= Uri_HAS_PATH|Uri_HAS_PATH_AND_QUERY;
4875 if(This->query_start > -1)
4876 *pdwProperties |= Uri_HAS_QUERY|Uri_HAS_PATH_AND_QUERY;
4878 if(This->extension_offset > -1)
4879 *pdwProperties |= Uri_HAS_EXTENSION;
4881 if(This->fragment_start > -1)
4882 *pdwProperties |= Uri_HAS_FRAGMENT;
4884 return S_OK;
4887 static HRESULT WINAPI Uri_IsEqual(IUri *iface, IUri *pUri, BOOL *pfEqual)
4889 Uri *This = URI_THIS(iface);
4890 Uri *other;
4892 TRACE("(%p)->(%p %p)\n", This, pUri, pfEqual);
4894 if(!pfEqual)
4895 return E_POINTER;
4897 if(!pUri) {
4898 *pfEqual = FALSE;
4900 /* For some reason Windows returns S_OK here... */
4901 return S_OK;
4904 /* Try to convert it to a Uri (allows for a more simple comparison). */
4905 if((other = get_uri_obj(pUri)))
4906 *pfEqual = are_equal_simple(This, other);
4907 else {
4908 /* Do it the hard way. */
4909 FIXME("(%p)->(%p %p) No support for unknown IUri's yet.\n", iface, pUri, pfEqual);
4910 return E_NOTIMPL;
4913 return S_OK;
4916 #undef URI_THIS
4918 static const IUriVtbl UriVtbl = {
4919 Uri_QueryInterface,
4920 Uri_AddRef,
4921 Uri_Release,
4922 Uri_GetPropertyBSTR,
4923 Uri_GetPropertyLength,
4924 Uri_GetPropertyDWORD,
4925 Uri_HasProperty,
4926 Uri_GetAbsoluteUri,
4927 Uri_GetAuthority,
4928 Uri_GetDisplayUri,
4929 Uri_GetDomain,
4930 Uri_GetExtension,
4931 Uri_GetFragment,
4932 Uri_GetHost,
4933 Uri_GetPassword,
4934 Uri_GetPath,
4935 Uri_GetPathAndQuery,
4936 Uri_GetQuery,
4937 Uri_GetRawUri,
4938 Uri_GetSchemeName,
4939 Uri_GetUserInfo,
4940 Uri_GetUserName,
4941 Uri_GetHostType,
4942 Uri_GetPort,
4943 Uri_GetScheme,
4944 Uri_GetZone,
4945 Uri_GetProperties,
4946 Uri_IsEqual
4949 static Uri* create_uri_obj(void) {
4950 Uri *ret = heap_alloc_zero(sizeof(Uri));
4951 if(ret) {
4952 ret->lpIUriVtbl = &UriVtbl;
4953 ret->ref = 1;
4956 return ret;
4959 /***********************************************************************
4960 * CreateUri (urlmon.@)
4962 * Creates a new IUri object using the URI represented by pwzURI. This function
4963 * parses and validates the components of pwzURI and then canonicalizes the
4964 * parsed components.
4966 * PARAMS
4967 * pwzURI [I] The URI to parse, validate, and canonicalize.
4968 * dwFlags [I] Flags which can affect how the parsing/canonicalization is performed.
4969 * dwReserved [I] Reserved (not used).
4970 * ppURI [O] The resulting IUri after parsing/canonicalization occurs.
4972 * RETURNS
4973 * Success: Returns S_OK. ppURI contains the pointer to the newly allocated IUri.
4974 * Failure: E_INVALIDARG if there's invalid flag combinations in dwFlags, or an
4975 * invalid parameters, or pwzURI doesn't represnt a valid URI.
4976 * E_OUTOFMEMORY if any memory allocation fails.
4978 * NOTES
4979 * Default flags:
4980 * Uri_CREATE_CANONICALIZE, Uri_CREATE_DECODE_EXTRA_INFO, Uri_CREATE_CRACK_UNKNOWN_SCHEMES,
4981 * Uri_CREATE_PRE_PROCESS_HTML_URI, Uri_CREATE_NO_IE_SETTINGS.
4983 HRESULT WINAPI CreateUri(LPCWSTR pwzURI, DWORD dwFlags, DWORD_PTR dwReserved, IUri **ppURI)
4985 const DWORD supported_flags = Uri_CREATE_ALLOW_RELATIVE|Uri_CREATE_ALLOW_IMPLICIT_WILDCARD_SCHEME|
4986 Uri_CREATE_ALLOW_IMPLICIT_FILE_SCHEME|Uri_CREATE_NO_CANONICALIZE|Uri_CREATE_CANONICALIZE|
4987 Uri_CREATE_DECODE_EXTRA_INFO|Uri_CREATE_NO_DECODE_EXTRA_INFO|Uri_CREATE_CRACK_UNKNOWN_SCHEMES|
4988 Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES|Uri_CREATE_PRE_PROCESS_HTML_URI|Uri_CREATE_NO_PRE_PROCESS_HTML_URI|
4989 Uri_CREATE_NO_IE_SETTINGS|Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS|Uri_CREATE_FILE_USE_DOS_PATH;
4990 Uri *ret;
4991 HRESULT hr;
4992 parse_data data;
4994 TRACE("(%s %x %x %p)\n", debugstr_w(pwzURI), dwFlags, (DWORD)dwReserved, ppURI);
4996 if(!ppURI)
4997 return E_INVALIDARG;
4999 if(!pwzURI || !*pwzURI) {
5000 *ppURI = NULL;
5001 return E_INVALIDARG;
5004 /* Check for invalid flags. */
5005 if(has_invalid_flag_combination(dwFlags)) {
5006 *ppURI = NULL;
5007 return E_INVALIDARG;
5010 /* Currently unsupported. */
5011 if(dwFlags & ~supported_flags)
5012 FIXME("Ignoring unsupported flag(s) %x\n", dwFlags & ~supported_flags);
5014 ret = create_uri_obj();
5015 if(!ret) {
5016 *ppURI = NULL;
5017 return E_OUTOFMEMORY;
5020 /* Explicitly set the default flags if it doesn't cause a flag conflict. */
5021 apply_default_flags(&dwFlags);
5023 /* Pre process the URI, unless told otherwise. */
5024 if(!(dwFlags & Uri_CREATE_NO_PRE_PROCESS_HTML_URI))
5025 ret->raw_uri = pre_process_uri(pwzURI);
5026 else
5027 ret->raw_uri = SysAllocString(pwzURI);
5029 if(!ret->raw_uri) {
5030 heap_free(ret);
5031 return E_OUTOFMEMORY;
5034 memset(&data, 0, sizeof(parse_data));
5035 data.uri = ret->raw_uri;
5037 /* Validate and parse the URI into it's components. */
5038 if(!parse_uri(&data, dwFlags)) {
5039 /* Encountered an unsupported or invalid URI */
5040 IUri_Release(URI(ret));
5041 *ppURI = NULL;
5042 return E_INVALIDARG;
5045 /* Canonicalize the URI. */
5046 hr = canonicalize_uri(&data, ret, dwFlags);
5047 if(FAILED(hr)) {
5048 IUri_Release(URI(ret));
5049 *ppURI = NULL;
5050 return hr;
5053 ret->create_flags = dwFlags;
5055 *ppURI = URI(ret);
5056 return S_OK;
5059 /***********************************************************************
5060 * CreateUriWithFragment (urlmon.@)
5062 * Creates a new IUri object. This is almost the same as CreateUri, expect that
5063 * it allows you to explicitly specify a fragment (pwzFragment) for pwzURI.
5065 * PARAMS
5066 * pwzURI [I] The URI to parse and perform canonicalization on.
5067 * pwzFragment [I] The explict fragment string which should be added to pwzURI.
5068 * dwFlags [I] The flags which will be passed to CreateUri.
5069 * dwReserved [I] Reserved (not used).
5070 * ppURI [O] The resulting IUri after parsing/canonicalization.
5072 * RETURNS
5073 * Success: S_OK. ppURI contains the pointer to the newly allocated IUri.
5074 * Failure: E_INVALIDARG if pwzURI already contains a fragment and pwzFragment
5075 * isn't NULL. Will also return E_INVALIDARG for the same reasons as
5076 * CreateUri will. E_OUTOFMEMORY if any allocations fail.
5078 HRESULT WINAPI CreateUriWithFragment(LPCWSTR pwzURI, LPCWSTR pwzFragment, DWORD dwFlags,
5079 DWORD_PTR dwReserved, IUri **ppURI)
5081 HRESULT hres;
5082 TRACE("(%s %s %x %x %p)\n", debugstr_w(pwzURI), debugstr_w(pwzFragment), dwFlags, (DWORD)dwReserved, ppURI);
5084 if(!ppURI)
5085 return E_INVALIDARG;
5087 if(!pwzURI) {
5088 *ppURI = NULL;
5089 return E_INVALIDARG;
5092 /* Check if a fragment should be appended to the URI string. */
5093 if(pwzFragment) {
5094 WCHAR *uriW;
5095 DWORD uri_len, frag_len;
5096 BOOL add_pound;
5098 /* Check if the original URI already has a fragment component. */
5099 if(StrChrW(pwzURI, '#')) {
5100 *ppURI = NULL;
5101 return E_INVALIDARG;
5104 uri_len = lstrlenW(pwzURI);
5105 frag_len = lstrlenW(pwzFragment);
5107 /* If the fragment doesn't start with a '#', one will be added. */
5108 add_pound = *pwzFragment != '#';
5110 if(add_pound)
5111 uriW = heap_alloc((uri_len+frag_len+2)*sizeof(WCHAR));
5112 else
5113 uriW = heap_alloc((uri_len+frag_len+1)*sizeof(WCHAR));
5115 if(!uriW)
5116 return E_OUTOFMEMORY;
5118 memcpy(uriW, pwzURI, uri_len*sizeof(WCHAR));
5119 if(add_pound)
5120 uriW[uri_len++] = '#';
5121 memcpy(uriW+uri_len, pwzFragment, (frag_len+1)*sizeof(WCHAR));
5123 hres = CreateUri(uriW, dwFlags, 0, ppURI);
5125 heap_free(uriW);
5126 } else
5127 /* A fragment string wasn't specified, so just forward the call. */
5128 hres = CreateUri(pwzURI, dwFlags, 0, ppURI);
5130 return hres;
5133 static HRESULT build_uri(const UriBuilder *builder, IUri **uri, DWORD create_flags,
5134 DWORD use_orig_flags, DWORD encoding_mask)
5136 HRESULT hr;
5137 parse_data data;
5138 Uri *ret;
5140 if(!uri)
5141 return E_POINTER;
5143 if(encoding_mask && (!builder->uri || builder->modified_props)) {
5144 *uri = NULL;
5145 return E_NOTIMPL;
5148 /* Decide what flags should be used when creating the Uri. */
5149 if((use_orig_flags & UriBuilder_USE_ORIGINAL_FLAGS) && builder->uri)
5150 create_flags = builder->uri->create_flags;
5151 else {
5152 if(has_invalid_flag_combination(create_flags)) {
5153 *uri = NULL;
5154 return E_INVALIDARG;
5157 /* Set the default flags if they don't cause a conflict. */
5158 apply_default_flags(&create_flags);
5161 /* Return the base IUri if no changes have been made and the create_flags match. */
5162 if(builder->uri && !builder->modified_props && builder->uri->create_flags == create_flags) {
5163 *uri = URI(builder->uri);
5164 IUri_AddRef(*uri);
5165 return S_OK;
5168 hr = validate_components(builder, &data, create_flags);
5169 if(FAILED(hr)) {
5170 *uri = NULL;
5171 return hr;
5174 ret = create_uri_obj();
5175 if(!ret) {
5176 *uri = NULL;
5177 return E_OUTOFMEMORY;
5180 hr = generate_uri(builder, &data, ret, create_flags);
5181 if(FAILED(hr)) {
5182 IUri_Release(URI(ret));
5183 *uri = NULL;
5184 return hr;
5187 *uri = URI(ret);
5188 return S_OK;
5191 #define URIBUILDER_THIS(iface) DEFINE_THIS(UriBuilder, IUriBuilder, iface)
5193 static HRESULT WINAPI UriBuilder_QueryInterface(IUriBuilder *iface, REFIID riid, void **ppv)
5195 UriBuilder *This = URIBUILDER_THIS(iface);
5197 if(IsEqualGUID(&IID_IUnknown, riid)) {
5198 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
5199 *ppv = URIBUILDER(This);
5200 }else if(IsEqualGUID(&IID_IUriBuilder, riid)) {
5201 TRACE("(%p)->(IID_IUri %p)\n", This, ppv);
5202 *ppv = URIBUILDER(This);
5203 }else {
5204 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
5205 *ppv = NULL;
5206 return E_NOINTERFACE;
5209 IUnknown_AddRef((IUnknown*)*ppv);
5210 return S_OK;
5213 static ULONG WINAPI UriBuilder_AddRef(IUriBuilder *iface)
5215 UriBuilder *This = URIBUILDER_THIS(iface);
5216 LONG ref = InterlockedIncrement(&This->ref);
5218 TRACE("(%p) ref=%d\n", This, ref);
5220 return ref;
5223 static ULONG WINAPI UriBuilder_Release(IUriBuilder *iface)
5225 UriBuilder *This = URIBUILDER_THIS(iface);
5226 LONG ref = InterlockedDecrement(&This->ref);
5228 TRACE("(%p) ref=%d\n", This, ref);
5230 if(!ref) {
5231 if(This->uri) IUri_Release(URI(This->uri));
5232 heap_free(This->fragment);
5233 heap_free(This->host);
5234 heap_free(This->password);
5235 heap_free(This->path);
5236 heap_free(This->query);
5237 heap_free(This->scheme);
5238 heap_free(This->username);
5239 heap_free(This);
5242 return ref;
5245 static HRESULT WINAPI UriBuilder_CreateUriSimple(IUriBuilder *iface,
5246 DWORD dwAllowEncodingPropertyMask,
5247 DWORD_PTR dwReserved,
5248 IUri **ppIUri)
5250 UriBuilder *This = URIBUILDER_THIS(iface);
5251 HRESULT hr;
5252 TRACE("(%p)->(%d %d %p)\n", This, dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
5254 hr = build_uri(This, ppIUri, 0, UriBuilder_USE_ORIGINAL_FLAGS, dwAllowEncodingPropertyMask);
5255 if(hr == E_NOTIMPL)
5256 FIXME("(%p)->(%d %d %p)\n", This, dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
5257 return hr;
5260 static HRESULT WINAPI UriBuilder_CreateUri(IUriBuilder *iface,
5261 DWORD dwCreateFlags,
5262 DWORD dwAllowEncodingPropertyMask,
5263 DWORD_PTR dwReserved,
5264 IUri **ppIUri)
5266 UriBuilder *This = URIBUILDER_THIS(iface);
5267 HRESULT hr;
5268 TRACE("(%p)->(0x%08x %d %d %p)\n", This, dwCreateFlags, dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
5270 if(dwCreateFlags == -1)
5271 hr = build_uri(This, ppIUri, 0, UriBuilder_USE_ORIGINAL_FLAGS, dwAllowEncodingPropertyMask);
5272 else
5273 hr = build_uri(This, ppIUri, dwCreateFlags, 0, dwAllowEncodingPropertyMask);
5275 if(hr == E_NOTIMPL)
5276 FIXME("(%p)->(0x%08x %d %d %p)\n", This, dwCreateFlags, dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
5277 return hr;
5280 static HRESULT WINAPI UriBuilder_CreateUriWithFlags(IUriBuilder *iface,
5281 DWORD dwCreateFlags,
5282 DWORD dwUriBuilderFlags,
5283 DWORD dwAllowEncodingPropertyMask,
5284 DWORD_PTR dwReserved,
5285 IUri **ppIUri)
5287 UriBuilder *This = URIBUILDER_THIS(iface);
5288 HRESULT hr;
5289 TRACE("(%p)->(0x%08x 0x%08x %d %d %p)\n", This, dwCreateFlags, dwUriBuilderFlags,
5290 dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
5292 hr = build_uri(This, ppIUri, dwCreateFlags, dwUriBuilderFlags, dwAllowEncodingPropertyMask);
5293 if(hr == E_NOTIMPL)
5294 FIXME("(%p)->(0x%08x 0x%08x %d %d %p)\n", This, dwCreateFlags, dwUriBuilderFlags,
5295 dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
5296 return hr;
5299 static HRESULT WINAPI UriBuilder_GetIUri(IUriBuilder *iface, IUri **ppIUri)
5301 UriBuilder *This = URIBUILDER_THIS(iface);
5302 TRACE("(%p)->(%p)\n", This, ppIUri);
5304 if(!ppIUri)
5305 return E_POINTER;
5307 if(This->uri) {
5308 IUri *uri = URI(This->uri);
5309 IUri_AddRef(uri);
5310 *ppIUri = uri;
5311 } else
5312 *ppIUri = NULL;
5314 return S_OK;
5317 static HRESULT WINAPI UriBuilder_SetIUri(IUriBuilder *iface, IUri *pIUri)
5319 UriBuilder *This = URIBUILDER_THIS(iface);
5320 TRACE("(%p)->(%p)\n", This, pIUri);
5322 if(pIUri) {
5323 Uri *uri;
5325 if((uri = get_uri_obj(pIUri))) {
5326 /* Only reset the builder if it's Uri isn't the same as
5327 * the Uri passed to the function.
5329 if(This->uri != uri) {
5330 reset_builder(This);
5332 This->uri = uri;
5333 if(uri->has_port)
5334 This->port = uri->port;
5336 IUri_AddRef(pIUri);
5338 } else {
5339 FIXME("(%p)->(%p) Unknown IUri types not supported yet.\n", This, pIUri);
5340 return E_NOTIMPL;
5342 } else if(This->uri)
5343 /* Only reset the builder if it's Uri isn't NULL. */
5344 reset_builder(This);
5346 return S_OK;
5349 static HRESULT WINAPI UriBuilder_GetFragment(IUriBuilder *iface, DWORD *pcchFragment, LPCWSTR *ppwzFragment)
5351 UriBuilder *This = URIBUILDER_THIS(iface);
5352 TRACE("(%p)->(%p %p)\n", This, pcchFragment, ppwzFragment);
5354 if(!This->uri || This->uri->fragment_start == -1 || This->modified_props & Uri_HAS_FRAGMENT)
5355 return get_builder_component(&This->fragment, &This->fragment_len, NULL, 0, ppwzFragment, pcchFragment);
5356 else
5357 return get_builder_component(&This->fragment, &This->fragment_len, This->uri->canon_uri+This->uri->fragment_start,
5358 This->uri->fragment_len, ppwzFragment, pcchFragment);
5361 static HRESULT WINAPI UriBuilder_GetHost(IUriBuilder *iface, DWORD *pcchHost, LPCWSTR *ppwzHost)
5363 UriBuilder *This = URIBUILDER_THIS(iface);
5364 TRACE("(%p)->(%p %p)\n", This, pcchHost, ppwzHost);
5366 if(!This->uri || This->uri->host_start == -1 || This->modified_props & Uri_HAS_HOST)
5367 return get_builder_component(&This->host, &This->host_len, NULL, 0, ppwzHost, pcchHost);
5368 else {
5369 if(This->uri->host_type == Uri_HOST_IPV6)
5370 /* Don't include the '[' and ']' around the address. */
5371 return get_builder_component(&This->host, &This->host_len, This->uri->canon_uri+This->uri->host_start+1,
5372 This->uri->host_len-2, ppwzHost, pcchHost);
5373 else
5374 return get_builder_component(&This->host, &This->host_len, This->uri->canon_uri+This->uri->host_start,
5375 This->uri->host_len, ppwzHost, pcchHost);
5379 static HRESULT WINAPI UriBuilder_GetPassword(IUriBuilder *iface, DWORD *pcchPassword, LPCWSTR *ppwzPassword)
5381 UriBuilder *This = URIBUILDER_THIS(iface);
5382 TRACE("(%p)->(%p %p)\n", This, pcchPassword, ppwzPassword);
5384 if(!This->uri || This->uri->userinfo_split == -1 || This->modified_props & Uri_HAS_PASSWORD)
5385 return get_builder_component(&This->password, &This->password_len, NULL, 0, ppwzPassword, pcchPassword);
5386 else {
5387 const WCHAR *start = This->uri->canon_uri+This->uri->userinfo_start+This->uri->userinfo_split+1;
5388 DWORD len = This->uri->userinfo_len-This->uri->userinfo_split-1;
5389 return get_builder_component(&This->password, &This->password_len, start, len, ppwzPassword, pcchPassword);
5393 static HRESULT WINAPI UriBuilder_GetPath(IUriBuilder *iface, DWORD *pcchPath, LPCWSTR *ppwzPath)
5395 UriBuilder *This = URIBUILDER_THIS(iface);
5396 TRACE("(%p)->(%p %p)\n", This, pcchPath, ppwzPath);
5398 if(!This->uri || This->uri->path_start == -1 || This->modified_props & Uri_HAS_PATH)
5399 return get_builder_component(&This->path, &This->path_len, NULL, 0, ppwzPath, pcchPath);
5400 else
5401 return get_builder_component(&This->path, &This->path_len, This->uri->canon_uri+This->uri->path_start,
5402 This->uri->path_len, ppwzPath, pcchPath);
5405 static HRESULT WINAPI UriBuilder_GetPort(IUriBuilder *iface, BOOL *pfHasPort, DWORD *pdwPort)
5407 UriBuilder *This = URIBUILDER_THIS(iface);
5408 TRACE("(%p)->(%p %p)\n", This, pfHasPort, pdwPort);
5410 if(!pfHasPort) {
5411 if(pdwPort)
5412 *pdwPort = 0;
5413 return E_POINTER;
5416 if(!pdwPort) {
5417 *pfHasPort = FALSE;
5418 return E_POINTER;
5421 *pfHasPort = This->has_port;
5422 *pdwPort = This->port;
5423 return S_OK;
5426 static HRESULT WINAPI UriBuilder_GetQuery(IUriBuilder *iface, DWORD *pcchQuery, LPCWSTR *ppwzQuery)
5428 UriBuilder *This = URIBUILDER_THIS(iface);
5429 TRACE("(%p)->(%p %p)\n", This, pcchQuery, ppwzQuery);
5431 if(!This->uri || This->uri->query_start == -1 || This->modified_props & Uri_HAS_QUERY)
5432 return get_builder_component(&This->query, &This->query_len, NULL, 0, ppwzQuery, pcchQuery);
5433 else
5434 return get_builder_component(&This->query, &This->query_len, This->uri->canon_uri+This->uri->query_start,
5435 This->uri->query_len, ppwzQuery, pcchQuery);
5438 static HRESULT WINAPI UriBuilder_GetSchemeName(IUriBuilder *iface, DWORD *pcchSchemeName, LPCWSTR *ppwzSchemeName)
5440 UriBuilder *This = URIBUILDER_THIS(iface);
5441 TRACE("(%p)->(%p %p)\n", This, pcchSchemeName, ppwzSchemeName);
5443 if(!This->uri || This->uri->scheme_start == -1 || This->modified_props & Uri_HAS_SCHEME_NAME)
5444 return get_builder_component(&This->scheme, &This->scheme_len, NULL, 0, ppwzSchemeName, pcchSchemeName);
5445 else
5446 return get_builder_component(&This->scheme, &This->scheme_len, This->uri->canon_uri+This->uri->scheme_start,
5447 This->uri->scheme_len, ppwzSchemeName, pcchSchemeName);
5450 static HRESULT WINAPI UriBuilder_GetUserName(IUriBuilder *iface, DWORD *pcchUserName, LPCWSTR *ppwzUserName)
5452 UriBuilder *This = URIBUILDER_THIS(iface);
5453 TRACE("(%p)->(%p %p)\n", This, pcchUserName, ppwzUserName);
5455 if(!This->uri || This->uri->userinfo_start == -1 || This->uri->userinfo_split == 0 ||
5456 This->modified_props & Uri_HAS_USER_NAME)
5457 return get_builder_component(&This->username, &This->username_len, NULL, 0, ppwzUserName, pcchUserName);
5458 else {
5459 const WCHAR *start = This->uri->canon_uri+This->uri->userinfo_start;
5461 /* Check if there's a password in the userinfo section. */
5462 if(This->uri->userinfo_split > -1)
5463 /* Don't include the password. */
5464 return get_builder_component(&This->username, &This->username_len, start,
5465 This->uri->userinfo_split, ppwzUserName, pcchUserName);
5466 else
5467 return get_builder_component(&This->username, &This->username_len, start,
5468 This->uri->userinfo_len, ppwzUserName, pcchUserName);
5472 static HRESULT WINAPI UriBuilder_SetFragment(IUriBuilder *iface, LPCWSTR pwzNewValue)
5474 UriBuilder *This = URIBUILDER_THIS(iface);
5475 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
5476 return set_builder_component(&This->fragment, &This->fragment_len, pwzNewValue, '#',
5477 &This->modified_props, Uri_HAS_FRAGMENT);
5480 static HRESULT WINAPI UriBuilder_SetHost(IUriBuilder *iface, LPCWSTR pwzNewValue)
5482 UriBuilder *This = URIBUILDER_THIS(iface);
5483 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
5485 /* Host name can't be set to NULL. */
5486 if(!pwzNewValue)
5487 return E_INVALIDARG;
5489 return set_builder_component(&This->host, &This->host_len, pwzNewValue, 0,
5490 &This->modified_props, Uri_HAS_HOST);
5493 static HRESULT WINAPI UriBuilder_SetPassword(IUriBuilder *iface, LPCWSTR pwzNewValue)
5495 UriBuilder *This = URIBUILDER_THIS(iface);
5496 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
5497 return set_builder_component(&This->password, &This->password_len, pwzNewValue, 0,
5498 &This->modified_props, Uri_HAS_PASSWORD);
5501 static HRESULT WINAPI UriBuilder_SetPath(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->path, &This->path_len, pwzNewValue, 0,
5506 &This->modified_props, Uri_HAS_PATH);
5509 static HRESULT WINAPI UriBuilder_SetPort(IUriBuilder *iface, BOOL fHasPort, DWORD dwNewValue)
5511 UriBuilder *This = URIBUILDER_THIS(iface);
5512 TRACE("(%p)->(%d %d)\n", This, fHasPort, dwNewValue);
5514 This->has_port = fHasPort;
5515 This->port = dwNewValue;
5516 This->modified_props |= Uri_HAS_PORT;
5517 return S_OK;
5520 static HRESULT WINAPI UriBuilder_SetQuery(IUriBuilder *iface, LPCWSTR pwzNewValue)
5522 UriBuilder *This = URIBUILDER_THIS(iface);
5523 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
5524 return set_builder_component(&This->query, &This->query_len, pwzNewValue, '?',
5525 &This->modified_props, Uri_HAS_QUERY);
5528 static HRESULT WINAPI UriBuilder_SetSchemeName(IUriBuilder *iface, LPCWSTR pwzNewValue)
5530 UriBuilder *This = URIBUILDER_THIS(iface);
5531 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
5533 /* Only set the scheme name if it's not NULL or empty. */
5534 if(!pwzNewValue || !*pwzNewValue)
5535 return E_INVALIDARG;
5537 return set_builder_component(&This->scheme, &This->scheme_len, pwzNewValue, 0,
5538 &This->modified_props, Uri_HAS_SCHEME_NAME);
5541 static HRESULT WINAPI UriBuilder_SetUserName(IUriBuilder *iface, LPCWSTR pwzNewValue)
5543 UriBuilder *This = URIBUILDER_THIS(iface);
5544 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
5545 return set_builder_component(&This->username, &This->username_len, pwzNewValue, 0,
5546 &This->modified_props, Uri_HAS_USER_NAME);
5549 static HRESULT WINAPI UriBuilder_RemoveProperties(IUriBuilder *iface, DWORD dwPropertyMask)
5551 const DWORD accepted_flags = Uri_HAS_AUTHORITY|Uri_HAS_DOMAIN|Uri_HAS_EXTENSION|Uri_HAS_FRAGMENT|Uri_HAS_HOST|
5552 Uri_HAS_PASSWORD|Uri_HAS_PATH|Uri_HAS_PATH_AND_QUERY|Uri_HAS_QUERY|
5553 Uri_HAS_USER_INFO|Uri_HAS_USER_NAME;
5555 UriBuilder *This = URIBUILDER_THIS(iface);
5556 TRACE("(%p)->(0x%08x)\n", This, dwPropertyMask);
5558 if(dwPropertyMask & ~accepted_flags)
5559 return E_INVALIDARG;
5561 if(dwPropertyMask & Uri_HAS_FRAGMENT)
5562 UriBuilder_SetFragment(iface, NULL);
5564 /* Even though you can't set the host name to NULL or an
5565 * empty string, you can still remove it... for some reason.
5567 if(dwPropertyMask & Uri_HAS_HOST)
5568 set_builder_component(&This->host, &This->host_len, NULL, 0,
5569 &This->modified_props, Uri_HAS_HOST);
5571 if(dwPropertyMask & Uri_HAS_PASSWORD)
5572 UriBuilder_SetPassword(iface, NULL);
5574 if(dwPropertyMask & Uri_HAS_PATH)
5575 UriBuilder_SetPath(iface, NULL);
5577 if(dwPropertyMask & Uri_HAS_PORT)
5578 UriBuilder_SetPort(iface, FALSE, 0);
5580 if(dwPropertyMask & Uri_HAS_QUERY)
5581 UriBuilder_SetQuery(iface, NULL);
5583 if(dwPropertyMask & Uri_HAS_USER_NAME)
5584 UriBuilder_SetUserName(iface, NULL);
5586 return S_OK;
5589 static HRESULT WINAPI UriBuilder_HasBeenModified(IUriBuilder *iface, BOOL *pfModified)
5591 UriBuilder *This = URIBUILDER_THIS(iface);
5592 TRACE("(%p)->(%p)\n", This, pfModified);
5594 if(!pfModified)
5595 return E_POINTER;
5597 *pfModified = This->modified_props > 0;
5598 return S_OK;
5601 #undef URIBUILDER_THIS
5603 static const IUriBuilderVtbl UriBuilderVtbl = {
5604 UriBuilder_QueryInterface,
5605 UriBuilder_AddRef,
5606 UriBuilder_Release,
5607 UriBuilder_CreateUriSimple,
5608 UriBuilder_CreateUri,
5609 UriBuilder_CreateUriWithFlags,
5610 UriBuilder_GetIUri,
5611 UriBuilder_SetIUri,
5612 UriBuilder_GetFragment,
5613 UriBuilder_GetHost,
5614 UriBuilder_GetPassword,
5615 UriBuilder_GetPath,
5616 UriBuilder_GetPort,
5617 UriBuilder_GetQuery,
5618 UriBuilder_GetSchemeName,
5619 UriBuilder_GetUserName,
5620 UriBuilder_SetFragment,
5621 UriBuilder_SetHost,
5622 UriBuilder_SetPassword,
5623 UriBuilder_SetPath,
5624 UriBuilder_SetPort,
5625 UriBuilder_SetQuery,
5626 UriBuilder_SetSchemeName,
5627 UriBuilder_SetUserName,
5628 UriBuilder_RemoveProperties,
5629 UriBuilder_HasBeenModified,
5632 /***********************************************************************
5633 * CreateIUriBuilder (urlmon.@)
5635 HRESULT WINAPI CreateIUriBuilder(IUri *pIUri, DWORD dwFlags, DWORD_PTR dwReserved, IUriBuilder **ppIUriBuilder)
5637 UriBuilder *ret;
5639 TRACE("(%p %x %x %p)\n", pIUri, dwFlags, (DWORD)dwReserved, ppIUriBuilder);
5641 if(!ppIUriBuilder)
5642 return E_POINTER;
5644 ret = heap_alloc_zero(sizeof(UriBuilder));
5645 if(!ret)
5646 return E_OUTOFMEMORY;
5648 ret->lpIUriBuilderVtbl = &UriBuilderVtbl;
5649 ret->ref = 1;
5651 if(pIUri) {
5652 Uri *uri;
5654 if((uri = get_uri_obj(pIUri))) {
5655 IUri_AddRef(pIUri);
5656 ret->uri = uri;
5658 if(uri->has_port)
5659 /* Windows doesn't set 'has_port' to TRUE in this case. */
5660 ret->port = uri->port;
5662 } else {
5663 heap_free(ret);
5664 *ppIUriBuilder = NULL;
5665 FIXME("(%p %x %x %p): Unknown IUri types not supported yet.\n", pIUri, dwFlags,
5666 (DWORD)dwReserved, ppIUriBuilder);
5667 return E_NOTIMPL;
5671 *ppIUriBuilder = URIBUILDER(ret);
5672 return S_OK;
5675 /* Merges the base path with the relative path and stores the resulting path
5676 * and path len in 'result' and 'result_len'.
5678 static HRESULT merge_paths(parse_data *data, const WCHAR *base, DWORD base_len, const WCHAR *relative,
5679 DWORD relative_len, WCHAR **result, DWORD *result_len, DWORD flags)
5681 const WCHAR *end = NULL;
5682 DWORD base_copy_len = 0;
5683 WCHAR *ptr;
5685 if(base_len) {
5686 /* Find the characters the will be copied over from
5687 * the base path.
5689 end = str_last_of(base, base+(base_len-1), '/');
5690 if(!end && data->scheme_type == URL_SCHEME_FILE)
5691 /* Try looking for a '\\'. */
5692 end = str_last_of(base, base+(base_len-1), '\\');
5695 if(end) {
5696 base_copy_len = (end+1)-base;
5697 *result = heap_alloc((base_copy_len+relative_len+1)*sizeof(WCHAR));
5698 } else
5699 *result = heap_alloc((relative_len+1)*sizeof(WCHAR));
5701 if(!(*result)) {
5702 *result_len = 0;
5703 return E_OUTOFMEMORY;
5706 ptr = *result;
5707 if(end) {
5708 memcpy(ptr, base, base_copy_len*sizeof(WCHAR));
5709 ptr += base_copy_len;
5712 memcpy(ptr, relative, relative_len*sizeof(WCHAR));
5713 ptr += relative_len;
5714 *ptr = '\0';
5716 *result_len = (ptr-*result);
5717 return S_OK;
5720 static HRESULT combine_uri(Uri *base, Uri *relative, DWORD flags, IUri **result) {
5721 Uri *ret;
5722 HRESULT hr;
5723 parse_data data;
5724 DWORD create_flags = 0, len = 0;
5726 memset(&data, 0, sizeof(parse_data));
5728 /* Base case is when the relative Uri has a scheme name,
5729 * if it does, then 'result' will contain the same data
5730 * as the relative Uri.
5732 if(relative->scheme_start > -1) {
5733 data.uri = SysAllocString(relative->raw_uri);
5734 if(!data.uri) {
5735 *result = NULL;
5736 return E_OUTOFMEMORY;
5739 parse_uri(&data, 0);
5741 ret = create_uri_obj();
5742 if(!ret) {
5743 *result = NULL;
5744 return E_OUTOFMEMORY;
5747 ret->raw_uri = data.uri;
5748 hr = canonicalize_uri(&data, ret, 0);
5749 if(FAILED(hr)) {
5750 IUri_Release(URI(ret));
5751 *result = NULL;
5752 return hr;
5755 apply_default_flags(&create_flags);
5756 ret->create_flags = create_flags;
5758 *result = URI(ret);
5759 } else {
5760 WCHAR *path = NULL;
5761 DWORD raw_flags = 0;
5763 if(base->scheme_start > -1) {
5764 data.scheme = base->canon_uri+base->scheme_start;
5765 data.scheme_len = base->scheme_len;
5766 data.scheme_type = base->scheme_type;
5767 } else {
5768 data.is_relative = TRUE;
5769 data.scheme_type = URL_SCHEME_UNKNOWN;
5770 create_flags |= Uri_CREATE_ALLOW_RELATIVE;
5773 if(base->authority_start > -1) {
5774 if(base->userinfo_start > -1 && base->userinfo_split != 0) {
5775 data.username = base->canon_uri+base->userinfo_start;
5776 data.username_len = (base->userinfo_split > -1) ? base->userinfo_split : base->userinfo_len;
5779 if(base->userinfo_split > -1) {
5780 data.password = base->canon_uri+base->userinfo_start+base->userinfo_split+1;
5781 data.password_len = base->userinfo_len-base->userinfo_split-1;
5784 if(base->host_start > -1) {
5785 data.host = base->canon_uri+base->host_start;
5786 data.host_len = base->host_len;
5787 data.host_type = base->host_type;
5790 if(base->has_port) {
5791 data.has_port = TRUE;
5792 data.port_value = base->port;
5794 } else if(base->scheme_type != URL_SCHEME_FILE)
5795 data.is_opaque = TRUE;
5797 if(relative->path_start == -1 || !relative->path_len) {
5798 if(base->path_start > -1) {
5799 data.path = base->canon_uri+base->path_start;
5800 data.path_len = base->path_len;
5801 } else if((base->path_start == -1 || !base->path_len) && !data.is_opaque) {
5802 /* Just set the path as a '/' if the base didn't have
5803 * one and if it's an hierarchical URI.
5805 static const WCHAR slashW[] = {'/',0};
5806 data.path = slashW;
5807 data.path_len = 1;
5810 if(relative->query_start > -1) {
5811 data.query = relative->canon_uri+relative->query_start;
5812 data.query_len = relative->query_len;
5813 } else if(base->query_start > -1) {
5814 data.query = base->canon_uri+base->query_start;
5815 data.query_len = base->query_len;
5817 } else {
5818 const WCHAR *ptr, **pptr;
5819 DWORD path_offset = 0, path_len = 0;
5821 /* There's two possibilities on what will happen to the path component
5822 * of the result IUri. First, if the relative path begins with a '/'
5823 * then the resulting path will just be the relative path. Second, if
5824 * relative path doesn't begin with a '/' then the base path and relative
5825 * path are merged together.
5827 if(relative->path_len && *(relative->canon_uri+relative->path_start) == '/') {
5828 WCHAR *tmp = NULL;
5829 BOOL copy_drive_path = FALSE;
5831 /* If the relative IUri's path starts with a '/', then we
5832 * don't use the base IUri's path. Unless the base IUri
5833 * is a file URI, in which case it uses the drive path of
5834 * the base IUri (if it has any) in the new path.
5836 if(base->scheme_type == URL_SCHEME_FILE) {
5837 if(base->path_len > 3 && *(base->canon_uri+base->path_start) == '/' &&
5838 is_drive_path(base->canon_uri+base->path_start+1)) {
5839 path_len += 3;
5840 copy_drive_path = TRUE;
5844 path_len += relative->path_len;
5846 path = heap_alloc((path_len+1)*sizeof(WCHAR));
5847 if(!path) {
5848 *result = NULL;
5849 return E_OUTOFMEMORY;
5852 tmp = path;
5854 /* Copy the base paths, drive path over. */
5855 if(copy_drive_path) {
5856 memcpy(tmp, base->canon_uri+base->path_start, 3*sizeof(WCHAR));
5857 tmp += 3;
5860 memcpy(tmp, relative->canon_uri+relative->path_start, relative->path_len*sizeof(WCHAR));
5861 path[path_len] = '\0';
5862 } else {
5863 /* Merge the base path with the relative path. */
5864 hr = merge_paths(&data, base->canon_uri+base->path_start, base->path_len,
5865 relative->canon_uri+relative->path_start, relative->path_len,
5866 &path, &path_len, flags);
5867 if(FAILED(hr)) {
5868 *result = NULL;
5869 return hr;
5872 /* If the resulting IUri is a file URI, the drive path isn't
5873 * reduced out when the dot segments are removed.
5875 if(path_len >= 3 && data.scheme_type == URL_SCHEME_FILE && !data.host) {
5876 if(*path == '/' && is_drive_path(path+1))
5877 path_offset = 2;
5878 else if(is_drive_path(path))
5879 path_offset = 1;
5883 /* Check if the dot segments need to be removed from the path. */
5884 if(!(flags & URL_DONT_SIMPLIFY) && !data.is_opaque) {
5885 DWORD offset = (path_offset > 0) ? path_offset+1 : 0;
5886 DWORD new_len = remove_dot_segments(path+offset,path_len-offset);
5888 if(new_len != path_len) {
5889 WCHAR *tmp = heap_realloc(path, (path_offset+new_len+1)*sizeof(WCHAR));
5890 if(!tmp) {
5891 heap_free(path);
5892 *result = NULL;
5893 return E_OUTOFMEMORY;
5896 tmp[new_len+offset] = '\0';
5897 path = tmp;
5898 path_len = new_len+offset;
5902 /* Make sure the path component is valid. */
5903 ptr = path;
5904 pptr = &ptr;
5905 if((data.is_opaque && !parse_path_opaque(pptr, &data, 0)) ||
5906 (!data.is_opaque && !parse_path_hierarchical(pptr, &data, 0))) {
5907 heap_free(path);
5908 *result = NULL;
5909 return E_INVALIDARG;
5913 if(relative->fragment_start > -1) {
5914 data.fragment = relative->canon_uri+relative->fragment_start;
5915 data.fragment_len = relative->fragment_len;
5918 if(flags & URL_DONT_SIMPLIFY)
5919 raw_flags |= RAW_URI_FORCE_PORT_DISP;
5920 if(flags & URL_FILE_USE_PATHURL)
5921 raw_flags |= RAW_URI_CONVERT_TO_DOS_PATH;
5923 len = generate_raw_uri(&data, data.uri, raw_flags);
5924 data.uri = SysAllocStringLen(NULL, len);
5925 if(!data.uri) {
5926 heap_free(path);
5927 *result = NULL;
5928 return E_OUTOFMEMORY;
5931 generate_raw_uri(&data, data.uri, raw_flags);
5933 ret = create_uri_obj();
5934 if(!ret) {
5935 SysFreeString(data.uri);
5936 heap_free(path);
5937 *result = NULL;
5938 return E_OUTOFMEMORY;
5941 if(flags & URL_DONT_SIMPLIFY)
5942 create_flags |= Uri_CREATE_NO_CANONICALIZE;
5943 if(flags & URL_FILE_USE_PATHURL)
5944 create_flags |= Uri_CREATE_FILE_USE_DOS_PATH;
5946 ret->raw_uri = data.uri;
5947 hr = canonicalize_uri(&data, ret, create_flags);
5948 if(FAILED(hr)) {
5949 IUri_Release(URI(ret));
5950 *result = NULL;
5951 return hr;
5954 if(flags & URL_DONT_SIMPLIFY)
5955 ret->display_modifiers |= URI_DISPLAY_NO_DEFAULT_PORT_AUTH;
5957 apply_default_flags(&create_flags);
5958 ret->create_flags = create_flags;
5959 *result = URI(ret);
5961 heap_free(path);
5964 return S_OK;
5967 /***********************************************************************
5968 * CoInternetCombineIUri (urlmon.@)
5970 HRESULT WINAPI CoInternetCombineIUri(IUri *pBaseUri, IUri *pRelativeUri, DWORD dwCombineFlags,
5971 IUri **ppCombinedUri, DWORD_PTR dwReserved)
5973 HRESULT hr;
5974 IInternetProtocolInfo *info;
5975 Uri *relative, *base;
5976 TRACE("(%p %p %x %p %x)\n", pBaseUri, pRelativeUri, dwCombineFlags, ppCombinedUri, (DWORD)dwReserved);
5978 if(!ppCombinedUri)
5979 return E_INVALIDARG;
5981 if(!pBaseUri || !pRelativeUri) {
5982 *ppCombinedUri = NULL;
5983 return E_INVALIDARG;
5986 relative = get_uri_obj(pRelativeUri);
5987 base = get_uri_obj(pBaseUri);
5988 if(!relative || !base) {
5989 *ppCombinedUri = NULL;
5990 FIXME("(%p %p %x %p %x) Unknown IUri types not supported yet.\n",
5991 pBaseUri, pRelativeUri, dwCombineFlags, ppCombinedUri, (DWORD)dwReserved);
5992 return E_NOTIMPL;
5995 info = get_protocol_info(base->canon_uri);
5996 if(info) {
5997 WCHAR result[INTERNET_MAX_URL_LENGTH+1];
5998 DWORD result_len = 0;
6000 hr = IInternetProtocolInfo_CombineUrl(info, base->canon_uri, relative->canon_uri, dwCombineFlags,
6001 result, INTERNET_MAX_URL_LENGTH+1, &result_len, 0);
6002 IInternetProtocolInfo_Release(info);
6003 if(SUCCEEDED(hr)) {
6004 hr = CreateUri(result, Uri_CREATE_ALLOW_RELATIVE, 0, ppCombinedUri);
6005 if(SUCCEEDED(hr))
6006 return hr;
6010 return combine_uri(base, relative, dwCombineFlags, ppCombinedUri);