services: Check for services without lpBinaryPathName in get_winedevice_process.
[wine.git] / dlls / winhttp / url.c
blob26969cdf669261bb91d214b47c157d713e8209e1
1 /*
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
19 #include "config.h"
20 #include <stdarg.h>
22 #include "wine/debug.h"
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winreg.h"
27 #include "winhttp.h"
28 #include "shlwapi.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 DWORD set_component( WCHAR **str, DWORD *str_len, WCHAR *value, DWORD len, DWORD flags, BOOL *overflow )
39 if (*str && !*str_len) return ERROR_INVALID_PARAMETER;
40 if (!*str_len) return ERROR_SUCCESS;
41 if (!*str)
43 if (len && *str_len && (flags & (ICU_DECODE|ICU_ESCAPE))) return ERROR_INVALID_PARAMETER;
44 *str = value;
45 *str_len = len;
47 else
49 if (len >= *str_len)
51 *str_len = len+1;
52 *overflow = TRUE;
53 return ERROR_SUCCESS;
55 memcpy( *str, value, len * sizeof(WCHAR) );
56 (*str)[len] = 0;
57 *str_len = len;
59 return ERROR_SUCCESS;
62 static WCHAR *decode_url( LPCWSTR url, DWORD *len )
64 const WCHAR *p = url;
65 WCHAR hex[3], *q, *ret;
67 if (!(ret = heap_alloc( *len * sizeof(WCHAR) ))) return NULL;
68 q = ret;
69 while (*len > 0)
71 if (p[0] == '%' && isxdigitW( p[1] ) && isxdigitW( p[2] ))
73 hex[0] = p[1];
74 hex[1] = p[2];
75 hex[2] = 0;
76 *q++ = strtolW( hex, NULL, 16 );
77 p += 3;
78 *len -= 3;
80 else
82 *q++ = *p++;
83 *len -= 1;
86 *len = q - ret;
87 return ret;
90 static BOOL need_escape( WCHAR c )
92 if (isalnumW( c )) return FALSE;
94 if (c <= 31 || c >= 127) return TRUE;
95 else
97 switch (c)
99 case ' ':
100 case '"':
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 return TRUE;
115 default:
116 return FALSE;
121 static DWORD copy_escape( WCHAR *dst, const WCHAR *src, DWORD len )
123 static const WCHAR hex[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
124 DWORD ret = len;
125 unsigned int i;
126 WCHAR *p = dst;
128 for (i = 0; i < len; i++, p++)
130 if (need_escape( src[i] ))
132 p[0] = '%';
133 p[1] = hex[(src[i] >> 4) & 0xf];
134 p[2] = hex[src[i] & 0xf];
135 ret += 2;
136 p += 2;
138 else *p = src[i];
140 dst[ret] = 0;
141 return ret;
144 static WCHAR *escape_url( LPCWSTR url, DWORD *len )
146 WCHAR *ret;
147 const WCHAR *p, *q;
149 if ((p = q = strrchrW( url, '/' )))
151 while (*q)
153 if (need_escape( *q )) *len += 2;
154 q++;
157 if (!(ret = heap_alloc( (*len + 1) * sizeof(WCHAR) ))) return NULL;
158 if (!p) strcpyW( ret, url );
159 else
161 memcpy( ret, url, (p - url) * sizeof(WCHAR) );
162 copy_escape( ret + (p - url), p, q - p );
164 return ret;
167 static DWORD parse_port( const WCHAR *str, DWORD len, INTERNET_PORT *ret )
169 const WCHAR *p = str;
170 DWORD port = 0;
171 while (len && isdigitW( *p ))
173 if ((port = port * 10 + *p - '0') > 65535) return ERROR_WINHTTP_INVALID_URL;
174 p++; len--;
176 *ret = port;
177 return ERROR_SUCCESS;
180 /***********************************************************************
181 * WinHttpCrackUrl (winhttp.@)
183 BOOL WINAPI WinHttpCrackUrl( LPCWSTR url, DWORD len, DWORD flags, LPURL_COMPONENTSW uc )
185 WCHAR *p, *q, *r, *url_decoded = NULL, *url_escaped = NULL;
186 INTERNET_SCHEME scheme = 0;
187 BOOL overflow = FALSE;
188 DWORD err;
190 TRACE("%s, %d, %x, %p\n", debugstr_wn(url, len), len, flags, uc);
192 if (!url || !uc || uc->dwStructSize != sizeof(URL_COMPONENTS))
194 set_last_error( ERROR_INVALID_PARAMETER );
195 return FALSE;
197 if (!len) len = strlenW( url );
199 if (flags & ICU_ESCAPE)
201 if (!(url_escaped = escape_url( url, &len )))
203 set_last_error( ERROR_OUTOFMEMORY );
204 return FALSE;
206 url = url_escaped;
208 else if (flags & ICU_DECODE)
210 if (!(url_decoded = decode_url( url, &len )))
212 set_last_error( ERROR_OUTOFMEMORY );
213 return FALSE;
215 url = url_decoded;
217 if (!(p = strchrW( url, ':' )))
219 set_last_error( ERROR_WINHTTP_UNRECOGNIZED_SCHEME );
220 return FALSE;
222 if (p - url == 4 && !strncmpiW( url, scheme_http, 4 )) scheme = INTERNET_SCHEME_HTTP;
223 else if (p - url == 5 && !strncmpiW( url, scheme_https, 5 )) scheme = INTERNET_SCHEME_HTTPS;
224 else
226 err = ERROR_WINHTTP_UNRECOGNIZED_SCHEME;
227 goto exit;
230 if ((err = set_component( &uc->lpszScheme, &uc->dwSchemeLength, (WCHAR *)url, p - url, flags, &overflow ))) goto exit;
232 p++; /* skip ':' */
233 if (!p[0] || p[0] != '/' || p[1] != '/')
235 err = ERROR_WINHTTP_INVALID_URL;
236 goto exit;
238 p += 2;
239 if (!p[0])
241 err = ERROR_WINHTTP_INVALID_URL;
242 goto exit;
244 if ((q = memchrW( p, '@', len - (p - url) )) && !(memchrW( p, '/', q - p )))
246 if ((r = memchrW( p, ':', q - p )))
248 if ((err = set_component( &uc->lpszUserName, &uc->dwUserNameLength, p, r - p, flags, &overflow ))) goto exit;
249 r++;
250 if ((err = set_component( &uc->lpszPassword, &uc->dwPasswordLength, r, q - r, flags, &overflow ))) goto exit;
252 else
254 if ((err = set_component( &uc->lpszUserName, &uc->dwUserNameLength, p, q - p, flags, &overflow ))) goto exit;
255 if ((err = set_component( &uc->lpszPassword, &uc->dwPasswordLength, NULL, 0, flags, &overflow ))) goto exit;
257 p = q + 1;
259 else
261 if ((err = set_component( &uc->lpszUserName, &uc->dwUserNameLength, NULL, 0, flags, &overflow ))) goto exit;
262 if ((err = set_component( &uc->lpszPassword, &uc->dwPasswordLength, NULL, 0, flags, &overflow ))) goto exit;
264 if ((q = memchrW( p, '/', len - (p - url) )))
266 if ((r = memchrW( p, ':', q - p )))
268 if ((err = set_component( &uc->lpszHostName, &uc->dwHostNameLength, p, r - p, flags, &overflow ))) goto exit;
269 r++;
270 if ((err = parse_port( r, q - r, &uc->nPort ))) goto exit;
272 else
274 if ((err = set_component( &uc->lpszHostName, &uc->dwHostNameLength, p, q - p, flags, &overflow ))) goto exit;
275 if (scheme == INTERNET_SCHEME_HTTP) uc->nPort = INTERNET_DEFAULT_HTTP_PORT;
276 if (scheme == INTERNET_SCHEME_HTTPS) uc->nPort = INTERNET_DEFAULT_HTTPS_PORT;
279 if ((r = memchrW( q, '?', len - (q - url) )))
281 if ((err = set_component( &uc->lpszUrlPath, &uc->dwUrlPathLength, q, r - q, flags, &overflow ))) goto exit;
282 if ((err = set_component( &uc->lpszExtraInfo, &uc->dwExtraInfoLength, r, len - (r - url), flags, &overflow ))) goto exit;
284 else
286 if ((err = set_component( &uc->lpszUrlPath, &uc->dwUrlPathLength, q, len - (q - url), flags, &overflow ))) goto exit;
287 if ((err = set_component( &uc->lpszExtraInfo, &uc->dwExtraInfoLength, (WCHAR *)url + len, 0, flags, &overflow ))) goto exit;
290 else
292 if ((r = memchrW( p, ':', len - (p - url) )))
294 if ((err = set_component( &uc->lpszHostName, &uc->dwHostNameLength, p, r - p, flags, &overflow ))) goto exit;
295 r++;
296 if ((err = parse_port( r, len - (r - url), &uc->nPort ))) goto exit;
298 else
300 if ((err = set_component( &uc->lpszHostName, &uc->dwHostNameLength, p, len - (p - url), flags, &overflow ))) goto exit;
301 if (scheme == INTERNET_SCHEME_HTTP) uc->nPort = INTERNET_DEFAULT_HTTP_PORT;
302 if (scheme == INTERNET_SCHEME_HTTPS) uc->nPort = INTERNET_DEFAULT_HTTPS_PORT;
304 if ((err = set_component( &uc->lpszUrlPath, &uc->dwUrlPathLength, (WCHAR *)url + len, 0, flags, &overflow ))) goto exit;
305 if ((err = set_component( &uc->lpszExtraInfo, &uc->dwExtraInfoLength, (WCHAR *)url + len, 0, flags, &overflow ))) goto exit;
308 TRACE("scheme(%s) host(%s) port(%d) path(%s) extra(%s)\n", debugstr_wn( uc->lpszScheme, uc->dwSchemeLength ),
309 debugstr_wn( uc->lpszHostName, uc->dwHostNameLength ), uc->nPort, debugstr_wn( uc->lpszUrlPath, uc->dwUrlPathLength ),
310 debugstr_wn( uc->lpszExtraInfo, uc->dwExtraInfoLength ));
312 exit:
313 if (!err)
315 if (overflow) err = ERROR_INSUFFICIENT_BUFFER;
316 uc->nScheme = scheme;
318 heap_free( url_decoded );
319 heap_free( url_escaped );
320 set_last_error( err );
321 return !err;
324 static INTERNET_SCHEME get_scheme( const WCHAR *scheme, DWORD len )
326 if (!strncmpW( scheme, scheme_http, len )) return INTERNET_SCHEME_HTTP;
327 if (!strncmpW( scheme, scheme_https, len )) return INTERNET_SCHEME_HTTPS;
328 return 0;
331 static const WCHAR *get_scheme_string( INTERNET_SCHEME scheme )
333 if (scheme == INTERNET_SCHEME_HTTP) return scheme_http;
334 if (scheme == INTERNET_SCHEME_HTTPS) return scheme_https;
335 return NULL;
338 static BOOL uses_default_port( INTERNET_SCHEME scheme, INTERNET_PORT port )
340 if ((scheme == INTERNET_SCHEME_HTTP) && (port == INTERNET_DEFAULT_HTTP_PORT)) return TRUE;
341 if ((scheme == INTERNET_SCHEME_HTTPS) && (port == INTERNET_DEFAULT_HTTPS_PORT)) return TRUE;
342 return FALSE;
345 static DWORD comp_length( DWORD len, DWORD flags, WCHAR *comp )
347 DWORD ret;
348 unsigned int i;
350 ret = len ? len : strlenW( comp );
351 if (!(flags & ICU_ESCAPE)) return ret;
352 for (i = 0; i < len; i++) if (need_escape( comp[i] )) ret += 2;
353 return ret;
356 static BOOL calc_length( URL_COMPONENTS *uc, DWORD flags, LPDWORD len )
358 static const WCHAR formatW[] = {'%','u',0};
359 INTERNET_SCHEME scheme;
361 *len = 0;
362 if (uc->lpszScheme)
364 DWORD scheme_len = comp_length( uc->dwSchemeLength, 0, uc->lpszScheme );
365 *len += scheme_len;
366 scheme = get_scheme( uc->lpszScheme, scheme_len );
368 else
370 scheme = uc->nScheme;
371 if (!scheme) scheme = INTERNET_SCHEME_HTTP;
372 *len += strlenW( get_scheme_string( scheme ) );
374 *len += 1; /* ':' */
375 if (uc->lpszHostName) *len += 2; /* "//" */
377 if (uc->lpszUserName)
379 *len += comp_length( uc->dwUserNameLength, 0, uc->lpszUserName );
380 *len += 1; /* "@" */
382 else
384 if (uc->lpszPassword)
386 set_last_error( ERROR_INVALID_PARAMETER );
387 return FALSE;
390 if (uc->lpszPassword)
392 *len += 1; /* ":" */
393 *len += comp_length( uc->dwPasswordLength, 0, uc->lpszPassword );
395 if (uc->lpszHostName)
397 *len += comp_length( uc->dwHostNameLength, 0, uc->lpszHostName );
399 if (!uses_default_port( scheme, uc->nPort ))
401 WCHAR port[sizeof("65535")];
403 sprintfW( port, formatW, uc->nPort );
404 *len += strlenW( port );
405 *len += 1; /* ":" */
407 if (uc->lpszUrlPath && *uc->lpszUrlPath != '/') *len += 1; /* '/' */
409 if (uc->lpszUrlPath) *len += comp_length( uc->dwUrlPathLength, flags, uc->lpszUrlPath );
410 if (uc->lpszExtraInfo) *len += comp_length( uc->dwExtraInfoLength, flags, uc->lpszExtraInfo );
411 return TRUE;
414 /***********************************************************************
415 * WinHttpCreateUrl (winhttp.@)
417 BOOL WINAPI WinHttpCreateUrl( LPURL_COMPONENTS uc, DWORD flags, LPWSTR url, LPDWORD required )
419 static const WCHAR formatW[] = {'%','u',0};
420 static const WCHAR twoslashW[] = {'/','/'};
422 DWORD len;
423 INTERNET_SCHEME scheme;
425 TRACE("%p, 0x%08x, %p, %p\n", uc, flags, url, required);
427 if (!uc || uc->dwStructSize != sizeof(URL_COMPONENTS) || !required || !url)
429 set_last_error( ERROR_INVALID_PARAMETER );
430 return FALSE;
433 if (!calc_length( uc, flags, &len )) return FALSE;
435 if (*required < len)
437 *required = len + 1;
438 set_last_error( ERROR_INSUFFICIENT_BUFFER );
439 return FALSE;
442 url[0] = 0;
443 *required = len;
444 if (uc->lpszScheme)
446 len = comp_length( uc->dwSchemeLength, 0, uc->lpszScheme );
447 memcpy( url, uc->lpszScheme, len * sizeof(WCHAR) );
448 url += len;
450 scheme = get_scheme( uc->lpszScheme, len );
452 else
454 const WCHAR *schemeW;
455 scheme = uc->nScheme;
457 if (!scheme) scheme = INTERNET_SCHEME_HTTP;
459 schemeW = get_scheme_string( scheme );
460 len = strlenW( schemeW );
461 memcpy( url, schemeW, len * sizeof(WCHAR) );
462 url += len;
465 /* all schemes are followed by at least a colon */
466 *url = ':';
467 url++;
469 if (uc->lpszHostName)
471 memcpy( url, twoslashW, sizeof(twoslashW) );
472 url += sizeof(twoslashW) / sizeof(twoslashW[0]);
474 if (uc->lpszUserName)
476 len = comp_length( uc->dwUserNameLength, 0, uc->lpszUserName );
477 memcpy( url, uc->lpszUserName, len * sizeof(WCHAR) );
478 url += len;
480 if (uc->lpszPassword)
482 *url = ':';
483 url++;
485 len = comp_length( uc->dwPasswordLength, 0, uc->lpszPassword );
486 memcpy( url, uc->lpszPassword, len * sizeof(WCHAR) );
487 url += len;
489 *url = '@';
490 url++;
492 if (uc->lpszHostName)
494 len = comp_length( uc->dwHostNameLength, 0, uc->lpszHostName );
495 memcpy( url, uc->lpszHostName, len * sizeof(WCHAR) );
496 url += len;
498 if (!uses_default_port( scheme, uc->nPort ))
500 WCHAR port[sizeof("65535")];
502 sprintfW( port, formatW, uc->nPort );
503 *url = ':';
504 url++;
506 len = strlenW( port );
507 memcpy( url, port, len * sizeof(WCHAR) );
508 url += len;
511 /* add slash between hostname and path if necessary */
512 if (uc->lpszUrlPath && *uc->lpszUrlPath != '/')
514 *url = '/';
515 url++;
518 if (uc->lpszUrlPath)
520 len = comp_length( uc->dwUrlPathLength, 0, uc->lpszUrlPath );
521 if (flags & ICU_ESCAPE) url += copy_escape( url, uc->lpszUrlPath, len );
522 else
524 memcpy( url, uc->lpszUrlPath, len * sizeof(WCHAR) );
525 url += len;
528 if (uc->lpszExtraInfo)
530 len = comp_length( uc->dwExtraInfoLength, 0, uc->lpszExtraInfo );
531 if (flags & ICU_ESCAPE) url += copy_escape( url, uc->lpszExtraInfo, len );
532 else
534 memcpy( url, uc->lpszExtraInfo, len * sizeof(WCHAR) );
535 url += len;
538 *url = 0;
539 set_last_error( ERROR_SUCCESS );
540 return TRUE;