http_parser.rb 0.6.0.beta.1 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. checksums.yaml +7 -0
  2. data/.github/workflows/linux.yml +23 -0
  3. data/.github/workflows/windows.yml +23 -0
  4. data/.gitignore +5 -4
  5. data/.gitmodules +2 -2
  6. data/README.md +2 -2
  7. data/Rakefile +4 -2
  8. data/ext/ruby_http_parser/extconf.rb +1 -1
  9. data/ext/ruby_http_parser/org/ruby_http_parser/RubyHttpParser.java +86 -52
  10. data/ext/ruby_http_parser/ruby_http_parser.c +53 -7
  11. data/ext/ruby_http_parser/vendor/http-parser/AUTHORS +37 -1
  12. data/ext/ruby_http_parser/vendor/http-parser/LICENSE-MIT +1 -5
  13. data/ext/ruby_http_parser/vendor/http-parser/Makefile +110 -8
  14. data/ext/ruby_http_parser/vendor/http-parser/README.md +105 -37
  15. data/ext/ruby_http_parser/vendor/http-parser/bench.c +128 -0
  16. data/ext/ruby_http_parser/vendor/http-parser/contrib/parsertrace.c +157 -0
  17. data/ext/ruby_http_parser/vendor/http-parser/contrib/url_parser.c +47 -0
  18. data/ext/ruby_http_parser/vendor/http-parser/http_parser.c +892 -510
  19. data/ext/ruby_http_parser/vendor/http-parser/http_parser.gyp +34 -2
  20. data/ext/ruby_http_parser/vendor/http-parser/http_parser.h +198 -77
  21. data/ext/ruby_http_parser/vendor/http-parser/test.c +1781 -201
  22. data/ext/ruby_http_parser/vendor/http-parser-java/http_parser.c +271 -154
  23. data/ext/ruby_http_parser/vendor/http-parser-java/http_parser.h +48 -61
  24. data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/HTTPMethod.java +5 -3
  25. data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/ParserSettings.java +37 -104
  26. data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/lolevel/HTTPParser.java +116 -101
  27. data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/lolevel/ParserSettings.java +9 -5
  28. data/ext/ruby_http_parser/vendor/http-parser-java/src/test/http_parser/lolevel/Message.java +1 -1
  29. data/ext/ruby_http_parser/vendor/http-parser-java/test.c +579 -153
  30. data/http_parser.rb.gemspec +14 -9
  31. data/spec/parser_spec.rb +177 -99
  32. data/spec/support/requests.json +2 -2
  33. data/spec/support/responses.json +20 -0
  34. data/tasks/spec.rake +1 -1
  35. metadata +131 -162
  36. data/Gemfile.lock +0 -39
  37. data/ext/ruby_http_parser/vendor/http-parser/CONTRIBUTIONS +0 -4
  38. data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/HTTPHeadersCompleteCallback.java +0 -13
  39. data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/lolevel/HTTPHeadersCompleteCallback.java +0 -12
@@ -28,5 +28,41 @@ Andre Caron <andre.l.caron@gmail.com>
28
28
  Ivo Raisr <ivosh@ivosh.net>
29
29
  James McLaughlin <jamie@lacewing-project.org>
30
30
  David Gwynne <loki@animata.net>
31
- LE ROUX Thomas <thomas@procheo.fr>
31
+ Thomas LE ROUX <thomas@november-eleven.fr>
32
32
  Randy Rizun <rrizun@ortivawireless.com>
