Skip to content

Commit

Permalink
Upgrade http parser, change node as needed.
Browse files Browse the repository at this point in the history
The latest version of http-parser is a bit more stringent EOF semantics.
  • Loading branch information
ry committed Oct 15, 2009
1 parent 3456a16 commit b893859
Show file tree
Hide file tree
Showing 13 changed files with 3,176 additions and 2,998 deletions.
16 changes: 7 additions & 9 deletions deps/http_parser/LICENSE
@@ -1,4 +1,5 @@
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
Expand All @@ -15,16 +16,13 @@ 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.



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 --
---- 8< ---- 8< ---- 8< ---- 8< ---- 8< ---- 8< ---- 8< ---- 8< ----
Mongrel Web Server (Mongrel) is copyrighted free software by Zed A. Shaw
<zedshaw at zedshaw dot com> and contributors. You can redistribute it
<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
Expand Down Expand Up @@ -66,14 +64,14 @@ and/or modify it under either the terms of the GPL2 or the conditions below:
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
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,
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 --
---- 8< ---- 8< ---- 8< ---- 8< ---- 8< ---- 8< ---- 8< ---- 8< ----
4 changes: 2 additions & 2 deletions deps/http_parser/Makefile
@@ -1,8 +1,8 @@
#OPT=-O0 -g -Wall -Wextra -Werror
OPT=-O2

test: http_parser.o test.c
gcc $(OPT) http_parser.o test.c -o $@
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
Expand Down
33 changes: 27 additions & 6 deletions deps/http_parser/README.md
Expand Up @@ -5,11 +5,11 @@ 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.
* Decodes chunked encoding.
Expand Down Expand Up @@ -43,21 +43,35 @@ When data is received on the socket execute the parser and check for errors.
char buf[len];
ssize_t recved;

recved = read(fd, buf, len);
if (recved != 0) // handle error
recved = recv(fd, buf, len, 0);

if (recved < 0) {
/* Handle error. */
}

/* Start up / continue the parser.
* Note we pass the recved==0 to http_parser_execute to signal
* that EOF has been recieved.
*/
http_parser_execute(parser, buf, recved);

if (http_parser_has_error(parser)) {
// handle error. usually just close the connection
/* 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_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
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.
Expand Down Expand Up @@ -129,3 +143,10 @@ Releases
* [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).

Bindings
--------

* [Ruby](http://github.com/yakischloba/http-parser-ffi)

* [Lua](http://github.com/phoenixsol/lua-http-parser)

0 comments on commit b893859

Please sign in to comment.