Change in output of HTTPParser.execute

73 views
Skip to first unread message

James Coglan

unread,
Nov 29, 2011, 11:31:38 AM11/29/11
to Node list
In working on my WebSocket client, I discovered this strange behaviour. Take this program, which runs on Node 0.4 through 0.6:

var data = 'HTTP/1.1 101 Switching Protocols\r\n' +
           'Upgrade: websocket\r\n' +
           'Connection: Upgrade\r\n' +
           'Sec-WebSocket-Accept: mpjqnyL7O0+du6j6anallhhkLKo=' +
           '\r\n\r\n'

var HTTPParser = process.binding('http_parser').HTTPParser,
    parser     = new HTTPParser(HTTPParser.RESPONSE || 'response')

var buffer   = new Buffer(data),
    consumed = parser.execute(buffer, 0, buffer.length)

console.log(buffer.length, consumed)

Some time during the 0.5.x series, the output of this program changed from "129 129" to "129 128". The parser under-reports the amount of data it consumed. The data may often include the beginning of a WebSocket frame so I need to split the data into the HTTP parts and the WebSocket frame, and so I need to version-sniff to make sure I select the right fragment of data to parse.

Has anyone else seen this? Am I using the new API incorrectly?

--
James Coglan
http://jcoglan.com
+44 (0) 7771512510

Ben Noordhuis

unread,
Nov 29, 2011, 1:30:32 PM11/29/11
to nod...@googlegroups.com

Your example prints "129 129" with v0.6.3 here.

James Coglan

unread,
Nov 29, 2011, 1:37:49 PM11/29/11
to nod...@googlegroups.com
On 29 November 2011 18:30, Ben Noordhuis <in...@bnoordhuis.nl> wrote:
Your example prints "129 129" with v0.6.3 here.

My mistake. It prints "129 128" on v0.4.12.

Ben Noordhuis

unread,
Nov 29, 2011, 1:48:11 PM11/29/11
to nod...@googlegroups.com

Just so we're on the same page, you're saying that v0.6.3 parses it
correctly and v0.4.12 is off by one byte, right?

James Coglan

unread,
Nov 29, 2011, 1:52:10 PM11/29/11
to nod...@googlegroups.com
On 29 November 2011 18:48, Ben Noordhuis <in...@bnoordhuis.nl> wrote:
Just so we're on the same page, you're saying that v0.6.3 parses it
correctly and v0.4.12 is off by one byte, right?

They both parse the headers correctly, and I get all the data I need. I'm trying to deal with the case where this comes over the wire in one Buffer:

var data = 'HTTP/1.1 101 Switching Protocols\r\n' +
           'Upgrade: websocket\r\n' +
           'Connection: Upgrade\r\n' +
           'Sec-WebSocket-Accept: mpjqnyL7O0+du6j6anallhhkLKo=' +
           '\r\n\r\n' +
           'some-websocket-data'

I need to know how much of the data was taken up with the HTTP headers so I can split off the rest of it to parse as WebSocket data. Having to version-sniff to figure out which HTTPParser implementation I'm using seems weird.

Not a giant problem, just wondering why it's happening.

James Coglan

unread,
Nov 29, 2011, 1:55:15 PM11/29/11
to nod...@googlegroups.com
On 29 November 2011 18:52, James Coglan <jco...@gmail.com> wrote:
var data = 'HTTP/1.1 101 Switching Protocols\r\n' +
           'Upgrade: websocket\r\n' +
           'Connection: Upgrade\r\n' +
           'Sec-WebSocket-Accept: mpjqnyL7O0+du6j6anallhhkLKo=' +
           '\r\n\r\n' +
           'some-websocket-data'

Koichi Kobayashi

unread,
Nov 29, 2011, 9:00:04 PM11/29/11
to nod...@googlegroups.com
Hi,

It has fixed since Node v0.5.4.

https://github.com/joyent/http-parser/commit/d4ca280af5287d6753099e1624727f4b530fe2cb

> --
> Job Board: http://jobs.nodejs.org/
> Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> You received this message because you are subscribed to the Google
> Groups "nodejs" group.
> To post to this group, send email to nod...@googlegroups.com
> To unsubscribe from this group, send email to
> nodejs+un...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/nodejs?hl=en?hl=en

Micheil Smith

unread,
Nov 30, 2011, 3:11:46 PM11/30/11
to nod...@googlegroups.com
Yeah, this is definitely the one off bug that either ryah or myself introduced at
some stage, it's definitely since been fixed.

– Micheil

Reply all
Reply to author
Forward
0 new messages