This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

Closes GH-610 Better proxy stuff.

  • Loading branch information...
isaacs committed Feb 23, 2011
1 parent 6917dc7 commit dbce209238a647c57389f39ff4809cafc157901a
Showing with 25 additions and 48 deletions.
  1. +1 −11 lib/utils/fetch.js
  2. +22 −28 lib/utils/proxyify.js
  3. +2 −9 lib/utils/registry/request.js
@@ -11,7 +11,6 @@ var http = require("http")
, set = require("./set")
, log = require("./log")
, npm = require("../../npm")
, consts
, path = require("path")
, mkdir = require("./mkdir-p")
, consts = require("constants")
@@ -59,16 +58,7 @@ function fetchAndWrite (remote, fstr, headers, maxRedirects, redirects) {
}
if (!opts.port) opts.port = opts.secure ? 443 : 80
delete headers.host // this will get changed automatically
if (remote.auth) {
headers["authorization"] = (new Buffer(remote.auth).toString("base64"))
delete remote.href
delete remote.host
delete remote.auth
}
log.verbose(remote, "proxyifying")
opts = proxyify(remote, opts)
opts = proxyify(npm.config.get("proxy"), remote, opts)
if (!opts) return cb(new Error("Bad proxy config: "+npm.config.get("proxy")))
;(opts.secure ? https : http).get(opts, function (response) {
@@ -1,53 +1,47 @@
// given a proxy url, a remote href, and an option object,
// return a modified option object that goes through the proxy.
module.exports = proxyify
var npm = require("../../npm")
, url = require("url")
, log = require("./log")
var url = require("url")
, proxy
var proxy
function proxyify (remote, opts) {
function proxyify (proxyConfig, remote, opts) {
if (proxy === false) return opts
if (!proxy) {
var proxyConfig = npm.config.get("proxy")
if (!proxyConfig) {
log.verbose(proxyConfig, "no proxy")
proxy = false
return opts
}
log.verbose(proxyConfig, "proxy config")
if (!proxyConfig.match(/^https?:\/\//)) {
proxyConfig = remote.protocol + "//" + proxyConfig
}
proxy = url.parse(proxyConfig)
if (!proxy) {
log.warn(proxyConfig, "invalid proxy config")
return null
}
if (!proxy) return null
}
if (proxy.auth) {
opts.headers["proxy-authorization"] =
"Basic " + (new Buffer(proxy.auth).toString("base64"))
//TODO: remove debuggery
var unpw = proxy.auth.split(":")
, un = unpw.shift()
, pw = unpw.join(":")
log.verbose([un, pw?"****":"<no password>"], "proxy auth")
}
opts.headers.host = proxy.hostname
opts.path = url.format(remote)
opts.secure = proxy.protocol === "https:"
opts.host = proxy.hostname
opts.port = +(proxy.port ? proxy.port
:proxy.protocol === "https:" ? 443
:80)
//TODO: remove debuggery
var o = {headers:{}}
for (var i in opts) if (i !== "headers") o[i] = opts[i]
if (opts.headers) for (var i in opts.headers) o.headers[i] = opts.headers[i]
if (o.headers.authorization) o.headers.authorization = "<hidden>"
if (o.headers["proxy-authorization"]) {
o.headers["proxy-authorization"] = "<hidden>"
if (typeof remote === "string") remote = url.parse(remote)
if (remote.auth) {
headers.authorization = (new Buffer(remote.auth).toString("base64"))
delete remote.href
delete remote.host
delete remote.auth
}
log.verbose(o, "modified opts for proxy")
opts.path = url.format(remote)
opts.secure = proxy.protocol === "https:"
return opts
}
@@ -89,13 +89,6 @@ function request (method, where, what, etag, nofollow, cb_) {
}
log.silly(headers, "headers")
if (remote.auth) {
headers["authorization"] = (new Buffer(remote.auth).toString("base64"))
delete remote.href
delete remote.host
delete remote.auth
}
var opts = { method: method
, headers: headers
, path: (remote.pathname||"/")
@@ -107,8 +100,7 @@ function request (method, where, what, etag, nofollow, cb_) {
}
if (!opts.port) opts.port = opts.secure ? 443 : 80
log.verbose(remote, "about to proxyify")
opts = proxyify(remote, opts)
opts = proxyify(npm.config.get("proxy"), remote, opts)
if (!opts) return cb(new Error("Bad proxy config: "+npm.config.get("proxy")))
var req = (opts.secure ? https : http).request(opts, function (response) {
@@ -144,6 +136,7 @@ function request (method, where, what, etag, nofollow, cb_) {
parsed = JSON.parse(data)
} catch (ex) {
ex.message += "\n" + data
log.verbose(data, "bad json")
log.error("error parsing json", "registry")
return cb(ex, null, data, response)
}

0 comments on commit dbce209

Please sign in to comment.