summaryrefslogtreecommitdiff
path: root/std/http/racing_server.ts
diff options
context:
space:
mode:
authorYusuke Sakurai <kerokerokerop@gmail.com>2020-02-25 12:49:39 +0900
committerGitHub <noreply@github.com>2020-02-24 22:49:39 -0500
commit22f88b9f37b7e233fd4f15b73d5a2096224e56dc (patch)
treea623c34e8af0e98ed8d56d875412688cab79634b /std/http/racing_server.ts
parent5c1ab080cd8989c3ac2b3a8c919fe14052feac79 (diff)
fix: [http] Consume unread body and trailers before reading next request (#3990)
- Added `ServerRequest.finalize()`: consuming all unread body stream and trailers. - This is cleanup method for reading next request from same keep-alive connection. - Needed when handler didn't consume all body and trailers even after responding. - refactor: `ServerRequest._bodyStream()`, `ServerRequestBody` are removed. - Now using `bodyReader()` and `chunkedBodyReader()` instead. - fix: Trailers should only be read `transfer-encoding` is `chunked` and `trailer` header is set and its value is valid. - fix: use `Headers.append()` on reading trailers. - fix: delete `trailer` field from headers after reading trailers. - reorg: Several functions related to IO are moved into `http/io.ts`
Diffstat (limited to 'std/http/racing_server.ts')
-rw-r--r--std/http/racing_server.ts36
1 files changed, 29 insertions, 7 deletions
diff --git a/std/http/racing_server.ts b/std/http/racing_server.ts
index 629fef2db..0b0e5a8a5 100644
--- a/std/http/racing_server.ts
+++ b/std/http/racing_server.ts
@@ -5,12 +5,15 @@ import { delay } from "../util/async.ts";
const addr = Deno.args[1] || "127.0.0.1:4501";
const server = serve(addr);
-const body = new TextEncoder().encode("Hello 1\n");
-const body4 = new TextEncoder().encode("World 4\n");
-
-async function delayedRespond(request: ServerRequest): Promise<void> {
+function body(i: number): string {
+ return `Step${i}\n`;
+}
+async function delayedRespond(
+ request: ServerRequest,
+ step: number
+): Promise<void> {
await delay(3000);
- await request.respond({ status: 200, body });
+ await request.respond({ status: 200, body: body(step) });
}
async function largeRespond(request: ServerRequest, c: string): Promise<void> {
@@ -19,6 +22,13 @@ async function largeRespond(request: ServerRequest, c: string): Promise<void> {
await request.respond({ status: 200, body: b });
}
+async function ignoreToConsume(
+ request: ServerRequest,
+ step: number
+): Promise<void> {
+ await request.respond({ status: 200, body: body(step) });
+}
+
console.log("Racing server listening...\n");
let step = 1;
@@ -28,7 +38,7 @@ for await (const request of server) {
// Try to wait long enough.
// For pipelining, this should cause all the following response
// to block.
- delayedRespond(request);
+ delayedRespond(request, step);
break;
case 2:
// HUGE body.
@@ -38,8 +48,20 @@ for await (const request of server) {
// HUGE body.
largeRespond(request, "b");
break;
+ case 4:
+ // Ignore to consume body (content-length)
+ ignoreToConsume(request, step);
+ break;
+ case 5:
+ // Ignore to consume body (chunked)
+ ignoreToConsume(request, step);
+ break;
+ case 6:
+ // Ignore to consume body (chunked + trailers)
+ ignoreToConsume(request, step);
+ break;
default:
- request.respond({ status: 200, body: body4 });
+ request.respond({ status: 200, body: body(step) });
break;
}
step++;