From e1b7fa7e5c93a823df8ac265b64b00e557af42e1 Mon Sep 17 00:00:00 2001 From: Astro Date: Wed, 4 Feb 2015 20:34:46 +0100 Subject: [PATCH 1/2] server: save HTTP req/res in params For GH issue #58 --- server.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/server.js b/server.js index f3964e8..07fcad8 100644 --- a/server.js +++ b/server.js @@ -127,6 +127,8 @@ Server.prototype.onHttpRequest = function (req, res) { var params try { params = parseHttpRequest(req, { trustProxy: self._trustProxy }) + params.httpReq = req + params.httpRes = res } catch (err) { debug('sent error %s', err.message) res.end(bencode.encode({ From b33531d83547fab52ccabcc60763ddee4f53ab9b Mon Sep 17 00:00:00 2001 From: Astro Date: Wed, 4 Feb 2015 20:35:52 +0100 Subject: [PATCH 2/2] server: allow onHttpRequest() with options={ action: 'announce' } for custom routing For GH issue #58 --- lib/parse_http.js | 5 +++-- server.js | 6 ++++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/lib/parse_http.js b/lib/parse_http.js index eaacbaf..d90d3e5 100644 --- a/lib/parse_http.js +++ b/lib/parse_http.js @@ -5,10 +5,11 @@ var common = require('./common') var REMOVE_IPV4_MAPPED_IPV6_RE = /^::ffff:/ function parseHttpRequest (req, options) { + options = options || {} var s = req.url.split('?') var params = common.querystringParse(s[1]) - if (s[0] === '/announce') { + if (options.action === 'announce' || s[0] === '/announce') { params.action = common.ACTIONS.ANNOUNCE if (typeof params.info_hash !== 'string' || params.info_hash.length !== 20) @@ -32,7 +33,7 @@ function parseHttpRequest (req, options) { ? req.headers['x-forwarded-for'] || req.connection.remoteAddress : req.connection.remoteAddress.replace(REMOVE_IPV4_MAPPED_IPV6_RE, '') // force ipv4 params.addr = (common.IPV6_RE.test(params.ip) ? '[' + params.ip + ']' : params.ip) + ':' + params.port - } else if (s[0] === '/scrape') { + } else if (options.action === 'scrape' || s[0] === '/scrape') { params.action = common.ACTIONS.SCRAPE if (typeof params.info_hash === 'string') params.info_hash = [ params.info_hash ] diff --git a/server.js b/server.js index 07fcad8..8891e24 100644 --- a/server.js +++ b/server.js @@ -121,12 +121,14 @@ Server.prototype.getSwarm = function (infoHash) { return swarm } -Server.prototype.onHttpRequest = function (req, res) { +Server.prototype.onHttpRequest = function (req, res, options) { var self = this + options = options || {} + options.trustProxy = options.trustProxy || self._trustProxy var params try { - params = parseHttpRequest(req, { trustProxy: self._trustProxy }) + params = parseHttpRequest(req, options) params.httpReq = req params.httpRes = res } catch (err) {