doc: add maintenance notice to readme
I’m moving on and as the last (semi-)active maintainer, that means http-parser is now effectively unmaintained.
Refs: https://github.com/nodejs/http-parser/issues/522 Reviewed-By: Ben Noordhuis info@bnoordhuis.nl
版权所有:中国计算机学会技术支持:开源发展技术委员会
京ICP备13000930号-9
京公网安备 11010802032778号
HTTP Parser
http-parser is not actively maintained. New projects and projects looking to migrate should consider llhttp.
This is a parser for HTTP messages written in C. It parses both requests and responses. The parser is designed to be used in performance HTTP applications. It does not make any syscalls nor allocations, it does not buffer data, it can be interrupted at anytime. Depending on your architecture, it only requires about 40 bytes of data per message stream (in a web server that is per connection).
Features:
The parser extracts the following information from HTTP messages:
Usage
One
http_parserobject is used per TCP connection. Initialize the struct usinghttp_parser_init()and set the callbacks. That might look something like this for a request parser:When data is received on the socket execute the parser and check for errors.
http_parserneeds to know where the end of the stream is. For example, sometimes servers send responses without Content-Length and expect the client to consume input (for the body) until EOF. To tellhttp_parserabout EOF, give0as the fourth parameter tohttp_parser_execute(). Callbacks and errors can still be encountered during an EOF, so one must still be prepared to receive them.Scalar valued message information such as
status_code,method, and the HTTP version are stored in the parser structure. This data is only temporally stored inhttp_parserand gets reset on each new message. If this information is needed later, copy it out of the structure during theheaders_completecallback.The parser decodes the transfer-encoding for both requests and responses transparently. That is, a chunked encoding is decoded before being sent to the on_body callback.
The Special Problem of Upgrade
http_parsersupports upgrading the connection to a different protocol. An increasingly common example of this is the WebSocket protocol which sends a request likefollowed by non-HTTP data.
(See RFC6455 for more information the WebSocket protocol.)
To support this, the parser will treat this as a normal HTTP message without a body, issuing both on_headers_complete and on_message_complete callbacks. However http_parser_execute() will stop parsing at the end of the headers and return.
The user is expected to check if
parser->upgradehas been set to 1 afterhttp_parser_execute()returns. Non-HTTP data begins at the buffer supplied offset by the return value ofhttp_parser_execute().Callbacks
During the
http_parser_execute()call, the callbacks set inhttp_parser_settingswill be executed. The parser maintains state and never looks behind, so buffering the data is not necessary. If you need to save certain data for later usage, you can do that from the callbacks.There are two types of callbacks:
typedef int (*http_cb) (http_parser*);Callbacks: on_message_begin, on_headers_complete, on_message_complete.typedef int (*http_data_cb) (http_parser*, const char *at, size_t length);Callbacks: (requests only) on_url,Callbacks must return 0 on success. Returning a non-zero value indicates error to the parser, making it exit immediately.
For cases where it is necessary to pass local information to/from a callback, the
http_parserobject’sdatafield can be used. An example of such a case is when using threads to handle a socket connection, parse a request, and then give a response over that socket. By instantiation of a thread-local struct containing relevant data (e.g. accepted socket, allocated memory for callbacks to write into, etc), a parser’s callbacks are able to communicate data between the scope of the thread and the scope of the callback in a threadsafe manner. This allowshttp_parserto be used in multi-threaded contexts.Example:
In case you parse HTTP message in chunks (i.e.
read()request line from socket, parse, read half headers, parse, etc) your data callbacks may be called more than once.http_parserguarantees that data pointer is only valid for the lifetime of callback. You can alsoread()into a heap allocated buffer to avoid copying memory around if this fits your application.Reading headers may be a tricky task if you read/parse headers partially. Basically, you need to remember whether last header callback was field or value and apply the following logic:
Parsing URLs
A simplistic zero-copy URL parser is provided as
http_parser_parse_url(). Users of this library may wish to use it to parse URLs constructed from consecutiveon_urlcallbacks.See examples of reading in headers: