request
Advanced tools
Comparing version 2.9.202 to 2.9.203
107
main.js
@@ -30,2 +30,3 @@ // Copyright 2010-2012 Mikeal Rogers | ||
, tunnel = require('./tunnel') | ||
, aws = require('./aws') | ||
; | ||
@@ -55,4 +56,4 @@ | ||
util.inherits(https.Agent, http.Agent) | ||
https.Agent.prototype._getConnection = function(host, port, cb) { | ||
var s = tls.connect(port, host, this.options, function() { | ||
https.Agent.prototype._getConnection = function (host, port, cb) { | ||
var s = tls.connect(port, host, this.options, function () { | ||
// do other checks here? | ||
@@ -112,3 +113,3 @@ if (cb) cb() | ||
if (!self.pool) self.pool = globalPool | ||
if (!self.pool && self.pool !== false) self.pool = globalPool | ||
self.dests = [] | ||
@@ -159,2 +160,17 @@ self.__isRequestRequest = true | ||
if (!self.uri.host || !self.uri.pathname) { | ||
// Invalid URI: it may generate lot of bad errors, like "TypeError: Cannot call method 'indexOf' of undefined" in CookieJar | ||
// Detect and reject it as soon as possible | ||
var faultyUri = url.format(self.uri) | ||
var message = 'Invalid URI "' + faultyUri + '"' | ||
if (Object.keys(options).length === 0) { | ||
// No option ? This can be the sign of a redirect | ||
// As this is a case where the user cannot do anything (he didn't call request directly with this URL) | ||
// he should be warned that it can be caused by a redirection (can save some hair) | ||
message += '. This can be caused by a crappy redirection.' | ||
} | ||
self.emit('error', new Error(message)) | ||
return // This error was fatal | ||
} | ||
self._redirectsFollowed = self._redirectsFollowed || 0 | ||
@@ -221,2 +237,6 @@ self.maxRedirects = (self.maxRedirects !== undefined) ? self.maxRedirects : 10 | ||
} | ||
if (options.aws) { | ||
self.aws(options.aws) | ||
} | ||
@@ -245,2 +265,3 @@ if (self.uri.auth && !self.headers.authorization) { | ||
} else if (options.multipart) { | ||
self.boundary = uuid() | ||
self.multipart(options.multipart) | ||
@@ -335,3 +356,3 @@ } | ||
if (Array.isArray(self.body)) { | ||
self.body.forEach(function(part) { | ||
self.body.forEach(function (part) { | ||
self.write(part) | ||
@@ -347,4 +368,6 @@ }) | ||
} else if (!self.src) { | ||
self.headers['content-length'] = 0 | ||
self.end() | ||
if (self.method !== 'GET' && typeof self.method !== 'undefined') { | ||
self.headers['content-length'] = 0; | ||
} | ||
self.end(); | ||
} | ||
@@ -405,2 +428,10 @@ self.ntick = true | ||
if (log) log('%method %href', self) | ||
if (self.src && self.src.stat && self.src.stat.size) { | ||
self.headers['content-length'] = self.src.stat.size | ||
} | ||
if (self._aws) { | ||
self.aws(self._aws, true) | ||
} | ||
self.req = self.httpModule.request(self, function (response) { | ||
@@ -428,7 +459,10 @@ if (self._aborted) return | ||
var addCookie = function (cookie) { | ||
if (self._jar) self._jar.add(new Cookie(cookie)) | ||
else cookieJar.add(new Cookie(cookie)) | ||
} | ||
if (response.headers['set-cookie'] && (!self._disableCookies)) { | ||
response.headers['set-cookie'].forEach(function(cookie) { | ||
if (self._jar) self._jar.add(new Cookie(cookie)) | ||
else cookieJar.add(new Cookie(cookie)) | ||
}) | ||
if (Array.isArray(response.headers['set-cookie'])) response.headers['set-cookie'].forEach(addCookie) | ||
else addCookie(response.headers['set-cookie']) | ||
} | ||
@@ -538,3 +572,3 @@ | ||
if (self.timeout && !self.timeoutTimer) { | ||
self.timeoutTimer = setTimeout(function() { | ||
self.timeoutTimer = setTimeout(function () { | ||
self.req.abort() | ||
@@ -549,3 +583,3 @@ var e = new Error("ETIMEDOUT") | ||
if (self.req.setTimeout) { // only works on node 0.6+ | ||
self.req.setTimeout(self.timeout, function(){ | ||
self.req.setTimeout(self.timeout, function () { | ||
if (self.req) { | ||
@@ -562,2 +596,5 @@ self.req.abort() | ||
self.req.on('error', self.clientErrorHandler) | ||
self.req.on('drain', function() { | ||
self.emit('drain') | ||
}) | ||
@@ -567,3 +604,3 @@ self.emit('request', self.req) | ||
Request.prototype.abort = function() { | ||
Request.prototype.abort = function () { | ||
this._aborted = true; | ||
@@ -607,3 +644,3 @@ | ||
Request.prototype.setHeaders = function (headers) { | ||
for (i in headers) {this.setHeader(i, headers[i])} | ||
for (var i in headers) {this.setHeader(i, headers[i])} | ||
return this | ||
@@ -635,7 +672,9 @@ } | ||
if (!self.headers['content-type']) { | ||
self.headers['content-type'] = 'multipart/related; boundary=frontier'; | ||
self.headers['content-type'] = 'multipart/related; boundary=' + self.boundary; | ||
} else { | ||
self.headers['content-type'] = self.headers['content-type'].split(';')[0] + '; boundary=frontier'; | ||
self.headers['content-type'] = self.headers['content-type'].split(';')[0] + '; boundary=' + self.boundary; | ||
} | ||
console.log('boundary >> ' + self.boundary) | ||
if (!multipart.forEach) throw new Error('Argument error, options.multipart.') | ||
@@ -645,6 +684,6 @@ | ||
var body = part.body | ||
if(!body) throw Error('Body attribute missing in multipart.') | ||
if(body == null) throw Error('Body attribute missing in multipart.') | ||
delete part.body | ||
var preamble = '--frontier\r\n' | ||
Object.keys(part).forEach(function(key){ | ||
var preamble = '--' + self.boundary + '\r\n' | ||
Object.keys(part).forEach(function (key) { | ||
preamble += key + ': ' + part[key] + '\r\n' | ||
@@ -657,3 +696,3 @@ }) | ||
}) | ||
self.body.push(new Buffer('--frontier--')) | ||
self.body.push(new Buffer('--' + self.boundary + '--')) | ||
return self | ||
@@ -672,2 +711,24 @@ } | ||
} | ||
Request.prototype.aws = function (opts, now) { | ||
if (!now) { | ||
this._aws = opts | ||
return this | ||
} | ||
var date = new Date() | ||
this.setHeader('date', date.toUTCString()) | ||
this.setHeader('authorization', aws.authorization( | ||
{ key: opts.key | ||
, secret: opts.secret | ||
, verb: this.method | ||
, date: date | ||
, resource: aws.canonicalizeResource('/' + opts.bucket + this.path) | ||
, contentType: this.headers['content-type'] || '' | ||
, md5: this.headers['content-md5'] || '' | ||
, amazonHeaders: aws.canonicalizeHeaders(this.headers) | ||
} | ||
)) | ||
return this | ||
} | ||
Request.prototype.oauth = function (_oauth) { | ||
@@ -771,3 +832,3 @@ var form | ||
if (!this._started) this.start() | ||
this.req.write.apply(this.req, arguments) | ||
return this.req.write.apply(this.req, arguments) | ||
} | ||
@@ -794,3 +855,3 @@ Request.prototype.end = function (chunk) { | ||
if ((typeof options === 'function') && !callback) callback = options; | ||
if (typeof options === 'object') { | ||
if (options && typeof options === 'object') { | ||
options.uri = uri; | ||
@@ -809,3 +870,3 @@ } else if (typeof uri === 'string') { | ||
if ((typeof options === 'function') && !callback) callback = options; | ||
if (typeof options === 'object') { | ||
if (options && typeof options === 'object') { | ||
options.uri = uri; | ||
@@ -812,0 +873,0 @@ } else if (typeof uri === 'string') { |
@@ -28,4 +28,4 @@ var crypto = require('crypto') | ||
}).join("%26") | ||
var key = consumer_secret + '&' | ||
if (token_secret) key += token_secret | ||
var key = encodeURIComponent(consumer_secret) + '&' | ||
if (token_secret) key += encodeURIComponent(token_secret) | ||
return sha1(key, base) | ||
@@ -32,0 +32,0 @@ } |
{ "name" : "request" | ||
, "description" : "Simplified HTTP request client." | ||
, "tags" : ["http", "simple", "util", "utility"] | ||
, "version" : "2.9.202" | ||
, "version" : "2.9.203" | ||
, "author" : "Mikeal Rogers <mikeal.rogers@gmail.com>" | ||
@@ -6,0 +6,0 @@ , "repository" : |
@@ -19,3 +19,3 @@ # Request -- Simplified HTTP request method | ||
Request is designed to be the simplest way possible to make http calls. It support HTTPS and follows redirects by default. | ||
Request is designed to be the simplest way possible to make http calls. It supports HTTPS and follows redirects by default. | ||
@@ -22,0 +22,0 @@ ```javascript |
@@ -17,2 +17,3 @@ var spawn = require('child_process').spawn | ||
, 'test-pipes.js' | ||
, 'test-pool.js' | ||
, 'test-proxy.js' | ||
@@ -19,0 +20,0 @@ , 'test-qs.js' |
@@ -53,18 +53,3 @@ var server = require('./server') | ||
} | ||
, testPutMultipart : | ||
{ resp: server.createPostValidator( | ||
'--frontier\r\n' + | ||
'content-type: text/html\r\n' + | ||
'\r\n' + | ||
'<html><body>Oh hi.</body></html>' + | ||
'\r\n--frontier\r\n\r\n' + | ||
'Oh hi.' + | ||
'\r\n--frontier--' | ||
) | ||
, method: "PUT" | ||
, multipart: | ||
[ {'content-type': 'text/html', 'body': '<html><body>Oh hi.</body></html>'} | ||
, {'body': 'Oh hi.'} | ||
] | ||
} | ||
} | ||
@@ -71,0 +56,0 @@ |
@@ -11,5 +11,5 @@ var request = require('../main') | ||
var r = request('http://localhost:8080', function (e, resp) { | ||
assert(JSON.parse(JSON.stringify(r)).response.statusCode, 200) | ||
assert.equal(JSON.parse(JSON.stringify(r)).response.statusCode, 200) | ||
s.close() | ||
}) | ||
}) |
173706
51
3140
20