etag and last-modified work now

This commit is contained in:
UnknownShadow200 2018-05-13 22:43:53 +10:00
parent 6db245f9d8
commit ad6ab7fb2a
6 changed files with 44 additions and 17 deletions

View File

@ -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 {

View File

@ -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)];

View File

@ -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

View File

@ -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");

View File

@ -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) {

View File

@ -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;
}