README 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. httpd.js README
  2. ===============
  3. httpd.js is a small cross-platform implementation of an HTTP/1.1 server in
  4. JavaScript for the Mozilla platform.
  5. httpd.js may be used as an XPCOM component, as an inline script in a document
  6. with XPCOM privileges, or from the XPCOM shell (xpcshell). Currently, its most-
  7. supported method of use is from the XPCOM shell, where you can get all the
  8. dynamicity of JS in adding request handlers and the like, but component-based
  9. equivalent functionality is planned.
  10. Using httpd.js as an XPCOM Component
  11. ------------------------------------
  12. First, create an XPT file for nsIHttpServer.idl, using the xpidl tool included
  13. in the Mozilla SDK for the environment in which you wish to run httpd.js. See
  14. <http://developer.mozilla.org/en/docs/XPIDL:xpidl> for further details on how to
  15. do this.
  16. Next, register httpd.js and nsIHttpServer.xpt in your Mozilla application. In
  17. Firefox, these simply need to be added to the /components directory of your XPI.
  18. Other applications may require use of regxpcom or other techniques; consult the
  19. applicable documentation for further details.
  20. Finally, create an instance of the server using the following command:
  21. var server = Components.classes["@mozilla.org/server/jshttp;1"]
  22. .createInstance(Components.interfaces.nsIHttpServer);
  23. At this point you'll want to initialize the server, since by default it doesn't
  24. serve many useful paths. For more information on this, see the IDL docs for the
  25. nsIHttpServer interface in nsIHttpServer.idl, particularly for
  26. registerDirectory (useful for mapping the contents of directories onto request
  27. paths), registerPathHandler (for setting a custom handler for a specific path on
  28. the server, such as CGI functionality), and registerFile (for mapping a file to
  29. a specific path).
  30. Finally, you'll want to start (and later stop) the server. Here's some example
  31. code which does this:
  32. server.start(8080); // port on which server will operate
  33. // ...server now runs and serves requests...
  34. server.stop();
  35. This server will only respond to requests on 127.0.0.1:8080 or localhost:8080.
  36. If you want it to respond to requests at different hosts (say via a proxy
  37. mechanism), you must use server.identity.add() or server.identity.setPrimary()
  38. to add it.
  39. Using httpd.js as an Inline Script or from xpcshell
  40. ---------------------------------------------------
  41. Using httpd.js as a script or from xpcshell isn't very different from using it
  42. as a component; the only real difference lies in how you create an instance of
  43. the server. To create an instance, do the following:
  44. var server = new nsHttpServer();
  45. You now can use |server| exactly as you would when |server| was created as an
  46. XPCOM component. Note, however, that doing so will trample over the global
  47. namespace, and global values defined in httpd.js will leak into your script.
  48. This may typically be benign, but since some of the global values defined are
  49. constants (specifically, Cc/Ci/Cr as abbreviations for the classes, interfaces,
  50. and results properties of Components), it's possible this trampling could
  51. break your script. In general you should use httpd.js as an XPCOM component
  52. whenever possible.
  53. Known Issues
  54. ------------
  55. httpd.js makes no effort to time out requests, beyond any the socket itself
  56. might or might not provide. I don't believe it provides any by default, but
  57. I haven't verified this.
  58. Every incoming request is processed by the corresponding request handler
  59. synchronously. In other words, once the first CRLFCRLF of a request is
  60. received, the entire response is created before any new incoming requests can be
  61. served. I anticipate adding asynchronous handler functionality in bug 396226,
  62. but it may be some time before that happens.
  63. There is no way to access the body of an incoming request. This problem is
  64. merely a symptom of the previous one, and they will probably both be addressed
  65. at the same time.
  66. Other Goodies
  67. -------------
  68. A special testing function, |server|, is provided for use in xpcshell for quick
  69. testing of the server; see the source code for details on its use. You don't
  70. want to use this in a script, however, because doing so will block until the
  71. server is shut down. It's also a good example of how to use the basic
  72. functionality of httpd.js, if you need one.
  73. Have fun!