~mna/tulip

The tulip.handler module contains functions and tables related to implementation of web and worker handlers.

#API

Assuming local handler = require 'tulip.handler'.

#handler.DEFAULTMIME

Default MIME type when unknown.

#handler.EXTMIME

List mapping well-known file extensions to MIME types.

#handler.HTTPSTATUS

List mapping common HTTP status codes to standard status text.

#handler.chain_middleware(mws, req, res[, last[, i]])

Starts a call to a chain of middleware, where mws is an array of middleware functions. This calls the middleware at index i with a next() function generated to call the following middleware, ending with a call to last() if it is non-nil. If is is not provided, it is set to 1.

#handler.chain_wmiddleware(mws, msg[, last[, i]])

Starts a call to a chain of wmiddleware, where mws is an array of wmiddleware functions. This calls the wmiddleware at index i with a next() function generated to call the following wmiddleware, ending with a call to last() if it is non-nil. If i is not provided, it is set to 1.

#f = handler.dir(path)

Returns a handler that serves a directory based on the first Request.pathargs argument, then calls the next middleware. Sets the content-type based on some well-known file extensions.

#f = handler.errhandler(t)

Returns an HTTP error handler that accepts req, res, nxt, err and dispatches handling to the function indicated by t, where t is a table where keys are error codes (e.g. 'EINVALID') and values are either error handlers (accepting req, res, nxt, err) or a number that indicates the HTTP status code to return (the body will be set to the default text of that status code).

If the error does not correspond to any of the defined error codes, the handler at array position 1 is called (or if it's a number, it is used as status code), or if it is not set, the error is thrown.

#f = handler.recover(f)

Returns a handler that recovers from an error raised in subsequent middleware, and calls f with req, res and the error. Note that there is no next function argument in the arguments to f.

#handler.set_cookie(res, cfg)

Writes a cookie to the response. The cfg table may have the following fields:

  • name: string = name of the cookie
  • value: string = value of the cookie, set to '' if missing
  • ttl: number = time-to-live, do not set for a browser-session cookie, set to negative to delete the cookie.
  • domain: string = domain of the cookie
  • path: string = path of the cookie
  • insecure: boolean = allow sending cookie on http (defaults to false, which is Secure)
  • allowjs: boolean = if true, cookie can be accessed via javascript (defaults to false, which is HttpOnly).
  • same_site: string = set same-site constraint, can be 'strict', 'lax' or 'none'. Defaults to the browser's default behaviour (lax).
#f = handler.wrecover(f)

Returns a worker handler that recovers from an error raised in subsequent wmiddleware, and calls f with msg and the error. Note that there is no next function argument in the arguments to f.

#f = handler.write(t)

Returns a handler that just calls Response:write with t and calls the next middleware.

Back to index

About this wiki

commit 3ebfbd288b8e5c95fdf8ce2027a0e94cfa1c8976
Author: Martin Angers <martin.n.angers@gmail.com>
Date:   2021-02-25T14:07:12-05:00

Update to reflect Request:validate_body
Clone this wiki
https://git.sr.ht/~mna/tulip-wiki (read-only)
git@git.sr.ht:~mna/tulip-wiki (read/write)