quartz: Free two assert calls from having side effects.
[wine/testsucceed.git] / dlls / winhttp / url.c
bloba662855cd1e122351aa7c3a9ba247b81d02ec018
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)
41 if (len && (flags & ICU_DECODE))
43 set_last_error( ERROR_INVALID_PARAMETER );
44 return FALSE;
46 *str = value;
47 *str_len = len;
49 else
51 if (len > (*str_len) - 1)
53 *str_len = len + 1;
54 set_last_error( ERROR_INSUFFICIENT_BUFFER );
55 return FALSE;
57 memcpy( *str, value, len * sizeof(WCHAR) );
58 (*str)[len] = 0;
59 *str_len = len;
61 return TRUE;
64 static WCHAR *decode_url( LPCWSTR url, DWORD *len )
66 const WCHAR *p = url;
67 WCHAR hex[3], *q, *ret;
69 if (!(ret = heap_alloc( *len * sizeof(WCHAR) ))) return NULL;
70 q = ret;
71 while (*len > 0)
73 if (p[0] == '%' && isxdigitW( p[1] ) && isxdigitW( p[2] ))
75 hex[0] = p[1];
76 hex[1] = p[2];
77 hex[2] = 0;
78 *q++ = strtolW( hex, NULL, 16 );
79 p += 3;
80 *len -= 3;
82 else
84 *q++ = *p++;
85 *len -= 1;
88 *len = q - ret;
89 return ret;
92 /***********************************************************************
93 * WinHttpCrackUrl (winhttp.@)
95 BOOL WINAPI WinHttpCrackUrl( LPCWSTR url, DWORD len, DWORD flags, LPURL_COMPONENTSW uc )
97 BOOL ret = FALSE;
98 WCHAR *p, *q, *r, *url_decoded = NULL;
100 TRACE("%s, %d, %x, %p\n", debugstr_w(url), len, flags, uc);
102 if (flags & ICU_ESCAPE) FIXME("flag ICU_ESCAPE not supported\n");
104 if (!url || !uc || uc->dwStructSize != sizeof(URL_COMPONENTS))
106 set_last_error( ERROR_INVALID_PARAMETER );
107 return FALSE;
109 if (!len) len = strlenW( url );
111 if (flags & ICU_DECODE)
113 if (!(url_decoded = decode_url( url, &len )))
115 set_last_error( ERROR_OUTOFMEMORY );
116 return FALSE;
118 url = url_decoded;
120 if (!(p = strchrW( url, ':' )))
122 set_last_error( ERROR_WINHTTP_UNRECOGNIZED_SCHEME );
123 return FALSE;
125 if (p - url == 4 && !strncmpiW( url, scheme_http, 4 )) uc->nScheme = INTERNET_SCHEME_HTTP;
126 else if (p - url == 5 && !strncmpiW( url, scheme_https, 5 )) uc->nScheme = INTERNET_SCHEME_HTTPS;
127 else
129 set_last_error( ERROR_WINHTTP_UNRECOGNIZED_SCHEME );
130 goto exit;
132 if (!(set_component( &uc->lpszScheme, &uc->dwSchemeLength, (WCHAR *)url, p - url, flags ))) goto exit;
134 p++; /* skip ':' */
135 if (!p[0] || p[0] != '/' || p[1] != '/') goto exit;
136 p += 2;
138 if (!p[0]) goto exit;
139 if ((q = memchrW( p, '@', len - (p - url) )) && !(memchrW( p, '/', q - p )))
141 if ((r = memchrW( p, ':', q - p )))
143 if (!(set_component( &uc->lpszUserName, &uc->dwUserNameLength, p, r - p, flags ))) goto exit;
144 r++;
145 if (!(set_component( &uc->lpszPassword, &uc->dwPasswordLength, r, q - r, flags ))) goto exit;
147 else
149 if (!(set_component( &uc->lpszUserName, &uc->dwUserNameLength, p, q - p, flags ))) goto exit;
150 if (!(set_component( &uc->lpszPassword, &uc->dwPasswordLength, NULL, 0, flags ))) goto exit;
152 p = q + 1;
154 else
156 if (!(set_component( &uc->lpszUserName, &uc->dwUserNameLength, NULL, 0, flags ))) goto exit;
157 if (!(set_component( &uc->lpszPassword, &uc->dwPasswordLength, NULL, 0, flags ))) goto exit;
159 if ((q = memchrW( p, '/', len - (p - url) )))
161 if ((r = memchrW( p, ':', q - p )))
163 if (!(set_component( &uc->lpszHostName, &uc->dwHostNameLength, p, r - p, flags ))) goto exit;
164 r++;
165 uc->nPort = atoiW( r );
167 else
169 if (!(set_component( &uc->lpszHostName, &uc->dwHostNameLength, p, q - p, flags ))) goto exit;
170 if (uc->nScheme == INTERNET_SCHEME_HTTP) uc->nPort = INTERNET_DEFAULT_HTTP_PORT;
171 if (uc->nScheme == INTERNET_SCHEME_HTTPS) uc->nPort = INTERNET_DEFAULT_HTTPS_PORT;
174 if ((r = memchrW( q, '?', len - (q - url) )))
176 if (!(set_component( &uc->lpszUrlPath, &uc->dwUrlPathLength, q, r - q, flags ))) goto exit;
177 if (!(set_component( &uc->lpszExtraInfo, &uc->dwExtraInfoLength, r, len - (r - url), flags ))) goto exit;
179 else
181 if (!(set_component( &uc->lpszUrlPath, &uc->dwUrlPathLength, q, len - (q - url), flags ))) goto exit;
182 if (!(set_component( &uc->lpszExtraInfo, &uc->dwExtraInfoLength, (WCHAR *)url + len, 0, flags ))) goto exit;
185 else
187 if ((r = memchrW( p, ':', len - (p - url) )))
189 if (!(set_component( &uc->lpszHostName, &uc->dwHostNameLength, p, r - p, flags ))) goto exit;
190 r++;
191 uc->nPort = atoiW( r );
193 else
195 if (!(set_component( &uc->lpszHostName, &uc->dwHostNameLength, p, len - (p - url), flags ))) goto exit;
196 if (uc->nScheme == INTERNET_SCHEME_HTTP) uc->nPort = INTERNET_DEFAULT_HTTP_PORT;
197 if (uc->nScheme == INTERNET_SCHEME_HTTPS) uc->nPort = INTERNET_DEFAULT_HTTPS_PORT;
199 if (!(set_component( &uc->lpszUrlPath, &uc->dwUrlPathLength, (WCHAR *)url + len, 0, flags ))) goto exit;
200 if (!(set_component( &uc->lpszExtraInfo, &uc->dwExtraInfoLength, (WCHAR *)url + len, 0, flags ))) goto exit;
203 ret = TRUE;
205 TRACE("scheme(%s) host(%s) port(%d) path(%s) extra(%s)\n",
206 debugstr_wn( uc->lpszScheme, uc->dwSchemeLength ),
207 debugstr_wn( uc->lpszHostName, uc->dwHostNameLength ),
208 uc->nPort,
209 debugstr_wn( uc->lpszUrlPath, uc->dwUrlPathLength ),
210 debugstr_wn( uc->lpszExtraInfo, uc->dwExtraInfoLength ));
212 exit:
213 HeapFree( GetProcessHeap(), 0, url_decoded );
214 return ret;
217 static INTERNET_SCHEME get_scheme( const WCHAR *scheme, DWORD len )
219 if (!strncmpW( scheme, scheme_http, len )) return INTERNET_SCHEME_HTTP;
220 if (!strncmpW( scheme, scheme_https, len )) return INTERNET_SCHEME_HTTPS;
221 return 0;
224 static const WCHAR *get_scheme_string( INTERNET_SCHEME scheme )
226 if (scheme == INTERNET_SCHEME_HTTP) return scheme_http;
227 if (scheme == INTERNET_SCHEME_HTTPS) return scheme_https;
228 return NULL;
231 static BOOL uses_default_port( INTERNET_SCHEME scheme, INTERNET_PORT port )
233 if ((scheme == INTERNET_SCHEME_HTTP) && (port == INTERNET_DEFAULT_HTTP_PORT)) return TRUE;
234 if ((scheme == INTERNET_SCHEME_HTTPS) && (port == INTERNET_DEFAULT_HTTPS_PORT)) return TRUE;
235 return FALSE;
238 static BOOL need_escape( WCHAR c )
240 if (isalnumW( c )) return FALSE;
242 if (c <= 31 || c >= 127) return TRUE;
243 else
245 switch (c)
247 case ' ':
248 case '"':
249 case '#':
250 case '%':
251 case '<':
252 case '>':
253 case ']':
254 case '\\':
255 case '[':
256 case '^':
257 case '`':
258 case '{':
259 case '|':
260 case '}':
261 case '~':
262 return TRUE;
263 default:
264 return FALSE;
269 static DWORD copy_escape( WCHAR *dst, const WCHAR *src, DWORD len )
271 static const WCHAR hex[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
272 DWORD ret = len;
273 unsigned int i;
274 WCHAR *p = dst;
276 for (i = 0; i < len; i++, p++)
278 if (need_escape( src[i] ))
280 p[0] = '%';
281 p[1] = hex[(src[i] >> 4) & 0xf];
282 p[2] = hex[src[i] & 0xf];
283 ret += 2;
284 p += 2;
286 else *p = src[i];
288 dst[ret] = 0;
289 return ret;
292 static DWORD comp_length( DWORD len, DWORD flags, WCHAR *comp )
294 DWORD ret;
295 unsigned int i;
297 ret = len ? len : strlenW( comp );
298 if (!(flags & ICU_ESCAPE)) return ret;
299 for (i = 0; i < len; i++) if (need_escape( comp[i] )) ret += 2;
300 return ret;
303 static BOOL calc_length( URL_COMPONENTS *uc, DWORD flags, LPDWORD len )
305 static const WCHAR formatW[] = {'%','u',0};
306 INTERNET_SCHEME scheme;
308 *len = 0;
309 if (uc->lpszScheme)
311 DWORD scheme_len = comp_length( uc->dwSchemeLength, 0, uc->lpszScheme );
312 *len += scheme_len;
313 scheme = get_scheme( uc->lpszScheme, scheme_len );
315 else
317 scheme = uc->nScheme;
318 if (!scheme) scheme = INTERNET_SCHEME_HTTP;
319 *len += strlenW( get_scheme_string( scheme ) );
321 *len += 1; /* ':' */
322 if (uc->lpszHostName) *len += 2; /* "//" */
324 if (uc->lpszUserName)
326 *len += comp_length( uc->dwUserNameLength, 0, uc->lpszUserName );
327 *len += 1; /* "@" */
329 else
331 if (uc->lpszPassword)
333 set_last_error( ERROR_INVALID_PARAMETER );
334 return FALSE;
337 if (uc->lpszPassword)
339 *len += 1; /* ":" */
340 *len += comp_length( uc->dwPasswordLength, 0, uc->lpszPassword );
342 if (uc->lpszHostName)
344 *len += comp_length( uc->dwHostNameLength, 0, uc->lpszHostName );
346 if (!uses_default_port( scheme, uc->nPort ))
348 WCHAR port[sizeof("65535")];
350 sprintfW( port, formatW, uc->nPort );
351 *len += strlenW( port );
352 *len += 1; /* ":" */
354 if (uc->lpszUrlPath && *uc->lpszUrlPath != '/') *len += 1; /* '/' */
356 if (uc->lpszUrlPath) *len += comp_length( uc->dwUrlPathLength, flags, uc->lpszUrlPath );
357 if (uc->lpszExtraInfo) *len += comp_length( uc->dwExtraInfoLength, flags, uc->lpszExtraInfo );
358 return TRUE;
361 /***********************************************************************
362 * WinHttpCreateUrl (winhttp.@)
364 BOOL WINAPI WinHttpCreateUrl( LPURL_COMPONENTS uc, DWORD flags, LPWSTR url, LPDWORD required )
366 static const WCHAR formatW[] = {'%','u',0};
367 static const WCHAR twoslashW[] = {'/','/'};
369 DWORD len;
370 INTERNET_SCHEME scheme;
372 TRACE("%p, 0x%08x, %p, %p\n", uc, flags, url, required);
374 if (!uc || uc->dwStructSize != sizeof(URL_COMPONENTS) || !required || !url)
376 set_last_error( ERROR_INVALID_PARAMETER );
377 return FALSE;
380 if (!calc_length( uc, flags, &len )) return FALSE;
382 if (*required < len)
384 *required = len + 1;
385 set_last_error( ERROR_INSUFFICIENT_BUFFER );
386 return FALSE;
389 url[0] = 0;
390 *required = len;
391 if (uc->lpszScheme)
393 len = comp_length( uc->dwSchemeLength, 0, uc->lpszScheme );
394 memcpy( url, uc->lpszScheme, len * sizeof(WCHAR) );
395 url += len;
397 scheme = get_scheme( uc->lpszScheme, len );
399 else
401 const WCHAR *schemeW;
402 scheme = uc->nScheme;
404 if (!scheme) scheme = INTERNET_SCHEME_HTTP;
406 schemeW = get_scheme_string( scheme );
407 len = strlenW( schemeW );
408 memcpy( url, schemeW, len * sizeof(WCHAR) );
409 url += len;
412 /* all schemes are followed by at least a colon */
413 *url = ':';
414 url++;
416 if (uc->lpszHostName)
418 memcpy( url, twoslashW, sizeof(twoslashW) );
419 url += sizeof(twoslashW) / sizeof(twoslashW[0]);
421 if (uc->lpszUserName)
423 len = comp_length( uc->dwUserNameLength, 0, uc->lpszUserName );
424 memcpy( url, uc->lpszUserName, len * sizeof(WCHAR) );
425 url += len;
427 if (uc->lpszPassword)
429 *url = ':';
430 url++;
432 len = comp_length( uc->dwPasswordLength, 0, uc->lpszPassword );
433 memcpy( url, uc->lpszPassword, len * sizeof(WCHAR) );
434 url += len;
436 *url = '@';
437 url++;
439 if (uc->lpszHostName)
441 len = comp_length( uc->dwHostNameLength, 0, uc->lpszHostName );
442 memcpy( url, uc->lpszHostName, len * sizeof(WCHAR) );
443 url += len;
445 if (!uses_default_port( scheme, uc->nPort ))
447 WCHAR port[sizeof("65535")];
449 sprintfW( port, formatW, uc->nPort );
450 *url = ':';
451 url++;
453 len = strlenW( port );
454 memcpy( url, port, len * sizeof(WCHAR) );
455 url += len;
458 /* add slash between hostname and path if necessary */
459 if (uc->lpszUrlPath && *uc->lpszUrlPath != '/')
461 *url = '/';
462 url++;
465 if (uc->lpszUrlPath)
467 len = comp_length( uc->dwUrlPathLength, 0, uc->lpszUrlPath );
468 if (flags & ICU_ESCAPE) url += copy_escape( url, uc->lpszUrlPath, len );
469 else
471 memcpy( url, uc->lpszUrlPath, len * sizeof(WCHAR) );
472 url += len;
475 if (uc->lpszExtraInfo)
477 len = comp_length( uc->dwExtraInfoLength, 0, uc->lpszExtraInfo );
478 if (flags & ICU_ESCAPE) url += copy_escape( url, uc->lpszExtraInfo, len );
479 else
481 memcpy( url, uc->lpszExtraInfo, len * sizeof(WCHAR) );
482 url += len;
485 *url = 0;
486 return TRUE;