Switch to using WinHTTP for parsing URLs

It seems better to rely on the well-tested WinHTTP URL parsing functions
rather than on our own wxURI. It should also allow to support any new
URI schemas if support for them is ever added to WinHTTP.
This commit is contained in:
Vadim Zeitlin
2021-01-09 21:23:31 +01:00
parent 8ace65bbec
commit aa7c6c3aa8

View File

@@ -15,7 +15,6 @@
#if wxUSE_WEBREQUEST_WINHTTP #if wxUSE_WEBREQUEST_WINHTTP
#include "wx/mstream.h" #include "wx/mstream.h"
#include "wx/uri.h"
#include "wx/msw/private.h" #include "wx/msw/private.h"
#include "wx/msw/private/webrequest_winhttp.h" #include "wx/msw/private/webrequest_winhttp.h"
@@ -238,19 +237,28 @@ void wxWebRequestWinHTTP::SetFailed(DWORD errorCode)
void wxWebRequestWinHTTP::Start() void wxWebRequestWinHTTP::Start()
{ {
// Parse the URL // Parse the URL
wxURI uri(m_url); URL_COMPONENTS urlComps;
const bool isSecure = uri.GetScheme().CmpNoCase("HTTPS") == 0; wxZeroMemory(urlComps);
urlComps.dwStructSize = sizeof(urlComps);
urlComps.dwSchemeLength =
urlComps.dwHostNameLength =
urlComps.dwUrlPathLength =
urlComps.dwExtraInfoLength = (DWORD)-1;
int port; if ( !::WinHttpCrackUrl(m_url.wc_str(), m_url.length(), 0, &urlComps) )
if ( !uri.HasPort() ) {
port = isSecure ? INTERNET_DEFAULT_HTTPS_PORT : INTERNET_DEFAULT_HTTP_PORT; SetFailedWithLastError();
else return;
port = wxAtoi(uri.GetPort()); }
// Open a connection // Open a connection
m_connect = ::WinHttpConnect( m_connect = ::WinHttpConnect
m_sessionWinHTTP.GetHandle(), (
uri.GetServer().wc_str(), port, 0); m_sessionWinHTTP.GetHandle(),
wxString(urlComps.lpszHostName, urlComps.dwHostNameLength),
urlComps.nPort,
0 // reserved
);
if ( m_connect == NULL ) if ( m_connect == NULL )
{ {
SetFailedWithLastError(); SetFailedWithLastError();
@@ -265,16 +273,16 @@ void wxWebRequestWinHTTP::Start()
else else
method = "GET"; method = "GET";
wxString objectName = uri.GetPath(); wxString objectName(urlComps.lpszUrlPath, urlComps.dwUrlPathLength);
if ( uri.HasQuery() ) if ( urlComps.dwExtraInfoLength )
objectName += "?" + uri.GetQuery(); objectName += "?" + wxString(urlComps.lpszExtraInfo, urlComps.dwExtraInfoLength);
// Open a request // Open a request
m_request = ::WinHttpOpenRequest(m_connect, m_request = ::WinHttpOpenRequest(m_connect,
method.wc_str(), objectName.wc_str(), method.wc_str(), objectName.wc_str(),
NULL, WINHTTP_NO_REFERER, NULL, WINHTTP_NO_REFERER,
WINHTTP_DEFAULT_ACCEPT_TYPES, WINHTTP_DEFAULT_ACCEPT_TYPES,
(isSecure) ? WINHTTP_FLAG_SECURE : 0); urlComps.nScheme == INTERNET_SCHEME_HTTPS ? WINHTTP_FLAG_SECURE : 0);
if ( m_request == NULL ) if ( m_request == NULL )
{ {
SetFailedWithLastError(); SetFailedWithLastError();