joyent/http-parser — Issue #97: Forcing a pause at the end of HTTP headers
Author: AndreLouisCaron
Created: 2012-02-25 22:41:10+00:00
State: open

I've implemented a [simple C++ wrapper](https://github.com/AndreLouisCaron/httpxx) for the `http-parser` library and ran into a problem when using the parser pause feature.

Basically, I have a `Request` object that wraps a `http_parser` instance. This request object has a `feed()` method which invokes `http_parser_execute()`. The request object also has a `headers_complete()` which reports the end of HTTP headers. To make this test reliable (mainly for proxying purposes), I force `http_parser_execute()` to return by pausing the parser calling `http_parser_pause()` from the `on_headers_complete()` callback. All this seemingly works, except that it doesn't consume the last byte of a request without a body. To finish processing the request, I have to call `http_parser_execute()` again with the last byte of data.

Now, calling `feed()`/`http_parser_execute()` one extra time is not that much of a problem, except that it makes it prevents clients from accurately finding the exact position of the end of HTTP headers. In a proxying scenario (HTTP proxy, CGI/SCGI/FastCGI or even WebSockets), where you want to forward the body byte-for-byte, clients end up prefixing the HTTP request body with an extra byte.

This was reported to me on the `httpxx` issue tracker, but I believe it's an issue in `http-parser`. Basically, the `on_headers_complete()` callback is called upon examining the last byte of the header data, and pausing in that callback prevents the library from marking the last byte (the one the triggered the callback) as consumed.

Visit the `httpx` issue #5 for a [detailed discussion](https://github.com/AndreLouisCaron/httpxx/issues/5). If it's unclear, I might be able to concoct an [SSCCE](http://sscce.org) that illustrates the problem.

Note: this issue seems related to [pull request 89](https://github.com/joyent/http-parser/pull/89).