33
+ Andre Louis Caron <andre.louis.caron@usherbrooke.ca>
34
+ Simon Zimmermann <simonz05@gmail.com>
35
+ Erik Dubbelboer <erik@dubbelboer.com>
36
+ Martell Malone <martellmalone@gmail.com>
37
+ Bertrand Paquet <bpaquet@octo.com>
38
+ BogDan Vatra <bogdan@kde.org>
39
+ Peter Faiman <peter@thepicard.org>
40
+ Corey Richardson <corey@octayn.net>
41
+ Tóth Tamás <tomika_nospam@freemail.hu>
42
+ Cam Swords <cam.swords@gmail.com>
43
+ Chris Dickinson <christopher.s.dickinson@gmail.com>
44
+ Uli Köhler <ukoehler@btronik.de>
45
+ Charlie Somerville <charlie@charliesomerville.com>
46
+ Patrik Stutz <patrik.stutz@gmail.com>
47
+ Fedor Indutny <fedor.indutny@gmail.com>
48
+ runner <runner.mei@gmail.com>
49
+ Alexis Campailla <alexis@janeasystems.com>
50
+ David Wragg <david@wragg.org>
51
+ Vinnie Falco <vinnie.falco@gmail.com>
52
+ Alex Butum <alexbutum@linux.com>
53
+ Rex Feng <rexfeng@gmail.com>
54
+ Alex Kocharin <alex@kocharin.ru>
55
+ Mark Koopman <markmontymark@yahoo.com>
56
+ Helge Heß <me@helgehess.eu>
57
+ Alexis La Goutte <alexis.lagoutte@gmail.com>
58
+ George Miroshnykov <george.miroshnykov@gmail.com>
59
+ Maciej Małecki <me@mmalecki.com>
60
+ Marc O'Morain <github.com@marcomorain.com>
61
+ Jeff Pinner <jpinner@twitter.com>
62
+ Timothy J Fontaine <tjfontaine@gmail.com>
63
+ Akagi201 <akagi201@gmail.com>
64
+ Romain Giraud <giraud.romain@gmail.com>
65
+ Jay Satiro <raysatiro@yahoo.com>
66
+ Arne Steen <Arne.Steen@gmx.de>
67
+ Kjell Schubert <kjell.schubert@gmail.com>
68
+ Olivier Mengué <dolmen@cpan.org>
@@ -1,8 +1,4 @@
1
- http_parser.c is based on src/http/ngx_http_parse.c from NGINX copyright
2
- Igor Sysoev.
3
-
4
- Additional changes are licensed under the same terms as NGINX and
5
- copyright Joyent, Inc. and other Node contributors. All rights reserved.
1
+ Copyright Joyent, Inc. and other Node contributors.
6
2
 
7
3
  Permission is hereby granted, free of charge, to any person obtaining a copy
8
4
  of this software and associated documentation files (the "Software"), to
@@ -1,20 +1,80 @@
1
+ # Copyright Joyent, Inc. and other Node contributors. 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
+ PLATFORM ?= $(shell sh -c 'uname -s | tr "[A-Z]" "[a-z]"')
22
+ HELPER ?=
23
+ BINEXT ?=
24
+ SOLIBNAME = libhttp_parser
25
+ SOMAJOR = 2
26
+ SOMINOR = 8
27
+ SOREV = 1
28
+ ifeq (darwin,$(PLATFORM))
29
+ SOEXT ?= dylib
30
+ SONAME ?= $(SOLIBNAME).$(SOMAJOR).$(SOMINOR).$(SOEXT)
31
+ LIBNAME ?= $(SOLIBNAME).$(SOMAJOR).$(SOMINOR).$(SOREV).$(SOEXT)
32
+ else ifeq (wine,$(PLATFORM))
33
+ CC = winegcc
34
+ BINEXT = .exe.so
35
+ HELPER = wine
36
+ else
37
+ SOEXT ?= so
38
+ SONAME ?= $(SOLIBNAME).$(SOEXT).$(SOMAJOR).$(SOMINOR)
39
+ LIBNAME ?= $(SOLIBNAME).$(SOEXT).$(SOMAJOR).$(SOMINOR).$(SOREV)
40
+ endif
41
+
1
42
  CC?=gcc
2
43
  AR?=ar
3
44
 
45
+ CPPFLAGS ?=
46
+ LDFLAGS ?=
47
+
4
48
  CPPFLAGS += -I.
5
- CPPFLAGS_DEBUG = $(CPPFLAGS) -DHTTP_PARSER_STRICT=1 -DHTTP_PARSER_DEBUG=1
49
+ CPPFLAGS_DEBUG = $(CPPFLAGS) -DHTTP_PARSER_STRICT=1
6
50
  CPPFLAGS_DEBUG += $(CPPFLAGS_DEBUG_EXTRA)
7
- CPPFLAGS_FAST = $(CPPFLAGS) -DHTTP_PARSER_STRICT=0 -DHTTP_PARSER_DEBUG=0
51
+ CPPFLAGS_FAST = $(CPPFLAGS) -DHTTP_PARSER_STRICT=0
8
52
  CPPFLAGS_FAST += $(CPPFLAGS_FAST_EXTRA)
53
+ CPPFLAGS_BENCH = $(CPPFLAGS_FAST)
9
54
 
10
55
  CFLAGS += -Wall -Wextra -Werror
