Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
99 lines (89 loc) · 3.69 KB

File metadata and controls

99 lines (89 loc) · 3.69 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright 2016 The Emscripten Authors. All rights reserved.
// Emscripten is available under two separate licenses, the MIT license and the
// University of Illinois/NCSA Open Source License. Both these licenses can be
// found in the LICENSE file.
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <assert.h>
#include <emscripten/fetch.h>
int result = 0;
// This test is run in two modes: if FILE_DOES_NOT_EXIST defined,
// then testing an XHR of a missing file.
// #define FILE_DOES_NOT_EXIST
int main()
{
emscripten_fetch_attr_t attr;
emscripten_fetch_attr_init(&attr);
strcpy(attr.requestMethod, "GET");
attr.userData = (void*)0x12345678;
attr.attributes = EMSCRIPTEN_FETCH_REPLACE | EMSCRIPTEN_FETCH_LOAD_TO_MEMORY;
attr.onsuccess = [](emscripten_fetch_t *fetch) {
#if FILE_DOES_NOT_EXIST
assert(false && "onsuccess handler called, but the file shouldn't exist"); // Shouldn't reach here if the file doesn't exist
#endif
assert(fetch);
printf("Finished downloading %llu bytes\n", fetch->numBytes);
assert(fetch->url);
assert(!strcmp(fetch->url, "gears.png"));
assert(fetch->id != 0);
assert((uintptr_t)fetch->userData == 0x12345678);
assert(fetch->totalBytes == 6407);
assert(fetch->numBytes == fetch->totalBytes);
assert(fetch->data != 0);
// Compute rudimentary checksum of data
uint8_t checksum = 0;
for(int i = 0; i < fetch->numBytes; ++i)
checksum ^= fetch->data[i];
printf("Data checksum: %02X\n", checksum);
assert(checksum == 0x08);
emscripten_fetch_close(fetch);
#ifdef REPORT_RESULT
#ifndef FILE_DOES_NOT_EXIST
result = 1;
#endif
// Fetch API appears to sometimes call the handlers more than once, see https://github.com/emscripten-core/emscripten/pull/8191
MAYBE_REPORT_RESULT(result);
#endif
};
attr.onprogress = [](emscripten_fetch_t *fetch) {
assert(fetch);
if (fetch->status != 200) return;
printf("onprogress: dataOffset: %llu, numBytes: %llu, totalBytes: %llu\n", fetch->dataOffset, fetch->numBytes, fetch->totalBytes);
if (fetch->totalBytes > 0) {
printf("Downloading.. %.2f%% complete.\n", (fetch->dataOffset + fetch->numBytes) * 100.0 / fetch->totalBytes);
} else {
printf("Downloading.. %lld bytes complete.\n", fetch->dataOffset + fetch->numBytes);
}
#ifdef FILE_DOES_NOT_EXIST
assert(false && "onprogress handler called, but the file should not exist"); // We should not receive progress reports if the file doesn't exist.
#endif
// We must receive a call to the onprogress handler with 100% completion.
if (fetch->dataOffset + fetch->numBytes == fetch->totalBytes) ++result;
assert(fetch->dataOffset + fetch->numBytes <= fetch->totalBytes);
assert(fetch->url);
assert(!strcmp(fetch->url, "gears.png"));
assert(fetch->id != 0);
assert((uintptr_t)fetch->userData == 0x12345678);
};
attr.onerror = [](emscripten_fetch_t *fetch) {
printf("Download failed!\n");
#ifndef FILE_DOES_NOT_EXIST
assert(false && "onerror handler called, but the transfer should have succeeded!"); // The file exists, shouldn't reach here.
#endif
assert(fetch);
assert(fetch->id != 0);
assert(!strcmp(fetch->url, "gears.png"));
assert((uintptr_t)fetch->userData == 0x12345678);
#ifdef REPORT_RESULT
#ifdef FILE_DOES_NOT_EXIST
result = 1;
#endif
// Fetch API appears to sometimes call the handlers more than once, see https://github.com/emscripten-core/emscripten/pull/8191
MAYBE_REPORT_RESULT(result);
#endif
};
emscripten_fetch_t *fetch = emscripten_fetch(&attr, "gears.png");
assert(fetch != 0);
memset(&attr, 0, sizeof(attr)); // emscripten_fetch() must be able to operate without referencing to this structure after the call.
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.