@@ -5,11 +5,11 @@ This is a parser for HTTP messages written in C. It parses both requests
5
5
and responses. The parser is designed to be used in performance HTTP
6
6
applications. It does not make any allocations, it does not buffer data, and
7
7
it can be interrupted at anytime. It only requires about 128 bytes of data
8
- per message stream (in a web server that is per connection).
8
+ per message stream (in a web server that is per connection).
9
9
10
10
Features:
11
11
12
- * No dependencies
12
+ * No dependencies
13
13
* Parses both requests and responses.
14
14
* Handles keep-alive streams.
15
15
* Decodes chunked encoding.
@@ -43,21 +43,35 @@ When data is received on the socket execute the parser and check for errors.
43
43
char buf[len];
44
44
ssize_t recved;
45
45
46
- recved = read(fd, buf, len);
47
- if (recved != 0) // handle error
46
+ recved = recv(fd, buf, len, 0);
48
47
48
+ if (recved < 0) {
49
+ /* Handle error. */
50
+ }
51
+
52
+ /* Start up / continue the parser.
53
+ * Note we pass the recved==0 to http_parser_execute to signal
54
+ * that EOF has been recieved.
55
+ */
49
56
http_parser_execute(parser, buf, recved);
50
57
51
58
if (http_parser_has_error(parser)) {
52
- // handle error. usually just close the connection
59
+ /* Handle error. Usually just close the connection. */
53
60
}
54
61
62
+ HTTP needs to know where the end of the stream is. For example, sometimes
63
+ servers send responses without Content-Length and expect the client to
64
+ consume input (for the body) until EOF. To tell http_parser about EOF, give
65
+ ` 0 ` as the third parameter to ` http_parser_execute() ` . Callbacks and errors
66
+ can still be encountered during an EOF, so one must still be prepared
67
+ to receive them.
68
+
55
69
Scalar valued message information such as ` status_code ` , ` method ` , and the
56
70
HTTP version are stored in the parser structure. This data is only
57
71
temporarlly stored in ` http_parser ` and gets reset on each new message. If
58
72
this information is needed later, copy it out of the structure during the
59
73
` headers_complete ` callback.
60
-
74
+
61
75
The parser decodes the transfer-encoding for both requests and responses
62
76
transparently. That is, a chunked encoding is decoded before being sent to
63
77
the on_body callback.
@@ -129,3 +143,10 @@ Releases
129
143
* [ 0.1] ( http://s3.amazonaws.com/four.livejournal/20090427/http_parser-0.1.tar.gz )
130
144
131
145
The source repo is at [ github] ( http://github.com/ry/http-parser ) .
146
+
147
+ Bindings
148
+ --------
149
+
150
+ * [ Ruby] ( http://github.com/yakischloba/http-parser-ffi )
151
+
152
+ * [ Lua] ( http://github.com/phoenixsol/lua-http-parser )
0 commit comments