github
Advanced Search
  • Home
  • Pricing and Signup
  • Explore GitHub
  • Blog
  • Login

ry / http-parser

  • Admin
  • Watch Unwatch
  • Fork
  • Your Fork
  • Pull Request
  • Download Source
    • 189
    • 11
  • Source
  • Commits
  • Network (11)
  • Issues (0)
  • Downloads (3)
  • Graphs
  • Branch: master

click here to add a description

click here to add a homepage

  • Branches (2)
    • master ✓
    • version0.2
  • Tags (3)
    • v0.3
    • v0.2
    • v0.1
Sending Request…
Enable Donations

Pledgie Donations

Once activated, we'll place the following badge in your repository's detail box:
Pledgie_example
This service is courtesy of Pledgie.

http request/response parser for c — Read more

  cancel

  cancel
  • Private
  • Read-Only
  • HTTP Read-Only

This URL has Read+Write access

Only allow 80kb of header bytes 
ry (author)
Tue Feb 02 16:41:45 -0800 2010
commit  1b30bf4ba56a8a9081ed092b46330a59fee652aa
tree    b5b8ed917d610bead287445730d526f38e165646
parent  d5a900264f5a04c9056f0f949d29d2d14b0b0fe2
http-parser /
name age
history
message
file .gitignore Fri Nov 20 06:56:22 -0800 2009 new version Trashing the old Ragel parser (whi... [ry]
file LICENSE-MIT Fri Jan 08 21:42:02 -0800 2010 Update license for 2010 [ry]
file Makefile Tue Feb 02 09:13:45 -0800 2010 Makefile: fix include path [Cliff Frey]
file README.md Fri Jan 08 21:38:17 -0800 2010 API: Define parser type in http_parser_init() ... [ry]
file http_parser.c Tue Feb 02 16:41:45 -0800 2010 Only allow 80kb of header bytes [ry]
file http_parser.h Tue Feb 02 16:41:45 -0800 2010 Only allow 80kb of header bytes [ry]
file test.c Tue Feb 02 09:17:19 -0800 2010 Allow newlines before HTTP requests. I have se... [Cliff Frey]
README.md

HTTP Parser

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 136 bytes of data per message stream (in a web server that is per connection).

Features:

  • No dependencies
  • Parses both requests and responses.
  • Handles persistent streams.
  • Decodes chunked encoding.
  • Extracts the following data from a message
    • header fields and values
    • content-length
    • request method
    • response status code
    • transfer-encoding
    • http version
    • request path, query string, fragment
    • message body
  • Defends against buffer overflow attacks.

Usage

One http_parser object is used per TCP connection. Initialize the struct using http_parser_init() and set the callbacks. That might look something like this for a request parser:

http_parser *parser = malloc(sizeof(http_parser));
http_parser_init(parser, HTTP_REQUEST);
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, nparsed;
char buf[len];
ssize_t recved;

recved = recv(fd, buf, len, 0);

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_parser_execute(parser, buf, recved);

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_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.

It does not decode the content-encoding (gzip). Not all HTTP applications need to inspect the body. Decoding gzip is non-neglagable amount of processing (and requires making allocations). HTTP proxies using this parser, for example, would not want such a feature.

Callbacks

During the http_parser_execute() 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.

There are two types of callbacks:

  • notification typedef int (*http_cb) (http_parser*); Callbacks: on_message_begin, on_headers_complete, on_message_complete.
  • data typedef int (*http_data_cb) (http_parser*, const char *at, size_t length); Callbacks: (requests only) on_path, on_query_string, on_uri, on_fragment,
             (common) on_header_field, on_header_value, on_body;
    

In case you parse HTTP message in chunks (i.e. read() request line from socket, parse, read half headers, parse, etc) your data callbacks may be called more than once. Http-parser guarantees that data pointer is only valid for the lifetime of callback. You can also read() into a heap allocated buffer to avoid copying memory around if this fits your application.

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_*)
 ------------------------ ------------ --------------------------------------------
| State (prev. callback) | Callback   | Description/action                         |
 ------------------------ ------------ --------------------------------------------
| nothing (first call)   | on_h_field | Allocate new buffer and copy callback data |
|                        |            | into it                                    |
 ------------------------ ------------ --------------------------------------------
| value                  | on_h_field | New header started.                        |
|                        |            | Copy current name,value buffers to headers |
|                        |            | list and allocate new buffer for new name  |
 ------------------------ ------------ --------------------------------------------
| field                  | on_h_field | Previous name continues. Reallocate name   |
|                        |            | buffer and append callback data to it      |
 ------------------------ ------------ --------------------------------------------
| field                  | on_h_value | Value for current header started. Allocate |
|                        |            | new buffer and copy callback data to it    |
 ------------------------ ------------ --------------------------------------------
| value                  | on_h_value | Value continues. Reallocate value buffer   |
|                        |            | and append callback data to it             |
 ------------------------ ------------ --------------------------------------------

See examples of reading in headers:

  • partial example in C
  • from http-parser tests in C
  • from Node library in Javascript
Blog | Support | Training | Contact | API | Status | Twitter | Help | Security
© 2010 GitHub Inc. All rights reserved. | Terms of Service | Privacy Policy
Powered by the Dedicated Servers and
Cloud Computing of Rackspace Hosting®
Dedicated Server