From ad6ab7fb2a11bed196ceb81e0076a69172528a7a Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Sun, 13 May 2018 22:43:53 +1000 Subject: [PATCH] etag and last-modified work now --- src/Client/AsyncDownloader.c | 17 ++++++++--------- src/Client/AsyncDownloader.h | 4 ++++ src/Client/Platform.h | 2 +- src/Client/Screens.c | 4 ++-- src/Client/TexturePack.c | 2 +- src/Client/WinPlatform.c | 32 ++++++++++++++++++++++++++++---- 6 files changed, 44 insertions(+), 17 deletions(-) diff --git a/src/Client/AsyncDownloader.c b/src/Client/AsyncDownloader.c index 9af4f7060..5dc4e42ec 100644 --- a/src/Client/AsyncDownloader.c +++ b/src/Client/AsyncDownloader.c @@ -79,7 +79,7 @@ AsyncRequestList async_pending; AsyncRequestList async_processed; String async_skinServer = String_FromConst("http://static.classicube.net/skins/"); AsyncRequest async_curRequest; -volatile Int32 async_curRequestProgress = -3; +volatile Int32 async_curProgress = ASYNC_PROGRESS_NOTHING; /* TODO: Implement these */ bool ManageCookies; bool KeepAlive; @@ -202,7 +202,7 @@ bool AsyncDownloader_GetCurrent(AsyncRequest* request, Int32* progress) { Platform_MutexLock(async_curRequestMutex); { *request = async_curRequest; - *progress = async_curRequestProgress; + *progress = async_curProgress; } Platform_MutexUnlock(async_curRequestMutex); return request->ID[0] != NULL; @@ -221,6 +221,7 @@ void AsyncDownloader_ProcessRequest(AsyncRequest* request) { Platform_Log2("HTTP make request: ret code %i, in %i ms", &result, &elapsedMS); if (!ErrorHandler_Check(result)) return; + async_curProgress = ASYNC_PROGRESS_FETCHING_DATA; UInt32 size = 0; Stopwatch_Start(&stopwatch); result = Platform_HttpGetRequestHeaders(request, handle, &size); @@ -228,15 +229,15 @@ void AsyncDownloader_ProcessRequest(AsyncRequest* request) { UInt32 status = request->StatusCode; Platform_Log3("HTTP get headers: ret code %i (http %i), in %i ms", &result, &status, &elapsedMS); - if (!ErrorHandler_Check(result) || request->StatusCode != 200) { + if (!ErrorHandler_Check(result) || request->StatusCode != 200 || request->RequestType == REQUEST_TYPE_CONTENT_LENGTH) { Platform_HttpFreeRequest(handle); return; } void* data = NULL; Stopwatch_Start(&stopwatch); - result = Platform_HttpGetRequestData(request, handle, &data, size); + result = Platform_HttpGetRequestData(request, handle, &data, size, &async_curProgress); elapsedMS = Stopwatch_ElapsedMicroseconds(&stopwatch) / 1000; - Platform_Log2("HTTP get data: ret code %i, in %i ms", &result, &elapsedMS); + Platform_Log3("HTTP get data: ret code %i (size %i), in %i ms", &result, &size, &elapsedMS); Platform_HttpFreeRequest(handle); if (!ErrorHandler_Check(result)) return; @@ -265,8 +266,6 @@ void AsyncDownloader_ProcessRequest(AsyncRequest* request) { break; } - /* TODO: Http response codes */ - /* TODO: Progress of download */ Platform_CurrentUTCTime(&request->TimeDownloaded); Platform_MutexLock(async_processedMutex); { @@ -311,7 +310,7 @@ void AsyncDownloader_WorkerFunc(void) { Platform_MutexLock(async_curRequestMutex); { async_curRequest = request; - async_curRequestProgress = -2; + async_curProgress = ASYNC_PROGRESS_MAKING_REQUEST; } Platform_MutexUnlock(async_curRequestMutex); @@ -321,7 +320,7 @@ void AsyncDownloader_WorkerFunc(void) { Platform_MutexLock(async_curRequestMutex); { async_curRequest.ID[0] = NULL; - async_curRequestProgress = -3; + async_curProgress = ASYNC_PROGRESS_NOTHING; } Platform_MutexUnlock(async_curRequestMutex); } else { diff --git a/src/Client/AsyncDownloader.h b/src/Client/AsyncDownloader.h index 2cda79370..6246dd949 100644 --- a/src/Client/AsyncDownloader.h +++ b/src/Client/AsyncDownloader.h @@ -12,6 +12,10 @@ enum REQUEST_TYPE { REQUEST_TYPE_DATA, REQUEST_TYPE_IMAGE, REQUEST_TYPE_STRING, REQUEST_TYPE_CONTENT_LENGTH, }; +#define ASYNC_PROGRESS_NOTHING -3 +#define ASYNC_PROGRESS_MAKING_REQUEST -2 +#define ASYNC_PROGRESS_FETCHING_DATA -1 + typedef struct AsyncRequest_ { UInt8 URL[String_BufferSize(STRING_SIZE)]; UInt8 ID[String_BufferSize(STRING_SIZE)]; diff --git a/src/Client/Platform.h b/src/Client/Platform.h index 1fd144bfa..5a9fb8da4 100644 --- a/src/Client/Platform.h +++ b/src/Client/Platform.h @@ -92,7 +92,7 @@ ReturnCode Platform_SocketSelectRead(void* socket, Int32 microseconds, bool* suc void Platform_HttpInit(void); ReturnCode Platform_HttpMakeRequest(AsyncRequest* request, void** handle); ReturnCode Platform_HttpGetRequestHeaders(AsyncRequest* request, void* handle, UInt32* size); -ReturnCode Platform_HttpGetRequestData(AsyncRequest* request, void* handle, void** data, UInt32 size); +ReturnCode Platform_HttpGetRequestData(AsyncRequest* request, void* handle, void** data, UInt32 size, volatile Int32* progress); ReturnCode Platform_HttpFreeRequest(void* handle); ReturnCode Platform_HttpFree(void); #endif \ No newline at end of file diff --git a/src/Client/Screens.c b/src/Client/Screens.c index f5a36cad3..9c2a80b2c 100644 --- a/src/Client/Screens.c +++ b/src/Client/Screens.c @@ -788,9 +788,9 @@ void ChatScreen_CheckOtherStatuses(ChatScreen* screen) { UInt8 strBuffer[String_BufferSize(STRING_SIZE)]; String str = String_InitAndClearArray(strBuffer); - if (progress == -2) { + if (progress == ASYNC_PROGRESS_MAKING_REQUEST) { String_AppendConst(&str, "&eRetrieving texture pack.."); - } else if (progress == -1) { + } else if (progress == ASYNC_PROGRESS_FETCHING_DATA) { String_AppendConst(&str, "&eDownloading texture pack"); } else if (progress >= 0 && progress <= 100) { String_AppendConst(&str, "&eDownloading texture pack (&7"); diff --git a/src/Client/TexturePack.c b/src/Client/TexturePack.c index c146ccb6b..b352ea8f6 100644 --- a/src/Client/TexturePack.c +++ b/src/Client/TexturePack.c @@ -298,7 +298,7 @@ void TextureCache_MakePath(STRING_TRANSIENT String* path, STRING_PURE String* ur bool TextureCache_HasUrl(STRING_PURE String* url) { String path; TexCache_InitAndMakePath(url); - return Platform_FileExists(url); + return Platform_FileExists(&path); } bool TextureCache_GetStream(STRING_PURE String* url, Stream* stream) { diff --git a/src/Client/WinPlatform.c b/src/Client/WinPlatform.c index 9464e70c9..3c5921ae8 100644 --- a/src/Client/WinPlatform.c +++ b/src/Client/WinPlatform.c @@ -604,7 +604,27 @@ void Platform_HttpInit(void) { ReturnCode Platform_HttpMakeRequest(AsyncRequest* request, void** handle) { String url = String_FromRawArray(request->URL); - *handle = InternetOpenUrlA(hInternet, url.buffer, NULL, 0, + UInt8 headersBuffer[String_BufferSize(STRING_SIZE * 2)]; + String headers = String_MakeNull(); + + if (request->Etag[0] != NULL || request->LastModified.Year > 0) { + headers = String_InitAndClearArray(headersBuffer); + if (request->LastModified.Year > 0) { + String_AppendConst(&headers, "If-Modified-Since: "); + DateTime_HttpDate(&request->LastModified, &headers); + String_AppendConst(&headers, "\r\n"); + } + + if (request->Etag[0] != NULL) { + String etag = String_FromRawArray(request->Etag); + String_AppendConst(&headers, "If-None-Match: "); + String_AppendString(&headers, &etag); + String_AppendConst(&headers, "\r\n"); + } + String_AppendConst(&headers, "\r\n\r\n"); + } + + *handle = InternetOpenUrlA(hInternet, url.buffer, headers.buffer, headers.length, INTERNET_FLAG_NO_CACHE_WRITE | INTERNET_FLAG_NO_UI | INTERNET_FLAG_RELOAD, NULL); return *handle == NULL ? GetLastError() : 0; } @@ -635,21 +655,25 @@ ReturnCode Platform_HttpGetRequestHeaders(AsyncRequest* request, void* handle, U return 0; } -ReturnCode Platform_HttpGetRequestData(AsyncRequest* request, void* handle, void** data, UInt32 size) { +ReturnCode Platform_HttpGetRequestData(AsyncRequest* request, void* handle, void** data, UInt32 size, volatile Int32* progress) { if (size == 0) return 1; *data = Platform_MemAlloc(size, 1); if (*data == NULL) ErrorHandler_Fail("Failed to allocate memory for http get data"); + *progress = 0; UInt8* buffer = *data; - UInt32 left = size, read; + UInt32 left = size, read, totalRead = 0; while (left > 0) { bool success = InternetReadFile(handle, buffer, left, &read); if (!success) { Platform_MemFree(data); return GetLastError(); } if (read == 0) break; - buffer += read; left -= read; + buffer += read; totalRead += read; left -= read; + *progress = (Int32)(100.0f * totalRead / size); } + + *progress = 100; return 0; }