Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
New http-parser
Browse files Browse the repository at this point in the history
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
ry committed Nov 21, 2009
1 parent 1eba0ca commit 7719ce3
Show file tree
Hide file tree
Showing 12 changed files with 2,135 additions and 6,904 deletions.
4 changes: 1 addition & 3 deletions LICENSE
Expand Up @@ -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:

Expand Down
79 changes: 0 additions & 79 deletions deps/http_parser/LICENSE

This file was deleted.

19 changes: 19 additions & 0 deletions deps/http_parser/LICENSE-MIT
@@ -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.
40 changes: 22 additions & 18 deletions deps/http_parser/Makefile
@@ -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
51 changes: 28 additions & 23 deletions deps/http_parser/README.md
Expand Up @@ -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.

This comment has been minimized.

Copy link
@erichocean

erichocean Nov 21, 2009

perstent -> persistent

* Decodes chunked encoding.
* Extracts the following data from a message
* header fields and values
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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 |
------------------------ ------------ --------------------------------------------
Expand All @@ -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).

1 comment on commit 7719ce3

@ry
Copy link
Author

@ry ry commented on 7719ce3 Nov 21, 2009

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks

Please sign in to comment.