Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

new structure. new convenience methods

  • Loading branch information...
commit 2a2e2a2f5c4760d4da3caa1a0f2d14c31a4222dc 1 parent cec3f3f
@mikeal mikeal authored
Showing with 114 additions and 2 deletions.
  1. +112 −0 main.js
  2. +2 −2 package.json
View
112 main.js
@@ -0,0 +1,112 @@
+var http = require('http')
+ , url = require('url')
+ , sys = require('sys')
+ ;
+
+var toBase64 = function(str) {
+ return (new Buffer(str || "", "ascii")).toString("base64");
+};
+
+function request (options, callback) {
+ if (!options.uri) {
+ throw new Error("options.uri is a required argument")
+ } else {
+ if (typeof options.uri == "string") {
+ options.uri = url.parse(options.uri);
+ }
+ }
+
+ options.followRedirect = options.followRedirect ? options.followRedirect : true;
+ options.method = options.method ? options.method : 'GET';
+
+ options.headers = options.headers ? options.headers : {};
+ if (!options.headers.host) {
+ options.headers.host = options.uri.hostname;
+ if (options.uri.port) {
+ if ( !(options.uri.port === 80 && options.uri.protocol === 'http:') &&
+ !(options.uri.port === 443 && options.uri.protocol === 'https:') )
+ options.headers.host += (':'+options.uri.port)
+ }
+ var setHost = true;
+ } else {
+ var setHost = false;
+ }
+
+ if (!options.uri.pathname) {options.uri.pathname = '/'}
+ if (!options.uri.port) {
+ if (options.uri.protocol == 'http:') {options.uri.port = 80}
+ else if (options.uri.protocol == 'https:') {options.uri.port = 443}
+ }
+
+ if (options.uri.protocol == 'https:') {
+ var secure = true;
+ } else {
+ var secure = false;
+ }
+
+ if (options.bodyStream) {
+ sys.error('options.bodyStream is deprecated. use options.reponseBodyStream instead.');
+ options.responseBodyStream = options.bodyStream;
+ }
+
+ options.client = options.client ? options.client : http.createClient(options.uri.port, options.uri.hostname, secure);
+
+ var clientErrorHandler = function (error) {
+ if (setHost) delete options.headers.host;
+ if (callback) callback(error);
+ }
+ options.client.addListener('error', clientErrorHandler);
+
+ if (options.uri.auth && !options.headers.authorization) {
+ options.headers.authorization = "Basic " + toBase64(options.uri.auth);
+ }
+ options.fullpath = options.uri.href.replace(options.uri.protocol + '//' + options.uri.host, '');
+ if (options.fullpath.length === 0) options.fullpath = '/'
+ if (options.body) {options.headers['content-length'] = options.body.length}
+ options.request = options.client.request(options.method, options.fullpath, options.headers);
+
+ options.request.addListener("response", function (response) {
+ var buffer;
+ if (options.responseBodyStream) {
+ buffer = options.responseBodyStream;
+ sys.pump(response, options.responseBodyStream);
+ }
+ else {
+ buffer = '';
+ response.addListener("data", function (chunk) { buffer += chunk; } )
+ }
+
+ response.addListener("end", function () {
+ options.client.removeListener("error", clientErrorHandler);
+
+ if (response.statusCode > 299 && response.statusCode < 400 && options.followRedirect && response.headers.location) {
+ options.uri = response.headers.location;
+ delete options.client;
+ if (options.headers) {
+ delete options.headers.host;
+ }
+ request(options, callback);
+ return;
+ }
+
+ if (setHost) delete options.headers.host;
+ if (callback) callback(null, response, buffer);
+ })
+ })
+
+ if (options.body) {
+ options.request.write(options.body, 'binary');
+ options.request.end();
+ } else if (options.requestBodyStream) {
+ sys.pump(options.requestBodyStream, options.request);
+ } else {
+ options.request.end();
+ }
+}
+
+module.exports = request;
+
+request.get = request;
+request.post = function () {arguments[0].method = 'POST', request.apply(request, arguments)};
+request.put = function () {arguments[0].method = 'PUT', request.apply(request, arguments)};
+request.head = function () {arguments[0].method = 'HEAD', request.apply(request, arguments)};
View
4 package.json
@@ -1,7 +1,7 @@
{ "name" : "request"
, "description" : "Simplified HTTP request method."
, "tags" : ["http", "simple", "util", "utility"]
-, "version" : "0.10.0"
+, "version" : "0.11.0"
, "author" : "Mikeal Rogers <mikeal.rogers@gmail.com>"
, "directories" :
{ "lib" : "lib" }
@@ -12,5 +12,5 @@
, "bugs" :
{ "web" : "http://github.com/mikeal/node-utils/issues" }
, "engines" : ["node >=0.1.90"]
-, "main" : "./lib/main"
+, "main" : "./main"
}
Please sign in to comment.
Something went wrong with that request. Please try again.