Skip to content
This repository was archived by the owner on Feb 15, 2023. It is now read-only.

Commit 05ba06d

Browse files
committed
Update http-parser to 2.5.0
1 parent 7b18990 commit 05ba06d

12 files changed

+1407
-316
lines changed

http-parser/.gitignore

+24
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,28 @@
1+
/out/
2+
core
13
tags
24
*.o
35
test
46
test_g
7+
test_fast
8+
bench
9+
url_parser
10+
parsertrace
11+
parsertrace_g
12+
*.mk
13+
*.Makefile
14+
*.so.*
15+
*.a
16+
17+
18+
# Visual Studio uglies
19+
*.suo
20+
*.sln
21+
*.vcxproj
22+
*.vcxproj.filters
23+
*.vcxproj.user
24+
*.opensdf
25+
*.ncrunchsolution*
26+
*.sdf
27+
*.vsp
28+
*.psess

http-parser/.mailmap

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# update AUTHORS with:
2+
# git log --all --reverse --format='%aN <%aE>' | perl -ne 'BEGIN{print "# Authors ordered by first contribution.\n"} print unless $h{$_}; $h{$_} = 1' > AUTHORS
3+
Ryan Dahl <[email protected]>
4+
Salman Haq <[email protected]>
5+
Simon Zimmermann <[email protected]>
6+
Thomas LE ROUX <[email protected]> LE ROUX Thomas <[email protected]>
7+
Thomas LE ROUX <[email protected]> Thomas LE ROUX <[email protected]>
8+
Fedor Indutny <[email protected]>

http-parser/.travis.yml

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
language: c
2+
3+
compiler:
4+
- clang
5+
- gcc
6+
7+
script:
8+
- "make"
9+
10+
notifications:
11+
email: false
12+
irc:
13+
- "irc.freenode.net#node-ci"

http-parser/AUTHORS

+28-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Andre Caron <[email protected]>
2828
Ivo Raisr <[email protected]>
2929
James McLaughlin <[email protected]>
3030
David Gwynne <[email protected]>
31-
LE ROUX Thomas <[email protected]>
31+
Thomas LE ROUX <[email protected]>
3232
Randy Rizun <[email protected]>
3333
Andre Louis Caron <[email protected]>
3434
Simon Zimmermann <[email protected]>
@@ -38,3 +38,30 @@ Bertrand Paquet <[email protected]>
3838
BogDan Vatra <[email protected]>
3939
Peter Faiman <[email protected]>
4040
Corey Richardson <[email protected]>
41+
Tóth Tamás <[email protected]>
42+
Cam Swords <[email protected]>
43+
Chris Dickinson <[email protected]>
44+
Uli Köhler <[email protected]>
45+
Charlie Somerville <[email protected]>
46+
Patrik Stutz <[email protected]>
47+
Fedor Indutny <[email protected]>
48+
49+
Alexis Campailla <[email protected]>
50+
David Wragg <[email protected]>
51+
Vinnie Falco <[email protected]>
52+
Alex Butum <[email protected]>
53+
54+
Alex Kocharin <[email protected]>
55+
Mark Koopman <[email protected]>
56+
Helge Heß <[email protected]>
57+
Alexis La Goutte <[email protected]>
58+
George Miroshnykov <[email protected]>
59+
Maciej Małecki <[email protected]>
60+
Marc O'Morain <[email protected]>
61+
Jeff Pinner <[email protected]>
62+
Timothy J Fontaine <[email protected]>
63+
64+
Romain Giraud <[email protected]>
65+
Jay Satiro <[email protected]>
66+
Arne Steen <[email protected]>
67+
Kjell Schubert <[email protected]>

http-parser/CONTRIBUTIONS

-4
This file was deleted.

http-parser/README.md

+33-28
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
HTTP Parser
22
===========
33

