This repository has been archived by the owner. It is now read-only.

HTTPParser called with same buffer causes parsing error of the headers. #2235

Closed
joeferner opened this Issue Nov 30, 2011 · 2 comments

Comments

Projects
None yet
2 participants

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.

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

Member

bnoordhuis commented Dec 1, 2011

That's expected behaviour. The new HTTP parser tries very hard to copy as little data as possible. Don't reuse the buffer until the request is finished.

@bnoordhuis bnoordhuis closed this Dec 1, 2011

That's sorta what I figured.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.