forked from for-GET/bin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.coffee
229 lines (209 loc) · 6.68 KB
/
index.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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
fs = require 'fs'
zlib = require 'zlib'
querystring = require 'querystring'
README = fs.readFileSync __dirname + '/README.md', 'utf8'
express = require 'express'
js2xml = require 'js2xmlparser'
xml2js = require 'xml2js'
_ = require 'lodash'
httpWell = require 'know-your-http-well'
module.exports = app = express()
# HELPER - CONTENT-TYPE NEGOCIATION
# FIXME replace with otw
negociateContent = (req, res, body) ->
accept = req.headers.accept or ''
return ['text/plain', body] if typeof body is 'string'
return ['application/xml', js2xml 'response', body] if /\bxml\b/.test accept
['application/json', JSON.stringify body, null, 2]
# HELPER - ENCODING NEGOCIATION
# FIXME replace with otw
negociateEncoding = (req, res, body, callback) ->
encoding = req.headers['accept-encoding'] or ''
encoding += ', ' + (res.get('Content-Encoding') or '')
if /\bdeflate\b/.test encoding
zlib.deflate new Buffer(body, 'utf-8'), (err, result) ->
callback err, ['deflate', result]
else if /\bgzip\b/.test encoding
zlib.gzip new Buffer(body, 'utf-8'), (err, result) ->
callback err, ['gzip', result]
else
callback null, [undefined, body]
# HELPER - GZIP,DEFLATE,RAW
send = (body = '', req, res, next) ->
[contentType, body] = negociateContent req, res, body
res.set 'Content-Type', contentType unless res.get 'Content-Type'
negociateEncoding req, res, body, (err, [encoding, body]) ->
return res.send 500, err if err
res.set 'Content-Encoding', encoding unless res.get 'Content-Encoding'
res.send body
# HELPER - JSON TRACE
fakeTrace = (req, res, next) ->
trace =
method: req.method
uri: req.url
httpVersion: req.httpVersion
headers: req.headers
body: req.body
cookies: req.cookies
send trace, req, res, next
# APP
app.set 'strict routing'
app.disable 'x-powered-by'
app.use (req, res, next) ->
req.rawBody = ''
req.setEncoding 'utf8'
req.on 'data', (chunk) -> req.rawBody += chunk
req.on 'end', () ->
req.body = req.rawBody
if /\bjson\b/.test(req.headers['content-type'] or '')
try
req.body = JSON.parse req.rawBody
catch e
return res.send 400
next()
else if /\bxml\b/.test(req.headers['content-type'] or '')
try
xml2js.parseString req.rawBody, (err, result) ->
return res.send 400 if err
req.body = result
next()
catch e
return res.send 400
else if req.headers['content-type'] is 'application/x-www-form-urlencoded'
try
req.body = querystring.parse req.rawBody
catch e
return res.send 400
next()
app.use express.cookieParser()
# ORIGIN IP
app.use (req, res, next) ->
res.set 'X-Originating-IP', req.socket.remoteAddress
next()
# METHOD OVERRIDE
app.use (req, res, next) ->
method = req.headers['x-http-method-override']
return next() unless method
return res.send 400 if req.method isnt 'POST'
req.method = method.toUpperCase()
next()
# TRACE
app.use (req, res, next) ->
return next() unless req.method.toUpperCase() is 'TRACE'
if req.accepts 'message/http'
meta = ["#{req.method} #{req.url} HTTP/#{req.httpVersion}"]
meta.push "#{header}: #{headerValue}" for header, headerValue of req.headers
meta = meta.join '\n'
body = req.body or ''
res.set 'Content-Type', 'message/http'
send "#{meta}\n#{body}", req, res, next
else
fakeTrace req, res, next
# PREFER
app.use (req, res, next) ->
# FIXME replace with otw.tokenizedHeader
prefer = req.headers['x-prefer']
return next() unless prefer
prefer = prefer.split ','
result = {}
for pref, prefIndex in prefer
[key, value] = prefer[prefIndex].trim().split '='
value or= 'true'
if result[key]
result[key] = [result[key]] unless Array.isArray result[key]
result[key].push value
else
result[key] = value
req.prefer = result
next()
# PREFER STATUS
app.use (req, res, next) ->
status = req.prefer?.status
return next() unless status?
status = status[0] if Array.isArray status
res.status status
next()
# PREFER COOKIE
app.use (req, res, next) ->
cookies = req.prefer?.cookie
return next() unless cookies?
cookies = [cookies] unless Array.isArray cookies
for cookie in cookies
[name, value] = cookie.split '|'
if value
res.cookie name, value
else
res.clearCookie name
next()
# PREFER WAIT
app.use (req, res, next) ->
wait = req.prefer?.wait
return next() unless wait?
wait = wait[0] if Array.isArray wait
setTimeout (() -> next()), wait * 1000
# PREFER RETURN-MINIMAL
app.use (req, res, next) ->
returnMinimal = req.prefer?['return-minimal']
returnMinimal = returnMinimal[0] if Array.isArray returnMinimal
return next() unless returnMinimal is 'true'
res.send()
# PREFER RETURN-REQUEST
app.use (req, res, next) ->
returnRequest = req.prefer?['return-request']
returnRequest = returnRequest[0] if Array.isArray returnRequest
return next() unless returnRequest is 'true'
return next() unless req.get('Content-Type') is 'application/json' and req.body?
res.status req.body.status if req.body.status?
headers = req.body.headers or {}
res.set header, headerValue for header, headerValue of headers
return next() unless req.body.body?
send req.body.body, req, res, next
# ROUTES
app.use app.router
# know-your-http-well
hitFun = {}
hitFun.method = (value) ->
_.findWhere httpWell.methods, (method) ->
method.method.toUpperCase() is value.toUpperCase()
hitFun.header = (value) ->
_.findWhere httpWell.headers, (header) ->
header.header.toLowerCase() is value.toLowerCase()
hitFun.statusCode = (value) ->
_.findWhere httpWell.statusCodes, (statusCode) ->
statusCode.code is value
hitFun.relation = (value) ->
_.findWhere httpWell.relations, (relation) ->
relation.relation.toLowerCase() is value.toLowerCase()
app.get '/spec/:value', (req, res, next) ->
specs = [
'method'
'header'
'statusCode'
'relation'
]
for spec in specs
hit = hitFun[spec] req.params.value
return res.redirect hit.spec_href if hit
res.send 404
app.get '/:spec/:value', (req, res, next) ->
return next() unless req.params.spec in [
'statusCode'
'method'
'header'
'relation'
]
hit = hitFun[spec] req.params.value
return res.redirect hit.spec_href if hit
res.send 404
# ...
app.all '*', (req, res, next) ->
if req.accepts 'text/plain'
res.set 'Content-Type', 'text/plain'
send README, req, res, next
else if req.accepts('application/json') or req.accepts('application/xml')
fakeTrace req, res, next
else
returnRepr = req.prefer?['return-representation']
returnRepr = returnRepr[0] if Array.isArray returnRepr
return res.send() unless returnRepr is 'true'
fakeTrace req, res, next