nsIHttpServer.idl (26469B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 #include "nsISupports.idl" 6 7 interface nsIInputStream; 8 interface nsIFile; 9 interface nsIOutputStream; 10 interface nsISimpleEnumerator; 11 12 interface nsIHttpServer; 13 interface nsIHttpServerStoppedCallback; 14 interface nsIHttpRequestHandler; 15 interface nsIHttpRequest; 16 interface nsIHttpResponse; 17 interface nsIHttpServerIdentity; 18 19 /** 20 * An interface which represents an HTTP server. 21 */ 22 [scriptable, uuid(cea8812e-faa6-4013-9396-f9936cbb74ec)] 23 interface nsIHttpServer : nsISupports 24 { 25 /** 26 * Starts up this server, listening upon the given port. 27 * 28 * @param port 29 * the port upon which listening should happen, or -1 if no specific port is 30 * desired 31 * @throws NS_ERROR_ALREADY_INITIALIZED 32 * if this server is already started 33 * @throws NS_ERROR_NOT_AVAILABLE 34 * if the server is not started and cannot be started on the desired port 35 * (perhaps because the port is already in use or because the process does 36 * not have privileges to do so) 37 * @note 38 * Behavior is undefined if this method is called after stop() has been 39 * called on this but before the provided callback function has been 40 * called. 41 */ 42 void start(in long port); 43 44 /** 45 * Starts up this server, listening upon the given port on a ipv6 adddress. 46 * 47 * @param port 48 * the port upon which listening should happen, or -1 if no specific port is 49 * desired 50 * @throws NS_ERROR_ALREADY_INITIALIZED 51 * if this server is already started 52 * @throws NS_ERROR_NOT_AVAILABLE 53 * if the server is not started and cannot be started on the desired port 54 * (perhaps because the port is already in use or because the process does 55 * not have privileges to do so) 56 * @note 57 * Behavior is undefined if this method is called after stop() has been 58 * called on this but before the provided callback function has been 59 * called. 60 */ 61 void start_ipv6(in long port); 62 63 /** 64 * Like the two functions above, but this server supports both IPv6 and 65 * IPv4 addresses. 66 */ 67 void start_dualStack(in long port); 68 69 /** 70 * Shuts down this server if it is running (including the period of time after 71 * stop() has been called but before the provided callback has been called). 72 * 73 * @param callback 74 * an asynchronous callback used to notify the user when this server is 75 * stopped and all pending requests have been fully served 76 * @throws NS_ERROR_NULL_POINTER 77 * if callback is null 78 * @throws NS_ERROR_UNEXPECTED 79 * if this server is not running 80 */ 81 void stop(in nsIHttpServerStoppedCallback callback); 82 83 /** 84 * Associates the local file represented by the string file with all requests 85 * which match request. 86 * 87 * @param path 88 * the path which is to be mapped to the given file; must begin with "/" and 89 * be a valid URI path (i.e., no query string, hash reference, etc.) 90 * @param file 91 * the file to serve for the given path, or null to remove any mapping that 92 * might exist; this file must exist for the lifetime of the server 93 * @param handler 94 * an optional object which can be used to handle any further changes. 95 */ 96 void registerFile(in string path, 97 in nsIFile file, 98 [optional] in nsIHttpRequestHandler handler); 99 100 /** 101 * Registers a custom path handler. 102 * 103 * @param path 104 * the path on the server (beginning with a "/") which is to be handled by 105 * handler; this path must not include a query string or hash component; it 106 * also should usually be canonicalized, since most browsers will do so 107 * before sending otherwise-matching requests 108 * @param handler 109 * an object which will handle any requests for the given path, or null to 110 * remove any existing handler; if while the server is running the handler 111 * throws an exception while responding to a request, an HTTP 500 response 112 * will be returned 113 * @throws NS_ERROR_INVALID_ARG 114 * if path does not begin with a "/" 115 */ 116 void registerPathHandler(in string path, in nsIHttpRequestHandler handler); 117 118 /** 119 * Registers a custom prefix handler. 120 * 121 * @param prefix 122 * the path on the server (beginning and ending with "/") which is to be 123 * handled by handler; this path must not include a query string or hash 124 * component. All requests that start with this prefix will be directed to 125 * the given handler. 126 * @param handler 127 * an object which will handle any requests for the given path, or null to 128 * remove any existing handler; if while the server is running the handler 129 * throws an exception while responding to a request, an HTTP 500 response 130 * will be returned 131 * @throws NS_ERROR_INVALID_ARG 132 * if path does not begin with a "/" or does not end with a "/" 133 */ 134 void registerPrefixHandler(in string prefix, in nsIHttpRequestHandler handler); 135 136 /** 137 * Registers a custom error page handler. 138 * 139 * @param code 140 * the error code which is to be handled by handler 141 * @param handler 142 * an object which will handle any requests which generate the given status 143 * code, or null to remove any existing handler. If the handler throws an 144 * exception during server operation, fallback is to the genericized error 145 * handler (the x00 version), then to 500, using a user-defined error 146 * handler if one exists or the server default handler otherwise. Fallback 147 * will never occur from a user-provided handler that throws to the same 148 * handler as provided by the server, e.g. a throwing user 404 falls back to 149 * 400, not a server-provided 404 that might not throw. 150 * @note 151 * If the error handler handles HTTP 500 and throws, behavior is undefined. 152 */ 153 void registerErrorHandler(in unsigned long code, in nsIHttpRequestHandler handler); 154 155 /** 156 * Maps all requests to paths beneath path to the corresponding file beneath 157 * dir. 158 * 159 * @param path 160 * the absolute path on the server against which requests will be served 161 * from dir (e.g., "/", "/foo/", etc.); must begin and end with a forward 162 * slash 163 * @param dir 164 * the directory to be used to serve all requests for paths underneath path 165 * (except those further overridden by another, deeper path registered with 166 * another directory); if null, any current mapping for the given path is 167 * removed 168 * @throws NS_ERROR_INVALID_ARG 169 * if dir is non-null and does not exist or is not a directory, or if path 170 * does not begin with and end with a forward slash 171 */ 172 void registerDirectory(in string path, in nsIFile dir); 173 174 /** 175 * Associates files with the given extension with the given Content-Type when 176 * served by this server, in the absence of any file-specific information 177 * about the desired Content-Type. If type is empty, removes any extant 178 * mapping, if one is present. 179 * 180 * @throws NS_ERROR_INVALID_ARG 181 * if the given type is not a valid header field value, i.e. if it doesn't 182 * match the field-value production in RFC 2616 183 * @note 184 * No syntax checking is done of the given type, beyond ensuring that it is 185 * a valid header field value. Behavior when not given a string matching 186 * the media-type production in RFC 2616 section 3.7 is undefined. 187 * Implementations may choose to define specific behavior for types which do 188 * not match the production, such as for CGI functionality. 189 * @note 190 * Implementations MAY treat type as a trusted argument; users who fail to 191 * generate this string from trusted data risk security vulnerabilities. 192 */ 193 void registerContentType(in string extension, in string type); 194 195 /** 196 * Sets the handler used to display the contents of a directory if 197 * the directory contains no index page. 198 * 199 * @param handler 200 * an object which will handle any requests for directories which 201 * do not contain index pages, or null to reset to the default 202 * index handler; if while the server is running the handler 203 * throws an exception while responding to a request, an HTTP 500 204 * response will be returned. An nsIFile corresponding to the 205 * directory is available from the metadata object passed to the 206 * handler, under the key "directory". 207 */ 208 void setIndexHandler(in nsIHttpRequestHandler handler); 209 210 /** Represents the locations at which this server is reachable. */ 211 readonly attribute nsIHttpServerIdentity identity; 212 213 /** 214 * Retrieves the string associated with the given key in this, for the given 215 * path's saved state. All keys are initially associated with the empty 216 * string. 217 */ 218 AString getState(in AString path, in AString key); 219 220 /** 221 * Sets the string associated with the given key in this, for the given path's 222 * saved state. 223 */ 224 void setState(in AString path, in AString key, in AString value); 225 226 /** 227 * Retrieves the string associated with the given key in this, in 228 * entire-server saved state. All keys are initially associated with the 229 * empty string. 230 */ 231 AString getSharedState(in AString key); 232 233 /** 234 * Sets the string associated with the given key in this, in entire-server 235 * saved state. 236 */ 237 void setSharedState(in AString key, in AString value); 238 239 /** 240 * Retrieves the object associated with the given key in this in 241 * object-valued saved state. All keys are initially associated with null. 242 */ 243 nsISupports getObjectState(in AString key); 244 245 /** 246 * Sets the object associated with the given key in this in object-valued 247 * saved state. The value may be null. 248 */ 249 void setObjectState(in AString key, in nsISupports value); 250 }; 251 252 /** 253 * An interface through which a notification of the complete stopping (socket 254 * closure, in-flight requests all fully served and responded to) of an HTTP 255 * server may be received. 256 */ 257 [scriptable, function, uuid(925a6d33-9937-4c63-abe1-a1c56a986455)] 258 interface nsIHttpServerStoppedCallback : nsISupports 259 { 260 /** Called when the corresponding server has been fully stopped. */ 261 void onStopped(); 262 }; 263 264 /** 265 * Represents a set of names for a server, one of which is the primary name for 266 * the server and the rest of which are secondary. By default every server will 267 * contain ("http", "localhost", port) and ("http", "127.0.0.1", port) as names, 268 * where port is what was provided to the corresponding server when started; 269 * however, except for their being removed when the corresponding server stops 270 * they have no special importance. 271 */ 272 [scriptable, uuid(a89de175-ae8e-4c46-91a5-0dba99bbd284)] 273 interface nsIHttpServerIdentity : nsISupports 274 { 275 /** 276 * The primary scheme at which the corresponding server is located, defaulting 277 * to 'http'. This name will be the value of nsIHttpRequest.scheme for 278 * HTTP/1.0 requests. 279 * 280 * This value is always set when the corresponding server is running. If the 281 * server is not running, this value is set only if it has been set to a 282 * non-default name using setPrimary. In this case reading this value will 283 * throw NS_ERROR_NOT_INITIALIZED. 284 */ 285 readonly attribute string primaryScheme; 286 287 /** 288 * The primary name by which the corresponding server is known, defaulting to 289 * 'localhost'. This name will be the value of nsIHttpRequest.host for 290 * HTTP/1.0 requests. 291 * 292 * This value is always set when the corresponding server is running. If the 293 * server is not running, this value is set only if it has been set to a 294 * non-default name using setPrimary. In this case reading this value will 295 * throw NS_ERROR_NOT_INITIALIZED. 296 */ 297 readonly attribute string primaryHost; 298 299 /** 300 * The primary port on which the corresponding server runs, defaulting to the 301 * associated server's port. This name will be the value of 302 * nsIHttpRequest.port for HTTP/1.0 requests. 303 * 304 * This value is always set when the corresponding server is running. If the 305 * server is not running, this value is set only if it has been set to a 306 * non-default name using setPrimary. In this case reading this value will 307 * throw NS_ERROR_NOT_INITIALIZED. 308 */ 309 readonly attribute long primaryPort; 310 311 /** 312 * Adds a location at which this server may be accessed. 313 * 314 * @throws NS_ERROR_ILLEGAL_VALUE 315 * if scheme or host do not match the scheme or host productions imported 316 * into RFC 2616 from RFC 2396, or if port is not a valid port number 317 */ 318 void add(in string scheme, in string host, in long port); 319 320 /** 321 * Removes this name from the list of names by which the corresponding server 322 * is known. If name is also the primary name for the server, the primary 323 * name reverts to 'http://127.0.0.1' with the associated server's port. 324 * 325 * @throws NS_ERROR_ILLEGAL_VALUE 326 * if scheme or host do not match the scheme or host productions imported 327 * into RFC 2616 from RFC 2396, or if port is not a valid port number 328 * @returns 329 * true if the given name was a name for this server, false otherwise 330 */ 331 boolean remove(in string scheme, in string host, in long port); 332 333 /** 334 * Returns true if the given name is in this, false otherwise. 335 * 336 * @throws NS_ERROR_ILLEGAL_VALUE 337 * if scheme or host do not match the scheme or host productions imported 338 * into RFC 2616 from RFC 2396, or if port is not a valid port number 339 */ 340 boolean has(in string scheme, in string host, in long port); 341 342 /** 343 * Returns the scheme for the name with the given host and port, if one is 344 * present; otherwise returns the empty string. 345 * 346 * @throws NS_ERROR_ILLEGAL_VALUE 347 * if host does not match the host production imported into RFC 2616 from 348 * RFC 2396, or if port is not a valid port number 349 */ 350 string getScheme(in string host, in long port); 351 352 /** 353 * Designates the given name as the primary name in this and adds it to this 354 * if it is not already present. 355 * 356 * @throws NS_ERROR_ILLEGAL_VALUE 357 * if scheme or host do not match the scheme or host productions imported 358 * into RFC 2616 from RFC 2396, or if port is not a valid port number 359 */ 360 void setPrimary(in string scheme, in string host, in long port); 361 }; 362 363 /** 364 * A representation of a handler for HTTP requests. The handler is used by 365 * calling its .handle method with data for an incoming request; it is the 366 * handler's job to use that data as it sees fit to make the desired response. 367 * 368 * @note 369 * This interface uses the [function] attribute, so you can pass a 370 * script-defined function with the functionality of handle() to any 371 * method which has a nsIHttpRequestHandler parameter, instead of wrapping 372 * it in an otherwise empty object. 373 */ 374 [scriptable, function, uuid(2bbb4db7-d285-42b3-a3ce-142b8cc7e139)] 375 interface nsIHttpRequestHandler : nsISupports 376 { 377 /** 378 * Processes an HTTP request and initializes the passed-in response to reflect 379 * the correct HTTP response. 380 * 381 * If this method throws an exception, externally observable behavior depends 382 * upon whether is being processed asynchronously. If such is the case, the 383 * output is some prefix (perhaps all, perhaps none, perhaps only some) of the 384 * data which would have been sent if, instead, the response had been finished 385 * at that point. If no data has been written, the response has not had 386 * seizePower() called on it, and it is not being asynchronously created, an 387 * error handler will be invoked (usually 500 unless otherwise specified). 388 * 389 * Some uses of nsIHttpRequestHandler may require this method to never throw 390 * an exception; in the general case, however, this method may throw an 391 * exception (causing an HTTP 500 response to occur, if the above conditions 392 * are met). 393 * 394 * @param request 395 * data representing an HTTP request 396 * @param response 397 * an initially-empty response which must be modified to reflect the data 398 * which should be sent as the response to the request described by metadata 399 */ 400 void handle(in nsIHttpRequest request, in nsIHttpResponse response); 401 }; 402 403 404 /** 405 * A representation of the data included in an HTTP request. 406 */ 407 [scriptable, uuid(978cf30e-ad73-42ee-8f22-fe0aaf1bf5d2)] 408 interface nsIHttpRequest : nsISupports 409 { 410 /** 411 * The request type for this request (see RFC 2616, section 5.1.1). 412 */ 413 readonly attribute string method; 414 415 /** 416 * The scheme of the requested path, usually 'http' but might possibly be 417 * 'https' if some form of SSL tunneling is in use. Note that this value 418 * cannot be accurately determined unless the incoming request used the 419 * absolute-path form of the request line; it defaults to 'http', so only 420 * if it is something else can you be entirely certain it's correct. 421 */ 422 readonly attribute string scheme; 423 424 /** 425 * The host of the data being requested (e.g. "localhost" for the 426 * http://localhost:8080/file resource). Note that the relevant port on the 427 * host is specified in this.port. This value is in the ASCII character 428 * encoding. 429 */ 430 readonly attribute string host; 431 432 /** 433 * The port on the server on which the request was received. 434 */ 435 readonly attribute unsigned long port; 436 437 /** 438 * The requested path, without any query string (e.g. "/dir/file.txt"). It is 439 * guaranteed to begin with a "/". The individual components in this string 440 * are URL-encoded. 441 */ 442 readonly attribute string path; 443 444 /** 445 * The URL-encoded query string associated with this request, not including 446 * the initial "?", or "" if no query string was present. 447 */ 448 readonly attribute string queryString; 449 450 /** 451 * A string containing the HTTP version of the request (i.e., "1.1"). Leading 452 * zeros for either component of the version will be omitted. (In other 453 * words, if the request contains the version "1.01", this attribute will be 454 * "1.1"; see RFC 2616, section 3.1.) 455 */ 456 readonly attribute string httpVersion; 457 458 /** 459 * Returns the value for the header in this request specified by fieldName. 460 * 461 * @param fieldName 462 * the name of the field whose value is to be gotten; note that since HTTP 463 * header field names are case-insensitive, this method produces equivalent 464 * results for "HeAdER" and "hEADer" as fieldName 465 * @returns 466 * The result is a string containing the individual values of the header, 467 * usually separated with a comma. The headers WWW-Authenticate, 468 * Proxy-Authenticate, and Set-Cookie violate the HTTP specification, 469 * however, and for these headers only the separator string is '\n'. 470 * 471 * @throws NS_ERROR_INVALID_ARG 472 * if fieldName does not constitute a valid header field name 473 * @throws NS_ERROR_NOT_AVAILABLE 474 * if the given header does not exist in this 475 */ 476 string getHeader(in string fieldName); 477 478 /** 479 * Returns true if a header with the given field name exists in this, false 480 * otherwise. 481 * 482 * @param fieldName 483 * the field name whose existence is to be determined in this; note that 484 * since HTTP header field names are case-insensitive, this method produces 485 * equivalent results for "HeAdER" and "hEADer" as fieldName 486 * @throws NS_ERROR_INVALID_ARG 487 * if fieldName does not constitute a valid header field name 488 */ 489 boolean hasHeader(in string fieldName); 490 491 /** 492 * An nsISimpleEnumerator of nsISupportsStrings over the names of the headers 493 * in this request. The header field names in the enumerator may not 494 * necessarily have the same case as they do in the request itself. 495 */ 496 readonly attribute nsISimpleEnumerator headers; 497 498 /** 499 * A stream from which data appearing in the body of this request can be read. 500 */ 501 readonly attribute nsIInputStream bodyInputStream; 502 }; 503 504 505 /** 506 * Represents an HTTP response, as described in RFC 2616, section 6. 507 */ 508 [scriptable, uuid(1acd16c2-dc59-42fa-9160-4f26c43c1c21)] 509 interface nsIHttpResponse : nsISupports 510 { 511 /** 512 * Sets the status line for this. If this method is never called on this, the 513 * status line defaults to "HTTP/", followed by the server's default HTTP 514 * version (e.g. "1.1"), followed by " 200 OK". 515 * 516 * @param httpVersion 517 * the HTTP version of this, as a string (e.g. "1.1"); if null, the server 518 * default is used 519 * @param code 520 * the numeric HTTP status code for this 521 * @param description 522 * a human-readable description of code; may be null if no description is 523 * desired 524 * @throws NS_ERROR_INVALID_ARG 525 * if httpVersion is not a valid HTTP version string, statusCode is greater 526 * than 999, or description contains invalid characters 527 * @throws NS_ERROR_NOT_AVAILABLE 528 * if this response is being processed asynchronously and data has been 529 * written to this response's body, or if seizePower() has been called on 530 * this 531 */ 532 void setStatusLine(in string httpVersion, 533 in unsigned short statusCode, 534 in string description); 535 536 /** 537 * Sets the specified header in this. 538 * 539 * @param name 540 * the name of the header; must match the field-name production per RFC 2616 541 * @param value 542 * the value of the header; must match the field-value production per RFC 543 * 2616 544 * @param merge 545 * when true, if the given header already exists in this, the values passed 546 * to this function will be merged into the existing header, per RFC 2616 547 * header semantics (except for the Set-Cookie, WWW-Authenticate, and 548 * Proxy-Authenticate headers, which will treat each such merged header as 549 * an additional instance of the header, for real-world compatibility 550 * reasons); when false, replaces any existing header of the given name (if 551 * any exists) with a new header with the specified value, defaults to false 552 * @throws NS_ERROR_INVALID_ARG 553 * if name or value is not a valid header component 554 * @throws NS_ERROR_NOT_AVAILABLE 555 * if this response is being processed asynchronously and data has been 556 * written to this response's body, or if seizePower() has been called on 557 * this 558 */ 559 void setHeader(in string name, in string value, [optional] in boolean merge); 560 561 /** 562 * This is used for testing our header handling, so header will be sent out 563 * without transformation. There can be multiple headers. 564 */ 565 void setHeaderNoCheck(in string name, in string value); 566 567 /** 568 * A stream to which data appearing in the body of this response (or in the 569 * totality of the response if seizePower() is called) should be written. 570 * After this response has been designated as being processed asynchronously, 571 * or after seizePower() has been called on this, subsequent writes will no 572 * longer be buffered and will be written to the underlying transport without 573 * delaying until the entire response is constructed. Write-through may or 574 * may not be synchronous in the implementation, and in any case particular 575 * behavior may not be observable to the HTTP client as intermediate buffers 576 * both in the server socket and in the client may delay written data; be 577 * prepared for delays at any time. 578 * 579 * @throws NS_ERROR_NOT_AVAILABLE 580 * if accessed after this response is fully constructed 581 */ 582 readonly attribute nsIOutputStream bodyOutputStream; 583 584 /** 585 * Writes a string to the response's output stream. This method is merely a 586 * convenient shorthand for writing the same data to bodyOutputStream 587 * directly. 588 * 589 * @note 590 * This method is only guaranteed to work with ASCII data. 591 * @throws NS_ERROR_NOT_AVAILABLE 592 * if called after this response has been fully constructed 593 */ 594 void write(in string data); 595 596 /** 597 * Signals that this response is being constructed asynchronously. Requests 598 * are typically completely constructed during nsIHttpRequestHandler.handle; 599 * however, responses which require significant resources (time, memory, 600 * processing) to construct can be created and sent incrementally by calling 601 * this method during the call to nsIHttpRequestHandler.handle. This method 602 * only has this effect when called during nsIHttpRequestHandler.handle; 603 * behavior is undefined if it is called at a later time. It may be called 604 * multiple times with no ill effect, so long as each call occurs before 605 * finish() is called. 606 * 607 * @throws NS_ERROR_UNEXPECTED 608 * if not initially called within a nsIHttpRequestHandler.handle call or if 609 * called after this response has been finished 610 * @throws NS_ERROR_NOT_AVAILABLE 611 * if seizePower() has been called on this 612 */ 613 void processAsync(); 614 615 /** 616 * Seizes complete control of this response (and its connection) from the 617 * server, allowing raw and unfettered access to data being sent in the HTTP 618 * response. Once this method has been called the only property which may be 619 * accessed without an exception being thrown is bodyOutputStream, and the 620 * only methods which may be accessed without an exception being thrown are 621 * write(), finish(), and seizePower() (which may be called multiple times 622 * without ill effect so long as all calls are otherwise allowed). 623 * 624 * After a successful call, all data subsequently written to the body of this 625 * response is written directly to the corresponding connection. (Previously- 626 * written data is silently discarded.) No status line or headers are sent 627 * before doing so; if the response handler wishes to write such data, it must 628 * do so manually. Data generation completes only when finish() is called; it 629 * is not enough to simply call close() on bodyOutputStream. 630 * 631 * @throws NS_ERROR_NOT_AVAILABLE 632 * if processAsync() has been called on this 633 * @throws NS_ERROR_UNEXPECTED 634 * if finish() has been called on this 635 */ 636 void seizePower(); 637 638 /** 639 * Signals that construction of this response is complete and that it may be 640 * sent over the network to the client, or if seizePower() has been called 641 * signals that all data has been written and that the underlying connection 642 * may be closed. This method may only be called after processAsync() or 643 * seizePower() has been called. This method is idempotent. 644 * 645 * @throws NS_ERROR_UNEXPECTED 646 * if processAsync() or seizePower() has not already been properly called 647 */ 648 void finish(); 649 };