Reformat WinHTTP code
No real changes, just try to avoid over long lines. Also use early returns in case of WinHTTP functions failures everywhere for consistency. This commit is best viewed ignoring whitespace-only changes.
This commit is contained in:
@@ -144,8 +144,10 @@ wxWebRequestWinHTTP::~wxWebRequestWinHTTP()
|
|||||||
::WinHttpCloseHandle(m_connect);
|
::WinHttpCloseHandle(m_connect);
|
||||||
}
|
}
|
||||||
|
|
||||||
void wxWebRequestWinHTTP::HandleCallback(DWORD dwInternetStatus,
|
void
|
||||||
LPVOID lpvStatusInformation, DWORD dwStatusInformationLength)
|
wxWebRequestWinHTTP::HandleCallback(DWORD dwInternetStatus,
|
||||||
|
LPVOID lpvStatusInformation,
|
||||||
|
DWORD dwStatusInformationLength)
|
||||||
{
|
{
|
||||||
switch ( dwInternetStatus )
|
switch ( dwInternetStatus )
|
||||||
{
|
{
|
||||||
@@ -155,22 +157,29 @@ void wxWebRequestWinHTTP::HandleCallback(DWORD dwInternetStatus,
|
|||||||
else
|
else
|
||||||
CreateResponse();
|
CreateResponse();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case WINHTTP_CALLBACK_STATUS_READ_COMPLETE:
|
case WINHTTP_CALLBACK_STATUS_READ_COMPLETE:
|
||||||
if ( dwStatusInformationLength > 0 )
|
if ( dwStatusInformationLength > 0 )
|
||||||
{
|
{
|
||||||
if ( !m_response->ReportAvailableData(dwStatusInformationLength) &&
|
if ( !m_response->ReportAvailableData(dwStatusInformationLength)
|
||||||
GetState() != wxWebRequest::State_Cancelled )
|
&& GetState() != wxWebRequest::State_Cancelled )
|
||||||
SetFailedWithLastError();
|
SetFailedWithLastError();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
{
|
||||||
SetState(wxWebRequest::State_Completed);
|
SetState(wxWebRequest::State_Completed);
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case WINHTTP_CALLBACK_STATUS_WRITE_COMPLETE:
|
case WINHTTP_CALLBACK_STATUS_WRITE_COMPLETE:
|
||||||
WriteData();
|
WriteData();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case WINHTTP_CALLBACK_STATUS_REQUEST_ERROR:
|
case WINHTTP_CALLBACK_STATUS_REQUEST_ERROR:
|
||||||
{
|
{
|
||||||
LPWINHTTP_ASYNC_RESULT asyncResult = reinterpret_cast<LPWINHTTP_ASYNC_RESULT>(lpvStatusInformation);
|
LPWINHTTP_ASYNC_RESULT
|
||||||
|
asyncResult = reinterpret_cast<LPWINHTTP_ASYNC_RESULT>(lpvStatusInformation);
|
||||||
|
|
||||||
// "Failing" with "cancelled" error is not actually an error if
|
// "Failing" with "cancelled" error is not actually an error if
|
||||||
// we're expecting it, i.e. if our Cancel() had been called.
|
// we're expecting it, i.e. if our Cancel() had been called.
|
||||||
if ( asyncResult->dwError == ERROR_WINHTTP_OPERATION_CANCELLED &&
|
if ( asyncResult->dwError == ERROR_WINHTTP_OPERATION_CANCELLED &&
|
||||||
@@ -188,49 +197,67 @@ void wxWebRequestWinHTTP::WriteData()
|
|||||||
int dataWriteSize = 8 * 1024;
|
int dataWriteSize = 8 * 1024;
|
||||||
if ( m_dataWritten + dataWriteSize > m_dataSize )
|
if ( m_dataWritten + dataWriteSize > m_dataSize )
|
||||||
dataWriteSize = m_dataSize - m_dataWritten;
|
dataWriteSize = m_dataSize - m_dataWritten;
|
||||||
if ( dataWriteSize )
|
if ( !dataWriteSize )
|
||||||
{
|
{
|
||||||
m_dataWriteBuffer.Clear();
|
|
||||||
m_dataWriteBuffer.GetWriteBuf(dataWriteSize);
|
|
||||||
m_dataStream->Read(m_dataWriteBuffer.GetData(), dataWriteSize);
|
|
||||||
|
|
||||||
if ( !::WinHttpWriteData(m_request, m_dataWriteBuffer.GetData(), dataWriteSize, NULL) )
|
|
||||||
SetFailedWithLastError();
|
|
||||||
m_dataWritten += dataWriteSize;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
CreateResponse();
|
CreateResponse();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
m_dataWriteBuffer.Clear();
|
||||||
|
m_dataWriteBuffer.GetWriteBuf(dataWriteSize);
|
||||||
|
m_dataStream->Read(m_dataWriteBuffer.GetData(), dataWriteSize);
|
||||||
|
|
||||||
|
if ( !::WinHttpWriteData
|
||||||
|
(
|
||||||
|
m_request,
|
||||||
|
m_dataWriteBuffer.GetData(),
|
||||||
|
dataWriteSize,
|
||||||
|
NULL // [out] bytes written, must be null in async mode
|
||||||
|
) )
|
||||||
|
{
|
||||||
|
SetFailedWithLastError();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
m_dataWritten += dataWriteSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
void wxWebRequestWinHTTP::CreateResponse()
|
void wxWebRequestWinHTTP::CreateResponse()
|
||||||
{
|
{
|
||||||
if ( ::WinHttpReceiveResponse(m_request, NULL) )
|
if ( !::WinHttpReceiveResponse(m_request, NULL) )
|
||||||
{
|
{
|
||||||
m_response.reset(new wxWebResponseWinHTTP(*this));
|
|
||||||
// wxWebResponseWinHTTP ctor could have changed the state if its
|
|
||||||
// initialization failed, so check for this.
|
|
||||||
if ( GetState() == wxWebRequest::State_Failed )
|
|
||||||
return;
|
|
||||||
|
|
||||||
int status = m_response->GetStatus();
|
|
||||||
if ( status == HTTP_STATUS_DENIED || status == HTTP_STATUS_PROXY_AUTH_REQ )
|
|
||||||
{
|
|
||||||
m_authChallenge.reset(new wxWebAuthChallengeWinHTTP(
|
|
||||||
status == HTTP_STATUS_PROXY_AUTH_REQ ? wxWebAuthChallenge::Source_Proxy : wxWebAuthChallenge::Source_Server, *this));
|
|
||||||
if ( m_authChallenge->Init() )
|
|
||||||
SetState(wxWebRequest::State_Unauthorized, m_response->GetStatusText());
|
|
||||||
else
|
|
||||||
SetFailedWithLastError();
|
|
||||||
}
|
|
||||||
else if ( CheckServerStatus() )
|
|
||||||
{
|
|
||||||
// Start reading the response
|
|
||||||
if ( !m_response->ReadData() )
|
|
||||||
SetFailedWithLastError();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
SetFailedWithLastError();
|
SetFailedWithLastError();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
m_response.reset(new wxWebResponseWinHTTP(*this));
|
||||||
|
// wxWebResponseWinHTTP ctor could have changed the state if its
|
||||||
|
// initialization failed, so check for this.
|
||||||
|
if ( GetState() == wxWebRequest::State_Failed )
|
||||||
|
return;
|
||||||
|
|
||||||
|
int status = m_response->GetStatus();
|
||||||
|
if ( status == HTTP_STATUS_DENIED || status == HTTP_STATUS_PROXY_AUTH_REQ )
|
||||||
|
{
|
||||||
|
m_authChallenge.reset(new wxWebAuthChallengeWinHTTP
|
||||||
|
(
|
||||||
|
status == HTTP_STATUS_PROXY_AUTH_REQ
|
||||||
|
? wxWebAuthChallenge::Source_Proxy
|
||||||
|
: wxWebAuthChallenge::Source_Server,
|
||||||
|
*this
|
||||||
|
));
|
||||||
|
|
||||||
|
if ( m_authChallenge->Init() )
|
||||||
|
SetState(wxWebRequest::State_Unauthorized, m_response->GetStatusText());
|
||||||
|
else
|
||||||
|
SetFailedWithLastError();
|
||||||
|
}
|
||||||
|
else if ( CheckServerStatus() )
|
||||||
|
{
|
||||||
|
// Start reading the response
|
||||||
|
if ( !m_response->ReadData() )
|
||||||
|
SetFailedWithLastError();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void wxWebRequestWinHTTP::SetFailed(DWORD errorCode)
|
void wxWebRequestWinHTTP::SetFailed(DWORD errorCode)
|
||||||
@@ -284,11 +311,17 @@ void wxWebRequestWinHTTP::Start()
|
|||||||
objectName += "?" + wxString(urlComps.lpszExtraInfo, urlComps.dwExtraInfoLength);
|
objectName += "?" + wxString(urlComps.lpszExtraInfo, urlComps.dwExtraInfoLength);
|
||||||
|
|
||||||
// Open a request
|
// Open a request
|
||||||
m_request = ::WinHttpOpenRequest(m_connect,
|
m_request = ::WinHttpOpenRequest
|
||||||
method.wc_str(), objectName.wc_str(),
|
(
|
||||||
NULL, WINHTTP_NO_REFERER,
|
m_connect,
|
||||||
WINHTTP_DEFAULT_ACCEPT_TYPES,
|
method.wc_str(), objectName.wc_str(),
|
||||||
urlComps.nScheme == INTERNET_SCHEME_HTTPS ? WINHTTP_FLAG_SECURE : 0);
|
NULL, // protocol version: use default, i.e. HTTP/1.1
|
||||||
|
WINHTTP_NO_REFERER,
|
||||||
|
WINHTTP_DEFAULT_ACCEPT_TYPES,
|
||||||
|
urlComps.nScheme == INTERNET_SCHEME_HTTPS
|
||||||
|
? WINHTTP_FLAG_SECURE
|
||||||
|
: 0
|
||||||
|
);
|
||||||
if ( m_request == NULL )
|
if ( m_request == NULL )
|
||||||
{
|
{
|
||||||
SetFailedWithLastError();
|
SetFailedWithLastError();
|
||||||
@@ -296,13 +329,16 @@ void wxWebRequestWinHTTP::Start()
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Register callback
|
// Register callback
|
||||||
if ( ::WinHttpSetStatusCallback(m_request,
|
if ( ::WinHttpSetStatusCallback
|
||||||
wxRequestStatusCallback,
|
(
|
||||||
WINHTTP_CALLBACK_FLAG_READ_COMPLETE |
|
m_request,
|
||||||
WINHTTP_CALLBACK_FLAG_WRITE_COMPLETE |
|
wxRequestStatusCallback,
|
||||||
WINHTTP_CALLBACK_FLAG_SENDREQUEST_COMPLETE |
|
WINHTTP_CALLBACK_FLAG_READ_COMPLETE |
|
||||||
WINHTTP_CALLBACK_FLAG_REQUEST_ERROR,
|
WINHTTP_CALLBACK_FLAG_WRITE_COMPLETE |
|
||||||
wxRESERVED_PARAM) == WINHTTP_INVALID_STATUS_CALLBACK )
|
WINHTTP_CALLBACK_FLAG_SENDREQUEST_COMPLETE |
|
||||||
|
WINHTTP_CALLBACK_FLAG_REQUEST_ERROR,
|
||||||
|
wxRESERVED_PARAM
|
||||||
|
) == WINHTTP_INVALID_STATUS_CALLBACK )
|
||||||
{
|
{
|
||||||
SetFailedWithLastError();
|
SetFailedWithLastError();
|
||||||
return;
|
return;
|
||||||
@@ -315,22 +351,31 @@ void wxWebRequestWinHTTP::SendRequest()
|
|||||||
{
|
{
|
||||||
// Combine all headers to a string
|
// Combine all headers to a string
|
||||||
wxString allHeaders;
|
wxString allHeaders;
|
||||||
for ( wxWebRequestHeaderMap::const_iterator header = m_headers.begin(); header != m_headers.end(); ++header )
|
for ( wxWebRequestHeaderMap::const_iterator header = m_headers.begin();
|
||||||
|
header != m_headers.end();
|
||||||
|
++header )
|
||||||
|
{
|
||||||
allHeaders.append(wxString::Format("%s: %s\n", header->first, header->second));
|
allHeaders.append(wxString::Format("%s: %s\n", header->first, header->second));
|
||||||
|
}
|
||||||
|
|
||||||
if ( m_dataSize )
|
if ( m_dataSize )
|
||||||
m_dataWritten = 0;
|
m_dataWritten = 0;
|
||||||
|
|
||||||
// Send request
|
// Send request
|
||||||
if ( ::WinHttpSendRequest(m_request,
|
if ( !::WinHttpSendRequest
|
||||||
allHeaders.wc_str(), allHeaders.length(),
|
(
|
||||||
NULL, 0, m_dataSize,
|
m_request,
|
||||||
(DWORD_PTR)this) )
|
allHeaders.wc_str(), allHeaders.length(),
|
||||||
|
NULL, 0, // No extra optional data right now
|
||||||
|
m_dataSize,
|
||||||
|
(DWORD_PTR)this
|
||||||
|
) )
|
||||||
{
|
{
|
||||||
SetState(wxWebRequest::State_Active);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
SetFailedWithLastError();
|
SetFailedWithLastError();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
SetState(wxWebRequest::State_Active);
|
||||||
}
|
}
|
||||||
|
|
||||||
void wxWebRequestWinHTTP::Cancel()
|
void wxWebRequestWinHTTP::Cancel()
|
||||||
@@ -351,10 +396,10 @@ wxWebResponseWinHTTP::wxWebResponseWinHTTP(wxWebRequestWinHTTP& request):
|
|||||||
wxWebResponseImpl(request),
|
wxWebResponseImpl(request),
|
||||||
m_requestHandle(request.GetHandle())
|
m_requestHandle(request.GetHandle())
|
||||||
{
|
{
|
||||||
wxString contentLengthStr = wxWinHTTPQueryHeaderString(m_requestHandle,
|
const wxString contentLengthStr =
|
||||||
WINHTTP_QUERY_CONTENT_LENGTH);
|
wxWinHTTPQueryHeaderString(m_requestHandle, WINHTTP_QUERY_CONTENT_LENGTH);
|
||||||
if ( contentLengthStr.empty() ||
|
if ( contentLengthStr.empty() ||
|
||||||
!contentLengthStr.ToLongLong(&m_contentLength) )
|
!contentLengthStr.ToLongLong(&m_contentLength) )
|
||||||
m_contentLength = -1;
|
m_contentLength = -1;
|
||||||
|
|
||||||
Init();
|
Init();
|
||||||
@@ -367,17 +412,23 @@ wxString wxWebResponseWinHTTP::GetURL() const
|
|||||||
|
|
||||||
wxString wxWebResponseWinHTTP::GetHeader(const wxString& name) const
|
wxString wxWebResponseWinHTTP::GetHeader(const wxString& name) const
|
||||||
{
|
{
|
||||||
return wxWinHTTPQueryHeaderString(m_requestHandle,
|
return wxWinHTTPQueryHeaderString(m_requestHandle, WINHTTP_QUERY_CUSTOM,
|
||||||
WINHTTP_QUERY_CUSTOM, name.wc_str());
|
name.wc_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
int wxWebResponseWinHTTP::GetStatus() const
|
int wxWebResponseWinHTTP::GetStatus() const
|
||||||
{
|
{
|
||||||
DWORD status = 0;
|
DWORD status = 0;
|
||||||
DWORD statusSize = sizeof(status);
|
DWORD statusSize = sizeof(status);
|
||||||
if ( !::WinHttpQueryHeaders(m_requestHandle,
|
if ( !::WinHttpQueryHeaders
|
||||||
WINHTTP_QUERY_STATUS_CODE | WINHTTP_QUERY_FLAG_NUMBER,
|
(
|
||||||
WINHTTP_HEADER_NAME_BY_INDEX, &status, &statusSize, 0) )
|
m_requestHandle,
|
||||||
|
WINHTTP_QUERY_STATUS_CODE | WINHTTP_QUERY_FLAG_NUMBER,
|
||||||
|
WINHTTP_HEADER_NAME_BY_INDEX,
|
||||||
|
&status,
|
||||||
|
&statusSize,
|
||||||
|
0 // header index, unused with status code "header"
|
||||||
|
) )
|
||||||
{
|
{
|
||||||
wxLogLastError("WinHttpQueryHeaders/status");
|
wxLogLastError("WinHttpQueryHeaders/status");
|
||||||
}
|
}
|
||||||
@@ -392,11 +443,13 @@ wxString wxWebResponseWinHTTP::GetStatusText() const
|
|||||||
|
|
||||||
bool wxWebResponseWinHTTP::ReadData()
|
bool wxWebResponseWinHTTP::ReadData()
|
||||||
{
|
{
|
||||||
if ( ::WinHttpReadData(m_requestHandle,
|
return ::WinHttpReadData
|
||||||
GetDataBuffer(m_readSize), m_readSize, NULL) )
|
(
|
||||||
return true;
|
m_requestHandle,
|
||||||
else
|
GetDataBuffer(m_readSize),
|
||||||
return false;
|
m_readSize,
|
||||||
|
NULL // [out] bytes read, must be null in async mode
|
||||||
|
) == TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool wxWebResponseWinHTTP::ReportAvailableData(DWORD dataLen)
|
bool wxWebResponseWinHTTP::ReportAvailableData(DWORD dataLen)
|
||||||
@@ -423,37 +476,53 @@ bool wxWebAuthChallengeWinHTTP::Init()
|
|||||||
DWORD supportedSchemes;
|
DWORD supportedSchemes;
|
||||||
DWORD firstScheme;
|
DWORD firstScheme;
|
||||||
|
|
||||||
if ( ::WinHttpQueryAuthSchemes(m_request.GetHandle(),
|
if ( !::WinHttpQueryAuthSchemes
|
||||||
&supportedSchemes, &firstScheme, &m_target) )
|
(
|
||||||
|
m_request.GetHandle(),
|
||||||
|
&supportedSchemes,
|
||||||
|
&firstScheme,
|
||||||
|
&m_target
|
||||||
|
) )
|
||||||
{
|
{
|
||||||
if ( supportedSchemes & WINHTTP_AUTH_SCHEME_NEGOTIATE )
|
wxLogLastError("WinHttpQueryAuthSchemes");
|
||||||
m_selectedScheme = WINHTTP_AUTH_SCHEME_NEGOTIATE;
|
return false;
|
||||||
else if ( supportedSchemes & WINHTTP_AUTH_SCHEME_NTLM )
|
|
||||||
m_selectedScheme = WINHTTP_AUTH_SCHEME_NTLM;
|
|
||||||
else if ( supportedSchemes & WINHTTP_AUTH_SCHEME_PASSPORT )
|
|
||||||
m_selectedScheme = WINHTTP_AUTH_SCHEME_PASSPORT;
|
|
||||||
else if ( supportedSchemes & WINHTTP_AUTH_SCHEME_DIGEST )
|
|
||||||
m_selectedScheme = WINHTTP_AUTH_SCHEME_DIGEST;
|
|
||||||
else if ( supportedSchemes & WINHTTP_AUTH_SCHEME_BASIC )
|
|
||||||
m_selectedScheme = WINHTTP_AUTH_SCHEME_BASIC;
|
|
||||||
else
|
|
||||||
m_selectedScheme = 0;
|
|
||||||
|
|
||||||
if ( m_selectedScheme )
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
if ( supportedSchemes & WINHTTP_AUTH_SCHEME_NEGOTIATE )
|
||||||
|
m_selectedScheme = WINHTTP_AUTH_SCHEME_NEGOTIATE;
|
||||||
|
else if ( supportedSchemes & WINHTTP_AUTH_SCHEME_NTLM )
|
||||||
|
m_selectedScheme = WINHTTP_AUTH_SCHEME_NTLM;
|
||||||
|
else if ( supportedSchemes & WINHTTP_AUTH_SCHEME_PASSPORT )
|
||||||
|
m_selectedScheme = WINHTTP_AUTH_SCHEME_PASSPORT;
|
||||||
|
else if ( supportedSchemes & WINHTTP_AUTH_SCHEME_DIGEST )
|
||||||
|
m_selectedScheme = WINHTTP_AUTH_SCHEME_DIGEST;
|
||||||
|
else if ( supportedSchemes & WINHTTP_AUTH_SCHEME_BASIC )
|
||||||
|
m_selectedScheme = WINHTTP_AUTH_SCHEME_BASIC;
|
||||||
|
else
|
||||||
|
m_selectedScheme = 0;
|
||||||
|
|
||||||
|
return m_selectedScheme != 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void wxWebAuthChallengeWinHTTP::SetCredentials(const wxString& user,
|
void
|
||||||
const wxString& password)
|
wxWebAuthChallengeWinHTTP::SetCredentials(const wxString& user,
|
||||||
|
const wxString& password)
|
||||||
{
|
{
|
||||||
if ( ::WinHttpSetCredentials(m_request.GetHandle(), m_target, m_selectedScheme,
|
if ( !::WinHttpSetCredentials
|
||||||
user.wc_str(), password.wc_str(), NULL) )
|
(
|
||||||
m_request.SendRequest();
|
m_request.GetHandle(),
|
||||||
else
|
m_target,
|
||||||
|
m_selectedScheme,
|
||||||
|
user.wc_str(),
|
||||||
|
password.wc_str(),
|
||||||
|
wxRESERVED_PARAM
|
||||||
|
) )
|
||||||
|
{
|
||||||
m_request.SetFailedWithLastError();
|
m_request.SetFailedWithLastError();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
m_request.SendRequest();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -480,9 +549,14 @@ bool wxWebSessionWinHTTP::Open()
|
|||||||
else
|
else
|
||||||
accessType = WINHTTP_ACCESS_TYPE_DEFAULT_PROXY;
|
accessType = WINHTTP_ACCESS_TYPE_DEFAULT_PROXY;
|
||||||
|
|
||||||
m_handle = ::WinHttpOpen(GetHeaders().find("User-Agent")->second.wc_str(), accessType,
|
m_handle = ::WinHttpOpen
|
||||||
WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS,
|
(
|
||||||
WINHTTP_FLAG_ASYNC);
|
GetHeaders().find("User-Agent")->second.wc_str(),
|
||||||
|
accessType,
|
||||||
|
WINHTTP_NO_PROXY_NAME,
|
||||||
|
WINHTTP_NO_PROXY_BYPASS,
|
||||||
|
WINHTTP_FLAG_ASYNC
|
||||||
|
);
|
||||||
if ( !m_handle )
|
if ( !m_handle )
|
||||||
{
|
{
|
||||||
wxLogLastError("WinHttpOpen");
|
wxLogLastError("WinHttpOpen");
|
||||||
|
Reference in New Issue
Block a user