-
Notifications
You must be signed in to change notification settings - Fork 0
/
node-http-server.coffee
78 lines (58 loc) · 1.88 KB
/
node-http-server.coffee
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
respond_error = (res, error="") ->
console.log '----- Error, 500-ing -----'
console.log error
console.log '--------------------------'
res.status(500).render('500')
respond_js = (res, code, js) ->
_respond_text res, code, 'text/javascript', js
respond_json = (res, code, x) ->
_respond_text res, code, 'application/json', JSON.stringify x, null, 2
respond_jsonp = (req, res, x) ->
{callback} = req.query
text = JSON.stringify x, null, 2
text = "#{callback}(#{text})" if callback
_respond_text res, 200, 'text/javascript', text
respond_not_found = (res) ->
res.status(404).render('404')
respond_storage_file = ({req, res, storage, key, mime}) ->
{range} = req.headers
storage.size key, (e, size) ->
return respond_not_found res if e and e.notFound
return respond_error res, e if e
# No Range header?
if not range
res.writeHead 200, {'Content-Type': mime, 'Content-Length': size}
storage.createReadStream(key).pipe res
return
m = range.match /^bytes=([0-9]+)-/
start = parseInt m[1], 10
m = range.match /^bytes=([0-9]+)-([0-9]+)/
end = if m then parseInt(m[2], 10) else (size - 1)
res_size = end - start + 1
# invalid range?
if (start > end) or ((end + 1) > size)
return respond_plain res, 416, "invalid range"
res.writeHead 206, {
'Content-Type': mime
'Content-Length': res_size
'Content-Range': "bytes #{start}-#{end}/#{size}"
}
storage.createReadStream(key, {start, end}).pipe res
respond_plain = (res, code, text) ->
_respond_text res, code, 'text/plain', text
_respond_text = (res, code, mime, text) ->
data = new Buffer text, 'utf8'
res.writeHead code, {
'Content-Type': mime
'Content-Length': data.length
}
res.end data
module.exports = {
respond_error
respond_js
respond_json
respond_jsonp
respond_not_found
respond_storage_file
respond_plain
}