dinput: Clear action mapping with SetDataFormat().
[wine.git] / dlls / winhttp / url.c
blob3e248bca9f76cecec64f373898745ec6b5866654
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 <wchar.h>
20 #include "windef.h"
21 #include "winbase.h"
22 #include "ws2tcpip.h"
23 #include "winreg.h"
24 #include "winhttp.h"
25 #include "shlwapi.h"
27 #include "wine/debug.h"
28 #include "winhttp_private.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(winhttp);
32 struct url_component
34 WCHAR **str;
35 DWORD *len;
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;
42 if (!*comp->str)
44 if (len && *comp->len && (flags & (ICU_DECODE|ICU_ESCAPE))) return ERROR_INVALID_PARAMETER;
45 *comp->str = value;
46 *comp->len = len;
48 else
50 if (len >= *comp->len)
52 *comp->len = len + 1;
53 *overflow = TRUE;
54 return ERROR_SUCCESS;
56 memcpy( *comp->str, value, len * sizeof(WCHAR) );
57 (*comp->str)[len] = 0;
58 *comp->len = len;
60 return ERROR_SUCCESS;
63 static WCHAR *decode_url( LPCWSTR url, DWORD *len )
65 const WCHAR *p = url;
66 WCHAR hex[3], *q, *ret;
68 if (!(ret = heap_alloc( *len * sizeof(WCHAR) ))) return NULL;
69 q = ret;
70 while (*len > 0)
72 if (p[0] == '%' && iswxdigit( p[1] ) && iswxdigit( p[2] ))
74 hex[0] = p[1];
75 hex[1] = p[2];
76 hex[2] = 0;
77 *q++ = wcstol( hex, NULL, 16 );
78 p += 3;
79 *len -= 3;
81 else
83 *q++ = *p++;
84 *len -= 1;
87 *len = q - ret;
88 return ret;
91 static inline BOOL need_escape( WCHAR ch )
93 const WCHAR *p = L" \"#%<>[\\]^`{|}~";
95 if (ch <= 31 || ch >= 127) return TRUE;
96 while (*p)
98 if (ch == *p++) return TRUE;
100 return FALSE;
103 static BOOL escape_string( const WCHAR *src, DWORD src_len, WCHAR *dst, DWORD *dst_len )
105 static const WCHAR hex[] = L"0123456789ABCDEF";
106 WCHAR *p = dst;
107 DWORD i;
109 *dst_len = src_len;
110 for (i = 0; i < src_len; i++)
112 if (src[i] > 0xff) return FALSE;
113 if (need_escape( src[i] ))
115 if (dst)
117 p[0] = '%';
118 p[1] = hex[(src[i] >> 4) & 0xf];
119 p[2] = hex[src[i] & 0xf];
120 p += 3;
122 *dst_len += 2;
124 else if (dst) *p++ = src[i];
127 if (dst) dst[*dst_len] = 0;
128 return TRUE;
131 static DWORD escape_url( const WCHAR *url, DWORD *len, WCHAR **ret )
133 const WCHAR *p;
134 DWORD len_base, len_path;
136 if ((p = wcsrchr( url, '/' )))
138 len_base = p - url;
139 if (!escape_string( p, *len - len_base, NULL, &len_path )) return ERROR_INVALID_PARAMETER;
141 else
143 len_base = *len;
144 len_path = 0;
147 if (!(*ret = heap_alloc( (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;
160 DWORD port = 0;
161 while (len && '0' <= *p && *p <= '9')
163 if ((port = port * 10 + *p - '0') > 65535) return ERROR_WINHTTP_INVALID_URL;
164 p++; len--;
166 *ret = port;
167 return ERROR_SUCCESS;
170 /***********************************************************************
171 * WinHttpCrackUrl (winhttp.@)
173 BOOL WINAPI WinHttpCrackUrl( LPCWSTR url, DWORD len, DWORD flags, LPURL_COMPONENTSW uc )
175 WCHAR *p, *q, *r, *url_decoded = NULL, *url_escaped = NULL;
176 INTERNET_SCHEME scheme_number = 0;
177 struct url_component scheme, username, password, hostname, path, extra;
178 BOOL overflow = FALSE;
179 DWORD err;
181 TRACE("%s, %d, %x, %p\n", debugstr_wn(url, len), len, flags, uc);
183 if (!url || !uc || uc->dwStructSize != sizeof(*uc))
185 SetLastError( ERROR_INVALID_PARAMETER );
186 return FALSE;
188 if (!len) len = lstrlenW( url );
190 if (flags & ICU_ESCAPE)
192 if ((err = escape_url( url, &len, &url_escaped )))
194 SetLastError( err );
195 return FALSE;
197 url = url_escaped;
199 else if (flags & ICU_DECODE)
201 if (!(url_decoded = decode_url( url, &len )))
203 SetLastError( ERROR_OUTOFMEMORY );
204 return FALSE;
206 url = url_decoded;
208 if (!(p = wcschr( url, ':' )))
210 SetLastError( ERROR_WINHTTP_UNRECOGNIZED_SCHEME );
211 return FALSE;
213 if (p - url == 4 && !wcsnicmp( url, L"http", 4 )) scheme_number = INTERNET_SCHEME_HTTP;
214 else if (p - url == 5 && !wcsnicmp( url, L"https", 5 )) scheme_number = INTERNET_SCHEME_HTTPS;
215 else
217 err = ERROR_WINHTTP_UNRECOGNIZED_SCHEME;
218 goto exit;
221 scheme.str = &uc->lpszScheme;
222 scheme.len = &uc->dwSchemeLength;
224 if ((err = set_component( &scheme, (WCHAR *)url, p - url, flags, &overflow ))) goto exit;
226 p++; /* skip ':' */
227 if (!p[0] || p[0] != '/' || p[1] != '/')
229 err = ERROR_WINHTTP_INVALID_URL;
230 goto exit;
232 p += 2;
233 if (!p[0])
235 err = ERROR_WINHTTP_INVALID_URL;
236 goto exit;
239 username.str = &uc->lpszUserName;
240 username.len = &uc->dwUserNameLength;
242 password.str = &uc->lpszPassword;
243 password.len = &uc->dwPasswordLength;
245 if ((q = wmemchr( p, '@', len - (p - url) )) && !(wmemchr( p, '/', q - p )))
248 if ((r = wmemchr( p, ':', q - p )))
250 if ((err = set_component( &username, p, r - p, flags, &overflow ))) goto exit;
251 r++;
252 if ((err = set_component( &password, r, q - r, flags, &overflow ))) goto exit;
254 else
256 if ((err = set_component( &username, p, q - p, flags, &overflow ))) goto exit;
257 if ((err = set_component( &password, NULL, 0, flags, &overflow ))) goto exit;
259 p = q + 1;
261 else
263 if ((err = set_component( &username, NULL, 0, flags, &overflow ))) goto exit;
264 if ((err = set_component( &password, NULL, 0, flags, &overflow ))) goto exit;
267 hostname.str = &uc->lpszHostName;
268 hostname.len = &uc->dwHostNameLength;
270 path.str = &uc->lpszUrlPath;
271 path.len = &uc->dwUrlPathLength;
273 extra.str = &uc->lpszExtraInfo;
274 extra.len = &uc->dwExtraInfoLength;
276 if ((q = wmemchr( p, '/', len - (p - url) )))
278 if ((r = wmemchr( p, ':', q - p )))
280 if ((err = set_component( &hostname, p, r - p, flags, &overflow ))) goto exit;
281 r++;
282 if ((err = parse_port( r, q - r, &uc->nPort ))) goto exit;
284 else
286 if ((err = set_component( &hostname, p, q - p, flags, &overflow ))) goto exit;
287 if (scheme_number == INTERNET_SCHEME_HTTP) uc->nPort = INTERNET_DEFAULT_HTTP_PORT;
288 if (scheme_number == INTERNET_SCHEME_HTTPS) uc->nPort = INTERNET_DEFAULT_HTTPS_PORT;
291 if ((r = wmemchr( q, '?', len - (q - url) )))
293 if (*extra.len)
295 if ((err = set_component( &path, q, r - q, flags, &overflow ))) goto exit;
296 if ((err = set_component( &extra, r, len - (r - url), flags, &overflow ))) goto exit;
298 else if ((err = set_component( &path, q, len - (q - url), flags, &overflow ))) goto exit;
300 else
302 if ((err = set_component( &path, q, len - (q - url), flags, &overflow ))) goto exit;
303 if ((err = set_component( &extra, (WCHAR *)url + len, 0, flags, &overflow ))) goto exit;
306 else
308 if ((r = wmemchr( p, ':', len - (p - url) )))
310 if ((err = set_component( &hostname, p, r - p, flags, &overflow ))) goto exit;
311 r++;
312 if ((err = parse_port( r, len - (r - url), &uc->nPort ))) goto exit;
314 else
316 if ((err = set_component( &hostname, p, len - (p - url), flags, &overflow ))) goto exit;
317 if (scheme_number == INTERNET_SCHEME_HTTP) uc->nPort = INTERNET_DEFAULT_HTTP_PORT;
318 if (scheme_number == INTERNET_SCHEME_HTTPS) uc->nPort = INTERNET_DEFAULT_HTTPS_PORT;
320 if ((err = set_component( &path, (WCHAR *)url + len, 0, flags, &overflow ))) goto exit;
321 if ((err = set_component( &extra, (WCHAR *)url + len, 0, flags, &overflow ))) goto exit;
324 TRACE("scheme(%s) host(%s) port(%d) path(%s) extra(%s)\n", debugstr_wn(*scheme.str, *scheme.len),
325 debugstr_wn(*hostname.str, *hostname.len ), uc->nPort, debugstr_wn(*path.str, *path.len),
326 debugstr_wn(*extra.str, *extra.len));
328 exit:
329 if (!err)
331 if (overflow) err = ERROR_INSUFFICIENT_BUFFER;
332 uc->nScheme = scheme_number;
334 heap_free( url_decoded );
335 heap_free( url_escaped );
336 SetLastError( err );
337 return !err;
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;
344 return 0;
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";
351 return NULL;
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;
358 return FALSE;
361 static DWORD get_comp_length( DWORD len, DWORD flags, WCHAR *comp )
363 DWORD ret;
364 unsigned int i;
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;
369 return ret;
372 static BOOL get_url_length( URL_COMPONENTS *uc, DWORD flags, DWORD *len )
374 INTERNET_SCHEME scheme;
376 *len = 0;
377 if (uc->lpszScheme)
379 DWORD scheme_len = get_comp_length( uc->dwSchemeLength, 0, uc->lpszScheme );
380 *len += scheme_len;
381 scheme = get_scheme( uc->lpszScheme, scheme_len );
383 else
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 );
394 *len += 1; /* "@" */
396 else
398 if (uc->lpszPassword)
400 SetLastError( ERROR_INVALID_PARAMETER );
401 return FALSE;
404 if (uc->lpszPassword)
406 *len += 1; /* ":" */
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 );
418 *len += 1; /* ":" */
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 );
424 return TRUE;
427 /***********************************************************************
428 * WinHttpCreateUrl (winhttp.@)
430 BOOL WINAPI WinHttpCreateUrl( LPURL_COMPONENTS uc, DWORD flags, LPWSTR url, LPDWORD required )
432 DWORD len, len_escaped;
433 INTERNET_SCHEME scheme;
435 TRACE("%p, 0x%08x, %p, %p\n", uc, flags, url, required);
437 if (!uc || uc->dwStructSize != sizeof(URL_COMPONENTS) || !required)
439 SetLastError( ERROR_INVALID_PARAMETER );
440 return FALSE;
443 if (!get_url_length( uc, flags, &len )) return FALSE;
445 if (*required < len)
447 *required = len + 1;
448 SetLastError( ERROR_INSUFFICIENT_BUFFER );
449 return FALSE;
451 if (!url)
453 SetLastError( ERROR_INVALID_PARAMETER );
454 return FALSE;
457 url[0] = 0;
458 *required = len;
459 if (uc->lpszScheme)
461 len = get_comp_length( uc->dwSchemeLength, 0, uc->lpszScheme );
462 memcpy( url, uc->lpszScheme, len * sizeof(WCHAR) );
463 url += len;
465 scheme = get_scheme( uc->lpszScheme, len );
467 else
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) );
477 url += len;
480 *url++ = ':';
481 *url++ = '/';
482 *url++ = '/';
484 if (uc->lpszUserName)
486 len = get_comp_length( uc->dwUserNameLength, 0, uc->lpszUserName );
487 memcpy( url, uc->lpszUserName, len * sizeof(WCHAR) );
488 url += len;
490 if (uc->lpszPassword)
492 *url++ = ':';
493 len = get_comp_length( uc->dwPasswordLength, 0, uc->lpszPassword );
494 memcpy( url, uc->lpszPassword, len * sizeof(WCHAR) );
495 url += len;
497 *url++ = '@';
499 if (uc->lpszHostName)
501 len = get_comp_length( uc->dwHostNameLength, 0, uc->lpszHostName );
502 memcpy( url, uc->lpszHostName, len * sizeof(WCHAR) );
503 url += len;
505 if (!uses_default_port( scheme, uc->nPort ))
507 *url++ = ':';
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 != '/')
514 *url++ = '/';
517 if (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 );
525 return FALSE;
527 url += len_escaped;
529 else
531 memcpy( url, uc->lpszUrlPath, len * sizeof(WCHAR) );
532 url += len;
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 );
543 return FALSE;
545 url += len_escaped;
547 else
549 memcpy( url, uc->lpszExtraInfo, len * sizeof(WCHAR) );
550 url += len;
553 *url = 0;
554 SetLastError( ERROR_SUCCESS );
555 return TRUE;