diff --git a/lib/parse_http.js b/lib/parse_http.js index eaacbaf2..d90d3e56 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 f3964e84..8891e249 100644 --- a/server.js +++ b/server.js @@ -121,12 +121,16 @@ 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) { debug('sent error %s', err.message) res.end(bencode.encode({