Skip to content

Commit b33531d

Browse files
committed
server: allow onHttpRequest() with options={ action: 'announce' } for custom routing
For GH issue webtorrent#58
1 parent e1b7fa7 commit b33531d

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

lib/parse_http.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ var common = require('./common')
55
var REMOVE_IPV4_MAPPED_IPV6_RE = /^::ffff:/
66

77
function parseHttpRequest (req, options) {
8+
options = options || {}
89
var s = req.url.split('?')
910
var params = common.querystringParse(s[1])
1011

11-
if (s[0] === '/announce') {
12+
if (options.action === 'announce' || s[0] === '/announce') {
1213
params.action = common.ACTIONS.ANNOUNCE
1314

1415
if (typeof params.info_hash !== 'string' || params.info_hash.length !== 20)
@@ -32,7 +33,7 @@ function parseHttpRequest (req, options) {
3233
? req.headers['x-forwarded-for'] || req.connection.remoteAddress
3334
: req.connection.remoteAddress.replace(REMOVE_IPV4_MAPPED_IPV6_RE, '') // force ipv4
3435
params.addr = (common.IPV6_RE.test(params.ip) ? '[' + params.ip + ']' : params.ip) + ':' + params.port
35-
} else if (s[0] === '/scrape') {
36+
} else if (options.action === 'scrape' || s[0] === '/scrape') {
3637
params.action = common.ACTIONS.SCRAPE
3738
if (typeof params.info_hash === 'string')
3839
params.info_hash = [ params.info_hash ]

server.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,12 +121,14 @@ Server.prototype.getSwarm = function (infoHash) {
121121
return swarm
122122
}
123123

124-
Server.prototype.onHttpRequest = function (req, res) {
124+
Server.prototype.onHttpRequest = function (req, res, options) {
125125
var self = this
126+
options = options || {}
127+
options.trustProxy = options.trustProxy || self._trustProxy
126128

127129
var params
128130
try {
129-
params = parseHttpRequest(req, { trustProxy: self._trustProxy })
131+
params = parseHttpRequest(req, options)
130132
params.httpReq = req
131133
params.httpRes = res
132134
} catch (err) {

0 commit comments

Comments
 (0)