diff options
author | isaacs <i@izs.me> | 2010-05-03 04:01:21 +0400 |
---|---|---|
committer | isaacs <i@izs.me> | 2010-05-03 05:07:50 +0400 |
commit | f7ee93e6b1ac01edbbe655d482fc6d14df639d6c (patch) | |
tree | c33f173d1860bbe9e95c3937f943ad334958b621 /lib | |
parent | 27b2a5a1d54c5469ee4bcdb179acc77dbeeee4b4 (diff) |
Update https handling for 0.1.92 node, and update code style
Diffstat (limited to 'lib')
-rw-r--r-- | lib/utils/fetch.js | 82 |
1 files changed, 41 insertions, 41 deletions
diff --git a/lib/utils/fetch.js b/lib/utils/fetch.js index 95b7efec3..b5fa78778 100644 --- a/lib/utils/fetch.js +++ b/lib/utils/fetch.js @@ -2,81 +2,81 @@ * Fetch an HTTP url to a local file. **/ -var http = require("http"), - url = require("url"), - sys = require("sys"), - fs = require("fs"), - get = require("./get"), - set = require("./set"), - log = require("./log"), - Promise = require("events").Promise; +var http = require("http") + , url = require("url") + , sys = require("sys") + , fs = require("fs") + , get = require("./get") + , set = require("./set") + , log = require("./log") module.exports = function fetch (remote, local, headers, cb) { if (!cb) { - cb = headers; - headers = {}; + cb = headers + headers = {} } - log(remote+" to "+local, "fetch"); - headers.host = url.parse(remote).hostname; + log(remote+" to "+local, "fetch") + headers.host = url.parse(remote).hostname fs.open(local, process.O_CREAT | process.O_WRONLY | process.O_TRUNC, 0755, function (er, fd) { - if (er) return cb(er, fd); - fetchAndWrite(remote, fd, headers, cb); + if (er) return cb(er, fd) + fetchAndWrite(remote, fd, headers, cb) } - ); + ) } function fetchAndWrite (remote, fd, headers, maxRedirects, redirects, cb) { - // log("fetchAndWrite "+sys.inspect(Array.prototype.slice.call(arguments, 0))); + // log("fetchAndWrite "+sys.inspect(Array.prototype.slice.call(arguments, 0))) if (!cb) { - cb = redirects; - redirects = 0; + cb = redirects + redirects = 0 } if (!cb) { - cb = maxRedirects; - maxRedirects = 10; + cb = maxRedirects + maxRedirects = 10 } - if (!cb) throw new Error("No callback provided"); - log(remote, "fetch"); - remote = url.parse(remote); - set(headers, "host", remote.hostname); - remote.path = remote.pathname+(remote.search||"")+(remote.hash||""); + if (!cb) throw new Error("No callback provided") + log(remote, "fetch") + remote = url.parse(remote) + set(headers, "host", remote.hostname) + remote.path = remote.pathname+(remote.search||"")+(remote.hash||"") + var https = (remote.protocol === "https:") http - .createClient(remote.port || (remote.protocol === "https:" ? 443 : 80), remote.hostname) + .createClient(remote.port || (https ? 443 : 80), remote.hostname, https) .request("GET", (remote.pathname||"/")+(remote.search||"")+(remote.hash||""), headers) .addListener("response", function (response) { // handle redirects. - var loc = get(response.headers, "location"); + var loc = get(response.headers, "location") if (Math.floor(response.statusCode / 100) === 3 && loc && loc !== remote.href && redirects < maxRedirects) { // This is a laughably naïve way to handle this situation. // @TODO: Really need a full curl or wget style module that would // do all this kind of stuff for us. - var cookie = get(response.headers, "Set-Cookie"); + var cookie = get(response.headers, "Set-Cookie") if (cookie) { - cookie = cookie.split(";").shift(); - set(headers, "Cookie", cookie); + cookie = cookie.split(";").shift() + set(headers, "Cookie", cookie) } - log(response.statusCode, "fetch"); - return fetchAndWrite(loc, fd, headers, maxRedirects, redirects + 1, cb); + log(response.statusCode, "fetch") + return fetchAndWrite(loc, fd, headers, maxRedirects, redirects + 1, cb) } if (response.statusCode !== 200) { return cb(new Error(response.statusCode + " " + - (sys.inspect(response.headers).replace(/\s+/, ' ')))); + (sys.inspect(response.headers).replace(/\s+/, ' ')))) } - response.setBodyEncoding("binary"); - var written = 0; + response.setBodyEncoding("binary") + var written = 0 response.addListener("data", function (chunk) { // write the chunk... - log((written += chunk.length) + " bytes", "fetch"); + log((written += chunk.length) + " bytes", "fetch") fs.write(fd, chunk, "binary", function (er) { if (er) cb(er) }) }) - response.addListener("error", cb); + response.addListener("error", cb) response.addListener("end", function () { - log("finished", "fetch"); - fs.close(fd, cb); - }); + log("finished", "fetch") + fs.close(fd, cb) + }) }) - .end(); + .end() } |