diff --git a/src/Errors.h b/src/Errors.h index 20915280f..8b35bfd6e 100644 --- a/src/Errors.h +++ b/src/Errors.h @@ -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 diff --git a/src/Http_Web.c b/src/Http_Web.c index ec78856b7..c652306ef 100644 --- a/src/Http_Web.c +++ b/src/Http_Web.c @@ -3,7 +3,7 @@ #include "_HttpBase.h" #include #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) { diff --git a/src/Logger.c b/src/Logger.c index 21caaabd6..06c4aa96b 100644 --- a/src/Logger.c +++ b/src/Logger.c @@ -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; } diff --git a/src/interop_web.js b/src/interop_web.js index f06116975..877e6f133 100644 --- a/src/interop_web.js +++ b/src/interop_web.js @@ -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