11
56
  CFLAGS_DEBUG = $(CFLAGS) -O0 -g $(CFLAGS_DEBUG_EXTRA)
12
57
  CFLAGS_FAST = $(CFLAGS) -O3 $(CFLAGS_FAST_EXTRA)
58
+ CFLAGS_BENCH = $(CFLAGS_FAST) -Wno-unused-parameter
13
59
  CFLAGS_LIB = $(CFLAGS_FAST) -fPIC
14
60
 
61
+ LDFLAGS_LIB = $(LDFLAGS) -shared
62
+
63
+ INSTALL ?= install
64
+ PREFIX ?= /usr/local
65
+ LIBDIR = $(PREFIX)/lib
66
+ INCLUDEDIR = $(PREFIX)/include
67
+
68
+ ifeq (darwin,$(PLATFORM))
69
+ LDFLAGS_LIB += -Wl,-install_name,$(LIBDIR)/$(SONAME)
70
+ else
71
+ # TODO(bnoordhuis) The native SunOS linker expects -h rather than -soname...
72
+ LDFLAGS_LIB += -Wl,-soname=$(SONAME)
73
+ endif
74
+
15
75
  test: test_g test_fast
16
- ./test_g
17
- ./test_fast
76
+ $(HELPER) ./test_g$(BINEXT)
77
+ $(HELPER) ./test_fast$(BINEXT)
18
78
 
19
79
  test_g: http_parser_g.o test_g.o
20
80
  $(CC) $(CFLAGS_DEBUG) $(LDFLAGS) http_parser_g.o test_g.o -o $@
@@ -31,11 +91,17 @@ test_fast: http_parser.o test.o http_parser.h
31
91
  test.o: test.c http_parser.h Makefile
32
92
  $(CC) $(CPPFLAGS_FAST) $(CFLAGS_FAST) -c test.c -o $@
33
93
 
94
+ bench: http_parser.o bench.o
95
+ $(CC) $(CFLAGS_BENCH) $(LDFLAGS) http_parser.o bench.o -o $@
96
+
97
+ bench.o: bench.c http_parser.h Makefile
98
+ $(CC) $(CPPFLAGS_BENCH) $(CFLAGS_BENCH) -c bench.c -o $@
99
+
34
100
  http_parser.o: http_parser.c http_parser.h Makefile
35
101
  $(CC) $(CPPFLAGS_FAST) $(CFLAGS_FAST) -c http_parser.c
36
102
 
37
103
  test-run-timed: test_fast
38
- while(true) do time ./test_fast > /dev/null; done
104
+ while(true) do time $(HELPER) ./test_fast$(BINEXT) > /dev/null; done
39
105
 
40
106
  test-valgrind: test_g
41
107
  valgrind ./test_g
@@ -44,15 +110,51 @@ libhttp_parser.o: http_parser.c http_parser.h Makefile
44
110
  $(CC) $(CPPFLAGS_FAST) $(CFLAGS_LIB) -c http_parser.c -o libhttp_parser.o
45
111
 
46
112
  library: libhttp_parser.o
47
- $(CC) -shared -o libhttp_parser.so libhttp_parser.o
113
+ $(CC) $(LDFLAGS_LIB) -o $(LIBNAME) $<
48
114
 
49
115
  package: http_parser.o
50
116
  $(AR) rcs libhttp_parser.a http_parser.o
51
117
 
118
+ url_parser: http_parser.o contrib/url_parser.c
119
+ $(CC) $(CPPFLAGS_FAST) $(CFLAGS_FAST) $^ -o $@
120
+
121
+ url_parser_g: http_parser_g.o contrib/url_parser.c
122
+ $(CC) $(CPPFLAGS_DEBUG) $(CFLAGS_DEBUG) $^ -o $@
123
+
124
+ parsertrace: http_parser.o contrib/parsertrace.c
125
+ $(CC) $(CPPFLAGS_FAST) $(CFLAGS_FAST) $^ -o parsertrace$(BINEXT)
126
+
127
+ parsertrace_g: http_parser_g.o contrib/parsertrace.c
128
+ $(CC) $(CPPFLAGS_DEBUG) $(CFLAGS_DEBUG) $^ -o parsertrace_g$(BINEXT)
129
+
52
130
  tags: http_parser.c http_parser.h test.c
53
131
  ctags $^
54
132
 
