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
23 #include "urlmon_main.h"
24 #include "wine/debug.h"
26 #define NO_SHLWAPI_REG
33 #include "ip2string.h"
35 #define URI_DISPLAY_NO_ABSOLUTE_URI 0x1
36 #define URI_DISPLAY_NO_DEFAULT_PORT_AUTH 0x2
38 #define ALLOW_NULL_TERM_SCHEME 0x01
39 #define ALLOW_NULL_TERM_USER_NAME 0x02
40 #define ALLOW_NULL_TERM_PASSWORD 0x04
41 #define ALLOW_BRACKETLESS_IP_LITERAL 0x08
42 #define SKIP_IP_FUTURE_CHECK 0x10
43 #define IGNORE_PORT_DELIMITER 0x20
45 #define RAW_URI_FORCE_PORT_DISP 0x1
46 #define RAW_URI_CONVERT_TO_DOS_PATH 0x2
48 #define COMBINE_URI_FORCE_FLAG_USE 0x1
50 WINE_DEFAULT_DEBUG_CHANNEL(urlmon
);
52 static const IID IID_IUriObj
= {0x4b364760,0x9f51,0x11df,{0x98,0x1c,0x08,0x00,0x20,0x0c,0x9a,0x66}};
56 IUriBuilderFactory IUriBuilderFactory_iface
;
57 IPersistStream IPersistStream_iface
;
58 IMarshal IMarshal_iface
;
64 /* Information about the canonicalized URI's buffer. */
68 BOOL display_modifiers
;
73 URL_SCHEME scheme_type
;
81 Uri_HOST_TYPE host_type
;
104 IUriBuilder IUriBuilder_iface
;
108 DWORD modified_props
;
140 BOOL has_implicit_scheme
;
141 BOOL has_implicit_ip
;
147 URL_SCHEME scheme_type
;
149 const WCHAR
*username
;
152 const WCHAR
*password
;
157 Uri_HOST_TYPE host_type
;
159 IN6_ADDR ipv6_address
;
172 const WCHAR
*fragment
;
176 static const CHAR hexDigits
[] = "0123456789ABCDEF";
178 /* List of scheme types/scheme names that are recognized by the IUri interface as of IE 7. */
179 static const struct {
181 WCHAR scheme_name
[16];
182 } recognized_schemes
[] = {
183 {URL_SCHEME_FTP
, L
"ftp"},
184 {URL_SCHEME_HTTP
, L
"http"},
185 {URL_SCHEME_GOPHER
, L
"gopher"},
186 {URL_SCHEME_MAILTO
, L
"mailto"},
187 {URL_SCHEME_NEWS
, L
"news"},
188 {URL_SCHEME_NNTP
, L
"nntp"},
189 {URL_SCHEME_TELNET
, L
"telnet"},
190 {URL_SCHEME_WAIS
, L
"wais"},
191 {URL_SCHEME_FILE
, L
"file"},
192 {URL_SCHEME_MK
, L
"mk"},
193 {URL_SCHEME_HTTPS
, L
"https"},
194 {URL_SCHEME_SHELL
, L
"shell"},
195 {URL_SCHEME_SNEWS
, L
"snews"},
196 {URL_SCHEME_LOCAL
, L
"local"},
197 {URL_SCHEME_JAVASCRIPT
, L
"javascript"},
198 {URL_SCHEME_VBSCRIPT
, L
"vbscript"},
199 {URL_SCHEME_ABOUT
, L
"about"},
200 {URL_SCHEME_RES
, L
"res"},
201 {URL_SCHEME_MSSHELLROOTED
, L
"ms-shell-rooted"},
202 {URL_SCHEME_MSSHELLIDLIST
, L
"ms-shell-idlist"},
203 {URL_SCHEME_MSHELP
, L
"hcp"},
204 {URL_SCHEME_WILDCARD
, L
"*"}
207 /* List of default ports Windows recognizes. */
208 static const struct {
211 } default_ports
[] = {
212 {URL_SCHEME_FTP
, 21},
213 {URL_SCHEME_HTTP
, 80},
214 {URL_SCHEME_GOPHER
, 70},
215 {URL_SCHEME_NNTP
, 119},
216 {URL_SCHEME_TELNET
, 23},
217 {URL_SCHEME_WAIS
, 210},
218 {URL_SCHEME_HTTPS
, 443},
221 /* List of 3-character top level domain names Windows seems to recognize.
222 * There might be more, but, these are the only ones I've found so far.
224 static const struct {
226 } recognized_tlds
[] = {
236 static Uri
*get_uri_obj(IUri
*uri
)
241 hres
= IUri_QueryInterface(uri
, &IID_IUriObj
, (void**)&ret
);
242 return SUCCEEDED(hres
) ? ret
: NULL
;
245 static inline BOOL
is_alpha(WCHAR val
) {
246 return ((val
>= 'a' && val
<= 'z') || (val
>= 'A' && val
<= 'Z'));
249 static inline BOOL
is_num(WCHAR val
) {
250 return (val
>= '0' && val
<= '9');
253 static inline BOOL
is_drive_path(const WCHAR
*str
) {
254 return (is_alpha(str
[0]) && (str
[1] == ':' || str
[1] == '|'));
257 static inline BOOL
is_unc_path(const WCHAR
*str
) {
258 return (str
[0] == '\\' && str
[1] == '\\');
261 static inline BOOL
is_forbidden_dos_path_char(WCHAR val
) {
262 return (val
== '>' || val
== '<' || val
== '\"');
265 /* A URI is implicitly a file path if it begins with
266 * a drive letter (e.g. X:) or starts with "\\" (UNC path).
268 static inline BOOL
is_implicit_file_path(const WCHAR
*str
) {
269 return (is_unc_path(str
) || (is_alpha(str
[0]) && str
[1] == ':'));
272 /* Checks if the URI is a hierarchical URI. A hierarchical
273 * URI is one that has "//" after the scheme.
275 static BOOL
check_hierarchical(const WCHAR
**ptr
) {
276 const WCHAR
*start
= *ptr
;
291 /* unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~" */
292 static inline BOOL
is_unreserved(WCHAR val
) {
293 return (is_alpha(val
) || is_num(val
) || val
== '-' || val
== '.' ||
294 val
== '_' || val
== '~');
297 /* sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
298 * / "*" / "+" / "," / ";" / "="
300 static inline BOOL
is_subdelim(WCHAR val
) {
301 return (val
== '!' || val
== '$' || val
== '&' ||
302 val
== '\'' || val
== '(' || val
== ')' ||
303 val
== '*' || val
== '+' || val
== ',' ||
304 val
== ';' || val
== '=');
307 /* gen-delims = ":" / "/" / "?" / "#" / "[" / "]" / "@" */
308 static inline BOOL
is_gendelim(WCHAR val
) {
309 return (val
== ':' || val
== '/' || val
== '?' ||
310 val
== '#' || val
== '[' || val
== ']' ||
314 /* Characters that delimit the end of the authority
315 * section of a URI. Sometimes a '\\' is considered
316 * an authority delimiter.
318 static inline BOOL
is_auth_delim(WCHAR val
, BOOL acceptSlash
) {
319 return (val
== '#' || val
== '/' || val
== '?' ||
320 val
== '\0' || (acceptSlash
&& val
== '\\'));
323 /* reserved = gen-delims / sub-delims */
324 static inline BOOL
is_reserved(WCHAR val
) {
325 return (is_subdelim(val
) || is_gendelim(val
));
328 static inline BOOL
is_hexdigit(WCHAR val
) {
329 return ((val
>= 'a' && val
<= 'f') ||
330 (val
>= 'A' && val
<= 'F') ||
331 (val
>= '0' && val
<= '9'));
334 static inline BOOL
is_path_delim(URL_SCHEME scheme
, WCHAR val
) {
335 return (!val
|| (val
== '#' && scheme
!= URL_SCHEME_FILE
) || val
== '?');
338 static inline BOOL
is_slash(WCHAR c
)
340 return c
== '/' || c
== '\\';
343 static inline BOOL
is_ascii(WCHAR c
)
348 static BOOL
is_default_port(URL_SCHEME scheme
, DWORD port
) {
351 for(i
= 0; i
< ARRAY_SIZE(default_ports
); ++i
) {
352 if(default_ports
[i
].scheme
== scheme
&& default_ports
[i
].port
)
359 /* List of schemes types Windows seems to expect to be hierarchical. */
360 static inline BOOL
is_hierarchical_scheme(URL_SCHEME type
) {
361 return(type
== URL_SCHEME_HTTP
|| type
== URL_SCHEME_FTP
||
362 type
== URL_SCHEME_GOPHER
|| type
== URL_SCHEME_NNTP
||
363 type
== URL_SCHEME_TELNET
|| type
== URL_SCHEME_WAIS
||
364 type
== URL_SCHEME_FILE
|| type
== URL_SCHEME_HTTPS
||
365 type
== URL_SCHEME_RES
);
368 /* Checks if 'flags' contains an invalid combination of Uri_CREATE flags. */
369 static inline BOOL
has_invalid_flag_combination(DWORD flags
) {
370 return((flags
& Uri_CREATE_DECODE_EXTRA_INFO
&& flags
& Uri_CREATE_NO_DECODE_EXTRA_INFO
) ||
371 (flags
& Uri_CREATE_CANONICALIZE
&& flags
& Uri_CREATE_NO_CANONICALIZE
) ||
372 (flags
& Uri_CREATE_CRACK_UNKNOWN_SCHEMES
&& flags
& Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES
) ||
373 (flags
& Uri_CREATE_PRE_PROCESS_HTML_URI
&& flags
& Uri_CREATE_NO_PRE_PROCESS_HTML_URI
) ||
374 (flags
& Uri_CREATE_IE_SETTINGS
&& flags
& Uri_CREATE_NO_IE_SETTINGS
));
377 /* Applies each default Uri_CREATE flags to 'flags' if it
378 * doesn't cause a flag conflict.
380 static void apply_default_flags(DWORD
*flags
) {
381 if(!(*flags
& Uri_CREATE_NO_CANONICALIZE
))
382 *flags
|= Uri_CREATE_CANONICALIZE
;
383 if(!(*flags
& Uri_CREATE_NO_DECODE_EXTRA_INFO
))
384 *flags
|= Uri_CREATE_DECODE_EXTRA_INFO
;
385 if(!(*flags
& Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES
))
386 *flags
|= Uri_CREATE_CRACK_UNKNOWN_SCHEMES
;
387 if(!(*flags
& Uri_CREATE_NO_PRE_PROCESS_HTML_URI
))
388 *flags
|= Uri_CREATE_PRE_PROCESS_HTML_URI
;
389 if(!(*flags
& Uri_CREATE_IE_SETTINGS
))
390 *flags
|= Uri_CREATE_NO_IE_SETTINGS
;
393 /* Determines if the URI is hierarchical using the information already parsed into
394 * data and using the current location of parsing in the URI string.
396 * Windows considers a URI hierarchical if one of the following is true:
397 * A.) It's a wildcard scheme.
398 * B.) It's an implicit file scheme.
399 * C.) It's a known hierarchical scheme and it has two '\\' after the scheme name.
400 * (the '\\' will be converted into "//" during canonicalization).
401 * D.) "//" appears after the scheme name (or at the beginning if no scheme is given).
403 static inline BOOL
is_hierarchical_uri(const WCHAR
**ptr
, const parse_data
*data
) {
404 const WCHAR
*start
= *ptr
;
406 if(data
->scheme_type
== URL_SCHEME_WILDCARD
)
408 else if(data
->scheme_type
== URL_SCHEME_FILE
&& data
->has_implicit_scheme
)
410 else if(is_hierarchical_scheme(data
->scheme_type
) && (*ptr
)[0] == '\\' && (*ptr
)[1] == '\\') {
413 } else if(data
->scheme_type
!= URL_SCHEME_MAILTO
&& check_hierarchical(ptr
))
420 /* Taken from dlls/jscript/lex.c */
421 static int hex_to_int(WCHAR val
) {
422 if(val
>= '0' && val
<= '9')
424 else if(val
>= 'a' && val
<= 'f')
425 return val
- 'a' + 10;
426 else if(val
>= 'A' && val
<= 'F')
427 return val
- 'A' + 10;
432 /* Helper function for converting a percent encoded string
433 * representation of a WCHAR value into its actual WCHAR value. If
434 * the two characters following the '%' aren't valid hex values then
435 * this function returns the NULL character.
438 * "%2E" will result in '.' being returned by this function.
440 static WCHAR
decode_pct_val(const WCHAR
*ptr
) {
443 if(*ptr
== '%' && is_hexdigit(*(ptr
+ 1)) && is_hexdigit(*(ptr
+ 2))) {
444 INT a
= hex_to_int(*(ptr
+ 1));
445 INT b
= hex_to_int(*(ptr
+ 2));
454 /* Helper function for percent encoding a given character
455 * and storing the encoded value into a given buffer (dest).
457 * It's up to the calling function to ensure that there is
458 * at least enough space in 'dest' for the percent encoded
459 * value to be stored (so dest + 3 spaces available).
461 static inline void pct_encode_val(WCHAR val
, WCHAR
*dest
) {
463 dest
[1] = hexDigits
[(val
>> 4) & 0xf];
464 dest
[2] = hexDigits
[val
& 0xf];
467 /* Attempts to parse the domain name from the host.
469 * This function also includes the Top-level Domain (TLD) name
470 * of the host when it tries to find the domain name. If it finds
471 * a valid domain name it will assign 'domain_start' the offset
472 * into 'host' where the domain name starts.
474 * It's implied that if there is a domain name its range is:
475 * [host+domain_start, host+host_len).
477 void find_domain_name(const WCHAR
*host
, DWORD host_len
,
479 const WCHAR
*last_tld
, *sec_last_tld
, *end
, *p
;
481 end
= host
+host_len
-1;
485 /* There has to be at least enough room for a '.' followed by a
486 * 3-character TLD for a domain to even exist in the host name.
491 for (last_tld
= sec_last_tld
= NULL
, p
= host
; p
<= end
; p
++)
495 sec_last_tld
= last_tld
;
500 /* http://hostname -> has no domain name. */
504 /* If the '.' is at the beginning of the host there
505 * has to be at least 3 characters in the TLD for it
507 * Ex: .com -> .com as the domain name.
508 * .co -> has no domain name.
510 if(last_tld
-host
== 0) {
511 if(end
-(last_tld
-1) < 3)
513 } else if(last_tld
-host
== 3) {
516 /* If there are three characters in front of last_tld and
517 * they are on the list of recognized TLDs, then this
518 * host doesn't have a domain (since the host only contains
520 * Ex: edu.uk -> has no domain name.
521 * foo.uk -> foo.uk as the domain name.
523 for(i
= 0; i
< ARRAY_SIZE(recognized_tlds
); ++i
) {
524 if(!StrCmpNIW(host
, recognized_tlds
[i
].tld_name
, 3))
527 } else if(last_tld
-host
< 3)
528 /* Anything less than 3 characters is considered part
530 * Ex: ak.uk -> Has no domain name.
534 /* Otherwise the domain name is the whole host name. */
536 } else if(end
+1-last_tld
> 3) {
537 /* If the last_tld has more than 3 characters, then it's automatically
538 * considered the TLD of the domain name.
539 * Ex: www.winehq.org.uk.test -> uk.test as the domain name.
541 *domain_start
= (sec_last_tld
+1)-host
;
542 } else if(last_tld
- (sec_last_tld
+1) < 4) {
544 /* If the sec_last_tld is 3 characters long it HAS to be on the list of
545 * recognized to still be considered part of the TLD name, otherwise
546 * it's considered the domain name.
547 * Ex: www.google.com.uk -> google.com.uk as the domain name.
548 * www.google.foo.uk -> foo.uk as the domain name.
550 if(last_tld
- (sec_last_tld
+1) == 3) {
551 for(i
= 0; i
< ARRAY_SIZE(recognized_tlds
); ++i
) {
552 if(!StrCmpNIW(sec_last_tld
+1, recognized_tlds
[i
].tld_name
, 3)) {
553 for (p
= sec_last_tld
; p
> host
; p
--) if (p
[-1] == '.') break;
554 *domain_start
= p
- host
;
555 TRACE("Found domain name %s\n", debugstr_wn(host
+*domain_start
,
556 (host
+host_len
)-(host
+*domain_start
)));
561 *domain_start
= (sec_last_tld
+1)-host
;
563 /* Since the sec_last_tld is less than 3 characters it's considered
565 * Ex: www.google.fo.uk -> google.fo.uk as the domain name.
567 for (p
= sec_last_tld
; p
> host
; p
--) if (p
[-1] == '.') break;
568 *domain_start
= p
- host
;
571 /* The second to last TLD has more than 3 characters making it
573 * Ex: www.google.test.us -> test.us as the domain name.
575 *domain_start
= (sec_last_tld
+1)-host
;
578 TRACE("Found domain name %s\n", debugstr_wn(host
+*domain_start
,
579 (host
+host_len
)-(host
+*domain_start
)));
582 /* Removes the dot segments from a hierarchical URIs path component. This
583 * function performs the removal in place.
585 * This function returns the new length of the path string.
587 static DWORD
remove_dot_segments(WCHAR
*path
, DWORD path_len
) {
589 const WCHAR
*in
= out
;
590 const WCHAR
*end
= out
+ path_len
;
594 /* Move the first path segment in the input buffer to the end of
595 * the output buffer, and any subsequent characters up to, including
596 * the next "/" character (if any) or the end of the input buffer.
598 while(in
< end
&& !is_slash(*in
))
608 /* Handle ending "/." */
615 if(is_slash(in
[1])) {
620 /* If we don't have "/../" or ending "/.." */
621 if(in
[1] != '.' || (in
+ 2 != end
&& !is_slash(in
[2])))
624 /* Find the slash preceding out pointer and move out pointer to it */
625 if(out
> path
+1 && is_slash(*--out
))
627 while(out
> path
&& !is_slash(*(--out
)));
637 TRACE("(%p %ld): Path after dot segments removed %s len=%ld\n", path
, path_len
,
638 debugstr_wn(path
, len
), len
);
642 /* Attempts to find the file extension in a given path. */
643 static INT
find_file_extension(const WCHAR
*path
, DWORD path_len
) {
646 for(end
= path
+path_len
-1; end
>= path
&& *end
!= '/' && *end
!= '\\'; --end
) {
654 /* Removes all the leading and trailing white spaces or
655 * control characters from the URI and removes all control
656 * characters inside of the URI string.
658 static BSTR
pre_process_uri(LPCWSTR uri
) {
659 const WCHAR
*start
, *end
, *ptr
;
665 /* Skip leading controls and whitespace. */
666 while(*start
&& (iswcntrl(*start
) || iswspace(*start
))) ++start
;
668 /* URI consisted only of control/whitespace. */
670 return SysAllocStringLen(NULL
, 0);
672 end
= start
+ lstrlenW(start
);
673 while(--end
> start
&& (iswcntrl(*end
) || iswspace(*end
)));
676 for(ptr
= start
; ptr
< end
; ptr
++) {
681 ret
= SysAllocStringLen(NULL
, len
);
685 for(ptr
= start
, ptr2
=ret
; ptr
< end
; ptr
++) {
693 /* Converts an IPv4 address in numerical form into its fully qualified
694 * string form. This function returns the number of characters written
695 * to 'dest'. If 'dest' is NULL this function will return the number of
696 * characters that would have been written.
698 * It's up to the caller to ensure there's enough space in 'dest' for the
701 static DWORD
ui2ipv4(WCHAR
*dest
, UINT address
) {
705 digits
[0] = (address
>> 24) & 0xff;
706 digits
[1] = (address
>> 16) & 0xff;
707 digits
[2] = (address
>> 8) & 0xff;
708 digits
[3] = address
& 0xff;
712 ret
= swprintf(tmp
, ARRAY_SIZE(tmp
), L
"%u.%u.%u.%u", digits
[0], digits
[1], digits
[2], digits
[3]);
714 ret
= swprintf(dest
, 16, L
"%u.%u.%u.%u", digits
[0], digits
[1], digits
[2], digits
[3]);
719 static DWORD
ui2str(WCHAR
*dest
, UINT value
) {
724 ret
= swprintf(tmp
, ARRAY_SIZE(tmp
), L
"%u", value
);
726 ret
= swprintf(dest
, 11, L
"%u", value
);
731 /* Checks if the characters pointed to by 'ptr' are
732 * a percent encoded data octet.
734 * pct-encoded = "%" HEXDIG HEXDIG
736 static BOOL
check_pct_encoded(const WCHAR
**ptr
) {
737 const WCHAR
*start
= *ptr
;
743 if(!is_hexdigit(**ptr
)) {
749 if(!is_hexdigit(**ptr
)) {
758 /* dec-octet = DIGIT ; 0-9
759 * / %x31-39 DIGIT ; 10-99
760 * / "1" 2DIGIT ; 100-199
761 * / "2" %x30-34 DIGIT ; 200-249
762 * / "25" %x30-35 ; 250-255
764 static BOOL
check_dec_octet(const WCHAR
**ptr
) {
765 const WCHAR
*c1
, *c2
, *c3
;
768 /* A dec-octet must be at least 1 digit long. */
769 if(*c1
< '0' || *c1
> '9')
775 /* Since the 1-digit requirement was met, it doesn't
776 * matter if this is a DIGIT value, it's considered a
779 if(*c2
< '0' || *c2
> '9')
785 /* Same explanation as above. */
786 if(*c3
< '0' || *c3
> '9')
789 /* Anything > 255 isn't a valid IP dec-octet. */
790 if(*c1
>= '2' && *c2
>= '5' && *c3
>= '5') {
799 /* Checks if there is an implicit IPv4 address in the host component of the URI.
800 * The max value of an implicit IPv4 address is UINT_MAX.
803 * "234567" would be considered an implicit IPv4 address.
805 static BOOL
check_implicit_ipv4(const WCHAR
**ptr
, UINT
*val
) {
806 const WCHAR
*start
= *ptr
;
810 while(is_num(**ptr
)) {
811 ret
= ret
*10 + (**ptr
- '0');
827 /* Checks if the string contains an IPv4 address.
829 * This function has a strict mode or a non-strict mode of operation
830 * When 'strict' is set to FALSE this function will return TRUE if
831 * the string contains at least 'dec-octet "." dec-octet' since partial
832 * IPv4 addresses will be normalized out into full IPv4 addresses. When
833 * 'strict' is set this function expects there to be a full IPv4 address.
835 * IPv4address = dec-octet "." dec-octet "." dec-octet "." dec-octet
837 static BOOL
check_ipv4address(const WCHAR
**ptr
, BOOL strict
) {
838 const WCHAR
*start
= *ptr
;
840 if(!check_dec_octet(ptr
)) {
851 if(!check_dec_octet(ptr
)) {
865 if(!check_dec_octet(ptr
)) {
879 if(!check_dec_octet(ptr
)) {
884 /* Found a four digit ip address. */
887 /* Tries to parse the scheme name of the URI.
889 * scheme = ALPHA *(ALPHA | NUM | '+' | '-' | '.') as defined by RFC 3896.
890 * NOTE: Windows accepts a number as the first character of a scheme.
892 static BOOL
parse_scheme_name(const WCHAR
**ptr
, parse_data
*data
, DWORD extras
) {
893 const WCHAR
*start
= *ptr
;
896 data
->scheme_len
= 0;
899 if(**ptr
== '*' && *ptr
== start
) {
900 /* Might have found a wildcard scheme. If it is the next
901 * char has to be a ':' for it to be a valid URI
905 } else if(!is_num(**ptr
) && !is_alpha(**ptr
) && **ptr
!= '+' &&
906 **ptr
!= '-' && **ptr
!= '.')
915 /* Schemes must end with a ':' */
916 if(**ptr
!= ':' && !((extras
& ALLOW_NULL_TERM_SCHEME
) && !**ptr
)) {
921 data
->scheme
= start
;
922 data
->scheme_len
= *ptr
- start
;
928 /* Tries to deduce the corresponding URL_SCHEME for the given URI. Stores
929 * the deduced URL_SCHEME in data->scheme_type.
931 static BOOL
parse_scheme_type(parse_data
*data
) {
932 /* If there's scheme data then see if it's a recognized scheme. */
933 if(data
->scheme
&& data
->scheme_len
) {
936 for(i
= 0; i
< ARRAY_SIZE(recognized_schemes
); ++i
) {
937 if(lstrlenW(recognized_schemes
[i
].scheme_name
) == data
->scheme_len
) {
938 /* Has to be a case insensitive compare. */
939 if(!StrCmpNIW(recognized_schemes
[i
].scheme_name
, data
->scheme
, data
->scheme_len
)) {
940 data
->scheme_type
= recognized_schemes
[i
].scheme
;
946 /* If we get here it means it's not a recognized scheme. */
947 data
->scheme_type
= URL_SCHEME_UNKNOWN
;
949 } else if(data
->is_relative
) {
950 /* Relative URI's have no scheme. */
951 data
->scheme_type
= URL_SCHEME_UNKNOWN
;
954 /* Should never reach here! what happened... */
955 FIXME("(%p): Unable to determine scheme type for URI %s\n", data
, debugstr_w(data
->uri
));
960 /* Tries to parse (or deduce) the scheme_name of a URI. If it can't
961 * parse a scheme from the URI it will try to deduce the scheme_name and scheme_type
962 * using the flags specified in 'flags' (if any). Flags that affect how this function
963 * operates are the Uri_CREATE_ALLOW_* flags.
965 * All parsed/deduced information will be stored in 'data' when the function returns.
967 * Returns TRUE if it was able to successfully parse the information.
969 static BOOL
parse_scheme(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
, DWORD extras
) {
970 /* First check to see if the uri could implicitly be a file path. */
971 if(is_implicit_file_path(*ptr
)) {
972 if(flags
& Uri_CREATE_ALLOW_IMPLICIT_FILE_SCHEME
) {
973 data
->scheme
= L
"file";
974 data
->scheme_len
= lstrlenW(L
"file");
975 data
->has_implicit_scheme
= TRUE
;
977 TRACE("(%p %p %lx): URI is an implicit file path.\n", ptr
, data
, flags
);
979 /* Windows does not consider anything that can implicitly be a file
980 * path to be a valid URI if the ALLOW_IMPLICIT_FILE_SCHEME flag is not set...
982 TRACE("(%p %p %lx): URI is implicitly a file path, but, the ALLOW_IMPLICIT_FILE_SCHEME flag wasn't set.\n",
986 } else if(!parse_scheme_name(ptr
, data
, extras
)) {
987 /* No scheme was found, this means it could be:
988 * a) an implicit Wildcard scheme
992 if(flags
& Uri_CREATE_ALLOW_IMPLICIT_WILDCARD_SCHEME
) {
994 data
->scheme_len
= lstrlenW(L
"*");
995 data
->has_implicit_scheme
= TRUE
;
997 TRACE("(%p %p %lx): URI is an implicit wildcard scheme.\n", ptr
, data
, flags
);
998 } else if (flags
& Uri_CREATE_ALLOW_RELATIVE
) {
999 data
->is_relative
= TRUE
;
1000 TRACE("(%p %p %lx): URI is relative.\n", ptr
, data
, flags
);
1002 TRACE("(%p %p %lx): Malformed URI found. Unable to deduce scheme name.\n", ptr
, data
, flags
);
1007 if(!data
->is_relative
)
1008 TRACE("(%p %p %lx): Found scheme=%s scheme_len=%ld\n", ptr
, data
, flags
,
1009 debugstr_wn(data
->scheme
, data
->scheme_len
), data
->scheme_len
);
1011 if(!parse_scheme_type(data
))
1014 TRACE("(%p %p %lx): Assigned %d as the URL_SCHEME.\n", ptr
, data
, flags
, data
->scheme_type
);
1018 static BOOL
parse_username(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
, DWORD extras
) {
1019 data
->username
= *ptr
;
1021 while(**ptr
!= ':' && **ptr
!= '@') {
1023 if(!check_pct_encoded(ptr
)) {
1024 if(data
->scheme_type
!= URL_SCHEME_UNKNOWN
) {
1025 *ptr
= data
->username
;
1026 data
->username
= NULL
;
1031 } else if(extras
& ALLOW_NULL_TERM_USER_NAME
&& !**ptr
)
1033 else if(is_auth_delim(**ptr
, data
->scheme_type
!= URL_SCHEME_UNKNOWN
)) {
1034 *ptr
= data
->username
;
1035 data
->username
= NULL
;
1042 data
->username_len
= *ptr
- data
->username
;
1046 static BOOL
parse_password(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
, DWORD extras
) {
1047 data
->password
= *ptr
;
1049 while(**ptr
!= '@') {
1051 if(!check_pct_encoded(ptr
)) {
1052 if(data
->scheme_type
!= URL_SCHEME_UNKNOWN
) {
1053 *ptr
= data
->password
;
1054 data
->password
= NULL
;
1059 } else if(extras
& ALLOW_NULL_TERM_PASSWORD
&& !**ptr
)
1061 else if(is_auth_delim(**ptr
, data
->scheme_type
!= URL_SCHEME_UNKNOWN
)) {
1062 *ptr
= data
->password
;
1063 data
->password
= NULL
;
1070 data
->password_len
= *ptr
- data
->password
;
1074 /* Parses the userinfo part of the URI (if it exists). The userinfo field of
1075 * a URI can consist of "username:password@", or just "username@".
1078 * userinfo = *( unreserved / pct-encoded / sub-delims / ":" )
1081 * 1) If there is more than one ':' in the userinfo part of the URI Windows
1082 * uses the first occurrence of ':' to delimit the username and password
1086 * ftp://user:pass:word@winehq.org
1088 * would yield "user" as the username and "pass:word" as the password.
1090 * 2) Windows allows any character to appear in the "userinfo" part of
1091 * a URI, as long as it's not an authority delimiter character set.
1093 static void parse_userinfo(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
) {
1094 const WCHAR
*start
= *ptr
;
1096 if(!parse_username(ptr
, data
, flags
, 0)) {
1097 TRACE("(%p %p %lx): URI contained no userinfo.\n", ptr
, data
, flags
);
1103 if(!parse_password(ptr
, data
, flags
, 0)) {
1105 data
->username
= NULL
;
1106 data
->username_len
= 0;
1107 TRACE("(%p %p %lx): URI contained no userinfo.\n", ptr
, data
, flags
);
1114 data
->username
= NULL
;
1115 data
->username_len
= 0;
1116 data
->password
= NULL
;
1117 data
->password_len
= 0;
1119 TRACE("(%p %p %lx): URI contained no userinfo.\n", ptr
, data
, flags
);
1124 TRACE("(%p %p %lx): Found username %s len=%ld.\n", ptr
, data
, flags
,
1125 debugstr_wn(data
->username
, data
->username_len
), data
->username_len
);
1128 TRACE("(%p %p %lx): Found password %s len=%ld.\n", ptr
, data
, flags
,
1129 debugstr_wn(data
->password
, data
->password_len
), data
->password_len
);
1134 /* Attempts to parse a port from the URI.
1137 * Windows seems to have a cap on what the maximum value
1138 * for a port can be. The max value is USHORT_MAX.
1142 static BOOL
parse_port(const WCHAR
**ptr
, parse_data
*data
) {
1146 while(!is_auth_delim(**ptr
, data
->scheme_type
!= URL_SCHEME_UNKNOWN
)) {
1147 if(!is_num(**ptr
)) {
1153 port
= port
*10 + (**ptr
-'0');
1155 if(port
> USHRT_MAX
) {
1164 data
->has_port
= TRUE
;
1165 data
->port_value
= port
;
1166 data
->port_len
= *ptr
- data
->port
;
1168 TRACE("(%p %p): Found port %s len=%ld value=%lu\n", ptr
, data
,
1169 debugstr_wn(data
->port
, data
->port_len
), data
->port_len
, data
->port_value
);
1173 /* Attempts to parse a IPv4 address from the URI.
1176 * Windows normalizes IPv4 addresses, This means there are three
1177 * possibilities for the URI to contain an IPv4 address.
1178 * 1) A well formed address (ex. 192.2.2.2).
1179 * 2) A partially formed address. For example "192.0" would
1180 * normalize to "192.0.0.0" during canonicalization.
1181 * 3) An implicit IPv4 address. For example "256" would
1182 * normalize to "0.0.1.0" during canonicalization. Also
1183 * note that the maximum value for an implicit IP address
1184 * is UINT_MAX, if the value in the URI exceeds this then
1185 * it is not considered an IPv4 address.
1187 static BOOL
parse_ipv4address(const WCHAR
**ptr
, parse_data
*data
) {
1188 const BOOL is_unknown
= data
->scheme_type
== URL_SCHEME_UNKNOWN
;
1191 if(!check_ipv4address(ptr
, FALSE
)) {
1192 if(!check_implicit_ipv4(ptr
, &data
->implicit_ipv4
)) {
1193 TRACE("(%p %p): URI didn't contain anything looking like an IPv4 address.\n", ptr
, data
);
1198 data
->has_implicit_ip
= TRUE
;
1201 data
->host_len
= *ptr
- data
->host
;
1202 data
->host_type
= Uri_HOST_IPV4
;
1204 /* Check if what we found is the only part of the host name (if it isn't
1205 * we don't have an IPv4 address).
1209 if(!parse_port(ptr
, data
)) {
1214 } else if(!is_auth_delim(**ptr
, !is_unknown
)) {
1215 /* Found more data which belongs to the host, so this isn't an IPv4. */
1218 data
->has_implicit_ip
= FALSE
;
1222 TRACE("(%p %p): IPv4 address found. host=%s host_len=%ld host_type=%d\n",
1223 ptr
, data
, debugstr_wn(data
->host
, data
->host_len
),
1224 data
->host_len
, data
->host_type
);
1228 /* Attempts to parse the reg-name from the URI.
1230 * Because of the way Windows handles ':' this function also
1231 * handles parsing the port.
1233 * reg-name = *( unreserved / pct-encoded / sub-delims )
1236 * Windows allows everything, but, the characters in "auth_delims" and ':'
1237 * to appear in a reg-name, unless it's an unknown scheme type then ':' is
1238 * allowed to appear (even if a valid port isn't after it).
1240 * Windows doesn't like host names which start with '[' and end with ']'
1241 * and don't contain a valid IP literal address in between them.
1243 * On Windows if a '[' is encountered in the host name the ':' no longer
1244 * counts as a delimiter until you reach the next ']' or an "authority delimiter".
1246 * A reg-name CAN be empty.
1248 static BOOL
parse_reg_name(const WCHAR
**ptr
, parse_data
*data
, DWORD extras
) {
1249 const BOOL has_start_bracket
= **ptr
== '[';
1250 const BOOL known_scheme
= data
->scheme_type
!= URL_SCHEME_UNKNOWN
;
1251 const BOOL is_res
= data
->scheme_type
== URL_SCHEME_RES
;
1252 BOOL inside_brackets
= has_start_bracket
;
1254 /* res URIs don't have ports. */
1255 BOOL ignore_col
= (extras
& IGNORE_PORT_DELIMITER
) || is_res
;
1257 /* We have to be careful with file schemes. */
1258 if(data
->scheme_type
== URL_SCHEME_FILE
) {
1259 /* This is because an implicit file scheme could be "C:\\test" and it
1260 * would trick this function into thinking the host is "C", when after
1261 * canonicalization the host would end up being an empty string. A drive
1262 * path can also have a '|' instead of a ':' after the drive letter.
1264 if(is_drive_path(*ptr
)) {
1265 /* Regular old drive paths have no host type (or host name). */
1266 data
->host_type
= Uri_HOST_UNKNOWN
;
1270 } else if(is_unc_path(*ptr
))
1271 /* Skip past the "\\" of a UNC path. */
1277 /* For res URIs, everything before the first '/' is
1278 * considered the host.
1280 while((!is_res
&& !is_auth_delim(**ptr
, known_scheme
)) ||
1281 (is_res
&& **ptr
&& **ptr
!= '/')) {
1282 if(**ptr
== ':' && !ignore_col
) {
1283 /* We can ignore ':' if we are inside brackets.*/
1284 if(!inside_brackets
) {
1285 const WCHAR
*tmp
= (*ptr
)++;
1287 /* Attempt to parse the port. */
1288 if(!parse_port(ptr
, data
)) {
1289 /* Windows expects there to be a valid port for known scheme types. */
1290 if(data
->scheme_type
!= URL_SCHEME_UNKNOWN
) {
1293 TRACE("(%p %p %lx): Expected valid port\n", ptr
, data
, extras
);
1296 /* Windows gives up on trying to parse a port when it
1297 * encounters an invalid port.
1301 data
->host_len
= tmp
- data
->host
;
1305 } else if(**ptr
== '%' && (known_scheme
&& !is_res
)) {
1306 /* Has to be a legit % encoded value. */
1307 if(!check_pct_encoded(ptr
)) {
1313 } else if(is_res
&& is_forbidden_dos_path_char(**ptr
)) {
1317 } else if(**ptr
== ']')
1318 inside_brackets
= FALSE
;
1319 else if(**ptr
== '[')
1320 inside_brackets
= TRUE
;
1325 if(has_start_bracket
) {
1326 /* Make sure the last character of the host wasn't a ']'. */
1327 if(*(*ptr
-1) == ']') {
1328 TRACE("(%p %p %lx): Expected an IP literal inside of the host\n", ptr
, data
, extras
);
1335 /* Don't overwrite our length if we found a port earlier. */
1337 data
->host_len
= *ptr
- data
->host
;
1339 /* If the host is empty, then it's an unknown host type. */
1340 if(data
->host_len
== 0 || is_res
)
1341 data
->host_type
= Uri_HOST_UNKNOWN
;
1343 data
->host_type
= Uri_HOST_DNS
;
1345 TRACE("(%p %p %lx): Parsed reg-name. host=%s len=%ld\n", ptr
, data
, extras
,
1346 debugstr_wn(data
->host
, data
->host_len
), data
->host_len
);
1350 /* Attempts to parse an IPv6 address out of the URI.
1352 * IPv6address = 6( h16 ":" ) ls32
1353 * / "::" 5( h16 ":" ) ls32
1354 * / [ h16 ] "::" 4( h16 ":" ) ls32
1355 * / [ *1( h16 ":" ) h16 ] "::" 3( h16 ":" ) ls32
1356 * / [ *2( h16 ":" ) h16 ] "::" 2( h16 ":" ) ls32
1357 * / [ *3( h16 ":" ) h16 ] "::" h16 ":" ls32
1358 * / [ *4( h16 ":" ) h16 ] "::" ls32
1359 * / [ *5( h16 ":" ) h16 ] "::" h16
1360 * / [ *6( h16 ":" ) h16 ] "::"
1362 * ls32 = ( h16 ":" h16 ) / IPv4address
1363 * ; least-significant 32 bits of address.
1366 * ; 16 bits of address represented in hexadecimal.
1368 static BOOL
parse_ipv6address(const WCHAR
**ptr
, parse_data
*data
) {
1369 const WCHAR
*terminator
;
1371 if(RtlIpv6StringToAddressW(*ptr
, &terminator
, &data
->ipv6_address
))
1373 if(*terminator
!= ']' && !is_auth_delim(*terminator
, data
->scheme_type
!= URL_SCHEME_UNKNOWN
))
1377 data
->host_type
= Uri_HOST_IPV6
;
1381 /* IPvFuture = "v" 1*HEXDIG "." 1*( unreserved / sub-delims / ":" ) */
1382 static BOOL
parse_ipvfuture(const WCHAR
**ptr
, parse_data
*data
) {
1383 const WCHAR
*start
= *ptr
;
1385 /* IPvFuture has to start with a 'v' or 'V'. */
1386 if(**ptr
!= 'v' && **ptr
!= 'V')
1389 /* Following the v there must be at least 1 hex digit. */
1391 if(!is_hexdigit(**ptr
)) {
1397 while(is_hexdigit(**ptr
))
1400 /* End of the hexdigit sequence must be a '.' */
1407 if(!is_unreserved(**ptr
) && !is_subdelim(**ptr
) && **ptr
!= ':') {
1413 while(is_unreserved(**ptr
) || is_subdelim(**ptr
) || **ptr
== ':')
1416 data
->host_type
= Uri_HOST_UNKNOWN
;
1418 TRACE("(%p %p): Parsed IPvFuture address %s len=%d\n", ptr
, data
,
1419 debugstr_wn(start
, *ptr
-start
), (int)(*ptr
-start
));
1424 /* IP-literal = "[" ( IPv6address / IPvFuture ) "]" */
1425 static BOOL
parse_ip_literal(const WCHAR
**ptr
, parse_data
*data
, DWORD extras
) {
1428 if(**ptr
!= '[' && !(extras
& ALLOW_BRACKETLESS_IP_LITERAL
)) {
1431 } else if(**ptr
== '[')
1434 if(!parse_ipv6address(ptr
, data
)) {
1435 if(extras
& SKIP_IP_FUTURE_CHECK
|| !parse_ipvfuture(ptr
, data
)) {
1442 if(**ptr
!= ']' && !(extras
& ALLOW_BRACKETLESS_IP_LITERAL
)) {
1446 } else if(!**ptr
&& extras
& ALLOW_BRACKETLESS_IP_LITERAL
) {
1447 /* The IP literal didn't contain brackets and was followed by
1448 * a NULL terminator, so no reason to even check the port.
1450 data
->host_len
= *ptr
- data
->host
;
1457 /* If a valid port is not found, then let it trickle down to
1460 if(!parse_port(ptr
, data
)) {
1466 data
->host_len
= *ptr
- data
->host
;
1471 /* Parses the host information from the URI.
1473 * host = IP-literal / IPv4address / reg-name
1475 static BOOL
parse_host(const WCHAR
**ptr
, parse_data
*data
, DWORD extras
) {
1476 if(!parse_ip_literal(ptr
, data
, extras
)) {
1477 if(!parse_ipv4address(ptr
, data
)) {
1478 if(!parse_reg_name(ptr
, data
, extras
)) {
1479 TRACE("(%p %p %lx): Malformed URI, Unknown host type.\n", ptr
, data
, extras
);
1488 /* Parses the authority information from the URI.
1490 * authority = [ userinfo "@" ] host [ ":" port ]
1492 static BOOL
parse_authority(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
) {
1493 parse_userinfo(ptr
, data
, flags
);
1495 /* Parsing the port will happen during one of the host parsing
1496 * routines (if the URI has a port).
1498 if(!parse_host(ptr
, data
, 0))
1504 /* Attempts to parse the path information of a hierarchical URI. */
1505 static BOOL
parse_path_hierarchical(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
) {
1506 const WCHAR
*start
= *ptr
;
1507 const BOOL is_file
= data
->scheme_type
== URL_SCHEME_FILE
;
1509 if(is_path_delim(data
->scheme_type
, **ptr
)) {
1510 if(data
->scheme_type
== URL_SCHEME_WILDCARD
&& !data
->must_have_path
) {
1513 } else if(!(flags
& Uri_CREATE_NO_CANONICALIZE
)) {
1514 /* If the path component is empty, then a '/' is added. */
1519 while(!is_path_delim(data
->scheme_type
, **ptr
)) {
1520 if(**ptr
== '%' && data
->scheme_type
!= URL_SCHEME_UNKNOWN
&& !is_file
) {
1521 if(!check_pct_encoded(ptr
)) {
1526 } else if(is_forbidden_dos_path_char(**ptr
) && is_file
&&
1527 (flags
& Uri_CREATE_FILE_USE_DOS_PATH
)) {
1528 /* File schemes with USE_DOS_PATH set aren't allowed to have
1529 * a '<' or '>' or '\"' appear in them.
1533 } else if(**ptr
== '\\') {
1534 /* Not allowed to have a backslash if NO_CANONICALIZE is set
1535 * and the scheme is known type (but not a file scheme).
1537 if(flags
& Uri_CREATE_NO_CANONICALIZE
) {
1538 if(data
->scheme_type
!= URL_SCHEME_FILE
&&
1539 data
->scheme_type
!= URL_SCHEME_UNKNOWN
) {
1549 /* The only time a URI doesn't have a path is when
1550 * the NO_CANONICALIZE flag is set and the raw URI
1551 * didn't contain one.
1558 data
->path_len
= *ptr
- start
;
1563 TRACE("(%p %p %lx): Parsed path %s len=%ld\n", ptr
, data
, flags
,
1564 debugstr_wn(data
->path
, data
->path_len
), data
->path_len
);
1566 TRACE("(%p %p %lx): The URI contained no path\n", ptr
, data
, flags
);
1571 /* Parses the path of an opaque URI (much less strict than the parser
1572 * for a hierarchical URI).
1575 * Windows allows invalid % encoded data to appear in opaque URI paths
1576 * for unknown scheme types.
1578 * File schemes with USE_DOS_PATH set aren't allowed to have '<', '>', or '\"'
1581 static BOOL
parse_path_opaque(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
) {
1582 const BOOL known_scheme
= data
->scheme_type
!= URL_SCHEME_UNKNOWN
;
1583 const BOOL is_file
= data
->scheme_type
== URL_SCHEME_FILE
;
1584 const BOOL is_mailto
= data
->scheme_type
== URL_SCHEME_MAILTO
;
1586 if (is_mailto
&& (*ptr
)[0] == '/' && (*ptr
)[1] == '/')
1588 if ((*ptr
)[2]) data
->path
= *ptr
+ 2;
1589 else data
->path
= NULL
;
1594 while(!is_path_delim(data
->scheme_type
, **ptr
)) {
1595 if(**ptr
== '%' && known_scheme
) {
1596 if(!check_pct_encoded(ptr
)) {
1602 } else if(is_forbidden_dos_path_char(**ptr
) && is_file
&&
1603 (flags
& Uri_CREATE_FILE_USE_DOS_PATH
)) {
1612 if (data
->path
) data
->path_len
= *ptr
- data
->path
;
1613 TRACE("(%p %p %lx): Parsed opaque URI path %s len=%ld\n", ptr
, data
, flags
,
1614 debugstr_wn(data
->path
, data
->path_len
), data
->path_len
);
1618 /* Determines how the URI should be parsed after the scheme information.
1620 * If the scheme is followed by "//", then it is treated as a hierarchical URI
1621 * which then the authority and path information will be parsed out. Otherwise, the
1622 * URI will be treated as an opaque URI which the authority information is not parsed
1625 * RFC 3896 definition of hier-part:
1627 * hier-part = "//" authority path-abempty
1632 * MSDN opaque URI definition:
1633 * scheme ":" path [ "#" fragment ]
1636 * If the URI is of an unknown scheme type and has a "//" following the scheme then it
1637 * is treated as a hierarchical URI, but, if the CREATE_NO_CRACK_UNKNOWN_SCHEMES flag is
1638 * set then it is considered an opaque URI regardless of what follows the scheme information
1639 * (per MSDN documentation).
1641 static BOOL
parse_hierpart(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
) {
1642 const WCHAR
*start
= *ptr
;
1644 data
->must_have_path
= FALSE
;
1646 /* For javascript: URIs, simply set everything as a path */
1647 if(data
->scheme_type
== URL_SCHEME_JAVASCRIPT
) {
1649 data
->path_len
= lstrlenW(*ptr
);
1650 data
->is_opaque
= TRUE
;
1651 *ptr
+= data
->path_len
;
1655 /* Checks if the authority information needs to be parsed. */
1656 if(is_hierarchical_uri(ptr
, data
)) {
1657 /* Only treat it as a hierarchical URI if the scheme_type is known or
1658 * the Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES flag is not set.
1660 if(data
->scheme_type
!= URL_SCHEME_UNKNOWN
||
1661 !(flags
& Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES
)) {
1662 TRACE("(%p %p %lx): Treating URI as an hierarchical URI.\n", ptr
, data
, flags
);
1663 data
->is_opaque
= FALSE
;
1665 if(data
->scheme_type
== URL_SCHEME_WILDCARD
&& !data
->has_implicit_scheme
) {
1666 if(**ptr
== '/' && *(*ptr
+1) == '/') {
1667 data
->must_have_path
= TRUE
;
1672 /* TODO: Handle hierarchical URI's, parse authority then parse the path. */
1673 if(!parse_authority(ptr
, data
, flags
))
1676 return parse_path_hierarchical(ptr
, data
, flags
);
1678 /* Reset ptr to its starting position so opaque path parsing
1679 * begins at the correct location.
1684 /* If it reaches here, then the URI will be treated as an opaque
1688 TRACE("(%p %p %lx): Treating URI as an opaque URI.\n", ptr
, data
, flags
);
1690 data
->is_opaque
= TRUE
;
1691 if(!parse_path_opaque(ptr
, data
, flags
))
1697 /* Attempts to parse the query string from the URI.
1700 * If NO_DECODE_EXTRA_INFO flag is set, then invalid percent encoded
1701 * data is allowed to appear in the query string. For unknown scheme types
1702 * invalid percent encoded data is allowed to appear regardless.
1704 static BOOL
parse_query(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
) {
1705 const BOOL known_scheme
= data
->scheme_type
!= URL_SCHEME_UNKNOWN
;
1708 TRACE("(%p %p %lx): URI didn't contain a query string.\n", ptr
, data
, flags
);
1715 while(**ptr
&& **ptr
!= '#') {
1716 if(**ptr
== '%' && known_scheme
&&
1717 !(flags
& Uri_CREATE_NO_DECODE_EXTRA_INFO
)) {
1718 if(!check_pct_encoded(ptr
)) {
1729 data
->query_len
= *ptr
- data
->query
;
1731 TRACE("(%p %p %lx): Parsed query string %s len=%ld\n", ptr
, data
, flags
,
1732 debugstr_wn(data
->query
, data
->query_len
), data
->query_len
);
1736 /* Attempts to parse the fragment from the URI.
1739 * If NO_DECODE_EXTRA_INFO flag is set, then invalid percent encoded
1740 * data is allowed to appear in the query string. For unknown scheme types
1741 * invalid percent encoded data is allowed to appear regardless.
1743 static BOOL
parse_fragment(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
) {
1744 const BOOL known_scheme
= data
->scheme_type
!= URL_SCHEME_UNKNOWN
;
1747 TRACE("(%p %p %lx): URI didn't contain a fragment.\n", ptr
, data
, flags
);
1751 data
->fragment
= *ptr
;
1755 if(**ptr
== '%' && known_scheme
&&
1756 !(flags
& Uri_CREATE_NO_DECODE_EXTRA_INFO
)) {
1757 if(!check_pct_encoded(ptr
)) {
1758 *ptr
= data
->fragment
;
1759 data
->fragment
= NULL
;
1768 data
->fragment_len
= *ptr
- data
->fragment
;
1770 TRACE("(%p %p %lx): Parsed fragment %s len=%ld\n", ptr
, data
, flags
,
1771 debugstr_wn(data
->fragment
, data
->fragment_len
), data
->fragment_len
);
1775 /* Parses and validates the components of the specified by data->uri
1776 * and stores the information it parses into 'data'.
1778 * Returns TRUE if it successfully parsed the URI. False otherwise.
1780 static BOOL
parse_uri(parse_data
*data
, DWORD flags
) {
1787 TRACE("(%p %lx): BEGINNING TO PARSE URI %s.\n", data
, flags
, debugstr_w(data
->uri
));
1789 if(!parse_scheme(pptr
, data
, flags
, 0))
1792 if(!parse_hierpart(pptr
, data
, flags
))
1795 if(!parse_query(pptr
, data
, flags
))
1798 if(!parse_fragment(pptr
, data
, flags
))
1801 TRACE("(%p %lx): FINISHED PARSING URI.\n", data
, flags
);
1805 static BOOL
canonicalize_username(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
1808 if(!data
->username
) {
1809 uri
->userinfo_start
= -1;
1813 uri
->userinfo_start
= uri
->canon_len
;
1814 for(ptr
= data
->username
; ptr
< data
->username
+data
->username_len
; ++ptr
) {
1816 /* Only decode % encoded values for known scheme types. */
1817 if(data
->scheme_type
!= URL_SCHEME_UNKNOWN
) {
1818 /* See if the value really needs decoding. */
1819 WCHAR val
= decode_pct_val(ptr
);
1820 if(is_unreserved(val
)) {
1822 uri
->canon_uri
[uri
->canon_len
] = val
;
1826 /* Move pass the hex characters. */
1831 } else if(is_ascii(*ptr
) && !is_reserved(*ptr
) && !is_unreserved(*ptr
) && *ptr
!= '\\') {
1832 /* Only percent encode forbidden characters if the NO_ENCODE_FORBIDDEN_CHARACTERS flag
1835 if(!(flags
& Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS
)) {
1837 pct_encode_val(*ptr
, uri
->canon_uri
+ uri
->canon_len
);
1839 uri
->canon_len
+= 3;
1845 /* Nothing special, so just copy the character over. */
1846 uri
->canon_uri
[uri
->canon_len
] = *ptr
;
1853 static BOOL
canonicalize_password(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
1856 if(!data
->password
) {
1857 uri
->userinfo_split
= -1;
1861 if(uri
->userinfo_start
== -1)
1862 /* Has a password, but, doesn't have a username. */
1863 uri
->userinfo_start
= uri
->canon_len
;
1865 uri
->userinfo_split
= uri
->canon_len
- uri
->userinfo_start
;
1867 /* Add the ':' to the userinfo component. */
1869 uri
->canon_uri
[uri
->canon_len
] = ':';
1872 for(ptr
= data
->password
; ptr
< data
->password
+data
->password_len
; ++ptr
) {
1874 /* Only decode % encoded values for known scheme types. */
1875 if(data
->scheme_type
!= URL_SCHEME_UNKNOWN
) {
1876 /* See if the value really needs decoding. */
1877 WCHAR val
= decode_pct_val(ptr
);
1878 if(is_unreserved(val
)) {
1880 uri
->canon_uri
[uri
->canon_len
] = val
;
1884 /* Move pass the hex characters. */
1889 } else if(is_ascii(*ptr
) && !is_reserved(*ptr
) && !is_unreserved(*ptr
) && *ptr
!= '\\') {
1890 /* Only percent encode forbidden characters if the NO_ENCODE_FORBIDDEN_CHARACTERS flag
1893 if(!(flags
& Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS
)) {
1895 pct_encode_val(*ptr
, uri
->canon_uri
+ uri
->canon_len
);
1897 uri
->canon_len
+= 3;
1903 /* Nothing special, so just copy the character over. */
1904 uri
->canon_uri
[uri
->canon_len
] = *ptr
;
1911 /* Canonicalizes the userinfo of the URI represented by the parse_data.
1913 * Canonicalization of the userinfo is a simple process. If there are any percent
1914 * encoded characters that fall in the "unreserved" character set, they are decoded
1915 * to their actual value. If a character is not in the "unreserved" or "reserved" sets
1916 * then it is percent encoded. Other than that the characters are copied over without
1919 static BOOL
canonicalize_userinfo(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
1920 uri
->userinfo_start
= uri
->userinfo_split
= -1;
1921 uri
->userinfo_len
= 0;
1923 if(!data
->username
&& !data
->password
)
1924 /* URI doesn't have userinfo, so nothing to do here. */
1927 if(!canonicalize_username(data
, uri
, flags
, computeOnly
))
1930 if(!canonicalize_password(data
, uri
, flags
, computeOnly
))
1933 uri
->userinfo_len
= uri
->canon_len
- uri
->userinfo_start
;
1935 TRACE("(%p %p %lx %d): Canonicalized userinfo, userinfo_start=%d, userinfo=%s, userinfo_split=%d userinfo_len=%ld.\n",
1936 data
, uri
, flags
, computeOnly
, uri
->userinfo_start
, debugstr_wn(uri
->canon_uri
+ uri
->userinfo_start
, uri
->userinfo_len
),
1937 uri
->userinfo_split
, uri
->userinfo_len
);
1939 /* Now insert the '@' after the userinfo. */
1941 uri
->canon_uri
[uri
->canon_len
] = '@';
1947 /* Attempts to canonicalize a reg_name.
1949 * Things that happen:
1950 * 1) If Uri_CREATE_NO_CANONICALIZE flag is not set, then the reg_name is
1951 * lower cased. Unless it's an unknown scheme type, which case it's
1952 * no lower cased regardless.
1954 * 2) Unreserved % encoded characters are decoded for known
1957 * 3) Forbidden characters are % encoded as long as
1958 * Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS flag is not set and
1959 * it isn't an unknown scheme type.
1961 * 4) If it's a file scheme and the host is "localhost" it's removed.
1963 * 5) If it's a file scheme and Uri_CREATE_FILE_USE_DOS_PATH is set,
1964 * then the UNC path characters are added before the host name.
1966 static BOOL
canonicalize_reg_name(const parse_data
*data
, Uri
*uri
,
1967 DWORD flags
, BOOL computeOnly
) {
1969 const BOOL known_scheme
= data
->scheme_type
!= URL_SCHEME_UNKNOWN
;
1971 if(data
->scheme_type
== URL_SCHEME_FILE
&&
1972 data
->host_len
== lstrlenW(L
"localhost")) {
1973 if(!StrCmpNIW(data
->host
, L
"localhost", data
->host_len
)) {
1974 uri
->host_start
= -1;
1976 uri
->host_type
= Uri_HOST_UNKNOWN
;
1981 if(data
->scheme_type
== URL_SCHEME_FILE
&& flags
& Uri_CREATE_FILE_USE_DOS_PATH
) {
1983 uri
->canon_uri
[uri
->canon_len
] = '\\';
1984 uri
->canon_uri
[uri
->canon_len
+1] = '\\';
1986 uri
->canon_len
+= 2;
1987 uri
->authority_start
= uri
->canon_len
;
1990 uri
->host_start
= uri
->canon_len
;
1992 for(ptr
= data
->host
; ptr
< data
->host
+data
->host_len
; ++ptr
) {
1993 if(*ptr
== '%' && known_scheme
) {
1994 WCHAR val
= decode_pct_val(ptr
);
1995 if(is_unreserved(val
)) {
1996 /* If NO_CANONICALIZE is not set, then windows lower cases the
1999 if(!(flags
& Uri_CREATE_NO_CANONICALIZE
) && iswupper(val
)) {
2001 uri
->canon_uri
[uri
->canon_len
] = towlower(val
);
2004 uri
->canon_uri
[uri
->canon_len
] = val
;
2008 /* Skip past the % encoded character. */
2012 /* Just copy the % over. */
2014 uri
->canon_uri
[uri
->canon_len
] = *ptr
;
2017 } else if(*ptr
== '\\') {
2018 /* Only unknown scheme types could have made it here with a '\\' in the host name. */
2020 uri
->canon_uri
[uri
->canon_len
] = *ptr
;
2022 } else if(!(flags
& Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS
) && is_ascii(*ptr
) &&
2023 !is_unreserved(*ptr
) && !is_reserved(*ptr
) && known_scheme
) {
2025 pct_encode_val(*ptr
, uri
->canon_uri
+uri
->canon_len
);
2027 /* The percent encoded value gets lower cased also. */
2028 if(!(flags
& Uri_CREATE_NO_CANONICALIZE
)) {
2029 uri
->canon_uri
[uri
->canon_len
+1] = towlower(uri
->canon_uri
[uri
->canon_len
+1]);
2030 uri
->canon_uri
[uri
->canon_len
+2] = towlower(uri
->canon_uri
[uri
->canon_len
+2]);
2034 uri
->canon_len
+= 3;
2037 if(!(flags
& Uri_CREATE_NO_CANONICALIZE
) && known_scheme
)
2038 uri
->canon_uri
[uri
->canon_len
] = towlower(*ptr
);
2040 uri
->canon_uri
[uri
->canon_len
] = *ptr
;
2047 uri
->host_len
= uri
->canon_len
- uri
->host_start
;
2050 TRACE("(%p %p %lx %d): Canonicalize reg_name=%s len=%ld\n", data
, uri
, flags
,
2051 computeOnly
, debugstr_wn(uri
->canon_uri
+uri
->host_start
, uri
->host_len
),
2055 find_domain_name(uri
->canon_uri
+uri
->host_start
, uri
->host_len
,
2056 &(uri
->domain_offset
));
2061 /* Attempts to canonicalize an implicit IPv4 address. */
2062 static BOOL
canonicalize_implicit_ipv4address(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
2063 uri
->host_start
= uri
->canon_len
;
2065 TRACE("%u\n", data
->implicit_ipv4
);
2066 /* For unknown scheme types Windows doesn't convert
2067 * the value into an IP address, but it still considers
2068 * it an IPv4 address.
2070 if(data
->scheme_type
== URL_SCHEME_UNKNOWN
) {
2072 memcpy(uri
->canon_uri
+uri
->canon_len
, data
->host
, data
->host_len
*sizeof(WCHAR
));
2073 uri
->canon_len
+= data
->host_len
;
2076 uri
->canon_len
+= ui2ipv4(uri
->canon_uri
+uri
->canon_len
, data
->implicit_ipv4
);
2078 uri
->canon_len
+= ui2ipv4(NULL
, data
->implicit_ipv4
);
2081 uri
->host_len
= uri
->canon_len
- uri
->host_start
;
2082 uri
->host_type
= Uri_HOST_IPV4
;
2085 TRACE("%p %p %lx %d): Canonicalized implicit IP address=%s len=%ld\n",
2086 data
, uri
, flags
, computeOnly
,
2087 debugstr_wn(uri
->canon_uri
+uri
->host_start
, uri
->host_len
),
2093 /* Attempts to canonicalize an IPv4 address.
2095 * If the parse_data represents a URI that has an implicit IPv4 address
2096 * (ex. http://256/, this function will convert 256 into 0.0.1.0). If
2097 * the implicit IP address exceeds the value of UINT_MAX (maximum value
2098 * for an IPv4 address) it's canonicalized as if it were a reg-name.
2100 * If the parse_data contains a partial or full IPv4 address it normalizes it.
2101 * A partial IPv4 address is something like "192.0" and would be normalized to
2102 * "192.0.0.0". With a full (or partial) IPv4 address like "192.002.01.003" would
2103 * be normalized to "192.2.1.3".
2106 * Windows ONLY normalizes IPv4 address for known scheme types (one that isn't
2107 * URL_SCHEME_UNKNOWN). For unknown scheme types, it simply copies the data from
2108 * the original URI into the canonicalized URI, but, it still recognizes URI's
2109 * host type as HOST_IPV4.
2111 static BOOL
canonicalize_ipv4address(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
2112 if(data
->has_implicit_ip
)
2113 return canonicalize_implicit_ipv4address(data
, uri
, flags
, computeOnly
);
2115 uri
->host_start
= uri
->canon_len
;
2117 /* Windows only normalizes for known scheme types. */
2118 if(data
->scheme_type
!= URL_SCHEME_UNKNOWN
) {
2119 /* parse_data contains a partial or full IPv4 address, so normalize it. */
2120 DWORD i
, octetDigitCount
= 0, octetCount
= 0;
2121 BOOL octetHasDigit
= FALSE
;
2123 for(i
= 0; i
< data
->host_len
; ++i
) {
2124 if(data
->host
[i
] == '0' && !octetHasDigit
) {
2125 /* Can ignore leading zeros if:
2126 * 1) It isn't the last digit of the octet.
2127 * 2) i+1 != data->host_len
2130 if(octetDigitCount
== 2 ||
2131 i
+1 == data
->host_len
||
2132 data
->host
[i
+1] == '.') {
2134 uri
->canon_uri
[uri
->canon_len
] = data
->host
[i
];
2136 TRACE("Adding zero\n");
2138 } else if(data
->host
[i
] == '.') {
2140 uri
->canon_uri
[uri
->canon_len
] = data
->host
[i
];
2143 octetDigitCount
= 0;
2144 octetHasDigit
= FALSE
;
2148 uri
->canon_uri
[uri
->canon_len
] = data
->host
[i
];
2152 octetHasDigit
= TRUE
;
2156 /* Make sure the canonicalized IP address has 4 dec-octets.
2157 * If doesn't add "0" ones until there is 4;
2159 for( ; octetCount
< 3; ++octetCount
) {
2161 uri
->canon_uri
[uri
->canon_len
] = '.';
2162 uri
->canon_uri
[uri
->canon_len
+1] = '0';
2165 uri
->canon_len
+= 2;
2168 /* Windows doesn't normalize addresses in unknown schemes. */
2170 memcpy(uri
->canon_uri
+uri
->canon_len
, data
->host
, data
->host_len
*sizeof(WCHAR
));
2171 uri
->canon_len
+= data
->host_len
;
2174 uri
->host_len
= uri
->canon_len
- uri
->host_start
;
2176 TRACE("(%p %p %lx %d): Canonicalized IPv4 address, ip=%s len=%ld\n",
2177 data
, uri
, flags
, computeOnly
,
2178 debugstr_wn(uri
->canon_uri
+uri
->host_start
, uri
->host_len
),
2185 /* Attempts to canonicalize the IPv6 address of the URI.
2187 * Multiple things happen during the canonicalization of an IPv6 address:
2188 * 1) Any leading zero's in a h16 component are removed.
2189 * Ex: [0001:0022::] -> [1:22::]
2191 * 2) The longest sequence of zero h16 components are compressed
2192 * into a "::" (elision). If there's a tie, the first is chosen.
2194 * Ex: [0:0:0:0:1:6:7:8] -> [::1:6:7:8]
2195 * [0:0:0:0:1:2::] -> [::1:2:0:0]
2196 * [0:0:1:2:0:0:7:8] -> [::1:2:0:0:7:8]
2198 * 3) If an IPv4 address is attached to the IPv6 address, it's
2200 * Ex: [::001.002.022.000] -> [::1.2.22.0]
2202 * 4) If an elision is present, but, only represents one h16 component
2205 * Ex: [1::2:3:4:5:6:7] -> [1:0:2:3:4:5:6:7]
2207 * 5) If the IPv6 address contains an IPv4 address and there exists
2208 * at least 1 non-zero h16 component the IPv4 address is converted
2209 * into two h16 components, otherwise it's normalized and kept as is.
2211 * Ex: [::192.200.003.4] -> [::192.200.3.4]
2212 * [ffff::192.200.003.4] -> [ffff::c0c8:3041]
2215 * For unknown scheme types Windows simply copies the address over without any
2218 * IPv4 address can be included in an elision if all its components are 0's.
2220 static BOOL
canonicalize_ipv6address(const parse_data
*data
, Uri
*uri
,
2221 DWORD flags
, BOOL computeOnly
) {
2222 uri
->host_start
= uri
->canon_len
;
2224 if(data
->scheme_type
== URL_SCHEME_UNKNOWN
) {
2226 memcpy(uri
->canon_uri
+uri
->canon_len
, data
->host
, data
->host_len
*sizeof(WCHAR
));
2227 uri
->canon_len
+= data
->host_len
;
2230 ULONG size
= ARRAY_SIZE(buffer
);
2233 RtlIpv6AddressToStringExW(&data
->ipv6_address
, 0, 0, buffer
, &size
);
2234 uri
->canon_len
+= size
+ 1;
2236 uri
->canon_uri
[uri
->canon_len
++] = '[';
2237 RtlIpv6AddressToStringExW(&data
->ipv6_address
, 0, 0, uri
->canon_uri
+ uri
->canon_len
, &size
);
2238 uri
->canon_len
+= size
- 1;
2239 uri
->canon_uri
[uri
->canon_len
++] = ']';
2243 uri
->host_len
= uri
->canon_len
- uri
->host_start
;
2246 TRACE("(%p %p %lx %d): Canonicalized IPv6 address %s, len=%ld\n", data
, uri
, flags
,
2247 computeOnly
, debugstr_wn(uri
->canon_uri
+uri
->host_start
, uri
->host_len
),
2253 /* Attempts to canonicalize the host of the URI (if any). */
2254 static BOOL
canonicalize_host(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
2255 uri
->host_start
= -1;
2257 uri
->domain_offset
= -1;
2260 switch(data
->host_type
) {
2262 uri
->host_type
= Uri_HOST_DNS
;
2263 if(!canonicalize_reg_name(data
, uri
, flags
, computeOnly
))
2268 uri
->host_type
= Uri_HOST_IPV4
;
2269 if(!canonicalize_ipv4address(data
, uri
, flags
, computeOnly
))
2274 if(!canonicalize_ipv6address(data
, uri
, flags
, computeOnly
))
2277 uri
->host_type
= Uri_HOST_IPV6
;
2279 case Uri_HOST_UNKNOWN
:
2280 if(data
->host_len
> 0 || data
->scheme_type
!= URL_SCHEME_FILE
) {
2281 uri
->host_start
= uri
->canon_len
;
2283 /* Nothing happens to unknown host types. */
2285 memcpy(uri
->canon_uri
+uri
->canon_len
, data
->host
, data
->host_len
*sizeof(WCHAR
));
2286 uri
->canon_len
+= data
->host_len
;
2287 uri
->host_len
= data
->host_len
;
2290 uri
->host_type
= Uri_HOST_UNKNOWN
;
2293 FIXME("(%p %p %lx %d): Canonicalization for host type %d not supported.\n", data
,
2294 uri
, flags
, computeOnly
, data
->host_type
);
2302 static BOOL
canonicalize_port(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
2303 BOOL has_default_port
= FALSE
;
2304 USHORT default_port
= 0;
2307 uri
->port_offset
= -1;
2309 /* Check if the scheme has a default port. */
2310 for(i
= 0; i
< ARRAY_SIZE(default_ports
); ++i
) {
2311 if(default_ports
[i
].scheme
== data
->scheme_type
) {
2312 has_default_port
= TRUE
;
2313 default_port
= default_ports
[i
].port
;
2318 uri
->has_port
= data
->has_port
|| has_default_port
;
2321 * 1) Has a port which is the default port.
2322 * 2) Has a port (not the default).
2323 * 3) Doesn't have a port, but, scheme has a default port.
2326 if(has_default_port
&& data
->has_port
&& data
->port_value
== default_port
) {
2327 /* If it's the default port and this flag isn't set, don't do anything. */
2328 if(flags
& Uri_CREATE_NO_CANONICALIZE
) {
2329 uri
->port_offset
= uri
->canon_len
-uri
->authority_start
;
2331 uri
->canon_uri
[uri
->canon_len
] = ':';
2335 /* Copy the original port over. */
2337 memcpy(uri
->canon_uri
+uri
->canon_len
, data
->port
, data
->port_len
*sizeof(WCHAR
));
2338 uri
->canon_len
+= data
->port_len
;
2341 uri
->canon_len
+= ui2str(uri
->canon_uri
+uri
->canon_len
, data
->port_value
);
2343 uri
->canon_len
+= ui2str(NULL
, data
->port_value
);
2347 uri
->port
= default_port
;
2348 } else if(data
->has_port
) {
2349 uri
->port_offset
= uri
->canon_len
-uri
->authority_start
;
2351 uri
->canon_uri
[uri
->canon_len
] = ':';
2354 if(flags
& Uri_CREATE_NO_CANONICALIZE
&& data
->port
) {
2355 /* Copy the original over without changes. */
2357 memcpy(uri
->canon_uri
+uri
->canon_len
, data
->port
, data
->port_len
*sizeof(WCHAR
));
2358 uri
->canon_len
+= data
->port_len
;
2361 uri
->canon_len
+= ui2str(uri
->canon_uri
+uri
->canon_len
, data
->port_value
);
2363 uri
->canon_len
+= ui2str(NULL
, data
->port_value
);
2366 uri
->port
= data
->port_value
;
2367 } else if(has_default_port
)
2368 uri
->port
= default_port
;
2373 /* Canonicalizes the authority of the URI represented by the parse_data. */
2374 static BOOL
canonicalize_authority(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
2375 uri
->authority_start
= uri
->canon_len
;
2376 uri
->authority_len
= 0;
2378 if(!canonicalize_userinfo(data
, uri
, flags
, computeOnly
))
2381 if(!canonicalize_host(data
, uri
, flags
, computeOnly
))
2384 if(!canonicalize_port(data
, uri
, flags
, computeOnly
))
2387 if(uri
->host_start
!= -1 || (data
->is_relative
&& (data
->password
|| data
->username
)))
2388 uri
->authority_len
= uri
->canon_len
- uri
->authority_start
;
2390 uri
->authority_start
= -1;
2395 /* Attempts to canonicalize the path of a hierarchical URI.
2397 * Things that happen:
2398 * 1). Forbidden characters are percent encoded, unless the NO_ENCODE_FORBIDDEN
2399 * flag is set or it's a file URI. Forbidden characters are always encoded
2400 * for file schemes regardless and forbidden characters are never encoded
2401 * for unknown scheme types.
2403 * 2). For known scheme types '\\' are changed to '/'.
2405 * 3). Percent encoded, unreserved characters are decoded to their actual values.
2406 * Unless the scheme type is unknown. For file schemes any percent encoded
2407 * character in the unreserved or reserved set is decoded.
2409 * 4). For File schemes if the path is starts with a drive letter and doesn't
2410 * start with a '/' then one is appended.
2411 * Ex: file://c:/test.mp3 -> file:///c:/test.mp3
2413 * 5). Dot segments are removed from the path for all scheme types
2414 * unless NO_CANONICALIZE flag is set. Dot segments aren't removed
2415 * for wildcard scheme types.
2418 * file://c:/test%20test -> file:///c:/test%2520test
2419 * file://c:/test%3Etest -> file:///c:/test%253Etest
2420 * if Uri_CREATE_FILE_USE_DOS_PATH is not set:
2421 * file:///c:/test%20test -> file:///c:/test%20test
2422 * file:///c:/test%test -> file:///c:/test%25test
2424 static DWORD
canonicalize_path_hierarchical(const WCHAR
*path
, DWORD path_len
, URL_SCHEME scheme_type
, BOOL has_host
, DWORD flags
,
2425 BOOL is_implicit_scheme
, WCHAR
*ret_path
) {
2426 const BOOL known_scheme
= scheme_type
!= URL_SCHEME_UNKNOWN
;
2427 const BOOL is_file
= scheme_type
== URL_SCHEME_FILE
;
2428 const BOOL is_res
= scheme_type
== URL_SCHEME_RES
;
2430 BOOL escape_pct
= FALSE
;
2438 if(is_file
&& !has_host
) {
2439 /* Check if a '/' needs to be appended for the file scheme. */
2440 if(path_len
> 1 && is_drive_path(ptr
) && !(flags
& Uri_CREATE_FILE_USE_DOS_PATH
)) {
2442 ret_path
[len
] = '/';
2445 } else if(*ptr
== '/') {
2446 if(!(flags
& Uri_CREATE_FILE_USE_DOS_PATH
)) {
2447 /* Copy the extra '/' over. */
2449 ret_path
[len
] = '/';
2455 if(is_drive_path(ptr
)) {
2457 ret_path
[len
] = *ptr
;
2458 /* If there's a '|' after the drive letter, convert it to a ':'. */
2459 ret_path
[len
+1] = ':';
2466 if(!is_file
&& *path
&& *path
!= '/') {
2467 /* Prepend a '/' to the path if it doesn't have one. */
2469 ret_path
[len
] = '/';
2473 for(; ptr
< path
+path_len
; ++ptr
) {
2474 BOOL do_default_action
= TRUE
;
2476 if(*ptr
== '%' && !is_res
) {
2477 const WCHAR
*tmp
= ptr
;
2480 /* Check if the % represents a valid encoded char, or if it needs encoding. */
2481 BOOL force_encode
= !check_pct_encoded(&tmp
) && is_file
&& !(flags
&Uri_CREATE_FILE_USE_DOS_PATH
);
2482 val
= decode_pct_val(ptr
);
2484 if(force_encode
|| escape_pct
) {
2485 /* Escape the percent sign in the file URI. */
2487 pct_encode_val(*ptr
, ret_path
+len
);
2489 do_default_action
= FALSE
;
2490 } else if((is_unreserved(val
) && known_scheme
) ||
2491 (is_file
&& !is_implicit_scheme
&& (is_unreserved(val
) || is_reserved(val
) ||
2492 (val
&& flags
&Uri_CREATE_FILE_USE_DOS_PATH
&& !is_forbidden_dos_path_char(val
))))) {
2494 ret_path
[len
] = val
;
2500 } else if(*ptr
== '/' && is_file
&& (flags
& Uri_CREATE_FILE_USE_DOS_PATH
)) {
2501 /* Convert the '/' back to a '\\'. */
2503 ret_path
[len
] = '\\';
2505 do_default_action
= FALSE
;
2506 } else if(*ptr
== '\\' && known_scheme
) {
2507 if(!(is_file
&& (flags
& Uri_CREATE_FILE_USE_DOS_PATH
))) {
2508 /* Convert '\\' into a '/'. */
2510 ret_path
[len
] = '/';
2512 do_default_action
= FALSE
;
2514 } else if(known_scheme
&& !is_res
&& is_ascii(*ptr
) && !is_unreserved(*ptr
) && !is_reserved(*ptr
) &&
2515 (!(flags
& Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS
) || is_file
)) {
2516 if(!is_file
|| !(flags
& Uri_CREATE_FILE_USE_DOS_PATH
)) {
2517 /* Escape the forbidden character. */
2519 pct_encode_val(*ptr
, ret_path
+len
);
2521 do_default_action
= FALSE
;
2525 if(do_default_action
) {
2527 ret_path
[len
] = *ptr
;
2532 /* Removing the dot segments only happens when it's not in
2533 * computeOnly mode and it's not a wildcard scheme. File schemes
2534 * with USE_DOS_PATH set don't get dot segments removed.
2536 if(!(is_file
&& (flags
& Uri_CREATE_FILE_USE_DOS_PATH
)) &&
2537 scheme_type
!= URL_SCHEME_WILDCARD
) {
2538 if(!(flags
& Uri_CREATE_NO_CANONICALIZE
) && ret_path
) {
2539 /* Remove the dot segments (if any) and reset everything to the new
2542 len
= remove_dot_segments(ret_path
, len
);
2547 TRACE("Canonicalized path %s len=%ld\n", debugstr_wn(ret_path
, len
), len
);
2551 /* Attempts to canonicalize the path for an opaque URI.
2553 * For known scheme types:
2554 * 1) forbidden characters are percent encoded if
2555 * NO_ENCODE_FORBIDDEN_CHARACTERS isn't set.
2557 * 2) Percent encoded, unreserved characters are decoded
2558 * to their actual values, for known scheme types.
2560 * 3) '\\' are changed to '/' for known scheme types
2561 * except for mailto schemes.
2563 * 4) For file schemes, if USE_DOS_PATH is set all '/'
2564 * are converted to backslashes.
2566 * 5) For file schemes, if USE_DOS_PATH isn't set all '\'
2567 * are converted to forward slashes.
2569 static BOOL
canonicalize_path_opaque(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
2571 const BOOL known_scheme
= data
->scheme_type
!= URL_SCHEME_UNKNOWN
;
2572 const BOOL is_file
= data
->scheme_type
== URL_SCHEME_FILE
;
2573 const BOOL is_mk
= data
->scheme_type
== URL_SCHEME_MK
;
2576 uri
->path_start
= -1;
2581 uri
->path_start
= uri
->canon_len
;
2584 /* hijack this flag for SCHEME_MK to tell the function when to start
2585 * converting slashes */
2586 flags
|= Uri_CREATE_FILE_USE_DOS_PATH
;
2589 /* For javascript: URIs, simply copy path part without any canonicalization */
2590 if(data
->scheme_type
== URL_SCHEME_JAVASCRIPT
) {
2592 memcpy(uri
->canon_uri
+uri
->canon_len
, data
->path
, data
->path_len
*sizeof(WCHAR
));
2593 uri
->path_len
= data
->path_len
;
2594 uri
->canon_len
+= data
->path_len
;
2598 /* Windows doesn't allow a "//" to appear after the scheme
2599 * of a URI, if it's an opaque URI.
2601 if(data
->scheme
&& *(data
->path
) == '/' && *(data
->path
+1) == '/') {
2602 /* So it inserts a "/." before the "//" if it exists. */
2604 uri
->canon_uri
[uri
->canon_len
] = '/';
2605 uri
->canon_uri
[uri
->canon_len
+1] = '.';
2608 uri
->canon_len
+= 2;
2611 for(ptr
= data
->path
; ptr
< data
->path
+data
->path_len
; ++ptr
) {
2612 BOOL do_default_action
= TRUE
;
2614 if(*ptr
== '%' && known_scheme
) {
2615 WCHAR val
= decode_pct_val(ptr
);
2617 if(is_unreserved(val
)) {
2619 uri
->canon_uri
[uri
->canon_len
] = val
;
2625 } else if(*ptr
== '/' && is_file
&& (flags
& Uri_CREATE_FILE_USE_DOS_PATH
)) {
2627 uri
->canon_uri
[uri
->canon_len
] = '\\';
2629 do_default_action
= FALSE
;
2630 } else if(*ptr
== '\\') {
2631 if((data
->is_relative
|| is_mk
|| is_file
) && !(flags
& Uri_CREATE_FILE_USE_DOS_PATH
)) {
2632 /* Convert to a '/'. */
2634 uri
->canon_uri
[uri
->canon_len
] = '/';
2636 do_default_action
= FALSE
;
2638 } else if(is_mk
&& *ptr
== ':' && ptr
+ 1 < data
->path
+ data
->path_len
&& *(ptr
+ 1) == ':') {
2639 flags
&= ~Uri_CREATE_FILE_USE_DOS_PATH
;
2640 } else if(known_scheme
&& is_ascii(*ptr
) && !is_unreserved(*ptr
) && !is_reserved(*ptr
) &&
2641 !(flags
& Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS
)) {
2642 if(!(is_file
&& (flags
& Uri_CREATE_FILE_USE_DOS_PATH
))) {
2644 pct_encode_val(*ptr
, uri
->canon_uri
+uri
->canon_len
);
2645 uri
->canon_len
+= 3;
2646 do_default_action
= FALSE
;
2650 if(do_default_action
) {
2652 uri
->canon_uri
[uri
->canon_len
] = *ptr
;
2657 if(is_mk
&& !computeOnly
&& !(flags
& Uri_CREATE_NO_CANONICALIZE
)) {
2658 DWORD new_len
= remove_dot_segments(uri
->canon_uri
+ uri
->path_start
,
2659 uri
->canon_len
- uri
->path_start
);
2660 uri
->canon_len
= uri
->path_start
+ new_len
;
2663 uri
->path_len
= uri
->canon_len
- uri
->path_start
;
2666 TRACE("(%p %p %lx %d): Canonicalized opaque URI path %s len=%ld\n", data
, uri
, flags
, computeOnly
,
2667 debugstr_wn(uri
->canon_uri
+uri
->path_start
, uri
->path_len
), uri
->path_len
);
2671 /* Determines how the URI represented by the parse_data should be canonicalized.
2673 * Essentially, if the parse_data represents an hierarchical URI then it calls
2674 * canonicalize_authority and the canonicalization functions for the path. If the
2675 * URI is opaque it canonicalizes the path of the URI.
2677 static BOOL
canonicalize_hierpart(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
2678 if(!data
->is_opaque
|| (data
->is_relative
&& (data
->password
|| data
->username
))) {
2679 /* "//" is only added for non-wildcard scheme types.
2681 * A "//" is only added to a relative URI if it has a
2682 * host or port component (this only happens if a IUriBuilder
2683 * is generating an IUri).
2685 if((data
->is_relative
&& (data
->host
|| data
->has_port
)) ||
2686 (!data
->is_relative
&& data
->scheme_type
!= URL_SCHEME_WILDCARD
)) {
2687 if(data
->scheme_type
== URL_SCHEME_WILDCARD
)
2691 INT pos
= uri
->canon_len
;
2693 uri
->canon_uri
[pos
] = '/';
2694 uri
->canon_uri
[pos
+1] = '/';
2696 uri
->canon_len
+= 2;
2699 if(!canonicalize_authority(data
, uri
, flags
, computeOnly
))
2702 if(data
->is_relative
&& (data
->password
|| data
->username
)) {
2703 if(!canonicalize_path_opaque(data
, uri
, flags
, computeOnly
))
2707 uri
->path_start
= uri
->canon_len
;
2708 uri
->path_len
= canonicalize_path_hierarchical(data
->path
, data
->path_len
, data
->scheme_type
, data
->host_len
!= 0,
2709 flags
, data
->has_implicit_scheme
, computeOnly
? NULL
: uri
->canon_uri
+uri
->canon_len
);
2710 uri
->canon_len
+= uri
->path_len
;
2711 if(!computeOnly
&& !uri
->path_len
)
2712 uri
->path_start
= -1;
2715 /* Opaque URI's don't have an authority. */
2716 uri
->userinfo_start
= uri
->userinfo_split
= -1;
2717 uri
->userinfo_len
= 0;
2718 uri
->host_start
= -1;
2720 uri
->host_type
= Uri_HOST_UNKNOWN
;
2721 uri
->has_port
= FALSE
;
2722 uri
->authority_start
= -1;
2723 uri
->authority_len
= 0;
2724 uri
->domain_offset
= -1;
2725 uri
->port_offset
= -1;
2727 if(is_hierarchical_scheme(data
->scheme_type
)) {
2730 /* Absolute URIs aren't displayed for known scheme types
2731 * which should be hierarchical URIs.
2733 uri
->display_modifiers
|= URI_DISPLAY_NO_ABSOLUTE_URI
;
2735 /* Windows also sets the port for these (if they have one). */
2736 for(i
= 0; i
< ARRAY_SIZE(default_ports
); ++i
) {
2737 if(data
->scheme_type
== default_ports
[i
].scheme
) {
2738 uri
->has_port
= TRUE
;
2739 uri
->port
= default_ports
[i
].port
;
2745 if(!canonicalize_path_opaque(data
, uri
, flags
, computeOnly
))
2749 if(uri
->path_start
> -1 && !computeOnly
)
2750 /* Finding file extensions happens for both types of URIs. */
2751 uri
->extension_offset
= find_file_extension(uri
->canon_uri
+uri
->path_start
, uri
->path_len
);
2753 uri
->extension_offset
= -1;
2758 /* Attempts to canonicalize the query string of the URI.
2760 * Things that happen:
2761 * 1) For known scheme types forbidden characters
2762 * are percent encoded, unless the NO_DECODE_EXTRA_INFO flag is set
2763 * or NO_ENCODE_FORBIDDEN_CHARACTERS is set.
2765 * 2) For known scheme types, percent encoded, unreserved characters
2766 * are decoded as long as the NO_DECODE_EXTRA_INFO flag isn't set.
2768 static BOOL
canonicalize_query(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
2769 const WCHAR
*ptr
, *end
;
2770 const BOOL known_scheme
= data
->scheme_type
!= URL_SCHEME_UNKNOWN
;
2773 uri
->query_start
= -1;
2778 uri
->query_start
= uri
->canon_len
;
2780 end
= data
->query
+data
->query_len
;
2781 for(ptr
= data
->query
; ptr
< end
; ++ptr
) {
2783 if(known_scheme
&& !(flags
& Uri_CREATE_NO_DECODE_EXTRA_INFO
)) {
2784 WCHAR val
= decode_pct_val(ptr
);
2785 if(is_unreserved(val
)) {
2787 uri
->canon_uri
[uri
->canon_len
] = val
;
2794 } else if(known_scheme
&& is_ascii(*ptr
) && !is_unreserved(*ptr
) && !is_reserved(*ptr
)) {
2795 if(!(flags
& Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS
) &&
2796 !(flags
& Uri_CREATE_NO_DECODE_EXTRA_INFO
)) {
2798 pct_encode_val(*ptr
, uri
->canon_uri
+uri
->canon_len
);
2799 uri
->canon_len
+= 3;
2805 uri
->canon_uri
[uri
->canon_len
] = *ptr
;
2809 uri
->query_len
= uri
->canon_len
- uri
->query_start
;
2812 TRACE("(%p %p %lx %d): Canonicalized query string %s len=%ld\n", data
, uri
, flags
,
2813 computeOnly
, debugstr_wn(uri
->canon_uri
+uri
->query_start
, uri
->query_len
),
2818 static BOOL
canonicalize_fragment(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
2819 const WCHAR
*ptr
, *end
;
2820 const BOOL known_scheme
= data
->scheme_type
!= URL_SCHEME_UNKNOWN
;
2822 if(!data
->fragment
) {
2823 uri
->fragment_start
= -1;
2824 uri
->fragment_len
= 0;
2828 uri
->fragment_start
= uri
->canon_len
;
2830 end
= data
->fragment
+ data
->fragment_len
;
2831 for(ptr
= data
->fragment
; ptr
< end
; ++ptr
) {
2833 if(known_scheme
&& !(flags
& Uri_CREATE_NO_DECODE_EXTRA_INFO
)) {
2834 WCHAR val
= decode_pct_val(ptr
);
2835 if(is_unreserved(val
)) {
2837 uri
->canon_uri
[uri
->canon_len
] = val
;
2844 } else if(known_scheme
&& is_ascii(*ptr
) && !is_unreserved(*ptr
) && !is_reserved(*ptr
)) {
2845 if(!(flags
& Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS
) &&
2846 !(flags
& Uri_CREATE_NO_DECODE_EXTRA_INFO
)) {
2848 pct_encode_val(*ptr
, uri
->canon_uri
+uri
->canon_len
);
2849 uri
->canon_len
+= 3;
2855 uri
->canon_uri
[uri
->canon_len
] = *ptr
;
2859 uri
->fragment_len
= uri
->canon_len
- uri
->fragment_start
;
2862 TRACE("(%p %p %lx %d): Canonicalized fragment %s len=%ld\n", data
, uri
, flags
,
2863 computeOnly
, debugstr_wn(uri
->canon_uri
+uri
->fragment_start
, uri
->fragment_len
),
2868 /* Canonicalizes the scheme information specified in the parse_data using the specified flags. */
2869 static BOOL
canonicalize_scheme(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
2870 uri
->scheme_start
= -1;
2871 uri
->scheme_len
= 0;
2874 /* The only type of URI that doesn't have to have a scheme is a relative
2877 if(!data
->is_relative
) {
2878 FIXME("(%p %p %lx): Unable to determine the scheme type of %s.\n", data
,
2879 uri
, flags
, debugstr_w(data
->uri
));
2885 INT pos
= uri
->canon_len
;
2887 for(i
= 0; i
< data
->scheme_len
; ++i
) {
2888 /* Scheme name must be lower case after canonicalization. */
2889 uri
->canon_uri
[i
+ pos
] = towlower(data
->scheme
[i
]);
2892 uri
->canon_uri
[i
+ pos
] = ':';
2893 uri
->scheme_start
= pos
;
2895 TRACE("(%p %p %lx): Canonicalized scheme=%s, len=%ld.\n", data
, uri
, flags
,
2896 debugstr_wn(uri
->canon_uri
+uri
->scheme_start
, data
->scheme_len
), data
->scheme_len
);
2899 /* This happens in both computation modes. */
2900 uri
->canon_len
+= data
->scheme_len
+ 1;
2901 uri
->scheme_len
= data
->scheme_len
;
2906 /* Computes what the length of the URI specified by the parse_data will be
2907 * after canonicalization occurs using the specified flags.
2909 * This function will return a non-zero value indicating the length of the canonicalized
2910 * URI, or -1 on error.
2912 static int compute_canonicalized_length(const parse_data
*data
, DWORD flags
) {
2915 memset(&uri
, 0, sizeof(Uri
));
2917 TRACE("(%p %lx): Beginning to compute canonicalized length for URI %s\n", data
, flags
,
2918 debugstr_w(data
->uri
));
2920 if(!canonicalize_scheme(data
, &uri
, flags
, TRUE
)) {
2921 ERR("(%p %lx): Failed to compute URI scheme length.\n", data
, flags
);
2925 if(!canonicalize_hierpart(data
, &uri
, flags
, TRUE
)) {
2926 ERR("(%p %lx): Failed to compute URI hierpart length.\n", data
, flags
);
2930 if(!canonicalize_query(data
, &uri
, flags
, TRUE
)) {
2931 ERR("(%p %lx): Failed to compute query string length.\n", data
, flags
);
2935 if(!canonicalize_fragment(data
, &uri
, flags
, TRUE
)) {
2936 ERR("(%p %lx): Failed to compute fragment length.\n", data
, flags
);
2940 TRACE("(%p %lx): Finished computing canonicalized URI length. length=%ld\n", data
, flags
, uri
.canon_len
);
2942 return uri
.canon_len
;
2945 /* Canonicalizes the URI data specified in the parse_data, using the given flags. If the
2946 * canonicalization succeeds it will store all the canonicalization information
2947 * in the pointer to the Uri.
2949 * To canonicalize a URI this function first computes what the length of the URI
2950 * specified by the parse_data will be. Once this is done it will then perform the actual
2951 * canonicalization of the URI.
2953 static HRESULT
canonicalize_uri(const parse_data
*data
, Uri
*uri
, DWORD flags
) {
2956 uri
->canon_uri
= NULL
;
2957 uri
->canon_size
= uri
->canon_len
= 0;
2959 TRACE("(%p %p %lx): beginning to canonicalize URI %s.\n", data
, uri
, flags
, debugstr_w(data
->uri
));
2961 /* First try to compute the length of the URI. */
2962 len
= compute_canonicalized_length(data
, flags
);
2964 ERR("(%p %p %lx): Could not compute the canonicalized length of %s.\n", data
, uri
, flags
,
2965 debugstr_w(data
->uri
));
2966 return E_INVALIDARG
;
2969 uri
->canon_uri
= malloc((len
+ 1) * sizeof(WCHAR
));
2971 return E_OUTOFMEMORY
;
2973 uri
->canon_size
= len
;
2974 if(!canonicalize_scheme(data
, uri
, flags
, FALSE
)) {
2975 ERR("(%p %p %lx): Unable to canonicalize the scheme of the URI.\n", data
, uri
, flags
);
2976 return E_INVALIDARG
;
2978 uri
->scheme_type
= data
->scheme_type
;
2980 if(!canonicalize_hierpart(data
, uri
, flags
, FALSE
)) {
2981 ERR("(%p %p %lx): Unable to canonicalize the hierpart of the URI\n", data
, uri
, flags
);
2982 return E_INVALIDARG
;
2985 if(!canonicalize_query(data
, uri
, flags
, FALSE
)) {
2986 ERR("(%p %p %lx): Unable to canonicalize query string of the URI.\n",
2988 return E_INVALIDARG
;
2991 if(!canonicalize_fragment(data
, uri
, flags
, FALSE
)) {
2992 ERR("(%p %p %lx): Unable to canonicalize fragment of the URI.\n",
2994 return E_INVALIDARG
;
2997 /* There's a possibility we didn't use all the space we allocated
3000 if(uri
->canon_len
< uri
->canon_size
) {
3001 /* This happens if the URI is hierarchical and dot
3002 * segments were removed from its path.
3004 WCHAR
*tmp
= realloc(uri
->canon_uri
, (uri
->canon_len
+ 1) * sizeof(WCHAR
));
3006 return E_OUTOFMEMORY
;
3008 uri
->canon_uri
= tmp
;
3009 uri
->canon_size
= uri
->canon_len
;
3012 uri
->canon_uri
[uri
->canon_len
] = '\0';
3013 TRACE("(%p %p %lx): finished canonicalizing the URI. uri=%s\n", data
, uri
, flags
, debugstr_w(uri
->canon_uri
));
3018 static HRESULT
get_builder_component(LPWSTR
*component
, DWORD
*component_len
,
3019 LPCWSTR source
, DWORD source_len
,
3020 LPCWSTR
*output
, DWORD
*output_len
)
3033 if(!(*component
) && source
) {
3034 /* Allocate 'component', and copy the contents from 'source'
3035 * into the new allocation.
3037 *component
= malloc((source_len
+ 1) * sizeof(WCHAR
));
3039 return E_OUTOFMEMORY
;
3041 memcpy(*component
, source
, source_len
*sizeof(WCHAR
));
3042 (*component
)[source_len
] = '\0';
3043 *component_len
= source_len
;
3046 *output
= *component
;
3047 *output_len
= *component_len
;
3048 return *output
? S_OK
: S_FALSE
;
3051 /* Allocates 'component' and copies the string from 'new_value' into 'component'.
3052 * If 'prefix' is set and 'new_value' isn't NULL, then it checks if 'new_value'
3053 * starts with 'prefix'. If it doesn't then 'prefix' is prepended to 'component'.
3055 * If everything is successful, then will set 'success_flag' in 'flags'.
3057 static HRESULT
set_builder_component(LPWSTR
*component
, DWORD
*component_len
, LPCWSTR new_value
,
3058 WCHAR prefix
, DWORD
*flags
, DWORD success_flag
)
3066 BOOL add_prefix
= FALSE
;
3067 DWORD len
= lstrlenW(new_value
);
3070 if(prefix
&& *new_value
!= prefix
) {
3072 *component
= malloc((len
+ 2) * sizeof(WCHAR
));
3074 *component
= malloc((len
+ 1) * sizeof(WCHAR
));
3077 return E_OUTOFMEMORY
;
3080 (*component
)[pos
++] = prefix
;
3082 memcpy(*component
+pos
, new_value
, (len
+1)*sizeof(WCHAR
));
3083 *component_len
= len
+pos
;
3086 *flags
|= success_flag
;
3090 static void reset_builder(UriBuilder
*builder
) {
3092 IUri_Release(&builder
->uri
->IUri_iface
);
3093 builder
->uri
= NULL
;
3095 free(builder
->fragment
);
3096 builder
->fragment
= NULL
;
3097 builder
->fragment_len
= 0;
3099 free(builder
->host
);
3100 builder
->host
= NULL
;
3101 builder
->host_len
= 0;
3103 free(builder
->password
);
3104 builder
->password
= NULL
;
3105 builder
->password_len
= 0;
3107 free(builder
->path
);
3108 builder
->path
= NULL
;
3109 builder
->path_len
= 0;
3111 free(builder
->query
);
3112 builder
->query
= NULL
;
3113 builder
->query_len
= 0;
3115 free(builder
->scheme
);
3116 builder
->scheme
= NULL
;
3117 builder
->scheme_len
= 0;
3119 free(builder
->username
);
3120 builder
->username
= NULL
;
3121 builder
->username_len
= 0;
3123 builder
->has_port
= FALSE
;
3125 builder
->modified_props
= 0;
3128 static HRESULT
validate_scheme_name(const UriBuilder
*builder
, parse_data
*data
, DWORD flags
) {
3129 const WCHAR
*component
;
3134 if(builder
->scheme
) {
3135 ptr
= builder
->scheme
;
3136 expected_len
= builder
->scheme_len
;
3137 } else if(builder
->uri
&& builder
->uri
->scheme_start
> -1) {
3138 ptr
= builder
->uri
->canon_uri
+builder
->uri
->scheme_start
;
3139 expected_len
= builder
->uri
->scheme_len
;
3147 if(parse_scheme(pptr
, data
, flags
, ALLOW_NULL_TERM_SCHEME
) &&
3148 data
->scheme_len
== expected_len
) {
3150 TRACE("(%p %p %lx): Found valid scheme component %s len=%ld.\n", builder
, data
, flags
,
3151 debugstr_wn(data
->scheme
, data
->scheme_len
), data
->scheme_len
);
3153 TRACE("(%p %p %lx): Invalid scheme component found %s.\n", builder
, data
, flags
,
3154 debugstr_wn(component
, expected_len
));
3155 return INET_E_INVALID_URL
;
3161 static HRESULT
validate_username(const UriBuilder
*builder
, parse_data
*data
, DWORD flags
) {
3166 if(builder
->username
) {
3167 ptr
= builder
->username
;
3168 expected_len
= builder
->username_len
;
3169 } else if(!(builder
->modified_props
& Uri_HAS_USER_NAME
) && builder
->uri
&&
3170 builder
->uri
->userinfo_start
> -1 && builder
->uri
->userinfo_split
!= 0) {
3171 /* Just use the username from the base Uri. */
3172 data
->username
= builder
->uri
->canon_uri
+builder
->uri
->userinfo_start
;
3173 data
->username_len
= (builder
->uri
->userinfo_split
> -1) ?
3174 builder
->uri
->userinfo_split
: builder
->uri
->userinfo_len
;
3182 const WCHAR
*component
= ptr
;
3184 if(parse_username(pptr
, data
, flags
, ALLOW_NULL_TERM_USER_NAME
) &&
3185 data
->username_len
== expected_len
)
3186 TRACE("(%p %p %lx): Found valid username component %s len=%ld.\n", builder
, data
, flags
,
3187 debugstr_wn(data
->username
, data
->username_len
), data
->username_len
);
3189 TRACE("(%p %p %lx): Invalid username component found %s.\n", builder
, data
, flags
,
3190 debugstr_wn(component
, expected_len
));
3191 return INET_E_INVALID_URL
;
3198 static HRESULT
validate_password(const UriBuilder
*builder
, parse_data
*data
, DWORD flags
) {
3203 if(builder
->password
) {
3204 ptr
= builder
->password
;
3205 expected_len
= builder
->password_len
;
3206 } else if(!(builder
->modified_props
& Uri_HAS_PASSWORD
) && builder
->uri
&&
3207 builder
->uri
->userinfo_split
> -1) {
3208 data
->password
= builder
->uri
->canon_uri
+builder
->uri
->userinfo_start
+builder
->uri
->userinfo_split
+1;
3209 data
->password_len
= builder
->uri
->userinfo_len
-builder
->uri
->userinfo_split
-1;
3217 const WCHAR
*component
= ptr
;
3219 if(parse_password(pptr
, data
, flags
, ALLOW_NULL_TERM_PASSWORD
) &&
3220 data
->password_len
== expected_len
)
3221 TRACE("(%p %p %lx): Found valid password component %s len=%ld.\n", builder
, data
, flags
,
3222 debugstr_wn(data
->password
, data
->password_len
), data
->password_len
);
3224 TRACE("(%p %p %lx): Invalid password component found %s.\n", builder
, data
, flags
,
3225 debugstr_wn(component
, expected_len
));
3226 return INET_E_INVALID_URL
;
3233 static HRESULT
validate_userinfo(const UriBuilder
*builder
, parse_data
*data
, DWORD flags
) {
3236 hr
= validate_username(builder
, data
, flags
);
3240 hr
= validate_password(builder
, data
, flags
);
3247 static HRESULT
validate_host(const UriBuilder
*builder
, parse_data
*data
) {
3253 ptr
= builder
->host
;
3254 expected_len
= builder
->host_len
;
3255 } else if(!(builder
->modified_props
& Uri_HAS_HOST
) && builder
->uri
&& builder
->uri
->host_start
> -1) {
3256 ptr
= builder
->uri
->canon_uri
+ builder
->uri
->host_start
;
3257 expected_len
= builder
->uri
->host_len
;
3262 const WCHAR
*component
= ptr
;
3263 DWORD extras
= ALLOW_BRACKETLESS_IP_LITERAL
|IGNORE_PORT_DELIMITER
|SKIP_IP_FUTURE_CHECK
;
3266 if(parse_host(pptr
, data
, extras
) && data
->host_len
== expected_len
)
3267 TRACE("(%p %p): Found valid host name %s len=%ld type=%d.\n", builder
, data
,
3268 debugstr_wn(data
->host
, data
->host_len
), data
->host_len
, data
->host_type
);
3270 TRACE("(%p %p): Invalid host name found %s.\n", builder
, data
,
3271 debugstr_wn(component
, expected_len
));
3272 return INET_E_INVALID_URL
;
3279 static void setup_port(const UriBuilder
*builder
, parse_data
*data
, DWORD flags
) {
3280 if(builder
->modified_props
& Uri_HAS_PORT
) {
3281 if(builder
->has_port
) {
3282 data
->has_port
= TRUE
;
3283 data
->port_value
= builder
->port
;
3285 } else if(builder
->uri
&& builder
->uri
->has_port
) {
3286 data
->has_port
= TRUE
;
3287 data
->port_value
= builder
->uri
->port
;
3291 TRACE("(%p %p %lx): Using %lu as port for IUri.\n", builder
, data
, flags
, data
->port_value
);
3294 static HRESULT
validate_path(const UriBuilder
*builder
, parse_data
*data
, DWORD flags
) {
3295 const WCHAR
*ptr
= NULL
;
3296 const WCHAR
*component
;
3299 BOOL check_len
= TRUE
;
3303 ptr
= builder
->path
;
3304 expected_len
= builder
->path_len
;
3305 } else if(!(builder
->modified_props
& Uri_HAS_PATH
) &&
3306 builder
->uri
&& builder
->uri
->path_start
> -1) {
3307 ptr
= builder
->uri
->canon_uri
+builder
->uri
->path_start
;
3308 expected_len
= builder
->uri
->path_len
;
3318 /* How the path is validated depends on what type of
3321 valid
= data
->is_opaque
?
3322 parse_path_opaque(pptr
, data
, flags
) : parse_path_hierarchical(pptr
, data
, flags
);
3324 if(!valid
|| (check_len
&& expected_len
!= data
->path_len
)) {
3325 TRACE("(%p %p %lx): Invalid path component %s.\n", builder
, data
, flags
,
3326 debugstr_wn(component
, expected_len
) );
3327 return INET_E_INVALID_URL
;
3330 TRACE("(%p %p %lx): Valid path component %s len=%ld.\n", builder
, data
, flags
,
3331 debugstr_wn(data
->path
, data
->path_len
), data
->path_len
);
3336 static HRESULT
validate_query(const UriBuilder
*builder
, parse_data
*data
, DWORD flags
) {
3337 const WCHAR
*ptr
= NULL
;
3341 if(builder
->query
) {
3342 ptr
= builder
->query
;
3343 expected_len
= builder
->query_len
;
3344 } else if(!(builder
->modified_props
& Uri_HAS_QUERY
) && builder
->uri
&&
3345 builder
->uri
->query_start
> -1) {
3346 ptr
= builder
->uri
->canon_uri
+builder
->uri
->query_start
;
3347 expected_len
= builder
->uri
->query_len
;
3351 const WCHAR
*component
= ptr
;
3354 if(parse_query(pptr
, data
, flags
) && expected_len
== data
->query_len
)
3355 TRACE("(%p %p %lx): Valid query component %s len=%ld.\n", builder
, data
, flags
,
3356 debugstr_wn(data
->query
, data
->query_len
), data
->query_len
);
3358 TRACE("(%p %p %lx): Invalid query component %s.\n", builder
, data
, flags
,
3359 debugstr_wn(component
, expected_len
));
3360 return INET_E_INVALID_URL
;
3367 static HRESULT
validate_fragment(const UriBuilder
*builder
, parse_data
*data
, DWORD flags
) {
3368 const WCHAR
*ptr
= NULL
;
3372 if(builder
->fragment
) {
3373 ptr
= builder
->fragment
;
3374 expected_len
= builder
->fragment_len
;
3375 } else if(!(builder
->modified_props
& Uri_HAS_FRAGMENT
) && builder
->uri
&&
3376 builder
->uri
->fragment_start
> -1) {
3377 ptr
= builder
->uri
->canon_uri
+builder
->uri
->fragment_start
;
3378 expected_len
= builder
->uri
->fragment_len
;
3382 const WCHAR
*component
= ptr
;
3385 if(parse_fragment(pptr
, data
, flags
) && expected_len
== data
->fragment_len
)
3386 TRACE("(%p %p %lx): Valid fragment component %s len=%ld.\n", builder
, data
, flags
,
3387 debugstr_wn(data
->fragment
, data
->fragment_len
), data
->fragment_len
);
3389 TRACE("(%p %p %lx): Invalid fragment component %s.\n", builder
, data
, flags
,
3390 debugstr_wn(component
, expected_len
));
3391 return INET_E_INVALID_URL
;
3398 static HRESULT
validate_components(const UriBuilder
*builder
, parse_data
*data
, DWORD flags
) {
3401 memset(data
, 0, sizeof(parse_data
));
3403 TRACE("(%p %p %lx): Beginning to validate builder components.\n", builder
, data
, flags
);
3405 hr
= validate_scheme_name(builder
, data
, flags
);
3409 /* Extra validation for file schemes. */
3410 if(data
->scheme_type
== URL_SCHEME_FILE
) {
3411 if((builder
->password
|| (builder
->uri
&& builder
->uri
->userinfo_split
> -1)) ||
3412 (builder
->username
|| (builder
->uri
&& builder
->uri
->userinfo_start
> -1))) {
3413 TRACE("(%p %p %lx): File schemes can't contain a username or password.\n",
3414 builder
, data
, flags
);
3415 return INET_E_INVALID_URL
;
3419 hr
= validate_userinfo(builder
, data
, flags
);
3423 hr
= validate_host(builder
, data
);
3427 setup_port(builder
, data
, flags
);
3429 /* The URI is opaque if it doesn't have an authority component. */
3430 if(!data
->is_relative
)
3431 data
->is_opaque
= !data
->username
&& !data
->password
&& !data
->host
&& !data
->has_port
3432 && data
->scheme_type
!= URL_SCHEME_FILE
;
3434 data
->is_opaque
= !data
->host
&& !data
->has_port
;
3436 hr
= validate_path(builder
, data
, flags
);
3440 hr
= validate_query(builder
, data
, flags
);
3444 hr
= validate_fragment(builder
, data
, flags
);
3448 TRACE("(%p %p %lx): Finished validating builder components.\n", builder
, data
, flags
);
3453 static HRESULT
compare_file_paths(const Uri
*a
, const Uri
*b
, BOOL
*ret
)
3455 WCHAR
*canon_path_a
, *canon_path_b
;
3459 *ret
= !b
->path_len
;
3469 if(a
->path_len
== b
->path_len
&& !wcsnicmp(a
->canon_uri
+a
->path_start
, b
->canon_uri
+b
->path_start
, a
->path_len
)) {
3474 len_a
= canonicalize_path_hierarchical(a
->canon_uri
+a
->path_start
, a
->path_len
, a
->scheme_type
, FALSE
, 0, FALSE
, NULL
);
3475 len_b
= canonicalize_path_hierarchical(b
->canon_uri
+b
->path_start
, b
->path_len
, b
->scheme_type
, FALSE
, 0, FALSE
, NULL
);
3477 canon_path_a
= malloc(len_a
* sizeof(WCHAR
));
3479 return E_OUTOFMEMORY
;
3480 canon_path_b
= malloc(len_b
* sizeof(WCHAR
));
3483 return E_OUTOFMEMORY
;
3486 len_a
= canonicalize_path_hierarchical(a
->canon_uri
+a
->path_start
, a
->path_len
, a
->scheme_type
, FALSE
, 0, FALSE
, canon_path_a
);
3487 len_b
= canonicalize_path_hierarchical(b
->canon_uri
+b
->path_start
, b
->path_len
, b
->scheme_type
, FALSE
, 0, FALSE
, canon_path_b
);
3489 *ret
= len_a
== len_b
&& !wcsnicmp(canon_path_a
, canon_path_b
, len_a
);
3496 /* Checks if the two Uri's are logically equivalent. It's a simple
3497 * comparison, since they are both of type Uri, and it can access
3498 * the properties of each Uri directly without the need to go
3499 * through the "IUri_Get*" interface calls.
3501 static HRESULT
compare_uris(const Uri
*a
, const Uri
*b
, BOOL
*ret
) {
3502 const BOOL known_scheme
= a
->scheme_type
!= URL_SCHEME_UNKNOWN
;
3503 const BOOL are_hierarchical
= a
->authority_start
> -1 && b
->authority_start
> -1;
3508 if(a
->scheme_type
!= b
->scheme_type
)
3511 /* Only compare the scheme names (if any) if their unknown scheme types. */
3513 if((a
->scheme_start
> -1 && b
->scheme_start
> -1) &&
3514 (a
->scheme_len
== b
->scheme_len
)) {
3515 /* Make sure the schemes are the same. */
3516 if(StrCmpNW(a
->canon_uri
+a
->scheme_start
, b
->canon_uri
+b
->scheme_start
, a
->scheme_len
))
3518 } else if(a
->scheme_len
!= b
->scheme_len
)
3519 /* One of the Uri's has a scheme name, while the other doesn't. */
3523 /* If they have a userinfo component, perform case sensitive compare. */
3524 if((a
->userinfo_start
> -1 && b
->userinfo_start
> -1) &&
3525 (a
->userinfo_len
== b
->userinfo_len
)) {
3526 if(StrCmpNW(a
->canon_uri
+a
->userinfo_start
, b
->canon_uri
+b
->userinfo_start
, a
->userinfo_len
))
3528 } else if(a
->userinfo_len
!= b
->userinfo_len
)
3529 /* One of the Uri's had a userinfo, while the other one doesn't. */
3532 /* Check if they have a host name. */
3533 if((a
->host_start
> -1 && b
->host_start
> -1) &&
3534 (a
->host_len
== b
->host_len
)) {
3535 /* Perform a case insensitive compare if they are a known scheme type. */
3537 if(StrCmpNIW(a
->canon_uri
+a
->host_start
, b
->canon_uri
+b
->host_start
, a
->host_len
))
3539 } else if(StrCmpNW(a
->canon_uri
+a
->host_start
, b
->canon_uri
+b
->host_start
, a
->host_len
))
3541 } else if(a
->host_len
!= b
->host_len
)
3542 /* One of the Uri's had a host, while the other one didn't. */
3545 if(a
->has_port
&& b
->has_port
) {
3546 if(a
->port
!= b
->port
)
3548 } else if(a
->has_port
|| b
->has_port
)
3549 /* One had a port, while the other one didn't. */
3552 /* Windows is weird with how it handles paths. For example
3553 * One URI could be "http://google.com" (after canonicalization)
3554 * and one could be "http://google.com/" and the IsEqual function
3555 * would still evaluate to TRUE, but, only if they are both hierarchical
3558 if(a
->scheme_type
== URL_SCHEME_FILE
) {
3561 hres
= compare_file_paths(a
, b
, &cmp
);
3562 if(FAILED(hres
) || !cmp
)
3564 } else if((a
->path_start
> -1 && b
->path_start
> -1) &&
3565 (a
->path_len
== b
->path_len
)) {
3566 if(StrCmpNW(a
->canon_uri
+a
->path_start
, b
->canon_uri
+b
->path_start
, a
->path_len
))
3568 } else if(are_hierarchical
&& a
->path_len
== -1 && b
->path_len
== 0) {
3569 if(*(a
->canon_uri
+a
->path_start
) != '/')
3571 } else if(are_hierarchical
&& b
->path_len
== 1 && a
->path_len
== 0) {
3572 if(*(b
->canon_uri
+b
->path_start
) != '/')
3574 } else if(a
->path_len
!= b
->path_len
)
3577 /* Compare the query strings of the two URIs. */
3578 if((a
->query_start
> -1 && b
->query_start
> -1) &&
3579 (a
->query_len
== b
->query_len
)) {
3580 if(StrCmpNW(a
->canon_uri
+a
->query_start
, b
->canon_uri
+b
->query_start
, a
->query_len
))
3582 } else if(a
->query_len
!= b
->query_len
)
3585 if((a
->fragment_start
> -1 && b
->fragment_start
> -1) &&
3586 (a
->fragment_len
== b
->fragment_len
)) {
3587 if(StrCmpNW(a
->canon_uri
+a
->fragment_start
, b
->canon_uri
+b
->fragment_start
, a
->fragment_len
))
3589 } else if(a
->fragment_len
!= b
->fragment_len
)
3592 /* If we get here, the two URIs are equivalent. */
3597 static void convert_to_dos_path(const WCHAR
*path
, DWORD path_len
,
3598 WCHAR
*output
, DWORD
*output_len
)
3600 const WCHAR
*ptr
= path
;
3602 if(path_len
> 3 && *ptr
== '/' && is_drive_path(path
+1))
3603 /* Skip over the leading / before the drive path. */
3606 for(; ptr
< path
+path_len
; ++ptr
) {
3619 /* Generates a raw uri string using the parse_data. */
3620 static DWORD
generate_raw_uri(const parse_data
*data
, BSTR uri
, DWORD flags
) {
3625 memcpy(uri
, data
->scheme
, data
->scheme_len
*sizeof(WCHAR
));
3626 uri
[data
->scheme_len
] = ':';
3628 length
+= data
->scheme_len
+1;
3631 if(!data
->is_opaque
) {
3632 /* For the "//" which appears before the authority component. */
3635 uri
[length
+1] = '/';
3639 /* Check if we need to add the "\\" before the host name
3640 * of a UNC server name in a DOS path.
3642 if(flags
& RAW_URI_CONVERT_TO_DOS_PATH
&&
3643 data
->scheme_type
== URL_SCHEME_FILE
&& data
->host
) {
3646 uri
[length
+1] = '\\';
3652 if(data
->username
) {
3654 memcpy(uri
+length
, data
->username
, data
->username_len
*sizeof(WCHAR
));
3655 length
+= data
->username_len
;
3658 if(data
->password
) {
3661 memcpy(uri
+length
+1, data
->password
, data
->password_len
*sizeof(WCHAR
));
3663 length
+= data
->password_len
+1;
3666 if(data
->password
|| data
->username
) {
3673 /* IPv6 addresses get the brackets added around them if they don't already
3676 const BOOL add_brackets
= data
->host_type
== Uri_HOST_IPV6
&& *(data
->host
) != '[';
3684 memcpy(uri
+length
, data
->host
, data
->host_len
*sizeof(WCHAR
));
3685 length
+= data
->host_len
;
3694 if(data
->has_port
) {
3695 /* The port isn't included in the raw uri if it's the default
3696 * port for the scheme type.
3699 BOOL is_default
= FALSE
;
3701 for(i
= 0; i
< ARRAY_SIZE(default_ports
); ++i
) {
3702 if(data
->scheme_type
== default_ports
[i
].scheme
&&
3703 data
->port_value
== default_ports
[i
].port
)
3707 if(!is_default
|| flags
& RAW_URI_FORCE_PORT_DISP
) {
3713 length
+= ui2str(uri
+length
, data
->port_value
);
3715 length
+= ui2str(NULL
, data
->port_value
);
3719 /* Check if a '/' should be added before the path for hierarchical URIs. */
3720 if(!data
->is_opaque
&& data
->path
&& *(data
->path
) != '/') {
3727 if(!data
->is_opaque
&& data
->scheme_type
== URL_SCHEME_FILE
&&
3728 flags
& RAW_URI_CONVERT_TO_DOS_PATH
) {
3732 convert_to_dos_path(data
->path
, data
->path_len
, uri
+length
, &len
);
3734 convert_to_dos_path(data
->path
, data
->path_len
, NULL
, &len
);
3739 memcpy(uri
+length
, data
->path
, data
->path_len
*sizeof(WCHAR
));
3740 length
+= data
->path_len
;
3746 memcpy(uri
+length
, data
->query
, data
->query_len
*sizeof(WCHAR
));
3747 length
+= data
->query_len
;
3750 if(data
->fragment
) {
3752 memcpy(uri
+length
, data
->fragment
, data
->fragment_len
*sizeof(WCHAR
));
3753 length
+= data
->fragment_len
;
3757 TRACE("(%p %p): Generated raw uri=%s len=%ld\n", data
, uri
, debugstr_wn(uri
, length
), length
);
3759 TRACE("(%p %p): Computed raw uri len=%ld\n", data
, uri
, length
);
3764 static HRESULT
generate_uri(const UriBuilder
*builder
, const parse_data
*data
, Uri
*uri
, DWORD flags
) {
3766 DWORD length
= generate_raw_uri(data
, NULL
, 0);
3767 uri
->raw_uri
= SysAllocStringLen(NULL
, length
);
3769 return E_OUTOFMEMORY
;
3771 generate_raw_uri(data
, uri
->raw_uri
, 0);
3773 hr
= canonicalize_uri(data
, uri
, flags
);
3775 if(hr
== E_INVALIDARG
)
3776 return INET_E_INVALID_URL
;
3780 uri
->create_flags
= flags
;
3784 static inline Uri
* impl_from_IUri(IUri
*iface
)
3786 return CONTAINING_RECORD(iface
, Uri
, IUri_iface
);
3789 static inline void destroy_uri_obj(Uri
*This
)
3791 SysFreeString(This
->raw_uri
);
3792 free(This
->canon_uri
);
3796 static HRESULT WINAPI
Uri_QueryInterface(IUri
*iface
, REFIID riid
, void **ppv
)
3798 Uri
*This
= impl_from_IUri(iface
);
3800 if(IsEqualGUID(&IID_IUnknown
, riid
)) {
3801 TRACE("(%p)->(IID_IUnknown %p)\n", This
, ppv
);
3802 *ppv
= &This
->IUri_iface
;
3803 }else if(IsEqualGUID(&IID_IUri
, riid
)) {
3804 TRACE("(%p)->(IID_IUri %p)\n", This
, ppv
);
3805 *ppv
= &This
->IUri_iface
;
3806 }else if(IsEqualGUID(&IID_IUriBuilderFactory
, riid
)) {
3807 TRACE("(%p)->(IID_IUriBuilderFactory %p)\n", This
, ppv
);
3808 *ppv
= &This
->IUriBuilderFactory_iface
;
3809 }else if(IsEqualGUID(&IID_IPersistStream
, riid
)) {
3810 TRACE("(%p)->(IID_IPersistStream %p)\n", This
, ppv
);
3811 *ppv
= &This
->IPersistStream_iface
;
3812 }else if(IsEqualGUID(&IID_IMarshal
, riid
)) {
3813 TRACE("(%p)->(IID_IMarshal %p)\n", This
, ppv
);
3814 *ppv
= &This
->IMarshal_iface
;
3815 }else if(IsEqualGUID(&IID_IUriObj
, riid
)) {
3816 TRACE("(%p)->(IID_IUriObj %p)\n", This
, ppv
);
3820 TRACE("(%p)->(%s %p)\n", This
, debugstr_guid(riid
), ppv
);
3822 return E_NOINTERFACE
;
3825 IUnknown_AddRef((IUnknown
*)*ppv
);
3829 static ULONG WINAPI
Uri_AddRef(IUri
*iface
)
3831 Uri
*This
= impl_from_IUri(iface
);
3832 LONG ref
= InterlockedIncrement(&This
->ref
);
3834 TRACE("(%p) ref=%ld\n", This
, ref
);
3839 static ULONG WINAPI
Uri_Release(IUri
*iface
)
3841 Uri
*This
= impl_from_IUri(iface
);
3842 LONG ref
= InterlockedDecrement(&This
->ref
);
3844 TRACE("(%p) ref=%ld\n", This
, ref
);
3847 destroy_uri_obj(This
);
3852 static HRESULT WINAPI
Uri_GetPropertyBSTR(IUri
*iface
, Uri_PROPERTY uriProp
, BSTR
*pbstrProperty
, DWORD dwFlags
)
3854 Uri
*This
= impl_from_IUri(iface
);
3856 TRACE("(%p %s)->(%d %p %lx)\n", This
, debugstr_w(This
->canon_uri
), uriProp
, pbstrProperty
, dwFlags
);
3858 if(!This
->create_flags
)
3859 return E_UNEXPECTED
;
3863 if(uriProp
> Uri_PROPERTY_STRING_LAST
) {
3864 /* It only returns S_FALSE for the ZONE property... */
3865 if(uriProp
== Uri_PROPERTY_ZONE
) {
3866 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
3867 if(!(*pbstrProperty
))
3868 return E_OUTOFMEMORY
;
3872 *pbstrProperty
= NULL
;
3873 return E_INVALIDARG
;
3876 /* Don't have support for flags yet. */
3878 FIXME("(%p)->(%d %p %lx)\n", This
, uriProp
, pbstrProperty
, dwFlags
);
3883 case Uri_PROPERTY_ABSOLUTE_URI
:
3884 if(This
->display_modifiers
& URI_DISPLAY_NO_ABSOLUTE_URI
) {
3885 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
3888 if(This
->scheme_type
!= URL_SCHEME_UNKNOWN
&& This
->userinfo_start
> -1) {
3889 if(This
->userinfo_len
== 0) {
3890 /* Don't include the '@' after the userinfo component. */
3891 *pbstrProperty
= SysAllocStringLen(NULL
, This
->canon_len
-1);
3893 if(*pbstrProperty
) {
3894 /* Copy everything before it. */
3895 memcpy(*pbstrProperty
, This
->canon_uri
, This
->userinfo_start
*sizeof(WCHAR
));
3897 /* And everything after it. */
3898 memcpy(*pbstrProperty
+This
->userinfo_start
, This
->canon_uri
+This
->userinfo_start
+1,
3899 (This
->canon_len
-This
->userinfo_start
-1)*sizeof(WCHAR
));
3901 } else if(This
->userinfo_split
== 0 && This
->userinfo_len
== 1) {
3902 /* Don't include the ":@" */
3903 *pbstrProperty
= SysAllocStringLen(NULL
, This
->canon_len
-2);
3905 if(*pbstrProperty
) {
3906 memcpy(*pbstrProperty
, This
->canon_uri
, This
->userinfo_start
*sizeof(WCHAR
));
3907 memcpy(*pbstrProperty
+This
->userinfo_start
, This
->canon_uri
+This
->userinfo_start
+2,
3908 (This
->canon_len
-This
->userinfo_start
-2)*sizeof(WCHAR
));
3911 *pbstrProperty
= SysAllocString(This
->canon_uri
);
3915 *pbstrProperty
= SysAllocString(This
->canon_uri
);
3920 if(!(*pbstrProperty
))
3921 hres
= E_OUTOFMEMORY
;
3924 case Uri_PROPERTY_AUTHORITY
:
3925 if(This
->authority_start
> -1) {
3926 if(This
->port_offset
> -1 && is_default_port(This
->scheme_type
, This
->port
) &&
3927 This
->display_modifiers
& URI_DISPLAY_NO_DEFAULT_PORT_AUTH
)
3928 /* Don't include the port in the authority component. */
3929 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+This
->authority_start
, This
->port_offset
);
3931 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+This
->authority_start
, This
->authority_len
);
3934 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
3938 if(!(*pbstrProperty
))
3939 hres
= E_OUTOFMEMORY
;
3942 case Uri_PROPERTY_DISPLAY_URI
:
3943 /* The Display URI contains everything except for the userinfo for known
3946 if(This
->scheme_type
!= URL_SCHEME_UNKNOWN
&& This
->userinfo_start
> -1) {
3947 *pbstrProperty
= SysAllocStringLen(NULL
, This
->canon_len
-This
->userinfo_len
);
3949 if(*pbstrProperty
) {
3950 /* Copy everything before the userinfo over. */
3951 memcpy(*pbstrProperty
, This
->canon_uri
, This
->userinfo_start
*sizeof(WCHAR
));
3952 /* Copy everything after the userinfo over. */
3953 memcpy(*pbstrProperty
+This
->userinfo_start
,
3954 This
->canon_uri
+This
->userinfo_start
+This
->userinfo_len
+1,
3955 (This
->canon_len
-(This
->userinfo_start
+This
->userinfo_len
+1))*sizeof(WCHAR
));
3958 *pbstrProperty
= SysAllocString(This
->canon_uri
);
3960 if(!(*pbstrProperty
))
3961 hres
= E_OUTOFMEMORY
;
3966 case Uri_PROPERTY_DOMAIN
:
3967 if(This
->domain_offset
> -1) {
3968 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+This
->host_start
+This
->domain_offset
,
3969 This
->host_len
-This
->domain_offset
);
3972 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
3976 if(!(*pbstrProperty
))
3977 hres
= E_OUTOFMEMORY
;
3980 case Uri_PROPERTY_EXTENSION
:
3981 if(This
->extension_offset
> -1) {
3982 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+This
->path_start
+This
->extension_offset
,
3983 This
->path_len
-This
->extension_offset
);
3986 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
3990 if(!(*pbstrProperty
))
3991 hres
= E_OUTOFMEMORY
;
3994 case Uri_PROPERTY_FRAGMENT
:
3995 if(This
->fragment_start
> -1) {
3996 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+This
->fragment_start
, This
->fragment_len
);
3999 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
4003 if(!(*pbstrProperty
))
4004 hres
= E_OUTOFMEMORY
;
4007 case Uri_PROPERTY_HOST
:
4008 if(This
->host_start
> -1) {
4009 /* The '[' and ']' aren't included for IPv6 addresses. */
4010 if(This
->host_type
== Uri_HOST_IPV6
)
4011 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+This
->host_start
+1, This
->host_len
-2);
4013 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+This
->host_start
, This
->host_len
);
4017 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
4021 if(!(*pbstrProperty
))
4022 hres
= E_OUTOFMEMORY
;
4025 case Uri_PROPERTY_PASSWORD
:
4026 if(This
->userinfo_split
> -1) {
4027 *pbstrProperty
= SysAllocStringLen(
4028 This
->canon_uri
+This
->userinfo_start
+This
->userinfo_split
+1,
4029 This
->userinfo_len
-This
->userinfo_split
-1);
4032 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
4036 if(!(*pbstrProperty
))
4037 return E_OUTOFMEMORY
;
4040 case Uri_PROPERTY_PATH
:
4041 if(This
->path_start
> -1) {
4042 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+This
->path_start
, This
->path_len
);
4045 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
4049 if(!(*pbstrProperty
))
4050 hres
= E_OUTOFMEMORY
;
4053 case Uri_PROPERTY_PATH_AND_QUERY
:
4054 if(This
->path_start
> -1) {
4055 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+This
->path_start
, This
->path_len
+This
->query_len
);
4057 } else if(This
->query_start
> -1) {
4058 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+This
->query_start
, This
->query_len
);
4061 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
4065 if(!(*pbstrProperty
))
4066 hres
= E_OUTOFMEMORY
;
4069 case Uri_PROPERTY_QUERY
:
4070 if(This
->query_start
> -1) {
4071 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+This
->query_start
, This
->query_len
);
4074 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
4078 if(!(*pbstrProperty
))
4079 hres
= E_OUTOFMEMORY
;
4082 case Uri_PROPERTY_RAW_URI
:
4083 *pbstrProperty
= SysAllocString(This
->raw_uri
);
4084 if(!(*pbstrProperty
))
4085 hres
= E_OUTOFMEMORY
;
4089 case Uri_PROPERTY_SCHEME_NAME
:
4090 if(This
->scheme_start
> -1) {
4091 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+ This
->scheme_start
, This
->scheme_len
);
4094 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
4098 if(!(*pbstrProperty
))
4099 hres
= E_OUTOFMEMORY
;
4102 case Uri_PROPERTY_USER_INFO
:
4103 if(This
->userinfo_start
> -1) {
4104 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+This
->userinfo_start
, This
->userinfo_len
);
4107 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
4111 if(!(*pbstrProperty
))
4112 hres
= E_OUTOFMEMORY
;
4115 case Uri_PROPERTY_USER_NAME
:
4116 if(This
->userinfo_start
> -1 && This
->userinfo_split
!= 0) {
4117 /* If userinfo_split is set, that means a password exists
4118 * so the username is only from userinfo_start to userinfo_split.
4120 if(This
->userinfo_split
> -1) {
4121 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+ This
->userinfo_start
, This
->userinfo_split
);
4124 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+ This
->userinfo_start
, This
->userinfo_len
);
4128 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
4132 if(!(*pbstrProperty
))
4133 return E_OUTOFMEMORY
;
4137 FIXME("(%p)->(%d %p %lx)\n", This
, uriProp
, pbstrProperty
, dwFlags
);
4144 static HRESULT WINAPI
Uri_GetPropertyLength(IUri
*iface
, Uri_PROPERTY uriProp
, DWORD
*pcchProperty
, DWORD dwFlags
)
4146 Uri
*This
= impl_from_IUri(iface
);
4148 TRACE("(%p %s)->(%d %p %lx)\n", This
, debugstr_w(This
->canon_uri
), uriProp
, pcchProperty
, dwFlags
);
4150 if(!This
->create_flags
)
4151 return E_UNEXPECTED
;
4153 return E_INVALIDARG
;
4155 /* Can only return a length for a property if it's a string. */
4156 if(uriProp
> Uri_PROPERTY_STRING_LAST
)
4157 return E_INVALIDARG
;
4159 /* Don't have support for flags yet. */
4161 FIXME("(%p)->(%d %p %lx)\n", This
, uriProp
, pcchProperty
, dwFlags
);
4166 case Uri_PROPERTY_ABSOLUTE_URI
:
4167 if(This
->display_modifiers
& URI_DISPLAY_NO_ABSOLUTE_URI
) {
4171 if(This
->scheme_type
!= URL_SCHEME_UNKNOWN
) {
4172 if(This
->userinfo_start
> -1 && This
->userinfo_len
== 0)
4173 /* Don't include the '@' in the length. */
4174 *pcchProperty
= This
->canon_len
-1;
4175 else if(This
->userinfo_start
> -1 && This
->userinfo_len
== 1 &&
4176 This
->userinfo_split
== 0)
4177 /* Don't include the ":@" in the length. */
4178 *pcchProperty
= This
->canon_len
-2;
4180 *pcchProperty
= This
->canon_len
;
4182 *pcchProperty
= This
->canon_len
;
4188 case Uri_PROPERTY_AUTHORITY
:
4189 if(This
->port_offset
> -1 &&
4190 This
->display_modifiers
& URI_DISPLAY_NO_DEFAULT_PORT_AUTH
&&
4191 is_default_port(This
->scheme_type
, This
->port
))
4192 /* Only count up until the port in the authority. */
4193 *pcchProperty
= This
->port_offset
;
4195 *pcchProperty
= This
->authority_len
;
4196 hres
= (This
->authority_start
> -1) ? S_OK
: S_FALSE
;
4198 case Uri_PROPERTY_DISPLAY_URI
:
4199 if(This
->scheme_type
!= URL_SCHEME_UNKNOWN
&& This
->userinfo_start
> -1)
4200 *pcchProperty
= This
->canon_len
-This
->userinfo_len
-1;
4202 *pcchProperty
= This
->canon_len
;
4206 case Uri_PROPERTY_DOMAIN
:
4207 if(This
->domain_offset
> -1)
4208 *pcchProperty
= This
->host_len
- This
->domain_offset
;
4212 hres
= (This
->domain_offset
> -1) ? S_OK
: S_FALSE
;
4214 case Uri_PROPERTY_EXTENSION
:
4215 if(This
->extension_offset
> -1) {
4216 *pcchProperty
= This
->path_len
- This
->extension_offset
;
4224 case Uri_PROPERTY_FRAGMENT
:
4225 *pcchProperty
= This
->fragment_len
;
4226 hres
= (This
->fragment_start
> -1) ? S_OK
: S_FALSE
;
4228 case Uri_PROPERTY_HOST
:
4229 *pcchProperty
= This
->host_len
;
4231 /* '[' and ']' aren't included in the length. */
4232 if(This
->host_type
== Uri_HOST_IPV6
)
4235 hres
= (This
->host_start
> -1) ? S_OK
: S_FALSE
;
4237 case Uri_PROPERTY_PASSWORD
:
4238 *pcchProperty
= (This
->userinfo_split
> -1) ? This
->userinfo_len
-This
->userinfo_split
-1 : 0;
4239 hres
= (This
->userinfo_split
> -1) ? S_OK
: S_FALSE
;
4241 case Uri_PROPERTY_PATH
:
4242 *pcchProperty
= This
->path_len
;
4243 hres
= (This
->path_start
> -1) ? S_OK
: S_FALSE
;
4245 case Uri_PROPERTY_PATH_AND_QUERY
:
4246 *pcchProperty
= This
->path_len
+This
->query_len
;
4247 hres
= (This
->path_start
> -1 || This
->query_start
> -1) ? S_OK
: S_FALSE
;
4249 case Uri_PROPERTY_QUERY
:
4250 *pcchProperty
= This
->query_len
;
4251 hres
= (This
->query_start
> -1) ? S_OK
: S_FALSE
;
4253 case Uri_PROPERTY_RAW_URI
:
4254 *pcchProperty
= SysStringLen(This
->raw_uri
);
4257 case Uri_PROPERTY_SCHEME_NAME
:
4258 *pcchProperty
= This
->scheme_len
;
4259 hres
= (This
->scheme_start
> -1) ? S_OK
: S_FALSE
;
4261 case Uri_PROPERTY_USER_INFO
:
4262 *pcchProperty
= This
->userinfo_len
;
4263 hres
= (This
->userinfo_start
> -1) ? S_OK
: S_FALSE
;
4265 case Uri_PROPERTY_USER_NAME
:
4266 *pcchProperty
= (This
->userinfo_split
> -1) ? This
->userinfo_split
: This
->userinfo_len
;
4267 if(This
->userinfo_split
== 0)
4270 hres
= (This
->userinfo_start
> -1) ? S_OK
: S_FALSE
;
4273 FIXME("(%p)->(%d %p %lx)\n", This
, uriProp
, pcchProperty
, dwFlags
);
4280 static HRESULT WINAPI
Uri_GetPropertyDWORD(IUri
*iface
, Uri_PROPERTY uriProp
, DWORD
*pcchProperty
, DWORD dwFlags
)
4282 Uri
*This
= impl_from_IUri(iface
);
4285 TRACE("(%p %s)->(%d %p %lx)\n", This
, debugstr_w(This
->canon_uri
), uriProp
, pcchProperty
, dwFlags
);
4287 if(!This
->create_flags
)
4288 return E_UNEXPECTED
;
4290 return E_INVALIDARG
;
4292 /* Microsoft's implementation for the ZONE property of a URI seems to be lacking...
4293 * From what I can tell, instead of checking which URLZONE the URI belongs to it
4294 * simply assigns URLZONE_INVALID and returns E_NOTIMPL. This also applies to the GetZone
4297 if(uriProp
== Uri_PROPERTY_ZONE
) {
4298 *pcchProperty
= URLZONE_INVALID
;
4302 if(uriProp
< Uri_PROPERTY_DWORD_START
) {
4304 return E_INVALIDARG
;
4308 case Uri_PROPERTY_HOST_TYPE
:
4309 *pcchProperty
= This
->host_type
;
4312 case Uri_PROPERTY_PORT
:
4313 if(!This
->has_port
) {
4317 *pcchProperty
= This
->port
;
4322 case Uri_PROPERTY_SCHEME
:
4323 *pcchProperty
= This
->scheme_type
;
4327 FIXME("(%p)->(%d %p %lx)\n", This
, uriProp
, pcchProperty
, dwFlags
);
4334 static HRESULT WINAPI
Uri_HasProperty(IUri
*iface
, Uri_PROPERTY uriProp
, BOOL
*pfHasProperty
)
4336 Uri
*This
= impl_from_IUri(iface
);
4338 TRACE("(%p %s)->(%d %p)\n", This
, debugstr_w(This
->canon_uri
), uriProp
, pfHasProperty
);
4341 return E_INVALIDARG
;
4344 case Uri_PROPERTY_ABSOLUTE_URI
:
4345 *pfHasProperty
= !(This
->display_modifiers
& URI_DISPLAY_NO_ABSOLUTE_URI
);
4347 case Uri_PROPERTY_AUTHORITY
:
4348 *pfHasProperty
= This
->authority_start
> -1;
4350 case Uri_PROPERTY_DISPLAY_URI
:
4351 *pfHasProperty
= TRUE
;
4353 case Uri_PROPERTY_DOMAIN
:
4354 *pfHasProperty
= This
->domain_offset
> -1;
4356 case Uri_PROPERTY_EXTENSION
:
4357 *pfHasProperty
= This
->extension_offset
> -1;
4359 case Uri_PROPERTY_FRAGMENT
:
4360 *pfHasProperty
= This
->fragment_start
> -1;
4362 case Uri_PROPERTY_HOST
:
4363 *pfHasProperty
= This
->host_start
> -1;
4365 case Uri_PROPERTY_PASSWORD
:
4366 *pfHasProperty
= This
->userinfo_split
> -1;
4368 case Uri_PROPERTY_PATH
:
4369 *pfHasProperty
= This
->path_start
> -1;
4371 case Uri_PROPERTY_PATH_AND_QUERY
:
4372 *pfHasProperty
= (This
->path_start
> -1 || This
->query_start
> -1);
4374 case Uri_PROPERTY_QUERY
:
4375 *pfHasProperty
= This
->query_start
> -1;
4377 case Uri_PROPERTY_RAW_URI
:
4378 *pfHasProperty
= TRUE
;
4380 case Uri_PROPERTY_SCHEME_NAME
:
4381 *pfHasProperty
= This
->scheme_start
> -1;
4383 case Uri_PROPERTY_USER_INFO
:
4384 *pfHasProperty
= This
->userinfo_start
> -1;
4386 case Uri_PROPERTY_USER_NAME
:
4387 if(This
->userinfo_split
== 0)
4388 *pfHasProperty
= FALSE
;
4390 *pfHasProperty
= This
->userinfo_start
> -1;
4392 case Uri_PROPERTY_HOST_TYPE
:
4393 *pfHasProperty
= TRUE
;
4395 case Uri_PROPERTY_PORT
:
4396 *pfHasProperty
= This
->has_port
;
4398 case Uri_PROPERTY_SCHEME
:
4399 *pfHasProperty
= TRUE
;
4401 case Uri_PROPERTY_ZONE
:
4402 *pfHasProperty
= FALSE
;
4405 FIXME("(%p)->(%d %p): Unsupported property type.\n", This
, uriProp
, pfHasProperty
);
4412 static HRESULT WINAPI
Uri_GetAbsoluteUri(IUri
*iface
, BSTR
*pstrAbsoluteUri
)
4414 TRACE("(%p)->(%p)\n", iface
, pstrAbsoluteUri
);
4415 return IUri_GetPropertyBSTR(iface
, Uri_PROPERTY_ABSOLUTE_URI
, pstrAbsoluteUri
, 0);
4418 static HRESULT WINAPI
Uri_GetAuthority(IUri
*iface
, BSTR
*pstrAuthority
)
4420 TRACE("(%p)->(%p)\n", iface
, pstrAuthority
);
4421 return IUri_GetPropertyBSTR(iface
, Uri_PROPERTY_AUTHORITY
, pstrAuthority
, 0);
4424 static HRESULT WINAPI
Uri_GetDisplayUri(IUri
*iface
, BSTR
*pstrDisplayUri
)
4426 TRACE("(%p)->(%p)\n", iface
, pstrDisplayUri
);
4427 return IUri_GetPropertyBSTR(iface
, Uri_PROPERTY_DISPLAY_URI
, pstrDisplayUri
, 0);
4430 static HRESULT WINAPI
Uri_GetDomain(IUri
*iface
, BSTR
*pstrDomain
)
4432 TRACE("(%p)->(%p)\n", iface
, pstrDomain
);
4433 return IUri_GetPropertyBSTR(iface
, Uri_PROPERTY_DOMAIN
, pstrDomain
, 0);
4436 static HRESULT WINAPI
Uri_GetExtension(IUri
*iface
, BSTR
*pstrExtension
)
4438 TRACE("(%p)->(%p)\n", iface
, pstrExtension
);
4439 return IUri_GetPropertyBSTR(iface
, Uri_PROPERTY_EXTENSION
, pstrExtension
, 0);
4442 static HRESULT WINAPI
Uri_GetFragment(IUri
*iface
, BSTR
*pstrFragment
)
4444 TRACE("(%p)->(%p)\n", iface
, pstrFragment
);
4445 return IUri_GetPropertyBSTR(iface
, Uri_PROPERTY_FRAGMENT
, pstrFragment
, 0);
4448 static HRESULT WINAPI
Uri_GetHost(IUri
*iface
, BSTR
*pstrHost
)
4450 TRACE("(%p)->(%p)\n", iface
, pstrHost
);
4451 return IUri_GetPropertyBSTR(iface
, Uri_PROPERTY_HOST
, pstrHost
, 0);
4454 static HRESULT WINAPI
Uri_GetPassword(IUri
*iface
, BSTR
*pstrPassword
)
4456 TRACE("(%p)->(%p)\n", iface
, pstrPassword
);
4457 return IUri_GetPropertyBSTR(iface
, Uri_PROPERTY_PASSWORD
, pstrPassword
, 0);
4460 static HRESULT WINAPI
Uri_GetPath(IUri
*iface
, BSTR
*pstrPath
)
4462 TRACE("(%p)->(%p)\n", iface
, pstrPath
);
4463 return IUri_GetPropertyBSTR(iface
, Uri_PROPERTY_PATH
, pstrPath
, 0);
4466 static HRESULT WINAPI
Uri_GetPathAndQuery(IUri
*iface
, BSTR
*pstrPathAndQuery
)
4468 TRACE("(%p)->(%p)\n", iface
, pstrPathAndQuery
);
4469 return IUri_GetPropertyBSTR(iface
, Uri_PROPERTY_PATH_AND_QUERY
, pstrPathAndQuery
, 0);
4472 static HRESULT WINAPI
Uri_GetQuery(IUri
*iface
, BSTR
*pstrQuery
)
4474 TRACE("(%p)->(%p)\n", iface
, pstrQuery
);
4475 return IUri_GetPropertyBSTR(iface
, Uri_PROPERTY_QUERY
, pstrQuery
, 0);
4478 static HRESULT WINAPI
Uri_GetRawUri(IUri
*iface
, BSTR
*pstrRawUri
)
4480 TRACE("(%p)->(%p)\n", iface
, pstrRawUri
);
4481 return IUri_GetPropertyBSTR(iface
, Uri_PROPERTY_RAW_URI
, pstrRawUri
, 0);
4484 static HRESULT WINAPI
Uri_GetSchemeName(IUri
*iface
, BSTR
*pstrSchemeName
)
4486 TRACE("(%p)->(%p)\n", iface
, pstrSchemeName
);
4487 return IUri_GetPropertyBSTR(iface
, Uri_PROPERTY_SCHEME_NAME
, pstrSchemeName
, 0);
4490 static HRESULT WINAPI
Uri_GetUserInfo(IUri
*iface
, BSTR
*pstrUserInfo
)
4492 TRACE("(%p)->(%p)\n", iface
, pstrUserInfo
);
4493 return IUri_GetPropertyBSTR(iface
, Uri_PROPERTY_USER_INFO
, pstrUserInfo
, 0);
4496 static HRESULT WINAPI
Uri_GetUserName(IUri
*iface
, BSTR
*pstrUserName
)
4498 TRACE("(%p)->(%p)\n", iface
, pstrUserName
);
4499 return IUri_GetPropertyBSTR(iface
, Uri_PROPERTY_USER_NAME
, pstrUserName
, 0);
4502 static HRESULT WINAPI
Uri_GetHostType(IUri
*iface
, DWORD
*pdwHostType
)
4504 TRACE("(%p)->(%p)\n", iface
, pdwHostType
);
4505 return IUri_GetPropertyDWORD(iface
, Uri_PROPERTY_HOST_TYPE
, pdwHostType
, 0);
4508 static HRESULT WINAPI
Uri_GetPort(IUri
*iface
, DWORD
*pdwPort
)
4510 TRACE("(%p)->(%p)\n", iface
, pdwPort
);
4511 return IUri_GetPropertyDWORD(iface
, Uri_PROPERTY_PORT
, pdwPort
, 0);
4514 static HRESULT WINAPI
Uri_GetScheme(IUri
*iface
, DWORD
*pdwScheme
)
4516 TRACE("(%p)->(%p)\n", iface
, pdwScheme
);
4517 return IUri_GetPropertyDWORD(iface
, Uri_PROPERTY_SCHEME
, pdwScheme
, 0);
4520 static HRESULT WINAPI
Uri_GetZone(IUri
*iface
, DWORD
*pdwZone
)
4522 TRACE("(%p)->(%p)\n", iface
, pdwZone
);
4523 return IUri_GetPropertyDWORD(iface
, Uri_PROPERTY_ZONE
,pdwZone
, 0);
4526 static HRESULT WINAPI
Uri_GetProperties(IUri
*iface
, DWORD
*pdwProperties
)
4528 Uri
*This
= impl_from_IUri(iface
);
4529 TRACE("(%p %s)->(%p)\n", This
, debugstr_w(This
->canon_uri
), pdwProperties
);
4531 if(!This
->create_flags
)
4532 return E_UNEXPECTED
;
4534 return E_INVALIDARG
;
4536 /* All URIs have these. */
4537 *pdwProperties
= Uri_HAS_DISPLAY_URI
|Uri_HAS_RAW_URI
|Uri_HAS_SCHEME
|Uri_HAS_HOST_TYPE
;
4539 if(!(This
->display_modifiers
& URI_DISPLAY_NO_ABSOLUTE_URI
))
4540 *pdwProperties
|= Uri_HAS_ABSOLUTE_URI
;
4542 if(This
->scheme_start
> -1)
4543 *pdwProperties
|= Uri_HAS_SCHEME_NAME
;
4545 if(This
->authority_start
> -1) {
4546 *pdwProperties
|= Uri_HAS_AUTHORITY
;
4547 if(This
->userinfo_start
> -1) {
4548 *pdwProperties
|= Uri_HAS_USER_INFO
;
4549 if(This
->userinfo_split
!= 0)
4550 *pdwProperties
|= Uri_HAS_USER_NAME
;
4552 if(This
->userinfo_split
> -1)
4553 *pdwProperties
|= Uri_HAS_PASSWORD
;
4554 if(This
->host_start
> -1)
4555 *pdwProperties
|= Uri_HAS_HOST
;
4556 if(This
->domain_offset
> -1)
4557 *pdwProperties
|= Uri_HAS_DOMAIN
;
4561 *pdwProperties
|= Uri_HAS_PORT
;
4562 if(This
->path_start
> -1)
4563 *pdwProperties
|= Uri_HAS_PATH
|Uri_HAS_PATH_AND_QUERY
;
4564 if(This
->query_start
> -1)
4565 *pdwProperties
|= Uri_HAS_QUERY
|Uri_HAS_PATH_AND_QUERY
;
4567 if(This
->extension_offset
> -1)
4568 *pdwProperties
|= Uri_HAS_EXTENSION
;
4570 if(This
->fragment_start
> -1)
4571 *pdwProperties
|= Uri_HAS_FRAGMENT
;
4576 static HRESULT WINAPI
Uri_IsEqual(IUri
*iface
, IUri
*pUri
, BOOL
*pfEqual
)
4578 Uri
*This
= impl_from_IUri(iface
);
4581 TRACE("(%p %s)->(%p %p)\n", This
, debugstr_w(This
->canon_uri
), pUri
, pfEqual
);
4583 if(!This
->create_flags
)
4584 return E_UNEXPECTED
;
4591 /* For some reason Windows returns S_OK here... */
4595 /* Try to convert it to a Uri (allows for a more simple comparison). */
4596 if(!(other
= get_uri_obj(pUri
))) {
4597 FIXME("(%p)->(%p %p) No support for unknown IUri's yet.\n", iface
, pUri
, pfEqual
);
4601 TRACE("comparing to %s\n", debugstr_w(other
->canon_uri
));
4602 return compare_uris(This
, other
, pfEqual
);
4605 static const IUriVtbl UriVtbl
= {
4609 Uri_GetPropertyBSTR
,
4610 Uri_GetPropertyLength
,
4611 Uri_GetPropertyDWORD
,
4622 Uri_GetPathAndQuery
,
4636 static inline Uri
* impl_from_IUriBuilderFactory(IUriBuilderFactory
*iface
)
4638 return CONTAINING_RECORD(iface
, Uri
, IUriBuilderFactory_iface
);
4641 static HRESULT WINAPI
UriBuilderFactory_QueryInterface(IUriBuilderFactory
*iface
, REFIID riid
, void **ppv
)
4643 Uri
*This
= impl_from_IUriBuilderFactory(iface
);
4644 return IUri_QueryInterface(&This
->IUri_iface
, riid
, ppv
);
4647 static ULONG WINAPI
UriBuilderFactory_AddRef(IUriBuilderFactory
*iface
)
4649 Uri
*This
= impl_from_IUriBuilderFactory(iface
);
4650 return IUri_AddRef(&This
->IUri_iface
);
4653 static ULONG WINAPI
UriBuilderFactory_Release(IUriBuilderFactory
*iface
)
4655 Uri
*This
= impl_from_IUriBuilderFactory(iface
);
4656 return IUri_Release(&This
->IUri_iface
);
4659 static HRESULT WINAPI
UriBuilderFactory_CreateIUriBuilder(IUriBuilderFactory
*iface
,
4661 DWORD_PTR dwReserved
,
4662 IUriBuilder
**ppIUriBuilder
)
4664 Uri
*This
= impl_from_IUriBuilderFactory(iface
);
4665 TRACE("(%p)->(%08lx %08Ix %p)\n", This
, dwFlags
, dwReserved
, ppIUriBuilder
);
4670 if(dwFlags
|| dwReserved
) {
4671 *ppIUriBuilder
= NULL
;
4672 return E_INVALIDARG
;
4675 return CreateIUriBuilder(NULL
, 0, 0, ppIUriBuilder
);
4678 static HRESULT WINAPI
UriBuilderFactory_CreateInitializedIUriBuilder(IUriBuilderFactory
*iface
,
4680 DWORD_PTR dwReserved
,
4681 IUriBuilder
**ppIUriBuilder
)
4683 Uri
*This
= impl_from_IUriBuilderFactory(iface
);
4684 TRACE("(%p)->(%08lx %08Ix %p)\n", This
, dwFlags
, dwReserved
, ppIUriBuilder
);
4689 if(dwFlags
|| dwReserved
) {
4690 *ppIUriBuilder
= NULL
;
4691 return E_INVALIDARG
;
4694 return CreateIUriBuilder(&This
->IUri_iface
, 0, 0, ppIUriBuilder
);
4697 static const IUriBuilderFactoryVtbl UriBuilderFactoryVtbl
= {
4698 UriBuilderFactory_QueryInterface
,
4699 UriBuilderFactory_AddRef
,
4700 UriBuilderFactory_Release
,
4701 UriBuilderFactory_CreateIUriBuilder
,
4702 UriBuilderFactory_CreateInitializedIUriBuilder
4705 static inline Uri
* impl_from_IPersistStream(IPersistStream
*iface
)
4707 return CONTAINING_RECORD(iface
, Uri
, IPersistStream_iface
);
4710 static HRESULT WINAPI
PersistStream_QueryInterface(IPersistStream
*iface
, REFIID riid
, void **ppvObject
)
4712 Uri
*This
= impl_from_IPersistStream(iface
);
4713 return IUri_QueryInterface(&This
->IUri_iface
, riid
, ppvObject
);
4716 static ULONG WINAPI
PersistStream_AddRef(IPersistStream
*iface
)
4718 Uri
*This
= impl_from_IPersistStream(iface
);
4719 return IUri_AddRef(&This
->IUri_iface
);
4722 static ULONG WINAPI
PersistStream_Release(IPersistStream
*iface
)
4724 Uri
*This
= impl_from_IPersistStream(iface
);
4725 return IUri_Release(&This
->IUri_iface
);
4728 static HRESULT WINAPI
PersistStream_GetClassID(IPersistStream
*iface
, CLSID
*pClassID
)
4730 Uri
*This
= impl_from_IPersistStream(iface
);
4731 TRACE("(%p)->(%p)\n", This
, pClassID
);
4734 return E_INVALIDARG
;
4736 *pClassID
= CLSID_CUri
;
4740 static HRESULT WINAPI
PersistStream_IsDirty(IPersistStream
*iface
)
4742 Uri
*This
= impl_from_IPersistStream(iface
);
4743 TRACE("(%p)\n", This
);
4747 struct persist_uri
{
4756 static HRESULT WINAPI
PersistStream_Load(IPersistStream
*iface
, IStream
*pStm
)
4758 Uri
*This
= impl_from_IPersistStream(iface
);
4759 struct persist_uri
*data
;
4764 TRACE("(%p)->(%p)\n", This
, pStm
);
4766 if(This
->create_flags
)
4767 return E_UNEXPECTED
;
4769 return E_INVALIDARG
;
4771 hr
= IStream_Read(pStm
, &size
, sizeof(DWORD
), NULL
);
4774 data
= malloc(size
);
4776 return E_OUTOFMEMORY
;
4777 hr
= IStream_Read(pStm
, data
->unk1
, size
-sizeof(DWORD
)-2, NULL
);
4783 if(size
< sizeof(struct persist_uri
)) {
4788 if(*(DWORD
*)data
->data
!= Uri_PROPERTY_RAW_URI
) {
4790 ERR("Can't find raw_uri\n");
4791 return E_UNEXPECTED
;
4794 This
->raw_uri
= SysAllocString((WCHAR
*)(data
->data
+sizeof(DWORD
)*2));
4795 if(!This
->raw_uri
) {
4797 return E_OUTOFMEMORY
;
4799 This
->create_flags
= data
->create_flags
;
4801 TRACE("%lx %s\n", This
->create_flags
, debugstr_w(This
->raw_uri
));
4803 memset(&parse
, 0, sizeof(parse_data
));
4804 parse
.uri
= This
->raw_uri
;
4805 if(!parse_uri(&parse
, This
->create_flags
)) {
4806 SysFreeString(This
->raw_uri
);
4807 This
->create_flags
= 0;
4808 return E_UNEXPECTED
;
4811 hr
= canonicalize_uri(&parse
, This
, This
->create_flags
);
4813 SysFreeString(This
->raw_uri
);
4814 This
->create_flags
= 0;
4821 static inline BYTE
* persist_stream_add_strprop(Uri
*This
, BYTE
*p
, DWORD type
, DWORD len
, WCHAR
*data
)
4823 len
*= sizeof(WCHAR
);
4826 *(DWORD
*)p
= len
+sizeof(WCHAR
);
4828 memcpy(p
, data
, len
);
4831 return p
+sizeof(WCHAR
);
4834 static inline void persist_stream_save(Uri
*This
, IStream
*pStm
, BOOL marshal
, struct persist_uri
*data
)
4838 data
->create_flags
= This
->create_flags
;
4840 if(This
->create_flags
) {
4841 data
->fields_no
= 1;
4842 p
= persist_stream_add_strprop(This
, data
->data
, Uri_PROPERTY_RAW_URI
,
4843 SysStringLen(This
->raw_uri
), This
->raw_uri
);
4845 if(This
->scheme_type
!=URL_SCHEME_HTTP
&& This
->scheme_type
!=URL_SCHEME_HTTPS
4846 && This
->scheme_type
!=URL_SCHEME_FTP
)
4849 if(This
->fragment_len
) {
4851 p
= persist_stream_add_strprop(This
, p
, Uri_PROPERTY_FRAGMENT
,
4852 This
->fragment_len
, This
->canon_uri
+This
->fragment_start
);
4855 if(This
->host_len
) {
4857 if(This
->host_type
== Uri_HOST_IPV6
)
4858 p
= persist_stream_add_strprop(This
, p
, Uri_PROPERTY_HOST
,
4859 This
->host_len
-2, This
->canon_uri
+This
->host_start
+1);
4861 p
= persist_stream_add_strprop(This
, p
, Uri_PROPERTY_HOST
,
4862 This
->host_len
, This
->canon_uri
+This
->host_start
);
4865 if(This
->userinfo_split
> -1) {
4867 p
= persist_stream_add_strprop(This
, p
, Uri_PROPERTY_PASSWORD
,
4868 This
->userinfo_len
-This
->userinfo_split
-1,
4869 This
->canon_uri
+This
->userinfo_start
+This
->userinfo_split
+1);
4872 if(This
->path_len
) {
4874 p
= persist_stream_add_strprop(This
, p
, Uri_PROPERTY_PATH
,
4875 This
->path_len
, This
->canon_uri
+This
->path_start
);
4876 } else if(marshal
) {
4877 WCHAR no_path
= '/';
4879 p
= persist_stream_add_strprop(This
, p
, Uri_PROPERTY_PATH
, 1, &no_path
);
4882 if(This
->has_port
) {
4884 *(DWORD
*)p
= Uri_PROPERTY_PORT
;
4886 *(DWORD
*)p
= sizeof(DWORD
);
4888 *(DWORD
*)p
= This
->port
;
4892 if(This
->query_len
) {
4894 p
= persist_stream_add_strprop(This
, p
, Uri_PROPERTY_QUERY
,
4895 This
->query_len
, This
->canon_uri
+This
->query_start
);
4898 if(This
->scheme_len
) {
4900 p
= persist_stream_add_strprop(This
, p
, Uri_PROPERTY_SCHEME_NAME
,
4901 This
->scheme_len
, This
->canon_uri
+This
->scheme_start
);
4904 if(This
->userinfo_start
>-1 && This
->userinfo_split
!=0) {
4906 if(This
->userinfo_split
> -1)
4907 p
= persist_stream_add_strprop(This
, p
, Uri_PROPERTY_USER_NAME
,
4908 This
->userinfo_split
, This
->canon_uri
+This
->userinfo_start
);
4910 p
= persist_stream_add_strprop(This
, p
, Uri_PROPERTY_USER_NAME
,
4911 This
->userinfo_len
, This
->canon_uri
+This
->userinfo_start
);
4915 static HRESULT WINAPI
PersistStream_Save(IPersistStream
*iface
, IStream
*pStm
, BOOL fClearDirty
)
4917 Uri
*This
= impl_from_IPersistStream(iface
);
4918 struct persist_uri
*data
;
4919 ULARGE_INTEGER size
;
4922 TRACE("(%p)->(%p %x)\n", This
, pStm
, fClearDirty
);
4925 return E_INVALIDARG
;
4927 hres
= IPersistStream_GetSizeMax(&This
->IPersistStream_iface
, &size
);
4931 data
= calloc(1, size
.u
.LowPart
);
4933 return E_OUTOFMEMORY
;
4934 data
->size
= size
.u
.LowPart
;
4935 persist_stream_save(This
, pStm
, FALSE
, data
);
4937 hres
= IStream_Write(pStm
, data
, data
->size
-2, NULL
);
4942 static HRESULT WINAPI
PersistStream_GetSizeMax(IPersistStream
*iface
, ULARGE_INTEGER
*pcbSize
)
4944 Uri
*This
= impl_from_IPersistStream(iface
);
4945 TRACE("(%p)->(%p)\n", This
, pcbSize
);
4948 return E_INVALIDARG
;
4950 pcbSize
->u
.LowPart
= 2+sizeof(struct persist_uri
);
4951 pcbSize
->u
.HighPart
= 0;
4952 if(This
->create_flags
)
4953 pcbSize
->u
.LowPart
+= (SysStringLen(This
->raw_uri
)+1)*sizeof(WCHAR
) + 2*sizeof(DWORD
);
4954 else /* there's no place for fields no */
4955 pcbSize
->u
.LowPart
-= sizeof(DWORD
);
4956 if(This
->scheme_type
!=URL_SCHEME_HTTP
&& This
->scheme_type
!=URL_SCHEME_HTTPS
4957 && This
->scheme_type
!=URL_SCHEME_FTP
)
4960 if(This
->fragment_len
)
4961 pcbSize
->u
.LowPart
+= (This
->fragment_len
+1)*sizeof(WCHAR
) + 2*sizeof(DWORD
);
4962 if(This
->host_len
) {
4963 if(This
->host_type
== Uri_HOST_IPV6
)
4964 pcbSize
->u
.LowPart
+= (This
->host_len
-1)*sizeof(WCHAR
) + 2*sizeof(DWORD
);
4966 pcbSize
->u
.LowPart
+= (This
->host_len
+1)*sizeof(WCHAR
) + 2*sizeof(DWORD
);
4968 if(This
->userinfo_split
> -1)
4969 pcbSize
->u
.LowPart
+= (This
->userinfo_len
-This
->userinfo_split
)*sizeof(WCHAR
) + 2*sizeof(DWORD
);
4971 pcbSize
->u
.LowPart
+= (This
->path_len
+1)*sizeof(WCHAR
) + 2*sizeof(DWORD
);
4973 pcbSize
->u
.LowPart
+= 3*sizeof(DWORD
);
4975 pcbSize
->u
.LowPart
+= (This
->query_len
+1)*sizeof(WCHAR
) + 2*sizeof(DWORD
);
4976 if(This
->scheme_len
)
4977 pcbSize
->u
.LowPart
+= (This
->scheme_len
+1)*sizeof(WCHAR
) + 2*sizeof(DWORD
);
4978 if(This
->userinfo_start
>-1 && This
->userinfo_split
!=0) {
4979 if(This
->userinfo_split
> -1)
4980 pcbSize
->u
.LowPart
+= (This
->userinfo_split
+1)*sizeof(WCHAR
) + 2*sizeof(DWORD
);
4982 pcbSize
->u
.LowPart
+= (This
->userinfo_len
+1)*sizeof(WCHAR
) + 2*sizeof(DWORD
);
4987 static const IPersistStreamVtbl PersistStreamVtbl
= {
4988 PersistStream_QueryInterface
,
4989 PersistStream_AddRef
,
4990 PersistStream_Release
,
4991 PersistStream_GetClassID
,
4992 PersistStream_IsDirty
,
4995 PersistStream_GetSizeMax
4998 static inline Uri
* impl_from_IMarshal(IMarshal
*iface
)
5000 return CONTAINING_RECORD(iface
, Uri
, IMarshal_iface
);
5003 static HRESULT WINAPI
Marshal_QueryInterface(IMarshal
*iface
, REFIID riid
, void **ppvObject
)
5005 Uri
*This
= impl_from_IMarshal(iface
);
5006 return IUri_QueryInterface(&This
->IUri_iface
, riid
, ppvObject
);
5009 static ULONG WINAPI
Marshal_AddRef(IMarshal
*iface
)
5011 Uri
*This
= impl_from_IMarshal(iface
);
5012 return IUri_AddRef(&This
->IUri_iface
);
5015 static ULONG WINAPI
Marshal_Release(IMarshal
*iface
)
5017 Uri
*This
= impl_from_IMarshal(iface
);
5018 return IUri_Release(&This
->IUri_iface
);
5021 static HRESULT WINAPI
Marshal_GetUnmarshalClass(IMarshal
*iface
, REFIID riid
, void *pv
,
5022 DWORD dwDestContext
, void *pvDestContext
, DWORD mshlflags
, CLSID
*pCid
)
5024 Uri
*This
= impl_from_IMarshal(iface
);
5025 TRACE("(%p)->(%s %p %lx %p %lx %p)\n", This
, debugstr_guid(riid
), pv
,
5026 dwDestContext
, pvDestContext
, mshlflags
, pCid
);
5028 if(!pCid
|| (dwDestContext
!=MSHCTX_LOCAL
&& dwDestContext
!=MSHCTX_NOSHAREDMEM
5029 && dwDestContext
!=MSHCTX_INPROC
))
5030 return E_INVALIDARG
;
5036 struct inproc_marshal_uri
{
5039 DWORD unk
[4]; /* process identifier? */
5043 static HRESULT WINAPI
Marshal_GetMarshalSizeMax(IMarshal
*iface
, REFIID riid
, void *pv
,
5044 DWORD dwDestContext
, void *pvDestContext
, DWORD mshlflags
, DWORD
*pSize
)
5046 Uri
*This
= impl_from_IMarshal(iface
);
5047 ULARGE_INTEGER size
;
5049 TRACE("(%p)->(%s %p %lx %p %lx %p)\n", This
, debugstr_guid(riid
), pv
,
5050 dwDestContext
, pvDestContext
, mshlflags
, pSize
);
5052 if(!pSize
|| (dwDestContext
!=MSHCTX_LOCAL
&& dwDestContext
!=MSHCTX_NOSHAREDMEM
5053 && dwDestContext
!=MSHCTX_INPROC
))
5054 return E_INVALIDARG
;
5056 if(dwDestContext
== MSHCTX_INPROC
) {
5057 *pSize
= sizeof(struct inproc_marshal_uri
);
5061 hres
= IPersistStream_GetSizeMax(&This
->IPersistStream_iface
, &size
);
5064 if(!This
->path_len
&& (This
->scheme_type
==URL_SCHEME_HTTP
5065 || This
->scheme_type
==URL_SCHEME_HTTPS
5066 || This
->scheme_type
==URL_SCHEME_FTP
))
5067 size
.u
.LowPart
+= 3*sizeof(DWORD
);
5068 *pSize
= size
.u
.LowPart
+2*sizeof(DWORD
);
5072 static HRESULT WINAPI
Marshal_MarshalInterface(IMarshal
*iface
, IStream
*pStm
, REFIID riid
,
5073 void *pv
, DWORD dwDestContext
, void *pvDestContext
, DWORD mshlflags
)
5075 Uri
*This
= impl_from_IMarshal(iface
);
5080 TRACE("(%p)->(%p %s %p %lx %p %lx)\n", This
, pStm
, debugstr_guid(riid
), pv
,
5081 dwDestContext
, pvDestContext
, mshlflags
);
5083 if(!pStm
|| mshlflags
!=MSHLFLAGS_NORMAL
|| (dwDestContext
!=MSHCTX_LOCAL
5084 && dwDestContext
!=MSHCTX_NOSHAREDMEM
&& dwDestContext
!=MSHCTX_INPROC
))
5085 return E_INVALIDARG
;
5087 if(dwDestContext
== MSHCTX_INPROC
) {
5088 struct inproc_marshal_uri data
;
5090 data
.size
= sizeof(data
);
5091 data
.mshlflags
= MSHCTX_INPROC
;
5098 hres
= IStream_Write(pStm
, &data
, data
.size
, NULL
);
5102 IUri_AddRef(&This
->IUri_iface
);
5106 hres
= IMarshal_GetMarshalSizeMax(iface
, riid
, pv
, dwDestContext
,
5107 pvDestContext
, mshlflags
, &size
);
5111 data
= calloc(1, size
);
5113 return E_OUTOFMEMORY
;
5116 data
[1] = dwDestContext
;
5117 data
[2] = size
-2*sizeof(DWORD
);
5118 persist_stream_save(This
, pStm
, TRUE
, (struct persist_uri
*)(data
+2));
5120 hres
= IStream_Write(pStm
, data
, data
[0]-2, NULL
);
5125 static HRESULT WINAPI
Marshal_UnmarshalInterface(IMarshal
*iface
,
5126 IStream
*pStm
, REFIID riid
, void **ppv
)
5128 Uri
*This
= impl_from_IMarshal(iface
);
5132 TRACE("(%p)->(%p %s %p)\n", This
, pStm
, debugstr_guid(riid
), ppv
);
5134 if(This
->create_flags
)
5135 return E_UNEXPECTED
;
5136 if(!pStm
|| !riid
|| !ppv
)
5137 return E_INVALIDARG
;
5139 hres
= IStream_Read(pStm
, header
, sizeof(header
), NULL
);
5143 if(header
[1]!=MSHCTX_LOCAL
&& header
[1]!=MSHCTX_NOSHAREDMEM
5144 && header
[1]!=MSHCTX_INPROC
)
5145 return E_UNEXPECTED
;
5147 if(header
[1] == MSHCTX_INPROC
) {
5148 struct inproc_marshal_uri data
;
5151 hres
= IStream_Read(pStm
, data
.unk
, sizeof(data
)-2*sizeof(DWORD
), NULL
);
5155 This
->raw_uri
= SysAllocString(data
.uri
->raw_uri
);
5156 if(!This
->raw_uri
) {
5157 return E_OUTOFMEMORY
;
5160 memset(&parse
, 0, sizeof(parse_data
));
5161 parse
.uri
= This
->raw_uri
;
5163 if(!parse_uri(&parse
, data
.uri
->create_flags
))
5164 return E_INVALIDARG
;
5166 hres
= canonicalize_uri(&parse
, This
, data
.uri
->create_flags
);
5170 This
->create_flags
= data
.uri
->create_flags
;
5171 IUri_Release(&data
.uri
->IUri_iface
);
5173 return IUri_QueryInterface(&This
->IUri_iface
, riid
, ppv
);
5176 hres
= IPersistStream_Load(&This
->IPersistStream_iface
, pStm
);
5180 return IUri_QueryInterface(&This
->IUri_iface
, riid
, ppv
);
5183 static HRESULT WINAPI
Marshal_ReleaseMarshalData(IMarshal
*iface
, IStream
*pStm
)
5185 Uri
*This
= impl_from_IMarshal(iface
);
5190 TRACE("(%p)->(%p)\n", This
, pStm
);
5193 return E_INVALIDARG
;
5195 hres
= IStream_Read(pStm
, header
, 2*sizeof(DWORD
), NULL
);
5199 if(header
[1] == MSHCTX_INPROC
) {
5200 struct inproc_marshal_uri data
;
5202 hres
= IStream_Read(pStm
, data
.unk
, sizeof(data
)-2*sizeof(DWORD
), NULL
);
5206 IUri_Release(&data
.uri
->IUri_iface
);
5210 off
.u
.LowPart
= header
[0]-sizeof(header
)-2;
5212 return IStream_Seek(pStm
, off
, STREAM_SEEK_CUR
, NULL
);
5215 static HRESULT WINAPI
Marshal_DisconnectObject(IMarshal
*iface
, DWORD dwReserved
)
5217 Uri
*This
= impl_from_IMarshal(iface
);
5218 TRACE("(%p)->(%lx)\n", This
, dwReserved
);
5222 static const IMarshalVtbl MarshalVtbl
= {
5223 Marshal_QueryInterface
,
5226 Marshal_GetUnmarshalClass
,
5227 Marshal_GetMarshalSizeMax
,
5228 Marshal_MarshalInterface
,
5229 Marshal_UnmarshalInterface
,
5230 Marshal_ReleaseMarshalData
,
5231 Marshal_DisconnectObject
5234 HRESULT
Uri_Construct(IUnknown
*pUnkOuter
, LPVOID
*ppobj
)
5236 Uri
*ret
= calloc(1, sizeof(Uri
));
5238 TRACE("(%p %p)\n", pUnkOuter
, ppobj
);
5242 return E_OUTOFMEMORY
;
5244 ret
->IUri_iface
.lpVtbl
= &UriVtbl
;
5245 ret
->IUriBuilderFactory_iface
.lpVtbl
= &UriBuilderFactoryVtbl
;
5246 ret
->IPersistStream_iface
.lpVtbl
= &PersistStreamVtbl
;
5247 ret
->IMarshal_iface
.lpVtbl
= &MarshalVtbl
;
5250 *ppobj
= &ret
->IUri_iface
;
5254 /***********************************************************************
5255 * CreateUri (urlmon.@)
5257 * Creates a new IUri object using the URI represented by pwzURI. This function
5258 * parses and validates the components of pwzURI and then canonicalizes the
5259 * parsed components.
5262 * pwzURI [I] The URI to parse, validate, and canonicalize.
5263 * dwFlags [I] Flags which can affect how the parsing/canonicalization is performed.
5264 * dwReserved [I] Reserved (not used).
5265 * ppURI [O] The resulting IUri after parsing/canonicalization occurs.
5268 * Success: Returns S_OK. ppURI contains the pointer to the newly allocated IUri.
5269 * Failure: E_INVALIDARG if there are invalid flag combinations in dwFlags, or an
5270 * invalid parameter, or pwzURI doesn't represent a valid URI.
5271 * E_OUTOFMEMORY if any memory allocation fails.
5275 * Uri_CREATE_CANONICALIZE, Uri_CREATE_DECODE_EXTRA_INFO, Uri_CREATE_CRACK_UNKNOWN_SCHEMES,
5276 * Uri_CREATE_PRE_PROCESS_HTML_URI, Uri_CREATE_NO_IE_SETTINGS.
5278 HRESULT WINAPI
CreateUri(LPCWSTR pwzURI
, DWORD dwFlags
, DWORD_PTR dwReserved
, IUri
**ppURI
)
5280 const DWORD supported_flags
= Uri_CREATE_ALLOW_RELATIVE
|Uri_CREATE_ALLOW_IMPLICIT_WILDCARD_SCHEME
|
5281 Uri_CREATE_ALLOW_IMPLICIT_FILE_SCHEME
|Uri_CREATE_NO_CANONICALIZE
|Uri_CREATE_CANONICALIZE
|
5282 Uri_CREATE_DECODE_EXTRA_INFO
|Uri_CREATE_NO_DECODE_EXTRA_INFO
|Uri_CREATE_CRACK_UNKNOWN_SCHEMES
|
5283 Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES
|Uri_CREATE_PRE_PROCESS_HTML_URI
|Uri_CREATE_NO_PRE_PROCESS_HTML_URI
|
5284 Uri_CREATE_NO_IE_SETTINGS
|Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS
|Uri_CREATE_FILE_USE_DOS_PATH
;
5289 TRACE("(%s %lx %Ix %p)\n", debugstr_w(pwzURI
), dwFlags
, dwReserved
, ppURI
);
5292 return E_INVALIDARG
;
5296 return E_INVALIDARG
;
5299 /* Check for invalid flags. */
5300 if(has_invalid_flag_combination(dwFlags
)) {
5302 return E_INVALIDARG
;
5305 /* Currently unsupported. */
5306 if(dwFlags
& ~supported_flags
)
5307 FIXME("Ignoring unsupported flag(s) %lx\n", dwFlags
& ~supported_flags
);
5309 hr
= Uri_Construct(NULL
, (void**)&ret
);
5315 /* Explicitly set the default flags if it doesn't cause a flag conflict. */
5316 apply_default_flags(&dwFlags
);
5318 /* Pre process the URI, unless told otherwise. */
5319 if(!(dwFlags
& Uri_CREATE_NO_PRE_PROCESS_HTML_URI
))
5320 ret
->raw_uri
= pre_process_uri(pwzURI
);
5322 ret
->raw_uri
= SysAllocString(pwzURI
);
5326 return E_OUTOFMEMORY
;
5329 memset(&data
, 0, sizeof(parse_data
));
5330 data
.uri
= ret
->raw_uri
;
5332 /* Validate and parse the URI into its components. */
5333 if(!parse_uri(&data
, dwFlags
)) {
5334 /* Encountered an unsupported or invalid URI */
5335 IUri_Release(&ret
->IUri_iface
);
5337 return E_INVALIDARG
;
5340 /* Canonicalize the URI. */
5341 hr
= canonicalize_uri(&data
, ret
, dwFlags
);
5343 IUri_Release(&ret
->IUri_iface
);
5348 ret
->create_flags
= dwFlags
;
5350 *ppURI
= &ret
->IUri_iface
;
5354 /***********************************************************************
5355 * CreateUriWithFragment (urlmon.@)
5357 * Creates a new IUri object. This is almost the same as CreateUri, expect that
5358 * it allows you to explicitly specify a fragment (pwzFragment) for pwzURI.
5361 * pwzURI [I] The URI to parse and perform canonicalization on.
5362 * pwzFragment [I] The explicit fragment string which should be added to pwzURI.
5363 * dwFlags [I] The flags which will be passed to CreateUri.
5364 * dwReserved [I] Reserved (not used).
5365 * ppURI [O] The resulting IUri after parsing/canonicalization.
5368 * Success: S_OK. ppURI contains the pointer to the newly allocated IUri.
5369 * Failure: E_INVALIDARG if pwzURI already contains a fragment and pwzFragment
5370 * isn't NULL. Will also return E_INVALIDARG for the same reasons as
5371 * CreateUri will. E_OUTOFMEMORY if any allocation fails.
5373 HRESULT WINAPI
CreateUriWithFragment(LPCWSTR pwzURI
, LPCWSTR pwzFragment
, DWORD dwFlags
,
5374 DWORD_PTR dwReserved
, IUri
**ppURI
)
5377 TRACE("(%s %s %lx %Ix %p)\n", debugstr_w(pwzURI
), debugstr_w(pwzFragment
), dwFlags
, dwReserved
, ppURI
);
5380 return E_INVALIDARG
;
5384 return E_INVALIDARG
;
5387 /* Check if a fragment should be appended to the URI string. */
5390 DWORD uri_len
, frag_len
;
5393 /* Check if the original URI already has a fragment component. */
5394 if(StrChrW(pwzURI
, '#')) {
5396 return E_INVALIDARG
;
5399 uri_len
= lstrlenW(pwzURI
);
5400 frag_len
= lstrlenW(pwzFragment
);
5402 /* If the fragment doesn't start with a '#', one will be added. */
5403 add_pound
= *pwzFragment
!= '#';
5406 uriW
= malloc((uri_len
+ frag_len
+ 2) * sizeof(WCHAR
));
5408 uriW
= malloc((uri_len
+ frag_len
+ 1) * sizeof(WCHAR
));
5411 return E_OUTOFMEMORY
;
5413 memcpy(uriW
, pwzURI
, uri_len
*sizeof(WCHAR
));
5415 uriW
[uri_len
++] = '#';
5416 memcpy(uriW
+uri_len
, pwzFragment
, (frag_len
+1)*sizeof(WCHAR
));
5418 hres
= CreateUri(uriW
, dwFlags
, 0, ppURI
);
5422 /* A fragment string wasn't specified, so just forward the call. */
5423 hres
= CreateUri(pwzURI
, dwFlags
, 0, ppURI
);
5428 static HRESULT
build_uri(const UriBuilder
*builder
, IUri
**uri
, DWORD create_flags
,
5429 DWORD use_orig_flags
, DWORD encoding_mask
)
5438 if(encoding_mask
&& (!builder
->uri
|| builder
->modified_props
)) {
5443 /* Decide what flags should be used when creating the Uri. */
5444 if((use_orig_flags
& UriBuilder_USE_ORIGINAL_FLAGS
) && builder
->uri
)
5445 create_flags
= builder
->uri
->create_flags
;
5447 if(has_invalid_flag_combination(create_flags
)) {
5449 return E_INVALIDARG
;
5452 /* Set the default flags if they don't cause a conflict. */
5453 apply_default_flags(&create_flags
);
5456 /* Return the base IUri if no changes have been made and the create_flags match. */
5457 if(builder
->uri
&& !builder
->modified_props
&& builder
->uri
->create_flags
== create_flags
) {
5458 *uri
= &builder
->uri
->IUri_iface
;
5463 hr
= validate_components(builder
, &data
, create_flags
);
5469 hr
= Uri_Construct(NULL
, (void**)&ret
);
5475 hr
= generate_uri(builder
, &data
, ret
, create_flags
);
5477 IUri_Release(&ret
->IUri_iface
);
5482 *uri
= &ret
->IUri_iface
;
5486 static inline UriBuilder
* impl_from_IUriBuilder(IUriBuilder
*iface
)
5488 return CONTAINING_RECORD(iface
, UriBuilder
, IUriBuilder_iface
);
5491 static HRESULT WINAPI
UriBuilder_QueryInterface(IUriBuilder
*iface
, REFIID riid
, void **ppv
)
5493 UriBuilder
*This
= impl_from_IUriBuilder(iface
);
5495 if(IsEqualGUID(&IID_IUnknown
, riid
)) {
5496 TRACE("(%p)->(IID_IUnknown %p)\n", This
, ppv
);
5497 *ppv
= &This
->IUriBuilder_iface
;
5498 }else if(IsEqualGUID(&IID_IUriBuilder
, riid
)) {
5499 TRACE("(%p)->(IID_IUriBuilder %p)\n", This
, ppv
);
5500 *ppv
= &This
->IUriBuilder_iface
;
5502 TRACE("(%p)->(%s %p)\n", This
, debugstr_guid(riid
), ppv
);
5504 return E_NOINTERFACE
;
5507 IUnknown_AddRef((IUnknown
*)*ppv
);
5511 static ULONG WINAPI
UriBuilder_AddRef(IUriBuilder
*iface
)
5513 UriBuilder
*This
= impl_from_IUriBuilder(iface
);
5514 LONG ref
= InterlockedIncrement(&This
->ref
);
5516 TRACE("(%p) ref=%ld\n", This
, ref
);
5521 static ULONG WINAPI
UriBuilder_Release(IUriBuilder
*iface
)
5523 UriBuilder
*This
= impl_from_IUriBuilder(iface
);
5524 LONG ref
= InterlockedDecrement(&This
->ref
);
5526 TRACE("(%p) ref=%ld\n", This
, ref
);
5529 if(This
->uri
) IUri_Release(&This
->uri
->IUri_iface
);
5530 free(This
->fragment
);
5532 free(This
->password
);
5536 free(This
->username
);
5543 static HRESULT WINAPI
UriBuilder_CreateUriSimple(IUriBuilder
*iface
,
5544 DWORD dwAllowEncodingPropertyMask
,
5545 DWORD_PTR dwReserved
,
5548 UriBuilder
*This
= impl_from_IUriBuilder(iface
);
5550 TRACE("(%p)->(%ld %Id %p)\n", This
, dwAllowEncodingPropertyMask
, dwReserved
, ppIUri
);
5552 hr
= build_uri(This
, ppIUri
, 0, UriBuilder_USE_ORIGINAL_FLAGS
, dwAllowEncodingPropertyMask
);
5554 FIXME("(%p)->(%ld %Id %p)\n", This
, dwAllowEncodingPropertyMask
, dwReserved
, ppIUri
);
5558 static HRESULT WINAPI
UriBuilder_CreateUri(IUriBuilder
*iface
,
5559 DWORD dwCreateFlags
,
5560 DWORD dwAllowEncodingPropertyMask
,
5561 DWORD_PTR dwReserved
,
5564 UriBuilder
*This
= impl_from_IUriBuilder(iface
);
5566 TRACE("(%p)->(0x%08lx %ld %Id %p)\n", This
, dwCreateFlags
, dwAllowEncodingPropertyMask
, dwReserved
, ppIUri
);
5568 if(dwCreateFlags
== -1)
5569 hr
= build_uri(This
, ppIUri
, 0, UriBuilder_USE_ORIGINAL_FLAGS
, dwAllowEncodingPropertyMask
);
5571 hr
= build_uri(This
, ppIUri
, dwCreateFlags
, 0, dwAllowEncodingPropertyMask
);
5574 FIXME("(%p)->(0x%08lx %ld %Id %p)\n", This
, dwCreateFlags
, dwAllowEncodingPropertyMask
, dwReserved
, ppIUri
);
5578 static HRESULT WINAPI
UriBuilder_CreateUriWithFlags(IUriBuilder
*iface
,
5579 DWORD dwCreateFlags
,
5580 DWORD dwUriBuilderFlags
,
5581 DWORD dwAllowEncodingPropertyMask
,
5582 DWORD_PTR dwReserved
,
5585 UriBuilder
*This
= impl_from_IUriBuilder(iface
);
5587 TRACE("(%p)->(0x%08lx 0x%08lx %ld %Id %p)\n", This
, dwCreateFlags
, dwUriBuilderFlags
,
5588 dwAllowEncodingPropertyMask
, dwReserved
, ppIUri
);
5590 hr
= build_uri(This
, ppIUri
, dwCreateFlags
, dwUriBuilderFlags
, dwAllowEncodingPropertyMask
);
5592 FIXME("(%p)->(0x%08lx 0x%08lx %ld %Id %p)\n", This
, dwCreateFlags
, dwUriBuilderFlags
,
5593 dwAllowEncodingPropertyMask
, dwReserved
, ppIUri
);
5597 static HRESULT WINAPI
UriBuilder_GetIUri(IUriBuilder
*iface
, IUri
**ppIUri
)
5599 UriBuilder
*This
= impl_from_IUriBuilder(iface
);
5600 TRACE("(%p)->(%p)\n", This
, ppIUri
);
5606 IUri
*uri
= &This
->uri
->IUri_iface
;
5615 static HRESULT WINAPI
UriBuilder_SetIUri(IUriBuilder
*iface
, IUri
*pIUri
)
5617 UriBuilder
*This
= impl_from_IUriBuilder(iface
);
5618 TRACE("(%p)->(%p)\n", This
, pIUri
);
5623 if((uri
= get_uri_obj(pIUri
))) {
5624 /* Only reset the builder if its Uri isn't the same as
5625 * the Uri passed to the function.
5627 if(This
->uri
!= uri
) {
5628 reset_builder(This
);
5632 This
->port
= uri
->port
;
5637 FIXME("(%p)->(%p) Unknown IUri types not supported yet.\n", This
, pIUri
);
5640 } else if(This
->uri
)
5641 /* Only reset the builder if its Uri isn't NULL. */
5642 reset_builder(This
);
5647 static HRESULT WINAPI
UriBuilder_GetFragment(IUriBuilder
*iface
, DWORD
*pcchFragment
, LPCWSTR
*ppwzFragment
)
5649 UriBuilder
*This
= impl_from_IUriBuilder(iface
);
5650 TRACE("(%p)->(%p %p)\n", This
, pcchFragment
, ppwzFragment
);
5652 if(!This
->uri
|| This
->uri
->fragment_start
== -1 || This
->modified_props
& Uri_HAS_FRAGMENT
)
5653 return get_builder_component(&This
->fragment
, &This
->fragment_len
, NULL
, 0, ppwzFragment
, pcchFragment
);
5655 return get_builder_component(&This
->fragment
, &This
->fragment_len
, This
->uri
->canon_uri
+This
->uri
->fragment_start
,
5656 This
->uri
->fragment_len
, ppwzFragment
, pcchFragment
);
5659 static HRESULT WINAPI
UriBuilder_GetHost(IUriBuilder
*iface
, DWORD
*pcchHost
, LPCWSTR
*ppwzHost
)
5661 UriBuilder
*This
= impl_from_IUriBuilder(iface
);
5662 TRACE("(%p)->(%p %p)\n", This
, pcchHost
, ppwzHost
);
5664 if(!This
->uri
|| This
->uri
->host_start
== -1 || This
->modified_props
& Uri_HAS_HOST
)
5665 return get_builder_component(&This
->host
, &This
->host_len
, NULL
, 0, ppwzHost
, pcchHost
);
5667 if(This
->uri
->host_type
== Uri_HOST_IPV6
)
5668 /* Don't include the '[' and ']' around the address. */
5669 return get_builder_component(&This
->host
, &This
->host_len
, This
->uri
->canon_uri
+This
->uri
->host_start
+1,
5670 This
->uri
->host_len
-2, ppwzHost
, pcchHost
);
5672 return get_builder_component(&This
->host
, &This
->host_len
, This
->uri
->canon_uri
+This
->uri
->host_start
,
5673 This
->uri
->host_len
, ppwzHost
, pcchHost
);
5677 static HRESULT WINAPI
UriBuilder_GetPassword(IUriBuilder
*iface
, DWORD
*pcchPassword
, LPCWSTR
*ppwzPassword
)
5679 UriBuilder
*This
= impl_from_IUriBuilder(iface
);
5680 TRACE("(%p)->(%p %p)\n", This
, pcchPassword
, ppwzPassword
);
5682 if(!This
->uri
|| This
->uri
->userinfo_split
== -1 || This
->modified_props
& Uri_HAS_PASSWORD
)
5683 return get_builder_component(&This
->password
, &This
->password_len
, NULL
, 0, ppwzPassword
, pcchPassword
);
5685 const WCHAR
*start
= This
->uri
->canon_uri
+This
->uri
->userinfo_start
+This
->uri
->userinfo_split
+1;
5686 DWORD len
= This
->uri
->userinfo_len
-This
->uri
->userinfo_split
-1;
5687 return get_builder_component(&This
->password
, &This
->password_len
, start
, len
, ppwzPassword
, pcchPassword
);
5691 static HRESULT WINAPI
UriBuilder_GetPath(IUriBuilder
*iface
, DWORD
*pcchPath
, LPCWSTR
*ppwzPath
)
5693 UriBuilder
*This
= impl_from_IUriBuilder(iface
);
5694 TRACE("(%p)->(%p %p)\n", This
, pcchPath
, ppwzPath
);
5696 if(!This
->uri
|| This
->uri
->path_start
== -1 || This
->modified_props
& Uri_HAS_PATH
)
5697 return get_builder_component(&This
->path
, &This
->path_len
, NULL
, 0, ppwzPath
, pcchPath
);
5699 return get_builder_component(&This
->path
, &This
->path_len
, This
->uri
->canon_uri
+This
->uri
->path_start
,
5700 This
->uri
->path_len
, ppwzPath
, pcchPath
);
5703 static HRESULT WINAPI
UriBuilder_GetPort(IUriBuilder
*iface
, BOOL
*pfHasPort
, DWORD
*pdwPort
)
5705 UriBuilder
*This
= impl_from_IUriBuilder(iface
);
5706 TRACE("(%p)->(%p %p)\n", This
, pfHasPort
, pdwPort
);
5719 *pfHasPort
= This
->has_port
;
5720 *pdwPort
= This
->port
;
5724 static HRESULT WINAPI
UriBuilder_GetQuery(IUriBuilder
*iface
, DWORD
*pcchQuery
, LPCWSTR
*ppwzQuery
)
5726 UriBuilder
*This
= impl_from_IUriBuilder(iface
);
5727 TRACE("(%p)->(%p %p)\n", This
, pcchQuery
, ppwzQuery
);
5729 if(!This
->uri
|| This
->uri
->query_start
== -1 || This
->modified_props
& Uri_HAS_QUERY
)
5730 return get_builder_component(&This
->query
, &This
->query_len
, NULL
, 0, ppwzQuery
, pcchQuery
);
5732 return get_builder_component(&This
->query
, &This
->query_len
, This
->uri
->canon_uri
+This
->uri
->query_start
,
5733 This
->uri
->query_len
, ppwzQuery
, pcchQuery
);
5736 static HRESULT WINAPI
UriBuilder_GetSchemeName(IUriBuilder
*iface
, DWORD
*pcchSchemeName
, LPCWSTR
*ppwzSchemeName
)
5738 UriBuilder
*This
= impl_from_IUriBuilder(iface
);
5739 TRACE("(%p)->(%p %p)\n", This
, pcchSchemeName
, ppwzSchemeName
);
5741 if(!This
->uri
|| This
->uri
->scheme_start
== -1 || This
->modified_props
& Uri_HAS_SCHEME_NAME
)
5742 return get_builder_component(&This
->scheme
, &This
->scheme_len
, NULL
, 0, ppwzSchemeName
, pcchSchemeName
);
5744 return get_builder_component(&This
->scheme
, &This
->scheme_len
, This
->uri
->canon_uri
+This
->uri
->scheme_start
,
5745 This
->uri
->scheme_len
, ppwzSchemeName
, pcchSchemeName
);
5748 static HRESULT WINAPI
UriBuilder_GetUserName(IUriBuilder
*iface
, DWORD
*pcchUserName
, LPCWSTR
*ppwzUserName
)
5750 UriBuilder
*This
= impl_from_IUriBuilder(iface
);
5751 TRACE("(%p)->(%p %p)\n", This
, pcchUserName
, ppwzUserName
);
5753 if(!This
->uri
|| This
->uri
->userinfo_start
== -1 || This
->uri
->userinfo_split
== 0 ||
5754 This
->modified_props
& Uri_HAS_USER_NAME
)
5755 return get_builder_component(&This
->username
, &This
->username_len
, NULL
, 0, ppwzUserName
, pcchUserName
);
5757 const WCHAR
*start
= This
->uri
->canon_uri
+This
->uri
->userinfo_start
;
5759 /* Check if there's a password in the userinfo section. */
5760 if(This
->uri
->userinfo_split
> -1)
5761 /* Don't include the password. */
5762 return get_builder_component(&This
->username
, &This
->username_len
, start
,
5763 This
->uri
->userinfo_split
, ppwzUserName
, pcchUserName
);
5765 return get_builder_component(&This
->username
, &This
->username_len
, start
,
5766 This
->uri
->userinfo_len
, ppwzUserName
, pcchUserName
);
5770 static HRESULT WINAPI
UriBuilder_SetFragment(IUriBuilder
*iface
, LPCWSTR pwzNewValue
)
5772 UriBuilder
*This
= impl_from_IUriBuilder(iface
);
5773 TRACE("(%p)->(%s)\n", This
, debugstr_w(pwzNewValue
));
5774 return set_builder_component(&This
->fragment
, &This
->fragment_len
, pwzNewValue
, '#',
5775 &This
->modified_props
, Uri_HAS_FRAGMENT
);
5778 static HRESULT WINAPI
UriBuilder_SetHost(IUriBuilder
*iface
, LPCWSTR pwzNewValue
)
5780 UriBuilder
*This
= impl_from_IUriBuilder(iface
);
5781 TRACE("(%p)->(%s)\n", This
, debugstr_w(pwzNewValue
));
5783 /* Host name can't be set to NULL. */
5785 return E_INVALIDARG
;
5787 return set_builder_component(&This
->host
, &This
->host_len
, pwzNewValue
, 0,
5788 &This
->modified_props
, Uri_HAS_HOST
);
5791 static HRESULT WINAPI
UriBuilder_SetPassword(IUriBuilder
*iface
, LPCWSTR pwzNewValue
)
5793 UriBuilder
*This
= impl_from_IUriBuilder(iface
);
5794 TRACE("(%p)->(%s)\n", This
, debugstr_w(pwzNewValue
));
5795 return set_builder_component(&This
->password
, &This
->password_len
, pwzNewValue
, 0,
5796 &This
->modified_props
, Uri_HAS_PASSWORD
);
5799 static HRESULT WINAPI
UriBuilder_SetPath(IUriBuilder
*iface
, LPCWSTR pwzNewValue
)
5801 UriBuilder
*This
= impl_from_IUriBuilder(iface
);
5802 TRACE("(%p)->(%s)\n", This
, debugstr_w(pwzNewValue
));
5803 return set_builder_component(&This
->path
, &This
->path_len
, pwzNewValue
, 0,
5804 &This
->modified_props
, Uri_HAS_PATH
);
5807 static HRESULT WINAPI
UriBuilder_SetPort(IUriBuilder
*iface
, BOOL fHasPort
, DWORD dwNewValue
)
5809 UriBuilder
*This
= impl_from_IUriBuilder(iface
);
5810 TRACE("(%p)->(%d %ld)\n", This
, fHasPort
, dwNewValue
);
5812 This
->has_port
= fHasPort
;
5813 This
->port
= dwNewValue
;
5814 This
->modified_props
|= Uri_HAS_PORT
;
5818 static HRESULT WINAPI
UriBuilder_SetQuery(IUriBuilder
*iface
, LPCWSTR pwzNewValue
)
5820 UriBuilder
*This
= impl_from_IUriBuilder(iface
);
5821 TRACE("(%p)->(%s)\n", This
, debugstr_w(pwzNewValue
));
5822 return set_builder_component(&This
->query
, &This
->query_len
, pwzNewValue
, '?',
5823 &This
->modified_props
, Uri_HAS_QUERY
);
5826 static HRESULT WINAPI
UriBuilder_SetSchemeName(IUriBuilder
*iface
, LPCWSTR pwzNewValue
)
5828 UriBuilder
*This
= impl_from_IUriBuilder(iface
);
5829 TRACE("(%p)->(%s)\n", This
, debugstr_w(pwzNewValue
));
5831 /* Only set the scheme name if it's not NULL or empty. */
5832 if(!pwzNewValue
|| !*pwzNewValue
)
5833 return E_INVALIDARG
;
5835 return set_builder_component(&This
->scheme
, &This
->scheme_len
, pwzNewValue
, 0,
5836 &This
->modified_props
, Uri_HAS_SCHEME_NAME
);
5839 static HRESULT WINAPI
UriBuilder_SetUserName(IUriBuilder
*iface
, LPCWSTR pwzNewValue
)
5841 UriBuilder
*This
= impl_from_IUriBuilder(iface
);
5842 TRACE("(%p)->(%s)\n", This
, debugstr_w(pwzNewValue
));
5843 return set_builder_component(&This
->username
, &This
->username_len
, pwzNewValue
, 0,
5844 &This
->modified_props
, Uri_HAS_USER_NAME
);
5847 static HRESULT WINAPI
UriBuilder_RemoveProperties(IUriBuilder
*iface
, DWORD dwPropertyMask
)
5849 const DWORD accepted_flags
= Uri_HAS_AUTHORITY
|Uri_HAS_DOMAIN
|Uri_HAS_EXTENSION
|Uri_HAS_FRAGMENT
|Uri_HAS_HOST
|
5850 Uri_HAS_PASSWORD
|Uri_HAS_PATH
|Uri_HAS_PATH_AND_QUERY
|Uri_HAS_QUERY
|
5851 Uri_HAS_USER_INFO
|Uri_HAS_USER_NAME
;
5853 UriBuilder
*This
= impl_from_IUriBuilder(iface
);
5854 TRACE("(%p)->(0x%08lx)\n", This
, dwPropertyMask
);
5856 if(dwPropertyMask
& ~accepted_flags
)
5857 return E_INVALIDARG
;
5859 if(dwPropertyMask
& Uri_HAS_FRAGMENT
)
5860 UriBuilder_SetFragment(iface
, NULL
);
5862 /* Even though you can't set the host name to NULL or an
5863 * empty string, you can still remove it... for some reason.
5865 if(dwPropertyMask
& Uri_HAS_HOST
)
5866 set_builder_component(&This
->host
, &This
->host_len
, NULL
, 0,
5867 &This
->modified_props
, Uri_HAS_HOST
);
5869 if(dwPropertyMask
& Uri_HAS_PASSWORD
)
5870 UriBuilder_SetPassword(iface
, NULL
);
5872 if(dwPropertyMask
& Uri_HAS_PATH
)
5873 UriBuilder_SetPath(iface
, NULL
);
5875 if(dwPropertyMask
& Uri_HAS_PORT
)
5876 UriBuilder_SetPort(iface
, FALSE
, 0);
5878 if(dwPropertyMask
& Uri_HAS_QUERY
)
5879 UriBuilder_SetQuery(iface
, NULL
);
5881 if(dwPropertyMask
& Uri_HAS_USER_NAME
)
5882 UriBuilder_SetUserName(iface
, NULL
);
5887 static HRESULT WINAPI
UriBuilder_HasBeenModified(IUriBuilder
*iface
, BOOL
*pfModified
)
5889 UriBuilder
*This
= impl_from_IUriBuilder(iface
);
5890 TRACE("(%p)->(%p)\n", This
, pfModified
);
5895 *pfModified
= This
->modified_props
> 0;
5899 static const IUriBuilderVtbl UriBuilderVtbl
= {
5900 UriBuilder_QueryInterface
,
5903 UriBuilder_CreateUriSimple
,
5904 UriBuilder_CreateUri
,
5905 UriBuilder_CreateUriWithFlags
,
5908 UriBuilder_GetFragment
,
5910 UriBuilder_GetPassword
,
5913 UriBuilder_GetQuery
,
5914 UriBuilder_GetSchemeName
,
5915 UriBuilder_GetUserName
,
5916 UriBuilder_SetFragment
,
5918 UriBuilder_SetPassword
,
5921 UriBuilder_SetQuery
,
5922 UriBuilder_SetSchemeName
,
5923 UriBuilder_SetUserName
,
5924 UriBuilder_RemoveProperties
,
5925 UriBuilder_HasBeenModified
,
5928 /***********************************************************************
5929 * CreateIUriBuilder (urlmon.@)
5931 HRESULT WINAPI
CreateIUriBuilder(IUri
*pIUri
, DWORD dwFlags
, DWORD_PTR dwReserved
, IUriBuilder
**ppIUriBuilder
)
5935 TRACE("(%p %lx %Ix %p)\n", pIUri
, dwFlags
, dwReserved
, ppIUriBuilder
);
5940 ret
= calloc(1, sizeof(UriBuilder
));
5942 return E_OUTOFMEMORY
;
5944 ret
->IUriBuilder_iface
.lpVtbl
= &UriBuilderVtbl
;
5950 if((uri
= get_uri_obj(pIUri
))) {
5951 if(!uri
->create_flags
) {
5953 return E_UNEXPECTED
;
5959 /* Windows doesn't set 'has_port' to TRUE in this case. */
5960 ret
->port
= uri
->port
;
5964 *ppIUriBuilder
= NULL
;
5965 FIXME("(%p %lx %Ix %p): Unknown IUri types not supported yet.\n", pIUri
, dwFlags
,
5966 dwReserved
, ppIUriBuilder
);
5971 *ppIUriBuilder
= &ret
->IUriBuilder_iface
;
5975 /* Merges the base path with the relative path and stores the resulting path
5976 * and path len in 'result' and 'result_len'.
5978 static HRESULT
merge_paths(parse_data
*data
, const WCHAR
*base
, DWORD base_len
, const WCHAR
*relative
,
5979 DWORD relative_len
, WCHAR
**result
, DWORD
*result_len
, DWORD flags
)
5981 const WCHAR
*end
= NULL
;
5982 DWORD base_copy_len
= 0;
5986 if(data
->scheme_type
== URL_SCHEME_MK
&& *relative
== '/') {
5987 /* Find '::' segment */
5988 for(end
= base
; end
< base
+base_len
-1; end
++) {
5989 if(end
[0] == ':' && end
[1] == ':') {
5995 /* If not found, try finding the end of @xxx: */
5996 if(end
== base
+base_len
-1)
5997 end
= *base
== '@' ? wmemchr(base
, ':', base_len
) : NULL
;
5999 /* Find the characters that will be copied over from the base path. */
6000 for (end
= base
+ base_len
- 1; end
>= base
; end
--) if (*end
== '/') break;
6001 if(end
< base
&& data
->scheme_type
== URL_SCHEME_FILE
)
6002 /* Try looking for a '\\'. */
6003 for (end
= base
+ base_len
- 1; end
>= base
; end
--) if (*end
== '\\') break;
6007 if (end
) base_copy_len
= (end
+1)-base
;
6008 *result
= malloc((base_copy_len
+ relative_len
+ 1) * sizeof(WCHAR
));
6012 return E_OUTOFMEMORY
;
6016 memcpy(ptr
, base
, base_copy_len
*sizeof(WCHAR
));
6017 ptr
+= base_copy_len
;
6019 memcpy(ptr
, relative
, relative_len
*sizeof(WCHAR
));
6020 ptr
+= relative_len
;
6023 *result_len
= (ptr
-*result
);
6024 TRACE("ret %s\n", debugstr_wn(*result
, *result_len
));
6028 static HRESULT
combine_uri(Uri
*base
, Uri
*relative
, DWORD flags
, IUri
**result
, DWORD extras
) {
6032 Uri
*proc_uri
= base
;
6033 DWORD create_flags
= 0, len
= 0;
6035 memset(&data
, 0, sizeof(parse_data
));
6037 /* Base case is when the relative Uri has a scheme name,
6038 * if it does, then 'result' will contain the same data
6039 * as the relative Uri.
6041 if(relative
->scheme_start
> -1) {
6042 data
.uri
= SysAllocString(relative
->raw_uri
);
6045 return E_OUTOFMEMORY
;
6048 parse_uri(&data
, Uri_CREATE_ALLOW_IMPLICIT_FILE_SCHEME
);
6050 hr
= Uri_Construct(NULL
, (void**)&ret
);
6056 if(extras
& COMBINE_URI_FORCE_FLAG_USE
) {
6057 if(flags
& URL_DONT_SIMPLIFY
)
6058 create_flags
|= Uri_CREATE_NO_CANONICALIZE
;
6059 if(flags
& URL_DONT_UNESCAPE_EXTRA_INFO
)
6060 create_flags
|= Uri_CREATE_NO_DECODE_EXTRA_INFO
;
6063 ret
->raw_uri
= data
.uri
;
6064 hr
= canonicalize_uri(&data
, ret
, create_flags
);
6066 IUri_Release(&ret
->IUri_iface
);
6071 apply_default_flags(&create_flags
);
6072 ret
->create_flags
= create_flags
;
6074 *result
= &ret
->IUri_iface
;
6077 DWORD raw_flags
= 0;
6079 if(base
->scheme_start
> -1) {
6080 data
.scheme
= base
->canon_uri
+base
->scheme_start
;
6081 data
.scheme_len
= base
->scheme_len
;
6082 data
.scheme_type
= base
->scheme_type
;
6084 data
.is_relative
= TRUE
;
6085 data
.scheme_type
= URL_SCHEME_UNKNOWN
;
6086 create_flags
|= Uri_CREATE_ALLOW_RELATIVE
;
6089 if(relative
->authority_start
> -1)
6090 proc_uri
= relative
;
6092 if(proc_uri
->authority_start
> -1) {
6093 if(proc_uri
->userinfo_start
> -1 && proc_uri
->userinfo_split
!= 0) {
6094 data
.username
= proc_uri
->canon_uri
+proc_uri
->userinfo_start
;
6095 data
.username_len
= (proc_uri
->userinfo_split
> -1) ? proc_uri
->userinfo_split
: proc_uri
->userinfo_len
;
6098 if(proc_uri
->userinfo_split
> -1) {
6099 data
.password
= proc_uri
->canon_uri
+proc_uri
->userinfo_start
+proc_uri
->userinfo_split
+1;
6100 data
.password_len
= proc_uri
->userinfo_len
-proc_uri
->userinfo_split
-1;
6103 if(proc_uri
->host_start
> -1) {
6104 const WCHAR
*host
= proc_uri
->canon_uri
+proc_uri
->host_start
;
6105 parse_host(&host
, &data
, 0);
6108 if(proc_uri
->has_port
) {
6109 data
.has_port
= TRUE
;
6110 data
.port_value
= proc_uri
->port
;
6112 } else if(base
->scheme_type
!= URL_SCHEME_FILE
)
6113 data
.is_opaque
= TRUE
;
6115 if(proc_uri
== relative
|| relative
->path_start
== -1 || !relative
->path_len
) {
6116 if(proc_uri
->path_start
> -1) {
6117 data
.path
= proc_uri
->canon_uri
+proc_uri
->path_start
;
6118 data
.path_len
= proc_uri
->path_len
;
6119 } else if(!data
.is_opaque
) {
6120 /* Just set the path as a '/' if the base didn't have
6121 * one and if it's a hierarchical URI.
6127 if(relative
->query_start
> -1)
6128 proc_uri
= relative
;
6130 if(proc_uri
->query_start
> -1) {
6131 data
.query
= proc_uri
->canon_uri
+proc_uri
->query_start
;
6132 data
.query_len
= proc_uri
->query_len
;
6135 const WCHAR
*ptr
, **pptr
;
6136 DWORD path_offset
= 0, path_len
= 0;
6138 /* There's two possibilities on what will happen to the path component
6139 * of the result IUri. First, if the relative path begins with a '/'
6140 * then the resulting path will just be the relative path. Second, if
6141 * relative path doesn't begin with a '/' then the base path and relative
6142 * path are merged together.
6144 if(relative
->path_len
&& *(relative
->canon_uri
+relative
->path_start
) == '/' && data
.scheme_type
!= URL_SCHEME_MK
) {
6146 BOOL copy_drive_path
= FALSE
;
6148 /* If the relative IUri's path starts with a '/', then we
6149 * don't use the base IUri's path. Unless the base IUri
6150 * is a file URI, in which case it uses the drive path of
6151 * the base IUri (if it has any) in the new path.
6153 if(base
->scheme_type
== URL_SCHEME_FILE
) {
6154 if(base
->path_len
> 3 && *(base
->canon_uri
+base
->path_start
) == '/' &&
6155 is_drive_path(base
->canon_uri
+base
->path_start
+1)) {
6157 copy_drive_path
= TRUE
;
6161 path_len
+= relative
->path_len
;
6163 path
= malloc((path_len
+ 1) * sizeof(WCHAR
));
6166 return E_OUTOFMEMORY
;
6171 /* Copy the base paths, drive path over. */
6172 if(copy_drive_path
) {
6173 memcpy(tmp
, base
->canon_uri
+base
->path_start
, 3*sizeof(WCHAR
));
6177 memcpy(tmp
, relative
->canon_uri
+relative
->path_start
, relative
->path_len
*sizeof(WCHAR
));
6178 path
[path_len
] = '\0';
6180 /* Merge the base path with the relative path. */
6181 hr
= merge_paths(&data
, base
->canon_uri
+base
->path_start
, base
->path_len
,
6182 relative
->canon_uri
+relative
->path_start
, relative
->path_len
,
6183 &path
, &path_len
, flags
);
6189 /* If the resulting IUri is a file URI, the drive path isn't
6190 * reduced out when the dot segments are removed.
6192 if(path_len
>= 3 && data
.scheme_type
== URL_SCHEME_FILE
&& !data
.host
) {
6193 if(*path
== '/' && is_drive_path(path
+1))
6195 else if(is_drive_path(path
))
6200 /* Check if the dot segments need to be removed from the path. */
6201 if(!(flags
& URL_DONT_SIMPLIFY
) && !data
.is_opaque
) {
6202 DWORD offset
= (path_offset
> 0) ? path_offset
+1 : 0;
6203 DWORD new_len
= remove_dot_segments(path
+offset
,path_len
-offset
);
6205 if(new_len
!= path_len
) {
6206 WCHAR
*tmp
= realloc(path
, (offset
+ new_len
+ 1) * sizeof(WCHAR
));
6210 return E_OUTOFMEMORY
;
6213 tmp
[new_len
+offset
] = '\0';
6215 path_len
= new_len
+offset
;
6219 if(relative
->query_start
> -1) {
6220 data
.query
= relative
->canon_uri
+relative
->query_start
;
6221 data
.query_len
= relative
->query_len
;
6224 /* Make sure the path component is valid. */
6227 if((data
.is_opaque
&& !parse_path_opaque(pptr
, &data
, 0)) ||
6228 (!data
.is_opaque
&& !parse_path_hierarchical(pptr
, &data
, 0))) {
6231 return E_INVALIDARG
;
6235 if(relative
->fragment_start
> -1) {
6236 data
.fragment
= relative
->canon_uri
+relative
->fragment_start
;
6237 data
.fragment_len
= relative
->fragment_len
;
6240 if(flags
& URL_DONT_SIMPLIFY
)
6241 raw_flags
|= RAW_URI_FORCE_PORT_DISP
;
6242 if(flags
& URL_FILE_USE_PATHURL
)
6243 raw_flags
|= RAW_URI_CONVERT_TO_DOS_PATH
;
6245 len
= generate_raw_uri(&data
, data
.uri
, raw_flags
);
6246 data
.uri
= SysAllocStringLen(NULL
, len
);
6250 return E_OUTOFMEMORY
;
6253 generate_raw_uri(&data
, data
.uri
, raw_flags
);
6255 hr
= Uri_Construct(NULL
, (void**)&ret
);
6257 SysFreeString(data
.uri
);
6263 if(flags
& URL_DONT_SIMPLIFY
)
6264 create_flags
|= Uri_CREATE_NO_CANONICALIZE
;
6265 if(flags
& URL_FILE_USE_PATHURL
)
6266 create_flags
|= Uri_CREATE_FILE_USE_DOS_PATH
;
6268 ret
->raw_uri
= data
.uri
;
6269 hr
= canonicalize_uri(&data
, ret
, create_flags
);
6271 IUri_Release(&ret
->IUri_iface
);
6276 if(flags
& URL_DONT_SIMPLIFY
)
6277 ret
->display_modifiers
|= URI_DISPLAY_NO_DEFAULT_PORT_AUTH
;
6279 apply_default_flags(&create_flags
);
6280 ret
->create_flags
= create_flags
;
6281 *result
= &ret
->IUri_iface
;
6289 /***********************************************************************
6290 * CoInternetCombineIUri (urlmon.@)
6292 HRESULT WINAPI
CoInternetCombineIUri(IUri
*pBaseUri
, IUri
*pRelativeUri
, DWORD dwCombineFlags
,
6293 IUri
**ppCombinedUri
, DWORD_PTR dwReserved
)
6296 IInternetProtocolInfo
*info
;
6297 Uri
*relative
, *base
;
6298 TRACE("(%p %p %lx %p %Ix)\n", pBaseUri
, pRelativeUri
, dwCombineFlags
, ppCombinedUri
, dwReserved
);
6301 return E_INVALIDARG
;
6303 if(!pBaseUri
|| !pRelativeUri
) {
6304 *ppCombinedUri
= NULL
;
6305 return E_INVALIDARG
;
6308 relative
= get_uri_obj(pRelativeUri
);
6309 base
= get_uri_obj(pBaseUri
);
6310 if(!relative
|| !base
) {
6311 *ppCombinedUri
= NULL
;
6312 FIXME("(%p %p %lx %p %Ix) Unknown IUri types not supported yet.\n",
6313 pBaseUri
, pRelativeUri
, dwCombineFlags
, ppCombinedUri
, dwReserved
);
6317 info
= get_protocol_info(base
->canon_uri
);
6319 WCHAR result
[INTERNET_MAX_URL_LENGTH
+1];
6320 DWORD result_len
= 0;
6322 hr
= IInternetProtocolInfo_CombineUrl(info
, base
->canon_uri
, relative
->canon_uri
, dwCombineFlags
,
6323 result
, INTERNET_MAX_URL_LENGTH
+1, &result_len
, 0);
6324 IInternetProtocolInfo_Release(info
);
6326 hr
= CreateUri(result
, Uri_CREATE_ALLOW_RELATIVE
, 0, ppCombinedUri
);
6332 return combine_uri(base
, relative
, dwCombineFlags
, ppCombinedUri
, 0);
6335 /***********************************************************************
6336 * CoInternetCombineUrlEx (urlmon.@)
6338 HRESULT WINAPI
CoInternetCombineUrlEx(IUri
*pBaseUri
, LPCWSTR pwzRelativeUrl
, DWORD dwCombineFlags
,
6339 IUri
**ppCombinedUri
, DWORD_PTR dwReserved
)
6344 IInternetProtocolInfo
*info
;
6346 TRACE("(%p %s %lx %p %Ix)\n", pBaseUri
, debugstr_w(pwzRelativeUrl
), dwCombineFlags
,
6347 ppCombinedUri
, dwReserved
);
6352 if(!pwzRelativeUrl
) {
6353 *ppCombinedUri
= NULL
;
6354 return E_UNEXPECTED
;
6358 *ppCombinedUri
= NULL
;
6359 return E_INVALIDARG
;
6362 base
= get_uri_obj(pBaseUri
);
6364 *ppCombinedUri
= NULL
;
6365 FIXME("(%p %s %lx %p %Ix) Unknown IUri's not supported yet.\n", pBaseUri
, debugstr_w(pwzRelativeUrl
),
6366 dwCombineFlags
, ppCombinedUri
, dwReserved
);
6370 info
= get_protocol_info(base
->canon_uri
);
6372 WCHAR result
[INTERNET_MAX_URL_LENGTH
+1];
6373 DWORD result_len
= 0;
6375 hr
= IInternetProtocolInfo_CombineUrl(info
, base
->canon_uri
, pwzRelativeUrl
, dwCombineFlags
,
6376 result
, INTERNET_MAX_URL_LENGTH
+1, &result_len
, 0);
6377 IInternetProtocolInfo_Release(info
);
6379 hr
= CreateUri(result
, Uri_CREATE_ALLOW_RELATIVE
, 0, ppCombinedUri
);
6385 hr
= CreateUri(pwzRelativeUrl
, Uri_CREATE_ALLOW_RELATIVE
|Uri_CREATE_ALLOW_IMPLICIT_FILE_SCHEME
, 0, &relative
);
6387 *ppCombinedUri
= NULL
;
6391 hr
= combine_uri(base
, get_uri_obj(relative
), dwCombineFlags
, ppCombinedUri
, COMBINE_URI_FORCE_FLAG_USE
);
6393 IUri_Release(relative
);
6397 static HRESULT
parse_canonicalize(const Uri
*uri
, DWORD flags
, LPWSTR output
,
6398 DWORD output_len
, DWORD
*result_len
)
6400 const WCHAR
*ptr
= NULL
;
6406 /* URL_UNESCAPE only has effect if none of the URL_ESCAPE flags are set. */
6407 const BOOL allow_unescape
= !(flags
& URL_ESCAPE_UNSAFE
) &&
6408 !(flags
& URL_ESCAPE_SPACES_ONLY
) &&
6409 !(flags
& URL_ESCAPE_PERCENT
);
6412 /* Check if the dot segments need to be removed from the
6415 if(uri
->scheme_start
> -1 && uri
->path_start
> -1) {
6416 ptr
= uri
->canon_uri
+uri
->scheme_start
+uri
->scheme_len
+1;
6419 reduce_path
= !(flags
& URL_DONT_SIMPLIFY
) &&
6420 ptr
&& check_hierarchical(pptr
);
6422 for(ptr
= uri
->canon_uri
; ptr
< uri
->canon_uri
+uri
->canon_len
; ++ptr
) {
6423 BOOL do_default_action
= TRUE
;
6425 /* Keep track of the path if we need to remove dot segments from
6428 if(reduce_path
&& !path
&& ptr
== uri
->canon_uri
+uri
->path_start
)
6431 /* Check if it's time to reduce the path. */
6432 if(reduce_path
&& ptr
== uri
->canon_uri
+uri
->path_start
+uri
->path_len
) {
6433 DWORD current_path_len
= (output
+len
) - path
;
6434 DWORD new_path_len
= remove_dot_segments(path
, current_path_len
);
6436 /* Update the current length. */
6437 len
-= (current_path_len
-new_path_len
);
6438 reduce_path
= FALSE
;
6442 const WCHAR decoded
= decode_pct_val(ptr
);
6444 if(allow_unescape
&& (flags
& URL_UNESCAPE
)) {
6445 if(len
< output_len
)
6446 output
[len
] = decoded
;
6449 do_default_action
= FALSE
;
6453 /* See if %'s needed to encoded. */
6454 if(do_default_action
&& (flags
& URL_ESCAPE_PERCENT
)) {
6455 if(len
+ 3 < output_len
)
6456 pct_encode_val(*ptr
, output
+len
);
6458 do_default_action
= FALSE
;
6460 } else if(*ptr
== ' ') {
6461 if((flags
& URL_ESCAPE_SPACES_ONLY
) &&
6462 !(flags
& URL_ESCAPE_UNSAFE
)) {
6463 if(len
+ 3 < output_len
)
6464 pct_encode_val(*ptr
, output
+len
);
6466 do_default_action
= FALSE
;
6468 } else if(is_ascii(*ptr
) && !is_reserved(*ptr
) && !is_unreserved(*ptr
)) {
6469 if(flags
& URL_ESCAPE_UNSAFE
) {
6470 if(len
+ 3 < output_len
)
6471 pct_encode_val(*ptr
, output
+len
);
6473 do_default_action
= FALSE
;
6477 if(do_default_action
) {
6478 if(len
< output_len
)
6484 /* Sometimes the path is the very last component of the IUri, so
6485 * see if the dot segments need to be reduced now.
6487 if(reduce_path
&& path
) {
6488 DWORD current_path_len
= (output
+len
) - path
;
6489 DWORD new_path_len
= remove_dot_segments(path
, current_path_len
);
6491 /* Update the current length. */
6492 len
-= (current_path_len
-new_path_len
);
6495 if(len
< output_len
)
6498 output
[output_len
-1] = 0;
6500 /* The null terminator isn't included in the length. */
6502 if(len
>= output_len
)
6503 return STRSAFE_E_INSUFFICIENT_BUFFER
;
6508 static HRESULT
parse_friendly(IUri
*uri
, LPWSTR output
, DWORD output_len
,
6515 hr
= IUri_GetPropertyLength(uri
, Uri_PROPERTY_DISPLAY_URI
, &display_len
, 0);
6521 *result_len
= display_len
;
6522 if(display_len
+1 > output_len
)
6523 return STRSAFE_E_INSUFFICIENT_BUFFER
;
6525 hr
= IUri_GetDisplayUri(uri
, &display
);
6531 memcpy(output
, display
, (display_len
+1)*sizeof(WCHAR
));
6532 SysFreeString(display
);
6536 static HRESULT
parse_rootdocument(const Uri
*uri
, LPWSTR output
, DWORD output_len
,
6539 static const WCHAR colon_slashesW
[] = {':','/','/'};
6544 /* Windows only returns the root document if the URI has an authority
6545 * and it's not an unknown scheme type or a file scheme type.
6547 if(uri
->authority_start
== -1 ||
6548 uri
->scheme_type
== URL_SCHEME_UNKNOWN
||
6549 uri
->scheme_type
== URL_SCHEME_FILE
) {
6552 return STRSAFE_E_INSUFFICIENT_BUFFER
;
6558 len
= uri
->scheme_len
+uri
->authority_len
;
6559 /* For the "://" and '/' which will be added. */
6562 if(len
+1 > output_len
) {
6564 return STRSAFE_E_INSUFFICIENT_BUFFER
;
6568 memcpy(ptr
, uri
->canon_uri
+uri
->scheme_start
, uri
->scheme_len
*sizeof(WCHAR
));
6570 /* Add the "://". */
6571 ptr
+= uri
->scheme_len
;
6572 memcpy(ptr
, colon_slashesW
, sizeof(colon_slashesW
));
6574 /* Add the authority. */
6575 ptr
+= ARRAY_SIZE(colon_slashesW
);
6576 memcpy(ptr
, uri
->canon_uri
+uri
->authority_start
, uri
->authority_len
*sizeof(WCHAR
));
6578 /* Add the '/' after the authority. */
6579 ptr
+= uri
->authority_len
;
6587 static HRESULT
parse_document(const Uri
*uri
, LPWSTR output
, DWORD output_len
,
6592 /* It has to be a known scheme type, but, it can't be a file
6593 * scheme. It also has to hierarchical.
6595 if(uri
->scheme_type
== URL_SCHEME_UNKNOWN
||
6596 uri
->scheme_type
== URL_SCHEME_FILE
||
6597 uri
->authority_start
== -1) {
6600 return STRSAFE_E_INSUFFICIENT_BUFFER
;
6606 if(uri
->fragment_start
> -1)
6607 len
= uri
->fragment_start
;
6609 len
= uri
->canon_len
;
6612 if(len
+1 > output_len
)
6613 return STRSAFE_E_INSUFFICIENT_BUFFER
;
6615 memcpy(output
, uri
->canon_uri
, len
*sizeof(WCHAR
));
6620 static HRESULT
parse_path_from_url(const Uri
*uri
, LPWSTR output
, DWORD output_len
,
6623 const WCHAR
*path_ptr
;
6624 WCHAR buffer
[INTERNET_MAX_URL_LENGTH
+1];
6627 if(uri
->scheme_type
!= URL_SCHEME_FILE
) {
6631 return E_INVALIDARG
;
6635 if(uri
->host_start
> -1) {
6636 static const WCHAR slash_slashW
[] = {'\\','\\'};
6638 memcpy(ptr
, slash_slashW
, sizeof(slash_slashW
));
6639 ptr
+= ARRAY_SIZE(slash_slashW
);
6640 memcpy(ptr
, uri
->canon_uri
+uri
->host_start
, uri
->host_len
*sizeof(WCHAR
));
6641 ptr
+= uri
->host_len
;
6644 path_ptr
= uri
->canon_uri
+uri
->path_start
;
6645 if(uri
->path_len
> 3 && *path_ptr
== '/' && is_drive_path(path_ptr
+1))
6646 /* Skip past the '/' in front of the drive path. */
6649 for(; path_ptr
< uri
->canon_uri
+uri
->path_start
+uri
->path_len
; ++path_ptr
, ++ptr
) {
6650 BOOL do_default_action
= TRUE
;
6652 if(*path_ptr
== '%') {
6653 const WCHAR decoded
= decode_pct_val(path_ptr
);
6657 do_default_action
= FALSE
;
6659 } else if(*path_ptr
== '/') {
6661 do_default_action
= FALSE
;
6664 if(do_default_action
)
6670 *result_len
= ptr
-buffer
;
6671 if(*result_len
+1 > output_len
)
6672 return STRSAFE_E_INSUFFICIENT_BUFFER
;
6674 memcpy(output
, buffer
, (*result_len
+1)*sizeof(WCHAR
));
6678 static HRESULT
parse_url_from_path(IUri
*uri
, LPWSTR output
, DWORD output_len
,
6685 hr
= IUri_GetPropertyLength(uri
, Uri_PROPERTY_ABSOLUTE_URI
, &len
, 0);
6692 if(len
+1 > output_len
)
6693 return STRSAFE_E_INSUFFICIENT_BUFFER
;
6695 hr
= IUri_GetAbsoluteUri(uri
, &received
);
6701 memcpy(output
, received
, (len
+1)*sizeof(WCHAR
));
6702 SysFreeString(received
);
6707 static HRESULT
parse_schema(IUri
*uri
, LPWSTR output
, DWORD output_len
,
6714 hr
= IUri_GetPropertyLength(uri
, Uri_PROPERTY_SCHEME_NAME
, &len
, 0);
6721 if(len
+1 > output_len
)
6722 return STRSAFE_E_INSUFFICIENT_BUFFER
;
6724 hr
= IUri_GetSchemeName(uri
, &received
);
6730 memcpy(output
, received
, (len
+1)*sizeof(WCHAR
));
6731 SysFreeString(received
);
6736 static HRESULT
parse_site(IUri
*uri
, LPWSTR output
, DWORD output_len
, DWORD
*result_len
)
6742 hr
= IUri_GetPropertyLength(uri
, Uri_PROPERTY_HOST
, &len
, 0);
6749 if(len
+1 > output_len
)
6750 return STRSAFE_E_INSUFFICIENT_BUFFER
;
6752 hr
= IUri_GetHost(uri
, &received
);
6758 memcpy(output
, received
, (len
+1)*sizeof(WCHAR
));
6759 SysFreeString(received
);
6764 static HRESULT
parse_domain(IUri
*uri
, LPWSTR output
, DWORD output_len
, DWORD
*result_len
)
6770 hr
= IUri_GetPropertyLength(uri
, Uri_PROPERTY_DOMAIN
, &len
, 0);
6777 if(len
+1 > output_len
)
6778 return STRSAFE_E_INSUFFICIENT_BUFFER
;
6780 hr
= IUri_GetDomain(uri
, &received
);
6786 memcpy(output
, received
, (len
+1)*sizeof(WCHAR
));
6787 SysFreeString(received
);
6792 static HRESULT
parse_anchor(IUri
*uri
, LPWSTR output
, DWORD output_len
, DWORD
*result_len
)
6798 hr
= IUri_GetPropertyLength(uri
, Uri_PROPERTY_FRAGMENT
, &len
, 0);
6805 if(len
+1 > output_len
)
6806 return STRSAFE_E_INSUFFICIENT_BUFFER
;
6808 hr
= IUri_GetFragment(uri
, &received
);
6814 memcpy(output
, received
, (len
+1)*sizeof(WCHAR
));
6815 SysFreeString(received
);
6820 /***********************************************************************
6821 * CoInternetParseIUri (urlmon.@)
6823 HRESULT WINAPI
CoInternetParseIUri(IUri
*pIUri
, PARSEACTION ParseAction
, DWORD dwFlags
,
6824 LPWSTR pwzResult
, DWORD cchResult
, DWORD
*pcchResult
,
6825 DWORD_PTR dwReserved
)
6829 IInternetProtocolInfo
*info
;
6831 TRACE("(%p %d %lx %p %ld %p %Ix)\n", pIUri
, ParseAction
, dwFlags
, pwzResult
,
6832 cchResult
, pcchResult
, dwReserved
);
6837 if(!pwzResult
|| !pIUri
) {
6839 return E_INVALIDARG
;
6842 if(!(uri
= get_uri_obj(pIUri
))) {
6844 FIXME("(%p %d %lx %p %ld %p %Ix) Unknown IUri's not supported for this action.\n",
6845 pIUri
, ParseAction
, dwFlags
, pwzResult
, cchResult
, pcchResult
, dwReserved
);
6849 info
= get_protocol_info(uri
->canon_uri
);
6851 hr
= IInternetProtocolInfo_ParseUrl(info
, uri
->canon_uri
, ParseAction
, dwFlags
,
6852 pwzResult
, cchResult
, pcchResult
, 0);
6853 IInternetProtocolInfo_Release(info
);
6854 if(SUCCEEDED(hr
)) return hr
;
6857 switch(ParseAction
) {
6858 case PARSE_CANONICALIZE
:
6859 hr
= parse_canonicalize(uri
, dwFlags
, pwzResult
, cchResult
, pcchResult
);
6861 case PARSE_FRIENDLY
:
6862 hr
= parse_friendly(pIUri
, pwzResult
, cchResult
, pcchResult
);
6864 case PARSE_ROOTDOCUMENT
:
6865 hr
= parse_rootdocument(uri
, pwzResult
, cchResult
, pcchResult
);
6867 case PARSE_DOCUMENT
:
6868 hr
= parse_document(uri
, pwzResult
, cchResult
, pcchResult
);
6870 case PARSE_PATH_FROM_URL
:
6871 hr
= parse_path_from_url(uri
, pwzResult
, cchResult
, pcchResult
);
6873 case PARSE_URL_FROM_PATH
:
6874 hr
= parse_url_from_path(pIUri
, pwzResult
, cchResult
, pcchResult
);
6877 hr
= parse_schema(pIUri
, pwzResult
, cchResult
, pcchResult
);
6880 hr
= parse_site(pIUri
, pwzResult
, cchResult
, pcchResult
);
6883 hr
= parse_domain(pIUri
, pwzResult
, cchResult
, pcchResult
);
6885 case PARSE_LOCATION
:
6887 hr
= parse_anchor(pIUri
, pwzResult
, cchResult
, pcchResult
);
6889 case PARSE_SECURITY_URL
:
6892 case PARSE_SECURITY_DOMAIN
:
6899 FIXME("(%p %d %lx %p %ld %p %Ix) Partial stub.\n", pIUri
, ParseAction
, dwFlags
,
6900 pwzResult
, cchResult
, pcchResult
, dwReserved
);