diff options
author | lideming <me@yuuza.net> | 2020-11-19 00:47:47 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-11-18 17:47:47 +0100 |
commit | 60d9ab08dbcf2b9874206bc8411a25ca44e8118e (patch) | |
tree | 6bbce950ce91fad1a58ca15a6c020672ed1d5afa /std/http/server.ts | |
parent | b6fa6d6aac10e992c37b6df887d6f1311356b430 (diff) |
fix(std/http): fix error handling in the request iterator (#8365)
If the request body is using chunked encoding, errors may
be thrown in "request.finalize()".
In this case, we should untrack and close the connection.
Diffstat (limited to 'std/http/server.ts')
-rw-r--r-- | std/http/server.ts | 28 |
1 files changed, 21 insertions, 7 deletions
diff --git a/std/http/server.ts b/std/http/server.ts index 7bbdb7829..4d9f70723 100644 --- a/std/http/server.ts +++ b/std/http/server.ts @@ -151,10 +151,15 @@ export class Server implements AsyncIterable<ServerRequest> { error instanceof Deno.errors.UnexpectedEof ) { // An error was thrown while parsing request headers. - await writeResponse(writer, { - status: 400, - body: encode(`${error.message}\r\n\r\n`), - }); + // Try to send the "400 Bad Request" before closing the connection. + try { + await writeResponse(writer, { + status: 400, + body: encode(`${error.message}\r\n\r\n`), + }); + } catch (error) { + // The connection is broken. + } } break; } @@ -175,8 +180,14 @@ export class Server implements AsyncIterable<ServerRequest> { this.untrackConnection(request.conn); return; } - // Consume unread body and trailers if receiver didn't consume those data - await request.finalize(); + + try { + // Consume unread body and trailers if receiver didn't consume those data + await request.finalize(); + } catch (error) { + // Invalid data was received or the connection was closed. + break; + } } this.untrackConnection(conn); @@ -212,9 +223,12 @@ export class Server implements AsyncIterable<ServerRequest> { conn = await this.listener.accept(); } catch (error) { if ( + // The listener is closed: error instanceof Deno.errors.BadResource || + // TLS handshake errors: error instanceof Deno.errors.InvalidData || - error instanceof Deno.errors.UnexpectedEof + error instanceof Deno.errors.UnexpectedEof || + error instanceof Deno.errors.ConnectionReset ) { return mux.add(this.acceptConnAndIterateHttpRequests(mux)); } |