voiddownloadSucceeded(emscripten_fetch_t *fetch){ printf("Finished downloading %llu bytes from URL %s.\n", fetch->numBytes, fetch->url); // The data is now available at fetch->data[0] through fetch->data[fetch->numBytes-1]; emscripten_fetch_close(fetch); // Free data associated with the fetch. }
voiddownloadFailed(emscripten_fetch_t *fetch){ printf("Downloading %s failed, HTTP failure status code: %d.\n", fetch->url, fetch->status); emscripten_fetch_close(fetch); // Also free data on failure. }
EMSCRIPTEN_RESULT emscripten_fetch_close(emscripten_fetch_t* fetch){ if (!fetch) { return EMSCRIPTEN_RESULT_SUCCESS; // Closing null pointer is ok, same as with free(). }
// This function frees the fetch pointer so that it is invalid to access it anymore. // Use a few key fields as an integrity check that we are being passed a good pointer to a valid // fetch structure, which has not been yet closed. (double close is an error) if (fetch->id == 0 || fetch->readyState > STATE_MAX) { return EMSCRIPTEN_RESULT_INVALID_PARAM; }
// This fetch is aborted. Call the error handler if the fetch was still in progress and was // canceled in flight. if (fetch->readyState != STATE_DONE && fetch->__attributes.onerror) { fetch->status = (unsignedshort)-1; strcpy(fetch->statusText, "aborted with emscripten_fetch_close()"); fetch->__attributes.onerror(fetch); }