Webclient: Fix crash when invalid URL is given to download something from (e.g. skin or texture pack)

This commit is contained in:
UnknownShadow200 2021-11-26 07:32:20 +11:00
parent e7e06cc650
commit fdfd521676
4 changed files with 31 additions and 7 deletions

View File

@ -109,6 +109,7 @@ enum CC_ERRORS {
INF_ERR_NUM_CODES = 0xCCDED056UL, /* Too many codewords specified for bit length */
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

View File

@ -3,7 +3,7 @@
#include "_HttpBase.h"
#include <emscripten/emscripten.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);
static struct RequestList workingReqs, queuedReqs;
@ -54,6 +54,7 @@ static void Http_StartNextDownload(void) {
char urlBuffer[URL_MAX_SIZE]; cc_string url;
char urlStr[NATIVE_STR_LEN];
struct HttpRequest* req;
cc_result res;
/* Avoid making too many requests at once */
if (workingReqs.count >= HTTP_MAX_CONCURRENCY) return;
@ -65,9 +66,21 @@ static void Http_StartNextDownload(void) {
Platform_Log1("Fetching %s", &url);
Platform_EncodeUtf8(urlStr, &url);
interop_DownloadAsync(urlStr, req->requestType, req->id);
RequestList_Append(&workingReqs, req, false);
RequestList_RemoveAt(&queuedReqs, 0);
res = interop_DownloadAsync(urlStr, req->requestType, req->id);
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) {

View File

@ -81,7 +81,8 @@ static const char* GetCCErrorDesc(cc_result res) {
case CW_ERR_STRING_LEN: return "NBT string too long";
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;
}

View File

@ -50,7 +50,15 @@ mergeInto(LibraryManager.library, {
var onProgress = Module["_Http_OnUpdateProgress"];
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';
var getContentLength = function(e) {
@ -74,6 +82,7 @@ mergeInto(LibraryManager.library, {
xhr.onprogress = function(e) { onProgress(reqID, e.loaded, e.total); };
try { xhr.send(); } catch (e) { onFinished(reqID, 0, 0, 0); }
return 0;
},
interop_IsHttpsOnly : function() {
// If this webpage is https://, browsers deny any http:// downloading