-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Conversation
lgtm. this changes binary compatibility - but for Node I don't consider http_parser.h to be public - so it could be landed in v0.6 if desired. |
Can you sign the CLA? It's different from the Node CLA. |
What is this feature for? I read nodejs/node-v0.x-archive#2612 but I'm not entirely clear on how this would enable faster proxying. Is the idea that you want to know where the headers end so that you can slice off a Incidentally, this change adds 4 bytes to the |
Another way to go, which would enable |
@pgriess Nice of you to join us. It's not about faster proxying, it's about better proxying. The websocket proxying code in node-http-proxy is terrible because it has to be written on top of an The logic is simple: Adapted from here. We'll be updating this once this pull-request lands. var parsed = false;
var buffer = [];
socket.ondata = function (chunk) {
var ret = parser.execute(d, start, end - start);
if (parser.boundary && !parsed) {
parsed = true;
buffer.push(chunk.slice(0, parser.boundary);
heyUserModifyTheHeadersIfYouWant(parser._headers, function (_, newHeaders) {
//
// Serialize the headers back to the outgoing socket e.g.
// https://github.com/joyent/node/blob/master/lib/http.js#L471-562
//
ready = true;
//
// Flush any buffered chunks
//
for (var i = 0; i < buffer.length; i++) {
// Write to outgoing socket.
}
buffer.length = 0;
})
}
else if (parser.boundary && parsed && !ready) {
buffer.push(chunk);
}
else {
//
// Just write to the outgoing socket.
//
}
}; Oh, and (unrelated) users frequently bother me about why still use a vendored version of |
@bnoordhuis CLA signed. |
After speaking @pgriess I'm actually inclined to agree with him. I did not know that the parser.onBody would actually be called for each chunk of the body. I had assumed it would be called with the entirety of the body. With this in mind here's the new logic: var buffer = [];
var parsed = false;
var ready = false;
var HTTPParser = process.binding('http_parser').HTTPParser;
parser = new HTTPParser(HTTPParser.REQUEST);
parser._headers = [];
parser._url = '';
parser.socket = socket;
parser.onBody(function (b) {
if (ready) {
//
// If there is any buffer length write it out first
//
if (buffer.length) {
for (var i = 0; i < buffer.length; i++) {
// Write buffer to socket
}
buffer.length = 0;
}
//
// Write the current body chunk, b, out to the socket
//
return;
}
buffer.push(b);
});
socket.ondata = function (chunk) {
var ret = parser.execute(d, start, end - start);
if (parser._headers && !parsed) {
parsed = true;
heyUserModifyTheHeadersIfYouWant(parser._headers, function (_, newHeaders) {
//
// Serialize the headers back to the outgoing socket e.g.
// https://github.com/joyent/node/blob/master/lib/http.js#L471-562
//
ready = true;
});
}
}; I'm still +1 on this though. 4 bytes per request additional doesn't seem like killer overhead to me. |
The reason I'm still +1 on this is that the approach using |
A slight clarification: The callbacks that @indexzero is referring to are those that cross the JavaScript<->C++ boundary in Node (which are particularly slow relative to those that do not cross the boundary). I'm still -1 on this because fundamentally I think this is a Node issue (callbacks into JavaScript are expensive), not an http_parser issue. Providing an API that allowed the caller to inspect the raw byte stream is kind of counter to the ethos of this parser library, which as it stands now owns all framing and data manipulation decisions and delegates action by callbacks: it decides when to eat characters (spurious whitespace), join header values when they span lines, de-chunks body data, etc. Instead, I think we can solve this in the Node/http_parser integration layer by providing an |
So after digging around in In most cases (except for https://github.com/joyent/node/blob/master/lib/http.js#L1229-1271 This won't help parsing scenarios do any better than node.js core already does so might as well scrap it. :( |
Can this issue be closed if it's being scrapped? |
Yes, it turned out it's not necessary. |
Fixes nodejs/node-v0.x-archive#2612.