Permalink
Browse files

New http-parser

No longer based on Ragel, but hand-written.

Had to add HTTPConnection.resetParser() because the parser is stricter and
will error out when you try to give it a message after the previous had
"Connection: close". The HTTP client was doing that. Thus we reset the
parser manually after each new connection.
  • Loading branch information...
1 parent 1eba0ca commit 7719ce33db2a68f65f1bed58faece83a2342c7c3 @ry ry committed Nov 21, 2009
View
@@ -26,9 +26,7 @@ are:
Michael Tokarev <mjt@corpit.ru>. Released under the GNU Lesser General
Public License version 2.1.
-Additionally deps/http_parser is based on Zed Shaw's Mongrel. Mongrel is
-copyrighted by Zed Shaw and distributed under GPL2 or a permissive open
-licence. See deps/http_parser/LICENCE for more information.
+Other external libraries are my own and all use the same license as Node.
Node's license follows:
View
@@ -1,79 +0,0 @@
-Copyright 2009, Ryan Lienhart Dahl. All rights reserved.
-Permission is hereby granted, free of charge, to any person obtaining a copy
-of this software and associated documentation files (the "Software"), to
-deal in the Software without restriction, including without limitation the
-rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
-sell copies of the Software, and to permit persons to whom the Software is
-furnished to do so, subject to the following conditions:
-
-The above copyright notice and this permission notice shall be included in
-all copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
-FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
-IN THE SOFTWARE.
-
-
-
-
-http_parser is based on Zed Shaw's Mongrel. Mongrel's license is as follows.
-
--- CUT ---- CUT ---- CUT ---- CUT ---- CUT ---- CUT ---- CUT ---- CUT --
-Mongrel Web Server (Mongrel) is copyrighted free software by Zed A. Shaw
-<zedshaw at zedshaw dot com> and contributors. You can redistribute it
-and/or modify it under either the terms of the GPL2 or the conditions below:
-
-1. You may make and give away verbatim copies of the source form of the
- software without restriction, provided that you duplicate all of the
- original copyright notices and associated disclaimers.
-
-2. You may modify your copy of the software in any way, provided that
- you do at least ONE of the following:
-
- a) place your modifications in the Public Domain or otherwise make them
- Freely Available, such as by posting said modifications to Usenet or an
- equivalent medium, or by allowing the author to include your
- modifications in the software.
-
- b) use the modified software only within your corporation or
- organization.
-
- c) rename any non-standard executables so the names do not conflict with
- standard executables, which must also be provided.
-
- d) make other distribution arrangements with the author.
-
-3. You may distribute the software in object code or executable
- form, provided that you do at least ONE of the following:
-
- a) distribute the executables and library files of the software,
- together with instructions (in the manual page or equivalent) on where
- to get the original distribution.
-
- b) accompany the distribution with the machine-readable source of the
- software.
-
- c) give non-standard executables non-standard names, with
- instructions on where to get the original software distribution.
-
- d) make other distribution arrangements with the author.
-
-4. You may modify and include the part of the software into any other
- software (possibly commercial). But some files in the distribution
- are not written by the author, so that they are not under this terms.
-
-5. The scripts and library files supplied as input to or produced as
- output from the software do not automatically fall under the
- copyright of the software, but belong to whomever generated them,
- and may be sold commercially, and may be aggregated with this
- software.
-
-6. THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
- WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
- PURPOSE.
--- CUT ---- CUT ---- CUT ---- CUT ---- CUT ---- CUT ---- CUT ---- CUT --
@@ -0,0 +1,19 @@
+Copyright 2009 Ryan Dahl <ry@tinyclouds.org>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to
+deal in the Software without restriction, including without limitation the
+rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+sell copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+IN THE SOFTWARE.
View
@@ -1,27 +1,31 @@
-#OPT=-O0 -g -Wall -Wextra -Werror
-OPT=-O2
+OPT_DEBUG=-O0 -g -Wall -Wextra -Werror
+OPT_FAST=-O3 -DHTTP_PARSER_STRICT=0
+
+
+http_parser_g.o: http_parser.c http_parser.h Makefile
+ gcc $(OPT_DEBUG) -c http_parser.c
+
+test_g: http_parser_g.o test.c
+ gcc $(OPT_DEBUG) http_parser.o test.c -o $@
+
+test-run: test_g
+ ./test_g
-test: http_parser.o test.c
- gcc $(OPT) http_parser.o test.c -o $@
http_parser.o: http_parser.c http_parser.h Makefile
- gcc $(OPT) -c http_parser.c
+ gcc $(OPT_FAST) -c http_parser.c
-http_parser.c: http_parser.rl Makefile
- ragel -s -G2 http_parser.rl -o $@
+test: http_parser.o test.c
+ gcc $(OPT_FAST) http_parser.o test.c -o $@
-tags: http_parser.rl http_parser.h test.c
+test-run-timed: test
+ while(true) do time ./test > /dev/null; done
+
+
+tags: http_parser.c http_parser.h test.c
ctags $^
clean:
- rm -f *.o http_parser.c test http_parser.tar
-
-package: http_parser.c
- @rm -rf /tmp/http_parser && mkdir /tmp/http_parser && \
- cp LICENSE README.md Makefile http_parser.c http_parser.rl \
- http_parser.h test.c /tmp/http_parser && \
- cd /tmp && \
- tar -cf http_parser.tar http_parser/
- @echo /tmp/http_parser.tar
+ rm -f *.o test test_g http_parser.tar
-.PHONY: clean package
+.PHONY: clean package test-run test-run-timed
View
@@ -5,13 +5,13 @@ 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 allocations, it does not buffer data, and
it can be interrupted at anytime. It only requires about 128 bytes of data
-per message stream (in a web server that is per connection).
+per message stream (in a web server that is per connection).
Features:
- * No dependencies
+ * No dependencies
* Parses both requests and responses.
- * Handles keep-alive streams.
+ * Handles perstent streams.
@erichocean

