Skip to content

Commit 1b7d402

Browse files
xenucraigberry
authored andcommitted
Simplify read loop with single buffer allocation
1 parent 2f34bff commit 1b7d402

File tree

1 file changed

+16
-39
lines changed

1 file changed

+16
-39
lines changed

Win32.xs

Lines changed: 16 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1688,10 +1688,6 @@ XS(w32_HttpGetFile)
16881688
{
16891689
dXSARGS;
16901690
WCHAR *url = NULL, *file = NULL, *hostName = NULL, *urlPath = NULL;
1691-
DWORD dwSize = 0;
1692-
DWORD dwDownloaded = 0;
1693-
DWORD dwBytesWritten = 0;
1694-
LPSTR pszOutBuffer;
16951691
BOOL bResults = FALSE;
16961692
HINTERNET hSession = NULL,
16971693
hConnect = NULL,
@@ -1703,9 +1699,6 @@ XS(w32_HttpGetFile)
17031699
DWORD error = 0;
17041700
URL_COMPONENTS urlComp;
17051701
LPCWSTR acceptTypes[] = { L"*/*", NULL };
1706-
WINHTTP_AUTOPROXY_OPTIONS AutoProxyOptions;
1707-
WINHTTP_PROXY_INFO ProxyInfo;
1708-
DWORD cbProxyInfoSize = sizeof(ProxyInfo);
17091702

17101703
if (items != 2)
17111704
croak("usage: Win32::HttpGetFile($url, $file)");
@@ -1777,6 +1770,10 @@ XS(w32_HttpGetFile)
17771770
* configuration, which the request handle will inherit from the session).
17781771
*/
17791772
if (hRequest) {
1773+
WINHTTP_AUTOPROXY_OPTIONS AutoProxyOptions;
1774+
WINHTTP_PROXY_INFO ProxyInfo;
1775+
DWORD cbProxyInfoSize = sizeof(ProxyInfo);
1776+
17801777
ZeroMemory(&AutoProxyOptions, sizeof(AutoProxyOptions));
17811778
ZeroMemory(&ProxyInfo, sizeof(ProxyInfo));
17821779
AutoProxyOptions.dwFlags = WINHTTP_AUTOPROXY_AUTO_DETECT;
@@ -1825,42 +1822,29 @@ XS(w32_HttpGetFile)
18251822
FILE_ATTRIBUTE_NORMAL,
18261823
NULL);
18271824

1828-
if (!hOut || hOut == INVALID_HANDLE_VALUE)
1825+
if (hOut == INVALID_HANDLE_VALUE)
18291826
bFileError = TRUE;
18301827
}
18311828

1832-
/* Keep checking for data until there is nothing left. */
18331829
if (!bFileError && bResults) {
1834-
do {
1835-
/* Check for available data. */
1836-
dwSize = 0;
1837-
if (!WinHttpQueryDataAvailable(hRequest, &dwSize)) {
1838-
bAborted = TRUE;
1839-
break;
1840-
}
1841-
1842-
/* No more available data. */
1843-
if (!dwSize)
1844-
break;
1845-
1846-
/* Allocate space for the buffer. */
1847-
New(0, pszOutBuffer, dwSize + 1, char);
1848-
if (!pszOutBuffer) {
1849-
bAborted = TRUE;
1850-
break;
1851-
}
1830+
DWORD dwDownloaded = 0;
1831+
DWORD dwBytesWritten = 0;
1832+
DWORD dwSize = 65536;
1833+
char *pszOutBuffer;
18521834

1853-
/* Read the Data. */
1854-
ZeroMemory(pszOutBuffer, dwSize+1);
1835+
New(0, pszOutBuffer, dwSize, char);
18551836

1837+
/* Keep checking for data until there is nothing left. */
1838+
while (1) {
18561839
if (!WinHttpReadData(hRequest,
18571840
(LPVOID)pszOutBuffer,
18581841
dwSize,
18591842
&dwDownloaded)) {
18601843
bAborted = TRUE;
1861-
Safefree(pszOutBuffer);
18621844
break;
18631845
}
1846+
if (!dwDownloaded)
1847+
break;
18641848

18651849
/* Write what we just read to the output file */
18661850
if (!WriteFile(hOut,
@@ -1870,19 +1854,12 @@ XS(w32_HttpGetFile)
18701854
NULL)) {
18711855
bAborted = TRUE;
18721856
bFileError = TRUE;
1873-
Safefree(pszOutBuffer);
18741857
break;
18751858
}
18761859

1877-
Safefree(pszOutBuffer);
1878-
1879-
/* This condition would only be reached if WinHttpQueryDataAvailable
1880-
* said there are more data to read but WinHttpReadData didn't get any.
1881-
*/
1882-
if (!dwDownloaded)
1883-
break;
18841860
}
1885-
while (dwSize > 0);
1861+
1862+
Safefree(pszOutBuffer);
18861863
}
18871864
else {
18881865
bAborted = TRUE;

0 commit comments

Comments
 (0)