Skip to content

Commit

Permalink
(api) Add support for HTTP Basic auth
Browse files Browse the repository at this point in the history
  • Loading branch information
indexzero authored and Alexis Sellier committed Nov 9, 2010
1 parent 787f269 commit bae5e37
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/cradle.js
Expand Up @@ -33,7 +33,7 @@ cradle.setup = function (settings) {

cradle.Connection = function Connection(/* variable args */) {
var args = Array.prototype.slice.call(arguments),
host, port, remote, options = {};
host, port, remote, auth, options = {};

if (typeof(args[0]) === 'string') {
remote = args[0].replace('http://', '').split(':');
Expand All @@ -46,6 +46,7 @@ cradle.Connection = function Connection(/* variable args */) {
options = args[0];
host = options.host;
port = parseInt(options.port);
auth = options.auth;
// The host and port were passed separately
} else if (args.length >= 2) {
host = args[0];
Expand All @@ -55,6 +56,7 @@ cradle.Connection = function Connection(/* variable args */) {

this.host = host || cradle.host;
this.port = port || cradle.port;
this.auth = auth || cradle.auth;
this.options = cradle.merge({}, cradle.options, options);

this.socket = http.createClient(this.port, this.host);
Expand All @@ -74,6 +76,11 @@ cradle.Connection.prototype.rawRequest = function (method, path, options, data,
// HTTP Headers
headers = cradle.merge({ host: this.host }, headers || {});

// Set HTTP Basic Auth
if (this.auth) {
headers['Authorization'] = "Basic " + new Buffer(this.auth.user + ':' + this.auth.pass).toString('base64');
}

path = (path || '/').replace('http://','').replace(/\/{2,}/g, '/');
if (path.charAt(0) !== '/') { path = '/' + path }

Expand Down

0 comments on commit bae5e37

Please sign in to comment.