Merge pull request #59 from feross/issue-58

Allow more control over HTTP routing
This commit is contained in:
Astro 2015-02-06 03:05:09 +01:00
commit 3101afd431
2 changed files with 9 additions and 4 deletions

View File

@ -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 ]

View File

@ -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({