From 54639c5dc5c336662547726ce89322498e85e8be Mon Sep 17 00:00:00 2001 From: Jacek Caban Date: Fri, 2 Nov 2012 13:49:22 +0100 Subject: [PATCH] wininet: Commit cache file as soon as all data is written. --- dlls/wininet/http.c | 51 ++++++++++++++++++++++++++------------- dlls/wininet/tests/http.c | 2 +- 2 files changed, 35 insertions(+), 18 deletions(-) diff --git a/dlls/wininet/http.c b/dlls/wininet/http.c index 73dd99190fa..8937a3915b7 100644 --- a/dlls/wininet/http.c +++ b/dlls/wininet/http.c @@ -1815,18 +1815,8 @@ static void HTTPREQ_Destroy(object_header_t *hdr) TRACE("\n"); if(request->hCacheFile) { - WCHAR url[INTERNET_MAX_URL_LENGTH]; - CloseHandle(request->hCacheFile); - - if(HTTP_GetRequestURL(request, url)) { - DWORD headersLen; - - headersLen = request->rawHeaders ? strlenW(request->rawHeaders) : 0; - CommitUrlCacheEntryW(url, request->cacheFile, request->expires, - request->last_modified, NORMAL_CACHE_ENTRY, - request->rawHeaders, headersLen, NULL, 0); - } + DeleteFileW(request->cacheFile); } heap_free(request->cacheFile); @@ -2233,6 +2223,25 @@ static DWORD HTTPREQ_SetOption(object_header_t *hdr, DWORD option, void *buffer, return INET_SetOption(hdr, option, buffer, size); } +static void commit_cache_entry(http_request_t *req) +{ + WCHAR url[INTERNET_MAX_URL_LENGTH]; + + TRACE("%p\n", req); + + CloseHandle(req->hCacheFile); + req->hCacheFile = NULL; + + if(HTTP_GetRequestURL(req, url)) { + DWORD headersLen; + + headersLen = req->rawHeaders ? strlenW(req->rawHeaders) : 0; + CommitUrlCacheEntryW(url, req->cacheFile, req->expires, + req->last_modified, NORMAL_CACHE_ENTRY, + req->rawHeaders, headersLen, NULL, 0); + } +} + static void create_cache_entry(http_request_t *req) { WCHAR url[INTERNET_MAX_URL_LENGTH]; @@ -2271,6 +2280,9 @@ static void create_cache_entry(http_request_t *req) b = WriteFile(req->hCacheFile, req->read_buf+req->read_pos, req->read_size, &written, NULL); if(!b) FIXME("WriteFile failed: %u\n", GetLastError()); + + if(req->data_stream->vtbl->end_of_data(req->data_stream, req)) + commit_cache_entry(req); } } @@ -2362,13 +2374,18 @@ static DWORD read_http_stream(http_request_t *req, BYTE *buf, DWORD size, DWORD res = req->data_stream->vtbl->read(req->data_stream, req, buf, size, read, read_mode); assert(*read <= size); - if(*read && req->hCacheFile) { - BOOL bres; - DWORD written; + if(req->hCacheFile) { + if(*read) { + BOOL bres; + DWORD written; - bres = WriteFile(req->hCacheFile, buf, *read, &written, NULL); - if(!bres) - FIXME("WriteFile failed: %u\n", GetLastError()); + bres = WriteFile(req->hCacheFile, buf, *read, &written, NULL); + if(!bres) + FIXME("WriteFile failed: %u\n", GetLastError()); + } + + if(req->data_stream->vtbl->end_of_data(req->data_stream, req)) + commit_cache_entry(req); } return res; diff --git a/dlls/wininet/tests/http.c b/dlls/wininet/tests/http.c index 38d1845b0d8..c9bd0a49ec0 100644 --- a/dlls/wininet/tests/http.c +++ b/dlls/wininet/tests/http.c @@ -1255,7 +1255,7 @@ static void test_http_cache(void) ret = InternetQueryOptionA(request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size); ok(ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) failed: %u\n", GetLastError()); - file = CreateFile(file_name, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, + file = CreateFile(file_name, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); ok(file != INVALID_HANDLE_VALUE, "Could not create file: %u\n", GetLastError()); file_size = GetFileSize(file, NULL);