Nathaniel Wesley Filardo dcc1ea2a49 A generic fifo and fifosock wrapper, under telnet and http server (#2650)
* lua_modules/fifo: a generic queue & socket wrapper

One occasionally wants a generic fifo, so here's a plausible
implementation that's reasonably flexible in its usage.

One possible consumer of this is a variant of TerryE's two-level fifo
trick currently in the telnetd example.  Factor that out to fifosock for
more general use.

* lua_examples/telnet: use factored out fifosock

* lua_modules/http: improve implementation

Switch to fifosock for in-order sending and waiting for everything to be
sent before closing.

Fix header callback by moving the invocation of the handler higher

* fifosock: optimistically cork and delay tx

If we just pushed a little bit of data into a fifosock that had idled,
wait a tick (1 ms) before transmitting.  Hopefully, this means that
we let the rest of the system push more data in before we send the first
packet.  But in a high-throughput situation, where we are streaming data
without idling the fifo, there won't be any additional delay and we'll
coalesce during operation as usual.

The fifosocktest mocks up enough of tmr for this to run, but assumes
an arbitrarily slow processor. ;)
2019-02-16 13:51:40 +01:00

2.1 KiB

HTTP Server Module

Since Origin / Contributor Maintainer Source
2015-01-19 Vladimir Dronnikov Vladimir Dronnikov http.lua

This Lua module provides a simple callback implementation of a HTTP 1.1 server.

Require

httpserver = require("httpserver")

Release

httpserver = nil
package.loaded["httpserver"] = nil

httpserver.createServer()

Function to start HTTP server.

Syntax

httpserver.createServer(port, handler(req, res))

Parameters

  • port: Port number for HTTP server. Most HTTP servers listen at port 80.
  • handler: callback function for when HTTP request was made.

Returns

net.server sub module.

Notes

Callback function has 2 arguments: req (request) and res (response). The first object holds values:

  • conn: net.socket sub module. DO NOT call :on or :send on this object.

  • method: Request method that was used (e.g.POST or GET)

  • url: Requested URL

  • onheader: value to setup handler function for HTTP headers like content-type. Handler function has 3 parameters:

    • self: req object
    • name: Header name
    • value: Header value
  • ondata: value to setup handler function HTTP data. Handler function has 2 parameters:

    • self: req object
    • chunk: Request data

The second object holds functions:

  • send(self, data, [response_code]): Function to send data to client. self is req object, data is data to send and response_code is HTTP response code like 200 or 404 (for example)
  • send_header(self, header_name, header_data): Function to send HTTP headers to client. self is req object, header_name is HTTP header name and header_data is HTTP header data for client.
  • finish([data]): Function to finalize connection, optionally sending data. data is optional data to send on connection finalizing.

Full example can be found in http-example.lua