2 * Copyright 2010 Jacek Caban for CodeWeavers
3 * Copyright 2010 Thomas Mullaly
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #include "urlmon_main.h"
21 #include "wine/debug.h"
23 #define NO_SHLWAPI_REG
28 #define UINT_MAX 0xffffffff
29 #define USHORT_MAX 0xffff
31 #define URI_DISPLAY_NO_ABSOLUTE_URI 0x1
32 #define URI_DISPLAY_NO_DEFAULT_PORT_AUTH 0x2
34 #define ALLOW_NULL_TERM_SCHEME 0x01
35 #define ALLOW_NULL_TERM_USER_NAME 0x02
36 #define ALLOW_NULL_TERM_PASSWORD 0x04
37 #define ALLOW_BRACKETLESS_IP_LITERAL 0x08
38 #define SKIP_IP_FUTURE_CHECK 0x10
39 #define IGNORE_PORT_DELIMITER 0x20
41 #define RAW_URI_FORCE_PORT_DISP 0x1
42 #define RAW_URI_CONVERT_TO_DOS_PATH 0x2
44 #define COMBINE_URI_FORCE_FLAG_USE 0x1
46 WINE_DEFAULT_DEBUG_CHANNEL(urlmon);
48 static const IID IID_IUriObj = {0x4b364760,0x9f51,0x11df,{0x98,0x1c,0x08,0x00,0x20,0x0c,0x9a,0x66}};
52 IUriBuilderFactory IUriBuilderFactory_iface;
58 /* Information about the canonicalized URI's buffer. */
62 BOOL display_modifiers;
67 URL_SCHEME scheme_type;
75 Uri_HOST_TYPE host_type;
98 IUriBuilder IUriBuilder_iface;
102 DWORD modified_props;
135 /* IPv6 addresses can hold up to 8 h16 components. */
139 /* An IPv6 can have 1 elision ("::"). */
140 const WCHAR *elision;
142 /* An IPv6 can contain 1 IPv4 address as the last 32bits of the address. */
155 BOOL has_implicit_scheme;
156 BOOL has_implicit_ip;
162 URL_SCHEME scheme_type;
164 const WCHAR *username;
167 const WCHAR *password;
172 Uri_HOST_TYPE host_type;
175 ipv6_address ipv6_address;
188 const WCHAR *fragment;
192 static const CHAR hexDigits[] = "0123456789ABCDEF";
194 /* List of scheme types/scheme names that are recognized by the IUri interface as of IE 7. */
195 static const struct {
197 WCHAR scheme_name[16];
198 } recognized_schemes[] = {
199 {URL_SCHEME_FTP, {'f','t','p',0}},
200 {URL_SCHEME_HTTP, {'h','t','t','p',0}},
201 {URL_SCHEME_GOPHER, {'g','o','p','h','e','r',0}},
202 {URL_SCHEME_MAILTO, {'m','a','i','l','t','o',0}},
203 {URL_SCHEME_NEWS, {'n','e','w','s',0}},
204 {URL_SCHEME_NNTP, {'n','n','t','p',0}},
205 {URL_SCHEME_TELNET, {'t','e','l','n','e','t',0}},
206 {URL_SCHEME_WAIS, {'w','a','i','s',0}},
207 {URL_SCHEME_FILE, {'f','i','l','e',0}},
208 {URL_SCHEME_MK, {'m','k',0}},
209 {URL_SCHEME_HTTPS, {'h','t','t','p','s',0}},
210 {URL_SCHEME_SHELL, {'s','h','e','l','l',0}},
211 {URL_SCHEME_SNEWS, {'s','n','e','w','s',0}},
212 {URL_SCHEME_LOCAL, {'l','o','c','a','l',0}},
213 {URL_SCHEME_JAVASCRIPT, {'j','a','v','a','s','c','r','i','p','t',0}},
214 {URL_SCHEME_VBSCRIPT, {'v','b','s','c','r','i','p','t',0}},
215 {URL_SCHEME_ABOUT, {'a','b','o','u','t',0}},
216 {URL_SCHEME_RES, {'r','e','s',0}},
217 {URL_SCHEME_MSSHELLROOTED, {'m','s','-','s','h','e','l','l','-','r','o','o','t','e','d',0}},
218 {URL_SCHEME_MSSHELLIDLIST, {'m','s','-','s','h','e','l','l','-','i','d','l','i','s','t',0}},
219 {URL_SCHEME_MSHELP, {'h','c','p',0}},
220 {URL_SCHEME_WILDCARD, {'*',0}}
223 /* List of default ports Windows recognizes. */
224 static const struct {
227 } default_ports[] = {
228 {URL_SCHEME_FTP, 21},
229 {URL_SCHEME_HTTP, 80},
230 {URL_SCHEME_GOPHER, 70},
231 {URL_SCHEME_NNTP, 119},
232 {URL_SCHEME_TELNET, 23},
233 {URL_SCHEME_WAIS, 210},
234 {URL_SCHEME_HTTPS, 443},
237 /* List of 3-character top level domain names Windows seems to recognize.
238 * There might be more, but, these are the only ones I've found so far.
240 static const struct {
242 } recognized_tlds[] = {
252 static Uri *get_uri_obj(IUri *uri)
257 hres = IUri_QueryInterface(uri, &IID_IUriObj, (void**)&ret);
258 return SUCCEEDED(hres) ? ret : NULL;
261 static inline BOOL is_alpha(WCHAR val) {
262 return ((val >= 'a' && val <= 'z') || (val >= 'A' && val <= 'Z'));
265 static inline BOOL is_num(WCHAR val) {
266 return (val >= '0' && val <= '9');
269 static inline BOOL is_drive_path(const WCHAR *str) {
270 return (is_alpha(str[0]) && (str[1] == ':' || str[1] == '|'));
273 static inline BOOL is_unc_path(const WCHAR *str) {
274 return (str[0] == '\\' && str[0] == '\\');
277 static inline BOOL is_forbidden_dos_path_char(WCHAR val) {
278 return (val == '>' || val == '<' || val == '\"');
281 /* A URI is implicitly a file path if it begins with
282 * a drive letter (e.g. X:) or starts with "\\" (UNC path).
284 static inline BOOL is_implicit_file_path(const WCHAR *str) {
285 return (is_unc_path(str) || (is_alpha(str[0]) && str[1] == ':'));
288 /* Checks if the URI is a hierarchical URI. A hierarchical
289 * URI is one that has "//" after the scheme.
291 static BOOL check_hierarchical(const WCHAR **ptr) {
292 const WCHAR *start = *ptr;
307 /* unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~" */
308 static inline BOOL is_unreserved(WCHAR val) {
309 return (is_alpha(val) || is_num(val) || val == '-' || val == '.' ||
310 val == '_' || val == '~');
313 /* sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
314 * / "*" / "+" / "," / ";" / "="
316 static inline BOOL is_subdelim(WCHAR val) {
317 return (val == '!' || val == '$' || val == '&' ||
318 val == '\'' || val == '(' || val == ')' ||
319 val == '*' || val == '+' || val == ',' ||
320 val == ';' || val == '=');
323 /* gen-delims = ":" / "/" / "?" / "#" / "[" / "]" / "@" */
324 static inline BOOL is_gendelim(WCHAR val) {
325 return (val == ':' || val == '/' || val == '?' ||
326 val == '#' || val == '[' || val == ']' ||
330 /* Characters that delimit the end of the authority
331 * section of a URI. Sometimes a '\\' is considered
332 * an authority delimiter.
334 static inline BOOL is_auth_delim(WCHAR val, BOOL acceptSlash) {
335 return (val == '#' || val == '/' || val == '?' ||
336 val == '\0' || (acceptSlash && val == '\\'));
339 /* reserved = gen-delims / sub-delims */
340 static inline BOOL is_reserved(WCHAR val) {
341 return (is_subdelim(val) || is_gendelim(val));
344 static inline BOOL is_hexdigit(WCHAR val) {
345 return ((val >= 'a' && val <= 'f') ||
346 (val >= 'A' && val <= 'F') ||
347 (val >= '0' && val <= '9'));
350 static inline BOOL is_path_delim(WCHAR val) {
351 return (!val || val == '#' || val == '?');
354 static inline BOOL is_slash(WCHAR c)
356 return c == '/' || c == '\\';
359 static BOOL is_default_port(URL_SCHEME scheme, DWORD port) {
362 for(i = 0; i < sizeof(default_ports)/sizeof(default_ports[0]); ++i) {
363 if(default_ports[i].scheme == scheme && default_ports[i].port)
370 /* List of schemes types Windows seems to expect to be hierarchical. */
371 static inline BOOL is_hierarchical_scheme(URL_SCHEME type) {
372 return(type == URL_SCHEME_HTTP || type == URL_SCHEME_FTP ||
373 type == URL_SCHEME_GOPHER || type == URL_SCHEME_NNTP ||
374 type == URL_SCHEME_TELNET || type == URL_SCHEME_WAIS ||
375 type == URL_SCHEME_FILE || type == URL_SCHEME_HTTPS ||
376 type == URL_SCHEME_RES);
379 /* Checks if 'flags' contains an invalid combination of Uri_CREATE flags. */
380 static inline BOOL has_invalid_flag_combination(DWORD flags) {
381 return((flags & Uri_CREATE_DECODE_EXTRA_INFO && flags & Uri_CREATE_NO_DECODE_EXTRA_INFO) ||
382 (flags & Uri_CREATE_CANONICALIZE && flags & Uri_CREATE_NO_CANONICALIZE) ||
383 (flags & Uri_CREATE_CRACK_UNKNOWN_SCHEMES && flags & Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES) ||
384 (flags & Uri_CREATE_PRE_PROCESS_HTML_URI && flags & Uri_CREATE_NO_PRE_PROCESS_HTML_URI) ||
385 (flags & Uri_CREATE_IE_SETTINGS && flags & Uri_CREATE_NO_IE_SETTINGS));
388 /* Applies each default Uri_CREATE flags to 'flags' if it
389 * doesn't cause a flag conflict.
391 static void apply_default_flags(DWORD *flags) {
392 if(!(*flags & Uri_CREATE_NO_CANONICALIZE))
393 *flags |= Uri_CREATE_CANONICALIZE;
394 if(!(*flags & Uri_CREATE_NO_DECODE_EXTRA_INFO))
395 *flags |= Uri_CREATE_DECODE_EXTRA_INFO;
396 if(!(*flags & Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES))
397 *flags |= Uri_CREATE_CRACK_UNKNOWN_SCHEMES;
398 if(!(*flags & Uri_CREATE_NO_PRE_PROCESS_HTML_URI))
399 *flags |= Uri_CREATE_PRE_PROCESS_HTML_URI;
400 if(!(*flags & Uri_CREATE_IE_SETTINGS))
401 *flags |= Uri_CREATE_NO_IE_SETTINGS;
404 /* Determines if the URI is hierarchical using the information already parsed into
405 * data and using the current location of parsing in the URI string.
407 * Windows considers a URI hierarchical if one of the following is true:
408 * A.) It's a wildcard scheme.
409 * B.) It's an implicit file scheme.
410 * C.) It's a known hierarchical scheme and it has two '\\' after the scheme name.
411 * (the '\\' will be converted into "//" during canonicalization).
412 * D.) It's not a relative URI and "//" appears after the scheme name.
414 static inline BOOL is_hierarchical_uri(const WCHAR **ptr, const parse_data *data) {
415 const WCHAR *start = *ptr;
417 if(data->scheme_type == URL_SCHEME_WILDCARD)
419 else if(data->scheme_type == URL_SCHEME_FILE && data->has_implicit_scheme)
421 else if(is_hierarchical_scheme(data->scheme_type) && (*ptr)[0] == '\\' && (*ptr)[1] == '\\') {
424 } else if(!data->is_relative && check_hierarchical(ptr))
431 /* Computes the size of the given IPv6 address.
432 * Each h16 component is 16 bits. If there is an IPv4 address, it's
433 * 32 bits. If there's an elision it can be 16 to 128 bits, depending
434 * on the number of other components.
436 * Modeled after google-url's CheckIPv6ComponentsSize function
438 static void compute_ipv6_comps_size(ipv6_address *address) {
439 address->components_size = address->h16_count * 2;
442 /* IPv4 address is 4 bytes. */
443 address->components_size += 4;
445 if(address->elision) {
446 /* An elision can be anywhere from 2 bytes up to 16 bytes.
447 * Its size depends on the size of the h16 and IPv4 components.
449 address->elision_size = 16 - address->components_size;
450 if(address->elision_size < 2)
451 address->elision_size = 2;
453 address->elision_size = 0;
456 /* Taken from dlls/jscript/lex.c */
457 static int hex_to_int(WCHAR val) {
458 if(val >= '0' && val <= '9')
460 else if(val >= 'a' && val <= 'f')
461 return val - 'a' + 10;
462 else if(val >= 'A' && val <= 'F')
463 return val - 'A' + 10;
468 /* Helper function for converting a percent encoded string
469 * representation of a WCHAR value into its actual WCHAR value. If
470 * the two characters following the '%' aren't valid hex values then
471 * this function returns the NULL character.
474 * "%2E" will result in '.' being returned by this function.
476 static WCHAR decode_pct_val(const WCHAR *ptr) {
479 if(*ptr == '%' && is_hexdigit(*(ptr + 1)) && is_hexdigit(*(ptr + 2))) {
480 INT a = hex_to_int(*(ptr + 1));
481 INT b = hex_to_int(*(ptr + 2));
490 /* Helper function for percent encoding a given character
491 * and storing the encoded value into a given buffer (dest).
493 * It's up to the calling function to ensure that there is
494 * at least enough space in 'dest' for the percent encoded
495 * value to be stored (so dest + 3 spaces available).
497 static inline void pct_encode_val(WCHAR val, WCHAR *dest) {
499 dest[1] = hexDigits[(val >> 4) & 0xf];
500 dest[2] = hexDigits[val & 0xf];
503 /* Attempts to parse the domain name from the host.
505 * This function also includes the Top-level Domain (TLD) name
506 * of the host when it tries to find the domain name. If it finds
507 * a valid domain name it will assign 'domain_start' the offset
508 * into 'host' where the domain name starts.
510 * It's implied that if there is a domain name its range is:
511 * [host+domain_start, host+host_len).
513 void find_domain_name(const WCHAR *host, DWORD host_len,
515 const WCHAR *last_tld, *sec_last_tld, *end;
517 end = host+host_len-1;
521 /* There has to be at least enough room for a '.' followed by a
522 * 3-character TLD for a domain to even exist in the host name.
527 last_tld = memrchrW(host, '.', host_len);
529 /* http://hostname -> has no domain name. */
532 sec_last_tld = memrchrW(host, '.', last_tld-host);
534 /* If the '.' is at the beginning of the host there
535 * has to be at least 3 characters in the TLD for it
537 * Ex: .com -> .com as the domain name.
538 * .co -> has no domain name.
540 if(last_tld-host == 0) {
541 if(end-(last_tld-1) < 3)
543 } else if(last_tld-host == 3) {
546 /* If there are three characters in front of last_tld and
547 * they are on the list of recognized TLDs, then this
548 * host doesn't have a domain (since the host only contains
550 * Ex: edu.uk -> has no domain name.
551 * foo.uk -> foo.uk as the domain name.
553 for(i = 0; i < sizeof(recognized_tlds)/sizeof(recognized_tlds[0]); ++i) {
554 if(!StrCmpNIW(host, recognized_tlds[i].tld_name, 3))
557 } else if(last_tld-host < 3)
558 /* Anything less than 3 characters is considered part
560 * Ex: ak.uk -> Has no domain name.
564 /* Otherwise the domain name is the whole host name. */
566 } else if(end+1-last_tld > 3) {
567 /* If the last_tld has more than 3 characters, then it's automatically
568 * considered the TLD of the domain name.
569 * Ex: www.winehq.org.uk.test -> uk.test as the domain name.
571 *domain_start = (sec_last_tld+1)-host;
572 } else if(last_tld - (sec_last_tld+1) < 4) {
574 /* If the sec_last_tld is 3 characters long it HAS to be on the list of
575 * recognized to still be considered part of the TLD name, otherwise
576 * its considered the domain name.
577 * Ex: www.google.com.uk -> google.com.uk as the domain name.
578 * www.google.foo.uk -> foo.uk as the domain name.
580 if(last_tld - (sec_last_tld+1) == 3) {
581 for(i = 0; i < sizeof(recognized_tlds)/sizeof(recognized_tlds[0]); ++i) {
582 if(!StrCmpNIW(sec_last_tld+1, recognized_tlds[i].tld_name, 3)) {
583 const WCHAR *domain = memrchrW(host, '.', sec_last_tld-host);
588 *domain_start = (domain+1) - host;
589 TRACE("Found domain name %s\n", debugstr_wn(host+*domain_start,
590 (host+host_len)-(host+*domain_start)));
595 *domain_start = (sec_last_tld+1)-host;
597 /* Since the sec_last_tld is less than 3 characters it's considered
599 * Ex: www.google.fo.uk -> google.fo.uk as the domain name.
601 const WCHAR *domain = memrchrW(host, '.', sec_last_tld-host);
606 *domain_start = (domain+1) - host;
609 /* The second to last TLD has more than 3 characters making it
611 * Ex: www.google.test.us -> test.us as the domain name.
613 *domain_start = (sec_last_tld+1)-host;
616 TRACE("Found domain name %s\n", debugstr_wn(host+*domain_start,
617 (host+host_len)-(host+*domain_start)));
620 /* Removes the dot segments from a hierarchical URIs path component. This
621 * function performs the removal in place.
623 * This function returns the new length of the path string.
625 static DWORD remove_dot_segments(WCHAR *path, DWORD path_len) {
627 const WCHAR *in = out;
628 const WCHAR *end = out + path_len;
632 /* Move the first path segment in the input buffer to the end of
633 * the output buffer, and any subsequent characters up to, including
634 * the next "/" character (if any) or the end of the input buffer.
636 while(in < end && !is_slash(*in))
646 /* Handle ending "/." */
653 if(is_slash(in[1])) {
658 /* If we don't have "/../" or ending "/.." */
659 if(in[1] != '.' || (in + 2 != end && !is_slash(in[2])))
662 /* Find the slash preceding out pointer and move out pointer to it */
663 if(out > path+1 && is_slash(*--out))
665 while(out > path && !is_slash(*(--out)));
675 TRACE("(%p %d): Path after dot segments removed %s len=%d\n", path, path_len,
676 debugstr_wn(path, len), len);
680 /* Attempts to find the file extension in a given path. */
681 static INT find_file_extension(const WCHAR *path, DWORD path_len) {
684 for(end = path+path_len-1; end >= path && *end != '/' && *end != '\\'; --end) {
692 /* Computes the location where the elision should occur in the IPv6
693 * address using the numerical values of each component stored in
694 * 'values'. If the address shouldn't contain an elision then 'index'
695 * is assigned -1 as its value. Otherwise 'index' will contain the
696 * starting index (into values) where the elision should be, and 'count'
697 * will contain the number of cells the elision covers.
700 * Windows will expand an elision if the elision only represents one h16
701 * component of the address.
703 * Ex: [1::2:3:4:5:6:7] -> [1:0:2:3:4:5:6:7]
705 * If the IPv6 address contains an IPv4 address, the IPv4 address is also
706 * considered for being included as part of an elision if all its components
709 * Ex: [1:2:3:4:5:6:0.0.0.0] -> [1:2:3:4:5:6::]
711 static void compute_elision_location(const ipv6_address *address, const USHORT values[8],
712 INT *index, DWORD *count) {
713 DWORD i, max_len, cur_len;
714 INT max_index, cur_index;
716 max_len = cur_len = 0;
717 max_index = cur_index = -1;
718 for(i = 0; i < 8; ++i) {
719 BOOL check_ipv4 = (address->ipv4 && i == 6);
720 BOOL is_end = (check_ipv4 || i == 7);
723 /* Check if the IPv4 address contains only zeros. */
724 if(values[i] == 0 && values[i+1] == 0) {
731 } else if(values[i] == 0) {
738 if(is_end || values[i] != 0) {
739 /* We only consider it for an elision if it's
740 * more than 1 component long.
742 if(cur_len > 1 && cur_len > max_len) {
743 /* Found the new elision location. */
745 max_index = cur_index;
748 /* Reset the current range for the next range of zeros. */
758 /* Removes all the leading and trailing white spaces or
759 * control characters from the URI and removes all control
760 * characters inside of the URI string.
762 static BSTR pre_process_uri(LPCWSTR uri) {
763 const WCHAR *start, *end, *ptr;
769 /* Skip leading controls and whitespace. */
770 while(*start && (iscntrlW(*start) || isspaceW(*start))) ++start;
772 /* URI consisted only of control/whitespace. */
774 return SysAllocStringLen(NULL, 0);
776 end = start + strlenW(start);
777 while(--end > start && (iscntrlW(*end) || isspaceW(*end)));
780 for(ptr = start; ptr < end; ptr++) {
785 ret = SysAllocStringLen(NULL, len);
789 for(ptr = start, ptr2=ret; ptr < end; ptr++) {
797 /* Converts the specified IPv4 address into an uint value.
799 * This function assumes that the IPv4 address has already been validated.
801 static UINT ipv4toui(const WCHAR *ip, DWORD len) {
803 DWORD comp_value = 0;
806 for(ptr = ip; ptr < ip+len; ++ptr) {
812 comp_value = comp_value*10 + (*ptr-'0');
821 /* Converts an IPv4 address in numerical form into its fully qualified
822 * string form. This function returns the number of characters written
823 * to 'dest'. If 'dest' is NULL this function will return the number of
824 * characters that would have been written.
826 * It's up to the caller to ensure there's enough space in 'dest' for the
829 static DWORD ui2ipv4(WCHAR *dest, UINT address) {
830 static const WCHAR formatW[] =
831 {'%','u','.','%','u','.','%','u','.','%','u',0};
835 digits[0] = (address >> 24) & 0xff;
836 digits[1] = (address >> 16) & 0xff;
837 digits[2] = (address >> 8) & 0xff;
838 digits[3] = address & 0xff;
842 ret = sprintfW(tmp, formatW, digits[0], digits[1], digits[2], digits[3]);
844 ret = sprintfW(dest, formatW, digits[0], digits[1], digits[2], digits[3]);
849 static DWORD ui2str(WCHAR *dest, UINT value) {
850 static const WCHAR formatW[] = {'%','u',0};
855 ret = sprintfW(tmp, formatW, value);
857 ret = sprintfW(dest, formatW, value);
862 /* Converts a h16 component (from an IPv6 address) into its
865 * This function assumes that the h16 component has already been validated.
867 static USHORT h16tous(h16 component) {
871 for(i = 0; i < component.len; ++i) {
873 ret += hex_to_int(component.str[i]);
879 /* Converts an IPv6 address into its 128 bits (16 bytes) numerical value.
881 * This function assumes that the ipv6_address has already been validated.
883 static BOOL ipv6_to_number(const ipv6_address *address, USHORT number[8]) {
884 DWORD i, cur_component = 0;
885 BOOL already_passed_elision = FALSE;
887 for(i = 0; i < address->h16_count; ++i) {
888 if(address->elision) {
889 if(address->components[i].str > address->elision && !already_passed_elision) {
890 /* Means we just passed the elision and need to add its values to
891 * 'number' before we do anything else.
894 for(j = 0; j < address->elision_size; j+=2)
895 number[cur_component++] = 0;
897 already_passed_elision = TRUE;
901 number[cur_component++] = h16tous(address->components[i]);
904 /* Case when the elision appears after the h16 components. */
905 if(!already_passed_elision && address->elision) {
906 for(i = 0; i < address->elision_size; i+=2)
907 number[cur_component++] = 0;
911 UINT value = ipv4toui(address->ipv4, address->ipv4_len);
913 if(cur_component != 6) {
914 ERR("(%p %p): Failed sanity check with %d\n", address, number, cur_component);
918 number[cur_component++] = (value >> 16) & 0xffff;
919 number[cur_component] = value & 0xffff;
925 /* Checks if the characters pointed to by 'ptr' are
926 * a percent encoded data octet.
928 * pct-encoded = "%" HEXDIG HEXDIG
930 static BOOL check_pct_encoded(const WCHAR **ptr) {
931 const WCHAR *start = *ptr;
937 if(!is_hexdigit(**ptr)) {
943 if(!is_hexdigit(**ptr)) {
952 /* dec-octet = DIGIT ; 0-9
953 * / %x31-39 DIGIT ; 10-99
954 * / "1" 2DIGIT ; 100-199
955 * / "2" %x30-34 DIGIT ; 200-249
956 * / "25" %x30-35 ; 250-255
958 static BOOL check_dec_octet(const WCHAR **ptr) {
959 const WCHAR *c1, *c2, *c3;
962 /* A dec-octet must be at least 1 digit long. */
963 if(*c1 < '0' || *c1 > '9')
969 /* Since the 1-digit requirement was met, it doesn't
970 * matter if this is a DIGIT value, it's considered a
973 if(*c2 < '0' || *c2 > '9')
979 /* Same explanation as above. */
980 if(*c3 < '0' || *c3 > '9')
983 /* Anything > 255 isn't a valid IP dec-octet. */
984 if(*c1 >= '2' && *c2 >= '5' && *c3 >= '5') {
993 /* Checks if there is an implicit IPv4 address in the host component of the URI.
994 * The max value of an implicit IPv4 address is UINT_MAX.
997 * "234567" would be considered an implicit IPv4 address.
999 static BOOL check_implicit_ipv4(const WCHAR **ptr, UINT *val) {
1000 const WCHAR *start = *ptr;
1004 while(is_num(**ptr)) {
1005 ret = ret*10 + (**ptr - '0');
1007 if(ret > UINT_MAX) {
1021 /* Checks if the string contains an IPv4 address.
1023 * This function has a strict mode or a non-strict mode of operation
1024 * When 'strict' is set to FALSE this function will return TRUE if
1025 * the string contains at least 'dec-octet "." dec-octet' since partial
1026 * IPv4 addresses will be normalized out into full IPv4 addresses. When
1027 * 'strict' is set this function expects there to be a full IPv4 address.
1029 * IPv4address = dec-octet "." dec-octet "." dec-octet "." dec-octet
1031 static BOOL check_ipv4address(const WCHAR **ptr, BOOL strict) {
1032 const WCHAR *start = *ptr;
1034 if(!check_dec_octet(ptr)) {
1045 if(!check_dec_octet(ptr)) {
1059 if(!check_dec_octet(ptr)) {
1073 if(!check_dec_octet(ptr)) {
1078 /* Found a four digit ip address. */
1081 /* Tries to parse the scheme name of the URI.
1083 * scheme = ALPHA *(ALPHA | NUM | '+' | '-' | '.') as defined by RFC 3896.
1084 * NOTE: Windows accepts a number as the first character of a scheme.
1086 static BOOL parse_scheme_name(const WCHAR **ptr, parse_data *data, DWORD extras) {
1087 const WCHAR *start = *ptr;
1089 data->scheme = NULL;
1090 data->scheme_len = 0;
1093 if(**ptr == '*' && *ptr == start) {
1094 /* Might have found a wildcard scheme. If it is the next
1095 * char has to be a ':' for it to be a valid URI
1099 } else if(!is_num(**ptr) && !is_alpha(**ptr) && **ptr != '+' &&
1100 **ptr != '-' && **ptr != '.')
1109 /* Schemes must end with a ':' */
1110 if(**ptr != ':' && !((extras & ALLOW_NULL_TERM_SCHEME) && !**ptr)) {
1115 data->scheme = start;
1116 data->scheme_len = *ptr - start;
1122 /* Tries to deduce the corresponding URL_SCHEME for the given URI. Stores
1123 * the deduced URL_SCHEME in data->scheme_type.
1125 static BOOL parse_scheme_type(parse_data *data) {
1126 /* If there's scheme data then see if it's a recognized scheme. */
1127 if(data->scheme && data->scheme_len) {
1130 for(i = 0; i < sizeof(recognized_schemes)/sizeof(recognized_schemes[0]); ++i) {
1131 if(lstrlenW(recognized_schemes[i].scheme_name) == data->scheme_len) {
1132 /* Has to be a case insensitive compare. */
1133 if(!StrCmpNIW(recognized_schemes[i].scheme_name, data->scheme, data->scheme_len)) {
1134 data->scheme_type = recognized_schemes[i].scheme;
1140 /* If we get here it means it's not a recognized scheme. */
1141 data->scheme_type = URL_SCHEME_UNKNOWN;
1143 } else if(data->is_relative) {
1144 /* Relative URI's have no scheme. */
1145 data->scheme_type = URL_SCHEME_UNKNOWN;
1148 /* Should never reach here! what happened... */
1149 FIXME("(%p): Unable to determine scheme type for URI %s\n", data, debugstr_w(data->uri));
1154 /* Tries to parse (or deduce) the scheme_name of a URI. If it can't
1155 * parse a scheme from the URI it will try to deduce the scheme_name and scheme_type
1156 * using the flags specified in 'flags' (if any). Flags that affect how this function
1157 * operates are the Uri_CREATE_ALLOW_* flags.
1159 * All parsed/deduced information will be stored in 'data' when the function returns.
1161 * Returns TRUE if it was able to successfully parse the information.
1163 static BOOL parse_scheme(const WCHAR **ptr, parse_data *data, DWORD flags, DWORD extras) {
1164 static const WCHAR fileW[] = {'f','i','l','e',0};
1165 static const WCHAR wildcardW[] = {'*',0};
1167 /* First check to see if the uri could implicitly be a file path. */
1168 if(is_implicit_file_path(*ptr)) {
1169 if(flags & Uri_CREATE_ALLOW_IMPLICIT_FILE_SCHEME) {
1170 data->scheme = fileW;
1171 data->scheme_len = lstrlenW(fileW);
1172 data->has_implicit_scheme = TRUE;
1174 TRACE("(%p %p %x): URI is an implicit file path.\n", ptr, data, flags);
1176 /* Windows does not consider anything that can implicitly be a file
1177 * path to be a valid URI if the ALLOW_IMPLICIT_FILE_SCHEME flag is not set...
1179 TRACE("(%p %p %x): URI is implicitly a file path, but, the ALLOW_IMPLICIT_FILE_SCHEME flag wasn't set.\n",
1183 } else if(!parse_scheme_name(ptr, data, extras)) {
1184 /* No scheme was found, this means it could be:
1185 * a) an implicit Wildcard scheme
1187 * c) an invalid URI.
1189 if(flags & Uri_CREATE_ALLOW_IMPLICIT_WILDCARD_SCHEME) {
1190 data->scheme = wildcardW;
1191 data->scheme_len = lstrlenW(wildcardW);
1192 data->has_implicit_scheme = TRUE;
1194 TRACE("(%p %p %x): URI is an implicit wildcard scheme.\n", ptr, data, flags);
1195 } else if (flags & Uri_CREATE_ALLOW_RELATIVE) {
1196 data->is_relative = TRUE;
1197 TRACE("(%p %p %x): URI is relative.\n", ptr, data, flags);
1199 TRACE("(%p %p %x): Malformed URI found. Unable to deduce scheme name.\n", ptr, data, flags);
1204 if(!data->is_relative)
1205 TRACE("(%p %p %x): Found scheme=%s scheme_len=%d\n", ptr, data, flags,
1206 debugstr_wn(data->scheme, data->scheme_len), data->scheme_len);
1208 if(!parse_scheme_type(data))
1211 TRACE("(%p %p %x): Assigned %d as the URL_SCHEME.\n", ptr, data, flags, data->scheme_type);
1215 static BOOL parse_username(const WCHAR **ptr, parse_data *data, DWORD flags, DWORD extras) {
1216 data->username = *ptr;
1218 while(**ptr != ':' && **ptr != '@') {
1220 if(!check_pct_encoded(ptr)) {
1221 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
1222 *ptr = data->username;
1223 data->username = NULL;
1228 } else if(extras & ALLOW_NULL_TERM_USER_NAME && !**ptr)
1230 else if(is_auth_delim(**ptr, data->scheme_type != URL_SCHEME_UNKNOWN)) {
1231 *ptr = data->username;
1232 data->username = NULL;
1239 data->username_len = *ptr - data->username;
1243 static BOOL parse_password(const WCHAR **ptr, parse_data *data, DWORD flags, DWORD extras) {
1244 data->password = *ptr;
1246 while(**ptr != '@') {
1248 if(!check_pct_encoded(ptr)) {
1249 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
1250 *ptr = data->password;
1251 data->password = NULL;
1256 } else if(extras & ALLOW_NULL_TERM_PASSWORD && !**ptr)
1258 else if(is_auth_delim(**ptr, data->scheme_type != URL_SCHEME_UNKNOWN)) {
1259 *ptr = data->password;
1260 data->password = NULL;
1267 data->password_len = *ptr - data->password;
1271 /* Parses the userinfo part of the URI (if it exists). The userinfo field of
1272 * a URI can consist of "username:password@", or just "username@".
1275 * userinfo = *( unreserved / pct-encoded / sub-delims / ":" )
1278 * 1) If there is more than one ':' in the userinfo part of the URI Windows
1279 * uses the first occurrence of ':' to delimit the username and password
1283 * ftp://user:pass:word@winehq.org
1285 * would yield "user" as the username and "pass:word" as the password.
1287 * 2) Windows allows any character to appear in the "userinfo" part of
1288 * a URI, as long as it's not an authority delimiter character set.
1290 static void parse_userinfo(const WCHAR **ptr, parse_data *data, DWORD flags) {
1291 const WCHAR *start = *ptr;
1293 if(!parse_username(ptr, data, flags, 0)) {
1294 TRACE("(%p %p %x): URI contained no userinfo.\n", ptr, data, flags);
1300 if(!parse_password(ptr, data, flags, 0)) {
1302 data->username = NULL;
1303 data->username_len = 0;
1304 TRACE("(%p %p %x): URI contained no userinfo.\n", ptr, data, flags);
1311 data->username = NULL;
1312 data->username_len = 0;
1313 data->password = NULL;
1314 data->password_len = 0;
1316 TRACE("(%p %p %x): URI contained no userinfo.\n", ptr, data, flags);
1321 TRACE("(%p %p %x): Found username %s len=%d.\n", ptr, data, flags,
1322 debugstr_wn(data->username, data->username_len), data->username_len);
1325 TRACE("(%p %p %x): Found password %s len=%d.\n", ptr, data, flags,
1326 debugstr_wn(data->password, data->password_len), data->password_len);
1331 /* Attempts to parse a port from the URI.
1334 * Windows seems to have a cap on what the maximum value
1335 * for a port can be. The max value is USHORT_MAX.
1339 static BOOL parse_port(const WCHAR **ptr, parse_data *data, DWORD flags) {
1343 while(!is_auth_delim(**ptr, data->scheme_type != URL_SCHEME_UNKNOWN)) {
1344 if(!is_num(**ptr)) {
1350 port = port*10 + (**ptr-'0');
1352 if(port > USHORT_MAX) {
1361 data->has_port = TRUE;
1362 data->port_value = port;
1363 data->port_len = *ptr - data->port;
1365 TRACE("(%p %p %x): Found port %s len=%d value=%u\n", ptr, data, flags,
1366 debugstr_wn(data->port, data->port_len), data->port_len, data->port_value);
1370 /* Attempts to parse a IPv4 address from the URI.
1373 * Windows normalizes IPv4 addresses, This means there are three
1374 * possibilities for the URI to contain an IPv4 address.
1375 * 1) A well formed address (ex. 192.2.2.2).
1376 * 2) A partially formed address. For example "192.0" would
1377 * normalize to "192.0.0.0" during canonicalization.
1378 * 3) An implicit IPv4 address. For example "256" would
1379 * normalize to "0.0.1.0" during canonicalization. Also
1380 * note that the maximum value for an implicit IP address
1381 * is UINT_MAX, if the value in the URI exceeds this then
1382 * it is not considered an IPv4 address.
1384 static BOOL parse_ipv4address(const WCHAR **ptr, parse_data *data, DWORD flags) {
1385 const BOOL is_unknown = data->scheme_type == URL_SCHEME_UNKNOWN;
1388 if(!check_ipv4address(ptr, FALSE)) {
1389 if(!check_implicit_ipv4(ptr, &data->implicit_ipv4)) {
1390 TRACE("(%p %p %x): URI didn't contain anything looking like an IPv4 address.\n",
1396 data->has_implicit_ip = TRUE;
1399 data->host_len = *ptr - data->host;
1400 data->host_type = Uri_HOST_IPV4;
1402 /* Check if what we found is the only part of the host name (if it isn't
1403 * we don't have an IPv4 address).
1407 if(!parse_port(ptr, data, flags)) {
1412 } else if(!is_auth_delim(**ptr, !is_unknown)) {
1413 /* Found more data which belongs to the host, so this isn't an IPv4. */
1416 data->has_implicit_ip = FALSE;
1420 TRACE("(%p %p %x): IPv4 address found. host=%s host_len=%d host_type=%d\n",
1421 ptr, data, flags, debugstr_wn(data->host, data->host_len),
1422 data->host_len, data->host_type);
1426 /* Attempts to parse the reg-name from the URI.
1428 * Because of the way Windows handles ':' this function also
1429 * handles parsing the port.
1431 * reg-name = *( unreserved / pct-encoded / sub-delims )
1434 * Windows allows everything, but, the characters in "auth_delims" and ':'
1435 * to appear in a reg-name, unless it's an unknown scheme type then ':' is
1436 * allowed to appear (even if a valid port isn't after it).
1438 * Windows doesn't like host names which start with '[' and end with ']'
1439 * and don't contain a valid IP literal address in between them.
1441 * On Windows if a '[' is encountered in the host name the ':' no longer
1442 * counts as a delimiter until you reach the next ']' or an "authority delimiter".
1444 * A reg-name CAN be empty.
1446 static BOOL parse_reg_name(const WCHAR **ptr, parse_data *data, DWORD flags, DWORD extras) {
1447 const BOOL has_start_bracket = **ptr == '[';
1448 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
1449 const BOOL is_res = data->scheme_type == URL_SCHEME_RES;
1450 BOOL inside_brackets = has_start_bracket;
1452 /* res URIs don't have ports. */
1453 BOOL ignore_col = (extras & IGNORE_PORT_DELIMITER) || is_res;
1455 /* We have to be careful with file schemes. */
1456 if(data->scheme_type == URL_SCHEME_FILE) {
1457 /* This is because an implicit file scheme could be "C:\\test" and it
1458 * would trick this function into thinking the host is "C", when after
1459 * canonicalization the host would end up being an empty string. A drive
1460 * path can also have a '|' instead of a ':' after the drive letter.
1462 if(is_drive_path(*ptr)) {
1463 /* Regular old drive paths have no host type (or host name). */
1464 data->host_type = Uri_HOST_UNKNOWN;
1468 } else if(is_unc_path(*ptr))
1469 /* Skip past the "\\" of a UNC path. */
1475 /* For res URIs, everything before the first '/' is
1476 * considered the host.
1478 while((!is_res && !is_auth_delim(**ptr, known_scheme)) ||
1479 (is_res && **ptr && **ptr != '/')) {
1480 if(**ptr == ':' && !ignore_col) {
1481 /* We can ignore ':' if were inside brackets.*/
1482 if(!inside_brackets) {
1483 const WCHAR *tmp = (*ptr)++;
1485 /* Attempt to parse the port. */
1486 if(!parse_port(ptr, data, flags)) {
1487 /* Windows expects there to be a valid port for known scheme types. */
1488 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
1491 TRACE("(%p %p %x %x): Expected valid port\n", ptr, data, flags, extras);
1494 /* Windows gives up on trying to parse a port when it
1495 * encounters an invalid port.
1499 data->host_len = tmp - data->host;
1503 } else if(**ptr == '%' && (known_scheme && !is_res)) {
1504 /* Has to be a legit % encoded value. */
1505 if(!check_pct_encoded(ptr)) {
1511 } else if(is_res && is_forbidden_dos_path_char(**ptr)) {
1515 } else if(**ptr == ']')
1516 inside_brackets = FALSE;
1517 else if(**ptr == '[')
1518 inside_brackets = TRUE;
1523 if(has_start_bracket) {
1524 /* Make sure the last character of the host wasn't a ']'. */
1525 if(*(*ptr-1) == ']') {
1526 TRACE("(%p %p %x %x): Expected an IP literal inside of the host\n",
1527 ptr, data, flags, extras);
1534 /* Don't overwrite our length if we found a port earlier. */
1536 data->host_len = *ptr - data->host;
1538 /* If the host is empty, then it's an unknown host type. */
1539 if(data->host_len == 0 || is_res)
1540 data->host_type = Uri_HOST_UNKNOWN;
1542 data->host_type = Uri_HOST_DNS;
1544 TRACE("(%p %p %x %x): Parsed reg-name. host=%s len=%d\n", ptr, data, flags, extras,
1545 debugstr_wn(data->host, data->host_len), data->host_len);
1549 /* Attempts to parse an IPv6 address out of the URI.
1551 * IPv6address = 6( h16 ":" ) ls32
1552 * / "::" 5( h16 ":" ) ls32
1553 * / [ h16 ] "::" 4( h16 ":" ) ls32
1554 * / [ *1( h16 ":" ) h16 ] "::" 3( h16 ":" ) ls32
1555 * / [ *2( h16 ":" ) h16 ] "::" 2( h16 ":" ) ls32
1556 * / [ *3( h16 ":" ) h16 ] "::" h16 ":" ls32
1557 * / [ *4( h16 ":" ) h16 ] "::" ls32
1558 * / [ *5( h16 ":" ) h16 ] "::" h16
1559 * / [ *6( h16 ":" ) h16 ] "::"
1561 * ls32 = ( h16 ":" h16 ) / IPv4address
1562 * ; least-significant 32 bits of address.
1565 * ; 16 bits of address represented in hexadecimal.
1567 * Modeled after google-url's 'DoParseIPv6' function.
1569 static BOOL parse_ipv6address(const WCHAR **ptr, parse_data *data, DWORD flags) {
1570 const WCHAR *start, *cur_start;
1573 start = cur_start = *ptr;
1574 memset(&ip, 0, sizeof(ipv6_address));
1577 /* Check if we're on the last character of the host. */
1578 BOOL is_end = (is_auth_delim(**ptr, data->scheme_type != URL_SCHEME_UNKNOWN)
1581 BOOL is_split = (**ptr == ':');
1582 BOOL is_elision = (is_split && !is_end && *(*ptr+1) == ':');
1584 /* Check if we're at the end of a component, or
1585 * if we're at the end of the IPv6 address.
1587 if(is_split || is_end) {
1590 cur_len = *ptr - cur_start;
1592 /* h16 can't have a length > 4. */
1596 TRACE("(%p %p %x): h16 component to long.\n",
1602 /* An h16 component can't have the length of 0 unless
1603 * the elision is at the beginning of the address, or
1604 * at the end of the address.
1606 if(!((*ptr == start && is_elision) ||
1607 (is_end && (*ptr-2) == ip.elision))) {
1609 TRACE("(%p %p %x): IPv6 component cannot have a length of 0.\n",
1616 /* An IPv6 address can have no more than 8 h16 components. */
1617 if(ip.h16_count >= 8) {
1619 TRACE("(%p %p %x): Not a IPv6 address, to many h16 components.\n",
1624 ip.components[ip.h16_count].str = cur_start;
1625 ip.components[ip.h16_count].len = cur_len;
1627 TRACE("(%p %p %x): Found h16 component %s, len=%d, h16_count=%d\n",
1628 ptr, data, flags, debugstr_wn(cur_start, cur_len), cur_len,
1638 /* A IPv6 address can only have 1 elision ('::'). */
1642 TRACE("(%p %p %x): IPv6 address cannot have 2 elisions.\n",
1654 if(!check_ipv4address(ptr, TRUE)) {
1655 if(!is_hexdigit(**ptr)) {
1656 /* Not a valid character for an IPv6 address. */
1661 /* Found an IPv4 address. */
1662 ip.ipv4 = cur_start;
1663 ip.ipv4_len = *ptr - cur_start;
1665 TRACE("(%p %p %x): Found an attached IPv4 address %s len=%d.\n",
1666 ptr, data, flags, debugstr_wn(ip.ipv4, ip.ipv4_len),
1669 /* IPv4 addresses can only appear at the end of a IPv6. */
1675 compute_ipv6_comps_size(&ip);
1677 /* Make sure the IPv6 address adds up to 16 bytes. */
1678 if(ip.components_size + ip.elision_size != 16) {
1680 TRACE("(%p %p %x): Invalid IPv6 address, did not add up to 16 bytes.\n",
1685 if(ip.elision_size == 2) {
1686 /* For some reason on Windows if an elision that represents
1687 * only one h16 component is encountered at the very begin or
1688 * end of an IPv6 address, Windows does not consider it a
1689 * valid IPv6 address.
1691 * Ex: [::2:3:4:5:6:7] is not valid, even though the sum
1692 * of all the components == 128bits.
1694 if(ip.elision < ip.components[0].str ||
1695 ip.elision > ip.components[ip.h16_count-1].str) {
1697 TRACE("(%p %p %x): Invalid IPv6 address. Detected elision of 2 bytes at the beginning or end of the address.\n",
1703 data->host_type = Uri_HOST_IPV6;
1704 data->has_ipv6 = TRUE;
1705 data->ipv6_address = ip;
1707 TRACE("(%p %p %x): Found valid IPv6 literal %s len=%d\n",
1708 ptr, data, flags, debugstr_wn(start, *ptr-start),
1713 /* IPvFuture = "v" 1*HEXDIG "." 1*( unreserved / sub-delims / ":" ) */
1714 static BOOL parse_ipvfuture(const WCHAR **ptr, parse_data *data, DWORD flags) {
1715 const WCHAR *start = *ptr;
1717 /* IPvFuture has to start with a 'v' or 'V'. */
1718 if(**ptr != 'v' && **ptr != 'V')
1721 /* Following the v there must be at least 1 hex digit. */
1723 if(!is_hexdigit(**ptr)) {
1729 while(is_hexdigit(**ptr))
1732 /* End of the hexdigit sequence must be a '.' */
1739 if(!is_unreserved(**ptr) && !is_subdelim(**ptr) && **ptr != ':') {
1745 while(is_unreserved(**ptr) || is_subdelim(**ptr) || **ptr == ':')
1748 data->host_type = Uri_HOST_UNKNOWN;
1750 TRACE("(%p %p %x): Parsed IPvFuture address %s len=%d\n", ptr, data, flags,
1751 debugstr_wn(start, *ptr-start), (int)(*ptr-start));
1756 /* IP-literal = "[" ( IPv6address / IPvFuture ) "]" */
1757 static BOOL parse_ip_literal(const WCHAR **ptr, parse_data *data, DWORD flags, DWORD extras) {
1760 if(**ptr != '[' && !(extras & ALLOW_BRACKETLESS_IP_LITERAL)) {
1763 } else if(**ptr == '[')
1766 if(!parse_ipv6address(ptr, data, flags)) {
1767 if(extras & SKIP_IP_FUTURE_CHECK || !parse_ipvfuture(ptr, data, flags)) {
1774 if(**ptr != ']' && !(extras & ALLOW_BRACKETLESS_IP_LITERAL)) {
1778 } else if(!**ptr && extras & ALLOW_BRACKETLESS_IP_LITERAL) {
1779 /* The IP literal didn't contain brackets and was followed by
1780 * a NULL terminator, so no reason to even check the port.
1782 data->host_len = *ptr - data->host;
1789 /* If a valid port is not found, then let it trickle down to
1792 if(!parse_port(ptr, data, flags)) {
1798 data->host_len = *ptr - data->host;
1803 /* Parses the host information from the URI.
1805 * host = IP-literal / IPv4address / reg-name
1807 static BOOL parse_host(const WCHAR **ptr, parse_data *data, DWORD flags, DWORD extras) {
1808 if(!parse_ip_literal(ptr, data, flags, extras)) {
1809 if(!parse_ipv4address(ptr, data, flags)) {
1810 if(!parse_reg_name(ptr, data, flags, extras)) {
1811 TRACE("(%p %p %x %x): Malformed URI, Unknown host type.\n",
1812 ptr, data, flags, extras);
1821 /* Parses the authority information from the URI.
1823 * authority = [ userinfo "@" ] host [ ":" port ]
1825 static BOOL parse_authority(const WCHAR **ptr, parse_data *data, DWORD flags) {
1826 parse_userinfo(ptr, data, flags);
1828 /* Parsing the port will happen during one of the host parsing
1829 * routines (if the URI has a port).
1831 if(!parse_host(ptr, data, flags, 0))
1837 /* Attempts to parse the path information of a hierarchical URI. */
1838 static BOOL parse_path_hierarchical(const WCHAR **ptr, parse_data *data, DWORD flags) {
1839 const WCHAR *start = *ptr;
1840 static const WCHAR slash[] = {'/',0};
1841 const BOOL is_file = data->scheme_type == URL_SCHEME_FILE;
1843 if(is_path_delim(**ptr)) {
1844 if(data->scheme_type == URL_SCHEME_WILDCARD && !data->must_have_path) {
1847 } else if(!(flags & Uri_CREATE_NO_CANONICALIZE)) {
1848 /* If the path component is empty, then a '/' is added. */
1853 while(!is_path_delim(**ptr)) {
1854 if(**ptr == '%' && data->scheme_type != URL_SCHEME_UNKNOWN && !is_file) {
1855 if(!check_pct_encoded(ptr)) {
1860 } else if(is_forbidden_dos_path_char(**ptr) && is_file &&
1861 (flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
1862 /* File schemes with USE_DOS_PATH set aren't allowed to have
1863 * a '<' or '>' or '\"' appear in them.
1867 } else if(**ptr == '\\') {
1868 /* Not allowed to have a backslash if NO_CANONICALIZE is set
1869 * and the scheme is known type (but not a file scheme).
1871 if(flags & Uri_CREATE_NO_CANONICALIZE) {
1872 if(data->scheme_type != URL_SCHEME_FILE &&
1873 data->scheme_type != URL_SCHEME_UNKNOWN) {
1883 /* The only time a URI doesn't have a path is when
1884 * the NO_CANONICALIZE flag is set and the raw URI
1885 * didn't contain one.
1892 data->path_len = *ptr - start;
1897 TRACE("(%p %p %x): Parsed path %s len=%d\n", ptr, data, flags,
1898 debugstr_wn(data->path, data->path_len), data->path_len);
1900 TRACE("(%p %p %x): The URI contained no path\n", ptr, data, flags);
1905 /* Parses the path of an opaque URI (much less strict then the parser
1906 * for a hierarchical URI).
1909 * Windows allows invalid % encoded data to appear in opaque URI paths
1910 * for unknown scheme types.
1912 * File schemes with USE_DOS_PATH set aren't allowed to have '<', '>', or '\"'
1915 static BOOL parse_path_opaque(const WCHAR **ptr, parse_data *data, DWORD flags) {
1916 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
1917 const BOOL is_file = data->scheme_type == URL_SCHEME_FILE;
1921 while(!is_path_delim(**ptr)) {
1922 if(**ptr == '%' && known_scheme) {
1923 if(!check_pct_encoded(ptr)) {
1929 } else if(is_forbidden_dos_path_char(**ptr) && is_file &&
1930 (flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
1939 data->path_len = *ptr - data->path;
1940 TRACE("(%p %p %x): Parsed opaque URI path %s len=%d\n", ptr, data, flags,
1941 debugstr_wn(data->path, data->path_len), data->path_len);
1945 /* Determines how the URI should be parsed after the scheme information.
1947 * If the scheme is followed by "//", then it is treated as a hierarchical URI
1948 * which then the authority and path information will be parsed out. Otherwise, the
1949 * URI will be treated as an opaque URI which the authority information is not parsed
1952 * RFC 3896 definition of hier-part:
1954 * hier-part = "//" authority path-abempty
1959 * MSDN opaque URI definition:
1960 * scheme ":" path [ "#" fragment ]
1963 * If the URI is of an unknown scheme type and has a "//" following the scheme then it
1964 * is treated as a hierarchical URI, but, if the CREATE_NO_CRACK_UNKNOWN_SCHEMES flag is
1965 * set then it is considered an opaque URI regardless of what follows the scheme information
1966 * (per MSDN documentation).
1968 static BOOL parse_hierpart(const WCHAR **ptr, parse_data *data, DWORD flags) {
1969 const WCHAR *start = *ptr;
1971 data->must_have_path = FALSE;
1973 /* For javascript: URIs, simply set everything as a path */
1974 if(data->scheme_type == URL_SCHEME_JAVASCRIPT) {
1976 data->path_len = strlenW(*ptr);
1977 data->is_opaque = TRUE;
1978 *ptr += data->path_len;
1982 /* Checks if the authority information needs to be parsed. */
1983 if(is_hierarchical_uri(ptr, data)) {
1984 /* Only treat it as a hierarchical URI if the scheme_type is known or
1985 * the Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES flag is not set.
1987 if(data->scheme_type != URL_SCHEME_UNKNOWN ||
1988 !(flags & Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES)) {
1989 TRACE("(%p %p %x): Treating URI as an hierarchical URI.\n", ptr, data, flags);
1990 data->is_opaque = FALSE;
1992 if(data->scheme_type == URL_SCHEME_WILDCARD && !data->has_implicit_scheme) {
1993 if(**ptr == '/' && *(*ptr+1) == '/') {
1994 data->must_have_path = TRUE;
1999 /* TODO: Handle hierarchical URI's, parse authority then parse the path. */
2000 if(!parse_authority(ptr, data, flags))
2003 return parse_path_hierarchical(ptr, data, flags);
2005 /* Reset ptr to its starting position so opaque path parsing
2006 * begins at the correct location.
2011 /* If it reaches here, then the URI will be treated as an opaque
2015 TRACE("(%p %p %x): Treating URI as an opaque URI.\n", ptr, data, flags);
2017 data->is_opaque = TRUE;
2018 if(!parse_path_opaque(ptr, data, flags))
2024 /* Attempts to parse the query string from the URI.
2027 * If NO_DECODE_EXTRA_INFO flag is set, then invalid percent encoded
2028 * data is allowed to appear in the query string. For unknown scheme types
2029 * invalid percent encoded data is allowed to appear regardless.
2031 static BOOL parse_query(const WCHAR **ptr, parse_data *data, DWORD flags) {
2032 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
2035 TRACE("(%p %p %x): URI didn't contain a query string.\n", ptr, data, flags);
2042 while(**ptr && **ptr != '#') {
2043 if(**ptr == '%' && known_scheme &&
2044 !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
2045 if(!check_pct_encoded(ptr)) {
2056 data->query_len = *ptr - data->query;
2058 TRACE("(%p %p %x): Parsed query string %s len=%d\n", ptr, data, flags,
2059 debugstr_wn(data->query, data->query_len), data->query_len);
2063 /* Attempts to parse the fragment from the URI.
2066 * If NO_DECODE_EXTRA_INFO flag is set, then invalid percent encoded
2067 * data is allowed to appear in the query string. For unknown scheme types
2068 * invalid percent encoded data is allowed to appear regardless.
2070 static BOOL parse_fragment(const WCHAR **ptr, parse_data *data, DWORD flags) {
2071 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
2074 TRACE("(%p %p %x): URI didn't contain a fragment.\n", ptr, data, flags);
2078 data->fragment = *ptr;
2082 if(**ptr == '%' && known_scheme &&
2083 !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
2084 if(!check_pct_encoded(ptr)) {
2085 *ptr = data->fragment;
2086 data->fragment = NULL;
2095 data->fragment_len = *ptr - data->fragment;
2097 TRACE("(%p %p %x): Parsed fragment %s len=%d\n", ptr, data, flags,
2098 debugstr_wn(data->fragment, data->fragment_len), data->fragment_len);
2102 /* Parses and validates the components of the specified by data->uri
2103 * and stores the information it parses into 'data'.
2105 * Returns TRUE if it successfully parsed the URI. False otherwise.
2107 static BOOL parse_uri(parse_data *data, DWORD flags) {
2114 TRACE("(%p %x): BEGINNING TO PARSE URI %s.\n", data, flags, debugstr_w(data->uri));
2116 if(!parse_scheme(pptr, data, flags, 0))
2119 if(!parse_hierpart(pptr, data, flags))
2122 if(!parse_query(pptr, data, flags))
2125 if(!parse_fragment(pptr, data, flags))
2128 TRACE("(%p %x): FINISHED PARSING URI.\n", data, flags);
2132 static BOOL canonicalize_username(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2135 if(!data->username) {
2136 uri->userinfo_start = -1;
2140 uri->userinfo_start = uri->canon_len;
2141 for(ptr = data->username; ptr < data->username+data->username_len; ++ptr) {
2143 /* Only decode % encoded values for known scheme types. */
2144 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
2145 /* See if the value really needs decoding. */
2146 WCHAR val = decode_pct_val(ptr);
2147 if(is_unreserved(val)) {
2149 uri->canon_uri[uri->canon_len] = val;
2153 /* Move pass the hex characters. */
2158 } else if(!is_reserved(*ptr) && !is_unreserved(*ptr) && *ptr != '\\') {
2159 /* Only percent encode forbidden characters if the NO_ENCODE_FORBIDDEN_CHARACTERS flag
2162 if(!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS)) {
2164 pct_encode_val(*ptr, uri->canon_uri + uri->canon_len);
2166 uri->canon_len += 3;
2172 /* Nothing special, so just copy the character over. */
2173 uri->canon_uri[uri->canon_len] = *ptr;
2180 static BOOL canonicalize_password(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2183 if(!data->password) {
2184 uri->userinfo_split = -1;
2188 if(uri->userinfo_start == -1)
2189 /* Has a password, but, doesn't have a username. */
2190 uri->userinfo_start = uri->canon_len;
2192 uri->userinfo_split = uri->canon_len - uri->userinfo_start;
2194 /* Add the ':' to the userinfo component. */
2196 uri->canon_uri[uri->canon_len] = ':';
2199 for(ptr = data->password; ptr < data->password+data->password_len; ++ptr) {
2201 /* Only decode % encoded values for known scheme types. */
2202 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
2203 /* See if the value really needs decoding. */
2204 WCHAR val = decode_pct_val(ptr);
2205 if(is_unreserved(val)) {
2207 uri->canon_uri[uri->canon_len] = val;
2211 /* Move pass the hex characters. */
2216 } else if(!is_reserved(*ptr) && !is_unreserved(*ptr) && *ptr != '\\') {
2217 /* Only percent encode forbidden characters if the NO_ENCODE_FORBIDDEN_CHARACTERS flag
2220 if(!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS)) {
2222 pct_encode_val(*ptr, uri->canon_uri + uri->canon_len);
2224 uri->canon_len += 3;
2230 /* Nothing special, so just copy the character over. */
2231 uri->canon_uri[uri->canon_len] = *ptr;
2238 /* Canonicalizes the userinfo of the URI represented by the parse_data.
2240 * Canonicalization of the userinfo is a simple process. If there are any percent
2241 * encoded characters that fall in the "unreserved" character set, they are decoded
2242 * to their actual value. If a character is not in the "unreserved" or "reserved" sets
2243 * then it is percent encoded. Other than that the characters are copied over without
2246 static BOOL canonicalize_userinfo(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2247 uri->userinfo_start = uri->userinfo_split = -1;
2248 uri->userinfo_len = 0;
2250 if(!data->username && !data->password)
2251 /* URI doesn't have userinfo, so nothing to do here. */
2254 if(!canonicalize_username(data, uri, flags, computeOnly))
2257 if(!canonicalize_password(data, uri, flags, computeOnly))
2260 uri->userinfo_len = uri->canon_len - uri->userinfo_start;
2262 TRACE("(%p %p %x %d): Canonicalized userinfo, userinfo_start=%d, userinfo=%s, userinfo_split=%d userinfo_len=%d.\n",
2263 data, uri, flags, computeOnly, uri->userinfo_start, debugstr_wn(uri->canon_uri + uri->userinfo_start, uri->userinfo_len),
2264 uri->userinfo_split, uri->userinfo_len);
2266 /* Now insert the '@' after the userinfo. */
2268 uri->canon_uri[uri->canon_len] = '@';
2274 /* Attempts to canonicalize a reg_name.
2276 * Things that happen:
2277 * 1) If Uri_CREATE_NO_CANONICALIZE flag is not set, then the reg_name is
2278 * lower cased. Unless it's an unknown scheme type, which case it's
2279 * no lower cased regardless.
2281 * 2) Unreserved % encoded characters are decoded for known
2284 * 3) Forbidden characters are % encoded as long as
2285 * Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS flag is not set and
2286 * it isn't an unknown scheme type.
2288 * 4) If it's a file scheme and the host is "localhost" it's removed.
2290 * 5) If it's a file scheme and Uri_CREATE_FILE_USE_DOS_PATH is set,
2291 * then the UNC path characters are added before the host name.
2293 static BOOL canonicalize_reg_name(const parse_data *data, Uri *uri,
2294 DWORD flags, BOOL computeOnly) {
2295 static const WCHAR localhostW[] =
2296 {'l','o','c','a','l','h','o','s','t',0};
2298 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
2300 if(data->scheme_type == URL_SCHEME_FILE &&
2301 data->host_len == lstrlenW(localhostW)) {
2302 if(!StrCmpNIW(data->host, localhostW, data->host_len)) {
2303 uri->host_start = -1;
2305 uri->host_type = Uri_HOST_UNKNOWN;
2310 if(data->scheme_type == URL_SCHEME_FILE && flags & Uri_CREATE_FILE_USE_DOS_PATH) {
2312 uri->canon_uri[uri->canon_len] = '\\';
2313 uri->canon_uri[uri->canon_len+1] = '\\';
2315 uri->canon_len += 2;
2316 uri->authority_start = uri->canon_len;
2319 uri->host_start = uri->canon_len;
2321 for(ptr = data->host; ptr < data->host+data->host_len; ++ptr) {
2322 if(*ptr == '%' && known_scheme) {
2323 WCHAR val = decode_pct_val(ptr);
2324 if(is_unreserved(val)) {
2325 /* If NO_CANONICALIZE is not set, then windows lower cases the
2328 if(!(flags & Uri_CREATE_NO_CANONICALIZE) && isupperW(val)) {
2330 uri->canon_uri[uri->canon_len] = tolowerW(val);
2333 uri->canon_uri[uri->canon_len] = val;
2337 /* Skip past the % encoded character. */
2341 /* Just copy the % over. */
2343 uri->canon_uri[uri->canon_len] = *ptr;
2346 } else if(*ptr == '\\') {
2347 /* Only unknown scheme types could have made it here with a '\\' in the host name. */
2349 uri->canon_uri[uri->canon_len] = *ptr;
2351 } else if(!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS) &&
2352 !is_unreserved(*ptr) && !is_reserved(*ptr) && known_scheme) {
2354 pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
2356 /* The percent encoded value gets lower cased also. */
2357 if(!(flags & Uri_CREATE_NO_CANONICALIZE)) {
2358 uri->canon_uri[uri->canon_len+1] = tolowerW(uri->canon_uri[uri->canon_len+1]);
2359 uri->canon_uri[uri->canon_len+2] = tolowerW(uri->canon_uri[uri->canon_len+2]);
2363 uri->canon_len += 3;
2366 if(!(flags & Uri_CREATE_NO_CANONICALIZE) && known_scheme)
2367 uri->canon_uri[uri->canon_len] = tolowerW(*ptr);
2369 uri->canon_uri[uri->canon_len] = *ptr;
2376 uri->host_len = uri->canon_len - uri->host_start;
2379 TRACE("(%p %p %x %d): Canonicalize reg_name=%s len=%d\n", data, uri, flags,
2380 computeOnly, debugstr_wn(uri->canon_uri+uri->host_start, uri->host_len),
2384 find_domain_name(uri->canon_uri+uri->host_start, uri->host_len,
2385 &(uri->domain_offset));
2390 /* Attempts to canonicalize an implicit IPv4 address. */
2391 static BOOL canonicalize_implicit_ipv4address(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2392 uri->host_start = uri->canon_len;
2394 TRACE("%u\n", data->implicit_ipv4);
2395 /* For unknown scheme types Windows doesn't convert
2396 * the value into an IP address, but it still considers
2397 * it an IPv4 address.
2399 if(data->scheme_type == URL_SCHEME_UNKNOWN) {
2401 memcpy(uri->canon_uri+uri->canon_len, data->host, data->host_len*sizeof(WCHAR));
2402 uri->canon_len += data->host_len;
2405 uri->canon_len += ui2ipv4(uri->canon_uri+uri->canon_len, data->implicit_ipv4);
2407 uri->canon_len += ui2ipv4(NULL, data->implicit_ipv4);
2410 uri->host_len = uri->canon_len - uri->host_start;
2411 uri->host_type = Uri_HOST_IPV4;
2414 TRACE("%p %p %x %d): Canonicalized implicit IP address=%s len=%d\n",
2415 data, uri, flags, computeOnly,
2416 debugstr_wn(uri->canon_uri+uri->host_start, uri->host_len),
2422 /* Attempts to canonicalize an IPv4 address.
2424 * If the parse_data represents a URI that has an implicit IPv4 address
2425 * (ex. http://256/, this function will convert 256 into 0.0.1.0). If
2426 * the implicit IP address exceeds the value of UINT_MAX (maximum value
2427 * for an IPv4 address) it's canonicalized as if it were a reg-name.
2429 * If the parse_data contains a partial or full IPv4 address it normalizes it.
2430 * A partial IPv4 address is something like "192.0" and would be normalized to
2431 * "192.0.0.0". With a full (or partial) IPv4 address like "192.002.01.003" would
2432 * be normalized to "192.2.1.3".
2435 * Windows ONLY normalizes IPv4 address for known scheme types (one that isn't
2436 * URL_SCHEME_UNKNOWN). For unknown scheme types, it simply copies the data from
2437 * the original URI into the canonicalized URI, but, it still recognizes URI's
2438 * host type as HOST_IPV4.
2440 static BOOL canonicalize_ipv4address(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2441 if(data->has_implicit_ip)
2442 return canonicalize_implicit_ipv4address(data, uri, flags, computeOnly);
2444 uri->host_start = uri->canon_len;
2446 /* Windows only normalizes for known scheme types. */
2447 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
2448 /* parse_data contains a partial or full IPv4 address, so normalize it. */
2449 DWORD i, octetDigitCount = 0, octetCount = 0;
2450 BOOL octetHasDigit = FALSE;
2452 for(i = 0; i < data->host_len; ++i) {
2453 if(data->host[i] == '0' && !octetHasDigit) {
2454 /* Can ignore leading zeros if:
2455 * 1) It isn't the last digit of the octet.
2456 * 2) i+1 != data->host_len
2459 if(octetDigitCount == 2 ||
2460 i+1 == data->host_len ||
2461 data->host[i+1] == '.') {
2463 uri->canon_uri[uri->canon_len] = data->host[i];
2465 TRACE("Adding zero\n");
2467 } else if(data->host[i] == '.') {
2469 uri->canon_uri[uri->canon_len] = data->host[i];
2472 octetDigitCount = 0;
2473 octetHasDigit = FALSE;
2477 uri->canon_uri[uri->canon_len] = data->host[i];
2481 octetHasDigit = TRUE;
2485 /* Make sure the canonicalized IP address has 4 dec-octets.
2486 * If doesn't add "0" ones until there is 4;
2488 for( ; octetCount < 3; ++octetCount) {
2490 uri->canon_uri[uri->canon_len] = '.';
2491 uri->canon_uri[uri->canon_len+1] = '0';
2494 uri->canon_len += 2;
2497 /* Windows doesn't normalize addresses in unknown schemes. */
2499 memcpy(uri->canon_uri+uri->canon_len, data->host, data->host_len*sizeof(WCHAR));
2500 uri->canon_len += data->host_len;
2503 uri->host_len = uri->canon_len - uri->host_start;
2505 TRACE("(%p %p %x %d): Canonicalized IPv4 address, ip=%s len=%d\n",
2506 data, uri, flags, computeOnly,
2507 debugstr_wn(uri->canon_uri+uri->host_start, uri->host_len),
2514 /* Attempts to canonicalize the IPv6 address of the URI.
2516 * Multiple things happen during the canonicalization of an IPv6 address:
2517 * 1) Any leading zero's in a h16 component are removed.
2518 * Ex: [0001:0022::] -> [1:22::]
2520 * 2) The longest sequence of zero h16 components are compressed
2521 * into a "::" (elision). If there's a tie, the first is chosen.
2523 * Ex: [0:0:0:0:1:6:7:8] -> [::1:6:7:8]
2524 * [0:0:0:0:1:2::] -> [::1:2:0:0]
2525 * [0:0:1:2:0:0:7:8] -> [::1:2:0:0:7:8]
2527 * 3) If an IPv4 address is attached to the IPv6 address, it's
2529 * Ex: [::001.002.022.000] -> [::1.2.22.0]
2531 * 4) If an elision is present, but, only represents one h16 component
2534 * Ex: [1::2:3:4:5:6:7] -> [1:0:2:3:4:5:6:7]
2536 * 5) If the IPv6 address contains an IPv4 address and there exists
2537 * at least 1 non-zero h16 component the IPv4 address is converted
2538 * into two h16 components, otherwise it's normalized and kept as is.
2540 * Ex: [::192.200.003.4] -> [::192.200.3.4]
2541 * [ffff::192.200.003.4] -> [ffff::c0c8:3041]
2544 * For unknown scheme types Windows simply copies the address over without any
2547 * IPv4 address can be included in an elision if all its components are 0's.
2549 static BOOL canonicalize_ipv6address(const parse_data *data, Uri *uri,
2550 DWORD flags, BOOL computeOnly) {
2551 uri->host_start = uri->canon_len;
2553 if(data->scheme_type == URL_SCHEME_UNKNOWN) {
2555 memcpy(uri->canon_uri+uri->canon_len, data->host, data->host_len*sizeof(WCHAR));
2556 uri->canon_len += data->host_len;
2560 DWORD i, elision_len;
2562 if(!ipv6_to_number(&(data->ipv6_address), values)) {
2563 TRACE("(%p %p %x %d): Failed to compute numerical value for IPv6 address.\n",
2564 data, uri, flags, computeOnly);
2569 uri->canon_uri[uri->canon_len] = '[';
2572 /* Find where the elision should occur (if any). */
2573 compute_elision_location(&(data->ipv6_address), values, &elision_start, &elision_len);
2575 TRACE("%p %p %x %d): Elision starts at %d, len=%u\n", data, uri, flags,
2576 computeOnly, elision_start, elision_len);
2578 for(i = 0; i < 8; ++i) {
2579 BOOL in_elision = (elision_start > -1 && i >= elision_start &&
2580 i < elision_start+elision_len);
2581 BOOL do_ipv4 = (i == 6 && data->ipv6_address.ipv4 && !in_elision &&
2582 data->ipv6_address.h16_count == 0);
2584 if(i == elision_start) {
2586 uri->canon_uri[uri->canon_len] = ':';
2587 uri->canon_uri[uri->canon_len+1] = ':';
2589 uri->canon_len += 2;
2592 /* We can ignore the current component if we're in the elision. */
2596 /* We only add a ':' if we're not at i == 0, or when we're at
2597 * the very end of elision range since the ':' colon was handled
2598 * earlier. Otherwise we would end up with ":::" after elision.
2600 if(i != 0 && !(elision_start > -1 && i == elision_start+elision_len)) {
2602 uri->canon_uri[uri->canon_len] = ':';
2610 /* Combine the two parts of the IPv4 address values. */
2616 len = ui2ipv4(uri->canon_uri+uri->canon_len, val);
2618 len = ui2ipv4(NULL, val);
2620 uri->canon_len += len;
2623 /* Write a regular h16 component to the URI. */
2625 /* Short circuit for the trivial case. */
2626 if(values[i] == 0) {
2628 uri->canon_uri[uri->canon_len] = '0';
2631 static const WCHAR formatW[] = {'%','x',0};
2634 uri->canon_len += sprintfW(uri->canon_uri+uri->canon_len,
2635 formatW, values[i]);
2638 uri->canon_len += sprintfW(tmp, formatW, values[i]);
2644 /* Add the closing ']'. */
2646 uri->canon_uri[uri->canon_len] = ']';
2650 uri->host_len = uri->canon_len - uri->host_start;
2653 TRACE("(%p %p %x %d): Canonicalized IPv6 address %s, len=%d\n", data, uri, flags,
2654 computeOnly, debugstr_wn(uri->canon_uri+uri->host_start, uri->host_len),
2660 /* Attempts to canonicalize the host of the URI (if any). */
2661 static BOOL canonicalize_host(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2662 uri->host_start = -1;
2664 uri->domain_offset = -1;
2667 switch(data->host_type) {
2669 uri->host_type = Uri_HOST_DNS;
2670 if(!canonicalize_reg_name(data, uri, flags, computeOnly))
2675 uri->host_type = Uri_HOST_IPV4;
2676 if(!canonicalize_ipv4address(data, uri, flags, computeOnly))
2681 if(!canonicalize_ipv6address(data, uri, flags, computeOnly))
2684 uri->host_type = Uri_HOST_IPV6;
2686 case Uri_HOST_UNKNOWN:
2687 if(data->host_len > 0 || data->scheme_type != URL_SCHEME_FILE) {
2688 uri->host_start = uri->canon_len;
2690 /* Nothing happens to unknown host types. */
2692 memcpy(uri->canon_uri+uri->canon_len, data->host, data->host_len*sizeof(WCHAR));
2693 uri->canon_len += data->host_len;
2694 uri->host_len = data->host_len;
2697 uri->host_type = Uri_HOST_UNKNOWN;
2700 FIXME("(%p %p %x %d): Canonicalization for host type %d not supported.\n", data,
2701 uri, flags, computeOnly, data->host_type);
2709 static BOOL canonicalize_port(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2710 BOOL has_default_port = FALSE;
2711 USHORT default_port = 0;
2714 uri->port_offset = -1;
2716 /* Check if the scheme has a default port. */
2717 for(i = 0; i < sizeof(default_ports)/sizeof(default_ports[0]); ++i) {
2718 if(default_ports[i].scheme == data->scheme_type) {
2719 has_default_port = TRUE;
2720 default_port = default_ports[i].port;
2725 uri->has_port = data->has_port || has_default_port;
2728 * 1) Has a port which is the default port.
2729 * 2) Has a port (not the default).
2730 * 3) Doesn't have a port, but, scheme has a default port.
2733 if(has_default_port && data->has_port && data->port_value == default_port) {
2734 /* If it's the default port and this flag isn't set, don't do anything. */
2735 if(flags & Uri_CREATE_NO_CANONICALIZE) {
2736 uri->port_offset = uri->canon_len-uri->authority_start;
2738 uri->canon_uri[uri->canon_len] = ':';
2742 /* Copy the original port over. */
2744 memcpy(uri->canon_uri+uri->canon_len, data->port, data->port_len*sizeof(WCHAR));
2745 uri->canon_len += data->port_len;
2748 uri->canon_len += ui2str(uri->canon_uri+uri->canon_len, data->port_value);
2750 uri->canon_len += ui2str(NULL, data->port_value);
2754 uri->port = default_port;
2755 } else if(data->has_port) {
2756 uri->port_offset = uri->canon_len-uri->authority_start;
2758 uri->canon_uri[uri->canon_len] = ':';
2761 if(flags & Uri_CREATE_NO_CANONICALIZE && data->port) {
2762 /* Copy the original over without changes. */
2764 memcpy(uri->canon_uri+uri->canon_len, data->port, data->port_len*sizeof(WCHAR));
2765 uri->canon_len += data->port_len;
2768 uri->canon_len += ui2str(uri->canon_uri+uri->canon_len, data->port_value);
2770 uri->canon_len += ui2str(NULL, data->port_value);
2773 uri->port = data->port_value;
2774 } else if(has_default_port)
2775 uri->port = default_port;
2780 /* Canonicalizes the authority of the URI represented by the parse_data. */
2781 static BOOL canonicalize_authority(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2782 uri->authority_start = uri->canon_len;
2783 uri->authority_len = 0;
2785 if(!canonicalize_userinfo(data, uri, flags, computeOnly))
2788 if(!canonicalize_host(data, uri, flags, computeOnly))
2791 if(!canonicalize_port(data, uri, flags, computeOnly))
2794 if(uri->host_start != -1 || (data->is_relative && (data->password || data->username)))
2795 uri->authority_len = uri->canon_len - uri->authority_start;
2797 uri->authority_start = -1;
2802 /* Attempts to canonicalize the path of a hierarchical URI.
2804 * Things that happen:
2805 * 1). Forbidden characters are percent encoded, unless the NO_ENCODE_FORBIDDEN
2806 * flag is set or it's a file URI. Forbidden characters are always encoded
2807 * for file schemes regardless and forbidden characters are never encoded
2808 * for unknown scheme types.
2810 * 2). For known scheme types '\\' are changed to '/'.
2812 * 3). Percent encoded, unreserved characters are decoded to their actual values.
2813 * Unless the scheme type is unknown. For file schemes any percent encoded
2814 * character in the unreserved or reserved set is decoded.
2816 * 4). For File schemes if the path is starts with a drive letter and doesn't
2817 * start with a '/' then one is appended.
2818 * Ex: file://c:/test.mp3 -> file:///c:/test.mp3
2820 * 5). Dot segments are removed from the path for all scheme types
2821 * unless NO_CANONICALIZE flag is set. Dot segments aren't removed
2822 * for wildcard scheme types.
2825 * file://c:/test%20test -> file:///c:/test%2520test
2826 * file://c:/test%3Etest -> file:///c:/test%253Etest
2827 * if Uri_CREATE_FILE_USE_DOS_PATH is not set:
2828 * file:///c:/test%20test -> file:///c:/test%20test
2829 * file:///c:/test%test -> file:///c:/test%25test
2831 static DWORD canonicalize_path_hierarchical(const WCHAR *path, DWORD path_len, URL_SCHEME scheme_type, BOOL has_host, DWORD flags,
2833 const BOOL known_scheme = scheme_type != URL_SCHEME_UNKNOWN;
2834 const BOOL is_file = scheme_type == URL_SCHEME_FILE;
2835 const BOOL is_res = scheme_type == URL_SCHEME_RES;
2837 BOOL escape_pct = FALSE;
2845 if(is_file && !has_host) {
2846 /* Check if a '/' needs to be appended for the file scheme. */
2847 if(path_len > 1 && is_drive_path(ptr) && !(flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
2849 ret_path[len] = '/';
2852 } else if(*ptr == '/') {
2853 if(!(flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
2854 /* Copy the extra '/' over. */
2856 ret_path[len] = '/';
2862 if(is_drive_path(ptr)) {
2864 ret_path[len] = *ptr;
2865 /* If there's a '|' after the drive letter, convert it to a ':'. */
2866 ret_path[len+1] = ':';
2873 if(!is_file && *path && *path != '/') {
2874 /* Prepend a '/' to the path if it doesn't have one. */
2876 ret_path[len] = '/';
2880 for(; ptr < path+path_len; ++ptr) {
2881 BOOL do_default_action = TRUE;
2883 if(*ptr == '%' && !is_res) {
2884 const WCHAR *tmp = ptr;
2887 /* Check if the % represents a valid encoded char, or if it needs encoding. */
2888 BOOL force_encode = !check_pct_encoded(&tmp) && is_file && !(flags&Uri_CREATE_FILE_USE_DOS_PATH);
2889 val = decode_pct_val(ptr);
2891 if(force_encode || escape_pct) {
2892 /* Escape the percent sign in the file URI. */
2894 pct_encode_val(*ptr, ret_path+len);
2896 do_default_action = FALSE;
2897 } else if((is_unreserved(val) && known_scheme) ||
2898 (is_file && (is_unreserved(val) || is_reserved(val) ||
2899 (val && flags&Uri_CREATE_FILE_USE_DOS_PATH && !is_forbidden_dos_path_char(val))))) {
2901 ret_path[len] = val;
2907 } else if(*ptr == '/' && is_file && (flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
2908 /* Convert the '/' back to a '\\'. */
2910 ret_path[len] = '\\';
2912 do_default_action = FALSE;
2913 } else if(*ptr == '\\' && known_scheme) {
2914 if(!(is_file && (flags & Uri_CREATE_FILE_USE_DOS_PATH))) {
2915 /* Convert '\\' into a '/'. */
2917 ret_path[len] = '/';
2919 do_default_action = FALSE;
2921 } else if(known_scheme && !is_res && !is_unreserved(*ptr) && !is_reserved(*ptr) &&
2922 (!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS) || is_file)) {
2923 if(!(is_file && (flags & Uri_CREATE_FILE_USE_DOS_PATH))) {
2924 /* Escape the forbidden character. */
2926 pct_encode_val(*ptr, ret_path+len);
2928 do_default_action = FALSE;
2932 if(do_default_action) {
2934 ret_path[len] = *ptr;
2939 /* Removing the dot segments only happens when it's not in
2940 * computeOnly mode and it's not a wildcard scheme. File schemes
2941 * with USE_DOS_PATH set don't get dot segments removed.
2943 if(!(is_file && (flags & Uri_CREATE_FILE_USE_DOS_PATH)) &&
2944 scheme_type != URL_SCHEME_WILDCARD) {
2945 if(!(flags & Uri_CREATE_NO_CANONICALIZE) && ret_path) {
2946 /* Remove the dot segments (if any) and reset everything to the new
2949 len = remove_dot_segments(ret_path, len);
2954 TRACE("Canonicalized path %s len=%d\n", debugstr_wn(ret_path, len), len);
2958 /* Attempts to canonicalize the path for an opaque URI.
2960 * For known scheme types:
2961 * 1) forbidden characters are percent encoded if
2962 * NO_ENCODE_FORBIDDEN_CHARACTERS isn't set.
2964 * 2) Percent encoded, unreserved characters are decoded
2965 * to their actual values, for known scheme types.
2967 * 3) '\\' are changed to '/' for known scheme types
2968 * except for mailto schemes.
2970 * 4) For file schemes, if USE_DOS_PATH is set all '/'
2971 * are converted to backslashes.
2973 * 5) For file schemes, if USE_DOS_PATH isn't set all '\'
2974 * are converted to forward slashes.
2976 static BOOL canonicalize_path_opaque(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2978 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
2979 const BOOL is_file = data->scheme_type == URL_SCHEME_FILE;
2980 const BOOL is_mk = data->scheme_type == URL_SCHEME_MK;
2983 uri->path_start = -1;
2988 uri->path_start = uri->canon_len;
2991 /* hijack this flag for SCHEME_MK to tell the function when to start
2992 * converting slashes */
2993 flags |= Uri_CREATE_FILE_USE_DOS_PATH;
2996 /* For javascript: URIs, simply copy path part without any canonicalization */
2997 if(data->scheme_type == URL_SCHEME_JAVASCRIPT) {
2999 memcpy(uri->canon_uri+uri->canon_len, data->path, data->path_len*sizeof(WCHAR));
3000 uri->path_len = data->path_len;
3001 uri->canon_len += data->path_len;
3005 /* Windows doesn't allow a "//" to appear after the scheme
3006 * of a URI, if it's an opaque URI.
3008 if(data->scheme && *(data->path) == '/' && *(data->path+1) == '/') {
3009 /* So it inserts a "/." before the "//" if it exists. */
3011 uri->canon_uri[uri->canon_len] = '/';
3012 uri->canon_uri[uri->canon_len+1] = '.';
3015 uri->canon_len += 2;
3018 for(ptr = data->path; ptr < data->path+data->path_len; ++ptr) {
3019 BOOL do_default_action = TRUE;
3021 if(*ptr == '%' && known_scheme) {
3022 WCHAR val = decode_pct_val(ptr);
3024 if(is_unreserved(val)) {
3026 uri->canon_uri[uri->canon_len] = val;
3032 } else if(*ptr == '/' && is_file && (flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
3034 uri->canon_uri[uri->canon_len] = '\\';
3036 do_default_action = FALSE;
3037 } else if(*ptr == '\\') {
3038 if((data->is_relative || is_mk || is_file) && !(flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
3039 /* Convert to a '/'. */
3041 uri->canon_uri[uri->canon_len] = '/';
3043 do_default_action = FALSE;
3045 } else if(is_mk && *ptr == ':' && ptr + 1 < data->path + data->path_len && *(ptr + 1) == ':') {
3046 flags &= ~Uri_CREATE_FILE_USE_DOS_PATH;
3047 } else if(known_scheme && !is_unreserved(*ptr) && !is_reserved(*ptr) &&
3048 !(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS)) {
3049 if(!(is_file && (flags & Uri_CREATE_FILE_USE_DOS_PATH))) {
3051 pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
3052 uri->canon_len += 3;
3053 do_default_action = FALSE;
3057 if(do_default_action) {
3059 uri->canon_uri[uri->canon_len] = *ptr;
3064 if(is_mk && !computeOnly && !(flags & Uri_CREATE_NO_CANONICALIZE)) {
3065 DWORD new_len = remove_dot_segments(uri->canon_uri + uri->path_start,
3066 uri->canon_len - uri->path_start);
3067 uri->canon_len = uri->path_start + new_len;
3070 uri->path_len = uri->canon_len - uri->path_start;
3073 TRACE("(%p %p %x %d): Canonicalized opaque URI path %s len=%d\n", data, uri, flags, computeOnly,
3074 debugstr_wn(uri->canon_uri+uri->path_start, uri->path_len), uri->path_len);
3078 /* Determines how the URI represented by the parse_data should be canonicalized.
3080 * Essentially, if the parse_data represents an hierarchical URI then it calls
3081 * canonicalize_authority and the canonicalization functions for the path. If the
3082 * URI is opaque it canonicalizes the path of the URI.
3084 static BOOL canonicalize_hierpart(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
3085 if(!data->is_opaque || (data->is_relative && (data->password || data->username))) {
3086 /* "//" is only added for non-wildcard scheme types.
3088 * A "//" is only added to a relative URI if it has a
3089 * host or port component (this only happens if a IUriBuilder
3090 * is generating an IUri).
3092 if((data->is_relative && (data->host || data->has_port)) ||
3093 (!data->is_relative && data->scheme_type != URL_SCHEME_WILDCARD)) {
3094 if(data->scheme_type == URL_SCHEME_WILDCARD)
3098 INT pos = uri->canon_len;
3100 uri->canon_uri[pos] = '/';
3101 uri->canon_uri[pos+1] = '/';
3103 uri->canon_len += 2;
3106 if(!canonicalize_authority(data, uri, flags, computeOnly))
3109 if(data->is_relative && (data->password || data->username)) {
3110 if(!canonicalize_path_opaque(data, uri, flags, computeOnly))
3114 uri->path_start = uri->canon_len;
3115 uri->path_len = canonicalize_path_hierarchical(data->path, data->path_len, data->scheme_type, data->host_len != 0,
3116 flags, computeOnly ? NULL : uri->canon_uri+uri->canon_len);
3117 uri->canon_len += uri->path_len;
3118 if(!computeOnly && !uri->path_len)
3119 uri->path_start = -1;
3122 /* Opaque URI's don't have an authority. */
3123 uri->userinfo_start = uri->userinfo_split = -1;
3124 uri->userinfo_len = 0;
3125 uri->host_start = -1;
3127 uri->host_type = Uri_HOST_UNKNOWN;
3128 uri->has_port = FALSE;
3129 uri->authority_start = -1;
3130 uri->authority_len = 0;
3131 uri->domain_offset = -1;
3132 uri->port_offset = -1;
3134 if(is_hierarchical_scheme(data->scheme_type)) {
3137 /* Absolute URIs aren't displayed for known scheme types
3138 * which should be hierarchical URIs.
3140 uri->display_modifiers |= URI_DISPLAY_NO_ABSOLUTE_URI;
3142 /* Windows also sets the port for these (if they have one). */
3143 for(i = 0; i < sizeof(default_ports)/sizeof(default_ports[0]); ++i) {
3144 if(data->scheme_type == default_ports[i].scheme) {
3145 uri->has_port = TRUE;
3146 uri->port = default_ports[i].port;
3152 if(!canonicalize_path_opaque(data, uri, flags, computeOnly))
3156 if(uri->path_start > -1 && !computeOnly)
3157 /* Finding file extensions happens for both types of URIs. */
3158 uri->extension_offset = find_file_extension(uri->canon_uri+uri->path_start, uri->path_len);
3160 uri->extension_offset = -1;
3165 /* Attempts to canonicalize the query string of the URI.
3167 * Things that happen:
3168 * 1) For known scheme types forbidden characters
3169 * are percent encoded, unless the NO_DECODE_EXTRA_INFO flag is set
3170 * or NO_ENCODE_FORBIDDEN_CHARACTERS is set.
3172 * 2) For known scheme types, percent encoded, unreserved characters
3173 * are decoded as long as the NO_DECODE_EXTRA_INFO flag isn't set.
3175 static BOOL canonicalize_query(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
3176 const WCHAR *ptr, *end;
3177 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
3180 uri->query_start = -1;
3185 uri->query_start = uri->canon_len;
3187 end = data->query+data->query_len;
3188 for(ptr = data->query; ptr < end; ++ptr) {
3190 if(known_scheme && !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
3191 WCHAR val = decode_pct_val(ptr);
3192 if(is_unreserved(val)) {
3194 uri->canon_uri[uri->canon_len] = val;
3201 } else if(known_scheme && !is_unreserved(*ptr) && !is_reserved(*ptr)) {
3202 if(!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS) &&
3203 !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
3205 pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
3206 uri->canon_len += 3;
3212 uri->canon_uri[uri->canon_len] = *ptr;
3216 uri->query_len = uri->canon_len - uri->query_start;
3219 TRACE("(%p %p %x %d): Canonicalized query string %s len=%d\n", data, uri, flags,
3220 computeOnly, debugstr_wn(uri->canon_uri+uri->query_start, uri->query_len),
3225 static BOOL canonicalize_fragment(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
3226 const WCHAR *ptr, *end;
3227 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
3229 if(!data->fragment) {
3230 uri->fragment_start = -1;
3231 uri->fragment_len = 0;
3235 uri->fragment_start = uri->canon_len;
3237 end = data->fragment + data->fragment_len;
3238 for(ptr = data->fragment; ptr < end; ++ptr) {
3240 if(known_scheme && !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
3241 WCHAR val = decode_pct_val(ptr);
3242 if(is_unreserved(val)) {
3244 uri->canon_uri[uri->canon_len] = val;
3251 } else if(known_scheme && !is_unreserved(*ptr) && !is_reserved(*ptr)) {
3252 if(!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS) &&
3253 !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
3255 pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
3256 uri->canon_len += 3;
3262 uri->canon_uri[uri->canon_len] = *ptr;
3266 uri->fragment_len = uri->canon_len - uri->fragment_start;
3269 TRACE("(%p %p %x %d): Canonicalized fragment %s len=%d\n", data, uri, flags,
3270 computeOnly, debugstr_wn(uri->canon_uri+uri->fragment_start, uri->fragment_len),
3275 /* Canonicalizes the scheme information specified in the parse_data using the specified flags. */
3276 static BOOL canonicalize_scheme(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
3277 uri->scheme_start = -1;
3278 uri->scheme_len = 0;
3281 /* The only type of URI that doesn't have to have a scheme is a relative
3284 if(!data->is_relative) {
3285 FIXME("(%p %p %x): Unable to determine the scheme type of %s.\n", data,
3286 uri, flags, debugstr_w(data->uri));
3292 INT pos = uri->canon_len;
3294 for(i = 0; i < data->scheme_len; ++i) {
3295 /* Scheme name must be lower case after canonicalization. */
3296 uri->canon_uri[i + pos] = tolowerW(data->scheme[i]);
3299 uri->canon_uri[i + pos] = ':';
3300 uri->scheme_start = pos;
3302 TRACE("(%p %p %x): Canonicalized scheme=%s, len=%d.\n", data, uri, flags,
3303 debugstr_wn(uri->canon_uri, uri->scheme_len), data->scheme_len);
3306 /* This happens in both computation modes. */
3307 uri->canon_len += data->scheme_len + 1;
3308 uri->scheme_len = data->scheme_len;
3313 /* Computes what the length of the URI specified by the parse_data will be
3314 * after canonicalization occurs using the specified flags.
3316 * This function will return a non-zero value indicating the length of the canonicalized
3317 * URI, or -1 on error.
3319 static int compute_canonicalized_length(const parse_data *data, DWORD flags) {
3322 memset(&uri, 0, sizeof(Uri));
3324 TRACE("(%p %x): Beginning to compute canonicalized length for URI %s\n", data, flags,
3325 debugstr_w(data->uri));
3327 if(!canonicalize_scheme(data, &uri, flags, TRUE)) {
3328 ERR("(%p %x): Failed to compute URI scheme length.\n", data, flags);
3332 if(!canonicalize_hierpart(data, &uri, flags, TRUE)) {
3333 ERR("(%p %x): Failed to compute URI hierpart length.\n", data, flags);
3337 if(!canonicalize_query(data, &uri, flags, TRUE)) {
3338 ERR("(%p %x): Failed to compute query string length.\n", data, flags);
3342 if(!canonicalize_fragment(data, &uri, flags, TRUE)) {
3343 ERR("(%p %x): Failed to compute fragment length.\n", data, flags);
3347 TRACE("(%p %x): Finished computing canonicalized URI length. length=%d\n", data, flags, uri.canon_len);
3349 return uri.canon_len;
3352 /* Canonicalizes the URI data specified in the parse_data, using the given flags. If the
3353 * canonicalization succeeds it will store all the canonicalization information
3354 * in the pointer to the Uri.
3356 * To canonicalize a URI this function first computes what the length of the URI
3357 * specified by the parse_data will be. Once this is done it will then perform the actual
3358 * canonicalization of the URI.
3360 static HRESULT canonicalize_uri(const parse_data *data, Uri *uri, DWORD flags) {
3363 uri->canon_uri = NULL;
3364 uri->canon_size = uri->canon_len = 0;
3366 TRACE("(%p %p %x): beginning to canonicalize URI %s.\n", data, uri, flags, debugstr_w(data->uri));
3368 /* First try to compute the length of the URI. */
3369 len = compute_canonicalized_length(data, flags);
3371 ERR("(%p %p %x): Could not compute the canonicalized length of %s.\n", data, uri, flags,
3372 debugstr_w(data->uri));
3373 return E_INVALIDARG;
3376 uri->canon_uri = heap_alloc((len+1)*sizeof(WCHAR));
3378 return E_OUTOFMEMORY;
3380 uri->canon_size = len;
3381 if(!canonicalize_scheme(data, uri, flags, FALSE)) {
3382 ERR("(%p %p %x): Unable to canonicalize the scheme of the URI.\n", data, uri, flags);
3383 return E_INVALIDARG;
3385 uri->scheme_type = data->scheme_type;
3387 if(!canonicalize_hierpart(data, uri, flags, FALSE)) {
3388 ERR("(%p %p %x): Unable to canonicalize the heirpart of the URI\n", data, uri, flags);
3389 return E_INVALIDARG;
3392 if(!canonicalize_query(data, uri, flags, FALSE)) {
3393 ERR("(%p %p %x): Unable to canonicalize query string of the URI.\n",
3395 return E_INVALIDARG;
3398 if(!canonicalize_fragment(data, uri, flags, FALSE)) {
3399 ERR("(%p %p %x): Unable to canonicalize fragment of the URI.\n",
3401 return E_INVALIDARG;
3404 /* There's a possibility we didn't use all the space we allocated
3407 if(uri->canon_len < uri->canon_size) {
3408 /* This happens if the URI is hierarchical and dot
3409 * segments were removed from its path.
3411 WCHAR *tmp = heap_realloc(uri->canon_uri, (uri->canon_len+1)*sizeof(WCHAR));
3413 return E_OUTOFMEMORY;
3415 uri->canon_uri = tmp;
3416 uri->canon_size = uri->canon_len;
3419 uri->canon_uri[uri->canon_len] = '\0';
3420 TRACE("(%p %p %x): finished canonicalizing the URI. uri=%s\n", data, uri, flags, debugstr_w(uri->canon_uri));
3425 static HRESULT get_builder_component(LPWSTR *component, DWORD *component_len,
3426 LPCWSTR source, DWORD source_len,
3427 LPCWSTR *output, DWORD *output_len)
3440 if(!(*component) && source) {
3441 /* Allocate 'component', and copy the contents from 'source'
3442 * into the new allocation.
3444 *component = heap_alloc((source_len+1)*sizeof(WCHAR));
3446 return E_OUTOFMEMORY;
3448 memcpy(*component, source, source_len*sizeof(WCHAR));
3449 (*component)[source_len] = '\0';
3450 *component_len = source_len;
3453 *output = *component;
3454 *output_len = *component_len;
3455 return *output ? S_OK : S_FALSE;
3458 /* Allocates 'component' and copies the string from 'new_value' into 'component'.
3459 * If 'prefix' is set and 'new_value' isn't NULL, then it checks if 'new_value'
3460 * starts with 'prefix'. If it doesn't then 'prefix' is prepended to 'component'.
3462 * If everything is successful, then will set 'success_flag' in 'flags'.
3464 static HRESULT set_builder_component(LPWSTR *component, DWORD *component_len, LPCWSTR new_value,
3465 WCHAR prefix, DWORD *flags, DWORD success_flag)
3467 heap_free(*component);
3473 BOOL add_prefix = FALSE;
3474 DWORD len = lstrlenW(new_value);
3477 if(prefix && *new_value != prefix) {
3479 *component = heap_alloc((len+2)*sizeof(WCHAR));
3481 *component = heap_alloc((len+1)*sizeof(WCHAR));
3484 return E_OUTOFMEMORY;
3487 (*component)[pos++] = prefix;
3489 memcpy(*component+pos, new_value, (len+1)*sizeof(WCHAR));
3490 *component_len = len+pos;
3493 *flags |= success_flag;
3497 static void reset_builder(UriBuilder *builder) {
3499 IUri_Release(&builder->uri->IUri_iface);
3500 builder->uri = NULL;
3502 heap_free(builder->fragment);
3503 builder->fragment = NULL;
3504 builder->fragment_len = 0;
3506 heap_free(builder->host);
3507 builder->host = NULL;
3508 builder->host_len = 0;
3510 heap_free(builder->password);
3511 builder->password = NULL;
3512 builder->password_len = 0;
3514 heap_free(builder->path);
3515 builder->path = NULL;
3516 builder->path_len = 0;
3518 heap_free(builder->query);
3519 builder->query = NULL;
3520 builder->query_len = 0;
3522 heap_free(builder->scheme);
3523 builder->scheme = NULL;
3524 builder->scheme_len = 0;
3526 heap_free(builder->username);
3527 builder->username = NULL;
3528 builder->username_len = 0;
3530 builder->has_port = FALSE;
3532 builder->modified_props = 0;
3535 static HRESULT validate_scheme_name(const UriBuilder *builder, parse_data *data, DWORD flags) {
3536 const WCHAR *component;
3541 if(builder->scheme) {
3542 ptr = builder->scheme;
3543 expected_len = builder->scheme_len;
3544 } else if(builder->uri && builder->uri->scheme_start > -1) {
3545 ptr = builder->uri->canon_uri+builder->uri->scheme_start;
3546 expected_len = builder->uri->scheme_len;
3548 static const WCHAR nullW[] = {0};
3555 if(parse_scheme(pptr, data, flags, ALLOW_NULL_TERM_SCHEME) &&
3556 data->scheme_len == expected_len) {
3558 TRACE("(%p %p %x): Found valid scheme component %s len=%d.\n", builder, data, flags,
3559 debugstr_wn(data->scheme, data->scheme_len), data->scheme_len);
3561 TRACE("(%p %p %x): Invalid scheme component found %s.\n", builder, data, flags,
3562 debugstr_wn(component, expected_len));
3563 return INET_E_INVALID_URL;
3569 static HRESULT validate_username(const UriBuilder *builder, parse_data *data, DWORD flags) {
3574 if(builder->username) {
3575 ptr = builder->username;
3576 expected_len = builder->username_len;
3577 } else if(!(builder->modified_props & Uri_HAS_USER_NAME) && builder->uri &&
3578 builder->uri->userinfo_start > -1 && builder->uri->userinfo_split != 0) {
3579 /* Just use the username from the base Uri. */
3580 data->username = builder->uri->canon_uri+builder->uri->userinfo_start;
3581 data->username_len = (builder->uri->userinfo_split > -1) ?
3582 builder->uri->userinfo_split : builder->uri->userinfo_len;
3590 const WCHAR *component = ptr;
3592 if(parse_username(pptr, data, flags, ALLOW_NULL_TERM_USER_NAME) &&
3593 data->username_len == expected_len)
3594 TRACE("(%p %p %x): Found valid username component %s len=%d.\n", builder, data, flags,
3595 debugstr_wn(data->username, data->username_len), data->username_len);
3597 TRACE("(%p %p %x): Invalid username component found %s.\n", builder, data, flags,
3598 debugstr_wn(component, expected_len));
3599 return INET_E_INVALID_URL;
3606 static HRESULT validate_password(const UriBuilder *builder, parse_data *data, DWORD flags) {
3611 if(builder->password) {
3612 ptr = builder->password;
3613 expected_len = builder->password_len;
3614 } else if(!(builder->modified_props & Uri_HAS_PASSWORD) && builder->uri &&
3615 builder->uri->userinfo_split > -1) {
3616 data->password = builder->uri->canon_uri+builder->uri->userinfo_start+builder->uri->userinfo_split+1;
3617 data->password_len = builder->uri->userinfo_len-builder->uri->userinfo_split-1;
3625 const WCHAR *component = ptr;
3627 if(parse_password(pptr, data, flags, ALLOW_NULL_TERM_PASSWORD) &&
3628 data->password_len == expected_len)
3629 TRACE("(%p %p %x): Found valid password component %s len=%d.\n", builder, data, flags,
3630 debugstr_wn(data->password, data->password_len), data->password_len);
3632 TRACE("(%p %p %x): Invalid password component found %s.\n", builder, data, flags,
3633 debugstr_wn(component, expected_len));
3634 return INET_E_INVALID_URL;
3641 static HRESULT validate_userinfo(const UriBuilder *builder, parse_data *data, DWORD flags) {
3644 hr = validate_username(builder, data, flags);
3648 hr = validate_password(builder, data, flags);
3655 static HRESULT validate_host(const UriBuilder *builder, parse_data *data, DWORD flags) {
3661 ptr = builder->host;
3662 expected_len = builder->host_len;
3663 } else if(!(builder->modified_props & Uri_HAS_HOST) && builder->uri && builder->uri->host_start > -1) {
3664 ptr = builder->uri->canon_uri + builder->uri->host_start;
3665 expected_len = builder->uri->host_len;
3670 const WCHAR *component = ptr;
3671 DWORD extras = ALLOW_BRACKETLESS_IP_LITERAL|IGNORE_PORT_DELIMITER|SKIP_IP_FUTURE_CHECK;
3674 if(parse_host(pptr, data, flags, extras) && data->host_len == expected_len)
3675 TRACE("(%p %p %x): Found valid host name %s len=%d type=%d.\n", builder, data, flags,
3676 debugstr_wn(data->host, data->host_len), data->host_len, data->host_type);
3678 TRACE("(%p %p %x): Invalid host name found %s.\n", builder, data, flags,
3679 debugstr_wn(component, expected_len));
3680 return INET_E_INVALID_URL;
3687 static void setup_port(const UriBuilder *builder, parse_data *data, DWORD flags) {
3688 if(builder->modified_props & Uri_HAS_PORT) {
3689 if(builder->has_port) {
3690 data->has_port = TRUE;
3691 data->port_value = builder->port;
3693 } else if(builder->uri && builder->uri->has_port) {
3694 data->has_port = TRUE;
3695 data->port_value = builder->uri->port;
3699 TRACE("(%p %p %x): Using %u as port for IUri.\n", builder, data, flags, data->port_value);
3702 static HRESULT validate_path(const UriBuilder *builder, parse_data *data, DWORD flags) {
3703 const WCHAR *ptr = NULL;
3704 const WCHAR *component;
3707 BOOL check_len = TRUE;
3711 ptr = builder->path;
3712 expected_len = builder->path_len;
3713 } else if(!(builder->modified_props & Uri_HAS_PATH) &&
3714 builder->uri && builder->uri->path_start > -1) {
3715 ptr = builder->uri->canon_uri+builder->uri->path_start;
3716 expected_len = builder->uri->path_len;
3718 static const WCHAR nullW[] = {0};
3727 /* How the path is validated depends on what type of
3730 valid = data->is_opaque ?
3731 parse_path_opaque(pptr, data, flags) : parse_path_hierarchical(pptr, data, flags);
3733 if(!valid || (check_len && expected_len != data->path_len)) {
3734 TRACE("(%p %p %x): Invalid path component %s.\n", builder, data, flags,
3735 debugstr_wn(component, expected_len) );
3736 return INET_E_INVALID_URL;
3739 TRACE("(%p %p %x): Valid path component %s len=%d.\n", builder, data, flags,
3740 debugstr_wn(data->path, data->path_len), data->path_len);
3745 static HRESULT validate_query(const UriBuilder *builder, parse_data *data, DWORD flags) {
3746 const WCHAR *ptr = NULL;
3750 if(builder->query) {
3751 ptr = builder->query;
3752 expected_len = builder->query_len;
3753 } else if(!(builder->modified_props & Uri_HAS_QUERY) && builder->uri &&
3754 builder->uri->query_start > -1) {
3755 ptr = builder->uri->canon_uri+builder->uri->query_start;
3756 expected_len = builder->uri->query_len;
3760 const WCHAR *component = ptr;
3763 if(parse_query(pptr, data, flags) && expected_len == data->query_len)
3764 TRACE("(%p %p %x): Valid query component %s len=%d.\n", builder, data, flags,
3765 debugstr_wn(data->query, data->query_len), data->query_len);
3767 TRACE("(%p %p %x): Invalid query component %s.\n", builder, data, flags,
3768 debugstr_wn(component, expected_len));
3769 return INET_E_INVALID_URL;
3776 static HRESULT validate_fragment(const UriBuilder *builder, parse_data *data, DWORD flags) {
3777 const WCHAR *ptr = NULL;
3781 if(builder->fragment) {
3782 ptr = builder->fragment;
3783 expected_len = builder->fragment_len;
3784 } else if(!(builder->modified_props & Uri_HAS_FRAGMENT) && builder->uri &&
3785 builder->uri->fragment_start > -1) {
3786 ptr = builder->uri->canon_uri+builder->uri->fragment_start;
3787 expected_len = builder->uri->fragment_len;
3791 const WCHAR *component = ptr;
3794 if(parse_fragment(pptr, data, flags) && expected_len == data->fragment_len)
3795 TRACE("(%p %p %x): Valid fragment component %s len=%d.\n", builder, data, flags,
3796 debugstr_wn(data->fragment, data->fragment_len), data->fragment_len);
3798 TRACE("(%p %p %x): Invalid fragment component %s.\n", builder, data, flags,
3799 debugstr_wn(component, expected_len));
3800 return INET_E_INVALID_URL;
3807 static HRESULT validate_components(const UriBuilder *builder, parse_data *data, DWORD flags) {
3810 memset(data, 0, sizeof(parse_data));
3812 TRACE("(%p %p %x): Beginning to validate builder components.\n", builder, data, flags);
3814 hr = validate_scheme_name(builder, data, flags);
3818 /* Extra validation for file schemes. */
3819 if(data->scheme_type == URL_SCHEME_FILE) {
3820 if((builder->password || (builder->uri && builder->uri->userinfo_split > -1)) ||
3821 (builder->username || (builder->uri && builder->uri->userinfo_start > -1))) {
3822 TRACE("(%p %p %x): File schemes can't contain a username or password.\n",
3823 builder, data, flags);
3824 return INET_E_INVALID_URL;
3828 hr = validate_userinfo(builder, data, flags);
3832 hr = validate_host(builder, data, flags);
3836 setup_port(builder, data, flags);
3838 /* The URI is opaque if it doesn't have an authority component. */
3839 if(!data->is_relative)
3840 data->is_opaque = !data->username && !data->password && !data->host && !data->has_port
3841 && data->scheme_type != URL_SCHEME_FILE;
3843 data->is_opaque = !data->host && !data->has_port;
3845 hr = validate_path(builder, data, flags);
3849 hr = validate_query(builder, data, flags);
3853 hr = validate_fragment(builder, data, flags);
3857 TRACE("(%p %p %x): Finished validating builder components.\n", builder, data, flags);
3862 static HRESULT compare_file_paths(const Uri *a, const Uri *b, BOOL *ret)
3864 WCHAR *canon_path_a, *canon_path_b;
3868 *ret = !b->path_len;
3878 if(a->path_len == b->path_len && !memicmpW(a->canon_uri+a->path_start, b->canon_uri+b->path_start, a->path_len)) {
3883 len_a = canonicalize_path_hierarchical(a->canon_uri+a->path_start, a->path_len, a->scheme_type, FALSE, 0, NULL);
3884 len_b = canonicalize_path_hierarchical(b->canon_uri+b->path_start, b->path_len, b->scheme_type, FALSE, 0, NULL);
3886 canon_path_a = heap_alloc(len_a*sizeof(WCHAR));
3888 return E_OUTOFMEMORY;
3889 canon_path_b = heap_alloc(len_b*sizeof(WCHAR));
3891 heap_free(canon_path_a);
3892 return E_OUTOFMEMORY;
3895 len_a = canonicalize_path_hierarchical(a->canon_uri+a->path_start, a->path_len, a->scheme_type, FALSE, 0, canon_path_a);
3896 len_b = canonicalize_path_hierarchical(b->canon_uri+b->path_start, b->path_len, b->scheme_type, FALSE, 0, canon_path_b);
3898 *ret = len_a == len_b && !memicmpW(canon_path_a, canon_path_b, len_a);
3900 heap_free(canon_path_a);
3901 heap_free(canon_path_b);
3905 /* Checks if the two Uri's are logically equivalent. It's a simple
3906 * comparison, since they are both of type Uri, and it can access
3907 * the properties of each Uri directly without the need to go
3908 * through the "IUri_Get*" interface calls.
3910 static HRESULT compare_uris(const Uri *a, const Uri *b, BOOL *ret) {
3911 const BOOL known_scheme = a->scheme_type != URL_SCHEME_UNKNOWN;
3912 const BOOL are_hierarchical = a->authority_start > -1 && b->authority_start > -1;
3917 if(a->scheme_type != b->scheme_type)
3920 /* Only compare the scheme names (if any) if their unknown scheme types. */
3922 if((a->scheme_start > -1 && b->scheme_start > -1) &&
3923 (a->scheme_len == b->scheme_len)) {
3924 /* Make sure the schemes are the same. */
3925 if(StrCmpNW(a->canon_uri+a->scheme_start, b->canon_uri+b->scheme_start, a->scheme_len))
3927 } else if(a->scheme_len != b->scheme_len)
3928 /* One of the Uri's has a scheme name, while the other doesn't. */
3932 /* If they have a userinfo component, perform case sensitive compare. */
3933 if((a->userinfo_start > -1 && b->userinfo_start > -1) &&
3934 (a->userinfo_len == b->userinfo_len)) {
3935 if(StrCmpNW(a->canon_uri+a->userinfo_start, b->canon_uri+b->userinfo_start, a->userinfo_len))
3937 } else if(a->userinfo_len != b->userinfo_len)
3938 /* One of the Uri's had a userinfo, while the other one doesn't. */
3941 /* Check if they have a host name. */
3942 if((a->host_start > -1 && b->host_start > -1) &&
3943 (a->host_len == b->host_len)) {
3944 /* Perform a case insensitive compare if they are a known scheme type. */
3946 if(StrCmpNIW(a->canon_uri+a->host_start, b->canon_uri+b->host_start, a->host_len))
3948 } else if(StrCmpNW(a->canon_uri+a->host_start, b->canon_uri+b->host_start, a->host_len))
3950 } else if(a->host_len != b->host_len)
3951 /* One of the Uri's had a host, while the other one didn't. */
3954 if(a->has_port && b->has_port) {
3955 if(a->port != b->port)
3957 } else if(a->has_port || b->has_port)
3958 /* One had a port, while the other one didn't. */
3961 /* Windows is weird with how it handles paths. For example
3962 * One URI could be "http://google.com" (after canonicalization)
3963 * and one could be "http://google.com/" and the IsEqual function
3964 * would still evaluate to TRUE, but, only if they are both hierarchical
3967 if(a->scheme_type == URL_SCHEME_FILE) {
3970 hres = compare_file_paths(a, b, &cmp);
3971 if(FAILED(hres) || !cmp)
3973 } else if((a->path_start > -1 && b->path_start > -1) &&
3974 (a->path_len == b->path_len)) {
3975 if(StrCmpNW(a->canon_uri+a->path_start, b->canon_uri+b->path_start, a->path_len))
3977 } else if(are_hierarchical && a->path_len == -1 && b->path_len == 0) {
3978 if(*(a->canon_uri+a->path_start) != '/')
3980 } else if(are_hierarchical && b->path_len == 1 && a->path_len == 0) {
3981 if(*(b->canon_uri+b->path_start) != '/')
3983 } else if(a->path_len != b->path_len)
3986 /* Compare the query strings of the two URIs. */
3987 if((a->query_start > -1 && b->query_start > -1) &&
3988 (a->query_len == b->query_len)) {
3989 if(StrCmpNW(a->canon_uri+a->query_start, b->canon_uri+b->query_start, a->query_len))
3991 } else if(a->query_len != b->query_len)
3994 if((a->fragment_start > -1 && b->fragment_start > -1) &&
3995 (a->fragment_len == b->fragment_len)) {
3996 if(StrCmpNW(a->canon_uri+a->fragment_start, b->canon_uri+b->fragment_start, a->fragment_len))
3998 } else if(a->fragment_len != b->fragment_len)
4001 /* If we get here, the two URIs are equivalent. */
4006 static void convert_to_dos_path(const WCHAR *path, DWORD path_len,
4007 WCHAR *output, DWORD *output_len)
4009 const WCHAR *ptr = path;
4011 if(path_len > 3 && *ptr == '/' && is_drive_path(path+1))
4012 /* Skip over the leading / before the drive path. */
4015 for(; ptr < path+path_len; ++ptr) {
4028 /* Generates a raw uri string using the parse_data. */
4029 static DWORD generate_raw_uri(const parse_data *data, BSTR uri, DWORD flags) {
4034 memcpy(uri, data->scheme, data->scheme_len*sizeof(WCHAR));
4035 uri[data->scheme_len] = ':';
4037 length += data->scheme_len+1;
4040 if(!data->is_opaque) {
4041 /* For the "//" which appears before the authority component. */
4044 uri[length+1] = '/';
4048 /* Check if we need to add the "\\" before the host name
4049 * of a UNC server name in a DOS path.
4051 if(flags & RAW_URI_CONVERT_TO_DOS_PATH &&
4052 data->scheme_type == URL_SCHEME_FILE && data->host) {
4055 uri[length+1] = '\\';
4061 if(data->username) {
4063 memcpy(uri+length, data->username, data->username_len*sizeof(WCHAR));
4064 length += data->username_len;
4067 if(data->password) {
4070 memcpy(uri+length+1, data->password, data->password_len*sizeof(WCHAR));
4072 length += data->password_len+1;
4075 if(data->password || data->username) {
4082 /* IPv6 addresses get the brackets added around them if they don't already
4085 const BOOL add_brackets = data->host_type == Uri_HOST_IPV6 && *(data->host) != '[';
4093 memcpy(uri+length, data->host, data->host_len*sizeof(WCHAR));
4094 length += data->host_len;
4103 if(data->has_port) {
4104 /* The port isn't included in the raw uri if it's the default
4105 * port for the scheme type.
4108 BOOL is_default = FALSE;
4110 for(i = 0; i < sizeof(default_ports)/sizeof(default_ports[0]); ++i) {
4111 if(data->scheme_type == default_ports[i].scheme &&
4112 data->port_value == default_ports[i].port)
4116 if(!is_default || flags & RAW_URI_FORCE_PORT_DISP) {
4122 length += ui2str(uri+length, data->port_value);
4124 length += ui2str(NULL, data->port_value);
4128 /* Check if a '/' should be added before the path for hierarchical URIs. */
4129 if(!data->is_opaque && data->path && *(data->path) != '/') {
4136 if(!data->is_opaque && data->scheme_type == URL_SCHEME_FILE &&
4137 flags & RAW_URI_CONVERT_TO_DOS_PATH) {
4141 convert_to_dos_path(data->path, data->path_len, uri+length, &len);
4143 convert_to_dos_path(data->path, data->path_len, NULL, &len);
4148 memcpy(uri+length, data->path, data->path_len*sizeof(WCHAR));
4149 length += data->path_len;
4155 memcpy(uri+length, data->query, data->query_len*sizeof(WCHAR));
4156 length += data->query_len;
4159 if(data->fragment) {
4161 memcpy(uri+length, data->fragment, data->fragment_len*sizeof(WCHAR));
4162 length += data->fragment_len;
4166 TRACE("(%p %p): Generated raw uri=%s len=%d\n", data, uri, debugstr_wn(uri, length), length);
4168 TRACE("(%p %p): Computed raw uri len=%d\n", data, uri, length);
4173 static HRESULT generate_uri(const UriBuilder *builder, const parse_data *data, Uri *uri, DWORD flags) {
4175 DWORD length = generate_raw_uri(data, NULL, 0);
4176 uri->raw_uri = SysAllocStringLen(NULL, length);
4178 return E_OUTOFMEMORY;
4180 generate_raw_uri(data, uri->raw_uri, 0);
4182 hr = canonicalize_uri(data, uri, flags);
4184 if(hr == E_INVALIDARG)
4185 return INET_E_INVALID_URL;
4189 uri->create_flags = flags;
4193 static inline Uri* impl_from_IUri(IUri *iface)
4195 return CONTAINING_RECORD(iface, Uri, IUri_iface);
4198 static inline void destory_uri_obj(Uri *This)
4200 SysFreeString(This->raw_uri);
4201 heap_free(This->canon_uri);
4205 static HRESULT WINAPI Uri_QueryInterface(IUri *iface, REFIID riid, void **ppv)
4207 Uri *This = impl_from_IUri(iface);
4209 if(IsEqualGUID(&IID_IUnknown, riid)) {
4210 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
4211 *ppv = &This->IUri_iface;
4212 }else if(IsEqualGUID(&IID_IUri, riid)) {
4213 TRACE("(%p)->(IID_IUri %p)\n", This, ppv);
4214 *ppv = &This->IUri_iface;
4215 }else if(IsEqualGUID(&IID_IUriBuilderFactory, riid)) {
4216 TRACE("(%p)->(IID_IUriBuilderFactory %p)\n", This, riid);
4217 *ppv = &This->IUriBuilderFactory_iface;
4218 }else if(IsEqualGUID(&IID_IUriObj, riid)) {
4219 TRACE("(%p)->(IID_IUriObj %p)\n", This, ppv);
4223 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
4225 return E_NOINTERFACE;
4228 IUnknown_AddRef((IUnknown*)*ppv);
4232 static ULONG WINAPI Uri_AddRef(IUri *iface)
4234 Uri *This = impl_from_IUri(iface);
4235 LONG ref = InterlockedIncrement(&This->ref);
4237 TRACE("(%p) ref=%d\n", This, ref);
4242 static ULONG WINAPI Uri_Release(IUri *iface)
4244 Uri *This = impl_from_IUri(iface);
4245 LONG ref = InterlockedDecrement(&This->ref);
4247 TRACE("(%p) ref=%d\n", This, ref);
4250 destory_uri_obj(This);
4255 static HRESULT WINAPI Uri_GetPropertyBSTR(IUri *iface, Uri_PROPERTY uriProp, BSTR *pbstrProperty, DWORD dwFlags)
4257 Uri *This = impl_from_IUri(iface);
4259 TRACE("(%p %s)->(%d %p %x)\n", This, debugstr_w(This->canon_uri), uriProp, pbstrProperty, dwFlags);
4264 if(uriProp > Uri_PROPERTY_STRING_LAST) {
4265 /* Windows allocates an empty BSTR for invalid Uri_PROPERTY's. */
4266 *pbstrProperty = SysAllocStringLen(NULL, 0);
4267 if(!(*pbstrProperty))
4268 return E_OUTOFMEMORY;
4270 /* It only returns S_FALSE for the ZONE property... */
4271 if(uriProp == Uri_PROPERTY_ZONE)
4277 /* Don't have support for flags yet. */
4279 FIXME("(%p)->(%d %p %x)\n", This, uriProp, pbstrProperty, dwFlags);
4284 case Uri_PROPERTY_ABSOLUTE_URI:
4285 if(This->display_modifiers & URI_DISPLAY_NO_ABSOLUTE_URI) {
4286 *pbstrProperty = SysAllocStringLen(NULL, 0);
4289 if(This->scheme_type != URL_SCHEME_UNKNOWN && This->userinfo_start > -1) {
4290 if(This->userinfo_len == 0) {
4291 /* Don't include the '@' after the userinfo component. */
4292 *pbstrProperty = SysAllocStringLen(NULL, This->canon_len-1);
4294 if(*pbstrProperty) {
4295 /* Copy everything before it. */
4296 memcpy(*pbstrProperty, This->canon_uri, This->userinfo_start*sizeof(WCHAR));
4298 /* And everything after it. */
4299 memcpy(*pbstrProperty+This->userinfo_start, This->canon_uri+This->userinfo_start+1,
4300 (This->canon_len-This->userinfo_start-1)*sizeof(WCHAR));
4302 } else if(This->userinfo_split == 0 && This->userinfo_len == 1) {
4303 /* Don't include the ":@" */
4304 *pbstrProperty = SysAllocStringLen(NULL, This->canon_len-2);
4306 if(*pbstrProperty) {
4307 memcpy(*pbstrProperty, This->canon_uri, This->userinfo_start*sizeof(WCHAR));
4308 memcpy(*pbstrProperty+This->userinfo_start, This->canon_uri+This->userinfo_start+2,
4309 (This->canon_len-This->userinfo_start-2)*sizeof(WCHAR));
4312 *pbstrProperty = SysAllocString(This->canon_uri);
4316 *pbstrProperty = SysAllocString(This->canon_uri);
4321 if(!(*pbstrProperty))
4322 hres = E_OUTOFMEMORY;
4325 case Uri_PROPERTY_AUTHORITY:
4326 if(This->authority_start > -1) {
4327 if(This->port_offset > -1 && is_default_port(This->scheme_type, This->port) &&
4328 This->display_modifiers & URI_DISPLAY_NO_DEFAULT_PORT_AUTH)
4329 /* Don't include the port in the authority component. */
4330 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->authority_start, This->port_offset);
4332 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->authority_start, This->authority_len);
4335 *pbstrProperty = SysAllocStringLen(NULL, 0);
4339 if(!(*pbstrProperty))
4340 hres = E_OUTOFMEMORY;
4343 case Uri_PROPERTY_DISPLAY_URI:
4344 /* The Display URI contains everything except for the userinfo for known
4347 if(This->scheme_type != URL_SCHEME_UNKNOWN && This->userinfo_start > -1) {
4348 *pbstrProperty = SysAllocStringLen(NULL, This->canon_len-This->userinfo_len);
4350 if(*pbstrProperty) {
4351 /* Copy everything before the userinfo over. */
4352 memcpy(*pbstrProperty, This->canon_uri, This->userinfo_start*sizeof(WCHAR));
4353 /* Copy everything after the userinfo over. */
4354 memcpy(*pbstrProperty+This->userinfo_start,
4355 This->canon_uri+This->userinfo_start+This->userinfo_len+1,
4356 (This->canon_len-(This->userinfo_start+This->userinfo_len+1))*sizeof(WCHAR));
4359 *pbstrProperty = SysAllocString(This->canon_uri);
4361 if(!(*pbstrProperty))
4362 hres = E_OUTOFMEMORY;
4367 case Uri_PROPERTY_DOMAIN:
4368 if(This->domain_offset > -1) {
4369 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->host_start+This->domain_offset,
4370 This->host_len-This->domain_offset);
4373 *pbstrProperty = SysAllocStringLen(NULL, 0);
4377 if(!(*pbstrProperty))
4378 hres = E_OUTOFMEMORY;
4381 case Uri_PROPERTY_EXTENSION:
4382 if(This->extension_offset > -1) {
4383 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->path_start+This->extension_offset,
4384 This->path_len-This->extension_offset);
4387 *pbstrProperty = SysAllocStringLen(NULL, 0);
4391 if(!(*pbstrProperty))
4392 hres = E_OUTOFMEMORY;
4395 case Uri_PROPERTY_FRAGMENT:
4396 if(This->fragment_start > -1) {
4397 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->fragment_start, This->fragment_len);
4400 *pbstrProperty = SysAllocStringLen(NULL, 0);
4404 if(!(*pbstrProperty))
4405 hres = E_OUTOFMEMORY;
4408 case Uri_PROPERTY_HOST:
4409 if(This->host_start > -1) {
4410 /* The '[' and ']' aren't included for IPv6 addresses. */
4411 if(This->host_type == Uri_HOST_IPV6)
4412 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->host_start+1, This->host_len-2);
4414 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->host_start, This->host_len);
4418 *pbstrProperty = SysAllocStringLen(NULL, 0);
4422 if(!(*pbstrProperty))
4423 hres = E_OUTOFMEMORY;
4426 case Uri_PROPERTY_PASSWORD:
4427 if(This->userinfo_split > -1) {
4428 *pbstrProperty = SysAllocStringLen(
4429 This->canon_uri+This->userinfo_start+This->userinfo_split+1,
4430 This->userinfo_len-This->userinfo_split-1);
4433 *pbstrProperty = SysAllocStringLen(NULL, 0);
4437 if(!(*pbstrProperty))
4438 return E_OUTOFMEMORY;
4441 case Uri_PROPERTY_PATH:
4442 if(This->path_start > -1) {
4443 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->path_start, This->path_len);
4446 *pbstrProperty = SysAllocStringLen(NULL, 0);
4450 if(!(*pbstrProperty))
4451 hres = E_OUTOFMEMORY;
4454 case Uri_PROPERTY_PATH_AND_QUERY:
4455 if(This->path_start > -1) {
4456 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->path_start, This->path_len+This->query_len);
4458 } else if(This->query_start > -1) {
4459 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->query_start, This->query_len);
4462 *pbstrProperty = SysAllocStringLen(NULL, 0);
4466 if(!(*pbstrProperty))
4467 hres = E_OUTOFMEMORY;
4470 case Uri_PROPERTY_QUERY:
4471 if(This->query_start > -1) {
4472 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->query_start, This->query_len);
4475 *pbstrProperty = SysAllocStringLen(NULL, 0);
4479 if(!(*pbstrProperty))
4480 hres = E_OUTOFMEMORY;
4483 case Uri_PROPERTY_RAW_URI:
4484 *pbstrProperty = SysAllocString(This->raw_uri);
4485 if(!(*pbstrProperty))
4486 hres = E_OUTOFMEMORY;
4490 case Uri_PROPERTY_SCHEME_NAME:
4491 if(This->scheme_start > -1) {
4492 *pbstrProperty = SysAllocStringLen(This->canon_uri + This->scheme_start, This->scheme_len);
4495 *pbstrProperty = SysAllocStringLen(NULL, 0);
4499 if(!(*pbstrProperty))
4500 hres = E_OUTOFMEMORY;
4503 case Uri_PROPERTY_USER_INFO:
4504 if(This->userinfo_start > -1) {
4505 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->userinfo_start, This->userinfo_len);
4508 *pbstrProperty = SysAllocStringLen(NULL, 0);
4512 if(!(*pbstrProperty))
4513 hres = E_OUTOFMEMORY;
4516 case Uri_PROPERTY_USER_NAME:
4517 if(This->userinfo_start > -1 && This->userinfo_split != 0) {
4518 /* If userinfo_split is set, that means a password exists
4519 * so the username is only from userinfo_start to userinfo_split.
4521 if(This->userinfo_split > -1) {
4522 *pbstrProperty = SysAllocStringLen(This->canon_uri + This->userinfo_start, This->userinfo_split);
4525 *pbstrProperty = SysAllocStringLen(This->canon_uri + This->userinfo_start, This->userinfo_len);
4529 *pbstrProperty = SysAllocStringLen(NULL, 0);
4533 if(!(*pbstrProperty))
4534 return E_OUTOFMEMORY;
4538 FIXME("(%p)->(%d %p %x)\n", This, uriProp, pbstrProperty, dwFlags);
4545 static HRESULT WINAPI Uri_GetPropertyLength(IUri *iface, Uri_PROPERTY uriProp, DWORD *pcchProperty, DWORD dwFlags)
4547 Uri *This = impl_from_IUri(iface);
4549 TRACE("(%p %s)->(%d %p %x)\n", This, debugstr_w(This->canon_uri), uriProp, pcchProperty, dwFlags);
4552 return E_INVALIDARG;
4554 /* Can only return a length for a property if it's a string. */
4555 if(uriProp > Uri_PROPERTY_STRING_LAST)
4556 return E_INVALIDARG;
4558 /* Don't have support for flags yet. */
4560 FIXME("(%p)->(%d %p %x)\n", This, uriProp, pcchProperty, dwFlags);
4565 case Uri_PROPERTY_ABSOLUTE_URI:
4566 if(This->display_modifiers & URI_DISPLAY_NO_ABSOLUTE_URI) {
4570 if(This->scheme_type != URL_SCHEME_UNKNOWN) {
4571 if(This->userinfo_start > -1 && This->userinfo_len == 0)
4572 /* Don't include the '@' in the length. */
4573 *pcchProperty = This->canon_len-1;
4574 else if(This->userinfo_start > -1 && This->userinfo_len == 1 &&
4575 This->userinfo_split == 0)
4576 /* Don't include the ":@" in the length. */
4577 *pcchProperty = This->canon_len-2;
4579 *pcchProperty = This->canon_len;
4581 *pcchProperty = This->canon_len;
4587 case Uri_PROPERTY_AUTHORITY:
4588 if(This->port_offset > -1 &&
4589 This->display_modifiers & URI_DISPLAY_NO_DEFAULT_PORT_AUTH &&
4590 is_default_port(This->scheme_type, This->port))
4591 /* Only count up until the port in the authority. */
4592 *pcchProperty = This->port_offset;
4594 *pcchProperty = This->authority_len;
4595 hres = (This->authority_start > -1) ? S_OK : S_FALSE;
4597 case Uri_PROPERTY_DISPLAY_URI:
4598 if(This->scheme_type != URL_SCHEME_UNKNOWN && This->userinfo_start > -1)
4599 *pcchProperty = This->canon_len-This->userinfo_len-1;
4601 *pcchProperty = This->canon_len;
4605 case Uri_PROPERTY_DOMAIN:
4606 if(This->domain_offset > -1)
4607 *pcchProperty = This->host_len - This->domain_offset;
4611 hres = (This->domain_offset > -1) ? S_OK : S_FALSE;
4613 case Uri_PROPERTY_EXTENSION:
4614 if(This->extension_offset > -1) {
4615 *pcchProperty = This->path_len - This->extension_offset;
4623 case Uri_PROPERTY_FRAGMENT:
4624 *pcchProperty = This->fragment_len;
4625 hres = (This->fragment_start > -1) ? S_OK : S_FALSE;
4627 case Uri_PROPERTY_HOST:
4628 *pcchProperty = This->host_len;
4630 /* '[' and ']' aren't included in the length. */
4631 if(This->host_type == Uri_HOST_IPV6)
4634 hres = (This->host_start > -1) ? S_OK : S_FALSE;
4636 case Uri_PROPERTY_PASSWORD:
4637 *pcchProperty = (This->userinfo_split > -1) ? This->userinfo_len-This->userinfo_split-1 : 0;
4638 hres = (This->userinfo_split > -1) ? S_OK : S_FALSE;
4640 case Uri_PROPERTY_PATH:
4641 *pcchProperty = This->path_len;
4642 hres = (This->path_start > -1) ? S_OK : S_FALSE;
4644 case Uri_PROPERTY_PATH_AND_QUERY:
4645 *pcchProperty = This->path_len+This->query_len;
4646 hres = (This->path_start > -1 || This->query_start > -1) ? S_OK : S_FALSE;
4648 case Uri_PROPERTY_QUERY:
4649 *pcchProperty = This->query_len;
4650 hres = (This->query_start > -1) ? S_OK : S_FALSE;
4652 case Uri_PROPERTY_RAW_URI:
4653 *pcchProperty = SysStringLen(This->raw_uri);
4656 case Uri_PROPERTY_SCHEME_NAME:
4657 *pcchProperty = This->scheme_len;
4658 hres = (This->scheme_start > -1) ? S_OK : S_FALSE;
4660 case Uri_PROPERTY_USER_INFO:
4661 *pcchProperty = This->userinfo_len;
4662 hres = (This->userinfo_start > -1) ? S_OK : S_FALSE;
4664 case Uri_PROPERTY_USER_NAME:
4665 *pcchProperty = (This->userinfo_split > -1) ? This->userinfo_split : This->userinfo_len;
4666 if(This->userinfo_split == 0)
4669 hres = (This->userinfo_start > -1) ? S_OK : S_FALSE;
4672 FIXME("(%p)->(%d %p %x)\n", This, uriProp, pcchProperty, dwFlags);
4679 static HRESULT WINAPI Uri_GetPropertyDWORD(IUri *iface, Uri_PROPERTY uriProp, DWORD *pcchProperty, DWORD dwFlags)
4681 Uri *This = impl_from_IUri(iface);
4684 TRACE("(%p %s)->(%d %p %x)\n", This, debugstr_w(This->canon_uri), uriProp, pcchProperty, dwFlags);
4687 return E_INVALIDARG;
4689 /* Microsoft's implementation for the ZONE property of a URI seems to be lacking...
4690 * From what I can tell, instead of checking which URLZONE the URI belongs to it
4691 * simply assigns URLZONE_INVALID and returns E_NOTIMPL. This also applies to the GetZone
4694 if(uriProp == Uri_PROPERTY_ZONE) {
4695 *pcchProperty = URLZONE_INVALID;
4699 if(uriProp < Uri_PROPERTY_DWORD_START) {
4701 return E_INVALIDARG;
4705 case Uri_PROPERTY_HOST_TYPE:
4706 *pcchProperty = This->host_type;
4709 case Uri_PROPERTY_PORT:
4710 if(!This->has_port) {
4714 *pcchProperty = This->port;
4719 case Uri_PROPERTY_SCHEME:
4720 *pcchProperty = This->scheme_type;
4724 FIXME("(%p)->(%d %p %x)\n", This, uriProp, pcchProperty, dwFlags);
4731 static HRESULT WINAPI Uri_HasProperty(IUri *iface, Uri_PROPERTY uriProp, BOOL *pfHasProperty)
4733 Uri *This = impl_from_IUri(iface);
4735 TRACE("(%p %s)->(%d %p)\n", This, debugstr_w(This->canon_uri), uriProp, pfHasProperty);
4738 return E_INVALIDARG;
4741 case Uri_PROPERTY_ABSOLUTE_URI:
4742 *pfHasProperty = !(This->display_modifiers & URI_DISPLAY_NO_ABSOLUTE_URI);
4744 case Uri_PROPERTY_AUTHORITY:
4745 *pfHasProperty = This->authority_start > -1;
4747 case Uri_PROPERTY_DISPLAY_URI:
4748 *pfHasProperty = TRUE;
4750 case Uri_PROPERTY_DOMAIN:
4751 *pfHasProperty = This->domain_offset > -1;
4753 case Uri_PROPERTY_EXTENSION:
4754 *pfHasProperty = This->extension_offset > -1;
4756 case Uri_PROPERTY_FRAGMENT:
4757 *pfHasProperty = This->fragment_start > -1;
4759 case Uri_PROPERTY_HOST:
4760 *pfHasProperty = This->host_start > -1;
4762 case Uri_PROPERTY_PASSWORD:
4763 *pfHasProperty = This->userinfo_split > -1;
4765 case Uri_PROPERTY_PATH:
4766 *pfHasProperty = This->path_start > -1;
4768 case Uri_PROPERTY_PATH_AND_QUERY:
4769 *pfHasProperty = (This->path_start > -1 || This->query_start > -1);
4771 case Uri_PROPERTY_QUERY:
4772 *pfHasProperty = This->query_start > -1;
4774 case Uri_PROPERTY_RAW_URI:
4775 *pfHasProperty = TRUE;
4777 case Uri_PROPERTY_SCHEME_NAME:
4778 *pfHasProperty = This->scheme_start > -1;
4780 case Uri_PROPERTY_USER_INFO:
4781 *pfHasProperty = This->userinfo_start > -1;
4783 case Uri_PROPERTY_USER_NAME:
4784 if(This->userinfo_split == 0)
4785 *pfHasProperty = FALSE;
4787 *pfHasProperty = This->userinfo_start > -1;
4789 case Uri_PROPERTY_HOST_TYPE:
4790 *pfHasProperty = TRUE;
4792 case Uri_PROPERTY_PORT:
4793 *pfHasProperty = This->has_port;
4795 case Uri_PROPERTY_SCHEME:
4796 *pfHasProperty = TRUE;
4798 case Uri_PROPERTY_ZONE:
4799 *pfHasProperty = FALSE;
4802 FIXME("(%p)->(%d %p): Unsupported property type.\n", This, uriProp, pfHasProperty);
4809 static HRESULT WINAPI Uri_GetAbsoluteUri(IUri *iface, BSTR *pstrAbsoluteUri)
4811 TRACE("(%p)->(%p)\n", iface, pstrAbsoluteUri);
4812 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_ABSOLUTE_URI, pstrAbsoluteUri, 0);
4815 static HRESULT WINAPI Uri_GetAuthority(IUri *iface, BSTR *pstrAuthority)
4817 TRACE("(%p)->(%p)\n", iface, pstrAuthority);
4818 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_AUTHORITY, pstrAuthority, 0);
4821 static HRESULT WINAPI Uri_GetDisplayUri(IUri *iface, BSTR *pstrDisplayUri)
4823 TRACE("(%p)->(%p)\n", iface, pstrDisplayUri);
4824 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_DISPLAY_URI, pstrDisplayUri, 0);
4827 static HRESULT WINAPI Uri_GetDomain(IUri *iface, BSTR *pstrDomain)
4829 TRACE("(%p)->(%p)\n", iface, pstrDomain);
4830 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_DOMAIN, pstrDomain, 0);
4833 static HRESULT WINAPI Uri_GetExtension(IUri *iface, BSTR *pstrExtension)
4835 TRACE("(%p)->(%p)\n", iface, pstrExtension);
4836 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_EXTENSION, pstrExtension, 0);
4839 static HRESULT WINAPI Uri_GetFragment(IUri *iface, BSTR *pstrFragment)
4841 TRACE("(%p)->(%p)\n", iface, pstrFragment);
4842 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_FRAGMENT, pstrFragment, 0);
4845 static HRESULT WINAPI Uri_GetHost(IUri *iface, BSTR *pstrHost)
4847 TRACE("(%p)->(%p)\n", iface, pstrHost);
4848 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_HOST, pstrHost, 0);
4851 static HRESULT WINAPI Uri_GetPassword(IUri *iface, BSTR *pstrPassword)
4853 TRACE("(%p)->(%p)\n", iface, pstrPassword);
4854 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_PASSWORD, pstrPassword, 0);
4857 static HRESULT WINAPI Uri_GetPath(IUri *iface, BSTR *pstrPath)
4859 TRACE("(%p)->(%p)\n", iface, pstrPath);
4860 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_PATH, pstrPath, 0);
4863 static HRESULT WINAPI Uri_GetPathAndQuery(IUri *iface, BSTR *pstrPathAndQuery)
4865 TRACE("(%p)->(%p)\n", iface, pstrPathAndQuery);
4866 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_PATH_AND_QUERY, pstrPathAndQuery, 0);
4869 static HRESULT WINAPI Uri_GetQuery(IUri *iface, BSTR *pstrQuery)
4871 TRACE("(%p)->(%p)\n", iface, pstrQuery);
4872 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_QUERY, pstrQuery, 0);
4875 static HRESULT WINAPI Uri_GetRawUri(IUri *iface, BSTR *pstrRawUri)
4877 TRACE("(%p)->(%p)\n", iface, pstrRawUri);
4878 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_RAW_URI, pstrRawUri, 0);
4881 static HRESULT WINAPI Uri_GetSchemeName(IUri *iface, BSTR *pstrSchemeName)
4883 TRACE("(%p)->(%p)\n", iface, pstrSchemeName);
4884 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_SCHEME_NAME, pstrSchemeName, 0);
4887 static HRESULT WINAPI Uri_GetUserInfo(IUri *iface, BSTR *pstrUserInfo)
4889 TRACE("(%p)->(%p)\n", iface, pstrUserInfo);
4890 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_USER_INFO, pstrUserInfo, 0);
4893 static HRESULT WINAPI Uri_GetUserName(IUri *iface, BSTR *pstrUserName)
4895 TRACE("(%p)->(%p)\n", iface, pstrUserName);
4896 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_USER_NAME, pstrUserName, 0);
4899 static HRESULT WINAPI Uri_GetHostType(IUri *iface, DWORD *pdwHostType)
4901 TRACE("(%p)->(%p)\n", iface, pdwHostType);
4902 return IUri_GetPropertyDWORD(iface, Uri_PROPERTY_HOST_TYPE, pdwHostType, 0);
4905 static HRESULT WINAPI Uri_GetPort(IUri *iface, DWORD *pdwPort)
4907 TRACE("(%p)->(%p)\n", iface, pdwPort);
4908 return IUri_GetPropertyDWORD(iface, Uri_PROPERTY_PORT, pdwPort, 0);
4911 static HRESULT WINAPI Uri_GetScheme(IUri *iface, DWORD *pdwScheme)
4913 TRACE("(%p)->(%p)\n", iface, pdwScheme);
4914 return IUri_GetPropertyDWORD(iface, Uri_PROPERTY_SCHEME, pdwScheme, 0);
4917 static HRESULT WINAPI Uri_GetZone(IUri *iface, DWORD *pdwZone)
4919 TRACE("(%p)->(%p)\n", iface, pdwZone);
4920 return IUri_GetPropertyDWORD(iface, Uri_PROPERTY_ZONE,pdwZone, 0);
4923 static HRESULT WINAPI Uri_GetProperties(IUri *iface, DWORD *pdwProperties)
4925 Uri *This = impl_from_IUri(iface);
4926 TRACE("(%p %s)->(%p)\n", This, debugstr_w(This->canon_uri), pdwProperties);
4929 return E_INVALIDARG;
4931 /* All URIs have these. */
4932 *pdwProperties = Uri_HAS_DISPLAY_URI|Uri_HAS_RAW_URI|Uri_HAS_SCHEME|Uri_HAS_HOST_TYPE;
4934 if(!(This->display_modifiers & URI_DISPLAY_NO_ABSOLUTE_URI))
4935 *pdwProperties |= Uri_HAS_ABSOLUTE_URI;
4937 if(This->scheme_start > -1)
4938 *pdwProperties |= Uri_HAS_SCHEME_NAME;
4940 if(This->authority_start > -1) {
4941 *pdwProperties |= Uri_HAS_AUTHORITY;
4942 if(This->userinfo_start > -1) {
4943 *pdwProperties |= Uri_HAS_USER_INFO;
4944 if(This->userinfo_split != 0)
4945 *pdwProperties |= Uri_HAS_USER_NAME;
4947 if(This->userinfo_split > -1)
4948 *pdwProperties |= Uri_HAS_PASSWORD;
4949 if(This->host_start > -1)
4950 *pdwProperties |= Uri_HAS_HOST;
4951 if(This->domain_offset > -1)
4952 *pdwProperties |= Uri_HAS_DOMAIN;
4956 *pdwProperties |= Uri_HAS_PORT;
4957 if(This->path_start > -1)
4958 *pdwProperties |= Uri_HAS_PATH|Uri_HAS_PATH_AND_QUERY;
4959 if(This->query_start > -1)
4960 *pdwProperties |= Uri_HAS_QUERY|Uri_HAS_PATH_AND_QUERY;
4962 if(This->extension_offset > -1)
4963 *pdwProperties |= Uri_HAS_EXTENSION;
4965 if(This->fragment_start > -1)
4966 *pdwProperties |= Uri_HAS_FRAGMENT;
4971 static HRESULT WINAPI Uri_IsEqual(IUri *iface, IUri *pUri, BOOL *pfEqual)
4973 Uri *This = impl_from_IUri(iface);
4976 TRACE("(%p %s)->(%p %p)\n", This, debugstr_w(This->canon_uri), pUri, pfEqual);
4984 /* For some reason Windows returns S_OK here... */
4988 /* Try to convert it to a Uri (allows for a more simple comparison). */
4989 if(!(other = get_uri_obj(pUri))) {
4990 FIXME("(%p)->(%p %p) No support for unknown IUri's yet.\n", iface, pUri, pfEqual);
4994 TRACE("comparing to %s\n", debugstr_w(other->canon_uri));
4995 return compare_uris(This, other, pfEqual);
4998 static const IUriVtbl UriVtbl = {
5002 Uri_GetPropertyBSTR,
5003 Uri_GetPropertyLength,
5004 Uri_GetPropertyDWORD,
5015 Uri_GetPathAndQuery,
5029 static inline Uri* impl_from_IUriBuilderFactory(IUriBuilderFactory *iface)
5031 return CONTAINING_RECORD(iface, Uri, IUriBuilderFactory_iface);
5034 static HRESULT WINAPI UriBuilderFactory_QueryInterface(IUriBuilderFactory *iface, REFIID riid, void **ppv)
5036 Uri *This = impl_from_IUriBuilderFactory(iface);
5038 if(IsEqualGUID(&IID_IUnknown, riid)) {
5039 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
5040 *ppv = &This->IUriBuilderFactory_iface;
5041 }else if(IsEqualGUID(&IID_IUriBuilderFactory, riid)) {
5042 TRACE("(%p)->(IID_IUriBuilderFactory %p)\n", This, ppv);
5043 *ppv = &This->IUriBuilderFactory_iface;
5044 }else if(IsEqualGUID(&IID_IUri, riid)) {
5045 TRACE("(%p)->(IID_IUri %p)\n", This, ppv);
5046 *ppv = &This->IUri_iface;
5048 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
5050 return E_NOINTERFACE;
5053 IUnknown_AddRef((IUnknown*)*ppv);
5057 static ULONG WINAPI UriBuilderFactory_AddRef(IUriBuilderFactory *iface)
5059 Uri *This = impl_from_IUriBuilderFactory(iface);
5060 LONG ref = InterlockedIncrement(&This->ref);
5062 TRACE("(%p) ref=%d\n", This, ref);
5067 static ULONG WINAPI UriBuilderFactory_Release(IUriBuilderFactory *iface)
5069 Uri *This = impl_from_IUriBuilderFactory(iface);
5070 LONG ref = InterlockedDecrement(&This->ref);
5072 TRACE("(%p) ref=%d\n", This, ref);
5075 destory_uri_obj(This);
5080 static HRESULT WINAPI UriBuilderFactory_CreateIUriBuilder(IUriBuilderFactory *iface,
5082 DWORD_PTR dwReserved,
5083 IUriBuilder **ppIUriBuilder)
5085 Uri *This = impl_from_IUriBuilderFactory(iface);
5086 TRACE("(%p)->(%08x %08x %p)\n", This, dwFlags, (DWORD)dwReserved, ppIUriBuilder);
5091 if(dwFlags || dwReserved) {
5092 *ppIUriBuilder = NULL;
5093 return E_INVALIDARG;
5096 return CreateIUriBuilder(NULL, 0, 0, ppIUriBuilder);
5099 static HRESULT WINAPI UriBuilderFactory_CreateInitializedIUriBuilder(IUriBuilderFactory *iface,
5101 DWORD_PTR dwReserved,
5102 IUriBuilder **ppIUriBuilder)
5104 Uri *This = impl_from_IUriBuilderFactory(iface);
5105 TRACE("(%p)->(%08x %08x %p)\n", This, dwFlags, (DWORD)dwReserved, ppIUriBuilder);
5110 if(dwFlags || dwReserved) {
5111 *ppIUriBuilder = NULL;
5112 return E_INVALIDARG;
5115 return CreateIUriBuilder(&This->IUri_iface, 0, 0, ppIUriBuilder);
5118 static const IUriBuilderFactoryVtbl UriBuilderFactoryVtbl = {
5119 UriBuilderFactory_QueryInterface,
5120 UriBuilderFactory_AddRef,
5121 UriBuilderFactory_Release,
5122 UriBuilderFactory_CreateIUriBuilder,
5123 UriBuilderFactory_CreateInitializedIUriBuilder
5126 static Uri* create_uri_obj(void) {
5127 Uri *ret = heap_alloc_zero(sizeof(Uri));
5129 ret->IUri_iface.lpVtbl = &UriVtbl;
5130 ret->IUriBuilderFactory_iface.lpVtbl = &UriBuilderFactoryVtbl;
5137 /***********************************************************************
5138 * CreateUri (urlmon.@)
5140 * Creates a new IUri object using the URI represented by pwzURI. This function
5141 * parses and validates the components of pwzURI and then canonicalizes the
5142 * parsed components.
5145 * pwzURI [I] The URI to parse, validate, and canonicalize.
5146 * dwFlags [I] Flags which can affect how the parsing/canonicalization is performed.
5147 * dwReserved [I] Reserved (not used).
5148 * ppURI [O] The resulting IUri after parsing/canonicalization occurs.
5151 * Success: Returns S_OK. ppURI contains the pointer to the newly allocated IUri.
5152 * Failure: E_INVALIDARG if there are invalid flag combinations in dwFlags, or an
5153 * invalid parameter, or pwzURI doesn't represent a valid URI.
5154 * E_OUTOFMEMORY if any memory allocation fails.
5158 * Uri_CREATE_CANONICALIZE, Uri_CREATE_DECODE_EXTRA_INFO, Uri_CREATE_CRACK_UNKNOWN_SCHEMES,
5159 * Uri_CREATE_PRE_PROCESS_HTML_URI, Uri_CREATE_NO_IE_SETTINGS.
5161 HRESULT WINAPI CreateUri(LPCWSTR pwzURI, DWORD dwFlags, DWORD_PTR dwReserved, IUri **ppURI)
5163 const DWORD supported_flags = Uri_CREATE_ALLOW_RELATIVE|Uri_CREATE_ALLOW_IMPLICIT_WILDCARD_SCHEME|
5164 Uri_CREATE_ALLOW_IMPLICIT_FILE_SCHEME|Uri_CREATE_NO_CANONICALIZE|Uri_CREATE_CANONICALIZE|
5165 Uri_CREATE_DECODE_EXTRA_INFO|Uri_CREATE_NO_DECODE_EXTRA_INFO|Uri_CREATE_CRACK_UNKNOWN_SCHEMES|
5166 Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES|Uri_CREATE_PRE_PROCESS_HTML_URI|Uri_CREATE_NO_PRE_PROCESS_HTML_URI|
5167 Uri_CREATE_NO_IE_SETTINGS|Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS|Uri_CREATE_FILE_USE_DOS_PATH;
5172 TRACE("(%s %x %x %p)\n", debugstr_w(pwzURI), dwFlags, (DWORD)dwReserved, ppURI);
5175 return E_INVALIDARG;
5179 return E_INVALIDARG;
5182 /* Check for invalid flags. */
5183 if(has_invalid_flag_combination(dwFlags)) {
5185 return E_INVALIDARG;
5188 /* Currently unsupported. */
5189 if(dwFlags & ~supported_flags)
5190 FIXME("Ignoring unsupported flag(s) %x\n", dwFlags & ~supported_flags);
5192 ret = create_uri_obj();
5195 return E_OUTOFMEMORY;
5198 /* Explicitly set the default flags if it doesn't cause a flag conflict. */
5199 apply_default_flags(&dwFlags);
5201 /* Pre process the URI, unless told otherwise. */
5202 if(!(dwFlags & Uri_CREATE_NO_PRE_PROCESS_HTML_URI))
5203 ret->raw_uri = pre_process_uri(pwzURI);
5205 ret->raw_uri = SysAllocString(pwzURI);
5209 return E_OUTOFMEMORY;
5212 memset(&data, 0, sizeof(parse_data));
5213 data.uri = ret->raw_uri;
5215 /* Validate and parse the URI into its components. */
5216 if(!parse_uri(&data, dwFlags)) {
5217 /* Encountered an unsupported or invalid URI */
5218 IUri_Release(&ret->IUri_iface);
5220 return E_INVALIDARG;
5223 /* Canonicalize the URI. */
5224 hr = canonicalize_uri(&data, ret, dwFlags);
5226 IUri_Release(&ret->IUri_iface);
5231 ret->create_flags = dwFlags;
5233 *ppURI = &ret->IUri_iface;
5237 /***********************************************************************
5238 * CreateUriWithFragment (urlmon.@)
5240 * Creates a new IUri object. This is almost the same as CreateUri, expect that
5241 * it allows you to explicitly specify a fragment (pwzFragment) for pwzURI.
5244 * pwzURI [I] The URI to parse and perform canonicalization on.
5245 * pwzFragment [I] The explicit fragment string which should be added to pwzURI.
5246 * dwFlags [I] The flags which will be passed to CreateUri.
5247 * dwReserved [I] Reserved (not used).
5248 * ppURI [O] The resulting IUri after parsing/canonicalization.
5251 * Success: S_OK. ppURI contains the pointer to the newly allocated IUri.
5252 * Failure: E_INVALIDARG if pwzURI already contains a fragment and pwzFragment
5253 * isn't NULL. Will also return E_INVALIDARG for the same reasons as
5254 * CreateUri will. E_OUTOFMEMORY if any allocation fails.
5256 HRESULT WINAPI CreateUriWithFragment(LPCWSTR pwzURI, LPCWSTR pwzFragment, DWORD dwFlags,
5257 DWORD_PTR dwReserved, IUri **ppURI)
5260 TRACE("(%s %s %x %x %p)\n", debugstr_w(pwzURI), debugstr_w(pwzFragment), dwFlags, (DWORD)dwReserved, ppURI);
5263 return E_INVALIDARG;
5267 return E_INVALIDARG;
5270 /* Check if a fragment should be appended to the URI string. */
5273 DWORD uri_len, frag_len;
5276 /* Check if the original URI already has a fragment component. */
5277 if(StrChrW(pwzURI, '#')) {
5279 return E_INVALIDARG;
5282 uri_len = lstrlenW(pwzURI);
5283 frag_len = lstrlenW(pwzFragment);
5285 /* If the fragment doesn't start with a '#', one will be added. */
5286 add_pound = *pwzFragment != '#';
5289 uriW = heap_alloc((uri_len+frag_len+2)*sizeof(WCHAR));
5291 uriW = heap_alloc((uri_len+frag_len+1)*sizeof(WCHAR));
5294 return E_OUTOFMEMORY;
5296 memcpy(uriW, pwzURI, uri_len*sizeof(WCHAR));
5298 uriW[uri_len++] = '#';
5299 memcpy(uriW+uri_len, pwzFragment, (frag_len+1)*sizeof(WCHAR));
5301 hres = CreateUri(uriW, dwFlags, 0, ppURI);
5305 /* A fragment string wasn't specified, so just forward the call. */
5306 hres = CreateUri(pwzURI, dwFlags, 0, ppURI);
5311 static HRESULT build_uri(const UriBuilder *builder, IUri **uri, DWORD create_flags,
5312 DWORD use_orig_flags, DWORD encoding_mask)
5321 if(encoding_mask && (!builder->uri || builder->modified_props)) {
5326 /* Decide what flags should be used when creating the Uri. */
5327 if((use_orig_flags & UriBuilder_USE_ORIGINAL_FLAGS) && builder->uri)
5328 create_flags = builder->uri->create_flags;
5330 if(has_invalid_flag_combination(create_flags)) {
5332 return E_INVALIDARG;
5335 /* Set the default flags if they don't cause a conflict. */
5336 apply_default_flags(&create_flags);
5339 /* Return the base IUri if no changes have been made and the create_flags match. */
5340 if(builder->uri && !builder->modified_props && builder->uri->create_flags == create_flags) {
5341 *uri = &builder->uri->IUri_iface;
5346 hr = validate_components(builder, &data, create_flags);
5352 ret = create_uri_obj();
5355 return E_OUTOFMEMORY;
5358 hr = generate_uri(builder, &data, ret, create_flags);
5360 IUri_Release(&ret->IUri_iface);
5365 *uri = &ret->IUri_iface;
5369 static inline UriBuilder* impl_from_IUriBuilder(IUriBuilder *iface)
5371 return CONTAINING_RECORD(iface, UriBuilder, IUriBuilder_iface);
5374 static HRESULT WINAPI UriBuilder_QueryInterface(IUriBuilder *iface, REFIID riid, void **ppv)
5376 UriBuilder *This = impl_from_IUriBuilder(iface);
5378 if(IsEqualGUID(&IID_IUnknown, riid)) {
5379 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
5380 *ppv = &This->IUriBuilder_iface;
5381 }else if(IsEqualGUID(&IID_IUriBuilder, riid)) {
5382 TRACE("(%p)->(IID_IUriBuilder %p)\n", This, ppv);
5383 *ppv = &This->IUriBuilder_iface;
5385 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
5387 return E_NOINTERFACE;
5390 IUnknown_AddRef((IUnknown*)*ppv);
5394 static ULONG WINAPI UriBuilder_AddRef(IUriBuilder *iface)
5396 UriBuilder *This = impl_from_IUriBuilder(iface);
5397 LONG ref = InterlockedIncrement(&This->ref);
5399 TRACE("(%p) ref=%d\n", This, ref);
5404 static ULONG WINAPI UriBuilder_Release(IUriBuilder *iface)
5406 UriBuilder *This = impl_from_IUriBuilder(iface);
5407 LONG ref = InterlockedDecrement(&This->ref);
5409 TRACE("(%p) ref=%d\n", This, ref);
5412 if(This->uri) IUri_Release(&This->uri->IUri_iface);
5413 heap_free(This->fragment);
5414 heap_free(This->host);
5415 heap_free(This->password);
5416 heap_free(This->path);
5417 heap_free(This->query);
5418 heap_free(This->scheme);
5419 heap_free(This->username);
5426 static HRESULT WINAPI UriBuilder_CreateUriSimple(IUriBuilder *iface,
5427 DWORD dwAllowEncodingPropertyMask,
5428 DWORD_PTR dwReserved,
5431 UriBuilder *This = impl_from_IUriBuilder(iface);
5433 TRACE("(%p)->(%d %d %p)\n", This, dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
5435 hr = build_uri(This, ppIUri, 0, UriBuilder_USE_ORIGINAL_FLAGS, dwAllowEncodingPropertyMask);
5437 FIXME("(%p)->(%d %d %p)\n", This, dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
5441 static HRESULT WINAPI UriBuilder_CreateUri(IUriBuilder *iface,
5442 DWORD dwCreateFlags,
5443 DWORD dwAllowEncodingPropertyMask,
5444 DWORD_PTR dwReserved,
5447 UriBuilder *This = impl_from_IUriBuilder(iface);
5449 TRACE("(%p)->(0x%08x %d %d %p)\n", This, dwCreateFlags, dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
5451 if(dwCreateFlags == -1)
5452 hr = build_uri(This, ppIUri, 0, UriBuilder_USE_ORIGINAL_FLAGS, dwAllowEncodingPropertyMask);
5454 hr = build_uri(This, ppIUri, dwCreateFlags, 0, dwAllowEncodingPropertyMask);
5457 FIXME("(%p)->(0x%08x %d %d %p)\n", This, dwCreateFlags, dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
5461 static HRESULT WINAPI UriBuilder_CreateUriWithFlags(IUriBuilder *iface,
5462 DWORD dwCreateFlags,
5463 DWORD dwUriBuilderFlags,
5464 DWORD dwAllowEncodingPropertyMask,
5465 DWORD_PTR dwReserved,
5468 UriBuilder *This = impl_from_IUriBuilder(iface);
5470 TRACE("(%p)->(0x%08x 0x%08x %d %d %p)\n", This, dwCreateFlags, dwUriBuilderFlags,
5471 dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
5473 hr = build_uri(This, ppIUri, dwCreateFlags, dwUriBuilderFlags, dwAllowEncodingPropertyMask);
5475 FIXME("(%p)->(0x%08x 0x%08x %d %d %p)\n", This, dwCreateFlags, dwUriBuilderFlags,
5476 dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
5480 static HRESULT WINAPI UriBuilder_GetIUri(IUriBuilder *iface, IUri **ppIUri)
5482 UriBuilder *This = impl_from_IUriBuilder(iface);
5483 TRACE("(%p)->(%p)\n", This, ppIUri);
5489 IUri *uri = &This->uri->IUri_iface;
5498 static HRESULT WINAPI UriBuilder_SetIUri(IUriBuilder *iface, IUri *pIUri)
5500 UriBuilder *This = impl_from_IUriBuilder(iface);
5501 TRACE("(%p)->(%p)\n", This, pIUri);
5506 if((uri = get_uri_obj(pIUri))) {
5507 /* Only reset the builder if it's Uri isn't the same as
5508 * the Uri passed to the function.
5510 if(This->uri != uri) {
5511 reset_builder(This);
5515 This->port = uri->port;
5520 FIXME("(%p)->(%p) Unknown IUri types not supported yet.\n", This, pIUri);
5523 } else if(This->uri)
5524 /* Only reset the builder if it's Uri isn't NULL. */
5525 reset_builder(This);
5530 static HRESULT WINAPI UriBuilder_GetFragment(IUriBuilder *iface, DWORD *pcchFragment, LPCWSTR *ppwzFragment)
5532 UriBuilder *This = impl_from_IUriBuilder(iface);
5533 TRACE("(%p)->(%p %p)\n", This, pcchFragment, ppwzFragment);
5535 if(!This->uri || This->uri->fragment_start == -1 || This->modified_props & Uri_HAS_FRAGMENT)
5536 return get_builder_component(&This->fragment, &This->fragment_len, NULL, 0, ppwzFragment, pcchFragment);
5538 return get_builder_component(&This->fragment, &This->fragment_len, This->uri->canon_uri+This->uri->fragment_start,
5539 This->uri->fragment_len, ppwzFragment, pcchFragment);
5542 static HRESULT WINAPI UriBuilder_GetHost(IUriBuilder *iface, DWORD *pcchHost, LPCWSTR *ppwzHost)
5544 UriBuilder *This = impl_from_IUriBuilder(iface);
5545 TRACE("(%p)->(%p %p)\n", This, pcchHost, ppwzHost);
5547 if(!This->uri || This->uri->host_start == -1 || This->modified_props & Uri_HAS_HOST)
5548 return get_builder_component(&This->host, &This->host_len, NULL, 0, ppwzHost, pcchHost);
5550 if(This->uri->host_type == Uri_HOST_IPV6)
5551 /* Don't include the '[' and ']' around the address. */
5552 return get_builder_component(&This->host, &This->host_len, This->uri->canon_uri+This->uri->host_start+1,
5553 This->uri->host_len-2, ppwzHost, pcchHost);
5555 return get_builder_component(&This->host, &This->host_len, This->uri->canon_uri+This->uri->host_start,
5556 This->uri->host_len, ppwzHost, pcchHost);
5560 static HRESULT WINAPI UriBuilder_GetPassword(IUriBuilder *iface, DWORD *pcchPassword, LPCWSTR *ppwzPassword)
5562 UriBuilder *This = impl_from_IUriBuilder(iface);
5563 TRACE("(%p)->(%p %p)\n", This, pcchPassword, ppwzPassword);
5565 if(!This->uri || This->uri->userinfo_split == -1 || This->modified_props & Uri_HAS_PASSWORD)
5566 return get_builder_component(&This->password, &This->password_len, NULL, 0, ppwzPassword, pcchPassword);
5568 const WCHAR *start = This->uri->canon_uri+This->uri->userinfo_start+This->uri->userinfo_split+1;
5569 DWORD len = This->uri->userinfo_len-This->uri->userinfo_split-1;
5570 return get_builder_component(&This->password, &This->password_len, start, len, ppwzPassword, pcchPassword);
5574 static HRESULT WINAPI UriBuilder_GetPath(IUriBuilder *iface, DWORD *pcchPath, LPCWSTR *ppwzPath)
5576 UriBuilder *This = impl_from_IUriBuilder(iface);
5577 TRACE("(%p)->(%p %p)\n", This, pcchPath, ppwzPath);
5579 if(!This->uri || This->uri->path_start == -1 || This->modified_props & Uri_HAS_PATH)
5580 return get_builder_component(&This->path, &This->path_len, NULL, 0, ppwzPath, pcchPath);
5582 return get_builder_component(&This->path, &This->path_len, This->uri->canon_uri+This->uri->path_start,
5583 This->uri->path_len, ppwzPath, pcchPath);
5586 static HRESULT WINAPI UriBuilder_GetPort(IUriBuilder *iface, BOOL *pfHasPort, DWORD *pdwPort)
5588 UriBuilder *This = impl_from_IUriBuilder(iface);
5589 TRACE("(%p)->(%p %p)\n", This, pfHasPort, pdwPort);
5602 *pfHasPort = This->has_port;
5603 *pdwPort = This->port;
5607 static HRESULT WINAPI UriBuilder_GetQuery(IUriBuilder *iface, DWORD *pcchQuery, LPCWSTR *ppwzQuery)
5609 UriBuilder *This = impl_from_IUriBuilder(iface);
5610 TRACE("(%p)->(%p %p)\n", This, pcchQuery, ppwzQuery);
5612 if(!This->uri || This->uri->query_start == -1 || This->modified_props & Uri_HAS_QUERY)
5613 return get_builder_component(&This->query, &This->query_len, NULL, 0, ppwzQuery, pcchQuery);
5615 return get_builder_component(&This->query, &This->query_len, This->uri->canon_uri+This->uri->query_start,
5616 This->uri->query_len, ppwzQuery, pcchQuery);
5619 static HRESULT WINAPI UriBuilder_GetSchemeName(IUriBuilder *iface, DWORD *pcchSchemeName, LPCWSTR *ppwzSchemeName)
5621 UriBuilder *This = impl_from_IUriBuilder(iface);
5622 TRACE("(%p)->(%p %p)\n", This, pcchSchemeName, ppwzSchemeName);
5624 if(!This->uri || This->uri->scheme_start == -1 || This->modified_props & Uri_HAS_SCHEME_NAME)
5625 return get_builder_component(&This->scheme, &This->scheme_len, NULL, 0, ppwzSchemeName, pcchSchemeName);
5627 return get_builder_component(&This->scheme, &This->scheme_len, This->uri->canon_uri+This->uri->scheme_start,
5628 This->uri->scheme_len, ppwzSchemeName, pcchSchemeName);
5631 static HRESULT WINAPI UriBuilder_GetUserName(IUriBuilder *iface, DWORD *pcchUserName, LPCWSTR *ppwzUserName)
5633 UriBuilder *This = impl_from_IUriBuilder(iface);
5634 TRACE("(%p)->(%p %p)\n", This, pcchUserName, ppwzUserName);
5636 if(!This->uri || This->uri->userinfo_start == -1 || This->uri->userinfo_split == 0 ||
5637 This->modified_props & Uri_HAS_USER_NAME)
5638 return get_builder_component(&This->username, &This->username_len, NULL, 0, ppwzUserName, pcchUserName);
5640 const WCHAR *start = This->uri->canon_uri+This->uri->userinfo_start;
5642 /* Check if there's a password in the userinfo section. */
5643 if(This->uri->userinfo_split > -1)
5644 /* Don't include the password. */
5645 return get_builder_component(&This->username, &This->username_len, start,
5646 This->uri->userinfo_split, ppwzUserName, pcchUserName);
5648 return get_builder_component(&This->username, &This->username_len, start,
5649 This->uri->userinfo_len, ppwzUserName, pcchUserName);
5653 static HRESULT WINAPI UriBuilder_SetFragment(IUriBuilder *iface, LPCWSTR pwzNewValue)
5655 UriBuilder *This = impl_from_IUriBuilder(iface);
5656 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
5657 return set_builder_component(&This->fragment, &This->fragment_len, pwzNewValue, '#',
5658 &This->modified_props, Uri_HAS_FRAGMENT);
5661 static HRESULT WINAPI UriBuilder_SetHost(IUriBuilder *iface, LPCWSTR pwzNewValue)
5663 UriBuilder *This = impl_from_IUriBuilder(iface);
5664 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
5666 /* Host name can't be set to NULL. */
5668 return E_INVALIDARG;
5670 return set_builder_component(&This->host, &This->host_len, pwzNewValue, 0,
5671 &This->modified_props, Uri_HAS_HOST);
5674 static HRESULT WINAPI UriBuilder_SetPassword(IUriBuilder *iface, LPCWSTR pwzNewValue)
5676 UriBuilder *This = impl_from_IUriBuilder(iface);
5677 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
5678 return set_builder_component(&This->password, &This->password_len, pwzNewValue, 0,
5679 &This->modified_props, Uri_HAS_PASSWORD);
5682 static HRESULT WINAPI UriBuilder_SetPath(IUriBuilder *iface, LPCWSTR pwzNewValue)
5684 UriBuilder *This = impl_from_IUriBuilder(iface);
5685 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
5686 return set_builder_component(&This->path, &This->path_len, pwzNewValue, 0,
5687 &This->modified_props, Uri_HAS_PATH);
5690 static HRESULT WINAPI UriBuilder_SetPort(IUriBuilder *iface, BOOL fHasPort, DWORD dwNewValue)
5692 UriBuilder *This = impl_from_IUriBuilder(iface);
5693 TRACE("(%p)->(%d %d)\n", This, fHasPort, dwNewValue);
5695 This->has_port = fHasPort;
5696 This->port = dwNewValue;
5697 This->modified_props |= Uri_HAS_PORT;
5701 static HRESULT WINAPI UriBuilder_SetQuery(IUriBuilder *iface, LPCWSTR pwzNewValue)
5703 UriBuilder *This = impl_from_IUriBuilder(iface);
5704 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
5705 return set_builder_component(&This->query, &This->query_len, pwzNewValue, '?',
5706 &This->modified_props, Uri_HAS_QUERY);
5709 static HRESULT WINAPI UriBuilder_SetSchemeName(IUriBuilder *iface, LPCWSTR pwzNewValue)
5711 UriBuilder *This = impl_from_IUriBuilder(iface);
5712 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
5714 /* Only set the scheme name if it's not NULL or empty. */
5715 if(!pwzNewValue || !*pwzNewValue)
5716 return E_INVALIDARG;
5718 return set_builder_component(&This->scheme, &This->scheme_len, pwzNewValue, 0,
5719 &This->modified_props, Uri_HAS_SCHEME_NAME);
5722 static HRESULT WINAPI UriBuilder_SetUserName(IUriBuilder *iface, LPCWSTR pwzNewValue)
5724 UriBuilder *This = impl_from_IUriBuilder(iface);
5725 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
5726 return set_builder_component(&This->username, &This->username_len, pwzNewValue, 0,
5727 &This->modified_props, Uri_HAS_USER_NAME);
5730 static HRESULT WINAPI UriBuilder_RemoveProperties(IUriBuilder *iface, DWORD dwPropertyMask)
5732 const DWORD accepted_flags = Uri_HAS_AUTHORITY|Uri_HAS_DOMAIN|Uri_HAS_EXTENSION|Uri_HAS_FRAGMENT|Uri_HAS_HOST|
5733 Uri_HAS_PASSWORD|Uri_HAS_PATH|Uri_HAS_PATH_AND_QUERY|Uri_HAS_QUERY|
5734 Uri_HAS_USER_INFO|Uri_HAS_USER_NAME;
5736 UriBuilder *This = impl_from_IUriBuilder(iface);
5737 TRACE("(%p)->(0x%08x)\n", This, dwPropertyMask);
5739 if(dwPropertyMask & ~accepted_flags)
5740 return E_INVALIDARG;
5742 if(dwPropertyMask & Uri_HAS_FRAGMENT)
5743 UriBuilder_SetFragment(iface, NULL);
5745 /* Even though you can't set the host name to NULL or an
5746 * empty string, you can still remove it... for some reason.
5748 if(dwPropertyMask & Uri_HAS_HOST)
5749 set_builder_component(&This->host, &This->host_len, NULL, 0,
5750 &This->modified_props, Uri_HAS_HOST);
5752 if(dwPropertyMask & Uri_HAS_PASSWORD)
5753 UriBuilder_SetPassword(iface, NULL);
5755 if(dwPropertyMask & Uri_HAS_PATH)
5756 UriBuilder_SetPath(iface, NULL);
5758 if(dwPropertyMask & Uri_HAS_PORT)
5759 UriBuilder_SetPort(iface, FALSE, 0);
5761 if(dwPropertyMask & Uri_HAS_QUERY)
5762 UriBuilder_SetQuery(iface, NULL);
5764 if(dwPropertyMask & Uri_HAS_USER_NAME)
5765 UriBuilder_SetUserName(iface, NULL);
5770 static HRESULT WINAPI UriBuilder_HasBeenModified(IUriBuilder *iface, BOOL *pfModified)
5772 UriBuilder *This = impl_from_IUriBuilder(iface);
5773 TRACE("(%p)->(%p)\n", This, pfModified);
5778 *pfModified = This->modified_props > 0;
5782 static const IUriBuilderVtbl UriBuilderVtbl = {
5783 UriBuilder_QueryInterface,
5786 UriBuilder_CreateUriSimple,
5787 UriBuilder_CreateUri,
5788 UriBuilder_CreateUriWithFlags,
5791 UriBuilder_GetFragment,
5793 UriBuilder_GetPassword,
5796 UriBuilder_GetQuery,
5797 UriBuilder_GetSchemeName,
5798 UriBuilder_GetUserName,
5799 UriBuilder_SetFragment,
5801 UriBuilder_SetPassword,
5804 UriBuilder_SetQuery,
5805 UriBuilder_SetSchemeName,
5806 UriBuilder_SetUserName,
5807 UriBuilder_RemoveProperties,
5808 UriBuilder_HasBeenModified,
5811 /***********************************************************************
5812 * CreateIUriBuilder (urlmon.@)
5814 HRESULT WINAPI CreateIUriBuilder(IUri *pIUri, DWORD dwFlags, DWORD_PTR dwReserved, IUriBuilder **ppIUriBuilder)
5818 TRACE("(%p %x %x %p)\n", pIUri, dwFlags, (DWORD)dwReserved, ppIUriBuilder);
5823 ret = heap_alloc_zero(sizeof(UriBuilder));
5825 return E_OUTOFMEMORY;
5827 ret->IUriBuilder_iface.lpVtbl = &UriBuilderVtbl;
5833 if((uri = get_uri_obj(pIUri))) {
5838 /* Windows doesn't set 'has_port' to TRUE in this case. */
5839 ret->port = uri->port;
5843 *ppIUriBuilder = NULL;
5844 FIXME("(%p %x %x %p): Unknown IUri types not supported yet.\n", pIUri, dwFlags,
5845 (DWORD)dwReserved, ppIUriBuilder);
5850 *ppIUriBuilder = &ret->IUriBuilder_iface;
5854 /* Merges the base path with the relative path and stores the resulting path
5855 * and path len in 'result' and 'result_len'.
5857 static HRESULT merge_paths(parse_data *data, const WCHAR *base, DWORD base_len, const WCHAR *relative,
5858 DWORD relative_len, WCHAR **result, DWORD *result_len, DWORD flags)
5860 const WCHAR *end = NULL;
5861 DWORD base_copy_len = 0;
5865 /* Find the characters that will be copied over from
5868 end = memrchrW(base, '/', base_len);
5869 if(!end && data->scheme_type == URL_SCHEME_FILE)
5870 /* Try looking for a '\\'. */
5871 end = memrchrW(base, '\\', base_len);
5875 base_copy_len = (end+1)-base;
5876 *result = heap_alloc((base_copy_len+relative_len+1)*sizeof(WCHAR));
5878 *result = heap_alloc((relative_len+1)*sizeof(WCHAR));
5882 return E_OUTOFMEMORY;
5887 memcpy(ptr, base, base_copy_len*sizeof(WCHAR));
5888 ptr += base_copy_len;
5891 memcpy(ptr, relative, relative_len*sizeof(WCHAR));
5892 ptr += relative_len;
5895 *result_len = (ptr-*result);
5899 static HRESULT combine_uri(Uri *base, Uri *relative, DWORD flags, IUri **result, DWORD extras) {
5903 DWORD create_flags = 0, len = 0;
5905 memset(&data, 0, sizeof(parse_data));
5907 /* Base case is when the relative Uri has a scheme name,
5908 * if it does, then 'result' will contain the same data
5909 * as the relative Uri.
5911 if(relative->scheme_start > -1) {
5912 data.uri = SysAllocString(relative->raw_uri);
5915 return E_OUTOFMEMORY;
5918 parse_uri(&data, 0);
5920 ret = create_uri_obj();
5923 return E_OUTOFMEMORY;
5926 if(extras & COMBINE_URI_FORCE_FLAG_USE) {
5927 if(flags & URL_DONT_SIMPLIFY)
5928 create_flags |= Uri_CREATE_NO_CANONICALIZE;
5929 if(flags & URL_DONT_UNESCAPE_EXTRA_INFO)
5930 create_flags |= Uri_CREATE_NO_DECODE_EXTRA_INFO;
5933 ret->raw_uri = data.uri;
5934 hr = canonicalize_uri(&data, ret, create_flags);
5936 IUri_Release(&ret->IUri_iface);
5941 apply_default_flags(&create_flags);
5942 ret->create_flags = create_flags;
5944 *result = &ret->IUri_iface;
5947 DWORD raw_flags = 0;
5949 if(base->scheme_start > -1) {
5950 data.scheme = base->canon_uri+base->scheme_start;
5951 data.scheme_len = base->scheme_len;
5952 data.scheme_type = base->scheme_type;
5954 data.is_relative = TRUE;
5955 data.scheme_type = URL_SCHEME_UNKNOWN;
5956 create_flags |= Uri_CREATE_ALLOW_RELATIVE;
5959 if(base->authority_start > -1) {
5960 if(base->userinfo_start > -1 && base->userinfo_split != 0) {
5961 data.username = base->canon_uri+base->userinfo_start;
5962 data.username_len = (base->userinfo_split > -1) ? base->userinfo_split : base->userinfo_len;
5965 if(base->userinfo_split > -1) {
5966 data.password = base->canon_uri+base->userinfo_start+base->userinfo_split+1;
5967 data.password_len = base->userinfo_len-base->userinfo_split-1;
5970 if(base->host_start > -1) {
5971 data.host = base->canon_uri+base->host_start;
5972 data.host_len = base->host_len;
5973 data.host_type = base->host_type;
5976 if(base->has_port) {
5977 data.has_port = TRUE;
5978 data.port_value = base->port;
5980 } else if(base->scheme_type != URL_SCHEME_FILE)
5981 data.is_opaque = TRUE;
5983 if(relative->path_start == -1 || !relative->path_len) {
5984 if(base->path_start > -1) {
5985 data.path = base->canon_uri+base->path_start;
5986 data.path_len = base->path_len;
5987 } else if((base->path_start == -1 || !base->path_len) && !data.is_opaque) {
5988 /* Just set the path as a '/' if the base didn't have
5989 * one and if it's an hierarchical URI.
5991 static const WCHAR slashW[] = {'/',0};
5996 if(relative->query_start > -1) {
5997 data.query = relative->canon_uri+relative->query_start;
5998 data.query_len = relative->query_len;
5999 } else if(base->query_start > -1) {
6000 data.query = base->canon_uri+base->query_start;
6001 data.query_len = base->query_len;
6004 const WCHAR *ptr, **pptr;
6005 DWORD path_offset = 0, path_len = 0;
6007 /* There's two possibilities on what will happen to the path component
6008 * of the result IUri. First, if the relative path begins with a '/'
6009 * then the resulting path will just be the relative path. Second, if
6010 * relative path doesn't begin with a '/' then the base path and relative
6011 * path are merged together.
6013 if(relative->path_len && *(relative->canon_uri+relative->path_start) == '/') {
6015 BOOL copy_drive_path = FALSE;
6017 /* If the relative IUri's path starts with a '/', then we
6018 * don't use the base IUri's path. Unless the base IUri
6019 * is a file URI, in which case it uses the drive path of
6020 * the base IUri (if it has any) in the new path.
6022 if(base->scheme_type == URL_SCHEME_FILE) {
6023 if(base->path_len > 3 && *(base->canon_uri+base->path_start) == '/' &&
6024 is_drive_path(base->canon_uri+base->path_start+1)) {
6026 copy_drive_path = TRUE;
6030 path_len += relative->path_len;
6032 path = heap_alloc((path_len+1)*sizeof(WCHAR));
6035 return E_OUTOFMEMORY;
6040 /* Copy the base paths, drive path over. */
6041 if(copy_drive_path) {
6042 memcpy(tmp, base->canon_uri+base->path_start, 3*sizeof(WCHAR));
6046 memcpy(tmp, relative->canon_uri+relative->path_start, relative->path_len*sizeof(WCHAR));
6047 path[path_len] = '\0';
6049 /* Merge the base path with the relative path. */
6050 hr = merge_paths(&data, base->canon_uri+base->path_start, base->path_len,
6051 relative->canon_uri+relative->path_start, relative->path_len,
6052 &path, &path_len, flags);
6058 /* If the resulting IUri is a file URI, the drive path isn't
6059 * reduced out when the dot segments are removed.
6061 if(path_len >= 3 && data.scheme_type == URL_SCHEME_FILE && !data.host) {
6062 if(*path == '/' && is_drive_path(path+1))
6064 else if(is_drive_path(path))
6069 /* Check if the dot segments need to be removed from the path. */
6070 if(!(flags & URL_DONT_SIMPLIFY) && !data.is_opaque) {
6071 DWORD offset = (path_offset > 0) ? path_offset+1 : 0;
6072 DWORD new_len = remove_dot_segments(path+offset,path_len-offset);
6074 if(new_len != path_len) {
6075 WCHAR *tmp = heap_realloc(path, (offset+new_len+1)*sizeof(WCHAR));
6079 return E_OUTOFMEMORY;
6082 tmp[new_len+offset] = '\0';
6084 path_len = new_len+offset;
6088 if(relative->query_start > -1) {
6089 data.query = relative->canon_uri+relative->query_start;
6090 data.query_len = relative->query_len;
6093 /* Make sure the path component is valid. */
6096 if((data.is_opaque && !parse_path_opaque(pptr, &data, 0)) ||
6097 (!data.is_opaque && !parse_path_hierarchical(pptr, &data, 0))) {
6100 return E_INVALIDARG;
6104 if(relative->fragment_start > -1) {
6105 data.fragment = relative->canon_uri+relative->fragment_start;
6106 data.fragment_len = relative->fragment_len;
6109 if(flags & URL_DONT_SIMPLIFY)
6110 raw_flags |= RAW_URI_FORCE_PORT_DISP;
6111 if(flags & URL_FILE_USE_PATHURL)
6112 raw_flags |= RAW_URI_CONVERT_TO_DOS_PATH;
6114 len = generate_raw_uri(&data, data.uri, raw_flags);
6115 data.uri = SysAllocStringLen(NULL, len);
6119 return E_OUTOFMEMORY;
6122 generate_raw_uri(&data, data.uri, raw_flags);
6124 ret = create_uri_obj();
6126 SysFreeString(data.uri);
6129 return E_OUTOFMEMORY;
6132 if(flags & URL_DONT_SIMPLIFY)
6133 create_flags |= Uri_CREATE_NO_CANONICALIZE;
6134 if(flags & URL_FILE_USE_PATHURL)
6135 create_flags |= Uri_CREATE_FILE_USE_DOS_PATH;
6137 ret->raw_uri = data.uri;
6138 hr = canonicalize_uri(&data, ret, create_flags);
6140 IUri_Release(&ret->IUri_iface);
6145 if(flags & URL_DONT_SIMPLIFY)
6146 ret->display_modifiers |= URI_DISPLAY_NO_DEFAULT_PORT_AUTH;
6148 apply_default_flags(&create_flags);
6149 ret->create_flags = create_flags;
6150 *result = &ret->IUri_iface;
6158 /***********************************************************************
6159 * CoInternetCombineIUri (urlmon.@)
6161 HRESULT WINAPI CoInternetCombineIUri(IUri *pBaseUri, IUri *pRelativeUri, DWORD dwCombineFlags,
6162 IUri **ppCombinedUri, DWORD_PTR dwReserved)
6165 IInternetProtocolInfo *info;
6166 Uri *relative, *base;
6167 TRACE("(%p %p %x %p %x)\n", pBaseUri, pRelativeUri, dwCombineFlags, ppCombinedUri, (DWORD)dwReserved);
6170 return E_INVALIDARG;
6172 if(!pBaseUri || !pRelativeUri) {
6173 *ppCombinedUri = NULL;
6174 return E_INVALIDARG;
6177 relative = get_uri_obj(pRelativeUri);
6178 base = get_uri_obj(pBaseUri);
6179 if(!relative || !base) {
6180 *ppCombinedUri = NULL;
6181 FIXME("(%p %p %x %p %x) Unknown IUri types not supported yet.\n",
6182 pBaseUri, pRelativeUri, dwCombineFlags, ppCombinedUri, (DWORD)dwReserved);
6186 info = get_protocol_info(base->canon_uri);
6188 WCHAR result[INTERNET_MAX_URL_LENGTH+1];
6189 DWORD result_len = 0;
6191 hr = IInternetProtocolInfo_CombineUrl(info, base->canon_uri, relative->canon_uri, dwCombineFlags,
6192 result, INTERNET_MAX_URL_LENGTH+1, &result_len, 0);
6193 IInternetProtocolInfo_Release(info);
6195 hr = CreateUri(result, Uri_CREATE_ALLOW_RELATIVE, 0, ppCombinedUri);
6201 return combine_uri(base, relative, dwCombineFlags, ppCombinedUri, 0);
6204 /***********************************************************************
6205 * CoInternetCombineUrlEx (urlmon.@)
6207 HRESULT WINAPI CoInternetCombineUrlEx(IUri *pBaseUri, LPCWSTR pwzRelativeUrl, DWORD dwCombineFlags,
6208 IUri **ppCombinedUri, DWORD_PTR dwReserved)
6213 IInternetProtocolInfo *info;
6215 TRACE("(%p %s %x %p %x) stub\n", pBaseUri, debugstr_w(pwzRelativeUrl), dwCombineFlags,
6216 ppCombinedUri, (DWORD)dwReserved);
6221 if(!pwzRelativeUrl) {
6222 *ppCombinedUri = NULL;
6223 return E_UNEXPECTED;
6227 *ppCombinedUri = NULL;
6228 return E_INVALIDARG;
6231 base = get_uri_obj(pBaseUri);
6233 *ppCombinedUri = NULL;
6234 FIXME("(%p %s %x %p %x) Unknown IUri's not supported yet.\n", pBaseUri, debugstr_w(pwzRelativeUrl),
6235 dwCombineFlags, ppCombinedUri, (DWORD)dwReserved);
6239 info = get_protocol_info(base->canon_uri);
6241 WCHAR result[INTERNET_MAX_URL_LENGTH+1];
6242 DWORD result_len = 0;
6244 hr = IInternetProtocolInfo_CombineUrl(info, base->canon_uri, pwzRelativeUrl, dwCombineFlags,
6245 result, INTERNET_MAX_URL_LENGTH+1, &result_len, 0);
6246 IInternetProtocolInfo_Release(info);
6248 hr = CreateUri(result, Uri_CREATE_ALLOW_RELATIVE, 0, ppCombinedUri);
6254 hr = CreateUri(pwzRelativeUrl, Uri_CREATE_ALLOW_RELATIVE, 0, &relative);
6256 *ppCombinedUri = NULL;
6260 hr = combine_uri(base, get_uri_obj(relative), dwCombineFlags, ppCombinedUri, COMBINE_URI_FORCE_FLAG_USE);
6262 IUri_Release(relative);
6266 static HRESULT parse_canonicalize(const Uri *uri, DWORD flags, LPWSTR output,
6267 DWORD output_len, DWORD *result_len)
6269 const WCHAR *ptr = NULL;
6272 WCHAR buffer[INTERNET_MAX_URL_LENGTH+1];
6276 /* URL_UNESCAPE only has effect if none of the URL_ESCAPE flags are set. */
6277 const BOOL allow_unescape = !(flags & URL_ESCAPE_UNSAFE) &&
6278 !(flags & URL_ESCAPE_SPACES_ONLY) &&
6279 !(flags & URL_ESCAPE_PERCENT);
6282 /* Check if the dot segments need to be removed from the
6285 if(uri->scheme_start > -1 && uri->path_start > -1) {
6286 ptr = uri->canon_uri+uri->scheme_start+uri->scheme_len+1;
6289 reduce_path = !(flags & URL_NO_META) &&
6290 !(flags & URL_DONT_SIMPLIFY) &&
6291 ptr && check_hierarchical(pptr);
6293 for(ptr = uri->canon_uri; ptr < uri->canon_uri+uri->canon_len; ++ptr) {
6294 BOOL do_default_action = TRUE;
6296 /* Keep track of the path if we need to remove dot segments from
6299 if(reduce_path && !path && ptr == uri->canon_uri+uri->path_start)
6302 /* Check if it's time to reduce the path. */
6303 if(reduce_path && ptr == uri->canon_uri+uri->path_start+uri->path_len) {
6304 DWORD current_path_len = (buffer+len) - path;
6305 DWORD new_path_len = remove_dot_segments(path, current_path_len);
6307 /* Update the current length. */
6308 len -= (current_path_len-new_path_len);
6309 reduce_path = FALSE;
6313 const WCHAR decoded = decode_pct_val(ptr);
6315 if(allow_unescape && (flags & URL_UNESCAPE)) {
6316 buffer[len++] = decoded;
6318 do_default_action = FALSE;
6322 /* See if %'s needed to encoded. */
6323 if(do_default_action && (flags & URL_ESCAPE_PERCENT)) {
6324 pct_encode_val(*ptr, buffer+len);
6326 do_default_action = FALSE;
6328 } else if(*ptr == ' ') {
6329 if((flags & URL_ESCAPE_SPACES_ONLY) &&
6330 !(flags & URL_ESCAPE_UNSAFE)) {
6331 pct_encode_val(*ptr, buffer+len);
6333 do_default_action = FALSE;
6335 } else if(!is_reserved(*ptr) && !is_unreserved(*ptr)) {
6336 if(flags & URL_ESCAPE_UNSAFE) {
6337 pct_encode_val(*ptr, buffer+len);
6339 do_default_action = FALSE;
6343 if(do_default_action)
6344 buffer[len++] = *ptr;
6347 /* Sometimes the path is the very last component of the IUri, so
6348 * see if the dot segments need to be reduced now.
6350 if(reduce_path && path) {
6351 DWORD current_path_len = (buffer+len) - path;
6352 DWORD new_path_len = remove_dot_segments(path, current_path_len);
6354 /* Update the current length. */
6355 len -= (current_path_len-new_path_len);
6360 /* The null terminator isn't included in the length. */
6361 *result_len = len-1;
6362 if(len > output_len)
6363 return STRSAFE_E_INSUFFICIENT_BUFFER;
6365 memcpy(output, buffer, len*sizeof(WCHAR));
6370 static HRESULT parse_friendly(IUri *uri, LPWSTR output, DWORD output_len,
6377 hr = IUri_GetPropertyLength(uri, Uri_PROPERTY_DISPLAY_URI, &display_len, 0);
6383 *result_len = display_len;
6384 if(display_len+1 > output_len)
6385 return STRSAFE_E_INSUFFICIENT_BUFFER;
6387 hr = IUri_GetDisplayUri(uri, &display);
6393 memcpy(output, display, (display_len+1)*sizeof(WCHAR));
6394 SysFreeString(display);
6398 static HRESULT parse_rootdocument(const Uri *uri, LPWSTR output, DWORD output_len,
6401 static const WCHAR colon_slashesW[] = {':','/','/'};
6406 /* Windows only returns the root document if the URI has an authority
6407 * and it's not an unknown scheme type or a file scheme type.
6409 if(uri->authority_start == -1 ||
6410 uri->scheme_type == URL_SCHEME_UNKNOWN ||
6411 uri->scheme_type == URL_SCHEME_FILE) {
6414 return STRSAFE_E_INSUFFICIENT_BUFFER;
6420 len = uri->scheme_len+uri->authority_len;
6421 /* For the "://" and '/' which will be added. */
6424 if(len+1 > output_len) {
6426 return STRSAFE_E_INSUFFICIENT_BUFFER;
6430 memcpy(ptr, uri->canon_uri+uri->scheme_start, uri->scheme_len*sizeof(WCHAR));
6432 /* Add the "://". */
6433 ptr += uri->scheme_len;
6434 memcpy(ptr, colon_slashesW, sizeof(colon_slashesW));
6436 /* Add the authority. */
6437 ptr += sizeof(colon_slashesW)/sizeof(WCHAR);
6438 memcpy(ptr, uri->canon_uri+uri->authority_start, uri->authority_len*sizeof(WCHAR));
6440 /* Add the '/' after the authority. */
6441 ptr += uri->authority_len;
6449 static HRESULT parse_document(const Uri *uri, LPWSTR output, DWORD output_len,
6454 /* It has to be a known scheme type, but, it can't be a file
6455 * scheme. It also has to hierarchical.
6457 if(uri->scheme_type == URL_SCHEME_UNKNOWN ||
6458 uri->scheme_type == URL_SCHEME_FILE ||
6459 uri->authority_start == -1) {
6462 return STRSAFE_E_INSUFFICIENT_BUFFER;
6468 if(uri->fragment_start > -1)
6469 len = uri->fragment_start;
6471 len = uri->canon_len;
6474 if(len+1 > output_len)
6475 return STRSAFE_E_INSUFFICIENT_BUFFER;
6477 memcpy(output, uri->canon_uri, len*sizeof(WCHAR));
6482 static HRESULT parse_path_from_url(const Uri *uri, LPWSTR output, DWORD output_len,
6485 const WCHAR *path_ptr;
6486 WCHAR buffer[INTERNET_MAX_URL_LENGTH+1];
6489 if(uri->scheme_type != URL_SCHEME_FILE) {
6493 return E_INVALIDARG;
6497 if(uri->host_start > -1) {
6498 static const WCHAR slash_slashW[] = {'\\','\\'};
6500 memcpy(ptr, slash_slashW, sizeof(slash_slashW));
6501 ptr += sizeof(slash_slashW)/sizeof(WCHAR);
6502 memcpy(ptr, uri->canon_uri+uri->host_start, uri->host_len*sizeof(WCHAR));
6503 ptr += uri->host_len;
6506 path_ptr = uri->canon_uri+uri->path_start;
6507 if(uri->path_len > 3 && *path_ptr == '/' && is_drive_path(path_ptr+1))
6508 /* Skip past the '/' in front of the drive path. */
6511 for(; path_ptr < uri->canon_uri+uri->path_start+uri->path_len; ++path_ptr, ++ptr) {
6512 BOOL do_default_action = TRUE;
6514 if(*path_ptr == '%') {
6515 const WCHAR decoded = decode_pct_val(path_ptr);
6519 do_default_action = FALSE;
6521 } else if(*path_ptr == '/') {
6523 do_default_action = FALSE;
6526 if(do_default_action)
6532 *result_len = ptr-buffer;
6533 if(*result_len+1 > output_len)
6534 return STRSAFE_E_INSUFFICIENT_BUFFER;
6536 memcpy(output, buffer, (*result_len+1)*sizeof(WCHAR));
6540 static HRESULT parse_url_from_path(IUri *uri, LPWSTR output, DWORD output_len,
6547 hr = IUri_GetPropertyLength(uri, Uri_PROPERTY_ABSOLUTE_URI, &len, 0);
6554 if(len+1 > output_len)
6555 return STRSAFE_E_INSUFFICIENT_BUFFER;
6557 hr = IUri_GetAbsoluteUri(uri, &received);
6563 memcpy(output, received, (len+1)*sizeof(WCHAR));
6564 SysFreeString(received);
6569 static HRESULT parse_schema(IUri *uri, LPWSTR output, DWORD output_len,
6576 hr = IUri_GetPropertyLength(uri, Uri_PROPERTY_SCHEME_NAME, &len, 0);
6583 if(len+1 > output_len)
6584 return STRSAFE_E_INSUFFICIENT_BUFFER;
6586 hr = IUri_GetSchemeName(uri, &received);
6592 memcpy(output, received, (len+1)*sizeof(WCHAR));
6593 SysFreeString(received);
6598 static HRESULT parse_site(IUri *uri, LPWSTR output, DWORD output_len, DWORD *result_len)
6604 hr = IUri_GetPropertyLength(uri, Uri_PROPERTY_HOST, &len, 0);
6611 if(len+1 > output_len)
6612 return STRSAFE_E_INSUFFICIENT_BUFFER;
6614 hr = IUri_GetHost(uri, &received);
6620 memcpy(output, received, (len+1)*sizeof(WCHAR));
6621 SysFreeString(received);
6626 static HRESULT parse_domain(IUri *uri, LPWSTR output, DWORD output_len, DWORD *result_len)
6632 hr = IUri_GetPropertyLength(uri, Uri_PROPERTY_DOMAIN, &len, 0);
6639 if(len+1 > output_len)
6640 return STRSAFE_E_INSUFFICIENT_BUFFER;
6642 hr = IUri_GetDomain(uri, &received);
6648 memcpy(output, received, (len+1)*sizeof(WCHAR));
6649 SysFreeString(received);
6654 static HRESULT parse_anchor(IUri *uri, LPWSTR output, DWORD output_len, DWORD *result_len)
6660 hr = IUri_GetPropertyLength(uri, Uri_PROPERTY_FRAGMENT, &len, 0);
6667 if(len+1 > output_len)
6668 return STRSAFE_E_INSUFFICIENT_BUFFER;
6670 hr = IUri_GetFragment(uri, &received);
6676 memcpy(output, received, (len+1)*sizeof(WCHAR));
6677 SysFreeString(received);
6682 /***********************************************************************
6683 * CoInternetParseIUri (urlmon.@)
6685 HRESULT WINAPI CoInternetParseIUri(IUri *pIUri, PARSEACTION ParseAction, DWORD dwFlags,
6686 LPWSTR pwzResult, DWORD cchResult, DWORD *pcchResult,
6687 DWORD_PTR dwReserved)
6691 IInternetProtocolInfo *info;
6693 TRACE("(%p %d %x %p %d %p %x)\n", pIUri, ParseAction, dwFlags, pwzResult,
6694 cchResult, pcchResult, (DWORD)dwReserved);
6699 if(!pwzResult || !pIUri) {
6701 return E_INVALIDARG;
6704 if(!(uri = get_uri_obj(pIUri))) {
6706 FIXME("(%p %d %x %p %d %p %x) Unknown IUri's not supported for this action.\n",
6707 pIUri, ParseAction, dwFlags, pwzResult, cchResult, pcchResult, (DWORD)dwReserved);
6711 info = get_protocol_info(uri->canon_uri);
6713 hr = IInternetProtocolInfo_ParseUrl(info, uri->canon_uri, ParseAction, dwFlags,
6714 pwzResult, cchResult, pcchResult, 0);
6715 IInternetProtocolInfo_Release(info);
6716 if(SUCCEEDED(hr)) return hr;
6719 switch(ParseAction) {
6720 case PARSE_CANONICALIZE:
6721 hr = parse_canonicalize(uri, dwFlags, pwzResult, cchResult, pcchResult);
6723 case PARSE_FRIENDLY:
6724 hr = parse_friendly(pIUri, pwzResult, cchResult, pcchResult);
6726 case PARSE_ROOTDOCUMENT:
6727 hr = parse_rootdocument(uri, pwzResult, cchResult, pcchResult);
6729 case PARSE_DOCUMENT:
6730 hr = parse_document(uri, pwzResult, cchResult, pcchResult);
6732 case PARSE_PATH_FROM_URL:
6733 hr = parse_path_from_url(uri, pwzResult, cchResult, pcchResult);
6735 case PARSE_URL_FROM_PATH:
6736 hr = parse_url_from_path(pIUri, pwzResult, cchResult, pcchResult);
6739 hr = parse_schema(pIUri, pwzResult, cchResult, pcchResult);
6742 hr = parse_site(pIUri, pwzResult, cchResult, pcchResult);
6745 hr = parse_domain(pIUri, pwzResult, cchResult, pcchResult);
6747 case PARSE_LOCATION:
6749 hr = parse_anchor(pIUri, pwzResult, cchResult, pcchResult);
6751 case PARSE_SECURITY_URL:
6754 case PARSE_SECURITY_DOMAIN:
6761 FIXME("(%p %d %x %p %d %p %x) Partial stub.\n", pIUri, ParseAction, dwFlags,
6762 pwzResult, cchResult, pcchResult, (DWORD)dwReserved);