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}};
51 const IUriVtbl *lpIUriVtbl;
56 /* Information about the canonicalized URI's buffer. */
60 BOOL display_modifiers;
65 URL_SCHEME scheme_type;
73 Uri_HOST_TYPE host_type;
96 const IUriBuilderVtbl *lpIUriBuilderVtbl;
100 DWORD modified_props;
133 /* IPv6 addresses can hold up to 8 h16 components. */
137 /* An IPv6 can have 1 elision ("::"). */
138 const WCHAR *elision;
140 /* An IPv6 can contain 1 IPv4 address as the last 32bits of the address. */
153 BOOL has_implicit_scheme;
154 BOOL has_implicit_ip;
159 URL_SCHEME scheme_type;
161 const WCHAR *username;
164 const WCHAR *password;
169 Uri_HOST_TYPE host_type;
172 ipv6_address ipv6_address;
185 const WCHAR *fragment;
189 static const CHAR hexDigits[] = "0123456789ABCDEF";
191 /* List of scheme types/scheme names that are recognized by the IUri interface as of IE 7. */
192 static const struct {
194 WCHAR scheme_name[16];
195 } recognized_schemes[] = {
196 {URL_SCHEME_FTP, {'f','t','p',0}},
197 {URL_SCHEME_HTTP, {'h','t','t','p',0}},
198 {URL_SCHEME_GOPHER, {'g','o','p','h','e','r',0}},
199 {URL_SCHEME_MAILTO, {'m','a','i','l','t','o',0}},
200 {URL_SCHEME_NEWS, {'n','e','w','s',0}},
201 {URL_SCHEME_NNTP, {'n','n','t','p',0}},
202 {URL_SCHEME_TELNET, {'t','e','l','n','e','t',0}},
203 {URL_SCHEME_WAIS, {'w','a','i','s',0}},
204 {URL_SCHEME_FILE, {'f','i','l','e',0}},
205 {URL_SCHEME_MK, {'m','k',0}},
206 {URL_SCHEME_HTTPS, {'h','t','t','p','s',0}},
207 {URL_SCHEME_SHELL, {'s','h','e','l','l',0}},
208 {URL_SCHEME_SNEWS, {'s','n','e','w','s',0}},
209 {URL_SCHEME_LOCAL, {'l','o','c','a','l',0}},
210 {URL_SCHEME_JAVASCRIPT, {'j','a','v','a','s','c','r','i','p','t',0}},
211 {URL_SCHEME_VBSCRIPT, {'v','b','s','c','r','i','p','t',0}},
212 {URL_SCHEME_ABOUT, {'a','b','o','u','t',0}},
213 {URL_SCHEME_RES, {'r','e','s',0}},
214 {URL_SCHEME_MSSHELLROOTED, {'m','s','-','s','h','e','l','l','-','r','o','o','t','e','d',0}},
215 {URL_SCHEME_MSSHELLIDLIST, {'m','s','-','s','h','e','l','l','-','i','d','l','i','s','t',0}},
216 {URL_SCHEME_MSHELP, {'h','c','p',0}},
217 {URL_SCHEME_WILDCARD, {'*',0}}
220 /* List of default ports Windows recognizes. */
221 static const struct {
224 } default_ports[] = {
225 {URL_SCHEME_FTP, 21},
226 {URL_SCHEME_HTTP, 80},
227 {URL_SCHEME_GOPHER, 70},
228 {URL_SCHEME_NNTP, 119},
229 {URL_SCHEME_TELNET, 23},
230 {URL_SCHEME_WAIS, 210},
231 {URL_SCHEME_HTTPS, 443},
234 /* List of 3 character top level domain names Windows seems to recognize.
235 * There might be more, but, these are the only ones I've found so far.
237 static const struct {
239 } recognized_tlds[] = {
249 static Uri *get_uri_obj(IUri *uri)
254 hres = IUri_QueryInterface(uri, &IID_IUriObj, (void**)&ret);
255 return SUCCEEDED(hres) ? ret : NULL;
258 static inline BOOL is_alpha(WCHAR val) {
259 return ((val >= 'a' && val <= 'z') || (val >= 'A' && val <= 'Z'));
262 static inline BOOL is_num(WCHAR val) {
263 return (val >= '0' && val <= '9');
266 static inline BOOL is_drive_path(const WCHAR *str) {
267 return (is_alpha(str[0]) && (str[1] == ':' || str[1] == '|'));
270 static inline BOOL is_unc_path(const WCHAR *str) {
271 return (str[0] == '\\' && str[0] == '\\');
274 static inline BOOL is_forbidden_dos_path_char(WCHAR val) {
275 return (val == '>' || val == '<' || val == '\"');
278 /* A URI is implicitly a file path if it begins with
279 * a drive letter (eg X:) or starts with "\\" (UNC path).
281 static inline BOOL is_implicit_file_path(const WCHAR *str) {
282 return (is_unc_path(str) || (is_alpha(str[0]) && str[1] == ':'));
285 /* Checks if the URI is a hierarchical URI. A hierarchical
286 * URI is one that has "//" after the scheme.
288 static BOOL check_hierarchical(const WCHAR **ptr) {
289 const WCHAR *start = *ptr;
304 /* unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~" */
305 static inline BOOL is_unreserved(WCHAR val) {
306 return (is_alpha(val) || is_num(val) || val == '-' || val == '.' ||
307 val == '_' || val == '~');
310 /* sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
311 * / "*" / "+" / "," / ";" / "="
313 static inline BOOL is_subdelim(WCHAR val) {
314 return (val == '!' || val == '$' || val == '&' ||
315 val == '\'' || val == '(' || val == ')' ||
316 val == '*' || val == '+' || val == ',' ||
317 val == ';' || val == '=');
320 /* gen-delims = ":" / "/" / "?" / "#" / "[" / "]" / "@" */
321 static inline BOOL is_gendelim(WCHAR val) {
322 return (val == ':' || val == '/' || val == '?' ||
323 val == '#' || val == '[' || val == ']' ||
327 /* Characters that delimit the end of the authority
328 * section of a URI. Sometimes a '\\' is considered
329 * an authority delimeter.
331 static inline BOOL is_auth_delim(WCHAR val, BOOL acceptSlash) {
332 return (val == '#' || val == '/' || val == '?' ||
333 val == '\0' || (acceptSlash && val == '\\'));
336 /* reserved = gen-delims / sub-delims */
337 static inline BOOL is_reserved(WCHAR val) {
338 return (is_subdelim(val) || is_gendelim(val));
341 static inline BOOL is_hexdigit(WCHAR val) {
342 return ((val >= 'a' && val <= 'f') ||
343 (val >= 'A' && val <= 'F') ||
344 (val >= '0' && val <= '9'));
347 static inline BOOL is_path_delim(WCHAR val) {
348 return (!val || val == '#' || val == '?');
351 static BOOL is_default_port(URL_SCHEME scheme, DWORD port) {
354 for(i = 0; i < sizeof(default_ports)/sizeof(default_ports[0]); ++i) {
355 if(default_ports[i].scheme == scheme && default_ports[i].port)
362 /* List of schemes types Windows seems to expect to be hierarchical. */
363 static inline BOOL is_hierarchical_scheme(URL_SCHEME type) {
364 return(type == URL_SCHEME_HTTP || type == URL_SCHEME_FTP ||
365 type == URL_SCHEME_GOPHER || type == URL_SCHEME_NNTP ||
366 type == URL_SCHEME_TELNET || type == URL_SCHEME_WAIS ||
367 type == URL_SCHEME_FILE || type == URL_SCHEME_HTTPS ||
368 type == URL_SCHEME_RES);
371 /* Checks if 'flags' contains an invalid combination of Uri_CREATE flags. */
372 static inline BOOL has_invalid_flag_combination(DWORD flags) {
373 return((flags & Uri_CREATE_DECODE_EXTRA_INFO && flags & Uri_CREATE_NO_DECODE_EXTRA_INFO) ||
374 (flags & Uri_CREATE_CANONICALIZE && flags & Uri_CREATE_NO_CANONICALIZE) ||
375 (flags & Uri_CREATE_CRACK_UNKNOWN_SCHEMES && flags & Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES) ||
376 (flags & Uri_CREATE_PRE_PROCESS_HTML_URI && flags & Uri_CREATE_NO_PRE_PROCESS_HTML_URI) ||
377 (flags & Uri_CREATE_IE_SETTINGS && flags & Uri_CREATE_NO_IE_SETTINGS));
380 /* Applies each default Uri_CREATE flags to 'flags' if it
381 * doesn't cause a flag conflict.
383 static void apply_default_flags(DWORD *flags) {
384 if(!(*flags & Uri_CREATE_NO_CANONICALIZE))
385 *flags |= Uri_CREATE_CANONICALIZE;
386 if(!(*flags & Uri_CREATE_NO_DECODE_EXTRA_INFO))
387 *flags |= Uri_CREATE_DECODE_EXTRA_INFO;
388 if(!(*flags & Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES))
389 *flags |= Uri_CREATE_CRACK_UNKNOWN_SCHEMES;
390 if(!(*flags & Uri_CREATE_NO_PRE_PROCESS_HTML_URI))
391 *flags |= Uri_CREATE_PRE_PROCESS_HTML_URI;
392 if(!(*flags & Uri_CREATE_IE_SETTINGS))
393 *flags |= Uri_CREATE_NO_IE_SETTINGS;
396 /* Determines if the URI is hierarchical using the information already parsed into
397 * data and using the current location of parsing in the URI string.
399 * Windows considers a URI hierarchical if on of the following is true:
400 * A.) It's a wildcard scheme.
401 * B.) It's an implicit file scheme.
402 * C.) It's a known hierarchical scheme and it has two '\\' after the scheme name.
403 * (the '\\' will be converted into "//" during canonicalization).
404 * D.) It's not a relative URI and "//" appears after the scheme name.
406 static inline BOOL is_hierarchical_uri(const WCHAR **ptr, const parse_data *data) {
407 const WCHAR *start = *ptr;
409 if(data->scheme_type == URL_SCHEME_WILDCARD)
411 else if(data->scheme_type == URL_SCHEME_FILE && data->has_implicit_scheme)
413 else if(is_hierarchical_scheme(data->scheme_type) && (*ptr)[0] == '\\' && (*ptr)[1] == '\\') {
416 } else if(!data->is_relative && check_hierarchical(ptr))
423 /* Checks if the two Uri's are logically equivalent. It's a simple
424 * comparison, since they are both of type Uri, and it can access
425 * the properties of each Uri directly without the need to go
426 * through the "IUri_Get*" interface calls.
428 static BOOL are_equal_simple(const Uri *a, const Uri *b) {
429 if(a->scheme_type == b->scheme_type) {
430 const BOOL known_scheme = a->scheme_type != URL_SCHEME_UNKNOWN;
431 const BOOL are_hierarchical =
432 (a->authority_start > -1 && b->authority_start > -1);
434 if(a->scheme_type == URL_SCHEME_FILE) {
435 if(a->canon_len == b->canon_len)
436 return !StrCmpIW(a->canon_uri, b->canon_uri);
439 /* Only compare the scheme names (if any) if their unknown scheme types. */
441 if((a->scheme_start > -1 && b->scheme_start > -1) &&
442 (a->scheme_len == b->scheme_len)) {
443 /* Make sure the schemes are the same. */
444 if(StrCmpNW(a->canon_uri+a->scheme_start, b->canon_uri+b->scheme_start, a->scheme_len))
446 } else if(a->scheme_len != b->scheme_len)
447 /* One of the Uri's has a scheme name, while the other doesn't. */
451 /* If they have a userinfo component, perform case sensitive compare. */
452 if((a->userinfo_start > -1 && b->userinfo_start > -1) &&
453 (a->userinfo_len == b->userinfo_len)) {
454 if(StrCmpNW(a->canon_uri+a->userinfo_start, b->canon_uri+b->userinfo_start, a->userinfo_len))
456 } else if(a->userinfo_len != b->userinfo_len)
457 /* One of the Uri's had a userinfo, while the other one doesn't. */
460 /* Check if they have a host name. */
461 if((a->host_start > -1 && b->host_start > -1) &&
462 (a->host_len == b->host_len)) {
463 /* Perform a case insensitive compare if they are a known scheme type. */
465 if(StrCmpNIW(a->canon_uri+a->host_start, b->canon_uri+b->host_start, a->host_len))
467 } else if(StrCmpNW(a->canon_uri+a->host_start, b->canon_uri+b->host_start, a->host_len))
469 } else if(a->host_len != b->host_len)
470 /* One of the Uri's had a host, while the other one didn't. */
473 if(a->has_port && b->has_port) {
474 if(a->port != b->port)
476 } else if(a->has_port || b->has_port)
477 /* One had a port, while the other one didn't. */
480 /* Windows is weird with how it handles paths. For example
481 * One URI could be "http://google.com" (after canonicalization)
482 * and one could be "http://google.com/" and the IsEqual function
483 * would still evaluate to TRUE, but, only if they are both hierarchical
486 if((a->path_start > -1 && b->path_start > -1) &&
487 (a->path_len == b->path_len)) {
488 if(StrCmpNW(a->canon_uri+a->path_start, b->canon_uri+b->path_start, a->path_len))
490 } else if(are_hierarchical && a->path_len == -1 && b->path_len == 0) {
491 if(*(a->canon_uri+a->path_start) != '/')
493 } else if(are_hierarchical && b->path_len == 1 && a->path_len == 0) {
494 if(*(b->canon_uri+b->path_start) != '/')
496 } else if(a->path_len != b->path_len)
499 /* Compare the query strings of the two URIs. */
500 if((a->query_start > -1 && b->query_start > -1) &&
501 (a->query_len == b->query_len)) {
502 if(StrCmpNW(a->canon_uri+a->query_start, b->canon_uri+b->query_start, a->query_len))
504 } else if(a->query_len != b->query_len)
507 if((a->fragment_start > -1 && b->fragment_start > -1) &&
508 (a->fragment_len == b->fragment_len)) {
509 if(StrCmpNW(a->canon_uri+a->fragment_start, b->canon_uri+b->fragment_start, a->fragment_len))
511 } else if(a->fragment_len != b->fragment_len)
514 /* If we get here, the two URIs are equivalent. */
521 /* Computes the size of the given IPv6 address.
522 * Each h16 component is 16bits, if there is an IPv4 address, it's
523 * 32bits. If there's an elision it can be 16bits to 128bits, depending
524 * on the number of other components.
526 * Modeled after google-url's CheckIPv6ComponentsSize function
528 static void compute_ipv6_comps_size(ipv6_address *address) {
529 address->components_size = address->h16_count * 2;
532 /* IPv4 address is 4 bytes. */
533 address->components_size += 4;
535 if(address->elision) {
536 /* An elision can be anywhere from 2 bytes up to 16 bytes.
537 * It size depends on the size of the h16 and IPv4 components.
539 address->elision_size = 16 - address->components_size;
540 if(address->elision_size < 2)
541 address->elision_size = 2;
543 address->elision_size = 0;
546 /* Taken from dlls/jscript/lex.c */
547 static int hex_to_int(WCHAR val) {
548 if(val >= '0' && val <= '9')
550 else if(val >= 'a' && val <= 'f')
551 return val - 'a' + 10;
552 else if(val >= 'A' && val <= 'F')
553 return val - 'A' + 10;
558 /* Helper function for converting a percent encoded string
559 * representation of a WCHAR value into its actual WCHAR value. If
560 * the two characters following the '%' aren't valid hex values then
561 * this function returns the NULL character.
564 * "%2E" will result in '.' being returned by this function.
566 static WCHAR decode_pct_val(const WCHAR *ptr) {
569 if(*ptr == '%' && is_hexdigit(*(ptr + 1)) && is_hexdigit(*(ptr + 2))) {
570 INT a = hex_to_int(*(ptr + 1));
571 INT b = hex_to_int(*(ptr + 2));
580 /* Helper function for percent encoding a given character
581 * and storing the encoded value into a given buffer (dest).
583 * It's up to the calling function to ensure that there is
584 * at least enough space in 'dest' for the percent encoded
585 * value to be stored (so dest + 3 spaces available).
587 static inline void pct_encode_val(WCHAR val, WCHAR *dest) {
589 dest[1] = hexDigits[(val >> 4) & 0xf];
590 dest[2] = hexDigits[val & 0xf];
593 /* Scans the range of characters [str, end] and returns the last occurrence
594 * of 'ch' or returns NULL.
596 static const WCHAR *str_last_of(const WCHAR *str, const WCHAR *end, WCHAR ch) {
597 const WCHAR *ptr = end;
608 /* Attempts to parse the domain name from the host.
610 * This function also includes the Top-level Domain (TLD) name
611 * of the host when it tries to find the domain name. If it finds
612 * a valid domain name it will assign 'domain_start' the offset
613 * into 'host' where the domain name starts.
615 * It's implied that if a domain name its range is implied to be
616 * [host+domain_start, host+host_len).
618 static void find_domain_name(const WCHAR *host, DWORD host_len,
620 const WCHAR *last_tld, *sec_last_tld, *end;
622 end = host+host_len-1;
626 /* There has to be at least enough room for a '.' followed by a
627 * 3 character TLD for a domain to even exist in the host name.
632 last_tld = str_last_of(host, end, '.');
634 /* http://hostname -> has no domain name. */
637 sec_last_tld = str_last_of(host, last_tld-1, '.');
639 /* If the '.' is at the beginning of the host there
640 * has to be at least 3 characters in the TLD for it
642 * Ex: .com -> .com as the domain name.
643 * .co -> has no domain name.
645 if(last_tld-host == 0) {
646 if(end-(last_tld-1) < 3)
648 } else if(last_tld-host == 3) {
651 /* If there's three characters in front of last_tld and
652 * they are on the list of recognized TLDs, then this
653 * host doesn't have a domain (since the host only contains
655 * Ex: edu.uk -> has no domain name.
656 * foo.uk -> foo.uk as the domain name.
658 for(i = 0; i < sizeof(recognized_tlds)/sizeof(recognized_tlds[0]); ++i) {
659 if(!StrCmpNIW(host, recognized_tlds[i].tld_name, 3))
662 } else if(last_tld-host < 3)
663 /* Anything less than 3 characters is considered part
665 * Ex: ak.uk -> Has no domain name.
669 /* Otherwise the domain name is the whole host name. */
671 } else if(end+1-last_tld > 3) {
672 /* If the last_tld has more than 3 characters, then it's automatically
673 * considered the TLD of the domain name.
674 * Ex: www.winehq.org.uk.test -> uk.test as the domain name.
676 *domain_start = (sec_last_tld+1)-host;
677 } else if(last_tld - (sec_last_tld+1) < 4) {
679 /* If the sec_last_tld is 3 characters long it HAS to be on the list of
680 * recognized to still be considered part of the TLD name, otherwise
681 * its considered the domain name.
682 * Ex: www.google.com.uk -> google.com.uk as the domain name.
683 * www.google.foo.uk -> foo.uk as the domain name.
685 if(last_tld - (sec_last_tld+1) == 3) {
686 for(i = 0; i < sizeof(recognized_tlds)/sizeof(recognized_tlds[0]); ++i) {
687 if(!StrCmpNIW(sec_last_tld+1, recognized_tlds[i].tld_name, 3)) {
688 const WCHAR *domain = str_last_of(host, sec_last_tld-1, '.');
693 *domain_start = (domain+1) - host;
694 TRACE("Found domain name %s\n", debugstr_wn(host+*domain_start,
695 (host+host_len)-(host+*domain_start)));
700 *domain_start = (sec_last_tld+1)-host;
702 /* Since the sec_last_tld is less than 3 characters it's considered
704 * Ex: www.google.fo.uk -> google.fo.uk as the domain name.
706 const WCHAR *domain = str_last_of(host, sec_last_tld-1, '.');
711 *domain_start = (domain+1) - host;
714 /* The second to last TLD has more than 3 characters making it
716 * Ex: www.google.test.us -> test.us as the domain name.
718 *domain_start = (sec_last_tld+1)-host;
721 TRACE("Found domain name %s\n", debugstr_wn(host+*domain_start,
722 (host+host_len)-(host+*domain_start)));
725 /* Removes the dot segments from a hierarchical URIs path component. This
726 * function performs the removal in place.
728 * This is a modified version of Qt's QUrl function "removeDotsFromPath".
730 * This function returns the new length of the path string.
732 static DWORD remove_dot_segments(WCHAR *path, DWORD path_len) {
734 const WCHAR *in = out;
735 const WCHAR *end = out + path_len;
739 /* A. if the input buffer begins with a prefix of "/./" or "/.",
740 * where "." is a complete path segment, then replace that
741 * prefix with "/" in the input buffer; otherwise,
743 if(in <= end - 3 && in[0] == '/' && in[1] == '.' && in[2] == '/') {
746 } else if(in == end - 2 && in[0] == '/' && in[1] == '.') {
752 /* B. if the input buffer begins with a prefix of "/../" or "/..",
753 * where ".." is a complete path segment, then replace that
754 * prefix with "/" in the input buffer and remove the last
755 * segment and its preceding "/" (if any) from the output
758 if(in <= end - 4 && in[0] == '/' && in[1] == '.' && in[2] == '.' && in[3] == '/') {
759 while(out > path && *(--out) != '/');
763 } else if(in == end - 3 && in[0] == '/' && in[1] == '.' && in[2] == '.') {
764 while(out > path && *(--out) != '/');
773 /* C. move the first path segment in the input buffer to the end of
774 * the output buffer, including the initial "/" character (if
775 * any) and any subsequent characters up to, but not including,
776 * the next "/" character or the end of the input buffer.
779 while(in < end && *in != '/')
784 TRACE("(%p %d): Path after dot segments removed %s len=%d\n", path, path_len,
785 debugstr_wn(path, len), len);
789 /* Attempts to find the file extension in a given path. */
790 static INT find_file_extension(const WCHAR *path, DWORD path_len) {
793 for(end = path+path_len-1; end >= path && *end != '/' && *end != '\\'; --end) {
801 /* Computes the location where the elision should occur in the IPv6
802 * address using the numerical values of each component stored in
803 * 'values'. If the address shouldn't contain an elision then 'index'
804 * is assigned -1 as it's value. Otherwise 'index' will contain the
805 * starting index (into values) where the elision should be, and 'count'
806 * will contain the number of cells the elision covers.
809 * Windows will expand an elision if the elision only represents 1 h16
810 * component of the URI.
812 * Ex: [1::2:3:4:5:6:7] -> [1:0:2:3:4:5:6:7]
814 * If the IPv6 address contains an IPv4 address, the IPv4 address is also
815 * considered for being included as part of an elision if all it's components
818 * Ex: [1:2:3:4:5:6:0.0.0.0] -> [1:2:3:4:5:6::]
820 static void compute_elision_location(const ipv6_address *address, const USHORT values[8],
821 INT *index, DWORD *count) {
822 DWORD i, max_len, cur_len;
823 INT max_index, cur_index;
825 max_len = cur_len = 0;
826 max_index = cur_index = -1;
827 for(i = 0; i < 8; ++i) {
828 BOOL check_ipv4 = (address->ipv4 && i == 6);
829 BOOL is_end = (check_ipv4 || i == 7);
832 /* Check if the IPv4 address contains only zeros. */
833 if(values[i] == 0 && values[i+1] == 0) {
840 } else if(values[i] == 0) {
847 if(is_end || values[i] != 0) {
848 /* We only consider it for an elision if it's
849 * more than 1 component long.
851 if(cur_len > 1 && cur_len > max_len) {
852 /* Found the new elision location. */
854 max_index = cur_index;
857 /* Reset the current range for the next range of zeros. */
867 /* Removes all the leading and trailing white spaces or
868 * control characters from the URI and removes all control
869 * characters inside of the URI string.
871 static BSTR pre_process_uri(LPCWSTR uri) {
874 const WCHAR *start, *end;
880 /* Skip leading controls and whitespace. */
881 while(iscntrlW(*start) || isspaceW(*start)) ++start;
885 /* URI consisted only of control/whitespace. */
886 ret = SysAllocStringLen(NULL, 0);
888 while(iscntrlW(*end) || isspaceW(*end)) --end;
890 buf = heap_alloc(((end+1)-start)*sizeof(WCHAR));
894 for(ptr = buf; start < end+1; ++start) {
895 if(!iscntrlW(*start))
899 ret = SysAllocStringLen(buf, ptr-buf);
906 /* Converts the specified IPv4 address into an uint value.
908 * This function assumes that the IPv4 address has already been validated.
910 static UINT ipv4toui(const WCHAR *ip, DWORD len) {
912 DWORD comp_value = 0;
915 for(ptr = ip; ptr < ip+len; ++ptr) {
921 comp_value = comp_value*10 + (*ptr-'0');
930 /* Converts an IPv4 address in numerical form into it's fully qualified
931 * string form. This function returns the number of characters written
932 * to 'dest'. If 'dest' is NULL this function will return the number of
933 * characters that would have been written.
935 * It's up to the caller to ensure there's enough space in 'dest' for the
938 static DWORD ui2ipv4(WCHAR *dest, UINT address) {
939 static const WCHAR formatW[] =
940 {'%','u','.','%','u','.','%','u','.','%','u',0};
944 digits[0] = (address >> 24) & 0xff;
945 digits[1] = (address >> 16) & 0xff;
946 digits[2] = (address >> 8) & 0xff;
947 digits[3] = address & 0xff;
951 ret = sprintfW(tmp, formatW, digits[0], digits[1], digits[2], digits[3]);
953 ret = sprintfW(dest, formatW, digits[0], digits[1], digits[2], digits[3]);
958 static DWORD ui2str(WCHAR *dest, UINT value) {
959 static const WCHAR formatW[] = {'%','u',0};
964 ret = sprintfW(tmp, formatW, value);
966 ret = sprintfW(dest, formatW, value);
971 /* Converts an h16 component (from an IPv6 address) into it's
974 * This function assumes that the h16 component has already been validated.
976 static USHORT h16tous(h16 component) {
980 for(i = 0; i < component.len; ++i) {
982 ret += hex_to_int(component.str[i]);
988 /* Converts an IPv6 address into it's 128 bits (16 bytes) numerical value.
990 * This function assumes that the ipv6_address has already been validated.
992 static BOOL ipv6_to_number(const ipv6_address *address, USHORT number[8]) {
993 DWORD i, cur_component = 0;
994 BOOL already_passed_elision = FALSE;
996 for(i = 0; i < address->h16_count; ++i) {
997 if(address->elision) {
998 if(address->components[i].str > address->elision && !already_passed_elision) {
999 /* Means we just passed the elision and need to add it's values to
1000 * 'number' before we do anything else.
1003 for(j = 0; j < address->elision_size; j+=2)
1004 number[cur_component++] = 0;
1006 already_passed_elision = TRUE;
1010 number[cur_component++] = h16tous(address->components[i]);
1013 /* Case when the elision appears after the h16 components. */
1014 if(!already_passed_elision && address->elision) {
1015 for(i = 0; i < address->elision_size; i+=2)
1016 number[cur_component++] = 0;
1017 already_passed_elision = TRUE;
1021 UINT value = ipv4toui(address->ipv4, address->ipv4_len);
1023 if(cur_component != 6) {
1024 ERR("(%p %p): Failed sanity check with %d\n", address, number, cur_component);
1028 number[cur_component++] = (value >> 16) & 0xffff;
1029 number[cur_component] = value & 0xffff;
1035 /* Checks if the characters pointed to by 'ptr' are
1036 * a percent encoded data octet.
1038 * pct-encoded = "%" HEXDIG HEXDIG
1040 static BOOL check_pct_encoded(const WCHAR **ptr) {
1041 const WCHAR *start = *ptr;
1047 if(!is_hexdigit(**ptr)) {
1053 if(!is_hexdigit(**ptr)) {
1062 /* dec-octet = DIGIT ; 0-9
1063 * / %x31-39 DIGIT ; 10-99
1064 * / "1" 2DIGIT ; 100-199
1065 * / "2" %x30-34 DIGIT ; 200-249
1066 * / "25" %x30-35 ; 250-255
1068 static BOOL check_dec_octet(const WCHAR **ptr) {
1069 const WCHAR *c1, *c2, *c3;
1072 /* A dec-octet must be at least 1 digit long. */
1073 if(*c1 < '0' || *c1 > '9')
1079 /* Since the 1 digit requirment was meet, it doesn't
1080 * matter if this is a DIGIT value, it's considered a
1083 if(*c2 < '0' || *c2 > '9')
1089 /* Same explanation as above. */
1090 if(*c3 < '0' || *c3 > '9')
1093 /* Anything > 255 isn't a valid IP dec-octet. */
1094 if(*c1 >= '2' && *c2 >= '5' && *c3 >= '5') {
1103 /* Checks if there is an implicit IPv4 address in the host component of the URI.
1104 * The max value of an implicit IPv4 address is UINT_MAX.
1107 * "234567" would be considered an implicit IPv4 address.
1109 static BOOL check_implicit_ipv4(const WCHAR **ptr, UINT *val) {
1110 const WCHAR *start = *ptr;
1114 while(is_num(**ptr)) {
1115 ret = ret*10 + (**ptr - '0');
1117 if(ret > UINT_MAX) {
1131 /* Checks if the string contains an IPv4 address.
1133 * This function has a strict mode or a non-strict mode of operation
1134 * When 'strict' is set to FALSE this function will return TRUE if
1135 * the string contains at least 'dec-octet "." dec-octet' since partial
1136 * IPv4 addresses will be normalized out into full IPv4 addresses. When
1137 * 'strict' is set this function expects there to be a full IPv4 address.
1139 * IPv4address = dec-octet "." dec-octet "." dec-octet "." dec-octet
1141 static BOOL check_ipv4address(const WCHAR **ptr, BOOL strict) {
1142 const WCHAR *start = *ptr;
1144 if(!check_dec_octet(ptr)) {
1155 if(!check_dec_octet(ptr)) {
1169 if(!check_dec_octet(ptr)) {
1183 if(!check_dec_octet(ptr)) {
1188 /* Found a four digit ip address. */
1191 /* Tries to parse the scheme name of the URI.
1193 * scheme = ALPHA *(ALPHA | NUM | '+' | '-' | '.') as defined by RFC 3896.
1194 * NOTE: Windows accepts a number as the first character of a scheme.
1196 static BOOL parse_scheme_name(const WCHAR **ptr, parse_data *data, DWORD extras) {
1197 const WCHAR *start = *ptr;
1199 data->scheme = NULL;
1200 data->scheme_len = 0;
1203 if(**ptr == '*' && *ptr == start) {
1204 /* Might have found a wildcard scheme. If it is the next
1205 * char has to be a ':' for it to be a valid URI
1209 } else if(!is_num(**ptr) && !is_alpha(**ptr) && **ptr != '+' &&
1210 **ptr != '-' && **ptr != '.')
1219 /* Schemes must end with a ':' */
1220 if(**ptr != ':' && !((extras & ALLOW_NULL_TERM_SCHEME) && !**ptr)) {
1225 data->scheme = start;
1226 data->scheme_len = *ptr - start;
1232 /* Tries to deduce the corresponding URL_SCHEME for the given URI. Stores
1233 * the deduced URL_SCHEME in data->scheme_type.
1235 static BOOL parse_scheme_type(parse_data *data) {
1236 /* If there's scheme data then see if it's a recognized scheme. */
1237 if(data->scheme && data->scheme_len) {
1240 for(i = 0; i < sizeof(recognized_schemes)/sizeof(recognized_schemes[0]); ++i) {
1241 if(lstrlenW(recognized_schemes[i].scheme_name) == data->scheme_len) {
1242 /* Has to be a case insensitive compare. */
1243 if(!StrCmpNIW(recognized_schemes[i].scheme_name, data->scheme, data->scheme_len)) {
1244 data->scheme_type = recognized_schemes[i].scheme;
1250 /* If we get here it means it's not a recognized scheme. */
1251 data->scheme_type = URL_SCHEME_UNKNOWN;
1253 } else if(data->is_relative) {
1254 /* Relative URI's have no scheme. */
1255 data->scheme_type = URL_SCHEME_UNKNOWN;
1258 /* Should never reach here! what happened... */
1259 FIXME("(%p): Unable to determine scheme type for URI %s\n", data, debugstr_w(data->uri));
1264 /* Tries to parse (or deduce) the scheme_name of a URI. If it can't
1265 * parse a scheme from the URI it will try to deduce the scheme_name and scheme_type
1266 * using the flags specified in 'flags' (if any). Flags that affect how this function
1267 * operates are the Uri_CREATE_ALLOW_* flags.
1269 * All parsed/deduced information will be stored in 'data' when the function returns.
1271 * Returns TRUE if it was able to successfully parse the information.
1273 static BOOL parse_scheme(const WCHAR **ptr, parse_data *data, DWORD flags, DWORD extras) {
1274 static const WCHAR fileW[] = {'f','i','l','e',0};
1275 static const WCHAR wildcardW[] = {'*',0};
1277 /* First check to see if the uri could implicitly be a file path. */
1278 if(is_implicit_file_path(*ptr)) {
1279 if(flags & Uri_CREATE_ALLOW_IMPLICIT_FILE_SCHEME) {
1280 data->scheme = fileW;
1281 data->scheme_len = lstrlenW(fileW);
1282 data->has_implicit_scheme = TRUE;
1284 TRACE("(%p %p %x): URI is an implicit file path.\n", ptr, data, flags);
1286 /* Window's does not consider anything that can implicitly be a file
1287 * path to be a valid URI if the ALLOW_IMPLICIT_FILE_SCHEME flag is not set...
1289 TRACE("(%p %p %x): URI is implicitly a file path, but, the ALLOW_IMPLICIT_FILE_SCHEME flag wasn't set.\n",
1293 } else if(!parse_scheme_name(ptr, data, extras)) {
1294 /* No Scheme was found, this means it could be:
1295 * a) an implicit Wildcard scheme
1299 if(flags & Uri_CREATE_ALLOW_IMPLICIT_WILDCARD_SCHEME) {
1300 data->scheme = wildcardW;
1301 data->scheme_len = lstrlenW(wildcardW);
1302 data->has_implicit_scheme = TRUE;
1304 TRACE("(%p %p %x): URI is an implicit wildcard scheme.\n", ptr, data, flags);
1305 } else if (flags & Uri_CREATE_ALLOW_RELATIVE) {
1306 data->is_relative = TRUE;
1307 TRACE("(%p %p %x): URI is relative.\n", ptr, data, flags);
1309 TRACE("(%p %p %x): Malformed URI found. Unable to deduce scheme name.\n", ptr, data, flags);
1314 if(!data->is_relative)
1315 TRACE("(%p %p %x): Found scheme=%s scheme_len=%d\n", ptr, data, flags,
1316 debugstr_wn(data->scheme, data->scheme_len), data->scheme_len);
1318 if(!parse_scheme_type(data))
1321 TRACE("(%p %p %x): Assigned %d as the URL_SCHEME.\n", ptr, data, flags, data->scheme_type);
1325 static BOOL parse_username(const WCHAR **ptr, parse_data *data, DWORD flags, DWORD extras) {
1326 data->username = *ptr;
1328 while(**ptr != ':' && **ptr != '@') {
1330 if(!check_pct_encoded(ptr)) {
1331 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
1332 *ptr = data->username;
1333 data->username = NULL;
1338 } else if(extras & ALLOW_NULL_TERM_USER_NAME && !**ptr)
1340 else if(is_auth_delim(**ptr, data->scheme_type != URL_SCHEME_UNKNOWN)) {
1341 *ptr = data->username;
1342 data->username = NULL;
1349 data->username_len = *ptr - data->username;
1353 static BOOL parse_password(const WCHAR **ptr, parse_data *data, DWORD flags, DWORD extras) {
1354 data->password = *ptr;
1356 while(**ptr != '@') {
1358 if(!check_pct_encoded(ptr)) {
1359 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
1360 *ptr = data->password;
1361 data->password = NULL;
1366 } else if(extras & ALLOW_NULL_TERM_PASSWORD && !**ptr)
1368 else if(is_auth_delim(**ptr, data->scheme_type != URL_SCHEME_UNKNOWN)) {
1369 *ptr = data->password;
1370 data->password = NULL;
1377 data->password_len = *ptr - data->password;
1381 /* Parses the userinfo part of the URI (if it exists). The userinfo field of
1382 * a URI can consist of "username:password@", or just "username@".
1385 * userinfo = *( unreserved / pct-encoded / sub-delims / ":" )
1388 * 1) If there is more than one ':' in the userinfo part of the URI Windows
1389 * uses the first occurrence of ':' to delimit the username and password
1393 * ftp://user:pass:word@winehq.org
1395 * Would yield, "user" as the username and "pass:word" as the password.
1397 * 2) Windows allows any character to appear in the "userinfo" part of
1398 * a URI, as long as it's not an authority delimeter character set.
1400 static void parse_userinfo(const WCHAR **ptr, parse_data *data, DWORD flags) {
1401 const WCHAR *start = *ptr;
1403 if(!parse_username(ptr, data, flags, 0)) {
1404 TRACE("(%p %p %x): URI contained no userinfo.\n", ptr, data, flags);
1410 if(!parse_password(ptr, data, flags, 0)) {
1412 data->username = NULL;
1413 data->username_len = 0;
1414 TRACE("(%p %p %x): URI contained no userinfo.\n", ptr, data, flags);
1421 data->username = NULL;
1422 data->username_len = 0;
1423 data->password = NULL;
1424 data->password_len = 0;
1426 TRACE("(%p %p %x): URI contained no userinfo.\n", ptr, data, flags);
1431 TRACE("(%p %p %x): Found username %s len=%d.\n", ptr, data, flags,
1432 debugstr_wn(data->username, data->username_len), data->username_len);
1435 TRACE("(%p %p %x): Found password %s len=%d.\n", ptr, data, flags,
1436 debugstr_wn(data->password, data->password_len), data->password_len);
1441 /* Attempts to parse a port from the URI.
1444 * Windows seems to have a cap on what the maximum value
1445 * for a port can be. The max value is USHORT_MAX.
1449 static BOOL parse_port(const WCHAR **ptr, parse_data *data, DWORD flags) {
1453 while(!is_auth_delim(**ptr, data->scheme_type != URL_SCHEME_UNKNOWN)) {
1454 if(!is_num(**ptr)) {
1460 port = port*10 + (**ptr-'0');
1462 if(port > USHORT_MAX) {
1471 data->has_port = TRUE;
1472 data->port_value = port;
1473 data->port_len = *ptr - data->port;
1475 TRACE("(%p %p %x): Found port %s len=%d value=%u\n", ptr, data, flags,
1476 debugstr_wn(data->port, data->port_len), data->port_len, data->port_value);
1480 /* Attempts to parse a IPv4 address from the URI.
1483 * Window's normalizes IPv4 addresses, This means there's three
1484 * possibilities for the URI to contain an IPv4 address.
1485 * 1) A well formed address (ex. 192.2.2.2).
1486 * 2) A partially formed address. For example "192.0" would
1487 * normalize to "192.0.0.0" during canonicalization.
1488 * 3) An implicit IPv4 address. For example "256" would
1489 * normalize to "0.0.1.0" during canonicalization. Also
1490 * note that the maximum value for an implicit IP address
1491 * is UINT_MAX, if the value in the URI exceeds this then
1492 * it is not considered an IPv4 address.
1494 static BOOL parse_ipv4address(const WCHAR **ptr, parse_data *data, DWORD flags) {
1495 const BOOL is_unknown = data->scheme_type == URL_SCHEME_UNKNOWN;
1498 if(!check_ipv4address(ptr, FALSE)) {
1499 if(!check_implicit_ipv4(ptr, &data->implicit_ipv4)) {
1500 TRACE("(%p %p %x): URI didn't contain anything looking like an IPv4 address.\n",
1506 data->has_implicit_ip = TRUE;
1509 /* Check if what we found is the only part of the host name (if it isn't
1510 * we don't have an IPv4 address).
1514 if(!parse_port(ptr, data, flags)) {
1519 } else if(!is_auth_delim(**ptr, !is_unknown)) {
1520 /* Found more data which belongs the host, so this isn't an IPv4. */
1523 data->has_implicit_ip = FALSE;
1527 data->host_len = *ptr - data->host;
1528 data->host_type = Uri_HOST_IPV4;
1530 TRACE("(%p %p %x): IPv4 address found. host=%s host_len=%d host_type=%d\n",
1531 ptr, data, flags, debugstr_wn(data->host, data->host_len),
1532 data->host_len, data->host_type);
1536 /* Attempts to parse the reg-name from the URI.
1538 * Because of the way Windows handles ':' this function also
1539 * handles parsing the port.
1541 * reg-name = *( unreserved / pct-encoded / sub-delims )
1544 * Windows allows everything, but, the characters in "auth_delims" and ':'
1545 * to appear in a reg-name, unless it's an unknown scheme type then ':' is
1546 * allowed to appear (even if a valid port isn't after it).
1548 * Windows doesn't like host names which start with '[' and end with ']'
1549 * and don't contain a valid IP literal address in between them.
1551 * On Windows if an '[' is encountered in the host name the ':' no longer
1552 * counts as a delimiter until you reach the next ']' or an "authority delimeter".
1554 * A reg-name CAN be empty.
1556 static BOOL parse_reg_name(const WCHAR **ptr, parse_data *data, DWORD flags, DWORD extras) {
1557 const BOOL has_start_bracket = **ptr == '[';
1558 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
1559 const BOOL is_res = data->scheme_type == URL_SCHEME_RES;
1560 BOOL inside_brackets = has_start_bracket;
1562 /* res URIs don't have ports. */
1563 BOOL ignore_col = (extras & IGNORE_PORT_DELIMITER) || is_res;
1565 /* We have to be careful with file schemes. */
1566 if(data->scheme_type == URL_SCHEME_FILE) {
1567 /* This is because an implicit file scheme could be "C:\\test" and it
1568 * would trick this function into thinking the host is "C", when after
1569 * canonicalization the host would end up being an empty string. A drive
1570 * path can also have a '|' instead of a ':' after the drive letter.
1572 if(is_drive_path(*ptr)) {
1573 /* Regular old drive paths don't have a host type (or host name). */
1574 data->host_type = Uri_HOST_UNKNOWN;
1578 } else if(is_unc_path(*ptr))
1579 /* Skip past the "\\" of a UNC path. */
1585 /* For res URIs, everything before the first '/' is
1586 * considered the host.
1588 while((!is_res && !is_auth_delim(**ptr, known_scheme)) ||
1589 (is_res && **ptr && **ptr != '/')) {
1590 if(**ptr == ':' && !ignore_col) {
1591 /* We can ignore ':' if were inside brackets.*/
1592 if(!inside_brackets) {
1593 const WCHAR *tmp = (*ptr)++;
1595 /* Attempt to parse the port. */
1596 if(!parse_port(ptr, data, flags)) {
1597 /* Windows expects there to be a valid port for known scheme types. */
1598 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
1601 TRACE("(%p %p %x %x): Expected valid port\n", ptr, data, flags, extras);
1604 /* Windows gives up on trying to parse a port when it
1605 * encounters 1 invalid port.
1609 data->host_len = tmp - data->host;
1613 } else if(**ptr == '%' && (known_scheme && !is_res)) {
1614 /* Has to be a legit % encoded value. */
1615 if(!check_pct_encoded(ptr)) {
1621 } else if(is_res && is_forbidden_dos_path_char(**ptr)) {
1625 } else if(**ptr == ']')
1626 inside_brackets = FALSE;
1627 else if(**ptr == '[')
1628 inside_brackets = TRUE;
1633 if(has_start_bracket) {
1634 /* Make sure the last character of the host wasn't a ']'. */
1635 if(*(*ptr-1) == ']') {
1636 TRACE("(%p %p %x %x): Expected an IP literal inside of the host\n",
1637 ptr, data, flags, extras);
1644 /* Don't overwrite our length if we found a port earlier. */
1646 data->host_len = *ptr - data->host;
1648 /* If the host is empty, then it's an unknown host type. */
1649 if(data->host_len == 0 || is_res)
1650 data->host_type = Uri_HOST_UNKNOWN;
1652 data->host_type = Uri_HOST_DNS;
1654 TRACE("(%p %p %x %x): Parsed reg-name. host=%s len=%d\n", ptr, data, flags, extras,
1655 debugstr_wn(data->host, data->host_len), data->host_len);
1659 /* Attempts to parse an IPv6 address out of the URI.
1661 * IPv6address = 6( h16 ":" ) ls32
1662 * / "::" 5( h16 ":" ) ls32
1663 * / [ h16 ] "::" 4( h16 ":" ) ls32
1664 * / [ *1( h16 ":" ) h16 ] "::" 3( h16 ":" ) ls32
1665 * / [ *2( h16 ":" ) h16 ] "::" 2( h16 ":" ) ls32
1666 * / [ *3( h16 ":" ) h16 ] "::" h16 ":" ls32
1667 * / [ *4( h16 ":" ) h16 ] "::" ls32
1668 * / [ *5( h16 ":" ) h16 ] "::" h16
1669 * / [ *6( h16 ":" ) h16 ] "::"
1671 * ls32 = ( h16 ":" h16 ) / IPv4address
1672 * ; least-significant 32 bits of address.
1675 * ; 16 bits of address represented in hexadecimal.
1677 * Modeled after google-url's 'DoParseIPv6' function.
1679 static BOOL parse_ipv6address(const WCHAR **ptr, parse_data *data, DWORD flags) {
1680 const WCHAR *start, *cur_start;
1683 start = cur_start = *ptr;
1684 memset(&ip, 0, sizeof(ipv6_address));
1687 /* Check if we're on the last character of the host. */
1688 BOOL is_end = (is_auth_delim(**ptr, data->scheme_type != URL_SCHEME_UNKNOWN)
1691 BOOL is_split = (**ptr == ':');
1692 BOOL is_elision = (is_split && !is_end && *(*ptr+1) == ':');
1694 /* Check if we're at the end of a component, or
1695 * if we're at the end of the IPv6 address.
1697 if(is_split || is_end) {
1700 cur_len = *ptr - cur_start;
1702 /* h16 can't have a length > 4. */
1706 TRACE("(%p %p %x): h16 component to long.\n",
1712 /* An h16 component can't have the length of 0 unless
1713 * the elision is at the beginning of the address, or
1714 * at the end of the address.
1716 if(!((*ptr == start && is_elision) ||
1717 (is_end && (*ptr-2) == ip.elision))) {
1719 TRACE("(%p %p %x): IPv6 component cannot have a length of 0.\n",
1726 /* An IPv6 address can have no more than 8 h16 components. */
1727 if(ip.h16_count >= 8) {
1729 TRACE("(%p %p %x): Not a IPv6 address, to many h16 components.\n",
1734 ip.components[ip.h16_count].str = cur_start;
1735 ip.components[ip.h16_count].len = cur_len;
1737 TRACE("(%p %p %x): Found h16 component %s, len=%d, h16_count=%d\n",
1738 ptr, data, flags, debugstr_wn(cur_start, cur_len), cur_len,
1748 /* A IPv6 address can only have 1 elision ('::'). */
1752 TRACE("(%p %p %x): IPv6 address cannot have 2 elisions.\n",
1764 if(!check_ipv4address(ptr, TRUE)) {
1765 if(!is_hexdigit(**ptr)) {
1766 /* Not a valid character for an IPv6 address. */
1771 /* Found an IPv4 address. */
1772 ip.ipv4 = cur_start;
1773 ip.ipv4_len = *ptr - cur_start;
1775 TRACE("(%p %p %x): Found an attached IPv4 address %s len=%d.\n",
1776 ptr, data, flags, debugstr_wn(ip.ipv4, ip.ipv4_len),
1779 /* IPv4 addresses can only appear at the end of a IPv6. */
1785 compute_ipv6_comps_size(&ip);
1787 /* Make sure the IPv6 address adds up to 16 bytes. */
1788 if(ip.components_size + ip.elision_size != 16) {
1790 TRACE("(%p %p %x): Invalid IPv6 address, did not add up to 16 bytes.\n",
1795 if(ip.elision_size == 2) {
1796 /* For some reason on Windows if an elision that represents
1797 * only 1 h16 component is encountered at the very begin or
1798 * end of an IPv6 address, Windows does not consider it a
1799 * valid IPv6 address.
1801 * Ex: [::2:3:4:5:6:7] is not valid, even though the sum
1802 * of all the components == 128bits.
1804 if(ip.elision < ip.components[0].str ||
1805 ip.elision > ip.components[ip.h16_count-1].str) {
1807 TRACE("(%p %p %x): Invalid IPv6 address. Detected elision of 2 bytes at the beginning or end of the address.\n",
1813 data->host_type = Uri_HOST_IPV6;
1814 data->has_ipv6 = TRUE;
1815 data->ipv6_address = ip;
1817 TRACE("(%p %p %x): Found valid IPv6 literal %s len=%d\n",
1818 ptr, data, flags, debugstr_wn(start, *ptr-start),
1823 /* IPvFuture = "v" 1*HEXDIG "." 1*( unreserved / sub-delims / ":" ) */
1824 static BOOL parse_ipvfuture(const WCHAR **ptr, parse_data *data, DWORD flags) {
1825 const WCHAR *start = *ptr;
1827 /* IPvFuture has to start with a 'v' or 'V'. */
1828 if(**ptr != 'v' && **ptr != 'V')
1831 /* Following the v there must be at least 1 hex digit. */
1833 if(!is_hexdigit(**ptr)) {
1839 while(is_hexdigit(**ptr))
1842 /* End of the hexdigit sequence must be a '.' */
1849 if(!is_unreserved(**ptr) && !is_subdelim(**ptr) && **ptr != ':') {
1855 while(is_unreserved(**ptr) || is_subdelim(**ptr) || **ptr == ':')
1858 data->host_type = Uri_HOST_UNKNOWN;
1860 TRACE("(%p %p %x): Parsed IPvFuture address %s len=%d\n", ptr, data, flags,
1861 debugstr_wn(start, *ptr-start), *ptr-start);
1866 /* IP-literal = "[" ( IPv6address / IPvFuture ) "]" */
1867 static BOOL parse_ip_literal(const WCHAR **ptr, parse_data *data, DWORD flags, DWORD extras) {
1870 if(**ptr != '[' && !(extras & ALLOW_BRACKETLESS_IP_LITERAL)) {
1873 } else if(**ptr == '[')
1876 if(!parse_ipv6address(ptr, data, flags)) {
1877 if(extras & SKIP_IP_FUTURE_CHECK || !parse_ipvfuture(ptr, data, flags)) {
1884 if(**ptr != ']' && !(extras & ALLOW_BRACKETLESS_IP_LITERAL)) {
1888 } else if(!**ptr && extras & ALLOW_BRACKETLESS_IP_LITERAL) {
1889 /* The IP literal didn't contain brackets and was followed by
1890 * a NULL terminator, so no reason to even check the port.
1892 data->host_len = *ptr - data->host;
1899 /* If a valid port is not found, then let it trickle down to
1902 if(!parse_port(ptr, data, flags)) {
1908 data->host_len = *ptr - data->host;
1913 /* Parses the host information from the URI.
1915 * host = IP-literal / IPv4address / reg-name
1917 static BOOL parse_host(const WCHAR **ptr, parse_data *data, DWORD flags, DWORD extras) {
1918 if(!parse_ip_literal(ptr, data, flags, extras)) {
1919 if(!parse_ipv4address(ptr, data, flags)) {
1920 if(!parse_reg_name(ptr, data, flags, extras)) {
1921 TRACE("(%p %p %x %x): Malformed URI, Unknown host type.\n",
1922 ptr, data, flags, extras);
1931 /* Parses the authority information from the URI.
1933 * authority = [ userinfo "@" ] host [ ":" port ]
1935 static BOOL parse_authority(const WCHAR **ptr, parse_data *data, DWORD flags) {
1936 parse_userinfo(ptr, data, flags);
1938 /* Parsing the port will happen during one of the host parsing
1939 * routines (if the URI has a port).
1941 if(!parse_host(ptr, data, flags, 0))
1947 /* Attempts to parse the path information of a hierarchical URI. */
1948 static BOOL parse_path_hierarchical(const WCHAR **ptr, parse_data *data, DWORD flags) {
1949 const WCHAR *start = *ptr;
1950 static const WCHAR slash[] = {'/',0};
1951 const BOOL is_file = data->scheme_type == URL_SCHEME_FILE;
1953 if(is_path_delim(**ptr)) {
1954 if(data->scheme_type == URL_SCHEME_WILDCARD) {
1955 /* Wildcard schemes don't get a '/' attached if their path is
1960 } else if(!(flags & Uri_CREATE_NO_CANONICALIZE)) {
1961 /* If the path component is empty, then a '/' is added. */
1966 while(!is_path_delim(**ptr)) {
1967 if(**ptr == '%' && data->scheme_type != URL_SCHEME_UNKNOWN && !is_file) {
1968 if(!check_pct_encoded(ptr)) {
1973 } else if(is_forbidden_dos_path_char(**ptr) && is_file &&
1974 (flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
1975 /* File schemes with USE_DOS_PATH set aren't allowed to have
1976 * a '<' or '>' or '\"' appear in them.
1980 } else if(**ptr == '\\') {
1981 /* Not allowed to have a backslash if NO_CANONICALIZE is set
1982 * and the scheme is known type (but not a file scheme).
1984 if(flags & Uri_CREATE_NO_CANONICALIZE) {
1985 if(data->scheme_type != URL_SCHEME_FILE &&
1986 data->scheme_type != URL_SCHEME_UNKNOWN) {
1996 /* The only time a URI doesn't have a path is when
1997 * the NO_CANONICALIZE flag is set and the raw URI
1998 * didn't contain one.
2005 data->path_len = *ptr - start;
2010 TRACE("(%p %p %x): Parsed path %s len=%d\n", ptr, data, flags,
2011 debugstr_wn(data->path, data->path_len), data->path_len);
2013 TRACE("(%p %p %x): The URI contained no path\n", ptr, data, flags);
2018 /* Parses the path of a opaque URI (much less strict then the parser
2019 * for a hierarchical URI).
2022 * Windows allows invalid % encoded data to appear in opaque URI paths
2023 * for unknown scheme types.
2025 * File schemes with USE_DOS_PATH set aren't allowed to have '<', '>', or '\"'
2028 static BOOL parse_path_opaque(const WCHAR **ptr, parse_data *data, DWORD flags) {
2029 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
2030 const BOOL is_file = data->scheme_type == URL_SCHEME_FILE;
2034 while(!is_path_delim(**ptr)) {
2035 if(**ptr == '%' && known_scheme) {
2036 if(!check_pct_encoded(ptr)) {
2042 } else if(is_forbidden_dos_path_char(**ptr) && is_file &&
2043 (flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
2052 data->path_len = *ptr - data->path;
2053 TRACE("(%p %p %x): Parsed opaque URI path %s len=%d\n", ptr, data, flags,
2054 debugstr_wn(data->path, data->path_len), data->path_len);
2058 /* Determines how the URI should be parsed after the scheme information.
2060 * If the scheme is followed, by "//" then, it is treated as an hierarchical URI
2061 * which then the authority and path information will be parsed out. Otherwise, the
2062 * URI will be treated as an opaque URI which the authority information is not parsed
2065 * RFC 3896 definition of hier-part:
2067 * hier-part = "//" authority path-abempty
2072 * MSDN opaque URI definition:
2073 * scheme ":" path [ "#" fragment ]
2076 * If the URI is of an unknown scheme type and has a "//" following the scheme then it
2077 * is treated as a hierarchical URI, but, if the CREATE_NO_CRACK_UNKNOWN_SCHEMES flag is
2078 * set then it is considered an opaque URI reguardless of what follows the scheme information
2079 * (per MSDN documentation).
2081 static BOOL parse_hierpart(const WCHAR **ptr, parse_data *data, DWORD flags) {
2082 const WCHAR *start = *ptr;
2084 /* Checks if the authority information needs to be parsed. */
2085 if(is_hierarchical_uri(ptr, data)) {
2086 /* Only treat it as a hierarchical URI if the scheme_type is known or
2087 * the Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES flag is not set.
2089 if(data->scheme_type != URL_SCHEME_UNKNOWN ||
2090 !(flags & Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES)) {
2091 TRACE("(%p %p %x): Treating URI as an hierarchical URI.\n", ptr, data, flags);
2092 data->is_opaque = FALSE;
2094 /* TODO: Handle hierarchical URI's, parse authority then parse the path. */
2095 if(!parse_authority(ptr, data, flags))
2098 return parse_path_hierarchical(ptr, data, flags);
2100 /* Reset ptr to it's starting position so opaque path parsing
2101 * begins at the correct location.
2106 /* If it reaches here, then the URI will be treated as an opaque
2110 TRACE("(%p %p %x): Treating URI as an opaque URI.\n", ptr, data, flags);
2112 data->is_opaque = TRUE;
2113 if(!parse_path_opaque(ptr, data, flags))
2119 /* Attempts to parse the query string from the URI.
2122 * If NO_DECODE_EXTRA_INFO flag is set, then invalid percent encoded
2123 * data is allowed appear in the query string. For unknown scheme types
2124 * invalid percent encoded data is allowed to appear reguardless.
2126 static BOOL parse_query(const WCHAR **ptr, parse_data *data, DWORD flags) {
2127 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
2130 TRACE("(%p %p %x): URI didn't contain a query string.\n", ptr, data, flags);
2137 while(**ptr && **ptr != '#') {
2138 if(**ptr == '%' && known_scheme &&
2139 !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
2140 if(!check_pct_encoded(ptr)) {
2151 data->query_len = *ptr - data->query;
2153 TRACE("(%p %p %x): Parsed query string %s len=%d\n", ptr, data, flags,
2154 debugstr_wn(data->query, data->query_len), data->query_len);
2158 /* Attempts to parse the fragment from the URI.
2161 * If NO_DECODE_EXTRA_INFO flag is set, then invalid percent encoded
2162 * data is allowed appear in the query string. For unknown scheme types
2163 * invalid percent encoded data is allowed to appear reguardless.
2165 static BOOL parse_fragment(const WCHAR **ptr, parse_data *data, DWORD flags) {
2166 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
2169 TRACE("(%p %p %x): URI didn't contain a fragment.\n", ptr, data, flags);
2173 data->fragment = *ptr;
2177 if(**ptr == '%' && known_scheme &&
2178 !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
2179 if(!check_pct_encoded(ptr)) {
2180 *ptr = data->fragment;
2181 data->fragment = NULL;
2190 data->fragment_len = *ptr - data->fragment;
2192 TRACE("(%p %p %x): Parsed fragment %s len=%d\n", ptr, data, flags,
2193 debugstr_wn(data->fragment, data->fragment_len), data->fragment_len);
2197 /* Parses and validates the components of the specified by data->uri
2198 * and stores the information it parses into 'data'.
2200 * Returns TRUE if it successfully parsed the URI. False otherwise.
2202 static BOOL parse_uri(parse_data *data, DWORD flags) {
2209 TRACE("(%p %x): BEGINNING TO PARSE URI %s.\n", data, flags, debugstr_w(data->uri));
2211 if(!parse_scheme(pptr, data, flags, 0))
2214 if(!parse_hierpart(pptr, data, flags))
2217 if(!parse_query(pptr, data, flags))
2220 if(!parse_fragment(pptr, data, flags))
2223 TRACE("(%p %x): FINISHED PARSING URI.\n", data, flags);
2227 static BOOL canonicalize_username(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2230 if(!data->username) {
2231 uri->userinfo_start = -1;
2235 uri->userinfo_start = uri->canon_len;
2236 for(ptr = data->username; ptr < data->username+data->username_len; ++ptr) {
2238 /* Only decode % encoded values for known scheme types. */
2239 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
2240 /* See if the value really needs decoded. */
2241 WCHAR val = decode_pct_val(ptr);
2242 if(is_unreserved(val)) {
2244 uri->canon_uri[uri->canon_len] = val;
2248 /* Move pass the hex characters. */
2253 } else if(!is_reserved(*ptr) && !is_unreserved(*ptr) && *ptr != '\\') {
2254 /* Only percent encode forbidden characters if the NO_ENCODE_FORBIDDEN_CHARACTERS flag
2257 if(!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS)) {
2259 pct_encode_val(*ptr, uri->canon_uri + uri->canon_len);
2261 uri->canon_len += 3;
2267 /* Nothing special, so just copy the character over. */
2268 uri->canon_uri[uri->canon_len] = *ptr;
2275 static BOOL canonicalize_password(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2278 if(!data->password) {
2279 uri->userinfo_split = -1;
2283 if(uri->userinfo_start == -1)
2284 /* Has a password, but, doesn't have a username. */
2285 uri->userinfo_start = uri->canon_len;
2287 uri->userinfo_split = uri->canon_len - uri->userinfo_start;
2289 /* Add the ':' to the userinfo component. */
2291 uri->canon_uri[uri->canon_len] = ':';
2294 for(ptr = data->password; ptr < data->password+data->password_len; ++ptr) {
2296 /* Only decode % encoded values for known scheme types. */
2297 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
2298 /* See if the value really needs decoded. */
2299 WCHAR val = decode_pct_val(ptr);
2300 if(is_unreserved(val)) {
2302 uri->canon_uri[uri->canon_len] = val;
2306 /* Move pass the hex characters. */
2311 } else if(!is_reserved(*ptr) && !is_unreserved(*ptr) && *ptr != '\\') {
2312 /* Only percent encode forbidden characters if the NO_ENCODE_FORBIDDEN_CHARACTERS flag
2315 if(!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS)) {
2317 pct_encode_val(*ptr, uri->canon_uri + uri->canon_len);
2319 uri->canon_len += 3;
2325 /* Nothing special, so just copy the character over. */
2326 uri->canon_uri[uri->canon_len] = *ptr;
2333 /* Canonicalizes the userinfo of the URI represented by the parse_data.
2335 * Canonicalization of the userinfo is a simple process. If there are any percent
2336 * encoded characters that fall in the "unreserved" character set, they are decoded
2337 * to their actual value. If a character is not in the "unreserved" or "reserved" sets
2338 * then it is percent encoded. Other than that the characters are copied over without
2341 static BOOL canonicalize_userinfo(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2342 uri->userinfo_start = uri->userinfo_split = -1;
2343 uri->userinfo_len = 0;
2345 if(!data->username && !data->password)
2346 /* URI doesn't have userinfo, so nothing to do here. */
2349 if(!canonicalize_username(data, uri, flags, computeOnly))
2352 if(!canonicalize_password(data, uri, flags, computeOnly))
2355 uri->userinfo_len = uri->canon_len - uri->userinfo_start;
2357 TRACE("(%p %p %x %d): Canonicalized userinfo, userinfo_start=%d, userinfo=%s, userinfo_split=%d userinfo_len=%d.\n",
2358 data, uri, flags, computeOnly, uri->userinfo_start, debugstr_wn(uri->canon_uri + uri->userinfo_start, uri->userinfo_len),
2359 uri->userinfo_split, uri->userinfo_len);
2361 /* Now insert the '@' after the userinfo. */
2363 uri->canon_uri[uri->canon_len] = '@';
2369 /* Attempts to canonicalize a reg_name.
2371 * Things that happen:
2372 * 1) If Uri_CREATE_NO_CANONICALIZE flag is not set, then the reg_name is
2373 * lower cased. Unless it's an unknown scheme type, which case it's
2374 * no lower cased reguardless.
2376 * 2) Unreserved % encoded characters are decoded for known
2379 * 3) Forbidden characters are % encoded as long as
2380 * Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS flag is not set and
2381 * it isn't an unknown scheme type.
2383 * 4) If it's a file scheme and the host is "localhost" it's removed.
2385 * 5) If it's a file scheme and Uri_CREATE_FILE_USE_DOS_PATH is set,
2386 * then the UNC path characters are added before the host name.
2388 static BOOL canonicalize_reg_name(const parse_data *data, Uri *uri,
2389 DWORD flags, BOOL computeOnly) {
2390 static const WCHAR localhostW[] =
2391 {'l','o','c','a','l','h','o','s','t',0};
2393 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
2395 if(data->scheme_type == URL_SCHEME_FILE &&
2396 data->host_len == lstrlenW(localhostW)) {
2397 if(!StrCmpNIW(data->host, localhostW, data->host_len)) {
2398 uri->host_start = -1;
2400 uri->host_type = Uri_HOST_UNKNOWN;
2405 if(data->scheme_type == URL_SCHEME_FILE && flags & Uri_CREATE_FILE_USE_DOS_PATH) {
2407 uri->canon_uri[uri->canon_len] = '\\';
2408 uri->canon_uri[uri->canon_len+1] = '\\';
2410 uri->canon_len += 2;
2411 uri->authority_start = uri->canon_len;
2414 uri->host_start = uri->canon_len;
2416 for(ptr = data->host; ptr < data->host+data->host_len; ++ptr) {
2417 if(*ptr == '%' && known_scheme) {
2418 WCHAR val = decode_pct_val(ptr);
2419 if(is_unreserved(val)) {
2420 /* If NO_CANONICALZE is not set, then windows lower cases the
2423 if(!(flags & Uri_CREATE_NO_CANONICALIZE) && isupperW(val)) {
2425 uri->canon_uri[uri->canon_len] = tolowerW(val);
2428 uri->canon_uri[uri->canon_len] = val;
2432 /* Skip past the % encoded character. */
2436 /* Just copy the % over. */
2438 uri->canon_uri[uri->canon_len] = *ptr;
2441 } else if(*ptr == '\\') {
2442 /* Only unknown scheme types could have made it here with a '\\' in the host name. */
2444 uri->canon_uri[uri->canon_len] = *ptr;
2446 } else if(!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS) &&
2447 !is_unreserved(*ptr) && !is_reserved(*ptr) && known_scheme) {
2449 pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
2451 /* The percent encoded value gets lower cased also. */
2452 if(!(flags & Uri_CREATE_NO_CANONICALIZE)) {
2453 uri->canon_uri[uri->canon_len+1] = tolowerW(uri->canon_uri[uri->canon_len+1]);
2454 uri->canon_uri[uri->canon_len+2] = tolowerW(uri->canon_uri[uri->canon_len+2]);
2458 uri->canon_len += 3;
2461 if(!(flags & Uri_CREATE_NO_CANONICALIZE) && known_scheme)
2462 uri->canon_uri[uri->canon_len] = tolowerW(*ptr);
2464 uri->canon_uri[uri->canon_len] = *ptr;
2471 uri->host_len = uri->canon_len - uri->host_start;
2474 TRACE("(%p %p %x %d): Canonicalize reg_name=%s len=%d\n", data, uri, flags,
2475 computeOnly, debugstr_wn(uri->canon_uri+uri->host_start, uri->host_len),
2479 find_domain_name(uri->canon_uri+uri->host_start, uri->host_len,
2480 &(uri->domain_offset));
2485 /* Attempts to canonicalize an implicit IPv4 address. */
2486 static BOOL canonicalize_implicit_ipv4address(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2487 uri->host_start = uri->canon_len;
2489 TRACE("%u\n", data->implicit_ipv4);
2490 /* For unknown scheme types Window's doesn't convert
2491 * the value into an IP address, but, it still considers
2492 * it an IPv4 address.
2494 if(data->scheme_type == URL_SCHEME_UNKNOWN) {
2496 memcpy(uri->canon_uri+uri->canon_len, data->host, data->host_len*sizeof(WCHAR));
2497 uri->canon_len += data->host_len;
2500 uri->canon_len += ui2ipv4(uri->canon_uri+uri->canon_len, data->implicit_ipv4);
2502 uri->canon_len += ui2ipv4(NULL, data->implicit_ipv4);
2505 uri->host_len = uri->canon_len - uri->host_start;
2506 uri->host_type = Uri_HOST_IPV4;
2509 TRACE("%p %p %x %d): Canonicalized implicit IP address=%s len=%d\n",
2510 data, uri, flags, computeOnly,
2511 debugstr_wn(uri->canon_uri+uri->host_start, uri->host_len),
2517 /* Attempts to canonicalize an IPv4 address.
2519 * If the parse_data represents a URI that has an implicit IPv4 address
2520 * (ex. http://256/, this function will convert 256 into 0.0.1.0). If
2521 * the implicit IP address exceeds the value of UINT_MAX (maximum value
2522 * for an IPv4 address) it's canonicalized as if were a reg-name.
2524 * If the parse_data contains a partial or full IPv4 address it normalizes it.
2525 * A partial IPv4 address is something like "192.0" and would be normalized to
2526 * "192.0.0.0". With a full (or partial) IPv4 address like "192.002.01.003" would
2527 * be normalized to "192.2.1.3".
2530 * Window's ONLY normalizes IPv4 address for known scheme types (one that isn't
2531 * URL_SCHEME_UNKNOWN). For unknown scheme types, it simply copies the data from
2532 * the original URI into the canonicalized URI, but, it still recognizes URI's
2533 * host type as HOST_IPV4.
2535 static BOOL canonicalize_ipv4address(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2536 if(data->has_implicit_ip)
2537 return canonicalize_implicit_ipv4address(data, uri, flags, computeOnly);
2539 uri->host_start = uri->canon_len;
2541 /* Windows only normalizes for known scheme types. */
2542 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
2543 /* parse_data contains a partial or full IPv4 address, so normalize it. */
2544 DWORD i, octetDigitCount = 0, octetCount = 0;
2545 BOOL octetHasDigit = FALSE;
2547 for(i = 0; i < data->host_len; ++i) {
2548 if(data->host[i] == '0' && !octetHasDigit) {
2549 /* Can ignore leading zeros if:
2550 * 1) It isn't the last digit of the octet.
2551 * 2) i+1 != data->host_len
2554 if(octetDigitCount == 2 ||
2555 i+1 == data->host_len ||
2556 data->host[i+1] == '.') {
2558 uri->canon_uri[uri->canon_len] = data->host[i];
2560 TRACE("Adding zero\n");
2562 } else if(data->host[i] == '.') {
2564 uri->canon_uri[uri->canon_len] = data->host[i];
2567 octetDigitCount = 0;
2568 octetHasDigit = FALSE;
2572 uri->canon_uri[uri->canon_len] = data->host[i];
2576 octetHasDigit = TRUE;
2580 /* Make sure the canonicalized IP address has 4 dec-octets.
2581 * If doesn't add "0" ones until there is 4;
2583 for( ; octetCount < 3; ++octetCount) {
2585 uri->canon_uri[uri->canon_len] = '.';
2586 uri->canon_uri[uri->canon_len+1] = '0';
2589 uri->canon_len += 2;
2592 /* Windows doesn't normalize addresses in unknown schemes. */
2594 memcpy(uri->canon_uri+uri->canon_len, data->host, data->host_len*sizeof(WCHAR));
2595 uri->canon_len += data->host_len;
2598 uri->host_len = uri->canon_len - uri->host_start;
2600 TRACE("(%p %p %x %d): Canonicalized IPv4 address, ip=%s len=%d\n",
2601 data, uri, flags, computeOnly,
2602 debugstr_wn(uri->canon_uri+uri->host_start, uri->host_len),
2609 /* Attempts to canonicalize the IPv6 address of the URI.
2611 * Multiple things happen during the canonicalization of an IPv6 address:
2612 * 1) Any leading zero's in an h16 component are removed.
2613 * Ex: [0001:0022::] -> [1:22::]
2615 * 2) The longest sequence of zero h16 components are compressed
2616 * into a "::" (elision). If there's a tie, the first is choosen.
2618 * Ex: [0:0:0:0:1:6:7:8] -> [::1:6:7:8]
2619 * [0:0:0:0:1:2::] -> [::1:2:0:0]
2620 * [0:0:1:2:0:0:7:8] -> [::1:2:0:0:7:8]
2622 * 3) If an IPv4 address is attached to the IPv6 address, it's
2624 * Ex: [::001.002.022.000] -> [::1.2.22.0]
2626 * 4) If an elision is present, but, only represents 1 h16 component
2629 * Ex: [1::2:3:4:5:6:7] -> [1:0:2:3:4:5:6:7]
2631 * 5) If the IPv6 address contains an IPv4 address and there exists
2632 * at least 1 non-zero h16 component the IPv4 address is converted
2633 * into two h16 components, otherwise it's normalized and kept as is.
2635 * Ex: [::192.200.003.4] -> [::192.200.3.4]
2636 * [ffff::192.200.003.4] -> [ffff::c0c8:3041]
2639 * For unknown scheme types Windows simply copies the address over without any
2642 * IPv4 address can be included in an elision if all its components are 0's.
2644 static BOOL canonicalize_ipv6address(const parse_data *data, Uri *uri,
2645 DWORD flags, BOOL computeOnly) {
2646 uri->host_start = uri->canon_len;
2648 if(data->scheme_type == URL_SCHEME_UNKNOWN) {
2650 memcpy(uri->canon_uri+uri->canon_len, data->host, data->host_len*sizeof(WCHAR));
2651 uri->canon_len += data->host_len;
2655 DWORD i, elision_len;
2657 if(!ipv6_to_number(&(data->ipv6_address), values)) {
2658 TRACE("(%p %p %x %d): Failed to compute numerical value for IPv6 address.\n",
2659 data, uri, flags, computeOnly);
2664 uri->canon_uri[uri->canon_len] = '[';
2667 /* Find where the elision should occur (if any). */
2668 compute_elision_location(&(data->ipv6_address), values, &elision_start, &elision_len);
2670 TRACE("%p %p %x %d): Elision starts at %d, len=%u\n", data, uri, flags,
2671 computeOnly, elision_start, elision_len);
2673 for(i = 0; i < 8; ++i) {
2674 BOOL in_elision = (elision_start > -1 && i >= elision_start &&
2675 i < elision_start+elision_len);
2676 BOOL do_ipv4 = (i == 6 && data->ipv6_address.ipv4 && !in_elision &&
2677 data->ipv6_address.h16_count == 0);
2679 if(i == elision_start) {
2681 uri->canon_uri[uri->canon_len] = ':';
2682 uri->canon_uri[uri->canon_len+1] = ':';
2684 uri->canon_len += 2;
2687 /* We can ignore the current component if we're in the elision. */
2691 /* We only add a ':' if we're not at i == 0, or when we're at
2692 * the very end of elision range since the ':' colon was handled
2693 * earlier. Otherwise we would end up with ":::" after elision.
2695 if(i != 0 && !(elision_start > -1 && i == elision_start+elision_len)) {
2697 uri->canon_uri[uri->canon_len] = ':';
2705 /* Combine the two parts of the IPv4 address values. */
2711 len = ui2ipv4(uri->canon_uri+uri->canon_len, val);
2713 len = ui2ipv4(NULL, val);
2715 uri->canon_len += len;
2718 /* Write a regular h16 component to the URI. */
2720 /* Short circuit for the trivial case. */
2721 if(values[i] == 0) {
2723 uri->canon_uri[uri->canon_len] = '0';
2726 static const WCHAR formatW[] = {'%','x',0};
2729 uri->canon_len += sprintfW(uri->canon_uri+uri->canon_len,
2730 formatW, values[i]);
2733 uri->canon_len += sprintfW(tmp, formatW, values[i]);
2739 /* Add the closing ']'. */
2741 uri->canon_uri[uri->canon_len] = ']';
2745 uri->host_len = uri->canon_len - uri->host_start;
2748 TRACE("(%p %p %x %d): Canonicalized IPv6 address %s, len=%d\n", data, uri, flags,
2749 computeOnly, debugstr_wn(uri->canon_uri+uri->host_start, uri->host_len),
2755 /* Attempts to canonicalize the host of the URI (if any). */
2756 static BOOL canonicalize_host(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2757 uri->host_start = -1;
2759 uri->domain_offset = -1;
2762 switch(data->host_type) {
2764 uri->host_type = Uri_HOST_DNS;
2765 if(!canonicalize_reg_name(data, uri, flags, computeOnly))
2770 uri->host_type = Uri_HOST_IPV4;
2771 if(!canonicalize_ipv4address(data, uri, flags, computeOnly))
2776 if(!canonicalize_ipv6address(data, uri, flags, computeOnly))
2779 uri->host_type = Uri_HOST_IPV6;
2781 case Uri_HOST_UNKNOWN:
2782 if(data->host_len > 0 || data->scheme_type != URL_SCHEME_FILE) {
2783 uri->host_start = uri->canon_len;
2785 /* Nothing happens to unknown host types. */
2787 memcpy(uri->canon_uri+uri->canon_len, data->host, data->host_len*sizeof(WCHAR));
2788 uri->canon_len += data->host_len;
2789 uri->host_len = data->host_len;
2792 uri->host_type = Uri_HOST_UNKNOWN;
2795 FIXME("(%p %p %x %d): Canonicalization for host type %d not supported.\n", data,
2796 uri, flags, computeOnly, data->host_type);
2804 static BOOL canonicalize_port(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2805 BOOL has_default_port = FALSE;
2806 USHORT default_port = 0;
2809 uri->port_offset = -1;
2811 /* Check if the scheme has a default port. */
2812 for(i = 0; i < sizeof(default_ports)/sizeof(default_ports[0]); ++i) {
2813 if(default_ports[i].scheme == data->scheme_type) {
2814 has_default_port = TRUE;
2815 default_port = default_ports[i].port;
2820 uri->has_port = data->has_port || has_default_port;
2823 * 1) Has a port which is the default port.
2824 * 2) Has a port (not the default).
2825 * 3) Doesn't have a port, but, scheme has a default port.
2828 if(has_default_port && data->has_port && data->port_value == default_port) {
2829 /* If it's the default port and this flag isn't set, don't do anything. */
2830 if(flags & Uri_CREATE_NO_CANONICALIZE) {
2831 uri->port_offset = uri->canon_len-uri->authority_start;
2833 uri->canon_uri[uri->canon_len] = ':';
2837 /* Copy the original port over. */
2839 memcpy(uri->canon_uri+uri->canon_len, data->port, data->port_len*sizeof(WCHAR));
2840 uri->canon_len += data->port_len;
2843 uri->canon_len += ui2str(uri->canon_uri+uri->canon_len, data->port_value);
2845 uri->canon_len += ui2str(NULL, data->port_value);
2849 uri->port = default_port;
2850 } else if(data->has_port) {
2851 uri->port_offset = uri->canon_len-uri->authority_start;
2853 uri->canon_uri[uri->canon_len] = ':';
2856 if(flags & Uri_CREATE_NO_CANONICALIZE && data->port) {
2857 /* Copy the original over without changes. */
2859 memcpy(uri->canon_uri+uri->canon_len, data->port, data->port_len*sizeof(WCHAR));
2860 uri->canon_len += data->port_len;
2863 uri->canon_len += ui2str(uri->canon_uri+uri->canon_len, data->port_value);
2865 uri->canon_len += ui2str(NULL, data->port_value);
2868 uri->port = data->port_value;
2869 } else if(has_default_port)
2870 uri->port = default_port;
2875 /* Canonicalizes the authority of the URI represented by the parse_data. */
2876 static BOOL canonicalize_authority(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2877 uri->authority_start = uri->canon_len;
2878 uri->authority_len = 0;
2880 if(!canonicalize_userinfo(data, uri, flags, computeOnly))
2883 if(!canonicalize_host(data, uri, flags, computeOnly))
2886 if(!canonicalize_port(data, uri, flags, computeOnly))
2889 if(uri->host_start != -1 || (data->is_relative && (data->password || data->username)))
2890 uri->authority_len = uri->canon_len - uri->authority_start;
2892 uri->authority_start = -1;
2897 /* Attempts to canonicalize the path of a hierarchical URI.
2899 * Things that happen:
2900 * 1). Forbidden characters are percent encoded, unless the NO_ENCODE_FORBIDDEN
2901 * flag is set or it's a file URI. Forbidden characters are always encoded
2902 * for file schemes reguardless and forbidden characters are never encoded
2903 * for unknown scheme types.
2905 * 2). For known scheme types '\\' are changed to '/'.
2907 * 3). Percent encoded, unreserved characters are decoded to their actual values.
2908 * Unless the scheme type is unknown. For file schemes any percent encoded
2909 * character in the unreserved or reserved set is decoded.
2911 * 4). For File schemes if the path is starts with a drive letter and doesn't
2912 * start with a '/' then one is appended.
2913 * Ex: file://c:/test.mp3 -> file:///c:/test.mp3
2915 * 5). Dot segments are removed from the path for all scheme types
2916 * unless NO_CANONICALIZE flag is set. Dot segments aren't removed
2917 * for wildcard scheme types.
2920 * file://c:/test%20test -> file:///c:/test%2520test
2921 * file://c:/test%3Etest -> file:///c:/test%253Etest
2922 * if Uri_CREATE_FILE_USE_DOS_PATH is not set:
2923 * file:///c:/test%20test -> file:///c:/test%20test
2924 * file:///c:/test%test -> file:///c:/test%25test
2926 static BOOL canonicalize_path_hierarchical(const parse_data *data, Uri *uri,
2927 DWORD flags, BOOL computeOnly) {
2929 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
2930 const BOOL is_file = data->scheme_type == URL_SCHEME_FILE;
2931 const BOOL is_res = data->scheme_type == URL_SCHEME_RES;
2933 BOOL escape_pct = FALSE;
2936 uri->path_start = -1;
2941 uri->path_start = uri->canon_len;
2944 if(is_file && uri->host_start == -1) {
2945 /* Check if a '/' needs to be appended for the file scheme. */
2946 if(data->path_len > 1 && is_drive_path(ptr) && !(flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
2948 uri->canon_uri[uri->canon_len] = '/';
2951 } else if(*ptr == '/') {
2952 if(!(flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
2953 /* Copy the extra '/' over. */
2955 uri->canon_uri[uri->canon_len] = '/';
2961 if(is_drive_path(ptr)) {
2963 uri->canon_uri[uri->canon_len] = *ptr;
2964 /* If theres a '|' after the drive letter, convert it to a ':'. */
2965 uri->canon_uri[uri->canon_len+1] = ':';
2968 uri->canon_len += 2;
2972 if(!is_file && *(data->path) && *(data->path) != '/') {
2973 /* Prepend a '/' to the path if it doesn't have one. */
2975 uri->canon_uri[uri->canon_len] = '/';
2979 for(; ptr < data->path+data->path_len; ++ptr) {
2980 BOOL do_default_action = TRUE;
2982 if(*ptr == '%' && !is_res) {
2983 const WCHAR *tmp = ptr;
2986 /* Check if the % represents a valid encoded char, or if it needs encoded. */
2987 BOOL force_encode = !check_pct_encoded(&tmp) && is_file && !(flags&Uri_CREATE_FILE_USE_DOS_PATH);
2988 val = decode_pct_val(ptr);
2990 if(force_encode || escape_pct) {
2991 /* Escape the percent sign in the file URI. */
2993 pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
2994 uri->canon_len += 3;
2995 do_default_action = FALSE;
2996 } else if((is_unreserved(val) && known_scheme) ||
2997 (is_file && (is_unreserved(val) || is_reserved(val) ||
2998 (val && flags&Uri_CREATE_FILE_USE_DOS_PATH && !is_forbidden_dos_path_char(val))))) {
3000 uri->canon_uri[uri->canon_len] = val;
3006 } else if(*ptr == '/' && is_file && (flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
3007 /* Convert the '/' back to a '\\'. */
3009 uri->canon_uri[uri->canon_len] = '\\';
3011 do_default_action = FALSE;
3012 } else if(*ptr == '\\' && known_scheme) {
3013 if(!(is_file && (flags & Uri_CREATE_FILE_USE_DOS_PATH))) {
3014 /* Convert '\\' into a '/'. */
3016 uri->canon_uri[uri->canon_len] = '/';
3018 do_default_action = FALSE;
3020 } else if(known_scheme && !is_res && !is_unreserved(*ptr) && !is_reserved(*ptr) &&
3021 (!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS) || is_file)) {
3022 if(!(is_file && (flags & Uri_CREATE_FILE_USE_DOS_PATH))) {
3023 /* Escape the forbidden character. */
3025 pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
3026 uri->canon_len += 3;
3027 do_default_action = FALSE;
3031 if(do_default_action) {
3033 uri->canon_uri[uri->canon_len] = *ptr;
3038 uri->path_len = uri->canon_len - uri->path_start;
3040 /* Removing the dot segments only happens when it's not in
3041 * computeOnly mode and it's not a wildcard scheme. File schemes
3042 * with USE_DOS_PATH set don't get dot segments removed.
3044 if(!(is_file && (flags & Uri_CREATE_FILE_USE_DOS_PATH)) &&
3045 data->scheme_type != URL_SCHEME_WILDCARD) {
3046 if(!(flags & Uri_CREATE_NO_CANONICALIZE) && !computeOnly) {
3047 /* Remove the dot segments (if any) and reset everything to the new
3050 DWORD new_len = remove_dot_segments(uri->canon_uri+uri->path_start, uri->path_len);
3051 uri->canon_len -= uri->path_len-new_len;
3052 uri->path_len = new_len;
3057 TRACE("Canonicalized path %s len=%d\n",
3058 debugstr_wn(uri->canon_uri+uri->path_start, uri->path_len),
3064 /* Attempts to canonicalize the path for an opaque URI.
3066 * For known scheme types:
3067 * 1) forbidden characters are percent encoded if
3068 * NO_ENCODE_FORBIDDEN_CHARACTERS isn't set.
3070 * 2) Percent encoded, unreserved characters are decoded
3071 * to their actual values, for known scheme types.
3073 * 3) '\\' are changed to '/' for known scheme types
3074 * except for mailto schemes.
3076 * 4) For file schemes, if USE_DOS_PATH is set all '/'
3077 * are converted to backslashes.
3079 * 5) For file schemes, if USE_DOS_PATH isn't set all '\'
3080 * are converted to forward slashes.
3082 static BOOL canonicalize_path_opaque(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
3084 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
3085 const BOOL is_file = data->scheme_type == URL_SCHEME_FILE;
3088 uri->path_start = -1;
3093 uri->path_start = uri->canon_len;
3095 /* Windows doesn't allow a "//" to appear after the scheme
3096 * of a URI, if it's an opaque URI.
3098 if(data->scheme && *(data->path) == '/' && *(data->path+1) == '/') {
3099 /* So it inserts a "/." before the "//" if it exists. */
3101 uri->canon_uri[uri->canon_len] = '/';
3102 uri->canon_uri[uri->canon_len+1] = '.';
3105 uri->canon_len += 2;
3108 for(ptr = data->path; ptr < data->path+data->path_len; ++ptr) {
3109 BOOL do_default_action = TRUE;
3111 if(*ptr == '%' && known_scheme) {
3112 WCHAR val = decode_pct_val(ptr);
3114 if(is_unreserved(val)) {
3116 uri->canon_uri[uri->canon_len] = val;
3122 } else if(*ptr == '/' && is_file && (flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
3124 uri->canon_uri[uri->canon_len] = '\\';
3126 do_default_action = FALSE;
3127 } else if(*ptr == '\\') {
3128 if(is_file && !(flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
3129 /* Convert to a '/'. */
3131 uri->canon_uri[uri->canon_len] = '/';
3133 do_default_action = FALSE;
3135 } else if(known_scheme && !is_unreserved(*ptr) && !is_reserved(*ptr) &&
3136 !(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS)) {
3137 if(!(is_file && (flags & Uri_CREATE_FILE_USE_DOS_PATH))) {
3139 pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
3140 uri->canon_len += 3;
3141 do_default_action = FALSE;
3145 if(do_default_action) {
3147 uri->canon_uri[uri->canon_len] = *ptr;
3152 uri->path_len = uri->canon_len - uri->path_start;
3154 TRACE("(%p %p %x %d): Canonicalized opaque URI path %s len=%d\n", data, uri, flags, computeOnly,
3155 debugstr_wn(uri->canon_uri+uri->path_start, uri->path_len), uri->path_len);
3159 /* Determines how the URI represented by the parse_data should be canonicalized.
3161 * Essentially, if the parse_data represents an hierarchical URI then it calls
3162 * canonicalize_authority and the canonicalization functions for the path. If the
3163 * URI is opaque it canonicalizes the path of the URI.
3165 static BOOL canonicalize_hierpart(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
3166 if(!data->is_opaque || (data->is_relative && (data->password || data->username))) {
3167 /* "//" is only added for non-wildcard scheme types.
3169 * A "//" is only added to a relative URI if it has a
3170 * host or port component (this only happens if a IUriBuilder
3171 * is generating an IUri).
3173 if((data->is_relative && (data->host || data->has_port)) ||
3174 (!data->is_relative && data->scheme_type != URL_SCHEME_WILDCARD)) {
3176 INT pos = uri->canon_len;
3178 uri->canon_uri[pos] = '/';
3179 uri->canon_uri[pos+1] = '/';
3181 uri->canon_len += 2;
3184 if(!canonicalize_authority(data, uri, flags, computeOnly))
3187 if(data->is_relative && (data->password || data->username)) {
3188 if(!canonicalize_path_opaque(data, uri, flags, computeOnly))
3191 if(!canonicalize_path_hierarchical(data, uri, flags, computeOnly))
3195 /* Opaque URI's don't have an authority. */
3196 uri->userinfo_start = uri->userinfo_split = -1;
3197 uri->userinfo_len = 0;
3198 uri->host_start = -1;
3200 uri->host_type = Uri_HOST_UNKNOWN;
3201 uri->has_port = FALSE;
3202 uri->authority_start = -1;
3203 uri->authority_len = 0;
3204 uri->domain_offset = -1;
3205 uri->port_offset = -1;
3207 if(is_hierarchical_scheme(data->scheme_type)) {
3210 /* Absolute URIs aren't displayed for known scheme types
3211 * which should be hierarchical URIs.
3213 uri->display_modifiers |= URI_DISPLAY_NO_ABSOLUTE_URI;
3215 /* Windows also sets the port for these (if they have one). */
3216 for(i = 0; i < sizeof(default_ports)/sizeof(default_ports[0]); ++i) {
3217 if(data->scheme_type == default_ports[i].scheme) {
3218 uri->has_port = TRUE;
3219 uri->port = default_ports[i].port;
3225 if(!canonicalize_path_opaque(data, uri, flags, computeOnly))
3229 if(uri->path_start > -1 && !computeOnly)
3230 /* Finding file extensions happens for both types of URIs. */
3231 uri->extension_offset = find_file_extension(uri->canon_uri+uri->path_start, uri->path_len);
3233 uri->extension_offset = -1;
3238 /* Attempts to canonicalize the query string of the URI.
3240 * Things that happen:
3241 * 1) For known scheme types forbidden characters
3242 * are percent encoded, unless the NO_DECODE_EXTRA_INFO flag is set
3243 * or NO_ENCODE_FORBIDDEN_CHARACTERS is set.
3245 * 2) For known scheme types, percent encoded, unreserved characters
3246 * are decoded as long as the NO_DECODE_EXTRA_INFO flag isn't set.
3248 static BOOL canonicalize_query(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
3249 const WCHAR *ptr, *end;
3250 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
3253 uri->query_start = -1;
3258 uri->query_start = uri->canon_len;
3260 end = data->query+data->query_len;
3261 for(ptr = data->query; ptr < end; ++ptr) {
3263 if(known_scheme && !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
3264 WCHAR val = decode_pct_val(ptr);
3265 if(is_unreserved(val)) {
3267 uri->canon_uri[uri->canon_len] = val;
3274 } else if(known_scheme && !is_unreserved(*ptr) && !is_reserved(*ptr)) {
3275 if(!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS) &&
3276 !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
3278 pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
3279 uri->canon_len += 3;
3285 uri->canon_uri[uri->canon_len] = *ptr;
3289 uri->query_len = uri->canon_len - uri->query_start;
3292 TRACE("(%p %p %x %d): Canonicalized query string %s len=%d\n", data, uri, flags,
3293 computeOnly, debugstr_wn(uri->canon_uri+uri->query_start, uri->query_len),
3298 static BOOL canonicalize_fragment(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
3299 const WCHAR *ptr, *end;
3300 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
3302 if(!data->fragment) {
3303 uri->fragment_start = -1;
3304 uri->fragment_len = 0;
3308 uri->fragment_start = uri->canon_len;
3310 end = data->fragment + data->fragment_len;
3311 for(ptr = data->fragment; ptr < end; ++ptr) {
3313 if(known_scheme && !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
3314 WCHAR val = decode_pct_val(ptr);
3315 if(is_unreserved(val)) {
3317 uri->canon_uri[uri->canon_len] = val;
3324 } else if(known_scheme && !is_unreserved(*ptr) && !is_reserved(*ptr)) {
3325 if(!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS) &&
3326 !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
3328 pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
3329 uri->canon_len += 3;
3335 uri->canon_uri[uri->canon_len] = *ptr;
3339 uri->fragment_len = uri->canon_len - uri->fragment_start;
3342 TRACE("(%p %p %x %d): Canonicalized fragment %s len=%d\n", data, uri, flags,
3343 computeOnly, debugstr_wn(uri->canon_uri+uri->fragment_start, uri->fragment_len),
3348 /* Canonicalizes the scheme information specified in the parse_data using the specified flags. */
3349 static BOOL canonicalize_scheme(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
3350 uri->scheme_start = -1;
3351 uri->scheme_len = 0;
3354 /* The only type of URI that doesn't have to have a scheme is a relative
3357 if(!data->is_relative) {
3358 FIXME("(%p %p %x): Unable to determine the scheme type of %s.\n", data,
3359 uri, flags, debugstr_w(data->uri));
3365 INT pos = uri->canon_len;
3367 for(i = 0; i < data->scheme_len; ++i) {
3368 /* Scheme name must be lower case after canonicalization. */
3369 uri->canon_uri[i + pos] = tolowerW(data->scheme[i]);
3372 uri->canon_uri[i + pos] = ':';
3373 uri->scheme_start = pos;
3375 TRACE("(%p %p %x): Canonicalized scheme=%s, len=%d.\n", data, uri, flags,
3376 debugstr_wn(uri->canon_uri, uri->scheme_len), data->scheme_len);
3379 /* This happens in both computation modes. */
3380 uri->canon_len += data->scheme_len + 1;
3381 uri->scheme_len = data->scheme_len;
3386 /* Compute's what the length of the URI specified by the parse_data will be
3387 * after canonicalization occurs using the specified flags.
3389 * This function will return a non-zero value indicating the length of the canonicalized
3390 * URI, or -1 on error.
3392 static int compute_canonicalized_length(const parse_data *data, DWORD flags) {
3395 memset(&uri, 0, sizeof(Uri));
3397 TRACE("(%p %x): Beginning to compute canonicalized length for URI %s\n", data, flags,
3398 debugstr_w(data->uri));
3400 if(!canonicalize_scheme(data, &uri, flags, TRUE)) {
3401 ERR("(%p %x): Failed to compute URI scheme length.\n", data, flags);
3405 if(!canonicalize_hierpart(data, &uri, flags, TRUE)) {
3406 ERR("(%p %x): Failed to compute URI hierpart length.\n", data, flags);
3410 if(!canonicalize_query(data, &uri, flags, TRUE)) {
3411 ERR("(%p %x): Failed to compute query string length.\n", data, flags);
3415 if(!canonicalize_fragment(data, &uri, flags, TRUE)) {
3416 ERR("(%p %x): Failed to compute fragment length.\n", data, flags);
3420 TRACE("(%p %x): Finished computing canonicalized URI length. length=%d\n", data, flags, uri.canon_len);
3422 return uri.canon_len;
3425 /* Canonicalizes the URI data specified in the parse_data, using the given flags. If the
3426 * canonicalization succeededs it will store all the canonicalization information
3427 * in the pointer to the Uri.
3429 * To canonicalize a URI this function first computes what the length of the URI
3430 * specified by the parse_data will be. Once this is done it will then perfom the actual
3431 * canonicalization of the URI.
3433 static HRESULT canonicalize_uri(const parse_data *data, Uri *uri, DWORD flags) {
3436 uri->canon_uri = NULL;
3437 len = uri->canon_size = uri->canon_len = 0;
3439 TRACE("(%p %p %x): beginning to canonicalize URI %s.\n", data, uri, flags, debugstr_w(data->uri));
3441 /* First try to compute the length of the URI. */
3442 len = compute_canonicalized_length(data, flags);
3444 ERR("(%p %p %x): Could not compute the canonicalized length of %s.\n", data, uri, flags,
3445 debugstr_w(data->uri));
3446 return E_INVALIDARG;
3449 uri->canon_uri = heap_alloc((len+1)*sizeof(WCHAR));
3451 return E_OUTOFMEMORY;
3453 uri->canon_size = len;
3454 if(!canonicalize_scheme(data, uri, flags, FALSE)) {
3455 ERR("(%p %p %x): Unable to canonicalize the scheme of the URI.\n", data, uri, flags);
3456 return E_INVALIDARG;
3458 uri->scheme_type = data->scheme_type;
3460 if(!canonicalize_hierpart(data, uri, flags, FALSE)) {
3461 ERR("(%p %p %x): Unable to canonicalize the heirpart of the URI\n", data, uri, flags);
3462 return E_INVALIDARG;
3465 if(!canonicalize_query(data, uri, flags, FALSE)) {
3466 ERR("(%p %p %x): Unable to canonicalize query string of the URI.\n",
3468 return E_INVALIDARG;
3471 if(!canonicalize_fragment(data, uri, flags, FALSE)) {
3472 ERR("(%p %p %x): Unable to canonicalize fragment of the URI.\n",
3474 return E_INVALIDARG;
3477 /* There's a possibility we didn't use all the space we allocated
3480 if(uri->canon_len < uri->canon_size) {
3481 /* This happens if the URI is hierarchical and dot
3482 * segments were removed from it's path.
3484 WCHAR *tmp = heap_realloc(uri->canon_uri, (uri->canon_len+1)*sizeof(WCHAR));
3486 return E_OUTOFMEMORY;
3488 uri->canon_uri = tmp;
3489 uri->canon_size = uri->canon_len;
3492 uri->canon_uri[uri->canon_len] = '\0';
3493 TRACE("(%p %p %x): finished canonicalizing the URI. uri=%s\n", data, uri, flags, debugstr_w(uri->canon_uri));
3498 static HRESULT get_builder_component(LPWSTR *component, DWORD *component_len,
3499 LPCWSTR source, DWORD source_len,
3500 LPCWSTR *output, DWORD *output_len)
3513 if(!(*component) && source) {
3514 /* Allocate 'component', and copy the contents from 'source'
3515 * into the new allocation.
3517 *component = heap_alloc((source_len+1)*sizeof(WCHAR));
3519 return E_OUTOFMEMORY;
3521 memcpy(*component, source, source_len*sizeof(WCHAR));
3522 (*component)[source_len] = '\0';
3523 *component_len = source_len;
3526 *output = *component;
3527 *output_len = *component_len;
3528 return *output ? S_OK : S_FALSE;
3531 /* Allocates 'component' and copies the string from 'new_value' into 'component'.
3532 * If 'prefix' is set and 'new_value' isn't NULL, then it checks if 'new_value'
3533 * starts with 'prefix'. If it doesn't then 'prefix' is prepended to 'component'.
3535 * If everything is successful, then will set 'success_flag' in 'flags'.
3537 static HRESULT set_builder_component(LPWSTR *component, DWORD *component_len, LPCWSTR new_value,
3538 WCHAR prefix, DWORD *flags, DWORD success_flag)
3540 heap_free(*component);
3546 BOOL add_prefix = FALSE;
3547 DWORD len = lstrlenW(new_value);
3550 if(prefix && *new_value != prefix) {
3552 *component = heap_alloc((len+2)*sizeof(WCHAR));
3554 *component = heap_alloc((len+1)*sizeof(WCHAR));
3557 return E_OUTOFMEMORY;
3560 (*component)[pos++] = prefix;
3562 memcpy(*component+pos, new_value, (len+1)*sizeof(WCHAR));
3563 *component_len = len+pos;
3566 *flags |= success_flag;
3570 #define URI(x) ((IUri*) &(x)->lpIUriVtbl)
3571 #define URIBUILDER(x) ((IUriBuilder*) &(x)->lpIUriBuilderVtbl)
3573 static void reset_builder(UriBuilder *builder) {
3575 IUri_Release(URI(builder->uri));
3576 builder->uri = NULL;
3578 heap_free(builder->fragment);
3579 builder->fragment = NULL;
3580 builder->fragment_len = 0;
3582 heap_free(builder->host);
3583 builder->host = NULL;
3584 builder->host_len = 0;
3586 heap_free(builder->password);
3587 builder->password = NULL;
3588 builder->password_len = 0;
3590 heap_free(builder->path);
3591 builder->path = NULL;
3592 builder->path_len = 0;
3594 heap_free(builder->query);
3595 builder->query = NULL;
3596 builder->query_len = 0;
3598 heap_free(builder->scheme);
3599 builder->scheme = NULL;
3600 builder->scheme_len = 0;
3602 heap_free(builder->username);
3603 builder->username = NULL;
3604 builder->username_len = 0;
3606 builder->has_port = FALSE;
3608 builder->modified_props = 0;
3611 static HRESULT validate_scheme_name(const UriBuilder *builder, parse_data *data, DWORD flags) {
3612 const WCHAR *component;
3617 if(builder->scheme) {
3618 ptr = builder->scheme;
3619 expected_len = builder->scheme_len;
3620 } else if(builder->uri && builder->uri->scheme_start > -1) {
3621 ptr = builder->uri->canon_uri+builder->uri->scheme_start;
3622 expected_len = builder->uri->scheme_len;
3624 static const WCHAR nullW[] = {0};
3631 if(parse_scheme(pptr, data, flags, ALLOW_NULL_TERM_SCHEME) &&
3632 data->scheme_len == expected_len) {
3634 TRACE("(%p %p %x): Found valid scheme component %s len=%d.\n", builder, data, flags,
3635 debugstr_wn(data->scheme, data->scheme_len), data->scheme_len);
3637 TRACE("(%p %p %x): Invalid scheme component found %s.\n", builder, data, flags,
3638 debugstr_wn(component, expected_len));
3639 return INET_E_INVALID_URL;
3645 static HRESULT validate_username(const UriBuilder *builder, parse_data *data, DWORD flags) {
3650 if(builder->username) {
3651 ptr = builder->username;
3652 expected_len = builder->username_len;
3653 } else if(!(builder->modified_props & Uri_HAS_USER_NAME) && builder->uri &&
3654 builder->uri->userinfo_start > -1 && builder->uri->userinfo_split != 0) {
3655 /* Just use the username from the base Uri. */
3656 data->username = builder->uri->canon_uri+builder->uri->userinfo_start;
3657 data->username_len = (builder->uri->userinfo_split > -1) ?
3658 builder->uri->userinfo_split : builder->uri->userinfo_len;
3666 const WCHAR *component = ptr;
3668 if(parse_username(pptr, data, flags, ALLOW_NULL_TERM_USER_NAME) &&
3669 data->username_len == expected_len)
3670 TRACE("(%p %p %x): Found valid username component %s len=%d.\n", builder, data, flags,
3671 debugstr_wn(data->username, data->username_len), data->username_len);
3673 TRACE("(%p %p %x): Invalid username component found %s.\n", builder, data, flags,
3674 debugstr_wn(component, expected_len));
3675 return INET_E_INVALID_URL;
3682 static HRESULT validate_password(const UriBuilder *builder, parse_data *data, DWORD flags) {
3687 if(builder->password) {
3688 ptr = builder->password;
3689 expected_len = builder->password_len;
3690 } else if(!(builder->modified_props & Uri_HAS_PASSWORD) && builder->uri &&
3691 builder->uri->userinfo_split > -1) {
3692 data->password = builder->uri->canon_uri+builder->uri->userinfo_start+builder->uri->userinfo_split+1;
3693 data->password_len = builder->uri->userinfo_len-builder->uri->userinfo_split-1;
3701 const WCHAR *component = ptr;
3703 if(parse_password(pptr, data, flags, ALLOW_NULL_TERM_PASSWORD) &&
3704 data->password_len == expected_len)
3705 TRACE("(%p %p %x): Found valid password component %s len=%d.\n", builder, data, flags,
3706 debugstr_wn(data->password, data->password_len), data->password_len);
3708 TRACE("(%p %p %x): Invalid password component found %s.\n", builder, data, flags,
3709 debugstr_wn(component, expected_len));
3710 return INET_E_INVALID_URL;
3717 static HRESULT validate_userinfo(const UriBuilder *builder, parse_data *data, DWORD flags) {
3720 hr = validate_username(builder, data, flags);
3724 hr = validate_password(builder, data, flags);
3731 static HRESULT validate_host(const UriBuilder *builder, parse_data *data, DWORD flags) {
3737 ptr = builder->host;
3738 expected_len = builder->host_len;
3739 } else if(!(builder->modified_props & Uri_HAS_HOST) && builder->uri && builder->uri->host_start > -1) {
3740 ptr = builder->uri->canon_uri + builder->uri->host_start;
3741 expected_len = builder->uri->host_len;
3746 const WCHAR *component = ptr;
3747 DWORD extras = ALLOW_BRACKETLESS_IP_LITERAL|IGNORE_PORT_DELIMITER|SKIP_IP_FUTURE_CHECK;
3750 if(parse_host(pptr, data, flags, extras) && data->host_len == expected_len)
3751 TRACE("(%p %p %x): Found valid host name %s len=%d type=%d.\n", builder, data, flags,
3752 debugstr_wn(data->host, data->host_len), data->host_len, data->host_type);
3754 TRACE("(%p %p %x): Invalid host name found %s.\n", builder, data, flags,
3755 debugstr_wn(component, expected_len));
3756 return INET_E_INVALID_URL;
3763 static void setup_port(const UriBuilder *builder, parse_data *data, DWORD flags) {
3764 if(builder->modified_props & Uri_HAS_PORT) {
3765 if(builder->has_port) {
3766 data->has_port = TRUE;
3767 data->port_value = builder->port;
3769 } else if(builder->uri && builder->uri->has_port) {
3770 data->has_port = TRUE;
3771 data->port_value = builder->uri->port;
3775 TRACE("(%p %p %x): Using %u as port for IUri.\n", builder, data, flags, data->port_value);
3778 static HRESULT validate_path(const UriBuilder *builder, parse_data *data, DWORD flags) {
3779 const WCHAR *ptr = NULL;
3780 const WCHAR *component;
3783 BOOL check_len = TRUE;
3787 ptr = builder->path;
3788 expected_len = builder->path_len;
3789 } else if(!(builder->modified_props & Uri_HAS_PATH) &&
3790 builder->uri && builder->uri->path_start > -1) {
3791 ptr = builder->uri->canon_uri+builder->uri->path_start;
3792 expected_len = builder->uri->path_len;
3794 static const WCHAR nullW[] = {0};
3802 /* How the path is validated depends on what type of
3805 valid = data->is_opaque ?
3806 parse_path_opaque(pptr, data, flags) : parse_path_hierarchical(pptr, data, flags);
3808 if(!valid || (check_len && expected_len != data->path_len)) {
3809 TRACE("(%p %p %x): Invalid path component %s.\n", builder, data, flags,
3810 debugstr_wn(component, check_len ? expected_len : -1) );
3811 return INET_E_INVALID_URL;
3814 TRACE("(%p %p %x): Valid path component %s len=%d.\n", builder, data, flags,
3815 debugstr_wn(data->path, data->path_len), data->path_len);
3820 static HRESULT validate_query(const UriBuilder *builder, parse_data *data, DWORD flags) {
3821 const WCHAR *ptr = NULL;
3825 if(builder->query) {
3826 ptr = builder->query;
3827 expected_len = builder->query_len;
3828 } else if(!(builder->modified_props & Uri_HAS_QUERY) && builder->uri &&
3829 builder->uri->query_start > -1) {
3830 ptr = builder->uri->canon_uri+builder->uri->query_start;
3831 expected_len = builder->uri->query_len;
3835 const WCHAR *component = ptr;
3838 if(parse_query(pptr, data, flags) && expected_len == data->query_len)
3839 TRACE("(%p %p %x): Valid query component %s len=%d.\n", builder, data, flags,
3840 debugstr_wn(data->query, data->query_len), data->query_len);
3842 TRACE("(%p %p %x): Invalid query component %s.\n", builder, data, flags,
3843 debugstr_wn(component, expected_len));
3844 return INET_E_INVALID_URL;
3851 static HRESULT validate_fragment(const UriBuilder *builder, parse_data *data, DWORD flags) {
3852 const WCHAR *ptr = NULL;
3856 if(builder->fragment) {
3857 ptr = builder->fragment;
3858 expected_len = builder->fragment_len;
3859 } else if(!(builder->modified_props & Uri_HAS_FRAGMENT) && builder->uri &&
3860 builder->uri->fragment_start > -1) {
3861 ptr = builder->uri->canon_uri+builder->uri->fragment_start;
3862 expected_len = builder->uri->fragment_len;
3866 const WCHAR *component = ptr;
3869 if(parse_fragment(pptr, data, flags) && expected_len == data->fragment_len)
3870 TRACE("(%p %p %x): Valid fragment component %s len=%d.\n", builder, data, flags,
3871 debugstr_wn(data->fragment, data->fragment_len), data->fragment_len);
3873 TRACE("(%p %p %x): Invalid fragment component %s.\n", builder, data, flags,
3874 debugstr_wn(component, expected_len));
3875 return INET_E_INVALID_URL;
3882 static HRESULT validate_components(const UriBuilder *builder, parse_data *data, DWORD flags) {
3885 memset(data, 0, sizeof(parse_data));
3887 TRACE("(%p %p %x): Beginning to validate builder components.\n", builder, data, flags);
3889 hr = validate_scheme_name(builder, data, flags);
3893 /* Extra validation for file schemes. */
3894 if(data->scheme_type == URL_SCHEME_FILE) {
3895 if((builder->password || (builder->uri && builder->uri->userinfo_split > -1)) ||
3896 (builder->username || (builder->uri && builder->uri->userinfo_start > -1))) {
3897 TRACE("(%p %p %x): File schemes can't contain a username or password.\n",
3898 builder, data, flags);
3899 return INET_E_INVALID_URL;
3903 hr = validate_userinfo(builder, data, flags);
3907 hr = validate_host(builder, data, flags);
3911 setup_port(builder, data, flags);
3913 /* The URI is opaque if it doesn't have an authority component. */
3914 if(!data->is_relative)
3915 data->is_opaque = !data->username && !data->password && !data->host && !data->has_port;
3917 data->is_opaque = !data->host && !data->has_port;
3919 hr = validate_path(builder, data, flags);
3923 hr = validate_query(builder, data, flags);
3927 hr = validate_fragment(builder, data, flags);
3931 TRACE("(%p %p %x): Finished validating builder components.\n", builder, data, flags);
3936 static void convert_to_dos_path(const WCHAR *path, DWORD path_len,
3937 WCHAR *output, DWORD *output_len)
3939 const WCHAR *ptr = path;
3941 if(path_len > 3 && *ptr == '/' && is_drive_path(path+1))
3942 /* Skip over the leading / before the drive path. */
3945 for(; ptr < path+path_len; ++ptr) {
3958 /* Generates a raw uri string using the parse_data. */
3959 static DWORD generate_raw_uri(const parse_data *data, BSTR uri, DWORD flags) {
3964 memcpy(uri, data->scheme, data->scheme_len*sizeof(WCHAR));
3965 uri[data->scheme_len] = ':';
3967 length += data->scheme_len+1;
3970 if(!data->is_opaque) {
3971 /* For the "//" which appears before the authority component. */
3974 uri[length+1] = '/';
3978 /* Check if we need to add the "\\" before the host name
3979 * of a UNC server name in a DOS path.
3981 if(flags & RAW_URI_CONVERT_TO_DOS_PATH &&
3982 data->scheme_type == URL_SCHEME_FILE && data->host) {
3985 uri[length+1] = '\\';
3991 if(data->username) {
3993 memcpy(uri+length, data->username, data->username_len*sizeof(WCHAR));
3994 length += data->username_len;
3997 if(data->password) {
4000 memcpy(uri+length+1, data->password, data->password_len*sizeof(WCHAR));
4002 length += data->password_len+1;
4005 if(data->password || data->username) {
4012 /* IPv6 addresses get the brackets added around them if they don't already
4015 const BOOL add_brackets = data->host_type == Uri_HOST_IPV6 && *(data->host) != '[';
4023 memcpy(uri+length, data->host, data->host_len*sizeof(WCHAR));
4024 length += data->host_len;
4033 if(data->has_port) {
4034 /* The port isn't included in the raw uri if it's the default
4035 * port for the scheme type.
4038 BOOL is_default = FALSE;
4040 for(i = 0; i < sizeof(default_ports)/sizeof(default_ports[0]); ++i) {
4041 if(data->scheme_type == default_ports[i].scheme &&
4042 data->port_value == default_ports[i].port)
4046 if(!is_default || flags & RAW_URI_FORCE_PORT_DISP) {
4052 length += ui2str(uri+length, data->port_value);
4054 length += ui2str(NULL, data->port_value);
4058 /* Check if a '/' should be added before the path for hierarchical URIs. */
4059 if(!data->is_opaque && data->path && *(data->path) != '/') {
4066 if(!data->is_opaque && data->scheme_type == URL_SCHEME_FILE &&
4067 flags & RAW_URI_CONVERT_TO_DOS_PATH) {
4071 convert_to_dos_path(data->path, data->path_len, uri+length, &len);
4073 convert_to_dos_path(data->path, data->path_len, NULL, &len);
4078 memcpy(uri+length, data->path, data->path_len*sizeof(WCHAR));
4079 length += data->path_len;
4085 memcpy(uri+length, data->query, data->query_len*sizeof(WCHAR));
4086 length += data->query_len;
4089 if(data->fragment) {
4091 memcpy(uri+length, data->fragment, data->fragment_len*sizeof(WCHAR));
4092 length += data->fragment_len;
4096 TRACE("(%p %p): Generated raw uri=%s len=%d\n", data, uri, debugstr_wn(uri, length), length);
4098 TRACE("(%p %p): Computed raw uri len=%d\n", data, uri, length);
4103 static HRESULT generate_uri(const UriBuilder *builder, const parse_data *data, Uri *uri, DWORD flags) {
4105 DWORD length = generate_raw_uri(data, NULL, 0);
4106 uri->raw_uri = SysAllocStringLen(NULL, length);
4108 return E_OUTOFMEMORY;
4110 generate_raw_uri(data, uri->raw_uri, 0);
4112 hr = canonicalize_uri(data, uri, flags);
4114 if(hr == E_INVALIDARG)
4115 return INET_E_INVALID_URL;
4119 uri->create_flags = flags;
4123 #define URI_THIS(iface) DEFINE_THIS(Uri, IUri, iface)
4125 static HRESULT WINAPI Uri_QueryInterface(IUri *iface, REFIID riid, void **ppv)
4127 Uri *This = URI_THIS(iface);
4129 if(IsEqualGUID(&IID_IUnknown, riid)) {
4130 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
4132 }else if(IsEqualGUID(&IID_IUri, riid)) {
4133 TRACE("(%p)->(IID_IUri %p)\n", This, ppv);
4135 }else if(IsEqualGUID(&IID_IUriObj, riid)) {
4136 TRACE("(%p)->(IID_IUriObj %p)\n", This, ppv);
4140 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
4142 return E_NOINTERFACE;
4145 IUnknown_AddRef((IUnknown*)*ppv);
4149 static ULONG WINAPI Uri_AddRef(IUri *iface)
4151 Uri *This = URI_THIS(iface);
4152 LONG ref = InterlockedIncrement(&This->ref);
4154 TRACE("(%p) ref=%d\n", This, ref);
4159 static ULONG WINAPI Uri_Release(IUri *iface)
4161 Uri *This = URI_THIS(iface);
4162 LONG ref = InterlockedDecrement(&This->ref);
4164 TRACE("(%p) ref=%d\n", This, ref);
4167 SysFreeString(This->raw_uri);
4168 heap_free(This->canon_uri);
4175 static HRESULT WINAPI Uri_GetPropertyBSTR(IUri *iface, Uri_PROPERTY uriProp, BSTR *pbstrProperty, DWORD dwFlags)
4177 Uri *This = URI_THIS(iface);
4179 TRACE("(%p)->(%d %p %x)\n", This, uriProp, pbstrProperty, dwFlags);
4184 if(uriProp > Uri_PROPERTY_STRING_LAST) {
4185 /* Windows allocates an empty BSTR for invalid Uri_PROPERTY's. */
4186 *pbstrProperty = SysAllocStringLen(NULL, 0);
4187 if(!(*pbstrProperty))
4188 return E_OUTOFMEMORY;
4190 /* It only returns S_FALSE for the ZONE property... */
4191 if(uriProp == Uri_PROPERTY_ZONE)
4197 /* Don't have support for flags yet. */
4199 FIXME("(%p)->(%d %p %x)\n", This, uriProp, pbstrProperty, dwFlags);
4204 case Uri_PROPERTY_ABSOLUTE_URI:
4205 if(This->display_modifiers & URI_DISPLAY_NO_ABSOLUTE_URI) {
4206 *pbstrProperty = SysAllocStringLen(NULL, 0);
4209 if(This->scheme_type != URL_SCHEME_UNKNOWN && This->userinfo_start > -1) {
4210 if(This->userinfo_len == 0) {
4211 /* Don't include the '@' after the userinfo component. */
4212 *pbstrProperty = SysAllocStringLen(NULL, This->canon_len-1);
4214 if(*pbstrProperty) {
4215 /* Copy everything before it. */
4216 memcpy(*pbstrProperty, This->canon_uri, This->userinfo_start*sizeof(WCHAR));
4218 /* And everything after it. */
4219 memcpy(*pbstrProperty+This->userinfo_start, This->canon_uri+This->userinfo_start+1,
4220 (This->canon_len-This->userinfo_start-1)*sizeof(WCHAR));
4222 } else if(This->userinfo_split == 0 && This->userinfo_len == 1) {
4223 /* Don't include the ":@" */
4224 *pbstrProperty = SysAllocStringLen(NULL, This->canon_len-2);
4226 if(*pbstrProperty) {
4227 memcpy(*pbstrProperty, This->canon_uri, This->userinfo_start*sizeof(WCHAR));
4228 memcpy(*pbstrProperty+This->userinfo_start, This->canon_uri+This->userinfo_start+2,
4229 (This->canon_len-This->userinfo_start-2)*sizeof(WCHAR));
4232 *pbstrProperty = SysAllocString(This->canon_uri);
4236 *pbstrProperty = SysAllocString(This->canon_uri);
4241 if(!(*pbstrProperty))
4242 hres = E_OUTOFMEMORY;
4245 case Uri_PROPERTY_AUTHORITY:
4246 if(This->authority_start > -1) {
4247 if(This->port_offset > -1 && is_default_port(This->scheme_type, This->port) &&
4248 This->display_modifiers & URI_DISPLAY_NO_DEFAULT_PORT_AUTH)
4249 /* Don't include the port in the authority component. */
4250 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->authority_start, This->port_offset);
4252 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->authority_start, This->authority_len);
4255 *pbstrProperty = SysAllocStringLen(NULL, 0);
4259 if(!(*pbstrProperty))
4260 hres = E_OUTOFMEMORY;
4263 case Uri_PROPERTY_DISPLAY_URI:
4264 /* The Display URI contains everything except for the userinfo for known
4267 if(This->scheme_type != URL_SCHEME_UNKNOWN && This->userinfo_start > -1) {
4268 *pbstrProperty = SysAllocStringLen(NULL, This->canon_len-This->userinfo_len);
4270 if(*pbstrProperty) {
4271 /* Copy everything before the userinfo over. */
4272 memcpy(*pbstrProperty, This->canon_uri, This->userinfo_start*sizeof(WCHAR));
4273 /* Copy everything after the userinfo over. */
4274 memcpy(*pbstrProperty+This->userinfo_start,
4275 This->canon_uri+This->userinfo_start+This->userinfo_len+1,
4276 (This->canon_len-(This->userinfo_start+This->userinfo_len+1))*sizeof(WCHAR));
4279 *pbstrProperty = SysAllocString(This->canon_uri);
4281 if(!(*pbstrProperty))
4282 hres = E_OUTOFMEMORY;
4287 case Uri_PROPERTY_DOMAIN:
4288 if(This->domain_offset > -1) {
4289 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->host_start+This->domain_offset,
4290 This->host_len-This->domain_offset);
4293 *pbstrProperty = SysAllocStringLen(NULL, 0);
4297 if(!(*pbstrProperty))
4298 hres = E_OUTOFMEMORY;
4301 case Uri_PROPERTY_EXTENSION:
4302 if(This->extension_offset > -1) {
4303 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->path_start+This->extension_offset,
4304 This->path_len-This->extension_offset);
4307 *pbstrProperty = SysAllocStringLen(NULL, 0);
4311 if(!(*pbstrProperty))
4312 hres = E_OUTOFMEMORY;
4315 case Uri_PROPERTY_FRAGMENT:
4316 if(This->fragment_start > -1) {
4317 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->fragment_start, This->fragment_len);
4320 *pbstrProperty = SysAllocStringLen(NULL, 0);
4324 if(!(*pbstrProperty))
4325 hres = E_OUTOFMEMORY;
4328 case Uri_PROPERTY_HOST:
4329 if(This->host_start > -1) {
4330 /* The '[' and ']' aren't included for IPv6 addresses. */
4331 if(This->host_type == Uri_HOST_IPV6)
4332 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->host_start+1, This->host_len-2);
4334 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->host_start, This->host_len);
4338 *pbstrProperty = SysAllocStringLen(NULL, 0);
4342 if(!(*pbstrProperty))
4343 hres = E_OUTOFMEMORY;
4346 case Uri_PROPERTY_PASSWORD:
4347 if(This->userinfo_split > -1) {
4348 *pbstrProperty = SysAllocStringLen(
4349 This->canon_uri+This->userinfo_start+This->userinfo_split+1,
4350 This->userinfo_len-This->userinfo_split-1);
4353 *pbstrProperty = SysAllocStringLen(NULL, 0);
4357 if(!(*pbstrProperty))
4358 return E_OUTOFMEMORY;
4361 case Uri_PROPERTY_PATH:
4362 if(This->path_start > -1) {
4363 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->path_start, This->path_len);
4366 *pbstrProperty = SysAllocStringLen(NULL, 0);
4370 if(!(*pbstrProperty))
4371 hres = E_OUTOFMEMORY;
4374 case Uri_PROPERTY_PATH_AND_QUERY:
4375 if(This->path_start > -1) {
4376 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->path_start, This->path_len+This->query_len);
4378 } else if(This->query_start > -1) {
4379 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->query_start, This->query_len);
4382 *pbstrProperty = SysAllocStringLen(NULL, 0);
4386 if(!(*pbstrProperty))
4387 hres = E_OUTOFMEMORY;
4390 case Uri_PROPERTY_QUERY:
4391 if(This->query_start > -1) {
4392 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->query_start, This->query_len);
4395 *pbstrProperty = SysAllocStringLen(NULL, 0);
4399 if(!(*pbstrProperty))
4400 hres = E_OUTOFMEMORY;
4403 case Uri_PROPERTY_RAW_URI:
4404 *pbstrProperty = SysAllocString(This->raw_uri);
4405 if(!(*pbstrProperty))
4406 hres = E_OUTOFMEMORY;
4410 case Uri_PROPERTY_SCHEME_NAME:
4411 if(This->scheme_start > -1) {
4412 *pbstrProperty = SysAllocStringLen(This->canon_uri + This->scheme_start, This->scheme_len);
4415 *pbstrProperty = SysAllocStringLen(NULL, 0);
4419 if(!(*pbstrProperty))
4420 hres = E_OUTOFMEMORY;
4423 case Uri_PROPERTY_USER_INFO:
4424 if(This->userinfo_start > -1) {
4425 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->userinfo_start, This->userinfo_len);
4428 *pbstrProperty = SysAllocStringLen(NULL, 0);
4432 if(!(*pbstrProperty))
4433 hres = E_OUTOFMEMORY;
4436 case Uri_PROPERTY_USER_NAME:
4437 if(This->userinfo_start > -1 && This->userinfo_split != 0) {
4438 /* If userinfo_split is set, that means a password exists
4439 * so the username is only from userinfo_start to userinfo_split.
4441 if(This->userinfo_split > -1) {
4442 *pbstrProperty = SysAllocStringLen(This->canon_uri + This->userinfo_start, This->userinfo_split);
4445 *pbstrProperty = SysAllocStringLen(This->canon_uri + This->userinfo_start, This->userinfo_len);
4449 *pbstrProperty = SysAllocStringLen(NULL, 0);
4453 if(!(*pbstrProperty))
4454 return E_OUTOFMEMORY;
4458 FIXME("(%p)->(%d %p %x)\n", This, uriProp, pbstrProperty, dwFlags);
4465 static HRESULT WINAPI Uri_GetPropertyLength(IUri *iface, Uri_PROPERTY uriProp, DWORD *pcchProperty, DWORD dwFlags)
4467 Uri *This = URI_THIS(iface);
4469 TRACE("(%p)->(%d %p %x)\n", This, uriProp, pcchProperty, dwFlags);
4472 return E_INVALIDARG;
4474 /* Can only return a length for a property if it's a string. */
4475 if(uriProp > Uri_PROPERTY_STRING_LAST)
4476 return E_INVALIDARG;
4478 /* Don't have support for flags yet. */
4480 FIXME("(%p)->(%d %p %x)\n", This, uriProp, pcchProperty, dwFlags);
4485 case Uri_PROPERTY_ABSOLUTE_URI:
4486 if(This->display_modifiers & URI_DISPLAY_NO_ABSOLUTE_URI) {
4490 if(This->scheme_type != URL_SCHEME_UNKNOWN) {
4491 if(This->userinfo_start > -1 && This->userinfo_len == 0)
4492 /* Don't include the '@' in the length. */
4493 *pcchProperty = This->canon_len-1;
4494 else if(This->userinfo_start > -1 && This->userinfo_len == 1 &&
4495 This->userinfo_split == 0)
4496 /* Don't include the ":@" in the length. */
4497 *pcchProperty = This->canon_len-2;
4499 *pcchProperty = This->canon_len;
4501 *pcchProperty = This->canon_len;
4507 case Uri_PROPERTY_AUTHORITY:
4508 if(This->port_offset > -1 &&
4509 This->display_modifiers & URI_DISPLAY_NO_DEFAULT_PORT_AUTH &&
4510 is_default_port(This->scheme_type, This->port))
4511 /* Only count up until the port in the authority. */
4512 *pcchProperty = This->port_offset;
4514 *pcchProperty = This->authority_len;
4515 hres = (This->authority_start > -1) ? S_OK : S_FALSE;
4517 case Uri_PROPERTY_DISPLAY_URI:
4518 if(This->scheme_type != URL_SCHEME_UNKNOWN && This->userinfo_start > -1)
4519 *pcchProperty = This->canon_len-This->userinfo_len-1;
4521 *pcchProperty = This->canon_len;
4525 case Uri_PROPERTY_DOMAIN:
4526 if(This->domain_offset > -1)
4527 *pcchProperty = This->host_len - This->domain_offset;
4531 hres = (This->domain_offset > -1) ? S_OK : S_FALSE;
4533 case Uri_PROPERTY_EXTENSION:
4534 if(This->extension_offset > -1) {
4535 *pcchProperty = This->path_len - This->extension_offset;
4543 case Uri_PROPERTY_FRAGMENT:
4544 *pcchProperty = This->fragment_len;
4545 hres = (This->fragment_start > -1) ? S_OK : S_FALSE;
4547 case Uri_PROPERTY_HOST:
4548 *pcchProperty = This->host_len;
4550 /* '[' and ']' aren't included in the length. */
4551 if(This->host_type == Uri_HOST_IPV6)
4554 hres = (This->host_start > -1) ? S_OK : S_FALSE;
4556 case Uri_PROPERTY_PASSWORD:
4557 *pcchProperty = (This->userinfo_split > -1) ? This->userinfo_len-This->userinfo_split-1 : 0;
4558 hres = (This->userinfo_split > -1) ? S_OK : S_FALSE;
4560 case Uri_PROPERTY_PATH:
4561 *pcchProperty = This->path_len;
4562 hres = (This->path_start > -1) ? S_OK : S_FALSE;
4564 case Uri_PROPERTY_PATH_AND_QUERY:
4565 *pcchProperty = This->path_len+This->query_len;
4566 hres = (This->path_start > -1 || This->query_start > -1) ? S_OK : S_FALSE;
4568 case Uri_PROPERTY_QUERY:
4569 *pcchProperty = This->query_len;
4570 hres = (This->query_start > -1) ? S_OK : S_FALSE;
4572 case Uri_PROPERTY_RAW_URI:
4573 *pcchProperty = SysStringLen(This->raw_uri);
4576 case Uri_PROPERTY_SCHEME_NAME:
4577 *pcchProperty = This->scheme_len;
4578 hres = (This->scheme_start > -1) ? S_OK : S_FALSE;
4580 case Uri_PROPERTY_USER_INFO:
4581 *pcchProperty = This->userinfo_len;
4582 hres = (This->userinfo_start > -1) ? S_OK : S_FALSE;
4584 case Uri_PROPERTY_USER_NAME:
4585 *pcchProperty = (This->userinfo_split > -1) ? This->userinfo_split : This->userinfo_len;
4586 if(This->userinfo_split == 0)
4589 hres = (This->userinfo_start > -1) ? S_OK : S_FALSE;
4592 FIXME("(%p)->(%d %p %x)\n", This, uriProp, pcchProperty, dwFlags);
4599 static HRESULT WINAPI Uri_GetPropertyDWORD(IUri *iface, Uri_PROPERTY uriProp, DWORD *pcchProperty, DWORD dwFlags)
4601 Uri *This = URI_THIS(iface);
4604 TRACE("(%p)->(%d %p %x)\n", This, uriProp, pcchProperty, dwFlags);
4607 return E_INVALIDARG;
4609 /* Microsoft's implementation for the ZONE property of a URI seems to be lacking...
4610 * From what I can tell, instead of checking which URLZONE the URI belongs to it
4611 * simply assigns URLZONE_INVALID and returns E_NOTIMPL. This also applies to the GetZone
4614 if(uriProp == Uri_PROPERTY_ZONE) {
4615 *pcchProperty = URLZONE_INVALID;
4619 if(uriProp < Uri_PROPERTY_DWORD_START) {
4621 return E_INVALIDARG;
4625 case Uri_PROPERTY_HOST_TYPE:
4626 *pcchProperty = This->host_type;
4629 case Uri_PROPERTY_PORT:
4630 if(!This->has_port) {
4634 *pcchProperty = This->port;
4639 case Uri_PROPERTY_SCHEME:
4640 *pcchProperty = This->scheme_type;
4644 FIXME("(%p)->(%d %p %x)\n", This, uriProp, pcchProperty, dwFlags);
4651 static HRESULT WINAPI Uri_HasProperty(IUri *iface, Uri_PROPERTY uriProp, BOOL *pfHasProperty)
4653 Uri *This = URI_THIS(iface);
4654 TRACE("(%p)->(%d %p)\n", This, uriProp, pfHasProperty);
4657 return E_INVALIDARG;
4660 case Uri_PROPERTY_ABSOLUTE_URI:
4661 *pfHasProperty = !(This->display_modifiers & URI_DISPLAY_NO_ABSOLUTE_URI);
4663 case Uri_PROPERTY_AUTHORITY:
4664 *pfHasProperty = This->authority_start > -1;
4666 case Uri_PROPERTY_DISPLAY_URI:
4667 *pfHasProperty = TRUE;
4669 case Uri_PROPERTY_DOMAIN:
4670 *pfHasProperty = This->domain_offset > -1;
4672 case Uri_PROPERTY_EXTENSION:
4673 *pfHasProperty = This->extension_offset > -1;
4675 case Uri_PROPERTY_FRAGMENT:
4676 *pfHasProperty = This->fragment_start > -1;
4678 case Uri_PROPERTY_HOST:
4679 *pfHasProperty = This->host_start > -1;
4681 case Uri_PROPERTY_PASSWORD:
4682 *pfHasProperty = This->userinfo_split > -1;
4684 case Uri_PROPERTY_PATH:
4685 *pfHasProperty = This->path_start > -1;
4687 case Uri_PROPERTY_PATH_AND_QUERY:
4688 *pfHasProperty = (This->path_start > -1 || This->query_start > -1);
4690 case Uri_PROPERTY_QUERY:
4691 *pfHasProperty = This->query_start > -1;
4693 case Uri_PROPERTY_RAW_URI:
4694 *pfHasProperty = TRUE;
4696 case Uri_PROPERTY_SCHEME_NAME:
4697 *pfHasProperty = This->scheme_start > -1;
4699 case Uri_PROPERTY_USER_INFO:
4700 *pfHasProperty = This->userinfo_start > -1;
4702 case Uri_PROPERTY_USER_NAME:
4703 if(This->userinfo_split == 0)
4704 *pfHasProperty = FALSE;
4706 *pfHasProperty = This->userinfo_start > -1;
4708 case Uri_PROPERTY_HOST_TYPE:
4709 *pfHasProperty = TRUE;
4711 case Uri_PROPERTY_PORT:
4712 *pfHasProperty = This->has_port;
4714 case Uri_PROPERTY_SCHEME:
4715 *pfHasProperty = TRUE;
4717 case Uri_PROPERTY_ZONE:
4718 *pfHasProperty = FALSE;
4721 FIXME("(%p)->(%d %p): Unsupported property type.\n", This, uriProp, pfHasProperty);
4728 static HRESULT WINAPI Uri_GetAbsoluteUri(IUri *iface, BSTR *pstrAbsoluteUri)
4730 TRACE("(%p)->(%p)\n", iface, pstrAbsoluteUri);
4731 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_ABSOLUTE_URI, pstrAbsoluteUri, 0);
4734 static HRESULT WINAPI Uri_GetAuthority(IUri *iface, BSTR *pstrAuthority)
4736 TRACE("(%p)->(%p)\n", iface, pstrAuthority);
4737 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_AUTHORITY, pstrAuthority, 0);
4740 static HRESULT WINAPI Uri_GetDisplayUri(IUri *iface, BSTR *pstrDisplayUri)
4742 TRACE("(%p)->(%p)\n", iface, pstrDisplayUri);
4743 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_DISPLAY_URI, pstrDisplayUri, 0);
4746 static HRESULT WINAPI Uri_GetDomain(IUri *iface, BSTR *pstrDomain)
4748 TRACE("(%p)->(%p)\n", iface, pstrDomain);
4749 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_DOMAIN, pstrDomain, 0);
4752 static HRESULT WINAPI Uri_GetExtension(IUri *iface, BSTR *pstrExtension)
4754 TRACE("(%p)->(%p)\n", iface, pstrExtension);
4755 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_EXTENSION, pstrExtension, 0);
4758 static HRESULT WINAPI Uri_GetFragment(IUri *iface, BSTR *pstrFragment)
4760 TRACE("(%p)->(%p)\n", iface, pstrFragment);
4761 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_FRAGMENT, pstrFragment, 0);
4764 static HRESULT WINAPI Uri_GetHost(IUri *iface, BSTR *pstrHost)
4766 TRACE("(%p)->(%p)\n", iface, pstrHost);
4767 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_HOST, pstrHost, 0);
4770 static HRESULT WINAPI Uri_GetPassword(IUri *iface, BSTR *pstrPassword)
4772 TRACE("(%p)->(%p)\n", iface, pstrPassword);
4773 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_PASSWORD, pstrPassword, 0);
4776 static HRESULT WINAPI Uri_GetPath(IUri *iface, BSTR *pstrPath)
4778 TRACE("(%p)->(%p)\n", iface, pstrPath);
4779 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_PATH, pstrPath, 0);
4782 static HRESULT WINAPI Uri_GetPathAndQuery(IUri *iface, BSTR *pstrPathAndQuery)
4784 TRACE("(%p)->(%p)\n", iface, pstrPathAndQuery);
4785 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_PATH_AND_QUERY, pstrPathAndQuery, 0);
4788 static HRESULT WINAPI Uri_GetQuery(IUri *iface, BSTR *pstrQuery)
4790 TRACE("(%p)->(%p)\n", iface, pstrQuery);
4791 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_QUERY, pstrQuery, 0);
4794 static HRESULT WINAPI Uri_GetRawUri(IUri *iface, BSTR *pstrRawUri)
4796 TRACE("(%p)->(%p)\n", iface, pstrRawUri);
4797 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_RAW_URI, pstrRawUri, 0);
4800 static HRESULT WINAPI Uri_GetSchemeName(IUri *iface, BSTR *pstrSchemeName)
4802 TRACE("(%p)->(%p)\n", iface, pstrSchemeName);
4803 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_SCHEME_NAME, pstrSchemeName, 0);
4806 static HRESULT WINAPI Uri_GetUserInfo(IUri *iface, BSTR *pstrUserInfo)
4808 TRACE("(%p)->(%p)\n", iface, pstrUserInfo);
4809 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_USER_INFO, pstrUserInfo, 0);
4812 static HRESULT WINAPI Uri_GetUserName(IUri *iface, BSTR *pstrUserName)
4814 TRACE("(%p)->(%p)\n", iface, pstrUserName);
4815 return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_USER_NAME, pstrUserName, 0);
4818 static HRESULT WINAPI Uri_GetHostType(IUri *iface, DWORD *pdwHostType)
4820 TRACE("(%p)->(%p)\n", iface, pdwHostType);
4821 return IUri_GetPropertyDWORD(iface, Uri_PROPERTY_HOST_TYPE, pdwHostType, 0);
4824 static HRESULT WINAPI Uri_GetPort(IUri *iface, DWORD *pdwPort)
4826 TRACE("(%p)->(%p)\n", iface, pdwPort);
4827 return IUri_GetPropertyDWORD(iface, Uri_PROPERTY_PORT, pdwPort, 0);
4830 static HRESULT WINAPI Uri_GetScheme(IUri *iface, DWORD *pdwScheme)
4832 Uri *This = URI_THIS(iface);
4833 TRACE("(%p)->(%p)\n", This, pdwScheme);
4834 return IUri_GetPropertyDWORD(iface, Uri_PROPERTY_SCHEME, pdwScheme, 0);
4837 static HRESULT WINAPI Uri_GetZone(IUri *iface, DWORD *pdwZone)
4839 TRACE("(%p)->(%p)\n", iface, pdwZone);
4840 return IUri_GetPropertyDWORD(iface, Uri_PROPERTY_ZONE,pdwZone, 0);
4843 static HRESULT WINAPI Uri_GetProperties(IUri *iface, DWORD *pdwProperties)
4845 Uri *This = URI_THIS(iface);
4846 TRACE("(%p)->(%p)\n", This, pdwProperties);
4849 return E_INVALIDARG;
4851 /* All URIs have these. */
4852 *pdwProperties = Uri_HAS_DISPLAY_URI|Uri_HAS_RAW_URI|Uri_HAS_SCHEME|Uri_HAS_HOST_TYPE;
4854 if(!(This->display_modifiers & URI_DISPLAY_NO_ABSOLUTE_URI))
4855 *pdwProperties |= Uri_HAS_ABSOLUTE_URI;
4857 if(This->scheme_start > -1)
4858 *pdwProperties |= Uri_HAS_SCHEME_NAME;
4860 if(This->authority_start > -1) {
4861 *pdwProperties |= Uri_HAS_AUTHORITY;
4862 if(This->userinfo_start > -1) {
4863 *pdwProperties |= Uri_HAS_USER_INFO;
4864 if(This->userinfo_split != 0)
4865 *pdwProperties |= Uri_HAS_USER_NAME;
4867 if(This->userinfo_split > -1)
4868 *pdwProperties |= Uri_HAS_PASSWORD;
4869 if(This->host_start > -1)
4870 *pdwProperties |= Uri_HAS_HOST;
4871 if(This->domain_offset > -1)
4872 *pdwProperties |= Uri_HAS_DOMAIN;
4876 *pdwProperties |= Uri_HAS_PORT;
4877 if(This->path_start > -1)
4878 *pdwProperties |= Uri_HAS_PATH|Uri_HAS_PATH_AND_QUERY;
4879 if(This->query_start > -1)
4880 *pdwProperties |= Uri_HAS_QUERY|Uri_HAS_PATH_AND_QUERY;
4882 if(This->extension_offset > -1)
4883 *pdwProperties |= Uri_HAS_EXTENSION;
4885 if(This->fragment_start > -1)
4886 *pdwProperties |= Uri_HAS_FRAGMENT;
4891 static HRESULT WINAPI Uri_IsEqual(IUri *iface, IUri *pUri, BOOL *pfEqual)
4893 Uri *This = URI_THIS(iface);
4896 TRACE("(%p)->(%p %p)\n", This, pUri, pfEqual);
4904 /* For some reason Windows returns S_OK here... */
4908 /* Try to convert it to a Uri (allows for a more simple comparison). */
4909 if((other = get_uri_obj(pUri)))
4910 *pfEqual = are_equal_simple(This, other);
4912 /* Do it the hard way. */
4913 FIXME("(%p)->(%p %p) No support for unknown IUri's yet.\n", iface, pUri, pfEqual);
4922 static const IUriVtbl UriVtbl = {
4926 Uri_GetPropertyBSTR,
4927 Uri_GetPropertyLength,
4928 Uri_GetPropertyDWORD,
4939 Uri_GetPathAndQuery,
4953 static Uri* create_uri_obj(void) {
4954 Uri *ret = heap_alloc_zero(sizeof(Uri));
4956 ret->lpIUriVtbl = &UriVtbl;
4963 /***********************************************************************
4964 * CreateUri (urlmon.@)
4966 * Creates a new IUri object using the URI represented by pwzURI. This function
4967 * parses and validates the components of pwzURI and then canonicalizes the
4968 * parsed components.
4971 * pwzURI [I] The URI to parse, validate, and canonicalize.
4972 * dwFlags [I] Flags which can affect how the parsing/canonicalization is performed.
4973 * dwReserved [I] Reserved (not used).
4974 * ppURI [O] The resulting IUri after parsing/canonicalization occurs.
4977 * Success: Returns S_OK. ppURI contains the pointer to the newly allocated IUri.
4978 * Failure: E_INVALIDARG if there's invalid flag combinations in dwFlags, or an
4979 * invalid parameters, or pwzURI doesn't represnt a valid URI.
4980 * E_OUTOFMEMORY if any memory allocation fails.
4984 * Uri_CREATE_CANONICALIZE, Uri_CREATE_DECODE_EXTRA_INFO, Uri_CREATE_CRACK_UNKNOWN_SCHEMES,
4985 * Uri_CREATE_PRE_PROCESS_HTML_URI, Uri_CREATE_NO_IE_SETTINGS.
4987 HRESULT WINAPI CreateUri(LPCWSTR pwzURI, DWORD dwFlags, DWORD_PTR dwReserved, IUri **ppURI)
4989 const DWORD supported_flags = Uri_CREATE_ALLOW_RELATIVE|Uri_CREATE_ALLOW_IMPLICIT_WILDCARD_SCHEME|
4990 Uri_CREATE_ALLOW_IMPLICIT_FILE_SCHEME|Uri_CREATE_NO_CANONICALIZE|Uri_CREATE_CANONICALIZE|
4991 Uri_CREATE_DECODE_EXTRA_INFO|Uri_CREATE_NO_DECODE_EXTRA_INFO|Uri_CREATE_CRACK_UNKNOWN_SCHEMES|
4992 Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES|Uri_CREATE_PRE_PROCESS_HTML_URI|Uri_CREATE_NO_PRE_PROCESS_HTML_URI|
4993 Uri_CREATE_NO_IE_SETTINGS|Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS|Uri_CREATE_FILE_USE_DOS_PATH;
4998 TRACE("(%s %x %x %p)\n", debugstr_w(pwzURI), dwFlags, (DWORD)dwReserved, ppURI);
5001 return E_INVALIDARG;
5003 if(!pwzURI || !*pwzURI) {
5005 return E_INVALIDARG;
5008 /* Check for invalid flags. */
5009 if(has_invalid_flag_combination(dwFlags)) {
5011 return E_INVALIDARG;
5014 /* Currently unsupported. */
5015 if(dwFlags & ~supported_flags)
5016 FIXME("Ignoring unsupported flag(s) %x\n", dwFlags & ~supported_flags);
5018 ret = create_uri_obj();
5021 return E_OUTOFMEMORY;
5024 /* Explicitly set the default flags if it doesn't cause a flag conflict. */
5025 apply_default_flags(&dwFlags);
5027 /* Pre process the URI, unless told otherwise. */
5028 if(!(dwFlags & Uri_CREATE_NO_PRE_PROCESS_HTML_URI))
5029 ret->raw_uri = pre_process_uri(pwzURI);
5031 ret->raw_uri = SysAllocString(pwzURI);
5035 return E_OUTOFMEMORY;
5038 memset(&data, 0, sizeof(parse_data));
5039 data.uri = ret->raw_uri;
5041 /* Validate and parse the URI into it's components. */
5042 if(!parse_uri(&data, dwFlags)) {
5043 /* Encountered an unsupported or invalid URI */
5044 IUri_Release(URI(ret));
5046 return E_INVALIDARG;
5049 /* Canonicalize the URI. */
5050 hr = canonicalize_uri(&data, ret, dwFlags);
5052 IUri_Release(URI(ret));
5057 ret->create_flags = dwFlags;
5063 /***********************************************************************
5064 * CreateUriWithFragment (urlmon.@)
5066 * Creates a new IUri object. This is almost the same as CreateUri, expect that
5067 * it allows you to explicitly specify a fragment (pwzFragment) for pwzURI.
5070 * pwzURI [I] The URI to parse and perform canonicalization on.
5071 * pwzFragment [I] The explict fragment string which should be added to pwzURI.
5072 * dwFlags [I] The flags which will be passed to CreateUri.
5073 * dwReserved [I] Reserved (not used).
5074 * ppURI [O] The resulting IUri after parsing/canonicalization.
5077 * Success: S_OK. ppURI contains the pointer to the newly allocated IUri.
5078 * Failure: E_INVALIDARG if pwzURI already contains a fragment and pwzFragment
5079 * isn't NULL. Will also return E_INVALIDARG for the same reasons as
5080 * CreateUri will. E_OUTOFMEMORY if any allocations fail.
5082 HRESULT WINAPI CreateUriWithFragment(LPCWSTR pwzURI, LPCWSTR pwzFragment, DWORD dwFlags,
5083 DWORD_PTR dwReserved, IUri **ppURI)
5086 TRACE("(%s %s %x %x %p)\n", debugstr_w(pwzURI), debugstr_w(pwzFragment), dwFlags, (DWORD)dwReserved, ppURI);
5089 return E_INVALIDARG;
5093 return E_INVALIDARG;
5096 /* Check if a fragment should be appended to the URI string. */
5099 DWORD uri_len, frag_len;
5102 /* Check if the original URI already has a fragment component. */
5103 if(StrChrW(pwzURI, '#')) {
5105 return E_INVALIDARG;
5108 uri_len = lstrlenW(pwzURI);
5109 frag_len = lstrlenW(pwzFragment);
5111 /* If the fragment doesn't start with a '#', one will be added. */
5112 add_pound = *pwzFragment != '#';
5115 uriW = heap_alloc((uri_len+frag_len+2)*sizeof(WCHAR));
5117 uriW = heap_alloc((uri_len+frag_len+1)*sizeof(WCHAR));
5120 return E_OUTOFMEMORY;
5122 memcpy(uriW, pwzURI, uri_len*sizeof(WCHAR));
5124 uriW[uri_len++] = '#';
5125 memcpy(uriW+uri_len, pwzFragment, (frag_len+1)*sizeof(WCHAR));
5127 hres = CreateUri(uriW, dwFlags, 0, ppURI);
5131 /* A fragment string wasn't specified, so just forward the call. */
5132 hres = CreateUri(pwzURI, dwFlags, 0, ppURI);
5137 static HRESULT build_uri(const UriBuilder *builder, IUri **uri, DWORD create_flags,
5138 DWORD use_orig_flags, DWORD encoding_mask)
5147 if(encoding_mask && (!builder->uri || builder->modified_props)) {
5152 /* Decide what flags should be used when creating the Uri. */
5153 if((use_orig_flags & UriBuilder_USE_ORIGINAL_FLAGS) && builder->uri)
5154 create_flags = builder->uri->create_flags;
5156 if(has_invalid_flag_combination(create_flags)) {
5158 return E_INVALIDARG;
5161 /* Set the default flags if they don't cause a conflict. */
5162 apply_default_flags(&create_flags);
5165 /* Return the base IUri if no changes have been made and the create_flags match. */
5166 if(builder->uri && !builder->modified_props && builder->uri->create_flags == create_flags) {
5167 *uri = URI(builder->uri);
5172 hr = validate_components(builder, &data, create_flags);
5178 ret = create_uri_obj();
5181 return E_OUTOFMEMORY;
5184 hr = generate_uri(builder, &data, ret, create_flags);
5186 IUri_Release(URI(ret));
5195 #define URIBUILDER_THIS(iface) DEFINE_THIS(UriBuilder, IUriBuilder, iface)
5197 static HRESULT WINAPI UriBuilder_QueryInterface(IUriBuilder *iface, REFIID riid, void **ppv)
5199 UriBuilder *This = URIBUILDER_THIS(iface);
5201 if(IsEqualGUID(&IID_IUnknown, riid)) {
5202 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
5203 *ppv = URIBUILDER(This);
5204 }else if(IsEqualGUID(&IID_IUriBuilder, riid)) {
5205 TRACE("(%p)->(IID_IUri %p)\n", This, ppv);
5206 *ppv = URIBUILDER(This);
5208 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
5210 return E_NOINTERFACE;
5213 IUnknown_AddRef((IUnknown*)*ppv);
5217 static ULONG WINAPI UriBuilder_AddRef(IUriBuilder *iface)
5219 UriBuilder *This = URIBUILDER_THIS(iface);
5220 LONG ref = InterlockedIncrement(&This->ref);
5222 TRACE("(%p) ref=%d\n", This, ref);
5227 static ULONG WINAPI UriBuilder_Release(IUriBuilder *iface)
5229 UriBuilder *This = URIBUILDER_THIS(iface);
5230 LONG ref = InterlockedDecrement(&This->ref);
5232 TRACE("(%p) ref=%d\n", This, ref);
5235 if(This->uri) IUri_Release(URI(This->uri));
5236 heap_free(This->fragment);
5237 heap_free(This->host);
5238 heap_free(This->password);
5239 heap_free(This->path);
5240 heap_free(This->query);
5241 heap_free(This->scheme);
5242 heap_free(This->username);
5249 static HRESULT WINAPI UriBuilder_CreateUriSimple(IUriBuilder *iface,
5250 DWORD dwAllowEncodingPropertyMask,
5251 DWORD_PTR dwReserved,
5254 UriBuilder *This = URIBUILDER_THIS(iface);
5256 TRACE("(%p)->(%d %d %p)\n", This, dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
5258 hr = build_uri(This, ppIUri, 0, UriBuilder_USE_ORIGINAL_FLAGS, dwAllowEncodingPropertyMask);
5260 FIXME("(%p)->(%d %d %p)\n", This, dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
5264 static HRESULT WINAPI UriBuilder_CreateUri(IUriBuilder *iface,
5265 DWORD dwCreateFlags,
5266 DWORD dwAllowEncodingPropertyMask,
5267 DWORD_PTR dwReserved,
5270 UriBuilder *This = URIBUILDER_THIS(iface);
5272 TRACE("(%p)->(0x%08x %d %d %p)\n", This, dwCreateFlags, dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
5274 if(dwCreateFlags == -1)
5275 hr = build_uri(This, ppIUri, 0, UriBuilder_USE_ORIGINAL_FLAGS, dwAllowEncodingPropertyMask);
5277 hr = build_uri(This, ppIUri, dwCreateFlags, 0, dwAllowEncodingPropertyMask);
5280 FIXME("(%p)->(0x%08x %d %d %p)\n", This, dwCreateFlags, dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
5284 static HRESULT WINAPI UriBuilder_CreateUriWithFlags(IUriBuilder *iface,
5285 DWORD dwCreateFlags,
5286 DWORD dwUriBuilderFlags,
5287 DWORD dwAllowEncodingPropertyMask,
5288 DWORD_PTR dwReserved,
5291 UriBuilder *This = URIBUILDER_THIS(iface);
5293 TRACE("(%p)->(0x%08x 0x%08x %d %d %p)\n", This, dwCreateFlags, dwUriBuilderFlags,
5294 dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
5296 hr = build_uri(This, ppIUri, dwCreateFlags, dwUriBuilderFlags, dwAllowEncodingPropertyMask);
5298 FIXME("(%p)->(0x%08x 0x%08x %d %d %p)\n", This, dwCreateFlags, dwUriBuilderFlags,
5299 dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
5303 static HRESULT WINAPI UriBuilder_GetIUri(IUriBuilder *iface, IUri **ppIUri)
5305 UriBuilder *This = URIBUILDER_THIS(iface);
5306 TRACE("(%p)->(%p)\n", This, ppIUri);
5312 IUri *uri = URI(This->uri);
5321 static HRESULT WINAPI UriBuilder_SetIUri(IUriBuilder *iface, IUri *pIUri)
5323 UriBuilder *This = URIBUILDER_THIS(iface);
5324 TRACE("(%p)->(%p)\n", This, pIUri);
5329 if((uri = get_uri_obj(pIUri))) {
5330 /* Only reset the builder if it's Uri isn't the same as
5331 * the Uri passed to the function.
5333 if(This->uri != uri) {
5334 reset_builder(This);
5338 This->port = uri->port;
5343 FIXME("(%p)->(%p) Unknown IUri types not supported yet.\n", This, pIUri);
5346 } else if(This->uri)
5347 /* Only reset the builder if it's Uri isn't NULL. */
5348 reset_builder(This);
5353 static HRESULT WINAPI UriBuilder_GetFragment(IUriBuilder *iface, DWORD *pcchFragment, LPCWSTR *ppwzFragment)
5355 UriBuilder *This = URIBUILDER_THIS(iface);
5356 TRACE("(%p)->(%p %p)\n", This, pcchFragment, ppwzFragment);
5358 if(!This->uri || This->uri->fragment_start == -1 || This->modified_props & Uri_HAS_FRAGMENT)
5359 return get_builder_component(&This->fragment, &This->fragment_len, NULL, 0, ppwzFragment, pcchFragment);
5361 return get_builder_component(&This->fragment, &This->fragment_len, This->uri->canon_uri+This->uri->fragment_start,
5362 This->uri->fragment_len, ppwzFragment, pcchFragment);
5365 static HRESULT WINAPI UriBuilder_GetHost(IUriBuilder *iface, DWORD *pcchHost, LPCWSTR *ppwzHost)
5367 UriBuilder *This = URIBUILDER_THIS(iface);
5368 TRACE("(%p)->(%p %p)\n", This, pcchHost, ppwzHost);
5370 if(!This->uri || This->uri->host_start == -1 || This->modified_props & Uri_HAS_HOST)
5371 return get_builder_component(&This->host, &This->host_len, NULL, 0, ppwzHost, pcchHost);
5373 if(This->uri->host_type == Uri_HOST_IPV6)
5374 /* Don't include the '[' and ']' around the address. */
5375 return get_builder_component(&This->host, &This->host_len, This->uri->canon_uri+This->uri->host_start+1,
5376 This->uri->host_len-2, ppwzHost, pcchHost);
5378 return get_builder_component(&This->host, &This->host_len, This->uri->canon_uri+This->uri->host_start,
5379 This->uri->host_len, ppwzHost, pcchHost);
5383 static HRESULT WINAPI UriBuilder_GetPassword(IUriBuilder *iface, DWORD *pcchPassword, LPCWSTR *ppwzPassword)
5385 UriBuilder *This = URIBUILDER_THIS(iface);
5386 TRACE("(%p)->(%p %p)\n", This, pcchPassword, ppwzPassword);
5388 if(!This->uri || This->uri->userinfo_split == -1 || This->modified_props & Uri_HAS_PASSWORD)
5389 return get_builder_component(&This->password, &This->password_len, NULL, 0, ppwzPassword, pcchPassword);
5391 const WCHAR *start = This->uri->canon_uri+This->uri->userinfo_start+This->uri->userinfo_split+1;
5392 DWORD len = This->uri->userinfo_len-This->uri->userinfo_split-1;
5393 return get_builder_component(&This->password, &This->password_len, start, len, ppwzPassword, pcchPassword);
5397 static HRESULT WINAPI UriBuilder_GetPath(IUriBuilder *iface, DWORD *pcchPath, LPCWSTR *ppwzPath)
5399 UriBuilder *This = URIBUILDER_THIS(iface);
5400 TRACE("(%p)->(%p %p)\n", This, pcchPath, ppwzPath);
5402 if(!This->uri || This->uri->path_start == -1 || This->modified_props & Uri_HAS_PATH)
5403 return get_builder_component(&This->path, &This->path_len, NULL, 0, ppwzPath, pcchPath);
5405 return get_builder_component(&This->path, &This->path_len, This->uri->canon_uri+This->uri->path_start,
5406 This->uri->path_len, ppwzPath, pcchPath);
5409 static HRESULT WINAPI UriBuilder_GetPort(IUriBuilder *iface, BOOL *pfHasPort, DWORD *pdwPort)
5411 UriBuilder *This = URIBUILDER_THIS(iface);
5412 TRACE("(%p)->(%p %p)\n", This, pfHasPort, pdwPort);
5425 *pfHasPort = This->has_port;
5426 *pdwPort = This->port;
5430 static HRESULT WINAPI UriBuilder_GetQuery(IUriBuilder *iface, DWORD *pcchQuery, LPCWSTR *ppwzQuery)
5432 UriBuilder *This = URIBUILDER_THIS(iface);
5433 TRACE("(%p)->(%p %p)\n", This, pcchQuery, ppwzQuery);
5435 if(!This->uri || This->uri->query_start == -1 || This->modified_props & Uri_HAS_QUERY)
5436 return get_builder_component(&This->query, &This->query_len, NULL, 0, ppwzQuery, pcchQuery);
5438 return get_builder_component(&This->query, &This->query_len, This->uri->canon_uri+This->uri->query_start,
5439 This->uri->query_len, ppwzQuery, pcchQuery);
5442 static HRESULT WINAPI UriBuilder_GetSchemeName(IUriBuilder *iface, DWORD *pcchSchemeName, LPCWSTR *ppwzSchemeName)
5444 UriBuilder *This = URIBUILDER_THIS(iface);
5445 TRACE("(%p)->(%p %p)\n", This, pcchSchemeName, ppwzSchemeName);
5447 if(!This->uri || This->uri->scheme_start == -1 || This->modified_props & Uri_HAS_SCHEME_NAME)
5448 return get_builder_component(&This->scheme, &This->scheme_len, NULL, 0, ppwzSchemeName, pcchSchemeName);
5450 return get_builder_component(&This->scheme, &This->scheme_len, This->uri->canon_uri+This->uri->scheme_start,
5451 This->uri->scheme_len, ppwzSchemeName, pcchSchemeName);
5454 static HRESULT WINAPI UriBuilder_GetUserName(IUriBuilder *iface, DWORD *pcchUserName, LPCWSTR *ppwzUserName)
5456 UriBuilder *This = URIBUILDER_THIS(iface);
5457 TRACE("(%p)->(%p %p)\n", This, pcchUserName, ppwzUserName);
5459 if(!This->uri || This->uri->userinfo_start == -1 || This->uri->userinfo_split == 0 ||
5460 This->modified_props & Uri_HAS_USER_NAME)
5461 return get_builder_component(&This->username, &This->username_len, NULL, 0, ppwzUserName, pcchUserName);
5463 const WCHAR *start = This->uri->canon_uri+This->uri->userinfo_start;
5465 /* Check if there's a password in the userinfo section. */
5466 if(This->uri->userinfo_split > -1)
5467 /* Don't include the password. */
5468 return get_builder_component(&This->username, &This->username_len, start,
5469 This->uri->userinfo_split, ppwzUserName, pcchUserName);
5471 return get_builder_component(&This->username, &This->username_len, start,
5472 This->uri->userinfo_len, ppwzUserName, pcchUserName);
5476 static HRESULT WINAPI UriBuilder_SetFragment(IUriBuilder *iface, LPCWSTR pwzNewValue)
5478 UriBuilder *This = URIBUILDER_THIS(iface);
5479 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
5480 return set_builder_component(&This->fragment, &This->fragment_len, pwzNewValue, '#',
5481 &This->modified_props, Uri_HAS_FRAGMENT);
5484 static HRESULT WINAPI UriBuilder_SetHost(IUriBuilder *iface, LPCWSTR pwzNewValue)
5486 UriBuilder *This = URIBUILDER_THIS(iface);
5487 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
5489 /* Host name can't be set to NULL. */
5491 return E_INVALIDARG;
5493 return set_builder_component(&This->host, &This->host_len, pwzNewValue, 0,
5494 &This->modified_props, Uri_HAS_HOST);
5497 static HRESULT WINAPI UriBuilder_SetPassword(IUriBuilder *iface, LPCWSTR pwzNewValue)
5499 UriBuilder *This = URIBUILDER_THIS(iface);
5500 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
5501 return set_builder_component(&This->password, &This->password_len, pwzNewValue, 0,
5502 &This->modified_props, Uri_HAS_PASSWORD);
5505 static HRESULT WINAPI UriBuilder_SetPath(IUriBuilder *iface, LPCWSTR pwzNewValue)
5507 UriBuilder *This = URIBUILDER_THIS(iface);
5508 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
5509 return set_builder_component(&This->path, &This->path_len, pwzNewValue, 0,
5510 &This->modified_props, Uri_HAS_PATH);
5513 static HRESULT WINAPI UriBuilder_SetPort(IUriBuilder *iface, BOOL fHasPort, DWORD dwNewValue)
5515 UriBuilder *This = URIBUILDER_THIS(iface);
5516 TRACE("(%p)->(%d %d)\n", This, fHasPort, dwNewValue);
5518 This->has_port = fHasPort;
5519 This->port = dwNewValue;
5520 This->modified_props |= Uri_HAS_PORT;
5524 static HRESULT WINAPI UriBuilder_SetQuery(IUriBuilder *iface, LPCWSTR pwzNewValue)
5526 UriBuilder *This = URIBUILDER_THIS(iface);
5527 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
5528 return set_builder_component(&This->query, &This->query_len, pwzNewValue, '?',
5529 &This->modified_props, Uri_HAS_QUERY);
5532 static HRESULT WINAPI UriBuilder_SetSchemeName(IUriBuilder *iface, LPCWSTR pwzNewValue)
5534 UriBuilder *This = URIBUILDER_THIS(iface);
5535 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
5537 /* Only set the scheme name if it's not NULL or empty. */
5538 if(!pwzNewValue || !*pwzNewValue)
5539 return E_INVALIDARG;
5541 return set_builder_component(&This->scheme, &This->scheme_len, pwzNewValue, 0,
5542 &This->modified_props, Uri_HAS_SCHEME_NAME);
5545 static HRESULT WINAPI UriBuilder_SetUserName(IUriBuilder *iface, LPCWSTR pwzNewValue)
5547 UriBuilder *This = URIBUILDER_THIS(iface);
5548 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
5549 return set_builder_component(&This->username, &This->username_len, pwzNewValue, 0,
5550 &This->modified_props, Uri_HAS_USER_NAME);
5553 static HRESULT WINAPI UriBuilder_RemoveProperties(IUriBuilder *iface, DWORD dwPropertyMask)
5555 const DWORD accepted_flags = Uri_HAS_AUTHORITY|Uri_HAS_DOMAIN|Uri_HAS_EXTENSION|Uri_HAS_FRAGMENT|Uri_HAS_HOST|
5556 Uri_HAS_PASSWORD|Uri_HAS_PATH|Uri_HAS_PATH_AND_QUERY|Uri_HAS_QUERY|
5557 Uri_HAS_USER_INFO|Uri_HAS_USER_NAME;
5559 UriBuilder *This = URIBUILDER_THIS(iface);
5560 TRACE("(%p)->(0x%08x)\n", This, dwPropertyMask);
5562 if(dwPropertyMask & ~accepted_flags)
5563 return E_INVALIDARG;
5565 if(dwPropertyMask & Uri_HAS_FRAGMENT)
5566 UriBuilder_SetFragment(iface, NULL);
5568 /* Even though you can't set the host name to NULL or an
5569 * empty string, you can still remove it... for some reason.
5571 if(dwPropertyMask & Uri_HAS_HOST)
5572 set_builder_component(&This->host, &This->host_len, NULL, 0,
5573 &This->modified_props, Uri_HAS_HOST);
5575 if(dwPropertyMask & Uri_HAS_PASSWORD)
5576 UriBuilder_SetPassword(iface, NULL);
5578 if(dwPropertyMask & Uri_HAS_PATH)
5579 UriBuilder_SetPath(iface, NULL);
5581 if(dwPropertyMask & Uri_HAS_PORT)
5582 UriBuilder_SetPort(iface, FALSE, 0);
5584 if(dwPropertyMask & Uri_HAS_QUERY)
5585 UriBuilder_SetQuery(iface, NULL);
5587 if(dwPropertyMask & Uri_HAS_USER_NAME)
5588 UriBuilder_SetUserName(iface, NULL);
5593 static HRESULT WINAPI UriBuilder_HasBeenModified(IUriBuilder *iface, BOOL *pfModified)
5595 UriBuilder *This = URIBUILDER_THIS(iface);
5596 TRACE("(%p)->(%p)\n", This, pfModified);
5601 *pfModified = This->modified_props > 0;
5605 #undef URIBUILDER_THIS
5607 static const IUriBuilderVtbl UriBuilderVtbl = {
5608 UriBuilder_QueryInterface,
5611 UriBuilder_CreateUriSimple,
5612 UriBuilder_CreateUri,
5613 UriBuilder_CreateUriWithFlags,
5616 UriBuilder_GetFragment,
5618 UriBuilder_GetPassword,
5621 UriBuilder_GetQuery,
5622 UriBuilder_GetSchemeName,
5623 UriBuilder_GetUserName,
5624 UriBuilder_SetFragment,
5626 UriBuilder_SetPassword,
5629 UriBuilder_SetQuery,
5630 UriBuilder_SetSchemeName,
5631 UriBuilder_SetUserName,
5632 UriBuilder_RemoveProperties,
5633 UriBuilder_HasBeenModified,
5636 /***********************************************************************
5637 * CreateIUriBuilder (urlmon.@)
5639 HRESULT WINAPI CreateIUriBuilder(IUri *pIUri, DWORD dwFlags, DWORD_PTR dwReserved, IUriBuilder **ppIUriBuilder)
5643 TRACE("(%p %x %x %p)\n", pIUri, dwFlags, (DWORD)dwReserved, ppIUriBuilder);
5648 ret = heap_alloc_zero(sizeof(UriBuilder));
5650 return E_OUTOFMEMORY;
5652 ret->lpIUriBuilderVtbl = &UriBuilderVtbl;
5658 if((uri = get_uri_obj(pIUri))) {
5663 /* Windows doesn't set 'has_port' to TRUE in this case. */
5664 ret->port = uri->port;
5668 *ppIUriBuilder = NULL;
5669 FIXME("(%p %x %x %p): Unknown IUri types not supported yet.\n", pIUri, dwFlags,
5670 (DWORD)dwReserved, ppIUriBuilder);
5675 *ppIUriBuilder = URIBUILDER(ret);
5679 /* Merges the base path with the relative path and stores the resulting path
5680 * and path len in 'result' and 'result_len'.
5682 static HRESULT merge_paths(parse_data *data, const WCHAR *base, DWORD base_len, const WCHAR *relative,
5683 DWORD relative_len, WCHAR **result, DWORD *result_len, DWORD flags)
5685 const WCHAR *end = NULL;
5686 DWORD base_copy_len = 0;
5690 /* Find the characters the will be copied over from
5693 end = str_last_of(base, base+(base_len-1), '/');
5694 if(!end && data->scheme_type == URL_SCHEME_FILE)
5695 /* Try looking for a '\\'. */
5696 end = str_last_of(base, base+(base_len-1), '\\');
5700 base_copy_len = (end+1)-base;
5701 *result = heap_alloc((base_copy_len+relative_len+1)*sizeof(WCHAR));
5703 *result = heap_alloc((relative_len+1)*sizeof(WCHAR));
5707 return E_OUTOFMEMORY;
5712 memcpy(ptr, base, base_copy_len*sizeof(WCHAR));
5713 ptr += base_copy_len;
5716 memcpy(ptr, relative, relative_len*sizeof(WCHAR));
5717 ptr += relative_len;
5720 *result_len = (ptr-*result);
5724 static HRESULT combine_uri(Uri *base, Uri *relative, DWORD flags, IUri **result, DWORD extras) {
5728 DWORD create_flags = 0, len = 0;
5730 memset(&data, 0, sizeof(parse_data));
5732 /* Base case is when the relative Uri has a scheme name,
5733 * if it does, then 'result' will contain the same data
5734 * as the relative Uri.
5736 if(relative->scheme_start > -1) {
5737 data.uri = SysAllocString(relative->raw_uri);
5740 return E_OUTOFMEMORY;
5743 parse_uri(&data, 0);
5745 ret = create_uri_obj();
5748 return E_OUTOFMEMORY;
5751 if(extras & COMBINE_URI_FORCE_FLAG_USE) {
5752 if(flags & URL_DONT_SIMPLIFY)
5753 create_flags |= Uri_CREATE_NO_CANONICALIZE;
5754 if(flags & URL_DONT_UNESCAPE_EXTRA_INFO)
5755 create_flags |= Uri_CREATE_NO_DECODE_EXTRA_INFO;
5758 ret->raw_uri = data.uri;
5759 hr = canonicalize_uri(&data, ret, create_flags);
5761 IUri_Release(URI(ret));
5766 apply_default_flags(&create_flags);
5767 ret->create_flags = create_flags;
5772 DWORD raw_flags = 0;
5774 if(base->scheme_start > -1) {
5775 data.scheme = base->canon_uri+base->scheme_start;
5776 data.scheme_len = base->scheme_len;
5777 data.scheme_type = base->scheme_type;
5779 data.is_relative = TRUE;
5780 data.scheme_type = URL_SCHEME_UNKNOWN;
5781 create_flags |= Uri_CREATE_ALLOW_RELATIVE;
5784 if(base->authority_start > -1) {
5785 if(base->userinfo_start > -1 && base->userinfo_split != 0) {
5786 data.username = base->canon_uri+base->userinfo_start;
5787 data.username_len = (base->userinfo_split > -1) ? base->userinfo_split : base->userinfo_len;
5790 if(base->userinfo_split > -1) {
5791 data.password = base->canon_uri+base->userinfo_start+base->userinfo_split+1;
5792 data.password_len = base->userinfo_len-base->userinfo_split-1;
5795 if(base->host_start > -1) {
5796 data.host = base->canon_uri+base->host_start;
5797 data.host_len = base->host_len;
5798 data.host_type = base->host_type;
5801 if(base->has_port) {
5802 data.has_port = TRUE;
5803 data.port_value = base->port;
5805 } else if(base->scheme_type != URL_SCHEME_FILE)
5806 data.is_opaque = TRUE;
5808 if(relative->path_start == -1 || !relative->path_len) {
5809 if(base->path_start > -1) {
5810 data.path = base->canon_uri+base->path_start;
5811 data.path_len = base->path_len;
5812 } else if((base->path_start == -1 || !base->path_len) && !data.is_opaque) {
5813 /* Just set the path as a '/' if the base didn't have
5814 * one and if it's an hierarchical URI.
5816 static const WCHAR slashW[] = {'/',0};
5821 if(relative->query_start > -1) {
5822 data.query = relative->canon_uri+relative->query_start;
5823 data.query_len = relative->query_len;
5824 } else if(base->query_start > -1) {
5825 data.query = base->canon_uri+base->query_start;
5826 data.query_len = base->query_len;
5829 const WCHAR *ptr, **pptr;
5830 DWORD path_offset = 0, path_len = 0;
5832 /* There's two possibilities on what will happen to the path component
5833 * of the result IUri. First, if the relative path begins with a '/'
5834 * then the resulting path will just be the relative path. Second, if
5835 * relative path doesn't begin with a '/' then the base path and relative
5836 * path are merged together.
5838 if(relative->path_len && *(relative->canon_uri+relative->path_start) == '/') {
5840 BOOL copy_drive_path = FALSE;
5842 /* If the relative IUri's path starts with a '/', then we
5843 * don't use the base IUri's path. Unless the base IUri
5844 * is a file URI, in which case it uses the drive path of
5845 * the base IUri (if it has any) in the new path.
5847 if(base->scheme_type == URL_SCHEME_FILE) {
5848 if(base->path_len > 3 && *(base->canon_uri+base->path_start) == '/' &&
5849 is_drive_path(base->canon_uri+base->path_start+1)) {
5851 copy_drive_path = TRUE;
5855 path_len += relative->path_len;
5857 path = heap_alloc((path_len+1)*sizeof(WCHAR));
5860 return E_OUTOFMEMORY;
5865 /* Copy the base paths, drive path over. */
5866 if(copy_drive_path) {
5867 memcpy(tmp, base->canon_uri+base->path_start, 3*sizeof(WCHAR));
5871 memcpy(tmp, relative->canon_uri+relative->path_start, relative->path_len*sizeof(WCHAR));
5872 path[path_len] = '\0';
5874 /* Merge the base path with the relative path. */
5875 hr = merge_paths(&data, base->canon_uri+base->path_start, base->path_len,
5876 relative->canon_uri+relative->path_start, relative->path_len,
5877 &path, &path_len, flags);
5883 /* If the resulting IUri is a file URI, the drive path isn't
5884 * reduced out when the dot segments are removed.
5886 if(path_len >= 3 && data.scheme_type == URL_SCHEME_FILE && !data.host) {
5887 if(*path == '/' && is_drive_path(path+1))
5889 else if(is_drive_path(path))
5894 /* Check if the dot segments need to be removed from the path. */
5895 if(!(flags & URL_DONT_SIMPLIFY) && !data.is_opaque) {
5896 DWORD offset = (path_offset > 0) ? path_offset+1 : 0;
5897 DWORD new_len = remove_dot_segments(path+offset,path_len-offset);
5899 if(new_len != path_len) {
5900 WCHAR *tmp = heap_realloc(path, (path_offset+new_len+1)*sizeof(WCHAR));
5904 return E_OUTOFMEMORY;
5907 tmp[new_len+offset] = '\0';
5909 path_len = new_len+offset;
5913 /* Make sure the path component is valid. */
5916 if((data.is_opaque && !parse_path_opaque(pptr, &data, 0)) ||
5917 (!data.is_opaque && !parse_path_hierarchical(pptr, &data, 0))) {
5920 return E_INVALIDARG;
5924 if(relative->fragment_start > -1) {
5925 data.fragment = relative->canon_uri+relative->fragment_start;
5926 data.fragment_len = relative->fragment_len;
5929 if(flags & URL_DONT_SIMPLIFY)
5930 raw_flags |= RAW_URI_FORCE_PORT_DISP;
5931 if(flags & URL_FILE_USE_PATHURL)
5932 raw_flags |= RAW_URI_CONVERT_TO_DOS_PATH;
5934 len = generate_raw_uri(&data, data.uri, raw_flags);
5935 data.uri = SysAllocStringLen(NULL, len);
5939 return E_OUTOFMEMORY;
5942 generate_raw_uri(&data, data.uri, raw_flags);
5944 ret = create_uri_obj();
5946 SysFreeString(data.uri);
5949 return E_OUTOFMEMORY;
5952 if(flags & URL_DONT_SIMPLIFY)
5953 create_flags |= Uri_CREATE_NO_CANONICALIZE;
5954 if(flags & URL_FILE_USE_PATHURL)
5955 create_flags |= Uri_CREATE_FILE_USE_DOS_PATH;
5957 ret->raw_uri = data.uri;
5958 hr = canonicalize_uri(&data, ret, create_flags);
5960 IUri_Release(URI(ret));
5965 if(flags & URL_DONT_SIMPLIFY)
5966 ret->display_modifiers |= URI_DISPLAY_NO_DEFAULT_PORT_AUTH;
5968 apply_default_flags(&create_flags);
5969 ret->create_flags = create_flags;
5978 /***********************************************************************
5979 * CoInternetCombineIUri (urlmon.@)
5981 HRESULT WINAPI CoInternetCombineIUri(IUri *pBaseUri, IUri *pRelativeUri, DWORD dwCombineFlags,
5982 IUri **ppCombinedUri, DWORD_PTR dwReserved)
5985 IInternetProtocolInfo *info;
5986 Uri *relative, *base;
5987 TRACE("(%p %p %x %p %x)\n", pBaseUri, pRelativeUri, dwCombineFlags, ppCombinedUri, (DWORD)dwReserved);
5990 return E_INVALIDARG;
5992 if(!pBaseUri || !pRelativeUri) {
5993 *ppCombinedUri = NULL;
5994 return E_INVALIDARG;
5997 relative = get_uri_obj(pRelativeUri);
5998 base = get_uri_obj(pBaseUri);
5999 if(!relative || !base) {
6000 *ppCombinedUri = NULL;
6001 FIXME("(%p %p %x %p %x) Unknown IUri types not supported yet.\n",
6002 pBaseUri, pRelativeUri, dwCombineFlags, ppCombinedUri, (DWORD)dwReserved);
6006 info = get_protocol_info(base->canon_uri);
6008 WCHAR result[INTERNET_MAX_URL_LENGTH+1];
6009 DWORD result_len = 0;
6011 hr = IInternetProtocolInfo_CombineUrl(info, base->canon_uri, relative->canon_uri, dwCombineFlags,
6012 result, INTERNET_MAX_URL_LENGTH+1, &result_len, 0);
6013 IInternetProtocolInfo_Release(info);
6015 hr = CreateUri(result, Uri_CREATE_ALLOW_RELATIVE, 0, ppCombinedUri);
6021 return combine_uri(base, relative, dwCombineFlags, ppCombinedUri, 0);
6024 /***********************************************************************
6025 * CoInternetCombineUrlEx (urlmon.@)
6027 HRESULT WINAPI CoInternetCombineUrlEx(IUri *pBaseUri, LPCWSTR pwzRelativeUrl, DWORD dwCombineFlags,
6028 IUri **ppCombinedUri, DWORD_PTR dwReserved)
6033 IInternetProtocolInfo *info;
6035 TRACE("(%p %s %x %p %x) stub\n", pBaseUri, debugstr_w(pwzRelativeUrl), dwCombineFlags,
6036 ppCombinedUri, (DWORD)dwReserved);
6041 if(!pwzRelativeUrl) {
6042 *ppCombinedUri = NULL;
6043 return E_UNEXPECTED;
6047 *ppCombinedUri = NULL;
6048 return E_INVALIDARG;
6051 base = get_uri_obj(pBaseUri);
6053 *ppCombinedUri = NULL;
6054 FIXME("(%p %s %x %p %x) Unknown IUri's not supported yet.\n", pBaseUri, debugstr_w(pwzRelativeUrl),
6055 dwCombineFlags, ppCombinedUri, (DWORD)dwReserved);
6059 info = get_protocol_info(base->canon_uri);
6061 WCHAR result[INTERNET_MAX_URL_LENGTH+1];
6062 DWORD result_len = 0;
6064 hr = IInternetProtocolInfo_CombineUrl(info, base->canon_uri, pwzRelativeUrl, dwCombineFlags,
6065 result, INTERNET_MAX_URL_LENGTH+1, &result_len, 0);
6066 IInternetProtocolInfo_Release(info);
6068 hr = CreateUri(result, Uri_CREATE_ALLOW_RELATIVE, 0, ppCombinedUri);
6074 hr = CreateUri(pwzRelativeUrl, Uri_CREATE_ALLOW_RELATIVE, 0, &relative);
6076 *ppCombinedUri = NULL;
6080 hr = combine_uri(base, get_uri_obj(relative), dwCombineFlags, ppCombinedUri, COMBINE_URI_FORCE_FLAG_USE);
6082 IUri_Release(relative);
6086 static HRESULT parse_canonicalize(const Uri *uri, DWORD flags, LPWSTR output,
6087 DWORD output_len, DWORD *result_len)
6089 const WCHAR *ptr = NULL;
6092 WCHAR buffer[INTERNET_MAX_URL_LENGTH+1];
6096 /* URL_UNESCAPE only has effect if none of the URL_ESCAPE flags are set. */
6097 const BOOL allow_unescape = !(flags & URL_ESCAPE_UNSAFE) &&
6098 !(flags & URL_ESCAPE_SPACES_ONLY) &&
6099 !(flags & URL_ESCAPE_PERCENT);
6101 /* Check if the dot segments need to be removed from the
6104 if(uri->scheme_start > -1 && uri->path_start > -1) {
6105 ptr = uri->canon_uri+uri->scheme_start+uri->scheme_len+1;
6108 reduce_path = !(flags & URL_NO_META) &&
6109 !(flags & URL_DONT_SIMPLIFY) &&
6110 ptr && check_hierarchical(pptr);
6112 for(ptr = uri->canon_uri; ptr < uri->canon_uri+uri->canon_len; ++ptr) {
6113 BOOL do_default_action = TRUE;
6115 /* Keep track of the path if we need to remove dot segments from
6118 if(reduce_path && !path && ptr == uri->canon_uri+uri->path_start)
6121 /* Check if it's time to reduce the path. */
6122 if(reduce_path && ptr == uri->canon_uri+uri->path_start+uri->path_len) {
6123 DWORD current_path_len = (buffer+len) - path;
6124 DWORD new_path_len = remove_dot_segments(path, current_path_len);
6126 /* Update the current length. */
6127 len -= (current_path_len-new_path_len);
6128 reduce_path = FALSE;
6132 const WCHAR decoded = decode_pct_val(ptr);
6134 if(allow_unescape && (flags & URL_UNESCAPE)) {
6135 buffer[len++] = decoded;
6137 do_default_action = FALSE;
6141 /* See if %'s needed to encoded. */
6142 if(do_default_action && (flags & URL_ESCAPE_PERCENT)) {
6143 pct_encode_val(*ptr, buffer+len);
6145 do_default_action = FALSE;
6147 } else if(*ptr == ' ') {
6148 if((flags & URL_ESCAPE_SPACES_ONLY) &&
6149 !(flags & URL_ESCAPE_UNSAFE)) {
6150 pct_encode_val(*ptr, buffer+len);
6152 do_default_action = FALSE;
6154 } else if(!is_reserved(*ptr) && !is_unreserved(*ptr)) {
6155 if(flags & URL_ESCAPE_UNSAFE) {
6156 pct_encode_val(*ptr, buffer+len);
6158 do_default_action = FALSE;
6162 if(do_default_action)
6163 buffer[len++] = *ptr;
6166 /* Sometimes the path is the very last component of the IUri, so
6167 * see if the dot segments need to be reduced now.
6169 if(reduce_path && path) {
6170 DWORD current_path_len = (buffer+len) - path;
6171 DWORD new_path_len = remove_dot_segments(path, current_path_len);
6173 /* Update the current length. */
6174 len -= (current_path_len-new_path_len);
6179 /* The null terminator isn't included the length. */
6180 *result_len = len-1;
6181 if(len > output_len)
6182 return STRSAFE_E_INSUFFICIENT_BUFFER;
6184 memcpy(output, buffer, len*sizeof(WCHAR));
6189 static HRESULT parse_friendly(IUri *uri, LPWSTR output, DWORD output_len,
6196 hr = IUri_GetPropertyLength(uri, Uri_PROPERTY_DISPLAY_URI, &display_len, 0);
6202 *result_len = display_len;
6203 if(display_len+1 > output_len)
6204 return STRSAFE_E_INSUFFICIENT_BUFFER;
6206 hr = IUri_GetDisplayUri(uri, &display);
6212 memcpy(output, display, (display_len+1)*sizeof(WCHAR));
6213 SysFreeString(display);
6217 static HRESULT parse_rootdocument(const Uri *uri, LPWSTR output, DWORD output_len,
6220 static const WCHAR colon_slashesW[] = {':','/','/'};
6225 /* Windows only returns the root document if the URI has an authority
6226 * and it's not an unknown scheme type or a file scheme type.
6228 if(uri->authority_start == -1 ||
6229 uri->scheme_type == URL_SCHEME_UNKNOWN ||
6230 uri->scheme_type == URL_SCHEME_FILE) {
6233 return STRSAFE_E_INSUFFICIENT_BUFFER;
6239 len = uri->scheme_len+uri->authority_len;
6240 /* For the "://" and '/' which will be added. */
6243 if(len+1 > output_len) {
6245 return STRSAFE_E_INSUFFICIENT_BUFFER;
6249 memcpy(ptr, uri->canon_uri+uri->scheme_start, uri->scheme_len*sizeof(WCHAR));
6251 /* Add the "://". */
6252 ptr += uri->scheme_len;
6253 memcpy(ptr, colon_slashesW, sizeof(colon_slashesW));
6255 /* Add the authority. */
6256 ptr += sizeof(colon_slashesW)/sizeof(WCHAR);
6257 memcpy(ptr, uri->canon_uri+uri->authority_start, uri->authority_len*sizeof(WCHAR));
6259 /* Add the '/' after the authority. */
6260 ptr += uri->authority_len;
6268 static HRESULT parse_document(const Uri *uri, LPWSTR output, DWORD output_len,
6273 /* It has to be a known scheme type, but, it can't be a file
6274 * scheme. It also has to hierarchical.
6276 if(uri->scheme_type == URL_SCHEME_UNKNOWN ||
6277 uri->scheme_type == URL_SCHEME_FILE ||
6278 uri->authority_start == -1) {
6281 return STRSAFE_E_INSUFFICIENT_BUFFER;
6287 if(uri->fragment_start > -1)
6288 len = uri->fragment_start;
6290 len = uri->canon_len;
6293 if(len+1 > output_len)
6294 return STRSAFE_E_INSUFFICIENT_BUFFER;
6296 memcpy(output, uri->canon_uri, len*sizeof(WCHAR));
6301 static HRESULT parse_path_from_url(const Uri *uri, LPWSTR output, DWORD output_len,
6304 const WCHAR *path_ptr;
6305 WCHAR buffer[INTERNET_MAX_URL_LENGTH+1];
6308 if(uri->scheme_type != URL_SCHEME_FILE) {
6312 return E_INVALIDARG;
6316 if(uri->host_start > -1) {
6317 static const WCHAR slash_slashW[] = {'\\','\\'};
6319 memcpy(ptr, slash_slashW, sizeof(slash_slashW));
6320 ptr += sizeof(slash_slashW)/sizeof(WCHAR);
6321 memcpy(ptr, uri->canon_uri+uri->host_start, uri->host_len*sizeof(WCHAR));
6322 ptr += uri->host_len;
6325 path_ptr = uri->canon_uri+uri->path_start;
6326 if(uri->path_len > 3 && *path_ptr == '/' && is_drive_path(path_ptr+1))
6327 /* Skip past the '/' in front of the drive path. */
6330 for(; path_ptr < uri->canon_uri+uri->path_start+uri->path_len; ++path_ptr, ++ptr) {
6331 BOOL do_default_action = TRUE;
6333 if(*path_ptr == '%') {
6334 const WCHAR decoded = decode_pct_val(path_ptr);
6338 do_default_action = FALSE;
6340 } else if(*path_ptr == '/') {
6342 do_default_action = FALSE;
6345 if(do_default_action)
6351 *result_len = ptr-buffer;
6352 if(*result_len+1 > output_len)
6353 return STRSAFE_E_INSUFFICIENT_BUFFER;
6355 memcpy(output, buffer, (*result_len+1)*sizeof(WCHAR));
6359 static HRESULT parse_url_from_path(IUri *uri, LPWSTR output, DWORD output_len,
6366 hr = IUri_GetPropertyLength(uri, Uri_PROPERTY_ABSOLUTE_URI, &len, 0);
6373 if(len+1 > output_len)
6374 return STRSAFE_E_INSUFFICIENT_BUFFER;
6376 hr = IUri_GetAbsoluteUri(uri, &received);
6382 memcpy(output, received, (len+1)*sizeof(WCHAR));
6383 SysFreeString(received);
6388 static HRESULT parse_schema(IUri *uri, LPWSTR output, DWORD output_len,
6395 hr = IUri_GetPropertyLength(uri, Uri_PROPERTY_SCHEME_NAME, &len, 0);
6402 if(len+1 > output_len)
6403 return STRSAFE_E_INSUFFICIENT_BUFFER;
6405 hr = IUri_GetSchemeName(uri, &received);
6411 memcpy(output, received, (len+1)*sizeof(WCHAR));
6412 SysFreeString(received);
6417 static HRESULT parse_site(IUri *uri, LPWSTR output, DWORD output_len, DWORD *result_len)
6423 hr = IUri_GetPropertyLength(uri, Uri_PROPERTY_HOST, &len, 0);
6430 if(len+1 > output_len)
6431 return STRSAFE_E_INSUFFICIENT_BUFFER;
6433 hr = IUri_GetHost(uri, &received);
6439 memcpy(output, received, (len+1)*sizeof(WCHAR));
6440 SysFreeString(received);
6445 static HRESULT parse_domain(IUri *uri, LPWSTR output, DWORD output_len, DWORD *result_len)
6451 hr = IUri_GetPropertyLength(uri, Uri_PROPERTY_DOMAIN, &len, 0);
6458 if(len+1 > output_len)
6459 return STRSAFE_E_INSUFFICIENT_BUFFER;
6461 hr = IUri_GetDomain(uri, &received);
6467 memcpy(output, received, (len+1)*sizeof(WCHAR));
6468 SysFreeString(received);
6473 /***********************************************************************
6474 * CoInternetParseIUri (urlmon.@)
6476 HRESULT WINAPI CoInternetParseIUri(IUri *pIUri, PARSEACTION ParseAction, DWORD dwFlags,
6477 LPWSTR pwzResult, DWORD cchResult, DWORD *pcchResult,
6478 DWORD_PTR dwReserved)
6483 TRACE("(%p %d %x %p %d %p %x)\n", pIUri, ParseAction, dwFlags, pwzResult,
6484 cchResult, pcchResult, (DWORD)dwReserved);
6489 if(!pwzResult || !pIUri) {
6491 return E_INVALIDARG;
6494 switch(ParseAction) {
6495 case PARSE_CANONICALIZE:
6496 if(!(uri = get_uri_obj(pIUri))) {
6498 FIXME("(%p %d %x %p %d %p %x) Unknown IUri's not supported for this action.\n",
6499 pIUri, ParseAction, dwFlags, pwzResult, cchResult, pcchResult, (DWORD)dwReserved);
6502 hr = parse_canonicalize(uri, dwFlags, pwzResult, cchResult, pcchResult);
6504 case PARSE_FRIENDLY:
6505 hr = parse_friendly(pIUri, pwzResult, cchResult, pcchResult);
6507 case PARSE_ROOTDOCUMENT:
6508 if(!(uri = get_uri_obj(pIUri))) {
6510 FIXME("(%p %d %x %p %d %p %x) Unknown IUri's not supported for this action.\n",
6511 pIUri, ParseAction, dwFlags, pwzResult, cchResult, pcchResult, (DWORD)dwReserved);
6514 hr = parse_rootdocument(uri, pwzResult, cchResult, pcchResult);
6516 case PARSE_DOCUMENT:
6517 if(!(uri = get_uri_obj(pIUri))) {
6519 FIXME("(%p %d %x %p %d %p %x) Unknown IUri's not supported for this action.\n",
6520 pIUri, ParseAction, dwFlags, pwzResult, cchResult, pcchResult, (DWORD)dwReserved);
6523 hr = parse_document(uri, pwzResult, cchResult, pcchResult);
6525 case PARSE_PATH_FROM_URL:
6526 if(!(uri = get_uri_obj(pIUri))) {
6528 FIXME("(%p %d %x %p %d %p %x) Unknown IUri's not supported for this action.\n",
6529 pIUri, ParseAction, dwFlags, pwzResult, cchResult, pcchResult, (DWORD)dwReserved);
6532 hr = parse_path_from_url(uri, pwzResult, cchResult, pcchResult);
6534 case PARSE_URL_FROM_PATH:
6535 hr = parse_url_from_path(pIUri, pwzResult, cchResult, pcchResult);
6538 hr = parse_schema(pIUri, pwzResult, cchResult, pcchResult);
6541 hr = parse_site(pIUri, pwzResult, cchResult, pcchResult);
6544 hr = parse_domain(pIUri, pwzResult, cchResult, pcchResult);
6546 case PARSE_SECURITY_URL:
6549 case PARSE_SECURITY_DOMAIN:
6556 FIXME("(%p %d %x %p %d %p %x) Partial stub.\n", pIUri, ParseAction, dwFlags,
6557 pwzResult, cchResult, pcchResult, (DWORD)dwReserved);