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