Điểm qua nodejs/http-parser: 6.4k sao trên GitHub, viết chủ yếu bằng C, thuộc nhóm Backend. http request/response parser for c
Tóm tắt dựng từ metadata GitHub của chính dự án — chưa có bài review TopGit. Trang sẽ tự động cập nhật khi bài review đầy đủ được xuất bản.
VÌ SAO CHƯA CÓ REVIEW
TopGit viết bài đầy đủ cho repo có nhiều sao nhất và được yêu cầu nhiều nhất. Trang này là snapshot trong thời gian chờ — xem README gốc ở tab READ ME.
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:
No dependencies
Handles persistent streams (keep-alive).
Decodes chunked encoding.
Upgrade support
Defends against buffer overflow attacks.
The parser extracts the following information from HTTP messages:
Header fields and values
Content-Length
Request method
Response status code
Transfer-Encoding
HTTP version
Request URL
Message body
Usage
One http_parser object is used per TCP connection. Initialize the struct
using http_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.
size_t len = 80*1024, nparsed;
char buf[len];
ssize_t recved;
recved = recv(fd, buf, len, 0);
if (recved < 0) {
/* Handle error. */
}
/* Start up / continue the parser.
* Note we pass recved==0 to signal that EOF has been received.
*/
nparsed = http_parser_execute(parser, &settings, buf, recved);
if (parser->upgrade) {
/* handle new protocol */
} else if (nparsed != recved) {
/* Handle error. Usually just close the connection. */
}
http_parser needs 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 tell http_parser about EOF, give
0 as the fourth parameter to http_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 in http_parser and gets reset on each new message. If
this information is needed later, copy it out of the structure during the
headers_complete callback.
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_parser supports upgrading the connection to a different protocol. An
increasingly common example of this is the WebSocket protocol which sends
a request like
(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->upgrade has been set to 1 after
http_parser_execute() returns. Non-HTTP data begins at the buffer supplied
offset by the return value of http_parser_execute().
Callbacks
During the http_parser_execute() call, the callbacks set in
http_parser_settings will 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:
notification typedef int (*http_cb) (http_parser*);
Callbacks: on_message_begin, on_headers_complete, on_message_complete.
data typedef int (*http_data_cb) (http_parser*, const char *at, size_t length);
Callbacks: (requests only) on_url,
(common) on_header_field, on_header_value, on_body;
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_parser object's data field 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 allows http_parser to be used in
multi-threaded contexts.
Example:
typedef struct {
socket_t sock;
void* buffer;
int buf_len;
} custom_data_t;
int my_url_callback(http_parser* parser, const char *at, size_t length) {
/* access to thread local custom_data_t struct.
Use this access save parsed data for later use into thread local
buffer, or communicate over socket
*/
parser->data;
...
return 0;
}
...
void http_parser_thread(socket_t sock) {
int nparsed = 0;
/* allocate memory for user data */
custom_data_t *my_data = malloc(sizeof(custom_data_t));
/* some information for use by callbacks.
* achieves thread -> callback information flow */
my_data->sock = sock;
/* instantiate a thread-local parser */
http_parser *parser = malloc(sizeof(http_parser));
http_parser_init(parser, HTTP_REQUEST); /* initialise parser */
/* this custom data reference is accessible through the reference to the
parser supplied to callback functions */
parser->data = my_data;
http_parser_settings settings; /* set up callbacks */
settings.on_url = my_url_callback;
/* execute parser */
nparsed = http_parser_execute(parser, &settings, buf, recved);
...
/* parsed information copied from callback.
can now perform action on data copied into thread-local memory from callbacks.
achieves callback -> thread information flow */
my_data->buffer;
...
}
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_parser guarantees that data pointer is only
valid for the lifetime of callback. You can also read() 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:
(on_header_field and on_header_value shortened to on_h_*)
------------------------ ------------ --------------------------------------------
| State (prev. callback) | Callback | Description/action |
------------------------ ------------ --------------------------------------------
| nothing (first call) | on_h_field | Allocate new buffer and copy callback data |
| | | into it |
------------------------ ------------ --------------------------------------------
| value | on_h_field | New header started. |
| | | Copy current name,value buffers to headers |
| | | list and allocate new buffer for new name |
------------------------ ------------ --------------------------------------------
| field | on_h_field | Previous name continues. Reallocate name |
| | | buffer and append callback data to it |
------------------------ ------------ --------------------------------------------
| field | on_h_value | Value for current header started. Allocate |
| | | new buffer and copy callback data to it |
------------------------ ------------ --------------------------------------------
| value | on_h_value | Value continues. Reallocate value buffer |
| | | and append callback data to it |
------------------------ ------------ --------------------------------------------
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
consecutive on_url callbacks.
nodejs/http-parser có 6.4k sao GitHub — tải lại trang để xem số mới nhất, hoặc xem trực tiếp github.com/nodejs/http-parser. TopGit phản chiếu số sao của GitHub nhưng không cam kết đến từng phút.
nodejs/http-parser có những chủ đề gì?
GitHub topics của nodejs/http-parser: "node", "nodejs". TopGit xếp repo vào nhóm Backend.
nodejs/http-parser còn đang phát triển không?
Commit gần nhất trên nodejs/http-parser là 4.4 năm trước (theo timestamp GitHub). Repo có 1.5k fork — một chỉ báo về mức độ quan tâm của cộng đồng.
nodejs/http-parser là gì?
nodejs/http-parser (nodejs/http-parser) là dự án C trên GitHub. Theo mô tả gốc: http request/response parser for c
nodejs/http-parser so với các dự án Backend khác thế nào?
nodejs/http-parser được TopGit xếp vào nhóm Backend, với 6.4k sao GitHub và viết bằng C. Xem trang chủ đề Backend trên TopGit để so sánh với các dự án tương tự theo số sao và mức độ hoạt động.
nodejs/http-parser viết bằng ngôn ngữ gì?
nodejs/http-parser chủ yếu viết bằng C. Trường "language" của GitHub dựa trên phần lớn byte ở nhánh mặc định.
Vì sao nodejs/http-parser được xếp vào nhóm Backend?
TopGit xếp nodejs/http-parser vào nhóm Backend dựa trên GitHub topics và mô tả của repo (gắn thẻ: "node", "nodejs"). Việc phân loại dựa trên metadata thật của repo, không phải đoán theo cảm tính biên tập.
Đọc đầy đủ README ở tab phía trên.
Vẫn đang phân vân về http-parser?
Một cú bấm sẽ gửi câu hỏi kèm trang này cho AI — xem AI nói gì về http-parser.