{"id":227998,"date":"2021-10-17T08:50:02","date_gmt":"2021-10-17T00:50:02","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=227998"},"modified":"2021-10-07T14:51:23","modified_gmt":"2021-10-07T06:51:23","slug":"node-js-http-llhttp","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/node-js-http-llhttp.html","title":{"rendered":"HTTP\u89e3\u6790\u5668llhttp\u7684\u4f7f\u7528\u6307\u5357"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\nllhttp \u662f Node.js \u7684 HTTP 1.1 \u89e3\u6790\u5668\uff0c\u7528\u4e8e\u66ff\u4ee3\u65e9\u671f\u7684http_parser\uff0c\u6027\u80fd\u4e0a\u6709\u4e86\u975e\u5e38\u5927\u7684\u63d0\u5347\uff0c\u6700\u8fd1\u6253\u7b97\u5728 No.js \u91cc\u5f15\u5165 llhttp \u6765\u5904\u7406 HTTP \u534f\u8bae\u7684\u89e3\u6790\uff0c\u672c\u6587\u7b80\u5355\u4ecb\u7ecd\u4e00\u4e0b\u5982\u4f55\u4f7f\u7528\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"
\nllhttp \u662f Node.js \u7684 HTTP 1.1 \u89e3\u6790\u5668\uff0c\u7528\u4e8e\u66ff\u4ee3\u65e9\u671f\u7684http_parser\uff0c\u6027\u80fd\u4e0a\u6709\u4e86\u975e\u5e38\u5927\u7684\u63d0\u5347\uff0c\u6700\u8fd1\u6253\u7b97\u5728 No.js \u91cc\u5f15\u5165 llhttp \u6765\u5904\u7406 HTTP \u534f\u8bae\u7684\u89e3\u6790\uff0c\u672c\u6587\u7b80\u5355\u4ecb\u7ecd\u4e00\u4e0b\u5982\u4f55\u4f7f\u7528\u3002<\/p>\n

llhttp \u9879\u76ee\u662f Node.js \u4e2d\u7684\u5b50\u9879\u76ee\uff0c\u5730\u5740\u5728\uff1a<\/p>\n

https:\/\/github.com\/nodejs\/llhttp\u3002<\/p>\n

\u4f7f\u7528\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n

1. \u5b89\u88c5 npx\uff1anpm i npx -g<\/p>\n

2. \u6267\u884c ts \u751f\u6210 c \u4ee3\u7801\uff1anpx ts-node bin\/generate.ts\uff0c\u6216\u8005\u6267\u884c make generate<\/p>\n

3. \u8fd9\u65f6\u5019build \u76ee\u5f55\u4e0b\u751f\u6210\u4e86 llhttp.h \u548c llhttp.c\uff0c\u518d\u52a0\u4e0a native \u4e0b\u7684 c \u4ee3\u7801\uff0c\u5c31\u662f llhttp \u7684\u5168\u90e8\u4ee3\u7801\uff0c\u6211\u4eec\u53ef\u4ee5\u628a\u4ed6\u590d\u5236\u5230\u81ea\u5df1\u7684\u9879\u76ee\u4e2d\u4f7f\u7528<\/p>\n

\u4e0b\u9762\u770b\u770b\u5982\u4f55\u4f7f\u7528\u3002llhttp \u4f7f\u7528\u56de\u8c03\u94a9\u5b50\u7684\u8bbe\u8ba1\u601d\u60f3\uff0c\u521d\u59cb\u5316\u89e3\u6790\u5668\u7684\u65f6\u5019\uff0c\u6211\u4eec\u53ef\u4ee5\u8bbe\u7f6e\u89e3\u6790\u7c7b\u578b\uff0c\u662f\u8bf7\u6c42\u6216\u54cd\u5e94\u62a5\u6587\uff0c\u7136\u540e\u8bbe\u7f6e\u89e3\u6790\u72b6\u6001\u7684\u56de\u8c03\uff0c\u6bd4\u5982\u89e3\u6790\u9053 URL \u65f6\u56de\u8c03\uff0c\u89e3\u6790\u5230 header \u65f6\u56de\u8c03\u3002\u63a5\u7740\u4f20\u5165\u62a5\u6587\u6267\u884c llhttp_execute \u5c31\u53ef\u4ee5\uff0c\u4e0b\u9762\u662f\u89e3\u6790\u8bf7\u6c42\u62a5\u6587\u7684\u4f8b\u5b50\u3002<\/p>\n

\r\n#include\r\n#include\r\n#include \"llhttp.h\"\r\n#define MAX_LEN 2048\r\nint on_message_begin(llhttp_t* parser){\r\nprintf(\"parse start\\n\");\r\nreturn 0;\r\n}\r\n\r\nint on_url(llhttp_t* parser, const char* at, size_t length){\r\nchar url[MAX_LEN];\r\nstrncpy(url, at, length);\r\nurl[length] = '\\0';\r\nprintf(\"on_url: %s\\n\", url);\r\nreturn 0;\r\n}\r\n\r\nint on_header_field(llhttp_t* parser, const char* at, size_t length){\r\nchar header_field[MAX_LEN];\r\nstrncpy(header_field, at, length);\r\nheader_field[length] = '\\0';\r\nprintf(\"head field: %s\\n\", header_field);\r\nreturn 0;\r\n}\r\n\r\nint on_header_value(llhttp_t* parser, const char* at, size_t length){\r\nchar header_value[MAX_LEN];\r\nstrncpy(header_value, at, length);\r\nheader_value[length] = '\\0';\r\nprintf(\"head value: %s\\n\", header_value);\r\nreturn 0;\r\n}\r\n\r\nint on_headers_complete(llhttp_t* parser){\r\nprintf(\"on_headers_complete, major: %d, major: %d, keep-alive: %d, upgrade: %d\\n\", parser->http_major, parser->http_minor, llhttp_should_keep_alive(parser), parser->upgrade);\r\nreturn 0;\r\n}\r\n\r\nint on_body(llhttp_t* parser, const char* at, size_t length){\r\nchar body[MAX_LEN];\r\nstrncpy(body, at, length);\r\nbody[length] = '\\0';\r\nprintf(\"on_body: %s\\n\", body);\r\nreturn 0;\r\n}\r\n\r\nint on_message_complete(llhttp_t* parser){\r\nprintf(\"on_message_complete\\n\");\r\nreturn 0;\r\n}\r\n\r\nint main(){\r\nllhttp_t parser;\r\nllhttp_settings_t settings;\r\nllhttp_settings_init(&settings);\r\nllhttp_init(&parser, HTTP_REQUEST, &settings);\r\n\r\nsettings.on_message_begin = on_message_begin;\r\nsettings.on_url = on_url;\r\nsettings.on_header_field = on_header_field;\r\nsettings.on_header_value = on_header_value;\r\nsettings.on_headers_complete = on_headers_complete;\r\nsettings.on_body = on_body;\r\nsettings.on_message_complete = on_message_complete;\r\n\r\nconst char* request = \"POST \/index.html HTTP\/1.1\\r\\nconnection:close\\r\\ncontent-length: 1\\r\\n\\r\\n1\\r\\n\\r\\n\";\r\nint request_len = strlen(request);\r\n\r\nenum llhttp_errno err = llhttp_execute(&parser, request, request_len);\r\n\r\nif (err != HPE_OK) {\r\nfprintf(stderr, \"Parse error: %s %s\\n\", llhttp_errno_name(err),\r\nparser.reason);\r\n}\r\n\r\nreturn 0;\r\n}\r\n<\/pre>\n

\u63a5\u7740\u770b\u89e3\u6790\u54cd\u5e94\u7684\u4f8b\u5b50\u3002<\/p>\n

\r\n#include\r\n#include\r\n#include \"llhttp.h\"\r\n#define MAX_LEN 2048\r\nint on_message_begin(llhttp_t* parser){\r\nprintf(\"parse start\\n\");\r\nreturn 0;\r\n}\r\n\r\nint on_url(llhttp_t* parser, const char* at, size_t length){\r\nchar url[MAX_LEN];\r\nstrncpy(url, at, length);\r\nurl[length] = '\\0';\r\nprintf(\"on_url: %s\\n\", url);\r\nreturn 0;\r\n}\r\n\r\nint on_status(llhttp_t* parser, const char* at, size_t length){\r\nchar status[MAX_LEN];\r\nstrncpy(status, at, length);\r\nstatus[length] = '\\0';\r\nprintf(\"on_status: %s\\n\", status);\r\nreturn 0;\r\n}\r\n\r\nint on_header_field(llhttp_t* parser, const char* at, size_t length){\r\nchar header_field[MAX_LEN];\r\nstrncpy(header_field, at, length);\r\nheader_field[length] = '\\0';\r\nprintf(\"head field: %s\\n\", header_field);\r\nreturn 0;\r\n}\r\n\r\nint on_header_value(llhttp_t* parser, const char* at, size_t length){\r\nchar header_value[MAX_LEN];\r\nstrncpy(header_value, at, length);\r\nheader_value[length] = '\\0';\r\nprintf(\"head value: %s\\n\", header_value);\r\nreturn 0;\r\n}\r\n\r\nint on_headers_complete(llhttp_t* parser){\r\nprintf(\"on_headers_complete, major: %d, major: %d, keep-alive: %d, upgrade: %d\\n\", parser->http_major, parser->http_minor, llhttp_should_keep_alive(parser), parser->upgrade);\r\nreturn 0;\r\n}\r\n\r\nint on_body(llhttp_t* parser, const char* at, size_t length){\r\nchar body[MAX_LEN];\r\nstrncpy(body, at, length);\r\nbody[length] = '\\0';\r\nprintf(\"on_body: %s\\n\", body);\r\nreturn 0;\r\n}\r\n\r\nint on_message_complete(llhttp_t* parser){\r\nprintf(\"on_message_complete\\n\");\r\nreturn 0;\r\n}\r\n\r\nint main(){\r\nllhttp_t parser;\r\nllhttp_settings_t settings;\r\nllhttp_settings_init(&settings);\r\nllhttp_init(&parser, HTTP_RESPONSE, &settings);\r\n\r\nsettings.on_message_begin = on_message_begin;\r\nsettings.on_url = on_url;\r\nsettings.on_status = on_status;\r\nsettings.on_header_field = on_header_field;\r\nsettings.on_header_value = on_header_value;\r\nsettings.on_headers_complete = on_headers_complete;\r\nsettings.on_body = on_body;\r\nsettings.on_message_complete = on_message_complete;\r\n\r\nconst char* reponse = \"HTTP\/1.1 200 OK\\r\\nServer: nginx\\r\\ncontent-length: 11\\r\\n\\r\\nhello:world\\r\\n\\r\\n\";\r\nint reponse_len = strlen(reponse);\r\n\r\nenum llhttp_errno err = llhttp_execute(&parser, reponse, reponse_len);\r\n\r\nif (err != HPE_OK) {\r\nfprintf(stderr, \"Parse error: %s %s\\n\", llhttp_errno_name(err),\r\nparser.reason);\r\n}\r\n\r\nreturn 0;\r\n}\r\n<\/pre>\n

llhttp \u76ee\u524d\u652f\u6301\u4ee5\u4e0b\u94a9\u5b50\u56de\u8c03\u3002<\/p>\n

\r\nstruct llhttp_settings_s {\r\n\/* Possible return values 0, -1, `HPE_PAUSED` *\/\r\nllhttp_cb on_message_begin;\r\n\r\n\/* Possible return values 0, -1, HPE_USER *\/\r\nllhttp_data_cb on_url;\r\nllhttp_data_cb on_status;\r\nllhttp_data_cb on_header_field;\r\nllhttp_data_cb on_header_value;\r\n\r\n\/* Possible return values:\r\n* 0 - Proceed normally\r\n* 1 - Assume that request\/response has no body, and proceed to parsing the\r\n* next message\r\n* 2 - Assume absence of body (as above) and make `llhttp_execute()` return\r\n* `HPE_PAUSED_UPGRADE`\r\n* -1 - Error\r\n* `HPE_PAUSED`\r\n*\/\r\nllhttp_cb on_headers_complete;\r\n\r\n\/* Possible return values 0, -1, HPE_USER *\/\r\nllhttp_data_cb on_body;\r\n\r\n\/* Possible return values 0, -1, `HPE_PAUSED` *\/\r\nllhttp_cb on_message_complete;\r\n\r\n\/* When on_chunk_header is called, the current chunk length is stored\r\n* in parser->content_length.\r\n* Possible return values 0, -1, `HPE_PAUSED`\r\n*\/\r\nllhttp_cb on_chunk_header;\r\nllhttp_cb on_chunk_complete;\r\n\r\n\/* Information-only callbacks, return value is ignored *\/\r\nllhttp_cb on_url_complete;\r\nllhttp_cb on_status_complete;\r\nllhttp_cb on_header_field_complete;\r\nllhttp_cb on_header_value_complete;};\r\n<\/pre>\n

\u6211\u4eec\u4e5f\u53ef\u4ee5\u4ee5\u9759\u6001\u5e93\u6216\u52a8\u6001\u5e93\u7684\u65b9\u5f0f\u4f7f\u7528 llhttp\u3002\u6267\u884c make all \u5c31\u4f1a\u5728 build \u76ee\u5f55\u4e0b\u751f\u6210\u9759\u6001\u548c\u52a8\u6001\u5e93\uff0c\u6211\u4eec\u628a\u5934\u6587\u4ef6 llhttp.h \u548c \u9759\u6001\u5e93\u6216\u52a8\u6001\u5e93\u590d\u5236\u5230\u81ea\u5df1\u9879\u76ee\u91cc\u4f7f\u7528\u5c31\u53ef\u4ee5\uff0c\u7f16\u8bd1\u7684\u65f6\u5019\u52a0\u4e0a -lllhttp -L.\u3002<\/p>\n

\u603b\u7ed3\uff1allhttp \u7684\u4f7f\u7528\u4e0a\u8fd8\u7b97\u6bd4\u8f83\u7b80\u5355\u6e05\u6670\uff0c\u5982\u679c\u6211\u4eec\u9879\u76ee\u91cc\u9700\u8981\u89e3\u6790 HTTP \u534f\u8bae\u7684\u8bdd\u53ef\u4ee5\u8bd5\u8bd5\uff0c\u4f7f\u7528 demo \u53ef\u4ee5\u53c2\u8003 https:\/\/github.com\/theanarkh\/llhttp-demo\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

llhttp \u662f Node.js \u7684 HTTP 1.1 \u89e3\u6790\u5668\uff0c\u7528\u4e8e\u66ff\u4ee3\u65e9\u671f\u7684http_parser\uff0c\u6027\u80fd\u4e0a\u6709 […]<\/p>\n","protected":false},"author":1898,"featured_media":228004,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[757],"class_list":["post-227998","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread","tag-llhttp"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/227998","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1898"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=227998"}],"version-history":[{"count":4,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/227998\/revisions"}],"predecessor-version":[{"id":228003,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/227998\/revisions\/228003"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/228004"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=227998"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=227998"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=227998"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}