mirror of
https://github.com/tinygo-org/net.git
synced 2026-08-10 06:33:38 +00:00
Add network device driver model, netdev
This PR adds a network device driver model called netdev. There will be a companion PR for TinyGo drivers to update the netdev drivers and network examples. This PR covers the core "net" package. An RFC for the work is here: #tinygo-org/drivers#487. Some things have changed from the RFC, but nothing major. The "net" package is a partial port of Go's "net" package, version 1.19.3. The src/net/README file has details on what is modified from Go's "net" package. Most "net" features are working as they would in normal Go. TCP/UDP/TLS protocol support is there. As well as HTTP client and server support. Standard Go network packages such as golang.org/x/net/websockets and Paho MQTT client work as-is. Other packages are likely to work as-is. Testing results are here (https://docs.google.com/spreadsheets/d/e/2PACX-1vT0cCjBvwXf9HJf6aJV2Sw198F2ief02gmbMV0sQocKT4y4RpfKv3dh6Jyew8lQW64FouZ8GwA2yjxI/pubhtml?gid=1013173032&single=true).
This commit is contained in:
committed by
deadprogram
parent
b46e2ec2ac
commit
693edae782
@@ -0,0 +1,63 @@
|
||||
// TINYGO: The following is copied from Go 1.19.3 official implementation.
|
||||
|
||||
// Copyright 2021 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.
|
||||
|
||||
package ascii
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"unicode"
|
||||
)
|
||||
|
||||
// EqualFold is strings.EqualFold, ASCII only. It reports whether s and t
|
||||
// are equal, ASCII-case-insensitively.
|
||||
func EqualFold(s, t string) bool {
|
||||
if len(s) != len(t) {
|
||||
return false
|
||||
}
|
||||
for i := 0; i < len(s); i++ {
|
||||
if lower(s[i]) != lower(t[i]) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// lower returns the ASCII lowercase version of b.
|
||||
func lower(b byte) byte {
|
||||
if 'A' <= b && b <= 'Z' {
|
||||
return b + ('a' - 'A')
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
// IsPrint returns whether s is ASCII and printable according to
|
||||
// https://tools.ietf.org/html/rfc20#section-4.2.
|
||||
func IsPrint(s string) bool {
|
||||
for i := 0; i < len(s); i++ {
|
||||
if s[i] < ' ' || s[i] > '~' {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// Is returns whether s is ASCII.
|
||||
func Is(s string) bool {
|
||||
for i := 0; i < len(s); i++ {
|
||||
if s[i] > unicode.MaxASCII {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// ToLower returns the lowercase version of s if s is ASCII and printable.
|
||||
func ToLower(s string) (lower string, ok bool) {
|
||||
if !IsPrint(s) {
|
||||
return "", false
|
||||
}
|
||||
return strings.ToLower(s), true
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
// Copyright 2021 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.
|
||||
|
||||
package ascii
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestEqualFold(t *testing.T) {
|
||||
var tests = []struct {
|
||||
name string
|
||||
a, b string
|
||||
want bool
|
||||
}{
|
||||
{
|
||||
name: "empty",
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "simple match",
|
||||
a: "CHUNKED",
|
||||
b: "chunked",
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "same string",
|
||||
a: "chunked",
|
||||
b: "chunked",
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "Unicode Kelvin symbol",
|
||||
a: "chunKed", // This "K" is 'KELVIN SIGN' (\u212A)
|
||||
b: "chunked",
|
||||
want: false,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := EqualFold(tt.a, tt.b); got != tt.want {
|
||||
t.Errorf("AsciiEqualFold(%q,%q): got %v want %v", tt.a, tt.b, got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsPrint(t *testing.T) {
|
||||
var tests = []struct {
|
||||
name string
|
||||
in string
|
||||
want bool
|
||||
}{
|
||||
{
|
||||
name: "empty",
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "ASCII low",
|
||||
in: "This is a space: ' '",
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "ASCII high",
|
||||
in: "This is a tilde: '~'",
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "ASCII low non-print",
|
||||
in: "This is a unit separator: \x1F",
|
||||
want: false,
|
||||
},
|
||||
{
|
||||
name: "Ascii high non-print",
|
||||
in: "This is a Delete: \x7F",
|
||||
want: false,
|
||||
},
|
||||
{
|
||||
name: "Unicode letter",
|
||||
in: "Today it's 280K outside: it's freezing!", // This "K" is 'KELVIN SIGN' (\u212A)
|
||||
want: false,
|
||||
},
|
||||
{
|
||||
name: "Unicode emoji",
|
||||
in: "Gophers like 🧀",
|
||||
want: false,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := IsPrint(tt.in); got != tt.want {
|
||||
t.Errorf("IsASCIIPrint(%q): got %v want %v", tt.in, got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,264 @@
|
||||
// TINYGO: The following is copied from Go 1.19.3 official implementation.
|
||||
|
||||
// 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.
|
||||
|
||||
// The wire protocol for HTTP's "chunked" Transfer-Encoding.
|
||||
|
||||
// Package internal contains HTTP internals shared by net/http and
|
||||
// net/http/httputil.
|
||||
package internal
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
)
|
||||
|
||||
const maxLineLength = 4096 // assumed <= bufio.defaultBufSize
|
||||
|
||||
var ErrLineTooLong = errors.New("header line too long")
|
||||
|
||||
// NewChunkedReader returns a new chunkedReader that translates the data read from r
|
||||
// out of HTTP "chunked" format before returning it.
|
||||
// The chunkedReader returns io.EOF when the final 0-length chunk is read.
|
||||
//
|
||||
// NewChunkedReader is not needed by normal applications. The http package
|
||||
// automatically decodes chunking when reading response bodies.
|
||||
func NewChunkedReader(r io.Reader) io.Reader {
|
||||
br, ok := r.(*bufio.Reader)
|
||||
if !ok {
|
||||
br = bufio.NewReader(r)
|
||||
}
|
||||
return &chunkedReader{r: br}
|
||||
}
|
||||
|
||||
type chunkedReader struct {
|
||||
r *bufio.Reader
|
||||
n uint64 // unread bytes in chunk
|
||||
err error
|
||||
buf [2]byte
|
||||
checkEnd bool // whether need to check for \r\n chunk footer
|
||||
}
|
||||
|
||||
func (cr *chunkedReader) beginChunk() {
|
||||
// chunk-size CRLF
|
||||
var line []byte
|
||||
line, cr.err = readChunkLine(cr.r)
|
||||
if cr.err != nil {
|
||||
return
|
||||
}
|
||||
cr.n, cr.err = parseHexUint(line)
|
||||
if cr.err != nil {
|
||||
return
|
||||
}
|
||||
if cr.n == 0 {
|
||||
cr.err = io.EOF
|
||||
}
|
||||
}
|
||||
|
||||
func (cr *chunkedReader) chunkHeaderAvailable() bool {
|
||||
n := cr.r.Buffered()
|
||||
if n > 0 {
|
||||
peek, _ := cr.r.Peek(n)
|
||||
return bytes.IndexByte(peek, '\n') >= 0
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (cr *chunkedReader) Read(b []uint8) (n int, err error) {
|
||||
for cr.err == nil {
|
||||
if cr.checkEnd {
|
||||
if n > 0 && cr.r.Buffered() < 2 {
|
||||
// We have some data. Return early (per the io.Reader
|
||||
// contract) instead of potentially blocking while
|
||||
// reading more.
|
||||
break
|
||||
}
|
||||
if _, cr.err = io.ReadFull(cr.r, cr.buf[:2]); cr.err == nil {
|
||||
if string(cr.buf[:]) != "\r\n" {
|
||||
cr.err = errors.New("malformed chunked encoding")
|
||||
break
|
||||
}
|
||||
} else {
|
||||
if cr.err == io.EOF {
|
||||
cr.err = io.ErrUnexpectedEOF
|
||||
}
|
||||
break
|
||||
}
|
||||
cr.checkEnd = false
|
||||
}
|
||||
if cr.n == 0 {
|
||||
if n > 0 && !cr.chunkHeaderAvailable() {
|
||||
// We've read enough. Don't potentially block
|
||||
// reading a new chunk header.
|
||||
break
|
||||
}
|
||||
cr.beginChunk()
|
||||
continue
|
||||
}
|
||||
if len(b) == 0 {
|
||||
break
|
||||
}
|
||||
rbuf := b
|
||||
if uint64(len(rbuf)) > cr.n {
|
||||
rbuf = rbuf[:cr.n]
|
||||
}
|
||||
var n0 int
|
||||
n0, cr.err = cr.r.Read(rbuf)
|
||||
n += n0
|
||||
b = b[n0:]
|
||||
cr.n -= uint64(n0)
|
||||
// If we're at the end of a chunk, read the next two
|
||||
// bytes to verify they are "\r\n".
|
||||
if cr.n == 0 && cr.err == nil {
|
||||
cr.checkEnd = true
|
||||
} else if cr.err == io.EOF {
|
||||
cr.err = io.ErrUnexpectedEOF
|
||||
}
|
||||
}
|
||||
return n, cr.err
|
||||
}
|
||||
|
||||
// Read a line of bytes (up to \n) from b.
|
||||
// Give up if the line exceeds maxLineLength.
|
||||
// The returned bytes are owned by the bufio.Reader
|
||||
// so they are only valid until the next bufio read.
|
||||
func readChunkLine(b *bufio.Reader) ([]byte, error) {
|
||||
p, err := b.ReadSlice('\n')
|
||||
if err != nil {
|
||||
// We always know when EOF is coming.
|
||||
// If the caller asked for a line, there should be a line.
|
||||
if err == io.EOF {
|
||||
err = io.ErrUnexpectedEOF
|
||||
} else if err == bufio.ErrBufferFull {
|
||||
err = ErrLineTooLong
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
if len(p) >= maxLineLength {
|
||||
return nil, ErrLineTooLong
|
||||
}
|
||||
p = trimTrailingWhitespace(p)
|
||||
p, err = removeChunkExtension(p)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return p, nil
|
||||
}
|
||||
|
||||
func trimTrailingWhitespace(b []byte) []byte {
|
||||
for len(b) > 0 && isASCIISpace(b[len(b)-1]) {
|
||||
b = b[:len(b)-1]
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
func isASCIISpace(b byte) bool {
|
||||
return b == ' ' || b == '\t' || b == '\n' || b == '\r'
|
||||
}
|
||||
|
||||
var semi = []byte(";")
|
||||
|
||||
// removeChunkExtension removes any chunk-extension from p.
|
||||
// For example,
|
||||
//
|
||||
// "0" => "0"
|
||||
// "0;token" => "0"
|
||||
// "0;token=val" => "0"
|
||||
// `0;token="quoted string"` => "0"
|
||||
func removeChunkExtension(p []byte) ([]byte, error) {
|
||||
p, _, _ = bytes.Cut(p, semi)
|
||||
// TODO: care about exact syntax of chunk extensions? We're
|
||||
// ignoring and stripping them anyway. For now just never
|
||||
// return an error.
|
||||
return p, nil
|
||||
}
|
||||
|
||||
// NewChunkedWriter returns a new chunkedWriter that translates writes into HTTP
|
||||
// "chunked" format before writing them to w. Closing the returned chunkedWriter
|
||||
// sends the final 0-length chunk that marks the end of the stream but does
|
||||
// not send the final CRLF that appears after trailers; trailers and the last
|
||||
// CRLF must be written separately.
|
||||
//
|
||||
// NewChunkedWriter is not needed by normal applications. The http
|
||||
// package adds chunking automatically if handlers don't set a
|
||||
// Content-Length header. Using newChunkedWriter inside a handler
|
||||
// would result in double chunking or chunking with a Content-Length
|
||||
// length, both of which are wrong.
|
||||
func NewChunkedWriter(w io.Writer) io.WriteCloser {
|
||||
return &chunkedWriter{w}
|
||||
}
|
||||
|
||||
// Writing to chunkedWriter translates to writing in HTTP chunked Transfer
|
||||
// Encoding wire format to the underlying Wire chunkedWriter.
|
||||
type chunkedWriter struct {
|
||||
Wire io.Writer
|
||||
}
|
||||
|
||||
// Write the contents of data as one chunk to Wire.
|
||||
// NOTE: Note that the corresponding chunk-writing procedure in Conn.Write has
|
||||
// a bug since it does not check for success of io.WriteString
|
||||
func (cw *chunkedWriter) Write(data []byte) (n int, err error) {
|
||||
|
||||
// Don't send 0-length data. It looks like EOF for chunked encoding.
|
||||
if len(data) == 0 {
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
if _, err = fmt.Fprintf(cw.Wire, "%x\r\n", len(data)); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if n, err = cw.Wire.Write(data); err != nil {
|
||||
return
|
||||
}
|
||||
if n != len(data) {
|
||||
err = io.ErrShortWrite
|
||||
return
|
||||
}
|
||||
if _, err = io.WriteString(cw.Wire, "\r\n"); err != nil {
|
||||
return
|
||||
}
|
||||
if bw, ok := cw.Wire.(*FlushAfterChunkWriter); ok {
|
||||
err = bw.Flush()
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (cw *chunkedWriter) Close() error {
|
||||
_, err := io.WriteString(cw.Wire, "0\r\n")
|
||||
return err
|
||||
}
|
||||
|
||||
// FlushAfterChunkWriter signals from the caller of NewChunkedWriter
|
||||
// that each chunk should be followed by a flush. It is used by the
|
||||
// http.Transport code to keep the buffering behavior for headers and
|
||||
// trailers, but flush out chunks aggressively in the middle for
|
||||
// request bodies which may be generated slowly. See Issue 6574.
|
||||
type FlushAfterChunkWriter struct {
|
||||
*bufio.Writer
|
||||
}
|
||||
|
||||
func parseHexUint(v []byte) (n uint64, err error) {
|
||||
for i, b := range v {
|
||||
switch {
|
||||
case '0' <= b && b <= '9':
|
||||
b = b - '0'
|
||||
case 'a' <= b && b <= 'f':
|
||||
b = b - 'a' + 10
|
||||
case 'A' <= b && b <= 'F':
|
||||
b = b - 'A' + 10
|
||||
default:
|
||||
return 0, errors.New("invalid byte in chunk length")
|
||||
}
|
||||
if i == 16 {
|
||||
return 0, errors.New("http chunk length too large")
|
||||
}
|
||||
n <<= 4
|
||||
n |= uint64(b)
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,241 @@
|
||||
// Copyright 2011 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.
|
||||
|
||||
package internal
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"strings"
|
||||
"testing"
|
||||
"testing/iotest"
|
||||
)
|
||||
|
||||
func TestChunk(t *testing.T) {
|
||||
var b bytes.Buffer
|
||||
|
||||
w := NewChunkedWriter(&b)
|
||||
const chunk1 = "hello, "
|
||||
const chunk2 = "world! 0123456789abcdef"
|
||||
w.Write([]byte(chunk1))
|
||||
w.Write([]byte(chunk2))
|
||||
w.Close()
|
||||
|
||||
if g, e := b.String(), "7\r\nhello, \r\n17\r\nworld! 0123456789abcdef\r\n0\r\n"; g != e {
|
||||
t.Fatalf("chunk writer wrote %q; want %q", g, e)
|
||||
}
|
||||
|
||||
r := NewChunkedReader(&b)
|
||||
data, err := io.ReadAll(r)
|
||||
if err != nil {
|
||||
t.Logf(`data: "%s"`, data)
|
||||
t.Fatalf("ReadAll from reader: %v", err)
|
||||
}
|
||||
if g, e := string(data), chunk1+chunk2; g != e {
|
||||
t.Errorf("chunk reader read %q; want %q", g, e)
|
||||
}
|
||||
}
|
||||
|
||||
func TestChunkReadMultiple(t *testing.T) {
|
||||
// Bunch of small chunks, all read together.
|
||||
{
|
||||
var b bytes.Buffer
|
||||
w := NewChunkedWriter(&b)
|
||||
w.Write([]byte("foo"))
|
||||
w.Write([]byte("bar"))
|
||||
w.Close()
|
||||
|
||||
r := NewChunkedReader(&b)
|
||||
buf := make([]byte, 10)
|
||||
n, err := r.Read(buf)
|
||||
if n != 6 || err != io.EOF {
|
||||
t.Errorf("Read = %d, %v; want 6, EOF", n, err)
|
||||
}
|
||||
buf = buf[:n]
|
||||
if string(buf) != "foobar" {
|
||||
t.Errorf("Read = %q; want %q", buf, "foobar")
|
||||
}
|
||||
}
|
||||
|
||||
// One big chunk followed by a little chunk, but the small bufio.Reader size
|
||||
// should prevent the second chunk header from being read.
|
||||
{
|
||||
var b bytes.Buffer
|
||||
w := NewChunkedWriter(&b)
|
||||
// fillBufChunk is 11 bytes + 3 bytes header + 2 bytes footer = 16 bytes,
|
||||
// the same as the bufio ReaderSize below (the minimum), so even
|
||||
// though we're going to try to Read with a buffer larger enough to also
|
||||
// receive "foo", the second chunk header won't be read yet.
|
||||
const fillBufChunk = "0123456789a"
|
||||
const shortChunk = "foo"
|
||||
w.Write([]byte(fillBufChunk))
|
||||
w.Write([]byte(shortChunk))
|
||||
w.Close()
|
||||
|
||||
r := NewChunkedReader(bufio.NewReaderSize(&b, 16))
|
||||
buf := make([]byte, len(fillBufChunk)+len(shortChunk))
|
||||
n, err := r.Read(buf)
|
||||
if n != len(fillBufChunk) || err != nil {
|
||||
t.Errorf("Read = %d, %v; want %d, nil", n, err, len(fillBufChunk))
|
||||
}
|
||||
buf = buf[:n]
|
||||
if string(buf) != fillBufChunk {
|
||||
t.Errorf("Read = %q; want %q", buf, fillBufChunk)
|
||||
}
|
||||
|
||||
n, err = r.Read(buf)
|
||||
if n != len(shortChunk) || err != io.EOF {
|
||||
t.Errorf("Read = %d, %v; want %d, EOF", n, err, len(shortChunk))
|
||||
}
|
||||
}
|
||||
|
||||
// And test that we see an EOF chunk, even though our buffer is already full:
|
||||
{
|
||||
r := NewChunkedReader(bufio.NewReader(strings.NewReader("3\r\nfoo\r\n0\r\n")))
|
||||
buf := make([]byte, 3)
|
||||
n, err := r.Read(buf)
|
||||
if n != 3 || err != io.EOF {
|
||||
t.Errorf("Read = %d, %v; want 3, EOF", n, err)
|
||||
}
|
||||
if string(buf) != "foo" {
|
||||
t.Errorf("buf = %q; want foo", buf)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestChunkReaderAllocs(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.Skip("skipping in short mode")
|
||||
}
|
||||
var buf bytes.Buffer
|
||||
w := NewChunkedWriter(&buf)
|
||||
a, b, c := []byte("aaaaaa"), []byte("bbbbbbbbbbbb"), []byte("cccccccccccccccccccccccc")
|
||||
w.Write(a)
|
||||
w.Write(b)
|
||||
w.Write(c)
|
||||
w.Close()
|
||||
|
||||
readBuf := make([]byte, len(a)+len(b)+len(c)+1)
|
||||
byter := bytes.NewReader(buf.Bytes())
|
||||
bufr := bufio.NewReader(byter)
|
||||
mallocs := testing.AllocsPerRun(100, func() {
|
||||
byter.Seek(0, io.SeekStart)
|
||||
bufr.Reset(byter)
|
||||
r := NewChunkedReader(bufr)
|
||||
n, err := io.ReadFull(r, readBuf)
|
||||
if n != len(readBuf)-1 {
|
||||
t.Fatalf("read %d bytes; want %d", n, len(readBuf)-1)
|
||||
}
|
||||
if err != io.ErrUnexpectedEOF {
|
||||
t.Fatalf("read error = %v; want ErrUnexpectedEOF", err)
|
||||
}
|
||||
})
|
||||
if mallocs > 1.5 {
|
||||
t.Errorf("mallocs = %v; want 1", mallocs)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseHexUint(t *testing.T) {
|
||||
type testCase struct {
|
||||
in string
|
||||
want uint64
|
||||
wantErr string
|
||||
}
|
||||
tests := []testCase{
|
||||
{"x", 0, "invalid byte in chunk length"},
|
||||
{"0000000000000000", 0, ""},
|
||||
{"0000000000000001", 1, ""},
|
||||
{"ffffffffffffffff", 1<<64 - 1, ""},
|
||||
{"000000000000bogus", 0, "invalid byte in chunk length"},
|
||||
{"00000000000000000", 0, "http chunk length too large"}, // could accept if we wanted
|
||||
{"10000000000000000", 0, "http chunk length too large"},
|
||||
{"00000000000000001", 0, "http chunk length too large"}, // could accept if we wanted
|
||||
}
|
||||
for i := uint64(0); i <= 1234; i++ {
|
||||
tests = append(tests, testCase{in: fmt.Sprintf("%x", i), want: i})
|
||||
}
|
||||
for _, tt := range tests {
|
||||
got, err := parseHexUint([]byte(tt.in))
|
||||
if tt.wantErr != "" {
|
||||
if !strings.Contains(fmt.Sprint(err), tt.wantErr) {
|
||||
t.Errorf("parseHexUint(%q) = %v, %v; want error %q", tt.in, got, err, tt.wantErr)
|
||||
}
|
||||
} else {
|
||||
if err != nil || got != tt.want {
|
||||
t.Errorf("parseHexUint(%q) = %v, %v; want %v", tt.in, got, err, tt.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestChunkReadingIgnoresExtensions(t *testing.T) {
|
||||
in := "7;ext=\"some quoted string\"\r\n" + // token=quoted string
|
||||
"hello, \r\n" +
|
||||
"17;someext\r\n" + // token without value
|
||||
"world! 0123456789abcdef\r\n" +
|
||||
"0;someextension=sometoken\r\n" // token=token
|
||||
data, err := io.ReadAll(NewChunkedReader(strings.NewReader(in)))
|
||||
if err != nil {
|
||||
t.Fatalf("ReadAll = %q, %v", data, err)
|
||||
}
|
||||
if g, e := string(data), "hello, world! 0123456789abcdef"; g != e {
|
||||
t.Errorf("read %q; want %q", g, e)
|
||||
}
|
||||
}
|
||||
|
||||
// Issue 17355: ChunkedReader shouldn't block waiting for more data
|
||||
// if it can return something.
|
||||
func TestChunkReadPartial(t *testing.T) {
|
||||
pr, pw := io.Pipe()
|
||||
go func() {
|
||||
pw.Write([]byte("7\r\n1234567"))
|
||||
}()
|
||||
cr := NewChunkedReader(pr)
|
||||
readBuf := make([]byte, 7)
|
||||
n, err := cr.Read(readBuf)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want := "1234567"
|
||||
if n != 7 || string(readBuf) != want {
|
||||
t.Fatalf("Read: %v %q; want %d, %q", n, readBuf[:n], len(want), want)
|
||||
}
|
||||
go func() {
|
||||
pw.Write([]byte("xx"))
|
||||
}()
|
||||
_, err = cr.Read(readBuf)
|
||||
if got := fmt.Sprint(err); !strings.Contains(got, "malformed") {
|
||||
t.Fatalf("second read = %v; want malformed error", err)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Issue 48861: ChunkedReader should report incomplete chunks
|
||||
func TestIncompleteChunk(t *testing.T) {
|
||||
const valid = "4\r\nabcd\r\n" + "5\r\nabc\r\n\r\n" + "0\r\n"
|
||||
|
||||
for i := 0; i < len(valid); i++ {
|
||||
incomplete := valid[:i]
|
||||
r := NewChunkedReader(strings.NewReader(incomplete))
|
||||
if _, err := io.ReadAll(r); err != io.ErrUnexpectedEOF {
|
||||
t.Errorf("expected io.ErrUnexpectedEOF for %q, got %v", incomplete, err)
|
||||
}
|
||||
}
|
||||
|
||||
r := NewChunkedReader(strings.NewReader(valid))
|
||||
if _, err := io.ReadAll(r); err != nil {
|
||||
t.Errorf("unexpected error for %q: %v", valid, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestChunkEndReadError(t *testing.T) {
|
||||
readErr := fmt.Errorf("chunk end read error")
|
||||
|
||||
r := NewChunkedReader(io.MultiReader(strings.NewReader("4\r\nabcd"), iotest.ErrReader(readErr)))
|
||||
if _, err := io.ReadAll(r); err != readErr {
|
||||
t.Errorf("expected %v, got %v", readErr, err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user