-
Notifications
You must be signed in to change notification settings - Fork 1
/
connection.js
293 lines (272 loc) · 8.63 KB
/
connection.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
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
const qs = require('qs')
const iconv = require('iconv-lite')
const contentType = require('content-type')
const multipart = require('multipart-formdata')
const { Readable } = require('stream')
const ServerError = require('./errors')
const utils = require('./utils')
const { cache, templateEngine, maxBodySize } = require('./constants')
const methodsWithBody = ['POST', 'PUT', 'PATCH', 'OPTIONS']
class Connection {
constructor (app, request, response, wsContext) {
this.app = app
this.request = request
this.response = response
this.headers = {}
this.request.forEach((k, v) => {
if (k in this.headers) {
this.headers[k] = [].concat(this.headers[k], v)
} else {
this.headers[k] = v
}
})
this.url = this.request.getUrl()
this.method = this.request.getMethod().toUpperCase()
this.rawQuery = this.request.getQuery()
this._req_info = {}
this._method = null
this._body = null
this._bodyStream = null
this._reject_data = null
this._on_aborted = []
this._on_writable = null
this.aborted = false
this.upgraded = false
this.response.onAborted(() => {
this._on_aborted.forEach(call => call())
this.aborted = true
})
this.response.onWritable((offset) =>
this._on_writable ? this._on_writable(offset) : true)
this.wsContext = wsContext
this._remote_address = null
}
static create (app, request, response, wsContext = null) {
return new Connection(app, request, response, wsContext)
}
processBodyData () {
if (!this.response) return
if (!methodsWithBody.includes(this.method)) return
const contentLength = this.headers['content-length']
// Verify Content-Length
if (!contentLength) {
throw new ServerError({ code: 'CLIENT_NO_LENGTH', message: '', httpCode: 411 })
} else if (!/^[1-9]\d*$/.test(contentLength)) {
throw new ServerError({ code: 'CLIENT_LENGTH_INVALID', message: '', httpCode: 400 })
} else if (this.bodyLimit && Number(contentLength) > this.bodyLimit) {
throw new ServerError({ code: 'CLIENT_LENGTH_TOO_LARGE', message: '', httpCode: 413 })
}
this.bodyLength = Number(contentLength || 0)
this._buffer = []
this._dataEnd = false
this._received = 0
this._onData = null
this._dataError = null
this.onAborted(() => {
this._dataEnd = true
this._dataError = new ServerError({ code: 'CONNECTION_ABORTED' })
if (this._onData) {
this._onData()
}
})
this.response.onData((chunk, isLast) => {
if (this._dataEnd) return
this._received += chunk.byteLength
if (this.bodyLength && this._received > this.bodyLength) {
this._dataEnd = true
this._dataError = new ServerError({ code: 'CLIENT_BAD_REQUEST', httpCode: 400 })
} else if (this.bodyLength && isLast && this._received < this.bodyLength) {
this._dataEnd = true
this._dataError = new ServerError({ code: 'CLIENT_BAD_REQUEST', httpCode: 400 })
} else {
// Copy buffer to avoid memory release
this._buffer.push(Buffer.from(Buffer.from(chunk)))
this._dataEnd = isLast
}
if (this._onData) {
this._onData()
}
})
}
bodyDataStream () {
if (!this.response) {
throw new ServerError({ code: 'SERVER_INVALID_CONNECTION' })
}
if (!methodsWithBody.includes(this.method)) {
throw new ServerError({
code: 'SERVER_INVALID_OPERATE',
message: `The method "${this.method}" should not have body.`
})
}
if (this._bodyStream !== null) {
return this._bodyStream
}
const readData = (callback) => {
if (this._dataError) {
callback(this._dataError)
} else {
if (!this._buffer.length && !this._dataEnd) {
this._onData = () => readData(callback)
} else {
this._onData = null
const chunk = this._buffer.shift()
if (!chunk && this._dataEnd) callback(null, null)
else callback(null, Buffer.from(chunk))
}
}
}
this._bodyStream = new Readable({
read () {
readData((err, chunk) => {
if (err) this.destroy(err)
else this.push(chunk)
})
}
})
this._bodyStream.bodyLength = this.bodyLength
return this._bodyStream
}
bodyData () {
if (this._body !== null) {
return this._body
}
const type = this.headers['content-type'] || 'application/octet-stream'
const stream = this.bodyDataStream()
this._body = new Promise((resolve, reject) => {
let data = null
stream.on('error', reject)
stream.on('data', (chunk) => {
data = data !== null ? Buffer.concat([data, chunk]) : chunk
})
stream.on('end', () => {
try {
const content = contentType.parse(type)
// In RFC, charset default is ISO-8859-1, and it equal to latin1
const charset = content.parameters.charset || 'latin1'
if (content.type.startsWith('text/')) {
resolve(iconv.decode(data, charset))
} else if (content.type === 'application/json') {
resolve(JSON.parse(iconv.decode(data, charset)))
} else if (content.type === 'application/x-www-form-urlencoded') {
resolve(qs.parse(iconv.decode(data, charset)))
} else if (content.type === 'multipart/form-data') {
if (!content.parameters.boundary) {
throw new Error('NO_BOUNDARY')
}
resolve(multipart.parse(data, content.parameters.boundary))
} else {
resolve(data)
}
} catch (e) {
reject(new ServerError({ code: 'SERVER_BODY_PARSE', originError: e, httpCode: 400 }))
}
})
})
return this._body
}
get cacheProvider () {
return this.app.getParam(cache)
}
get renderer () {
return this.app.getParam(templateEngine)
}
get bodyLimit () {
return this.app.getParam(maxBodySize)
}
get remoteAddress () {
return this.getInfo(
'remoteAddress',
() => utils.toFraindlyIP(Buffer.from(this.response.getRemoteAddressAsText()).toString())
)
}
getInfo (name, valueFn) {
if (!this._req_info[name]) this._req_info[name] = valueFn()
return this._req_info[name]
}
onWritable (callback) {
this._on_writable = callback
}
onAborted (callback) {
if (this.aborted) {
callback()
}
this._on_aborted.push(callback)
}
writeStatus (statusText) {
if (this.aborted) {
throw new ServerError({ code: 'CONNECTION_ABORTED' })
}
if (this.upgraded) {
throw new ServerError({ code: 'SERVER_CONNECTION_HAD_UPGRADED' })
}
return this.response.writeStatus(statusText)
}
writeHeader (key, value) {
if (this.aborted) {
throw new ServerError({ code: 'CONNECTION_ABORTED' })
}
if (this.upgraded) {
throw new ServerError({ code: 'SERVER_CONNECTION_HAD_UPGRADED' })
}
return this.response.writeHeader(key, value)
}
writeBody (data, totalSize = 0) {
if (this.aborted) {
throw new ServerError({ code: 'CONNECTION_ABORTED' })
}
if (this.upgraded) {
throw new ServerError({ code: 'SERVER_CONNECTION_HAD_UPGRADED' })
}
if (totalSize) {
const [ok, done] = this.response.tryEnd(data, totalSize)
if (done) {
this.aborted = true
}
return ok
} else {
return this.response.write(data)
}
}
getWriteOffset () {
if (this.aborted) {
throw new ServerError({ code: 'CONNECTION_ABORTED' })
}
if (this.upgraded) {
throw new ServerError({ code: 'SERVER_CONNECTION_HAD_UPGRADED' })
}
return this.response.getWriteOffset()
}
end (data) {
if (!data && this.aborted) return
if (this.aborted) {
throw new ServerError({ code: 'CONNECTION_ABORTED' })
}
if (this.upgraded) {
throw new ServerError({ code: 'SERVER_CONNECTION_HAD_UPGRADED' })
}
return data ? this.response.end(data) : this.response.endWithoutBody()
}
cork (callback) {
if (this.aborted) {
throw new ServerError({ code: 'CONNECTION_ABORTED' })
}
if (this.upgraded) {
throw new ServerError({ code: 'SERVER_CONNECTION_HAD_UPGRADED' })
}
return this.response.cork(callback)
}
upgrade (data, key, protocol, extension) {
if (this.aborted) {
throw new ServerError({ code: 'CONNECTION_ABORTED' })
}
if (!this.wsContext) {
throw new ServerError({ code: 'SERVER_INVALID_OPERATE' })
}
if (this.upgraded) {
throw new ServerError({ code: 'SERVER_CONNECTION_HAD_UPGRADED' })
}
this.upgraded = true
return this.response.upgrade(data, key, protocol, extension, this.wsContext)
}
}
module.exports = Connection