2019-01-29 18:18:42 +03:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
var EXPORTED_SYMBOLS = [
|
|
|
|
"TargetListHandler",
|
|
|
|
"ProtocolHandler",
|
|
|
|
];
|
|
|
|
|
|
|
|
const {Log} = ChromeUtils.import("chrome://remote/content/Log.jsm");
|
|
|
|
const {XPCOMUtils} = ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
|
|
const {Protocol} = ChromeUtils.import("chrome://remote/content/Protocol.jsm");
|
|
|
|
|
|
|
|
XPCOMUtils.defineLazyGetter(this, "log", Log.get);
|
|
|
|
|
2019-03-04 20:45:55 +03:00
|
|
|
class JSONHandler {
|
2019-01-29 18:18:42 +03:00
|
|
|
register(server) {
|
2019-03-04 20:45:55 +03:00
|
|
|
server.registerPathHandler(this.path, (req, resp) => {
|
|
|
|
resp.setHeader("content-type", "application/json");
|
|
|
|
this.rawHandle(req, new JSONWriter(resp));
|
|
|
|
});
|
2019-01-29 18:18:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
rawHandle(request, response) {
|
|
|
|
log.trace(`(${request._scheme})-> ${request._method} ${request._path}`);
|
|
|
|
|
|
|
|
try {
|
|
|
|
this.handle(request, response);
|
|
|
|
} catch (e) {
|
|
|
|
log.warn(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-12 16:58:23 +03:00
|
|
|
class TargetListHandler extends JSONHandler {
|
2019-01-29 18:18:42 +03:00
|
|
|
constructor(targets) {
|
|
|
|
super();
|
|
|
|
this.targets = targets;
|
|
|
|
}
|
|
|
|
|
|
|
|
get path() {
|
|
|
|
return "/json/list";
|
|
|
|
}
|
|
|
|
|
|
|
|
handle(request, response) {
|
|
|
|
response.write([...this.targets]);
|
|
|
|
}
|
2019-02-12 17:44:54 +03:00
|
|
|
}
|
2019-01-29 18:18:42 +03:00
|
|
|
|
2019-02-12 16:58:23 +03:00
|
|
|
class ProtocolHandler extends JSONHandler {
|
2019-01-29 18:18:42 +03:00
|
|
|
get path() {
|
|
|
|
return "/json/protocol";
|
|
|
|
}
|
|
|
|
|
|
|
|
handle(request, response) {
|
|
|
|
response.write(Protocol.Description);
|
|
|
|
}
|
2019-02-12 17:44:54 +03:00
|
|
|
}
|
2019-01-29 18:18:42 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Wraps an httpd.js response and serialises anything passed to
|
|
|
|
* write() to JSON.
|
|
|
|
*/
|
|
|
|
class JSONWriter {
|
|
|
|
constructor(response) {
|
|
|
|
this._response = response;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Filters out null and empty strings. */
|
|
|
|
_replacer(key, value) {
|
|
|
|
if (value === null || (typeof value == "string" && value.length == 0)) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
write(data) {
|
|
|
|
try {
|
|
|
|
const json = JSON.stringify(data, this._replacer, "\t");
|
|
|
|
this._response.write(json);
|
|
|
|
} catch (e) {
|
|
|
|
log.error(`Unable to serialise JSON: ${e.message}`, e);
|
2019-02-17 20:39:19 +03:00
|
|
|
this._response.setStatusLine("1.1", 500, "Internal Server Error");
|
2019-01-29 18:18:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|