summaryrefslogtreecommitdiff
path: root/deps/npm/node_modules/request/request.js
diff options
context:
space:
mode:
Diffstat (limited to 'deps/npm/node_modules/request/request.js')
-rw-r--r--deps/npm/node_modules/request/request.js69
1 files changed, 54 insertions, 15 deletions
diff --git a/deps/npm/node_modules/request/request.js b/deps/npm/node_modules/request/request.js
index 441db80f5a..ac2cd8098f 100644
--- a/deps/npm/node_modules/request/request.js
+++ b/deps/npm/node_modules/request/request.js
@@ -16,6 +16,7 @@ var http = require('http')
, ForeverAgent = require('forever-agent')
, FormData = require('form-data')
, extend = require('extend')
+ , isstream = require('isstream')
, isTypedArray = require('is-typedarray').strict
, helpers = require('./lib/helpers')
, cookies = require('./lib/cookies')
@@ -398,7 +399,7 @@ Request.prototype.init = function (options) {
}
if (self.gzip && !self.hasHeader('accept-encoding')) {
- self.setHeader('accept-encoding', 'gzip')
+ self.setHeader('accept-encoding', 'gzip, deflate')
}
if (self.uri.auth && !self.hasHeader('authorization')) {
@@ -452,7 +453,7 @@ Request.prototype.init = function (options) {
}
}
}
- if (self.body) {
+ if (self.body && !isstream(self.body)) {
setContentLength()
}
@@ -552,15 +553,19 @@ Request.prototype.init = function (options) {
self._multipart.body.pipe(self)
}
if (self.body) {
- setContentLength()
- if (Array.isArray(self.body)) {
- self.body.forEach(function (part) {
- self.write(part)
- })
+ if (isstream(self.body)) {
+ self.body.pipe(self)
} else {
- self.write(self.body)
+ setContentLength()
+ if (Array.isArray(self.body)) {
+ self.body.forEach(function (part) {
+ self.write(part)
+ })
+ } else {
+ self.write(self.body)
+ }
+ self.end()
}
- self.end()
} else if (self.requestBodyStream) {
console.warn('options.requestBodyStream is deprecated, please pass the request object to stream.pipe.')
self.requestBodyStream.pipe(self)
@@ -744,7 +749,12 @@ Request.prototype.start = function () {
debug('make request', self.uri.href)
- self.req = self.httpModule.request(reqOptions)
+ try {
+ self.req = self.httpModule.request(reqOptions)
+ } catch (err) {
+ self.emit('error', err)
+ return
+ }
if (self.timing) {
self.startTime = new Date().getTime()
@@ -910,14 +920,29 @@ Request.prototype.onRequestResponse = function (response) {
self._ended = true
})
+ var noBody = function (code) {
+ return (
+ self.method === 'HEAD'
+ // Informational
+ || (code >= 100 && code < 200)
+ // No Content
+ || code === 204
+ // Not Modified
+ || code === 304
+ )
+ }
+
var responseContent
- if (self.gzip) {
+ if (self.gzip && !noBody(response.statusCode)) {
var contentEncoding = response.headers['content-encoding'] || 'identity'
contentEncoding = contentEncoding.trim().toLowerCase()
if (contentEncoding === 'gzip') {
responseContent = zlib.createGunzip()
response.pipe(responseContent)
+ } else if (contentEncoding === 'deflate') {
+ responseContent = zlib.createInflate()
+ response.pipe(responseContent)
} else {
// Since previous versions didn't check for Content-Encoding header,
// ignore any invalid values to preserve backwards-compatibility
@@ -1001,6 +1026,9 @@ Request.prototype.readResponseBody = function (response) {
debug('end event', self.uri.href)
if (self._aborted) {
debug('aborted', self.uri.href)
+ // `buffer` is defined in the parent scope and used in a closure it exists for the life of the request.
+ // This can lead to leaky behavior if the user retains a reference to the request object.
+ buffer.destroy()
return
}
@@ -1013,6 +1041,9 @@ Request.prototype.readResponseBody = function (response) {
} else {
response.body = buffer.toString(self.encoding)
}
+ // `buffer` is defined in the parent scope and used in a closure it exists for the life of the Request.
+ // This can lead to leaky behavior if the user retains a reference to the request object.
+ buffer.destroy()
} else if (strings.length) {
// The UTF8 BOM [0xEF,0xBB,0xBF] is converted to [0xFE,0xFF] in the JS UTC16/UCS2 representation.
// Strip this value out when the encoding is set to 'utf8', as upstream consumers won't expect it and it breaks JSON.parse().
@@ -1157,11 +1188,15 @@ Request.prototype.json = function (val) {
self.setHeader('accept', 'application/json')
}
+ if (typeof self.jsonReplacer === 'function') {
+ self._jsonReplacer = self.jsonReplacer
+ }
+
self._json = true
if (typeof val === 'boolean') {
if (self.body !== undefined) {
if (!/^application\/x-www-form-urlencoded\b/.test(self.getHeader('content-type'))) {
- self.body = safeStringify(self.body)
+ self.body = safeStringify(self.body, self._jsonReplacer)
} else {
self.body = self._qs.rfc3986(self.body)
}
@@ -1170,7 +1205,7 @@ Request.prototype.json = function (val) {
}
}
} else {
- self.body = safeStringify(val)
+ self.body = safeStringify(val, self._jsonReplacer)
if (!self.hasHeader('content-type')) {
self.setHeader('content-type', 'application/json')
}
@@ -1368,7 +1403,9 @@ Request.prototype.write = function () {
if (!self._started) {
self.start()
}
- return self.req.write.apply(self.req, arguments)
+ if (self.req) {
+ return self.req.write.apply(self.req, arguments)
+ }
}
Request.prototype.end = function (chunk) {
var self = this
@@ -1380,7 +1417,9 @@ Request.prototype.end = function (chunk) {
if (!self._started) {
self.start()
}
- self.req.end()
+ if (self.req) {
+ self.req.end()
+ }
}
Request.prototype.pause = function () {
var self = this