This is http-parser
binding for lua.
git clone
make
In lua, please use local lhp = require('lhttpparser')
.
In c application, lhttp_parser can be compiled and linked statically. Just call
the function luaopen_lhttp_parser(L)
to create a table with the http parser
functions and leave it on the top of the stack.
To test the library run, please run ./test.lua
,
lhttp-parser
has tow method:
parseUrl
to parse a url, and return a parsed table, which haveschema
,host
,port
,port_string
,path
,query
andfragment
.new
to create a request or response parser.
To create a http parser you need make a table contains any fields of http_parser_settings
C | Lua |
---|---|
on_message_begin | onMessageBegin |
on_url | onUrl |
on_status | onStatus |
on_header_field | onHeaderField |
on_header_value | onHeaderValue |
on_headers_complete | onHeadersComplete |
on_body | onBody |
on_message_complete | onMessageComplete |
on_chunk_header | onChunkHeader |
on_chunk_complete | onChunkComplete |
To create a request parser.
parser = lhp.new('request', {
onUrl = function(url) ... end
onHeaderField = function(hkey) ... end
onHeaderValue = function(hval) ... end
onBody = function(body) ... end
onMessageBegin = function() ... end
onMessageComplete = function() ... end
onHeadersComplete = function() ... end
onChunkHeader = function(content_length) ... end
onChunkComplete = function() ... end
})
To create a response parser.
parser = lhp.new('request', {
onStatus = function(code, text) ... end
onHeaderField = function(hkey) ... end
onHeaderValue = function(hval) ... end
onBody = function(body) ... end
onMessageBegin = function() ... end
onMessageComplete = function() ... end
onHeadersComplete = function() ... end
onChunkHeader = function(content_length) ... end
onChunkComplete = function() ... end
})
Create a new HTTP parser to handle either an HTTP request or HTTP response respectively. Pass in a table of callbacks that are ran when the parser encounters the various fields. All callbacks will be ran with the full value for that field with onBody being the notible exception.
Feed the parser some partial input, that starts at i and continues until j; i and j can be negative. If j is absent, then it is assumed to be equal to -1 (which is the same as the string length), same with string.sub(...)
. The first character is at position 1 (not at 0, as in C). Indices are allowed to be negative and are interpreted as indexing backwards, from the end of the string. Thus, the last character is at position -1, and so on.
Returns how many bytes where read. A short read may happen if a request is being "upgraded" or was an invalid format. See parser:is_upgrade() below to differentiate between these two events (if you want to support an upgraded protocol).
Tell the parser end of input.
Returns HTTP version as two numbers (major, minor), only valid on requests.
Returns the HTTP status code of a response. This is only valid on responses.
Return name of method which maybe DELETE
, GET
, HEAD
, POST
, PUT
,
CONNECT
, OPTIONS
, TRACE
, COPY
, LOCK
, MKCOL
, MOVE
, PROPFIND
,
PROPPATCH
, UNLOCK
, REPORT
, MKACTIVITY
, CHECKOUT
, MERGE
, MSEARCH
,
NOTIFY
, SUBSCRIBE
, UNSUBSCRIBE
or UNKNOWN_METHOD
, only valid on
requests.
Returns true if this TCP connection should be "kept alive" so that it can be re-used for a future request/response. If this returns false and you are the server, then respond with the "Connection: close" header. If you are the client, then simply close the connection.
Returns true if the input request wants to "upgrade" to a different protocol.
Returns errno(number), error name(string), error description(string).
Re-initialize HTTP parser clearing any previous error/state.