HTTP Parser
===========
[![Build Status](https://travis-ci.org/joyent/http-parser.png?branch=master)](https://travis-ci.org/joyent/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 Web Socket 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 http://tools.ietf.org/html/draft-hixie-thewebsocketprotocol-75 for more
information the Web Socket 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_uri,
(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.
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.
See examples of reading in headers:
* [partial example](http://gist.github.com/155877) in C
* [from http-parser tests](http://github.com/joyent/http-parser/blob/37a0ff8/test.c#L403) in C
* [from Node library](http://github.com/joyent/node/blob/842eaf4/src/http.js#L284) in Javascript
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
一、前言 Lua 是一种轻量小巧的脚本语言,用标准 C 语言编写并以源代码形式开放,其设计目的是为了嵌入应用程序中,从而为应用程序提供灵活的扩展和定制功能。 Lua 可以应用在游戏开发、独立应用脚本、Web 应用脚本、扩展和数据库插件、安全系统等场景。 笔者学习的目的主要是为了能在 Web 应用(Nginx、Redis)中使用到 Lua 脚本。 特点 Lua脚本可以很容易的被C/C++ 代码调用,也可以反过来调用C/C++的函数。 Lua由标准C编写而成,代码简洁优美,几乎在所有操作系统和平台上都可以编译,运行。 以极小的体积和简单的语法提供全面的功能。[一个完整的Lua解释器不过200k,在目前所有脚本引擎中,Lua的速度是最快的] 实现与宿主语言的简洁接口 成为一个很容易嵌入其它语言中使用的语言。 特性 支持面向对象编程 轻量级 可扩展 函数式编程 自动内存管理 通用类型的表–table,可实现数组,哈希表,集合,对象 支持闭包 支持多线程 支持交互式编程
资源推荐
资源详情
资源评论
收起资源包目录
lua客户端网络库,主要供手机游戏客户端使用.rar (59个子文件)
lua客户端网络库,主要供手机游戏客户端使用
新建文本文档.txt 19B
clientnet-select-master
deps
http-parser
.mailmap 446B
test.c 93KB
.travis.yml 130B
Makefile 4KB
AUTHORS 2KB
LICENSE-MIT 1KB
http_parser.h 12KB
http_parser.gyp 3KB
.gitignore 235B
http_parser.c 62KB
testparser.c 2KB
README.md 7KB
contrib
url_parser.c 1KB
parsertrace.c 4KB
llist.h 2KB
SysTime.h 5KB
dlist.h 969B
Makefile 783B
LICENSE 18KB
LuaUtil.h 1KB
Packet.h 2KB
ByteBuffer.h 3KB
Socket.cpp 8KB
cjson.dll 578KB
des56.dll 538KB
main.cpp 357B
HttpPacket.h 3KB
Socket.h 3KB
libmysql.dll 2.38MB
md5
md5.lua 836B
core.dll 537KB
Reactor.h 254B
LuaPacket.h 299B
RawBinPacket.h 1020B
RPacket.h 3KB
Decoder.h 1KB
lua
http.lua 4KB
router.lua 873B
socket.lua 506B
HttpDecoder.h 3KB
NetLua.cpp 5KB
WPacket.h 3KB
SysTime.cpp 187B
LuaPacket.cpp 22KB
Reactor.cpp 2KB
example
httpserver.lua 287B
httpclient.lua 846B
client.lua 323B
echo.lua 275B
sdkserver
clientGetToken.lua 987B
clientLoginServer.lua 817B
server.lua 317B
getToken.lua 350B
loginServer.lua 415B
testmysql.lua 789B
README.md 135B
luasql
mysql.dll 282KB
RPacket.cpp 258B
共 59 条
- 1
资源评论
野生的狒狒
- 粉丝: 1498
- 资源: 1530
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功