From 1db59afb758fa3ad497334ce3f9422269f019d96 Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Mon, 29 Nov 2010 07:41:08 -0800 Subject: [PATCH] style --- lib/http.js | 129 ++++++++++++++++++++++++++++------------------------ 1 file changed, 70 insertions(+), 59 deletions(-) diff --git a/lib/http.js b/lib/http.js index b0e86f28333..7a24f1bbd30 100644 --- a/lib/http.js +++ b/lib/http.js @@ -1,4 +1,9 @@ var util = require('util'); +var net = require('net'); +var stream = require('stream'); +var FreeList = require('freelist').FreeList; +var HTTPParser = process.binding('http_parser').HTTPParser; + var debug; var debugLevel = parseInt(process.env.NODE_DEBUG, 16); @@ -8,11 +13,6 @@ if (debugLevel & 0x4) { debug = function () { }; } -var net = require('net'); -var stream = require('stream'); - -var FreeList = require('freelist').FreeList; -var HTTPParser = process.binding('http_parser').HTTPParser; var parsers = new FreeList('parsers', 1000, function () { var parser = new HTTPParser('request'); @@ -66,16 +66,14 @@ var parsers = new FreeList('parsers', 1000, function () { parser.incoming.httpVersionMajor = info.versionMajor; parser.incoming.httpVersionMinor = info.versionMinor; - parser.incoming.httpVersion = info.versionMajor - + '.' - + info.versionMinor ; + parser.incoming.httpVersion = info.versionMajor + '.' + info.versionMinor; if (info.method) { // server only parser.incoming.method = info.method; } else { // client only - parser.incoming.statusCode = info.statusCode; + parser.incoming.statusCode = info.statusCode; } parser.incoming.upgrade = info.upgrade; @@ -174,6 +172,7 @@ var STATUS_CODES = exports.STATUS_CODES = { 510 : 'Not Extended' // RFC 2774 }; + var connectionExpression = /Connection/i; var transferEncodingExpression = /Transfer-Encoding/i; var closeExpression = /close/i; @@ -208,6 +207,8 @@ function IncomingMessage (socket) { this.client = this.socket; } util.inherits(IncomingMessage, stream.Stream); + + exports.IncomingMessage = IncomingMessage; @@ -221,14 +222,17 @@ IncomingMessage.prototype.setEncoding = function (encoding) { this._decoder = new StringDecoder(encoding); }; + IncomingMessage.prototype.pause = function () { this.socket.pause(); }; + IncomingMessage.prototype.resume = function () { this.socket.resume(); }; + // Add the given (field, value) pair to the message // // Per RFC2616, section 4.2 it is acceptable to join multiple instances of the @@ -284,6 +288,7 @@ IncomingMessage.prototype._addHeaderLine = function (field, value) { } }; + function OutgoingMessage (socket) { stream.Stream.call(this); @@ -307,6 +312,8 @@ function OutgoingMessage (socket) { this.finished = false; } util.inherits(OutgoingMessage, stream.Stream); + + exports.OutgoingMessage = OutgoingMessage; @@ -332,6 +339,7 @@ OutgoingMessage.prototype._send = function (data, encoding) { return this._writeRaw(data, encoding); }; + OutgoingMessage.prototype._writeRaw = function(data, encoding) { if (this.connection._outgoing[0] === this && this.connection.writable) { // There might be pending data in the this.output buffer. @@ -353,6 +361,7 @@ OutgoingMessage.prototype._writeRaw = function(data, encoding) { } }; + OutgoingMessage.prototype._buffer = function (data, encoding) { if (data.length === 0) return; @@ -469,13 +478,13 @@ OutgoingMessage.prototype._storeHeader = function (firstLine, headers) { this._header = messageHeader + CRLF; this._headerSent = false; + // wait until the first body chunk, or close(), is sent to flush, // UNLESS we're sending Expect: 100-continue. - if (sentExpect) { - this._send(""); - } + if (sentExpect) this._send(""); }; + OutgoingMessage.prototype.write = function (chunk, encoding) { if (!this._header) { throw new Error("You have to call writeHead() before write()"); @@ -486,9 +495,8 @@ OutgoingMessage.prototype.write = function (chunk, encoding) { return true; } - if (typeof chunk !== "string" - && !Buffer.isBuffer(chunk) - && !Array.isArray(chunk)) { + if (typeof chunk !== "string" && !Buffer.isBuffer(chunk) && + !Array.isArray(chunk)) { throw new TypeError("first argument must be a string, Array, or Buffer"); } @@ -539,13 +547,12 @@ OutgoingMessage.prototype.addTrailers = function (headers) { OutgoingMessage.prototype.end = function (data, encoding) { var ret; - var hot = this._headerSent === false - && typeof(data) === "string" - && data.length > 0 - && this.output.length === 0 - && this.connection.writable - && this.connection._outgoing[0] === this - ; + var hot = this._headerSent === false && + typeof(data) === "string" && + data.length > 0 && + this.output.length === 0 && + this.connection.writable && + this.connection._outgoing[0] === this; if (hot) { // Hot path. They're doing @@ -554,15 +561,9 @@ OutgoingMessage.prototype.end = function (data, encoding) { // HACKY. if (this.chunkedEncoding) { var l = Buffer.byteLength(data, encoding).toString(16); - ret = this.connection.write( this._header - + l - + CRLF - + data - + "\r\n0\r\n" - + this._trailer - + "\r\n" - , encoding - ); + ret = this.connection.write(this._header + l + CRLF + + data + "\r\n0\r\n" + + this._trailer + "\r\n", encoding); } else { ret = this.connection.write(this._header + data, encoding); } @@ -606,13 +607,17 @@ function ServerResponse (req) { } } util.inherits(ServerResponse, OutgoingMessage); + + exports.ServerResponse = ServerResponse; + ServerResponse.prototype.writeContinue = function () { - this._writeRaw("HTTP/1.1 100 Continue" + CRLF + CRLF, 'ascii'); - this._sent100 = true; + this._writeRaw("HTTP/1.1 100 Continue" + CRLF + CRLF, 'ascii'); + this._sent100 = true; }; + ServerResponse.prototype.writeHead = function (statusCode) { var reasonPhrase, headers, headerIndex; @@ -630,13 +635,11 @@ ServerResponse.prototype.writeHead = function (statusCode) { headers = {}; } - var statusLine = "HTTP/1.1 " + statusCode.toString() + " " - + reasonPhrase + CRLF; + var statusLine = "HTTP/1.1 " + statusCode.toString() + " " + + reasonPhrase + CRLF; - if ( statusCode === 204 - || statusCode === 304 - || (statusCode >= 100 && statusCode <= 199) - ) { + if (statusCode === 204 || statusCode === 304 || + (100 <= statusCode && statusCode <= 199)) { // RFC 2616, 10.2.5: // The 204 response MUST NOT include a message-body, and thus is always // terminated by the first empty line after the header fields. @@ -653,7 +656,7 @@ ServerResponse.prototype.writeHead = function (statusCode) { // don't keep alive connections where the client expects 100 Continue // but we sent a final status; they may put extra bytes on the wire. if (this._expect_continue && ! this._sent100) { - this.shouldKeepAlive = false; + this.shouldKeepAlive = false; } this._storeHeader(statusLine, headers); @@ -664,6 +667,7 @@ ServerResponse.prototype.writeHeader = function () { this.writeHead.apply(this, arguments); }; + function ClientRequest (socket, method, url, headers) { OutgoingMessage.call(this, socket); @@ -679,8 +683,11 @@ function ClientRequest (socket, method, url, headers) { this._storeHeader(method + " " + url + " HTTP/1.1\r\n", headers); } util.inherits(ClientRequest, OutgoingMessage); + + exports.ClientRequest = ClientRequest; + function outgoingFlush (socket) { // This logic is probably a bit confusing. Let me explain a bit: // @@ -691,17 +698,16 @@ function outgoingFlush (socket) { // req1 = client.request('GET', '/'); // req2 = client.request('POST', '/'); // - // The question is what happens when the user does + // When the user does // // req2.write("hello world\n"); // - // It's possible that the first request has not been completely flushed to + // it's possible that the first request has not been completely flushed to // the socket yet. Thus the outgoing messages need to be prepared to queue // up data internally before sending it on further to the socket's queue. // - // This function, outgoingFlush(), is called by both the Server - // implementation and the Client implementation to attempt to flush any - // pending messages out to the socket. + // This function, outgoingFlush(), is called by both the Server and Client + // to attempt to flush any pending messages out to the socket. var message = socket._outgoing[0]; if (!message) return; @@ -740,7 +746,6 @@ function httpSocketSetup (socket) { } - function Server (requestListener) { if (!(this instanceof Server)) return new Server(requestListener); net.Server.call(this, { allowHalfOpen: true }); @@ -753,12 +758,15 @@ function Server (requestListener) { } util.inherits(Server, net.Server); + exports.Server = Server; + exports.createServer = function (requestListener) { return new Server(requestListener); }; + function connectionListener (socket) { var self = this; @@ -848,18 +856,18 @@ function connectionListener (socket) { res.shouldKeepAlive = shouldKeepAlive; socket._outgoing.push(res); - if ('expect' in req.headers - && (req.httpVersionMajor == 1 && req.httpVersionMinor == 1) - && continueExpression.test(req.headers['expect'])) { - res._expect_continue = true; - if (self.listeners("checkContinue").length) { - self.emit("checkContinue", req, res); - } else { - res.writeContinue(); - self.emit('request', req, res); - } - } else { + if ('expect' in req.headers && + (req.httpVersionMajor == 1 && req.httpVersionMinor == 1) && + continueExpression.test(req.headers['expect'])) { + res._expect_continue = true; + if (self.listeners("checkContinue").length) { + self.emit("checkContinue", req, res); + } else { + res.writeContinue(); self.emit('request', req, res); + } + } else { + self.emit('request', req, res); } return false; // Not a HEAD response. (Not even a response!) }; @@ -933,8 +941,10 @@ function Client ( ) { }; util.inherits(Client, net.Stream); + exports.Client = Client; + exports.createClient = function (port, host, https, credentials) { var c = new Client(); c.port = port; @@ -959,13 +969,13 @@ Client.prototype._initParser = function () { // A major oversight in HTTP. Hence this nastiness. var isHeadResponse = req.method == "HEAD"; debug('isHeadResponse ' + isHeadResponse); - + if (res.statusCode == 100) { // restart the parser, as this is a continue message. req.emit("continue"); return true; } - + if (req.shouldKeepAlive && res.headers.connection === 'close') { req.shouldKeepAlive = false; } @@ -1023,6 +1033,7 @@ Client.prototype._reconnect = function () { } }; + Client.prototype.request = function (method, url, headers) { if (typeof(url) != "string") { // assume method was omitted, shift arguments