133
+ install: library
134
+ $(INSTALL) -D http_parser.h $(DESTDIR)$(INCLUDEDIR)/http_parser.h
135
+ $(INSTALL) -D $(LIBNAME) $(DESTDIR)$(LIBDIR)/$(LIBNAME)
136
+ ln -s $(LIBNAME) $(DESTDIR)$(LIBDIR)/$(SONAME)
137
+ ln -s $(LIBNAME) $(DESTDIR)$(LIBDIR)/$(SOLIBNAME).$(SOEXT)
138
+
139
+ install-strip: library
140
+ $(INSTALL) -D http_parser.h $(DESTDIR)$(INCLUDEDIR)/http_parser.h
141
+ $(INSTALL) -D -s $(LIBNAME) $(DESTDIR)$(LIBDIR)/$(LIBNAME)
142
+ ln -s $(LIBNAME) $(DESTDIR)$(LIBDIR)/$(SONAME)
143
+ ln -s $(LIBNAME) $(DESTDIR)$(LIBDIR)/$(SOLIBNAME).$(SOEXT)
144
+
145
+ uninstall:
146
+ rm $(DESTDIR)$(INCLUDEDIR)/http_parser.h
147
+ rm $(DESTDIR)$(LIBDIR)/$(SOLIBNAME).$(SOEXT)
148
+ rm $(DESTDIR)$(LIBDIR)/$(SONAME)
149
+ rm $(DESTDIR)$(LIBDIR)/$(LIBNAME)
150
+
55
151
  clean:
56
- rm -f *.o *.a test test_fast test_g http_parser.tar tags libhttp_parser.so libhttp_parser.o
152
+ rm -f *.o *.a tags test test_fast test_g \
153
+ http_parser.tar libhttp_parser.so.* \
154
+ url_parser url_parser_g parsertrace parsertrace_g \
155
+ *.exe *.exe.so
156
+
157
+ contrib/url_parser.c: http_parser.h
158
+ contrib/parsertrace.c: http_parser.h
57
159
 
58
- .PHONY: clean package test-run test-run-timed test-valgrind
160
+ .PHONY: clean package test-run test-run-timed test-valgrind install install-strip uninstall
@@ -1,6 +1,8 @@
1
1
  HTTP Parser
2
2
  ===========
3
3
 
