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