Permalink
Browse files

(new) implement HTTP BasicAuth

  • Loading branch information...
cloudhead
cloudhead committed Dec 3, 2010
1 parent 0b07809 commit 25fe4f36171b91c1333b57edf13b90ea4099149d
Showing with 21 additions and 6 deletions.
  1. +17 −6 bin/http-console
  2. +4 −0 lib/http-console.js
View
@@ -1,7 +1,7 @@
#!/usr/bin/env node
var path = require('path'),
- sys = require('sys');
+ sys = require('util');
require.paths.unshift(path.join(__dirname, '..', 'lib'));
@@ -64,13 +64,24 @@ while (arg = argv.shift()) {
}
}
-var hostPort = args.shift() || 'http://127.0.0.1:8080',
- protocol = hostPort.match(/^(https?)?/)[0] || 'http';
+var url = args.shift() || 'http://127.0.0.1:8080',
+ protocol = url.match(/^(https?)?/)[0] || 'http';
-hostPort = hostPort.replace(protocol + '://', '').split(':');
+url = url.replace(protocol + '://', '').split('@');
-var host = hostPort[0],
- port = hostPort[1] || (options.useSSL ? 443 : 80);
+var hostPort = url.pop().split(':'),
+ auth = url.pop();
+
+if (auth) {
+ auth = auth.split(':');
+ options.auth = {
+ username: auth.shift(),
+ password: auth.shift()
+ };
+}
+
+var host = hostPort.shift(),
+ port = hostPort.shift() || (options.useSSL ? 443 : 80);
var console = new(httpConsole.Console)(host, port, options);
View
@@ -48,6 +48,10 @@ this.Console.prototype = new(function () {
this.headers = { 'Accept':'*/*' };
if (this.options.json) { this.headers['Content-Type'] = 'application/json' }
+ if (this.options.auth) {
+ this.headers['Authorization'] = "Basic " +
+ new(Buffer)(this.options.auth.user + ':' + this.options.auth.password).toString('base64');
+ }
this.stdin = process.openStdin();
this.readline = readline.createInterface(this.stdin, this.stdin.fd < 3);

0 comments on commit 25fe4f3

Please sign in to comment.