Next: , Previous: , Up: Top   [Contents][Index]


2 Handlers

The function ws-start takes takes two arguments handlers and port. It starts a server listening on port responding to requests with handlers. Handlers may be either a single function or an association list composed of pairs of matchers and handler functions. When handlers is a single function the given function is used to serve every request, when it is an association list, the function of the first matcher to match each request handles that request.

2.1 Matchers

Matchers may be a regular expression or a function. Regular expression matchers consists of an HTTP header and a regular expression. When the regular expression matches the content of the given header the matcher succeeds and the associated handler is called. For example the following matches any GET request whose path starts with the substring “foo”.

(:GET . "^foo")

A function matcher is a function which takes the request object (see Requests) and succeeds when the function returns a non-nil value. For example the following matcher matches every request,

(lambda (_) t)

and the following matches only requests in which the supplied “number” parameter is odd.

(lambda (request)
  (oddp (string-to-number (cdr (assoc "number" request)))))

2.2 Handler Function

Each handler is a function which takes a request object (see Requests) as its only argument. The function may respond to the request by writing to the network process held in the process field of the request object. For example, the process-send-string function may be used to write string data to a request as in the following.

  (process-send-string (process request) "hello world")

When the handler function exits the connection is terminated unless the handler function returns the keyword :keep-alive.


Next: , Previous: , Up: Top   [Contents][Index]