erichocean Nov 21, 2009

perstent -> persistent

* Decodes chunked encoding.
* Extracts the following data from a message
* header fields and values
@@ -32,32 +32,47 @@ using `http_parser_init()` and set the callbacks. That might look something
like this:
http_parser *parser = malloc(sizeof(http_parser));
- http_parser_init(parser, HTTP_REQUEST);
+ http_parser_init(parser);
parser->on_path = my_path_callback;
parser->on_header_field = my_header_field_callback;
+ /* ... */
parser->data = my_socket;
When data is received on the socket execute the parser and check for errors.
- size_t len = 80*1024;
+ size_t len = 80*1024, nparsed;
char buf[len];
ssize_t recved;
- recved = read(fd, buf, len);
- if (recved != 0) // handle error
+ recved = recv(fd, buf, len, 0);
- http_parser_execute(parser, buf, recved);
+ if (recved < 0) {
+ /* Handle error. */
+ }
+
+ /* Start up / continue the parser.
+ * Note we pass the recved==0 to http_parse_requests to signal
+ * that EOF has been recieved.
+ */
+ nparsed = http_parse_requests(parser, buf, recved);
- if (http_parser_has_error(parser)) {
- // handle error. usually just close the connection
+ 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 third parameter to `http_parse_requests()`. 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
temporarlly 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.
@@ -70,7 +85,7 @@ parser, for example, would not want such a feature.
Callbacks
---------
-During the `http_parser_execute()` call, the callbacks set in `http_parser`
+During the `http_parse_requests()` call, the callbacks set in `http_parser`
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.
@@ -93,7 +108,7 @@ 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 following logic:
- /* on_header_field and on_header_value shortened to on_h_*
+ (on_header_field and on_header_value shortened to on_h_*)
------------------------ ------------ --------------------------------------------
| State (prev. callback) | Callback | Description/action |
------------------------ ------------ --------------------------------------------
@@ -113,19 +128,9 @@ and apply following logic:
| value | on_h_value | Value continues. Reallocate value buffer |
| | | and append callback data to it |
------------------------ ------------ --------------------------------------------
- */
See examples of reading in headers:
* [partial example](http://gist.github.com/155877) in C
* [from http-parser tests](http://github.com/ry/http-parser/blob/37a0ff8928fb0d83cec0d0d8909c5a4abcd221af/test.c#L403) in C
* [from Node library](http://github.com/ry/node/blob/842eaf446d2fdcb33b296c67c911c32a0dabc747/src/http.js#L284) in Javascript
-
-Releases
---------
-
- * [0.2](http://s3.amazonaws.com/four.livejournal/20090807/http_parser-0.2.tar.gz)
-
- * [0.1](http://s3.amazonaws.com/four.livejournal/20090427/http_parser-0.1.tar.gz)
-
-The source repo is at [github](http://github.com/ry/http-parser).
Oops, something went wrong.

1 comment on commit 7719ce3

ry commented on 7719ce3 Nov 21, 2009

thanks

Please sign in to comment.