libwine: Fix double byte default char initialization.
[wine.git] / dlls / winhttp / url.c
blob8469ea37200c886d1251e96ff7716367d19e65c4
1 /*
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 "config.h"
20 #include <stdarg.h>
22 #include "wine/debug.h"
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winreg.h"
27 #include "winhttp.h"
28 #include "shlwapi.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};
37 static BOOL set_component( WCHAR **str, DWORD *str_len, WCHAR *value, DWORD len, DWORD flags )
39 if (*str && !*str_len)
41 set_last_error( ERROR_INVALID_PARAMETER );
42 return FALSE;
44 if (!*str_len) return TRUE;
45 if (!*str)
47 if (len && *str_len && (flags & (ICU_DECODE|ICU_ESCAPE)))
49 set_last_error( ERROR_INVALID_PARAMETER );
50 return FALSE;
52 *str = value;
53 *str_len = len;
55 else
57 if (len > (*str_len) - 1)
59 *str_len = len + 1;
60 set_last_error( ERROR_INSUFFICIENT_BUFFER );
61 return FALSE;
63 memcpy( *str, value, len * sizeof(WCHAR) );
64 (*str)[len] = 0;
65 *str_len = len;
67 return TRUE;
70 static WCHAR *decode_url( LPCWSTR url, DWORD *len )
72 const WCHAR *p = url;
73 WCHAR hex[3], *q, *ret;
75 if (!(ret = heap_alloc( *len * sizeof(WCHAR) ))) return NULL;
76 q = ret;
77 while (*len > 0)
79 if (p[0] == '%' && isxdigitW( p[1] ) && isxdigitW( p[2] ))
81 hex[0] = p[1];
82 hex[1] = p[2];
83 hex[2] = 0;
84 *q++ = strtolW( hex, NULL, 16 );
85 p += 3;
86 *len -= 3;
88 else
90 *q++ = *p++;
91 *len -= 1;
94 *len = q - ret;
95 return ret;
98 static BOOL need_escape( WCHAR c )
100 if (isalnumW( c )) return FALSE;
102 if (c <= 31 || c >= 127) return TRUE;
103 else
105 switch (c)
107 case ' ':
108 case '"':
109 case '#':
110 case '%':
111 case '<':
112 case '>':
113 case ']':
114 case '\\':
115 case '[':
116 case '^':
117 case '`':
118 case '{':
119 case '|':
120 case '}':
121 case '~':
122 return TRUE;
123 default:
124 return FALSE;
129 static DWORD copy_escape( WCHAR *dst, const WCHAR *src, DWORD len )
131 static const WCHAR hex[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
132 DWORD ret = len;
133 unsigned int i;
134 WCHAR *p = dst;
136 for (i = 0; i < len; i++, p++)
138 if (need_escape( src[i] ))
140 p[0] = '%';
141 p[1] = hex[(src[i] >> 4) & 0xf];
142 p[2] = hex[src[i] & 0xf];
143 ret += 2;
144 p += 2;
146 else *p = src[i];
148 dst[ret] = 0;
149 return ret;
152 static WCHAR *escape_url( LPCWSTR url, DWORD *len )
154 WCHAR *ret;
155 const WCHAR *p, *q;
157 if ((p = q = strrchrW( url, '/' )))
159 while (*q)
161 if (need_escape( *q )) *len += 2;
162 q++;
165 if (!(ret = heap_alloc( (*len + 1) * sizeof(WCHAR) ))) return NULL;
166 if (!p) strcpyW( ret, url );
167 else
169 memcpy( ret, url, (p - url) * sizeof(WCHAR) );
170 copy_escape( ret + (p - url), p, q - p );
172 return ret;
175 /***********************************************************************
176 * WinHttpCrackUrl (winhttp.@)
178 BOOL WINAPI WinHttpCrackUrl( LPCWSTR url, DWORD len, DWORD flags, LPURL_COMPONENTSW uc )
180 BOOL ret = FALSE;
181 WCHAR *p, *q, *r, *url_decoded = NULL, *url_escaped = NULL;
182 INTERNET_SCHEME scheme = 0;
184 TRACE("%s, %d, %x, %p\n", debugstr_w(url), len, flags, uc);
186 if (!url || !uc || uc->dwStructSize != sizeof(URL_COMPONENTS))
188 set_last_error( ERROR_INVALID_PARAMETER );
189 return FALSE;
191 if (!len) len = strlenW( url );
193 if (flags & ICU_ESCAPE)
195 if (!(url_escaped = escape_url( url, &len )))
197 set_last_error( ERROR_OUTOFMEMORY );
198 return FALSE;
200 url = url_escaped;
202 else if (flags & ICU_DECODE)
204 if (!(url_decoded = decode_url( url, &len )))
206 set_last_error( ERROR_OUTOFMEMORY );
207 return FALSE;
209 url = url_decoded;
211 if (!(p = strchrW( url, ':' )))
213 set_last_error( ERROR_WINHTTP_UNRECOGNIZED_SCHEME );
214 return FALSE;
216 if (p - url == 4 && !strncmpiW( url, scheme_http, 4 )) scheme = INTERNET_SCHEME_HTTP;
217 else if (p - url == 5 && !strncmpiW( url, scheme_https, 5 )) scheme = INTERNET_SCHEME_HTTPS;
218 else
220 set_last_error( ERROR_WINHTTP_UNRECOGNIZED_SCHEME );
221 goto exit;
223 if (!(set_component( &uc->lpszScheme, &uc->dwSchemeLength, (WCHAR *)url, p - url, flags ))) goto exit;
225 p++; /* skip ':' */
226 if (!p[0] || p[0] != '/' || p[1] != '/') goto exit;
227 p += 2;
229 if (!p[0]) goto exit;
230 if ((q = memchrW( p, '@', len - (p - url) )) && !(memchrW( p, '/', q - p )))
232 if ((r = memchrW( p, ':', q - p )))
234 if (!(set_component( &uc->lpszUserName, &uc->dwUserNameLength, p, r - p, flags ))) goto exit;
235 r++;
236 if (!(set_component( &uc->lpszPassword, &uc->dwPasswordLength, r, q - r, flags ))) goto exit;
238 else
240 if (!(set_component( &uc->lpszUserName, &uc->dwUserNameLength, p, q - p, flags ))) goto exit;
241 if (!(set_component( &uc->lpszPassword, &uc->dwPasswordLength, NULL, 0, flags ))) goto exit;
243 p = q + 1;
245 else
247 if (!(set_component( &uc->lpszUserName, &uc->dwUserNameLength, NULL, 0, flags ))) goto exit;
248 if (!(set_component( &uc->lpszPassword, &uc->dwPasswordLength, NULL, 0, flags ))) goto exit;
250 if ((q = memchrW( p, '/', len - (p - url) )))
252 if ((r = memchrW( p, ':', q - p )))
254 if (!(set_component( &uc->lpszHostName, &uc->dwHostNameLength, p, r - p, flags ))) goto exit;
255 r++;
256 uc->nPort = atoiW( r );
258 else
260 if (!(set_component( &uc->lpszHostName, &uc->dwHostNameLength, p, q - p, flags ))) goto exit;
261 if (scheme == INTERNET_SCHEME_HTTP) uc->nPort = INTERNET_DEFAULT_HTTP_PORT;
262 if (scheme == INTERNET_SCHEME_HTTPS) uc->nPort = INTERNET_DEFAULT_HTTPS_PORT;
265 if ((r = memchrW( q, '?', len - (q - url) )))
267 if (!(set_component( &uc->lpszUrlPath, &uc->dwUrlPathLength, q, r - q, flags ))) goto exit;
268 if (!(set_component( &uc->lpszExtraInfo, &uc->dwExtraInfoLength, r, len - (r - url), flags ))) goto exit;
270 else
272 if (!(set_component( &uc->lpszUrlPath, &uc->dwUrlPathLength, q, len - (q - url), flags ))) goto exit;
273 if (!(set_component( &uc->lpszExtraInfo, &uc->dwExtraInfoLength, (WCHAR *)url + len, 0, flags ))) goto exit;
276 else
278 if ((r = memchrW( p, ':', len - (p - url) )))
280 if (!(set_component( &uc->lpszHostName, &uc->dwHostNameLength, p, r - p, flags ))) goto exit;
281 r++;
282 uc->nPort = atoiW( r );
284 else
286 if (!(set_component( &uc->lpszHostName, &uc->dwHostNameLength, p, len - (p - url), flags ))) goto exit;
287 if (scheme == INTERNET_SCHEME_HTTP) uc->nPort = INTERNET_DEFAULT_HTTP_PORT;
288 if (scheme == INTERNET_SCHEME_HTTPS) uc->nPort = INTERNET_DEFAULT_HTTPS_PORT;
290 if (!(set_component( &uc->lpszUrlPath, &uc->dwUrlPathLength, (WCHAR *)url + len, 0, flags ))) goto exit;
291 if (!(set_component( &uc->lpszExtraInfo, &uc->dwExtraInfoLength, (WCHAR *)url + len, 0, flags ))) goto exit;
294 ret = TRUE;
296 TRACE("scheme(%s) host(%s) port(%d) path(%s) extra(%s)\n",
297 debugstr_wn( uc->lpszScheme, uc->dwSchemeLength ),
298 debugstr_wn( uc->lpszHostName, uc->dwHostNameLength ),
299 uc->nPort,
300 debugstr_wn( uc->lpszUrlPath, uc->dwUrlPathLength ),
301 debugstr_wn( uc->lpszExtraInfo, uc->dwExtraInfoLength ));
303 exit:
304 if (ret) uc->nScheme = scheme;
305 heap_free( url_decoded );
306 heap_free( url_escaped );
307 if (ret) set_last_error( ERROR_SUCCESS );
308 return ret;
311 static INTERNET_SCHEME get_scheme( const WCHAR *scheme, DWORD len )
313 if (!strncmpW( scheme, scheme_http, len )) return INTERNET_SCHEME_HTTP;
314 if (!strncmpW( scheme, scheme_https, len )) return INTERNET_SCHEME_HTTPS;
315 return 0;
318 static const WCHAR *get_scheme_string( INTERNET_SCHEME scheme )
320 if (scheme == INTERNET_SCHEME_HTTP) return scheme_http;
321 if (scheme == INTERNET_SCHEME_HTTPS) return scheme_https;
322 return NULL;
325 static BOOL uses_default_port( INTERNET_SCHEME scheme, INTERNET_PORT port )
327 if ((scheme == INTERNET_SCHEME_HTTP) && (port == INTERNET_DEFAULT_HTTP_PORT)) return TRUE;
328 if ((scheme == INTERNET_SCHEME_HTTPS) && (port == INTERNET_DEFAULT_HTTPS_PORT)) return TRUE;
329 return FALSE;
332 static DWORD comp_length( DWORD len, DWORD flags, WCHAR *comp )
334 DWORD ret;
335 unsigned int i;
337 ret = len ? len : strlenW( comp );
338 if (!(flags & ICU_ESCAPE)) return ret;
339 for (i = 0; i < len; i++) if (need_escape( comp[i] )) ret += 2;
340 return ret;
343 static BOOL calc_length( URL_COMPONENTS *uc, DWORD flags, LPDWORD len )
345 static const WCHAR formatW[] = {'%','u',0};
346 INTERNET_SCHEME scheme;
348 *len = 0;
349 if (uc->lpszScheme)
351 DWORD scheme_len = comp_length( uc->dwSchemeLength, 0, uc->lpszScheme );
352 *len += scheme_len;
353 scheme = get_scheme( uc->lpszScheme, scheme_len );
355 else
357 scheme = uc->nScheme;
358 if (!scheme) scheme = INTERNET_SCHEME_HTTP;
359 *len += strlenW( get_scheme_string( scheme ) );
361 *len += 1; /* ':' */
362 if (uc->lpszHostName) *len += 2; /* "//" */
364 if (uc->lpszUserName)
366 *len += comp_length( uc->dwUserNameLength, 0, uc->lpszUserName );
367 *len += 1; /* "@" */
369 else
371 if (uc->lpszPassword)
373 set_last_error( ERROR_INVALID_PARAMETER );
374 return FALSE;
377 if (uc->lpszPassword)
379 *len += 1; /* ":" */
380 *len += comp_length( uc->dwPasswordLength, 0, uc->lpszPassword );
382 if (uc->lpszHostName)
384 *len += comp_length( uc->dwHostNameLength, 0, uc->lpszHostName );
386 if (!uses_default_port( scheme, uc->nPort ))
388 WCHAR port[sizeof("65535")];
390 sprintfW( port, formatW, uc->nPort );
391 *len += strlenW( port );
392 *len += 1; /* ":" */
394 if (uc->lpszUrlPath && *uc->lpszUrlPath != '/') *len += 1; /* '/' */
396 if (uc->lpszUrlPath) *len += comp_length( uc->dwUrlPathLength, flags, uc->lpszUrlPath );
397 if (uc->lpszExtraInfo) *len += comp_length( uc->dwExtraInfoLength, flags, uc->lpszExtraInfo );
398 return TRUE;
401 /***********************************************************************
402 * WinHttpCreateUrl (winhttp.@)
404 BOOL WINAPI WinHttpCreateUrl( LPURL_COMPONENTS uc, DWORD flags, LPWSTR url, LPDWORD required )
406 static const WCHAR formatW[] = {'%','u',0};
407 static const WCHAR twoslashW[] = {'/','/'};
409 DWORD len;
410 INTERNET_SCHEME scheme;
412 TRACE("%p, 0x%08x, %p, %p\n", uc, flags, url, required);
414 if (!uc || uc->dwStructSize != sizeof(URL_COMPONENTS) || !required || !url)
416 set_last_error( ERROR_INVALID_PARAMETER );
417 return FALSE;
420 if (!calc_length( uc, flags, &len )) return FALSE;
422 if (*required < len)
424 *required = len + 1;
425 set_last_error( ERROR_INSUFFICIENT_BUFFER );
426 return FALSE;
429 url[0] = 0;
430 *required = len;
431 if (uc->lpszScheme)
433 len = comp_length( uc->dwSchemeLength, 0, uc->lpszScheme );
434 memcpy( url, uc->lpszScheme, len * sizeof(WCHAR) );
435 url += len;
437 scheme = get_scheme( uc->lpszScheme, len );
439 else
441 const WCHAR *schemeW;
442 scheme = uc->nScheme;
444 if (!scheme) scheme = INTERNET_SCHEME_HTTP;
446 schemeW = get_scheme_string( scheme );
447 len = strlenW( schemeW );
448 memcpy( url, schemeW, len * sizeof(WCHAR) );
449 url += len;
452 /* all schemes are followed by at least a colon */
453 *url = ':';
454 url++;
456 if (uc->lpszHostName)
458 memcpy( url, twoslashW, sizeof(twoslashW) );
459 url += sizeof(twoslashW) / sizeof(twoslashW[0]);
461 if (uc->lpszUserName)
463 len = comp_length( uc->dwUserNameLength, 0, uc->lpszUserName );
464 memcpy( url, uc->lpszUserName, len * sizeof(WCHAR) );
465 url += len;
467 if (uc->lpszPassword)
469 *url = ':';
470 url++;
472 len = comp_length( uc->dwPasswordLength, 0, uc->lpszPassword );
473 memcpy( url, uc->lpszPassword, len * sizeof(WCHAR) );
474 url += len;
476 *url = '@';
477 url++;
479 if (uc->lpszHostName)
481 len = comp_length( uc->dwHostNameLength, 0, uc->lpszHostName );
482 memcpy( url, uc->lpszHostName, len * sizeof(WCHAR) );
483 url += len;
485 if (!uses_default_port( scheme, uc->nPort ))
487 WCHAR port[sizeof("65535")];
489 sprintfW( port, formatW, uc->nPort );
490 *url = ':';
491 url++;
493 len = strlenW( port );
494 memcpy( url, port, len * sizeof(WCHAR) );
495 url += len;
498 /* add slash between hostname and path if necessary */
499 if (uc->lpszUrlPath && *uc->lpszUrlPath != '/')
501 *url = '/';
502 url++;
505 if (uc->lpszUrlPath)
507 len = comp_length( uc->dwUrlPathLength, 0, uc->lpszUrlPath );
508 if (flags & ICU_ESCAPE) url += copy_escape( url, uc->lpszUrlPath, len );
509 else
511 memcpy( url, uc->lpszUrlPath, len * sizeof(WCHAR) );
512 url += len;
515 if (uc->lpszExtraInfo)
517 len = comp_length( uc->dwExtraInfoLength, 0, uc->lpszExtraInfo );
518 if (flags & ICU_ESCAPE) url += copy_escape( url, uc->lpszExtraInfo, len );
519 else
521 memcpy( url, uc->lpszExtraInfo, len * sizeof(WCHAR) );
522 url += len;
525 *url = 0;
526 set_last_error( ERROR_SUCCESS );
527 return TRUE;