|
@ -107,7 +107,7 @@ var parsers = new FreeList('parsers', 1000, function () { |
|
|
} |
|
|
} |
|
|
if (!parser.incoming.upgrade) { |
|
|
if (!parser.incoming.upgrade) { |
|
|
// For upgraded connections, also emit this after parser.execute
|
|
|
// For upgraded connections, also emit this after parser.execute
|
|
|
parser.incoming.emit("end"); |
|
|
parser.incoming.emit('end'); |
|
|
} |
|
|
} |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
@ -116,7 +116,7 @@ var parsers = new FreeList('parsers', 1000, function () { |
|
|
exports.parsers = parsers; |
|
|
exports.parsers = parsers; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var CRLF = "\r\n"; |
|
|
var CRLF = '\r\n'; |
|
|
var STATUS_CODES = exports.STATUS_CODES = { |
|
|
var STATUS_CODES = exports.STATUS_CODES = { |
|
|
100 : 'Continue', |
|
|
100 : 'Continue', |
|
|
101 : 'Switching Protocols', |
|
|
101 : 'Switching Protocols', |
|
@ -198,7 +198,7 @@ function IncomingMessage (socket) { |
|
|
this.readable = true; |
|
|
this.readable = true; |
|
|
|
|
|
|
|
|
// request (server) only
|
|
|
// request (server) only
|
|
|
this.url = ""; |
|
|
this.url = ''; |
|
|
|
|
|
|
|
|
this.method = null; |
|
|
this.method = null; |
|
|
|
|
|
|
|
@ -218,7 +218,7 @@ IncomingMessage.prototype.destroy = function (error) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
IncomingMessage.prototype.setEncoding = function(encoding) { |
|
|
IncomingMessage.prototype.setEncoding = function(encoding) { |
|
|
var StringDecoder = require("string_decoder").StringDecoder; // lazy load
|
|
|
var StringDecoder = require('string_decoder').StringDecoder; // lazy load
|
|
|
this._decoder = new StringDecoder(encoding); |
|
|
this._decoder = new StringDecoder(encoding); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
@ -241,12 +241,8 @@ IncomingMessage.prototype.resume = function () { |
|
|
// and drop the second. Extended header fields (those beginning with 'x-') are
|
|
|
// and drop the second. Extended header fields (those beginning with 'x-') are
|
|
|
// always joined.
|
|
|
// always joined.
|
|
|
IncomingMessage.prototype._addHeaderLine = function(field, value) { |
|
|
IncomingMessage.prototype._addHeaderLine = function(field, value) { |
|
|
var dest; |
|
|
var dest = this.complete ? this.trailers : this.headers; |
|
|
if (this.complete) { |
|
|
|
|
|
dest = this.trailers; |
|
|
|
|
|
} else { |
|
|
|
|
|
dest = this.headers; |
|
|
|
|
|
} |
|
|
|
|
|
switch (field) { |
|
|
switch (field) { |
|
|
// Array headers:
|
|
|
// Array headers:
|
|
|
case 'set-cookie': |
|
|
case 'set-cookie': |
|
@ -369,7 +365,7 @@ OutgoingMessage.prototype._buffer = function (data, encoding) { |
|
|
|
|
|
|
|
|
if (length === 0 || typeof data != 'string') { |
|
|
if (length === 0 || typeof data != 'string') { |
|
|
this.output.push(data); |
|
|
this.output.push(data); |
|
|
encoding = encoding || "ascii"; |
|
|
encoding = encoding || 'ascii'; |
|
|
this.outputEncodings.push(encoding); |
|
|
this.outputEncodings.push(encoding); |
|
|
return false; |
|
|
return false; |
|
|
} |
|
|
} |
|
@ -384,7 +380,7 @@ OutgoingMessage.prototype._buffer = function (data, encoding) { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
this.output.push(data); |
|
|
this.output.push(data); |
|
|
encoding = encoding || "ascii"; |
|
|
encoding = encoding || 'ascii'; |
|
|
this.outputEncodings.push(encoding); |
|
|
this.outputEncodings.push(encoding); |
|
|
|
|
|
|
|
|
return false; |
|
|
return false; |
|
@ -397,14 +393,14 @@ OutgoingMessage.prototype._storeHeader = function (firstLine, headers) { |
|
|
var sentTransferEncodingHeader = false; |
|
|
var sentTransferEncodingHeader = false; |
|
|
var sentExpect = false; |
|
|
var sentExpect = false; |
|
|
|
|
|
|
|
|
// firstLine 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 messageHeader = firstLine; |
|
|
var messageHeader = firstLine; |
|
|
var field, value; |
|
|
var field, value; |
|
|
var self = this; |
|
|
var self = this; |
|
|
|
|
|
|
|
|
function store(field, value) { |
|
|
function store(field, value) { |
|
|
messageHeader += field + ": " + value + CRLF; |
|
|
messageHeader += field + ': ' + value + CRLF; |
|
|
|
|
|
|
|
|
if (connectionExpression.test(field)) { |
|
|
if (connectionExpression.test(field)) { |
|
|
sentConnectionHeader = true; |
|
|
sentConnectionHeader = true; |
|
@ -455,17 +451,17 @@ OutgoingMessage.prototype._storeHeader = function (firstLine, headers) { |
|
|
if (sentConnectionHeader == false) { |
|
|
if (sentConnectionHeader == false) { |
|
|
if (this.shouldKeepAlive && |
|
|
if (this.shouldKeepAlive && |
|
|
(sentContentLengthHeader || this.useChunkedEncodingByDefault)) { |
|
|
(sentContentLengthHeader || this.useChunkedEncodingByDefault)) { |
|
|
messageHeader += "Connection: keep-alive\r\n"; |
|
|
messageHeader += 'Connection: keep-alive\r\n'; |
|
|
} else { |
|
|
} else { |
|
|
this._last = true; |
|
|
this._last = true; |
|
|
messageHeader += "Connection: close\r\n"; |
|
|
messageHeader += 'Connection: close\r\n'; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (sentContentLengthHeader == false && sentTransferEncodingHeader == false) { |
|
|
if (sentContentLengthHeader == false && sentTransferEncodingHeader == false) { |
|
|
if (this._hasBody) { |
|
|
if (this._hasBody) { |
|
|
if (this.useChunkedEncodingByDefault) { |
|
|
if (this.useChunkedEncodingByDefault) { |
|
|
messageHeader += "Transfer-Encoding: chunked\r\n"; |
|
|
messageHeader += 'Transfer-Encoding: chunked\r\n'; |
|
|
this.chunkedEncoding = true; |
|
|
this.chunkedEncoding = true; |
|
|
} else { |
|
|
} else { |
|
|
this._last = true; |
|
|
this._last = true; |
|
@ -481,23 +477,24 @@ OutgoingMessage.prototype._storeHeader = function (firstLine, headers) { |
|
|
|
|
|
|
|
|
// wait until the first body chunk, or close(), is sent to flush,
|
|
|
// wait until the first body chunk, or close(), is sent to flush,
|
|
|
// UNLESS we're sending Expect: 100-continue.
|
|
|
// UNLESS we're sending Expect: 100-continue.
|
|
|
if (sentExpect) this._send(""); |
|
|
if (sentExpect) this._send(''); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
OutgoingMessage.prototype.write = function(chunk, encoding) { |
|
|
OutgoingMessage.prototype.write = function(chunk, encoding) { |
|
|
if (!this._header) { |
|
|
if (!this._header) { |
|
|
throw new Error("You have to call writeHead() before write()"); |
|
|
throw new Error('You have to call writeHead() before write()'); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (!this._hasBody) { |
|
|
if (!this._hasBody) { |
|
|
console.error("This type of response MUST NOT have a body. Ignoring write() calls."); |
|
|
console.error('This type of response MUST NOT have a body. ' + |
|
|
|
|
|
'Ignoring write() calls.'); |
|
|
return true; |
|
|
return true; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (typeof chunk !== "string" && !Buffer.isBuffer(chunk) && |
|
|
if (typeof chunk !== 'string' && !Buffer.isBuffer(chunk) && |
|
|
!Array.isArray(chunk)) { |
|
|
!Array.isArray(chunk)) { |
|
|
throw new TypeError("first argument must be a string, Array, or Buffer"); |
|
|
throw new TypeError('first argument must be a string, Array, or Buffer'); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (chunk.length === 0) return false; |
|
|
if (chunk.length === 0) return false; |
|
@ -526,7 +523,7 @@ OutgoingMessage.prototype.write = function (chunk, encoding) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
OutgoingMessage.prototype.addTrailers = function(headers) { |
|
|
OutgoingMessage.prototype.addTrailers = function(headers) { |
|
|
this._trailer = ""; |
|
|
this._trailer = ''; |
|
|
var keys = Object.keys(headers); |
|
|
var keys = Object.keys(headers); |
|
|
var isArray = (Array.isArray(headers)); |
|
|
var isArray = (Array.isArray(headers)); |
|
|
for (var i = 0, l = keys.length; i < l; i++) { |
|
|
for (var i = 0, l = keys.length; i < l; i++) { |
|
@ -539,7 +536,7 @@ OutgoingMessage.prototype.addTrailers = function (headers) { |
|
|
value = headers[key]; |
|
|
value = headers[key]; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
this._trailer += field + ": " + value + CRLF; |
|
|
this._trailer += field + ': ' + value + CRLF; |
|
|
} |
|
|
} |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
@ -548,7 +545,7 @@ OutgoingMessage.prototype.end = function (data, encoding) { |
|
|
var ret; |
|
|
var ret; |
|
|
|
|
|
|
|
|
var hot = this._headerSent === false && |
|
|
var hot = this._headerSent === false && |
|
|
typeof(data) === "string" && |
|
|
typeof(data) === 'string' && |
|
|
data.length > 0 && |
|
|
data.length > 0 && |
|
|
this.output.length === 0 && |
|
|
this.output.length === 0 && |
|
|
this.connection.writable && |
|
|
this.connection.writable && |
|
@ -562,8 +559,8 @@ OutgoingMessage.prototype.end = function (data, encoding) { |
|
|
if (this.chunkedEncoding) { |
|
|
if (this.chunkedEncoding) { |
|
|
var l = Buffer.byteLength(data, encoding).toString(16); |
|
|
var l = Buffer.byteLength(data, encoding).toString(16); |
|
|
ret = this.connection.write(this._header + l + CRLF + |
|
|
ret = this.connection.write(this._header + l + CRLF + |
|
|
data + "\r\n0\r\n" + |
|
|
data + '\r\n0\r\n' + |
|
|
this._trailer + "\r\n", encoding); |
|
|
this._trailer + '\r\n', encoding); |
|
|
} else { |
|
|
} else { |
|
|
ret = this.connection.write(this._header + data, encoding); |
|
|
ret = this.connection.write(this._header + data, encoding); |
|
|
} |
|
|
} |
|
@ -613,7 +610,7 @@ exports.ServerResponse = ServerResponse; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ServerResponse.prototype.writeContinue = function() { |
|
|
ServerResponse.prototype.writeContinue = function() { |
|
|
this._writeRaw("HTTP/1.1 100 Continue" + CRLF + CRLF, 'ascii'); |
|
|
this._writeRaw('HTTP/1.1 100 Continue' + CRLF + CRLF, 'ascii'); |
|
|
this._sent100 = true; |
|
|
this._sent100 = true; |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
@ -625,7 +622,7 @@ ServerResponse.prototype.writeHead = function (statusCode) { |
|
|
reasonPhrase = arguments[1]; |
|
|
reasonPhrase = arguments[1]; |
|
|
headerIndex = 2; |
|
|
headerIndex = 2; |
|
|
} else { |
|
|
} else { |
|
|
reasonPhrase = STATUS_CODES[statusCode] || "unknown"; |
|
|
reasonPhrase = STATUS_CODES[statusCode] || 'unknown'; |
|
|
headerIndex = 1; |
|
|
headerIndex = 1; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -635,7 +632,7 @@ ServerResponse.prototype.writeHead = function (statusCode) { |
|
|
headers = {}; |
|
|
headers = {}; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
var statusLine = "HTTP/1.1 " + statusCode.toString() + " " + |
|
|
var statusLine = 'HTTP/1.1 ' + statusCode.toString() + ' ' + |
|
|
reasonPhrase + CRLF; |
|
|
reasonPhrase + CRLF; |
|
|
|
|
|
|
|
|
if (statusCode === 204 || statusCode === 304 || |
|
|
if (statusCode === 204 || statusCode === 304 || |
|
@ -673,14 +670,14 @@ function ClientRequest (socket, method, url, headers) { |
|
|
|
|
|
|
|
|
this.method = method = method.toUpperCase(); |
|
|
this.method = method = method.toUpperCase(); |
|
|
this.shouldKeepAlive = false; |
|
|
this.shouldKeepAlive = false; |
|
|
if (method === "GET" || method === "HEAD") { |
|
|
if (method === 'GET' || method === 'HEAD') { |
|
|
this.useChunkedEncodingByDefault = false; |
|
|
this.useChunkedEncodingByDefault = false; |
|
|
} else { |
|
|
} else { |
|
|
this.useChunkedEncodingByDefault = true; |
|
|
this.useChunkedEncodingByDefault = true; |
|
|
} |
|
|
} |
|
|
this._last = true; |
|
|
this._last = true; |
|
|
|
|
|
|
|
|
this._storeHeader(method + " " + url + " HTTP/1.1\r\n", headers); |
|
|
this._storeHeader(method + ' ' + url + ' HTTP/1.1\r\n', headers); |
|
|
} |
|
|
} |
|
|
util.inherits(ClientRequest, OutgoingMessage); |
|
|
util.inherits(ClientRequest, OutgoingMessage); |
|
|
|
|
|
|
|
@ -700,7 +697,7 @@ function outgoingFlush (socket) { |
|
|
//
|
|
|
//
|
|
|
// When the user does
|
|
|
// When the user does
|
|
|
//
|
|
|
//
|
|
|
// req2.write("hello world\n");
|
|
|
// 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
|
|
|
// the socket yet. Thus the outgoing messages need to be prepared to queue
|
|
@ -751,10 +748,10 @@ function Server (requestListener) { |
|
|
net.Server.call(this, { allowHalfOpen: true }); |
|
|
net.Server.call(this, { allowHalfOpen: true }); |
|
|
|
|
|
|
|
|
if (requestListener) { |
|
|
if (requestListener) { |
|
|
this.addListener("request", requestListener); |
|
|
this.addListener('request', requestListener); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
this.addListener("connection", connectionListener); |
|
|
this.addListener('connection', connectionListener); |
|
|
} |
|
|
} |
|
|
util.inherits(Server, net.Server); |
|
|
util.inherits(Server, net.Server); |
|
|
|
|
|
|
|
@ -770,7 +767,7 @@ exports.createServer = function (requestListener) { |
|
|
function connectionListener(socket) { |
|
|
function connectionListener(socket) { |
|
|
var self = this; |
|
|
var self = this; |
|
|
|
|
|
|
|
|
debug("SERVER new http connection"); |
|
|
debug('SERVER new http connection'); |
|
|
|
|
|
|
|
|
httpSocketSetup(socket); |
|
|
httpSocketSetup(socket); |
|
|
|
|
|
|
|
@ -790,7 +787,7 @@ function connectionListener (socket) { |
|
|
socket.ondata = function(d, start, end) { |
|
|
socket.ondata = function(d, start, end) { |
|
|
var ret = parser.execute(d, start, end - start); |
|
|
var ret = parser.execute(d, start, end - start); |
|
|
if (ret instanceof Error) { |
|
|
if (ret instanceof Error) { |
|
|
debug("parse error"); |
|
|
debug('parse error'); |
|
|
socket.destroy(ret); |
|
|
socket.destroy(ret); |
|
|
} else if (parser.incoming && parser.incoming.upgrade) { |
|
|
} else if (parser.incoming && parser.incoming.upgrade) { |
|
|
var bytesParsed = ret; |
|
|
var bytesParsed = ret; |
|
@ -803,7 +800,7 @@ function connectionListener (socket) { |
|
|
// in the upgradeHead from the closing lines of the headers
|
|
|
// in the upgradeHead from the closing lines of the headers
|
|
|
var upgradeHead = d.slice(start + bytesParsed + 1, end); |
|
|
var upgradeHead = d.slice(start + bytesParsed + 1, end); |
|
|
|
|
|
|
|
|
if (self.listeners("upgrade").length) { |
|
|
if (self.listeners('upgrade').length) { |
|
|
self.emit('upgrade', req, req.socket, upgradeHead); |
|
|
self.emit('upgrade', req, req.socket, upgradeHead); |
|
|
} else { |
|
|
} else { |
|
|
// Got upgrade header, but have no handler.
|
|
|
// Got upgrade header, but have no handler.
|
|
@ -861,8 +858,8 @@ function connectionListener (socket) { |
|
|
(req.httpVersionMajor == 1 && req.httpVersionMinor == 1) && |
|
|
(req.httpVersionMajor == 1 && req.httpVersionMinor == 1) && |
|
|
continueExpression.test(req.headers['expect'])) { |
|
|
continueExpression.test(req.headers['expect'])) { |
|
|
res._expect_continue = true; |
|
|
res._expect_continue = true; |
|
|
if (self.listeners("checkContinue").length) { |
|
|
if (self.listeners('checkContinue').length) { |
|
|
self.emit("checkContinue", req, res); |
|
|
self.emit('checkContinue', req, res); |
|
|
} else { |
|
|
} else { |
|
|
res.writeContinue(); |
|
|
res.writeContinue(); |
|
|
self.emit('request', req, res); |
|
|
self.emit('request', req, res); |
|
@ -890,7 +887,7 @@ function Client ( ) { |
|
|
|
|
|
|
|
|
function onData(d, start, end) { |
|
|
function onData(d, start, end) { |
|
|
if (!self.parser) { |
|
|
if (!self.parser) { |
|
|
throw new Error("parser not initialized prior to Client.ondata call"); |
|
|
throw new Error('parser not initialized prior to Client.ondata call'); |
|
|
} |
|
|
} |
|
|
var ret = self.parser.execute(d, start, end - start); |
|
|
var ret = self.parser.execute(d, start, end - start); |
|
|
if (ret instanceof Error) { |
|
|
if (ret instanceof Error) { |
|
@ -912,30 +909,29 @@ function Client ( ) { |
|
|
} |
|
|
} |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
self.addListener("connect", function () { |
|
|
self.addListener('connect', function() { |
|
|
debug('CLIENT connected'); |
|
|
debug('CLIENT connected'); |
|
|
|
|
|
|
|
|
self.ondata = onData; |
|
|
self.ondata = onData; |
|
|
self.onend = onEnd; |
|
|
self.onend = onEnd; |
|
|
|
|
|
|
|
|
self._state = "connected"; |
|
|
self._state = 'connected'; |
|
|
|
|
|
|
|
|
self._initParser(); |
|
|
self._initParser(); |
|
|
debug('CLIENT requests: ' + util.inspect(self._outgoing.map(function (r) { return r.method; }))); |
|
|
|
|
|
outgoingFlush(self); |
|
|
outgoingFlush(self); |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
function onEnd() { |
|
|
function onEnd() { |
|
|
if (self.parser) self.parser.finish(); |
|
|
if (self.parser) self.parser.finish(); |
|
|
debug("CLIENT got end closing. state = " + self._state); |
|
|
debug('CLIENT got end closing. state = ' + self._state); |
|
|
self.end(); |
|
|
self.end(); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
self.addListener("close", function (e) { |
|
|
self.addListener('close', function(e) { |
|
|
self._state = "disconnected"; |
|
|
self._state = 'disconnected'; |
|
|
if (e) return; |
|
|
if (e) return; |
|
|
|
|
|
|
|
|
debug("CLIENT onClose. state = " + self._state); |
|
|
debug('CLIENT onClose. state = ' + self._state); |
|
|
|
|
|
|
|
|
// finally done with the request
|
|
|
// finally done with the request
|
|
|
self._outgoing.shift(); |
|
|
self._outgoing.shift(); |
|
@ -948,7 +944,7 @@ function Client ( ) { |
|
|
self.parser = null; |
|
|
self.parser = null; |
|
|
} |
|
|
} |
|
|
}); |
|
|
}); |
|
|
}; |
|
|
} |
|
|
util.inherits(Client, net.Stream); |
|
|
util.inherits(Client, net.Stream); |
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -971,18 +967,18 @@ Client.prototype._initParser = function () { |
|
|
self.parser.reinitialize('response'); |
|
|
self.parser.reinitialize('response'); |
|
|
self.parser.socket = self; |
|
|
self.parser.socket = self; |
|
|
self.parser.onIncoming = function(res) { |
|
|
self.parser.onIncoming = function(res) { |
|
|
debug("CLIENT incoming response!"); |
|
|
debug('CLIENT incoming response!'); |
|
|
|
|
|
|
|
|
var req = self._outgoing[0]; |
|
|
var req = self._outgoing[0]; |
|
|
|
|
|
|
|
|
// Responses to HEAD requests are AWFUL. Ask Ryan.
|
|
|
// Responses to HEAD requests are AWFUL. Ask Ryan.
|
|
|
// A major oversight in HTTP. Hence this nastiness.
|
|
|
// A major oversight in HTTP. Hence this nastiness.
|
|
|
var isHeadResponse = req.method == "HEAD"; |
|
|
var isHeadResponse = req.method == 'HEAD'; |
|
|
debug('CLIENT isHeadResponse ' + isHeadResponse); |
|
|
debug('CLIENT isHeadResponse ' + isHeadResponse); |
|
|
|
|
|
|
|
|
if (res.statusCode == 100) { |
|
|
if (res.statusCode == 100) { |
|
|
// restart the parser, as this is a continue message.
|
|
|
// restart the parser, as this is a continue message.
|
|
|
req.emit("continue"); |
|
|
req.emit('continue'); |
|
|
return true; |
|
|
return true; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -991,7 +987,7 @@ Client.prototype._initParser = function () { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
res.addListener('end', function() { |
|
|
res.addListener('end', function() { |
|
|
debug("CLIENT request complete disconnecting. state = " + self._state); |
|
|
debug('CLIENT request complete disconnecting. state = ' + self._state); |
|
|
// For the moment we reconnect for every request. FIXME!
|
|
|
// For the moment we reconnect for every request. FIXME!
|
|
|
// All that should be required for keep-alive is to not reconnect,
|
|
|
// All that should be required for keep-alive is to not reconnect,
|
|
|
// but outgoingFlush instead.
|
|
|
// but outgoingFlush instead.
|
|
@ -1004,7 +1000,7 @@ Client.prototype._initParser = function () { |
|
|
} |
|
|
} |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
req.emit("response", res); |
|
|
req.emit('response', res); |
|
|
|
|
|
|
|
|
return isHeadResponse; |
|
|
return isHeadResponse; |
|
|
}; |
|
|
}; |
|
@ -1030,7 +1026,7 @@ Client.prototype._onOutgoingSent = function (message) { |
|
|
// Instead, we just check if the connection is closed, and if so
|
|
|
// Instead, we just check if the connection is closed, and if so
|
|
|
// reconnect if we have pending messages.
|
|
|
// reconnect if we have pending messages.
|
|
|
if (this._outgoing.length) { |
|
|
if (this._outgoing.length) { |
|
|
debug("CLIENT request flush. ensure connection. state = " + this._state); |
|
|
debug('CLIENT request flush. ensure connection. state = ' + this._state); |
|
|
this._ensureConnection(); |
|
|
this._ensureConnection(); |
|
|
} |
|
|
} |
|
|
}; |
|
|
}; |
|
@ -1038,19 +1034,19 @@ Client.prototype._onOutgoingSent = function (message) { |
|
|
|
|
|
|
|
|
Client.prototype._ensureConnection = function() { |
|
|
Client.prototype._ensureConnection = function() { |
|
|
if (this._state == 'disconnected') { |
|
|
if (this._state == 'disconnected') { |
|
|
debug("CLIENT reconnecting state = " + this._state); |
|
|
debug('CLIENT reconnecting state = ' + this._state); |
|
|
this.connect(this.port, this.host); |
|
|
this.connect(this.port, this.host); |
|
|
this._state = "connecting"; |
|
|
this._state = 'connecting'; |
|
|
} |
|
|
} |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Client.prototype.request = function(method, url, headers) { |
|
|
Client.prototype.request = function(method, url, headers) { |
|
|
if (typeof(url) != "string") { |
|
|
if (typeof(url) != 'string') { |
|
|
// assume method was omitted, shift arguments
|
|
|
// assume method was omitted, shift arguments
|
|
|
headers = url; |
|
|
headers = url; |
|
|
url = method; |
|
|
url = method; |
|
|
method = "GET"; |
|
|
method = 'GET'; |
|
|
} |
|
|
} |
|
|
var req = new ClientRequest(this, method, url, headers); |
|
|
var req = new ClientRequest(this, method, url, headers); |
|
|
this._outgoing.push(req); |
|
|
this._outgoing.push(req); |
|
@ -1083,7 +1079,7 @@ exports.cat = function (url, encoding_, headers_) { |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
var url = require("url").parse(url); |
|
|
var url = require('url').parse(url); |
|
|
|
|
|
|
|
|
var hasHost = false; |
|
|
var hasHost = false; |
|
|
if (Array.isArray(headers)) { |
|
|
if (Array.isArray(headers)) { |
|
@ -1093,7 +1089,7 @@ exports.cat = function (url, encoding_, headers_) { |
|
|
break; |
|
|
break; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} else if (typeof headers === "Object") { |
|
|
} else if (typeof headers === 'Object') { |
|
|
var keys = Object.keys(headers); |
|
|
var keys = Object.keys(headers); |
|
|
for (var i = 0, l = keys.length; i < l; i++) { |
|
|
for (var i = 0, l = keys.length; i < l; i++) { |
|
|
var key = keys[i]; |
|
|
var key = keys[i]; |
|
@ -1103,14 +1099,17 @@ exports.cat = function (url, encoding_, headers_) { |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
if (!hasHost) headers["Host"] = url.hostname; |
|
|
if (!hasHost) headers['Host'] = url.hostname; |
|
|
|
|
|
|
|
|
var content = ""; |
|
|
var content = ''; |
|
|
|
|
|
|
|
|
var client = exports.createClient(url.port || 80, url.hostname); |
|
|
var client = exports.createClient(url.port || 80, url.hostname); |
|
|
var req = client.request((url.pathname || "/")+(url.search || "")+(url.hash || ""), headers); |
|
|
var req = client.request((url.pathname || '/') + |
|
|
|
|
|
(url.search || '') + |
|
|
|
|
|
(url.hash || ''), |
|
|
|
|
|
headers); |
|
|
|
|
|
|
|
|
if (url.protocol=="https:") { |
|
|
if (url.protocol == 'https:') { |
|
|
client.https = true; |
|
|
client.https = true; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -1135,14 +1134,14 @@ exports.cat = function (url, encoding_, headers_) { |
|
|
}); |
|
|
}); |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
client.addListener("error", function (err) { |
|
|
client.addListener('error', function(err) { |
|
|
if (callback && !callbackSent) { |
|
|
if (callback && !callbackSent) { |
|
|
callback(err); |
|
|
callback(err); |
|
|
callbackSent = true; |
|
|
callbackSent = true; |
|
|
} |
|
|
} |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
client.addListener("close", function () { |
|
|
client.addListener('close', function() { |
|
|
if (callback && !callbackSent) { |
|
|
if (callback && !callbackSent) { |
|
|
callback(new Error('Connection closed unexpectedly')); |
|
|
callback(new Error('Connection closed unexpectedly')); |
|
|
callbackSent = true; |
|
|
callbackSent = true; |
|
|