windows.applicationmodel/tests: Use PathRemoveFileSpecW() instead of PathCchRemoveFil...
[wine.git] / dlls / urlmon / uri.c
blob9d31df22862c834653a59cc0689f4523ab66980e
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 %ld): Path after dot segments removed %s len=%ld\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 DWORD ret = 0;
703 UCHAR digits[4];
705 digits[0] = (address >> 24) & 0xff;
706 digits[1] = (address >> 16) & 0xff;
707 digits[2] = (address >> 8) & 0xff;
708 digits[3] = address & 0xff;
710 if(!dest) {
711 WCHAR tmp[16];
712 ret = swprintf(tmp, ARRAY_SIZE(tmp), L"%u.%u.%u.%u", digits[0], digits[1], digits[2], digits[3]);
713 } else
714 ret = swprintf(dest, 16, L"%u.%u.%u.%u", digits[0], digits[1], digits[2], digits[3]);
716 return ret;
719 static DWORD ui2str(WCHAR *dest, UINT value) {
720 DWORD ret = 0;
722 if(!dest) {
723 WCHAR tmp[11];
724 ret = swprintf(tmp, ARRAY_SIZE(tmp), L"%u", value);
725 } else
726 ret = swprintf(dest, 11, L"%u", value);
728 return ret;
731 /* Checks if the characters pointed to by 'ptr' are
732 * a percent encoded data octet.
734 * pct-encoded = "%" HEXDIG HEXDIG
736 static BOOL check_pct_encoded(const WCHAR **ptr) {
737 const WCHAR *start = *ptr;
739 if(**ptr != '%')
740 return FALSE;
742 ++(*ptr);
743 if(!is_hexdigit(**ptr)) {
744 *ptr = start;
745 return FALSE;
748 ++(*ptr);
749 if(!is_hexdigit(**ptr)) {
750 *ptr = start;
751 return FALSE;
754 ++(*ptr);
755 return TRUE;
758 /* dec-octet = DIGIT ; 0-9
759 * / %x31-39 DIGIT ; 10-99
760 * / "1" 2DIGIT ; 100-199
761 * / "2" %x30-34 DIGIT ; 200-249
762 * / "25" %x30-35 ; 250-255
764 static BOOL check_dec_octet(const WCHAR **ptr) {
765 const WCHAR *c1, *c2, *c3;
767 c1 = *ptr;
768 /* A dec-octet must be at least 1 digit long. */
769 if(*c1 < '0' || *c1 > '9')
770 return FALSE;
772 ++(*ptr);
774 c2 = *ptr;
775 /* Since the 1-digit requirement was met, it doesn't
776 * matter if this is a DIGIT value, it's considered a
777 * dec-octet.
779 if(*c2 < '0' || *c2 > '9')
780 return TRUE;
782 ++(*ptr);
784 c3 = *ptr;
785 /* Same explanation as above. */
786 if(*c3 < '0' || *c3 > '9')
787 return TRUE;
789 /* Anything > 255 isn't a valid IP dec-octet. */
790 if(*c1 >= '2' && *c2 >= '5' && *c3 >= '5') {
791 *ptr = c1;
792 return FALSE;
795 ++(*ptr);
796 return TRUE;
799 /* Checks if there is an implicit IPv4 address in the host component of the URI.
800 * The max value of an implicit IPv4 address is UINT_MAX.
802 * Ex:
803 * "234567" would be considered an implicit IPv4 address.
805 static BOOL check_implicit_ipv4(const WCHAR **ptr, UINT *val) {
806 const WCHAR *start = *ptr;
807 ULONGLONG ret = 0;
808 *val = 0;
810 while(is_num(**ptr)) {
811 ret = ret*10 + (**ptr - '0');
813 if(ret > UINT_MAX) {
814 *ptr = start;
815 return FALSE;
817 ++(*ptr);
820 if(*ptr == start)
821 return FALSE;
823 *val = ret;
824 return TRUE;
827 /* Checks if the string contains an IPv4 address.
829 * This function has a strict mode or a non-strict mode of operation
830 * When 'strict' is set to FALSE this function will return TRUE if
831 * the string contains at least 'dec-octet "." dec-octet' since partial
832 * IPv4 addresses will be normalized out into full IPv4 addresses. When
833 * 'strict' is set this function expects there to be a full IPv4 address.
835 * IPv4address = dec-octet "." dec-octet "." dec-octet "." dec-octet
837 static BOOL check_ipv4address(const WCHAR **ptr, BOOL strict) {
838 const WCHAR *start = *ptr;
840 if(!check_dec_octet(ptr)) {
841 *ptr = start;
842 return FALSE;
845 if(**ptr != '.') {
846 *ptr = start;
847 return FALSE;
850 ++(*ptr);
851 if(!check_dec_octet(ptr)) {
852 *ptr = start;
853 return FALSE;
856 if(**ptr != '.') {
857 if(strict) {
858 *ptr = start;
859 return FALSE;
860 } else
861 return TRUE;
864 ++(*ptr);
865 if(!check_dec_octet(ptr)) {
866 *ptr = start;
867 return FALSE;
870 if(**ptr != '.') {
871 if(strict) {
872 *ptr = start;
873 return FALSE;
874 } else
875 return TRUE;
878 ++(*ptr);
879 if(!check_dec_octet(ptr)) {
880 *ptr = start;
881 return FALSE;
884 /* Found a four digit ip address. */
885 return TRUE;
887 /* Tries to parse the scheme name of the URI.
889 * scheme = ALPHA *(ALPHA | NUM | '+' | '-' | '.') as defined by RFC 3896.
890 * NOTE: Windows accepts a number as the first character of a scheme.
892 static BOOL parse_scheme_name(const WCHAR **ptr, parse_data *data, DWORD extras) {
893 const WCHAR *start = *ptr;
895 data->scheme = NULL;
896 data->scheme_len = 0;
898 while(**ptr) {
899 if(**ptr == '*' && *ptr == start) {
900 /* Might have found a wildcard scheme. If it is the next
901 * char has to be a ':' for it to be a valid URI
903 ++(*ptr);
904 break;
905 } else if(!is_num(**ptr) && !is_alpha(**ptr) && **ptr != '+' &&
906 **ptr != '-' && **ptr != '.')
907 break;
909 (*ptr)++;
912 if(*ptr == start)
913 return FALSE;
915 /* Schemes must end with a ':' */
916 if(**ptr != ':' && !((extras & ALLOW_NULL_TERM_SCHEME) && !**ptr)) {
917 *ptr = start;
918 return FALSE;
921 data->scheme = start;
922 data->scheme_len = *ptr - start;
924 ++(*ptr);
925 return TRUE;
928 /* Tries to deduce the corresponding URL_SCHEME for the given URI. Stores
929 * the deduced URL_SCHEME in data->scheme_type.
931 static BOOL parse_scheme_type(parse_data *data) {
932 /* If there's scheme data then see if it's a recognized scheme. */
933 if(data->scheme && data->scheme_len) {
934 DWORD i;
936 for(i = 0; i < ARRAY_SIZE(recognized_schemes); ++i) {
937 if(lstrlenW(recognized_schemes[i].scheme_name) == data->scheme_len) {
938 /* Has to be a case insensitive compare. */
939 if(!StrCmpNIW(recognized_schemes[i].scheme_name, data->scheme, data->scheme_len)) {
940 data->scheme_type = recognized_schemes[i].scheme;
941 return TRUE;
946 /* If we get here it means it's not a recognized scheme. */
947 data->scheme_type = URL_SCHEME_UNKNOWN;
948 return TRUE;
949 } else if(data->is_relative) {
950 /* Relative URI's have no scheme. */
951 data->scheme_type = URL_SCHEME_UNKNOWN;
952 return TRUE;
953 } else {
954 /* Should never reach here! what happened... */
955 FIXME("(%p): Unable to determine scheme type for URI %s\n", data, debugstr_w(data->uri));
956 return FALSE;
960 /* Tries to parse (or deduce) the scheme_name of a URI. If it can't
961 * parse a scheme from the URI it will try to deduce the scheme_name and scheme_type
962 * using the flags specified in 'flags' (if any). Flags that affect how this function
963 * operates are the Uri_CREATE_ALLOW_* flags.
965 * All parsed/deduced information will be stored in 'data' when the function returns.
967 * Returns TRUE if it was able to successfully parse the information.
969 static BOOL parse_scheme(const WCHAR **ptr, parse_data *data, DWORD flags, DWORD extras) {
970 /* First check to see if the uri could implicitly be a file path. */
971 if(is_implicit_file_path(*ptr)) {
972 if(flags & Uri_CREATE_ALLOW_IMPLICIT_FILE_SCHEME) {
973 data->scheme = L"file";
974 data->scheme_len = lstrlenW(L"file");
975 data->has_implicit_scheme = TRUE;
977 TRACE("(%p %p %lx): URI is an implicit file path.\n", ptr, data, flags);
978 } else {
979 /* Windows does not consider anything that can implicitly be a file
980 * path to be a valid URI if the ALLOW_IMPLICIT_FILE_SCHEME flag is not set...
982 TRACE("(%p %p %lx): URI is implicitly a file path, but, the ALLOW_IMPLICIT_FILE_SCHEME flag wasn't set.\n",
983 ptr, data, flags);
984 return FALSE;
986 } else if(!parse_scheme_name(ptr, data, extras)) {
987 /* No scheme was found, this means it could be:
988 * a) an implicit Wildcard scheme
989 * b) a relative URI
990 * c) an invalid URI.
992 if(flags & Uri_CREATE_ALLOW_IMPLICIT_WILDCARD_SCHEME) {
993 data->scheme = L"*";
994 data->scheme_len = lstrlenW(L"*");
995 data->has_implicit_scheme = TRUE;
997 TRACE("(%p %p %lx): URI is an implicit wildcard scheme.\n", ptr, data, flags);
998 } else if (flags & Uri_CREATE_ALLOW_RELATIVE) {
999 data->is_relative = TRUE;
1000 TRACE("(%p %p %lx): URI is relative.\n", ptr, data, flags);
1001 } else {
1002 TRACE("(%p %p %lx): Malformed URI found. Unable to deduce scheme name.\n", ptr, data, flags);
1003 return FALSE;
1007 if(!data->is_relative)
1008 TRACE("(%p %p %lx): Found scheme=%s scheme_len=%ld\n", ptr, data, flags,
1009 debugstr_wn(data->scheme, data->scheme_len), data->scheme_len);
1011 if(!parse_scheme_type(data))
1012 return FALSE;
1014 TRACE("(%p %p %lx): Assigned %d as the URL_SCHEME.\n", ptr, data, flags, data->scheme_type);
1015 return TRUE;
1018 static BOOL parse_username(const WCHAR **ptr, parse_data *data, DWORD flags, DWORD extras) {
1019 data->username = *ptr;
1021 while(**ptr != ':' && **ptr != '@') {
1022 if(**ptr == '%') {
1023 if(!check_pct_encoded(ptr)) {
1024 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
1025 *ptr = data->username;
1026 data->username = NULL;
1027 return FALSE;
1029 } else
1030 continue;
1031 } else if(extras & ALLOW_NULL_TERM_USER_NAME && !**ptr)
1032 break;
1033 else if(is_auth_delim(**ptr, data->scheme_type != URL_SCHEME_UNKNOWN)) {
1034 *ptr = data->username;
1035 data->username = NULL;
1036 return FALSE;
1039 ++(*ptr);
1042 data->username_len = *ptr - data->username;
1043 return TRUE;
1046 static BOOL parse_password(const WCHAR **ptr, parse_data *data, DWORD flags, DWORD extras) {
1047 data->password = *ptr;
1049 while(**ptr != '@') {
1050 if(**ptr == '%') {
1051 if(!check_pct_encoded(ptr)) {
1052 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
1053 *ptr = data->password;
1054 data->password = NULL;
1055 return FALSE;
1057 } else
1058 continue;
1059 } else if(extras & ALLOW_NULL_TERM_PASSWORD && !**ptr)
1060 break;
1061 else if(is_auth_delim(**ptr, data->scheme_type != URL_SCHEME_UNKNOWN)) {
1062 *ptr = data->password;
1063 data->password = NULL;
1064 return FALSE;
1067 ++(*ptr);
1070 data->password_len = *ptr - data->password;
1071 return TRUE;
1074 /* Parses the userinfo part of the URI (if it exists). The userinfo field of
1075 * a URI can consist of "username:password@", or just "username@".
1077 * RFC def:
1078 * userinfo = *( unreserved / pct-encoded / sub-delims / ":" )
1080 * NOTES:
1081 * 1) If there is more than one ':' in the userinfo part of the URI Windows
1082 * uses the first occurrence of ':' to delimit the username and password
1083 * components.
1085 * ex:
1086 * ftp://user:pass:word@winehq.org
1088 * would yield "user" as the username and "pass:word" as the password.
1090 * 2) Windows allows any character to appear in the "userinfo" part of
1091 * a URI, as long as it's not an authority delimiter character set.
1093 static void parse_userinfo(const WCHAR **ptr, parse_data *data, DWORD flags) {
1094 const WCHAR *start = *ptr;
1096 if(!parse_username(ptr, data, flags, 0)) {
1097 TRACE("(%p %p %lx): URI contained no userinfo.\n", ptr, data, flags);
1098 return;
1101 if(**ptr == ':') {
1102 ++(*ptr);
1103 if(!parse_password(ptr, data, flags, 0)) {
1104 *ptr = start;
1105 data->username = NULL;
1106 data->username_len = 0;
1107 TRACE("(%p %p %lx): URI contained no userinfo.\n", ptr, data, flags);
1108 return;
1112 if(**ptr != '@') {
1113 *ptr = start;
1114 data->username = NULL;
1115 data->username_len = 0;
1116 data->password = NULL;
1117 data->password_len = 0;
1119 TRACE("(%p %p %lx): URI contained no userinfo.\n", ptr, data, flags);
1120 return;
1123 if(data->username)
1124 TRACE("(%p %p %lx): Found username %s len=%ld.\n", ptr, data, flags,
1125 debugstr_wn(data->username, data->username_len), data->username_len);
1127 if(data->password)
1128 TRACE("(%p %p %lx): Found password %s len=%ld.\n", ptr, data, flags,
1129 debugstr_wn(data->password, data->password_len), data->password_len);
1131 ++(*ptr);
1134 /* Attempts to parse a port from the URI.
1136 * NOTES:
1137 * Windows seems to have a cap on what the maximum value
1138 * for a port can be. The max value is USHORT_MAX.
1140 * port = *DIGIT
1142 static BOOL parse_port(const WCHAR **ptr, parse_data *data) {
1143 UINT port = 0;
1144 data->port = *ptr;
1146 while(!is_auth_delim(**ptr, data->scheme_type != URL_SCHEME_UNKNOWN)) {
1147 if(!is_num(**ptr)) {
1148 *ptr = data->port;
1149 data->port = NULL;
1150 return FALSE;
1153 port = port*10 + (**ptr-'0');
1155 if(port > USHRT_MAX) {
1156 *ptr = data->port;
1157 data->port = NULL;
1158 return FALSE;
1161 ++(*ptr);
1164 data->has_port = TRUE;
1165 data->port_value = port;
1166 data->port_len = *ptr - data->port;
1168 TRACE("(%p %p): Found port %s len=%ld value=%lu\n", ptr, data,
1169 debugstr_wn(data->port, data->port_len), data->port_len, data->port_value);
1170 return TRUE;
1173 /* Attempts to parse a IPv4 address from the URI.
1175 * NOTES:
1176 * Windows normalizes IPv4 addresses, This means there are three
1177 * possibilities for the URI to contain an IPv4 address.
1178 * 1) A well formed address (ex. 192.2.2.2).
1179 * 2) A partially formed address. For example "192.0" would
1180 * normalize to "192.0.0.0" during canonicalization.
1181 * 3) An implicit IPv4 address. For example "256" would
1182 * normalize to "0.0.1.0" during canonicalization. Also
1183 * note that the maximum value for an implicit IP address
1184 * is UINT_MAX, if the value in the URI exceeds this then
1185 * it is not considered an IPv4 address.
1187 static BOOL parse_ipv4address(const WCHAR **ptr, parse_data *data) {
1188 const BOOL is_unknown = data->scheme_type == URL_SCHEME_UNKNOWN;
1189 data->host = *ptr;
1191 if(!check_ipv4address(ptr, FALSE)) {
1192 if(!check_implicit_ipv4(ptr, &data->implicit_ipv4)) {
1193 TRACE("(%p %p): URI didn't contain anything looking like an IPv4 address.\n", ptr, data);
1194 *ptr = data->host;
1195 data->host = NULL;
1196 return FALSE;
1197 } else
1198 data->has_implicit_ip = TRUE;
1201 data->host_len = *ptr - data->host;
1202 data->host_type = Uri_HOST_IPV4;
1204 /* Check if what we found is the only part of the host name (if it isn't
1205 * we don't have an IPv4 address).
1207 if(**ptr == ':') {
1208 ++(*ptr);
1209 if(!parse_port(ptr, data)) {
1210 *ptr = data->host;
1211 data->host = NULL;
1212 return FALSE;
1214 } else if(!is_auth_delim(**ptr, !is_unknown)) {
1215 /* Found more data which belongs to the host, so this isn't an IPv4. */
1216 *ptr = data->host;
1217 data->host = NULL;
1218 data->has_implicit_ip = FALSE;
1219 return FALSE;
1222 TRACE("(%p %p): IPv4 address found. host=%s host_len=%ld host_type=%d\n",
1223 ptr, data, debugstr_wn(data->host, data->host_len),
1224 data->host_len, data->host_type);
1225 return TRUE;
1228 /* Attempts to parse the reg-name from the URI.
1230 * Because of the way Windows handles ':' this function also
1231 * handles parsing the port.
1233 * reg-name = *( unreserved / pct-encoded / sub-delims )
1235 * NOTE:
1236 * Windows allows everything, but, the characters in "auth_delims" and ':'
1237 * to appear in a reg-name, unless it's an unknown scheme type then ':' is
1238 * allowed to appear (even if a valid port isn't after it).
1240 * Windows doesn't like host names which start with '[' and end with ']'
1241 * and don't contain a valid IP literal address in between them.
1243 * On Windows if a '[' is encountered in the host name the ':' no longer
1244 * counts as a delimiter until you reach the next ']' or an "authority delimiter".
1246 * A reg-name CAN be empty.
1248 static BOOL parse_reg_name(const WCHAR **ptr, parse_data *data, DWORD extras) {
1249 const BOOL has_start_bracket = **ptr == '[';
1250 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
1251 const BOOL is_res = data->scheme_type == URL_SCHEME_RES;
1252 BOOL inside_brackets = has_start_bracket;
1254 /* res URIs don't have ports. */
1255 BOOL ignore_col = (extras & IGNORE_PORT_DELIMITER) || is_res;
1257 /* We have to be careful with file schemes. */
1258 if(data->scheme_type == URL_SCHEME_FILE) {
1259 /* This is because an implicit file scheme could be "C:\\test" and it
1260 * would trick this function into thinking the host is "C", when after
1261 * canonicalization the host would end up being an empty string. A drive
1262 * path can also have a '|' instead of a ':' after the drive letter.
1264 if(is_drive_path(*ptr)) {
1265 /* Regular old drive paths have no host type (or host name). */
1266 data->host_type = Uri_HOST_UNKNOWN;
1267 data->host = *ptr;
1268 data->host_len = 0;
1269 return TRUE;
1270 } else if(is_unc_path(*ptr))
1271 /* Skip past the "\\" of a UNC path. */
1272 *ptr += 2;
1275 data->host = *ptr;
1277 /* For res URIs, everything before the first '/' is
1278 * considered the host.
1280 while((!is_res && !is_auth_delim(**ptr, known_scheme)) ||
1281 (is_res && **ptr && **ptr != '/')) {
1282 if(**ptr == ':' && !ignore_col) {
1283 /* We can ignore ':' if we are inside brackets.*/
1284 if(!inside_brackets) {
1285 const WCHAR *tmp = (*ptr)++;
1287 /* Attempt to parse the port. */
1288 if(!parse_port(ptr, data)) {
1289 /* Windows expects there to be a valid port for known scheme types. */
1290 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
1291 *ptr = data->host;
1292 data->host = NULL;
1293 TRACE("(%p %p %lx): Expected valid port\n", ptr, data, extras);
1294 return FALSE;
1295 } else
1296 /* Windows gives up on trying to parse a port when it
1297 * encounters an invalid port.
1299 ignore_col = TRUE;
1300 } else {
1301 data->host_len = tmp - data->host;
1302 break;
1305 } else if(**ptr == '%' && (known_scheme && !is_res)) {
1306 /* Has to be a legit % encoded value. */
1307 if(!check_pct_encoded(ptr)) {
1308 *ptr = data->host;
1309 data->host = NULL;
1310 return FALSE;
1311 } else
1312 continue;
1313 } else if(is_res && is_forbidden_dos_path_char(**ptr)) {
1314 *ptr = data->host;
1315 data->host = NULL;
1316 return FALSE;
1317 } else if(**ptr == ']')
1318 inside_brackets = FALSE;
1319 else if(**ptr == '[')
1320 inside_brackets = TRUE;
1322 ++(*ptr);
1325 if(has_start_bracket) {
1326 /* Make sure the last character of the host wasn't a ']'. */
1327 if(*(*ptr-1) == ']') {
1328 TRACE("(%p %p %lx): Expected an IP literal inside of the host\n", ptr, data, extras);
1329 *ptr = data->host;
1330 data->host = NULL;
1331 return FALSE;
1335 /* Don't overwrite our length if we found a port earlier. */
1336 if(!data->port)
1337 data->host_len = *ptr - data->host;
1339 /* If the host is empty, then it's an unknown host type. */
1340 if(data->host_len == 0 || is_res)
1341 data->host_type = Uri_HOST_UNKNOWN;
1342 else
1343 data->host_type = Uri_HOST_DNS;
1345 TRACE("(%p %p %lx): Parsed reg-name. host=%s len=%ld\n", ptr, data, extras,
1346 debugstr_wn(data->host, data->host_len), data->host_len);
1347 return TRUE;
1350 /* Attempts to parse an IPv6 address out of the URI.
1352 * IPv6address = 6( h16 ":" ) ls32
1353 * / "::" 5( h16 ":" ) ls32
1354 * / [ h16 ] "::" 4( h16 ":" ) ls32
1355 * / [ *1( h16 ":" ) h16 ] "::" 3( h16 ":" ) ls32
1356 * / [ *2( h16 ":" ) h16 ] "::" 2( h16 ":" ) ls32
1357 * / [ *3( h16 ":" ) h16 ] "::" h16 ":" ls32
1358 * / [ *4( h16 ":" ) h16 ] "::" ls32
1359 * / [ *5( h16 ":" ) h16 ] "::" h16
1360 * / [ *6( h16 ":" ) h16 ] "::"
1362 * ls32 = ( h16 ":" h16 ) / IPv4address
1363 * ; least-significant 32 bits of address.
1365 * h16 = 1*4HEXDIG
1366 * ; 16 bits of address represented in hexadecimal.
1368 static BOOL parse_ipv6address(const WCHAR **ptr, parse_data *data) {
1369 const WCHAR *terminator;
1371 if(RtlIpv6StringToAddressW(*ptr, &terminator, &data->ipv6_address))
1372 return FALSE;
1373 if(*terminator != ']' && !is_auth_delim(*terminator, data->scheme_type != URL_SCHEME_UNKNOWN))
1374 return FALSE;
1376 *ptr = terminator;
1377 data->host_type = Uri_HOST_IPV6;
1378 return TRUE;
1381 /* IPvFuture = "v" 1*HEXDIG "." 1*( unreserved / sub-delims / ":" ) */
1382 static BOOL parse_ipvfuture(const WCHAR **ptr, parse_data *data) {
1383 const WCHAR *start = *ptr;
1385 /* IPvFuture has to start with a 'v' or 'V'. */
1386 if(**ptr != 'v' && **ptr != 'V')
1387 return FALSE;
1389 /* Following the v there must be at least 1 hex digit. */
1390 ++(*ptr);
1391 if(!is_hexdigit(**ptr)) {
1392 *ptr = start;
1393 return FALSE;
1396 ++(*ptr);
1397 while(is_hexdigit(**ptr))
1398 ++(*ptr);
1400 /* End of the hexdigit sequence must be a '.' */
1401 if(**ptr != '.') {
1402 *ptr = start;
1403 return FALSE;
1406 ++(*ptr);
1407 if(!is_unreserved(**ptr) && !is_subdelim(**ptr) && **ptr != ':') {
1408 *ptr = start;
1409 return FALSE;
1412 ++(*ptr);
1413 while(is_unreserved(**ptr) || is_subdelim(**ptr) || **ptr == ':')
1414 ++(*ptr);
1416 data->host_type = Uri_HOST_UNKNOWN;
1418 TRACE("(%p %p): Parsed IPvFuture address %s len=%d\n", ptr, data,
1419 debugstr_wn(start, *ptr-start), (int)(*ptr-start));
1421 return TRUE;
1424 /* IP-literal = "[" ( IPv6address / IPvFuture ) "]" */
1425 static BOOL parse_ip_literal(const WCHAR **ptr, parse_data *data, DWORD extras) {
1426 data->host = *ptr;
1428 if(**ptr != '[' && !(extras & ALLOW_BRACKETLESS_IP_LITERAL)) {
1429 data->host = NULL;
1430 return FALSE;
1431 } else if(**ptr == '[')
1432 ++(*ptr);
1434 if(!parse_ipv6address(ptr, data)) {
1435 if(extras & SKIP_IP_FUTURE_CHECK || !parse_ipvfuture(ptr, data)) {
1436 *ptr = data->host;
1437 data->host = NULL;
1438 return FALSE;
1442 if(**ptr != ']' && !(extras & ALLOW_BRACKETLESS_IP_LITERAL)) {
1443 *ptr = data->host;
1444 data->host = NULL;
1445 return FALSE;
1446 } else if(!**ptr && extras & ALLOW_BRACKETLESS_IP_LITERAL) {
1447 /* The IP literal didn't contain brackets and was followed by
1448 * a NULL terminator, so no reason to even check the port.
1450 data->host_len = *ptr - data->host;
1451 return TRUE;
1454 ++(*ptr);
1455 if(**ptr == ':') {
1456 ++(*ptr);
1457 /* If a valid port is not found, then let it trickle down to
1458 * parse_reg_name.
1460 if(!parse_port(ptr, data)) {
1461 *ptr = data->host;
1462 data->host = NULL;
1463 return FALSE;
1465 } else
1466 data->host_len = *ptr - data->host;
1468 return TRUE;
1471 /* Parses the host information from the URI.
1473 * host = IP-literal / IPv4address / reg-name
1475 static BOOL parse_host(const WCHAR **ptr, parse_data *data, DWORD extras) {
1476 if(!parse_ip_literal(ptr, data, extras)) {
1477 if(!parse_ipv4address(ptr, data)) {
1478 if(!parse_reg_name(ptr, data, extras)) {
1479 TRACE("(%p %p %lx): Malformed URI, Unknown host type.\n", ptr, data, extras);
1480 return FALSE;
1485 return TRUE;
1488 /* Parses the authority information from the URI.
1490 * authority = [ userinfo "@" ] host [ ":" port ]
1492 static BOOL parse_authority(const WCHAR **ptr, parse_data *data, DWORD flags) {
1493 parse_userinfo(ptr, data, flags);
1495 /* Parsing the port will happen during one of the host parsing
1496 * routines (if the URI has a port).
1498 if(!parse_host(ptr, data, 0))
1499 return FALSE;
1501 return TRUE;
1504 /* Attempts to parse the path information of a hierarchical URI. */
1505 static BOOL parse_path_hierarchical(const WCHAR **ptr, parse_data *data, DWORD flags) {
1506 const WCHAR *start = *ptr;
1507 const BOOL is_file = data->scheme_type == URL_SCHEME_FILE;
1509 if(is_path_delim(data->scheme_type, **ptr)) {
1510 if(data->scheme_type == URL_SCHEME_WILDCARD && !data->must_have_path) {
1511 data->path = NULL;
1512 data->path_len = 0;
1513 } else if(!(flags & Uri_CREATE_NO_CANONICALIZE)) {
1514 /* If the path component is empty, then a '/' is added. */
1515 data->path = L"/";
1516 data->path_len = 1;
1518 } else {
1519 while(!is_path_delim(data->scheme_type, **ptr)) {
1520 if(**ptr == '%' && data->scheme_type != URL_SCHEME_UNKNOWN && !is_file) {
1521 if(!check_pct_encoded(ptr)) {
1522 *ptr = start;
1523 return FALSE;
1524 } else
1525 continue;
1526 } else if(is_forbidden_dos_path_char(**ptr) && is_file &&
1527 (flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
1528 /* File schemes with USE_DOS_PATH set aren't allowed to have
1529 * a '<' or '>' or '\"' appear in them.
1531 *ptr = start;
1532 return FALSE;
1533 } else if(**ptr == '\\') {
1534 /* Not allowed to have a backslash if NO_CANONICALIZE is set
1535 * and the scheme is known type (but not a file scheme).
1537 if(flags & Uri_CREATE_NO_CANONICALIZE) {
1538 if(data->scheme_type != URL_SCHEME_FILE &&
1539 data->scheme_type != URL_SCHEME_UNKNOWN) {
1540 *ptr = start;
1541 return FALSE;
1546 ++(*ptr);
1549 /* The only time a URI doesn't have a path is when
1550 * the NO_CANONICALIZE flag is set and the raw URI
1551 * didn't contain one.
1553 if(*ptr == start) {
1554 data->path = NULL;
1555 data->path_len = 0;
1556 } else {
1557 data->path = start;
1558 data->path_len = *ptr - start;
1562 if(data->path)
1563 TRACE("(%p %p %lx): Parsed path %s len=%ld\n", ptr, data, flags,
1564 debugstr_wn(data->path, data->path_len), data->path_len);
1565 else
1566 TRACE("(%p %p %lx): The URI contained no path\n", ptr, data, flags);
1568 return TRUE;
1571 /* Parses the path of an opaque URI (much less strict than the parser
1572 * for a hierarchical URI).
1574 * NOTE:
1575 * Windows allows invalid % encoded data to appear in opaque URI paths
1576 * for unknown scheme types.
1578 * File schemes with USE_DOS_PATH set aren't allowed to have '<', '>', or '\"'
1579 * appear in them.
1581 static BOOL parse_path_opaque(const WCHAR **ptr, parse_data *data, DWORD flags) {
1582 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
1583 const BOOL is_file = data->scheme_type == URL_SCHEME_FILE;
1584 const BOOL is_mailto = data->scheme_type == URL_SCHEME_MAILTO;
1586 if (is_mailto && (*ptr)[0] == '/' && (*ptr)[1] == '/')
1588 if ((*ptr)[2]) data->path = *ptr + 2;
1589 else data->path = NULL;
1591 else
1592 data->path = *ptr;
1594 while(!is_path_delim(data->scheme_type, **ptr)) {
1595 if(**ptr == '%' && known_scheme) {
1596 if(!check_pct_encoded(ptr)) {
1597 *ptr = data->path;
1598 data->path = NULL;
1599 return FALSE;
1600 } else
1601 continue;
1602 } else if(is_forbidden_dos_path_char(**ptr) && is_file &&
1603 (flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
1604 *ptr = data->path;
1605 data->path = NULL;
1606 return FALSE;
1609 ++(*ptr);
1612 if (data->path) data->path_len = *ptr - data->path;
1613 TRACE("(%p %p %lx): Parsed opaque URI path %s len=%ld\n", ptr, data, flags,
1614 debugstr_wn(data->path, data->path_len), data->path_len);
1615 return TRUE;
1618 /* Determines how the URI should be parsed after the scheme information.
1620 * If the scheme is followed by "//", then it is treated as a hierarchical URI
1621 * which then the authority and path information will be parsed out. Otherwise, the
1622 * URI will be treated as an opaque URI which the authority information is not parsed
1623 * out.
1625 * RFC 3896 definition of hier-part:
1627 * hier-part = "//" authority path-abempty
1628 * / path-absolute
1629 * / path-rootless
1630 * / path-empty
1632 * MSDN opaque URI definition:
1633 * scheme ":" path [ "#" fragment ]
1635 * NOTES:
1636 * If the URI is of an unknown scheme type and has a "//" following the scheme then it
1637 * is treated as a hierarchical URI, but, if the CREATE_NO_CRACK_UNKNOWN_SCHEMES flag is
1638 * set then it is considered an opaque URI regardless of what follows the scheme information
1639 * (per MSDN documentation).
1641 static BOOL parse_hierpart(const WCHAR **ptr, parse_data *data, DWORD flags) {
1642 const WCHAR *start = *ptr;
1644 data->must_have_path = FALSE;
1646 /* For javascript: URIs, simply set everything as a path */
1647 if(data->scheme_type == URL_SCHEME_JAVASCRIPT) {
1648 data->path = *ptr;
1649 data->path_len = lstrlenW(*ptr);
1650 data->is_opaque = TRUE;
1651 *ptr += data->path_len;
1652 return TRUE;
1655 /* Checks if the authority information needs to be parsed. */
1656 if(is_hierarchical_uri(ptr, data)) {
1657 /* Only treat it as a hierarchical URI if the scheme_type is known or
1658 * the Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES flag is not set.
1660 if(data->scheme_type != URL_SCHEME_UNKNOWN ||
1661 !(flags & Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES)) {
1662 TRACE("(%p %p %lx): Treating URI as an hierarchical URI.\n", ptr, data, flags);
1663 data->is_opaque = FALSE;
1665 if(data->scheme_type == URL_SCHEME_WILDCARD && !data->has_implicit_scheme) {
1666 if(**ptr == '/' && *(*ptr+1) == '/') {
1667 data->must_have_path = TRUE;
1668 *ptr += 2;
1672 /* TODO: Handle hierarchical URI's, parse authority then parse the path. */
1673 if(!parse_authority(ptr, data, flags))
1674 return FALSE;
1676 return parse_path_hierarchical(ptr, data, flags);
1677 } else
1678 /* Reset ptr to its starting position so opaque path parsing
1679 * begins at the correct location.
1681 *ptr = start;
1684 /* If it reaches here, then the URI will be treated as an opaque
1685 * URI.
1688 TRACE("(%p %p %lx): Treating URI as an opaque URI.\n", ptr, data, flags);
1690 data->is_opaque = TRUE;
1691 if(!parse_path_opaque(ptr, data, flags))
1692 return FALSE;
1694 return TRUE;
1697 /* Attempts to parse the query string from the URI.
1699 * NOTES:
1700 * If NO_DECODE_EXTRA_INFO flag is set, then invalid percent encoded
1701 * data is allowed to appear in the query string. For unknown scheme types
1702 * invalid percent encoded data is allowed to appear regardless.
1704 static BOOL parse_query(const WCHAR **ptr, parse_data *data, DWORD flags) {
1705 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
1707 if(**ptr != '?') {
1708 TRACE("(%p %p %lx): URI didn't contain a query string.\n", ptr, data, flags);
1709 return TRUE;
1712 data->query = *ptr;
1714 ++(*ptr);
1715 while(**ptr && **ptr != '#') {
1716 if(**ptr == '%' && known_scheme &&
1717 !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
1718 if(!check_pct_encoded(ptr)) {
1719 *ptr = data->query;
1720 data->query = NULL;
1721 return FALSE;
1722 } else
1723 continue;
1726 ++(*ptr);
1729 data->query_len = *ptr - data->query;
1731 TRACE("(%p %p %lx): Parsed query string %s len=%ld\n", ptr, data, flags,
1732 debugstr_wn(data->query, data->query_len), data->query_len);
1733 return TRUE;
1736 /* Attempts to parse the fragment from the URI.
1738 * NOTES:
1739 * If NO_DECODE_EXTRA_INFO flag is set, then invalid percent encoded
1740 * data is allowed to appear in the query string. For unknown scheme types
1741 * invalid percent encoded data is allowed to appear regardless.
1743 static BOOL parse_fragment(const WCHAR **ptr, parse_data *data, DWORD flags) {
1744 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
1746 if(**ptr != '#') {
1747 TRACE("(%p %p %lx): URI didn't contain a fragment.\n", ptr, data, flags);
1748 return TRUE;
1751 data->fragment = *ptr;
1753 ++(*ptr);
1754 while(**ptr) {
1755 if(**ptr == '%' && known_scheme &&
1756 !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
1757 if(!check_pct_encoded(ptr)) {
1758 *ptr = data->fragment;
1759 data->fragment = NULL;
1760 return FALSE;
1761 } else
1762 continue;
1765 ++(*ptr);
1768 data->fragment_len = *ptr - data->fragment;
1770 TRACE("(%p %p %lx): Parsed fragment %s len=%ld\n", ptr, data, flags,
1771 debugstr_wn(data->fragment, data->fragment_len), data->fragment_len);
1772 return TRUE;
1775 /* Parses and validates the components of the specified by data->uri
1776 * and stores the information it parses into 'data'.
1778 * Returns TRUE if it successfully parsed the URI. False otherwise.
1780 static BOOL parse_uri(parse_data *data, DWORD flags) {
1781 const WCHAR *ptr;
1782 const WCHAR **pptr;
1784 ptr = data->uri;
1785 pptr = &ptr;
1787 TRACE("(%p %lx): BEGINNING TO PARSE URI %s.\n", data, flags, debugstr_w(data->uri));
1789 if(!parse_scheme(pptr, data, flags, 0))
1790 return FALSE;
1792 if(!parse_hierpart(pptr, data, flags))
1793 return FALSE;
1795 if(!parse_query(pptr, data, flags))
1796 return FALSE;
1798 if(!parse_fragment(pptr, data, flags))
1799 return FALSE;
1801 TRACE("(%p %lx): FINISHED PARSING URI.\n", data, flags);
1802 return TRUE;
1805 static BOOL canonicalize_username(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
1806 const WCHAR *ptr;
1808 if(!data->username) {
1809 uri->userinfo_start = -1;
1810 return TRUE;
1813 uri->userinfo_start = uri->canon_len;
1814 for(ptr = data->username; ptr < data->username+data->username_len; ++ptr) {
1815 if(*ptr == '%') {
1816 /* Only decode % encoded values for known scheme types. */
1817 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
1818 /* See if the value really needs decoding. */
1819 WCHAR val = decode_pct_val(ptr);
1820 if(is_unreserved(val)) {
1821 if(!computeOnly)
1822 uri->canon_uri[uri->canon_len] = val;
1824 ++uri->canon_len;
1826 /* Move pass the hex characters. */
1827 ptr += 2;
1828 continue;
1831 } else if(is_ascii(*ptr) && !is_reserved(*ptr) && !is_unreserved(*ptr) && *ptr != '\\') {
1832 /* Only percent encode forbidden characters if the NO_ENCODE_FORBIDDEN_CHARACTERS flag
1833 * is NOT set.
1835 if(!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS)) {
1836 if(!computeOnly)
1837 pct_encode_val(*ptr, uri->canon_uri + uri->canon_len);
1839 uri->canon_len += 3;
1840 continue;
1844 if(!computeOnly)
1845 /* Nothing special, so just copy the character over. */
1846 uri->canon_uri[uri->canon_len] = *ptr;
1847 ++uri->canon_len;
1850 return TRUE;
1853 static BOOL canonicalize_password(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
1854 const WCHAR *ptr;
1856 if(!data->password) {
1857 uri->userinfo_split = -1;
1858 return TRUE;
1861 if(uri->userinfo_start == -1)
1862 /* Has a password, but, doesn't have a username. */
1863 uri->userinfo_start = uri->canon_len;
1865 uri->userinfo_split = uri->canon_len - uri->userinfo_start;
1867 /* Add the ':' to the userinfo component. */
1868 if(!computeOnly)
1869 uri->canon_uri[uri->canon_len] = ':';
1870 ++uri->canon_len;
1872 for(ptr = data->password; ptr < data->password+data->password_len; ++ptr) {
1873 if(*ptr == '%') {
1874 /* Only decode % encoded values for known scheme types. */
1875 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
1876 /* See if the value really needs decoding. */
1877 WCHAR val = decode_pct_val(ptr);
1878 if(is_unreserved(val)) {
1879 if(!computeOnly)
1880 uri->canon_uri[uri->canon_len] = val;
1882 ++uri->canon_len;
1884 /* Move pass the hex characters. */
1885 ptr += 2;
1886 continue;
1889 } else if(is_ascii(*ptr) && !is_reserved(*ptr) && !is_unreserved(*ptr) && *ptr != '\\') {
1890 /* Only percent encode forbidden characters if the NO_ENCODE_FORBIDDEN_CHARACTERS flag
1891 * is NOT set.
1893 if(!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS)) {
1894 if(!computeOnly)
1895 pct_encode_val(*ptr, uri->canon_uri + uri->canon_len);
1897 uri->canon_len += 3;
1898 continue;
1902 if(!computeOnly)
1903 /* Nothing special, so just copy the character over. */
1904 uri->canon_uri[uri->canon_len] = *ptr;
1905 ++uri->canon_len;
1908 return TRUE;
1911 /* Canonicalizes the userinfo of the URI represented by the parse_data.
1913 * Canonicalization of the userinfo is a simple process. If there are any percent
1914 * encoded characters that fall in the "unreserved" character set, they are decoded
1915 * to their actual value. If a character is not in the "unreserved" or "reserved" sets
1916 * then it is percent encoded. Other than that the characters are copied over without
1917 * change.
1919 static BOOL canonicalize_userinfo(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
1920 uri->userinfo_start = uri->userinfo_split = -1;
1921 uri->userinfo_len = 0;
1923 if(!data->username && !data->password)
1924 /* URI doesn't have userinfo, so nothing to do here. */
1925 return TRUE;
1927 if(!canonicalize_username(data, uri, flags, computeOnly))
1928 return FALSE;
1930 if(!canonicalize_password(data, uri, flags, computeOnly))
1931 return FALSE;
1933 uri->userinfo_len = uri->canon_len - uri->userinfo_start;
1934 if(!computeOnly)
1935 TRACE("(%p %p %lx %d): Canonicalized userinfo, userinfo_start=%d, userinfo=%s, userinfo_split=%d userinfo_len=%ld.\n",
1936 data, uri, flags, computeOnly, uri->userinfo_start, debugstr_wn(uri->canon_uri + uri->userinfo_start, uri->userinfo_len),
1937 uri->userinfo_split, uri->userinfo_len);
1939 /* Now insert the '@' after the userinfo. */
1940 if(!computeOnly)
1941 uri->canon_uri[uri->canon_len] = '@';
1942 ++uri->canon_len;
1944 return TRUE;
1947 /* Attempts to canonicalize a reg_name.
1949 * Things that happen:
1950 * 1) If Uri_CREATE_NO_CANONICALIZE flag is not set, then the reg_name is
1951 * lower cased. Unless it's an unknown scheme type, which case it's
1952 * no lower cased regardless.
1954 * 2) Unreserved % encoded characters are decoded for known
1955 * scheme types.
1957 * 3) Forbidden characters are % encoded as long as
1958 * Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS flag is not set and
1959 * it isn't an unknown scheme type.
1961 * 4) If it's a file scheme and the host is "localhost" it's removed.
1963 * 5) If it's a file scheme and Uri_CREATE_FILE_USE_DOS_PATH is set,
1964 * then the UNC path characters are added before the host name.
1966 static BOOL canonicalize_reg_name(const parse_data *data, Uri *uri,
1967 DWORD flags, BOOL computeOnly) {
1968 const WCHAR *ptr;
1969 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
1971 if(data->scheme_type == URL_SCHEME_FILE &&
1972 data->host_len == lstrlenW(L"localhost")) {
1973 if(!StrCmpNIW(data->host, L"localhost", data->host_len)) {
1974 uri->host_start = -1;
1975 uri->host_len = 0;
1976 uri->host_type = Uri_HOST_UNKNOWN;
1977 return TRUE;
1981 if(data->scheme_type == URL_SCHEME_FILE && flags & Uri_CREATE_FILE_USE_DOS_PATH) {
1982 if(!computeOnly) {
1983 uri->canon_uri[uri->canon_len] = '\\';
1984 uri->canon_uri[uri->canon_len+1] = '\\';
1986 uri->canon_len += 2;
1987 uri->authority_start = uri->canon_len;
1990 uri->host_start = uri->canon_len;
1992 for(ptr = data->host; ptr < data->host+data->host_len; ++ptr) {
1993 if(*ptr == '%' && known_scheme) {
1994 WCHAR val = decode_pct_val(ptr);
1995 if(is_unreserved(val)) {
1996 /* If NO_CANONICALIZE is not set, then windows lower cases the
1997 * decoded value.
1999 if(!(flags & Uri_CREATE_NO_CANONICALIZE) && iswupper(val)) {
2000 if(!computeOnly)
2001 uri->canon_uri[uri->canon_len] = towlower(val);
2002 } else {
2003 if(!computeOnly)
2004 uri->canon_uri[uri->canon_len] = val;
2006 ++uri->canon_len;
2008 /* Skip past the % encoded character. */
2009 ptr += 2;
2010 continue;
2011 } else {
2012 /* Just copy the % over. */
2013 if(!computeOnly)
2014 uri->canon_uri[uri->canon_len] = *ptr;
2015 ++uri->canon_len;
2017 } else if(*ptr == '\\') {
2018 /* Only unknown scheme types could have made it here with a '\\' in the host name. */
2019 if(!computeOnly)
2020 uri->canon_uri[uri->canon_len] = *ptr;
2021 ++uri->canon_len;
2022 } else if(!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS) && is_ascii(*ptr) &&
2023 !is_unreserved(*ptr) && !is_reserved(*ptr) && known_scheme) {
2024 if(!computeOnly) {
2025 pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
2027 /* The percent encoded value gets lower cased also. */
2028 if(!(flags & Uri_CREATE_NO_CANONICALIZE)) {
2029 uri->canon_uri[uri->canon_len+1] = towlower(uri->canon_uri[uri->canon_len+1]);
2030 uri->canon_uri[uri->canon_len+2] = towlower(uri->canon_uri[uri->canon_len+2]);
2034 uri->canon_len += 3;
2035 } else {
2036 if(!computeOnly) {
2037 if(!(flags & Uri_CREATE_NO_CANONICALIZE) && known_scheme)
2038 uri->canon_uri[uri->canon_len] = towlower(*ptr);
2039 else
2040 uri->canon_uri[uri->canon_len] = *ptr;
2043 ++uri->canon_len;
2047 uri->host_len = uri->canon_len - uri->host_start;
2049 if(!computeOnly)
2050 TRACE("(%p %p %lx %d): Canonicalize reg_name=%s len=%ld\n", data, uri, flags,
2051 computeOnly, debugstr_wn(uri->canon_uri+uri->host_start, uri->host_len),
2052 uri->host_len);
2054 if(!computeOnly)
2055 find_domain_name(uri->canon_uri+uri->host_start, uri->host_len,
2056 &(uri->domain_offset));
2058 return TRUE;
2061 /* Attempts to canonicalize an implicit IPv4 address. */
2062 static BOOL canonicalize_implicit_ipv4address(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2063 uri->host_start = uri->canon_len;
2065 TRACE("%u\n", data->implicit_ipv4);
2066 /* For unknown scheme types Windows doesn't convert
2067 * the value into an IP address, but it still considers
2068 * it an IPv4 address.
2070 if(data->scheme_type == URL_SCHEME_UNKNOWN) {
2071 if(!computeOnly)
2072 memcpy(uri->canon_uri+uri->canon_len, data->host, data->host_len*sizeof(WCHAR));
2073 uri->canon_len += data->host_len;
2074 } else {
2075 if(!computeOnly)
2076 uri->canon_len += ui2ipv4(uri->canon_uri+uri->canon_len, data->implicit_ipv4);
2077 else
2078 uri->canon_len += ui2ipv4(NULL, data->implicit_ipv4);
2081 uri->host_len = uri->canon_len - uri->host_start;
2082 uri->host_type = Uri_HOST_IPV4;
2084 if(!computeOnly)
2085 TRACE("%p %p %lx %d): Canonicalized implicit IP address=%s len=%ld\n",
2086 data, uri, flags, computeOnly,
2087 debugstr_wn(uri->canon_uri+uri->host_start, uri->host_len),
2088 uri->host_len);
2090 return TRUE;
2093 /* Attempts to canonicalize an IPv4 address.
2095 * If the parse_data represents a URI that has an implicit IPv4 address
2096 * (ex. http://256/, this function will convert 256 into 0.0.1.0). If
2097 * the implicit IP address exceeds the value of UINT_MAX (maximum value
2098 * for an IPv4 address) it's canonicalized as if it were a reg-name.
2100 * If the parse_data contains a partial or full IPv4 address it normalizes it.
2101 * A partial IPv4 address is something like "192.0" and would be normalized to
2102 * "192.0.0.0". With a full (or partial) IPv4 address like "192.002.01.003" would
2103 * be normalized to "192.2.1.3".
2105 * NOTES:
2106 * Windows ONLY normalizes IPv4 address for known scheme types (one that isn't
2107 * URL_SCHEME_UNKNOWN). For unknown scheme types, it simply copies the data from
2108 * the original URI into the canonicalized URI, but, it still recognizes URI's
2109 * host type as HOST_IPV4.
2111 static BOOL canonicalize_ipv4address(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2112 if(data->has_implicit_ip)
2113 return canonicalize_implicit_ipv4address(data, uri, flags, computeOnly);
2114 else {
2115 uri->host_start = uri->canon_len;
2117 /* Windows only normalizes for known scheme types. */
2118 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
2119 /* parse_data contains a partial or full IPv4 address, so normalize it. */
2120 DWORD i, octetDigitCount = 0, octetCount = 0;
2121 BOOL octetHasDigit = FALSE;
2123 for(i = 0; i < data->host_len; ++i) {
2124 if(data->host[i] == '0' && !octetHasDigit) {
2125 /* Can ignore leading zeros if:
2126 * 1) It isn't the last digit of the octet.
2127 * 2) i+1 != data->host_len
2128 * 3) i+1 != '.'
2130 if(octetDigitCount == 2 ||
2131 i+1 == data->host_len ||
2132 data->host[i+1] == '.') {
2133 if(!computeOnly)
2134 uri->canon_uri[uri->canon_len] = data->host[i];
2135 ++uri->canon_len;
2136 TRACE("Adding zero\n");
2138 } else if(data->host[i] == '.') {
2139 if(!computeOnly)
2140 uri->canon_uri[uri->canon_len] = data->host[i];
2141 ++uri->canon_len;
2143 octetDigitCount = 0;
2144 octetHasDigit = FALSE;
2145 ++octetCount;
2146 } else {
2147 if(!computeOnly)
2148 uri->canon_uri[uri->canon_len] = data->host[i];
2149 ++uri->canon_len;
2151 ++octetDigitCount;
2152 octetHasDigit = TRUE;
2156 /* Make sure the canonicalized IP address has 4 dec-octets.
2157 * If doesn't add "0" ones until there is 4;
2159 for( ; octetCount < 3; ++octetCount) {
2160 if(!computeOnly) {
2161 uri->canon_uri[uri->canon_len] = '.';
2162 uri->canon_uri[uri->canon_len+1] = '0';
2165 uri->canon_len += 2;
2167 } else {
2168 /* Windows doesn't normalize addresses in unknown schemes. */
2169 if(!computeOnly)
2170 memcpy(uri->canon_uri+uri->canon_len, data->host, data->host_len*sizeof(WCHAR));
2171 uri->canon_len += data->host_len;
2174 uri->host_len = uri->canon_len - uri->host_start;
2175 if(!computeOnly)
2176 TRACE("(%p %p %lx %d): Canonicalized IPv4 address, ip=%s len=%ld\n",
2177 data, uri, flags, computeOnly,
2178 debugstr_wn(uri->canon_uri+uri->host_start, uri->host_len),
2179 uri->host_len);
2182 return TRUE;
2185 /* Attempts to canonicalize the IPv6 address of the URI.
2187 * Multiple things happen during the canonicalization of an IPv6 address:
2188 * 1) Any leading zero's in a h16 component are removed.
2189 * Ex: [0001:0022::] -> [1:22::]
2191 * 2) The longest sequence of zero h16 components are compressed
2192 * into a "::" (elision). If there's a tie, the first is chosen.
2194 * Ex: [0:0:0:0:1:6:7:8] -> [::1:6:7:8]
2195 * [0:0:0:0:1:2::] -> [::1:2:0:0]
2196 * [0:0:1:2:0:0:7:8] -> [::1:2:0:0:7:8]
2198 * 3) If an IPv4 address is attached to the IPv6 address, it's
2199 * also normalized.
2200 * Ex: [::001.002.022.000] -> [::1.2.22.0]
2202 * 4) If an elision is present, but, only represents one h16 component
2203 * it's expanded.
2205 * Ex: [1::2:3:4:5:6:7] -> [1:0:2:3:4:5:6:7]
2207 * 5) If the IPv6 address contains an IPv4 address and there exists
2208 * at least 1 non-zero h16 component the IPv4 address is converted
2209 * into two h16 components, otherwise it's normalized and kept as is.
2211 * Ex: [::192.200.003.4] -> [::192.200.3.4]
2212 * [ffff::192.200.003.4] -> [ffff::c0c8:3041]
2214 * NOTE:
2215 * For unknown scheme types Windows simply copies the address over without any
2216 * changes.
2218 * IPv4 address can be included in an elision if all its components are 0's.
2220 static BOOL canonicalize_ipv6address(const parse_data *data, Uri *uri,
2221 DWORD flags, BOOL computeOnly) {
2222 uri->host_start = uri->canon_len;
2224 if(data->scheme_type == URL_SCHEME_UNKNOWN) {
2225 if(!computeOnly)
2226 memcpy(uri->canon_uri+uri->canon_len, data->host, data->host_len*sizeof(WCHAR));
2227 uri->canon_len += data->host_len;
2228 } else {
2229 WCHAR buffer[46];
2230 ULONG size = ARRAY_SIZE(buffer);
2232 if(computeOnly) {
2233 RtlIpv6AddressToStringExW(&data->ipv6_address, 0, 0, buffer, &size);
2234 uri->canon_len += size + 1;
2235 } else {
2236 uri->canon_uri[uri->canon_len++] = '[';
2237 RtlIpv6AddressToStringExW(&data->ipv6_address, 0, 0, uri->canon_uri + uri->canon_len, &size);
2238 uri->canon_len += size - 1;
2239 uri->canon_uri[uri->canon_len++] = ']';
2243 uri->host_len = uri->canon_len - uri->host_start;
2245 if(!computeOnly)
2246 TRACE("(%p %p %lx %d): Canonicalized IPv6 address %s, len=%ld\n", data, uri, flags,
2247 computeOnly, debugstr_wn(uri->canon_uri+uri->host_start, uri->host_len),
2248 uri->host_len);
2250 return TRUE;
2253 /* Attempts to canonicalize the host of the URI (if any). */
2254 static BOOL canonicalize_host(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2255 uri->host_start = -1;
2256 uri->host_len = 0;
2257 uri->domain_offset = -1;
2259 if(data->host) {
2260 switch(data->host_type) {
2261 case Uri_HOST_DNS:
2262 uri->host_type = Uri_HOST_DNS;
2263 if(!canonicalize_reg_name(data, uri, flags, computeOnly))
2264 return FALSE;
2266 break;
2267 case Uri_HOST_IPV4:
2268 uri->host_type = Uri_HOST_IPV4;
2269 if(!canonicalize_ipv4address(data, uri, flags, computeOnly))
2270 return FALSE;
2272 break;
2273 case Uri_HOST_IPV6:
2274 if(!canonicalize_ipv6address(data, uri, flags, computeOnly))
2275 return FALSE;
2277 uri->host_type = Uri_HOST_IPV6;
2278 break;
2279 case Uri_HOST_UNKNOWN:
2280 if(data->host_len > 0 || data->scheme_type != URL_SCHEME_FILE) {
2281 uri->host_start = uri->canon_len;
2283 /* Nothing happens to unknown host types. */
2284 if(!computeOnly)
2285 memcpy(uri->canon_uri+uri->canon_len, data->host, data->host_len*sizeof(WCHAR));
2286 uri->canon_len += data->host_len;
2287 uri->host_len = data->host_len;
2290 uri->host_type = Uri_HOST_UNKNOWN;
2291 break;
2292 default:
2293 FIXME("(%p %p %lx %d): Canonicalization for host type %d not supported.\n", data,
2294 uri, flags, computeOnly, data->host_type);
2295 return FALSE;
2299 return TRUE;
2302 static BOOL canonicalize_port(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2303 BOOL has_default_port = FALSE;
2304 USHORT default_port = 0;
2305 DWORD i;
2307 uri->port_offset = -1;
2309 /* Check if the scheme has a default port. */
2310 for(i = 0; i < ARRAY_SIZE(default_ports); ++i) {
2311 if(default_ports[i].scheme == data->scheme_type) {
2312 has_default_port = TRUE;
2313 default_port = default_ports[i].port;
2314 break;
2318 uri->has_port = data->has_port || has_default_port;
2320 /* Possible cases:
2321 * 1) Has a port which is the default port.
2322 * 2) Has a port (not the default).
2323 * 3) Doesn't have a port, but, scheme has a default port.
2324 * 4) No port.
2326 if(has_default_port && data->has_port && data->port_value == default_port) {
2327 /* If it's the default port and this flag isn't set, don't do anything. */
2328 if(flags & Uri_CREATE_NO_CANONICALIZE) {
2329 uri->port_offset = uri->canon_len-uri->authority_start;
2330 if(!computeOnly)
2331 uri->canon_uri[uri->canon_len] = ':';
2332 ++uri->canon_len;
2334 if(data->port) {
2335 /* Copy the original port over. */
2336 if(!computeOnly)
2337 memcpy(uri->canon_uri+uri->canon_len, data->port, data->port_len*sizeof(WCHAR));
2338 uri->canon_len += data->port_len;
2339 } else {
2340 if(!computeOnly)
2341 uri->canon_len += ui2str(uri->canon_uri+uri->canon_len, data->port_value);
2342 else
2343 uri->canon_len += ui2str(NULL, data->port_value);
2347 uri->port = default_port;
2348 } else if(data->has_port) {
2349 uri->port_offset = uri->canon_len-uri->authority_start;
2350 if(!computeOnly)
2351 uri->canon_uri[uri->canon_len] = ':';
2352 ++uri->canon_len;
2354 if(flags & Uri_CREATE_NO_CANONICALIZE && data->port) {
2355 /* Copy the original over without changes. */
2356 if(!computeOnly)
2357 memcpy(uri->canon_uri+uri->canon_len, data->port, data->port_len*sizeof(WCHAR));
2358 uri->canon_len += data->port_len;
2359 } else {
2360 if(!computeOnly)
2361 uri->canon_len += ui2str(uri->canon_uri+uri->canon_len, data->port_value);
2362 else
2363 uri->canon_len += ui2str(NULL, data->port_value);
2366 uri->port = data->port_value;
2367 } else if(has_default_port)
2368 uri->port = default_port;
2370 return TRUE;
2373 /* Canonicalizes the authority of the URI represented by the parse_data. */
2374 static BOOL canonicalize_authority(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2375 uri->authority_start = uri->canon_len;
2376 uri->authority_len = 0;
2378 if(!canonicalize_userinfo(data, uri, flags, computeOnly))
2379 return FALSE;
2381 if(!canonicalize_host(data, uri, flags, computeOnly))
2382 return FALSE;
2384 if(!canonicalize_port(data, uri, flags, computeOnly))
2385 return FALSE;
2387 if(uri->host_start != -1 || (data->is_relative && (data->password || data->username)))
2388 uri->authority_len = uri->canon_len - uri->authority_start;
2389 else
2390 uri->authority_start = -1;
2392 return TRUE;
2395 /* Attempts to canonicalize the path of a hierarchical URI.
2397 * Things that happen:
2398 * 1). Forbidden characters are percent encoded, unless the NO_ENCODE_FORBIDDEN
2399 * flag is set or it's a file URI. Forbidden characters are always encoded
2400 * for file schemes regardless and forbidden characters are never encoded
2401 * for unknown scheme types.
2403 * 2). For known scheme types '\\' are changed to '/'.
2405 * 3). Percent encoded, unreserved characters are decoded to their actual values.
2406 * Unless the scheme type is unknown. For file schemes any percent encoded
2407 * character in the unreserved or reserved set is decoded.
2409 * 4). For File schemes if the path is starts with a drive letter and doesn't
2410 * start with a '/' then one is appended.
2411 * Ex: file://c:/test.mp3 -> file:///c:/test.mp3
2413 * 5). Dot segments are removed from the path for all scheme types
2414 * unless NO_CANONICALIZE flag is set. Dot segments aren't removed
2415 * for wildcard scheme types.
2417 * NOTES:
2418 * file://c:/test%20test -> file:///c:/test%2520test
2419 * file://c:/test%3Etest -> file:///c:/test%253Etest
2420 * if Uri_CREATE_FILE_USE_DOS_PATH is not set:
2421 * file:///c:/test%20test -> file:///c:/test%20test
2422 * file:///c:/test%test -> file:///c:/test%25test
2424 static DWORD canonicalize_path_hierarchical(const WCHAR *path, DWORD path_len, URL_SCHEME scheme_type, BOOL has_host, DWORD flags,
2425 BOOL is_implicit_scheme, WCHAR *ret_path) {
2426 const BOOL known_scheme = scheme_type != URL_SCHEME_UNKNOWN;
2427 const BOOL is_file = scheme_type == URL_SCHEME_FILE;
2428 const BOOL is_res = scheme_type == URL_SCHEME_RES;
2429 const WCHAR *ptr;
2430 BOOL escape_pct = FALSE;
2431 DWORD len = 0;
2433 if(!path)
2434 return 0;
2436 ptr = path;
2438 if(is_file && !has_host) {
2439 /* Check if a '/' needs to be appended for the file scheme. */
2440 if(path_len > 1 && is_drive_path(ptr) && !(flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
2441 if(ret_path)
2442 ret_path[len] = '/';
2443 len++;
2444 escape_pct = TRUE;
2445 } else if(*ptr == '/') {
2446 if(!(flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
2447 /* Copy the extra '/' over. */
2448 if(ret_path)
2449 ret_path[len] = '/';
2450 len++;
2452 ++ptr;
2455 if(is_drive_path(ptr)) {
2456 if(ret_path) {
2457 ret_path[len] = *ptr;
2458 /* If there's a '|' after the drive letter, convert it to a ':'. */
2459 ret_path[len+1] = ':';
2461 ptr += 2;
2462 len += 2;
2466 if(!is_file && *path && *path != '/') {
2467 /* Prepend a '/' to the path if it doesn't have one. */
2468 if(ret_path)
2469 ret_path[len] = '/';
2470 len++;
2473 for(; ptr < path+path_len; ++ptr) {
2474 BOOL do_default_action = TRUE;
2476 if(*ptr == '%' && !is_res) {
2477 const WCHAR *tmp = ptr;
2478 WCHAR val;
2480 /* Check if the % represents a valid encoded char, or if it needs encoding. */
2481 BOOL force_encode = !check_pct_encoded(&tmp) && is_file && !(flags&Uri_CREATE_FILE_USE_DOS_PATH);
2482 val = decode_pct_val(ptr);
2484 if(force_encode || escape_pct) {
2485 /* Escape the percent sign in the file URI. */
2486 if(ret_path)
2487 pct_encode_val(*ptr, ret_path+len);
2488 len += 3;
2489 do_default_action = FALSE;
2490 } else if((is_unreserved(val) && known_scheme) ||
2491 (is_file && !is_implicit_scheme && (is_unreserved(val) || is_reserved(val) ||
2492 (val && flags&Uri_CREATE_FILE_USE_DOS_PATH && !is_forbidden_dos_path_char(val))))) {
2493 if(ret_path)
2494 ret_path[len] = val;
2495 len++;
2497 ptr += 2;
2498 continue;
2500 } else if(*ptr == '/' && is_file && (flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
2501 /* Convert the '/' back to a '\\'. */
2502 if(ret_path)
2503 ret_path[len] = '\\';
2504 len++;
2505 do_default_action = FALSE;
2506 } else if(*ptr == '\\' && known_scheme) {
2507 if(!(is_file && (flags & Uri_CREATE_FILE_USE_DOS_PATH))) {
2508 /* Convert '\\' into a '/'. */
2509 if(ret_path)
2510 ret_path[len] = '/';
2511 len++;
2512 do_default_action = FALSE;
2514 } else if(known_scheme && !is_res && is_ascii(*ptr) && !is_unreserved(*ptr) && !is_reserved(*ptr) &&
2515 (!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS) || is_file)) {
2516 if(!is_file || !(flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
2517 /* Escape the forbidden character. */
2518 if(ret_path)
2519 pct_encode_val(*ptr, ret_path+len);
2520 len += 3;
2521 do_default_action = FALSE;
2525 if(do_default_action) {
2526 if(ret_path)
2527 ret_path[len] = *ptr;
2528 len++;
2532 /* Removing the dot segments only happens when it's not in
2533 * computeOnly mode and it's not a wildcard scheme. File schemes
2534 * with USE_DOS_PATH set don't get dot segments removed.
2536 if(!(is_file && (flags & Uri_CREATE_FILE_USE_DOS_PATH)) &&
2537 scheme_type != URL_SCHEME_WILDCARD) {
2538 if(!(flags & Uri_CREATE_NO_CANONICALIZE) && ret_path) {
2539 /* Remove the dot segments (if any) and reset everything to the new
2540 * correct length.
2542 len = remove_dot_segments(ret_path, len);
2546 if(ret_path)
2547 TRACE("Canonicalized path %s len=%ld\n", debugstr_wn(ret_path, len), len);
2548 return len;
2551 /* Attempts to canonicalize the path for an opaque URI.
2553 * For known scheme types:
2554 * 1) forbidden characters are percent encoded if
2555 * NO_ENCODE_FORBIDDEN_CHARACTERS isn't set.
2557 * 2) Percent encoded, unreserved characters are decoded
2558 * to their actual values, for known scheme types.
2560 * 3) '\\' are changed to '/' for known scheme types
2561 * except for mailto schemes.
2563 * 4) For file schemes, if USE_DOS_PATH is set all '/'
2564 * are converted to backslashes.
2566 * 5) For file schemes, if USE_DOS_PATH isn't set all '\'
2567 * are converted to forward slashes.
2569 static BOOL canonicalize_path_opaque(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2570 const WCHAR *ptr;
2571 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
2572 const BOOL is_file = data->scheme_type == URL_SCHEME_FILE;
2573 const BOOL is_mk = data->scheme_type == URL_SCHEME_MK;
2575 if(!data->path) {
2576 uri->path_start = -1;
2577 uri->path_len = 0;
2578 return TRUE;
2581 uri->path_start = uri->canon_len;
2583 if(is_mk){
2584 /* hijack this flag for SCHEME_MK to tell the function when to start
2585 * converting slashes */
2586 flags |= Uri_CREATE_FILE_USE_DOS_PATH;
2589 /* For javascript: URIs, simply copy path part without any canonicalization */
2590 if(data->scheme_type == URL_SCHEME_JAVASCRIPT) {
2591 if(!computeOnly)
2592 memcpy(uri->canon_uri+uri->canon_len, data->path, data->path_len*sizeof(WCHAR));
2593 uri->path_len = data->path_len;
2594 uri->canon_len += data->path_len;
2595 return TRUE;
2598 /* Windows doesn't allow a "//" to appear after the scheme
2599 * of a URI, if it's an opaque URI.
2601 if(data->scheme && *(data->path) == '/' && *(data->path+1) == '/') {
2602 /* So it inserts a "/." before the "//" if it exists. */
2603 if(!computeOnly) {
2604 uri->canon_uri[uri->canon_len] = '/';
2605 uri->canon_uri[uri->canon_len+1] = '.';
2608 uri->canon_len += 2;
2611 for(ptr = data->path; ptr < data->path+data->path_len; ++ptr) {
2612 BOOL do_default_action = TRUE;
2614 if(*ptr == '%' && known_scheme) {
2615 WCHAR val = decode_pct_val(ptr);
2617 if(is_unreserved(val)) {
2618 if(!computeOnly)
2619 uri->canon_uri[uri->canon_len] = val;
2620 ++uri->canon_len;
2622 ptr += 2;
2623 continue;
2625 } else if(*ptr == '/' && is_file && (flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
2626 if(!computeOnly)
2627 uri->canon_uri[uri->canon_len] = '\\';
2628 ++uri->canon_len;
2629 do_default_action = FALSE;
2630 } else if(*ptr == '\\') {
2631 if((data->is_relative || is_mk || is_file) && !(flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
2632 /* Convert to a '/'. */
2633 if(!computeOnly)
2634 uri->canon_uri[uri->canon_len] = '/';
2635 ++uri->canon_len;
2636 do_default_action = FALSE;
2638 } else if(is_mk && *ptr == ':' && ptr + 1 < data->path + data->path_len && *(ptr + 1) == ':') {
2639 flags &= ~Uri_CREATE_FILE_USE_DOS_PATH;
2640 } else if(known_scheme && is_ascii(*ptr) && !is_unreserved(*ptr) && !is_reserved(*ptr) &&
2641 !(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS)) {
2642 if(!(is_file && (flags & Uri_CREATE_FILE_USE_DOS_PATH))) {
2643 if(!computeOnly)
2644 pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
2645 uri->canon_len += 3;
2646 do_default_action = FALSE;
2650 if(do_default_action) {
2651 if(!computeOnly)
2652 uri->canon_uri[uri->canon_len] = *ptr;
2653 ++uri->canon_len;
2657 if(is_mk && !computeOnly && !(flags & Uri_CREATE_NO_CANONICALIZE)) {
2658 DWORD new_len = remove_dot_segments(uri->canon_uri + uri->path_start,
2659 uri->canon_len - uri->path_start);
2660 uri->canon_len = uri->path_start + new_len;
2663 uri->path_len = uri->canon_len - uri->path_start;
2665 if(!computeOnly)
2666 TRACE("(%p %p %lx %d): Canonicalized opaque URI path %s len=%ld\n", data, uri, flags, computeOnly,
2667 debugstr_wn(uri->canon_uri+uri->path_start, uri->path_len), uri->path_len);
2668 return TRUE;
2671 /* Determines how the URI represented by the parse_data should be canonicalized.
2673 * Essentially, if the parse_data represents an hierarchical URI then it calls
2674 * canonicalize_authority and the canonicalization functions for the path. If the
2675 * URI is opaque it canonicalizes the path of the URI.
2677 static BOOL canonicalize_hierpart(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2678 if(!data->is_opaque || (data->is_relative && (data->password || data->username))) {
2679 /* "//" is only added for non-wildcard scheme types.
2681 * A "//" is only added to a relative URI if it has a
2682 * host or port component (this only happens if a IUriBuilder
2683 * is generating an IUri).
2685 if((data->is_relative && (data->host || data->has_port)) ||
2686 (!data->is_relative && data->scheme_type != URL_SCHEME_WILDCARD)) {
2687 if(data->scheme_type == URL_SCHEME_WILDCARD)
2688 FIXME("Here\n");
2690 if(!computeOnly) {
2691 INT pos = uri->canon_len;
2693 uri->canon_uri[pos] = '/';
2694 uri->canon_uri[pos+1] = '/';
2696 uri->canon_len += 2;
2699 if(!canonicalize_authority(data, uri, flags, computeOnly))
2700 return FALSE;
2702 if(data->is_relative && (data->password || data->username)) {
2703 if(!canonicalize_path_opaque(data, uri, flags, computeOnly))
2704 return FALSE;
2705 } else {
2706 if(!computeOnly)
2707 uri->path_start = uri->canon_len;
2708 uri->path_len = canonicalize_path_hierarchical(data->path, data->path_len, data->scheme_type, data->host_len != 0,
2709 flags, data->has_implicit_scheme, computeOnly ? NULL : uri->canon_uri+uri->canon_len);
2710 uri->canon_len += uri->path_len;
2711 if(!computeOnly && !uri->path_len)
2712 uri->path_start = -1;
2714 } else {
2715 /* Opaque URI's don't have an authority. */
2716 uri->userinfo_start = uri->userinfo_split = -1;
2717 uri->userinfo_len = 0;
2718 uri->host_start = -1;
2719 uri->host_len = 0;
2720 uri->host_type = Uri_HOST_UNKNOWN;
2721 uri->has_port = FALSE;
2722 uri->authority_start = -1;
2723 uri->authority_len = 0;
2724 uri->domain_offset = -1;
2725 uri->port_offset = -1;
2727 if(is_hierarchical_scheme(data->scheme_type)) {
2728 DWORD i;
2730 /* Absolute URIs aren't displayed for known scheme types
2731 * which should be hierarchical URIs.
2733 uri->display_modifiers |= URI_DISPLAY_NO_ABSOLUTE_URI;
2735 /* Windows also sets the port for these (if they have one). */
2736 for(i = 0; i < ARRAY_SIZE(default_ports); ++i) {
2737 if(data->scheme_type == default_ports[i].scheme) {
2738 uri->has_port = TRUE;
2739 uri->port = default_ports[i].port;
2740 break;
2745 if(!canonicalize_path_opaque(data, uri, flags, computeOnly))
2746 return FALSE;
2749 if(uri->path_start > -1 && !computeOnly)
2750 /* Finding file extensions happens for both types of URIs. */
2751 uri->extension_offset = find_file_extension(uri->canon_uri+uri->path_start, uri->path_len);
2752 else
2753 uri->extension_offset = -1;
2755 return TRUE;
2758 /* Attempts to canonicalize the query string of the URI.
2760 * Things that happen:
2761 * 1) For known scheme types forbidden characters
2762 * are percent encoded, unless the NO_DECODE_EXTRA_INFO flag is set
2763 * or NO_ENCODE_FORBIDDEN_CHARACTERS is set.
2765 * 2) For known scheme types, percent encoded, unreserved characters
2766 * are decoded as long as the NO_DECODE_EXTRA_INFO flag isn't set.
2768 static BOOL canonicalize_query(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2769 const WCHAR *ptr, *end;
2770 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
2772 if(!data->query) {
2773 uri->query_start = -1;
2774 uri->query_len = 0;
2775 return TRUE;
2778 uri->query_start = uri->canon_len;
2780 end = data->query+data->query_len;
2781 for(ptr = data->query; ptr < end; ++ptr) {
2782 if(*ptr == '%') {
2783 if(known_scheme && !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
2784 WCHAR val = decode_pct_val(ptr);
2785 if(is_unreserved(val)) {
2786 if(!computeOnly)
2787 uri->canon_uri[uri->canon_len] = val;
2788 ++uri->canon_len;
2790 ptr += 2;
2791 continue;
2794 } else if(known_scheme && is_ascii(*ptr) && !is_unreserved(*ptr) && !is_reserved(*ptr)) {
2795 if(!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS) &&
2796 !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
2797 if(!computeOnly)
2798 pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
2799 uri->canon_len += 3;
2800 continue;
2804 if(!computeOnly)
2805 uri->canon_uri[uri->canon_len] = *ptr;
2806 ++uri->canon_len;
2809 uri->query_len = uri->canon_len - uri->query_start;
2811 if(!computeOnly)
2812 TRACE("(%p %p %lx %d): Canonicalized query string %s len=%ld\n", data, uri, flags,
2813 computeOnly, debugstr_wn(uri->canon_uri+uri->query_start, uri->query_len),
2814 uri->query_len);
2815 return TRUE;
2818 static BOOL canonicalize_fragment(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2819 const WCHAR *ptr, *end;
2820 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
2822 if(!data->fragment) {
2823 uri->fragment_start = -1;
2824 uri->fragment_len = 0;
2825 return TRUE;
2828 uri->fragment_start = uri->canon_len;
2830 end = data->fragment + data->fragment_len;
2831 for(ptr = data->fragment; ptr < end; ++ptr) {
2832 if(*ptr == '%') {
2833 if(known_scheme && !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
2834 WCHAR val = decode_pct_val(ptr);
2835 if(is_unreserved(val)) {
2836 if(!computeOnly)
2837 uri->canon_uri[uri->canon_len] = val;
2838 ++uri->canon_len;
2840 ptr += 2;
2841 continue;
2844 } else if(known_scheme && is_ascii(*ptr) && !is_unreserved(*ptr) && !is_reserved(*ptr)) {
2845 if(!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS) &&
2846 !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
2847 if(!computeOnly)
2848 pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
2849 uri->canon_len += 3;
2850 continue;
2854 if(!computeOnly)
2855 uri->canon_uri[uri->canon_len] = *ptr;
2856 ++uri->canon_len;
2859 uri->fragment_len = uri->canon_len - uri->fragment_start;
2861 if(!computeOnly)
2862 TRACE("(%p %p %lx %d): Canonicalized fragment %s len=%ld\n", data, uri, flags,
2863 computeOnly, debugstr_wn(uri->canon_uri+uri->fragment_start, uri->fragment_len),
2864 uri->fragment_len);
2865 return TRUE;
2868 /* Canonicalizes the scheme information specified in the parse_data using the specified flags. */
2869 static BOOL canonicalize_scheme(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2870 uri->scheme_start = -1;
2871 uri->scheme_len = 0;
2873 if(!data->scheme) {
2874 /* The only type of URI that doesn't have to have a scheme is a relative
2875 * URI.
2877 if(!data->is_relative) {
2878 FIXME("(%p %p %lx): Unable to determine the scheme type of %s.\n", data,
2879 uri, flags, debugstr_w(data->uri));
2880 return FALSE;
2882 } else {
2883 if(!computeOnly) {
2884 DWORD i;
2885 INT pos = uri->canon_len;
2887 for(i = 0; i < data->scheme_len; ++i) {
2888 /* Scheme name must be lower case after canonicalization. */
2889 uri->canon_uri[i + pos] = towlower(data->scheme[i]);
2892 uri->canon_uri[i + pos] = ':';
2893 uri->scheme_start = pos;
2895 TRACE("(%p %p %lx): Canonicalized scheme=%s, len=%ld.\n", data, uri, flags,
2896 debugstr_wn(uri->canon_uri+uri->scheme_start, data->scheme_len), data->scheme_len);
2899 /* This happens in both computation modes. */
2900 uri->canon_len += data->scheme_len + 1;
2901 uri->scheme_len = data->scheme_len;
2903 return TRUE;
2906 /* Computes what the length of the URI specified by the parse_data will be
2907 * after canonicalization occurs using the specified flags.
2909 * This function will return a non-zero value indicating the length of the canonicalized
2910 * URI, or -1 on error.
2912 static int compute_canonicalized_length(const parse_data *data, DWORD flags) {
2913 Uri uri;
2915 memset(&uri, 0, sizeof(Uri));
2917 TRACE("(%p %lx): Beginning to compute canonicalized length for URI %s\n", data, flags,
2918 debugstr_w(data->uri));
2920 if(!canonicalize_scheme(data, &uri, flags, TRUE)) {
2921 ERR("(%p %lx): Failed to compute URI scheme length.\n", data, flags);
2922 return -1;
2925 if(!canonicalize_hierpart(data, &uri, flags, TRUE)) {
2926 ERR("(%p %lx): Failed to compute URI hierpart length.\n", data, flags);
2927 return -1;
2930 if(!canonicalize_query(data, &uri, flags, TRUE)) {
2931 ERR("(%p %lx): Failed to compute query string length.\n", data, flags);
2932 return -1;
2935 if(!canonicalize_fragment(data, &uri, flags, TRUE)) {
2936 ERR("(%p %lx): Failed to compute fragment length.\n", data, flags);
2937 return -1;
2940 TRACE("(%p %lx): Finished computing canonicalized URI length. length=%ld\n", data, flags, uri.canon_len);
2942 return uri.canon_len;
2945 /* Canonicalizes the URI data specified in the parse_data, using the given flags. If the
2946 * canonicalization succeeds it will store all the canonicalization information
2947 * in the pointer to the Uri.
2949 * To canonicalize a URI this function first computes what the length of the URI
2950 * specified by the parse_data will be. Once this is done it will then perform the actual
2951 * canonicalization of the URI.
2953 static HRESULT canonicalize_uri(const parse_data *data, Uri *uri, DWORD flags) {
2954 INT len;
2956 uri->canon_uri = NULL;
2957 uri->canon_size = uri->canon_len = 0;
2959 TRACE("(%p %p %lx): beginning to canonicalize URI %s.\n", data, uri, flags, debugstr_w(data->uri));
2961 /* First try to compute the length of the URI. */
2962 len = compute_canonicalized_length(data, flags);
2963 if(len == -1) {
2964 ERR("(%p %p %lx): Could not compute the canonicalized length of %s.\n", data, uri, flags,
2965 debugstr_w(data->uri));
2966 return E_INVALIDARG;
2969 uri->canon_uri = malloc((len + 1) * sizeof(WCHAR));
2970 if(!uri->canon_uri)
2971 return E_OUTOFMEMORY;
2973 uri->canon_size = len;
2974 if(!canonicalize_scheme(data, uri, flags, FALSE)) {
2975 ERR("(%p %p %lx): Unable to canonicalize the scheme of the URI.\n", data, uri, flags);
2976 return E_INVALIDARG;
2978 uri->scheme_type = data->scheme_type;
2980 if(!canonicalize_hierpart(data, uri, flags, FALSE)) {
2981 ERR("(%p %p %lx): Unable to canonicalize the hierpart of the URI\n", data, uri, flags);
2982 return E_INVALIDARG;
2985 if(!canonicalize_query(data, uri, flags, FALSE)) {
2986 ERR("(%p %p %lx): Unable to canonicalize query string of the URI.\n",
2987 data, uri, flags);
2988 return E_INVALIDARG;
2991 if(!canonicalize_fragment(data, uri, flags, FALSE)) {
2992 ERR("(%p %p %lx): Unable to canonicalize fragment of the URI.\n",
2993 data, uri, flags);
2994 return E_INVALIDARG;
2997 /* There's a possibility we didn't use all the space we allocated
2998 * earlier.
3000 if(uri->canon_len < uri->canon_size) {
3001 /* This happens if the URI is hierarchical and dot
3002 * segments were removed from its path.
3004 WCHAR *tmp = realloc(uri->canon_uri, (uri->canon_len + 1) * sizeof(WCHAR));
3005 if(!tmp)
3006 return E_OUTOFMEMORY;
3008 uri->canon_uri = tmp;
3009 uri->canon_size = uri->canon_len;
3012 uri->canon_uri[uri->canon_len] = '\0';
3013 TRACE("(%p %p %lx): finished canonicalizing the URI. uri=%s\n", data, uri, flags, debugstr_w(uri->canon_uri));
3015 return S_OK;
3018 static HRESULT get_builder_component(LPWSTR *component, DWORD *component_len,
3019 LPCWSTR source, DWORD source_len,
3020 LPCWSTR *output, DWORD *output_len)
3022 if(!output_len) {
3023 if(output)
3024 *output = NULL;
3025 return E_POINTER;
3028 if(!output) {
3029 *output_len = 0;
3030 return E_POINTER;
3033 if(!(*component) && source) {
3034 /* Allocate 'component', and copy the contents from 'source'
3035 * into the new allocation.
3037 *component = malloc((source_len + 1) * sizeof(WCHAR));
3038 if(!(*component))
3039 return E_OUTOFMEMORY;
3041 memcpy(*component, source, source_len*sizeof(WCHAR));
3042 (*component)[source_len] = '\0';
3043 *component_len = source_len;
3046 *output = *component;
3047 *output_len = *component_len;
3048 return *output ? S_OK : S_FALSE;
3051 /* Allocates 'component' and copies the string from 'new_value' into 'component'.
3052 * If 'prefix' is set and 'new_value' isn't NULL, then it checks if 'new_value'
3053 * starts with 'prefix'. If it doesn't then 'prefix' is prepended to 'component'.
3055 * If everything is successful, then will set 'success_flag' in 'flags'.
3057 static HRESULT set_builder_component(LPWSTR *component, DWORD *component_len, LPCWSTR new_value,
3058 WCHAR prefix, DWORD *flags, DWORD success_flag)
3060 free(*component);
3062 if(!new_value) {
3063 *component = NULL;
3064 *component_len = 0;
3065 } else {
3066 BOOL add_prefix = FALSE;
3067 DWORD len = lstrlenW(new_value);
3068 DWORD pos = 0;
3070 if(prefix && *new_value != prefix) {
3071 add_prefix = TRUE;
3072 *component = malloc((len + 2) * sizeof(WCHAR));
3073 } else
3074 *component = malloc((len + 1) * sizeof(WCHAR));
3076 if(!(*component))
3077 return E_OUTOFMEMORY;
3079 if(add_prefix)
3080 (*component)[pos++] = prefix;
3082 memcpy(*component+pos, new_value, (len+1)*sizeof(WCHAR));
3083 *component_len = len+pos;
3086 *flags |= success_flag;
3087 return S_OK;
3090 static void reset_builder(UriBuilder *builder) {
3091 if(builder->uri)
3092 IUri_Release(&builder->uri->IUri_iface);
3093 builder->uri = NULL;
3095 free(builder->fragment);
3096 builder->fragment = NULL;
3097 builder->fragment_len = 0;
3099 free(builder->host);
3100 builder->host = NULL;
3101 builder->host_len = 0;
3103 free(builder->password);
3104 builder->password = NULL;
3105 builder->password_len = 0;
3107 free(builder->path);
3108 builder->path = NULL;
3109 builder->path_len = 0;
3111 free(builder->query);
3112 builder->query = NULL;
3113 builder->query_len = 0;
3115 free(builder->scheme);
3116 builder->scheme = NULL;
3117 builder->scheme_len = 0;
3119 free(builder->username);
3120 builder->username = NULL;
3121 builder->username_len = 0;
3123 builder->has_port = FALSE;
3124 builder->port = 0;
3125 builder->modified_props = 0;
3128 static HRESULT validate_scheme_name(const UriBuilder *builder, parse_data *data, DWORD flags) {
3129 const WCHAR *component;
3130 const WCHAR *ptr;
3131 const WCHAR **pptr;
3132 DWORD expected_len;
3134 if(builder->scheme) {
3135 ptr = builder->scheme;
3136 expected_len = builder->scheme_len;
3137 } else if(builder->uri && builder->uri->scheme_start > -1) {
3138 ptr = builder->uri->canon_uri+builder->uri->scheme_start;
3139 expected_len = builder->uri->scheme_len;
3140 } else {
3141 ptr = L"";
3142 expected_len = 0;
3145 component = ptr;
3146 pptr = &ptr;
3147 if(parse_scheme(pptr, data, flags, ALLOW_NULL_TERM_SCHEME) &&
3148 data->scheme_len == expected_len) {
3149 if(data->scheme)
3150 TRACE("(%p %p %lx): Found valid scheme component %s len=%ld.\n", builder, data, flags,
3151 debugstr_wn(data->scheme, data->scheme_len), data->scheme_len);
3152 } else {
3153 TRACE("(%p %p %lx): Invalid scheme component found %s.\n", builder, data, flags,
3154 debugstr_wn(component, expected_len));
3155 return INET_E_INVALID_URL;
3158 return S_OK;
3161 static HRESULT validate_username(const UriBuilder *builder, parse_data *data, DWORD flags) {
3162 const WCHAR *ptr;
3163 const WCHAR **pptr;
3164 DWORD expected_len;
3166 if(builder->username) {
3167 ptr = builder->username;
3168 expected_len = builder->username_len;
3169 } else if(!(builder->modified_props & Uri_HAS_USER_NAME) && builder->uri &&
3170 builder->uri->userinfo_start > -1 && builder->uri->userinfo_split != 0) {
3171 /* Just use the username from the base Uri. */
3172 data->username = builder->uri->canon_uri+builder->uri->userinfo_start;
3173 data->username_len = (builder->uri->userinfo_split > -1) ?
3174 builder->uri->userinfo_split : builder->uri->userinfo_len;
3175 ptr = NULL;
3176 } else {
3177 ptr = NULL;
3178 expected_len = 0;
3181 if(ptr) {
3182 const WCHAR *component = ptr;
3183 pptr = &ptr;
3184 if(parse_username(pptr, data, flags, ALLOW_NULL_TERM_USER_NAME) &&
3185 data->username_len == expected_len)
3186 TRACE("(%p %p %lx): Found valid username component %s len=%ld.\n", builder, data, flags,
3187 debugstr_wn(data->username, data->username_len), data->username_len);
3188 else {
3189 TRACE("(%p %p %lx): Invalid username component found %s.\n", builder, data, flags,
3190 debugstr_wn(component, expected_len));
3191 return INET_E_INVALID_URL;
3195 return S_OK;
3198 static HRESULT validate_password(const UriBuilder *builder, parse_data *data, DWORD flags) {
3199 const WCHAR *ptr;
3200 const WCHAR **pptr;
3201 DWORD expected_len;
3203 if(builder->password) {
3204 ptr = builder->password;
3205 expected_len = builder->password_len;
3206 } else if(!(builder->modified_props & Uri_HAS_PASSWORD) && builder->uri &&
3207 builder->uri->userinfo_split > -1) {
3208 data->password = builder->uri->canon_uri+builder->uri->userinfo_start+builder->uri->userinfo_split+1;
3209 data->password_len = builder->uri->userinfo_len-builder->uri->userinfo_split-1;
3210 ptr = NULL;
3211 } else {
3212 ptr = NULL;
3213 expected_len = 0;
3216 if(ptr) {
3217 const WCHAR *component = ptr;
3218 pptr = &ptr;
3219 if(parse_password(pptr, data, flags, ALLOW_NULL_TERM_PASSWORD) &&
3220 data->password_len == expected_len)
3221 TRACE("(%p %p %lx): Found valid password component %s len=%ld.\n", builder, data, flags,
3222 debugstr_wn(data->password, data->password_len), data->password_len);
3223 else {
3224 TRACE("(%p %p %lx): Invalid password component found %s.\n", builder, data, flags,
3225 debugstr_wn(component, expected_len));
3226 return INET_E_INVALID_URL;
3230 return S_OK;
3233 static HRESULT validate_userinfo(const UriBuilder *builder, parse_data *data, DWORD flags) {
3234 HRESULT hr;
3236 hr = validate_username(builder, data, flags);
3237 if(FAILED(hr))
3238 return hr;
3240 hr = validate_password(builder, data, flags);
3241 if(FAILED(hr))
3242 return hr;
3244 return S_OK;
3247 static HRESULT validate_host(const UriBuilder *builder, parse_data *data) {
3248 const WCHAR *ptr;
3249 const WCHAR **pptr;
3250 DWORD expected_len;
3252 if(builder->host) {
3253 ptr = builder->host;
3254 expected_len = builder->host_len;
3255 } else if(!(builder->modified_props & Uri_HAS_HOST) && builder->uri && builder->uri->host_start > -1) {
3256 ptr = builder->uri->canon_uri + builder->uri->host_start;
3257 expected_len = builder->uri->host_len;
3258 } else
3259 ptr = NULL;
3261 if(ptr) {
3262 const WCHAR *component = ptr;
3263 DWORD extras = ALLOW_BRACKETLESS_IP_LITERAL|IGNORE_PORT_DELIMITER|SKIP_IP_FUTURE_CHECK;
3264 pptr = &ptr;
3266 if(parse_host(pptr, data, extras) && data->host_len == expected_len)
3267 TRACE("(%p %p): Found valid host name %s len=%ld type=%d.\n", builder, data,
3268 debugstr_wn(data->host, data->host_len), data->host_len, data->host_type);
3269 else {
3270 TRACE("(%p %p): Invalid host name found %s.\n", builder, data,
3271 debugstr_wn(component, expected_len));
3272 return INET_E_INVALID_URL;
3276 return S_OK;
3279 static void setup_port(const UriBuilder *builder, parse_data *data, DWORD flags) {
3280 if(builder->modified_props & Uri_HAS_PORT) {
3281 if(builder->has_port) {
3282 data->has_port = TRUE;
3283 data->port_value = builder->port;
3285 } else if(builder->uri && builder->uri->has_port) {
3286 data->has_port = TRUE;
3287 data->port_value = builder->uri->port;
3290 if(data->has_port)
3291 TRACE("(%p %p %lx): Using %lu as port for IUri.\n", builder, data, flags, data->port_value);
3294 static HRESULT validate_path(const UriBuilder *builder, parse_data *data, DWORD flags) {
3295 const WCHAR *ptr = NULL;
3296 const WCHAR *component;
3297 const WCHAR **pptr;
3298 DWORD expected_len;
3299 BOOL check_len = TRUE;
3300 BOOL valid = FALSE;
3302 if(builder->path) {
3303 ptr = builder->path;
3304 expected_len = builder->path_len;
3305 } else if(!(builder->modified_props & Uri_HAS_PATH) &&
3306 builder->uri && builder->uri->path_start > -1) {
3307 ptr = builder->uri->canon_uri+builder->uri->path_start;
3308 expected_len = builder->uri->path_len;
3309 } else {
3310 ptr = L"";
3311 check_len = FALSE;
3312 expected_len = -1;
3315 component = ptr;
3316 pptr = &ptr;
3318 /* How the path is validated depends on what type of
3319 * URI it is.
3321 valid = data->is_opaque ?
3322 parse_path_opaque(pptr, data, flags) : parse_path_hierarchical(pptr, data, flags);
3324 if(!valid || (check_len && expected_len != data->path_len)) {
3325 TRACE("(%p %p %lx): Invalid path component %s.\n", builder, data, flags,
3326 debugstr_wn(component, expected_len) );
3327 return INET_E_INVALID_URL;
3330 TRACE("(%p %p %lx): Valid path component %s len=%ld.\n", builder, data, flags,
3331 debugstr_wn(data->path, data->path_len), data->path_len);
3333 return S_OK;
3336 static HRESULT validate_query(const UriBuilder *builder, parse_data *data, DWORD flags) {
3337 const WCHAR *ptr = NULL;
3338 const WCHAR **pptr;
3339 DWORD expected_len;
3341 if(builder->query) {
3342 ptr = builder->query;
3343 expected_len = builder->query_len;
3344 } else if(!(builder->modified_props & Uri_HAS_QUERY) && builder->uri &&
3345 builder->uri->query_start > -1) {
3346 ptr = builder->uri->canon_uri+builder->uri->query_start;
3347 expected_len = builder->uri->query_len;
3350 if(ptr) {
3351 const WCHAR *component = ptr;
3352 pptr = &ptr;
3354 if(parse_query(pptr, data, flags) && expected_len == data->query_len)
3355 TRACE("(%p %p %lx): Valid query component %s len=%ld.\n", builder, data, flags,
3356 debugstr_wn(data->query, data->query_len), data->query_len);
3357 else {
3358 TRACE("(%p %p %lx): Invalid query component %s.\n", builder, data, flags,
3359 debugstr_wn(component, expected_len));
3360 return INET_E_INVALID_URL;
3364 return S_OK;
3367 static HRESULT validate_fragment(const UriBuilder *builder, parse_data *data, DWORD flags) {
3368 const WCHAR *ptr = NULL;
3369 const WCHAR **pptr;
3370 DWORD expected_len;
3372 if(builder->fragment) {
3373 ptr = builder->fragment;
3374 expected_len = builder->fragment_len;
3375 } else if(!(builder->modified_props & Uri_HAS_FRAGMENT) && builder->uri &&
3376 builder->uri->fragment_start > -1) {
3377 ptr = builder->uri->canon_uri+builder->uri->fragment_start;
3378 expected_len = builder->uri->fragment_len;
3381 if(ptr) {
3382 const WCHAR *component = ptr;
3383 pptr = &ptr;
3385 if(parse_fragment(pptr, data, flags) && expected_len == data->fragment_len)
3386 TRACE("(%p %p %lx): Valid fragment component %s len=%ld.\n", builder, data, flags,
3387 debugstr_wn(data->fragment, data->fragment_len), data->fragment_len);
3388 else {
3389 TRACE("(%p %p %lx): Invalid fragment component %s.\n", builder, data, flags,
3390 debugstr_wn(component, expected_len));
3391 return INET_E_INVALID_URL;
3395 return S_OK;
3398 static HRESULT validate_components(const UriBuilder *builder, parse_data *data, DWORD flags) {
3399 HRESULT hr;
3401 memset(data, 0, sizeof(parse_data));
3403 TRACE("(%p %p %lx): Beginning to validate builder components.\n", builder, data, flags);
3405 hr = validate_scheme_name(builder, data, flags);
3406 if(FAILED(hr))
3407 return hr;
3409 /* Extra validation for file schemes. */
3410 if(data->scheme_type == URL_SCHEME_FILE) {
3411 if((builder->password || (builder->uri && builder->uri->userinfo_split > -1)) ||
3412 (builder->username || (builder->uri && builder->uri->userinfo_start > -1))) {
3413 TRACE("(%p %p %lx): File schemes can't contain a username or password.\n",
3414 builder, data, flags);
3415 return INET_E_INVALID_URL;
3419 hr = validate_userinfo(builder, data, flags);
3420 if(FAILED(hr))
3421 return hr;
3423 hr = validate_host(builder, data);
3424 if(FAILED(hr))
3425 return hr;
3427 setup_port(builder, data, flags);
3429 /* The URI is opaque if it doesn't have an authority component. */
3430 if(!data->is_relative)
3431 data->is_opaque = !data->username && !data->password && !data->host && !data->has_port
3432 && data->scheme_type != URL_SCHEME_FILE;
3433 else
3434 data->is_opaque = !data->host && !data->has_port;
3436 hr = validate_path(builder, data, flags);
3437 if(FAILED(hr))
3438 return hr;
3440 hr = validate_query(builder, data, flags);
3441 if(FAILED(hr))
3442 return hr;
3444 hr = validate_fragment(builder, data, flags);
3445 if(FAILED(hr))
3446 return hr;
3448 TRACE("(%p %p %lx): Finished validating builder components.\n", builder, data, flags);
3450 return S_OK;
3453 static HRESULT compare_file_paths(const Uri *a, const Uri *b, BOOL *ret)
3455 WCHAR *canon_path_a, *canon_path_b;
3456 DWORD len_a, len_b;
3458 if(!a->path_len) {
3459 *ret = !b->path_len;
3460 return S_OK;
3463 if(!b->path_len) {
3464 *ret = FALSE;
3465 return S_OK;
3468 /* Fast path */
3469 if(a->path_len == b->path_len && !wcsnicmp(a->canon_uri+a->path_start, b->canon_uri+b->path_start, a->path_len)) {
3470 *ret = TRUE;
3471 return S_OK;
3474 len_a = canonicalize_path_hierarchical(a->canon_uri+a->path_start, a->path_len, a->scheme_type, FALSE, 0, FALSE, NULL);
3475 len_b = canonicalize_path_hierarchical(b->canon_uri+b->path_start, b->path_len, b->scheme_type, FALSE, 0, FALSE, NULL);
3477 canon_path_a = malloc(len_a * sizeof(WCHAR));
3478 if(!canon_path_a)
3479 return E_OUTOFMEMORY;
3480 canon_path_b = malloc(len_b * sizeof(WCHAR));
3481 if(!canon_path_b) {
3482 free(canon_path_a);
3483 return E_OUTOFMEMORY;
3486 len_a = canonicalize_path_hierarchical(a->canon_uri+a->path_start, a->path_len, a->scheme_type, FALSE, 0, FALSE, canon_path_a);
3487 len_b = canonicalize_path_hierarchical(b->canon_uri+b->path_start, b->path_len, b->scheme_type, FALSE, 0, FALSE, canon_path_b);
3489 *ret = len_a == len_b && !wcsnicmp(canon_path_a, canon_path_b, len_a);
3491 free(canon_path_a);
3492 free(canon_path_b);
3493 return S_OK;
3496 /* Checks if the two Uri's are logically equivalent. It's a simple
3497 * comparison, since they are both of type Uri, and it can access
3498 * the properties of each Uri directly without the need to go
3499 * through the "IUri_Get*" interface calls.
3501 static HRESULT compare_uris(const Uri *a, const Uri *b, BOOL *ret) {
3502 const BOOL known_scheme = a->scheme_type != URL_SCHEME_UNKNOWN;
3503 const BOOL are_hierarchical = a->authority_start > -1 && b->authority_start > -1;
3504 HRESULT hres;
3506 *ret = FALSE;
3508 if(a->scheme_type != b->scheme_type)
3509 return S_OK;
3511 /* Only compare the scheme names (if any) if their unknown scheme types. */
3512 if(!known_scheme) {
3513 if((a->scheme_start > -1 && b->scheme_start > -1) &&
3514 (a->scheme_len == b->scheme_len)) {
3515 /* Make sure the schemes are the same. */
3516 if(StrCmpNW(a->canon_uri+a->scheme_start, b->canon_uri+b->scheme_start, a->scheme_len))
3517 return S_OK;
3518 } else if(a->scheme_len != b->scheme_len)
3519 /* One of the Uri's has a scheme name, while the other doesn't. */
3520 return S_OK;
3523 /* If they have a userinfo component, perform case sensitive compare. */
3524 if((a->userinfo_start > -1 && b->userinfo_start > -1) &&
3525 (a->userinfo_len == b->userinfo_len)) {
3526 if(StrCmpNW(a->canon_uri+a->userinfo_start, b->canon_uri+b->userinfo_start, a->userinfo_len))
3527 return S_OK;
3528 } else if(a->userinfo_len != b->userinfo_len)
3529 /* One of the Uri's had a userinfo, while the other one doesn't. */
3530 return S_OK;
3532 /* Check if they have a host name. */
3533 if((a->host_start > -1 && b->host_start > -1) &&
3534 (a->host_len == b->host_len)) {
3535 /* Perform a case insensitive compare if they are a known scheme type. */
3536 if(known_scheme) {
3537 if(StrCmpNIW(a->canon_uri+a->host_start, b->canon_uri+b->host_start, a->host_len))
3538 return S_OK;
3539 } else if(StrCmpNW(a->canon_uri+a->host_start, b->canon_uri+b->host_start, a->host_len))
3540 return S_OK;
3541 } else if(a->host_len != b->host_len)
3542 /* One of the Uri's had a host, while the other one didn't. */
3543 return S_OK;
3545 if(a->has_port && b->has_port) {
3546 if(a->port != b->port)
3547 return S_OK;
3548 } else if(a->has_port || b->has_port)
3549 /* One had a port, while the other one didn't. */
3550 return S_OK;
3552 /* Windows is weird with how it handles paths. For example
3553 * One URI could be "http://google.com" (after canonicalization)
3554 * and one could be "http://google.com/" and the IsEqual function
3555 * would still evaluate to TRUE, but, only if they are both hierarchical
3556 * URIs.
3558 if(a->scheme_type == URL_SCHEME_FILE) {
3559 BOOL cmp;
3561 hres = compare_file_paths(a, b, &cmp);
3562 if(FAILED(hres) || !cmp)
3563 return hres;
3564 } else if((a->path_start > -1 && b->path_start > -1) &&
3565 (a->path_len == b->path_len)) {
3566 if(StrCmpNW(a->canon_uri+a->path_start, b->canon_uri+b->path_start, a->path_len))
3567 return S_OK;
3568 } else if(are_hierarchical && a->path_len == -1 && b->path_len == 0) {
3569 if(*(a->canon_uri+a->path_start) != '/')
3570 return S_OK;
3571 } else if(are_hierarchical && b->path_len == 1 && a->path_len == 0) {
3572 if(*(b->canon_uri+b->path_start) != '/')
3573 return S_OK;
3574 } else if(a->path_len != b->path_len)
3575 return S_OK;
3577 /* Compare the query strings of the two URIs. */
3578 if((a->query_start > -1 && b->query_start > -1) &&
3579 (a->query_len == b->query_len)) {
3580 if(StrCmpNW(a->canon_uri+a->query_start, b->canon_uri+b->query_start, a->query_len))
3581 return S_OK;
3582 } else if(a->query_len != b->query_len)
3583 return S_OK;
3585 if((a->fragment_start > -1 && b->fragment_start > -1) &&
3586 (a->fragment_len == b->fragment_len)) {
3587 if(StrCmpNW(a->canon_uri+a->fragment_start, b->canon_uri+b->fragment_start, a->fragment_len))
3588 return S_OK;
3589 } else if(a->fragment_len != b->fragment_len)
3590 return S_OK;
3592 /* If we get here, the two URIs are equivalent. */
3593 *ret = TRUE;
3594 return S_OK;
3597 static void convert_to_dos_path(const WCHAR *path, DWORD path_len,
3598 WCHAR *output, DWORD *output_len)
3600 const WCHAR *ptr = path;
3602 if(path_len > 3 && *ptr == '/' && is_drive_path(path+1))
3603 /* Skip over the leading / before the drive path. */
3604 ++ptr;
3606 for(; ptr < path+path_len; ++ptr) {
3607 if(*ptr == '/') {
3608 if(output)
3609 *output++ = '\\';
3610 (*output_len)++;
3611 } else {
3612 if(output)
3613 *output++ = *ptr;
3614 (*output_len)++;
3619 /* Generates a raw uri string using the parse_data. */
3620 static DWORD generate_raw_uri(const parse_data *data, BSTR uri, DWORD flags) {
3621 DWORD length = 0;
3623 if(data->scheme) {
3624 if(uri) {
3625 memcpy(uri, data->scheme, data->scheme_len*sizeof(WCHAR));
3626 uri[data->scheme_len] = ':';
3628 length += data->scheme_len+1;
3631 if(!data->is_opaque) {
3632 /* For the "//" which appears before the authority component. */
3633 if(uri) {
3634 uri[length] = '/';
3635 uri[length+1] = '/';
3637 length += 2;
3639 /* Check if we need to add the "\\" before the host name
3640 * of a UNC server name in a DOS path.
3642 if(flags & RAW_URI_CONVERT_TO_DOS_PATH &&
3643 data->scheme_type == URL_SCHEME_FILE && data->host) {
3644 if(uri) {
3645 uri[length] = '\\';
3646 uri[length+1] = '\\';
3648 length += 2;
3652 if(data->username) {
3653 if(uri)
3654 memcpy(uri+length, data->username, data->username_len*sizeof(WCHAR));
3655 length += data->username_len;
3658 if(data->password) {
3659 if(uri) {
3660 uri[length] = ':';
3661 memcpy(uri+length+1, data->password, data->password_len*sizeof(WCHAR));
3663 length += data->password_len+1;
3666 if(data->password || data->username) {
3667 if(uri)
3668 uri[length] = '@';
3669 ++length;
3672 if(data->host) {
3673 /* IPv6 addresses get the brackets added around them if they don't already
3674 * have them.
3676 const BOOL add_brackets = data->host_type == Uri_HOST_IPV6 && *(data->host) != '[';
3677 if(add_brackets) {
3678 if(uri)
3679 uri[length] = '[';
3680 ++length;
3683 if(uri)
3684 memcpy(uri+length, data->host, data->host_len*sizeof(WCHAR));
3685 length += data->host_len;
3687 if(add_brackets) {
3688 if(uri)
3689 uri[length] = ']';
3690 length++;
3694 if(data->has_port) {
3695 /* The port isn't included in the raw uri if it's the default
3696 * port for the scheme type.
3698 DWORD i;
3699 BOOL is_default = FALSE;
3701 for(i = 0; i < ARRAY_SIZE(default_ports); ++i) {
3702 if(data->scheme_type == default_ports[i].scheme &&
3703 data->port_value == default_ports[i].port)
3704 is_default = TRUE;
3707 if(!is_default || flags & RAW_URI_FORCE_PORT_DISP) {
3708 if(uri)
3709 uri[length] = ':';
3710 ++length;
3712 if(uri)
3713 length += ui2str(uri+length, data->port_value);
3714 else
3715 length += ui2str(NULL, data->port_value);
3719 /* Check if a '/' should be added before the path for hierarchical URIs. */
3720 if(!data->is_opaque && data->path && *(data->path) != '/') {
3721 if(uri)
3722 uri[length] = '/';
3723 ++length;
3726 if(data->path) {
3727 if(!data->is_opaque && data->scheme_type == URL_SCHEME_FILE &&
3728 flags & RAW_URI_CONVERT_TO_DOS_PATH) {
3729 DWORD len = 0;
3731 if(uri)
3732 convert_to_dos_path(data->path, data->path_len, uri+length, &len);
3733 else
3734 convert_to_dos_path(data->path, data->path_len, NULL, &len);
3736 length += len;
3737 } else {
3738 if(uri)
3739 memcpy(uri+length, data->path, data->path_len*sizeof(WCHAR));
3740 length += data->path_len;
3744 if(data->query) {
3745 if(uri)
3746 memcpy(uri+length, data->query, data->query_len*sizeof(WCHAR));
3747 length += data->query_len;
3750 if(data->fragment) {
3751 if(uri)
3752 memcpy(uri+length, data->fragment, data->fragment_len*sizeof(WCHAR));
3753 length += data->fragment_len;
3756 if(uri)
3757 TRACE("(%p %p): Generated raw uri=%s len=%ld\n", data, uri, debugstr_wn(uri, length), length);
3758 else
3759 TRACE("(%p %p): Computed raw uri len=%ld\n", data, uri, length);
3761 return length;
3764 static HRESULT generate_uri(const UriBuilder *builder, const parse_data *data, Uri *uri, DWORD flags) {
3765 HRESULT hr;
3766 DWORD length = generate_raw_uri(data, NULL, 0);
3767 uri->raw_uri = SysAllocStringLen(NULL, length);
3768 if(!uri->raw_uri)
3769 return E_OUTOFMEMORY;
3771 generate_raw_uri(data, uri->raw_uri, 0);
3773 hr = canonicalize_uri(data, uri, flags);
3774 if(FAILED(hr)) {
3775 if(hr == E_INVALIDARG)
3776 return INET_E_INVALID_URL;
3777 return hr;
3780 uri->create_flags = flags;
3781 return S_OK;
3784 static inline Uri* impl_from_IUri(IUri *iface)
3786 return CONTAINING_RECORD(iface, Uri, IUri_iface);
3789 static inline void destroy_uri_obj(Uri *This)
3791 SysFreeString(This->raw_uri);
3792 free(This->canon_uri);
3793 free(This);
3796 static HRESULT WINAPI Uri_QueryInterface(IUri *iface, REFIID riid, void **ppv)
3798 Uri *This = impl_from_IUri(iface);
3800 if(IsEqualGUID(&IID_IUnknown, riid)) {
3801 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
3802 *ppv = &This->IUri_iface;
3803 }else if(IsEqualGUID(&IID_IUri, riid)) {
3804 TRACE("(%p)->(IID_IUri %p)\n", This, ppv);
3805 *ppv = &This->IUri_iface;
3806 }else if(IsEqualGUID(&IID_IUriBuilderFactory, riid)) {
3807 TRACE("(%p)->(IID_IUriBuilderFactory %p)\n", This, ppv);
3808 *ppv = &This->IUriBuilderFactory_iface;
3809 }else if(IsEqualGUID(&IID_IPersistStream, riid)) {
3810 TRACE("(%p)->(IID_IPersistStream %p)\n", This, ppv);
3811 *ppv = &This->IPersistStream_iface;
3812 }else if(IsEqualGUID(&IID_IMarshal, riid)) {
3813 TRACE("(%p)->(IID_IMarshal %p)\n", This, ppv);
3814 *ppv = &This->IMarshal_iface;
3815 }else if(IsEqualGUID(&IID_IUriObj, riid)) {
3816 TRACE("(%p)->(IID_IUriObj %p)\n", This, ppv);
3817 *ppv = This;
3818 return S_OK;
3819 }else {
3820 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
3821 *ppv = NULL;
3822 return E_NOINTERFACE;
3825 IUnknown_AddRef((IUnknown*)*ppv);
3826 return S_OK;
3829 static ULONG WINAPI Uri_AddRef(IUri *iface)
3831 Uri *This = impl_from_IUri(iface);
3832 LONG ref = InterlockedIncrement(&This->ref);
3834 TRACE("(%p) ref=%ld\n", This, ref);
3836 return ref;
3839 static ULONG WINAPI Uri_Release(IUri *iface)
3841 Uri *This = impl_from_IUri(iface);
3842 LONG ref = InterlockedDecrement(&This->ref);
3844 TRACE("(%p) ref=%ld\n", This, ref);
3846 if(!ref)
3847 destroy_uri_obj(This);
3849 return ref;
3852 static HRESULT WINAPI Uri_GetPropertyBSTR(IUri *iface, Uri_PROPERTY uriProp, BSTR *pbstrProperty, DWORD dwFlags)
3854 Uri *This = impl_from_IUri(iface);
3855 HRESULT hres;
3856 TRACE("(%p %s)->(%d %p %lx)\n", This, debugstr_w(This->canon_uri), uriProp, pbstrProperty, dwFlags);
3858 if(!This->create_flags)
3859 return E_UNEXPECTED;
3860 if(!pbstrProperty)
3861 return E_POINTER;
3863 if(uriProp > Uri_PROPERTY_STRING_LAST) {
3864 /* It only returns S_FALSE for the ZONE property... */
3865 if(uriProp == Uri_PROPERTY_ZONE) {
3866 *pbstrProperty = SysAllocStringLen(NULL, 0);
3867 if(!(*pbstrProperty))
3868 return E_OUTOFMEMORY;
3869 return S_FALSE;
3872 *pbstrProperty = NULL;
3873 return E_INVALIDARG;
3876 /* Don't have support for flags yet. */
3877 if(dwFlags) {
3878 FIXME("(%p)->(%d %p %lx)\n", This, uriProp, pbstrProperty, dwFlags);
3879 return E_NOTIMPL;
3882 switch(uriProp) {
3883 case Uri_PROPERTY_ABSOLUTE_URI:
3884 if(This->display_modifiers & URI_DISPLAY_NO_ABSOLUTE_URI) {
3885 *pbstrProperty = SysAllocStringLen(NULL, 0);
3886 hres = S_FALSE;
3887 } else {
3888 if(This->scheme_type != URL_SCHEME_UNKNOWN && This->userinfo_start > -1) {
3889 if(This->userinfo_len == 0) {
3890 /* Don't include the '@' after the userinfo component. */
3891 *pbstrProperty = SysAllocStringLen(NULL, This->canon_len-1);
3892 hres = S_OK;
3893 if(*pbstrProperty) {
3894 /* Copy everything before it. */
3895 memcpy(*pbstrProperty, This->canon_uri, This->userinfo_start*sizeof(WCHAR));
3897 /* And everything after it. */
3898 memcpy(*pbstrProperty+This->userinfo_start, This->canon_uri+This->userinfo_start+1,
3899 (This->canon_len-This->userinfo_start-1)*sizeof(WCHAR));
3901 } else if(This->userinfo_split == 0 && This->userinfo_len == 1) {
3902 /* Don't include the ":@" */
3903 *pbstrProperty = SysAllocStringLen(NULL, This->canon_len-2);
3904 hres = S_OK;
3905 if(*pbstrProperty) {
3906 memcpy(*pbstrProperty, This->canon_uri, This->userinfo_start*sizeof(WCHAR));
3907 memcpy(*pbstrProperty+This->userinfo_start, This->canon_uri+This->userinfo_start+2,
3908 (This->canon_len-This->userinfo_start-2)*sizeof(WCHAR));
3910 } else {
3911 *pbstrProperty = SysAllocString(This->canon_uri);
3912 hres = S_OK;
3914 } else {
3915 *pbstrProperty = SysAllocString(This->canon_uri);
3916 hres = S_OK;
3920 if(!(*pbstrProperty))
3921 hres = E_OUTOFMEMORY;
3923 break;
3924 case Uri_PROPERTY_AUTHORITY:
3925 if(This->authority_start > -1) {
3926 if(This->port_offset > -1 && is_default_port(This->scheme_type, This->port) &&
3927 This->display_modifiers & URI_DISPLAY_NO_DEFAULT_PORT_AUTH)
3928 /* Don't include the port in the authority component. */
3929 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->authority_start, This->port_offset);
3930 else
3931 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->authority_start, This->authority_len);
3932 hres = S_OK;
3933 } else {
3934 *pbstrProperty = SysAllocStringLen(NULL, 0);
3935 hres = S_FALSE;
3938 if(!(*pbstrProperty))
3939 hres = E_OUTOFMEMORY;
3941 break;
3942 case Uri_PROPERTY_DISPLAY_URI:
3943 /* The Display URI contains everything except for the userinfo for known
3944 * scheme types.
3946 if(This->scheme_type != URL_SCHEME_UNKNOWN && This->userinfo_start > -1) {
3947 *pbstrProperty = SysAllocStringLen(NULL, This->canon_len-This->userinfo_len);
3949 if(*pbstrProperty) {
3950 /* Copy everything before the userinfo over. */
3951 memcpy(*pbstrProperty, This->canon_uri, This->userinfo_start*sizeof(WCHAR));
3952 /* Copy everything after the userinfo over. */
3953 memcpy(*pbstrProperty+This->userinfo_start,
3954 This->canon_uri+This->userinfo_start+This->userinfo_len+1,
3955 (This->canon_len-(This->userinfo_start+This->userinfo_len+1))*sizeof(WCHAR));
3957 } else
3958 *pbstrProperty = SysAllocString(This->canon_uri);
3960 if(!(*pbstrProperty))
3961 hres = E_OUTOFMEMORY;
3962 else
3963 hres = S_OK;
3965 break;
3966 case Uri_PROPERTY_DOMAIN:
3967 if(This->domain_offset > -1) {
3968 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->host_start+This->domain_offset,
3969 This->host_len-This->domain_offset);
3970 hres = S_OK;
3971 } else {
3972 *pbstrProperty = SysAllocStringLen(NULL, 0);
3973 hres = S_FALSE;
3976 if(!(*pbstrProperty))
3977 hres = E_OUTOFMEMORY;
3979 break;
3980 case Uri_PROPERTY_EXTENSION:
3981 if(This->extension_offset > -1) {
3982 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->path_start+This->extension_offset,
3983 This->path_len-This->extension_offset);
3984 hres = S_OK;
3985 } else {
3986 *pbstrProperty = SysAllocStringLen(NULL, 0);
3987 hres = S_FALSE;
3990 if(!(*pbstrProperty))
3991 hres = E_OUTOFMEMORY;
3993 break;
3994 case Uri_PROPERTY_FRAGMENT:
3995 if(This->fragment_start > -1) {
3996 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->fragment_start, This->fragment_len);
3997 hres = S_OK;
3998 } else {
3999 *pbstrProperty = SysAllocStringLen(NULL, 0);
4000 hres = S_FALSE;
4003 if(!(*pbstrProperty))
4004 hres = E_OUTOFMEMORY;
4006 break;
4007 case Uri_PROPERTY_HOST:
4008 if(This->host_start > -1) {
4009 /* The '[' and ']' aren't included for IPv6 addresses. */
4010 if(This->host_type == Uri_HOST_IPV6)
4011 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->host_start+1, This->host_len-2);
4012 else
4013 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->host_start, This->host_len);
4015 hres = S_OK;
4016 } else {
4017 *pbstrProperty = SysAllocStringLen(NULL, 0);
4018 hres = S_FALSE;
4021 if(!(*pbstrProperty))
4022 hres = E_OUTOFMEMORY;
4024 break;
4025 case Uri_PROPERTY_PASSWORD:
4026 if(This->userinfo_split > -1) {
4027 *pbstrProperty = SysAllocStringLen(
4028 This->canon_uri+This->userinfo_start+This->userinfo_split+1,
4029 This->userinfo_len-This->userinfo_split-1);
4030 hres = S_OK;
4031 } else {
4032 *pbstrProperty = SysAllocStringLen(NULL, 0);
4033 hres = S_FALSE;
4036 if(!(*pbstrProperty))
4037 return E_OUTOFMEMORY;
4039 break;
4040 case Uri_PROPERTY_PATH:
4041 if(This->path_start > -1) {
4042 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->path_start, This->path_len);
4043 hres = S_OK;
4044 } else {
4045 *pbstrProperty = SysAllocStringLen(NULL, 0);
4046 hres = S_FALSE;
4049 if(!(*pbstrProperty))
4050 hres = E_OUTOFMEMORY;
4052 break;
4053 case Uri_PROPERTY_PATH_AND_QUERY:
4054 if(This->path_start > -1) {
4055 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->path_start, This->path_len+This->query_len);
4056 hres = S_OK;
4057 } else if(This->query_start > -1) {
4058 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->query_start, This->query_len);
4059 hres = S_OK;
4060 } else {
4061 *pbstrProperty = SysAllocStringLen(NULL, 0);
4062 hres = S_FALSE;
4065 if(!(*pbstrProperty))
4066 hres = E_OUTOFMEMORY;
4068 break;
4069 case Uri_PROPERTY_QUERY:
4070 if(This->query_start > -1) {
4071 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->query_start, This->query_len);
4072 hres = S_OK;
4073 } else {
4074 *pbstrProperty = SysAllocStringLen(NULL, 0);
4075 hres = S_FALSE;
4078 if(!(*pbstrProperty))
4079 hres = E_OUTOFMEMORY;
4081 break;
4082 case Uri_PROPERTY_RAW_URI:
4083 *pbstrProperty = SysAllocString(This->raw_uri);
4084 if(!(*pbstrProperty))
4085 hres = E_OUTOFMEMORY;
4086 else
4087 hres = S_OK;
4088 break;
4089 case Uri_PROPERTY_SCHEME_NAME:
4090 if(This->scheme_start > -1) {
4091 *pbstrProperty = SysAllocStringLen(This->canon_uri + This->scheme_start, This->scheme_len);
4092 hres = S_OK;
4093 } else {
4094 *pbstrProperty = SysAllocStringLen(NULL, 0);
4095 hres = S_FALSE;
4098 if(!(*pbstrProperty))
4099 hres = E_OUTOFMEMORY;
4101 break;
4102 case Uri_PROPERTY_USER_INFO:
4103 if(This->userinfo_start > -1) {
4104 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->userinfo_start, This->userinfo_len);
4105 hres = S_OK;
4106 } else {
4107 *pbstrProperty = SysAllocStringLen(NULL, 0);
4108 hres = S_FALSE;
4111 if(!(*pbstrProperty))
4112 hres = E_OUTOFMEMORY;
4114 break;
4115 case Uri_PROPERTY_USER_NAME:
4116 if(This->userinfo_start > -1 && This->userinfo_split != 0) {
4117 /* If userinfo_split is set, that means a password exists
4118 * so the username is only from userinfo_start to userinfo_split.
4120 if(This->userinfo_split > -1) {
4121 *pbstrProperty = SysAllocStringLen(This->canon_uri + This->userinfo_start, This->userinfo_split);
4122 hres = S_OK;
4123 } else {
4124 *pbstrProperty = SysAllocStringLen(This->canon_uri + This->userinfo_start, This->userinfo_len);
4125 hres = S_OK;
4127 } else {
4128 *pbstrProperty = SysAllocStringLen(NULL, 0);
4129 hres = S_FALSE;
4132 if(!(*pbstrProperty))
4133 return E_OUTOFMEMORY;
4135 break;
4136 default:
4137 FIXME("(%p)->(%d %p %lx)\n", This, uriProp, pbstrProperty, dwFlags);
4138 hres = E_NOTIMPL;
4141 return hres;
4144 static HRESULT WINAPI Uri_GetPropertyLength(IUri *iface, Uri_PROPERTY uriProp, DWORD *pcchProperty, DWORD dwFlags)
4146 Uri *This = impl_from_IUri(iface);
4147 HRESULT hres;
4148 TRACE("(%p %s)->(%d %p %lx)\n", This, debugstr_w(This->canon_uri), uriProp, pcchProperty, dwFlags);
4150 if(!This->create_flags)
4151 return E_UNEXPECTED;
4152 if(!pcchProperty)
4153 return E_INVALIDARG;
4155 /* Can only return a length for a property if it's a string. */
4156 if(uriProp > Uri_PROPERTY_STRING_LAST)
4157 return E_INVALIDARG;
4159 /* Don't have support for flags yet. */
4160 if(dwFlags) {
4161 FIXME("(%p)->(%d %p %lx)\n", This, uriProp, pcchProperty, dwFlags);
4162 return E_NOTIMPL;
4165 switch(uriProp) {
4166 case Uri_PROPERTY_ABSOLUTE_URI:
4167 if(This->display_modifiers & URI_DISPLAY_NO_ABSOLUTE_URI) {
4168 *pcchProperty = 0;
4169 hres = S_FALSE;
4170 } else {
4171 if(This->scheme_type != URL_SCHEME_UNKNOWN) {
4172 if(This->userinfo_start > -1 && This->userinfo_len == 0)
4173 /* Don't include the '@' in the length. */
4174 *pcchProperty = This->canon_len-1;
4175 else if(This->userinfo_start > -1 && This->userinfo_len == 1 &&
4176 This->userinfo_split == 0)
4177 /* Don't include the ":@" in the length. */
4178 *pcchProperty = This->canon_len-2;
4179 else
4180 *pcchProperty = This->canon_len;
4181 } else
4182 *pcchProperty = This->canon_len;
4184 hres = S_OK;
4187 break;
4188 case Uri_PROPERTY_AUTHORITY:
4189 if(This->port_offset > -1 &&
4190 This->display_modifiers & URI_DISPLAY_NO_DEFAULT_PORT_AUTH &&
4191 is_default_port(This->scheme_type, This->port))
4192 /* Only count up until the port in the authority. */
4193 *pcchProperty = This->port_offset;
4194 else
4195 *pcchProperty = This->authority_len;
4196 hres = (This->authority_start > -1) ? S_OK : S_FALSE;
4197 break;
4198 case Uri_PROPERTY_DISPLAY_URI:
4199 if(This->scheme_type != URL_SCHEME_UNKNOWN && This->userinfo_start > -1)
4200 *pcchProperty = This->canon_len-This->userinfo_len-1;
4201 else
4202 *pcchProperty = This->canon_len;
4204 hres = S_OK;
4205 break;
4206 case Uri_PROPERTY_DOMAIN:
4207 if(This->domain_offset > -1)
4208 *pcchProperty = This->host_len - This->domain_offset;
4209 else
4210 *pcchProperty = 0;
4212 hres = (This->domain_offset > -1) ? S_OK : S_FALSE;
4213 break;
4214 case Uri_PROPERTY_EXTENSION:
4215 if(This->extension_offset > -1) {
4216 *pcchProperty = This->path_len - This->extension_offset;
4217 hres = S_OK;
4218 } else {
4219 *pcchProperty = 0;
4220 hres = S_FALSE;
4223 break;
4224 case Uri_PROPERTY_FRAGMENT:
4225 *pcchProperty = This->fragment_len;
4226 hres = (This->fragment_start > -1) ? S_OK : S_FALSE;
4227 break;
4228 case Uri_PROPERTY_HOST:
4229 *pcchProperty = This->host_len;
4231 /* '[' and ']' aren't included in the length. */
4232 if(This->host_type == Uri_HOST_IPV6)
4233 *pcchProperty -= 2;
4235 hres = (This->host_start > -1) ? S_OK : S_FALSE;
4236 break;
4237 case Uri_PROPERTY_PASSWORD:
4238 *pcchProperty = (This->userinfo_split > -1) ? This->userinfo_len-This->userinfo_split-1 : 0;
4239 hres = (This->userinfo_split > -1) ? S_OK : S_FALSE;
4240 break;
4241 case Uri_PROPERTY_PATH:
4242 *pcchProperty = This->path_len;
4243 hres = (This->path_start > -1) ? S_OK : S_FALSE;
4244 break;
4245 case Uri_PROPERTY_PATH_AND_QUERY:
4246 *pcchProperty = This->path_len+This->query_len;
4247 hres = (This->path_start > -1 || This->query_start > -1) ? S_OK : S_FALSE;
4248 break;
4249 case Uri_PROPERTY_QUERY:
4250 *pcchProperty = This->query_len;
4251 hres = (This->query_start > -1) ? S_OK : S_FALSE;
4252 break;
4253 case Uri_PROPERTY_RAW_URI:
4254 *pcchProperty = SysStringLen(This->raw_uri);
4255 hres = S_OK;
4256 break;
4257 case Uri_PROPERTY_SCHEME_NAME:
4258 *pcchProperty = This->scheme_len;
4259 hres = (This->scheme_start > -1) ? S_OK : S_FALSE;
4260 break;
4261 case Uri_PROPERTY_USER_INFO:
4262 *pcchProperty = This->userinfo_len;
4263 hres = (This->userinfo_start > -1) ? S_OK : S_FALSE;
4264 break;
4265 case Uri_PROPERTY_USER_NAME:
4266 *pcchProperty = (This->userinfo_split > -1) ? This->userinfo_split : This->userinfo_len;
4267 if(This->userinfo_split == 0)
4268 hres = S_FALSE;
4269 else
4270 hres = (This->userinfo_start > -1) ? S_OK : S_FALSE;
4271 break;
4272 default:
4273 FIXME("(%p)->(%d %p %lx)\n", This, uriProp, pcchProperty, dwFlags);
4274 hres = E_NOTIMPL;
4277 return hres;
4280 static HRESULT WINAPI Uri_GetPropertyDWORD(IUri *iface, Uri_PROPERTY uriProp, DWORD *pcchProperty, DWORD dwFlags)
4282 Uri *This = impl_from_IUri(iface);
4283 HRESULT hres;
4285 TRACE("(%p %s)->(%d %p %lx)\n", This, debugstr_w(This->canon_uri), uriProp, pcchProperty, dwFlags);
4287 if(!This->create_flags)
4288 return E_UNEXPECTED;
4289 if(!pcchProperty)
4290 return E_INVALIDARG;
4292 /* Microsoft's implementation for the ZONE property of a URI seems to be lacking...
4293 * From what I can tell, instead of checking which URLZONE the URI belongs to it
4294 * simply assigns URLZONE_INVALID and returns E_NOTIMPL. This also applies to the GetZone
4295 * function.
4297 if(uriProp == Uri_PROPERTY_ZONE) {
4298 *pcchProperty = URLZONE_INVALID;
4299 return E_NOTIMPL;
4302 if(uriProp < Uri_PROPERTY_DWORD_START) {
4303 *pcchProperty = 0;
4304 return E_INVALIDARG;
4307 switch(uriProp) {
4308 case Uri_PROPERTY_HOST_TYPE:
4309 *pcchProperty = This->host_type;
4310 hres = S_OK;
4311 break;
4312 case Uri_PROPERTY_PORT:
4313 if(!This->has_port) {
4314 *pcchProperty = 0;
4315 hres = S_FALSE;
4316 } else {
4317 *pcchProperty = This->port;
4318 hres = S_OK;
4321 break;
4322 case Uri_PROPERTY_SCHEME:
4323 *pcchProperty = This->scheme_type;
4324 hres = S_OK;
4325 break;
4326 default:
4327 FIXME("(%p)->(%d %p %lx)\n", This, uriProp, pcchProperty, dwFlags);
4328 hres = E_NOTIMPL;
4331 return hres;
4334 static HRESULT WINAPI Uri_HasProperty(IUri *iface, Uri_PROPERTY uriProp, BOOL *pfHasProperty)
4336 Uri *This = impl_from_IUri(iface);
4338 TRACE("(%p %s)->(%d %p)\n", This, debugstr_w(This->canon_uri), uriProp, pfHasProperty);
4340 if(!pfHasProperty)
4341 return E_INVALIDARG;
4343 switch(uriProp) {
4344 case Uri_PROPERTY_ABSOLUTE_URI:
4345 *pfHasProperty = !(This->display_modifiers & URI_DISPLAY_NO_ABSOLUTE_URI);
4346 break;
4347 case Uri_PROPERTY_AUTHORITY:
4348 *pfHasProperty = This->authority_start > -1;
4349 break;
4350 case Uri_PROPERTY_DISPLAY_URI:
4351 *pfHasProperty = TRUE;
4352 break;
4353 case Uri_PROPERTY_DOMAIN:
4354 *pfHasProperty = This->domain_offset > -1;
4355 break;
4356 case Uri_PROPERTY_EXTENSION:
4357 *pfHasProperty = This->extension_offset > -1;
4358 break;
4359 case Uri_PROPERTY_FRAGMENT:
4360 *pfHasProperty = This->fragment_start > -1;
4361 break;
4362 case Uri_PROPERTY_HOST:
4363 *pfHasProperty = This->host_start > -1;
4364 break;
4365 case Uri_PROPERTY_PASSWORD:
4366 *pfHasProperty = This->userinfo_split > -1;
4367 break;
4368 case Uri_PROPERTY_PATH:
4369 *pfHasProperty = This->path_start > -1;
4370 break;
4371 case Uri_PROPERTY_PATH_AND_QUERY:
4372 *pfHasProperty = (This->path_start > -1 || This->query_start > -1);
4373 break;
4374 case Uri_PROPERTY_QUERY:
4375 *pfHasProperty = This->query_start > -1;
4376 break;
4377 case Uri_PROPERTY_RAW_URI:
4378 *pfHasProperty = TRUE;
4379 break;
4380 case Uri_PROPERTY_SCHEME_NAME:
4381 *pfHasProperty = This->scheme_start > -1;
4382 break;
4383 case Uri_PROPERTY_USER_INFO:
4384 *pfHasProperty = This->userinfo_start > -1;
4385 break;
4386 case Uri_PROPERTY_USER_NAME:
4387 if(This->userinfo_split == 0)
4388 *pfHasProperty = FALSE;
4389 else
4390 *pfHasProperty = This->userinfo_start > -1;
4391 break;
4392 case Uri_PROPERTY_HOST_TYPE:
4393 *pfHasProperty = TRUE;
4394 break;
4395 case Uri_PROPERTY_PORT:
4396 *pfHasProperty = This->has_port;
4397 break;
4398 case Uri_PROPERTY_SCHEME:
4399 *pfHasProperty = TRUE;
4400 break;
4401 case Uri_PROPERTY_ZONE:
4402 *pfHasProperty = FALSE;
4403 break;
4404 default:
4405 FIXME("(%p)->(%d %p): Unsupported property type.\n", This, uriProp, pfHasProperty);
4406 return E_NOTIMPL;
4409 return S_OK;
4412 static HRESULT WINAPI Uri_GetAbsoluteUri(IUri *iface, BSTR *pstrAbsoluteUri)
4414 TRACE("(%p)->(%p)\n", iface, pstrAbsoluteUri);
4415 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_ABSOLUTE_URI, pstrAbsoluteUri, 0);
4418 static HRESULT WINAPI Uri_GetAuthority(IUri *iface, BSTR *pstrAuthority)
4420 TRACE("(%p)->(%p)\n", iface, pstrAuthority);
4421 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_AUTHORITY, pstrAuthority, 0);
4424 static HRESULT WINAPI Uri_GetDisplayUri(IUri *iface, BSTR *pstrDisplayUri)
4426 TRACE("(%p)->(%p)\n", iface, pstrDisplayUri);
4427 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_DISPLAY_URI, pstrDisplayUri, 0);
4430 static HRESULT WINAPI Uri_GetDomain(IUri *iface, BSTR *pstrDomain)
4432 TRACE("(%p)->(%p)\n", iface, pstrDomain);
4433 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_DOMAIN, pstrDomain, 0);
4436 static HRESULT WINAPI Uri_GetExtension(IUri *iface, BSTR *pstrExtension)
4438 TRACE("(%p)->(%p)\n", iface, pstrExtension);
4439 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_EXTENSION, pstrExtension, 0);
4442 static HRESULT WINAPI Uri_GetFragment(IUri *iface, BSTR *pstrFragment)
4444 TRACE("(%p)->(%p)\n", iface, pstrFragment);
4445 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_FRAGMENT, pstrFragment, 0);
4448 static HRESULT WINAPI Uri_GetHost(IUri *iface, BSTR *pstrHost)
4450 TRACE("(%p)->(%p)\n", iface, pstrHost);
4451 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_HOST, pstrHost, 0);
4454 static HRESULT WINAPI Uri_GetPassword(IUri *iface, BSTR *pstrPassword)
4456 TRACE("(%p)->(%p)\n", iface, pstrPassword);
4457 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_PASSWORD, pstrPassword, 0);
4460 static HRESULT WINAPI Uri_GetPath(IUri *iface, BSTR *pstrPath)
4462 TRACE("(%p)->(%p)\n", iface, pstrPath);
4463 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_PATH, pstrPath, 0);
4466 static HRESULT WINAPI Uri_GetPathAndQuery(IUri *iface, BSTR *pstrPathAndQuery)
4468 TRACE("(%p)->(%p)\n", iface, pstrPathAndQuery);
4469 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_PATH_AND_QUERY, pstrPathAndQuery, 0);
4472 static HRESULT WINAPI Uri_GetQuery(IUri *iface, BSTR *pstrQuery)
4474 TRACE("(%p)->(%p)\n", iface, pstrQuery);
4475 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_QUERY, pstrQuery, 0);
4478 static HRESULT WINAPI Uri_GetRawUri(IUri *iface, BSTR *pstrRawUri)
4480 TRACE("(%p)->(%p)\n", iface, pstrRawUri);
4481 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_RAW_URI, pstrRawUri, 0);
4484 static HRESULT WINAPI Uri_GetSchemeName(IUri *iface, BSTR *pstrSchemeName)
4486 TRACE("(%p)->(%p)\n", iface, pstrSchemeName);
4487 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_SCHEME_NAME, pstrSchemeName, 0);
4490 static HRESULT WINAPI Uri_GetUserInfo(IUri *iface, BSTR *pstrUserInfo)
4492 TRACE("(%p)->(%p)\n", iface, pstrUserInfo);
4493 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_USER_INFO, pstrUserInfo, 0);
4496 static HRESULT WINAPI Uri_GetUserName(IUri *iface, BSTR *pstrUserName)
4498 TRACE("(%p)->(%p)\n", iface, pstrUserName);
4499 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_USER_NAME, pstrUserName, 0);
4502 static HRESULT WINAPI Uri_GetHostType(IUri *iface, DWORD *pdwHostType)
4504 TRACE("(%p)->(%p)\n", iface, pdwHostType);
4505 return IUri_GetPropertyDWORD(iface, Uri_PROPERTY_HOST_TYPE, pdwHostType, 0);
4508 static HRESULT WINAPI Uri_GetPort(IUri *iface, DWORD *pdwPort)
4510 TRACE("(%p)->(%p)\n", iface, pdwPort);
4511 return IUri_GetPropertyDWORD(iface, Uri_PROPERTY_PORT, pdwPort, 0);
4514 static HRESULT WINAPI Uri_GetScheme(IUri *iface, DWORD *pdwScheme)
4516 TRACE("(%p)->(%p)\n", iface, pdwScheme);
4517 return IUri_GetPropertyDWORD(iface, Uri_PROPERTY_SCHEME, pdwScheme, 0);
4520 static HRESULT WINAPI Uri_GetZone(IUri *iface, DWORD *pdwZone)
4522 TRACE("(%p)->(%p)\n", iface, pdwZone);
4523 return IUri_GetPropertyDWORD(iface, Uri_PROPERTY_ZONE,pdwZone, 0);
4526 static HRESULT WINAPI Uri_GetProperties(IUri *iface, DWORD *pdwProperties)
4528 Uri *This = impl_from_IUri(iface);
4529 TRACE("(%p %s)->(%p)\n", This, debugstr_w(This->canon_uri), pdwProperties);
4531 if(!This->create_flags)
4532 return E_UNEXPECTED;
4533 if(!pdwProperties)
4534 return E_INVALIDARG;
4536 /* All URIs have these. */
4537 *pdwProperties = Uri_HAS_DISPLAY_URI|Uri_HAS_RAW_URI|Uri_HAS_SCHEME|Uri_HAS_HOST_TYPE;
4539 if(!(This->display_modifiers & URI_DISPLAY_NO_ABSOLUTE_URI))
4540 *pdwProperties |= Uri_HAS_ABSOLUTE_URI;
4542 if(This->scheme_start > -1)
4543 *pdwProperties |= Uri_HAS_SCHEME_NAME;
4545 if(This->authority_start > -1) {
4546 *pdwProperties |= Uri_HAS_AUTHORITY;
4547 if(This->userinfo_start > -1) {
4548 *pdwProperties |= Uri_HAS_USER_INFO;
4549 if(This->userinfo_split != 0)
4550 *pdwProperties |= Uri_HAS_USER_NAME;
4552 if(This->userinfo_split > -1)
4553 *pdwProperties |= Uri_HAS_PASSWORD;
4554 if(This->host_start > -1)
4555 *pdwProperties |= Uri_HAS_HOST;
4556 if(This->domain_offset > -1)
4557 *pdwProperties |= Uri_HAS_DOMAIN;
4560 if(This->has_port)
4561 *pdwProperties |= Uri_HAS_PORT;
4562 if(This->path_start > -1)
4563 *pdwProperties |= Uri_HAS_PATH|Uri_HAS_PATH_AND_QUERY;
4564 if(This->query_start > -1)
4565 *pdwProperties |= Uri_HAS_QUERY|Uri_HAS_PATH_AND_QUERY;
4567 if(This->extension_offset > -1)
4568 *pdwProperties |= Uri_HAS_EXTENSION;
4570 if(This->fragment_start > -1)
4571 *pdwProperties |= Uri_HAS_FRAGMENT;
4573 return S_OK;
4576 static HRESULT WINAPI Uri_IsEqual(IUri *iface, IUri *pUri, BOOL *pfEqual)
4578 Uri *This = impl_from_IUri(iface);
4579 Uri *other;
4581 TRACE("(%p %s)->(%p %p)\n", This, debugstr_w(This->canon_uri), pUri, pfEqual);
4583 if(!This->create_flags)
4584 return E_UNEXPECTED;
4585 if(!pfEqual)
4586 return E_POINTER;
4588 if(!pUri) {
4589 *pfEqual = FALSE;
4591 /* For some reason Windows returns S_OK here... */
4592 return S_OK;
4595 /* Try to convert it to a Uri (allows for a more simple comparison). */
4596 if(!(other = get_uri_obj(pUri))) {
4597 FIXME("(%p)->(%p %p) No support for unknown IUri's yet.\n", iface, pUri, pfEqual);
4598 return E_NOTIMPL;
4601 TRACE("comparing to %s\n", debugstr_w(other->canon_uri));
4602 return compare_uris(This, other, pfEqual);
4605 static const IUriVtbl UriVtbl = {
4606 Uri_QueryInterface,
4607 Uri_AddRef,
4608 Uri_Release,
4609 Uri_GetPropertyBSTR,
4610 Uri_GetPropertyLength,
4611 Uri_GetPropertyDWORD,
4612 Uri_HasProperty,
4613 Uri_GetAbsoluteUri,
4614 Uri_GetAuthority,
4615 Uri_GetDisplayUri,
4616 Uri_GetDomain,
4617 Uri_GetExtension,
4618 Uri_GetFragment,
4619 Uri_GetHost,
4620 Uri_GetPassword,
4621 Uri_GetPath,
4622 Uri_GetPathAndQuery,
4623 Uri_GetQuery,
4624 Uri_GetRawUri,
4625 Uri_GetSchemeName,
4626 Uri_GetUserInfo,
4627 Uri_GetUserName,
4628 Uri_GetHostType,
4629 Uri_GetPort,
4630 Uri_GetScheme,
4631 Uri_GetZone,
4632 Uri_GetProperties,
4633 Uri_IsEqual
4636 static inline Uri* impl_from_IUriBuilderFactory(IUriBuilderFactory *iface)
4638 return CONTAINING_RECORD(iface, Uri, IUriBuilderFactory_iface);
4641 static HRESULT WINAPI UriBuilderFactory_QueryInterface(IUriBuilderFactory *iface, REFIID riid, void **ppv)
4643 Uri *This = impl_from_IUriBuilderFactory(iface);
4644 return IUri_QueryInterface(&This->IUri_iface, riid, ppv);
4647 static ULONG WINAPI UriBuilderFactory_AddRef(IUriBuilderFactory *iface)
4649 Uri *This = impl_from_IUriBuilderFactory(iface);
4650 return IUri_AddRef(&This->IUri_iface);
4653 static ULONG WINAPI UriBuilderFactory_Release(IUriBuilderFactory *iface)
4655 Uri *This = impl_from_IUriBuilderFactory(iface);
4656 return IUri_Release(&This->IUri_iface);
4659 static HRESULT WINAPI UriBuilderFactory_CreateIUriBuilder(IUriBuilderFactory *iface,
4660 DWORD dwFlags,
4661 DWORD_PTR dwReserved,
4662 IUriBuilder **ppIUriBuilder)
4664 Uri *This = impl_from_IUriBuilderFactory(iface);
4665 TRACE("(%p)->(%08lx %08Ix %p)\n", This, dwFlags, dwReserved, ppIUriBuilder);
4667 if(!ppIUriBuilder)
4668 return E_POINTER;
4670 if(dwFlags || dwReserved) {
4671 *ppIUriBuilder = NULL;
4672 return E_INVALIDARG;
4675 return CreateIUriBuilder(NULL, 0, 0, ppIUriBuilder);
4678 static HRESULT WINAPI UriBuilderFactory_CreateInitializedIUriBuilder(IUriBuilderFactory *iface,
4679 DWORD dwFlags,
4680 DWORD_PTR dwReserved,
4681 IUriBuilder **ppIUriBuilder)
4683 Uri *This = impl_from_IUriBuilderFactory(iface);
4684 TRACE("(%p)->(%08lx %08Ix %p)\n", This, dwFlags, dwReserved, ppIUriBuilder);
4686 if(!ppIUriBuilder)
4687 return E_POINTER;
4689 if(dwFlags || dwReserved) {
4690 *ppIUriBuilder = NULL;
4691 return E_INVALIDARG;
4694 return CreateIUriBuilder(&This->IUri_iface, 0, 0, ppIUriBuilder);
4697 static const IUriBuilderFactoryVtbl UriBuilderFactoryVtbl = {
4698 UriBuilderFactory_QueryInterface,
4699 UriBuilderFactory_AddRef,
4700 UriBuilderFactory_Release,
4701 UriBuilderFactory_CreateIUriBuilder,
4702 UriBuilderFactory_CreateInitializedIUriBuilder
4705 static inline Uri* impl_from_IPersistStream(IPersistStream *iface)
4707 return CONTAINING_RECORD(iface, Uri, IPersistStream_iface);
4710 static HRESULT WINAPI PersistStream_QueryInterface(IPersistStream *iface, REFIID riid, void **ppvObject)
4712 Uri *This = impl_from_IPersistStream(iface);
4713 return IUri_QueryInterface(&This->IUri_iface, riid, ppvObject);
4716 static ULONG WINAPI PersistStream_AddRef(IPersistStream *iface)
4718 Uri *This = impl_from_IPersistStream(iface);
4719 return IUri_AddRef(&This->IUri_iface);
4722 static ULONG WINAPI PersistStream_Release(IPersistStream *iface)
4724 Uri *This = impl_from_IPersistStream(iface);
4725 return IUri_Release(&This->IUri_iface);
4728 static HRESULT WINAPI PersistStream_GetClassID(IPersistStream *iface, CLSID *pClassID)
4730 Uri *This = impl_from_IPersistStream(iface);
4731 TRACE("(%p)->(%p)\n", This, pClassID);
4733 if(!pClassID)
4734 return E_INVALIDARG;
4736 *pClassID = CLSID_CUri;
4737 return S_OK;
4740 static HRESULT WINAPI PersistStream_IsDirty(IPersistStream *iface)
4742 Uri *This = impl_from_IPersistStream(iface);
4743 TRACE("(%p)\n", This);
4744 return S_FALSE;
4747 struct persist_uri {
4748 DWORD size;
4749 DWORD unk1[2];
4750 DWORD create_flags;
4751 DWORD unk2[3];
4752 DWORD fields_no;
4753 BYTE data[1];
4756 static HRESULT WINAPI PersistStream_Load(IPersistStream *iface, IStream *pStm)
4758 Uri *This = impl_from_IPersistStream(iface);
4759 struct persist_uri *data;
4760 parse_data parse;
4761 DWORD size;
4762 HRESULT hr;
4764 TRACE("(%p)->(%p)\n", This, pStm);
4766 if(This->create_flags)
4767 return E_UNEXPECTED;
4768 if(!pStm)
4769 return E_INVALIDARG;
4771 hr = IStream_Read(pStm, &size, sizeof(DWORD), NULL);
4772 if(FAILED(hr))
4773 return hr;
4774 data = malloc(size);
4775 if(!data)
4776 return E_OUTOFMEMORY;
4777 hr = IStream_Read(pStm, data->unk1, size-sizeof(DWORD)-2, NULL);
4778 if(FAILED(hr)) {
4779 free(data);
4780 return hr;
4783 if(size < sizeof(struct persist_uri)) {
4784 free(data);
4785 return S_OK;
4788 if(*(DWORD*)data->data != Uri_PROPERTY_RAW_URI) {
4789 free(data);
4790 ERR("Can't find raw_uri\n");
4791 return E_UNEXPECTED;
4794 This->raw_uri = SysAllocString((WCHAR*)(data->data+sizeof(DWORD)*2));
4795 if(!This->raw_uri) {
4796 free(data);
4797 return E_OUTOFMEMORY;
4799 This->create_flags = data->create_flags;
4800 free(data);
4801 TRACE("%lx %s\n", This->create_flags, debugstr_w(This->raw_uri));
4803 memset(&parse, 0, sizeof(parse_data));
4804 parse.uri = This->raw_uri;
4805 if(!parse_uri(&parse, This->create_flags)) {
4806 SysFreeString(This->raw_uri);
4807 This->create_flags = 0;
4808 return E_UNEXPECTED;
4811 hr = canonicalize_uri(&parse, This, This->create_flags);
4812 if(FAILED(hr)) {
4813 SysFreeString(This->raw_uri);
4814 This->create_flags = 0;
4815 return hr;
4818 return S_OK;
4821 static inline BYTE* persist_stream_add_strprop(Uri *This, BYTE *p, DWORD type, DWORD len, WCHAR *data)
4823 len *= sizeof(WCHAR);
4824 *(DWORD*)p = type;
4825 p += sizeof(DWORD);
4826 *(DWORD*)p = len+sizeof(WCHAR);
4827 p += sizeof(DWORD);
4828 memcpy(p, data, len);
4829 p += len;
4830 *(WCHAR*)p = 0;
4831 return p+sizeof(WCHAR);
4834 static inline void persist_stream_save(Uri *This, IStream *pStm, BOOL marshal, struct persist_uri *data)
4836 BYTE *p = NULL;
4838 data->create_flags = This->create_flags;
4840 if(This->create_flags) {
4841 data->fields_no = 1;
4842 p = persist_stream_add_strprop(This, data->data, Uri_PROPERTY_RAW_URI,
4843 SysStringLen(This->raw_uri), This->raw_uri);
4845 if(This->scheme_type!=URL_SCHEME_HTTP && This->scheme_type!=URL_SCHEME_HTTPS
4846 && This->scheme_type!=URL_SCHEME_FTP)
4847 return;
4849 if(This->fragment_len) {
4850 data->fields_no++;
4851 p = persist_stream_add_strprop(This, p, Uri_PROPERTY_FRAGMENT,
4852 This->fragment_len, This->canon_uri+This->fragment_start);
4855 if(This->host_len) {
4856 data->fields_no++;
4857 if(This->host_type == Uri_HOST_IPV6)
4858 p = persist_stream_add_strprop(This, p, Uri_PROPERTY_HOST,
4859 This->host_len-2, This->canon_uri+This->host_start+1);
4860 else
4861 p = persist_stream_add_strprop(This, p, Uri_PROPERTY_HOST,
4862 This->host_len, This->canon_uri+This->host_start);
4865 if(This->userinfo_split > -1) {
4866 data->fields_no++;
4867 p = persist_stream_add_strprop(This, p, Uri_PROPERTY_PASSWORD,
4868 This->userinfo_len-This->userinfo_split-1,
4869 This->canon_uri+This->userinfo_start+This->userinfo_split+1);
4872 if(This->path_len) {
4873 data->fields_no++;
4874 p = persist_stream_add_strprop(This, p, Uri_PROPERTY_PATH,
4875 This->path_len, This->canon_uri+This->path_start);
4876 } else if(marshal) {
4877 WCHAR no_path = '/';
4878 data->fields_no++;
4879 p = persist_stream_add_strprop(This, p, Uri_PROPERTY_PATH, 1, &no_path);
4882 if(This->has_port) {
4883 data->fields_no++;
4884 *(DWORD*)p = Uri_PROPERTY_PORT;
4885 p += sizeof(DWORD);
4886 *(DWORD*)p = sizeof(DWORD);
4887 p += sizeof(DWORD);
4888 *(DWORD*)p = This->port;
4889 p += sizeof(DWORD);
4892 if(This->query_len) {
4893 data->fields_no++;
4894 p = persist_stream_add_strprop(This, p, Uri_PROPERTY_QUERY,
4895 This->query_len, This->canon_uri+This->query_start);
4898 if(This->scheme_len) {
4899 data->fields_no++;
4900 p = persist_stream_add_strprop(This, p, Uri_PROPERTY_SCHEME_NAME,
4901 This->scheme_len, This->canon_uri+This->scheme_start);
4904 if(This->userinfo_start>-1 && This->userinfo_split!=0) {
4905 data->fields_no++;
4906 if(This->userinfo_split > -1)
4907 p = persist_stream_add_strprop(This, p, Uri_PROPERTY_USER_NAME,
4908 This->userinfo_split, This->canon_uri+This->userinfo_start);
4909 else
4910 p = persist_stream_add_strprop(This, p, Uri_PROPERTY_USER_NAME,
4911 This->userinfo_len, This->canon_uri+This->userinfo_start);
4915 static HRESULT WINAPI PersistStream_Save(IPersistStream *iface, IStream *pStm, BOOL fClearDirty)
4917 Uri *This = impl_from_IPersistStream(iface);
4918 struct persist_uri *data;
4919 ULARGE_INTEGER size;
4920 HRESULT hres;
4922 TRACE("(%p)->(%p %x)\n", This, pStm, fClearDirty);
4924 if(!pStm)
4925 return E_INVALIDARG;
4927 hres = IPersistStream_GetSizeMax(&This->IPersistStream_iface, &size);
4928 if(FAILED(hres))
4929 return hres;
4931 data = calloc(1, size.u.LowPart);
4932 if(!data)
4933 return E_OUTOFMEMORY;
4934 data->size = size.u.LowPart;
4935 persist_stream_save(This, pStm, FALSE, data);
4937 hres = IStream_Write(pStm, data, data->size-2, NULL);
4938 free(data);
4939 return hres;
4942 static HRESULT WINAPI PersistStream_GetSizeMax(IPersistStream *iface, ULARGE_INTEGER *pcbSize)
4944 Uri *This = impl_from_IPersistStream(iface);
4945 TRACE("(%p)->(%p)\n", This, pcbSize);
4947 if(!pcbSize)
4948 return E_INVALIDARG;
4950 pcbSize->u.LowPart = 2+sizeof(struct persist_uri);
4951 pcbSize->u.HighPart = 0;
4952 if(This->create_flags)
4953 pcbSize->u.LowPart += (SysStringLen(This->raw_uri)+1)*sizeof(WCHAR) + 2*sizeof(DWORD);
4954 else /* there's no place for fields no */
4955 pcbSize->u.LowPart -= sizeof(DWORD);
4956 if(This->scheme_type!=URL_SCHEME_HTTP && This->scheme_type!=URL_SCHEME_HTTPS
4957 && This->scheme_type!=URL_SCHEME_FTP)
4958 return S_OK;
4960 if(This->fragment_len)
4961 pcbSize->u.LowPart += (This->fragment_len+1)*sizeof(WCHAR) + 2*sizeof(DWORD);
4962 if(This->host_len) {
4963 if(This->host_type == Uri_HOST_IPV6)
4964 pcbSize->u.LowPart += (This->host_len-1)*sizeof(WCHAR) + 2*sizeof(DWORD);
4965 else
4966 pcbSize->u.LowPart += (This->host_len+1)*sizeof(WCHAR) + 2*sizeof(DWORD);
4968 if(This->userinfo_split > -1)
4969 pcbSize->u.LowPart += (This->userinfo_len-This->userinfo_split)*sizeof(WCHAR) + 2*sizeof(DWORD);
4970 if(This->path_len)
4971 pcbSize->u.LowPart += (This->path_len+1)*sizeof(WCHAR) + 2*sizeof(DWORD);
4972 if(This->has_port)
4973 pcbSize->u.LowPart += 3*sizeof(DWORD);
4974 if(This->query_len)
4975 pcbSize->u.LowPart += (This->query_len+1)*sizeof(WCHAR) + 2*sizeof(DWORD);
4976 if(This->scheme_len)
4977 pcbSize->u.LowPart += (This->scheme_len+1)*sizeof(WCHAR) + 2*sizeof(DWORD);
4978 if(This->userinfo_start>-1 && This->userinfo_split!=0) {
4979 if(This->userinfo_split > -1)
4980 pcbSize->u.LowPart += (This->userinfo_split+1)*sizeof(WCHAR) + 2*sizeof(DWORD);
4981 else
4982 pcbSize->u.LowPart += (This->userinfo_len+1)*sizeof(WCHAR) + 2*sizeof(DWORD);
4984 return S_OK;
4987 static const IPersistStreamVtbl PersistStreamVtbl = {
4988 PersistStream_QueryInterface,
4989 PersistStream_AddRef,
4990 PersistStream_Release,
4991 PersistStream_GetClassID,
4992 PersistStream_IsDirty,
4993 PersistStream_Load,
4994 PersistStream_Save,
4995 PersistStream_GetSizeMax
4998 static inline Uri* impl_from_IMarshal(IMarshal *iface)
5000 return CONTAINING_RECORD(iface, Uri, IMarshal_iface);
5003 static HRESULT WINAPI Marshal_QueryInterface(IMarshal *iface, REFIID riid, void **ppvObject)
5005 Uri *This = impl_from_IMarshal(iface);
5006 return IUri_QueryInterface(&This->IUri_iface, riid, ppvObject);
5009 static ULONG WINAPI Marshal_AddRef(IMarshal *iface)
5011 Uri *This = impl_from_IMarshal(iface);
5012 return IUri_AddRef(&This->IUri_iface);
5015 static ULONG WINAPI Marshal_Release(IMarshal *iface)
5017 Uri *This = impl_from_IMarshal(iface);
5018 return IUri_Release(&This->IUri_iface);
5021 static HRESULT WINAPI Marshal_GetUnmarshalClass(IMarshal *iface, REFIID riid, void *pv,
5022 DWORD dwDestContext, void *pvDestContext, DWORD mshlflags, CLSID *pCid)
5024 Uri *This = impl_from_IMarshal(iface);
5025 TRACE("(%p)->(%s %p %lx %p %lx %p)\n", This, debugstr_guid(riid), pv,
5026 dwDestContext, pvDestContext, mshlflags, pCid);
5028 if(!pCid || (dwDestContext!=MSHCTX_LOCAL && dwDestContext!=MSHCTX_NOSHAREDMEM
5029 && dwDestContext!=MSHCTX_INPROC))
5030 return E_INVALIDARG;
5032 *pCid = CLSID_CUri;
5033 return S_OK;
5036 struct inproc_marshal_uri {
5037 DWORD size;
5038 DWORD mshlflags;
5039 DWORD unk[4]; /* process identifier? */
5040 Uri *uri;
5043 static HRESULT WINAPI Marshal_GetMarshalSizeMax(IMarshal *iface, REFIID riid, void *pv,
5044 DWORD dwDestContext, void *pvDestContext, DWORD mshlflags, DWORD *pSize)
5046 Uri *This = impl_from_IMarshal(iface);
5047 ULARGE_INTEGER size;
5048 HRESULT hres;
5049 TRACE("(%p)->(%s %p %lx %p %lx %p)\n", This, debugstr_guid(riid), pv,
5050 dwDestContext, pvDestContext, mshlflags, pSize);
5052 if(!pSize || (dwDestContext!=MSHCTX_LOCAL && dwDestContext!=MSHCTX_NOSHAREDMEM
5053 && dwDestContext!=MSHCTX_INPROC))
5054 return E_INVALIDARG;
5056 if(dwDestContext == MSHCTX_INPROC) {
5057 *pSize = sizeof(struct inproc_marshal_uri);
5058 return S_OK;
5061 hres = IPersistStream_GetSizeMax(&This->IPersistStream_iface, &size);
5062 if(FAILED(hres))
5063 return hres;
5064 if(!This->path_len && (This->scheme_type==URL_SCHEME_HTTP
5065 || This->scheme_type==URL_SCHEME_HTTPS
5066 || This->scheme_type==URL_SCHEME_FTP))
5067 size.u.LowPart += 3*sizeof(DWORD);
5068 *pSize = size.u.LowPart+2*sizeof(DWORD);
5069 return S_OK;
5072 static HRESULT WINAPI Marshal_MarshalInterface(IMarshal *iface, IStream *pStm, REFIID riid,
5073 void *pv, DWORD dwDestContext, void *pvDestContext, DWORD mshlflags)
5075 Uri *This = impl_from_IMarshal(iface);
5076 DWORD *data;
5077 DWORD size;
5078 HRESULT hres;
5080 TRACE("(%p)->(%p %s %p %lx %p %lx)\n", This, pStm, debugstr_guid(riid), pv,
5081 dwDestContext, pvDestContext, mshlflags);
5083 if(!pStm || mshlflags!=MSHLFLAGS_NORMAL || (dwDestContext!=MSHCTX_LOCAL
5084 && dwDestContext!=MSHCTX_NOSHAREDMEM && dwDestContext!=MSHCTX_INPROC))
5085 return E_INVALIDARG;
5087 if(dwDestContext == MSHCTX_INPROC) {
5088 struct inproc_marshal_uri data;
5090 data.size = sizeof(data);
5091 data.mshlflags = MSHCTX_INPROC;
5092 data.unk[0] = 0;
5093 data.unk[1] = 0;
5094 data.unk[2] = 0;
5095 data.unk[3] = 0;
5096 data.uri = This;
5098 hres = IStream_Write(pStm, &data, data.size, NULL);
5099 if(FAILED(hres))
5100 return hres;
5102 IUri_AddRef(&This->IUri_iface);
5103 return S_OK;
5106 hres = IMarshal_GetMarshalSizeMax(iface, riid, pv, dwDestContext,
5107 pvDestContext, mshlflags, &size);
5108 if(FAILED(hres))
5109 return hres;
5111 data = calloc(1, size);
5112 if(!data)
5113 return E_OUTOFMEMORY;
5115 data[0] = size;
5116 data[1] = dwDestContext;
5117 data[2] = size-2*sizeof(DWORD);
5118 persist_stream_save(This, pStm, TRUE, (struct persist_uri*)(data+2));
5120 hres = IStream_Write(pStm, data, data[0]-2, NULL);
5121 free(data);
5122 return hres;
5125 static HRESULT WINAPI Marshal_UnmarshalInterface(IMarshal *iface,
5126 IStream *pStm, REFIID riid, void **ppv)
5128 Uri *This = impl_from_IMarshal(iface);
5129 DWORD header[2];
5130 HRESULT hres;
5132 TRACE("(%p)->(%p %s %p)\n", This, pStm, debugstr_guid(riid), ppv);
5134 if(This->create_flags)
5135 return E_UNEXPECTED;
5136 if(!pStm || !riid || !ppv)
5137 return E_INVALIDARG;
5139 hres = IStream_Read(pStm, header, sizeof(header), NULL);
5140 if(FAILED(hres))
5141 return hres;
5143 if(header[1]!=MSHCTX_LOCAL && header[1]!=MSHCTX_NOSHAREDMEM
5144 && header[1]!=MSHCTX_INPROC)
5145 return E_UNEXPECTED;
5147 if(header[1] == MSHCTX_INPROC) {
5148 struct inproc_marshal_uri data;
5149 parse_data parse;
5151 hres = IStream_Read(pStm, data.unk, sizeof(data)-2*sizeof(DWORD), NULL);
5152 if(FAILED(hres))
5153 return hres;
5155 This->raw_uri = SysAllocString(data.uri->raw_uri);
5156 if(!This->raw_uri) {
5157 return E_OUTOFMEMORY;
5160 memset(&parse, 0, sizeof(parse_data));
5161 parse.uri = This->raw_uri;
5163 if(!parse_uri(&parse, data.uri->create_flags))
5164 return E_INVALIDARG;
5166 hres = canonicalize_uri(&parse, This, data.uri->create_flags);
5167 if(FAILED(hres))
5168 return hres;
5170 This->create_flags = data.uri->create_flags;
5171 IUri_Release(&data.uri->IUri_iface);
5173 return IUri_QueryInterface(&This->IUri_iface, riid, ppv);
5176 hres = IPersistStream_Load(&This->IPersistStream_iface, pStm);
5177 if(FAILED(hres))
5178 return hres;
5180 return IUri_QueryInterface(&This->IUri_iface, riid, ppv);
5183 static HRESULT WINAPI Marshal_ReleaseMarshalData(IMarshal *iface, IStream *pStm)
5185 Uri *This = impl_from_IMarshal(iface);
5186 LARGE_INTEGER off;
5187 DWORD header[2];
5188 HRESULT hres;
5190 TRACE("(%p)->(%p)\n", This, pStm);
5192 if(!pStm)
5193 return E_INVALIDARG;
5195 hres = IStream_Read(pStm, header, 2*sizeof(DWORD), NULL);
5196 if(FAILED(hres))
5197 return hres;
5199 if(header[1] == MSHCTX_INPROC) {
5200 struct inproc_marshal_uri data;
5202 hres = IStream_Read(pStm, data.unk, sizeof(data)-2*sizeof(DWORD), NULL);
5203 if(FAILED(hres))
5204 return hres;
5206 IUri_Release(&data.uri->IUri_iface);
5207 return S_OK;
5210 off.u.LowPart = header[0]-sizeof(header)-2;
5211 off.u.HighPart = 0;
5212 return IStream_Seek(pStm, off, STREAM_SEEK_CUR, NULL);
5215 static HRESULT WINAPI Marshal_DisconnectObject(IMarshal *iface, DWORD dwReserved)
5217 Uri *This = impl_from_IMarshal(iface);
5218 TRACE("(%p)->(%lx)\n", This, dwReserved);
5219 return S_OK;
5222 static const IMarshalVtbl MarshalVtbl = {
5223 Marshal_QueryInterface,
5224 Marshal_AddRef,
5225 Marshal_Release,
5226 Marshal_GetUnmarshalClass,
5227 Marshal_GetMarshalSizeMax,
5228 Marshal_MarshalInterface,
5229 Marshal_UnmarshalInterface,
5230 Marshal_ReleaseMarshalData,
5231 Marshal_DisconnectObject
5234 HRESULT Uri_Construct(IUnknown *pUnkOuter, LPVOID *ppobj)
5236 Uri *ret = calloc(1, sizeof(Uri));
5238 TRACE("(%p %p)\n", pUnkOuter, ppobj);
5240 *ppobj = ret;
5241 if(!ret)
5242 return E_OUTOFMEMORY;
5244 ret->IUri_iface.lpVtbl = &UriVtbl;
5245 ret->IUriBuilderFactory_iface.lpVtbl = &UriBuilderFactoryVtbl;
5246 ret->IPersistStream_iface.lpVtbl = &PersistStreamVtbl;
5247 ret->IMarshal_iface.lpVtbl = &MarshalVtbl;
5248 ret->ref = 1;
5250 *ppobj = &ret->IUri_iface;
5251 return S_OK;
5254 /***********************************************************************
5255 * CreateUri (urlmon.@)
5257 * Creates a new IUri object using the URI represented by pwzURI. This function
5258 * parses and validates the components of pwzURI and then canonicalizes the
5259 * parsed components.
5261 * PARAMS
5262 * pwzURI [I] The URI to parse, validate, and canonicalize.
5263 * dwFlags [I] Flags which can affect how the parsing/canonicalization is performed.
5264 * dwReserved [I] Reserved (not used).
5265 * ppURI [O] The resulting IUri after parsing/canonicalization occurs.
5267 * RETURNS
5268 * Success: Returns S_OK. ppURI contains the pointer to the newly allocated IUri.
5269 * Failure: E_INVALIDARG if there are invalid flag combinations in dwFlags, or an
5270 * invalid parameter, or pwzURI doesn't represent a valid URI.
5271 * E_OUTOFMEMORY if any memory allocation fails.
5273 * NOTES
5274 * Default flags:
5275 * Uri_CREATE_CANONICALIZE, Uri_CREATE_DECODE_EXTRA_INFO, Uri_CREATE_CRACK_UNKNOWN_SCHEMES,
5276 * Uri_CREATE_PRE_PROCESS_HTML_URI, Uri_CREATE_NO_IE_SETTINGS.
5278 HRESULT WINAPI CreateUri(LPCWSTR pwzURI, DWORD dwFlags, DWORD_PTR dwReserved, IUri **ppURI)
5280 const DWORD supported_flags = Uri_CREATE_ALLOW_RELATIVE|Uri_CREATE_ALLOW_IMPLICIT_WILDCARD_SCHEME|
5281 Uri_CREATE_ALLOW_IMPLICIT_FILE_SCHEME|Uri_CREATE_NO_CANONICALIZE|Uri_CREATE_CANONICALIZE|
5282 Uri_CREATE_DECODE_EXTRA_INFO|Uri_CREATE_NO_DECODE_EXTRA_INFO|Uri_CREATE_CRACK_UNKNOWN_SCHEMES|
5283 Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES|Uri_CREATE_PRE_PROCESS_HTML_URI|Uri_CREATE_NO_PRE_PROCESS_HTML_URI|
5284 Uri_CREATE_NO_IE_SETTINGS|Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS|Uri_CREATE_FILE_USE_DOS_PATH;
5285 Uri *ret;
5286 HRESULT hr;
5287 parse_data data;
5289 TRACE("(%s %lx %Ix %p)\n", debugstr_w(pwzURI), dwFlags, dwReserved, ppURI);
5291 if(!ppURI)
5292 return E_INVALIDARG;
5294 if(!pwzURI) {
5295 *ppURI = NULL;
5296 return E_INVALIDARG;
5299 /* Check for invalid flags. */
5300 if(has_invalid_flag_combination(dwFlags)) {
5301 *ppURI = NULL;
5302 return E_INVALIDARG;
5305 /* Currently unsupported. */
5306 if(dwFlags & ~supported_flags)
5307 FIXME("Ignoring unsupported flag(s) %lx\n", dwFlags & ~supported_flags);
5309 hr = Uri_Construct(NULL, (void**)&ret);
5310 if(FAILED(hr)) {
5311 *ppURI = NULL;
5312 return hr;
5315 /* Explicitly set the default flags if it doesn't cause a flag conflict. */
5316 apply_default_flags(&dwFlags);
5318 /* Pre process the URI, unless told otherwise. */
5319 if(!(dwFlags & Uri_CREATE_NO_PRE_PROCESS_HTML_URI))
5320 ret->raw_uri = pre_process_uri(pwzURI);
5321 else
5322 ret->raw_uri = SysAllocString(pwzURI);
5324 if(!ret->raw_uri) {
5325 free(ret);
5326 return E_OUTOFMEMORY;
5329 memset(&data, 0, sizeof(parse_data));
5330 data.uri = ret->raw_uri;
5332 /* Validate and parse the URI into its components. */
5333 if(!parse_uri(&data, dwFlags)) {
5334 /* Encountered an unsupported or invalid URI */
5335 IUri_Release(&ret->IUri_iface);
5336 *ppURI = NULL;
5337 return E_INVALIDARG;
5340 /* Canonicalize the URI. */
5341 hr = canonicalize_uri(&data, ret, dwFlags);
5342 if(FAILED(hr)) {
5343 IUri_Release(&ret->IUri_iface);
5344 *ppURI = NULL;
5345 return hr;
5348 ret->create_flags = dwFlags;
5350 *ppURI = &ret->IUri_iface;
5351 return S_OK;
5354 /***********************************************************************
5355 * CreateUriWithFragment (urlmon.@)
5357 * Creates a new IUri object. This is almost the same as CreateUri, expect that
5358 * it allows you to explicitly specify a fragment (pwzFragment) for pwzURI.
5360 * PARAMS
5361 * pwzURI [I] The URI to parse and perform canonicalization on.
5362 * pwzFragment [I] The explicit fragment string which should be added to pwzURI.
5363 * dwFlags [I] The flags which will be passed to CreateUri.
5364 * dwReserved [I] Reserved (not used).
5365 * ppURI [O] The resulting IUri after parsing/canonicalization.
5367 * RETURNS
5368 * Success: S_OK. ppURI contains the pointer to the newly allocated IUri.
5369 * Failure: E_INVALIDARG if pwzURI already contains a fragment and pwzFragment
5370 * isn't NULL. Will also return E_INVALIDARG for the same reasons as
5371 * CreateUri will. E_OUTOFMEMORY if any allocation fails.
5373 HRESULT WINAPI CreateUriWithFragment(LPCWSTR pwzURI, LPCWSTR pwzFragment, DWORD dwFlags,
5374 DWORD_PTR dwReserved, IUri **ppURI)
5376 HRESULT hres;
5377 TRACE("(%s %s %lx %Ix %p)\n", debugstr_w(pwzURI), debugstr_w(pwzFragment), dwFlags, dwReserved, ppURI);
5379 if(!ppURI)
5380 return E_INVALIDARG;
5382 if(!pwzURI) {
5383 *ppURI = NULL;
5384 return E_INVALIDARG;
5387 /* Check if a fragment should be appended to the URI string. */
5388 if(pwzFragment) {
5389 WCHAR *uriW;
5390 DWORD uri_len, frag_len;
5391 BOOL add_pound;
5393 /* Check if the original URI already has a fragment component. */
5394 if(StrChrW(pwzURI, '#')) {
5395 *ppURI = NULL;
5396 return E_INVALIDARG;
5399 uri_len = lstrlenW(pwzURI);
5400 frag_len = lstrlenW(pwzFragment);
5402 /* If the fragment doesn't start with a '#', one will be added. */
5403 add_pound = *pwzFragment != '#';
5405 if(add_pound)
5406 uriW = malloc((uri_len + frag_len + 2) * sizeof(WCHAR));
5407 else
5408 uriW = malloc((uri_len + frag_len + 1) * sizeof(WCHAR));
5410 if(!uriW)
5411 return E_OUTOFMEMORY;
5413 memcpy(uriW, pwzURI, uri_len*sizeof(WCHAR));
5414 if(add_pound)
5415 uriW[uri_len++] = '#';
5416 memcpy(uriW+uri_len, pwzFragment, (frag_len+1)*sizeof(WCHAR));
5418 hres = CreateUri(uriW, dwFlags, 0, ppURI);
5420 free(uriW);
5421 } else
5422 /* A fragment string wasn't specified, so just forward the call. */
5423 hres = CreateUri(pwzURI, dwFlags, 0, ppURI);
5425 return hres;
5428 static HRESULT build_uri(const UriBuilder *builder, IUri **uri, DWORD create_flags,
5429 DWORD use_orig_flags, DWORD encoding_mask)
5431 HRESULT hr;
5432 parse_data data;
5433 Uri *ret;
5435 if(!uri)
5436 return E_POINTER;
5438 if(encoding_mask && (!builder->uri || builder->modified_props)) {
5439 *uri = NULL;
5440 return E_NOTIMPL;
5443 /* Decide what flags should be used when creating the Uri. */
5444 if((use_orig_flags & UriBuilder_USE_ORIGINAL_FLAGS) && builder->uri)
5445 create_flags = builder->uri->create_flags;
5446 else {
5447 if(has_invalid_flag_combination(create_flags)) {
5448 *uri = NULL;
5449 return E_INVALIDARG;
5452 /* Set the default flags if they don't cause a conflict. */
5453 apply_default_flags(&create_flags);
5456 /* Return the base IUri if no changes have been made and the create_flags match. */
5457 if(builder->uri && !builder->modified_props && builder->uri->create_flags == create_flags) {
5458 *uri = &builder->uri->IUri_iface;
5459 IUri_AddRef(*uri);
5460 return S_OK;
5463 hr = validate_components(builder, &data, create_flags);
5464 if(FAILED(hr)) {
5465 *uri = NULL;
5466 return hr;
5469 hr = Uri_Construct(NULL, (void**)&ret);
5470 if(FAILED(hr)) {
5471 *uri = NULL;
5472 return hr;
5475 hr = generate_uri(builder, &data, ret, create_flags);
5476 if(FAILED(hr)) {
5477 IUri_Release(&ret->IUri_iface);
5478 *uri = NULL;
5479 return hr;
5482 *uri = &ret->IUri_iface;
5483 return S_OK;
5486 static inline UriBuilder* impl_from_IUriBuilder(IUriBuilder *iface)
5488 return CONTAINING_RECORD(iface, UriBuilder, IUriBuilder_iface);
5491 static HRESULT WINAPI UriBuilder_QueryInterface(IUriBuilder *iface, REFIID riid, void **ppv)
5493 UriBuilder *This = impl_from_IUriBuilder(iface);
5495 if(IsEqualGUID(&IID_IUnknown, riid)) {
5496 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
5497 *ppv = &This->IUriBuilder_iface;
5498 }else if(IsEqualGUID(&IID_IUriBuilder, riid)) {
5499 TRACE("(%p)->(IID_IUriBuilder %p)\n", This, ppv);
5500 *ppv = &This->IUriBuilder_iface;
5501 }else {
5502 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
5503 *ppv = NULL;
5504 return E_NOINTERFACE;
5507 IUnknown_AddRef((IUnknown*)*ppv);
5508 return S_OK;
5511 static ULONG WINAPI UriBuilder_AddRef(IUriBuilder *iface)
5513 UriBuilder *This = impl_from_IUriBuilder(iface);
5514 LONG ref = InterlockedIncrement(&This->ref);
5516 TRACE("(%p) ref=%ld\n", This, ref);
5518 return ref;
5521 static ULONG WINAPI UriBuilder_Release(IUriBuilder *iface)
5523 UriBuilder *This = impl_from_IUriBuilder(iface);
5524 LONG ref = InterlockedDecrement(&This->ref);
5526 TRACE("(%p) ref=%ld\n", This, ref);
5528 if(!ref) {
5529 if(This->uri) IUri_Release(&This->uri->IUri_iface);
5530 free(This->fragment);
5531 free(This->host);
5532 free(This->password);
5533 free(This->path);
5534 free(This->query);
5535 free(This->scheme);
5536 free(This->username);
5537 free(This);
5540 return ref;
5543 static HRESULT WINAPI UriBuilder_CreateUriSimple(IUriBuilder *iface,
5544 DWORD dwAllowEncodingPropertyMask,
5545 DWORD_PTR dwReserved,
5546 IUri **ppIUri)
5548 UriBuilder *This = impl_from_IUriBuilder(iface);
5549 HRESULT hr;
5550 TRACE("(%p)->(%ld %Id %p)\n", This, dwAllowEncodingPropertyMask, dwReserved, ppIUri);
5552 hr = build_uri(This, ppIUri, 0, UriBuilder_USE_ORIGINAL_FLAGS, dwAllowEncodingPropertyMask);
5553 if(hr == E_NOTIMPL)
5554 FIXME("(%p)->(%ld %Id %p)\n", This, dwAllowEncodingPropertyMask, dwReserved, ppIUri);
5555 return hr;
5558 static HRESULT WINAPI UriBuilder_CreateUri(IUriBuilder *iface,
5559 DWORD dwCreateFlags,
5560 DWORD dwAllowEncodingPropertyMask,
5561 DWORD_PTR dwReserved,
5562 IUri **ppIUri)
5564 UriBuilder *This = impl_from_IUriBuilder(iface);
5565 HRESULT hr;
5566 TRACE("(%p)->(0x%08lx %ld %Id %p)\n", This, dwCreateFlags, dwAllowEncodingPropertyMask, dwReserved, ppIUri);
5568 if(dwCreateFlags == -1)
5569 hr = build_uri(This, ppIUri, 0, UriBuilder_USE_ORIGINAL_FLAGS, dwAllowEncodingPropertyMask);
5570 else
5571 hr = build_uri(This, ppIUri, dwCreateFlags, 0, dwAllowEncodingPropertyMask);
5573 if(hr == E_NOTIMPL)
5574 FIXME("(%p)->(0x%08lx %ld %Id %p)\n", This, dwCreateFlags, dwAllowEncodingPropertyMask, dwReserved, ppIUri);
5575 return hr;
5578 static HRESULT WINAPI UriBuilder_CreateUriWithFlags(IUriBuilder *iface,
5579 DWORD dwCreateFlags,
5580 DWORD dwUriBuilderFlags,
5581 DWORD dwAllowEncodingPropertyMask,
5582 DWORD_PTR dwReserved,
5583 IUri **ppIUri)
5585 UriBuilder *This = impl_from_IUriBuilder(iface);
5586 HRESULT hr;
5587 TRACE("(%p)->(0x%08lx 0x%08lx %ld %Id %p)\n", This, dwCreateFlags, dwUriBuilderFlags,
5588 dwAllowEncodingPropertyMask, dwReserved, ppIUri);
5590 hr = build_uri(This, ppIUri, dwCreateFlags, dwUriBuilderFlags, dwAllowEncodingPropertyMask);
5591 if(hr == E_NOTIMPL)
5592 FIXME("(%p)->(0x%08lx 0x%08lx %ld %Id %p)\n", This, dwCreateFlags, dwUriBuilderFlags,
5593 dwAllowEncodingPropertyMask, dwReserved, ppIUri);
5594 return hr;
5597 static HRESULT WINAPI UriBuilder_GetIUri(IUriBuilder *iface, IUri **ppIUri)
5599 UriBuilder *This = impl_from_IUriBuilder(iface);
5600 TRACE("(%p)->(%p)\n", This, ppIUri);
5602 if(!ppIUri)
5603 return E_POINTER;
5605 if(This->uri) {
5606 IUri *uri = &This->uri->IUri_iface;
5607 IUri_AddRef(uri);
5608 *ppIUri = uri;
5609 } else
5610 *ppIUri = NULL;
5612 return S_OK;
5615 static HRESULT WINAPI UriBuilder_SetIUri(IUriBuilder *iface, IUri *pIUri)
5617 UriBuilder *This = impl_from_IUriBuilder(iface);
5618 TRACE("(%p)->(%p)\n", This, pIUri);
5620 if(pIUri) {
5621 Uri *uri;
5623 if((uri = get_uri_obj(pIUri))) {
5624 /* Only reset the builder if its Uri isn't the same as
5625 * the Uri passed to the function.
5627 if(This->uri != uri) {
5628 reset_builder(This);
5630 This->uri = uri;
5631 if(uri->has_port)
5632 This->port = uri->port;
5634 IUri_AddRef(pIUri);
5636 } else {
5637 FIXME("(%p)->(%p) Unknown IUri types not supported yet.\n", This, pIUri);
5638 return E_NOTIMPL;
5640 } else if(This->uri)
5641 /* Only reset the builder if its Uri isn't NULL. */
5642 reset_builder(This);
5644 return S_OK;
5647 static HRESULT WINAPI UriBuilder_GetFragment(IUriBuilder *iface, DWORD *pcchFragment, LPCWSTR *ppwzFragment)
5649 UriBuilder *This = impl_from_IUriBuilder(iface);
5650 TRACE("(%p)->(%p %p)\n", This, pcchFragment, ppwzFragment);
5652 if(!This->uri || This->uri->fragment_start == -1 || This->modified_props & Uri_HAS_FRAGMENT)
5653 return get_builder_component(&This->fragment, &This->fragment_len, NULL, 0, ppwzFragment, pcchFragment);
5654 else
5655 return get_builder_component(&This->fragment, &This->fragment_len, This->uri->canon_uri+This->uri->fragment_start,
5656 This->uri->fragment_len, ppwzFragment, pcchFragment);
5659 static HRESULT WINAPI UriBuilder_GetHost(IUriBuilder *iface, DWORD *pcchHost, LPCWSTR *ppwzHost)
5661 UriBuilder *This = impl_from_IUriBuilder(iface);
5662 TRACE("(%p)->(%p %p)\n", This, pcchHost, ppwzHost);
5664 if(!This->uri || This->uri->host_start == -1 || This->modified_props & Uri_HAS_HOST)
5665 return get_builder_component(&This->host, &This->host_len, NULL, 0, ppwzHost, pcchHost);
5666 else {
5667 if(This->uri->host_type == Uri_HOST_IPV6)
5668 /* Don't include the '[' and ']' around the address. */
5669 return get_builder_component(&This->host, &This->host_len, This->uri->canon_uri+This->uri->host_start+1,
5670 This->uri->host_len-2, ppwzHost, pcchHost);
5671 else
5672 return get_builder_component(&This->host, &This->host_len, This->uri->canon_uri+This->uri->host_start,
5673 This->uri->host_len, ppwzHost, pcchHost);
5677 static HRESULT WINAPI UriBuilder_GetPassword(IUriBuilder *iface, DWORD *pcchPassword, LPCWSTR *ppwzPassword)
5679 UriBuilder *This = impl_from_IUriBuilder(iface);
5680 TRACE("(%p)->(%p %p)\n", This, pcchPassword, ppwzPassword);
5682 if(!This->uri || This->uri->userinfo_split == -1 || This->modified_props & Uri_HAS_PASSWORD)
5683 return get_builder_component(&This->password, &This->password_len, NULL, 0, ppwzPassword, pcchPassword);
5684 else {
5685 const WCHAR *start = This->uri->canon_uri+This->uri->userinfo_start+This->uri->userinfo_split+1;
5686 DWORD len = This->uri->userinfo_len-This->uri->userinfo_split-1;
5687 return get_builder_component(&This->password, &This->password_len, start, len, ppwzPassword, pcchPassword);
5691 static HRESULT WINAPI UriBuilder_GetPath(IUriBuilder *iface, DWORD *pcchPath, LPCWSTR *ppwzPath)
5693 UriBuilder *This = impl_from_IUriBuilder(iface);
5694 TRACE("(%p)->(%p %p)\n", This, pcchPath, ppwzPath);
5696 if(!This->uri || This->uri->path_start == -1 || This->modified_props & Uri_HAS_PATH)
5697 return get_builder_component(&This->path, &This->path_len, NULL, 0, ppwzPath, pcchPath);
5698 else
5699 return get_builder_component(&This->path, &This->path_len, This->uri->canon_uri+This->uri->path_start,
5700 This->uri->path_len, ppwzPath, pcchPath);
5703 static HRESULT WINAPI UriBuilder_GetPort(IUriBuilder *iface, BOOL *pfHasPort, DWORD *pdwPort)
5705 UriBuilder *This = impl_from_IUriBuilder(iface);
5706 TRACE("(%p)->(%p %p)\n", This, pfHasPort, pdwPort);
5708 if(!pfHasPort) {
5709 if(pdwPort)
5710 *pdwPort = 0;
5711 return E_POINTER;
5714 if(!pdwPort) {
5715 *pfHasPort = FALSE;
5716 return E_POINTER;
5719 *pfHasPort = This->has_port;
5720 *pdwPort = This->port;
5721 return S_OK;
5724 static HRESULT WINAPI UriBuilder_GetQuery(IUriBuilder *iface, DWORD *pcchQuery, LPCWSTR *ppwzQuery)
5726 UriBuilder *This = impl_from_IUriBuilder(iface);
5727 TRACE("(%p)->(%p %p)\n", This, pcchQuery, ppwzQuery);
5729 if(!This->uri || This->uri->query_start == -1 || This->modified_props & Uri_HAS_QUERY)
5730 return get_builder_component(&This->query, &This->query_len, NULL, 0, ppwzQuery, pcchQuery);
5731 else
5732 return get_builder_component(&This->query, &This->query_len, This->uri->canon_uri+This->uri->query_start,
5733 This->uri->query_len, ppwzQuery, pcchQuery);
5736 static HRESULT WINAPI UriBuilder_GetSchemeName(IUriBuilder *iface, DWORD *pcchSchemeName, LPCWSTR *ppwzSchemeName)
5738 UriBuilder *This = impl_from_IUriBuilder(iface);
5739 TRACE("(%p)->(%p %p)\n", This, pcchSchemeName, ppwzSchemeName);
5741 if(!This->uri || This->uri->scheme_start == -1 || This->modified_props & Uri_HAS_SCHEME_NAME)
5742 return get_builder_component(&This->scheme, &This->scheme_len, NULL, 0, ppwzSchemeName, pcchSchemeName);
5743 else
5744 return get_builder_component(&This->scheme, &This->scheme_len, This->uri->canon_uri+This->uri->scheme_start,
5745 This->uri->scheme_len, ppwzSchemeName, pcchSchemeName);
5748 static HRESULT WINAPI UriBuilder_GetUserName(IUriBuilder *iface, DWORD *pcchUserName, LPCWSTR *ppwzUserName)
5750 UriBuilder *This = impl_from_IUriBuilder(iface);
5751 TRACE("(%p)->(%p %p)\n", This, pcchUserName, ppwzUserName);
5753 if(!This->uri || This->uri->userinfo_start == -1 || This->uri->userinfo_split == 0 ||
5754 This->modified_props & Uri_HAS_USER_NAME)
5755 return get_builder_component(&This->username, &This->username_len, NULL, 0, ppwzUserName, pcchUserName);
5756 else {
5757 const WCHAR *start = This->uri->canon_uri+This->uri->userinfo_start;
5759 /* Check if there's a password in the userinfo section. */
5760 if(This->uri->userinfo_split > -1)
5761 /* Don't include the password. */
5762 return get_builder_component(&This->username, &This->username_len, start,
5763 This->uri->userinfo_split, ppwzUserName, pcchUserName);
5764 else
5765 return get_builder_component(&This->username, &This->username_len, start,
5766 This->uri->userinfo_len, ppwzUserName, pcchUserName);
5770 static HRESULT WINAPI UriBuilder_SetFragment(IUriBuilder *iface, LPCWSTR pwzNewValue)
5772 UriBuilder *This = impl_from_IUriBuilder(iface);
5773 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
5774 return set_builder_component(&This->fragment, &This->fragment_len, pwzNewValue, '#',
5775 &This->modified_props, Uri_HAS_FRAGMENT);
5778 static HRESULT WINAPI UriBuilder_SetHost(IUriBuilder *iface, LPCWSTR pwzNewValue)
5780 UriBuilder *This = impl_from_IUriBuilder(iface);
5781 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
5783 /* Host name can't be set to NULL. */
5784 if(!pwzNewValue)
5785 return E_INVALIDARG;
5787 return set_builder_component(&This->host, &This->host_len, pwzNewValue, 0,
5788 &This->modified_props, Uri_HAS_HOST);
5791 static HRESULT WINAPI UriBuilder_SetPassword(IUriBuilder *iface, LPCWSTR pwzNewValue)
5793 UriBuilder *This = impl_from_IUriBuilder(iface);
5794 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
5795 return set_builder_component(&This->password, &This->password_len, pwzNewValue, 0,
5796 &This->modified_props, Uri_HAS_PASSWORD);
5799 static HRESULT WINAPI UriBuilder_SetPath(IUriBuilder *iface, LPCWSTR pwzNewValue)
5801 UriBuilder *This = impl_from_IUriBuilder(iface);
5802 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
5803 return set_builder_component(&This->path, &This->path_len, pwzNewValue, 0,
5804 &This->modified_props, Uri_HAS_PATH);
5807 static HRESULT WINAPI UriBuilder_SetPort(IUriBuilder *iface, BOOL fHasPort, DWORD dwNewValue)
5809 UriBuilder *This = impl_from_IUriBuilder(iface);
5810 TRACE("(%p)->(%d %ld)\n", This, fHasPort, dwNewValue);
5812 This->has_port = fHasPort;
5813 This->port = dwNewValue;
5814 This->modified_props |= Uri_HAS_PORT;
5815 return S_OK;
5818 static HRESULT WINAPI UriBuilder_SetQuery(IUriBuilder *iface, LPCWSTR pwzNewValue)
5820 UriBuilder *This = impl_from_IUriBuilder(iface);
5821 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
5822 return set_builder_component(&This->query, &This->query_len, pwzNewValue, '?',
5823 &This->modified_props, Uri_HAS_QUERY);
5826 static HRESULT WINAPI UriBuilder_SetSchemeName(IUriBuilder *iface, LPCWSTR pwzNewValue)
5828 UriBuilder *This = impl_from_IUriBuilder(iface);
5829 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
5831 /* Only set the scheme name if it's not NULL or empty. */
5832 if(!pwzNewValue || !*pwzNewValue)
5833 return E_INVALIDARG;
5835 return set_builder_component(&This->scheme, &This->scheme_len, pwzNewValue, 0,
5836 &This->modified_props, Uri_HAS_SCHEME_NAME);
5839 static HRESULT WINAPI UriBuilder_SetUserName(IUriBuilder *iface, LPCWSTR pwzNewValue)
5841 UriBuilder *This = impl_from_IUriBuilder(iface);
5842 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
5843 return set_builder_component(&This->username, &This->username_len, pwzNewValue, 0,
5844 &This->modified_props, Uri_HAS_USER_NAME);
5847 static HRESULT WINAPI UriBuilder_RemoveProperties(IUriBuilder *iface, DWORD dwPropertyMask)
5849 const DWORD accepted_flags = Uri_HAS_AUTHORITY|Uri_HAS_DOMAIN|Uri_HAS_EXTENSION|Uri_HAS_FRAGMENT|Uri_HAS_HOST|
5850 Uri_HAS_PASSWORD|Uri_HAS_PATH|Uri_HAS_PATH_AND_QUERY|Uri_HAS_QUERY|
5851 Uri_HAS_USER_INFO|Uri_HAS_USER_NAME;
5853 UriBuilder *This = impl_from_IUriBuilder(iface);
5854 TRACE("(%p)->(0x%08lx)\n", This, dwPropertyMask);
5856 if(dwPropertyMask & ~accepted_flags)
5857 return E_INVALIDARG;
5859 if(dwPropertyMask & Uri_HAS_FRAGMENT)
5860 UriBuilder_SetFragment(iface, NULL);
5862 /* Even though you can't set the host name to NULL or an
5863 * empty string, you can still remove it... for some reason.
5865 if(dwPropertyMask & Uri_HAS_HOST)
5866 set_builder_component(&This->host, &This->host_len, NULL, 0,
5867 &This->modified_props, Uri_HAS_HOST);
5869 if(dwPropertyMask & Uri_HAS_PASSWORD)
5870 UriBuilder_SetPassword(iface, NULL);
5872 if(dwPropertyMask & Uri_HAS_PATH)
5873 UriBuilder_SetPath(iface, NULL);
5875 if(dwPropertyMask & Uri_HAS_PORT)
5876 UriBuilder_SetPort(iface, FALSE, 0);
5878 if(dwPropertyMask & Uri_HAS_QUERY)
5879 UriBuilder_SetQuery(iface, NULL);
5881 if(dwPropertyMask & Uri_HAS_USER_NAME)
5882 UriBuilder_SetUserName(iface, NULL);
5884 return S_OK;
5887 static HRESULT WINAPI UriBuilder_HasBeenModified(IUriBuilder *iface, BOOL *pfModified)
5889 UriBuilder *This = impl_from_IUriBuilder(iface);
5890 TRACE("(%p)->(%p)\n", This, pfModified);
5892 if(!pfModified)
5893 return E_POINTER;
5895 *pfModified = This->modified_props > 0;
5896 return S_OK;
5899 static const IUriBuilderVtbl UriBuilderVtbl = {
5900 UriBuilder_QueryInterface,
5901 UriBuilder_AddRef,
5902 UriBuilder_Release,
5903 UriBuilder_CreateUriSimple,
5904 UriBuilder_CreateUri,
5905 UriBuilder_CreateUriWithFlags,
5906 UriBuilder_GetIUri,
5907 UriBuilder_SetIUri,
5908 UriBuilder_GetFragment,
5909 UriBuilder_GetHost,
5910 UriBuilder_GetPassword,
5911 UriBuilder_GetPath,
5912 UriBuilder_GetPort,
5913 UriBuilder_GetQuery,
5914 UriBuilder_GetSchemeName,
5915 UriBuilder_GetUserName,
5916 UriBuilder_SetFragment,
5917 UriBuilder_SetHost,
5918 UriBuilder_SetPassword,
5919 UriBuilder_SetPath,
5920 UriBuilder_SetPort,
5921 UriBuilder_SetQuery,
5922 UriBuilder_SetSchemeName,
5923 UriBuilder_SetUserName,
5924 UriBuilder_RemoveProperties,
5925 UriBuilder_HasBeenModified,
5928 /***********************************************************************
5929 * CreateIUriBuilder (urlmon.@)
5931 HRESULT WINAPI CreateIUriBuilder(IUri *pIUri, DWORD dwFlags, DWORD_PTR dwReserved, IUriBuilder **ppIUriBuilder)
5933 UriBuilder *ret;
5935 TRACE("(%p %lx %Ix %p)\n", pIUri, dwFlags, dwReserved, ppIUriBuilder);
5937 if(!ppIUriBuilder)
5938 return E_POINTER;
5940 ret = calloc(1, sizeof(UriBuilder));
5941 if(!ret)
5942 return E_OUTOFMEMORY;
5944 ret->IUriBuilder_iface.lpVtbl = &UriBuilderVtbl;
5945 ret->ref = 1;
5947 if(pIUri) {
5948 Uri *uri;
5950 if((uri = get_uri_obj(pIUri))) {
5951 if(!uri->create_flags) {
5952 free(ret);
5953 return E_UNEXPECTED;
5955 IUri_AddRef(pIUri);
5956 ret->uri = uri;
5958 if(uri->has_port)
5959 /* Windows doesn't set 'has_port' to TRUE in this case. */
5960 ret->port = uri->port;
5962 } else {
5963 free(ret);
5964 *ppIUriBuilder = NULL;
5965 FIXME("(%p %lx %Ix %p): Unknown IUri types not supported yet.\n", pIUri, dwFlags,
5966 dwReserved, ppIUriBuilder);
5967 return E_NOTIMPL;
5971 *ppIUriBuilder = &ret->IUriBuilder_iface;
5972 return S_OK;
5975 /* Merges the base path with the relative path and stores the resulting path
5976 * and path len in 'result' and 'result_len'.
5978 static HRESULT merge_paths(parse_data *data, const WCHAR *base, DWORD base_len, const WCHAR *relative,
5979 DWORD relative_len, WCHAR **result, DWORD *result_len, DWORD flags)
5981 const WCHAR *end = NULL;
5982 DWORD base_copy_len = 0;
5983 WCHAR *ptr;
5985 if(base_len) {
5986 if(data->scheme_type == URL_SCHEME_MK && *relative == '/') {
5987 /* Find '::' segment */
5988 for(end = base; end < base+base_len-1; end++) {
5989 if(end[0] == ':' && end[1] == ':') {
5990 end++;
5991 break;
5995 /* If not found, try finding the end of @xxx: */
5996 if(end == base+base_len-1)
5997 end = *base == '@' ? wmemchr(base, ':', base_len) : NULL;
5998 }else {
5999 /* Find the characters that will be copied over from the base path. */
6000 for (end = base + base_len - 1; end >= base; end--) if (*end == '/') break;
6001 if(end < base && data->scheme_type == URL_SCHEME_FILE)
6002 /* Try looking for a '\\'. */
6003 for (end = base + base_len - 1; end >= base; end--) if (*end == '\\') break;
6007 if (end) base_copy_len = (end+1)-base;
6008 *result = malloc((base_copy_len + relative_len + 1) * sizeof(WCHAR));
6010 if(!(*result)) {
6011 *result_len = 0;
6012 return E_OUTOFMEMORY;
6015 ptr = *result;
6016 memcpy(ptr, base, base_copy_len*sizeof(WCHAR));
6017 ptr += base_copy_len;
6019 memcpy(ptr, relative, relative_len*sizeof(WCHAR));
6020 ptr += relative_len;
6021 *ptr = '\0';
6023 *result_len = (ptr-*result);
6024 TRACE("ret %s\n", debugstr_wn(*result, *result_len));
6025 return S_OK;
6028 static HRESULT combine_uri(Uri *base, Uri *relative, DWORD flags, IUri **result, DWORD extras) {
6029 Uri *ret;
6030 HRESULT hr;
6031 parse_data data;
6032 Uri *proc_uri = base;
6033 DWORD create_flags = 0, len = 0;
6035 memset(&data, 0, sizeof(parse_data));
6037 /* Base case is when the relative Uri has a scheme name,
6038 * if it does, then 'result' will contain the same data
6039 * as the relative Uri.
6041 if(relative->scheme_start > -1) {
6042 data.uri = SysAllocString(relative->raw_uri);
6043 if(!data.uri) {
6044 *result = NULL;
6045 return E_OUTOFMEMORY;
6048 parse_uri(&data, Uri_CREATE_ALLOW_IMPLICIT_FILE_SCHEME);
6050 hr = Uri_Construct(NULL, (void**)&ret);
6051 if(FAILED(hr)) {
6052 *result = NULL;
6053 return hr;
6056 if(extras & COMBINE_URI_FORCE_FLAG_USE) {
6057 if(flags & URL_DONT_SIMPLIFY)
6058 create_flags |= Uri_CREATE_NO_CANONICALIZE;
6059 if(flags & URL_DONT_UNESCAPE_EXTRA_INFO)
6060 create_flags |= Uri_CREATE_NO_DECODE_EXTRA_INFO;
6063 ret->raw_uri = data.uri;
6064 hr = canonicalize_uri(&data, ret, create_flags);
6065 if(FAILED(hr)) {
6066 IUri_Release(&ret->IUri_iface);
6067 *result = NULL;
6068 return hr;
6071 apply_default_flags(&create_flags);
6072 ret->create_flags = create_flags;
6074 *result = &ret->IUri_iface;
6075 } else {
6076 WCHAR *path = NULL;
6077 DWORD raw_flags = 0;
6079 if(base->scheme_start > -1) {
6080 data.scheme = base->canon_uri+base->scheme_start;
6081 data.scheme_len = base->scheme_len;
6082 data.scheme_type = base->scheme_type;
6083 } else {
6084 data.is_relative = TRUE;
6085 data.scheme_type = URL_SCHEME_UNKNOWN;
6086 create_flags |= Uri_CREATE_ALLOW_RELATIVE;
6089 if(relative->authority_start > -1)
6090 proc_uri = relative;
6092 if(proc_uri->authority_start > -1) {
6093 if(proc_uri->userinfo_start > -1 && proc_uri->userinfo_split != 0) {
6094 data.username = proc_uri->canon_uri+proc_uri->userinfo_start;
6095 data.username_len = (proc_uri->userinfo_split > -1) ? proc_uri->userinfo_split : proc_uri->userinfo_len;
6098 if(proc_uri->userinfo_split > -1) {
6099 data.password = proc_uri->canon_uri+proc_uri->userinfo_start+proc_uri->userinfo_split+1;
6100 data.password_len = proc_uri->userinfo_len-proc_uri->userinfo_split-1;
6103 if(proc_uri->host_start > -1) {
6104 const WCHAR *host = proc_uri->canon_uri+proc_uri->host_start;
6105 parse_host(&host, &data, 0);
6108 if(proc_uri->has_port) {
6109 data.has_port = TRUE;
6110 data.port_value = proc_uri->port;
6112 } else if(base->scheme_type != URL_SCHEME_FILE)
6113 data.is_opaque = TRUE;
6115 if(proc_uri == relative || relative->path_start == -1 || !relative->path_len) {
6116 if(proc_uri->path_start > -1) {
6117 data.path = proc_uri->canon_uri+proc_uri->path_start;
6118 data.path_len = proc_uri->path_len;
6119 } else if(!data.is_opaque) {
6120 /* Just set the path as a '/' if the base didn't have
6121 * one and if it's a hierarchical URI.
6123 data.path = L"/";
6124 data.path_len = 1;
6127 if(relative->query_start > -1)
6128 proc_uri = relative;
6130 if(proc_uri->query_start > -1) {
6131 data.query = proc_uri->canon_uri+proc_uri->query_start;
6132 data.query_len = proc_uri->query_len;
6134 } else {
6135 const WCHAR *ptr, **pptr;
6136 DWORD path_offset = 0, path_len = 0;
6138 /* There's two possibilities on what will happen to the path component
6139 * of the result IUri. First, if the relative path begins with a '/'
6140 * then the resulting path will just be the relative path. Second, if
6141 * relative path doesn't begin with a '/' then the base path and relative
6142 * path are merged together.
6144 if(relative->path_len && *(relative->canon_uri+relative->path_start) == '/' && data.scheme_type != URL_SCHEME_MK) {
6145 WCHAR *tmp = NULL;
6146 BOOL copy_drive_path = FALSE;
6148 /* If the relative IUri's path starts with a '/', then we
6149 * don't use the base IUri's path. Unless the base IUri
6150 * is a file URI, in which case it uses the drive path of
6151 * the base IUri (if it has any) in the new path.
6153 if(base->scheme_type == URL_SCHEME_FILE) {
6154 if(base->path_len > 3 && *(base->canon_uri+base->path_start) == '/' &&
6155 is_drive_path(base->canon_uri+base->path_start+1)) {
6156 path_len += 3;
6157 copy_drive_path = TRUE;
6161 path_len += relative->path_len;
6163 path = malloc((path_len + 1) * sizeof(WCHAR));
6164 if(!path) {
6165 *result = NULL;
6166 return E_OUTOFMEMORY;
6169 tmp = path;
6171 /* Copy the base paths, drive path over. */
6172 if(copy_drive_path) {
6173 memcpy(tmp, base->canon_uri+base->path_start, 3*sizeof(WCHAR));
6174 tmp += 3;
6177 memcpy(tmp, relative->canon_uri+relative->path_start, relative->path_len*sizeof(WCHAR));
6178 path[path_len] = '\0';
6179 } else {
6180 /* Merge the base path with the relative path. */
6181 hr = merge_paths(&data, base->canon_uri+base->path_start, base->path_len,
6182 relative->canon_uri+relative->path_start, relative->path_len,
6183 &path, &path_len, flags);
6184 if(FAILED(hr)) {
6185 *result = NULL;
6186 return hr;
6189 /* If the resulting IUri is a file URI, the drive path isn't
6190 * reduced out when the dot segments are removed.
6192 if(path_len >= 3 && data.scheme_type == URL_SCHEME_FILE && !data.host) {
6193 if(*path == '/' && is_drive_path(path+1))
6194 path_offset = 2;
6195 else if(is_drive_path(path))
6196 path_offset = 1;
6200 /* Check if the dot segments need to be removed from the path. */
6201 if(!(flags & URL_DONT_SIMPLIFY) && !data.is_opaque) {
6202 DWORD offset = (path_offset > 0) ? path_offset+1 : 0;
6203 DWORD new_len = remove_dot_segments(path+offset,path_len-offset);
6205 if(new_len != path_len) {
6206 WCHAR *tmp = realloc(path, (offset + new_len + 1) * sizeof(WCHAR));
6207 if(!tmp) {
6208 free(path);
6209 *result = NULL;
6210 return E_OUTOFMEMORY;
6213 tmp[new_len+offset] = '\0';
6214 path = tmp;
6215 path_len = new_len+offset;
6219 if(relative->query_start > -1) {
6220 data.query = relative->canon_uri+relative->query_start;
6221 data.query_len = relative->query_len;
6224 /* Make sure the path component is valid. */
6225 ptr = path;
6226 pptr = &ptr;
6227 if((data.is_opaque && !parse_path_opaque(pptr, &data, 0)) ||
6228 (!data.is_opaque && !parse_path_hierarchical(pptr, &data, 0))) {
6229 free(path);
6230 *result = NULL;
6231 return E_INVALIDARG;
6235 if(relative->fragment_start > -1) {
6236 data.fragment = relative->canon_uri+relative->fragment_start;
6237 data.fragment_len = relative->fragment_len;
6240 if(flags & URL_DONT_SIMPLIFY)
6241 raw_flags |= RAW_URI_FORCE_PORT_DISP;
6242 if(flags & URL_FILE_USE_PATHURL)
6243 raw_flags |= RAW_URI_CONVERT_TO_DOS_PATH;
6245 len = generate_raw_uri(&data, data.uri, raw_flags);
6246 data.uri = SysAllocStringLen(NULL, len);
6247 if(!data.uri) {
6248 free(path);
6249 *result = NULL;
6250 return E_OUTOFMEMORY;
6253 generate_raw_uri(&data, data.uri, raw_flags);
6255 hr = Uri_Construct(NULL, (void**)&ret);
6256 if(FAILED(hr)) {
6257 SysFreeString(data.uri);
6258 free(path);
6259 *result = NULL;
6260 return hr;
6263 if(flags & URL_DONT_SIMPLIFY)
6264 create_flags |= Uri_CREATE_NO_CANONICALIZE;
6265 if(flags & URL_FILE_USE_PATHURL)
6266 create_flags |= Uri_CREATE_FILE_USE_DOS_PATH;
6268 ret->raw_uri = data.uri;
6269 hr = canonicalize_uri(&data, ret, create_flags);
6270 if(FAILED(hr)) {
6271 IUri_Release(&ret->IUri_iface);
6272 *result = NULL;
6273 return hr;
6276 if(flags & URL_DONT_SIMPLIFY)
6277 ret->display_modifiers |= URI_DISPLAY_NO_DEFAULT_PORT_AUTH;
6279 apply_default_flags(&create_flags);
6280 ret->create_flags = create_flags;
6281 *result = &ret->IUri_iface;
6283 free(path);
6286 return S_OK;
6289 /***********************************************************************
6290 * CoInternetCombineIUri (urlmon.@)
6292 HRESULT WINAPI CoInternetCombineIUri(IUri *pBaseUri, IUri *pRelativeUri, DWORD dwCombineFlags,
6293 IUri **ppCombinedUri, DWORD_PTR dwReserved)
6295 HRESULT hr;
6296 IInternetProtocolInfo *info;
6297 Uri *relative, *base;
6298 TRACE("(%p %p %lx %p %Ix)\n", pBaseUri, pRelativeUri, dwCombineFlags, ppCombinedUri, dwReserved);
6300 if(!ppCombinedUri)
6301 return E_INVALIDARG;
6303 if(!pBaseUri || !pRelativeUri) {
6304 *ppCombinedUri = NULL;
6305 return E_INVALIDARG;
6308 relative = get_uri_obj(pRelativeUri);
6309 base = get_uri_obj(pBaseUri);
6310 if(!relative || !base) {
6311 *ppCombinedUri = NULL;
6312 FIXME("(%p %p %lx %p %Ix) Unknown IUri types not supported yet.\n",
6313 pBaseUri, pRelativeUri, dwCombineFlags, ppCombinedUri, dwReserved);
6314 return E_NOTIMPL;
6317 info = get_protocol_info(base->canon_uri);
6318 if(info) {
6319 WCHAR result[INTERNET_MAX_URL_LENGTH+1];
6320 DWORD result_len = 0;
6322 hr = IInternetProtocolInfo_CombineUrl(info, base->canon_uri, relative->canon_uri, dwCombineFlags,
6323 result, INTERNET_MAX_URL_LENGTH+1, &result_len, 0);
6324 IInternetProtocolInfo_Release(info);
6325 if(SUCCEEDED(hr)) {
6326 hr = CreateUri(result, Uri_CREATE_ALLOW_RELATIVE, 0, ppCombinedUri);
6327 if(SUCCEEDED(hr))
6328 return hr;
6332 return combine_uri(base, relative, dwCombineFlags, ppCombinedUri, 0);
6335 /***********************************************************************
6336 * CoInternetCombineUrlEx (urlmon.@)
6338 HRESULT WINAPI CoInternetCombineUrlEx(IUri *pBaseUri, LPCWSTR pwzRelativeUrl, DWORD dwCombineFlags,
6339 IUri **ppCombinedUri, DWORD_PTR dwReserved)
6341 IUri *relative;
6342 Uri *base;
6343 HRESULT hr;
6344 IInternetProtocolInfo *info;
6346 TRACE("(%p %s %lx %p %Ix)\n", pBaseUri, debugstr_w(pwzRelativeUrl), dwCombineFlags,
6347 ppCombinedUri, dwReserved);
6349 if(!ppCombinedUri)
6350 return E_POINTER;
6352 if(!pwzRelativeUrl) {
6353 *ppCombinedUri = NULL;
6354 return E_UNEXPECTED;
6357 if(!pBaseUri) {
6358 *ppCombinedUri = NULL;
6359 return E_INVALIDARG;
6362 base = get_uri_obj(pBaseUri);
6363 if(!base) {
6364 *ppCombinedUri = NULL;
6365 FIXME("(%p %s %lx %p %Ix) Unknown IUri's not supported yet.\n", pBaseUri, debugstr_w(pwzRelativeUrl),
6366 dwCombineFlags, ppCombinedUri, dwReserved);
6367 return E_NOTIMPL;
6370 info = get_protocol_info(base->canon_uri);
6371 if(info) {
6372 WCHAR result[INTERNET_MAX_URL_LENGTH+1];
6373 DWORD result_len = 0;
6375 hr = IInternetProtocolInfo_CombineUrl(info, base->canon_uri, pwzRelativeUrl, dwCombineFlags,
6376 result, INTERNET_MAX_URL_LENGTH+1, &result_len, 0);
6377 IInternetProtocolInfo_Release(info);
6378 if(SUCCEEDED(hr)) {
6379 hr = CreateUri(result, Uri_CREATE_ALLOW_RELATIVE, 0, ppCombinedUri);
6380 if(SUCCEEDED(hr))
6381 return hr;
6385 hr = CreateUri(pwzRelativeUrl, Uri_CREATE_ALLOW_RELATIVE|Uri_CREATE_ALLOW_IMPLICIT_FILE_SCHEME, 0, &relative);
6386 if(FAILED(hr)) {
6387 *ppCombinedUri = NULL;
6388 return hr;
6391 hr = combine_uri(base, get_uri_obj(relative), dwCombineFlags, ppCombinedUri, COMBINE_URI_FORCE_FLAG_USE);
6393 IUri_Release(relative);
6394 return hr;
6397 static HRESULT parse_canonicalize(const Uri *uri, DWORD flags, LPWSTR output,
6398 DWORD output_len, DWORD *result_len)
6400 const WCHAR *ptr = NULL;
6401 WCHAR *path = NULL;
6402 const WCHAR **pptr;
6403 DWORD len = 0;
6404 BOOL reduce_path;
6406 /* URL_UNESCAPE only has effect if none of the URL_ESCAPE flags are set. */
6407 const BOOL allow_unescape = !(flags & URL_ESCAPE_UNSAFE) &&
6408 !(flags & URL_ESCAPE_SPACES_ONLY) &&
6409 !(flags & URL_ESCAPE_PERCENT);
6412 /* Check if the dot segments need to be removed from the
6413 * path component.
6415 if(uri->scheme_start > -1 && uri->path_start > -1) {
6416 ptr = uri->canon_uri+uri->scheme_start+uri->scheme_len+1;
6417 pptr = &ptr;
6419 reduce_path = !(flags & URL_DONT_SIMPLIFY) &&
6420 ptr && check_hierarchical(pptr);
6422 for(ptr = uri->canon_uri; ptr < uri->canon_uri+uri->canon_len; ++ptr) {
6423 BOOL do_default_action = TRUE;
6425 /* Keep track of the path if we need to remove dot segments from
6426 * it later.
6428 if(reduce_path && !path && ptr == uri->canon_uri+uri->path_start)
6429 path = output+len;
6431 /* Check if it's time to reduce the path. */
6432 if(reduce_path && ptr == uri->canon_uri+uri->path_start+uri->path_len) {
6433 DWORD current_path_len = (output+len) - path;
6434 DWORD new_path_len = remove_dot_segments(path, current_path_len);
6436 /* Update the current length. */
6437 len -= (current_path_len-new_path_len);
6438 reduce_path = FALSE;
6441 if(*ptr == '%') {
6442 const WCHAR decoded = decode_pct_val(ptr);
6443 if(decoded) {
6444 if(allow_unescape && (flags & URL_UNESCAPE)) {
6445 if(len < output_len)
6446 output[len] = decoded;
6447 len++;
6448 ptr += 2;
6449 do_default_action = FALSE;
6453 /* See if %'s needed to encoded. */
6454 if(do_default_action && (flags & URL_ESCAPE_PERCENT)) {
6455 if(len + 3 < output_len)
6456 pct_encode_val(*ptr, output+len);
6457 len += 3;
6458 do_default_action = FALSE;
6460 } else if(*ptr == ' ') {
6461 if((flags & URL_ESCAPE_SPACES_ONLY) &&
6462 !(flags & URL_ESCAPE_UNSAFE)) {
6463 if(len + 3 < output_len)
6464 pct_encode_val(*ptr, output+len);
6465 len += 3;
6466 do_default_action = FALSE;
6468 } else if(is_ascii(*ptr) && !is_reserved(*ptr) && !is_unreserved(*ptr)) {
6469 if(flags & URL_ESCAPE_UNSAFE) {
6470 if(len + 3 < output_len)
6471 pct_encode_val(*ptr, output+len);
6472 len += 3;
6473 do_default_action = FALSE;
6477 if(do_default_action) {
6478 if(len < output_len)
6479 output[len] = *ptr;
6480 len++;
6484 /* Sometimes the path is the very last component of the IUri, so
6485 * see if the dot segments need to be reduced now.
6487 if(reduce_path && path) {
6488 DWORD current_path_len = (output+len) - path;
6489 DWORD new_path_len = remove_dot_segments(path, current_path_len);
6491 /* Update the current length. */
6492 len -= (current_path_len-new_path_len);
6495 if(len < output_len)
6496 output[len] = 0;
6497 else
6498 output[output_len-1] = 0;
6500 /* The null terminator isn't included in the length. */
6501 *result_len = len;
6502 if(len >= output_len)
6503 return STRSAFE_E_INSUFFICIENT_BUFFER;
6505 return S_OK;
6508 static HRESULT parse_friendly(IUri *uri, LPWSTR output, DWORD output_len,
6509 DWORD *result_len)
6511 HRESULT hr;
6512 DWORD display_len;
6513 BSTR display;
6515 hr = IUri_GetPropertyLength(uri, Uri_PROPERTY_DISPLAY_URI, &display_len, 0);
6516 if(FAILED(hr)) {
6517 *result_len = 0;
6518 return hr;
6521 *result_len = display_len;
6522 if(display_len+1 > output_len)
6523 return STRSAFE_E_INSUFFICIENT_BUFFER;
6525 hr = IUri_GetDisplayUri(uri, &display);
6526 if(FAILED(hr)) {
6527 *result_len = 0;
6528 return hr;
6531 memcpy(output, display, (display_len+1)*sizeof(WCHAR));
6532 SysFreeString(display);
6533 return S_OK;
6536 static HRESULT parse_rootdocument(const Uri *uri, LPWSTR output, DWORD output_len,
6537 DWORD *result_len)
6539 static const WCHAR colon_slashesW[] = {':','/','/'};
6541 WCHAR *ptr;
6542 DWORD len = 0;
6544 /* Windows only returns the root document if the URI has an authority
6545 * and it's not an unknown scheme type or a file scheme type.
6547 if(uri->authority_start == -1 ||
6548 uri->scheme_type == URL_SCHEME_UNKNOWN ||
6549 uri->scheme_type == URL_SCHEME_FILE) {
6550 *result_len = 0;
6551 if(!output_len)
6552 return STRSAFE_E_INSUFFICIENT_BUFFER;
6554 output[0] = 0;
6555 return S_OK;
6558 len = uri->scheme_len+uri->authority_len;
6559 /* For the "://" and '/' which will be added. */
6560 len += 4;
6562 if(len+1 > output_len) {
6563 *result_len = len;
6564 return STRSAFE_E_INSUFFICIENT_BUFFER;
6567 ptr = output;
6568 memcpy(ptr, uri->canon_uri+uri->scheme_start, uri->scheme_len*sizeof(WCHAR));
6570 /* Add the "://". */
6571 ptr += uri->scheme_len;
6572 memcpy(ptr, colon_slashesW, sizeof(colon_slashesW));
6574 /* Add the authority. */
6575 ptr += ARRAY_SIZE(colon_slashesW);
6576 memcpy(ptr, uri->canon_uri+uri->authority_start, uri->authority_len*sizeof(WCHAR));
6578 /* Add the '/' after the authority. */
6579 ptr += uri->authority_len;
6580 *ptr = '/';
6581 ptr[1] = 0;
6583 *result_len = len;
6584 return S_OK;
6587 static HRESULT parse_document(const Uri *uri, LPWSTR output, DWORD output_len,
6588 DWORD *result_len)
6590 DWORD len = 0;
6592 /* It has to be a known scheme type, but, it can't be a file
6593 * scheme. It also has to hierarchical.
6595 if(uri->scheme_type == URL_SCHEME_UNKNOWN ||
6596 uri->scheme_type == URL_SCHEME_FILE ||
6597 uri->authority_start == -1) {
6598 *result_len = 0;
6599 if(output_len < 1)
6600 return STRSAFE_E_INSUFFICIENT_BUFFER;
6602 output[0] = 0;
6603 return S_OK;
6606 if(uri->fragment_start > -1)
6607 len = uri->fragment_start;
6608 else
6609 len = uri->canon_len;
6611 *result_len = len;
6612 if(len+1 > output_len)
6613 return STRSAFE_E_INSUFFICIENT_BUFFER;
6615 memcpy(output, uri->canon_uri, len*sizeof(WCHAR));
6616 output[len] = 0;
6617 return S_OK;
6620 static HRESULT parse_path_from_url(const Uri *uri, LPWSTR output, DWORD output_len,
6621 DWORD *result_len)
6623 const WCHAR *path_ptr;
6624 WCHAR buffer[INTERNET_MAX_URL_LENGTH+1];
6625 WCHAR *ptr;
6627 if(uri->scheme_type != URL_SCHEME_FILE) {
6628 *result_len = 0;
6629 if(output_len > 0)
6630 output[0] = 0;
6631 return E_INVALIDARG;
6634 ptr = buffer;
6635 if(uri->host_start > -1) {
6636 static const WCHAR slash_slashW[] = {'\\','\\'};
6638 memcpy(ptr, slash_slashW, sizeof(slash_slashW));
6639 ptr += ARRAY_SIZE(slash_slashW);
6640 memcpy(ptr, uri->canon_uri+uri->host_start, uri->host_len*sizeof(WCHAR));
6641 ptr += uri->host_len;
6644 path_ptr = uri->canon_uri+uri->path_start;
6645 if(uri->path_len > 3 && *path_ptr == '/' && is_drive_path(path_ptr+1))
6646 /* Skip past the '/' in front of the drive path. */
6647 ++path_ptr;
6649 for(; path_ptr < uri->canon_uri+uri->path_start+uri->path_len; ++path_ptr, ++ptr) {
6650 BOOL do_default_action = TRUE;
6652 if(*path_ptr == '%') {
6653 const WCHAR decoded = decode_pct_val(path_ptr);
6654 if(decoded) {
6655 *ptr = decoded;
6656 path_ptr += 2;
6657 do_default_action = FALSE;
6659 } else if(*path_ptr == '/') {
6660 *ptr = '\\';
6661 do_default_action = FALSE;
6664 if(do_default_action)
6665 *ptr = *path_ptr;
6668 *ptr = 0;
6670 *result_len = ptr-buffer;
6671 if(*result_len+1 > output_len)
6672 return STRSAFE_E_INSUFFICIENT_BUFFER;
6674 memcpy(output, buffer, (*result_len+1)*sizeof(WCHAR));
6675 return S_OK;
6678 static HRESULT parse_url_from_path(IUri *uri, LPWSTR output, DWORD output_len,
6679 DWORD *result_len)
6681 HRESULT hr;
6682 BSTR received;
6683 DWORD len = 0;
6685 hr = IUri_GetPropertyLength(uri, Uri_PROPERTY_ABSOLUTE_URI, &len, 0);
6686 if(FAILED(hr)) {
6687 *result_len = 0;
6688 return hr;
6691 *result_len = len;
6692 if(len+1 > output_len)
6693 return STRSAFE_E_INSUFFICIENT_BUFFER;
6695 hr = IUri_GetAbsoluteUri(uri, &received);
6696 if(FAILED(hr)) {
6697 *result_len = 0;
6698 return hr;
6701 memcpy(output, received, (len+1)*sizeof(WCHAR));
6702 SysFreeString(received);
6704 return S_OK;
6707 static HRESULT parse_schema(IUri *uri, LPWSTR output, DWORD output_len,
6708 DWORD *result_len)
6710 HRESULT hr;
6711 DWORD len;
6712 BSTR received;
6714 hr = IUri_GetPropertyLength(uri, Uri_PROPERTY_SCHEME_NAME, &len, 0);
6715 if(FAILED(hr)) {
6716 *result_len = 0;
6717 return hr;
6720 *result_len = len;
6721 if(len+1 > output_len)
6722 return STRSAFE_E_INSUFFICIENT_BUFFER;
6724 hr = IUri_GetSchemeName(uri, &received);
6725 if(FAILED(hr)) {
6726 *result_len = 0;
6727 return hr;
6730 memcpy(output, received, (len+1)*sizeof(WCHAR));
6731 SysFreeString(received);
6733 return S_OK;
6736 static HRESULT parse_site(IUri *uri, LPWSTR output, DWORD output_len, DWORD *result_len)
6738 HRESULT hr;
6739 DWORD len;
6740 BSTR received;
6742 hr = IUri_GetPropertyLength(uri, Uri_PROPERTY_HOST, &len, 0);
6743 if(FAILED(hr)) {
6744 *result_len = 0;
6745 return hr;
6748 *result_len = len;
6749 if(len+1 > output_len)
6750 return STRSAFE_E_INSUFFICIENT_BUFFER;
6752 hr = IUri_GetHost(uri, &received);
6753 if(FAILED(hr)) {
6754 *result_len = 0;
6755 return hr;
6758 memcpy(output, received, (len+1)*sizeof(WCHAR));
6759 SysFreeString(received);
6761 return S_OK;
6764 static HRESULT parse_domain(IUri *uri, LPWSTR output, DWORD output_len, DWORD *result_len)
6766 HRESULT hr;
6767 DWORD len;
6768 BSTR received;
6770 hr = IUri_GetPropertyLength(uri, Uri_PROPERTY_DOMAIN, &len, 0);
6771 if(FAILED(hr)) {
6772 *result_len = 0;
6773 return hr;
6776 *result_len = len;
6777 if(len+1 > output_len)
6778 return STRSAFE_E_INSUFFICIENT_BUFFER;
6780 hr = IUri_GetDomain(uri, &received);
6781 if(FAILED(hr)) {
6782 *result_len = 0;
6783 return hr;
6786 memcpy(output, received, (len+1)*sizeof(WCHAR));
6787 SysFreeString(received);
6789 return S_OK;
6792 static HRESULT parse_anchor(IUri *uri, LPWSTR output, DWORD output_len, DWORD *result_len)
6794 HRESULT hr;
6795 DWORD len;
6796 BSTR received;
6798 hr = IUri_GetPropertyLength(uri, Uri_PROPERTY_FRAGMENT, &len, 0);
6799 if(FAILED(hr)) {
6800 *result_len = 0;
6801 return hr;
6804 *result_len = len;
6805 if(len+1 > output_len)
6806 return STRSAFE_E_INSUFFICIENT_BUFFER;
6808 hr = IUri_GetFragment(uri, &received);
6809 if(FAILED(hr)) {
6810 *result_len = 0;
6811 return hr;
6814 memcpy(output, received, (len+1)*sizeof(WCHAR));
6815 SysFreeString(received);
6817 return S_OK;
6820 /***********************************************************************
6821 * CoInternetParseIUri (urlmon.@)
6823 HRESULT WINAPI CoInternetParseIUri(IUri *pIUri, PARSEACTION ParseAction, DWORD dwFlags,
6824 LPWSTR pwzResult, DWORD cchResult, DWORD *pcchResult,
6825 DWORD_PTR dwReserved)
6827 HRESULT hr;
6828 Uri *uri;
6829 IInternetProtocolInfo *info;
6831 TRACE("(%p %d %lx %p %ld %p %Ix)\n", pIUri, ParseAction, dwFlags, pwzResult,
6832 cchResult, pcchResult, dwReserved);
6834 if(!pcchResult)
6835 return E_POINTER;
6837 if(!pwzResult || !pIUri) {
6838 *pcchResult = 0;
6839 return E_INVALIDARG;
6842 if(!(uri = get_uri_obj(pIUri))) {
6843 *pcchResult = 0;
6844 FIXME("(%p %d %lx %p %ld %p %Ix) Unknown IUri's not supported for this action.\n",
6845 pIUri, ParseAction, dwFlags, pwzResult, cchResult, pcchResult, dwReserved);
6846 return E_NOTIMPL;
6849 info = get_protocol_info(uri->canon_uri);
6850 if(info) {
6851 hr = IInternetProtocolInfo_ParseUrl(info, uri->canon_uri, ParseAction, dwFlags,
6852 pwzResult, cchResult, pcchResult, 0);
6853 IInternetProtocolInfo_Release(info);
6854 if(SUCCEEDED(hr)) return hr;
6857 switch(ParseAction) {
6858 case PARSE_CANONICALIZE:
6859 hr = parse_canonicalize(uri, dwFlags, pwzResult, cchResult, pcchResult);
6860 break;
6861 case PARSE_FRIENDLY:
6862 hr = parse_friendly(pIUri, pwzResult, cchResult, pcchResult);
6863 break;
6864 case PARSE_ROOTDOCUMENT:
6865 hr = parse_rootdocument(uri, pwzResult, cchResult, pcchResult);
6866 break;
6867 case PARSE_DOCUMENT:
6868 hr = parse_document(uri, pwzResult, cchResult, pcchResult);
6869 break;
6870 case PARSE_PATH_FROM_URL:
6871 hr = parse_path_from_url(uri, pwzResult, cchResult, pcchResult);
6872 break;
6873 case PARSE_URL_FROM_PATH:
6874 hr = parse_url_from_path(pIUri, pwzResult, cchResult, pcchResult);
6875 break;
6876 case PARSE_SCHEMA:
6877 hr = parse_schema(pIUri, pwzResult, cchResult, pcchResult);
6878 break;
6879 case PARSE_SITE:
6880 hr = parse_site(pIUri, pwzResult, cchResult, pcchResult);
6881 break;
6882 case PARSE_DOMAIN:
6883 hr = parse_domain(pIUri, pwzResult, cchResult, pcchResult);
6884 break;
6885 case PARSE_LOCATION:
6886 case PARSE_ANCHOR:
6887 hr = parse_anchor(pIUri, pwzResult, cchResult, pcchResult);
6888 break;
6889 case PARSE_SECURITY_URL:
6890 case PARSE_MIME:
6891 case PARSE_SERVER:
6892 case PARSE_SECURITY_DOMAIN:
6893 *pcchResult = 0;
6894 hr = E_FAIL;
6895 break;
6896 default:
6897 *pcchResult = 0;
6898 hr = E_NOTIMPL;
6899 FIXME("(%p %d %lx %p %ld %p %Ix) Partial stub.\n", pIUri, ParseAction, dwFlags,
6900 pwzResult, cchResult, pcchResult, dwReserved);
6903 return hr;