Go to the source code of this file.
Functions | |
function | NS_ASSERT (cond, msg) |
function | HttpError (code, description) |
function | array2obj (arr) |
function | range (x, y) |
function | dumpn (str) |
function | dumpStack () |
function | getRootPrefBranch () |
function | toDateString (date) |
function | printObj (o, showMembers) |
function | nsHttpServer () |
function | ServerIdentity () |
function | Connection (input, output, server, port, outgoingPort, number) |
function | readBytes (inputStream, count) |
function | RequestReader (connection) |
function | findCRLF (array) |
function | LineData () |
function | createHandlerFunc (handler) |
function | defaultIndexHandler (metadata, response) |
function | fileSort (a, b) |
function | toInternalPath (path, encoded) |
function | maybeAddHeaders (file, metadata, response) |
function | ServerHandler (server) |
function | FileMap () |
function | isCTL (code) |
function | Response (connection) |
function | notImplemented () |
function | WriteThroughCopier (input, output, observer, context) |
function | htmlEscape (str) |
function | nsHttpVersion (versionString) |
function | nsHttpHeaders () |
function | nsSimpleEnumerator (items) |
function | Request (port) |
function | makeFactory (ctor) |
function | NSGetModule (compMgr, fileSpec) |
function | server (port, basePath) |
Variables | |
const | Cc = Components.classes |
const | Ci = Components.interfaces |
const | Cr = Components.results |
const | Cu = Components.utils |
const | CC = Components.Constructor |
const | PR_UINT32_MAX = Math.pow(2, 32) - 1 |
const | EXPORTED_SYMBOLS = ["server", "nsHttpServer", "HttpError"] |
var | DEBUG = false |
var | gGlobalObject = this |
HttpError | prototype |
const | HTTP_400 = new HttpError(400, "Bad Request") |
const | HTTP_401 = new HttpError(401, "Unauthorized") |
const | HTTP_402 = new HttpError(402, "Payment Required") |
const | HTTP_403 = new HttpError(403, "Forbidden") |
const | HTTP_404 = new HttpError(404, "Not Found") |
const | HTTP_405 = new HttpError(405, "Method Not Allowed") |
const | HTTP_406 = new HttpError(406, "Not Acceptable") |
const | HTTP_407 = new HttpError(407, "Proxy Authentication Required") |
const | HTTP_408 = new HttpError(408, "Request Timeout") |
const | HTTP_409 = new HttpError(409, "Conflict") |
const | HTTP_410 = new HttpError(410, "Gone") |
const | HTTP_411 = new HttpError(411, "Length Required") |
const | HTTP_412 = new HttpError(412, "Precondition Failed") |
const | HTTP_413 = new HttpError(413, "Request Entity Too Large") |
const | HTTP_414 = new HttpError(414, "Request-URI Too Long") |
const | HTTP_415 = new HttpError(415, "Unsupported Media Type") |
const | HTTP_416 = new HttpError(416, "Requested Range Not Satisfiable") |
const | HTTP_417 = new HttpError(417, "Expectation Failed") |
const | HTTP_500 = new HttpError(500, "Internal Server Error") |
const | HTTP_501 = new HttpError(501, "Not Implemented") |
const | HTTP_502 = new HttpError(502, "Bad Gateway") |
const | HTTP_503 = new HttpError(503, "Service Unavailable") |
const | HTTP_504 = new HttpError(504, "Gateway Timeout") |
const | HTTP_505 = new HttpError(505, "HTTP Version Not Supported") |
const | HTTP_ERROR_CODES = array2obj(range(400, 417).concat(range(500, 505))) |
const | HIDDEN_CHAR = "^" |
const | HEADERS_SUFFIX = HIDDEN_CHAR + "headers" + HIDDEN_CHAR |
const | SJS_TYPE = "sjs" |
var | gThreadManager = null |
var | gRootPrefBranch = null |
const | ServerSocket |
const | BinaryInputStream |
const | BinaryOutputStream |
const | ScriptableInputStream |
const | Pipe |
const | FileInputStream |
const | ConverterInputStream |
const | WritablePropertyBag |
const | SupportsString |
const | HOST_REGEX |
const | READER_IN_REQUEST_LINE = 0 |
const | READER_IN_HEADERS = 1 |
const | READER_IN_BODY = 2 |
const | READER_FINISHED = 3 |
const | CR = 0x0D |
const | LF = 0x0A |
const | IS_TOKEN_ARRAY |
Response | SEGMENT_SIZE = 8192 |
const | headerUtils |
nsHttpVersion | HTTP_1_0 = new nsHttpVersion("1.0") |
nsHttpVersion | HTTP_1_1 = new nsHttpVersion("1.1") |
const | module |
function array2obj | ( | arr | ) |
function Connection | ( | input, | |
output, | |||
server, | |||
port, | |||
outgoingPort, | |||
number | |||
) |
Represents a connection to the server (and possibly in the future the thread on which the connection is processed).
input | : nsIInputStream stream from which incoming data on the connection is read |
output | : nsIOutputStream stream to write data out the connection |
server | : nsHttpServer the server handling the connection |
port | : int the port on which the server is running |
outgoingPort | : int the outgoing port used by this connection |
number | : uint a serial number used to uniquely identify this connection |
Stream of incoming data.
Stream for outgoing data.
The server associated with this request.
The port on which the server is running.
The outgoing poort used by this connection.
The serial number of this connection.
The request for which a response is being generated, null if the incoming request has not been fully received or if it had errors.
State variables for debugging.
Definition at line 1049 of file httpd.js.
function createHandlerFunc | ( | handler | ) |
Creates a request-handling function for an nsIHttpRequestHandler object.
function defaultIndexHandler | ( | metadata, | |
response | |||
) |
function dumpn | ( | str | ) |
function dumpStack | ( | ) |
function FileMap | ( | ) |
function fileSort | ( | a, | |
b | |||
) |
function findCRLF | ( | array | ) |
Calculates the number of characters before the first CRLF pair in array, or -1 if the array contains no CRLF pair.
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 |
function htmlEscape | ( | str | ) |
function HttpError | ( | code, | |
description | |||
) |
function isCTL | ( | code | ) |
function LineData | ( | ) |
function makeFactory | ( | ctor | ) |
function maybeAddHeaders | ( | file, | |
metadata, | |||
response | |||
) |
Adds custom-specified headers for the given file to the given response, if any such headers are specified.
file | the file on the disk which is to be written |
metadata | metadata about the incoming request |
response | the Response to which any specified headers/data should be written |
HTTP_500 | if an error occurred while processing custom-specified headers |
Definition at line 2012 of file httpd.js.
function notImplemented | ( | ) |
function NS_ASSERT | ( | cond, | |
msg | |||
) |
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).
Definition at line 70 of file httpd.js.
function NSGetModule | ( | compMgr, | |
fileSpec | |||
) |
function nsHttpHeaders | ( | ) |
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.
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.
Definition at line 4285 of file httpd.js.
function nsHttpServer | ( | ) |
Instantiates a new HTTP server.
The port on which this server listens.
The socket associated with this.
The handler used to process requests to this server.
Naming information for this server.
Indicates when the server is to be shut down at the end of the request.
True if the socket in this is closed (and closure notifications have been sent and processed if the socket was ever opened), false otherwise.
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.
Hash of all open connections, indexed by connection number at time of creation.
Definition at line 339 of file httpd.js.
function nsHttpVersion | ( | versionString | ) |
Constructs an object representing an HTTP version (see section 3.1).
versionString | a string of the form "#.#", where # is an non-negative decimal integer with or without leading zeros |
if | versionString does not specify a valid HTTP version number |
The major version number of this, as a number.
The minor version number of this, as a number.
Definition at line 4223 of file httpd.js.
function nsSimpleEnumerator | ( | items | ) |
function printObj | ( | o, | |
showMembers | |||
) |
function range | ( | x, | |
y | |||
) |
function readBytes | ( | inputStream, | |
count | |||
) |
function Request | ( | port | ) |
A representation of the data in an HTTP request.
port | : uint the port on which the server receiving this request runs |
Method of this request, e.g. GET or POST.
Path of the requested resource; empty paths are converted to '/'.
Query string, if any, associated with this request (not including '?').
Scheme of requested resource, usually http, always lowercase.
Hostname on which the requested resource resides.
Port number over which the request was received.
Stream from which data in this request's body may be read.
Stream to which data in this request's body is written.
The headers in this request.
For the addition of ad-hoc properties and new functionality without having to change nsIHttpRequestMetadata every time; currently lazily created, as its only use is in directory listings.
Definition at line 4467 of file httpd.js.
function RequestReader | ( | connection | ) |
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.
connection | : Connection the connection for the request being read |
Connection metadata for this request.
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).
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.
The current state of parsing the incoming request.
Metadata constructed from the incoming request for the request handler.
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.
Definition at line 1180 of file httpd.js.
function Response | ( | connection | ) |
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.
connection | : Connection the connection over which this response is to be written |
The connection over which this response will be written.
The HTTP version of this response; defaults to 1.1 if not set by the handler.
The HTTP code of this response; defaults to 200.
The description of the HTTP code in this response; defaults to "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()).
Set to true when this response is ended (completely constructed if possible and the connection closed); further actions on this will then fail.
A stream used to hold data written to the body of this response.
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.)
The stream copier which copies data written to the body by a request handler to the network.
True if this response has been designated as being processed asynchronously rather than for the duration of a single call to nsIHttpRequestHandler.handle.
True iff finish() has been called on this, signaling that no more changes to this may be made.
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).
Definition at line 3334 of file httpd.js.
function server | ( | port, | |
basePath | |||
) |
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.
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 |
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 |
Definition at line 4758 of file httpd.js.
function ServerHandler | ( | server | ) |
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.
server | : nsHttpServer the server in which this handler is being used |
The nsHttpServer instance associated with this handler.
A FileMap object containing the set of path->nsILocalFile mappings for 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!
Custom request handlers for the server in which this resides. Path-handler pairs are stored as property-value pairs in this property.
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.
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.
Maps file extensions to their MIME types in the server, overriding any mapping that might or might not exist in the MIME service.
The default handler for requests for directories, used to serve directories when no index file is present.
Per-path state storage for the server.
Entire-server state storage.
Entire-server state storage for nsISupports values.
Definition at line 2101 of file httpd.js.
function ServerIdentity | ( | ) |
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.
The scheme of the primary location.
The hostname of the primary location.
The port number of the primary location.
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.
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".
Definition at line 811 of file httpd.js.
function toDateString | ( | date | ) |
Returns the RFC 822/1123 representation of a date.
date | : Number the date, in milliseconds from midnight (00:00:00), January 1, 1970 GMT |
Processes a date and returns the encoded UTC time as a string according to the format specified in RFC 2616.
date | : Date the date to process |
Processes a date and returns the encoded UTC date as a string according to the date1 format specified in RFC 2616.
date | : Date the date to process |
function toInternalPath | ( | path, | |
encoded | |||
) |
Converts an externally-provided path into an internal path for use in determining file mappings.
path | the path to convert |
encoded | true if the given path should be passed through decodeURI prior to conversion |
URIError | if path is incorrectly encoded |
function WriteThroughCopier | ( | input, | |
output, | |||
observer, | |||
context | |||
) |
Copies data from input to output as it becomes available.
input | : nsIAsyncInputStream the stream from which data is to be read |
output | : nsIOutputStream the stream to which data is to be copied |
observer | : nsIRequestObserver an observer which will be notified when the copy starts and finishes |
context | : nsISupports context passed to observer when notified of start/stop |
NS_ERROR_NULL_POINTER | if input, output, or observer are null |
Stream from which data is being read.
Stream to which data is being written.
Observer watching this copy.
Context for the observer watching this.
False until cancel() is called, when this copy is completed.
Required by nsIRequest, meaningless.
Required by nsIRequest, meaningless.
Required by nsIRequest, meaningless.
Status of this request.
Definition at line 3949 of file httpd.js.
const BinaryInputStream |
const BinaryOutputStream |
const Ci = Components.interfaces |
const ConverterInputStream |
var DEBUG = false |
const EXPORTED_SYMBOLS = ["server", "nsHttpServer", "HttpError"] |
const FileInputStream |
const HEADERS_SUFFIX = HIDDEN_CHAR + "headers" + HIDDEN_CHAR |
const headerUtils |
const HIDDEN_CHAR = "^" |
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 HOST_REGEX |
nsHttpVersion HTTP_1_0 = new nsHttpVersion("1.0") |
nsHttpVersion HTTP_1_1 = new nsHttpVersion("1.1") |
const HTTP_407 = new HttpError(407, "Proxy Authentication Required") |
const HTTP_413 = new HttpError(413, "Request Entity Too Large") |
const HTTP_414 = new HttpError(414, "Request-URI Too Long") |
const HTTP_415 = new HttpError(415, "Unsupported Media Type") |
const HTTP_416 = new HttpError(416, "Requested Range Not Satisfiable") |
const HTTP_500 = new HttpError(500, "Internal Server Error") |
const HTTP_505 = new HttpError(505, "HTTP Version Not Supported") |
const IS_TOKEN_ARRAY |
const Pipe |
Request prototype |
const READER_IN_REQUEST_LINE = 0 |
const ScriptableInputStream |
Response SEGMENT_SIZE = 8192 |
const ServerSocket |
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.
const SJS_TYPE = "sjs" |
const SupportsString |
const WritablePropertyBag |