Skip to content
This repository
tag: release_0_0_1
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 317 lines (180 sloc) 6.269 kb

elnode

An attempt to make an emacs lisp clone of nodejs.

Apparently Steve Yegge has already done this... but I couldn't find it so I'm writing it.

Why elnode??

It's like "hell, no". It's... well.... it's an, um, joke.

Installation

Unfortunately, elnode isn't packaged yet. I will try and get to that.

Until then you can install it by copying the elnode.el file to your machine, somewhere on your load-path and then:

(load-library "elnode")

When elnode initializes it automatically starts a webserver.

If you:

M-x customize-group
elnode

you can alter a number of variables pertaining to the default configuration.

You can also just ignore it and write your own servers.

How does it work?

You can define a handler function:

(defun nicferrier-handler (httpcon)
  "Demonstration function"
  (elnode-http-start httpcon "200" '("Content-type" . "text/html"))
  (elnode-http-return httpcon "<html><b>HELLO!</b></html>")
  )

And then start the server:

(elnode-start 'nicferrier-handler 8010 "localhost")

You can also start the server interactively... you still have to pass it a handler function and a port.

Stopping the server

If you can remember the port you started your server on then you'll be able to stop it, like:

(elnode-stop 8010)

API

elnode-child-process

Run the specified process asynchronously and send it's output to the http connection.

program is the program to run. args is a list of arguments to pass to the program.

It is NOT POSSIBLE to run more than one process at a time directed at the same http connection.

elnode-dispatcher

Dispatch the HTTPCON to the correct function based on the URL-MAPPING-TABLE.

URL-MAPPING-TABLE is an alist of:

 (url-regex . function-to-dispatch)

To map the root url you should use:

  $

'elnode-dispatcher' uses 'elnode-normalize-path' to ensure paths end in / so to map another url you should use:

  path/$

or:

  path/subpath/$

elnode-error

How errors are logged.

This function is available for handlers to call. It is also used by elnode iteslf.

There is only one error log, in the future there may be more.

elnode-http-header

Get the header specified by name from the header

elnode-http-method

Get the PATHINFO of the request

elnode-http-params

Get an alist of the parameters in the request.

If the method is a GET then the parameters are from the url. If the method is a POST then the parameters may come from either the url or the POST body or both:

 POST /path?a=b&x=y
 a=c

would result in:

 '(('a' 'b' 'c')('x' . 'y'))

elnode-http-pathinfo

Get the PATHINFO of the request

elnode-http-query

Get the QUERY of the request

elnode-http-return

End the http response on the specified http connection

httpcon is the http connection. data must be a string right now.

elnode-http-send-string

Send the string to the HTTP connection.

This is really only a placeholder function for doing transfer-encoding.

elnode-http-start

Start the http response on the specified http connection.

httpcon is the HTTP connection being handled. status is the HTTP status, eg: 200 or 404 header is a sequence of (header-name . value) pairs.

For example:

 (elnode-http-start httpcon "200" '("Content-type" . "text/html"))

elnode-http-version

Get the PATHINFO of the request

elnode-list-buffers

List the current buffers being managed by elnode

elnode-normalize-path

A decorator for 'handler' that normalizes paths to have a trailing slash.

This checks the path for a trailing slash and sends a 302 to the slash trailed url if there is none.

Otherwise it calls 'handler'

elnode-send-400

A generic 400 handler

elnode-send-404

A generic 404 handler

elnode-send-redirect

Sends a redirect to the specified location

elnode-start

Start the elnode server.

Most of the work done by the server is actually done by functions, the sentinel function, the log function and a filter function.

request-handler is a function which is called with the request. The function is called with one argument, the http-connection.

You can use functions such as elnode-http-start and elnode-http-send-body to send the http response.

Example:

 (defun nic-server (httpcon)
   (elnode-http-start 200 '(("Content-Type": "text/html")))
   (elnode-http-return "<html><b>BIG!</b></html>")
   )
 (elnode-start 'nic-server 8000)
 ;; End

You must also specify the port to start the server on.

You can optionally specify the hostname to start the server on, this must be bound to a local IP. Some names are special:

  localhost  means 127.0.0.1
  * means 0.0.0.0

specifying an IP is also possible.

Note that although host can be specified, elnode does not disambiguate on running servers by host. So you cannot start 2 different elnode servers on the same port on different hosts.

elnode-stop

Stop the elnode server

elnode-test-path

Check that the path requested is above the docroot specified.

Call 404-handler (or default 404 handler) on failure and handler on success.

handler is called: httpcon docroot targetfile

This is used by 'elnode--webserver-handler-proc' in the webservers that it creates... but it's also meant to be generally useful for other handler writers.

elnode-webserver-handler-maker

Make a webserver handler possibly with the specific 'docroot' and 'extra-mime-types'.

Returns a proc which is the handler. The handler serves files out of the docroot and marks them with the content types that Emacs knows about. You can add extra content types for the webserver just by supplying an alist of mime-types and extensions for 'extra-mime-types'.

The webserver handler also creates file indexes.

The webserver uses 'elnode-test-path' to make sure that the request does not go above the 'docroot'.

But...

There's always a but.

Here's a list of buts:

  • the HTTP parsing isn't great, it uses too many regexs
  • we don't parse any data sent through POST other than form-data
  • the error handling is absolute rubbish
Something went wrong with that request. Please try again.