diff options
| author | Jannis R <mail@jannisr.de> | 2020-10-28 13:32:26 +0100 |
|---|---|---|
| committer | Jannis R <mail@jannisr.de> | 2020-10-28 16:54:02 +0100 |
| commit | 15394d9a834a65f84c0307e087fb6677627dc37d (patch) | |
| tree | b1185f6ed3f4c831075a7ef94a1256608fb65ac6 | |
| parent | 2f13bd7349b798e4afd5693f6a258a56e30cf13d (diff) | |
followRedirects optionally as a function, 1.1.0
| -rw-r--r-- | client.js | 28 | ||||
| -rw-r--r-- | package.json | 2 | ||||
| -rw-r--r-- | readme.md | 1 |
3 files changed, 23 insertions, 8 deletions
@@ -14,8 +14,8 @@ const {CODES, MESSAGES} = require('./lib/statuses') const HOUR = 60 * 60 * 1000 -const _request = (pathOrUrl, opt, cb) => { - debug('_request', pathOrUrl, opt) +const _request = (pathOrUrl, opt, ctx, cb) => { + debug('_request', pathOrUrl, ctx, opt) const { verifyAlpnId, @@ -154,6 +154,10 @@ const sendGeminiRequest = (pathOrUrl, opt, done) => { ...opt, } + const shouldFollowRedirect = 'function' === typeof followRedirects + ? followRedirects + : () => followRedirects + if (useClientCerts) { if (typeof letUserConfirmClientCertUsage !== 'function') { throw new Error('letUserConfirmClientCertUsage must be a function') @@ -176,14 +180,24 @@ const sendGeminiRequest = (pathOrUrl, opt, done) => { if (verifyAlpnId) reqOpt.verifyAlpnId = verifyAlpnId + let ctx = { + redirectsFollowed: 0, + } + let cb = (err, res) => { if (err) return done(err) // handle redirect - if (followRedirects && ( + if (( res.statusCode === CODES.REDIRECT_TEMPORARY || res.statusCode === CODES.REDIRECT_PERMANENT - )) { + ) && shouldFollowRedirect(ctx.redirectsFollowed + 1, res)) { + ctx = { + ...ctx, + redirectsFollowed: ctx.redirectsFollowed + 1 + } + debug('following redirect nr', ctx.redirectsFollowed) + // todo: handle empty res.meta const newTarget = parseUrl(res.meta) reqOpt = { @@ -192,7 +206,7 @@ const sendGeminiRequest = (pathOrUrl, opt, done) => { port: newTarget.port || reqOpt.port, } pathOrUrl = res.meta - _request(res.meta, reqOpt, cb) + _request(res.meta, reqOpt, ctx, cb) return; } @@ -229,7 +243,7 @@ const sendGeminiRequest = (pathOrUrl, opt, done) => { _request(pathOrUrl, { ...reqOpt, cert, key, - }, cb) + }, ctx, cb) }) }) return; @@ -238,7 +252,7 @@ const sendGeminiRequest = (pathOrUrl, opt, done) => { done(null, res) } - _request(pathOrUrl, reqOpt, cb) + _request(pathOrUrl, reqOpt, ctx, cb) } module.exports = sendGeminiRequest diff --git a/package.json b/package.json index 72316c9..20fbdcf 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@derhuerst/gemini", "description": "Experimental Gemini server & client.", - "version": "1.0.4", + "version": "1.1.0", "main": "index.js", "files": [ "index.js", @@ -129,6 +129,7 @@ request(pathOrUrl, opt = {}, cb) ```js { // follow redirects automatically + // Can also be a function `(nrOfRedirects, response) => boolean`. followRedirects: false, // client certificates useClientCerts: false, |
