urlmon: Fixed UriBuilderFactory IUnknown implementation.
[wine/multimedia.git] / dlls / urlmon / uri.c
blobd334b9e398815815553f2531bea3f5aa32adb81e
1 /*
2 * Copyright 2010 Jacek Caban for CodeWeavers
3 * Copyright 2010 Thomas Mullaly
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #include "urlmon_main.h"
21 #include "wine/debug.h"
23 #define NO_SHLWAPI_REG
24 #include "shlwapi.h"
26 #include "strsafe.h"
28 #define UINT_MAX 0xffffffff
29 #define USHORT_MAX 0xffff
31 #define URI_DISPLAY_NO_ABSOLUTE_URI 0x1
32 #define URI_DISPLAY_NO_DEFAULT_PORT_AUTH 0x2
34 #define ALLOW_NULL_TERM_SCHEME 0x01
35 #define ALLOW_NULL_TERM_USER_NAME 0x02
36 #define ALLOW_NULL_TERM_PASSWORD 0x04
37 #define ALLOW_BRACKETLESS_IP_LITERAL 0x08
38 #define SKIP_IP_FUTURE_CHECK 0x10
39 #define IGNORE_PORT_DELIMITER 0x20
41 #define RAW_URI_FORCE_PORT_DISP 0x1
42 #define RAW_URI_CONVERT_TO_DOS_PATH 0x2
44 #define COMBINE_URI_FORCE_FLAG_USE 0x1
46 WINE_DEFAULT_DEBUG_CHANNEL(urlmon);
48 static const IID IID_IUriObj = {0x4b364760,0x9f51,0x11df,{0x98,0x1c,0x08,0x00,0x20,0x0c,0x9a,0x66}};
50 typedef struct {
51 IUri IUri_iface;
52 IUriBuilderFactory IUriBuilderFactory_iface;
54 LONG ref;
56 BSTR raw_uri;
58 /* Information about the canonicalized URI's buffer. */
59 WCHAR *canon_uri;
60 DWORD canon_size;
61 DWORD canon_len;
62 BOOL display_modifiers;
63 DWORD create_flags;
65 INT scheme_start;
66 DWORD scheme_len;
67 URL_SCHEME scheme_type;
69 INT userinfo_start;
70 DWORD userinfo_len;
71 INT userinfo_split;
73 INT host_start;
74 DWORD host_len;
75 Uri_HOST_TYPE host_type;
77 INT port_offset;
78 DWORD port;
79 BOOL has_port;
81 INT authority_start;
82 DWORD authority_len;
84 INT domain_offset;
86 INT path_start;
87 DWORD path_len;
88 INT extension_offset;
90 INT query_start;
91 DWORD query_len;
93 INT fragment_start;
94 DWORD fragment_len;
95 } Uri;
97 typedef struct {
98 IUriBuilder IUriBuilder_iface;
99 LONG ref;
101 Uri *uri;
102 DWORD modified_props;
104 WCHAR *fragment;
105 DWORD fragment_len;
107 WCHAR *host;
108 DWORD host_len;
110 WCHAR *password;
111 DWORD password_len;
113 WCHAR *path;
114 DWORD path_len;
116 BOOL has_port;
117 DWORD port;
119 WCHAR *query;
120 DWORD query_len;
122 WCHAR *scheme;
123 DWORD scheme_len;
125 WCHAR *username;
126 DWORD username_len;
127 } UriBuilder;
129 typedef struct {
130 const WCHAR *str;
131 DWORD len;
132 } h16;
134 typedef struct {
135 /* IPv6 addresses can hold up to 8 h16 components. */
136 h16 components[8];
137 DWORD h16_count;
139 /* An IPv6 can have 1 elision ("::"). */
140 const WCHAR *elision;
142 /* An IPv6 can contain 1 IPv4 address as the last 32bits of the address. */
143 const WCHAR *ipv4;
144 DWORD ipv4_len;
146 INT components_size;
147 INT elision_size;
148 } ipv6_address;
150 typedef struct {
151 BSTR uri;
153 BOOL is_relative;
154 BOOL is_opaque;
155 BOOL has_implicit_scheme;
156 BOOL has_implicit_ip;
157 UINT implicit_ipv4;
158 BOOL must_have_path;
160 const WCHAR *scheme;
161 DWORD scheme_len;
162 URL_SCHEME scheme_type;
164 const WCHAR *username;
165 DWORD username_len;
167 const WCHAR *password;
168 DWORD password_len;
170 const WCHAR *host;
171 DWORD host_len;
172 Uri_HOST_TYPE host_type;
174 BOOL has_ipv6;
175 ipv6_address ipv6_address;
177 BOOL has_port;
178 const WCHAR *port;
179 DWORD port_len;
180 DWORD port_value;
182 const WCHAR *path;
183 DWORD path_len;
185 const WCHAR *query;
186 DWORD query_len;
188 const WCHAR *fragment;
189 DWORD fragment_len;
190 } parse_data;
192 static const CHAR hexDigits[] = "0123456789ABCDEF";
194 /* List of scheme types/scheme names that are recognized by the IUri interface as of IE 7. */
195 static const struct {
196 URL_SCHEME scheme;
197 WCHAR scheme_name[16];
198 } recognized_schemes[] = {
199 {URL_SCHEME_FTP, {'f','t','p',0}},
200 {URL_SCHEME_HTTP, {'h','t','t','p',0}},
201 {URL_SCHEME_GOPHER, {'g','o','p','h','e','r',0}},
202 {URL_SCHEME_MAILTO, {'m','a','i','l','t','o',0}},
203 {URL_SCHEME_NEWS, {'n','e','w','s',0}},
204 {URL_SCHEME_NNTP, {'n','n','t','p',0}},
205 {URL_SCHEME_TELNET, {'t','e','l','n','e','t',0}},
206 {URL_SCHEME_WAIS, {'w','a','i','s',0}},
207 {URL_SCHEME_FILE, {'f','i','l','e',0}},
208 {URL_SCHEME_MK, {'m','k',0}},
209 {URL_SCHEME_HTTPS, {'h','t','t','p','s',0}},
210 {URL_SCHEME_SHELL, {'s','h','e','l','l',0}},
211 {URL_SCHEME_SNEWS, {'s','n','e','w','s',0}},
212 {URL_SCHEME_LOCAL, {'l','o','c','a','l',0}},
213 {URL_SCHEME_JAVASCRIPT, {'j','a','v','a','s','c','r','i','p','t',0}},
214 {URL_SCHEME_VBSCRIPT, {'v','b','s','c','r','i','p','t',0}},
215 {URL_SCHEME_ABOUT, {'a','b','o','u','t',0}},
216 {URL_SCHEME_RES, {'r','e','s',0}},
217 {URL_SCHEME_MSSHELLROOTED, {'m','s','-','s','h','e','l','l','-','r','o','o','t','e','d',0}},
218 {URL_SCHEME_MSSHELLIDLIST, {'m','s','-','s','h','e','l','l','-','i','d','l','i','s','t',0}},
219 {URL_SCHEME_MSHELP, {'h','c','p',0}},
220 {URL_SCHEME_WILDCARD, {'*',0}}
223 /* List of default ports Windows recognizes. */
224 static const struct {
225 URL_SCHEME scheme;
226 USHORT port;
227 } default_ports[] = {
228 {URL_SCHEME_FTP, 21},
229 {URL_SCHEME_HTTP, 80},
230 {URL_SCHEME_GOPHER, 70},
231 {URL_SCHEME_NNTP, 119},
232 {URL_SCHEME_TELNET, 23},
233 {URL_SCHEME_WAIS, 210},
234 {URL_SCHEME_HTTPS, 443},
237 /* List of 3-character top level domain names Windows seems to recognize.
238 * There might be more, but, these are the only ones I've found so far.
240 static const struct {
241 WCHAR tld_name[4];
242 } recognized_tlds[] = {
243 {{'c','o','m',0}},
244 {{'e','d','u',0}},
245 {{'g','o','v',0}},
246 {{'i','n','t',0}},
247 {{'m','i','l',0}},
248 {{'n','e','t',0}},
249 {{'o','r','g',0}}
252 static Uri *get_uri_obj(IUri *uri)
254 Uri *ret;
255 HRESULT hres;
257 hres = IUri_QueryInterface(uri, &IID_IUriObj, (void**)&ret);
258 return SUCCEEDED(hres) ? ret : NULL;
261 static inline BOOL is_alpha(WCHAR val) {
262 return ((val >= 'a' && val <= 'z') || (val >= 'A' && val <= 'Z'));
265 static inline BOOL is_num(WCHAR val) {
266 return (val >= '0' && val <= '9');
269 static inline BOOL is_drive_path(const WCHAR *str) {
270 return (is_alpha(str[0]) && (str[1] == ':' || str[1] == '|'));
273 static inline BOOL is_unc_path(const WCHAR *str) {
274 return (str[0] == '\\' && str[0] == '\\');
277 static inline BOOL is_forbidden_dos_path_char(WCHAR val) {
278 return (val == '>' || val == '<' || val == '\"');
281 /* A URI is implicitly a file path if it begins with
282 * a drive letter (e.g. X:) or starts with "\\" (UNC path).
284 static inline BOOL is_implicit_file_path(const WCHAR *str) {
285 return (is_unc_path(str) || (is_alpha(str[0]) && str[1] == ':'));
288 /* Checks if the URI is a hierarchical URI. A hierarchical
289 * URI is one that has "//" after the scheme.
291 static BOOL check_hierarchical(const WCHAR **ptr) {
292 const WCHAR *start = *ptr;
294 if(**ptr != '/')
295 return FALSE;
297 ++(*ptr);
298 if(**ptr != '/') {
299 *ptr = start;
300 return FALSE;
303 ++(*ptr);
304 return TRUE;
307 /* unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~" */
308 static inline BOOL is_unreserved(WCHAR val) {
309 return (is_alpha(val) || is_num(val) || val == '-' || val == '.' ||
310 val == '_' || val == '~');
313 /* sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
314 * / "*" / "+" / "," / ";" / "="
316 static inline BOOL is_subdelim(WCHAR val) {
317 return (val == '!' || val == '$' || val == '&' ||
318 val == '\'' || val == '(' || val == ')' ||
319 val == '*' || val == '+' || val == ',' ||
320 val == ';' || val == '=');
323 /* gen-delims = ":" / "/" / "?" / "#" / "[" / "]" / "@" */
324 static inline BOOL is_gendelim(WCHAR val) {
325 return (val == ':' || val == '/' || val == '?' ||
326 val == '#' || val == '[' || val == ']' ||
327 val == '@');
330 /* Characters that delimit the end of the authority
331 * section of a URI. Sometimes a '\\' is considered
332 * an authority delimiter.
334 static inline BOOL is_auth_delim(WCHAR val, BOOL acceptSlash) {
335 return (val == '#' || val == '/' || val == '?' ||
336 val == '\0' || (acceptSlash && val == '\\'));
339 /* reserved = gen-delims / sub-delims */
340 static inline BOOL is_reserved(WCHAR val) {
341 return (is_subdelim(val) || is_gendelim(val));
344 static inline BOOL is_hexdigit(WCHAR val) {
345 return ((val >= 'a' && val <= 'f') ||
346 (val >= 'A' && val <= 'F') ||
347 (val >= '0' && val <= '9'));
350 static inline BOOL is_path_delim(WCHAR val) {
351 return (!val || val == '#' || val == '?');
354 static inline BOOL is_slash(WCHAR c)
356 return c == '/' || c == '\\';
359 static BOOL is_default_port(URL_SCHEME scheme, DWORD port) {
360 DWORD i;
362 for(i = 0; i < sizeof(default_ports)/sizeof(default_ports[0]); ++i) {
363 if(default_ports[i].scheme == scheme && default_ports[i].port)
364 return TRUE;
367 return FALSE;
370 /* List of schemes types Windows seems to expect to be hierarchical. */
371 static inline BOOL is_hierarchical_scheme(URL_SCHEME type) {
372 return(type == URL_SCHEME_HTTP || type == URL_SCHEME_FTP ||
373 type == URL_SCHEME_GOPHER || type == URL_SCHEME_NNTP ||
374 type == URL_SCHEME_TELNET || type == URL_SCHEME_WAIS ||
375 type == URL_SCHEME_FILE || type == URL_SCHEME_HTTPS ||
376 type == URL_SCHEME_RES);
379 /* Checks if 'flags' contains an invalid combination of Uri_CREATE flags. */
380 static inline BOOL has_invalid_flag_combination(DWORD flags) {
381 return((flags & Uri_CREATE_DECODE_EXTRA_INFO && flags & Uri_CREATE_NO_DECODE_EXTRA_INFO) ||
382 (flags & Uri_CREATE_CANONICALIZE && flags & Uri_CREATE_NO_CANONICALIZE) ||
383 (flags & Uri_CREATE_CRACK_UNKNOWN_SCHEMES && flags & Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES) ||
384 (flags & Uri_CREATE_PRE_PROCESS_HTML_URI && flags & Uri_CREATE_NO_PRE_PROCESS_HTML_URI) ||
385 (flags & Uri_CREATE_IE_SETTINGS && flags & Uri_CREATE_NO_IE_SETTINGS));
388 /* Applies each default Uri_CREATE flags to 'flags' if it
389 * doesn't cause a flag conflict.
391 static void apply_default_flags(DWORD *flags) {
392 if(!(*flags & Uri_CREATE_NO_CANONICALIZE))
393 *flags |= Uri_CREATE_CANONICALIZE;
394 if(!(*flags & Uri_CREATE_NO_DECODE_EXTRA_INFO))
395 *flags |= Uri_CREATE_DECODE_EXTRA_INFO;
396 if(!(*flags & Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES))
397 *flags |= Uri_CREATE_CRACK_UNKNOWN_SCHEMES;
398 if(!(*flags & Uri_CREATE_NO_PRE_PROCESS_HTML_URI))
399 *flags |= Uri_CREATE_PRE_PROCESS_HTML_URI;
400 if(!(*flags & Uri_CREATE_IE_SETTINGS))
401 *flags |= Uri_CREATE_NO_IE_SETTINGS;
404 /* Determines if the URI is hierarchical using the information already parsed into
405 * data and using the current location of parsing in the URI string.
407 * Windows considers a URI hierarchical if one of the following is true:
408 * A.) It's a wildcard scheme.
409 * B.) It's an implicit file scheme.
410 * C.) It's a known hierarchical scheme and it has two '\\' after the scheme name.
411 * (the '\\' will be converted into "//" during canonicalization).
412 * D.) It's not a relative URI and "//" appears after the scheme name.
414 static inline BOOL is_hierarchical_uri(const WCHAR **ptr, const parse_data *data) {
415 const WCHAR *start = *ptr;
417 if(data->scheme_type == URL_SCHEME_WILDCARD)
418 return TRUE;
419 else if(data->scheme_type == URL_SCHEME_FILE && data->has_implicit_scheme)
420 return TRUE;
421 else if(is_hierarchical_scheme(data->scheme_type) && (*ptr)[0] == '\\' && (*ptr)[1] == '\\') {
422 *ptr += 2;
423 return TRUE;
424 } else if(!data->is_relative && check_hierarchical(ptr))
425 return TRUE;
427 *ptr = start;
428 return FALSE;
431 /* Computes the size of the given IPv6 address.
432 * Each h16 component is 16 bits. If there is an IPv4 address, it's
433 * 32 bits. If there's an elision it can be 16 to 128 bits, depending
434 * on the number of other components.
436 * Modeled after google-url's CheckIPv6ComponentsSize function
438 static void compute_ipv6_comps_size(ipv6_address *address) {
439 address->components_size = address->h16_count * 2;
441 if(address->ipv4)
442 /* IPv4 address is 4 bytes. */
443 address->components_size += 4;
445 if(address->elision) {
446 /* An elision can be anywhere from 2 bytes up to 16 bytes.
447 * Its size depends on the size of the h16 and IPv4 components.
449 address->elision_size = 16 - address->components_size;
450 if(address->elision_size < 2)
451 address->elision_size = 2;
452 } else
453 address->elision_size = 0;
456 /* Taken from dlls/jscript/lex.c */
457 static int hex_to_int(WCHAR val) {
458 if(val >= '0' && val <= '9')
459 return val - '0';
460 else if(val >= 'a' && val <= 'f')
461 return val - 'a' + 10;
462 else if(val >= 'A' && val <= 'F')
463 return val - 'A' + 10;
465 return -1;
468 /* Helper function for converting a percent encoded string
469 * representation of a WCHAR value into its actual WCHAR value. If
470 * the two characters following the '%' aren't valid hex values then
471 * this function returns the NULL character.
473 * E.g.
474 * "%2E" will result in '.' being returned by this function.
476 static WCHAR decode_pct_val(const WCHAR *ptr) {
477 WCHAR ret = '\0';
479 if(*ptr == '%' && is_hexdigit(*(ptr + 1)) && is_hexdigit(*(ptr + 2))) {
480 INT a = hex_to_int(*(ptr + 1));
481 INT b = hex_to_int(*(ptr + 2));
483 ret = a << 4;
484 ret += b;
487 return ret;
490 /* Helper function for percent encoding a given character
491 * and storing the encoded value into a given buffer (dest).
493 * It's up to the calling function to ensure that there is
494 * at least enough space in 'dest' for the percent encoded
495 * value to be stored (so dest + 3 spaces available).
497 static inline void pct_encode_val(WCHAR val, WCHAR *dest) {
498 dest[0] = '%';
499 dest[1] = hexDigits[(val >> 4) & 0xf];
500 dest[2] = hexDigits[val & 0xf];
503 /* Attempts to parse the domain name from the host.
505 * This function also includes the Top-level Domain (TLD) name
506 * of the host when it tries to find the domain name. If it finds
507 * a valid domain name it will assign 'domain_start' the offset
508 * into 'host' where the domain name starts.
510 * It's implied that if there is a domain name its range is:
511 * [host+domain_start, host+host_len).
513 void find_domain_name(const WCHAR *host, DWORD host_len,
514 INT *domain_start) {
515 const WCHAR *last_tld, *sec_last_tld, *end;
517 end = host+host_len-1;
519 *domain_start = -1;
521 /* There has to be at least enough room for a '.' followed by a
522 * 3-character TLD for a domain to even exist in the host name.
524 if(host_len < 4)
525 return;
527 last_tld = memrchrW(host, '.', host_len);
528 if(!last_tld)
529 /* http://hostname -> has no domain name. */
530 return;
532 sec_last_tld = memrchrW(host, '.', last_tld-host);
533 if(!sec_last_tld) {
534 /* If the '.' is at the beginning of the host there
535 * has to be at least 3 characters in the TLD for it
536 * to be valid.
537 * Ex: .com -> .com as the domain name.
538 * .co -> has no domain name.
540 if(last_tld-host == 0) {
541 if(end-(last_tld-1) < 3)
542 return;
543 } else if(last_tld-host == 3) {
544 DWORD i;
546 /* If there are three characters in front of last_tld and
547 * they are on the list of recognized TLDs, then this
548 * host doesn't have a domain (since the host only contains
549 * a TLD name.
550 * Ex: edu.uk -> has no domain name.
551 * foo.uk -> foo.uk as the domain name.
553 for(i = 0; i < sizeof(recognized_tlds)/sizeof(recognized_tlds[0]); ++i) {
554 if(!StrCmpNIW(host, recognized_tlds[i].tld_name, 3))
555 return;
557 } else if(last_tld-host < 3)
558 /* Anything less than 3 characters is considered part
559 * of the TLD name.
560 * Ex: ak.uk -> Has no domain name.
562 return;
564 /* Otherwise the domain name is the whole host name. */
565 *domain_start = 0;
566 } else if(end+1-last_tld > 3) {
567 /* If the last_tld has more than 3 characters, then it's automatically
568 * considered the TLD of the domain name.
569 * Ex: www.winehq.org.uk.test -> uk.test as the domain name.
571 *domain_start = (sec_last_tld+1)-host;
572 } else if(last_tld - (sec_last_tld+1) < 4) {
573 DWORD i;
574 /* If the sec_last_tld is 3 characters long it HAS to be on the list of
575 * recognized to still be considered part of the TLD name, otherwise
576 * its considered the domain name.
577 * Ex: www.google.com.uk -> google.com.uk as the domain name.
578 * www.google.foo.uk -> foo.uk as the domain name.
580 if(last_tld - (sec_last_tld+1) == 3) {
581 for(i = 0; i < sizeof(recognized_tlds)/sizeof(recognized_tlds[0]); ++i) {
582 if(!StrCmpNIW(sec_last_tld+1, recognized_tlds[i].tld_name, 3)) {
583 const WCHAR *domain = memrchrW(host, '.', sec_last_tld-host);
585 if(!domain)
586 *domain_start = 0;
587 else
588 *domain_start = (domain+1) - host;
589 TRACE("Found domain name %s\n", debugstr_wn(host+*domain_start,
590 (host+host_len)-(host+*domain_start)));
591 return;
595 *domain_start = (sec_last_tld+1)-host;
596 } else {
597 /* Since the sec_last_tld is less than 3 characters it's considered
598 * part of the TLD.
599 * Ex: www.google.fo.uk -> google.fo.uk as the domain name.
601 const WCHAR *domain = memrchrW(host, '.', sec_last_tld-host);
603 if(!domain)
604 *domain_start = 0;
605 else
606 *domain_start = (domain+1) - host;
608 } else {
609 /* The second to last TLD has more than 3 characters making it
610 * the domain name.
611 * Ex: www.google.test.us -> test.us as the domain name.
613 *domain_start = (sec_last_tld+1)-host;
616 TRACE("Found domain name %s\n", debugstr_wn(host+*domain_start,
617 (host+host_len)-(host+*domain_start)));
620 /* Removes the dot segments from a hierarchical URIs path component. This
621 * function performs the removal in place.
623 * This function returns the new length of the path string.
625 static DWORD remove_dot_segments(WCHAR *path, DWORD path_len) {
626 WCHAR *out = path;
627 const WCHAR *in = out;
628 const WCHAR *end = out + path_len;
629 DWORD len;
631 while(in < end) {
632 /* Move the first path segment in the input buffer to the end of
633 * the output buffer, and any subsequent characters up to, including
634 * the next "/" character (if any) or the end of the input buffer.
636 while(in < end && !is_slash(*in))
637 *out++ = *in++;
638 if(in == end)
639 break;
640 *out++ = *in++;
642 while(in < end) {
643 if(*in != '.')
644 break;
646 /* Handle ending "/." */
647 if(in + 1 == end) {
648 ++in;
649 break;
652 /* Handle "/./" */
653 if(is_slash(in[1])) {
654 in += 2;
655 continue;
658 /* If we don't have "/../" or ending "/.." */
659 if(in[1] != '.' || (in + 2 != end && !is_slash(in[2])))
660 break;
662 /* Find the slash preceding out pointer and move out pointer to it */
663 if(out > path+1 && is_slash(*--out))
664 --out;
665 while(out > path && !is_slash(*(--out)));
666 if(is_slash(*out))
667 ++out;
668 in += 2;
669 if(in != end)
670 ++in;
674 len = out - path;
675 TRACE("(%p %d): Path after dot segments removed %s len=%d\n", path, path_len,
676 debugstr_wn(path, len), len);
677 return len;
680 /* Attempts to find the file extension in a given path. */
681 static INT find_file_extension(const WCHAR *path, DWORD path_len) {
682 const WCHAR *end;
684 for(end = path+path_len-1; end >= path && *end != '/' && *end != '\\'; --end) {
685 if(*end == '.')
686 return end-path;
689 return -1;
692 /* Computes the location where the elision should occur in the IPv6
693 * address using the numerical values of each component stored in
694 * 'values'. If the address shouldn't contain an elision then 'index'
695 * is assigned -1 as its value. Otherwise 'index' will contain the
696 * starting index (into values) where the elision should be, and 'count'
697 * will contain the number of cells the elision covers.
699 * NOTES:
700 * Windows will expand an elision if the elision only represents one h16
701 * component of the address.
703 * Ex: [1::2:3:4:5:6:7] -> [1:0:2:3:4:5:6:7]
705 * If the IPv6 address contains an IPv4 address, the IPv4 address is also
706 * considered for being included as part of an elision if all its components
707 * are zeros.
709 * Ex: [1:2:3:4:5:6:0.0.0.0] -> [1:2:3:4:5:6::]
711 static void compute_elision_location(const ipv6_address *address, const USHORT values[8],
712 INT *index, DWORD *count) {
713 DWORD i, max_len, cur_len;
714 INT max_index, cur_index;
716 max_len = cur_len = 0;
717 max_index = cur_index = -1;
718 for(i = 0; i < 8; ++i) {
719 BOOL check_ipv4 = (address->ipv4 && i == 6);
720 BOOL is_end = (check_ipv4 || i == 7);
722 if(check_ipv4) {
723 /* Check if the IPv4 address contains only zeros. */
724 if(values[i] == 0 && values[i+1] == 0) {
725 if(cur_index == -1)
726 cur_index = i;
728 cur_len += 2;
729 ++i;
731 } else if(values[i] == 0) {
732 if(cur_index == -1)
733 cur_index = i;
735 ++cur_len;
738 if(is_end || values[i] != 0) {
739 /* We only consider it for an elision if it's
740 * more than 1 component long.
742 if(cur_len > 1 && cur_len > max_len) {
743 /* Found the new elision location. */
744 max_len = cur_len;
745 max_index = cur_index;
748 /* Reset the current range for the next range of zeros. */
749 cur_index = -1;
750 cur_len = 0;
754 *index = max_index;
755 *count = max_len;
758 /* Removes all the leading and trailing white spaces or
759 * control characters from the URI and removes all control
760 * characters inside of the URI string.
762 static BSTR pre_process_uri(LPCWSTR uri) {
763 const WCHAR *start, *end, *ptr;
764 WCHAR *ptr2;
765 DWORD len;
766 BSTR ret;
768 start = uri;
769 /* Skip leading controls and whitespace. */
770 while(*start && (iscntrlW(*start) || isspaceW(*start))) ++start;
772 /* URI consisted only of control/whitespace. */
773 if(!*start)
774 return SysAllocStringLen(NULL, 0);
776 end = start + strlenW(start);
777 while(--end > start && (iscntrlW(*end) || isspaceW(*end)));
779 len = ++end - start;
780 for(ptr = start; ptr < end; ptr++) {
781 if(iscntrlW(*ptr))
782 len--;
785 ret = SysAllocStringLen(NULL, len);
786 if(!ret)
787 return NULL;
789 for(ptr = start, ptr2=ret; ptr < end; ptr++) {
790 if(!iscntrlW(*ptr))
791 *ptr2++ = *ptr;
794 return ret;
797 /* Converts the specified IPv4 address into an uint value.
799 * This function assumes that the IPv4 address has already been validated.
801 static UINT ipv4toui(const WCHAR *ip, DWORD len) {
802 UINT ret = 0;
803 DWORD comp_value = 0;
804 const WCHAR *ptr;
806 for(ptr = ip; ptr < ip+len; ++ptr) {
807 if(*ptr == '.') {
808 ret <<= 8;
809 ret += comp_value;
810 comp_value = 0;
811 } else
812 comp_value = comp_value*10 + (*ptr-'0');
815 ret <<= 8;
816 ret += comp_value;
818 return ret;
821 /* Converts an IPv4 address in numerical form into its fully qualified
822 * string form. This function returns the number of characters written
823 * to 'dest'. If 'dest' is NULL this function will return the number of
824 * characters that would have been written.
826 * It's up to the caller to ensure there's enough space in 'dest' for the
827 * address.
829 static DWORD ui2ipv4(WCHAR *dest, UINT address) {
830 static const WCHAR formatW[] =
831 {'%','u','.','%','u','.','%','u','.','%','u',0};
832 DWORD ret = 0;
833 UCHAR digits[4];
835 digits[0] = (address >> 24) & 0xff;
836 digits[1] = (address >> 16) & 0xff;
837 digits[2] = (address >> 8) & 0xff;
838 digits[3] = address & 0xff;
840 if(!dest) {
841 WCHAR tmp[16];
842 ret = sprintfW(tmp, formatW, digits[0], digits[1], digits[2], digits[3]);
843 } else
844 ret = sprintfW(dest, formatW, digits[0], digits[1], digits[2], digits[3]);
846 return ret;
849 static DWORD ui2str(WCHAR *dest, UINT value) {
850 static const WCHAR formatW[] = {'%','u',0};
851 DWORD ret = 0;
853 if(!dest) {
854 WCHAR tmp[11];
855 ret = sprintfW(tmp, formatW, value);
856 } else
857 ret = sprintfW(dest, formatW, value);
859 return ret;
862 /* Converts a h16 component (from an IPv6 address) into its
863 * numerical value.
865 * This function assumes that the h16 component has already been validated.
867 static USHORT h16tous(h16 component) {
868 DWORD i;
869 USHORT ret = 0;
871 for(i = 0; i < component.len; ++i) {
872 ret <<= 4;
873 ret += hex_to_int(component.str[i]);
876 return ret;
879 /* Converts an IPv6 address into its 128 bits (16 bytes) numerical value.
881 * This function assumes that the ipv6_address has already been validated.
883 static BOOL ipv6_to_number(const ipv6_address *address, USHORT number[8]) {
884 DWORD i, cur_component = 0;
885 BOOL already_passed_elision = FALSE;
887 for(i = 0; i < address->h16_count; ++i) {
888 if(address->elision) {
889 if(address->components[i].str > address->elision && !already_passed_elision) {
890 /* Means we just passed the elision and need to add its values to
891 * 'number' before we do anything else.
893 DWORD j = 0;
894 for(j = 0; j < address->elision_size; j+=2)
895 number[cur_component++] = 0;
897 already_passed_elision = TRUE;
901 number[cur_component++] = h16tous(address->components[i]);
904 /* Case when the elision appears after the h16 components. */
905 if(!already_passed_elision && address->elision) {
906 for(i = 0; i < address->elision_size; i+=2)
907 number[cur_component++] = 0;
910 if(address->ipv4) {
911 UINT value = ipv4toui(address->ipv4, address->ipv4_len);
913 if(cur_component != 6) {
914 ERR("(%p %p): Failed sanity check with %d\n", address, number, cur_component);
915 return FALSE;
918 number[cur_component++] = (value >> 16) & 0xffff;
919 number[cur_component] = value & 0xffff;
922 return TRUE;
925 /* Checks if the characters pointed to by 'ptr' are
926 * a percent encoded data octet.
928 * pct-encoded = "%" HEXDIG HEXDIG
930 static BOOL check_pct_encoded(const WCHAR **ptr) {
931 const WCHAR *start = *ptr;
933 if(**ptr != '%')
934 return FALSE;
936 ++(*ptr);
937 if(!is_hexdigit(**ptr)) {
938 *ptr = start;
939 return FALSE;
942 ++(*ptr);
943 if(!is_hexdigit(**ptr)) {
944 *ptr = start;
945 return FALSE;
948 ++(*ptr);
949 return TRUE;
952 /* dec-octet = DIGIT ; 0-9
953 * / %x31-39 DIGIT ; 10-99
954 * / "1" 2DIGIT ; 100-199
955 * / "2" %x30-34 DIGIT ; 200-249
956 * / "25" %x30-35 ; 250-255
958 static BOOL check_dec_octet(const WCHAR **ptr) {
959 const WCHAR *c1, *c2, *c3;
961 c1 = *ptr;
962 /* A dec-octet must be at least 1 digit long. */
963 if(*c1 < '0' || *c1 > '9')
964 return FALSE;
966 ++(*ptr);
968 c2 = *ptr;
969 /* Since the 1-digit requirement was met, it doesn't
970 * matter if this is a DIGIT value, it's considered a
971 * dec-octet.
973 if(*c2 < '0' || *c2 > '9')
974 return TRUE;
976 ++(*ptr);
978 c3 = *ptr;
979 /* Same explanation as above. */
980 if(*c3 < '0' || *c3 > '9')
981 return TRUE;
983 /* Anything > 255 isn't a valid IP dec-octet. */
984 if(*c1 >= '2' && *c2 >= '5' && *c3 >= '5') {
985 *ptr = c1;
986 return FALSE;
989 ++(*ptr);
990 return TRUE;
993 /* Checks if there is an implicit IPv4 address in the host component of the URI.
994 * The max value of an implicit IPv4 address is UINT_MAX.
996 * Ex:
997 * "234567" would be considered an implicit IPv4 address.
999 static BOOL check_implicit_ipv4(const WCHAR **ptr, UINT *val) {
1000 const WCHAR *start = *ptr;
1001 ULONGLONG ret = 0;
1002 *val = 0;
1004 while(is_num(**ptr)) {
1005 ret = ret*10 + (**ptr - '0');
1007 if(ret > UINT_MAX) {
1008 *ptr = start;
1009 return FALSE;
1011 ++(*ptr);
1014 if(*ptr == start)
1015 return FALSE;
1017 *val = ret;
1018 return TRUE;
1021 /* Checks if the string contains an IPv4 address.
1023 * This function has a strict mode or a non-strict mode of operation
1024 * When 'strict' is set to FALSE this function will return TRUE if
1025 * the string contains at least 'dec-octet "." dec-octet' since partial
1026 * IPv4 addresses will be normalized out into full IPv4 addresses. When
1027 * 'strict' is set this function expects there to be a full IPv4 address.
1029 * IPv4address = dec-octet "." dec-octet "." dec-octet "." dec-octet
1031 static BOOL check_ipv4address(const WCHAR **ptr, BOOL strict) {
1032 const WCHAR *start = *ptr;
1034 if(!check_dec_octet(ptr)) {
1035 *ptr = start;
1036 return FALSE;
1039 if(**ptr != '.') {
1040 *ptr = start;
1041 return FALSE;
1044 ++(*ptr);
1045 if(!check_dec_octet(ptr)) {
1046 *ptr = start;
1047 return FALSE;
1050 if(**ptr != '.') {
1051 if(strict) {
1052 *ptr = start;
1053 return FALSE;
1054 } else
1055 return TRUE;
1058 ++(*ptr);
1059 if(!check_dec_octet(ptr)) {
1060 *ptr = start;
1061 return FALSE;
1064 if(**ptr != '.') {
1065 if(strict) {
1066 *ptr = start;
1067 return FALSE;
1068 } else
1069 return TRUE;
1072 ++(*ptr);
1073 if(!check_dec_octet(ptr)) {
1074 *ptr = start;
1075 return FALSE;
1078 /* Found a four digit ip address. */
1079 return TRUE;
1081 /* Tries to parse the scheme name of the URI.
1083 * scheme = ALPHA *(ALPHA | NUM | '+' | '-' | '.') as defined by RFC 3896.
1084 * NOTE: Windows accepts a number as the first character of a scheme.
1086 static BOOL parse_scheme_name(const WCHAR **ptr, parse_data *data, DWORD extras) {
1087 const WCHAR *start = *ptr;
1089 data->scheme = NULL;
1090 data->scheme_len = 0;
1092 while(**ptr) {
1093 if(**ptr == '*' && *ptr == start) {
1094 /* Might have found a wildcard scheme. If it is the next
1095 * char has to be a ':' for it to be a valid URI
1097 ++(*ptr);
1098 break;
1099 } else if(!is_num(**ptr) && !is_alpha(**ptr) && **ptr != '+' &&
1100 **ptr != '-' && **ptr != '.')
1101 break;
1103 (*ptr)++;
1106 if(*ptr == start)
1107 return FALSE;
1109 /* Schemes must end with a ':' */
1110 if(**ptr != ':' && !((extras & ALLOW_NULL_TERM_SCHEME) && !**ptr)) {
1111 *ptr = start;
1112 return FALSE;
1115 data->scheme = start;
1116 data->scheme_len = *ptr - start;
1118 ++(*ptr);
1119 return TRUE;
1122 /* Tries to deduce the corresponding URL_SCHEME for the given URI. Stores
1123 * the deduced URL_SCHEME in data->scheme_type.
1125 static BOOL parse_scheme_type(parse_data *data) {
1126 /* If there's scheme data then see if it's a recognized scheme. */
1127 if(data->scheme && data->scheme_len) {
1128 DWORD i;
1130 for(i = 0; i < sizeof(recognized_schemes)/sizeof(recognized_schemes[0]); ++i) {
1131 if(lstrlenW(recognized_schemes[i].scheme_name) == data->scheme_len) {
1132 /* Has to be a case insensitive compare. */
1133 if(!StrCmpNIW(recognized_schemes[i].scheme_name, data->scheme, data->scheme_len)) {
1134 data->scheme_type = recognized_schemes[i].scheme;
1135 return TRUE;
1140 /* If we get here it means it's not a recognized scheme. */
1141 data->scheme_type = URL_SCHEME_UNKNOWN;
1142 return TRUE;
1143 } else if(data->is_relative) {
1144 /* Relative URI's have no scheme. */
1145 data->scheme_type = URL_SCHEME_UNKNOWN;
1146 return TRUE;
1147 } else {
1148 /* Should never reach here! what happened... */
1149 FIXME("(%p): Unable to determine scheme type for URI %s\n", data, debugstr_w(data->uri));
1150 return FALSE;
1154 /* Tries to parse (or deduce) the scheme_name of a URI. If it can't
1155 * parse a scheme from the URI it will try to deduce the scheme_name and scheme_type
1156 * using the flags specified in 'flags' (if any). Flags that affect how this function
1157 * operates are the Uri_CREATE_ALLOW_* flags.
1159 * All parsed/deduced information will be stored in 'data' when the function returns.
1161 * Returns TRUE if it was able to successfully parse the information.
1163 static BOOL parse_scheme(const WCHAR **ptr, parse_data *data, DWORD flags, DWORD extras) {
1164 static const WCHAR fileW[] = {'f','i','l','e',0};
1165 static const WCHAR wildcardW[] = {'*',0};
1167 /* First check to see if the uri could implicitly be a file path. */
1168 if(is_implicit_file_path(*ptr)) {
1169 if(flags & Uri_CREATE_ALLOW_IMPLICIT_FILE_SCHEME) {
1170 data->scheme = fileW;
1171 data->scheme_len = lstrlenW(fileW);
1172 data->has_implicit_scheme = TRUE;
1174 TRACE("(%p %p %x): URI is an implicit file path.\n", ptr, data, flags);
1175 } else {
1176 /* Windows does not consider anything that can implicitly be a file
1177 * path to be a valid URI if the ALLOW_IMPLICIT_FILE_SCHEME flag is not set...
1179 TRACE("(%p %p %x): URI is implicitly a file path, but, the ALLOW_IMPLICIT_FILE_SCHEME flag wasn't set.\n",
1180 ptr, data, flags);
1181 return FALSE;
1183 } else if(!parse_scheme_name(ptr, data, extras)) {
1184 /* No scheme was found, this means it could be:
1185 * a) an implicit Wildcard scheme
1186 * b) a relative URI
1187 * c) an invalid URI.
1189 if(flags & Uri_CREATE_ALLOW_IMPLICIT_WILDCARD_SCHEME) {
1190 data->scheme = wildcardW;
1191 data->scheme_len = lstrlenW(wildcardW);
1192 data->has_implicit_scheme = TRUE;
1194 TRACE("(%p %p %x): URI is an implicit wildcard scheme.\n", ptr, data, flags);
1195 } else if (flags & Uri_CREATE_ALLOW_RELATIVE) {
1196 data->is_relative = TRUE;
1197 TRACE("(%p %p %x): URI is relative.\n", ptr, data, flags);
1198 } else {
1199 TRACE("(%p %p %x): Malformed URI found. Unable to deduce scheme name.\n", ptr, data, flags);
1200 return FALSE;
1204 if(!data->is_relative)
1205 TRACE("(%p %p %x): Found scheme=%s scheme_len=%d\n", ptr, data, flags,
1206 debugstr_wn(data->scheme, data->scheme_len), data->scheme_len);
1208 if(!parse_scheme_type(data))
1209 return FALSE;
1211 TRACE("(%p %p %x): Assigned %d as the URL_SCHEME.\n", ptr, data, flags, data->scheme_type);
1212 return TRUE;
1215 static BOOL parse_username(const WCHAR **ptr, parse_data *data, DWORD flags, DWORD extras) {
1216 data->username = *ptr;
1218 while(**ptr != ':' && **ptr != '@') {
1219 if(**ptr == '%') {
1220 if(!check_pct_encoded(ptr)) {
1221 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
1222 *ptr = data->username;
1223 data->username = NULL;
1224 return FALSE;
1226 } else
1227 continue;
1228 } else if(extras & ALLOW_NULL_TERM_USER_NAME && !**ptr)
1229 break;
1230 else if(is_auth_delim(**ptr, data->scheme_type != URL_SCHEME_UNKNOWN)) {
1231 *ptr = data->username;
1232 data->username = NULL;
1233 return FALSE;
1236 ++(*ptr);
1239 data->username_len = *ptr - data->username;
1240 return TRUE;
1243 static BOOL parse_password(const WCHAR **ptr, parse_data *data, DWORD flags, DWORD extras) {
1244 data->password = *ptr;
1246 while(**ptr != '@') {
1247 if(**ptr == '%') {
1248 if(!check_pct_encoded(ptr)) {
1249 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
1250 *ptr = data->password;
1251 data->password = NULL;
1252 return FALSE;
1254 } else
1255 continue;
1256 } else if(extras & ALLOW_NULL_TERM_PASSWORD && !**ptr)
1257 break;
1258 else if(is_auth_delim(**ptr, data->scheme_type != URL_SCHEME_UNKNOWN)) {
1259 *ptr = data->password;
1260 data->password = NULL;
1261 return FALSE;
1264 ++(*ptr);
1267 data->password_len = *ptr - data->password;
1268 return TRUE;
1271 /* Parses the userinfo part of the URI (if it exists). The userinfo field of
1272 * a URI can consist of "username:password@", or just "username@".
1274 * RFC def:
1275 * userinfo = *( unreserved / pct-encoded / sub-delims / ":" )
1277 * NOTES:
1278 * 1) If there is more than one ':' in the userinfo part of the URI Windows
1279 * uses the first occurrence of ':' to delimit the username and password
1280 * components.
1282 * ex:
1283 * ftp://user:pass:word@winehq.org
1285 * would yield "user" as the username and "pass:word" as the password.
1287 * 2) Windows allows any character to appear in the "userinfo" part of
1288 * a URI, as long as it's not an authority delimiter character set.
1290 static void parse_userinfo(const WCHAR **ptr, parse_data *data, DWORD flags) {
1291 const WCHAR *start = *ptr;
1293 if(!parse_username(ptr, data, flags, 0)) {
1294 TRACE("(%p %p %x): URI contained no userinfo.\n", ptr, data, flags);
1295 return;
1298 if(**ptr == ':') {
1299 ++(*ptr);
1300 if(!parse_password(ptr, data, flags, 0)) {
1301 *ptr = start;
1302 data->username = NULL;
1303 data->username_len = 0;
1304 TRACE("(%p %p %x): URI contained no userinfo.\n", ptr, data, flags);
1305 return;
1309 if(**ptr != '@') {
1310 *ptr = start;
1311 data->username = NULL;
1312 data->username_len = 0;
1313 data->password = NULL;
1314 data->password_len = 0;
1316 TRACE("(%p %p %x): URI contained no userinfo.\n", ptr, data, flags);
1317 return;
1320 if(data->username)
1321 TRACE("(%p %p %x): Found username %s len=%d.\n", ptr, data, flags,
1322 debugstr_wn(data->username, data->username_len), data->username_len);
1324 if(data->password)
1325 TRACE("(%p %p %x): Found password %s len=%d.\n", ptr, data, flags,
1326 debugstr_wn(data->password, data->password_len), data->password_len);
1328 ++(*ptr);
1331 /* Attempts to parse a port from the URI.
1333 * NOTES:
1334 * Windows seems to have a cap on what the maximum value
1335 * for a port can be. The max value is USHORT_MAX.
1337 * port = *DIGIT
1339 static BOOL parse_port(const WCHAR **ptr, parse_data *data, DWORD flags) {
1340 UINT port = 0;
1341 data->port = *ptr;
1343 while(!is_auth_delim(**ptr, data->scheme_type != URL_SCHEME_UNKNOWN)) {
1344 if(!is_num(**ptr)) {
1345 *ptr = data->port;
1346 data->port = NULL;
1347 return FALSE;
1350 port = port*10 + (**ptr-'0');
1352 if(port > USHORT_MAX) {
1353 *ptr = data->port;
1354 data->port = NULL;
1355 return FALSE;
1358 ++(*ptr);
1361 data->has_port = TRUE;
1362 data->port_value = port;
1363 data->port_len = *ptr - data->port;
1365 TRACE("(%p %p %x): Found port %s len=%d value=%u\n", ptr, data, flags,
1366 debugstr_wn(data->port, data->port_len), data->port_len, data->port_value);
1367 return TRUE;
1370 /* Attempts to parse a IPv4 address from the URI.
1372 * NOTES:
1373 * Windows normalizes IPv4 addresses, This means there are three
1374 * possibilities for the URI to contain an IPv4 address.
1375 * 1) A well formed address (ex. 192.2.2.2).
1376 * 2) A partially formed address. For example "192.0" would
1377 * normalize to "192.0.0.0" during canonicalization.
1378 * 3) An implicit IPv4 address. For example "256" would
1379 * normalize to "0.0.1.0" during canonicalization. Also
1380 * note that the maximum value for an implicit IP address
1381 * is UINT_MAX, if the value in the URI exceeds this then
1382 * it is not considered an IPv4 address.
1384 static BOOL parse_ipv4address(const WCHAR **ptr, parse_data *data, DWORD flags) {
1385 const BOOL is_unknown = data->scheme_type == URL_SCHEME_UNKNOWN;
1386 data->host = *ptr;
1388 if(!check_ipv4address(ptr, FALSE)) {
1389 if(!check_implicit_ipv4(ptr, &data->implicit_ipv4)) {
1390 TRACE("(%p %p %x): URI didn't contain anything looking like an IPv4 address.\n",
1391 ptr, data, flags);
1392 *ptr = data->host;
1393 data->host = NULL;
1394 return FALSE;
1395 } else
1396 data->has_implicit_ip = TRUE;
1399 data->host_len = *ptr - data->host;
1400 data->host_type = Uri_HOST_IPV4;
1402 /* Check if what we found is the only part of the host name (if it isn't
1403 * we don't have an IPv4 address).
1405 if(**ptr == ':') {
1406 ++(*ptr);
1407 if(!parse_port(ptr, data, flags)) {
1408 *ptr = data->host;
1409 data->host = NULL;
1410 return FALSE;
1412 } else if(!is_auth_delim(**ptr, !is_unknown)) {
1413 /* Found more data which belongs to the host, so this isn't an IPv4. */
1414 *ptr = data->host;
1415 data->host = NULL;
1416 data->has_implicit_ip = FALSE;
1417 return FALSE;
1420 TRACE("(%p %p %x): IPv4 address found. host=%s host_len=%d host_type=%d\n",
1421 ptr, data, flags, debugstr_wn(data->host, data->host_len),
1422 data->host_len, data->host_type);
1423 return TRUE;
1426 /* Attempts to parse the reg-name from the URI.
1428 * Because of the way Windows handles ':' this function also
1429 * handles parsing the port.
1431 * reg-name = *( unreserved / pct-encoded / sub-delims )
1433 * NOTE:
1434 * Windows allows everything, but, the characters in "auth_delims" and ':'
1435 * to appear in a reg-name, unless it's an unknown scheme type then ':' is
1436 * allowed to appear (even if a valid port isn't after it).
1438 * Windows doesn't like host names which start with '[' and end with ']'
1439 * and don't contain a valid IP literal address in between them.
1441 * On Windows if a '[' is encountered in the host name the ':' no longer
1442 * counts as a delimiter until you reach the next ']' or an "authority delimiter".
1444 * A reg-name CAN be empty.
1446 static BOOL parse_reg_name(const WCHAR **ptr, parse_data *data, DWORD flags, DWORD extras) {
1447 const BOOL has_start_bracket = **ptr == '[';
1448 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
1449 const BOOL is_res = data->scheme_type == URL_SCHEME_RES;
1450 BOOL inside_brackets = has_start_bracket;
1452 /* res URIs don't have ports. */
1453 BOOL ignore_col = (extras & IGNORE_PORT_DELIMITER) || is_res;
1455 /* We have to be careful with file schemes. */
1456 if(data->scheme_type == URL_SCHEME_FILE) {
1457 /* This is because an implicit file scheme could be "C:\\test" and it
1458 * would trick this function into thinking the host is "C", when after
1459 * canonicalization the host would end up being an empty string. A drive
1460 * path can also have a '|' instead of a ':' after the drive letter.
1462 if(is_drive_path(*ptr)) {
1463 /* Regular old drive paths have no host type (or host name). */
1464 data->host_type = Uri_HOST_UNKNOWN;
1465 data->host = *ptr;
1466 data->host_len = 0;
1467 return TRUE;
1468 } else if(is_unc_path(*ptr))
1469 /* Skip past the "\\" of a UNC path. */
1470 *ptr += 2;
1473 data->host = *ptr;
1475 /* For res URIs, everything before the first '/' is
1476 * considered the host.
1478 while((!is_res && !is_auth_delim(**ptr, known_scheme)) ||
1479 (is_res && **ptr && **ptr != '/')) {
1480 if(**ptr == ':' && !ignore_col) {
1481 /* We can ignore ':' if were inside brackets.*/
1482 if(!inside_brackets) {
1483 const WCHAR *tmp = (*ptr)++;
1485 /* Attempt to parse the port. */
1486 if(!parse_port(ptr, data, flags)) {
1487 /* Windows expects there to be a valid port for known scheme types. */
1488 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
1489 *ptr = data->host;
1490 data->host = NULL;
1491 TRACE("(%p %p %x %x): Expected valid port\n", ptr, data, flags, extras);
1492 return FALSE;
1493 } else
1494 /* Windows gives up on trying to parse a port when it
1495 * encounters an invalid port.
1497 ignore_col = TRUE;
1498 } else {
1499 data->host_len = tmp - data->host;
1500 break;
1503 } else if(**ptr == '%' && (known_scheme && !is_res)) {
1504 /* Has to be a legit % encoded value. */
1505 if(!check_pct_encoded(ptr)) {
1506 *ptr = data->host;
1507 data->host = NULL;
1508 return FALSE;
1509 } else
1510 continue;
1511 } else if(is_res && is_forbidden_dos_path_char(**ptr)) {
1512 *ptr = data->host;
1513 data->host = NULL;
1514 return FALSE;
1515 } else if(**ptr == ']')
1516 inside_brackets = FALSE;
1517 else if(**ptr == '[')
1518 inside_brackets = TRUE;
1520 ++(*ptr);
1523 if(has_start_bracket) {
1524 /* Make sure the last character of the host wasn't a ']'. */
1525 if(*(*ptr-1) == ']') {
1526 TRACE("(%p %p %x %x): Expected an IP literal inside of the host\n",
1527 ptr, data, flags, extras);
1528 *ptr = data->host;
1529 data->host = NULL;
1530 return FALSE;
1534 /* Don't overwrite our length if we found a port earlier. */
1535 if(!data->port)
1536 data->host_len = *ptr - data->host;
1538 /* If the host is empty, then it's an unknown host type. */
1539 if(data->host_len == 0 || is_res)
1540 data->host_type = Uri_HOST_UNKNOWN;
1541 else
1542 data->host_type = Uri_HOST_DNS;
1544 TRACE("(%p %p %x %x): Parsed reg-name. host=%s len=%d\n", ptr, data, flags, extras,
1545 debugstr_wn(data->host, data->host_len), data->host_len);
1546 return TRUE;
1549 /* Attempts to parse an IPv6 address out of the URI.
1551 * IPv6address = 6( h16 ":" ) ls32
1552 * / "::" 5( h16 ":" ) ls32
1553 * / [ h16 ] "::" 4( h16 ":" ) ls32
1554 * / [ *1( h16 ":" ) h16 ] "::" 3( h16 ":" ) ls32
1555 * / [ *2( h16 ":" ) h16 ] "::" 2( h16 ":" ) ls32
1556 * / [ *3( h16 ":" ) h16 ] "::" h16 ":" ls32
1557 * / [ *4( h16 ":" ) h16 ] "::" ls32
1558 * / [ *5( h16 ":" ) h16 ] "::" h16
1559 * / [ *6( h16 ":" ) h16 ] "::"
1561 * ls32 = ( h16 ":" h16 ) / IPv4address
1562 * ; least-significant 32 bits of address.
1564 * h16 = 1*4HEXDIG
1565 * ; 16 bits of address represented in hexadecimal.
1567 * Modeled after google-url's 'DoParseIPv6' function.
1569 static BOOL parse_ipv6address(const WCHAR **ptr, parse_data *data, DWORD flags) {
1570 const WCHAR *start, *cur_start;
1571 ipv6_address ip;
1573 start = cur_start = *ptr;
1574 memset(&ip, 0, sizeof(ipv6_address));
1576 for(;; ++(*ptr)) {
1577 /* Check if we're on the last character of the host. */
1578 BOOL is_end = (is_auth_delim(**ptr, data->scheme_type != URL_SCHEME_UNKNOWN)
1579 || **ptr == ']');
1581 BOOL is_split = (**ptr == ':');
1582 BOOL is_elision = (is_split && !is_end && *(*ptr+1) == ':');
1584 /* Check if we're at the end of a component, or
1585 * if we're at the end of the IPv6 address.
1587 if(is_split || is_end) {
1588 DWORD cur_len = 0;
1590 cur_len = *ptr - cur_start;
1592 /* h16 can't have a length > 4. */
1593 if(cur_len > 4) {
1594 *ptr = start;
1596 TRACE("(%p %p %x): h16 component to long.\n",
1597 ptr, data, flags);
1598 return FALSE;
1601 if(cur_len == 0) {
1602 /* An h16 component can't have the length of 0 unless
1603 * the elision is at the beginning of the address, or
1604 * at the end of the address.
1606 if(!((*ptr == start && is_elision) ||
1607 (is_end && (*ptr-2) == ip.elision))) {
1608 *ptr = start;
1609 TRACE("(%p %p %x): IPv6 component cannot have a length of 0.\n",
1610 ptr, data, flags);
1611 return FALSE;
1615 if(cur_len > 0) {
1616 /* An IPv6 address can have no more than 8 h16 components. */
1617 if(ip.h16_count >= 8) {
1618 *ptr = start;
1619 TRACE("(%p %p %x): Not a IPv6 address, to many h16 components.\n",
1620 ptr, data, flags);
1621 return FALSE;
1624 ip.components[ip.h16_count].str = cur_start;
1625 ip.components[ip.h16_count].len = cur_len;
1627 TRACE("(%p %p %x): Found h16 component %s, len=%d, h16_count=%d\n",
1628 ptr, data, flags, debugstr_wn(cur_start, cur_len), cur_len,
1629 ip.h16_count);
1630 ++ip.h16_count;
1634 if(is_end)
1635 break;
1637 if(is_elision) {
1638 /* A IPv6 address can only have 1 elision ('::'). */
1639 if(ip.elision) {
1640 *ptr = start;
1642 TRACE("(%p %p %x): IPv6 address cannot have 2 elisions.\n",
1643 ptr, data, flags);
1644 return FALSE;
1647 ip.elision = *ptr;
1648 ++(*ptr);
1651 if(is_split)
1652 cur_start = *ptr+1;
1653 else {
1654 if(!check_ipv4address(ptr, TRUE)) {
1655 if(!is_hexdigit(**ptr)) {
1656 /* Not a valid character for an IPv6 address. */
1657 *ptr = start;
1658 return FALSE;
1660 } else {
1661 /* Found an IPv4 address. */
1662 ip.ipv4 = cur_start;
1663 ip.ipv4_len = *ptr - cur_start;
1665 TRACE("(%p %p %x): Found an attached IPv4 address %s len=%d.\n",
1666 ptr, data, flags, debugstr_wn(ip.ipv4, ip.ipv4_len),
1667 ip.ipv4_len);
1669 /* IPv4 addresses can only appear at the end of a IPv6. */
1670 break;
1675 compute_ipv6_comps_size(&ip);
1677 /* Make sure the IPv6 address adds up to 16 bytes. */
1678 if(ip.components_size + ip.elision_size != 16) {
1679 *ptr = start;
1680 TRACE("(%p %p %x): Invalid IPv6 address, did not add up to 16 bytes.\n",
1681 ptr, data, flags);
1682 return FALSE;
1685 if(ip.elision_size == 2) {
1686 /* For some reason on Windows if an elision that represents
1687 * only one h16 component is encountered at the very begin or
1688 * end of an IPv6 address, Windows does not consider it a
1689 * valid IPv6 address.
1691 * Ex: [::2:3:4:5:6:7] is not valid, even though the sum
1692 * of all the components == 128bits.
1694 if(ip.elision < ip.components[0].str ||
1695 ip.elision > ip.components[ip.h16_count-1].str) {
1696 *ptr = start;
1697 TRACE("(%p %p %x): Invalid IPv6 address. Detected elision of 2 bytes at the beginning or end of the address.\n",
1698 ptr, data, flags);
1699 return FALSE;
1703 data->host_type = Uri_HOST_IPV6;
1704 data->has_ipv6 = TRUE;
1705 data->ipv6_address = ip;
1707 TRACE("(%p %p %x): Found valid IPv6 literal %s len=%d\n",
1708 ptr, data, flags, debugstr_wn(start, *ptr-start),
1709 (int)(*ptr-start));
1710 return TRUE;
1713 /* IPvFuture = "v" 1*HEXDIG "." 1*( unreserved / sub-delims / ":" ) */
1714 static BOOL parse_ipvfuture(const WCHAR **ptr, parse_data *data, DWORD flags) {
1715 const WCHAR *start = *ptr;
1717 /* IPvFuture has to start with a 'v' or 'V'. */
1718 if(**ptr != 'v' && **ptr != 'V')
1719 return FALSE;
1721 /* Following the v there must be at least 1 hex digit. */
1722 ++(*ptr);
1723 if(!is_hexdigit(**ptr)) {
1724 *ptr = start;
1725 return FALSE;
1728 ++(*ptr);
1729 while(is_hexdigit(**ptr))
1730 ++(*ptr);
1732 /* End of the hexdigit sequence must be a '.' */
1733 if(**ptr != '.') {
1734 *ptr = start;
1735 return FALSE;
1738 ++(*ptr);
1739 if(!is_unreserved(**ptr) && !is_subdelim(**ptr) && **ptr != ':') {
1740 *ptr = start;
1741 return FALSE;
1744 ++(*ptr);
1745 while(is_unreserved(**ptr) || is_subdelim(**ptr) || **ptr == ':')
1746 ++(*ptr);
1748 data->host_type = Uri_HOST_UNKNOWN;
1750 TRACE("(%p %p %x): Parsed IPvFuture address %s len=%d\n", ptr, data, flags,
1751 debugstr_wn(start, *ptr-start), (int)(*ptr-start));
1753 return TRUE;
1756 /* IP-literal = "[" ( IPv6address / IPvFuture ) "]" */
1757 static BOOL parse_ip_literal(const WCHAR **ptr, parse_data *data, DWORD flags, DWORD extras) {
1758 data->host = *ptr;
1760 if(**ptr != '[' && !(extras & ALLOW_BRACKETLESS_IP_LITERAL)) {
1761 data->host = NULL;
1762 return FALSE;
1763 } else if(**ptr == '[')
1764 ++(*ptr);
1766 if(!parse_ipv6address(ptr, data, flags)) {
1767 if(extras & SKIP_IP_FUTURE_CHECK || !parse_ipvfuture(ptr, data, flags)) {
1768 *ptr = data->host;
1769 data->host = NULL;
1770 return FALSE;
1774 if(**ptr != ']' && !(extras & ALLOW_BRACKETLESS_IP_LITERAL)) {
1775 *ptr = data->host;
1776 data->host = NULL;
1777 return FALSE;
1778 } else if(!**ptr && extras & ALLOW_BRACKETLESS_IP_LITERAL) {
1779 /* The IP literal didn't contain brackets and was followed by
1780 * a NULL terminator, so no reason to even check the port.
1782 data->host_len = *ptr - data->host;
1783 return TRUE;
1786 ++(*ptr);
1787 if(**ptr == ':') {
1788 ++(*ptr);
1789 /* If a valid port is not found, then let it trickle down to
1790 * parse_reg_name.
1792 if(!parse_port(ptr, data, flags)) {
1793 *ptr = data->host;
1794 data->host = NULL;
1795 return FALSE;
1797 } else
1798 data->host_len = *ptr - data->host;
1800 return TRUE;
1803 /* Parses the host information from the URI.
1805 * host = IP-literal / IPv4address / reg-name
1807 static BOOL parse_host(const WCHAR **ptr, parse_data *data, DWORD flags, DWORD extras) {
1808 if(!parse_ip_literal(ptr, data, flags, extras)) {
1809 if(!parse_ipv4address(ptr, data, flags)) {
1810 if(!parse_reg_name(ptr, data, flags, extras)) {
1811 TRACE("(%p %p %x %x): Malformed URI, Unknown host type.\n",
1812 ptr, data, flags, extras);
1813 return FALSE;
1818 return TRUE;
1821 /* Parses the authority information from the URI.
1823 * authority = [ userinfo "@" ] host [ ":" port ]
1825 static BOOL parse_authority(const WCHAR **ptr, parse_data *data, DWORD flags) {
1826 parse_userinfo(ptr, data, flags);
1828 /* Parsing the port will happen during one of the host parsing
1829 * routines (if the URI has a port).
1831 if(!parse_host(ptr, data, flags, 0))
1832 return FALSE;
1834 return TRUE;
1837 /* Attempts to parse the path information of a hierarchical URI. */
1838 static BOOL parse_path_hierarchical(const WCHAR **ptr, parse_data *data, DWORD flags) {
1839 const WCHAR *start = *ptr;
1840 static const WCHAR slash[] = {'/',0};
1841 const BOOL is_file = data->scheme_type == URL_SCHEME_FILE;
1843 if(is_path_delim(**ptr)) {
1844 if(data->scheme_type == URL_SCHEME_WILDCARD && !data->must_have_path) {
1845 data->path = NULL;
1846 data->path_len = 0;
1847 } else if(!(flags & Uri_CREATE_NO_CANONICALIZE)) {
1848 /* If the path component is empty, then a '/' is added. */
1849 data->path = slash;
1850 data->path_len = 1;
1852 } else {
1853 while(!is_path_delim(**ptr)) {
1854 if(**ptr == '%' && data->scheme_type != URL_SCHEME_UNKNOWN && !is_file) {
1855 if(!check_pct_encoded(ptr)) {
1856 *ptr = start;
1857 return FALSE;
1858 } else
1859 continue;
1860 } else if(is_forbidden_dos_path_char(**ptr) && is_file &&
1861 (flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
1862 /* File schemes with USE_DOS_PATH set aren't allowed to have
1863 * a '<' or '>' or '\"' appear in them.
1865 *ptr = start;
1866 return FALSE;
1867 } else if(**ptr == '\\') {
1868 /* Not allowed to have a backslash if NO_CANONICALIZE is set
1869 * and the scheme is known type (but not a file scheme).
1871 if(flags & Uri_CREATE_NO_CANONICALIZE) {
1872 if(data->scheme_type != URL_SCHEME_FILE &&
1873 data->scheme_type != URL_SCHEME_UNKNOWN) {
1874 *ptr = start;
1875 return FALSE;
1880 ++(*ptr);
1883 /* The only time a URI doesn't have a path is when
1884 * the NO_CANONICALIZE flag is set and the raw URI
1885 * didn't contain one.
1887 if(*ptr == start) {
1888 data->path = NULL;
1889 data->path_len = 0;
1890 } else {
1891 data->path = start;
1892 data->path_len = *ptr - start;
1896 if(data->path)
1897 TRACE("(%p %p %x): Parsed path %s len=%d\n", ptr, data, flags,
1898 debugstr_wn(data->path, data->path_len), data->path_len);
1899 else
1900 TRACE("(%p %p %x): The URI contained no path\n", ptr, data, flags);
1902 return TRUE;
1905 /* Parses the path of an opaque URI (much less strict then the parser
1906 * for a hierarchical URI).
1908 * NOTE:
1909 * Windows allows invalid % encoded data to appear in opaque URI paths
1910 * for unknown scheme types.
1912 * File schemes with USE_DOS_PATH set aren't allowed to have '<', '>', or '\"'
1913 * appear in them.
1915 static BOOL parse_path_opaque(const WCHAR **ptr, parse_data *data, DWORD flags) {
1916 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
1917 const BOOL is_file = data->scheme_type == URL_SCHEME_FILE;
1919 data->path = *ptr;
1921 while(!is_path_delim(**ptr)) {
1922 if(**ptr == '%' && known_scheme) {
1923 if(!check_pct_encoded(ptr)) {
1924 *ptr = data->path;
1925 data->path = NULL;
1926 return FALSE;
1927 } else
1928 continue;
1929 } else if(is_forbidden_dos_path_char(**ptr) && is_file &&
1930 (flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
1931 *ptr = data->path;
1932 data->path = NULL;
1933 return FALSE;
1936 ++(*ptr);
1939 data->path_len = *ptr - data->path;
1940 TRACE("(%p %p %x): Parsed opaque URI path %s len=%d\n", ptr, data, flags,
1941 debugstr_wn(data->path, data->path_len), data->path_len);
1942 return TRUE;
1945 /* Determines how the URI should be parsed after the scheme information.
1947 * If the scheme is followed by "//", then it is treated as a hierarchical URI
1948 * which then the authority and path information will be parsed out. Otherwise, the
1949 * URI will be treated as an opaque URI which the authority information is not parsed
1950 * out.
1952 * RFC 3896 definition of hier-part:
1954 * hier-part = "//" authority path-abempty
1955 * / path-absolute
1956 * / path-rootless
1957 * / path-empty
1959 * MSDN opaque URI definition:
1960 * scheme ":" path [ "#" fragment ]
1962 * NOTES:
1963 * If the URI is of an unknown scheme type and has a "//" following the scheme then it
1964 * is treated as a hierarchical URI, but, if the CREATE_NO_CRACK_UNKNOWN_SCHEMES flag is
1965 * set then it is considered an opaque URI regardless of what follows the scheme information
1966 * (per MSDN documentation).
1968 static BOOL parse_hierpart(const WCHAR **ptr, parse_data *data, DWORD flags) {
1969 const WCHAR *start = *ptr;
1971 data->must_have_path = FALSE;
1973 /* For javascript: URIs, simply set everything as a path */
1974 if(data->scheme_type == URL_SCHEME_JAVASCRIPT) {
1975 data->path = *ptr;
1976 data->path_len = strlenW(*ptr);
1977 data->is_opaque = TRUE;
1978 *ptr += data->path_len;
1979 return TRUE;
1982 /* Checks if the authority information needs to be parsed. */
1983 if(is_hierarchical_uri(ptr, data)) {
1984 /* Only treat it as a hierarchical URI if the scheme_type is known or
1985 * the Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES flag is not set.
1987 if(data->scheme_type != URL_SCHEME_UNKNOWN ||
1988 !(flags & Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES)) {
1989 TRACE("(%p %p %x): Treating URI as an hierarchical URI.\n", ptr, data, flags);
1990 data->is_opaque = FALSE;
1992 if(data->scheme_type == URL_SCHEME_WILDCARD && !data->has_implicit_scheme) {
1993 if(**ptr == '/' && *(*ptr+1) == '/') {
1994 data->must_have_path = TRUE;
1995 *ptr += 2;
1999 /* TODO: Handle hierarchical URI's, parse authority then parse the path. */
2000 if(!parse_authority(ptr, data, flags))
2001 return FALSE;
2003 return parse_path_hierarchical(ptr, data, flags);
2004 } else
2005 /* Reset ptr to its starting position so opaque path parsing
2006 * begins at the correct location.
2008 *ptr = start;
2011 /* If it reaches here, then the URI will be treated as an opaque
2012 * URI.
2015 TRACE("(%p %p %x): Treating URI as an opaque URI.\n", ptr, data, flags);
2017 data->is_opaque = TRUE;
2018 if(!parse_path_opaque(ptr, data, flags))
2019 return FALSE;
2021 return TRUE;
2024 /* Attempts to parse the query string from the URI.
2026 * NOTES:
2027 * If NO_DECODE_EXTRA_INFO flag is set, then invalid percent encoded
2028 * data is allowed to appear in the query string. For unknown scheme types
2029 * invalid percent encoded data is allowed to appear regardless.
2031 static BOOL parse_query(const WCHAR **ptr, parse_data *data, DWORD flags) {
2032 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
2034 if(**ptr != '?') {
2035 TRACE("(%p %p %x): URI didn't contain a query string.\n", ptr, data, flags);
2036 return TRUE;
2039 data->query = *ptr;
2041 ++(*ptr);
2042 while(**ptr && **ptr != '#') {
2043 if(**ptr == '%' && known_scheme &&
2044 !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
2045 if(!check_pct_encoded(ptr)) {
2046 *ptr = data->query;
2047 data->query = NULL;
2048 return FALSE;
2049 } else
2050 continue;
2053 ++(*ptr);
2056 data->query_len = *ptr - data->query;
2058 TRACE("(%p %p %x): Parsed query string %s len=%d\n", ptr, data, flags,
2059 debugstr_wn(data->query, data->query_len), data->query_len);
2060 return TRUE;
2063 /* Attempts to parse the fragment from the URI.
2065 * NOTES:
2066 * If NO_DECODE_EXTRA_INFO flag is set, then invalid percent encoded
2067 * data is allowed to appear in the query string. For unknown scheme types
2068 * invalid percent encoded data is allowed to appear regardless.
2070 static BOOL parse_fragment(const WCHAR **ptr, parse_data *data, DWORD flags) {
2071 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
2073 if(**ptr != '#') {
2074 TRACE("(%p %p %x): URI didn't contain a fragment.\n", ptr, data, flags);
2075 return TRUE;
2078 data->fragment = *ptr;
2080 ++(*ptr);
2081 while(**ptr) {
2082 if(**ptr == '%' && known_scheme &&
2083 !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
2084 if(!check_pct_encoded(ptr)) {
2085 *ptr = data->fragment;
2086 data->fragment = NULL;
2087 return FALSE;
2088 } else
2089 continue;
2092 ++(*ptr);
2095 data->fragment_len = *ptr - data->fragment;
2097 TRACE("(%p %p %x): Parsed fragment %s len=%d\n", ptr, data, flags,
2098 debugstr_wn(data->fragment, data->fragment_len), data->fragment_len);
2099 return TRUE;
2102 /* Parses and validates the components of the specified by data->uri
2103 * and stores the information it parses into 'data'.
2105 * Returns TRUE if it successfully parsed the URI. False otherwise.
2107 static BOOL parse_uri(parse_data *data, DWORD flags) {
2108 const WCHAR *ptr;
2109 const WCHAR **pptr;
2111 ptr = data->uri;
2112 pptr = &ptr;
2114 TRACE("(%p %x): BEGINNING TO PARSE URI %s.\n", data, flags, debugstr_w(data->uri));
2116 if(!parse_scheme(pptr, data, flags, 0))
2117 return FALSE;
2119 if(!parse_hierpart(pptr, data, flags))
2120 return FALSE;
2122 if(!parse_query(pptr, data, flags))
2123 return FALSE;
2125 if(!parse_fragment(pptr, data, flags))
2126 return FALSE;
2128 TRACE("(%p %x): FINISHED PARSING URI.\n", data, flags);
2129 return TRUE;
2132 static BOOL canonicalize_username(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2133 const WCHAR *ptr;
2135 if(!data->username) {
2136 uri->userinfo_start = -1;
2137 return TRUE;
2140 uri->userinfo_start = uri->canon_len;
2141 for(ptr = data->username; ptr < data->username+data->username_len; ++ptr) {
2142 if(*ptr == '%') {
2143 /* Only decode % encoded values for known scheme types. */
2144 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
2145 /* See if the value really needs decoding. */
2146 WCHAR val = decode_pct_val(ptr);
2147 if(is_unreserved(val)) {
2148 if(!computeOnly)
2149 uri->canon_uri[uri->canon_len] = val;
2151 ++uri->canon_len;
2153 /* Move pass the hex characters. */
2154 ptr += 2;
2155 continue;
2158 } else if(!is_reserved(*ptr) && !is_unreserved(*ptr) && *ptr != '\\') {
2159 /* Only percent encode forbidden characters if the NO_ENCODE_FORBIDDEN_CHARACTERS flag
2160 * is NOT set.
2162 if(!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS)) {
2163 if(!computeOnly)
2164 pct_encode_val(*ptr, uri->canon_uri + uri->canon_len);
2166 uri->canon_len += 3;
2167 continue;
2171 if(!computeOnly)
2172 /* Nothing special, so just copy the character over. */
2173 uri->canon_uri[uri->canon_len] = *ptr;
2174 ++uri->canon_len;
2177 return TRUE;
2180 static BOOL canonicalize_password(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2181 const WCHAR *ptr;
2183 if(!data->password) {
2184 uri->userinfo_split = -1;
2185 return TRUE;
2188 if(uri->userinfo_start == -1)
2189 /* Has a password, but, doesn't have a username. */
2190 uri->userinfo_start = uri->canon_len;
2192 uri->userinfo_split = uri->canon_len - uri->userinfo_start;
2194 /* Add the ':' to the userinfo component. */
2195 if(!computeOnly)
2196 uri->canon_uri[uri->canon_len] = ':';
2197 ++uri->canon_len;
2199 for(ptr = data->password; ptr < data->password+data->password_len; ++ptr) {
2200 if(*ptr == '%') {
2201 /* Only decode % encoded values for known scheme types. */
2202 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
2203 /* See if the value really needs decoding. */
2204 WCHAR val = decode_pct_val(ptr);
2205 if(is_unreserved(val)) {
2206 if(!computeOnly)
2207 uri->canon_uri[uri->canon_len] = val;
2209 ++uri->canon_len;
2211 /* Move pass the hex characters. */
2212 ptr += 2;
2213 continue;
2216 } else if(!is_reserved(*ptr) && !is_unreserved(*ptr) && *ptr != '\\') {
2217 /* Only percent encode forbidden characters if the NO_ENCODE_FORBIDDEN_CHARACTERS flag
2218 * is NOT set.
2220 if(!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS)) {
2221 if(!computeOnly)
2222 pct_encode_val(*ptr, uri->canon_uri + uri->canon_len);
2224 uri->canon_len += 3;
2225 continue;
2229 if(!computeOnly)
2230 /* Nothing special, so just copy the character over. */
2231 uri->canon_uri[uri->canon_len] = *ptr;
2232 ++uri->canon_len;
2235 return TRUE;
2238 /* Canonicalizes the userinfo of the URI represented by the parse_data.
2240 * Canonicalization of the userinfo is a simple process. If there are any percent
2241 * encoded characters that fall in the "unreserved" character set, they are decoded
2242 * to their actual value. If a character is not in the "unreserved" or "reserved" sets
2243 * then it is percent encoded. Other than that the characters are copied over without
2244 * change.
2246 static BOOL canonicalize_userinfo(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2247 uri->userinfo_start = uri->userinfo_split = -1;
2248 uri->userinfo_len = 0;
2250 if(!data->username && !data->password)
2251 /* URI doesn't have userinfo, so nothing to do here. */
2252 return TRUE;
2254 if(!canonicalize_username(data, uri, flags, computeOnly))
2255 return FALSE;
2257 if(!canonicalize_password(data, uri, flags, computeOnly))
2258 return FALSE;
2260 uri->userinfo_len = uri->canon_len - uri->userinfo_start;
2261 if(!computeOnly)
2262 TRACE("(%p %p %x %d): Canonicalized userinfo, userinfo_start=%d, userinfo=%s, userinfo_split=%d userinfo_len=%d.\n",
2263 data, uri, flags, computeOnly, uri->userinfo_start, debugstr_wn(uri->canon_uri + uri->userinfo_start, uri->userinfo_len),
2264 uri->userinfo_split, uri->userinfo_len);
2266 /* Now insert the '@' after the userinfo. */
2267 if(!computeOnly)
2268 uri->canon_uri[uri->canon_len] = '@';
2269 ++uri->canon_len;
2271 return TRUE;
2274 /* Attempts to canonicalize a reg_name.
2276 * Things that happen:
2277 * 1) If Uri_CREATE_NO_CANONICALIZE flag is not set, then the reg_name is
2278 * lower cased. Unless it's an unknown scheme type, which case it's
2279 * no lower cased regardless.
2281 * 2) Unreserved % encoded characters are decoded for known
2282 * scheme types.
2284 * 3) Forbidden characters are % encoded as long as
2285 * Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS flag is not set and
2286 * it isn't an unknown scheme type.
2288 * 4) If it's a file scheme and the host is "localhost" it's removed.
2290 * 5) If it's a file scheme and Uri_CREATE_FILE_USE_DOS_PATH is set,
2291 * then the UNC path characters are added before the host name.
2293 static BOOL canonicalize_reg_name(const parse_data *data, Uri *uri,
2294 DWORD flags, BOOL computeOnly) {
2295 static const WCHAR localhostW[] =
2296 {'l','o','c','a','l','h','o','s','t',0};
2297 const WCHAR *ptr;
2298 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
2300 if(data->scheme_type == URL_SCHEME_FILE &&
2301 data->host_len == lstrlenW(localhostW)) {
2302 if(!StrCmpNIW(data->host, localhostW, data->host_len)) {
2303 uri->host_start = -1;
2304 uri->host_len = 0;
2305 uri->host_type = Uri_HOST_UNKNOWN;
2306 return TRUE;
2310 if(data->scheme_type == URL_SCHEME_FILE && flags & Uri_CREATE_FILE_USE_DOS_PATH) {
2311 if(!computeOnly) {
2312 uri->canon_uri[uri->canon_len] = '\\';
2313 uri->canon_uri[uri->canon_len+1] = '\\';
2315 uri->canon_len += 2;
2316 uri->authority_start = uri->canon_len;
2319 uri->host_start = uri->canon_len;
2321 for(ptr = data->host; ptr < data->host+data->host_len; ++ptr) {
2322 if(*ptr == '%' && known_scheme) {
2323 WCHAR val = decode_pct_val(ptr);
2324 if(is_unreserved(val)) {
2325 /* If NO_CANONICALIZE is not set, then windows lower cases the
2326 * decoded value.
2328 if(!(flags & Uri_CREATE_NO_CANONICALIZE) && isupperW(val)) {
2329 if(!computeOnly)
2330 uri->canon_uri[uri->canon_len] = tolowerW(val);
2331 } else {
2332 if(!computeOnly)
2333 uri->canon_uri[uri->canon_len] = val;
2335 ++uri->canon_len;
2337 /* Skip past the % encoded character. */
2338 ptr += 2;
2339 continue;
2340 } else {
2341 /* Just copy the % over. */
2342 if(!computeOnly)
2343 uri->canon_uri[uri->canon_len] = *ptr;
2344 ++uri->canon_len;
2346 } else if(*ptr == '\\') {
2347 /* Only unknown scheme types could have made it here with a '\\' in the host name. */
2348 if(!computeOnly)
2349 uri->canon_uri[uri->canon_len] = *ptr;
2350 ++uri->canon_len;
2351 } else if(!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS) &&
2352 !is_unreserved(*ptr) && !is_reserved(*ptr) && known_scheme) {
2353 if(!computeOnly) {
2354 pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
2356 /* The percent encoded value gets lower cased also. */
2357 if(!(flags & Uri_CREATE_NO_CANONICALIZE)) {
2358 uri->canon_uri[uri->canon_len+1] = tolowerW(uri->canon_uri[uri->canon_len+1]);
2359 uri->canon_uri[uri->canon_len+2] = tolowerW(uri->canon_uri[uri->canon_len+2]);
2363 uri->canon_len += 3;
2364 } else {
2365 if(!computeOnly) {
2366 if(!(flags & Uri_CREATE_NO_CANONICALIZE) && known_scheme)
2367 uri->canon_uri[uri->canon_len] = tolowerW(*ptr);
2368 else
2369 uri->canon_uri[uri->canon_len] = *ptr;
2372 ++uri->canon_len;
2376 uri->host_len = uri->canon_len - uri->host_start;
2378 if(!computeOnly)
2379 TRACE("(%p %p %x %d): Canonicalize reg_name=%s len=%d\n", data, uri, flags,
2380 computeOnly, debugstr_wn(uri->canon_uri+uri->host_start, uri->host_len),
2381 uri->host_len);
2383 if(!computeOnly)
2384 find_domain_name(uri->canon_uri+uri->host_start, uri->host_len,
2385 &(uri->domain_offset));
2387 return TRUE;
2390 /* Attempts to canonicalize an implicit IPv4 address. */
2391 static BOOL canonicalize_implicit_ipv4address(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2392 uri->host_start = uri->canon_len;
2394 TRACE("%u\n", data->implicit_ipv4);
2395 /* For unknown scheme types Windows doesn't convert
2396 * the value into an IP address, but it still considers
2397 * it an IPv4 address.
2399 if(data->scheme_type == URL_SCHEME_UNKNOWN) {
2400 if(!computeOnly)
2401 memcpy(uri->canon_uri+uri->canon_len, data->host, data->host_len*sizeof(WCHAR));
2402 uri->canon_len += data->host_len;
2403 } else {
2404 if(!computeOnly)
2405 uri->canon_len += ui2ipv4(uri->canon_uri+uri->canon_len, data->implicit_ipv4);
2406 else
2407 uri->canon_len += ui2ipv4(NULL, data->implicit_ipv4);
2410 uri->host_len = uri->canon_len - uri->host_start;
2411 uri->host_type = Uri_HOST_IPV4;
2413 if(!computeOnly)
2414 TRACE("%p %p %x %d): Canonicalized implicit IP address=%s len=%d\n",
2415 data, uri, flags, computeOnly,
2416 debugstr_wn(uri->canon_uri+uri->host_start, uri->host_len),
2417 uri->host_len);
2419 return TRUE;
2422 /* Attempts to canonicalize an IPv4 address.
2424 * If the parse_data represents a URI that has an implicit IPv4 address
2425 * (ex. http://256/, this function will convert 256 into 0.0.1.0). If
2426 * the implicit IP address exceeds the value of UINT_MAX (maximum value
2427 * for an IPv4 address) it's canonicalized as if it were a reg-name.
2429 * If the parse_data contains a partial or full IPv4 address it normalizes it.
2430 * A partial IPv4 address is something like "192.0" and would be normalized to
2431 * "192.0.0.0". With a full (or partial) IPv4 address like "192.002.01.003" would
2432 * be normalized to "192.2.1.3".
2434 * NOTES:
2435 * Windows ONLY normalizes IPv4 address for known scheme types (one that isn't
2436 * URL_SCHEME_UNKNOWN). For unknown scheme types, it simply copies the data from
2437 * the original URI into the canonicalized URI, but, it still recognizes URI's
2438 * host type as HOST_IPV4.
2440 static BOOL canonicalize_ipv4address(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2441 if(data->has_implicit_ip)
2442 return canonicalize_implicit_ipv4address(data, uri, flags, computeOnly);
2443 else {
2444 uri->host_start = uri->canon_len;
2446 /* Windows only normalizes for known scheme types. */
2447 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
2448 /* parse_data contains a partial or full IPv4 address, so normalize it. */
2449 DWORD i, octetDigitCount = 0, octetCount = 0;
2450 BOOL octetHasDigit = FALSE;
2452 for(i = 0; i < data->host_len; ++i) {
2453 if(data->host[i] == '0' && !octetHasDigit) {
2454 /* Can ignore leading zeros if:
2455 * 1) It isn't the last digit of the octet.
2456 * 2) i+1 != data->host_len
2457 * 3) i+1 != '.'
2459 if(octetDigitCount == 2 ||
2460 i+1 == data->host_len ||
2461 data->host[i+1] == '.') {
2462 if(!computeOnly)
2463 uri->canon_uri[uri->canon_len] = data->host[i];
2464 ++uri->canon_len;
2465 TRACE("Adding zero\n");
2467 } else if(data->host[i] == '.') {
2468 if(!computeOnly)
2469 uri->canon_uri[uri->canon_len] = data->host[i];
2470 ++uri->canon_len;
2472 octetDigitCount = 0;
2473 octetHasDigit = FALSE;
2474 ++octetCount;
2475 } else {
2476 if(!computeOnly)
2477 uri->canon_uri[uri->canon_len] = data->host[i];
2478 ++uri->canon_len;
2480 ++octetDigitCount;
2481 octetHasDigit = TRUE;
2485 /* Make sure the canonicalized IP address has 4 dec-octets.
2486 * If doesn't add "0" ones until there is 4;
2488 for( ; octetCount < 3; ++octetCount) {
2489 if(!computeOnly) {
2490 uri->canon_uri[uri->canon_len] = '.';
2491 uri->canon_uri[uri->canon_len+1] = '0';
2494 uri->canon_len += 2;
2496 } else {
2497 /* Windows doesn't normalize addresses in unknown schemes. */
2498 if(!computeOnly)
2499 memcpy(uri->canon_uri+uri->canon_len, data->host, data->host_len*sizeof(WCHAR));
2500 uri->canon_len += data->host_len;
2503 uri->host_len = uri->canon_len - uri->host_start;
2504 if(!computeOnly)
2505 TRACE("(%p %p %x %d): Canonicalized IPv4 address, ip=%s len=%d\n",
2506 data, uri, flags, computeOnly,
2507 debugstr_wn(uri->canon_uri+uri->host_start, uri->host_len),
2508 uri->host_len);
2511 return TRUE;
2514 /* Attempts to canonicalize the IPv6 address of the URI.
2516 * Multiple things happen during the canonicalization of an IPv6 address:
2517 * 1) Any leading zero's in a h16 component are removed.
2518 * Ex: [0001:0022::] -> [1:22::]
2520 * 2) The longest sequence of zero h16 components are compressed
2521 * into a "::" (elision). If there's a tie, the first is chosen.
2523 * Ex: [0:0:0:0:1:6:7:8] -> [::1:6:7:8]
2524 * [0:0:0:0:1:2::] -> [::1:2:0:0]
2525 * [0:0:1:2:0:0:7:8] -> [::1:2:0:0:7:8]
2527 * 3) If an IPv4 address is attached to the IPv6 address, it's
2528 * also normalized.
2529 * Ex: [::001.002.022.000] -> [::1.2.22.0]
2531 * 4) If an elision is present, but, only represents one h16 component
2532 * it's expanded.
2534 * Ex: [1::2:3:4:5:6:7] -> [1:0:2:3:4:5:6:7]
2536 * 5) If the IPv6 address contains an IPv4 address and there exists
2537 * at least 1 non-zero h16 component the IPv4 address is converted
2538 * into two h16 components, otherwise it's normalized and kept as is.
2540 * Ex: [::192.200.003.4] -> [::192.200.3.4]
2541 * [ffff::192.200.003.4] -> [ffff::c0c8:3041]
2543 * NOTE:
2544 * For unknown scheme types Windows simply copies the address over without any
2545 * changes.
2547 * IPv4 address can be included in an elision if all its components are 0's.
2549 static BOOL canonicalize_ipv6address(const parse_data *data, Uri *uri,
2550 DWORD flags, BOOL computeOnly) {
2551 uri->host_start = uri->canon_len;
2553 if(data->scheme_type == URL_SCHEME_UNKNOWN) {
2554 if(!computeOnly)
2555 memcpy(uri->canon_uri+uri->canon_len, data->host, data->host_len*sizeof(WCHAR));
2556 uri->canon_len += data->host_len;
2557 } else {
2558 USHORT values[8];
2559 INT elision_start;
2560 DWORD i, elision_len;
2562 if(!ipv6_to_number(&(data->ipv6_address), values)) {
2563 TRACE("(%p %p %x %d): Failed to compute numerical value for IPv6 address.\n",
2564 data, uri, flags, computeOnly);
2565 return FALSE;
2568 if(!computeOnly)
2569 uri->canon_uri[uri->canon_len] = '[';
2570 ++uri->canon_len;
2572 /* Find where the elision should occur (if any). */
2573 compute_elision_location(&(data->ipv6_address), values, &elision_start, &elision_len);
2575 TRACE("%p %p %x %d): Elision starts at %d, len=%u\n", data, uri, flags,
2576 computeOnly, elision_start, elision_len);
2578 for(i = 0; i < 8; ++i) {
2579 BOOL in_elision = (elision_start > -1 && i >= elision_start &&
2580 i < elision_start+elision_len);
2581 BOOL do_ipv4 = (i == 6 && data->ipv6_address.ipv4 && !in_elision &&
2582 data->ipv6_address.h16_count == 0);
2584 if(i == elision_start) {
2585 if(!computeOnly) {
2586 uri->canon_uri[uri->canon_len] = ':';
2587 uri->canon_uri[uri->canon_len+1] = ':';
2589 uri->canon_len += 2;
2592 /* We can ignore the current component if we're in the elision. */
2593 if(in_elision)
2594 continue;
2596 /* We only add a ':' if we're not at i == 0, or when we're at
2597 * the very end of elision range since the ':' colon was handled
2598 * earlier. Otherwise we would end up with ":::" after elision.
2600 if(i != 0 && !(elision_start > -1 && i == elision_start+elision_len)) {
2601 if(!computeOnly)
2602 uri->canon_uri[uri->canon_len] = ':';
2603 ++uri->canon_len;
2606 if(do_ipv4) {
2607 UINT val;
2608 DWORD len;
2610 /* Combine the two parts of the IPv4 address values. */
2611 val = values[i];
2612 val <<= 16;
2613 val += values[i+1];
2615 if(!computeOnly)
2616 len = ui2ipv4(uri->canon_uri+uri->canon_len, val);
2617 else
2618 len = ui2ipv4(NULL, val);
2620 uri->canon_len += len;
2621 ++i;
2622 } else {
2623 /* Write a regular h16 component to the URI. */
2625 /* Short circuit for the trivial case. */
2626 if(values[i] == 0) {
2627 if(!computeOnly)
2628 uri->canon_uri[uri->canon_len] = '0';
2629 ++uri->canon_len;
2630 } else {
2631 static const WCHAR formatW[] = {'%','x',0};
2633 if(!computeOnly)
2634 uri->canon_len += sprintfW(uri->canon_uri+uri->canon_len,
2635 formatW, values[i]);
2636 else {
2637 WCHAR tmp[5];
2638 uri->canon_len += sprintfW(tmp, formatW, values[i]);
2644 /* Add the closing ']'. */
2645 if(!computeOnly)
2646 uri->canon_uri[uri->canon_len] = ']';
2647 ++uri->canon_len;
2650 uri->host_len = uri->canon_len - uri->host_start;
2652 if(!computeOnly)
2653 TRACE("(%p %p %x %d): Canonicalized IPv6 address %s, len=%d\n", data, uri, flags,
2654 computeOnly, debugstr_wn(uri->canon_uri+uri->host_start, uri->host_len),
2655 uri->host_len);
2657 return TRUE;
2660 /* Attempts to canonicalize the host of the URI (if any). */
2661 static BOOL canonicalize_host(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2662 uri->host_start = -1;
2663 uri->host_len = 0;
2664 uri->domain_offset = -1;
2666 if(data->host) {
2667 switch(data->host_type) {
2668 case Uri_HOST_DNS:
2669 uri->host_type = Uri_HOST_DNS;
2670 if(!canonicalize_reg_name(data, uri, flags, computeOnly))
2671 return FALSE;
2673 break;
2674 case Uri_HOST_IPV4:
2675 uri->host_type = Uri_HOST_IPV4;
2676 if(!canonicalize_ipv4address(data, uri, flags, computeOnly))
2677 return FALSE;
2679 break;
2680 case Uri_HOST_IPV6:
2681 if(!canonicalize_ipv6address(data, uri, flags, computeOnly))
2682 return FALSE;
2684 uri->host_type = Uri_HOST_IPV6;
2685 break;
2686 case Uri_HOST_UNKNOWN:
2687 if(data->host_len > 0 || data->scheme_type != URL_SCHEME_FILE) {
2688 uri->host_start = uri->canon_len;
2690 /* Nothing happens to unknown host types. */
2691 if(!computeOnly)
2692 memcpy(uri->canon_uri+uri->canon_len, data->host, data->host_len*sizeof(WCHAR));
2693 uri->canon_len += data->host_len;
2694 uri->host_len = data->host_len;
2697 uri->host_type = Uri_HOST_UNKNOWN;
2698 break;
2699 default:
2700 FIXME("(%p %p %x %d): Canonicalization for host type %d not supported.\n", data,
2701 uri, flags, computeOnly, data->host_type);
2702 return FALSE;
2706 return TRUE;
2709 static BOOL canonicalize_port(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2710 BOOL has_default_port = FALSE;
2711 USHORT default_port = 0;
2712 DWORD i;
2714 uri->port_offset = -1;
2716 /* Check if the scheme has a default port. */
2717 for(i = 0; i < sizeof(default_ports)/sizeof(default_ports[0]); ++i) {
2718 if(default_ports[i].scheme == data->scheme_type) {
2719 has_default_port = TRUE;
2720 default_port = default_ports[i].port;
2721 break;
2725 uri->has_port = data->has_port || has_default_port;
2727 /* Possible cases:
2728 * 1) Has a port which is the default port.
2729 * 2) Has a port (not the default).
2730 * 3) Doesn't have a port, but, scheme has a default port.
2731 * 4) No port.
2733 if(has_default_port && data->has_port && data->port_value == default_port) {
2734 /* If it's the default port and this flag isn't set, don't do anything. */
2735 if(flags & Uri_CREATE_NO_CANONICALIZE) {
2736 uri->port_offset = uri->canon_len-uri->authority_start;
2737 if(!computeOnly)
2738 uri->canon_uri[uri->canon_len] = ':';
2739 ++uri->canon_len;
2741 if(data->port) {
2742 /* Copy the original port over. */
2743 if(!computeOnly)
2744 memcpy(uri->canon_uri+uri->canon_len, data->port, data->port_len*sizeof(WCHAR));
2745 uri->canon_len += data->port_len;
2746 } else {
2747 if(!computeOnly)
2748 uri->canon_len += ui2str(uri->canon_uri+uri->canon_len, data->port_value);
2749 else
2750 uri->canon_len += ui2str(NULL, data->port_value);
2754 uri->port = default_port;
2755 } else if(data->has_port) {
2756 uri->port_offset = uri->canon_len-uri->authority_start;
2757 if(!computeOnly)
2758 uri->canon_uri[uri->canon_len] = ':';
2759 ++uri->canon_len;
2761 if(flags & Uri_CREATE_NO_CANONICALIZE && data->port) {
2762 /* Copy the original over without changes. */
2763 if(!computeOnly)
2764 memcpy(uri->canon_uri+uri->canon_len, data->port, data->port_len*sizeof(WCHAR));
2765 uri->canon_len += data->port_len;
2766 } else {
2767 if(!computeOnly)
2768 uri->canon_len += ui2str(uri->canon_uri+uri->canon_len, data->port_value);
2769 else
2770 uri->canon_len += ui2str(NULL, data->port_value);
2773 uri->port = data->port_value;
2774 } else if(has_default_port)
2775 uri->port = default_port;
2777 return TRUE;
2780 /* Canonicalizes the authority of the URI represented by the parse_data. */
2781 static BOOL canonicalize_authority(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2782 uri->authority_start = uri->canon_len;
2783 uri->authority_len = 0;
2785 if(!canonicalize_userinfo(data, uri, flags, computeOnly))
2786 return FALSE;
2788 if(!canonicalize_host(data, uri, flags, computeOnly))
2789 return FALSE;
2791 if(!canonicalize_port(data, uri, flags, computeOnly))
2792 return FALSE;
2794 if(uri->host_start != -1 || (data->is_relative && (data->password || data->username)))
2795 uri->authority_len = uri->canon_len - uri->authority_start;
2796 else
2797 uri->authority_start = -1;
2799 return TRUE;
2802 /* Attempts to canonicalize the path of a hierarchical URI.
2804 * Things that happen:
2805 * 1). Forbidden characters are percent encoded, unless the NO_ENCODE_FORBIDDEN
2806 * flag is set or it's a file URI. Forbidden characters are always encoded
2807 * for file schemes regardless and forbidden characters are never encoded
2808 * for unknown scheme types.
2810 * 2). For known scheme types '\\' are changed to '/'.
2812 * 3). Percent encoded, unreserved characters are decoded to their actual values.
2813 * Unless the scheme type is unknown. For file schemes any percent encoded
2814 * character in the unreserved or reserved set is decoded.
2816 * 4). For File schemes if the path is starts with a drive letter and doesn't
2817 * start with a '/' then one is appended.
2818 * Ex: file://c:/test.mp3 -> file:///c:/test.mp3
2820 * 5). Dot segments are removed from the path for all scheme types
2821 * unless NO_CANONICALIZE flag is set. Dot segments aren't removed
2822 * for wildcard scheme types.
2824 * NOTES:
2825 * file://c:/test%20test -> file:///c:/test%2520test
2826 * file://c:/test%3Etest -> file:///c:/test%253Etest
2827 * if Uri_CREATE_FILE_USE_DOS_PATH is not set:
2828 * file:///c:/test%20test -> file:///c:/test%20test
2829 * file:///c:/test%test -> file:///c:/test%25test
2831 static DWORD canonicalize_path_hierarchical(const WCHAR *path, DWORD path_len, URL_SCHEME scheme_type, BOOL has_host, DWORD flags,
2832 WCHAR *ret_path) {
2833 const BOOL known_scheme = scheme_type != URL_SCHEME_UNKNOWN;
2834 const BOOL is_file = scheme_type == URL_SCHEME_FILE;
2835 const BOOL is_res = scheme_type == URL_SCHEME_RES;
2836 const WCHAR *ptr;
2837 BOOL escape_pct = FALSE;
2838 DWORD len = 0;
2840 if(!path)
2841 return 0;
2843 ptr = path;
2845 if(is_file && !has_host) {
2846 /* Check if a '/' needs to be appended for the file scheme. */
2847 if(path_len > 1 && is_drive_path(ptr) && !(flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
2848 if(ret_path)
2849 ret_path[len] = '/';
2850 len++;
2851 escape_pct = TRUE;
2852 } else if(*ptr == '/') {
2853 if(!(flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
2854 /* Copy the extra '/' over. */
2855 if(ret_path)
2856 ret_path[len] = '/';
2857 len++;
2859 ++ptr;
2862 if(is_drive_path(ptr)) {
2863 if(ret_path) {
2864 ret_path[len] = *ptr;
2865 /* If there's a '|' after the drive letter, convert it to a ':'. */
2866 ret_path[len+1] = ':';
2868 ptr += 2;
2869 len += 2;
2873 if(!is_file && *path && *path != '/') {
2874 /* Prepend a '/' to the path if it doesn't have one. */
2875 if(ret_path)
2876 ret_path[len] = '/';
2877 len++;
2880 for(; ptr < path+path_len; ++ptr) {
2881 BOOL do_default_action = TRUE;
2883 if(*ptr == '%' && !is_res) {
2884 const WCHAR *tmp = ptr;
2885 WCHAR val;
2887 /* Check if the % represents a valid encoded char, or if it needs encoding. */
2888 BOOL force_encode = !check_pct_encoded(&tmp) && is_file && !(flags&Uri_CREATE_FILE_USE_DOS_PATH);
2889 val = decode_pct_val(ptr);
2891 if(force_encode || escape_pct) {
2892 /* Escape the percent sign in the file URI. */
2893 if(ret_path)
2894 pct_encode_val(*ptr, ret_path+len);
2895 len += 3;
2896 do_default_action = FALSE;
2897 } else if((is_unreserved(val) && known_scheme) ||
2898 (is_file && (is_unreserved(val) || is_reserved(val) ||
2899 (val && flags&Uri_CREATE_FILE_USE_DOS_PATH && !is_forbidden_dos_path_char(val))))) {
2900 if(ret_path)
2901 ret_path[len] = val;
2902 len++;
2904 ptr += 2;
2905 continue;
2907 } else if(*ptr == '/' && is_file && (flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
2908 /* Convert the '/' back to a '\\'. */
2909 if(ret_path)
2910 ret_path[len] = '\\';
2911 len++;
2912 do_default_action = FALSE;
2913 } else if(*ptr == '\\' && known_scheme) {
2914 if(!(is_file && (flags & Uri_CREATE_FILE_USE_DOS_PATH))) {
2915 /* Convert '\\' into a '/'. */
2916 if(ret_path)
2917 ret_path[len] = '/';
2918 len++;
2919 do_default_action = FALSE;
2921 } else if(known_scheme && !is_res && !is_unreserved(*ptr) && !is_reserved(*ptr) &&
2922 (!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS) || is_file)) {
2923 if(!(is_file && (flags & Uri_CREATE_FILE_USE_DOS_PATH))) {
2924 /* Escape the forbidden character. */
2925 if(ret_path)
2926 pct_encode_val(*ptr, ret_path+len);
2927 len += 3;
2928 do_default_action = FALSE;
2932 if(do_default_action) {
2933 if(ret_path)
2934 ret_path[len] = *ptr;
2935 len++;
2939 /* Removing the dot segments only happens when it's not in
2940 * computeOnly mode and it's not a wildcard scheme. File schemes
2941 * with USE_DOS_PATH set don't get dot segments removed.
2943 if(!(is_file && (flags & Uri_CREATE_FILE_USE_DOS_PATH)) &&
2944 scheme_type != URL_SCHEME_WILDCARD) {
2945 if(!(flags & Uri_CREATE_NO_CANONICALIZE) && ret_path) {
2946 /* Remove the dot segments (if any) and reset everything to the new
2947 * correct length.
2949 len = remove_dot_segments(ret_path, len);
2953 if(ret_path)
2954 TRACE("Canonicalized path %s len=%d\n", debugstr_wn(ret_path, len), len);
2955 return len;
2958 /* Attempts to canonicalize the path for an opaque URI.
2960 * For known scheme types:
2961 * 1) forbidden characters are percent encoded if
2962 * NO_ENCODE_FORBIDDEN_CHARACTERS isn't set.
2964 * 2) Percent encoded, unreserved characters are decoded
2965 * to their actual values, for known scheme types.
2967 * 3) '\\' are changed to '/' for known scheme types
2968 * except for mailto schemes.
2970 * 4) For file schemes, if USE_DOS_PATH is set all '/'
2971 * are converted to backslashes.
2973 * 5) For file schemes, if USE_DOS_PATH isn't set all '\'
2974 * are converted to forward slashes.
2976 static BOOL canonicalize_path_opaque(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2977 const WCHAR *ptr;
2978 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
2979 const BOOL is_file = data->scheme_type == URL_SCHEME_FILE;
2980 const BOOL is_mk = data->scheme_type == URL_SCHEME_MK;
2982 if(!data->path) {
2983 uri->path_start = -1;
2984 uri->path_len = 0;
2985 return TRUE;
2988 uri->path_start = uri->canon_len;
2990 if(is_mk){
2991 /* hijack this flag for SCHEME_MK to tell the function when to start
2992 * converting slashes */
2993 flags |= Uri_CREATE_FILE_USE_DOS_PATH;
2996 /* For javascript: URIs, simply copy path part without any canonicalization */
2997 if(data->scheme_type == URL_SCHEME_JAVASCRIPT) {
2998 if(!computeOnly)
2999 memcpy(uri->canon_uri+uri->canon_len, data->path, data->path_len*sizeof(WCHAR));
3000 uri->path_len = data->path_len;
3001 uri->canon_len += data->path_len;
3002 return TRUE;
3005 /* Windows doesn't allow a "//" to appear after the scheme
3006 * of a URI, if it's an opaque URI.
3008 if(data->scheme && *(data->path) == '/' && *(data->path+1) == '/') {
3009 /* So it inserts a "/." before the "//" if it exists. */
3010 if(!computeOnly) {
3011 uri->canon_uri[uri->canon_len] = '/';
3012 uri->canon_uri[uri->canon_len+1] = '.';
3015 uri->canon_len += 2;
3018 for(ptr = data->path; ptr < data->path+data->path_len; ++ptr) {
3019 BOOL do_default_action = TRUE;
3021 if(*ptr == '%' && known_scheme) {
3022 WCHAR val = decode_pct_val(ptr);
3024 if(is_unreserved(val)) {
3025 if(!computeOnly)
3026 uri->canon_uri[uri->canon_len] = val;
3027 ++uri->canon_len;
3029 ptr += 2;
3030 continue;
3032 } else if(*ptr == '/' && is_file && (flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
3033 if(!computeOnly)
3034 uri->canon_uri[uri->canon_len] = '\\';
3035 ++uri->canon_len;
3036 do_default_action = FALSE;
3037 } else if(*ptr == '\\') {
3038 if((data->is_relative || is_mk || is_file) && !(flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
3039 /* Convert to a '/'. */
3040 if(!computeOnly)
3041 uri->canon_uri[uri->canon_len] = '/';
3042 ++uri->canon_len;
3043 do_default_action = FALSE;
3045 } else if(is_mk && *ptr == ':' && ptr + 1 < data->path + data->path_len && *(ptr + 1) == ':') {
3046 flags &= ~Uri_CREATE_FILE_USE_DOS_PATH;
3047 } else if(known_scheme && !is_unreserved(*ptr) && !is_reserved(*ptr) &&
3048 !(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS)) {
3049 if(!(is_file && (flags & Uri_CREATE_FILE_USE_DOS_PATH))) {
3050 if(!computeOnly)
3051 pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
3052 uri->canon_len += 3;
3053 do_default_action = FALSE;
3057 if(do_default_action) {
3058 if(!computeOnly)
3059 uri->canon_uri[uri->canon_len] = *ptr;
3060 ++uri->canon_len;
3064 if(is_mk && !computeOnly && !(flags & Uri_CREATE_NO_CANONICALIZE)) {
3065 DWORD new_len = remove_dot_segments(uri->canon_uri + uri->path_start,
3066 uri->canon_len - uri->path_start);
3067 uri->canon_len = uri->path_start + new_len;
3070 uri->path_len = uri->canon_len - uri->path_start;
3072 if(!computeOnly)
3073 TRACE("(%p %p %x %d): Canonicalized opaque URI path %s len=%d\n", data, uri, flags, computeOnly,
3074 debugstr_wn(uri->canon_uri+uri->path_start, uri->path_len), uri->path_len);
3075 return TRUE;
3078 /* Determines how the URI represented by the parse_data should be canonicalized.
3080 * Essentially, if the parse_data represents an hierarchical URI then it calls
3081 * canonicalize_authority and the canonicalization functions for the path. If the
3082 * URI is opaque it canonicalizes the path of the URI.
3084 static BOOL canonicalize_hierpart(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
3085 if(!data->is_opaque || (data->is_relative && (data->password || data->username))) {
3086 /* "//" is only added for non-wildcard scheme types.
3088 * A "//" is only added to a relative URI if it has a
3089 * host or port component (this only happens if a IUriBuilder
3090 * is generating an IUri).
3092 if((data->is_relative && (data->host || data->has_port)) ||
3093 (!data->is_relative && data->scheme_type != URL_SCHEME_WILDCARD)) {
3094 if(data->scheme_type == URL_SCHEME_WILDCARD)
3095 FIXME("Here\n");
3097 if(!computeOnly) {
3098 INT pos = uri->canon_len;
3100 uri->canon_uri[pos] = '/';
3101 uri->canon_uri[pos+1] = '/';
3103 uri->canon_len += 2;
3106 if(!canonicalize_authority(data, uri, flags, computeOnly))
3107 return FALSE;
3109 if(data->is_relative && (data->password || data->username)) {
3110 if(!canonicalize_path_opaque(data, uri, flags, computeOnly))
3111 return FALSE;
3112 } else {
3113 if(!computeOnly)
3114 uri->path_start = uri->canon_len;
3115 uri->path_len = canonicalize_path_hierarchical(data->path, data->path_len, data->scheme_type, data->host_len != 0,
3116 flags, computeOnly ? NULL : uri->canon_uri+uri->canon_len);
3117 uri->canon_len += uri->path_len;
3118 if(!computeOnly && !uri->path_len)
3119 uri->path_start = -1;
3121 } else {
3122 /* Opaque URI's don't have an authority. */
3123 uri->userinfo_start = uri->userinfo_split = -1;
3124 uri->userinfo_len = 0;
3125 uri->host_start = -1;
3126 uri->host_len = 0;
3127 uri->host_type = Uri_HOST_UNKNOWN;
3128 uri->has_port = FALSE;
3129 uri->authority_start = -1;
3130 uri->authority_len = 0;
3131 uri->domain_offset = -1;
3132 uri->port_offset = -1;
3134 if(is_hierarchical_scheme(data->scheme_type)) {
3135 DWORD i;
3137 /* Absolute URIs aren't displayed for known scheme types
3138 * which should be hierarchical URIs.
3140 uri->display_modifiers |= URI_DISPLAY_NO_ABSOLUTE_URI;
3142 /* Windows also sets the port for these (if they have one). */
3143 for(i = 0; i < sizeof(default_ports)/sizeof(default_ports[0]); ++i) {
3144 if(data->scheme_type == default_ports[i].scheme) {
3145 uri->has_port = TRUE;
3146 uri->port = default_ports[i].port;
3147 break;
3152 if(!canonicalize_path_opaque(data, uri, flags, computeOnly))
3153 return FALSE;
3156 if(uri->path_start > -1 && !computeOnly)
3157 /* Finding file extensions happens for both types of URIs. */
3158 uri->extension_offset = find_file_extension(uri->canon_uri+uri->path_start, uri->path_len);
3159 else
3160 uri->extension_offset = -1;
3162 return TRUE;
3165 /* Attempts to canonicalize the query string of the URI.
3167 * Things that happen:
3168 * 1) For known scheme types forbidden characters
3169 * are percent encoded, unless the NO_DECODE_EXTRA_INFO flag is set
3170 * or NO_ENCODE_FORBIDDEN_CHARACTERS is set.
3172 * 2) For known scheme types, percent encoded, unreserved characters
3173 * are decoded as long as the NO_DECODE_EXTRA_INFO flag isn't set.
3175 static BOOL canonicalize_query(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
3176 const WCHAR *ptr, *end;
3177 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
3179 if(!data->query) {
3180 uri->query_start = -1;
3181 uri->query_len = 0;
3182 return TRUE;
3185 uri->query_start = uri->canon_len;
3187 end = data->query+data->query_len;
3188 for(ptr = data->query; ptr < end; ++ptr) {
3189 if(*ptr == '%') {
3190 if(known_scheme && !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
3191 WCHAR val = decode_pct_val(ptr);
3192 if(is_unreserved(val)) {
3193 if(!computeOnly)
3194 uri->canon_uri[uri->canon_len] = val;
3195 ++uri->canon_len;
3197 ptr += 2;
3198 continue;
3201 } else if(known_scheme && !is_unreserved(*ptr) && !is_reserved(*ptr)) {
3202 if(!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS) &&
3203 !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
3204 if(!computeOnly)
3205 pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
3206 uri->canon_len += 3;
3207 continue;
3211 if(!computeOnly)
3212 uri->canon_uri[uri->canon_len] = *ptr;
3213 ++uri->canon_len;
3216 uri->query_len = uri->canon_len - uri->query_start;
3218 if(!computeOnly)
3219 TRACE("(%p %p %x %d): Canonicalized query string %s len=%d\n", data, uri, flags,
3220 computeOnly, debugstr_wn(uri->canon_uri+uri->query_start, uri->query_len),
3221 uri->query_len);
3222 return TRUE;
3225 static BOOL canonicalize_fragment(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
3226 const WCHAR *ptr, *end;
3227 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
3229 if(!data->fragment) {
3230 uri->fragment_start = -1;
3231 uri->fragment_len = 0;
3232 return TRUE;
3235 uri->fragment_start = uri->canon_len;
3237 end = data->fragment + data->fragment_len;
3238 for(ptr = data->fragment; ptr < end; ++ptr) {
3239 if(*ptr == '%') {
3240 if(known_scheme && !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
3241 WCHAR val = decode_pct_val(ptr);
3242 if(is_unreserved(val)) {
3243 if(!computeOnly)
3244 uri->canon_uri[uri->canon_len] = val;
3245 ++uri->canon_len;
3247 ptr += 2;
3248 continue;
3251 } else if(known_scheme && !is_unreserved(*ptr) && !is_reserved(*ptr)) {
3252 if(!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS) &&
3253 !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
3254 if(!computeOnly)
3255 pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
3256 uri->canon_len += 3;
3257 continue;
3261 if(!computeOnly)
3262 uri->canon_uri[uri->canon_len] = *ptr;
3263 ++uri->canon_len;
3266 uri->fragment_len = uri->canon_len - uri->fragment_start;
3268 if(!computeOnly)
3269 TRACE("(%p %p %x %d): Canonicalized fragment %s len=%d\n", data, uri, flags,
3270 computeOnly, debugstr_wn(uri->canon_uri+uri->fragment_start, uri->fragment_len),
3271 uri->fragment_len);
3272 return TRUE;
3275 /* Canonicalizes the scheme information specified in the parse_data using the specified flags. */
3276 static BOOL canonicalize_scheme(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
3277 uri->scheme_start = -1;
3278 uri->scheme_len = 0;
3280 if(!data->scheme) {
3281 /* The only type of URI that doesn't have to have a scheme is a relative
3282 * URI.
3284 if(!data->is_relative) {
3285 FIXME("(%p %p %x): Unable to determine the scheme type of %s.\n", data,
3286 uri, flags, debugstr_w(data->uri));
3287 return FALSE;
3289 } else {
3290 if(!computeOnly) {
3291 DWORD i;
3292 INT pos = uri->canon_len;
3294 for(i = 0; i < data->scheme_len; ++i) {
3295 /* Scheme name must be lower case after canonicalization. */
3296 uri->canon_uri[i + pos] = tolowerW(data->scheme[i]);
3299 uri->canon_uri[i + pos] = ':';
3300 uri->scheme_start = pos;
3302 TRACE("(%p %p %x): Canonicalized scheme=%s, len=%d.\n", data, uri, flags,
3303 debugstr_wn(uri->canon_uri, uri->scheme_len), data->scheme_len);
3306 /* This happens in both computation modes. */
3307 uri->canon_len += data->scheme_len + 1;
3308 uri->scheme_len = data->scheme_len;
3310 return TRUE;
3313 /* Computes what the length of the URI specified by the parse_data will be
3314 * after canonicalization occurs using the specified flags.
3316 * This function will return a non-zero value indicating the length of the canonicalized
3317 * URI, or -1 on error.
3319 static int compute_canonicalized_length(const parse_data *data, DWORD flags) {
3320 Uri uri;
3322 memset(&uri, 0, sizeof(Uri));
3324 TRACE("(%p %x): Beginning to compute canonicalized length for URI %s\n", data, flags,
3325 debugstr_w(data->uri));
3327 if(!canonicalize_scheme(data, &uri, flags, TRUE)) {
3328 ERR("(%p %x): Failed to compute URI scheme length.\n", data, flags);
3329 return -1;
3332 if(!canonicalize_hierpart(data, &uri, flags, TRUE)) {
3333 ERR("(%p %x): Failed to compute URI hierpart length.\n", data, flags);
3334 return -1;
3337 if(!canonicalize_query(data, &uri, flags, TRUE)) {
3338 ERR("(%p %x): Failed to compute query string length.\n", data, flags);
3339 return -1;
3342 if(!canonicalize_fragment(data, &uri, flags, TRUE)) {
3343 ERR("(%p %x): Failed to compute fragment length.\n", data, flags);
3344 return -1;
3347 TRACE("(%p %x): Finished computing canonicalized URI length. length=%d\n", data, flags, uri.canon_len);
3349 return uri.canon_len;
3352 /* Canonicalizes the URI data specified in the parse_data, using the given flags. If the
3353 * canonicalization succeeds it will store all the canonicalization information
3354 * in the pointer to the Uri.
3356 * To canonicalize a URI this function first computes what the length of the URI
3357 * specified by the parse_data will be. Once this is done it will then perform the actual
3358 * canonicalization of the URI.
3360 static HRESULT canonicalize_uri(const parse_data *data, Uri *uri, DWORD flags) {
3361 INT len;
3363 uri->canon_uri = NULL;
3364 uri->canon_size = uri->canon_len = 0;
3366 TRACE("(%p %p %x): beginning to canonicalize URI %s.\n", data, uri, flags, debugstr_w(data->uri));
3368 /* First try to compute the length of the URI. */
3369 len = compute_canonicalized_length(data, flags);
3370 if(len == -1) {
3371 ERR("(%p %p %x): Could not compute the canonicalized length of %s.\n", data, uri, flags,
3372 debugstr_w(data->uri));
3373 return E_INVALIDARG;
3376 uri->canon_uri = heap_alloc((len+1)*sizeof(WCHAR));
3377 if(!uri->canon_uri)
3378 return E_OUTOFMEMORY;
3380 uri->canon_size = len;
3381 if(!canonicalize_scheme(data, uri, flags, FALSE)) {
3382 ERR("(%p %p %x): Unable to canonicalize the scheme of the URI.\n", data, uri, flags);
3383 return E_INVALIDARG;
3385 uri->scheme_type = data->scheme_type;
3387 if(!canonicalize_hierpart(data, uri, flags, FALSE)) {
3388 ERR("(%p %p %x): Unable to canonicalize the heirpart of the URI\n", data, uri, flags);
3389 return E_INVALIDARG;
3392 if(!canonicalize_query(data, uri, flags, FALSE)) {
3393 ERR("(%p %p %x): Unable to canonicalize query string of the URI.\n",
3394 data, uri, flags);
3395 return E_INVALIDARG;
3398 if(!canonicalize_fragment(data, uri, flags, FALSE)) {
3399 ERR("(%p %p %x): Unable to canonicalize fragment of the URI.\n",
3400 data, uri, flags);
3401 return E_INVALIDARG;
3404 /* There's a possibility we didn't use all the space we allocated
3405 * earlier.
3407 if(uri->canon_len < uri->canon_size) {
3408 /* This happens if the URI is hierarchical and dot
3409 * segments were removed from its path.
3411 WCHAR *tmp = heap_realloc(uri->canon_uri, (uri->canon_len+1)*sizeof(WCHAR));
3412 if(!tmp)
3413 return E_OUTOFMEMORY;
3415 uri->canon_uri = tmp;
3416 uri->canon_size = uri->canon_len;
3419 uri->canon_uri[uri->canon_len] = '\0';
3420 TRACE("(%p %p %x): finished canonicalizing the URI. uri=%s\n", data, uri, flags, debugstr_w(uri->canon_uri));
3422 return S_OK;
3425 static HRESULT get_builder_component(LPWSTR *component, DWORD *component_len,
3426 LPCWSTR source, DWORD source_len,
3427 LPCWSTR *output, DWORD *output_len)
3429 if(!output_len) {
3430 if(output)
3431 *output = NULL;
3432 return E_POINTER;
3435 if(!output) {
3436 *output_len = 0;
3437 return E_POINTER;
3440 if(!(*component) && source) {
3441 /* Allocate 'component', and copy the contents from 'source'
3442 * into the new allocation.
3444 *component = heap_alloc((source_len+1)*sizeof(WCHAR));
3445 if(!(*component))
3446 return E_OUTOFMEMORY;
3448 memcpy(*component, source, source_len*sizeof(WCHAR));
3449 (*component)[source_len] = '\0';
3450 *component_len = source_len;
3453 *output = *component;
3454 *output_len = *component_len;
3455 return *output ? S_OK : S_FALSE;
3458 /* Allocates 'component' and copies the string from 'new_value' into 'component'.
3459 * If 'prefix' is set and 'new_value' isn't NULL, then it checks if 'new_value'
3460 * starts with 'prefix'. If it doesn't then 'prefix' is prepended to 'component'.
3462 * If everything is successful, then will set 'success_flag' in 'flags'.
3464 static HRESULT set_builder_component(LPWSTR *component, DWORD *component_len, LPCWSTR new_value,
3465 WCHAR prefix, DWORD *flags, DWORD success_flag)
3467 heap_free(*component);
3469 if(!new_value) {
3470 *component = NULL;
3471 *component_len = 0;
3472 } else {
3473 BOOL add_prefix = FALSE;
3474 DWORD len = lstrlenW(new_value);
3475 DWORD pos = 0;
3477 if(prefix && *new_value != prefix) {
3478 add_prefix = TRUE;
3479 *component = heap_alloc((len+2)*sizeof(WCHAR));
3480 } else
3481 *component = heap_alloc((len+1)*sizeof(WCHAR));
3483 if(!(*component))
3484 return E_OUTOFMEMORY;
3486 if(add_prefix)
3487 (*component)[pos++] = prefix;
3489 memcpy(*component+pos, new_value, (len+1)*sizeof(WCHAR));
3490 *component_len = len+pos;
3493 *flags |= success_flag;
3494 return S_OK;
3497 static void reset_builder(UriBuilder *builder) {
3498 if(builder->uri)
3499 IUri_Release(&builder->uri->IUri_iface);
3500 builder->uri = NULL;
3502 heap_free(builder->fragment);
3503 builder->fragment = NULL;
3504 builder->fragment_len = 0;
3506 heap_free(builder->host);
3507 builder->host = NULL;
3508 builder->host_len = 0;
3510 heap_free(builder->password);
3511 builder->password = NULL;
3512 builder->password_len = 0;
3514 heap_free(builder->path);
3515 builder->path = NULL;
3516 builder->path_len = 0;
3518 heap_free(builder->query);
3519 builder->query = NULL;
3520 builder->query_len = 0;
3522 heap_free(builder->scheme);
3523 builder->scheme = NULL;
3524 builder->scheme_len = 0;
3526 heap_free(builder->username);
3527 builder->username = NULL;
3528 builder->username_len = 0;
3530 builder->has_port = FALSE;
3531 builder->port = 0;
3532 builder->modified_props = 0;
3535 static HRESULT validate_scheme_name(const UriBuilder *builder, parse_data *data, DWORD flags) {
3536 const WCHAR *component;
3537 const WCHAR *ptr;
3538 const WCHAR **pptr;
3539 DWORD expected_len;
3541 if(builder->scheme) {
3542 ptr = builder->scheme;
3543 expected_len = builder->scheme_len;
3544 } else if(builder->uri && builder->uri->scheme_start > -1) {
3545 ptr = builder->uri->canon_uri+builder->uri->scheme_start;
3546 expected_len = builder->uri->scheme_len;
3547 } else {
3548 static const WCHAR nullW[] = {0};
3549 ptr = nullW;
3550 expected_len = 0;
3553 component = ptr;
3554 pptr = &ptr;
3555 if(parse_scheme(pptr, data, flags, ALLOW_NULL_TERM_SCHEME) &&
3556 data->scheme_len == expected_len) {
3557 if(data->scheme)
3558 TRACE("(%p %p %x): Found valid scheme component %s len=%d.\n", builder, data, flags,
3559 debugstr_wn(data->scheme, data->scheme_len), data->scheme_len);
3560 } else {
3561 TRACE("(%p %p %x): Invalid scheme component found %s.\n", builder, data, flags,
3562 debugstr_wn(component, expected_len));
3563 return INET_E_INVALID_URL;
3566 return S_OK;
3569 static HRESULT validate_username(const UriBuilder *builder, parse_data *data, DWORD flags) {
3570 const WCHAR *ptr;
3571 const WCHAR **pptr;
3572 DWORD expected_len;
3574 if(builder->username) {
3575 ptr = builder->username;
3576 expected_len = builder->username_len;
3577 } else if(!(builder->modified_props & Uri_HAS_USER_NAME) && builder->uri &&
3578 builder->uri->userinfo_start > -1 && builder->uri->userinfo_split != 0) {
3579 /* Just use the username from the base Uri. */
3580 data->username = builder->uri->canon_uri+builder->uri->userinfo_start;
3581 data->username_len = (builder->uri->userinfo_split > -1) ?
3582 builder->uri->userinfo_split : builder->uri->userinfo_len;
3583 ptr = NULL;
3584 } else {
3585 ptr = NULL;
3586 expected_len = 0;
3589 if(ptr) {
3590 const WCHAR *component = ptr;
3591 pptr = &ptr;
3592 if(parse_username(pptr, data, flags, ALLOW_NULL_TERM_USER_NAME) &&
3593 data->username_len == expected_len)
3594 TRACE("(%p %p %x): Found valid username component %s len=%d.\n", builder, data, flags,
3595 debugstr_wn(data->username, data->username_len), data->username_len);
3596 else {
3597 TRACE("(%p %p %x): Invalid username component found %s.\n", builder, data, flags,
3598 debugstr_wn(component, expected_len));
3599 return INET_E_INVALID_URL;
3603 return S_OK;
3606 static HRESULT validate_password(const UriBuilder *builder, parse_data *data, DWORD flags) {
3607 const WCHAR *ptr;
3608 const WCHAR **pptr;
3609 DWORD expected_len;
3611 if(builder->password) {
3612 ptr = builder->password;
3613 expected_len = builder->password_len;
3614 } else if(!(builder->modified_props & Uri_HAS_PASSWORD) && builder->uri &&
3615 builder->uri->userinfo_split > -1) {
3616 data->password = builder->uri->canon_uri+builder->uri->userinfo_start+builder->uri->userinfo_split+1;
3617 data->password_len = builder->uri->userinfo_len-builder->uri->userinfo_split-1;
3618 ptr = NULL;
3619 } else {
3620 ptr = NULL;
3621 expected_len = 0;
3624 if(ptr) {
3625 const WCHAR *component = ptr;
3626 pptr = &ptr;
3627 if(parse_password(pptr, data, flags, ALLOW_NULL_TERM_PASSWORD) &&
3628 data->password_len == expected_len)
3629 TRACE("(%p %p %x): Found valid password component %s len=%d.\n", builder, data, flags,
3630 debugstr_wn(data->password, data->password_len), data->password_len);
3631 else {
3632 TRACE("(%p %p %x): Invalid password component found %s.\n", builder, data, flags,
3633 debugstr_wn(component, expected_len));
3634 return INET_E_INVALID_URL;
3638 return S_OK;
3641 static HRESULT validate_userinfo(const UriBuilder *builder, parse_data *data, DWORD flags) {
3642 HRESULT hr;
3644 hr = validate_username(builder, data, flags);
3645 if(FAILED(hr))
3646 return hr;
3648 hr = validate_password(builder, data, flags);
3649 if(FAILED(hr))
3650 return hr;
3652 return S_OK;
3655 static HRESULT validate_host(const UriBuilder *builder, parse_data *data, DWORD flags) {
3656 const WCHAR *ptr;
3657 const WCHAR **pptr;
3658 DWORD expected_len;
3660 if(builder->host) {
3661 ptr = builder->host;
3662 expected_len = builder->host_len;
3663 } else if(!(builder->modified_props & Uri_HAS_HOST) && builder->uri && builder->uri->host_start > -1) {
3664 ptr = builder->uri->canon_uri + builder->uri->host_start;
3665 expected_len = builder->uri->host_len;
3666 } else
3667 ptr = NULL;
3669 if(ptr) {
3670 const WCHAR *component = ptr;
3671 DWORD extras = ALLOW_BRACKETLESS_IP_LITERAL|IGNORE_PORT_DELIMITER|SKIP_IP_FUTURE_CHECK;
3672 pptr = &ptr;
3674 if(parse_host(pptr, data, flags, extras) && data->host_len == expected_len)
3675 TRACE("(%p %p %x): Found valid host name %s len=%d type=%d.\n", builder, data, flags,
3676 debugstr_wn(data->host, data->host_len), data->host_len, data->host_type);
3677 else {
3678 TRACE("(%p %p %x): Invalid host name found %s.\n", builder, data, flags,
3679 debugstr_wn(component, expected_len));
3680 return INET_E_INVALID_URL;
3684 return S_OK;
3687 static void setup_port(const UriBuilder *builder, parse_data *data, DWORD flags) {
3688 if(builder->modified_props & Uri_HAS_PORT) {
3689 if(builder->has_port) {
3690 data->has_port = TRUE;
3691 data->port_value = builder->port;
3693 } else if(builder->uri && builder->uri->has_port) {
3694 data->has_port = TRUE;
3695 data->port_value = builder->uri->port;
3698 if(data->has_port)
3699 TRACE("(%p %p %x): Using %u as port for IUri.\n", builder, data, flags, data->port_value);
3702 static HRESULT validate_path(const UriBuilder *builder, parse_data *data, DWORD flags) {
3703 const WCHAR *ptr = NULL;
3704 const WCHAR *component;
3705 const WCHAR **pptr;
3706 DWORD expected_len;
3707 BOOL check_len = TRUE;
3708 BOOL valid = FALSE;
3710 if(builder->path) {
3711 ptr = builder->path;
3712 expected_len = builder->path_len;
3713 } else if(!(builder->modified_props & Uri_HAS_PATH) &&
3714 builder->uri && builder->uri->path_start > -1) {
3715 ptr = builder->uri->canon_uri+builder->uri->path_start;
3716 expected_len = builder->uri->path_len;
3717 } else {
3718 static const WCHAR nullW[] = {0};
3719 ptr = nullW;
3720 check_len = FALSE;
3721 expected_len = -1;
3724 component = ptr;
3725 pptr = &ptr;
3727 /* How the path is validated depends on what type of
3728 * URI it is.
3730 valid = data->is_opaque ?
3731 parse_path_opaque(pptr, data, flags) : parse_path_hierarchical(pptr, data, flags);
3733 if(!valid || (check_len && expected_len != data->path_len)) {
3734 TRACE("(%p %p %x): Invalid path component %s.\n", builder, data, flags,
3735 debugstr_wn(component, expected_len) );
3736 return INET_E_INVALID_URL;
3739 TRACE("(%p %p %x): Valid path component %s len=%d.\n", builder, data, flags,
3740 debugstr_wn(data->path, data->path_len), data->path_len);
3742 return S_OK;
3745 static HRESULT validate_query(const UriBuilder *builder, parse_data *data, DWORD flags) {
3746 const WCHAR *ptr = NULL;
3747 const WCHAR **pptr;
3748 DWORD expected_len;
3750 if(builder->query) {
3751 ptr = builder->query;
3752 expected_len = builder->query_len;
3753 } else if(!(builder->modified_props & Uri_HAS_QUERY) && builder->uri &&
3754 builder->uri->query_start > -1) {
3755 ptr = builder->uri->canon_uri+builder->uri->query_start;
3756 expected_len = builder->uri->query_len;
3759 if(ptr) {
3760 const WCHAR *component = ptr;
3761 pptr = &ptr;
3763 if(parse_query(pptr, data, flags) && expected_len == data->query_len)
3764 TRACE("(%p %p %x): Valid query component %s len=%d.\n", builder, data, flags,
3765 debugstr_wn(data->query, data->query_len), data->query_len);
3766 else {
3767 TRACE("(%p %p %x): Invalid query component %s.\n", builder, data, flags,
3768 debugstr_wn(component, expected_len));
3769 return INET_E_INVALID_URL;
3773 return S_OK;
3776 static HRESULT validate_fragment(const UriBuilder *builder, parse_data *data, DWORD flags) {
3777 const WCHAR *ptr = NULL;
3778 const WCHAR **pptr;
3779 DWORD expected_len;
3781 if(builder->fragment) {
3782 ptr = builder->fragment;
3783 expected_len = builder->fragment_len;
3784 } else if(!(builder->modified_props & Uri_HAS_FRAGMENT) && builder->uri &&
3785 builder->uri->fragment_start > -1) {
3786 ptr = builder->uri->canon_uri+builder->uri->fragment_start;
3787 expected_len = builder->uri->fragment_len;
3790 if(ptr) {
3791 const WCHAR *component = ptr;
3792 pptr = &ptr;
3794 if(parse_fragment(pptr, data, flags) && expected_len == data->fragment_len)
3795 TRACE("(%p %p %x): Valid fragment component %s len=%d.\n", builder, data, flags,
3796 debugstr_wn(data->fragment, data->fragment_len), data->fragment_len);
3797 else {
3798 TRACE("(%p %p %x): Invalid fragment component %s.\n", builder, data, flags,
3799 debugstr_wn(component, expected_len));
3800 return INET_E_INVALID_URL;
3804 return S_OK;
3807 static HRESULT validate_components(const UriBuilder *builder, parse_data *data, DWORD flags) {
3808 HRESULT hr;
3810 memset(data, 0, sizeof(parse_data));
3812 TRACE("(%p %p %x): Beginning to validate builder components.\n", builder, data, flags);
3814 hr = validate_scheme_name(builder, data, flags);
3815 if(FAILED(hr))
3816 return hr;
3818 /* Extra validation for file schemes. */
3819 if(data->scheme_type == URL_SCHEME_FILE) {
3820 if((builder->password || (builder->uri && builder->uri->userinfo_split > -1)) ||
3821 (builder->username || (builder->uri && builder->uri->userinfo_start > -1))) {
3822 TRACE("(%p %p %x): File schemes can't contain a username or password.\n",
3823 builder, data, flags);
3824 return INET_E_INVALID_URL;
3828 hr = validate_userinfo(builder, data, flags);
3829 if(FAILED(hr))
3830 return hr;
3832 hr = validate_host(builder, data, flags);
3833 if(FAILED(hr))
3834 return hr;
3836 setup_port(builder, data, flags);
3838 /* The URI is opaque if it doesn't have an authority component. */
3839 if(!data->is_relative)
3840 data->is_opaque = !data->username && !data->password && !data->host && !data->has_port
3841 && data->scheme_type != URL_SCHEME_FILE;
3842 else
3843 data->is_opaque = !data->host && !data->has_port;
3845 hr = validate_path(builder, data, flags);
3846 if(FAILED(hr))
3847 return hr;
3849 hr = validate_query(builder, data, flags);
3850 if(FAILED(hr))
3851 return hr;
3853 hr = validate_fragment(builder, data, flags);
3854 if(FAILED(hr))
3855 return hr;
3857 TRACE("(%p %p %x): Finished validating builder components.\n", builder, data, flags);
3859 return S_OK;
3862 static HRESULT compare_file_paths(const Uri *a, const Uri *b, BOOL *ret)
3864 WCHAR *canon_path_a, *canon_path_b;
3865 DWORD len_a, len_b;
3867 if(!a->path_len) {
3868 *ret = !b->path_len;
3869 return S_OK;
3872 if(!b->path_len) {
3873 *ret = FALSE;
3874 return S_OK;
3877 /* Fast path */
3878 if(a->path_len == b->path_len && !memicmpW(a->canon_uri+a->path_start, b->canon_uri+b->path_start, a->path_len)) {
3879 *ret = TRUE;
3880 return S_OK;
3883 len_a = canonicalize_path_hierarchical(a->canon_uri+a->path_start, a->path_len, a->scheme_type, FALSE, 0, NULL);
3884 len_b = canonicalize_path_hierarchical(b->canon_uri+b->path_start, b->path_len, b->scheme_type, FALSE, 0, NULL);
3886 canon_path_a = heap_alloc(len_a*sizeof(WCHAR));
3887 if(!canon_path_a)
3888 return E_OUTOFMEMORY;
3889 canon_path_b = heap_alloc(len_b*sizeof(WCHAR));
3890 if(!canon_path_b) {
3891 heap_free(canon_path_a);
3892 return E_OUTOFMEMORY;
3895 len_a = canonicalize_path_hierarchical(a->canon_uri+a->path_start, a->path_len, a->scheme_type, FALSE, 0, canon_path_a);
3896 len_b = canonicalize_path_hierarchical(b->canon_uri+b->path_start, b->path_len, b->scheme_type, FALSE, 0, canon_path_b);
3898 *ret = len_a == len_b && !memicmpW(canon_path_a, canon_path_b, len_a);
3900 heap_free(canon_path_a);
3901 heap_free(canon_path_b);
3902 return S_OK;
3905 /* Checks if the two Uri's are logically equivalent. It's a simple
3906 * comparison, since they are both of type Uri, and it can access
3907 * the properties of each Uri directly without the need to go
3908 * through the "IUri_Get*" interface calls.
3910 static HRESULT compare_uris(const Uri *a, const Uri *b, BOOL *ret) {
3911 const BOOL known_scheme = a->scheme_type != URL_SCHEME_UNKNOWN;
3912 const BOOL are_hierarchical = a->authority_start > -1 && b->authority_start > -1;
3913 HRESULT hres;
3915 *ret = FALSE;
3917 if(a->scheme_type != b->scheme_type)
3918 return S_OK;
3920 /* Only compare the scheme names (if any) if their unknown scheme types. */
3921 if(!known_scheme) {
3922 if((a->scheme_start > -1 && b->scheme_start > -1) &&
3923 (a->scheme_len == b->scheme_len)) {
3924 /* Make sure the schemes are the same. */
3925 if(StrCmpNW(a->canon_uri+a->scheme_start, b->canon_uri+b->scheme_start, a->scheme_len))
3926 return S_OK;
3927 } else if(a->scheme_len != b->scheme_len)
3928 /* One of the Uri's has a scheme name, while the other doesn't. */
3929 return S_OK;
3932 /* If they have a userinfo component, perform case sensitive compare. */
3933 if((a->userinfo_start > -1 && b->userinfo_start > -1) &&
3934 (a->userinfo_len == b->userinfo_len)) {
3935 if(StrCmpNW(a->canon_uri+a->userinfo_start, b->canon_uri+b->userinfo_start, a->userinfo_len))
3936 return S_OK;
3937 } else if(a->userinfo_len != b->userinfo_len)
3938 /* One of the Uri's had a userinfo, while the other one doesn't. */
3939 return S_OK;
3941 /* Check if they have a host name. */
3942 if((a->host_start > -1 && b->host_start > -1) &&
3943 (a->host_len == b->host_len)) {
3944 /* Perform a case insensitive compare if they are a known scheme type. */
3945 if(known_scheme) {
3946 if(StrCmpNIW(a->canon_uri+a->host_start, b->canon_uri+b->host_start, a->host_len))
3947 return S_OK;
3948 } else if(StrCmpNW(a->canon_uri+a->host_start, b->canon_uri+b->host_start, a->host_len))
3949 return S_OK;
3950 } else if(a->host_len != b->host_len)
3951 /* One of the Uri's had a host, while the other one didn't. */
3952 return S_OK;
3954 if(a->has_port && b->has_port) {
3955 if(a->port != b->port)
3956 return S_OK;
3957 } else if(a->has_port || b->has_port)
3958 /* One had a port, while the other one didn't. */
3959 return S_OK;
3961 /* Windows is weird with how it handles paths. For example
3962 * One URI could be "http://google.com" (after canonicalization)
3963 * and one could be "http://google.com/" and the IsEqual function
3964 * would still evaluate to TRUE, but, only if they are both hierarchical
3965 * URIs.
3967 if(a->scheme_type == URL_SCHEME_FILE) {
3968 BOOL cmp;
3970 hres = compare_file_paths(a, b, &cmp);
3971 if(FAILED(hres) || !cmp)
3972 return hres;
3973 } else if((a->path_start > -1 && b->path_start > -1) &&
3974 (a->path_len == b->path_len)) {
3975 if(StrCmpNW(a->canon_uri+a->path_start, b->canon_uri+b->path_start, a->path_len))
3976 return S_OK;
3977 } else if(are_hierarchical && a->path_len == -1 && b->path_len == 0) {
3978 if(*(a->canon_uri+a->path_start) != '/')
3979 return S_OK;
3980 } else if(are_hierarchical && b->path_len == 1 && a->path_len == 0) {
3981 if(*(b->canon_uri+b->path_start) != '/')
3982 return S_OK;
3983 } else if(a->path_len != b->path_len)
3984 return S_OK;
3986 /* Compare the query strings of the two URIs. */
3987 if((a->query_start > -1 && b->query_start > -1) &&
3988 (a->query_len == b->query_len)) {
3989 if(StrCmpNW(a->canon_uri+a->query_start, b->canon_uri+b->query_start, a->query_len))
3990 return S_OK;
3991 } else if(a->query_len != b->query_len)
3992 return S_OK;
3994 if((a->fragment_start > -1 && b->fragment_start > -1) &&
3995 (a->fragment_len == b->fragment_len)) {
3996 if(StrCmpNW(a->canon_uri+a->fragment_start, b->canon_uri+b->fragment_start, a->fragment_len))
3997 return S_OK;
3998 } else if(a->fragment_len != b->fragment_len)
3999 return S_OK;
4001 /* If we get here, the two URIs are equivalent. */
4002 *ret = TRUE;
4003 return S_OK;
4006 static void convert_to_dos_path(const WCHAR *path, DWORD path_len,
4007 WCHAR *output, DWORD *output_len)
4009 const WCHAR *ptr = path;
4011 if(path_len > 3 && *ptr == '/' && is_drive_path(path+1))
4012 /* Skip over the leading / before the drive path. */
4013 ++ptr;
4015 for(; ptr < path+path_len; ++ptr) {
4016 if(*ptr == '/') {
4017 if(output)
4018 *output++ = '\\';
4019 (*output_len)++;
4020 } else {
4021 if(output)
4022 *output++ = *ptr;
4023 (*output_len)++;
4028 /* Generates a raw uri string using the parse_data. */
4029 static DWORD generate_raw_uri(const parse_data *data, BSTR uri, DWORD flags) {
4030 DWORD length = 0;
4032 if(data->scheme) {
4033 if(uri) {
4034 memcpy(uri, data->scheme, data->scheme_len*sizeof(WCHAR));
4035 uri[data->scheme_len] = ':';
4037 length += data->scheme_len+1;
4040 if(!data->is_opaque) {
4041 /* For the "//" which appears before the authority component. */
4042 if(uri) {
4043 uri[length] = '/';
4044 uri[length+1] = '/';
4046 length += 2;
4048 /* Check if we need to add the "\\" before the host name
4049 * of a UNC server name in a DOS path.
4051 if(flags & RAW_URI_CONVERT_TO_DOS_PATH &&
4052 data->scheme_type == URL_SCHEME_FILE && data->host) {
4053 if(uri) {
4054 uri[length] = '\\';
4055 uri[length+1] = '\\';
4057 length += 2;
4061 if(data->username) {
4062 if(uri)
4063 memcpy(uri+length, data->username, data->username_len*sizeof(WCHAR));
4064 length += data->username_len;
4067 if(data->password) {
4068 if(uri) {
4069 uri[length] = ':';
4070 memcpy(uri+length+1, data->password, data->password_len*sizeof(WCHAR));
4072 length += data->password_len+1;
4075 if(data->password || data->username) {
4076 if(uri)
4077 uri[length] = '@';
4078 ++length;
4081 if(data->host) {
4082 /* IPv6 addresses get the brackets added around them if they don't already
4083 * have them.
4085 const BOOL add_brackets = data->host_type == Uri_HOST_IPV6 && *(data->host) != '[';
4086 if(add_brackets) {
4087 if(uri)
4088 uri[length] = '[';
4089 ++length;
4092 if(uri)
4093 memcpy(uri+length, data->host, data->host_len*sizeof(WCHAR));
4094 length += data->host_len;
4096 if(add_brackets) {
4097 if(uri)
4098 uri[length] = ']';
4099 length++;
4103 if(data->has_port) {
4104 /* The port isn't included in the raw uri if it's the default
4105 * port for the scheme type.
4107 DWORD i;
4108 BOOL is_default = FALSE;
4110 for(i = 0; i < sizeof(default_ports)/sizeof(default_ports[0]); ++i) {
4111 if(data->scheme_type == default_ports[i].scheme &&
4112 data->port_value == default_ports[i].port)
4113 is_default = TRUE;
4116 if(!is_default || flags & RAW_URI_FORCE_PORT_DISP) {
4117 if(uri)
4118 uri[length] = ':';
4119 ++length;
4121 if(uri)
4122 length += ui2str(uri+length, data->port_value);
4123 else
4124 length += ui2str(NULL, data->port_value);
4128 /* Check if a '/' should be added before the path for hierarchical URIs. */
4129 if(!data->is_opaque && data->path && *(data->path) != '/') {
4130 if(uri)
4131 uri[length] = '/';
4132 ++length;
4135 if(data->path) {
4136 if(!data->is_opaque && data->scheme_type == URL_SCHEME_FILE &&
4137 flags & RAW_URI_CONVERT_TO_DOS_PATH) {
4138 DWORD len = 0;
4140 if(uri)
4141 convert_to_dos_path(data->path, data->path_len, uri+length, &len);
4142 else
4143 convert_to_dos_path(data->path, data->path_len, NULL, &len);
4145 length += len;
4146 } else {
4147 if(uri)
4148 memcpy(uri+length, data->path, data->path_len*sizeof(WCHAR));
4149 length += data->path_len;
4153 if(data->query) {
4154 if(uri)
4155 memcpy(uri+length, data->query, data->query_len*sizeof(WCHAR));
4156 length += data->query_len;
4159 if(data->fragment) {
4160 if(uri)
4161 memcpy(uri+length, data->fragment, data->fragment_len*sizeof(WCHAR));
4162 length += data->fragment_len;
4165 if(uri)
4166 TRACE("(%p %p): Generated raw uri=%s len=%d\n", data, uri, debugstr_wn(uri, length), length);
4167 else
4168 TRACE("(%p %p): Computed raw uri len=%d\n", data, uri, length);
4170 return length;
4173 static HRESULT generate_uri(const UriBuilder *builder, const parse_data *data, Uri *uri, DWORD flags) {
4174 HRESULT hr;
4175 DWORD length = generate_raw_uri(data, NULL, 0);
4176 uri->raw_uri = SysAllocStringLen(NULL, length);
4177 if(!uri->raw_uri)
4178 return E_OUTOFMEMORY;
4180 generate_raw_uri(data, uri->raw_uri, 0);
4182 hr = canonicalize_uri(data, uri, flags);
4183 if(FAILED(hr)) {
4184 if(hr == E_INVALIDARG)
4185 return INET_E_INVALID_URL;
4186 return hr;
4189 uri->create_flags = flags;
4190 return S_OK;
4193 static inline Uri* impl_from_IUri(IUri *iface)
4195 return CONTAINING_RECORD(iface, Uri, IUri_iface);
4198 static inline void destory_uri_obj(Uri *This)
4200 SysFreeString(This->raw_uri);
4201 heap_free(This->canon_uri);
4202 heap_free(This);
4205 static HRESULT WINAPI Uri_QueryInterface(IUri *iface, REFIID riid, void **ppv)
4207 Uri *This = impl_from_IUri(iface);
4209 if(IsEqualGUID(&IID_IUnknown, riid)) {
4210 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
4211 *ppv = &This->IUri_iface;
4212 }else if(IsEqualGUID(&IID_IUri, riid)) {
4213 TRACE("(%p)->(IID_IUri %p)\n", This, ppv);
4214 *ppv = &This->IUri_iface;
4215 }else if(IsEqualGUID(&IID_IUriBuilderFactory, riid)) {
4216 TRACE("(%p)->(IID_IUriBuilderFactory %p)\n", This, riid);
4217 *ppv = &This->IUriBuilderFactory_iface;
4218 }else if(IsEqualGUID(&IID_IUriObj, riid)) {
4219 TRACE("(%p)->(IID_IUriObj %p)\n", This, ppv);
4220 *ppv = This;
4221 return S_OK;
4222 }else {
4223 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
4224 *ppv = NULL;
4225 return E_NOINTERFACE;
4228 IUnknown_AddRef((IUnknown*)*ppv);
4229 return S_OK;
4232 static ULONG WINAPI Uri_AddRef(IUri *iface)
4234 Uri *This = impl_from_IUri(iface);
4235 LONG ref = InterlockedIncrement(&This->ref);
4237 TRACE("(%p) ref=%d\n", This, ref);
4239 return ref;
4242 static ULONG WINAPI Uri_Release(IUri *iface)
4244 Uri *This = impl_from_IUri(iface);
4245 LONG ref = InterlockedDecrement(&This->ref);
4247 TRACE("(%p) ref=%d\n", This, ref);
4249 if(!ref)
4250 destory_uri_obj(This);
4252 return ref;
4255 static HRESULT WINAPI Uri_GetPropertyBSTR(IUri *iface, Uri_PROPERTY uriProp, BSTR *pbstrProperty, DWORD dwFlags)
4257 Uri *This = impl_from_IUri(iface);
4258 HRESULT hres;
4259 TRACE("(%p %s)->(%d %p %x)\n", This, debugstr_w(This->canon_uri), uriProp, pbstrProperty, dwFlags);
4261 if(!pbstrProperty)
4262 return E_POINTER;
4264 if(uriProp > Uri_PROPERTY_STRING_LAST) {
4265 /* Windows allocates an empty BSTR for invalid Uri_PROPERTY's. */
4266 *pbstrProperty = SysAllocStringLen(NULL, 0);
4267 if(!(*pbstrProperty))
4268 return E_OUTOFMEMORY;
4270 /* It only returns S_FALSE for the ZONE property... */
4271 if(uriProp == Uri_PROPERTY_ZONE)
4272 return S_FALSE;
4273 else
4274 return S_OK;
4277 /* Don't have support for flags yet. */
4278 if(dwFlags) {
4279 FIXME("(%p)->(%d %p %x)\n", This, uriProp, pbstrProperty, dwFlags);
4280 return E_NOTIMPL;
4283 switch(uriProp) {
4284 case Uri_PROPERTY_ABSOLUTE_URI:
4285 if(This->display_modifiers & URI_DISPLAY_NO_ABSOLUTE_URI) {
4286 *pbstrProperty = SysAllocStringLen(NULL, 0);
4287 hres = S_FALSE;
4288 } else {
4289 if(This->scheme_type != URL_SCHEME_UNKNOWN && This->userinfo_start > -1) {
4290 if(This->userinfo_len == 0) {
4291 /* Don't include the '@' after the userinfo component. */
4292 *pbstrProperty = SysAllocStringLen(NULL, This->canon_len-1);
4293 hres = S_OK;
4294 if(*pbstrProperty) {
4295 /* Copy everything before it. */
4296 memcpy(*pbstrProperty, This->canon_uri, This->userinfo_start*sizeof(WCHAR));
4298 /* And everything after it. */
4299 memcpy(*pbstrProperty+This->userinfo_start, This->canon_uri+This->userinfo_start+1,
4300 (This->canon_len-This->userinfo_start-1)*sizeof(WCHAR));
4302 } else if(This->userinfo_split == 0 && This->userinfo_len == 1) {
4303 /* Don't include the ":@" */
4304 *pbstrProperty = SysAllocStringLen(NULL, This->canon_len-2);
4305 hres = S_OK;
4306 if(*pbstrProperty) {
4307 memcpy(*pbstrProperty, This->canon_uri, This->userinfo_start*sizeof(WCHAR));
4308 memcpy(*pbstrProperty+This->userinfo_start, This->canon_uri+This->userinfo_start+2,
4309 (This->canon_len-This->userinfo_start-2)*sizeof(WCHAR));
4311 } else {
4312 *pbstrProperty = SysAllocString(This->canon_uri);
4313 hres = S_OK;
4315 } else {
4316 *pbstrProperty = SysAllocString(This->canon_uri);
4317 hres = S_OK;
4321 if(!(*pbstrProperty))
4322 hres = E_OUTOFMEMORY;
4324 break;
4325 case Uri_PROPERTY_AUTHORITY:
4326 if(This->authority_start > -1) {
4327 if(This->port_offset > -1 && is_default_port(This->scheme_type, This->port) &&
4328 This->display_modifiers & URI_DISPLAY_NO_DEFAULT_PORT_AUTH)
4329 /* Don't include the port in the authority component. */
4330 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->authority_start, This->port_offset);
4331 else
4332 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->authority_start, This->authority_len);
4333 hres = S_OK;
4334 } else {
4335 *pbstrProperty = SysAllocStringLen(NULL, 0);
4336 hres = S_FALSE;
4339 if(!(*pbstrProperty))
4340 hres = E_OUTOFMEMORY;
4342 break;
4343 case Uri_PROPERTY_DISPLAY_URI:
4344 /* The Display URI contains everything except for the userinfo for known
4345 * scheme types.
4347 if(This->scheme_type != URL_SCHEME_UNKNOWN && This->userinfo_start > -1) {
4348 *pbstrProperty = SysAllocStringLen(NULL, This->canon_len-This->userinfo_len);
4350 if(*pbstrProperty) {
4351 /* Copy everything before the userinfo over. */
4352 memcpy(*pbstrProperty, This->canon_uri, This->userinfo_start*sizeof(WCHAR));
4353 /* Copy everything after the userinfo over. */
4354 memcpy(*pbstrProperty+This->userinfo_start,
4355 This->canon_uri+This->userinfo_start+This->userinfo_len+1,
4356 (This->canon_len-(This->userinfo_start+This->userinfo_len+1))*sizeof(WCHAR));
4358 } else
4359 *pbstrProperty = SysAllocString(This->canon_uri);
4361 if(!(*pbstrProperty))
4362 hres = E_OUTOFMEMORY;
4363 else
4364 hres = S_OK;
4366 break;
4367 case Uri_PROPERTY_DOMAIN:
4368 if(This->domain_offset > -1) {
4369 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->host_start+This->domain_offset,
4370 This->host_len-This->domain_offset);
4371 hres = S_OK;
4372 } else {
4373 *pbstrProperty = SysAllocStringLen(NULL, 0);
4374 hres = S_FALSE;
4377 if(!(*pbstrProperty))
4378 hres = E_OUTOFMEMORY;
4380 break;
4381 case Uri_PROPERTY_EXTENSION:
4382 if(This->extension_offset > -1) {
4383 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->path_start+This->extension_offset,
4384 This->path_len-This->extension_offset);
4385 hres = S_OK;
4386 } else {
4387 *pbstrProperty = SysAllocStringLen(NULL, 0);
4388 hres = S_FALSE;
4391 if(!(*pbstrProperty))
4392 hres = E_OUTOFMEMORY;
4394 break;
4395 case Uri_PROPERTY_FRAGMENT:
4396 if(This->fragment_start > -1) {
4397 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->fragment_start, This->fragment_len);
4398 hres = S_OK;
4399 } else {
4400 *pbstrProperty = SysAllocStringLen(NULL, 0);
4401 hres = S_FALSE;
4404 if(!(*pbstrProperty))
4405 hres = E_OUTOFMEMORY;
4407 break;
4408 case Uri_PROPERTY_HOST:
4409 if(This->host_start > -1) {
4410 /* The '[' and ']' aren't included for IPv6 addresses. */
4411 if(This->host_type == Uri_HOST_IPV6)
4412 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->host_start+1, This->host_len-2);
4413 else
4414 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->host_start, This->host_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_PASSWORD:
4427 if(This->userinfo_split > -1) {
4428 *pbstrProperty = SysAllocStringLen(
4429 This->canon_uri+This->userinfo_start+This->userinfo_split+1,
4430 This->userinfo_len-This->userinfo_split-1);
4431 hres = S_OK;
4432 } else {
4433 *pbstrProperty = SysAllocStringLen(NULL, 0);
4434 hres = S_FALSE;
4437 if(!(*pbstrProperty))
4438 return E_OUTOFMEMORY;
4440 break;
4441 case Uri_PROPERTY_PATH:
4442 if(This->path_start > -1) {
4443 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->path_start, This->path_len);
4444 hres = S_OK;
4445 } else {
4446 *pbstrProperty = SysAllocStringLen(NULL, 0);
4447 hres = S_FALSE;
4450 if(!(*pbstrProperty))
4451 hres = E_OUTOFMEMORY;
4453 break;
4454 case Uri_PROPERTY_PATH_AND_QUERY:
4455 if(This->path_start > -1) {
4456 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->path_start, This->path_len+This->query_len);
4457 hres = S_OK;
4458 } else if(This->query_start > -1) {
4459 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->query_start, This->query_len);
4460 hres = S_OK;
4461 } else {
4462 *pbstrProperty = SysAllocStringLen(NULL, 0);
4463 hres = S_FALSE;
4466 if(!(*pbstrProperty))
4467 hres = E_OUTOFMEMORY;
4469 break;
4470 case Uri_PROPERTY_QUERY:
4471 if(This->query_start > -1) {
4472 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->query_start, This->query_len);
4473 hres = S_OK;
4474 } else {
4475 *pbstrProperty = SysAllocStringLen(NULL, 0);
4476 hres = S_FALSE;
4479 if(!(*pbstrProperty))
4480 hres = E_OUTOFMEMORY;
4482 break;
4483 case Uri_PROPERTY_RAW_URI:
4484 *pbstrProperty = SysAllocString(This->raw_uri);
4485 if(!(*pbstrProperty))
4486 hres = E_OUTOFMEMORY;
4487 else
4488 hres = S_OK;
4489 break;
4490 case Uri_PROPERTY_SCHEME_NAME:
4491 if(This->scheme_start > -1) {
4492 *pbstrProperty = SysAllocStringLen(This->canon_uri + This->scheme_start, This->scheme_len);
4493 hres = S_OK;
4494 } else {
4495 *pbstrProperty = SysAllocStringLen(NULL, 0);
4496 hres = S_FALSE;
4499 if(!(*pbstrProperty))
4500 hres = E_OUTOFMEMORY;
4502 break;
4503 case Uri_PROPERTY_USER_INFO:
4504 if(This->userinfo_start > -1) {
4505 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->userinfo_start, This->userinfo_len);
4506 hres = S_OK;
4507 } else {
4508 *pbstrProperty = SysAllocStringLen(NULL, 0);
4509 hres = S_FALSE;
4512 if(!(*pbstrProperty))
4513 hres = E_OUTOFMEMORY;
4515 break;
4516 case Uri_PROPERTY_USER_NAME:
4517 if(This->userinfo_start > -1 && This->userinfo_split != 0) {
4518 /* If userinfo_split is set, that means a password exists
4519 * so the username is only from userinfo_start to userinfo_split.
4521 if(This->userinfo_split > -1) {
4522 *pbstrProperty = SysAllocStringLen(This->canon_uri + This->userinfo_start, This->userinfo_split);
4523 hres = S_OK;
4524 } else {
4525 *pbstrProperty = SysAllocStringLen(This->canon_uri + This->userinfo_start, This->userinfo_len);
4526 hres = S_OK;
4528 } else {
4529 *pbstrProperty = SysAllocStringLen(NULL, 0);
4530 hres = S_FALSE;
4533 if(!(*pbstrProperty))
4534 return E_OUTOFMEMORY;
4536 break;
4537 default:
4538 FIXME("(%p)->(%d %p %x)\n", This, uriProp, pbstrProperty, dwFlags);
4539 hres = E_NOTIMPL;
4542 return hres;
4545 static HRESULT WINAPI Uri_GetPropertyLength(IUri *iface, Uri_PROPERTY uriProp, DWORD *pcchProperty, DWORD dwFlags)
4547 Uri *This = impl_from_IUri(iface);
4548 HRESULT hres;
4549 TRACE("(%p %s)->(%d %p %x)\n", This, debugstr_w(This->canon_uri), uriProp, pcchProperty, dwFlags);
4551 if(!pcchProperty)
4552 return E_INVALIDARG;
4554 /* Can only return a length for a property if it's a string. */
4555 if(uriProp > Uri_PROPERTY_STRING_LAST)
4556 return E_INVALIDARG;
4558 /* Don't have support for flags yet. */
4559 if(dwFlags) {
4560 FIXME("(%p)->(%d %p %x)\n", This, uriProp, pcchProperty, dwFlags);
4561 return E_NOTIMPL;
4564 switch(uriProp) {
4565 case Uri_PROPERTY_ABSOLUTE_URI:
4566 if(This->display_modifiers & URI_DISPLAY_NO_ABSOLUTE_URI) {
4567 *pcchProperty = 0;
4568 hres = S_FALSE;
4569 } else {
4570 if(This->scheme_type != URL_SCHEME_UNKNOWN) {
4571 if(This->userinfo_start > -1 && This->userinfo_len == 0)
4572 /* Don't include the '@' in the length. */
4573 *pcchProperty = This->canon_len-1;
4574 else if(This->userinfo_start > -1 && This->userinfo_len == 1 &&
4575 This->userinfo_split == 0)
4576 /* Don't include the ":@" in the length. */
4577 *pcchProperty = This->canon_len-2;
4578 else
4579 *pcchProperty = This->canon_len;
4580 } else
4581 *pcchProperty = This->canon_len;
4583 hres = S_OK;
4586 break;
4587 case Uri_PROPERTY_AUTHORITY:
4588 if(This->port_offset > -1 &&
4589 This->display_modifiers & URI_DISPLAY_NO_DEFAULT_PORT_AUTH &&
4590 is_default_port(This->scheme_type, This->port))
4591 /* Only count up until the port in the authority. */
4592 *pcchProperty = This->port_offset;
4593 else
4594 *pcchProperty = This->authority_len;
4595 hres = (This->authority_start > -1) ? S_OK : S_FALSE;
4596 break;
4597 case Uri_PROPERTY_DISPLAY_URI:
4598 if(This->scheme_type != URL_SCHEME_UNKNOWN && This->userinfo_start > -1)
4599 *pcchProperty = This->canon_len-This->userinfo_len-1;
4600 else
4601 *pcchProperty = This->canon_len;
4603 hres = S_OK;
4604 break;
4605 case Uri_PROPERTY_DOMAIN:
4606 if(This->domain_offset > -1)
4607 *pcchProperty = This->host_len - This->domain_offset;
4608 else
4609 *pcchProperty = 0;
4611 hres = (This->domain_offset > -1) ? S_OK : S_FALSE;
4612 break;
4613 case Uri_PROPERTY_EXTENSION:
4614 if(This->extension_offset > -1) {
4615 *pcchProperty = This->path_len - This->extension_offset;
4616 hres = S_OK;
4617 } else {
4618 *pcchProperty = 0;
4619 hres = S_FALSE;
4622 break;
4623 case Uri_PROPERTY_FRAGMENT:
4624 *pcchProperty = This->fragment_len;
4625 hres = (This->fragment_start > -1) ? S_OK : S_FALSE;
4626 break;
4627 case Uri_PROPERTY_HOST:
4628 *pcchProperty = This->host_len;
4630 /* '[' and ']' aren't included in the length. */
4631 if(This->host_type == Uri_HOST_IPV6)
4632 *pcchProperty -= 2;
4634 hres = (This->host_start > -1) ? S_OK : S_FALSE;
4635 break;
4636 case Uri_PROPERTY_PASSWORD:
4637 *pcchProperty = (This->userinfo_split > -1) ? This->userinfo_len-This->userinfo_split-1 : 0;
4638 hres = (This->userinfo_split > -1) ? S_OK : S_FALSE;
4639 break;
4640 case Uri_PROPERTY_PATH:
4641 *pcchProperty = This->path_len;
4642 hres = (This->path_start > -1) ? S_OK : S_FALSE;
4643 break;
4644 case Uri_PROPERTY_PATH_AND_QUERY:
4645 *pcchProperty = This->path_len+This->query_len;
4646 hres = (This->path_start > -1 || This->query_start > -1) ? S_OK : S_FALSE;
4647 break;
4648 case Uri_PROPERTY_QUERY:
4649 *pcchProperty = This->query_len;
4650 hres = (This->query_start > -1) ? S_OK : S_FALSE;
4651 break;
4652 case Uri_PROPERTY_RAW_URI:
4653 *pcchProperty = SysStringLen(This->raw_uri);
4654 hres = S_OK;
4655 break;
4656 case Uri_PROPERTY_SCHEME_NAME:
4657 *pcchProperty = This->scheme_len;
4658 hres = (This->scheme_start > -1) ? S_OK : S_FALSE;
4659 break;
4660 case Uri_PROPERTY_USER_INFO:
4661 *pcchProperty = This->userinfo_len;
4662 hres = (This->userinfo_start > -1) ? S_OK : S_FALSE;
4663 break;
4664 case Uri_PROPERTY_USER_NAME:
4665 *pcchProperty = (This->userinfo_split > -1) ? This->userinfo_split : This->userinfo_len;
4666 if(This->userinfo_split == 0)
4667 hres = S_FALSE;
4668 else
4669 hres = (This->userinfo_start > -1) ? S_OK : S_FALSE;
4670 break;
4671 default:
4672 FIXME("(%p)->(%d %p %x)\n", This, uriProp, pcchProperty, dwFlags);
4673 hres = E_NOTIMPL;
4676 return hres;
4679 static HRESULT WINAPI Uri_GetPropertyDWORD(IUri *iface, Uri_PROPERTY uriProp, DWORD *pcchProperty, DWORD dwFlags)
4681 Uri *This = impl_from_IUri(iface);
4682 HRESULT hres;
4684 TRACE("(%p %s)->(%d %p %x)\n", This, debugstr_w(This->canon_uri), uriProp, pcchProperty, dwFlags);
4686 if(!pcchProperty)
4687 return E_INVALIDARG;
4689 /* Microsoft's implementation for the ZONE property of a URI seems to be lacking...
4690 * From what I can tell, instead of checking which URLZONE the URI belongs to it
4691 * simply assigns URLZONE_INVALID and returns E_NOTIMPL. This also applies to the GetZone
4692 * function.
4694 if(uriProp == Uri_PROPERTY_ZONE) {
4695 *pcchProperty = URLZONE_INVALID;
4696 return E_NOTIMPL;
4699 if(uriProp < Uri_PROPERTY_DWORD_START) {
4700 *pcchProperty = 0;
4701 return E_INVALIDARG;
4704 switch(uriProp) {
4705 case Uri_PROPERTY_HOST_TYPE:
4706 *pcchProperty = This->host_type;
4707 hres = S_OK;
4708 break;
4709 case Uri_PROPERTY_PORT:
4710 if(!This->has_port) {
4711 *pcchProperty = 0;
4712 hres = S_FALSE;
4713 } else {
4714 *pcchProperty = This->port;
4715 hres = S_OK;
4718 break;
4719 case Uri_PROPERTY_SCHEME:
4720 *pcchProperty = This->scheme_type;
4721 hres = S_OK;
4722 break;
4723 default:
4724 FIXME("(%p)->(%d %p %x)\n", This, uriProp, pcchProperty, dwFlags);
4725 hres = E_NOTIMPL;
4728 return hres;
4731 static HRESULT WINAPI Uri_HasProperty(IUri *iface, Uri_PROPERTY uriProp, BOOL *pfHasProperty)
4733 Uri *This = impl_from_IUri(iface);
4735 TRACE("(%p %s)->(%d %p)\n", This, debugstr_w(This->canon_uri), uriProp, pfHasProperty);
4737 if(!pfHasProperty)
4738 return E_INVALIDARG;
4740 switch(uriProp) {
4741 case Uri_PROPERTY_ABSOLUTE_URI:
4742 *pfHasProperty = !(This->display_modifiers & URI_DISPLAY_NO_ABSOLUTE_URI);
4743 break;
4744 case Uri_PROPERTY_AUTHORITY:
4745 *pfHasProperty = This->authority_start > -1;
4746 break;
4747 case Uri_PROPERTY_DISPLAY_URI:
4748 *pfHasProperty = TRUE;
4749 break;
4750 case Uri_PROPERTY_DOMAIN:
4751 *pfHasProperty = This->domain_offset > -1;
4752 break;
4753 case Uri_PROPERTY_EXTENSION:
4754 *pfHasProperty = This->extension_offset > -1;
4755 break;
4756 case Uri_PROPERTY_FRAGMENT:
4757 *pfHasProperty = This->fragment_start > -1;
4758 break;
4759 case Uri_PROPERTY_HOST:
4760 *pfHasProperty = This->host_start > -1;
4761 break;
4762 case Uri_PROPERTY_PASSWORD:
4763 *pfHasProperty = This->userinfo_split > -1;
4764 break;
4765 case Uri_PROPERTY_PATH:
4766 *pfHasProperty = This->path_start > -1;
4767 break;
4768 case Uri_PROPERTY_PATH_AND_QUERY:
4769 *pfHasProperty = (This->path_start > -1 || This->query_start > -1);
4770 break;
4771 case Uri_PROPERTY_QUERY:
4772 *pfHasProperty = This->query_start > -1;
4773 break;
4774 case Uri_PROPERTY_RAW_URI:
4775 *pfHasProperty = TRUE;
4776 break;
4777 case Uri_PROPERTY_SCHEME_NAME:
4778 *pfHasProperty = This->scheme_start > -1;
4779 break;
4780 case Uri_PROPERTY_USER_INFO:
4781 *pfHasProperty = This->userinfo_start > -1;
4782 break;
4783 case Uri_PROPERTY_USER_NAME:
4784 if(This->userinfo_split == 0)
4785 *pfHasProperty = FALSE;
4786 else
4787 *pfHasProperty = This->userinfo_start > -1;
4788 break;
4789 case Uri_PROPERTY_HOST_TYPE:
4790 *pfHasProperty = TRUE;
4791 break;
4792 case Uri_PROPERTY_PORT:
4793 *pfHasProperty = This->has_port;
4794 break;
4795 case Uri_PROPERTY_SCHEME:
4796 *pfHasProperty = TRUE;
4797 break;
4798 case Uri_PROPERTY_ZONE:
4799 *pfHasProperty = FALSE;
4800 break;
4801 default:
4802 FIXME("(%p)->(%d %p): Unsupported property type.\n", This, uriProp, pfHasProperty);
4803 return E_NOTIMPL;
4806 return S_OK;
4809 static HRESULT WINAPI Uri_GetAbsoluteUri(IUri *iface, BSTR *pstrAbsoluteUri)
4811 TRACE("(%p)->(%p)\n", iface, pstrAbsoluteUri);
4812 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_ABSOLUTE_URI, pstrAbsoluteUri, 0);
4815 static HRESULT WINAPI Uri_GetAuthority(IUri *iface, BSTR *pstrAuthority)
4817 TRACE("(%p)->(%p)\n", iface, pstrAuthority);
4818 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_AUTHORITY, pstrAuthority, 0);
4821 static HRESULT WINAPI Uri_GetDisplayUri(IUri *iface, BSTR *pstrDisplayUri)
4823 TRACE("(%p)->(%p)\n", iface, pstrDisplayUri);
4824 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_DISPLAY_URI, pstrDisplayUri, 0);
4827 static HRESULT WINAPI Uri_GetDomain(IUri *iface, BSTR *pstrDomain)
4829 TRACE("(%p)->(%p)\n", iface, pstrDomain);
4830 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_DOMAIN, pstrDomain, 0);
4833 static HRESULT WINAPI Uri_GetExtension(IUri *iface, BSTR *pstrExtension)
4835 TRACE("(%p)->(%p)\n", iface, pstrExtension);
4836 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_EXTENSION, pstrExtension, 0);
4839 static HRESULT WINAPI Uri_GetFragment(IUri *iface, BSTR *pstrFragment)
4841 TRACE("(%p)->(%p)\n", iface, pstrFragment);
4842 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_FRAGMENT, pstrFragment, 0);
4845 static HRESULT WINAPI Uri_GetHost(IUri *iface, BSTR *pstrHost)
4847 TRACE("(%p)->(%p)\n", iface, pstrHost);
4848 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_HOST, pstrHost, 0);
4851 static HRESULT WINAPI Uri_GetPassword(IUri *iface, BSTR *pstrPassword)
4853 TRACE("(%p)->(%p)\n", iface, pstrPassword);
4854 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_PASSWORD, pstrPassword, 0);
4857 static HRESULT WINAPI Uri_GetPath(IUri *iface, BSTR *pstrPath)
4859 TRACE("(%p)->(%p)\n", iface, pstrPath);
4860 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_PATH, pstrPath, 0);
4863 static HRESULT WINAPI Uri_GetPathAndQuery(IUri *iface, BSTR *pstrPathAndQuery)
4865 TRACE("(%p)->(%p)\n", iface, pstrPathAndQuery);
4866 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_PATH_AND_QUERY, pstrPathAndQuery, 0);
4869 static HRESULT WINAPI Uri_GetQuery(IUri *iface, BSTR *pstrQuery)
4871 TRACE("(%p)->(%p)\n", iface, pstrQuery);
4872 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_QUERY, pstrQuery, 0);
4875 static HRESULT WINAPI Uri_GetRawUri(IUri *iface, BSTR *pstrRawUri)
4877 TRACE("(%p)->(%p)\n", iface, pstrRawUri);
4878 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_RAW_URI, pstrRawUri, 0);
4881 static HRESULT WINAPI Uri_GetSchemeName(IUri *iface, BSTR *pstrSchemeName)
4883 TRACE("(%p)->(%p)\n", iface, pstrSchemeName);
4884 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_SCHEME_NAME, pstrSchemeName, 0);
4887 static HRESULT WINAPI Uri_GetUserInfo(IUri *iface, BSTR *pstrUserInfo)
4889 TRACE("(%p)->(%p)\n", iface, pstrUserInfo);
4890 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_USER_INFO, pstrUserInfo, 0);
4893 static HRESULT WINAPI Uri_GetUserName(IUri *iface, BSTR *pstrUserName)
4895 TRACE("(%p)->(%p)\n", iface, pstrUserName);
4896 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_USER_NAME, pstrUserName, 0);
4899 static HRESULT WINAPI Uri_GetHostType(IUri *iface, DWORD *pdwHostType)
4901 TRACE("(%p)->(%p)\n", iface, pdwHostType);
4902 return IUri_GetPropertyDWORD(iface, Uri_PROPERTY_HOST_TYPE, pdwHostType, 0);
4905 static HRESULT WINAPI Uri_GetPort(IUri *iface, DWORD *pdwPort)
4907 TRACE("(%p)->(%p)\n", iface, pdwPort);
4908 return IUri_GetPropertyDWORD(iface, Uri_PROPERTY_PORT, pdwPort, 0);
4911 static HRESULT WINAPI Uri_GetScheme(IUri *iface, DWORD *pdwScheme)
4913 TRACE("(%p)->(%p)\n", iface, pdwScheme);
4914 return IUri_GetPropertyDWORD(iface, Uri_PROPERTY_SCHEME, pdwScheme, 0);
4917 static HRESULT WINAPI Uri_GetZone(IUri *iface, DWORD *pdwZone)
4919 TRACE("(%p)->(%p)\n", iface, pdwZone);
4920 return IUri_GetPropertyDWORD(iface, Uri_PROPERTY_ZONE,pdwZone, 0);
4923 static HRESULT WINAPI Uri_GetProperties(IUri *iface, DWORD *pdwProperties)
4925 Uri *This = impl_from_IUri(iface);
4926 TRACE("(%p %s)->(%p)\n", This, debugstr_w(This->canon_uri), pdwProperties);
4928 if(!pdwProperties)
4929 return E_INVALIDARG;
4931 /* All URIs have these. */
4932 *pdwProperties = Uri_HAS_DISPLAY_URI|Uri_HAS_RAW_URI|Uri_HAS_SCHEME|Uri_HAS_HOST_TYPE;
4934 if(!(This->display_modifiers & URI_DISPLAY_NO_ABSOLUTE_URI))
4935 *pdwProperties |= Uri_HAS_ABSOLUTE_URI;
4937 if(This->scheme_start > -1)
4938 *pdwProperties |= Uri_HAS_SCHEME_NAME;
4940 if(This->authority_start > -1) {
4941 *pdwProperties |= Uri_HAS_AUTHORITY;
4942 if(This->userinfo_start > -1) {
4943 *pdwProperties |= Uri_HAS_USER_INFO;
4944 if(This->userinfo_split != 0)
4945 *pdwProperties |= Uri_HAS_USER_NAME;
4947 if(This->userinfo_split > -1)
4948 *pdwProperties |= Uri_HAS_PASSWORD;
4949 if(This->host_start > -1)
4950 *pdwProperties |= Uri_HAS_HOST;
4951 if(This->domain_offset > -1)
4952 *pdwProperties |= Uri_HAS_DOMAIN;
4955 if(This->has_port)
4956 *pdwProperties |= Uri_HAS_PORT;
4957 if(This->path_start > -1)
4958 *pdwProperties |= Uri_HAS_PATH|Uri_HAS_PATH_AND_QUERY;
4959 if(This->query_start > -1)
4960 *pdwProperties |= Uri_HAS_QUERY|Uri_HAS_PATH_AND_QUERY;
4962 if(This->extension_offset > -1)
4963 *pdwProperties |= Uri_HAS_EXTENSION;
4965 if(This->fragment_start > -1)
4966 *pdwProperties |= Uri_HAS_FRAGMENT;
4968 return S_OK;
4971 static HRESULT WINAPI Uri_IsEqual(IUri *iface, IUri *pUri, BOOL *pfEqual)
4973 Uri *This = impl_from_IUri(iface);
4974 Uri *other;
4976 TRACE("(%p %s)->(%p %p)\n", This, debugstr_w(This->canon_uri), pUri, pfEqual);
4978 if(!pfEqual)
4979 return E_POINTER;
4981 if(!pUri) {
4982 *pfEqual = FALSE;
4984 /* For some reason Windows returns S_OK here... */
4985 return S_OK;
4988 /* Try to convert it to a Uri (allows for a more simple comparison). */
4989 if(!(other = get_uri_obj(pUri))) {
4990 FIXME("(%p)->(%p %p) No support for unknown IUri's yet.\n", iface, pUri, pfEqual);
4991 return E_NOTIMPL;
4994 TRACE("comparing to %s\n", debugstr_w(other->canon_uri));
4995 return compare_uris(This, other, pfEqual);
4998 static const IUriVtbl UriVtbl = {
4999 Uri_QueryInterface,
5000 Uri_AddRef,
5001 Uri_Release,
5002 Uri_GetPropertyBSTR,
5003 Uri_GetPropertyLength,
5004 Uri_GetPropertyDWORD,
5005 Uri_HasProperty,
5006 Uri_GetAbsoluteUri,
5007 Uri_GetAuthority,
5008 Uri_GetDisplayUri,
5009 Uri_GetDomain,
5010 Uri_GetExtension,
5011 Uri_GetFragment,
5012 Uri_GetHost,
5013 Uri_GetPassword,
5014 Uri_GetPath,
5015 Uri_GetPathAndQuery,
5016 Uri_GetQuery,
5017 Uri_GetRawUri,
5018 Uri_GetSchemeName,
5019 Uri_GetUserInfo,
5020 Uri_GetUserName,
5021 Uri_GetHostType,
5022 Uri_GetPort,
5023 Uri_GetScheme,
5024 Uri_GetZone,
5025 Uri_GetProperties,
5026 Uri_IsEqual
5029 static inline Uri* impl_from_IUriBuilderFactory(IUriBuilderFactory *iface)
5031 return CONTAINING_RECORD(iface, Uri, IUriBuilderFactory_iface);
5034 static HRESULT WINAPI UriBuilderFactory_QueryInterface(IUriBuilderFactory *iface, REFIID riid, void **ppv)
5036 Uri *This = impl_from_IUriBuilderFactory(iface);
5037 return IUri_QueryInterface(&This->IUri_iface, riid, ppv);
5040 static ULONG WINAPI UriBuilderFactory_AddRef(IUriBuilderFactory *iface)
5042 Uri *This = impl_from_IUriBuilderFactory(iface);
5043 return IUri_AddRef(&This->IUri_iface);
5046 static ULONG WINAPI UriBuilderFactory_Release(IUriBuilderFactory *iface)
5048 Uri *This = impl_from_IUriBuilderFactory(iface);
5049 return IUri_Release(&This->IUri_iface);
5052 static HRESULT WINAPI UriBuilderFactory_CreateIUriBuilder(IUriBuilderFactory *iface,
5053 DWORD dwFlags,
5054 DWORD_PTR dwReserved,
5055 IUriBuilder **ppIUriBuilder)
5057 Uri *This = impl_from_IUriBuilderFactory(iface);
5058 TRACE("(%p)->(%08x %08x %p)\n", This, dwFlags, (DWORD)dwReserved, ppIUriBuilder);
5060 if(!ppIUriBuilder)
5061 return E_POINTER;
5063 if(dwFlags || dwReserved) {
5064 *ppIUriBuilder = NULL;
5065 return E_INVALIDARG;
5068 return CreateIUriBuilder(NULL, 0, 0, ppIUriBuilder);
5071 static HRESULT WINAPI UriBuilderFactory_CreateInitializedIUriBuilder(IUriBuilderFactory *iface,
5072 DWORD dwFlags,
5073 DWORD_PTR dwReserved,
5074 IUriBuilder **ppIUriBuilder)
5076 Uri *This = impl_from_IUriBuilderFactory(iface);
5077 TRACE("(%p)->(%08x %08x %p)\n", This, dwFlags, (DWORD)dwReserved, ppIUriBuilder);
5079 if(!ppIUriBuilder)
5080 return E_POINTER;
5082 if(dwFlags || dwReserved) {
5083 *ppIUriBuilder = NULL;
5084 return E_INVALIDARG;
5087 return CreateIUriBuilder(&This->IUri_iface, 0, 0, ppIUriBuilder);
5090 static const IUriBuilderFactoryVtbl UriBuilderFactoryVtbl = {
5091 UriBuilderFactory_QueryInterface,
5092 UriBuilderFactory_AddRef,
5093 UriBuilderFactory_Release,
5094 UriBuilderFactory_CreateIUriBuilder,
5095 UriBuilderFactory_CreateInitializedIUriBuilder
5098 static Uri* create_uri_obj(void) {
5099 Uri *ret = heap_alloc_zero(sizeof(Uri));
5100 if(ret) {
5101 ret->IUri_iface.lpVtbl = &UriVtbl;
5102 ret->IUriBuilderFactory_iface.lpVtbl = &UriBuilderFactoryVtbl;
5103 ret->ref = 1;
5106 return ret;
5109 /***********************************************************************
5110 * CreateUri (urlmon.@)
5112 * Creates a new IUri object using the URI represented by pwzURI. This function
5113 * parses and validates the components of pwzURI and then canonicalizes the
5114 * parsed components.
5116 * PARAMS
5117 * pwzURI [I] The URI to parse, validate, and canonicalize.
5118 * dwFlags [I] Flags which can affect how the parsing/canonicalization is performed.
5119 * dwReserved [I] Reserved (not used).
5120 * ppURI [O] The resulting IUri after parsing/canonicalization occurs.
5122 * RETURNS
5123 * Success: Returns S_OK. ppURI contains the pointer to the newly allocated IUri.
5124 * Failure: E_INVALIDARG if there are invalid flag combinations in dwFlags, or an
5125 * invalid parameter, or pwzURI doesn't represent a valid URI.
5126 * E_OUTOFMEMORY if any memory allocation fails.
5128 * NOTES
5129 * Default flags:
5130 * Uri_CREATE_CANONICALIZE, Uri_CREATE_DECODE_EXTRA_INFO, Uri_CREATE_CRACK_UNKNOWN_SCHEMES,
5131 * Uri_CREATE_PRE_PROCESS_HTML_URI, Uri_CREATE_NO_IE_SETTINGS.
5133 HRESULT WINAPI CreateUri(LPCWSTR pwzURI, DWORD dwFlags, DWORD_PTR dwReserved, IUri **ppURI)
5135 const DWORD supported_flags = Uri_CREATE_ALLOW_RELATIVE|Uri_CREATE_ALLOW_IMPLICIT_WILDCARD_SCHEME|
5136 Uri_CREATE_ALLOW_IMPLICIT_FILE_SCHEME|Uri_CREATE_NO_CANONICALIZE|Uri_CREATE_CANONICALIZE|
5137 Uri_CREATE_DECODE_EXTRA_INFO|Uri_CREATE_NO_DECODE_EXTRA_INFO|Uri_CREATE_CRACK_UNKNOWN_SCHEMES|
5138 Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES|Uri_CREATE_PRE_PROCESS_HTML_URI|Uri_CREATE_NO_PRE_PROCESS_HTML_URI|
5139 Uri_CREATE_NO_IE_SETTINGS|Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS|Uri_CREATE_FILE_USE_DOS_PATH;
5140 Uri *ret;
5141 HRESULT hr;
5142 parse_data data;
5144 TRACE("(%s %x %x %p)\n", debugstr_w(pwzURI), dwFlags, (DWORD)dwReserved, ppURI);
5146 if(!ppURI)
5147 return E_INVALIDARG;
5149 if(!pwzURI) {
5150 *ppURI = NULL;
5151 return E_INVALIDARG;
5154 /* Check for invalid flags. */
5155 if(has_invalid_flag_combination(dwFlags)) {
5156 *ppURI = NULL;
5157 return E_INVALIDARG;
5160 /* Currently unsupported. */
5161 if(dwFlags & ~supported_flags)
5162 FIXME("Ignoring unsupported flag(s) %x\n", dwFlags & ~supported_flags);
5164 ret = create_uri_obj();
5165 if(!ret) {
5166 *ppURI = NULL;
5167 return E_OUTOFMEMORY;
5170 /* Explicitly set the default flags if it doesn't cause a flag conflict. */
5171 apply_default_flags(&dwFlags);
5173 /* Pre process the URI, unless told otherwise. */
5174 if(!(dwFlags & Uri_CREATE_NO_PRE_PROCESS_HTML_URI))
5175 ret->raw_uri = pre_process_uri(pwzURI);
5176 else
5177 ret->raw_uri = SysAllocString(pwzURI);
5179 if(!ret->raw_uri) {
5180 heap_free(ret);
5181 return E_OUTOFMEMORY;
5184 memset(&data, 0, sizeof(parse_data));
5185 data.uri = ret->raw_uri;
5187 /* Validate and parse the URI into its components. */
5188 if(!parse_uri(&data, dwFlags)) {
5189 /* Encountered an unsupported or invalid URI */
5190 IUri_Release(&ret->IUri_iface);
5191 *ppURI = NULL;
5192 return E_INVALIDARG;
5195 /* Canonicalize the URI. */
5196 hr = canonicalize_uri(&data, ret, dwFlags);
5197 if(FAILED(hr)) {
5198 IUri_Release(&ret->IUri_iface);
5199 *ppURI = NULL;
5200 return hr;
5203 ret->create_flags = dwFlags;
5205 *ppURI = &ret->IUri_iface;
5206 return S_OK;
5209 /***********************************************************************
5210 * CreateUriWithFragment (urlmon.@)
5212 * Creates a new IUri object. This is almost the same as CreateUri, expect that
5213 * it allows you to explicitly specify a fragment (pwzFragment) for pwzURI.
5215 * PARAMS
5216 * pwzURI [I] The URI to parse and perform canonicalization on.
5217 * pwzFragment [I] The explicit fragment string which should be added to pwzURI.
5218 * dwFlags [I] The flags which will be passed to CreateUri.
5219 * dwReserved [I] Reserved (not used).
5220 * ppURI [O] The resulting IUri after parsing/canonicalization.
5222 * RETURNS
5223 * Success: S_OK. ppURI contains the pointer to the newly allocated IUri.
5224 * Failure: E_INVALIDARG if pwzURI already contains a fragment and pwzFragment
5225 * isn't NULL. Will also return E_INVALIDARG for the same reasons as
5226 * CreateUri will. E_OUTOFMEMORY if any allocation fails.
5228 HRESULT WINAPI CreateUriWithFragment(LPCWSTR pwzURI, LPCWSTR pwzFragment, DWORD dwFlags,
5229 DWORD_PTR dwReserved, IUri **ppURI)
5231 HRESULT hres;
5232 TRACE("(%s %s %x %x %p)\n", debugstr_w(pwzURI), debugstr_w(pwzFragment), dwFlags, (DWORD)dwReserved, ppURI);
5234 if(!ppURI)
5235 return E_INVALIDARG;
5237 if(!pwzURI) {
5238 *ppURI = NULL;
5239 return E_INVALIDARG;
5242 /* Check if a fragment should be appended to the URI string. */
5243 if(pwzFragment) {
5244 WCHAR *uriW;
5245 DWORD uri_len, frag_len;
5246 BOOL add_pound;
5248 /* Check if the original URI already has a fragment component. */
5249 if(StrChrW(pwzURI, '#')) {
5250 *ppURI = NULL;
5251 return E_INVALIDARG;
5254 uri_len = lstrlenW(pwzURI);
5255 frag_len = lstrlenW(pwzFragment);
5257 /* If the fragment doesn't start with a '#', one will be added. */
5258 add_pound = *pwzFragment != '#';
5260 if(add_pound)
5261 uriW = heap_alloc((uri_len+frag_len+2)*sizeof(WCHAR));
5262 else
5263 uriW = heap_alloc((uri_len+frag_len+1)*sizeof(WCHAR));
5265 if(!uriW)
5266 return E_OUTOFMEMORY;
5268 memcpy(uriW, pwzURI, uri_len*sizeof(WCHAR));
5269 if(add_pound)
5270 uriW[uri_len++] = '#';
5271 memcpy(uriW+uri_len, pwzFragment, (frag_len+1)*sizeof(WCHAR));
5273 hres = CreateUri(uriW, dwFlags, 0, ppURI);
5275 heap_free(uriW);
5276 } else
5277 /* A fragment string wasn't specified, so just forward the call. */
5278 hres = CreateUri(pwzURI, dwFlags, 0, ppURI);
5280 return hres;
5283 static HRESULT build_uri(const UriBuilder *builder, IUri **uri, DWORD create_flags,
5284 DWORD use_orig_flags, DWORD encoding_mask)
5286 HRESULT hr;
5287 parse_data data;
5288 Uri *ret;
5290 if(!uri)
5291 return E_POINTER;
5293 if(encoding_mask && (!builder->uri || builder->modified_props)) {
5294 *uri = NULL;
5295 return E_NOTIMPL;
5298 /* Decide what flags should be used when creating the Uri. */
5299 if((use_orig_flags & UriBuilder_USE_ORIGINAL_FLAGS) && builder->uri)
5300 create_flags = builder->uri->create_flags;
5301 else {
5302 if(has_invalid_flag_combination(create_flags)) {
5303 *uri = NULL;
5304 return E_INVALIDARG;
5307 /* Set the default flags if they don't cause a conflict. */
5308 apply_default_flags(&create_flags);
5311 /* Return the base IUri if no changes have been made and the create_flags match. */
5312 if(builder->uri && !builder->modified_props && builder->uri->create_flags == create_flags) {
5313 *uri = &builder->uri->IUri_iface;
5314 IUri_AddRef(*uri);
5315 return S_OK;
5318 hr = validate_components(builder, &data, create_flags);
5319 if(FAILED(hr)) {
5320 *uri = NULL;
5321 return hr;
5324 ret = create_uri_obj();
5325 if(!ret) {
5326 *uri = NULL;
5327 return E_OUTOFMEMORY;
5330 hr = generate_uri(builder, &data, ret, create_flags);
5331 if(FAILED(hr)) {
5332 IUri_Release(&ret->IUri_iface);
5333 *uri = NULL;
5334 return hr;
5337 *uri = &ret->IUri_iface;
5338 return S_OK;
5341 static inline UriBuilder* impl_from_IUriBuilder(IUriBuilder *iface)
5343 return CONTAINING_RECORD(iface, UriBuilder, IUriBuilder_iface);
5346 static HRESULT WINAPI UriBuilder_QueryInterface(IUriBuilder *iface, REFIID riid, void **ppv)
5348 UriBuilder *This = impl_from_IUriBuilder(iface);
5350 if(IsEqualGUID(&IID_IUnknown, riid)) {
5351 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
5352 *ppv = &This->IUriBuilder_iface;
5353 }else if(IsEqualGUID(&IID_IUriBuilder, riid)) {
5354 TRACE("(%p)->(IID_IUriBuilder %p)\n", This, ppv);
5355 *ppv = &This->IUriBuilder_iface;
5356 }else {
5357 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
5358 *ppv = NULL;
5359 return E_NOINTERFACE;
5362 IUnknown_AddRef((IUnknown*)*ppv);
5363 return S_OK;
5366 static ULONG WINAPI UriBuilder_AddRef(IUriBuilder *iface)
5368 UriBuilder *This = impl_from_IUriBuilder(iface);
5369 LONG ref = InterlockedIncrement(&This->ref);
5371 TRACE("(%p) ref=%d\n", This, ref);
5373 return ref;
5376 static ULONG WINAPI UriBuilder_Release(IUriBuilder *iface)
5378 UriBuilder *This = impl_from_IUriBuilder(iface);
5379 LONG ref = InterlockedDecrement(&This->ref);
5381 TRACE("(%p) ref=%d\n", This, ref);
5383 if(!ref) {
5384 if(This->uri) IUri_Release(&This->uri->IUri_iface);
5385 heap_free(This->fragment);
5386 heap_free(This->host);
5387 heap_free(This->password);
5388 heap_free(This->path);
5389 heap_free(This->query);
5390 heap_free(This->scheme);
5391 heap_free(This->username);
5392 heap_free(This);
5395 return ref;
5398 static HRESULT WINAPI UriBuilder_CreateUriSimple(IUriBuilder *iface,
5399 DWORD dwAllowEncodingPropertyMask,
5400 DWORD_PTR dwReserved,
5401 IUri **ppIUri)
5403 UriBuilder *This = impl_from_IUriBuilder(iface);
5404 HRESULT hr;
5405 TRACE("(%p)->(%d %d %p)\n", This, dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
5407 hr = build_uri(This, ppIUri, 0, UriBuilder_USE_ORIGINAL_FLAGS, dwAllowEncodingPropertyMask);
5408 if(hr == E_NOTIMPL)
5409 FIXME("(%p)->(%d %d %p)\n", This, dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
5410 return hr;
5413 static HRESULT WINAPI UriBuilder_CreateUri(IUriBuilder *iface,
5414 DWORD dwCreateFlags,
5415 DWORD dwAllowEncodingPropertyMask,
5416 DWORD_PTR dwReserved,
5417 IUri **ppIUri)
5419 UriBuilder *This = impl_from_IUriBuilder(iface);
5420 HRESULT hr;
5421 TRACE("(%p)->(0x%08x %d %d %p)\n", This, dwCreateFlags, dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
5423 if(dwCreateFlags == -1)
5424 hr = build_uri(This, ppIUri, 0, UriBuilder_USE_ORIGINAL_FLAGS, dwAllowEncodingPropertyMask);
5425 else
5426 hr = build_uri(This, ppIUri, dwCreateFlags, 0, dwAllowEncodingPropertyMask);
5428 if(hr == E_NOTIMPL)
5429 FIXME("(%p)->(0x%08x %d %d %p)\n", This, dwCreateFlags, dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
5430 return hr;
5433 static HRESULT WINAPI UriBuilder_CreateUriWithFlags(IUriBuilder *iface,
5434 DWORD dwCreateFlags,
5435 DWORD dwUriBuilderFlags,
5436 DWORD dwAllowEncodingPropertyMask,
5437 DWORD_PTR dwReserved,
5438 IUri **ppIUri)
5440 UriBuilder *This = impl_from_IUriBuilder(iface);
5441 HRESULT hr;
5442 TRACE("(%p)->(0x%08x 0x%08x %d %d %p)\n", This, dwCreateFlags, dwUriBuilderFlags,
5443 dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
5445 hr = build_uri(This, ppIUri, dwCreateFlags, dwUriBuilderFlags, dwAllowEncodingPropertyMask);
5446 if(hr == E_NOTIMPL)
5447 FIXME("(%p)->(0x%08x 0x%08x %d %d %p)\n", This, dwCreateFlags, dwUriBuilderFlags,
5448 dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
5449 return hr;
5452 static HRESULT WINAPI UriBuilder_GetIUri(IUriBuilder *iface, IUri **ppIUri)
5454 UriBuilder *This = impl_from_IUriBuilder(iface);
5455 TRACE("(%p)->(%p)\n", This, ppIUri);
5457 if(!ppIUri)
5458 return E_POINTER;
5460 if(This->uri) {
5461 IUri *uri = &This->uri->IUri_iface;
5462 IUri_AddRef(uri);
5463 *ppIUri = uri;
5464 } else
5465 *ppIUri = NULL;
5467 return S_OK;
5470 static HRESULT WINAPI UriBuilder_SetIUri(IUriBuilder *iface, IUri *pIUri)
5472 UriBuilder *This = impl_from_IUriBuilder(iface);
5473 TRACE("(%p)->(%p)\n", This, pIUri);
5475 if(pIUri) {
5476 Uri *uri;
5478 if((uri = get_uri_obj(pIUri))) {
5479 /* Only reset the builder if it's Uri isn't the same as
5480 * the Uri passed to the function.
5482 if(This->uri != uri) {
5483 reset_builder(This);
5485 This->uri = uri;
5486 if(uri->has_port)
5487 This->port = uri->port;
5489 IUri_AddRef(pIUri);
5491 } else {
5492 FIXME("(%p)->(%p) Unknown IUri types not supported yet.\n", This, pIUri);
5493 return E_NOTIMPL;
5495 } else if(This->uri)
5496 /* Only reset the builder if it's Uri isn't NULL. */
5497 reset_builder(This);
5499 return S_OK;
5502 static HRESULT WINAPI UriBuilder_GetFragment(IUriBuilder *iface, DWORD *pcchFragment, LPCWSTR *ppwzFragment)
5504 UriBuilder *This = impl_from_IUriBuilder(iface);
5505 TRACE("(%p)->(%p %p)\n", This, pcchFragment, ppwzFragment);
5507 if(!This->uri || This->uri->fragment_start == -1 || This->modified_props & Uri_HAS_FRAGMENT)
5508 return get_builder_component(&This->fragment, &This->fragment_len, NULL, 0, ppwzFragment, pcchFragment);
5509 else
5510 return get_builder_component(&This->fragment, &This->fragment_len, This->uri->canon_uri+This->uri->fragment_start,
5511 This->uri->fragment_len, ppwzFragment, pcchFragment);
5514 static HRESULT WINAPI UriBuilder_GetHost(IUriBuilder *iface, DWORD *pcchHost, LPCWSTR *ppwzHost)
5516 UriBuilder *This = impl_from_IUriBuilder(iface);
5517 TRACE("(%p)->(%p %p)\n", This, pcchHost, ppwzHost);
5519 if(!This->uri || This->uri->host_start == -1 || This->modified_props & Uri_HAS_HOST)
5520 return get_builder_component(&This->host, &This->host_len, NULL, 0, ppwzHost, pcchHost);
5521 else {
5522 if(This->uri->host_type == Uri_HOST_IPV6)
5523 /* Don't include the '[' and ']' around the address. */
5524 return get_builder_component(&This->host, &This->host_len, This->uri->canon_uri+This->uri->host_start+1,
5525 This->uri->host_len-2, ppwzHost, pcchHost);
5526 else
5527 return get_builder_component(&This->host, &This->host_len, This->uri->canon_uri+This->uri->host_start,
5528 This->uri->host_len, ppwzHost, pcchHost);
5532 static HRESULT WINAPI UriBuilder_GetPassword(IUriBuilder *iface, DWORD *pcchPassword, LPCWSTR *ppwzPassword)
5534 UriBuilder *This = impl_from_IUriBuilder(iface);
5535 TRACE("(%p)->(%p %p)\n", This, pcchPassword, ppwzPassword);
5537 if(!This->uri || This->uri->userinfo_split == -1 || This->modified_props & Uri_HAS_PASSWORD)
5538 return get_builder_component(&This->password, &This->password_len, NULL, 0, ppwzPassword, pcchPassword);
5539 else {
5540 const WCHAR *start = This->uri->canon_uri+This->uri->userinfo_start+This->uri->userinfo_split+1;
5541 DWORD len = This->uri->userinfo_len-This->uri->userinfo_split-1;
5542 return get_builder_component(&This->password, &This->password_len, start, len, ppwzPassword, pcchPassword);
5546 static HRESULT WINAPI UriBuilder_GetPath(IUriBuilder *iface, DWORD *pcchPath, LPCWSTR *ppwzPath)
5548 UriBuilder *This = impl_from_IUriBuilder(iface);
5549 TRACE("(%p)->(%p %p)\n", This, pcchPath, ppwzPath);
5551 if(!This->uri || This->uri->path_start == -1 || This->modified_props & Uri_HAS_PATH)
5552 return get_builder_component(&This->path, &This->path_len, NULL, 0, ppwzPath, pcchPath);
5553 else
5554 return get_builder_component(&This->path, &This->path_len, This->uri->canon_uri+This->uri->path_start,
5555 This->uri->path_len, ppwzPath, pcchPath);
5558 static HRESULT WINAPI UriBuilder_GetPort(IUriBuilder *iface, BOOL *pfHasPort, DWORD *pdwPort)
5560 UriBuilder *This = impl_from_IUriBuilder(iface);
5561 TRACE("(%p)->(%p %p)\n", This, pfHasPort, pdwPort);
5563 if(!pfHasPort) {
5564 if(pdwPort)
5565 *pdwPort = 0;
5566 return E_POINTER;
5569 if(!pdwPort) {
5570 *pfHasPort = FALSE;
5571 return E_POINTER;
5574 *pfHasPort = This->has_port;
5575 *pdwPort = This->port;
5576 return S_OK;
5579 static HRESULT WINAPI UriBuilder_GetQuery(IUriBuilder *iface, DWORD *pcchQuery, LPCWSTR *ppwzQuery)
5581 UriBuilder *This = impl_from_IUriBuilder(iface);
5582 TRACE("(%p)->(%p %p)\n", This, pcchQuery, ppwzQuery);
5584 if(!This->uri || This->uri->query_start == -1 || This->modified_props & Uri_HAS_QUERY)
5585 return get_builder_component(&This->query, &This->query_len, NULL, 0, ppwzQuery, pcchQuery);
5586 else
5587 return get_builder_component(&This->query, &This->query_len, This->uri->canon_uri+This->uri->query_start,
5588 This->uri->query_len, ppwzQuery, pcchQuery);
5591 static HRESULT WINAPI UriBuilder_GetSchemeName(IUriBuilder *iface, DWORD *pcchSchemeName, LPCWSTR *ppwzSchemeName)
5593 UriBuilder *This = impl_from_IUriBuilder(iface);
5594 TRACE("(%p)->(%p %p)\n", This, pcchSchemeName, ppwzSchemeName);
5596 if(!This->uri || This->uri->scheme_start == -1 || This->modified_props & Uri_HAS_SCHEME_NAME)
5597 return get_builder_component(&This->scheme, &This->scheme_len, NULL, 0, ppwzSchemeName, pcchSchemeName);
5598 else
5599 return get_builder_component(&This->scheme, &This->scheme_len, This->uri->canon_uri+This->uri->scheme_start,
5600 This->uri->scheme_len, ppwzSchemeName, pcchSchemeName);
5603 static HRESULT WINAPI UriBuilder_GetUserName(IUriBuilder *iface, DWORD *pcchUserName, LPCWSTR *ppwzUserName)
5605 UriBuilder *This = impl_from_IUriBuilder(iface);
5606 TRACE("(%p)->(%p %p)\n", This, pcchUserName, ppwzUserName);
5608 if(!This->uri || This->uri->userinfo_start == -1 || This->uri->userinfo_split == 0 ||
5609 This->modified_props & Uri_HAS_USER_NAME)
5610 return get_builder_component(&This->username, &This->username_len, NULL, 0, ppwzUserName, pcchUserName);
5611 else {
5612 const WCHAR *start = This->uri->canon_uri+This->uri->userinfo_start;
5614 /* Check if there's a password in the userinfo section. */
5615 if(This->uri->userinfo_split > -1)
5616 /* Don't include the password. */
5617 return get_builder_component(&This->username, &This->username_len, start,
5618 This->uri->userinfo_split, ppwzUserName, pcchUserName);
5619 else
5620 return get_builder_component(&This->username, &This->username_len, start,
5621 This->uri->userinfo_len, ppwzUserName, pcchUserName);
5625 static HRESULT WINAPI UriBuilder_SetFragment(IUriBuilder *iface, LPCWSTR pwzNewValue)
5627 UriBuilder *This = impl_from_IUriBuilder(iface);
5628 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
5629 return set_builder_component(&This->fragment, &This->fragment_len, pwzNewValue, '#',
5630 &This->modified_props, Uri_HAS_FRAGMENT);
5633 static HRESULT WINAPI UriBuilder_SetHost(IUriBuilder *iface, LPCWSTR pwzNewValue)
5635 UriBuilder *This = impl_from_IUriBuilder(iface);
5636 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
5638 /* Host name can't be set to NULL. */
5639 if(!pwzNewValue)
5640 return E_INVALIDARG;
5642 return set_builder_component(&This->host, &This->host_len, pwzNewValue, 0,
5643 &This->modified_props, Uri_HAS_HOST);
5646 static HRESULT WINAPI UriBuilder_SetPassword(IUriBuilder *iface, LPCWSTR pwzNewValue)
5648 UriBuilder *This = impl_from_IUriBuilder(iface);
5649 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
5650 return set_builder_component(&This->password, &This->password_len, pwzNewValue, 0,
5651 &This->modified_props, Uri_HAS_PASSWORD);
5654 static HRESULT WINAPI UriBuilder_SetPath(IUriBuilder *iface, LPCWSTR pwzNewValue)
5656 UriBuilder *This = impl_from_IUriBuilder(iface);
5657 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
5658 return set_builder_component(&This->path, &This->path_len, pwzNewValue, 0,
5659 &This->modified_props, Uri_HAS_PATH);
5662 static HRESULT WINAPI UriBuilder_SetPort(IUriBuilder *iface, BOOL fHasPort, DWORD dwNewValue)
5664 UriBuilder *This = impl_from_IUriBuilder(iface);
5665 TRACE("(%p)->(%d %d)\n", This, fHasPort, dwNewValue);
5667 This->has_port = fHasPort;
5668 This->port = dwNewValue;
5669 This->modified_props |= Uri_HAS_PORT;
5670 return S_OK;
5673 static HRESULT WINAPI UriBuilder_SetQuery(IUriBuilder *iface, LPCWSTR pwzNewValue)
5675 UriBuilder *This = impl_from_IUriBuilder(iface);
5676 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
5677 return set_builder_component(&This->query, &This->query_len, pwzNewValue, '?',
5678 &This->modified_props, Uri_HAS_QUERY);
5681 static HRESULT WINAPI UriBuilder_SetSchemeName(IUriBuilder *iface, LPCWSTR pwzNewValue)
5683 UriBuilder *This = impl_from_IUriBuilder(iface);
5684 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
5686 /* Only set the scheme name if it's not NULL or empty. */
5687 if(!pwzNewValue || !*pwzNewValue)
5688 return E_INVALIDARG;
5690 return set_builder_component(&This->scheme, &This->scheme_len, pwzNewValue, 0,
5691 &This->modified_props, Uri_HAS_SCHEME_NAME);
5694 static HRESULT WINAPI UriBuilder_SetUserName(IUriBuilder *iface, LPCWSTR pwzNewValue)
5696 UriBuilder *This = impl_from_IUriBuilder(iface);
5697 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
5698 return set_builder_component(&This->username, &This->username_len, pwzNewValue, 0,
5699 &This->modified_props, Uri_HAS_USER_NAME);
5702 static HRESULT WINAPI UriBuilder_RemoveProperties(IUriBuilder *iface, DWORD dwPropertyMask)
5704 const DWORD accepted_flags = Uri_HAS_AUTHORITY|Uri_HAS_DOMAIN|Uri_HAS_EXTENSION|Uri_HAS_FRAGMENT|Uri_HAS_HOST|
5705 Uri_HAS_PASSWORD|Uri_HAS_PATH|Uri_HAS_PATH_AND_QUERY|Uri_HAS_QUERY|
5706 Uri_HAS_USER_INFO|Uri_HAS_USER_NAME;
5708 UriBuilder *This = impl_from_IUriBuilder(iface);
5709 TRACE("(%p)->(0x%08x)\n", This, dwPropertyMask);
5711 if(dwPropertyMask & ~accepted_flags)
5712 return E_INVALIDARG;
5714 if(dwPropertyMask & Uri_HAS_FRAGMENT)
5715 UriBuilder_SetFragment(iface, NULL);
5717 /* Even though you can't set the host name to NULL or an
5718 * empty string, you can still remove it... for some reason.
5720 if(dwPropertyMask & Uri_HAS_HOST)
5721 set_builder_component(&This->host, &This->host_len, NULL, 0,
5722 &This->modified_props, Uri_HAS_HOST);
5724 if(dwPropertyMask & Uri_HAS_PASSWORD)
5725 UriBuilder_SetPassword(iface, NULL);
5727 if(dwPropertyMask & Uri_HAS_PATH)
5728 UriBuilder_SetPath(iface, NULL);
5730 if(dwPropertyMask & Uri_HAS_PORT)
5731 UriBuilder_SetPort(iface, FALSE, 0);
5733 if(dwPropertyMask & Uri_HAS_QUERY)
5734 UriBuilder_SetQuery(iface, NULL);
5736 if(dwPropertyMask & Uri_HAS_USER_NAME)
5737 UriBuilder_SetUserName(iface, NULL);
5739 return S_OK;
5742 static HRESULT WINAPI UriBuilder_HasBeenModified(IUriBuilder *iface, BOOL *pfModified)
5744 UriBuilder *This = impl_from_IUriBuilder(iface);
5745 TRACE("(%p)->(%p)\n", This, pfModified);
5747 if(!pfModified)
5748 return E_POINTER;
5750 *pfModified = This->modified_props > 0;
5751 return S_OK;
5754 static const IUriBuilderVtbl UriBuilderVtbl = {
5755 UriBuilder_QueryInterface,
5756 UriBuilder_AddRef,
5757 UriBuilder_Release,
5758 UriBuilder_CreateUriSimple,
5759 UriBuilder_CreateUri,
5760 UriBuilder_CreateUriWithFlags,
5761 UriBuilder_GetIUri,
5762 UriBuilder_SetIUri,
5763 UriBuilder_GetFragment,
5764 UriBuilder_GetHost,
5765 UriBuilder_GetPassword,
5766 UriBuilder_GetPath,
5767 UriBuilder_GetPort,
5768 UriBuilder_GetQuery,
5769 UriBuilder_GetSchemeName,
5770 UriBuilder_GetUserName,
5771 UriBuilder_SetFragment,
5772 UriBuilder_SetHost,
5773 UriBuilder_SetPassword,
5774 UriBuilder_SetPath,
5775 UriBuilder_SetPort,
5776 UriBuilder_SetQuery,
5777 UriBuilder_SetSchemeName,
5778 UriBuilder_SetUserName,
5779 UriBuilder_RemoveProperties,
5780 UriBuilder_HasBeenModified,
5783 /***********************************************************************
5784 * CreateIUriBuilder (urlmon.@)
5786 HRESULT WINAPI CreateIUriBuilder(IUri *pIUri, DWORD dwFlags, DWORD_PTR dwReserved, IUriBuilder **ppIUriBuilder)
5788 UriBuilder *ret;
5790 TRACE("(%p %x %x %p)\n", pIUri, dwFlags, (DWORD)dwReserved, ppIUriBuilder);
5792 if(!ppIUriBuilder)
5793 return E_POINTER;
5795 ret = heap_alloc_zero(sizeof(UriBuilder));
5796 if(!ret)
5797 return E_OUTOFMEMORY;
5799 ret->IUriBuilder_iface.lpVtbl = &UriBuilderVtbl;
5800 ret->ref = 1;
5802 if(pIUri) {
5803 Uri *uri;
5805 if((uri = get_uri_obj(pIUri))) {
5806 IUri_AddRef(pIUri);
5807 ret->uri = uri;
5809 if(uri->has_port)
5810 /* Windows doesn't set 'has_port' to TRUE in this case. */
5811 ret->port = uri->port;
5813 } else {
5814 heap_free(ret);
5815 *ppIUriBuilder = NULL;
5816 FIXME("(%p %x %x %p): Unknown IUri types not supported yet.\n", pIUri, dwFlags,
5817 (DWORD)dwReserved, ppIUriBuilder);
5818 return E_NOTIMPL;
5822 *ppIUriBuilder = &ret->IUriBuilder_iface;
5823 return S_OK;
5826 /* Merges the base path with the relative path and stores the resulting path
5827 * and path len in 'result' and 'result_len'.
5829 static HRESULT merge_paths(parse_data *data, const WCHAR *base, DWORD base_len, const WCHAR *relative,
5830 DWORD relative_len, WCHAR **result, DWORD *result_len, DWORD flags)
5832 const WCHAR *end = NULL;
5833 DWORD base_copy_len = 0;
5834 WCHAR *ptr;
5836 if(base_len) {
5837 /* Find the characters that will be copied over from
5838 * the base path.
5840 end = memrchrW(base, '/', base_len);
5841 if(!end && data->scheme_type == URL_SCHEME_FILE)
5842 /* Try looking for a '\\'. */
5843 end = memrchrW(base, '\\', base_len);
5846 if(end) {
5847 base_copy_len = (end+1)-base;
5848 *result = heap_alloc((base_copy_len+relative_len+1)*sizeof(WCHAR));
5849 } else
5850 *result = heap_alloc((relative_len+1)*sizeof(WCHAR));
5852 if(!(*result)) {
5853 *result_len = 0;
5854 return E_OUTOFMEMORY;
5857 ptr = *result;
5858 if(end) {
5859 memcpy(ptr, base, base_copy_len*sizeof(WCHAR));
5860 ptr += base_copy_len;
5863 memcpy(ptr, relative, relative_len*sizeof(WCHAR));
5864 ptr += relative_len;
5865 *ptr = '\0';
5867 *result_len = (ptr-*result);
5868 return S_OK;
5871 static HRESULT combine_uri(Uri *base, Uri *relative, DWORD flags, IUri **result, DWORD extras) {
5872 Uri *ret;
5873 HRESULT hr;
5874 parse_data data;
5875 DWORD create_flags = 0, len = 0;
5877 memset(&data, 0, sizeof(parse_data));
5879 /* Base case is when the relative Uri has a scheme name,
5880 * if it does, then 'result' will contain the same data
5881 * as the relative Uri.
5883 if(relative->scheme_start > -1) {
5884 data.uri = SysAllocString(relative->raw_uri);
5885 if(!data.uri) {
5886 *result = NULL;
5887 return E_OUTOFMEMORY;
5890 parse_uri(&data, 0);
5892 ret = create_uri_obj();
5893 if(!ret) {
5894 *result = NULL;
5895 return E_OUTOFMEMORY;
5898 if(extras & COMBINE_URI_FORCE_FLAG_USE) {
5899 if(flags & URL_DONT_SIMPLIFY)
5900 create_flags |= Uri_CREATE_NO_CANONICALIZE;
5901 if(flags & URL_DONT_UNESCAPE_EXTRA_INFO)
5902 create_flags |= Uri_CREATE_NO_DECODE_EXTRA_INFO;
5905 ret->raw_uri = data.uri;
5906 hr = canonicalize_uri(&data, ret, create_flags);
5907 if(FAILED(hr)) {
5908 IUri_Release(&ret->IUri_iface);
5909 *result = NULL;
5910 return hr;
5913 apply_default_flags(&create_flags);
5914 ret->create_flags = create_flags;
5916 *result = &ret->IUri_iface;
5917 } else {
5918 WCHAR *path = NULL;
5919 DWORD raw_flags = 0;
5921 if(base->scheme_start > -1) {
5922 data.scheme = base->canon_uri+base->scheme_start;
5923 data.scheme_len = base->scheme_len;
5924 data.scheme_type = base->scheme_type;
5925 } else {
5926 data.is_relative = TRUE;
5927 data.scheme_type = URL_SCHEME_UNKNOWN;
5928 create_flags |= Uri_CREATE_ALLOW_RELATIVE;
5931 if(base->authority_start > -1) {
5932 if(base->userinfo_start > -1 && base->userinfo_split != 0) {
5933 data.username = base->canon_uri+base->userinfo_start;
5934 data.username_len = (base->userinfo_split > -1) ? base->userinfo_split : base->userinfo_len;
5937 if(base->userinfo_split > -1) {
5938 data.password = base->canon_uri+base->userinfo_start+base->userinfo_split+1;
5939 data.password_len = base->userinfo_len-base->userinfo_split-1;
5942 if(base->host_start > -1) {
5943 data.host = base->canon_uri+base->host_start;
5944 data.host_len = base->host_len;
5945 data.host_type = base->host_type;
5948 if(base->has_port) {
5949 data.has_port = TRUE;
5950 data.port_value = base->port;
5952 } else if(base->scheme_type != URL_SCHEME_FILE)
5953 data.is_opaque = TRUE;
5955 if(relative->path_start == -1 || !relative->path_len) {
5956 if(base->path_start > -1) {
5957 data.path = base->canon_uri+base->path_start;
5958 data.path_len = base->path_len;
5959 } else if((base->path_start == -1 || !base->path_len) && !data.is_opaque) {
5960 /* Just set the path as a '/' if the base didn't have
5961 * one and if it's an hierarchical URI.
5963 static const WCHAR slashW[] = {'/',0};
5964 data.path = slashW;
5965 data.path_len = 1;
5968 if(relative->query_start > -1) {
5969 data.query = relative->canon_uri+relative->query_start;
5970 data.query_len = relative->query_len;
5971 } else if(base->query_start > -1) {
5972 data.query = base->canon_uri+base->query_start;
5973 data.query_len = base->query_len;
5975 } else {
5976 const WCHAR *ptr, **pptr;
5977 DWORD path_offset = 0, path_len = 0;
5979 /* There's two possibilities on what will happen to the path component
5980 * of the result IUri. First, if the relative path begins with a '/'
5981 * then the resulting path will just be the relative path. Second, if
5982 * relative path doesn't begin with a '/' then the base path and relative
5983 * path are merged together.
5985 if(relative->path_len && *(relative->canon_uri+relative->path_start) == '/') {
5986 WCHAR *tmp = NULL;
5987 BOOL copy_drive_path = FALSE;
5989 /* If the relative IUri's path starts with a '/', then we
5990 * don't use the base IUri's path. Unless the base IUri
5991 * is a file URI, in which case it uses the drive path of
5992 * the base IUri (if it has any) in the new path.
5994 if(base->scheme_type == URL_SCHEME_FILE) {
5995 if(base->path_len > 3 && *(base->canon_uri+base->path_start) == '/' &&
5996 is_drive_path(base->canon_uri+base->path_start+1)) {
5997 path_len += 3;
5998 copy_drive_path = TRUE;
6002 path_len += relative->path_len;
6004 path = heap_alloc((path_len+1)*sizeof(WCHAR));
6005 if(!path) {
6006 *result = NULL;
6007 return E_OUTOFMEMORY;
6010 tmp = path;
6012 /* Copy the base paths, drive path over. */
6013 if(copy_drive_path) {
6014 memcpy(tmp, base->canon_uri+base->path_start, 3*sizeof(WCHAR));
6015 tmp += 3;
6018 memcpy(tmp, relative->canon_uri+relative->path_start, relative->path_len*sizeof(WCHAR));
6019 path[path_len] = '\0';
6020 } else {
6021 /* Merge the base path with the relative path. */
6022 hr = merge_paths(&data, base->canon_uri+base->path_start, base->path_len,
6023 relative->canon_uri+relative->path_start, relative->path_len,
6024 &path, &path_len, flags);
6025 if(FAILED(hr)) {
6026 *result = NULL;
6027 return hr;
6030 /* If the resulting IUri is a file URI, the drive path isn't
6031 * reduced out when the dot segments are removed.
6033 if(path_len >= 3 && data.scheme_type == URL_SCHEME_FILE && !data.host) {
6034 if(*path == '/' && is_drive_path(path+1))
6035 path_offset = 2;
6036 else if(is_drive_path(path))
6037 path_offset = 1;
6041 /* Check if the dot segments need to be removed from the path. */
6042 if(!(flags & URL_DONT_SIMPLIFY) && !data.is_opaque) {
6043 DWORD offset = (path_offset > 0) ? path_offset+1 : 0;
6044 DWORD new_len = remove_dot_segments(path+offset,path_len-offset);
6046 if(new_len != path_len) {
6047 WCHAR *tmp = heap_realloc(path, (offset+new_len+1)*sizeof(WCHAR));
6048 if(!tmp) {
6049 heap_free(path);
6050 *result = NULL;
6051 return E_OUTOFMEMORY;
6054 tmp[new_len+offset] = '\0';
6055 path = tmp;
6056 path_len = new_len+offset;
6060 if(relative->query_start > -1) {
6061 data.query = relative->canon_uri+relative->query_start;
6062 data.query_len = relative->query_len;
6065 /* Make sure the path component is valid. */
6066 ptr = path;
6067 pptr = &ptr;
6068 if((data.is_opaque && !parse_path_opaque(pptr, &data, 0)) ||
6069 (!data.is_opaque && !parse_path_hierarchical(pptr, &data, 0))) {
6070 heap_free(path);
6071 *result = NULL;
6072 return E_INVALIDARG;
6076 if(relative->fragment_start > -1) {
6077 data.fragment = relative->canon_uri+relative->fragment_start;
6078 data.fragment_len = relative->fragment_len;
6081 if(flags & URL_DONT_SIMPLIFY)
6082 raw_flags |= RAW_URI_FORCE_PORT_DISP;
6083 if(flags & URL_FILE_USE_PATHURL)
6084 raw_flags |= RAW_URI_CONVERT_TO_DOS_PATH;
6086 len = generate_raw_uri(&data, data.uri, raw_flags);
6087 data.uri = SysAllocStringLen(NULL, len);
6088 if(!data.uri) {
6089 heap_free(path);
6090 *result = NULL;
6091 return E_OUTOFMEMORY;
6094 generate_raw_uri(&data, data.uri, raw_flags);
6096 ret = create_uri_obj();
6097 if(!ret) {
6098 SysFreeString(data.uri);
6099 heap_free(path);
6100 *result = NULL;
6101 return E_OUTOFMEMORY;
6104 if(flags & URL_DONT_SIMPLIFY)
6105 create_flags |= Uri_CREATE_NO_CANONICALIZE;
6106 if(flags & URL_FILE_USE_PATHURL)
6107 create_flags |= Uri_CREATE_FILE_USE_DOS_PATH;
6109 ret->raw_uri = data.uri;
6110 hr = canonicalize_uri(&data, ret, create_flags);
6111 if(FAILED(hr)) {
6112 IUri_Release(&ret->IUri_iface);
6113 *result = NULL;
6114 return hr;
6117 if(flags & URL_DONT_SIMPLIFY)
6118 ret->display_modifiers |= URI_DISPLAY_NO_DEFAULT_PORT_AUTH;
6120 apply_default_flags(&create_flags);
6121 ret->create_flags = create_flags;
6122 *result = &ret->IUri_iface;
6124 heap_free(path);
6127 return S_OK;
6130 /***********************************************************************
6131 * CoInternetCombineIUri (urlmon.@)
6133 HRESULT WINAPI CoInternetCombineIUri(IUri *pBaseUri, IUri *pRelativeUri, DWORD dwCombineFlags,
6134 IUri **ppCombinedUri, DWORD_PTR dwReserved)
6136 HRESULT hr;
6137 IInternetProtocolInfo *info;
6138 Uri *relative, *base;
6139 TRACE("(%p %p %x %p %x)\n", pBaseUri, pRelativeUri, dwCombineFlags, ppCombinedUri, (DWORD)dwReserved);
6141 if(!ppCombinedUri)
6142 return E_INVALIDARG;
6144 if(!pBaseUri || !pRelativeUri) {
6145 *ppCombinedUri = NULL;
6146 return E_INVALIDARG;
6149 relative = get_uri_obj(pRelativeUri);
6150 base = get_uri_obj(pBaseUri);
6151 if(!relative || !base) {
6152 *ppCombinedUri = NULL;
6153 FIXME("(%p %p %x %p %x) Unknown IUri types not supported yet.\n",
6154 pBaseUri, pRelativeUri, dwCombineFlags, ppCombinedUri, (DWORD)dwReserved);
6155 return E_NOTIMPL;
6158 info = get_protocol_info(base->canon_uri);
6159 if(info) {
6160 WCHAR result[INTERNET_MAX_URL_LENGTH+1];
6161 DWORD result_len = 0;
6163 hr = IInternetProtocolInfo_CombineUrl(info, base->canon_uri, relative->canon_uri, dwCombineFlags,
6164 result, INTERNET_MAX_URL_LENGTH+1, &result_len, 0);
6165 IInternetProtocolInfo_Release(info);
6166 if(SUCCEEDED(hr)) {
6167 hr = CreateUri(result, Uri_CREATE_ALLOW_RELATIVE, 0, ppCombinedUri);
6168 if(SUCCEEDED(hr))
6169 return hr;
6173 return combine_uri(base, relative, dwCombineFlags, ppCombinedUri, 0);
6176 /***********************************************************************
6177 * CoInternetCombineUrlEx (urlmon.@)
6179 HRESULT WINAPI CoInternetCombineUrlEx(IUri *pBaseUri, LPCWSTR pwzRelativeUrl, DWORD dwCombineFlags,
6180 IUri **ppCombinedUri, DWORD_PTR dwReserved)
6182 IUri *relative;
6183 Uri *base;
6184 HRESULT hr;
6185 IInternetProtocolInfo *info;
6187 TRACE("(%p %s %x %p %x) stub\n", pBaseUri, debugstr_w(pwzRelativeUrl), dwCombineFlags,
6188 ppCombinedUri, (DWORD)dwReserved);
6190 if(!ppCombinedUri)
6191 return E_POINTER;
6193 if(!pwzRelativeUrl) {
6194 *ppCombinedUri = NULL;
6195 return E_UNEXPECTED;
6198 if(!pBaseUri) {
6199 *ppCombinedUri = NULL;
6200 return E_INVALIDARG;
6203 base = get_uri_obj(pBaseUri);
6204 if(!base) {
6205 *ppCombinedUri = NULL;
6206 FIXME("(%p %s %x %p %x) Unknown IUri's not supported yet.\n", pBaseUri, debugstr_w(pwzRelativeUrl),
6207 dwCombineFlags, ppCombinedUri, (DWORD)dwReserved);
6208 return E_NOTIMPL;
6211 info = get_protocol_info(base->canon_uri);
6212 if(info) {
6213 WCHAR result[INTERNET_MAX_URL_LENGTH+1];
6214 DWORD result_len = 0;
6216 hr = IInternetProtocolInfo_CombineUrl(info, base->canon_uri, pwzRelativeUrl, dwCombineFlags,
6217 result, INTERNET_MAX_URL_LENGTH+1, &result_len, 0);
6218 IInternetProtocolInfo_Release(info);
6219 if(SUCCEEDED(hr)) {
6220 hr = CreateUri(result, Uri_CREATE_ALLOW_RELATIVE, 0, ppCombinedUri);
6221 if(SUCCEEDED(hr))
6222 return hr;
6226 hr = CreateUri(pwzRelativeUrl, Uri_CREATE_ALLOW_RELATIVE, 0, &relative);
6227 if(FAILED(hr)) {
6228 *ppCombinedUri = NULL;
6229 return hr;
6232 hr = combine_uri(base, get_uri_obj(relative), dwCombineFlags, ppCombinedUri, COMBINE_URI_FORCE_FLAG_USE);
6234 IUri_Release(relative);
6235 return hr;
6238 static HRESULT parse_canonicalize(const Uri *uri, DWORD flags, LPWSTR output,
6239 DWORD output_len, DWORD *result_len)
6241 const WCHAR *ptr = NULL;
6242 WCHAR *path = NULL;
6243 const WCHAR **pptr;
6244 WCHAR buffer[INTERNET_MAX_URL_LENGTH+1];
6245 DWORD len = 0;
6246 BOOL reduce_path;
6248 /* URL_UNESCAPE only has effect if none of the URL_ESCAPE flags are set. */
6249 const BOOL allow_unescape = !(flags & URL_ESCAPE_UNSAFE) &&
6250 !(flags & URL_ESCAPE_SPACES_ONLY) &&
6251 !(flags & URL_ESCAPE_PERCENT);
6254 /* Check if the dot segments need to be removed from the
6255 * path component.
6257 if(uri->scheme_start > -1 && uri->path_start > -1) {
6258 ptr = uri->canon_uri+uri->scheme_start+uri->scheme_len+1;
6259 pptr = &ptr;
6261 reduce_path = !(flags & URL_NO_META) &&
6262 !(flags & URL_DONT_SIMPLIFY) &&
6263 ptr && check_hierarchical(pptr);
6265 for(ptr = uri->canon_uri; ptr < uri->canon_uri+uri->canon_len; ++ptr) {
6266 BOOL do_default_action = TRUE;
6268 /* Keep track of the path if we need to remove dot segments from
6269 * it later.
6271 if(reduce_path && !path && ptr == uri->canon_uri+uri->path_start)
6272 path = buffer+len;
6274 /* Check if it's time to reduce the path. */
6275 if(reduce_path && ptr == uri->canon_uri+uri->path_start+uri->path_len) {
6276 DWORD current_path_len = (buffer+len) - path;
6277 DWORD new_path_len = remove_dot_segments(path, current_path_len);
6279 /* Update the current length. */
6280 len -= (current_path_len-new_path_len);
6281 reduce_path = FALSE;
6284 if(*ptr == '%') {
6285 const WCHAR decoded = decode_pct_val(ptr);
6286 if(decoded) {
6287 if(allow_unescape && (flags & URL_UNESCAPE)) {
6288 buffer[len++] = decoded;
6289 ptr += 2;
6290 do_default_action = FALSE;
6294 /* See if %'s needed to encoded. */
6295 if(do_default_action && (flags & URL_ESCAPE_PERCENT)) {
6296 pct_encode_val(*ptr, buffer+len);
6297 len += 3;
6298 do_default_action = FALSE;
6300 } else if(*ptr == ' ') {
6301 if((flags & URL_ESCAPE_SPACES_ONLY) &&
6302 !(flags & URL_ESCAPE_UNSAFE)) {
6303 pct_encode_val(*ptr, buffer+len);
6304 len += 3;
6305 do_default_action = FALSE;
6307 } else if(!is_reserved(*ptr) && !is_unreserved(*ptr)) {
6308 if(flags & URL_ESCAPE_UNSAFE) {
6309 pct_encode_val(*ptr, buffer+len);
6310 len += 3;
6311 do_default_action = FALSE;
6315 if(do_default_action)
6316 buffer[len++] = *ptr;
6319 /* Sometimes the path is the very last component of the IUri, so
6320 * see if the dot segments need to be reduced now.
6322 if(reduce_path && path) {
6323 DWORD current_path_len = (buffer+len) - path;
6324 DWORD new_path_len = remove_dot_segments(path, current_path_len);
6326 /* Update the current length. */
6327 len -= (current_path_len-new_path_len);
6330 buffer[len++] = 0;
6332 /* The null terminator isn't included in the length. */
6333 *result_len = len-1;
6334 if(len > output_len)
6335 return STRSAFE_E_INSUFFICIENT_BUFFER;
6336 else
6337 memcpy(output, buffer, len*sizeof(WCHAR));
6339 return S_OK;
6342 static HRESULT parse_friendly(IUri *uri, LPWSTR output, DWORD output_len,
6343 DWORD *result_len)
6345 HRESULT hr;
6346 DWORD display_len;
6347 BSTR display;
6349 hr = IUri_GetPropertyLength(uri, Uri_PROPERTY_DISPLAY_URI, &display_len, 0);
6350 if(FAILED(hr)) {
6351 *result_len = 0;
6352 return hr;
6355 *result_len = display_len;
6356 if(display_len+1 > output_len)
6357 return STRSAFE_E_INSUFFICIENT_BUFFER;
6359 hr = IUri_GetDisplayUri(uri, &display);
6360 if(FAILED(hr)) {
6361 *result_len = 0;
6362 return hr;
6365 memcpy(output, display, (display_len+1)*sizeof(WCHAR));
6366 SysFreeString(display);
6367 return S_OK;
6370 static HRESULT parse_rootdocument(const Uri *uri, LPWSTR output, DWORD output_len,
6371 DWORD *result_len)
6373 static const WCHAR colon_slashesW[] = {':','/','/'};
6375 WCHAR *ptr;
6376 DWORD len = 0;
6378 /* Windows only returns the root document if the URI has an authority
6379 * and it's not an unknown scheme type or a file scheme type.
6381 if(uri->authority_start == -1 ||
6382 uri->scheme_type == URL_SCHEME_UNKNOWN ||
6383 uri->scheme_type == URL_SCHEME_FILE) {
6384 *result_len = 0;
6385 if(!output_len)
6386 return STRSAFE_E_INSUFFICIENT_BUFFER;
6388 output[0] = 0;
6389 return S_OK;
6392 len = uri->scheme_len+uri->authority_len;
6393 /* For the "://" and '/' which will be added. */
6394 len += 4;
6396 if(len+1 > output_len) {
6397 *result_len = len;
6398 return STRSAFE_E_INSUFFICIENT_BUFFER;
6401 ptr = output;
6402 memcpy(ptr, uri->canon_uri+uri->scheme_start, uri->scheme_len*sizeof(WCHAR));
6404 /* Add the "://". */
6405 ptr += uri->scheme_len;
6406 memcpy(ptr, colon_slashesW, sizeof(colon_slashesW));
6408 /* Add the authority. */
6409 ptr += sizeof(colon_slashesW)/sizeof(WCHAR);
6410 memcpy(ptr, uri->canon_uri+uri->authority_start, uri->authority_len*sizeof(WCHAR));
6412 /* Add the '/' after the authority. */
6413 ptr += uri->authority_len;
6414 *ptr = '/';
6415 ptr[1] = 0;
6417 *result_len = len;
6418 return S_OK;
6421 static HRESULT parse_document(const Uri *uri, LPWSTR output, DWORD output_len,
6422 DWORD *result_len)
6424 DWORD len = 0;
6426 /* It has to be a known scheme type, but, it can't be a file
6427 * scheme. It also has to hierarchical.
6429 if(uri->scheme_type == URL_SCHEME_UNKNOWN ||
6430 uri->scheme_type == URL_SCHEME_FILE ||
6431 uri->authority_start == -1) {
6432 *result_len = 0;
6433 if(output_len < 1)
6434 return STRSAFE_E_INSUFFICIENT_BUFFER;
6436 output[0] = 0;
6437 return S_OK;
6440 if(uri->fragment_start > -1)
6441 len = uri->fragment_start;
6442 else
6443 len = uri->canon_len;
6445 *result_len = len;
6446 if(len+1 > output_len)
6447 return STRSAFE_E_INSUFFICIENT_BUFFER;
6449 memcpy(output, uri->canon_uri, len*sizeof(WCHAR));
6450 output[len] = 0;
6451 return S_OK;
6454 static HRESULT parse_path_from_url(const Uri *uri, LPWSTR output, DWORD output_len,
6455 DWORD *result_len)
6457 const WCHAR *path_ptr;
6458 WCHAR buffer[INTERNET_MAX_URL_LENGTH+1];
6459 WCHAR *ptr;
6461 if(uri->scheme_type != URL_SCHEME_FILE) {
6462 *result_len = 0;
6463 if(output_len > 0)
6464 output[0] = 0;
6465 return E_INVALIDARG;
6468 ptr = buffer;
6469 if(uri->host_start > -1) {
6470 static const WCHAR slash_slashW[] = {'\\','\\'};
6472 memcpy(ptr, slash_slashW, sizeof(slash_slashW));
6473 ptr += sizeof(slash_slashW)/sizeof(WCHAR);
6474 memcpy(ptr, uri->canon_uri+uri->host_start, uri->host_len*sizeof(WCHAR));
6475 ptr += uri->host_len;
6478 path_ptr = uri->canon_uri+uri->path_start;
6479 if(uri->path_len > 3 && *path_ptr == '/' && is_drive_path(path_ptr+1))
6480 /* Skip past the '/' in front of the drive path. */
6481 ++path_ptr;
6483 for(; path_ptr < uri->canon_uri+uri->path_start+uri->path_len; ++path_ptr, ++ptr) {
6484 BOOL do_default_action = TRUE;
6486 if(*path_ptr == '%') {
6487 const WCHAR decoded = decode_pct_val(path_ptr);
6488 if(decoded) {
6489 *ptr = decoded;
6490 path_ptr += 2;
6491 do_default_action = FALSE;
6493 } else if(*path_ptr == '/') {
6494 *ptr = '\\';
6495 do_default_action = FALSE;
6498 if(do_default_action)
6499 *ptr = *path_ptr;
6502 *ptr = 0;
6504 *result_len = ptr-buffer;
6505 if(*result_len+1 > output_len)
6506 return STRSAFE_E_INSUFFICIENT_BUFFER;
6508 memcpy(output, buffer, (*result_len+1)*sizeof(WCHAR));
6509 return S_OK;
6512 static HRESULT parse_url_from_path(IUri *uri, LPWSTR output, DWORD output_len,
6513 DWORD *result_len)
6515 HRESULT hr;
6516 BSTR received;
6517 DWORD len = 0;
6519 hr = IUri_GetPropertyLength(uri, Uri_PROPERTY_ABSOLUTE_URI, &len, 0);
6520 if(FAILED(hr)) {
6521 *result_len = 0;
6522 return hr;
6525 *result_len = len;
6526 if(len+1 > output_len)
6527 return STRSAFE_E_INSUFFICIENT_BUFFER;
6529 hr = IUri_GetAbsoluteUri(uri, &received);
6530 if(FAILED(hr)) {
6531 *result_len = 0;
6532 return hr;
6535 memcpy(output, received, (len+1)*sizeof(WCHAR));
6536 SysFreeString(received);
6538 return S_OK;
6541 static HRESULT parse_schema(IUri *uri, LPWSTR output, DWORD output_len,
6542 DWORD *result_len)
6544 HRESULT hr;
6545 DWORD len;
6546 BSTR received;
6548 hr = IUri_GetPropertyLength(uri, Uri_PROPERTY_SCHEME_NAME, &len, 0);
6549 if(FAILED(hr)) {
6550 *result_len = 0;
6551 return hr;
6554 *result_len = len;
6555 if(len+1 > output_len)
6556 return STRSAFE_E_INSUFFICIENT_BUFFER;
6558 hr = IUri_GetSchemeName(uri, &received);
6559 if(FAILED(hr)) {
6560 *result_len = 0;
6561 return hr;
6564 memcpy(output, received, (len+1)*sizeof(WCHAR));
6565 SysFreeString(received);
6567 return S_OK;
6570 static HRESULT parse_site(IUri *uri, LPWSTR output, DWORD output_len, DWORD *result_len)
6572 HRESULT hr;
6573 DWORD len;
6574 BSTR received;
6576 hr = IUri_GetPropertyLength(uri, Uri_PROPERTY_HOST, &len, 0);
6577 if(FAILED(hr)) {
6578 *result_len = 0;
6579 return hr;
6582 *result_len = len;
6583 if(len+1 > output_len)
6584 return STRSAFE_E_INSUFFICIENT_BUFFER;
6586 hr = IUri_GetHost(uri, &received);
6587 if(FAILED(hr)) {
6588 *result_len = 0;
6589 return hr;
6592 memcpy(output, received, (len+1)*sizeof(WCHAR));
6593 SysFreeString(received);
6595 return S_OK;
6598 static HRESULT parse_domain(IUri *uri, LPWSTR output, DWORD output_len, DWORD *result_len)
6600 HRESULT hr;
6601 DWORD len;
6602 BSTR received;
6604 hr = IUri_GetPropertyLength(uri, Uri_PROPERTY_DOMAIN, &len, 0);
6605 if(FAILED(hr)) {
6606 *result_len = 0;
6607 return hr;
6610 *result_len = len;
6611 if(len+1 > output_len)
6612 return STRSAFE_E_INSUFFICIENT_BUFFER;
6614 hr = IUri_GetDomain(uri, &received);
6615 if(FAILED(hr)) {
6616 *result_len = 0;
6617 return hr;
6620 memcpy(output, received, (len+1)*sizeof(WCHAR));
6621 SysFreeString(received);
6623 return S_OK;
6626 static HRESULT parse_anchor(IUri *uri, LPWSTR output, DWORD output_len, DWORD *result_len)
6628 HRESULT hr;
6629 DWORD len;
6630 BSTR received;
6632 hr = IUri_GetPropertyLength(uri, Uri_PROPERTY_FRAGMENT, &len, 0);
6633 if(FAILED(hr)) {
6634 *result_len = 0;
6635 return hr;
6638 *result_len = len;
6639 if(len+1 > output_len)
6640 return STRSAFE_E_INSUFFICIENT_BUFFER;
6642 hr = IUri_GetFragment(uri, &received);
6643 if(FAILED(hr)) {
6644 *result_len = 0;
6645 return hr;
6648 memcpy(output, received, (len+1)*sizeof(WCHAR));
6649 SysFreeString(received);
6651 return S_OK;
6654 /***********************************************************************
6655 * CoInternetParseIUri (urlmon.@)
6657 HRESULT WINAPI CoInternetParseIUri(IUri *pIUri, PARSEACTION ParseAction, DWORD dwFlags,
6658 LPWSTR pwzResult, DWORD cchResult, DWORD *pcchResult,
6659 DWORD_PTR dwReserved)
6661 HRESULT hr;
6662 Uri *uri;
6663 IInternetProtocolInfo *info;
6665 TRACE("(%p %d %x %p %d %p %x)\n", pIUri, ParseAction, dwFlags, pwzResult,
6666 cchResult, pcchResult, (DWORD)dwReserved);
6668 if(!pcchResult)
6669 return E_POINTER;
6671 if(!pwzResult || !pIUri) {
6672 *pcchResult = 0;
6673 return E_INVALIDARG;
6676 if(!(uri = get_uri_obj(pIUri))) {
6677 *pcchResult = 0;
6678 FIXME("(%p %d %x %p %d %p %x) Unknown IUri's not supported for this action.\n",
6679 pIUri, ParseAction, dwFlags, pwzResult, cchResult, pcchResult, (DWORD)dwReserved);
6680 return E_NOTIMPL;
6683 info = get_protocol_info(uri->canon_uri);
6684 if(info) {
6685 hr = IInternetProtocolInfo_ParseUrl(info, uri->canon_uri, ParseAction, dwFlags,
6686 pwzResult, cchResult, pcchResult, 0);
6687 IInternetProtocolInfo_Release(info);
6688 if(SUCCEEDED(hr)) return hr;
6691 switch(ParseAction) {
6692 case PARSE_CANONICALIZE:
6693 hr = parse_canonicalize(uri, dwFlags, pwzResult, cchResult, pcchResult);
6694 break;
6695 case PARSE_FRIENDLY:
6696 hr = parse_friendly(pIUri, pwzResult, cchResult, pcchResult);
6697 break;
6698 case PARSE_ROOTDOCUMENT:
6699 hr = parse_rootdocument(uri, pwzResult, cchResult, pcchResult);
6700 break;
6701 case PARSE_DOCUMENT:
6702 hr = parse_document(uri, pwzResult, cchResult, pcchResult);
6703 break;
6704 case PARSE_PATH_FROM_URL:
6705 hr = parse_path_from_url(uri, pwzResult, cchResult, pcchResult);
6706 break;
6707 case PARSE_URL_FROM_PATH:
6708 hr = parse_url_from_path(pIUri, pwzResult, cchResult, pcchResult);
6709 break;
6710 case PARSE_SCHEMA:
6711 hr = parse_schema(pIUri, pwzResult, cchResult, pcchResult);
6712 break;
6713 case PARSE_SITE:
6714 hr = parse_site(pIUri, pwzResult, cchResult, pcchResult);
6715 break;
6716 case PARSE_DOMAIN:
6717 hr = parse_domain(pIUri, pwzResult, cchResult, pcchResult);
6718 break;
6719 case PARSE_LOCATION:
6720 case PARSE_ANCHOR:
6721 hr = parse_anchor(pIUri, pwzResult, cchResult, pcchResult);
6722 break;
6723 case PARSE_SECURITY_URL:
6724 case PARSE_MIME:
6725 case PARSE_SERVER:
6726 case PARSE_SECURITY_DOMAIN:
6727 *pcchResult = 0;
6728 hr = E_FAIL;
6729 break;
6730 default:
6731 *pcchResult = 0;
6732 hr = E_NOTIMPL;
6733 FIXME("(%p %d %x %p %d %p %x) Partial stub.\n", pIUri, ParseAction, dwFlags,
6734 pwzResult, cchResult, pcchResult, (DWORD)dwReserved);
6737 return hr;