joyent/node — Issue #2235: HTTPParser called with same buffer causes parsing error of the headers.
Author: joeferner
Created: 2011-11-30 19:54:15+00:00
State: open

I'm helping with the node_pcap project and I ran across a case where an HTTP request came in as two packets. Because of node_pcap's buffering strategy (ie reuse buffers when possible) I found a weird parsing error in the HTTPParser. If you call HTTPParser.execute using the same buffer with different data (1st packet then 2nd packet) the results are incorrect. Below is some sample code that illustrates the problem. I could fix this problem on the node_pcap side by making new buffers every time but this could severely impact performance. I tried looking through the code of the http parser in node but I'm not an expert in C.

Let me know if you think this is a problem with node or a problem with how we are using your API.

```javascript
var HTTPParser = process.binding('http_parser').HTTPParser;

parser = new HTTPParser(HTTPParser.REQUEST);
parser.onHeadersComplete = function(info) {
console.log("onHeadersComplete: ", info);
};
parser.onBody = function(buf, start, len) {
var str = buf.slice(start, len).toString();
console.log("onBody: ", str);
};
parser.onMessageComplete = function() {
console.log("onMessageComplete");
}

var b = new Buffer(1000);

var s = "POST / HTTP/1.1\r\nUser-Agent: Mozilla\r\n";
b.write(s, 0, s.length);
parser.execute(b, 0, s.length);

s = "Accept-Charset: utf-8, iso-8859-1, utf-16, *;q=0.7\r\nContent-Length: 11\r\n\r\n";
b.write(s, 0, s.length);
parser.execute(b, 0, s.length);

s = "Hello World";
b.write(s, 0, s.length);
parser.execute(b, 0, s.length);
```

I am running node v0.6.2.