mirror of
https://github.com/denoland/deno.git
synced 2024-12-28 10:09:20 -05:00
fix(http): Skip 0 chunks when writing body (#387)
This commit is contained in:
parent
07ca4f9629
commit
79d5f556ba
2 changed files with 68 additions and 3 deletions
|
@ -1,5 +1,5 @@
|
||||||
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
||||||
const { listen, toAsyncIterator, copy } = Deno;
|
const { listen, copy, toAsyncIterator } = Deno;
|
||||||
type Conn = Deno.Conn;
|
type Conn = Deno.Conn;
|
||||||
type Reader = Deno.Reader;
|
type Reader = Deno.Reader;
|
||||||
type Writer = Deno.Writer;
|
type Writer = Deno.Writer;
|
||||||
|
@ -79,6 +79,7 @@ async function writeChunkedBody(w: Writer, r: Reader): Promise<void> {
|
||||||
const encoder = new TextEncoder();
|
const encoder = new TextEncoder();
|
||||||
|
|
||||||
for await (const chunk of toAsyncIterator(r)) {
|
for await (const chunk of toAsyncIterator(r)) {
|
||||||
|
if (chunk.byteLength <= 0) continue;
|
||||||
const start = encoder.encode(`${chunk.byteLength.toString(16)}\r\n`);
|
const start = encoder.encode(`${chunk.byteLength.toString(16)}\r\n`);
|
||||||
const end = encoder.encode("\r\n");
|
const end = encoder.encode("\r\n");
|
||||||
await writer.write(start);
|
await writer.write(start);
|
||||||
|
|
|
@ -6,10 +6,11 @@
|
||||||
// https://github.com/golang/go/blob/master/src/net/http/responsewrite_test.go
|
// https://github.com/golang/go/blob/master/src/net/http/responsewrite_test.go
|
||||||
|
|
||||||
const { Buffer } = Deno;
|
const { Buffer } = Deno;
|
||||||
import { test } from "../testing/mod.ts";
|
import { test, runIfMain } from "../testing/mod.ts";
|
||||||
import { assertEquals } from "../testing/asserts.ts";
|
import { assertEquals } from "../testing/asserts.ts";
|
||||||
import { Response, ServerRequest } from "./server.ts";
|
import { Response, ServerRequest, writeResponse } from "./server.ts";
|
||||||
import { BufReader, BufWriter } from "../io/bufio.ts";
|
import { BufReader, BufWriter } from "../io/bufio.ts";
|
||||||
|
import { StringReader } from "../io/readers.ts";
|
||||||
|
|
||||||
interface ResponseTest {
|
interface ResponseTest {
|
||||||
response: Response;
|
response: Response;
|
||||||
|
@ -261,3 +262,66 @@ test(async function requestBodyStreamWithTransferEncoding(): Promise<void> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test(async function writeUint8ArrayResponse(): Promise<void> {
|
||||||
|
const shortText = "Hello";
|
||||||
|
|
||||||
|
const body = new TextEncoder().encode(shortText);
|
||||||
|
const res: Response = { body };
|
||||||
|
|
||||||
|
const buf = new Deno.Buffer();
|
||||||
|
await writeResponse(buf, res);
|
||||||
|
|
||||||
|
const decoder = new TextDecoder("utf-8");
|
||||||
|
const reader = new BufReader(buf);
|
||||||
|
|
||||||
|
let line: Uint8Array;
|
||||||
|
line = (await reader.readLine())[0];
|
||||||
|
assertEquals(decoder.decode(line), "HTTP/1.1 200 OK");
|
||||||
|
|
||||||
|
line = (await reader.readLine())[0];
|
||||||
|
assertEquals(decoder.decode(line), `content-length: ${shortText.length}`);
|
||||||
|
|
||||||
|
line = (await reader.readLine())[0];
|
||||||
|
assertEquals(line.byteLength, 0);
|
||||||
|
|
||||||
|
line = (await reader.readLine())[0];
|
||||||
|
assertEquals(decoder.decode(line), shortText);
|
||||||
|
|
||||||
|
line = (await reader.readLine())[0];
|
||||||
|
assertEquals(line.byteLength, 0);
|
||||||
|
});
|
||||||
|
|
||||||
|
test(async function writeStringReaderResponse(): Promise<void> {
|
||||||
|
const shortText = "Hello";
|
||||||
|
|
||||||
|
const body = new StringReader(shortText);
|
||||||
|
const res: Response = { body };
|
||||||
|
|
||||||
|
const buf = new Deno.Buffer();
|
||||||
|
await writeResponse(buf, res);
|
||||||
|
|
||||||
|
const decoder = new TextDecoder("utf-8");
|
||||||
|
const reader = new BufReader(buf);
|
||||||
|
|
||||||
|
let line: Uint8Array;
|
||||||
|
line = (await reader.readLine())[0];
|
||||||
|
assertEquals(decoder.decode(line), "HTTP/1.1 200 OK");
|
||||||
|
|
||||||
|
line = (await reader.readLine())[0];
|
||||||
|
assertEquals(decoder.decode(line), "transfer-encoding: chunked");
|
||||||
|
|
||||||
|
line = (await reader.readLine())[0];
|
||||||
|
assertEquals(line.byteLength, 0);
|
||||||
|
|
||||||
|
line = (await reader.readLine())[0];
|
||||||
|
assertEquals(decoder.decode(line), shortText.length.toString());
|
||||||
|
|
||||||
|
line = (await reader.readLine())[0];
|
||||||
|
assertEquals(decoder.decode(line), shortText);
|
||||||
|
|
||||||
|
line = (await reader.readLine())[0];
|
||||||
|
assertEquals(decoder.decode(line), "0");
|
||||||
|
});
|
||||||
|
|
||||||
|
runIfMain(import.meta);
|
||||||
|
|
Loading…
Reference in a new issue