Need help with http-parser?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

5.9K Stars 1.5K Forks MIT License 453 Commits 78 Opened issues


http request/response parser for c

Services available


Need anything else?

Contributors list

HTTP Parser

http-parser is not actively maintained. New projects and projects looking to migrate should consider llhttp.

Build Status

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 syscalls nor allocations, it does not buffer data, it can be interrupted at anytime. Depending on your architecture, it only requires about 40 bytes of data per message stream (in a web server that is per connection).


  • No dependencies
  • Handles persistent streams (keep-alive).
  • Decodes chunked encoding.
  • Upgrade support
  • Defends against buffer overflow attacks.

The parser extracts the following information from HTTP messages:

  • Header fields and values
  • Content-Length
  • Request method
  • Response status code
  • Transfer-Encoding
  • HTTP version
  • Request URL
  • Message body



object is used per TCP connection. Initialize the struct using
and set the callbacks. That might look something like this for a request parser: ```c httpparsersettings settings; settings.onurl = myurlcallback; settings.onheaderfield = myheaderfieldcallback; /* ... */

httpparser *parser = malloc(sizeof(httpparser)); httpparserinit(parser, HTTPREQUEST); parser->data = mysocket; ```

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 recved==0 to signal that EOF has been received.
  • / nparsed = http_parser_execute(parser, &settings, buf, recved);

if (parser->upgrade) { /* handle new protocol / } else if (nparsed != recved) { / Handle error. Usually just close the connection. */ }

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
about EOF, give
as the fourth parameter to
. 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

, and the HTTP version are stored in the parser structure. This data is only temporally stored in
and gets reset on each new message. If this information is needed later, copy it out of the structure during the

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.

The Special Problem of Upgrade

supports upgrading the connection to a different protocol. An increasingly common example of this is the WebSocket protocol which sends a request like
    GET /demo HTTP/1.1
    Upgrade: WebSocket
    Connection: Upgrade
    WebSocket-Protocol: sample

followed by non-HTTP data.

(See RFC6455 for more information the WebSocket protocol.)

To support this, the parser will treat this as a normal HTTP message without a body, issuing both onheaderscomplete and onmessagecomplete callbacks. However httpparserexecute() will stop parsing at the end of the headers and return.

The user is expected to check if

has been set to 1 after
returns. Non-HTTP data begins at the buffer supplied offset by the return value of


During the

call, the callbacks set in
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: onmessagebegin, onheaderscomplete, onmessagecomplete.
  • data
    typedef int (*http_data_cb) (http_parser*, const char *at, size_t length);
    Callbacks: (requests only) onurl, (common) onheaderfield, onheadervalue, onbody;

Callbacks must return 0 on success. Returning a non-zero value indicates error to the parser, making it exit immediately.

For cases where it is necessary to pass local information to/from a callback, the

field can be used. An example of such a case is when using threads to handle a socket connection, parse a request, and then give a response over that socket. By instantiation of a thread-local struct containing relevant data (e.g. accepted socket, allocated memory for callbacks to write into, etc), a parser's callbacks are able to communicate data between the scope of the thread and the scope of the callback in a threadsafe manner. This allows
to be used in multi-threaded contexts.

Example: ```c typedef struct { sockett sock; void* buffer; int buflen; } customdatat;

int myurlcallback(httpparser* parser, const char *at, sizet length) { /* access to thread local customdatat struct. Use this access save parsed data for later use into thread local buffer, or communicate over socket */ parser->data; ... return 0; }


void httpparserthread(sockett sock) { int nparsed = 0; /* allocate memory for user data */ customdatat *mydata = malloc(sizeof(customdatat));

/* some information for use by callbacks. * achieves thread -> callback information flow */ my_data->sock = sock;

/* instantiate a thread-local parser / httpparser *parser = malloc(sizeof(httpparser)); httpparserinit(parser, HTTP_REQUEST); / initialise parser / / this custom data reference is accessible through the reference to the parser supplied to callback functions */ parser->data = my_data;

httpparsersettings settings; /* set up callbacks */ settings.onurl = myurl_callback;

/* execute parser */ nparsed = httpparserexecute(parser, &settings, buf, recved);

... /* parsed information copied from callback. can now perform action on data copied into thread-local memory from callbacks. achieves callback -> thread information flow */ my_data->buffer; ... }

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 the 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             |
 ------------------------ ------------ --------------------------------------------

Parsing URLs

A simplistic zero-copy URL parser is provided as http_parser_parse_url(). Users of this library may wish to use it to parse URLs constructed from consecutive on_url callbacks.

See examples of reading in headers:

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.