@@ -197,14 +197,7 @@ Tracker.prototype._announce = function (opts) {
197197Tracker . prototype . scrape = function ( ) {
198198 var self = this
199199
200- if ( ! self . _scrapeUrl ) {
201- var announce = 'announce'
202- var i = self . _announceUrl . lastIndexOf ( '/' ) + 1
203-
204- if ( i >= 1 && self . _announceUrl . slice ( i , i + announce . length ) === announce ) {
205- self . _scrapeUrl = self . _announceUrl . slice ( 0 , i ) + 'scrape' + self . _announceUrl . slice ( i + announce . length )
206- }
207- }
200+ self . _scrapeUrl = self . _scrapeUrl || getScrapeUrl ( self . _announceUrl )
208201
209202 if ( ! self . _scrapeUrl ) {
210203 debug ( 'scrape not supported by ' + self . _announceUrl )
@@ -213,7 +206,7 @@ Tracker.prototype.scrape = function () {
213206 }
214207
215208 debug ( 'sent `scrape` to ' + self . _announceUrl )
216- self . _requestImpl ( self . _scrapeUrl )
209+ self . _requestImpl ( self . _scrapeUrl , { _scrape : true } )
217210}
218211
219212Tracker . prototype . setInterval = function ( intervalMs ) {
@@ -229,7 +222,7 @@ Tracker.prototype.setInterval = function (intervalMs) {
229222Tracker . prototype . _requestHttp = function ( requestUrl , opts ) {
230223 var self = this
231224
232- if ( isScrapeUrl ( requestUrl ) ) {
225+ if ( opts . _scrape ) {
233226 opts = extend ( {
234227 info_hash : self . client . _infoHash . toString ( 'binary' )
235228 } , opts )
@@ -307,7 +300,7 @@ Tracker.prototype._requestUdp = function (requestUrl, opts) {
307300 return error ( 'invalid udp handshake' )
308301 }
309302
310- if ( isScrapeUrl ( requestUrl ) ) {
303+ if ( opts . _scrape ) {
311304 scrape ( msg . slice ( 8 , 16 ) )
312305 } else {
313306 announce ( msg . slice ( 8 , 16 ) , opts )
@@ -511,3 +504,16 @@ function toUInt64 (n) {
511504function isScrapeUrl ( u ) {
512505 return u . substr ( u . lastIndexOf ( '/' ) + 1 , 'scrape' . length ) === 'scrape'
513506}
507+
508+ var UDP_TRACKER = / ^ u d p : \/ \/ /
509+ var HTTP_SCRAPE_SUPPORT = / \/ ( a n n o u n c e ) [ ^ \/ ] * $ /
510+
511+ function getScrapeUrl ( announceUrl ) {
512+ if ( announceUrl . match ( UDP_TRACKER ) ) return announceUrl
513+ var match
514+ if ( match = announceUrl . match ( HTTP_SCRAPE_SUPPORT ) ) {
515+ var i = match . index
516+ return announceUrl . slice ( 0 , i ) + '/scrape' + announceUrl . slice ( i + 9 )
517+ }
518+ return null
519+ }
0 commit comments