Permalink
Browse files

Windows: Handle ERROR_INTERNET_FORCE_RETRY when opening a HTTP connec…

…tion
  • Loading branch information...
ed95 committed Jan 10, 2019
1 parent ebd6a7b commit 0fbb75330b88c66f6ee563645633f05ad53d4350
Showing with 44 additions and 29 deletions.
  1. +44 −29 modules/juce_core/native/juce_win32_Network.cpp
@@ -343,6 +343,35 @@ class WebInputStream::Pimpl
InternetSetOption (sessionHandle, option, &timeOutMs, sizeof (timeOutMs));
}

void sendHTTPRequest (INTERNET_BUFFERS& buffers, WebInputStream::Listener* listener)
{
if (! HttpSendRequestEx (request, &buffers, 0, HSR_INITIATE, 0))
return;

int totalBytesSent = 0;

while (totalBytesSent < postData.getSize())
{
auto bytesToSend = jmin (1024, (int) postData.getSize() - totalBytesSent);
DWORD bytesSent = 0;

if (bytesToSend == 0
|| ! InternetWriteFile (request, static_cast<const char*> (postData.getData()) + totalBytesSent,
(DWORD) bytesToSend, &bytesSent))
{
return;
}

totalBytesSent += bytesSent;

if (listener != nullptr
&& ! listener->postDataSendProgress (owner, totalBytesSent, (int) postData.getSize()))
{
return;
}
}
}

void openHTTPConnection (URL_COMPONENTS& uc, const String& address, WebInputStream::Listener* listener)
{
const TCHAR* mimeTypes[] = { _T("*/*"), nullptr };
@@ -365,43 +394,29 @@ class WebInputStream::Pimpl
if (request != 0)
{
INTERNET_BUFFERS buffers = { 0 };
buffers.dwStructSize = sizeof (INTERNET_BUFFERS);
buffers.lpcszHeader = headers.toWideCharPointer();
buffers.dwStructSize = sizeof (INTERNET_BUFFERS);
buffers.lpcszHeader = headers.toWideCharPointer();
buffers.dwHeadersLength = (DWORD) headers.length();
buffers.dwBufferTotal = (DWORD) postData.getSize();
buffers.dwBufferTotal = (DWORD) postData.getSize();

if (HttpSendRequestEx (request, &buffers, 0, HSR_INITIATE, 0))
auto sendRequestAndTryEnd = [this, &buffers, &listener]() -> bool
{
int bytesSent = 0;
sendHTTPRequest (buffers, listener);

for (;;)
{
const int bytesToDo = jmin (1024, (int) postData.getSize() - bytesSent);
DWORD bytesDone = 0;
if (HttpEndRequest (request, 0, 0, 0))
return true;

if (bytesToDo > 0
&& ! InternetWriteFile (request,
static_cast<const char*> (postData.getData()) + bytesSent,
(DWORD) bytesToDo, &bytesDone))
{
break;
}

if (bytesToDo == 0 || (int) bytesDone < bytesToDo)
{
if (HttpEndRequest (request, 0, 0, 0))
return;
return false;
};

break;
}
auto closed = sendRequestAndTryEnd();

bytesSent += bytesDone;
// N.B. this is needed for some authenticated HTTP connections
if (! closed && GetLastError() == ERROR_INTERNET_FORCE_RETRY)
closed = sendRequestAndTryEnd();

if (listener != nullptr
&& ! listener->postDataSendProgress (owner, bytesSent, (int) postData.getSize()))
break;
}
}
if (closed)
return;
}

closeConnection();

0 comments on commit 0fbb753

Please sign in to comment.