HTTP Parser
===========
[![Build Status](https://api.travis-ci.org/nodejs/http-parser.svg?branch=master)](https://travis-ci.org/nodejs/http-parser)
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:
```c
http_parser_settings settings;
settings.on_url = my_url_callback;
settings.on_header_field = my_header_field_callback;
/* ... */
http_parser *parser = malloc(sizeof(http_parser));
http_parser_init(parser, HTTP_REQUEST);
parser->data = my_socket;
```
When data is received on the socket execute the parser and check for errors.
```c
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 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 supports upgrading the connection to a different protocol. An
increasingly common example of this is the WebSocket protocol which sends
a request like
GET /demo HTTP/1.1
Upgrade: WebSocket
Connection: Upgrade
Host: example.com
Origin: http://example.com
WebSocket-Protocol: sample
followed by non-HTTP data.
(See [RFC6455](https://tools.ietf.org/html/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:
```c
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 |
------------------------ ------------ --------------------
没有合适的资源?快使用搜索试试~ 我知道了~
aiohttp-2.2.4.tar.gz
0 下载量 24 浏览量
2024-06-21
23:18:35
上传
评论
收藏 770KB GZ 举报
温馨提示
Python库是一组预先编写的代码模块,旨在帮助开发者实现特定的编程任务,无需从零开始编写代码。这些库可以包括各种功能,如数学运算、文件操作、数据分析和网络编程等。Python社区提供了大量的第三方库,如NumPy、Pandas和Requests,极大地丰富了Python的应用领域,从数据科学到Web开发。Python库的丰富性是Python成为最受欢迎的编程语言之一的关键原因之一。这些库不仅为初学者提供了快速入门的途径,而且为经验丰富的开发者提供了强大的工具,以高效率、高质量地完成复杂任务。例如,Matplotlib和Seaborn库在数据可视化领域内非常受欢迎,它们提供了广泛的工具和技术,可以创建高度定制化的图表和图形,帮助数据科学家和分析师在数据探索和结果展示中更有效地传达信息。
资源推荐
资源详情
资源评论
收起资源包目录
aiohttp-2.2.4.tar.gz (253个子文件)
AUTHORS 2KB
make.bat 7KB
2084.bugfix 74B
2143.bugfix 52B
2060.bugfix 51B
_http_parser.c 487KB
_frozenlist.c 206KB
test.c 110KB
_websocket.c 98KB
http_parser.c 70KB
parsertrace.c 4KB
bench.c 3KB
url_parser.c 1KB
setup.cfg 490B
build.cmd 838B
server.crt 1KB
sample.crt 822B
server.csr 952B
style.css 112B
sample.crt.der 567B
.gitignore 456B
.gitignore 255B
http_parser.gyp 3KB
hello.txt.gz 44B
http_parser.h 14KB
index.html 3KB
websocket.html 2KB
detail.html 524B
index.html 347B
base.html 346B
results.html 320B
500.html 69B
404.html 62B
aiohttp-icon.ico 3KB
MANIFEST.in 452B
tox.ini 1013B
tox.ini 109B
aiohttp.jpg 7KB
fuzzingclient.json 276B
fuzzingserver.json 217B
server.key 2KB
sample.key 887B
LICENSE-MIT 1KB
.mailmap 480B
Makefile 7KB
Makefile 5KB
Makefile 3KB
Makefile 560B
README.md 9KB
PULL_REQUEST_TEMPLATE.md 1KB
ISSUE_TEMPLATE.md 584B
PKG-INFO 17KB
PKG-INFO 17KB
example.png 63KB
aiohttp.png 60KB
aiohttp-icon-128x128.png 60KB
aiohttp-icon-96x96.png 38KB
aiohttp-icon-64x64.png 8KB
aiohttp-icon-32x32.png 6KB
_cparser.pxd 4KB
test_multipart.py 66KB
test_client_functional.py 61KB
test_web_functional.py 42KB
test_connector.py 36KB
test_client_request.py 33KB
test_urldispatch.py 30KB
multipart.py 30KB
test_streams.py 30KB
test_client_functional_oldstyle.py 29KB
web_urldispatcher.py 28KB
test_web_response.py 28KB
connector.py 28KB
client.py 26KB
test_proxy.py 25KB
client_reqrep.py 24KB
helpers.py 23KB
http_parser.py 23KB
test_http_parser.py 21KB
web_response.py 20KB
test_web_protocol.py 20KB
test_cookiejar.py 20KB
web_protocol.py 20KB
test_web_websocket_functional.py 19KB
http_websocket.py 19KB
web_request.py 18KB
test_client_ws.py 18KB
streams.py 17KB
test_client_response.py 17KB
test_utils.py 17KB
test_run_app.py 16KB
web.py 16KB
test_helpers.py 16KB
test_client_session.py 16KB
test_client_ws_functional.py 15KB
test_web_request.py 15KB
test_proxy_functional.py 15KB
backport_cookies.py 15KB
test_web_sendfile_functional.py 14KB
test_web_websocket.py 13KB
test_worker.py 13KB
共 253 条
- 1
- 2
- 3
资源评论
程序员Chino的日记
- 粉丝: 3729
- 资源: 5万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 【岗位说明】绩效考核主管岗位职责.doc
- 【岗位说明】客户关系主管岗位职责.doc
- 【岗位说明】客服经理岗位职责.doc
- 【岗位说明】内勤岗位职责.doc
- 【岗位说明】品管部职能说明.doc
- aWeb安全实践完整版推荐最新版本
- 【岗位说明】前台接待人员岗位职责说明书.doc
- 【岗位说明】前台职责说明.doc
- 【岗位说明】前台文员岗位说明书.doc
- 【岗位说明】人力资源总监职务描述.doc
- 【岗位说明】人事部岗位职责.doc
- 【岗位说明】人事管理岗位职责及要求.doc
- 【岗位说明】人事行政部岗位划分.doc
- 【岗位说明】人事行政经理岗位职责.doc
- 【岗位说明】人事行政部职责.doc
- 【岗位说明】人事行政部职能及岗位职责(非常实用).doc
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功