I've been working on implementing a solid HTTP library in Rust, currently at <a href="http://github.com/chris-morgan/rust-http" rel="nofollow">http://github.com/chris-morgan/rust-http</a>. Servo has been using Joyent's HTTP parser and this is a problem that I had observed with it. Yes, it is pretty badly implemented, but it's not a mess because of the style of code—that's for performance. It's only a mess because it's inconsistent and incorrect.<p>Reading the HTTP method in a high-performance way does lead to superficially ugly code. That's why code generation is good. Ragel has been mentioned and I intend to seriously consider using it, but for the moment my own HTTP method reading code is generated with:<p><pre><code> generate_branchified_method(
writer,
branchify!(case sensitive,
"CONNECT" => Connect,
"DELETE" => Delete,
"GET" => Get,
"HEAD" => Head,
"OPTIONS" => Options,
"PATCH" => Patch,
"POST" => Post,
"PUT" => Put,
"TRACE" => Trace
),
1,
"self.stream.read_byte()",
"SP",
"MAX_METHOD_LEN",
"is_token_item(b)",
"ExtensionMethod(%s)");
</code></pre>
This is pleasantly easy to read and meaningful.<p>This generates the high performance artwork shown at <a href="http://sprunge.us/HdTH" rel="nofollow">http://sprunge.us/HdTH</a>, which supports extension methods correctly. (Rust's algebraic data types are marvellous for many things in implementing such a spec.)
The most interesting part (to me) is that the server will handle e.g. "PUN" as though it actually said "PUT". I wonder if this could be used as an attack vector?<p>Sounds a lot like a "confused deputy" situation: imagine that your L7 firewall has a rule to reject any PUT request, but it sees PUN and thus allows the request to pass through to node.js, which then treats it as though it were actually PUT.
The level of bikeshedded micro-optimization going on in that file is hilarious. The whole thing could be swapped out with a Ragel parser and nobody would notice a thing
LINK was added <i>after</i> HTTP/1.0, and <i>removed</i> before HTTP/1.1. <a href="http://tools.ietf.org/html/draft-ietf-httpbis-method-registrations-12" rel="nofollow">http://tools.ietf.org/html/draft-ietf-httpbis-method-registr...</a> is being cited, but that is still a draft and the registry that refers to does not yet exist. I believe it is thus fair to say that LINK is not a standard method?
To people saying this is all micro optimization - have you guys actually measured? If so, please post the numbers.<p>It's presumptuous to simply say 'all this is unnecessary' unless you have measured it and we have no reason to believe the author hasn't measured it.<p>BTW, the file is copyright nginx.
A few points to make:<p>- I value software that keeps to the spec, because it's the spec that I (as a dev or non-dev) refer to. You never hear "NodeJS has HTTPish module", nor do you read documentation of that module's concepts and behaviour. Those are defined in the spec, and the __fill_in_with_any_language__ HTTP module just implements those definitions.<p>- Optimizations, simplifications, corrections should be done in the spec, whenever the you find them at implementation-time.<p>But until now there has not been <i>ONE</i> HTTP server that grasps and handles the HTTP specs in their whole. So then, I find it hilarious to read that about optimizations when neither of us have the whole picture.<p>That said, I don't think it's Node.js to blame here (albeit they do have weird views of standards: <a href="https://github.com/joyent/node/issues/4850" rel="nofollow">https://github.com/joyent/node/issues/4850</a>) but HTTP itself because the spec's abstraction levels have been far away from the implementations' reach. HTTPs concepts are gorgeous but they are worth nil if implementation is "hard" and never done properly.<p>Longer story at: <a href="http://andreineculau.github.io/hyperrest/2013-06-10-http-hell-no/" rel="nofollow">http://andreineculau.github.io/hyperrest/2013-06-10-http-hel...</a>
There are plenty of good ways to optimize this code. They didn't pick any of them. What I find more surprising than anything is that they didn't just optimize GET and handle the rest generically.
It should probably be noted that:
a) you don't have use node's built in HTTP server (yeah, I know, nearly everyone will), you are more thn free to write your own or use one from it's module repository (npm)
b) The entire HTTP module is currently being over-hauled in the 0.11.X development branch and the changes should appear in the stable 0.12.x releases.<p>Out of interest has anyone seen what other web servers support for these more 'esoteric' verbs is like?
Nice catch, reading the nodejs code is really discouraging. But the post points to the relevant specs, so I guess this should be fixed rather sooner than later. Initial response looks good: <a href="https://github.com/joyent/node/issues/6078" rel="nofollow">https://github.com/joyent/node/issues/6078</a>
Not a big deal. Even the REST people have long been just sending normal GET and POST and including some indicator that they really want it treated as some other verb.
No one remembered the conversations between Ryan Dahl and Zed Shaw on the http parser two years ago?<p>[1] <a href="https://news.ycombinator.com/item?id=2549403" rel="nofollow">https://news.ycombinator.com/item?id=2549403</a><p>[2] <a href="https://twitter.com/zedshaw/status/15714602817" rel="nofollow">https://twitter.com/zedshaw/status/15714602817</a>
Yet another reason to remove verbs from the HTTP spec: <a href="http://www.onebigfluke.com/2013/08/lets-remove-verbs-from-http-20.html" rel="nofollow">http://www.onebigfluke.com/2013/08/lets-remove-verbs-from-ht...</a>
Hm, didn't nginx do something similarly "ugly" for a while? Like inspecting the (completely making it up here) second character to see if it's 'E'?<p>A quick look now suggests they are using a parser generator now.
I've seen this kind of thing before. Someone falls in love with their optimization ("we can parse GET with a single int switch!") and when it is pointed out that it doesn't handle the spec correctly, and that handling the spec correctly would make this as slow as the naive way, it still ends up in the code because "it's good enough."