Browse Source

snake to camel in HTTP

v0.7.4-release
Ryan Dahl 15 years ago
parent
commit
5c6f063ef2
  1. 97
      lib/http.js

97
lib/http.js

@ -159,11 +159,11 @@ var STATUS_CODES = exports.STATUS_CODES = {
505 : 'HTTP Version not supported'
};
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;
var connectionExpression = /Connection/i;
var transferEncodingExpression = /Transfer-Encoding/i;
var closeExpression = /close/i;
var chunkExpression = /chunk/i;
var contentLengthExpression = /Content-Length/i;
/* Abstract base class for ServerRequest and ClientResponse. */
@ -227,9 +227,9 @@ function OutgoingMessage (socket) {
this.outputEncodings = [];
this.closeOnFinish = false;
this.chunked_encoding = false;
this.should_keep_alive = true;
this.use_chunked_encoding_by_default = true;
this.chunkedEncoding = false;
this.shouldKeepAlive = true;
this.useChunkedEncodingByDefault = true;
this.flushing = false;
this.headWritten = false;
@ -263,14 +263,14 @@ OutgoingMessage.prototype._send = function (data, encoding) {
this.outputEncodings.push(encoding);
};
OutgoingMessage.prototype.sendHeaderLines = function (first_line, headers) {
var sent_connection_header = false;
var sent_content_length_header = false;
var sent_transfer_encoding_header = false;
OutgoingMessage.prototype.sendHeaderLines = function (firstLine, headers) {
var sentConnectionHeader = false;
var sentContentLengthHeader = false;
var sentTransferEncodingHeader = false;
// first_line in the case of request is: "GET /index.html HTTP/1.1\r\n"
// firstLine in the case of request is: "GET /index.html HTTP/1.1\r\n"
// in the case of response it is: "HTTP/1.1 200 OK\r\n"
var message_header = first_line;
var messageHeader = firstLine;
var field, value;
for (var i in headers) {
if (headers[i] instanceof Array) {
@ -282,46 +282,46 @@ OutgoingMessage.prototype.sendHeaderLines = function (first_line, headers) {
value = headers[i];
}
message_header += field + ": " + value + CRLF;
messageHeader += field + ": " + value + CRLF;
if (connection_expression.test(field)) {
sent_connection_header = true;
if (close_expression.test(value)) this.closeOnFinish = true;
if (connectionExpression.test(field)) {
sentConnectionHeader = true;
if (closeExpression.test(value)) this.closeOnFinish = true;
} else if (transfer_encoding_expression.test(field)) {
sent_transfer_encoding_header = true;
if (chunk_expression.test(value)) this.chunked_encoding = true;
} else if (transferEncodingExpression.test(field)) {
sentTransferEncodingHeader = true;
if (chunkExpression.test(value)) this.chunkedEncoding = true;
} else if (content_length_expression.test(field)) {
sent_content_length_header = true;
} else if (contentLengthExpression.test(field)) {
sentContentLengthHeader = true;
}
}
// keep-alive logic
if (sent_connection_header == false) {
if (this.should_keep_alive &&
(sent_content_length_header || this.use_chunked_encoding_by_default)) {
message_header += "Connection: keep-alive\r\n";
if (sentConnectionHeader == false) {
if (this.shouldKeepAlive &&
(sentContentLengthHeader || this.useChunkedEncodingByDefault)) {
messageHeader += "Connection: keep-alive\r\n";
} else {
this.closeOnFinish = true;
message_header += "Connection: close\r\n";
messageHeader += "Connection: close\r\n";
}
}
if (sent_content_length_header == false && sent_transfer_encoding_header == false) {
if (this.use_chunked_encoding_by_default) {
message_header += "Transfer-Encoding: chunked\r\n";
this.chunked_encoding = true;
if (sentContentLengthHeader == false && sentTransferEncodingHeader == false) {
if (this.useChunkedEncodingByDefault) {
messageHeader += "Transfer-Encoding: chunked\r\n";
this.chunkedEncoding = true;
}
else {
this.closeOnFinish = true;
}
}
message_header += CRLF;
messageHeader += CRLF;
this._send(message_header);
this._send(messageHeader);
// wait until the first body chunk, or close(), is sent to flush.
};
@ -339,7 +339,7 @@ OutgoingMessage.prototype.write = function (chunk, encoding) {
}
encoding = encoding || "ascii";
if (this.chunked_encoding) {
if (this.chunkedEncoding) {
if (typeof chunk == 'string') {
this._send(process._byteLength(chunk, encoding).toString(16));
} else {
@ -368,7 +368,7 @@ OutgoingMessage.prototype.finish = function () {
};
OutgoingMessage.prototype.close = function () {
if (this.chunked_encoding) this._send("0\r\n\r\n"); // last chunk
if (this.chunkedEncoding) this._send("0\r\n\r\n"); // last chunk
this.finished = true;
this.flush();
};
@ -378,8 +378,8 @@ function ServerResponse (req) {
OutgoingMessage.call(this, req.socket);
if (req.httpVersionMajor < 1 || req.httpVersionMinor < 1) {
this.use_chunked_encoding_by_default = false;
this.should_keep_alive = false;
this.useChunkedEncodingByDefault = false;
this.shouldKeepAlive = false;
}
}
sys.inherits(ServerResponse, OutgoingMessage);
@ -403,9 +403,9 @@ ServerResponse.prototype.writeHead = function (statusCode) {
headers = {};
}
var status_line = "HTTP/1.1 " + statusCode.toString() + " "
var statusLine = "HTTP/1.1 " + statusCode.toString() + " "
+ reasonPhrase + CRLF;
this.sendHeaderLines(status_line, headers);
this.sendHeaderLines(statusLine, headers);
this.headWritten = true;
};
@ -416,11 +416,11 @@ ServerResponse.prototype.writeHeader = ServerResponse.prototype.writeHead;
function ClientRequest (socket, method, url, headers) {
OutgoingMessage.call(this, socket);
this.should_keep_alive = false;
this.shouldKeepAlive = false;
if (method === "GET" || method === "HEAD") {
this.use_chunked_encoding_by_default = false;
this.useChunkedEncodingByDefault = false;
} else {
this.use_chunked_encoding_by_default = true;
this.useChunkedEncodingByDefault = true;
}
this.closeOnFinish = true;
@ -513,11 +513,10 @@ function connectionListener (socket) {
// The following callback is issued after the headers have been read on a
// new message. In this callback we setup the response object and pass it
// to the user.
parser.onIncoming = function (incoming, shouldKeepAlive) {
var req = incoming;
parser.onIncoming = function (req, shouldKeepAlive) {
var res = new ServerResponse(req);
res.should_keep_alive = shouldKeepAlive;
res.shouldKeepAlive = shouldKeepAlive;
res.addListener('flush', function () {
if (flushMessageQueue(socket, responses)) {
socket.close();
@ -581,9 +580,9 @@ function Client ( ) {
self.close();
});
self.addListener("close", function (had_error) {
if (had_error) {
self.emit("error");
self.addListener("close", function (e) {
if (e) {
self.emit("error", e);
return;
}

Loading…
Cancel
Save