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;
161 URL_SCHEME scheme_type;
163 const WCHAR *username;
166 const WCHAR *password;
171 Uri_HOST_TYPE host_type;
174 ipv6_address ipv6_address;
187 const WCHAR *fragment;
191 static const CHAR hexDigits[] = "0123456789ABCDEF";
193 /* List of scheme types/scheme names that are recognized by the IUri interface as of IE 7. */
194 static const struct {
196 WCHAR scheme_name[16];
197 } recognized_schemes[] = {
198 {URL_SCHEME_FTP, {'f','t','p',0}},
199 {URL_SCHEME_HTTP, {'h','t','t','p',0}},
200 {URL_SCHEME_GOPHER, {'g','o','p','h','e','r',0}},
201 {URL_SCHEME_MAILTO, {'m','a','i','l','t','o',0}},
202 {URL_SCHEME_NEWS, {'n','e','w','s',0}},
203 {URL_SCHEME_NNTP, {'n','n','t','p',0}},
204 {URL_SCHEME_TELNET, {'t','e','l','n','e','t',0}},
205 {URL_SCHEME_WAIS, {'w','a','i','s',0}},
206 {URL_SCHEME_FILE, {'f','i','l','e',0}},
207 {URL_SCHEME_MK, {'m','k',0}},
208 {URL_SCHEME_HTTPS, {'h','t','t','p','s',0}},
209 {URL_SCHEME_SHELL, {'s','h','e','l','l',0}},
210 {URL_SCHEME_SNEWS, {'s','n','e','w','s',0}},
211 {URL_SCHEME_LOCAL, {'l','o','c','a','l',0}},
212 {URL_SCHEME_JAVASCRIPT, {'j','a','v','a','s','c','r','i','p','t',0}},
213 {URL_SCHEME_VBSCRIPT, {'v','b','s','c','r','i','p','t',0}},
214 {URL_SCHEME_ABOUT, {'a','b','o','u','t',0}},
215 {URL_SCHEME_RES, {'r','e','s',0}},
216 {URL_SCHEME_MSSHELLROOTED, {'m','s','-','s','h','e','l','l','-','r','o','o','t','e','d',0}},
217 {URL_SCHEME_MSSHELLIDLIST, {'m','s','-','s','h','e','l','l','-','i','d','l','i','s','t',0}},
218 {URL_SCHEME_MSHELP, {'h','c','p',0}},
219 {URL_SCHEME_WILDCARD, {'*',0}}
222 /* List of default ports Windows recognizes. */
223 static const struct {
226 } default_ports[] = {
227 {URL_SCHEME_FTP, 21},
228 {URL_SCHEME_HTTP, 80},
229 {URL_SCHEME_GOPHER, 70},
230 {URL_SCHEME_NNTP, 119},
231 {URL_SCHEME_TELNET, 23},
232 {URL_SCHEME_WAIS, 210},
233 {URL_SCHEME_HTTPS, 443},
236 /* List of 3 character top level domain names Windows seems to recognize.
237 * There might be more, but, these are the only ones I've found so far.
239 static const struct {
241 } recognized_tlds[] = {
251 static Uri *get_uri_obj(IUri *uri)
256 hres = IUri_QueryInterface(uri, &IID_IUriObj, (void**)&ret);
257 return SUCCEEDED(hres) ? ret : NULL;
260 static inline BOOL is_alpha(WCHAR val) {
261 return ((val >= 'a' && val <= 'z') || (val >= 'A' && val <= 'Z'));
264 static inline BOOL is_num(WCHAR val) {
265 return (val >= '0' && val <= '9');
268 static inline BOOL is_drive_path(const WCHAR *str) {
269 return (is_alpha(str[0]) && (str[1] == ':' || str[1] == '|'));
272 static inline BOOL is_unc_path(const WCHAR *str) {
273 return (str[0] == '\\' && str[0] == '\\');
276 static inline BOOL is_forbidden_dos_path_char(WCHAR val) {
277 return (val == '>' || val == '<' || val == '\"');
280 /* A URI is implicitly a file path if it begins with
281 * a drive letter (eg X:) or starts with "\\" (UNC path).
283 static inline BOOL is_implicit_file_path(const WCHAR *str) {
284 return (is_unc_path(str) || (is_alpha(str[0]) && str[1] == ':'));
287 /* Checks if the URI is a hierarchical URI. A hierarchical
288 * URI is one that has "//" after the scheme.
290 static BOOL check_hierarchical(const WCHAR **ptr) {
291 const WCHAR *start = *ptr;
306 /* unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~" */
307 static inline BOOL is_unreserved(WCHAR val) {
308 return (is_alpha(val) || is_num(val) || val == '-' || val == '.' ||
309 val == '_' || val == '~');
312 /* sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
313 * / "*" / "+" / "," / ";" / "="
315 static inline BOOL is_subdelim(WCHAR val) {
316 return (val == '!' || val == '$' || val == '&' ||
317 val == '\'' || val == '(' || val == ')' ||
318 val == '*' || val == '+' || val == ',' ||
319 val == ';' || val == '=');
322 /* gen-delims = ":" / "/" / "?" / "#" / "[" / "]" / "@" */
323 static inline BOOL is_gendelim(WCHAR val) {
324 return (val == ':' || val == '/' || val == '?' ||
325 val == '#' || val == '[' || val == ']' ||
329 /* Characters that delimit the end of the authority
330 * section of a URI. Sometimes a '\\' is considered
331 * an authority delimeter.
333 static inline BOOL is_auth_delim(WCHAR val, BOOL acceptSlash) {
334 return (val == '#' || val == '/' || val == '?' ||
335 val == '\0' || (acceptSlash && val == '\\'));
338 /* reserved = gen-delims / sub-delims */
339 static inline BOOL is_reserved(WCHAR val) {
340 return (is_subdelim(val) || is_gendelim(val));
343 static inline BOOL is_hexdigit(WCHAR val) {
344 return ((val >= 'a' && val <= 'f') ||
345 (val >= 'A' && val <= 'F') ||
346 (val >= '0' && val <= '9'));
349 static inline BOOL is_path_delim(WCHAR val) {
350 return (!val || val == '#' || val == '?');
353 static inline BOOL is_slash(WCHAR c)
355 return c == '/' || c == '\\';
358 static BOOL is_default_port(URL_SCHEME scheme, DWORD port) {
361 for(i = 0; i < sizeof(default_ports)/sizeof(default_ports[0]); ++i) {
362 if(default_ports[i].scheme == scheme && default_ports[i].port)
369 /* List of schemes types Windows seems to expect to be hierarchical. */
370 static inline BOOL is_hierarchical_scheme(URL_SCHEME type) {
371 return(type == URL_SCHEME_HTTP || type == URL_SCHEME_FTP ||
372 type == URL_SCHEME_GOPHER || type == URL_SCHEME_NNTP ||
373 type == URL_SCHEME_TELNET || type == URL_SCHEME_WAIS ||
374 type == URL_SCHEME_FILE || type == URL_SCHEME_HTTPS ||
375 type == URL_SCHEME_RES);
378 /* Checks if 'flags' contains an invalid combination of Uri_CREATE flags. */
379 static inline BOOL has_invalid_flag_combination(DWORD flags) {
380 return((flags & Uri_CREATE_DECODE_EXTRA_INFO && flags & Uri_CREATE_NO_DECODE_EXTRA_INFO) ||
381 (flags & Uri_CREATE_CANONICALIZE && flags & Uri_CREATE_NO_CANONICALIZE) ||
382 (flags & Uri_CREATE_CRACK_UNKNOWN_SCHEMES && flags & Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES) ||
383 (flags & Uri_CREATE_PRE_PROCESS_HTML_URI && flags & Uri_CREATE_NO_PRE_PROCESS_HTML_URI) ||
384 (flags & Uri_CREATE_IE_SETTINGS && flags & Uri_CREATE_NO_IE_SETTINGS));
387 /* Applies each default Uri_CREATE flags to 'flags' if it
388 * doesn't cause a flag conflict.
390 static void apply_default_flags(DWORD *flags) {
391 if(!(*flags & Uri_CREATE_NO_CANONICALIZE))
392 *flags |= Uri_CREATE_CANONICALIZE;
393 if(!(*flags & Uri_CREATE_NO_DECODE_EXTRA_INFO))
394 *flags |= Uri_CREATE_DECODE_EXTRA_INFO;
395 if(!(*flags & Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES))
396 *flags |= Uri_CREATE_CRACK_UNKNOWN_SCHEMES;
397 if(!(*flags & Uri_CREATE_NO_PRE_PROCESS_HTML_URI))
398 *flags |= Uri_CREATE_PRE_PROCESS_HTML_URI;
399 if(!(*flags & Uri_CREATE_IE_SETTINGS))
400 *flags |= Uri_CREATE_NO_IE_SETTINGS;
403 /* Determines if the URI is hierarchical using the information already parsed into
404 * data and using the current location of parsing in the URI string.
406 * Windows considers a URI hierarchical if on of the following is true:
407 * A.) It's a wildcard scheme.
408 * B.) It's an implicit file scheme.
409 * C.) It's a known hierarchical scheme and it has two '\\' after the scheme name.
410 * (the '\\' will be converted into "//" during canonicalization).
411 * D.) It's not a relative URI and "//" appears after the scheme name.
413 static inline BOOL is_hierarchical_uri(const WCHAR **ptr, const parse_data *data) {
414 const WCHAR *start = *ptr;
416 if(data->scheme_type == URL_SCHEME_WILDCARD)
418 else if(data->scheme_type == URL_SCHEME_FILE && data->has_implicit_scheme)
420 else if(is_hierarchical_scheme(data->scheme_type) && (*ptr)[0] == '\\' && (*ptr)[1] == '\\') {
423 } else if(!data->is_relative && check_hierarchical(ptr))
430 /* Checks if the two Uri's are logically equivalent. It's a simple
431 * comparison, since they are both of type Uri, and it can access
432 * the properties of each Uri directly without the need to go
433 * through the "IUri_Get*" interface calls.
435 static BOOL are_equal_simple(const Uri *a, const Uri *b) {
436 if(a->scheme_type == b->scheme_type) {
437 const BOOL known_scheme = a->scheme_type != URL_SCHEME_UNKNOWN;
438 const BOOL are_hierarchical =
439 (a->authority_start > -1 && b->authority_start > -1);
441 if(a->scheme_type == URL_SCHEME_FILE) {
442 if(a->canon_len == b->canon_len)
443 return !StrCmpIW(a->canon_uri, b->canon_uri);
446 /* Only compare the scheme names (if any) if their unknown scheme types. */
448 if((a->scheme_start > -1 && b->scheme_start > -1) &&
449 (a->scheme_len == b->scheme_len)) {
450 /* Make sure the schemes are the same. */
451 if(StrCmpNW(a->canon_uri+a->scheme_start, b->canon_uri+b->scheme_start, a->scheme_len))
453 } else if(a->scheme_len != b->scheme_len)
454 /* One of the Uri's has a scheme name, while the other doesn't. */
458 /* If they have a userinfo component, perform case sensitive compare. */
459 if((a->userinfo_start > -1 && b->userinfo_start > -1) &&
460 (a->userinfo_len == b->userinfo_len)) {
461 if(StrCmpNW(a->canon_uri+a->userinfo_start, b->canon_uri+b->userinfo_start, a->userinfo_len))
463 } else if(a->userinfo_len != b->userinfo_len)
464 /* One of the Uri's had a userinfo, while the other one doesn't. */
467 /* Check if they have a host name. */
468 if((a->host_start > -1 && b->host_start > -1) &&
469 (a->host_len == b->host_len)) {
470 /* Perform a case insensitive compare if they are a known scheme type. */
472 if(StrCmpNIW(a->canon_uri+a->host_start, b->canon_uri+b->host_start, a->host_len))
474 } else if(StrCmpNW(a->canon_uri+a->host_start, b->canon_uri+b->host_start, a->host_len))
476 } else if(a->host_len != b->host_len)
477 /* One of the Uri's had a host, while the other one didn't. */
480 if(a->has_port && b->has_port) {
481 if(a->port != b->port)
483 } else if(a->has_port || b->has_port)
484 /* One had a port, while the other one didn't. */
487 /* Windows is weird with how it handles paths. For example
488 * One URI could be "http://google.com" (after canonicalization)
489 * and one could be "http://google.com/" and the IsEqual function
490 * would still evaluate to TRUE, but, only if they are both hierarchical
493 if((a->path_start > -1 && b->path_start > -1) &&
494 (a->path_len == b->path_len)) {
495 if(StrCmpNW(a->canon_uri+a->path_start, b->canon_uri+b->path_start, a->path_len))
497 } else if(are_hierarchical && a->path_len == -1 && b->path_len == 0) {
498 if(*(a->canon_uri+a->path_start) != '/')
500 } else if(are_hierarchical && b->path_len == 1 && a->path_len == 0) {
501 if(*(b->canon_uri+b->path_start) != '/')
503 } else if(a->path_len != b->path_len)
506 /* Compare the query strings of the two URIs. */
507 if((a->query_start > -1 && b->query_start > -1) &&
508 (a->query_len == b->query_len)) {
509 if(StrCmpNW(a->canon_uri+a->query_start, b->canon_uri+b->query_start, a->query_len))
511 } else if(a->query_len != b->query_len)
514 if((a->fragment_start > -1 && b->fragment_start > -1) &&
515 (a->fragment_len == b->fragment_len)) {
516 if(StrCmpNW(a->canon_uri+a->fragment_start, b->canon_uri+b->fragment_start, a->fragment_len))
518 } else if(a->fragment_len != b->fragment_len)
521 /* If we get here, the two URIs are equivalent. */
528 /* Computes the size of the given IPv6 address.
529 * Each h16 component is 16bits, if there is an IPv4 address, it's
530 * 32bits. If there's an elision it can be 16bits to 128bits, depending
531 * on the number of other components.
533 * Modeled after google-url's CheckIPv6ComponentsSize function
535 static void compute_ipv6_comps_size(ipv6_address *address) {
536 address->components_size = address->h16_count * 2;
539 /* IPv4 address is 4 bytes. */
540 address->components_size += 4;
542 if(address->elision) {
543 /* An elision can be anywhere from 2 bytes up to 16 bytes.
544 * It size depends on the size of the h16 and IPv4 components.
546 address->elision_size = 16 - address->components_size;
547 if(address->elision_size < 2)
548 address->elision_size = 2;
550 address->elision_size = 0;
553 /* Taken from dlls/jscript/lex.c */
554 static int hex_to_int(WCHAR val) {
555 if(val >= '0' && val <= '9')
557 else if(val >= 'a' && val <= 'f')
558 return val - 'a' + 10;
559 else if(val >= 'A' && val <= 'F')
560 return val - 'A' + 10;
565 /* Helper function for converting a percent encoded string
566 * representation of a WCHAR value into its actual WCHAR value. If
567 * the two characters following the '%' aren't valid hex values then
568 * this function returns the NULL character.
571 * "%2E" will result in '.' being returned by this function.
573 static WCHAR decode_pct_val(const WCHAR *ptr) {
576 if(*ptr == '%' && is_hexdigit(*(ptr + 1)) && is_hexdigit(*(ptr + 2))) {
577 INT a = hex_to_int(*(ptr + 1));
578 INT b = hex_to_int(*(ptr + 2));
587 /* Helper function for percent encoding a given character
588 * and storing the encoded value into a given buffer (dest).
590 * It's up to the calling function to ensure that there is
591 * at least enough space in 'dest' for the percent encoded
592 * value to be stored (so dest + 3 spaces available).
594 static inline void pct_encode_val(WCHAR val, WCHAR *dest) {
596 dest[1] = hexDigits[(val >> 4) & 0xf];
597 dest[2] = hexDigits[val & 0xf];
600 /* Scans the range of characters [str, end] and returns the last occurrence
601 * of 'ch' or returns NULL.
603 static const WCHAR *str_last_of(const WCHAR *str, const WCHAR *end, WCHAR ch) {
604 const WCHAR *ptr = end;
615 /* Attempts to parse the domain name from the host.
617 * This function also includes the Top-level Domain (TLD) name
618 * of the host when it tries to find the domain name. If it finds
619 * a valid domain name it will assign 'domain_start' the offset
620 * into 'host' where the domain name starts.
622 * It's implied that if there is a domain name its range is:
623 * [host+domain_start, host+host_len).
625 static void find_domain_name(const WCHAR *host, DWORD host_len,
627 const WCHAR *last_tld, *sec_last_tld, *end;
629 end = host+host_len-1;
633 /* There has to be at least enough room for a '.' followed by a
634 * 3 character TLD for a domain to even exist in the host name.
639 last_tld = str_last_of(host, end, '.');
641 /* http://hostname -> has no domain name. */
644 sec_last_tld = str_last_of(host, last_tld-1, '.');
646 /* If the '.' is at the beginning of the host there
647 * has to be at least 3 characters in the TLD for it
649 * Ex: .com -> .com as the domain name.
650 * .co -> has no domain name.
652 if(last_tld-host == 0) {
653 if(end-(last_tld-1) < 3)
655 } else if(last_tld-host == 3) {
658 /* If there's three characters in front of last_tld and
659 * they are on the list of recognized TLDs, then this
660 * host doesn't have a domain (since the host only contains
662 * Ex: edu.uk -> has no domain name.
663 * foo.uk -> foo.uk as the domain name.
665 for(i = 0; i < sizeof(recognized_tlds)/sizeof(recognized_tlds[0]); ++i) {
666 if(!StrCmpNIW(host, recognized_tlds[i].tld_name, 3))
669 } else if(last_tld-host < 3)
670 /* Anything less than 3 characters is considered part
672 * Ex: ak.uk -> Has no domain name.
676 /* Otherwise the domain name is the whole host name. */
678 } else if(end+1-last_tld > 3) {
679 /* If the last_tld has more than 3 characters, then it's automatically
680 * considered the TLD of the domain name.
681 * Ex: www.winehq.org.uk.test -> uk.test as the domain name.
683 *domain_start = (sec_last_tld+1)-host;
684 } else if(last_tld - (sec_last_tld+1) < 4) {
686 /* If the sec_last_tld is 3 characters long it HAS to be on the list of
687 * recognized to still be considered part of the TLD name, otherwise
688 * its considered the domain name.
689 * Ex: www.google.com.uk -> google.com.uk as the domain name.
690 * www.google.foo.uk -> foo.uk as the domain name.
692 if(last_tld - (sec_last_tld+1) == 3) {
693 for(i = 0; i < sizeof(recognized_tlds)/sizeof(recognized_tlds[0]); ++i) {
694 if(!StrCmpNIW(sec_last_tld+1, recognized_tlds[i].tld_name, 3)) {
695 const WCHAR *domain = str_last_of(host, sec_last_tld-1, '.');
700 *domain_start = (domain+1) - host;
701 TRACE("Found domain name %s\n", debugstr_wn(host+*domain_start,
702 (host+host_len)-(host+*domain_start)));
707 *domain_start = (sec_last_tld+1)-host;
709 /* Since the sec_last_tld is less than 3 characters it's considered
711 * Ex: www.google.fo.uk -> google.fo.uk as the domain name.
713 const WCHAR *domain = str_last_of(host, sec_last_tld-1, '.');
718 *domain_start = (domain+1) - host;
721 /* The second to last TLD has more than 3 characters making it
723 * Ex: www.google.test.us -> test.us as the domain name.
725 *domain_start = (sec_last_tld+1)-host;
728 TRACE("Found domain name %s\n", debugstr_wn(host+*domain_start,
729 (host+host_len)-(host+*domain_start)));
732 /* Removes the dot segments from a hierarchical URIs path component. This
733 * function performs the removal in place.
735 * This function returns the new length of the path string.
737 static DWORD remove_dot_segments(WCHAR *path, DWORD path_len) {
739 const WCHAR *in = out;
740 const WCHAR *end = out + path_len;
744 /* Move the first path segment in the input buffer to the end of
745 * the output buffer, and any subsequent characters up to, including
746 * the next "/" character (if any) or the end of the input buffer.
748 while(in < end && !is_slash(*in))
758 /* Handle ending "/." */
765 if(is_slash(in[1])) {
770 /* If we don't have "/../" or ending "/.." */
771 if(in[1] != '.' || (in + 2 != end && !is_slash(in[2])))
774 /* Find the slash preceding out pointer and move out pointer to it */
775 if(out > path+1 && is_slash(*--out))
777 while(out > path && !is_slash(*(--out)));
787 TRACE("(%p %d): Path after dot segments removed %s len=%d\n", path, path_len,
788 debugstr_wn(path, len), len);
792 /* Attempts to find the file extension in a given path. */
793 static INT find_file_extension(const WCHAR *path, DWORD path_len) {
796 for(end = path+path_len-1; end >= path && *end != '/' && *end != '\\'; --end) {
804 /* Computes the location where the elision should occur in the IPv6
805 * address using the numerical values of each component stored in
806 * 'values'. If the address shouldn't contain an elision then 'index'
807 * is assigned -1 as it's value. Otherwise 'index' will contain the
808 * starting index (into values) where the elision should be, and 'count'
809 * will contain the number of cells the elision covers.
812 * Windows will expand an elision if the elision only represents 1 h16
813 * component of the address.
815 * Ex: [1::2:3:4:5:6:7] -> [1:0:2:3:4:5:6:7]
817 * If the IPv6 address contains an IPv4 address, the IPv4 address is also
818 * considered for being included as part of an elision if all its components
821 * Ex: [1:2:3:4:5:6:0.0.0.0] -> [1:2:3:4:5:6::]
823 static void compute_elision_location(const ipv6_address *address, const USHORT values[8],
824 INT *index, DWORD *count) {
825 DWORD i, max_len, cur_len;
826 INT max_index, cur_index;
828 max_len = cur_len = 0;
829 max_index = cur_index = -1;
830 for(i = 0; i < 8; ++i) {
831 BOOL check_ipv4 = (address->ipv4 && i == 6);
832 BOOL is_end = (check_ipv4 || i == 7);
835 /* Check if the IPv4 address contains only zeros. */
836 if(values[i] == 0 && values[i+1] == 0) {
843 } else if(values[i] == 0) {
850 if(is_end || values[i] != 0) {
851 /* We only consider it for an elision if it's
852 * more than 1 component long.
854 if(cur_len > 1 && cur_len > max_len) {
855 /* Found the new elision location. */
857 max_index = cur_index;
860 /* Reset the current range for the next range of zeros. */
870 /* Removes all the leading and trailing white spaces or
871 * control characters from the URI and removes all control
872 * characters inside of the URI string.
874 static BSTR pre_process_uri(LPCWSTR uri) {
877 const WCHAR *start, *end;
883 /* Skip leading controls and whitespace. */
884 while(iscntrlW(*start) || isspaceW(*start)) ++start;
888 /* URI consisted only of control/whitespace. */
889 ret = SysAllocStringLen(NULL, 0);
891 while(iscntrlW(*end) || isspaceW(*end)) --end;
893 buf = heap_alloc(((end+1)-start)*sizeof(WCHAR));
897 for(ptr = buf; start < end+1; ++start) {
898 if(!iscntrlW(*start))
902 ret = SysAllocStringLen(buf, ptr-buf);
909 /* Converts the specified IPv4 address into an uint value.
911 * This function assumes that the IPv4 address has already been validated.
913 static UINT ipv4toui(const WCHAR *ip, DWORD len) {
915 DWORD comp_value = 0;
918 for(ptr = ip; ptr < ip+len; ++ptr) {
924 comp_value = comp_value*10 + (*ptr-'0');
933 /* Converts an IPv4 address in numerical form into it's fully qualified
934 * string form. This function returns the number of characters written
935 * to 'dest'. If 'dest' is NULL this function will return the number of
936 * characters that would have been written.
938 * It's up to the caller to ensure there's enough space in 'dest' for the
941 static DWORD ui2ipv4(WCHAR *dest, UINT address) {
942 static const WCHAR formatW[] =
943 {'%','u','.','%','u','.','%','u','.','%','u',0};
947 digits[0] = (address >> 24) & 0xff;
948 digits[1] = (address >> 16) & 0xff;
949 digits[2] = (address >> 8) & 0xff;
950 digits[3] = address & 0xff;
954 ret = sprintfW(tmp, formatW, digits[0], digits[1], digits[2], digits[3]);
956 ret = sprintfW(dest, formatW, digits[0], digits[1], digits[2], digits[3]);
961 static DWORD ui2str(WCHAR *dest, UINT value) {
962 static const WCHAR formatW[] = {'%','u',0};
967 ret = sprintfW(tmp, formatW, value);
969 ret = sprintfW(dest, formatW, value);
974 /* Converts an h16 component (from an IPv6 address) into it's
977 * This function assumes that the h16 component has already been validated.
979 static USHORT h16tous(h16 component) {
983 for(i = 0; i < component.len; ++i) {
985 ret += hex_to_int(component.str[i]);
991 /* Converts an IPv6 address into its 128 bits (16 bytes) numerical value.
993 * This function assumes that the ipv6_address has already been validated.
995 static BOOL ipv6_to_number(const ipv6_address *address, USHORT number[8]) {
996 DWORD i, cur_component = 0;
997 BOOL already_passed_elision = FALSE;
999 for(i = 0; i < address->h16_count; ++i) {
1000 if(address->elision) {
1001 if(address->components[i].str > address->elision && !already_passed_elision) {
1002 /* Means we just passed the elision and need to add its values to
1003 * 'number' before we do anything else.
1006 for(j = 0; j < address->elision_size; j+=2)
1007 number[cur_component++] = 0;
1009 already_passed_elision = TRUE;
1013 number[cur_component++] = h16tous(address->components[i]);
1016 /* Case when the elision appears after the h16 components. */
1017 if(!already_passed_elision && address->elision) {
1018 for(i = 0; i < address->elision_size; i+=2)
1019 number[cur_component++] = 0;
1020 already_passed_elision = TRUE;
1024 UINT value = ipv4toui(address->ipv4, address->ipv4_len);
1026 if(cur_component != 6) {
1027 ERR("(%p %p): Failed sanity check with %d\n", address, number, cur_component);
1031 number[cur_component++] = (value >> 16) & 0xffff;
1032 number[cur_component] = value & 0xffff;
1038 /* Checks if the characters pointed to by 'ptr' are
1039 * a percent encoded data octet.
1041 * pct-encoded = "%" HEXDIG HEXDIG
1043 static BOOL check_pct_encoded(const WCHAR **ptr) {
1044 const WCHAR *start = *ptr;
1050 if(!is_hexdigit(**ptr)) {
1056 if(!is_hexdigit(**ptr)) {
1065 /* dec-octet = DIGIT ; 0-9
1066 * / %x31-39 DIGIT ; 10-99
1067 * / "1" 2DIGIT ; 100-199
1068 * / "2" %x30-34 DIGIT ; 200-249
1069 * / "25" %x30-35 ; 250-255
1071 static BOOL check_dec_octet(const WCHAR **ptr) {
1072 const WCHAR *c1, *c2, *c3;
1075 /* A dec-octet must be at least 1 digit long. */
1076 if(*c1 < '0' || *c1 > '9')
1082 /* Since the 1 digit requirment was meet, it doesn't
1083 * matter if this is a DIGIT value, it's considered a
1086 if(*c2 < '0' || *c2 > '9')
1092 /* Same explanation as above. */
1093 if(*c3 < '0' || *c3 > '9')
1096 /* Anything > 255 isn't a valid IP dec-octet. */
1097 if(*c1 >= '2' && *c2 >= '5' && *c3 >= '5') {
1106 /* Checks if there is an implicit IPv4 address in the host component of the URI.
1107 * The max value of an implicit IPv4 address is UINT_MAX.
1110 * "234567" would be considered an implicit IPv4 address.
1112 static BOOL check_implicit_ipv4(const WCHAR **ptr, UINT *val) {
1113 const WCHAR *start = *ptr;
1117 while(is_num(**ptr)) {
1118 ret = ret*10 + (**ptr - '0');
1120 if(ret > UINT_MAX) {
1134 /* Checks if the string contains an IPv4 address.
1136 * This function has a strict mode or a non-strict mode of operation
1137 * When 'strict' is set to FALSE this function will return TRUE if
1138 * the string contains at least 'dec-octet "." dec-octet' since partial
1139 * IPv4 addresses will be normalized out into full IPv4 addresses. When
1140 * 'strict' is set this function expects there to be a full IPv4 address.
1142 * IPv4address = dec-octet "." dec-octet "." dec-octet "." dec-octet
1144 static BOOL check_ipv4address(const WCHAR **ptr, BOOL strict) {
1145 const WCHAR *start = *ptr;
1147 if(!check_dec_octet(ptr)) {
1158 if(!check_dec_octet(ptr)) {
1172 if(!check_dec_octet(ptr)) {
1186 if(!check_dec_octet(ptr)) {
1191 /* Found a four digit ip address. */
1194 /* Tries to parse the scheme name of the URI.
1196 * scheme = ALPHA *(ALPHA | NUM | '+' | '-' | '.') as defined by RFC 3896.
1197 * NOTE: Windows accepts a number as the first character of a scheme.
1199 static BOOL parse_scheme_name(const WCHAR **ptr, parse_data *data, DWORD extras) {
1200 const WCHAR *start = *ptr;
1202 data->scheme = NULL;
1203 data->scheme_len = 0;
1206 if(**ptr == '*' && *ptr == start) {
1207 /* Might have found a wildcard scheme. If it is the next
1208 * char has to be a ':' for it to be a valid URI
1212 } else if(!is_num(**ptr) && !is_alpha(**ptr) && **ptr != '+' &&
1213 **ptr != '-' && **ptr != '.')
1222 /* Schemes must end with a ':' */
1223 if(**ptr != ':' && !((extras & ALLOW_NULL_TERM_SCHEME) && !**ptr)) {
1228 data->scheme = start;
1229 data->scheme_len = *ptr - start;
1235 /* Tries to deduce the corresponding URL_SCHEME for the given URI. Stores
1236 * the deduced URL_SCHEME in data->scheme_type.
1238 static BOOL parse_scheme_type(parse_data *data) {
1239 /* If there's scheme data then see if it's a recognized scheme. */
1240 if(data->scheme && data->scheme_len) {
1243 for(i = 0; i < sizeof(recognized_schemes)/sizeof(recognized_schemes[0]); ++i) {
1244 if(lstrlenW(recognized_schemes[i].scheme_name) == data->scheme_len) {
1245 /* Has to be a case insensitive compare. */
1246 if(!StrCmpNIW(recognized_schemes[i].scheme_name, data->scheme, data->scheme_len)) {
1247 data->scheme_type = recognized_schemes[i].scheme;
1253 /* If we get here it means it's not a recognized scheme. */
1254 data->scheme_type = URL_SCHEME_UNKNOWN;
1256 } else if(data->is_relative) {
1257 /* Relative URI's have no scheme. */
1258 data->scheme_type = URL_SCHEME_UNKNOWN;
1261 /* Should never reach here! what happened... */
1262 FIXME("(%p): Unable to determine scheme type for URI %s\n", data, debugstr_w(data->uri));
1267 /* Tries to parse (or deduce) the scheme_name of a URI. If it can't
1268 * parse a scheme from the URI it will try to deduce the scheme_name and scheme_type
1269 * using the flags specified in 'flags' (if any). Flags that affect how this function
1270 * operates are the Uri_CREATE_ALLOW_* flags.
1272 * All parsed/deduced information will be stored in 'data' when the function returns.
1274 * Returns TRUE if it was able to successfully parse the information.
1276 static BOOL parse_scheme(const WCHAR **ptr, parse_data *data, DWORD flags, DWORD extras) {
1277 static const WCHAR fileW[] = {'f','i','l','e',0};
1278 static const WCHAR wildcardW[] = {'*',0};
1280 /* First check to see if the uri could implicitly be a file path. */
1281 if(is_implicit_file_path(*ptr)) {
1282 if(flags & Uri_CREATE_ALLOW_IMPLICIT_FILE_SCHEME) {
1283 data->scheme = fileW;
1284 data->scheme_len = lstrlenW(fileW);
1285 data->has_implicit_scheme = TRUE;
1287 TRACE("(%p %p %x): URI is an implicit file path.\n", ptr, data, flags);
1289 /* Window's does not consider anything that can implicitly be a file
1290 * path to be a valid URI if the ALLOW_IMPLICIT_FILE_SCHEME flag is not set...
1292 TRACE("(%p %p %x): URI is implicitly a file path, but, the ALLOW_IMPLICIT_FILE_SCHEME flag wasn't set.\n",
1296 } else if(!parse_scheme_name(ptr, data, extras)) {
1297 /* No Scheme was found, this means it could be:
1298 * a) an implicit Wildcard scheme
1302 if(flags & Uri_CREATE_ALLOW_IMPLICIT_WILDCARD_SCHEME) {
1303 data->scheme = wildcardW;
1304 data->scheme_len = lstrlenW(wildcardW);
1305 data->has_implicit_scheme = TRUE;
1307 TRACE("(%p %p %x): URI is an implicit wildcard scheme.\n", ptr, data, flags);
1308 } else if (flags & Uri_CREATE_ALLOW_RELATIVE) {
1309 data->is_relative = TRUE;
1310 TRACE("(%p %p %x): URI is relative.\n", ptr, data, flags);
1312 TRACE("(%p %p %x): Malformed URI found. Unable to deduce scheme name.\n", ptr, data, flags);
1317 if(!data->is_relative)
1318 TRACE("(%p %p %x): Found scheme=%s scheme_len=%d\n", ptr, data, flags,
1319 debugstr_wn(data->scheme, data->scheme_len), data->scheme_len);
1321 if(!parse_scheme_type(data))
1324 TRACE("(%p %p %x): Assigned %d as the URL_SCHEME.\n", ptr, data, flags, data->scheme_type);
1328 static BOOL parse_username(const WCHAR **ptr, parse_data *data, DWORD flags, DWORD extras) {
1329 data->username = *ptr;
1331 while(**ptr != ':' && **ptr != '@') {
1333 if(!check_pct_encoded(ptr)) {
1334 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
1335 *ptr = data->username;
1336 data->username = NULL;
1341 } else if(extras & ALLOW_NULL_TERM_USER_NAME && !**ptr)
1343 else if(is_auth_delim(**ptr, data->scheme_type != URL_SCHEME_UNKNOWN)) {
1344 *ptr = data->username;
1345 data->username = NULL;
1352 data->username_len = *ptr - data->username;
1356 static BOOL parse_password(const WCHAR **ptr, parse_data *data, DWORD flags, DWORD extras) {
1357 data->password = *ptr;
1359 while(**ptr != '@') {
1361 if(!check_pct_encoded(ptr)) {
1362 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
1363 *ptr = data->password;
1364 data->password = NULL;
1369 } else if(extras & ALLOW_NULL_TERM_PASSWORD && !**ptr)
1371 else if(is_auth_delim(**ptr, data->scheme_type != URL_SCHEME_UNKNOWN)) {
1372 *ptr = data->password;
1373 data->password = NULL;
1380 data->password_len = *ptr - data->password;
1384 /* Parses the userinfo part of the URI (if it exists). The userinfo field of
1385 * a URI can consist of "username:password@", or just "username@".
1388 * userinfo = *( unreserved / pct-encoded / sub-delims / ":" )
1391 * 1) If there is more than one ':' in the userinfo part of the URI Windows
1392 * uses the first occurrence of ':' to delimit the username and password
1396 * ftp://user:pass:word@winehq.org
1398 * Would yield, "user" as the username and "pass:word" as the password.
1400 * 2) Windows allows any character to appear in the "userinfo" part of
1401 * a URI, as long as it's not an authority delimeter character set.
1403 static void parse_userinfo(const WCHAR **ptr, parse_data *data, DWORD flags) {
1404 const WCHAR *start = *ptr;
1406 if(!parse_username(ptr, data, flags, 0)) {
1407 TRACE("(%p %p %x): URI contained no userinfo.\n", ptr, data, flags);
1413 if(!parse_password(ptr, data, flags, 0)) {
1415 data->username = NULL;
1416 data->username_len = 0;
1417 TRACE("(%p %p %x): URI contained no userinfo.\n", ptr, data, flags);
1424 data->username = NULL;
1425 data->username_len = 0;
1426 data->password = NULL;
1427 data->password_len = 0;
1429 TRACE("(%p %p %x): URI contained no userinfo.\n", ptr, data, flags);
1434 TRACE("(%p %p %x): Found username %s len=%d.\n", ptr, data, flags,
1435 debugstr_wn(data->username, data->username_len), data->username_len);
1438 TRACE("(%p %p %x): Found password %s len=%d.\n", ptr, data, flags,
1439 debugstr_wn(data->password, data->password_len), data->password_len);
1444 /* Attempts to parse a port from the URI.
1447 * Windows seems to have a cap on what the maximum value
1448 * for a port can be. The max value is USHORT_MAX.
1452 static BOOL parse_port(const WCHAR **ptr, parse_data *data, DWORD flags) {
1456 while(!is_auth_delim(**ptr, data->scheme_type != URL_SCHEME_UNKNOWN)) {
1457 if(!is_num(**ptr)) {
1463 port = port*10 + (**ptr-'0');
1465 if(port > USHORT_MAX) {
1474 data->has_port = TRUE;
1475 data->port_value = port;
1476 data->port_len = *ptr - data->port;
1478 TRACE("(%p %p %x): Found port %s len=%d value=%u\n", ptr, data, flags,
1479 debugstr_wn(data->port, data->port_len), data->port_len, data->port_value);
1483 /* Attempts to parse a IPv4 address from the URI.
1486 * Window's normalizes IPv4 addresses, This means there's three
1487 * possibilities for the URI to contain an IPv4 address.
1488 * 1) A well formed address (ex. 192.2.2.2).
1489 * 2) A partially formed address. For example "192.0" would
1490 * normalize to "192.0.0.0" during canonicalization.
1491 * 3) An implicit IPv4 address. For example "256" would
1492 * normalize to "0.0.1.0" during canonicalization. Also
1493 * note that the maximum value for an implicit IP address
1494 * is UINT_MAX, if the value in the URI exceeds this then
1495 * it is not considered an IPv4 address.
1497 static BOOL parse_ipv4address(const WCHAR **ptr, parse_data *data, DWORD flags) {
1498 const BOOL is_unknown = data->scheme_type == URL_SCHEME_UNKNOWN;
1501 if(!check_ipv4address(ptr, FALSE)) {
1502 if(!check_implicit_ipv4(ptr, &data->implicit_ipv4)) {
1503 TRACE("(%p %p %x): URI didn't contain anything looking like an IPv4 address.\n",
1509 data->has_implicit_ip = TRUE;
1512 /* Check if what we found is the only part of the host name (if it isn't
1513 * we don't have an IPv4 address).
1517 if(!parse_port(ptr, data, flags)) {
1522 } else if(!is_auth_delim(**ptr, !is_unknown)) {
1523 /* Found more data which belongs the host, so this isn't an IPv4. */
1526 data->has_implicit_ip = FALSE;
1530 data->host_len = *ptr - data->host;
1531 data->host_type = Uri_HOST_IPV4;
1533 TRACE("(%p %p %x): IPv4 address found. host=%s host_len=%d host_type=%d\n",
1534 ptr, data, flags, debugstr_wn(data->host, data->host_len),
1535 data->host_len, data->host_type);
1539 /* Attempts to parse the reg-name from the URI.
1541 * Because of the way Windows handles ':' this function also
1542 * handles parsing the port.
1544 * reg-name = *( unreserved / pct-encoded / sub-delims )
1547 * Windows allows everything, but, the characters in "auth_delims" and ':'
1548 * to appear in a reg-name, unless it's an unknown scheme type then ':' is
1549 * allowed to appear (even if a valid port isn't after it).
1551 * Windows doesn't like host names which start with '[' and end with ']'
1552 * and don't contain a valid IP literal address in between them.
1554 * On Windows if an '[' is encountered in the host name the ':' no longer
1555 * counts as a delimiter until you reach the next ']' or an "authority delimeter".
1557 * A reg-name CAN be empty.
1559 static BOOL parse_reg_name(const WCHAR **ptr, parse_data *data, DWORD flags, DWORD extras) {
1560 const BOOL has_start_bracket = **ptr == '[';
1561 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
1562 const BOOL is_res = data->scheme_type == URL_SCHEME_RES;
1563 BOOL inside_brackets = has_start_bracket;
1565 /* res URIs don't have ports. */
1566 BOOL ignore_col = (extras & IGNORE_PORT_DELIMITER) || is_res;
1568 /* We have to be careful with file schemes. */
1569 if(data->scheme_type == URL_SCHEME_FILE) {
1570 /* This is because an implicit file scheme could be "C:\\test" and it
1571 * would trick this function into thinking the host is "C", when after
1572 * canonicalization the host would end up being an empty string. A drive
1573 * path can also have a '|' instead of a ':' after the drive letter.
1575 if(is_drive_path(*ptr)) {
1576 /* Regular old drive paths don't have a host type (or host name). */
1577 data->host_type = Uri_HOST_UNKNOWN;
1581 } else if(is_unc_path(*ptr))
1582 /* Skip past the "\\" of a UNC path. */
1588 /* For res URIs, everything before the first '/' is
1589 * considered the host.
1591 while((!is_res && !is_auth_delim(**ptr, known_scheme)) ||
1592 (is_res && **ptr && **ptr != '/')) {
1593 if(**ptr == ':' && !ignore_col) {
1594 /* We can ignore ':' if were inside brackets.*/
1595 if(!inside_brackets) {
1596 const WCHAR *tmp = (*ptr)++;
1598 /* Attempt to parse the port. */
1599 if(!parse_port(ptr, data, flags)) {
1600 /* Windows expects there to be a valid port for known scheme types. */
1601 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
1604 TRACE("(%p %p %x %x): Expected valid port\n", ptr, data, flags, extras);
1607 /* Windows gives up on trying to parse a port when it
1608 * encounters 1 invalid port.
1612 data->host_len = tmp - data->host;
1616 } else if(**ptr == '%' && (known_scheme && !is_res)) {
1617 /* Has to be a legit % encoded value. */
1618 if(!check_pct_encoded(ptr)) {
1624 } else if(is_res && is_forbidden_dos_path_char(**ptr)) {
1628 } else if(**ptr == ']')
1629 inside_brackets = FALSE;
1630 else if(**ptr == '[')
1631 inside_brackets = TRUE;
1636 if(has_start_bracket) {
1637 /* Make sure the last character of the host wasn't a ']'. */
1638 if(*(*ptr-1) == ']') {
1639 TRACE("(%p %p %x %x): Expected an IP literal inside of the host\n",
1640 ptr, data, flags, extras);
1647 /* Don't overwrite our length if we found a port earlier. */
1649 data->host_len = *ptr - data->host;
1651 /* If the host is empty, then it's an unknown host type. */
1652 if(data->host_len == 0 || is_res)
1653 data->host_type = Uri_HOST_UNKNOWN;
1655 data->host_type = Uri_HOST_DNS;
1657 TRACE("(%p %p %x %x): Parsed reg-name. host=%s len=%d\n", ptr, data, flags, extras,
1658 debugstr_wn(data->host, data->host_len), data->host_len);
1662 /* Attempts to parse an IPv6 address out of the URI.
1664 * IPv6address = 6( h16 ":" ) ls32
1665 * / "::" 5( h16 ":" ) ls32
1666 * / [ h16 ] "::" 4( h16 ":" ) ls32
1667 * / [ *1( h16 ":" ) h16 ] "::" 3( h16 ":" ) ls32
1668 * / [ *2( h16 ":" ) h16 ] "::" 2( h16 ":" ) ls32
1669 * / [ *3( h16 ":" ) h16 ] "::" h16 ":" ls32
1670 * / [ *4( h16 ":" ) h16 ] "::" ls32
1671 * / [ *5( h16 ":" ) h16 ] "::" h16
1672 * / [ *6( h16 ":" ) h16 ] "::"
1674 * ls32 = ( h16 ":" h16 ) / IPv4address
1675 * ; least-significant 32 bits of address.
1678 * ; 16 bits of address represented in hexadecimal.
1680 * Modeled after google-url's 'DoParseIPv6' function.
1682 static BOOL parse_ipv6address(const WCHAR **ptr, parse_data *data, DWORD flags) {
1683 const WCHAR *start, *cur_start;
1686 start = cur_start = *ptr;
1687 memset(&ip, 0, sizeof(ipv6_address));
1690 /* Check if we're on the last character of the host. */
1691 BOOL is_end = (is_auth_delim(**ptr, data->scheme_type != URL_SCHEME_UNKNOWN)
1694 BOOL is_split = (**ptr == ':');
1695 BOOL is_elision = (is_split && !is_end && *(*ptr+1) == ':');
1697 /* Check if we're at the end of a component, or
1698 * if we're at the end of the IPv6 address.
1700 if(is_split || is_end) {
1703 cur_len = *ptr - cur_start;
1705 /* h16 can't have a length > 4. */
1709 TRACE("(%p %p %x): h16 component to long.\n",
1715 /* An h16 component can't have the length of 0 unless
1716 * the elision is at the beginning of the address, or
1717 * at the end of the address.
1719 if(!((*ptr == start && is_elision) ||
1720 (is_end && (*ptr-2) == ip.elision))) {
1722 TRACE("(%p %p %x): IPv6 component cannot have a length of 0.\n",
1729 /* An IPv6 address can have no more than 8 h16 components. */
1730 if(ip.h16_count >= 8) {
1732 TRACE("(%p %p %x): Not a IPv6 address, to many h16 components.\n",
1737 ip.components[ip.h16_count].str = cur_start;
1738 ip.components[ip.h16_count].len = cur_len;
1740 TRACE("(%p %p %x): Found h16 component %s, len=%d, h16_count=%d\n",
1741 ptr, data, flags, debugstr_wn(cur_start, cur_len), cur_len,
1751 /* A IPv6 address can only have 1 elision ('::'). */
1755 TRACE("(%p %p %x): IPv6 address cannot have 2 elisions.\n",
1767 if(!check_ipv4address(ptr, TRUE)) {
1768 if(!is_hexdigit(**ptr)) {
1769 /* Not a valid character for an IPv6 address. */
1774 /* Found an IPv4 address. */
1775 ip.ipv4 = cur_start;
1776 ip.ipv4_len = *ptr - cur_start;
1778 TRACE("(%p %p %x): Found an attached IPv4 address %s len=%d.\n",
1779 ptr, data, flags, debugstr_wn(ip.ipv4, ip.ipv4_len),
1782 /* IPv4 addresses can only appear at the end of a IPv6. */
1788 compute_ipv6_comps_size(&ip);
1790 /* Make sure the IPv6 address adds up to 16 bytes. */
1791 if(ip.components_size + ip.elision_size != 16) {
1793 TRACE("(%p %p %x): Invalid IPv6 address, did not add up to 16 bytes.\n",
1798 if(ip.elision_size == 2) {
1799 /* For some reason on Windows if an elision that represents
1800 * only 1 h16 component is encountered at the very begin or
1801 * end of an IPv6 address, Windows does not consider it a
1802 * valid IPv6 address.
1804 * Ex: [::2:3:4:5:6:7] is not valid, even though the sum
1805 * of all the components == 128bits.
1807 if(ip.elision < ip.components[0].str ||
1808 ip.elision > ip.components[ip.h16_count-1].str) {
1810 TRACE("(%p %p %x): Invalid IPv6 address. Detected elision of 2 bytes at the beginning or end of the address.\n",
1816 data->host_type = Uri_HOST_IPV6;
1817 data->has_ipv6 = TRUE;
1818 data->ipv6_address = ip;
1820 TRACE("(%p %p %x): Found valid IPv6 literal %s len=%d\n",
1821 ptr, data, flags, debugstr_wn(start, *ptr-start),
1826 /* IPvFuture = "v" 1*HEXDIG "." 1*( unreserved / sub-delims / ":" ) */
1827 static BOOL parse_ipvfuture(const WCHAR **ptr, parse_data *data, DWORD flags) {
1828 const WCHAR *start = *ptr;
1830 /* IPvFuture has to start with a 'v' or 'V'. */
1831 if(**ptr != 'v' && **ptr != 'V')
1834 /* Following the v there must be at least 1 hex digit. */
1836 if(!is_hexdigit(**ptr)) {
1842 while(is_hexdigit(**ptr))
1845 /* End of the hexdigit sequence must be a '.' */
1852 if(!is_unreserved(**ptr) && !is_subdelim(**ptr) && **ptr != ':') {
1858 while(is_unreserved(**ptr) || is_subdelim(**ptr) || **ptr == ':')
1861 data->host_type = Uri_HOST_UNKNOWN;
1863 TRACE("(%p %p %x): Parsed IPvFuture address %s len=%d\n", ptr, data, flags,
1864 debugstr_wn(start, *ptr-start), (int)(*ptr-start));
1869 /* IP-literal = "[" ( IPv6address / IPvFuture ) "]" */
1870 static BOOL parse_ip_literal(const WCHAR **ptr, parse_data *data, DWORD flags, DWORD extras) {
1873 if(**ptr != '[' && !(extras & ALLOW_BRACKETLESS_IP_LITERAL)) {
1876 } else if(**ptr == '[')
1879 if(!parse_ipv6address(ptr, data, flags)) {
1880 if(extras & SKIP_IP_FUTURE_CHECK || !parse_ipvfuture(ptr, data, flags)) {
1887 if(**ptr != ']' && !(extras & ALLOW_BRACKETLESS_IP_LITERAL)) {
1891 } else if(!**ptr && extras & ALLOW_BRACKETLESS_IP_LITERAL) {
1892 /* The IP literal didn't contain brackets and was followed by
1893 * a NULL terminator, so no reason to even check the port.
1895 data->host_len = *ptr - data->host;
1902 /* If a valid port is not found, then let it trickle down to
1905 if(!parse_port(ptr, data, flags)) {
1911 data->host_len = *ptr - data->host;
1916 /* Parses the host information from the URI.
1918 * host = IP-literal / IPv4address / reg-name
1920 static BOOL parse_host(const WCHAR **ptr, parse_data *data, DWORD flags, DWORD extras) {
1921 if(!parse_ip_literal(ptr, data, flags, extras)) {
1922 if(!parse_ipv4address(ptr, data, flags)) {
1923 if(!parse_reg_name(ptr, data, flags, extras)) {
1924 TRACE("(%p %p %x %x): Malformed URI, Unknown host type.\n",
1925 ptr, data, flags, extras);
1934 /* Parses the authority information from the URI.
1936 * authority = [ userinfo "@" ] host [ ":" port ]
1938 static BOOL parse_authority(const WCHAR **ptr, parse_data *data, DWORD flags) {
1939 parse_userinfo(ptr, data, flags);
1941 /* Parsing the port will happen during one of the host parsing
1942 * routines (if the URI has a port).
1944 if(!parse_host(ptr, data, flags, 0))
1950 /* Attempts to parse the path information of a hierarchical URI. */
1951 static BOOL parse_path_hierarchical(const WCHAR **ptr, parse_data *data, DWORD flags) {
1952 const WCHAR *start = *ptr;
1953 static const WCHAR slash[] = {'/',0};
1954 const BOOL is_file = data->scheme_type == URL_SCHEME_FILE;
1956 if(is_path_delim(**ptr)) {
1957 if(data->scheme_type == URL_SCHEME_WILDCARD) {
1958 /* Wildcard schemes don't get a '/' attached if their path is
1963 } else if(!(flags & Uri_CREATE_NO_CANONICALIZE)) {
1964 /* If the path component is empty, then a '/' is added. */
1969 while(!is_path_delim(**ptr)) {
1970 if(**ptr == '%' && data->scheme_type != URL_SCHEME_UNKNOWN && !is_file) {
1971 if(!check_pct_encoded(ptr)) {
1976 } else if(is_forbidden_dos_path_char(**ptr) && is_file &&
1977 (flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
1978 /* File schemes with USE_DOS_PATH set aren't allowed to have
1979 * a '<' or '>' or '\"' appear in them.
1983 } else if(**ptr == '\\') {
1984 /* Not allowed to have a backslash if NO_CANONICALIZE is set
1985 * and the scheme is known type (but not a file scheme).
1987 if(flags & Uri_CREATE_NO_CANONICALIZE) {
1988 if(data->scheme_type != URL_SCHEME_FILE &&
1989 data->scheme_type != URL_SCHEME_UNKNOWN) {
1999 /* The only time a URI doesn't have a path is when
2000 * the NO_CANONICALIZE flag is set and the raw URI
2001 * didn't contain one.
2008 data->path_len = *ptr - start;
2013 TRACE("(%p %p %x): Parsed path %s len=%d\n", ptr, data, flags,
2014 debugstr_wn(data->path, data->path_len), data->path_len);
2016 TRACE("(%p %p %x): The URI contained no path\n", ptr, data, flags);
2021 /* Parses the path of a opaque URI (much less strict then the parser
2022 * for a hierarchical URI).
2025 * Windows allows invalid % encoded data to appear in opaque URI paths
2026 * for unknown scheme types.
2028 * File schemes with USE_DOS_PATH set aren't allowed to have '<', '>', or '\"'
2031 static BOOL parse_path_opaque(const WCHAR **ptr, parse_data *data, DWORD flags) {
2032 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
2033 const BOOL is_file = data->scheme_type == URL_SCHEME_FILE;
2037 while(!is_path_delim(**ptr)) {
2038 if(**ptr == '%' && known_scheme) {
2039 if(!check_pct_encoded(ptr)) {
2045 } else if(is_forbidden_dos_path_char(**ptr) && is_file &&
2046 (flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
2055 data->path_len = *ptr - data->path;
2056 TRACE("(%p %p %x): Parsed opaque URI path %s len=%d\n", ptr, data, flags,
2057 debugstr_wn(data->path, data->path_len), data->path_len);
2061 /* Determines how the URI should be parsed after the scheme information.
2063 * If the scheme is followed, by "//" then, it is treated as an hierarchical URI
2064 * which then the authority and path information will be parsed out. Otherwise, the
2065 * URI will be treated as an opaque URI which the authority information is not parsed
2068 * RFC 3896 definition of hier-part:
2070 * hier-part = "//" authority path-abempty
2075 * MSDN opaque URI definition:
2076 * scheme ":" path [ "#" fragment ]
2079 * If the URI is of an unknown scheme type and has a "//" following the scheme then it
2080 * is treated as a hierarchical URI, but, if the CREATE_NO_CRACK_UNKNOWN_SCHEMES flag is
2081 * set then it is considered an opaque URI reguardless of what follows the scheme information
2082 * (per MSDN documentation).
2084 static BOOL parse_hierpart(const WCHAR **ptr, parse_data *data, DWORD flags) {
2085 const WCHAR *start = *ptr;
2087 /* Checks if the authority information needs to be parsed. */
2088 if(is_hierarchical_uri(ptr, data)) {
2089 /* Only treat it as a hierarchical URI if the scheme_type is known or
2090 * the Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES flag is not set.
2092 if(data->scheme_type != URL_SCHEME_UNKNOWN ||
2093 !(flags & Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES)) {
2094 TRACE("(%p %p %x): Treating URI as an hierarchical URI.\n", ptr, data, flags);
2095 data->is_opaque = FALSE;
2097 /* TODO: Handle hierarchical URI's, parse authority then parse the path. */
2098 if(!parse_authority(ptr, data, flags))
2101 return parse_path_hierarchical(ptr, data, flags);
2103 /* Reset ptr to it's starting position so opaque path parsing
2104 * begins at the correct location.
2109 /* If it reaches here, then the URI will be treated as an opaque
2113 TRACE("(%p %p %x): Treating URI as an opaque URI.\n", ptr, data, flags);
2115 data->is_opaque = TRUE;
2116 if(!parse_path_opaque(ptr, data, flags))
2122 /* Attempts to parse the query string from the URI.
2125 * If NO_DECODE_EXTRA_INFO flag is set, then invalid percent encoded
2126 * data is allowed appear in the query string. For unknown scheme types
2127 * invalid percent encoded data is allowed to appear reguardless.
2129 static BOOL parse_query(const WCHAR **ptr, parse_data *data, DWORD flags) {
2130 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
2133 TRACE("(%p %p %x): URI didn't contain a query string.\n", ptr, data, flags);
2140 while(**ptr && **ptr != '#') {
2141 if(**ptr == '%' && known_scheme &&
2142 !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
2143 if(!check_pct_encoded(ptr)) {
2154 data->query_len = *ptr - data->query;
2156 TRACE("(%p %p %x): Parsed query string %s len=%d\n", ptr, data, flags,
2157 debugstr_wn(data->query, data->query_len), data->query_len);
2161 /* Attempts to parse the fragment from the URI.
2164 * If NO_DECODE_EXTRA_INFO flag is set, then invalid percent encoded
2165 * data is allowed appear in the query string. For unknown scheme types
2166 * invalid percent encoded data is allowed to appear reguardless.
2168 static BOOL parse_fragment(const WCHAR **ptr, parse_data *data, DWORD flags) {
2169 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
2172 TRACE("(%p %p %x): URI didn't contain a fragment.\n", ptr, data, flags);
2176 data->fragment = *ptr;
2180 if(**ptr == '%' && known_scheme &&
2181 !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
2182 if(!check_pct_encoded(ptr)) {
2183 *ptr = data->fragment;
2184 data->fragment = NULL;
2193 data->fragment_len = *ptr - data->fragment;
2195 TRACE("(%p %p %x): Parsed fragment %s len=%d\n", ptr, data, flags,
2196 debugstr_wn(data->fragment, data->fragment_len), data->fragment_len);
2200 /* Parses and validates the components of the specified by data->uri
2201 * and stores the information it parses into 'data'.
2203 * Returns TRUE if it successfully parsed the URI. False otherwise.
2205 static BOOL parse_uri(parse_data *data, DWORD flags) {
2212 TRACE("(%p %x): BEGINNING TO PARSE URI %s.\n", data, flags, debugstr_w(data->uri));
2214 if(!parse_scheme(pptr, data, flags, 0))
2217 if(!parse_hierpart(pptr, data, flags))
2220 if(!parse_query(pptr, data, flags))
2223 if(!parse_fragment(pptr, data, flags))
2226 TRACE("(%p %x): FINISHED PARSING URI.\n", data, flags);
2230 static BOOL canonicalize_username(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2233 if(!data->username) {
2234 uri->userinfo_start = -1;
2238 uri->userinfo_start = uri->canon_len;
2239 for(ptr = data->username; ptr < data->username+data->username_len; ++ptr) {
2241 /* Only decode % encoded values for known scheme types. */
2242 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
2243 /* See if the value really needs decoded. */
2244 WCHAR val = decode_pct_val(ptr);
2245 if(is_unreserved(val)) {
2247 uri->canon_uri[uri->canon_len] = val;
2251 /* Move pass the hex characters. */
2256 } else if(!is_reserved(*ptr) && !is_unreserved(*ptr) && *ptr != '\\') {
2257 /* Only percent encode forbidden characters if the NO_ENCODE_FORBIDDEN_CHARACTERS flag
2260 if(!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS)) {
2262 pct_encode_val(*ptr, uri->canon_uri + uri->canon_len);
2264 uri->canon_len += 3;
2270 /* Nothing special, so just copy the character over. */
2271 uri->canon_uri[uri->canon_len] = *ptr;
2278 static BOOL canonicalize_password(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2281 if(!data->password) {
2282 uri->userinfo_split = -1;
2286 if(uri->userinfo_start == -1)
2287 /* Has a password, but, doesn't have a username. */
2288 uri->userinfo_start = uri->canon_len;
2290 uri->userinfo_split = uri->canon_len - uri->userinfo_start;
2292 /* Add the ':' to the userinfo component. */
2294 uri->canon_uri[uri->canon_len] = ':';
2297 for(ptr = data->password; ptr < data->password+data->password_len; ++ptr) {
2299 /* Only decode % encoded values for known scheme types. */
2300 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
2301 /* See if the value really needs decoded. */
2302 WCHAR val = decode_pct_val(ptr);
2303 if(is_unreserved(val)) {
2305 uri->canon_uri[uri->canon_len] = val;
2309 /* Move pass the hex characters. */
2314 } else if(!is_reserved(*ptr) && !is_unreserved(*ptr) && *ptr != '\\') {
2315 /* Only percent encode forbidden characters if the NO_ENCODE_FORBIDDEN_CHARACTERS flag
2318 if(!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS)) {
2320 pct_encode_val(*ptr, uri->canon_uri + uri->canon_len);
2322 uri->canon_len += 3;
2328 /* Nothing special, so just copy the character over. */
2329 uri->canon_uri[uri->canon_len] = *ptr;
2336 /* Canonicalizes the userinfo of the URI represented by the parse_data.
2338 * Canonicalization of the userinfo is a simple process. If there are any percent
2339 * encoded characters that fall in the "unreserved" character set, they are decoded
2340 * to their actual value. If a character is not in the "unreserved" or "reserved" sets
2341 * then it is percent encoded. Other than that the characters are copied over without
2344 static BOOL canonicalize_userinfo(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2345 uri->userinfo_start = uri->userinfo_split = -1;
2346 uri->userinfo_len = 0;
2348 if(!data->username && !data->password)
2349 /* URI doesn't have userinfo, so nothing to do here. */
2352 if(!canonicalize_username(data, uri, flags, computeOnly))
2355 if(!canonicalize_password(data, uri, flags, computeOnly))
2358 uri->userinfo_len = uri->canon_len - uri->userinfo_start;
2360 TRACE("(%p %p %x %d): Canonicalized userinfo, userinfo_start=%d, userinfo=%s, userinfo_split=%d userinfo_len=%d.\n",
2361 data, uri, flags, computeOnly, uri->userinfo_start, debugstr_wn(uri->canon_uri + uri->userinfo_start, uri->userinfo_len),
2362 uri->userinfo_split, uri->userinfo_len);
2364 /* Now insert the '@' after the userinfo. */
2366 uri->canon_uri[uri->canon_len] = '@';
2372 /* Attempts to canonicalize a reg_name.
2374 * Things that happen:
2375 * 1) If Uri_CREATE_NO_CANONICALIZE flag is not set, then the reg_name is
2376 * lower cased. Unless it's an unknown scheme type, which case it's
2377 * no lower cased reguardless.
2379 * 2) Unreserved % encoded characters are decoded for known
2382 * 3) Forbidden characters are % encoded as long as
2383 * Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS flag is not set and
2384 * it isn't an unknown scheme type.
2386 * 4) If it's a file scheme and the host is "localhost" it's removed.
2388 * 5) If it's a file scheme and Uri_CREATE_FILE_USE_DOS_PATH is set,
2389 * then the UNC path characters are added before the host name.
2391 static BOOL canonicalize_reg_name(const parse_data *data, Uri *uri,
2392 DWORD flags, BOOL computeOnly) {
2393 static const WCHAR localhostW[] =
2394 {'l','o','c','a','l','h','o','s','t',0};
2396 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
2398 if(data->scheme_type == URL_SCHEME_FILE &&
2399 data->host_len == lstrlenW(localhostW)) {
2400 if(!StrCmpNIW(data->host, localhostW, data->host_len)) {
2401 uri->host_start = -1;
2403 uri->host_type = Uri_HOST_UNKNOWN;
2408 if(data->scheme_type == URL_SCHEME_FILE && flags & Uri_CREATE_FILE_USE_DOS_PATH) {
2410 uri->canon_uri[uri->canon_len] = '\\';
2411 uri->canon_uri[uri->canon_len+1] = '\\';
2413 uri->canon_len += 2;
2414 uri->authority_start = uri->canon_len;
2417 uri->host_start = uri->canon_len;
2419 for(ptr = data->host; ptr < data->host+data->host_len; ++ptr) {
2420 if(*ptr == '%' && known_scheme) {
2421 WCHAR val = decode_pct_val(ptr);
2422 if(is_unreserved(val)) {
2423 /* If NO_CANONICALZE is not set, then windows lower cases the
2426 if(!(flags & Uri_CREATE_NO_CANONICALIZE) && isupperW(val)) {
2428 uri->canon_uri[uri->canon_len] = tolowerW(val);
2431 uri->canon_uri[uri->canon_len] = val;
2435 /* Skip past the % encoded character. */
2439 /* Just copy the % over. */
2441 uri->canon_uri[uri->canon_len] = *ptr;
2444 } else if(*ptr == '\\') {
2445 /* Only unknown scheme types could have made it here with a '\\' in the host name. */
2447 uri->canon_uri[uri->canon_len] = *ptr;
2449 } else if(!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS) &&
2450 !is_unreserved(*ptr) && !is_reserved(*ptr) && known_scheme) {
2452 pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
2454 /* The percent encoded value gets lower cased also. */
2455 if(!(flags & Uri_CREATE_NO_CANONICALIZE)) {
2456 uri->canon_uri[uri->canon_len+1] = tolowerW(uri->canon_uri[uri->canon_len+1]);
2457 uri->canon_uri[uri->canon_len+2] = tolowerW(uri->canon_uri[uri->canon_len+2]);
2461 uri->canon_len += 3;
2464 if(!(flags & Uri_CREATE_NO_CANONICALIZE) && known_scheme)
2465 uri->canon_uri[uri->canon_len] = tolowerW(*ptr);
2467 uri->canon_uri[uri->canon_len] = *ptr;
2474 uri->host_len = uri->canon_len - uri->host_start;
2477 TRACE("(%p %p %x %d): Canonicalize reg_name=%s len=%d\n", data, uri, flags,
2478 computeOnly, debugstr_wn(uri->canon_uri+uri->host_start, uri->host_len),
2482 find_domain_name(uri->canon_uri+uri->host_start, uri->host_len,
2483 &(uri->domain_offset));
2488 /* Attempts to canonicalize an implicit IPv4 address. */
2489 static BOOL canonicalize_implicit_ipv4address(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2490 uri->host_start = uri->canon_len;
2492 TRACE("%u\n", data->implicit_ipv4);
2493 /* For unknown scheme types Window's doesn't convert
2494 * the value into an IP address, but, it still considers
2495 * it an IPv4 address.
2497 if(data->scheme_type == URL_SCHEME_UNKNOWN) {
2499 memcpy(uri->canon_uri+uri->canon_len, data->host, data->host_len*sizeof(WCHAR));
2500 uri->canon_len += data->host_len;
2503 uri->canon_len += ui2ipv4(uri->canon_uri+uri->canon_len, data->implicit_ipv4);
2505 uri->canon_len += ui2ipv4(NULL, data->implicit_ipv4);
2508 uri->host_len = uri->canon_len - uri->host_start;
2509 uri->host_type = Uri_HOST_IPV4;
2512 TRACE("%p %p %x %d): Canonicalized implicit IP address=%s len=%d\n",
2513 data, uri, flags, computeOnly,
2514 debugstr_wn(uri->canon_uri+uri->host_start, uri->host_len),
2520 /* Attempts to canonicalize an IPv4 address.
2522 * If the parse_data represents a URI that has an implicit IPv4 address
2523 * (ex. http://256/, this function will convert 256 into 0.0.1.0). If
2524 * the implicit IP address exceeds the value of UINT_MAX (maximum value
2525 * for an IPv4 address) it's canonicalized as if were a reg-name.
2527 * If the parse_data contains a partial or full IPv4 address it normalizes it.
2528 * A partial IPv4 address is something like "192.0" and would be normalized to
2529 * "192.0.0.0". With a full (or partial) IPv4 address like "192.002.01.003" would
2530 * be normalized to "192.2.1.3".
2533 * Window's ONLY normalizes IPv4 address for known scheme types (one that isn't
2534 * URL_SCHEME_UNKNOWN). For unknown scheme types, it simply copies the data from
2535 * the original URI into the canonicalized URI, but, it still recognizes URI's
2536 * host type as HOST_IPV4.
2538 static BOOL canonicalize_ipv4address(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2539 if(data->has_implicit_ip)
2540 return canonicalize_implicit_ipv4address(data, uri, flags, computeOnly);
2542 uri->host_start = uri->canon_len;
2544 /* Windows only normalizes for known scheme types. */
2545 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
2546 /* parse_data contains a partial or full IPv4 address, so normalize it. */
2547 DWORD i, octetDigitCount = 0, octetCount = 0;
2548 BOOL octetHasDigit = FALSE;
2550 for(i = 0; i < data->host_len; ++i) {
2551 if(data->host[i] == '0' && !octetHasDigit) {
2552 /* Can ignore leading zeros if:
2553 * 1) It isn't the last digit of the octet.
2554 * 2) i+1 != data->host_len
2557 if(octetDigitCount == 2 ||
2558 i+1 == data->host_len ||
2559 data->host[i+1] == '.') {
2561 uri->canon_uri[uri->canon_len] = data->host[i];
2563 TRACE("Adding zero\n");
2565 } else if(data->host[i] == '.') {
2567 uri->canon_uri[uri->canon_len] = data->host[i];
2570 octetDigitCount = 0;
2571 octetHasDigit = FALSE;
2575 uri->canon_uri[uri->canon_len] = data->host[i];
2579 octetHasDigit = TRUE;
2583 /* Make sure the canonicalized IP address has 4 dec-octets.
2584 * If doesn't add "0" ones until there is 4;
2586 for( ; octetCount < 3; ++octetCount) {
2588 uri->canon_uri[uri->canon_len] = '.';
2589 uri->canon_uri[uri->canon_len+1] = '0';
2592 uri->canon_len += 2;
2595 /* Windows doesn't normalize addresses in unknown schemes. */
2597 memcpy(uri->canon_uri+uri->canon_len, data->host, data->host_len*sizeof(WCHAR));
2598 uri->canon_len += data->host_len;
2601 uri->host_len = uri->canon_len - uri->host_start;
2603 TRACE("(%p %p %x %d): Canonicalized IPv4 address, ip=%s len=%d\n",
2604 data, uri, flags, computeOnly,
2605 debugstr_wn(uri->canon_uri+uri->host_start, uri->host_len),
2612 /* Attempts to canonicalize the IPv6 address of the URI.
2614 * Multiple things happen during the canonicalization of an IPv6 address:
2615 * 1) Any leading zero's in an h16 component are removed.
2616 * Ex: [0001:0022::] -> [1:22::]
2618 * 2) The longest sequence of zero h16 components are compressed
2619 * into a "::" (elision). If there's a tie, the first is choosen.
2621 * Ex: [0:0:0:0:1:6:7:8] -> [::1:6:7:8]
2622 * [0:0:0:0:1:2::] -> [::1:2:0:0]
2623 * [0:0:1:2:0:0:7:8] -> [::1:2:0:0:7:8]
2625 * 3) If an IPv4 address is attached to the IPv6 address, it's
2627 * Ex: [::001.002.022.000] -> [::1.2.22.0]
2629 * 4) If an elision is present, but, only represents 1 h16 component
2632 * Ex: [1::2:3:4:5:6:7] -> [1:0:2:3:4:5:6:7]
2634 * 5) If the IPv6 address contains an IPv4 address and there exists
2635 * at least 1 non-zero h16 component the IPv4 address is converted
2636 * into two h16 components, otherwise it's normalized and kept as is.
2638 * Ex: [::192.200.003.4] -> [::192.200.3.4]
2639 * [ffff::192.200.003.4] -> [ffff::c0c8:3041]
2642 * For unknown scheme types Windows simply copies the address over without any
2645 * IPv4 address can be included in an elision if all its components are 0's.
2647 static BOOL canonicalize_ipv6address(const parse_data *data, Uri *uri,
2648 DWORD flags, BOOL computeOnly) {
2649 uri->host_start = uri->canon_len;
2651 if(data->scheme_type == URL_SCHEME_UNKNOWN) {
2653 memcpy(uri->canon_uri+uri->canon_len, data->host, data->host_len*sizeof(WCHAR));
2654 uri->canon_len += data->host_len;
2658 DWORD i, elision_len;
2660 if(!ipv6_to_number(&(data->ipv6_address), values)) {
2661 TRACE("(%p %p %x %d): Failed to compute numerical value for IPv6 address.\n",
2662 data, uri, flags, computeOnly);
2667 uri->canon_uri[uri->canon_len] = '[';
2670 /* Find where the elision should occur (if any). */
2671 compute_elision_location(&(data->ipv6_address), values, &elision_start, &elision_len);
2673 TRACE("%p %p %x %d): Elision starts at %d, len=%u\n", data, uri, flags,
2674 computeOnly, elision_start, elision_len);
2676 for(i = 0; i < 8; ++i) {
2677 BOOL in_elision = (elision_start > -1 && i >= elision_start &&
2678 i < elision_start+elision_len);
2679 BOOL do_ipv4 = (i == 6 && data->ipv6_address.ipv4 && !in_elision &&
2680 data->ipv6_address.h16_count == 0);
2682 if(i == elision_start) {
2684 uri->canon_uri[uri->canon_len] = ':';
2685 uri->canon_uri[uri->canon_len+1] = ':';
2687 uri->canon_len += 2;
2690 /* We can ignore the current component if we're in the elision. */
2694 /* We only add a ':' if we're not at i == 0, or when we're at
2695 * the very end of elision range since the ':' colon was handled
2696 * earlier. Otherwise we would end up with ":::" after elision.
2698 if(i != 0 && !(elision_start > -1 && i == elision_start+elision_len)) {
2700 uri->canon_uri[uri->canon_len] = ':';
2708 /* Combine the two parts of the IPv4 address values. */
2714 len = ui2ipv4(uri->canon_uri+uri->canon_len, val);
2716 len = ui2ipv4(NULL, val);
2718 uri->canon_len += len;
2721 /* Write a regular h16 component to the URI. */
2723 /* Short circuit for the trivial case. */
2724 if(values[i] == 0) {
2726 uri->canon_uri[uri->canon_len] = '0';
2729 static const WCHAR formatW[] = {'%','x',0};
2732 uri->canon_len += sprintfW(uri->canon_uri+uri->canon_len,
2733 formatW, values[i]);
2736 uri->canon_len += sprintfW(tmp, formatW, values[i]);
2742 /* Add the closing ']'. */
2744 uri->canon_uri[uri->canon_len] = ']';
2748 uri->host_len = uri->canon_len - uri->host_start;
2751 TRACE("(%p %p %x %d): Canonicalized IPv6 address %s, len=%d\n", data, uri, flags,
2752 computeOnly, debugstr_wn(uri->canon_uri+uri->host_start, uri->host_len),
2758 /* Attempts to canonicalize the host of the URI (if any). */
2759 static BOOL canonicalize_host(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2760 uri->host_start = -1;
2762 uri->domain_offset = -1;
2765 switch(data->host_type) {
2767 uri->host_type = Uri_HOST_DNS;
2768 if(!canonicalize_reg_name(data, uri, flags, computeOnly))
2773 uri->host_type = Uri_HOST_IPV4;
2774 if(!canonicalize_ipv4address(data, uri, flags, computeOnly))
2779 if(!canonicalize_ipv6address(data, uri, flags, computeOnly))
2782 uri->host_type = Uri_HOST_IPV6;
2784 case Uri_HOST_UNKNOWN:
2785 if(data->host_len > 0 || data->scheme_type != URL_SCHEME_FILE) {
2786 uri->host_start = uri->canon_len;
2788 /* Nothing happens to unknown host types. */
2790 memcpy(uri->canon_uri+uri->canon_len, data->host, data->host_len*sizeof(WCHAR));
2791 uri->canon_len += data->host_len;
2792 uri->host_len = data->host_len;
2795 uri->host_type = Uri_HOST_UNKNOWN;
2798 FIXME("(%p %p %x %d): Canonicalization for host type %d not supported.\n", data,
2799 uri, flags, computeOnly, data->host_type);
2807 static BOOL canonicalize_port(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2808 BOOL has_default_port = FALSE;
2809 USHORT default_port = 0;
2812 uri->port_offset = -1;
2814 /* Check if the scheme has a default port. */
2815 for(i = 0; i < sizeof(default_ports)/sizeof(default_ports[0]); ++i) {
2816 if(default_ports[i].scheme == data->scheme_type) {
2817 has_default_port = TRUE;
2818 default_port = default_ports[i].port;
2823 uri->has_port = data->has_port || has_default_port;
2826 * 1) Has a port which is the default port.
2827 * 2) Has a port (not the default).
2828 * 3) Doesn't have a port, but, scheme has a default port.
2831 if(has_default_port && data->has_port && data->port_value == default_port) {
2832 /* If it's the default port and this flag isn't set, don't do anything. */
2833 if(flags & Uri_CREATE_NO_CANONICALIZE) {
2834 uri->port_offset = uri->canon_len-uri->authority_start;
2836 uri->canon_uri[uri->canon_len] = ':';
2840 /* Copy the original port over. */
2842 memcpy(uri->canon_uri+uri->canon_len, data->port, data->port_len*sizeof(WCHAR));
2843 uri->canon_len += data->port_len;
2846 uri->canon_len += ui2str(uri->canon_uri+uri->canon_len, data->port_value);
2848 uri->canon_len += ui2str(NULL, data->port_value);
2852 uri->port = default_port;
2853 } else if(data->has_port) {
2854 uri->port_offset = uri->canon_len-uri->authority_start;
2856 uri->canon_uri[uri->canon_len] = ':';
2859 if(flags & Uri_CREATE_NO_CANONICALIZE && data->port) {
2860 /* Copy the original over without changes. */
2862 memcpy(uri->canon_uri+uri->canon_len, data->port, data->port_len*sizeof(WCHAR));
2863 uri->canon_len += data->port_len;
2866 uri->canon_len += ui2str(uri->canon_uri+uri->canon_len, data->port_value);
2868 uri->canon_len += ui2str(NULL, data->port_value);
2871 uri->port = data->port_value;
2872 } else if(has_default_port)
2873 uri->port = default_port;
2878 /* Canonicalizes the authority of the URI represented by the parse_data. */
2879 static BOOL canonicalize_authority(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2880 uri->authority_start = uri->canon_len;
2881 uri->authority_len = 0;
2883 if(!canonicalize_userinfo(data, uri, flags, computeOnly))
2886 if(!canonicalize_host(data, uri, flags, computeOnly))
2889 if(!canonicalize_port(data, uri, flags, computeOnly))
2892 if(uri->host_start != -1 || (data->is_relative && (data->password || data->username)))
2893 uri->authority_len = uri->canon_len - uri->authority_start;
2895 uri->authority_start = -1;
2900 /* Attempts to canonicalize the path of a hierarchical URI.
2902 * Things that happen:
2903 * 1). Forbidden characters are percent encoded, unless the NO_ENCODE_FORBIDDEN
2904 * flag is set or it's a file URI. Forbidden characters are always encoded
2905 * for file schemes reguardless and forbidden characters are never encoded
2906 * for unknown scheme types.
2908 * 2). For known scheme types '\\' are changed to '/'.
2910 * 3). Percent encoded, unreserved characters are decoded to their actual values.
2911 * Unless the scheme type is unknown. For file schemes any percent encoded
2912 * character in the unreserved or reserved set is decoded.
2914 * 4). For File schemes if the path is starts with a drive letter and doesn't
2915 * start with a '/' then one is appended.
2916 * Ex: file://c:/test.mp3 -> file:///c:/test.mp3
2918 * 5). Dot segments are removed from the path for all scheme types
2919 * unless NO_CANONICALIZE flag is set. Dot segments aren't removed
2920 * for wildcard scheme types.
2923 * file://c:/test%20test -> file:///c:/test%2520test
2924 * file://c:/test%3Etest -> file:///c:/test%253Etest
2925 * if Uri_CREATE_FILE_USE_DOS_PATH is not set:
2926 * file:///c:/test%20test -> file:///c:/test%20test
2927 * file:///c:/test%test -> file:///c:/test%25test
2929 static BOOL canonicalize_path_hierarchical(const parse_data *data, Uri *uri,
2930 DWORD flags, BOOL computeOnly) {
2932 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
2933 const BOOL is_file = data->scheme_type == URL_SCHEME_FILE;
2934 const BOOL is_res = data->scheme_type == URL_SCHEME_RES;
2936 BOOL escape_pct = FALSE;
2939 uri->path_start = -1;
2944 uri->path_start = uri->canon_len;
2947 if(is_file && uri->host_start == -1) {
2948 /* Check if a '/' needs to be appended for the file scheme. */
2949 if(data->path_len > 1 && is_drive_path(ptr) && !(flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
2951 uri->canon_uri[uri->canon_len] = '/';
2954 } else if(*ptr == '/') {
2955 if(!(flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
2956 /* Copy the extra '/' over. */
2958 uri->canon_uri[uri->canon_len] = '/';
2964 if(is_drive_path(ptr)) {
2966 uri->canon_uri[uri->canon_len] = *ptr;
2967 /* If theres a '|' after the drive letter, convert it to a ':'. */
2968 uri->canon_uri[uri->canon_len+1] = ':';
2971 uri->canon_len += 2;
2975 if(!is_file && *(data->path) && *(data->path) != '/') {
2976 /* Prepend a '/' to the path if it doesn't have one. */
2978 uri->canon_uri[uri->canon_len] = '/';
2982 for(; ptr < data->path+data->path_len; ++ptr) {
2983 BOOL do_default_action = TRUE;
2985 if(*ptr == '%' && !is_res) {
2986 const WCHAR *tmp = ptr;
2989 /* Check if the % represents a valid encoded char, or if it needs encoded. */
2990 BOOL force_encode = !check_pct_encoded(&tmp) && is_file && !(flags&Uri_CREATE_FILE_USE_DOS_PATH);
2991 val = decode_pct_val(ptr);
2993 if(force_encode || escape_pct) {
2994 /* Escape the percent sign in the file URI. */
2996 pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
2997 uri->canon_len += 3;
2998 do_default_action = FALSE;
2999 } else if((is_unreserved(val) && known_scheme) ||
3000 (is_file && (is_unreserved(val) || is_reserved(val) ||
3001 (val && flags&Uri_CREATE_FILE_USE_DOS_PATH && !is_forbidden_dos_path_char(val))))) {
3003 uri->canon_uri[uri->canon_len] = val;
3009 } else if(*ptr == '/' && is_file && (flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
3010 /* Convert the '/' back to a '\\'. */
3012 uri->canon_uri[uri->canon_len] = '\\';
3014 do_default_action = FALSE;
3015 } else if(*ptr == '\\' && known_scheme) {
3016 if(!(is_file && (flags & Uri_CREATE_FILE_USE_DOS_PATH))) {
3017 /* Convert '\\' into a '/'. */
3019 uri->canon_uri[uri->canon_len] = '/';
3021 do_default_action = FALSE;
3023 } else if(known_scheme && !is_res && !is_unreserved(*ptr) && !is_reserved(*ptr) &&
3024 (!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS) || is_file)) {
3025 if(!(is_file && (flags & Uri_CREATE_FILE_USE_DOS_PATH))) {
3026 /* Escape the forbidden character. */
3028 pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
3029 uri->canon_len += 3;
3030 do_default_action = FALSE;
3034 if(do_default_action) {
3036 uri->canon_uri[uri->canon_len] = *ptr;
3041 uri->path_len = uri->canon_len - uri->path_start;
3043 /* Removing the dot segments only happens when it's not in
3044 * computeOnly mode and it's not a wildcard scheme. File schemes
3045 * with USE_DOS_PATH set don't get dot segments removed.
3047 if(!(is_file && (flags & Uri_CREATE_FILE_USE_DOS_PATH)) &&
3048 data->scheme_type != URL_SCHEME_WILDCARD) {
3049 if(!(flags & Uri_CREATE_NO_CANONICALIZE) && !computeOnly) {
3050 /* Remove the dot segments (if any) and reset everything to the new
3053 DWORD new_len = remove_dot_segments(uri->canon_uri+uri->path_start, uri->path_len);
3054 uri->canon_len -= uri->path_len-new_len;
3055 uri->path_len = new_len;
3060 TRACE("Canonicalized path %s len=%d\n",
3061 debugstr_wn(uri->canon_uri+uri->path_start, uri->path_len),
3067 /* Attempts to canonicalize the path for an opaque URI.
3069 * For known scheme types:
3070 * 1) forbidden characters are percent encoded if
3071 * NO_ENCODE_FORBIDDEN_CHARACTERS isn't set.
3073 * 2) Percent encoded, unreserved characters are decoded
3074 * to their actual values, for known scheme types.
3076 * 3) '\\' are changed to '/' for known scheme types
3077 * except for mailto schemes.
3079 * 4) For file schemes, if USE_DOS_PATH is set all '/'
3080 * are converted to backslashes.
3082 * 5) For file schemes, if USE_DOS_PATH isn't set all '\'
3083 * are converted to forward slashes.
3085 static BOOL canonicalize_path_opaque(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
3087 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
3088 const BOOL is_file = data->scheme_type == URL_SCHEME_FILE;
3091 uri->path_start = -1;
3096 uri->path_start = uri->canon_len;
3098 /* Windows doesn't allow a "//" to appear after the scheme
3099 * of a URI, if it's an opaque URI.
3101 if(data->scheme && *(data->path) == '/' && *(data->path+1) == '/') {
3102 /* So it inserts a "/." before the "//" if it exists. */
3104 uri->canon_uri[uri->canon_len] = '/';
3105 uri->canon_uri[uri->canon_len+1] = '.';
3108 uri->canon_len += 2;
3111 for(ptr = data->path; ptr < data->path+data->path_len; ++ptr) {
3112 BOOL do_default_action = TRUE;
3114 if(*ptr == '%' && known_scheme) {
3115 WCHAR val = decode_pct_val(ptr);
3117 if(is_unreserved(val)) {
3119 uri->canon_uri[uri->canon_len] = val;
3125 } else if(*ptr == '/' && is_file && (flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
3127 uri->canon_uri[uri->canon_len] = '\\';
3129 do_default_action = FALSE;
3130 } else if(*ptr == '\\') {
3131 if(is_file && !(flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
3132 /* Convert to a '/'. */
3134 uri->canon_uri[uri->canon_len] = '/';
3136 do_default_action = FALSE;
3138 } else if(known_scheme && !is_unreserved(*ptr) && !is_reserved(*ptr) &&
3139 !(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS)) {
3140 if(!(is_file && (flags & Uri_CREATE_FILE_USE_DOS_PATH))) {
3142 pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
3143 uri->canon_len += 3;
3144 do_default_action = FALSE;
3148 if(do_default_action) {
3150 uri->canon_uri[uri->canon_len] = *ptr;
3155 if(data->scheme_type == URL_SCHEME_MK && !computeOnly && !(flags & Uri_CREATE_NO_CANONICALIZE)) {
3156 DWORD new_len = remove_dot_segments(uri->canon_uri + uri->path_start,
3157 uri->canon_len - uri->path_start);
3158 uri->canon_len = uri->path_start + new_len;
3161 uri->path_len = uri->canon_len - uri->path_start;
3163 TRACE("(%p %p %x %d): Canonicalized opaque URI path %s len=%d\n", data, uri, flags, computeOnly,
3164 debugstr_wn(uri->canon_uri+uri->path_start, uri->path_len), uri->path_len);
3168 /* Determines how the URI represented by the parse_data should be canonicalized.
3170 * Essentially, if the parse_data represents an hierarchical URI then it calls
3171 * canonicalize_authority and the canonicalization functions for the path. If the
3172 * URI is opaque it canonicalizes the path of the URI.
3174 static BOOL canonicalize_hierpart(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
3175 if(!data->is_opaque || (data->is_relative && (data->password || data->username))) {
3176 /* "//" is only added for non-wildcard scheme types.
3178 * A "//" is only added to a relative URI if it has a
3179 * host or port component (this only happens if a IUriBuilder
3180 * is generating an IUri).
3182 if((data->is_relative && (data->host || data->has_port)) ||
3183 (!data->is_relative && data->scheme_type != URL_SCHEME_WILDCARD)) {
3185 INT pos = uri->canon_len;
3187 uri->canon_uri[pos] = '/';
3188 uri->canon_uri[pos+1] = '/';
3190 uri->canon_len += 2;
3193 if(!canonicalize_authority(data, uri, flags, computeOnly))
3196 if(data->is_relative && (data->password || data->username)) {
3197 if(!canonicalize_path_opaque(data, uri, flags, computeOnly))
3200 if(!canonicalize_path_hierarchical(data, uri, flags, computeOnly))
3204 /* Opaque URI's don't have an authority. */
3205 uri->userinfo_start = uri->userinfo_split = -1;
3206 uri->userinfo_len = 0;
3207 uri->host_start = -1;
3209 uri->host_type = Uri_HOST_UNKNOWN;
3210 uri->has_port = FALSE;
3211 uri->authority_start = -1;
3212 uri->authority_len = 0;
3213 uri->domain_offset = -1;
3214 uri->port_offset = -1;
3216 if(is_hierarchical_scheme(data->scheme_type)) {
3219 /* Absolute URIs aren't displayed for known scheme types
3220 * which should be hierarchical URIs.
3222 uri->display_modifiers |= URI_DISPLAY_NO_ABSOLUTE_URI;
3224 /* Windows also sets the port for these (if they have one). */
3225 for(i = 0; i < sizeof(default_ports)/sizeof(default_ports[0]); ++i) {
3226 if(data->scheme_type == default_ports[i].scheme) {
3227 uri->has_port = TRUE;
3228 uri->port = default_ports[i].port;
3234 if(!canonicalize_path_opaque(data, uri, flags, computeOnly))
3238 if(uri->path_start > -1 && !computeOnly)
3239 /* Finding file extensions happens for both types of URIs. */
3240 uri->extension_offset = find_file_extension(uri->canon_uri+uri->path_start, uri->path_len);
3242 uri->extension_offset = -1;
3247 /* Attempts to canonicalize the query string of the URI.
3249 * Things that happen:
3250 * 1) For known scheme types forbidden characters
3251 * are percent encoded, unless the NO_DECODE_EXTRA_INFO flag is set
3252 * or NO_ENCODE_FORBIDDEN_CHARACTERS is set.
3254 * 2) For known scheme types, percent encoded, unreserved characters
3255 * are decoded as long as the NO_DECODE_EXTRA_INFO flag isn't set.
3257 static BOOL canonicalize_query(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
3258 const WCHAR *ptr, *end;
3259 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
3262 uri->query_start = -1;
3267 uri->query_start = uri->canon_len;
3269 end = data->query+data->query_len;
3270 for(ptr = data->query; ptr < end; ++ptr) {
3272 if(known_scheme && !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
3273 WCHAR val = decode_pct_val(ptr);
3274 if(is_unreserved(val)) {
3276 uri->canon_uri[uri->canon_len] = val;
3283 } else if(known_scheme && !is_unreserved(*ptr) && !is_reserved(*ptr)) {
3284 if(!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS) &&
3285 !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
3287 pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
3288 uri->canon_len += 3;
3294 uri->canon_uri[uri->canon_len] = *ptr;
3298 uri->query_len = uri->canon_len - uri->query_start;
3301 TRACE("(%p %p %x %d): Canonicalized query string %s len=%d\n", data, uri, flags,
3302 computeOnly, debugstr_wn(uri->canon_uri+uri->query_start, uri->query_len),
3307 static BOOL canonicalize_fragment(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
3308 const WCHAR *ptr, *end;
3309 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
3311 if(!data->fragment) {
3312 uri->fragment_start = -1;
3313 uri->fragment_len = 0;
3317 uri->fragment_start = uri->canon_len;
3319 end = data->fragment + data->fragment_len;
3320 for(ptr = data->fragment; ptr < end; ++ptr) {
3322 if(known_scheme && !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
3323 WCHAR val = decode_pct_val(ptr);
3324 if(is_unreserved(val)) {
3326 uri->canon_uri[uri->canon_len] = val;
3333 } else if(known_scheme && !is_unreserved(*ptr) && !is_reserved(*ptr)) {
3334 if(!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS) &&
3335 !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
3337 pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
3338 uri->canon_len += 3;
3344 uri->canon_uri[uri->canon_len] = *ptr;
3348 uri->fragment_len = uri->canon_len - uri->fragment_start;
3351 TRACE("(%p %p %x %d): Canonicalized fragment %s len=%d\n", data, uri, flags,
3352 computeOnly, debugstr_wn(uri->canon_uri+uri->fragment_start, uri->fragment_len),
3357 /* Canonicalizes the scheme information specified in the parse_data using the specified flags. */
3358 static BOOL canonicalize_scheme(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
3359 uri->scheme_start = -1;
3360 uri->scheme_len = 0;
3363 /* The only type of URI that doesn't have to have a scheme is a relative
3366 if(!data->is_relative) {
3367 FIXME("(%p %p %x): Unable to determine the scheme type of %s.\n", data,
3368 uri, flags, debugstr_w(data->uri));
3374 INT pos = uri->canon_len;
3376 for(i = 0; i < data->scheme_len; ++i) {
3377 /* Scheme name must be lower case after canonicalization. */
3378 uri->canon_uri[i + pos] = tolowerW(data->scheme[i]);
3381 uri->canon_uri[i + pos] = ':';
3382 uri->scheme_start = pos;
3384 TRACE("(%p %p %x): Canonicalized scheme=%s, len=%d.\n", data, uri, flags,
3385 debugstr_wn(uri->canon_uri, uri->scheme_len), data->scheme_len);
3388 /* This happens in both computation modes. */
3389 uri->canon_len += data->scheme_len + 1;
3390 uri->scheme_len = data->scheme_len;
3395 /* Compute's what the length of the URI specified by the parse_data will be
3396 * after canonicalization occurs using the specified flags.
3398 * This function will return a non-zero value indicating the length of the canonicalized
3399 * URI, or -1 on error.
3401 static int compute_canonicalized_length(const parse_data *data, DWORD flags) {
3404 memset(&uri, 0, sizeof(Uri));
3406 TRACE("(%p %x): Beginning to compute canonicalized length for URI %s\n", data, flags,
3407 debugstr_w(data->uri));
3409 if(!canonicalize_scheme(data, &uri, flags, TRUE)) {
3410 ERR("(%p %x): Failed to compute URI scheme length.\n", data, flags);
3414 if(!canonicalize_hierpart(data, &uri, flags, TRUE)) {
3415 ERR("(%p %x): Failed to compute URI hierpart length.\n", data, flags);
3419 if(!canonicalize_query(data, &uri, flags, TRUE)) {
3420 ERR("(%p %x): Failed to compute query string length.\n", data, flags);
3424 if(!canonicalize_fragment(data, &uri, flags, TRUE)) {
3425 ERR("(%p %x): Failed to compute fragment length.\n", data, flags);
3429 TRACE("(%p %x): Finished computing canonicalized URI length. length=%d\n", data, flags, uri.canon_len);
3431 return uri.canon_len;
3434 /* Canonicalizes the URI data specified in the parse_data, using the given flags. If the
3435 * canonicalization succeededs it will store all the canonicalization information
3436 * in the pointer to the Uri.
3438 * To canonicalize a URI this function first computes what the length of the URI
3439 * specified by the parse_data will be. Once this is done it will then perfom the actual
3440 * canonicalization of the URI.
3442 static HRESULT canonicalize_uri(const parse_data *data, Uri *uri, DWORD flags) {
3445 uri->canon_uri = NULL;
3446 len = uri->canon_size = uri->canon_len = 0;
3448 TRACE("(%p %p %x): beginning to canonicalize URI %s.\n", data, uri, flags, debugstr_w(data->uri));
3450 /* First try to compute the length of the URI. */
3451 len = compute_canonicalized_length(data, flags);
3453 ERR("(%p %p %x): Could not compute the canonicalized length of %s.\n", data, uri, flags,
3454 debugstr_w(data->uri));
3455 return E_INVALIDARG;
3458 uri->canon_uri = heap_alloc((len+1)*sizeof(WCHAR));
3460 return E_OUTOFMEMORY;
3462 uri->canon_size = len;
3463 if(!canonicalize_scheme(data, uri, flags, FALSE)) {
3464 ERR("(%p %p %x): Unable to canonicalize the scheme of the URI.\n", data, uri, flags);
3465 return E_INVALIDARG;
3467 uri->scheme_type = data->scheme_type;
3469 if(!canonicalize_hierpart(data, uri, flags, FALSE)) {
3470 ERR("(%p %p %x): Unable to canonicalize the heirpart of the URI\n", data, uri, flags);
3471 return E_INVALIDARG;
3474 if(!canonicalize_query(data, uri, flags, FALSE)) {
3475 ERR("(%p %p %x): Unable to canonicalize query string of the URI.\n",
3477 return E_INVALIDARG;
3480 if(!canonicalize_fragment(data, uri, flags, FALSE)) {
3481 ERR("(%p %p %x): Unable to canonicalize fragment of the URI.\n",
3483 return E_INVALIDARG;
3486 /* There's a possibility we didn't use all the space we allocated
3489 if(uri->canon_len < uri->canon_size) {
3490 /* This happens if the URI is hierarchical and dot
3491 * segments were removed from it's path.
3493 WCHAR *tmp = heap_realloc(uri->canon_uri, (uri->canon_len+1)*sizeof(WCHAR));
3495 return E_OUTOFMEMORY;
3497 uri->canon_uri = tmp;
3498 uri->canon_size = uri->canon_len;
3501 uri->canon_uri[uri->canon_len] = '\0';
3502 TRACE("(%p %p %x): finished canonicalizing the URI. uri=%s\n", data, uri, flags, debugstr_w(uri->canon_uri));
3507 static HRESULT get_builder_component(LPWSTR *component, DWORD *component_len,
3508 LPCWSTR source, DWORD source_len,
3509 LPCWSTR *output, DWORD *output_len)
3522 if(!(*component) && source) {
3523 /* Allocate 'component', and copy the contents from 'source'
3524 * into the new allocation.
3526 *component = heap_alloc((source_len+1)*sizeof(WCHAR));
3528 return E_OUTOFMEMORY;
3530 memcpy(*component, source, source_len*sizeof(WCHAR));
3531 (*component)[source_len] = '\0';
3532 *component_len = source_len;
3535 *output = *component;
3536 *output_len = *component_len;
3537 return *output ? S_OK : S_FALSE;
3540 /* Allocates 'component' and copies the string from 'new_value' into 'component'.
3541 * If 'prefix' is set and 'new_value' isn't NULL, then it checks if 'new_value'
3542 * starts with 'prefix'. If it doesn't then 'prefix' is prepended to 'component'.
3544 * If everything is successful, then will set 'success_flag' in 'flags'.
3546 static HRESULT set_builder_component(LPWSTR *component, DWORD *component_len, LPCWSTR new_value,
3547 WCHAR prefix, DWORD *flags, DWORD success_flag)
3549 heap_free(*component);
3555 BOOL add_prefix = FALSE;
3556 DWORD len = lstrlenW(new_value);
3559 if(prefix && *new_value != prefix) {
3561 *component = heap_alloc((len+2)*sizeof(WCHAR));
3563 *component = heap_alloc((len+1)*sizeof(WCHAR));
3566 return E_OUTOFMEMORY;
3569 (*component)[pos++] = prefix;
3571 memcpy(*component+pos, new_value, (len+1)*sizeof(WCHAR));
3572 *component_len = len+pos;
3575 *flags |= success_flag;
3579 static void reset_builder(UriBuilder *builder) {
3581 IUri_Release(&builder->uri->IUri_iface);
3582 builder->uri = NULL;
3584 heap_free(builder->fragment);
3585 builder->fragment = NULL;
3586 builder->fragment_len = 0;
3588 heap_free(builder->host);
3589 builder->host = NULL;
3590 builder->host_len = 0;
3592 heap_free(builder->password);
3593 builder->password = NULL;
3594 builder->password_len = 0;
3596 heap_free(builder->path);
3597 builder->path = NULL;
3598 builder->path_len = 0;
3600 heap_free(builder->query);
3601 builder->query = NULL;
3602 builder->query_len = 0;
3604 heap_free(builder->scheme);
3605 builder->scheme = NULL;
3606 builder->scheme_len = 0;
3608 heap_free(builder->username);
3609 builder->username = NULL;
3610 builder->username_len = 0;
3612 builder->has_port = FALSE;
3614 builder->modified_props = 0;
3617 static HRESULT validate_scheme_name(const UriBuilder *builder, parse_data *data, DWORD flags) {
3618 const WCHAR *component;
3623 if(builder->scheme) {
3624 ptr = builder->scheme;
3625 expected_len = builder->scheme_len;
3626 } else if(builder->uri && builder->uri->scheme_start > -1) {
3627 ptr = builder->uri->canon_uri+builder->uri->scheme_start;
3628 expected_len = builder->uri->scheme_len;
3630 static const WCHAR nullW[] = {0};
3637 if(parse_scheme(pptr, data, flags, ALLOW_NULL_TERM_SCHEME) &&
3638 data->scheme_len == expected_len) {
3640 TRACE("(%p %p %x): Found valid scheme component %s len=%d.\n", builder, data, flags,
3641 debugstr_wn(data->scheme, data->scheme_len), data->scheme_len);
3643 TRACE("(%p %p %x): Invalid scheme component found %s.\n", builder, data, flags,
3644 debugstr_wn(component, expected_len));
3645 return INET_E_INVALID_URL;
3651 static HRESULT validate_username(const UriBuilder *builder, parse_data *data, DWORD flags) {
3656 if(builder->username) {
3657 ptr = builder->username;
3658 expected_len = builder->username_len;
3659 } else if(!(builder->modified_props & Uri_HAS_USER_NAME) && builder->uri &&
3660 builder->uri->userinfo_start > -1 && builder->uri->userinfo_split != 0) {
3661 /* Just use the username from the base Uri. */
3662 data->username = builder->uri->canon_uri+builder->uri->userinfo_start;
3663 data->username_len = (builder->uri->userinfo_split > -1) ?
3664 builder->uri->userinfo_split : builder->uri->userinfo_len;
3672 const WCHAR *component = ptr;
3674 if(parse_username(pptr, data, flags, ALLOW_NULL_TERM_USER_NAME) &&
3675 data->username_len == expected_len)
3676 TRACE("(%p %p %x): Found valid username component %s len=%d.\n", builder, data, flags,
3677 debugstr_wn(data->username, data->username_len), data->username_len);
3679 TRACE("(%p %p %x): Invalid username component found %s.\n", builder, data, flags,
3680 debugstr_wn(component, expected_len));
3681 return INET_E_INVALID_URL;
3688 static HRESULT validate_password(const UriBuilder *builder, parse_data *data, DWORD flags) {
3693 if(builder->password) {
3694 ptr = builder->password;
3695 expected_len = builder->password_len;
3696 } else if(!(builder->modified_props & Uri_HAS_PASSWORD) && builder->uri &&
3697 builder->uri->userinfo_split > -1) {
3698 data->password = builder->uri->canon_uri+builder->uri->userinfo_start+builder->uri->userinfo_split+1;
3699 data->password_len = builder->uri->userinfo_len-builder->uri->userinfo_split-1;
3707 const WCHAR *component = ptr;
3709 if(parse_password(pptr, data, flags, ALLOW_NULL_TERM_PASSWORD) &&
3710 data->password_len == expected_len)
3711 TRACE("(%p %p %x): Found valid password component %s len=%d.\n", builder, data, flags,
3712 debugstr_wn(data->password, data->password_len), data->password_len);
3714 TRACE("(%p %p %x): Invalid password component found %s.\n", builder, data, flags,
3715 debugstr_wn(component, expected_len));
3716 return INET_E_INVALID_URL;
3723 static HRESULT validate_userinfo(const UriBuilder *builder, parse_data *data, DWORD flags) {
3726 hr = validate_username(builder, data, flags);
3730 hr = validate_password(builder, data, flags);
3737 static HRESULT validate_host(const UriBuilder *builder, parse_data *data, DWORD flags) {
3743 ptr = builder->host;
3744 expected_len = builder->host_len;
3745 } else if(!(builder->modified_props & Uri_HAS_HOST) && builder->uri && builder->uri->host_start > -1) {
3746 ptr = builder->uri->canon_uri + builder->uri->host_start;
3747 expected_len = builder->uri->host_len;
3752 const WCHAR *component = ptr;
3753 DWORD extras = ALLOW_BRACKETLESS_IP_LITERAL|IGNORE_PORT_DELIMITER|SKIP_IP_FUTURE_CHECK;
3756 if(parse_host(pptr, data, flags, extras) && data->host_len == expected_len)
3757 TRACE("(%p %p %x): Found valid host name %s len=%d type=%d.\n", builder, data, flags,
3758 debugstr_wn(data->host, data->host_len), data->host_len, data->host_type);
3760 TRACE("(%p %p %x): Invalid host name found %s.\n", builder, data, flags,
3761 debugstr_wn(component, expected_len));
3762 return INET_E_INVALID_URL;
3769 static void setup_port(const UriBuilder *builder, parse_data *data, DWORD flags) {
3770 if(builder->modified_props & Uri_HAS_PORT) {
3771 if(builder->has_port) {
3772 data->has_port = TRUE;
3773 data->port_value = builder->port;
3775 } else if(builder->uri && builder->uri->has_port) {
3776 data->has_port = TRUE;
3777 data->port_value = builder->uri->port;
3781 TRACE("(%p %p %x): Using %u as port for IUri.\n", builder, data, flags, data->port_value);
3784 static HRESULT validate_path(const UriBuilder *builder, parse_data *data, DWORD flags) {
3785 const WCHAR *ptr = NULL;
3786 const WCHAR *component;
3789 BOOL check_len = TRUE;
3793 ptr = builder->path;
3794 expected_len = builder->path_len;
3795 } else if(!(builder->modified_props & Uri_HAS_PATH) &&
3796 builder->uri && builder->uri->path_start > -1) {
3797 ptr = builder->uri->canon_uri+builder->uri->path_start;
3798 expected_len = builder->uri->path_len;
3800 static const WCHAR nullW[] = {0};
3808 /* How the path is validated depends on what type of
3811 valid = data->is_opaque ?
3812 parse_path_opaque(pptr, data, flags) : parse_path_hierarchical(pptr, data, flags);
3814 if(!valid || (check_len && expected_len != data->path_len)) {
3815 TRACE("(%p %p %x): Invalid path component %s.\n", builder, data, flags,
3816 debugstr_wn(component, check_len ? expected_len : -1) );
3817 return INET_E_INVALID_URL;
3820 TRACE("(%p %p %x): Valid path component %s len=%d.\n", builder, data, flags,
3821 debugstr_wn(data->path, data->path_len), data->path_len);
3826 static HRESULT validate_query(const UriBuilder *builder, parse_data *data, DWORD flags) {
3827 const WCHAR *ptr = NULL;
3831 if(builder->query) {
3832 ptr = builder->query;
3833 expected_len = builder->query_len;
3834 } else if(!(builder->modified_props & Uri_HAS_QUERY) && builder->uri &&
3835 builder->uri->query_start > -1) {
3836 ptr = builder->uri->canon_uri+builder->uri->query_start;
3837 expected_len = builder->uri->query_len;
3841 const WCHAR *component = ptr;
3844 if(parse_query(pptr, data, flags) && expected_len == data->query_len)
3845 TRACE("(%p %p %x): Valid query component %s len=%d.\n", builder, data, flags,
3846 debugstr_wn(data->query, data->query_len), data->query_len);
3848 TRACE("(%p %p %x): Invalid query component %s.\n", builder, data, flags,
3849 debugstr_wn(component, expected_len));
3850 return INET_E_INVALID_URL;
3857 static HRESULT validate_fragment(const UriBuilder *builder, parse_data *data, DWORD flags) {
3858 const WCHAR *ptr = NULL;
3862 if(builder->fragment) {
3863 ptr = builder->fragment;
3864 expected_len = builder->fragment_len;
3865 } else if(!(builder->modified_props & Uri_HAS_FRAGMENT) && builder->uri &&
3866 builder->uri->fragment_start > -1) {
3867 ptr = builder->uri->canon_uri+builder->uri->fragment_start;
3868 expected_len = builder->uri->fragment_len;
3872 const WCHAR *component = ptr;
3875 if(parse_fragment(pptr, data, flags) && expected_len == data->fragment_len)
3876 TRACE("(%p %p %x): Valid fragment component %s len=%d.\n", builder, data, flags,
3877 debugstr_wn(data->fragment, data->fragment_len), data->fragment_len);
3879 TRACE("(%p %p %x): Invalid fragment component %s.\n", builder, data, flags,
3880 debugstr_wn(component, expected_len));
3881 return INET_E_INVALID_URL;
3888 static HRESULT validate_components(const UriBuilder *builder, parse_data *data, DWORD flags) {
3891 memset(data, 0, sizeof(parse_data));
3893 TRACE("(%p %p %x): Beginning to validate builder components.\n", builder, data, flags);
3895 hr = validate_scheme_name(builder, data, flags);
3899 /* Extra validation for file schemes. */
3900 if(data->scheme_type == URL_SCHEME_FILE) {
3901 if((builder->password || (builder->uri && builder->uri->userinfo_split > -1)) ||
3902 (builder->username || (builder->uri && builder->uri->userinfo_start > -1))) {
3903 TRACE("(%p %p %x): File schemes can't contain a username or password.\n",
3904 builder, data, flags);
3905 return INET_E_INVALID_URL;
3909 hr = validate_userinfo(builder, data, flags);
3913 hr = validate_host(builder, data, flags);
3917 setup_port(builder, data, flags);
3919 /* The URI is opaque if it doesn't have an authority component. */
3920 if(!data->is_relative)
3921 data->is_opaque = !data->username && !data->password && !data->host && !data->has_port;
3923 data->is_opaque = !data->host && !data->has_port;
3925 hr = validate_path(builder, data, flags);
3929 hr = validate_query(builder, data, flags);
3933 hr = validate_fragment(builder, data, flags);
3937 TRACE("(%p %p %x): Finished validating builder components.\n", builder, data, flags);
3942 static void convert_to_dos_path(const WCHAR *path, DWORD path_len,
3943 WCHAR *output, DWORD *output_len)
3945 const WCHAR *ptr = path;
3947 if(path_len > 3 && *ptr == '/' && is_drive_path(path+1))
3948 /* Skip over the leading / before the drive path. */
3951 for(; ptr < path+path_len; ++ptr) {
3964 /* Generates a raw uri string using the parse_data. */
3965 static DWORD generate_raw_uri(const parse_data *data, BSTR uri, DWORD flags) {
3970 memcpy(uri, data->scheme, data->scheme_len*sizeof(WCHAR));
3971 uri[data->scheme_len] = ':';
3973 length += data->scheme_len+1;
3976 if(!data->is_opaque) {
3977 /* For the "//" which appears before the authority component. */
3980 uri[length+1] = '/';
3984 /* Check if we need to add the "\\" before the host name
3985 * of a UNC server name in a DOS path.
3987 if(flags & RAW_URI_CONVERT_TO_DOS_PATH &&
3988 data->scheme_type == URL_SCHEME_FILE && data->host) {
3991 uri[length+1] = '\\';
3997 if(data->username) {
3999 memcpy(uri+length, data->username, data->username_len*sizeof(WCHAR));
4000 length += data->username_len;
4003 if(data->password) {
4006 memcpy(uri+length+1, data->password, data->password_len*sizeof(WCHAR));
4008 length += data->password_len+1;
4011 if(data->password || data->username) {
4018 /* IPv6 addresses get the brackets added around them if they don't already
4021 const BOOL add_brackets = data->host_type == Uri_HOST_IPV6 && *(data->host) != '[';
4029 memcpy(uri+length, data->host, data->host_len*sizeof(WCHAR));
4030 length += data->host_len;
4039 if(data->has_port) {
4040 /* The port isn't included in the raw uri if it's the default
4041 * port for the scheme type.
4044 BOOL is_default = FALSE;
4046 for(i = 0; i < sizeof(default_ports)/sizeof(default_ports[0]); ++i) {
4047 if(data->scheme_type == default_ports[i].scheme &&
4048 data->port_value == default_ports[i].port)
4052 if(!is_default || flags & RAW_URI_FORCE_PORT_DISP) {
4058 length += ui2str(uri+length, data->port_value);
4060 length += ui2str(NULL, data->port_value);
4064 /* Check if a '/' should be added before the path for hierarchical URIs. */
4065 if(!data->is_opaque && data->path && *(data->path) != '/') {
4072 if(!data->is_opaque && data->scheme_type == URL_SCHEME_FILE &&
4073 flags & RAW_URI_CONVERT_TO_DOS_PATH) {
4077 convert_to_dos_path(data->path, data->path_len, uri+length, &len);
4079 convert_to_dos_path(data->path, data->path_len, NULL, &len);
4084 memcpy(uri+length, data->path, data->path_len*sizeof(WCHAR));
4085 length += data->path_len;
4091 memcpy(uri+length, data->query, data->query_len*sizeof(WCHAR));
4092 length += data->query_len;
4095 if(data->fragment) {
4097 memcpy(uri+length, data->fragment, data->fragment_len*sizeof(WCHAR));
4098 length += data->fragment_len;
4102 TRACE("(%p %p): Generated raw uri=%s len=%d\n", data, uri, debugstr_wn(uri, length), length);
4104 TRACE("(%p %p): Computed raw uri len=%d\n", data, uri, length);
4109 static HRESULT generate_uri(const UriBuilder *builder, const parse_data *data, Uri *uri, DWORD flags) {
4111 DWORD length = generate_raw_uri(data, NULL, 0);
4112 uri->raw_uri = SysAllocStringLen(NULL, length);
4114 return E_OUTOFMEMORY;
4116 generate_raw_uri(data, uri->raw_uri, 0);
4118 hr = canonicalize_uri(data, uri, flags);
4120 if(hr == E_INVALIDARG)
4121 return INET_E_INVALID_URL;
4125 uri->create_flags = flags;
4129 static inline Uri* impl_from_IUri(IUri *iface)
4131 return CONTAINING_RECORD(iface, Uri, IUri_iface);
4134 static inline void destory_uri_obj(Uri *This)
4136 SysFreeString(This->raw_uri);
4137 heap_free(This->canon_uri);
4141 static HRESULT WINAPI Uri_QueryInterface(IUri *iface, REFIID riid, void **ppv)
4143 Uri *This = impl_from_IUri(iface);
4145 if(IsEqualGUID(&IID_IUnknown, riid)) {
4146 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
4147 *ppv = &This->IUri_iface;
4148 }else if(IsEqualGUID(&IID_IUri, riid)) {
4149 TRACE("(%p)->(IID_IUri %p)\n", This, ppv);
4150 *ppv = &This->IUri_iface;
4151 }else if(IsEqualGUID(&IID_IUriBuilderFactory, riid)) {
4152 TRACE("(%p)->(IID_IUriBuilderFactory %p)\n", This, riid);
4153 *ppv = &This->IUriBuilderFactory_iface;
4154 }else if(IsEqualGUID(&IID_IUriObj, riid)) {
4155 TRACE("(%p)->(IID_IUriObj %p)\n", This, ppv);
4159 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
4161 return E_NOINTERFACE;
4164 IUnknown_AddRef((IUnknown*)*ppv);
4168 static ULONG WINAPI Uri_AddRef(IUri *iface)
4170 Uri *This = impl_from_IUri(iface);
4171 LONG ref = InterlockedIncrement(&This->ref);
4173 TRACE("(%p) ref=%d\n", This, ref);
4178 static ULONG WINAPI Uri_Release(IUri *iface)
4180 Uri *This = impl_from_IUri(iface);
4181 LONG ref = InterlockedDecrement(&This->ref);
4183 TRACE("(%p) ref=%d\n", This, ref);
4186 destory_uri_obj(This);
4191 static HRESULT WINAPI Uri_GetPropertyBSTR(IUri *iface, Uri_PROPERTY uriProp, BSTR *pbstrProperty, DWORD dwFlags)
4193 Uri *This = impl_from_IUri(iface);
4195 TRACE("(%p)->(%d %p %x)\n", This, uriProp, pbstrProperty, dwFlags);
4200 if(uriProp > Uri_PROPERTY_STRING_LAST) {
4201 /* Windows allocates an empty BSTR for invalid Uri_PROPERTY's. */
4202 *pbstrProperty = SysAllocStringLen(NULL, 0);
4203 if(!(*pbstrProperty))
4204 return E_OUTOFMEMORY;
4206 /* It only returns S_FALSE for the ZONE property... */
4207 if(uriProp == Uri_PROPERTY_ZONE)
4213 /* Don't have support for flags yet. */
4215 FIXME("(%p)->(%d %p %x)\n", This, uriProp, pbstrProperty, dwFlags);
4220 case Uri_PROPERTY_ABSOLUTE_URI:
4221 if(This->display_modifiers & URI_DISPLAY_NO_ABSOLUTE_URI) {
4222 *pbstrProperty = SysAllocStringLen(NULL, 0);
4225 if(This->scheme_type != URL_SCHEME_UNKNOWN && This->userinfo_start > -1) {
4226 if(This->userinfo_len == 0) {
4227 /* Don't include the '@' after the userinfo component. */
4228 *pbstrProperty = SysAllocStringLen(NULL, This->canon_len-1);
4230 if(*pbstrProperty) {
4231 /* Copy everything before it. */
4232 memcpy(*pbstrProperty, This->canon_uri, This->userinfo_start*sizeof(WCHAR));
4234 /* And everything after it. */
4235 memcpy(*pbstrProperty+This->userinfo_start, This->canon_uri+This->userinfo_start+1,
4236 (This->canon_len-This->userinfo_start-1)*sizeof(WCHAR));
4238 } else if(This->userinfo_split == 0 && This->userinfo_len == 1) {
4239 /* Don't include the ":@" */
4240 *pbstrProperty = SysAllocStringLen(NULL, This->canon_len-2);
4242 if(*pbstrProperty) {
4243 memcpy(*pbstrProperty, This->canon_uri, This->userinfo_start*sizeof(WCHAR));
4244 memcpy(*pbstrProperty+This->userinfo_start, This->canon_uri+This->userinfo_start+2,
4245 (This->canon_len-This->userinfo_start-2)*sizeof(WCHAR));
4248 *pbstrProperty = SysAllocString(This->canon_uri);
4252 *pbstrProperty = SysAllocString(This->canon_uri);
4257 if(!(*pbstrProperty))
4258 hres = E_OUTOFMEMORY;
4261 case Uri_PROPERTY_AUTHORITY:
4262 if(This->authority_start > -1) {
4263 if(This->port_offset > -1 && is_default_port(This->scheme_type, This->port) &&
4264 This->display_modifiers & URI_DISPLAY_NO_DEFAULT_PORT_AUTH)
4265 /* Don't include the port in the authority component. */
4266 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->authority_start, This->port_offset);
4268 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->authority_start, This->authority_len);
4271 *pbstrProperty = SysAllocStringLen(NULL, 0);
4275 if(!(*pbstrProperty))
4276 hres = E_OUTOFMEMORY;
4279 case Uri_PROPERTY_DISPLAY_URI:
4280 /* The Display URI contains everything except for the userinfo for known
4283 if(This->scheme_type != URL_SCHEME_UNKNOWN && This->userinfo_start > -1) {
4284 *pbstrProperty = SysAllocStringLen(NULL, This->canon_len-This->userinfo_len);
4286 if(*pbstrProperty) {
4287 /* Copy everything before the userinfo over. */
4288 memcpy(*pbstrProperty, This->canon_uri, This->userinfo_start*sizeof(WCHAR));
4289 /* Copy everything after the userinfo over. */
4290 memcpy(*pbstrProperty+This->userinfo_start,
4291 This->canon_uri+This->userinfo_start+This->userinfo_len+1,
4292 (This->canon_len-(This->userinfo_start+This->userinfo_len+1))*sizeof(WCHAR));
4295 *pbstrProperty = SysAllocString(This->canon_uri);
4297 if(!(*pbstrProperty))
4298 hres = E_OUTOFMEMORY;
4303 case Uri_PROPERTY_DOMAIN:
4304 if(This->domain_offset > -1) {
4305 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->host_start+This->domain_offset,
4306 This->host_len-This->domain_offset);
4309 *pbstrProperty = SysAllocStringLen(NULL, 0);
4313 if(!(*pbstrProperty))
4314 hres = E_OUTOFMEMORY;
4317 case Uri_PROPERTY_EXTENSION:
4318 if(This->extension_offset > -1) {
4319 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->path_start+This->extension_offset,
4320 This->path_len-This->extension_offset);
4323 *pbstrProperty = SysAllocStringLen(NULL, 0);
4327 if(!(*pbstrProperty))
4328 hres = E_OUTOFMEMORY;
4331 case Uri_PROPERTY_FRAGMENT:
4332 if(This->fragment_start > -1) {
4333 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->fragment_start, This->fragment_len);
4336 *pbstrProperty = SysAllocStringLen(NULL, 0);
4340 if(!(*pbstrProperty))
4341 hres = E_OUTOFMEMORY;
4344 case Uri_PROPERTY_HOST:
4345 if(This->host_start > -1) {
4346 /* The '[' and ']' aren't included for IPv6 addresses. */
4347 if(This->host_type == Uri_HOST_IPV6)
4348 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->host_start+1, This->host_len-2);
4350 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->host_start, This->host_len);
4354 *pbstrProperty = SysAllocStringLen(NULL, 0);
4358 if(!(*pbstrProperty))
4359 hres = E_OUTOFMEMORY;
4362 case Uri_PROPERTY_PASSWORD:
4363 if(This->userinfo_split > -1) {
4364 *pbstrProperty = SysAllocStringLen(
4365 This->canon_uri+This->userinfo_start+This->userinfo_split+1,
4366 This->userinfo_len-This->userinfo_split-1);
4369 *pbstrProperty = SysAllocStringLen(NULL, 0);
4373 if(!(*pbstrProperty))
4374 return E_OUTOFMEMORY;
4377 case Uri_PROPERTY_PATH:
4378 if(This->path_start > -1) {
4379 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->path_start, This->path_len);
4382 *pbstrProperty = SysAllocStringLen(NULL, 0);
4386 if(!(*pbstrProperty))
4387 hres = E_OUTOFMEMORY;
4390 case Uri_PROPERTY_PATH_AND_QUERY:
4391 if(This->path_start > -1) {
4392 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->path_start, This->path_len+This->query_len);
4394 } else if(This->query_start > -1) {
4395 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->query_start, This->query_len);
4398 *pbstrProperty = SysAllocStringLen(NULL, 0);
4402 if(!(*pbstrProperty))
4403 hres = E_OUTOFMEMORY;
4406 case Uri_PROPERTY_QUERY:
4407 if(This->query_start > -1) {
4408 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->query_start, This->query_len);
4411 *pbstrProperty = SysAllocStringLen(NULL, 0);
4415 if(!(*pbstrProperty))
4416 hres = E_OUTOFMEMORY;
4419 case Uri_PROPERTY_RAW_URI:
4420 *pbstrProperty = SysAllocString(This->raw_uri);
4421 if(!(*pbstrProperty))
4422 hres = E_OUTOFMEMORY;
4426 case Uri_PROPERTY_SCHEME_NAME:
4427 if(This->scheme_start > -1) {
4428 *pbstrProperty = SysAllocStringLen(This->canon_uri + This->scheme_start, This->scheme_len);
4431 *pbstrProperty = SysAllocStringLen(NULL, 0);
4435 if(!(*pbstrProperty))
4436 hres = E_OUTOFMEMORY;
4439 case Uri_PROPERTY_USER_INFO:
4440 if(This->userinfo_start > -1) {
4441 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->userinfo_start, This->userinfo_len);
4444 *pbstrProperty = SysAllocStringLen(NULL, 0);
4448 if(!(*pbstrProperty))
4449 hres = E_OUTOFMEMORY;
4452 case Uri_PROPERTY_USER_NAME:
4453 if(This->userinfo_start > -1 && This->userinfo_split != 0) {
4454 /* If userinfo_split is set, that means a password exists
4455 * so the username is only from userinfo_start to userinfo_split.
4457 if(This->userinfo_split > -1) {
4458 *pbstrProperty = SysAllocStringLen(This->canon_uri + This->userinfo_start, This->userinfo_split);
4461 *pbstrProperty = SysAllocStringLen(This->canon_uri + This->userinfo_start, This->userinfo_len);
4465 *pbstrProperty = SysAllocStringLen(NULL, 0);
4469 if(!(*pbstrProperty))
4470 return E_OUTOFMEMORY;
4474 FIXME("(%p)->(%d %p %x)\n", This, uriProp, pbstrProperty, dwFlags);
4481 static HRESULT WINAPI Uri_GetPropertyLength(IUri *iface, Uri_PROPERTY uriProp, DWORD *pcchProperty, DWORD dwFlags)
4483 Uri *This = impl_from_IUri(iface);
4485 TRACE("(%p)->(%d %p %x)\n", This, uriProp, pcchProperty, dwFlags);
4488 return E_INVALIDARG;
4490 /* Can only return a length for a property if it's a string. */
4491 if(uriProp > Uri_PROPERTY_STRING_LAST)
4492 return E_INVALIDARG;
4494 /* Don't have support for flags yet. */
4496 FIXME("(%p)->(%d %p %x)\n", This, uriProp, pcchProperty, dwFlags);
4501 case Uri_PROPERTY_ABSOLUTE_URI:
4502 if(This->display_modifiers & URI_DISPLAY_NO_ABSOLUTE_URI) {
4506 if(This->scheme_type != URL_SCHEME_UNKNOWN) {
4507 if(This->userinfo_start > -1 && This->userinfo_len == 0)
4508 /* Don't include the '@' in the length. */
4509 *pcchProperty = This->canon_len-1;
4510 else if(This->userinfo_start > -1 && This->userinfo_len == 1 &&
4511 This->userinfo_split == 0)
4512 /* Don't include the ":@" in the length. */
4513 *pcchProperty = This->canon_len-2;
4515 *pcchProperty = This->canon_len;
4517 *pcchProperty = This->canon_len;
4523 case Uri_PROPERTY_AUTHORITY:
4524 if(This->port_offset > -1 &&
4525 This->display_modifiers & URI_DISPLAY_NO_DEFAULT_PORT_AUTH &&
4526 is_default_port(This->scheme_type, This->port))
4527 /* Only count up until the port in the authority. */
4528 *pcchProperty = This->port_offset;
4530 *pcchProperty = This->authority_len;
4531 hres = (This->authority_start > -1) ? S_OK : S_FALSE;
4533 case Uri_PROPERTY_DISPLAY_URI:
4534 if(This->scheme_type != URL_SCHEME_UNKNOWN && This->userinfo_start > -1)
4535 *pcchProperty = This->canon_len-This->userinfo_len-1;
4537 *pcchProperty = This->canon_len;
4541 case Uri_PROPERTY_DOMAIN:
4542 if(This->domain_offset > -1)
4543 *pcchProperty = This->host_len - This->domain_offset;
4547 hres = (This->domain_offset > -1) ? S_OK : S_FALSE;
4549 case Uri_PROPERTY_EXTENSION:
4550 if(This->extension_offset > -1) {
4551 *pcchProperty = This->path_len - This->extension_offset;
4559 case Uri_PROPERTY_FRAGMENT:
4560 *pcchProperty = This->fragment_len;
4561 hres = (This->fragment_start > -1) ? S_OK : S_FALSE;
4563 case Uri_PROPERTY_HOST:
4564 *pcchProperty = This->host_len;
4566 /* '[' and ']' aren't included in the length. */
4567 if(This->host_type == Uri_HOST_IPV6)
4570 hres = (This->host_start > -1) ? S_OK : S_FALSE;
4572 case Uri_PROPERTY_PASSWORD:
4573 *pcchProperty = (This->userinfo_split > -1) ? This->userinfo_len-This->userinfo_split-1 : 0;
4574 hres = (This->userinfo_split > -1) ? S_OK : S_FALSE;
4576 case Uri_PROPERTY_PATH:
4577 *pcchProperty = This->path_len;
4578 hres = (This->path_start > -1) ? S_OK : S_FALSE;
4580 case Uri_PROPERTY_PATH_AND_QUERY:
4581 *pcchProperty = This->path_len+This->query_len;
4582 hres = (This->path_start > -1 || This->query_start > -1) ? S_OK : S_FALSE;
4584 case Uri_PROPERTY_QUERY:
4585 *pcchProperty = This->query_len;
4586 hres = (This->query_start > -1) ? S_OK : S_FALSE;
4588 case Uri_PROPERTY_RAW_URI:
4589 *pcchProperty = SysStringLen(This->raw_uri);
4592 case Uri_PROPERTY_SCHEME_NAME:
4593 *pcchProperty = This->scheme_len;
4594 hres = (This->scheme_start > -1) ? S_OK : S_FALSE;
4596 case Uri_PROPERTY_USER_INFO:
4597 *pcchProperty = This->userinfo_len;
4598 hres = (This->userinfo_start > -1) ? S_OK : S_FALSE;
4600 case Uri_PROPERTY_USER_NAME:
4601 *pcchProperty = (This->userinfo_split > -1) ? This->userinfo_split : This->userinfo_len;
4602 if(This->userinfo_split == 0)
4605 hres = (This->userinfo_start > -1) ? S_OK : S_FALSE;
4608 FIXME("(%p)->(%d %p %x)\n", This, uriProp, pcchProperty, dwFlags);
4615 static HRESULT WINAPI Uri_GetPropertyDWORD(IUri *iface, Uri_PROPERTY uriProp, DWORD *pcchProperty, DWORD dwFlags)
4617 Uri *This = impl_from_IUri(iface);
4620 TRACE("(%p)->(%d %p %x)\n", This, uriProp, pcchProperty, dwFlags);
4623 return E_INVALIDARG;
4625 /* Microsoft's implementation for the ZONE property of a URI seems to be lacking...
4626 * From what I can tell, instead of checking which URLZONE the URI belongs to it
4627 * simply assigns URLZONE_INVALID and returns E_NOTIMPL. This also applies to the GetZone
4630 if(uriProp == Uri_PROPERTY_ZONE) {
4631 *pcchProperty = URLZONE_INVALID;
4635 if(uriProp < Uri_PROPERTY_DWORD_START) {
4637 return E_INVALIDARG;
4641 case Uri_PROPERTY_HOST_TYPE:
4642 *pcchProperty = This->host_type;
4645 case Uri_PROPERTY_PORT:
4646 if(!This->has_port) {
4650 *pcchProperty = This->port;
4655 case Uri_PROPERTY_SCHEME:
4656 *pcchProperty = This->scheme_type;
4660 FIXME("(%p)->(%d %p %x)\n", This, uriProp, pcchProperty, dwFlags);
4667 static HRESULT WINAPI Uri_HasProperty(IUri *iface, Uri_PROPERTY uriProp, BOOL *pfHasProperty)
4669 Uri *This = impl_from_IUri(iface);
4670 TRACE("(%p)->(%d %p)\n", This, uriProp, pfHasProperty);
4673 return E_INVALIDARG;
4676 case Uri_PROPERTY_ABSOLUTE_URI:
4677 *pfHasProperty = !(This->display_modifiers & URI_DISPLAY_NO_ABSOLUTE_URI);
4679 case Uri_PROPERTY_AUTHORITY:
4680 *pfHasProperty = This->authority_start > -1;
4682 case Uri_PROPERTY_DISPLAY_URI:
4683 *pfHasProperty = TRUE;
4685 case Uri_PROPERTY_DOMAIN:
4686 *pfHasProperty = This->domain_offset > -1;
4688 case Uri_PROPERTY_EXTENSION:
4689 *pfHasProperty = This->extension_offset > -1;
4691 case Uri_PROPERTY_FRAGMENT:
4692 *pfHasProperty = This->fragment_start > -1;
4694 case Uri_PROPERTY_HOST:
4695 *pfHasProperty = This->host_start > -1;
4697 case Uri_PROPERTY_PASSWORD:
4698 *pfHasProperty = This->userinfo_split > -1;
4700 case Uri_PROPERTY_PATH:
4701 *pfHasProperty = This->path_start > -1;
4703 case Uri_PROPERTY_PATH_AND_QUERY:
4704 *pfHasProperty = (This->path_start > -1 || This->query_start > -1);
4706 case Uri_PROPERTY_QUERY:
4707 *pfHasProperty = This->query_start > -1;
4709 case Uri_PROPERTY_RAW_URI:
4710 *pfHasProperty = TRUE;
4712 case Uri_PROPERTY_SCHEME_NAME:
4713 *pfHasProperty = This->scheme_start > -1;
4715 case Uri_PROPERTY_USER_INFO:
4716 *pfHasProperty = This->userinfo_start > -1;
4718 case Uri_PROPERTY_USER_NAME:
4719 if(This->userinfo_split == 0)
4720 *pfHasProperty = FALSE;
4722 *pfHasProperty = This->userinfo_start > -1;
4724 case Uri_PROPERTY_HOST_TYPE:
4725 *pfHasProperty = TRUE;
4727 case Uri_PROPERTY_PORT:
4728 *pfHasProperty = This->has_port;
4730 case Uri_PROPERTY_SCHEME:
4731 *pfHasProperty = TRUE;
4733 case Uri_PROPERTY_ZONE:
4734 *pfHasProperty = FALSE;
4737 FIXME("(%p)->(%d %p): Unsupported property type.\n", This, uriProp, pfHasProperty);
4744 static HRESULT WINAPI Uri_GetAbsoluteUri(IUri *iface, BSTR *pstrAbsoluteUri)
4746 TRACE("(%p)->(%p)\n", iface, pstrAbsoluteUri);
4747 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_ABSOLUTE_URI, pstrAbsoluteUri, 0);
4750 static HRESULT WINAPI Uri_GetAuthority(IUri *iface, BSTR *pstrAuthority)
4752 TRACE("(%p)->(%p)\n", iface, pstrAuthority);
4753 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_AUTHORITY, pstrAuthority, 0);
4756 static HRESULT WINAPI Uri_GetDisplayUri(IUri *iface, BSTR *pstrDisplayUri)
4758 TRACE("(%p)->(%p)\n", iface, pstrDisplayUri);
4759 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_DISPLAY_URI, pstrDisplayUri, 0);
4762 static HRESULT WINAPI Uri_GetDomain(IUri *iface, BSTR *pstrDomain)
4764 TRACE("(%p)->(%p)\n", iface, pstrDomain);
4765 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_DOMAIN, pstrDomain, 0);
4768 static HRESULT WINAPI Uri_GetExtension(IUri *iface, BSTR *pstrExtension)
4770 TRACE("(%p)->(%p)\n", iface, pstrExtension);
4771 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_EXTENSION, pstrExtension, 0);
4774 static HRESULT WINAPI Uri_GetFragment(IUri *iface, BSTR *pstrFragment)
4776 TRACE("(%p)->(%p)\n", iface, pstrFragment);
4777 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_FRAGMENT, pstrFragment, 0);
4780 static HRESULT WINAPI Uri_GetHost(IUri *iface, BSTR *pstrHost)
4782 TRACE("(%p)->(%p)\n", iface, pstrHost);
4783 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_HOST, pstrHost, 0);
4786 static HRESULT WINAPI Uri_GetPassword(IUri *iface, BSTR *pstrPassword)
4788 TRACE("(%p)->(%p)\n", iface, pstrPassword);
4789 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_PASSWORD, pstrPassword, 0);
4792 static HRESULT WINAPI Uri_GetPath(IUri *iface, BSTR *pstrPath)
4794 TRACE("(%p)->(%p)\n", iface, pstrPath);
4795 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_PATH, pstrPath, 0);
4798 static HRESULT WINAPI Uri_GetPathAndQuery(IUri *iface, BSTR *pstrPathAndQuery)
4800 TRACE("(%p)->(%p)\n", iface, pstrPathAndQuery);
4801 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_PATH_AND_QUERY, pstrPathAndQuery, 0);
4804 static HRESULT WINAPI Uri_GetQuery(IUri *iface, BSTR *pstrQuery)
4806 TRACE("(%p)->(%p)\n", iface, pstrQuery);
4807 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_QUERY, pstrQuery, 0);
4810 static HRESULT WINAPI Uri_GetRawUri(IUri *iface, BSTR *pstrRawUri)
4812 TRACE("(%p)->(%p)\n", iface, pstrRawUri);
4813 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_RAW_URI, pstrRawUri, 0);
4816 static HRESULT WINAPI Uri_GetSchemeName(IUri *iface, BSTR *pstrSchemeName)
4818 TRACE("(%p)->(%p)\n", iface, pstrSchemeName);
4819 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_SCHEME_NAME, pstrSchemeName, 0);
4822 static HRESULT WINAPI Uri_GetUserInfo(IUri *iface, BSTR *pstrUserInfo)
4824 TRACE("(%p)->(%p)\n", iface, pstrUserInfo);
4825 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_USER_INFO, pstrUserInfo, 0);
4828 static HRESULT WINAPI Uri_GetUserName(IUri *iface, BSTR *pstrUserName)
4830 TRACE("(%p)->(%p)\n", iface, pstrUserName);
4831 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_USER_NAME, pstrUserName, 0);
4834 static HRESULT WINAPI Uri_GetHostType(IUri *iface, DWORD *pdwHostType)
4836 TRACE("(%p)->(%p)\n", iface, pdwHostType);
4837 return IUri_GetPropertyDWORD(iface, Uri_PROPERTY_HOST_TYPE, pdwHostType, 0);
4840 static HRESULT WINAPI Uri_GetPort(IUri *iface, DWORD *pdwPort)
4842 TRACE("(%p)->(%p)\n", iface, pdwPort);
4843 return IUri_GetPropertyDWORD(iface, Uri_PROPERTY_PORT, pdwPort, 0);
4846 static HRESULT WINAPI Uri_GetScheme(IUri *iface, DWORD *pdwScheme)
4848 TRACE("(%p)->(%p)\n", iface, pdwScheme);
4849 return IUri_GetPropertyDWORD(iface, Uri_PROPERTY_SCHEME, pdwScheme, 0);
4852 static HRESULT WINAPI Uri_GetZone(IUri *iface, DWORD *pdwZone)
4854 TRACE("(%p)->(%p)\n", iface, pdwZone);
4855 return IUri_GetPropertyDWORD(iface, Uri_PROPERTY_ZONE,pdwZone, 0);
4858 static HRESULT WINAPI Uri_GetProperties(IUri *iface, DWORD *pdwProperties)
4860 Uri *This = impl_from_IUri(iface);
4861 TRACE("(%p)->(%p)\n", This, pdwProperties);
4864 return E_INVALIDARG;
4866 /* All URIs have these. */
4867 *pdwProperties = Uri_HAS_DISPLAY_URI|Uri_HAS_RAW_URI|Uri_HAS_SCHEME|Uri_HAS_HOST_TYPE;
4869 if(!(This->display_modifiers & URI_DISPLAY_NO_ABSOLUTE_URI))
4870 *pdwProperties |= Uri_HAS_ABSOLUTE_URI;
4872 if(This->scheme_start > -1)
4873 *pdwProperties |= Uri_HAS_SCHEME_NAME;
4875 if(This->authority_start > -1) {
4876 *pdwProperties |= Uri_HAS_AUTHORITY;
4877 if(This->userinfo_start > -1) {
4878 *pdwProperties |= Uri_HAS_USER_INFO;
4879 if(This->userinfo_split != 0)
4880 *pdwProperties |= Uri_HAS_USER_NAME;
4882 if(This->userinfo_split > -1)
4883 *pdwProperties |= Uri_HAS_PASSWORD;
4884 if(This->host_start > -1)
4885 *pdwProperties |= Uri_HAS_HOST;
4886 if(This->domain_offset > -1)
4887 *pdwProperties |= Uri_HAS_DOMAIN;
4891 *pdwProperties |= Uri_HAS_PORT;
4892 if(This->path_start > -1)
4893 *pdwProperties |= Uri_HAS_PATH|Uri_HAS_PATH_AND_QUERY;
4894 if(This->query_start > -1)
4895 *pdwProperties |= Uri_HAS_QUERY|Uri_HAS_PATH_AND_QUERY;
4897 if(This->extension_offset > -1)
4898 *pdwProperties |= Uri_HAS_EXTENSION;
4900 if(This->fragment_start > -1)
4901 *pdwProperties |= Uri_HAS_FRAGMENT;
4906 static HRESULT WINAPI Uri_IsEqual(IUri *iface, IUri *pUri, BOOL *pfEqual)
4908 Uri *This = impl_from_IUri(iface);
4911 TRACE("(%p)->(%p %p)\n", This, pUri, pfEqual);
4919 /* For some reason Windows returns S_OK here... */
4923 /* Try to convert it to a Uri (allows for a more simple comparison). */
4924 if((other = get_uri_obj(pUri)))
4925 *pfEqual = are_equal_simple(This, other);
4927 /* Do it the hard way. */
4928 FIXME("(%p)->(%p %p) No support for unknown IUri's yet.\n", iface, pUri, pfEqual);
4935 static const IUriVtbl UriVtbl = {
4939 Uri_GetPropertyBSTR,
4940 Uri_GetPropertyLength,
4941 Uri_GetPropertyDWORD,
4952 Uri_GetPathAndQuery,
4966 static inline Uri* impl_from_IUriBuilderFactory(IUriBuilderFactory *iface)
4968 return CONTAINING_RECORD(iface, Uri, IUriBuilderFactory_iface);
4971 static HRESULT WINAPI UriBuilderFactory_QueryInterface(IUriBuilderFactory *iface, REFIID riid, void **ppv)
4973 Uri *This = impl_from_IUriBuilderFactory(iface);
4975 if(IsEqualGUID(&IID_IUnknown, riid)) {
4976 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
4977 *ppv = &This->IUriBuilderFactory_iface;
4978 }else if(IsEqualGUID(&IID_IUriBuilderFactory, riid)) {
4979 TRACE("(%p)->(IID_IUriBuilderFactory %p)\n", This, ppv);
4980 *ppv = &This->IUriBuilderFactory_iface;
4981 }else if(IsEqualGUID(&IID_IUri, riid)) {
4982 TRACE("(%p)->(IID_IUri %p)\n", This, ppv);
4983 *ppv = &This->IUri_iface;
4985 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
4987 return E_NOINTERFACE;
4990 IUnknown_AddRef((IUnknown*)*ppv);
4994 static ULONG WINAPI UriBuilderFactory_AddRef(IUriBuilderFactory *iface)
4996 Uri *This = impl_from_IUriBuilderFactory(iface);
4997 LONG ref = InterlockedIncrement(&This->ref);
4999 TRACE("(%p) ref=%d\n", This, ref);
5004 static ULONG WINAPI UriBuilderFactory_Release(IUriBuilderFactory *iface)
5006 Uri *This = impl_from_IUriBuilderFactory(iface);
5007 LONG ref = InterlockedDecrement(&This->ref);
5009 TRACE("(%p) ref=%d\n", This, ref);
5012 destory_uri_obj(This);
5017 static HRESULT WINAPI UriBuilderFactory_CreateIUriBuilder(IUriBuilderFactory *iface,
5019 DWORD_PTR dwReserved,
5020 IUriBuilder **ppIUriBuilder)
5022 Uri *This = impl_from_IUriBuilderFactory(iface);
5023 TRACE("(%p)->(%08x %08x %p)\n", This, dwFlags, (DWORD)dwReserved, ppIUriBuilder);
5028 if(dwFlags || dwReserved) {
5029 *ppIUriBuilder = NULL;
5030 return E_INVALIDARG;
5033 return CreateIUriBuilder(NULL, 0, 0, ppIUriBuilder);
5036 static HRESULT WINAPI UriBuilderFactory_CreateInitializedIUriBuilder(IUriBuilderFactory *iface,
5038 DWORD_PTR dwReserved,
5039 IUriBuilder **ppIUriBuilder)
5041 Uri *This = impl_from_IUriBuilderFactory(iface);
5042 TRACE("(%p)->(%08x %08x %p)\n", This, dwFlags, (DWORD)dwReserved, ppIUriBuilder);
5047 if(dwFlags || dwReserved) {
5048 *ppIUriBuilder = NULL;
5049 return E_INVALIDARG;
5052 return CreateIUriBuilder(&This->IUri_iface, 0, 0, ppIUriBuilder);
5055 static const IUriBuilderFactoryVtbl UriBuilderFactoryVtbl = {
5056 UriBuilderFactory_QueryInterface,
5057 UriBuilderFactory_AddRef,
5058 UriBuilderFactory_Release,
5059 UriBuilderFactory_CreateIUriBuilder,
5060 UriBuilderFactory_CreateInitializedIUriBuilder
5063 static Uri* create_uri_obj(void) {
5064 Uri *ret = heap_alloc_zero(sizeof(Uri));
5066 ret->IUri_iface.lpVtbl = &UriVtbl;
5067 ret->IUriBuilderFactory_iface.lpVtbl = &UriBuilderFactoryVtbl;
5074 /***********************************************************************
5075 * CreateUri (urlmon.@)
5077 * Creates a new IUri object using the URI represented by pwzURI. This function
5078 * parses and validates the components of pwzURI and then canonicalizes the
5079 * parsed components.
5082 * pwzURI [I] The URI to parse, validate, and canonicalize.
5083 * dwFlags [I] Flags which can affect how the parsing/canonicalization is performed.
5084 * dwReserved [I] Reserved (not used).
5085 * ppURI [O] The resulting IUri after parsing/canonicalization occurs.
5088 * Success: Returns S_OK. ppURI contains the pointer to the newly allocated IUri.
5089 * Failure: E_INVALIDARG if there's invalid flag combinations in dwFlags, or an
5090 * invalid parameters, or pwzURI doesn't represnt a valid URI.
5091 * E_OUTOFMEMORY if any memory allocation fails.
5095 * Uri_CREATE_CANONICALIZE, Uri_CREATE_DECODE_EXTRA_INFO, Uri_CREATE_CRACK_UNKNOWN_SCHEMES,
5096 * Uri_CREATE_PRE_PROCESS_HTML_URI, Uri_CREATE_NO_IE_SETTINGS.
5098 HRESULT WINAPI CreateUri(LPCWSTR pwzURI, DWORD dwFlags, DWORD_PTR dwReserved, IUri **ppURI)
5100 const DWORD supported_flags = Uri_CREATE_ALLOW_RELATIVE|Uri_CREATE_ALLOW_IMPLICIT_WILDCARD_SCHEME|
5101 Uri_CREATE_ALLOW_IMPLICIT_FILE_SCHEME|Uri_CREATE_NO_CANONICALIZE|Uri_CREATE_CANONICALIZE|
5102 Uri_CREATE_DECODE_EXTRA_INFO|Uri_CREATE_NO_DECODE_EXTRA_INFO|Uri_CREATE_CRACK_UNKNOWN_SCHEMES|
5103 Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES|Uri_CREATE_PRE_PROCESS_HTML_URI|Uri_CREATE_NO_PRE_PROCESS_HTML_URI|
5104 Uri_CREATE_NO_IE_SETTINGS|Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS|Uri_CREATE_FILE_USE_DOS_PATH;
5109 TRACE("(%s %x %x %p)\n", debugstr_w(pwzURI), dwFlags, (DWORD)dwReserved, ppURI);
5112 return E_INVALIDARG;
5114 if(!pwzURI || !*pwzURI) {
5116 return E_INVALIDARG;
5119 /* Check for invalid flags. */
5120 if(has_invalid_flag_combination(dwFlags)) {
5122 return E_INVALIDARG;
5125 /* Currently unsupported. */
5126 if(dwFlags & ~supported_flags)
5127 FIXME("Ignoring unsupported flag(s) %x\n", dwFlags & ~supported_flags);
5129 ret = create_uri_obj();
5132 return E_OUTOFMEMORY;
5135 /* Explicitly set the default flags if it doesn't cause a flag conflict. */
5136 apply_default_flags(&dwFlags);
5138 /* Pre process the URI, unless told otherwise. */
5139 if(!(dwFlags & Uri_CREATE_NO_PRE_PROCESS_HTML_URI))
5140 ret->raw_uri = pre_process_uri(pwzURI);
5142 ret->raw_uri = SysAllocString(pwzURI);
5146 return E_OUTOFMEMORY;
5149 memset(&data, 0, sizeof(parse_data));
5150 data.uri = ret->raw_uri;
5152 /* Validate and parse the URI into it's components. */
5153 if(!parse_uri(&data, dwFlags)) {
5154 /* Encountered an unsupported or invalid URI */
5155 IUri_Release(&ret->IUri_iface);
5157 return E_INVALIDARG;
5160 /* Canonicalize the URI. */
5161 hr = canonicalize_uri(&data, ret, dwFlags);
5163 IUri_Release(&ret->IUri_iface);
5168 ret->create_flags = dwFlags;
5170 *ppURI = &ret->IUri_iface;
5174 /***********************************************************************
5175 * CreateUriWithFragment (urlmon.@)
5177 * Creates a new IUri object. This is almost the same as CreateUri, expect that
5178 * it allows you to explicitly specify a fragment (pwzFragment) for pwzURI.
5181 * pwzURI [I] The URI to parse and perform canonicalization on.
5182 * pwzFragment [I] The explict fragment string which should be added to pwzURI.
5183 * dwFlags [I] The flags which will be passed to CreateUri.
5184 * dwReserved [I] Reserved (not used).
5185 * ppURI [O] The resulting IUri after parsing/canonicalization.
5188 * Success: S_OK. ppURI contains the pointer to the newly allocated IUri.
5189 * Failure: E_INVALIDARG if pwzURI already contains a fragment and pwzFragment
5190 * isn't NULL. Will also return E_INVALIDARG for the same reasons as
5191 * CreateUri will. E_OUTOFMEMORY if any allocations fail.
5193 HRESULT WINAPI CreateUriWithFragment(LPCWSTR pwzURI, LPCWSTR pwzFragment, DWORD dwFlags,
5194 DWORD_PTR dwReserved, IUri **ppURI)
5197 TRACE("(%s %s %x %x %p)\n", debugstr_w(pwzURI), debugstr_w(pwzFragment), dwFlags, (DWORD)dwReserved, ppURI);
5200 return E_INVALIDARG;
5204 return E_INVALIDARG;
5207 /* Check if a fragment should be appended to the URI string. */
5210 DWORD uri_len, frag_len;
5213 /* Check if the original URI already has a fragment component. */
5214 if(StrChrW(pwzURI, '#')) {
5216 return E_INVALIDARG;
5219 uri_len = lstrlenW(pwzURI);
5220 frag_len = lstrlenW(pwzFragment);
5222 /* If the fragment doesn't start with a '#', one will be added. */
5223 add_pound = *pwzFragment != '#';
5226 uriW = heap_alloc((uri_len+frag_len+2)*sizeof(WCHAR));
5228 uriW = heap_alloc((uri_len+frag_len+1)*sizeof(WCHAR));
5231 return E_OUTOFMEMORY;
5233 memcpy(uriW, pwzURI, uri_len*sizeof(WCHAR));
5235 uriW[uri_len++] = '#';
5236 memcpy(uriW+uri_len, pwzFragment, (frag_len+1)*sizeof(WCHAR));
5238 hres = CreateUri(uriW, dwFlags, 0, ppURI);
5242 /* A fragment string wasn't specified, so just forward the call. */
5243 hres = CreateUri(pwzURI, dwFlags, 0, ppURI);
5248 static HRESULT build_uri(const UriBuilder *builder, IUri **uri, DWORD create_flags,
5249 DWORD use_orig_flags, DWORD encoding_mask)
5258 if(encoding_mask && (!builder->uri || builder->modified_props)) {
5263 /* Decide what flags should be used when creating the Uri. */
5264 if((use_orig_flags & UriBuilder_USE_ORIGINAL_FLAGS) && builder->uri)
5265 create_flags = builder->uri->create_flags;
5267 if(has_invalid_flag_combination(create_flags)) {
5269 return E_INVALIDARG;
5272 /* Set the default flags if they don't cause a conflict. */
5273 apply_default_flags(&create_flags);
5276 /* Return the base IUri if no changes have been made and the create_flags match. */
5277 if(builder->uri && !builder->modified_props && builder->uri->create_flags == create_flags) {
5278 *uri = &builder->uri->IUri_iface;
5283 hr = validate_components(builder, &data, create_flags);
5289 ret = create_uri_obj();
5292 return E_OUTOFMEMORY;
5295 hr = generate_uri(builder, &data, ret, create_flags);
5297 IUri_Release(&ret->IUri_iface);
5302 *uri = &ret->IUri_iface;
5306 static inline UriBuilder* impl_from_IUriBuilder(IUriBuilder *iface)
5308 return CONTAINING_RECORD(iface, UriBuilder, IUriBuilder_iface);
5311 static HRESULT WINAPI UriBuilder_QueryInterface(IUriBuilder *iface, REFIID riid, void **ppv)
5313 UriBuilder *This = impl_from_IUriBuilder(iface);
5315 if(IsEqualGUID(&IID_IUnknown, riid)) {
5316 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
5317 *ppv = &This->IUriBuilder_iface;
5318 }else if(IsEqualGUID(&IID_IUriBuilder, riid)) {
5319 TRACE("(%p)->(IID_IUriBuilder %p)\n", This, ppv);
5320 *ppv = &This->IUriBuilder_iface;
5322 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
5324 return E_NOINTERFACE;
5327 IUnknown_AddRef((IUnknown*)*ppv);
5331 static ULONG WINAPI UriBuilder_AddRef(IUriBuilder *iface)
5333 UriBuilder *This = impl_from_IUriBuilder(iface);
5334 LONG ref = InterlockedIncrement(&This->ref);
5336 TRACE("(%p) ref=%d\n", This, ref);
5341 static ULONG WINAPI UriBuilder_Release(IUriBuilder *iface)
5343 UriBuilder *This = impl_from_IUriBuilder(iface);
5344 LONG ref = InterlockedDecrement(&This->ref);
5346 TRACE("(%p) ref=%d\n", This, ref);
5349 if(This->uri) IUri_Release(&This->uri->IUri_iface);
5350 heap_free(This->fragment);
5351 heap_free(This->host);
5352 heap_free(This->password);
5353 heap_free(This->path);
5354 heap_free(This->query);
5355 heap_free(This->scheme);
5356 heap_free(This->username);
5363 static HRESULT WINAPI UriBuilder_CreateUriSimple(IUriBuilder *iface,
5364 DWORD dwAllowEncodingPropertyMask,
5365 DWORD_PTR dwReserved,
5368 UriBuilder *This = impl_from_IUriBuilder(iface);
5370 TRACE("(%p)->(%d %d %p)\n", This, dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
5372 hr = build_uri(This, ppIUri, 0, UriBuilder_USE_ORIGINAL_FLAGS, dwAllowEncodingPropertyMask);
5374 FIXME("(%p)->(%d %d %p)\n", This, dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
5378 static HRESULT WINAPI UriBuilder_CreateUri(IUriBuilder *iface,
5379 DWORD dwCreateFlags,
5380 DWORD dwAllowEncodingPropertyMask,
5381 DWORD_PTR dwReserved,
5384 UriBuilder *This = impl_from_IUriBuilder(iface);
5386 TRACE("(%p)->(0x%08x %d %d %p)\n", This, dwCreateFlags, dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
5388 if(dwCreateFlags == -1)
5389 hr = build_uri(This, ppIUri, 0, UriBuilder_USE_ORIGINAL_FLAGS, dwAllowEncodingPropertyMask);
5391 hr = build_uri(This, ppIUri, dwCreateFlags, 0, dwAllowEncodingPropertyMask);
5394 FIXME("(%p)->(0x%08x %d %d %p)\n", This, dwCreateFlags, dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
5398 static HRESULT WINAPI UriBuilder_CreateUriWithFlags(IUriBuilder *iface,
5399 DWORD dwCreateFlags,
5400 DWORD dwUriBuilderFlags,
5401 DWORD dwAllowEncodingPropertyMask,
5402 DWORD_PTR dwReserved,
5405 UriBuilder *This = impl_from_IUriBuilder(iface);
5407 TRACE("(%p)->(0x%08x 0x%08x %d %d %p)\n", This, dwCreateFlags, dwUriBuilderFlags,
5408 dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
5410 hr = build_uri(This, ppIUri, dwCreateFlags, dwUriBuilderFlags, dwAllowEncodingPropertyMask);
5412 FIXME("(%p)->(0x%08x 0x%08x %d %d %p)\n", This, dwCreateFlags, dwUriBuilderFlags,
5413 dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
5417 static HRESULT WINAPI UriBuilder_GetIUri(IUriBuilder *iface, IUri **ppIUri)
5419 UriBuilder *This = impl_from_IUriBuilder(iface);
5420 TRACE("(%p)->(%p)\n", This, ppIUri);
5426 IUri *uri = &This->uri->IUri_iface;
5435 static HRESULT WINAPI UriBuilder_SetIUri(IUriBuilder *iface, IUri *pIUri)
5437 UriBuilder *This = impl_from_IUriBuilder(iface);
5438 TRACE("(%p)->(%p)\n", This, pIUri);
5443 if((uri = get_uri_obj(pIUri))) {
5444 /* Only reset the builder if it's Uri isn't the same as
5445 * the Uri passed to the function.
5447 if(This->uri != uri) {
5448 reset_builder(This);
5452 This->port = uri->port;
5457 FIXME("(%p)->(%p) Unknown IUri types not supported yet.\n", This, pIUri);
5460 } else if(This->uri)
5461 /* Only reset the builder if it's Uri isn't NULL. */
5462 reset_builder(This);
5467 static HRESULT WINAPI UriBuilder_GetFragment(IUriBuilder *iface, DWORD *pcchFragment, LPCWSTR *ppwzFragment)
5469 UriBuilder *This = impl_from_IUriBuilder(iface);
5470 TRACE("(%p)->(%p %p)\n", This, pcchFragment, ppwzFragment);
5472 if(!This->uri || This->uri->fragment_start == -1 || This->modified_props & Uri_HAS_FRAGMENT)
5473 return get_builder_component(&This->fragment, &This->fragment_len, NULL, 0, ppwzFragment, pcchFragment);
5475 return get_builder_component(&This->fragment, &This->fragment_len, This->uri->canon_uri+This->uri->fragment_start,
5476 This->uri->fragment_len, ppwzFragment, pcchFragment);
5479 static HRESULT WINAPI UriBuilder_GetHost(IUriBuilder *iface, DWORD *pcchHost, LPCWSTR *ppwzHost)
5481 UriBuilder *This = impl_from_IUriBuilder(iface);
5482 TRACE("(%p)->(%p %p)\n", This, pcchHost, ppwzHost);
5484 if(!This->uri || This->uri->host_start == -1 || This->modified_props & Uri_HAS_HOST)
5485 return get_builder_component(&This->host, &This->host_len, NULL, 0, ppwzHost, pcchHost);
5487 if(This->uri->host_type == Uri_HOST_IPV6)
5488 /* Don't include the '[' and ']' around the address. */
5489 return get_builder_component(&This->host, &This->host_len, This->uri->canon_uri+This->uri->host_start+1,
5490 This->uri->host_len-2, ppwzHost, pcchHost);
5492 return get_builder_component(&This->host, &This->host_len, This->uri->canon_uri+This->uri->host_start,
5493 This->uri->host_len, ppwzHost, pcchHost);
5497 static HRESULT WINAPI UriBuilder_GetPassword(IUriBuilder *iface, DWORD *pcchPassword, LPCWSTR *ppwzPassword)
5499 UriBuilder *This = impl_from_IUriBuilder(iface);
5500 TRACE("(%p)->(%p %p)\n", This, pcchPassword, ppwzPassword);
5502 if(!This->uri || This->uri->userinfo_split == -1 || This->modified_props & Uri_HAS_PASSWORD)
5503 return get_builder_component(&This->password, &This->password_len, NULL, 0, ppwzPassword, pcchPassword);
5505 const WCHAR *start = This->uri->canon_uri+This->uri->userinfo_start+This->uri->userinfo_split+1;
5506 DWORD len = This->uri->userinfo_len-This->uri->userinfo_split-1;
5507 return get_builder_component(&This->password, &This->password_len, start, len, ppwzPassword, pcchPassword);
5511 static HRESULT WINAPI UriBuilder_GetPath(IUriBuilder *iface, DWORD *pcchPath, LPCWSTR *ppwzPath)
5513 UriBuilder *This = impl_from_IUriBuilder(iface);
5514 TRACE("(%p)->(%p %p)\n", This, pcchPath, ppwzPath);
5516 if(!This->uri || This->uri->path_start == -1 || This->modified_props & Uri_HAS_PATH)
5517 return get_builder_component(&This->path, &This->path_len, NULL, 0, ppwzPath, pcchPath);
5519 return get_builder_component(&This->path, &This->path_len, This->uri->canon_uri+This->uri->path_start,
5520 This->uri->path_len, ppwzPath, pcchPath);
5523 static HRESULT WINAPI UriBuilder_GetPort(IUriBuilder *iface, BOOL *pfHasPort, DWORD *pdwPort)
5525 UriBuilder *This = impl_from_IUriBuilder(iface);
5526 TRACE("(%p)->(%p %p)\n", This, pfHasPort, pdwPort);
5539 *pfHasPort = This->has_port;
5540 *pdwPort = This->port;
5544 static HRESULT WINAPI UriBuilder_GetQuery(IUriBuilder *iface, DWORD *pcchQuery, LPCWSTR *ppwzQuery)
5546 UriBuilder *This = impl_from_IUriBuilder(iface);
5547 TRACE("(%p)->(%p %p)\n", This, pcchQuery, ppwzQuery);
5549 if(!This->uri || This->uri->query_start == -1 || This->modified_props & Uri_HAS_QUERY)
5550 return get_builder_component(&This->query, &This->query_len, NULL, 0, ppwzQuery, pcchQuery);
5552 return get_builder_component(&This->query, &This->query_len, This->uri->canon_uri+This->uri->query_start,
5553 This->uri->query_len, ppwzQuery, pcchQuery);
5556 static HRESULT WINAPI UriBuilder_GetSchemeName(IUriBuilder *iface, DWORD *pcchSchemeName, LPCWSTR *ppwzSchemeName)
5558 UriBuilder *This = impl_from_IUriBuilder(iface);
5559 TRACE("(%p)->(%p %p)\n", This, pcchSchemeName, ppwzSchemeName);
5561 if(!This->uri || This->uri->scheme_start == -1 || This->modified_props & Uri_HAS_SCHEME_NAME)
5562 return get_builder_component(&This->scheme, &This->scheme_len, NULL, 0, ppwzSchemeName, pcchSchemeName);
5564 return get_builder_component(&This->scheme, &This->scheme_len, This->uri->canon_uri+This->uri->scheme_start,
5565 This->uri->scheme_len, ppwzSchemeName, pcchSchemeName);
5568 static HRESULT WINAPI UriBuilder_GetUserName(IUriBuilder *iface, DWORD *pcchUserName, LPCWSTR *ppwzUserName)
5570 UriBuilder *This = impl_from_IUriBuilder(iface);
5571 TRACE("(%p)->(%p %p)\n", This, pcchUserName, ppwzUserName);
5573 if(!This->uri || This->uri->userinfo_start == -1 || This->uri->userinfo_split == 0 ||
5574 This->modified_props & Uri_HAS_USER_NAME)
5575 return get_builder_component(&This->username, &This->username_len, NULL, 0, ppwzUserName, pcchUserName);
5577 const WCHAR *start = This->uri->canon_uri+This->uri->userinfo_start;
5579 /* Check if there's a password in the userinfo section. */
5580 if(This->uri->userinfo_split > -1)
5581 /* Don't include the password. */
5582 return get_builder_component(&This->username, &This->username_len, start,
5583 This->uri->userinfo_split, ppwzUserName, pcchUserName);
5585 return get_builder_component(&This->username, &This->username_len, start,
5586 This->uri->userinfo_len, ppwzUserName, pcchUserName);
5590 static HRESULT WINAPI UriBuilder_SetFragment(IUriBuilder *iface, LPCWSTR pwzNewValue)
5592 UriBuilder *This = impl_from_IUriBuilder(iface);
5593 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
5594 return set_builder_component(&This->fragment, &This->fragment_len, pwzNewValue, '#',
5595 &This->modified_props, Uri_HAS_FRAGMENT);
5598 static HRESULT WINAPI UriBuilder_SetHost(IUriBuilder *iface, LPCWSTR pwzNewValue)
5600 UriBuilder *This = impl_from_IUriBuilder(iface);
5601 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
5603 /* Host name can't be set to NULL. */
5605 return E_INVALIDARG;
5607 return set_builder_component(&This->host, &This->host_len, pwzNewValue, 0,
5608 &This->modified_props, Uri_HAS_HOST);
5611 static HRESULT WINAPI UriBuilder_SetPassword(IUriBuilder *iface, LPCWSTR pwzNewValue)
5613 UriBuilder *This = impl_from_IUriBuilder(iface);
5614 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
5615 return set_builder_component(&This->password, &This->password_len, pwzNewValue, 0,
5616 &This->modified_props, Uri_HAS_PASSWORD);
5619 static HRESULT WINAPI UriBuilder_SetPath(IUriBuilder *iface, LPCWSTR pwzNewValue)
5621 UriBuilder *This = impl_from_IUriBuilder(iface);
5622 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
5623 return set_builder_component(&This->path, &This->path_len, pwzNewValue, 0,
5624 &This->modified_props, Uri_HAS_PATH);
5627 static HRESULT WINAPI UriBuilder_SetPort(IUriBuilder *iface, BOOL fHasPort, DWORD dwNewValue)
5629 UriBuilder *This = impl_from_IUriBuilder(iface);
5630 TRACE("(%p)->(%d %d)\n", This, fHasPort, dwNewValue);
5632 This->has_port = fHasPort;
5633 This->port = dwNewValue;
5634 This->modified_props |= Uri_HAS_PORT;
5638 static HRESULT WINAPI UriBuilder_SetQuery(IUriBuilder *iface, LPCWSTR pwzNewValue)
5640 UriBuilder *This = impl_from_IUriBuilder(iface);
5641 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
5642 return set_builder_component(&This->query, &This->query_len, pwzNewValue, '?',
5643 &This->modified_props, Uri_HAS_QUERY);
5646 static HRESULT WINAPI UriBuilder_SetSchemeName(IUriBuilder *iface, LPCWSTR pwzNewValue)
5648 UriBuilder *This = impl_from_IUriBuilder(iface);
5649 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
5651 /* Only set the scheme name if it's not NULL or empty. */
5652 if(!pwzNewValue || !*pwzNewValue)
5653 return E_INVALIDARG;
5655 return set_builder_component(&This->scheme, &This->scheme_len, pwzNewValue, 0,
5656 &This->modified_props, Uri_HAS_SCHEME_NAME);
5659 static HRESULT WINAPI UriBuilder_SetUserName(IUriBuilder *iface, LPCWSTR pwzNewValue)
5661 UriBuilder *This = impl_from_IUriBuilder(iface);
5662 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
5663 return set_builder_component(&This->username, &This->username_len, pwzNewValue, 0,
5664 &This->modified_props, Uri_HAS_USER_NAME);
5667 static HRESULT WINAPI UriBuilder_RemoveProperties(IUriBuilder *iface, DWORD dwPropertyMask)
5669 const DWORD accepted_flags = Uri_HAS_AUTHORITY|Uri_HAS_DOMAIN|Uri_HAS_EXTENSION|Uri_HAS_FRAGMENT|Uri_HAS_HOST|
5670 Uri_HAS_PASSWORD|Uri_HAS_PATH|Uri_HAS_PATH_AND_QUERY|Uri_HAS_QUERY|
5671 Uri_HAS_USER_INFO|Uri_HAS_USER_NAME;
5673 UriBuilder *This = impl_from_IUriBuilder(iface);
5674 TRACE("(%p)->(0x%08x)\n", This, dwPropertyMask);
5676 if(dwPropertyMask & ~accepted_flags)
5677 return E_INVALIDARG;
5679 if(dwPropertyMask & Uri_HAS_FRAGMENT)
5680 UriBuilder_SetFragment(iface, NULL);
5682 /* Even though you can't set the host name to NULL or an
5683 * empty string, you can still remove it... for some reason.
5685 if(dwPropertyMask & Uri_HAS_HOST)
5686 set_builder_component(&This->host, &This->host_len, NULL, 0,
5687 &This->modified_props, Uri_HAS_HOST);
5689 if(dwPropertyMask & Uri_HAS_PASSWORD)
5690 UriBuilder_SetPassword(iface, NULL);
5692 if(dwPropertyMask & Uri_HAS_PATH)
5693 UriBuilder_SetPath(iface, NULL);
5695 if(dwPropertyMask & Uri_HAS_PORT)
5696 UriBuilder_SetPort(iface, FALSE, 0);
5698 if(dwPropertyMask & Uri_HAS_QUERY)
5699 UriBuilder_SetQuery(iface, NULL);
5701 if(dwPropertyMask & Uri_HAS_USER_NAME)
5702 UriBuilder_SetUserName(iface, NULL);
5707 static HRESULT WINAPI UriBuilder_HasBeenModified(IUriBuilder *iface, BOOL *pfModified)
5709 UriBuilder *This = impl_from_IUriBuilder(iface);
5710 TRACE("(%p)->(%p)\n", This, pfModified);
5715 *pfModified = This->modified_props > 0;
5719 static const IUriBuilderVtbl UriBuilderVtbl = {
5720 UriBuilder_QueryInterface,
5723 UriBuilder_CreateUriSimple,
5724 UriBuilder_CreateUri,
5725 UriBuilder_CreateUriWithFlags,
5728 UriBuilder_GetFragment,
5730 UriBuilder_GetPassword,
5733 UriBuilder_GetQuery,
5734 UriBuilder_GetSchemeName,
5735 UriBuilder_GetUserName,
5736 UriBuilder_SetFragment,
5738 UriBuilder_SetPassword,
5741 UriBuilder_SetQuery,
5742 UriBuilder_SetSchemeName,
5743 UriBuilder_SetUserName,
5744 UriBuilder_RemoveProperties,
5745 UriBuilder_HasBeenModified,
5748 /***********************************************************************
5749 * CreateIUriBuilder (urlmon.@)
5751 HRESULT WINAPI CreateIUriBuilder(IUri *pIUri, DWORD dwFlags, DWORD_PTR dwReserved, IUriBuilder **ppIUriBuilder)
5755 TRACE("(%p %x %x %p)\n", pIUri, dwFlags, (DWORD)dwReserved, ppIUriBuilder);
5760 ret = heap_alloc_zero(sizeof(UriBuilder));
5762 return E_OUTOFMEMORY;
5764 ret->IUriBuilder_iface.lpVtbl = &UriBuilderVtbl;
5770 if((uri = get_uri_obj(pIUri))) {
5775 /* Windows doesn't set 'has_port' to TRUE in this case. */
5776 ret->port = uri->port;
5780 *ppIUriBuilder = NULL;
5781 FIXME("(%p %x %x %p): Unknown IUri types not supported yet.\n", pIUri, dwFlags,
5782 (DWORD)dwReserved, ppIUriBuilder);
5787 *ppIUriBuilder = &ret->IUriBuilder_iface;
5791 /* Merges the base path with the relative path and stores the resulting path
5792 * and path len in 'result' and 'result_len'.
5794 static HRESULT merge_paths(parse_data *data, const WCHAR *base, DWORD base_len, const WCHAR *relative,
5795 DWORD relative_len, WCHAR **result, DWORD *result_len, DWORD flags)
5797 const WCHAR *end = NULL;
5798 DWORD base_copy_len = 0;
5802 /* Find the characters the will be copied over from
5805 end = str_last_of(base, base+(base_len-1), '/');
5806 if(!end && data->scheme_type == URL_SCHEME_FILE)
5807 /* Try looking for a '\\'. */
5808 end = str_last_of(base, base+(base_len-1), '\\');
5812 base_copy_len = (end+1)-base;
5813 *result = heap_alloc((base_copy_len+relative_len+1)*sizeof(WCHAR));
5815 *result = heap_alloc((relative_len+1)*sizeof(WCHAR));
5819 return E_OUTOFMEMORY;
5824 memcpy(ptr, base, base_copy_len*sizeof(WCHAR));
5825 ptr += base_copy_len;
5828 memcpy(ptr, relative, relative_len*sizeof(WCHAR));
5829 ptr += relative_len;
5832 *result_len = (ptr-*result);
5836 static HRESULT combine_uri(Uri *base, Uri *relative, DWORD flags, IUri **result, DWORD extras) {
5840 DWORD create_flags = 0, len = 0;
5842 memset(&data, 0, sizeof(parse_data));
5844 /* Base case is when the relative Uri has a scheme name,
5845 * if it does, then 'result' will contain the same data
5846 * as the relative Uri.
5848 if(relative->scheme_start > -1) {
5849 data.uri = SysAllocString(relative->raw_uri);
5852 return E_OUTOFMEMORY;
5855 parse_uri(&data, 0);
5857 ret = create_uri_obj();
5860 return E_OUTOFMEMORY;
5863 if(extras & COMBINE_URI_FORCE_FLAG_USE) {
5864 if(flags & URL_DONT_SIMPLIFY)
5865 create_flags |= Uri_CREATE_NO_CANONICALIZE;
5866 if(flags & URL_DONT_UNESCAPE_EXTRA_INFO)
5867 create_flags |= Uri_CREATE_NO_DECODE_EXTRA_INFO;
5870 ret->raw_uri = data.uri;
5871 hr = canonicalize_uri(&data, ret, create_flags);
5873 IUri_Release(&ret->IUri_iface);
5878 apply_default_flags(&create_flags);
5879 ret->create_flags = create_flags;
5881 *result = &ret->IUri_iface;
5884 DWORD raw_flags = 0;
5886 if(base->scheme_start > -1) {
5887 data.scheme = base->canon_uri+base->scheme_start;
5888 data.scheme_len = base->scheme_len;
5889 data.scheme_type = base->scheme_type;
5891 data.is_relative = TRUE;
5892 data.scheme_type = URL_SCHEME_UNKNOWN;
5893 create_flags |= Uri_CREATE_ALLOW_RELATIVE;
5896 if(base->authority_start > -1) {
5897 if(base->userinfo_start > -1 && base->userinfo_split != 0) {
5898 data.username = base->canon_uri+base->userinfo_start;
5899 data.username_len = (base->userinfo_split > -1) ? base->userinfo_split : base->userinfo_len;
5902 if(base->userinfo_split > -1) {
5903 data.password = base->canon_uri+base->userinfo_start+base->userinfo_split+1;
5904 data.password_len = base->userinfo_len-base->userinfo_split-1;
5907 if(base->host_start > -1) {
5908 data.host = base->canon_uri+base->host_start;
5909 data.host_len = base->host_len;
5910 data.host_type = base->host_type;
5913 if(base->has_port) {
5914 data.has_port = TRUE;
5915 data.port_value = base->port;
5917 } else if(base->scheme_type != URL_SCHEME_FILE)
5918 data.is_opaque = TRUE;
5920 if(relative->path_start == -1 || !relative->path_len) {
5921 if(base->path_start > -1) {
5922 data.path = base->canon_uri+base->path_start;
5923 data.path_len = base->path_len;
5924 } else if((base->path_start == -1 || !base->path_len) && !data.is_opaque) {
5925 /* Just set the path as a '/' if the base didn't have
5926 * one and if it's an hierarchical URI.
5928 static const WCHAR slashW[] = {'/',0};
5933 if(relative->query_start > -1) {
5934 data.query = relative->canon_uri+relative->query_start;
5935 data.query_len = relative->query_len;
5936 } else if(base->query_start > -1) {
5937 data.query = base->canon_uri+base->query_start;
5938 data.query_len = base->query_len;
5941 const WCHAR *ptr, **pptr;
5942 DWORD path_offset = 0, path_len = 0;
5944 /* There's two possibilities on what will happen to the path component
5945 * of the result IUri. First, if the relative path begins with a '/'
5946 * then the resulting path will just be the relative path. Second, if
5947 * relative path doesn't begin with a '/' then the base path and relative
5948 * path are merged together.
5950 if(relative->path_len && *(relative->canon_uri+relative->path_start) == '/') {
5952 BOOL copy_drive_path = FALSE;
5954 /* If the relative IUri's path starts with a '/', then we
5955 * don't use the base IUri's path. Unless the base IUri
5956 * is a file URI, in which case it uses the drive path of
5957 * the base IUri (if it has any) in the new path.
5959 if(base->scheme_type == URL_SCHEME_FILE) {
5960 if(base->path_len > 3 && *(base->canon_uri+base->path_start) == '/' &&
5961 is_drive_path(base->canon_uri+base->path_start+1)) {
5963 copy_drive_path = TRUE;
5967 path_len += relative->path_len;
5969 path = heap_alloc((path_len+1)*sizeof(WCHAR));
5972 return E_OUTOFMEMORY;
5977 /* Copy the base paths, drive path over. */
5978 if(copy_drive_path) {
5979 memcpy(tmp, base->canon_uri+base->path_start, 3*sizeof(WCHAR));
5983 memcpy(tmp, relative->canon_uri+relative->path_start, relative->path_len*sizeof(WCHAR));
5984 path[path_len] = '\0';
5986 /* Merge the base path with the relative path. */
5987 hr = merge_paths(&data, base->canon_uri+base->path_start, base->path_len,
5988 relative->canon_uri+relative->path_start, relative->path_len,
5989 &path, &path_len, flags);
5995 /* If the resulting IUri is a file URI, the drive path isn't
5996 * reduced out when the dot segments are removed.
5998 if(path_len >= 3 && data.scheme_type == URL_SCHEME_FILE && !data.host) {
5999 if(*path == '/' && is_drive_path(path+1))
6001 else if(is_drive_path(path))
6006 /* Check if the dot segments need to be removed from the path. */
6007 if(!(flags & URL_DONT_SIMPLIFY) && !data.is_opaque) {
6008 DWORD offset = (path_offset > 0) ? path_offset+1 : 0;
6009 DWORD new_len = remove_dot_segments(path+offset,path_len-offset);
6011 if(new_len != path_len) {
6012 WCHAR *tmp = heap_realloc(path, (path_offset+new_len+1)*sizeof(WCHAR));
6016 return E_OUTOFMEMORY;
6019 tmp[new_len+offset] = '\0';
6021 path_len = new_len+offset;
6025 /* Make sure the path component is valid. */
6028 if((data.is_opaque && !parse_path_opaque(pptr, &data, 0)) ||
6029 (!data.is_opaque && !parse_path_hierarchical(pptr, &data, 0))) {
6032 return E_INVALIDARG;
6036 if(relative->fragment_start > -1) {
6037 data.fragment = relative->canon_uri+relative->fragment_start;
6038 data.fragment_len = relative->fragment_len;
6041 if(flags & URL_DONT_SIMPLIFY)
6042 raw_flags |= RAW_URI_FORCE_PORT_DISP;
6043 if(flags & URL_FILE_USE_PATHURL)
6044 raw_flags |= RAW_URI_CONVERT_TO_DOS_PATH;
6046 len = generate_raw_uri(&data, data.uri, raw_flags);
6047 data.uri = SysAllocStringLen(NULL, len);
6051 return E_OUTOFMEMORY;
6054 generate_raw_uri(&data, data.uri, raw_flags);
6056 ret = create_uri_obj();
6058 SysFreeString(data.uri);
6061 return E_OUTOFMEMORY;
6064 if(flags & URL_DONT_SIMPLIFY)
6065 create_flags |= Uri_CREATE_NO_CANONICALIZE;
6066 if(flags & URL_FILE_USE_PATHURL)
6067 create_flags |= Uri_CREATE_FILE_USE_DOS_PATH;
6069 ret->raw_uri = data.uri;
6070 hr = canonicalize_uri(&data, ret, create_flags);
6072 IUri_Release(&ret->IUri_iface);
6077 if(flags & URL_DONT_SIMPLIFY)
6078 ret->display_modifiers |= URI_DISPLAY_NO_DEFAULT_PORT_AUTH;
6080 apply_default_flags(&create_flags);
6081 ret->create_flags = create_flags;
6082 *result = &ret->IUri_iface;
6090 /***********************************************************************
6091 * CoInternetCombineIUri (urlmon.@)
6093 HRESULT WINAPI CoInternetCombineIUri(IUri *pBaseUri, IUri *pRelativeUri, DWORD dwCombineFlags,
6094 IUri **ppCombinedUri, DWORD_PTR dwReserved)
6097 IInternetProtocolInfo *info;
6098 Uri *relative, *base;
6099 TRACE("(%p %p %x %p %x)\n", pBaseUri, pRelativeUri, dwCombineFlags, ppCombinedUri, (DWORD)dwReserved);
6102 return E_INVALIDARG;
6104 if(!pBaseUri || !pRelativeUri) {
6105 *ppCombinedUri = NULL;
6106 return E_INVALIDARG;
6109 relative = get_uri_obj(pRelativeUri);
6110 base = get_uri_obj(pBaseUri);
6111 if(!relative || !base) {
6112 *ppCombinedUri = NULL;
6113 FIXME("(%p %p %x %p %x) Unknown IUri types not supported yet.\n",
6114 pBaseUri, pRelativeUri, dwCombineFlags, ppCombinedUri, (DWORD)dwReserved);
6118 info = get_protocol_info(base->canon_uri);
6120 WCHAR result[INTERNET_MAX_URL_LENGTH+1];
6121 DWORD result_len = 0;
6123 hr = IInternetProtocolInfo_CombineUrl(info, base->canon_uri, relative->canon_uri, dwCombineFlags,
6124 result, INTERNET_MAX_URL_LENGTH+1, &result_len, 0);
6125 IInternetProtocolInfo_Release(info);
6127 hr = CreateUri(result, Uri_CREATE_ALLOW_RELATIVE, 0, ppCombinedUri);
6133 return combine_uri(base, relative, dwCombineFlags, ppCombinedUri, 0);
6136 /***********************************************************************
6137 * CoInternetCombineUrlEx (urlmon.@)
6139 HRESULT WINAPI CoInternetCombineUrlEx(IUri *pBaseUri, LPCWSTR pwzRelativeUrl, DWORD dwCombineFlags,
6140 IUri **ppCombinedUri, DWORD_PTR dwReserved)
6145 IInternetProtocolInfo *info;
6147 TRACE("(%p %s %x %p %x) stub\n", pBaseUri, debugstr_w(pwzRelativeUrl), dwCombineFlags,
6148 ppCombinedUri, (DWORD)dwReserved);
6153 if(!pwzRelativeUrl) {
6154 *ppCombinedUri = NULL;
6155 return E_UNEXPECTED;
6159 *ppCombinedUri = NULL;
6160 return E_INVALIDARG;
6163 base = get_uri_obj(pBaseUri);
6165 *ppCombinedUri = NULL;
6166 FIXME("(%p %s %x %p %x) Unknown IUri's not supported yet.\n", pBaseUri, debugstr_w(pwzRelativeUrl),
6167 dwCombineFlags, ppCombinedUri, (DWORD)dwReserved);
6171 info = get_protocol_info(base->canon_uri);
6173 WCHAR result[INTERNET_MAX_URL_LENGTH+1];
6174 DWORD result_len = 0;
6176 hr = IInternetProtocolInfo_CombineUrl(info, base->canon_uri, pwzRelativeUrl, dwCombineFlags,
6177 result, INTERNET_MAX_URL_LENGTH+1, &result_len, 0);
6178 IInternetProtocolInfo_Release(info);
6180 hr = CreateUri(result, Uri_CREATE_ALLOW_RELATIVE, 0, ppCombinedUri);
6186 hr = CreateUri(pwzRelativeUrl, Uri_CREATE_ALLOW_RELATIVE, 0, &relative);
6188 *ppCombinedUri = NULL;
6192 hr = combine_uri(base, get_uri_obj(relative), dwCombineFlags, ppCombinedUri, COMBINE_URI_FORCE_FLAG_USE);
6194 IUri_Release(relative);
6198 static HRESULT parse_canonicalize(const Uri *uri, DWORD flags, LPWSTR output,
6199 DWORD output_len, DWORD *result_len)
6201 const WCHAR *ptr = NULL;
6204 WCHAR buffer[INTERNET_MAX_URL_LENGTH+1];
6208 /* URL_UNESCAPE only has effect if none of the URL_ESCAPE flags are set. */
6209 const BOOL allow_unescape = !(flags & URL_ESCAPE_UNSAFE) &&
6210 !(flags & URL_ESCAPE_SPACES_ONLY) &&
6211 !(flags & URL_ESCAPE_PERCENT);
6214 /* Check if the dot segments need to be removed from the
6217 if(uri->scheme_start > -1 && uri->path_start > -1) {
6218 ptr = uri->canon_uri+uri->scheme_start+uri->scheme_len+1;
6221 reduce_path = !(flags & URL_NO_META) &&
6222 !(flags & URL_DONT_SIMPLIFY) &&
6223 ptr && check_hierarchical(pptr);
6225 for(ptr = uri->canon_uri; ptr < uri->canon_uri+uri->canon_len; ++ptr) {
6226 BOOL do_default_action = TRUE;
6228 /* Keep track of the path if we need to remove dot segments from
6231 if(reduce_path && !path && ptr == uri->canon_uri+uri->path_start)
6234 /* Check if it's time to reduce the path. */
6235 if(reduce_path && ptr == uri->canon_uri+uri->path_start+uri->path_len) {
6236 DWORD current_path_len = (buffer+len) - path;
6237 DWORD new_path_len = remove_dot_segments(path, current_path_len);
6239 /* Update the current length. */
6240 len -= (current_path_len-new_path_len);
6241 reduce_path = FALSE;
6245 const WCHAR decoded = decode_pct_val(ptr);
6247 if(allow_unescape && (flags & URL_UNESCAPE)) {
6248 buffer[len++] = decoded;
6250 do_default_action = FALSE;
6254 /* See if %'s needed to encoded. */
6255 if(do_default_action && (flags & URL_ESCAPE_PERCENT)) {
6256 pct_encode_val(*ptr, buffer+len);
6258 do_default_action = FALSE;
6260 } else if(*ptr == ' ') {
6261 if((flags & URL_ESCAPE_SPACES_ONLY) &&
6262 !(flags & URL_ESCAPE_UNSAFE)) {
6263 pct_encode_val(*ptr, buffer+len);
6265 do_default_action = FALSE;
6267 } else if(!is_reserved(*ptr) && !is_unreserved(*ptr)) {
6268 if(flags & URL_ESCAPE_UNSAFE) {
6269 pct_encode_val(*ptr, buffer+len);
6271 do_default_action = FALSE;
6275 if(do_default_action)
6276 buffer[len++] = *ptr;
6279 /* Sometimes the path is the very last component of the IUri, so
6280 * see if the dot segments need to be reduced now.
6282 if(reduce_path && path) {
6283 DWORD current_path_len = (buffer+len) - path;
6284 DWORD new_path_len = remove_dot_segments(path, current_path_len);
6286 /* Update the current length. */
6287 len -= (current_path_len-new_path_len);
6292 /* The null terminator isn't included the length. */
6293 *result_len = len-1;
6294 if(len > output_len)
6295 return STRSAFE_E_INSUFFICIENT_BUFFER;
6297 memcpy(output, buffer, len*sizeof(WCHAR));
6302 static HRESULT parse_friendly(IUri *uri, LPWSTR output, DWORD output_len,
6309 hr = IUri_GetPropertyLength(uri, Uri_PROPERTY_DISPLAY_URI, &display_len, 0);
6315 *result_len = display_len;
6316 if(display_len+1 > output_len)
6317 return STRSAFE_E_INSUFFICIENT_BUFFER;
6319 hr = IUri_GetDisplayUri(uri, &display);
6325 memcpy(output, display, (display_len+1)*sizeof(WCHAR));
6326 SysFreeString(display);
6330 static HRESULT parse_rootdocument(const Uri *uri, LPWSTR output, DWORD output_len,
6333 static const WCHAR colon_slashesW[] = {':','/','/'};
6338 /* Windows only returns the root document if the URI has an authority
6339 * and it's not an unknown scheme type or a file scheme type.
6341 if(uri->authority_start == -1 ||
6342 uri->scheme_type == URL_SCHEME_UNKNOWN ||
6343 uri->scheme_type == URL_SCHEME_FILE) {
6346 return STRSAFE_E_INSUFFICIENT_BUFFER;
6352 len = uri->scheme_len+uri->authority_len;
6353 /* For the "://" and '/' which will be added. */
6356 if(len+1 > output_len) {
6358 return STRSAFE_E_INSUFFICIENT_BUFFER;
6362 memcpy(ptr, uri->canon_uri+uri->scheme_start, uri->scheme_len*sizeof(WCHAR));
6364 /* Add the "://". */
6365 ptr += uri->scheme_len;
6366 memcpy(ptr, colon_slashesW, sizeof(colon_slashesW));
6368 /* Add the authority. */
6369 ptr += sizeof(colon_slashesW)/sizeof(WCHAR);
6370 memcpy(ptr, uri->canon_uri+uri->authority_start, uri->authority_len*sizeof(WCHAR));
6372 /* Add the '/' after the authority. */
6373 ptr += uri->authority_len;
6381 static HRESULT parse_document(const Uri *uri, LPWSTR output, DWORD output_len,
6386 /* It has to be a known scheme type, but, it can't be a file
6387 * scheme. It also has to hierarchical.
6389 if(uri->scheme_type == URL_SCHEME_UNKNOWN ||
6390 uri->scheme_type == URL_SCHEME_FILE ||
6391 uri->authority_start == -1) {
6394 return STRSAFE_E_INSUFFICIENT_BUFFER;
6400 if(uri->fragment_start > -1)
6401 len = uri->fragment_start;
6403 len = uri->canon_len;
6406 if(len+1 > output_len)
6407 return STRSAFE_E_INSUFFICIENT_BUFFER;
6409 memcpy(output, uri->canon_uri, len*sizeof(WCHAR));
6414 static HRESULT parse_path_from_url(const Uri *uri, LPWSTR output, DWORD output_len,
6417 const WCHAR *path_ptr;
6418 WCHAR buffer[INTERNET_MAX_URL_LENGTH+1];
6421 if(uri->scheme_type != URL_SCHEME_FILE) {
6425 return E_INVALIDARG;
6429 if(uri->host_start > -1) {
6430 static const WCHAR slash_slashW[] = {'\\','\\'};
6432 memcpy(ptr, slash_slashW, sizeof(slash_slashW));
6433 ptr += sizeof(slash_slashW)/sizeof(WCHAR);
6434 memcpy(ptr, uri->canon_uri+uri->host_start, uri->host_len*sizeof(WCHAR));
6435 ptr += uri->host_len;
6438 path_ptr = uri->canon_uri+uri->path_start;
6439 if(uri->path_len > 3 && *path_ptr == '/' && is_drive_path(path_ptr+1))
6440 /* Skip past the '/' in front of the drive path. */
6443 for(; path_ptr < uri->canon_uri+uri->path_start+uri->path_len; ++path_ptr, ++ptr) {
6444 BOOL do_default_action = TRUE;
6446 if(*path_ptr == '%') {
6447 const WCHAR decoded = decode_pct_val(path_ptr);
6451 do_default_action = FALSE;
6453 } else if(*path_ptr == '/') {
6455 do_default_action = FALSE;
6458 if(do_default_action)
6464 *result_len = ptr-buffer;
6465 if(*result_len+1 > output_len)
6466 return STRSAFE_E_INSUFFICIENT_BUFFER;
6468 memcpy(output, buffer, (*result_len+1)*sizeof(WCHAR));
6472 static HRESULT parse_url_from_path(IUri *uri, LPWSTR output, DWORD output_len,
6479 hr = IUri_GetPropertyLength(uri, Uri_PROPERTY_ABSOLUTE_URI, &len, 0);
6486 if(len+1 > output_len)
6487 return STRSAFE_E_INSUFFICIENT_BUFFER;
6489 hr = IUri_GetAbsoluteUri(uri, &received);
6495 memcpy(output, received, (len+1)*sizeof(WCHAR));
6496 SysFreeString(received);
6501 static HRESULT parse_schema(IUri *uri, LPWSTR output, DWORD output_len,
6508 hr = IUri_GetPropertyLength(uri, Uri_PROPERTY_SCHEME_NAME, &len, 0);
6515 if(len+1 > output_len)
6516 return STRSAFE_E_INSUFFICIENT_BUFFER;
6518 hr = IUri_GetSchemeName(uri, &received);
6524 memcpy(output, received, (len+1)*sizeof(WCHAR));
6525 SysFreeString(received);
6530 static HRESULT parse_site(IUri *uri, LPWSTR output, DWORD output_len, DWORD *result_len)
6536 hr = IUri_GetPropertyLength(uri, Uri_PROPERTY_HOST, &len, 0);
6543 if(len+1 > output_len)
6544 return STRSAFE_E_INSUFFICIENT_BUFFER;
6546 hr = IUri_GetHost(uri, &received);
6552 memcpy(output, received, (len+1)*sizeof(WCHAR));
6553 SysFreeString(received);
6558 static HRESULT parse_domain(IUri *uri, LPWSTR output, DWORD output_len, DWORD *result_len)
6564 hr = IUri_GetPropertyLength(uri, Uri_PROPERTY_DOMAIN, &len, 0);
6571 if(len+1 > output_len)
6572 return STRSAFE_E_INSUFFICIENT_BUFFER;
6574 hr = IUri_GetDomain(uri, &received);
6580 memcpy(output, received, (len+1)*sizeof(WCHAR));
6581 SysFreeString(received);
6586 static HRESULT parse_anchor(IUri *uri, LPWSTR output, DWORD output_len, DWORD *result_len)
6592 hr = IUri_GetPropertyLength(uri, Uri_PROPERTY_FRAGMENT, &len, 0);
6599 if(len+1 > output_len)
6600 return STRSAFE_E_INSUFFICIENT_BUFFER;
6602 hr = IUri_GetFragment(uri, &received);
6608 memcpy(output, received, (len+1)*sizeof(WCHAR));
6609 SysFreeString(received);
6614 /***********************************************************************
6615 * CoInternetParseIUri (urlmon.@)
6617 HRESULT WINAPI CoInternetParseIUri(IUri *pIUri, PARSEACTION ParseAction, DWORD dwFlags,
6618 LPWSTR pwzResult, DWORD cchResult, DWORD *pcchResult,
6619 DWORD_PTR dwReserved)
6623 IInternetProtocolInfo *info;
6625 TRACE("(%p %d %x %p %d %p %x)\n", pIUri, ParseAction, dwFlags, pwzResult,
6626 cchResult, pcchResult, (DWORD)dwReserved);
6631 if(!pwzResult || !pIUri) {
6633 return E_INVALIDARG;
6636 if(!(uri = get_uri_obj(pIUri))) {
6638 FIXME("(%p %d %x %p %d %p %x) Unknown IUri's not supported for this action.\n",
6639 pIUri, ParseAction, dwFlags, pwzResult, cchResult, pcchResult, (DWORD)dwReserved);
6643 info = get_protocol_info(uri->canon_uri);
6645 hr = IInternetProtocolInfo_ParseUrl(info, uri->canon_uri, ParseAction, dwFlags,
6646 pwzResult, cchResult, pcchResult, 0);
6647 IInternetProtocolInfo_Release(info);
6648 if(SUCCEEDED(hr)) return hr;
6651 switch(ParseAction) {
6652 case PARSE_CANONICALIZE:
6653 hr = parse_canonicalize(uri, dwFlags, pwzResult, cchResult, pcchResult);
6655 case PARSE_FRIENDLY:
6656 hr = parse_friendly(pIUri, pwzResult, cchResult, pcchResult);
6658 case PARSE_ROOTDOCUMENT:
6659 hr = parse_rootdocument(uri, pwzResult, cchResult, pcchResult);
6661 case PARSE_DOCUMENT:
6662 hr = parse_document(uri, pwzResult, cchResult, pcchResult);
6664 case PARSE_PATH_FROM_URL:
6665 hr = parse_path_from_url(uri, pwzResult, cchResult, pcchResult);
6667 case PARSE_URL_FROM_PATH:
6668 hr = parse_url_from_path(pIUri, pwzResult, cchResult, pcchResult);
6671 hr = parse_schema(pIUri, pwzResult, cchResult, pcchResult);
6674 hr = parse_site(pIUri, pwzResult, cchResult, pcchResult);
6677 hr = parse_domain(pIUri, pwzResult, cchResult, pcchResult);
6679 case PARSE_LOCATION:
6681 hr = parse_anchor(pIUri, pwzResult, cchResult, pcchResult);
6683 case PARSE_SECURITY_URL:
6686 case PARSE_SECURITY_DOMAIN:
6693 FIXME("(%p %d %x %p %d %p %x) Partial stub.\n", pIUri, ParseAction, dwFlags,
6694 pwzResult, cchResult, pcchResult, (DWORD)dwReserved);