iphlpapi: Implement IcmpSendEcho and IcmpSendEcho2 via IcmpSendEcho2Ex.
[wine.git] / dlls / urlmon / uri.c
blobcf901dfd8b7e6a04d9bb3fa4ea34b8026d7ff07f
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 <limits.h>
21 #include <wchar.h>
23 #include "urlmon_main.h"
24 #include "wine/debug.h"
26 #define NO_SHLWAPI_REG
27 #include "shlwapi.h"
29 #include "strsafe.h"
30 #include "winternl.h"
31 #include "inaddr.h"
32 #include "in6addr.h"
33 #include "ip2string.h"
35 #define URI_DISPLAY_NO_ABSOLUTE_URI 0x1
36 #define URI_DISPLAY_NO_DEFAULT_PORT_AUTH 0x2
38 #define ALLOW_NULL_TERM_SCHEME 0x01
39 #define ALLOW_NULL_TERM_USER_NAME 0x02
40 #define ALLOW_NULL_TERM_PASSWORD 0x04
41 #define ALLOW_BRACKETLESS_IP_LITERAL 0x08
42 #define SKIP_IP_FUTURE_CHECK 0x10
43 #define IGNORE_PORT_DELIMITER 0x20
45 #define RAW_URI_FORCE_PORT_DISP 0x1
46 #define RAW_URI_CONVERT_TO_DOS_PATH 0x2
48 #define COMBINE_URI_FORCE_FLAG_USE 0x1
50 WINE_DEFAULT_DEBUG_CHANNEL(urlmon);
52 static const IID IID_IUriObj = {0x4b364760,0x9f51,0x11df,{0x98,0x1c,0x08,0x00,0x20,0x0c,0x9a,0x66}};
54 typedef struct {
55 IUri IUri_iface;
56 IUriBuilderFactory IUriBuilderFactory_iface;
57 IPersistStream IPersistStream_iface;
58 IMarshal IMarshal_iface;
60 LONG ref;
62 BSTR raw_uri;
64 /* Information about the canonicalized URI's buffer. */
65 WCHAR *canon_uri;
66 DWORD canon_size;
67 DWORD canon_len;
68 BOOL display_modifiers;
69 DWORD create_flags;
71 INT scheme_start;
72 DWORD scheme_len;
73 URL_SCHEME scheme_type;
75 INT userinfo_start;
76 DWORD userinfo_len;
77 INT userinfo_split;
79 INT host_start;
80 DWORD host_len;
81 Uri_HOST_TYPE host_type;
83 INT port_offset;
84 DWORD port;
85 BOOL has_port;
87 INT authority_start;
88 DWORD authority_len;
90 INT domain_offset;
92 INT path_start;
93 DWORD path_len;
94 INT extension_offset;
96 INT query_start;
97 DWORD query_len;
99 INT fragment_start;
100 DWORD fragment_len;
101 } Uri;
103 typedef struct {
104 IUriBuilder IUriBuilder_iface;
105 LONG ref;
107 Uri *uri;
108 DWORD modified_props;
110 WCHAR *fragment;
111 DWORD fragment_len;
113 WCHAR *host;
114 DWORD host_len;
116 WCHAR *password;
117 DWORD password_len;
119 WCHAR *path;
120 DWORD path_len;
122 BOOL has_port;
123 DWORD port;
125 WCHAR *query;
126 DWORD query_len;
128 WCHAR *scheme;
129 DWORD scheme_len;
131 WCHAR *username;
132 DWORD username_len;
133 } UriBuilder;
135 typedef struct {
136 BSTR uri;
138 BOOL is_relative;
139 BOOL is_opaque;
140 BOOL has_implicit_scheme;
141 BOOL has_implicit_ip;
142 UINT implicit_ipv4;
143 BOOL must_have_path;
145 const WCHAR *scheme;
146 DWORD scheme_len;
147 URL_SCHEME scheme_type;
149 const WCHAR *username;
150 DWORD username_len;
152 const WCHAR *password;
153 DWORD password_len;
155 const WCHAR *host;
156 DWORD host_len;
157 Uri_HOST_TYPE host_type;
159 IN6_ADDR ipv6_address;
161 BOOL has_port;
162 const WCHAR *port;
163 DWORD port_len;
164 DWORD port_value;
166 const WCHAR *path;
167 DWORD path_len;
169 const WCHAR *query;
170 DWORD query_len;
172 const WCHAR *fragment;
173 DWORD fragment_len;
174 } parse_data;
176 static const CHAR hexDigits[] = "0123456789ABCDEF";
178 /* List of scheme types/scheme names that are recognized by the IUri interface as of IE 7. */
179 static const struct {
180 URL_SCHEME scheme;
181 WCHAR scheme_name[16];
182 } recognized_schemes[] = {
183 {URL_SCHEME_FTP, L"ftp"},
184 {URL_SCHEME_HTTP, L"http"},
185 {URL_SCHEME_GOPHER, L"gopher"},
186 {URL_SCHEME_MAILTO, L"mailto"},
187 {URL_SCHEME_NEWS, L"news"},
188 {URL_SCHEME_NNTP, L"nntp"},
189 {URL_SCHEME_TELNET, L"telnet"},
190 {URL_SCHEME_WAIS, L"wais"},
191 {URL_SCHEME_FILE, L"file"},
192 {URL_SCHEME_MK, L"mk"},
193 {URL_SCHEME_HTTPS, L"https"},
194 {URL_SCHEME_SHELL, L"shell"},
195 {URL_SCHEME_SNEWS, L"snews"},
196 {URL_SCHEME_LOCAL, L"local"},
197 {URL_SCHEME_JAVASCRIPT, L"javascript"},
198 {URL_SCHEME_VBSCRIPT, L"vbscript"},
199 {URL_SCHEME_ABOUT, L"about"},
200 {URL_SCHEME_RES, L"res"},
201 {URL_SCHEME_MSSHELLROOTED, L"ms-shell-rooted"},
202 {URL_SCHEME_MSSHELLIDLIST, L"ms-shell-idlist"},
203 {URL_SCHEME_MSHELP, L"hcp"},
204 {URL_SCHEME_WILDCARD, L"*"}
207 /* List of default ports Windows recognizes. */
208 static const struct {
209 URL_SCHEME scheme;
210 USHORT port;
211 } default_ports[] = {
212 {URL_SCHEME_FTP, 21},
213 {URL_SCHEME_HTTP, 80},
214 {URL_SCHEME_GOPHER, 70},
215 {URL_SCHEME_NNTP, 119},
216 {URL_SCHEME_TELNET, 23},
217 {URL_SCHEME_WAIS, 210},
218 {URL_SCHEME_HTTPS, 443},
221 /* List of 3-character top level domain names Windows seems to recognize.
222 * There might be more, but, these are the only ones I've found so far.
224 static const struct {
225 WCHAR tld_name[4];
226 } recognized_tlds[] = {
227 {L"com"},
228 {L"edu"},
229 {L"gov"},
230 {L"int"},
231 {L"mil"},
232 {L"net"},
233 {L"org"}
236 static Uri *get_uri_obj(IUri *uri)
238 Uri *ret;
239 HRESULT hres;
241 hres = IUri_QueryInterface(uri, &IID_IUriObj, (void**)&ret);
242 return SUCCEEDED(hres) ? ret : NULL;
245 static inline BOOL is_alpha(WCHAR val) {
246 return ((val >= 'a' && val <= 'z') || (val >= 'A' && val <= 'Z'));
249 static inline BOOL is_num(WCHAR val) {
250 return (val >= '0' && val <= '9');
253 static inline BOOL is_drive_path(const WCHAR *str) {
254 return (is_alpha(str[0]) && (str[1] == ':' || str[1] == '|'));
257 static inline BOOL is_unc_path(const WCHAR *str) {
258 return (str[0] == '\\' && str[1] == '\\');
261 static inline BOOL is_forbidden_dos_path_char(WCHAR val) {
262 return (val == '>' || val == '<' || val == '\"');
265 /* A URI is implicitly a file path if it begins with
266 * a drive letter (e.g. X:) or starts with "\\" (UNC path).
268 static inline BOOL is_implicit_file_path(const WCHAR *str) {
269 return (is_unc_path(str) || (is_alpha(str[0]) && str[1] == ':'));
272 /* Checks if the URI is a hierarchical URI. A hierarchical
273 * URI is one that has "//" after the scheme.
275 static BOOL check_hierarchical(const WCHAR **ptr) {
276 const WCHAR *start = *ptr;
278 if(**ptr != '/')
279 return FALSE;
281 ++(*ptr);
282 if(**ptr != '/') {
283 *ptr = start;
284 return FALSE;
287 ++(*ptr);
288 return TRUE;
291 /* unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~" */
292 static inline BOOL is_unreserved(WCHAR val) {
293 return (is_alpha(val) || is_num(val) || val == '-' || val == '.' ||
294 val == '_' || val == '~');
297 /* sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
298 * / "*" / "+" / "," / ";" / "="
300 static inline BOOL is_subdelim(WCHAR val) {
301 return (val == '!' || val == '$' || val == '&' ||
302 val == '\'' || val == '(' || val == ')' ||
303 val == '*' || val == '+' || val == ',' ||
304 val == ';' || val == '=');
307 /* gen-delims = ":" / "/" / "?" / "#" / "[" / "]" / "@" */
308 static inline BOOL is_gendelim(WCHAR val) {
309 return (val == ':' || val == '/' || val == '?' ||
310 val == '#' || val == '[' || val == ']' ||
311 val == '@');
314 /* Characters that delimit the end of the authority
315 * section of a URI. Sometimes a '\\' is considered
316 * an authority delimiter.
318 static inline BOOL is_auth_delim(WCHAR val, BOOL acceptSlash) {
319 return (val == '#' || val == '/' || val == '?' ||
320 val == '\0' || (acceptSlash && val == '\\'));
323 /* reserved = gen-delims / sub-delims */
324 static inline BOOL is_reserved(WCHAR val) {
325 return (is_subdelim(val) || is_gendelim(val));
328 static inline BOOL is_hexdigit(WCHAR val) {
329 return ((val >= 'a' && val <= 'f') ||
330 (val >= 'A' && val <= 'F') ||
331 (val >= '0' && val <= '9'));
334 static inline BOOL is_path_delim(URL_SCHEME scheme, WCHAR val) {
335 return (!val || (val == '#' && scheme != URL_SCHEME_FILE) || val == '?');
338 static inline BOOL is_slash(WCHAR c)
340 return c == '/' || c == '\\';
343 static inline BOOL is_ascii(WCHAR c)
345 return c < 0x80;
348 static BOOL is_default_port(URL_SCHEME scheme, DWORD port) {
349 DWORD i;
351 for(i = 0; i < ARRAY_SIZE(default_ports); ++i) {
352 if(default_ports[i].scheme == scheme && default_ports[i].port)
353 return TRUE;
356 return FALSE;
359 /* List of schemes types Windows seems to expect to be hierarchical. */
360 static inline BOOL is_hierarchical_scheme(URL_SCHEME type) {
361 return(type == URL_SCHEME_HTTP || type == URL_SCHEME_FTP ||
362 type == URL_SCHEME_GOPHER || type == URL_SCHEME_NNTP ||
363 type == URL_SCHEME_TELNET || type == URL_SCHEME_WAIS ||
364 type == URL_SCHEME_FILE || type == URL_SCHEME_HTTPS ||
365 type == URL_SCHEME_RES);
368 /* Checks if 'flags' contains an invalid combination of Uri_CREATE flags. */
369 static inline BOOL has_invalid_flag_combination(DWORD flags) {
370 return((flags & Uri_CREATE_DECODE_EXTRA_INFO && flags & Uri_CREATE_NO_DECODE_EXTRA_INFO) ||
371 (flags & Uri_CREATE_CANONICALIZE && flags & Uri_CREATE_NO_CANONICALIZE) ||
372 (flags & Uri_CREATE_CRACK_UNKNOWN_SCHEMES && flags & Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES) ||
373 (flags & Uri_CREATE_PRE_PROCESS_HTML_URI && flags & Uri_CREATE_NO_PRE_PROCESS_HTML_URI) ||
374 (flags & Uri_CREATE_IE_SETTINGS && flags & Uri_CREATE_NO_IE_SETTINGS));
377 /* Applies each default Uri_CREATE flags to 'flags' if it
378 * doesn't cause a flag conflict.
380 static void apply_default_flags(DWORD *flags) {
381 if(!(*flags & Uri_CREATE_NO_CANONICALIZE))
382 *flags |= Uri_CREATE_CANONICALIZE;
383 if(!(*flags & Uri_CREATE_NO_DECODE_EXTRA_INFO))
384 *flags |= Uri_CREATE_DECODE_EXTRA_INFO;
385 if(!(*flags & Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES))
386 *flags |= Uri_CREATE_CRACK_UNKNOWN_SCHEMES;
387 if(!(*flags & Uri_CREATE_NO_PRE_PROCESS_HTML_URI))
388 *flags |= Uri_CREATE_PRE_PROCESS_HTML_URI;
389 if(!(*flags & Uri_CREATE_IE_SETTINGS))
390 *flags |= Uri_CREATE_NO_IE_SETTINGS;
393 /* Determines if the URI is hierarchical using the information already parsed into
394 * data and using the current location of parsing in the URI string.
396 * Windows considers a URI hierarchical if one of the following is true:
397 * A.) It's a wildcard scheme.
398 * B.) It's an implicit file scheme.
399 * C.) It's a known hierarchical scheme and it has two '\\' after the scheme name.
400 * (the '\\' will be converted into "//" during canonicalization).
401 * D.) "//" appears after the scheme name (or at the beginning if no scheme is given).
403 static inline BOOL is_hierarchical_uri(const WCHAR **ptr, const parse_data *data) {
404 const WCHAR *start = *ptr;
406 if(data->scheme_type == URL_SCHEME_WILDCARD)
407 return TRUE;
408 else if(data->scheme_type == URL_SCHEME_FILE && data->has_implicit_scheme)
409 return TRUE;
410 else if(is_hierarchical_scheme(data->scheme_type) && (*ptr)[0] == '\\' && (*ptr)[1] == '\\') {
411 *ptr += 2;
412 return TRUE;
413 } else if(data->scheme_type != URL_SCHEME_MAILTO && check_hierarchical(ptr))
414 return TRUE;
416 *ptr = start;
417 return FALSE;
420 /* Taken from dlls/jscript/lex.c */
421 static int hex_to_int(WCHAR val) {
422 if(val >= '0' && val <= '9')
423 return val - '0';
424 else if(val >= 'a' && val <= 'f')
425 return val - 'a' + 10;
426 else if(val >= 'A' && val <= 'F')
427 return val - 'A' + 10;
429 return -1;
432 /* Helper function for converting a percent encoded string
433 * representation of a WCHAR value into its actual WCHAR value. If
434 * the two characters following the '%' aren't valid hex values then
435 * this function returns the NULL character.
437 * E.g.
438 * "%2E" will result in '.' being returned by this function.
440 static WCHAR decode_pct_val(const WCHAR *ptr) {
441 WCHAR ret = '\0';
443 if(*ptr == '%' && is_hexdigit(*(ptr + 1)) && is_hexdigit(*(ptr + 2))) {
444 INT a = hex_to_int(*(ptr + 1));
445 INT b = hex_to_int(*(ptr + 2));
447 ret = a << 4;
448 ret += b;
451 return ret;
454 /* Helper function for percent encoding a given character
455 * and storing the encoded value into a given buffer (dest).
457 * It's up to the calling function to ensure that there is
458 * at least enough space in 'dest' for the percent encoded
459 * value to be stored (so dest + 3 spaces available).
461 static inline void pct_encode_val(WCHAR val, WCHAR *dest) {
462 dest[0] = '%';
463 dest[1] = hexDigits[(val >> 4) & 0xf];
464 dest[2] = hexDigits[val & 0xf];
467 /* Attempts to parse the domain name from the host.
469 * This function also includes the Top-level Domain (TLD) name
470 * of the host when it tries to find the domain name. If it finds
471 * a valid domain name it will assign 'domain_start' the offset
472 * into 'host' where the domain name starts.
474 * It's implied that if there is a domain name its range is:
475 * [host+domain_start, host+host_len).
477 void find_domain_name(const WCHAR *host, DWORD host_len,
478 INT *domain_start) {
479 const WCHAR *last_tld, *sec_last_tld, *end, *p;
481 end = host+host_len-1;
483 *domain_start = -1;
485 /* There has to be at least enough room for a '.' followed by a
486 * 3-character TLD for a domain to even exist in the host name.
488 if(host_len < 4)
489 return;
491 for (last_tld = sec_last_tld = NULL, p = host; p <= end; p++)
493 if (*p == '.')
495 sec_last_tld = last_tld;
496 last_tld = p;
499 if(!last_tld)
500 /* http://hostname -> has no domain name. */
501 return;
503 if(!sec_last_tld) {
504 /* If the '.' is at the beginning of the host there
505 * has to be at least 3 characters in the TLD for it
506 * to be valid.
507 * Ex: .com -> .com as the domain name.
508 * .co -> has no domain name.
510 if(last_tld-host == 0) {
511 if(end-(last_tld-1) < 3)
512 return;
513 } else if(last_tld-host == 3) {
514 DWORD i;
516 /* If there are three characters in front of last_tld and
517 * they are on the list of recognized TLDs, then this
518 * host doesn't have a domain (since the host only contains
519 * a TLD name.
520 * Ex: edu.uk -> has no domain name.
521 * foo.uk -> foo.uk as the domain name.
523 for(i = 0; i < ARRAY_SIZE(recognized_tlds); ++i) {
524 if(!StrCmpNIW(host, recognized_tlds[i].tld_name, 3))
525 return;
527 } else if(last_tld-host < 3)
528 /* Anything less than 3 characters is considered part
529 * of the TLD name.
530 * Ex: ak.uk -> Has no domain name.
532 return;
534 /* Otherwise the domain name is the whole host name. */
535 *domain_start = 0;
536 } else if(end+1-last_tld > 3) {
537 /* If the last_tld has more than 3 characters, then it's automatically
538 * considered the TLD of the domain name.
539 * Ex: www.winehq.org.uk.test -> uk.test as the domain name.
541 *domain_start = (sec_last_tld+1)-host;
542 } else if(last_tld - (sec_last_tld+1) < 4) {
543 DWORD i;
544 /* If the sec_last_tld is 3 characters long it HAS to be on the list of
545 * recognized to still be considered part of the TLD name, otherwise
546 * it's considered the domain name.
547 * Ex: www.google.com.uk -> google.com.uk as the domain name.
548 * www.google.foo.uk -> foo.uk as the domain name.
550 if(last_tld - (sec_last_tld+1) == 3) {
551 for(i = 0; i < ARRAY_SIZE(recognized_tlds); ++i) {
552 if(!StrCmpNIW(sec_last_tld+1, recognized_tlds[i].tld_name, 3)) {
553 for (p = sec_last_tld; p > host; p--) if (p[-1] == '.') break;
554 *domain_start = p - host;
555 TRACE("Found domain name %s\n", debugstr_wn(host+*domain_start,
556 (host+host_len)-(host+*domain_start)));
557 return;
561 *domain_start = (sec_last_tld+1)-host;
562 } else {
563 /* Since the sec_last_tld is less than 3 characters it's considered
564 * part of the TLD.
565 * Ex: www.google.fo.uk -> google.fo.uk as the domain name.
567 for (p = sec_last_tld; p > host; p--) if (p[-1] == '.') break;
568 *domain_start = p - host;
570 } else {
571 /* The second to last TLD has more than 3 characters making it
572 * the domain name.
573 * Ex: www.google.test.us -> test.us as the domain name.
575 *domain_start = (sec_last_tld+1)-host;
578 TRACE("Found domain name %s\n", debugstr_wn(host+*domain_start,
579 (host+host_len)-(host+*domain_start)));
582 /* Removes the dot segments from a hierarchical URIs path component. This
583 * function performs the removal in place.
585 * This function returns the new length of the path string.
587 static DWORD remove_dot_segments(WCHAR *path, DWORD path_len) {
588 WCHAR *out = path;
589 const WCHAR *in = out;
590 const WCHAR *end = out + path_len;
591 DWORD len;
593 while(in < end) {
594 /* Move the first path segment in the input buffer to the end of
595 * the output buffer, and any subsequent characters up to, including
596 * the next "/" character (if any) or the end of the input buffer.
598 while(in < end && !is_slash(*in))
599 *out++ = *in++;
600 if(in == end)
601 break;
602 *out++ = *in++;
604 while(in < end) {
605 if(*in != '.')
606 break;
608 /* Handle ending "/." */
609 if(in + 1 == end) {
610 ++in;
611 break;
614 /* Handle "/./" */
615 if(is_slash(in[1])) {
616 in += 2;
617 continue;
620 /* If we don't have "/../" or ending "/.." */
621 if(in[1] != '.' || (in + 2 != end && !is_slash(in[2])))
622 break;
624 /* Find the slash preceding out pointer and move out pointer to it */
625 if(out > path+1 && is_slash(*--out))
626 --out;
627 while(out > path && !is_slash(*(--out)));
628 if(is_slash(*out))
629 ++out;
630 in += 2;
631 if(in != end)
632 ++in;
636 len = out - path;
637 TRACE("(%p %d): Path after dot segments removed %s len=%d\n", path, path_len,
638 debugstr_wn(path, len), len);
639 return len;
642 /* Attempts to find the file extension in a given path. */
643 static INT find_file_extension(const WCHAR *path, DWORD path_len) {
644 const WCHAR *end;
646 for(end = path+path_len-1; end >= path && *end != '/' && *end != '\\'; --end) {
647 if(*end == '.')
648 return end-path;
651 return -1;
654 /* Removes all the leading and trailing white spaces or
655 * control characters from the URI and removes all control
656 * characters inside of the URI string.
658 static BSTR pre_process_uri(LPCWSTR uri) {
659 const WCHAR *start, *end, *ptr;
660 WCHAR *ptr2;
661 DWORD len;
662 BSTR ret;
664 start = uri;
665 /* Skip leading controls and whitespace. */
666 while(*start && (iswcntrl(*start) || iswspace(*start))) ++start;
668 /* URI consisted only of control/whitespace. */
669 if(!*start)
670 return SysAllocStringLen(NULL, 0);
672 end = start + lstrlenW(start);
673 while(--end > start && (iswcntrl(*end) || iswspace(*end)));
675 len = ++end - start;
676 for(ptr = start; ptr < end; ptr++) {
677 if(iswcntrl(*ptr))
678 len--;
681 ret = SysAllocStringLen(NULL, len);
682 if(!ret)
683 return NULL;
685 for(ptr = start, ptr2=ret; ptr < end; ptr++) {
686 if(!iswcntrl(*ptr))
687 *ptr2++ = *ptr;
690 return ret;
693 /* Converts an IPv4 address in numerical form into its fully qualified
694 * string form. This function returns the number of characters written
695 * to 'dest'. If 'dest' is NULL this function will return the number of
696 * characters that would have been written.
698 * It's up to the caller to ensure there's enough space in 'dest' for the
699 * address.
701 static DWORD ui2ipv4(WCHAR *dest, UINT address) {
702 static const WCHAR formatW[] =
703 {'%','u','.','%','u','.','%','u','.','%','u',0};
704 DWORD ret = 0;
705 UCHAR digits[4];
707 digits[0] = (address >> 24) & 0xff;
708 digits[1] = (address >> 16) & 0xff;
709 digits[2] = (address >> 8) & 0xff;
710 digits[3] = address & 0xff;
712 if(!dest) {
713 WCHAR tmp[16];
714 ret = swprintf(tmp, ARRAY_SIZE(tmp), formatW, digits[0], digits[1], digits[2], digits[3]);
715 } else
716 ret = swprintf(dest, 16, formatW, digits[0], digits[1], digits[2], digits[3]);
718 return ret;
721 static DWORD ui2str(WCHAR *dest, UINT value) {
722 static const WCHAR formatW[] = {'%','u',0};
723 DWORD ret = 0;
725 if(!dest) {
726 WCHAR tmp[11];
727 ret = swprintf(tmp, ARRAY_SIZE(tmp), formatW, value);
728 } else
729 ret = swprintf(dest, 11, formatW, value);
731 return ret;
734 /* Checks if the characters pointed to by 'ptr' are
735 * a percent encoded data octet.
737 * pct-encoded = "%" HEXDIG HEXDIG
739 static BOOL check_pct_encoded(const WCHAR **ptr) {
740 const WCHAR *start = *ptr;
742 if(**ptr != '%')
743 return FALSE;
745 ++(*ptr);
746 if(!is_hexdigit(**ptr)) {
747 *ptr = start;
748 return FALSE;
751 ++(*ptr);
752 if(!is_hexdigit(**ptr)) {
753 *ptr = start;
754 return FALSE;
757 ++(*ptr);
758 return TRUE;
761 /* dec-octet = DIGIT ; 0-9
762 * / %x31-39 DIGIT ; 10-99
763 * / "1" 2DIGIT ; 100-199
764 * / "2" %x30-34 DIGIT ; 200-249
765 * / "25" %x30-35 ; 250-255
767 static BOOL check_dec_octet(const WCHAR **ptr) {
768 const WCHAR *c1, *c2, *c3;
770 c1 = *ptr;
771 /* A dec-octet must be at least 1 digit long. */
772 if(*c1 < '0' || *c1 > '9')
773 return FALSE;
775 ++(*ptr);
777 c2 = *ptr;
778 /* Since the 1-digit requirement was met, it doesn't
779 * matter if this is a DIGIT value, it's considered a
780 * dec-octet.
782 if(*c2 < '0' || *c2 > '9')
783 return TRUE;
785 ++(*ptr);
787 c3 = *ptr;
788 /* Same explanation as above. */
789 if(*c3 < '0' || *c3 > '9')
790 return TRUE;
792 /* Anything > 255 isn't a valid IP dec-octet. */
793 if(*c1 >= '2' && *c2 >= '5' && *c3 >= '5') {
794 *ptr = c1;
795 return FALSE;
798 ++(*ptr);
799 return TRUE;
802 /* Checks if there is an implicit IPv4 address in the host component of the URI.
803 * The max value of an implicit IPv4 address is UINT_MAX.
805 * Ex:
806 * "234567" would be considered an implicit IPv4 address.
808 static BOOL check_implicit_ipv4(const WCHAR **ptr, UINT *val) {
809 const WCHAR *start = *ptr;
810 ULONGLONG ret = 0;
811 *val = 0;
813 while(is_num(**ptr)) {
814 ret = ret*10 + (**ptr - '0');
816 if(ret > UINT_MAX) {
817 *ptr = start;
818 return FALSE;
820 ++(*ptr);
823 if(*ptr == start)
824 return FALSE;
826 *val = ret;
827 return TRUE;
830 /* Checks if the string contains an IPv4 address.
832 * This function has a strict mode or a non-strict mode of operation
833 * When 'strict' is set to FALSE this function will return TRUE if
834 * the string contains at least 'dec-octet "." dec-octet' since partial
835 * IPv4 addresses will be normalized out into full IPv4 addresses. When
836 * 'strict' is set this function expects there to be a full IPv4 address.
838 * IPv4address = dec-octet "." dec-octet "." dec-octet "." dec-octet
840 static BOOL check_ipv4address(const WCHAR **ptr, BOOL strict) {
841 const WCHAR *start = *ptr;
843 if(!check_dec_octet(ptr)) {
844 *ptr = start;
845 return FALSE;
848 if(**ptr != '.') {
849 *ptr = start;
850 return FALSE;
853 ++(*ptr);
854 if(!check_dec_octet(ptr)) {
855 *ptr = start;
856 return FALSE;
859 if(**ptr != '.') {
860 if(strict) {
861 *ptr = start;
862 return FALSE;
863 } else
864 return TRUE;
867 ++(*ptr);
868 if(!check_dec_octet(ptr)) {
869 *ptr = start;
870 return FALSE;
873 if(**ptr != '.') {
874 if(strict) {
875 *ptr = start;
876 return FALSE;
877 } else
878 return TRUE;
881 ++(*ptr);
882 if(!check_dec_octet(ptr)) {
883 *ptr = start;
884 return FALSE;
887 /* Found a four digit ip address. */
888 return TRUE;
890 /* Tries to parse the scheme name of the URI.
892 * scheme = ALPHA *(ALPHA | NUM | '+' | '-' | '.') as defined by RFC 3896.
893 * NOTE: Windows accepts a number as the first character of a scheme.
895 static BOOL parse_scheme_name(const WCHAR **ptr, parse_data *data, DWORD extras) {
896 const WCHAR *start = *ptr;
898 data->scheme = NULL;
899 data->scheme_len = 0;
901 while(**ptr) {
902 if(**ptr == '*' && *ptr == start) {
903 /* Might have found a wildcard scheme. If it is the next
904 * char has to be a ':' for it to be a valid URI
906 ++(*ptr);
907 break;
908 } else if(!is_num(**ptr) && !is_alpha(**ptr) && **ptr != '+' &&
909 **ptr != '-' && **ptr != '.')
910 break;
912 (*ptr)++;
915 if(*ptr == start)
916 return FALSE;
918 /* Schemes must end with a ':' */
919 if(**ptr != ':' && !((extras & ALLOW_NULL_TERM_SCHEME) && !**ptr)) {
920 *ptr = start;
921 return FALSE;
924 data->scheme = start;
925 data->scheme_len = *ptr - start;
927 ++(*ptr);
928 return TRUE;
931 /* Tries to deduce the corresponding URL_SCHEME for the given URI. Stores
932 * the deduced URL_SCHEME in data->scheme_type.
934 static BOOL parse_scheme_type(parse_data *data) {
935 /* If there's scheme data then see if it's a recognized scheme. */
936 if(data->scheme && data->scheme_len) {
937 DWORD i;
939 for(i = 0; i < ARRAY_SIZE(recognized_schemes); ++i) {
940 if(lstrlenW(recognized_schemes[i].scheme_name) == data->scheme_len) {
941 /* Has to be a case insensitive compare. */
942 if(!StrCmpNIW(recognized_schemes[i].scheme_name, data->scheme, data->scheme_len)) {
943 data->scheme_type = recognized_schemes[i].scheme;
944 return TRUE;
949 /* If we get here it means it's not a recognized scheme. */
950 data->scheme_type = URL_SCHEME_UNKNOWN;
951 return TRUE;
952 } else if(data->is_relative) {
953 /* Relative URI's have no scheme. */
954 data->scheme_type = URL_SCHEME_UNKNOWN;
955 return TRUE;
956 } else {
957 /* Should never reach here! what happened... */
958 FIXME("(%p): Unable to determine scheme type for URI %s\n", data, debugstr_w(data->uri));
959 return FALSE;
963 /* Tries to parse (or deduce) the scheme_name of a URI. If it can't
964 * parse a scheme from the URI it will try to deduce the scheme_name and scheme_type
965 * using the flags specified in 'flags' (if any). Flags that affect how this function
966 * operates are the Uri_CREATE_ALLOW_* flags.
968 * All parsed/deduced information will be stored in 'data' when the function returns.
970 * Returns TRUE if it was able to successfully parse the information.
972 static BOOL parse_scheme(const WCHAR **ptr, parse_data *data, DWORD flags, DWORD extras) {
973 static const WCHAR fileW[] = {'f','i','l','e',0};
974 static const WCHAR wildcardW[] = {'*',0};
976 /* First check to see if the uri could implicitly be a file path. */
977 if(is_implicit_file_path(*ptr)) {
978 if(flags & Uri_CREATE_ALLOW_IMPLICIT_FILE_SCHEME) {
979 data->scheme = fileW;
980 data->scheme_len = lstrlenW(fileW);
981 data->has_implicit_scheme = TRUE;
983 TRACE("(%p %p %x): URI is an implicit file path.\n", ptr, data, flags);
984 } else {
985 /* Windows does not consider anything that can implicitly be a file
986 * path to be a valid URI if the ALLOW_IMPLICIT_FILE_SCHEME flag is not set...
988 TRACE("(%p %p %x): URI is implicitly a file path, but, the ALLOW_IMPLICIT_FILE_SCHEME flag wasn't set.\n",
989 ptr, data, flags);
990 return FALSE;
992 } else if(!parse_scheme_name(ptr, data, extras)) {
993 /* No scheme was found, this means it could be:
994 * a) an implicit Wildcard scheme
995 * b) a relative URI
996 * c) an invalid URI.
998 if(flags & Uri_CREATE_ALLOW_IMPLICIT_WILDCARD_SCHEME) {
999 data->scheme = wildcardW;
1000 data->scheme_len = lstrlenW(wildcardW);
1001 data->has_implicit_scheme = TRUE;
1003 TRACE("(%p %p %x): URI is an implicit wildcard scheme.\n", ptr, data, flags);
1004 } else if (flags & Uri_CREATE_ALLOW_RELATIVE) {
1005 data->is_relative = TRUE;
1006 TRACE("(%p %p %x): URI is relative.\n", ptr, data, flags);
1007 } else {
1008 TRACE("(%p %p %x): Malformed URI found. Unable to deduce scheme name.\n", ptr, data, flags);
1009 return FALSE;
1013 if(!data->is_relative)
1014 TRACE("(%p %p %x): Found scheme=%s scheme_len=%d\n", ptr, data, flags,
1015 debugstr_wn(data->scheme, data->scheme_len), data->scheme_len);
1017 if(!parse_scheme_type(data))
1018 return FALSE;
1020 TRACE("(%p %p %x): Assigned %d as the URL_SCHEME.\n", ptr, data, flags, data->scheme_type);
1021 return TRUE;
1024 static BOOL parse_username(const WCHAR **ptr, parse_data *data, DWORD flags, DWORD extras) {
1025 data->username = *ptr;
1027 while(**ptr != ':' && **ptr != '@') {
1028 if(**ptr == '%') {
1029 if(!check_pct_encoded(ptr)) {
1030 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
1031 *ptr = data->username;
1032 data->username = NULL;
1033 return FALSE;
1035 } else
1036 continue;
1037 } else if(extras & ALLOW_NULL_TERM_USER_NAME && !**ptr)
1038 break;
1039 else if(is_auth_delim(**ptr, data->scheme_type != URL_SCHEME_UNKNOWN)) {
1040 *ptr = data->username;
1041 data->username = NULL;
1042 return FALSE;
1045 ++(*ptr);
1048 data->username_len = *ptr - data->username;
1049 return TRUE;
1052 static BOOL parse_password(const WCHAR **ptr, parse_data *data, DWORD flags, DWORD extras) {
1053 data->password = *ptr;
1055 while(**ptr != '@') {
1056 if(**ptr == '%') {
1057 if(!check_pct_encoded(ptr)) {
1058 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
1059 *ptr = data->password;
1060 data->password = NULL;
1061 return FALSE;
1063 } else
1064 continue;
1065 } else if(extras & ALLOW_NULL_TERM_PASSWORD && !**ptr)
1066 break;
1067 else if(is_auth_delim(**ptr, data->scheme_type != URL_SCHEME_UNKNOWN)) {
1068 *ptr = data->password;
1069 data->password = NULL;
1070 return FALSE;
1073 ++(*ptr);
1076 data->password_len = *ptr - data->password;
1077 return TRUE;
1080 /* Parses the userinfo part of the URI (if it exists). The userinfo field of
1081 * a URI can consist of "username:password@", or just "username@".
1083 * RFC def:
1084 * userinfo = *( unreserved / pct-encoded / sub-delims / ":" )
1086 * NOTES:
1087 * 1) If there is more than one ':' in the userinfo part of the URI Windows
1088 * uses the first occurrence of ':' to delimit the username and password
1089 * components.
1091 * ex:
1092 * ftp://user:pass:word@winehq.org
1094 * would yield "user" as the username and "pass:word" as the password.
1096 * 2) Windows allows any character to appear in the "userinfo" part of
1097 * a URI, as long as it's not an authority delimiter character set.
1099 static void parse_userinfo(const WCHAR **ptr, parse_data *data, DWORD flags) {
1100 const WCHAR *start = *ptr;
1102 if(!parse_username(ptr, data, flags, 0)) {
1103 TRACE("(%p %p %x): URI contained no userinfo.\n", ptr, data, flags);
1104 return;
1107 if(**ptr == ':') {
1108 ++(*ptr);
1109 if(!parse_password(ptr, data, flags, 0)) {
1110 *ptr = start;
1111 data->username = NULL;
1112 data->username_len = 0;
1113 TRACE("(%p %p %x): URI contained no userinfo.\n", ptr, data, flags);
1114 return;
1118 if(**ptr != '@') {
1119 *ptr = start;
1120 data->username = NULL;
1121 data->username_len = 0;
1122 data->password = NULL;
1123 data->password_len = 0;
1125 TRACE("(%p %p %x): URI contained no userinfo.\n", ptr, data, flags);
1126 return;
1129 if(data->username)
1130 TRACE("(%p %p %x): Found username %s len=%d.\n", ptr, data, flags,
1131 debugstr_wn(data->username, data->username_len), data->username_len);
1133 if(data->password)
1134 TRACE("(%p %p %x): Found password %s len=%d.\n", ptr, data, flags,
1135 debugstr_wn(data->password, data->password_len), data->password_len);
1137 ++(*ptr);
1140 /* Attempts to parse a port from the URI.
1142 * NOTES:
1143 * Windows seems to have a cap on what the maximum value
1144 * for a port can be. The max value is USHORT_MAX.
1146 * port = *DIGIT
1148 static BOOL parse_port(const WCHAR **ptr, parse_data *data) {
1149 UINT port = 0;
1150 data->port = *ptr;
1152 while(!is_auth_delim(**ptr, data->scheme_type != URL_SCHEME_UNKNOWN)) {
1153 if(!is_num(**ptr)) {
1154 *ptr = data->port;
1155 data->port = NULL;
1156 return FALSE;
1159 port = port*10 + (**ptr-'0');
1161 if(port > USHRT_MAX) {
1162 *ptr = data->port;
1163 data->port = NULL;
1164 return FALSE;
1167 ++(*ptr);
1170 data->has_port = TRUE;
1171 data->port_value = port;
1172 data->port_len = *ptr - data->port;
1174 TRACE("(%p %p): Found port %s len=%d value=%u\n", ptr, data,
1175 debugstr_wn(data->port, data->port_len), data->port_len, data->port_value);
1176 return TRUE;
1179 /* Attempts to parse a IPv4 address from the URI.
1181 * NOTES:
1182 * Windows normalizes IPv4 addresses, This means there are three
1183 * possibilities for the URI to contain an IPv4 address.
1184 * 1) A well formed address (ex. 192.2.2.2).
1185 * 2) A partially formed address. For example "192.0" would
1186 * normalize to "192.0.0.0" during canonicalization.
1187 * 3) An implicit IPv4 address. For example "256" would
1188 * normalize to "0.0.1.0" during canonicalization. Also
1189 * note that the maximum value for an implicit IP address
1190 * is UINT_MAX, if the value in the URI exceeds this then
1191 * it is not considered an IPv4 address.
1193 static BOOL parse_ipv4address(const WCHAR **ptr, parse_data *data) {
1194 const BOOL is_unknown = data->scheme_type == URL_SCHEME_UNKNOWN;
1195 data->host = *ptr;
1197 if(!check_ipv4address(ptr, FALSE)) {
1198 if(!check_implicit_ipv4(ptr, &data->implicit_ipv4)) {
1199 TRACE("(%p %p): URI didn't contain anything looking like an IPv4 address.\n", ptr, data);
1200 *ptr = data->host;
1201 data->host = NULL;
1202 return FALSE;
1203 } else
1204 data->has_implicit_ip = TRUE;
1207 data->host_len = *ptr - data->host;
1208 data->host_type = Uri_HOST_IPV4;
1210 /* Check if what we found is the only part of the host name (if it isn't
1211 * we don't have an IPv4 address).
1213 if(**ptr == ':') {
1214 ++(*ptr);
1215 if(!parse_port(ptr, data)) {
1216 *ptr = data->host;
1217 data->host = NULL;
1218 return FALSE;
1220 } else if(!is_auth_delim(**ptr, !is_unknown)) {
1221 /* Found more data which belongs to the host, so this isn't an IPv4. */
1222 *ptr = data->host;
1223 data->host = NULL;
1224 data->has_implicit_ip = FALSE;
1225 return FALSE;
1228 TRACE("(%p %p): IPv4 address found. host=%s host_len=%d host_type=%d\n",
1229 ptr, data, debugstr_wn(data->host, data->host_len),
1230 data->host_len, data->host_type);
1231 return TRUE;
1234 /* Attempts to parse the reg-name from the URI.
1236 * Because of the way Windows handles ':' this function also
1237 * handles parsing the port.
1239 * reg-name = *( unreserved / pct-encoded / sub-delims )
1241 * NOTE:
1242 * Windows allows everything, but, the characters in "auth_delims" and ':'
1243 * to appear in a reg-name, unless it's an unknown scheme type then ':' is
1244 * allowed to appear (even if a valid port isn't after it).
1246 * Windows doesn't like host names which start with '[' and end with ']'
1247 * and don't contain a valid IP literal address in between them.
1249 * On Windows if a '[' is encountered in the host name the ':' no longer
1250 * counts as a delimiter until you reach the next ']' or an "authority delimiter".
1252 * A reg-name CAN be empty.
1254 static BOOL parse_reg_name(const WCHAR **ptr, parse_data *data, DWORD extras) {
1255 const BOOL has_start_bracket = **ptr == '[';
1256 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
1257 const BOOL is_res = data->scheme_type == URL_SCHEME_RES;
1258 BOOL inside_brackets = has_start_bracket;
1260 /* res URIs don't have ports. */
1261 BOOL ignore_col = (extras & IGNORE_PORT_DELIMITER) || is_res;
1263 /* We have to be careful with file schemes. */
1264 if(data->scheme_type == URL_SCHEME_FILE) {
1265 /* This is because an implicit file scheme could be "C:\\test" and it
1266 * would trick this function into thinking the host is "C", when after
1267 * canonicalization the host would end up being an empty string. A drive
1268 * path can also have a '|' instead of a ':' after the drive letter.
1270 if(is_drive_path(*ptr)) {
1271 /* Regular old drive paths have no host type (or host name). */
1272 data->host_type = Uri_HOST_UNKNOWN;
1273 data->host = *ptr;
1274 data->host_len = 0;
1275 return TRUE;
1276 } else if(is_unc_path(*ptr))
1277 /* Skip past the "\\" of a UNC path. */
1278 *ptr += 2;
1281 data->host = *ptr;
1283 /* For res URIs, everything before the first '/' is
1284 * considered the host.
1286 while((!is_res && !is_auth_delim(**ptr, known_scheme)) ||
1287 (is_res && **ptr && **ptr != '/')) {
1288 if(**ptr == ':' && !ignore_col) {
1289 /* We can ignore ':' if we are inside brackets.*/
1290 if(!inside_brackets) {
1291 const WCHAR *tmp = (*ptr)++;
1293 /* Attempt to parse the port. */
1294 if(!parse_port(ptr, data)) {
1295 /* Windows expects there to be a valid port for known scheme types. */
1296 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
1297 *ptr = data->host;
1298 data->host = NULL;
1299 TRACE("(%p %p %x): Expected valid port\n", ptr, data, extras);
1300 return FALSE;
1301 } else
1302 /* Windows gives up on trying to parse a port when it
1303 * encounters an invalid port.
1305 ignore_col = TRUE;
1306 } else {
1307 data->host_len = tmp - data->host;
1308 break;
1311 } else if(**ptr == '%' && (known_scheme && !is_res)) {
1312 /* Has to be a legit % encoded value. */
1313 if(!check_pct_encoded(ptr)) {
1314 *ptr = data->host;
1315 data->host = NULL;
1316 return FALSE;
1317 } else
1318 continue;
1319 } else if(is_res && is_forbidden_dos_path_char(**ptr)) {
1320 *ptr = data->host;
1321 data->host = NULL;
1322 return FALSE;
1323 } else if(**ptr == ']')
1324 inside_brackets = FALSE;
1325 else if(**ptr == '[')
1326 inside_brackets = TRUE;
1328 ++(*ptr);
1331 if(has_start_bracket) {
1332 /* Make sure the last character of the host wasn't a ']'. */
1333 if(*(*ptr-1) == ']') {
1334 TRACE("(%p %p %x): Expected an IP literal inside of the host\n", ptr, data, extras);
1335 *ptr = data->host;
1336 data->host = NULL;
1337 return FALSE;
1341 /* Don't overwrite our length if we found a port earlier. */
1342 if(!data->port)
1343 data->host_len = *ptr - data->host;
1345 /* If the host is empty, then it's an unknown host type. */
1346 if(data->host_len == 0 || is_res)
1347 data->host_type = Uri_HOST_UNKNOWN;
1348 else
1349 data->host_type = Uri_HOST_DNS;
1351 TRACE("(%p %p %x): Parsed reg-name. host=%s len=%d\n", ptr, data, extras,
1352 debugstr_wn(data->host, data->host_len), data->host_len);
1353 return TRUE;
1356 /* Attempts to parse an IPv6 address out of the URI.
1358 * IPv6address = 6( h16 ":" ) ls32
1359 * / "::" 5( h16 ":" ) ls32
1360 * / [ h16 ] "::" 4( h16 ":" ) ls32
1361 * / [ *1( h16 ":" ) h16 ] "::" 3( h16 ":" ) ls32
1362 * / [ *2( h16 ":" ) h16 ] "::" 2( h16 ":" ) ls32
1363 * / [ *3( h16 ":" ) h16 ] "::" h16 ":" ls32
1364 * / [ *4( h16 ":" ) h16 ] "::" ls32
1365 * / [ *5( h16 ":" ) h16 ] "::" h16
1366 * / [ *6( h16 ":" ) h16 ] "::"
1368 * ls32 = ( h16 ":" h16 ) / IPv4address
1369 * ; least-significant 32 bits of address.
1371 * h16 = 1*4HEXDIG
1372 * ; 16 bits of address represented in hexadecimal.
1374 static BOOL parse_ipv6address(const WCHAR **ptr, parse_data *data) {
1375 const WCHAR *terminator;
1377 if(RtlIpv6StringToAddressW(*ptr, &terminator, &data->ipv6_address))
1378 return FALSE;
1379 if(*terminator != ']' && !is_auth_delim(*terminator, data->scheme_type != URL_SCHEME_UNKNOWN))
1380 return FALSE;
1382 *ptr = terminator;
1383 data->host_type = Uri_HOST_IPV6;
1384 return TRUE;
1387 /* IPvFuture = "v" 1*HEXDIG "." 1*( unreserved / sub-delims / ":" ) */
1388 static BOOL parse_ipvfuture(const WCHAR **ptr, parse_data *data) {
1389 const WCHAR *start = *ptr;
1391 /* IPvFuture has to start with a 'v' or 'V'. */
1392 if(**ptr != 'v' && **ptr != 'V')
1393 return FALSE;
1395 /* Following the v there must be at least 1 hex digit. */
1396 ++(*ptr);
1397 if(!is_hexdigit(**ptr)) {
1398 *ptr = start;
1399 return FALSE;
1402 ++(*ptr);
1403 while(is_hexdigit(**ptr))
1404 ++(*ptr);
1406 /* End of the hexdigit sequence must be a '.' */
1407 if(**ptr != '.') {
1408 *ptr = start;
1409 return FALSE;
1412 ++(*ptr);
1413 if(!is_unreserved(**ptr) && !is_subdelim(**ptr) && **ptr != ':') {
1414 *ptr = start;
1415 return FALSE;
1418 ++(*ptr);
1419 while(is_unreserved(**ptr) || is_subdelim(**ptr) || **ptr == ':')
1420 ++(*ptr);
1422 data->host_type = Uri_HOST_UNKNOWN;
1424 TRACE("(%p %p): Parsed IPvFuture address %s len=%d\n", ptr, data,
1425 debugstr_wn(start, *ptr-start), (int)(*ptr-start));
1427 return TRUE;
1430 /* IP-literal = "[" ( IPv6address / IPvFuture ) "]" */
1431 static BOOL parse_ip_literal(const WCHAR **ptr, parse_data *data, DWORD extras) {
1432 data->host = *ptr;
1434 if(**ptr != '[' && !(extras & ALLOW_BRACKETLESS_IP_LITERAL)) {
1435 data->host = NULL;
1436 return FALSE;
1437 } else if(**ptr == '[')
1438 ++(*ptr);
1440 if(!parse_ipv6address(ptr, data)) {
1441 if(extras & SKIP_IP_FUTURE_CHECK || !parse_ipvfuture(ptr, data)) {
1442 *ptr = data->host;
1443 data->host = NULL;
1444 return FALSE;
1448 if(**ptr != ']' && !(extras & ALLOW_BRACKETLESS_IP_LITERAL)) {
1449 *ptr = data->host;
1450 data->host = NULL;
1451 return FALSE;
1452 } else if(!**ptr && extras & ALLOW_BRACKETLESS_IP_LITERAL) {
1453 /* The IP literal didn't contain brackets and was followed by
1454 * a NULL terminator, so no reason to even check the port.
1456 data->host_len = *ptr - data->host;
1457 return TRUE;
1460 ++(*ptr);
1461 if(**ptr == ':') {
1462 ++(*ptr);
1463 /* If a valid port is not found, then let it trickle down to
1464 * parse_reg_name.
1466 if(!parse_port(ptr, data)) {
1467 *ptr = data->host;
1468 data->host = NULL;
1469 return FALSE;
1471 } else
1472 data->host_len = *ptr - data->host;
1474 return TRUE;
1477 /* Parses the host information from the URI.
1479 * host = IP-literal / IPv4address / reg-name
1481 static BOOL parse_host(const WCHAR **ptr, parse_data *data, DWORD extras) {
1482 if(!parse_ip_literal(ptr, data, extras)) {
1483 if(!parse_ipv4address(ptr, data)) {
1484 if(!parse_reg_name(ptr, data, extras)) {
1485 TRACE("(%p %p %x): Malformed URI, Unknown host type.\n", ptr, data, extras);
1486 return FALSE;
1491 return TRUE;
1494 /* Parses the authority information from the URI.
1496 * authority = [ userinfo "@" ] host [ ":" port ]
1498 static BOOL parse_authority(const WCHAR **ptr, parse_data *data, DWORD flags) {
1499 parse_userinfo(ptr, data, flags);
1501 /* Parsing the port will happen during one of the host parsing
1502 * routines (if the URI has a port).
1504 if(!parse_host(ptr, data, 0))
1505 return FALSE;
1507 return TRUE;
1510 /* Attempts to parse the path information of a hierarchical URI. */
1511 static BOOL parse_path_hierarchical(const WCHAR **ptr, parse_data *data, DWORD flags) {
1512 const WCHAR *start = *ptr;
1513 static const WCHAR slash[] = {'/',0};
1514 const BOOL is_file = data->scheme_type == URL_SCHEME_FILE;
1516 if(is_path_delim(data->scheme_type, **ptr)) {
1517 if(data->scheme_type == URL_SCHEME_WILDCARD && !data->must_have_path) {
1518 data->path = NULL;
1519 data->path_len = 0;
1520 } else if(!(flags & Uri_CREATE_NO_CANONICALIZE)) {
1521 /* If the path component is empty, then a '/' is added. */
1522 data->path = slash;
1523 data->path_len = 1;
1525 } else {
1526 while(!is_path_delim(data->scheme_type, **ptr)) {
1527 if(**ptr == '%' && data->scheme_type != URL_SCHEME_UNKNOWN && !is_file) {
1528 if(!check_pct_encoded(ptr)) {
1529 *ptr = start;
1530 return FALSE;
1531 } else
1532 continue;
1533 } else if(is_forbidden_dos_path_char(**ptr) && is_file &&
1534 (flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
1535 /* File schemes with USE_DOS_PATH set aren't allowed to have
1536 * a '<' or '>' or '\"' appear in them.
1538 *ptr = start;
1539 return FALSE;
1540 } else if(**ptr == '\\') {
1541 /* Not allowed to have a backslash if NO_CANONICALIZE is set
1542 * and the scheme is known type (but not a file scheme).
1544 if(flags & Uri_CREATE_NO_CANONICALIZE) {
1545 if(data->scheme_type != URL_SCHEME_FILE &&
1546 data->scheme_type != URL_SCHEME_UNKNOWN) {
1547 *ptr = start;
1548 return FALSE;
1553 ++(*ptr);
1556 /* The only time a URI doesn't have a path is when
1557 * the NO_CANONICALIZE flag is set and the raw URI
1558 * didn't contain one.
1560 if(*ptr == start) {
1561 data->path = NULL;
1562 data->path_len = 0;
1563 } else {
1564 data->path = start;
1565 data->path_len = *ptr - start;
1569 if(data->path)
1570 TRACE("(%p %p %x): Parsed path %s len=%d\n", ptr, data, flags,
1571 debugstr_wn(data->path, data->path_len), data->path_len);
1572 else
1573 TRACE("(%p %p %x): The URI contained no path\n", ptr, data, flags);
1575 return TRUE;
1578 /* Parses the path of an opaque URI (much less strict than the parser
1579 * for a hierarchical URI).
1581 * NOTE:
1582 * Windows allows invalid % encoded data to appear in opaque URI paths
1583 * for unknown scheme types.
1585 * File schemes with USE_DOS_PATH set aren't allowed to have '<', '>', or '\"'
1586 * appear in them.
1588 static BOOL parse_path_opaque(const WCHAR **ptr, parse_data *data, DWORD flags) {
1589 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
1590 const BOOL is_file = data->scheme_type == URL_SCHEME_FILE;
1591 const BOOL is_mailto = data->scheme_type == URL_SCHEME_MAILTO;
1593 if (is_mailto && (*ptr)[0] == '/' && (*ptr)[1] == '/')
1595 if ((*ptr)[2]) data->path = *ptr + 2;
1596 else data->path = NULL;
1598 else
1599 data->path = *ptr;
1601 while(!is_path_delim(data->scheme_type, **ptr)) {
1602 if(**ptr == '%' && known_scheme) {
1603 if(!check_pct_encoded(ptr)) {
1604 *ptr = data->path;
1605 data->path = NULL;
1606 return FALSE;
1607 } else
1608 continue;
1609 } else if(is_forbidden_dos_path_char(**ptr) && is_file &&
1610 (flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
1611 *ptr = data->path;
1612 data->path = NULL;
1613 return FALSE;
1616 ++(*ptr);
1619 if (data->path) data->path_len = *ptr - data->path;
1620 TRACE("(%p %p %x): Parsed opaque URI path %s len=%d\n", ptr, data, flags,
1621 debugstr_wn(data->path, data->path_len), data->path_len);
1622 return TRUE;
1625 /* Determines how the URI should be parsed after the scheme information.
1627 * If the scheme is followed by "//", then it is treated as a hierarchical URI
1628 * which then the authority and path information will be parsed out. Otherwise, the
1629 * URI will be treated as an opaque URI which the authority information is not parsed
1630 * out.
1632 * RFC 3896 definition of hier-part:
1634 * hier-part = "//" authority path-abempty
1635 * / path-absolute
1636 * / path-rootless
1637 * / path-empty
1639 * MSDN opaque URI definition:
1640 * scheme ":" path [ "#" fragment ]
1642 * NOTES:
1643 * If the URI is of an unknown scheme type and has a "//" following the scheme then it
1644 * is treated as a hierarchical URI, but, if the CREATE_NO_CRACK_UNKNOWN_SCHEMES flag is
1645 * set then it is considered an opaque URI regardless of what follows the scheme information
1646 * (per MSDN documentation).
1648 static BOOL parse_hierpart(const WCHAR **ptr, parse_data *data, DWORD flags) {
1649 const WCHAR *start = *ptr;
1651 data->must_have_path = FALSE;
1653 /* For javascript: URIs, simply set everything as a path */
1654 if(data->scheme_type == URL_SCHEME_JAVASCRIPT) {
1655 data->path = *ptr;
1656 data->path_len = lstrlenW(*ptr);
1657 data->is_opaque = TRUE;
1658 *ptr += data->path_len;
1659 return TRUE;
1662 /* Checks if the authority information needs to be parsed. */
1663 if(is_hierarchical_uri(ptr, data)) {
1664 /* Only treat it as a hierarchical URI if the scheme_type is known or
1665 * the Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES flag is not set.
1667 if(data->scheme_type != URL_SCHEME_UNKNOWN ||
1668 !(flags & Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES)) {
1669 TRACE("(%p %p %x): Treating URI as an hierarchical URI.\n", ptr, data, flags);
1670 data->is_opaque = FALSE;
1672 if(data->scheme_type == URL_SCHEME_WILDCARD && !data->has_implicit_scheme) {
1673 if(**ptr == '/' && *(*ptr+1) == '/') {
1674 data->must_have_path = TRUE;
1675 *ptr += 2;
1679 /* TODO: Handle hierarchical URI's, parse authority then parse the path. */
1680 if(!parse_authority(ptr, data, flags))
1681 return FALSE;
1683 return parse_path_hierarchical(ptr, data, flags);
1684 } else
1685 /* Reset ptr to its starting position so opaque path parsing
1686 * begins at the correct location.
1688 *ptr = start;
1691 /* If it reaches here, then the URI will be treated as an opaque
1692 * URI.
1695 TRACE("(%p %p %x): Treating URI as an opaque URI.\n", ptr, data, flags);
1697 data->is_opaque = TRUE;
1698 if(!parse_path_opaque(ptr, data, flags))
1699 return FALSE;
1701 return TRUE;
1704 /* Attempts to parse the query string from the URI.
1706 * NOTES:
1707 * If NO_DECODE_EXTRA_INFO flag is set, then invalid percent encoded
1708 * data is allowed to appear in the query string. For unknown scheme types
1709 * invalid percent encoded data is allowed to appear regardless.
1711 static BOOL parse_query(const WCHAR **ptr, parse_data *data, DWORD flags) {
1712 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
1714 if(**ptr != '?') {
1715 TRACE("(%p %p %x): URI didn't contain a query string.\n", ptr, data, flags);
1716 return TRUE;
1719 data->query = *ptr;
1721 ++(*ptr);
1722 while(**ptr && **ptr != '#') {
1723 if(**ptr == '%' && known_scheme &&
1724 !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
1725 if(!check_pct_encoded(ptr)) {
1726 *ptr = data->query;
1727 data->query = NULL;
1728 return FALSE;
1729 } else
1730 continue;
1733 ++(*ptr);
1736 data->query_len = *ptr - data->query;
1738 TRACE("(%p %p %x): Parsed query string %s len=%d\n", ptr, data, flags,
1739 debugstr_wn(data->query, data->query_len), data->query_len);
1740 return TRUE;
1743 /* Attempts to parse the fragment from the URI.
1745 * NOTES:
1746 * If NO_DECODE_EXTRA_INFO flag is set, then invalid percent encoded
1747 * data is allowed to appear in the query string. For unknown scheme types
1748 * invalid percent encoded data is allowed to appear regardless.
1750 static BOOL parse_fragment(const WCHAR **ptr, parse_data *data, DWORD flags) {
1751 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
1753 if(**ptr != '#') {
1754 TRACE("(%p %p %x): URI didn't contain a fragment.\n", ptr, data, flags);
1755 return TRUE;
1758 data->fragment = *ptr;
1760 ++(*ptr);
1761 while(**ptr) {
1762 if(**ptr == '%' && known_scheme &&
1763 !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
1764 if(!check_pct_encoded(ptr)) {
1765 *ptr = data->fragment;
1766 data->fragment = NULL;
1767 return FALSE;
1768 } else
1769 continue;
1772 ++(*ptr);
1775 data->fragment_len = *ptr - data->fragment;
1777 TRACE("(%p %p %x): Parsed fragment %s len=%d\n", ptr, data, flags,
1778 debugstr_wn(data->fragment, data->fragment_len), data->fragment_len);
1779 return TRUE;
1782 /* Parses and validates the components of the specified by data->uri
1783 * and stores the information it parses into 'data'.
1785 * Returns TRUE if it successfully parsed the URI. False otherwise.
1787 static BOOL parse_uri(parse_data *data, DWORD flags) {
1788 const WCHAR *ptr;
1789 const WCHAR **pptr;
1791 ptr = data->uri;
1792 pptr = &ptr;
1794 TRACE("(%p %x): BEGINNING TO PARSE URI %s.\n", data, flags, debugstr_w(data->uri));
1796 if(!parse_scheme(pptr, data, flags, 0))
1797 return FALSE;
1799 if(!parse_hierpart(pptr, data, flags))
1800 return FALSE;
1802 if(!parse_query(pptr, data, flags))
1803 return FALSE;
1805 if(!parse_fragment(pptr, data, flags))
1806 return FALSE;
1808 TRACE("(%p %x): FINISHED PARSING URI.\n", data, flags);
1809 return TRUE;
1812 static BOOL canonicalize_username(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
1813 const WCHAR *ptr;
1815 if(!data->username) {
1816 uri->userinfo_start = -1;
1817 return TRUE;
1820 uri->userinfo_start = uri->canon_len;
1821 for(ptr = data->username; ptr < data->username+data->username_len; ++ptr) {
1822 if(*ptr == '%') {
1823 /* Only decode % encoded values for known scheme types. */
1824 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
1825 /* See if the value really needs decoding. */
1826 WCHAR val = decode_pct_val(ptr);
1827 if(is_unreserved(val)) {
1828 if(!computeOnly)
1829 uri->canon_uri[uri->canon_len] = val;
1831 ++uri->canon_len;
1833 /* Move pass the hex characters. */
1834 ptr += 2;
1835 continue;
1838 } else if(is_ascii(*ptr) && !is_reserved(*ptr) && !is_unreserved(*ptr) && *ptr != '\\') {
1839 /* Only percent encode forbidden characters if the NO_ENCODE_FORBIDDEN_CHARACTERS flag
1840 * is NOT set.
1842 if(!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS)) {
1843 if(!computeOnly)
1844 pct_encode_val(*ptr, uri->canon_uri + uri->canon_len);
1846 uri->canon_len += 3;
1847 continue;
1851 if(!computeOnly)
1852 /* Nothing special, so just copy the character over. */
1853 uri->canon_uri[uri->canon_len] = *ptr;
1854 ++uri->canon_len;
1857 return TRUE;
1860 static BOOL canonicalize_password(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
1861 const WCHAR *ptr;
1863 if(!data->password) {
1864 uri->userinfo_split = -1;
1865 return TRUE;
1868 if(uri->userinfo_start == -1)
1869 /* Has a password, but, doesn't have a username. */
1870 uri->userinfo_start = uri->canon_len;
1872 uri->userinfo_split = uri->canon_len - uri->userinfo_start;
1874 /* Add the ':' to the userinfo component. */
1875 if(!computeOnly)
1876 uri->canon_uri[uri->canon_len] = ':';
1877 ++uri->canon_len;
1879 for(ptr = data->password; ptr < data->password+data->password_len; ++ptr) {
1880 if(*ptr == '%') {
1881 /* Only decode % encoded values for known scheme types. */
1882 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
1883 /* See if the value really needs decoding. */
1884 WCHAR val = decode_pct_val(ptr);
1885 if(is_unreserved(val)) {
1886 if(!computeOnly)
1887 uri->canon_uri[uri->canon_len] = val;
1889 ++uri->canon_len;
1891 /* Move pass the hex characters. */
1892 ptr += 2;
1893 continue;
1896 } else if(is_ascii(*ptr) && !is_reserved(*ptr) && !is_unreserved(*ptr) && *ptr != '\\') {
1897 /* Only percent encode forbidden characters if the NO_ENCODE_FORBIDDEN_CHARACTERS flag
1898 * is NOT set.
1900 if(!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS)) {
1901 if(!computeOnly)
1902 pct_encode_val(*ptr, uri->canon_uri + uri->canon_len);
1904 uri->canon_len += 3;
1905 continue;
1909 if(!computeOnly)
1910 /* Nothing special, so just copy the character over. */
1911 uri->canon_uri[uri->canon_len] = *ptr;
1912 ++uri->canon_len;
1915 return TRUE;
1918 /* Canonicalizes the userinfo of the URI represented by the parse_data.
1920 * Canonicalization of the userinfo is a simple process. If there are any percent
1921 * encoded characters that fall in the "unreserved" character set, they are decoded
1922 * to their actual value. If a character is not in the "unreserved" or "reserved" sets
1923 * then it is percent encoded. Other than that the characters are copied over without
1924 * change.
1926 static BOOL canonicalize_userinfo(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
1927 uri->userinfo_start = uri->userinfo_split = -1;
1928 uri->userinfo_len = 0;
1930 if(!data->username && !data->password)
1931 /* URI doesn't have userinfo, so nothing to do here. */
1932 return TRUE;
1934 if(!canonicalize_username(data, uri, flags, computeOnly))
1935 return FALSE;
1937 if(!canonicalize_password(data, uri, flags, computeOnly))
1938 return FALSE;
1940 uri->userinfo_len = uri->canon_len - uri->userinfo_start;
1941 if(!computeOnly)
1942 TRACE("(%p %p %x %d): Canonicalized userinfo, userinfo_start=%d, userinfo=%s, userinfo_split=%d userinfo_len=%d.\n",
1943 data, uri, flags, computeOnly, uri->userinfo_start, debugstr_wn(uri->canon_uri + uri->userinfo_start, uri->userinfo_len),
1944 uri->userinfo_split, uri->userinfo_len);
1946 /* Now insert the '@' after the userinfo. */
1947 if(!computeOnly)
1948 uri->canon_uri[uri->canon_len] = '@';
1949 ++uri->canon_len;
1951 return TRUE;
1954 /* Attempts to canonicalize a reg_name.
1956 * Things that happen:
1957 * 1) If Uri_CREATE_NO_CANONICALIZE flag is not set, then the reg_name is
1958 * lower cased. Unless it's an unknown scheme type, which case it's
1959 * no lower cased regardless.
1961 * 2) Unreserved % encoded characters are decoded for known
1962 * scheme types.
1964 * 3) Forbidden characters are % encoded as long as
1965 * Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS flag is not set and
1966 * it isn't an unknown scheme type.
1968 * 4) If it's a file scheme and the host is "localhost" it's removed.
1970 * 5) If it's a file scheme and Uri_CREATE_FILE_USE_DOS_PATH is set,
1971 * then the UNC path characters are added before the host name.
1973 static BOOL canonicalize_reg_name(const parse_data *data, Uri *uri,
1974 DWORD flags, BOOL computeOnly) {
1975 static const WCHAR localhostW[] =
1976 {'l','o','c','a','l','h','o','s','t',0};
1977 const WCHAR *ptr;
1978 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
1980 if(data->scheme_type == URL_SCHEME_FILE &&
1981 data->host_len == lstrlenW(localhostW)) {
1982 if(!StrCmpNIW(data->host, localhostW, data->host_len)) {
1983 uri->host_start = -1;
1984 uri->host_len = 0;
1985 uri->host_type = Uri_HOST_UNKNOWN;
1986 return TRUE;
1990 if(data->scheme_type == URL_SCHEME_FILE && flags & Uri_CREATE_FILE_USE_DOS_PATH) {
1991 if(!computeOnly) {
1992 uri->canon_uri[uri->canon_len] = '\\';
1993 uri->canon_uri[uri->canon_len+1] = '\\';
1995 uri->canon_len += 2;
1996 uri->authority_start = uri->canon_len;
1999 uri->host_start = uri->canon_len;
2001 for(ptr = data->host; ptr < data->host+data->host_len; ++ptr) {
2002 if(*ptr == '%' && known_scheme) {
2003 WCHAR val = decode_pct_val(ptr);
2004 if(is_unreserved(val)) {
2005 /* If NO_CANONICALIZE is not set, then windows lower cases the
2006 * decoded value.
2008 if(!(flags & Uri_CREATE_NO_CANONICALIZE) && iswupper(val)) {
2009 if(!computeOnly)
2010 uri->canon_uri[uri->canon_len] = towlower(val);
2011 } else {
2012 if(!computeOnly)
2013 uri->canon_uri[uri->canon_len] = val;
2015 ++uri->canon_len;
2017 /* Skip past the % encoded character. */
2018 ptr += 2;
2019 continue;
2020 } else {
2021 /* Just copy the % over. */
2022 if(!computeOnly)
2023 uri->canon_uri[uri->canon_len] = *ptr;
2024 ++uri->canon_len;
2026 } else if(*ptr == '\\') {
2027 /* Only unknown scheme types could have made it here with a '\\' in the host name. */
2028 if(!computeOnly)
2029 uri->canon_uri[uri->canon_len] = *ptr;
2030 ++uri->canon_len;
2031 } else if(!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS) && is_ascii(*ptr) &&
2032 !is_unreserved(*ptr) && !is_reserved(*ptr) && known_scheme) {
2033 if(!computeOnly) {
2034 pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
2036 /* The percent encoded value gets lower cased also. */
2037 if(!(flags & Uri_CREATE_NO_CANONICALIZE)) {
2038 uri->canon_uri[uri->canon_len+1] = towlower(uri->canon_uri[uri->canon_len+1]);
2039 uri->canon_uri[uri->canon_len+2] = towlower(uri->canon_uri[uri->canon_len+2]);
2043 uri->canon_len += 3;
2044 } else {
2045 if(!computeOnly) {
2046 if(!(flags & Uri_CREATE_NO_CANONICALIZE) && known_scheme)
2047 uri->canon_uri[uri->canon_len] = towlower(*ptr);
2048 else
2049 uri->canon_uri[uri->canon_len] = *ptr;
2052 ++uri->canon_len;
2056 uri->host_len = uri->canon_len - uri->host_start;
2058 if(!computeOnly)
2059 TRACE("(%p %p %x %d): Canonicalize reg_name=%s len=%d\n", data, uri, flags,
2060 computeOnly, debugstr_wn(uri->canon_uri+uri->host_start, uri->host_len),
2061 uri->host_len);
2063 if(!computeOnly)
2064 find_domain_name(uri->canon_uri+uri->host_start, uri->host_len,
2065 &(uri->domain_offset));
2067 return TRUE;
2070 /* Attempts to canonicalize an implicit IPv4 address. */
2071 static BOOL canonicalize_implicit_ipv4address(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2072 uri->host_start = uri->canon_len;
2074 TRACE("%u\n", data->implicit_ipv4);
2075 /* For unknown scheme types Windows doesn't convert
2076 * the value into an IP address, but it still considers
2077 * it an IPv4 address.
2079 if(data->scheme_type == URL_SCHEME_UNKNOWN) {
2080 if(!computeOnly)
2081 memcpy(uri->canon_uri+uri->canon_len, data->host, data->host_len*sizeof(WCHAR));
2082 uri->canon_len += data->host_len;
2083 } else {
2084 if(!computeOnly)
2085 uri->canon_len += ui2ipv4(uri->canon_uri+uri->canon_len, data->implicit_ipv4);
2086 else
2087 uri->canon_len += ui2ipv4(NULL, data->implicit_ipv4);
2090 uri->host_len = uri->canon_len - uri->host_start;
2091 uri->host_type = Uri_HOST_IPV4;
2093 if(!computeOnly)
2094 TRACE("%p %p %x %d): Canonicalized implicit IP address=%s len=%d\n",
2095 data, uri, flags, computeOnly,
2096 debugstr_wn(uri->canon_uri+uri->host_start, uri->host_len),
2097 uri->host_len);
2099 return TRUE;
2102 /* Attempts to canonicalize an IPv4 address.
2104 * If the parse_data represents a URI that has an implicit IPv4 address
2105 * (ex. http://256/, this function will convert 256 into 0.0.1.0). If
2106 * the implicit IP address exceeds the value of UINT_MAX (maximum value
2107 * for an IPv4 address) it's canonicalized as if it were a reg-name.
2109 * If the parse_data contains a partial or full IPv4 address it normalizes it.
2110 * A partial IPv4 address is something like "192.0" and would be normalized to
2111 * "192.0.0.0". With a full (or partial) IPv4 address like "192.002.01.003" would
2112 * be normalized to "192.2.1.3".
2114 * NOTES:
2115 * Windows ONLY normalizes IPv4 address for known scheme types (one that isn't
2116 * URL_SCHEME_UNKNOWN). For unknown scheme types, it simply copies the data from
2117 * the original URI into the canonicalized URI, but, it still recognizes URI's
2118 * host type as HOST_IPV4.
2120 static BOOL canonicalize_ipv4address(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2121 if(data->has_implicit_ip)
2122 return canonicalize_implicit_ipv4address(data, uri, flags, computeOnly);
2123 else {
2124 uri->host_start = uri->canon_len;
2126 /* Windows only normalizes for known scheme types. */
2127 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
2128 /* parse_data contains a partial or full IPv4 address, so normalize it. */
2129 DWORD i, octetDigitCount = 0, octetCount = 0;
2130 BOOL octetHasDigit = FALSE;
2132 for(i = 0; i < data->host_len; ++i) {
2133 if(data->host[i] == '0' && !octetHasDigit) {
2134 /* Can ignore leading zeros if:
2135 * 1) It isn't the last digit of the octet.
2136 * 2) i+1 != data->host_len
2137 * 3) i+1 != '.'
2139 if(octetDigitCount == 2 ||
2140 i+1 == data->host_len ||
2141 data->host[i+1] == '.') {
2142 if(!computeOnly)
2143 uri->canon_uri[uri->canon_len] = data->host[i];
2144 ++uri->canon_len;
2145 TRACE("Adding zero\n");
2147 } else if(data->host[i] == '.') {
2148 if(!computeOnly)
2149 uri->canon_uri[uri->canon_len] = data->host[i];
2150 ++uri->canon_len;
2152 octetDigitCount = 0;
2153 octetHasDigit = FALSE;
2154 ++octetCount;
2155 } else {
2156 if(!computeOnly)
2157 uri->canon_uri[uri->canon_len] = data->host[i];
2158 ++uri->canon_len;
2160 ++octetDigitCount;
2161 octetHasDigit = TRUE;
2165 /* Make sure the canonicalized IP address has 4 dec-octets.
2166 * If doesn't add "0" ones until there is 4;
2168 for( ; octetCount < 3; ++octetCount) {
2169 if(!computeOnly) {
2170 uri->canon_uri[uri->canon_len] = '.';
2171 uri->canon_uri[uri->canon_len+1] = '0';
2174 uri->canon_len += 2;
2176 } else {
2177 /* Windows doesn't normalize addresses in unknown schemes. */
2178 if(!computeOnly)
2179 memcpy(uri->canon_uri+uri->canon_len, data->host, data->host_len*sizeof(WCHAR));
2180 uri->canon_len += data->host_len;
2183 uri->host_len = uri->canon_len - uri->host_start;
2184 if(!computeOnly)
2185 TRACE("(%p %p %x %d): Canonicalized IPv4 address, ip=%s len=%d\n",
2186 data, uri, flags, computeOnly,
2187 debugstr_wn(uri->canon_uri+uri->host_start, uri->host_len),
2188 uri->host_len);
2191 return TRUE;
2194 /* Attempts to canonicalize the IPv6 address of the URI.
2196 * Multiple things happen during the canonicalization of an IPv6 address:
2197 * 1) Any leading zero's in a h16 component are removed.
2198 * Ex: [0001:0022::] -> [1:22::]
2200 * 2) The longest sequence of zero h16 components are compressed
2201 * into a "::" (elision). If there's a tie, the first is chosen.
2203 * Ex: [0:0:0:0:1:6:7:8] -> [::1:6:7:8]
2204 * [0:0:0:0:1:2::] -> [::1:2:0:0]
2205 * [0:0:1:2:0:0:7:8] -> [::1:2:0:0:7:8]
2207 * 3) If an IPv4 address is attached to the IPv6 address, it's
2208 * also normalized.
2209 * Ex: [::001.002.022.000] -> [::1.2.22.0]
2211 * 4) If an elision is present, but, only represents one h16 component
2212 * it's expanded.
2214 * Ex: [1::2:3:4:5:6:7] -> [1:0:2:3:4:5:6:7]
2216 * 5) If the IPv6 address contains an IPv4 address and there exists
2217 * at least 1 non-zero h16 component the IPv4 address is converted
2218 * into two h16 components, otherwise it's normalized and kept as is.
2220 * Ex: [::192.200.003.4] -> [::192.200.3.4]
2221 * [ffff::192.200.003.4] -> [ffff::c0c8:3041]
2223 * NOTE:
2224 * For unknown scheme types Windows simply copies the address over without any
2225 * changes.
2227 * IPv4 address can be included in an elision if all its components are 0's.
2229 static BOOL canonicalize_ipv6address(const parse_data *data, Uri *uri,
2230 DWORD flags, BOOL computeOnly) {
2231 uri->host_start = uri->canon_len;
2233 if(data->scheme_type == URL_SCHEME_UNKNOWN) {
2234 if(!computeOnly)
2235 memcpy(uri->canon_uri+uri->canon_len, data->host, data->host_len*sizeof(WCHAR));
2236 uri->canon_len += data->host_len;
2237 } else {
2238 WCHAR buffer[46];
2239 ULONG size = ARRAY_SIZE(buffer);
2241 if(computeOnly) {
2242 RtlIpv6AddressToStringExW(&data->ipv6_address, 0, 0, buffer, &size);
2243 uri->canon_len += size + 1;
2244 } else {
2245 uri->canon_uri[uri->canon_len++] = '[';
2246 RtlIpv6AddressToStringExW(&data->ipv6_address, 0, 0, uri->canon_uri + uri->canon_len, &size);
2247 uri->canon_len += size - 1;
2248 uri->canon_uri[uri->canon_len++] = ']';
2252 uri->host_len = uri->canon_len - uri->host_start;
2254 if(!computeOnly)
2255 TRACE("(%p %p %x %d): Canonicalized IPv6 address %s, len=%d\n", data, uri, flags,
2256 computeOnly, debugstr_wn(uri->canon_uri+uri->host_start, uri->host_len),
2257 uri->host_len);
2259 return TRUE;
2262 /* Attempts to canonicalize the host of the URI (if any). */
2263 static BOOL canonicalize_host(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2264 uri->host_start = -1;
2265 uri->host_len = 0;
2266 uri->domain_offset = -1;
2268 if(data->host) {
2269 switch(data->host_type) {
2270 case Uri_HOST_DNS:
2271 uri->host_type = Uri_HOST_DNS;
2272 if(!canonicalize_reg_name(data, uri, flags, computeOnly))
2273 return FALSE;
2275 break;
2276 case Uri_HOST_IPV4:
2277 uri->host_type = Uri_HOST_IPV4;
2278 if(!canonicalize_ipv4address(data, uri, flags, computeOnly))
2279 return FALSE;
2281 break;
2282 case Uri_HOST_IPV6:
2283 if(!canonicalize_ipv6address(data, uri, flags, computeOnly))
2284 return FALSE;
2286 uri->host_type = Uri_HOST_IPV6;
2287 break;
2288 case Uri_HOST_UNKNOWN:
2289 if(data->host_len > 0 || data->scheme_type != URL_SCHEME_FILE) {
2290 uri->host_start = uri->canon_len;
2292 /* Nothing happens to unknown host types. */
2293 if(!computeOnly)
2294 memcpy(uri->canon_uri+uri->canon_len, data->host, data->host_len*sizeof(WCHAR));
2295 uri->canon_len += data->host_len;
2296 uri->host_len = data->host_len;
2299 uri->host_type = Uri_HOST_UNKNOWN;
2300 break;
2301 default:
2302 FIXME("(%p %p %x %d): Canonicalization for host type %d not supported.\n", data,
2303 uri, flags, computeOnly, data->host_type);
2304 return FALSE;
2308 return TRUE;
2311 static BOOL canonicalize_port(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2312 BOOL has_default_port = FALSE;
2313 USHORT default_port = 0;
2314 DWORD i;
2316 uri->port_offset = -1;
2318 /* Check if the scheme has a default port. */
2319 for(i = 0; i < ARRAY_SIZE(default_ports); ++i) {
2320 if(default_ports[i].scheme == data->scheme_type) {
2321 has_default_port = TRUE;
2322 default_port = default_ports[i].port;
2323 break;
2327 uri->has_port = data->has_port || has_default_port;
2329 /* Possible cases:
2330 * 1) Has a port which is the default port.
2331 * 2) Has a port (not the default).
2332 * 3) Doesn't have a port, but, scheme has a default port.
2333 * 4) No port.
2335 if(has_default_port && data->has_port && data->port_value == default_port) {
2336 /* If it's the default port and this flag isn't set, don't do anything. */
2337 if(flags & Uri_CREATE_NO_CANONICALIZE) {
2338 uri->port_offset = uri->canon_len-uri->authority_start;
2339 if(!computeOnly)
2340 uri->canon_uri[uri->canon_len] = ':';
2341 ++uri->canon_len;
2343 if(data->port) {
2344 /* Copy the original port over. */
2345 if(!computeOnly)
2346 memcpy(uri->canon_uri+uri->canon_len, data->port, data->port_len*sizeof(WCHAR));
2347 uri->canon_len += data->port_len;
2348 } else {
2349 if(!computeOnly)
2350 uri->canon_len += ui2str(uri->canon_uri+uri->canon_len, data->port_value);
2351 else
2352 uri->canon_len += ui2str(NULL, data->port_value);
2356 uri->port = default_port;
2357 } else if(data->has_port) {
2358 uri->port_offset = uri->canon_len-uri->authority_start;
2359 if(!computeOnly)
2360 uri->canon_uri[uri->canon_len] = ':';
2361 ++uri->canon_len;
2363 if(flags & Uri_CREATE_NO_CANONICALIZE && data->port) {
2364 /* Copy the original over without changes. */
2365 if(!computeOnly)
2366 memcpy(uri->canon_uri+uri->canon_len, data->port, data->port_len*sizeof(WCHAR));
2367 uri->canon_len += data->port_len;
2368 } else {
2369 if(!computeOnly)
2370 uri->canon_len += ui2str(uri->canon_uri+uri->canon_len, data->port_value);
2371 else
2372 uri->canon_len += ui2str(NULL, data->port_value);
2375 uri->port = data->port_value;
2376 } else if(has_default_port)
2377 uri->port = default_port;
2379 return TRUE;
2382 /* Canonicalizes the authority of the URI represented by the parse_data. */
2383 static BOOL canonicalize_authority(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2384 uri->authority_start = uri->canon_len;
2385 uri->authority_len = 0;
2387 if(!canonicalize_userinfo(data, uri, flags, computeOnly))
2388 return FALSE;
2390 if(!canonicalize_host(data, uri, flags, computeOnly))
2391 return FALSE;
2393 if(!canonicalize_port(data, uri, flags, computeOnly))
2394 return FALSE;
2396 if(uri->host_start != -1 || (data->is_relative && (data->password || data->username)))
2397 uri->authority_len = uri->canon_len - uri->authority_start;
2398 else
2399 uri->authority_start = -1;
2401 return TRUE;
2404 /* Attempts to canonicalize the path of a hierarchical URI.
2406 * Things that happen:
2407 * 1). Forbidden characters are percent encoded, unless the NO_ENCODE_FORBIDDEN
2408 * flag is set or it's a file URI. Forbidden characters are always encoded
2409 * for file schemes regardless and forbidden characters are never encoded
2410 * for unknown scheme types.
2412 * 2). For known scheme types '\\' are changed to '/'.
2414 * 3). Percent encoded, unreserved characters are decoded to their actual values.
2415 * Unless the scheme type is unknown. For file schemes any percent encoded
2416 * character in the unreserved or reserved set is decoded.
2418 * 4). For File schemes if the path is starts with a drive letter and doesn't
2419 * start with a '/' then one is appended.
2420 * Ex: file://c:/test.mp3 -> file:///c:/test.mp3
2422 * 5). Dot segments are removed from the path for all scheme types
2423 * unless NO_CANONICALIZE flag is set. Dot segments aren't removed
2424 * for wildcard scheme types.
2426 * NOTES:
2427 * file://c:/test%20test -> file:///c:/test%2520test
2428 * file://c:/test%3Etest -> file:///c:/test%253Etest
2429 * if Uri_CREATE_FILE_USE_DOS_PATH is not set:
2430 * file:///c:/test%20test -> file:///c:/test%20test
2431 * file:///c:/test%test -> file:///c:/test%25test
2433 static DWORD canonicalize_path_hierarchical(const WCHAR *path, DWORD path_len, URL_SCHEME scheme_type, BOOL has_host, DWORD flags,
2434 BOOL is_implicit_scheme, WCHAR *ret_path) {
2435 const BOOL known_scheme = scheme_type != URL_SCHEME_UNKNOWN;
2436 const BOOL is_file = scheme_type == URL_SCHEME_FILE;
2437 const BOOL is_res = scheme_type == URL_SCHEME_RES;
2438 const WCHAR *ptr;
2439 BOOL escape_pct = FALSE;
2440 DWORD len = 0;
2442 if(!path)
2443 return 0;
2445 ptr = path;
2447 if(is_file && !has_host) {
2448 /* Check if a '/' needs to be appended for the file scheme. */
2449 if(path_len > 1 && is_drive_path(ptr) && !(flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
2450 if(ret_path)
2451 ret_path[len] = '/';
2452 len++;
2453 escape_pct = TRUE;
2454 } else if(*ptr == '/') {
2455 if(!(flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
2456 /* Copy the extra '/' over. */
2457 if(ret_path)
2458 ret_path[len] = '/';
2459 len++;
2461 ++ptr;
2464 if(is_drive_path(ptr)) {
2465 if(ret_path) {
2466 ret_path[len] = *ptr;
2467 /* If there's a '|' after the drive letter, convert it to a ':'. */
2468 ret_path[len+1] = ':';
2470 ptr += 2;
2471 len += 2;
2475 if(!is_file && *path && *path != '/') {
2476 /* Prepend a '/' to the path if it doesn't have one. */
2477 if(ret_path)
2478 ret_path[len] = '/';
2479 len++;
2482 for(; ptr < path+path_len; ++ptr) {
2483 BOOL do_default_action = TRUE;
2485 if(*ptr == '%' && !is_res) {
2486 const WCHAR *tmp = ptr;
2487 WCHAR val;
2489 /* Check if the % represents a valid encoded char, or if it needs encoding. */
2490 BOOL force_encode = !check_pct_encoded(&tmp) && is_file && !(flags&Uri_CREATE_FILE_USE_DOS_PATH);
2491 val = decode_pct_val(ptr);
2493 if(force_encode || escape_pct) {
2494 /* Escape the percent sign in the file URI. */
2495 if(ret_path)
2496 pct_encode_val(*ptr, ret_path+len);
2497 len += 3;
2498 do_default_action = FALSE;
2499 } else if((is_unreserved(val) && known_scheme) ||
2500 (is_file && !is_implicit_scheme && (is_unreserved(val) || is_reserved(val) ||
2501 (val && flags&Uri_CREATE_FILE_USE_DOS_PATH && !is_forbidden_dos_path_char(val))))) {
2502 if(ret_path)
2503 ret_path[len] = val;
2504 len++;
2506 ptr += 2;
2507 continue;
2509 } else if(*ptr == '/' && is_file && (flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
2510 /* Convert the '/' back to a '\\'. */
2511 if(ret_path)
2512 ret_path[len] = '\\';
2513 len++;
2514 do_default_action = FALSE;
2515 } else if(*ptr == '\\' && known_scheme) {
2516 if(!(is_file && (flags & Uri_CREATE_FILE_USE_DOS_PATH))) {
2517 /* Convert '\\' into a '/'. */
2518 if(ret_path)
2519 ret_path[len] = '/';
2520 len++;
2521 do_default_action = FALSE;
2523 } else if(known_scheme && !is_res && is_ascii(*ptr) && !is_unreserved(*ptr) && !is_reserved(*ptr) &&
2524 (!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS) || is_file)) {
2525 if(!is_file || !(flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
2526 /* Escape the forbidden character. */
2527 if(ret_path)
2528 pct_encode_val(*ptr, ret_path+len);
2529 len += 3;
2530 do_default_action = FALSE;
2534 if(do_default_action) {
2535 if(ret_path)
2536 ret_path[len] = *ptr;
2537 len++;
2541 /* Removing the dot segments only happens when it's not in
2542 * computeOnly mode and it's not a wildcard scheme. File schemes
2543 * with USE_DOS_PATH set don't get dot segments removed.
2545 if(!(is_file && (flags & Uri_CREATE_FILE_USE_DOS_PATH)) &&
2546 scheme_type != URL_SCHEME_WILDCARD) {
2547 if(!(flags & Uri_CREATE_NO_CANONICALIZE) && ret_path) {
2548 /* Remove the dot segments (if any) and reset everything to the new
2549 * correct length.
2551 len = remove_dot_segments(ret_path, len);
2555 if(ret_path)
2556 TRACE("Canonicalized path %s len=%d\n", debugstr_wn(ret_path, len), len);
2557 return len;
2560 /* Attempts to canonicalize the path for an opaque URI.
2562 * For known scheme types:
2563 * 1) forbidden characters are percent encoded if
2564 * NO_ENCODE_FORBIDDEN_CHARACTERS isn't set.
2566 * 2) Percent encoded, unreserved characters are decoded
2567 * to their actual values, for known scheme types.
2569 * 3) '\\' are changed to '/' for known scheme types
2570 * except for mailto schemes.
2572 * 4) For file schemes, if USE_DOS_PATH is set all '/'
2573 * are converted to backslashes.
2575 * 5) For file schemes, if USE_DOS_PATH isn't set all '\'
2576 * are converted to forward slashes.
2578 static BOOL canonicalize_path_opaque(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2579 const WCHAR *ptr;
2580 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
2581 const BOOL is_file = data->scheme_type == URL_SCHEME_FILE;
2582 const BOOL is_mk = data->scheme_type == URL_SCHEME_MK;
2584 if(!data->path) {
2585 uri->path_start = -1;
2586 uri->path_len = 0;
2587 return TRUE;
2590 uri->path_start = uri->canon_len;
2592 if(is_mk){
2593 /* hijack this flag for SCHEME_MK to tell the function when to start
2594 * converting slashes */
2595 flags |= Uri_CREATE_FILE_USE_DOS_PATH;
2598 /* For javascript: URIs, simply copy path part without any canonicalization */
2599 if(data->scheme_type == URL_SCHEME_JAVASCRIPT) {
2600 if(!computeOnly)
2601 memcpy(uri->canon_uri+uri->canon_len, data->path, data->path_len*sizeof(WCHAR));
2602 uri->path_len = data->path_len;
2603 uri->canon_len += data->path_len;
2604 return TRUE;
2607 /* Windows doesn't allow a "//" to appear after the scheme
2608 * of a URI, if it's an opaque URI.
2610 if(data->scheme && *(data->path) == '/' && *(data->path+1) == '/') {
2611 /* So it inserts a "/." before the "//" if it exists. */
2612 if(!computeOnly) {
2613 uri->canon_uri[uri->canon_len] = '/';
2614 uri->canon_uri[uri->canon_len+1] = '.';
2617 uri->canon_len += 2;
2620 for(ptr = data->path; ptr < data->path+data->path_len; ++ptr) {
2621 BOOL do_default_action = TRUE;
2623 if(*ptr == '%' && known_scheme) {
2624 WCHAR val = decode_pct_val(ptr);
2626 if(is_unreserved(val)) {
2627 if(!computeOnly)
2628 uri->canon_uri[uri->canon_len] = val;
2629 ++uri->canon_len;
2631 ptr += 2;
2632 continue;
2634 } else if(*ptr == '/' && is_file && (flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
2635 if(!computeOnly)
2636 uri->canon_uri[uri->canon_len] = '\\';
2637 ++uri->canon_len;
2638 do_default_action = FALSE;
2639 } else if(*ptr == '\\') {
2640 if((data->is_relative || is_mk || is_file) && !(flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
2641 /* Convert to a '/'. */
2642 if(!computeOnly)
2643 uri->canon_uri[uri->canon_len] = '/';
2644 ++uri->canon_len;
2645 do_default_action = FALSE;
2647 } else if(is_mk && *ptr == ':' && ptr + 1 < data->path + data->path_len && *(ptr + 1) == ':') {
2648 flags &= ~Uri_CREATE_FILE_USE_DOS_PATH;
2649 } else if(known_scheme && is_ascii(*ptr) && !is_unreserved(*ptr) && !is_reserved(*ptr) &&
2650 !(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS)) {
2651 if(!(is_file && (flags & Uri_CREATE_FILE_USE_DOS_PATH))) {
2652 if(!computeOnly)
2653 pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
2654 uri->canon_len += 3;
2655 do_default_action = FALSE;
2659 if(do_default_action) {
2660 if(!computeOnly)
2661 uri->canon_uri[uri->canon_len] = *ptr;
2662 ++uri->canon_len;
2666 if(is_mk && !computeOnly && !(flags & Uri_CREATE_NO_CANONICALIZE)) {
2667 DWORD new_len = remove_dot_segments(uri->canon_uri + uri->path_start,
2668 uri->canon_len - uri->path_start);
2669 uri->canon_len = uri->path_start + new_len;
2672 uri->path_len = uri->canon_len - uri->path_start;
2674 if(!computeOnly)
2675 TRACE("(%p %p %x %d): Canonicalized opaque URI path %s len=%d\n", data, uri, flags, computeOnly,
2676 debugstr_wn(uri->canon_uri+uri->path_start, uri->path_len), uri->path_len);
2677 return TRUE;
2680 /* Determines how the URI represented by the parse_data should be canonicalized.
2682 * Essentially, if the parse_data represents an hierarchical URI then it calls
2683 * canonicalize_authority and the canonicalization functions for the path. If the
2684 * URI is opaque it canonicalizes the path of the URI.
2686 static BOOL canonicalize_hierpart(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2687 if(!data->is_opaque || (data->is_relative && (data->password || data->username))) {
2688 /* "//" is only added for non-wildcard scheme types.
2690 * A "//" is only added to a relative URI if it has a
2691 * host or port component (this only happens if a IUriBuilder
2692 * is generating an IUri).
2694 if((data->is_relative && (data->host || data->has_port)) ||
2695 (!data->is_relative && data->scheme_type != URL_SCHEME_WILDCARD)) {
2696 if(data->scheme_type == URL_SCHEME_WILDCARD)
2697 FIXME("Here\n");
2699 if(!computeOnly) {
2700 INT pos = uri->canon_len;
2702 uri->canon_uri[pos] = '/';
2703 uri->canon_uri[pos+1] = '/';
2705 uri->canon_len += 2;
2708 if(!canonicalize_authority(data, uri, flags, computeOnly))
2709 return FALSE;
2711 if(data->is_relative && (data->password || data->username)) {
2712 if(!canonicalize_path_opaque(data, uri, flags, computeOnly))
2713 return FALSE;
2714 } else {
2715 if(!computeOnly)
2716 uri->path_start = uri->canon_len;
2717 uri->path_len = canonicalize_path_hierarchical(data->path, data->path_len, data->scheme_type, data->host_len != 0,
2718 flags, data->has_implicit_scheme, computeOnly ? NULL : uri->canon_uri+uri->canon_len);
2719 uri->canon_len += uri->path_len;
2720 if(!computeOnly && !uri->path_len)
2721 uri->path_start = -1;
2723 } else {
2724 /* Opaque URI's don't have an authority. */
2725 uri->userinfo_start = uri->userinfo_split = -1;
2726 uri->userinfo_len = 0;
2727 uri->host_start = -1;
2728 uri->host_len = 0;
2729 uri->host_type = Uri_HOST_UNKNOWN;
2730 uri->has_port = FALSE;
2731 uri->authority_start = -1;
2732 uri->authority_len = 0;
2733 uri->domain_offset = -1;
2734 uri->port_offset = -1;
2736 if(is_hierarchical_scheme(data->scheme_type)) {
2737 DWORD i;
2739 /* Absolute URIs aren't displayed for known scheme types
2740 * which should be hierarchical URIs.
2742 uri->display_modifiers |= URI_DISPLAY_NO_ABSOLUTE_URI;
2744 /* Windows also sets the port for these (if they have one). */
2745 for(i = 0; i < ARRAY_SIZE(default_ports); ++i) {
2746 if(data->scheme_type == default_ports[i].scheme) {
2747 uri->has_port = TRUE;
2748 uri->port = default_ports[i].port;
2749 break;
2754 if(!canonicalize_path_opaque(data, uri, flags, computeOnly))
2755 return FALSE;
2758 if(uri->path_start > -1 && !computeOnly)
2759 /* Finding file extensions happens for both types of URIs. */
2760 uri->extension_offset = find_file_extension(uri->canon_uri+uri->path_start, uri->path_len);
2761 else
2762 uri->extension_offset = -1;
2764 return TRUE;
2767 /* Attempts to canonicalize the query string of the URI.
2769 * Things that happen:
2770 * 1) For known scheme types forbidden characters
2771 * are percent encoded, unless the NO_DECODE_EXTRA_INFO flag is set
2772 * or NO_ENCODE_FORBIDDEN_CHARACTERS is set.
2774 * 2) For known scheme types, percent encoded, unreserved characters
2775 * are decoded as long as the NO_DECODE_EXTRA_INFO flag isn't set.
2777 static BOOL canonicalize_query(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2778 const WCHAR *ptr, *end;
2779 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
2781 if(!data->query) {
2782 uri->query_start = -1;
2783 uri->query_len = 0;
2784 return TRUE;
2787 uri->query_start = uri->canon_len;
2789 end = data->query+data->query_len;
2790 for(ptr = data->query; ptr < end; ++ptr) {
2791 if(*ptr == '%') {
2792 if(known_scheme && !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
2793 WCHAR val = decode_pct_val(ptr);
2794 if(is_unreserved(val)) {
2795 if(!computeOnly)
2796 uri->canon_uri[uri->canon_len] = val;
2797 ++uri->canon_len;
2799 ptr += 2;
2800 continue;
2803 } else if(known_scheme && is_ascii(*ptr) && !is_unreserved(*ptr) && !is_reserved(*ptr)) {
2804 if(!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS) &&
2805 !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
2806 if(!computeOnly)
2807 pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
2808 uri->canon_len += 3;
2809 continue;
2813 if(!computeOnly)
2814 uri->canon_uri[uri->canon_len] = *ptr;
2815 ++uri->canon_len;
2818 uri->query_len = uri->canon_len - uri->query_start;
2820 if(!computeOnly)
2821 TRACE("(%p %p %x %d): Canonicalized query string %s len=%d\n", data, uri, flags,
2822 computeOnly, debugstr_wn(uri->canon_uri+uri->query_start, uri->query_len),
2823 uri->query_len);
2824 return TRUE;
2827 static BOOL canonicalize_fragment(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2828 const WCHAR *ptr, *end;
2829 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
2831 if(!data->fragment) {
2832 uri->fragment_start = -1;
2833 uri->fragment_len = 0;
2834 return TRUE;
2837 uri->fragment_start = uri->canon_len;
2839 end = data->fragment + data->fragment_len;
2840 for(ptr = data->fragment; ptr < end; ++ptr) {
2841 if(*ptr == '%') {
2842 if(known_scheme && !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
2843 WCHAR val = decode_pct_val(ptr);
2844 if(is_unreserved(val)) {
2845 if(!computeOnly)
2846 uri->canon_uri[uri->canon_len] = val;
2847 ++uri->canon_len;
2849 ptr += 2;
2850 continue;
2853 } else if(known_scheme && is_ascii(*ptr) && !is_unreserved(*ptr) && !is_reserved(*ptr)) {
2854 if(!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS) &&
2855 !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
2856 if(!computeOnly)
2857 pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
2858 uri->canon_len += 3;
2859 continue;
2863 if(!computeOnly)
2864 uri->canon_uri[uri->canon_len] = *ptr;
2865 ++uri->canon_len;
2868 uri->fragment_len = uri->canon_len - uri->fragment_start;
2870 if(!computeOnly)
2871 TRACE("(%p %p %x %d): Canonicalized fragment %s len=%d\n", data, uri, flags,
2872 computeOnly, debugstr_wn(uri->canon_uri+uri->fragment_start, uri->fragment_len),
2873 uri->fragment_len);
2874 return TRUE;
2877 /* Canonicalizes the scheme information specified in the parse_data using the specified flags. */
2878 static BOOL canonicalize_scheme(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2879 uri->scheme_start = -1;
2880 uri->scheme_len = 0;
2882 if(!data->scheme) {
2883 /* The only type of URI that doesn't have to have a scheme is a relative
2884 * URI.
2886 if(!data->is_relative) {
2887 FIXME("(%p %p %x): Unable to determine the scheme type of %s.\n", data,
2888 uri, flags, debugstr_w(data->uri));
2889 return FALSE;
2891 } else {
2892 if(!computeOnly) {
2893 DWORD i;
2894 INT pos = uri->canon_len;
2896 for(i = 0; i < data->scheme_len; ++i) {
2897 /* Scheme name must be lower case after canonicalization. */
2898 uri->canon_uri[i + pos] = towlower(data->scheme[i]);
2901 uri->canon_uri[i + pos] = ':';
2902 uri->scheme_start = pos;
2904 TRACE("(%p %p %x): Canonicalized scheme=%s, len=%d.\n", data, uri, flags,
2905 debugstr_wn(uri->canon_uri+uri->scheme_start, data->scheme_len), data->scheme_len);
2908 /* This happens in both computation modes. */
2909 uri->canon_len += data->scheme_len + 1;
2910 uri->scheme_len = data->scheme_len;
2912 return TRUE;
2915 /* Computes what the length of the URI specified by the parse_data will be
2916 * after canonicalization occurs using the specified flags.
2918 * This function will return a non-zero value indicating the length of the canonicalized
2919 * URI, or -1 on error.
2921 static int compute_canonicalized_length(const parse_data *data, DWORD flags) {
2922 Uri uri;
2924 memset(&uri, 0, sizeof(Uri));
2926 TRACE("(%p %x): Beginning to compute canonicalized length for URI %s\n", data, flags,
2927 debugstr_w(data->uri));
2929 if(!canonicalize_scheme(data, &uri, flags, TRUE)) {
2930 ERR("(%p %x): Failed to compute URI scheme length.\n", data, flags);
2931 return -1;
2934 if(!canonicalize_hierpart(data, &uri, flags, TRUE)) {
2935 ERR("(%p %x): Failed to compute URI hierpart length.\n", data, flags);
2936 return -1;
2939 if(!canonicalize_query(data, &uri, flags, TRUE)) {
2940 ERR("(%p %x): Failed to compute query string length.\n", data, flags);
2941 return -1;
2944 if(!canonicalize_fragment(data, &uri, flags, TRUE)) {
2945 ERR("(%p %x): Failed to compute fragment length.\n", data, flags);
2946 return -1;
2949 TRACE("(%p %x): Finished computing canonicalized URI length. length=%d\n", data, flags, uri.canon_len);
2951 return uri.canon_len;
2954 /* Canonicalizes the URI data specified in the parse_data, using the given flags. If the
2955 * canonicalization succeeds it will store all the canonicalization information
2956 * in the pointer to the Uri.
2958 * To canonicalize a URI this function first computes what the length of the URI
2959 * specified by the parse_data will be. Once this is done it will then perform the actual
2960 * canonicalization of the URI.
2962 static HRESULT canonicalize_uri(const parse_data *data, Uri *uri, DWORD flags) {
2963 INT len;
2965 uri->canon_uri = NULL;
2966 uri->canon_size = uri->canon_len = 0;
2968 TRACE("(%p %p %x): beginning to canonicalize URI %s.\n", data, uri, flags, debugstr_w(data->uri));
2970 /* First try to compute the length of the URI. */
2971 len = compute_canonicalized_length(data, flags);
2972 if(len == -1) {
2973 ERR("(%p %p %x): Could not compute the canonicalized length of %s.\n", data, uri, flags,
2974 debugstr_w(data->uri));
2975 return E_INVALIDARG;
2978 uri->canon_uri = heap_alloc((len+1)*sizeof(WCHAR));
2979 if(!uri->canon_uri)
2980 return E_OUTOFMEMORY;
2982 uri->canon_size = len;
2983 if(!canonicalize_scheme(data, uri, flags, FALSE)) {
2984 ERR("(%p %p %x): Unable to canonicalize the scheme of the URI.\n", data, uri, flags);
2985 return E_INVALIDARG;
2987 uri->scheme_type = data->scheme_type;
2989 if(!canonicalize_hierpart(data, uri, flags, FALSE)) {
2990 ERR("(%p %p %x): Unable to canonicalize the hierpart of the URI\n", data, uri, flags);
2991 return E_INVALIDARG;
2994 if(!canonicalize_query(data, uri, flags, FALSE)) {
2995 ERR("(%p %p %x): Unable to canonicalize query string of the URI.\n",
2996 data, uri, flags);
2997 return E_INVALIDARG;
3000 if(!canonicalize_fragment(data, uri, flags, FALSE)) {
3001 ERR("(%p %p %x): Unable to canonicalize fragment of the URI.\n",
3002 data, uri, flags);
3003 return E_INVALIDARG;
3006 /* There's a possibility we didn't use all the space we allocated
3007 * earlier.
3009 if(uri->canon_len < uri->canon_size) {
3010 /* This happens if the URI is hierarchical and dot
3011 * segments were removed from its path.
3013 WCHAR *tmp = heap_realloc(uri->canon_uri, (uri->canon_len+1)*sizeof(WCHAR));
3014 if(!tmp)
3015 return E_OUTOFMEMORY;
3017 uri->canon_uri = tmp;
3018 uri->canon_size = uri->canon_len;
3021 uri->canon_uri[uri->canon_len] = '\0';
3022 TRACE("(%p %p %x): finished canonicalizing the URI. uri=%s\n", data, uri, flags, debugstr_w(uri->canon_uri));
3024 return S_OK;
3027 static HRESULT get_builder_component(LPWSTR *component, DWORD *component_len,
3028 LPCWSTR source, DWORD source_len,
3029 LPCWSTR *output, DWORD *output_len)
3031 if(!output_len) {
3032 if(output)
3033 *output = NULL;
3034 return E_POINTER;
3037 if(!output) {
3038 *output_len = 0;
3039 return E_POINTER;
3042 if(!(*component) && source) {
3043 /* Allocate 'component', and copy the contents from 'source'
3044 * into the new allocation.
3046 *component = heap_alloc((source_len+1)*sizeof(WCHAR));
3047 if(!(*component))
3048 return E_OUTOFMEMORY;
3050 memcpy(*component, source, source_len*sizeof(WCHAR));
3051 (*component)[source_len] = '\0';
3052 *component_len = source_len;
3055 *output = *component;
3056 *output_len = *component_len;
3057 return *output ? S_OK : S_FALSE;
3060 /* Allocates 'component' and copies the string from 'new_value' into 'component'.
3061 * If 'prefix' is set and 'new_value' isn't NULL, then it checks if 'new_value'
3062 * starts with 'prefix'. If it doesn't then 'prefix' is prepended to 'component'.
3064 * If everything is successful, then will set 'success_flag' in 'flags'.
3066 static HRESULT set_builder_component(LPWSTR *component, DWORD *component_len, LPCWSTR new_value,
3067 WCHAR prefix, DWORD *flags, DWORD success_flag)
3069 heap_free(*component);
3071 if(!new_value) {
3072 *component = NULL;
3073 *component_len = 0;
3074 } else {
3075 BOOL add_prefix = FALSE;
3076 DWORD len = lstrlenW(new_value);
3077 DWORD pos = 0;
3079 if(prefix && *new_value != prefix) {
3080 add_prefix = TRUE;
3081 *component = heap_alloc((len+2)*sizeof(WCHAR));
3082 } else
3083 *component = heap_alloc((len+1)*sizeof(WCHAR));
3085 if(!(*component))
3086 return E_OUTOFMEMORY;
3088 if(add_prefix)
3089 (*component)[pos++] = prefix;
3091 memcpy(*component+pos, new_value, (len+1)*sizeof(WCHAR));
3092 *component_len = len+pos;
3095 *flags |= success_flag;
3096 return S_OK;
3099 static void reset_builder(UriBuilder *builder) {
3100 if(builder->uri)
3101 IUri_Release(&builder->uri->IUri_iface);
3102 builder->uri = NULL;
3104 heap_free(builder->fragment);
3105 builder->fragment = NULL;
3106 builder->fragment_len = 0;
3108 heap_free(builder->host);
3109 builder->host = NULL;
3110 builder->host_len = 0;
3112 heap_free(builder->password);
3113 builder->password = NULL;
3114 builder->password_len = 0;
3116 heap_free(builder->path);
3117 builder->path = NULL;
3118 builder->path_len = 0;
3120 heap_free(builder->query);
3121 builder->query = NULL;
3122 builder->query_len = 0;
3124 heap_free(builder->scheme);
3125 builder->scheme = NULL;
3126 builder->scheme_len = 0;
3128 heap_free(builder->username);
3129 builder->username = NULL;
3130 builder->username_len = 0;
3132 builder->has_port = FALSE;
3133 builder->port = 0;
3134 builder->modified_props = 0;
3137 static HRESULT validate_scheme_name(const UriBuilder *builder, parse_data *data, DWORD flags) {
3138 const WCHAR *component;
3139 const WCHAR *ptr;
3140 const WCHAR **pptr;
3141 DWORD expected_len;
3143 if(builder->scheme) {
3144 ptr = builder->scheme;
3145 expected_len = builder->scheme_len;
3146 } else if(builder->uri && builder->uri->scheme_start > -1) {
3147 ptr = builder->uri->canon_uri+builder->uri->scheme_start;
3148 expected_len = builder->uri->scheme_len;
3149 } else {
3150 static const WCHAR nullW[] = {0};
3151 ptr = nullW;
3152 expected_len = 0;
3155 component = ptr;
3156 pptr = &ptr;
3157 if(parse_scheme(pptr, data, flags, ALLOW_NULL_TERM_SCHEME) &&
3158 data->scheme_len == expected_len) {
3159 if(data->scheme)
3160 TRACE("(%p %p %x): Found valid scheme component %s len=%d.\n", builder, data, flags,
3161 debugstr_wn(data->scheme, data->scheme_len), data->scheme_len);
3162 } else {
3163 TRACE("(%p %p %x): Invalid scheme component found %s.\n", builder, data, flags,
3164 debugstr_wn(component, expected_len));
3165 return INET_E_INVALID_URL;
3168 return S_OK;
3171 static HRESULT validate_username(const UriBuilder *builder, parse_data *data, DWORD flags) {
3172 const WCHAR *ptr;
3173 const WCHAR **pptr;
3174 DWORD expected_len;
3176 if(builder->username) {
3177 ptr = builder->username;
3178 expected_len = builder->username_len;
3179 } else if(!(builder->modified_props & Uri_HAS_USER_NAME) && builder->uri &&
3180 builder->uri->userinfo_start > -1 && builder->uri->userinfo_split != 0) {
3181 /* Just use the username from the base Uri. */
3182 data->username = builder->uri->canon_uri+builder->uri->userinfo_start;
3183 data->username_len = (builder->uri->userinfo_split > -1) ?
3184 builder->uri->userinfo_split : builder->uri->userinfo_len;
3185 ptr = NULL;
3186 } else {
3187 ptr = NULL;
3188 expected_len = 0;
3191 if(ptr) {
3192 const WCHAR *component = ptr;
3193 pptr = &ptr;
3194 if(parse_username(pptr, data, flags, ALLOW_NULL_TERM_USER_NAME) &&
3195 data->username_len == expected_len)
3196 TRACE("(%p %p %x): Found valid username component %s len=%d.\n", builder, data, flags,
3197 debugstr_wn(data->username, data->username_len), data->username_len);
3198 else {
3199 TRACE("(%p %p %x): Invalid username component found %s.\n", builder, data, flags,
3200 debugstr_wn(component, expected_len));
3201 return INET_E_INVALID_URL;
3205 return S_OK;
3208 static HRESULT validate_password(const UriBuilder *builder, parse_data *data, DWORD flags) {
3209 const WCHAR *ptr;
3210 const WCHAR **pptr;
3211 DWORD expected_len;
3213 if(builder->password) {
3214 ptr = builder->password;
3215 expected_len = builder->password_len;
3216 } else if(!(builder->modified_props & Uri_HAS_PASSWORD) && builder->uri &&
3217 builder->uri->userinfo_split > -1) {
3218 data->password = builder->uri->canon_uri+builder->uri->userinfo_start+builder->uri->userinfo_split+1;
3219 data->password_len = builder->uri->userinfo_len-builder->uri->userinfo_split-1;
3220 ptr = NULL;
3221 } else {
3222 ptr = NULL;
3223 expected_len = 0;
3226 if(ptr) {
3227 const WCHAR *component = ptr;
3228 pptr = &ptr;
3229 if(parse_password(pptr, data, flags, ALLOW_NULL_TERM_PASSWORD) &&
3230 data->password_len == expected_len)
3231 TRACE("(%p %p %x): Found valid password component %s len=%d.\n", builder, data, flags,
3232 debugstr_wn(data->password, data->password_len), data->password_len);
3233 else {
3234 TRACE("(%p %p %x): Invalid password component found %s.\n", builder, data, flags,
3235 debugstr_wn(component, expected_len));
3236 return INET_E_INVALID_URL;
3240 return S_OK;
3243 static HRESULT validate_userinfo(const UriBuilder *builder, parse_data *data, DWORD flags) {
3244 HRESULT hr;
3246 hr = validate_username(builder, data, flags);
3247 if(FAILED(hr))
3248 return hr;
3250 hr = validate_password(builder, data, flags);
3251 if(FAILED(hr))
3252 return hr;
3254 return S_OK;
3257 static HRESULT validate_host(const UriBuilder *builder, parse_data *data) {
3258 const WCHAR *ptr;
3259 const WCHAR **pptr;
3260 DWORD expected_len;
3262 if(builder->host) {
3263 ptr = builder->host;
3264 expected_len = builder->host_len;
3265 } else if(!(builder->modified_props & Uri_HAS_HOST) && builder->uri && builder->uri->host_start > -1) {
3266 ptr = builder->uri->canon_uri + builder->uri->host_start;
3267 expected_len = builder->uri->host_len;
3268 } else
3269 ptr = NULL;
3271 if(ptr) {
3272 const WCHAR *component = ptr;
3273 DWORD extras = ALLOW_BRACKETLESS_IP_LITERAL|IGNORE_PORT_DELIMITER|SKIP_IP_FUTURE_CHECK;
3274 pptr = &ptr;
3276 if(parse_host(pptr, data, extras) && data->host_len == expected_len)
3277 TRACE("(%p %p): Found valid host name %s len=%d type=%d.\n", builder, data,
3278 debugstr_wn(data->host, data->host_len), data->host_len, data->host_type);
3279 else {
3280 TRACE("(%p %p): Invalid host name found %s.\n", builder, data,
3281 debugstr_wn(component, expected_len));
3282 return INET_E_INVALID_URL;
3286 return S_OK;
3289 static void setup_port(const UriBuilder *builder, parse_data *data, DWORD flags) {
3290 if(builder->modified_props & Uri_HAS_PORT) {
3291 if(builder->has_port) {
3292 data->has_port = TRUE;
3293 data->port_value = builder->port;
3295 } else if(builder->uri && builder->uri->has_port) {
3296 data->has_port = TRUE;
3297 data->port_value = builder->uri->port;
3300 if(data->has_port)
3301 TRACE("(%p %p %x): Using %u as port for IUri.\n", builder, data, flags, data->port_value);
3304 static HRESULT validate_path(const UriBuilder *builder, parse_data *data, DWORD flags) {
3305 const WCHAR *ptr = NULL;
3306 const WCHAR *component;
3307 const WCHAR **pptr;
3308 DWORD expected_len;
3309 BOOL check_len = TRUE;
3310 BOOL valid = FALSE;
3312 if(builder->path) {
3313 ptr = builder->path;
3314 expected_len = builder->path_len;
3315 } else if(!(builder->modified_props & Uri_HAS_PATH) &&
3316 builder->uri && builder->uri->path_start > -1) {
3317 ptr = builder->uri->canon_uri+builder->uri->path_start;
3318 expected_len = builder->uri->path_len;
3319 } else {
3320 static const WCHAR nullW[] = {0};
3321 ptr = nullW;
3322 check_len = FALSE;
3323 expected_len = -1;
3326 component = ptr;
3327 pptr = &ptr;
3329 /* How the path is validated depends on what type of
3330 * URI it is.
3332 valid = data->is_opaque ?
3333 parse_path_opaque(pptr, data, flags) : parse_path_hierarchical(pptr, data, flags);
3335 if(!valid || (check_len && expected_len != data->path_len)) {
3336 TRACE("(%p %p %x): Invalid path component %s.\n", builder, data, flags,
3337 debugstr_wn(component, expected_len) );
3338 return INET_E_INVALID_URL;
3341 TRACE("(%p %p %x): Valid path component %s len=%d.\n", builder, data, flags,
3342 debugstr_wn(data->path, data->path_len), data->path_len);
3344 return S_OK;
3347 static HRESULT validate_query(const UriBuilder *builder, parse_data *data, DWORD flags) {
3348 const WCHAR *ptr = NULL;
3349 const WCHAR **pptr;
3350 DWORD expected_len;
3352 if(builder->query) {
3353 ptr = builder->query;
3354 expected_len = builder->query_len;
3355 } else if(!(builder->modified_props & Uri_HAS_QUERY) && builder->uri &&
3356 builder->uri->query_start > -1) {
3357 ptr = builder->uri->canon_uri+builder->uri->query_start;
3358 expected_len = builder->uri->query_len;
3361 if(ptr) {
3362 const WCHAR *component = ptr;
3363 pptr = &ptr;
3365 if(parse_query(pptr, data, flags) && expected_len == data->query_len)
3366 TRACE("(%p %p %x): Valid query component %s len=%d.\n", builder, data, flags,
3367 debugstr_wn(data->query, data->query_len), data->query_len);
3368 else {
3369 TRACE("(%p %p %x): Invalid query component %s.\n", builder, data, flags,
3370 debugstr_wn(component, expected_len));
3371 return INET_E_INVALID_URL;
3375 return S_OK;
3378 static HRESULT validate_fragment(const UriBuilder *builder, parse_data *data, DWORD flags) {
3379 const WCHAR *ptr = NULL;
3380 const WCHAR **pptr;
3381 DWORD expected_len;
3383 if(builder->fragment) {
3384 ptr = builder->fragment;
3385 expected_len = builder->fragment_len;
3386 } else if(!(builder->modified_props & Uri_HAS_FRAGMENT) && builder->uri &&
3387 builder->uri->fragment_start > -1) {
3388 ptr = builder->uri->canon_uri+builder->uri->fragment_start;
3389 expected_len = builder->uri->fragment_len;
3392 if(ptr) {
3393 const WCHAR *component = ptr;
3394 pptr = &ptr;
3396 if(parse_fragment(pptr, data, flags) && expected_len == data->fragment_len)
3397 TRACE("(%p %p %x): Valid fragment component %s len=%d.\n", builder, data, flags,
3398 debugstr_wn(data->fragment, data->fragment_len), data->fragment_len);
3399 else {
3400 TRACE("(%p %p %x): Invalid fragment component %s.\n", builder, data, flags,
3401 debugstr_wn(component, expected_len));
3402 return INET_E_INVALID_URL;
3406 return S_OK;
3409 static HRESULT validate_components(const UriBuilder *builder, parse_data *data, DWORD flags) {
3410 HRESULT hr;
3412 memset(data, 0, sizeof(parse_data));
3414 TRACE("(%p %p %x): Beginning to validate builder components.\n", builder, data, flags);
3416 hr = validate_scheme_name(builder, data, flags);
3417 if(FAILED(hr))
3418 return hr;
3420 /* Extra validation for file schemes. */
3421 if(data->scheme_type == URL_SCHEME_FILE) {
3422 if((builder->password || (builder->uri && builder->uri->userinfo_split > -1)) ||
3423 (builder->username || (builder->uri && builder->uri->userinfo_start > -1))) {
3424 TRACE("(%p %p %x): File schemes can't contain a username or password.\n",
3425 builder, data, flags);
3426 return INET_E_INVALID_URL;
3430 hr = validate_userinfo(builder, data, flags);
3431 if(FAILED(hr))
3432 return hr;
3434 hr = validate_host(builder, data);
3435 if(FAILED(hr))
3436 return hr;
3438 setup_port(builder, data, flags);
3440 /* The URI is opaque if it doesn't have an authority component. */
3441 if(!data->is_relative)
3442 data->is_opaque = !data->username && !data->password && !data->host && !data->has_port
3443 && data->scheme_type != URL_SCHEME_FILE;
3444 else
3445 data->is_opaque = !data->host && !data->has_port;
3447 hr = validate_path(builder, data, flags);
3448 if(FAILED(hr))
3449 return hr;
3451 hr = validate_query(builder, data, flags);
3452 if(FAILED(hr))
3453 return hr;
3455 hr = validate_fragment(builder, data, flags);
3456 if(FAILED(hr))
3457 return hr;
3459 TRACE("(%p %p %x): Finished validating builder components.\n", builder, data, flags);
3461 return S_OK;
3464 static HRESULT compare_file_paths(const Uri *a, const Uri *b, BOOL *ret)
3466 WCHAR *canon_path_a, *canon_path_b;
3467 DWORD len_a, len_b;
3469 if(!a->path_len) {
3470 *ret = !b->path_len;
3471 return S_OK;
3474 if(!b->path_len) {
3475 *ret = FALSE;
3476 return S_OK;
3479 /* Fast path */
3480 if(a->path_len == b->path_len && !wcsnicmp(a->canon_uri+a->path_start, b->canon_uri+b->path_start, a->path_len)) {
3481 *ret = TRUE;
3482 return S_OK;
3485 len_a = canonicalize_path_hierarchical(a->canon_uri+a->path_start, a->path_len, a->scheme_type, FALSE, 0, FALSE, NULL);
3486 len_b = canonicalize_path_hierarchical(b->canon_uri+b->path_start, b->path_len, b->scheme_type, FALSE, 0, FALSE, NULL);
3488 canon_path_a = heap_alloc(len_a*sizeof(WCHAR));
3489 if(!canon_path_a)
3490 return E_OUTOFMEMORY;
3491 canon_path_b = heap_alloc(len_b*sizeof(WCHAR));
3492 if(!canon_path_b) {
3493 heap_free(canon_path_a);
3494 return E_OUTOFMEMORY;
3497 len_a = canonicalize_path_hierarchical(a->canon_uri+a->path_start, a->path_len, a->scheme_type, FALSE, 0, FALSE, canon_path_a);
3498 len_b = canonicalize_path_hierarchical(b->canon_uri+b->path_start, b->path_len, b->scheme_type, FALSE, 0, FALSE, canon_path_b);
3500 *ret = len_a == len_b && !wcsnicmp(canon_path_a, canon_path_b, len_a);
3502 heap_free(canon_path_a);
3503 heap_free(canon_path_b);
3504 return S_OK;
3507 /* Checks if the two Uri's are logically equivalent. It's a simple
3508 * comparison, since they are both of type Uri, and it can access
3509 * the properties of each Uri directly without the need to go
3510 * through the "IUri_Get*" interface calls.
3512 static HRESULT compare_uris(const Uri *a, const Uri *b, BOOL *ret) {
3513 const BOOL known_scheme = a->scheme_type != URL_SCHEME_UNKNOWN;
3514 const BOOL are_hierarchical = a->authority_start > -1 && b->authority_start > -1;
3515 HRESULT hres;
3517 *ret = FALSE;
3519 if(a->scheme_type != b->scheme_type)
3520 return S_OK;
3522 /* Only compare the scheme names (if any) if their unknown scheme types. */
3523 if(!known_scheme) {
3524 if((a->scheme_start > -1 && b->scheme_start > -1) &&
3525 (a->scheme_len == b->scheme_len)) {
3526 /* Make sure the schemes are the same. */
3527 if(StrCmpNW(a->canon_uri+a->scheme_start, b->canon_uri+b->scheme_start, a->scheme_len))
3528 return S_OK;
3529 } else if(a->scheme_len != b->scheme_len)
3530 /* One of the Uri's has a scheme name, while the other doesn't. */
3531 return S_OK;
3534 /* If they have a userinfo component, perform case sensitive compare. */
3535 if((a->userinfo_start > -1 && b->userinfo_start > -1) &&
3536 (a->userinfo_len == b->userinfo_len)) {
3537 if(StrCmpNW(a->canon_uri+a->userinfo_start, b->canon_uri+b->userinfo_start, a->userinfo_len))
3538 return S_OK;
3539 } else if(a->userinfo_len != b->userinfo_len)
3540 /* One of the Uri's had a userinfo, while the other one doesn't. */
3541 return S_OK;
3543 /* Check if they have a host name. */
3544 if((a->host_start > -1 && b->host_start > -1) &&
3545 (a->host_len == b->host_len)) {
3546 /* Perform a case insensitive compare if they are a known scheme type. */
3547 if(known_scheme) {
3548 if(StrCmpNIW(a->canon_uri+a->host_start, b->canon_uri+b->host_start, a->host_len))
3549 return S_OK;
3550 } else if(StrCmpNW(a->canon_uri+a->host_start, b->canon_uri+b->host_start, a->host_len))
3551 return S_OK;
3552 } else if(a->host_len != b->host_len)
3553 /* One of the Uri's had a host, while the other one didn't. */
3554 return S_OK;
3556 if(a->has_port && b->has_port) {
3557 if(a->port != b->port)
3558 return S_OK;
3559 } else if(a->has_port || b->has_port)
3560 /* One had a port, while the other one didn't. */
3561 return S_OK;
3563 /* Windows is weird with how it handles paths. For example
3564 * One URI could be "http://google.com" (after canonicalization)
3565 * and one could be "http://google.com/" and the IsEqual function
3566 * would still evaluate to TRUE, but, only if they are both hierarchical
3567 * URIs.
3569 if(a->scheme_type == URL_SCHEME_FILE) {
3570 BOOL cmp;
3572 hres = compare_file_paths(a, b, &cmp);
3573 if(FAILED(hres) || !cmp)
3574 return hres;
3575 } else if((a->path_start > -1 && b->path_start > -1) &&
3576 (a->path_len == b->path_len)) {
3577 if(StrCmpNW(a->canon_uri+a->path_start, b->canon_uri+b->path_start, a->path_len))
3578 return S_OK;
3579 } else if(are_hierarchical && a->path_len == -1 && b->path_len == 0) {
3580 if(*(a->canon_uri+a->path_start) != '/')
3581 return S_OK;
3582 } else if(are_hierarchical && b->path_len == 1 && a->path_len == 0) {
3583 if(*(b->canon_uri+b->path_start) != '/')
3584 return S_OK;
3585 } else if(a->path_len != b->path_len)
3586 return S_OK;
3588 /* Compare the query strings of the two URIs. */
3589 if((a->query_start > -1 && b->query_start > -1) &&
3590 (a->query_len == b->query_len)) {
3591 if(StrCmpNW(a->canon_uri+a->query_start, b->canon_uri+b->query_start, a->query_len))
3592 return S_OK;
3593 } else if(a->query_len != b->query_len)
3594 return S_OK;
3596 if((a->fragment_start > -1 && b->fragment_start > -1) &&
3597 (a->fragment_len == b->fragment_len)) {
3598 if(StrCmpNW(a->canon_uri+a->fragment_start, b->canon_uri+b->fragment_start, a->fragment_len))
3599 return S_OK;
3600 } else if(a->fragment_len != b->fragment_len)
3601 return S_OK;
3603 /* If we get here, the two URIs are equivalent. */
3604 *ret = TRUE;
3605 return S_OK;
3608 static void convert_to_dos_path(const WCHAR *path, DWORD path_len,
3609 WCHAR *output, DWORD *output_len)
3611 const WCHAR *ptr = path;
3613 if(path_len > 3 && *ptr == '/' && is_drive_path(path+1))
3614 /* Skip over the leading / before the drive path. */
3615 ++ptr;
3617 for(; ptr < path+path_len; ++ptr) {
3618 if(*ptr == '/') {
3619 if(output)
3620 *output++ = '\\';
3621 (*output_len)++;
3622 } else {
3623 if(output)
3624 *output++ = *ptr;
3625 (*output_len)++;
3630 /* Generates a raw uri string using the parse_data. */
3631 static DWORD generate_raw_uri(const parse_data *data, BSTR uri, DWORD flags) {
3632 DWORD length = 0;
3634 if(data->scheme) {
3635 if(uri) {
3636 memcpy(uri, data->scheme, data->scheme_len*sizeof(WCHAR));
3637 uri[data->scheme_len] = ':';
3639 length += data->scheme_len+1;
3642 if(!data->is_opaque) {
3643 /* For the "//" which appears before the authority component. */
3644 if(uri) {
3645 uri[length] = '/';
3646 uri[length+1] = '/';
3648 length += 2;
3650 /* Check if we need to add the "\\" before the host name
3651 * of a UNC server name in a DOS path.
3653 if(flags & RAW_URI_CONVERT_TO_DOS_PATH &&
3654 data->scheme_type == URL_SCHEME_FILE && data->host) {
3655 if(uri) {
3656 uri[length] = '\\';
3657 uri[length+1] = '\\';
3659 length += 2;
3663 if(data->username) {
3664 if(uri)
3665 memcpy(uri+length, data->username, data->username_len*sizeof(WCHAR));
3666 length += data->username_len;
3669 if(data->password) {
3670 if(uri) {
3671 uri[length] = ':';
3672 memcpy(uri+length+1, data->password, data->password_len*sizeof(WCHAR));
3674 length += data->password_len+1;
3677 if(data->password || data->username) {
3678 if(uri)
3679 uri[length] = '@';
3680 ++length;
3683 if(data->host) {
3684 /* IPv6 addresses get the brackets added around them if they don't already
3685 * have them.
3687 const BOOL add_brackets = data->host_type == Uri_HOST_IPV6 && *(data->host) != '[';
3688 if(add_brackets) {
3689 if(uri)
3690 uri[length] = '[';
3691 ++length;
3694 if(uri)
3695 memcpy(uri+length, data->host, data->host_len*sizeof(WCHAR));
3696 length += data->host_len;
3698 if(add_brackets) {
3699 if(uri)
3700 uri[length] = ']';
3701 length++;
3705 if(data->has_port) {
3706 /* The port isn't included in the raw uri if it's the default
3707 * port for the scheme type.
3709 DWORD i;
3710 BOOL is_default = FALSE;
3712 for(i = 0; i < ARRAY_SIZE(default_ports); ++i) {
3713 if(data->scheme_type == default_ports[i].scheme &&
3714 data->port_value == default_ports[i].port)
3715 is_default = TRUE;
3718 if(!is_default || flags & RAW_URI_FORCE_PORT_DISP) {
3719 if(uri)
3720 uri[length] = ':';
3721 ++length;
3723 if(uri)
3724 length += ui2str(uri+length, data->port_value);
3725 else
3726 length += ui2str(NULL, data->port_value);
3730 /* Check if a '/' should be added before the path for hierarchical URIs. */
3731 if(!data->is_opaque && data->path && *(data->path) != '/') {
3732 if(uri)
3733 uri[length] = '/';
3734 ++length;
3737 if(data->path) {
3738 if(!data->is_opaque && data->scheme_type == URL_SCHEME_FILE &&
3739 flags & RAW_URI_CONVERT_TO_DOS_PATH) {
3740 DWORD len = 0;
3742 if(uri)
3743 convert_to_dos_path(data->path, data->path_len, uri+length, &len);
3744 else
3745 convert_to_dos_path(data->path, data->path_len, NULL, &len);
3747 length += len;
3748 } else {
3749 if(uri)
3750 memcpy(uri+length, data->path, data->path_len*sizeof(WCHAR));
3751 length += data->path_len;
3755 if(data->query) {
3756 if(uri)
3757 memcpy(uri+length, data->query, data->query_len*sizeof(WCHAR));
3758 length += data->query_len;
3761 if(data->fragment) {
3762 if(uri)
3763 memcpy(uri+length, data->fragment, data->fragment_len*sizeof(WCHAR));
3764 length += data->fragment_len;
3767 if(uri)
3768 TRACE("(%p %p): Generated raw uri=%s len=%d\n", data, uri, debugstr_wn(uri, length), length);
3769 else
3770 TRACE("(%p %p): Computed raw uri len=%d\n", data, uri, length);
3772 return length;
3775 static HRESULT generate_uri(const UriBuilder *builder, const parse_data *data, Uri *uri, DWORD flags) {
3776 HRESULT hr;
3777 DWORD length = generate_raw_uri(data, NULL, 0);
3778 uri->raw_uri = SysAllocStringLen(NULL, length);
3779 if(!uri->raw_uri)
3780 return E_OUTOFMEMORY;
3782 generate_raw_uri(data, uri->raw_uri, 0);
3784 hr = canonicalize_uri(data, uri, flags);
3785 if(FAILED(hr)) {
3786 if(hr == E_INVALIDARG)
3787 return INET_E_INVALID_URL;
3788 return hr;
3791 uri->create_flags = flags;
3792 return S_OK;
3795 static inline Uri* impl_from_IUri(IUri *iface)
3797 return CONTAINING_RECORD(iface, Uri, IUri_iface);
3800 static inline void destroy_uri_obj(Uri *This)
3802 SysFreeString(This->raw_uri);
3803 heap_free(This->canon_uri);
3804 heap_free(This);
3807 static HRESULT WINAPI Uri_QueryInterface(IUri *iface, REFIID riid, void **ppv)
3809 Uri *This = impl_from_IUri(iface);
3811 if(IsEqualGUID(&IID_IUnknown, riid)) {
3812 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
3813 *ppv = &This->IUri_iface;
3814 }else if(IsEqualGUID(&IID_IUri, riid)) {
3815 TRACE("(%p)->(IID_IUri %p)\n", This, ppv);
3816 *ppv = &This->IUri_iface;
3817 }else if(IsEqualGUID(&IID_IUriBuilderFactory, riid)) {
3818 TRACE("(%p)->(IID_IUriBuilderFactory %p)\n", This, ppv);
3819 *ppv = &This->IUriBuilderFactory_iface;
3820 }else if(IsEqualGUID(&IID_IPersistStream, riid)) {
3821 TRACE("(%p)->(IID_IPersistStream %p)\n", This, ppv);
3822 *ppv = &This->IPersistStream_iface;
3823 }else if(IsEqualGUID(&IID_IMarshal, riid)) {
3824 TRACE("(%p)->(IID_IMarshal %p)\n", This, ppv);
3825 *ppv = &This->IMarshal_iface;
3826 }else if(IsEqualGUID(&IID_IUriObj, riid)) {
3827 TRACE("(%p)->(IID_IUriObj %p)\n", This, ppv);
3828 *ppv = This;
3829 return S_OK;
3830 }else {
3831 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
3832 *ppv = NULL;
3833 return E_NOINTERFACE;
3836 IUnknown_AddRef((IUnknown*)*ppv);
3837 return S_OK;
3840 static ULONG WINAPI Uri_AddRef(IUri *iface)
3842 Uri *This = impl_from_IUri(iface);
3843 LONG ref = InterlockedIncrement(&This->ref);
3845 TRACE("(%p) ref=%d\n", This, ref);
3847 return ref;
3850 static ULONG WINAPI Uri_Release(IUri *iface)
3852 Uri *This = impl_from_IUri(iface);
3853 LONG ref = InterlockedDecrement(&This->ref);
3855 TRACE("(%p) ref=%d\n", This, ref);
3857 if(!ref)
3858 destroy_uri_obj(This);
3860 return ref;
3863 static HRESULT WINAPI Uri_GetPropertyBSTR(IUri *iface, Uri_PROPERTY uriProp, BSTR *pbstrProperty, DWORD dwFlags)
3865 Uri *This = impl_from_IUri(iface);
3866 HRESULT hres;
3867 TRACE("(%p %s)->(%d %p %x)\n", This, debugstr_w(This->canon_uri), uriProp, pbstrProperty, dwFlags);
3869 if(!This->create_flags)
3870 return E_UNEXPECTED;
3871 if(!pbstrProperty)
3872 return E_POINTER;
3874 if(uriProp > Uri_PROPERTY_STRING_LAST) {
3875 /* It only returns S_FALSE for the ZONE property... */
3876 if(uriProp == Uri_PROPERTY_ZONE) {
3877 *pbstrProperty = SysAllocStringLen(NULL, 0);
3878 if(!(*pbstrProperty))
3879 return E_OUTOFMEMORY;
3880 return S_FALSE;
3883 *pbstrProperty = NULL;
3884 return E_INVALIDARG;
3887 /* Don't have support for flags yet. */
3888 if(dwFlags) {
3889 FIXME("(%p)->(%d %p %x)\n", This, uriProp, pbstrProperty, dwFlags);
3890 return E_NOTIMPL;
3893 switch(uriProp) {
3894 case Uri_PROPERTY_ABSOLUTE_URI:
3895 if(This->display_modifiers & URI_DISPLAY_NO_ABSOLUTE_URI) {
3896 *pbstrProperty = SysAllocStringLen(NULL, 0);
3897 hres = S_FALSE;
3898 } else {
3899 if(This->scheme_type != URL_SCHEME_UNKNOWN && This->userinfo_start > -1) {
3900 if(This->userinfo_len == 0) {
3901 /* Don't include the '@' after the userinfo component. */
3902 *pbstrProperty = SysAllocStringLen(NULL, This->canon_len-1);
3903 hres = S_OK;
3904 if(*pbstrProperty) {
3905 /* Copy everything before it. */
3906 memcpy(*pbstrProperty, This->canon_uri, This->userinfo_start*sizeof(WCHAR));
3908 /* And everything after it. */
3909 memcpy(*pbstrProperty+This->userinfo_start, This->canon_uri+This->userinfo_start+1,
3910 (This->canon_len-This->userinfo_start-1)*sizeof(WCHAR));
3912 } else if(This->userinfo_split == 0 && This->userinfo_len == 1) {
3913 /* Don't include the ":@" */
3914 *pbstrProperty = SysAllocStringLen(NULL, This->canon_len-2);
3915 hres = S_OK;
3916 if(*pbstrProperty) {
3917 memcpy(*pbstrProperty, This->canon_uri, This->userinfo_start*sizeof(WCHAR));
3918 memcpy(*pbstrProperty+This->userinfo_start, This->canon_uri+This->userinfo_start+2,
3919 (This->canon_len-This->userinfo_start-2)*sizeof(WCHAR));
3921 } else {
3922 *pbstrProperty = SysAllocString(This->canon_uri);
3923 hres = S_OK;
3925 } else {
3926 *pbstrProperty = SysAllocString(This->canon_uri);
3927 hres = S_OK;
3931 if(!(*pbstrProperty))
3932 hres = E_OUTOFMEMORY;
3934 break;
3935 case Uri_PROPERTY_AUTHORITY:
3936 if(This->authority_start > -1) {
3937 if(This->port_offset > -1 && is_default_port(This->scheme_type, This->port) &&
3938 This->display_modifiers & URI_DISPLAY_NO_DEFAULT_PORT_AUTH)
3939 /* Don't include the port in the authority component. */
3940 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->authority_start, This->port_offset);
3941 else
3942 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->authority_start, This->authority_len);
3943 hres = S_OK;
3944 } else {
3945 *pbstrProperty = SysAllocStringLen(NULL, 0);
3946 hres = S_FALSE;
3949 if(!(*pbstrProperty))
3950 hres = E_OUTOFMEMORY;
3952 break;
3953 case Uri_PROPERTY_DISPLAY_URI:
3954 /* The Display URI contains everything except for the userinfo for known
3955 * scheme types.
3957 if(This->scheme_type != URL_SCHEME_UNKNOWN && This->userinfo_start > -1) {
3958 *pbstrProperty = SysAllocStringLen(NULL, This->canon_len-This->userinfo_len);
3960 if(*pbstrProperty) {
3961 /* Copy everything before the userinfo over. */
3962 memcpy(*pbstrProperty, This->canon_uri, This->userinfo_start*sizeof(WCHAR));
3963 /* Copy everything after the userinfo over. */
3964 memcpy(*pbstrProperty+This->userinfo_start,
3965 This->canon_uri+This->userinfo_start+This->userinfo_len+1,
3966 (This->canon_len-(This->userinfo_start+This->userinfo_len+1))*sizeof(WCHAR));
3968 } else
3969 *pbstrProperty = SysAllocString(This->canon_uri);
3971 if(!(*pbstrProperty))
3972 hres = E_OUTOFMEMORY;
3973 else
3974 hres = S_OK;
3976 break;
3977 case Uri_PROPERTY_DOMAIN:
3978 if(This->domain_offset > -1) {
3979 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->host_start+This->domain_offset,
3980 This->host_len-This->domain_offset);
3981 hres = S_OK;
3982 } else {
3983 *pbstrProperty = SysAllocStringLen(NULL, 0);
3984 hres = S_FALSE;
3987 if(!(*pbstrProperty))
3988 hres = E_OUTOFMEMORY;
3990 break;
3991 case Uri_PROPERTY_EXTENSION:
3992 if(This->extension_offset > -1) {
3993 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->path_start+This->extension_offset,
3994 This->path_len-This->extension_offset);
3995 hres = S_OK;
3996 } else {
3997 *pbstrProperty = SysAllocStringLen(NULL, 0);
3998 hres = S_FALSE;
4001 if(!(*pbstrProperty))
4002 hres = E_OUTOFMEMORY;
4004 break;
4005 case Uri_PROPERTY_FRAGMENT:
4006 if(This->fragment_start > -1) {
4007 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->fragment_start, This->fragment_len);
4008 hres = S_OK;
4009 } else {
4010 *pbstrProperty = SysAllocStringLen(NULL, 0);
4011 hres = S_FALSE;
4014 if(!(*pbstrProperty))
4015 hres = E_OUTOFMEMORY;
4017 break;
4018 case Uri_PROPERTY_HOST:
4019 if(This->host_start > -1) {
4020 /* The '[' and ']' aren't included for IPv6 addresses. */
4021 if(This->host_type == Uri_HOST_IPV6)
4022 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->host_start+1, This->host_len-2);
4023 else
4024 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->host_start, This->host_len);
4026 hres = S_OK;
4027 } else {
4028 *pbstrProperty = SysAllocStringLen(NULL, 0);
4029 hres = S_FALSE;
4032 if(!(*pbstrProperty))
4033 hres = E_OUTOFMEMORY;
4035 break;
4036 case Uri_PROPERTY_PASSWORD:
4037 if(This->userinfo_split > -1) {
4038 *pbstrProperty = SysAllocStringLen(
4039 This->canon_uri+This->userinfo_start+This->userinfo_split+1,
4040 This->userinfo_len-This->userinfo_split-1);
4041 hres = S_OK;
4042 } else {
4043 *pbstrProperty = SysAllocStringLen(NULL, 0);
4044 hres = S_FALSE;
4047 if(!(*pbstrProperty))
4048 return E_OUTOFMEMORY;
4050 break;
4051 case Uri_PROPERTY_PATH:
4052 if(This->path_start > -1) {
4053 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->path_start, This->path_len);
4054 hres = S_OK;
4055 } else {
4056 *pbstrProperty = SysAllocStringLen(NULL, 0);
4057 hres = S_FALSE;
4060 if(!(*pbstrProperty))
4061 hres = E_OUTOFMEMORY;
4063 break;
4064 case Uri_PROPERTY_PATH_AND_QUERY:
4065 if(This->path_start > -1) {
4066 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->path_start, This->path_len+This->query_len);
4067 hres = S_OK;
4068 } else if(This->query_start > -1) {
4069 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->query_start, This->query_len);
4070 hres = S_OK;
4071 } else {
4072 *pbstrProperty = SysAllocStringLen(NULL, 0);
4073 hres = S_FALSE;
4076 if(!(*pbstrProperty))
4077 hres = E_OUTOFMEMORY;
4079 break;
4080 case Uri_PROPERTY_QUERY:
4081 if(This->query_start > -1) {
4082 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->query_start, This->query_len);
4083 hres = S_OK;
4084 } else {
4085 *pbstrProperty = SysAllocStringLen(NULL, 0);
4086 hres = S_FALSE;
4089 if(!(*pbstrProperty))
4090 hres = E_OUTOFMEMORY;
4092 break;
4093 case Uri_PROPERTY_RAW_URI:
4094 *pbstrProperty = SysAllocString(This->raw_uri);
4095 if(!(*pbstrProperty))
4096 hres = E_OUTOFMEMORY;
4097 else
4098 hres = S_OK;
4099 break;
4100 case Uri_PROPERTY_SCHEME_NAME:
4101 if(This->scheme_start > -1) {
4102 *pbstrProperty = SysAllocStringLen(This->canon_uri + This->scheme_start, This->scheme_len);
4103 hres = S_OK;
4104 } else {
4105 *pbstrProperty = SysAllocStringLen(NULL, 0);
4106 hres = S_FALSE;
4109 if(!(*pbstrProperty))
4110 hres = E_OUTOFMEMORY;
4112 break;
4113 case Uri_PROPERTY_USER_INFO:
4114 if(This->userinfo_start > -1) {
4115 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->userinfo_start, This->userinfo_len);
4116 hres = S_OK;
4117 } else {
4118 *pbstrProperty = SysAllocStringLen(NULL, 0);
4119 hres = S_FALSE;
4122 if(!(*pbstrProperty))
4123 hres = E_OUTOFMEMORY;
4125 break;
4126 case Uri_PROPERTY_USER_NAME:
4127 if(This->userinfo_start > -1 && This->userinfo_split != 0) {
4128 /* If userinfo_split is set, that means a password exists
4129 * so the username is only from userinfo_start to userinfo_split.
4131 if(This->userinfo_split > -1) {
4132 *pbstrProperty = SysAllocStringLen(This->canon_uri + This->userinfo_start, This->userinfo_split);
4133 hres = S_OK;
4134 } else {
4135 *pbstrProperty = SysAllocStringLen(This->canon_uri + This->userinfo_start, This->userinfo_len);
4136 hres = S_OK;
4138 } else {
4139 *pbstrProperty = SysAllocStringLen(NULL, 0);
4140 hres = S_FALSE;
4143 if(!(*pbstrProperty))
4144 return E_OUTOFMEMORY;
4146 break;
4147 default:
4148 FIXME("(%p)->(%d %p %x)\n", This, uriProp, pbstrProperty, dwFlags);
4149 hres = E_NOTIMPL;
4152 return hres;
4155 static HRESULT WINAPI Uri_GetPropertyLength(IUri *iface, Uri_PROPERTY uriProp, DWORD *pcchProperty, DWORD dwFlags)
4157 Uri *This = impl_from_IUri(iface);
4158 HRESULT hres;
4159 TRACE("(%p %s)->(%d %p %x)\n", This, debugstr_w(This->canon_uri), uriProp, pcchProperty, dwFlags);
4161 if(!This->create_flags)
4162 return E_UNEXPECTED;
4163 if(!pcchProperty)
4164 return E_INVALIDARG;
4166 /* Can only return a length for a property if it's a string. */
4167 if(uriProp > Uri_PROPERTY_STRING_LAST)
4168 return E_INVALIDARG;
4170 /* Don't have support for flags yet. */
4171 if(dwFlags) {
4172 FIXME("(%p)->(%d %p %x)\n", This, uriProp, pcchProperty, dwFlags);
4173 return E_NOTIMPL;
4176 switch(uriProp) {
4177 case Uri_PROPERTY_ABSOLUTE_URI:
4178 if(This->display_modifiers & URI_DISPLAY_NO_ABSOLUTE_URI) {
4179 *pcchProperty = 0;
4180 hres = S_FALSE;
4181 } else {
4182 if(This->scheme_type != URL_SCHEME_UNKNOWN) {
4183 if(This->userinfo_start > -1 && This->userinfo_len == 0)
4184 /* Don't include the '@' in the length. */
4185 *pcchProperty = This->canon_len-1;
4186 else if(This->userinfo_start > -1 && This->userinfo_len == 1 &&
4187 This->userinfo_split == 0)
4188 /* Don't include the ":@" in the length. */
4189 *pcchProperty = This->canon_len-2;
4190 else
4191 *pcchProperty = This->canon_len;
4192 } else
4193 *pcchProperty = This->canon_len;
4195 hres = S_OK;
4198 break;
4199 case Uri_PROPERTY_AUTHORITY:
4200 if(This->port_offset > -1 &&
4201 This->display_modifiers & URI_DISPLAY_NO_DEFAULT_PORT_AUTH &&
4202 is_default_port(This->scheme_type, This->port))
4203 /* Only count up until the port in the authority. */
4204 *pcchProperty = This->port_offset;
4205 else
4206 *pcchProperty = This->authority_len;
4207 hres = (This->authority_start > -1) ? S_OK : S_FALSE;
4208 break;
4209 case Uri_PROPERTY_DISPLAY_URI:
4210 if(This->scheme_type != URL_SCHEME_UNKNOWN && This->userinfo_start > -1)
4211 *pcchProperty = This->canon_len-This->userinfo_len-1;
4212 else
4213 *pcchProperty = This->canon_len;
4215 hres = S_OK;
4216 break;
4217 case Uri_PROPERTY_DOMAIN:
4218 if(This->domain_offset > -1)
4219 *pcchProperty = This->host_len - This->domain_offset;
4220 else
4221 *pcchProperty = 0;
4223 hres = (This->domain_offset > -1) ? S_OK : S_FALSE;
4224 break;
4225 case Uri_PROPERTY_EXTENSION:
4226 if(This->extension_offset > -1) {
4227 *pcchProperty = This->path_len - This->extension_offset;
4228 hres = S_OK;
4229 } else {
4230 *pcchProperty = 0;
4231 hres = S_FALSE;
4234 break;
4235 case Uri_PROPERTY_FRAGMENT:
4236 *pcchProperty = This->fragment_len;
4237 hres = (This->fragment_start > -1) ? S_OK : S_FALSE;
4238 break;
4239 case Uri_PROPERTY_HOST:
4240 *pcchProperty = This->host_len;
4242 /* '[' and ']' aren't included in the length. */
4243 if(This->host_type == Uri_HOST_IPV6)
4244 *pcchProperty -= 2;
4246 hres = (This->host_start > -1) ? S_OK : S_FALSE;
4247 break;
4248 case Uri_PROPERTY_PASSWORD:
4249 *pcchProperty = (This->userinfo_split > -1) ? This->userinfo_len-This->userinfo_split-1 : 0;
4250 hres = (This->userinfo_split > -1) ? S_OK : S_FALSE;
4251 break;
4252 case Uri_PROPERTY_PATH:
4253 *pcchProperty = This->path_len;
4254 hres = (This->path_start > -1) ? S_OK : S_FALSE;
4255 break;
4256 case Uri_PROPERTY_PATH_AND_QUERY:
4257 *pcchProperty = This->path_len+This->query_len;
4258 hres = (This->path_start > -1 || This->query_start > -1) ? S_OK : S_FALSE;
4259 break;
4260 case Uri_PROPERTY_QUERY:
4261 *pcchProperty = This->query_len;
4262 hres = (This->query_start > -1) ? S_OK : S_FALSE;
4263 break;
4264 case Uri_PROPERTY_RAW_URI:
4265 *pcchProperty = SysStringLen(This->raw_uri);
4266 hres = S_OK;
4267 break;
4268 case Uri_PROPERTY_SCHEME_NAME:
4269 *pcchProperty = This->scheme_len;
4270 hres = (This->scheme_start > -1) ? S_OK : S_FALSE;
4271 break;
4272 case Uri_PROPERTY_USER_INFO:
4273 *pcchProperty = This->userinfo_len;
4274 hres = (This->userinfo_start > -1) ? S_OK : S_FALSE;
4275 break;
4276 case Uri_PROPERTY_USER_NAME:
4277 *pcchProperty = (This->userinfo_split > -1) ? This->userinfo_split : This->userinfo_len;
4278 if(This->userinfo_split == 0)
4279 hres = S_FALSE;
4280 else
4281 hres = (This->userinfo_start > -1) ? S_OK : S_FALSE;
4282 break;
4283 default:
4284 FIXME("(%p)->(%d %p %x)\n", This, uriProp, pcchProperty, dwFlags);
4285 hres = E_NOTIMPL;
4288 return hres;
4291 static HRESULT WINAPI Uri_GetPropertyDWORD(IUri *iface, Uri_PROPERTY uriProp, DWORD *pcchProperty, DWORD dwFlags)
4293 Uri *This = impl_from_IUri(iface);
4294 HRESULT hres;
4296 TRACE("(%p %s)->(%d %p %x)\n", This, debugstr_w(This->canon_uri), uriProp, pcchProperty, dwFlags);
4298 if(!This->create_flags)
4299 return E_UNEXPECTED;
4300 if(!pcchProperty)
4301 return E_INVALIDARG;
4303 /* Microsoft's implementation for the ZONE property of a URI seems to be lacking...
4304 * From what I can tell, instead of checking which URLZONE the URI belongs to it
4305 * simply assigns URLZONE_INVALID and returns E_NOTIMPL. This also applies to the GetZone
4306 * function.
4308 if(uriProp == Uri_PROPERTY_ZONE) {
4309 *pcchProperty = URLZONE_INVALID;
4310 return E_NOTIMPL;
4313 if(uriProp < Uri_PROPERTY_DWORD_START) {
4314 *pcchProperty = 0;
4315 return E_INVALIDARG;
4318 switch(uriProp) {
4319 case Uri_PROPERTY_HOST_TYPE:
4320 *pcchProperty = This->host_type;
4321 hres = S_OK;
4322 break;
4323 case Uri_PROPERTY_PORT:
4324 if(!This->has_port) {
4325 *pcchProperty = 0;
4326 hres = S_FALSE;
4327 } else {
4328 *pcchProperty = This->port;
4329 hres = S_OK;
4332 break;
4333 case Uri_PROPERTY_SCHEME:
4334 *pcchProperty = This->scheme_type;
4335 hres = S_OK;
4336 break;
4337 default:
4338 FIXME("(%p)->(%d %p %x)\n", This, uriProp, pcchProperty, dwFlags);
4339 hres = E_NOTIMPL;
4342 return hres;
4345 static HRESULT WINAPI Uri_HasProperty(IUri *iface, Uri_PROPERTY uriProp, BOOL *pfHasProperty)
4347 Uri *This = impl_from_IUri(iface);
4349 TRACE("(%p %s)->(%d %p)\n", This, debugstr_w(This->canon_uri), uriProp, pfHasProperty);
4351 if(!pfHasProperty)
4352 return E_INVALIDARG;
4354 switch(uriProp) {
4355 case Uri_PROPERTY_ABSOLUTE_URI:
4356 *pfHasProperty = !(This->display_modifiers & URI_DISPLAY_NO_ABSOLUTE_URI);
4357 break;
4358 case Uri_PROPERTY_AUTHORITY:
4359 *pfHasProperty = This->authority_start > -1;
4360 break;
4361 case Uri_PROPERTY_DISPLAY_URI:
4362 *pfHasProperty = TRUE;
4363 break;
4364 case Uri_PROPERTY_DOMAIN:
4365 *pfHasProperty = This->domain_offset > -1;
4366 break;
4367 case Uri_PROPERTY_EXTENSION:
4368 *pfHasProperty = This->extension_offset > -1;
4369 break;
4370 case Uri_PROPERTY_FRAGMENT:
4371 *pfHasProperty = This->fragment_start > -1;
4372 break;
4373 case Uri_PROPERTY_HOST:
4374 *pfHasProperty = This->host_start > -1;
4375 break;
4376 case Uri_PROPERTY_PASSWORD:
4377 *pfHasProperty = This->userinfo_split > -1;
4378 break;
4379 case Uri_PROPERTY_PATH:
4380 *pfHasProperty = This->path_start > -1;
4381 break;
4382 case Uri_PROPERTY_PATH_AND_QUERY:
4383 *pfHasProperty = (This->path_start > -1 || This->query_start > -1);
4384 break;
4385 case Uri_PROPERTY_QUERY:
4386 *pfHasProperty = This->query_start > -1;
4387 break;
4388 case Uri_PROPERTY_RAW_URI:
4389 *pfHasProperty = TRUE;
4390 break;
4391 case Uri_PROPERTY_SCHEME_NAME:
4392 *pfHasProperty = This->scheme_start > -1;
4393 break;
4394 case Uri_PROPERTY_USER_INFO:
4395 *pfHasProperty = This->userinfo_start > -1;
4396 break;
4397 case Uri_PROPERTY_USER_NAME:
4398 if(This->userinfo_split == 0)
4399 *pfHasProperty = FALSE;
4400 else
4401 *pfHasProperty = This->userinfo_start > -1;
4402 break;
4403 case Uri_PROPERTY_HOST_TYPE:
4404 *pfHasProperty = TRUE;
4405 break;
4406 case Uri_PROPERTY_PORT:
4407 *pfHasProperty = This->has_port;
4408 break;
4409 case Uri_PROPERTY_SCHEME:
4410 *pfHasProperty = TRUE;
4411 break;
4412 case Uri_PROPERTY_ZONE:
4413 *pfHasProperty = FALSE;
4414 break;
4415 default:
4416 FIXME("(%p)->(%d %p): Unsupported property type.\n", This, uriProp, pfHasProperty);
4417 return E_NOTIMPL;
4420 return S_OK;
4423 static HRESULT WINAPI Uri_GetAbsoluteUri(IUri *iface, BSTR *pstrAbsoluteUri)
4425 TRACE("(%p)->(%p)\n", iface, pstrAbsoluteUri);
4426 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_ABSOLUTE_URI, pstrAbsoluteUri, 0);
4429 static HRESULT WINAPI Uri_GetAuthority(IUri *iface, BSTR *pstrAuthority)
4431 TRACE("(%p)->(%p)\n", iface, pstrAuthority);
4432 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_AUTHORITY, pstrAuthority, 0);
4435 static HRESULT WINAPI Uri_GetDisplayUri(IUri *iface, BSTR *pstrDisplayUri)
4437 TRACE("(%p)->(%p)\n", iface, pstrDisplayUri);
4438 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_DISPLAY_URI, pstrDisplayUri, 0);
4441 static HRESULT WINAPI Uri_GetDomain(IUri *iface, BSTR *pstrDomain)
4443 TRACE("(%p)->(%p)\n", iface, pstrDomain);
4444 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_DOMAIN, pstrDomain, 0);
4447 static HRESULT WINAPI Uri_GetExtension(IUri *iface, BSTR *pstrExtension)
4449 TRACE("(%p)->(%p)\n", iface, pstrExtension);
4450 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_EXTENSION, pstrExtension, 0);
4453 static HRESULT WINAPI Uri_GetFragment(IUri *iface, BSTR *pstrFragment)
4455 TRACE("(%p)->(%p)\n", iface, pstrFragment);
4456 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_FRAGMENT, pstrFragment, 0);
4459 static HRESULT WINAPI Uri_GetHost(IUri *iface, BSTR *pstrHost)
4461 TRACE("(%p)->(%p)\n", iface, pstrHost);
4462 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_HOST, pstrHost, 0);
4465 static HRESULT WINAPI Uri_GetPassword(IUri *iface, BSTR *pstrPassword)
4467 TRACE("(%p)->(%p)\n", iface, pstrPassword);
4468 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_PASSWORD, pstrPassword, 0);
4471 static HRESULT WINAPI Uri_GetPath(IUri *iface, BSTR *pstrPath)
4473 TRACE("(%p)->(%p)\n", iface, pstrPath);
4474 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_PATH, pstrPath, 0);
4477 static HRESULT WINAPI Uri_GetPathAndQuery(IUri *iface, BSTR *pstrPathAndQuery)
4479 TRACE("(%p)->(%p)\n", iface, pstrPathAndQuery);
4480 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_PATH_AND_QUERY, pstrPathAndQuery, 0);
4483 static HRESULT WINAPI Uri_GetQuery(IUri *iface, BSTR *pstrQuery)
4485 TRACE("(%p)->(%p)\n", iface, pstrQuery);
4486 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_QUERY, pstrQuery, 0);
4489 static HRESULT WINAPI Uri_GetRawUri(IUri *iface, BSTR *pstrRawUri)
4491 TRACE("(%p)->(%p)\n", iface, pstrRawUri);
4492 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_RAW_URI, pstrRawUri, 0);
4495 static HRESULT WINAPI Uri_GetSchemeName(IUri *iface, BSTR *pstrSchemeName)
4497 TRACE("(%p)->(%p)\n", iface, pstrSchemeName);
4498 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_SCHEME_NAME, pstrSchemeName, 0);
4501 static HRESULT WINAPI Uri_GetUserInfo(IUri *iface, BSTR *pstrUserInfo)
4503 TRACE("(%p)->(%p)\n", iface, pstrUserInfo);
4504 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_USER_INFO, pstrUserInfo, 0);
4507 static HRESULT WINAPI Uri_GetUserName(IUri *iface, BSTR *pstrUserName)
4509 TRACE("(%p)->(%p)\n", iface, pstrUserName);
4510 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_USER_NAME, pstrUserName, 0);
4513 static HRESULT WINAPI Uri_GetHostType(IUri *iface, DWORD *pdwHostType)
4515 TRACE("(%p)->(%p)\n", iface, pdwHostType);
4516 return IUri_GetPropertyDWORD(iface, Uri_PROPERTY_HOST_TYPE, pdwHostType, 0);
4519 static HRESULT WINAPI Uri_GetPort(IUri *iface, DWORD *pdwPort)
4521 TRACE("(%p)->(%p)\n", iface, pdwPort);
4522 return IUri_GetPropertyDWORD(iface, Uri_PROPERTY_PORT, pdwPort, 0);
4525 static HRESULT WINAPI Uri_GetScheme(IUri *iface, DWORD *pdwScheme)
4527 TRACE("(%p)->(%p)\n", iface, pdwScheme);
4528 return IUri_GetPropertyDWORD(iface, Uri_PROPERTY_SCHEME, pdwScheme, 0);
4531 static HRESULT WINAPI Uri_GetZone(IUri *iface, DWORD *pdwZone)
4533 TRACE("(%p)->(%p)\n", iface, pdwZone);
4534 return IUri_GetPropertyDWORD(iface, Uri_PROPERTY_ZONE,pdwZone, 0);
4537 static HRESULT WINAPI Uri_GetProperties(IUri *iface, DWORD *pdwProperties)
4539 Uri *This = impl_from_IUri(iface);
4540 TRACE("(%p %s)->(%p)\n", This, debugstr_w(This->canon_uri), pdwProperties);
4542 if(!This->create_flags)
4543 return E_UNEXPECTED;
4544 if(!pdwProperties)
4545 return E_INVALIDARG;
4547 /* All URIs have these. */
4548 *pdwProperties = Uri_HAS_DISPLAY_URI|Uri_HAS_RAW_URI|Uri_HAS_SCHEME|Uri_HAS_HOST_TYPE;
4550 if(!(This->display_modifiers & URI_DISPLAY_NO_ABSOLUTE_URI))
4551 *pdwProperties |= Uri_HAS_ABSOLUTE_URI;
4553 if(This->scheme_start > -1)
4554 *pdwProperties |= Uri_HAS_SCHEME_NAME;
4556 if(This->authority_start > -1) {
4557 *pdwProperties |= Uri_HAS_AUTHORITY;
4558 if(This->userinfo_start > -1) {
4559 *pdwProperties |= Uri_HAS_USER_INFO;
4560 if(This->userinfo_split != 0)
4561 *pdwProperties |= Uri_HAS_USER_NAME;
4563 if(This->userinfo_split > -1)
4564 *pdwProperties |= Uri_HAS_PASSWORD;
4565 if(This->host_start > -1)
4566 *pdwProperties |= Uri_HAS_HOST;
4567 if(This->domain_offset > -1)
4568 *pdwProperties |= Uri_HAS_DOMAIN;
4571 if(This->has_port)
4572 *pdwProperties |= Uri_HAS_PORT;
4573 if(This->path_start > -1)
4574 *pdwProperties |= Uri_HAS_PATH|Uri_HAS_PATH_AND_QUERY;
4575 if(This->query_start > -1)
4576 *pdwProperties |= Uri_HAS_QUERY|Uri_HAS_PATH_AND_QUERY;
4578 if(This->extension_offset > -1)
4579 *pdwProperties |= Uri_HAS_EXTENSION;
4581 if(This->fragment_start > -1)
4582 *pdwProperties |= Uri_HAS_FRAGMENT;
4584 return S_OK;
4587 static HRESULT WINAPI Uri_IsEqual(IUri *iface, IUri *pUri, BOOL *pfEqual)
4589 Uri *This = impl_from_IUri(iface);
4590 Uri *other;
4592 TRACE("(%p %s)->(%p %p)\n", This, debugstr_w(This->canon_uri), pUri, pfEqual);
4594 if(!This->create_flags)
4595 return E_UNEXPECTED;
4596 if(!pfEqual)
4597 return E_POINTER;
4599 if(!pUri) {
4600 *pfEqual = FALSE;
4602 /* For some reason Windows returns S_OK here... */
4603 return S_OK;
4606 /* Try to convert it to a Uri (allows for a more simple comparison). */
4607 if(!(other = get_uri_obj(pUri))) {
4608 FIXME("(%p)->(%p %p) No support for unknown IUri's yet.\n", iface, pUri, pfEqual);
4609 return E_NOTIMPL;
4612 TRACE("comparing to %s\n", debugstr_w(other->canon_uri));
4613 return compare_uris(This, other, pfEqual);
4616 static const IUriVtbl UriVtbl = {
4617 Uri_QueryInterface,
4618 Uri_AddRef,
4619 Uri_Release,
4620 Uri_GetPropertyBSTR,
4621 Uri_GetPropertyLength,
4622 Uri_GetPropertyDWORD,
4623 Uri_HasProperty,
4624 Uri_GetAbsoluteUri,
4625 Uri_GetAuthority,
4626 Uri_GetDisplayUri,
4627 Uri_GetDomain,
4628 Uri_GetExtension,
4629 Uri_GetFragment,
4630 Uri_GetHost,
4631 Uri_GetPassword,
4632 Uri_GetPath,
4633 Uri_GetPathAndQuery,
4634 Uri_GetQuery,
4635 Uri_GetRawUri,
4636 Uri_GetSchemeName,
4637 Uri_GetUserInfo,
4638 Uri_GetUserName,
4639 Uri_GetHostType,
4640 Uri_GetPort,
4641 Uri_GetScheme,
4642 Uri_GetZone,
4643 Uri_GetProperties,
4644 Uri_IsEqual
4647 static inline Uri* impl_from_IUriBuilderFactory(IUriBuilderFactory *iface)
4649 return CONTAINING_RECORD(iface, Uri, IUriBuilderFactory_iface);
4652 static HRESULT WINAPI UriBuilderFactory_QueryInterface(IUriBuilderFactory *iface, REFIID riid, void **ppv)
4654 Uri *This = impl_from_IUriBuilderFactory(iface);
4655 return IUri_QueryInterface(&This->IUri_iface, riid, ppv);
4658 static ULONG WINAPI UriBuilderFactory_AddRef(IUriBuilderFactory *iface)
4660 Uri *This = impl_from_IUriBuilderFactory(iface);
4661 return IUri_AddRef(&This->IUri_iface);
4664 static ULONG WINAPI UriBuilderFactory_Release(IUriBuilderFactory *iface)
4666 Uri *This = impl_from_IUriBuilderFactory(iface);
4667 return IUri_Release(&This->IUri_iface);
4670 static HRESULT WINAPI UriBuilderFactory_CreateIUriBuilder(IUriBuilderFactory *iface,
4671 DWORD dwFlags,
4672 DWORD_PTR dwReserved,
4673 IUriBuilder **ppIUriBuilder)
4675 Uri *This = impl_from_IUriBuilderFactory(iface);
4676 TRACE("(%p)->(%08x %08x %p)\n", This, dwFlags, (DWORD)dwReserved, ppIUriBuilder);
4678 if(!ppIUriBuilder)
4679 return E_POINTER;
4681 if(dwFlags || dwReserved) {
4682 *ppIUriBuilder = NULL;
4683 return E_INVALIDARG;
4686 return CreateIUriBuilder(NULL, 0, 0, ppIUriBuilder);
4689 static HRESULT WINAPI UriBuilderFactory_CreateInitializedIUriBuilder(IUriBuilderFactory *iface,
4690 DWORD dwFlags,
4691 DWORD_PTR dwReserved,
4692 IUriBuilder **ppIUriBuilder)
4694 Uri *This = impl_from_IUriBuilderFactory(iface);
4695 TRACE("(%p)->(%08x %08x %p)\n", This, dwFlags, (DWORD)dwReserved, ppIUriBuilder);
4697 if(!ppIUriBuilder)
4698 return E_POINTER;
4700 if(dwFlags || dwReserved) {
4701 *ppIUriBuilder = NULL;
4702 return E_INVALIDARG;
4705 return CreateIUriBuilder(&This->IUri_iface, 0, 0, ppIUriBuilder);
4708 static const IUriBuilderFactoryVtbl UriBuilderFactoryVtbl = {
4709 UriBuilderFactory_QueryInterface,
4710 UriBuilderFactory_AddRef,
4711 UriBuilderFactory_Release,
4712 UriBuilderFactory_CreateIUriBuilder,
4713 UriBuilderFactory_CreateInitializedIUriBuilder
4716 static inline Uri* impl_from_IPersistStream(IPersistStream *iface)
4718 return CONTAINING_RECORD(iface, Uri, IPersistStream_iface);
4721 static HRESULT WINAPI PersistStream_QueryInterface(IPersistStream *iface, REFIID riid, void **ppvObject)
4723 Uri *This = impl_from_IPersistStream(iface);
4724 return IUri_QueryInterface(&This->IUri_iface, riid, ppvObject);
4727 static ULONG WINAPI PersistStream_AddRef(IPersistStream *iface)
4729 Uri *This = impl_from_IPersistStream(iface);
4730 return IUri_AddRef(&This->IUri_iface);
4733 static ULONG WINAPI PersistStream_Release(IPersistStream *iface)
4735 Uri *This = impl_from_IPersistStream(iface);
4736 return IUri_Release(&This->IUri_iface);
4739 static HRESULT WINAPI PersistStream_GetClassID(IPersistStream *iface, CLSID *pClassID)
4741 Uri *This = impl_from_IPersistStream(iface);
4742 TRACE("(%p)->(%p)\n", This, pClassID);
4744 if(!pClassID)
4745 return E_INVALIDARG;
4747 *pClassID = CLSID_CUri;
4748 return S_OK;
4751 static HRESULT WINAPI PersistStream_IsDirty(IPersistStream *iface)
4753 Uri *This = impl_from_IPersistStream(iface);
4754 TRACE("(%p)\n", This);
4755 return S_FALSE;
4758 struct persist_uri {
4759 DWORD size;
4760 DWORD unk1[2];
4761 DWORD create_flags;
4762 DWORD unk2[3];
4763 DWORD fields_no;
4764 BYTE data[1];
4767 static HRESULT WINAPI PersistStream_Load(IPersistStream *iface, IStream *pStm)
4769 Uri *This = impl_from_IPersistStream(iface);
4770 struct persist_uri *data;
4771 parse_data parse;
4772 DWORD size;
4773 HRESULT hr;
4775 TRACE("(%p)->(%p)\n", This, pStm);
4777 if(This->create_flags)
4778 return E_UNEXPECTED;
4779 if(!pStm)
4780 return E_INVALIDARG;
4782 hr = IStream_Read(pStm, &size, sizeof(DWORD), NULL);
4783 if(FAILED(hr))
4784 return hr;
4785 data = heap_alloc(size);
4786 if(!data)
4787 return E_OUTOFMEMORY;
4788 hr = IStream_Read(pStm, data->unk1, size-sizeof(DWORD)-2, NULL);
4789 if(FAILED(hr)) {
4790 heap_free(data);
4791 return hr;
4794 if(size < sizeof(struct persist_uri)) {
4795 heap_free(data);
4796 return S_OK;
4799 if(*(DWORD*)data->data != Uri_PROPERTY_RAW_URI) {
4800 heap_free(data);
4801 ERR("Can't find raw_uri\n");
4802 return E_UNEXPECTED;
4805 This->raw_uri = SysAllocString((WCHAR*)(data->data+sizeof(DWORD)*2));
4806 if(!This->raw_uri) {
4807 heap_free(data);
4808 return E_OUTOFMEMORY;
4810 This->create_flags = data->create_flags;
4811 heap_free(data);
4812 TRACE("%x %s\n", This->create_flags, debugstr_w(This->raw_uri));
4814 memset(&parse, 0, sizeof(parse_data));
4815 parse.uri = This->raw_uri;
4816 if(!parse_uri(&parse, This->create_flags)) {
4817 SysFreeString(This->raw_uri);
4818 This->create_flags = 0;
4819 return E_UNEXPECTED;
4822 hr = canonicalize_uri(&parse, This, This->create_flags);
4823 if(FAILED(hr)) {
4824 SysFreeString(This->raw_uri);
4825 This->create_flags = 0;
4826 return hr;
4829 return S_OK;
4832 static inline BYTE* persist_stream_add_strprop(Uri *This, BYTE *p, DWORD type, DWORD len, WCHAR *data)
4834 len *= sizeof(WCHAR);
4835 *(DWORD*)p = type;
4836 p += sizeof(DWORD);
4837 *(DWORD*)p = len+sizeof(WCHAR);
4838 p += sizeof(DWORD);
4839 memcpy(p, data, len);
4840 p += len;
4841 *(WCHAR*)p = 0;
4842 return p+sizeof(WCHAR);
4845 static inline void persist_stream_save(Uri *This, IStream *pStm, BOOL marshal, struct persist_uri *data)
4847 BYTE *p = NULL;
4849 data->create_flags = This->create_flags;
4851 if(This->create_flags) {
4852 data->fields_no = 1;
4853 p = persist_stream_add_strprop(This, data->data, Uri_PROPERTY_RAW_URI,
4854 SysStringLen(This->raw_uri), This->raw_uri);
4856 if(This->scheme_type!=URL_SCHEME_HTTP && This->scheme_type!=URL_SCHEME_HTTPS
4857 && This->scheme_type!=URL_SCHEME_FTP)
4858 return;
4860 if(This->fragment_len) {
4861 data->fields_no++;
4862 p = persist_stream_add_strprop(This, p, Uri_PROPERTY_FRAGMENT,
4863 This->fragment_len, This->canon_uri+This->fragment_start);
4866 if(This->host_len) {
4867 data->fields_no++;
4868 if(This->host_type == Uri_HOST_IPV6)
4869 p = persist_stream_add_strprop(This, p, Uri_PROPERTY_HOST,
4870 This->host_len-2, This->canon_uri+This->host_start+1);
4871 else
4872 p = persist_stream_add_strprop(This, p, Uri_PROPERTY_HOST,
4873 This->host_len, This->canon_uri+This->host_start);
4876 if(This->userinfo_split > -1) {
4877 data->fields_no++;
4878 p = persist_stream_add_strprop(This, p, Uri_PROPERTY_PASSWORD,
4879 This->userinfo_len-This->userinfo_split-1,
4880 This->canon_uri+This->userinfo_start+This->userinfo_split+1);
4883 if(This->path_len) {
4884 data->fields_no++;
4885 p = persist_stream_add_strprop(This, p, Uri_PROPERTY_PATH,
4886 This->path_len, This->canon_uri+This->path_start);
4887 } else if(marshal) {
4888 WCHAR no_path = '/';
4889 data->fields_no++;
4890 p = persist_stream_add_strprop(This, p, Uri_PROPERTY_PATH, 1, &no_path);
4893 if(This->has_port) {
4894 data->fields_no++;
4895 *(DWORD*)p = Uri_PROPERTY_PORT;
4896 p += sizeof(DWORD);
4897 *(DWORD*)p = sizeof(DWORD);
4898 p += sizeof(DWORD);
4899 *(DWORD*)p = This->port;
4900 p += sizeof(DWORD);
4903 if(This->query_len) {
4904 data->fields_no++;
4905 p = persist_stream_add_strprop(This, p, Uri_PROPERTY_QUERY,
4906 This->query_len, This->canon_uri+This->query_start);
4909 if(This->scheme_len) {
4910 data->fields_no++;
4911 p = persist_stream_add_strprop(This, p, Uri_PROPERTY_SCHEME_NAME,
4912 This->scheme_len, This->canon_uri+This->scheme_start);
4915 if(This->userinfo_start>-1 && This->userinfo_split!=0) {
4916 data->fields_no++;
4917 if(This->userinfo_split > -1)
4918 p = persist_stream_add_strprop(This, p, Uri_PROPERTY_USER_NAME,
4919 This->userinfo_split, This->canon_uri+This->userinfo_start);
4920 else
4921 p = persist_stream_add_strprop(This, p, Uri_PROPERTY_USER_NAME,
4922 This->userinfo_len, This->canon_uri+This->userinfo_start);
4926 static HRESULT WINAPI PersistStream_Save(IPersistStream *iface, IStream *pStm, BOOL fClearDirty)
4928 Uri *This = impl_from_IPersistStream(iface);
4929 struct persist_uri *data;
4930 ULARGE_INTEGER size;
4931 HRESULT hres;
4933 TRACE("(%p)->(%p %x)\n", This, pStm, fClearDirty);
4935 if(!pStm)
4936 return E_INVALIDARG;
4938 hres = IPersistStream_GetSizeMax(&This->IPersistStream_iface, &size);
4939 if(FAILED(hres))
4940 return hres;
4942 data = heap_alloc_zero(size.u.LowPart);
4943 if(!data)
4944 return E_OUTOFMEMORY;
4945 data->size = size.u.LowPart;
4946 persist_stream_save(This, pStm, FALSE, data);
4948 hres = IStream_Write(pStm, data, data->size-2, NULL);
4949 heap_free(data);
4950 return hres;
4953 static HRESULT WINAPI PersistStream_GetSizeMax(IPersistStream *iface, ULARGE_INTEGER *pcbSize)
4955 Uri *This = impl_from_IPersistStream(iface);
4956 TRACE("(%p)->(%p)\n", This, pcbSize);
4958 if(!pcbSize)
4959 return E_INVALIDARG;
4961 pcbSize->u.LowPart = 2+sizeof(struct persist_uri);
4962 pcbSize->u.HighPart = 0;
4963 if(This->create_flags)
4964 pcbSize->u.LowPart += (SysStringLen(This->raw_uri)+1)*sizeof(WCHAR) + 2*sizeof(DWORD);
4965 else /* there's no place for fields no */
4966 pcbSize->u.LowPart -= sizeof(DWORD);
4967 if(This->scheme_type!=URL_SCHEME_HTTP && This->scheme_type!=URL_SCHEME_HTTPS
4968 && This->scheme_type!=URL_SCHEME_FTP)
4969 return S_OK;
4971 if(This->fragment_len)
4972 pcbSize->u.LowPart += (This->fragment_len+1)*sizeof(WCHAR) + 2*sizeof(DWORD);
4973 if(This->host_len) {
4974 if(This->host_type == Uri_HOST_IPV6)
4975 pcbSize->u.LowPart += (This->host_len-1)*sizeof(WCHAR) + 2*sizeof(DWORD);
4976 else
4977 pcbSize->u.LowPart += (This->host_len+1)*sizeof(WCHAR) + 2*sizeof(DWORD);
4979 if(This->userinfo_split > -1)
4980 pcbSize->u.LowPart += (This->userinfo_len-This->userinfo_split)*sizeof(WCHAR) + 2*sizeof(DWORD);
4981 if(This->path_len)
4982 pcbSize->u.LowPart += (This->path_len+1)*sizeof(WCHAR) + 2*sizeof(DWORD);
4983 if(This->has_port)
4984 pcbSize->u.LowPart += 3*sizeof(DWORD);
4985 if(This->query_len)
4986 pcbSize->u.LowPart += (This->query_len+1)*sizeof(WCHAR) + 2*sizeof(DWORD);
4987 if(This->scheme_len)
4988 pcbSize->u.LowPart += (This->scheme_len+1)*sizeof(WCHAR) + 2*sizeof(DWORD);
4989 if(This->userinfo_start>-1 && This->userinfo_split!=0) {
4990 if(This->userinfo_split > -1)
4991 pcbSize->u.LowPart += (This->userinfo_split+1)*sizeof(WCHAR) + 2*sizeof(DWORD);
4992 else
4993 pcbSize->u.LowPart += (This->userinfo_len+1)*sizeof(WCHAR) + 2*sizeof(DWORD);
4995 return S_OK;
4998 static const IPersistStreamVtbl PersistStreamVtbl = {
4999 PersistStream_QueryInterface,
5000 PersistStream_AddRef,
5001 PersistStream_Release,
5002 PersistStream_GetClassID,
5003 PersistStream_IsDirty,
5004 PersistStream_Load,
5005 PersistStream_Save,
5006 PersistStream_GetSizeMax
5009 static inline Uri* impl_from_IMarshal(IMarshal *iface)
5011 return CONTAINING_RECORD(iface, Uri, IMarshal_iface);
5014 static HRESULT WINAPI Marshal_QueryInterface(IMarshal *iface, REFIID riid, void **ppvObject)
5016 Uri *This = impl_from_IMarshal(iface);
5017 return IUri_QueryInterface(&This->IUri_iface, riid, ppvObject);
5020 static ULONG WINAPI Marshal_AddRef(IMarshal *iface)
5022 Uri *This = impl_from_IMarshal(iface);
5023 return IUri_AddRef(&This->IUri_iface);
5026 static ULONG WINAPI Marshal_Release(IMarshal *iface)
5028 Uri *This = impl_from_IMarshal(iface);
5029 return IUri_Release(&This->IUri_iface);
5032 static HRESULT WINAPI Marshal_GetUnmarshalClass(IMarshal *iface, REFIID riid, void *pv,
5033 DWORD dwDestContext, void *pvDestContext, DWORD mshlflags, CLSID *pCid)
5035 Uri *This = impl_from_IMarshal(iface);
5036 TRACE("(%p)->(%s %p %x %p %x %p)\n", This, debugstr_guid(riid), pv,
5037 dwDestContext, pvDestContext, mshlflags, pCid);
5039 if(!pCid || (dwDestContext!=MSHCTX_LOCAL && dwDestContext!=MSHCTX_NOSHAREDMEM
5040 && dwDestContext!=MSHCTX_INPROC))
5041 return E_INVALIDARG;
5043 *pCid = CLSID_CUri;
5044 return S_OK;
5047 struct inproc_marshal_uri {
5048 DWORD size;
5049 DWORD mshlflags;
5050 DWORD unk[4]; /* process identifier? */
5051 Uri *uri;
5054 static HRESULT WINAPI Marshal_GetMarshalSizeMax(IMarshal *iface, REFIID riid, void *pv,
5055 DWORD dwDestContext, void *pvDestContext, DWORD mshlflags, DWORD *pSize)
5057 Uri *This = impl_from_IMarshal(iface);
5058 ULARGE_INTEGER size;
5059 HRESULT hres;
5060 TRACE("(%p)->(%s %p %x %p %x %p)\n", This, debugstr_guid(riid), pv,
5061 dwDestContext, pvDestContext, mshlflags, pSize);
5063 if(!pSize || (dwDestContext!=MSHCTX_LOCAL && dwDestContext!=MSHCTX_NOSHAREDMEM
5064 && dwDestContext!=MSHCTX_INPROC))
5065 return E_INVALIDARG;
5067 if(dwDestContext == MSHCTX_INPROC) {
5068 *pSize = sizeof(struct inproc_marshal_uri);
5069 return S_OK;
5072 hres = IPersistStream_GetSizeMax(&This->IPersistStream_iface, &size);
5073 if(FAILED(hres))
5074 return hres;
5075 if(!This->path_len && (This->scheme_type==URL_SCHEME_HTTP
5076 || This->scheme_type==URL_SCHEME_HTTPS
5077 || This->scheme_type==URL_SCHEME_FTP))
5078 size.u.LowPart += 3*sizeof(DWORD);
5079 *pSize = size.u.LowPart+2*sizeof(DWORD);
5080 return S_OK;
5083 static HRESULT WINAPI Marshal_MarshalInterface(IMarshal *iface, IStream *pStm, REFIID riid,
5084 void *pv, DWORD dwDestContext, void *pvDestContext, DWORD mshlflags)
5086 Uri *This = impl_from_IMarshal(iface);
5087 DWORD *data;
5088 DWORD size;
5089 HRESULT hres;
5091 TRACE("(%p)->(%p %s %p %x %p %x)\n", This, pStm, debugstr_guid(riid), pv,
5092 dwDestContext, pvDestContext, mshlflags);
5094 if(!pStm || mshlflags!=MSHLFLAGS_NORMAL || (dwDestContext!=MSHCTX_LOCAL
5095 && dwDestContext!=MSHCTX_NOSHAREDMEM && dwDestContext!=MSHCTX_INPROC))
5096 return E_INVALIDARG;
5098 if(dwDestContext == MSHCTX_INPROC) {
5099 struct inproc_marshal_uri data;
5101 data.size = sizeof(data);
5102 data.mshlflags = MSHCTX_INPROC;
5103 data.unk[0] = 0;
5104 data.unk[1] = 0;
5105 data.unk[2] = 0;
5106 data.unk[3] = 0;
5107 data.uri = This;
5109 hres = IStream_Write(pStm, &data, data.size, NULL);
5110 if(FAILED(hres))
5111 return hres;
5113 IUri_AddRef(&This->IUri_iface);
5114 return S_OK;
5117 hres = IMarshal_GetMarshalSizeMax(iface, riid, pv, dwDestContext,
5118 pvDestContext, mshlflags, &size);
5119 if(FAILED(hres))
5120 return hres;
5122 data = heap_alloc_zero(size);
5123 if(!data)
5124 return E_OUTOFMEMORY;
5126 data[0] = size;
5127 data[1] = dwDestContext;
5128 data[2] = size-2*sizeof(DWORD);
5129 persist_stream_save(This, pStm, TRUE, (struct persist_uri*)(data+2));
5131 hres = IStream_Write(pStm, data, data[0]-2, NULL);
5132 heap_free(data);
5133 return hres;
5136 static HRESULT WINAPI Marshal_UnmarshalInterface(IMarshal *iface,
5137 IStream *pStm, REFIID riid, void **ppv)
5139 Uri *This = impl_from_IMarshal(iface);
5140 DWORD header[2];
5141 HRESULT hres;
5143 TRACE("(%p)->(%p %s %p)\n", This, pStm, debugstr_guid(riid), ppv);
5145 if(This->create_flags)
5146 return E_UNEXPECTED;
5147 if(!pStm || !riid || !ppv)
5148 return E_INVALIDARG;
5150 hres = IStream_Read(pStm, header, sizeof(header), NULL);
5151 if(FAILED(hres))
5152 return hres;
5154 if(header[1]!=MSHCTX_LOCAL && header[1]!=MSHCTX_NOSHAREDMEM
5155 && header[1]!=MSHCTX_INPROC)
5156 return E_UNEXPECTED;
5158 if(header[1] == MSHCTX_INPROC) {
5159 struct inproc_marshal_uri data;
5160 parse_data parse;
5162 hres = IStream_Read(pStm, data.unk, sizeof(data)-2*sizeof(DWORD), NULL);
5163 if(FAILED(hres))
5164 return hres;
5166 This->raw_uri = SysAllocString(data.uri->raw_uri);
5167 if(!This->raw_uri) {
5168 return E_OUTOFMEMORY;
5171 memset(&parse, 0, sizeof(parse_data));
5172 parse.uri = This->raw_uri;
5174 if(!parse_uri(&parse, data.uri->create_flags))
5175 return E_INVALIDARG;
5177 hres = canonicalize_uri(&parse, This, data.uri->create_flags);
5178 if(FAILED(hres))
5179 return hres;
5181 This->create_flags = data.uri->create_flags;
5182 IUri_Release(&data.uri->IUri_iface);
5184 return IUri_QueryInterface(&This->IUri_iface, riid, ppv);
5187 hres = IPersistStream_Load(&This->IPersistStream_iface, pStm);
5188 if(FAILED(hres))
5189 return hres;
5191 return IUri_QueryInterface(&This->IUri_iface, riid, ppv);
5194 static HRESULT WINAPI Marshal_ReleaseMarshalData(IMarshal *iface, IStream *pStm)
5196 Uri *This = impl_from_IMarshal(iface);
5197 LARGE_INTEGER off;
5198 DWORD header[2];
5199 HRESULT hres;
5201 TRACE("(%p)->(%p)\n", This, pStm);
5203 if(!pStm)
5204 return E_INVALIDARG;
5206 hres = IStream_Read(pStm, header, 2*sizeof(DWORD), NULL);
5207 if(FAILED(hres))
5208 return hres;
5210 if(header[1] == MSHCTX_INPROC) {
5211 struct inproc_marshal_uri data;
5213 hres = IStream_Read(pStm, data.unk, sizeof(data)-2*sizeof(DWORD), NULL);
5214 if(FAILED(hres))
5215 return hres;
5217 IUri_Release(&data.uri->IUri_iface);
5218 return S_OK;
5221 off.u.LowPart = header[0]-sizeof(header)-2;
5222 off.u.HighPart = 0;
5223 return IStream_Seek(pStm, off, STREAM_SEEK_CUR, NULL);
5226 static HRESULT WINAPI Marshal_DisconnectObject(IMarshal *iface, DWORD dwReserved)
5228 Uri *This = impl_from_IMarshal(iface);
5229 TRACE("(%p)->(%x)\n", This, dwReserved);
5230 return S_OK;
5233 static const IMarshalVtbl MarshalVtbl = {
5234 Marshal_QueryInterface,
5235 Marshal_AddRef,
5236 Marshal_Release,
5237 Marshal_GetUnmarshalClass,
5238 Marshal_GetMarshalSizeMax,
5239 Marshal_MarshalInterface,
5240 Marshal_UnmarshalInterface,
5241 Marshal_ReleaseMarshalData,
5242 Marshal_DisconnectObject
5245 HRESULT Uri_Construct(IUnknown *pUnkOuter, LPVOID *ppobj)
5247 Uri *ret = heap_alloc_zero(sizeof(Uri));
5249 TRACE("(%p %p)\n", pUnkOuter, ppobj);
5251 *ppobj = ret;
5252 if(!ret)
5253 return E_OUTOFMEMORY;
5255 ret->IUri_iface.lpVtbl = &UriVtbl;
5256 ret->IUriBuilderFactory_iface.lpVtbl = &UriBuilderFactoryVtbl;
5257 ret->IPersistStream_iface.lpVtbl = &PersistStreamVtbl;
5258 ret->IMarshal_iface.lpVtbl = &MarshalVtbl;
5259 ret->ref = 1;
5261 *ppobj = &ret->IUri_iface;
5262 return S_OK;
5265 /***********************************************************************
5266 * CreateUri (urlmon.@)
5268 * Creates a new IUri object using the URI represented by pwzURI. This function
5269 * parses and validates the components of pwzURI and then canonicalizes the
5270 * parsed components.
5272 * PARAMS
5273 * pwzURI [I] The URI to parse, validate, and canonicalize.
5274 * dwFlags [I] Flags which can affect how the parsing/canonicalization is performed.
5275 * dwReserved [I] Reserved (not used).
5276 * ppURI [O] The resulting IUri after parsing/canonicalization occurs.
5278 * RETURNS
5279 * Success: Returns S_OK. ppURI contains the pointer to the newly allocated IUri.
5280 * Failure: E_INVALIDARG if there are invalid flag combinations in dwFlags, or an
5281 * invalid parameter, or pwzURI doesn't represent a valid URI.
5282 * E_OUTOFMEMORY if any memory allocation fails.
5284 * NOTES
5285 * Default flags:
5286 * Uri_CREATE_CANONICALIZE, Uri_CREATE_DECODE_EXTRA_INFO, Uri_CREATE_CRACK_UNKNOWN_SCHEMES,
5287 * Uri_CREATE_PRE_PROCESS_HTML_URI, Uri_CREATE_NO_IE_SETTINGS.
5289 HRESULT WINAPI CreateUri(LPCWSTR pwzURI, DWORD dwFlags, DWORD_PTR dwReserved, IUri **ppURI)
5291 const DWORD supported_flags = Uri_CREATE_ALLOW_RELATIVE|Uri_CREATE_ALLOW_IMPLICIT_WILDCARD_SCHEME|
5292 Uri_CREATE_ALLOW_IMPLICIT_FILE_SCHEME|Uri_CREATE_NO_CANONICALIZE|Uri_CREATE_CANONICALIZE|
5293 Uri_CREATE_DECODE_EXTRA_INFO|Uri_CREATE_NO_DECODE_EXTRA_INFO|Uri_CREATE_CRACK_UNKNOWN_SCHEMES|
5294 Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES|Uri_CREATE_PRE_PROCESS_HTML_URI|Uri_CREATE_NO_PRE_PROCESS_HTML_URI|
5295 Uri_CREATE_NO_IE_SETTINGS|Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS|Uri_CREATE_FILE_USE_DOS_PATH;
5296 Uri *ret;
5297 HRESULT hr;
5298 parse_data data;
5300 TRACE("(%s %x %x %p)\n", debugstr_w(pwzURI), dwFlags, (DWORD)dwReserved, ppURI);
5302 if(!ppURI)
5303 return E_INVALIDARG;
5305 if(!pwzURI) {
5306 *ppURI = NULL;
5307 return E_INVALIDARG;
5310 /* Check for invalid flags. */
5311 if(has_invalid_flag_combination(dwFlags)) {
5312 *ppURI = NULL;
5313 return E_INVALIDARG;
5316 /* Currently unsupported. */
5317 if(dwFlags & ~supported_flags)
5318 FIXME("Ignoring unsupported flag(s) %x\n", dwFlags & ~supported_flags);
5320 hr = Uri_Construct(NULL, (void**)&ret);
5321 if(FAILED(hr)) {
5322 *ppURI = NULL;
5323 return hr;
5326 /* Explicitly set the default flags if it doesn't cause a flag conflict. */
5327 apply_default_flags(&dwFlags);
5329 /* Pre process the URI, unless told otherwise. */
5330 if(!(dwFlags & Uri_CREATE_NO_PRE_PROCESS_HTML_URI))
5331 ret->raw_uri = pre_process_uri(pwzURI);
5332 else
5333 ret->raw_uri = SysAllocString(pwzURI);
5335 if(!ret->raw_uri) {
5336 heap_free(ret);
5337 return E_OUTOFMEMORY;
5340 memset(&data, 0, sizeof(parse_data));
5341 data.uri = ret->raw_uri;
5343 /* Validate and parse the URI into its components. */
5344 if(!parse_uri(&data, dwFlags)) {
5345 /* Encountered an unsupported or invalid URI */
5346 IUri_Release(&ret->IUri_iface);
5347 *ppURI = NULL;
5348 return E_INVALIDARG;
5351 /* Canonicalize the URI. */
5352 hr = canonicalize_uri(&data, ret, dwFlags);
5353 if(FAILED(hr)) {
5354 IUri_Release(&ret->IUri_iface);
5355 *ppURI = NULL;
5356 return hr;
5359 ret->create_flags = dwFlags;
5361 *ppURI = &ret->IUri_iface;
5362 return S_OK;
5365 /***********************************************************************
5366 * CreateUriWithFragment (urlmon.@)
5368 * Creates a new IUri object. This is almost the same as CreateUri, expect that
5369 * it allows you to explicitly specify a fragment (pwzFragment) for pwzURI.
5371 * PARAMS
5372 * pwzURI [I] The URI to parse and perform canonicalization on.
5373 * pwzFragment [I] The explicit fragment string which should be added to pwzURI.
5374 * dwFlags [I] The flags which will be passed to CreateUri.
5375 * dwReserved [I] Reserved (not used).
5376 * ppURI [O] The resulting IUri after parsing/canonicalization.
5378 * RETURNS
5379 * Success: S_OK. ppURI contains the pointer to the newly allocated IUri.
5380 * Failure: E_INVALIDARG if pwzURI already contains a fragment and pwzFragment
5381 * isn't NULL. Will also return E_INVALIDARG for the same reasons as
5382 * CreateUri will. E_OUTOFMEMORY if any allocation fails.
5384 HRESULT WINAPI CreateUriWithFragment(LPCWSTR pwzURI, LPCWSTR pwzFragment, DWORD dwFlags,
5385 DWORD_PTR dwReserved, IUri **ppURI)
5387 HRESULT hres;
5388 TRACE("(%s %s %x %x %p)\n", debugstr_w(pwzURI), debugstr_w(pwzFragment), dwFlags, (DWORD)dwReserved, ppURI);
5390 if(!ppURI)
5391 return E_INVALIDARG;
5393 if(!pwzURI) {
5394 *ppURI = NULL;
5395 return E_INVALIDARG;
5398 /* Check if a fragment should be appended to the URI string. */
5399 if(pwzFragment) {
5400 WCHAR *uriW;
5401 DWORD uri_len, frag_len;
5402 BOOL add_pound;
5404 /* Check if the original URI already has a fragment component. */
5405 if(StrChrW(pwzURI, '#')) {
5406 *ppURI = NULL;
5407 return E_INVALIDARG;
5410 uri_len = lstrlenW(pwzURI);
5411 frag_len = lstrlenW(pwzFragment);
5413 /* If the fragment doesn't start with a '#', one will be added. */
5414 add_pound = *pwzFragment != '#';
5416 if(add_pound)
5417 uriW = heap_alloc((uri_len+frag_len+2)*sizeof(WCHAR));
5418 else
5419 uriW = heap_alloc((uri_len+frag_len+1)*sizeof(WCHAR));
5421 if(!uriW)
5422 return E_OUTOFMEMORY;
5424 memcpy(uriW, pwzURI, uri_len*sizeof(WCHAR));
5425 if(add_pound)
5426 uriW[uri_len++] = '#';
5427 memcpy(uriW+uri_len, pwzFragment, (frag_len+1)*sizeof(WCHAR));
5429 hres = CreateUri(uriW, dwFlags, 0, ppURI);
5431 heap_free(uriW);
5432 } else
5433 /* A fragment string wasn't specified, so just forward the call. */
5434 hres = CreateUri(pwzURI, dwFlags, 0, ppURI);
5436 return hres;
5439 static HRESULT build_uri(const UriBuilder *builder, IUri **uri, DWORD create_flags,
5440 DWORD use_orig_flags, DWORD encoding_mask)
5442 HRESULT hr;
5443 parse_data data;
5444 Uri *ret;
5446 if(!uri)
5447 return E_POINTER;
5449 if(encoding_mask && (!builder->uri || builder->modified_props)) {
5450 *uri = NULL;
5451 return E_NOTIMPL;
5454 /* Decide what flags should be used when creating the Uri. */
5455 if((use_orig_flags & UriBuilder_USE_ORIGINAL_FLAGS) && builder->uri)
5456 create_flags = builder->uri->create_flags;
5457 else {
5458 if(has_invalid_flag_combination(create_flags)) {
5459 *uri = NULL;
5460 return E_INVALIDARG;
5463 /* Set the default flags if they don't cause a conflict. */
5464 apply_default_flags(&create_flags);
5467 /* Return the base IUri if no changes have been made and the create_flags match. */
5468 if(builder->uri && !builder->modified_props && builder->uri->create_flags == create_flags) {
5469 *uri = &builder->uri->IUri_iface;
5470 IUri_AddRef(*uri);
5471 return S_OK;
5474 hr = validate_components(builder, &data, create_flags);
5475 if(FAILED(hr)) {
5476 *uri = NULL;
5477 return hr;
5480 hr = Uri_Construct(NULL, (void**)&ret);
5481 if(FAILED(hr)) {
5482 *uri = NULL;
5483 return hr;
5486 hr = generate_uri(builder, &data, ret, create_flags);
5487 if(FAILED(hr)) {
5488 IUri_Release(&ret->IUri_iface);
5489 *uri = NULL;
5490 return hr;
5493 *uri = &ret->IUri_iface;
5494 return S_OK;
5497 static inline UriBuilder* impl_from_IUriBuilder(IUriBuilder *iface)
5499 return CONTAINING_RECORD(iface, UriBuilder, IUriBuilder_iface);
5502 static HRESULT WINAPI UriBuilder_QueryInterface(IUriBuilder *iface, REFIID riid, void **ppv)
5504 UriBuilder *This = impl_from_IUriBuilder(iface);
5506 if(IsEqualGUID(&IID_IUnknown, riid)) {
5507 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
5508 *ppv = &This->IUriBuilder_iface;
5509 }else if(IsEqualGUID(&IID_IUriBuilder, riid)) {
5510 TRACE("(%p)->(IID_IUriBuilder %p)\n", This, ppv);
5511 *ppv = &This->IUriBuilder_iface;
5512 }else {
5513 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
5514 *ppv = NULL;
5515 return E_NOINTERFACE;
5518 IUnknown_AddRef((IUnknown*)*ppv);
5519 return S_OK;
5522 static ULONG WINAPI UriBuilder_AddRef(IUriBuilder *iface)
5524 UriBuilder *This = impl_from_IUriBuilder(iface);
5525 LONG ref = InterlockedIncrement(&This->ref);
5527 TRACE("(%p) ref=%d\n", This, ref);
5529 return ref;
5532 static ULONG WINAPI UriBuilder_Release(IUriBuilder *iface)
5534 UriBuilder *This = impl_from_IUriBuilder(iface);
5535 LONG ref = InterlockedDecrement(&This->ref);
5537 TRACE("(%p) ref=%d\n", This, ref);
5539 if(!ref) {
5540 if(This->uri) IUri_Release(&This->uri->IUri_iface);
5541 heap_free(This->fragment);
5542 heap_free(This->host);
5543 heap_free(This->password);
5544 heap_free(This->path);
5545 heap_free(This->query);
5546 heap_free(This->scheme);
5547 heap_free(This->username);
5548 heap_free(This);
5551 return ref;
5554 static HRESULT WINAPI UriBuilder_CreateUriSimple(IUriBuilder *iface,
5555 DWORD dwAllowEncodingPropertyMask,
5556 DWORD_PTR dwReserved,
5557 IUri **ppIUri)
5559 UriBuilder *This = impl_from_IUriBuilder(iface);
5560 HRESULT hr;
5561 TRACE("(%p)->(%d %d %p)\n", This, dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
5563 hr = build_uri(This, ppIUri, 0, UriBuilder_USE_ORIGINAL_FLAGS, dwAllowEncodingPropertyMask);
5564 if(hr == E_NOTIMPL)
5565 FIXME("(%p)->(%d %d %p)\n", This, dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
5566 return hr;
5569 static HRESULT WINAPI UriBuilder_CreateUri(IUriBuilder *iface,
5570 DWORD dwCreateFlags,
5571 DWORD dwAllowEncodingPropertyMask,
5572 DWORD_PTR dwReserved,
5573 IUri **ppIUri)
5575 UriBuilder *This = impl_from_IUriBuilder(iface);
5576 HRESULT hr;
5577 TRACE("(%p)->(0x%08x %d %d %p)\n", This, dwCreateFlags, dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
5579 if(dwCreateFlags == -1)
5580 hr = build_uri(This, ppIUri, 0, UriBuilder_USE_ORIGINAL_FLAGS, dwAllowEncodingPropertyMask);
5581 else
5582 hr = build_uri(This, ppIUri, dwCreateFlags, 0, dwAllowEncodingPropertyMask);
5584 if(hr == E_NOTIMPL)
5585 FIXME("(%p)->(0x%08x %d %d %p)\n", This, dwCreateFlags, dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
5586 return hr;
5589 static HRESULT WINAPI UriBuilder_CreateUriWithFlags(IUriBuilder *iface,
5590 DWORD dwCreateFlags,
5591 DWORD dwUriBuilderFlags,
5592 DWORD dwAllowEncodingPropertyMask,
5593 DWORD_PTR dwReserved,
5594 IUri **ppIUri)
5596 UriBuilder *This = impl_from_IUriBuilder(iface);
5597 HRESULT hr;
5598 TRACE("(%p)->(0x%08x 0x%08x %d %d %p)\n", This, dwCreateFlags, dwUriBuilderFlags,
5599 dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
5601 hr = build_uri(This, ppIUri, dwCreateFlags, dwUriBuilderFlags, dwAllowEncodingPropertyMask);
5602 if(hr == E_NOTIMPL)
5603 FIXME("(%p)->(0x%08x 0x%08x %d %d %p)\n", This, dwCreateFlags, dwUriBuilderFlags,
5604 dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
5605 return hr;
5608 static HRESULT WINAPI UriBuilder_GetIUri(IUriBuilder *iface, IUri **ppIUri)
5610 UriBuilder *This = impl_from_IUriBuilder(iface);
5611 TRACE("(%p)->(%p)\n", This, ppIUri);
5613 if(!ppIUri)
5614 return E_POINTER;
5616 if(This->uri) {
5617 IUri *uri = &This->uri->IUri_iface;
5618 IUri_AddRef(uri);
5619 *ppIUri = uri;
5620 } else
5621 *ppIUri = NULL;
5623 return S_OK;
5626 static HRESULT WINAPI UriBuilder_SetIUri(IUriBuilder *iface, IUri *pIUri)
5628 UriBuilder *This = impl_from_IUriBuilder(iface);
5629 TRACE("(%p)->(%p)\n", This, pIUri);
5631 if(pIUri) {
5632 Uri *uri;
5634 if((uri = get_uri_obj(pIUri))) {
5635 /* Only reset the builder if its Uri isn't the same as
5636 * the Uri passed to the function.
5638 if(This->uri != uri) {
5639 reset_builder(This);
5641 This->uri = uri;
5642 if(uri->has_port)
5643 This->port = uri->port;
5645 IUri_AddRef(pIUri);
5647 } else {
5648 FIXME("(%p)->(%p) Unknown IUri types not supported yet.\n", This, pIUri);
5649 return E_NOTIMPL;
5651 } else if(This->uri)
5652 /* Only reset the builder if its Uri isn't NULL. */
5653 reset_builder(This);
5655 return S_OK;
5658 static HRESULT WINAPI UriBuilder_GetFragment(IUriBuilder *iface, DWORD *pcchFragment, LPCWSTR *ppwzFragment)
5660 UriBuilder *This = impl_from_IUriBuilder(iface);
5661 TRACE("(%p)->(%p %p)\n", This, pcchFragment, ppwzFragment);
5663 if(!This->uri || This->uri->fragment_start == -1 || This->modified_props & Uri_HAS_FRAGMENT)
5664 return get_builder_component(&This->fragment, &This->fragment_len, NULL, 0, ppwzFragment, pcchFragment);
5665 else
5666 return get_builder_component(&This->fragment, &This->fragment_len, This->uri->canon_uri+This->uri->fragment_start,
5667 This->uri->fragment_len, ppwzFragment, pcchFragment);
5670 static HRESULT WINAPI UriBuilder_GetHost(IUriBuilder *iface, DWORD *pcchHost, LPCWSTR *ppwzHost)
5672 UriBuilder *This = impl_from_IUriBuilder(iface);
5673 TRACE("(%p)->(%p %p)\n", This, pcchHost, ppwzHost);
5675 if(!This->uri || This->uri->host_start == -1 || This->modified_props & Uri_HAS_HOST)
5676 return get_builder_component(&This->host, &This->host_len, NULL, 0, ppwzHost, pcchHost);
5677 else {
5678 if(This->uri->host_type == Uri_HOST_IPV6)
5679 /* Don't include the '[' and ']' around the address. */
5680 return get_builder_component(&This->host, &This->host_len, This->uri->canon_uri+This->uri->host_start+1,
5681 This->uri->host_len-2, ppwzHost, pcchHost);
5682 else
5683 return get_builder_component(&This->host, &This->host_len, This->uri->canon_uri+This->uri->host_start,
5684 This->uri->host_len, ppwzHost, pcchHost);
5688 static HRESULT WINAPI UriBuilder_GetPassword(IUriBuilder *iface, DWORD *pcchPassword, LPCWSTR *ppwzPassword)
5690 UriBuilder *This = impl_from_IUriBuilder(iface);
5691 TRACE("(%p)->(%p %p)\n", This, pcchPassword, ppwzPassword);
5693 if(!This->uri || This->uri->userinfo_split == -1 || This->modified_props & Uri_HAS_PASSWORD)
5694 return get_builder_component(&This->password, &This->password_len, NULL, 0, ppwzPassword, pcchPassword);
5695 else {
5696 const WCHAR *start = This->uri->canon_uri+This->uri->userinfo_start+This->uri->userinfo_split+1;
5697 DWORD len = This->uri->userinfo_len-This->uri->userinfo_split-1;
5698 return get_builder_component(&This->password, &This->password_len, start, len, ppwzPassword, pcchPassword);
5702 static HRESULT WINAPI UriBuilder_GetPath(IUriBuilder *iface, DWORD *pcchPath, LPCWSTR *ppwzPath)
5704 UriBuilder *This = impl_from_IUriBuilder(iface);
5705 TRACE("(%p)->(%p %p)\n", This, pcchPath, ppwzPath);
5707 if(!This->uri || This->uri->path_start == -1 || This->modified_props & Uri_HAS_PATH)
5708 return get_builder_component(&This->path, &This->path_len, NULL, 0, ppwzPath, pcchPath);
5709 else
5710 return get_builder_component(&This->path, &This->path_len, This->uri->canon_uri+This->uri->path_start,
5711 This->uri->path_len, ppwzPath, pcchPath);
5714 static HRESULT WINAPI UriBuilder_GetPort(IUriBuilder *iface, BOOL *pfHasPort, DWORD *pdwPort)
5716 UriBuilder *This = impl_from_IUriBuilder(iface);
5717 TRACE("(%p)->(%p %p)\n", This, pfHasPort, pdwPort);
5719 if(!pfHasPort) {
5720 if(pdwPort)
5721 *pdwPort = 0;
5722 return E_POINTER;
5725 if(!pdwPort) {
5726 *pfHasPort = FALSE;
5727 return E_POINTER;
5730 *pfHasPort = This->has_port;
5731 *pdwPort = This->port;
5732 return S_OK;
5735 static HRESULT WINAPI UriBuilder_GetQuery(IUriBuilder *iface, DWORD *pcchQuery, LPCWSTR *ppwzQuery)
5737 UriBuilder *This = impl_from_IUriBuilder(iface);
5738 TRACE("(%p)->(%p %p)\n", This, pcchQuery, ppwzQuery);
5740 if(!This->uri || This->uri->query_start == -1 || This->modified_props & Uri_HAS_QUERY)
5741 return get_builder_component(&This->query, &This->query_len, NULL, 0, ppwzQuery, pcchQuery);
5742 else
5743 return get_builder_component(&This->query, &This->query_len, This->uri->canon_uri+This->uri->query_start,
5744 This->uri->query_len, ppwzQuery, pcchQuery);
5747 static HRESULT WINAPI UriBuilder_GetSchemeName(IUriBuilder *iface, DWORD *pcchSchemeName, LPCWSTR *ppwzSchemeName)
5749 UriBuilder *This = impl_from_IUriBuilder(iface);
5750 TRACE("(%p)->(%p %p)\n", This, pcchSchemeName, ppwzSchemeName);
5752 if(!This->uri || This->uri->scheme_start == -1 || This->modified_props & Uri_HAS_SCHEME_NAME)
5753 return get_builder_component(&This->scheme, &This->scheme_len, NULL, 0, ppwzSchemeName, pcchSchemeName);
5754 else
5755 return get_builder_component(&This->scheme, &This->scheme_len, This->uri->canon_uri+This->uri->scheme_start,
5756 This->uri->scheme_len, ppwzSchemeName, pcchSchemeName);
5759 static HRESULT WINAPI UriBuilder_GetUserName(IUriBuilder *iface, DWORD *pcchUserName, LPCWSTR *ppwzUserName)
5761 UriBuilder *This = impl_from_IUriBuilder(iface);
5762 TRACE("(%p)->(%p %p)\n", This, pcchUserName, ppwzUserName);
5764 if(!This->uri || This->uri->userinfo_start == -1 || This->uri->userinfo_split == 0 ||
5765 This->modified_props & Uri_HAS_USER_NAME)
5766 return get_builder_component(&This->username, &This->username_len, NULL, 0, ppwzUserName, pcchUserName);
5767 else {
5768 const WCHAR *start = This->uri->canon_uri+This->uri->userinfo_start;
5770 /* Check if there's a password in the userinfo section. */
5771 if(This->uri->userinfo_split > -1)
5772 /* Don't include the password. */
5773 return get_builder_component(&This->username, &This->username_len, start,
5774 This->uri->userinfo_split, ppwzUserName, pcchUserName);
5775 else
5776 return get_builder_component(&This->username, &This->username_len, start,
5777 This->uri->userinfo_len, ppwzUserName, pcchUserName);
5781 static HRESULT WINAPI UriBuilder_SetFragment(IUriBuilder *iface, LPCWSTR pwzNewValue)
5783 UriBuilder *This = impl_from_IUriBuilder(iface);
5784 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
5785 return set_builder_component(&This->fragment, &This->fragment_len, pwzNewValue, '#',
5786 &This->modified_props, Uri_HAS_FRAGMENT);
5789 static HRESULT WINAPI UriBuilder_SetHost(IUriBuilder *iface, LPCWSTR pwzNewValue)
5791 UriBuilder *This = impl_from_IUriBuilder(iface);
5792 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
5794 /* Host name can't be set to NULL. */
5795 if(!pwzNewValue)
5796 return E_INVALIDARG;
5798 return set_builder_component(&This->host, &This->host_len, pwzNewValue, 0,
5799 &This->modified_props, Uri_HAS_HOST);
5802 static HRESULT WINAPI UriBuilder_SetPassword(IUriBuilder *iface, LPCWSTR pwzNewValue)
5804 UriBuilder *This = impl_from_IUriBuilder(iface);
5805 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
5806 return set_builder_component(&This->password, &This->password_len, pwzNewValue, 0,
5807 &This->modified_props, Uri_HAS_PASSWORD);
5810 static HRESULT WINAPI UriBuilder_SetPath(IUriBuilder *iface, LPCWSTR pwzNewValue)
5812 UriBuilder *This = impl_from_IUriBuilder(iface);
5813 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
5814 return set_builder_component(&This->path, &This->path_len, pwzNewValue, 0,
5815 &This->modified_props, Uri_HAS_PATH);
5818 static HRESULT WINAPI UriBuilder_SetPort(IUriBuilder *iface, BOOL fHasPort, DWORD dwNewValue)
5820 UriBuilder *This = impl_from_IUriBuilder(iface);
5821 TRACE("(%p)->(%d %d)\n", This, fHasPort, dwNewValue);
5823 This->has_port = fHasPort;
5824 This->port = dwNewValue;
5825 This->modified_props |= Uri_HAS_PORT;
5826 return S_OK;
5829 static HRESULT WINAPI UriBuilder_SetQuery(IUriBuilder *iface, LPCWSTR pwzNewValue)
5831 UriBuilder *This = impl_from_IUriBuilder(iface);
5832 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
5833 return set_builder_component(&This->query, &This->query_len, pwzNewValue, '?',
5834 &This->modified_props, Uri_HAS_QUERY);
5837 static HRESULT WINAPI UriBuilder_SetSchemeName(IUriBuilder *iface, LPCWSTR pwzNewValue)
5839 UriBuilder *This = impl_from_IUriBuilder(iface);
5840 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
5842 /* Only set the scheme name if it's not NULL or empty. */
5843 if(!pwzNewValue || !*pwzNewValue)
5844 return E_INVALIDARG;
5846 return set_builder_component(&This->scheme, &This->scheme_len, pwzNewValue, 0,
5847 &This->modified_props, Uri_HAS_SCHEME_NAME);
5850 static HRESULT WINAPI UriBuilder_SetUserName(IUriBuilder *iface, LPCWSTR pwzNewValue)
5852 UriBuilder *This = impl_from_IUriBuilder(iface);
5853 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
5854 return set_builder_component(&This->username, &This->username_len, pwzNewValue, 0,
5855 &This->modified_props, Uri_HAS_USER_NAME);
5858 static HRESULT WINAPI UriBuilder_RemoveProperties(IUriBuilder *iface, DWORD dwPropertyMask)
5860 const DWORD accepted_flags = Uri_HAS_AUTHORITY|Uri_HAS_DOMAIN|Uri_HAS_EXTENSION|Uri_HAS_FRAGMENT|Uri_HAS_HOST|
5861 Uri_HAS_PASSWORD|Uri_HAS_PATH|Uri_HAS_PATH_AND_QUERY|Uri_HAS_QUERY|
5862 Uri_HAS_USER_INFO|Uri_HAS_USER_NAME;
5864 UriBuilder *This = impl_from_IUriBuilder(iface);
5865 TRACE("(%p)->(0x%08x)\n", This, dwPropertyMask);
5867 if(dwPropertyMask & ~accepted_flags)
5868 return E_INVALIDARG;
5870 if(dwPropertyMask & Uri_HAS_FRAGMENT)
5871 UriBuilder_SetFragment(iface, NULL);
5873 /* Even though you can't set the host name to NULL or an
5874 * empty string, you can still remove it... for some reason.
5876 if(dwPropertyMask & Uri_HAS_HOST)
5877 set_builder_component(&This->host, &This->host_len, NULL, 0,
5878 &This->modified_props, Uri_HAS_HOST);
5880 if(dwPropertyMask & Uri_HAS_PASSWORD)
5881 UriBuilder_SetPassword(iface, NULL);
5883 if(dwPropertyMask & Uri_HAS_PATH)
5884 UriBuilder_SetPath(iface, NULL);
5886 if(dwPropertyMask & Uri_HAS_PORT)
5887 UriBuilder_SetPort(iface, FALSE, 0);
5889 if(dwPropertyMask & Uri_HAS_QUERY)
5890 UriBuilder_SetQuery(iface, NULL);
5892 if(dwPropertyMask & Uri_HAS_USER_NAME)
5893 UriBuilder_SetUserName(iface, NULL);
5895 return S_OK;
5898 static HRESULT WINAPI UriBuilder_HasBeenModified(IUriBuilder *iface, BOOL *pfModified)
5900 UriBuilder *This = impl_from_IUriBuilder(iface);
5901 TRACE("(%p)->(%p)\n", This, pfModified);
5903 if(!pfModified)
5904 return E_POINTER;
5906 *pfModified = This->modified_props > 0;
5907 return S_OK;
5910 static const IUriBuilderVtbl UriBuilderVtbl = {
5911 UriBuilder_QueryInterface,
5912 UriBuilder_AddRef,
5913 UriBuilder_Release,
5914 UriBuilder_CreateUriSimple,
5915 UriBuilder_CreateUri,
5916 UriBuilder_CreateUriWithFlags,
5917 UriBuilder_GetIUri,
5918 UriBuilder_SetIUri,
5919 UriBuilder_GetFragment,
5920 UriBuilder_GetHost,
5921 UriBuilder_GetPassword,
5922 UriBuilder_GetPath,
5923 UriBuilder_GetPort,
5924 UriBuilder_GetQuery,
5925 UriBuilder_GetSchemeName,
5926 UriBuilder_GetUserName,
5927 UriBuilder_SetFragment,
5928 UriBuilder_SetHost,
5929 UriBuilder_SetPassword,
5930 UriBuilder_SetPath,
5931 UriBuilder_SetPort,
5932 UriBuilder_SetQuery,
5933 UriBuilder_SetSchemeName,
5934 UriBuilder_SetUserName,
5935 UriBuilder_RemoveProperties,
5936 UriBuilder_HasBeenModified,
5939 /***********************************************************************
5940 * CreateIUriBuilder (urlmon.@)
5942 HRESULT WINAPI CreateIUriBuilder(IUri *pIUri, DWORD dwFlags, DWORD_PTR dwReserved, IUriBuilder **ppIUriBuilder)
5944 UriBuilder *ret;
5946 TRACE("(%p %x %x %p)\n", pIUri, dwFlags, (DWORD)dwReserved, ppIUriBuilder);
5948 if(!ppIUriBuilder)
5949 return E_POINTER;
5951 ret = heap_alloc_zero(sizeof(UriBuilder));
5952 if(!ret)
5953 return E_OUTOFMEMORY;
5955 ret->IUriBuilder_iface.lpVtbl = &UriBuilderVtbl;
5956 ret->ref = 1;
5958 if(pIUri) {
5959 Uri *uri;
5961 if((uri = get_uri_obj(pIUri))) {
5962 if(!uri->create_flags) {
5963 heap_free(ret);
5964 return E_UNEXPECTED;
5966 IUri_AddRef(pIUri);
5967 ret->uri = uri;
5969 if(uri->has_port)
5970 /* Windows doesn't set 'has_port' to TRUE in this case. */
5971 ret->port = uri->port;
5973 } else {
5974 heap_free(ret);
5975 *ppIUriBuilder = NULL;
5976 FIXME("(%p %x %x %p): Unknown IUri types not supported yet.\n", pIUri, dwFlags,
5977 (DWORD)dwReserved, ppIUriBuilder);
5978 return E_NOTIMPL;
5982 *ppIUriBuilder = &ret->IUriBuilder_iface;
5983 return S_OK;
5986 /* Merges the base path with the relative path and stores the resulting path
5987 * and path len in 'result' and 'result_len'.
5989 static HRESULT merge_paths(parse_data *data, const WCHAR *base, DWORD base_len, const WCHAR *relative,
5990 DWORD relative_len, WCHAR **result, DWORD *result_len, DWORD flags)
5992 const WCHAR *end = NULL;
5993 DWORD base_copy_len = 0;
5994 WCHAR *ptr;
5996 if(base_len) {
5997 if(data->scheme_type == URL_SCHEME_MK && *relative == '/') {
5998 /* Find '::' segment */
5999 for(end = base; end < base+base_len-1; end++) {
6000 if(end[0] == ':' && end[1] == ':') {
6001 end++;
6002 break;
6006 /* If not found, try finding the end of @xxx: */
6007 if(end == base+base_len-1)
6008 end = *base == '@' ? wmemchr(base, ':', base_len) : NULL;
6009 }else {
6010 /* Find the characters that will be copied over from the base path. */
6011 for (end = base + base_len - 1; end >= base; end--) if (*end == '/') break;
6012 if(end < base && data->scheme_type == URL_SCHEME_FILE)
6013 /* Try looking for a '\\'. */
6014 for (end = base + base_len - 1; end >= base; end--) if (*end == '\\') break;
6018 if (end) base_copy_len = (end+1)-base;
6019 *result = heap_alloc((base_copy_len+relative_len+1)*sizeof(WCHAR));
6021 if(!(*result)) {
6022 *result_len = 0;
6023 return E_OUTOFMEMORY;
6026 ptr = *result;
6027 memcpy(ptr, base, base_copy_len*sizeof(WCHAR));
6028 ptr += base_copy_len;
6030 memcpy(ptr, relative, relative_len*sizeof(WCHAR));
6031 ptr += relative_len;
6032 *ptr = '\0';
6034 *result_len = (ptr-*result);
6035 TRACE("ret %s\n", debugstr_wn(*result, *result_len));
6036 return S_OK;
6039 static HRESULT combine_uri(Uri *base, Uri *relative, DWORD flags, IUri **result, DWORD extras) {
6040 Uri *ret;
6041 HRESULT hr;
6042 parse_data data;
6043 Uri *proc_uri = base;
6044 DWORD create_flags = 0, len = 0;
6046 memset(&data, 0, sizeof(parse_data));
6048 /* Base case is when the relative Uri has a scheme name,
6049 * if it does, then 'result' will contain the same data
6050 * as the relative Uri.
6052 if(relative->scheme_start > -1) {
6053 data.uri = SysAllocString(relative->raw_uri);
6054 if(!data.uri) {
6055 *result = NULL;
6056 return E_OUTOFMEMORY;
6059 parse_uri(&data, Uri_CREATE_ALLOW_IMPLICIT_FILE_SCHEME);
6061 hr = Uri_Construct(NULL, (void**)&ret);
6062 if(FAILED(hr)) {
6063 *result = NULL;
6064 return hr;
6067 if(extras & COMBINE_URI_FORCE_FLAG_USE) {
6068 if(flags & URL_DONT_SIMPLIFY)
6069 create_flags |= Uri_CREATE_NO_CANONICALIZE;
6070 if(flags & URL_DONT_UNESCAPE_EXTRA_INFO)
6071 create_flags |= Uri_CREATE_NO_DECODE_EXTRA_INFO;
6074 ret->raw_uri = data.uri;
6075 hr = canonicalize_uri(&data, ret, create_flags);
6076 if(FAILED(hr)) {
6077 IUri_Release(&ret->IUri_iface);
6078 *result = NULL;
6079 return hr;
6082 apply_default_flags(&create_flags);
6083 ret->create_flags = create_flags;
6085 *result = &ret->IUri_iface;
6086 } else {
6087 WCHAR *path = NULL;
6088 DWORD raw_flags = 0;
6090 if(base->scheme_start > -1) {
6091 data.scheme = base->canon_uri+base->scheme_start;
6092 data.scheme_len = base->scheme_len;
6093 data.scheme_type = base->scheme_type;
6094 } else {
6095 data.is_relative = TRUE;
6096 data.scheme_type = URL_SCHEME_UNKNOWN;
6097 create_flags |= Uri_CREATE_ALLOW_RELATIVE;
6100 if(relative->authority_start > -1)
6101 proc_uri = relative;
6103 if(proc_uri->authority_start > -1) {
6104 if(proc_uri->userinfo_start > -1 && proc_uri->userinfo_split != 0) {
6105 data.username = proc_uri->canon_uri+proc_uri->userinfo_start;
6106 data.username_len = (proc_uri->userinfo_split > -1) ? proc_uri->userinfo_split : proc_uri->userinfo_len;
6109 if(proc_uri->userinfo_split > -1) {
6110 data.password = proc_uri->canon_uri+proc_uri->userinfo_start+proc_uri->userinfo_split+1;
6111 data.password_len = proc_uri->userinfo_len-proc_uri->userinfo_split-1;
6114 if(proc_uri->host_start > -1) {
6115 const WCHAR *host = proc_uri->canon_uri+proc_uri->host_start;
6116 parse_host(&host, &data, 0);
6119 if(proc_uri->has_port) {
6120 data.has_port = TRUE;
6121 data.port_value = proc_uri->port;
6123 } else if(base->scheme_type != URL_SCHEME_FILE)
6124 data.is_opaque = TRUE;
6126 if(proc_uri == relative || relative->path_start == -1 || !relative->path_len) {
6127 if(proc_uri->path_start > -1) {
6128 data.path = proc_uri->canon_uri+proc_uri->path_start;
6129 data.path_len = proc_uri->path_len;
6130 } else if(!data.is_opaque) {
6131 /* Just set the path as a '/' if the base didn't have
6132 * one and if it's a hierarchical URI.
6134 static const WCHAR slashW[] = {'/',0};
6135 data.path = slashW;
6136 data.path_len = 1;
6139 if(relative->query_start > -1)
6140 proc_uri = relative;
6142 if(proc_uri->query_start > -1) {
6143 data.query = proc_uri->canon_uri+proc_uri->query_start;
6144 data.query_len = proc_uri->query_len;
6146 } else {
6147 const WCHAR *ptr, **pptr;
6148 DWORD path_offset = 0, path_len = 0;
6150 /* There's two possibilities on what will happen to the path component
6151 * of the result IUri. First, if the relative path begins with a '/'
6152 * then the resulting path will just be the relative path. Second, if
6153 * relative path doesn't begin with a '/' then the base path and relative
6154 * path are merged together.
6156 if(relative->path_len && *(relative->canon_uri+relative->path_start) == '/' && data.scheme_type != URL_SCHEME_MK) {
6157 WCHAR *tmp = NULL;
6158 BOOL copy_drive_path = FALSE;
6160 /* If the relative IUri's path starts with a '/', then we
6161 * don't use the base IUri's path. Unless the base IUri
6162 * is a file URI, in which case it uses the drive path of
6163 * the base IUri (if it has any) in the new path.
6165 if(base->scheme_type == URL_SCHEME_FILE) {
6166 if(base->path_len > 3 && *(base->canon_uri+base->path_start) == '/' &&
6167 is_drive_path(base->canon_uri+base->path_start+1)) {
6168 path_len += 3;
6169 copy_drive_path = TRUE;
6173 path_len += relative->path_len;
6175 path = heap_alloc((path_len+1)*sizeof(WCHAR));
6176 if(!path) {
6177 *result = NULL;
6178 return E_OUTOFMEMORY;
6181 tmp = path;
6183 /* Copy the base paths, drive path over. */
6184 if(copy_drive_path) {
6185 memcpy(tmp, base->canon_uri+base->path_start, 3*sizeof(WCHAR));
6186 tmp += 3;
6189 memcpy(tmp, relative->canon_uri+relative->path_start, relative->path_len*sizeof(WCHAR));
6190 path[path_len] = '\0';
6191 } else {
6192 /* Merge the base path with the relative path. */
6193 hr = merge_paths(&data, base->canon_uri+base->path_start, base->path_len,
6194 relative->canon_uri+relative->path_start, relative->path_len,
6195 &path, &path_len, flags);
6196 if(FAILED(hr)) {
6197 *result = NULL;
6198 return hr;
6201 /* If the resulting IUri is a file URI, the drive path isn't
6202 * reduced out when the dot segments are removed.
6204 if(path_len >= 3 && data.scheme_type == URL_SCHEME_FILE && !data.host) {
6205 if(*path == '/' && is_drive_path(path+1))
6206 path_offset = 2;
6207 else if(is_drive_path(path))
6208 path_offset = 1;
6212 /* Check if the dot segments need to be removed from the path. */
6213 if(!(flags & URL_DONT_SIMPLIFY) && !data.is_opaque) {
6214 DWORD offset = (path_offset > 0) ? path_offset+1 : 0;
6215 DWORD new_len = remove_dot_segments(path+offset,path_len-offset);
6217 if(new_len != path_len) {
6218 WCHAR *tmp = heap_realloc(path, (offset+new_len+1)*sizeof(WCHAR));
6219 if(!tmp) {
6220 heap_free(path);
6221 *result = NULL;
6222 return E_OUTOFMEMORY;
6225 tmp[new_len+offset] = '\0';
6226 path = tmp;
6227 path_len = new_len+offset;
6231 if(relative->query_start > -1) {
6232 data.query = relative->canon_uri+relative->query_start;
6233 data.query_len = relative->query_len;
6236 /* Make sure the path component is valid. */
6237 ptr = path;
6238 pptr = &ptr;
6239 if((data.is_opaque && !parse_path_opaque(pptr, &data, 0)) ||
6240 (!data.is_opaque && !parse_path_hierarchical(pptr, &data, 0))) {
6241 heap_free(path);
6242 *result = NULL;
6243 return E_INVALIDARG;
6247 if(relative->fragment_start > -1) {
6248 data.fragment = relative->canon_uri+relative->fragment_start;
6249 data.fragment_len = relative->fragment_len;
6252 if(flags & URL_DONT_SIMPLIFY)
6253 raw_flags |= RAW_URI_FORCE_PORT_DISP;
6254 if(flags & URL_FILE_USE_PATHURL)
6255 raw_flags |= RAW_URI_CONVERT_TO_DOS_PATH;
6257 len = generate_raw_uri(&data, data.uri, raw_flags);
6258 data.uri = SysAllocStringLen(NULL, len);
6259 if(!data.uri) {
6260 heap_free(path);
6261 *result = NULL;
6262 return E_OUTOFMEMORY;
6265 generate_raw_uri(&data, data.uri, raw_flags);
6267 hr = Uri_Construct(NULL, (void**)&ret);
6268 if(FAILED(hr)) {
6269 SysFreeString(data.uri);
6270 heap_free(path);
6271 *result = NULL;
6272 return hr;
6275 if(flags & URL_DONT_SIMPLIFY)
6276 create_flags |= Uri_CREATE_NO_CANONICALIZE;
6277 if(flags & URL_FILE_USE_PATHURL)
6278 create_flags |= Uri_CREATE_FILE_USE_DOS_PATH;
6280 ret->raw_uri = data.uri;
6281 hr = canonicalize_uri(&data, ret, create_flags);
6282 if(FAILED(hr)) {
6283 IUri_Release(&ret->IUri_iface);
6284 *result = NULL;
6285 return hr;
6288 if(flags & URL_DONT_SIMPLIFY)
6289 ret->display_modifiers |= URI_DISPLAY_NO_DEFAULT_PORT_AUTH;
6291 apply_default_flags(&create_flags);
6292 ret->create_flags = create_flags;
6293 *result = &ret->IUri_iface;
6295 heap_free(path);
6298 return S_OK;
6301 /***********************************************************************
6302 * CoInternetCombineIUri (urlmon.@)
6304 HRESULT WINAPI CoInternetCombineIUri(IUri *pBaseUri, IUri *pRelativeUri, DWORD dwCombineFlags,
6305 IUri **ppCombinedUri, DWORD_PTR dwReserved)
6307 HRESULT hr;
6308 IInternetProtocolInfo *info;
6309 Uri *relative, *base;
6310 TRACE("(%p %p %x %p %x)\n", pBaseUri, pRelativeUri, dwCombineFlags, ppCombinedUri, (DWORD)dwReserved);
6312 if(!ppCombinedUri)
6313 return E_INVALIDARG;
6315 if(!pBaseUri || !pRelativeUri) {
6316 *ppCombinedUri = NULL;
6317 return E_INVALIDARG;
6320 relative = get_uri_obj(pRelativeUri);
6321 base = get_uri_obj(pBaseUri);
6322 if(!relative || !base) {
6323 *ppCombinedUri = NULL;
6324 FIXME("(%p %p %x %p %x) Unknown IUri types not supported yet.\n",
6325 pBaseUri, pRelativeUri, dwCombineFlags, ppCombinedUri, (DWORD)dwReserved);
6326 return E_NOTIMPL;
6329 info = get_protocol_info(base->canon_uri);
6330 if(info) {
6331 WCHAR result[INTERNET_MAX_URL_LENGTH+1];
6332 DWORD result_len = 0;
6334 hr = IInternetProtocolInfo_CombineUrl(info, base->canon_uri, relative->canon_uri, dwCombineFlags,
6335 result, INTERNET_MAX_URL_LENGTH+1, &result_len, 0);
6336 IInternetProtocolInfo_Release(info);
6337 if(SUCCEEDED(hr)) {
6338 hr = CreateUri(result, Uri_CREATE_ALLOW_RELATIVE, 0, ppCombinedUri);
6339 if(SUCCEEDED(hr))
6340 return hr;
6344 return combine_uri(base, relative, dwCombineFlags, ppCombinedUri, 0);
6347 /***********************************************************************
6348 * CoInternetCombineUrlEx (urlmon.@)
6350 HRESULT WINAPI CoInternetCombineUrlEx(IUri *pBaseUri, LPCWSTR pwzRelativeUrl, DWORD dwCombineFlags,
6351 IUri **ppCombinedUri, DWORD_PTR dwReserved)
6353 IUri *relative;
6354 Uri *base;
6355 HRESULT hr;
6356 IInternetProtocolInfo *info;
6358 TRACE("(%p %s %x %p %x)\n", pBaseUri, debugstr_w(pwzRelativeUrl), dwCombineFlags,
6359 ppCombinedUri, (DWORD)dwReserved);
6361 if(!ppCombinedUri)
6362 return E_POINTER;
6364 if(!pwzRelativeUrl) {
6365 *ppCombinedUri = NULL;
6366 return E_UNEXPECTED;
6369 if(!pBaseUri) {
6370 *ppCombinedUri = NULL;
6371 return E_INVALIDARG;
6374 base = get_uri_obj(pBaseUri);
6375 if(!base) {
6376 *ppCombinedUri = NULL;
6377 FIXME("(%p %s %x %p %x) Unknown IUri's not supported yet.\n", pBaseUri, debugstr_w(pwzRelativeUrl),
6378 dwCombineFlags, ppCombinedUri, (DWORD)dwReserved);
6379 return E_NOTIMPL;
6382 info = get_protocol_info(base->canon_uri);
6383 if(info) {
6384 WCHAR result[INTERNET_MAX_URL_LENGTH+1];
6385 DWORD result_len = 0;
6387 hr = IInternetProtocolInfo_CombineUrl(info, base->canon_uri, pwzRelativeUrl, dwCombineFlags,
6388 result, INTERNET_MAX_URL_LENGTH+1, &result_len, 0);
6389 IInternetProtocolInfo_Release(info);
6390 if(SUCCEEDED(hr)) {
6391 hr = CreateUri(result, Uri_CREATE_ALLOW_RELATIVE, 0, ppCombinedUri);
6392 if(SUCCEEDED(hr))
6393 return hr;
6397 hr = CreateUri(pwzRelativeUrl, Uri_CREATE_ALLOW_RELATIVE|Uri_CREATE_ALLOW_IMPLICIT_FILE_SCHEME, 0, &relative);
6398 if(FAILED(hr)) {
6399 *ppCombinedUri = NULL;
6400 return hr;
6403 hr = combine_uri(base, get_uri_obj(relative), dwCombineFlags, ppCombinedUri, COMBINE_URI_FORCE_FLAG_USE);
6405 IUri_Release(relative);
6406 return hr;
6409 static HRESULT parse_canonicalize(const Uri *uri, DWORD flags, LPWSTR output,
6410 DWORD output_len, DWORD *result_len)
6412 const WCHAR *ptr = NULL;
6413 WCHAR *path = NULL;
6414 const WCHAR **pptr;
6415 DWORD len = 0;
6416 BOOL reduce_path;
6418 /* URL_UNESCAPE only has effect if none of the URL_ESCAPE flags are set. */
6419 const BOOL allow_unescape = !(flags & URL_ESCAPE_UNSAFE) &&
6420 !(flags & URL_ESCAPE_SPACES_ONLY) &&
6421 !(flags & URL_ESCAPE_PERCENT);
6424 /* Check if the dot segments need to be removed from the
6425 * path component.
6427 if(uri->scheme_start > -1 && uri->path_start > -1) {
6428 ptr = uri->canon_uri+uri->scheme_start+uri->scheme_len+1;
6429 pptr = &ptr;
6431 reduce_path = !(flags & URL_DONT_SIMPLIFY) &&
6432 ptr && check_hierarchical(pptr);
6434 for(ptr = uri->canon_uri; ptr < uri->canon_uri+uri->canon_len; ++ptr) {
6435 BOOL do_default_action = TRUE;
6437 /* Keep track of the path if we need to remove dot segments from
6438 * it later.
6440 if(reduce_path && !path && ptr == uri->canon_uri+uri->path_start)
6441 path = output+len;
6443 /* Check if it's time to reduce the path. */
6444 if(reduce_path && ptr == uri->canon_uri+uri->path_start+uri->path_len) {
6445 DWORD current_path_len = (output+len) - path;
6446 DWORD new_path_len = remove_dot_segments(path, current_path_len);
6448 /* Update the current length. */
6449 len -= (current_path_len-new_path_len);
6450 reduce_path = FALSE;
6453 if(*ptr == '%') {
6454 const WCHAR decoded = decode_pct_val(ptr);
6455 if(decoded) {
6456 if(allow_unescape && (flags & URL_UNESCAPE)) {
6457 if(len < output_len)
6458 output[len] = decoded;
6459 len++;
6460 ptr += 2;
6461 do_default_action = FALSE;
6465 /* See if %'s needed to encoded. */
6466 if(do_default_action && (flags & URL_ESCAPE_PERCENT)) {
6467 if(len + 3 < output_len)
6468 pct_encode_val(*ptr, output+len);
6469 len += 3;
6470 do_default_action = FALSE;
6472 } else if(*ptr == ' ') {
6473 if((flags & URL_ESCAPE_SPACES_ONLY) &&
6474 !(flags & URL_ESCAPE_UNSAFE)) {
6475 if(len + 3 < output_len)
6476 pct_encode_val(*ptr, output+len);
6477 len += 3;
6478 do_default_action = FALSE;
6480 } else if(is_ascii(*ptr) && !is_reserved(*ptr) && !is_unreserved(*ptr)) {
6481 if(flags & URL_ESCAPE_UNSAFE) {
6482 if(len + 3 < output_len)
6483 pct_encode_val(*ptr, output+len);
6484 len += 3;
6485 do_default_action = FALSE;
6489 if(do_default_action) {
6490 if(len < output_len)
6491 output[len] = *ptr;
6492 len++;
6496 /* Sometimes the path is the very last component of the IUri, so
6497 * see if the dot segments need to be reduced now.
6499 if(reduce_path && path) {
6500 DWORD current_path_len = (output+len) - path;
6501 DWORD new_path_len = remove_dot_segments(path, current_path_len);
6503 /* Update the current length. */
6504 len -= (current_path_len-new_path_len);
6507 if(len < output_len)
6508 output[len] = 0;
6509 else
6510 output[output_len-1] = 0;
6512 /* The null terminator isn't included in the length. */
6513 *result_len = len;
6514 if(len >= output_len)
6515 return STRSAFE_E_INSUFFICIENT_BUFFER;
6517 return S_OK;
6520 static HRESULT parse_friendly(IUri *uri, LPWSTR output, DWORD output_len,
6521 DWORD *result_len)
6523 HRESULT hr;
6524 DWORD display_len;
6525 BSTR display;
6527 hr = IUri_GetPropertyLength(uri, Uri_PROPERTY_DISPLAY_URI, &display_len, 0);
6528 if(FAILED(hr)) {
6529 *result_len = 0;
6530 return hr;
6533 *result_len = display_len;
6534 if(display_len+1 > output_len)
6535 return STRSAFE_E_INSUFFICIENT_BUFFER;
6537 hr = IUri_GetDisplayUri(uri, &display);
6538 if(FAILED(hr)) {
6539 *result_len = 0;
6540 return hr;
6543 memcpy(output, display, (display_len+1)*sizeof(WCHAR));
6544 SysFreeString(display);
6545 return S_OK;
6548 static HRESULT parse_rootdocument(const Uri *uri, LPWSTR output, DWORD output_len,
6549 DWORD *result_len)
6551 static const WCHAR colon_slashesW[] = {':','/','/'};
6553 WCHAR *ptr;
6554 DWORD len = 0;
6556 /* Windows only returns the root document if the URI has an authority
6557 * and it's not an unknown scheme type or a file scheme type.
6559 if(uri->authority_start == -1 ||
6560 uri->scheme_type == URL_SCHEME_UNKNOWN ||
6561 uri->scheme_type == URL_SCHEME_FILE) {
6562 *result_len = 0;
6563 if(!output_len)
6564 return STRSAFE_E_INSUFFICIENT_BUFFER;
6566 output[0] = 0;
6567 return S_OK;
6570 len = uri->scheme_len+uri->authority_len;
6571 /* For the "://" and '/' which will be added. */
6572 len += 4;
6574 if(len+1 > output_len) {
6575 *result_len = len;
6576 return STRSAFE_E_INSUFFICIENT_BUFFER;
6579 ptr = output;
6580 memcpy(ptr, uri->canon_uri+uri->scheme_start, uri->scheme_len*sizeof(WCHAR));
6582 /* Add the "://". */
6583 ptr += uri->scheme_len;
6584 memcpy(ptr, colon_slashesW, sizeof(colon_slashesW));
6586 /* Add the authority. */
6587 ptr += ARRAY_SIZE(colon_slashesW);
6588 memcpy(ptr, uri->canon_uri+uri->authority_start, uri->authority_len*sizeof(WCHAR));
6590 /* Add the '/' after the authority. */
6591 ptr += uri->authority_len;
6592 *ptr = '/';
6593 ptr[1] = 0;
6595 *result_len = len;
6596 return S_OK;
6599 static HRESULT parse_document(const Uri *uri, LPWSTR output, DWORD output_len,
6600 DWORD *result_len)
6602 DWORD len = 0;
6604 /* It has to be a known scheme type, but, it can't be a file
6605 * scheme. It also has to hierarchical.
6607 if(uri->scheme_type == URL_SCHEME_UNKNOWN ||
6608 uri->scheme_type == URL_SCHEME_FILE ||
6609 uri->authority_start == -1) {
6610 *result_len = 0;
6611 if(output_len < 1)
6612 return STRSAFE_E_INSUFFICIENT_BUFFER;
6614 output[0] = 0;
6615 return S_OK;
6618 if(uri->fragment_start > -1)
6619 len = uri->fragment_start;
6620 else
6621 len = uri->canon_len;
6623 *result_len = len;
6624 if(len+1 > output_len)
6625 return STRSAFE_E_INSUFFICIENT_BUFFER;
6627 memcpy(output, uri->canon_uri, len*sizeof(WCHAR));
6628 output[len] = 0;
6629 return S_OK;
6632 static HRESULT parse_path_from_url(const Uri *uri, LPWSTR output, DWORD output_len,
6633 DWORD *result_len)
6635 const WCHAR *path_ptr;
6636 WCHAR buffer[INTERNET_MAX_URL_LENGTH+1];
6637 WCHAR *ptr;
6639 if(uri->scheme_type != URL_SCHEME_FILE) {
6640 *result_len = 0;
6641 if(output_len > 0)
6642 output[0] = 0;
6643 return E_INVALIDARG;
6646 ptr = buffer;
6647 if(uri->host_start > -1) {
6648 static const WCHAR slash_slashW[] = {'\\','\\'};
6650 memcpy(ptr, slash_slashW, sizeof(slash_slashW));
6651 ptr += ARRAY_SIZE(slash_slashW);
6652 memcpy(ptr, uri->canon_uri+uri->host_start, uri->host_len*sizeof(WCHAR));
6653 ptr += uri->host_len;
6656 path_ptr = uri->canon_uri+uri->path_start;
6657 if(uri->path_len > 3 && *path_ptr == '/' && is_drive_path(path_ptr+1))
6658 /* Skip past the '/' in front of the drive path. */
6659 ++path_ptr;
6661 for(; path_ptr < uri->canon_uri+uri->path_start+uri->path_len; ++path_ptr, ++ptr) {
6662 BOOL do_default_action = TRUE;
6664 if(*path_ptr == '%') {
6665 const WCHAR decoded = decode_pct_val(path_ptr);
6666 if(decoded) {
6667 *ptr = decoded;
6668 path_ptr += 2;
6669 do_default_action = FALSE;
6671 } else if(*path_ptr == '/') {
6672 *ptr = '\\';
6673 do_default_action = FALSE;
6676 if(do_default_action)
6677 *ptr = *path_ptr;
6680 *ptr = 0;
6682 *result_len = ptr-buffer;
6683 if(*result_len+1 > output_len)
6684 return STRSAFE_E_INSUFFICIENT_BUFFER;
6686 memcpy(output, buffer, (*result_len+1)*sizeof(WCHAR));
6687 return S_OK;
6690 static HRESULT parse_url_from_path(IUri *uri, LPWSTR output, DWORD output_len,
6691 DWORD *result_len)
6693 HRESULT hr;
6694 BSTR received;
6695 DWORD len = 0;
6697 hr = IUri_GetPropertyLength(uri, Uri_PROPERTY_ABSOLUTE_URI, &len, 0);
6698 if(FAILED(hr)) {
6699 *result_len = 0;
6700 return hr;
6703 *result_len = len;
6704 if(len+1 > output_len)
6705 return STRSAFE_E_INSUFFICIENT_BUFFER;
6707 hr = IUri_GetAbsoluteUri(uri, &received);
6708 if(FAILED(hr)) {
6709 *result_len = 0;
6710 return hr;
6713 memcpy(output, received, (len+1)*sizeof(WCHAR));
6714 SysFreeString(received);
6716 return S_OK;
6719 static HRESULT parse_schema(IUri *uri, LPWSTR output, DWORD output_len,
6720 DWORD *result_len)
6722 HRESULT hr;
6723 DWORD len;
6724 BSTR received;
6726 hr = IUri_GetPropertyLength(uri, Uri_PROPERTY_SCHEME_NAME, &len, 0);
6727 if(FAILED(hr)) {
6728 *result_len = 0;
6729 return hr;
6732 *result_len = len;
6733 if(len+1 > output_len)
6734 return STRSAFE_E_INSUFFICIENT_BUFFER;
6736 hr = IUri_GetSchemeName(uri, &received);
6737 if(FAILED(hr)) {
6738 *result_len = 0;
6739 return hr;
6742 memcpy(output, received, (len+1)*sizeof(WCHAR));
6743 SysFreeString(received);
6745 return S_OK;
6748 static HRESULT parse_site(IUri *uri, LPWSTR output, DWORD output_len, DWORD *result_len)
6750 HRESULT hr;
6751 DWORD len;
6752 BSTR received;
6754 hr = IUri_GetPropertyLength(uri, Uri_PROPERTY_HOST, &len, 0);
6755 if(FAILED(hr)) {
6756 *result_len = 0;
6757 return hr;
6760 *result_len = len;
6761 if(len+1 > output_len)
6762 return STRSAFE_E_INSUFFICIENT_BUFFER;
6764 hr = IUri_GetHost(uri, &received);
6765 if(FAILED(hr)) {
6766 *result_len = 0;
6767 return hr;
6770 memcpy(output, received, (len+1)*sizeof(WCHAR));
6771 SysFreeString(received);
6773 return S_OK;
6776 static HRESULT parse_domain(IUri *uri, LPWSTR output, DWORD output_len, DWORD *result_len)
6778 HRESULT hr;
6779 DWORD len;
6780 BSTR received;
6782 hr = IUri_GetPropertyLength(uri, Uri_PROPERTY_DOMAIN, &len, 0);
6783 if(FAILED(hr)) {
6784 *result_len = 0;
6785 return hr;
6788 *result_len = len;
6789 if(len+1 > output_len)
6790 return STRSAFE_E_INSUFFICIENT_BUFFER;
6792 hr = IUri_GetDomain(uri, &received);
6793 if(FAILED(hr)) {
6794 *result_len = 0;
6795 return hr;
6798 memcpy(output, received, (len+1)*sizeof(WCHAR));
6799 SysFreeString(received);
6801 return S_OK;
6804 static HRESULT parse_anchor(IUri *uri, LPWSTR output, DWORD output_len, DWORD *result_len)
6806 HRESULT hr;
6807 DWORD len;
6808 BSTR received;
6810 hr = IUri_GetPropertyLength(uri, Uri_PROPERTY_FRAGMENT, &len, 0);
6811 if(FAILED(hr)) {
6812 *result_len = 0;
6813 return hr;
6816 *result_len = len;
6817 if(len+1 > output_len)
6818 return STRSAFE_E_INSUFFICIENT_BUFFER;
6820 hr = IUri_GetFragment(uri, &received);
6821 if(FAILED(hr)) {
6822 *result_len = 0;
6823 return hr;
6826 memcpy(output, received, (len+1)*sizeof(WCHAR));
6827 SysFreeString(received);
6829 return S_OK;
6832 /***********************************************************************
6833 * CoInternetParseIUri (urlmon.@)
6835 HRESULT WINAPI CoInternetParseIUri(IUri *pIUri, PARSEACTION ParseAction, DWORD dwFlags,
6836 LPWSTR pwzResult, DWORD cchResult, DWORD *pcchResult,
6837 DWORD_PTR dwReserved)
6839 HRESULT hr;
6840 Uri *uri;
6841 IInternetProtocolInfo *info;
6843 TRACE("(%p %d %x %p %d %p %x)\n", pIUri, ParseAction, dwFlags, pwzResult,
6844 cchResult, pcchResult, (DWORD)dwReserved);
6846 if(!pcchResult)
6847 return E_POINTER;
6849 if(!pwzResult || !pIUri) {
6850 *pcchResult = 0;
6851 return E_INVALIDARG;
6854 if(!(uri = get_uri_obj(pIUri))) {
6855 *pcchResult = 0;
6856 FIXME("(%p %d %x %p %d %p %x) Unknown IUri's not supported for this action.\n",
6857 pIUri, ParseAction, dwFlags, pwzResult, cchResult, pcchResult, (DWORD)dwReserved);
6858 return E_NOTIMPL;
6861 info = get_protocol_info(uri->canon_uri);
6862 if(info) {
6863 hr = IInternetProtocolInfo_ParseUrl(info, uri->canon_uri, ParseAction, dwFlags,
6864 pwzResult, cchResult, pcchResult, 0);
6865 IInternetProtocolInfo_Release(info);
6866 if(SUCCEEDED(hr)) return hr;
6869 switch(ParseAction) {
6870 case PARSE_CANONICALIZE:
6871 hr = parse_canonicalize(uri, dwFlags, pwzResult, cchResult, pcchResult);
6872 break;
6873 case PARSE_FRIENDLY:
6874 hr = parse_friendly(pIUri, pwzResult, cchResult, pcchResult);
6875 break;
6876 case PARSE_ROOTDOCUMENT:
6877 hr = parse_rootdocument(uri, pwzResult, cchResult, pcchResult);
6878 break;
6879 case PARSE_DOCUMENT:
6880 hr = parse_document(uri, pwzResult, cchResult, pcchResult);
6881 break;
6882 case PARSE_PATH_FROM_URL:
6883 hr = parse_path_from_url(uri, pwzResult, cchResult, pcchResult);
6884 break;
6885 case PARSE_URL_FROM_PATH:
6886 hr = parse_url_from_path(pIUri, pwzResult, cchResult, pcchResult);
6887 break;
6888 case PARSE_SCHEMA:
6889 hr = parse_schema(pIUri, pwzResult, cchResult, pcchResult);
6890 break;
6891 case PARSE_SITE:
6892 hr = parse_site(pIUri, pwzResult, cchResult, pcchResult);
6893 break;
6894 case PARSE_DOMAIN:
6895 hr = parse_domain(pIUri, pwzResult, cchResult, pcchResult);
6896 break;
6897 case PARSE_LOCATION:
6898 case PARSE_ANCHOR:
6899 hr = parse_anchor(pIUri, pwzResult, cchResult, pcchResult);
6900 break;
6901 case PARSE_SECURITY_URL:
6902 case PARSE_MIME:
6903 case PARSE_SERVER:
6904 case PARSE_SECURITY_DOMAIN:
6905 *pcchResult = 0;
6906 hr = E_FAIL;
6907 break;
6908 default:
6909 *pcchResult = 0;
6910 hr = E_NOTIMPL;
6911 FIXME("(%p %d %x %p %d %p %x) Partial stub.\n", pIUri, ParseAction, dwFlags,
6912 pwzResult, cchResult, pcchResult, (DWORD)dwReserved);
6915 return hr;