2008-09-23 13:32:26 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2008 Hans Leidekker for CodeWeavers
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
2019-11-20 14:29:08 +01:00
|
|
|
#include <wchar.h>
|
2008-09-23 13:32:26 +02:00
|
|
|
#include "windef.h"
|
|
|
|
#include "winbase.h"
|
2019-11-20 14:29:08 +01:00
|
|
|
#include "ws2tcpip.h"
|
2008-11-17 16:01:48 +01:00
|
|
|
#include "winreg.h"
|
2008-09-23 13:32:26 +02:00
|
|
|
#include "winhttp.h"
|
2008-11-17 16:01:48 +01:00
|
|
|
#include "shlwapi.h"
|
2008-09-23 13:32:26 +02:00
|
|
|
|
2018-08-27 14:36:38 +02:00
|
|
|
#include "wine/debug.h"
|
2008-09-23 13:32:26 +02:00
|
|
|
#include "winhttp_private.h"
|
|
|
|
|
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(winhttp);
|
|
|
|
|
2019-02-20 09:58:20 +01:00
|
|
|
struct url_component
|
2008-11-17 16:01:48 +01:00
|
|
|
{
|
2019-02-20 09:58:20 +01:00
|
|
|
WCHAR **str;
|
|
|
|
DWORD *len;
|
|
|
|
};
|
|
|
|
|
|
|
|
static DWORD set_component( struct url_component *comp, WCHAR *value, DWORD len, DWORD flags, BOOL *overflow )
|
|
|
|
{
|
|
|
|
if (*comp->str && !*comp->len) return ERROR_INVALID_PARAMETER;
|
|
|
|
if (!*comp->len) return ERROR_SUCCESS;
|
|
|
|
if (!*comp->str)
|
2008-11-17 16:01:48 +01:00
|
|
|
{
|
2019-02-20 09:58:20 +01:00
|
|
|
if (len && *comp->len && (flags & (ICU_DECODE|ICU_ESCAPE))) return ERROR_INVALID_PARAMETER;
|
|
|
|
*comp->str = value;
|
|
|
|
*comp->len = len;
|
2008-11-17 16:01:48 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-02-20 09:58:20 +01:00
|
|
|
if (len >= *comp->len)
|
2008-11-17 16:01:48 +01:00
|
|
|
{
|
2019-02-20 09:58:20 +01:00
|
|
|
*comp->len = len + 1;
|
2016-08-30 20:52:21 +02:00
|
|
|
*overflow = TRUE;
|
2016-08-16 12:30:01 +02:00
|
|
|
return ERROR_SUCCESS;
|
2008-11-17 16:01:48 +01:00
|
|
|
}
|
2019-02-20 09:58:20 +01:00
|
|
|
memcpy( *comp->str, value, len * sizeof(WCHAR) );
|
|
|
|
(*comp->str)[len] = 0;
|
|
|
|
*comp->len = len;
|
2008-11-17 16:01:48 +01:00
|
|
|
}
|
2016-08-16 12:30:01 +02:00
|
|
|
return ERROR_SUCCESS;
|
2008-11-17 16:01:48 +01:00
|
|
|
}
|
|
|
|
|
2011-08-05 15:00:19 +02:00
|
|
|
static WCHAR *decode_url( LPCWSTR url, DWORD *len )
|
2008-11-17 16:01:48 +01:00
|
|
|
{
|
2011-08-05 15:00:19 +02:00
|
|
|
const WCHAR *p = url;
|
|
|
|
WCHAR hex[3], *q, *ret;
|
|
|
|
|
2021-06-08 16:27:44 +02:00
|
|
|
if (!(ret = malloc( *len * sizeof(WCHAR) ))) return NULL;
|
2011-08-05 15:00:19 +02:00
|
|
|
q = ret;
|
|
|
|
while (*len > 0)
|
|
|
|
{
|
2019-11-20 14:29:08 +01:00
|
|
|
if (p[0] == '%' && iswxdigit( p[1] ) && iswxdigit( p[2] ))
|
2011-08-05 15:00:19 +02:00
|
|
|
{
|
|
|
|
hex[0] = p[1];
|
|
|
|
hex[1] = p[2];
|
|
|
|
hex[2] = 0;
|
2019-11-20 14:29:08 +01:00
|
|
|
*q++ = wcstol( hex, NULL, 16 );
|
2011-08-05 15:00:19 +02:00
|
|
|
p += 3;
|
|
|
|
*len -= 3;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*q++ = *p++;
|
|
|
|
*len -= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*len = q - ret;
|
|
|
|
return ret;
|
2008-11-17 16:01:48 +01:00
|
|
|
}
|
2008-09-23 13:32:26 +02:00
|
|
|
|
2018-11-06 16:09:04 +01:00
|
|
|
static inline BOOL need_escape( WCHAR ch )
|
2011-12-01 13:02:20 +01:00
|
|
|
{
|
2019-11-22 16:32:24 +01:00
|
|
|
const WCHAR *p = L" \"#%<>[\\]^`{|}~";
|
2011-12-01 13:02:20 +01:00
|
|
|
|
2018-10-04 10:32:01 +02:00
|
|
|
if (ch <= 31 || ch >= 127) return TRUE;
|
|
|
|
while (*p)
|
2011-12-01 13:02:20 +01:00
|
|
|
{
|
2018-10-04 10:32:01 +02:00
|
|
|
if (ch == *p++) return TRUE;
|
2011-12-01 13:02:20 +01:00
|
|
|
}
|
2018-10-04 10:32:01 +02:00
|
|
|
return FALSE;
|
2011-12-01 13:02:20 +01:00
|
|
|
}
|
|
|
|
|
2018-11-06 16:09:04 +01:00
|
|
|
static BOOL escape_string( const WCHAR *src, DWORD src_len, WCHAR *dst, DWORD *dst_len )
|
2011-12-01 13:02:20 +01:00
|
|
|
{
|
2019-11-22 16:32:24 +01:00
|
|
|
static const WCHAR hex[] = L"0123456789ABCDEF";
|
2011-12-01 13:02:20 +01:00
|
|
|
WCHAR *p = dst;
|
2018-11-06 16:09:04 +01:00
|
|
|
DWORD i;
|
2011-12-01 13:02:20 +01:00
|
|
|
|
2018-11-06 16:09:04 +01:00
|
|
|
*dst_len = src_len;
|
|
|
|
for (i = 0; i < src_len; i++)
|
2011-12-01 13:02:20 +01:00
|
|
|
{
|
2018-11-06 16:09:04 +01:00
|
|
|
if (src[i] > 0xff) return FALSE;
|
|
|
|
if (need_escape( src[i] ))
|
2011-12-01 13:02:20 +01:00
|
|
|
{
|
2018-08-24 13:32:42 +02:00
|
|
|
if (dst)
|
|
|
|
{
|
|
|
|
p[0] = '%';
|
|
|
|
p[1] = hex[(src[i] >> 4) & 0xf];
|
|
|
|
p[2] = hex[src[i] & 0xf];
|
2018-10-04 10:32:01 +02:00
|
|
|
p += 3;
|
2018-08-24 13:32:42 +02:00
|
|
|
}
|
2018-11-06 16:09:04 +01:00
|
|
|
*dst_len += 2;
|
2011-12-01 13:02:20 +01:00
|
|
|
}
|
2018-10-04 10:32:01 +02:00
|
|
|
else if (dst) *p++ = src[i];
|
2011-12-01 13:02:20 +01:00
|
|
|
}
|
2018-10-04 10:32:01 +02:00
|
|
|
|
2018-11-06 16:09:04 +01:00
|
|
|
if (dst) dst[*dst_len] = 0;
|
|
|
|
return TRUE;
|
2011-12-01 13:02:20 +01:00
|
|
|
}
|
|
|
|
|
2018-11-06 16:09:04 +01:00
|
|
|
static DWORD escape_url( const WCHAR *url, DWORD *len, WCHAR **ret )
|
2011-12-01 13:02:20 +01:00
|
|
|
{
|
2018-08-24 13:32:42 +02:00
|
|
|
const WCHAR *p;
|
|
|
|
DWORD len_base, len_path;
|
2011-12-01 13:02:20 +01:00
|
|
|
|
2019-11-20 14:29:08 +01:00
|
|
|
if ((p = wcsrchr( url, '/' )))
|
2011-12-01 13:02:20 +01:00
|
|
|
{
|
2018-08-24 13:32:42 +02:00
|
|
|
len_base = p - url;
|
2018-11-06 16:09:04 +01:00
|
|
|
if (!escape_string( p, *len - len_base, NULL, &len_path )) return ERROR_INVALID_PARAMETER;
|
2011-12-01 13:02:20 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-08-24 13:32:42 +02:00
|
|
|
len_base = *len;
|
|
|
|
len_path = 0;
|
2011-12-01 13:02:20 +01:00
|
|
|
}
|
2018-08-24 13:32:42 +02:00
|
|
|
|
2021-06-08 16:27:44 +02:00
|
|
|
if (!(*ret = malloc( (len_base + len_path + 1) * sizeof(WCHAR) ))) return ERROR_OUTOFMEMORY;
|
2018-11-06 16:09:04 +01:00
|
|
|
memcpy( *ret, url, len_base * sizeof(WCHAR) );
|
2018-08-24 13:32:42 +02:00
|
|
|
|
2018-11-06 16:09:04 +01:00
|
|
|
if (p) escape_string( p, *len - (p - url), *ret + len_base, &len_path );
|
|
|
|
(*ret)[len_base + len_path] = 0;
|
2018-08-24 13:32:42 +02:00
|
|
|
|
|
|
|
*len = len_base + len_path;
|
2018-11-06 16:09:04 +01:00
|
|
|
return ERROR_SUCCESS;
|
2011-12-01 13:02:20 +01:00
|
|
|
}
|
|
|
|
|
2016-08-16 12:30:02 +02:00
|
|
|
static DWORD parse_port( const WCHAR *str, DWORD len, INTERNET_PORT *ret )
|
|
|
|
{
|
|
|
|
const WCHAR *p = str;
|
|
|
|
DWORD port = 0;
|
2020-02-07 15:07:07 +01:00
|
|
|
while (len && '0' <= *p && *p <= '9')
|
2016-08-16 12:30:02 +02:00
|
|
|
{
|
|
|
|
if ((port = port * 10 + *p - '0') > 65535) return ERROR_WINHTTP_INVALID_URL;
|
|
|
|
p++; len--;
|
|
|
|
}
|
|
|
|
*ret = port;
|
|
|
|
return ERROR_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2008-09-23 13:32:26 +02:00
|
|
|
/***********************************************************************
|
|
|
|
* WinHttpCrackUrl (winhttp.@)
|
|
|
|
*/
|
2008-11-17 16:01:48 +01:00
|
|
|
BOOL WINAPI WinHttpCrackUrl( LPCWSTR url, DWORD len, DWORD flags, LPURL_COMPONENTSW uc )
|
2008-09-23 13:32:26 +02:00
|
|
|
{
|
2011-12-01 13:02:20 +01:00
|
|
|
WCHAR *p, *q, *r, *url_decoded = NULL, *url_escaped = NULL;
|
2019-02-20 09:58:20 +01:00
|
|
|
INTERNET_SCHEME scheme_number = 0;
|
|
|
|
struct url_component scheme, username, password, hostname, path, extra;
|
2016-08-30 20:52:21 +02:00
|
|
|
BOOL overflow = FALSE;
|
|
|
|
DWORD err;
|
2008-11-17 16:01:48 +01:00
|
|
|
|
2016-08-16 12:30:02 +02:00
|
|
|
TRACE("%s, %d, %x, %p\n", debugstr_wn(url, len), len, flags, uc);
|
2008-09-23 13:32:26 +02:00
|
|
|
|
2019-02-20 09:58:19 +01:00
|
|
|
if (!url || !uc || uc->dwStructSize != sizeof(*uc))
|
2008-09-23 13:32:26 +02:00
|
|
|
{
|
2018-11-22 21:15:55 +01:00
|
|
|
SetLastError( ERROR_INVALID_PARAMETER );
|
2008-11-17 16:01:48 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
2019-11-20 14:29:08 +01:00
|
|
|
if (!len) len = lstrlenW( url );
|
2008-11-17 16:01:48 +01:00
|
|
|
|
2011-12-01 13:02:20 +01:00
|
|
|
if (flags & ICU_ESCAPE)
|
|
|
|
{
|
2018-11-06 16:09:04 +01:00
|
|
|
if ((err = escape_url( url, &len, &url_escaped )))
|
2011-12-01 13:02:20 +01:00
|
|
|
{
|
2018-11-22 21:15:55 +01:00
|
|
|
SetLastError( err );
|
2011-12-01 13:02:20 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
url = url_escaped;
|
|
|
|
}
|
|
|
|
else if (flags & ICU_DECODE)
|
2008-11-17 16:01:48 +01:00
|
|
|
{
|
2011-08-05 15:00:19 +02:00
|
|
|
if (!(url_decoded = decode_url( url, &len )))
|
2008-11-17 16:01:48 +01:00
|
|
|
{
|
2018-11-22 21:15:55 +01:00
|
|
|
SetLastError( ERROR_OUTOFMEMORY );
|
2008-11-17 16:01:48 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
2011-08-05 15:00:19 +02:00
|
|
|
url = url_decoded;
|
2008-09-23 13:32:26 +02:00
|
|
|
}
|
2019-11-20 14:29:08 +01:00
|
|
|
if (!(p = wcschr( url, ':' )))
|
2009-01-05 16:00:02 +01:00
|
|
|
{
|
2018-11-22 21:15:55 +01:00
|
|
|
SetLastError( ERROR_WINHTTP_UNRECOGNIZED_SCHEME );
|
2009-01-05 16:00:02 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
2019-11-22 16:32:24 +01:00
|
|
|
if (p - url == 4 && !wcsnicmp( url, L"http", 4 )) scheme_number = INTERNET_SCHEME_HTTP;
|
|
|
|
else if (p - url == 5 && !wcsnicmp( url, L"https", 5 )) scheme_number = INTERNET_SCHEME_HTTPS;
|
2009-01-05 16:00:02 +01:00
|
|
|
else
|
|
|
|
{
|
2016-08-16 12:30:01 +02:00
|
|
|
err = ERROR_WINHTTP_UNRECOGNIZED_SCHEME;
|
2009-01-05 16:00:02 +01:00
|
|
|
goto exit;
|
|
|
|
}
|
2016-08-16 12:30:01 +02:00
|
|
|
|
2019-02-20 09:58:20 +01:00
|
|
|
scheme.str = &uc->lpszScheme;
|
|
|
|
scheme.len = &uc->dwSchemeLength;
|
|
|
|
|
|
|
|
if ((err = set_component( &scheme, (WCHAR *)url, p - url, flags, &overflow ))) goto exit;
|
2008-11-17 16:01:48 +01:00
|
|
|
|
|
|
|
p++; /* skip ':' */
|
2016-08-16 12:30:01 +02:00
|
|
|
if (!p[0] || p[0] != '/' || p[1] != '/')
|
|
|
|
{
|
|
|
|
err = ERROR_WINHTTP_INVALID_URL;
|
|
|
|
goto exit;
|
|
|
|
}
|
2008-11-17 16:01:48 +01:00
|
|
|
p += 2;
|
2016-08-16 12:30:01 +02:00
|
|
|
if (!p[0])
|
|
|
|
{
|
|
|
|
err = ERROR_WINHTTP_INVALID_URL;
|
|
|
|
goto exit;
|
|
|
|
}
|
2019-02-20 09:58:20 +01:00
|
|
|
|
|
|
|
username.str = &uc->lpszUserName;
|
|
|
|
username.len = &uc->dwUserNameLength;
|
|
|
|
|
|
|
|
password.str = &uc->lpszPassword;
|
|
|
|
password.len = &uc->dwPasswordLength;
|
|
|
|
|
2019-11-20 14:29:08 +01:00
|
|
|
if ((q = wmemchr( p, '@', len - (p - url) )) && !(wmemchr( p, '/', q - p )))
|
2008-11-17 16:01:48 +01:00
|
|
|
{
|
2019-02-20 09:58:20 +01:00
|
|
|
|
2019-11-20 14:29:08 +01:00
|
|
|
if ((r = wmemchr( p, ':', q - p )))
|
2008-11-17 16:01:48 +01:00
|
|
|
{
|
2019-02-20 09:58:20 +01:00
|
|
|
if ((err = set_component( &username, p, r - p, flags, &overflow ))) goto exit;
|
2008-11-17 16:01:48 +01:00
|
|
|
r++;
|
2019-02-20 09:58:20 +01:00
|
|
|
if ((err = set_component( &password, r, q - r, flags, &overflow ))) goto exit;
|
2008-11-17 16:01:48 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-02-20 09:58:20 +01:00
|
|
|
if ((err = set_component( &username, p, q - p, flags, &overflow ))) goto exit;
|
|
|
|
if ((err = set_component( &password, NULL, 0, flags, &overflow ))) goto exit;
|
2008-11-17 16:01:48 +01:00
|
|
|
}
|
|
|
|
p = q + 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-02-20 09:58:20 +01:00
|
|
|
if ((err = set_component( &username, NULL, 0, flags, &overflow ))) goto exit;
|
|
|
|
if ((err = set_component( &password, NULL, 0, flags, &overflow ))) goto exit;
|
2008-11-17 16:01:48 +01:00
|
|
|
}
|
2019-02-20 09:58:20 +01:00
|
|
|
|
|
|
|
hostname.str = &uc->lpszHostName;
|
|
|
|
hostname.len = &uc->dwHostNameLength;
|
|
|
|
|
|
|
|
path.str = &uc->lpszUrlPath;
|
|
|
|
path.len = &uc->dwUrlPathLength;
|
|
|
|
|
|
|
|
extra.str = &uc->lpszExtraInfo;
|
|
|
|
extra.len = &uc->dwExtraInfoLength;
|
|
|
|
|
2019-11-20 14:29:08 +01:00
|
|
|
if ((q = wmemchr( p, '/', len - (p - url) )))
|
2008-11-17 16:01:48 +01:00
|
|
|
{
|
2019-11-20 14:29:08 +01:00
|
|
|
if ((r = wmemchr( p, ':', q - p )))
|
2008-12-31 07:35:53 +01:00
|
|
|
{
|
2019-02-20 09:58:20 +01:00
|
|
|
if ((err = set_component( &hostname, p, r - p, flags, &overflow ))) goto exit;
|
2008-12-31 07:35:53 +01:00
|
|
|
r++;
|
2016-08-16 12:30:02 +02:00
|
|
|
if ((err = parse_port( r, q - r, &uc->nPort ))) goto exit;
|
2008-12-31 07:35:53 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-02-20 09:58:20 +01:00
|
|
|
if ((err = set_component( &hostname, p, q - p, flags, &overflow ))) goto exit;
|
|
|
|
if (scheme_number == INTERNET_SCHEME_HTTP) uc->nPort = INTERNET_DEFAULT_HTTP_PORT;
|
|
|
|
if (scheme_number == INTERNET_SCHEME_HTTPS) uc->nPort = INTERNET_DEFAULT_HTTPS_PORT;
|
2008-12-31 07:35:53 +01:00
|
|
|
}
|
2008-11-17 16:01:48 +01:00
|
|
|
|
2019-11-20 14:29:08 +01:00
|
|
|
if ((r = wmemchr( q, '?', len - (q - url) )))
|
2008-11-17 16:01:48 +01:00
|
|
|
{
|
2019-02-20 09:58:20 +01:00
|
|
|
if (*extra.len)
|
2019-02-20 09:58:19 +01:00
|
|
|
{
|
2019-02-20 09:58:20 +01:00
|
|
|
if ((err = set_component( &path, q, r - q, flags, &overflow ))) goto exit;
|
|
|
|
if ((err = set_component( &extra, r, len - (r - url), flags, &overflow ))) goto exit;
|
2019-02-20 09:58:19 +01:00
|
|
|
}
|
2019-02-20 09:58:20 +01:00
|
|
|
else if ((err = set_component( &path, q, len - (q - url), flags, &overflow ))) goto exit;
|
2008-11-17 16:01:48 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-02-20 09:58:20 +01:00
|
|
|
if ((err = set_component( &path, q, len - (q - url), flags, &overflow ))) goto exit;
|
|
|
|
if ((err = set_component( &extra, (WCHAR *)url + len, 0, flags, &overflow ))) goto exit;
|
2008-11-17 16:01:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-11-20 14:29:08 +01:00
|
|
|
if ((r = wmemchr( p, ':', len - (p - url) )))
|
2008-12-31 07:35:53 +01:00
|
|
|
{
|
2019-02-20 09:58:20 +01:00
|
|
|
if ((err = set_component( &hostname, p, r - p, flags, &overflow ))) goto exit;
|
2008-12-31 07:35:53 +01:00
|
|
|
r++;
|
2016-08-16 12:30:02 +02:00
|
|
|
if ((err = parse_port( r, len - (r - url), &uc->nPort ))) goto exit;
|
2008-12-31 07:35:53 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-02-20 09:58:20 +01:00
|
|
|
if ((err = set_component( &hostname, p, len - (p - url), flags, &overflow ))) goto exit;
|
|
|
|
if (scheme_number == INTERNET_SCHEME_HTTP) uc->nPort = INTERNET_DEFAULT_HTTP_PORT;
|
|
|
|
if (scheme_number == INTERNET_SCHEME_HTTPS) uc->nPort = INTERNET_DEFAULT_HTTPS_PORT;
|
2008-12-31 07:35:53 +01:00
|
|
|
}
|
2019-02-20 09:58:20 +01:00
|
|
|
if ((err = set_component( &path, (WCHAR *)url + len, 0, flags, &overflow ))) goto exit;
|
|
|
|
if ((err = set_component( &extra, (WCHAR *)url + len, 0, flags, &overflow ))) goto exit;
|
2008-11-17 16:01:48 +01:00
|
|
|
}
|
|
|
|
|
2019-02-20 09:58:20 +01:00
|
|
|
TRACE("scheme(%s) host(%s) port(%d) path(%s) extra(%s)\n", debugstr_wn(*scheme.str, *scheme.len),
|
|
|
|
debugstr_wn(*hostname.str, *hostname.len ), uc->nPort, debugstr_wn(*path.str, *path.len),
|
|
|
|
debugstr_wn(*extra.str, *extra.len));
|
2008-11-17 16:01:48 +01:00
|
|
|
|
|
|
|
exit:
|
2016-08-16 12:30:01 +02:00
|
|
|
if (!err)
|
|
|
|
{
|
2016-08-30 20:52:21 +02:00
|
|
|
if (overflow) err = ERROR_INSUFFICIENT_BUFFER;
|
2019-02-20 09:58:20 +01:00
|
|
|
uc->nScheme = scheme_number;
|
2016-08-16 12:30:01 +02:00
|
|
|
}
|
2021-06-08 16:27:44 +02:00
|
|
|
free( url_decoded );
|
|
|
|
free( url_escaped );
|
2018-11-22 21:15:55 +01:00
|
|
|
SetLastError( err );
|
2016-08-16 12:30:01 +02:00
|
|
|
return !err;
|
2008-09-23 13:32:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static INTERNET_SCHEME get_scheme( const WCHAR *scheme, DWORD len )
|
|
|
|
{
|
2019-11-22 16:32:24 +01:00
|
|
|
if (!wcsncmp( scheme, L"http", len )) return INTERNET_SCHEME_HTTP;
|
|
|
|
if (!wcsncmp( scheme, L"https", len )) return INTERNET_SCHEME_HTTPS;
|
2008-09-23 13:32:26 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const WCHAR *get_scheme_string( INTERNET_SCHEME scheme )
|
|
|
|
{
|
2019-11-22 16:32:24 +01:00
|
|
|
if (scheme == INTERNET_SCHEME_HTTP) return L"http";
|
|
|
|
if (scheme == INTERNET_SCHEME_HTTPS) return L"https";
|
2008-09-23 13:32:26 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL uses_default_port( INTERNET_SCHEME scheme, INTERNET_PORT port )
|
|
|
|
{
|
|
|
|
if ((scheme == INTERNET_SCHEME_HTTP) && (port == INTERNET_DEFAULT_HTTP_PORT)) return TRUE;
|
|
|
|
if ((scheme == INTERNET_SCHEME_HTTPS) && (port == INTERNET_DEFAULT_HTTPS_PORT)) return TRUE;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2018-10-04 10:32:01 +02:00
|
|
|
static DWORD get_comp_length( DWORD len, DWORD flags, WCHAR *comp )
|
2008-09-23 13:32:26 +02:00
|
|
|
{
|
|
|
|
DWORD ret;
|
|
|
|
unsigned int i;
|
|
|
|
|
2019-11-20 14:29:08 +01:00
|
|
|
ret = len ? len : lstrlenW( comp );
|
2008-09-23 13:32:26 +02:00
|
|
|
if (!(flags & ICU_ESCAPE)) return ret;
|
2018-11-06 16:09:04 +01:00
|
|
|
for (i = 0; i < len; i++) if (need_escape( comp[i] )) ret += 2;
|
2008-09-23 13:32:26 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-10-04 10:32:01 +02:00
|
|
|
static BOOL get_url_length( URL_COMPONENTS *uc, DWORD flags, DWORD *len )
|
2008-09-23 13:32:26 +02:00
|
|
|
{
|
|
|
|
INTERNET_SCHEME scheme;
|
|
|
|
|
|
|
|
*len = 0;
|
|
|
|
if (uc->lpszScheme)
|
|
|
|
{
|
2018-10-04 10:32:01 +02:00
|
|
|
DWORD scheme_len = get_comp_length( uc->dwSchemeLength, 0, uc->lpszScheme );
|
2008-09-23 13:32:26 +02:00
|
|
|
*len += scheme_len;
|
|
|
|
scheme = get_scheme( uc->lpszScheme, scheme_len );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
scheme = uc->nScheme;
|
|
|
|
if (!scheme) scheme = INTERNET_SCHEME_HTTP;
|
2019-11-20 14:29:08 +01:00
|
|
|
*len += lstrlenW( get_scheme_string( scheme ) );
|
2008-09-23 13:32:26 +02:00
|
|
|
}
|
2018-10-04 10:32:01 +02:00
|
|
|
*len += 3; /* "://" */
|
2008-09-23 13:32:26 +02:00
|
|
|
|
|
|
|
if (uc->lpszUserName)
|
|
|
|
{
|
2018-10-04 10:32:01 +02:00
|
|
|
*len += get_comp_length( uc->dwUserNameLength, 0, uc->lpszUserName );
|
2008-09-23 13:32:26 +02:00
|
|
|
*len += 1; /* "@" */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (uc->lpszPassword)
|
|
|
|
{
|
2018-11-22 21:15:55 +01:00
|
|
|
SetLastError( ERROR_INVALID_PARAMETER );
|
2008-09-23 13:32:26 +02:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (uc->lpszPassword)
|
|
|
|
{
|
|
|
|
*len += 1; /* ":" */
|
2018-10-04 10:32:01 +02:00
|
|
|
*len += get_comp_length( uc->dwPasswordLength, 0, uc->lpszPassword );
|
2008-09-23 13:32:26 +02:00
|
|
|
}
|
|
|
|
if (uc->lpszHostName)
|
|
|
|
{
|
2018-10-04 10:32:01 +02:00
|
|
|
*len += get_comp_length( uc->dwHostNameLength, 0, uc->lpszHostName );
|
2008-09-23 13:32:26 +02:00
|
|
|
|
|
|
|
if (!uses_default_port( scheme, uc->nPort ))
|
|
|
|
{
|
|
|
|
WCHAR port[sizeof("65535")];
|
|
|
|
|
2019-11-22 16:32:24 +01:00
|
|
|
*len += swprintf( port, ARRAY_SIZE(port), L"%u", uc->nPort );
|
2008-09-23 13:32:26 +02:00
|
|
|
*len += 1; /* ":" */
|
|
|
|
}
|
|
|
|
if (uc->lpszUrlPath && *uc->lpszUrlPath != '/') *len += 1; /* '/' */
|
|
|
|
}
|
2018-10-04 10:32:01 +02:00
|
|
|
if (uc->lpszUrlPath) *len += get_comp_length( uc->dwUrlPathLength, flags, uc->lpszUrlPath );
|
|
|
|
if (uc->lpszExtraInfo) *len += get_comp_length( uc->dwExtraInfoLength, flags, uc->lpszExtraInfo );
|
2008-09-23 13:32:26 +02:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* WinHttpCreateUrl (winhttp.@)
|
|
|
|
*/
|
|
|
|
BOOL WINAPI WinHttpCreateUrl( LPURL_COMPONENTS uc, DWORD flags, LPWSTR url, LPDWORD required )
|
|
|
|
{
|
2018-11-06 16:09:04 +01:00
|
|
|
DWORD len, len_escaped;
|
2008-09-23 13:32:26 +02:00
|
|
|
INTERNET_SCHEME scheme;
|
|
|
|
|
|
|
|
TRACE("%p, 0x%08x, %p, %p\n", uc, flags, url, required);
|
|
|
|
|
2017-01-27 10:28:43 +01:00
|
|
|
if (!uc || uc->dwStructSize != sizeof(URL_COMPONENTS) || !required)
|
2008-09-23 13:32:26 +02:00
|
|
|
{
|
2018-11-22 21:15:55 +01:00
|
|
|
SetLastError( ERROR_INVALID_PARAMETER );
|
2008-09-23 13:32:26 +02:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2018-10-04 10:32:01 +02:00
|
|
|
if (!get_url_length( uc, flags, &len )) return FALSE;
|
2008-09-23 13:32:26 +02:00
|
|
|
|
2009-09-03 16:30:47 +02:00
|
|
|
if (*required < len)
|
2008-09-23 13:32:26 +02:00
|
|
|
{
|
|
|
|
*required = len + 1;
|
2018-11-22 21:15:55 +01:00
|
|
|
SetLastError( ERROR_INSUFFICIENT_BUFFER );
|
2008-09-23 13:32:26 +02:00
|
|
|
return FALSE;
|
|
|
|
}
|
2017-01-27 10:28:43 +01:00
|
|
|
if (!url)
|
|
|
|
{
|
2018-11-22 21:15:55 +01:00
|
|
|
SetLastError( ERROR_INVALID_PARAMETER );
|
2017-01-27 10:28:43 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
2008-09-23 13:32:26 +02:00
|
|
|
|
|
|
|
url[0] = 0;
|
|
|
|
*required = len;
|
|
|
|
if (uc->lpszScheme)
|
|
|
|
{
|
2018-10-04 10:32:01 +02:00
|
|
|
len = get_comp_length( uc->dwSchemeLength, 0, uc->lpszScheme );
|
2008-09-23 13:32:26 +02:00
|
|
|
memcpy( url, uc->lpszScheme, len * sizeof(WCHAR) );
|
|
|
|
url += len;
|
|
|
|
|
|
|
|
scheme = get_scheme( uc->lpszScheme, len );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const WCHAR *schemeW;
|
|
|
|
scheme = uc->nScheme;
|
|
|
|
|
|
|
|
if (!scheme) scheme = INTERNET_SCHEME_HTTP;
|
|
|
|
|
|
|
|
schemeW = get_scheme_string( scheme );
|
2019-11-20 14:29:08 +01:00
|
|
|
len = lstrlenW( schemeW );
|
2008-09-23 13:32:26 +02:00
|
|
|
memcpy( url, schemeW, len * sizeof(WCHAR) );
|
|
|
|
url += len;
|
|
|
|
}
|
|
|
|
|
2018-10-04 10:32:01 +02:00
|
|
|
*url++ = ':';
|
|
|
|
*url++ = '/';
|
|
|
|
*url++ = '/';
|
2008-09-23 13:32:26 +02:00
|
|
|
|
|
|
|
if (uc->lpszUserName)
|
|
|
|
{
|
2018-10-04 10:32:01 +02:00
|
|
|
len = get_comp_length( uc->dwUserNameLength, 0, uc->lpszUserName );
|
2008-09-23 13:32:26 +02:00
|
|
|
memcpy( url, uc->lpszUserName, len * sizeof(WCHAR) );
|
|
|
|
url += len;
|
|
|
|
|
|
|
|
if (uc->lpszPassword)
|
|
|
|
{
|
2018-10-04 10:32:01 +02:00
|
|
|
*url++ = ':';
|
|
|
|
len = get_comp_length( uc->dwPasswordLength, 0, uc->lpszPassword );
|
2008-09-23 13:32:26 +02:00
|
|
|
memcpy( url, uc->lpszPassword, len * sizeof(WCHAR) );
|
|
|
|
url += len;
|
|
|
|
}
|
2018-10-04 10:32:01 +02:00
|
|
|
*url++ = '@';
|
2008-09-23 13:32:26 +02:00
|
|
|
}
|
|
|
|
if (uc->lpszHostName)
|
|
|
|
{
|
2018-10-04 10:32:01 +02:00
|
|
|
len = get_comp_length( uc->dwHostNameLength, 0, uc->lpszHostName );
|
2008-09-23 13:32:26 +02:00
|
|
|
memcpy( url, uc->lpszHostName, len * sizeof(WCHAR) );
|
|
|
|
url += len;
|
|
|
|
|
|
|
|
if (!uses_default_port( scheme, uc->nPort ))
|
|
|
|
{
|
2018-10-04 10:32:01 +02:00
|
|
|
*url++ = ':';
|
2019-11-22 16:32:24 +01:00
|
|
|
url += swprintf( url, sizeof("65535"), L"%u", uc->nPort );
|
2008-09-23 13:32:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* add slash between hostname and path if necessary */
|
|
|
|
if (uc->lpszUrlPath && *uc->lpszUrlPath != '/')
|
|
|
|
{
|
2018-10-04 10:32:01 +02:00
|
|
|
*url++ = '/';
|
2008-09-23 13:32:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (uc->lpszUrlPath)
|
|
|
|
{
|
2018-10-04 10:32:01 +02:00
|
|
|
len = get_comp_length( uc->dwUrlPathLength, 0, uc->lpszUrlPath );
|
2018-11-06 16:09:04 +01:00
|
|
|
if (flags & ICU_ESCAPE)
|
|
|
|
{
|
|
|
|
if (!escape_string( uc->lpszUrlPath, len, url, &len_escaped ))
|
|
|
|
{
|
2018-11-22 21:15:55 +01:00
|
|
|
SetLastError( ERROR_INVALID_PARAMETER );
|
2018-11-06 16:09:04 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
url += len_escaped;
|
|
|
|
}
|
2008-09-23 13:32:26 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
memcpy( url, uc->lpszUrlPath, len * sizeof(WCHAR) );
|
|
|
|
url += len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (uc->lpszExtraInfo)
|
|
|
|
{
|
2018-10-04 10:32:01 +02:00
|
|
|
len = get_comp_length( uc->dwExtraInfoLength, 0, uc->lpszExtraInfo );
|
2018-11-06 16:09:04 +01:00
|
|
|
if (flags & ICU_ESCAPE)
|
|
|
|
{
|
|
|
|
if (!escape_string( uc->lpszExtraInfo, len, url, &len_escaped ))
|
|
|
|
{
|
2018-11-22 21:15:55 +01:00
|
|
|
SetLastError( ERROR_INVALID_PARAMETER );
|
2018-11-06 16:09:04 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
url += len_escaped;
|
|
|
|
}
|
2008-09-23 13:32:26 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
memcpy( url, uc->lpszExtraInfo, len * sizeof(WCHAR) );
|
|
|
|
url += len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*url = 0;
|
2018-11-22 21:15:55 +01:00
|
|
|
SetLastError( ERROR_SUCCESS );
|
2008-09-23 13:32:26 +02:00
|
|
|
return TRUE;
|
|
|
|
}
|