|
| 1 | + |
| 2 | +exports.main = function (API) { |
| 3 | + |
| 4 | + const OBSERVER_SERVICE = API.CC["@mozilla.org/observer-service;1"].getService(API.CI.nsIObserverService); |
| 5 | + |
| 6 | + var requestIndex = 0; |
| 7 | + |
| 8 | + const LISTENER = { |
| 9 | + observe: function(subject, topic, data) { |
| 10 | + if (topic == "http-on-examine-response") { |
| 11 | + |
| 12 | + var httpChannel = subject.QueryInterface(API.CI.nsIHttpChannel); |
| 13 | + |
| 14 | + try { |
| 15 | + var requestHeaders = []; |
| 16 | + var requestId; |
| 17 | + httpChannel.visitRequestHeaders({ |
| 18 | + visitHeader: function(name, value) { |
| 19 | + requestHeaders.push({name: name, value: value}); |
| 20 | + if(name.toLowerCase()=="x-request-id") { |
| 21 | + requestId = value; |
| 22 | + } |
| 23 | + } |
| 24 | + }); |
| 25 | + var responseHeaders = [], |
| 26 | + contentType = false; |
| 27 | + httpChannel.visitResponseHeaders({ |
| 28 | + visitHeader: function(name, value) { |
| 29 | + responseHeaders.push({name: name, value: value}); |
| 30 | + if (name.toLowerCase() == "content-type") |
| 31 | + contentType = value; |
| 32 | + } |
| 33 | + }); |
| 34 | + |
| 35 | + requestIndex += 1; |
| 36 | + |
| 37 | + API.emit("response", { |
| 38 | + "request": { |
| 39 | + "id": requestId || "id:" + httpChannel.URI.spec + ":" + requestIndex, |
| 40 | + "url": httpChannel.URI.spec, |
| 41 | + "hostname": httpChannel.URI.host, |
| 42 | + "port": httpChannel.URI.port, |
| 43 | + "method": httpChannel.requestMethod, |
| 44 | + "headers": requestHeaders |
| 45 | + } |
| 46 | + "status": httpChannel.responseStatus, |
| 47 | + "contentType": contentType, |
| 48 | + "headers": responseHeaders |
| 49 | + }); |
| 50 | + |
| 51 | + } catch (err) { |
| 52 | + API.console.error(err); |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + }; |
| 57 | + |
| 58 | + |
| 59 | + OBSERVER_SERVICE.addObserver(LISTENER, "http-on-examine-response", false); |
| 60 | + |
| 61 | + API.on("destroy", function () { |
| 62 | + OBSERVER_SERVICE.removeObserver(LISTENER, "http-on-examine-response"); |
| 63 | + }); |
| 64 | +} |
0 commit comments