From 31ab43b9193a9107d965c991f17992fcee791b33 Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Thu, 8 Nov 2018 12:26:20 -0500 Subject: [PATCH] First pass at TextProtoReader. --- buffer.ts | 6 +++ bufio.ts | 2 +- bufio_test.ts | 9 +--- test.ts | 1 + textproto.ts | 144 ++++++++++++++++++++++++++++++++++++++++++++++++++ util.ts | 2 + 6 files changed, 156 insertions(+), 8 deletions(-) create mode 100644 textproto.ts diff --git a/buffer.ts b/buffer.ts index a795bc8423..ba2715cef7 100644 --- a/buffer.ts +++ b/buffer.ts @@ -13,6 +13,12 @@ import { assert, copyBytes } from "./util.ts"; const MIN_READ = 512; const MAX_SIZE = 2 ** 32 - 2; +const encoder = new TextEncoder(); +export function stringsReader(s: string): Reader { + const ui8 = encoder.encode(s); + return new Buffer(ui8.buffer as ArrayBuffer); +} + /** A Buffer is a variable-sized buffer of bytes with read() and write() * methods. Based on https://golang.org/pkg/bytes/#Buffer */ diff --git a/bufio.ts b/bufio.ts index d736e1fc4d..8ff02b7e9c 100644 --- a/bufio.ts +++ b/bufio.ts @@ -1,4 +1,4 @@ -// Ported to Deno from: +// Based on https://github.com/golang/go/blob/891682/src/bufio/bufio.go // Copyright 2009 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. diff --git a/bufio_test.ts b/bufio_test.ts index 80068f2d39..9c89f02168 100644 --- a/bufio_test.ts +++ b/bufio_test.ts @@ -1,4 +1,4 @@ -// Ported to Deno from: +// Based on https://github.com/golang/go/blob/891682/src/bufio/bufio_test.go // Copyright 2009 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. @@ -10,7 +10,7 @@ import { assertEqual } from "https://deno.land/x/testing/testing.ts"; import { BufReader, BufState } from "./bufio.ts"; -import { Buffer } from "./buffer.ts"; +import { Buffer, stringsReader } from "./buffer.ts"; import * as iotest from "./iotest.ts"; import { charCode, copyBytes } from "./util.ts"; @@ -31,11 +31,6 @@ async function readBytes(buf: BufReader): Promise { return decoder.decode(b.subarray(0, nb)); } -function stringsReader(s: string): Reader { - const ui8 = encoder.encode(s); - return new Buffer(ui8.buffer as ArrayBuffer); -} - test(async function bufioReaderSimple() { const data = "hello world"; const b = new BufReader(stringsReader(data)); diff --git a/test.ts b/test.ts index 4e65e4c391..2ee9a820b9 100644 --- a/test.ts +++ b/test.ts @@ -1,3 +1,4 @@ import "./buffer_test.ts"; import "./bufio_test.ts"; +import "./textproto_test.ts"; // TODO import "./http_test.ts"; diff --git a/textproto.ts b/textproto.ts new file mode 100644 index 0000000000..b4336c90de --- /dev/null +++ b/textproto.ts @@ -0,0 +1,144 @@ +// Based on https://github.com/golang/go/blob/891682/src/net/textproto/ +// Copyright 2009 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +import { BufReader, BufState } from "./bufio.ts"; +import { charCode } from "./util.ts"; + +const asciiDecoder = new TextDecoder("ascii"); +function str(buf: Uint8Array): string { + if (buf == null) { + return ""; + } else { + return asciiDecoder.decode(buf); + } +} + +export class ProtocolError extends Error { + constructor(msg: string) { + super(msg); + this.name = "ProtocolError"; + } +} + +export class TextProtoReader { + constructor(readonly r: BufReader) {} + + /** readLine() reads a single line from the TextProtoReader, + * eliding the final \n or \r\n from the returned string. + */ + async readLine(): Promise<[string, BufState]> { + let [line, err] = await this.readLineSlice(); + return [str(line), err]; + } + + /** ReadMIMEHeader reads a MIME-style header from r. + * The header is a sequence of possibly continued Key: Value lines + * ending in a blank line. + * The returned map m maps CanonicalMIMEHeaderKey(key) to a + * sequence of values in the same order encountered in the input. + * + * For example, consider this input: + * + * My-Key: Value 1 + * Long-Key: Even + * Longer Value + * My-Key: Value 2 + * + * Given that input, ReadMIMEHeader returns the map: + * + * map[string][]string{ + * "My-Key": {"Value 1", "Value 2"}, + * "Long-Key": {"Even Longer Value"}, + * } + */ + /* + async readMIMEHeader(): Promise { + let m = new Headers(); + let line: Uint8Array; + + // The first line cannot start with a leading space. + let [buf, err] = await this.r.peek(1); + if (buf[0] == charCode(' ') || buf[0] == charCode('\t')) { + [line, err] = await this.readLineSlice(); + } + + [buf, err] = await this.r.peek(1); + if (err == null && (buf[0] == charCode(' ') || buf[0] == charCode('\t'))) { + throw new ProtocolError(`malformed MIME header initial line: ${str(line)}`) + } + + while (true) { + let [kv, err] = await this.readLineSlice(); // readContinuedLineSlice + if (kv.byteLength == 0) { + return m; + } + + // Key ends at first colon; should not have trailing spaces + // but they appear in the wild, violating specs, so we remove + // them if present. + let i = kv.indexOf(charCode(':')); + if (i < 0) { + throw new ProtocolError(`malformed MIME header line: ${str(kv)}`); + } + let endKey = i; + while (endKey > 0 && kv[endKey - 1] == charCode(' ')) { + endKey--; + } + + //let key = canonicalMIMEHeaderKey(kv.subarray(0, endKey)); + let key = str(kv.subarray(0, endKey)); + + // As per RFC 7230 field-name is a token, tokens consist of one or more chars. + // We could return a ProtocolError here, but better to be liberal in what we + // accept, so if we get an empty key, skip it. + if (key == "") { + continue; + } + + // Skip initial spaces in value. + i++; // skip colon + while (i < kv.byteLength && + (kv[i] == charCode(' ') || kv[i] == charCode('\t'))) { + i++; + } + let value = str(kv.subarray(i)); + + m.append(key, value); + + if (err != null) { + throw err; + } + } + } + */ + + async readLineSlice(): Promise<[Uint8Array, BufState]> { + // this.closeDot(); + let line: null | Uint8Array; + while (true) { + let [l, more, err] = await this.r.readLine(); + if (err != null) { + return [null, err]; + } + // Avoid the copy if the first call produced a full line. + if (line == null && !more) { + return [l, null]; + } + line = append(line, l); + if (!more) { + break; + } + } + return [line, null]; + } +} + +function append(a: Uint8Array, b: Uint8Array): Uint8Array { + if (a == null) { + return b; + } else { + throw Error("Not implemented"); + } +} diff --git a/util.ts b/util.ts index d05bea8c04..6a230ef3b1 100644 --- a/util.ts +++ b/util.ts @@ -1,3 +1,5 @@ +import { Reader } from "deno"; + export function assert(cond: boolean, msg = "assert") { if (!cond) { throw Error(msg);