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
19 #include "winhttp_private.h"
21 static const WCHAR scheme_http
[] = {'h','t','t','p',0};
22 static const WCHAR scheme_https
[] = {'h','t','t','p','s',0};
24 static BOOL
set_component( WCHAR
**str
, DWORD
*str_len
, WCHAR
*value
, DWORD len
, DWORD flags
)
26 if (*str
&& !*str_len
)
28 set_last_error( ERROR_INVALID_PARAMETER
);
31 if (!*str_len
) return TRUE
;
34 if (len
&& *str_len
&& (flags
& (ICU_DECODE
|ICU_ESCAPE
)))
36 set_last_error( ERROR_INVALID_PARAMETER
);
44 if (len
> (*str_len
) - 1)
47 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
50 memcpy( *str
, value
, len
* sizeof(WCHAR
) );
57 static WCHAR
*decode_url( LPCWSTR url
, DWORD
*len
)
60 WCHAR hex
[3], *q
, *ret
;
62 if (!(ret
= heap_alloc( *len
* sizeof(WCHAR
) ))) return NULL
;
66 if (p
[0] == '%' && isxdigitW( p
[1] ) && isxdigitW( p
[2] ))
71 *q
++ = strtolW( hex
, NULL
, 16 );
85 static BOOL
need_escape( WCHAR c
)
87 if (isalnumW( c
)) return FALSE
;
89 if (c
<= 31 || c
>= 127) return TRUE
;
116 static DWORD
copy_escape( WCHAR
*dst
, const WCHAR
*src
, DWORD len
)
118 static const WCHAR hex
[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
123 for (i
= 0; i
< len
; i
++, p
++)
125 if (need_escape( src
[i
] ))
128 p
[1] = hex
[(src
[i
] >> 4) & 0xf];
129 p
[2] = hex
[src
[i
] & 0xf];
139 static WCHAR
*escape_url( LPCWSTR url
, DWORD
*len
)
144 if ((p
= q
= strrchrW( url
, '/' )))
148 if (need_escape( *q
)) *len
+= 2;
152 if (!(ret
= heap_alloc( (*len
+ 1) * sizeof(WCHAR
) ))) return NULL
;
153 if (!p
) strcpyW( ret
, url
);
156 memcpy( ret
, url
, (p
- url
) * sizeof(WCHAR
) );
157 copy_escape( ret
+ (p
- url
), p
, q
- p
);
162 /***********************************************************************
163 * WinHttpCrackUrl (winhttp.@)
165 BOOL WINAPI
WinHttpCrackUrl( LPCWSTR url
, DWORD len
, DWORD flags
, LPURL_COMPONENTSW uc
)
168 WCHAR
*p
, *q
, *r
, *url_decoded
= NULL
, *url_escaped
= NULL
;
169 INTERNET_SCHEME scheme
= 0;
171 TRACE("%s, %d, %x, %p\n", debugstr_w(url
), len
, flags
, uc
);
173 if (!url
|| !uc
|| uc
->dwStructSize
!= sizeof(URL_COMPONENTS
))
175 set_last_error( ERROR_INVALID_PARAMETER
);
178 if (!len
) len
= strlenW( url
);
180 if (flags
& ICU_ESCAPE
)
182 if (!(url_escaped
= escape_url( url
, &len
)))
184 set_last_error( ERROR_OUTOFMEMORY
);
189 else if (flags
& ICU_DECODE
)
191 if (!(url_decoded
= decode_url( url
, &len
)))
193 set_last_error( ERROR_OUTOFMEMORY
);
198 if (!(p
= strchrW( url
, ':' )))
200 set_last_error( ERROR_WINHTTP_UNRECOGNIZED_SCHEME
);
203 if (p
- url
== 4 && !strncmpiW( url
, scheme_http
, 4 )) scheme
= INTERNET_SCHEME_HTTP
;
204 else if (p
- url
== 5 && !strncmpiW( url
, scheme_https
, 5 )) scheme
= INTERNET_SCHEME_HTTPS
;
207 set_last_error( ERROR_WINHTTP_UNRECOGNIZED_SCHEME
);
210 if (!(set_component( &uc
->lpszScheme
, &uc
->dwSchemeLength
, (WCHAR
*)url
, p
- url
, flags
))) goto exit
;
213 if (!p
[0] || p
[0] != '/' || p
[1] != '/') goto exit
;
216 if (!p
[0]) goto exit
;
217 if ((q
= memchrW( p
, '@', len
- (p
- url
) )) && !(memchrW( p
, '/', q
- p
)))
219 if ((r
= memchrW( p
, ':', q
- p
)))
221 if (!(set_component( &uc
->lpszUserName
, &uc
->dwUserNameLength
, p
, r
- p
, flags
))) goto exit
;
223 if (!(set_component( &uc
->lpszPassword
, &uc
->dwPasswordLength
, r
, q
- r
, flags
))) goto exit
;
227 if (!(set_component( &uc
->lpszUserName
, &uc
->dwUserNameLength
, p
, q
- p
, flags
))) goto exit
;
228 if (!(set_component( &uc
->lpszPassword
, &uc
->dwPasswordLength
, NULL
, 0, flags
))) goto exit
;
234 if (!(set_component( &uc
->lpszUserName
, &uc
->dwUserNameLength
, NULL
, 0, flags
))) goto exit
;
235 if (!(set_component( &uc
->lpszPassword
, &uc
->dwPasswordLength
, NULL
, 0, flags
))) goto exit
;
237 if ((q
= memchrW( p
, '/', len
- (p
- url
) )))
239 if ((r
= memchrW( p
, ':', q
- p
)))
241 if (!(set_component( &uc
->lpszHostName
, &uc
->dwHostNameLength
, p
, r
- p
, flags
))) goto exit
;
243 uc
->nPort
= atoiW( r
);
247 if (!(set_component( &uc
->lpszHostName
, &uc
->dwHostNameLength
, p
, q
- p
, flags
))) goto exit
;
248 if (scheme
== INTERNET_SCHEME_HTTP
) uc
->nPort
= INTERNET_DEFAULT_HTTP_PORT
;
249 if (scheme
== INTERNET_SCHEME_HTTPS
) uc
->nPort
= INTERNET_DEFAULT_HTTPS_PORT
;
252 if ((r
= memchrW( q
, '?', len
- (q
- url
) )))
254 if (!(set_component( &uc
->lpszUrlPath
, &uc
->dwUrlPathLength
, q
, r
- q
, flags
))) goto exit
;
255 if (!(set_component( &uc
->lpszExtraInfo
, &uc
->dwExtraInfoLength
, r
, len
- (r
- url
), flags
))) goto exit
;
259 if (!(set_component( &uc
->lpszUrlPath
, &uc
->dwUrlPathLength
, q
, len
- (q
- url
), flags
))) goto exit
;
260 if (!(set_component( &uc
->lpszExtraInfo
, &uc
->dwExtraInfoLength
, (WCHAR
*)url
+ len
, 0, flags
))) goto exit
;
265 if ((r
= memchrW( p
, ':', len
- (p
- url
) )))
267 if (!(set_component( &uc
->lpszHostName
, &uc
->dwHostNameLength
, p
, r
- p
, flags
))) goto exit
;
269 uc
->nPort
= atoiW( r
);
273 if (!(set_component( &uc
->lpszHostName
, &uc
->dwHostNameLength
, p
, len
- (p
- url
), flags
))) goto exit
;
274 if (scheme
== INTERNET_SCHEME_HTTP
) uc
->nPort
= INTERNET_DEFAULT_HTTP_PORT
;
275 if (scheme
== INTERNET_SCHEME_HTTPS
) uc
->nPort
= INTERNET_DEFAULT_HTTPS_PORT
;
277 if (!(set_component( &uc
->lpszUrlPath
, &uc
->dwUrlPathLength
, (WCHAR
*)url
+ len
, 0, flags
))) goto exit
;
278 if (!(set_component( &uc
->lpszExtraInfo
, &uc
->dwExtraInfoLength
, (WCHAR
*)url
+ len
, 0, flags
))) goto exit
;
283 TRACE("scheme(%s) host(%s) port(%d) path(%s) extra(%s)\n",
284 debugstr_wn( uc
->lpszScheme
, uc
->dwSchemeLength
),
285 debugstr_wn( uc
->lpszHostName
, uc
->dwHostNameLength
),
287 debugstr_wn( uc
->lpszUrlPath
, uc
->dwUrlPathLength
),
288 debugstr_wn( uc
->lpszExtraInfo
, uc
->dwExtraInfoLength
));
291 if (ret
) uc
->nScheme
= scheme
;
292 heap_free( url_decoded
);
293 heap_free( url_escaped
);
297 static INTERNET_SCHEME
get_scheme( const WCHAR
*scheme
, DWORD len
)
299 if (!strncmpW( scheme
, scheme_http
, len
)) return INTERNET_SCHEME_HTTP
;
300 if (!strncmpW( scheme
, scheme_https
, len
)) return INTERNET_SCHEME_HTTPS
;
304 static const WCHAR
*get_scheme_string( INTERNET_SCHEME scheme
)
306 if (scheme
== INTERNET_SCHEME_HTTP
) return scheme_http
;
307 if (scheme
== INTERNET_SCHEME_HTTPS
) return scheme_https
;
311 static BOOL
uses_default_port( INTERNET_SCHEME scheme
, INTERNET_PORT port
)
313 if ((scheme
== INTERNET_SCHEME_HTTP
) && (port
== INTERNET_DEFAULT_HTTP_PORT
)) return TRUE
;
314 if ((scheme
== INTERNET_SCHEME_HTTPS
) && (port
== INTERNET_DEFAULT_HTTPS_PORT
)) return TRUE
;
318 static DWORD
comp_length( DWORD len
, DWORD flags
, WCHAR
*comp
)
323 ret
= len
? len
: strlenW( comp
);
324 if (!(flags
& ICU_ESCAPE
)) return ret
;
325 for (i
= 0; i
< len
; i
++) if (need_escape( comp
[i
] )) ret
+= 2;
329 static BOOL
calc_length( URL_COMPONENTS
*uc
, DWORD flags
, LPDWORD len
)
331 static const WCHAR formatW
[] = {'%','u',0};
332 INTERNET_SCHEME scheme
;
337 DWORD scheme_len
= comp_length( uc
->dwSchemeLength
, 0, uc
->lpszScheme
);
339 scheme
= get_scheme( uc
->lpszScheme
, scheme_len
);
343 scheme
= uc
->nScheme
;
344 if (!scheme
) scheme
= INTERNET_SCHEME_HTTP
;
345 *len
+= strlenW( get_scheme_string( scheme
) );
348 if (uc
->lpszHostName
) *len
+= 2; /* "//" */
350 if (uc
->lpszUserName
)
352 *len
+= comp_length( uc
->dwUserNameLength
, 0, uc
->lpszUserName
);
357 if (uc
->lpszPassword
)
359 set_last_error( ERROR_INVALID_PARAMETER
);
363 if (uc
->lpszPassword
)
366 *len
+= comp_length( uc
->dwPasswordLength
, 0, uc
->lpszPassword
);
368 if (uc
->lpszHostName
)
370 *len
+= comp_length( uc
->dwHostNameLength
, 0, uc
->lpszHostName
);
372 if (!uses_default_port( scheme
, uc
->nPort
))
374 WCHAR port
[sizeof("65535")];
376 sprintfW( port
, formatW
, uc
->nPort
);
377 *len
+= strlenW( port
);
380 if (uc
->lpszUrlPath
&& *uc
->lpszUrlPath
!= '/') *len
+= 1; /* '/' */
382 if (uc
->lpszUrlPath
) *len
+= comp_length( uc
->dwUrlPathLength
, flags
, uc
->lpszUrlPath
);
383 if (uc
->lpszExtraInfo
) *len
+= comp_length( uc
->dwExtraInfoLength
, flags
, uc
->lpszExtraInfo
);
387 /***********************************************************************
388 * WinHttpCreateUrl (winhttp.@)
390 BOOL WINAPI
WinHttpCreateUrl( LPURL_COMPONENTS uc
, DWORD flags
, LPWSTR url
, LPDWORD required
)
392 static const WCHAR formatW
[] = {'%','u',0};
393 static const WCHAR twoslashW
[] = {'/','/'};
396 INTERNET_SCHEME scheme
;
398 TRACE("%p, 0x%08x, %p, %p\n", uc
, flags
, url
, required
);
400 if (!uc
|| uc
->dwStructSize
!= sizeof(URL_COMPONENTS
) || !required
|| !url
)
402 set_last_error( ERROR_INVALID_PARAMETER
);
406 if (!calc_length( uc
, flags
, &len
)) return FALSE
;
411 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
419 len
= comp_length( uc
->dwSchemeLength
, 0, uc
->lpszScheme
);
420 memcpy( url
, uc
->lpszScheme
, len
* sizeof(WCHAR
) );
423 scheme
= get_scheme( uc
->lpszScheme
, len
);
427 const WCHAR
*schemeW
;
428 scheme
= uc
->nScheme
;
430 if (!scheme
) scheme
= INTERNET_SCHEME_HTTP
;
432 schemeW
= get_scheme_string( scheme
);
433 len
= strlenW( schemeW
);
434 memcpy( url
, schemeW
, len
* sizeof(WCHAR
) );
438 /* all schemes are followed by at least a colon */
442 if (uc
->lpszHostName
)
444 memcpy( url
, twoslashW
, sizeof(twoslashW
) );
445 url
+= sizeof(twoslashW
) / sizeof(twoslashW
[0]);
447 if (uc
->lpszUserName
)
449 len
= comp_length( uc
->dwUserNameLength
, 0, uc
->lpszUserName
);
450 memcpy( url
, uc
->lpszUserName
, len
* sizeof(WCHAR
) );
453 if (uc
->lpszPassword
)
458 len
= comp_length( uc
->dwPasswordLength
, 0, uc
->lpszPassword
);
459 memcpy( url
, uc
->lpszPassword
, len
* sizeof(WCHAR
) );
465 if (uc
->lpszHostName
)
467 len
= comp_length( uc
->dwHostNameLength
, 0, uc
->lpszHostName
);
468 memcpy( url
, uc
->lpszHostName
, len
* sizeof(WCHAR
) );
471 if (!uses_default_port( scheme
, uc
->nPort
))
473 WCHAR port
[sizeof("65535")];
475 sprintfW( port
, formatW
, uc
->nPort
);
479 len
= strlenW( port
);
480 memcpy( url
, port
, len
* sizeof(WCHAR
) );
484 /* add slash between hostname and path if necessary */
485 if (uc
->lpszUrlPath
&& *uc
->lpszUrlPath
!= '/')
493 len
= comp_length( uc
->dwUrlPathLength
, 0, uc
->lpszUrlPath
);
494 if (flags
& ICU_ESCAPE
) url
+= copy_escape( url
, uc
->lpszUrlPath
, len
);
497 memcpy( url
, uc
->lpszUrlPath
, len
* sizeof(WCHAR
) );
501 if (uc
->lpszExtraInfo
)
503 len
= comp_length( uc
->dwExtraInfoLength
, 0, uc
->lpszExtraInfo
);
504 if (flags
& ICU_ESCAPE
) url
+= copy_escape( url
, uc
->lpszExtraInfo
, len
);
507 memcpy( url
, uc
->lpszExtraInfo
, len
* sizeof(WCHAR
) );