ddraw/tests: Rewrite LimitTest().
[wine.git] / dlls / webservices / url.c
blobe5e04e47a1f2545414979208c99b2a98d8207c70
1 /*
2 * Copyright 2016 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 <stdarg.h>
21 #include "windef.h"
22 #include "winbase.h"
23 #include "winuser.h"
24 #include "webservices.h"
26 #include "wine/debug.h"
27 #include "wine/list.h"
28 #include "wine/unicode.h"
29 #include "webservices_private.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(webservices);
33 static const WCHAR http[] = {'h','t','t','p'};
34 static const WCHAR https[] = {'h','t','t','p','s'};
35 static const WCHAR nettcp[] = {'n','e','t','.','t','c','p'};
36 static const WCHAR soapudp[] = {'s','o','a','p','.','u','d','p'};
37 static const WCHAR netpipe[] = {'n','e','t','.','p','i','p','e'};
39 static WS_URL_SCHEME_TYPE scheme_type( const WCHAR *str, ULONG len )
41 if (len == sizeof(http)/sizeof(http[0]) && !memicmpW( str, http, sizeof(http)/sizeof(http[0]) ))
42 return WS_URL_HTTP_SCHEME_TYPE;
44 if (len == sizeof(https)/sizeof(https[0]) && !memicmpW( str, https, sizeof(https)/sizeof(https[0]) ))
45 return WS_URL_HTTPS_SCHEME_TYPE;
47 if (len == sizeof(nettcp)/sizeof(nettcp[0]) && !memicmpW( str, nettcp, sizeof(nettcp)/sizeof(nettcp[0]) ))
48 return WS_URL_NETTCP_SCHEME_TYPE;
50 if (len == sizeof(soapudp)/sizeof(soapudp[0]) && !memicmpW( str, soapudp, sizeof(soapudp)/sizeof(soapudp[0]) ))
51 return WS_URL_SOAPUDP_SCHEME_TYPE;
53 if (len == sizeof(netpipe)/sizeof(netpipe[0]) && !memicmpW( str, netpipe, sizeof(netpipe)/sizeof(netpipe[0]) ))
54 return WS_URL_NETPIPE_SCHEME_TYPE;
56 return ~0u;
59 static USHORT default_port( WS_URL_SCHEME_TYPE scheme )
61 switch (scheme)
63 case WS_URL_HTTP_SCHEME_TYPE: return 80;
64 case WS_URL_HTTPS_SCHEME_TYPE: return 443;
65 case WS_URL_NETTCP_SCHEME_TYPE: return 808;
66 case WS_URL_SOAPUDP_SCHEME_TYPE:
67 case WS_URL_NETPIPE_SCHEME_TYPE: return 65535;
68 default:
69 ERR( "unhandled scheme %u\n", scheme );
70 return 0;
74 static unsigned char *strdup_utf8( const WCHAR *str, ULONG len, ULONG *ret_len )
76 unsigned char *ret;
77 *ret_len = WideCharToMultiByte( CP_UTF8, 0, str, len, NULL, 0, NULL, NULL );
78 if ((ret = heap_alloc( *ret_len )))
79 WideCharToMultiByte( CP_UTF8, 0, str, len, (char *)ret, *ret_len, NULL, NULL );
80 return ret;
83 static inline int url_decode_byte( char c1, char c2 )
85 int ret;
87 if (c1 >= '0' && c1 <= '9') ret = (c1 - '0') * 16;
88 else if (c1 >= 'a' && c1 <= 'f') ret = (c1 - 'a' + 10) * 16;
89 else ret = (c1 - 'A' + 10) * 16;
91 if (c2 >= '0' && c2 <= '9') ret += c2 - '0';
92 else if (c2 >= 'a' && c2 <= 'f') ret += c2 - 'a' + 10;
93 else ret += c2 - 'A' + 10;
95 return ret;
98 static WCHAR *url_decode( WCHAR *str, ULONG len, WS_HEAP *heap, ULONG *ret_len )
100 WCHAR *p = str, *q, *ret;
101 BOOL decode = FALSE, convert = FALSE;
102 ULONG i, len_utf8, len_left;
103 unsigned char *utf8, *r;
105 *ret_len = len;
106 for (i = 0; i < len; i++, p++)
108 if ((len - i) < 3) break;
109 if (p[0] == '%' && isxdigitW( p[1] ) && isxdigitW( p[2] ))
111 decode = TRUE;
112 if (url_decode_byte( p[1], p[2] ) > 159)
114 convert = TRUE;
115 break;
117 *ret_len -= 2;
120 if (!decode) return str;
121 if (!convert)
123 if (!(q = ret = ws_alloc( heap, *ret_len * sizeof(WCHAR) ))) return NULL;
124 p = str;
125 while (len)
127 if (len >= 3 && p[0] == '%' && isxdigitW( p[1] ) && isxdigitW( p[2] ))
129 *q++ = url_decode_byte( p[1], p[2] );
130 p += 3;
131 len -= 3;
133 else
135 *q++ = *p++;
136 len -= 1;
139 return ret;
142 if (!(r = utf8 = strdup_utf8( str, len, &len_utf8 ))) return NULL;
143 len_left = len_utf8;
144 while (len_left)
146 if (len_left >= 3 && r[0] == '%' && isxdigit( r[1] ) && isxdigit( r[2] ))
148 r[0] = url_decode_byte( r[1], r[2] );
149 len_left -= 3;
150 memmove( r + 1, r + 3, len_left );
151 len_utf8 -= 2;
153 else len_left -= 1;
154 r++;
157 if (!(*ret_len = MultiByteToWideChar( CP_UTF8, MB_ERR_INVALID_CHARS, (char *)utf8,
158 len_utf8, NULL, 0 )))
160 WARN( "invalid UTF-8 sequence\n" );
161 heap_free( utf8 );
162 return NULL;
164 if ((ret = ws_alloc( heap, *ret_len * sizeof(WCHAR) )))
165 MultiByteToWideChar( CP_UTF8, 0, (char *)utf8, len_utf8, ret, *ret_len );
167 heap_free( utf8 );
168 return ret;
171 /**************************************************************************
172 * WsDecodeUrl [webservices.@]
174 HRESULT WINAPI WsDecodeUrl( const WS_STRING *str, ULONG flags, WS_HEAP *heap, WS_URL **ret,
175 WS_ERROR *error )
177 HRESULT hr = WS_E_QUOTA_EXCEEDED;
178 WCHAR *p, *q, *decoded = NULL;
179 WS_HTTP_URL *url = NULL;
180 ULONG len, len_decoded, port = 0;
182 TRACE( "%s %08x %p %p %p\n", str ? debugstr_wn(str->chars, str->length) : "null", flags,
183 heap, ret, error );
184 if (error) FIXME( "ignoring error parameter\n" );
186 if (!str || !heap) return E_INVALIDARG;
187 if (!str->length) return WS_E_INVALID_FORMAT;
188 if (flags)
190 FIXME( "unimplemented flags %08x\n", flags );
191 return E_NOTIMPL;
193 if (!(decoded = url_decode( str->chars, str->length, heap, &len_decoded )) ||
194 !(url = ws_alloc( heap, sizeof(*url) ))) goto error;
196 hr = WS_E_INVALID_FORMAT;
198 p = q = decoded;
199 len = len_decoded;
200 while (len && *q != ':') { q++; len--; };
201 if (*q != ':') goto error;
202 if ((url->url.scheme = scheme_type( p, q - p )) == ~0u) goto error;
204 if (!--len || *++q != '/') goto error;
205 if (!--len || *++q != '/') goto error;
207 p = ++q; len--;
208 while (len && *q != '/' && *q != ':' && *q != '?' && *q != '#') { q++; len--; };
209 if (q == p) goto error;
210 url->host.length = q - p;
211 url->host.chars = p;
213 if (len && *q == ':')
215 p = ++q; len--;
216 while (len && isdigitW( *q ))
218 if ((port = port * 10 + *q - '0') > 65535) goto error;
219 q++; len--;
221 url->port = port;
222 url->portAsString.length = q - p;
223 url->portAsString.chars = p;
225 if (!port)
227 url->port = default_port( url->url.scheme );
228 url->portAsString.length = 0;
229 url->portAsString.chars = NULL;
232 if (len && *q == '/')
234 p = q;
235 while (len && *q != '?') { q++; len--; };
236 url->path.length = q - p;
237 url->path.chars = p;
239 else url->path.length = 0;
241 if (len && *q == '?')
243 p = ++q; len--;
244 while (len && *q != '#') { q++; len--; };
245 url->query.length = q - p;
246 url->query.chars = p;
248 else url->query.length = 0;
250 if (len && *q == '#')
252 p = ++q; len--;
253 while (len && *q != '#') { q++; len--; };
254 url->fragment.length = q - p;
255 url->fragment.chars = p;
257 else url->fragment.length = 0;
259 *ret = (WS_URL *)url;
260 return S_OK;
262 error:
263 if (decoded != str->chars) ws_free( heap, decoded, len_decoded );
264 ws_free( heap, url, sizeof(*url) );
265 return hr;
268 static const WCHAR *scheme_str( WS_URL_SCHEME_TYPE scheme, ULONG *len )
270 switch (scheme)
272 case WS_URL_HTTP_SCHEME_TYPE:
273 *len = sizeof(http)/sizeof(http[0]);
274 return http;
276 case WS_URL_HTTPS_SCHEME_TYPE:
277 *len = sizeof(https)/sizeof(https[0]);
278 return https;
280 case WS_URL_NETTCP_SCHEME_TYPE:
281 *len = sizeof(nettcp)/sizeof(nettcp[0]);
282 return nettcp;
284 case WS_URL_SOAPUDP_SCHEME_TYPE:
285 *len = sizeof(soapudp)/sizeof(soapudp[0]);
286 return soapudp;
288 case WS_URL_NETPIPE_SCHEME_TYPE:
289 *len = sizeof(netpipe)/sizeof(netpipe[0]);
290 return netpipe;
292 default:
293 ERR( "unhandled scheme %u\n", scheme );
294 return NULL;
298 static inline ULONG escape_size( unsigned char ch, const char *except )
300 const char *p = except;
301 while (*p)
303 if (*p == ch) return 1;
304 p++;
306 if ((ch >= 'a' && ch <= 'z' ) || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9')) return 1;
307 if (ch < 33 || ch > 126) return 3;
308 switch (ch)
310 case '/':
311 case '?':
312 case '"':
313 case '#':
314 case '%':
315 case '<':
316 case '>':
317 case '\\':
318 case '[':
319 case ']':
320 case '^':
321 case '`':
322 case '{':
323 case '|':
324 case '}':
325 return 3;
326 default:
327 return 1;
331 static HRESULT url_encode_size( const WCHAR *str, ULONG len, const char *except, ULONG *ret_len )
333 ULONG i, len_utf8;
334 BOOL convert = FALSE;
335 unsigned char *utf8;
337 *ret_len = 0;
338 for (i = 0; i < len; i++)
340 if (str[i] > 159)
342 convert = TRUE;
343 break;
345 *ret_len += escape_size( str[i], except );
347 if (!convert) return S_OK;
349 *ret_len = 0;
350 if (!(utf8 = strdup_utf8( str, len, &len_utf8 ))) return E_OUTOFMEMORY;
351 for (i = 0; i < len_utf8; i++) *ret_len += escape_size( utf8[i], except );
352 heap_free( utf8 );
354 return S_OK;
357 static ULONG url_encode_byte( unsigned char byte, const char *except, WCHAR *buf )
359 static const WCHAR hex[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
360 switch (escape_size( byte, except ))
362 case 3:
363 buf[0] = '%';
364 buf[1] = hex[(byte >> 4) & 0xf];
365 buf[2] = hex[byte & 0xf];
366 return 3;
368 case 1:
369 buf[0] = byte;
370 return 1;
372 default:
373 ERR( "unhandled escape size\n" );
374 return 0;
379 static HRESULT url_encode( const WCHAR *str, ULONG len, WCHAR *buf, const char *except, ULONG *ret_len )
381 HRESULT hr = S_OK;
382 ULONG i, len_utf8, len_enc;
383 BOOL convert = FALSE;
384 WCHAR *p = buf;
385 unsigned char *utf8;
387 *ret_len = 0;
388 for (i = 0; i < len; i++)
390 if (str[i] > 159)
392 convert = TRUE;
393 break;
395 len_enc = url_encode_byte( str[i], except, p );
396 *ret_len += len_enc;
397 p += len_enc;
399 if (!convert) return S_OK;
401 p = buf;
402 *ret_len = 0;
403 if (!(utf8 = strdup_utf8( str, len, &len_utf8 ))) return E_OUTOFMEMORY;
404 for (i = 0; i < len_utf8; i++)
406 len_enc = url_encode_byte( utf8[i], except, p );
407 *ret_len += len_enc;
408 p += len_enc;
411 heap_free( utf8 );
412 return hr;
415 /**************************************************************************
416 * WsEncodeUrl [webservices.@]
418 HRESULT WINAPI WsEncodeUrl( const WS_URL *base, ULONG flags, WS_HEAP *heap, WS_STRING *ret,
419 WS_ERROR *error )
421 static const WCHAR fmtW[] = {':','%','u',0};
422 ULONG len = 0, len_scheme, len_enc, ret_size;
423 const WS_HTTP_URL *url = (const WS_HTTP_URL *)base;
424 const WCHAR *scheme;
425 WCHAR *str, *p, *q;
426 ULONG port = 0;
427 HRESULT hr;
429 TRACE( "%p %08x %p %p %p\n", base, flags, heap, ret, error );
430 if (error) FIXME( "ignoring error parameter\n" );
432 if (!url || !heap || !ret) return E_INVALIDARG;
433 if (flags)
435 FIXME( "unimplemented flags %08x\n", flags );
436 return E_NOTIMPL;
438 if (!(scheme = scheme_str( url->url.scheme, &len_scheme ))) return WS_E_INVALID_FORMAT;
439 len = len_scheme + 3; /* '://' */
440 len += 6; /* ':65535' */
442 if ((hr = url_encode_size( url->host.chars, url->host.length, "", &len_enc )) != S_OK)
443 return hr;
444 len += len_enc;
446 if ((hr = url_encode_size( url->path.chars, url->path.length, "/", &len_enc )) != S_OK)
447 return hr;
448 len += len_enc;
450 if ((hr = url_encode_size( url->query.chars, url->query.length, "/?", &len_enc )) != S_OK)
451 return hr;
452 len += len_enc + 1; /* '?' */
454 if ((hr = url_encode_size( url->fragment.chars, url->fragment.length, "/?", &len_enc )) != S_OK)
455 return hr;
456 len += len_enc + 1; /* '#' */
458 ret_size = len * sizeof(WCHAR);
459 if (!(str = ws_alloc( heap, ret_size ))) return WS_E_QUOTA_EXCEEDED;
461 memcpy( str, scheme, len_scheme * sizeof(WCHAR) );
462 p = str + len_scheme;
463 p[0] = ':';
464 p[1] = p[2] = '/';
465 p += 3;
467 if ((hr = url_encode( url->host.chars, url->host.length, p, "", &len_enc )) != S_OK)
468 goto error;
469 p += len_enc;
471 if (url->portAsString.length)
473 q = url->portAsString.chars;
474 len = url->portAsString.length;
475 while (len && isdigitW( *q ))
477 if ((port = port * 10 + *q - '0') > 65535)
479 hr = WS_E_INVALID_FORMAT;
480 goto error;
482 q++; len--;
484 if (url->port && port != url->port)
486 hr = E_INVALIDARG;
487 goto error;
489 } else port = url->port;
491 if (port == default_port( url->url.scheme )) port = 0;
492 if (port)
494 WCHAR buf[7];
495 len = sprintfW( buf, fmtW, port );
496 memcpy( p, buf, len * sizeof(WCHAR) );
497 p += len;
500 if ((hr = url_encode( url->path.chars, url->path.length, p, "/", &len_enc )) != S_OK)
501 goto error;
502 p += len_enc;
504 if (url->query.length)
506 *p++ = '?';
507 if ((hr = url_encode( url->query.chars, url->query.length, p, "/?", &len_enc )) != S_OK)
508 goto error;
509 p += len_enc;
512 if (url->fragment.length)
514 *p++ = '#';
515 if ((hr = url_encode( url->fragment.chars, url->fragment.length, p, "/?", &len_enc )) != S_OK)
516 goto error;
517 p += len_enc;
520 ret->length = p - str;
521 ret->chars = str;
522 return S_OK;
524 error:
525 ws_free( heap, str, ret_size );
526 return hr;