-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
serveio.ts
449 lines (434 loc) · 12 KB
/
serveio.ts
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
// Copyright 2019 Yusuke Sakurai. All rights reserved. MIT license.
import {
BufReader,
BufWriter
} from "https://deno.land/std@v0.12.0/io/bufio.ts";
import { TextProtoReader } from "https://deno.land/std@v0.12.0/textproto/mod.ts";
import {
BodyReader,
ChunkedBodyReader,
readUntilEof,
TimeoutReader
} from "./readers.ts";
import { defer, promiseInterrupter } from "./promises.ts";
import { assert } from "https://deno.land/std@v0.12.0/testing/asserts.ts";
import {
ClientRequest,
IncomingHttpRequest,
IncomingHttpResponse,
KeepAlive,
ServeOptions,
ServerResponse
} from "./server.ts";
import Reader = Deno.Reader;
import Writer = Deno.Writer;
import Buffer = Deno.Buffer;
import { encode } from "https://deno.land/std@v0.12.0/strings/encode.ts";
import { decode } from "https://deno.land/std@v0.12.0/strings/decode.ts";
import EOF = Deno.EOF;
function bufReader(r: Reader): BufReader {
if (r instanceof BufReader) {
return r;
} else {
return new BufReader(r);
}
}
const kDefaultKeepAliveTimeout = 75000; // ms
export function initServeOptions(opts: ServeOptions = {}): ServeOptions {
let cancel = opts.cancel;
let keepAliveTimeout = kDefaultKeepAliveTimeout;
let readTimeout = kDefaultKeepAliveTimeout;
if (opts.keepAliveTimeout !== void 0) {
keepAliveTimeout = opts.keepAliveTimeout;
}
if (opts.readTimeout !== void 0) {
readTimeout = opts.readTimeout;
}
assert(keepAliveTimeout >= 0, "keepAliveTimeout must be >= 0");
assert(readTimeout >= 0, "readTimeout must be >= 0");
return { cancel, keepAliveTimeout, readTimeout };
}
/**
* read http request from reader
* status-line and headers are certainly read. body and trailers may not be read
* read will be aborted when opts.cancel is called or any read wait to reader is over opts.readTimeout
* */
export async function readRequest(
r: Reader,
opts: ServeOptions = {}
): Promise<IncomingHttpRequest> {
opts = initServeOptions(opts);
const reader = bufReader(r);
const tpReader = new TextProtoReader(reader);
// read status line
// use keepAliveTimeout for reading request line
const resLine = await promiseInterrupter({
timeout: opts.keepAliveTimeout,
cancel: opts.cancel
})(tpReader.readLine());
if (resLine === EOF) {
throw EOF;
}
const [m, method, url, proto] = resLine.match(/^([^ ]+)? ([^ ]+?) ([^ ]+?)$/);
// read header
const headers = await promiseInterrupter({
timeout: opts.readTimeout,
cancel: opts.cancel
})(tpReader.readMIMEHeader());
if (headers === EOF) {
throw EOF;
}
let keepAlive;
if (headers.has("keep-alive")) {
keepAlive = parseKeepAlive(headers);
}
// body
let body: Reader;
let trailers: Headers;
let finalizers = [];
const finalize = async () => {
for (const f of finalizers) {
await f();
}
};
if (method === "POST" || method === "PUT") {
finalizers.push(async () => {
await readUntilEof(body);
});
if (headers.get("transfer-encoding") === "chunked") {
if (headers.has("trailer")) {
finalizers.push(async () => {
trailers = await readTrailers(reader, headers);
});
}
body = new TimeoutReader(new ChunkedBodyReader(reader), {
timeout: opts.readTimeout,
cancel: opts.cancel
});
} else {
const contentLength = parseInt(headers.get("content-length"));
assert(
contentLength >= 0,
`content-length is missing or invalid: ${headers.get("content-length")}`
);
body = new TimeoutReader(new BodyReader(reader, contentLength), {
timeout: opts.readTimeout,
cancel: opts.cancel
});
}
}
return {
method,
url,
proto,
headers,
body,
keepAlive,
get trailers() {
return trailers;
},
finalize
};
}
/** write http request. Host, Content-Length, Transfer-Encoding headers are set if needed */
export async function writeRequest(
w: Writer,
req: ClientRequest
): Promise<void> {
const writer = bufWriter(w);
let { method, body, headers } = req;
method = method.toUpperCase();
const url = new URL(req.url);
if (!headers) {
headers = new Headers();
}
// start line
await writer.write(
encode(`${method} ${url.pathname}${url.search || ""} HTTP/1.1\r\n`)
);
// header
if (!headers.has("Host")) {
headers.set("Host", url.host);
}
let contentLength: number;
if (body) {
if (headers.has("Content-Length")) {
const cl = headers.get("Content-Length");
contentLength = parseInt(cl);
assert(
Number.isInteger(contentLength),
`content-length is not number: ${cl}`
);
} else if (body instanceof Uint8Array) {
contentLength = body.byteLength;
headers.set("Content-Length", `${body.byteLength}`);
} else {
headers.set("Transfer-Encoding", "chunked");
}
}
await writeHeaders(writer, headers);
await writer.flush();
if (body) {
await writeBody(writer, body, contentLength);
}
}
/** read http response from reader */
export async function readResponse(
r: Reader,
opts?: { timeout?: number; cancel?: Promise<void> }
): Promise<IncomingHttpResponse> {
let timeout = -1;
if (opts && Number.isInteger(opts.timeout)) {
timeout = opts.timeout;
}
let cancel = defer().promise;
if (opts && opts.cancel) {
cancel = opts.cancel;
}
const reader = bufReader(r);
const tp = new TextProtoReader(reader);
const timeoutOrCancel = promiseInterrupter({ timeout, cancel });
// First line: HTTP/1,1 200 OK
const line = await timeoutOrCancel(tp.readLine());
if (line === EOF) {
throw EOF;
}
const [proto, status, statusText] = line.split(" ", 3);
const headers = await timeoutOrCancel(tp.readMIMEHeader());
if (headers === EOF) {
throw EOF;
}
const contentLength = headers.get("content-length");
const isChunked = headers.get("transfer-encoding") === "chunked";
let body: Reader;
let finalizers = [
async () => {
await readUntilEof(body);
}
];
const finalize = async () => {
for (const f of finalizers) {
await f();
}
};
let trailers: Headers;
if (isChunked) {
if (headers.has("trailer")) {
finalizers.push(async () => {
trailers = await readTrailers(reader, headers);
});
}
body = new TimeoutReader(new ChunkedBodyReader(reader), {
timeout,
cancel
});
} else {
body = new TimeoutReader(new BodyReader(reader, parseInt(contentLength)), {
timeout,
cancel
});
}
return {
proto,
status: parseInt(status),
statusText,
headers,
body,
get trailers() {
return trailers;
},
finalize
};
}
function bufWriter(w: Writer) {
if (w instanceof BufWriter) {
return w;
} else {
return new BufWriter(w);
}
}
const kHttpStatusCodes = {
100: "Continue",
101: "Switching Protocols",
102: "Processing",
103: "Early Hints",
200: "OK",
201: "Created",
202: "Accepted",
301: "Moved Permanently",
302: "Found",
304: "Not Modified",
400: "Bad Request",
401: "Unauthorized",
403: "Forbidden",
404: "Not Found",
500: "Internal Server Error"
};
/** write http response to writer. Content-Length, Transfer-Encoding headers are set if needed */
export async function writeResponse(
w: Writer,
res: ServerResponse
): Promise<void> {
const writer = bufWriter(w);
if (res.headers === void 0) {
res.headers = new Headers();
}
// status line
const statusText = kHttpStatusCodes[res.status];
assert(!!statusText, `unsupported status code: ${statusText}`);
await writer.write(encode(`HTTP/1.1 ${res.status} ${statusText}\r\n`));
if (res.body && !res.headers.has("content-length")) {
if (res.body instanceof Uint8Array) {
res.headers.set("content-length", `${res.body.byteLength}`);
} else if (!res.headers.has("transfer-encoding")) {
res.headers.set("transfer-encoding", "chunked");
}
}
await writeHeaders(writer, res.headers);
if (res.body) {
let contentLength;
if (res.headers.has("content-length")) {
contentLength = parseInt(res.headers.get("content-length"));
}
await writeBody(writer, res.body, contentLength);
}
}
/** write headers to writer */
export async function writeHeaders(w: Writer, headers: Headers): Promise<void> {
const lines = [];
const writer = bufWriter(w);
for (const [key, value] of headers) {
lines.push(`${key}: ${value}`);
}
lines.push("\r\n");
const headerText = lines.join("\r\n");
await writer.write(encode(headerText));
await writer.flush();
}
/** write http body to writer. Reader without contentLength will be written by chunked encoding */
export async function writeBody(
w: Writer,
body: Uint8Array | Reader,
contentLength?: number
): Promise<void> {
if (!body) return;
const buf = new Uint8Array(1024);
let writer = bufWriter(w);
const reader = body instanceof Uint8Array ? new Buffer(body) : body;
const hasContentLength = Number.isInteger(contentLength);
while (true) {
const result = await reader.read(buf);
if (result === EOF) {
if (!hasContentLength) {
await writer.write(encode("0\r\n\r\n"));
await writer.flush();
}
break;
} else if (result > 0) {
const chunk = buf.slice(0, result);
if (hasContentLength) {
await writer.write(chunk);
} else {
const size = chunk.byteLength.toString(16);
await writer.write(encode(`${size}\r\n`));
await writer.write(chunk);
await writer.write(encode("\r\n"));
}
await writer.flush();
}
}
}
const kProhibitedTrailerHeaders = [
"transfer-encoding",
"content-length",
"trailer"
];
/** write trailer headers to writer. it mostly should be called after writeResponse */
export async function writeTrailers(
w: Writer,
headers: Headers,
trailers: Headers
): Promise<void> {
assert(
headers.has("trailer"),
'response headers must have "trailer" header field'
);
const transferEncoding = headers.get("transfer-encoding");
assert(
transferEncoding === "chunked",
`trailer headers is only allowed for "transfer-encoding: chunked": got "${transferEncoding}"`
);
const writer = bufWriter(w);
const trailerHeaderFields = headers
.get("trailer")
.split(",")
.map(s => s.trim().toLowerCase());
for (const f of trailerHeaderFields) {
assert(
!kProhibitedTrailerHeaders.includes(f),
`"${f}" is prohibited for trailer header`
);
}
for (const [key, value] of trailers) {
assert(
trailerHeaderFields.includes(key),
`Not trailed header field: ${key}`
);
await writer.write(encode(`${key}: ${value}\r\n`));
}
await writer.flush();
}
/** read trailer headers from reader. it should mostly be called after readRequest */
export async function readTrailers(
r: Reader,
headers: Headers
): Promise<Headers> {
const h = new Headers();
const reader = bufReader(r);
const trailerHeaderFields = headers
.get("trailer")
.split(",")
.map(s => s.trim().toLowerCase());
for (const field of trailerHeaderFields) {
assert(
kProhibitedTrailerHeaders.indexOf(field) < 0,
`"${field}" is prohibited for trailer field`
);
}
for (let i = 0; i < trailerHeaderFields.length; i++) {
const readLine = await reader.readLine();
if (readLine === EOF) {
throw EOF;
}
const [_, field, value] = decode(readLine.line)
.trim()
.match(/^([^ :]+?):(.+?)$/);
assert(
trailerHeaderFields.includes(field),
`unexpected trailer field: ${field}`
);
h.set(field.trim(), value.trim());
}
return h;
}
export function parseKeepAlive(h: Headers): KeepAlive {
let timeout;
let max;
const kv = h
.get("keep-alive")
.split(",")
.map(s => s.trim().split("="));
for (const [key, value] of kv) {
if (key === "timeout") {
timeout = parseInt(value);
assert(
Number.isInteger(timeout),
`"timeout" must be integer: ${timeout}`
);
} else if (key === "max") {
max = parseInt(value);
assert(Number.isInteger(max), `"max" max be integer: ${max}`);
}
}
return { timeout, max };
}