(scheme, url, body, headers)
| 7 | var ply_net_http_requests = {}; |
| 8 | |
| 9 | function ply_net_http_make_request(scheme, url, body, headers) { |
| 10 | var cid = ply_net_uid; |
| 11 | ply_net_uid += 1; |
| 12 | |
| 13 | var method; |
| 14 | if (scheme === 0) { method = 'GET'; } |
| 15 | else if (scheme === 1) { method = 'POST'; } |
| 16 | else if (scheme === 2) { method = 'PUT'; } |
| 17 | else if (scheme === 3) { method = 'DELETE'; } |
| 18 | |
| 19 | var url_string = consume_js_object(url); |
| 20 | var body_string = consume_js_object(body); |
| 21 | var headers_obj = consume_js_object(headers); |
| 22 | |
| 23 | var xhr = new XMLHttpRequest(); |
| 24 | xhr.open(method, url_string, true); |
| 25 | xhr.responseType = 'arraybuffer'; |
| 26 | |
| 27 | for (var header in headers_obj) { |
| 28 | if (headers_obj.hasOwnProperty(header)) { |
| 29 | xhr.setRequestHeader(header, headers_obj[header]); |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | xhr.onload = function () { |
| 34 | // Forward status + body regardless of status code |
| 35 | ply_net_http_requests[cid] = { |
| 36 | status: this.status, |
| 37 | body: new Uint8Array(this.response) |
| 38 | }; |
| 39 | }; |
| 40 | |
| 41 | xhr.onerror = function () { |
| 42 | ply_net_http_requests[cid] = { |
| 43 | status: 0, |
| 44 | error: "Network error" |
| 45 | }; |
| 46 | }; |
| 47 | |
| 48 | xhr.ontimeout = function () { |
| 49 | ply_net_http_requests[cid] = { |
| 50 | status: 0, |
| 51 | error: "Request timed out" |
| 52 | }; |
| 53 | }; |
| 54 | |
| 55 | xhr.send(body_string.length > 0 ? body_string : null); |
| 56 | return cid; |
| 57 | } |
| 58 | |
| 59 | function ply_net_http_try_recv(cid) { |
| 60 | if (ply_net_http_requests[cid] !== undefined && ply_net_http_requests[cid] !== null) { |
no test coverage detected