mirror of
https://github.com/ClassiCube/ClassiCube.git
synced 2025-09-14 10:05:44 -04:00
Use HttpRequest_Free instead of directly freeing data of http request
This commit is contained in:
parent
93e1491564
commit
af18fae4f7
17
src/Entity.c
17
src/Entity.c
@ -458,15 +458,18 @@ static void Entity_CheckSkin(struct Entity* e) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!Http_GetResult(e->_skinReqID, &item)) return;
|
if (!Http_GetResult(e->_skinReqID, &item)) return;
|
||||||
if (!item.success) { Entity_SetSkinAll(e, true); return; }
|
|
||||||
|
|
||||||
Stream_ReadonlyMemory(&mem, item.data, item.size);
|
if (!item.success) {
|
||||||
if ((res = ApplySkin(e, &bmp, &mem, &skin))) {
|
Entity_SetSkinAll(e, true);
|
||||||
LogInvalidSkin(res, &skin, item.data, item.size);
|
} else {
|
||||||
|
Stream_ReadonlyMemory(&mem, item.data, item.size);
|
||||||
|
|
||||||
|
if ((res = ApplySkin(e, &bmp, &mem, &skin))) {
|
||||||
|
LogInvalidSkin(res, &skin, item.data, item.size);
|
||||||
|
}
|
||||||
|
Mem_Free(bmp.scan0);
|
||||||
}
|
}
|
||||||
|
HttpRequest_Free(&item);
|
||||||
Mem_Free(bmp.scan0);
|
|
||||||
Mem_Free(item.data);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Returns true if no other entities are sharing this skin texture */
|
/* Returns true if no other entities are sharing this skin texture */
|
||||||
|
@ -45,6 +45,9 @@ struct HttpRequest {
|
|||||||
struct StringsBuffer* cookies; /* Cookie list sent in requests. May be modified by the response. */
|
struct StringsBuffer* cookies; /* Cookie list sent in requests. May be modified by the response. */
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/* Frees all dynamically allocated data from a HTTP request */
|
||||||
|
void HttpRequest_Free(struct HttpRequest* request);
|
||||||
|
|
||||||
/* Aschronously performs a http GET request to download a skin. */
|
/* Aschronously performs a http GET request to download a skin. */
|
||||||
/* If url is a skin, downloads from there. (if not, downloads from SKIN_SERVER/[skinName].png) */
|
/* If url is a skin, downloads from there. (if not, downloads from SKIN_SERVER/[skinName].png) */
|
||||||
int Http_AsyncGetSkin(const cc_string* skinName, cc_uint8 flags);
|
int Http_AsyncGetSkin(const cc_string* skinName, cc_uint8 flags);
|
||||||
|
14
src/LWeb.c
14
src/LWeb.c
@ -215,20 +215,20 @@ static void LWebTask_Reset(struct LWebTask* task) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void LWebTask_Tick(struct LWebTask* task, LWebTask_ErrorCallback errorCallback) {
|
void LWebTask_Tick(struct LWebTask* task, LWebTask_ErrorCallback errorCallback) {
|
||||||
struct HttpRequest req;
|
struct HttpRequest item;
|
||||||
if (task->completed) return;
|
if (task->completed) return;
|
||||||
if (!Http_GetResult(task->reqID, &req)) return;
|
if (!Http_GetResult(task->reqID, &item)) return;
|
||||||
|
|
||||||
task->working = false;
|
task->working = false;
|
||||||
task->completed = true;
|
task->completed = true;
|
||||||
task->success = req.success;
|
task->success = item.success;
|
||||||
|
|
||||||
if (req.success) {
|
if (item.success) {
|
||||||
task->Handle((cc_uint8*)req.data, req.size);
|
task->Handle(item.data, item.size);
|
||||||
Mem_Free(req.data);
|
|
||||||
} else if (errorCallback) {
|
} else if (errorCallback) {
|
||||||
errorCallback(&req);
|
errorCallback(&item);
|
||||||
}
|
}
|
||||||
|
HttpRequest_Free(&item);
|
||||||
}
|
}
|
||||||
|
|
||||||
void LWebTasks_Init(void) {
|
void LWebTasks_Init(void) {
|
||||||
|
@ -3584,7 +3584,9 @@ static void TexPackOverlay_Update(void* screen, double delta) {
|
|||||||
s->dirty = true;
|
s->dirty = true;
|
||||||
s->gotContent = true;
|
s->gotContent = true;
|
||||||
s->contentLength = item.contentLength;
|
s->contentLength = item.contentLength;
|
||||||
|
|
||||||
TexPackOverlay_UpdateLine3(s);
|
TexPackOverlay_UpdateLine3(s);
|
||||||
|
HttpRequest_Free(&item);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void TexPackOverlay_ContextLost(void* screen) {
|
static void TexPackOverlay_ContextLost(void* screen) {
|
||||||
|
@ -272,10 +272,9 @@ static void WoM_Reset(void) {
|
|||||||
static void WoM_Tick(void) {
|
static void WoM_Tick(void) {
|
||||||
struct HttpRequest item;
|
struct HttpRequest item;
|
||||||
if (!Http_GetResult(wom_identifier, &item)) return;
|
if (!Http_GetResult(wom_identifier, &item)) return;
|
||||||
if (!item.success) return;
|
|
||||||
|
|
||||||
WoM_ParseConfig(&item);
|
if (item.success) WoM_ParseConfig(&item);
|
||||||
Mem_Free(item.data);
|
HttpRequest_Free(&item);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -837,54 +837,55 @@ static void Fetcher_Finish(void) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
CC_NOINLINE static cc_bool Fetcher_Get(int reqID, struct HttpRequest* req) {
|
CC_NOINLINE static cc_bool Fetcher_Get(int reqID, struct HttpRequest* item) {
|
||||||
if (!Http_GetResult(reqID, req)) return false;
|
if (!Http_GetResult(reqID, item)) return false;
|
||||||
|
|
||||||
if (req->success) {
|
if (item->success) {
|
||||||
Fetcher_Downloaded++;
|
Fetcher_Downloaded++;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Only show error for first failed download */
|
/* Only show error for first failed download */
|
||||||
if (!Fetcher_Failed) Fetcher_ErrorCallback(req);
|
if (!Fetcher_Failed) Fetcher_ErrorCallback(item);
|
||||||
Fetcher_Failed = true;
|
Fetcher_Failed = true;
|
||||||
|
HttpRequest_Free(item);
|
||||||
|
|
||||||
Fetcher_Finish();
|
Fetcher_Finish();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void Fetcher_CheckFile(struct ZipfileSource* file) {
|
static void Fetcher_CheckFile(struct ZipfileSource* file) {
|
||||||
struct HttpRequest req;
|
struct HttpRequest item;
|
||||||
cc_result res;
|
cc_result res;
|
||||||
if (!Fetcher_Get(file->reqID, &req)) return;
|
if (!Fetcher_Get(file->reqID, &item)) return;
|
||||||
|
|
||||||
file->downloaded = true;
|
file->downloaded = true;
|
||||||
res = file->Process(&req);
|
res = file->Process(&item);
|
||||||
|
|
||||||
if (res) {
|
if (res) {
|
||||||
cc_string name = String_FromReadonly(file->name);
|
cc_string name = String_FromReadonly(file->name);
|
||||||
Logger_SysWarn2(res, "making", &name);
|
Logger_SysWarn2(res, "making", &name);
|
||||||
}
|
}
|
||||||
Mem_Free(req.data);
|
HttpRequest_Free(&item);
|
||||||
|
|
||||||
if (file->last) DefaultZip_Create();
|
if (file->last) DefaultZip_Create();
|
||||||
}
|
}
|
||||||
|
|
||||||
static void Fetcher_CheckMusic(struct MusicResource* music) {
|
static void Fetcher_CheckMusic(struct MusicResource* music) {
|
||||||
struct HttpRequest req;
|
struct HttpRequest item;
|
||||||
if (!Fetcher_Get(music->reqID, &req)) return;
|
if (!Fetcher_Get(music->reqID, &item)) return;
|
||||||
|
|
||||||
music->downloaded = true;
|
music->downloaded = true;
|
||||||
MusicResource_Save(music->name, &req);
|
MusicResource_Save(music->name, &item);
|
||||||
Mem_Free(req.data);
|
HttpRequest_Free(&item);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void Fetcher_CheckSound(const struct SoundResource* sound) {
|
static void Fetcher_CheckSound(const struct SoundResource* sound) {
|
||||||
struct HttpRequest req;
|
struct HttpRequest item;
|
||||||
if (!Fetcher_Get(sound->reqID, &req)) return;
|
if (!Fetcher_Get(sound->reqID, &item)) return;
|
||||||
|
|
||||||
SoundPatcher_Save(sound->name, &req);
|
SoundPatcher_Save(sound->name, &item);
|
||||||
Mem_Free(req.data);
|
HttpRequest_Free(&item);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* TODO: Implement this.. */
|
/* TODO: Implement this.. */
|
||||||
|
@ -406,7 +406,6 @@ void TexturePack_CheckPending(void) {
|
|||||||
|
|
||||||
if (item.success) {
|
if (item.success) {
|
||||||
ApplyDownloaded(&item);
|
ApplyDownloaded(&item);
|
||||||
Mem_Free(item.data);
|
|
||||||
} else if (item.result) {
|
} else if (item.result) {
|
||||||
Logger_Warn(item.result, "trying to download texture pack", Http_DescribeError);
|
Logger_Warn(item.result, "trying to download texture pack", Http_DescribeError);
|
||||||
} else if (item.statusCode == 200 || item.statusCode == 304) {
|
} else if (item.statusCode == 200 || item.statusCode == 304) {
|
||||||
@ -420,6 +419,7 @@ void TexturePack_CheckPending(void) {
|
|||||||
} else {
|
} else {
|
||||||
Chat_Add1("&c%i error when trying to download texture pack", &item.statusCode);
|
Chat_Add1("&c%i error when trying to download texture pack", &item.statusCode);
|
||||||
}
|
}
|
||||||
|
HttpRequest_Free(&item);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Asynchronously downloads the given texture pack */
|
/* Asynchronously downloads the given texture pack */
|
||||||
|
@ -12,8 +12,7 @@ static cc_bool httpsOnly, httpOnly, httpsVerify;
|
|||||||
static char skinServer_buffer[128];
|
static char skinServer_buffer[128];
|
||||||
static cc_string skinServer = String_FromArray(skinServer_buffer);
|
static cc_string skinServer = String_FromArray(skinServer_buffer);
|
||||||
|
|
||||||
/* Frees data from a HTTP request. */
|
void HttpRequest_Free(struct HttpRequest* request) {
|
||||||
static void HttpRequest_Free(struct HttpRequest* request) {
|
|
||||||
Mem_Free(request->data);
|
Mem_Free(request->data);
|
||||||
request->data = NULL;
|
request->data = NULL;
|
||||||
request->size = 0;
|
request->size = 0;
|
||||||
@ -82,7 +81,7 @@ static void RequestList_TryFree(struct RequestList* list, int id) {
|
|||||||
int i = RequestList_Find(list, id);
|
int i = RequestList_Find(list, id);
|
||||||
if (i < 0) return;
|
if (i < 0) return;
|
||||||
|
|
||||||
Mem_Free(list->entries[i].data);
|
HttpRequest_Free(&list->entries[i]);
|
||||||
RequestList_RemoveAt(list, i);
|
RequestList_RemoveAt(list, i);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -197,7 +196,7 @@ static void Http_CleanCacheTask(struct ScheduledTask* task) {
|
|||||||
item = &processedReqs.entries[i];
|
item = &processedReqs.entries[i];
|
||||||
if (item->timeDownloaded + (10 * 1000) >= now) continue;
|
if (item->timeDownloaded + (10 * 1000) >= now) continue;
|
||||||
|
|
||||||
Mem_Free(item->data);
|
HttpRequest_Free(item);
|
||||||
RequestList_RemoveAt(&processedReqs, i);
|
RequestList_RemoveAt(&processedReqs, i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user