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 %d): Path after dot segments removed %s len=%d\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
) {
702 static const WCHAR formatW
[] =
703 {'%','u','.','%','u','.','%','u','.','%','u',0};
707 digits
[0] = (address
>> 24) & 0xff;
708 digits
[1] = (address
>> 16) & 0xff;
709 digits
[2] = (address
>> 8) & 0xff;
710 digits
[3] = address
& 0xff;
714 ret
= swprintf(tmp
, ARRAY_SIZE(tmp
), formatW
, digits
[0], digits
[1], digits
[2], digits
[3]);
716 ret
= swprintf(dest
, 16, formatW
, digits
[0], digits
[1], digits
[2], digits
[3]);
721 static DWORD
ui2str(WCHAR
*dest
, UINT value
) {
722 static const WCHAR formatW
[] = {'%','u',0};
727 ret
= swprintf(tmp
, ARRAY_SIZE(tmp
), formatW
, value
);
729 ret
= swprintf(dest
, 11, formatW
, value
);
734 /* Checks if the characters pointed to by 'ptr' are
735 * a percent encoded data octet.
737 * pct-encoded = "%" HEXDIG HEXDIG
739 static BOOL
check_pct_encoded(const WCHAR
**ptr
) {
740 const WCHAR
*start
= *ptr
;
746 if(!is_hexdigit(**ptr
)) {
752 if(!is_hexdigit(**ptr
)) {
761 /* dec-octet = DIGIT ; 0-9
762 * / %x31-39 DIGIT ; 10-99
763 * / "1" 2DIGIT ; 100-199
764 * / "2" %x30-34 DIGIT ; 200-249
765 * / "25" %x30-35 ; 250-255
767 static BOOL
check_dec_octet(const WCHAR
**ptr
) {
768 const WCHAR
*c1
, *c2
, *c3
;
771 /* A dec-octet must be at least 1 digit long. */
772 if(*c1
< '0' || *c1
> '9')
778 /* Since the 1-digit requirement was met, it doesn't
779 * matter if this is a DIGIT value, it's considered a
782 if(*c2
< '0' || *c2
> '9')
788 /* Same explanation as above. */
789 if(*c3
< '0' || *c3
> '9')
792 /* Anything > 255 isn't a valid IP dec-octet. */
793 if(*c1
>= '2' && *c2
>= '5' && *c3
>= '5') {
802 /* Checks if there is an implicit IPv4 address in the host component of the URI.
803 * The max value of an implicit IPv4 address is UINT_MAX.
806 * "234567" would be considered an implicit IPv4 address.
808 static BOOL
check_implicit_ipv4(const WCHAR
**ptr
, UINT
*val
) {
809 const WCHAR
*start
= *ptr
;
813 while(is_num(**ptr
)) {
814 ret
= ret
*10 + (**ptr
- '0');
830 /* Checks if the string contains an IPv4 address.
832 * This function has a strict mode or a non-strict mode of operation
833 * When 'strict' is set to FALSE this function will return TRUE if
834 * the string contains at least 'dec-octet "." dec-octet' since partial
835 * IPv4 addresses will be normalized out into full IPv4 addresses. When
836 * 'strict' is set this function expects there to be a full IPv4 address.
838 * IPv4address = dec-octet "." dec-octet "." dec-octet "." dec-octet
840 static BOOL
check_ipv4address(const WCHAR
**ptr
, BOOL strict
) {
841 const WCHAR
*start
= *ptr
;
843 if(!check_dec_octet(ptr
)) {
854 if(!check_dec_octet(ptr
)) {
868 if(!check_dec_octet(ptr
)) {
882 if(!check_dec_octet(ptr
)) {
887 /* Found a four digit ip address. */
890 /* Tries to parse the scheme name of the URI.
892 * scheme = ALPHA *(ALPHA | NUM | '+' | '-' | '.') as defined by RFC 3896.
893 * NOTE: Windows accepts a number as the first character of a scheme.
895 static BOOL
parse_scheme_name(const WCHAR
**ptr
, parse_data
*data
, DWORD extras
) {
896 const WCHAR
*start
= *ptr
;
899 data
->scheme_len
= 0;
902 if(**ptr
== '*' && *ptr
== start
) {
903 /* Might have found a wildcard scheme. If it is the next
904 * char has to be a ':' for it to be a valid URI
908 } else if(!is_num(**ptr
) && !is_alpha(**ptr
) && **ptr
!= '+' &&
909 **ptr
!= '-' && **ptr
!= '.')
918 /* Schemes must end with a ':' */
919 if(**ptr
!= ':' && !((extras
& ALLOW_NULL_TERM_SCHEME
) && !**ptr
)) {
924 data
->scheme
= start
;
925 data
->scheme_len
= *ptr
- start
;
931 /* Tries to deduce the corresponding URL_SCHEME for the given URI. Stores
932 * the deduced URL_SCHEME in data->scheme_type.
934 static BOOL
parse_scheme_type(parse_data
*data
) {
935 /* If there's scheme data then see if it's a recognized scheme. */
936 if(data
->scheme
&& data
->scheme_len
) {
939 for(i
= 0; i
< ARRAY_SIZE(recognized_schemes
); ++i
) {
940 if(lstrlenW(recognized_schemes
[i
].scheme_name
) == data
->scheme_len
) {
941 /* Has to be a case insensitive compare. */
942 if(!StrCmpNIW(recognized_schemes
[i
].scheme_name
, data
->scheme
, data
->scheme_len
)) {
943 data
->scheme_type
= recognized_schemes
[i
].scheme
;
949 /* If we get here it means it's not a recognized scheme. */
950 data
->scheme_type
= URL_SCHEME_UNKNOWN
;
952 } else if(data
->is_relative
) {
953 /* Relative URI's have no scheme. */
954 data
->scheme_type
= URL_SCHEME_UNKNOWN
;
957 /* Should never reach here! what happened... */
958 FIXME("(%p): Unable to determine scheme type for URI %s\n", data
, debugstr_w(data
->uri
));
963 /* Tries to parse (or deduce) the scheme_name of a URI. If it can't
964 * parse a scheme from the URI it will try to deduce the scheme_name and scheme_type
965 * using the flags specified in 'flags' (if any). Flags that affect how this function
966 * operates are the Uri_CREATE_ALLOW_* flags.
968 * All parsed/deduced information will be stored in 'data' when the function returns.
970 * Returns TRUE if it was able to successfully parse the information.
972 static BOOL
parse_scheme(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
, DWORD extras
) {
973 static const WCHAR fileW
[] = {'f','i','l','e',0};
974 static const WCHAR wildcardW
[] = {'*',0};
976 /* First check to see if the uri could implicitly be a file path. */
977 if(is_implicit_file_path(*ptr
)) {
978 if(flags
& Uri_CREATE_ALLOW_IMPLICIT_FILE_SCHEME
) {
979 data
->scheme
= fileW
;
980 data
->scheme_len
= lstrlenW(fileW
);
981 data
->has_implicit_scheme
= TRUE
;
983 TRACE("(%p %p %x): URI is an implicit file path.\n", ptr
, data
, flags
);
985 /* Windows does not consider anything that can implicitly be a file
986 * path to be a valid URI if the ALLOW_IMPLICIT_FILE_SCHEME flag is not set...
988 TRACE("(%p %p %x): URI is implicitly a file path, but, the ALLOW_IMPLICIT_FILE_SCHEME flag wasn't set.\n",
992 } else if(!parse_scheme_name(ptr
, data
, extras
)) {
993 /* No scheme was found, this means it could be:
994 * a) an implicit Wildcard scheme
998 if(flags
& Uri_CREATE_ALLOW_IMPLICIT_WILDCARD_SCHEME
) {
999 data
->scheme
= wildcardW
;
1000 data
->scheme_len
= lstrlenW(wildcardW
);
1001 data
->has_implicit_scheme
= TRUE
;
1003 TRACE("(%p %p %x): URI is an implicit wildcard scheme.\n", ptr
, data
, flags
);
1004 } else if (flags
& Uri_CREATE_ALLOW_RELATIVE
) {
1005 data
->is_relative
= TRUE
;
1006 TRACE("(%p %p %x): URI is relative.\n", ptr
, data
, flags
);
1008 TRACE("(%p %p %x): Malformed URI found. Unable to deduce scheme name.\n", ptr
, data
, flags
);
1013 if(!data
->is_relative
)
1014 TRACE("(%p %p %x): Found scheme=%s scheme_len=%d\n", ptr
, data
, flags
,
1015 debugstr_wn(data
->scheme
, data
->scheme_len
), data
->scheme_len
);
1017 if(!parse_scheme_type(data
))
1020 TRACE("(%p %p %x): Assigned %d as the URL_SCHEME.\n", ptr
, data
, flags
, data
->scheme_type
);
1024 static BOOL
parse_username(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
, DWORD extras
) {
1025 data
->username
= *ptr
;
1027 while(**ptr
!= ':' && **ptr
!= '@') {
1029 if(!check_pct_encoded(ptr
)) {
1030 if(data
->scheme_type
!= URL_SCHEME_UNKNOWN
) {
1031 *ptr
= data
->username
;
1032 data
->username
= NULL
;
1037 } else if(extras
& ALLOW_NULL_TERM_USER_NAME
&& !**ptr
)
1039 else if(is_auth_delim(**ptr
, data
->scheme_type
!= URL_SCHEME_UNKNOWN
)) {
1040 *ptr
= data
->username
;
1041 data
->username
= NULL
;
1048 data
->username_len
= *ptr
- data
->username
;
1052 static BOOL
parse_password(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
, DWORD extras
) {
1053 data
->password
= *ptr
;
1055 while(**ptr
!= '@') {
1057 if(!check_pct_encoded(ptr
)) {
1058 if(data
->scheme_type
!= URL_SCHEME_UNKNOWN
) {
1059 *ptr
= data
->password
;
1060 data
->password
= NULL
;
1065 } else if(extras
& ALLOW_NULL_TERM_PASSWORD
&& !**ptr
)
1067 else if(is_auth_delim(**ptr
, data
->scheme_type
!= URL_SCHEME_UNKNOWN
)) {
1068 *ptr
= data
->password
;
1069 data
->password
= NULL
;
1076 data
->password_len
= *ptr
- data
->password
;
1080 /* Parses the userinfo part of the URI (if it exists). The userinfo field of
1081 * a URI can consist of "username:password@", or just "username@".
1084 * userinfo = *( unreserved / pct-encoded / sub-delims / ":" )
1087 * 1) If there is more than one ':' in the userinfo part of the URI Windows
1088 * uses the first occurrence of ':' to delimit the username and password
1092 * ftp://user:pass:word@winehq.org
1094 * would yield "user" as the username and "pass:word" as the password.
1096 * 2) Windows allows any character to appear in the "userinfo" part of
1097 * a URI, as long as it's not an authority delimiter character set.
1099 static void parse_userinfo(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
) {
1100 const WCHAR
*start
= *ptr
;
1102 if(!parse_username(ptr
, data
, flags
, 0)) {
1103 TRACE("(%p %p %x): URI contained no userinfo.\n", ptr
, data
, flags
);
1109 if(!parse_password(ptr
, data
, flags
, 0)) {
1111 data
->username
= NULL
;
1112 data
->username_len
= 0;
1113 TRACE("(%p %p %x): URI contained no userinfo.\n", ptr
, data
, flags
);
1120 data
->username
= NULL
;
1121 data
->username_len
= 0;
1122 data
->password
= NULL
;
1123 data
->password_len
= 0;
1125 TRACE("(%p %p %x): URI contained no userinfo.\n", ptr
, data
, flags
);
1130 TRACE("(%p %p %x): Found username %s len=%d.\n", ptr
, data
, flags
,
1131 debugstr_wn(data
->username
, data
->username_len
), data
->username_len
);
1134 TRACE("(%p %p %x): Found password %s len=%d.\n", ptr
, data
, flags
,
1135 debugstr_wn(data
->password
, data
->password_len
), data
->password_len
);
1140 /* Attempts to parse a port from the URI.
1143 * Windows seems to have a cap on what the maximum value
1144 * for a port can be. The max value is USHORT_MAX.
1148 static BOOL
parse_port(const WCHAR
**ptr
, parse_data
*data
) {
1152 while(!is_auth_delim(**ptr
, data
->scheme_type
!= URL_SCHEME_UNKNOWN
)) {
1153 if(!is_num(**ptr
)) {
1159 port
= port
*10 + (**ptr
-'0');
1161 if(port
> USHRT_MAX
) {
1170 data
->has_port
= TRUE
;
1171 data
->port_value
= port
;
1172 data
->port_len
= *ptr
- data
->port
;
1174 TRACE("(%p %p): Found port %s len=%d value=%u\n", ptr
, data
,
1175 debugstr_wn(data
->port
, data
->port_len
), data
->port_len
, data
->port_value
);
1179 /* Attempts to parse a IPv4 address from the URI.
1182 * Windows normalizes IPv4 addresses, This means there are three
1183 * possibilities for the URI to contain an IPv4 address.
1184 * 1) A well formed address (ex. 192.2.2.2).
1185 * 2) A partially formed address. For example "192.0" would
1186 * normalize to "192.0.0.0" during canonicalization.
1187 * 3) An implicit IPv4 address. For example "256" would
1188 * normalize to "0.0.1.0" during canonicalization. Also
1189 * note that the maximum value for an implicit IP address
1190 * is UINT_MAX, if the value in the URI exceeds this then
1191 * it is not considered an IPv4 address.
1193 static BOOL
parse_ipv4address(const WCHAR
**ptr
, parse_data
*data
) {
1194 const BOOL is_unknown
= data
->scheme_type
== URL_SCHEME_UNKNOWN
;
1197 if(!check_ipv4address(ptr
, FALSE
)) {
1198 if(!check_implicit_ipv4(ptr
, &data
->implicit_ipv4
)) {
1199 TRACE("(%p %p): URI didn't contain anything looking like an IPv4 address.\n", ptr
, data
);
1204 data
->has_implicit_ip
= TRUE
;
1207 data
->host_len
= *ptr
- data
->host
;
1208 data
->host_type
= Uri_HOST_IPV4
;
1210 /* Check if what we found is the only part of the host name (if it isn't
1211 * we don't have an IPv4 address).
1215 if(!parse_port(ptr
, data
)) {
1220 } else if(!is_auth_delim(**ptr
, !is_unknown
)) {
1221 /* Found more data which belongs to the host, so this isn't an IPv4. */
1224 data
->has_implicit_ip
= FALSE
;
1228 TRACE("(%p %p): IPv4 address found. host=%s host_len=%d host_type=%d\n",
1229 ptr
, data
, debugstr_wn(data
->host
, data
->host_len
),
1230 data
->host_len
, data
->host_type
);
1234 /* Attempts to parse the reg-name from the URI.
1236 * Because of the way Windows handles ':' this function also
1237 * handles parsing the port.
1239 * reg-name = *( unreserved / pct-encoded / sub-delims )
1242 * Windows allows everything, but, the characters in "auth_delims" and ':'
1243 * to appear in a reg-name, unless it's an unknown scheme type then ':' is
1244 * allowed to appear (even if a valid port isn't after it).
1246 * Windows doesn't like host names which start with '[' and end with ']'
1247 * and don't contain a valid IP literal address in between them.
1249 * On Windows if a '[' is encountered in the host name the ':' no longer
1250 * counts as a delimiter until you reach the next ']' or an "authority delimiter".
1252 * A reg-name CAN be empty.
1254 static BOOL
parse_reg_name(const WCHAR
**ptr
, parse_data
*data
, DWORD extras
) {
1255 const BOOL has_start_bracket
= **ptr
== '[';
1256 const BOOL known_scheme
= data
->scheme_type
!= URL_SCHEME_UNKNOWN
;
1257 const BOOL is_res
= data
->scheme_type
== URL_SCHEME_RES
;
1258 BOOL inside_brackets
= has_start_bracket
;
1260 /* res URIs don't have ports. */
1261 BOOL ignore_col
= (extras
& IGNORE_PORT_DELIMITER
) || is_res
;
1263 /* We have to be careful with file schemes. */
1264 if(data
->scheme_type
== URL_SCHEME_FILE
) {
1265 /* This is because an implicit file scheme could be "C:\\test" and it
1266 * would trick this function into thinking the host is "C", when after
1267 * canonicalization the host would end up being an empty string. A drive
1268 * path can also have a '|' instead of a ':' after the drive letter.
1270 if(is_drive_path(*ptr
)) {
1271 /* Regular old drive paths have no host type (or host name). */
1272 data
->host_type
= Uri_HOST_UNKNOWN
;
1276 } else if(is_unc_path(*ptr
))
1277 /* Skip past the "\\" of a UNC path. */
1283 /* For res URIs, everything before the first '/' is
1284 * considered the host.
1286 while((!is_res
&& !is_auth_delim(**ptr
, known_scheme
)) ||
1287 (is_res
&& **ptr
&& **ptr
!= '/')) {
1288 if(**ptr
== ':' && !ignore_col
) {
1289 /* We can ignore ':' if we are inside brackets.*/
1290 if(!inside_brackets
) {
1291 const WCHAR
*tmp
= (*ptr
)++;
1293 /* Attempt to parse the port. */
1294 if(!parse_port(ptr
, data
)) {
1295 /* Windows expects there to be a valid port for known scheme types. */
1296 if(data
->scheme_type
!= URL_SCHEME_UNKNOWN
) {
1299 TRACE("(%p %p %x): Expected valid port\n", ptr
, data
, extras
);
1302 /* Windows gives up on trying to parse a port when it
1303 * encounters an invalid port.
1307 data
->host_len
= tmp
- data
->host
;
1311 } else if(**ptr
== '%' && (known_scheme
&& !is_res
)) {
1312 /* Has to be a legit % encoded value. */
1313 if(!check_pct_encoded(ptr
)) {
1319 } else if(is_res
&& is_forbidden_dos_path_char(**ptr
)) {
1323 } else if(**ptr
== ']')
1324 inside_brackets
= FALSE
;
1325 else if(**ptr
== '[')
1326 inside_brackets
= TRUE
;
1331 if(has_start_bracket
) {
1332 /* Make sure the last character of the host wasn't a ']'. */
1333 if(*(*ptr
-1) == ']') {
1334 TRACE("(%p %p %x): Expected an IP literal inside of the host\n", ptr
, data
, extras
);
1341 /* Don't overwrite our length if we found a port earlier. */
1343 data
->host_len
= *ptr
- data
->host
;
1345 /* If the host is empty, then it's an unknown host type. */
1346 if(data
->host_len
== 0 || is_res
)
1347 data
->host_type
= Uri_HOST_UNKNOWN
;
1349 data
->host_type
= Uri_HOST_DNS
;
1351 TRACE("(%p %p %x): Parsed reg-name. host=%s len=%d\n", ptr
, data
, extras
,
1352 debugstr_wn(data
->host
, data
->host_len
), data
->host_len
);
1356 /* Attempts to parse an IPv6 address out of the URI.
1358 * IPv6address = 6( h16 ":" ) ls32
1359 * / "::" 5( h16 ":" ) ls32
1360 * / [ h16 ] "::" 4( h16 ":" ) ls32
1361 * / [ *1( h16 ":" ) h16 ] "::" 3( h16 ":" ) ls32
1362 * / [ *2( h16 ":" ) h16 ] "::" 2( h16 ":" ) ls32
1363 * / [ *3( h16 ":" ) h16 ] "::" h16 ":" ls32
1364 * / [ *4( h16 ":" ) h16 ] "::" ls32
1365 * / [ *5( h16 ":" ) h16 ] "::" h16
1366 * / [ *6( h16 ":" ) h16 ] "::"
1368 * ls32 = ( h16 ":" h16 ) / IPv4address
1369 * ; least-significant 32 bits of address.
1372 * ; 16 bits of address represented in hexadecimal.
1374 static BOOL
parse_ipv6address(const WCHAR
**ptr
, parse_data
*data
) {
1375 const WCHAR
*terminator
;
1377 if(RtlIpv6StringToAddressW(*ptr
, &terminator
, &data
->ipv6_address
))
1379 if(*terminator
!= ']' && !is_auth_delim(*terminator
, data
->scheme_type
!= URL_SCHEME_UNKNOWN
))
1383 data
->host_type
= Uri_HOST_IPV6
;
1387 /* IPvFuture = "v" 1*HEXDIG "." 1*( unreserved / sub-delims / ":" ) */
1388 static BOOL
parse_ipvfuture(const WCHAR
**ptr
, parse_data
*data
) {
1389 const WCHAR
*start
= *ptr
;
1391 /* IPvFuture has to start with a 'v' or 'V'. */
1392 if(**ptr
!= 'v' && **ptr
!= 'V')
1395 /* Following the v there must be at least 1 hex digit. */
1397 if(!is_hexdigit(**ptr
)) {
1403 while(is_hexdigit(**ptr
))
1406 /* End of the hexdigit sequence must be a '.' */
1413 if(!is_unreserved(**ptr
) && !is_subdelim(**ptr
) && **ptr
!= ':') {
1419 while(is_unreserved(**ptr
) || is_subdelim(**ptr
) || **ptr
== ':')
1422 data
->host_type
= Uri_HOST_UNKNOWN
;
1424 TRACE("(%p %p): Parsed IPvFuture address %s len=%d\n", ptr
, data
,
1425 debugstr_wn(start
, *ptr
-start
), (int)(*ptr
-start
));
1430 /* IP-literal = "[" ( IPv6address / IPvFuture ) "]" */
1431 static BOOL
parse_ip_literal(const WCHAR
**ptr
, parse_data
*data
, DWORD extras
) {
1434 if(**ptr
!= '[' && !(extras
& ALLOW_BRACKETLESS_IP_LITERAL
)) {
1437 } else if(**ptr
== '[')
1440 if(!parse_ipv6address(ptr
, data
)) {
1441 if(extras
& SKIP_IP_FUTURE_CHECK
|| !parse_ipvfuture(ptr
, data
)) {
1448 if(**ptr
!= ']' && !(extras
& ALLOW_BRACKETLESS_IP_LITERAL
)) {
1452 } else if(!**ptr
&& extras
& ALLOW_BRACKETLESS_IP_LITERAL
) {
1453 /* The IP literal didn't contain brackets and was followed by
1454 * a NULL terminator, so no reason to even check the port.
1456 data
->host_len
= *ptr
- data
->host
;
1463 /* If a valid port is not found, then let it trickle down to
1466 if(!parse_port(ptr
, data
)) {
1472 data
->host_len
= *ptr
- data
->host
;
1477 /* Parses the host information from the URI.
1479 * host = IP-literal / IPv4address / reg-name
1481 static BOOL
parse_host(const WCHAR
**ptr
, parse_data
*data
, DWORD extras
) {
1482 if(!parse_ip_literal(ptr
, data
, extras
)) {
1483 if(!parse_ipv4address(ptr
, data
)) {
1484 if(!parse_reg_name(ptr
, data
, extras
)) {
1485 TRACE("(%p %p %x): Malformed URI, Unknown host type.\n", ptr
, data
, extras
);
1494 /* Parses the authority information from the URI.
1496 * authority = [ userinfo "@" ] host [ ":" port ]
1498 static BOOL
parse_authority(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
) {
1499 parse_userinfo(ptr
, data
, flags
);
1501 /* Parsing the port will happen during one of the host parsing
1502 * routines (if the URI has a port).
1504 if(!parse_host(ptr
, data
, 0))
1510 /* Attempts to parse the path information of a hierarchical URI. */
1511 static BOOL
parse_path_hierarchical(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
) {
1512 const WCHAR
*start
= *ptr
;
1513 static const WCHAR slash
[] = {'/',0};
1514 const BOOL is_file
= data
->scheme_type
== URL_SCHEME_FILE
;
1516 if(is_path_delim(data
->scheme_type
, **ptr
)) {
1517 if(data
->scheme_type
== URL_SCHEME_WILDCARD
&& !data
->must_have_path
) {
1520 } else if(!(flags
& Uri_CREATE_NO_CANONICALIZE
)) {
1521 /* If the path component is empty, then a '/' is added. */
1526 while(!is_path_delim(data
->scheme_type
, **ptr
)) {
1527 if(**ptr
== '%' && data
->scheme_type
!= URL_SCHEME_UNKNOWN
&& !is_file
) {
1528 if(!check_pct_encoded(ptr
)) {
1533 } else if(is_forbidden_dos_path_char(**ptr
) && is_file
&&
1534 (flags
& Uri_CREATE_FILE_USE_DOS_PATH
)) {
1535 /* File schemes with USE_DOS_PATH set aren't allowed to have
1536 * a '<' or '>' or '\"' appear in them.
1540 } else if(**ptr
== '\\') {
1541 /* Not allowed to have a backslash if NO_CANONICALIZE is set
1542 * and the scheme is known type (but not a file scheme).
1544 if(flags
& Uri_CREATE_NO_CANONICALIZE
) {
1545 if(data
->scheme_type
!= URL_SCHEME_FILE
&&
1546 data
->scheme_type
!= URL_SCHEME_UNKNOWN
) {
1556 /* The only time a URI doesn't have a path is when
1557 * the NO_CANONICALIZE flag is set and the raw URI
1558 * didn't contain one.
1565 data
->path_len
= *ptr
- start
;
1570 TRACE("(%p %p %x): Parsed path %s len=%d\n", ptr
, data
, flags
,
1571 debugstr_wn(data
->path
, data
->path_len
), data
->path_len
);
1573 TRACE("(%p %p %x): The URI contained no path\n", ptr
, data
, flags
);
1578 /* Parses the path of an opaque URI (much less strict than the parser
1579 * for a hierarchical URI).
1582 * Windows allows invalid % encoded data to appear in opaque URI paths
1583 * for unknown scheme types.
1585 * File schemes with USE_DOS_PATH set aren't allowed to have '<', '>', or '\"'
1588 static BOOL
parse_path_opaque(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
) {
1589 const BOOL known_scheme
= data
->scheme_type
!= URL_SCHEME_UNKNOWN
;
1590 const BOOL is_file
= data
->scheme_type
== URL_SCHEME_FILE
;
1591 const BOOL is_mailto
= data
->scheme_type
== URL_SCHEME_MAILTO
;
1593 if (is_mailto
&& (*ptr
)[0] == '/' && (*ptr
)[1] == '/')
1595 if ((*ptr
)[2]) data
->path
= *ptr
+ 2;
1596 else data
->path
= NULL
;
1601 while(!is_path_delim(data
->scheme_type
, **ptr
)) {
1602 if(**ptr
== '%' && known_scheme
) {
1603 if(!check_pct_encoded(ptr
)) {
1609 } else if(is_forbidden_dos_path_char(**ptr
) && is_file
&&
1610 (flags
& Uri_CREATE_FILE_USE_DOS_PATH
)) {
1619 if (data
->path
) data
->path_len
= *ptr
- data
->path
;
1620 TRACE("(%p %p %x): Parsed opaque URI path %s len=%d\n", ptr
, data
, flags
,
1621 debugstr_wn(data
->path
, data
->path_len
), data
->path_len
);
1625 /* Determines how the URI should be parsed after the scheme information.
1627 * If the scheme is followed by "//", then it is treated as a hierarchical URI
1628 * which then the authority and path information will be parsed out. Otherwise, the
1629 * URI will be treated as an opaque URI which the authority information is not parsed
1632 * RFC 3896 definition of hier-part:
1634 * hier-part = "//" authority path-abempty
1639 * MSDN opaque URI definition:
1640 * scheme ":" path [ "#" fragment ]
1643 * If the URI is of an unknown scheme type and has a "//" following the scheme then it
1644 * is treated as a hierarchical URI, but, if the CREATE_NO_CRACK_UNKNOWN_SCHEMES flag is
1645 * set then it is considered an opaque URI regardless of what follows the scheme information
1646 * (per MSDN documentation).
1648 static BOOL
parse_hierpart(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
) {
1649 const WCHAR
*start
= *ptr
;
1651 data
->must_have_path
= FALSE
;
1653 /* For javascript: URIs, simply set everything as a path */
1654 if(data
->scheme_type
== URL_SCHEME_JAVASCRIPT
) {
1656 data
->path_len
= lstrlenW(*ptr
);
1657 data
->is_opaque
= TRUE
;
1658 *ptr
+= data
->path_len
;
1662 /* Checks if the authority information needs to be parsed. */
1663 if(is_hierarchical_uri(ptr
, data
)) {
1664 /* Only treat it as a hierarchical URI if the scheme_type is known or
1665 * the Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES flag is not set.
1667 if(data
->scheme_type
!= URL_SCHEME_UNKNOWN
||
1668 !(flags
& Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES
)) {
1669 TRACE("(%p %p %x): Treating URI as an hierarchical URI.\n", ptr
, data
, flags
);
1670 data
->is_opaque
= FALSE
;
1672 if(data
->scheme_type
== URL_SCHEME_WILDCARD
&& !data
->has_implicit_scheme
) {
1673 if(**ptr
== '/' && *(*ptr
+1) == '/') {
1674 data
->must_have_path
= TRUE
;
1679 /* TODO: Handle hierarchical URI's, parse authority then parse the path. */
1680 if(!parse_authority(ptr
, data
, flags
))
1683 return parse_path_hierarchical(ptr
, data
, flags
);
1685 /* Reset ptr to its starting position so opaque path parsing
1686 * begins at the correct location.
1691 /* If it reaches here, then the URI will be treated as an opaque
1695 TRACE("(%p %p %x): Treating URI as an opaque URI.\n", ptr
, data
, flags
);
1697 data
->is_opaque
= TRUE
;
1698 if(!parse_path_opaque(ptr
, data
, flags
))
1704 /* Attempts to parse the query string from the URI.
1707 * If NO_DECODE_EXTRA_INFO flag is set, then invalid percent encoded
1708 * data is allowed to appear in the query string. For unknown scheme types
1709 * invalid percent encoded data is allowed to appear regardless.
1711 static BOOL
parse_query(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
) {
1712 const BOOL known_scheme
= data
->scheme_type
!= URL_SCHEME_UNKNOWN
;
1715 TRACE("(%p %p %x): URI didn't contain a query string.\n", ptr
, data
, flags
);
1722 while(**ptr
&& **ptr
!= '#') {
1723 if(**ptr
== '%' && known_scheme
&&
1724 !(flags
& Uri_CREATE_NO_DECODE_EXTRA_INFO
)) {
1725 if(!check_pct_encoded(ptr
)) {
1736 data
->query_len
= *ptr
- data
->query
;
1738 TRACE("(%p %p %x): Parsed query string %s len=%d\n", ptr
, data
, flags
,
1739 debugstr_wn(data
->query
, data
->query_len
), data
->query_len
);
1743 /* Attempts to parse the fragment from the URI.
1746 * If NO_DECODE_EXTRA_INFO flag is set, then invalid percent encoded
1747 * data is allowed to appear in the query string. For unknown scheme types
1748 * invalid percent encoded data is allowed to appear regardless.
1750 static BOOL
parse_fragment(const WCHAR
**ptr
, parse_data
*data
, DWORD flags
) {
1751 const BOOL known_scheme
= data
->scheme_type
!= URL_SCHEME_UNKNOWN
;
1754 TRACE("(%p %p %x): URI didn't contain a fragment.\n", ptr
, data
, flags
);
1758 data
->fragment
= *ptr
;
1762 if(**ptr
== '%' && known_scheme
&&
1763 !(flags
& Uri_CREATE_NO_DECODE_EXTRA_INFO
)) {
1764 if(!check_pct_encoded(ptr
)) {
1765 *ptr
= data
->fragment
;
1766 data
->fragment
= NULL
;
1775 data
->fragment_len
= *ptr
- data
->fragment
;
1777 TRACE("(%p %p %x): Parsed fragment %s len=%d\n", ptr
, data
, flags
,
1778 debugstr_wn(data
->fragment
, data
->fragment_len
), data
->fragment_len
);
1782 /* Parses and validates the components of the specified by data->uri
1783 * and stores the information it parses into 'data'.
1785 * Returns TRUE if it successfully parsed the URI. False otherwise.
1787 static BOOL
parse_uri(parse_data
*data
, DWORD flags
) {
1794 TRACE("(%p %x): BEGINNING TO PARSE URI %s.\n", data
, flags
, debugstr_w(data
->uri
));
1796 if(!parse_scheme(pptr
, data
, flags
, 0))
1799 if(!parse_hierpart(pptr
, data
, flags
))
1802 if(!parse_query(pptr
, data
, flags
))
1805 if(!parse_fragment(pptr
, data
, flags
))
1808 TRACE("(%p %x): FINISHED PARSING URI.\n", data
, flags
);
1812 static BOOL
canonicalize_username(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
1815 if(!data
->username
) {
1816 uri
->userinfo_start
= -1;
1820 uri
->userinfo_start
= uri
->canon_len
;
1821 for(ptr
= data
->username
; ptr
< data
->username
+data
->username_len
; ++ptr
) {
1823 /* Only decode % encoded values for known scheme types. */
1824 if(data
->scheme_type
!= URL_SCHEME_UNKNOWN
) {
1825 /* See if the value really needs decoding. */
1826 WCHAR val
= decode_pct_val(ptr
);
1827 if(is_unreserved(val
)) {
1829 uri
->canon_uri
[uri
->canon_len
] = val
;
1833 /* Move pass the hex characters. */
1838 } else if(is_ascii(*ptr
) && !is_reserved(*ptr
) && !is_unreserved(*ptr
) && *ptr
!= '\\') {
1839 /* Only percent encode forbidden characters if the NO_ENCODE_FORBIDDEN_CHARACTERS flag
1842 if(!(flags
& Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS
)) {
1844 pct_encode_val(*ptr
, uri
->canon_uri
+ uri
->canon_len
);
1846 uri
->canon_len
+= 3;
1852 /* Nothing special, so just copy the character over. */
1853 uri
->canon_uri
[uri
->canon_len
] = *ptr
;
1860 static BOOL
canonicalize_password(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
1863 if(!data
->password
) {
1864 uri
->userinfo_split
= -1;
1868 if(uri
->userinfo_start
== -1)
1869 /* Has a password, but, doesn't have a username. */
1870 uri
->userinfo_start
= uri
->canon_len
;
1872 uri
->userinfo_split
= uri
->canon_len
- uri
->userinfo_start
;
1874 /* Add the ':' to the userinfo component. */
1876 uri
->canon_uri
[uri
->canon_len
] = ':';
1879 for(ptr
= data
->password
; ptr
< data
->password
+data
->password_len
; ++ptr
) {
1881 /* Only decode % encoded values for known scheme types. */
1882 if(data
->scheme_type
!= URL_SCHEME_UNKNOWN
) {
1883 /* See if the value really needs decoding. */
1884 WCHAR val
= decode_pct_val(ptr
);
1885 if(is_unreserved(val
)) {
1887 uri
->canon_uri
[uri
->canon_len
] = val
;
1891 /* Move pass the hex characters. */
1896 } else if(is_ascii(*ptr
) && !is_reserved(*ptr
) && !is_unreserved(*ptr
) && *ptr
!= '\\') {
1897 /* Only percent encode forbidden characters if the NO_ENCODE_FORBIDDEN_CHARACTERS flag
1900 if(!(flags
& Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS
)) {
1902 pct_encode_val(*ptr
, uri
->canon_uri
+ uri
->canon_len
);
1904 uri
->canon_len
+= 3;
1910 /* Nothing special, so just copy the character over. */
1911 uri
->canon_uri
[uri
->canon_len
] = *ptr
;
1918 /* Canonicalizes the userinfo of the URI represented by the parse_data.
1920 * Canonicalization of the userinfo is a simple process. If there are any percent
1921 * encoded characters that fall in the "unreserved" character set, they are decoded
1922 * to their actual value. If a character is not in the "unreserved" or "reserved" sets
1923 * then it is percent encoded. Other than that the characters are copied over without
1926 static BOOL
canonicalize_userinfo(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
1927 uri
->userinfo_start
= uri
->userinfo_split
= -1;
1928 uri
->userinfo_len
= 0;
1930 if(!data
->username
&& !data
->password
)
1931 /* URI doesn't have userinfo, so nothing to do here. */
1934 if(!canonicalize_username(data
, uri
, flags
, computeOnly
))
1937 if(!canonicalize_password(data
, uri
, flags
, computeOnly
))
1940 uri
->userinfo_len
= uri
->canon_len
- uri
->userinfo_start
;
1942 TRACE("(%p %p %x %d): Canonicalized userinfo, userinfo_start=%d, userinfo=%s, userinfo_split=%d userinfo_len=%d.\n",
1943 data
, uri
, flags
, computeOnly
, uri
->userinfo_start
, debugstr_wn(uri
->canon_uri
+ uri
->userinfo_start
, uri
->userinfo_len
),
1944 uri
->userinfo_split
, uri
->userinfo_len
);
1946 /* Now insert the '@' after the userinfo. */
1948 uri
->canon_uri
[uri
->canon_len
] = '@';
1954 /* Attempts to canonicalize a reg_name.
1956 * Things that happen:
1957 * 1) If Uri_CREATE_NO_CANONICALIZE flag is not set, then the reg_name is
1958 * lower cased. Unless it's an unknown scheme type, which case it's
1959 * no lower cased regardless.
1961 * 2) Unreserved % encoded characters are decoded for known
1964 * 3) Forbidden characters are % encoded as long as
1965 * Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS flag is not set and
1966 * it isn't an unknown scheme type.
1968 * 4) If it's a file scheme and the host is "localhost" it's removed.
1970 * 5) If it's a file scheme and Uri_CREATE_FILE_USE_DOS_PATH is set,
1971 * then the UNC path characters are added before the host name.
1973 static BOOL
canonicalize_reg_name(const parse_data
*data
, Uri
*uri
,
1974 DWORD flags
, BOOL computeOnly
) {
1975 static const WCHAR localhostW
[] =
1976 {'l','o','c','a','l','h','o','s','t',0};
1978 const BOOL known_scheme
= data
->scheme_type
!= URL_SCHEME_UNKNOWN
;
1980 if(data
->scheme_type
== URL_SCHEME_FILE
&&
1981 data
->host_len
== lstrlenW(localhostW
)) {
1982 if(!StrCmpNIW(data
->host
, localhostW
, data
->host_len
)) {
1983 uri
->host_start
= -1;
1985 uri
->host_type
= Uri_HOST_UNKNOWN
;
1990 if(data
->scheme_type
== URL_SCHEME_FILE
&& flags
& Uri_CREATE_FILE_USE_DOS_PATH
) {
1992 uri
->canon_uri
[uri
->canon_len
] = '\\';
1993 uri
->canon_uri
[uri
->canon_len
+1] = '\\';
1995 uri
->canon_len
+= 2;
1996 uri
->authority_start
= uri
->canon_len
;
1999 uri
->host_start
= uri
->canon_len
;
2001 for(ptr
= data
->host
; ptr
< data
->host
+data
->host_len
; ++ptr
) {
2002 if(*ptr
== '%' && known_scheme
) {
2003 WCHAR val
= decode_pct_val(ptr
);
2004 if(is_unreserved(val
)) {
2005 /* If NO_CANONICALIZE is not set, then windows lower cases the
2008 if(!(flags
& Uri_CREATE_NO_CANONICALIZE
) && iswupper(val
)) {
2010 uri
->canon_uri
[uri
->canon_len
] = towlower(val
);
2013 uri
->canon_uri
[uri
->canon_len
] = val
;
2017 /* Skip past the % encoded character. */
2021 /* Just copy the % over. */
2023 uri
->canon_uri
[uri
->canon_len
] = *ptr
;
2026 } else if(*ptr
== '\\') {
2027 /* Only unknown scheme types could have made it here with a '\\' in the host name. */
2029 uri
->canon_uri
[uri
->canon_len
] = *ptr
;
2031 } else if(!(flags
& Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS
) && is_ascii(*ptr
) &&
2032 !is_unreserved(*ptr
) && !is_reserved(*ptr
) && known_scheme
) {
2034 pct_encode_val(*ptr
, uri
->canon_uri
+uri
->canon_len
);
2036 /* The percent encoded value gets lower cased also. */
2037 if(!(flags
& Uri_CREATE_NO_CANONICALIZE
)) {
2038 uri
->canon_uri
[uri
->canon_len
+1] = towlower(uri
->canon_uri
[uri
->canon_len
+1]);
2039 uri
->canon_uri
[uri
->canon_len
+2] = towlower(uri
->canon_uri
[uri
->canon_len
+2]);
2043 uri
->canon_len
+= 3;
2046 if(!(flags
& Uri_CREATE_NO_CANONICALIZE
) && known_scheme
)
2047 uri
->canon_uri
[uri
->canon_len
] = towlower(*ptr
);
2049 uri
->canon_uri
[uri
->canon_len
] = *ptr
;
2056 uri
->host_len
= uri
->canon_len
- uri
->host_start
;
2059 TRACE("(%p %p %x %d): Canonicalize reg_name=%s len=%d\n", data
, uri
, flags
,
2060 computeOnly
, debugstr_wn(uri
->canon_uri
+uri
->host_start
, uri
->host_len
),
2064 find_domain_name(uri
->canon_uri
+uri
->host_start
, uri
->host_len
,
2065 &(uri
->domain_offset
));
2070 /* Attempts to canonicalize an implicit IPv4 address. */
2071 static BOOL
canonicalize_implicit_ipv4address(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
2072 uri
->host_start
= uri
->canon_len
;
2074 TRACE("%u\n", data
->implicit_ipv4
);
2075 /* For unknown scheme types Windows doesn't convert
2076 * the value into an IP address, but it still considers
2077 * it an IPv4 address.
2079 if(data
->scheme_type
== URL_SCHEME_UNKNOWN
) {
2081 memcpy(uri
->canon_uri
+uri
->canon_len
, data
->host
, data
->host_len
*sizeof(WCHAR
));
2082 uri
->canon_len
+= data
->host_len
;
2085 uri
->canon_len
+= ui2ipv4(uri
->canon_uri
+uri
->canon_len
, data
->implicit_ipv4
);
2087 uri
->canon_len
+= ui2ipv4(NULL
, data
->implicit_ipv4
);
2090 uri
->host_len
= uri
->canon_len
- uri
->host_start
;
2091 uri
->host_type
= Uri_HOST_IPV4
;
2094 TRACE("%p %p %x %d): Canonicalized implicit IP address=%s len=%d\n",
2095 data
, uri
, flags
, computeOnly
,
2096 debugstr_wn(uri
->canon_uri
+uri
->host_start
, uri
->host_len
),
2102 /* Attempts to canonicalize an IPv4 address.
2104 * If the parse_data represents a URI that has an implicit IPv4 address
2105 * (ex. http://256/, this function will convert 256 into 0.0.1.0). If
2106 * the implicit IP address exceeds the value of UINT_MAX (maximum value
2107 * for an IPv4 address) it's canonicalized as if it were a reg-name.
2109 * If the parse_data contains a partial or full IPv4 address it normalizes it.
2110 * A partial IPv4 address is something like "192.0" and would be normalized to
2111 * "192.0.0.0". With a full (or partial) IPv4 address like "192.002.01.003" would
2112 * be normalized to "192.2.1.3".
2115 * Windows ONLY normalizes IPv4 address for known scheme types (one that isn't
2116 * URL_SCHEME_UNKNOWN). For unknown scheme types, it simply copies the data from
2117 * the original URI into the canonicalized URI, but, it still recognizes URI's
2118 * host type as HOST_IPV4.
2120 static BOOL
canonicalize_ipv4address(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
2121 if(data
->has_implicit_ip
)
2122 return canonicalize_implicit_ipv4address(data
, uri
, flags
, computeOnly
);
2124 uri
->host_start
= uri
->canon_len
;
2126 /* Windows only normalizes for known scheme types. */
2127 if(data
->scheme_type
!= URL_SCHEME_UNKNOWN
) {
2128 /* parse_data contains a partial or full IPv4 address, so normalize it. */
2129 DWORD i
, octetDigitCount
= 0, octetCount
= 0;
2130 BOOL octetHasDigit
= FALSE
;
2132 for(i
= 0; i
< data
->host_len
; ++i
) {
2133 if(data
->host
[i
] == '0' && !octetHasDigit
) {
2134 /* Can ignore leading zeros if:
2135 * 1) It isn't the last digit of the octet.
2136 * 2) i+1 != data->host_len
2139 if(octetDigitCount
== 2 ||
2140 i
+1 == data
->host_len
||
2141 data
->host
[i
+1] == '.') {
2143 uri
->canon_uri
[uri
->canon_len
] = data
->host
[i
];
2145 TRACE("Adding zero\n");
2147 } else if(data
->host
[i
] == '.') {
2149 uri
->canon_uri
[uri
->canon_len
] = data
->host
[i
];
2152 octetDigitCount
= 0;
2153 octetHasDigit
= FALSE
;
2157 uri
->canon_uri
[uri
->canon_len
] = data
->host
[i
];
2161 octetHasDigit
= TRUE
;
2165 /* Make sure the canonicalized IP address has 4 dec-octets.
2166 * If doesn't add "0" ones until there is 4;
2168 for( ; octetCount
< 3; ++octetCount
) {
2170 uri
->canon_uri
[uri
->canon_len
] = '.';
2171 uri
->canon_uri
[uri
->canon_len
+1] = '0';
2174 uri
->canon_len
+= 2;
2177 /* Windows doesn't normalize addresses in unknown schemes. */
2179 memcpy(uri
->canon_uri
+uri
->canon_len
, data
->host
, data
->host_len
*sizeof(WCHAR
));
2180 uri
->canon_len
+= data
->host_len
;
2183 uri
->host_len
= uri
->canon_len
- uri
->host_start
;
2185 TRACE("(%p %p %x %d): Canonicalized IPv4 address, ip=%s len=%d\n",
2186 data
, uri
, flags
, computeOnly
,
2187 debugstr_wn(uri
->canon_uri
+uri
->host_start
, uri
->host_len
),
2194 /* Attempts to canonicalize the IPv6 address of the URI.
2196 * Multiple things happen during the canonicalization of an IPv6 address:
2197 * 1) Any leading zero's in a h16 component are removed.
2198 * Ex: [0001:0022::] -> [1:22::]
2200 * 2) The longest sequence of zero h16 components are compressed
2201 * into a "::" (elision). If there's a tie, the first is chosen.
2203 * Ex: [0:0:0:0:1:6:7:8] -> [::1:6:7:8]
2204 * [0:0:0:0:1:2::] -> [::1:2:0:0]
2205 * [0:0:1:2:0:0:7:8] -> [::1:2:0:0:7:8]
2207 * 3) If an IPv4 address is attached to the IPv6 address, it's
2209 * Ex: [::001.002.022.000] -> [::1.2.22.0]
2211 * 4) If an elision is present, but, only represents one h16 component
2214 * Ex: [1::2:3:4:5:6:7] -> [1:0:2:3:4:5:6:7]
2216 * 5) If the IPv6 address contains an IPv4 address and there exists
2217 * at least 1 non-zero h16 component the IPv4 address is converted
2218 * into two h16 components, otherwise it's normalized and kept as is.
2220 * Ex: [::192.200.003.4] -> [::192.200.3.4]
2221 * [ffff::192.200.003.4] -> [ffff::c0c8:3041]
2224 * For unknown scheme types Windows simply copies the address over without any
2227 * IPv4 address can be included in an elision if all its components are 0's.
2229 static BOOL
canonicalize_ipv6address(const parse_data
*data
, Uri
*uri
,
2230 DWORD flags
, BOOL computeOnly
) {
2231 uri
->host_start
= uri
->canon_len
;
2233 if(data
->scheme_type
== URL_SCHEME_UNKNOWN
) {
2235 memcpy(uri
->canon_uri
+uri
->canon_len
, data
->host
, data
->host_len
*sizeof(WCHAR
));
2236 uri
->canon_len
+= data
->host_len
;
2239 ULONG size
= ARRAY_SIZE(buffer
);
2242 RtlIpv6AddressToStringExW(&data
->ipv6_address
, 0, 0, buffer
, &size
);
2243 uri
->canon_len
+= size
+ 1;
2245 uri
->canon_uri
[uri
->canon_len
++] = '[';
2246 RtlIpv6AddressToStringExW(&data
->ipv6_address
, 0, 0, uri
->canon_uri
+ uri
->canon_len
, &size
);
2247 uri
->canon_len
+= size
- 1;
2248 uri
->canon_uri
[uri
->canon_len
++] = ']';
2252 uri
->host_len
= uri
->canon_len
- uri
->host_start
;
2255 TRACE("(%p %p %x %d): Canonicalized IPv6 address %s, len=%d\n", data
, uri
, flags
,
2256 computeOnly
, debugstr_wn(uri
->canon_uri
+uri
->host_start
, uri
->host_len
),
2262 /* Attempts to canonicalize the host of the URI (if any). */
2263 static BOOL
canonicalize_host(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
2264 uri
->host_start
= -1;
2266 uri
->domain_offset
= -1;
2269 switch(data
->host_type
) {
2271 uri
->host_type
= Uri_HOST_DNS
;
2272 if(!canonicalize_reg_name(data
, uri
, flags
, computeOnly
))
2277 uri
->host_type
= Uri_HOST_IPV4
;
2278 if(!canonicalize_ipv4address(data
, uri
, flags
, computeOnly
))
2283 if(!canonicalize_ipv6address(data
, uri
, flags
, computeOnly
))
2286 uri
->host_type
= Uri_HOST_IPV6
;
2288 case Uri_HOST_UNKNOWN
:
2289 if(data
->host_len
> 0 || data
->scheme_type
!= URL_SCHEME_FILE
) {
2290 uri
->host_start
= uri
->canon_len
;
2292 /* Nothing happens to unknown host types. */
2294 memcpy(uri
->canon_uri
+uri
->canon_len
, data
->host
, data
->host_len
*sizeof(WCHAR
));
2295 uri
->canon_len
+= data
->host_len
;
2296 uri
->host_len
= data
->host_len
;
2299 uri
->host_type
= Uri_HOST_UNKNOWN
;
2302 FIXME("(%p %p %x %d): Canonicalization for host type %d not supported.\n", data
,
2303 uri
, flags
, computeOnly
, data
->host_type
);
2311 static BOOL
canonicalize_port(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
2312 BOOL has_default_port
= FALSE
;
2313 USHORT default_port
= 0;
2316 uri
->port_offset
= -1;
2318 /* Check if the scheme has a default port. */
2319 for(i
= 0; i
< ARRAY_SIZE(default_ports
); ++i
) {
2320 if(default_ports
[i
].scheme
== data
->scheme_type
) {
2321 has_default_port
= TRUE
;
2322 default_port
= default_ports
[i
].port
;
2327 uri
->has_port
= data
->has_port
|| has_default_port
;
2330 * 1) Has a port which is the default port.
2331 * 2) Has a port (not the default).
2332 * 3) Doesn't have a port, but, scheme has a default port.
2335 if(has_default_port
&& data
->has_port
&& data
->port_value
== default_port
) {
2336 /* If it's the default port and this flag isn't set, don't do anything. */
2337 if(flags
& Uri_CREATE_NO_CANONICALIZE
) {
2338 uri
->port_offset
= uri
->canon_len
-uri
->authority_start
;
2340 uri
->canon_uri
[uri
->canon_len
] = ':';
2344 /* Copy the original port over. */
2346 memcpy(uri
->canon_uri
+uri
->canon_len
, data
->port
, data
->port_len
*sizeof(WCHAR
));
2347 uri
->canon_len
+= data
->port_len
;
2350 uri
->canon_len
+= ui2str(uri
->canon_uri
+uri
->canon_len
, data
->port_value
);
2352 uri
->canon_len
+= ui2str(NULL
, data
->port_value
);
2356 uri
->port
= default_port
;
2357 } else if(data
->has_port
) {
2358 uri
->port_offset
= uri
->canon_len
-uri
->authority_start
;
2360 uri
->canon_uri
[uri
->canon_len
] = ':';
2363 if(flags
& Uri_CREATE_NO_CANONICALIZE
&& data
->port
) {
2364 /* Copy the original over without changes. */
2366 memcpy(uri
->canon_uri
+uri
->canon_len
, data
->port
, data
->port_len
*sizeof(WCHAR
));
2367 uri
->canon_len
+= data
->port_len
;
2370 uri
->canon_len
+= ui2str(uri
->canon_uri
+uri
->canon_len
, data
->port_value
);
2372 uri
->canon_len
+= ui2str(NULL
, data
->port_value
);
2375 uri
->port
= data
->port_value
;
2376 } else if(has_default_port
)
2377 uri
->port
= default_port
;
2382 /* Canonicalizes the authority of the URI represented by the parse_data. */
2383 static BOOL
canonicalize_authority(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
2384 uri
->authority_start
= uri
->canon_len
;
2385 uri
->authority_len
= 0;
2387 if(!canonicalize_userinfo(data
, uri
, flags
, computeOnly
))
2390 if(!canonicalize_host(data
, uri
, flags
, computeOnly
))
2393 if(!canonicalize_port(data
, uri
, flags
, computeOnly
))
2396 if(uri
->host_start
!= -1 || (data
->is_relative
&& (data
->password
|| data
->username
)))
2397 uri
->authority_len
= uri
->canon_len
- uri
->authority_start
;
2399 uri
->authority_start
= -1;
2404 /* Attempts to canonicalize the path of a hierarchical URI.
2406 * Things that happen:
2407 * 1). Forbidden characters are percent encoded, unless the NO_ENCODE_FORBIDDEN
2408 * flag is set or it's a file URI. Forbidden characters are always encoded
2409 * for file schemes regardless and forbidden characters are never encoded
2410 * for unknown scheme types.
2412 * 2). For known scheme types '\\' are changed to '/'.
2414 * 3). Percent encoded, unreserved characters are decoded to their actual values.
2415 * Unless the scheme type is unknown. For file schemes any percent encoded
2416 * character in the unreserved or reserved set is decoded.
2418 * 4). For File schemes if the path is starts with a drive letter and doesn't
2419 * start with a '/' then one is appended.
2420 * Ex: file://c:/test.mp3 -> file:///c:/test.mp3
2422 * 5). Dot segments are removed from the path for all scheme types
2423 * unless NO_CANONICALIZE flag is set. Dot segments aren't removed
2424 * for wildcard scheme types.
2427 * file://c:/test%20test -> file:///c:/test%2520test
2428 * file://c:/test%3Etest -> file:///c:/test%253Etest
2429 * if Uri_CREATE_FILE_USE_DOS_PATH is not set:
2430 * file:///c:/test%20test -> file:///c:/test%20test
2431 * file:///c:/test%test -> file:///c:/test%25test
2433 static DWORD
canonicalize_path_hierarchical(const WCHAR
*path
, DWORD path_len
, URL_SCHEME scheme_type
, BOOL has_host
, DWORD flags
,
2434 BOOL is_implicit_scheme
, WCHAR
*ret_path
) {
2435 const BOOL known_scheme
= scheme_type
!= URL_SCHEME_UNKNOWN
;
2436 const BOOL is_file
= scheme_type
== URL_SCHEME_FILE
;
2437 const BOOL is_res
= scheme_type
== URL_SCHEME_RES
;
2439 BOOL escape_pct
= FALSE
;
2447 if(is_file
&& !has_host
) {
2448 /* Check if a '/' needs to be appended for the file scheme. */
2449 if(path_len
> 1 && is_drive_path(ptr
) && !(flags
& Uri_CREATE_FILE_USE_DOS_PATH
)) {
2451 ret_path
[len
] = '/';
2454 } else if(*ptr
== '/') {
2455 if(!(flags
& Uri_CREATE_FILE_USE_DOS_PATH
)) {
2456 /* Copy the extra '/' over. */
2458 ret_path
[len
] = '/';
2464 if(is_drive_path(ptr
)) {
2466 ret_path
[len
] = *ptr
;
2467 /* If there's a '|' after the drive letter, convert it to a ':'. */
2468 ret_path
[len
+1] = ':';
2475 if(!is_file
&& *path
&& *path
!= '/') {
2476 /* Prepend a '/' to the path if it doesn't have one. */
2478 ret_path
[len
] = '/';
2482 for(; ptr
< path
+path_len
; ++ptr
) {
2483 BOOL do_default_action
= TRUE
;
2485 if(*ptr
== '%' && !is_res
) {
2486 const WCHAR
*tmp
= ptr
;
2489 /* Check if the % represents a valid encoded char, or if it needs encoding. */
2490 BOOL force_encode
= !check_pct_encoded(&tmp
) && is_file
&& !(flags
&Uri_CREATE_FILE_USE_DOS_PATH
);
2491 val
= decode_pct_val(ptr
);
2493 if(force_encode
|| escape_pct
) {
2494 /* Escape the percent sign in the file URI. */
2496 pct_encode_val(*ptr
, ret_path
+len
);
2498 do_default_action
= FALSE
;
2499 } else if((is_unreserved(val
) && known_scheme
) ||
2500 (is_file
&& !is_implicit_scheme
&& (is_unreserved(val
) || is_reserved(val
) ||
2501 (val
&& flags
&Uri_CREATE_FILE_USE_DOS_PATH
&& !is_forbidden_dos_path_char(val
))))) {
2503 ret_path
[len
] = val
;
2509 } else if(*ptr
== '/' && is_file
&& (flags
& Uri_CREATE_FILE_USE_DOS_PATH
)) {
2510 /* Convert the '/' back to a '\\'. */
2512 ret_path
[len
] = '\\';
2514 do_default_action
= FALSE
;
2515 } else if(*ptr
== '\\' && known_scheme
) {
2516 if(!(is_file
&& (flags
& Uri_CREATE_FILE_USE_DOS_PATH
))) {
2517 /* Convert '\\' into a '/'. */
2519 ret_path
[len
] = '/';
2521 do_default_action
= FALSE
;
2523 } else if(known_scheme
&& !is_res
&& is_ascii(*ptr
) && !is_unreserved(*ptr
) && !is_reserved(*ptr
) &&
2524 (!(flags
& Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS
) || is_file
)) {
2525 if(!is_file
|| !(flags
& Uri_CREATE_FILE_USE_DOS_PATH
)) {
2526 /* Escape the forbidden character. */
2528 pct_encode_val(*ptr
, ret_path
+len
);
2530 do_default_action
= FALSE
;
2534 if(do_default_action
) {
2536 ret_path
[len
] = *ptr
;
2541 /* Removing the dot segments only happens when it's not in
2542 * computeOnly mode and it's not a wildcard scheme. File schemes
2543 * with USE_DOS_PATH set don't get dot segments removed.
2545 if(!(is_file
&& (flags
& Uri_CREATE_FILE_USE_DOS_PATH
)) &&
2546 scheme_type
!= URL_SCHEME_WILDCARD
) {
2547 if(!(flags
& Uri_CREATE_NO_CANONICALIZE
) && ret_path
) {
2548 /* Remove the dot segments (if any) and reset everything to the new
2551 len
= remove_dot_segments(ret_path
, len
);
2556 TRACE("Canonicalized path %s len=%d\n", debugstr_wn(ret_path
, len
), len
);
2560 /* Attempts to canonicalize the path for an opaque URI.
2562 * For known scheme types:
2563 * 1) forbidden characters are percent encoded if
2564 * NO_ENCODE_FORBIDDEN_CHARACTERS isn't set.
2566 * 2) Percent encoded, unreserved characters are decoded
2567 * to their actual values, for known scheme types.
2569 * 3) '\\' are changed to '/' for known scheme types
2570 * except for mailto schemes.
2572 * 4) For file schemes, if USE_DOS_PATH is set all '/'
2573 * are converted to backslashes.
2575 * 5) For file schemes, if USE_DOS_PATH isn't set all '\'
2576 * are converted to forward slashes.
2578 static BOOL
canonicalize_path_opaque(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
2580 const BOOL known_scheme
= data
->scheme_type
!= URL_SCHEME_UNKNOWN
;
2581 const BOOL is_file
= data
->scheme_type
== URL_SCHEME_FILE
;
2582 const BOOL is_mk
= data
->scheme_type
== URL_SCHEME_MK
;
2585 uri
->path_start
= -1;
2590 uri
->path_start
= uri
->canon_len
;
2593 /* hijack this flag for SCHEME_MK to tell the function when to start
2594 * converting slashes */
2595 flags
|= Uri_CREATE_FILE_USE_DOS_PATH
;
2598 /* For javascript: URIs, simply copy path part without any canonicalization */
2599 if(data
->scheme_type
== URL_SCHEME_JAVASCRIPT
) {
2601 memcpy(uri
->canon_uri
+uri
->canon_len
, data
->path
, data
->path_len
*sizeof(WCHAR
));
2602 uri
->path_len
= data
->path_len
;
2603 uri
->canon_len
+= data
->path_len
;
2607 /* Windows doesn't allow a "//" to appear after the scheme
2608 * of a URI, if it's an opaque URI.
2610 if(data
->scheme
&& *(data
->path
) == '/' && *(data
->path
+1) == '/') {
2611 /* So it inserts a "/." before the "//" if it exists. */
2613 uri
->canon_uri
[uri
->canon_len
] = '/';
2614 uri
->canon_uri
[uri
->canon_len
+1] = '.';
2617 uri
->canon_len
+= 2;
2620 for(ptr
= data
->path
; ptr
< data
->path
+data
->path_len
; ++ptr
) {
2621 BOOL do_default_action
= TRUE
;
2623 if(*ptr
== '%' && known_scheme
) {
2624 WCHAR val
= decode_pct_val(ptr
);
2626 if(is_unreserved(val
)) {
2628 uri
->canon_uri
[uri
->canon_len
] = val
;
2634 } else if(*ptr
== '/' && is_file
&& (flags
& Uri_CREATE_FILE_USE_DOS_PATH
)) {
2636 uri
->canon_uri
[uri
->canon_len
] = '\\';
2638 do_default_action
= FALSE
;
2639 } else if(*ptr
== '\\') {
2640 if((data
->is_relative
|| is_mk
|| is_file
) && !(flags
& Uri_CREATE_FILE_USE_DOS_PATH
)) {
2641 /* Convert to a '/'. */
2643 uri
->canon_uri
[uri
->canon_len
] = '/';
2645 do_default_action
= FALSE
;
2647 } else if(is_mk
&& *ptr
== ':' && ptr
+ 1 < data
->path
+ data
->path_len
&& *(ptr
+ 1) == ':') {
2648 flags
&= ~Uri_CREATE_FILE_USE_DOS_PATH
;
2649 } else if(known_scheme
&& is_ascii(*ptr
) && !is_unreserved(*ptr
) && !is_reserved(*ptr
) &&
2650 !(flags
& Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS
)) {
2651 if(!(is_file
&& (flags
& Uri_CREATE_FILE_USE_DOS_PATH
))) {
2653 pct_encode_val(*ptr
, uri
->canon_uri
+uri
->canon_len
);
2654 uri
->canon_len
+= 3;
2655 do_default_action
= FALSE
;
2659 if(do_default_action
) {
2661 uri
->canon_uri
[uri
->canon_len
] = *ptr
;
2666 if(is_mk
&& !computeOnly
&& !(flags
& Uri_CREATE_NO_CANONICALIZE
)) {
2667 DWORD new_len
= remove_dot_segments(uri
->canon_uri
+ uri
->path_start
,
2668 uri
->canon_len
- uri
->path_start
);
2669 uri
->canon_len
= uri
->path_start
+ new_len
;
2672 uri
->path_len
= uri
->canon_len
- uri
->path_start
;
2675 TRACE("(%p %p %x %d): Canonicalized opaque URI path %s len=%d\n", data
, uri
, flags
, computeOnly
,
2676 debugstr_wn(uri
->canon_uri
+uri
->path_start
, uri
->path_len
), uri
->path_len
);
2680 /* Determines how the URI represented by the parse_data should be canonicalized.
2682 * Essentially, if the parse_data represents an hierarchical URI then it calls
2683 * canonicalize_authority and the canonicalization functions for the path. If the
2684 * URI is opaque it canonicalizes the path of the URI.
2686 static BOOL
canonicalize_hierpart(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
2687 if(!data
->is_opaque
|| (data
->is_relative
&& (data
->password
|| data
->username
))) {
2688 /* "//" is only added for non-wildcard scheme types.
2690 * A "//" is only added to a relative URI if it has a
2691 * host or port component (this only happens if a IUriBuilder
2692 * is generating an IUri).
2694 if((data
->is_relative
&& (data
->host
|| data
->has_port
)) ||
2695 (!data
->is_relative
&& data
->scheme_type
!= URL_SCHEME_WILDCARD
)) {
2696 if(data
->scheme_type
== URL_SCHEME_WILDCARD
)
2700 INT pos
= uri
->canon_len
;
2702 uri
->canon_uri
[pos
] = '/';
2703 uri
->canon_uri
[pos
+1] = '/';
2705 uri
->canon_len
+= 2;
2708 if(!canonicalize_authority(data
, uri
, flags
, computeOnly
))
2711 if(data
->is_relative
&& (data
->password
|| data
->username
)) {
2712 if(!canonicalize_path_opaque(data
, uri
, flags
, computeOnly
))
2716 uri
->path_start
= uri
->canon_len
;
2717 uri
->path_len
= canonicalize_path_hierarchical(data
->path
, data
->path_len
, data
->scheme_type
, data
->host_len
!= 0,
2718 flags
, data
->has_implicit_scheme
, computeOnly
? NULL
: uri
->canon_uri
+uri
->canon_len
);
2719 uri
->canon_len
+= uri
->path_len
;
2720 if(!computeOnly
&& !uri
->path_len
)
2721 uri
->path_start
= -1;
2724 /* Opaque URI's don't have an authority. */
2725 uri
->userinfo_start
= uri
->userinfo_split
= -1;
2726 uri
->userinfo_len
= 0;
2727 uri
->host_start
= -1;
2729 uri
->host_type
= Uri_HOST_UNKNOWN
;
2730 uri
->has_port
= FALSE
;
2731 uri
->authority_start
= -1;
2732 uri
->authority_len
= 0;
2733 uri
->domain_offset
= -1;
2734 uri
->port_offset
= -1;
2736 if(is_hierarchical_scheme(data
->scheme_type
)) {
2739 /* Absolute URIs aren't displayed for known scheme types
2740 * which should be hierarchical URIs.
2742 uri
->display_modifiers
|= URI_DISPLAY_NO_ABSOLUTE_URI
;
2744 /* Windows also sets the port for these (if they have one). */
2745 for(i
= 0; i
< ARRAY_SIZE(default_ports
); ++i
) {
2746 if(data
->scheme_type
== default_ports
[i
].scheme
) {
2747 uri
->has_port
= TRUE
;
2748 uri
->port
= default_ports
[i
].port
;
2754 if(!canonicalize_path_opaque(data
, uri
, flags
, computeOnly
))
2758 if(uri
->path_start
> -1 && !computeOnly
)
2759 /* Finding file extensions happens for both types of URIs. */
2760 uri
->extension_offset
= find_file_extension(uri
->canon_uri
+uri
->path_start
, uri
->path_len
);
2762 uri
->extension_offset
= -1;
2767 /* Attempts to canonicalize the query string of the URI.
2769 * Things that happen:
2770 * 1) For known scheme types forbidden characters
2771 * are percent encoded, unless the NO_DECODE_EXTRA_INFO flag is set
2772 * or NO_ENCODE_FORBIDDEN_CHARACTERS is set.
2774 * 2) For known scheme types, percent encoded, unreserved characters
2775 * are decoded as long as the NO_DECODE_EXTRA_INFO flag isn't set.
2777 static BOOL
canonicalize_query(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
2778 const WCHAR
*ptr
, *end
;
2779 const BOOL known_scheme
= data
->scheme_type
!= URL_SCHEME_UNKNOWN
;
2782 uri
->query_start
= -1;
2787 uri
->query_start
= uri
->canon_len
;
2789 end
= data
->query
+data
->query_len
;
2790 for(ptr
= data
->query
; ptr
< end
; ++ptr
) {
2792 if(known_scheme
&& !(flags
& Uri_CREATE_NO_DECODE_EXTRA_INFO
)) {
2793 WCHAR val
= decode_pct_val(ptr
);
2794 if(is_unreserved(val
)) {
2796 uri
->canon_uri
[uri
->canon_len
] = val
;
2803 } else if(known_scheme
&& is_ascii(*ptr
) && !is_unreserved(*ptr
) && !is_reserved(*ptr
)) {
2804 if(!(flags
& Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS
) &&
2805 !(flags
& Uri_CREATE_NO_DECODE_EXTRA_INFO
)) {
2807 pct_encode_val(*ptr
, uri
->canon_uri
+uri
->canon_len
);
2808 uri
->canon_len
+= 3;
2814 uri
->canon_uri
[uri
->canon_len
] = *ptr
;
2818 uri
->query_len
= uri
->canon_len
- uri
->query_start
;
2821 TRACE("(%p %p %x %d): Canonicalized query string %s len=%d\n", data
, uri
, flags
,
2822 computeOnly
, debugstr_wn(uri
->canon_uri
+uri
->query_start
, uri
->query_len
),
2827 static BOOL
canonicalize_fragment(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
2828 const WCHAR
*ptr
, *end
;
2829 const BOOL known_scheme
= data
->scheme_type
!= URL_SCHEME_UNKNOWN
;
2831 if(!data
->fragment
) {
2832 uri
->fragment_start
= -1;
2833 uri
->fragment_len
= 0;
2837 uri
->fragment_start
= uri
->canon_len
;
2839 end
= data
->fragment
+ data
->fragment_len
;
2840 for(ptr
= data
->fragment
; ptr
< end
; ++ptr
) {
2842 if(known_scheme
&& !(flags
& Uri_CREATE_NO_DECODE_EXTRA_INFO
)) {
2843 WCHAR val
= decode_pct_val(ptr
);
2844 if(is_unreserved(val
)) {
2846 uri
->canon_uri
[uri
->canon_len
] = val
;
2853 } else if(known_scheme
&& is_ascii(*ptr
) && !is_unreserved(*ptr
) && !is_reserved(*ptr
)) {
2854 if(!(flags
& Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS
) &&
2855 !(flags
& Uri_CREATE_NO_DECODE_EXTRA_INFO
)) {
2857 pct_encode_val(*ptr
, uri
->canon_uri
+uri
->canon_len
);
2858 uri
->canon_len
+= 3;
2864 uri
->canon_uri
[uri
->canon_len
] = *ptr
;
2868 uri
->fragment_len
= uri
->canon_len
- uri
->fragment_start
;
2871 TRACE("(%p %p %x %d): Canonicalized fragment %s len=%d\n", data
, uri
, flags
,
2872 computeOnly
, debugstr_wn(uri
->canon_uri
+uri
->fragment_start
, uri
->fragment_len
),
2877 /* Canonicalizes the scheme information specified in the parse_data using the specified flags. */
2878 static BOOL
canonicalize_scheme(const parse_data
*data
, Uri
*uri
, DWORD flags
, BOOL computeOnly
) {
2879 uri
->scheme_start
= -1;
2880 uri
->scheme_len
= 0;
2883 /* The only type of URI that doesn't have to have a scheme is a relative
2886 if(!data
->is_relative
) {
2887 FIXME("(%p %p %x): Unable to determine the scheme type of %s.\n", data
,
2888 uri
, flags
, debugstr_w(data
->uri
));
2894 INT pos
= uri
->canon_len
;
2896 for(i
= 0; i
< data
->scheme_len
; ++i
) {
2897 /* Scheme name must be lower case after canonicalization. */
2898 uri
->canon_uri
[i
+ pos
] = towlower(data
->scheme
[i
]);
2901 uri
->canon_uri
[i
+ pos
] = ':';
2902 uri
->scheme_start
= pos
;
2904 TRACE("(%p %p %x): Canonicalized scheme=%s, len=%d.\n", data
, uri
, flags
,
2905 debugstr_wn(uri
->canon_uri
+uri
->scheme_start
, data
->scheme_len
), data
->scheme_len
);
2908 /* This happens in both computation modes. */
2909 uri
->canon_len
+= data
->scheme_len
+ 1;
2910 uri
->scheme_len
= data
->scheme_len
;
2915 /* Computes what the length of the URI specified by the parse_data will be
2916 * after canonicalization occurs using the specified flags.
2918 * This function will return a non-zero value indicating the length of the canonicalized
2919 * URI, or -1 on error.
2921 static int compute_canonicalized_length(const parse_data
*data
, DWORD flags
) {
2924 memset(&uri
, 0, sizeof(Uri
));
2926 TRACE("(%p %x): Beginning to compute canonicalized length for URI %s\n", data
, flags
,
2927 debugstr_w(data
->uri
));
2929 if(!canonicalize_scheme(data
, &uri
, flags
, TRUE
)) {
2930 ERR("(%p %x): Failed to compute URI scheme length.\n", data
, flags
);
2934 if(!canonicalize_hierpart(data
, &uri
, flags
, TRUE
)) {
2935 ERR("(%p %x): Failed to compute URI hierpart length.\n", data
, flags
);
2939 if(!canonicalize_query(data
, &uri
, flags
, TRUE
)) {
2940 ERR("(%p %x): Failed to compute query string length.\n", data
, flags
);
2944 if(!canonicalize_fragment(data
, &uri
, flags
, TRUE
)) {
2945 ERR("(%p %x): Failed to compute fragment length.\n", data
, flags
);
2949 TRACE("(%p %x): Finished computing canonicalized URI length. length=%d\n", data
, flags
, uri
.canon_len
);
2951 return uri
.canon_len
;
2954 /* Canonicalizes the URI data specified in the parse_data, using the given flags. If the
2955 * canonicalization succeeds it will store all the canonicalization information
2956 * in the pointer to the Uri.
2958 * To canonicalize a URI this function first computes what the length of the URI
2959 * specified by the parse_data will be. Once this is done it will then perform the actual
2960 * canonicalization of the URI.
2962 static HRESULT
canonicalize_uri(const parse_data
*data
, Uri
*uri
, DWORD flags
) {
2965 uri
->canon_uri
= NULL
;
2966 uri
->canon_size
= uri
->canon_len
= 0;
2968 TRACE("(%p %p %x): beginning to canonicalize URI %s.\n", data
, uri
, flags
, debugstr_w(data
->uri
));
2970 /* First try to compute the length of the URI. */
2971 len
= compute_canonicalized_length(data
, flags
);
2973 ERR("(%p %p %x): Could not compute the canonicalized length of %s.\n", data
, uri
, flags
,
2974 debugstr_w(data
->uri
));
2975 return E_INVALIDARG
;
2978 uri
->canon_uri
= heap_alloc((len
+1)*sizeof(WCHAR
));
2980 return E_OUTOFMEMORY
;
2982 uri
->canon_size
= len
;
2983 if(!canonicalize_scheme(data
, uri
, flags
, FALSE
)) {
2984 ERR("(%p %p %x): Unable to canonicalize the scheme of the URI.\n", data
, uri
, flags
);
2985 return E_INVALIDARG
;
2987 uri
->scheme_type
= data
->scheme_type
;
2989 if(!canonicalize_hierpart(data
, uri
, flags
, FALSE
)) {
2990 ERR("(%p %p %x): Unable to canonicalize the hierpart of the URI\n", data
, uri
, flags
);
2991 return E_INVALIDARG
;
2994 if(!canonicalize_query(data
, uri
, flags
, FALSE
)) {
2995 ERR("(%p %p %x): Unable to canonicalize query string of the URI.\n",
2997 return E_INVALIDARG
;
3000 if(!canonicalize_fragment(data
, uri
, flags
, FALSE
)) {
3001 ERR("(%p %p %x): Unable to canonicalize fragment of the URI.\n",
3003 return E_INVALIDARG
;
3006 /* There's a possibility we didn't use all the space we allocated
3009 if(uri
->canon_len
< uri
->canon_size
) {
3010 /* This happens if the URI is hierarchical and dot
3011 * segments were removed from its path.
3013 WCHAR
*tmp
= heap_realloc(uri
->canon_uri
, (uri
->canon_len
+1)*sizeof(WCHAR
));
3015 return E_OUTOFMEMORY
;
3017 uri
->canon_uri
= tmp
;
3018 uri
->canon_size
= uri
->canon_len
;
3021 uri
->canon_uri
[uri
->canon_len
] = '\0';
3022 TRACE("(%p %p %x): finished canonicalizing the URI. uri=%s\n", data
, uri
, flags
, debugstr_w(uri
->canon_uri
));
3027 static HRESULT
get_builder_component(LPWSTR
*component
, DWORD
*component_len
,
3028 LPCWSTR source
, DWORD source_len
,
3029 LPCWSTR
*output
, DWORD
*output_len
)
3042 if(!(*component
) && source
) {
3043 /* Allocate 'component', and copy the contents from 'source'
3044 * into the new allocation.
3046 *component
= heap_alloc((source_len
+1)*sizeof(WCHAR
));
3048 return E_OUTOFMEMORY
;
3050 memcpy(*component
, source
, source_len
*sizeof(WCHAR
));
3051 (*component
)[source_len
] = '\0';
3052 *component_len
= source_len
;
3055 *output
= *component
;
3056 *output_len
= *component_len
;
3057 return *output
? S_OK
: S_FALSE
;
3060 /* Allocates 'component' and copies the string from 'new_value' into 'component'.
3061 * If 'prefix' is set and 'new_value' isn't NULL, then it checks if 'new_value'
3062 * starts with 'prefix'. If it doesn't then 'prefix' is prepended to 'component'.
3064 * If everything is successful, then will set 'success_flag' in 'flags'.
3066 static HRESULT
set_builder_component(LPWSTR
*component
, DWORD
*component_len
, LPCWSTR new_value
,
3067 WCHAR prefix
, DWORD
*flags
, DWORD success_flag
)
3069 heap_free(*component
);
3075 BOOL add_prefix
= FALSE
;
3076 DWORD len
= lstrlenW(new_value
);
3079 if(prefix
&& *new_value
!= prefix
) {
3081 *component
= heap_alloc((len
+2)*sizeof(WCHAR
));
3083 *component
= heap_alloc((len
+1)*sizeof(WCHAR
));
3086 return E_OUTOFMEMORY
;
3089 (*component
)[pos
++] = prefix
;
3091 memcpy(*component
+pos
, new_value
, (len
+1)*sizeof(WCHAR
));
3092 *component_len
= len
+pos
;
3095 *flags
|= success_flag
;
3099 static void reset_builder(UriBuilder
*builder
) {
3101 IUri_Release(&builder
->uri
->IUri_iface
);
3102 builder
->uri
= NULL
;
3104 heap_free(builder
->fragment
);
3105 builder
->fragment
= NULL
;
3106 builder
->fragment_len
= 0;
3108 heap_free(builder
->host
);
3109 builder
->host
= NULL
;
3110 builder
->host_len
= 0;
3112 heap_free(builder
->password
);
3113 builder
->password
= NULL
;
3114 builder
->password_len
= 0;
3116 heap_free(builder
->path
);
3117 builder
->path
= NULL
;
3118 builder
->path_len
= 0;
3120 heap_free(builder
->query
);
3121 builder
->query
= NULL
;
3122 builder
->query_len
= 0;
3124 heap_free(builder
->scheme
);
3125 builder
->scheme
= NULL
;
3126 builder
->scheme_len
= 0;
3128 heap_free(builder
->username
);
3129 builder
->username
= NULL
;
3130 builder
->username_len
= 0;
3132 builder
->has_port
= FALSE
;
3134 builder
->modified_props
= 0;
3137 static HRESULT
validate_scheme_name(const UriBuilder
*builder
, parse_data
*data
, DWORD flags
) {
3138 const WCHAR
*component
;
3143 if(builder
->scheme
) {
3144 ptr
= builder
->scheme
;
3145 expected_len
= builder
->scheme_len
;
3146 } else if(builder
->uri
&& builder
->uri
->scheme_start
> -1) {
3147 ptr
= builder
->uri
->canon_uri
+builder
->uri
->scheme_start
;
3148 expected_len
= builder
->uri
->scheme_len
;
3150 static const WCHAR nullW
[] = {0};
3157 if(parse_scheme(pptr
, data
, flags
, ALLOW_NULL_TERM_SCHEME
) &&
3158 data
->scheme_len
== expected_len
) {
3160 TRACE("(%p %p %x): Found valid scheme component %s len=%d.\n", builder
, data
, flags
,
3161 debugstr_wn(data
->scheme
, data
->scheme_len
), data
->scheme_len
);
3163 TRACE("(%p %p %x): Invalid scheme component found %s.\n", builder
, data
, flags
,
3164 debugstr_wn(component
, expected_len
));
3165 return INET_E_INVALID_URL
;
3171 static HRESULT
validate_username(const UriBuilder
*builder
, parse_data
*data
, DWORD flags
) {
3176 if(builder
->username
) {
3177 ptr
= builder
->username
;
3178 expected_len
= builder
->username_len
;
3179 } else if(!(builder
->modified_props
& Uri_HAS_USER_NAME
) && builder
->uri
&&
3180 builder
->uri
->userinfo_start
> -1 && builder
->uri
->userinfo_split
!= 0) {
3181 /* Just use the username from the base Uri. */
3182 data
->username
= builder
->uri
->canon_uri
+builder
->uri
->userinfo_start
;
3183 data
->username_len
= (builder
->uri
->userinfo_split
> -1) ?
3184 builder
->uri
->userinfo_split
: builder
->uri
->userinfo_len
;
3192 const WCHAR
*component
= ptr
;
3194 if(parse_username(pptr
, data
, flags
, ALLOW_NULL_TERM_USER_NAME
) &&
3195 data
->username_len
== expected_len
)
3196 TRACE("(%p %p %x): Found valid username component %s len=%d.\n", builder
, data
, flags
,
3197 debugstr_wn(data
->username
, data
->username_len
), data
->username_len
);
3199 TRACE("(%p %p %x): Invalid username component found %s.\n", builder
, data
, flags
,
3200 debugstr_wn(component
, expected_len
));
3201 return INET_E_INVALID_URL
;
3208 static HRESULT
validate_password(const UriBuilder
*builder
, parse_data
*data
, DWORD flags
) {
3213 if(builder
->password
) {
3214 ptr
= builder
->password
;
3215 expected_len
= builder
->password_len
;
3216 } else if(!(builder
->modified_props
& Uri_HAS_PASSWORD
) && builder
->uri
&&
3217 builder
->uri
->userinfo_split
> -1) {
3218 data
->password
= builder
->uri
->canon_uri
+builder
->uri
->userinfo_start
+builder
->uri
->userinfo_split
+1;
3219 data
->password_len
= builder
->uri
->userinfo_len
-builder
->uri
->userinfo_split
-1;
3227 const WCHAR
*component
= ptr
;
3229 if(parse_password(pptr
, data
, flags
, ALLOW_NULL_TERM_PASSWORD
) &&
3230 data
->password_len
== expected_len
)
3231 TRACE("(%p %p %x): Found valid password component %s len=%d.\n", builder
, data
, flags
,
3232 debugstr_wn(data
->password
, data
->password_len
), data
->password_len
);
3234 TRACE("(%p %p %x): Invalid password component found %s.\n", builder
, data
, flags
,
3235 debugstr_wn(component
, expected_len
));
3236 return INET_E_INVALID_URL
;
3243 static HRESULT
validate_userinfo(const UriBuilder
*builder
, parse_data
*data
, DWORD flags
) {
3246 hr
= validate_username(builder
, data
, flags
);
3250 hr
= validate_password(builder
, data
, flags
);
3257 static HRESULT
validate_host(const UriBuilder
*builder
, parse_data
*data
) {
3263 ptr
= builder
->host
;
3264 expected_len
= builder
->host_len
;
3265 } else if(!(builder
->modified_props
& Uri_HAS_HOST
) && builder
->uri
&& builder
->uri
->host_start
> -1) {
3266 ptr
= builder
->uri
->canon_uri
+ builder
->uri
->host_start
;
3267 expected_len
= builder
->uri
->host_len
;
3272 const WCHAR
*component
= ptr
;
3273 DWORD extras
= ALLOW_BRACKETLESS_IP_LITERAL
|IGNORE_PORT_DELIMITER
|SKIP_IP_FUTURE_CHECK
;
3276 if(parse_host(pptr
, data
, extras
) && data
->host_len
== expected_len
)
3277 TRACE("(%p %p): Found valid host name %s len=%d type=%d.\n", builder
, data
,
3278 debugstr_wn(data
->host
, data
->host_len
), data
->host_len
, data
->host_type
);
3280 TRACE("(%p %p): Invalid host name found %s.\n", builder
, data
,
3281 debugstr_wn(component
, expected_len
));
3282 return INET_E_INVALID_URL
;
3289 static void setup_port(const UriBuilder
*builder
, parse_data
*data
, DWORD flags
) {
3290 if(builder
->modified_props
& Uri_HAS_PORT
) {
3291 if(builder
->has_port
) {
3292 data
->has_port
= TRUE
;
3293 data
->port_value
= builder
->port
;
3295 } else if(builder
->uri
&& builder
->uri
->has_port
) {
3296 data
->has_port
= TRUE
;
3297 data
->port_value
= builder
->uri
->port
;
3301 TRACE("(%p %p %x): Using %u as port for IUri.\n", builder
, data
, flags
, data
->port_value
);
3304 static HRESULT
validate_path(const UriBuilder
*builder
, parse_data
*data
, DWORD flags
) {
3305 const WCHAR
*ptr
= NULL
;
3306 const WCHAR
*component
;
3309 BOOL check_len
= TRUE
;
3313 ptr
= builder
->path
;
3314 expected_len
= builder
->path_len
;
3315 } else if(!(builder
->modified_props
& Uri_HAS_PATH
) &&
3316 builder
->uri
&& builder
->uri
->path_start
> -1) {
3317 ptr
= builder
->uri
->canon_uri
+builder
->uri
->path_start
;
3318 expected_len
= builder
->uri
->path_len
;
3320 static const WCHAR nullW
[] = {0};
3329 /* How the path is validated depends on what type of
3332 valid
= data
->is_opaque
?
3333 parse_path_opaque(pptr
, data
, flags
) : parse_path_hierarchical(pptr
, data
, flags
);
3335 if(!valid
|| (check_len
&& expected_len
!= data
->path_len
)) {
3336 TRACE("(%p %p %x): Invalid path component %s.\n", builder
, data
, flags
,
3337 debugstr_wn(component
, expected_len
) );
3338 return INET_E_INVALID_URL
;
3341 TRACE("(%p %p %x): Valid path component %s len=%d.\n", builder
, data
, flags
,
3342 debugstr_wn(data
->path
, data
->path_len
), data
->path_len
);
3347 static HRESULT
validate_query(const UriBuilder
*builder
, parse_data
*data
, DWORD flags
) {
3348 const WCHAR
*ptr
= NULL
;
3352 if(builder
->query
) {
3353 ptr
= builder
->query
;
3354 expected_len
= builder
->query_len
;
3355 } else if(!(builder
->modified_props
& Uri_HAS_QUERY
) && builder
->uri
&&
3356 builder
->uri
->query_start
> -1) {
3357 ptr
= builder
->uri
->canon_uri
+builder
->uri
->query_start
;
3358 expected_len
= builder
->uri
->query_len
;
3362 const WCHAR
*component
= ptr
;
3365 if(parse_query(pptr
, data
, flags
) && expected_len
== data
->query_len
)
3366 TRACE("(%p %p %x): Valid query component %s len=%d.\n", builder
, data
, flags
,
3367 debugstr_wn(data
->query
, data
->query_len
), data
->query_len
);
3369 TRACE("(%p %p %x): Invalid query component %s.\n", builder
, data
, flags
,
3370 debugstr_wn(component
, expected_len
));
3371 return INET_E_INVALID_URL
;
3378 static HRESULT
validate_fragment(const UriBuilder
*builder
, parse_data
*data
, DWORD flags
) {
3379 const WCHAR
*ptr
= NULL
;
3383 if(builder
->fragment
) {
3384 ptr
= builder
->fragment
;
3385 expected_len
= builder
->fragment_len
;
3386 } else if(!(builder
->modified_props
& Uri_HAS_FRAGMENT
) && builder
->uri
&&
3387 builder
->uri
->fragment_start
> -1) {
3388 ptr
= builder
->uri
->canon_uri
+builder
->uri
->fragment_start
;
3389 expected_len
= builder
->uri
->fragment_len
;
3393 const WCHAR
*component
= ptr
;
3396 if(parse_fragment(pptr
, data
, flags
) && expected_len
== data
->fragment_len
)
3397 TRACE("(%p %p %x): Valid fragment component %s len=%d.\n", builder
, data
, flags
,
3398 debugstr_wn(data
->fragment
, data
->fragment_len
), data
->fragment_len
);
3400 TRACE("(%p %p %x): Invalid fragment component %s.\n", builder
, data
, flags
,
3401 debugstr_wn(component
, expected_len
));
3402 return INET_E_INVALID_URL
;
3409 static HRESULT
validate_components(const UriBuilder
*builder
, parse_data
*data
, DWORD flags
) {
3412 memset(data
, 0, sizeof(parse_data
));
3414 TRACE("(%p %p %x): Beginning to validate builder components.\n", builder
, data
, flags
);
3416 hr
= validate_scheme_name(builder
, data
, flags
);
3420 /* Extra validation for file schemes. */
3421 if(data
->scheme_type
== URL_SCHEME_FILE
) {
3422 if((builder
->password
|| (builder
->uri
&& builder
->uri
->userinfo_split
> -1)) ||
3423 (builder
->username
|| (builder
->uri
&& builder
->uri
->userinfo_start
> -1))) {
3424 TRACE("(%p %p %x): File schemes can't contain a username or password.\n",
3425 builder
, data
, flags
);
3426 return INET_E_INVALID_URL
;
3430 hr
= validate_userinfo(builder
, data
, flags
);
3434 hr
= validate_host(builder
, data
);
3438 setup_port(builder
, data
, flags
);
3440 /* The URI is opaque if it doesn't have an authority component. */
3441 if(!data
->is_relative
)
3442 data
->is_opaque
= !data
->username
&& !data
->password
&& !data
->host
&& !data
->has_port
3443 && data
->scheme_type
!= URL_SCHEME_FILE
;
3445 data
->is_opaque
= !data
->host
&& !data
->has_port
;
3447 hr
= validate_path(builder
, data
, flags
);
3451 hr
= validate_query(builder
, data
, flags
);
3455 hr
= validate_fragment(builder
, data
, flags
);
3459 TRACE("(%p %p %x): Finished validating builder components.\n", builder
, data
, flags
);
3464 static HRESULT
compare_file_paths(const Uri
*a
, const Uri
*b
, BOOL
*ret
)
3466 WCHAR
*canon_path_a
, *canon_path_b
;
3470 *ret
= !b
->path_len
;
3480 if(a
->path_len
== b
->path_len
&& !wcsnicmp(a
->canon_uri
+a
->path_start
, b
->canon_uri
+b
->path_start
, a
->path_len
)) {
3485 len_a
= canonicalize_path_hierarchical(a
->canon_uri
+a
->path_start
, a
->path_len
, a
->scheme_type
, FALSE
, 0, FALSE
, NULL
);
3486 len_b
= canonicalize_path_hierarchical(b
->canon_uri
+b
->path_start
, b
->path_len
, b
->scheme_type
, FALSE
, 0, FALSE
, NULL
);
3488 canon_path_a
= heap_alloc(len_a
*sizeof(WCHAR
));
3490 return E_OUTOFMEMORY
;
3491 canon_path_b
= heap_alloc(len_b
*sizeof(WCHAR
));
3493 heap_free(canon_path_a
);
3494 return E_OUTOFMEMORY
;
3497 len_a
= canonicalize_path_hierarchical(a
->canon_uri
+a
->path_start
, a
->path_len
, a
->scheme_type
, FALSE
, 0, FALSE
, canon_path_a
);
3498 len_b
= canonicalize_path_hierarchical(b
->canon_uri
+b
->path_start
, b
->path_len
, b
->scheme_type
, FALSE
, 0, FALSE
, canon_path_b
);
3500 *ret
= len_a
== len_b
&& !wcsnicmp(canon_path_a
, canon_path_b
, len_a
);
3502 heap_free(canon_path_a
);
3503 heap_free(canon_path_b
);
3507 /* Checks if the two Uri's are logically equivalent. It's a simple
3508 * comparison, since they are both of type Uri, and it can access
3509 * the properties of each Uri directly without the need to go
3510 * through the "IUri_Get*" interface calls.
3512 static HRESULT
compare_uris(const Uri
*a
, const Uri
*b
, BOOL
*ret
) {
3513 const BOOL known_scheme
= a
->scheme_type
!= URL_SCHEME_UNKNOWN
;
3514 const BOOL are_hierarchical
= a
->authority_start
> -1 && b
->authority_start
> -1;
3519 if(a
->scheme_type
!= b
->scheme_type
)
3522 /* Only compare the scheme names (if any) if their unknown scheme types. */
3524 if((a
->scheme_start
> -1 && b
->scheme_start
> -1) &&
3525 (a
->scheme_len
== b
->scheme_len
)) {
3526 /* Make sure the schemes are the same. */
3527 if(StrCmpNW(a
->canon_uri
+a
->scheme_start
, b
->canon_uri
+b
->scheme_start
, a
->scheme_len
))
3529 } else if(a
->scheme_len
!= b
->scheme_len
)
3530 /* One of the Uri's has a scheme name, while the other doesn't. */
3534 /* If they have a userinfo component, perform case sensitive compare. */
3535 if((a
->userinfo_start
> -1 && b
->userinfo_start
> -1) &&
3536 (a
->userinfo_len
== b
->userinfo_len
)) {
3537 if(StrCmpNW(a
->canon_uri
+a
->userinfo_start
, b
->canon_uri
+b
->userinfo_start
, a
->userinfo_len
))
3539 } else if(a
->userinfo_len
!= b
->userinfo_len
)
3540 /* One of the Uri's had a userinfo, while the other one doesn't. */
3543 /* Check if they have a host name. */
3544 if((a
->host_start
> -1 && b
->host_start
> -1) &&
3545 (a
->host_len
== b
->host_len
)) {
3546 /* Perform a case insensitive compare if they are a known scheme type. */
3548 if(StrCmpNIW(a
->canon_uri
+a
->host_start
, b
->canon_uri
+b
->host_start
, a
->host_len
))
3550 } else if(StrCmpNW(a
->canon_uri
+a
->host_start
, b
->canon_uri
+b
->host_start
, a
->host_len
))
3552 } else if(a
->host_len
!= b
->host_len
)
3553 /* One of the Uri's had a host, while the other one didn't. */
3556 if(a
->has_port
&& b
->has_port
) {
3557 if(a
->port
!= b
->port
)
3559 } else if(a
->has_port
|| b
->has_port
)
3560 /* One had a port, while the other one didn't. */
3563 /* Windows is weird with how it handles paths. For example
3564 * One URI could be "http://google.com" (after canonicalization)
3565 * and one could be "http://google.com/" and the IsEqual function
3566 * would still evaluate to TRUE, but, only if they are both hierarchical
3569 if(a
->scheme_type
== URL_SCHEME_FILE
) {
3572 hres
= compare_file_paths(a
, b
, &cmp
);
3573 if(FAILED(hres
) || !cmp
)
3575 } else if((a
->path_start
> -1 && b
->path_start
> -1) &&
3576 (a
->path_len
== b
->path_len
)) {
3577 if(StrCmpNW(a
->canon_uri
+a
->path_start
, b
->canon_uri
+b
->path_start
, a
->path_len
))
3579 } else if(are_hierarchical
&& a
->path_len
== -1 && b
->path_len
== 0) {
3580 if(*(a
->canon_uri
+a
->path_start
) != '/')
3582 } else if(are_hierarchical
&& b
->path_len
== 1 && a
->path_len
== 0) {
3583 if(*(b
->canon_uri
+b
->path_start
) != '/')
3585 } else if(a
->path_len
!= b
->path_len
)
3588 /* Compare the query strings of the two URIs. */
3589 if((a
->query_start
> -1 && b
->query_start
> -1) &&
3590 (a
->query_len
== b
->query_len
)) {
3591 if(StrCmpNW(a
->canon_uri
+a
->query_start
, b
->canon_uri
+b
->query_start
, a
->query_len
))
3593 } else if(a
->query_len
!= b
->query_len
)
3596 if((a
->fragment_start
> -1 && b
->fragment_start
> -1) &&
3597 (a
->fragment_len
== b
->fragment_len
)) {
3598 if(StrCmpNW(a
->canon_uri
+a
->fragment_start
, b
->canon_uri
+b
->fragment_start
, a
->fragment_len
))
3600 } else if(a
->fragment_len
!= b
->fragment_len
)
3603 /* If we get here, the two URIs are equivalent. */
3608 static void convert_to_dos_path(const WCHAR
*path
, DWORD path_len
,
3609 WCHAR
*output
, DWORD
*output_len
)
3611 const WCHAR
*ptr
= path
;
3613 if(path_len
> 3 && *ptr
== '/' && is_drive_path(path
+1))
3614 /* Skip over the leading / before the drive path. */
3617 for(; ptr
< path
+path_len
; ++ptr
) {
3630 /* Generates a raw uri string using the parse_data. */
3631 static DWORD
generate_raw_uri(const parse_data
*data
, BSTR uri
, DWORD flags
) {
3636 memcpy(uri
, data
->scheme
, data
->scheme_len
*sizeof(WCHAR
));
3637 uri
[data
->scheme_len
] = ':';
3639 length
+= data
->scheme_len
+1;
3642 if(!data
->is_opaque
) {
3643 /* For the "//" which appears before the authority component. */
3646 uri
[length
+1] = '/';
3650 /* Check if we need to add the "\\" before the host name
3651 * of a UNC server name in a DOS path.
3653 if(flags
& RAW_URI_CONVERT_TO_DOS_PATH
&&
3654 data
->scheme_type
== URL_SCHEME_FILE
&& data
->host
) {
3657 uri
[length
+1] = '\\';
3663 if(data
->username
) {
3665 memcpy(uri
+length
, data
->username
, data
->username_len
*sizeof(WCHAR
));
3666 length
+= data
->username_len
;
3669 if(data
->password
) {
3672 memcpy(uri
+length
+1, data
->password
, data
->password_len
*sizeof(WCHAR
));
3674 length
+= data
->password_len
+1;
3677 if(data
->password
|| data
->username
) {
3684 /* IPv6 addresses get the brackets added around them if they don't already
3687 const BOOL add_brackets
= data
->host_type
== Uri_HOST_IPV6
&& *(data
->host
) != '[';
3695 memcpy(uri
+length
, data
->host
, data
->host_len
*sizeof(WCHAR
));
3696 length
+= data
->host_len
;
3705 if(data
->has_port
) {
3706 /* The port isn't included in the raw uri if it's the default
3707 * port for the scheme type.
3710 BOOL is_default
= FALSE
;
3712 for(i
= 0; i
< ARRAY_SIZE(default_ports
); ++i
) {
3713 if(data
->scheme_type
== default_ports
[i
].scheme
&&
3714 data
->port_value
== default_ports
[i
].port
)
3718 if(!is_default
|| flags
& RAW_URI_FORCE_PORT_DISP
) {
3724 length
+= ui2str(uri
+length
, data
->port_value
);
3726 length
+= ui2str(NULL
, data
->port_value
);
3730 /* Check if a '/' should be added before the path for hierarchical URIs. */
3731 if(!data
->is_opaque
&& data
->path
&& *(data
->path
) != '/') {
3738 if(!data
->is_opaque
&& data
->scheme_type
== URL_SCHEME_FILE
&&
3739 flags
& RAW_URI_CONVERT_TO_DOS_PATH
) {
3743 convert_to_dos_path(data
->path
, data
->path_len
, uri
+length
, &len
);
3745 convert_to_dos_path(data
->path
, data
->path_len
, NULL
, &len
);
3750 memcpy(uri
+length
, data
->path
, data
->path_len
*sizeof(WCHAR
));
3751 length
+= data
->path_len
;
3757 memcpy(uri
+length
, data
->query
, data
->query_len
*sizeof(WCHAR
));
3758 length
+= data
->query_len
;
3761 if(data
->fragment
) {
3763 memcpy(uri
+length
, data
->fragment
, data
->fragment_len
*sizeof(WCHAR
));
3764 length
+= data
->fragment_len
;
3768 TRACE("(%p %p): Generated raw uri=%s len=%d\n", data
, uri
, debugstr_wn(uri
, length
), length
);
3770 TRACE("(%p %p): Computed raw uri len=%d\n", data
, uri
, length
);
3775 static HRESULT
generate_uri(const UriBuilder
*builder
, const parse_data
*data
, Uri
*uri
, DWORD flags
) {
3777 DWORD length
= generate_raw_uri(data
, NULL
, 0);
3778 uri
->raw_uri
= SysAllocStringLen(NULL
, length
);
3780 return E_OUTOFMEMORY
;
3782 generate_raw_uri(data
, uri
->raw_uri
, 0);
3784 hr
= canonicalize_uri(data
, uri
, flags
);
3786 if(hr
== E_INVALIDARG
)
3787 return INET_E_INVALID_URL
;
3791 uri
->create_flags
= flags
;
3795 static inline Uri
* impl_from_IUri(IUri
*iface
)
3797 return CONTAINING_RECORD(iface
, Uri
, IUri_iface
);
3800 static inline void destroy_uri_obj(Uri
*This
)
3802 SysFreeString(This
->raw_uri
);
3803 heap_free(This
->canon_uri
);
3807 static HRESULT WINAPI
Uri_QueryInterface(IUri
*iface
, REFIID riid
, void **ppv
)
3809 Uri
*This
= impl_from_IUri(iface
);
3811 if(IsEqualGUID(&IID_IUnknown
, riid
)) {
3812 TRACE("(%p)->(IID_IUnknown %p)\n", This
, ppv
);
3813 *ppv
= &This
->IUri_iface
;
3814 }else if(IsEqualGUID(&IID_IUri
, riid
)) {
3815 TRACE("(%p)->(IID_IUri %p)\n", This
, ppv
);
3816 *ppv
= &This
->IUri_iface
;
3817 }else if(IsEqualGUID(&IID_IUriBuilderFactory
, riid
)) {
3818 TRACE("(%p)->(IID_IUriBuilderFactory %p)\n", This
, ppv
);
3819 *ppv
= &This
->IUriBuilderFactory_iface
;
3820 }else if(IsEqualGUID(&IID_IPersistStream
, riid
)) {
3821 TRACE("(%p)->(IID_IPersistStream %p)\n", This
, ppv
);
3822 *ppv
= &This
->IPersistStream_iface
;
3823 }else if(IsEqualGUID(&IID_IMarshal
, riid
)) {
3824 TRACE("(%p)->(IID_IMarshal %p)\n", This
, ppv
);
3825 *ppv
= &This
->IMarshal_iface
;
3826 }else if(IsEqualGUID(&IID_IUriObj
, riid
)) {
3827 TRACE("(%p)->(IID_IUriObj %p)\n", This
, ppv
);
3831 TRACE("(%p)->(%s %p)\n", This
, debugstr_guid(riid
), ppv
);
3833 return E_NOINTERFACE
;
3836 IUnknown_AddRef((IUnknown
*)*ppv
);
3840 static ULONG WINAPI
Uri_AddRef(IUri
*iface
)
3842 Uri
*This
= impl_from_IUri(iface
);
3843 LONG ref
= InterlockedIncrement(&This
->ref
);
3845 TRACE("(%p) ref=%d\n", This
, ref
);
3850 static ULONG WINAPI
Uri_Release(IUri
*iface
)
3852 Uri
*This
= impl_from_IUri(iface
);
3853 LONG ref
= InterlockedDecrement(&This
->ref
);
3855 TRACE("(%p) ref=%d\n", This
, ref
);
3858 destroy_uri_obj(This
);
3863 static HRESULT WINAPI
Uri_GetPropertyBSTR(IUri
*iface
, Uri_PROPERTY uriProp
, BSTR
*pbstrProperty
, DWORD dwFlags
)
3865 Uri
*This
= impl_from_IUri(iface
);
3867 TRACE("(%p %s)->(%d %p %x)\n", This
, debugstr_w(This
->canon_uri
), uriProp
, pbstrProperty
, dwFlags
);
3869 if(!This
->create_flags
)
3870 return E_UNEXPECTED
;
3874 if(uriProp
> Uri_PROPERTY_STRING_LAST
) {
3875 /* It only returns S_FALSE for the ZONE property... */
3876 if(uriProp
== Uri_PROPERTY_ZONE
) {
3877 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
3878 if(!(*pbstrProperty
))
3879 return E_OUTOFMEMORY
;
3883 *pbstrProperty
= NULL
;
3884 return E_INVALIDARG
;
3887 /* Don't have support for flags yet. */
3889 FIXME("(%p)->(%d %p %x)\n", This
, uriProp
, pbstrProperty
, dwFlags
);
3894 case Uri_PROPERTY_ABSOLUTE_URI
:
3895 if(This
->display_modifiers
& URI_DISPLAY_NO_ABSOLUTE_URI
) {
3896 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
3899 if(This
->scheme_type
!= URL_SCHEME_UNKNOWN
&& This
->userinfo_start
> -1) {
3900 if(This
->userinfo_len
== 0) {
3901 /* Don't include the '@' after the userinfo component. */
3902 *pbstrProperty
= SysAllocStringLen(NULL
, This
->canon_len
-1);
3904 if(*pbstrProperty
) {
3905 /* Copy everything before it. */
3906 memcpy(*pbstrProperty
, This
->canon_uri
, This
->userinfo_start
*sizeof(WCHAR
));
3908 /* And everything after it. */
3909 memcpy(*pbstrProperty
+This
->userinfo_start
, This
->canon_uri
+This
->userinfo_start
+1,
3910 (This
->canon_len
-This
->userinfo_start
-1)*sizeof(WCHAR
));
3912 } else if(This
->userinfo_split
== 0 && This
->userinfo_len
== 1) {
3913 /* Don't include the ":@" */
3914 *pbstrProperty
= SysAllocStringLen(NULL
, This
->canon_len
-2);
3916 if(*pbstrProperty
) {
3917 memcpy(*pbstrProperty
, This
->canon_uri
, This
->userinfo_start
*sizeof(WCHAR
));
3918 memcpy(*pbstrProperty
+This
->userinfo_start
, This
->canon_uri
+This
->userinfo_start
+2,
3919 (This
->canon_len
-This
->userinfo_start
-2)*sizeof(WCHAR
));
3922 *pbstrProperty
= SysAllocString(This
->canon_uri
);
3926 *pbstrProperty
= SysAllocString(This
->canon_uri
);
3931 if(!(*pbstrProperty
))
3932 hres
= E_OUTOFMEMORY
;
3935 case Uri_PROPERTY_AUTHORITY
:
3936 if(This
->authority_start
> -1) {
3937 if(This
->port_offset
> -1 && is_default_port(This
->scheme_type
, This
->port
) &&
3938 This
->display_modifiers
& URI_DISPLAY_NO_DEFAULT_PORT_AUTH
)
3939 /* Don't include the port in the authority component. */
3940 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+This
->authority_start
, This
->port_offset
);
3942 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+This
->authority_start
, This
->authority_len
);
3945 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
3949 if(!(*pbstrProperty
))
3950 hres
= E_OUTOFMEMORY
;
3953 case Uri_PROPERTY_DISPLAY_URI
:
3954 /* The Display URI contains everything except for the userinfo for known
3957 if(This
->scheme_type
!= URL_SCHEME_UNKNOWN
&& This
->userinfo_start
> -1) {
3958 *pbstrProperty
= SysAllocStringLen(NULL
, This
->canon_len
-This
->userinfo_len
);
3960 if(*pbstrProperty
) {
3961 /* Copy everything before the userinfo over. */
3962 memcpy(*pbstrProperty
, This
->canon_uri
, This
->userinfo_start
*sizeof(WCHAR
));
3963 /* Copy everything after the userinfo over. */
3964 memcpy(*pbstrProperty
+This
->userinfo_start
,
3965 This
->canon_uri
+This
->userinfo_start
+This
->userinfo_len
+1,
3966 (This
->canon_len
-(This
->userinfo_start
+This
->userinfo_len
+1))*sizeof(WCHAR
));
3969 *pbstrProperty
= SysAllocString(This
->canon_uri
);
3971 if(!(*pbstrProperty
))
3972 hres
= E_OUTOFMEMORY
;
3977 case Uri_PROPERTY_DOMAIN
:
3978 if(This
->domain_offset
> -1) {
3979 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+This
->host_start
+This
->domain_offset
,
3980 This
->host_len
-This
->domain_offset
);
3983 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
3987 if(!(*pbstrProperty
))
3988 hres
= E_OUTOFMEMORY
;
3991 case Uri_PROPERTY_EXTENSION
:
3992 if(This
->extension_offset
> -1) {
3993 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+This
->path_start
+This
->extension_offset
,
3994 This
->path_len
-This
->extension_offset
);
3997 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
4001 if(!(*pbstrProperty
))
4002 hres
= E_OUTOFMEMORY
;
4005 case Uri_PROPERTY_FRAGMENT
:
4006 if(This
->fragment_start
> -1) {
4007 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+This
->fragment_start
, This
->fragment_len
);
4010 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
4014 if(!(*pbstrProperty
))
4015 hres
= E_OUTOFMEMORY
;
4018 case Uri_PROPERTY_HOST
:
4019 if(This
->host_start
> -1) {
4020 /* The '[' and ']' aren't included for IPv6 addresses. */
4021 if(This
->host_type
== Uri_HOST_IPV6
)
4022 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+This
->host_start
+1, This
->host_len
-2);
4024 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+This
->host_start
, This
->host_len
);
4028 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
4032 if(!(*pbstrProperty
))
4033 hres
= E_OUTOFMEMORY
;
4036 case Uri_PROPERTY_PASSWORD
:
4037 if(This
->userinfo_split
> -1) {
4038 *pbstrProperty
= SysAllocStringLen(
4039 This
->canon_uri
+This
->userinfo_start
+This
->userinfo_split
+1,
4040 This
->userinfo_len
-This
->userinfo_split
-1);
4043 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
4047 if(!(*pbstrProperty
))
4048 return E_OUTOFMEMORY
;
4051 case Uri_PROPERTY_PATH
:
4052 if(This
->path_start
> -1) {
4053 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+This
->path_start
, This
->path_len
);
4056 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
4060 if(!(*pbstrProperty
))
4061 hres
= E_OUTOFMEMORY
;
4064 case Uri_PROPERTY_PATH_AND_QUERY
:
4065 if(This
->path_start
> -1) {
4066 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+This
->path_start
, This
->path_len
+This
->query_len
);
4068 } else if(This
->query_start
> -1) {
4069 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+This
->query_start
, This
->query_len
);
4072 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
4076 if(!(*pbstrProperty
))
4077 hres
= E_OUTOFMEMORY
;
4080 case Uri_PROPERTY_QUERY
:
4081 if(This
->query_start
> -1) {
4082 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+This
->query_start
, This
->query_len
);
4085 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
4089 if(!(*pbstrProperty
))
4090 hres
= E_OUTOFMEMORY
;
4093 case Uri_PROPERTY_RAW_URI
:
4094 *pbstrProperty
= SysAllocString(This
->raw_uri
);
4095 if(!(*pbstrProperty
))
4096 hres
= E_OUTOFMEMORY
;
4100 case Uri_PROPERTY_SCHEME_NAME
:
4101 if(This
->scheme_start
> -1) {
4102 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+ This
->scheme_start
, This
->scheme_len
);
4105 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
4109 if(!(*pbstrProperty
))
4110 hres
= E_OUTOFMEMORY
;
4113 case Uri_PROPERTY_USER_INFO
:
4114 if(This
->userinfo_start
> -1) {
4115 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+This
->userinfo_start
, This
->userinfo_len
);
4118 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
4122 if(!(*pbstrProperty
))
4123 hres
= E_OUTOFMEMORY
;
4126 case Uri_PROPERTY_USER_NAME
:
4127 if(This
->userinfo_start
> -1 && This
->userinfo_split
!= 0) {
4128 /* If userinfo_split is set, that means a password exists
4129 * so the username is only from userinfo_start to userinfo_split.
4131 if(This
->userinfo_split
> -1) {
4132 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+ This
->userinfo_start
, This
->userinfo_split
);
4135 *pbstrProperty
= SysAllocStringLen(This
->canon_uri
+ This
->userinfo_start
, This
->userinfo_len
);
4139 *pbstrProperty
= SysAllocStringLen(NULL
, 0);
4143 if(!(*pbstrProperty
))
4144 return E_OUTOFMEMORY
;
4148 FIXME("(%p)->(%d %p %x)\n", This
, uriProp
, pbstrProperty
, dwFlags
);
4155 static HRESULT WINAPI
Uri_GetPropertyLength(IUri
*iface
, Uri_PROPERTY uriProp
, DWORD
*pcchProperty
, DWORD dwFlags
)
4157 Uri
*This
= impl_from_IUri(iface
);
4159 TRACE("(%p %s)->(%d %p %x)\n", This
, debugstr_w(This
->canon_uri
), uriProp
, pcchProperty
, dwFlags
);
4161 if(!This
->create_flags
)
4162 return E_UNEXPECTED
;
4164 return E_INVALIDARG
;
4166 /* Can only return a length for a property if it's a string. */
4167 if(uriProp
> Uri_PROPERTY_STRING_LAST
)
4168 return E_INVALIDARG
;
4170 /* Don't have support for flags yet. */
4172 FIXME("(%p)->(%d %p %x)\n", This
, uriProp
, pcchProperty
, dwFlags
);
4177 case Uri_PROPERTY_ABSOLUTE_URI
:
4178 if(This
->display_modifiers
& URI_DISPLAY_NO_ABSOLUTE_URI
) {
4182 if(This
->scheme_type
!= URL_SCHEME_UNKNOWN
) {
4183 if(This
->userinfo_start
> -1 && This
->userinfo_len
== 0)
4184 /* Don't include the '@' in the length. */
4185 *pcchProperty
= This
->canon_len
-1;
4186 else if(This
->userinfo_start
> -1 && This
->userinfo_len
== 1 &&
4187 This
->userinfo_split
== 0)
4188 /* Don't include the ":@" in the length. */
4189 *pcchProperty
= This
->canon_len
-2;
4191 *pcchProperty
= This
->canon_len
;
4193 *pcchProperty
= This
->canon_len
;
4199 case Uri_PROPERTY_AUTHORITY
:
4200 if(This
->port_offset
> -1 &&
4201 This
->display_modifiers
& URI_DISPLAY_NO_DEFAULT_PORT_AUTH
&&
4202 is_default_port(This
->scheme_type
, This
->port
))
4203 /* Only count up until the port in the authority. */
4204 *pcchProperty
= This
->port_offset
;
4206 *pcchProperty
= This
->authority_len
;
4207 hres
= (This
->authority_start
> -1) ? S_OK
: S_FALSE
;
4209 case Uri_PROPERTY_DISPLAY_URI
:
4210 if(This
->scheme_type
!= URL_SCHEME_UNKNOWN
&& This
->userinfo_start
> -1)
4211 *pcchProperty
= This
->canon_len
-This
->userinfo_len
-1;
4213 *pcchProperty
= This
->canon_len
;
4217 case Uri_PROPERTY_DOMAIN
:
4218 if(This
->domain_offset
> -1)
4219 *pcchProperty
= This
->host_len
- This
->domain_offset
;
4223 hres
= (This
->domain_offset
> -1) ? S_OK
: S_FALSE
;
4225 case Uri_PROPERTY_EXTENSION
:
4226 if(This
->extension_offset
> -1) {
4227 *pcchProperty
= This
->path_len
- This
->extension_offset
;
4235 case Uri_PROPERTY_FRAGMENT
:
4236 *pcchProperty
= This
->fragment_len
;
4237 hres
= (This
->fragment_start
> -1) ? S_OK
: S_FALSE
;
4239 case Uri_PROPERTY_HOST
:
4240 *pcchProperty
= This
->host_len
;
4242 /* '[' and ']' aren't included in the length. */
4243 if(This
->host_type
== Uri_HOST_IPV6
)
4246 hres
= (This
->host_start
> -1) ? S_OK
: S_FALSE
;
4248 case Uri_PROPERTY_PASSWORD
:
4249 *pcchProperty
= (This
->userinfo_split
> -1) ? This
->userinfo_len
-This
->userinfo_split
-1 : 0;
4250 hres
= (This
->userinfo_split
> -1) ? S_OK
: S_FALSE
;
4252 case Uri_PROPERTY_PATH
:
4253 *pcchProperty
= This
->path_len
;
4254 hres
= (This
->path_start
> -1) ? S_OK
: S_FALSE
;
4256 case Uri_PROPERTY_PATH_AND_QUERY
:
4257 *pcchProperty
= This
->path_len
+This
->query_len
;
4258 hres
= (This
->path_start
> -1 || This
->query_start
> -1) ? S_OK
: S_FALSE
;
4260 case Uri_PROPERTY_QUERY
:
4261 *pcchProperty
= This
->query_len
;
4262 hres
= (This
->query_start
> -1) ? S_OK
: S_FALSE
;
4264 case Uri_PROPERTY_RAW_URI
:
4265 *pcchProperty
= SysStringLen(This
->raw_uri
);
4268 case Uri_PROPERTY_SCHEME_NAME
:
4269 *pcchProperty
= This
->scheme_len
;
4270 hres
= (This
->scheme_start
> -1) ? S_OK
: S_FALSE
;
4272 case Uri_PROPERTY_USER_INFO
:
4273 *pcchProperty
= This
->userinfo_len
;
4274 hres
= (This
->userinfo_start
> -1) ? S_OK
: S_FALSE
;
4276 case Uri_PROPERTY_USER_NAME
:
4277 *pcchProperty
= (This
->userinfo_split
> -1) ? This
->userinfo_split
: This
->userinfo_len
;
4278 if(This
->userinfo_split
== 0)
4281 hres
= (This
->userinfo_start
> -1) ? S_OK
: S_FALSE
;
4284 FIXME("(%p)->(%d %p %x)\n", This
, uriProp
, pcchProperty
, dwFlags
);
4291 static HRESULT WINAPI
Uri_GetPropertyDWORD(IUri
*iface
, Uri_PROPERTY uriProp
, DWORD
*pcchProperty
, DWORD dwFlags
)
4293 Uri
*This
= impl_from_IUri(iface
);
4296 TRACE("(%p %s)->(%d %p %x)\n", This
, debugstr_w(This
->canon_uri
), uriProp
, pcchProperty
, dwFlags
);
4298 if(!This
->create_flags
)
4299 return E_UNEXPECTED
;
4301 return E_INVALIDARG
;
4303 /* Microsoft's implementation for the ZONE property of a URI seems to be lacking...
4304 * From what I can tell, instead of checking which URLZONE the URI belongs to it
4305 * simply assigns URLZONE_INVALID and returns E_NOTIMPL. This also applies to the GetZone
4308 if(uriProp
== Uri_PROPERTY_ZONE
) {
4309 *pcchProperty
= URLZONE_INVALID
;
4313 if(uriProp
< Uri_PROPERTY_DWORD_START
) {
4315 return E_INVALIDARG
;
4319 case Uri_PROPERTY_HOST_TYPE
:
4320 *pcchProperty
= This
->host_type
;
4323 case Uri_PROPERTY_PORT
:
4324 if(!This
->has_port
) {
4328 *pcchProperty
= This
->port
;
4333 case Uri_PROPERTY_SCHEME
:
4334 *pcchProperty
= This
->scheme_type
;
4338 FIXME("(%p)->(%d %p %x)\n", This
, uriProp
, pcchProperty
, dwFlags
);
4345 static HRESULT WINAPI
Uri_HasProperty(IUri
*iface
, Uri_PROPERTY uriProp
, BOOL
*pfHasProperty
)
4347 Uri
*This
= impl_from_IUri(iface
);
4349 TRACE("(%p %s)->(%d %p)\n", This
, debugstr_w(This
->canon_uri
), uriProp
, pfHasProperty
);
4352 return E_INVALIDARG
;
4355 case Uri_PROPERTY_ABSOLUTE_URI
:
4356 *pfHasProperty
= !(This
->display_modifiers
& URI_DISPLAY_NO_ABSOLUTE_URI
);
4358 case Uri_PROPERTY_AUTHORITY
:
4359 *pfHasProperty
= This
->authority_start
> -1;
4361 case Uri_PROPERTY_DISPLAY_URI
:
4362 *pfHasProperty
= TRUE
;
4364 case Uri_PROPERTY_DOMAIN
:
4365 *pfHasProperty
= This
->domain_offset
> -1;
4367 case Uri_PROPERTY_EXTENSION
:
4368 *pfHasProperty
= This
->extension_offset
> -1;
4370 case Uri_PROPERTY_FRAGMENT
:
4371 *pfHasProperty
= This
->fragment_start
> -1;
4373 case Uri_PROPERTY_HOST
:
4374 *pfHasProperty
= This
->host_start
> -1;
4376 case Uri_PROPERTY_PASSWORD
:
4377 *pfHasProperty
= This
->userinfo_split
> -1;
4379 case Uri_PROPERTY_PATH
:
4380 *pfHasProperty
= This
->path_start
> -1;
4382 case Uri_PROPERTY_PATH_AND_QUERY
:
4383 *pfHasProperty
= (This
->path_start
> -1 || This
->query_start
> -1);
4385 case Uri_PROPERTY_QUERY
:
4386 *pfHasProperty
= This
->query_start
> -1;
4388 case Uri_PROPERTY_RAW_URI
:
4389 *pfHasProperty
= TRUE
;
4391 case Uri_PROPERTY_SCHEME_NAME
:
4392 *pfHasProperty
= This
->scheme_start
> -1;
4394 case Uri_PROPERTY_USER_INFO
:
4395 *pfHasProperty
= This
->userinfo_start
> -1;
4397 case Uri_PROPERTY_USER_NAME
:
4398 if(This
->userinfo_split
== 0)
4399 *pfHasProperty
= FALSE
;
4401 *pfHasProperty
= This
->userinfo_start
> -1;
4403 case Uri_PROPERTY_HOST_TYPE
:
4404 *pfHasProperty
= TRUE
;
4406 case Uri_PROPERTY_PORT
:
4407 *pfHasProperty
= This
->has_port
;
4409 case Uri_PROPERTY_SCHEME
:
4410 *pfHasProperty
= TRUE
;
4412 case Uri_PROPERTY_ZONE
:
4413 *pfHasProperty
= FALSE
;
4416 FIXME("(%p)->(%d %p): Unsupported property type.\n", This
, uriProp
, pfHasProperty
);
4423 static HRESULT WINAPI
Uri_GetAbsoluteUri(IUri
*iface
, BSTR
*pstrAbsoluteUri
)
4425 TRACE("(%p)->(%p)\n", iface
, pstrAbsoluteUri
);
4426 return IUri_GetPropertyBSTR(iface
, Uri_PROPERTY_ABSOLUTE_URI
, pstrAbsoluteUri
, 0);
4429 static HRESULT WINAPI
Uri_GetAuthority(IUri
*iface
, BSTR
*pstrAuthority
)
4431 TRACE("(%p)->(%p)\n", iface
, pstrAuthority
);
4432 return IUri_GetPropertyBSTR(iface
, Uri_PROPERTY_AUTHORITY
, pstrAuthority
, 0);
4435 static HRESULT WINAPI
Uri_GetDisplayUri(IUri
*iface
, BSTR
*pstrDisplayUri
)
4437 TRACE("(%p)->(%p)\n", iface
, pstrDisplayUri
);
4438 return IUri_GetPropertyBSTR(iface
, Uri_PROPERTY_DISPLAY_URI
, pstrDisplayUri
, 0);
4441 static HRESULT WINAPI
Uri_GetDomain(IUri
*iface
, BSTR
*pstrDomain
)
4443 TRACE("(%p)->(%p)\n", iface
, pstrDomain
);
4444 return IUri_GetPropertyBSTR(iface
, Uri_PROPERTY_DOMAIN
, pstrDomain
, 0);
4447 static HRESULT WINAPI
Uri_GetExtension(IUri
*iface
, BSTR
*pstrExtension
)
4449 TRACE("(%p)->(%p)\n", iface
, pstrExtension
);
4450 return IUri_GetPropertyBSTR(iface
, Uri_PROPERTY_EXTENSION
, pstrExtension
, 0);
4453 static HRESULT WINAPI
Uri_GetFragment(IUri
*iface
, BSTR
*pstrFragment
)
4455 TRACE("(%p)->(%p)\n", iface
, pstrFragment
);
4456 return IUri_GetPropertyBSTR(iface
, Uri_PROPERTY_FRAGMENT
, pstrFragment
, 0);
4459 static HRESULT WINAPI
Uri_GetHost(IUri
*iface
, BSTR
*pstrHost
)
4461 TRACE("(%p)->(%p)\n", iface
, pstrHost
);
4462 return IUri_GetPropertyBSTR(iface
, Uri_PROPERTY_HOST
, pstrHost
, 0);
4465 static HRESULT WINAPI
Uri_GetPassword(IUri
*iface
, BSTR
*pstrPassword
)
4467 TRACE("(%p)->(%p)\n", iface
, pstrPassword
);
4468 return IUri_GetPropertyBSTR(iface
, Uri_PROPERTY_PASSWORD
, pstrPassword
, 0);
4471 static HRESULT WINAPI
Uri_GetPath(IUri
*iface
, BSTR
*pstrPath
)
4473 TRACE("(%p)->(%p)\n", iface
, pstrPath
);
4474 return IUri_GetPropertyBSTR(iface
, Uri_PROPERTY_PATH
, pstrPath
, 0);
4477 static HRESULT WINAPI
Uri_GetPathAndQuery(IUri
*iface
, BSTR
*pstrPathAndQuery
)
4479 TRACE("(%p)->(%p)\n", iface
, pstrPathAndQuery
);
4480 return IUri_GetPropertyBSTR(iface
, Uri_PROPERTY_PATH_AND_QUERY
, pstrPathAndQuery
, 0);
4483 static HRESULT WINAPI
Uri_GetQuery(IUri
*iface
, BSTR
*pstrQuery
)
4485 TRACE("(%p)->(%p)\n", iface
, pstrQuery
);
4486 return IUri_GetPropertyBSTR(iface
, Uri_PROPERTY_QUERY
, pstrQuery
, 0);
4489 static HRESULT WINAPI
Uri_GetRawUri(IUri
*iface
, BSTR
*pstrRawUri
)
4491 TRACE("(%p)->(%p)\n", iface
, pstrRawUri
);
4492 return IUri_GetPropertyBSTR(iface
, Uri_PROPERTY_RAW_URI
, pstrRawUri
, 0);
4495 static HRESULT WINAPI
Uri_GetSchemeName(IUri
*iface
, BSTR
*pstrSchemeName
)
4497 TRACE("(%p)->(%p)\n", iface
, pstrSchemeName
);
4498 return IUri_GetPropertyBSTR(iface
, Uri_PROPERTY_SCHEME_NAME
, pstrSchemeName
, 0);
4501 static HRESULT WINAPI
Uri_GetUserInfo(IUri
*iface
, BSTR
*pstrUserInfo
)
4503 TRACE("(%p)->(%p)\n", iface
, pstrUserInfo
);
4504 return IUri_GetPropertyBSTR(iface
, Uri_PROPERTY_USER_INFO
, pstrUserInfo
, 0);
4507 static HRESULT WINAPI
Uri_GetUserName(IUri
*iface
, BSTR
*pstrUserName
)
4509 TRACE("(%p)->(%p)\n", iface
, pstrUserName
);
4510 return IUri_GetPropertyBSTR(iface
, Uri_PROPERTY_USER_NAME
, pstrUserName
, 0);
4513 static HRESULT WINAPI
Uri_GetHostType(IUri
*iface
, DWORD
*pdwHostType
)
4515 TRACE("(%p)->(%p)\n", iface
, pdwHostType
);
4516 return IUri_GetPropertyDWORD(iface
, Uri_PROPERTY_HOST_TYPE
, pdwHostType
, 0);
4519 static HRESULT WINAPI
Uri_GetPort(IUri
*iface
, DWORD
*pdwPort
)
4521 TRACE("(%p)->(%p)\n", iface
, pdwPort
);
4522 return IUri_GetPropertyDWORD(iface
, Uri_PROPERTY_PORT
, pdwPort
, 0);
4525 static HRESULT WINAPI
Uri_GetScheme(IUri
*iface
, DWORD
*pdwScheme
)
4527 TRACE("(%p)->(%p)\n", iface
, pdwScheme
);
4528 return IUri_GetPropertyDWORD(iface
, Uri_PROPERTY_SCHEME
, pdwScheme
, 0);
4531 static HRESULT WINAPI
Uri_GetZone(IUri
*iface
, DWORD
*pdwZone
)
4533 TRACE("(%p)->(%p)\n", iface
, pdwZone
);
4534 return IUri_GetPropertyDWORD(iface
, Uri_PROPERTY_ZONE
,pdwZone
, 0);
4537 static HRESULT WINAPI
Uri_GetProperties(IUri
*iface
, DWORD
*pdwProperties
)
4539 Uri
*This
= impl_from_IUri(iface
);
4540 TRACE("(%p %s)->(%p)\n", This
, debugstr_w(This
->canon_uri
), pdwProperties
);
4542 if(!This
->create_flags
)
4543 return E_UNEXPECTED
;
4545 return E_INVALIDARG
;
4547 /* All URIs have these. */
4548 *pdwProperties
= Uri_HAS_DISPLAY_URI
|Uri_HAS_RAW_URI
|Uri_HAS_SCHEME
|Uri_HAS_HOST_TYPE
;
4550 if(!(This
->display_modifiers
& URI_DISPLAY_NO_ABSOLUTE_URI
))
4551 *pdwProperties
|= Uri_HAS_ABSOLUTE_URI
;
4553 if(This
->scheme_start
> -1)
4554 *pdwProperties
|= Uri_HAS_SCHEME_NAME
;
4556 if(This
->authority_start
> -1) {
4557 *pdwProperties
|= Uri_HAS_AUTHORITY
;
4558 if(This
->userinfo_start
> -1) {
4559 *pdwProperties
|= Uri_HAS_USER_INFO
;
4560 if(This
->userinfo_split
!= 0)
4561 *pdwProperties
|= Uri_HAS_USER_NAME
;
4563 if(This
->userinfo_split
> -1)
4564 *pdwProperties
|= Uri_HAS_PASSWORD
;
4565 if(This
->host_start
> -1)
4566 *pdwProperties
|= Uri_HAS_HOST
;
4567 if(This
->domain_offset
> -1)
4568 *pdwProperties
|= Uri_HAS_DOMAIN
;
4572 *pdwProperties
|= Uri_HAS_PORT
;
4573 if(This
->path_start
> -1)
4574 *pdwProperties
|= Uri_HAS_PATH
|Uri_HAS_PATH_AND_QUERY
;
4575 if(This
->query_start
> -1)
4576 *pdwProperties
|= Uri_HAS_QUERY
|Uri_HAS_PATH_AND_QUERY
;
4578 if(This
->extension_offset
> -1)
4579 *pdwProperties
|= Uri_HAS_EXTENSION
;
4581 if(This
->fragment_start
> -1)
4582 *pdwProperties
|= Uri_HAS_FRAGMENT
;
4587 static HRESULT WINAPI
Uri_IsEqual(IUri
*iface
, IUri
*pUri
, BOOL
*pfEqual
)
4589 Uri
*This
= impl_from_IUri(iface
);
4592 TRACE("(%p %s)->(%p %p)\n", This
, debugstr_w(This
->canon_uri
), pUri
, pfEqual
);
4594 if(!This
->create_flags
)
4595 return E_UNEXPECTED
;
4602 /* For some reason Windows returns S_OK here... */
4606 /* Try to convert it to a Uri (allows for a more simple comparison). */
4607 if(!(other
= get_uri_obj(pUri
))) {
4608 FIXME("(%p)->(%p %p) No support for unknown IUri's yet.\n", iface
, pUri
, pfEqual
);
4612 TRACE("comparing to %s\n", debugstr_w(other
->canon_uri
));
4613 return compare_uris(This
, other
, pfEqual
);
4616 static const IUriVtbl UriVtbl
= {
4620 Uri_GetPropertyBSTR
,
4621 Uri_GetPropertyLength
,
4622 Uri_GetPropertyDWORD
,
4633 Uri_GetPathAndQuery
,
4647 static inline Uri
* impl_from_IUriBuilderFactory(IUriBuilderFactory
*iface
)
4649 return CONTAINING_RECORD(iface
, Uri
, IUriBuilderFactory_iface
);
4652 static HRESULT WINAPI
UriBuilderFactory_QueryInterface(IUriBuilderFactory
*iface
, REFIID riid
, void **ppv
)
4654 Uri
*This
= impl_from_IUriBuilderFactory(iface
);
4655 return IUri_QueryInterface(&This
->IUri_iface
, riid
, ppv
);
4658 static ULONG WINAPI
UriBuilderFactory_AddRef(IUriBuilderFactory
*iface
)
4660 Uri
*This
= impl_from_IUriBuilderFactory(iface
);
4661 return IUri_AddRef(&This
->IUri_iface
);
4664 static ULONG WINAPI
UriBuilderFactory_Release(IUriBuilderFactory
*iface
)
4666 Uri
*This
= impl_from_IUriBuilderFactory(iface
);
4667 return IUri_Release(&This
->IUri_iface
);
4670 static HRESULT WINAPI
UriBuilderFactory_CreateIUriBuilder(IUriBuilderFactory
*iface
,
4672 DWORD_PTR dwReserved
,
4673 IUriBuilder
**ppIUriBuilder
)
4675 Uri
*This
= impl_from_IUriBuilderFactory(iface
);
4676 TRACE("(%p)->(%08x %08x %p)\n", This
, dwFlags
, (DWORD
)dwReserved
, ppIUriBuilder
);
4681 if(dwFlags
|| dwReserved
) {
4682 *ppIUriBuilder
= NULL
;
4683 return E_INVALIDARG
;
4686 return CreateIUriBuilder(NULL
, 0, 0, ppIUriBuilder
);
4689 static HRESULT WINAPI
UriBuilderFactory_CreateInitializedIUriBuilder(IUriBuilderFactory
*iface
,
4691 DWORD_PTR dwReserved
,
4692 IUriBuilder
**ppIUriBuilder
)
4694 Uri
*This
= impl_from_IUriBuilderFactory(iface
);
4695 TRACE("(%p)->(%08x %08x %p)\n", This
, dwFlags
, (DWORD
)dwReserved
, ppIUriBuilder
);
4700 if(dwFlags
|| dwReserved
) {
4701 *ppIUriBuilder
= NULL
;
4702 return E_INVALIDARG
;
4705 return CreateIUriBuilder(&This
->IUri_iface
, 0, 0, ppIUriBuilder
);
4708 static const IUriBuilderFactoryVtbl UriBuilderFactoryVtbl
= {
4709 UriBuilderFactory_QueryInterface
,
4710 UriBuilderFactory_AddRef
,
4711 UriBuilderFactory_Release
,
4712 UriBuilderFactory_CreateIUriBuilder
,
4713 UriBuilderFactory_CreateInitializedIUriBuilder
4716 static inline Uri
* impl_from_IPersistStream(IPersistStream
*iface
)
4718 return CONTAINING_RECORD(iface
, Uri
, IPersistStream_iface
);
4721 static HRESULT WINAPI
PersistStream_QueryInterface(IPersistStream
*iface
, REFIID riid
, void **ppvObject
)
4723 Uri
*This
= impl_from_IPersistStream(iface
);
4724 return IUri_QueryInterface(&This
->IUri_iface
, riid
, ppvObject
);
4727 static ULONG WINAPI
PersistStream_AddRef(IPersistStream
*iface
)
4729 Uri
*This
= impl_from_IPersistStream(iface
);
4730 return IUri_AddRef(&This
->IUri_iface
);
4733 static ULONG WINAPI
PersistStream_Release(IPersistStream
*iface
)
4735 Uri
*This
= impl_from_IPersistStream(iface
);
4736 return IUri_Release(&This
->IUri_iface
);
4739 static HRESULT WINAPI
PersistStream_GetClassID(IPersistStream
*iface
, CLSID
*pClassID
)
4741 Uri
*This
= impl_from_IPersistStream(iface
);
4742 TRACE("(%p)->(%p)\n", This
, pClassID
);
4745 return E_INVALIDARG
;
4747 *pClassID
= CLSID_CUri
;
4751 static HRESULT WINAPI
PersistStream_IsDirty(IPersistStream
*iface
)
4753 Uri
*This
= impl_from_IPersistStream(iface
);
4754 TRACE("(%p)\n", This
);
4758 struct persist_uri
{
4767 static HRESULT WINAPI
PersistStream_Load(IPersistStream
*iface
, IStream
*pStm
)
4769 Uri
*This
= impl_from_IPersistStream(iface
);
4770 struct persist_uri
*data
;
4775 TRACE("(%p)->(%p)\n", This
, pStm
);
4777 if(This
->create_flags
)
4778 return E_UNEXPECTED
;
4780 return E_INVALIDARG
;
4782 hr
= IStream_Read(pStm
, &size
, sizeof(DWORD
), NULL
);
4785 data
= heap_alloc(size
);
4787 return E_OUTOFMEMORY
;
4788 hr
= IStream_Read(pStm
, data
->unk1
, size
-sizeof(DWORD
)-2, NULL
);
4794 if(size
< sizeof(struct persist_uri
)) {
4799 if(*(DWORD
*)data
->data
!= Uri_PROPERTY_RAW_URI
) {
4801 ERR("Can't find raw_uri\n");
4802 return E_UNEXPECTED
;
4805 This
->raw_uri
= SysAllocString((WCHAR
*)(data
->data
+sizeof(DWORD
)*2));
4806 if(!This
->raw_uri
) {
4808 return E_OUTOFMEMORY
;
4810 This
->create_flags
= data
->create_flags
;
4812 TRACE("%x %s\n", This
->create_flags
, debugstr_w(This
->raw_uri
));
4814 memset(&parse
, 0, sizeof(parse_data
));
4815 parse
.uri
= This
->raw_uri
;
4816 if(!parse_uri(&parse
, This
->create_flags
)) {
4817 SysFreeString(This
->raw_uri
);
4818 This
->create_flags
= 0;
4819 return E_UNEXPECTED
;
4822 hr
= canonicalize_uri(&parse
, This
, This
->create_flags
);
4824 SysFreeString(This
->raw_uri
);
4825 This
->create_flags
= 0;
4832 static inline BYTE
* persist_stream_add_strprop(Uri
*This
, BYTE
*p
, DWORD type
, DWORD len
, WCHAR
*data
)
4834 len
*= sizeof(WCHAR
);
4837 *(DWORD
*)p
= len
+sizeof(WCHAR
);
4839 memcpy(p
, data
, len
);
4842 return p
+sizeof(WCHAR
);
4845 static inline void persist_stream_save(Uri
*This
, IStream
*pStm
, BOOL marshal
, struct persist_uri
*data
)
4849 data
->create_flags
= This
->create_flags
;
4851 if(This
->create_flags
) {
4852 data
->fields_no
= 1;
4853 p
= persist_stream_add_strprop(This
, data
->data
, Uri_PROPERTY_RAW_URI
,
4854 SysStringLen(This
->raw_uri
), This
->raw_uri
);
4856 if(This
->scheme_type
!=URL_SCHEME_HTTP
&& This
->scheme_type
!=URL_SCHEME_HTTPS
4857 && This
->scheme_type
!=URL_SCHEME_FTP
)
4860 if(This
->fragment_len
) {
4862 p
= persist_stream_add_strprop(This
, p
, Uri_PROPERTY_FRAGMENT
,
4863 This
->fragment_len
, This
->canon_uri
+This
->fragment_start
);
4866 if(This
->host_len
) {
4868 if(This
->host_type
== Uri_HOST_IPV6
)
4869 p
= persist_stream_add_strprop(This
, p
, Uri_PROPERTY_HOST
,
4870 This
->host_len
-2, This
->canon_uri
+This
->host_start
+1);
4872 p
= persist_stream_add_strprop(This
, p
, Uri_PROPERTY_HOST
,
4873 This
->host_len
, This
->canon_uri
+This
->host_start
);
4876 if(This
->userinfo_split
> -1) {
4878 p
= persist_stream_add_strprop(This
, p
, Uri_PROPERTY_PASSWORD
,
4879 This
->userinfo_len
-This
->userinfo_split
-1,
4880 This
->canon_uri
+This
->userinfo_start
+This
->userinfo_split
+1);
4883 if(This
->path_len
) {
4885 p
= persist_stream_add_strprop(This
, p
, Uri_PROPERTY_PATH
,
4886 This
->path_len
, This
->canon_uri
+This
->path_start
);
4887 } else if(marshal
) {
4888 WCHAR no_path
= '/';
4890 p
= persist_stream_add_strprop(This
, p
, Uri_PROPERTY_PATH
, 1, &no_path
);
4893 if(This
->has_port
) {
4895 *(DWORD
*)p
= Uri_PROPERTY_PORT
;
4897 *(DWORD
*)p
= sizeof(DWORD
);
4899 *(DWORD
*)p
= This
->port
;
4903 if(This
->query_len
) {
4905 p
= persist_stream_add_strprop(This
, p
, Uri_PROPERTY_QUERY
,
4906 This
->query_len
, This
->canon_uri
+This
->query_start
);
4909 if(This
->scheme_len
) {
4911 p
= persist_stream_add_strprop(This
, p
, Uri_PROPERTY_SCHEME_NAME
,
4912 This
->scheme_len
, This
->canon_uri
+This
->scheme_start
);
4915 if(This
->userinfo_start
>-1 && This
->userinfo_split
!=0) {
4917 if(This
->userinfo_split
> -1)
4918 p
= persist_stream_add_strprop(This
, p
, Uri_PROPERTY_USER_NAME
,
4919 This
->userinfo_split
, This
->canon_uri
+This
->userinfo_start
);
4921 p
= persist_stream_add_strprop(This
, p
, Uri_PROPERTY_USER_NAME
,
4922 This
->userinfo_len
, This
->canon_uri
+This
->userinfo_start
);
4926 static HRESULT WINAPI
PersistStream_Save(IPersistStream
*iface
, IStream
*pStm
, BOOL fClearDirty
)
4928 Uri
*This
= impl_from_IPersistStream(iface
);
4929 struct persist_uri
*data
;
4930 ULARGE_INTEGER size
;
4933 TRACE("(%p)->(%p %x)\n", This
, pStm
, fClearDirty
);
4936 return E_INVALIDARG
;
4938 hres
= IPersistStream_GetSizeMax(&This
->IPersistStream_iface
, &size
);
4942 data
= heap_alloc_zero(size
.u
.LowPart
);
4944 return E_OUTOFMEMORY
;
4945 data
->size
= size
.u
.LowPart
;
4946 persist_stream_save(This
, pStm
, FALSE
, data
);
4948 hres
= IStream_Write(pStm
, data
, data
->size
-2, NULL
);
4953 static HRESULT WINAPI
PersistStream_GetSizeMax(IPersistStream
*iface
, ULARGE_INTEGER
*pcbSize
)
4955 Uri
*This
= impl_from_IPersistStream(iface
);
4956 TRACE("(%p)->(%p)\n", This
, pcbSize
);
4959 return E_INVALIDARG
;
4961 pcbSize
->u
.LowPart
= 2+sizeof(struct persist_uri
);
4962 pcbSize
->u
.HighPart
= 0;
4963 if(This
->create_flags
)
4964 pcbSize
->u
.LowPart
+= (SysStringLen(This
->raw_uri
)+1)*sizeof(WCHAR
) + 2*sizeof(DWORD
);
4965 else /* there's no place for fields no */
4966 pcbSize
->u
.LowPart
-= sizeof(DWORD
);
4967 if(This
->scheme_type
!=URL_SCHEME_HTTP
&& This
->scheme_type
!=URL_SCHEME_HTTPS
4968 && This
->scheme_type
!=URL_SCHEME_FTP
)
4971 if(This
->fragment_len
)
4972 pcbSize
->u
.LowPart
+= (This
->fragment_len
+1)*sizeof(WCHAR
) + 2*sizeof(DWORD
);
4973 if(This
->host_len
) {
4974 if(This
->host_type
== Uri_HOST_IPV6
)
4975 pcbSize
->u
.LowPart
+= (This
->host_len
-1)*sizeof(WCHAR
) + 2*sizeof(DWORD
);
4977 pcbSize
->u
.LowPart
+= (This
->host_len
+1)*sizeof(WCHAR
) + 2*sizeof(DWORD
);
4979 if(This
->userinfo_split
> -1)
4980 pcbSize
->u
.LowPart
+= (This
->userinfo_len
-This
->userinfo_split
)*sizeof(WCHAR
) + 2*sizeof(DWORD
);
4982 pcbSize
->u
.LowPart
+= (This
->path_len
+1)*sizeof(WCHAR
) + 2*sizeof(DWORD
);
4984 pcbSize
->u
.LowPart
+= 3*sizeof(DWORD
);
4986 pcbSize
->u
.LowPart
+= (This
->query_len
+1)*sizeof(WCHAR
) + 2*sizeof(DWORD
);
4987 if(This
->scheme_len
)
4988 pcbSize
->u
.LowPart
+= (This
->scheme_len
+1)*sizeof(WCHAR
) + 2*sizeof(DWORD
);
4989 if(This
->userinfo_start
>-1 && This
->userinfo_split
!=0) {
4990 if(This
->userinfo_split
> -1)
4991 pcbSize
->u
.LowPart
+= (This
->userinfo_split
+1)*sizeof(WCHAR
) + 2*sizeof(DWORD
);
4993 pcbSize
->u
.LowPart
+= (This
->userinfo_len
+1)*sizeof(WCHAR
) + 2*sizeof(DWORD
);
4998 static const IPersistStreamVtbl PersistStreamVtbl
= {
4999 PersistStream_QueryInterface
,
5000 PersistStream_AddRef
,
5001 PersistStream_Release
,
5002 PersistStream_GetClassID
,
5003 PersistStream_IsDirty
,
5006 PersistStream_GetSizeMax
5009 static inline Uri
* impl_from_IMarshal(IMarshal
*iface
)
5011 return CONTAINING_RECORD(iface
, Uri
, IMarshal_iface
);
5014 static HRESULT WINAPI
Marshal_QueryInterface(IMarshal
*iface
, REFIID riid
, void **ppvObject
)
5016 Uri
*This
= impl_from_IMarshal(iface
);
5017 return IUri_QueryInterface(&This
->IUri_iface
, riid
, ppvObject
);
5020 static ULONG WINAPI
Marshal_AddRef(IMarshal
*iface
)
5022 Uri
*This
= impl_from_IMarshal(iface
);
5023 return IUri_AddRef(&This
->IUri_iface
);
5026 static ULONG WINAPI
Marshal_Release(IMarshal
*iface
)
5028 Uri
*This
= impl_from_IMarshal(iface
);
5029 return IUri_Release(&This
->IUri_iface
);
5032 static HRESULT WINAPI
Marshal_GetUnmarshalClass(IMarshal
*iface
, REFIID riid
, void *pv
,
5033 DWORD dwDestContext
, void *pvDestContext
, DWORD mshlflags
, CLSID
*pCid
)
5035 Uri
*This
= impl_from_IMarshal(iface
);
5036 TRACE("(%p)->(%s %p %x %p %x %p)\n", This
, debugstr_guid(riid
), pv
,
5037 dwDestContext
, pvDestContext
, mshlflags
, pCid
);
5039 if(!pCid
|| (dwDestContext
!=MSHCTX_LOCAL
&& dwDestContext
!=MSHCTX_NOSHAREDMEM
5040 && dwDestContext
!=MSHCTX_INPROC
))
5041 return E_INVALIDARG
;
5047 struct inproc_marshal_uri
{
5050 DWORD unk
[4]; /* process identifier? */
5054 static HRESULT WINAPI
Marshal_GetMarshalSizeMax(IMarshal
*iface
, REFIID riid
, void *pv
,
5055 DWORD dwDestContext
, void *pvDestContext
, DWORD mshlflags
, DWORD
*pSize
)
5057 Uri
*This
= impl_from_IMarshal(iface
);
5058 ULARGE_INTEGER size
;
5060 TRACE("(%p)->(%s %p %x %p %x %p)\n", This
, debugstr_guid(riid
), pv
,
5061 dwDestContext
, pvDestContext
, mshlflags
, pSize
);
5063 if(!pSize
|| (dwDestContext
!=MSHCTX_LOCAL
&& dwDestContext
!=MSHCTX_NOSHAREDMEM
5064 && dwDestContext
!=MSHCTX_INPROC
))
5065 return E_INVALIDARG
;
5067 if(dwDestContext
== MSHCTX_INPROC
) {
5068 *pSize
= sizeof(struct inproc_marshal_uri
);
5072 hres
= IPersistStream_GetSizeMax(&This
->IPersistStream_iface
, &size
);
5075 if(!This
->path_len
&& (This
->scheme_type
==URL_SCHEME_HTTP
5076 || This
->scheme_type
==URL_SCHEME_HTTPS
5077 || This
->scheme_type
==URL_SCHEME_FTP
))
5078 size
.u
.LowPart
+= 3*sizeof(DWORD
);
5079 *pSize
= size
.u
.LowPart
+2*sizeof(DWORD
);
5083 static HRESULT WINAPI
Marshal_MarshalInterface(IMarshal
*iface
, IStream
*pStm
, REFIID riid
,
5084 void *pv
, DWORD dwDestContext
, void *pvDestContext
, DWORD mshlflags
)
5086 Uri
*This
= impl_from_IMarshal(iface
);
5091 TRACE("(%p)->(%p %s %p %x %p %x)\n", This
, pStm
, debugstr_guid(riid
), pv
,
5092 dwDestContext
, pvDestContext
, mshlflags
);
5094 if(!pStm
|| mshlflags
!=MSHLFLAGS_NORMAL
|| (dwDestContext
!=MSHCTX_LOCAL
5095 && dwDestContext
!=MSHCTX_NOSHAREDMEM
&& dwDestContext
!=MSHCTX_INPROC
))
5096 return E_INVALIDARG
;
5098 if(dwDestContext
== MSHCTX_INPROC
) {
5099 struct inproc_marshal_uri data
;
5101 data
.size
= sizeof(data
);
5102 data
.mshlflags
= MSHCTX_INPROC
;
5109 hres
= IStream_Write(pStm
, &data
, data
.size
, NULL
);
5113 IUri_AddRef(&This
->IUri_iface
);
5117 hres
= IMarshal_GetMarshalSizeMax(iface
, riid
, pv
, dwDestContext
,
5118 pvDestContext
, mshlflags
, &size
);
5122 data
= heap_alloc_zero(size
);
5124 return E_OUTOFMEMORY
;
5127 data
[1] = dwDestContext
;
5128 data
[2] = size
-2*sizeof(DWORD
);
5129 persist_stream_save(This
, pStm
, TRUE
, (struct persist_uri
*)(data
+2));
5131 hres
= IStream_Write(pStm
, data
, data
[0]-2, NULL
);
5136 static HRESULT WINAPI
Marshal_UnmarshalInterface(IMarshal
*iface
,
5137 IStream
*pStm
, REFIID riid
, void **ppv
)
5139 Uri
*This
= impl_from_IMarshal(iface
);
5143 TRACE("(%p)->(%p %s %p)\n", This
, pStm
, debugstr_guid(riid
), ppv
);
5145 if(This
->create_flags
)
5146 return E_UNEXPECTED
;
5147 if(!pStm
|| !riid
|| !ppv
)
5148 return E_INVALIDARG
;
5150 hres
= IStream_Read(pStm
, header
, sizeof(header
), NULL
);
5154 if(header
[1]!=MSHCTX_LOCAL
&& header
[1]!=MSHCTX_NOSHAREDMEM
5155 && header
[1]!=MSHCTX_INPROC
)
5156 return E_UNEXPECTED
;
5158 if(header
[1] == MSHCTX_INPROC
) {
5159 struct inproc_marshal_uri data
;
5162 hres
= IStream_Read(pStm
, data
.unk
, sizeof(data
)-2*sizeof(DWORD
), NULL
);
5166 This
->raw_uri
= SysAllocString(data
.uri
->raw_uri
);
5167 if(!This
->raw_uri
) {
5168 return E_OUTOFMEMORY
;
5171 memset(&parse
, 0, sizeof(parse_data
));
5172 parse
.uri
= This
->raw_uri
;
5174 if(!parse_uri(&parse
, data
.uri
->create_flags
))
5175 return E_INVALIDARG
;
5177 hres
= canonicalize_uri(&parse
, This
, data
.uri
->create_flags
);
5181 This
->create_flags
= data
.uri
->create_flags
;
5182 IUri_Release(&data
.uri
->IUri_iface
);
5184 return IUri_QueryInterface(&This
->IUri_iface
, riid
, ppv
);
5187 hres
= IPersistStream_Load(&This
->IPersistStream_iface
, pStm
);
5191 return IUri_QueryInterface(&This
->IUri_iface
, riid
, ppv
);
5194 static HRESULT WINAPI
Marshal_ReleaseMarshalData(IMarshal
*iface
, IStream
*pStm
)
5196 Uri
*This
= impl_from_IMarshal(iface
);
5201 TRACE("(%p)->(%p)\n", This
, pStm
);
5204 return E_INVALIDARG
;
5206 hres
= IStream_Read(pStm
, header
, 2*sizeof(DWORD
), NULL
);
5210 if(header
[1] == MSHCTX_INPROC
) {
5211 struct inproc_marshal_uri data
;
5213 hres
= IStream_Read(pStm
, data
.unk
, sizeof(data
)-2*sizeof(DWORD
), NULL
);
5217 IUri_Release(&data
.uri
->IUri_iface
);
5221 off
.u
.LowPart
= header
[0]-sizeof(header
)-2;
5223 return IStream_Seek(pStm
, off
, STREAM_SEEK_CUR
, NULL
);
5226 static HRESULT WINAPI
Marshal_DisconnectObject(IMarshal
*iface
, DWORD dwReserved
)
5228 Uri
*This
= impl_from_IMarshal(iface
);
5229 TRACE("(%p)->(%x)\n", This
, dwReserved
);
5233 static const IMarshalVtbl MarshalVtbl
= {
5234 Marshal_QueryInterface
,
5237 Marshal_GetUnmarshalClass
,
5238 Marshal_GetMarshalSizeMax
,
5239 Marshal_MarshalInterface
,
5240 Marshal_UnmarshalInterface
,
5241 Marshal_ReleaseMarshalData
,
5242 Marshal_DisconnectObject
5245 HRESULT
Uri_Construct(IUnknown
*pUnkOuter
, LPVOID
*ppobj
)
5247 Uri
*ret
= heap_alloc_zero(sizeof(Uri
));
5249 TRACE("(%p %p)\n", pUnkOuter
, ppobj
);
5253 return E_OUTOFMEMORY
;
5255 ret
->IUri_iface
.lpVtbl
= &UriVtbl
;
5256 ret
->IUriBuilderFactory_iface
.lpVtbl
= &UriBuilderFactoryVtbl
;
5257 ret
->IPersistStream_iface
.lpVtbl
= &PersistStreamVtbl
;
5258 ret
->IMarshal_iface
.lpVtbl
= &MarshalVtbl
;
5261 *ppobj
= &ret
->IUri_iface
;
5265 /***********************************************************************
5266 * CreateUri (urlmon.@)
5268 * Creates a new IUri object using the URI represented by pwzURI. This function
5269 * parses and validates the components of pwzURI and then canonicalizes the
5270 * parsed components.
5273 * pwzURI [I] The URI to parse, validate, and canonicalize.
5274 * dwFlags [I] Flags which can affect how the parsing/canonicalization is performed.
5275 * dwReserved [I] Reserved (not used).
5276 * ppURI [O] The resulting IUri after parsing/canonicalization occurs.
5279 * Success: Returns S_OK. ppURI contains the pointer to the newly allocated IUri.
5280 * Failure: E_INVALIDARG if there are invalid flag combinations in dwFlags, or an
5281 * invalid parameter, or pwzURI doesn't represent a valid URI.
5282 * E_OUTOFMEMORY if any memory allocation fails.
5286 * Uri_CREATE_CANONICALIZE, Uri_CREATE_DECODE_EXTRA_INFO, Uri_CREATE_CRACK_UNKNOWN_SCHEMES,
5287 * Uri_CREATE_PRE_PROCESS_HTML_URI, Uri_CREATE_NO_IE_SETTINGS.
5289 HRESULT WINAPI
CreateUri(LPCWSTR pwzURI
, DWORD dwFlags
, DWORD_PTR dwReserved
, IUri
**ppURI
)
5291 const DWORD supported_flags
= Uri_CREATE_ALLOW_RELATIVE
|Uri_CREATE_ALLOW_IMPLICIT_WILDCARD_SCHEME
|
5292 Uri_CREATE_ALLOW_IMPLICIT_FILE_SCHEME
|Uri_CREATE_NO_CANONICALIZE
|Uri_CREATE_CANONICALIZE
|
5293 Uri_CREATE_DECODE_EXTRA_INFO
|Uri_CREATE_NO_DECODE_EXTRA_INFO
|Uri_CREATE_CRACK_UNKNOWN_SCHEMES
|
5294 Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES
|Uri_CREATE_PRE_PROCESS_HTML_URI
|Uri_CREATE_NO_PRE_PROCESS_HTML_URI
|
5295 Uri_CREATE_NO_IE_SETTINGS
|Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS
|Uri_CREATE_FILE_USE_DOS_PATH
;
5300 TRACE("(%s %x %x %p)\n", debugstr_w(pwzURI
), dwFlags
, (DWORD
)dwReserved
, ppURI
);
5303 return E_INVALIDARG
;
5307 return E_INVALIDARG
;
5310 /* Check for invalid flags. */
5311 if(has_invalid_flag_combination(dwFlags
)) {
5313 return E_INVALIDARG
;
5316 /* Currently unsupported. */
5317 if(dwFlags
& ~supported_flags
)
5318 FIXME("Ignoring unsupported flag(s) %x\n", dwFlags
& ~supported_flags
);
5320 hr
= Uri_Construct(NULL
, (void**)&ret
);
5326 /* Explicitly set the default flags if it doesn't cause a flag conflict. */
5327 apply_default_flags(&dwFlags
);
5329 /* Pre process the URI, unless told otherwise. */
5330 if(!(dwFlags
& Uri_CREATE_NO_PRE_PROCESS_HTML_URI
))
5331 ret
->raw_uri
= pre_process_uri(pwzURI
);
5333 ret
->raw_uri
= SysAllocString(pwzURI
);
5337 return E_OUTOFMEMORY
;
5340 memset(&data
, 0, sizeof(parse_data
));
5341 data
.uri
= ret
->raw_uri
;
5343 /* Validate and parse the URI into its components. */
5344 if(!parse_uri(&data
, dwFlags
)) {
5345 /* Encountered an unsupported or invalid URI */
5346 IUri_Release(&ret
->IUri_iface
);
5348 return E_INVALIDARG
;
5351 /* Canonicalize the URI. */
5352 hr
= canonicalize_uri(&data
, ret
, dwFlags
);
5354 IUri_Release(&ret
->IUri_iface
);
5359 ret
->create_flags
= dwFlags
;
5361 *ppURI
= &ret
->IUri_iface
;
5365 /***********************************************************************
5366 * CreateUriWithFragment (urlmon.@)
5368 * Creates a new IUri object. This is almost the same as CreateUri, expect that
5369 * it allows you to explicitly specify a fragment (pwzFragment) for pwzURI.
5372 * pwzURI [I] The URI to parse and perform canonicalization on.
5373 * pwzFragment [I] The explicit fragment string which should be added to pwzURI.
5374 * dwFlags [I] The flags which will be passed to CreateUri.
5375 * dwReserved [I] Reserved (not used).
5376 * ppURI [O] The resulting IUri after parsing/canonicalization.
5379 * Success: S_OK. ppURI contains the pointer to the newly allocated IUri.
5380 * Failure: E_INVALIDARG if pwzURI already contains a fragment and pwzFragment
5381 * isn't NULL. Will also return E_INVALIDARG for the same reasons as
5382 * CreateUri will. E_OUTOFMEMORY if any allocation fails.
5384 HRESULT WINAPI
CreateUriWithFragment(LPCWSTR pwzURI
, LPCWSTR pwzFragment
, DWORD dwFlags
,
5385 DWORD_PTR dwReserved
, IUri
**ppURI
)
5388 TRACE("(%s %s %x %x %p)\n", debugstr_w(pwzURI
), debugstr_w(pwzFragment
), dwFlags
, (DWORD
)dwReserved
, ppURI
);
5391 return E_INVALIDARG
;
5395 return E_INVALIDARG
;
5398 /* Check if a fragment should be appended to the URI string. */
5401 DWORD uri_len
, frag_len
;
5404 /* Check if the original URI already has a fragment component. */
5405 if(StrChrW(pwzURI
, '#')) {
5407 return E_INVALIDARG
;
5410 uri_len
= lstrlenW(pwzURI
);
5411 frag_len
= lstrlenW(pwzFragment
);
5413 /* If the fragment doesn't start with a '#', one will be added. */
5414 add_pound
= *pwzFragment
!= '#';
5417 uriW
= heap_alloc((uri_len
+frag_len
+2)*sizeof(WCHAR
));
5419 uriW
= heap_alloc((uri_len
+frag_len
+1)*sizeof(WCHAR
));
5422 return E_OUTOFMEMORY
;
5424 memcpy(uriW
, pwzURI
, uri_len
*sizeof(WCHAR
));
5426 uriW
[uri_len
++] = '#';
5427 memcpy(uriW
+uri_len
, pwzFragment
, (frag_len
+1)*sizeof(WCHAR
));
5429 hres
= CreateUri(uriW
, dwFlags
, 0, ppURI
);
5433 /* A fragment string wasn't specified, so just forward the call. */
5434 hres
= CreateUri(pwzURI
, dwFlags
, 0, ppURI
);
5439 static HRESULT
build_uri(const UriBuilder
*builder
, IUri
**uri
, DWORD create_flags
,
5440 DWORD use_orig_flags
, DWORD encoding_mask
)
5449 if(encoding_mask
&& (!builder
->uri
|| builder
->modified_props
)) {
5454 /* Decide what flags should be used when creating the Uri. */
5455 if((use_orig_flags
& UriBuilder_USE_ORIGINAL_FLAGS
) && builder
->uri
)
5456 create_flags
= builder
->uri
->create_flags
;
5458 if(has_invalid_flag_combination(create_flags
)) {
5460 return E_INVALIDARG
;
5463 /* Set the default flags if they don't cause a conflict. */
5464 apply_default_flags(&create_flags
);
5467 /* Return the base IUri if no changes have been made and the create_flags match. */
5468 if(builder
->uri
&& !builder
->modified_props
&& builder
->uri
->create_flags
== create_flags
) {
5469 *uri
= &builder
->uri
->IUri_iface
;
5474 hr
= validate_components(builder
, &data
, create_flags
);
5480 hr
= Uri_Construct(NULL
, (void**)&ret
);
5486 hr
= generate_uri(builder
, &data
, ret
, create_flags
);
5488 IUri_Release(&ret
->IUri_iface
);
5493 *uri
= &ret
->IUri_iface
;
5497 static inline UriBuilder
* impl_from_IUriBuilder(IUriBuilder
*iface
)
5499 return CONTAINING_RECORD(iface
, UriBuilder
, IUriBuilder_iface
);
5502 static HRESULT WINAPI
UriBuilder_QueryInterface(IUriBuilder
*iface
, REFIID riid
, void **ppv
)
5504 UriBuilder
*This
= impl_from_IUriBuilder(iface
);
5506 if(IsEqualGUID(&IID_IUnknown
, riid
)) {
5507 TRACE("(%p)->(IID_IUnknown %p)\n", This
, ppv
);
5508 *ppv
= &This
->IUriBuilder_iface
;
5509 }else if(IsEqualGUID(&IID_IUriBuilder
, riid
)) {
5510 TRACE("(%p)->(IID_IUriBuilder %p)\n", This
, ppv
);
5511 *ppv
= &This
->IUriBuilder_iface
;
5513 TRACE("(%p)->(%s %p)\n", This
, debugstr_guid(riid
), ppv
);
5515 return E_NOINTERFACE
;
5518 IUnknown_AddRef((IUnknown
*)*ppv
);
5522 static ULONG WINAPI
UriBuilder_AddRef(IUriBuilder
*iface
)
5524 UriBuilder
*This
= impl_from_IUriBuilder(iface
);
5525 LONG ref
= InterlockedIncrement(&This
->ref
);
5527 TRACE("(%p) ref=%d\n", This
, ref
);
5532 static ULONG WINAPI
UriBuilder_Release(IUriBuilder
*iface
)
5534 UriBuilder
*This
= impl_from_IUriBuilder(iface
);
5535 LONG ref
= InterlockedDecrement(&This
->ref
);
5537 TRACE("(%p) ref=%d\n", This
, ref
);
5540 if(This
->uri
) IUri_Release(&This
->uri
->IUri_iface
);
5541 heap_free(This
->fragment
);
5542 heap_free(This
->host
);
5543 heap_free(This
->password
);
5544 heap_free(This
->path
);
5545 heap_free(This
->query
);
5546 heap_free(This
->scheme
);
5547 heap_free(This
->username
);
5554 static HRESULT WINAPI
UriBuilder_CreateUriSimple(IUriBuilder
*iface
,
5555 DWORD dwAllowEncodingPropertyMask
,
5556 DWORD_PTR dwReserved
,
5559 UriBuilder
*This
= impl_from_IUriBuilder(iface
);
5561 TRACE("(%p)->(%d %d %p)\n", This
, dwAllowEncodingPropertyMask
, (DWORD
)dwReserved
, ppIUri
);
5563 hr
= build_uri(This
, ppIUri
, 0, UriBuilder_USE_ORIGINAL_FLAGS
, dwAllowEncodingPropertyMask
);
5565 FIXME("(%p)->(%d %d %p)\n", This
, dwAllowEncodingPropertyMask
, (DWORD
)dwReserved
, ppIUri
);
5569 static HRESULT WINAPI
UriBuilder_CreateUri(IUriBuilder
*iface
,
5570 DWORD dwCreateFlags
,
5571 DWORD dwAllowEncodingPropertyMask
,
5572 DWORD_PTR dwReserved
,
5575 UriBuilder
*This
= impl_from_IUriBuilder(iface
);
5577 TRACE("(%p)->(0x%08x %d %d %p)\n", This
, dwCreateFlags
, dwAllowEncodingPropertyMask
, (DWORD
)dwReserved
, ppIUri
);
5579 if(dwCreateFlags
== -1)
5580 hr
= build_uri(This
, ppIUri
, 0, UriBuilder_USE_ORIGINAL_FLAGS
, dwAllowEncodingPropertyMask
);
5582 hr
= build_uri(This
, ppIUri
, dwCreateFlags
, 0, dwAllowEncodingPropertyMask
);
5585 FIXME("(%p)->(0x%08x %d %d %p)\n", This
, dwCreateFlags
, dwAllowEncodingPropertyMask
, (DWORD
)dwReserved
, ppIUri
);
5589 static HRESULT WINAPI
UriBuilder_CreateUriWithFlags(IUriBuilder
*iface
,
5590 DWORD dwCreateFlags
,
5591 DWORD dwUriBuilderFlags
,
5592 DWORD dwAllowEncodingPropertyMask
,
5593 DWORD_PTR dwReserved
,
5596 UriBuilder
*This
= impl_from_IUriBuilder(iface
);
5598 TRACE("(%p)->(0x%08x 0x%08x %d %d %p)\n", This
, dwCreateFlags
, dwUriBuilderFlags
,
5599 dwAllowEncodingPropertyMask
, (DWORD
)dwReserved
, ppIUri
);
5601 hr
= build_uri(This
, ppIUri
, dwCreateFlags
, dwUriBuilderFlags
, dwAllowEncodingPropertyMask
);
5603 FIXME("(%p)->(0x%08x 0x%08x %d %d %p)\n", This
, dwCreateFlags
, dwUriBuilderFlags
,
5604 dwAllowEncodingPropertyMask
, (DWORD
)dwReserved
, ppIUri
);
5608 static HRESULT WINAPI
UriBuilder_GetIUri(IUriBuilder
*iface
, IUri
**ppIUri
)
5610 UriBuilder
*This
= impl_from_IUriBuilder(iface
);
5611 TRACE("(%p)->(%p)\n", This
, ppIUri
);
5617 IUri
*uri
= &This
->uri
->IUri_iface
;
5626 static HRESULT WINAPI
UriBuilder_SetIUri(IUriBuilder
*iface
, IUri
*pIUri
)
5628 UriBuilder
*This
= impl_from_IUriBuilder(iface
);
5629 TRACE("(%p)->(%p)\n", This
, pIUri
);
5634 if((uri
= get_uri_obj(pIUri
))) {
5635 /* Only reset the builder if its Uri isn't the same as
5636 * the Uri passed to the function.
5638 if(This
->uri
!= uri
) {
5639 reset_builder(This
);
5643 This
->port
= uri
->port
;
5648 FIXME("(%p)->(%p) Unknown IUri types not supported yet.\n", This
, pIUri
);
5651 } else if(This
->uri
)
5652 /* Only reset the builder if its Uri isn't NULL. */
5653 reset_builder(This
);
5658 static HRESULT WINAPI
UriBuilder_GetFragment(IUriBuilder
*iface
, DWORD
*pcchFragment
, LPCWSTR
*ppwzFragment
)
5660 UriBuilder
*This
= impl_from_IUriBuilder(iface
);
5661 TRACE("(%p)->(%p %p)\n", This
, pcchFragment
, ppwzFragment
);
5663 if(!This
->uri
|| This
->uri
->fragment_start
== -1 || This
->modified_props
& Uri_HAS_FRAGMENT
)
5664 return get_builder_component(&This
->fragment
, &This
->fragment_len
, NULL
, 0, ppwzFragment
, pcchFragment
);
5666 return get_builder_component(&This
->fragment
, &This
->fragment_len
, This
->uri
->canon_uri
+This
->uri
->fragment_start
,
5667 This
->uri
->fragment_len
, ppwzFragment
, pcchFragment
);
5670 static HRESULT WINAPI
UriBuilder_GetHost(IUriBuilder
*iface
, DWORD
*pcchHost
, LPCWSTR
*ppwzHost
)
5672 UriBuilder
*This
= impl_from_IUriBuilder(iface
);
5673 TRACE("(%p)->(%p %p)\n", This
, pcchHost
, ppwzHost
);
5675 if(!This
->uri
|| This
->uri
->host_start
== -1 || This
->modified_props
& Uri_HAS_HOST
)
5676 return get_builder_component(&This
->host
, &This
->host_len
, NULL
, 0, ppwzHost
, pcchHost
);
5678 if(This
->uri
->host_type
== Uri_HOST_IPV6
)
5679 /* Don't include the '[' and ']' around the address. */
5680 return get_builder_component(&This
->host
, &This
->host_len
, This
->uri
->canon_uri
+This
->uri
->host_start
+1,
5681 This
->uri
->host_len
-2, ppwzHost
, pcchHost
);
5683 return get_builder_component(&This
->host
, &This
->host_len
, This
->uri
->canon_uri
+This
->uri
->host_start
,
5684 This
->uri
->host_len
, ppwzHost
, pcchHost
);
5688 static HRESULT WINAPI
UriBuilder_GetPassword(IUriBuilder
*iface
, DWORD
*pcchPassword
, LPCWSTR
*ppwzPassword
)
5690 UriBuilder
*This
= impl_from_IUriBuilder(iface
);
5691 TRACE("(%p)->(%p %p)\n", This
, pcchPassword
, ppwzPassword
);
5693 if(!This
->uri
|| This
->uri
->userinfo_split
== -1 || This
->modified_props
& Uri_HAS_PASSWORD
)
5694 return get_builder_component(&This
->password
, &This
->password_len
, NULL
, 0, ppwzPassword
, pcchPassword
);
5696 const WCHAR
*start
= This
->uri
->canon_uri
+This
->uri
->userinfo_start
+This
->uri
->userinfo_split
+1;
5697 DWORD len
= This
->uri
->userinfo_len
-This
->uri
->userinfo_split
-1;
5698 return get_builder_component(&This
->password
, &This
->password_len
, start
, len
, ppwzPassword
, pcchPassword
);
5702 static HRESULT WINAPI
UriBuilder_GetPath(IUriBuilder
*iface
, DWORD
*pcchPath
, LPCWSTR
*ppwzPath
)
5704 UriBuilder
*This
= impl_from_IUriBuilder(iface
);
5705 TRACE("(%p)->(%p %p)\n", This
, pcchPath
, ppwzPath
);
5707 if(!This
->uri
|| This
->uri
->path_start
== -1 || This
->modified_props
& Uri_HAS_PATH
)
5708 return get_builder_component(&This
->path
, &This
->path_len
, NULL
, 0, ppwzPath
, pcchPath
);
5710 return get_builder_component(&This
->path
, &This
->path_len
, This
->uri
->canon_uri
+This
->uri
->path_start
,
5711 This
->uri
->path_len
, ppwzPath
, pcchPath
);
5714 static HRESULT WINAPI
UriBuilder_GetPort(IUriBuilder
*iface
, BOOL
*pfHasPort
, DWORD
*pdwPort
)
5716 UriBuilder
*This
= impl_from_IUriBuilder(iface
);
5717 TRACE("(%p)->(%p %p)\n", This
, pfHasPort
, pdwPort
);
5730 *pfHasPort
= This
->has_port
;
5731 *pdwPort
= This
->port
;
5735 static HRESULT WINAPI
UriBuilder_GetQuery(IUriBuilder
*iface
, DWORD
*pcchQuery
, LPCWSTR
*ppwzQuery
)
5737 UriBuilder
*This
= impl_from_IUriBuilder(iface
);
5738 TRACE("(%p)->(%p %p)\n", This
, pcchQuery
, ppwzQuery
);
5740 if(!This
->uri
|| This
->uri
->query_start
== -1 || This
->modified_props
& Uri_HAS_QUERY
)
5741 return get_builder_component(&This
->query
, &This
->query_len
, NULL
, 0, ppwzQuery
, pcchQuery
);
5743 return get_builder_component(&This
->query
, &This
->query_len
, This
->uri
->canon_uri
+This
->uri
->query_start
,
5744 This
->uri
->query_len
, ppwzQuery
, pcchQuery
);
5747 static HRESULT WINAPI
UriBuilder_GetSchemeName(IUriBuilder
*iface
, DWORD
*pcchSchemeName
, LPCWSTR
*ppwzSchemeName
)
5749 UriBuilder
*This
= impl_from_IUriBuilder(iface
);
5750 TRACE("(%p)->(%p %p)\n", This
, pcchSchemeName
, ppwzSchemeName
);
5752 if(!This
->uri
|| This
->uri
->scheme_start
== -1 || This
->modified_props
& Uri_HAS_SCHEME_NAME
)
5753 return get_builder_component(&This
->scheme
, &This
->scheme_len
, NULL
, 0, ppwzSchemeName
, pcchSchemeName
);
5755 return get_builder_component(&This
->scheme
, &This
->scheme_len
, This
->uri
->canon_uri
+This
->uri
->scheme_start
,
5756 This
->uri
->scheme_len
, ppwzSchemeName
, pcchSchemeName
);
5759 static HRESULT WINAPI
UriBuilder_GetUserName(IUriBuilder
*iface
, DWORD
*pcchUserName
, LPCWSTR
*ppwzUserName
)
5761 UriBuilder
*This
= impl_from_IUriBuilder(iface
);
5762 TRACE("(%p)->(%p %p)\n", This
, pcchUserName
, ppwzUserName
);
5764 if(!This
->uri
|| This
->uri
->userinfo_start
== -1 || This
->uri
->userinfo_split
== 0 ||
5765 This
->modified_props
& Uri_HAS_USER_NAME
)
5766 return get_builder_component(&This
->username
, &This
->username_len
, NULL
, 0, ppwzUserName
, pcchUserName
);
5768 const WCHAR
*start
= This
->uri
->canon_uri
+This
->uri
->userinfo_start
;
5770 /* Check if there's a password in the userinfo section. */
5771 if(This
->uri
->userinfo_split
> -1)
5772 /* Don't include the password. */
5773 return get_builder_component(&This
->username
, &This
->username_len
, start
,
5774 This
->uri
->userinfo_split
, ppwzUserName
, pcchUserName
);
5776 return get_builder_component(&This
->username
, &This
->username_len
, start
,
5777 This
->uri
->userinfo_len
, ppwzUserName
, pcchUserName
);
5781 static HRESULT WINAPI
UriBuilder_SetFragment(IUriBuilder
*iface
, LPCWSTR pwzNewValue
)
5783 UriBuilder
*This
= impl_from_IUriBuilder(iface
);
5784 TRACE("(%p)->(%s)\n", This
, debugstr_w(pwzNewValue
));
5785 return set_builder_component(&This
->fragment
, &This
->fragment_len
, pwzNewValue
, '#',
5786 &This
->modified_props
, Uri_HAS_FRAGMENT
);
5789 static HRESULT WINAPI
UriBuilder_SetHost(IUriBuilder
*iface
, LPCWSTR pwzNewValue
)
5791 UriBuilder
*This
= impl_from_IUriBuilder(iface
);
5792 TRACE("(%p)->(%s)\n", This
, debugstr_w(pwzNewValue
));
5794 /* Host name can't be set to NULL. */
5796 return E_INVALIDARG
;
5798 return set_builder_component(&This
->host
, &This
->host_len
, pwzNewValue
, 0,
5799 &This
->modified_props
, Uri_HAS_HOST
);
5802 static HRESULT WINAPI
UriBuilder_SetPassword(IUriBuilder
*iface
, LPCWSTR pwzNewValue
)
5804 UriBuilder
*This
= impl_from_IUriBuilder(iface
);
5805 TRACE("(%p)->(%s)\n", This
, debugstr_w(pwzNewValue
));
5806 return set_builder_component(&This
->password
, &This
->password_len
, pwzNewValue
, 0,
5807 &This
->modified_props
, Uri_HAS_PASSWORD
);
5810 static HRESULT WINAPI
UriBuilder_SetPath(IUriBuilder
*iface
, LPCWSTR pwzNewValue
)
5812 UriBuilder
*This
= impl_from_IUriBuilder(iface
);
5813 TRACE("(%p)->(%s)\n", This
, debugstr_w(pwzNewValue
));
5814 return set_builder_component(&This
->path
, &This
->path_len
, pwzNewValue
, 0,
5815 &This
->modified_props
, Uri_HAS_PATH
);
5818 static HRESULT WINAPI
UriBuilder_SetPort(IUriBuilder
*iface
, BOOL fHasPort
, DWORD dwNewValue
)
5820 UriBuilder
*This
= impl_from_IUriBuilder(iface
);
5821 TRACE("(%p)->(%d %d)\n", This
, fHasPort
, dwNewValue
);
5823 This
->has_port
= fHasPort
;
5824 This
->port
= dwNewValue
;
5825 This
->modified_props
|= Uri_HAS_PORT
;
5829 static HRESULT WINAPI
UriBuilder_SetQuery(IUriBuilder
*iface
, LPCWSTR pwzNewValue
)
5831 UriBuilder
*This
= impl_from_IUriBuilder(iface
);
5832 TRACE("(%p)->(%s)\n", This
, debugstr_w(pwzNewValue
));
5833 return set_builder_component(&This
->query
, &This
->query_len
, pwzNewValue
, '?',
5834 &This
->modified_props
, Uri_HAS_QUERY
);
5837 static HRESULT WINAPI
UriBuilder_SetSchemeName(IUriBuilder
*iface
, LPCWSTR pwzNewValue
)
5839 UriBuilder
*This
= impl_from_IUriBuilder(iface
);
5840 TRACE("(%p)->(%s)\n", This
, debugstr_w(pwzNewValue
));
5842 /* Only set the scheme name if it's not NULL or empty. */
5843 if(!pwzNewValue
|| !*pwzNewValue
)
5844 return E_INVALIDARG
;
5846 return set_builder_component(&This
->scheme
, &This
->scheme_len
, pwzNewValue
, 0,
5847 &This
->modified_props
, Uri_HAS_SCHEME_NAME
);
5850 static HRESULT WINAPI
UriBuilder_SetUserName(IUriBuilder
*iface
, LPCWSTR pwzNewValue
)
5852 UriBuilder
*This
= impl_from_IUriBuilder(iface
);
5853 TRACE("(%p)->(%s)\n", This
, debugstr_w(pwzNewValue
));
5854 return set_builder_component(&This
->username
, &This
->username_len
, pwzNewValue
, 0,
5855 &This
->modified_props
, Uri_HAS_USER_NAME
);
5858 static HRESULT WINAPI
UriBuilder_RemoveProperties(IUriBuilder
*iface
, DWORD dwPropertyMask
)
5860 const DWORD accepted_flags
= Uri_HAS_AUTHORITY
|Uri_HAS_DOMAIN
|Uri_HAS_EXTENSION
|Uri_HAS_FRAGMENT
|Uri_HAS_HOST
|
5861 Uri_HAS_PASSWORD
|Uri_HAS_PATH
|Uri_HAS_PATH_AND_QUERY
|Uri_HAS_QUERY
|
5862 Uri_HAS_USER_INFO
|Uri_HAS_USER_NAME
;
5864 UriBuilder
*This
= impl_from_IUriBuilder(iface
);
5865 TRACE("(%p)->(0x%08x)\n", This
, dwPropertyMask
);
5867 if(dwPropertyMask
& ~accepted_flags
)
5868 return E_INVALIDARG
;
5870 if(dwPropertyMask
& Uri_HAS_FRAGMENT
)
5871 UriBuilder_SetFragment(iface
, NULL
);
5873 /* Even though you can't set the host name to NULL or an
5874 * empty string, you can still remove it... for some reason.
5876 if(dwPropertyMask
& Uri_HAS_HOST
)
5877 set_builder_component(&This
->host
, &This
->host_len
, NULL
, 0,
5878 &This
->modified_props
, Uri_HAS_HOST
);
5880 if(dwPropertyMask
& Uri_HAS_PASSWORD
)
5881 UriBuilder_SetPassword(iface
, NULL
);
5883 if(dwPropertyMask
& Uri_HAS_PATH
)
5884 UriBuilder_SetPath(iface
, NULL
);
5886 if(dwPropertyMask
& Uri_HAS_PORT
)
5887 UriBuilder_SetPort(iface
, FALSE
, 0);
5889 if(dwPropertyMask
& Uri_HAS_QUERY
)
5890 UriBuilder_SetQuery(iface
, NULL
);
5892 if(dwPropertyMask
& Uri_HAS_USER_NAME
)
5893 UriBuilder_SetUserName(iface
, NULL
);
5898 static HRESULT WINAPI
UriBuilder_HasBeenModified(IUriBuilder
*iface
, BOOL
*pfModified
)
5900 UriBuilder
*This
= impl_from_IUriBuilder(iface
);
5901 TRACE("(%p)->(%p)\n", This
, pfModified
);
5906 *pfModified
= This
->modified_props
> 0;
5910 static const IUriBuilderVtbl UriBuilderVtbl
= {
5911 UriBuilder_QueryInterface
,
5914 UriBuilder_CreateUriSimple
,
5915 UriBuilder_CreateUri
,
5916 UriBuilder_CreateUriWithFlags
,
5919 UriBuilder_GetFragment
,
5921 UriBuilder_GetPassword
,
5924 UriBuilder_GetQuery
,
5925 UriBuilder_GetSchemeName
,
5926 UriBuilder_GetUserName
,
5927 UriBuilder_SetFragment
,
5929 UriBuilder_SetPassword
,
5932 UriBuilder_SetQuery
,
5933 UriBuilder_SetSchemeName
,
5934 UriBuilder_SetUserName
,
5935 UriBuilder_RemoveProperties
,
5936 UriBuilder_HasBeenModified
,
5939 /***********************************************************************
5940 * CreateIUriBuilder (urlmon.@)
5942 HRESULT WINAPI
CreateIUriBuilder(IUri
*pIUri
, DWORD dwFlags
, DWORD_PTR dwReserved
, IUriBuilder
**ppIUriBuilder
)
5946 TRACE("(%p %x %x %p)\n", pIUri
, dwFlags
, (DWORD
)dwReserved
, ppIUriBuilder
);
5951 ret
= heap_alloc_zero(sizeof(UriBuilder
));
5953 return E_OUTOFMEMORY
;
5955 ret
->IUriBuilder_iface
.lpVtbl
= &UriBuilderVtbl
;
5961 if((uri
= get_uri_obj(pIUri
))) {
5962 if(!uri
->create_flags
) {
5964 return E_UNEXPECTED
;
5970 /* Windows doesn't set 'has_port' to TRUE in this case. */
5971 ret
->port
= uri
->port
;
5975 *ppIUriBuilder
= NULL
;
5976 FIXME("(%p %x %x %p): Unknown IUri types not supported yet.\n", pIUri
, dwFlags
,
5977 (DWORD
)dwReserved
, ppIUriBuilder
);
5982 *ppIUriBuilder
= &ret
->IUriBuilder_iface
;
5986 /* Merges the base path with the relative path and stores the resulting path
5987 * and path len in 'result' and 'result_len'.
5989 static HRESULT
merge_paths(parse_data
*data
, const WCHAR
*base
, DWORD base_len
, const WCHAR
*relative
,
5990 DWORD relative_len
, WCHAR
**result
, DWORD
*result_len
, DWORD flags
)
5992 const WCHAR
*end
= NULL
;
5993 DWORD base_copy_len
= 0;
5997 if(data
->scheme_type
== URL_SCHEME_MK
&& *relative
== '/') {
5998 /* Find '::' segment */
5999 for(end
= base
; end
< base
+base_len
-1; end
++) {
6000 if(end
[0] == ':' && end
[1] == ':') {
6006 /* If not found, try finding the end of @xxx: */
6007 if(end
== base
+base_len
-1)
6008 end
= *base
== '@' ? wmemchr(base
, ':', base_len
) : NULL
;
6010 /* Find the characters that will be copied over from the base path. */
6011 for (end
= base
+ base_len
- 1; end
>= base
; end
--) if (*end
== '/') break;
6012 if(end
< base
&& data
->scheme_type
== URL_SCHEME_FILE
)
6013 /* Try looking for a '\\'. */
6014 for (end
= base
+ base_len
- 1; end
>= base
; end
--) if (*end
== '\\') break;
6018 if (end
) base_copy_len
= (end
+1)-base
;
6019 *result
= heap_alloc((base_copy_len
+relative_len
+1)*sizeof(WCHAR
));
6023 return E_OUTOFMEMORY
;
6027 memcpy(ptr
, base
, base_copy_len
*sizeof(WCHAR
));
6028 ptr
+= base_copy_len
;
6030 memcpy(ptr
, relative
, relative_len
*sizeof(WCHAR
));
6031 ptr
+= relative_len
;
6034 *result_len
= (ptr
-*result
);
6035 TRACE("ret %s\n", debugstr_wn(*result
, *result_len
));
6039 static HRESULT
combine_uri(Uri
*base
, Uri
*relative
, DWORD flags
, IUri
**result
, DWORD extras
) {
6043 Uri
*proc_uri
= base
;
6044 DWORD create_flags
= 0, len
= 0;
6046 memset(&data
, 0, sizeof(parse_data
));
6048 /* Base case is when the relative Uri has a scheme name,
6049 * if it does, then 'result' will contain the same data
6050 * as the relative Uri.
6052 if(relative
->scheme_start
> -1) {
6053 data
.uri
= SysAllocString(relative
->raw_uri
);
6056 return E_OUTOFMEMORY
;
6059 parse_uri(&data
, Uri_CREATE_ALLOW_IMPLICIT_FILE_SCHEME
);
6061 hr
= Uri_Construct(NULL
, (void**)&ret
);
6067 if(extras
& COMBINE_URI_FORCE_FLAG_USE
) {
6068 if(flags
& URL_DONT_SIMPLIFY
)
6069 create_flags
|= Uri_CREATE_NO_CANONICALIZE
;
6070 if(flags
& URL_DONT_UNESCAPE_EXTRA_INFO
)
6071 create_flags
|= Uri_CREATE_NO_DECODE_EXTRA_INFO
;
6074 ret
->raw_uri
= data
.uri
;
6075 hr
= canonicalize_uri(&data
, ret
, create_flags
);
6077 IUri_Release(&ret
->IUri_iface
);
6082 apply_default_flags(&create_flags
);
6083 ret
->create_flags
= create_flags
;
6085 *result
= &ret
->IUri_iface
;
6088 DWORD raw_flags
= 0;
6090 if(base
->scheme_start
> -1) {
6091 data
.scheme
= base
->canon_uri
+base
->scheme_start
;
6092 data
.scheme_len
= base
->scheme_len
;
6093 data
.scheme_type
= base
->scheme_type
;
6095 data
.is_relative
= TRUE
;
6096 data
.scheme_type
= URL_SCHEME_UNKNOWN
;
6097 create_flags
|= Uri_CREATE_ALLOW_RELATIVE
;
6100 if(relative
->authority_start
> -1)
6101 proc_uri
= relative
;
6103 if(proc_uri
->authority_start
> -1) {
6104 if(proc_uri
->userinfo_start
> -1 && proc_uri
->userinfo_split
!= 0) {
6105 data
.username
= proc_uri
->canon_uri
+proc_uri
->userinfo_start
;
6106 data
.username_len
= (proc_uri
->userinfo_split
> -1) ? proc_uri
->userinfo_split
: proc_uri
->userinfo_len
;
6109 if(proc_uri
->userinfo_split
> -1) {
6110 data
.password
= proc_uri
->canon_uri
+proc_uri
->userinfo_start
+proc_uri
->userinfo_split
+1;
6111 data
.password_len
= proc_uri
->userinfo_len
-proc_uri
->userinfo_split
-1;
6114 if(proc_uri
->host_start
> -1) {
6115 const WCHAR
*host
= proc_uri
->canon_uri
+proc_uri
->host_start
;
6116 parse_host(&host
, &data
, 0);
6119 if(proc_uri
->has_port
) {
6120 data
.has_port
= TRUE
;
6121 data
.port_value
= proc_uri
->port
;
6123 } else if(base
->scheme_type
!= URL_SCHEME_FILE
)
6124 data
.is_opaque
= TRUE
;
6126 if(proc_uri
== relative
|| relative
->path_start
== -1 || !relative
->path_len
) {
6127 if(proc_uri
->path_start
> -1) {
6128 data
.path
= proc_uri
->canon_uri
+proc_uri
->path_start
;
6129 data
.path_len
= proc_uri
->path_len
;
6130 } else if(!data
.is_opaque
) {
6131 /* Just set the path as a '/' if the base didn't have
6132 * one and if it's a hierarchical URI.
6134 static const WCHAR slashW
[] = {'/',0};
6139 if(relative
->query_start
> -1)
6140 proc_uri
= relative
;
6142 if(proc_uri
->query_start
> -1) {
6143 data
.query
= proc_uri
->canon_uri
+proc_uri
->query_start
;
6144 data
.query_len
= proc_uri
->query_len
;
6147 const WCHAR
*ptr
, **pptr
;
6148 DWORD path_offset
= 0, path_len
= 0;
6150 /* There's two possibilities on what will happen to the path component
6151 * of the result IUri. First, if the relative path begins with a '/'
6152 * then the resulting path will just be the relative path. Second, if
6153 * relative path doesn't begin with a '/' then the base path and relative
6154 * path are merged together.
6156 if(relative
->path_len
&& *(relative
->canon_uri
+relative
->path_start
) == '/' && data
.scheme_type
!= URL_SCHEME_MK
) {
6158 BOOL copy_drive_path
= FALSE
;
6160 /* If the relative IUri's path starts with a '/', then we
6161 * don't use the base IUri's path. Unless the base IUri
6162 * is a file URI, in which case it uses the drive path of
6163 * the base IUri (if it has any) in the new path.
6165 if(base
->scheme_type
== URL_SCHEME_FILE
) {
6166 if(base
->path_len
> 3 && *(base
->canon_uri
+base
->path_start
) == '/' &&
6167 is_drive_path(base
->canon_uri
+base
->path_start
+1)) {
6169 copy_drive_path
= TRUE
;
6173 path_len
+= relative
->path_len
;
6175 path
= heap_alloc((path_len
+1)*sizeof(WCHAR
));
6178 return E_OUTOFMEMORY
;
6183 /* Copy the base paths, drive path over. */
6184 if(copy_drive_path
) {
6185 memcpy(tmp
, base
->canon_uri
+base
->path_start
, 3*sizeof(WCHAR
));
6189 memcpy(tmp
, relative
->canon_uri
+relative
->path_start
, relative
->path_len
*sizeof(WCHAR
));
6190 path
[path_len
] = '\0';
6192 /* Merge the base path with the relative path. */
6193 hr
= merge_paths(&data
, base
->canon_uri
+base
->path_start
, base
->path_len
,
6194 relative
->canon_uri
+relative
->path_start
, relative
->path_len
,
6195 &path
, &path_len
, flags
);
6201 /* If the resulting IUri is a file URI, the drive path isn't
6202 * reduced out when the dot segments are removed.
6204 if(path_len
>= 3 && data
.scheme_type
== URL_SCHEME_FILE
&& !data
.host
) {
6205 if(*path
== '/' && is_drive_path(path
+1))
6207 else if(is_drive_path(path
))
6212 /* Check if the dot segments need to be removed from the path. */
6213 if(!(flags
& URL_DONT_SIMPLIFY
) && !data
.is_opaque
) {
6214 DWORD offset
= (path_offset
> 0) ? path_offset
+1 : 0;
6215 DWORD new_len
= remove_dot_segments(path
+offset
,path_len
-offset
);
6217 if(new_len
!= path_len
) {
6218 WCHAR
*tmp
= heap_realloc(path
, (offset
+new_len
+1)*sizeof(WCHAR
));
6222 return E_OUTOFMEMORY
;
6225 tmp
[new_len
+offset
] = '\0';
6227 path_len
= new_len
+offset
;
6231 if(relative
->query_start
> -1) {
6232 data
.query
= relative
->canon_uri
+relative
->query_start
;
6233 data
.query_len
= relative
->query_len
;
6236 /* Make sure the path component is valid. */
6239 if((data
.is_opaque
&& !parse_path_opaque(pptr
, &data
, 0)) ||
6240 (!data
.is_opaque
&& !parse_path_hierarchical(pptr
, &data
, 0))) {
6243 return E_INVALIDARG
;
6247 if(relative
->fragment_start
> -1) {
6248 data
.fragment
= relative
->canon_uri
+relative
->fragment_start
;
6249 data
.fragment_len
= relative
->fragment_len
;
6252 if(flags
& URL_DONT_SIMPLIFY
)
6253 raw_flags
|= RAW_URI_FORCE_PORT_DISP
;
6254 if(flags
& URL_FILE_USE_PATHURL
)
6255 raw_flags
|= RAW_URI_CONVERT_TO_DOS_PATH
;
6257 len
= generate_raw_uri(&data
, data
.uri
, raw_flags
);
6258 data
.uri
= SysAllocStringLen(NULL
, len
);
6262 return E_OUTOFMEMORY
;
6265 generate_raw_uri(&data
, data
.uri
, raw_flags
);
6267 hr
= Uri_Construct(NULL
, (void**)&ret
);
6269 SysFreeString(data
.uri
);
6275 if(flags
& URL_DONT_SIMPLIFY
)
6276 create_flags
|= Uri_CREATE_NO_CANONICALIZE
;
6277 if(flags
& URL_FILE_USE_PATHURL
)
6278 create_flags
|= Uri_CREATE_FILE_USE_DOS_PATH
;
6280 ret
->raw_uri
= data
.uri
;
6281 hr
= canonicalize_uri(&data
, ret
, create_flags
);
6283 IUri_Release(&ret
->IUri_iface
);
6288 if(flags
& URL_DONT_SIMPLIFY
)
6289 ret
->display_modifiers
|= URI_DISPLAY_NO_DEFAULT_PORT_AUTH
;
6291 apply_default_flags(&create_flags
);
6292 ret
->create_flags
= create_flags
;
6293 *result
= &ret
->IUri_iface
;
6301 /***********************************************************************
6302 * CoInternetCombineIUri (urlmon.@)
6304 HRESULT WINAPI
CoInternetCombineIUri(IUri
*pBaseUri
, IUri
*pRelativeUri
, DWORD dwCombineFlags
,
6305 IUri
**ppCombinedUri
, DWORD_PTR dwReserved
)
6308 IInternetProtocolInfo
*info
;
6309 Uri
*relative
, *base
;
6310 TRACE("(%p %p %x %p %x)\n", pBaseUri
, pRelativeUri
, dwCombineFlags
, ppCombinedUri
, (DWORD
)dwReserved
);
6313 return E_INVALIDARG
;
6315 if(!pBaseUri
|| !pRelativeUri
) {
6316 *ppCombinedUri
= NULL
;
6317 return E_INVALIDARG
;
6320 relative
= get_uri_obj(pRelativeUri
);
6321 base
= get_uri_obj(pBaseUri
);
6322 if(!relative
|| !base
) {
6323 *ppCombinedUri
= NULL
;
6324 FIXME("(%p %p %x %p %x) Unknown IUri types not supported yet.\n",
6325 pBaseUri
, pRelativeUri
, dwCombineFlags
, ppCombinedUri
, (DWORD
)dwReserved
);
6329 info
= get_protocol_info(base
->canon_uri
);
6331 WCHAR result
[INTERNET_MAX_URL_LENGTH
+1];
6332 DWORD result_len
= 0;
6334 hr
= IInternetProtocolInfo_CombineUrl(info
, base
->canon_uri
, relative
->canon_uri
, dwCombineFlags
,
6335 result
, INTERNET_MAX_URL_LENGTH
+1, &result_len
, 0);
6336 IInternetProtocolInfo_Release(info
);
6338 hr
= CreateUri(result
, Uri_CREATE_ALLOW_RELATIVE
, 0, ppCombinedUri
);
6344 return combine_uri(base
, relative
, dwCombineFlags
, ppCombinedUri
, 0);
6347 /***********************************************************************
6348 * CoInternetCombineUrlEx (urlmon.@)
6350 HRESULT WINAPI
CoInternetCombineUrlEx(IUri
*pBaseUri
, LPCWSTR pwzRelativeUrl
, DWORD dwCombineFlags
,
6351 IUri
**ppCombinedUri
, DWORD_PTR dwReserved
)
6356 IInternetProtocolInfo
*info
;
6358 TRACE("(%p %s %x %p %x)\n", pBaseUri
, debugstr_w(pwzRelativeUrl
), dwCombineFlags
,
6359 ppCombinedUri
, (DWORD
)dwReserved
);
6364 if(!pwzRelativeUrl
) {
6365 *ppCombinedUri
= NULL
;
6366 return E_UNEXPECTED
;
6370 *ppCombinedUri
= NULL
;
6371 return E_INVALIDARG
;
6374 base
= get_uri_obj(pBaseUri
);
6376 *ppCombinedUri
= NULL
;
6377 FIXME("(%p %s %x %p %x) Unknown IUri's not supported yet.\n", pBaseUri
, debugstr_w(pwzRelativeUrl
),
6378 dwCombineFlags
, ppCombinedUri
, (DWORD
)dwReserved
);
6382 info
= get_protocol_info(base
->canon_uri
);
6384 WCHAR result
[INTERNET_MAX_URL_LENGTH
+1];
6385 DWORD result_len
= 0;
6387 hr
= IInternetProtocolInfo_CombineUrl(info
, base
->canon_uri
, pwzRelativeUrl
, dwCombineFlags
,
6388 result
, INTERNET_MAX_URL_LENGTH
+1, &result_len
, 0);
6389 IInternetProtocolInfo_Release(info
);
6391 hr
= CreateUri(result
, Uri_CREATE_ALLOW_RELATIVE
, 0, ppCombinedUri
);
6397 hr
= CreateUri(pwzRelativeUrl
, Uri_CREATE_ALLOW_RELATIVE
|Uri_CREATE_ALLOW_IMPLICIT_FILE_SCHEME
, 0, &relative
);
6399 *ppCombinedUri
= NULL
;
6403 hr
= combine_uri(base
, get_uri_obj(relative
), dwCombineFlags
, ppCombinedUri
, COMBINE_URI_FORCE_FLAG_USE
);
6405 IUri_Release(relative
);
6409 static HRESULT
parse_canonicalize(const Uri
*uri
, DWORD flags
, LPWSTR output
,
6410 DWORD output_len
, DWORD
*result_len
)
6412 const WCHAR
*ptr
= NULL
;
6418 /* URL_UNESCAPE only has effect if none of the URL_ESCAPE flags are set. */
6419 const BOOL allow_unescape
= !(flags
& URL_ESCAPE_UNSAFE
) &&
6420 !(flags
& URL_ESCAPE_SPACES_ONLY
) &&
6421 !(flags
& URL_ESCAPE_PERCENT
);
6424 /* Check if the dot segments need to be removed from the
6427 if(uri
->scheme_start
> -1 && uri
->path_start
> -1) {
6428 ptr
= uri
->canon_uri
+uri
->scheme_start
+uri
->scheme_len
+1;
6431 reduce_path
= !(flags
& URL_DONT_SIMPLIFY
) &&
6432 ptr
&& check_hierarchical(pptr
);
6434 for(ptr
= uri
->canon_uri
; ptr
< uri
->canon_uri
+uri
->canon_len
; ++ptr
) {
6435 BOOL do_default_action
= TRUE
;
6437 /* Keep track of the path if we need to remove dot segments from
6440 if(reduce_path
&& !path
&& ptr
== uri
->canon_uri
+uri
->path_start
)
6443 /* Check if it's time to reduce the path. */
6444 if(reduce_path
&& ptr
== uri
->canon_uri
+uri
->path_start
+uri
->path_len
) {
6445 DWORD current_path_len
= (output
+len
) - path
;
6446 DWORD new_path_len
= remove_dot_segments(path
, current_path_len
);
6448 /* Update the current length. */
6449 len
-= (current_path_len
-new_path_len
);
6450 reduce_path
= FALSE
;
6454 const WCHAR decoded
= decode_pct_val(ptr
);
6456 if(allow_unescape
&& (flags
& URL_UNESCAPE
)) {
6457 if(len
< output_len
)
6458 output
[len
] = decoded
;
6461 do_default_action
= FALSE
;
6465 /* See if %'s needed to encoded. */
6466 if(do_default_action
&& (flags
& URL_ESCAPE_PERCENT
)) {
6467 if(len
+ 3 < output_len
)
6468 pct_encode_val(*ptr
, output
+len
);
6470 do_default_action
= FALSE
;
6472 } else if(*ptr
== ' ') {
6473 if((flags
& URL_ESCAPE_SPACES_ONLY
) &&
6474 !(flags
& URL_ESCAPE_UNSAFE
)) {
6475 if(len
+ 3 < output_len
)
6476 pct_encode_val(*ptr
, output
+len
);
6478 do_default_action
= FALSE
;
6480 } else if(is_ascii(*ptr
) && !is_reserved(*ptr
) && !is_unreserved(*ptr
)) {
6481 if(flags
& URL_ESCAPE_UNSAFE
) {
6482 if(len
+ 3 < output_len
)
6483 pct_encode_val(*ptr
, output
+len
);
6485 do_default_action
= FALSE
;
6489 if(do_default_action
) {
6490 if(len
< output_len
)
6496 /* Sometimes the path is the very last component of the IUri, so
6497 * see if the dot segments need to be reduced now.
6499 if(reduce_path
&& path
) {
6500 DWORD current_path_len
= (output
+len
) - path
;
6501 DWORD new_path_len
= remove_dot_segments(path
, current_path_len
);
6503 /* Update the current length. */
6504 len
-= (current_path_len
-new_path_len
);
6507 if(len
< output_len
)
6510 output
[output_len
-1] = 0;
6512 /* The null terminator isn't included in the length. */
6514 if(len
>= output_len
)
6515 return STRSAFE_E_INSUFFICIENT_BUFFER
;
6520 static HRESULT
parse_friendly(IUri
*uri
, LPWSTR output
, DWORD output_len
,
6527 hr
= IUri_GetPropertyLength(uri
, Uri_PROPERTY_DISPLAY_URI
, &display_len
, 0);
6533 *result_len
= display_len
;
6534 if(display_len
+1 > output_len
)
6535 return STRSAFE_E_INSUFFICIENT_BUFFER
;
6537 hr
= IUri_GetDisplayUri(uri
, &display
);
6543 memcpy(output
, display
, (display_len
+1)*sizeof(WCHAR
));
6544 SysFreeString(display
);
6548 static HRESULT
parse_rootdocument(const Uri
*uri
, LPWSTR output
, DWORD output_len
,
6551 static const WCHAR colon_slashesW
[] = {':','/','/'};
6556 /* Windows only returns the root document if the URI has an authority
6557 * and it's not an unknown scheme type or a file scheme type.
6559 if(uri
->authority_start
== -1 ||
6560 uri
->scheme_type
== URL_SCHEME_UNKNOWN
||
6561 uri
->scheme_type
== URL_SCHEME_FILE
) {
6564 return STRSAFE_E_INSUFFICIENT_BUFFER
;
6570 len
= uri
->scheme_len
+uri
->authority_len
;
6571 /* For the "://" and '/' which will be added. */
6574 if(len
+1 > output_len
) {
6576 return STRSAFE_E_INSUFFICIENT_BUFFER
;
6580 memcpy(ptr
, uri
->canon_uri
+uri
->scheme_start
, uri
->scheme_len
*sizeof(WCHAR
));
6582 /* Add the "://". */
6583 ptr
+= uri
->scheme_len
;
6584 memcpy(ptr
, colon_slashesW
, sizeof(colon_slashesW
));
6586 /* Add the authority. */
6587 ptr
+= ARRAY_SIZE(colon_slashesW
);
6588 memcpy(ptr
, uri
->canon_uri
+uri
->authority_start
, uri
->authority_len
*sizeof(WCHAR
));
6590 /* Add the '/' after the authority. */
6591 ptr
+= uri
->authority_len
;
6599 static HRESULT
parse_document(const Uri
*uri
, LPWSTR output
, DWORD output_len
,
6604 /* It has to be a known scheme type, but, it can't be a file
6605 * scheme. It also has to hierarchical.
6607 if(uri
->scheme_type
== URL_SCHEME_UNKNOWN
||
6608 uri
->scheme_type
== URL_SCHEME_FILE
||
6609 uri
->authority_start
== -1) {
6612 return STRSAFE_E_INSUFFICIENT_BUFFER
;
6618 if(uri
->fragment_start
> -1)
6619 len
= uri
->fragment_start
;
6621 len
= uri
->canon_len
;
6624 if(len
+1 > output_len
)
6625 return STRSAFE_E_INSUFFICIENT_BUFFER
;
6627 memcpy(output
, uri
->canon_uri
, len
*sizeof(WCHAR
));
6632 static HRESULT
parse_path_from_url(const Uri
*uri
, LPWSTR output
, DWORD output_len
,
6635 const WCHAR
*path_ptr
;
6636 WCHAR buffer
[INTERNET_MAX_URL_LENGTH
+1];
6639 if(uri
->scheme_type
!= URL_SCHEME_FILE
) {
6643 return E_INVALIDARG
;
6647 if(uri
->host_start
> -1) {
6648 static const WCHAR slash_slashW
[] = {'\\','\\'};
6650 memcpy(ptr
, slash_slashW
, sizeof(slash_slashW
));
6651 ptr
+= ARRAY_SIZE(slash_slashW
);
6652 memcpy(ptr
, uri
->canon_uri
+uri
->host_start
, uri
->host_len
*sizeof(WCHAR
));
6653 ptr
+= uri
->host_len
;
6656 path_ptr
= uri
->canon_uri
+uri
->path_start
;
6657 if(uri
->path_len
> 3 && *path_ptr
== '/' && is_drive_path(path_ptr
+1))
6658 /* Skip past the '/' in front of the drive path. */
6661 for(; path_ptr
< uri
->canon_uri
+uri
->path_start
+uri
->path_len
; ++path_ptr
, ++ptr
) {
6662 BOOL do_default_action
= TRUE
;
6664 if(*path_ptr
== '%') {
6665 const WCHAR decoded
= decode_pct_val(path_ptr
);
6669 do_default_action
= FALSE
;
6671 } else if(*path_ptr
== '/') {
6673 do_default_action
= FALSE
;
6676 if(do_default_action
)
6682 *result_len
= ptr
-buffer
;
6683 if(*result_len
+1 > output_len
)
6684 return STRSAFE_E_INSUFFICIENT_BUFFER
;
6686 memcpy(output
, buffer
, (*result_len
+1)*sizeof(WCHAR
));
6690 static HRESULT
parse_url_from_path(IUri
*uri
, LPWSTR output
, DWORD output_len
,
6697 hr
= IUri_GetPropertyLength(uri
, Uri_PROPERTY_ABSOLUTE_URI
, &len
, 0);
6704 if(len
+1 > output_len
)
6705 return STRSAFE_E_INSUFFICIENT_BUFFER
;
6707 hr
= IUri_GetAbsoluteUri(uri
, &received
);
6713 memcpy(output
, received
, (len
+1)*sizeof(WCHAR
));
6714 SysFreeString(received
);
6719 static HRESULT
parse_schema(IUri
*uri
, LPWSTR output
, DWORD output_len
,
6726 hr
= IUri_GetPropertyLength(uri
, Uri_PROPERTY_SCHEME_NAME
, &len
, 0);
6733 if(len
+1 > output_len
)
6734 return STRSAFE_E_INSUFFICIENT_BUFFER
;
6736 hr
= IUri_GetSchemeName(uri
, &received
);
6742 memcpy(output
, received
, (len
+1)*sizeof(WCHAR
));
6743 SysFreeString(received
);
6748 static HRESULT
parse_site(IUri
*uri
, LPWSTR output
, DWORD output_len
, DWORD
*result_len
)
6754 hr
= IUri_GetPropertyLength(uri
, Uri_PROPERTY_HOST
, &len
, 0);
6761 if(len
+1 > output_len
)
6762 return STRSAFE_E_INSUFFICIENT_BUFFER
;
6764 hr
= IUri_GetHost(uri
, &received
);
6770 memcpy(output
, received
, (len
+1)*sizeof(WCHAR
));
6771 SysFreeString(received
);
6776 static HRESULT
parse_domain(IUri
*uri
, LPWSTR output
, DWORD output_len
, DWORD
*result_len
)
6782 hr
= IUri_GetPropertyLength(uri
, Uri_PROPERTY_DOMAIN
, &len
, 0);
6789 if(len
+1 > output_len
)
6790 return STRSAFE_E_INSUFFICIENT_BUFFER
;
6792 hr
= IUri_GetDomain(uri
, &received
);
6798 memcpy(output
, received
, (len
+1)*sizeof(WCHAR
));
6799 SysFreeString(received
);
6804 static HRESULT
parse_anchor(IUri
*uri
, LPWSTR output
, DWORD output_len
, DWORD
*result_len
)
6810 hr
= IUri_GetPropertyLength(uri
, Uri_PROPERTY_FRAGMENT
, &len
, 0);
6817 if(len
+1 > output_len
)
6818 return STRSAFE_E_INSUFFICIENT_BUFFER
;
6820 hr
= IUri_GetFragment(uri
, &received
);
6826 memcpy(output
, received
, (len
+1)*sizeof(WCHAR
));
6827 SysFreeString(received
);
6832 /***********************************************************************
6833 * CoInternetParseIUri (urlmon.@)
6835 HRESULT WINAPI
CoInternetParseIUri(IUri
*pIUri
, PARSEACTION ParseAction
, DWORD dwFlags
,
6836 LPWSTR pwzResult
, DWORD cchResult
, DWORD
*pcchResult
,
6837 DWORD_PTR dwReserved
)
6841 IInternetProtocolInfo
*info
;
6843 TRACE("(%p %d %x %p %d %p %x)\n", pIUri
, ParseAction
, dwFlags
, pwzResult
,
6844 cchResult
, pcchResult
, (DWORD
)dwReserved
);
6849 if(!pwzResult
|| !pIUri
) {
6851 return E_INVALIDARG
;
6854 if(!(uri
= get_uri_obj(pIUri
))) {
6856 FIXME("(%p %d %x %p %d %p %x) Unknown IUri's not supported for this action.\n",
6857 pIUri
, ParseAction
, dwFlags
, pwzResult
, cchResult
, pcchResult
, (DWORD
)dwReserved
);
6861 info
= get_protocol_info(uri
->canon_uri
);
6863 hr
= IInternetProtocolInfo_ParseUrl(info
, uri
->canon_uri
, ParseAction
, dwFlags
,
6864 pwzResult
, cchResult
, pcchResult
, 0);
6865 IInternetProtocolInfo_Release(info
);
6866 if(SUCCEEDED(hr
)) return hr
;
6869 switch(ParseAction
) {
6870 case PARSE_CANONICALIZE
:
6871 hr
= parse_canonicalize(uri
, dwFlags
, pwzResult
, cchResult
, pcchResult
);
6873 case PARSE_FRIENDLY
:
6874 hr
= parse_friendly(pIUri
, pwzResult
, cchResult
, pcchResult
);
6876 case PARSE_ROOTDOCUMENT
:
6877 hr
= parse_rootdocument(uri
, pwzResult
, cchResult
, pcchResult
);
6879 case PARSE_DOCUMENT
:
6880 hr
= parse_document(uri
, pwzResult
, cchResult
, pcchResult
);
6882 case PARSE_PATH_FROM_URL
:
6883 hr
= parse_path_from_url(uri
, pwzResult
, cchResult
, pcchResult
);
6885 case PARSE_URL_FROM_PATH
:
6886 hr
= parse_url_from_path(pIUri
, pwzResult
, cchResult
, pcchResult
);
6889 hr
= parse_schema(pIUri
, pwzResult
, cchResult
, pcchResult
);
6892 hr
= parse_site(pIUri
, pwzResult
, cchResult
, pcchResult
);
6895 hr
= parse_domain(pIUri
, pwzResult
, cchResult
, pcchResult
);
6897 case PARSE_LOCATION
:
6899 hr
= parse_anchor(pIUri
, pwzResult
, cchResult
, pcchResult
);
6901 case PARSE_SECURITY_URL
:
6904 case PARSE_SECURITY_DOMAIN
:
6911 FIXME("(%p %d %x %p %d %p %x) Partial stub.\n", pIUri
, ParseAction
, dwFlags
,
6912 pwzResult
, cchResult
, pcchResult
, (DWORD
)dwReserved
);