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