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