urlmon: Implemented case when relative URI has a path that doesn't start with '/'.
[wine/multimedia.git] / dlls / urlmon / uri.c
blob3bbe54fbc6303e0f382d21dfd79cf14f9d6862c5
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 BOOL inside_brackets = has_start_bracket;
1556 BOOL ignore_col = extras & IGNORE_PORT_DELIMITER;
1558 /* We have to be careful with file schemes. */
1559 if(data->scheme_type == URL_SCHEME_FILE) {
1560 /* This is because an implicit file scheme could be "C:\\test" and it
1561 * would trick this function into thinking the host is "C", when after
1562 * canonicalization the host would end up being an empty string. A drive
1563 * path can also have a '|' instead of a ':' after the drive letter.
1565 if(is_drive_path(*ptr)) {
1566 /* Regular old drive paths don't have a host type (or host name). */
1567 data->host_type = Uri_HOST_UNKNOWN;
1568 data->host = *ptr;
1569 data->host_len = 0;
1570 return TRUE;
1571 } else if(is_unc_path(*ptr))
1572 /* Skip past the "\\" of a UNC path. */
1573 *ptr += 2;
1576 data->host = *ptr;
1578 while(!is_auth_delim(**ptr, known_scheme)) {
1579 if(**ptr == ':' && !ignore_col) {
1580 /* We can ignore ':' if were inside brackets.*/
1581 if(!inside_brackets) {
1582 const WCHAR *tmp = (*ptr)++;
1584 /* Attempt to parse the port. */
1585 if(!parse_port(ptr, data, flags)) {
1586 /* Windows expects there to be a valid port for known scheme types. */
1587 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
1588 *ptr = data->host;
1589 data->host = NULL;
1590 TRACE("(%p %p %x %x): Expected valid port\n", ptr, data, flags, extras);
1591 return FALSE;
1592 } else
1593 /* Windows gives up on trying to parse a port when it
1594 * encounters 1 invalid port.
1596 ignore_col = TRUE;
1597 } else {
1598 data->host_len = tmp - data->host;
1599 break;
1602 } else if(**ptr == '%' && known_scheme) {
1603 /* Has to be a legit % encoded value. */
1604 if(!check_pct_encoded(ptr)) {
1605 *ptr = data->host;
1606 data->host = NULL;
1607 return FALSE;
1608 } else
1609 continue;
1610 } else if(**ptr == ']')
1611 inside_brackets = FALSE;
1612 else if(**ptr == '[')
1613 inside_brackets = TRUE;
1615 ++(*ptr);
1618 if(has_start_bracket) {
1619 /* Make sure the last character of the host wasn't a ']'. */
1620 if(*(*ptr-1) == ']') {
1621 TRACE("(%p %p %x %x): Expected an IP literal inside of the host\n",
1622 ptr, data, flags, extras);
1623 *ptr = data->host;
1624 data->host = NULL;
1625 return FALSE;
1629 /* Don't overwrite our length if we found a port earlier. */
1630 if(!data->port)
1631 data->host_len = *ptr - data->host;
1633 /* If the host is empty, then it's an unknown host type. */
1634 if(data->host_len == 0)
1635 data->host_type = Uri_HOST_UNKNOWN;
1636 else
1637 data->host_type = Uri_HOST_DNS;
1639 TRACE("(%p %p %x %x): Parsed reg-name. host=%s len=%d\n", ptr, data, flags, extras,
1640 debugstr_wn(data->host, data->host_len), data->host_len);
1641 return TRUE;
1644 /* Attempts to parse an IPv6 address out of the URI.
1646 * IPv6address = 6( h16 ":" ) ls32
1647 * / "::" 5( h16 ":" ) ls32
1648 * / [ h16 ] "::" 4( h16 ":" ) ls32
1649 * / [ *1( h16 ":" ) h16 ] "::" 3( h16 ":" ) ls32
1650 * / [ *2( h16 ":" ) h16 ] "::" 2( h16 ":" ) ls32
1651 * / [ *3( h16 ":" ) h16 ] "::" h16 ":" ls32
1652 * / [ *4( h16 ":" ) h16 ] "::" ls32
1653 * / [ *5( h16 ":" ) h16 ] "::" h16
1654 * / [ *6( h16 ":" ) h16 ] "::"
1656 * ls32 = ( h16 ":" h16 ) / IPv4address
1657 * ; least-significant 32 bits of address.
1659 * h16 = 1*4HEXDIG
1660 * ; 16 bits of address represented in hexadecimal.
1662 * Modeled after google-url's 'DoParseIPv6' function.
1664 static BOOL parse_ipv6address(const WCHAR **ptr, parse_data *data, DWORD flags) {
1665 const WCHAR *start, *cur_start;
1666 ipv6_address ip;
1668 start = cur_start = *ptr;
1669 memset(&ip, 0, sizeof(ipv6_address));
1671 for(;; ++(*ptr)) {
1672 /* Check if we're on the last character of the host. */
1673 BOOL is_end = (is_auth_delim(**ptr, data->scheme_type != URL_SCHEME_UNKNOWN)
1674 || **ptr == ']');
1676 BOOL is_split = (**ptr == ':');
1677 BOOL is_elision = (is_split && !is_end && *(*ptr+1) == ':');
1679 /* Check if we're at the end of a component, or
1680 * if we're at the end of the IPv6 address.
1682 if(is_split || is_end) {
1683 DWORD cur_len = 0;
1685 cur_len = *ptr - cur_start;
1687 /* h16 can't have a length > 4. */
1688 if(cur_len > 4) {
1689 *ptr = start;
1691 TRACE("(%p %p %x): h16 component to long.\n",
1692 ptr, data, flags);
1693 return FALSE;
1696 if(cur_len == 0) {
1697 /* An h16 component can't have the length of 0 unless
1698 * the elision is at the beginning of the address, or
1699 * at the end of the address.
1701 if(!((*ptr == start && is_elision) ||
1702 (is_end && (*ptr-2) == ip.elision))) {
1703 *ptr = start;
1704 TRACE("(%p %p %x): IPv6 component cannot have a length of 0.\n",
1705 ptr, data, flags);
1706 return FALSE;
1710 if(cur_len > 0) {
1711 /* An IPv6 address can have no more than 8 h16 components. */
1712 if(ip.h16_count >= 8) {
1713 *ptr = start;
1714 TRACE("(%p %p %x): Not a IPv6 address, to many h16 components.\n",
1715 ptr, data, flags);
1716 return FALSE;
1719 ip.components[ip.h16_count].str = cur_start;
1720 ip.components[ip.h16_count].len = cur_len;
1722 TRACE("(%p %p %x): Found h16 component %s, len=%d, h16_count=%d\n",
1723 ptr, data, flags, debugstr_wn(cur_start, cur_len), cur_len,
1724 ip.h16_count);
1725 ++ip.h16_count;
1729 if(is_end)
1730 break;
1732 if(is_elision) {
1733 /* A IPv6 address can only have 1 elision ('::'). */
1734 if(ip.elision) {
1735 *ptr = start;
1737 TRACE("(%p %p %x): IPv6 address cannot have 2 elisions.\n",
1738 ptr, data, flags);
1739 return FALSE;
1742 ip.elision = *ptr;
1743 ++(*ptr);
1746 if(is_split)
1747 cur_start = *ptr+1;
1748 else {
1749 if(!check_ipv4address(ptr, TRUE)) {
1750 if(!is_hexdigit(**ptr)) {
1751 /* Not a valid character for an IPv6 address. */
1752 *ptr = start;
1753 return FALSE;
1755 } else {
1756 /* Found an IPv4 address. */
1757 ip.ipv4 = cur_start;
1758 ip.ipv4_len = *ptr - cur_start;
1760 TRACE("(%p %p %x): Found an attached IPv4 address %s len=%d.\n",
1761 ptr, data, flags, debugstr_wn(ip.ipv4, ip.ipv4_len),
1762 ip.ipv4_len);
1764 /* IPv4 addresses can only appear at the end of a IPv6. */
1765 break;
1770 compute_ipv6_comps_size(&ip);
1772 /* Make sure the IPv6 address adds up to 16 bytes. */
1773 if(ip.components_size + ip.elision_size != 16) {
1774 *ptr = start;
1775 TRACE("(%p %p %x): Invalid IPv6 address, did not add up to 16 bytes.\n",
1776 ptr, data, flags);
1777 return FALSE;
1780 if(ip.elision_size == 2) {
1781 /* For some reason on Windows if an elision that represents
1782 * only 1 h16 component is encountered at the very begin or
1783 * end of an IPv6 address, Windows does not consider it a
1784 * valid IPv6 address.
1786 * Ex: [::2:3:4:5:6:7] is not valid, even though the sum
1787 * of all the components == 128bits.
1789 if(ip.elision < ip.components[0].str ||
1790 ip.elision > ip.components[ip.h16_count-1].str) {
1791 *ptr = start;
1792 TRACE("(%p %p %x): Invalid IPv6 address. Detected elision of 2 bytes at the beginning or end of the address.\n",
1793 ptr, data, flags);
1794 return FALSE;
1798 data->host_type = Uri_HOST_IPV6;
1799 data->has_ipv6 = TRUE;
1800 data->ipv6_address = ip;
1802 TRACE("(%p %p %x): Found valid IPv6 literal %s len=%d\n",
1803 ptr, data, flags, debugstr_wn(start, *ptr-start),
1804 *ptr-start);
1805 return TRUE;
1808 /* IPvFuture = "v" 1*HEXDIG "." 1*( unreserved / sub-delims / ":" ) */
1809 static BOOL parse_ipvfuture(const WCHAR **ptr, parse_data *data, DWORD flags) {
1810 const WCHAR *start = *ptr;
1812 /* IPvFuture has to start with a 'v' or 'V'. */
1813 if(**ptr != 'v' && **ptr != 'V')
1814 return FALSE;
1816 /* Following the v there must be at least 1 hex digit. */
1817 ++(*ptr);
1818 if(!is_hexdigit(**ptr)) {
1819 *ptr = start;
1820 return FALSE;
1823 ++(*ptr);
1824 while(is_hexdigit(**ptr))
1825 ++(*ptr);
1827 /* End of the hexdigit sequence must be a '.' */
1828 if(**ptr != '.') {
1829 *ptr = start;
1830 return FALSE;
1833 ++(*ptr);
1834 if(!is_unreserved(**ptr) && !is_subdelim(**ptr) && **ptr != ':') {
1835 *ptr = start;
1836 return FALSE;
1839 ++(*ptr);
1840 while(is_unreserved(**ptr) || is_subdelim(**ptr) || **ptr == ':')
1841 ++(*ptr);
1843 data->host_type = Uri_HOST_UNKNOWN;
1845 TRACE("(%p %p %x): Parsed IPvFuture address %s len=%d\n", ptr, data, flags,
1846 debugstr_wn(start, *ptr-start), *ptr-start);
1848 return TRUE;
1851 /* IP-literal = "[" ( IPv6address / IPvFuture ) "]" */
1852 static BOOL parse_ip_literal(const WCHAR **ptr, parse_data *data, DWORD flags, DWORD extras) {
1853 data->host = *ptr;
1855 if(**ptr != '[' && !(extras & ALLOW_BRACKETLESS_IP_LITERAL)) {
1856 data->host = NULL;
1857 return FALSE;
1858 } else if(**ptr == '[')
1859 ++(*ptr);
1861 if(!parse_ipv6address(ptr, data, flags)) {
1862 if(extras & SKIP_IP_FUTURE_CHECK || !parse_ipvfuture(ptr, data, flags)) {
1863 *ptr = data->host;
1864 data->host = NULL;
1865 return FALSE;
1869 if(**ptr != ']' && !(extras & ALLOW_BRACKETLESS_IP_LITERAL)) {
1870 *ptr = data->host;
1871 data->host = NULL;
1872 return FALSE;
1873 } else if(!**ptr && extras & ALLOW_BRACKETLESS_IP_LITERAL) {
1874 /* The IP literal didn't contain brackets and was followed by
1875 * a NULL terminator, so no reason to even check the port.
1877 data->host_len = *ptr - data->host;
1878 return TRUE;
1881 ++(*ptr);
1882 if(**ptr == ':') {
1883 ++(*ptr);
1884 /* If a valid port is not found, then let it trickle down to
1885 * parse_reg_name.
1887 if(!parse_port(ptr, data, flags)) {
1888 *ptr = data->host;
1889 data->host = NULL;
1890 return FALSE;
1892 } else
1893 data->host_len = *ptr - data->host;
1895 return TRUE;
1898 /* Parses the host information from the URI.
1900 * host = IP-literal / IPv4address / reg-name
1902 static BOOL parse_host(const WCHAR **ptr, parse_data *data, DWORD flags, DWORD extras) {
1903 if(!parse_ip_literal(ptr, data, flags, extras)) {
1904 if(!parse_ipv4address(ptr, data, flags)) {
1905 if(!parse_reg_name(ptr, data, flags, extras)) {
1906 TRACE("(%p %p %x %x): Malformed URI, Unknown host type.\n",
1907 ptr, data, flags, extras);
1908 return FALSE;
1913 return TRUE;
1916 /* Parses the authority information from the URI.
1918 * authority = [ userinfo "@" ] host [ ":" port ]
1920 static BOOL parse_authority(const WCHAR **ptr, parse_data *data, DWORD flags) {
1921 parse_userinfo(ptr, data, flags);
1923 /* Parsing the port will happen during one of the host parsing
1924 * routines (if the URI has a port).
1926 if(!parse_host(ptr, data, flags, 0))
1927 return FALSE;
1929 return TRUE;
1932 /* Attempts to parse the path information of a hierarchical URI. */
1933 static BOOL parse_path_hierarchical(const WCHAR **ptr, parse_data *data, DWORD flags) {
1934 const WCHAR *start = *ptr;
1935 static const WCHAR slash[] = {'/',0};
1936 const BOOL is_file = data->scheme_type == URL_SCHEME_FILE;
1938 if(is_path_delim(**ptr)) {
1939 if(data->scheme_type == URL_SCHEME_WILDCARD) {
1940 /* Wildcard schemes don't get a '/' attached if their path is
1941 * empty.
1943 data->path = NULL;
1944 data->path_len = 0;
1945 } else if(!(flags & Uri_CREATE_NO_CANONICALIZE)) {
1946 /* If the path component is empty, then a '/' is added. */
1947 data->path = slash;
1948 data->path_len = 1;
1950 } else {
1951 while(!is_path_delim(**ptr)) {
1952 if(**ptr == '%' && data->scheme_type != URL_SCHEME_UNKNOWN && !is_file) {
1953 if(!check_pct_encoded(ptr)) {
1954 *ptr = start;
1955 return FALSE;
1956 } else
1957 continue;
1958 } else if(is_forbidden_dos_path_char(**ptr) && is_file &&
1959 (flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
1960 /* File schemes with USE_DOS_PATH set aren't allowed to have
1961 * a '<' or '>' or '\"' appear in them.
1963 *ptr = start;
1964 return FALSE;
1965 } else if(**ptr == '\\') {
1966 /* Not allowed to have a backslash if NO_CANONICALIZE is set
1967 * and the scheme is known type (but not a file scheme).
1969 if(flags & Uri_CREATE_NO_CANONICALIZE) {
1970 if(data->scheme_type != URL_SCHEME_FILE &&
1971 data->scheme_type != URL_SCHEME_UNKNOWN) {
1972 *ptr = start;
1973 return FALSE;
1978 ++(*ptr);
1981 /* The only time a URI doesn't have a path is when
1982 * the NO_CANONICALIZE flag is set and the raw URI
1983 * didn't contain one.
1985 if(*ptr == start) {
1986 data->path = NULL;
1987 data->path_len = 0;
1988 } else {
1989 data->path = start;
1990 data->path_len = *ptr - start;
1994 if(data->path)
1995 TRACE("(%p %p %x): Parsed path %s len=%d\n", ptr, data, flags,
1996 debugstr_wn(data->path, data->path_len), data->path_len);
1997 else
1998 TRACE("(%p %p %x): The URI contained no path\n", ptr, data, flags);
2000 return TRUE;
2003 /* Parses the path of a opaque URI (much less strict then the parser
2004 * for a hierarchical URI).
2006 * NOTE:
2007 * Windows allows invalid % encoded data to appear in opaque URI paths
2008 * for unknown scheme types.
2010 * File schemes with USE_DOS_PATH set aren't allowed to have '<', '>', or '\"'
2011 * appear in them.
2013 static BOOL parse_path_opaque(const WCHAR **ptr, parse_data *data, DWORD flags) {
2014 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
2015 const BOOL is_file = data->scheme_type == URL_SCHEME_FILE;
2017 data->path = *ptr;
2019 while(!is_path_delim(**ptr)) {
2020 if(**ptr == '%' && known_scheme) {
2021 if(!check_pct_encoded(ptr)) {
2022 *ptr = data->path;
2023 data->path = NULL;
2024 return FALSE;
2025 } else
2026 continue;
2027 } else if(is_forbidden_dos_path_char(**ptr) && is_file &&
2028 (flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
2029 *ptr = data->path;
2030 data->path = NULL;
2031 return FALSE;
2034 ++(*ptr);
2037 data->path_len = *ptr - data->path;
2038 TRACE("(%p %p %x): Parsed opaque URI path %s len=%d\n", ptr, data, flags,
2039 debugstr_wn(data->path, data->path_len), data->path_len);
2040 return TRUE;
2043 /* Determines how the URI should be parsed after the scheme information.
2045 * If the scheme is followed, by "//" then, it is treated as an hierarchical URI
2046 * which then the authority and path information will be parsed out. Otherwise, the
2047 * URI will be treated as an opaque URI which the authority information is not parsed
2048 * out.
2050 * RFC 3896 definition of hier-part:
2052 * hier-part = "//" authority path-abempty
2053 * / path-absolute
2054 * / path-rootless
2055 * / path-empty
2057 * MSDN opaque URI definition:
2058 * scheme ":" path [ "#" fragment ]
2060 * NOTES:
2061 * If the URI is of an unknown scheme type and has a "//" following the scheme then it
2062 * is treated as a hierarchical URI, but, if the CREATE_NO_CRACK_UNKNOWN_SCHEMES flag is
2063 * set then it is considered an opaque URI reguardless of what follows the scheme information
2064 * (per MSDN documentation).
2066 static BOOL parse_hierpart(const WCHAR **ptr, parse_data *data, DWORD flags) {
2067 const WCHAR *start = *ptr;
2069 /* Checks if the authority information needs to be parsed. */
2070 if(is_hierarchical_uri(ptr, data)) {
2071 /* Only treat it as a hierarchical URI if the scheme_type is known or
2072 * the Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES flag is not set.
2074 if(data->scheme_type != URL_SCHEME_UNKNOWN ||
2075 !(flags & Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES)) {
2076 TRACE("(%p %p %x): Treating URI as an hierarchical URI.\n", ptr, data, flags);
2077 data->is_opaque = FALSE;
2079 /* TODO: Handle hierarchical URI's, parse authority then parse the path. */
2080 if(!parse_authority(ptr, data, flags))
2081 return FALSE;
2083 return parse_path_hierarchical(ptr, data, flags);
2084 } else
2085 /* Reset ptr to it's starting position so opaque path parsing
2086 * begins at the correct location.
2088 *ptr = start;
2091 /* If it reaches here, then the URI will be treated as an opaque
2092 * URI.
2095 TRACE("(%p %p %x): Treating URI as an opaque URI.\n", ptr, data, flags);
2097 data->is_opaque = TRUE;
2098 if(!parse_path_opaque(ptr, data, flags))
2099 return FALSE;
2101 return TRUE;
2104 /* Attempts to parse the query string from the URI.
2106 * NOTES:
2107 * If NO_DECODE_EXTRA_INFO flag is set, then invalid percent encoded
2108 * data is allowed appear in the query string. For unknown scheme types
2109 * invalid percent encoded data is allowed to appear reguardless.
2111 static BOOL parse_query(const WCHAR **ptr, parse_data *data, DWORD flags) {
2112 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
2114 if(**ptr != '?') {
2115 TRACE("(%p %p %x): URI didn't contain a query string.\n", ptr, data, flags);
2116 return TRUE;
2119 data->query = *ptr;
2121 ++(*ptr);
2122 while(**ptr && **ptr != '#') {
2123 if(**ptr == '%' && known_scheme &&
2124 !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
2125 if(!check_pct_encoded(ptr)) {
2126 *ptr = data->query;
2127 data->query = NULL;
2128 return FALSE;
2129 } else
2130 continue;
2133 ++(*ptr);
2136 data->query_len = *ptr - data->query;
2138 TRACE("(%p %p %x): Parsed query string %s len=%d\n", ptr, data, flags,
2139 debugstr_wn(data->query, data->query_len), data->query_len);
2140 return TRUE;
2143 /* Attempts to parse the fragment from the URI.
2145 * NOTES:
2146 * If NO_DECODE_EXTRA_INFO flag is set, then invalid percent encoded
2147 * data is allowed appear in the query string. For unknown scheme types
2148 * invalid percent encoded data is allowed to appear reguardless.
2150 static BOOL parse_fragment(const WCHAR **ptr, parse_data *data, DWORD flags) {
2151 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
2153 if(**ptr != '#') {
2154 TRACE("(%p %p %x): URI didn't contain a fragment.\n", ptr, data, flags);
2155 return TRUE;
2158 data->fragment = *ptr;
2160 ++(*ptr);
2161 while(**ptr) {
2162 if(**ptr == '%' && known_scheme &&
2163 !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
2164 if(!check_pct_encoded(ptr)) {
2165 *ptr = data->fragment;
2166 data->fragment = NULL;
2167 return FALSE;
2168 } else
2169 continue;
2172 ++(*ptr);
2175 data->fragment_len = *ptr - data->fragment;
2177 TRACE("(%p %p %x): Parsed fragment %s len=%d\n", ptr, data, flags,
2178 debugstr_wn(data->fragment, data->fragment_len), data->fragment_len);
2179 return TRUE;
2182 /* Parses and validates the components of the specified by data->uri
2183 * and stores the information it parses into 'data'.
2185 * Returns TRUE if it successfully parsed the URI. False otherwise.
2187 static BOOL parse_uri(parse_data *data, DWORD flags) {
2188 const WCHAR *ptr;
2189 const WCHAR **pptr;
2191 ptr = data->uri;
2192 pptr = &ptr;
2194 TRACE("(%p %x): BEGINNING TO PARSE URI %s.\n", data, flags, debugstr_w(data->uri));
2196 if(!parse_scheme(pptr, data, flags, 0))
2197 return FALSE;
2199 if(!parse_hierpart(pptr, data, flags))
2200 return FALSE;
2202 if(!parse_query(pptr, data, flags))
2203 return FALSE;
2205 if(!parse_fragment(pptr, data, flags))
2206 return FALSE;
2208 TRACE("(%p %x): FINISHED PARSING URI.\n", data, flags);
2209 return TRUE;
2212 static BOOL canonicalize_username(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2213 const WCHAR *ptr;
2215 if(!data->username) {
2216 uri->userinfo_start = -1;
2217 return TRUE;
2220 uri->userinfo_start = uri->canon_len;
2221 for(ptr = data->username; ptr < data->username+data->username_len; ++ptr) {
2222 if(*ptr == '%') {
2223 /* Only decode % encoded values for known scheme types. */
2224 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
2225 /* See if the value really needs decoded. */
2226 WCHAR val = decode_pct_val(ptr);
2227 if(is_unreserved(val)) {
2228 if(!computeOnly)
2229 uri->canon_uri[uri->canon_len] = val;
2231 ++uri->canon_len;
2233 /* Move pass the hex characters. */
2234 ptr += 2;
2235 continue;
2238 } else if(!is_reserved(*ptr) && !is_unreserved(*ptr) && *ptr != '\\') {
2239 /* Only percent encode forbidden characters if the NO_ENCODE_FORBIDDEN_CHARACTERS flag
2240 * is NOT set.
2242 if(!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS)) {
2243 if(!computeOnly)
2244 pct_encode_val(*ptr, uri->canon_uri + uri->canon_len);
2246 uri->canon_len += 3;
2247 continue;
2251 if(!computeOnly)
2252 /* Nothing special, so just copy the character over. */
2253 uri->canon_uri[uri->canon_len] = *ptr;
2254 ++uri->canon_len;
2257 return TRUE;
2260 static BOOL canonicalize_password(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2261 const WCHAR *ptr;
2263 if(!data->password) {
2264 uri->userinfo_split = -1;
2265 return TRUE;
2268 if(uri->userinfo_start == -1)
2269 /* Has a password, but, doesn't have a username. */
2270 uri->userinfo_start = uri->canon_len;
2272 uri->userinfo_split = uri->canon_len - uri->userinfo_start;
2274 /* Add the ':' to the userinfo component. */
2275 if(!computeOnly)
2276 uri->canon_uri[uri->canon_len] = ':';
2277 ++uri->canon_len;
2279 for(ptr = data->password; ptr < data->password+data->password_len; ++ptr) {
2280 if(*ptr == '%') {
2281 /* Only decode % encoded values for known scheme types. */
2282 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
2283 /* See if the value really needs decoded. */
2284 WCHAR val = decode_pct_val(ptr);
2285 if(is_unreserved(val)) {
2286 if(!computeOnly)
2287 uri->canon_uri[uri->canon_len] = val;
2289 ++uri->canon_len;
2291 /* Move pass the hex characters. */
2292 ptr += 2;
2293 continue;
2296 } else if(!is_reserved(*ptr) && !is_unreserved(*ptr) && *ptr != '\\') {
2297 /* Only percent encode forbidden characters if the NO_ENCODE_FORBIDDEN_CHARACTERS flag
2298 * is NOT set.
2300 if(!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS)) {
2301 if(!computeOnly)
2302 pct_encode_val(*ptr, uri->canon_uri + uri->canon_len);
2304 uri->canon_len += 3;
2305 continue;
2309 if(!computeOnly)
2310 /* Nothing special, so just copy the character over. */
2311 uri->canon_uri[uri->canon_len] = *ptr;
2312 ++uri->canon_len;
2315 return TRUE;
2318 /* Canonicalizes the userinfo of the URI represented by the parse_data.
2320 * Canonicalization of the userinfo is a simple process. If there are any percent
2321 * encoded characters that fall in the "unreserved" character set, they are decoded
2322 * to their actual value. If a character is not in the "unreserved" or "reserved" sets
2323 * then it is percent encoded. Other than that the characters are copied over without
2324 * change.
2326 static BOOL canonicalize_userinfo(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2327 uri->userinfo_start = uri->userinfo_split = -1;
2328 uri->userinfo_len = 0;
2330 if(!data->username && !data->password)
2331 /* URI doesn't have userinfo, so nothing to do here. */
2332 return TRUE;
2334 if(!canonicalize_username(data, uri, flags, computeOnly))
2335 return FALSE;
2337 if(!canonicalize_password(data, uri, flags, computeOnly))
2338 return FALSE;
2340 uri->userinfo_len = uri->canon_len - uri->userinfo_start;
2341 if(!computeOnly)
2342 TRACE("(%p %p %x %d): Canonicalized userinfo, userinfo_start=%d, userinfo=%s, userinfo_split=%d userinfo_len=%d.\n",
2343 data, uri, flags, computeOnly, uri->userinfo_start, debugstr_wn(uri->canon_uri + uri->userinfo_start, uri->userinfo_len),
2344 uri->userinfo_split, uri->userinfo_len);
2346 /* Now insert the '@' after the userinfo. */
2347 if(!computeOnly)
2348 uri->canon_uri[uri->canon_len] = '@';
2349 ++uri->canon_len;
2351 return TRUE;
2354 /* Attempts to canonicalize a reg_name.
2356 * Things that happen:
2357 * 1) If Uri_CREATE_NO_CANONICALIZE flag is not set, then the reg_name is
2358 * lower cased. Unless it's an unknown scheme type, which case it's
2359 * no lower cased reguardless.
2361 * 2) Unreserved % encoded characters are decoded for known
2362 * scheme types.
2364 * 3) Forbidden characters are % encoded as long as
2365 * Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS flag is not set and
2366 * it isn't an unknown scheme type.
2368 * 4) If it's a file scheme and the host is "localhost" it's removed.
2370 * 5) If it's a file scheme and Uri_CREATE_FILE_USE_DOS_PATH is set,
2371 * then the UNC path characters are added before the host name.
2373 static BOOL canonicalize_reg_name(const parse_data *data, Uri *uri,
2374 DWORD flags, BOOL computeOnly) {
2375 static const WCHAR localhostW[] =
2376 {'l','o','c','a','l','h','o','s','t',0};
2377 const WCHAR *ptr;
2378 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
2380 if(data->scheme_type == URL_SCHEME_FILE &&
2381 data->host_len == lstrlenW(localhostW)) {
2382 if(!StrCmpNIW(data->host, localhostW, data->host_len)) {
2383 uri->host_start = -1;
2384 uri->host_len = 0;
2385 uri->host_type = Uri_HOST_UNKNOWN;
2386 return TRUE;
2390 if(data->scheme_type == URL_SCHEME_FILE && flags & Uri_CREATE_FILE_USE_DOS_PATH) {
2391 if(!computeOnly) {
2392 uri->canon_uri[uri->canon_len] = '\\';
2393 uri->canon_uri[uri->canon_len+1] = '\\';
2395 uri->canon_len += 2;
2396 uri->authority_start = uri->canon_len;
2399 uri->host_start = uri->canon_len;
2401 for(ptr = data->host; ptr < data->host+data->host_len; ++ptr) {
2402 if(*ptr == '%' && known_scheme) {
2403 WCHAR val = decode_pct_val(ptr);
2404 if(is_unreserved(val)) {
2405 /* If NO_CANONICALZE is not set, then windows lower cases the
2406 * decoded value.
2408 if(!(flags & Uri_CREATE_NO_CANONICALIZE) && isupperW(val)) {
2409 if(!computeOnly)
2410 uri->canon_uri[uri->canon_len] = tolowerW(val);
2411 } else {
2412 if(!computeOnly)
2413 uri->canon_uri[uri->canon_len] = val;
2415 ++uri->canon_len;
2417 /* Skip past the % encoded character. */
2418 ptr += 2;
2419 continue;
2420 } else {
2421 /* Just copy the % over. */
2422 if(!computeOnly)
2423 uri->canon_uri[uri->canon_len] = *ptr;
2424 ++uri->canon_len;
2426 } else if(*ptr == '\\') {
2427 /* Only unknown scheme types could have made it here with a '\\' in the host name. */
2428 if(!computeOnly)
2429 uri->canon_uri[uri->canon_len] = *ptr;
2430 ++uri->canon_len;
2431 } else if(!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS) &&
2432 !is_unreserved(*ptr) && !is_reserved(*ptr) && known_scheme) {
2433 if(!computeOnly) {
2434 pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
2436 /* The percent encoded value gets lower cased also. */
2437 if(!(flags & Uri_CREATE_NO_CANONICALIZE)) {
2438 uri->canon_uri[uri->canon_len+1] = tolowerW(uri->canon_uri[uri->canon_len+1]);
2439 uri->canon_uri[uri->canon_len+2] = tolowerW(uri->canon_uri[uri->canon_len+2]);
2443 uri->canon_len += 3;
2444 } else {
2445 if(!computeOnly) {
2446 if(!(flags & Uri_CREATE_NO_CANONICALIZE) && known_scheme)
2447 uri->canon_uri[uri->canon_len] = tolowerW(*ptr);
2448 else
2449 uri->canon_uri[uri->canon_len] = *ptr;
2452 ++uri->canon_len;
2456 uri->host_len = uri->canon_len - uri->host_start;
2458 if(!computeOnly)
2459 TRACE("(%p %p %x %d): Canonicalize reg_name=%s len=%d\n", data, uri, flags,
2460 computeOnly, debugstr_wn(uri->canon_uri+uri->host_start, uri->host_len),
2461 uri->host_len);
2463 if(!computeOnly)
2464 find_domain_name(uri->canon_uri+uri->host_start, uri->host_len,
2465 &(uri->domain_offset));
2467 return TRUE;
2470 /* Attempts to canonicalize an implicit IPv4 address. */
2471 static BOOL canonicalize_implicit_ipv4address(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2472 uri->host_start = uri->canon_len;
2474 TRACE("%u\n", data->implicit_ipv4);
2475 /* For unknown scheme types Window's doesn't convert
2476 * the value into an IP address, but, it still considers
2477 * it an IPv4 address.
2479 if(data->scheme_type == URL_SCHEME_UNKNOWN) {
2480 if(!computeOnly)
2481 memcpy(uri->canon_uri+uri->canon_len, data->host, data->host_len*sizeof(WCHAR));
2482 uri->canon_len += data->host_len;
2483 } else {
2484 if(!computeOnly)
2485 uri->canon_len += ui2ipv4(uri->canon_uri+uri->canon_len, data->implicit_ipv4);
2486 else
2487 uri->canon_len += ui2ipv4(NULL, data->implicit_ipv4);
2490 uri->host_len = uri->canon_len - uri->host_start;
2491 uri->host_type = Uri_HOST_IPV4;
2493 if(!computeOnly)
2494 TRACE("%p %p %x %d): Canonicalized implicit IP address=%s len=%d\n",
2495 data, uri, flags, computeOnly,
2496 debugstr_wn(uri->canon_uri+uri->host_start, uri->host_len),
2497 uri->host_len);
2499 return TRUE;
2502 /* Attempts to canonicalize an IPv4 address.
2504 * If the parse_data represents a URI that has an implicit IPv4 address
2505 * (ex. http://256/, this function will convert 256 into 0.0.1.0). If
2506 * the implicit IP address exceeds the value of UINT_MAX (maximum value
2507 * for an IPv4 address) it's canonicalized as if were a reg-name.
2509 * If the parse_data contains a partial or full IPv4 address it normalizes it.
2510 * A partial IPv4 address is something like "192.0" and would be normalized to
2511 * "192.0.0.0". With a full (or partial) IPv4 address like "192.002.01.003" would
2512 * be normalized to "192.2.1.3".
2514 * NOTES:
2515 * Window's ONLY normalizes IPv4 address for known scheme types (one that isn't
2516 * URL_SCHEME_UNKNOWN). For unknown scheme types, it simply copies the data from
2517 * the original URI into the canonicalized URI, but, it still recognizes URI's
2518 * host type as HOST_IPV4.
2520 static BOOL canonicalize_ipv4address(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2521 if(data->has_implicit_ip)
2522 return canonicalize_implicit_ipv4address(data, uri, flags, computeOnly);
2523 else {
2524 uri->host_start = uri->canon_len;
2526 /* Windows only normalizes for known scheme types. */
2527 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
2528 /* parse_data contains a partial or full IPv4 address, so normalize it. */
2529 DWORD i, octetDigitCount = 0, octetCount = 0;
2530 BOOL octetHasDigit = FALSE;
2532 for(i = 0; i < data->host_len; ++i) {
2533 if(data->host[i] == '0' && !octetHasDigit) {
2534 /* Can ignore leading zeros if:
2535 * 1) It isn't the last digit of the octet.
2536 * 2) i+1 != data->host_len
2537 * 3) i+1 != '.'
2539 if(octetDigitCount == 2 ||
2540 i+1 == data->host_len ||
2541 data->host[i+1] == '.') {
2542 if(!computeOnly)
2543 uri->canon_uri[uri->canon_len] = data->host[i];
2544 ++uri->canon_len;
2545 TRACE("Adding zero\n");
2547 } else if(data->host[i] == '.') {
2548 if(!computeOnly)
2549 uri->canon_uri[uri->canon_len] = data->host[i];
2550 ++uri->canon_len;
2552 octetDigitCount = 0;
2553 octetHasDigit = FALSE;
2554 ++octetCount;
2555 } else {
2556 if(!computeOnly)
2557 uri->canon_uri[uri->canon_len] = data->host[i];
2558 ++uri->canon_len;
2560 ++octetDigitCount;
2561 octetHasDigit = TRUE;
2565 /* Make sure the canonicalized IP address has 4 dec-octets.
2566 * If doesn't add "0" ones until there is 4;
2568 for( ; octetCount < 3; ++octetCount) {
2569 if(!computeOnly) {
2570 uri->canon_uri[uri->canon_len] = '.';
2571 uri->canon_uri[uri->canon_len+1] = '0';
2574 uri->canon_len += 2;
2576 } else {
2577 /* Windows doesn't normalize addresses in unknown schemes. */
2578 if(!computeOnly)
2579 memcpy(uri->canon_uri+uri->canon_len, data->host, data->host_len*sizeof(WCHAR));
2580 uri->canon_len += data->host_len;
2583 uri->host_len = uri->canon_len - uri->host_start;
2584 if(!computeOnly)
2585 TRACE("(%p %p %x %d): Canonicalized IPv4 address, ip=%s len=%d\n",
2586 data, uri, flags, computeOnly,
2587 debugstr_wn(uri->canon_uri+uri->host_start, uri->host_len),
2588 uri->host_len);
2591 return TRUE;
2594 /* Attempts to canonicalize the IPv6 address of the URI.
2596 * Multiple things happen during the canonicalization of an IPv6 address:
2597 * 1) Any leading zero's in an h16 component are removed.
2598 * Ex: [0001:0022::] -> [1:22::]
2600 * 2) The longest sequence of zero h16 components are compressed
2601 * into a "::" (elision). If there's a tie, the first is choosen.
2603 * Ex: [0:0:0:0:1:6:7:8] -> [::1:6:7:8]
2604 * [0:0:0:0:1:2::] -> [::1:2:0:0]
2605 * [0:0:1:2:0:0:7:8] -> [::1:2:0:0:7:8]
2607 * 3) If an IPv4 address is attached to the IPv6 address, it's
2608 * also normalized.
2609 * Ex: [::001.002.022.000] -> [::1.2.22.0]
2611 * 4) If an elision is present, but, only represents 1 h16 component
2612 * it's expanded.
2614 * Ex: [1::2:3:4:5:6:7] -> [1:0:2:3:4:5:6:7]
2616 * 5) If the IPv6 address contains an IPv4 address and there exists
2617 * at least 1 non-zero h16 component the IPv4 address is converted
2618 * into two h16 components, otherwise it's normalized and kept as is.
2620 * Ex: [::192.200.003.4] -> [::192.200.3.4]
2621 * [ffff::192.200.003.4] -> [ffff::c0c8:3041]
2623 * NOTE:
2624 * For unknown scheme types Windows simply copies the address over without any
2625 * changes.
2627 * IPv4 address can be included in an elision if all its components are 0's.
2629 static BOOL canonicalize_ipv6address(const parse_data *data, Uri *uri,
2630 DWORD flags, BOOL computeOnly) {
2631 uri->host_start = uri->canon_len;
2633 if(data->scheme_type == URL_SCHEME_UNKNOWN) {
2634 if(!computeOnly)
2635 memcpy(uri->canon_uri+uri->canon_len, data->host, data->host_len*sizeof(WCHAR));
2636 uri->canon_len += data->host_len;
2637 } else {
2638 USHORT values[8];
2639 INT elision_start;
2640 DWORD i, elision_len;
2642 if(!ipv6_to_number(&(data->ipv6_address), values)) {
2643 TRACE("(%p %p %x %d): Failed to compute numerical value for IPv6 address.\n",
2644 data, uri, flags, computeOnly);
2645 return FALSE;
2648 if(!computeOnly)
2649 uri->canon_uri[uri->canon_len] = '[';
2650 ++uri->canon_len;
2652 /* Find where the elision should occur (if any). */
2653 compute_elision_location(&(data->ipv6_address), values, &elision_start, &elision_len);
2655 TRACE("%p %p %x %d): Elision starts at %d, len=%u\n", data, uri, flags,
2656 computeOnly, elision_start, elision_len);
2658 for(i = 0; i < 8; ++i) {
2659 BOOL in_elision = (elision_start > -1 && i >= elision_start &&
2660 i < elision_start+elision_len);
2661 BOOL do_ipv4 = (i == 6 && data->ipv6_address.ipv4 && !in_elision &&
2662 data->ipv6_address.h16_count == 0);
2664 if(i == elision_start) {
2665 if(!computeOnly) {
2666 uri->canon_uri[uri->canon_len] = ':';
2667 uri->canon_uri[uri->canon_len+1] = ':';
2669 uri->canon_len += 2;
2672 /* We can ignore the current component if we're in the elision. */
2673 if(in_elision)
2674 continue;
2676 /* We only add a ':' if we're not at i == 0, or when we're at
2677 * the very end of elision range since the ':' colon was handled
2678 * earlier. Otherwise we would end up with ":::" after elision.
2680 if(i != 0 && !(elision_start > -1 && i == elision_start+elision_len)) {
2681 if(!computeOnly)
2682 uri->canon_uri[uri->canon_len] = ':';
2683 ++uri->canon_len;
2686 if(do_ipv4) {
2687 UINT val;
2688 DWORD len;
2690 /* Combine the two parts of the IPv4 address values. */
2691 val = values[i];
2692 val <<= 16;
2693 val += values[i+1];
2695 if(!computeOnly)
2696 len = ui2ipv4(uri->canon_uri+uri->canon_len, val);
2697 else
2698 len = ui2ipv4(NULL, val);
2700 uri->canon_len += len;
2701 ++i;
2702 } else {
2703 /* Write a regular h16 component to the URI. */
2705 /* Short circuit for the trivial case. */
2706 if(values[i] == 0) {
2707 if(!computeOnly)
2708 uri->canon_uri[uri->canon_len] = '0';
2709 ++uri->canon_len;
2710 } else {
2711 static const WCHAR formatW[] = {'%','x',0};
2713 if(!computeOnly)
2714 uri->canon_len += sprintfW(uri->canon_uri+uri->canon_len,
2715 formatW, values[i]);
2716 else {
2717 WCHAR tmp[5];
2718 uri->canon_len += sprintfW(tmp, formatW, values[i]);
2724 /* Add the closing ']'. */
2725 if(!computeOnly)
2726 uri->canon_uri[uri->canon_len] = ']';
2727 ++uri->canon_len;
2730 uri->host_len = uri->canon_len - uri->host_start;
2732 if(!computeOnly)
2733 TRACE("(%p %p %x %d): Canonicalized IPv6 address %s, len=%d\n", data, uri, flags,
2734 computeOnly, debugstr_wn(uri->canon_uri+uri->host_start, uri->host_len),
2735 uri->host_len);
2737 return TRUE;
2740 /* Attempts to canonicalize the host of the URI (if any). */
2741 static BOOL canonicalize_host(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2742 uri->host_start = -1;
2743 uri->host_len = 0;
2744 uri->domain_offset = -1;
2746 if(data->host) {
2747 switch(data->host_type) {
2748 case Uri_HOST_DNS:
2749 uri->host_type = Uri_HOST_DNS;
2750 if(!canonicalize_reg_name(data, uri, flags, computeOnly))
2751 return FALSE;
2753 break;
2754 case Uri_HOST_IPV4:
2755 uri->host_type = Uri_HOST_IPV4;
2756 if(!canonicalize_ipv4address(data, uri, flags, computeOnly))
2757 return FALSE;
2759 break;
2760 case Uri_HOST_IPV6:
2761 if(!canonicalize_ipv6address(data, uri, flags, computeOnly))
2762 return FALSE;
2764 uri->host_type = Uri_HOST_IPV6;
2765 break;
2766 case Uri_HOST_UNKNOWN:
2767 if(data->host_len > 0 || data->scheme_type != URL_SCHEME_FILE) {
2768 uri->host_start = uri->canon_len;
2770 /* Nothing happens to unknown host types. */
2771 if(!computeOnly)
2772 memcpy(uri->canon_uri+uri->canon_len, data->host, data->host_len*sizeof(WCHAR));
2773 uri->canon_len += data->host_len;
2774 uri->host_len = data->host_len;
2777 uri->host_type = Uri_HOST_UNKNOWN;
2778 break;
2779 default:
2780 FIXME("(%p %p %x %d): Canonicalization for host type %d not supported.\n", data,
2781 uri, flags, computeOnly, data->host_type);
2782 return FALSE;
2786 return TRUE;
2789 static BOOL canonicalize_port(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2790 BOOL has_default_port = FALSE;
2791 USHORT default_port = 0;
2792 DWORD i;
2794 uri->port_offset = -1;
2796 /* Check if the scheme has a default port. */
2797 for(i = 0; i < sizeof(default_ports)/sizeof(default_ports[0]); ++i) {
2798 if(default_ports[i].scheme == data->scheme_type) {
2799 has_default_port = TRUE;
2800 default_port = default_ports[i].port;
2801 break;
2805 uri->has_port = data->has_port || has_default_port;
2807 /* Possible cases:
2808 * 1) Has a port which is the default port.
2809 * 2) Has a port (not the default).
2810 * 3) Doesn't have a port, but, scheme has a default port.
2811 * 4) No port.
2813 if(has_default_port && data->has_port && data->port_value == default_port) {
2814 /* If it's the default port and this flag isn't set, don't do anything. */
2815 if(flags & Uri_CREATE_NO_CANONICALIZE) {
2816 uri->port_offset = uri->canon_len-uri->authority_start;
2817 if(!computeOnly)
2818 uri->canon_uri[uri->canon_len] = ':';
2819 ++uri->canon_len;
2821 if(data->port) {
2822 /* Copy the original port over. */
2823 if(!computeOnly)
2824 memcpy(uri->canon_uri+uri->canon_len, data->port, data->port_len*sizeof(WCHAR));
2825 uri->canon_len += data->port_len;
2826 } else {
2827 if(!computeOnly)
2828 uri->canon_len += ui2str(uri->canon_uri+uri->canon_len, data->port_value);
2829 else
2830 uri->canon_len += ui2str(NULL, data->port_value);
2834 uri->port = default_port;
2835 } else if(data->has_port) {
2836 uri->port_offset = uri->canon_len-uri->authority_start;
2837 if(!computeOnly)
2838 uri->canon_uri[uri->canon_len] = ':';
2839 ++uri->canon_len;
2841 if(flags & Uri_CREATE_NO_CANONICALIZE && data->port) {
2842 /* Copy the original over without changes. */
2843 if(!computeOnly)
2844 memcpy(uri->canon_uri+uri->canon_len, data->port, data->port_len*sizeof(WCHAR));
2845 uri->canon_len += data->port_len;
2846 } else {
2847 if(!computeOnly)
2848 uri->canon_len += ui2str(uri->canon_uri+uri->canon_len, data->port_value);
2849 else
2850 uri->canon_len += ui2str(NULL, data->port_value);
2853 uri->port = data->port_value;
2854 } else if(has_default_port)
2855 uri->port = default_port;
2857 return TRUE;
2860 /* Canonicalizes the authority of the URI represented by the parse_data. */
2861 static BOOL canonicalize_authority(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2862 uri->authority_start = uri->canon_len;
2863 uri->authority_len = 0;
2865 if(!canonicalize_userinfo(data, uri, flags, computeOnly))
2866 return FALSE;
2868 if(!canonicalize_host(data, uri, flags, computeOnly))
2869 return FALSE;
2871 if(!canonicalize_port(data, uri, flags, computeOnly))
2872 return FALSE;
2874 if(uri->host_start != -1 || (data->is_relative && (data->password || data->username)))
2875 uri->authority_len = uri->canon_len - uri->authority_start;
2876 else
2877 uri->authority_start = -1;
2879 return TRUE;
2882 /* Attempts to canonicalize the path of a hierarchical URI.
2884 * Things that happen:
2885 * 1). Forbidden characters are percent encoded, unless the NO_ENCODE_FORBIDDEN
2886 * flag is set or it's a file URI. Forbidden characters are always encoded
2887 * for file schemes reguardless and forbidden characters are never encoded
2888 * for unknown scheme types.
2890 * 2). For known scheme types '\\' are changed to '/'.
2892 * 3). Percent encoded, unreserved characters are decoded to their actual values.
2893 * Unless the scheme type is unknown. For file schemes any percent encoded
2894 * character in the unreserved or reserved set is decoded.
2896 * 4). For File schemes if the path is starts with a drive letter and doesn't
2897 * start with a '/' then one is appended.
2898 * Ex: file://c:/test.mp3 -> file:///c:/test.mp3
2900 * 5). Dot segments are removed from the path for all scheme types
2901 * unless NO_CANONICALIZE flag is set. Dot segments aren't removed
2902 * for wildcard scheme types.
2904 * NOTES:
2905 * file://c:/test%20test -> file:///c:/test%2520test
2906 * file://c:/test%3Etest -> file:///c:/test%253Etest
2907 * if Uri_CREATE_FILE_USE_DOS_PATH is not set:
2908 * file:///c:/test%20test -> file:///c:/test%20test
2909 * file:///c:/test%test -> file:///c:/test%25test
2911 static BOOL canonicalize_path_hierarchical(const parse_data *data, Uri *uri,
2912 DWORD flags, BOOL computeOnly) {
2913 const WCHAR *ptr;
2914 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
2915 const BOOL is_file = data->scheme_type == URL_SCHEME_FILE;
2917 BOOL escape_pct = FALSE;
2919 if(!data->path) {
2920 uri->path_start = -1;
2921 uri->path_len = 0;
2922 return TRUE;
2925 uri->path_start = uri->canon_len;
2926 ptr = data->path;
2928 if(is_file && uri->host_start == -1) {
2929 /* Check if a '/' needs to be appended for the file scheme. */
2930 if(data->path_len > 1 && is_drive_path(ptr) && !(flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
2931 if(!computeOnly)
2932 uri->canon_uri[uri->canon_len] = '/';
2933 uri->canon_len++;
2934 escape_pct = TRUE;
2935 } else if(*ptr == '/') {
2936 if(!(flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
2937 /* Copy the extra '/' over. */
2938 if(!computeOnly)
2939 uri->canon_uri[uri->canon_len] = '/';
2940 ++uri->canon_len;
2942 ++ptr;
2945 if(is_drive_path(ptr)) {
2946 if(!computeOnly) {
2947 uri->canon_uri[uri->canon_len] = *ptr;
2948 /* If theres a '|' after the drive letter, convert it to a ':'. */
2949 uri->canon_uri[uri->canon_len+1] = ':';
2951 ptr += 2;
2952 uri->canon_len += 2;
2956 if(!is_file && *(data->path) && *(data->path) != '/') {
2957 /* Prepend a '/' to the path if it doesn't have one. */
2958 if(!computeOnly)
2959 uri->canon_uri[uri->canon_len] = '/';
2960 ++uri->canon_len;
2963 for(; ptr < data->path+data->path_len; ++ptr) {
2964 if(*ptr == '%') {
2965 const WCHAR *tmp = ptr;
2966 WCHAR val;
2968 /* Check if the % represents a valid encoded char, or if it needs encoded. */
2969 BOOL force_encode = !check_pct_encoded(&tmp) && is_file && !(flags&Uri_CREATE_FILE_USE_DOS_PATH);
2970 val = decode_pct_val(ptr);
2972 if(force_encode || escape_pct) {
2973 /* Escape the percent sign in the file URI. */
2974 if(!computeOnly)
2975 pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
2976 uri->canon_len += 3;
2977 } else if((is_unreserved(val) && known_scheme) ||
2978 (is_file && (is_unreserved(val) || is_reserved(val) ||
2979 (val && flags&Uri_CREATE_FILE_USE_DOS_PATH && !is_forbidden_dos_path_char(val))))) {
2980 if(!computeOnly)
2981 uri->canon_uri[uri->canon_len] = val;
2982 ++uri->canon_len;
2984 ptr += 2;
2985 continue;
2986 } else {
2987 if(!computeOnly)
2988 uri->canon_uri[uri->canon_len] = *ptr;
2989 ++uri->canon_len;
2991 } else if(*ptr == '/' && is_file && (flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
2992 /* Convert the '/' back to a '\\'. */
2993 if(!computeOnly)
2994 uri->canon_uri[uri->canon_len] = '\\';
2995 ++uri->canon_len;
2996 } else if(*ptr == '\\' && known_scheme) {
2997 if(is_file && (flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
2998 /* Don't convert the '\\' to a '/'. */
2999 if(!computeOnly)
3000 uri->canon_uri[uri->canon_len] = *ptr;
3001 ++uri->canon_len;
3002 } else {
3003 if(!computeOnly)
3004 uri->canon_uri[uri->canon_len] = '/';
3005 ++uri->canon_len;
3007 } else if(known_scheme && !is_unreserved(*ptr) && !is_reserved(*ptr) &&
3008 (!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS) || is_file)) {
3009 if(is_file && (flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
3010 /* Don't escape the character. */
3011 if(!computeOnly)
3012 uri->canon_uri[uri->canon_len] = *ptr;
3013 ++uri->canon_len;
3014 } else {
3015 /* Escape the forbidden character. */
3016 if(!computeOnly)
3017 pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
3018 uri->canon_len += 3;
3020 } else {
3021 if(!computeOnly)
3022 uri->canon_uri[uri->canon_len] = *ptr;
3023 ++uri->canon_len;
3027 uri->path_len = uri->canon_len - uri->path_start;
3029 /* Removing the dot segments only happens when it's not in
3030 * computeOnly mode and it's not a wildcard scheme. File schemes
3031 * with USE_DOS_PATH set don't get dot segments removed.
3033 if(!(is_file && (flags & Uri_CREATE_FILE_USE_DOS_PATH)) &&
3034 data->scheme_type != URL_SCHEME_WILDCARD) {
3035 if(!(flags & Uri_CREATE_NO_CANONICALIZE) && !computeOnly) {
3036 /* Remove the dot segments (if any) and reset everything to the new
3037 * correct length.
3039 DWORD new_len = remove_dot_segments(uri->canon_uri+uri->path_start, uri->path_len);
3040 uri->canon_len -= uri->path_len-new_len;
3041 uri->path_len = new_len;
3045 if(!computeOnly)
3046 TRACE("Canonicalized path %s len=%d\n",
3047 debugstr_wn(uri->canon_uri+uri->path_start, uri->path_len),
3048 uri->path_len);
3050 return TRUE;
3053 /* Attempts to canonicalize the path for an opaque URI.
3055 * For known scheme types:
3056 * 1) forbidden characters are percent encoded if
3057 * NO_ENCODE_FORBIDDEN_CHARACTERS isn't set.
3059 * 2) Percent encoded, unreserved characters are decoded
3060 * to their actual values, for known scheme types.
3062 * 3) '\\' are changed to '/' for known scheme types
3063 * except for mailto schemes.
3065 * 4) For file schemes, if USE_DOS_PATH is set all '/'
3066 * are converted to backslashes.
3068 * 5) For file schemes, if USE_DOS_PATH isn't set all '\'
3069 * are converted to forward slashes.
3071 static BOOL canonicalize_path_opaque(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
3072 const WCHAR *ptr;
3073 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
3074 const BOOL is_file = data->scheme_type == URL_SCHEME_FILE;
3076 if(!data->path) {
3077 uri->path_start = -1;
3078 uri->path_len = 0;
3079 return TRUE;
3082 uri->path_start = uri->canon_len;
3084 /* Windows doesn't allow a "//" to appear after the scheme
3085 * of a URI, if it's an opaque URI.
3087 if(data->scheme && *(data->path) == '/' && *(data->path+1) == '/') {
3088 /* So it inserts a "/." before the "//" if it exists. */
3089 if(!computeOnly) {
3090 uri->canon_uri[uri->canon_len] = '/';
3091 uri->canon_uri[uri->canon_len+1] = '.';
3094 uri->canon_len += 2;
3097 for(ptr = data->path; ptr < data->path+data->path_len; ++ptr) {
3098 if(*ptr == '%' && known_scheme) {
3099 WCHAR val = decode_pct_val(ptr);
3101 if(is_unreserved(val)) {
3102 if(!computeOnly)
3103 uri->canon_uri[uri->canon_len] = val;
3104 ++uri->canon_len;
3106 ptr += 2;
3107 continue;
3108 } else {
3109 if(!computeOnly)
3110 uri->canon_uri[uri->canon_len] = *ptr;
3111 ++uri->canon_len;
3113 } else if(*ptr == '/' && is_file && (flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
3114 if(!computeOnly)
3115 uri->canon_uri[uri->canon_len] = '\\';
3116 ++uri->canon_len;
3117 } else if(*ptr == '\\' && is_file) {
3118 if(!(flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
3119 /* Convert to a '/'. */
3120 if(!computeOnly)
3121 uri->canon_uri[uri->canon_len] = '/';
3122 ++uri->canon_len;
3123 } else {
3124 /* Just copy it over. */
3125 if(!computeOnly)
3126 uri->canon_uri[uri->canon_len] = *ptr;
3127 ++uri->canon_len;
3129 } else if(known_scheme && !is_unreserved(*ptr) && !is_reserved(*ptr) &&
3130 !(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS)) {
3131 if(is_file && (flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
3132 /* Forbidden characters aren't percent encoded for file schemes
3133 * with USE_DOS_PATH set.
3135 if(!computeOnly)
3136 uri->canon_uri[uri->canon_len] = *ptr;
3137 ++uri->canon_len;
3138 } else if(data->scheme_type == URL_SCHEME_MK && *ptr == '\\') {
3139 /* MK URIs don't get '\\' percent encoded. */
3140 if(!computeOnly)
3141 uri->canon_uri[uri->canon_len] = *ptr;
3142 ++uri->canon_len;
3143 } else {
3144 if(!computeOnly)
3145 pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
3146 uri->canon_len += 3;
3148 } else {
3149 if(!computeOnly)
3150 uri->canon_uri[uri->canon_len] = *ptr;
3151 ++uri->canon_len;
3155 uri->path_len = uri->canon_len - uri->path_start;
3157 TRACE("(%p %p %x %d): Canonicalized opaque URI path %s len=%d\n", data, uri, flags, computeOnly,
3158 debugstr_wn(uri->canon_uri+uri->path_start, uri->path_len), uri->path_len);
3159 return TRUE;
3162 /* Determines how the URI represented by the parse_data should be canonicalized.
3164 * Essentially, if the parse_data represents an hierarchical URI then it calls
3165 * canonicalize_authority and the canonicalization functions for the path. If the
3166 * URI is opaque it canonicalizes the path of the URI.
3168 static BOOL canonicalize_hierpart(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
3169 if(!data->is_opaque || (data->is_relative && (data->password || data->username))) {
3170 /* "//" is only added for non-wildcard scheme types.
3172 * A "//" is only added to a relative URI if it has a
3173 * host or port component (this only happens if a IUriBuilder
3174 * is generating an IUri).
3176 if((data->is_relative && (data->host || data->has_port)) ||
3177 (!data->is_relative && data->scheme_type != URL_SCHEME_WILDCARD)) {
3178 if(!computeOnly) {
3179 INT pos = uri->canon_len;
3181 uri->canon_uri[pos] = '/';
3182 uri->canon_uri[pos+1] = '/';
3184 uri->canon_len += 2;
3187 if(!canonicalize_authority(data, uri, flags, computeOnly))
3188 return FALSE;
3190 if(data->is_relative && (data->password || data->username)) {
3191 if(!canonicalize_path_opaque(data, uri, flags, computeOnly))
3192 return FALSE;
3193 } else {
3194 if(!canonicalize_path_hierarchical(data, uri, flags, computeOnly))
3195 return FALSE;
3197 } else {
3198 /* Opaque URI's don't have an authority. */
3199 uri->userinfo_start = uri->userinfo_split = -1;
3200 uri->userinfo_len = 0;
3201 uri->host_start = -1;
3202 uri->host_len = 0;
3203 uri->host_type = Uri_HOST_UNKNOWN;
3204 uri->has_port = FALSE;
3205 uri->authority_start = -1;
3206 uri->authority_len = 0;
3207 uri->domain_offset = -1;
3208 uri->port_offset = -1;
3210 if(is_hierarchical_scheme(data->scheme_type)) {
3211 DWORD i;
3213 /* Absolute URIs aren't displayed for known scheme types
3214 * which should be hierarchical URIs.
3216 uri->display_modifiers |= URI_DISPLAY_NO_ABSOLUTE_URI;
3218 /* Windows also sets the port for these (if they have one). */
3219 for(i = 0; i < sizeof(default_ports)/sizeof(default_ports[0]); ++i) {
3220 if(data->scheme_type == default_ports[i].scheme) {
3221 uri->has_port = TRUE;
3222 uri->port = default_ports[i].port;
3223 break;
3228 if(!canonicalize_path_opaque(data, uri, flags, computeOnly))
3229 return FALSE;
3232 if(uri->path_start > -1 && !computeOnly)
3233 /* Finding file extensions happens for both types of URIs. */
3234 uri->extension_offset = find_file_extension(uri->canon_uri+uri->path_start, uri->path_len);
3235 else
3236 uri->extension_offset = -1;
3238 return TRUE;
3241 /* Attempts to canonicalize the query string of the URI.
3243 * Things that happen:
3244 * 1) For known scheme types forbidden characters
3245 * are percent encoded, unless the NO_DECODE_EXTRA_INFO flag is set
3246 * or NO_ENCODE_FORBIDDEN_CHARACTERS is set.
3248 * 2) For known scheme types, percent encoded, unreserved characters
3249 * are decoded as long as the NO_DECODE_EXTRA_INFO flag isn't set.
3251 static BOOL canonicalize_query(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
3252 const WCHAR *ptr, *end;
3253 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
3255 if(!data->query) {
3256 uri->query_start = -1;
3257 uri->query_len = 0;
3258 return TRUE;
3261 uri->query_start = uri->canon_len;
3263 end = data->query+data->query_len;
3264 for(ptr = data->query; ptr < end; ++ptr) {
3265 if(*ptr == '%') {
3266 if(known_scheme && !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
3267 WCHAR val = decode_pct_val(ptr);
3268 if(is_unreserved(val)) {
3269 if(!computeOnly)
3270 uri->canon_uri[uri->canon_len] = val;
3271 ++uri->canon_len;
3273 ptr += 2;
3274 continue;
3277 } else if(known_scheme && !is_unreserved(*ptr) && !is_reserved(*ptr)) {
3278 if(!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS) &&
3279 !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
3280 if(!computeOnly)
3281 pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
3282 uri->canon_len += 3;
3283 continue;
3287 if(!computeOnly)
3288 uri->canon_uri[uri->canon_len] = *ptr;
3289 ++uri->canon_len;
3292 uri->query_len = uri->canon_len - uri->query_start;
3294 if(!computeOnly)
3295 TRACE("(%p %p %x %d): Canonicalized query string %s len=%d\n", data, uri, flags,
3296 computeOnly, debugstr_wn(uri->canon_uri+uri->query_start, uri->query_len),
3297 uri->query_len);
3298 return TRUE;
3301 static BOOL canonicalize_fragment(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
3302 const WCHAR *ptr, *end;
3303 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
3305 if(!data->fragment) {
3306 uri->fragment_start = -1;
3307 uri->fragment_len = 0;
3308 return TRUE;
3311 uri->fragment_start = uri->canon_len;
3313 end = data->fragment + data->fragment_len;
3314 for(ptr = data->fragment; ptr < end; ++ptr) {
3315 if(*ptr == '%') {
3316 if(known_scheme && !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
3317 WCHAR val = decode_pct_val(ptr);
3318 if(is_unreserved(val)) {
3319 if(!computeOnly)
3320 uri->canon_uri[uri->canon_len] = val;
3321 ++uri->canon_len;
3323 ptr += 2;
3324 continue;
3327 } else if(known_scheme && !is_unreserved(*ptr) && !is_reserved(*ptr)) {
3328 if(!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS) &&
3329 !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
3330 if(!computeOnly)
3331 pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
3332 uri->canon_len += 3;
3333 continue;
3337 if(!computeOnly)
3338 uri->canon_uri[uri->canon_len] = *ptr;
3339 ++uri->canon_len;
3342 uri->fragment_len = uri->canon_len - uri->fragment_start;
3344 if(!computeOnly)
3345 TRACE("(%p %p %x %d): Canonicalized fragment %s len=%d\n", data, uri, flags,
3346 computeOnly, debugstr_wn(uri->canon_uri+uri->fragment_start, uri->fragment_len),
3347 uri->fragment_len);
3348 return TRUE;
3351 /* Canonicalizes the scheme information specified in the parse_data using the specified flags. */
3352 static BOOL canonicalize_scheme(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
3353 uri->scheme_start = -1;
3354 uri->scheme_len = 0;
3356 if(!data->scheme) {
3357 /* The only type of URI that doesn't have to have a scheme is a relative
3358 * URI.
3360 if(!data->is_relative) {
3361 FIXME("(%p %p %x): Unable to determine the scheme type of %s.\n", data,
3362 uri, flags, debugstr_w(data->uri));
3363 return FALSE;
3365 } else {
3366 if(!computeOnly) {
3367 DWORD i;
3368 INT pos = uri->canon_len;
3370 for(i = 0; i < data->scheme_len; ++i) {
3371 /* Scheme name must be lower case after canonicalization. */
3372 uri->canon_uri[i + pos] = tolowerW(data->scheme[i]);
3375 uri->canon_uri[i + pos] = ':';
3376 uri->scheme_start = pos;
3378 TRACE("(%p %p %x): Canonicalized scheme=%s, len=%d.\n", data, uri, flags,
3379 debugstr_wn(uri->canon_uri, uri->scheme_len), data->scheme_len);
3382 /* This happens in both computation modes. */
3383 uri->canon_len += data->scheme_len + 1;
3384 uri->scheme_len = data->scheme_len;
3386 return TRUE;
3389 /* Compute's what the length of the URI specified by the parse_data will be
3390 * after canonicalization occurs using the specified flags.
3392 * This function will return a non-zero value indicating the length of the canonicalized
3393 * URI, or -1 on error.
3395 static int compute_canonicalized_length(const parse_data *data, DWORD flags) {
3396 Uri uri;
3398 memset(&uri, 0, sizeof(Uri));
3400 TRACE("(%p %x): Beginning to compute canonicalized length for URI %s\n", data, flags,
3401 debugstr_w(data->uri));
3403 if(!canonicalize_scheme(data, &uri, flags, TRUE)) {
3404 ERR("(%p %x): Failed to compute URI scheme length.\n", data, flags);
3405 return -1;
3408 if(!canonicalize_hierpart(data, &uri, flags, TRUE)) {
3409 ERR("(%p %x): Failed to compute URI hierpart length.\n", data, flags);
3410 return -1;
3413 if(!canonicalize_query(data, &uri, flags, TRUE)) {
3414 ERR("(%p %x): Failed to compute query string length.\n", data, flags);
3415 return -1;
3418 if(!canonicalize_fragment(data, &uri, flags, TRUE)) {
3419 ERR("(%p %x): Failed to compute fragment length.\n", data, flags);
3420 return -1;
3423 TRACE("(%p %x): Finished computing canonicalized URI length. length=%d\n", data, flags, uri.canon_len);
3425 return uri.canon_len;
3428 /* Canonicalizes the URI data specified in the parse_data, using the given flags. If the
3429 * canonicalization succeededs it will store all the canonicalization information
3430 * in the pointer to the Uri.
3432 * To canonicalize a URI this function first computes what the length of the URI
3433 * specified by the parse_data will be. Once this is done it will then perfom the actual
3434 * canonicalization of the URI.
3436 static HRESULT canonicalize_uri(const parse_data *data, Uri *uri, DWORD flags) {
3437 INT len;
3439 uri->canon_uri = NULL;
3440 len = uri->canon_size = uri->canon_len = 0;
3442 TRACE("(%p %p %x): beginning to canonicalize URI %s.\n", data, uri, flags, debugstr_w(data->uri));
3444 /* First try to compute the length of the URI. */
3445 len = compute_canonicalized_length(data, flags);
3446 if(len == -1) {
3447 ERR("(%p %p %x): Could not compute the canonicalized length of %s.\n", data, uri, flags,
3448 debugstr_w(data->uri));
3449 return E_INVALIDARG;
3452 uri->canon_uri = heap_alloc((len+1)*sizeof(WCHAR));
3453 if(!uri->canon_uri)
3454 return E_OUTOFMEMORY;
3456 uri->canon_size = len;
3457 if(!canonicalize_scheme(data, uri, flags, FALSE)) {
3458 ERR("(%p %p %x): Unable to canonicalize the scheme of the URI.\n", data, uri, flags);
3459 return E_INVALIDARG;
3461 uri->scheme_type = data->scheme_type;
3463 if(!canonicalize_hierpart(data, uri, flags, FALSE)) {
3464 ERR("(%p %p %x): Unable to canonicalize the heirpart of the URI\n", data, uri, flags);
3465 return E_INVALIDARG;
3468 if(!canonicalize_query(data, uri, flags, FALSE)) {
3469 ERR("(%p %p %x): Unable to canonicalize query string of the URI.\n",
3470 data, uri, flags);
3471 return E_INVALIDARG;
3474 if(!canonicalize_fragment(data, uri, flags, FALSE)) {
3475 ERR("(%p %p %x): Unable to canonicalize fragment of the URI.\n",
3476 data, uri, flags);
3477 return E_INVALIDARG;
3480 /* There's a possibility we didn't use all the space we allocated
3481 * earlier.
3483 if(uri->canon_len < uri->canon_size) {
3484 /* This happens if the URI is hierarchical and dot
3485 * segments were removed from it's path.
3487 WCHAR *tmp = heap_realloc(uri->canon_uri, (uri->canon_len+1)*sizeof(WCHAR));
3488 if(!tmp)
3489 return E_OUTOFMEMORY;
3491 uri->canon_uri = tmp;
3492 uri->canon_size = uri->canon_len;
3495 uri->canon_uri[uri->canon_len] = '\0';
3496 TRACE("(%p %p %x): finished canonicalizing the URI. uri=%s\n", data, uri, flags, debugstr_w(uri->canon_uri));
3498 return S_OK;
3501 static HRESULT get_builder_component(LPWSTR *component, DWORD *component_len,
3502 LPCWSTR source, DWORD source_len,
3503 LPCWSTR *output, DWORD *output_len)
3505 if(!output_len) {
3506 if(output)
3507 *output = NULL;
3508 return E_POINTER;
3511 if(!output) {
3512 *output_len = 0;
3513 return E_POINTER;
3516 if(!(*component) && source) {
3517 /* Allocate 'component', and copy the contents from 'source'
3518 * into the new allocation.
3520 *component = heap_alloc((source_len+1)*sizeof(WCHAR));
3521 if(!(*component))
3522 return E_OUTOFMEMORY;
3524 memcpy(*component, source, source_len*sizeof(WCHAR));
3525 (*component)[source_len] = '\0';
3526 *component_len = source_len;
3529 *output = *component;
3530 *output_len = *component_len;
3531 return *output ? S_OK : S_FALSE;
3534 /* Allocates 'component' and copies the string from 'new_value' into 'component'.
3535 * If 'prefix' is set and 'new_value' isn't NULL, then it checks if 'new_value'
3536 * starts with 'prefix'. If it doesn't then 'prefix' is prepended to 'component'.
3538 * If everything is successful, then will set 'success_flag' in 'flags'.
3540 static HRESULT set_builder_component(LPWSTR *component, DWORD *component_len, LPCWSTR new_value,
3541 WCHAR prefix, DWORD *flags, DWORD success_flag)
3543 heap_free(*component);
3545 if(!new_value) {
3546 *component = NULL;
3547 *component_len = 0;
3548 } else {
3549 BOOL add_prefix = FALSE;
3550 DWORD len = lstrlenW(new_value);
3551 DWORD pos = 0;
3553 if(prefix && *new_value != prefix) {
3554 add_prefix = TRUE;
3555 *component = heap_alloc((len+2)*sizeof(WCHAR));
3556 } else
3557 *component = heap_alloc((len+1)*sizeof(WCHAR));
3559 if(!(*component))
3560 return E_OUTOFMEMORY;
3562 if(add_prefix)
3563 (*component)[pos++] = prefix;
3565 memcpy(*component+pos, new_value, (len+1)*sizeof(WCHAR));
3566 *component_len = len+pos;
3569 *flags |= success_flag;
3570 return S_OK;
3573 #define URI(x) ((IUri*) &(x)->lpIUriVtbl)
3574 #define URIBUILDER(x) ((IUriBuilder*) &(x)->lpIUriBuilderVtbl)
3576 static void reset_builder(UriBuilder *builder) {
3577 if(builder->uri)
3578 IUri_Release(URI(builder->uri));
3579 builder->uri = NULL;
3581 heap_free(builder->fragment);
3582 builder->fragment = NULL;
3583 builder->fragment_len = 0;
3585 heap_free(builder->host);
3586 builder->host = NULL;
3587 builder->host_len = 0;
3589 heap_free(builder->password);
3590 builder->password = NULL;
3591 builder->password_len = 0;
3593 heap_free(builder->path);
3594 builder->path = NULL;
3595 builder->path_len = 0;
3597 heap_free(builder->query);
3598 builder->query = NULL;
3599 builder->query_len = 0;
3601 heap_free(builder->scheme);
3602 builder->scheme = NULL;
3603 builder->scheme_len = 0;
3605 heap_free(builder->username);
3606 builder->username = NULL;
3607 builder->username_len = 0;
3609 builder->has_port = FALSE;
3610 builder->port = 0;
3611 builder->modified_props = 0;
3614 static HRESULT validate_scheme_name(const UriBuilder *builder, parse_data *data, DWORD flags) {
3615 const WCHAR *component;
3616 const WCHAR *ptr;
3617 const WCHAR **pptr;
3618 DWORD expected_len;
3620 if(builder->scheme) {
3621 ptr = builder->scheme;
3622 expected_len = builder->scheme_len;
3623 } else if(builder->uri && builder->uri->scheme_start > -1) {
3624 ptr = builder->uri->canon_uri+builder->uri->scheme_start;
3625 expected_len = builder->uri->scheme_len;
3626 } else {
3627 static const WCHAR nullW[] = {0};
3628 ptr = nullW;
3629 expected_len = 0;
3632 component = ptr;
3633 pptr = &ptr;
3634 if(parse_scheme(pptr, data, flags, ALLOW_NULL_TERM_SCHEME) &&
3635 data->scheme_len == expected_len) {
3636 if(data->scheme)
3637 TRACE("(%p %p %x): Found valid scheme component %s len=%d.\n", builder, data, flags,
3638 debugstr_wn(data->scheme, data->scheme_len), data->scheme_len);
3639 } else {
3640 TRACE("(%p %p %x): Invalid scheme component found %s.\n", builder, data, flags,
3641 debugstr_wn(component, expected_len));
3642 return INET_E_INVALID_URL;
3645 return S_OK;
3648 static HRESULT validate_username(const UriBuilder *builder, parse_data *data, DWORD flags) {
3649 const WCHAR *ptr;
3650 const WCHAR **pptr;
3651 DWORD expected_len;
3653 if(builder->username) {
3654 ptr = builder->username;
3655 expected_len = builder->username_len;
3656 } else if(!(builder->modified_props & Uri_HAS_USER_NAME) && builder->uri &&
3657 builder->uri->userinfo_start > -1 && builder->uri->userinfo_split != 0) {
3658 /* Just use the username from the base Uri. */
3659 data->username = builder->uri->canon_uri+builder->uri->userinfo_start;
3660 data->username_len = (builder->uri->userinfo_split > -1) ?
3661 builder->uri->userinfo_split : builder->uri->userinfo_len;
3662 ptr = NULL;
3663 } else {
3664 ptr = NULL;
3665 expected_len = 0;
3668 if(ptr) {
3669 const WCHAR *component = ptr;
3670 pptr = &ptr;
3671 if(parse_username(pptr, data, flags, ALLOW_NULL_TERM_USER_NAME) &&
3672 data->username_len == expected_len)
3673 TRACE("(%p %p %x): Found valid username component %s len=%d.\n", builder, data, flags,
3674 debugstr_wn(data->username, data->username_len), data->username_len);
3675 else {
3676 TRACE("(%p %p %x): Invalid username component found %s.\n", builder, data, flags,
3677 debugstr_wn(component, expected_len));
3678 return INET_E_INVALID_URL;
3682 return S_OK;
3685 static HRESULT validate_password(const UriBuilder *builder, parse_data *data, DWORD flags) {
3686 const WCHAR *ptr;
3687 const WCHAR **pptr;
3688 DWORD expected_len;
3690 if(builder->password) {
3691 ptr = builder->password;
3692 expected_len = builder->password_len;
3693 } else if(!(builder->modified_props & Uri_HAS_PASSWORD) && builder->uri &&
3694 builder->uri->userinfo_split > -1) {
3695 data->password = builder->uri->canon_uri+builder->uri->userinfo_start+builder->uri->userinfo_split+1;
3696 data->password_len = builder->uri->userinfo_len-builder->uri->userinfo_split-1;
3697 ptr = NULL;
3698 } else {
3699 ptr = NULL;
3700 expected_len = 0;
3703 if(ptr) {
3704 const WCHAR *component = ptr;
3705 pptr = &ptr;
3706 if(parse_password(pptr, data, flags, ALLOW_NULL_TERM_PASSWORD) &&
3707 data->password_len == expected_len)
3708 TRACE("(%p %p %x): Found valid password component %s len=%d.\n", builder, data, flags,
3709 debugstr_wn(data->password, data->password_len), data->password_len);
3710 else {
3711 TRACE("(%p %p %x): Invalid password component found %s.\n", builder, data, flags,
3712 debugstr_wn(component, expected_len));
3713 return INET_E_INVALID_URL;
3717 return S_OK;
3720 static HRESULT validate_userinfo(const UriBuilder *builder, parse_data *data, DWORD flags) {
3721 HRESULT hr;
3723 hr = validate_username(builder, data, flags);
3724 if(FAILED(hr))
3725 return hr;
3727 hr = validate_password(builder, data, flags);
3728 if(FAILED(hr))
3729 return hr;
3731 return S_OK;
3734 static HRESULT validate_host(const UriBuilder *builder, parse_data *data, DWORD flags) {
3735 const WCHAR *ptr;
3736 const WCHAR **pptr;
3737 DWORD expected_len;
3739 if(builder->host) {
3740 ptr = builder->host;
3741 expected_len = builder->host_len;
3742 } else if(!(builder->modified_props & Uri_HAS_HOST) && builder->uri && builder->uri->host_start > -1) {
3743 ptr = builder->uri->canon_uri + builder->uri->host_start;
3744 expected_len = builder->uri->host_len;
3745 } else
3746 ptr = NULL;
3748 if(ptr) {
3749 const WCHAR *component = ptr;
3750 DWORD extras = ALLOW_BRACKETLESS_IP_LITERAL|IGNORE_PORT_DELIMITER|SKIP_IP_FUTURE_CHECK;
3751 pptr = &ptr;
3753 if(parse_host(pptr, data, flags, extras) && data->host_len == expected_len)
3754 TRACE("(%p %p %x): Found valid host name %s len=%d type=%d.\n", builder, data, flags,
3755 debugstr_wn(data->host, data->host_len), data->host_len, data->host_type);
3756 else {
3757 TRACE("(%p %p %x): Invalid host name found %s.\n", builder, data, flags,
3758 debugstr_wn(component, expected_len));
3759 return INET_E_INVALID_URL;
3763 return S_OK;
3766 static void setup_port(const UriBuilder *builder, parse_data *data, DWORD flags) {
3767 if(builder->modified_props & Uri_HAS_PORT) {
3768 if(builder->has_port) {
3769 data->has_port = TRUE;
3770 data->port_value = builder->port;
3772 } else if(builder->uri && builder->uri->has_port) {
3773 data->has_port = TRUE;
3774 data->port_value = builder->uri->port;
3777 if(data->has_port)
3778 TRACE("(%p %p %x): Using %u as port for IUri.\n", builder, data, flags, data->port_value);
3781 static HRESULT validate_path(const UriBuilder *builder, parse_data *data, DWORD flags) {
3782 const WCHAR *ptr = NULL;
3783 const WCHAR *component;
3784 const WCHAR **pptr;
3785 DWORD expected_len;
3786 BOOL check_len = TRUE;
3787 BOOL valid = FALSE;
3789 if(builder->path) {
3790 ptr = builder->path;
3791 expected_len = builder->path_len;
3792 } else if(!(builder->modified_props & Uri_HAS_PATH) &&
3793 builder->uri && builder->uri->path_start > -1) {
3794 ptr = builder->uri->canon_uri+builder->uri->path_start;
3795 expected_len = builder->uri->path_len;
3796 } else {
3797 static const WCHAR nullW[] = {0};
3798 ptr = nullW;
3799 check_len = FALSE;
3802 component = ptr;
3803 pptr = &ptr;
3805 /* How the path is validated depends on what type of
3806 * URI it is.
3808 valid = data->is_opaque ?
3809 parse_path_opaque(pptr, data, flags) : parse_path_hierarchical(pptr, data, flags);
3811 if(!valid || (check_len && expected_len != data->path_len)) {
3812 TRACE("(%p %p %x): Invalid path component %s.\n", builder, data, flags,
3813 debugstr_wn(component, expected_len));
3814 return INET_E_INVALID_URL;
3817 TRACE("(%p %p %x): Valid path component %s len=%d.\n", builder, data, flags,
3818 debugstr_wn(data->path, data->path_len), data->path_len);
3820 return S_OK;
3823 static HRESULT validate_query(const UriBuilder *builder, parse_data *data, DWORD flags) {
3824 const WCHAR *ptr = NULL;
3825 const WCHAR **pptr;
3826 DWORD expected_len;
3828 if(builder->query) {
3829 ptr = builder->query;
3830 expected_len = builder->query_len;
3831 } else if(!(builder->modified_props & Uri_HAS_QUERY) && builder->uri &&
3832 builder->uri->query_start > -1) {
3833 ptr = builder->uri->canon_uri+builder->uri->query_start;
3834 expected_len = builder->uri->query_len;
3837 if(ptr) {
3838 const WCHAR *component = ptr;
3839 pptr = &ptr;
3841 if(parse_query(pptr, data, flags) && expected_len == data->query_len)
3842 TRACE("(%p %p %x): Valid query component %s len=%d.\n", builder, data, flags,
3843 debugstr_wn(data->query, data->query_len), data->query_len);
3844 else {
3845 TRACE("(%p %p %x): Invalid query component %s.\n", builder, data, flags,
3846 debugstr_wn(component, expected_len));
3847 return INET_E_INVALID_URL;
3851 return S_OK;
3854 static HRESULT validate_fragment(const UriBuilder *builder, parse_data *data, DWORD flags) {
3855 const WCHAR *ptr = NULL;
3856 const WCHAR **pptr;
3857 DWORD expected_len;
3859 if(builder->fragment) {
3860 ptr = builder->fragment;
3861 expected_len = builder->fragment_len;
3862 } else if(!(builder->modified_props & Uri_HAS_FRAGMENT) && builder->uri &&
3863 builder->uri->fragment_start > -1) {
3864 ptr = builder->uri->canon_uri+builder->uri->fragment_start;
3865 expected_len = builder->uri->fragment_len;
3868 if(ptr) {
3869 const WCHAR *component = ptr;
3870 pptr = &ptr;
3872 if(parse_fragment(pptr, data, flags) && expected_len == data->fragment_len)
3873 TRACE("(%p %p %x): Valid fragment component %s len=%d.\n", builder, data, flags,
3874 debugstr_wn(data->fragment, data->fragment_len), data->fragment_len);
3875 else {
3876 TRACE("(%p %p %x): Invalid fragment component %s.\n", builder, data, flags,
3877 debugstr_wn(component, expected_len));
3878 return INET_E_INVALID_URL;
3882 return S_OK;
3885 static HRESULT validate_components(const UriBuilder *builder, parse_data *data, DWORD flags) {
3886 HRESULT hr;
3888 memset(data, 0, sizeof(parse_data));
3890 TRACE("(%p %p %x): Beginning to validate builder components.\n", builder, data, flags);
3892 hr = validate_scheme_name(builder, data, flags);
3893 if(FAILED(hr))
3894 return hr;
3896 /* Extra validation for file schemes. */
3897 if(data->scheme_type == URL_SCHEME_FILE) {
3898 if((builder->password || (builder->uri && builder->uri->userinfo_split > -1)) ||
3899 (builder->username || (builder->uri && builder->uri->userinfo_start > -1))) {
3900 TRACE("(%p %p %x): File schemes can't contain a username or password.\n",
3901 builder, data, flags);
3902 return INET_E_INVALID_URL;
3906 hr = validate_userinfo(builder, data, flags);
3907 if(FAILED(hr))
3908 return hr;
3910 hr = validate_host(builder, data, flags);
3911 if(FAILED(hr))
3912 return hr;
3914 setup_port(builder, data, flags);
3916 /* The URI is opaque if it doesn't have an authority component. */
3917 if(!data->is_relative)
3918 data->is_opaque = !data->username && !data->password && !data->host && !data->has_port;
3919 else
3920 data->is_opaque = !data->host && !data->has_port;
3922 hr = validate_path(builder, data, flags);
3923 if(FAILED(hr))
3924 return hr;
3926 hr = validate_query(builder, data, flags);
3927 if(FAILED(hr))
3928 return hr;
3930 hr = validate_fragment(builder, data, flags);
3931 if(FAILED(hr))
3932 return hr;
3934 TRACE("(%p %p %x): Finished validating builder components.\n", builder, data, flags);
3936 return S_OK;
3939 static void convert_to_dos_path(const WCHAR *path, DWORD path_len,
3940 WCHAR *output, DWORD *output_len)
3942 const WCHAR *ptr = path;
3944 if(path_len > 3 && *ptr == '/' && is_drive_path(path+1))
3945 /* Skip over the leading / before the drive path. */
3946 ++ptr;
3948 for(; ptr < path+path_len; ++ptr) {
3949 if(*ptr == '/') {
3950 if(output)
3951 *output++ = '\\';
3952 (*output_len)++;
3953 } else {
3954 if(output)
3955 *output++ = *ptr;
3956 (*output_len)++;
3961 /* Generates a raw uri string using the parse_data. */
3962 static DWORD generate_raw_uri(const parse_data *data, BSTR uri, DWORD flags) {
3963 DWORD length = 0;
3965 if(data->scheme) {
3966 if(uri) {
3967 memcpy(uri, data->scheme, data->scheme_len*sizeof(WCHAR));
3968 uri[data->scheme_len] = ':';
3970 length += data->scheme_len+1;
3973 if(!data->is_opaque) {
3974 /* For the "//" which appears before the authority component. */
3975 if(uri) {
3976 uri[length] = '/';
3977 uri[length+1] = '/';
3979 length += 2;
3981 /* Check if we need to add the "\\" before the host name
3982 * of a UNC server name in a DOS path.
3984 if(flags & RAW_URI_CONVERT_TO_DOS_PATH &&
3985 data->scheme_type == URL_SCHEME_FILE && data->host) {
3986 if(uri) {
3987 uri[length] = '\\';
3988 uri[length+1] = '\\';
3990 length += 2;
3994 if(data->username) {
3995 if(uri)
3996 memcpy(uri+length, data->username, data->username_len*sizeof(WCHAR));
3997 length += data->username_len;
4000 if(data->password) {
4001 if(uri) {
4002 uri[length] = ':';
4003 memcpy(uri+length+1, data->password, data->password_len*sizeof(WCHAR));
4005 length += data->password_len+1;
4008 if(data->password || data->username) {
4009 if(uri)
4010 uri[length] = '@';
4011 ++length;
4014 if(data->host) {
4015 /* IPv6 addresses get the brackets added around them if they don't already
4016 * have them.
4018 const BOOL add_brackets = data->host_type == Uri_HOST_IPV6 && *(data->host) != '[';
4019 if(add_brackets) {
4020 if(uri)
4021 uri[length] = '[';
4022 ++length;
4025 if(uri)
4026 memcpy(uri+length, data->host, data->host_len*sizeof(WCHAR));
4027 length += data->host_len;
4029 if(add_brackets) {
4030 if(uri)
4031 uri[length] = ']';
4032 length++;
4036 if(data->has_port) {
4037 /* The port isn't included in the raw uri if it's the default
4038 * port for the scheme type.
4040 DWORD i;
4041 BOOL is_default = FALSE;
4043 for(i = 0; i < sizeof(default_ports)/sizeof(default_ports[0]); ++i) {
4044 if(data->scheme_type == default_ports[i].scheme &&
4045 data->port_value == default_ports[i].port)
4046 is_default = TRUE;
4049 if(!is_default || flags & RAW_URI_FORCE_PORT_DISP) {
4050 if(uri)
4051 uri[length] = ':';
4052 ++length;
4054 if(uri)
4055 length += ui2str(uri+length, data->port_value);
4056 else
4057 length += ui2str(NULL, data->port_value);
4061 /* Check if a '/' should be added before the path for hierarchical URIs. */
4062 if(!data->is_opaque && data->path && *(data->path) != '/') {
4063 if(uri)
4064 uri[length] = '/';
4065 ++length;
4068 if(data->path) {
4069 if(!data->is_opaque && data->scheme_type == URL_SCHEME_FILE &&
4070 flags & RAW_URI_CONVERT_TO_DOS_PATH) {
4071 DWORD len = 0;
4073 if(uri)
4074 convert_to_dos_path(data->path, data->path_len, uri+length, &len);
4075 else
4076 convert_to_dos_path(data->path, data->path_len, NULL, &len);
4078 length += len;
4079 } else {
4080 if(uri)
4081 memcpy(uri+length, data->path, data->path_len*sizeof(WCHAR));
4082 length += data->path_len;
4086 if(data->query) {
4087 if(uri)
4088 memcpy(uri+length, data->query, data->query_len*sizeof(WCHAR));
4089 length += data->query_len;
4092 if(data->fragment) {
4093 if(uri)
4094 memcpy(uri+length, data->fragment, data->fragment_len*sizeof(WCHAR));
4095 length += data->fragment_len;
4098 if(uri)
4099 TRACE("(%p %p): Generated raw uri=%s len=%d\n", data, uri, debugstr_wn(uri, length), length);
4100 else
4101 TRACE("(%p %p): Computed raw uri len=%d\n", data, uri, length);
4103 return length;
4106 static HRESULT generate_uri(const UriBuilder *builder, const parse_data *data, Uri *uri, DWORD flags) {
4107 HRESULT hr;
4108 DWORD length = generate_raw_uri(data, NULL, 0);
4109 uri->raw_uri = SysAllocStringLen(NULL, length);
4110 if(!uri->raw_uri)
4111 return E_OUTOFMEMORY;
4113 generate_raw_uri(data, uri->raw_uri, 0);
4115 hr = canonicalize_uri(data, uri, flags);
4116 if(FAILED(hr)) {
4117 if(hr == E_INVALIDARG)
4118 return INET_E_INVALID_URL;
4119 return hr;
4122 uri->create_flags = flags;
4123 return S_OK;
4126 #define URI_THIS(iface) DEFINE_THIS(Uri, IUri, iface)
4128 static HRESULT WINAPI Uri_QueryInterface(IUri *iface, REFIID riid, void **ppv)
4130 Uri *This = URI_THIS(iface);
4132 if(IsEqualGUID(&IID_IUnknown, riid)) {
4133 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
4134 *ppv = URI(This);
4135 }else if(IsEqualGUID(&IID_IUri, riid)) {
4136 TRACE("(%p)->(IID_IUri %p)\n", This, ppv);
4137 *ppv = URI(This);
4138 }else if(IsEqualGUID(&IID_IUriObj, riid)) {
4139 TRACE("(%p)->(IID_IUriObj %p)\n", This, ppv);
4140 *ppv = This;
4141 return S_OK;
4142 }else {
4143 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
4144 *ppv = NULL;
4145 return E_NOINTERFACE;
4148 IUnknown_AddRef((IUnknown*)*ppv);
4149 return S_OK;
4152 static ULONG WINAPI Uri_AddRef(IUri *iface)
4154 Uri *This = URI_THIS(iface);
4155 LONG ref = InterlockedIncrement(&This->ref);
4157 TRACE("(%p) ref=%d\n", This, ref);
4159 return ref;
4162 static ULONG WINAPI Uri_Release(IUri *iface)
4164 Uri *This = URI_THIS(iface);
4165 LONG ref = InterlockedDecrement(&This->ref);
4167 TRACE("(%p) ref=%d\n", This, ref);
4169 if(!ref) {
4170 SysFreeString(This->raw_uri);
4171 heap_free(This->canon_uri);
4172 heap_free(This);
4175 return ref;
4178 static HRESULT WINAPI Uri_GetPropertyBSTR(IUri *iface, Uri_PROPERTY uriProp, BSTR *pbstrProperty, DWORD dwFlags)
4180 Uri *This = URI_THIS(iface);
4181 HRESULT hres;
4182 TRACE("(%p)->(%d %p %x)\n", This, uriProp, pbstrProperty, dwFlags);
4184 if(!pbstrProperty)
4185 return E_POINTER;
4187 if(uriProp > Uri_PROPERTY_STRING_LAST) {
4188 /* Windows allocates an empty BSTR for invalid Uri_PROPERTY's. */
4189 *pbstrProperty = SysAllocStringLen(NULL, 0);
4190 if(!(*pbstrProperty))
4191 return E_OUTOFMEMORY;
4193 /* It only returns S_FALSE for the ZONE property... */
4194 if(uriProp == Uri_PROPERTY_ZONE)
4195 return S_FALSE;
4196 else
4197 return S_OK;
4200 /* Don't have support for flags yet. */
4201 if(dwFlags) {
4202 FIXME("(%p)->(%d %p %x)\n", This, uriProp, pbstrProperty, dwFlags);
4203 return E_NOTIMPL;
4206 switch(uriProp) {
4207 case Uri_PROPERTY_ABSOLUTE_URI:
4208 if(This->display_modifiers & URI_DISPLAY_NO_ABSOLUTE_URI) {
4209 *pbstrProperty = SysAllocStringLen(NULL, 0);
4210 hres = S_FALSE;
4211 } else {
4212 if(This->scheme_type != URL_SCHEME_UNKNOWN && This->userinfo_start > -1) {
4213 if(This->userinfo_len == 0) {
4214 /* Don't include the '@' after the userinfo component. */
4215 *pbstrProperty = SysAllocStringLen(NULL, This->canon_len-1);
4216 hres = S_OK;
4217 if(*pbstrProperty) {
4218 /* Copy everything before it. */
4219 memcpy(*pbstrProperty, This->canon_uri, This->userinfo_start*sizeof(WCHAR));
4221 /* And everything after it. */
4222 memcpy(*pbstrProperty+This->userinfo_start, This->canon_uri+This->userinfo_start+1,
4223 (This->canon_len-This->userinfo_start-1)*sizeof(WCHAR));
4225 } else if(This->userinfo_split == 0 && This->userinfo_len == 1) {
4226 /* Don't include the ":@" */
4227 *pbstrProperty = SysAllocStringLen(NULL, This->canon_len-2);
4228 hres = S_OK;
4229 if(*pbstrProperty) {
4230 memcpy(*pbstrProperty, This->canon_uri, This->userinfo_start*sizeof(WCHAR));
4231 memcpy(*pbstrProperty+This->userinfo_start, This->canon_uri+This->userinfo_start+2,
4232 (This->canon_len-This->userinfo_start-2)*sizeof(WCHAR));
4234 } else {
4235 *pbstrProperty = SysAllocString(This->canon_uri);
4236 hres = S_OK;
4238 } else {
4239 *pbstrProperty = SysAllocString(This->canon_uri);
4240 hres = S_OK;
4244 if(!(*pbstrProperty))
4245 hres = E_OUTOFMEMORY;
4247 break;
4248 case Uri_PROPERTY_AUTHORITY:
4249 if(This->authority_start > -1) {
4250 if(This->port_offset > -1 && is_default_port(This->scheme_type, This->port) &&
4251 This->display_modifiers & URI_DISPLAY_NO_DEFAULT_PORT_AUTH)
4252 /* Don't include the port in the authority component. */
4253 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->authority_start, This->port_offset);
4254 else
4255 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->authority_start, This->authority_len);
4256 hres = S_OK;
4257 } else {
4258 *pbstrProperty = SysAllocStringLen(NULL, 0);
4259 hres = S_FALSE;
4262 if(!(*pbstrProperty))
4263 hres = E_OUTOFMEMORY;
4265 break;
4266 case Uri_PROPERTY_DISPLAY_URI:
4267 /* The Display URI contains everything except for the userinfo for known
4268 * scheme types.
4270 if(This->scheme_type != URL_SCHEME_UNKNOWN && This->userinfo_start > -1) {
4271 *pbstrProperty = SysAllocStringLen(NULL, This->canon_len-This->userinfo_len);
4273 if(*pbstrProperty) {
4274 /* Copy everything before the userinfo over. */
4275 memcpy(*pbstrProperty, This->canon_uri, This->userinfo_start*sizeof(WCHAR));
4276 /* Copy everything after the userinfo over. */
4277 memcpy(*pbstrProperty+This->userinfo_start,
4278 This->canon_uri+This->userinfo_start+This->userinfo_len+1,
4279 (This->canon_len-(This->userinfo_start+This->userinfo_len+1))*sizeof(WCHAR));
4281 } else
4282 *pbstrProperty = SysAllocString(This->canon_uri);
4284 if(!(*pbstrProperty))
4285 hres = E_OUTOFMEMORY;
4286 else
4287 hres = S_OK;
4289 break;
4290 case Uri_PROPERTY_DOMAIN:
4291 if(This->domain_offset > -1) {
4292 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->host_start+This->domain_offset,
4293 This->host_len-This->domain_offset);
4294 hres = S_OK;
4295 } else {
4296 *pbstrProperty = SysAllocStringLen(NULL, 0);
4297 hres = S_FALSE;
4300 if(!(*pbstrProperty))
4301 hres = E_OUTOFMEMORY;
4303 break;
4304 case Uri_PROPERTY_EXTENSION:
4305 if(This->extension_offset > -1) {
4306 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->path_start+This->extension_offset,
4307 This->path_len-This->extension_offset);
4308 hres = S_OK;
4309 } else {
4310 *pbstrProperty = SysAllocStringLen(NULL, 0);
4311 hres = S_FALSE;
4314 if(!(*pbstrProperty))
4315 hres = E_OUTOFMEMORY;
4317 break;
4318 case Uri_PROPERTY_FRAGMENT:
4319 if(This->fragment_start > -1) {
4320 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->fragment_start, This->fragment_len);
4321 hres = S_OK;
4322 } else {
4323 *pbstrProperty = SysAllocStringLen(NULL, 0);
4324 hres = S_FALSE;
4327 if(!(*pbstrProperty))
4328 hres = E_OUTOFMEMORY;
4330 break;
4331 case Uri_PROPERTY_HOST:
4332 if(This->host_start > -1) {
4333 /* The '[' and ']' aren't included for IPv6 addresses. */
4334 if(This->host_type == Uri_HOST_IPV6)
4335 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->host_start+1, This->host_len-2);
4336 else
4337 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->host_start, This->host_len);
4339 hres = S_OK;
4340 } else {
4341 *pbstrProperty = SysAllocStringLen(NULL, 0);
4342 hres = S_FALSE;
4345 if(!(*pbstrProperty))
4346 hres = E_OUTOFMEMORY;
4348 break;
4349 case Uri_PROPERTY_PASSWORD:
4350 if(This->userinfo_split > -1) {
4351 *pbstrProperty = SysAllocStringLen(
4352 This->canon_uri+This->userinfo_start+This->userinfo_split+1,
4353 This->userinfo_len-This->userinfo_split-1);
4354 hres = S_OK;
4355 } else {
4356 *pbstrProperty = SysAllocStringLen(NULL, 0);
4357 hres = S_FALSE;
4360 if(!(*pbstrProperty))
4361 return E_OUTOFMEMORY;
4363 break;
4364 case Uri_PROPERTY_PATH:
4365 if(This->path_start > -1) {
4366 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->path_start, This->path_len);
4367 hres = S_OK;
4368 } else {
4369 *pbstrProperty = SysAllocStringLen(NULL, 0);
4370 hres = S_FALSE;
4373 if(!(*pbstrProperty))
4374 hres = E_OUTOFMEMORY;
4376 break;
4377 case Uri_PROPERTY_PATH_AND_QUERY:
4378 if(This->path_start > -1) {
4379 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->path_start, This->path_len+This->query_len);
4380 hres = S_OK;
4381 } else if(This->query_start > -1) {
4382 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->query_start, This->query_len);
4383 hres = S_OK;
4384 } else {
4385 *pbstrProperty = SysAllocStringLen(NULL, 0);
4386 hres = S_FALSE;
4389 if(!(*pbstrProperty))
4390 hres = E_OUTOFMEMORY;
4392 break;
4393 case Uri_PROPERTY_QUERY:
4394 if(This->query_start > -1) {
4395 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->query_start, This->query_len);
4396 hres = S_OK;
4397 } else {
4398 *pbstrProperty = SysAllocStringLen(NULL, 0);
4399 hres = S_FALSE;
4402 if(!(*pbstrProperty))
4403 hres = E_OUTOFMEMORY;
4405 break;
4406 case Uri_PROPERTY_RAW_URI:
4407 *pbstrProperty = SysAllocString(This->raw_uri);
4408 if(!(*pbstrProperty))
4409 hres = E_OUTOFMEMORY;
4410 else
4411 hres = S_OK;
4412 break;
4413 case Uri_PROPERTY_SCHEME_NAME:
4414 if(This->scheme_start > -1) {
4415 *pbstrProperty = SysAllocStringLen(This->canon_uri + This->scheme_start, This->scheme_len);
4416 hres = S_OK;
4417 } else {
4418 *pbstrProperty = SysAllocStringLen(NULL, 0);
4419 hres = S_FALSE;
4422 if(!(*pbstrProperty))
4423 hres = E_OUTOFMEMORY;
4425 break;
4426 case Uri_PROPERTY_USER_INFO:
4427 if(This->userinfo_start > -1) {
4428 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->userinfo_start, This->userinfo_len);
4429 hres = S_OK;
4430 } else {
4431 *pbstrProperty = SysAllocStringLen(NULL, 0);
4432 hres = S_FALSE;
4435 if(!(*pbstrProperty))
4436 hres = E_OUTOFMEMORY;
4438 break;
4439 case Uri_PROPERTY_USER_NAME:
4440 if(This->userinfo_start > -1 && This->userinfo_split != 0) {
4441 /* If userinfo_split is set, that means a password exists
4442 * so the username is only from userinfo_start to userinfo_split.
4444 if(This->userinfo_split > -1) {
4445 *pbstrProperty = SysAllocStringLen(This->canon_uri + This->userinfo_start, This->userinfo_split);
4446 hres = S_OK;
4447 } else {
4448 *pbstrProperty = SysAllocStringLen(This->canon_uri + This->userinfo_start, This->userinfo_len);
4449 hres = S_OK;
4451 } else {
4452 *pbstrProperty = SysAllocStringLen(NULL, 0);
4453 hres = S_FALSE;
4456 if(!(*pbstrProperty))
4457 return E_OUTOFMEMORY;
4459 break;
4460 default:
4461 FIXME("(%p)->(%d %p %x)\n", This, uriProp, pbstrProperty, dwFlags);
4462 hres = E_NOTIMPL;
4465 return hres;
4468 static HRESULT WINAPI Uri_GetPropertyLength(IUri *iface, Uri_PROPERTY uriProp, DWORD *pcchProperty, DWORD dwFlags)
4470 Uri *This = URI_THIS(iface);
4471 HRESULT hres;
4472 TRACE("(%p)->(%d %p %x)\n", This, uriProp, pcchProperty, dwFlags);
4474 if(!pcchProperty)
4475 return E_INVALIDARG;
4477 /* Can only return a length for a property if it's a string. */
4478 if(uriProp > Uri_PROPERTY_STRING_LAST)
4479 return E_INVALIDARG;
4481 /* Don't have support for flags yet. */
4482 if(dwFlags) {
4483 FIXME("(%p)->(%d %p %x)\n", This, uriProp, pcchProperty, dwFlags);
4484 return E_NOTIMPL;
4487 switch(uriProp) {
4488 case Uri_PROPERTY_ABSOLUTE_URI:
4489 if(This->display_modifiers & URI_DISPLAY_NO_ABSOLUTE_URI) {
4490 *pcchProperty = 0;
4491 hres = S_FALSE;
4492 } else {
4493 if(This->scheme_type != URL_SCHEME_UNKNOWN) {
4494 if(This->userinfo_start > -1 && This->userinfo_len == 0)
4495 /* Don't include the '@' in the length. */
4496 *pcchProperty = This->canon_len-1;
4497 else if(This->userinfo_start > -1 && This->userinfo_len == 1 &&
4498 This->userinfo_split == 0)
4499 /* Don't include the ":@" in the length. */
4500 *pcchProperty = This->canon_len-2;
4501 else
4502 *pcchProperty = This->canon_len;
4503 } else
4504 *pcchProperty = This->canon_len;
4506 hres = S_OK;
4509 break;
4510 case Uri_PROPERTY_AUTHORITY:
4511 if(This->port_offset > -1 &&
4512 This->display_modifiers & URI_DISPLAY_NO_DEFAULT_PORT_AUTH &&
4513 is_default_port(This->scheme_type, This->port))
4514 /* Only count up until the port in the authority. */
4515 *pcchProperty = This->port_offset;
4516 else
4517 *pcchProperty = This->authority_len;
4518 hres = (This->authority_start > -1) ? S_OK : S_FALSE;
4519 break;
4520 case Uri_PROPERTY_DISPLAY_URI:
4521 if(This->scheme_type != URL_SCHEME_UNKNOWN && This->userinfo_start > -1)
4522 *pcchProperty = This->canon_len-This->userinfo_len-1;
4523 else
4524 *pcchProperty = This->canon_len;
4526 hres = S_OK;
4527 break;
4528 case Uri_PROPERTY_DOMAIN:
4529 if(This->domain_offset > -1)
4530 *pcchProperty = This->host_len - This->domain_offset;
4531 else
4532 *pcchProperty = 0;
4534 hres = (This->domain_offset > -1) ? S_OK : S_FALSE;
4535 break;
4536 case Uri_PROPERTY_EXTENSION:
4537 if(This->extension_offset > -1) {
4538 *pcchProperty = This->path_len - This->extension_offset;
4539 hres = S_OK;
4540 } else {
4541 *pcchProperty = 0;
4542 hres = S_FALSE;
4545 break;
4546 case Uri_PROPERTY_FRAGMENT:
4547 *pcchProperty = This->fragment_len;
4548 hres = (This->fragment_start > -1) ? S_OK : S_FALSE;
4549 break;
4550 case Uri_PROPERTY_HOST:
4551 *pcchProperty = This->host_len;
4553 /* '[' and ']' aren't included in the length. */
4554 if(This->host_type == Uri_HOST_IPV6)
4555 *pcchProperty -= 2;
4557 hres = (This->host_start > -1) ? S_OK : S_FALSE;
4558 break;
4559 case Uri_PROPERTY_PASSWORD:
4560 *pcchProperty = (This->userinfo_split > -1) ? This->userinfo_len-This->userinfo_split-1 : 0;
4561 hres = (This->userinfo_split > -1) ? S_OK : S_FALSE;
4562 break;
4563 case Uri_PROPERTY_PATH:
4564 *pcchProperty = This->path_len;
4565 hres = (This->path_start > -1) ? S_OK : S_FALSE;
4566 break;
4567 case Uri_PROPERTY_PATH_AND_QUERY:
4568 *pcchProperty = This->path_len+This->query_len;
4569 hres = (This->path_start > -1 || This->query_start > -1) ? S_OK : S_FALSE;
4570 break;
4571 case Uri_PROPERTY_QUERY:
4572 *pcchProperty = This->query_len;
4573 hres = (This->query_start > -1) ? S_OK : S_FALSE;
4574 break;
4575 case Uri_PROPERTY_RAW_URI:
4576 *pcchProperty = SysStringLen(This->raw_uri);
4577 hres = S_OK;
4578 break;
4579 case Uri_PROPERTY_SCHEME_NAME:
4580 *pcchProperty = This->scheme_len;
4581 hres = (This->scheme_start > -1) ? S_OK : S_FALSE;
4582 break;
4583 case Uri_PROPERTY_USER_INFO:
4584 *pcchProperty = This->userinfo_len;
4585 hres = (This->userinfo_start > -1) ? S_OK : S_FALSE;
4586 break;
4587 case Uri_PROPERTY_USER_NAME:
4588 *pcchProperty = (This->userinfo_split > -1) ? This->userinfo_split : This->userinfo_len;
4589 if(This->userinfo_split == 0)
4590 hres = S_FALSE;
4591 else
4592 hres = (This->userinfo_start > -1) ? S_OK : S_FALSE;
4593 break;
4594 default:
4595 FIXME("(%p)->(%d %p %x)\n", This, uriProp, pcchProperty, dwFlags);
4596 hres = E_NOTIMPL;
4599 return hres;
4602 static HRESULT WINAPI Uri_GetPropertyDWORD(IUri *iface, Uri_PROPERTY uriProp, DWORD *pcchProperty, DWORD dwFlags)
4604 Uri *This = URI_THIS(iface);
4605 HRESULT hres;
4607 TRACE("(%p)->(%d %p %x)\n", This, uriProp, pcchProperty, dwFlags);
4609 if(!pcchProperty)
4610 return E_INVALIDARG;
4612 /* Microsoft's implementation for the ZONE property of a URI seems to be lacking...
4613 * From what I can tell, instead of checking which URLZONE the URI belongs to it
4614 * simply assigns URLZONE_INVALID and returns E_NOTIMPL. This also applies to the GetZone
4615 * function.
4617 if(uriProp == Uri_PROPERTY_ZONE) {
4618 *pcchProperty = URLZONE_INVALID;
4619 return E_NOTIMPL;
4622 if(uriProp < Uri_PROPERTY_DWORD_START) {
4623 *pcchProperty = 0;
4624 return E_INVALIDARG;
4627 switch(uriProp) {
4628 case Uri_PROPERTY_HOST_TYPE:
4629 *pcchProperty = This->host_type;
4630 hres = S_OK;
4631 break;
4632 case Uri_PROPERTY_PORT:
4633 if(!This->has_port) {
4634 *pcchProperty = 0;
4635 hres = S_FALSE;
4636 } else {
4637 *pcchProperty = This->port;
4638 hres = S_OK;
4641 break;
4642 case Uri_PROPERTY_SCHEME:
4643 *pcchProperty = This->scheme_type;
4644 hres = S_OK;
4645 break;
4646 default:
4647 FIXME("(%p)->(%d %p %x)\n", This, uriProp, pcchProperty, dwFlags);
4648 hres = E_NOTIMPL;
4651 return hres;
4654 static HRESULT WINAPI Uri_HasProperty(IUri *iface, Uri_PROPERTY uriProp, BOOL *pfHasProperty)
4656 Uri *This = URI_THIS(iface);
4657 TRACE("(%p)->(%d %p)\n", This, uriProp, pfHasProperty);
4659 if(!pfHasProperty)
4660 return E_INVALIDARG;
4662 switch(uriProp) {
4663 case Uri_PROPERTY_ABSOLUTE_URI:
4664 *pfHasProperty = !(This->display_modifiers & URI_DISPLAY_NO_ABSOLUTE_URI);
4665 break;
4666 case Uri_PROPERTY_AUTHORITY:
4667 *pfHasProperty = This->authority_start > -1;
4668 break;
4669 case Uri_PROPERTY_DISPLAY_URI:
4670 *pfHasProperty = TRUE;
4671 break;
4672 case Uri_PROPERTY_DOMAIN:
4673 *pfHasProperty = This->domain_offset > -1;
4674 break;
4675 case Uri_PROPERTY_EXTENSION:
4676 *pfHasProperty = This->extension_offset > -1;
4677 break;
4678 case Uri_PROPERTY_FRAGMENT:
4679 *pfHasProperty = This->fragment_start > -1;
4680 break;
4681 case Uri_PROPERTY_HOST:
4682 *pfHasProperty = This->host_start > -1;
4683 break;
4684 case Uri_PROPERTY_PASSWORD:
4685 *pfHasProperty = This->userinfo_split > -1;
4686 break;
4687 case Uri_PROPERTY_PATH:
4688 *pfHasProperty = This->path_start > -1;
4689 break;
4690 case Uri_PROPERTY_PATH_AND_QUERY:
4691 *pfHasProperty = (This->path_start > -1 || This->query_start > -1);
4692 break;
4693 case Uri_PROPERTY_QUERY:
4694 *pfHasProperty = This->query_start > -1;
4695 break;
4696 case Uri_PROPERTY_RAW_URI:
4697 *pfHasProperty = TRUE;
4698 break;
4699 case Uri_PROPERTY_SCHEME_NAME:
4700 *pfHasProperty = This->scheme_start > -1;
4701 break;
4702 case Uri_PROPERTY_USER_INFO:
4703 *pfHasProperty = This->userinfo_start > -1;
4704 break;
4705 case Uri_PROPERTY_USER_NAME:
4706 if(This->userinfo_split == 0)
4707 *pfHasProperty = FALSE;
4708 else
4709 *pfHasProperty = This->userinfo_start > -1;
4710 break;
4711 case Uri_PROPERTY_HOST_TYPE:
4712 *pfHasProperty = TRUE;
4713 break;
4714 case Uri_PROPERTY_PORT:
4715 *pfHasProperty = This->has_port;
4716 break;
4717 case Uri_PROPERTY_SCHEME:
4718 *pfHasProperty = TRUE;
4719 break;
4720 case Uri_PROPERTY_ZONE:
4721 *pfHasProperty = FALSE;
4722 break;
4723 default:
4724 FIXME("(%p)->(%d %p): Unsupported property type.\n", This, uriProp, pfHasProperty);
4725 return E_NOTIMPL;
4728 return S_OK;
4731 static HRESULT WINAPI Uri_GetAbsoluteUri(IUri *iface, BSTR *pstrAbsoluteUri)
4733 TRACE("(%p)->(%p)\n", iface, pstrAbsoluteUri);
4734 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_ABSOLUTE_URI, pstrAbsoluteUri, 0);
4737 static HRESULT WINAPI Uri_GetAuthority(IUri *iface, BSTR *pstrAuthority)
4739 TRACE("(%p)->(%p)\n", iface, pstrAuthority);
4740 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_AUTHORITY, pstrAuthority, 0);
4743 static HRESULT WINAPI Uri_GetDisplayUri(IUri *iface, BSTR *pstrDisplayUri)
4745 TRACE("(%p)->(%p)\n", iface, pstrDisplayUri);
4746 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_DISPLAY_URI, pstrDisplayUri, 0);
4749 static HRESULT WINAPI Uri_GetDomain(IUri *iface, BSTR *pstrDomain)
4751 TRACE("(%p)->(%p)\n", iface, pstrDomain);
4752 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_DOMAIN, pstrDomain, 0);
4755 static HRESULT WINAPI Uri_GetExtension(IUri *iface, BSTR *pstrExtension)
4757 TRACE("(%p)->(%p)\n", iface, pstrExtension);
4758 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_EXTENSION, pstrExtension, 0);
4761 static HRESULT WINAPI Uri_GetFragment(IUri *iface, BSTR *pstrFragment)
4763 TRACE("(%p)->(%p)\n", iface, pstrFragment);
4764 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_FRAGMENT, pstrFragment, 0);
4767 static HRESULT WINAPI Uri_GetHost(IUri *iface, BSTR *pstrHost)
4769 TRACE("(%p)->(%p)\n", iface, pstrHost);
4770 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_HOST, pstrHost, 0);
4773 static HRESULT WINAPI Uri_GetPassword(IUri *iface, BSTR *pstrPassword)
4775 TRACE("(%p)->(%p)\n", iface, pstrPassword);
4776 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_PASSWORD, pstrPassword, 0);
4779 static HRESULT WINAPI Uri_GetPath(IUri *iface, BSTR *pstrPath)
4781 TRACE("(%p)->(%p)\n", iface, pstrPath);
4782 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_PATH, pstrPath, 0);
4785 static HRESULT WINAPI Uri_GetPathAndQuery(IUri *iface, BSTR *pstrPathAndQuery)
4787 TRACE("(%p)->(%p)\n", iface, pstrPathAndQuery);
4788 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_PATH_AND_QUERY, pstrPathAndQuery, 0);
4791 static HRESULT WINAPI Uri_GetQuery(IUri *iface, BSTR *pstrQuery)
4793 TRACE("(%p)->(%p)\n", iface, pstrQuery);
4794 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_QUERY, pstrQuery, 0);
4797 static HRESULT WINAPI Uri_GetRawUri(IUri *iface, BSTR *pstrRawUri)
4799 TRACE("(%p)->(%p)\n", iface, pstrRawUri);
4800 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_RAW_URI, pstrRawUri, 0);
4803 static HRESULT WINAPI Uri_GetSchemeName(IUri *iface, BSTR *pstrSchemeName)
4805 TRACE("(%p)->(%p)\n", iface, pstrSchemeName);
4806 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_SCHEME_NAME, pstrSchemeName, 0);
4809 static HRESULT WINAPI Uri_GetUserInfo(IUri *iface, BSTR *pstrUserInfo)
4811 TRACE("(%p)->(%p)\n", iface, pstrUserInfo);
4812 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_USER_INFO, pstrUserInfo, 0);
4815 static HRESULT WINAPI Uri_GetUserName(IUri *iface, BSTR *pstrUserName)
4817 TRACE("(%p)->(%p)\n", iface, pstrUserName);
4818 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_USER_NAME, pstrUserName, 0);
4821 static HRESULT WINAPI Uri_GetHostType(IUri *iface, DWORD *pdwHostType)
4823 TRACE("(%p)->(%p)\n", iface, pdwHostType);
4824 return Uri_GetPropertyDWORD(iface, Uri_PROPERTY_HOST_TYPE, pdwHostType, 0);
4827 static HRESULT WINAPI Uri_GetPort(IUri *iface, DWORD *pdwPort)
4829 TRACE("(%p)->(%p)\n", iface, pdwPort);
4830 return Uri_GetPropertyDWORD(iface, Uri_PROPERTY_PORT, pdwPort, 0);
4833 static HRESULT WINAPI Uri_GetScheme(IUri *iface, DWORD *pdwScheme)
4835 Uri *This = URI_THIS(iface);
4836 TRACE("(%p)->(%p)\n", This, pdwScheme);
4837 return Uri_GetPropertyDWORD(iface, Uri_PROPERTY_SCHEME, pdwScheme, 0);
4840 static HRESULT WINAPI Uri_GetZone(IUri *iface, DWORD *pdwZone)
4842 TRACE("(%p)->(%p)\n", iface, pdwZone);
4843 return Uri_GetPropertyDWORD(iface, Uri_PROPERTY_ZONE,pdwZone, 0);
4846 static HRESULT WINAPI Uri_GetProperties(IUri *iface, DWORD *pdwProperties)
4848 Uri *This = URI_THIS(iface);
4849 TRACE("(%p)->(%p)\n", This, pdwProperties);
4851 if(!pdwProperties)
4852 return E_INVALIDARG;
4854 /* All URIs have these. */
4855 *pdwProperties = Uri_HAS_DISPLAY_URI|Uri_HAS_RAW_URI|Uri_HAS_SCHEME|Uri_HAS_HOST_TYPE;
4857 if(!(This->display_modifiers & URI_DISPLAY_NO_ABSOLUTE_URI))
4858 *pdwProperties |= Uri_HAS_ABSOLUTE_URI;
4860 if(This->scheme_start > -1)
4861 *pdwProperties |= Uri_HAS_SCHEME_NAME;
4863 if(This->authority_start > -1) {
4864 *pdwProperties |= Uri_HAS_AUTHORITY;
4865 if(This->userinfo_start > -1) {
4866 *pdwProperties |= Uri_HAS_USER_INFO;
4867 if(This->userinfo_split != 0)
4868 *pdwProperties |= Uri_HAS_USER_NAME;
4870 if(This->userinfo_split > -1)
4871 *pdwProperties |= Uri_HAS_PASSWORD;
4872 if(This->host_start > -1)
4873 *pdwProperties |= Uri_HAS_HOST;
4874 if(This->domain_offset > -1)
4875 *pdwProperties |= Uri_HAS_DOMAIN;
4878 if(This->has_port)
4879 *pdwProperties |= Uri_HAS_PORT;
4880 if(This->path_start > -1)
4881 *pdwProperties |= Uri_HAS_PATH|Uri_HAS_PATH_AND_QUERY;
4882 if(This->query_start > -1)
4883 *pdwProperties |= Uri_HAS_QUERY|Uri_HAS_PATH_AND_QUERY;
4885 if(This->extension_offset > -1)
4886 *pdwProperties |= Uri_HAS_EXTENSION;
4888 if(This->fragment_start > -1)
4889 *pdwProperties |= Uri_HAS_FRAGMENT;
4891 return S_OK;
4894 static HRESULT WINAPI Uri_IsEqual(IUri *iface, IUri *pUri, BOOL *pfEqual)
4896 Uri *This = URI_THIS(iface);
4897 Uri *other;
4899 TRACE("(%p)->(%p %p)\n", This, pUri, pfEqual);
4901 if(!pfEqual)
4902 return E_POINTER;
4904 if(!pUri) {
4905 *pfEqual = FALSE;
4907 /* For some reason Windows returns S_OK here... */
4908 return S_OK;
4911 /* Try to convert it to a Uri (allows for a more simple comparison). */
4912 if((other = get_uri_obj(pUri)))
4913 *pfEqual = are_equal_simple(This, other);
4914 else {
4915 /* Do it the hard way. */
4916 FIXME("(%p)->(%p %p) No support for unknown IUri's yet.\n", iface, pUri, pfEqual);
4917 return E_NOTIMPL;
4920 return S_OK;
4923 #undef URI_THIS
4925 static const IUriVtbl UriVtbl = {
4926 Uri_QueryInterface,
4927 Uri_AddRef,
4928 Uri_Release,
4929 Uri_GetPropertyBSTR,
4930 Uri_GetPropertyLength,
4931 Uri_GetPropertyDWORD,
4932 Uri_HasProperty,
4933 Uri_GetAbsoluteUri,
4934 Uri_GetAuthority,
4935 Uri_GetDisplayUri,
4936 Uri_GetDomain,
4937 Uri_GetExtension,
4938 Uri_GetFragment,
4939 Uri_GetHost,
4940 Uri_GetPassword,
4941 Uri_GetPath,
4942 Uri_GetPathAndQuery,
4943 Uri_GetQuery,
4944 Uri_GetRawUri,
4945 Uri_GetSchemeName,
4946 Uri_GetUserInfo,
4947 Uri_GetUserName,
4948 Uri_GetHostType,
4949 Uri_GetPort,
4950 Uri_GetScheme,
4951 Uri_GetZone,
4952 Uri_GetProperties,
4953 Uri_IsEqual
4956 static Uri* create_uri_obj(void) {
4957 Uri *ret = heap_alloc_zero(sizeof(Uri));
4958 if(ret) {
4959 ret->lpIUriVtbl = &UriVtbl;
4960 ret->ref = 1;
4963 return ret;
4966 /***********************************************************************
4967 * CreateUri (urlmon.@)
4969 * Creates a new IUri object using the URI represented by pwzURI. This function
4970 * parses and validates the components of pwzURI and then canonicalizes the
4971 * parsed components.
4973 * PARAMS
4974 * pwzURI [I] The URI to parse, validate, and canonicalize.
4975 * dwFlags [I] Flags which can affect how the parsing/canonicalization is performed.
4976 * dwReserved [I] Reserved (not used).
4977 * ppURI [O] The resulting IUri after parsing/canonicalization occurs.
4979 * RETURNS
4980 * Success: Returns S_OK. ppURI contains the pointer to the newly allocated IUri.
4981 * Failure: E_INVALIDARG if there's invalid flag combinations in dwFlags, or an
4982 * invalid parameters, or pwzURI doesn't represnt a valid URI.
4983 * E_OUTOFMEMORY if any memory allocation fails.
4985 * NOTES
4986 * Default flags:
4987 * Uri_CREATE_CANONICALIZE, Uri_CREATE_DECODE_EXTRA_INFO, Uri_CREATE_CRACK_UNKNOWN_SCHEMES,
4988 * Uri_CREATE_PRE_PROCESS_HTML_URI, Uri_CREATE_NO_IE_SETTINGS.
4990 HRESULT WINAPI CreateUri(LPCWSTR pwzURI, DWORD dwFlags, DWORD_PTR dwReserved, IUri **ppURI)
4992 const DWORD supported_flags = Uri_CREATE_ALLOW_RELATIVE|Uri_CREATE_ALLOW_IMPLICIT_WILDCARD_SCHEME|
4993 Uri_CREATE_ALLOW_IMPLICIT_FILE_SCHEME|Uri_CREATE_NO_CANONICALIZE|Uri_CREATE_CANONICALIZE|
4994 Uri_CREATE_DECODE_EXTRA_INFO|Uri_CREATE_NO_DECODE_EXTRA_INFO|Uri_CREATE_CRACK_UNKNOWN_SCHEMES|
4995 Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES|Uri_CREATE_PRE_PROCESS_HTML_URI|Uri_CREATE_NO_PRE_PROCESS_HTML_URI|
4996 Uri_CREATE_NO_IE_SETTINGS|Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS|Uri_CREATE_FILE_USE_DOS_PATH;
4997 Uri *ret;
4998 HRESULT hr;
4999 parse_data data;
5001 TRACE("(%s %x %x %p)\n", debugstr_w(pwzURI), dwFlags, (DWORD)dwReserved, ppURI);
5003 if(!ppURI)
5004 return E_INVALIDARG;
5006 if(!pwzURI || !*pwzURI) {
5007 *ppURI = NULL;
5008 return E_INVALIDARG;
5011 /* Check for invalid flags. */
5012 if(has_invalid_flag_combination(dwFlags)) {
5013 *ppURI = NULL;
5014 return E_INVALIDARG;
5017 /* Currently unsupported. */
5018 if(dwFlags & ~supported_flags)
5019 FIXME("Ignoring unsupported flag(s) %x\n", dwFlags & ~supported_flags);
5021 ret = create_uri_obj();
5022 if(!ret) {
5023 *ppURI = NULL;
5024 return E_OUTOFMEMORY;
5027 /* Explicitly set the default flags if it doesn't cause a flag conflict. */
5028 apply_default_flags(&dwFlags);
5030 /* Pre process the URI, unless told otherwise. */
5031 if(!(dwFlags & Uri_CREATE_NO_PRE_PROCESS_HTML_URI))
5032 ret->raw_uri = pre_process_uri(pwzURI);
5033 else
5034 ret->raw_uri = SysAllocString(pwzURI);
5036 if(!ret->raw_uri) {
5037 heap_free(ret);
5038 return E_OUTOFMEMORY;
5041 memset(&data, 0, sizeof(parse_data));
5042 data.uri = ret->raw_uri;
5044 /* Validate and parse the URI into it's components. */
5045 if(!parse_uri(&data, dwFlags)) {
5046 /* Encountered an unsupported or invalid URI */
5047 IUri_Release(URI(ret));
5048 *ppURI = NULL;
5049 return E_INVALIDARG;
5052 /* Canonicalize the URI. */
5053 hr = canonicalize_uri(&data, ret, dwFlags);
5054 if(FAILED(hr)) {
5055 IUri_Release(URI(ret));
5056 *ppURI = NULL;
5057 return hr;
5060 ret->create_flags = dwFlags;
5062 *ppURI = URI(ret);
5063 return S_OK;
5066 /***********************************************************************
5067 * CreateUriWithFragment (urlmon.@)
5069 * Creates a new IUri object. This is almost the same as CreateUri, expect that
5070 * it allows you to explicitly specify a fragment (pwzFragment) for pwzURI.
5072 * PARAMS
5073 * pwzURI [I] The URI to parse and perform canonicalization on.
5074 * pwzFragment [I] The explict fragment string which should be added to pwzURI.
5075 * dwFlags [I] The flags which will be passed to CreateUri.
5076 * dwReserved [I] Reserved (not used).
5077 * ppURI [O] The resulting IUri after parsing/canonicalization.
5079 * RETURNS
5080 * Success: S_OK. ppURI contains the pointer to the newly allocated IUri.
5081 * Failure: E_INVALIDARG if pwzURI already contains a fragment and pwzFragment
5082 * isn't NULL. Will also return E_INVALIDARG for the same reasons as
5083 * CreateUri will. E_OUTOFMEMORY if any allocations fail.
5085 HRESULT WINAPI CreateUriWithFragment(LPCWSTR pwzURI, LPCWSTR pwzFragment, DWORD dwFlags,
5086 DWORD_PTR dwReserved, IUri **ppURI)
5088 HRESULT hres;
5089 TRACE("(%s %s %x %x %p)\n", debugstr_w(pwzURI), debugstr_w(pwzFragment), dwFlags, (DWORD)dwReserved, ppURI);
5091 if(!ppURI)
5092 return E_INVALIDARG;
5094 if(!pwzURI) {
5095 *ppURI = NULL;
5096 return E_INVALIDARG;
5099 /* Check if a fragment should be appended to the URI string. */
5100 if(pwzFragment) {
5101 WCHAR *uriW;
5102 DWORD uri_len, frag_len;
5103 BOOL add_pound;
5105 /* Check if the original URI already has a fragment component. */
5106 if(StrChrW(pwzURI, '#')) {
5107 *ppURI = NULL;
5108 return E_INVALIDARG;
5111 uri_len = lstrlenW(pwzURI);
5112 frag_len = lstrlenW(pwzFragment);
5114 /* If the fragment doesn't start with a '#', one will be added. */
5115 add_pound = *pwzFragment != '#';
5117 if(add_pound)
5118 uriW = heap_alloc((uri_len+frag_len+2)*sizeof(WCHAR));
5119 else
5120 uriW = heap_alloc((uri_len+frag_len+1)*sizeof(WCHAR));
5122 if(!uriW)
5123 return E_OUTOFMEMORY;
5125 memcpy(uriW, pwzURI, uri_len*sizeof(WCHAR));
5126 if(add_pound)
5127 uriW[uri_len++] = '#';
5128 memcpy(uriW+uri_len, pwzFragment, (frag_len+1)*sizeof(WCHAR));
5130 hres = CreateUri(uriW, dwFlags, 0, ppURI);
5132 heap_free(uriW);
5133 } else
5134 /* A fragment string wasn't specified, so just forward the call. */
5135 hres = CreateUri(pwzURI, dwFlags, 0, ppURI);
5137 return hres;
5140 static HRESULT build_uri(const UriBuilder *builder, IUri **uri, DWORD create_flags,
5141 DWORD use_orig_flags, DWORD encoding_mask)
5143 HRESULT hr;
5144 parse_data data;
5145 Uri *ret;
5147 if(!uri)
5148 return E_POINTER;
5150 if(encoding_mask && (!builder->uri || builder->modified_props)) {
5151 *uri = NULL;
5152 return E_NOTIMPL;
5155 /* Decide what flags should be used when creating the Uri. */
5156 if((use_orig_flags & UriBuilder_USE_ORIGINAL_FLAGS) && builder->uri)
5157 create_flags = builder->uri->create_flags;
5158 else {
5159 if(has_invalid_flag_combination(create_flags)) {
5160 *uri = NULL;
5161 return E_INVALIDARG;
5164 /* Set the default flags if they don't cause a conflict. */
5165 apply_default_flags(&create_flags);
5168 /* Return the base IUri if no changes have been made and the create_flags match. */
5169 if(builder->uri && !builder->modified_props && builder->uri->create_flags == create_flags) {
5170 *uri = URI(builder->uri);
5171 IUri_AddRef(*uri);
5172 return S_OK;
5175 hr = validate_components(builder, &data, create_flags);
5176 if(FAILED(hr)) {
5177 *uri = NULL;
5178 return hr;
5181 ret = create_uri_obj();
5182 if(!ret) {
5183 *uri = NULL;
5184 return E_OUTOFMEMORY;
5187 hr = generate_uri(builder, &data, ret, create_flags);
5188 if(FAILED(hr)) {
5189 IUri_Release(URI(ret));
5190 *uri = NULL;
5191 return hr;
5194 *uri = URI(ret);
5195 return S_OK;
5198 #define URIBUILDER_THIS(iface) DEFINE_THIS(UriBuilder, IUriBuilder, iface)
5200 static HRESULT WINAPI UriBuilder_QueryInterface(IUriBuilder *iface, REFIID riid, void **ppv)
5202 UriBuilder *This = URIBUILDER_THIS(iface);
5204 if(IsEqualGUID(&IID_IUnknown, riid)) {
5205 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
5206 *ppv = URIBUILDER(This);
5207 }else if(IsEqualGUID(&IID_IUriBuilder, riid)) {
5208 TRACE("(%p)->(IID_IUri %p)\n", This, ppv);
5209 *ppv = URIBUILDER(This);
5210 }else {
5211 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
5212 *ppv = NULL;
5213 return E_NOINTERFACE;
5216 IUnknown_AddRef((IUnknown*)*ppv);
5217 return S_OK;
5220 static ULONG WINAPI UriBuilder_AddRef(IUriBuilder *iface)
5222 UriBuilder *This = URIBUILDER_THIS(iface);
5223 LONG ref = InterlockedIncrement(&This->ref);
5225 TRACE("(%p) ref=%d\n", This, ref);
5227 return ref;
5230 static ULONG WINAPI UriBuilder_Release(IUriBuilder *iface)
5232 UriBuilder *This = URIBUILDER_THIS(iface);
5233 LONG ref = InterlockedDecrement(&This->ref);
5235 TRACE("(%p) ref=%d\n", This, ref);
5237 if(!ref) {
5238 if(This->uri) IUri_Release(URI(This->uri));
5239 heap_free(This->fragment);
5240 heap_free(This->host);
5241 heap_free(This->password);
5242 heap_free(This->path);
5243 heap_free(This->query);
5244 heap_free(This->scheme);
5245 heap_free(This->username);
5246 heap_free(This);
5249 return ref;
5252 static HRESULT WINAPI UriBuilder_CreateUriSimple(IUriBuilder *iface,
5253 DWORD dwAllowEncodingPropertyMask,
5254 DWORD_PTR dwReserved,
5255 IUri **ppIUri)
5257 UriBuilder *This = URIBUILDER_THIS(iface);
5258 HRESULT hr;
5259 TRACE("(%p)->(%d %d %p)\n", This, dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
5261 hr = build_uri(This, ppIUri, 0, UriBuilder_USE_ORIGINAL_FLAGS, dwAllowEncodingPropertyMask);
5262 if(hr == E_NOTIMPL)
5263 FIXME("(%p)->(%d %d %p)\n", This, dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
5264 return hr;
5267 static HRESULT WINAPI UriBuilder_CreateUri(IUriBuilder *iface,
5268 DWORD dwCreateFlags,
5269 DWORD dwAllowEncodingPropertyMask,
5270 DWORD_PTR dwReserved,
5271 IUri **ppIUri)
5273 UriBuilder *This = URIBUILDER_THIS(iface);
5274 HRESULT hr;
5275 TRACE("(%p)->(0x%08x %d %d %p)\n", This, dwCreateFlags, dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
5277 if(dwCreateFlags == -1)
5278 hr = build_uri(This, ppIUri, 0, UriBuilder_USE_ORIGINAL_FLAGS, dwAllowEncodingPropertyMask);
5279 else
5280 hr = build_uri(This, ppIUri, dwCreateFlags, 0, dwAllowEncodingPropertyMask);
5282 if(hr == E_NOTIMPL)
5283 FIXME("(%p)->(0x%08x %d %d %p)\n", This, dwCreateFlags, dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
5284 return hr;
5287 static HRESULT WINAPI UriBuilder_CreateUriWithFlags(IUriBuilder *iface,
5288 DWORD dwCreateFlags,
5289 DWORD dwUriBuilderFlags,
5290 DWORD dwAllowEncodingPropertyMask,
5291 DWORD_PTR dwReserved,
5292 IUri **ppIUri)
5294 UriBuilder *This = URIBUILDER_THIS(iface);
5295 HRESULT hr;
5296 TRACE("(%p)->(0x%08x 0x%08x %d %d %p)\n", This, dwCreateFlags, dwUriBuilderFlags,
5297 dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
5299 hr = build_uri(This, ppIUri, dwCreateFlags, dwUriBuilderFlags, dwAllowEncodingPropertyMask);
5300 if(hr == E_NOTIMPL)
5301 FIXME("(%p)->(0x%08x 0x%08x %d %d %p)\n", This, dwCreateFlags, dwUriBuilderFlags,
5302 dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
5303 return hr;
5306 static HRESULT WINAPI UriBuilder_GetIUri(IUriBuilder *iface, IUri **ppIUri)
5308 UriBuilder *This = URIBUILDER_THIS(iface);
5309 TRACE("(%p)->(%p)\n", This, ppIUri);
5311 if(!ppIUri)
5312 return E_POINTER;
5314 if(This->uri) {
5315 IUri *uri = URI(This->uri);
5316 IUri_AddRef(uri);
5317 *ppIUri = uri;
5318 } else
5319 *ppIUri = NULL;
5321 return S_OK;
5324 static HRESULT WINAPI UriBuilder_SetIUri(IUriBuilder *iface, IUri *pIUri)
5326 UriBuilder *This = URIBUILDER_THIS(iface);
5327 TRACE("(%p)->(%p)\n", This, pIUri);
5329 if(pIUri) {
5330 Uri *uri;
5332 if((uri = get_uri_obj(pIUri))) {
5333 /* Only reset the builder if it's Uri isn't the same as
5334 * the Uri passed to the function.
5336 if(This->uri != uri) {
5337 reset_builder(This);
5339 This->uri = uri;
5340 if(uri->has_port)
5341 This->port = uri->port;
5343 IUri_AddRef(pIUri);
5345 } else {
5346 FIXME("(%p)->(%p) Unknown IUri types not supported yet.\n", This, pIUri);
5347 return E_NOTIMPL;
5349 } else if(This->uri)
5350 /* Only reset the builder if it's Uri isn't NULL. */
5351 reset_builder(This);
5353 return S_OK;
5356 static HRESULT WINAPI UriBuilder_GetFragment(IUriBuilder *iface, DWORD *pcchFragment, LPCWSTR *ppwzFragment)
5358 UriBuilder *This = URIBUILDER_THIS(iface);
5359 TRACE("(%p)->(%p %p)\n", This, pcchFragment, ppwzFragment);
5361 if(!This->uri || This->uri->fragment_start == -1 || This->modified_props & Uri_HAS_FRAGMENT)
5362 return get_builder_component(&This->fragment, &This->fragment_len, NULL, 0, ppwzFragment, pcchFragment);
5363 else
5364 return get_builder_component(&This->fragment, &This->fragment_len, This->uri->canon_uri+This->uri->fragment_start,
5365 This->uri->fragment_len, ppwzFragment, pcchFragment);
5368 static HRESULT WINAPI UriBuilder_GetHost(IUriBuilder *iface, DWORD *pcchHost, LPCWSTR *ppwzHost)
5370 UriBuilder *This = URIBUILDER_THIS(iface);
5371 TRACE("(%p)->(%p %p)\n", This, pcchHost, ppwzHost);
5373 if(!This->uri || This->uri->host_start == -1 || This->modified_props & Uri_HAS_HOST)
5374 return get_builder_component(&This->host, &This->host_len, NULL, 0, ppwzHost, pcchHost);
5375 else {
5376 if(This->uri->host_type == Uri_HOST_IPV6)
5377 /* Don't include the '[' and ']' around the address. */
5378 return get_builder_component(&This->host, &This->host_len, This->uri->canon_uri+This->uri->host_start+1,
5379 This->uri->host_len-2, ppwzHost, pcchHost);
5380 else
5381 return get_builder_component(&This->host, &This->host_len, This->uri->canon_uri+This->uri->host_start,
5382 This->uri->host_len, ppwzHost, pcchHost);
5386 static HRESULT WINAPI UriBuilder_GetPassword(IUriBuilder *iface, DWORD *pcchPassword, LPCWSTR *ppwzPassword)
5388 UriBuilder *This = URIBUILDER_THIS(iface);
5389 TRACE("(%p)->(%p %p)\n", This, pcchPassword, ppwzPassword);
5391 if(!This->uri || This->uri->userinfo_split == -1 || This->modified_props & Uri_HAS_PASSWORD)
5392 return get_builder_component(&This->password, &This->password_len, NULL, 0, ppwzPassword, pcchPassword);
5393 else {
5394 const WCHAR *start = This->uri->canon_uri+This->uri->userinfo_start+This->uri->userinfo_split+1;
5395 DWORD len = This->uri->userinfo_len-This->uri->userinfo_split-1;
5396 return get_builder_component(&This->password, &This->password_len, start, len, ppwzPassword, pcchPassword);
5400 static HRESULT WINAPI UriBuilder_GetPath(IUriBuilder *iface, DWORD *pcchPath, LPCWSTR *ppwzPath)
5402 UriBuilder *This = URIBUILDER_THIS(iface);
5403 TRACE("(%p)->(%p %p)\n", This, pcchPath, ppwzPath);
5405 if(!This->uri || This->uri->path_start == -1 || This->modified_props & Uri_HAS_PATH)
5406 return get_builder_component(&This->path, &This->path_len, NULL, 0, ppwzPath, pcchPath);
5407 else
5408 return get_builder_component(&This->path, &This->path_len, This->uri->canon_uri+This->uri->path_start,
5409 This->uri->path_len, ppwzPath, pcchPath);
5412 static HRESULT WINAPI UriBuilder_GetPort(IUriBuilder *iface, BOOL *pfHasPort, DWORD *pdwPort)
5414 UriBuilder *This = URIBUILDER_THIS(iface);
5415 TRACE("(%p)->(%p %p)\n", This, pfHasPort, pdwPort);
5417 if(!pfHasPort) {
5418 if(pdwPort)
5419 *pdwPort = 0;
5420 return E_POINTER;
5423 if(!pdwPort) {
5424 *pfHasPort = FALSE;
5425 return E_POINTER;
5428 *pfHasPort = This->has_port;
5429 *pdwPort = This->port;
5430 return S_OK;
5433 static HRESULT WINAPI UriBuilder_GetQuery(IUriBuilder *iface, DWORD *pcchQuery, LPCWSTR *ppwzQuery)
5435 UriBuilder *This = URIBUILDER_THIS(iface);
5436 TRACE("(%p)->(%p %p)\n", This, pcchQuery, ppwzQuery);
5438 if(!This->uri || This->uri->query_start == -1 || This->modified_props & Uri_HAS_QUERY)
5439 return get_builder_component(&This->query, &This->query_len, NULL, 0, ppwzQuery, pcchQuery);
5440 else
5441 return get_builder_component(&This->query, &This->query_len, This->uri->canon_uri+This->uri->query_start,
5442 This->uri->query_len, ppwzQuery, pcchQuery);
5445 static HRESULT WINAPI UriBuilder_GetSchemeName(IUriBuilder *iface, DWORD *pcchSchemeName, LPCWSTR *ppwzSchemeName)
5447 UriBuilder *This = URIBUILDER_THIS(iface);
5448 TRACE("(%p)->(%p %p)\n", This, pcchSchemeName, ppwzSchemeName);
5450 if(!This->uri || This->uri->scheme_start == -1 || This->modified_props & Uri_HAS_SCHEME_NAME)
5451 return get_builder_component(&This->scheme, &This->scheme_len, NULL, 0, ppwzSchemeName, pcchSchemeName);
5452 else
5453 return get_builder_component(&This->scheme, &This->scheme_len, This->uri->canon_uri+This->uri->scheme_start,
5454 This->uri->scheme_len, ppwzSchemeName, pcchSchemeName);
5457 static HRESULT WINAPI UriBuilder_GetUserName(IUriBuilder *iface, DWORD *pcchUserName, LPCWSTR *ppwzUserName)
5459 UriBuilder *This = URIBUILDER_THIS(iface);
5460 TRACE("(%p)->(%p %p)\n", This, pcchUserName, ppwzUserName);
5462 if(!This->uri || This->uri->userinfo_start == -1 || This->uri->userinfo_split == 0 ||
5463 This->modified_props & Uri_HAS_USER_NAME)
5464 return get_builder_component(&This->username, &This->username_len, NULL, 0, ppwzUserName, pcchUserName);
5465 else {
5466 const WCHAR *start = This->uri->canon_uri+This->uri->userinfo_start;
5468 /* Check if there's a password in the userinfo section. */
5469 if(This->uri->userinfo_split > -1)
5470 /* Don't include the password. */
5471 return get_builder_component(&This->username, &This->username_len, start,
5472 This->uri->userinfo_split, ppwzUserName, pcchUserName);
5473 else
5474 return get_builder_component(&This->username, &This->username_len, start,
5475 This->uri->userinfo_len, ppwzUserName, pcchUserName);
5479 static HRESULT WINAPI UriBuilder_SetFragment(IUriBuilder *iface, LPCWSTR pwzNewValue)
5481 UriBuilder *This = URIBUILDER_THIS(iface);
5482 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
5483 return set_builder_component(&This->fragment, &This->fragment_len, pwzNewValue, '#',
5484 &This->modified_props, Uri_HAS_FRAGMENT);
5487 static HRESULT WINAPI UriBuilder_SetHost(IUriBuilder *iface, LPCWSTR pwzNewValue)
5489 UriBuilder *This = URIBUILDER_THIS(iface);
5490 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
5492 /* Host name can't be set to NULL. */
5493 if(!pwzNewValue)
5494 return E_INVALIDARG;
5496 return set_builder_component(&This->host, &This->host_len, pwzNewValue, 0,
5497 &This->modified_props, Uri_HAS_HOST);
5500 static HRESULT WINAPI UriBuilder_SetPassword(IUriBuilder *iface, LPCWSTR pwzNewValue)
5502 UriBuilder *This = URIBUILDER_THIS(iface);
5503 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
5504 return set_builder_component(&This->password, &This->password_len, pwzNewValue, 0,
5505 &This->modified_props, Uri_HAS_PASSWORD);
5508 static HRESULT WINAPI UriBuilder_SetPath(IUriBuilder *iface, LPCWSTR pwzNewValue)
5510 UriBuilder *This = URIBUILDER_THIS(iface);
5511 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
5512 return set_builder_component(&This->path, &This->path_len, pwzNewValue, 0,
5513 &This->modified_props, Uri_HAS_PATH);
5516 static HRESULT WINAPI UriBuilder_SetPort(IUriBuilder *iface, BOOL fHasPort, DWORD dwNewValue)
5518 UriBuilder *This = URIBUILDER_THIS(iface);
5519 TRACE("(%p)->(%d %d)\n", This, fHasPort, dwNewValue);
5521 This->has_port = fHasPort;
5522 This->port = dwNewValue;
5523 This->modified_props |= Uri_HAS_PORT;
5524 return S_OK;
5527 static HRESULT WINAPI UriBuilder_SetQuery(IUriBuilder *iface, LPCWSTR pwzNewValue)
5529 UriBuilder *This = URIBUILDER_THIS(iface);
5530 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
5531 return set_builder_component(&This->query, &This->query_len, pwzNewValue, '?',
5532 &This->modified_props, Uri_HAS_QUERY);
5535 static HRESULT WINAPI UriBuilder_SetSchemeName(IUriBuilder *iface, LPCWSTR pwzNewValue)
5537 UriBuilder *This = URIBUILDER_THIS(iface);
5538 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
5540 /* Only set the scheme name if it's not NULL or empty. */
5541 if(!pwzNewValue || !*pwzNewValue)
5542 return E_INVALIDARG;
5544 return set_builder_component(&This->scheme, &This->scheme_len, pwzNewValue, 0,
5545 &This->modified_props, Uri_HAS_SCHEME_NAME);
5548 static HRESULT WINAPI UriBuilder_SetUserName(IUriBuilder *iface, LPCWSTR pwzNewValue)
5550 UriBuilder *This = URIBUILDER_THIS(iface);
5551 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
5552 return set_builder_component(&This->username, &This->username_len, pwzNewValue, 0,
5553 &This->modified_props, Uri_HAS_USER_NAME);
5556 static HRESULT WINAPI UriBuilder_RemoveProperties(IUriBuilder *iface, DWORD dwPropertyMask)
5558 const DWORD accepted_flags = Uri_HAS_AUTHORITY|Uri_HAS_DOMAIN|Uri_HAS_EXTENSION|Uri_HAS_FRAGMENT|Uri_HAS_HOST|
5559 Uri_HAS_PASSWORD|Uri_HAS_PATH|Uri_HAS_PATH_AND_QUERY|Uri_HAS_QUERY|
5560 Uri_HAS_USER_INFO|Uri_HAS_USER_NAME;
5562 UriBuilder *This = URIBUILDER_THIS(iface);
5563 TRACE("(%p)->(0x%08x)\n", This, dwPropertyMask);
5565 if(dwPropertyMask & ~accepted_flags)
5566 return E_INVALIDARG;
5568 if(dwPropertyMask & Uri_HAS_FRAGMENT)
5569 UriBuilder_SetFragment(iface, NULL);
5571 /* Even though you can't set the host name to NULL or an
5572 * empty string, you can still remove it... for some reason.
5574 if(dwPropertyMask & Uri_HAS_HOST)
5575 set_builder_component(&This->host, &This->host_len, NULL, 0,
5576 &This->modified_props, Uri_HAS_HOST);
5578 if(dwPropertyMask & Uri_HAS_PASSWORD)
5579 UriBuilder_SetPassword(iface, NULL);
5581 if(dwPropertyMask & Uri_HAS_PATH)
5582 UriBuilder_SetPath(iface, NULL);
5584 if(dwPropertyMask & Uri_HAS_PORT)
5585 UriBuilder_SetPort(iface, FALSE, 0);
5587 if(dwPropertyMask & Uri_HAS_QUERY)
5588 UriBuilder_SetQuery(iface, NULL);
5590 if(dwPropertyMask & Uri_HAS_USER_NAME)
5591 UriBuilder_SetUserName(iface, NULL);
5593 return S_OK;
5596 static HRESULT WINAPI UriBuilder_HasBeenModified(IUriBuilder *iface, BOOL *pfModified)
5598 UriBuilder *This = URIBUILDER_THIS(iface);
5599 TRACE("(%p)->(%p)\n", This, pfModified);
5601 if(!pfModified)
5602 return E_POINTER;
5604 *pfModified = This->modified_props > 0;
5605 return S_OK;
5608 #undef URIBUILDER_THIS
5610 static const IUriBuilderVtbl UriBuilderVtbl = {
5611 UriBuilder_QueryInterface,
5612 UriBuilder_AddRef,
5613 UriBuilder_Release,
5614 UriBuilder_CreateUriSimple,
5615 UriBuilder_CreateUri,
5616 UriBuilder_CreateUriWithFlags,
5617 UriBuilder_GetIUri,
5618 UriBuilder_SetIUri,
5619 UriBuilder_GetFragment,
5620 UriBuilder_GetHost,
5621 UriBuilder_GetPassword,
5622 UriBuilder_GetPath,
5623 UriBuilder_GetPort,
5624 UriBuilder_GetQuery,
5625 UriBuilder_GetSchemeName,
5626 UriBuilder_GetUserName,
5627 UriBuilder_SetFragment,
5628 UriBuilder_SetHost,
5629 UriBuilder_SetPassword,
5630 UriBuilder_SetPath,
5631 UriBuilder_SetPort,
5632 UriBuilder_SetQuery,
5633 UriBuilder_SetSchemeName,
5634 UriBuilder_SetUserName,
5635 UriBuilder_RemoveProperties,
5636 UriBuilder_HasBeenModified,
5639 /***********************************************************************
5640 * CreateIUriBuilder (urlmon.@)
5642 HRESULT WINAPI CreateIUriBuilder(IUri *pIUri, DWORD dwFlags, DWORD_PTR dwReserved, IUriBuilder **ppIUriBuilder)
5644 UriBuilder *ret;
5646 TRACE("(%p %x %x %p)\n", pIUri, dwFlags, (DWORD)dwReserved, ppIUriBuilder);
5648 if(!ppIUriBuilder)
5649 return E_POINTER;
5651 ret = heap_alloc_zero(sizeof(UriBuilder));
5652 if(!ret)
5653 return E_OUTOFMEMORY;
5655 ret->lpIUriBuilderVtbl = &UriBuilderVtbl;
5656 ret->ref = 1;
5658 if(pIUri) {
5659 Uri *uri;
5661 if((uri = get_uri_obj(pIUri))) {
5662 IUri_AddRef(pIUri);
5663 ret->uri = uri;
5665 if(uri->has_port)
5666 /* Windows doesn't set 'has_port' to TRUE in this case. */
5667 ret->port = uri->port;
5669 } else {
5670 heap_free(ret);
5671 *ppIUriBuilder = NULL;
5672 FIXME("(%p %x %x %p): Unknown IUri types not supported yet.\n", pIUri, dwFlags,
5673 (DWORD)dwReserved, ppIUriBuilder);
5674 return E_NOTIMPL;
5678 *ppIUriBuilder = URIBUILDER(ret);
5679 return S_OK;
5682 /* Merges the base path with the relative path and stores the resulting path
5683 * and path len in 'result' and 'result_len'.
5685 static HRESULT merge_paths(parse_data *data, const WCHAR *base, DWORD base_len, const WCHAR *relative,
5686 DWORD relative_len, WCHAR **result, DWORD *result_len, DWORD flags)
5688 const WCHAR *end = NULL;
5689 DWORD base_copy_len = 0;
5690 WCHAR *ptr;
5692 if(base_len) {
5693 /* Find the characters the will be copied over from
5694 * the base path.
5696 end = str_last_of(base, base+(base_len-1), '/');
5697 if(!end && data->scheme_type == URL_SCHEME_FILE)
5698 /* Try looking for a '\\'. */
5699 end = str_last_of(base, base+(base_len-1), '\\');
5702 if(end) {
5703 base_copy_len = (end+1)-base;
5704 *result = heap_alloc((base_copy_len+relative_len+1)*sizeof(WCHAR));
5705 } else
5706 *result = heap_alloc((relative_len+1)*sizeof(WCHAR));
5708 if(!(*result)) {
5709 *result_len = 0;
5710 return E_OUTOFMEMORY;
5713 ptr = *result;
5714 if(end) {
5715 memcpy(ptr, base, base_copy_len*sizeof(WCHAR));
5716 ptr += base_copy_len;
5719 memcpy(ptr, relative, relative_len*sizeof(WCHAR));
5720 ptr += relative_len;
5721 *ptr = '\0';
5723 *result_len = (ptr-*result);
5724 return S_OK;
5727 static HRESULT combine_uri(Uri *base, Uri *relative, DWORD flags, IUri **result) {
5728 Uri *ret;
5729 HRESULT hr;
5730 parse_data data;
5731 DWORD create_flags = 0, len = 0;
5733 memset(&data, 0, sizeof(parse_data));
5735 /* Base case is when the relative Uri has a scheme name,
5736 * if it does, then 'result' will contain the same data
5737 * as the relative Uri.
5739 if(relative->scheme_start > -1) {
5740 data.uri = SysAllocString(relative->raw_uri);
5741 if(!data.uri) {
5742 *result = NULL;
5743 return E_OUTOFMEMORY;
5746 parse_uri(&data, 0);
5748 ret = create_uri_obj();
5749 if(!ret) {
5750 *result = NULL;
5751 return E_OUTOFMEMORY;
5754 ret->raw_uri = data.uri;
5755 hr = canonicalize_uri(&data, ret, 0);
5756 if(FAILED(hr)) {
5757 IUri_Release(URI(ret));
5758 *result = NULL;
5759 return hr;
5762 apply_default_flags(&create_flags);
5763 ret->create_flags = create_flags;
5765 *result = URI(ret);
5766 } else {
5767 WCHAR *path = NULL;
5768 DWORD raw_flags = 0;
5770 if(base->scheme_start > -1) {
5771 data.scheme = base->canon_uri+base->scheme_start;
5772 data.scheme_len = base->scheme_len;
5773 data.scheme_type = base->scheme_type;
5774 } else {
5775 data.is_relative = TRUE;
5776 data.scheme_type = URL_SCHEME_UNKNOWN;
5777 create_flags |= Uri_CREATE_ALLOW_RELATIVE;
5780 if(base->authority_start > -1) {
5781 if(base->userinfo_start > -1 && base->userinfo_split != 0) {
5782 data.username = base->canon_uri+base->userinfo_start;
5783 data.username_len = (base->userinfo_split > -1) ? base->userinfo_split : base->userinfo_len;
5786 if(base->userinfo_split > -1) {
5787 data.password = base->canon_uri+base->userinfo_start+base->userinfo_split+1;
5788 data.password_len = base->userinfo_len-base->userinfo_split-1;
5791 if(base->host_start > -1) {
5792 data.host = base->canon_uri+base->host_start;
5793 data.host_len = base->host_len;
5794 data.host_type = base->host_type;
5797 if(base->has_port) {
5798 data.has_port = TRUE;
5799 data.port_value = base->port;
5801 } else if(base->scheme_type != URL_SCHEME_FILE)
5802 data.is_opaque = TRUE;
5804 if(relative->path_start == -1 || !relative->path_len) {
5805 if(base->path_start > -1) {
5806 data.path = base->canon_uri+base->path_start;
5807 data.path_len = base->path_len;
5808 } else if((base->path_start == -1 || !base->path_len) && !data.is_opaque) {
5809 /* Just set the path as a '/' if the base didn't have
5810 * one and if it's an hierarchical URI.
5812 static const WCHAR slashW[] = {'/',0};
5813 data.path = slashW;
5814 data.path_len = 1;
5817 if(relative->query_start > -1) {
5818 data.query = relative->canon_uri+relative->query_start;
5819 data.query_len = relative->query_len;
5820 } else if(base->query_start > -1) {
5821 data.query = base->canon_uri+base->query_start;
5822 data.query_len = base->query_len;
5824 } else {
5825 const WCHAR *ptr, **pptr;
5826 DWORD path_offset = 0, path_len = 0;
5828 /* There's two possibilities on what will happen to the path component
5829 * of the result IUri. First, if the relative path begins with a '/'
5830 * then the resulting path will just be the relative path. Second, if
5831 * relative path doesn't begin with a '/' then the base path and relative
5832 * path are merged together.
5834 if(relative->path_len && *(relative->canon_uri+relative->path_start) == '/') {
5835 WCHAR *tmp = NULL;
5836 BOOL copy_drive_path = FALSE;
5838 /* If the relative IUri's path starts with a '/', then we
5839 * don't use the base IUri's path. Unless the base IUri
5840 * is a file URI, in which case it uses the drive path of
5841 * the base IUri (if it has any) in the new path.
5843 if(base->scheme_type == URL_SCHEME_FILE) {
5844 if(base->path_len > 3 && *(base->canon_uri+base->path_start) == '/' &&
5845 is_drive_path(base->canon_uri+base->path_start+1)) {
5846 path_len += 3;
5847 copy_drive_path = TRUE;
5851 path_len += relative->path_len;
5853 path = heap_alloc((path_len+1)*sizeof(WCHAR));
5854 if(!path) {
5855 *result = NULL;
5856 return E_OUTOFMEMORY;
5859 tmp = path;
5861 /* Copy the base paths, drive path over. */
5862 if(copy_drive_path) {
5863 memcpy(tmp, base->canon_uri+base->path_start, 3*sizeof(WCHAR));
5864 tmp += 3;
5867 memcpy(tmp, relative->canon_uri+relative->path_start, relative->path_len*sizeof(WCHAR));
5868 path[path_len] = '\0';
5869 } else {
5870 /* Merge the base path with the relative path. */
5871 hr = merge_paths(&data, base->canon_uri+base->path_start, base->path_len,
5872 relative->canon_uri+relative->path_start, relative->path_len,
5873 &path, &path_len, flags);
5874 if(FAILED(hr)) {
5875 *result = NULL;
5876 return hr;
5879 /* If the resulting IUri is a file URI, the drive path isn't
5880 * reduced out when the dot segments are removed.
5882 if(path_len >= 3 && data.scheme_type == URL_SCHEME_FILE && !data.host) {
5883 if(*path == '/' && is_drive_path(path+1))
5884 path_offset = 2;
5885 else if(is_drive_path(path))
5886 path_offset = 1;
5890 /* Check if the dot segments need to be removed from the path. */
5891 if(!(flags & URL_DONT_SIMPLIFY) && !data.is_opaque) {
5892 DWORD offset = (path_offset > 0) ? path_offset+1 : 0;
5893 DWORD new_len = remove_dot_segments(path+offset,path_len-offset);
5895 if(new_len != path_len) {
5896 WCHAR *tmp = heap_realloc(path, (path_offset+new_len+1)*sizeof(WCHAR));
5897 if(!tmp) {
5898 heap_free(path);
5899 *result = NULL;
5900 return E_OUTOFMEMORY;
5903 tmp[new_len+offset] = '\0';
5904 path = tmp;
5905 path_len = new_len+offset;
5909 /* Make sure the path component is valid. */
5910 ptr = path;
5911 pptr = &ptr;
5912 if((data.is_opaque && !parse_path_opaque(pptr, &data, 0)) ||
5913 (!data.is_opaque && !parse_path_hierarchical(pptr, &data, 0))) {
5914 heap_free(path);
5915 *result = NULL;
5916 return E_INVALIDARG;
5920 if(relative->fragment_start > -1) {
5921 data.fragment = relative->canon_uri+relative->fragment_start;
5922 data.fragment_len = relative->fragment_len;
5925 if(flags & URL_DONT_SIMPLIFY)
5926 raw_flags |= RAW_URI_FORCE_PORT_DISP;
5927 if(flags & URL_FILE_USE_PATHURL)
5928 raw_flags |= RAW_URI_CONVERT_TO_DOS_PATH;
5930 len = generate_raw_uri(&data, data.uri, raw_flags);
5931 data.uri = SysAllocStringLen(NULL, len);
5932 if(!data.uri) {
5933 heap_free(path);
5934 *result = NULL;
5935 return E_OUTOFMEMORY;
5938 generate_raw_uri(&data, data.uri, raw_flags);
5940 ret = create_uri_obj();
5941 if(!ret) {
5942 SysFreeString(data.uri);
5943 heap_free(path);
5944 *result = NULL;
5945 return E_OUTOFMEMORY;
5948 if(flags & URL_DONT_SIMPLIFY)
5949 create_flags |= Uri_CREATE_NO_CANONICALIZE;
5950 if(flags & URL_FILE_USE_PATHURL)
5951 create_flags |= Uri_CREATE_FILE_USE_DOS_PATH;
5953 ret->raw_uri = data.uri;
5954 hr = canonicalize_uri(&data, ret, create_flags);
5955 if(FAILED(hr)) {
5956 IUri_Release(URI(ret));
5957 *result = NULL;
5958 return hr;
5961 if(flags & URL_DONT_SIMPLIFY)
5962 ret->display_modifiers |= URI_DISPLAY_NO_DEFAULT_PORT_AUTH;
5964 apply_default_flags(&create_flags);
5965 ret->create_flags = create_flags;
5966 *result = URI(ret);
5968 heap_free(path);
5971 return S_OK;
5974 /***********************************************************************
5975 * CoInternetCombineIUri (urlmon.@)
5977 HRESULT WINAPI CoInternetCombineIUri(IUri *pBaseUri, IUri *pRelativeUri, DWORD dwCombineFlags,
5978 IUri **ppCombinedUri, DWORD_PTR dwReserved)
5980 HRESULT hr;
5981 Uri *relative, *base;
5982 TRACE("(%p %p %x %p %x)\n", pBaseUri, pRelativeUri, dwCombineFlags, ppCombinedUri, (DWORD)dwReserved);
5984 if(!ppCombinedUri)
5985 return E_INVALIDARG;
5987 if(!pBaseUri || !pRelativeUri) {
5988 *ppCombinedUri = NULL;
5989 return E_INVALIDARG;
5992 relative = get_uri_obj(pRelativeUri);
5993 base = get_uri_obj(pBaseUri);
5994 if(!relative || !base) {
5995 *ppCombinedUri = NULL;
5996 FIXME("(%p %p %x %p %x) Unknown IUri types not supported yet.\n",
5997 pBaseUri, pRelativeUri, dwCombineFlags, ppCombinedUri, (DWORD)dwReserved);
5998 return E_NOTIMPL;
6001 hr = combine_uri(base, relative, dwCombineFlags, ppCombinedUri);
6002 if(hr == E_NOTIMPL)
6003 FIXME("(%p %p %x %p %x): stub\n", pBaseUri, pRelativeUri, dwCombineFlags, ppCombinedUri, (DWORD)dwReserved);
6004 return hr;