mirror of
https://github.com/ClassiCube/ClassiCube.git
synced 2025-09-12 09:06:55 -04:00
Webclient: Fix crash when invalid URL is given to download something from (e.g. skin or texture pack)
This commit is contained in:
parent
e7e06cc650
commit
fdfd521676
@ -109,6 +109,7 @@ enum CC_ERRORS {
|
|||||||
INF_ERR_NUM_CODES = 0xCCDED056UL, /* Too many codewords specified for bit length */
|
INF_ERR_NUM_CODES = 0xCCDED056UL, /* Too many codewords specified for bit length */
|
||||||
|
|
||||||
ERR_DOWNLOAD_INVALID = 0xCCDED057UL, /* Unspecified error occurred downloading data */
|
ERR_DOWNLOAD_INVALID = 0xCCDED057UL, /* Unspecified error occurred downloading data */
|
||||||
ERR_NO_AUDIO_OUTPUT = 0xCCDED058UL /* No audio output devices are connected */
|
ERR_NO_AUDIO_OUTPUT = 0xCCDED058UL, /* No audio output devices are connected */
|
||||||
|
ERR_INVALID_URL = 0xCCDED059UL /* Invalid URL provided to download from */
|
||||||
};
|
};
|
||||||
#endif
|
#endif
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
#include "_HttpBase.h"
|
#include "_HttpBase.h"
|
||||||
#include <emscripten/emscripten.h>
|
#include <emscripten/emscripten.h>
|
||||||
#include "Errors.h"
|
#include "Errors.h"
|
||||||
extern void interop_DownloadAsync(const char* url, int method, int reqID);
|
extern int interop_DownloadAsync(const char* url, int method, int reqID);
|
||||||
extern int interop_IsHttpsOnly(void);
|
extern int interop_IsHttpsOnly(void);
|
||||||
static struct RequestList workingReqs, queuedReqs;
|
static struct RequestList workingReqs, queuedReqs;
|
||||||
|
|
||||||
@ -54,6 +54,7 @@ static void Http_StartNextDownload(void) {
|
|||||||
char urlBuffer[URL_MAX_SIZE]; cc_string url;
|
char urlBuffer[URL_MAX_SIZE]; cc_string url;
|
||||||
char urlStr[NATIVE_STR_LEN];
|
char urlStr[NATIVE_STR_LEN];
|
||||||
struct HttpRequest* req;
|
struct HttpRequest* req;
|
||||||
|
cc_result res;
|
||||||
|
|
||||||
/* Avoid making too many requests at once */
|
/* Avoid making too many requests at once */
|
||||||
if (workingReqs.count >= HTTP_MAX_CONCURRENCY) return;
|
if (workingReqs.count >= HTTP_MAX_CONCURRENCY) return;
|
||||||
@ -65,9 +66,21 @@ static void Http_StartNextDownload(void) {
|
|||||||
Platform_Log1("Fetching %s", &url);
|
Platform_Log1("Fetching %s", &url);
|
||||||
|
|
||||||
Platform_EncodeUtf8(urlStr, &url);
|
Platform_EncodeUtf8(urlStr, &url);
|
||||||
interop_DownloadAsync(urlStr, req->requestType, req->id);
|
res = interop_DownloadAsync(urlStr, req->requestType, req->id);
|
||||||
RequestList_Append(&workingReqs, req, false);
|
|
||||||
RequestList_RemoveAt(&queuedReqs, 0);
|
if (res) {
|
||||||
|
/* Download error code -> ClassiCube error code */
|
||||||
|
if (res == 1) res = ERR_INVALID_URL;
|
||||||
|
req->result = res;
|
||||||
|
|
||||||
|
/* Invalid URL so move onto next request */
|
||||||
|
Http_FinishRequest(req);
|
||||||
|
RequestList_RemoveAt(&queuedReqs, 0);
|
||||||
|
Http_StartNextDownload();
|
||||||
|
} else {
|
||||||
|
RequestList_Append(&workingReqs, req, false);
|
||||||
|
RequestList_RemoveAt(&queuedReqs, 0);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
EMSCRIPTEN_KEEPALIVE void Http_OnUpdateProgress(int reqID, int read, int total) {
|
EMSCRIPTEN_KEEPALIVE void Http_OnUpdateProgress(int reqID, int read, int total) {
|
||||||
|
@ -81,7 +81,8 @@ static const char* GetCCErrorDesc(cc_result res) {
|
|||||||
case CW_ERR_STRING_LEN: return "NBT string too long";
|
case CW_ERR_STRING_LEN: return "NBT string too long";
|
||||||
|
|
||||||
case ERR_DOWNLOAD_INVALID: return "Website denied download or doesn't exist";
|
case ERR_DOWNLOAD_INVALID: return "Website denied download or doesn't exist";
|
||||||
case ERR_NO_AUDIO_OUTPUT: return "No audio output devices plugged in";
|
case ERR_NO_AUDIO_OUTPUT: return "No audio output devices plugged in";
|
||||||
|
case ERR_INVALID_URL: return "Cannot download from invalid URL";
|
||||||
}
|
}
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
@ -50,7 +50,15 @@ mergeInto(LibraryManager.library, {
|
|||||||
var onProgress = Module["_Http_OnUpdateProgress"];
|
var onProgress = Module["_Http_OnUpdateProgress"];
|
||||||
|
|
||||||
var xhr = new XMLHttpRequest();
|
var xhr = new XMLHttpRequest();
|
||||||
xhr.open(reqMethod, url);
|
try {
|
||||||
|
xhr.open(reqMethod, url);
|
||||||
|
} catch (e) {
|
||||||
|
// DOMException gets thrown when invalid URL provided. Test cases:
|
||||||
|
// http://%7https://www.example.com/test.zip
|
||||||
|
// http://example:app/test.zip
|
||||||
|
console.log(e);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
xhr.responseType = 'arraybuffer';
|
xhr.responseType = 'arraybuffer';
|
||||||
|
|
||||||
var getContentLength = function(e) {
|
var getContentLength = function(e) {
|
||||||
@ -74,6 +82,7 @@ mergeInto(LibraryManager.library, {
|
|||||||
xhr.onprogress = function(e) { onProgress(reqID, e.loaded, e.total); };
|
xhr.onprogress = function(e) { onProgress(reqID, e.loaded, e.total); };
|
||||||
|
|
||||||
try { xhr.send(); } catch (e) { onFinished(reqID, 0, 0, 0); }
|
try { xhr.send(); } catch (e) { onFinished(reqID, 0, 0, 0); }
|
||||||
|
return 0;
|
||||||
},
|
},
|
||||||
interop_IsHttpsOnly : function() {
|
interop_IsHttpsOnly : function() {
|
||||||
// If this webpage is https://, browsers deny any http:// downloading
|
// If this webpage is https://, browsers deny any http:// downloading
|
||||||
|
Loading…
x
Reference in New Issue
Block a user