Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
Normalize HTTP headers.
Browse files Browse the repository at this point in the history
"Content-Length" becomes "content-length".
  • Loading branch information
ry committed Oct 7, 2009
1 parent d03b676 commit f623fd7
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
9 changes: 3 additions & 6 deletions lib/http.js
Expand Up @@ -109,11 +109,8 @@ exports.parseUri.options = {
};


var connection_expression = /Connection/i;
var transfer_encoding_expression = /Transfer-Encoding/i;
var close_expression = /close/i;
var chunk_expression = /chunk/i;
var content_length_expression = /Content-Length/i;


/* Abstract base class for ServerRequest and ClientResponse. */
Expand Down Expand Up @@ -245,15 +242,15 @@ OutgoingMessage.prototype.sendHeaderLines = function (first_line, headers) {

message_header += field + ": " + value + CRLF;

if (connection_expression.exec(field)) {
if ("connection" === field) {
sent_connection_header = true;
if (close_expression.exec(value)) this.closeOnFinish = true;

} else if (transfer_encoding_expression.exec(field)) {
} else if ("transfer-encoding" === field) {
sent_transfer_encoding_header = true;
if (chunk_expression.exec(value)) this.chunked_encoding = true;

} else if (content_length_expression.exec(field)) {
} else if ("content-length" === field) {
sent_content_length_header = true;

}
Expand Down
17 changes: 17 additions & 0 deletions src/http.cc
Expand Up @@ -128,12 +128,28 @@ HTTPConnection::on_fragment (http_parser *parser, const char *buf, size_t len)
return 0;
}

const static char normalizer[] =
"\0------------------------------"
"-----------------0123456789-----"
"--abcdefghijklmnopqrstuvwxyz----"
"--abcdefghijklmnopqrstuvwxyz----"
"--------------------------------"
"--------------------------------"
"--------------------------------"
"--------------------------------";

int
HTTPConnection::on_header_field (http_parser *parser, const char *buf, size_t len)
{
HandleScope scope;
HTTPConnection *connection = static_cast<HTTPConnection*>(parser->data);
assert(connection->attached_);

// NORMALIZE
size_t i;
char *nonconstbuf = (char*)buf; // FIXME
for (i = 0; i < len; i++) { nonconstbuf[i] = normalizer[buf[i]]; }

Local<Value> argv[1] = { String::New(buf, len) };
connection->Emit("headerField", 1, argv);
return 0;
Expand All @@ -145,6 +161,7 @@ HTTPConnection::on_header_value (http_parser *parser, const char *buf, size_t le
HandleScope scope;
HTTPConnection *connection = static_cast<HTTPConnection*>(parser->data);
assert(connection->attached_);

Local<Value> argv[1] = { String::New(buf, len) };
connection->Emit("headerValue", 1, argv);
return 0;
Expand Down
8 changes: 4 additions & 4 deletions test/mjsunit/test-http.js
Expand Up @@ -13,11 +13,11 @@ http.createServer(function (req, res) {
assertEquals("/hello", req.uri.path);

p(req.headers);
assertTrue("Accept" in req.headers);
assertEquals("*/*", req.headers["Accept"]);
assertTrue("accept" in req.headers);
assertEquals("*/*", req.headers["accept"]);

assertTrue("Foo" in req.headers);
assertEquals("bar", req.headers["Foo"]);
assertTrue("foo" in req.headers);
assertEquals("bar", req.headers["foo"]);
}

if (responses_sent == 1) {
Expand Down

0 comments on commit f623fd7

Please sign in to comment.