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