2 * Copyright 2008 Hans Leidekker for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 #include "wine/debug.h"
28 #include "winhttp_private.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(winhttp
);
38 static DWORD
set_component( struct url_component
*comp
, WCHAR
*value
, DWORD len
, DWORD flags
, BOOL
*overflow
)
40 if (*comp
->str
&& !*comp
->len
) return ERROR_INVALID_PARAMETER
;
41 if (!*comp
->len
) return ERROR_SUCCESS
;
44 if (len
&& *comp
->len
&& (flags
& (ICU_DECODE
|ICU_ESCAPE
))) return ERROR_INVALID_PARAMETER
;
50 if (len
>= *comp
->len
)
56 memcpy( *comp
->str
, value
, len
* sizeof(WCHAR
) );
57 (*comp
->str
)[len
] = 0;
63 static WCHAR
*decode_url( LPCWSTR url
, DWORD
*len
)
66 WCHAR hex
[3], *q
, *ret
;
68 if (!(ret
= malloc( *len
* sizeof(WCHAR
) ))) return NULL
;
72 if (p
[0] == '%' && iswxdigit( p
[1] ) && iswxdigit( p
[2] ))
77 *q
++ = wcstol( hex
, NULL
, 16 );
91 static inline BOOL
need_escape( WCHAR ch
)
93 const WCHAR
*p
= L
" \"#%<>[\\]^`{|}~";
95 if (ch
<= 31 || ch
>= 127) return TRUE
;
98 if (ch
== *p
++) return TRUE
;
103 static BOOL
escape_string( const WCHAR
*src
, DWORD src_len
, WCHAR
*dst
, DWORD
*dst_len
)
105 static const WCHAR hex
[] = L
"0123456789ABCDEF";
110 for (i
= 0; i
< src_len
; i
++)
112 if (src
[i
] > 0xff) return FALSE
;
113 if (need_escape( src
[i
] ))
118 p
[1] = hex
[(src
[i
] >> 4) & 0xf];
119 p
[2] = hex
[src
[i
] & 0xf];
124 else if (dst
) *p
++ = src
[i
];
127 if (dst
) dst
[*dst_len
] = 0;
131 static DWORD
escape_url( const WCHAR
*url
, DWORD
*len
, WCHAR
**ret
)
134 DWORD len_base
, len_path
;
136 if ((p
= wcsrchr( url
, '/' )))
139 if (!escape_string( p
, *len
- len_base
, NULL
, &len_path
)) return ERROR_INVALID_PARAMETER
;
147 if (!(*ret
= malloc( (len_base
+ len_path
+ 1) * sizeof(WCHAR
) ))) return ERROR_OUTOFMEMORY
;
148 memcpy( *ret
, url
, len_base
* sizeof(WCHAR
) );
150 if (p
) escape_string( p
, *len
- (p
- url
), *ret
+ len_base
, &len_path
);
151 (*ret
)[len_base
+ len_path
] = 0;
153 *len
= len_base
+ len_path
;
154 return ERROR_SUCCESS
;
157 static DWORD
parse_port( const WCHAR
*str
, DWORD len
, INTERNET_PORT
*ret
)
159 const WCHAR
*p
= str
;
161 while (len
&& '0' <= *p
&& *p
<= '9')
163 if ((port
= port
* 10 + *p
- '0') > 65535) return ERROR_WINHTTP_INVALID_URL
;
167 return ERROR_SUCCESS
;
170 /***********************************************************************
171 * WinHttpCrackUrl (winhttp.@)
173 BOOL WINAPI
WinHttpCrackUrl( const WCHAR
*url
, DWORD len
, DWORD flags
, URL_COMPONENTSW
*uc
)
175 WCHAR
*p
, *q
, *r
, *url_transformed
= NULL
;
176 INTERNET_SCHEME scheme_number
= 0;
177 struct url_component scheme
, username
, password
, hostname
, path
, extra
;
178 BOOL overflow
= FALSE
;
181 TRACE( "%s, %lu, %#lx, %p\n", debugstr_wn(url
, len
), len
, flags
, uc
);
183 if (!url
|| !uc
|| uc
->dwStructSize
!= sizeof(*uc
))
185 SetLastError( ERROR_INVALID_PARAMETER
);
188 if (!len
) len
= lstrlenW( url
);
190 if (flags
& ICU_ESCAPE
)
192 if ((err
= escape_url( url
, &len
, &url_transformed
)))
197 url
= url_transformed
;
199 else if (flags
& ICU_DECODE
)
201 if (!(url_transformed
= decode_url( url
, &len
)))
203 SetLastError( ERROR_OUTOFMEMORY
);
206 url
= url_transformed
;
208 if (!(p
= wcschr( url
, ':' )))
210 SetLastError( ERROR_WINHTTP_UNRECOGNIZED_SCHEME
);
211 free( url_transformed
);
214 if (p
- url
== 4 && !wcsnicmp( url
, L
"http", 4 )) scheme_number
= INTERNET_SCHEME_HTTP
;
215 else if (p
- url
== 5 && !wcsnicmp( url
, L
"https", 5 )) scheme_number
= INTERNET_SCHEME_HTTPS
;
218 err
= ERROR_WINHTTP_UNRECOGNIZED_SCHEME
;
222 scheme
.str
= &uc
->lpszScheme
;
223 scheme
.len
= &uc
->dwSchemeLength
;
225 if ((err
= set_component( &scheme
, (WCHAR
*)url
, p
- url
, flags
, &overflow
))) goto exit
;
228 if (!p
[0] || p
[0] != '/' || p
[1] != '/')
230 err
= ERROR_WINHTTP_INVALID_URL
;
236 err
= ERROR_WINHTTP_INVALID_URL
;
240 username
.str
= &uc
->lpszUserName
;
241 username
.len
= &uc
->dwUserNameLength
;
243 password
.str
= &uc
->lpszPassword
;
244 password
.len
= &uc
->dwPasswordLength
;
246 if ((q
= wmemchr( p
, '@', len
- (p
- url
) )) && !(wmemchr( p
, '/', q
- p
)))
249 if ((r
= wmemchr( p
, ':', q
- p
)))
251 if ((err
= set_component( &username
, p
, r
- p
, flags
, &overflow
))) goto exit
;
253 if ((err
= set_component( &password
, r
, q
- r
, flags
, &overflow
))) goto exit
;
257 if ((err
= set_component( &username
, p
, q
- p
, flags
, &overflow
))) goto exit
;
258 if ((err
= set_component( &password
, NULL
, 0, flags
, &overflow
))) goto exit
;
264 if ((err
= set_component( &username
, NULL
, 0, flags
, &overflow
))) goto exit
;
265 if ((err
= set_component( &password
, NULL
, 0, flags
, &overflow
))) goto exit
;
268 hostname
.str
= &uc
->lpszHostName
;
269 hostname
.len
= &uc
->dwHostNameLength
;
271 path
.str
= &uc
->lpszUrlPath
;
272 path
.len
= &uc
->dwUrlPathLength
;
274 extra
.str
= &uc
->lpszExtraInfo
;
275 extra
.len
= &uc
->dwExtraInfoLength
;
277 if ((q
= wmemchr( p
, '/', len
- (p
- url
) )))
279 if ((r
= wmemchr( p
, ':', q
- p
)))
281 if ((err
= set_component( &hostname
, p
, r
- p
, flags
, &overflow
))) goto exit
;
283 if ((err
= parse_port( r
, q
- r
, &uc
->nPort
))) goto exit
;
287 if ((err
= set_component( &hostname
, p
, q
- p
, flags
, &overflow
))) goto exit
;
288 if (scheme_number
== INTERNET_SCHEME_HTTP
) uc
->nPort
= INTERNET_DEFAULT_HTTP_PORT
;
289 if (scheme_number
== INTERNET_SCHEME_HTTPS
) uc
->nPort
= INTERNET_DEFAULT_HTTPS_PORT
;
292 if ((r
= wmemchr( q
, '?', len
- (q
- url
) )))
296 if ((err
= set_component( &path
, q
, r
- q
, flags
, &overflow
))) goto exit
;
297 if ((err
= set_component( &extra
, r
, len
- (r
- url
), flags
, &overflow
))) goto exit
;
299 else if ((err
= set_component( &path
, q
, len
- (q
- url
), flags
, &overflow
))) goto exit
;
303 if ((err
= set_component( &path
, q
, len
- (q
- url
), flags
, &overflow
))) goto exit
;
304 if ((err
= set_component( &extra
, (WCHAR
*)url
+ len
, 0, flags
, &overflow
))) goto exit
;
309 if ((r
= wmemchr( p
, ':', len
- (p
- url
) )))
311 if ((err
= set_component( &hostname
, p
, r
- p
, flags
, &overflow
))) goto exit
;
313 if ((err
= parse_port( r
, len
- (r
- url
), &uc
->nPort
))) goto exit
;
317 if ((err
= set_component( &hostname
, p
, len
- (p
- url
), flags
, &overflow
))) goto exit
;
318 if (scheme_number
== INTERNET_SCHEME_HTTP
) uc
->nPort
= INTERNET_DEFAULT_HTTP_PORT
;
319 if (scheme_number
== INTERNET_SCHEME_HTTPS
) uc
->nPort
= INTERNET_DEFAULT_HTTPS_PORT
;
321 if ((err
= set_component( &path
, (WCHAR
*)url
+ len
, 0, flags
, &overflow
))) goto exit
;
322 if ((err
= set_component( &extra
, (WCHAR
*)url
+ len
, 0, flags
, &overflow
))) goto exit
;
325 TRACE("scheme(%s) host(%s) port(%d) path(%s) extra(%s)\n", debugstr_wn(*scheme
.str
, *scheme
.len
),
326 debugstr_wn(*hostname
.str
, *hostname
.len
), uc
->nPort
, debugstr_wn(*path
.str
, *path
.len
),
327 debugstr_wn(*extra
.str
, *extra
.len
));
332 if (overflow
) err
= ERROR_INSUFFICIENT_BUFFER
;
333 uc
->nScheme
= scheme_number
;
335 free( url_transformed
);
340 static INTERNET_SCHEME
get_scheme( const WCHAR
*scheme
, DWORD len
)
342 if (!wcsncmp( scheme
, L
"http", len
)) return INTERNET_SCHEME_HTTP
;
343 if (!wcsncmp( scheme
, L
"https", len
)) return INTERNET_SCHEME_HTTPS
;
347 static const WCHAR
*get_scheme_string( INTERNET_SCHEME scheme
)
349 if (scheme
== INTERNET_SCHEME_HTTP
) return L
"http";
350 if (scheme
== INTERNET_SCHEME_HTTPS
) return L
"https";
354 static BOOL
uses_default_port( INTERNET_SCHEME scheme
, INTERNET_PORT port
)
356 if ((scheme
== INTERNET_SCHEME_HTTP
) && (port
== INTERNET_DEFAULT_HTTP_PORT
)) return TRUE
;
357 if ((scheme
== INTERNET_SCHEME_HTTPS
) && (port
== INTERNET_DEFAULT_HTTPS_PORT
)) return TRUE
;
361 static DWORD
get_comp_length( DWORD len
, DWORD flags
, WCHAR
*comp
)
366 ret
= len
? len
: lstrlenW( comp
);
367 if (!(flags
& ICU_ESCAPE
)) return ret
;
368 for (i
= 0; i
< len
; i
++) if (need_escape( comp
[i
] )) ret
+= 2;
372 static BOOL
get_url_length( URL_COMPONENTS
*uc
, DWORD flags
, DWORD
*len
)
374 INTERNET_SCHEME scheme
;
379 DWORD scheme_len
= get_comp_length( uc
->dwSchemeLength
, 0, uc
->lpszScheme
);
381 scheme
= get_scheme( uc
->lpszScheme
, scheme_len
);
385 scheme
= uc
->nScheme
;
386 if (!scheme
) scheme
= INTERNET_SCHEME_HTTP
;
387 *len
+= lstrlenW( get_scheme_string( scheme
) );
389 *len
+= 3; /* "://" */
391 if (uc
->lpszUserName
)
393 *len
+= get_comp_length( uc
->dwUserNameLength
, 0, uc
->lpszUserName
);
398 if (uc
->lpszPassword
)
400 SetLastError( ERROR_INVALID_PARAMETER
);
404 if (uc
->lpszPassword
)
407 *len
+= get_comp_length( uc
->dwPasswordLength
, 0, uc
->lpszPassword
);
409 if (uc
->lpszHostName
)
411 *len
+= get_comp_length( uc
->dwHostNameLength
, 0, uc
->lpszHostName
);
413 if (!uses_default_port( scheme
, uc
->nPort
))
415 WCHAR port
[sizeof("65535")];
417 *len
+= swprintf( port
, ARRAY_SIZE(port
), L
"%u", uc
->nPort
);
420 if (uc
->lpszUrlPath
&& *uc
->lpszUrlPath
!= '/') *len
+= 1; /* '/' */
422 if (uc
->lpszUrlPath
) *len
+= get_comp_length( uc
->dwUrlPathLength
, flags
, uc
->lpszUrlPath
);
423 if (uc
->lpszExtraInfo
) *len
+= get_comp_length( uc
->dwExtraInfoLength
, flags
, uc
->lpszExtraInfo
);
427 /***********************************************************************
428 * WinHttpCreateUrl (winhttp.@)
430 BOOL WINAPI
WinHttpCreateUrl( URL_COMPONENTS
*uc
, DWORD flags
, WCHAR
*url
, DWORD
*required
)
432 DWORD len
, len_escaped
;
433 INTERNET_SCHEME scheme
;
435 TRACE( "%p, %#lx, %p, %p\n", uc
, flags
, url
, required
);
437 if (!uc
|| uc
->dwStructSize
!= sizeof(URL_COMPONENTS
) || !required
)
439 SetLastError( ERROR_INVALID_PARAMETER
);
443 if (!get_url_length( uc
, flags
, &len
)) return FALSE
;
448 SetLastError( ERROR_INSUFFICIENT_BUFFER
);
453 SetLastError( ERROR_INVALID_PARAMETER
);
461 len
= get_comp_length( uc
->dwSchemeLength
, 0, uc
->lpszScheme
);
462 memcpy( url
, uc
->lpszScheme
, len
* sizeof(WCHAR
) );
465 scheme
= get_scheme( uc
->lpszScheme
, len
);
469 const WCHAR
*schemeW
;
470 scheme
= uc
->nScheme
;
472 if (!scheme
) scheme
= INTERNET_SCHEME_HTTP
;
474 schemeW
= get_scheme_string( scheme
);
475 len
= lstrlenW( schemeW
);
476 memcpy( url
, schemeW
, len
* sizeof(WCHAR
) );
484 if (uc
->lpszUserName
)
486 len
= get_comp_length( uc
->dwUserNameLength
, 0, uc
->lpszUserName
);
487 memcpy( url
, uc
->lpszUserName
, len
* sizeof(WCHAR
) );
490 if (uc
->lpszPassword
)
493 len
= get_comp_length( uc
->dwPasswordLength
, 0, uc
->lpszPassword
);
494 memcpy( url
, uc
->lpszPassword
, len
* sizeof(WCHAR
) );
499 if (uc
->lpszHostName
)
501 len
= get_comp_length( uc
->dwHostNameLength
, 0, uc
->lpszHostName
);
502 memcpy( url
, uc
->lpszHostName
, len
* sizeof(WCHAR
) );
505 if (!uses_default_port( scheme
, uc
->nPort
))
508 url
+= swprintf( url
, sizeof("65535"), L
"%u", uc
->nPort
);
511 /* add slash between hostname and path if necessary */
512 if (uc
->lpszUrlPath
&& *uc
->lpszUrlPath
!= '/')
519 len
= get_comp_length( uc
->dwUrlPathLength
, 0, uc
->lpszUrlPath
);
520 if (flags
& ICU_ESCAPE
)
522 if (!escape_string( uc
->lpszUrlPath
, len
, url
, &len_escaped
))
524 SetLastError( ERROR_INVALID_PARAMETER
);
531 memcpy( url
, uc
->lpszUrlPath
, len
* sizeof(WCHAR
) );
535 if (uc
->lpszExtraInfo
)
537 len
= get_comp_length( uc
->dwExtraInfoLength
, 0, uc
->lpszExtraInfo
);
538 if (flags
& ICU_ESCAPE
)
540 if (!escape_string( uc
->lpszExtraInfo
, len
, url
, &len_escaped
))
542 SetLastError( ERROR_INVALID_PARAMETER
);
549 memcpy( url
, uc
->lpszExtraInfo
, len
* sizeof(WCHAR
) );
554 SetLastError( ERROR_SUCCESS
);