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
29 #include "wine/debug.h"
30 #include "winhttp_private.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(winhttp
);
34 static const WCHAR scheme_http
[] = {'h','t','t','p',0};
35 static const WCHAR scheme_https
[] = {'h','t','t','p','s',0};
43 static DWORD
set_component( struct url_component
*comp
, WCHAR
*value
, DWORD len
, DWORD flags
, BOOL
*overflow
)
45 if (*comp
->str
&& !*comp
->len
) return ERROR_INVALID_PARAMETER
;
46 if (!*comp
->len
) return ERROR_SUCCESS
;
49 if (len
&& *comp
->len
&& (flags
& (ICU_DECODE
|ICU_ESCAPE
))) return ERROR_INVALID_PARAMETER
;
55 if (len
>= *comp
->len
)
61 memcpy( *comp
->str
, value
, len
* sizeof(WCHAR
) );
62 (*comp
->str
)[len
] = 0;
68 static WCHAR
*decode_url( LPCWSTR url
, DWORD
*len
)
71 WCHAR hex
[3], *q
, *ret
;
73 if (!(ret
= heap_alloc( *len
* sizeof(WCHAR
) ))) return NULL
;
77 if (p
[0] == '%' && isxdigitW( p
[1] ) && isxdigitW( p
[2] ))
82 *q
++ = strtolW( hex
, NULL
, 16 );
96 static inline BOOL
need_escape( WCHAR ch
)
98 static const WCHAR escapes
[] = {' ','"','#','%','<','>','[','\\',']','^','`','{','|','}','~',0};
99 const WCHAR
*p
= escapes
;
101 if (ch
<= 31 || ch
>= 127) return TRUE
;
104 if (ch
== *p
++) return TRUE
;
109 static BOOL
escape_string( const WCHAR
*src
, DWORD src_len
, WCHAR
*dst
, DWORD
*dst_len
)
111 static const WCHAR hex
[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
116 for (i
= 0; i
< src_len
; i
++)
118 if (src
[i
] > 0xff) return FALSE
;
119 if (need_escape( src
[i
] ))
124 p
[1] = hex
[(src
[i
] >> 4) & 0xf];
125 p
[2] = hex
[src
[i
] & 0xf];
130 else if (dst
) *p
++ = src
[i
];
133 if (dst
) dst
[*dst_len
] = 0;
137 static DWORD
escape_url( const WCHAR
*url
, DWORD
*len
, WCHAR
**ret
)
140 DWORD len_base
, len_path
;
142 if ((p
= strrchrW( url
, '/' )))
145 if (!escape_string( p
, *len
- len_base
, NULL
, &len_path
)) return ERROR_INVALID_PARAMETER
;
153 if (!(*ret
= heap_alloc( (len_base
+ len_path
+ 1) * sizeof(WCHAR
) ))) return ERROR_OUTOFMEMORY
;
154 memcpy( *ret
, url
, len_base
* sizeof(WCHAR
) );
156 if (p
) escape_string( p
, *len
- (p
- url
), *ret
+ len_base
, &len_path
);
157 (*ret
)[len_base
+ len_path
] = 0;
159 *len
= len_base
+ len_path
;
160 return ERROR_SUCCESS
;
163 static DWORD
parse_port( const WCHAR
*str
, DWORD len
, INTERNET_PORT
*ret
)
165 const WCHAR
*p
= str
;
167 while (len
&& isdigitW( *p
))
169 if ((port
= port
* 10 + *p
- '0') > 65535) return ERROR_WINHTTP_INVALID_URL
;
173 return ERROR_SUCCESS
;
176 /***********************************************************************
177 * WinHttpCrackUrl (winhttp.@)
179 BOOL WINAPI
WinHttpCrackUrl( LPCWSTR url
, DWORD len
, DWORD flags
, LPURL_COMPONENTSW uc
)
181 WCHAR
*p
, *q
, *r
, *url_decoded
= NULL
, *url_escaped
= NULL
;
182 INTERNET_SCHEME scheme_number
= 0;
183 struct url_component scheme
, username
, password
, hostname
, path
, extra
;
184 BOOL overflow
= FALSE
;
187 TRACE("%s, %d, %x, %p\n", debugstr_wn(url
, len
), len
, flags
, uc
);
189 if (!url
|| !uc
|| uc
->dwStructSize
!= sizeof(*uc
))
191 SetLastError( ERROR_INVALID_PARAMETER
);
194 if (!len
) len
= strlenW( url
);
196 if (flags
& ICU_ESCAPE
)
198 if ((err
= escape_url( url
, &len
, &url_escaped
)))
205 else if (flags
& ICU_DECODE
)
207 if (!(url_decoded
= decode_url( url
, &len
)))
209 SetLastError( ERROR_OUTOFMEMORY
);
214 if (!(p
= strchrW( url
, ':' )))
216 SetLastError( ERROR_WINHTTP_UNRECOGNIZED_SCHEME
);
219 if (p
- url
== 4 && !strncmpiW( url
, scheme_http
, 4 )) scheme_number
= INTERNET_SCHEME_HTTP
;
220 else if (p
- url
== 5 && !strncmpiW( url
, scheme_https
, 5 )) scheme_number
= INTERNET_SCHEME_HTTPS
;
223 err
= ERROR_WINHTTP_UNRECOGNIZED_SCHEME
;
227 scheme
.str
= &uc
->lpszScheme
;
228 scheme
.len
= &uc
->dwSchemeLength
;
230 if ((err
= set_component( &scheme
, (WCHAR
*)url
, p
- url
, flags
, &overflow
))) goto exit
;
233 if (!p
[0] || p
[0] != '/' || p
[1] != '/')
235 err
= ERROR_WINHTTP_INVALID_URL
;
241 err
= ERROR_WINHTTP_INVALID_URL
;
245 username
.str
= &uc
->lpszUserName
;
246 username
.len
= &uc
->dwUserNameLength
;
248 password
.str
= &uc
->lpszPassword
;
249 password
.len
= &uc
->dwPasswordLength
;
251 if ((q
= memchrW( p
, '@', len
- (p
- url
) )) && !(memchrW( p
, '/', q
- p
)))
254 if ((r
= memchrW( p
, ':', q
- p
)))
256 if ((err
= set_component( &username
, p
, r
- p
, flags
, &overflow
))) goto exit
;
258 if ((err
= set_component( &password
, r
, q
- r
, flags
, &overflow
))) goto exit
;
262 if ((err
= set_component( &username
, p
, q
- p
, flags
, &overflow
))) goto exit
;
263 if ((err
= set_component( &password
, NULL
, 0, flags
, &overflow
))) goto exit
;
269 if ((err
= set_component( &username
, NULL
, 0, flags
, &overflow
))) goto exit
;
270 if ((err
= set_component( &password
, NULL
, 0, flags
, &overflow
))) goto exit
;
273 hostname
.str
= &uc
->lpszHostName
;
274 hostname
.len
= &uc
->dwHostNameLength
;
276 path
.str
= &uc
->lpszUrlPath
;
277 path
.len
= &uc
->dwUrlPathLength
;
279 extra
.str
= &uc
->lpszExtraInfo
;
280 extra
.len
= &uc
->dwExtraInfoLength
;
282 if ((q
= memchrW( p
, '/', len
- (p
- url
) )))
284 if ((r
= memchrW( p
, ':', q
- p
)))
286 if ((err
= set_component( &hostname
, p
, r
- p
, flags
, &overflow
))) goto exit
;
288 if ((err
= parse_port( r
, q
- r
, &uc
->nPort
))) goto exit
;
292 if ((err
= set_component( &hostname
, p
, q
- p
, flags
, &overflow
))) goto exit
;
293 if (scheme_number
== INTERNET_SCHEME_HTTP
) uc
->nPort
= INTERNET_DEFAULT_HTTP_PORT
;
294 if (scheme_number
== INTERNET_SCHEME_HTTPS
) uc
->nPort
= INTERNET_DEFAULT_HTTPS_PORT
;
297 if ((r
= memchrW( q
, '?', len
- (q
- url
) )))
301 if ((err
= set_component( &path
, q
, r
- q
, flags
, &overflow
))) goto exit
;
302 if ((err
= set_component( &extra
, r
, len
- (r
- url
), flags
, &overflow
))) goto exit
;
304 else if ((err
= set_component( &path
, q
, len
- (q
- url
), flags
, &overflow
))) goto exit
;
308 if ((err
= set_component( &path
, q
, len
- (q
- url
), flags
, &overflow
))) goto exit
;
309 if ((err
= set_component( &extra
, (WCHAR
*)url
+ len
, 0, flags
, &overflow
))) goto exit
;
314 if ((r
= memchrW( p
, ':', len
- (p
- url
) )))
316 if ((err
= set_component( &hostname
, p
, r
- p
, flags
, &overflow
))) goto exit
;
318 if ((err
= parse_port( r
, len
- (r
- url
), &uc
->nPort
))) goto exit
;
322 if ((err
= set_component( &hostname
, p
, len
- (p
- url
), flags
, &overflow
))) goto exit
;
323 if (scheme_number
== INTERNET_SCHEME_HTTP
) uc
->nPort
= INTERNET_DEFAULT_HTTP_PORT
;
324 if (scheme_number
== INTERNET_SCHEME_HTTPS
) uc
->nPort
= INTERNET_DEFAULT_HTTPS_PORT
;
326 if ((err
= set_component( &path
, (WCHAR
*)url
+ len
, 0, flags
, &overflow
))) goto exit
;
327 if ((err
= set_component( &extra
, (WCHAR
*)url
+ len
, 0, flags
, &overflow
))) goto exit
;
330 TRACE("scheme(%s) host(%s) port(%d) path(%s) extra(%s)\n", debugstr_wn(*scheme
.str
, *scheme
.len
),
331 debugstr_wn(*hostname
.str
, *hostname
.len
), uc
->nPort
, debugstr_wn(*path
.str
, *path
.len
),
332 debugstr_wn(*extra
.str
, *extra
.len
));
337 if (overflow
) err
= ERROR_INSUFFICIENT_BUFFER
;
338 uc
->nScheme
= scheme_number
;
340 heap_free( url_decoded
);
341 heap_free( url_escaped
);
346 static INTERNET_SCHEME
get_scheme( const WCHAR
*scheme
, DWORD len
)
348 if (!strncmpW( scheme
, scheme_http
, len
)) return INTERNET_SCHEME_HTTP
;
349 if (!strncmpW( scheme
, scheme_https
, len
)) return INTERNET_SCHEME_HTTPS
;
353 static const WCHAR
*get_scheme_string( INTERNET_SCHEME scheme
)
355 if (scheme
== INTERNET_SCHEME_HTTP
) return scheme_http
;
356 if (scheme
== INTERNET_SCHEME_HTTPS
) return scheme_https
;
360 static BOOL
uses_default_port( INTERNET_SCHEME scheme
, INTERNET_PORT port
)
362 if ((scheme
== INTERNET_SCHEME_HTTP
) && (port
== INTERNET_DEFAULT_HTTP_PORT
)) return TRUE
;
363 if ((scheme
== INTERNET_SCHEME_HTTPS
) && (port
== INTERNET_DEFAULT_HTTPS_PORT
)) return TRUE
;
367 static DWORD
get_comp_length( DWORD len
, DWORD flags
, WCHAR
*comp
)
372 ret
= len
? len
: strlenW( comp
);
373 if (!(flags
& ICU_ESCAPE
)) return ret
;
374 for (i
= 0; i
< len
; i
++) if (need_escape( comp
[i
] )) ret
+= 2;
378 static BOOL
get_url_length( URL_COMPONENTS
*uc
, DWORD flags
, DWORD
*len
)
380 static const WCHAR formatW
[] = {'%','u',0};
381 INTERNET_SCHEME scheme
;
386 DWORD scheme_len
= get_comp_length( uc
->dwSchemeLength
, 0, uc
->lpszScheme
);
388 scheme
= get_scheme( uc
->lpszScheme
, scheme_len
);
392 scheme
= uc
->nScheme
;
393 if (!scheme
) scheme
= INTERNET_SCHEME_HTTP
;
394 *len
+= strlenW( get_scheme_string( scheme
) );
396 *len
+= 3; /* "://" */
398 if (uc
->lpszUserName
)
400 *len
+= get_comp_length( uc
->dwUserNameLength
, 0, uc
->lpszUserName
);
405 if (uc
->lpszPassword
)
407 SetLastError( ERROR_INVALID_PARAMETER
);
411 if (uc
->lpszPassword
)
414 *len
+= get_comp_length( uc
->dwPasswordLength
, 0, uc
->lpszPassword
);
416 if (uc
->lpszHostName
)
418 *len
+= get_comp_length( uc
->dwHostNameLength
, 0, uc
->lpszHostName
);
420 if (!uses_default_port( scheme
, uc
->nPort
))
422 WCHAR port
[sizeof("65535")];
424 *len
+= sprintfW( port
, formatW
, uc
->nPort
);
427 if (uc
->lpszUrlPath
&& *uc
->lpszUrlPath
!= '/') *len
+= 1; /* '/' */
429 if (uc
->lpszUrlPath
) *len
+= get_comp_length( uc
->dwUrlPathLength
, flags
, uc
->lpszUrlPath
);
430 if (uc
->lpszExtraInfo
) *len
+= get_comp_length( uc
->dwExtraInfoLength
, flags
, uc
->lpszExtraInfo
);
434 /***********************************************************************
435 * WinHttpCreateUrl (winhttp.@)
437 BOOL WINAPI
WinHttpCreateUrl( LPURL_COMPONENTS uc
, DWORD flags
, LPWSTR url
, LPDWORD required
)
439 static const WCHAR formatW
[] = {'%','u',0};
440 DWORD len
, len_escaped
;
441 INTERNET_SCHEME scheme
;
443 TRACE("%p, 0x%08x, %p, %p\n", uc
, flags
, url
, required
);
445 if (!uc
|| uc
->dwStructSize
!= sizeof(URL_COMPONENTS
) || !required
)
447 SetLastError( ERROR_INVALID_PARAMETER
);
451 if (!get_url_length( uc
, flags
, &len
)) return FALSE
;
456 SetLastError( ERROR_INSUFFICIENT_BUFFER
);
461 SetLastError( ERROR_INVALID_PARAMETER
);
469 len
= get_comp_length( uc
->dwSchemeLength
, 0, uc
->lpszScheme
);
470 memcpy( url
, uc
->lpszScheme
, len
* sizeof(WCHAR
) );
473 scheme
= get_scheme( uc
->lpszScheme
, len
);
477 const WCHAR
*schemeW
;
478 scheme
= uc
->nScheme
;
480 if (!scheme
) scheme
= INTERNET_SCHEME_HTTP
;
482 schemeW
= get_scheme_string( scheme
);
483 len
= strlenW( schemeW
);
484 memcpy( url
, schemeW
, len
* sizeof(WCHAR
) );
492 if (uc
->lpszUserName
)
494 len
= get_comp_length( uc
->dwUserNameLength
, 0, uc
->lpszUserName
);
495 memcpy( url
, uc
->lpszUserName
, len
* sizeof(WCHAR
) );
498 if (uc
->lpszPassword
)
501 len
= get_comp_length( uc
->dwPasswordLength
, 0, uc
->lpszPassword
);
502 memcpy( url
, uc
->lpszPassword
, len
* sizeof(WCHAR
) );
507 if (uc
->lpszHostName
)
509 len
= get_comp_length( uc
->dwHostNameLength
, 0, uc
->lpszHostName
);
510 memcpy( url
, uc
->lpszHostName
, len
* sizeof(WCHAR
) );
513 if (!uses_default_port( scheme
, uc
->nPort
))
516 url
+= sprintfW( url
, formatW
, uc
->nPort
);
519 /* add slash between hostname and path if necessary */
520 if (uc
->lpszUrlPath
&& *uc
->lpszUrlPath
!= '/')
527 len
= get_comp_length( uc
->dwUrlPathLength
, 0, uc
->lpszUrlPath
);
528 if (flags
& ICU_ESCAPE
)
530 if (!escape_string( uc
->lpszUrlPath
, len
, url
, &len_escaped
))
532 SetLastError( ERROR_INVALID_PARAMETER
);
539 memcpy( url
, uc
->lpszUrlPath
, len
* sizeof(WCHAR
) );
543 if (uc
->lpszExtraInfo
)
545 len
= get_comp_length( uc
->dwExtraInfoLength
, 0, uc
->lpszExtraInfo
);
546 if (flags
& ICU_ESCAPE
)
548 if (!escape_string( uc
->lpszExtraInfo
, len
, url
, &len_escaped
))
550 SetLastError( ERROR_INVALID_PARAMETER
);
557 memcpy( url
, uc
->lpszExtraInfo
, len
* sizeof(WCHAR
) );
562 SetLastError( ERROR_SUCCESS
);