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 /* Checks if the two Uri's are logically equivalent. It's a simple
3863 * comparison, since they are both of type Uri, and it can access
3864 * the properties of each Uri directly without the need to go
3865 * through the "IUri_Get*" interface calls.
3867 static HRESULT compare_uris(const Uri *a, const Uri *b, BOOL *ret) {
3868 const BOOL known_scheme = a->scheme_type != URL_SCHEME_UNKNOWN;
3869 const BOOL are_hierarchical = a->authority_start > -1 && b->authority_start > -1;
3873 if(a->scheme_type != b->scheme_type)
3876 if(a->scheme_type == URL_SCHEME_FILE) {
3877 if(a->canon_len == b->canon_len) {
3878 *ret = !StrCmpIW(a->canon_uri, b->canon_uri);
3883 /* Only compare the scheme names (if any) if their unknown scheme types. */
3885 if((a->scheme_start > -1 && b->scheme_start > -1) &&
3886 (a->scheme_len == b->scheme_len)) {
3887 /* Make sure the schemes are the same. */
3888 if(StrCmpNW(a->canon_uri+a->scheme_start, b->canon_uri+b->scheme_start, a->scheme_len))
3890 } else if(a->scheme_len != b->scheme_len)
3891 /* One of the Uri's has a scheme name, while the other doesn't. */
3895 /* If they have a userinfo component, perform case sensitive compare. */
3896 if((a->userinfo_start > -1 && b->userinfo_start > -1) &&
3897 (a->userinfo_len == b->userinfo_len)) {
3898 if(StrCmpNW(a->canon_uri+a->userinfo_start, b->canon_uri+b->userinfo_start, a->userinfo_len))
3900 } else if(a->userinfo_len != b->userinfo_len)
3901 /* One of the Uri's had a userinfo, while the other one doesn't. */
3904 /* Check if they have a host name. */
3905 if((a->host_start > -1 && b->host_start > -1) &&
3906 (a->host_len == b->host_len)) {
3907 /* Perform a case insensitive compare if they are a known scheme type. */
3909 if(StrCmpNIW(a->canon_uri+a->host_start, b->canon_uri+b->host_start, a->host_len))
3911 } else if(StrCmpNW(a->canon_uri+a->host_start, b->canon_uri+b->host_start, a->host_len))
3913 } else if(a->host_len != b->host_len)
3914 /* One of the Uri's had a host, while the other one didn't. */
3917 if(a->has_port && b->has_port) {
3918 if(a->port != b->port)
3920 } else if(a->has_port || b->has_port)
3921 /* One had a port, while the other one didn't. */
3924 /* Windows is weird with how it handles paths. For example
3925 * One URI could be "http://google.com" (after canonicalization)
3926 * and one could be "http://google.com/" and the IsEqual function
3927 * would still evaluate to TRUE, but, only if they are both hierarchical
3930 if((a->path_start > -1 && b->path_start > -1) &&
3931 (a->path_len == b->path_len)) {
3932 if(StrCmpNW(a->canon_uri+a->path_start, b->canon_uri+b->path_start, a->path_len))
3934 } else if(are_hierarchical && a->path_len == -1 && b->path_len == 0) {
3935 if(*(a->canon_uri+a->path_start) != '/')
3937 } else if(are_hierarchical && b->path_len == 1 && a->path_len == 0) {
3938 if(*(b->canon_uri+b->path_start) != '/')
3940 } else if(a->path_len != b->path_len)
3943 /* Compare the query strings of the two URIs. */
3944 if((a->query_start > -1 && b->query_start > -1) &&
3945 (a->query_len == b->query_len)) {
3946 if(StrCmpNW(a->canon_uri+a->query_start, b->canon_uri+b->query_start, a->query_len))
3948 } else if(a->query_len != b->query_len)
3951 if((a->fragment_start > -1 && b->fragment_start > -1) &&
3952 (a->fragment_len == b->fragment_len)) {
3953 if(StrCmpNW(a->canon_uri+a->fragment_start, b->canon_uri+b->fragment_start, a->fragment_len))
3955 } else if(a->fragment_len != b->fragment_len)
3958 /* If we get here, the two URIs are equivalent. */
3963 static void convert_to_dos_path(const WCHAR *path, DWORD path_len,
3964 WCHAR *output, DWORD *output_len)
3966 const WCHAR *ptr = path;
3968 if(path_len > 3 && *ptr == '/' && is_drive_path(path+1))
3969 /* Skip over the leading / before the drive path. */
3972 for(; ptr < path+path_len; ++ptr) {
3985 /* Generates a raw uri string using the parse_data. */
3986 static DWORD generate_raw_uri(const parse_data *data, BSTR uri, DWORD flags) {
3991 memcpy(uri, data->scheme, data->scheme_len*sizeof(WCHAR));
3992 uri[data->scheme_len] = ':';
3994 length += data->scheme_len+1;
3997 if(!data->is_opaque) {
3998 /* For the "//" which appears before the authority component. */
4001 uri[length+1] = '/';
4005 /* Check if we need to add the "\\" before the host name
4006 * of a UNC server name in a DOS path.
4008 if(flags & RAW_URI_CONVERT_TO_DOS_PATH &&
4009 data->scheme_type == URL_SCHEME_FILE && data->host) {
4012 uri[length+1] = '\\';
4018 if(data->username) {
4020 memcpy(uri+length, data->username, data->username_len*sizeof(WCHAR));
4021 length += data->username_len;
4024 if(data->password) {
4027 memcpy(uri+length+1, data->password, data->password_len*sizeof(WCHAR));
4029 length += data->password_len+1;
4032 if(data->password || data->username) {
4039 /* IPv6 addresses get the brackets added around them if they don't already
4042 const BOOL add_brackets = data->host_type == Uri_HOST_IPV6 && *(data->host) != '[';
4050 memcpy(uri+length, data->host, data->host_len*sizeof(WCHAR));
4051 length += data->host_len;
4060 if(data->has_port) {
4061 /* The port isn't included in the raw uri if it's the default
4062 * port for the scheme type.
4065 BOOL is_default = FALSE;
4067 for(i = 0; i < sizeof(default_ports)/sizeof(default_ports[0]); ++i) {
4068 if(data->scheme_type == default_ports[i].scheme &&
4069 data->port_value == default_ports[i].port)
4073 if(!is_default || flags & RAW_URI_FORCE_PORT_DISP) {
4079 length += ui2str(uri+length, data->port_value);
4081 length += ui2str(NULL, data->port_value);
4085 /* Check if a '/' should be added before the path for hierarchical URIs. */
4086 if(!data->is_opaque && data->path && *(data->path) != '/') {
4093 if(!data->is_opaque && data->scheme_type == URL_SCHEME_FILE &&
4094 flags & RAW_URI_CONVERT_TO_DOS_PATH) {
4098 convert_to_dos_path(data->path, data->path_len, uri+length, &len);
4100 convert_to_dos_path(data->path, data->path_len, NULL, &len);
4105 memcpy(uri+length, data->path, data->path_len*sizeof(WCHAR));
4106 length += data->path_len;
4112 memcpy(uri+length, data->query, data->query_len*sizeof(WCHAR));
4113 length += data->query_len;
4116 if(data->fragment) {
4118 memcpy(uri+length, data->fragment, data->fragment_len*sizeof(WCHAR));
4119 length += data->fragment_len;
4123 TRACE("(%p %p): Generated raw uri=%s len=%d\n", data, uri, debugstr_wn(uri, length), length);
4125 TRACE("(%p %p): Computed raw uri len=%d\n", data, uri, length);
4130 static HRESULT generate_uri(const UriBuilder *builder, const parse_data *data, Uri *uri, DWORD flags) {
4132 DWORD length = generate_raw_uri(data, NULL, 0);
4133 uri->raw_uri = SysAllocStringLen(NULL, length);
4135 return E_OUTOFMEMORY;
4137 generate_raw_uri(data, uri->raw_uri, 0);
4139 hr = canonicalize_uri(data, uri, flags);
4141 if(hr == E_INVALIDARG)
4142 return INET_E_INVALID_URL;
4146 uri->create_flags = flags;
4150 static inline Uri* impl_from_IUri(IUri *iface)
4152 return CONTAINING_RECORD(iface, Uri, IUri_iface);
4155 static inline void destory_uri_obj(Uri *This)
4157 SysFreeString(This->raw_uri);
4158 heap_free(This->canon_uri);
4162 static HRESULT WINAPI Uri_QueryInterface(IUri *iface, REFIID riid, void **ppv)
4164 Uri *This = impl_from_IUri(iface);
4166 if(IsEqualGUID(&IID_IUnknown, riid)) {
4167 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
4168 *ppv = &This->IUri_iface;
4169 }else if(IsEqualGUID(&IID_IUri, riid)) {
4170 TRACE("(%p)->(IID_IUri %p)\n", This, ppv);
4171 *ppv = &This->IUri_iface;
4172 }else if(IsEqualGUID(&IID_IUriBuilderFactory, riid)) {
4173 TRACE("(%p)->(IID_IUriBuilderFactory %p)\n", This, riid);
4174 *ppv = &This->IUriBuilderFactory_iface;
4175 }else if(IsEqualGUID(&IID_IUriObj, riid)) {
4176 TRACE("(%p)->(IID_IUriObj %p)\n", This, ppv);
4180 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
4182 return E_NOINTERFACE;
4185 IUnknown_AddRef((IUnknown*)*ppv);
4189 static ULONG WINAPI Uri_AddRef(IUri *iface)
4191 Uri *This = impl_from_IUri(iface);
4192 LONG ref = InterlockedIncrement(&This->ref);
4194 TRACE("(%p) ref=%d\n", This, ref);
4199 static ULONG WINAPI Uri_Release(IUri *iface)
4201 Uri *This = impl_from_IUri(iface);
4202 LONG ref = InterlockedDecrement(&This->ref);
4204 TRACE("(%p) ref=%d\n", This, ref);
4207 destory_uri_obj(This);
4212 static HRESULT WINAPI Uri_GetPropertyBSTR(IUri *iface, Uri_PROPERTY uriProp, BSTR *pbstrProperty, DWORD dwFlags)
4214 Uri *This = impl_from_IUri(iface);
4216 TRACE("(%p %s)->(%d %p %x)\n", This, debugstr_w(This->canon_uri), uriProp, pbstrProperty, dwFlags);
4221 if(uriProp > Uri_PROPERTY_STRING_LAST) {
4222 /* Windows allocates an empty BSTR for invalid Uri_PROPERTY's. */
4223 *pbstrProperty = SysAllocStringLen(NULL, 0);
4224 if(!(*pbstrProperty))
4225 return E_OUTOFMEMORY;
4227 /* It only returns S_FALSE for the ZONE property... */
4228 if(uriProp == Uri_PROPERTY_ZONE)
4234 /* Don't have support for flags yet. */
4236 FIXME("(%p)->(%d %p %x)\n", This, uriProp, pbstrProperty, dwFlags);
4241 case Uri_PROPERTY_ABSOLUTE_URI:
4242 if(This->display_modifiers & URI_DISPLAY_NO_ABSOLUTE_URI) {
4243 *pbstrProperty = SysAllocStringLen(NULL, 0);
4246 if(This->scheme_type != URL_SCHEME_UNKNOWN && This->userinfo_start > -1) {
4247 if(This->userinfo_len == 0) {
4248 /* Don't include the '@' after the userinfo component. */
4249 *pbstrProperty = SysAllocStringLen(NULL, This->canon_len-1);
4251 if(*pbstrProperty) {
4252 /* Copy everything before it. */
4253 memcpy(*pbstrProperty, This->canon_uri, This->userinfo_start*sizeof(WCHAR));
4255 /* And everything after it. */
4256 memcpy(*pbstrProperty+This->userinfo_start, This->canon_uri+This->userinfo_start+1,
4257 (This->canon_len-This->userinfo_start-1)*sizeof(WCHAR));
4259 } else if(This->userinfo_split == 0 && This->userinfo_len == 1) {
4260 /* Don't include the ":@" */
4261 *pbstrProperty = SysAllocStringLen(NULL, This->canon_len-2);
4263 if(*pbstrProperty) {
4264 memcpy(*pbstrProperty, This->canon_uri, This->userinfo_start*sizeof(WCHAR));
4265 memcpy(*pbstrProperty+This->userinfo_start, This->canon_uri+This->userinfo_start+2,
4266 (This->canon_len-This->userinfo_start-2)*sizeof(WCHAR));
4269 *pbstrProperty = SysAllocString(This->canon_uri);
4273 *pbstrProperty = SysAllocString(This->canon_uri);
4278 if(!(*pbstrProperty))
4279 hres = E_OUTOFMEMORY;
4282 case Uri_PROPERTY_AUTHORITY:
4283 if(This->authority_start > -1) {
4284 if(This->port_offset > -1 && is_default_port(This->scheme_type, This->port) &&
4285 This->display_modifiers & URI_DISPLAY_NO_DEFAULT_PORT_AUTH)
4286 /* Don't include the port in the authority component. */
4287 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->authority_start, This->port_offset);
4289 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->authority_start, This->authority_len);
4292 *pbstrProperty = SysAllocStringLen(NULL, 0);
4296 if(!(*pbstrProperty))
4297 hres = E_OUTOFMEMORY;
4300 case Uri_PROPERTY_DISPLAY_URI:
4301 /* The Display URI contains everything except for the userinfo for known
4304 if(This->scheme_type != URL_SCHEME_UNKNOWN && This->userinfo_start > -1) {
4305 *pbstrProperty = SysAllocStringLen(NULL, This->canon_len-This->userinfo_len);
4307 if(*pbstrProperty) {
4308 /* Copy everything before the userinfo over. */
4309 memcpy(*pbstrProperty, This->canon_uri, This->userinfo_start*sizeof(WCHAR));
4310 /* Copy everything after the userinfo over. */
4311 memcpy(*pbstrProperty+This->userinfo_start,
4312 This->canon_uri+This->userinfo_start+This->userinfo_len+1,
4313 (This->canon_len-(This->userinfo_start+This->userinfo_len+1))*sizeof(WCHAR));
4316 *pbstrProperty = SysAllocString(This->canon_uri);
4318 if(!(*pbstrProperty))
4319 hres = E_OUTOFMEMORY;
4324 case Uri_PROPERTY_DOMAIN:
4325 if(This->domain_offset > -1) {
4326 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->host_start+This->domain_offset,
4327 This->host_len-This->domain_offset);
4330 *pbstrProperty = SysAllocStringLen(NULL, 0);
4334 if(!(*pbstrProperty))
4335 hres = E_OUTOFMEMORY;
4338 case Uri_PROPERTY_EXTENSION:
4339 if(This->extension_offset > -1) {
4340 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->path_start+This->extension_offset,
4341 This->path_len-This->extension_offset);
4344 *pbstrProperty = SysAllocStringLen(NULL, 0);
4348 if(!(*pbstrProperty))
4349 hres = E_OUTOFMEMORY;
4352 case Uri_PROPERTY_FRAGMENT:
4353 if(This->fragment_start > -1) {
4354 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->fragment_start, This->fragment_len);
4357 *pbstrProperty = SysAllocStringLen(NULL, 0);
4361 if(!(*pbstrProperty))
4362 hres = E_OUTOFMEMORY;
4365 case Uri_PROPERTY_HOST:
4366 if(This->host_start > -1) {
4367 /* The '[' and ']' aren't included for IPv6 addresses. */
4368 if(This->host_type == Uri_HOST_IPV6)
4369 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->host_start+1, This->host_len-2);
4371 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->host_start, This->host_len);
4375 *pbstrProperty = SysAllocStringLen(NULL, 0);
4379 if(!(*pbstrProperty))
4380 hres = E_OUTOFMEMORY;
4383 case Uri_PROPERTY_PASSWORD:
4384 if(This->userinfo_split > -1) {
4385 *pbstrProperty = SysAllocStringLen(
4386 This->canon_uri+This->userinfo_start+This->userinfo_split+1,
4387 This->userinfo_len-This->userinfo_split-1);
4390 *pbstrProperty = SysAllocStringLen(NULL, 0);
4394 if(!(*pbstrProperty))
4395 return E_OUTOFMEMORY;
4398 case Uri_PROPERTY_PATH:
4399 if(This->path_start > -1) {
4400 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->path_start, This->path_len);
4403 *pbstrProperty = SysAllocStringLen(NULL, 0);
4407 if(!(*pbstrProperty))
4408 hres = E_OUTOFMEMORY;
4411 case Uri_PROPERTY_PATH_AND_QUERY:
4412 if(This->path_start > -1) {
4413 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->path_start, This->path_len+This->query_len);
4415 } else if(This->query_start > -1) {
4416 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->query_start, This->query_len);
4419 *pbstrProperty = SysAllocStringLen(NULL, 0);
4423 if(!(*pbstrProperty))
4424 hres = E_OUTOFMEMORY;
4427 case Uri_PROPERTY_QUERY:
4428 if(This->query_start > -1) {
4429 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->query_start, This->query_len);
4432 *pbstrProperty = SysAllocStringLen(NULL, 0);
4436 if(!(*pbstrProperty))
4437 hres = E_OUTOFMEMORY;
4440 case Uri_PROPERTY_RAW_URI:
4441 *pbstrProperty = SysAllocString(This->raw_uri);
4442 if(!(*pbstrProperty))
4443 hres = E_OUTOFMEMORY;
4447 case Uri_PROPERTY_SCHEME_NAME:
4448 if(This->scheme_start > -1) {
4449 *pbstrProperty = SysAllocStringLen(This->canon_uri + This->scheme_start, This->scheme_len);
4452 *pbstrProperty = SysAllocStringLen(NULL, 0);
4456 if(!(*pbstrProperty))
4457 hres = E_OUTOFMEMORY;
4460 case Uri_PROPERTY_USER_INFO:
4461 if(This->userinfo_start > -1) {
4462 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->userinfo_start, This->userinfo_len);
4465 *pbstrProperty = SysAllocStringLen(NULL, 0);
4469 if(!(*pbstrProperty))
4470 hres = E_OUTOFMEMORY;
4473 case Uri_PROPERTY_USER_NAME:
4474 if(This->userinfo_start > -1 && This->userinfo_split != 0) {
4475 /* If userinfo_split is set, that means a password exists
4476 * so the username is only from userinfo_start to userinfo_split.
4478 if(This->userinfo_split > -1) {
4479 *pbstrProperty = SysAllocStringLen(This->canon_uri + This->userinfo_start, This->userinfo_split);
4482 *pbstrProperty = SysAllocStringLen(This->canon_uri + This->userinfo_start, This->userinfo_len);
4486 *pbstrProperty = SysAllocStringLen(NULL, 0);
4490 if(!(*pbstrProperty))
4491 return E_OUTOFMEMORY;
4495 FIXME("(%p)->(%d %p %x)\n", This, uriProp, pbstrProperty, dwFlags);
4502 static HRESULT WINAPI Uri_GetPropertyLength(IUri *iface, Uri_PROPERTY uriProp, DWORD *pcchProperty, DWORD dwFlags)
4504 Uri *This = impl_from_IUri(iface);
4506 TRACE("(%p %s)->(%d %p %x)\n", This, debugstr_w(This->canon_uri), uriProp, pcchProperty, dwFlags);
4509 return E_INVALIDARG;
4511 /* Can only return a length for a property if it's a string. */
4512 if(uriProp > Uri_PROPERTY_STRING_LAST)
4513 return E_INVALIDARG;
4515 /* Don't have support for flags yet. */
4517 FIXME("(%p)->(%d %p %x)\n", This, uriProp, pcchProperty, dwFlags);
4522 case Uri_PROPERTY_ABSOLUTE_URI:
4523 if(This->display_modifiers & URI_DISPLAY_NO_ABSOLUTE_URI) {
4527 if(This->scheme_type != URL_SCHEME_UNKNOWN) {
4528 if(This->userinfo_start > -1 && This->userinfo_len == 0)
4529 /* Don't include the '@' in the length. */
4530 *pcchProperty = This->canon_len-1;
4531 else if(This->userinfo_start > -1 && This->userinfo_len == 1 &&
4532 This->userinfo_split == 0)
4533 /* Don't include the ":@" in the length. */
4534 *pcchProperty = This->canon_len-2;
4536 *pcchProperty = This->canon_len;
4538 *pcchProperty = This->canon_len;
4544 case Uri_PROPERTY_AUTHORITY:
4545 if(This->port_offset > -1 &&
4546 This->display_modifiers & URI_DISPLAY_NO_DEFAULT_PORT_AUTH &&
4547 is_default_port(This->scheme_type, This->port))
4548 /* Only count up until the port in the authority. */
4549 *pcchProperty = This->port_offset;
4551 *pcchProperty = This->authority_len;
4552 hres = (This->authority_start > -1) ? S_OK : S_FALSE;
4554 case Uri_PROPERTY_DISPLAY_URI:
4555 if(This->scheme_type != URL_SCHEME_UNKNOWN && This->userinfo_start > -1)
4556 *pcchProperty = This->canon_len-This->userinfo_len-1;
4558 *pcchProperty = This->canon_len;
4562 case Uri_PROPERTY_DOMAIN:
4563 if(This->domain_offset > -1)
4564 *pcchProperty = This->host_len - This->domain_offset;
4568 hres = (This->domain_offset > -1) ? S_OK : S_FALSE;
4570 case Uri_PROPERTY_EXTENSION:
4571 if(This->extension_offset > -1) {
4572 *pcchProperty = This->path_len - This->extension_offset;
4580 case Uri_PROPERTY_FRAGMENT:
4581 *pcchProperty = This->fragment_len;
4582 hres = (This->fragment_start > -1) ? S_OK : S_FALSE;
4584 case Uri_PROPERTY_HOST:
4585 *pcchProperty = This->host_len;
4587 /* '[' and ']' aren't included in the length. */
4588 if(This->host_type == Uri_HOST_IPV6)
4591 hres = (This->host_start > -1) ? S_OK : S_FALSE;
4593 case Uri_PROPERTY_PASSWORD:
4594 *pcchProperty = (This->userinfo_split > -1) ? This->userinfo_len-This->userinfo_split-1 : 0;
4595 hres = (This->userinfo_split > -1) ? S_OK : S_FALSE;
4597 case Uri_PROPERTY_PATH:
4598 *pcchProperty = This->path_len;
4599 hres = (This->path_start > -1) ? S_OK : S_FALSE;
4601 case Uri_PROPERTY_PATH_AND_QUERY:
4602 *pcchProperty = This->path_len+This->query_len;
4603 hres = (This->path_start > -1 || This->query_start > -1) ? S_OK : S_FALSE;
4605 case Uri_PROPERTY_QUERY:
4606 *pcchProperty = This->query_len;
4607 hres = (This->query_start > -1) ? S_OK : S_FALSE;
4609 case Uri_PROPERTY_RAW_URI:
4610 *pcchProperty = SysStringLen(This->raw_uri);
4613 case Uri_PROPERTY_SCHEME_NAME:
4614 *pcchProperty = This->scheme_len;
4615 hres = (This->scheme_start > -1) ? S_OK : S_FALSE;
4617 case Uri_PROPERTY_USER_INFO:
4618 *pcchProperty = This->userinfo_len;
4619 hres = (This->userinfo_start > -1) ? S_OK : S_FALSE;
4621 case Uri_PROPERTY_USER_NAME:
4622 *pcchProperty = (This->userinfo_split > -1) ? This->userinfo_split : This->userinfo_len;
4623 if(This->userinfo_split == 0)
4626 hres = (This->userinfo_start > -1) ? S_OK : S_FALSE;
4629 FIXME("(%p)->(%d %p %x)\n", This, uriProp, pcchProperty, dwFlags);
4636 static HRESULT WINAPI Uri_GetPropertyDWORD(IUri *iface, Uri_PROPERTY uriProp, DWORD *pcchProperty, DWORD dwFlags)
4638 Uri *This = impl_from_IUri(iface);
4641 TRACE("(%p %s)->(%d %p %x)\n", This, debugstr_w(This->canon_uri), uriProp, pcchProperty, dwFlags);
4644 return E_INVALIDARG;
4646 /* Microsoft's implementation for the ZONE property of a URI seems to be lacking...
4647 * From what I can tell, instead of checking which URLZONE the URI belongs to it
4648 * simply assigns URLZONE_INVALID and returns E_NOTIMPL. This also applies to the GetZone
4651 if(uriProp == Uri_PROPERTY_ZONE) {
4652 *pcchProperty = URLZONE_INVALID;
4656 if(uriProp < Uri_PROPERTY_DWORD_START) {
4658 return E_INVALIDARG;
4662 case Uri_PROPERTY_HOST_TYPE:
4663 *pcchProperty = This->host_type;
4666 case Uri_PROPERTY_PORT:
4667 if(!This->has_port) {
4671 *pcchProperty = This->port;
4676 case Uri_PROPERTY_SCHEME:
4677 *pcchProperty = This->scheme_type;
4681 FIXME("(%p)->(%d %p %x)\n", This, uriProp, pcchProperty, dwFlags);
4688 static HRESULT WINAPI Uri_HasProperty(IUri *iface, Uri_PROPERTY uriProp, BOOL *pfHasProperty)
4690 Uri *This = impl_from_IUri(iface);
4692 TRACE("(%p %s)->(%d %p)\n", This, debugstr_w(This->canon_uri), uriProp, pfHasProperty);
4695 return E_INVALIDARG;
4698 case Uri_PROPERTY_ABSOLUTE_URI:
4699 *pfHasProperty = !(This->display_modifiers & URI_DISPLAY_NO_ABSOLUTE_URI);
4701 case Uri_PROPERTY_AUTHORITY:
4702 *pfHasProperty = This->authority_start > -1;
4704 case Uri_PROPERTY_DISPLAY_URI:
4705 *pfHasProperty = TRUE;
4707 case Uri_PROPERTY_DOMAIN:
4708 *pfHasProperty = This->domain_offset > -1;
4710 case Uri_PROPERTY_EXTENSION:
4711 *pfHasProperty = This->extension_offset > -1;
4713 case Uri_PROPERTY_FRAGMENT:
4714 *pfHasProperty = This->fragment_start > -1;
4716 case Uri_PROPERTY_HOST:
4717 *pfHasProperty = This->host_start > -1;
4719 case Uri_PROPERTY_PASSWORD:
4720 *pfHasProperty = This->userinfo_split > -1;
4722 case Uri_PROPERTY_PATH:
4723 *pfHasProperty = This->path_start > -1;
4725 case Uri_PROPERTY_PATH_AND_QUERY:
4726 *pfHasProperty = (This->path_start > -1 || This->query_start > -1);
4728 case Uri_PROPERTY_QUERY:
4729 *pfHasProperty = This->query_start > -1;
4731 case Uri_PROPERTY_RAW_URI:
4732 *pfHasProperty = TRUE;
4734 case Uri_PROPERTY_SCHEME_NAME:
4735 *pfHasProperty = This->scheme_start > -1;
4737 case Uri_PROPERTY_USER_INFO:
4738 *pfHasProperty = This->userinfo_start > -1;
4740 case Uri_PROPERTY_USER_NAME:
4741 if(This->userinfo_split == 0)
4742 *pfHasProperty = FALSE;
4744 *pfHasProperty = This->userinfo_start > -1;
4746 case Uri_PROPERTY_HOST_TYPE:
4747 *pfHasProperty = TRUE;
4749 case Uri_PROPERTY_PORT:
4750 *pfHasProperty = This->has_port;
4752 case Uri_PROPERTY_SCHEME:
4753 *pfHasProperty = TRUE;
4755 case Uri_PROPERTY_ZONE:
4756 *pfHasProperty = FALSE;
4759 FIXME("(%p)->(%d %p): Unsupported property type.\n", This, uriProp, pfHasProperty);
4766 static HRESULT WINAPI Uri_GetAbsoluteUri(IUri *iface, BSTR *pstrAbsoluteUri)
4768 TRACE("(%p)->(%p)\n", iface, pstrAbsoluteUri);
4769 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_ABSOLUTE_URI, pstrAbsoluteUri, 0);
4772 static HRESULT WINAPI Uri_GetAuthority(IUri *iface, BSTR *pstrAuthority)
4774 TRACE("(%p)->(%p)\n", iface, pstrAuthority);
4775 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_AUTHORITY, pstrAuthority, 0);
4778 static HRESULT WINAPI Uri_GetDisplayUri(IUri *iface, BSTR *pstrDisplayUri)
4780 TRACE("(%p)->(%p)\n", iface, pstrDisplayUri);
4781 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_DISPLAY_URI, pstrDisplayUri, 0);
4784 static HRESULT WINAPI Uri_GetDomain(IUri *iface, BSTR *pstrDomain)
4786 TRACE("(%p)->(%p)\n", iface, pstrDomain);
4787 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_DOMAIN, pstrDomain, 0);
4790 static HRESULT WINAPI Uri_GetExtension(IUri *iface, BSTR *pstrExtension)
4792 TRACE("(%p)->(%p)\n", iface, pstrExtension);
4793 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_EXTENSION, pstrExtension, 0);
4796 static HRESULT WINAPI Uri_GetFragment(IUri *iface, BSTR *pstrFragment)
4798 TRACE("(%p)->(%p)\n", iface, pstrFragment);
4799 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_FRAGMENT, pstrFragment, 0);
4802 static HRESULT WINAPI Uri_GetHost(IUri *iface, BSTR *pstrHost)
4804 TRACE("(%p)->(%p)\n", iface, pstrHost);
4805 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_HOST, pstrHost, 0);
4808 static HRESULT WINAPI Uri_GetPassword(IUri *iface, BSTR *pstrPassword)
4810 TRACE("(%p)->(%p)\n", iface, pstrPassword);
4811 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_PASSWORD, pstrPassword, 0);
4814 static HRESULT WINAPI Uri_GetPath(IUri *iface, BSTR *pstrPath)
4816 TRACE("(%p)->(%p)\n", iface, pstrPath);
4817 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_PATH, pstrPath, 0);
4820 static HRESULT WINAPI Uri_GetPathAndQuery(IUri *iface, BSTR *pstrPathAndQuery)
4822 TRACE("(%p)->(%p)\n", iface, pstrPathAndQuery);
4823 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_PATH_AND_QUERY, pstrPathAndQuery, 0);
4826 static HRESULT WINAPI Uri_GetQuery(IUri *iface, BSTR *pstrQuery)
4828 TRACE("(%p)->(%p)\n", iface, pstrQuery);
4829 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_QUERY, pstrQuery, 0);
4832 static HRESULT WINAPI Uri_GetRawUri(IUri *iface, BSTR *pstrRawUri)
4834 TRACE("(%p)->(%p)\n", iface, pstrRawUri);
4835 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_RAW_URI, pstrRawUri, 0);
4838 static HRESULT WINAPI Uri_GetSchemeName(IUri *iface, BSTR *pstrSchemeName)
4840 TRACE("(%p)->(%p)\n", iface, pstrSchemeName);
4841 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_SCHEME_NAME, pstrSchemeName, 0);
4844 static HRESULT WINAPI Uri_GetUserInfo(IUri *iface, BSTR *pstrUserInfo)
4846 TRACE("(%p)->(%p)\n", iface, pstrUserInfo);
4847 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_USER_INFO, pstrUserInfo, 0);
4850 static HRESULT WINAPI Uri_GetUserName(IUri *iface, BSTR *pstrUserName)
4852 TRACE("(%p)->(%p)\n", iface, pstrUserName);
4853 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_USER_NAME, pstrUserName, 0);
4856 static HRESULT WINAPI Uri_GetHostType(IUri *iface, DWORD *pdwHostType)
4858 TRACE("(%p)->(%p)\n", iface, pdwHostType);
4859 return IUri_GetPropertyDWORD(iface, Uri_PROPERTY_HOST_TYPE, pdwHostType, 0);
4862 static HRESULT WINAPI Uri_GetPort(IUri *iface, DWORD *pdwPort)
4864 TRACE("(%p)->(%p)\n", iface, pdwPort);
4865 return IUri_GetPropertyDWORD(iface, Uri_PROPERTY_PORT, pdwPort, 0);
4868 static HRESULT WINAPI Uri_GetScheme(IUri *iface, DWORD *pdwScheme)
4870 TRACE("(%p)->(%p)\n", iface, pdwScheme);
4871 return IUri_GetPropertyDWORD(iface, Uri_PROPERTY_SCHEME, pdwScheme, 0);
4874 static HRESULT WINAPI Uri_GetZone(IUri *iface, DWORD *pdwZone)
4876 TRACE("(%p)->(%p)\n", iface, pdwZone);
4877 return IUri_GetPropertyDWORD(iface, Uri_PROPERTY_ZONE,pdwZone, 0);
4880 static HRESULT WINAPI Uri_GetProperties(IUri *iface, DWORD *pdwProperties)
4882 Uri *This = impl_from_IUri(iface);
4883 TRACE("(%p %s)->(%p)\n", This, debugstr_w(This->canon_uri), pdwProperties);
4886 return E_INVALIDARG;
4888 /* All URIs have these. */
4889 *pdwProperties = Uri_HAS_DISPLAY_URI|Uri_HAS_RAW_URI|Uri_HAS_SCHEME|Uri_HAS_HOST_TYPE;
4891 if(!(This->display_modifiers & URI_DISPLAY_NO_ABSOLUTE_URI))
4892 *pdwProperties |= Uri_HAS_ABSOLUTE_URI;
4894 if(This->scheme_start > -1)
4895 *pdwProperties |= Uri_HAS_SCHEME_NAME;
4897 if(This->authority_start > -1) {
4898 *pdwProperties |= Uri_HAS_AUTHORITY;
4899 if(This->userinfo_start > -1) {
4900 *pdwProperties |= Uri_HAS_USER_INFO;
4901 if(This->userinfo_split != 0)
4902 *pdwProperties |= Uri_HAS_USER_NAME;
4904 if(This->userinfo_split > -1)
4905 *pdwProperties |= Uri_HAS_PASSWORD;
4906 if(This->host_start > -1)
4907 *pdwProperties |= Uri_HAS_HOST;
4908 if(This->domain_offset > -1)
4909 *pdwProperties |= Uri_HAS_DOMAIN;
4913 *pdwProperties |= Uri_HAS_PORT;
4914 if(This->path_start > -1)
4915 *pdwProperties |= Uri_HAS_PATH|Uri_HAS_PATH_AND_QUERY;
4916 if(This->query_start > -1)
4917 *pdwProperties |= Uri_HAS_QUERY|Uri_HAS_PATH_AND_QUERY;
4919 if(This->extension_offset > -1)
4920 *pdwProperties |= Uri_HAS_EXTENSION;
4922 if(This->fragment_start > -1)
4923 *pdwProperties |= Uri_HAS_FRAGMENT;
4928 static HRESULT WINAPI Uri_IsEqual(IUri *iface, IUri *pUri, BOOL *pfEqual)
4930 Uri *This = impl_from_IUri(iface);
4933 TRACE("(%p %s)->(%p %p)\n", This, debugstr_w(This->canon_uri), pUri, pfEqual);
4941 /* For some reason Windows returns S_OK here... */
4945 /* Try to convert it to a Uri (allows for a more simple comparison). */
4946 if(!(other = get_uri_obj(pUri))) {
4947 FIXME("(%p)->(%p %p) No support for unknown IUri's yet.\n", iface, pUri, pfEqual);
4951 TRACE("comparing to %s\n", debugstr_w(other->canon_uri));
4952 return compare_uris(This, other, pfEqual);
4955 static const IUriVtbl UriVtbl = {
4959 Uri_GetPropertyBSTR,
4960 Uri_GetPropertyLength,
4961 Uri_GetPropertyDWORD,
4972 Uri_GetPathAndQuery,
4986 static inline Uri* impl_from_IUriBuilderFactory(IUriBuilderFactory *iface)
4988 return CONTAINING_RECORD(iface, Uri, IUriBuilderFactory_iface);
4991 static HRESULT WINAPI UriBuilderFactory_QueryInterface(IUriBuilderFactory *iface, REFIID riid, void **ppv)
4993 Uri *This = impl_from_IUriBuilderFactory(iface);
4995 if(IsEqualGUID(&IID_IUnknown, riid)) {
4996 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
4997 *ppv = &This->IUriBuilderFactory_iface;
4998 }else if(IsEqualGUID(&IID_IUriBuilderFactory, riid)) {
4999 TRACE("(%p)->(IID_IUriBuilderFactory %p)\n", This, ppv);
5000 *ppv = &This->IUriBuilderFactory_iface;
5001 }else if(IsEqualGUID(&IID_IUri, riid)) {
5002 TRACE("(%p)->(IID_IUri %p)\n", This, ppv);
5003 *ppv = &This->IUri_iface;
5005 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
5007 return E_NOINTERFACE;
5010 IUnknown_AddRef((IUnknown*)*ppv);
5014 static ULONG WINAPI UriBuilderFactory_AddRef(IUriBuilderFactory *iface)
5016 Uri *This = impl_from_IUriBuilderFactory(iface);
5017 LONG ref = InterlockedIncrement(&This->ref);
5019 TRACE("(%p) ref=%d\n", This, ref);
5024 static ULONG WINAPI UriBuilderFactory_Release(IUriBuilderFactory *iface)
5026 Uri *This = impl_from_IUriBuilderFactory(iface);
5027 LONG ref = InterlockedDecrement(&This->ref);
5029 TRACE("(%p) ref=%d\n", This, ref);
5032 destory_uri_obj(This);
5037 static HRESULT WINAPI UriBuilderFactory_CreateIUriBuilder(IUriBuilderFactory *iface,
5039 DWORD_PTR dwReserved,
5040 IUriBuilder **ppIUriBuilder)
5042 Uri *This = impl_from_IUriBuilderFactory(iface);
5043 TRACE("(%p)->(%08x %08x %p)\n", This, dwFlags, (DWORD)dwReserved, ppIUriBuilder);
5048 if(dwFlags || dwReserved) {
5049 *ppIUriBuilder = NULL;
5050 return E_INVALIDARG;
5053 return CreateIUriBuilder(NULL, 0, 0, ppIUriBuilder);
5056 static HRESULT WINAPI UriBuilderFactory_CreateInitializedIUriBuilder(IUriBuilderFactory *iface,
5058 DWORD_PTR dwReserved,
5059 IUriBuilder **ppIUriBuilder)
5061 Uri *This = impl_from_IUriBuilderFactory(iface);
5062 TRACE("(%p)->(%08x %08x %p)\n", This, dwFlags, (DWORD)dwReserved, ppIUriBuilder);
5067 if(dwFlags || dwReserved) {
5068 *ppIUriBuilder = NULL;
5069 return E_INVALIDARG;
5072 return CreateIUriBuilder(&This->IUri_iface, 0, 0, ppIUriBuilder);
5075 static const IUriBuilderFactoryVtbl UriBuilderFactoryVtbl = {
5076 UriBuilderFactory_QueryInterface,
5077 UriBuilderFactory_AddRef,
5078 UriBuilderFactory_Release,
5079 UriBuilderFactory_CreateIUriBuilder,
5080 UriBuilderFactory_CreateInitializedIUriBuilder
5083 static Uri* create_uri_obj(void) {
5084 Uri *ret = heap_alloc_zero(sizeof(Uri));
5086 ret->IUri_iface.lpVtbl = &UriVtbl;
5087 ret->IUriBuilderFactory_iface.lpVtbl = &UriBuilderFactoryVtbl;
5094 /***********************************************************************
5095 * CreateUri (urlmon.@)
5097 * Creates a new IUri object using the URI represented by pwzURI. This function
5098 * parses and validates the components of pwzURI and then canonicalizes the
5099 * parsed components.
5102 * pwzURI [I] The URI to parse, validate, and canonicalize.
5103 * dwFlags [I] Flags which can affect how the parsing/canonicalization is performed.
5104 * dwReserved [I] Reserved (not used).
5105 * ppURI [O] The resulting IUri after parsing/canonicalization occurs.
5108 * Success: Returns S_OK. ppURI contains the pointer to the newly allocated IUri.
5109 * Failure: E_INVALIDARG if there are invalid flag combinations in dwFlags, or an
5110 * invalid parameter, or pwzURI doesn't represent a valid URI.
5111 * E_OUTOFMEMORY if any memory allocation fails.
5115 * Uri_CREATE_CANONICALIZE, Uri_CREATE_DECODE_EXTRA_INFO, Uri_CREATE_CRACK_UNKNOWN_SCHEMES,
5116 * Uri_CREATE_PRE_PROCESS_HTML_URI, Uri_CREATE_NO_IE_SETTINGS.
5118 HRESULT WINAPI CreateUri(LPCWSTR pwzURI, DWORD dwFlags, DWORD_PTR dwReserved, IUri **ppURI)
5120 const DWORD supported_flags = Uri_CREATE_ALLOW_RELATIVE|Uri_CREATE_ALLOW_IMPLICIT_WILDCARD_SCHEME|
5121 Uri_CREATE_ALLOW_IMPLICIT_FILE_SCHEME|Uri_CREATE_NO_CANONICALIZE|Uri_CREATE_CANONICALIZE|
5122 Uri_CREATE_DECODE_EXTRA_INFO|Uri_CREATE_NO_DECODE_EXTRA_INFO|Uri_CREATE_CRACK_UNKNOWN_SCHEMES|
5123 Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES|Uri_CREATE_PRE_PROCESS_HTML_URI|Uri_CREATE_NO_PRE_PROCESS_HTML_URI|
5124 Uri_CREATE_NO_IE_SETTINGS|Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS|Uri_CREATE_FILE_USE_DOS_PATH;
5129 TRACE("(%s %x %x %p)\n", debugstr_w(pwzURI), dwFlags, (DWORD)dwReserved, ppURI);
5132 return E_INVALIDARG;
5136 return E_INVALIDARG;
5139 /* Check for invalid flags. */
5140 if(has_invalid_flag_combination(dwFlags)) {
5142 return E_INVALIDARG;
5145 /* Currently unsupported. */
5146 if(dwFlags & ~supported_flags)
5147 FIXME("Ignoring unsupported flag(s) %x\n", dwFlags & ~supported_flags);
5149 ret = create_uri_obj();
5152 return E_OUTOFMEMORY;
5155 /* Explicitly set the default flags if it doesn't cause a flag conflict. */
5156 apply_default_flags(&dwFlags);
5158 /* Pre process the URI, unless told otherwise. */
5159 if(!(dwFlags & Uri_CREATE_NO_PRE_PROCESS_HTML_URI))
5160 ret->raw_uri = pre_process_uri(pwzURI);
5162 ret->raw_uri = SysAllocString(pwzURI);
5166 return E_OUTOFMEMORY;
5169 memset(&data, 0, sizeof(parse_data));
5170 data.uri = ret->raw_uri;
5172 /* Validate and parse the URI into its components. */
5173 if(!parse_uri(&data, dwFlags)) {
5174 /* Encountered an unsupported or invalid URI */
5175 IUri_Release(&ret->IUri_iface);
5177 return E_INVALIDARG;
5180 /* Canonicalize the URI. */
5181 hr = canonicalize_uri(&data, ret, dwFlags);
5183 IUri_Release(&ret->IUri_iface);
5188 ret->create_flags = dwFlags;
5190 *ppURI = &ret->IUri_iface;
5194 /***********************************************************************
5195 * CreateUriWithFragment (urlmon.@)
5197 * Creates a new IUri object. This is almost the same as CreateUri, expect that
5198 * it allows you to explicitly specify a fragment (pwzFragment) for pwzURI.
5201 * pwzURI [I] The URI to parse and perform canonicalization on.
5202 * pwzFragment [I] The explicit fragment string which should be added to pwzURI.
5203 * dwFlags [I] The flags which will be passed to CreateUri.
5204 * dwReserved [I] Reserved (not used).
5205 * ppURI [O] The resulting IUri after parsing/canonicalization.
5208 * Success: S_OK. ppURI contains the pointer to the newly allocated IUri.
5209 * Failure: E_INVALIDARG if pwzURI already contains a fragment and pwzFragment
5210 * isn't NULL. Will also return E_INVALIDARG for the same reasons as
5211 * CreateUri will. E_OUTOFMEMORY if any allocation fails.
5213 HRESULT WINAPI CreateUriWithFragment(LPCWSTR pwzURI, LPCWSTR pwzFragment, DWORD dwFlags,
5214 DWORD_PTR dwReserved, IUri **ppURI)
5217 TRACE("(%s %s %x %x %p)\n", debugstr_w(pwzURI), debugstr_w(pwzFragment), dwFlags, (DWORD)dwReserved, ppURI);
5220 return E_INVALIDARG;
5224 return E_INVALIDARG;
5227 /* Check if a fragment should be appended to the URI string. */
5230 DWORD uri_len, frag_len;
5233 /* Check if the original URI already has a fragment component. */
5234 if(StrChrW(pwzURI, '#')) {
5236 return E_INVALIDARG;
5239 uri_len = lstrlenW(pwzURI);
5240 frag_len = lstrlenW(pwzFragment);
5242 /* If the fragment doesn't start with a '#', one will be added. */
5243 add_pound = *pwzFragment != '#';
5246 uriW = heap_alloc((uri_len+frag_len+2)*sizeof(WCHAR));
5248 uriW = heap_alloc((uri_len+frag_len+1)*sizeof(WCHAR));
5251 return E_OUTOFMEMORY;
5253 memcpy(uriW, pwzURI, uri_len*sizeof(WCHAR));
5255 uriW[uri_len++] = '#';
5256 memcpy(uriW+uri_len, pwzFragment, (frag_len+1)*sizeof(WCHAR));
5258 hres = CreateUri(uriW, dwFlags, 0, ppURI);
5262 /* A fragment string wasn't specified, so just forward the call. */
5263 hres = CreateUri(pwzURI, dwFlags, 0, ppURI);
5268 static HRESULT build_uri(const UriBuilder *builder, IUri **uri, DWORD create_flags,
5269 DWORD use_orig_flags, DWORD encoding_mask)
5278 if(encoding_mask && (!builder->uri || builder->modified_props)) {
5283 /* Decide what flags should be used when creating the Uri. */
5284 if((use_orig_flags & UriBuilder_USE_ORIGINAL_FLAGS) && builder->uri)
5285 create_flags = builder->uri->create_flags;
5287 if(has_invalid_flag_combination(create_flags)) {
5289 return E_INVALIDARG;
5292 /* Set the default flags if they don't cause a conflict. */
5293 apply_default_flags(&create_flags);
5296 /* Return the base IUri if no changes have been made and the create_flags match. */
5297 if(builder->uri && !builder->modified_props && builder->uri->create_flags == create_flags) {
5298 *uri = &builder->uri->IUri_iface;
5303 hr = validate_components(builder, &data, create_flags);
5309 ret = create_uri_obj();
5312 return E_OUTOFMEMORY;
5315 hr = generate_uri(builder, &data, ret, create_flags);
5317 IUri_Release(&ret->IUri_iface);
5322 *uri = &ret->IUri_iface;
5326 static inline UriBuilder* impl_from_IUriBuilder(IUriBuilder *iface)
5328 return CONTAINING_RECORD(iface, UriBuilder, IUriBuilder_iface);
5331 static HRESULT WINAPI UriBuilder_QueryInterface(IUriBuilder *iface, REFIID riid, void **ppv)
5333 UriBuilder *This = impl_from_IUriBuilder(iface);
5335 if(IsEqualGUID(&IID_IUnknown, riid)) {
5336 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
5337 *ppv = &This->IUriBuilder_iface;
5338 }else if(IsEqualGUID(&IID_IUriBuilder, riid)) {
5339 TRACE("(%p)->(IID_IUriBuilder %p)\n", This, ppv);
5340 *ppv = &This->IUriBuilder_iface;
5342 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
5344 return E_NOINTERFACE;
5347 IUnknown_AddRef((IUnknown*)*ppv);
5351 static ULONG WINAPI UriBuilder_AddRef(IUriBuilder *iface)
5353 UriBuilder *This = impl_from_IUriBuilder(iface);
5354 LONG ref = InterlockedIncrement(&This->ref);
5356 TRACE("(%p) ref=%d\n", This, ref);
5361 static ULONG WINAPI UriBuilder_Release(IUriBuilder *iface)
5363 UriBuilder *This = impl_from_IUriBuilder(iface);
5364 LONG ref = InterlockedDecrement(&This->ref);
5366 TRACE("(%p) ref=%d\n", This, ref);
5369 if(This->uri) IUri_Release(&This->uri->IUri_iface);
5370 heap_free(This->fragment);
5371 heap_free(This->host);
5372 heap_free(This->password);
5373 heap_free(This->path);
5374 heap_free(This->query);
5375 heap_free(This->scheme);
5376 heap_free(This->username);
5383 static HRESULT WINAPI UriBuilder_CreateUriSimple(IUriBuilder *iface,
5384 DWORD dwAllowEncodingPropertyMask,
5385 DWORD_PTR dwReserved,
5388 UriBuilder *This = impl_from_IUriBuilder(iface);
5390 TRACE("(%p)->(%d %d %p)\n", This, dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
5392 hr = build_uri(This, ppIUri, 0, UriBuilder_USE_ORIGINAL_FLAGS, dwAllowEncodingPropertyMask);
5394 FIXME("(%p)->(%d %d %p)\n", This, dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
5398 static HRESULT WINAPI UriBuilder_CreateUri(IUriBuilder *iface,
5399 DWORD dwCreateFlags,
5400 DWORD dwAllowEncodingPropertyMask,
5401 DWORD_PTR dwReserved,
5404 UriBuilder *This = impl_from_IUriBuilder(iface);
5406 TRACE("(%p)->(0x%08x %d %d %p)\n", This, dwCreateFlags, dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
5408 if(dwCreateFlags == -1)
5409 hr = build_uri(This, ppIUri, 0, UriBuilder_USE_ORIGINAL_FLAGS, dwAllowEncodingPropertyMask);
5411 hr = build_uri(This, ppIUri, dwCreateFlags, 0, dwAllowEncodingPropertyMask);
5414 FIXME("(%p)->(0x%08x %d %d %p)\n", This, dwCreateFlags, dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
5418 static HRESULT WINAPI UriBuilder_CreateUriWithFlags(IUriBuilder *iface,
5419 DWORD dwCreateFlags,
5420 DWORD dwUriBuilderFlags,
5421 DWORD dwAllowEncodingPropertyMask,
5422 DWORD_PTR dwReserved,
5425 UriBuilder *This = impl_from_IUriBuilder(iface);
5427 TRACE("(%p)->(0x%08x 0x%08x %d %d %p)\n", This, dwCreateFlags, dwUriBuilderFlags,
5428 dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
5430 hr = build_uri(This, ppIUri, dwCreateFlags, dwUriBuilderFlags, dwAllowEncodingPropertyMask);
5432 FIXME("(%p)->(0x%08x 0x%08x %d %d %p)\n", This, dwCreateFlags, dwUriBuilderFlags,
5433 dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
5437 static HRESULT WINAPI UriBuilder_GetIUri(IUriBuilder *iface, IUri **ppIUri)
5439 UriBuilder *This = impl_from_IUriBuilder(iface);
5440 TRACE("(%p)->(%p)\n", This, ppIUri);
5446 IUri *uri = &This->uri->IUri_iface;
5455 static HRESULT WINAPI UriBuilder_SetIUri(IUriBuilder *iface, IUri *pIUri)
5457 UriBuilder *This = impl_from_IUriBuilder(iface);
5458 TRACE("(%p)->(%p)\n", This, pIUri);
5463 if((uri = get_uri_obj(pIUri))) {
5464 /* Only reset the builder if it's Uri isn't the same as
5465 * the Uri passed to the function.
5467 if(This->uri != uri) {
5468 reset_builder(This);
5472 This->port = uri->port;
5477 FIXME("(%p)->(%p) Unknown IUri types not supported yet.\n", This, pIUri);
5480 } else if(This->uri)
5481 /* Only reset the builder if it's Uri isn't NULL. */
5482 reset_builder(This);
5487 static HRESULT WINAPI UriBuilder_GetFragment(IUriBuilder *iface, DWORD *pcchFragment, LPCWSTR *ppwzFragment)
5489 UriBuilder *This = impl_from_IUriBuilder(iface);
5490 TRACE("(%p)->(%p %p)\n", This, pcchFragment, ppwzFragment);
5492 if(!This->uri || This->uri->fragment_start == -1 || This->modified_props & Uri_HAS_FRAGMENT)
5493 return get_builder_component(&This->fragment, &This->fragment_len, NULL, 0, ppwzFragment, pcchFragment);
5495 return get_builder_component(&This->fragment, &This->fragment_len, This->uri->canon_uri+This->uri->fragment_start,
5496 This->uri->fragment_len, ppwzFragment, pcchFragment);
5499 static HRESULT WINAPI UriBuilder_GetHost(IUriBuilder *iface, DWORD *pcchHost, LPCWSTR *ppwzHost)
5501 UriBuilder *This = impl_from_IUriBuilder(iface);
5502 TRACE("(%p)->(%p %p)\n", This, pcchHost, ppwzHost);
5504 if(!This->uri || This->uri->host_start == -1 || This->modified_props & Uri_HAS_HOST)
5505 return get_builder_component(&This->host, &This->host_len, NULL, 0, ppwzHost, pcchHost);
5507 if(This->uri->host_type == Uri_HOST_IPV6)
5508 /* Don't include the '[' and ']' around the address. */
5509 return get_builder_component(&This->host, &This->host_len, This->uri->canon_uri+This->uri->host_start+1,
5510 This->uri->host_len-2, ppwzHost, pcchHost);
5512 return get_builder_component(&This->host, &This->host_len, This->uri->canon_uri+This->uri->host_start,
5513 This->uri->host_len, ppwzHost, pcchHost);
5517 static HRESULT WINAPI UriBuilder_GetPassword(IUriBuilder *iface, DWORD *pcchPassword, LPCWSTR *ppwzPassword)
5519 UriBuilder *This = impl_from_IUriBuilder(iface);
5520 TRACE("(%p)->(%p %p)\n", This, pcchPassword, ppwzPassword);
5522 if(!This->uri || This->uri->userinfo_split == -1 || This->modified_props & Uri_HAS_PASSWORD)
5523 return get_builder_component(&This->password, &This->password_len, NULL, 0, ppwzPassword, pcchPassword);
5525 const WCHAR *start = This->uri->canon_uri+This->uri->userinfo_start+This->uri->userinfo_split+1;
5526 DWORD len = This->uri->userinfo_len-This->uri->userinfo_split-1;
5527 return get_builder_component(&This->password, &This->password_len, start, len, ppwzPassword, pcchPassword);
5531 static HRESULT WINAPI UriBuilder_GetPath(IUriBuilder *iface, DWORD *pcchPath, LPCWSTR *ppwzPath)
5533 UriBuilder *This = impl_from_IUriBuilder(iface);
5534 TRACE("(%p)->(%p %p)\n", This, pcchPath, ppwzPath);
5536 if(!This->uri || This->uri->path_start == -1 || This->modified_props & Uri_HAS_PATH)
5537 return get_builder_component(&This->path, &This->path_len, NULL, 0, ppwzPath, pcchPath);
5539 return get_builder_component(&This->path, &This->path_len, This->uri->canon_uri+This->uri->path_start,
5540 This->uri->path_len, ppwzPath, pcchPath);
5543 static HRESULT WINAPI UriBuilder_GetPort(IUriBuilder *iface, BOOL *pfHasPort, DWORD *pdwPort)
5545 UriBuilder *This = impl_from_IUriBuilder(iface);
5546 TRACE("(%p)->(%p %p)\n", This, pfHasPort, pdwPort);
5559 *pfHasPort = This->has_port;
5560 *pdwPort = This->port;
5564 static HRESULT WINAPI UriBuilder_GetQuery(IUriBuilder *iface, DWORD *pcchQuery, LPCWSTR *ppwzQuery)
5566 UriBuilder *This = impl_from_IUriBuilder(iface);
5567 TRACE("(%p)->(%p %p)\n", This, pcchQuery, ppwzQuery);
5569 if(!This->uri || This->uri->query_start == -1 || This->modified_props & Uri_HAS_QUERY)
5570 return get_builder_component(&This->query, &This->query_len, NULL, 0, ppwzQuery, pcchQuery);
5572 return get_builder_component(&This->query, &This->query_len, This->uri->canon_uri+This->uri->query_start,
5573 This->uri->query_len, ppwzQuery, pcchQuery);
5576 static HRESULT WINAPI UriBuilder_GetSchemeName(IUriBuilder *iface, DWORD *pcchSchemeName, LPCWSTR *ppwzSchemeName)
5578 UriBuilder *This = impl_from_IUriBuilder(iface);
5579 TRACE("(%p)->(%p %p)\n", This, pcchSchemeName, ppwzSchemeName);
5581 if(!This->uri || This->uri->scheme_start == -1 || This->modified_props & Uri_HAS_SCHEME_NAME)
5582 return get_builder_component(&This->scheme, &This->scheme_len, NULL, 0, ppwzSchemeName, pcchSchemeName);
5584 return get_builder_component(&This->scheme, &This->scheme_len, This->uri->canon_uri+This->uri->scheme_start,
5585 This->uri->scheme_len, ppwzSchemeName, pcchSchemeName);
5588 static HRESULT WINAPI UriBuilder_GetUserName(IUriBuilder *iface, DWORD *pcchUserName, LPCWSTR *ppwzUserName)
5590 UriBuilder *This = impl_from_IUriBuilder(iface);
5591 TRACE("(%p)->(%p %p)\n", This, pcchUserName, ppwzUserName);
5593 if(!This->uri || This->uri->userinfo_start == -1 || This->uri->userinfo_split == 0 ||
5594 This->modified_props & Uri_HAS_USER_NAME)
5595 return get_builder_component(&This->username, &This->username_len, NULL, 0, ppwzUserName, pcchUserName);
5597 const WCHAR *start = This->uri->canon_uri+This->uri->userinfo_start;
5599 /* Check if there's a password in the userinfo section. */
5600 if(This->uri->userinfo_split > -1)
5601 /* Don't include the password. */
5602 return get_builder_component(&This->username, &This->username_len, start,
5603 This->uri->userinfo_split, ppwzUserName, pcchUserName);
5605 return get_builder_component(&This->username, &This->username_len, start,
5606 This->uri->userinfo_len, ppwzUserName, pcchUserName);
5610 static HRESULT WINAPI UriBuilder_SetFragment(IUriBuilder *iface, LPCWSTR pwzNewValue)
5612 UriBuilder *This = impl_from_IUriBuilder(iface);
5613 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
5614 return set_builder_component(&This->fragment, &This->fragment_len, pwzNewValue, '#',
5615 &This->modified_props, Uri_HAS_FRAGMENT);
5618 static HRESULT WINAPI UriBuilder_SetHost(IUriBuilder *iface, LPCWSTR pwzNewValue)
5620 UriBuilder *This = impl_from_IUriBuilder(iface);
5621 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
5623 /* Host name can't be set to NULL. */
5625 return E_INVALIDARG;
5627 return set_builder_component(&This->host, &This->host_len, pwzNewValue, 0,
5628 &This->modified_props, Uri_HAS_HOST);
5631 static HRESULT WINAPI UriBuilder_SetPassword(IUriBuilder *iface, LPCWSTR pwzNewValue)
5633 UriBuilder *This = impl_from_IUriBuilder(iface);
5634 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
5635 return set_builder_component(&This->password, &This->password_len, pwzNewValue, 0,
5636 &This->modified_props, Uri_HAS_PASSWORD);
5639 static HRESULT WINAPI UriBuilder_SetPath(IUriBuilder *iface, LPCWSTR pwzNewValue)
5641 UriBuilder *This = impl_from_IUriBuilder(iface);
5642 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
5643 return set_builder_component(&This->path, &This->path_len, pwzNewValue, 0,
5644 &This->modified_props, Uri_HAS_PATH);
5647 static HRESULT WINAPI UriBuilder_SetPort(IUriBuilder *iface, BOOL fHasPort, DWORD dwNewValue)
5649 UriBuilder *This = impl_from_IUriBuilder(iface);
5650 TRACE("(%p)->(%d %d)\n", This, fHasPort, dwNewValue);
5652 This->has_port = fHasPort;
5653 This->port = dwNewValue;
5654 This->modified_props |= Uri_HAS_PORT;
5658 static HRESULT WINAPI UriBuilder_SetQuery(IUriBuilder *iface, LPCWSTR pwzNewValue)
5660 UriBuilder *This = impl_from_IUriBuilder(iface);
5661 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
5662 return set_builder_component(&This->query, &This->query_len, pwzNewValue, '?',
5663 &This->modified_props, Uri_HAS_QUERY);
5666 static HRESULT WINAPI UriBuilder_SetSchemeName(IUriBuilder *iface, LPCWSTR pwzNewValue)
5668 UriBuilder *This = impl_from_IUriBuilder(iface);
5669 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
5671 /* Only set the scheme name if it's not NULL or empty. */
5672 if(!pwzNewValue || !*pwzNewValue)
5673 return E_INVALIDARG;
5675 return set_builder_component(&This->scheme, &This->scheme_len, pwzNewValue, 0,
5676 &This->modified_props, Uri_HAS_SCHEME_NAME);
5679 static HRESULT WINAPI UriBuilder_SetUserName(IUriBuilder *iface, LPCWSTR pwzNewValue)
5681 UriBuilder *This = impl_from_IUriBuilder(iface);
5682 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
5683 return set_builder_component(&This->username, &This->username_len, pwzNewValue, 0,
5684 &This->modified_props, Uri_HAS_USER_NAME);
5687 static HRESULT WINAPI UriBuilder_RemoveProperties(IUriBuilder *iface, DWORD dwPropertyMask)
5689 const DWORD accepted_flags = Uri_HAS_AUTHORITY|Uri_HAS_DOMAIN|Uri_HAS_EXTENSION|Uri_HAS_FRAGMENT|Uri_HAS_HOST|
5690 Uri_HAS_PASSWORD|Uri_HAS_PATH|Uri_HAS_PATH_AND_QUERY|Uri_HAS_QUERY|
5691 Uri_HAS_USER_INFO|Uri_HAS_USER_NAME;
5693 UriBuilder *This = impl_from_IUriBuilder(iface);
5694 TRACE("(%p)->(0x%08x)\n", This, dwPropertyMask);
5696 if(dwPropertyMask & ~accepted_flags)
5697 return E_INVALIDARG;
5699 if(dwPropertyMask & Uri_HAS_FRAGMENT)
5700 UriBuilder_SetFragment(iface, NULL);
5702 /* Even though you can't set the host name to NULL or an
5703 * empty string, you can still remove it... for some reason.
5705 if(dwPropertyMask & Uri_HAS_HOST)
5706 set_builder_component(&This->host, &This->host_len, NULL, 0,
5707 &This->modified_props, Uri_HAS_HOST);
5709 if(dwPropertyMask & Uri_HAS_PASSWORD)
5710 UriBuilder_SetPassword(iface, NULL);
5712 if(dwPropertyMask & Uri_HAS_PATH)
5713 UriBuilder_SetPath(iface, NULL);
5715 if(dwPropertyMask & Uri_HAS_PORT)
5716 UriBuilder_SetPort(iface, FALSE, 0);
5718 if(dwPropertyMask & Uri_HAS_QUERY)
5719 UriBuilder_SetQuery(iface, NULL);
5721 if(dwPropertyMask & Uri_HAS_USER_NAME)
5722 UriBuilder_SetUserName(iface, NULL);
5727 static HRESULT WINAPI UriBuilder_HasBeenModified(IUriBuilder *iface, BOOL *pfModified)
5729 UriBuilder *This = impl_from_IUriBuilder(iface);
5730 TRACE("(%p)->(%p)\n", This, pfModified);
5735 *pfModified = This->modified_props > 0;
5739 static const IUriBuilderVtbl UriBuilderVtbl = {
5740 UriBuilder_QueryInterface,
5743 UriBuilder_CreateUriSimple,
5744 UriBuilder_CreateUri,
5745 UriBuilder_CreateUriWithFlags,
5748 UriBuilder_GetFragment,
5750 UriBuilder_GetPassword,
5753 UriBuilder_GetQuery,
5754 UriBuilder_GetSchemeName,
5755 UriBuilder_GetUserName,
5756 UriBuilder_SetFragment,
5758 UriBuilder_SetPassword,
5761 UriBuilder_SetQuery,
5762 UriBuilder_SetSchemeName,
5763 UriBuilder_SetUserName,
5764 UriBuilder_RemoveProperties,
5765 UriBuilder_HasBeenModified,
5768 /***********************************************************************
5769 * CreateIUriBuilder (urlmon.@)
5771 HRESULT WINAPI CreateIUriBuilder(IUri *pIUri, DWORD dwFlags, DWORD_PTR dwReserved, IUriBuilder **ppIUriBuilder)
5775 TRACE("(%p %x %x %p)\n", pIUri, dwFlags, (DWORD)dwReserved, ppIUriBuilder);
5780 ret = heap_alloc_zero(sizeof(UriBuilder));
5782 return E_OUTOFMEMORY;
5784 ret->IUriBuilder_iface.lpVtbl = &UriBuilderVtbl;
5790 if((uri = get_uri_obj(pIUri))) {
5795 /* Windows doesn't set 'has_port' to TRUE in this case. */
5796 ret->port = uri->port;
5800 *ppIUriBuilder = NULL;
5801 FIXME("(%p %x %x %p): Unknown IUri types not supported yet.\n", pIUri, dwFlags,
5802 (DWORD)dwReserved, ppIUriBuilder);
5807 *ppIUriBuilder = &ret->IUriBuilder_iface;
5811 /* Merges the base path with the relative path and stores the resulting path
5812 * and path len in 'result' and 'result_len'.
5814 static HRESULT merge_paths(parse_data *data, const WCHAR *base, DWORD base_len, const WCHAR *relative,
5815 DWORD relative_len, WCHAR **result, DWORD *result_len, DWORD flags)
5817 const WCHAR *end = NULL;
5818 DWORD base_copy_len = 0;
5822 /* Find the characters that will be copied over from
5825 end = memrchrW(base, '/', base_len);
5826 if(!end && data->scheme_type == URL_SCHEME_FILE)
5827 /* Try looking for a '\\'. */
5828 end = memrchrW(base, '\\', base_len);
5832 base_copy_len = (end+1)-base;
5833 *result = heap_alloc((base_copy_len+relative_len+1)*sizeof(WCHAR));
5835 *result = heap_alloc((relative_len+1)*sizeof(WCHAR));
5839 return E_OUTOFMEMORY;
5844 memcpy(ptr, base, base_copy_len*sizeof(WCHAR));
5845 ptr += base_copy_len;
5848 memcpy(ptr, relative, relative_len*sizeof(WCHAR));
5849 ptr += relative_len;
5852 *result_len = (ptr-*result);
5856 static HRESULT combine_uri(Uri *base, Uri *relative, DWORD flags, IUri **result, DWORD extras) {
5860 DWORD create_flags = 0, len = 0;
5862 memset(&data, 0, sizeof(parse_data));
5864 /* Base case is when the relative Uri has a scheme name,
5865 * if it does, then 'result' will contain the same data
5866 * as the relative Uri.
5868 if(relative->scheme_start > -1) {
5869 data.uri = SysAllocString(relative->raw_uri);
5872 return E_OUTOFMEMORY;
5875 parse_uri(&data, 0);
5877 ret = create_uri_obj();
5880 return E_OUTOFMEMORY;
5883 if(extras & COMBINE_URI_FORCE_FLAG_USE) {
5884 if(flags & URL_DONT_SIMPLIFY)
5885 create_flags |= Uri_CREATE_NO_CANONICALIZE;
5886 if(flags & URL_DONT_UNESCAPE_EXTRA_INFO)
5887 create_flags |= Uri_CREATE_NO_DECODE_EXTRA_INFO;
5890 ret->raw_uri = data.uri;
5891 hr = canonicalize_uri(&data, ret, create_flags);
5893 IUri_Release(&ret->IUri_iface);
5898 apply_default_flags(&create_flags);
5899 ret->create_flags = create_flags;
5901 *result = &ret->IUri_iface;
5904 DWORD raw_flags = 0;
5906 if(base->scheme_start > -1) {
5907 data.scheme = base->canon_uri+base->scheme_start;
5908 data.scheme_len = base->scheme_len;
5909 data.scheme_type = base->scheme_type;
5911 data.is_relative = TRUE;
5912 data.scheme_type = URL_SCHEME_UNKNOWN;
5913 create_flags |= Uri_CREATE_ALLOW_RELATIVE;
5916 if(base->authority_start > -1) {
5917 if(base->userinfo_start > -1 && base->userinfo_split != 0) {
5918 data.username = base->canon_uri+base->userinfo_start;
5919 data.username_len = (base->userinfo_split > -1) ? base->userinfo_split : base->userinfo_len;
5922 if(base->userinfo_split > -1) {
5923 data.password = base->canon_uri+base->userinfo_start+base->userinfo_split+1;
5924 data.password_len = base->userinfo_len-base->userinfo_split-1;
5927 if(base->host_start > -1) {
5928 data.host = base->canon_uri+base->host_start;
5929 data.host_len = base->host_len;
5930 data.host_type = base->host_type;
5933 if(base->has_port) {
5934 data.has_port = TRUE;
5935 data.port_value = base->port;
5937 } else if(base->scheme_type != URL_SCHEME_FILE)
5938 data.is_opaque = TRUE;
5940 if(relative->path_start == -1 || !relative->path_len) {
5941 if(base->path_start > -1) {
5942 data.path = base->canon_uri+base->path_start;
5943 data.path_len = base->path_len;
5944 } else if((base->path_start == -1 || !base->path_len) && !data.is_opaque) {
5945 /* Just set the path as a '/' if the base didn't have
5946 * one and if it's an hierarchical URI.
5948 static const WCHAR slashW[] = {'/',0};
5953 if(relative->query_start > -1) {
5954 data.query = relative->canon_uri+relative->query_start;
5955 data.query_len = relative->query_len;
5956 } else if(base->query_start > -1) {
5957 data.query = base->canon_uri+base->query_start;
5958 data.query_len = base->query_len;
5961 const WCHAR *ptr, **pptr;
5962 DWORD path_offset = 0, path_len = 0;
5964 /* There's two possibilities on what will happen to the path component
5965 * of the result IUri. First, if the relative path begins with a '/'
5966 * then the resulting path will just be the relative path. Second, if
5967 * relative path doesn't begin with a '/' then the base path and relative
5968 * path are merged together.
5970 if(relative->path_len && *(relative->canon_uri+relative->path_start) == '/') {
5972 BOOL copy_drive_path = FALSE;
5974 /* If the relative IUri's path starts with a '/', then we
5975 * don't use the base IUri's path. Unless the base IUri
5976 * is a file URI, in which case it uses the drive path of
5977 * the base IUri (if it has any) in the new path.
5979 if(base->scheme_type == URL_SCHEME_FILE) {
5980 if(base->path_len > 3 && *(base->canon_uri+base->path_start) == '/' &&
5981 is_drive_path(base->canon_uri+base->path_start+1)) {
5983 copy_drive_path = TRUE;
5987 path_len += relative->path_len;
5989 path = heap_alloc((path_len+1)*sizeof(WCHAR));
5992 return E_OUTOFMEMORY;
5997 /* Copy the base paths, drive path over. */
5998 if(copy_drive_path) {
5999 memcpy(tmp, base->canon_uri+base->path_start, 3*sizeof(WCHAR));
6003 memcpy(tmp, relative->canon_uri+relative->path_start, relative->path_len*sizeof(WCHAR));
6004 path[path_len] = '\0';
6006 /* Merge the base path with the relative path. */
6007 hr = merge_paths(&data, base->canon_uri+base->path_start, base->path_len,
6008 relative->canon_uri+relative->path_start, relative->path_len,
6009 &path, &path_len, flags);
6015 /* If the resulting IUri is a file URI, the drive path isn't
6016 * reduced out when the dot segments are removed.
6018 if(path_len >= 3 && data.scheme_type == URL_SCHEME_FILE && !data.host) {
6019 if(*path == '/' && is_drive_path(path+1))
6021 else if(is_drive_path(path))
6026 /* Check if the dot segments need to be removed from the path. */
6027 if(!(flags & URL_DONT_SIMPLIFY) && !data.is_opaque) {
6028 DWORD offset = (path_offset > 0) ? path_offset+1 : 0;
6029 DWORD new_len = remove_dot_segments(path+offset,path_len-offset);
6031 if(new_len != path_len) {
6032 WCHAR *tmp = heap_realloc(path, (offset+new_len+1)*sizeof(WCHAR));
6036 return E_OUTOFMEMORY;
6039 tmp[new_len+offset] = '\0';
6041 path_len = new_len+offset;
6045 if(relative->query_start > -1) {
6046 data.query = relative->canon_uri+relative->query_start;
6047 data.query_len = relative->query_len;
6050 /* Make sure the path component is valid. */
6053 if((data.is_opaque && !parse_path_opaque(pptr, &data, 0)) ||
6054 (!data.is_opaque && !parse_path_hierarchical(pptr, &data, 0))) {
6057 return E_INVALIDARG;
6061 if(relative->fragment_start > -1) {
6062 data.fragment = relative->canon_uri+relative->fragment_start;
6063 data.fragment_len = relative->fragment_len;
6066 if(flags & URL_DONT_SIMPLIFY)
6067 raw_flags |= RAW_URI_FORCE_PORT_DISP;
6068 if(flags & URL_FILE_USE_PATHURL)
6069 raw_flags |= RAW_URI_CONVERT_TO_DOS_PATH;
6071 len = generate_raw_uri(&data, data.uri, raw_flags);
6072 data.uri = SysAllocStringLen(NULL, len);
6076 return E_OUTOFMEMORY;
6079 generate_raw_uri(&data, data.uri, raw_flags);
6081 ret = create_uri_obj();
6083 SysFreeString(data.uri);
6086 return E_OUTOFMEMORY;
6089 if(flags & URL_DONT_SIMPLIFY)
6090 create_flags |= Uri_CREATE_NO_CANONICALIZE;
6091 if(flags & URL_FILE_USE_PATHURL)
6092 create_flags |= Uri_CREATE_FILE_USE_DOS_PATH;
6094 ret->raw_uri = data.uri;
6095 hr = canonicalize_uri(&data, ret, create_flags);
6097 IUri_Release(&ret->IUri_iface);
6102 if(flags & URL_DONT_SIMPLIFY)
6103 ret->display_modifiers |= URI_DISPLAY_NO_DEFAULT_PORT_AUTH;
6105 apply_default_flags(&create_flags);
6106 ret->create_flags = create_flags;
6107 *result = &ret->IUri_iface;
6115 /***********************************************************************
6116 * CoInternetCombineIUri (urlmon.@)
6118 HRESULT WINAPI CoInternetCombineIUri(IUri *pBaseUri, IUri *pRelativeUri, DWORD dwCombineFlags,
6119 IUri **ppCombinedUri, DWORD_PTR dwReserved)
6122 IInternetProtocolInfo *info;
6123 Uri *relative, *base;
6124 TRACE("(%p %p %x %p %x)\n", pBaseUri, pRelativeUri, dwCombineFlags, ppCombinedUri, (DWORD)dwReserved);
6127 return E_INVALIDARG;
6129 if(!pBaseUri || !pRelativeUri) {
6130 *ppCombinedUri = NULL;
6131 return E_INVALIDARG;
6134 relative = get_uri_obj(pRelativeUri);
6135 base = get_uri_obj(pBaseUri);
6136 if(!relative || !base) {
6137 *ppCombinedUri = NULL;
6138 FIXME("(%p %p %x %p %x) Unknown IUri types not supported yet.\n",
6139 pBaseUri, pRelativeUri, dwCombineFlags, ppCombinedUri, (DWORD)dwReserved);
6143 info = get_protocol_info(base->canon_uri);
6145 WCHAR result[INTERNET_MAX_URL_LENGTH+1];
6146 DWORD result_len = 0;
6148 hr = IInternetProtocolInfo_CombineUrl(info, base->canon_uri, relative->canon_uri, dwCombineFlags,
6149 result, INTERNET_MAX_URL_LENGTH+1, &result_len, 0);
6150 IInternetProtocolInfo_Release(info);
6152 hr = CreateUri(result, Uri_CREATE_ALLOW_RELATIVE, 0, ppCombinedUri);
6158 return combine_uri(base, relative, dwCombineFlags, ppCombinedUri, 0);
6161 /***********************************************************************
6162 * CoInternetCombineUrlEx (urlmon.@)
6164 HRESULT WINAPI CoInternetCombineUrlEx(IUri *pBaseUri, LPCWSTR pwzRelativeUrl, DWORD dwCombineFlags,
6165 IUri **ppCombinedUri, DWORD_PTR dwReserved)
6170 IInternetProtocolInfo *info;
6172 TRACE("(%p %s %x %p %x) stub\n", pBaseUri, debugstr_w(pwzRelativeUrl), dwCombineFlags,
6173 ppCombinedUri, (DWORD)dwReserved);
6178 if(!pwzRelativeUrl) {
6179 *ppCombinedUri = NULL;
6180 return E_UNEXPECTED;
6184 *ppCombinedUri = NULL;
6185 return E_INVALIDARG;
6188 base = get_uri_obj(pBaseUri);
6190 *ppCombinedUri = NULL;
6191 FIXME("(%p %s %x %p %x) Unknown IUri's not supported yet.\n", pBaseUri, debugstr_w(pwzRelativeUrl),
6192 dwCombineFlags, ppCombinedUri, (DWORD)dwReserved);
6196 info = get_protocol_info(base->canon_uri);
6198 WCHAR result[INTERNET_MAX_URL_LENGTH+1];
6199 DWORD result_len = 0;
6201 hr = IInternetProtocolInfo_CombineUrl(info, base->canon_uri, pwzRelativeUrl, dwCombineFlags,
6202 result, INTERNET_MAX_URL_LENGTH+1, &result_len, 0);
6203 IInternetProtocolInfo_Release(info);
6205 hr = CreateUri(result, Uri_CREATE_ALLOW_RELATIVE, 0, ppCombinedUri);
6211 hr = CreateUri(pwzRelativeUrl, Uri_CREATE_ALLOW_RELATIVE, 0, &relative);
6213 *ppCombinedUri = NULL;
6217 hr = combine_uri(base, get_uri_obj(relative), dwCombineFlags, ppCombinedUri, COMBINE_URI_FORCE_FLAG_USE);
6219 IUri_Release(relative);
6223 static HRESULT parse_canonicalize(const Uri *uri, DWORD flags, LPWSTR output,
6224 DWORD output_len, DWORD *result_len)
6226 const WCHAR *ptr = NULL;
6229 WCHAR buffer[INTERNET_MAX_URL_LENGTH+1];
6233 /* URL_UNESCAPE only has effect if none of the URL_ESCAPE flags are set. */
6234 const BOOL allow_unescape = !(flags & URL_ESCAPE_UNSAFE) &&
6235 !(flags & URL_ESCAPE_SPACES_ONLY) &&
6236 !(flags & URL_ESCAPE_PERCENT);
6239 /* Check if the dot segments need to be removed from the
6242 if(uri->scheme_start > -1 && uri->path_start > -1) {
6243 ptr = uri->canon_uri+uri->scheme_start+uri->scheme_len+1;
6246 reduce_path = !(flags & URL_NO_META) &&
6247 !(flags & URL_DONT_SIMPLIFY) &&
6248 ptr && check_hierarchical(pptr);
6250 for(ptr = uri->canon_uri; ptr < uri->canon_uri+uri->canon_len; ++ptr) {
6251 BOOL do_default_action = TRUE;
6253 /* Keep track of the path if we need to remove dot segments from
6256 if(reduce_path && !path && ptr == uri->canon_uri+uri->path_start)
6259 /* Check if it's time to reduce the path. */
6260 if(reduce_path && ptr == uri->canon_uri+uri->path_start+uri->path_len) {
6261 DWORD current_path_len = (buffer+len) - path;
6262 DWORD new_path_len = remove_dot_segments(path, current_path_len);
6264 /* Update the current length. */
6265 len -= (current_path_len-new_path_len);
6266 reduce_path = FALSE;
6270 const WCHAR decoded = decode_pct_val(ptr);
6272 if(allow_unescape && (flags & URL_UNESCAPE)) {
6273 buffer[len++] = decoded;
6275 do_default_action = FALSE;
6279 /* See if %'s needed to encoded. */
6280 if(do_default_action && (flags & URL_ESCAPE_PERCENT)) {
6281 pct_encode_val(*ptr, buffer+len);
6283 do_default_action = FALSE;
6285 } else if(*ptr == ' ') {
6286 if((flags & URL_ESCAPE_SPACES_ONLY) &&
6287 !(flags & URL_ESCAPE_UNSAFE)) {
6288 pct_encode_val(*ptr, buffer+len);
6290 do_default_action = FALSE;
6292 } else if(!is_reserved(*ptr) && !is_unreserved(*ptr)) {
6293 if(flags & URL_ESCAPE_UNSAFE) {
6294 pct_encode_val(*ptr, buffer+len);
6296 do_default_action = FALSE;
6300 if(do_default_action)
6301 buffer[len++] = *ptr;
6304 /* Sometimes the path is the very last component of the IUri, so
6305 * see if the dot segments need to be reduced now.
6307 if(reduce_path && path) {
6308 DWORD current_path_len = (buffer+len) - path;
6309 DWORD new_path_len = remove_dot_segments(path, current_path_len);
6311 /* Update the current length. */
6312 len -= (current_path_len-new_path_len);
6317 /* The null terminator isn't included in the length. */
6318 *result_len = len-1;
6319 if(len > output_len)
6320 return STRSAFE_E_INSUFFICIENT_BUFFER;
6322 memcpy(output, buffer, len*sizeof(WCHAR));
6327 static HRESULT parse_friendly(IUri *uri, LPWSTR output, DWORD output_len,
6334 hr = IUri_GetPropertyLength(uri, Uri_PROPERTY_DISPLAY_URI, &display_len, 0);
6340 *result_len = display_len;
6341 if(display_len+1 > output_len)
6342 return STRSAFE_E_INSUFFICIENT_BUFFER;
6344 hr = IUri_GetDisplayUri(uri, &display);
6350 memcpy(output, display, (display_len+1)*sizeof(WCHAR));
6351 SysFreeString(display);
6355 static HRESULT parse_rootdocument(const Uri *uri, LPWSTR output, DWORD output_len,
6358 static const WCHAR colon_slashesW[] = {':','/','/'};
6363 /* Windows only returns the root document if the URI has an authority
6364 * and it's not an unknown scheme type or a file scheme type.
6366 if(uri->authority_start == -1 ||
6367 uri->scheme_type == URL_SCHEME_UNKNOWN ||
6368 uri->scheme_type == URL_SCHEME_FILE) {
6371 return STRSAFE_E_INSUFFICIENT_BUFFER;
6377 len = uri->scheme_len+uri->authority_len;
6378 /* For the "://" and '/' which will be added. */
6381 if(len+1 > output_len) {
6383 return STRSAFE_E_INSUFFICIENT_BUFFER;
6387 memcpy(ptr, uri->canon_uri+uri->scheme_start, uri->scheme_len*sizeof(WCHAR));
6389 /* Add the "://". */
6390 ptr += uri->scheme_len;
6391 memcpy(ptr, colon_slashesW, sizeof(colon_slashesW));
6393 /* Add the authority. */
6394 ptr += sizeof(colon_slashesW)/sizeof(WCHAR);
6395 memcpy(ptr, uri->canon_uri+uri->authority_start, uri->authority_len*sizeof(WCHAR));
6397 /* Add the '/' after the authority. */
6398 ptr += uri->authority_len;
6406 static HRESULT parse_document(const Uri *uri, LPWSTR output, DWORD output_len,
6411 /* It has to be a known scheme type, but, it can't be a file
6412 * scheme. It also has to hierarchical.
6414 if(uri->scheme_type == URL_SCHEME_UNKNOWN ||
6415 uri->scheme_type == URL_SCHEME_FILE ||
6416 uri->authority_start == -1) {
6419 return STRSAFE_E_INSUFFICIENT_BUFFER;
6425 if(uri->fragment_start > -1)
6426 len = uri->fragment_start;
6428 len = uri->canon_len;
6431 if(len+1 > output_len)
6432 return STRSAFE_E_INSUFFICIENT_BUFFER;
6434 memcpy(output, uri->canon_uri, len*sizeof(WCHAR));
6439 static HRESULT parse_path_from_url(const Uri *uri, LPWSTR output, DWORD output_len,
6442 const WCHAR *path_ptr;
6443 WCHAR buffer[INTERNET_MAX_URL_LENGTH+1];
6446 if(uri->scheme_type != URL_SCHEME_FILE) {
6450 return E_INVALIDARG;
6454 if(uri->host_start > -1) {
6455 static const WCHAR slash_slashW[] = {'\\','\\'};
6457 memcpy(ptr, slash_slashW, sizeof(slash_slashW));
6458 ptr += sizeof(slash_slashW)/sizeof(WCHAR);
6459 memcpy(ptr, uri->canon_uri+uri->host_start, uri->host_len*sizeof(WCHAR));
6460 ptr += uri->host_len;
6463 path_ptr = uri->canon_uri+uri->path_start;
6464 if(uri->path_len > 3 && *path_ptr == '/' && is_drive_path(path_ptr+1))
6465 /* Skip past the '/' in front of the drive path. */
6468 for(; path_ptr < uri->canon_uri+uri->path_start+uri->path_len; ++path_ptr, ++ptr) {
6469 BOOL do_default_action = TRUE;
6471 if(*path_ptr == '%') {
6472 const WCHAR decoded = decode_pct_val(path_ptr);
6476 do_default_action = FALSE;
6478 } else if(*path_ptr == '/') {
6480 do_default_action = FALSE;
6483 if(do_default_action)
6489 *result_len = ptr-buffer;
6490 if(*result_len+1 > output_len)
6491 return STRSAFE_E_INSUFFICIENT_BUFFER;
6493 memcpy(output, buffer, (*result_len+1)*sizeof(WCHAR));
6497 static HRESULT parse_url_from_path(IUri *uri, LPWSTR output, DWORD output_len,
6504 hr = IUri_GetPropertyLength(uri, Uri_PROPERTY_ABSOLUTE_URI, &len, 0);
6511 if(len+1 > output_len)
6512 return STRSAFE_E_INSUFFICIENT_BUFFER;
6514 hr = IUri_GetAbsoluteUri(uri, &received);
6520 memcpy(output, received, (len+1)*sizeof(WCHAR));
6521 SysFreeString(received);
6526 static HRESULT parse_schema(IUri *uri, LPWSTR output, DWORD output_len,
6533 hr = IUri_GetPropertyLength(uri, Uri_PROPERTY_SCHEME_NAME, &len, 0);
6540 if(len+1 > output_len)
6541 return STRSAFE_E_INSUFFICIENT_BUFFER;
6543 hr = IUri_GetSchemeName(uri, &received);
6549 memcpy(output, received, (len+1)*sizeof(WCHAR));
6550 SysFreeString(received);
6555 static HRESULT parse_site(IUri *uri, LPWSTR output, DWORD output_len, DWORD *result_len)
6561 hr = IUri_GetPropertyLength(uri, Uri_PROPERTY_HOST, &len, 0);
6568 if(len+1 > output_len)
6569 return STRSAFE_E_INSUFFICIENT_BUFFER;
6571 hr = IUri_GetHost(uri, &received);
6577 memcpy(output, received, (len+1)*sizeof(WCHAR));
6578 SysFreeString(received);
6583 static HRESULT parse_domain(IUri *uri, LPWSTR output, DWORD output_len, DWORD *result_len)
6589 hr = IUri_GetPropertyLength(uri, Uri_PROPERTY_DOMAIN, &len, 0);
6596 if(len+1 > output_len)
6597 return STRSAFE_E_INSUFFICIENT_BUFFER;
6599 hr = IUri_GetDomain(uri, &received);
6605 memcpy(output, received, (len+1)*sizeof(WCHAR));
6606 SysFreeString(received);
6611 static HRESULT parse_anchor(IUri *uri, LPWSTR output, DWORD output_len, DWORD *result_len)
6617 hr = IUri_GetPropertyLength(uri, Uri_PROPERTY_FRAGMENT, &len, 0);
6624 if(len+1 > output_len)
6625 return STRSAFE_E_INSUFFICIENT_BUFFER;
6627 hr = IUri_GetFragment(uri, &received);
6633 memcpy(output, received, (len+1)*sizeof(WCHAR));
6634 SysFreeString(received);
6639 /***********************************************************************
6640 * CoInternetParseIUri (urlmon.@)
6642 HRESULT WINAPI CoInternetParseIUri(IUri *pIUri, PARSEACTION ParseAction, DWORD dwFlags,
6643 LPWSTR pwzResult, DWORD cchResult, DWORD *pcchResult,
6644 DWORD_PTR dwReserved)
6648 IInternetProtocolInfo *info;
6650 TRACE("(%p %d %x %p %d %p %x)\n", pIUri, ParseAction, dwFlags, pwzResult,
6651 cchResult, pcchResult, (DWORD)dwReserved);
6656 if(!pwzResult || !pIUri) {
6658 return E_INVALIDARG;
6661 if(!(uri = get_uri_obj(pIUri))) {
6663 FIXME("(%p %d %x %p %d %p %x) Unknown IUri's not supported for this action.\n",
6664 pIUri, ParseAction, dwFlags, pwzResult, cchResult, pcchResult, (DWORD)dwReserved);
6668 info = get_protocol_info(uri->canon_uri);
6670 hr = IInternetProtocolInfo_ParseUrl(info, uri->canon_uri, ParseAction, dwFlags,
6671 pwzResult, cchResult, pcchResult, 0);
6672 IInternetProtocolInfo_Release(info);
6673 if(SUCCEEDED(hr)) return hr;
6676 switch(ParseAction) {
6677 case PARSE_CANONICALIZE:
6678 hr = parse_canonicalize(uri, dwFlags, pwzResult, cchResult, pcchResult);
6680 case PARSE_FRIENDLY:
6681 hr = parse_friendly(pIUri, pwzResult, cchResult, pcchResult);
6683 case PARSE_ROOTDOCUMENT:
6684 hr = parse_rootdocument(uri, pwzResult, cchResult, pcchResult);
6686 case PARSE_DOCUMENT:
6687 hr = parse_document(uri, pwzResult, cchResult, pcchResult);
6689 case PARSE_PATH_FROM_URL:
6690 hr = parse_path_from_url(uri, pwzResult, cchResult, pcchResult);
6692 case PARSE_URL_FROM_PATH:
6693 hr = parse_url_from_path(pIUri, pwzResult, cchResult, pcchResult);
6696 hr = parse_schema(pIUri, pwzResult, cchResult, pcchResult);
6699 hr = parse_site(pIUri, pwzResult, cchResult, pcchResult);
6702 hr = parse_domain(pIUri, pwzResult, cchResult, pcchResult);
6704 case PARSE_LOCATION:
6706 hr = parse_anchor(pIUri, pwzResult, cchResult, pcchResult);
6708 case PARSE_SECURITY_URL:
6711 case PARSE_SECURITY_DOMAIN:
6718 FIXME("(%p %d %x %p %d %p %x) Partial stub.\n", pIUri, ParseAction, dwFlags,
6719 pwzResult, cchResult, pcchResult, (DWORD)dwReserved);