Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions client.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ var EventEmitter = require('events').EventEmitter
var extend = require('extend.js')
var hat = require('hat')
var http = require('http')
var https = require('https')
var inherits = require('inherits')
var once = require('once')
var url = require('url')
Expand Down Expand Up @@ -52,7 +53,8 @@ function Client (peerId, port, torrent, opts) {
if (typeof torrent.announce === 'string') torrent.announce = [ torrent.announce ]
self._trackers = (torrent.announce || [])
.filter(function (announceUrl) {
return announceUrl.indexOf('udp://') === 0 || announceUrl.indexOf('http://') === 0
var protocol = url.parse(announceUrl).protocol
return protocol === 'udp:' || protocol === 'http:' || protocol === 'https:'
})
.map(function (announceUrl) {
return new Tracker(self, announceUrl, self._opts)
Expand Down Expand Up @@ -150,9 +152,10 @@ function Tracker (client, announceUrl, opts) {
self._intervalMs = self.client._intervalMs // use client interval initially
self._interval = null

if (self._announceUrl.indexOf('udp://') === 0) {
var protocol = url.parse(self._announceUrl).protocol
if (protocol === 'udp:') {
self._requestImpl = self._requestUdp
} else if (self._announceUrl.indexOf('http://') === 0) {
} else if (protocol === 'http:' || protocol === 'https:') {
self._requestImpl = self._requestHttp
}
}
Expand Down Expand Up @@ -265,9 +268,10 @@ Tracker.prototype._requestHttp = function (requestUrl, opts) {
}
}

var protocol = url.parse(self._announceUrl).protocol
var fullUrl = requestUrl + '?' + common.querystringStringify(opts)

var req = http.get(fullUrl, function (res) {
var req = (protocol === 'https:' ? https : http).get(fullUrl, function (res) {
if (res.statusCode !== 200) {
res.resume() // consume the whole stream
self.client.emit('warning', new Error('Invalid response code ' + res.statusCode + ' from tracker ' + requestUrl))
Expand Down