Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Closes GH-610 Better proxy stuff.

  • Loading branch information...
commit dbce209238a647c57389f39ff4809cafc157901a 1 parent 6917dc7
@isaacs isaacs authored
View
12 lib/utils/fetch.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) {
View
50 lib/utils/proxyify.js
@@ -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
}
View
11 lib/utils/registry/request.js
@@ -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)
}
Please sign in to comment.
Something went wrong with that request. Please try again.