2 * Copyright 2008 Hans Leidekker for CodeWeavers
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.
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.
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
22 #include "wine/debug.h"
30 #include "winhttp_private.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(winhttp);
34 static const WCHAR scheme_http[] = {'h','t','t','p',0};
35 static const WCHAR scheme_https[] = {'h','t','t','p','s',0};
37 static BOOL set_component( WCHAR **str, DWORD *str_len, WCHAR *value, DWORD len, DWORD flags )
41 if (len && (flags & ICU_DECODE))
43 set_last_error( ERROR_INVALID_PARAMETER );
51 if (len > (*str_len) - 1)
54 set_last_error( ERROR_INSUFFICIENT_BUFFER );
57 memcpy( *str, value, len * sizeof(WCHAR) );
64 static BOOL decode_url( LPCWSTR url, LPWSTR buffer, LPDWORD buflen )
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;
72 /***********************************************************************
73 * WinHttpCrackUrl (winhttp.@)
75 BOOL WINAPI WinHttpCrackUrl( LPCWSTR url, DWORD len, DWORD flags, LPURL_COMPONENTSW uc )
79 WCHAR *url_decoded = NULL;
81 TRACE("%s, %d, %x, %p\n", debugstr_w(url), len, flags, uc);
83 if (flags & ICU_ESCAPE) FIXME("flag ICU_ESCAPE not supported\n");
85 if (!url || !url[0] || !uc || uc->dwStructSize != sizeof(URL_COMPONENTS))
87 set_last_error( ERROR_INVALID_PARAMETER );
90 if (!len) len = strlenW( url );
92 if (flags & ICU_DECODE)
95 DWORD url_len = len + 1;
97 if (!(url_tmp = HeapAlloc( GetProcessHeap(), 0, url_len * sizeof(WCHAR) )))
99 set_last_error( ERROR_OUTOFMEMORY );
102 memcpy( url_tmp, url, len * sizeof(WCHAR) );
104 if (!(url_decoded = HeapAlloc( GetProcessHeap(), 0, url_len * sizeof(WCHAR) )))
106 HeapFree( GetProcessHeap(), 0, url_tmp );
107 set_last_error( ERROR_OUTOFMEMORY );
110 if (decode_url( url_tmp, url_decoded, &url_len ))
115 HeapFree( GetProcessHeap(), 0, url_tmp );
117 if (!(p = strchrW( url, ':' ))) return FALSE;
119 if (p - url == 4 && !strncmpiW( url, scheme_http, 4 )) uc->nScheme = INTERNET_SCHEME_HTTP;
120 else if (p - url == 5 && !strncmpiW( url, scheme_https, 5 )) uc->nScheme = INTERNET_SCHEME_HTTPS;
123 if (!(set_component( &uc->lpszScheme, &uc->dwSchemeLength, (WCHAR *)url, p - url, flags ))) goto exit;
126 if (!p[0] || p[0] != '/' || p[1] != '/') goto exit;
129 if (!p[0]) goto exit;
130 if ((q = memchrW( p, '@', len - (p - url) )))
132 if ((r = memchrW( p, ':', q - p )))
134 if (!(set_component( &uc->lpszUserName, &uc->dwUserNameLength, p, r - p, flags ))) goto exit;
136 if (!(set_component( &uc->lpszPassword, &uc->dwPasswordLength, r, q - r, flags ))) goto exit;
140 if (!(set_component( &uc->lpszUserName, &uc->dwUserNameLength, p, q - p, flags ))) goto exit;
141 if (!(set_component( &uc->lpszPassword, &uc->dwPasswordLength, NULL, 0, flags ))) goto exit;
147 if (!(set_component( &uc->lpszUserName, &uc->dwUserNameLength, NULL, 0, flags ))) goto exit;
148 if (!(set_component( &uc->lpszPassword, &uc->dwPasswordLength, NULL, 0, flags ))) goto exit;
150 if ((q = memchrW( p, '/', len - (p - url) )))
152 if (!(set_component( &uc->lpszHostName, &uc->dwHostNameLength, p, q - p, flags ))) goto exit;
154 if ((r = memchrW( q, '?', len - (q - url) )))
156 if (!(set_component( &uc->lpszUrlPath, &uc->dwUrlPathLength, q, r - q, flags ))) goto exit;
157 if (!(set_component( &uc->lpszExtraInfo, &uc->dwExtraInfoLength, r, len - (r - url), flags ))) goto exit;
161 if (!(set_component( &uc->lpszUrlPath, &uc->dwUrlPathLength, q, len - (q - url), flags ))) goto exit;
162 if (!(set_component( &uc->lpszExtraInfo, &uc->dwExtraInfoLength, (WCHAR *)url + len, 0, flags ))) goto exit;
167 if (!(set_component( &uc->lpszHostName, &uc->dwHostNameLength, p, len - (p - url), flags ))) goto exit;
168 if (!(set_component( &uc->lpszUrlPath, &uc->dwUrlPathLength, (WCHAR *)url + len, 0, flags ))) goto exit;
169 if (!(set_component( &uc->lpszExtraInfo, &uc->dwExtraInfoLength, (WCHAR *)url + len, 0, flags ))) goto exit;
174 TRACE("scheme(%s) host(%s) path(%s) extra(%s)\n",
175 debugstr_wn( uc->lpszScheme, uc->dwSchemeLength ),
176 debugstr_wn( uc->lpszHostName, uc->dwHostNameLength ),
177 debugstr_wn( uc->lpszUrlPath, uc->dwUrlPathLength ),
178 debugstr_wn( uc->lpszExtraInfo, uc->dwExtraInfoLength ));
181 HeapFree( GetProcessHeap(), 0, url_decoded );
185 static INTERNET_SCHEME get_scheme( const WCHAR *scheme, DWORD len )
187 if (!strncmpW( scheme, scheme_http, len )) return INTERNET_SCHEME_HTTP;
188 if (!strncmpW( scheme, scheme_https, len )) return INTERNET_SCHEME_HTTPS;
192 static const WCHAR *get_scheme_string( INTERNET_SCHEME scheme )
194 if (scheme == INTERNET_SCHEME_HTTP) return scheme_http;
195 if (scheme == INTERNET_SCHEME_HTTPS) return scheme_https;
199 static BOOL uses_default_port( INTERNET_SCHEME scheme, INTERNET_PORT port )
201 if ((scheme == INTERNET_SCHEME_HTTP) && (port == INTERNET_DEFAULT_HTTP_PORT)) return TRUE;
202 if ((scheme == INTERNET_SCHEME_HTTPS) && (port == INTERNET_DEFAULT_HTTPS_PORT)) return TRUE;
206 static BOOL need_escape( WCHAR c )
208 if (isalnumW( c )) return FALSE;
210 if (c <= 31 || c >= 127) return TRUE;
237 static DWORD copy_escape( WCHAR *dst, const WCHAR *src, DWORD len )
239 static const WCHAR hex[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
244 for (i = 0; i < len; i++, p++)
246 if (need_escape( src[i] ))
249 p[1] = hex[(src[i] >> 4) & 0xf];
250 p[2] = hex[src[i] & 0xf];
260 static DWORD comp_length( DWORD len, DWORD flags, WCHAR *comp )
265 ret = len ? len : strlenW( comp );
266 if (!(flags & ICU_ESCAPE)) return ret;
267 for (i = 0; i < len; i++) if (need_escape( comp[i] )) ret += 2;
271 static BOOL calc_length( URL_COMPONENTS *uc, DWORD flags, LPDWORD len )
273 static const WCHAR formatW[] = {'%','d',0};
274 INTERNET_SCHEME scheme;
279 DWORD scheme_len = comp_length( uc->dwSchemeLength, 0, uc->lpszScheme );
281 scheme = get_scheme( uc->lpszScheme, scheme_len );
285 scheme = uc->nScheme;
286 if (!scheme) scheme = INTERNET_SCHEME_HTTP;
287 *len += strlenW( get_scheme_string( scheme ) );
290 if (uc->lpszHostName) *len += 2; /* "//" */
292 if (uc->lpszUserName)
294 *len += comp_length( uc->dwUserNameLength, 0, uc->lpszUserName );
299 if (uc->lpszPassword)
301 set_last_error( ERROR_INVALID_PARAMETER );
305 if (uc->lpszPassword)
308 *len += comp_length( uc->dwPasswordLength, 0, uc->lpszPassword );
310 if (uc->lpszHostName)
312 *len += comp_length( uc->dwHostNameLength, 0, uc->lpszHostName );
314 if (!uses_default_port( scheme, uc->nPort ))
316 WCHAR port[sizeof("65535")];
318 sprintfW( port, formatW, uc->nPort );
319 *len += strlenW( port );
322 if (uc->lpszUrlPath && *uc->lpszUrlPath != '/') *len += 1; /* '/' */
324 if (uc->lpszUrlPath) *len += comp_length( uc->dwUrlPathLength, flags, uc->lpszUrlPath );
325 if (uc->lpszExtraInfo) *len += comp_length( uc->dwExtraInfoLength, flags, uc->lpszExtraInfo );
329 /***********************************************************************
330 * WinHttpCreateUrl (winhttp.@)
332 BOOL WINAPI WinHttpCreateUrl( LPURL_COMPONENTS uc, DWORD flags, LPWSTR url, LPDWORD required )
334 static const WCHAR formatW[] = {'%','d',0};
335 static const WCHAR twoslashW[] = {'/','/'};
338 INTERNET_SCHEME scheme;
340 TRACE("%p, 0x%08x, %p, %p\n", uc, flags, url, required);
342 if (!uc || uc->dwStructSize != sizeof(URL_COMPONENTS) || !required)
344 set_last_error( ERROR_INVALID_PARAMETER );
348 if (!calc_length( uc, flags, &len )) return FALSE;
350 if (!url || *required < len)
353 set_last_error( ERROR_INSUFFICIENT_BUFFER );
361 len = comp_length( uc->dwSchemeLength, 0, uc->lpszScheme );
362 memcpy( url, uc->lpszScheme, len * sizeof(WCHAR) );
365 scheme = get_scheme( uc->lpszScheme, len );
369 const WCHAR *schemeW;
370 scheme = uc->nScheme;
372 if (!scheme) scheme = INTERNET_SCHEME_HTTP;
374 schemeW = get_scheme_string( scheme );
375 len = strlenW( schemeW );
376 memcpy( url, schemeW, len * sizeof(WCHAR) );
380 /* all schemes are followed by at least a colon */
384 if (uc->lpszHostName)
386 memcpy( url, twoslashW, sizeof(twoslashW) );
387 url += sizeof(twoslashW) / sizeof(twoslashW[0]);
389 if (uc->lpszUserName)
391 len = comp_length( uc->dwUserNameLength, 0, uc->lpszUserName );
392 memcpy( url, uc->lpszUserName, len * sizeof(WCHAR) );
395 if (uc->lpszPassword)
400 len = comp_length( uc->dwPasswordLength, 0, uc->lpszPassword );
401 memcpy( url, uc->lpszPassword, len * sizeof(WCHAR) );
407 if (uc->lpszHostName)
409 len = comp_length( uc->dwHostNameLength, 0, uc->lpszHostName );
410 memcpy( url, uc->lpszHostName, len * sizeof(WCHAR) );
413 if (!uses_default_port( scheme, uc->nPort ))
415 WCHAR port[sizeof("65535")];
417 sprintfW( port, formatW, uc->nPort );
421 len = strlenW( port );
422 memcpy( url, port, len * sizeof(WCHAR) );
426 /* add slash between hostname and path if necessary */
427 if (uc->lpszUrlPath && *uc->lpszUrlPath != '/')
435 len = comp_length( uc->dwUrlPathLength, 0, uc->lpszUrlPath );
436 if (flags & ICU_ESCAPE) url += copy_escape( url, uc->lpszUrlPath, len );
439 memcpy( url, uc->lpszUrlPath, len * sizeof(WCHAR) );
443 if (uc->lpszExtraInfo)
445 len = comp_length( uc->dwExtraInfoLength, 0, uc->lpszExtraInfo );
446 if (flags & ICU_ESCAPE) url += copy_escape( url, uc->lpszExtraInfo, len );
449 memcpy( url, uc->lpszExtraInfo, len * sizeof(WCHAR) );