Skip to content

Commit 73c577b

Browse files
Don't make a full GET request from try_init_cache
1 parent 3f04bbf commit 73c577b

1 file changed

Lines changed: 30 additions & 17 deletions

File tree

requests.c

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -223,22 +223,25 @@ init_request_file(Request *req)
223223
}
224224

225225
bool
226-
try_init_from_cache(Request *req)
226+
try_init_from_cache(Request *req, char *hostname, char *filePath, INTERNET_SCHEME scheme, INTERNET_PORT port)
227227
{
228228
DWORD lenlen;
229229
DWORD err;
230230
HANDLE hFile;
231231
INTERNET_CACHE_ENTRY_INFOA *cacheData;
232-
HINTERNET hUrl;
232+
HINTERNET hConn;
233+
HINTERNET hReq;
233234
SYSTEMTIME modifiedTimeSys;
234235
FILETIME modifiedTimeFile;
235236
DWORD contentLength;
237+
DWORD flags;
236238
bool success;
237239
bool deleteCache;
238240

239241
success = false;
240242
deleteCache = false;
241-
hUrl = NULL;
243+
hConn = NULL;
244+
hReq = NULL;
242245
lenlen = sizeof(INTERNET_CACHE_ENTRY_INFOA) + MAX_URL_LENGTH + MAX_PATH;
243246
cacheData = malloc(lenlen);
244247

@@ -257,14 +260,21 @@ try_init_from_cache(Request *req)
257260
goto cleanup;
258261
}
259262

260-
hUrl = InternetOpenUrlA(hInternet, req->url, NULL, 0,
261-
INTERNET_FLAG_RELOAD | INTERNET_FLAG_NO_CACHE_WRITE, (DWORD_PTR)NULL);
262-
263-
/* If we have internet, check the validity of the cached file. */
264-
if (hUrl != NULL) {
265-
/* If the remote file is newer than what we have cached, bail. */
263+
/* Send a HEAD request to validate the cached entry without downloading. */
264+
if (hInternet != NULL) {
265+
hConn = InternetConnectA(hInternet, hostname, port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
266+
}
267+
if (hConn != NULL) {
268+
flags = INTERNET_FLAG_RELOAD | INTERNET_FLAG_NO_CACHE_WRITE;
269+
if (scheme == INTERNET_SCHEME_HTTPS) {
270+
flags |= INTERNET_FLAG_SECURE;
271+
}
272+
hReq = HttpOpenRequestA(hConn, "HEAD", filePath, NULL, NULL, NULL, flags, 0);
273+
}
274+
if (hReq != NULL && HttpSendRequestA(hReq, NULL, 0, NULL, 0)) {
275+
/* If the remote file is newer than what we have cached, invalidate. */
266276
lenlen = sizeof(modifiedTimeSys);
267-
if (HttpQueryInfoA(hUrl, HTTP_QUERY_FLAG_SYSTEMTIME | HTTP_QUERY_LAST_MODIFIED, &modifiedTimeSys, &lenlen, 0)
277+
if (HttpQueryInfoA(hReq, HTTP_QUERY_FLAG_SYSTEMTIME | HTTP_QUERY_LAST_MODIFIED, &modifiedTimeSys, &lenlen, 0)
268278
&& SystemTimeToFileTime(&modifiedTimeSys, &modifiedTimeFile)
269279
&& CompareFileTime(&modifiedTimeFile, &cacheData->LastModifiedTime) == 1) {
270280
goto cleanup;
@@ -277,13 +287,13 @@ try_init_from_cache(Request *req)
277287
}
278288

279289
/* Compare actual file size on disk against Content-Length to catch truncation. */
280-
if (hUrl != NULL) {
290+
if (hReq != NULL) {
281291
DWORD fileSize = GetFileSize(hFile, NULL);
282292
lenlen = sizeof(contentLength);
283293
if (fileSize != INVALID_FILE_SIZE
284-
&& HttpQueryInfoA(hUrl, HTTP_QUERY_FLAG_NUMBER | HTTP_QUERY_CONTENT_LENGTH, &contentLength, &lenlen, 0)
294+
&& HttpQueryInfoA(hReq, HTTP_QUERY_FLAG_NUMBER | HTTP_QUERY_CONTENT_LENGTH, &contentLength, &lenlen, 0)
285295
&& fileSize != contentLength) {
286-
logmsg("Cache file size mismatch for %s: on disk %d, expected %d\n",
296+
logmsg("Cache file truncated for %s: on disk %d, expected %d\n",
287297
req->url, fileSize, contentLength);
288298
CloseHandle(hFile);
289299
deleteCache = true;
@@ -300,16 +310,19 @@ try_init_from_cache(Request *req)
300310
logmsg(" Cache file: %s\n", cacheData->lpszLocalFileName);
301311

302312
cleanup:
313+
if (hReq != NULL) {
314+
InternetCloseHandle(hReq);
315+
}
316+
if (hConn != NULL) {
317+
InternetCloseHandle(hConn);
318+
}
303319
if (!success) {
304320
UnlockUrlCacheEntryFileA(req->url, 0);
305321
if (deleteCache) {
306322
DeleteUrlCacheEntryA(req->url);
307323
logmsg("Deleted cache entry for %s\n", req->url);
308324
}
309325
}
310-
if (hUrl != NULL) {
311-
InternetCloseHandle(hUrl);
312-
}
313326
free(cacheData);
314327
return success;
315328
}
@@ -337,7 +350,7 @@ init_request_http(Request *req, char *hostname, char *filePath, INTERNET_SCHEME
337350
* transparently reading from the cache, so we attempt to
338351
* explicitly read from it before falling back to HTTP.
339352
*/
340-
if (try_init_from_cache(req)) {
353+
if (try_init_from_cache(req, hostname, filePath, scheme, port)) {
341354
return;
342355
}
343356
}

0 commit comments

Comments
 (0)