request
Advanced tools
Comparing version 1.9.5 to 1.9.7
144
main.js
@@ -62,2 +62,6 @@ // Copyright 2010-2011 Mikeal Rogers | ||
if (typeof options === 'string') { | ||
options = {uri:options}; | ||
} | ||
for (i in options) { | ||
@@ -68,2 +72,3 @@ this[i] = options[i]; | ||
this.dests = []; | ||
this.__isRequestRequest = true; | ||
} | ||
@@ -145,6 +150,6 @@ util.inherits(Request, stream.Stream); | ||
if (options.uri.auth && !options.headers.authorization) { | ||
options.headers.authorization = "Basic " + toBase64(options.uri.auth.split(':').map(qs.unescape).join(':')); | ||
options.headers.authorization = "Basic " + toBase64(options.uri.auth.split(':').map(function(item){ return qs.unescape(item)}).join(':')); | ||
} | ||
if (options.proxy && options.proxy.auth && !options.headers['proxy-authorization']) { | ||
options.headers['proxy-authorization'] = "Basic " + toBase64(options.proxy.auth.split(':').map(qs.unescape).join(':')); | ||
options.headers.authorization = "Basic " + toBase64(options.uri.auth.split(':').map(function(item){ return qs.unescape(item)}).join(':')); | ||
} | ||
@@ -206,50 +211,57 @@ | ||
} | ||
options.start = function () { | ||
options._started = true; | ||
options.req = options.httpModule.request(options, function (response) { | ||
options.response = response; | ||
if (setHost) delete options.headers.host; | ||
options.req = options.httpModule.request(options, function (response) { | ||
options.response = response; | ||
if (setHost) delete options.headers.host; | ||
if (response.statusCode >= 300 && | ||
response.statusCode < 400 && | ||
options.followRedirect && | ||
options.method !== 'PUT' && | ||
options.method !== 'POST' && | ||
response.headers.location) { | ||
if (options._redirectsFollowed >= options.maxRedirects) { | ||
options.emit('error', new Error("Exceeded maxRedirects. Probably stuck in a redirect loop.")); | ||
} | ||
options._redirectsFollowed += 1; | ||
if (!isUrl.test(response.headers.location)) { | ||
response.headers.location = url.resolve(options.uri.href, response.headers.location); | ||
} | ||
options.uri = response.headers.location; | ||
delete options.req; | ||
delete options.agent; | ||
if (options.headers) { | ||
delete options.headers.host; | ||
} | ||
request(options, options.callback); | ||
return; // Ignore the rest of the response | ||
} else { | ||
options._redirectsFollowed = 0; | ||
// Be a good stream and emit end when the response is finished. | ||
// Hack to emit end on close becuase of a core bug that never fires end | ||
response.on('close', function () {options.emit('end')}) | ||
if (options.encoding) { | ||
if (options.dests.length !== 0) { | ||
console.error("Ingoring encoding parameter as this stream is being piped to another stream which makes the encoding option invalid."); | ||
} else { | ||
response.setEncoding(options.encoding); | ||
if (response.statusCode >= 300 && | ||
response.statusCode < 400 && | ||
options.followRedirect && | ||
options.method !== 'PUT' && | ||
options.method !== 'POST' && | ||
response.headers.location) { | ||
if (options._redirectsFollowed >= options.maxRedirects) { | ||
options.emit('error', new Error("Exceeded maxRedirects. Probably stuck in a redirect loop.")); | ||
} | ||
} | ||
if (options.dests.length !== 0) { | ||
options._redirectsFollowed += 1; | ||
if (!isUrl.test(response.headers.location)) { | ||
response.headers.location = url.resolve(options.uri.href, response.headers.location); | ||
} | ||
options.uri = response.headers.location; | ||
delete options.req; | ||
delete options.agent; | ||
if (options.headers) { | ||
delete options.headers.host; | ||
} | ||
request(options, options.callback); | ||
return; // Ignore the rest of the response | ||
} else { | ||
options._redirectsFollowed = 0; | ||
// Be a good stream and emit end when the response is finished. | ||
// Hack to emit end on close because of a core bug that never fires end | ||
response.on('close', function () {options.response.emit('end')}) | ||
if (options.encoding) { | ||
if (options.dests.length !== 0) { | ||
console.error("Ingoring encoding parameter as this stream is being piped to another stream which makes the encoding option invalid."); | ||
} else { | ||
response.setEncoding(options.encoding); | ||
} | ||
} | ||
options.dests.forEach(function (dest) { | ||
response.pipe(dest); | ||
if (dest.headers) { | ||
dest.headers['content-type'] = response.headers['content-type']; | ||
if (response.headers['content-length']) { | ||
dest.headers['content-length'] = response.headers['content-length']; | ||
} | ||
} | ||
}) | ||
if (options.onResponse) options.onResponse(null, response); | ||
if (options.callback) options.callback(null, response, options.responseBodyStream); | ||
} else { | ||
response.on("data", function (chunk) {options.emit("data", chunk)}); | ||
response.on("end", function (chunk) {options.emit("end", chunk)}); | ||
response.on("close", function () {options.emit("close")}); | ||
if (options.onResponse) { | ||
@@ -260,6 +272,6 @@ options.onResponse(null, response); | ||
var buffer = ''; | ||
response.on("data", function (chunk) { | ||
options.on("data", function (chunk) { | ||
buffer += chunk; | ||
}) | ||
response.on("end", function () { | ||
options.on("end", function () { | ||
options.callback(null, response, buffer); | ||
@@ -270,6 +282,6 @@ }) | ||
} | ||
} | ||
}) | ||
options.req.on('error', clientErrorHandler); | ||
}) | ||
options.req.on('error', clientErrorHandler); | ||
} | ||
@@ -286,4 +298,4 @@ options.once('pipe', function (src) { | ||
if (options.body) { | ||
options.req.write(options.body); | ||
options.req.end(); | ||
options.write(options.body); | ||
options.end(); | ||
} else if (options.requestBodyStream) { | ||
@@ -293,3 +305,3 @@ console.warn("options.requestBodyStream is deprecated, please pass the request object to stream.pipe.") | ||
} else if (!options.src) { | ||
options.req.end(); | ||
options.end(); | ||
} | ||
@@ -302,4 +314,6 @@ options.ntick = true; | ||
this.dests.push(dest); | ||
stream.Stream.prototype.pipe.call(this, dest) | ||
} | ||
Request.prototype.write = function () { | ||
if (!this._started) this.start(); | ||
if (!this.req) throw new Error("This request has been piped before http.request() was called."); | ||
@@ -309,2 +323,3 @@ this.req.write.apply(this.req, arguments); | ||
Request.prototype.end = function () { | ||
if (!this._started) this.start(); | ||
if (!this.req) throw new Error("This request has been piped before http.request() was called."); | ||
@@ -314,11 +329,12 @@ this.req.end.apply(this.req, arguments); | ||
Request.prototype.pause = function () { | ||
if (!this.req) throw new Error("This request has been piped before http.request() was called."); | ||
this.req.pause.apply(this.req, arguments); | ||
if (!this.response) throw new Error("This request has been piped before http.request() was called."); | ||
this.response.pause.apply(this.response, arguments); | ||
} | ||
Request.prototype.resume = function () { | ||
if (!this.req) throw new Error("This request has been piped before http.request() was called."); | ||
this.req.resume.apply(this.req, arguments); | ||
if (!this.response) throw new Error("This request has been piped before http.request() was called."); | ||
this.response.resume.apply(this.response, arguments); | ||
} | ||
function request (options, callback) { | ||
if (typeof options === 'string') options = {uri:options}; | ||
if (callback) options.callback = callback; | ||
@@ -336,5 +352,5 @@ var r = new Request(options); | ||
for (i in options) { | ||
if (!opts[i]) opts[i] = options[i]; | ||
return method(opts, callback); | ||
if (opts[i] === undefined) opts[i] = options[i]; | ||
} | ||
return method(opts, callback); | ||
} | ||
@@ -349,3 +365,3 @@ return d; | ||
de.del = def(request.del); | ||
return d; | ||
return de; | ||
} | ||
@@ -355,2 +371,3 @@ | ||
request.post = function (options, callback) { | ||
if (typeof options === 'string') options = {uri:options}; | ||
options.method = 'POST'; | ||
@@ -360,2 +377,3 @@ return request(options, callback); | ||
request.put = function (options, callback) { | ||
if (typeof options === 'string') options = {uri:options}; | ||
options.method = 'PUT'; | ||
@@ -365,2 +383,3 @@ return request(options, callback); | ||
request.head = function (options, callback) { | ||
if (typeof options === 'string') options = {uri:options}; | ||
options.method = 'HEAD'; | ||
@@ -373,4 +392,5 @@ if (options.body || options.requestBodyStream || options.json || options.multipart) { | ||
request.del = function (options, callback) { | ||
if (typeof options === 'string') options = {uri:options}; | ||
options.method = 'DELETE'; | ||
return request(options, callback); | ||
} |
{ "name" : "request" | ||
, "description" : "Simplified HTTP request client." | ||
, "tags" : ["http", "simple", "util", "utility"] | ||
, "version" : "1.9.5" | ||
, "version" : "1.9.7" | ||
, "author" : "Mikeal Rogers <mikeal.rogers@gmail.com>" | ||
@@ -6,0 +6,0 @@ , "repository" : |
@@ -13,3 +13,3 @@ var server = require('./server') | ||
function check () { | ||
if (passes === 2) { | ||
if (passes === 3) { | ||
console.log('All tests passed.') | ||
@@ -55,1 +55,24 @@ process.exit(); | ||
s.on('/cat', function (req, resp) { | ||
if (req.method === "GET") { | ||
resp.writeHead(200, {'content-type':'text/plain', 'content-length':4}); | ||
resp.write('asdf'); | ||
resp.end() | ||
} else if (req.method === "PUT") { | ||
assert.ok(req.headers['content-type'] === 'text/plain'); | ||
assert.ok(req.headers['content-length'] == 4) | ||
var validate = ''; | ||
req.on('data', function (chunk) {validate += chunk}) | ||
req.on('end', function () { | ||
resp.writeHead(201); | ||
resp.end(); | ||
assert.ok(validate === 'asdf'); | ||
passes += 1; | ||
check(); | ||
}) | ||
} | ||
}) | ||
request.get('http://localhost:3453/cat').pipe(request.put('http://localhost:3453/cat')) | ||
35736
677
12