Git conversion: Make reactos the root directory, move rosapps, rostests, wallpapers...
[reactos.git] / dll / win32 / winhttp / url.c
1 /*
2 * Copyright 2008 Hans Leidekker for CodeWeavers
3 *
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.
8 *
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.
13 *
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
17 */
18
19 #include "winhttp_private.h"
20
21 static const WCHAR scheme_http[] = {'h','t','t','p',0};
22 static const WCHAR scheme_https[] = {'h','t','t','p','s',0};
23
24 static DWORD set_component( WCHAR **str, DWORD *str_len, WCHAR *value, DWORD len, DWORD flags, BOOL *overflow )
25 {
26 if (*str && !*str_len) return ERROR_INVALID_PARAMETER;
27 if (!*str_len) return ERROR_SUCCESS;
28 if (!*str)
29 {
30 if (len && *str_len && (flags & (ICU_DECODE|ICU_ESCAPE))) return ERROR_INVALID_PARAMETER;
31 *str = value;
32 *str_len = len;
33 }
34 else
35 {
36 if (len >= *str_len)
37 {
38 *str_len = len+1;
39 *overflow = TRUE;
40 return ERROR_SUCCESS;
41 }
42 memcpy( *str, value, len * sizeof(WCHAR) );
43 (*str)[len] = 0;
44 *str_len = len;
45 }
46 return ERROR_SUCCESS;
47 }
48
49 static WCHAR *decode_url( LPCWSTR url, DWORD *len )
50 {
51 const WCHAR *p = url;
52 WCHAR hex[3], *q, *ret;
53
54 if (!(ret = heap_alloc( *len * sizeof(WCHAR) ))) return NULL;
55 q = ret;
56 while (*len > 0)
57 {
58 if (p[0] == '%' && isxdigitW( p[1] ) && isxdigitW( p[2] ))
59 {
60 hex[0] = p[1];
61 hex[1] = p[2];
62 hex[2] = 0;
63 *q++ = strtolW( hex, NULL, 16 );
64 p += 3;
65 *len -= 3;
66 }
67 else
68 {
69 *q++ = *p++;
70 *len -= 1;
71 }
72 }
73 *len = q - ret;
74 return ret;
75 }
76
77 static BOOL need_escape( WCHAR c )
78 {
79 if (isalnumW( c )) return FALSE;
80
81 if (c <= 31 || c >= 127) return TRUE;
82 else
83 {
84 switch (c)
85 {
86 case ' ':
87 case '"':
88 case '#':
89 case '%':
90 case '<':
91 case '>':
92 case ']':
93 case '\\':
94 case '[':
95 case '^':
96 case '`':
97 case '{':
98 case '|':
99 case '}':
100 case '~':
101 return TRUE;
102 default:
103 return FALSE;
104 }
105 }
106 }
107
108 static DWORD copy_escape( WCHAR *dst, const WCHAR *src, DWORD len )
109 {
110 static const WCHAR hex[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
111 DWORD ret = len;
112 unsigned int i;
113 WCHAR *p = dst;
114
115 for (i = 0; i < len; i++, p++)
116 {
117 if (need_escape( src[i] ))
118 {
119 p[0] = '%';
120 p[1] = hex[(src[i] >> 4) & 0xf];
121 p[2] = hex[src[i] & 0xf];
122 ret += 2;
123 p += 2;
124 }
125 else *p = src[i];
126 }
127 dst[ret] = 0;
128 return ret;
129 }
130
131 static WCHAR *escape_url( LPCWSTR url, DWORD *len )
132 {
133 WCHAR *ret;
134 const WCHAR *p, *q;
135
136 if ((p = q = strrchrW( url, '/' )))
137 {
138 while (*q)
139 {
140 if (need_escape( *q )) *len += 2;
141 q++;
142 }
143 }
144 if (!(ret = heap_alloc( (*len + 1) * sizeof(WCHAR) ))) return NULL;
145 if (!p) strcpyW( ret, url );
146 else
147 {
148 memcpy( ret, url, (p - url) * sizeof(WCHAR) );
149 copy_escape( ret + (p - url), p, q - p );
150 }
151 return ret;
152 }
153
154 static DWORD parse_port( const WCHAR *str, DWORD len, INTERNET_PORT *ret )
155 {
156 const WCHAR *p = str;
157 DWORD port = 0;
158 while (len && isdigitW( *p ))
159 {
160 if ((port = port * 10 + *p - '0') > 65535) return ERROR_WINHTTP_INVALID_URL;
161 p++; len--;
162 }
163 *ret = port;
164 return ERROR_SUCCESS;
165 }
166
167 /***********************************************************************
168 * WinHttpCrackUrl (winhttp.@)
169 */
170 BOOL WINAPI WinHttpCrackUrl( LPCWSTR url, DWORD len, DWORD flags, LPURL_COMPONENTSW uc )
171 {
172 WCHAR *p, *q, *r, *url_decoded = NULL, *url_escaped = NULL;
173 INTERNET_SCHEME scheme = 0;
174 BOOL overflow = FALSE;
175 DWORD err;
176
177 TRACE("%s, %d, %x, %p\n", debugstr_wn(url, len), len, flags, uc);
178
179 if (!url || !uc || uc->dwStructSize != sizeof(URL_COMPONENTS))
180 {
181 set_last_error( ERROR_INVALID_PARAMETER );
182 return FALSE;
183 }
184 if (!len) len = strlenW( url );
185
186 if (flags & ICU_ESCAPE)
187 {
188 if (!(url_escaped = escape_url( url, &len )))
189 {
190 set_last_error( ERROR_OUTOFMEMORY );
191 return FALSE;
192 }
193 url = url_escaped;
194 }
195 else if (flags & ICU_DECODE)
196 {
197 if (!(url_decoded = decode_url( url, &len )))
198 {
199 set_last_error( ERROR_OUTOFMEMORY );
200 return FALSE;
201 }
202 url = url_decoded;
203 }
204 if (!(p = strchrW( url, ':' )))
205 {
206 set_last_error( ERROR_WINHTTP_UNRECOGNIZED_SCHEME );
207 return FALSE;
208 }
209 if (p - url == 4 && !strncmpiW( url, scheme_http, 4 )) scheme = INTERNET_SCHEME_HTTP;
210 else if (p - url == 5 && !strncmpiW( url, scheme_https, 5 )) scheme = INTERNET_SCHEME_HTTPS;
211 else
212 {
213 err = ERROR_WINHTTP_UNRECOGNIZED_SCHEME;
214 goto exit;
215 }
216
217 if ((err = set_component( &uc->lpszScheme, &uc->dwSchemeLength, (WCHAR *)url, p - url, flags, &overflow ))) goto exit;
218
219 p++; /* skip ':' */
220 if (!p[0] || p[0] != '/' || p[1] != '/')
221 {
222 err = ERROR_WINHTTP_INVALID_URL;
223 goto exit;
224 }
225 p += 2;
226 if (!p[0])
227 {
228 err = ERROR_WINHTTP_INVALID_URL;
229 goto exit;
230 }
231 if ((q = memchrW( p, '@', len - (p - url) )) && !(memchrW( p, '/', q - p )))
232 {
233 if ((r = memchrW( p, ':', q - p )))
234 {
235 if ((err = set_component( &uc->lpszUserName, &uc->dwUserNameLength, p, r - p, flags, &overflow ))) goto exit;
236 r++;
237 if ((err = set_component( &uc->lpszPassword, &uc->dwPasswordLength, r, q - r, flags, &overflow ))) goto exit;
238 }
239 else
240 {
241 if ((err = set_component( &uc->lpszUserName, &uc->dwUserNameLength, p, q - p, flags, &overflow ))) goto exit;
242 if ((err = set_component( &uc->lpszPassword, &uc->dwPasswordLength, NULL, 0, flags, &overflow ))) goto exit;
243 }
244 p = q + 1;
245 }
246 else
247 {
248 if ((err = set_component( &uc->lpszUserName, &uc->dwUserNameLength, NULL, 0, flags, &overflow ))) goto exit;
249 if ((err = set_component( &uc->lpszPassword, &uc->dwPasswordLength, NULL, 0, flags, &overflow ))) goto exit;
250 }
251 if ((q = memchrW( p, '/', len - (p - url) )))
252 {
253 if ((r = memchrW( p, ':', q - p )))
254 {
255 if ((err = set_component( &uc->lpszHostName, &uc->dwHostNameLength, p, r - p, flags, &overflow ))) goto exit;
256 r++;
257 if ((err = parse_port( r, q - r, &uc->nPort ))) goto exit;
258 }
259 else
260 {
261 if ((err = set_component( &uc->lpszHostName, &uc->dwHostNameLength, p, q - p, flags, &overflow ))) goto exit;
262 if (scheme == INTERNET_SCHEME_HTTP) uc->nPort = INTERNET_DEFAULT_HTTP_PORT;
263 if (scheme == INTERNET_SCHEME_HTTPS) uc->nPort = INTERNET_DEFAULT_HTTPS_PORT;
264 }
265
266 if ((r = memchrW( q, '?', len - (q - url) )))
267 {
268 if ((err = set_component( &uc->lpszUrlPath, &uc->dwUrlPathLength, q, r - q, flags, &overflow ))) goto exit;
269 if ((err = set_component( &uc->lpszExtraInfo, &uc->dwExtraInfoLength, r, len - (r - url), flags, &overflow ))) goto exit;
270 }
271 else
272 {
273 if ((err = set_component( &uc->lpszUrlPath, &uc->dwUrlPathLength, q, len - (q - url), flags, &overflow ))) goto exit;
274 if ((err = set_component( &uc->lpszExtraInfo, &uc->dwExtraInfoLength, (WCHAR *)url + len, 0, flags, &overflow ))) goto exit;
275 }
276 }
277 else
278 {
279 if ((r = memchrW( p, ':', len - (p - url) )))
280 {
281 if ((err = set_component( &uc->lpszHostName, &uc->dwHostNameLength, p, r - p, flags, &overflow ))) goto exit;
282 r++;
283 if ((err = parse_port( r, len - (r - url), &uc->nPort ))) goto exit;
284 }
285 else
286 {
287 if ((err = set_component( &uc->lpszHostName, &uc->dwHostNameLength, p, len - (p - url), flags, &overflow ))) goto exit;
288 if (scheme == INTERNET_SCHEME_HTTP) uc->nPort = INTERNET_DEFAULT_HTTP_PORT;
289 if (scheme == INTERNET_SCHEME_HTTPS) uc->nPort = INTERNET_DEFAULT_HTTPS_PORT;
290 }
291 if ((err = set_component( &uc->lpszUrlPath, &uc->dwUrlPathLength, (WCHAR *)url + len, 0, flags, &overflow ))) goto exit;
292 if ((err = set_component( &uc->lpszExtraInfo, &uc->dwExtraInfoLength, (WCHAR *)url + len, 0, flags, &overflow ))) goto exit;
293 }
294
295 TRACE("scheme(%s) host(%s) port(%d) path(%s) extra(%s)\n", debugstr_wn( uc->lpszScheme, uc->dwSchemeLength ),
296 debugstr_wn( uc->lpszHostName, uc->dwHostNameLength ), uc->nPort, debugstr_wn( uc->lpszUrlPath, uc->dwUrlPathLength ),
297 debugstr_wn( uc->lpszExtraInfo, uc->dwExtraInfoLength ));
298
299 exit:
300 if (!err)
301 {
302 if (overflow) err = ERROR_INSUFFICIENT_BUFFER;
303 uc->nScheme = scheme;
304 }
305 heap_free( url_decoded );
306 heap_free( url_escaped );
307 set_last_error( err );
308 return !err;
309 }
310
311 static INTERNET_SCHEME get_scheme( const WCHAR *scheme, DWORD len )
312 {
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;
316 }
317
318 static const WCHAR *get_scheme_string( INTERNET_SCHEME scheme )
319 {
320 if (scheme == INTERNET_SCHEME_HTTP) return scheme_http;
321 if (scheme == INTERNET_SCHEME_HTTPS) return scheme_https;
322 return NULL;
323 }
324
325 static BOOL uses_default_port( INTERNET_SCHEME scheme, INTERNET_PORT port )
326 {
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;
330 }
331
332 static DWORD comp_length( DWORD len, DWORD flags, WCHAR *comp )
333 {
334 DWORD ret;
335 unsigned int i;
336
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;
341 }
342
343 static BOOL calc_length( URL_COMPONENTS *uc, DWORD flags, LPDWORD len )
344 {
345 static const WCHAR formatW[] = {'%','u',0};
346 INTERNET_SCHEME scheme;
347
348 *len = 0;
349 if (uc->lpszScheme)
350 {
351 DWORD scheme_len = comp_length( uc->dwSchemeLength, 0, uc->lpszScheme );
352 *len += scheme_len;
353 scheme = get_scheme( uc->lpszScheme, scheme_len );
354 }
355 else
356 {
357 scheme = uc->nScheme;
358 if (!scheme) scheme = INTERNET_SCHEME_HTTP;
359 *len += strlenW( get_scheme_string( scheme ) );
360 }
361 *len += 1; /* ':' */
362 if (uc->lpszHostName) *len += 2; /* "//" */
363
364 if (uc->lpszUserName)
365 {
366 *len += comp_length( uc->dwUserNameLength, 0, uc->lpszUserName );
367 *len += 1; /* "@" */
368 }
369 else
370 {
371 if (uc->lpszPassword)
372 {
373 set_last_error( ERROR_INVALID_PARAMETER );
374 return FALSE;
375 }
376 }
377 if (uc->lpszPassword)
378 {
379 *len += 1; /* ":" */
380 *len += comp_length( uc->dwPasswordLength, 0, uc->lpszPassword );
381 }
382 if (uc->lpszHostName)
383 {
384 *len += comp_length( uc->dwHostNameLength, 0, uc->lpszHostName );
385
386 if (!uses_default_port( scheme, uc->nPort ))
387 {
388 WCHAR port[sizeof("65535")];
389
390 *len += sprintfW( port, formatW, uc->nPort );
391 *len += 1; /* ":" */
392 }
393 if (uc->lpszUrlPath && *uc->lpszUrlPath != '/') *len += 1; /* '/' */
394 }
395 if (uc->lpszUrlPath) *len += comp_length( uc->dwUrlPathLength, flags, uc->lpszUrlPath );
396 if (uc->lpszExtraInfo) *len += comp_length( uc->dwExtraInfoLength, flags, uc->lpszExtraInfo );
397 return TRUE;
398 }
399
400 /***********************************************************************
401 * WinHttpCreateUrl (winhttp.@)
402 */
403 BOOL WINAPI WinHttpCreateUrl( LPURL_COMPONENTS uc, DWORD flags, LPWSTR url, LPDWORD required )
404 {
405 static const WCHAR formatW[] = {'%','u',0};
406 static const WCHAR twoslashW[] = {'/','/'};
407 DWORD len;
408 INTERNET_SCHEME scheme;
409
410 TRACE("%p, 0x%08x, %p, %p\n", uc, flags, url, required);
411
412 if (!uc || uc->dwStructSize != sizeof(URL_COMPONENTS) || !required)
413 {
414 set_last_error( ERROR_INVALID_PARAMETER );
415 return FALSE;
416 }
417
418 if (!calc_length( uc, flags, &len )) return FALSE;
419
420 if (*required < len)
421 {
422 *required = len + 1;
423 set_last_error( ERROR_INSUFFICIENT_BUFFER );
424 return FALSE;
425 }
426 if (!url)
427 {
428 set_last_error( ERROR_INVALID_PARAMETER );
429 return FALSE;
430 }
431
432 url[0] = 0;
433 *required = len;
434 if (uc->lpszScheme)
435 {
436 len = comp_length( uc->dwSchemeLength, 0, uc->lpszScheme );
437 memcpy( url, uc->lpszScheme, len * sizeof(WCHAR) );
438 url += len;
439
440 scheme = get_scheme( uc->lpszScheme, len );
441 }
442 else
443 {
444 const WCHAR *schemeW;
445 scheme = uc->nScheme;
446
447 if (!scheme) scheme = INTERNET_SCHEME_HTTP;
448
449 schemeW = get_scheme_string( scheme );
450 len = strlenW( schemeW );
451 memcpy( url, schemeW, len * sizeof(WCHAR) );
452 url += len;
453 }
454
455 /* all schemes are followed by at least a colon */
456 *url = ':';
457 url++;
458
459 if (uc->lpszHostName)
460 {
461 memcpy( url, twoslashW, sizeof(twoslashW) );
462 url += sizeof(twoslashW) / sizeof(twoslashW[0]);
463 }
464 if (uc->lpszUserName)
465 {
466 len = comp_length( uc->dwUserNameLength, 0, uc->lpszUserName );
467 memcpy( url, uc->lpszUserName, len * sizeof(WCHAR) );
468 url += len;
469
470 if (uc->lpszPassword)
471 {
472 *url = ':';
473 url++;
474
475 len = comp_length( uc->dwPasswordLength, 0, uc->lpszPassword );
476 memcpy( url, uc->lpszPassword, len * sizeof(WCHAR) );
477 url += len;
478 }
479 *url = '@';
480 url++;
481 }
482 if (uc->lpszHostName)
483 {
484 len = comp_length( uc->dwHostNameLength, 0, uc->lpszHostName );
485 memcpy( url, uc->lpszHostName, len * sizeof(WCHAR) );
486 url += len;
487
488 if (!uses_default_port( scheme, uc->nPort ))
489 {
490 *url = ':';
491 url++;
492
493 url += sprintfW( url, formatW, uc->nPort );
494 }
495
496 /* add slash between hostname and path if necessary */
497 if (uc->lpszUrlPath && *uc->lpszUrlPath != '/')
498 {
499 *url = '/';
500 url++;
501 }
502 }
503 if (uc->lpszUrlPath)
504 {
505 len = comp_length( uc->dwUrlPathLength, 0, uc->lpszUrlPath );
506 if (flags & ICU_ESCAPE) url += copy_escape( url, uc->lpszUrlPath, len );
507 else
508 {
509 memcpy( url, uc->lpszUrlPath, len * sizeof(WCHAR) );
510 url += len;
511 }
512 }
513 if (uc->lpszExtraInfo)
514 {
515 len = comp_length( uc->dwExtraInfoLength, 0, uc->lpszExtraInfo );
516 if (flags & ICU_ESCAPE) url += copy_escape( url, uc->lpszExtraInfo, len );
517 else
518 {
519 memcpy( url, uc->lpszExtraInfo, len * sizeof(WCHAR) );
520 url += len;
521 }
522 }
523 *url = 0;
524 set_last_error( ERROR_SUCCESS );
525 return TRUE;
526 }