From 70baf36fb92d0dd71c5bcb7c3ef2c4efd1bd4a83 Mon Sep 17 00:00:00 2001 From: Zhangyuan Nie Date: Sun, 3 Jan 2021 21:57:23 -0500 Subject: [PATCH] fix http/server --- std/http/server.ts | 53 +++++++++++++++++++++++----------------------- 1 file changed, 26 insertions(+), 27 deletions(-) diff --git a/std/http/server.ts b/std/http/server.ts index df5d1ff724d54e..2833c48dc4be53 100644 --- a/std/http/server.ts +++ b/std/http/server.ts @@ -21,9 +21,11 @@ export class ServerRequest { conn!: Deno.Conn; r!: BufReader; w!: BufWriter; - #done: Deferred = deferred(); - private _contentLength: number | undefined | null = undefined; + #done: Deferred = deferred(); + #contentLength?: number | null = undefined; + #body?: Deno.Reader = undefined; + #finalized = false; get done(): Promise { return this.#done.then((e) => e); @@ -36,32 +38,30 @@ export class ServerRequest { get contentLength(): number | null { // undefined means not cached. // null means invalid or not provided. - if (this._contentLength === undefined) { + if (this.#contentLength === undefined) { const cl = this.headers.get("content-length"); if (cl) { - this._contentLength = parseInt(cl); + this.#contentLength = parseInt(cl); // Convert NaN to null (as NaN harder to test) - if (Number.isNaN(this._contentLength)) { - this._contentLength = null; + if (Number.isNaN(this.#contentLength)) { + this.#contentLength = null; } } else { - this._contentLength = null; + this.#contentLength = null; } } - return this._contentLength; + return this.#contentLength; } - private _body: Deno.Reader | null = null; - /** * Body of the request. The easiest way to consume the body is: * * const buf: Uint8Array = await Deno.readAll(req.body); */ get body(): Deno.Reader { - if (!this._body) { + if (!this.#body) { if (this.contentLength != null) { - this._body = bodyReader(this.contentLength, this.r); + this.#body = bodyReader(this.contentLength, this.r); } else { const transferEncoding = this.headers.get("transfer-encoding"); if (transferEncoding != null) { @@ -72,14 +72,14 @@ export class ServerRequest { parts.includes("chunked"), 'transfer-encoding must include "chunked" if content-length is not set', ); - this._body = chunkedBodyReader(this.headers, this.r); + this.#body = chunkedBodyReader(this.headers, this.r); } else { // Neither content-length nor transfer-encoding: chunked - this._body = emptyReader(); + this.#body = emptyReader(); } } } - return this._body; + return this.#body; } async respond(r: Response): Promise { @@ -105,29 +105,28 @@ export class ServerRequest { } } - private finalized = false; async finalize(): Promise { - if (this.finalized) return; + if (this.#finalized) return; // Consume unread body const body = this.body; const buf = new Uint8Array(1024); while ((await body.read(buf)) !== null) { // Pass } - this.finalized = true; + this.#finalized = true; } } export class Server implements AsyncIterable { - private closing = false; - private connections: Deno.Conn[] = []; + #closing = false; + #connections: Deno.Conn[] = []; constructor(public listener: Deno.Listener) {} close(): void { - this.closing = true; + this.#closing = true; this.listener.close(); - for (const conn of this.connections) { + for (const conn of this.#connections) { try { conn.close(); } catch (e) { @@ -146,7 +145,7 @@ export class Server implements AsyncIterable { const reader = new BufReader(conn); const writer = new BufWriter(conn); - while (!this.closing) { + while (!this.#closing) { let request: ServerRequest | null; try { request = await readRequest(conn, reader); @@ -204,13 +203,13 @@ export class Server implements AsyncIterable { } private trackConnection(conn: Deno.Conn): void { - this.connections.push(conn); + this.#connections.push(conn); } private untrackConnection(conn: Deno.Conn): void { - const index = this.connections.indexOf(conn); + const index = this.#connections.indexOf(conn); if (index !== -1) { - this.connections.splice(index, 1); + this.#connections.splice(index, 1); } } @@ -221,7 +220,7 @@ export class Server implements AsyncIterable { private async *acceptConnAndIterateHttpRequests( mux: MuxAsyncIterator, ): AsyncIterableIterator { - if (this.closing) return; + if (this.#closing) return; // Wait for a new connection. let conn: Deno.Conn; try {