4+
[![Build Status](https://travis-ci.org/joyent/http-parser.png?branch=master)](https://travis-ci.org/joyent/http-parser)
5+
46
This is a parser for HTTP messages written in C. It parses both requests and
57
responses. The parser is designed to be used in performance HTTP
68
applications. It does not make any syscalls nor allocations, it does not
@@ -34,43 +36,46 @@ Usage
3436
One `http_parser` object is used per TCP connection. Initialize the struct
3537
using `http_parser_init()` and set the callbacks. That might look something
3638
like this for a request parser:
39+
```c
40+
http_parser_settings settings;
41+
settings.on_url = my_url_callback;
42+
settings.on_header_field = my_header_field_callback;
43+
/* ... */
3744

38-
http_parser_settings settings;
39-
settings.on_url = my_url_callback;
40-
settings.on_header_field = my_header_field_callback;
41-
/* ... */
42-
43-
http_parser *parser = malloc(sizeof(http_parser));
44-
http_parser_init(parser, HTTP_REQUEST);
45-
parser->data = my_socket;
45+
http_parser *parser = malloc(sizeof(http_parser));
46+
http_parser_init(parser, HTTP_REQUEST);
47+
parser->data = my_socket;
48+
```
4649
4750
When data is received on the socket execute the parser and check for errors.
4851
49-
size_t len = 80*1024, nparsed;
50-
char buf[len];
51-
ssize_t recved;
52+
```c
53+
size_t len = 80*1024, nparsed;
54+
char buf[len];
55+
ssize_t recved;
5256
53-
recved = recv(fd, buf, len, 0);
57+
recved = recv(fd, buf, len, 0);
5458
55-
if (recved < 0) {
56-
/* Handle error. */
57-
}
59+
if (recved < 0) {
60+
/* Handle error. */
61+
}
5862
59-
/* Start up / continue the parser.
60-
* Note we pass recved==0 to signal that EOF has been recieved.
61-
*/
62-
nparsed = http_parser_execute(parser, &settings, buf, recved);
63+
/* Start up / continue the parser.
64+
* Note we pass recved==0 to signal that EOF has been received.
65+
*/
66+
nparsed = http_parser_execute(parser, &settings, buf, recved);
6367
64-
if (parser->upgrade) {
65-
/* handle new protocol */
66-
} else if (nparsed != recved) {
67-
/* Handle error. Usually just close the connection. */
68-
}
68+
if (parser->upgrade) {
69+
/* handle new protocol */
70+
} else if (nparsed != recved) {
71+
/* Handle error. Usually just close the connection. */
72+
}
73+
```
6974

7075
HTTP needs to know where the end of the stream is. For example, sometimes
7176
servers send responses without Content-Length and expect the client to
7277
consume input (for the body) until EOF. To tell http_parser about EOF, give
73-
`0` as the forth parameter to `http_parser_execute()`. Callbacks and errors
78+
`0` as the fourth parameter to `http_parser_execute()`. Callbacks and errors
7479
can still be encountered during an EOF, so one must still be prepared
7580
to receive them.
7681

@@ -105,7 +110,7 @@ followed by non-HTTP data.
105110
information the Web Socket protocol.)
106111

107112
To support this, the parser will treat this as a normal HTTP message without a
108-
body. Issuing both on_headers_complete and on_message_complete callbacks. However
113+
body, issuing both on_headers_complete and on_message_complete callbacks. However
109114
http_parser_execute() will stop parsing at the end of the headers and return.
110115

111116
The user is expected to check if `parser->upgrade` has been set to 1 after
@@ -126,7 +131,7 @@ There are two types of callbacks:
126131
* notification `typedef int (*http_cb) (http_parser*);`
127132
Callbacks: on_message_begin, on_headers_complete, on_message_complete.
128133
* data `typedef int (*http_data_cb) (http_parser*, const char *at, size_t length);`
129-
Callbacks: (requests only) on_uri,
134+
Callbacks: (requests only) on_url,
130135
(common) on_header_field, on_header_value, on_body;
131136

132137
Callbacks must return 0 on success. Returning a non-zero value indicates
@@ -140,7 +145,7 @@ buffer to avoid copying memory around if this fits your application.
140145

141146
Reading headers may be a tricky task if you read/parse headers partially.
142147
Basically, you need to remember whether last header callback was field or value
143-
and apply following logic:
148+
and apply the following logic:
144149

145150
(on_header_field and on_header_value shortened to on_h_*)
146151
------------------------ ------------ --------------------------------------------

http-parser/bench.c

+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/* Copyright Fedor Indutny. All rights reserved.
2+
*
3+
* Permission is hereby granted, free of charge, to any person obtaining a copy
4+
* of this software and associated documentation files (the "Software"), to
5+
* deal in the Software without restriction, including without limitation the
6+
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7+
* sell copies of the Software, and to permit persons to whom the Software is
8+
* furnished to do so, subject to the following conditions:
9+
*
10+
* The above copyright notice and this permission notice shall be included in
11+
* all copies or substantial portions of the Software.
12+
*
13+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19+
* IN THE SOFTWARE.
20+
*/
21+
#include "http_parser.h"
22+
#include <assert.h>
23+
#include <stdio.h>
24+
#include <string.h>
25+
#include <sys/time.h>
26+
27+
static const char data[] =
28+
"POST /joyent/http-parser HTTP/1.1\r\n"
29+
"Host: github.com\r\n"
30+
"DNT: 1\r\n"
31+
"Accept-Encoding: gzip, deflate, sdch\r\n"
32+
"Accept-Language: ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4\r\n"
33+
"User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) "
34+
"AppleWebKit/537.36 (KHTML, like Gecko) "
35+
"Chrome/39.0.2171.65 Safari/537.36\r\n"
36+
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,"
37+
"image/webp,*/*;q=0.8\r\n"
38+
"Referer: https://github.com/joyent/http-parser\r\n"
39+
"Connection: keep-alive\r\n"
40+
"Transfer-Encoding: chunked\r\n"
41+
"Cache-Control: max-age=0\r\n\r\nb\r\nhello world\r\n0\r\n\r\n";
42+
static const size_t data_len = sizeof(data) - 1;
43+
44+
static int on_info(http_parser* p) {
45+
return 0;
46+
}
47+
48+
49+
static int on_data(http_parser* p, const char *at, size_t length) {
50+
return 0;
51+
}
52+
53+
static http_parser_settings settings = {
54+
.on_message_begin = on_info,
55+
.on_headers_complete = on_info,
56+
.on_message_complete = on_info,
57+
.on_header_field = on_data,
58+
.on_header_value = on_data,
59+
.on_url = on_data,
60+
.on_status = on_data,
61+
.on_body = on_data
62+
};
63+
64+
int bench(int iter_count, int silent) {
65+
struct http_parser parser;
66+
int i;
67+
int err;
68+
struct timeval start;
69+
struct timeval end;
70+
float rps;
71+
72+
if (!silent) {
73+
err = gettimeofday(&start, NULL);
74+
assert(err == 0);
75+
}
76+
77+
for (i = 0; i < iter_count; i++) {
78+
size_t parsed;
79+
http_parser_init(&parser, HTTP_REQUEST);
80+
81+
parsed = http_parser_execute(&parser, &settings, data, data_len);
82+
assert(parsed == data_len);
83+
}
84+
85+
if (!silent) {
86+
err = gettimeofday(&end, NULL);
87+
assert(err == 0);
88+
89+
fprintf(stdout, "Benchmark result:\n");
90+
91+
rps = (float) (end.tv_sec - start.tv_sec) +
92+
(end.tv_usec - start.tv_usec) * 1e-6f;
93+
fprintf(stdout, "Took %f seconds to run\n", rps);
94+
95+
rps = (float) iter_count / rps;
96+
fprintf(stdout, "%f req/sec\n", rps);
97+
fflush(stdout);
98+
}
99+
100+
return 0;
101+
}
102+
103+
int main(int argc, char** argv) {
104+
if (argc == 2 && strcmp(argv[1], "infinite") == 0) {
105+
for (;;)
106+
bench(5000000, 1);
107+
return 0;
108+
} else {
109+
return bench(5000000, 0);
110+
}
111+
}

0 commit comments

Comments
 (0)