2018-12-19 12:23:14 +01:00
# HTTP Server Module
2018-12-16 21:39:43 +01:00
| Since | Origin / Contributor | Maintainer | Source |
| :----- | :-------------------- | :---------- | :------ |
2019-01-13 22:01:57 +01:00
| 2015-01-19 | [Vladimir Dronnikov ](https://github.com/dvv ) | [Vladimir Dronnikov ](https://github.com/dvv ) | [http.lua ](../../lua_modules/http/httpserver.lua ) |
2018-12-16 21:39:43 +01:00
This Lua module provides a simple callback implementation of a [HTTP 1.1 ](https://www.w3.org/Protocols/rfc2616/rfc2616.html ) server.
### Require
```lua
2018-12-19 12:23:14 +01:00
httpserver = require("httpserver")
2018-12-16 21:39:43 +01:00
```
### Release
```lua
httpserver = nil
2018-12-19 12:23:14 +01:00
package.loaded["httpserver"] = nil
2018-12-16 21:39:43 +01:00
```
## httpserver.createServer()
2018-12-19 12:23:14 +01:00
Function to start HTTP server.
2018-12-16 21:39:43 +01:00
#### Syntax
2019-02-17 19:26:29 +01:00
`httpserver.createServer(port, handler(req, res))`
2018-12-16 21:39:43 +01:00
#### 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
2018-12-19 12:23:14 +01:00
Callback function has 2 arguments: `req` (request) and `res` (response). The first object holds values:
2018-12-16 21:39:43 +01:00
2019-02-16 13:51:40 +01:00
- `conn` : `net.socket` sub module. **DO NOT** call `:on` or `:send` on this
object.
2018-12-19 12:23:14 +01:00
- `method` : Request method that was used (e.g.`POST` or `GET` )
- `url` : Requested URL
2019-11-26 12:12:45 +01:00
- `onheader` : assign a function to this value which will be called as soon as HTTP headers like `content-type` are available.
This handler function has 3 parameters:
2018-12-19 12:23:14 +01:00
- `self` : `req` object
2019-11-26 12:12:45 +01:00
- `name` : Header name. Will allways be lowercase.
2019-02-17 19:26:29 +01:00
- `value` : Header value
2019-11-26 12:12:45 +01:00
- `ondata` : assign a function to this value which will be called as soon as body data is available.
This handler function has 2 parameters:
2018-12-19 12:23:14 +01:00
- `self` : `req` object
2019-11-26 12:12:45 +01:00
- `chunk` : Request data. If all data is received there will be one last call with data = nil
2018-12-16 21:39:43 +01:00
The second object holds functions:
2018-12-19 12:23:14 +01:00
2019-11-26 12:12:45 +01:00
- `send(self, data, [response_code])` : Function to send data to client.
2020-11-04 09:34:05 +01:00
- `self` : `res` object
- `data` : data to send (may be nil)
- `response_code` : the HTTP response code like `200` (default) or `404` (for example) *NOTE* if there are several calls with response_code given only the first one will be used. Any further codes given will be ignored.
2019-11-26 12:12:45 +01:00
- `send_header(self, header_name, header_data)` : Function to send HTTP headers to client. This function will not be available after data has been sent. (It will be nil.)
2020-11-04 09:34:05 +01:00
- `self` : `res` object
- `header_name` : the HTTP header name
- `header_data` : the HTTP header data
2019-11-26 12:12:45 +01:00
- `finish([data[, response_code]])` : Function to finalize connection, optionally sending data and return code.
2020-11-04 09:34:05 +01:00
- `data` : optional data to send on connection finalizing
- `response_code` : the HTTP response code like `200` (default) or `404` (for example) *NOTE* if there are several calls with response_code given only the first one will be used. Any further codes given will be ignored.
2018-12-16 21:39:43 +01:00
2019-01-13 22:01:57 +01:00
Full example can be found in [http-example.lua ](../../lua_modules/http/http-example.lua )