Skip to content

Commit 76f6d41

Browse files
authored
Merge pull request webtorrent#362 from webtorrent/fix_udp_url
2 parents 2a12b75 + ce7dd6e commit 76f6d41

File tree

4 files changed

+52
-15
lines changed

4 files changed

+52
-15
lines changed

client.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ class Client extends EventEmitter {
8686
.map(announceUrl => {
8787
let parsedUrl
8888
try {
89-
parsedUrl = new URL(announceUrl)
89+
parsedUrl = common.parseUrl(announceUrl)
9090
} catch (err) {
9191
nextTickWarn(new Error(`Invalid tracker URL: ${announceUrl}`))
9292
return null

lib/client/udp-tracker.js

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -74,20 +74,7 @@ class UDPTracker extends Tracker {
7474
const self = this
7575
if (!opts) opts = {}
7676

77-
// HACK: Fix for WHATWG URL object not parsing non-standard URL schemes like
78-
// 'udp:'. Just replace it with 'http:' since we only need the `hostname`
79-
// and `port` properties.
80-
//
81-
// Note: Only affects Chrome and Firefox. Works fine in Node.js, Safari, and
82-
// Edge.
83-
//
84-
// Note: UDP trackers aren't used in the normal browser build, but they are
85-
// used in a Chrome App build (i.e. by Brave Browser).
86-
//
87-
// Bug reports:
88-
// - Chrome: https://bugs.chromium.org/p/chromium/issues/detail?id=734880
89-
// - Firefox: https://bugzilla.mozilla.org/show_bug.cgi?id=1374505
90-
let { hostname, port } = new URL(this.announceUrl.replace(/^udp:/, 'http:'))
77+
let { hostname, port } = common.parseUrl(this.announceUrl)
9178
if (port === '') port = 80
9279

9380
let transactionId = genTransactionId()

lib/common.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,31 @@ exports.hexToBinary = function (str) {
1919
return Buffer.from(str, 'hex').toString('binary')
2020
}
2121

22+
// HACK: Fix for WHATWG URL object not parsing non-standard URL schemes like
23+
// 'udp:'. Just replace it with 'http:' since we only need a few properties.
24+
//
25+
// Note: Only affects Chrome and Firefox. Works fine in Node.js, Safari, and
26+
// Edge.
27+
//
28+
// Note: UDP trackers aren't used in the normal browser build, but they are
29+
// used in a Chrome App build (i.e. by Brave Browser).
30+
//
31+
// Bug reports:
32+
// - Chrome: https://bugs.chromium.org/p/chromium/issues/detail?id=734880
33+
// - Firefox: https://bugzilla.mozilla.org/show_bug.cgi?id=1374505
34+
exports.parseUrl = function (str) {
35+
const url = new URL(str.replace(/^udp:/, 'http:'))
36+
37+
if (str.match(/^udp:/)) {
38+
Object.defineProperties(url, {
39+
href: { value: url.href.replace(/^http/, 'udp') },
40+
protocol: { value: url.protocol.replace(/^http/, 'udp') },
41+
origin: { value: url.origin.replace(/^http/, 'udp') }
42+
})
43+
}
44+
45+
return url
46+
}
47+
2248
const config = require('./common-node')
2349
Object.assign(exports, config)

test/client.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,10 +534,34 @@ test('http: invalid tracker port', function (t) {
534534
testUnsupportedTracker(t, 'http://127.0.0.1:69691337/announce')
535535
})
536536

537+
test('http: invalid tracker url', function (t) {
538+
testUnsupportedTracker(t, 'http:')
539+
})
540+
541+
test('http: invalid tracker url with slash', function (t) {
542+
testUnsupportedTracker(t, 'http://')
543+
})
544+
537545
test('udp: invalid tracker port', function (t) {
538546
testUnsupportedTracker(t, 'udp://127.0.0.1:69691337')
539547
})
540548

549+
test('udp: invalid tracker url', function (t) {
550+
testUnsupportedTracker(t, 'udp:')
551+
})
552+
553+
test('udp: invalid tracker url with slash', function (t) {
554+
testUnsupportedTracker(t, 'udp://')
555+
})
556+
541557
test('ws: invalid tracker port', function (t) {
542558
testUnsupportedTracker(t, 'ws://127.0.0.1:69691337')
543559
})
560+
561+
test('ws: invalid tracker url', function (t) {
562+
testUnsupportedTracker(t, 'ws:')
563+
})
564+
565+
test('ws: invalid tracker url with slash', function (t) {
566+
testUnsupportedTracker(t, 'ws://')
567+
})

0 commit comments

Comments
 (0)