2015-10-29 07:41:54 +00:00
|
|
|
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
|
|
|
|
/* vim:set ts=2 sw=2 sts=2 et: */
|
|
|
|
/* 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/. */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* An implementation of an HTTP server both as a loadable script and as an XPCOM
|
|
|
|
* component. See the accompanying README file for user documentation on
|
|
|
|
* httpd.js.
|
|
|
|
*/
|
|
|
|
|
2019-08-27 09:30:04 +00:00
|
|
|
var EXPORTED_SYMBOLS = [
|
2015-10-29 07:41:54 +00:00
|
|
|
"HTTP_400",
|
|
|
|
"HTTP_401",
|
|
|
|
"HTTP_402",
|
|
|
|
"HTTP_403",
|
|
|
|
"HTTP_404",
|
|
|
|
"HTTP_405",
|
|
|
|
"HTTP_406",
|
|
|
|
"HTTP_407",
|
|
|
|
"HTTP_408",
|
|
|
|
"HTTP_409",
|
|
|
|
"HTTP_410",
|
|
|
|
"HTTP_411",
|
|
|
|
"HTTP_412",
|
|
|
|
"HTTP_413",
|
|
|
|
"HTTP_414",
|
|
|
|
"HTTP_415",
|
|
|
|
"HTTP_417",
|
|
|
|
"HTTP_500",
|
|
|
|
"HTTP_501",
|
|
|
|
"HTTP_502",
|
|
|
|
"HTTP_503",
|
|
|
|
"HTTP_504",
|
|
|
|
"HTTP_505",
|
|
|
|
"HttpError",
|
|
|
|
"HttpServer",
|
2022-06-02 04:42:21 +00:00
|
|
|
"NodeServer",
|
2015-10-29 07:41:54 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
const CC = Components.Constructor;
|
|
|
|
|
|
|
|
const PR_UINT32_MAX = Math.pow(2, 32) - 1;
|
|
|
|
|
|
|
|
/** True if debugging output is enabled, false otherwise. */
|
|
|
|
var DEBUG = false; // non-const *only* so tweakable in server tests
|
|
|
|
|
|
|
|
/** True if debugging output should be timestamped. */
|
|
|
|
var DEBUG_TIMESTAMP = false; // non-const so tweakable in server tests
|
|
|
|
|
2022-06-02 04:42:21 +00:00
|
|
|
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
|
|
|
|
const { AppConstants } = ChromeUtils.import(
|
|
|
|
"resource://gre/modules/AppConstants.jsm"
|
|
|
|
);
|
2015-10-29 07:41:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Asserts that the given condition holds. If it doesn't, the given message is
|
|
|
|
* dumped, a stack trace is printed, and an exception is thrown to attempt to
|
|
|
|
* stop execution (which unfortunately must rely upon the exception not being
|
|
|
|
* accidentally swallowed by the code that uses it).
|
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
function NS_ASSERT(cond, msg) {
|
|
|
|
if (DEBUG && !cond) {
|
2015-10-29 07:41:54 +00:00
|
|
|
dumpn("###!!!");
|
|
|
|
dumpn("###!!! ASSERTION" + (msg ? ": " + msg : "!"));
|
|
|
|
dumpn("###!!! Stack follows:");
|
|
|
|
|
|
|
|
var stack = new Error().stack.split(/\n/);
|
2022-06-02 04:42:21 +00:00
|
|
|
dumpn(
|
|
|
|
stack
|
|
|
|
.map(function(val) {
|
|
|
|
return "###!!! " + val;
|
|
|
|
})
|
|
|
|
.join("\n")
|
|
|
|
);
|
|
|
|
|
|
|
|
throw Components.Exception("", Cr.NS_ERROR_ABORT);
|
2015-10-29 07:41:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Constructs an HTTP error object. */
|
2022-06-02 04:42:21 +00:00
|
|
|
function HttpError(code, description) {
|
2015-10-29 07:41:54 +00:00
|
|
|
this.code = code;
|
|
|
|
this.description = description;
|
|
|
|
}
|
2022-06-02 04:42:21 +00:00
|
|
|
HttpError.prototype = {
|
|
|
|
toString() {
|
2015-10-29 07:41:54 +00:00
|
|
|
return this.code + " " + this.description;
|
2022-06-02 04:42:21 +00:00
|
|
|
},
|
2015-10-29 07:41:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Errors thrown to trigger specific HTTP server responses.
|
|
|
|
*/
|
2019-08-27 09:30:04 +00:00
|
|
|
var HTTP_400 = new HttpError(400, "Bad Request");
|
|
|
|
var HTTP_401 = new HttpError(401, "Unauthorized");
|
|
|
|
var HTTP_402 = new HttpError(402, "Payment Required");
|
|
|
|
var HTTP_403 = new HttpError(403, "Forbidden");
|
|
|
|
var HTTP_404 = new HttpError(404, "Not Found");
|
|
|
|
var HTTP_405 = new HttpError(405, "Method Not Allowed");
|
|
|
|
var HTTP_406 = new HttpError(406, "Not Acceptable");
|
|
|
|
var HTTP_407 = new HttpError(407, "Proxy Authentication Required");
|
|
|
|
var HTTP_408 = new HttpError(408, "Request Timeout");
|
|
|
|
var HTTP_409 = new HttpError(409, "Conflict");
|
|
|
|
var HTTP_410 = new HttpError(410, "Gone");
|
|
|
|
var HTTP_411 = new HttpError(411, "Length Required");
|
|
|
|
var HTTP_412 = new HttpError(412, "Precondition Failed");
|
|
|
|
var HTTP_413 = new HttpError(413, "Request Entity Too Large");
|
|
|
|
var HTTP_414 = new HttpError(414, "Request-URI Too Long");
|
|
|
|
var HTTP_415 = new HttpError(415, "Unsupported Media Type");
|
|
|
|
var HTTP_417 = new HttpError(417, "Expectation Failed");
|
|
|
|
|
|
|
|
var HTTP_500 = new HttpError(500, "Internal Server Error");
|
|
|
|
var HTTP_501 = new HttpError(501, "Not Implemented");
|
|
|
|
var HTTP_502 = new HttpError(502, "Bad Gateway");
|
|
|
|
var HTTP_503 = new HttpError(503, "Service Unavailable");
|
|
|
|
var HTTP_504 = new HttpError(504, "Gateway Timeout");
|
|
|
|
var HTTP_505 = new HttpError(505, "HTTP Version Not Supported");
|
2015-10-29 07:41:54 +00:00
|
|
|
|
|
|
|
/** Creates a hash with fields corresponding to the values in arr. */
|
2022-06-02 04:42:21 +00:00
|
|
|
function array2obj(arr) {
|
2015-10-29 07:41:54 +00:00
|
|
|
var obj = {};
|
2022-06-02 04:42:21 +00:00
|
|
|
for (var i = 0; i < arr.length; i++) {
|
2015-10-29 07:41:54 +00:00
|
|
|
obj[arr[i]] = arr[i];
|
2022-06-02 04:42:21 +00:00
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Returns an array of the integers x through y, inclusive. */
|
2022-06-02 04:42:21 +00:00
|
|
|
function range(x, y) {
|
2015-10-29 07:41:54 +00:00
|
|
|
var arr = [];
|
2022-06-02 04:42:21 +00:00
|
|
|
for (var i = x; i <= y; i++) {
|
2015-10-29 07:41:54 +00:00
|
|
|
arr.push(i);
|
2022-06-02 04:42:21 +00:00
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
return arr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** An object (hash) whose fields are the numbers of all HTTP error codes. */
|
|
|
|
const HTTP_ERROR_CODES = array2obj(range(400, 417).concat(range(500, 505)));
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The character used to distinguish hidden files from non-hidden files, a la
|
|
|
|
* the leading dot in Apache. Since that mechanism also hides files from
|
|
|
|
* easy display in LXR, ls output, etc. however, we choose instead to use a
|
|
|
|
* suffix character. If a requested file ends with it, we append another
|
|
|
|
* when getting the file on the server. If it doesn't, we just look up that
|
|
|
|
* file. Therefore, any file whose name ends with exactly one of the character
|
|
|
|
* is "hidden" and available for use by the server.
|
|
|
|
*/
|
|
|
|
const HIDDEN_CHAR = "^";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The file name suffix indicating the file containing overridden headers for
|
|
|
|
* a requested file.
|
|
|
|
*/
|
|
|
|
const HEADERS_SUFFIX = HIDDEN_CHAR + "headers" + HIDDEN_CHAR;
|
2022-06-02 04:42:21 +00:00
|
|
|
const INFORMATIONAL_RESPONSE_SUFFIX =
|
|
|
|
HIDDEN_CHAR + "informationalResponse" + HIDDEN_CHAR;
|
2015-10-29 07:41:54 +00:00
|
|
|
|
|
|
|
/** Type used to denote SJS scripts for CGI-like functionality. */
|
|
|
|
const SJS_TYPE = "sjs";
|
|
|
|
|
|
|
|
/** Base for relative timestamps produced by dumpn(). */
|
|
|
|
var firstStamp = 0;
|
|
|
|
|
|
|
|
/** dump(str) with a trailing "\n" -- only outputs if DEBUG. */
|
2022-06-02 04:42:21 +00:00
|
|
|
function dumpn(str) {
|
|
|
|
if (DEBUG) {
|
2015-10-29 07:41:54 +00:00
|
|
|
var prefix = "HTTPD-INFO | ";
|
2022-06-02 04:42:21 +00:00
|
|
|
if (DEBUG_TIMESTAMP) {
|
|
|
|
if (firstStamp === 0) {
|
2015-10-29 07:41:54 +00:00
|
|
|
firstStamp = Date.now();
|
2022-06-02 04:42:21 +00:00
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
|
|
|
|
var elapsed = Date.now() - firstStamp; // milliseconds
|
|
|
|
var min = Math.floor(elapsed / 60000);
|
|
|
|
var sec = (elapsed % 60000) / 1000;
|
|
|
|
|
2022-06-02 04:42:21 +00:00
|
|
|
if (sec < 10) {
|
2015-10-29 07:41:54 +00:00
|
|
|
prefix += min + ":0" + sec.toFixed(3) + " | ";
|
2022-06-02 04:42:21 +00:00
|
|
|
} else {
|
2015-10-29 07:41:54 +00:00
|
|
|
prefix += min + ":" + sec.toFixed(3) + " | ";
|
2022-06-02 04:42:21 +00:00
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
dump(prefix + str + "\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Dumps the current JS stack if DEBUG. */
|
2022-06-02 04:42:21 +00:00
|
|
|
function dumpStack() {
|
2015-10-29 07:41:54 +00:00
|
|
|
// peel off the frames for dumpStack() and Error()
|
|
|
|
var stack = new Error().stack.split(/\n/).slice(2);
|
|
|
|
stack.forEach(dumpn);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** The XPCOM thread manager. */
|
|
|
|
var gThreadManager = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* JavaScript constructors for commonly-used classes; precreating these is a
|
|
|
|
* speedup over doing the same from base principles. See the docs at
|
|
|
|
* http://developer.mozilla.org/en/docs/Components.Constructor for details.
|
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
const ServerSocket = CC(
|
|
|
|
"@mozilla.org/network/server-socket;1",
|
|
|
|
"nsIServerSocket",
|
|
|
|
"init"
|
|
|
|
);
|
|
|
|
const ServerSocketIPv6 = CC(
|
|
|
|
"@mozilla.org/network/server-socket;1",
|
|
|
|
"nsIServerSocket",
|
|
|
|
"initIPv6"
|
|
|
|
);
|
|
|
|
const ScriptableInputStream = CC(
|
|
|
|
"@mozilla.org/scriptableinputstream;1",
|
|
|
|
"nsIScriptableInputStream",
|
|
|
|
"init"
|
|
|
|
);
|
|
|
|
const Pipe = CC("@mozilla.org/pipe;1", "nsIPipe", "init");
|
|
|
|
const FileInputStream = CC(
|
|
|
|
"@mozilla.org/network/file-input-stream;1",
|
|
|
|
"nsIFileInputStream",
|
|
|
|
"init"
|
|
|
|
);
|
|
|
|
const ConverterInputStream = CC(
|
|
|
|
"@mozilla.org/intl/converter-input-stream;1",
|
|
|
|
"nsIConverterInputStream",
|
|
|
|
"init"
|
|
|
|
);
|
|
|
|
const WritablePropertyBag = CC(
|
|
|
|
"@mozilla.org/hash-property-bag;1",
|
|
|
|
"nsIWritablePropertyBag2"
|
|
|
|
);
|
|
|
|
const SupportsString = CC(
|
|
|
|
"@mozilla.org/supports-string;1",
|
|
|
|
"nsISupportsString"
|
|
|
|
);
|
2015-10-29 07:41:54 +00:00
|
|
|
|
|
|
|
/* These two are non-const only so a test can overwrite them. */
|
2022-06-02 04:42:21 +00:00
|
|
|
var BinaryInputStream = CC(
|
|
|
|
"@mozilla.org/binaryinputstream;1",
|
|
|
|
"nsIBinaryInputStream",
|
|
|
|
"setInputStream"
|
|
|
|
);
|
|
|
|
var BinaryOutputStream = CC(
|
|
|
|
"@mozilla.org/binaryoutputstream;1",
|
|
|
|
"nsIBinaryOutputStream",
|
|
|
|
"setOutputStream"
|
|
|
|
);
|
2015-10-29 07:41:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the RFC 822/1123 representation of a date.
|
|
|
|
*
|
|
|
|
* @param date : Number
|
|
|
|
* the date, in milliseconds from midnight (00:00:00), January 1, 1970 GMT
|
|
|
|
* @returns string
|
|
|
|
* the representation of the given date
|
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
function toDateString(date) {
|
2015-10-29 07:41:54 +00:00
|
|
|
//
|
|
|
|
// rfc1123-date = wkday "," SP date1 SP time SP "GMT"
|
|
|
|
// date1 = 2DIGIT SP month SP 4DIGIT
|
|
|
|
// ; day month year (e.g., 02 Jun 1982)
|
|
|
|
// time = 2DIGIT ":" 2DIGIT ":" 2DIGIT
|
|
|
|
// ; 00:00:00 - 23:59:59
|
|
|
|
// wkday = "Mon" | "Tue" | "Wed"
|
|
|
|
// | "Thu" | "Fri" | "Sat" | "Sun"
|
|
|
|
// month = "Jan" | "Feb" | "Mar" | "Apr"
|
|
|
|
// | "May" | "Jun" | "Jul" | "Aug"
|
|
|
|
// | "Sep" | "Oct" | "Nov" | "Dec"
|
|
|
|
//
|
|
|
|
|
|
|
|
const wkdayStrings = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
|
2022-06-02 04:42:21 +00:00
|
|
|
const monthStrings = [
|
|
|
|
"Jan",
|
|
|
|
"Feb",
|
|
|
|
"Mar",
|
|
|
|
"Apr",
|
|
|
|
"May",
|
|
|
|
"Jun",
|
|
|
|
"Jul",
|
|
|
|
"Aug",
|
|
|
|
"Sep",
|
|
|
|
"Oct",
|
|
|
|
"Nov",
|
|
|
|
"Dec",
|
|
|
|
];
|
2015-10-29 07:41:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Processes a date and returns the encoded UTC time as a string according to
|
|
|
|
* the format specified in RFC 2616.
|
|
|
|
*
|
|
|
|
* @param date : Date
|
|
|
|
* the date to process
|
|
|
|
* @returns string
|
|
|
|
* a string of the form "HH:MM:SS", ranging from "00:00:00" to "23:59:59"
|
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
function toTime(date) {
|
2015-10-29 07:41:54 +00:00
|
|
|
var hrs = date.getUTCHours();
|
2022-06-02 04:42:21 +00:00
|
|
|
var rv = hrs < 10 ? "0" + hrs : hrs;
|
|
|
|
|
2015-10-29 07:41:54 +00:00
|
|
|
var mins = date.getUTCMinutes();
|
|
|
|
rv += ":";
|
2022-06-02 04:42:21 +00:00
|
|
|
rv += mins < 10 ? "0" + mins : mins;
|
2015-10-29 07:41:54 +00:00
|
|
|
|
|
|
|
var secs = date.getUTCSeconds();
|
|
|
|
rv += ":";
|
2022-06-02 04:42:21 +00:00
|
|
|
rv += secs < 10 ? "0" + secs : secs;
|
2015-10-29 07:41:54 +00:00
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Processes a date and returns the encoded UTC date as a string according to
|
|
|
|
* the date1 format specified in RFC 2616.
|
|
|
|
*
|
|
|
|
* @param date : Date
|
|
|
|
* the date to process
|
|
|
|
* @returns string
|
|
|
|
* a string of the form "HH:MM:SS", ranging from "00:00:00" to "23:59:59"
|
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
function toDate1(date) {
|
2015-10-29 07:41:54 +00:00
|
|
|
var day = date.getUTCDate();
|
|
|
|
var month = date.getUTCMonth();
|
|
|
|
var year = date.getUTCFullYear();
|
|
|
|
|
2022-06-02 04:42:21 +00:00
|
|
|
var rv = day < 10 ? "0" + day : day;
|
2015-10-29 07:41:54 +00:00
|
|
|
rv += " " + monthStrings[month];
|
|
|
|
rv += " " + year;
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
date = new Date(date);
|
|
|
|
|
|
|
|
const fmtString = "%wkday%, %date1% %time% GMT";
|
|
|
|
var rv = fmtString.replace("%wkday%", wkdayStrings[date.getUTCDay()]);
|
|
|
|
rv = rv.replace("%time%", toTime(date));
|
|
|
|
return rv.replace("%date1%", toDate1(date));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prints out a human-readable representation of the object o and its fields,
|
|
|
|
* omitting those whose names begin with "_" if showMembers != true (to ignore
|
|
|
|
* "private" properties exposed via getters/setters).
|
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
function printObj(o, showMembers) {
|
2015-10-29 07:41:54 +00:00
|
|
|
var s = "******************************\n";
|
2022-06-02 04:42:21 +00:00
|
|
|
s += "o = {\n";
|
|
|
|
for (var i in o) {
|
|
|
|
if (typeof i != "string" || showMembers || (i.length > 0 && i[0] != "_")) {
|
|
|
|
s += " " + i + ": " + o[i] + ",\n";
|
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
}
|
2022-06-02 04:42:21 +00:00
|
|
|
s += " };\n";
|
|
|
|
s += "******************************";
|
2015-10-29 07:41:54 +00:00
|
|
|
dumpn(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Instantiates a new HTTP server.
|
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
function nsHttpServer() {
|
|
|
|
if (!gThreadManager) {
|
2015-10-29 07:41:54 +00:00
|
|
|
gThreadManager = Cc["@mozilla.org/thread-manager;1"].getService();
|
2022-06-02 04:42:21 +00:00
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
|
|
|
|
/** The port on which this server listens. */
|
|
|
|
this._port = undefined;
|
|
|
|
|
|
|
|
/** The socket associated with this. */
|
|
|
|
this._socket = null;
|
|
|
|
|
|
|
|
/** The handler used to process requests to this server. */
|
|
|
|
this._handler = new ServerHandler(this);
|
|
|
|
|
|
|
|
/** Naming information for this server. */
|
|
|
|
this._identity = new ServerIdentity();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Indicates when the server is to be shut down at the end of the request.
|
|
|
|
*/
|
|
|
|
this._doQuit = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* True if the socket in this is closed (and closure notifications have been
|
|
|
|
* sent and processed if the socket was ever opened), false otherwise.
|
|
|
|
*/
|
|
|
|
this._socketClosed = true;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used for tracking existing connections and ensuring that all connections
|
|
|
|
* are properly cleaned up before server shutdown; increases by 1 for every
|
|
|
|
* new incoming connection.
|
|
|
|
*/
|
|
|
|
this._connectionGen = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Hash of all open connections, indexed by connection number at time of
|
|
|
|
* creation.
|
|
|
|
*/
|
|
|
|
this._connections = {};
|
|
|
|
}
|
2022-06-02 04:42:21 +00:00
|
|
|
nsHttpServer.prototype = {
|
2015-10-29 07:41:54 +00:00
|
|
|
// NSISERVERSOCKETLISTENER
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Processes an incoming request coming in on the given socket and contained
|
|
|
|
* in the given transport.
|
|
|
|
*
|
|
|
|
* @param socket : nsIServerSocket
|
|
|
|
* the socket through which the request was served
|
|
|
|
* @param trans : nsISocketTransport
|
|
|
|
* the transport for the request/response
|
|
|
|
* @see nsIServerSocketListener.onSocketAccepted
|
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
onSocketAccepted(socket, trans) {
|
2015-10-29 07:41:54 +00:00
|
|
|
dumpn("*** onSocketAccepted(socket=" + socket + ", trans=" + trans + ")");
|
|
|
|
|
|
|
|
dumpn(">>> new connection on " + trans.host + ":" + trans.port);
|
|
|
|
|
|
|
|
const SEGMENT_SIZE = 8192;
|
|
|
|
const SEGMENT_COUNT = 1024;
|
2022-06-02 04:42:21 +00:00
|
|
|
try {
|
|
|
|
var input = trans
|
|
|
|
.openInputStream(0, SEGMENT_SIZE, SEGMENT_COUNT)
|
|
|
|
.QueryInterface(Ci.nsIAsyncInputStream);
|
2015-10-29 07:41:54 +00:00
|
|
|
var output = trans.openOutputStream(0, 0, 0);
|
2022-06-02 04:42:21 +00:00
|
|
|
} catch (e) {
|
2015-10-29 07:41:54 +00:00
|
|
|
dumpn("*** error opening transport streams: " + e);
|
|
|
|
trans.close(Cr.NS_BINDING_ABORTED);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var connectionNumber = ++this._connectionGen;
|
|
|
|
|
2022-06-02 04:42:21 +00:00
|
|
|
try {
|
|
|
|
var conn = new Connection(
|
|
|
|
input,
|
|
|
|
output,
|
|
|
|
this,
|
|
|
|
socket.port,
|
|
|
|
trans.port,
|
|
|
|
connectionNumber,
|
|
|
|
trans
|
|
|
|
);
|
2015-10-29 07:41:54 +00:00
|
|
|
var reader = new RequestReader(conn);
|
|
|
|
|
|
|
|
// XXX add request timeout functionality here!
|
|
|
|
|
|
|
|
// Note: must use main thread here, or we might get a GC that will cause
|
|
|
|
// threadsafety assertions. We really need to fix XPConnect so that
|
|
|
|
// you can actually do things in multi-threaded JS. :-(
|
|
|
|
input.asyncWait(reader, 0, 0, gThreadManager.mainThread);
|
2022-06-02 04:42:21 +00:00
|
|
|
} catch (e) {
|
2015-10-29 07:41:54 +00:00
|
|
|
// Assume this connection can't be salvaged and bail on it completely;
|
|
|
|
// don't attempt to close it so that we can assert that any connection
|
|
|
|
// being closed is in this._connections.
|
|
|
|
dumpn("*** error in initial request-processing stages: " + e);
|
|
|
|
trans.close(Cr.NS_BINDING_ABORTED);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._connections[connectionNumber] = conn;
|
|
|
|
dumpn("*** starting connection " + connectionNumber);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called when the socket associated with this is closed.
|
|
|
|
*
|
|
|
|
* @param socket : nsIServerSocket
|
|
|
|
* the socket being closed
|
|
|
|
* @param status : nsresult
|
|
|
|
* the reason the socket stopped listening (NS_BINDING_ABORTED if the server
|
|
|
|
* was stopped using nsIHttpServer.stop)
|
|
|
|
* @see nsIServerSocketListener.onStopListening
|
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
onStopListening(socket, status) {
|
2015-10-29 07:41:54 +00:00
|
|
|
dumpn(">>> shutting down server on port " + socket.port);
|
|
|
|
for (var n in this._connections) {
|
|
|
|
if (!this._connections[n]._requestStarted) {
|
|
|
|
this._connections[n].close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this._socketClosed = true;
|
|
|
|
if (this._hasOpenConnections()) {
|
|
|
|
dumpn("*** open connections!!!");
|
|
|
|
}
|
2022-06-02 04:42:21 +00:00
|
|
|
if (!this._hasOpenConnections()) {
|
2015-10-29 07:41:54 +00:00
|
|
|
dumpn("*** no open connections, notifying async from onStopListening");
|
|
|
|
|
|
|
|
// Notify asynchronously so that any pending teardown in stop() has a
|
|
|
|
// chance to run first.
|
|
|
|
var self = this;
|
2022-06-02 04:42:21 +00:00
|
|
|
var stopEvent = {
|
|
|
|
run() {
|
|
|
|
dumpn("*** _notifyStopped async callback");
|
|
|
|
self._notifyStopped();
|
|
|
|
},
|
|
|
|
};
|
|
|
|
gThreadManager.currentThread.dispatch(
|
|
|
|
stopEvent,
|
|
|
|
Ci.nsIThread.DISPATCH_NORMAL
|
|
|
|
);
|
2015-10-29 07:41:54 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
// NSIHTTPSERVER
|
|
|
|
|
|
|
|
//
|
|
|
|
// see nsIHttpServer.start
|
|
|
|
//
|
2022-06-02 04:42:21 +00:00
|
|
|
start(port) {
|
|
|
|
this._start(port, "localhost");
|
2015-10-29 07:41:54 +00:00
|
|
|
},
|
|
|
|
|
2022-06-02 04:42:21 +00:00
|
|
|
//
|
|
|
|
// see nsIHttpServer.start_ipv6
|
|
|
|
//
|
|
|
|
start_ipv6(port) {
|
|
|
|
this._start(port, "[::1]");
|
|
|
|
},
|
|
|
|
|
|
|
|
_start(port, host) {
|
|
|
|
if (this._socket) {
|
|
|
|
throw Components.Exception("", Cr.NS_ERROR_ALREADY_INITIALIZED);
|
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
|
|
|
|
this._port = port;
|
|
|
|
this._doQuit = this._socketClosed = false;
|
|
|
|
|
|
|
|
this._host = host;
|
|
|
|
|
|
|
|
// The listen queue needs to be long enough to handle
|
|
|
|
// network.http.max-persistent-connections-per-server or
|
|
|
|
// network.http.max-persistent-connections-per-proxy concurrent
|
|
|
|
// connections, plus a safety margin in case some other process is
|
|
|
|
// talking to the server as well.
|
2022-06-02 04:42:21 +00:00
|
|
|
var maxConnections =
|
|
|
|
5 +
|
|
|
|
Math.max(
|
|
|
|
Services.prefs.getIntPref(
|
|
|
|
"network.http.max-persistent-connections-per-server"
|
|
|
|
),
|
|
|
|
Services.prefs.getIntPref(
|
|
|
|
"network.http.max-persistent-connections-per-proxy"
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
try {
|
2015-10-29 07:41:54 +00:00
|
|
|
var loopback = true;
|
2022-06-02 04:42:21 +00:00
|
|
|
if (
|
|
|
|
this._host != "127.0.0.1" &&
|
|
|
|
this._host != "localhost" &&
|
|
|
|
this._host != "[::1]"
|
|
|
|
) {
|
|
|
|
loopback = false;
|
2015-10-29 07:41:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// When automatically selecting a port, sometimes the chosen port is
|
|
|
|
// "blocked" from clients. We don't want to use these ports because
|
|
|
|
// tests will intermittently fail. So, we simply keep trying to to
|
|
|
|
// get a server socket until a valid port is obtained. We limit
|
|
|
|
// ourselves to finite attempts just so we don't loop forever.
|
|
|
|
var socket;
|
2022-06-02 04:42:21 +00:00
|
|
|
for (var i = 100; i; i--) {
|
|
|
|
var temp = null;
|
|
|
|
if (this._host.includes(":")) {
|
|
|
|
temp = new ServerSocketIPv6(
|
|
|
|
this._port,
|
|
|
|
loopback, // true = localhost, false = everybody
|
|
|
|
maxConnections
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
temp = new ServerSocket(
|
|
|
|
this._port,
|
|
|
|
loopback, // true = localhost, false = everybody
|
|
|
|
maxConnections
|
|
|
|
);
|
2015-10-29 07:41:54 +00:00
|
|
|
}
|
|
|
|
|
2022-06-02 04:42:21 +00:00
|
|
|
var allowed = Services.io.allowPort(temp.port, "http");
|
|
|
|
if (!allowed) {
|
|
|
|
dumpn(
|
|
|
|
">>>Warning: obtained ServerSocket listens on a blocked " +
|
|
|
|
"port: " +
|
|
|
|
temp.port
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!allowed && this._port == -1) {
|
2015-10-29 07:41:54 +00:00
|
|
|
dumpn(">>>Throwing away ServerSocket with bad port.");
|
|
|
|
temp.close();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
socket = temp;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!socket) {
|
2022-06-02 04:42:21 +00:00
|
|
|
throw new Error(
|
|
|
|
"No socket server available. Are there no available ports?"
|
|
|
|
);
|
2015-10-29 07:41:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
socket.asyncListen(this);
|
|
|
|
this._port = socket.port;
|
|
|
|
this._identity._initialize(socket.port, host, true);
|
|
|
|
this._socket = socket;
|
2022-06-02 04:42:21 +00:00
|
|
|
dumpn(
|
|
|
|
">>> listening on port " +
|
|
|
|
socket.port +
|
|
|
|
", " +
|
|
|
|
maxConnections +
|
|
|
|
" pending connections"
|
|
|
|
);
|
|
|
|
} catch (e) {
|
2015-10-29 07:41:54 +00:00
|
|
|
dump("\n!!! could not start server on port " + port + ": " + e + "\n\n");
|
2022-06-02 04:42:21 +00:00
|
|
|
throw Components.Exception("", Cr.NS_ERROR_NOT_AVAILABLE);
|
2015-10-29 07:41:54 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
//
|
|
|
|
// see nsIHttpServer.stop
|
|
|
|
//
|
2022-06-02 04:42:21 +00:00
|
|
|
stop(callback) {
|
|
|
|
if (!this._socket) {
|
|
|
|
throw Components.Exception("", Cr.NS_ERROR_UNEXPECTED);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If no argument was provided to stop, return a promise.
|
|
|
|
let returnValue = undefined;
|
|
|
|
if (!callback) {
|
|
|
|
returnValue = new Promise(resolve => {
|
|
|
|
callback = resolve;
|
|
|
|
});
|
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
|
2022-06-02 04:42:21 +00:00
|
|
|
this._stopCallback =
|
|
|
|
typeof callback === "function"
|
|
|
|
? callback
|
|
|
|
: function() {
|
|
|
|
callback.onStopped();
|
|
|
|
};
|
2015-10-29 07:41:54 +00:00
|
|
|
|
|
|
|
dumpn(">>> stopping listening on port " + this._socket.port);
|
|
|
|
this._socket.close();
|
|
|
|
this._socket = null;
|
|
|
|
|
|
|
|
// We can't have this identity any more, and the port on which we're running
|
|
|
|
// this server now could be meaningless the next time around.
|
|
|
|
this._identity._teardown();
|
|
|
|
|
|
|
|
this._doQuit = false;
|
|
|
|
|
|
|
|
// socket-close notification and pending request completion happen async
|
2022-06-02 04:42:21 +00:00
|
|
|
|
|
|
|
return returnValue;
|
2015-10-29 07:41:54 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
//
|
|
|
|
// see nsIHttpServer.registerFile
|
|
|
|
//
|
2022-06-02 04:42:21 +00:00
|
|
|
registerFile(path, file, handler) {
|
|
|
|
if (file && (!file.exists() || file.isDirectory())) {
|
|
|
|
throw Components.Exception("", Cr.NS_ERROR_INVALID_ARG);
|
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
|
2022-06-02 04:42:21 +00:00
|
|
|
this._handler.registerFile(path, file, handler);
|
2015-10-29 07:41:54 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
//
|
|
|
|
// see nsIHttpServer.registerDirectory
|
|
|
|
//
|
2022-06-02 04:42:21 +00:00
|
|
|
registerDirectory(path, directory) {
|
2015-10-29 07:41:54 +00:00
|
|
|
// XXX true path validation!
|
2022-06-02 04:42:21 +00:00
|
|
|
if (
|
|
|
|
path.charAt(0) != "/" ||
|
|
|
|
path.charAt(path.length - 1) != "/" ||
|
|
|
|
(directory && (!directory.exists() || !directory.isDirectory()))
|
|
|
|
) {
|
|
|
|
throw Components.Exception("", Cr.NS_ERROR_INVALID_ARG);
|
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
|
|
|
|
// XXX determine behavior of nonexistent /foo/bar when a /foo/bar/ mapping
|
|
|
|
// exists!
|
|
|
|
|
|
|
|
this._handler.registerDirectory(path, directory);
|
|
|
|
},
|
|
|
|
|
|
|
|
//
|
|
|
|
// see nsIHttpServer.registerPathHandler
|
|
|
|
//
|
2022-06-02 04:42:21 +00:00
|
|
|
registerPathHandler(path, handler) {
|
2015-10-29 07:41:54 +00:00
|
|
|
this._handler.registerPathHandler(path, handler);
|
|
|
|
},
|
|
|
|
|
|
|
|
//
|
|
|
|
// see nsIHttpServer.registerPrefixHandler
|
|
|
|
//
|
2022-06-02 04:42:21 +00:00
|
|
|
registerPrefixHandler(prefix, handler) {
|
2015-10-29 07:41:54 +00:00
|
|
|
this._handler.registerPrefixHandler(prefix, handler);
|
|
|
|
},
|
|
|
|
|
|
|
|
//
|
|
|
|
// see nsIHttpServer.registerErrorHandler
|
|
|
|
//
|
2022-06-02 04:42:21 +00:00
|
|
|
registerErrorHandler(code, handler) {
|
2015-10-29 07:41:54 +00:00
|
|
|
this._handler.registerErrorHandler(code, handler);
|
|
|
|
},
|
|
|
|
|
|
|
|
//
|
|
|
|
// see nsIHttpServer.setIndexHandler
|
|
|
|
//
|
2022-06-02 04:42:21 +00:00
|
|
|
setIndexHandler(handler) {
|
2015-10-29 07:41:54 +00:00
|
|
|
this._handler.setIndexHandler(handler);
|
|
|
|
},
|
|
|
|
|
|
|
|
//
|
|
|
|
// see nsIHttpServer.registerContentType
|
|
|
|
//
|
2022-06-02 04:42:21 +00:00
|
|
|
registerContentType(ext, type) {
|
2015-10-29 07:41:54 +00:00
|
|
|
this._handler.registerContentType(ext, type);
|
|
|
|
},
|
|
|
|
|
2022-06-02 04:42:21 +00:00
|
|
|
get connectionNumber() {
|
2019-08-27 09:30:04 +00:00
|
|
|
return this._connectionGen;
|
|
|
|
},
|
|
|
|
|
2015-10-29 07:41:54 +00:00
|
|
|
//
|
|
|
|
// see nsIHttpServer.serverIdentity
|
|
|
|
//
|
2022-06-02 04:42:21 +00:00
|
|
|
get identity() {
|
2015-10-29 07:41:54 +00:00
|
|
|
return this._identity;
|
|
|
|
},
|
|
|
|
|
|
|
|
//
|
|
|
|
// see nsIHttpServer.getState
|
|
|
|
//
|
2022-06-02 04:42:21 +00:00
|
|
|
getState(path, k) {
|
2015-10-29 07:41:54 +00:00
|
|
|
return this._handler._getState(path, k);
|
|
|
|
},
|
|
|
|
|
|
|
|
//
|
|
|
|
// see nsIHttpServer.setState
|
|
|
|
//
|
2022-06-02 04:42:21 +00:00
|
|
|
setState(path, k, v) {
|
2015-10-29 07:41:54 +00:00
|
|
|
return this._handler._setState(path, k, v);
|
|
|
|
},
|
|
|
|
|
|
|
|
//
|
|
|
|
// see nsIHttpServer.getSharedState
|
|
|
|
//
|
2022-06-02 04:42:21 +00:00
|
|
|
getSharedState(k) {
|
2015-10-29 07:41:54 +00:00
|
|
|
return this._handler._getSharedState(k);
|
|
|
|
},
|
|
|
|
|
|
|
|
//
|
|
|
|
// see nsIHttpServer.setSharedState
|
|
|
|
//
|
2022-06-02 04:42:21 +00:00
|
|
|
setSharedState(k, v) {
|
2015-10-29 07:41:54 +00:00
|
|
|
return this._handler._setSharedState(k, v);
|
|
|
|
},
|
|
|
|
|
|
|
|
//
|
|
|
|
// see nsIHttpServer.getObjectState
|
|
|
|
//
|
2022-06-02 04:42:21 +00:00
|
|
|
getObjectState(k) {
|
2015-10-29 07:41:54 +00:00
|
|
|
return this._handler._getObjectState(k);
|
|
|
|
},
|
|
|
|
|
|
|
|
//
|
|
|
|
// see nsIHttpServer.setObjectState
|
|
|
|
//
|
2022-06-02 04:42:21 +00:00
|
|
|
setObjectState(k, v) {
|
2015-10-29 07:41:54 +00:00
|
|
|
return this._handler._setObjectState(k, v);
|
|
|
|
},
|
|
|
|
|
2019-08-27 09:30:04 +00:00
|
|
|
get wrappedJSObject() {
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2015-10-29 07:41:54 +00:00
|
|
|
// NSISUPPORTS
|
|
|
|
|
|
|
|
//
|
|
|
|
// see nsISupports.QueryInterface
|
|
|
|
//
|
2022-06-02 04:42:21 +00:00
|
|
|
QueryInterface: ChromeUtils.generateQI([
|
|
|
|
"nsIHttpServer",
|
|
|
|
"nsIServerSocketListener",
|
|
|
|
]),
|
2015-10-29 07:41:54 +00:00
|
|
|
|
|
|
|
// NON-XPCOM PUBLIC API
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true iff this server is not running (and is not in the process of
|
|
|
|
* serving any requests still to be processed when the server was last
|
|
|
|
* stopped after being run).
|
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
isStopped() {
|
2015-10-29 07:41:54 +00:00
|
|
|
return this._socketClosed && !this._hasOpenConnections();
|
|
|
|
},
|
|
|
|
|
|
|
|
// PRIVATE IMPLEMENTATION
|
|
|
|
|
|
|
|
/** True if this server has any open connections to it, false otherwise. */
|
2022-06-02 04:42:21 +00:00
|
|
|
_hasOpenConnections() {
|
2015-10-29 07:41:54 +00:00
|
|
|
//
|
|
|
|
// If we have any open connections, they're tracked as numeric properties on
|
|
|
|
// |this._connections|. The non-standard __count__ property could be used
|
|
|
|
// to check whether there are any properties, but standard-wise, even
|
|
|
|
// looking forward to ES5, there's no less ugly yet still O(1) way to do
|
|
|
|
// this.
|
|
|
|
//
|
2022-06-02 04:42:21 +00:00
|
|
|
for (var n in this._connections) {
|
2015-10-29 07:41:54 +00:00
|
|
|
return true;
|
2022-06-02 04:42:21 +00:00
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
|
|
|
/** Calls the server-stopped callback provided when stop() was called. */
|
2022-06-02 04:42:21 +00:00
|
|
|
_notifyStopped() {
|
2015-10-29 07:41:54 +00:00
|
|
|
NS_ASSERT(this._stopCallback !== null, "double-notifying?");
|
|
|
|
NS_ASSERT(!this._hasOpenConnections(), "should be done serving by now");
|
|
|
|
|
|
|
|
//
|
|
|
|
// NB: We have to grab this now, null out the member, *then* call the
|
|
|
|
// callback here, or otherwise the callback could (indirectly) futz with
|
|
|
|
// this._stopCallback by starting and immediately stopping this, at
|
|
|
|
// which point we'd be nulling out a field we no longer have a right to
|
|
|
|
// modify.
|
|
|
|
//
|
|
|
|
var callback = this._stopCallback;
|
|
|
|
this._stopCallback = null;
|
2022-06-02 04:42:21 +00:00
|
|
|
try {
|
2015-10-29 07:41:54 +00:00
|
|
|
callback();
|
2022-06-02 04:42:21 +00:00
|
|
|
} catch (e) {
|
2015-10-29 07:41:54 +00:00
|
|
|
// not throwing because this is specified as being usually (but not
|
|
|
|
// always) asynchronous
|
|
|
|
dump("!!! error running onStopped callback: " + e + "\n");
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Notifies this server that the given connection has been closed.
|
|
|
|
*
|
|
|
|
* @param connection : Connection
|
|
|
|
* the connection that was closed
|
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
_connectionClosed(connection) {
|
|
|
|
NS_ASSERT(
|
|
|
|
connection.number in this._connections,
|
|
|
|
"closing a connection " +
|
|
|
|
this +
|
|
|
|
" that we never added to the " +
|
|
|
|
"set of open connections?"
|
|
|
|
);
|
|
|
|
NS_ASSERT(
|
|
|
|
this._connections[connection.number] === connection,
|
|
|
|
"connection number mismatch? " + this._connections[connection.number]
|
|
|
|
);
|
2015-10-29 07:41:54 +00:00
|
|
|
delete this._connections[connection.number];
|
|
|
|
|
|
|
|
// Fire a pending server-stopped notification if it's our responsibility.
|
2022-06-02 04:42:21 +00:00
|
|
|
if (!this._hasOpenConnections() && this._socketClosed) {
|
2015-10-29 07:41:54 +00:00
|
|
|
this._notifyStopped();
|
2022-06-02 04:42:21 +00:00
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Requests that the server be shut down when possible.
|
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
_requestQuit() {
|
2015-10-29 07:41:54 +00:00
|
|
|
dumpn(">>> requesting a quit");
|
|
|
|
dumpStack();
|
|
|
|
this._doQuit = true;
|
2022-06-02 04:42:21 +00:00
|
|
|
},
|
2015-10-29 07:41:54 +00:00
|
|
|
};
|
|
|
|
|
2019-08-27 09:30:04 +00:00
|
|
|
var HttpServer = nsHttpServer;
|
2015-10-29 07:41:54 +00:00
|
|
|
|
2022-06-02 04:42:21 +00:00
|
|
|
class NodeServer {
|
|
|
|
// Executes command in the context of a node server.
|
|
|
|
// See handler in moz-http2.js
|
|
|
|
//
|
|
|
|
// Example use:
|
|
|
|
// let id = NodeServer.fork(); // id is a random string
|
|
|
|
// await NodeServer.execute(id, `"hello"`)
|
|
|
|
// > "hello"
|
|
|
|
// await NodeServer.execute(id, `(() => "hello")()`)
|
|
|
|
// > "hello"
|
|
|
|
// await NodeServer.execute(id, `(() => var_defined_on_server)()`)
|
|
|
|
// > "0"
|
|
|
|
// await NodeServer.execute(id, `var_defined_on_server`)
|
|
|
|
// > "0"
|
|
|
|
// function f(param) { if (param) return param; return "bla"; }
|
|
|
|
// await NodeServer.execute(id, f); // Defines the function on the server
|
|
|
|
// await NodeServer.execute(id, `f()`) // executes defined function
|
|
|
|
// > "bla"
|
|
|
|
// let result = await NodeServer.execute(id, `f("test")`);
|
|
|
|
// > "test"
|
|
|
|
// await NodeServer.kill(id); // shuts down the server
|
|
|
|
|
|
|
|
// Forks a new node server using moz-http2-child.js as a starting point
|
|
|
|
static fork() {
|
|
|
|
return this.sendCommand("", "/fork");
|
|
|
|
}
|
|
|
|
// Executes command in the context of the node server indicated by `id`
|
|
|
|
static execute(id, command) {
|
|
|
|
return this.sendCommand(command, `/execute/${id}`);
|
|
|
|
}
|
|
|
|
// Shuts down the server
|
|
|
|
static kill(id) {
|
|
|
|
return this.sendCommand("", `/kill/${id}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Issues a request to the node server (handler defined in moz-http2.js)
|
|
|
|
// This method should not be called directly.
|
|
|
|
static sendCommand(command, path) {
|
|
|
|
let env = Cc["@mozilla.org/process/environment;1"].getService(
|
|
|
|
Ci.nsIEnvironment
|
|
|
|
);
|
|
|
|
let h2Port = env.get("MOZNODE_EXEC_PORT");
|
|
|
|
if (!h2Port) {
|
|
|
|
throw new Error("Could not find MOZNODE_EXEC_PORT");
|
|
|
|
}
|
|
|
|
|
|
|
|
let req = new XMLHttpRequest();
|
|
|
|
const serverIP =
|
|
|
|
AppConstants.platform == "android" ? "10.0.2.2" : "127.0.0.1";
|
|
|
|
req.open("POST", `http://${serverIP}:${h2Port}${path}`);
|
|
|
|
|
|
|
|
// Passing a function to NodeServer.execute will define that function
|
|
|
|
// in node. It can be called in a later execute command.
|
|
|
|
let isFunction = function(obj) {
|
|
|
|
return !!(obj && obj.constructor && obj.call && obj.apply);
|
|
|
|
};
|
|
|
|
let payload = command;
|
|
|
|
if (isFunction(command)) {
|
|
|
|
payload = `${command.name} = ${command.toString()};`;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
req.onload = () => {
|
|
|
|
let x = null;
|
|
|
|
|
|
|
|
if (req.statusText != "OK") {
|
|
|
|
reject(`XHR request failed: ${req.statusText}`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
x = JSON.parse(req.responseText);
|
|
|
|
} catch (e) {
|
|
|
|
reject(`Failed to parse ${req.responseText} - ${e}`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (x.error) {
|
|
|
|
let e = new Error(x.error, "", 0);
|
|
|
|
e.stack = x.errorStack;
|
|
|
|
reject(e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
resolve(x.result);
|
|
|
|
};
|
|
|
|
req.onerror = e => {
|
|
|
|
reject(e);
|
|
|
|
};
|
|
|
|
|
|
|
|
req.send(payload.toString());
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-29 07:41:54 +00:00
|
|
|
//
|
|
|
|
// RFC 2396 section 3.2.2:
|
|
|
|
//
|
|
|
|
// host = hostname | IPv4address
|
|
|
|
// hostname = *( domainlabel "." ) toplabel [ "." ]
|
|
|
|
// domainlabel = alphanum | alphanum *( alphanum | "-" ) alphanum
|
|
|
|
// toplabel = alpha | alpha *( alphanum | "-" ) alphanum
|
|
|
|
// IPv4address = 1*digit "." 1*digit "." 1*digit "." 1*digit
|
|
|
|
//
|
|
|
|
|
2022-06-02 04:42:21 +00:00
|
|
|
const HOST_REGEX = new RegExp(
|
|
|
|
"^(?:" +
|
|
|
|
// *( domainlabel "." )
|
|
|
|
"(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)*" +
|
|
|
|
// toplabel
|
|
|
|
"[a-z](?:[a-z0-9-]*[a-z0-9])?" +
|
|
|
|
"|" +
|
|
|
|
// IPv4 address
|
|
|
|
"\\d+\\.\\d+\\.\\d+\\.\\d+" +
|
|
|
|
")$",
|
|
|
|
"i"
|
|
|
|
);
|
2015-10-29 07:41:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents the identity of a server. An identity consists of a set of
|
|
|
|
* (scheme, host, port) tuples denoted as locations (allowing a single server to
|
|
|
|
* serve multiple sites or to be used behind both HTTP and HTTPS proxies for any
|
|
|
|
* host/port). Any incoming request must be to one of these locations, or it
|
|
|
|
* will be rejected with an HTTP 400 error. One location, denoted as the
|
|
|
|
* primary location, is the location assigned in contexts where a location
|
|
|
|
* cannot otherwise be endogenously derived, such as for HTTP/1.0 requests.
|
|
|
|
*
|
|
|
|
* A single identity may contain at most one location per unique host/port pair;
|
|
|
|
* other than that, no restrictions are placed upon what locations may
|
|
|
|
* constitute an identity.
|
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
function ServerIdentity() {
|
2015-10-29 07:41:54 +00:00
|
|
|
/** The scheme of the primary location. */
|
|
|
|
this._primaryScheme = "http";
|
|
|
|
|
|
|
|
/** The hostname of the primary location. */
|
2022-06-02 04:42:21 +00:00
|
|
|
this._primaryHost = "127.0.0.1";
|
2015-10-29 07:41:54 +00:00
|
|
|
|
|
|
|
/** The port number of the primary location. */
|
|
|
|
this._primaryPort = -1;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The current port number for the corresponding server, stored so that a new
|
|
|
|
* primary location can always be set if the current one is removed.
|
|
|
|
*/
|
|
|
|
this._defaultPort = -1;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Maps hosts to maps of ports to schemes, e.g. the following would represent
|
|
|
|
* https://example.com:789/ and http://example.org/:
|
|
|
|
*
|
|
|
|
* {
|
|
|
|
* "xexample.com": { 789: "https" },
|
|
|
|
* "xexample.org": { 80: "http" }
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* Note the "x" prefix on hostnames, which prevents collisions with special
|
|
|
|
* JS names like "prototype".
|
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
this._locations = { xlocalhost: {} };
|
2015-10-29 07:41:54 +00:00
|
|
|
}
|
2022-06-02 04:42:21 +00:00
|
|
|
ServerIdentity.prototype = {
|
2015-10-29 07:41:54 +00:00
|
|
|
// NSIHTTPSERVERIDENTITY
|
|
|
|
|
|
|
|
//
|
|
|
|
// see nsIHttpServerIdentity.primaryScheme
|
|
|
|
//
|
2022-06-02 04:42:21 +00:00
|
|
|
get primaryScheme() {
|
|
|
|
if (this._primaryPort === -1) {
|
|
|
|
throw Components.Exception("", Cr.NS_ERROR_NOT_INITIALIZED);
|
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
return this._primaryScheme;
|
|
|
|
},
|
|
|
|
|
|
|
|
//
|
|
|
|
// see nsIHttpServerIdentity.primaryHost
|
|
|
|
//
|
2022-06-02 04:42:21 +00:00
|
|
|
get primaryHost() {
|
|
|
|
if (this._primaryPort === -1) {
|
|
|
|
throw Components.Exception("", Cr.NS_ERROR_NOT_INITIALIZED);
|
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
return this._primaryHost;
|
|
|
|
},
|
|
|
|
|
|
|
|
//
|
|
|
|
// see nsIHttpServerIdentity.primaryPort
|
|
|
|
//
|
2022-06-02 04:42:21 +00:00
|
|
|
get primaryPort() {
|
|
|
|
if (this._primaryPort === -1) {
|
|
|
|
throw Components.Exception("", Cr.NS_ERROR_NOT_INITIALIZED);
|
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
return this._primaryPort;
|
|
|
|
},
|
|
|
|
|
|
|
|
//
|
|
|
|
// see nsIHttpServerIdentity.add
|
|
|
|
//
|
2022-06-02 04:42:21 +00:00
|
|
|
add(scheme, host, port) {
|
2015-10-29 07:41:54 +00:00
|
|
|
this._validate(scheme, host, port);
|
|
|
|
|
|
|
|
var entry = this._locations["x" + host];
|
2022-06-02 04:42:21 +00:00
|
|
|
if (!entry) {
|
2015-10-29 07:41:54 +00:00
|
|
|
this._locations["x" + host] = entry = {};
|
2022-06-02 04:42:21 +00:00
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
|
|
|
|
entry[port] = scheme;
|
|
|
|
},
|
|
|
|
|
|
|
|
//
|
|
|
|
// see nsIHttpServerIdentity.remove
|
|
|
|
//
|
2022-06-02 04:42:21 +00:00
|
|
|
remove(scheme, host, port) {
|
2015-10-29 07:41:54 +00:00
|
|
|
this._validate(scheme, host, port);
|
|
|
|
|
|
|
|
var entry = this._locations["x" + host];
|
2022-06-02 04:42:21 +00:00
|
|
|
if (!entry) {
|
2015-10-29 07:41:54 +00:00
|
|
|
return false;
|
2022-06-02 04:42:21 +00:00
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
|
|
|
|
var present = port in entry;
|
|
|
|
delete entry[port];
|
|
|
|
|
2022-06-02 04:42:21 +00:00
|
|
|
if (
|
|
|
|
this._primaryScheme == scheme &&
|
|
|
|
this._primaryHost == host &&
|
|
|
|
this._primaryPort == port &&
|
|
|
|
this._defaultPort !== -1
|
|
|
|
) {
|
2015-10-29 07:41:54 +00:00
|
|
|
// Always keep at least one identity in existence at any time, unless
|
|
|
|
// we're in the process of shutting down (the last condition above).
|
|
|
|
this._primaryPort = -1;
|
|
|
|
this._initialize(this._defaultPort, host, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
return present;
|
|
|
|
},
|
|
|
|
|
|
|
|
//
|
|
|
|
// see nsIHttpServerIdentity.has
|
|
|
|
//
|
2022-06-02 04:42:21 +00:00
|
|
|
has(scheme, host, port) {
|
2015-10-29 07:41:54 +00:00
|
|
|
this._validate(scheme, host, port);
|
|
|
|
|
2022-06-02 04:42:21 +00:00
|
|
|
return (
|
|
|
|
"x" + host in this._locations &&
|
|
|
|
scheme === this._locations["x" + host][port]
|
|
|
|
);
|
2015-10-29 07:41:54 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
//
|
|
|
|
// see nsIHttpServerIdentity.has
|
|
|
|
//
|
2022-06-02 04:42:21 +00:00
|
|
|
getScheme(host, port) {
|
2015-10-29 07:41:54 +00:00
|
|
|
this._validate("http", host, port);
|
|
|
|
|
|
|
|
var entry = this._locations["x" + host];
|
2022-06-02 04:42:21 +00:00
|
|
|
if (!entry) {
|
2015-10-29 07:41:54 +00:00
|
|
|
return "";
|
2022-06-02 04:42:21 +00:00
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
|
|
|
|
return entry[port] || "";
|
|
|
|
},
|
|
|
|
|
|
|
|
//
|
|
|
|
// see nsIHttpServerIdentity.setPrimary
|
|
|
|
//
|
2022-06-02 04:42:21 +00:00
|
|
|
setPrimary(scheme, host, port) {
|
2015-10-29 07:41:54 +00:00
|
|
|
this._validate(scheme, host, port);
|
|
|
|
|
|
|
|
this.add(scheme, host, port);
|
|
|
|
|
|
|
|
this._primaryScheme = scheme;
|
|
|
|
this._primaryHost = host;
|
|
|
|
this._primaryPort = port;
|
|
|
|
},
|
|
|
|
|
|
|
|
// NSISUPPORTS
|
|
|
|
|
|
|
|
//
|
|
|
|
// see nsISupports.QueryInterface
|
|
|
|
//
|
2022-06-02 04:42:21 +00:00
|
|
|
QueryInterface: ChromeUtils.generateQI(["nsIHttpServerIdentity"]),
|
2015-10-29 07:41:54 +00:00
|
|
|
|
|
|
|
// PRIVATE IMPLEMENTATION
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes the primary name for the corresponding server, based on the
|
|
|
|
* provided port number.
|
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
_initialize(port, host, addSecondaryDefault) {
|
2015-10-29 07:41:54 +00:00
|
|
|
this._host = host;
|
2022-06-02 04:42:21 +00:00
|
|
|
if (this._primaryPort !== -1) {
|
2015-10-29 07:41:54 +00:00
|
|
|
this.add("http", host, port);
|
2022-06-02 04:42:21 +00:00
|
|
|
} else {
|
2015-10-29 07:41:54 +00:00
|
|
|
this.setPrimary("http", "localhost", port);
|
2022-06-02 04:42:21 +00:00
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
this._defaultPort = port;
|
|
|
|
|
|
|
|
// Only add this if we're being called at server startup
|
2022-06-02 04:42:21 +00:00
|
|
|
if (addSecondaryDefault && host != "127.0.0.1") {
|
|
|
|
if (host.includes(":")) {
|
|
|
|
this.add("http", "[::1]", port);
|
|
|
|
} else {
|
|
|
|
this.add("http", "127.0.0.1", port);
|
|
|
|
}
|
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called at server shutdown time, unsets the primary location only if it was
|
|
|
|
* the default-assigned location and removes the default location from the
|
|
|
|
* set of locations used.
|
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
_teardown() {
|
2015-10-29 07:41:54 +00:00
|
|
|
if (this._host != "127.0.0.1") {
|
|
|
|
// Not the default primary location, nothing special to do here
|
|
|
|
this.remove("http", "127.0.0.1", this._defaultPort);
|
|
|
|
}
|
2022-06-02 04:42:21 +00:00
|
|
|
|
2015-10-29 07:41:54 +00:00
|
|
|
// This is a *very* tricky bit of reasoning here; make absolutely sure the
|
|
|
|
// tests for this code pass before you commit changes to it.
|
2022-06-02 04:42:21 +00:00
|
|
|
if (
|
|
|
|
this._primaryScheme == "http" &&
|
|
|
|
this._primaryHost == this._host &&
|
|
|
|
this._primaryPort == this._defaultPort
|
|
|
|
) {
|
2015-10-29 07:41:54 +00:00
|
|
|
// Make sure we don't trigger the readding logic in .remove(), then remove
|
|
|
|
// the default location.
|
|
|
|
var port = this._defaultPort;
|
|
|
|
this._defaultPort = -1;
|
|
|
|
this.remove("http", this._host, port);
|
|
|
|
|
|
|
|
// Ensure a server start triggers the setPrimary() path in ._initialize()
|
|
|
|
this._primaryPort = -1;
|
2022-06-02 04:42:21 +00:00
|
|
|
} else {
|
2015-10-29 07:41:54 +00:00
|
|
|
// No reason not to remove directly as it's not our primary location
|
|
|
|
this.remove("http", this._host, this._defaultPort);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Ensures scheme, host, and port are all valid with respect to RFC 2396.
|
|
|
|
*
|
|
|
|
* @throws NS_ERROR_ILLEGAL_VALUE
|
|
|
|
* if any argument doesn't match the corresponding production
|
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
_validate(scheme, host, port) {
|
|
|
|
if (scheme !== "http" && scheme !== "https") {
|
2015-10-29 07:41:54 +00:00
|
|
|
dumpn("*** server only supports http/https schemes: '" + scheme + "'");
|
|
|
|
dumpStack();
|
2022-06-02 04:42:21 +00:00
|
|
|
throw Components.Exception("", Cr.NS_ERROR_ILLEGAL_VALUE);
|
2015-10-29 07:41:54 +00:00
|
|
|
}
|
2022-06-02 04:42:21 +00:00
|
|
|
if (!HOST_REGEX.test(host) && host != "[::1]") {
|
2015-10-29 07:41:54 +00:00
|
|
|
dumpn("*** unexpected host: '" + host + "'");
|
2022-06-02 04:42:21 +00:00
|
|
|
throw Components.Exception("", Cr.NS_ERROR_ILLEGAL_VALUE);
|
2015-10-29 07:41:54 +00:00
|
|
|
}
|
2022-06-02 04:42:21 +00:00
|
|
|
if (port < 0 || port > 65535) {
|
2015-10-29 07:41:54 +00:00
|
|
|
dumpn("*** unexpected port: '" + port + "'");
|
2022-06-02 04:42:21 +00:00
|
|
|
throw Components.Exception("", Cr.NS_ERROR_ILLEGAL_VALUE);
|
2015-10-29 07:41:54 +00:00
|
|
|
}
|
2022-06-02 04:42:21 +00:00
|
|
|
},
|
2015-10-29 07:41:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents a connection to the server (and possibly in the future the thread
|
|
|
|
* on which the connection is processed).
|
|
|
|
*
|
|
|
|
* @param input : nsIInputStream
|
|
|
|
* stream from which incoming data on the connection is read
|
|
|
|
* @param output : nsIOutputStream
|
|
|
|
* stream to write data out the connection
|
|
|
|
* @param server : nsHttpServer
|
|
|
|
* the server handling the connection
|
|
|
|
* @param port : int
|
|
|
|
* the port on which the server is running
|
|
|
|
* @param outgoingPort : int
|
|
|
|
* the outgoing port used by this connection
|
|
|
|
* @param number : uint
|
|
|
|
* a serial number used to uniquely identify this connection
|
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
function Connection(
|
|
|
|
input,
|
|
|
|
output,
|
|
|
|
server,
|
|
|
|
port,
|
|
|
|
outgoingPort,
|
|
|
|
number,
|
|
|
|
transport
|
|
|
|
) {
|
2015-10-29 07:41:54 +00:00
|
|
|
dumpn("*** opening new connection " + number + " on port " + outgoingPort);
|
|
|
|
|
|
|
|
/** Stream of incoming data. */
|
|
|
|
this.input = input;
|
|
|
|
|
|
|
|
/** Stream for outgoing data. */
|
|
|
|
this.output = output;
|
|
|
|
|
|
|
|
/** The server associated with this request. */
|
|
|
|
this.server = server;
|
|
|
|
|
|
|
|
/** The port on which the server is running. */
|
|
|
|
this.port = port;
|
|
|
|
|
|
|
|
/** The outgoing poort used by this connection. */
|
|
|
|
this._outgoingPort = outgoingPort;
|
|
|
|
|
|
|
|
/** The serial number of this connection. */
|
|
|
|
this.number = number;
|
|
|
|
|
2022-06-02 04:42:21 +00:00
|
|
|
/** Reference to the underlying transport. */
|
|
|
|
this.transport = transport;
|
|
|
|
|
2015-10-29 07:41:54 +00:00
|
|
|
/**
|
|
|
|
* The request for which a response is being generated, null if the
|
|
|
|
* incoming request has not been fully received or if it had errors.
|
|
|
|
*/
|
|
|
|
this.request = null;
|
|
|
|
|
|
|
|
/** This allows a connection to disambiguate between a peer initiating a
|
|
|
|
* close and the socket being forced closed on shutdown.
|
|
|
|
*/
|
|
|
|
this._closed = false;
|
|
|
|
|
|
|
|
/** State variable for debugging. */
|
|
|
|
this._processed = false;
|
|
|
|
|
|
|
|
/** whether or not 1st line of request has been received */
|
2022-06-02 04:42:21 +00:00
|
|
|
this._requestStarted = false;
|
2015-10-29 07:41:54 +00:00
|
|
|
}
|
2022-06-02 04:42:21 +00:00
|
|
|
Connection.prototype = {
|
2015-10-29 07:41:54 +00:00
|
|
|
/** Closes this connection's input/output streams. */
|
2022-06-02 04:42:21 +00:00
|
|
|
close() {
|
|
|
|
if (this._closed) {
|
|
|
|
return;
|
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
|
2022-06-02 04:42:21 +00:00
|
|
|
dumpn(
|
|
|
|
"*** closing connection " + this.number + " on port " + this._outgoingPort
|
|
|
|
);
|
2015-10-29 07:41:54 +00:00
|
|
|
|
|
|
|
this.input.close();
|
|
|
|
this.output.close();
|
|
|
|
this._closed = true;
|
|
|
|
|
|
|
|
var server = this.server;
|
|
|
|
server._connectionClosed(this);
|
|
|
|
|
|
|
|
// If an error triggered a server shutdown, act on it now
|
2022-06-02 04:42:21 +00:00
|
|
|
if (server._doQuit) {
|
|
|
|
server.stop(function() {
|
|
|
|
/* not like we can do anything better */
|
|
|
|
});
|
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initiates processing of this connection, using the data in the given
|
|
|
|
* request.
|
|
|
|
*
|
|
|
|
* @param request : Request
|
|
|
|
* the request which should be processed
|
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
process(request) {
|
2015-10-29 07:41:54 +00:00
|
|
|
NS_ASSERT(!this._closed && !this._processed);
|
|
|
|
|
|
|
|
this._processed = true;
|
|
|
|
|
|
|
|
this.request = request;
|
|
|
|
this.server._handler.handleResponse(this);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initiates processing of this connection, generating a response with the
|
|
|
|
* given HTTP error code.
|
|
|
|
*
|
|
|
|
* @param code : uint
|
|
|
|
* an HTTP code, so in the range [0, 1000)
|
|
|
|
* @param request : Request
|
|
|
|
* incomplete data about the incoming request (since there were errors
|
|
|
|
* during its processing
|
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
processError(code, request) {
|
2015-10-29 07:41:54 +00:00
|
|
|
NS_ASSERT(!this._closed && !this._processed);
|
|
|
|
|
|
|
|
this._processed = true;
|
|
|
|
this.request = request;
|
|
|
|
this.server._handler.handleError(code, this);
|
|
|
|
},
|
|
|
|
|
|
|
|
/** Converts this to a string for debugging purposes. */
|
2022-06-02 04:42:21 +00:00
|
|
|
toString() {
|
|
|
|
return (
|
|
|
|
"<Connection(" +
|
|
|
|
this.number +
|
|
|
|
(this.request ? ", " + this.request.path : "") +
|
|
|
|
"): " +
|
|
|
|
(this._closed ? "closed" : "open") +
|
|
|
|
">"
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
|
|
|
requestStarted() {
|
2015-10-29 07:41:54 +00:00
|
|
|
this._requestStarted = true;
|
2022-06-02 04:42:21 +00:00
|
|
|
},
|
2015-10-29 07:41:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/** Returns an array of count bytes from the given input stream. */
|
2022-06-02 04:42:21 +00:00
|
|
|
function readBytes(inputStream, count) {
|
2015-10-29 07:41:54 +00:00
|
|
|
return new BinaryInputStream(inputStream).readByteArray(count);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Request reader processing states; see RequestReader for details. */
|
|
|
|
const READER_IN_REQUEST_LINE = 0;
|
2022-06-02 04:42:21 +00:00
|
|
|
const READER_IN_HEADERS = 1;
|
|
|
|
const READER_IN_BODY = 2;
|
|
|
|
const READER_FINISHED = 3;
|
2015-10-29 07:41:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Reads incoming request data asynchronously, does any necessary preprocessing,
|
|
|
|
* and forwards it to the request handler. Processing occurs in three states:
|
|
|
|
*
|
|
|
|
* READER_IN_REQUEST_LINE Reading the request's status line
|
|
|
|
* READER_IN_HEADERS Reading headers in the request
|
|
|
|
* READER_IN_BODY Reading the body of the request
|
|
|
|
* READER_FINISHED Entire request has been read and processed
|
|
|
|
*
|
|
|
|
* During the first two stages, initial metadata about the request is gathered
|
|
|
|
* into a Request object. Once the status line and headers have been processed,
|
|
|
|
* we start processing the body of the request into the Request. Finally, when
|
|
|
|
* the entire body has been read, we create a Response and hand it off to the
|
|
|
|
* ServerHandler to be given to the appropriate request handler.
|
|
|
|
*
|
|
|
|
* @param connection : Connection
|
|
|
|
* the connection for the request being read
|
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
function RequestReader(connection) {
|
2015-10-29 07:41:54 +00:00
|
|
|
/** Connection metadata for this request. */
|
|
|
|
this._connection = connection;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A container providing line-by-line access to the raw bytes that make up the
|
|
|
|
* data which has been read from the connection but has not yet been acted
|
|
|
|
* upon (by passing it to the request handler or by extracting request
|
|
|
|
* metadata from it).
|
|
|
|
*/
|
|
|
|
this._data = new LineData();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The amount of data remaining to be read from the body of this request.
|
|
|
|
* After all headers in the request have been read this is the value in the
|
|
|
|
* Content-Length header, but as the body is read its value decreases to zero.
|
|
|
|
*/
|
|
|
|
this._contentLength = 0;
|
|
|
|
|
|
|
|
/** The current state of parsing the incoming request. */
|
|
|
|
this._state = READER_IN_REQUEST_LINE;
|
|
|
|
|
|
|
|
/** Metadata constructed from the incoming request for the request handler. */
|
|
|
|
this._metadata = new Request(connection.port);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used to preserve state if we run out of line data midway through a
|
|
|
|
* multi-line header. _lastHeaderName stores the name of the header, while
|
|
|
|
* _lastHeaderValue stores the value we've seen so far for the header.
|
|
|
|
*
|
|
|
|
* These fields are always either both undefined or both strings.
|
|
|
|
*/
|
|
|
|
this._lastHeaderName = this._lastHeaderValue = undefined;
|
|
|
|
}
|
2022-06-02 04:42:21 +00:00
|
|
|
RequestReader.prototype = {
|
2015-10-29 07:41:54 +00:00
|
|
|
// NSIINPUTSTREAMCALLBACK
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called when more data from the incoming request is available. This method
|
|
|
|
* then reads the available data from input and deals with that data as
|
|
|
|
* necessary, depending upon the syntax of already-downloaded data.
|
|
|
|
*
|
|
|
|
* @param input : nsIAsyncInputStream
|
|
|
|
* the stream of incoming data from the connection
|
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
onInputStreamReady(input) {
|
|
|
|
dumpn(
|
|
|
|
"*** onInputStreamReady(input=" +
|
|
|
|
input +
|
|
|
|
") on thread " +
|
|
|
|
gThreadManager.currentThread +
|
|
|
|
" (main is " +
|
|
|
|
gThreadManager.mainThread +
|
|
|
|
")"
|
|
|
|
);
|
2015-10-29 07:41:54 +00:00
|
|
|
dumpn("*** this._state == " + this._state);
|
|
|
|
|
|
|
|
// Handle cases where we get more data after a request error has been
|
|
|
|
// discovered but *before* we can close the connection.
|
|
|
|
var data = this._data;
|
2022-06-02 04:42:21 +00:00
|
|
|
if (!data) {
|
2015-10-29 07:41:54 +00:00
|
|
|
return;
|
2022-06-02 04:42:21 +00:00
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
|
2022-06-02 04:42:21 +00:00
|
|
|
try {
|
2015-10-29 07:41:54 +00:00
|
|
|
data.appendBytes(readBytes(input, input.available()));
|
2022-06-02 04:42:21 +00:00
|
|
|
} catch (e) {
|
|
|
|
if (streamClosed(e)) {
|
|
|
|
dumpn(
|
|
|
|
"*** WARNING: unexpected error when reading from socket; will " +
|
|
|
|
"be treated as if the input stream had been closed"
|
|
|
|
);
|
2015-10-29 07:41:54 +00:00
|
|
|
dumpn("*** WARNING: actual error was: " + e);
|
|
|
|
}
|
|
|
|
|
|
|
|
// We've lost a race -- input has been closed, but we're still expecting
|
|
|
|
// to read more data. available() will throw in this case, and since
|
|
|
|
// we're dead in the water now, destroy the connection.
|
2022-06-02 04:42:21 +00:00
|
|
|
dumpn(
|
|
|
|
"*** onInputStreamReady called on a closed input, destroying " +
|
|
|
|
"connection"
|
|
|
|
);
|
2015-10-29 07:41:54 +00:00
|
|
|
this._connection.close();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-02 04:42:21 +00:00
|
|
|
switch (this._state) {
|
2015-10-29 07:41:54 +00:00
|
|
|
default:
|
|
|
|
NS_ASSERT(false, "invalid state: " + this._state);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case READER_IN_REQUEST_LINE:
|
2022-06-02 04:42:21 +00:00
|
|
|
if (!this._processRequestLine()) {
|
2015-10-29 07:41:54 +00:00
|
|
|
break;
|
2022-06-02 04:42:21 +00:00
|
|
|
}
|
|
|
|
/* fall through */
|
2015-10-29 07:41:54 +00:00
|
|
|
|
|
|
|
case READER_IN_HEADERS:
|
2022-06-02 04:42:21 +00:00
|
|
|
if (!this._processHeaders()) {
|
2015-10-29 07:41:54 +00:00
|
|
|
break;
|
2022-06-02 04:42:21 +00:00
|
|
|
}
|
|
|
|
/* fall through */
|
2015-10-29 07:41:54 +00:00
|
|
|
|
|
|
|
case READER_IN_BODY:
|
|
|
|
this._processBody();
|
|
|
|
}
|
|
|
|
|
2022-06-02 04:42:21 +00:00
|
|
|
if (this._state != READER_FINISHED) {
|
2015-10-29 07:41:54 +00:00
|
|
|
input.asyncWait(this, 0, 0, gThreadManager.currentThread);
|
2022-06-02 04:42:21 +00:00
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
//
|
|
|
|
// see nsISupports.QueryInterface
|
|
|
|
//
|
2022-06-02 04:42:21 +00:00
|
|
|
QueryInterface: ChromeUtils.generateQI(["nsIInputStreamCallback"]),
|
2015-10-29 07:41:54 +00:00
|
|
|
|
|
|
|
// PRIVATE API
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Processes unprocessed, downloaded data as a request line.
|
|
|
|
*
|
|
|
|
* @returns boolean
|
|
|
|
* true iff the request line has been fully processed
|
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
_processRequestLine() {
|
2015-10-29 07:41:54 +00:00
|
|
|
NS_ASSERT(this._state == READER_IN_REQUEST_LINE);
|
|
|
|
|
|
|
|
// Servers SHOULD ignore any empty line(s) received where a Request-Line
|
|
|
|
// is expected (section 4.1).
|
|
|
|
var data = this._data;
|
|
|
|
var line = {};
|
|
|
|
var readSuccess;
|
2022-06-02 04:42:21 +00:00
|
|
|
while ((readSuccess = data.readLine(line)) && line.value == "") {
|
2015-10-29 07:41:54 +00:00
|
|
|
dumpn("*** ignoring beginning blank line...");
|
2022-06-02 04:42:21 +00:00
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
|
|
|
|
// if we don't have a full line, wait until we do
|
2022-06-02 04:42:21 +00:00
|
|
|
if (!readSuccess) {
|
2015-10-29 07:41:54 +00:00
|
|
|
return false;
|
2022-06-02 04:42:21 +00:00
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
|
|
|
|
// we have the first non-blank line
|
2022-06-02 04:42:21 +00:00
|
|
|
try {
|
2015-10-29 07:41:54 +00:00
|
|
|
this._parseRequestLine(line.value);
|
|
|
|
this._state = READER_IN_HEADERS;
|
|
|
|
this._connection.requestStarted();
|
|
|
|
return true;
|
2022-06-02 04:42:21 +00:00
|
|
|
} catch (e) {
|
2015-10-29 07:41:54 +00:00
|
|
|
this._handleError(e);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Processes stored data, assuming it is either at the beginning or in
|
|
|
|
* the middle of processing request headers.
|
|
|
|
*
|
|
|
|
* @returns boolean
|
|
|
|
* true iff header data in the request has been fully processed
|
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
_processHeaders() {
|
2015-10-29 07:41:54 +00:00
|
|
|
NS_ASSERT(this._state == READER_IN_HEADERS);
|
|
|
|
|
|
|
|
// XXX things to fix here:
|
|
|
|
//
|
|
|
|
// - need to support RFC 2047-encoded non-US-ASCII characters
|
|
|
|
|
2022-06-02 04:42:21 +00:00
|
|
|
try {
|
2015-10-29 07:41:54 +00:00
|
|
|
var done = this._parseHeaders();
|
2022-06-02 04:42:21 +00:00
|
|
|
if (done) {
|
2015-10-29 07:41:54 +00:00
|
|
|
var request = this._metadata;
|
|
|
|
|
|
|
|
// XXX this is wrong for requests with transfer-encodings applied to
|
|
|
|
// them, particularly chunked (which by its nature can have no
|
|
|
|
// meaningful Content-Length header)!
|
|
|
|
this._contentLength = request.hasHeader("Content-Length")
|
2022-06-02 04:42:21 +00:00
|
|
|
? parseInt(request.getHeader("Content-Length"), 10)
|
|
|
|
: 0;
|
2015-10-29 07:41:54 +00:00
|
|
|
dumpn("_processHeaders, Content-length=" + this._contentLength);
|
|
|
|
|
|
|
|
this._state = READER_IN_BODY;
|
|
|
|
}
|
|
|
|
return done;
|
2022-06-02 04:42:21 +00:00
|
|
|
} catch (e) {
|
2015-10-29 07:41:54 +00:00
|
|
|
this._handleError(e);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Processes stored data, assuming it is either at the beginning or in
|
|
|
|
* the middle of processing the request body.
|
|
|
|
*
|
|
|
|
* @returns boolean
|
|
|
|
* true iff the request body has been fully processed
|
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
_processBody() {
|
2015-10-29 07:41:54 +00:00
|
|
|
NS_ASSERT(this._state == READER_IN_BODY);
|
|
|
|
|
|
|
|
// XXX handle chunked transfer-coding request bodies!
|
|
|
|
|
2022-06-02 04:42:21 +00:00
|
|
|
try {
|
|
|
|
if (this._contentLength > 0) {
|
2015-10-29 07:41:54 +00:00
|
|
|
var data = this._data.purge();
|
|
|
|
var count = Math.min(data.length, this._contentLength);
|
2022-06-02 04:42:21 +00:00
|
|
|
dumpn(
|
|
|
|
"*** loading data=" +
|
|
|
|
data +
|
|
|
|
" len=" +
|
|
|
|
data.length +
|
|
|
|
" excess=" +
|
|
|
|
(data.length - count)
|
|
|
|
);
|
|
|
|
data.length = count;
|
2015-10-29 07:41:54 +00:00
|
|
|
|
|
|
|
var bos = new BinaryOutputStream(this._metadata._bodyOutputStream);
|
2022-06-02 04:42:21 +00:00
|
|
|
bos.writeByteArray(data);
|
2015-10-29 07:41:54 +00:00
|
|
|
this._contentLength -= count;
|
|
|
|
}
|
|
|
|
|
|
|
|
dumpn("*** remaining body data len=" + this._contentLength);
|
2022-06-02 04:42:21 +00:00
|
|
|
if (this._contentLength == 0) {
|
2015-10-29 07:41:54 +00:00
|
|
|
this._validateRequest();
|
|
|
|
this._state = READER_FINISHED;
|
|
|
|
this._handleResponse();
|
|
|
|
return true;
|
|
|
|
}
|
2022-06-02 04:42:21 +00:00
|
|
|
|
2015-10-29 07:41:54 +00:00
|
|
|
return false;
|
2022-06-02 04:42:21 +00:00
|
|
|
} catch (e) {
|
2015-10-29 07:41:54 +00:00
|
|
|
this._handleError(e);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Does various post-header checks on the data in this request.
|
|
|
|
*
|
|
|
|
* @throws : HttpError
|
|
|
|
* if the request was malformed in some way
|
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
_validateRequest() {
|
2015-10-29 07:41:54 +00:00
|
|
|
NS_ASSERT(this._state == READER_IN_BODY);
|
|
|
|
|
|
|
|
dumpn("*** _validateRequest");
|
|
|
|
|
|
|
|
var metadata = this._metadata;
|
|
|
|
var headers = metadata._headers;
|
|
|
|
|
|
|
|
// 19.6.1.1 -- servers MUST report 400 to HTTP/1.1 requests w/o Host header
|
|
|
|
var identity = this._connection.server.identity;
|
2022-06-02 04:42:21 +00:00
|
|
|
if (metadata._httpVersion.atLeast(nsHttpVersion.HTTP_1_1)) {
|
|
|
|
if (!headers.hasHeader("Host")) {
|
2015-10-29 07:41:54 +00:00
|
|
|
dumpn("*** malformed HTTP/1.1 or greater request with no Host header!");
|
|
|
|
throw HTTP_400;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the Request-URI wasn't absolute, then we need to determine our host.
|
|
|
|
// We have to determine what scheme was used to access us based on the
|
|
|
|
// server identity data at this point, because the request just doesn't
|
|
|
|
// contain enough data on its own to do this, sadly.
|
2022-06-02 04:42:21 +00:00
|
|
|
if (!metadata._host) {
|
2015-10-29 07:41:54 +00:00
|
|
|
var host, port;
|
|
|
|
var hostPort = headers.getHeader("Host");
|
2022-06-02 04:42:21 +00:00
|
|
|
var colon = hostPort.lastIndexOf(":");
|
|
|
|
if (hostPort.lastIndexOf("]") > colon) {
|
|
|
|
colon = -1;
|
|
|
|
}
|
|
|
|
if (colon < 0) {
|
2015-10-29 07:41:54 +00:00
|
|
|
host = hostPort;
|
|
|
|
port = "";
|
2022-06-02 04:42:21 +00:00
|
|
|
} else {
|
2015-10-29 07:41:54 +00:00
|
|
|
host = hostPort.substring(0, colon);
|
|
|
|
port = hostPort.substring(colon + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// NB: We allow an empty port here because, oddly, a colon may be
|
|
|
|
// present even without a port number, e.g. "example.com:"; in this
|
|
|
|
// case the default port applies.
|
2022-06-02 04:42:21 +00:00
|
|
|
if (
|
|
|
|
(!HOST_REGEX.test(host) && host != "[::1]") ||
|
|
|
|
!/^\d*$/.test(port)
|
|
|
|
) {
|
|
|
|
dumpn(
|
|
|
|
"*** malformed hostname (" +
|
|
|
|
hostPort +
|
|
|
|
") in Host " +
|
|
|
|
"header, 400 time"
|
|
|
|
);
|
2015-10-29 07:41:54 +00:00
|
|
|
throw HTTP_400;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we're not given a port, we're stuck, because we don't know what
|
|
|
|
// scheme to use to look up the correct port here, in general. Since
|
|
|
|
// the HTTPS case requires a tunnel/proxy and thus requires that the
|
|
|
|
// requested URI be absolute (and thus contain the necessary
|
|
|
|
// information), let's assume HTTP will prevail and use that.
|
|
|
|
port = +port || 80;
|
|
|
|
|
|
|
|
var scheme = identity.getScheme(host, port);
|
2022-06-02 04:42:21 +00:00
|
|
|
if (!scheme) {
|
|
|
|
dumpn(
|
|
|
|
"*** unrecognized hostname (" +
|
|
|
|
hostPort +
|
|
|
|
") in Host " +
|
|
|
|
"header, 400 time"
|
|
|
|
);
|
2015-10-29 07:41:54 +00:00
|
|
|
throw HTTP_400;
|
|
|
|
}
|
|
|
|
|
|
|
|
metadata._scheme = scheme;
|
|
|
|
metadata._host = host;
|
|
|
|
metadata._port = port;
|
|
|
|
}
|
2022-06-02 04:42:21 +00:00
|
|
|
} else {
|
|
|
|
NS_ASSERT(
|
|
|
|
metadata._host === undefined,
|
|
|
|
"HTTP/1.0 doesn't allow absolute paths in the request line!"
|
|
|
|
);
|
2015-10-29 07:41:54 +00:00
|
|
|
|
|
|
|
metadata._scheme = identity.primaryScheme;
|
|
|
|
metadata._host = identity.primaryHost;
|
|
|
|
metadata._port = identity.primaryPort;
|
|
|
|
}
|
|
|
|
|
2022-06-02 04:42:21 +00:00
|
|
|
NS_ASSERT(
|
|
|
|
identity.has(metadata._scheme, metadata._host, metadata._port),
|
|
|
|
"must have a location we recognize by now!"
|
|
|
|
);
|
2015-10-29 07:41:54 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles responses in case of error, either in the server or in the request.
|
|
|
|
*
|
|
|
|
* @param e
|
|
|
|
* the specific error encountered, which is an HttpError in the case where
|
|
|
|
* the request is in some way invalid or cannot be fulfilled; if this isn't
|
|
|
|
* an HttpError we're going to be paranoid and shut down, because that
|
|
|
|
* shouldn't happen, ever
|
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
_handleError(e) {
|
2015-10-29 07:41:54 +00:00
|
|
|
// Don't fall back into normal processing!
|
|
|
|
this._state = READER_FINISHED;
|
|
|
|
|
|
|
|
var server = this._connection.server;
|
2022-06-02 04:42:21 +00:00
|
|
|
if (e instanceof HttpError) {
|
2015-10-29 07:41:54 +00:00
|
|
|
var code = e.code;
|
2022-06-02 04:42:21 +00:00
|
|
|
} else {
|
|
|
|
dumpn(
|
|
|
|
"!!! UNEXPECTED ERROR: " +
|
|
|
|
e +
|
|
|
|
(e.lineNumber ? ", line " + e.lineNumber : "")
|
|
|
|
);
|
2015-10-29 07:41:54 +00:00
|
|
|
|
|
|
|
// no idea what happened -- be paranoid and shut down
|
|
|
|
code = 500;
|
|
|
|
server._requestQuit();
|
|
|
|
}
|
|
|
|
|
|
|
|
// make attempted reuse of data an error
|
|
|
|
this._data = null;
|
|
|
|
|
|
|
|
this._connection.processError(code, this._metadata);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Now that we've read the request line and headers, we can actually hand off
|
|
|
|
* the request to be handled.
|
|
|
|
*
|
|
|
|
* This method is called once per request, after the request line and all
|
|
|
|
* headers and the body, if any, have been received.
|
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
_handleResponse() {
|
2015-10-29 07:41:54 +00:00
|
|
|
NS_ASSERT(this._state == READER_FINISHED);
|
|
|
|
|
|
|
|
// We don't need the line-based data any more, so make attempted reuse an
|
|
|
|
// error.
|
|
|
|
this._data = null;
|
|
|
|
|
|
|
|
this._connection.process(this._metadata);
|
|
|
|
},
|
|
|
|
|
|
|
|
// PARSING
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parses the request line for the HTTP request associated with this.
|
|
|
|
*
|
|
|
|
* @param line : string
|
|
|
|
* the request line
|
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
_parseRequestLine(line) {
|
2015-10-29 07:41:54 +00:00
|
|
|
NS_ASSERT(this._state == READER_IN_REQUEST_LINE);
|
|
|
|
|
|
|
|
dumpn("*** _parseRequestLine('" + line + "')");
|
|
|
|
|
|
|
|
var metadata = this._metadata;
|
|
|
|
|
|
|
|
// clients and servers SHOULD accept any amount of SP or HT characters
|
|
|
|
// between fields, even though only a single SP is required (section 19.3)
|
|
|
|
var request = line.split(/[ \t]+/);
|
2022-06-02 04:42:21 +00:00
|
|
|
if (!request || request.length != 3) {
|
2015-10-29 07:41:54 +00:00
|
|
|
dumpn("*** No request in line");
|
|
|
|
throw HTTP_400;
|
|
|
|
}
|
|
|
|
|
|
|
|
metadata._method = request[0];
|
|
|
|
|
|
|
|
// get the HTTP version
|
|
|
|
var ver = request[2];
|
|
|
|
var match = ver.match(/^HTTP\/(\d+\.\d+)$/);
|
2022-06-02 04:42:21 +00:00
|
|
|
if (!match) {
|
2015-10-29 07:41:54 +00:00
|
|
|
dumpn("*** No HTTP version in line");
|
|
|
|
throw HTTP_400;
|
|
|
|
}
|
|
|
|
|
|
|
|
// determine HTTP version
|
2022-06-02 04:42:21 +00:00
|
|
|
try {
|
2015-10-29 07:41:54 +00:00
|
|
|
metadata._httpVersion = new nsHttpVersion(match[1]);
|
2022-06-02 04:42:21 +00:00
|
|
|
if (!metadata._httpVersion.atLeast(nsHttpVersion.HTTP_1_0)) {
|
|
|
|
throw new Error("unsupported HTTP version");
|
|
|
|
}
|
|
|
|
} catch (e) {
|
2015-10-29 07:41:54 +00:00
|
|
|
// we support HTTP/1.0 and HTTP/1.1 only
|
|
|
|
throw HTTP_501;
|
|
|
|
}
|
|
|
|
|
|
|
|
var fullPath = request[1];
|
|
|
|
|
2022-06-02 04:42:21 +00:00
|
|
|
if (metadata._method == "CONNECT") {
|
|
|
|
metadata._path = "CONNECT";
|
|
|
|
metadata._scheme = "https";
|
|
|
|
[metadata._host, metadata._port] = fullPath.split(":");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var serverIdentity = this._connection.server.identity;
|
2015-10-29 07:41:54 +00:00
|
|
|
var scheme, host, port;
|
|
|
|
|
2022-06-02 04:42:21 +00:00
|
|
|
if (fullPath.charAt(0) != "/") {
|
2015-10-29 07:41:54 +00:00
|
|
|
// No absolute paths in the request line in HTTP prior to 1.1
|
2022-06-02 04:42:21 +00:00
|
|
|
if (!metadata._httpVersion.atLeast(nsHttpVersion.HTTP_1_1)) {
|
2015-10-29 07:41:54 +00:00
|
|
|
dumpn("*** Metadata version too low");
|
|
|
|
throw HTTP_400;
|
|
|
|
}
|
|
|
|
|
2022-06-02 04:42:21 +00:00
|
|
|
try {
|
|
|
|
var uri = Services.io.newURI(fullPath);
|
2019-08-27 09:30:04 +00:00
|
|
|
fullPath = uri.pathQueryRef;
|
2015-10-29 07:41:54 +00:00
|
|
|
scheme = uri.scheme;
|
|
|
|
host = metadata._host = uri.asciiHost;
|
|
|
|
port = uri.port;
|
2022-06-02 04:42:21 +00:00
|
|
|
if (port === -1) {
|
|
|
|
if (scheme === "http") {
|
2015-10-29 07:41:54 +00:00
|
|
|
port = 80;
|
2022-06-02 04:42:21 +00:00
|
|
|
} else if (scheme === "https") {
|
2015-10-29 07:41:54 +00:00
|
|
|
port = 443;
|
2022-06-02 04:42:21 +00:00
|
|
|
} else {
|
2015-10-29 07:41:54 +00:00
|
|
|
dumpn("*** Unknown scheme: " + scheme);
|
|
|
|
throw HTTP_400;
|
|
|
|
}
|
|
|
|
}
|
2022-06-02 04:42:21 +00:00
|
|
|
} catch (e) {
|
2015-10-29 07:41:54 +00:00
|
|
|
// If the host is not a valid host on the server, the response MUST be a
|
|
|
|
// 400 (Bad Request) error message (section 5.2). Alternately, the URI
|
|
|
|
// is malformed.
|
|
|
|
dumpn("*** Threw when dealing with URI: " + e);
|
|
|
|
throw HTTP_400;
|
|
|
|
}
|
|
|
|
|
2022-06-02 04:42:21 +00:00
|
|
|
if (
|
|
|
|
!serverIdentity.has(scheme, host, port) ||
|
|
|
|
fullPath.charAt(0) != "/"
|
|
|
|
) {
|
2015-10-29 07:41:54 +00:00
|
|
|
dumpn("*** serverIdentity unknown or path does not start with '/'");
|
|
|
|
throw HTTP_400;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var splitter = fullPath.indexOf("?");
|
2022-06-02 04:42:21 +00:00
|
|
|
if (splitter < 0) {
|
2015-10-29 07:41:54 +00:00
|
|
|
// _queryString already set in ctor
|
|
|
|
metadata._path = fullPath;
|
2022-06-02 04:42:21 +00:00
|
|
|
} else {
|
2015-10-29 07:41:54 +00:00
|
|
|
metadata._path = fullPath.substring(0, splitter);
|
|
|
|
metadata._queryString = fullPath.substring(splitter + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
metadata._scheme = scheme;
|
|
|
|
metadata._host = host;
|
|
|
|
metadata._port = port;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parses all available HTTP headers in this until the header-ending CRLFCRLF,
|
|
|
|
* adding them to the store of headers in the request.
|
|
|
|
*
|
|
|
|
* @throws
|
|
|
|
* HTTP_400 if the headers are malformed
|
|
|
|
* @returns boolean
|
|
|
|
* true if all headers have now been processed, false otherwise
|
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
_parseHeaders() {
|
2015-10-29 07:41:54 +00:00
|
|
|
NS_ASSERT(this._state == READER_IN_HEADERS);
|
|
|
|
|
|
|
|
dumpn("*** _parseHeaders");
|
|
|
|
|
|
|
|
var data = this._data;
|
|
|
|
|
|
|
|
var headers = this._metadata._headers;
|
|
|
|
var lastName = this._lastHeaderName;
|
|
|
|
var lastVal = this._lastHeaderValue;
|
|
|
|
|
|
|
|
var line = {};
|
2022-06-02 04:42:21 +00:00
|
|
|
while (true) {
|
2015-10-29 07:41:54 +00:00
|
|
|
dumpn("*** Last name: '" + lastName + "'");
|
|
|
|
dumpn("*** Last val: '" + lastVal + "'");
|
2022-06-02 04:42:21 +00:00
|
|
|
NS_ASSERT(
|
|
|
|
!((lastVal === undefined) ^ (lastName === undefined)),
|
|
|
|
lastName === undefined
|
|
|
|
? "lastVal without lastName? lastVal: '" + lastVal + "'"
|
|
|
|
: "lastName without lastVal? lastName: '" + lastName + "'"
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!data.readLine(line)) {
|
2015-10-29 07:41:54 +00:00
|
|
|
// save any data we have from the header we might still be processing
|
|
|
|
this._lastHeaderName = lastName;
|
|
|
|
this._lastHeaderValue = lastVal;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
var lineText = line.value;
|
|
|
|
dumpn("*** Line text: '" + lineText + "'");
|
|
|
|
var firstChar = lineText.charAt(0);
|
|
|
|
|
|
|
|
// blank line means end of headers
|
2022-06-02 04:42:21 +00:00
|
|
|
if (lineText == "") {
|
2015-10-29 07:41:54 +00:00
|
|
|
// we're finished with the previous header
|
2022-06-02 04:42:21 +00:00
|
|
|
if (lastName) {
|
|
|
|
try {
|
2015-10-29 07:41:54 +00:00
|
|
|
headers.setHeader(lastName, lastVal, true);
|
2022-06-02 04:42:21 +00:00
|
|
|
} catch (e) {
|
2015-10-29 07:41:54 +00:00
|
|
|
dumpn("*** setHeader threw on last header, e == " + e);
|
|
|
|
throw HTTP_400;
|
|
|
|
}
|
2022-06-02 04:42:21 +00:00
|
|
|
} else {
|
2015-10-29 07:41:54 +00:00
|
|
|
// no headers in request -- valid for HTTP/1.0 requests
|
|
|
|
}
|
|
|
|
|
|
|
|
// either way, we're done processing headers
|
|
|
|
this._state = READER_IN_BODY;
|
|
|
|
return true;
|
2022-06-02 04:42:21 +00:00
|
|
|
} else if (firstChar == " " || firstChar == "\t") {
|
2015-10-29 07:41:54 +00:00
|
|
|
// multi-line header if we've already seen a header line
|
2022-06-02 04:42:21 +00:00
|
|
|
if (!lastName) {
|
2015-10-29 07:41:54 +00:00
|
|
|
dumpn("We don't have a header to continue!");
|
|
|
|
throw HTTP_400;
|
|
|
|
}
|
|
|
|
|
|
|
|
// append this line's text to the value; starts with SP/HT, so no need
|
|
|
|
// for separating whitespace
|
|
|
|
lastVal += lineText;
|
2022-06-02 04:42:21 +00:00
|
|
|
} else {
|
2015-10-29 07:41:54 +00:00
|
|
|
// we have a new header, so set the old one (if one existed)
|
2022-06-02 04:42:21 +00:00
|
|
|
if (lastName) {
|
|
|
|
try {
|
2015-10-29 07:41:54 +00:00
|
|
|
headers.setHeader(lastName, lastVal, true);
|
2022-06-02 04:42:21 +00:00
|
|
|
} catch (e) {
|
2015-10-29 07:41:54 +00:00
|
|
|
dumpn("*** setHeader threw on a header, e == " + e);
|
|
|
|
throw HTTP_400;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var colon = lineText.indexOf(":"); // first colon must be splitter
|
2022-06-02 04:42:21 +00:00
|
|
|
if (colon < 1) {
|
2015-10-29 07:41:54 +00:00
|
|
|
dumpn("*** No colon or missing header field-name");
|
|
|
|
throw HTTP_400;
|
|
|
|
}
|
|
|
|
|
|
|
|
// set header name, value (to be set in the next loop, usually)
|
|
|
|
lastName = lineText.substring(0, colon);
|
|
|
|
lastVal = lineText.substring(colon + 1);
|
|
|
|
} // empty, continuation, start of header
|
|
|
|
} // while (true)
|
2022-06-02 04:42:21 +00:00
|
|
|
},
|
2015-10-29 07:41:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/** The character codes for CR and LF. */
|
2022-06-02 04:42:21 +00:00
|
|
|
const CR = 0x0d,
|
|
|
|
LF = 0x0a;
|
2015-10-29 07:41:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculates the number of characters before the first CRLF pair in array, or
|
|
|
|
* -1 if the array contains no CRLF pair.
|
|
|
|
*
|
|
|
|
* @param array : Array
|
|
|
|
* an array of numbers in the range [0, 256), each representing a single
|
|
|
|
* character; the first CRLF is the lowest index i where
|
|
|
|
* |array[i] == "\r".charCodeAt(0)| and |array[i+1] == "\n".charCodeAt(0)|,
|
|
|
|
* if such an |i| exists, and -1 otherwise
|
|
|
|
* @param start : uint
|
|
|
|
* start index from which to begin searching in array
|
|
|
|
* @returns int
|
|
|
|
* the index of the first CRLF if any were present, -1 otherwise
|
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
function findCRLF(array, start) {
|
|
|
|
for (var i = array.indexOf(CR, start); i >= 0; i = array.indexOf(CR, i + 1)) {
|
|
|
|
if (array[i + 1] == LF) {
|
2015-10-29 07:41:54 +00:00
|
|
|
return i;
|
2022-06-02 04:42:21 +00:00
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A container which provides line-by-line access to the arrays of bytes with
|
|
|
|
* which it is seeded.
|
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
function LineData() {
|
2015-10-29 07:41:54 +00:00
|
|
|
/** An array of queued bytes from which to get line-based characters. */
|
|
|
|
this._data = [];
|
|
|
|
|
|
|
|
/** Start index from which to search for CRLF. */
|
|
|
|
this._start = 0;
|
|
|
|
}
|
2022-06-02 04:42:21 +00:00
|
|
|
LineData.prototype = {
|
2015-10-29 07:41:54 +00:00
|
|
|
/**
|
|
|
|
* Appends the bytes in the given array to the internal data cache maintained
|
|
|
|
* by this.
|
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
appendBytes(bytes) {
|
2015-10-29 07:41:54 +00:00
|
|
|
var count = bytes.length;
|
|
|
|
var quantum = 262144; // just above half SpiderMonkey's argument-count limit
|
2022-06-02 04:42:21 +00:00
|
|
|
if (count < quantum) {
|
2015-10-29 07:41:54 +00:00
|
|
|
Array.prototype.push.apply(this._data, bytes);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Large numbers of bytes may cause Array.prototype.push to be called with
|
|
|
|
// more arguments than the JavaScript engine supports. In that case append
|
|
|
|
// bytes in fixed-size amounts until all bytes are appended.
|
2022-06-02 04:42:21 +00:00
|
|
|
for (var start = 0; start < count; start += quantum) {
|
2015-10-29 07:41:54 +00:00
|
|
|
var slice = bytes.slice(start, Math.min(start + quantum, count));
|
|
|
|
Array.prototype.push.apply(this._data, slice);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes and returns a line of data, delimited by CRLF, from this.
|
|
|
|
*
|
|
|
|
* @param out
|
|
|
|
* an object whose "value" property will be set to the first line of text
|
|
|
|
* present in this, sans CRLF, if this contains a full CRLF-delimited line
|
|
|
|
* of text; if this doesn't contain enough data, the value of the property
|
|
|
|
* is undefined
|
|
|
|
* @returns boolean
|
|
|
|
* true if a full line of data could be read from the data in this, false
|
|
|
|
* otherwise
|
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
readLine(out) {
|
2015-10-29 07:41:54 +00:00
|
|
|
var data = this._data;
|
|
|
|
var length = findCRLF(data, this._start);
|
2022-06-02 04:42:21 +00:00
|
|
|
if (length < 0) {
|
2015-10-29 07:41:54 +00:00
|
|
|
this._start = data.length;
|
|
|
|
|
|
|
|
// But if our data ends in a CR, we have to back up one, because
|
|
|
|
// the first byte in the next packet might be an LF and if we
|
|
|
|
// start looking at data.length we won't find it.
|
2022-06-02 04:42:21 +00:00
|
|
|
if (data.length > 0 && data[data.length - 1] === CR) {
|
2015-10-29 07:41:54 +00:00
|
|
|
--this._start;
|
2022-06-02 04:42:21 +00:00
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset for future lines.
|
|
|
|
this._start = 0;
|
|
|
|
|
|
|
|
//
|
|
|
|
// We have the index of the CR, so remove all the characters, including
|
|
|
|
// CRLF, from the array with splice, and convert the removed array
|
|
|
|
// (excluding the trailing CRLF characters) into the corresponding string.
|
|
|
|
//
|
|
|
|
var leading = data.splice(0, length + 2);
|
|
|
|
var quantum = 262144;
|
|
|
|
var line = "";
|
2022-06-02 04:42:21 +00:00
|
|
|
for (var start = 0; start < length; start += quantum) {
|
2015-10-29 07:41:54 +00:00
|
|
|
var slice = leading.slice(start, Math.min(start + quantum, length));
|
|
|
|
line += String.fromCharCode.apply(null, slice);
|
|
|
|
}
|
|
|
|
|
|
|
|
out.value = line;
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes the bytes currently within this and returns them in an array.
|
|
|
|
*
|
|
|
|
* @returns Array
|
|
|
|
* the bytes within this when this method is called
|
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
purge() {
|
2015-10-29 07:41:54 +00:00
|
|
|
var data = this._data;
|
|
|
|
this._data = [];
|
|
|
|
return data;
|
2022-06-02 04:42:21 +00:00
|
|
|
},
|
2015-10-29 07:41:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a request-handling function for an nsIHttpRequestHandler object.
|
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
function createHandlerFunc(handler) {
|
|
|
|
return function(metadata, response) {
|
|
|
|
handler.handle(metadata, response);
|
|
|
|
};
|
2015-10-29 07:41:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The default handler for directories; writes an HTML response containing a
|
|
|
|
* slightly-formatted directory listing.
|
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
function defaultIndexHandler(metadata, response) {
|
2015-10-29 07:41:54 +00:00
|
|
|
response.setHeader("Content-Type", "text/html;charset=utf-8", false);
|
|
|
|
|
|
|
|
var path = htmlEscape(decodeURI(metadata.path));
|
|
|
|
|
|
|
|
//
|
|
|
|
// Just do a very basic bit of directory listings -- no need for too much
|
|
|
|
// fanciness, especially since we don't have a style sheet in which we can
|
|
|
|
// stick rules (don't want to pollute the default path-space).
|
|
|
|
//
|
|
|
|
|
2022-06-02 04:42:21 +00:00
|
|
|
var body =
|
|
|
|
"<html>\
|
2015-10-29 07:41:54 +00:00
|
|
|
<head>\
|
2022-06-02 04:42:21 +00:00
|
|
|
<title>" +
|
|
|
|
path +
|
|
|
|
"</title>\
|
2015-10-29 07:41:54 +00:00
|
|
|
</head>\
|
|
|
|
<body>\
|
2022-06-02 04:42:21 +00:00
|
|
|
<h1>" +
|
|
|
|
path +
|
|
|
|
'</h1>\
|
2015-10-29 07:41:54 +00:00
|
|
|
<ol style="list-style-type: none">';
|
|
|
|
|
|
|
|
var directory = metadata.getProperty("directory");
|
|
|
|
NS_ASSERT(directory && directory.isDirectory());
|
|
|
|
|
|
|
|
var fileList = [];
|
|
|
|
var files = directory.directoryEntries;
|
2022-06-02 04:42:21 +00:00
|
|
|
while (files.hasMoreElements()) {
|
|
|
|
var f = files.nextFile;
|
|
|
|
let name = f.leafName;
|
|
|
|
if (
|
|
|
|
!f.isHidden() &&
|
|
|
|
(name.charAt(name.length - 1) != HIDDEN_CHAR ||
|
|
|
|
name.charAt(name.length - 2) == HIDDEN_CHAR)
|
|
|
|
) {
|
2015-10-29 07:41:54 +00:00
|
|
|
fileList.push(f);
|
2022-06-02 04:42:21 +00:00
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fileList.sort(fileSort);
|
|
|
|
|
2022-06-02 04:42:21 +00:00
|
|
|
for (var i = 0; i < fileList.length; i++) {
|
2015-10-29 07:41:54 +00:00
|
|
|
var file = fileList[i];
|
2022-06-02 04:42:21 +00:00
|
|
|
try {
|
|
|
|
let name = file.leafName;
|
|
|
|
if (name.charAt(name.length - 1) == HIDDEN_CHAR) {
|
2015-10-29 07:41:54 +00:00
|
|
|
name = name.substring(0, name.length - 1);
|
2022-06-02 04:42:21 +00:00
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
var sep = file.isDirectory() ? "/" : "";
|
|
|
|
|
|
|
|
// Note: using " to delimit the attribute here because encodeURIComponent
|
|
|
|
// passes through '.
|
2022-06-02 04:42:21 +00:00
|
|
|
var item =
|
|
|
|
'<li><a href="' +
|
|
|
|
encodeURIComponent(name) +
|
|
|
|
sep +
|
|
|
|
'">' +
|
|
|
|
htmlEscape(name) +
|
|
|
|
sep +
|
|
|
|
"</a></li>";
|
2015-10-29 07:41:54 +00:00
|
|
|
|
|
|
|
body += item;
|
2022-06-02 04:42:21 +00:00
|
|
|
} catch (e) {
|
|
|
|
/* some file system error, ignore the file */
|
2015-10-29 07:41:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-02 04:42:21 +00:00
|
|
|
body += " </ol>\
|
2015-10-29 07:41:54 +00:00
|
|
|
</body>\
|
2022-06-02 04:42:21 +00:00
|
|
|
</html>";
|
2015-10-29 07:41:54 +00:00
|
|
|
|
|
|
|
response.bodyOutputStream.write(body, body.length);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sorts a and b (nsIFile objects) into an aesthetically pleasing order.
|
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
function fileSort(a, b) {
|
|
|
|
var dira = a.isDirectory(),
|
|
|
|
dirb = b.isDirectory();
|
2015-10-29 07:41:54 +00:00
|
|
|
|
2022-06-02 04:42:21 +00:00
|
|
|
if (dira && !dirb) {
|
2015-10-29 07:41:54 +00:00
|
|
|
return -1;
|
2022-06-02 04:42:21 +00:00
|
|
|
}
|
|
|
|
if (dirb && !dira) {
|
2015-10-29 07:41:54 +00:00
|
|
|
return 1;
|
2022-06-02 04:42:21 +00:00
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
|
2022-06-02 04:42:21 +00:00
|
|
|
var namea = a.leafName.toLowerCase(),
|
|
|
|
nameb = b.leafName.toLowerCase();
|
2015-10-29 07:41:54 +00:00
|
|
|
return nameb > namea ? -1 : 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts an externally-provided path into an internal path for use in
|
|
|
|
* determining file mappings.
|
|
|
|
*
|
|
|
|
* @param path
|
|
|
|
* the path to convert
|
|
|
|
* @param encoded
|
|
|
|
* true if the given path should be passed through decodeURI prior to
|
|
|
|
* conversion
|
|
|
|
* @throws URIError
|
|
|
|
* if path is incorrectly encoded
|
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
function toInternalPath(path, encoded) {
|
|
|
|
if (encoded) {
|
2015-10-29 07:41:54 +00:00
|
|
|
path = decodeURI(path);
|
2022-06-02 04:42:21 +00:00
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
|
|
|
|
var comps = path.split("/");
|
2022-06-02 04:42:21 +00:00
|
|
|
for (var i = 0, sz = comps.length; i < sz; i++) {
|
2015-10-29 07:41:54 +00:00
|
|
|
var comp = comps[i];
|
2022-06-02 04:42:21 +00:00
|
|
|
if (comp.charAt(comp.length - 1) == HIDDEN_CHAR) {
|
2015-10-29 07:41:54 +00:00
|
|
|
comps[i] = comp + HIDDEN_CHAR;
|
2022-06-02 04:42:21 +00:00
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
}
|
|
|
|
return comps.join("/");
|
|
|
|
}
|
|
|
|
|
|
|
|
const PERMS_READONLY = (4 << 6) | (4 << 3) | 4;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds custom-specified headers for the given file to the given response, if
|
|
|
|
* any such headers are specified.
|
|
|
|
*
|
|
|
|
* @param file
|
|
|
|
* the file on the disk which is to be written
|
|
|
|
* @param metadata
|
|
|
|
* metadata about the incoming request
|
|
|
|
* @param response
|
|
|
|
* the Response to which any specified headers/data should be written
|
|
|
|
* @throws HTTP_500
|
|
|
|
* if an error occurred while processing custom-specified headers
|
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
function maybeAddHeadersInternal(
|
|
|
|
file,
|
|
|
|
metadata,
|
|
|
|
response,
|
|
|
|
informationalResponse
|
|
|
|
) {
|
2015-10-29 07:41:54 +00:00
|
|
|
var name = file.leafName;
|
2022-06-02 04:42:21 +00:00
|
|
|
if (name.charAt(name.length - 1) == HIDDEN_CHAR) {
|
2015-10-29 07:41:54 +00:00
|
|
|
name = name.substring(0, name.length - 1);
|
2022-06-02 04:42:21 +00:00
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
|
|
|
|
var headerFile = file.parent;
|
2022-06-02 04:42:21 +00:00
|
|
|
if (!informationalResponse) {
|
|
|
|
headerFile.append(name + HEADERS_SUFFIX);
|
|
|
|
} else {
|
|
|
|
headerFile.append(name + INFORMATIONAL_RESPONSE_SUFFIX);
|
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
|
2022-06-02 04:42:21 +00:00
|
|
|
if (!headerFile.exists()) {
|
2015-10-29 07:41:54 +00:00
|
|
|
return;
|
2022-06-02 04:42:21 +00:00
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
|
|
|
|
const PR_RDONLY = 0x01;
|
2022-06-02 04:42:21 +00:00
|
|
|
var fis = new FileInputStream(
|
|
|
|
headerFile,
|
|
|
|
PR_RDONLY,
|
|
|
|
PERMS_READONLY,
|
|
|
|
Ci.nsIFileInputStream.CLOSE_ON_EOF
|
|
|
|
);
|
|
|
|
|
|
|
|
try {
|
2015-10-29 07:41:54 +00:00
|
|
|
var lis = new ConverterInputStream(fis, "UTF-8", 1024, 0x0);
|
|
|
|
lis.QueryInterface(Ci.nsIUnicharLineInputStream);
|
|
|
|
|
2022-06-02 04:42:21 +00:00
|
|
|
var line = { value: "" };
|
2015-10-29 07:41:54 +00:00
|
|
|
var more = lis.readLine(line);
|
|
|
|
|
2022-06-02 04:42:21 +00:00
|
|
|
if (!more && line.value == "") {
|
2015-10-29 07:41:54 +00:00
|
|
|
return;
|
2022-06-02 04:42:21 +00:00
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
|
|
|
|
// request line
|
|
|
|
|
|
|
|
var status = line.value;
|
2022-06-02 04:42:21 +00:00
|
|
|
if (status.indexOf("HTTP ") == 0) {
|
2015-10-29 07:41:54 +00:00
|
|
|
status = status.substring(5);
|
|
|
|
var space = status.indexOf(" ");
|
|
|
|
var code, description;
|
2022-06-02 04:42:21 +00:00
|
|
|
if (space < 0) {
|
2015-10-29 07:41:54 +00:00
|
|
|
code = status;
|
|
|
|
description = "";
|
2022-06-02 04:42:21 +00:00
|
|
|
} else {
|
2015-10-29 07:41:54 +00:00
|
|
|
code = status.substring(0, space);
|
|
|
|
description = status.substring(space + 1, status.length);
|
|
|
|
}
|
2022-06-02 04:42:21 +00:00
|
|
|
|
|
|
|
if (!informationalResponse) {
|
|
|
|
response.setStatusLine(
|
|
|
|
metadata.httpVersion,
|
|
|
|
parseInt(code, 10),
|
|
|
|
description
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
response.setInformationalResponseStatusLine(
|
|
|
|
metadata.httpVersion,
|
|
|
|
parseInt(code, 10),
|
|
|
|
description
|
|
|
|
);
|
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
|
|
|
|
line.value = "";
|
|
|
|
more = lis.readLine(line);
|
2022-06-02 04:42:21 +00:00
|
|
|
} else if (informationalResponse) {
|
|
|
|
// An informational response must have a status line.
|
|
|
|
return;
|
2015-10-29 07:41:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// headers
|
2022-06-02 04:42:21 +00:00
|
|
|
while (more || line.value != "") {
|
2015-10-29 07:41:54 +00:00
|
|
|
var header = line.value;
|
|
|
|
var colon = header.indexOf(":");
|
|
|
|
|
2022-06-02 04:42:21 +00:00
|
|
|
if (!informationalResponse) {
|
|
|
|
response.setHeader(
|
|
|
|
header.substring(0, colon),
|
|
|
|
header.substring(colon + 1, header.length),
|
|
|
|
false
|
|
|
|
); // allow overriding server-set headers
|
|
|
|
} else {
|
|
|
|
response.setInformationalResponseHeader(
|
|
|
|
header.substring(0, colon),
|
|
|
|
header.substring(colon + 1, header.length),
|
|
|
|
false
|
|
|
|
); // allow overriding server-set headers
|
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
|
|
|
|
line.value = "";
|
|
|
|
more = lis.readLine(line);
|
|
|
|
}
|
2022-06-02 04:42:21 +00:00
|
|
|
} catch (e) {
|
2015-10-29 07:41:54 +00:00
|
|
|
dumpn("WARNING: error in headers for " + metadata.path + ": " + e);
|
|
|
|
throw HTTP_500;
|
2022-06-02 04:42:21 +00:00
|
|
|
} finally {
|
2015-10-29 07:41:54 +00:00
|
|
|
fis.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-02 04:42:21 +00:00
|
|
|
function maybeAddHeaders(file, metadata, response) {
|
|
|
|
maybeAddHeadersInternal(file, metadata, response, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
function maybeAddInformationalResponse(file, metadata, response) {
|
|
|
|
maybeAddHeadersInternal(file, metadata, response, true);
|
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* An object which handles requests for a server, executing default and
|
|
|
|
* overridden behaviors as instructed by the code which uses and manipulates it.
|
|
|
|
* Default behavior includes the paths / and /trace (diagnostics), with some
|
|
|
|
* support for HTTP error pages for various codes and fallback to HTTP 500 if
|
|
|
|
* those codes fail for any reason.
|
|
|
|
*
|
|
|
|
* @param server : nsHttpServer
|
|
|
|
* the server in which this handler is being used
|
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
function ServerHandler(server) {
|
2015-10-29 07:41:54 +00:00
|
|
|
// FIELDS
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The nsHttpServer instance associated with this handler.
|
|
|
|
*/
|
|
|
|
this._server = server;
|
|
|
|
|
|
|
|
/**
|
2019-08-25 10:51:11 +00:00
|
|
|
* A FileMap object containing the set of path->nsIFile mappings for
|
2015-10-29 07:41:54 +00:00
|
|
|
* all directory mappings set in the server (e.g., "/" for /var/www/html/,
|
|
|
|
* "/foo/bar/" for /local/path/, and "/foo/bar/baz/" for /local/path2).
|
|
|
|
*
|
|
|
|
* Note carefully: the leading and trailing "/" in each path (not file) are
|
|
|
|
* removed before insertion to simplify the code which uses this. You have
|
|
|
|
* been warned!
|
|
|
|
*/
|
|
|
|
this._pathDirectoryMap = new FileMap();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Custom request handlers for the server in which this resides. Path-handler
|
|
|
|
* pairs are stored as property-value pairs in this property.
|
|
|
|
*
|
|
|
|
* @see ServerHandler.prototype._defaultPaths
|
|
|
|
*/
|
|
|
|
this._overridePaths = {};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Custom request handlers for the path prefixes on the server in which this
|
|
|
|
* resides. Path-handler pairs are stored as property-value pairs in this
|
|
|
|
* property.
|
|
|
|
*
|
|
|
|
* @see ServerHandler.prototype._defaultPaths
|
|
|
|
*/
|
|
|
|
this._overridePrefixes = {};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Custom request handlers for the error handlers in the server in which this
|
|
|
|
* resides. Path-handler pairs are stored as property-value pairs in this
|
|
|
|
* property.
|
|
|
|
*
|
|
|
|
* @see ServerHandler.prototype._defaultErrors
|
|
|
|
*/
|
|
|
|
this._overrideErrors = {};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Maps file extensions to their MIME types in the server, overriding any
|
|
|
|
* mapping that might or might not exist in the MIME service.
|
|
|
|
*/
|
|
|
|
this._mimeMappings = {};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The default handler for requests for directories, used to serve directories
|
|
|
|
* when no index file is present.
|
|
|
|
*/
|
|
|
|
this._indexHandler = defaultIndexHandler;
|
|
|
|
|
|
|
|
/** Per-path state storage for the server. */
|
|
|
|
this._state = {};
|
|
|
|
|
|
|
|
/** Entire-server state storage. */
|
|
|
|
this._sharedState = {};
|
|
|
|
|
|
|
|
/** Entire-server state storage for nsISupports values. */
|
|
|
|
this._objectState = {};
|
|
|
|
}
|
2022-06-02 04:42:21 +00:00
|
|
|
ServerHandler.prototype = {
|
2015-10-29 07:41:54 +00:00
|
|
|
// PUBLIC API
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles a request to this server, responding to the request appropriately
|
|
|
|
* and initiating server shutdown if necessary.
|
|
|
|
*
|
|
|
|
* This method never throws an exception.
|
|
|
|
*
|
|
|
|
* @param connection : Connection
|
|
|
|
* the connection for this request
|
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
handleResponse(connection) {
|
2015-10-29 07:41:54 +00:00
|
|
|
var request = connection.request;
|
|
|
|
var response = new Response(connection);
|
|
|
|
|
|
|
|
var path = request.path;
|
|
|
|
dumpn("*** path == " + path);
|
|
|
|
|
2022-06-02 04:42:21 +00:00
|
|
|
try {
|
|
|
|
try {
|
|
|
|
if (path in this._overridePaths) {
|
2015-10-29 07:41:54 +00:00
|
|
|
// explicit paths first, then files based on existing directory mappings,
|
|
|
|
// then (if the file doesn't exist) built-in server default paths
|
|
|
|
dumpn("calling override for " + path);
|
|
|
|
this._overridePaths[path](request, response);
|
2022-06-02 04:42:21 +00:00
|
|
|
} else {
|
2015-10-29 07:41:54 +00:00
|
|
|
var longestPrefix = "";
|
|
|
|
for (let prefix in this._overridePrefixes) {
|
2022-06-02 04:42:21 +00:00
|
|
|
if (
|
|
|
|
prefix.length > longestPrefix.length &&
|
|
|
|
path.substr(0, prefix.length) == prefix
|
|
|
|
) {
|
2015-10-29 07:41:54 +00:00
|
|
|
longestPrefix = prefix;
|
|
|
|
}
|
|
|
|
}
|
2022-06-02 04:42:21 +00:00
|
|
|
if (longestPrefix.length > 0) {
|
2015-10-29 07:41:54 +00:00
|
|
|
dumpn("calling prefix override for " + longestPrefix);
|
|
|
|
this._overridePrefixes[longestPrefix](request, response);
|
2022-06-02 04:42:21 +00:00
|
|
|
} else {
|
2015-10-29 07:41:54 +00:00
|
|
|
this._handleDefault(request, response);
|
|
|
|
}
|
|
|
|
}
|
2022-06-02 04:42:21 +00:00
|
|
|
} catch (e) {
|
|
|
|
if (response.partiallySent()) {
|
2015-10-29 07:41:54 +00:00
|
|
|
response.abort(e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-02 04:42:21 +00:00
|
|
|
if (!(e instanceof HttpError)) {
|
2015-10-29 07:41:54 +00:00
|
|
|
dumpn("*** unexpected error: e == " + e);
|
|
|
|
throw HTTP_500;
|
|
|
|
}
|
2022-06-02 04:42:21 +00:00
|
|
|
if (e.code !== 404) {
|
2015-10-29 07:41:54 +00:00
|
|
|
throw e;
|
2022-06-02 04:42:21 +00:00
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
|
|
|
|
dumpn("*** default: " + (path in this._defaultPaths));
|
|
|
|
|
|
|
|
response = new Response(connection);
|
2022-06-02 04:42:21 +00:00
|
|
|
if (path in this._defaultPaths) {
|
2015-10-29 07:41:54 +00:00
|
|
|
this._defaultPaths[path](request, response);
|
2022-06-02 04:42:21 +00:00
|
|
|
} else {
|
2015-10-29 07:41:54 +00:00
|
|
|
throw HTTP_404;
|
2022-06-02 04:42:21 +00:00
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
}
|
2022-06-02 04:42:21 +00:00
|
|
|
} catch (e) {
|
|
|
|
if (response.partiallySent()) {
|
2015-10-29 07:41:54 +00:00
|
|
|
response.abort(e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var errorCode = "internal";
|
|
|
|
|
2022-06-02 04:42:21 +00:00
|
|
|
try {
|
|
|
|
if (!(e instanceof HttpError)) {
|
2015-10-29 07:41:54 +00:00
|
|
|
throw e;
|
2022-06-02 04:42:21 +00:00
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
|
|
|
|
errorCode = e.code;
|
|
|
|
dumpn("*** errorCode == " + errorCode);
|
|
|
|
|
|
|
|
response = new Response(connection);
|
2022-06-02 04:42:21 +00:00
|
|
|
if (e.customErrorHandling) {
|
2015-10-29 07:41:54 +00:00
|
|
|
e.customErrorHandling(response);
|
2022-06-02 04:42:21 +00:00
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
this._handleError(errorCode, request, response);
|
|
|
|
return;
|
2022-06-02 04:42:21 +00:00
|
|
|
} catch (e2) {
|
|
|
|
dumpn(
|
|
|
|
"*** error handling " +
|
|
|
|
errorCode +
|
|
|
|
" error: " +
|
|
|
|
"e2 == " +
|
|
|
|
e2 +
|
|
|
|
", shutting down server"
|
|
|
|
);
|
2015-10-29 07:41:54 +00:00
|
|
|
|
|
|
|
connection.server._requestQuit();
|
|
|
|
response.abort(e2);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
response.complete();
|
|
|
|
},
|
|
|
|
|
|
|
|
//
|
|
|
|
// see nsIHttpServer.registerFile
|
|
|
|
//
|
2022-06-02 04:42:21 +00:00
|
|
|
registerFile(path, file, handler) {
|
|
|
|
if (!file) {
|
2015-10-29 07:41:54 +00:00
|
|
|
dumpn("*** unregistering '" + path + "' mapping");
|
|
|
|
delete this._overridePaths[path];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
dumpn("*** registering '" + path + "' as mapping to " + file.path);
|
|
|
|
file = file.clone();
|
|
|
|
|
|
|
|
var self = this;
|
2022-06-02 04:42:21 +00:00
|
|
|
this._overridePaths[path] = function(request, response) {
|
|
|
|
if (!file.exists()) {
|
|
|
|
throw HTTP_404;
|
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
|
2022-06-02 04:42:21 +00:00
|
|
|
dumpn("*** responding '" + path + "' as mapping to " + file.path);
|
|
|
|
|
|
|
|
response.setStatusLine(request.httpVersion, 200, "OK");
|
|
|
|
if (typeof handler === "function") {
|
|
|
|
handler(request, response);
|
|
|
|
}
|
|
|
|
self._writeFileResponse(request, file, response, 0, file.fileSize);
|
|
|
|
};
|
2015-10-29 07:41:54 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
//
|
|
|
|
// see nsIHttpServer.registerPathHandler
|
|
|
|
//
|
2022-06-02 04:42:21 +00:00
|
|
|
registerPathHandler(path, handler) {
|
|
|
|
if (path.length == 0) {
|
|
|
|
throw Components.Exception(
|
|
|
|
"Handler path cannot be empty",
|
|
|
|
Cr.NS_ERROR_INVALID_ARG
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-10-29 07:41:54 +00:00
|
|
|
// XXX true path validation!
|
2022-06-02 04:42:21 +00:00
|
|
|
if (path.charAt(0) != "/" && path != "CONNECT") {
|
|
|
|
throw Components.Exception("", Cr.NS_ERROR_INVALID_ARG);
|
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
|
|
|
|
this._handlerToField(handler, this._overridePaths, path);
|
|
|
|
},
|
|
|
|
|
|
|
|
//
|
|
|
|
// see nsIHttpServer.registerPrefixHandler
|
|
|
|
//
|
2022-06-02 04:42:21 +00:00
|
|
|
registerPrefixHandler(path, handler) {
|
2015-10-29 07:41:54 +00:00
|
|
|
// XXX true path validation!
|
2022-06-02 04:42:21 +00:00
|
|
|
if (path.charAt(0) != "/" || path.charAt(path.length - 1) != "/") {
|
|
|
|
throw Components.Exception("", Cr.NS_ERROR_INVALID_ARG);
|
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
|
|
|
|
this._handlerToField(handler, this._overridePrefixes, path);
|
|
|
|
},
|
|
|
|
|
|
|
|
//
|
|
|
|
// see nsIHttpServer.registerDirectory
|
|
|
|
//
|
2022-06-02 04:42:21 +00:00
|
|
|
registerDirectory(path, directory) {
|
2015-10-29 07:41:54 +00:00
|
|
|
// strip off leading and trailing '/' so that we can use lastIndexOf when
|
|
|
|
// determining exactly how a path maps onto a mapped directory --
|
|
|
|
// conditional is required here to deal with "/".substring(1, 0) being
|
|
|
|
// converted to "/".substring(0, 1) per the JS specification
|
|
|
|
var key = path.length == 1 ? "" : path.substring(1, path.length - 1);
|
|
|
|
|
|
|
|
// the path-to-directory mapping code requires that the first character not
|
|
|
|
// be "/", or it will go into an infinite loop
|
2022-06-02 04:42:21 +00:00
|
|
|
if (key.charAt(0) == "/") {
|
|
|
|
throw Components.Exception("", Cr.NS_ERROR_INVALID_ARG);
|
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
|
|
|
|
key = toInternalPath(key, false);
|
|
|
|
|
2022-06-02 04:42:21 +00:00
|
|
|
if (directory) {
|
2015-10-29 07:41:54 +00:00
|
|
|
dumpn("*** mapping '" + path + "' to the location " + directory.path);
|
|
|
|
this._pathDirectoryMap.put(key, directory);
|
2022-06-02 04:42:21 +00:00
|
|
|
} else {
|
2015-10-29 07:41:54 +00:00
|
|
|
dumpn("*** removing mapping for '" + path + "'");
|
|
|
|
this._pathDirectoryMap.put(key, null);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
//
|
|
|
|
// see nsIHttpServer.registerErrorHandler
|
|
|
|
//
|
2022-06-02 04:42:21 +00:00
|
|
|
registerErrorHandler(err, handler) {
|
|
|
|
if (!(err in HTTP_ERROR_CODES)) {
|
|
|
|
dumpn(
|
|
|
|
"*** WARNING: registering non-HTTP/1.1 error code " +
|
|
|
|
"(" +
|
|
|
|
err +
|
|
|
|
") handler -- was this intentional?"
|
|
|
|
);
|
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
|
|
|
|
this._handlerToField(handler, this._overrideErrors, err);
|
|
|
|
},
|
|
|
|
|
|
|
|
//
|
|
|
|
// see nsIHttpServer.setIndexHandler
|
|
|
|
//
|
2022-06-02 04:42:21 +00:00
|
|
|
setIndexHandler(handler) {
|
|
|
|
if (!handler) {
|
2015-10-29 07:41:54 +00:00
|
|
|
handler = defaultIndexHandler;
|
2022-06-02 04:42:21 +00:00
|
|
|
} else if (typeof handler != "function") {
|
2015-10-29 07:41:54 +00:00
|
|
|
handler = createHandlerFunc(handler);
|
2022-06-02 04:42:21 +00:00
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
|
|
|
|
this._indexHandler = handler;
|
|
|
|
},
|
|
|
|
|
|
|
|
//
|
|
|
|
// see nsIHttpServer.registerContentType
|
|
|
|
//
|
2022-06-02 04:42:21 +00:00
|
|
|
registerContentType(ext, type) {
|
|
|
|
if (!type) {
|
2015-10-29 07:41:54 +00:00
|
|
|
delete this._mimeMappings[ext];
|
2022-06-02 04:42:21 +00:00
|
|
|
} else {
|
2015-10-29 07:41:54 +00:00
|
|
|
this._mimeMappings[ext] = headerUtils.normalizeFieldValue(type);
|
2022-06-02 04:42:21 +00:00
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
// PRIVATE API
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets or remove (if handler is null) a handler in an object with a key.
|
|
|
|
*
|
|
|
|
* @param handler
|
|
|
|
* a handler, either function or an nsIHttpRequestHandler
|
|
|
|
* @param dict
|
|
|
|
* The object to attach the handler to.
|
|
|
|
* @param key
|
|
|
|
* The field name of the handler.
|
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
_handlerToField(handler, dict, key) {
|
2015-10-29 07:41:54 +00:00
|
|
|
// for convenience, handler can be a function if this is run from xpcshell
|
2022-06-02 04:42:21 +00:00
|
|
|
if (typeof handler == "function") {
|
2015-10-29 07:41:54 +00:00
|
|
|
dict[key] = handler;
|
2022-06-02 04:42:21 +00:00
|
|
|
} else if (handler) {
|
2015-10-29 07:41:54 +00:00
|
|
|
dict[key] = createHandlerFunc(handler);
|
2022-06-02 04:42:21 +00:00
|
|
|
} else {
|
2015-10-29 07:41:54 +00:00
|
|
|
delete dict[key];
|
2022-06-02 04:42:21 +00:00
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles a request which maps to a file in the local filesystem (if a base
|
|
|
|
* path has already been set; otherwise the 404 error is thrown).
|
|
|
|
*
|
|
|
|
* @param metadata : Request
|
|
|
|
* metadata for the incoming request
|
|
|
|
* @param response : Response
|
|
|
|
* an uninitialized Response to the given request, to be initialized by a
|
|
|
|
* request handler
|
|
|
|
* @throws HTTP_###
|
|
|
|
* if an HTTP error occurred (usually HTTP_404); note that in this case the
|
|
|
|
* calling code must handle post-processing of the response
|
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
_handleDefault(metadata, response) {
|
2015-10-29 07:41:54 +00:00
|
|
|
dumpn("*** _handleDefault()");
|
|
|
|
|
|
|
|
response.setStatusLine(metadata.httpVersion, 200, "OK");
|
|
|
|
|
|
|
|
var path = metadata.path;
|
|
|
|
NS_ASSERT(path.charAt(0) == "/", "invalid path: <" + path + ">");
|
|
|
|
|
|
|
|
// determine the actual on-disk file; this requires finding the deepest
|
|
|
|
// path-to-directory mapping in the requested URL
|
|
|
|
var file = this._getFileForPath(path);
|
|
|
|
|
|
|
|
// the "file" might be a directory, in which case we either serve the
|
|
|
|
// contained index.html or make the index handler write the response
|
2022-06-02 04:42:21 +00:00
|
|
|
if (file.exists() && file.isDirectory()) {
|
2015-10-29 07:41:54 +00:00
|
|
|
file.append("index.html"); // make configurable?
|
2022-06-02 04:42:21 +00:00
|
|
|
if (!file.exists() || file.isDirectory()) {
|
2015-10-29 07:41:54 +00:00
|
|
|
metadata._ensurePropertyBag();
|
|
|
|
metadata._bag.setPropertyAsInterface("directory", file.parent);
|
|
|
|
this._indexHandler(metadata, response);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// alternately, the file might not exist
|
2022-06-02 04:42:21 +00:00
|
|
|
if (!file.exists()) {
|
2015-10-29 07:41:54 +00:00
|
|
|
throw HTTP_404;
|
2022-06-02 04:42:21 +00:00
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
|
|
|
|
var start, end;
|
2022-06-02 04:42:21 +00:00
|
|
|
if (
|
|
|
|
metadata._httpVersion.atLeast(nsHttpVersion.HTTP_1_1) &&
|
|
|
|
metadata.hasHeader("Range") &&
|
|
|
|
this._getTypeFromFile(file) !== SJS_TYPE
|
|
|
|
) {
|
|
|
|
var rangeMatch = metadata
|
|
|
|
.getHeader("Range")
|
|
|
|
.match(/^bytes=(\d+)?-(\d+)?$/);
|
|
|
|
if (!rangeMatch) {
|
|
|
|
dumpn(
|
|
|
|
"*** Range header bogosity: '" + metadata.getHeader("Range") + "'"
|
|
|
|
);
|
2015-10-29 07:41:54 +00:00
|
|
|
throw HTTP_400;
|
|
|
|
}
|
|
|
|
|
2022-06-02 04:42:21 +00:00
|
|
|
if (rangeMatch[1] !== undefined) {
|
2015-10-29 07:41:54 +00:00
|
|
|
start = parseInt(rangeMatch[1], 10);
|
2022-06-02 04:42:21 +00:00
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
|
2022-06-02 04:42:21 +00:00
|
|
|
if (rangeMatch[2] !== undefined) {
|
2015-10-29 07:41:54 +00:00
|
|
|
end = parseInt(rangeMatch[2], 10);
|
2022-06-02 04:42:21 +00:00
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
|
2022-06-02 04:42:21 +00:00
|
|
|
if (start === undefined && end === undefined) {
|
|
|
|
dumpn(
|
|
|
|
"*** More Range header bogosity: '" +
|
|
|
|
metadata.getHeader("Range") +
|
|
|
|
"'"
|
|
|
|
);
|
2015-10-29 07:41:54 +00:00
|
|
|
throw HTTP_400;
|
|
|
|
}
|
|
|
|
|
|
|
|
// No start given, so the end is really the count of bytes from the
|
|
|
|
// end of the file.
|
2022-06-02 04:42:21 +00:00
|
|
|
if (start === undefined) {
|
2015-10-29 07:41:54 +00:00
|
|
|
start = Math.max(0, file.fileSize - end);
|
2022-06-02 04:42:21 +00:00
|
|
|
end = file.fileSize - 1;
|
2015-10-29 07:41:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// start and end are inclusive
|
2022-06-02 04:42:21 +00:00
|
|
|
if (end === undefined || end >= file.fileSize) {
|
2015-10-29 07:41:54 +00:00
|
|
|
end = file.fileSize - 1;
|
2022-06-02 04:42:21 +00:00
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
|
|
|
|
if (start !== undefined && start >= file.fileSize) {
|
|
|
|
var HTTP_416 = new HttpError(416, "Requested Range Not Satisfiable");
|
2022-06-02 04:42:21 +00:00
|
|
|
HTTP_416.customErrorHandling = function(errorResponse) {
|
2015-10-29 07:41:54 +00:00
|
|
|
maybeAddHeaders(file, metadata, errorResponse);
|
|
|
|
};
|
|
|
|
throw HTTP_416;
|
|
|
|
}
|
|
|
|
|
2022-06-02 04:42:21 +00:00
|
|
|
if (end < start) {
|
2015-10-29 07:41:54 +00:00
|
|
|
response.setStatusLine(metadata.httpVersion, 200, "OK");
|
|
|
|
start = 0;
|
|
|
|
end = file.fileSize - 1;
|
2022-06-02 04:42:21 +00:00
|
|
|
} else {
|
2015-10-29 07:41:54 +00:00
|
|
|
response.setStatusLine(metadata.httpVersion, 206, "Partial Content");
|
|
|
|
var contentRange = "bytes " + start + "-" + end + "/" + file.fileSize;
|
|
|
|
response.setHeader("Content-Range", contentRange);
|
|
|
|
}
|
2022-06-02 04:42:21 +00:00
|
|
|
} else {
|
2015-10-29 07:41:54 +00:00
|
|
|
start = 0;
|
|
|
|
end = file.fileSize - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// finally...
|
2022-06-02 04:42:21 +00:00
|
|
|
dumpn(
|
|
|
|
"*** handling '" +
|
|
|
|
path +
|
|
|
|
"' as mapping to " +
|
|
|
|
file.path +
|
|
|
|
" from " +
|
|
|
|
start +
|
|
|
|
" to " +
|
|
|
|
end +
|
|
|
|
" inclusive"
|
|
|
|
);
|
2015-10-29 07:41:54 +00:00
|
|
|
this._writeFileResponse(metadata, file, response, start, end - start + 1);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Writes an HTTP response for the given file, including setting headers for
|
|
|
|
* file metadata.
|
|
|
|
*
|
|
|
|
* @param metadata : Request
|
|
|
|
* the Request for which a response is being generated
|
2019-08-25 10:51:11 +00:00
|
|
|
* @param file : nsIFile
|
2015-10-29 07:41:54 +00:00
|
|
|
* the file which is to be sent in the response
|
|
|
|
* @param response : Response
|
|
|
|
* the response to which the file should be written
|
|
|
|
* @param offset: uint
|
|
|
|
* the byte offset to skip to when writing
|
|
|
|
* @param count: uint
|
|
|
|
* the number of bytes to write
|
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
_writeFileResponse(metadata, file, response, offset, count) {
|
2015-10-29 07:41:54 +00:00
|
|
|
const PR_RDONLY = 0x01;
|
|
|
|
|
|
|
|
var type = this._getTypeFromFile(file);
|
2022-06-02 04:42:21 +00:00
|
|
|
if (type === SJS_TYPE) {
|
|
|
|
let fis = new FileInputStream(
|
|
|
|
file,
|
|
|
|
PR_RDONLY,
|
|
|
|
PERMS_READONLY,
|
|
|
|
Ci.nsIFileInputStream.CLOSE_ON_EOF
|
|
|
|
);
|
|
|
|
|
|
|
|
try {
|
|
|
|
// If you update the list of imports, please update the list in
|
|
|
|
// tools/lint/eslint/eslint-plugin-mozilla/lib/environments/sjs.js
|
|
|
|
// as well.
|
|
|
|
var s = Cu.Sandbox(globalThis);
|
2015-10-29 07:41:54 +00:00
|
|
|
s.importFunction(dump, "dump");
|
|
|
|
s.importFunction(atob, "atob");
|
|
|
|
s.importFunction(btoa, "btoa");
|
2022-06-02 04:42:21 +00:00
|
|
|
s.importFunction(ChromeUtils, "ChromeUtils");
|
2015-10-29 07:41:54 +00:00
|
|
|
|
|
|
|
// Define a basic key-value state-preservation API across requests, with
|
|
|
|
// keys initially corresponding to the empty string.
|
|
|
|
var self = this;
|
|
|
|
var path = metadata.path;
|
2022-06-02 04:42:21 +00:00
|
|
|
s.importFunction(function getState(k) {
|
2015-10-29 07:41:54 +00:00
|
|
|
return self._getState(path, k);
|
|
|
|
});
|
2022-06-02 04:42:21 +00:00
|
|
|
s.importFunction(function setState(k, v) {
|
2015-10-29 07:41:54 +00:00
|
|
|
self._setState(path, k, v);
|
|
|
|
});
|
2022-06-02 04:42:21 +00:00
|
|
|
s.importFunction(function getSharedState(k) {
|
2015-10-29 07:41:54 +00:00
|
|
|
return self._getSharedState(k);
|
|
|
|
});
|
2022-06-02 04:42:21 +00:00
|
|
|
s.importFunction(function setSharedState(k, v) {
|
2015-10-29 07:41:54 +00:00
|
|
|
self._setSharedState(k, v);
|
|
|
|
});
|
2022-06-02 04:42:21 +00:00
|
|
|
s.importFunction(function getObjectState(k, callback) {
|
2015-10-29 07:41:54 +00:00
|
|
|
callback(self._getObjectState(k));
|
|
|
|
});
|
2022-06-02 04:42:21 +00:00
|
|
|
s.importFunction(function setObjectState(k, v) {
|
2015-10-29 07:41:54 +00:00
|
|
|
self._setObjectState(k, v);
|
|
|
|
});
|
2022-06-02 04:42:21 +00:00
|
|
|
s.importFunction(function registerPathHandler(p, h) {
|
2015-10-29 07:41:54 +00:00
|
|
|
self.registerPathHandler(p, h);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Make it possible for sjs files to access their location
|
|
|
|
this._setState(path, "__LOCATION__", file.path);
|
|
|
|
|
2022-06-02 04:42:21 +00:00
|
|
|
try {
|
2015-10-29 07:41:54 +00:00
|
|
|
// Alas, the line number in errors dumped to console when calling the
|
|
|
|
// request handler is simply an offset from where we load the SJS file.
|
|
|
|
// Work around this in a reasonably non-fragile way by dynamically
|
|
|
|
// getting the line number where we evaluate the SJS file. Don't
|
|
|
|
// separate these two lines!
|
|
|
|
var line = new Error().lineNumber;
|
2022-06-02 04:42:21 +00:00
|
|
|
let uri = Services.io.newFileURI(file);
|
|
|
|
Services.scriptloader.loadSubScript(uri.spec, s);
|
|
|
|
} catch (e) {
|
2015-10-29 07:41:54 +00:00
|
|
|
dumpn("*** syntax error in SJS at " + file.path + ": " + e);
|
|
|
|
throw HTTP_500;
|
|
|
|
}
|
|
|
|
|
2022-06-02 04:42:21 +00:00
|
|
|
try {
|
2015-10-29 07:41:54 +00:00
|
|
|
s.handleRequest(metadata, response);
|
2022-06-02 04:42:21 +00:00
|
|
|
} catch (e) {
|
|
|
|
dump(
|
|
|
|
"*** error running SJS at " +
|
|
|
|
file.path +
|
|
|
|
": " +
|
|
|
|
e +
|
|
|
|
" on line " +
|
|
|
|
(e instanceof Error
|
|
|
|
? e.lineNumber + " in httpd.js"
|
|
|
|
: e.lineNumber - line) +
|
|
|
|
"\n"
|
|
|
|
);
|
2015-10-29 07:41:54 +00:00
|
|
|
throw HTTP_500;
|
|
|
|
}
|
2022-06-02 04:42:21 +00:00
|
|
|
} finally {
|
2015-10-29 07:41:54 +00:00
|
|
|
fis.close();
|
|
|
|
}
|
2022-06-02 04:42:21 +00:00
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
response.setHeader(
|
|
|
|
"Last-Modified",
|
|
|
|
toDateString(file.lastModifiedTime),
|
|
|
|
false
|
|
|
|
);
|
|
|
|
} catch (e) {
|
|
|
|
/* lastModifiedTime threw, ignore */
|
2015-10-29 07:41:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
response.setHeader("Content-Type", type, false);
|
2022-06-02 04:42:21 +00:00
|
|
|
maybeAddInformationalResponse(file, metadata, response);
|
2015-10-29 07:41:54 +00:00
|
|
|
maybeAddHeaders(file, metadata, response);
|
|
|
|
response.setHeader("Content-Length", "" + count, false);
|
|
|
|
|
2022-06-02 04:42:21 +00:00
|
|
|
let fis = new FileInputStream(
|
|
|
|
file,
|
|
|
|
PR_RDONLY,
|
|
|
|
PERMS_READONLY,
|
|
|
|
Ci.nsIFileInputStream.CLOSE_ON_EOF
|
|
|
|
);
|
2015-10-29 07:41:54 +00:00
|
|
|
|
|
|
|
offset = offset || 0;
|
2022-06-02 04:42:21 +00:00
|
|
|
count = count || file.fileSize;
|
2015-10-29 07:41:54 +00:00
|
|
|
NS_ASSERT(offset === 0 || offset < file.fileSize, "bad offset");
|
|
|
|
NS_ASSERT(count >= 0, "bad count");
|
|
|
|
NS_ASSERT(offset + count <= file.fileSize, "bad total data size");
|
|
|
|
|
2022-06-02 04:42:21 +00:00
|
|
|
try {
|
|
|
|
if (offset !== 0) {
|
2015-10-29 07:41:54 +00:00
|
|
|
// Seek (or read, if seeking isn't supported) to the correct offset so
|
|
|
|
// the data sent to the client matches the requested range.
|
2022-06-02 04:42:21 +00:00
|
|
|
if (fis instanceof Ci.nsISeekableStream) {
|
2015-10-29 07:41:54 +00:00
|
|
|
fis.seek(Ci.nsISeekableStream.NS_SEEK_SET, offset);
|
2022-06-02 04:42:21 +00:00
|
|
|
} else {
|
2015-10-29 07:41:54 +00:00
|
|
|
new ScriptableInputStream(fis).read(offset);
|
2022-06-02 04:42:21 +00:00
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
}
|
2022-06-02 04:42:21 +00:00
|
|
|
} catch (e) {
|
2015-10-29 07:41:54 +00:00
|
|
|
fis.close();
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
|
2022-06-02 04:42:21 +00:00
|
|
|
let writeMore = function() {
|
|
|
|
gThreadManager.currentThread.dispatch(
|
|
|
|
writeData,
|
|
|
|
Ci.nsIThread.DISPATCH_NORMAL
|
|
|
|
);
|
|
|
|
};
|
2015-10-29 07:41:54 +00:00
|
|
|
|
|
|
|
var input = new BinaryInputStream(fis);
|
|
|
|
var output = new BinaryOutputStream(response.bodyOutputStream);
|
2022-06-02 04:42:21 +00:00
|
|
|
var writeData = {
|
|
|
|
run() {
|
|
|
|
var chunkSize = Math.min(65536, count);
|
|
|
|
count -= chunkSize;
|
|
|
|
NS_ASSERT(count >= 0, "underflow");
|
|
|
|
|
|
|
|
try {
|
|
|
|
var data = input.readByteArray(chunkSize);
|
|
|
|
NS_ASSERT(
|
|
|
|
data.length === chunkSize,
|
|
|
|
"incorrect data returned? got " +
|
|
|
|
data.length +
|
|
|
|
", expected " +
|
|
|
|
chunkSize
|
|
|
|
);
|
|
|
|
output.writeByteArray(data);
|
|
|
|
if (count === 0) {
|
|
|
|
fis.close();
|
|
|
|
response.finish();
|
|
|
|
} else {
|
|
|
|
writeMore();
|
2015-10-29 07:41:54 +00:00
|
|
|
}
|
2022-06-02 04:42:21 +00:00
|
|
|
} catch (e) {
|
|
|
|
try {
|
|
|
|
fis.close();
|
|
|
|
} finally {
|
|
|
|
response.finish();
|
2015-10-29 07:41:54 +00:00
|
|
|
}
|
2022-06-02 04:42:21 +00:00
|
|
|
throw e;
|
2015-10-29 07:41:54 +00:00
|
|
|
}
|
2022-06-02 04:42:21 +00:00
|
|
|
},
|
|
|
|
};
|
2015-10-29 07:41:54 +00:00
|
|
|
|
|
|
|
writeMore();
|
|
|
|
|
|
|
|
// Now that we know copying will start, flag the response as async.
|
|
|
|
response.processAsync();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the value corresponding to a given key for the given path for SJS state
|
|
|
|
* preservation across requests.
|
|
|
|
*
|
|
|
|
* @param path : string
|
|
|
|
* the path from which the given state is to be retrieved
|
|
|
|
* @param k : string
|
|
|
|
* the key whose corresponding value is to be returned
|
|
|
|
* @returns string
|
|
|
|
* the corresponding value, which is initially the empty string
|
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
_getState(path, k) {
|
2015-10-29 07:41:54 +00:00
|
|
|
var state = this._state;
|
2022-06-02 04:42:21 +00:00
|
|
|
if (path in state && k in state[path]) {
|
2015-10-29 07:41:54 +00:00
|
|
|
return state[path][k];
|
2022-06-02 04:42:21 +00:00
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
return "";
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the value corresponding to a given key for the given path for SJS state
|
|
|
|
* preservation across requests.
|
|
|
|
*
|
|
|
|
* @param path : string
|
|
|
|
* the path from which the given state is to be retrieved
|
|
|
|
* @param k : string
|
|
|
|
* the key whose corresponding value is to be set
|
|
|
|
* @param v : string
|
|
|
|
* the value to be set
|
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
_setState(path, k, v) {
|
|
|
|
if (typeof v !== "string") {
|
2015-10-29 07:41:54 +00:00
|
|
|
throw new Error("non-string value passed");
|
2022-06-02 04:42:21 +00:00
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
var state = this._state;
|
2022-06-02 04:42:21 +00:00
|
|
|
if (!(path in state)) {
|
2015-10-29 07:41:54 +00:00
|
|
|
state[path] = {};
|
2022-06-02 04:42:21 +00:00
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
state[path][k] = v;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the value corresponding to a given key for SJS state preservation
|
|
|
|
* across requests.
|
|
|
|
*
|
|
|
|
* @param k : string
|
|
|
|
* the key whose corresponding value is to be returned
|
|
|
|
* @returns string
|
|
|
|
* the corresponding value, which is initially the empty string
|
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
_getSharedState(k) {
|
2015-10-29 07:41:54 +00:00
|
|
|
var state = this._sharedState;
|
2022-06-02 04:42:21 +00:00
|
|
|
if (k in state) {
|
2015-10-29 07:41:54 +00:00
|
|
|
return state[k];
|
2022-06-02 04:42:21 +00:00
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
return "";
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the value corresponding to a given key for SJS state preservation
|
|
|
|
* across requests.
|
|
|
|
*
|
|
|
|
* @param k : string
|
|
|
|
* the key whose corresponding value is to be set
|
|
|
|
* @param v : string
|
|
|
|
* the value to be set
|
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
_setSharedState(k, v) {
|
|
|
|
if (typeof v !== "string") {
|
2015-10-29 07:41:54 +00:00
|
|
|
throw new Error("non-string value passed");
|
2022-06-02 04:42:21 +00:00
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
this._sharedState[k] = v;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the object associated with the given key in the server for SJS
|
|
|
|
* state preservation across requests.
|
|
|
|
*
|
|
|
|
* @param k : string
|
|
|
|
* the key whose corresponding object is to be returned
|
|
|
|
* @returns nsISupports
|
|
|
|
* the corresponding object, or null if none was present
|
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
_getObjectState(k) {
|
|
|
|
if (typeof k !== "string") {
|
2015-10-29 07:41:54 +00:00
|
|
|
throw new Error("non-string key passed");
|
2022-06-02 04:42:21 +00:00
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
return this._objectState[k] || null;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the object associated with the given key in the server for SJS
|
|
|
|
* state preservation across requests.
|
|
|
|
*
|
|
|
|
* @param k : string
|
|
|
|
* the key whose corresponding object is to be set
|
|
|
|
* @param v : nsISupports
|
|
|
|
* the object to be associated with the given key; may be null
|
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
_setObjectState(k, v) {
|
|
|
|
if (typeof k !== "string") {
|
2015-10-29 07:41:54 +00:00
|
|
|
throw new Error("non-string key passed");
|
2022-06-02 04:42:21 +00:00
|
|
|
}
|
|
|
|
if (typeof v !== "object") {
|
2015-10-29 07:41:54 +00:00
|
|
|
throw new Error("non-object value passed");
|
2022-06-02 04:42:21 +00:00
|
|
|
}
|
|
|
|
if (v && !("QueryInterface" in v)) {
|
|
|
|
throw new Error(
|
|
|
|
"must pass an nsISupports; use wrappedJSObject to ease " +
|
|
|
|
"pain when using the server from JS"
|
|
|
|
);
|
2015-10-29 07:41:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this._objectState[k] = v;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a content-type for the given file, first by checking for any custom
|
|
|
|
* MIME-types registered with this handler for the file's extension, second by
|
|
|
|
* asking the global MIME service for a content-type, and finally by failing
|
|
|
|
* over to application/octet-stream.
|
|
|
|
*
|
|
|
|
* @param file : nsIFile
|
|
|
|
* the nsIFile for which to get a file type
|
|
|
|
* @returns string
|
|
|
|
* the best content-type which can be determined for the file
|
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
_getTypeFromFile(file) {
|
|
|
|
try {
|
2015-10-29 07:41:54 +00:00
|
|
|
var name = file.leafName;
|
|
|
|
var dot = name.lastIndexOf(".");
|
2022-06-02 04:42:21 +00:00
|
|
|
if (dot > 0) {
|
2015-10-29 07:41:54 +00:00
|
|
|
var ext = name.slice(dot + 1);
|
2022-06-02 04:42:21 +00:00
|
|
|
if (ext in this._mimeMappings) {
|
2015-10-29 07:41:54 +00:00
|
|
|
return this._mimeMappings[ext];
|
2022-06-02 04:42:21 +00:00
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
}
|
2022-06-02 04:42:21 +00:00
|
|
|
return Cc["@mozilla.org/mime;1"]
|
|
|
|
.getService(Ci.nsIMIMEService)
|
|
|
|
.getTypeFromFile(file);
|
|
|
|
} catch (e) {
|
2015-10-29 07:41:54 +00:00
|
|
|
return "application/octet-stream";
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2019-08-25 10:51:11 +00:00
|
|
|
* Returns the nsIFile which corresponds to the path, as determined using
|
2015-10-29 07:41:54 +00:00
|
|
|
* all registered path->directory mappings and any paths which are explicitly
|
|
|
|
* overridden.
|
|
|
|
*
|
|
|
|
* @param path : string
|
|
|
|
* the server path for which a file should be retrieved, e.g. "/foo/bar"
|
|
|
|
* @throws HttpError
|
|
|
|
* when the correct action is the corresponding HTTP error (i.e., because no
|
|
|
|
* mapping was found for a directory in path, the referenced file doesn't
|
|
|
|
* exist, etc.)
|
2019-08-25 10:51:11 +00:00
|
|
|
* @returns nsIFile
|
2015-10-29 07:41:54 +00:00
|
|
|
* the file to be sent as the response to a request for the path
|
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
_getFileForPath(path) {
|
2015-10-29 07:41:54 +00:00
|
|
|
// decode and add underscores as necessary
|
2022-06-02 04:42:21 +00:00
|
|
|
try {
|
2015-10-29 07:41:54 +00:00
|
|
|
path = toInternalPath(path, true);
|
2022-06-02 04:42:21 +00:00
|
|
|
} catch (e) {
|
2015-10-29 07:41:54 +00:00
|
|
|
dumpn("*** toInternalPath threw " + e);
|
|
|
|
throw HTTP_400; // malformed path
|
|
|
|
}
|
|
|
|
|
|
|
|
// next, get the directory which contains this path
|
|
|
|
var pathMap = this._pathDirectoryMap;
|
|
|
|
|
|
|
|
// An example progression of tmp for a path "/foo/bar/baz/" might be:
|
|
|
|
// "foo/bar/baz/", "foo/bar/baz", "foo/bar", "foo", ""
|
|
|
|
var tmp = path.substring(1);
|
2022-06-02 04:42:21 +00:00
|
|
|
while (true) {
|
2015-10-29 07:41:54 +00:00
|
|
|
// do we have a match for current head of the path?
|
|
|
|
var file = pathMap.get(tmp);
|
2022-06-02 04:42:21 +00:00
|
|
|
if (file) {
|
2015-10-29 07:41:54 +00:00
|
|
|
// XXX hack; basically disable showing mapping for /foo/bar/ when the
|
|
|
|
// requested path was /foo/bar, because relative links on the page
|
|
|
|
// will all be incorrect -- we really need the ability to easily
|
|
|
|
// redirect here instead
|
2022-06-02 04:42:21 +00:00
|
|
|
if (
|
|
|
|
tmp == path.substring(1) &&
|
|
|
|
tmp.length != 0 &&
|
|
|
|
tmp.charAt(tmp.length - 1) != "/"
|
|
|
|
) {
|
2015-10-29 07:41:54 +00:00
|
|
|
file = null;
|
2022-06-02 04:42:21 +00:00
|
|
|
} else {
|
2015-10-29 07:41:54 +00:00
|
|
|
break;
|
2022-06-02 04:42:21 +00:00
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// if we've finished trying all prefixes, exit
|
2022-06-02 04:42:21 +00:00
|
|
|
if (tmp == "") {
|
2015-10-29 07:41:54 +00:00
|
|
|
break;
|
2022-06-02 04:42:21 +00:00
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
|
|
|
|
tmp = tmp.substring(0, tmp.lastIndexOf("/"));
|
|
|
|
}
|
|
|
|
|
|
|
|
// no mapping applies, so 404
|
2022-06-02 04:42:21 +00:00
|
|
|
if (!file) {
|
2015-10-29 07:41:54 +00:00
|
|
|
throw HTTP_404;
|
2022-06-02 04:42:21 +00:00
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
|
|
|
|
// last, get the file for the path within the determined directory
|
|
|
|
var parentFolder = file.parent;
|
2022-06-02 04:42:21 +00:00
|
|
|
var dirIsRoot = parentFolder == null;
|
2015-10-29 07:41:54 +00:00
|
|
|
|
|
|
|
// Strategy here is to append components individually, making sure we
|
|
|
|
// never move above the given directory; this allows paths such as
|
|
|
|
// "<file>/foo/../bar" but prevents paths such as "<file>/../base-sibling";
|
|
|
|
// this component-wise approach also means the code works even on platforms
|
|
|
|
// which don't use "/" as the directory separator, such as Windows
|
|
|
|
var leafPath = path.substring(tmp.length + 1);
|
|
|
|
var comps = leafPath.split("/");
|
2022-06-02 04:42:21 +00:00
|
|
|
for (var i = 0, sz = comps.length; i < sz; i++) {
|
2015-10-29 07:41:54 +00:00
|
|
|
var comp = comps[i];
|
|
|
|
|
2022-06-02 04:42:21 +00:00
|
|
|
if (comp == "..") {
|
2015-10-29 07:41:54 +00:00
|
|
|
file = file.parent;
|
2022-06-02 04:42:21 +00:00
|
|
|
} else if (comp == "." || comp == "") {
|
2015-10-29 07:41:54 +00:00
|
|
|
continue;
|
2022-06-02 04:42:21 +00:00
|
|
|
} else {
|
2015-10-29 07:41:54 +00:00
|
|
|
file.append(comp);
|
2022-06-02 04:42:21 +00:00
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
|
2022-06-02 04:42:21 +00:00
|
|
|
if (!dirIsRoot && file.equals(parentFolder)) {
|
2015-10-29 07:41:54 +00:00
|
|
|
throw HTTP_403;
|
2022-06-02 04:42:21 +00:00
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return file;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Writes the error page for the given HTTP error code over the given
|
|
|
|
* connection.
|
|
|
|
*
|
|
|
|
* @param errorCode : uint
|
|
|
|
* the HTTP error code to be used
|
|
|
|
* @param connection : Connection
|
|
|
|
* the connection on which the error occurred
|
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
handleError(errorCode, connection) {
|
2015-10-29 07:41:54 +00:00
|
|
|
var response = new Response(connection);
|
|
|
|
|
|
|
|
dumpn("*** error in request: " + errorCode);
|
|
|
|
|
|
|
|
this._handleError(errorCode, new Request(connection.port), response);
|
2022-06-02 04:42:21 +00:00
|
|
|
},
|
2015-10-29 07:41:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles a request which generates the given error code, using the
|
|
|
|
* user-defined error handler if one has been set, gracefully falling back to
|
|
|
|
* the x00 status code if the code has no handler, and failing to status code
|
|
|
|
* 500 if all else fails.
|
|
|
|
*
|
|
|
|
* @param errorCode : uint
|
|
|
|
* the HTTP error which is to be returned
|
|
|
|
* @param metadata : Request
|
|
|
|
* metadata for the request, which will often be incomplete since this is an
|
|
|
|
* error
|
|
|
|
* @param response : Response
|
|
|
|
* an uninitialized Response should be initialized when this method
|
|
|
|
* completes with information which represents the desired error code in the
|
|
|
|
* ideal case or a fallback code in abnormal circumstances (i.e., 500 is a
|
|
|
|
* fallback for 505, per HTTP specs)
|
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
_handleError(errorCode, metadata, response) {
|
|
|
|
if (!metadata) {
|
|
|
|
throw Components.Exception("", Cr.NS_ERROR_NULL_POINTER);
|
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
|
|
|
|
var errorX00 = errorCode - (errorCode % 100);
|
|
|
|
|
2022-06-02 04:42:21 +00:00
|
|
|
try {
|
|
|
|
if (!(errorCode in HTTP_ERROR_CODES)) {
|
2015-10-29 07:41:54 +00:00
|
|
|
dumpn("*** WARNING: requested invalid error: " + errorCode);
|
2022-06-02 04:42:21 +00:00
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
|
|
|
|
// RFC 2616 says that we should try to handle an error by its class if we
|
|
|
|
// can't otherwise handle it -- if that fails, we revert to handling it as
|
|
|
|
// a 500 internal server error, and if that fails we throw and shut down
|
|
|
|
// the server
|
|
|
|
|
|
|
|
// actually handle the error
|
2022-06-02 04:42:21 +00:00
|
|
|
try {
|
|
|
|
if (errorCode in this._overrideErrors) {
|
2015-10-29 07:41:54 +00:00
|
|
|
this._overrideErrors[errorCode](metadata, response);
|
2022-06-02 04:42:21 +00:00
|
|
|
} else {
|
2015-10-29 07:41:54 +00:00
|
|
|
this._defaultErrors[errorCode](metadata, response);
|
2022-06-02 04:42:21 +00:00
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
if (response.partiallySent()) {
|
2015-10-29 07:41:54 +00:00
|
|
|
response.abort(e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// don't retry the handler that threw
|
2022-06-02 04:42:21 +00:00
|
|
|
if (errorX00 == errorCode) {
|
2015-10-29 07:41:54 +00:00
|
|
|
throw HTTP_500;
|
2022-06-02 04:42:21 +00:00
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
|
2022-06-02 04:42:21 +00:00
|
|
|
dumpn(
|
|
|
|
"*** error in handling for error code " +
|
|
|
|
errorCode +
|
|
|
|
", " +
|
|
|
|
"falling back to " +
|
|
|
|
errorX00 +
|
|
|
|
"..."
|
|
|
|
);
|
2015-10-29 07:41:54 +00:00
|
|
|
response = new Response(response._connection);
|
2022-06-02 04:42:21 +00:00
|
|
|
if (errorX00 in this._overrideErrors) {
|
2015-10-29 07:41:54 +00:00
|
|
|
this._overrideErrors[errorX00](metadata, response);
|
2022-06-02 04:42:21 +00:00
|
|
|
} else if (errorX00 in this._defaultErrors) {
|
2015-10-29 07:41:54 +00:00
|
|
|
this._defaultErrors[errorX00](metadata, response);
|
2022-06-02 04:42:21 +00:00
|
|
|
} else {
|
2015-10-29 07:41:54 +00:00
|
|
|
throw HTTP_500;
|
2022-06-02 04:42:21 +00:00
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
}
|
2022-06-02 04:42:21 +00:00
|
|
|
} catch (e) {
|
|
|
|
if (response.partiallySent()) {
|
2015-10-29 07:41:54 +00:00
|
|
|
response.abort();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// we've tried everything possible for a meaningful error -- now try 500
|
2022-06-02 04:42:21 +00:00
|
|
|
dumpn(
|
|
|
|
"*** error in handling for error code " +
|
|
|
|
errorX00 +
|
|
|
|
", falling " +
|
|
|
|
"back to 500..."
|
|
|
|
);
|
|
|
|
|
|
|
|
try {
|
2015-10-29 07:41:54 +00:00
|
|
|
response = new Response(response._connection);
|
2022-06-02 04:42:21 +00:00
|
|
|
if (500 in this._overrideErrors) {
|
2015-10-29 07:41:54 +00:00
|
|
|
this._overrideErrors[500](metadata, response);
|
2022-06-02 04:42:21 +00:00
|
|
|
} else {
|
2015-10-29 07:41:54 +00:00
|
|
|
this._defaultErrors[500](metadata, response);
|
2022-06-02 04:42:21 +00:00
|
|
|
}
|
|
|
|
} catch (e2) {
|
2015-10-29 07:41:54 +00:00
|
|
|
dumpn("*** multiple errors in default error handlers!");
|
|
|
|
dumpn("*** e == " + e + ", e2 == " + e2);
|
|
|
|
response.abort(e2);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
response.complete();
|
|
|
|
},
|
|
|
|
|
|
|
|
// FIELDS
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This object contains the default handlers for the various HTTP error codes.
|
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
_defaultErrors: {
|
|
|
|
400(metadata, response) {
|
2015-10-29 07:41:54 +00:00
|
|
|
// none of the data in metadata is reliable, so hard-code everything here
|
|
|
|
response.setStatusLine("1.1", 400, "Bad Request");
|
|
|
|
response.setHeader("Content-Type", "text/plain;charset=utf-8", false);
|
|
|
|
|
|
|
|
var body = "Bad request\n";
|
|
|
|
response.bodyOutputStream.write(body, body.length);
|
|
|
|
},
|
2022-06-02 04:42:21 +00:00
|
|
|
403(metadata, response) {
|
2015-10-29 07:41:54 +00:00
|
|
|
response.setStatusLine(metadata.httpVersion, 403, "Forbidden");
|
|
|
|
response.setHeader("Content-Type", "text/html;charset=utf-8", false);
|
|
|
|
|
2022-06-02 04:42:21 +00:00
|
|
|
var body =
|
|
|
|
"<html>\
|
2015-10-29 07:41:54 +00:00
|
|
|
<head><title>403 Forbidden</title></head>\
|
|
|
|
<body>\
|
|
|
|
<h1>403 Forbidden</h1>\
|
|
|
|
</body>\
|
|
|
|
</html>";
|
|
|
|
response.bodyOutputStream.write(body, body.length);
|
|
|
|
},
|
2022-06-02 04:42:21 +00:00
|
|
|
404(metadata, response) {
|
2015-10-29 07:41:54 +00:00
|
|
|
response.setStatusLine(metadata.httpVersion, 404, "Not Found");
|
|
|
|
response.setHeader("Content-Type", "text/html;charset=utf-8", false);
|
|
|
|
|
2022-06-02 04:42:21 +00:00
|
|
|
var body =
|
|
|
|
"<html>\
|
2015-10-29 07:41:54 +00:00
|
|
|
<head><title>404 Not Found</title></head>\
|
|
|
|
<body>\
|
|
|
|
<h1>404 Not Found</h1>\
|
|
|
|
<p>\
|
|
|
|
<span style='font-family: monospace;'>" +
|
2022-06-02 04:42:21 +00:00
|
|
|
htmlEscape(metadata.path) +
|
|
|
|
"</span> was not found.\
|
2015-10-29 07:41:54 +00:00
|
|
|
</p>\
|
|
|
|
</body>\
|
|
|
|
</html>";
|
|
|
|
response.bodyOutputStream.write(body, body.length);
|
|
|
|
},
|
2022-06-02 04:42:21 +00:00
|
|
|
416(metadata, response) {
|
|
|
|
response.setStatusLine(
|
|
|
|
metadata.httpVersion,
|
|
|
|
416,
|
|
|
|
"Requested Range Not Satisfiable"
|
|
|
|
);
|
2015-10-29 07:41:54 +00:00
|
|
|
response.setHeader("Content-Type", "text/html;charset=utf-8", false);
|
|
|
|
|
2022-06-02 04:42:21 +00:00
|
|
|
var body =
|
|
|
|
"<html>\
|
2015-10-29 07:41:54 +00:00
|
|
|
<head>\
|
|
|
|
<title>416 Requested Range Not Satisfiable</title></head>\
|
|
|
|
<body>\
|
|
|
|
<h1>416 Requested Range Not Satisfiable</h1>\
|
|
|
|
<p>The byte range was not valid for the\
|
|
|
|
requested resource.\
|
|
|
|
</p>\
|
|
|
|
</body>\
|
|
|
|
</html>";
|
|
|
|
response.bodyOutputStream.write(body, body.length);
|
|
|
|
},
|
2022-06-02 04:42:21 +00:00
|
|
|
500(metadata, response) {
|
|
|
|
response.setStatusLine(
|
|
|
|
metadata.httpVersion,
|
|
|
|
500,
|
|
|
|
"Internal Server Error"
|
|
|
|
);
|
2015-10-29 07:41:54 +00:00
|
|
|
response.setHeader("Content-Type", "text/html;charset=utf-8", false);
|
|
|
|
|
2022-06-02 04:42:21 +00:00
|
|
|
var body =
|
|
|
|
"<html>\
|
2015-10-29 07:41:54 +00:00
|
|
|
<head><title>500 Internal Server Error</title></head>\
|
|
|
|
<body>\
|
|
|
|
<h1>500 Internal Server Error</h1>\
|
|
|
|
<p>Something's broken in this server and\
|
|
|
|
needs to be fixed.</p>\
|
|
|
|
</body>\
|
|
|
|
</html>";
|
|
|
|
response.bodyOutputStream.write(body, body.length);
|
|
|
|
},
|
2022-06-02 04:42:21 +00:00
|
|
|
501(metadata, response) {
|
2015-10-29 07:41:54 +00:00
|
|
|
response.setStatusLine(metadata.httpVersion, 501, "Not Implemented");
|
|
|
|
response.setHeader("Content-Type", "text/html;charset=utf-8", false);
|
|
|
|
|
2022-06-02 04:42:21 +00:00
|
|
|
var body =
|
|
|
|
"<html>\
|
2015-10-29 07:41:54 +00:00
|
|
|
<head><title>501 Not Implemented</title></head>\
|
|
|
|
<body>\
|
|
|
|
<h1>501 Not Implemented</h1>\
|
|
|
|
<p>This server is not (yet) Apache.</p>\
|
|
|
|
</body>\
|
|
|
|
</html>";
|
|
|
|
response.bodyOutputStream.write(body, body.length);
|
|
|
|
},
|
2022-06-02 04:42:21 +00:00
|
|
|
505(metadata, response) {
|
2015-10-29 07:41:54 +00:00
|
|
|
response.setStatusLine("1.1", 505, "HTTP Version Not Supported");
|
|
|
|
response.setHeader("Content-Type", "text/html;charset=utf-8", false);
|
|
|
|
|
2022-06-02 04:42:21 +00:00
|
|
|
var body =
|
|
|
|
"<html>\
|
2015-10-29 07:41:54 +00:00
|
|
|
<head><title>505 HTTP Version Not Supported</title></head>\
|
|
|
|
<body>\
|
|
|
|
<h1>505 HTTP Version Not Supported</h1>\
|
|
|
|
<p>This server only supports HTTP/1.0 and HTTP/1.1\
|
|
|
|
connections.</p>\
|
|
|
|
</body>\
|
|
|
|
</html>";
|
|
|
|
response.bodyOutputStream.write(body, body.length);
|
2022-06-02 04:42:21 +00:00
|
|
|
},
|
2015-10-29 07:41:54 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Contains handlers for the default set of URIs contained in this server.
|
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
_defaultPaths: {
|
|
|
|
"/": function(metadata, response) {
|
2015-10-29 07:41:54 +00:00
|
|
|
response.setStatusLine(metadata.httpVersion, 200, "OK");
|
|
|
|
response.setHeader("Content-Type", "text/html;charset=utf-8", false);
|
|
|
|
|
2022-06-02 04:42:21 +00:00
|
|
|
var body =
|
|
|
|
"<html>\
|
2015-10-29 07:41:54 +00:00
|
|
|
<head><title>httpd.js</title></head>\
|
|
|
|
<body>\
|
|
|
|
<h1>httpd.js</h1>\
|
|
|
|
<p>If you're seeing this page, httpd.js is up and\
|
|
|
|
serving requests! Now set a base path and serve some\
|
|
|
|
files!</p>\
|
|
|
|
</body>\
|
|
|
|
</html>";
|
|
|
|
|
|
|
|
response.bodyOutputStream.write(body, body.length);
|
|
|
|
},
|
|
|
|
|
2022-06-02 04:42:21 +00:00
|
|
|
"/trace": function(metadata, response) {
|
2015-10-29 07:41:54 +00:00
|
|
|
response.setStatusLine(metadata.httpVersion, 200, "OK");
|
|
|
|
response.setHeader("Content-Type", "text/plain;charset=utf-8", false);
|
|
|
|
|
2022-06-02 04:42:21 +00:00
|
|
|
var body =
|
|
|
|
"Request-URI: " +
|
|
|
|
metadata.scheme +
|
|
|
|
"://" +
|
|
|
|
metadata.host +
|
|
|
|
":" +
|
|
|
|
metadata.port +
|
|
|
|
metadata.path +
|
|
|
|
"\n\n";
|
2015-10-29 07:41:54 +00:00
|
|
|
body += "Request (semantically equivalent, slightly reformatted):\n\n";
|
|
|
|
body += metadata.method + " " + metadata.path;
|
|
|
|
|
2022-06-02 04:42:21 +00:00
|
|
|
if (metadata.queryString) {
|
|
|
|
body += "?" + metadata.queryString;
|
|
|
|
}
|
|
|
|
|
2015-10-29 07:41:54 +00:00
|
|
|
body += " HTTP/" + metadata.httpVersion + "\r\n";
|
|
|
|
|
|
|
|
var headEnum = metadata.headers;
|
2022-06-02 04:42:21 +00:00
|
|
|
while (headEnum.hasMoreElements()) {
|
|
|
|
var fieldName = headEnum.getNext().QueryInterface(Ci.nsISupportsString)
|
|
|
|
.data;
|
2015-10-29 07:41:54 +00:00
|
|
|
body += fieldName + ": " + metadata.getHeader(fieldName) + "\r\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
response.bodyOutputStream.write(body, body.length);
|
2022-06-02 04:42:21 +00:00
|
|
|
},
|
|
|
|
},
|
2015-10-29 07:41:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2019-08-27 09:30:04 +00:00
|
|
|
* Maps absolute paths to files on the local file system (as nsILocalFiles).
|
2015-10-29 07:41:54 +00:00
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
function FileMap() {
|
2019-08-27 09:30:04 +00:00
|
|
|
/** Hash which will map paths to nsILocalFiles. */
|
2015-10-29 07:41:54 +00:00
|
|
|
this._map = {};
|
|
|
|
}
|
2022-06-02 04:42:21 +00:00
|
|
|
FileMap.prototype = {
|
2015-10-29 07:41:54 +00:00
|
|
|
// PUBLIC API
|
|
|
|
|
|
|
|
/**
|
2019-08-25 10:51:11 +00:00
|
|
|
* Maps key to a clone of the nsIFile value if value is non-null;
|
2015-10-29 07:41:54 +00:00
|
|
|
* otherwise, removes any extant mapping for key.
|
|
|
|
*
|
|
|
|
* @param key : string
|
|
|
|
* string to which a clone of value is mapped
|
2019-08-25 10:51:11 +00:00
|
|
|
* @param value : nsIFile
|
2015-10-29 07:41:54 +00:00
|
|
|
* the file to map to key, or null to remove a mapping
|
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
put(key, value) {
|
|
|
|
if (value) {
|
2015-10-29 07:41:54 +00:00
|
|
|
this._map[key] = value.clone();
|
2022-06-02 04:42:21 +00:00
|
|
|
} else {
|
2015-10-29 07:41:54 +00:00
|
|
|
delete this._map[key];
|
2022-06-02 04:42:21 +00:00
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2019-08-25 10:51:11 +00:00
|
|
|
* Returns a clone of the nsIFile mapped to key, or null if no such
|
2015-10-29 07:41:54 +00:00
|
|
|
* mapping exists.
|
|
|
|
*
|
|
|
|
* @param key : string
|
|
|
|
* key to which the returned file maps
|
2019-08-25 10:51:11 +00:00
|
|
|
* @returns nsIFile
|
2015-10-29 07:41:54 +00:00
|
|
|
* a clone of the mapped file, or null if no mapping exists
|
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
get(key) {
|
2015-10-29 07:41:54 +00:00
|
|
|
var val = this._map[key];
|
|
|
|
return val ? val.clone() : null;
|
2022-06-02 04:42:21 +00:00
|
|
|
},
|
2015-10-29 07:41:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Response CONSTANTS
|
|
|
|
|
|
|
|
// token = *<any CHAR except CTLs or separators>
|
|
|
|
// CHAR = <any US-ASCII character (0-127)>
|
|
|
|
// CTL = <any US-ASCII control character (0-31) and DEL (127)>
|
|
|
|
// separators = "(" | ")" | "<" | ">" | "@"
|
|
|
|
// | "," | ";" | ":" | "\" | <">
|
|
|
|
// | "/" | "[" | "]" | "?" | "="
|
|
|
|
// | "{" | "}" | SP | HT
|
2022-06-02 04:42:21 +00:00
|
|
|
const IS_TOKEN_ARRAY = [
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0, // 0
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0, // 8
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0, // 16
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0, // 24
|
|
|
|
|
|
|
|
0,
|
|
|
|
1,
|
|
|
|
0,
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
1, // 32
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
0,
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
0, // 40
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
1, // 48
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0, // 56
|
|
|
|
|
|
|
|
0,
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
1, // 64
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
1, // 72
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
1, // 80
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
1,
|
|
|
|
1, // 88
|
|
|
|
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
1, // 96
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
1, // 104
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
1, // 112
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
0,
|
|
|
|
1,
|
|
|
|
0,
|
|
|
|
1,
|
|
|
|
]; // 120
|
2015-10-29 07:41:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines whether the given character code is a CTL.
|
|
|
|
*
|
|
|
|
* @param code : uint
|
|
|
|
* the character code
|
|
|
|
* @returns boolean
|
|
|
|
* true if code is a CTL, false otherwise
|
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
function isCTL(code) {
|
|
|
|
return (code >= 0 && code <= 31) || code == 127;
|
2015-10-29 07:41:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents a response to an HTTP request, encapsulating all details of that
|
|
|
|
* response. This includes all headers, the HTTP version, status code and
|
|
|
|
* explanation, and the entity itself.
|
|
|
|
*
|
|
|
|
* @param connection : Connection
|
|
|
|
* the connection over which this response is to be written
|
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
function Response(connection) {
|
2015-10-29 07:41:54 +00:00
|
|
|
/** The connection over which this response will be written. */
|
|
|
|
this._connection = connection;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The HTTP version of this response; defaults to 1.1 if not set by the
|
|
|
|
* handler.
|
|
|
|
*/
|
|
|
|
this._httpVersion = nsHttpVersion.HTTP_1_1;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The HTTP code of this response; defaults to 200.
|
|
|
|
*/
|
|
|
|
this._httpCode = 200;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The description of the HTTP code in this response; defaults to "OK".
|
|
|
|
*/
|
|
|
|
this._httpDescription = "OK";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An nsIHttpHeaders object in which the headers in this response should be
|
|
|
|
* stored. This property is null after the status line and headers have been
|
|
|
|
* written to the network, and it may be modified up until it is cleared,
|
|
|
|
* except if this._finished is set first (in which case headers are written
|
|
|
|
* asynchronously in response to a finish() call not preceded by
|
|
|
|
* flushHeaders()).
|
|
|
|
*/
|
|
|
|
this._headers = new nsHttpHeaders();
|
|
|
|
|
2022-06-02 04:42:21 +00:00
|
|
|
/**
|
|
|
|
* Informational response:
|
|
|
|
* For example 103 Early Hint
|
|
|
|
**/
|
|
|
|
this._informationalResponseHttpVersion = nsHttpVersion.HTTP_1_1;
|
|
|
|
this._informationalResponseHttpCode = 0;
|
|
|
|
this._informationalResponseHttpDescription = "";
|
|
|
|
this._informationalResponseHeaders = new nsHttpHeaders();
|
|
|
|
this._informationalResponseSet = false;
|
|
|
|
|
2015-10-29 07:41:54 +00:00
|
|
|
/**
|
|
|
|
* Set to true when this response is ended (completely constructed if possible
|
|
|
|
* and the connection closed); further actions on this will then fail.
|
|
|
|
*/
|
|
|
|
this._ended = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A stream used to hold data written to the body of this response.
|
|
|
|
*/
|
|
|
|
this._bodyOutputStream = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A stream containing all data that has been written to the body of this
|
|
|
|
* response so far. (Async handlers make the data contained in this
|
|
|
|
* unreliable as a way of determining content length in general, but auxiliary
|
|
|
|
* saved information can sometimes be used to guarantee reliability.)
|
|
|
|
*/
|
|
|
|
this._bodyInputStream = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A stream copier which copies data to the network. It is initially null
|
|
|
|
* until replaced with a copier for response headers; when headers have been
|
|
|
|
* fully sent it is replaced with a copier for the response body, remaining
|
|
|
|
* so for the duration of response processing.
|
|
|
|
*/
|
|
|
|
this._asyncCopier = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* True if this response has been designated as being processed
|
|
|
|
* asynchronously rather than for the duration of a single call to
|
|
|
|
* nsIHttpRequestHandler.handle.
|
|
|
|
*/
|
|
|
|
this._processAsync = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* True iff finish() has been called on this, signaling that no more changes
|
|
|
|
* to this may be made.
|
|
|
|
*/
|
|
|
|
this._finished = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* True iff powerSeized() has been called on this, signaling that this
|
|
|
|
* response is to be handled manually by the response handler (which may then
|
|
|
|
* send arbitrary data in response, even non-HTTP responses).
|
|
|
|
*/
|
|
|
|
this._powerSeized = false;
|
|
|
|
}
|
2022-06-02 04:42:21 +00:00
|
|
|
Response.prototype = {
|
2015-10-29 07:41:54 +00:00
|
|
|
// PUBLIC CONSTRUCTION API
|
|
|
|
|
|
|
|
//
|
|
|
|
// see nsIHttpResponse.bodyOutputStream
|
|
|
|
//
|
2022-06-02 04:42:21 +00:00
|
|
|
get bodyOutputStream() {
|
|
|
|
if (this._finished) {
|
|
|
|
throw Components.Exception("", Cr.NS_ERROR_NOT_AVAILABLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this._bodyOutputStream) {
|
|
|
|
var pipe = new Pipe(
|
|
|
|
true,
|
|
|
|
false,
|
|
|
|
Response.SEGMENT_SIZE,
|
|
|
|
PR_UINT32_MAX,
|
|
|
|
null
|
|
|
|
);
|
2015-10-29 07:41:54 +00:00
|
|
|
this._bodyOutputStream = pipe.outputStream;
|
|
|
|
this._bodyInputStream = pipe.inputStream;
|
2022-06-02 04:42:21 +00:00
|
|
|
if (this._processAsync || this._powerSeized) {
|
2015-10-29 07:41:54 +00:00
|
|
|
this._startAsyncProcessor();
|
2022-06-02 04:42:21 +00:00
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return this._bodyOutputStream;
|
|
|
|
},
|
|
|
|
|
|
|
|
//
|
|
|
|
// see nsIHttpResponse.write
|
|
|
|
//
|
2022-06-02 04:42:21 +00:00
|
|
|
write(data) {
|
|
|
|
if (this._finished) {
|
|
|
|
throw Components.Exception("", Cr.NS_ERROR_NOT_AVAILABLE);
|
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
|
|
|
|
var dataAsString = String(data);
|
|
|
|
this.bodyOutputStream.write(dataAsString, dataAsString.length);
|
|
|
|
},
|
|
|
|
|
|
|
|
//
|
|
|
|
// see nsIHttpResponse.setStatusLine
|
|
|
|
//
|
2022-06-02 04:42:21 +00:00
|
|
|
setStatusLineInternal(httpVersion, code, description, informationalResponse) {
|
|
|
|
if (this._finished || this._powerSeized) {
|
|
|
|
throw Components.Exception("", Cr.NS_ERROR_NOT_AVAILABLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!informationalResponse) {
|
|
|
|
if (!this._headers) {
|
|
|
|
throw Components.Exception("", Cr.NS_ERROR_NOT_AVAILABLE);
|
|
|
|
}
|
|
|
|
} else if (!this._informationalResponseHeaders) {
|
|
|
|
throw Components.Exception("", Cr.NS_ERROR_NOT_AVAILABLE);
|
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
this._ensureAlive();
|
|
|
|
|
2022-06-02 04:42:21 +00:00
|
|
|
if (!(code >= 0 && code < 1000)) {
|
|
|
|
throw Components.Exception("", Cr.NS_ERROR_INVALID_ARG);
|
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
|
2022-06-02 04:42:21 +00:00
|
|
|
try {
|
2015-10-29 07:41:54 +00:00
|
|
|
var httpVer;
|
|
|
|
// avoid version construction for the most common cases
|
2022-06-02 04:42:21 +00:00
|
|
|
if (!httpVersion || httpVersion == "1.1") {
|
2015-10-29 07:41:54 +00:00
|
|
|
httpVer = nsHttpVersion.HTTP_1_1;
|
2022-06-02 04:42:21 +00:00
|
|
|
} else if (httpVersion == "1.0") {
|
2015-10-29 07:41:54 +00:00
|
|
|
httpVer = nsHttpVersion.HTTP_1_0;
|
2022-06-02 04:42:21 +00:00
|
|
|
} else {
|
2015-10-29 07:41:54 +00:00
|
|
|
httpVer = new nsHttpVersion(httpVersion);
|
2022-06-02 04:42:21 +00:00
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
throw Components.Exception("", Cr.NS_ERROR_INVALID_ARG);
|
2015-10-29 07:41:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Reason-Phrase = *<TEXT, excluding CR, LF>
|
|
|
|
// TEXT = <any OCTET except CTLs, but including LWS>
|
|
|
|
//
|
|
|
|
// XXX this ends up disallowing octets which aren't Unicode, I think -- not
|
|
|
|
// much to do if description is IDL'd as string
|
2022-06-02 04:42:21 +00:00
|
|
|
if (!description) {
|
2015-10-29 07:41:54 +00:00
|
|
|
description = "";
|
2022-06-02 04:42:21 +00:00
|
|
|
}
|
|
|
|
for (var i = 0; i < description.length; i++) {
|
|
|
|
if (isCTL(description.charCodeAt(i)) && description.charAt(i) != "\t") {
|
|
|
|
throw Components.Exception("", Cr.NS_ERROR_INVALID_ARG);
|
|
|
|
}
|
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
|
|
|
|
// set the values only after validation to preserve atomicity
|
2022-06-02 04:42:21 +00:00
|
|
|
if (!informationalResponse) {
|
|
|
|
this._httpDescription = description;
|
|
|
|
this._httpCode = code;
|
|
|
|
this._httpVersion = httpVer;
|
|
|
|
} else {
|
|
|
|
this._informationalResponseSet = true;
|
|
|
|
this._informationalResponseHttpDescription = description;
|
|
|
|
this._informationalResponseHttpCode = code;
|
|
|
|
this._informationalResponseHttpVersion = httpVer;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
//
|
|
|
|
// see nsIHttpResponse.setStatusLine
|
|
|
|
//
|
|
|
|
setStatusLine(httpVersion, code, description) {
|
|
|
|
this.setStatusLineInternal(httpVersion, code, description, false);
|
|
|
|
},
|
|
|
|
|
|
|
|
setInformationalResponseStatusLine(httpVersion, code, description) {
|
|
|
|
this.setStatusLineInternal(httpVersion, code, description, true);
|
2015-10-29 07:41:54 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
//
|
|
|
|
// see nsIHttpResponse.setHeader
|
|
|
|
//
|
2022-06-02 04:42:21 +00:00
|
|
|
setHeader(name, value, merge) {
|
|
|
|
if (!this._headers || this._finished || this._powerSeized) {
|
|
|
|
throw Components.Exception("", Cr.NS_ERROR_NOT_AVAILABLE);
|
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
this._ensureAlive();
|
|
|
|
|
|
|
|
this._headers.setHeader(name, value, merge);
|
|
|
|
},
|
|
|
|
|
2022-06-02 04:42:21 +00:00
|
|
|
setInformationalResponseHeader(name, value, merge) {
|
|
|
|
if (
|
|
|
|
!this._informationalResponseHeaders ||
|
|
|
|
this._finished ||
|
|
|
|
this._powerSeized
|
|
|
|
) {
|
|
|
|
throw Components.Exception("", Cr.NS_ERROR_NOT_AVAILABLE);
|
|
|
|
}
|
|
|
|
this._ensureAlive();
|
|
|
|
|
|
|
|
this._informationalResponseHeaders.setHeader(name, value, merge);
|
|
|
|
},
|
|
|
|
|
|
|
|
setHeaderNoCheck(name, value) {
|
|
|
|
if (!this._headers || this._finished || this._powerSeized) {
|
|
|
|
throw Components.Exception("", Cr.NS_ERROR_NOT_AVAILABLE);
|
|
|
|
}
|
2019-08-27 09:30:04 +00:00
|
|
|
this._ensureAlive();
|
|
|
|
|
|
|
|
this._headers.setHeaderNoCheck(name, value);
|
|
|
|
},
|
|
|
|
|
2022-06-02 04:42:21 +00:00
|
|
|
setInformationalHeaderNoCheck(name, value) {
|
|
|
|
if (!this._headers || this._finished || this._powerSeized) {
|
|
|
|
throw Components.Exception("", Cr.NS_ERROR_NOT_AVAILABLE);
|
|
|
|
}
|
|
|
|
this._ensureAlive();
|
|
|
|
|
|
|
|
this._informationalResponseHeaders.setHeaderNoCheck(name, value);
|
|
|
|
},
|
|
|
|
|
2015-10-29 07:41:54 +00:00
|
|
|
//
|
|
|
|
// see nsIHttpResponse.processAsync
|
|
|
|
//
|
2022-06-02 04:42:21 +00:00
|
|
|
processAsync() {
|
|
|
|
if (this._finished) {
|
|
|
|
throw Components.Exception("", Cr.NS_ERROR_UNEXPECTED);
|
|
|
|
}
|
|
|
|
if (this._powerSeized) {
|
|
|
|
throw Components.Exception("", Cr.NS_ERROR_NOT_AVAILABLE);
|
|
|
|
}
|
|
|
|
if (this._processAsync) {
|
2015-10-29 07:41:54 +00:00
|
|
|
return;
|
2022-06-02 04:42:21 +00:00
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
this._ensureAlive();
|
|
|
|
|
|
|
|
dumpn("*** processing connection " + this._connection.number + " async");
|
|
|
|
this._processAsync = true;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Either the bodyOutputStream getter or this method is responsible for
|
|
|
|
* starting the asynchronous processor and catching writes of data to the
|
|
|
|
* response body of async responses as they happen, for the purpose of
|
|
|
|
* forwarding those writes to the actual connection's output stream.
|
|
|
|
* If bodyOutputStream is accessed first, calling this method will create
|
|
|
|
* the processor (when it first is clear that body data is to be written
|
|
|
|
* immediately, not buffered). If this method is called first, accessing
|
|
|
|
* bodyOutputStream will create the processor. If only this method is
|
|
|
|
* called, we'll write nothing, neither headers nor the nonexistent body,
|
|
|
|
* until finish() is called. Since that delay is easily avoided by simply
|
|
|
|
* getting bodyOutputStream or calling write(""), we don't worry about it.
|
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
if (this._bodyOutputStream && !this._asyncCopier) {
|
2015-10-29 07:41:54 +00:00
|
|
|
this._startAsyncProcessor();
|
2022-06-02 04:42:21 +00:00
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
//
|
|
|
|
// see nsIHttpResponse.seizePower
|
|
|
|
//
|
2022-06-02 04:42:21 +00:00
|
|
|
seizePower() {
|
|
|
|
if (this._processAsync) {
|
|
|
|
throw Components.Exception("", Cr.NS_ERROR_NOT_AVAILABLE);
|
|
|
|
}
|
|
|
|
if (this._finished) {
|
|
|
|
throw Components.Exception("", Cr.NS_ERROR_UNEXPECTED);
|
|
|
|
}
|
|
|
|
if (this._powerSeized) {
|
2015-10-29 07:41:54 +00:00
|
|
|
return;
|
2022-06-02 04:42:21 +00:00
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
this._ensureAlive();
|
|
|
|
|
2022-06-02 04:42:21 +00:00
|
|
|
dumpn(
|
|
|
|
"*** forcefully seizing power over connection " +
|
|
|
|
this._connection.number +
|
|
|
|
"..."
|
|
|
|
);
|
2015-10-29 07:41:54 +00:00
|
|
|
|
|
|
|
// Purge any already-written data without sending it. We could as easily
|
|
|
|
// swap out the streams entirely, but that makes it possible to acquire and
|
|
|
|
// unknowingly use a stale reference, so we require there only be one of
|
|
|
|
// each stream ever for any response to avoid this complication.
|
2022-06-02 04:42:21 +00:00
|
|
|
if (this._asyncCopier) {
|
2015-10-29 07:41:54 +00:00
|
|
|
this._asyncCopier.cancel(Cr.NS_BINDING_ABORTED);
|
2022-06-02 04:42:21 +00:00
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
this._asyncCopier = null;
|
2022-06-02 04:42:21 +00:00
|
|
|
if (this._bodyOutputStream) {
|
2015-10-29 07:41:54 +00:00
|
|
|
var input = new BinaryInputStream(this._bodyInputStream);
|
|
|
|
var avail;
|
2022-06-02 04:42:21 +00:00
|
|
|
while ((avail = input.available()) > 0) {
|
2015-10-29 07:41:54 +00:00
|
|
|
input.readByteArray(avail);
|
2022-06-02 04:42:21 +00:00
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this._powerSeized = true;
|
2022-06-02 04:42:21 +00:00
|
|
|
if (this._bodyOutputStream) {
|
2015-10-29 07:41:54 +00:00
|
|
|
this._startAsyncProcessor();
|
2022-06-02 04:42:21 +00:00
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
//
|
|
|
|
// see nsIHttpResponse.finish
|
|
|
|
//
|
2022-06-02 04:42:21 +00:00
|
|
|
finish() {
|
|
|
|
if (!this._processAsync && !this._powerSeized) {
|
|
|
|
throw Components.Exception("", Cr.NS_ERROR_UNEXPECTED);
|
|
|
|
}
|
|
|
|
if (this._finished) {
|
2015-10-29 07:41:54 +00:00
|
|
|
return;
|
2022-06-02 04:42:21 +00:00
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
|
|
|
|
dumpn("*** finishing connection " + this._connection.number);
|
|
|
|
this._startAsyncProcessor(); // in case bodyOutputStream was never accessed
|
2022-06-02 04:42:21 +00:00
|
|
|
if (this._bodyOutputStream) {
|
2015-10-29 07:41:54 +00:00
|
|
|
this._bodyOutputStream.close();
|
2022-06-02 04:42:21 +00:00
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
this._finished = true;
|
|
|
|
},
|
|
|
|
|
|
|
|
// NSISUPPORTS
|
|
|
|
|
|
|
|
//
|
|
|
|
// see nsISupports.QueryInterface
|
|
|
|
//
|
2022-06-02 04:42:21 +00:00
|
|
|
QueryInterface: ChromeUtils.generateQI(["nsIHttpResponse"]),
|
2015-10-29 07:41:54 +00:00
|
|
|
|
|
|
|
// POST-CONSTRUCTION API (not exposed externally)
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The HTTP version number of this, as a string (e.g. "1.1").
|
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
get httpVersion() {
|
2015-10-29 07:41:54 +00:00
|
|
|
this._ensureAlive();
|
|
|
|
return this._httpVersion.toString();
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The HTTP status code of this response, as a string of three characters per
|
|
|
|
* RFC 2616.
|
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
get httpCode() {
|
2015-10-29 07:41:54 +00:00
|
|
|
this._ensureAlive();
|
|
|
|
|
2022-06-02 04:42:21 +00:00
|
|
|
var codeString =
|
|
|
|
(this._httpCode < 10 ? "0" : "") +
|
|
|
|
(this._httpCode < 100 ? "0" : "") +
|
|
|
|
this._httpCode;
|
2015-10-29 07:41:54 +00:00
|
|
|
return codeString;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The description of the HTTP status code of this response, or "" if none is
|
|
|
|
* set.
|
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
get httpDescription() {
|
2015-10-29 07:41:54 +00:00
|
|
|
this._ensureAlive();
|
|
|
|
|
|
|
|
return this._httpDescription;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The headers in this response, as an nsHttpHeaders object.
|
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
get headers() {
|
2015-10-29 07:41:54 +00:00
|
|
|
this._ensureAlive();
|
|
|
|
|
|
|
|
return this._headers;
|
|
|
|
},
|
|
|
|
|
|
|
|
//
|
|
|
|
// see nsHttpHeaders.getHeader
|
|
|
|
//
|
2022-06-02 04:42:21 +00:00
|
|
|
getHeader(name) {
|
2015-10-29 07:41:54 +00:00
|
|
|
this._ensureAlive();
|
|
|
|
|
|
|
|
return this._headers.getHeader(name);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines whether this response may be abandoned in favor of a newly
|
|
|
|
* constructed response. A response may be abandoned only if it is not being
|
|
|
|
* sent asynchronously and if raw control over it has not been taken from the
|
|
|
|
* server.
|
|
|
|
*
|
|
|
|
* @returns boolean
|
|
|
|
* true iff no data has been written to the network
|
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
partiallySent() {
|
2015-10-29 07:41:54 +00:00
|
|
|
dumpn("*** partiallySent()");
|
|
|
|
return this._processAsync || this._powerSeized;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If necessary, kicks off the remaining request processing needed to be done
|
|
|
|
* after a request handler performs its initial work upon this response.
|
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
complete() {
|
2015-10-29 07:41:54 +00:00
|
|
|
dumpn("*** complete()");
|
2022-06-02 04:42:21 +00:00
|
|
|
if (this._processAsync || this._powerSeized) {
|
|
|
|
NS_ASSERT(
|
|
|
|
this._processAsync ^ this._powerSeized,
|
|
|
|
"can't both send async and relinquish power"
|
|
|
|
);
|
2015-10-29 07:41:54 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ASSERT(!this.partiallySent(), "completing a partially-sent response?");
|
|
|
|
|
|
|
|
this._startAsyncProcessor();
|
|
|
|
|
|
|
|
// Now make sure we finish processing this request!
|
2022-06-02 04:42:21 +00:00
|
|
|
if (this._bodyOutputStream) {
|
2015-10-29 07:41:54 +00:00
|
|
|
this._bodyOutputStream.close();
|
2022-06-02 04:42:21 +00:00
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Abruptly ends processing of this response, usually due to an error in an
|
|
|
|
* incoming request but potentially due to a bad error handler. Since we
|
|
|
|
* cannot handle the error in the usual way (giving an HTTP error page in
|
|
|
|
* response) because data may already have been sent (or because the response
|
|
|
|
* might be expected to have been generated asynchronously or completely from
|
|
|
|
* scratch by the handler), we stop processing this response and abruptly
|
|
|
|
* close the connection.
|
|
|
|
*
|
|
|
|
* @param e : Error
|
|
|
|
* the exception which precipitated this abort, or null if no such exception
|
|
|
|
* was generated
|
2022-06-02 04:42:21 +00:00
|
|
|
* @param truncateConnection : Boolean
|
|
|
|
* ensures that we truncate the connection using an RST packet, so the
|
|
|
|
* client testing code is aware that an error occurred, otherwise it may
|
|
|
|
* consider the response as valid.
|
2015-10-29 07:41:54 +00:00
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
abort(e, truncateConnection = false) {
|
2015-10-29 07:41:54 +00:00
|
|
|
dumpn("*** abort(<" + e + ">)");
|
|
|
|
|
2022-06-02 04:42:21 +00:00
|
|
|
if (truncateConnection) {
|
|
|
|
dumpn("*** truncate connection");
|
|
|
|
this._connection.transport.setLinger(true, 0);
|
|
|
|
}
|
|
|
|
|
2015-10-29 07:41:54 +00:00
|
|
|
// This response will be ended by the processor if one was created.
|
|
|
|
var copier = this._asyncCopier;
|
2022-06-02 04:42:21 +00:00
|
|
|
if (copier) {
|
2015-10-29 07:41:54 +00:00
|
|
|
// We dispatch asynchronously here so that any pending writes of data to
|
|
|
|
// the connection will be deterministically written. This makes it easier
|
|
|
|
// to specify exact behavior, and it makes observable behavior more
|
|
|
|
// predictable for clients. Note that the correctness of this depends on
|
|
|
|
// callbacks in response to _waitToReadData in WriteThroughCopier
|
|
|
|
// happening asynchronously with respect to the actual writing of data to
|
|
|
|
// bodyOutputStream, as they currently do; if they happened synchronously,
|
|
|
|
// an event which ran before this one could write more data to the
|
|
|
|
// response body before we get around to canceling the copier. We have
|
|
|
|
// tests for this in test_seizepower.js, however, and I can't think of a
|
|
|
|
// way to handle both cases without removing bodyOutputStream access and
|
|
|
|
// moving its effective write(data, length) method onto Response, which
|
|
|
|
// would be slower and require more code than this anyway.
|
2022-06-02 04:42:21 +00:00
|
|
|
gThreadManager.currentThread.dispatch(
|
2015-10-29 07:41:54 +00:00
|
|
|
{
|
2022-06-02 04:42:21 +00:00
|
|
|
run() {
|
|
|
|
dumpn("*** canceling copy asynchronously...");
|
|
|
|
copier.cancel(Cr.NS_ERROR_UNEXPECTED);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Ci.nsIThread.DISPATCH_NORMAL
|
|
|
|
);
|
|
|
|
} else {
|
2015-10-29 07:41:54 +00:00
|
|
|
this.end();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Closes this response's network connection, marks the response as finished,
|
|
|
|
* and notifies the server handler that the request is done being processed.
|
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
end() {
|
2015-10-29 07:41:54 +00:00
|
|
|
NS_ASSERT(!this._ended, "ending this response twice?!?!");
|
|
|
|
|
|
|
|
this._connection.close();
|
2022-06-02 04:42:21 +00:00
|
|
|
if (this._bodyOutputStream) {
|
2015-10-29 07:41:54 +00:00
|
|
|
this._bodyOutputStream.close();
|
2022-06-02 04:42:21 +00:00
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
|
|
|
|
this._finished = true;
|
|
|
|
this._ended = true;
|
|
|
|
},
|
|
|
|
|
|
|
|
// PRIVATE IMPLEMENTATION
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends the status line and headers of this response if they haven't been
|
|
|
|
* sent and initiates the process of copying data written to this response's
|
|
|
|
* body to the network.
|
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
_startAsyncProcessor() {
|
2015-10-29 07:41:54 +00:00
|
|
|
dumpn("*** _startAsyncProcessor()");
|
|
|
|
|
|
|
|
// Handle cases where we're being called a second time. The former case
|
|
|
|
// happens when this is triggered both by complete() and by processAsync(),
|
|
|
|
// while the latter happens when processAsync() in conjunction with sent
|
|
|
|
// data causes abort() to be called.
|
2022-06-02 04:42:21 +00:00
|
|
|
if (this._asyncCopier || this._ended) {
|
2015-10-29 07:41:54 +00:00
|
|
|
dumpn("*** ignoring second call to _startAsyncProcessor");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send headers if they haven't been sent already and should be sent, then
|
|
|
|
// asynchronously continue to send the body.
|
2022-06-02 04:42:21 +00:00
|
|
|
if (this._headers && !this._powerSeized) {
|
2015-10-29 07:41:54 +00:00
|
|
|
this._sendHeaders();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._headers = null;
|
|
|
|
this._sendBody();
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Signals that all modifications to the response status line and headers are
|
|
|
|
* complete and then sends that data over the network to the client. Once
|
|
|
|
* this method completes, a different response to the request that resulted
|
|
|
|
* in this response cannot be sent -- the only possible action in case of
|
|
|
|
* error is to abort the response and close the connection.
|
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
_sendHeaders() {
|
2015-10-29 07:41:54 +00:00
|
|
|
dumpn("*** _sendHeaders()");
|
|
|
|
|
|
|
|
NS_ASSERT(this._headers);
|
2022-06-02 04:42:21 +00:00
|
|
|
NS_ASSERT(this._informationalResponseHeaders);
|
2015-10-29 07:41:54 +00:00
|
|
|
NS_ASSERT(!this._powerSeized);
|
|
|
|
|
2022-06-02 04:42:21 +00:00
|
|
|
var preambleData = [];
|
|
|
|
|
|
|
|
// Informational response, e.g. 103
|
|
|
|
if (this._informationalResponseSet) {
|
|
|
|
// request-line
|
|
|
|
let statusLine =
|
|
|
|
"HTTP/" +
|
|
|
|
this._informationalResponseHttpVersion +
|
|
|
|
" " +
|
|
|
|
this._informationalResponseHttpCode +
|
|
|
|
" " +
|
|
|
|
this._informationalResponseHttpDescription +
|
|
|
|
"\r\n";
|
|
|
|
preambleData.push(statusLine);
|
|
|
|
|
|
|
|
// headers
|
|
|
|
let headEnum = this._informationalResponseHeaders.enumerator;
|
|
|
|
while (headEnum.hasMoreElements()) {
|
|
|
|
let fieldName = headEnum.getNext().QueryInterface(Ci.nsISupportsString)
|
|
|
|
.data;
|
|
|
|
let values = this._informationalResponseHeaders.getHeaderValues(
|
|
|
|
fieldName
|
|
|
|
);
|
|
|
|
for (let i = 0, sz = values.length; i < sz; i++) {
|
|
|
|
preambleData.push(fieldName + ": " + values[i] + "\r\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// end request-line/headers
|
|
|
|
preambleData.push("\r\n");
|
|
|
|
}
|
|
|
|
|
2015-10-29 07:41:54 +00:00
|
|
|
// request-line
|
2022-06-02 04:42:21 +00:00
|
|
|
var statusLine =
|
|
|
|
"HTTP/" +
|
|
|
|
this.httpVersion +
|
|
|
|
" " +
|
|
|
|
this.httpCode +
|
|
|
|
" " +
|
|
|
|
this.httpDescription +
|
|
|
|
"\r\n";
|
2015-10-29 07:41:54 +00:00
|
|
|
|
|
|
|
// header post-processing
|
|
|
|
|
|
|
|
var headers = this._headers;
|
|
|
|
headers.setHeader("Connection", "close", false);
|
|
|
|
headers.setHeader("Server", "httpd.js", false);
|
2022-06-02 04:42:21 +00:00
|
|
|
if (!headers.hasHeader("Date")) {
|
2015-10-29 07:41:54 +00:00
|
|
|
headers.setHeader("Date", toDateString(Date.now()), false);
|
2022-06-02 04:42:21 +00:00
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
|
|
|
|
// Any response not being processed asynchronously must have an associated
|
|
|
|
// Content-Length header for reasons of backwards compatibility with the
|
|
|
|
// initial server, which fully buffered every response before sending it.
|
|
|
|
// Beyond that, however, it's good to do this anyway because otherwise it's
|
|
|
|
// impossible to test behaviors that depend on the presence or absence of a
|
|
|
|
// Content-Length header.
|
2022-06-02 04:42:21 +00:00
|
|
|
if (!this._processAsync) {
|
2015-10-29 07:41:54 +00:00
|
|
|
dumpn("*** non-async response, set Content-Length");
|
|
|
|
|
|
|
|
var bodyStream = this._bodyInputStream;
|
|
|
|
var avail = bodyStream ? bodyStream.available() : 0;
|
|
|
|
|
|
|
|
// XXX assumes stream will always report the full amount of data available
|
|
|
|
headers.setHeader("Content-Length", "" + avail, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
// construct and send response
|
|
|
|
dumpn("*** header post-processing completed, sending response head...");
|
|
|
|
|
|
|
|
// request-line
|
2022-06-02 04:42:21 +00:00
|
|
|
preambleData.push(statusLine);
|
2015-10-29 07:41:54 +00:00
|
|
|
|
|
|
|
// headers
|
|
|
|
var headEnum = headers.enumerator;
|
2022-06-02 04:42:21 +00:00
|
|
|
while (headEnum.hasMoreElements()) {
|
|
|
|
var fieldName = headEnum.getNext().QueryInterface(Ci.nsISupportsString)
|
|
|
|
.data;
|
2015-10-29 07:41:54 +00:00
|
|
|
var values = headers.getHeaderValues(fieldName);
|
2022-06-02 04:42:21 +00:00
|
|
|
for (var i = 0, sz = values.length; i < sz; i++) {
|
2015-10-29 07:41:54 +00:00
|
|
|
preambleData.push(fieldName + ": " + values[i] + "\r\n");
|
2022-06-02 04:42:21 +00:00
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// end request-line/headers
|
|
|
|
preambleData.push("\r\n");
|
|
|
|
|
|
|
|
var preamble = preambleData.join("");
|
|
|
|
|
|
|
|
var responseHeadPipe = new Pipe(true, false, 0, PR_UINT32_MAX, null);
|
|
|
|
responseHeadPipe.outputStream.write(preamble, preamble.length);
|
|
|
|
|
|
|
|
var response = this;
|
2022-06-02 04:42:21 +00:00
|
|
|
var copyObserver = {
|
|
|
|
onStartRequest(request) {
|
|
|
|
dumpn("*** preamble copying started");
|
|
|
|
},
|
|
|
|
|
|
|
|
onStopRequest(request, statusCode) {
|
|
|
|
dumpn(
|
|
|
|
"*** preamble copying complete " +
|
|
|
|
"[status=0x" +
|
|
|
|
statusCode.toString(16) +
|
|
|
|
"]"
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!Components.isSuccessCode(statusCode)) {
|
|
|
|
dumpn(
|
|
|
|
"!!! header copying problems: non-success statusCode, " +
|
|
|
|
"ending response"
|
|
|
|
);
|
|
|
|
|
|
|
|
response.end();
|
|
|
|
} else {
|
|
|
|
response._sendBody();
|
2015-10-29 07:41:54 +00:00
|
|
|
}
|
2022-06-02 04:42:21 +00:00
|
|
|
},
|
2015-10-29 07:41:54 +00:00
|
|
|
|
2022-06-02 04:42:21 +00:00
|
|
|
QueryInterface: ChromeUtils.generateQI(["nsIRequestObserver"]),
|
|
|
|
};
|
|
|
|
|
|
|
|
this._asyncCopier = new WriteThroughCopier(
|
|
|
|
responseHeadPipe.inputStream,
|
|
|
|
this._connection.output,
|
|
|
|
copyObserver,
|
|
|
|
null
|
|
|
|
);
|
2015-10-29 07:41:54 +00:00
|
|
|
|
|
|
|
responseHeadPipe.outputStream.close();
|
|
|
|
|
|
|
|
// Forbid setting any more headers or modifying the request line.
|
|
|
|
this._headers = null;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Asynchronously writes the body of the response (or the entire response, if
|
|
|
|
* seizePower() has been called) to the network.
|
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
_sendBody() {
|
2015-10-29 07:41:54 +00:00
|
|
|
dumpn("*** _sendBody");
|
|
|
|
|
|
|
|
NS_ASSERT(!this._headers, "still have headers around but sending body?");
|
|
|
|
|
|
|
|
// If no body data was written, we're done
|
2022-06-02 04:42:21 +00:00
|
|
|
if (!this._bodyInputStream) {
|
2015-10-29 07:41:54 +00:00
|
|
|
dumpn("*** empty body, response finished");
|
|
|
|
this.end();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var response = this;
|
2022-06-02 04:42:21 +00:00
|
|
|
var copyObserver = {
|
|
|
|
onStartRequest(request) {
|
|
|
|
dumpn("*** onStartRequest");
|
|
|
|
},
|
|
|
|
|
|
|
|
onStopRequest(request, statusCode) {
|
|
|
|
dumpn("*** onStopRequest [status=0x" + statusCode.toString(16) + "]");
|
|
|
|
|
|
|
|
if (statusCode === Cr.NS_BINDING_ABORTED) {
|
|
|
|
dumpn("*** terminating copy observer without ending the response");
|
|
|
|
} else {
|
|
|
|
if (!Components.isSuccessCode(statusCode)) {
|
|
|
|
dumpn("*** WARNING: non-success statusCode in onStopRequest");
|
2015-10-29 07:41:54 +00:00
|
|
|
}
|
|
|
|
|
2022-06-02 04:42:21 +00:00
|
|
|
response.end();
|
2015-10-29 07:41:54 +00:00
|
|
|
}
|
2022-06-02 04:42:21 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
QueryInterface: ChromeUtils.generateQI(["nsIRequestObserver"]),
|
|
|
|
};
|
2015-10-29 07:41:54 +00:00
|
|
|
|
|
|
|
dumpn("*** starting async copier of body data...");
|
2022-06-02 04:42:21 +00:00
|
|
|
this._asyncCopier = new WriteThroughCopier(
|
|
|
|
this._bodyInputStream,
|
|
|
|
this._connection.output,
|
|
|
|
copyObserver,
|
|
|
|
null
|
|
|
|
);
|
2015-10-29 07:41:54 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/** Ensures that this hasn't been ended. */
|
2022-06-02 04:42:21 +00:00
|
|
|
_ensureAlive() {
|
2015-10-29 07:41:54 +00:00
|
|
|
NS_ASSERT(!this._ended, "not handling response lifetime correctly");
|
2022-06-02 04:42:21 +00:00
|
|
|
},
|
2015-10-29 07:41:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Size of the segments in the buffer used in storing response data and writing
|
|
|
|
* it to the socket.
|
|
|
|
*/
|
|
|
|
Response.SEGMENT_SIZE = 8192;
|
|
|
|
|
|
|
|
/** Serves double duty in WriteThroughCopier implementation. */
|
2022-06-02 04:42:21 +00:00
|
|
|
function notImplemented() {
|
|
|
|
throw Components.Exception("", Cr.NS_ERROR_NOT_IMPLEMENTED);
|
2015-10-29 07:41:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Returns true iff the given exception represents stream closure. */
|
2022-06-02 04:42:21 +00:00
|
|
|
function streamClosed(e) {
|
|
|
|
return (
|
|
|
|
e === Cr.NS_BASE_STREAM_CLOSED ||
|
|
|
|
(typeof e === "object" && e.result === Cr.NS_BASE_STREAM_CLOSED)
|
|
|
|
);
|
2015-10-29 07:41:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Returns true iff the given exception represents a blocked stream. */
|
2022-06-02 04:42:21 +00:00
|
|
|
function wouldBlock(e) {
|
|
|
|
return (
|
|
|
|
e === Cr.NS_BASE_STREAM_WOULD_BLOCK ||
|
|
|
|
(typeof e === "object" && e.result === Cr.NS_BASE_STREAM_WOULD_BLOCK)
|
|
|
|
);
|
2015-10-29 07:41:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Copies data from source to sink as it becomes available, when that data can
|
|
|
|
* be written to sink without blocking.
|
|
|
|
*
|
|
|
|
* @param source : nsIAsyncInputStream
|
|
|
|
* the stream from which data is to be read
|
|
|
|
* @param sink : nsIAsyncOutputStream
|
|
|
|
* the stream to which data is to be copied
|
|
|
|
* @param observer : nsIRequestObserver
|
|
|
|
* an observer which will be notified when the copy starts and finishes
|
|
|
|
* @param context : nsISupports
|
|
|
|
* context passed to observer when notified of start/stop
|
|
|
|
* @throws NS_ERROR_NULL_POINTER
|
|
|
|
* if source, sink, or observer are null
|
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
function WriteThroughCopier(source, sink, observer, context) {
|
|
|
|
if (!source || !sink || !observer) {
|
|
|
|
throw Components.Exception("", Cr.NS_ERROR_NULL_POINTER);
|
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
|
|
|
|
/** Stream from which data is being read. */
|
|
|
|
this._source = source;
|
|
|
|
|
|
|
|
/** Stream to which data is being written. */
|
|
|
|
this._sink = sink;
|
|
|
|
|
|
|
|
/** Observer watching this copy. */
|
|
|
|
this._observer = observer;
|
|
|
|
|
|
|
|
/** Context for the observer watching this. */
|
|
|
|
this._context = context;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* True iff this is currently being canceled (cancel has been called, the
|
|
|
|
* callback may not yet have been made).
|
|
|
|
*/
|
|
|
|
this._canceled = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* False until all data has been read from input and written to output, at
|
|
|
|
* which point this copy is completed and cancel() is asynchronously called.
|
|
|
|
*/
|
|
|
|
this._completed = false;
|
|
|
|
|
|
|
|
/** Required by nsIRequest, meaningless. */
|
|
|
|
this.loadFlags = 0;
|
|
|
|
/** Required by nsIRequest, meaningless. */
|
|
|
|
this.loadGroup = null;
|
|
|
|
/** Required by nsIRequest, meaningless. */
|
|
|
|
this.name = "response-body-copy";
|
|
|
|
|
|
|
|
/** Status of this request. */
|
|
|
|
this.status = Cr.NS_OK;
|
|
|
|
|
|
|
|
/** Arrays of byte strings waiting to be written to output. */
|
|
|
|
this._pendingData = [];
|
|
|
|
|
|
|
|
// start copying
|
2022-06-02 04:42:21 +00:00
|
|
|
try {
|
2020-07-25 00:53:59 +00:00
|
|
|
observer.onStartRequest(this);
|
2015-10-29 07:41:54 +00:00
|
|
|
this._waitToReadData();
|
|
|
|
this._waitForSinkClosure();
|
2022-06-02 04:42:21 +00:00
|
|
|
} catch (e) {
|
|
|
|
dumpn(
|
|
|
|
"!!! error starting copy: " +
|
|
|
|
e +
|
|
|
|
("lineNumber" in e ? ", line " + e.lineNumber : "")
|
|
|
|
);
|
2015-10-29 07:41:54 +00:00
|
|
|
dumpn(e.stack);
|
|
|
|
this.cancel(Cr.NS_ERROR_UNEXPECTED);
|
|
|
|
}
|
|
|
|
}
|
2022-06-02 04:42:21 +00:00
|
|
|
WriteThroughCopier.prototype = {
|
2015-10-29 07:41:54 +00:00
|
|
|
/* nsISupports implementation */
|
|
|
|
|
2022-06-02 04:42:21 +00:00
|
|
|
QueryInterface: ChromeUtils.generateQI([
|
|
|
|
"nsIInputStreamCallback",
|
|
|
|
"nsIOutputStreamCallback",
|
|
|
|
"nsIRequest",
|
|
|
|
]),
|
2015-10-29 07:41:54 +00:00
|
|
|
|
|
|
|
// NSIINPUTSTREAMCALLBACK
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Receives a more-data-in-input notification and writes the corresponding
|
|
|
|
* data to the output.
|
|
|
|
*
|
|
|
|
* @param input : nsIAsyncInputStream
|
|
|
|
* the input stream on whose data we have been waiting
|
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
onInputStreamReady(input) {
|
|
|
|
if (this._source === null) {
|
2015-10-29 07:41:54 +00:00
|
|
|
return;
|
2022-06-02 04:42:21 +00:00
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
|
|
|
|
dumpn("*** onInputStreamReady");
|
|
|
|
|
|
|
|
//
|
|
|
|
// Ordinarily we'll read a non-zero amount of data from input, queue it up
|
|
|
|
// to be written and then wait for further callbacks. The complications in
|
|
|
|
// this method are the cases where we deviate from that behavior when errors
|
|
|
|
// occur or when copying is drawing to a finish.
|
|
|
|
//
|
|
|
|
// The edge cases when reading data are:
|
|
|
|
//
|
|
|
|
// Zero data is read
|
|
|
|
// If zero data was read, we're at the end of available data, so we can
|
|
|
|
// should stop reading and move on to writing out what we have (or, if
|
|
|
|
// we've already done that, onto notifying of completion).
|
|
|
|
// A stream-closed exception is thrown
|
|
|
|
// This is effectively a less kind version of zero data being read; the
|
|
|
|
// only difference is that we notify of completion with that result
|
|
|
|
// rather than with NS_OK.
|
|
|
|
// Some other exception is thrown
|
|
|
|
// This is the least kind result. We don't know what happened, so we
|
|
|
|
// act as though the stream closed except that we notify of completion
|
|
|
|
// with the result NS_ERROR_UNEXPECTED.
|
|
|
|
//
|
|
|
|
|
2022-06-02 04:42:21 +00:00
|
|
|
var bytesWanted = 0,
|
|
|
|
bytesConsumed = -1;
|
|
|
|
try {
|
2015-10-29 07:41:54 +00:00
|
|
|
input = new BinaryInputStream(input);
|
|
|
|
|
|
|
|
bytesWanted = Math.min(input.available(), Response.SEGMENT_SIZE);
|
|
|
|
dumpn("*** input wanted: " + bytesWanted);
|
|
|
|
|
2022-06-02 04:42:21 +00:00
|
|
|
if (bytesWanted > 0) {
|
2015-10-29 07:41:54 +00:00
|
|
|
var data = input.readByteArray(bytesWanted);
|
|
|
|
bytesConsumed = data.length;
|
|
|
|
this._pendingData.push(String.fromCharCode.apply(String, data));
|
|
|
|
}
|
|
|
|
|
|
|
|
dumpn("*** " + bytesConsumed + " bytes read");
|
|
|
|
|
|
|
|
// Handle the zero-data edge case in the same place as all other edge
|
|
|
|
// cases are handled.
|
2022-06-02 04:42:21 +00:00
|
|
|
if (bytesWanted === 0) {
|
|
|
|
throw Components.Exception("", Cr.NS_BASE_STREAM_CLOSED);
|
2015-10-29 07:41:54 +00:00
|
|
|
}
|
2022-06-02 04:42:21 +00:00
|
|
|
} catch (e) {
|
|
|
|
let rv;
|
|
|
|
if (streamClosed(e)) {
|
|
|
|
dumpn("*** input stream closed");
|
|
|
|
rv = bytesWanted === 0 ? Cr.NS_OK : Cr.NS_ERROR_UNEXPECTED;
|
|
|
|
} else {
|
2015-10-29 07:41:54 +00:00
|
|
|
dumpn("!!! unexpected error reading from input, canceling: " + e);
|
2022-06-02 04:42:21 +00:00
|
|
|
rv = Cr.NS_ERROR_UNEXPECTED;
|
2015-10-29 07:41:54 +00:00
|
|
|
}
|
|
|
|
|
2022-06-02 04:42:21 +00:00
|
|
|
this._doneReadingSource(rv);
|
2015-10-29 07:41:54 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var pendingData = this._pendingData;
|
|
|
|
|
|
|
|
NS_ASSERT(bytesConsumed > 0);
|
|
|
|
NS_ASSERT(pendingData.length > 0, "no pending data somehow?");
|
2022-06-02 04:42:21 +00:00
|
|
|
NS_ASSERT(
|
|
|
|
pendingData[pendingData.length - 1].length > 0,
|
|
|
|
"buffered zero bytes of data?"
|
|
|
|
);
|
2015-10-29 07:41:54 +00:00
|
|
|
|
|
|
|
NS_ASSERT(this._source !== null);
|
|
|
|
|
|
|
|
// Reading has gone great, and we've gotten data to write now. What if we
|
|
|
|
// don't have a place to write that data, because output went away just
|
|
|
|
// before this read? Drop everything on the floor, including new data, and
|
|
|
|
// cancel at this point.
|
2022-06-02 04:42:21 +00:00
|
|
|
if (this._sink === null) {
|
2015-10-29 07:41:54 +00:00
|
|
|
pendingData.length = 0;
|
|
|
|
this._doneReadingSource(Cr.NS_ERROR_UNEXPECTED);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Okay, we've read the data, and we know we have a place to write it. We
|
|
|
|
// need to queue up the data to be written, but *only* if none is queued
|
|
|
|
// already -- if data's already queued, the code that actually writes the
|
|
|
|
// data will make sure to wait on unconsumed pending data.
|
2022-06-02 04:42:21 +00:00
|
|
|
try {
|
|
|
|
if (pendingData.length === 1) {
|
2015-10-29 07:41:54 +00:00
|
|
|
this._waitToWriteData();
|
2022-06-02 04:42:21 +00:00
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
dumpn(
|
|
|
|
"!!! error waiting to write data just read, swallowing and " +
|
|
|
|
"writing only what we already have: " +
|
|
|
|
e
|
|
|
|
);
|
2015-10-29 07:41:54 +00:00
|
|
|
this._doneWritingToSink(Cr.NS_ERROR_UNEXPECTED);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Whee! We successfully read some data, and it's successfully queued up to
|
|
|
|
// be written. All that remains now is to wait for more data to read.
|
2022-06-02 04:42:21 +00:00
|
|
|
try {
|
2015-10-29 07:41:54 +00:00
|
|
|
this._waitToReadData();
|
2022-06-02 04:42:21 +00:00
|
|
|
} catch (e) {
|
2015-10-29 07:41:54 +00:00
|
|
|
dumpn("!!! error waiting to read more data: " + e);
|
|
|
|
this._doneReadingSource(Cr.NS_ERROR_UNEXPECTED);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
// NSIOUTPUTSTREAMCALLBACK
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback when data may be written to the output stream without blocking, or
|
|
|
|
* when the output stream has been closed.
|
|
|
|
*
|
|
|
|
* @param output : nsIAsyncOutputStream
|
|
|
|
* the output stream on whose writability we've been waiting, also known as
|
|
|
|
* this._sink
|
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
onOutputStreamReady(output) {
|
|
|
|
if (this._sink === null) {
|
2015-10-29 07:41:54 +00:00
|
|
|
return;
|
2022-06-02 04:42:21 +00:00
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
|
|
|
|
dumpn("*** onOutputStreamReady");
|
|
|
|
|
|
|
|
var pendingData = this._pendingData;
|
2022-06-02 04:42:21 +00:00
|
|
|
if (pendingData.length === 0) {
|
2015-10-29 07:41:54 +00:00
|
|
|
// There's no pending data to write. The only way this can happen is if
|
|
|
|
// we're waiting on the output stream's closure, so we can respond to a
|
|
|
|
// copying failure as quickly as possible (rather than waiting for data to
|
|
|
|
// be available to read and then fail to be copied). Therefore, we must
|
|
|
|
// be done now -- don't bother to attempt to write anything and wrap
|
|
|
|
// things up.
|
|
|
|
dumpn("!!! output stream closed prematurely, ending copy");
|
|
|
|
|
|
|
|
this._doneWritingToSink(Cr.NS_ERROR_UNEXPECTED);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ASSERT(pendingData[0].length > 0, "queued up an empty quantum?");
|
|
|
|
|
|
|
|
//
|
|
|
|
// Write out the first pending quantum of data. The possible errors here
|
|
|
|
// are:
|
|
|
|
//
|
|
|
|
// The write might fail because we can't write that much data
|
|
|
|
// Okay, we've written what we can now, so re-queue what's left and
|
|
|
|
// finish writing it out later.
|
|
|
|
// The write failed because the stream was closed
|
|
|
|
// Discard pending data that we can no longer write, stop reading, and
|
|
|
|
// signal that copying finished.
|
|
|
|
// Some other error occurred.
|
|
|
|
// Same as if the stream were closed, but notify with the status
|
|
|
|
// NS_ERROR_UNEXPECTED so the observer knows something was wonky.
|
|
|
|
//
|
|
|
|
|
2022-06-02 04:42:21 +00:00
|
|
|
try {
|
2015-10-29 07:41:54 +00:00
|
|
|
var quantum = pendingData[0];
|
|
|
|
|
|
|
|
// XXX |quantum| isn't guaranteed to be ASCII, so we're relying on
|
|
|
|
// undefined behavior! We're only using this because writeByteArray
|
|
|
|
// is unusably broken for asynchronous output streams; see bug 532834
|
|
|
|
// for details.
|
|
|
|
var bytesWritten = output.write(quantum, quantum.length);
|
2022-06-02 04:42:21 +00:00
|
|
|
if (bytesWritten === quantum.length) {
|
2015-10-29 07:41:54 +00:00
|
|
|
pendingData.shift();
|
2022-06-02 04:42:21 +00:00
|
|
|
} else {
|
2015-10-29 07:41:54 +00:00
|
|
|
pendingData[0] = quantum.substring(bytesWritten);
|
2022-06-02 04:42:21 +00:00
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
|
|
|
|
dumpn("*** wrote " + bytesWritten + " bytes of data");
|
2022-06-02 04:42:21 +00:00
|
|
|
} catch (e) {
|
|
|
|
if (wouldBlock(e)) {
|
|
|
|
NS_ASSERT(
|
|
|
|
pendingData.length > 0,
|
|
|
|
"stream-blocking exception with no data to write?"
|
|
|
|
);
|
|
|
|
NS_ASSERT(
|
|
|
|
pendingData[0].length > 0,
|
|
|
|
"stream-blocking exception with empty quantum?"
|
|
|
|
);
|
2015-10-29 07:41:54 +00:00
|
|
|
this._waitToWriteData();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-02 04:42:21 +00:00
|
|
|
if (streamClosed(e)) {
|
2015-10-29 07:41:54 +00:00
|
|
|
dumpn("!!! output stream prematurely closed, signaling error...");
|
2022-06-02 04:42:21 +00:00
|
|
|
} else {
|
2015-10-29 07:41:54 +00:00
|
|
|
dumpn("!!! unknown error: " + e + ", quantum=" + quantum);
|
2022-06-02 04:42:21 +00:00
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
|
|
|
|
this._doneWritingToSink(Cr.NS_ERROR_UNEXPECTED);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The day is ours! Quantum written, now let's see if we have more data
|
|
|
|
// still to write.
|
2022-06-02 04:42:21 +00:00
|
|
|
try {
|
|
|
|
if (pendingData.length > 0) {
|
2015-10-29 07:41:54 +00:00
|
|
|
this._waitToWriteData();
|
|
|
|
return;
|
|
|
|
}
|
2022-06-02 04:42:21 +00:00
|
|
|
} catch (e) {
|
2015-10-29 07:41:54 +00:00
|
|
|
dumpn("!!! unexpected error waiting to write pending data: " + e);
|
|
|
|
this._doneWritingToSink(Cr.NS_ERROR_UNEXPECTED);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Okay, we have no more pending data to write -- but might we get more in
|
|
|
|
// the future?
|
2022-06-02 04:42:21 +00:00
|
|
|
if (this._source !== null) {
|
2015-10-29 07:41:54 +00:00
|
|
|
/*
|
|
|
|
* If we might, then wait for the output stream to be closed. (We wait
|
|
|
|
* only for closure because we have no data to write -- and if we waited
|
|
|
|
* for a specific amount of data, we would get repeatedly notified for no
|
|
|
|
* reason if over time the output stream permitted more and more data to
|
|
|
|
* be written to it without blocking.)
|
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
this._waitForSinkClosure();
|
|
|
|
} else {
|
2015-10-29 07:41:54 +00:00
|
|
|
/*
|
|
|
|
* On the other hand, if we can't have more data because the input
|
|
|
|
* stream's gone away, then it's time to notify of copy completion.
|
|
|
|
* Victory!
|
|
|
|
*/
|
|
|
|
this._sink = null;
|
|
|
|
this._cancelOrDispatchCancelCallback(Cr.NS_OK);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
// NSIREQUEST
|
|
|
|
|
|
|
|
/** Returns true if the cancel observer hasn't been notified yet. */
|
2022-06-02 04:42:21 +00:00
|
|
|
isPending() {
|
2015-10-29 07:41:54 +00:00
|
|
|
return !this._completed;
|
|
|
|
},
|
|
|
|
|
|
|
|
/** Not implemented, don't use! */
|
|
|
|
suspend: notImplemented,
|
|
|
|
/** Not implemented, don't use! */
|
|
|
|
resume: notImplemented,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cancels data reading from input, asynchronously writes out any pending
|
|
|
|
* data, and causes the observer to be notified with the given error code when
|
|
|
|
* all writing has finished.
|
|
|
|
*
|
|
|
|
* @param status : nsresult
|
|
|
|
* the status to pass to the observer when data copying has been canceled
|
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
cancel(status) {
|
2015-10-29 07:41:54 +00:00
|
|
|
dumpn("*** cancel(" + status.toString(16) + ")");
|
|
|
|
|
2022-06-02 04:42:21 +00:00
|
|
|
if (this._canceled) {
|
2015-10-29 07:41:54 +00:00
|
|
|
dumpn("*** suppressing a late cancel");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._canceled = true;
|
|
|
|
this.status = status;
|
|
|
|
|
|
|
|
// We could be in the middle of absolutely anything at this point. Both
|
|
|
|
// input and output might still be around, we might have pending data to
|
|
|
|
// write, and in general we know nothing about the state of the world. We
|
|
|
|
// therefore must assume everything's in progress and take everything to its
|
|
|
|
// final steady state (or so far as it can go before we need to finish
|
|
|
|
// writing out remaining data).
|
|
|
|
|
|
|
|
this._doneReadingSource(status);
|
|
|
|
},
|
|
|
|
|
|
|
|
// PRIVATE IMPLEMENTATION
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stop reading input if we haven't already done so, passing e as the status
|
|
|
|
* when closing the stream, and kick off a copy-completion notice if no more
|
|
|
|
* data remains to be written.
|
|
|
|
*
|
|
|
|
* @param e : nsresult
|
|
|
|
* the status to be used when closing the input stream
|
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
_doneReadingSource(e) {
|
2015-10-29 07:41:54 +00:00
|
|
|
dumpn("*** _doneReadingSource(0x" + e.toString(16) + ")");
|
|
|
|
|
|
|
|
this._finishSource(e);
|
2022-06-02 04:42:21 +00:00
|
|
|
if (this._pendingData.length === 0) {
|
2015-10-29 07:41:54 +00:00
|
|
|
this._sink = null;
|
2022-06-02 04:42:21 +00:00
|
|
|
} else {
|
2015-10-29 07:41:54 +00:00
|
|
|
NS_ASSERT(this._sink !== null, "null output?");
|
2022-06-02 04:42:21 +00:00
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
|
|
|
|
// If we've written out all data read up to this point, then it's time to
|
|
|
|
// signal completion.
|
2022-06-02 04:42:21 +00:00
|
|
|
if (this._sink === null) {
|
2015-10-29 07:41:54 +00:00
|
|
|
NS_ASSERT(this._pendingData.length === 0, "pending data still?");
|
|
|
|
this._cancelOrDispatchCancelCallback(e);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stop writing output if we haven't already done so, discard any data that
|
|
|
|
* remained to be sent, close off input if it wasn't already closed, and kick
|
|
|
|
* off a copy-completion notice.
|
|
|
|
*
|
|
|
|
* @param e : nsresult
|
|
|
|
* the status to be used when closing input if it wasn't already closed
|
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
_doneWritingToSink(e) {
|
2015-10-29 07:41:54 +00:00
|
|
|
dumpn("*** _doneWritingToSink(0x" + e.toString(16) + ")");
|
|
|
|
|
|
|
|
this._pendingData.length = 0;
|
|
|
|
this._sink = null;
|
|
|
|
this._doneReadingSource(e);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Completes processing of this copy: either by canceling the copy if it
|
|
|
|
* hasn't already been canceled using the provided status, or by dispatching
|
|
|
|
* the cancel callback event (with the originally provided status, of course)
|
|
|
|
* if it already has been canceled.
|
|
|
|
*
|
|
|
|
* @param status : nsresult
|
|
|
|
* the status code to use to cancel this, if this hasn't already been
|
|
|
|
* canceled
|
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
_cancelOrDispatchCancelCallback(status) {
|
2015-10-29 07:41:54 +00:00
|
|
|
dumpn("*** _cancelOrDispatchCancelCallback(" + status + ")");
|
|
|
|
|
|
|
|
NS_ASSERT(this._source === null, "should have finished input");
|
|
|
|
NS_ASSERT(this._sink === null, "should have finished output");
|
|
|
|
NS_ASSERT(this._pendingData.length === 0, "should have no pending data");
|
|
|
|
|
2022-06-02 04:42:21 +00:00
|
|
|
if (!this._canceled) {
|
2015-10-29 07:41:54 +00:00
|
|
|
this.cancel(status);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var self = this;
|
2022-06-02 04:42:21 +00:00
|
|
|
var event = {
|
|
|
|
run() {
|
|
|
|
dumpn("*** onStopRequest async callback");
|
|
|
|
|
|
|
|
self._completed = true;
|
|
|
|
try {
|
|
|
|
self._observer.onStopRequest(self, self.status);
|
|
|
|
} catch (e) {
|
|
|
|
NS_ASSERT(
|
|
|
|
false,
|
|
|
|
"how are we throwing an exception here? we control " +
|
|
|
|
"all the callers! " +
|
|
|
|
e
|
|
|
|
);
|
2015-10-29 07:41:54 +00:00
|
|
|
}
|
2022-06-02 04:42:21 +00:00
|
|
|
},
|
|
|
|
};
|
2015-10-29 07:41:54 +00:00
|
|
|
|
|
|
|
gThreadManager.currentThread.dispatch(event, Ci.nsIThread.DISPATCH_NORMAL);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Kicks off another wait for more data to be available from the input stream.
|
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
_waitToReadData() {
|
2015-10-29 07:41:54 +00:00
|
|
|
dumpn("*** _waitToReadData");
|
2022-06-02 04:42:21 +00:00
|
|
|
this._source.asyncWait(
|
|
|
|
this,
|
|
|
|
0,
|
|
|
|
Response.SEGMENT_SIZE,
|
|
|
|
gThreadManager.mainThread
|
|
|
|
);
|
2015-10-29 07:41:54 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Kicks off another wait until data can be written to the output stream.
|
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
_waitToWriteData() {
|
2015-10-29 07:41:54 +00:00
|
|
|
dumpn("*** _waitToWriteData");
|
|
|
|
|
|
|
|
var pendingData = this._pendingData;
|
|
|
|
NS_ASSERT(pendingData.length > 0, "no pending data to write?");
|
|
|
|
NS_ASSERT(pendingData[0].length > 0, "buffered an empty write?");
|
|
|
|
|
2022-06-02 04:42:21 +00:00
|
|
|
this._sink.asyncWait(
|
|
|
|
this,
|
|
|
|
0,
|
|
|
|
pendingData[0].length,
|
|
|
|
gThreadManager.mainThread
|
|
|
|
);
|
2015-10-29 07:41:54 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Kicks off a wait for the sink to which data is being copied to be closed.
|
|
|
|
* We wait for stream closure when we don't have any data to be copied, rather
|
|
|
|
* than waiting to write a specific amount of data. We can't wait to write
|
|
|
|
* data because the sink might be infinitely writable, and if no data appears
|
|
|
|
* in the source for a long time we might have to spin quite a bit waiting to
|
|
|
|
* write, waiting to write again, &c. Waiting on stream closure instead means
|
|
|
|
* we'll get just one notification if the sink dies. Note that when data
|
|
|
|
* starts arriving from the sink we'll resume waiting for data to be written,
|
|
|
|
* dropping this closure-only callback entirely.
|
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
_waitForSinkClosure() {
|
2015-10-29 07:41:54 +00:00
|
|
|
dumpn("*** _waitForSinkClosure");
|
|
|
|
|
2022-06-02 04:42:21 +00:00
|
|
|
this._sink.asyncWait(
|
|
|
|
this,
|
|
|
|
Ci.nsIAsyncOutputStream.WAIT_CLOSURE_ONLY,
|
|
|
|
0,
|
|
|
|
gThreadManager.mainThread
|
|
|
|
);
|
2015-10-29 07:41:54 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Closes input with the given status, if it hasn't already been closed;
|
|
|
|
* otherwise a no-op.
|
|
|
|
*
|
|
|
|
* @param status : nsresult
|
|
|
|
* status code use to close the source stream if necessary
|
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
_finishSource(status) {
|
2015-10-29 07:41:54 +00:00
|
|
|
dumpn("*** _finishSource(" + status.toString(16) + ")");
|
|
|
|
|
2022-06-02 04:42:21 +00:00
|
|
|
if (this._source !== null) {
|
2015-10-29 07:41:54 +00:00
|
|
|
this._source.closeWithStatus(status);
|
|
|
|
this._source = null;
|
|
|
|
}
|
2022-06-02 04:42:21 +00:00
|
|
|
},
|
2015-10-29 07:41:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A container for utility functions used with HTTP headers.
|
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
const headerUtils = {
|
2015-10-29 07:41:54 +00:00
|
|
|
/**
|
|
|
|
* Normalizes fieldName (by converting it to lowercase) and ensures it is a
|
|
|
|
* valid header field name (although not necessarily one specified in RFC
|
|
|
|
* 2616).
|
|
|
|
*
|
|
|
|
* @throws NS_ERROR_INVALID_ARG
|
|
|
|
* if fieldName does not match the field-name production in RFC 2616
|
|
|
|
* @returns string
|
|
|
|
* fieldName converted to lowercase if it is a valid header, for characters
|
|
|
|
* where case conversion is possible
|
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
normalizeFieldName(fieldName) {
|
|
|
|
if (fieldName == "") {
|
2015-10-29 07:41:54 +00:00
|
|
|
dumpn("*** Empty fieldName");
|
2022-06-02 04:42:21 +00:00
|
|
|
throw Components.Exception("", Cr.NS_ERROR_INVALID_ARG);
|
2015-10-29 07:41:54 +00:00
|
|
|
}
|
|
|
|
|
2022-06-02 04:42:21 +00:00
|
|
|
for (var i = 0, sz = fieldName.length; i < sz; i++) {
|
|
|
|
if (!IS_TOKEN_ARRAY[fieldName.charCodeAt(i)]) {
|
2015-10-29 07:41:54 +00:00
|
|
|
dumpn(fieldName + " is not a valid header field name!");
|
2022-06-02 04:42:21 +00:00
|
|
|
throw Components.Exception("", Cr.NS_ERROR_INVALID_ARG);
|
2015-10-29 07:41:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return fieldName.toLowerCase();
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Ensures that fieldValue is a valid header field value (although not
|
|
|
|
* necessarily as specified in RFC 2616 if the corresponding field name is
|
|
|
|
* part of the HTTP protocol), normalizes the value if it is, and
|
|
|
|
* returns the normalized value.
|
|
|
|
*
|
|
|
|
* @param fieldValue : string
|
|
|
|
* a value to be normalized as an HTTP header field value
|
|
|
|
* @throws NS_ERROR_INVALID_ARG
|
|
|
|
* if fieldValue does not match the field-value production in RFC 2616
|
|
|
|
* @returns string
|
|
|
|
* fieldValue as a normalized HTTP header field value
|
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
normalizeFieldValue(fieldValue) {
|
2015-10-29 07:41:54 +00:00
|
|
|
// field-value = *( field-content | LWS )
|
|
|
|
// field-content = <the OCTETs making up the field-value
|
|
|
|
// and consisting of either *TEXT or combinations
|
|
|
|
// of token, separators, and quoted-string>
|
|
|
|
// TEXT = <any OCTET except CTLs,
|
|
|
|
// but including LWS>
|
|
|
|
// LWS = [CRLF] 1*( SP | HT )
|
|
|
|
//
|
|
|
|
// quoted-string = ( <"> *(qdtext | quoted-pair ) <"> )
|
|
|
|
// qdtext = <any TEXT except <">>
|
|
|
|
// quoted-pair = "\" CHAR
|
|
|
|
// CHAR = <any US-ASCII character (octets 0 - 127)>
|
|
|
|
|
|
|
|
// Any LWS that occurs between field-content MAY be replaced with a single
|
|
|
|
// SP before interpreting the field value or forwarding the message
|
|
|
|
// downstream (section 4.2); we replace 1*LWS with a single SP
|
|
|
|
var val = fieldValue.replace(/(?:(?:\r\n)?[ \t]+)+/g, " ");
|
|
|
|
|
|
|
|
// remove leading/trailing LWS (which has been converted to SP)
|
|
|
|
val = val.replace(/^ +/, "").replace(/ +$/, "");
|
|
|
|
|
|
|
|
// that should have taken care of all CTLs, so val should contain no CTLs
|
|
|
|
dumpn("*** Normalized value: '" + val + "'");
|
2022-06-02 04:42:21 +00:00
|
|
|
for (var i = 0, len = val.length; i < len; i++) {
|
|
|
|
if (isCTL(val.charCodeAt(i))) {
|
2015-10-29 07:41:54 +00:00
|
|
|
dump("*** Char " + i + " has charcode " + val.charCodeAt(i));
|
2022-06-02 04:42:21 +00:00
|
|
|
throw Components.Exception("", Cr.NS_ERROR_INVALID_ARG);
|
2015-10-29 07:41:54 +00:00
|
|
|
}
|
2022-06-02 04:42:21 +00:00
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
|
|
|
|
// XXX disallows quoted-pair where CHAR is a CTL -- will not invalidly
|
|
|
|
// normalize, however, so this can be construed as a tightening of the
|
|
|
|
// spec and not entirely as a bug
|
|
|
|
return val;
|
2022-06-02 04:42:21 +00:00
|
|
|
},
|
2015-10-29 07:41:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts the given string into a string which is safe for use in an HTML
|
|
|
|
* context.
|
|
|
|
*
|
|
|
|
* @param str : string
|
|
|
|
* the string to make HTML-safe
|
|
|
|
* @returns string
|
|
|
|
* an HTML-safe version of str
|
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
function htmlEscape(str) {
|
2015-10-29 07:41:54 +00:00
|
|
|
// this is naive, but it'll work
|
|
|
|
var s = "";
|
2022-06-02 04:42:21 +00:00
|
|
|
for (var i = 0; i < str.length; i++) {
|
2015-10-29 07:41:54 +00:00
|
|
|
s += "&#" + str.charCodeAt(i) + ";";
|
2022-06-02 04:42:21 +00:00
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructs an object representing an HTTP version (see section 3.1).
|
|
|
|
*
|
|
|
|
* @param versionString
|
|
|
|
* a string of the form "#.#", where # is an non-negative decimal integer with
|
|
|
|
* or without leading zeros
|
|
|
|
* @throws
|
|
|
|
* if versionString does not specify a valid HTTP version number
|
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
function nsHttpVersion(versionString) {
|
2015-10-29 07:41:54 +00:00
|
|
|
var matches = /^(\d+)\.(\d+)$/.exec(versionString);
|
2022-06-02 04:42:21 +00:00
|
|
|
if (!matches) {
|
|
|
|
throw new Error("Not a valid HTTP version!");
|
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
|
|
|
|
/** The major version number of this, as a number. */
|
|
|
|
this.major = parseInt(matches[1], 10);
|
|
|
|
|
|
|
|
/** The minor version number of this, as a number. */
|
|
|
|
this.minor = parseInt(matches[2], 10);
|
|
|
|
|
2022-06-02 04:42:21 +00:00
|
|
|
if (
|
|
|
|
isNaN(this.major) ||
|
|
|
|
isNaN(this.minor) ||
|
|
|
|
this.major < 0 ||
|
|
|
|
this.minor < 0
|
|
|
|
) {
|
|
|
|
throw new Error("Not a valid HTTP version!");
|
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
}
|
2022-06-02 04:42:21 +00:00
|
|
|
nsHttpVersion.prototype = {
|
2015-10-29 07:41:54 +00:00
|
|
|
/**
|
|
|
|
* Returns the standard string representation of the HTTP version represented
|
|
|
|
* by this (e.g., "1.1").
|
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
toString() {
|
2015-10-29 07:41:54 +00:00
|
|
|
return this.major + "." + this.minor;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if this represents the same HTTP version as otherVersion,
|
|
|
|
* false otherwise.
|
|
|
|
*
|
|
|
|
* @param otherVersion : nsHttpVersion
|
|
|
|
* the version to compare against this
|
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
equals(otherVersion) {
|
|
|
|
return this.major == otherVersion.major && this.minor == otherVersion.minor;
|
2015-10-29 07:41:54 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/** True if this >= otherVersion, false otherwise. */
|
2022-06-02 04:42:21 +00:00
|
|
|
atLeast(otherVersion) {
|
|
|
|
return (
|
|
|
|
this.major > otherVersion.major ||
|
|
|
|
(this.major == otherVersion.major && this.minor >= otherVersion.minor)
|
|
|
|
);
|
|
|
|
},
|
2015-10-29 07:41:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
nsHttpVersion.HTTP_1_0 = new nsHttpVersion("1.0");
|
|
|
|
nsHttpVersion.HTTP_1_1 = new nsHttpVersion("1.1");
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An object which stores HTTP headers for a request or response.
|
|
|
|
*
|
|
|
|
* Note that since headers are case-insensitive, this object converts headers to
|
|
|
|
* lowercase before storing them. This allows the getHeader and hasHeader
|
|
|
|
* methods to work correctly for any case of a header, but it means that the
|
|
|
|
* values returned by .enumerator may not be equal case-sensitively to the
|
|
|
|
* values passed to setHeader when adding headers to this.
|
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
function nsHttpHeaders() {
|
2015-10-29 07:41:54 +00:00
|
|
|
/**
|
|
|
|
* A hash of headers, with header field names as the keys and header field
|
|
|
|
* values as the values. Header field names are case-insensitive, but upon
|
|
|
|
* insertion here they are converted to lowercase. Header field values are
|
|
|
|
* normalized upon insertion to contain no leading or trailing whitespace.
|
|
|
|
*
|
|
|
|
* Note also that per RFC 2616, section 4.2, two headers with the same name in
|
|
|
|
* a message may be treated as one header with the same field name and a field
|
|
|
|
* value consisting of the separate field values joined together with a "," in
|
|
|
|
* their original order. This hash stores multiple headers with the same name
|
|
|
|
* in this manner.
|
|
|
|
*/
|
|
|
|
this._headers = {};
|
|
|
|
}
|
2022-06-02 04:42:21 +00:00
|
|
|
nsHttpHeaders.prototype = {
|
2015-10-29 07:41:54 +00:00
|
|
|
/**
|
|
|
|
* Sets the header represented by name and value in this.
|
|
|
|
*
|
|
|
|
* @param name : string
|
|
|
|
* the header name
|
|
|
|
* @param value : string
|
|
|
|
* the header value
|
|
|
|
* @throws NS_ERROR_INVALID_ARG
|
|
|
|
* if name or value is not a valid header component
|
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
setHeader(fieldName, fieldValue, merge) {
|
2015-10-29 07:41:54 +00:00
|
|
|
var name = headerUtils.normalizeFieldName(fieldName);
|
|
|
|
var value = headerUtils.normalizeFieldValue(fieldValue);
|
|
|
|
|
|
|
|
// The following three headers are stored as arrays because their real-world
|
2022-06-02 04:42:21 +00:00
|
|
|
// syntax prevents joining individual headers into a single header using
|
2019-08-27 09:30:04 +00:00
|
|
|
// ",". See also <https://hg.mozilla.org/mozilla-central/diff/9b2a99adc05e/netwerk/protocol/http/src/nsHttpHeaderArray.cpp#l77>
|
2022-06-02 04:42:21 +00:00
|
|
|
if (merge && name in this._headers) {
|
|
|
|
if (
|
|
|
|
name === "www-authenticate" ||
|
|
|
|
name === "proxy-authenticate" ||
|
|
|
|
name === "set-cookie"
|
|
|
|
) {
|
2015-10-29 07:41:54 +00:00
|
|
|
this._headers[name].push(value);
|
2022-06-02 04:42:21 +00:00
|
|
|
} else {
|
2015-10-29 07:41:54 +00:00
|
|
|
this._headers[name][0] += "," + value;
|
2022-06-02 04:42:21 +00:00
|
|
|
NS_ASSERT(
|
|
|
|
this._headers[name].length === 1,
|
|
|
|
"how'd a non-special header have multiple values?"
|
|
|
|
);
|
2015-10-29 07:41:54 +00:00
|
|
|
}
|
2022-06-02 04:42:21 +00:00
|
|
|
} else {
|
2015-10-29 07:41:54 +00:00
|
|
|
this._headers[name] = [value];
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2022-06-02 04:42:21 +00:00
|
|
|
setHeaderNoCheck(fieldName, fieldValue) {
|
2019-08-27 09:30:04 +00:00
|
|
|
var name = headerUtils.normalizeFieldName(fieldName);
|
|
|
|
var value = headerUtils.normalizeFieldValue(fieldValue);
|
|
|
|
if (name in this._headers) {
|
2022-06-02 04:42:21 +00:00
|
|
|
this._headers[name].push(value);
|
2019-08-27 09:30:04 +00:00
|
|
|
} else {
|
2022-06-02 04:42:21 +00:00
|
|
|
this._headers[name] = [value];
|
2019-08-27 09:30:04 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-10-29 07:41:54 +00:00
|
|
|
/**
|
|
|
|
* Returns the value for the header specified by this.
|
|
|
|
*
|
|
|
|
* @throws NS_ERROR_INVALID_ARG
|
|
|
|
* if fieldName does not constitute a valid header field name
|
|
|
|
* @throws NS_ERROR_NOT_AVAILABLE
|
|
|
|
* if the given header does not exist in this
|
|
|
|
* @returns string
|
|
|
|
* the field value for the given header, possibly with non-semantic changes
|
|
|
|
* (i.e., leading/trailing whitespace stripped, whitespace runs replaced
|
2022-06-02 04:42:21 +00:00
|
|
|
* with spaces, etc.) at the option of the implementation; multiple
|
|
|
|
* instances of the header will be combined with a comma, except for
|
2015-10-29 07:41:54 +00:00
|
|
|
* the three headers noted in the description of getHeaderValues
|
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
getHeader(fieldName) {
|
2015-10-29 07:41:54 +00:00
|
|
|
return this.getHeaderValues(fieldName).join("\n");
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the value for the header specified by fieldName as an array.
|
|
|
|
*
|
|
|
|
* @throws NS_ERROR_INVALID_ARG
|
|
|
|
* if fieldName does not constitute a valid header field name
|
|
|
|
* @throws NS_ERROR_NOT_AVAILABLE
|
|
|
|
* if the given header does not exist in this
|
|
|
|
* @returns [string]
|
|
|
|
* an array of all the header values in this for the given
|
|
|
|
* header name. Header values will generally be collapsed
|
|
|
|
* into a single header by joining all header values together
|
|
|
|
* with commas, but certain headers (Proxy-Authenticate,
|
|
|
|
* WWW-Authenticate, and Set-Cookie) violate the HTTP spec
|
|
|
|
* and cannot be collapsed in this manner. For these headers
|
|
|
|
* only, the returned array may contain multiple elements if
|
|
|
|
* that header has been added more than once.
|
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
getHeaderValues(fieldName) {
|
2015-10-29 07:41:54 +00:00
|
|
|
var name = headerUtils.normalizeFieldName(fieldName);
|
|
|
|
|
2022-06-02 04:42:21 +00:00
|
|
|
if (name in this._headers) {
|
2015-10-29 07:41:54 +00:00
|
|
|
return this._headers[name];
|
2022-06-02 04:42:21 +00:00
|
|
|
}
|
|
|
|
throw Components.Exception("", Cr.NS_ERROR_NOT_AVAILABLE);
|
2015-10-29 07:41:54 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if a header with the given field name exists in this, false
|
|
|
|
* otherwise.
|
|
|
|
*
|
|
|
|
* @param fieldName : string
|
|
|
|
* the field name whose existence is to be determined in this
|
|
|
|
* @throws NS_ERROR_INVALID_ARG
|
|
|
|
* if fieldName does not constitute a valid header field name
|
|
|
|
* @returns boolean
|
|
|
|
* true if the header's present, false otherwise
|
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
hasHeader(fieldName) {
|
2015-10-29 07:41:54 +00:00
|
|
|
var name = headerUtils.normalizeFieldName(fieldName);
|
2022-06-02 04:42:21 +00:00
|
|
|
return name in this._headers;
|
2015-10-29 07:41:54 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a new enumerator over the field names of the headers in this, as
|
|
|
|
* nsISupportsStrings. The names returned will be in lowercase, regardless of
|
|
|
|
* how they were input using setHeader (header names are case-insensitive per
|
|
|
|
* RFC 2616).
|
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
get enumerator() {
|
2015-10-29 07:41:54 +00:00
|
|
|
var headers = [];
|
2022-06-02 04:42:21 +00:00
|
|
|
for (var i in this._headers) {
|
2015-10-29 07:41:54 +00:00
|
|
|
var supports = new SupportsString();
|
|
|
|
supports.data = i;
|
|
|
|
headers.push(supports);
|
|
|
|
}
|
|
|
|
|
|
|
|
return new nsSimpleEnumerator(headers);
|
2022-06-02 04:42:21 +00:00
|
|
|
},
|
2015-10-29 07:41:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructs an nsISimpleEnumerator for the given array of items.
|
|
|
|
*
|
|
|
|
* @param items : Array
|
|
|
|
* the items, which must all implement nsISupports
|
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
function nsSimpleEnumerator(items) {
|
2015-10-29 07:41:54 +00:00
|
|
|
this._items = items;
|
|
|
|
this._nextIndex = 0;
|
|
|
|
}
|
2022-06-02 04:42:21 +00:00
|
|
|
nsSimpleEnumerator.prototype = {
|
|
|
|
hasMoreElements() {
|
2015-10-29 07:41:54 +00:00
|
|
|
return this._nextIndex < this._items.length;
|
|
|
|
},
|
2022-06-02 04:42:21 +00:00
|
|
|
getNext() {
|
|
|
|
if (!this.hasMoreElements()) {
|
|
|
|
throw Components.Exception("", Cr.NS_ERROR_NOT_AVAILABLE);
|
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
|
|
|
|
return this._items[this._nextIndex++];
|
|
|
|
},
|
2022-06-02 04:42:21 +00:00
|
|
|
[Symbol.iterator]() {
|
|
|
|
return this._items.values();
|
|
|
|
},
|
|
|
|
QueryInterface: ChromeUtils.generateQI(["nsISimpleEnumerator"]),
|
2015-10-29 07:41:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A representation of the data in an HTTP request.
|
|
|
|
*
|
|
|
|
* @param port : uint
|
|
|
|
* the port on which the server receiving this request runs
|
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
function Request(port) {
|
2015-10-29 07:41:54 +00:00
|
|
|
/** Method of this request, e.g. GET or POST. */
|
|
|
|
this._method = "";
|
|
|
|
|
|
|
|
/** Path of the requested resource; empty paths are converted to '/'. */
|
|
|
|
this._path = "";
|
|
|
|
|
|
|
|
/** Query string, if any, associated with this request (not including '?'). */
|
|
|
|
this._queryString = "";
|
|
|
|
|
|
|
|
/** Scheme of requested resource, usually http, always lowercase. */
|
|
|
|
this._scheme = "http";
|
|
|
|
|
|
|
|
/** Hostname on which the requested resource resides. */
|
|
|
|
this._host = undefined;
|
|
|
|
|
|
|
|
/** Port number over which the request was received. */
|
|
|
|
this._port = port;
|
|
|
|
|
|
|
|
var bodyPipe = new Pipe(false, false, 0, PR_UINT32_MAX, null);
|
|
|
|
|
|
|
|
/** Stream from which data in this request's body may be read. */
|
|
|
|
this._bodyInputStream = bodyPipe.inputStream;
|
|
|
|
|
|
|
|
/** Stream to which data in this request's body is written. */
|
|
|
|
this._bodyOutputStream = bodyPipe.outputStream;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The headers in this request.
|
|
|
|
*/
|
|
|
|
this._headers = new nsHttpHeaders();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* For the addition of ad-hoc properties and new functionality without having
|
|
|
|
* to change nsIHttpRequest every time; currently lazily created, as its only
|
|
|
|
* use is in directory listings.
|
|
|
|
*/
|
|
|
|
this._bag = null;
|
|
|
|
}
|
2022-06-02 04:42:21 +00:00
|
|
|
Request.prototype = {
|
2015-10-29 07:41:54 +00:00
|
|
|
// SERVER METADATA
|
|
|
|
|
|
|
|
//
|
|
|
|
// see nsIHttpRequest.scheme
|
|
|
|
//
|
2022-06-02 04:42:21 +00:00
|
|
|
get scheme() {
|
2015-10-29 07:41:54 +00:00
|
|
|
return this._scheme;
|
|
|
|
},
|
|
|
|
|
|
|
|
//
|
|
|
|
// see nsIHttpRequest.host
|
|
|
|
//
|
2022-06-02 04:42:21 +00:00
|
|
|
get host() {
|
2015-10-29 07:41:54 +00:00
|
|
|
return this._host;
|
|
|
|
},
|
|
|
|
|
|
|
|
//
|
|
|
|
// see nsIHttpRequest.port
|
|
|
|
//
|
2022-06-02 04:42:21 +00:00
|
|
|
get port() {
|
2015-10-29 07:41:54 +00:00
|
|
|
return this._port;
|
|
|
|
},
|
|
|
|
|
|
|
|
// REQUEST LINE
|
|
|
|
|
|
|
|
//
|
|
|
|
// see nsIHttpRequest.method
|
|
|
|
//
|
2022-06-02 04:42:21 +00:00
|
|
|
get method() {
|
2015-10-29 07:41:54 +00:00
|
|
|
return this._method;
|
|
|
|
},
|
|
|
|
|
|
|
|
//
|
|
|
|
// see nsIHttpRequest.httpVersion
|
|
|
|
//
|
2022-06-02 04:42:21 +00:00
|
|
|
get httpVersion() {
|
2015-10-29 07:41:54 +00:00
|
|
|
return this._httpVersion.toString();
|
|
|
|
},
|
|
|
|
|
|
|
|
//
|
|
|
|
// see nsIHttpRequest.path
|
|
|
|
//
|
2022-06-02 04:42:21 +00:00
|
|
|
get path() {
|
2015-10-29 07:41:54 +00:00
|
|
|
return this._path;
|
|
|
|
},
|
|
|
|
|
|
|
|
//
|
|
|
|
// see nsIHttpRequest.queryString
|
|
|
|
//
|
2022-06-02 04:42:21 +00:00
|
|
|
get queryString() {
|
2015-10-29 07:41:54 +00:00
|
|
|
return this._queryString;
|
|
|
|
},
|
|
|
|
|
|
|
|
// HEADERS
|
|
|
|
|
|
|
|
//
|
|
|
|
// see nsIHttpRequest.getHeader
|
|
|
|
//
|
2022-06-02 04:42:21 +00:00
|
|
|
getHeader(name) {
|
2015-10-29 07:41:54 +00:00
|
|
|
return this._headers.getHeader(name);
|
|
|
|
},
|
|
|
|
|
|
|
|
//
|
|
|
|
// see nsIHttpRequest.hasHeader
|
|
|
|
//
|
2022-06-02 04:42:21 +00:00
|
|
|
hasHeader(name) {
|
2015-10-29 07:41:54 +00:00
|
|
|
return this._headers.hasHeader(name);
|
|
|
|
},
|
|
|
|
|
|
|
|
//
|
|
|
|
// see nsIHttpRequest.headers
|
|
|
|
//
|
2022-06-02 04:42:21 +00:00
|
|
|
get headers() {
|
2015-10-29 07:41:54 +00:00
|
|
|
return this._headers.enumerator;
|
|
|
|
},
|
|
|
|
|
|
|
|
//
|
|
|
|
// see nsIPropertyBag.enumerator
|
|
|
|
//
|
2022-06-02 04:42:21 +00:00
|
|
|
get enumerator() {
|
2015-10-29 07:41:54 +00:00
|
|
|
this._ensurePropertyBag();
|
|
|
|
return this._bag.enumerator;
|
|
|
|
},
|
|
|
|
|
|
|
|
//
|
|
|
|
// see nsIHttpRequest.headers
|
|
|
|
//
|
2022-06-02 04:42:21 +00:00
|
|
|
get bodyInputStream() {
|
2015-10-29 07:41:54 +00:00
|
|
|
return this._bodyInputStream;
|
|
|
|
},
|
|
|
|
|
|
|
|
//
|
|
|
|
// see nsIPropertyBag.getProperty
|
|
|
|
//
|
2022-06-02 04:42:21 +00:00
|
|
|
getProperty(name) {
|
2015-10-29 07:41:54 +00:00
|
|
|
this._ensurePropertyBag();
|
|
|
|
return this._bag.getProperty(name);
|
|
|
|
},
|
|
|
|
|
|
|
|
// NSISUPPORTS
|
|
|
|
|
|
|
|
//
|
|
|
|
// see nsISupports.QueryInterface
|
|
|
|
//
|
2022-06-02 04:42:21 +00:00
|
|
|
QueryInterface: ChromeUtils.generateQI(["nsIHttpRequest"]),
|
2015-10-29 07:41:54 +00:00
|
|
|
|
|
|
|
// PRIVATE IMPLEMENTATION
|
2022-06-02 04:42:21 +00:00
|
|
|
|
2015-10-29 07:41:54 +00:00
|
|
|
/** Ensures a property bag has been created for ad-hoc behaviors. */
|
2022-06-02 04:42:21 +00:00
|
|
|
_ensurePropertyBag() {
|
|
|
|
if (!this._bag) {
|
2015-10-29 07:41:54 +00:00
|
|
|
this._bag = new WritablePropertyBag();
|
2022-06-02 04:42:21 +00:00
|
|
|
}
|
|
|
|
},
|
2015-10-29 07:41:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new HTTP server listening for loopback traffic on the given port,
|
|
|
|
* starts it, and runs the server until the server processes a shutdown request,
|
|
|
|
* spinning an event loop so that events posted by the server's socket are
|
|
|
|
* processed.
|
|
|
|
*
|
|
|
|
* This method is primarily intended for use in running this script from within
|
|
|
|
* xpcshell and running a functional HTTP server without having to deal with
|
|
|
|
* non-essential details.
|
|
|
|
*
|
|
|
|
* Note that running multiple servers using variants of this method probably
|
|
|
|
* doesn't work, simply due to how the internal event loop is spun and stopped.
|
|
|
|
*
|
|
|
|
* @note
|
|
|
|
* This method only works with Mozilla 1.9 (i.e., Firefox 3 or trunk code);
|
|
|
|
* you should use this server as a component in Mozilla 1.8.
|
|
|
|
* @param port
|
|
|
|
* the port on which the server will run, or -1 if there exists no preference
|
|
|
|
* for a specific port; note that attempting to use some values for this
|
|
|
|
* parameter (particularly those below 1024) may cause this method to throw or
|
|
|
|
* may result in the server being prematurely shut down
|
|
|
|
* @param basePath
|
|
|
|
* a local directory from which requests will be served (i.e., if this is
|
|
|
|
* "/home/jwalden/" then a request to /index.html will load
|
|
|
|
* /home/jwalden/index.html); if this is omitted, only the default URLs in
|
|
|
|
* this server implementation will be functional
|
|
|
|
*/
|
2022-06-02 04:42:21 +00:00
|
|
|
function server(port, basePath) {
|
|
|
|
if (basePath) {
|
|
|
|
var lp = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsIFile);
|
2015-10-29 07:41:54 +00:00
|
|
|
lp.initWithPath(basePath);
|
|
|
|
}
|
|
|
|
|
|
|
|
// if you're running this, you probably want to see debugging info
|
|
|
|
DEBUG = true;
|
|
|
|
|
|
|
|
var srv = new nsHttpServer();
|
2022-06-02 04:42:21 +00:00
|
|
|
if (lp) {
|
2015-10-29 07:41:54 +00:00
|
|
|
srv.registerDirectory("/", lp);
|
2022-06-02 04:42:21 +00:00
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
srv.registerContentType("sjs", SJS_TYPE);
|
|
|
|
srv.identity.setPrimary("http", "localhost", port);
|
|
|
|
srv.start(port);
|
|
|
|
|
|
|
|
var thread = gThreadManager.currentThread;
|
2022-06-02 04:42:21 +00:00
|
|
|
while (!srv.isStopped()) {
|
2015-10-29 07:41:54 +00:00
|
|
|
thread.processNextEvent(true);
|
2022-06-02 04:42:21 +00:00
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
|
|
|
|
// get rid of any pending requests
|
2022-06-02 04:42:21 +00:00
|
|
|
while (thread.hasPendingEvents()) {
|
2015-10-29 07:41:54 +00:00
|
|
|
thread.processNextEvent(true);
|
2022-06-02 04:42:21 +00:00
|
|
|
}
|
2015-10-29 07:41:54 +00:00
|
|
|
|
|
|
|
DEBUG = false;
|
|
|
|
}
|