| 48 | } |
| 49 | |
| 50 | bool send(HttpResponse& response) { |
| 51 | struct { |
| 52 | const char* url; |
| 53 | const char* body; |
| 54 | uintptr_t bodySize; |
| 55 | const char* headers; |
| 56 | char* response; |
| 57 | uintptr_t responseSize; |
| 58 | uintptr_t statusCode; |
| 59 | } args { |
| 60 | m_url.c_str(), // url |
| 61 | m_body.c_str(), // body |
| 62 | m_body.size(), // bodySize |
| 63 | m_headers.c_str(), // headers |
| 64 | nullptr, // response |
| 65 | 0, // responseSize |
| 66 | ~uintptr_t{}, // statusCode |
| 67 | }; |
| 68 | |
| 69 | MAIN_THREAD_EM_ASM({ |
| 70 | const index = 'url,body,bodySize,headers,response,responseSize,statusCode,callbackArg,callback'.split(','); |
| 71 | const HEAP32 = GROWABLE_HEAP_U32(); |
| 72 | const HEAP8 = GROWABLE_HEAP_U8(); |
| 73 | |
| 74 | function get(name) { |
| 75 | if ($1 == 4) |
| 76 | return HEAP32[($0 >> 2) + index.indexOf(name)]; |
| 77 | else |
| 78 | return HEAP64[($0 >> 3) + index.indexOf(name)]; |
| 79 | } |
| 80 | function set(name, value) { |
| 81 | if ($1 == 4) |
| 82 | HEAP32[($0 >> 2) + index.indexOf(name)] = value; |
| 83 | else |
| 84 | HEAP64[($0 >> 3) + index.indexOf(name)] = value; |
| 85 | } |
| 86 | |
| 87 | const url = UTF8ToString(get('url')); |
| 88 | |
| 89 | const isPOST = $2; |
| 90 | const body = isPOST ? UTF8ToString(get('body')) : undefined; |
| 91 | |
| 92 | const headers = UTF8ToString(get('headers')); |
| 93 | const parsedHeaders = {}; |
| 94 | headers.split('\n').forEach(line => { |
| 95 | const arr = line.split(':'); |
| 96 | if (arr.length == 2) |
| 97 | parsedHeaders[arr[0].trim()] = arr[1].trim(); |
| 98 | }); |
| 99 | |
| 100 | fetch(url, { |
| 101 | method: isPOST ? 'POST' : 'GET', |
| 102 | mode: 'cors', |
| 103 | headers: parsedHeaders, |
| 104 | body |
| 105 | }) |
| 106 | .then(response => response.arrayBuffer()) |
| 107 | .then(buffer => { |