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