|
| 1 | +/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | + * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
| 3 | + * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 | +"use strict"; |
| 5 | + |
| 6 | +Cu.importGlobalProperties(["XMLHttpRequest", "Blob", "File"]); |
| 7 | + |
| 8 | +exports.Blob = Blob; |
| 9 | +exports.File = File; |
| 10 | +exports.xhr = exports.XMLHttpRequest = XMLHttpRequest; |
| 11 | + |
| 12 | +function Callbacks(underlay) { |
| 13 | + try { |
| 14 | + this.underlay = underlay.QueryInterface(Ci.nsIInterfaceRequestor); |
| 15 | + } |
| 16 | + catch (ex) { |
| 17 | + this.underlay = null; |
| 18 | + } |
| 19 | +} |
| 20 | +Callbacks.prototype = { |
| 21 | + badHost: null, |
| 22 | + getInterface: function(iid) { |
| 23 | + try { |
| 24 | + return this.QueryInterface(iid); |
| 25 | + } |
| 26 | + catch (ex) { |
| 27 | + if (this.underlay) { |
| 28 | + return this.underlay.getInterface(iid); |
| 29 | + } |
| 30 | + throw ex; |
| 31 | + } |
| 32 | + }, |
| 33 | + QueryInterface: XPCOMUtils.generateQI([Ci.nsIBadCertListener2, Ci.nsIInterfaceRequestor]), |
| 34 | + notifyCertProblem: function(si, status, host) { |
| 35 | + this.badHost = host; |
| 36 | + return false; |
| 37 | + } |
| 38 | +}; |
| 39 | + |
| 40 | +exports.request = (method, url, data, options) => { |
| 41 | + options = options || {}; |
| 42 | + return new Promise((resolve, reject) => { |
| 43 | + let req = new XMLHttpRequest(); |
| 44 | + let cbs = null; |
| 45 | + try { |
| 46 | + url = url.spec || url; |
| 47 | + if (method == "POST" && !data) { |
| 48 | + let [u, ...pieces] = url.split("?"); |
| 49 | + url = u; |
| 50 | + data = pieces.length && pieces.join("?") || null; |
| 51 | + } |
| 52 | + req.timeout = options.timeout || 10000; |
| 53 | + if (options.mime) { |
| 54 | + req.overrideMimeType(options.mime); |
| 55 | + } |
| 56 | + if (options.type) { |
| 57 | + req.responseType = options.type; |
| 58 | + } |
| 59 | + else { |
| 60 | + req.responseType = "json"; |
| 61 | + } |
| 62 | + if (options.setup) { |
| 63 | + options.setup(req); |
| 64 | + } |
| 65 | + req.addEventListener("loadend", function end() { |
| 66 | + req.removeEventListener("loadend", end, false); |
| 67 | + try { |
| 68 | + if (req.status < 100 || req.status >= 400) { |
| 69 | + if (cbs && cbs.badHost) { |
| 70 | + throw new Error(`${cbs.badHost} has an invalid TLS Certificate`); |
| 71 | + } |
| 72 | + throw new Error("Bad response"); |
| 73 | + } |
| 74 | + log(LOG_DEBUG, `request to ${url} resolved`); |
| 75 | + resolve(req); |
| 76 | + } |
| 77 | + catch (ex) { |
| 78 | + log(LOG_DEBUG, `request to ${url} rejected ${ex}`); |
| 79 | + reject(ex); |
| 80 | + } |
| 81 | + }, false); |
| 82 | + log(LOG_DEBUG, `Making request to ${url} w/ data ${data}`); |
| 83 | + req.open(method, url); |
| 84 | + if (req.channel) { |
| 85 | + try { |
| 86 | + cbs = new Callbacks(req.channel.notificationCallbacks); |
| 87 | + req.channel.notificationCallbacks = cbs; |
| 88 | + } |
| 89 | + catch (ex) { |
| 90 | + log(LOG_DEBUG, "Failed to set callbacks", ex); |
| 91 | + } |
| 92 | + if (req.channel instanceof Ci.nsIHttpChannel) { |
| 93 | + if (options.cookies) { |
| 94 | + for (let n of Object.keys(options.cookies)) { |
| 95 | + req.channel.setRequestHeader("Cookie", `${encodeURIComponent(n)}=${encodeURIComponent(options.cookies[n])}`, true); |
| 96 | + } |
| 97 | + } |
| 98 | + if (options.headers) { |
| 99 | + for (let n of Object.keys(options.headers)) { |
| 100 | + let h = options.headers[n]; |
| 101 | + let merge = true; |
| 102 | + if (Array.isArray(h)) { |
| 103 | + merge = !!h[1]; |
| 104 | + h = h[0]; |
| 105 | + } |
| 106 | + req.channel.setRequestHeader(n, h, merge); |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + req.send(data || null); |
| 112 | + } |
| 113 | + catch (ex) { |
| 114 | + reject(ex); |
| 115 | + } |
| 116 | + }); |
| 117 | +}; |
| 118 | + |
| 119 | +/* vim: set et ts=2 sw=2 : */ |
0 commit comments