Permalink
Browse files

Add username/password support for http_proxy.

  • Loading branch information...
1 parent 7e20109 commit 86915a6e590e3386b9e6775192a2d3bdb9bcca87 @paulbaumgart paulbaumgart committed Mar 18, 2011
Showing with 33 additions and 14 deletions.
  1. +1 −8 engines/rhino/bin/narwhal-rhino
  2. +32 −6 engines/rhino/lib/http-client-engine.js
@@ -22,14 +22,7 @@ fi
CLASSPATH=$NARWHAL_ENGINE_HOME/jars/jna.jar
BOOTCLASSPATH=$NARWHAL_ENGINE_HOME/jars/js.jar
-# Parse the http_proxy environment variable.
-# http://cache.myproxy.com:42/ => cache.myproxy.com
-http_proxy_host=`echo $http_proxy | sed 's/http:\/\/\(.*\):.*/\1/'`
-
-# http://cache.myproxy.com:42/ => 42
-http_proxy_port=`echo $http_proxy | sed 's/http:\/\/.*:\(.*\)\//\1/'`
-
-JAVA_OPTS="-Dhttp.proxyHost=$http_proxy_host -Dhttp.proxyPort=$http_proxy_port"
+JAVA_OPTS="-Dnarwhal.http_proxy=$http_proxy"
if [ -n "$NARWHAL_CLASSPATH" ]; then
CLASSPATH=$NARWHAL_CLASSPATH:$CLASSPATH
@@ -3,15 +3,42 @@
// -- tlrobinson Tom Robinson
// -- jukart Jürgen Kartnaller
// -- paulbaumgart Paul Baumgart
+// -- danielphelps Daniel Phelps
-var IO = require("io").IO;
+var IO = require("io").IO,
+ URI = require("uri");
+
+var http_proxy = java.lang.System.getProperty("narwhal.http_proxy");
+if (http_proxy) {
+ var proxySettings = URI.parse(http_proxy);
+ java.lang.System.setProperty("http.proxyHost", proxySettings.domain);
+ java.lang.System.setProperty("http.proxyPort", proxySettings.port);
+ java.lang.System.setProperty("http.proxyUsername", proxySettings.user);
+ java.lang.System.setProperty("http.proxyPassword", proxySettings.password);
+}
exports.open = function(url, mode, options) {
var connection, output, input;
- function findProxy() {
- selector = java.net.ProxySelector.getDefault();
- proxies = selector.select(java.net.URI(url));
+ function setAuthenticator() {
+ var username = java.lang.System.getProperty("http.proxyUsername");
+ var password = java.lang.System.getProperty("http.proxyPassword");
+ var passwordAuthentication = function() {
+ return new PasswordAuthentication(username, password.toCharArray());
+ };
+
+ if (username && password) {
+ java.net.Authenticator.setDefault(
+ new Authenticator({
+ "getPasswordAuthentication": passwordAuthentication
+ })
+ );
+ }
+ }
+
+ function getProxy() {
+ var selector = java.net.ProxySelector.getDefault();
+ var proxies = selector.select(java.net.URI(url));
//This list is never empty -- at the very least, it contains
//java.net.Proxy.NO_PROXY. See java.net.ProxySelector.select()
@@ -20,8 +47,7 @@ exports.open = function(url, mode, options) {
}
function initConnection() {
- proxy = findProxy();
- connection = new java.net.URL(url).openConnection(proxy);
+ connection = new java.net.URL(url).openConnection(getProxy());
connection.setDoInput(true);
connection.setDoOutput(false);
connection.setRequestMethod(options.method);

0 comments on commit 86915a6

Please sign in to comment.