Browse Source

snake to camel in HTTP

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

99
lib/http.js

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

Loading…
Cancel
Save