-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
server.js
144 lines (119 loc) · 4.13 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
module.exports = Server
var arrayRemove = require('unordered-array-remove')
var debug = require('debug')('webtorrent:server')
var http = require('http')
var mime = require('mime')
var pump = require('pump')
var rangeParser = require('range-parser')
var url = require('url')
function Server (torrent, opts) {
var server = http.createServer(opts)
var sockets = []
var pendingReady = []
var closed = false
server.on('connection', onConnection)
server.on('request', onRequest)
var _close = server.close
server.close = function (cb) {
closed = true
server.removeListener('connection', onConnection)
server.removeListener('request', onRequest)
while (pendingReady.length) {
var onReady = pendingReady.pop()
torrent.removeListener('ready', onReady)
}
torrent = null
_close.call(server, cb)
}
server.destroy = function (cb) {
sockets.forEach(function (socket) {
socket.destroy()
})
// Only call `server.close` if user has not called it already
if (!cb) cb = function () {}
if (closed) process.nextTick(cb)
else server.close(cb)
}
function onConnection (socket) {
socket.setTimeout(36000000)
sockets.push(socket)
socket.once('close', function () {
arrayRemove(sockets, sockets.indexOf(socket))
})
}
function onRequest (req, res) {
debug('onRequest')
// Allow CORS requests to specify arbitrary headers, e.g. 'Range',
// by responding to the OPTIONS preflight request with the specified
// origin and requested headers.
if (req.method === 'OPTIONS' && req.headers['access-control-request-headers']) {
res.setHeader('Access-Control-Allow-Methods', 'POST, GET, OPTIONS')
res.setHeader(
'Access-Control-Allow-Headers',
req.headers['access-control-request-headers']
)
res.setHeader('Access-Control-Max-Age', '1728000')
return res.end()
}
if (req.headers.origin) {
res.setHeader('Access-Control-Allow-Origin', req.headers.origin)
}
var pathname = url.parse(req.url).pathname
if (pathname === '/favicon.ico') return res.end()
if (torrent.ready) {
onReady()
} else {
pendingReady.push(onReady)
torrent.once('ready', onReady)
}
function onReady () {
arrayRemove(pendingReady, pendingReady.indexOf(onReady))
if (pathname === '/') {
res.setHeader('Content-Type', 'text/html')
var listHtml = torrent.files.map(function (file, i) {
return '<li><a download="' + file.name + '" href="/' + i + '">' + file.path + '</a> ' +
'(' + file.length + ' bytes)</li>'
}).join('<br>')
var html = '<h1>' + torrent.name + '</h1><ol>' + listHtml + '</ol>'
return res.end(html)
}
var index = Number(pathname.slice(1))
if (Number.isNaN(index) || index >= torrent.files.length) {
res.statusCode = 404
return res.end('404 Not Found')
}
var file = torrent.files[index]
res.setHeader('Accept-Ranges', 'bytes')
res.setHeader('Content-Type', mime.lookup(file.name))
res.statusCode = 200
// Support DLNA streaming
res.setHeader('transferMode.dlna.org', 'Streaming')
res.setHeader(
'contentFeatures.dlna.org',
'DLNA.ORG_OP=01;DLNA.ORG_CI=0;DLNA.ORG_FLAGS=01700000000000000000000000000000'
)
// `rangeParser` returns an array of ranges, or an error code (number) if
// there was an error parsing the range.
var range = rangeParser(file.length, req.headers.range || '')
if (Array.isArray(range)) {
// no support for multi-range request, just use the first range
range = range[0]
res.statusCode = 206
debug('range %s', JSON.stringify(range))
res.setHeader(
'Content-Range',
'bytes ' + range.start + '-' + range.end + '/' + file.length
)
res.setHeader('Content-Length', range.end - range.start + 1)
} else {
range = null
res.setHeader('Content-Length', file.length)
}
if (req.method === 'HEAD') {
return res.end()
}
pump(file.createReadStream(range), res)
}
}
return server
}