4
+ [![Build Status](https://api.travis-ci.org/nodejs/http-parser.svg?branch=master)](https://travis-ci.org/nodejs/http-parser)
5
+
4
6
  This is a parser for HTTP messages written in C. It parses both requests and
5
7
  responses. The parser is designed to be used in performance HTTP
6
8
  applications. It does not make any syscalls nor allocations, it does not
@@ -34,43 +36,46 @@ Usage
34
36
  One `http_parser` object is used per TCP connection. Initialize the struct
35
37
  using `http_parser_init()` and set the callbacks. That might look something
36
38
  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
+ /* ... */
37
44
 
38
- http_parser_settings settings;
39
- settings.on_path = my_path_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
+ ```
46
49
 
47
50
  When data is received on the socket execute the parser and check for errors.
48
51
 
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;
52
56
 
53
- recved = recv(fd, buf, len, 0);
57
+ recved = recv(fd, buf, len, 0);
54
58
 
55
- if (recved < 0) {
56
- /* Handle error. */
57
- }
59
+ if (recved < 0) {
60
+ /* Handle error. */
61
+ }
58
62
 
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);
63
67
 
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
+ ```
69
74
 
70
- HTTP needs to know where the end of the stream is. For example, sometimes
75
+ `http_parser` needs to know where the end of the stream is. For example, sometimes
71
76
  servers send responses without Content-Length and expect the client to
72
- 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
77
+ consume input (for the body) until EOF. To tell `http_parser` about EOF, give
78
+ `0` as the fourth parameter to `http_parser_execute()`. Callbacks and errors
74
79
  can still be encountered during an EOF, so one must still be prepared
75
80
  to receive them.
76
81
 
@@ -88,8 +93,8 @@ the on_body callback.
88
93
  The Special Problem of Upgrade
89
94
  ------------------------------
90
95
 
91
- HTTP supports upgrading the connection to a different protocol. An
92
- increasingly common example of this is the Web Socket protocol which sends
96
+ `http_parser` supports upgrading the connection to a different protocol. An
97
+ increasingly common example of this is the WebSocket protocol which sends
93
98
  a request like
94
99
 
95
100
  GET /demo HTTP/1.1
@@ -101,11 +106,11 @@ a request like
101
106
 
102
107
  followed by non-HTTP data.
103
108
 
104
- (See http://tools.ietf.org/html/draft-hixie-thewebsocketprotocol-75 for more
105
- information the Web Socket protocol.)
109
+ (See [RFC6455](https://tools.ietf.org/html/rfc6455) for more information the
110
+ WebSocket protocol.)
106
111
 
107
112
  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
109
114
  http_parser_execute() will stop parsing at the end of the headers and return.
110
115
 
111
116
  The user is expected to check if `parser->upgrade` has been set to 1 after
@@ -126,21 +131,84 @@ There are two types of callbacks:
126
131
  * notification `typedef int (*http_cb) (http_parser*);`
127
132
  Callbacks: on_message_begin, on_headers_complete, on_message_complete.
128
133
  * 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,
130
135
  (common) on_header_field, on_header_value, on_body;
131
136
 
132
137
  Callbacks must return 0 on success. Returning a non-zero value indicates
133
138
  error to the parser, making it exit immediately.
134
139
 
140
+ For cases where it is necessary to pass local information to/from a callback,
141
+ the `http_parser` object's `data` field can be used.
142
+ An example of such a case is when using threads to handle a socket connection,
143
+ parse a request, and then give a response over that socket. By instantiation
144
+ of a thread-local struct containing relevant data (e.g. accepted socket,
145
+ allocated memory for callbacks to write into, etc), a parser's callbacks are
146
+ able to communicate data between the scope of the thread and the scope of the
147
+ callback in a threadsafe manner. This allows `http_parser` to be used in
148
+ multi-threaded contexts.
149
+
150
+ Example:
151
+ ```c
152
+ typedef struct {
153
+ socket_t sock;
154
+ void* buffer;
155
+ int buf_len;
156
+ } custom_data_t;
157
+
158
+
159
+ int my_url_callback(http_parser* parser, const char *at, size_t length) {
160
+ /* access to thread local custom_data_t struct.
161
+ Use this access save parsed data for later use into thread local
162
+ buffer, or communicate over socket
163
+ */
164
+ parser->data;
165
+ ...
166
+ return 0;
167
+ }
168
+
169
+ ...
170
+
171
+ void http_parser_thread(socket_t sock) {
172
+ int nparsed = 0;
173
+ /* allocate memory for user data */
174
+ custom_data_t *my_data = malloc(sizeof(custom_data_t));
175
+
176
+ /* some information for use by callbacks.
177
+ * achieves thread -> callback information flow */
178
+ my_data->sock = sock;
179
+
180
+ /* instantiate a thread-local parser */
181
+ http_parser *parser = malloc(sizeof(http_parser));
182
+ http_parser_init(parser, HTTP_REQUEST); /* initialise parser */
183
+ /* this custom data reference is accessible through the reference to the
184
+ parser supplied to callback functions */
185
+ parser->data = my_data;
186
+
187
+ http_parser_settings settings; /* set up callbacks */
188
+ settings.on_url = my_url_callback;
189
+
190
+ /* execute parser */
191
+ nparsed = http_parser_execute(parser, &settings, buf, recved);
192
+
193
+ ...
194
+ /* parsed information copied from callback.
195
+ can now perform action on data copied into thread-local memory from callbacks.
196
+ achieves callback -> thread information flow */
197
+ my_data->buffer;
198
+ ...
199
+ }
200
+
201
+ ```
202
+
135
203
  In case you parse HTTP message in chunks (i.e. `read()` request line
136
204
  from socket, parse, read half headers, parse, etc) your data callbacks
137
- may be called more than once. Http-parser guarantees that data pointer is only
205
+ may be called more than once. `http_parser` guarantees that data pointer is only
138
206
  valid for the lifetime of callback. You can also `read()` into a heap allocated
139
207
  buffer to avoid copying memory around if this fits your application.
140
208
 
141
209
  Reading headers may be a tricky task if you read/parse headers partially.
142
210
  Basically, you need to remember whether last header callback was field or value
143
- and apply following logic:
211
+ and apply the following logic:
144
212
 
145
213
  (on_header_field and on_header_value shortened to on_h_*)
146
214
  ------------------------ ------------ --------------------------------------------
@@ -174,5 +242,5 @@ consecutive `on_url` callbacks.
174
242
  See examples of reading in headers:
175
243
 
176
244
  * [partial example](http://gist.github.com/155877) in C
177
- * [from http-parser tests](http://github.com/ry/http-parser/blob/37a0ff8928fb0d83cec0d0d8909c5a4abcd221af/test.c#L403) in C
178
- * [from Node library](http://github.com/ry/node/blob/842eaf446d2fdcb33b296c67c911c32a0dabc747/src/http.js#L284) in Javascript
245
+ * [from http-parser tests](http://github.com/joyent/http-parser/blob/37a0ff8/test.c#L403) in C
246
+ * [from Node library](http://github.com/joyent/node/blob/842eaf4/src/http.js#L284) in Javascript
@@ -0,0 +1,128 @@
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 <stdint.h>
24
+ #include <stdio.h>
25
+ #include <string.h>
26
+ #include <sys/time.h>
27
+
28
+ /* 8 gb */
29
+ static const int64_t kBytes = 8LL << 30;
30
+
31
+ static const char data[] =
32
+ "POST /joyent/http-parser HTTP/1.1\r\n"
33
+ "Host: github.com\r\n"
34
+ "DNT: 1\r\n"
35
+ "Accept-Encoding: gzip, deflate, sdch\r\n"
36
+ "Accept-Language: ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4\r\n"
37
+ "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) "
38
+ "AppleWebKit/537.36 (KHTML, like Gecko) "
39
+ "Chrome/39.0.2171.65 Safari/537.36\r\n"
40
+ "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,"
41
+ "image/webp,*/*;q=0.8\r\n"
42
+ "Referer: https://github.com/joyent/http-parser\r\n"
43
+ "Connection: keep-alive\r\n"
44
+ "Transfer-Encoding: chunked\r\n"
45
+ "Cache-Control: max-age=0\r\n\r\nb\r\nhello world\r\n0\r\n";
46
+ static const size_t data_len = sizeof(data) - 1;
47
+
48
+ static int on_info(http_parser* p) {
49
+ return 0;
50
+ }
51
+
52
+
53
+ static int on_data(http_parser* p, const char *at, size_t length) {
54
+ return 0;
55
+ }
56
+
57
+ static http_parser_settings settings = {
58
+ .on_message_begin = on_info,
59
+ .on_headers_complete = on_info,
60
+ .on_message_complete = on_info,
61
+ .on_header_field = on_data,
62
+ .on_header_value = on_data,
63
+ .on_url = on_data,
64
+ .on_status = on_data,
65
+ .on_body = on_data
66
+ };
67
+
68
+ int bench(int iter_count, int silent) {
69
+ struct http_parser parser;
70
+ int i;
71
+ int err;
72
+ struct timeval start;
73
+ struct timeval end;
74
+
75
+ if (!silent) {
76
+ err = gettimeofday(&start, NULL);
77
+ assert(err == 0);
78
+ }
79
+
80
+ fprintf(stderr, "req_len=%d\n", (int) data_len);
81
+ for (i = 0; i < iter_count; i++) {
82
+ size_t parsed;
83
+ http_parser_init(&parser, HTTP_REQUEST);
84
+
85
+ parsed = http_parser_execute(&parser, &settings, data, data_len);
86
+ assert(parsed == data_len);
87
+ }
88
+
89
+ if (!silent) {
90
+ double elapsed;
91
+ double bw;
92
+ double total;
93
+
94
+ err = gettimeofday(&end, NULL);
95
+ assert(err == 0);
96
+
97
+ fprintf(stdout, "Benchmark result:\n");
98
+
99
+ elapsed = (double) (end.tv_sec - start.tv_sec) +
100
+ (end.tv_usec - start.tv_usec) * 1e-6f;
101
+
102
+ total = (double) iter_count * data_len;
103
+ bw = (double) total / elapsed;
104
+
105
+ fprintf(stdout, "%.2f mb | %.2f mb/s | %.2f req/sec | %.2f s\n",
106
+ (double) total / (1024 * 1024),
107
+ bw / (1024 * 1024),
108
+ (double) iter_count / elapsed,
109
+ elapsed);
110
+
111
+ fflush(stdout);
112
+ }
113
+
114
+ return 0;
115
+ }
116
+
117
+ int main(int argc, char** argv) {
118
+ int64_t iterations;
119
+
120
+ iterations = kBytes / (int64_t) data_len;
121
+ if (argc == 2 && strcmp(argv[1], "infinite") == 0) {
122
+ for (;;)
123
+ bench(iterations, 1);
124
+ return 0;
125
+ } else {
126
+ return bench(iterations, 0);
127
+ }
128
+ }