mirror of
https://github.com/soypat/lneto.git
synced 2026-08-08 08:53:40 +00:00
finish rounding up multipart form parsing
This commit is contained in:
@@ -114,11 +114,11 @@ func (h *Header) TryParse(asResponse bool) (needMoreData bool, err error) {
|
||||
if asResponse && h.statusCode.len == 0 || !asResponse && h.requestTarget.start == 0 {
|
||||
err = h.parseFirstLine(asResponse)
|
||||
if err != nil {
|
||||
return err == errNeedMore, err
|
||||
return err == ErrNeedMoreData, err
|
||||
}
|
||||
}
|
||||
err = h.parseNextHeaders()
|
||||
return err == errNeedMore, err
|
||||
return err == ErrNeedMoreData, err
|
||||
}
|
||||
|
||||
// ParsingSuccess returns true if TryParse was successful, that is to say it returned needMoreData==false and err==nil.
|
||||
|
||||
@@ -411,7 +411,7 @@ func TestHeader_LargeBufferOverflow(t *testing.T) {
|
||||
}
|
||||
|
||||
// a complete but malformed header line with no colon must be a hard error,
|
||||
// not errNeedMore (which makes a streaming parser wait forever).
|
||||
// not ErrNeedMoreData (which makes a streaming parser wait forever).
|
||||
func TestHeader_ColonlessLineIsHardError(t *testing.T) {
|
||||
raw := "GET / HTTP/1.1\r\nBadHeaderNoColon\r\n\r\n"
|
||||
var h Header
|
||||
@@ -419,8 +419,8 @@ func TestHeader_ColonlessLineIsHardError(t *testing.T) {
|
||||
if err == nil {
|
||||
t.Fatal("want error on colonless header line, got nil")
|
||||
}
|
||||
if err == errNeedMore {
|
||||
t.Fatalf("colonless line reported as errNeedMore (parser would hang); want a hard error like errInvalidName")
|
||||
if err == ErrNeedMoreData {
|
||||
t.Fatalf("colonless line reported as ErrNeedMoreData (parser would hang); want a hard error like errInvalidName")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -437,8 +437,8 @@ func TestHeader_SplitBeforeColonStillParses(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
needMore, err := h.TryParse(false)
|
||||
if err != nil && err != errNeedMore {
|
||||
t.Fatalf("split before colon: want errNeedMore/nil, got %v", err)
|
||||
if err != nil && err != ErrNeedMoreData {
|
||||
t.Fatalf("split before colon: want ErrNeedMoreData/nil, got %v", err)
|
||||
}
|
||||
if !needMore {
|
||||
t.Fatal("want needMoreData=true after partial input")
|
||||
|
||||
+136
-112
@@ -1,37 +1,159 @@
|
||||
package httpraw
|
||||
|
||||
// Multipart bodies frame their fields with a delimiter instead of escaping them,
|
||||
// so a part's value has no length: it ends where the next delimiter begins. The
|
||||
// functions below split a body without storing any of it, leaving the caller to
|
||||
// decide what to keep, what to skip and when a part has grown too large:
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
)
|
||||
|
||||
// Multipart splits a "multipart/form-data" body into its parts. Such bodies
|
||||
// frame their fields with a delimiter instead of escaping them, so a part's
|
||||
// value has no length: it ends where the next delimiter begins.
|
||||
//
|
||||
// boundary := httpraw.MultipartBoundary(contentType)
|
||||
// Multipart stores none of the body, leaving the caller to decide what to keep,
|
||||
// what to skip and when a part has grown too large:
|
||||
//
|
||||
// m := httpraw.Multipart{Boundary: httpraw.MultipartBoundary(contentType)}
|
||||
// var hdr httpraw.MultipartHeader
|
||||
// for {
|
||||
// hdr, rest, err := httpraw.NextPartHeader(buf, boundary)
|
||||
// rest, err := m.NextHeader(&hdr, buf)
|
||||
// if err != nil {
|
||||
// break // ErrEndOfParts, or ErrNeedMoreData: read more into buf and retry.
|
||||
// break // io.EOF, or ErrNeedMoreData: read more into buf and retry.
|
||||
// }
|
||||
// name := httpraw.PartName(hdr)
|
||||
// for {
|
||||
// body, next, done := httpraw.NextPartBody(rest, boundary)
|
||||
// // Consume body, then compact next to the front of buf and read more.
|
||||
// body, next, done := m.NextBody(rest)
|
||||
// // Consume body for hdr.Name, then compact next to the front of buf
|
||||
// // and read more.
|
||||
// rest = next
|
||||
// if done {
|
||||
// break
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
type Multipart struct {
|
||||
// Boundary is the delimiter parameter of the body's Content-Type field,
|
||||
// without the leading "--" the delimiter carries on the wire.
|
||||
Boundary []byte
|
||||
}
|
||||
|
||||
func ()
|
||||
// MultipartHeader is a part's header block and the Content-Disposition
|
||||
// parameters that identify it. All fields alias the buffer they were parsed
|
||||
// from and stay valid as long as it does.
|
||||
type MultipartHeader struct {
|
||||
// Part is the part's raw header block, ending in its final CRLF.
|
||||
Part []byte
|
||||
// Name is the name parameter of a part's Content-Disposition field,
|
||||
// i.e: "photo" for `form-data; name="photo"; filename="beach.png"`.
|
||||
Name []byte
|
||||
// Filename is the filename parameter of a part's Content-Disposition
|
||||
// field, nil when the part is not a file upload.
|
||||
Filename []byte
|
||||
}
|
||||
|
||||
// MultipartBoundary returns the boundary parameter of a Content-Type field value,
|
||||
// SetContentType sets the boundary parameter of a Content-Type field value,
|
||||
// i.e: "abc123" for "multipart/form-data; boundary=abc123". The leading "--" of
|
||||
// the wire delimiter is not included. Returns nil if there is no such parameter.
|
||||
func MultipartBoundary(contentType []byte) []byte { return ContentParam(contentType, "boundary") }
|
||||
func (m *Multipart) SetContentType(contentType []byte) error {
|
||||
m.Boundary = ContentParam(contentType, "boundary")
|
||||
if len(m.Boundary) == 0 || len(m.Boundary) > 70 {
|
||||
return errNoBoundary // RFC 2046 5.1.1: 1..70 characters, required.
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// NextHeader splits the leading part's header block off a multipart body into
|
||||
// dst, returning the body bytes that follow it. Returns [ErrNeedMoreData] while
|
||||
// data holds no complete delimiter and header block, and [io.EOF] once the
|
||||
// closing delimiter is reached. dst is left zeroed on error.
|
||||
func (m *Multipart) NextHeader(dst *MultipartHeader, data []byte) (rest []byte, err error) {
|
||||
*dst = MultipartHeader{}
|
||||
if len(m.Boundary) == 0 {
|
||||
return nil, errNoBoundary
|
||||
}
|
||||
idx := m.indexDelimiter(data)
|
||||
if idx < 0 {
|
||||
return nil, ErrNeedMoreData
|
||||
}
|
||||
after := idx + len("--") + len(m.Boundary)
|
||||
if after+2 > len(data) {
|
||||
return nil, ErrNeedMoreData // Cannot tell a closing delimiter yet.
|
||||
} else if data[after] == '-' && data[after+1] == '-' {
|
||||
return nil, io.EOF
|
||||
}
|
||||
// Delimiter is followed by CRLF, then the part's header block.
|
||||
if data[after] == '\r' {
|
||||
after++
|
||||
}
|
||||
if after >= len(data) {
|
||||
return nil, ErrNeedMoreData
|
||||
} else if data[after] != '\n' {
|
||||
return nil, errInvalidName // Junk between delimiter and part.
|
||||
}
|
||||
after++
|
||||
end := bytes.Index(data[after:], []byte("\r\n\r\n"))
|
||||
if end < 0 {
|
||||
return nil, ErrNeedMoreData
|
||||
}
|
||||
dst.Part = data[after : after+end+2]
|
||||
disposition := partField(dst.Part)
|
||||
dst.Name = ContentParam(disposition, "name")
|
||||
dst.Filename = ContentParam(disposition, "filename")
|
||||
return data[after+end+4:], nil
|
||||
}
|
||||
|
||||
// NextBody returns the part bytes available in data, holding back any tail
|
||||
// that could be the start of a delimiter. done reports the part ended, in which
|
||||
// case rest begins the next part's delimiter; otherwise rest is the held back
|
||||
// tail, which the caller compacts before reading more data into the buffer.
|
||||
func (m *Multipart) NextBody(data []byte) (body, rest []byte, done bool) {
|
||||
idx := m.indexPartEnd(data)
|
||||
if idx >= 0 {
|
||||
return data[:idx], data[idx+len("\r\n"):], true
|
||||
}
|
||||
// Longest prefix of "\r\n--"+boundary that could still be completed.
|
||||
hold := len("\r\n--") + len(m.Boundary) - 1
|
||||
if hold > len(data) {
|
||||
hold = len(data)
|
||||
}
|
||||
return data[:len(data)-hold], data[len(data)-hold:], false
|
||||
}
|
||||
|
||||
// indexDelimiter returns the offset of the leading "--"+Boundary in data.
|
||||
func (m *Multipart) indexDelimiter(data []byte) int {
|
||||
for i := 0; i+len("--")+len(m.Boundary) <= len(data); i++ {
|
||||
dash := bytes.IndexByte(data[i:], '-')
|
||||
if dash < 0 {
|
||||
return -1
|
||||
}
|
||||
i += dash
|
||||
if i+len("--")+len(m.Boundary) > len(data) {
|
||||
return -1
|
||||
}
|
||||
if data[i+1] == '-' && b2s(data[i+2:i+2+len(m.Boundary)]) == b2s(m.Boundary) {
|
||||
return i
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
// indexPartEnd returns the offset of the CRLF that closes a part, that is the
|
||||
// CRLF preceding the next delimiter.
|
||||
func (m *Multipart) indexPartEnd(data []byte) int {
|
||||
for i := 0; i+len("\r\n--")+len(m.Boundary) <= len(data); i++ {
|
||||
cr := bytes.IndexByte(data[i:], '\r')
|
||||
if cr < 0 {
|
||||
return -1
|
||||
}
|
||||
i += cr
|
||||
if i+len("\r\n--")+len(m.Boundary) > len(data) {
|
||||
return -1
|
||||
}
|
||||
if data[i+1] == '\n' && data[i+2] == '-' && data[i+3] == '-' &&
|
||||
b2s(data[i+4:i+4+len(m.Boundary)]) == b2s(m.Boundary) {
|
||||
return i
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
// ContentParam returns the value of a parameter of a header field value, i.e:
|
||||
// "utf-8" for key "charset" of "text/plain; charset=utf-8". Quoted values are
|
||||
@@ -73,66 +195,6 @@ func ContentParam(value []byte, key string) []byte {
|
||||
return nil
|
||||
}
|
||||
|
||||
// NextPartHeader splits the leading part's header block off a multipart body,
|
||||
// returning it with its final CRLF and the body bytes that follow it. Returns
|
||||
// [ErrNeedMoreData] while data holds no complete delimiter and header block, and
|
||||
// [ErrEndOfParts] once the closing delimiter is reached.
|
||||
func NextPartHeader(data, boundary []byte) (partHdr, rest []byte, err error) {
|
||||
if len(boundary) == 0 {
|
||||
return nil, nil, errNoBoundary
|
||||
}
|
||||
idx := indexDelimiter(data, boundary)
|
||||
if idx < 0 {
|
||||
return nil, nil, ErrNeedMoreData
|
||||
}
|
||||
after := idx + len("--") + len(boundary)
|
||||
if after+2 > len(data) {
|
||||
return nil, nil, ErrNeedMoreData // Cannot tell a closing delimiter yet.
|
||||
} else if data[after] == '-' && data[after+1] == '-' {
|
||||
return nil, nil, ErrEndOfParts
|
||||
}
|
||||
// Delimiter is followed by CRLF, then the part's header block.
|
||||
if data[after] == '\r' {
|
||||
after++
|
||||
}
|
||||
if after >= len(data) {
|
||||
return nil, nil, ErrNeedMoreData
|
||||
} else if data[after] != '\n' {
|
||||
return nil, nil, errInvalidName // Junk between delimiter and part.
|
||||
}
|
||||
after++
|
||||
end := bytes.Index(data[after:], []byte("\r\n\r\n"))
|
||||
if end < 0 {
|
||||
return nil, nil, ErrNeedMoreData
|
||||
}
|
||||
return data[after : after+end+2], data[after+end+4:], nil
|
||||
}
|
||||
|
||||
// NextPartBody returns the part bytes available in data, holding back any tail
|
||||
// that could be the start of a delimiter. done reports the part ended, in which
|
||||
// case rest begins the next part's delimiter; otherwise rest is the held back
|
||||
// tail, which the caller compacts before reading more data into the buffer.
|
||||
func NextPartBody(data, boundary []byte) (body, rest []byte, done bool) {
|
||||
idx := indexPartEnd(data, boundary)
|
||||
if idx >= 0 {
|
||||
return data[:idx], data[idx+len("\r\n"):], true
|
||||
}
|
||||
// Longest prefix of "\r\n--"+boundary that could still be completed.
|
||||
hold := len("\r\n--") + len(boundary) - 1
|
||||
if hold > len(data) {
|
||||
hold = len(data)
|
||||
}
|
||||
return data[:len(data)-hold], data[len(data)-hold:], false
|
||||
}
|
||||
|
||||
// PartName returns the name parameter of a part's Content-Disposition field,
|
||||
// i.e: "photo" for `form-data; name="photo"; filename="beach.png"`.
|
||||
func PartName(partHdr []byte) []byte { return ContentParam(partField(partHdr), "name") }
|
||||
|
||||
// PartFileName returns the filename parameter of a part's Content-Disposition
|
||||
// field, nil when the part is not a file upload.
|
||||
func PartFileName(partHdr []byte) []byte { return ContentParam(partField(partHdr), "filename") }
|
||||
|
||||
// partField returns the Content-Disposition field value of a part header block.
|
||||
func partField(partHdr []byte) []byte {
|
||||
const key = "content-disposition"
|
||||
@@ -152,44 +214,6 @@ func partField(partHdr []byte) []byte {
|
||||
return nil
|
||||
}
|
||||
|
||||
// indexDelimiter returns the offset of the leading "--"+boundary in data.
|
||||
func indexDelimiter(data, boundary []byte) int {
|
||||
for i := 0; i+len("--")+len(boundary) <= len(data); i++ {
|
||||
dash := bytes.IndexByte(data[i:], '-')
|
||||
if dash < 0 {
|
||||
return -1
|
||||
}
|
||||
i += dash
|
||||
if i+len("--")+len(boundary) > len(data) {
|
||||
return -1
|
||||
}
|
||||
if data[i+1] == '-' && b2s(data[i+2:i+2+len(boundary)]) == b2s(boundary) {
|
||||
return i
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
// indexPartEnd returns the offset of the CRLF that closes a part, that is the
|
||||
// CRLF preceding the next delimiter.
|
||||
func indexPartEnd(data, boundary []byte) int {
|
||||
for i := 0; i+len("\r\n--")+len(boundary) <= len(data); i++ {
|
||||
cr := bytes.IndexByte(data[i:], '\r')
|
||||
if cr < 0 {
|
||||
return -1
|
||||
}
|
||||
i += cr
|
||||
if i+len("\r\n--")+len(boundary) > len(data) {
|
||||
return -1
|
||||
}
|
||||
if data[i+1] == '\n' && data[i+2] == '-' && data[i+3] == '-' &&
|
||||
b2s(data[i+4:i+4+len(boundary)]) == b2s(boundary) {
|
||||
return i
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
// trimOWS trims optional whitespace off both ends of b, RFC 9110 5.6.3.
|
||||
func trimOWS(b []byte) []byte {
|
||||
for len(b) > 0 && (b[0] == ' ' || b[0] == '\t') {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package httpraw
|
||||
|
||||
import (
|
||||
"io"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
@@ -23,6 +24,7 @@ const (
|
||||
)
|
||||
|
||||
func TestMultipartBoundary(t *testing.T) {
|
||||
var mp Multipart
|
||||
for _, test := range []struct {
|
||||
contentType string
|
||||
want string
|
||||
@@ -34,8 +36,12 @@ func TestMultipartBoundary(t *testing.T) {
|
||||
{contentType: "multipart/form-data", want: ""}, // Absent.
|
||||
{contentType: "application/x-www-form-urlencoded", want: ""},
|
||||
} {
|
||||
got := MultipartBoundary([]byte(test.contentType))
|
||||
if string(got) != test.want {
|
||||
err := mp.SetContentType([]byte(test.contentType))
|
||||
if err != nil {
|
||||
t.Skip("asdasd")
|
||||
}
|
||||
got := string(mp.Boundary)
|
||||
if got != test.want {
|
||||
t.Errorf("%q: want %q, got %q", test.contentType, test.want, got)
|
||||
}
|
||||
}
|
||||
@@ -63,14 +69,21 @@ func TestContentParam(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestNextPartHeader(t *testing.T) {
|
||||
boundary := []byte(multiBoundary)
|
||||
hdr, rest, err := NextPartHeader([]byte(multiBody), boundary)
|
||||
m := Multipart{Boundary: []byte(multiBoundary)}
|
||||
var hdr MultipartHeader
|
||||
rest, err := m.NextHeader(&hdr, []byte(multiBody))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
const wantHdr = "Content-Disposition: form-data; name=\"caption\"\r\n"
|
||||
if string(hdr) != wantHdr {
|
||||
t.Errorf("want header %q, got %q", wantHdr, hdr)
|
||||
if string(hdr.Part) != wantHdr {
|
||||
t.Errorf("want header %q, got %q", wantHdr, hdr.Part)
|
||||
}
|
||||
if string(hdr.Name) != "caption" {
|
||||
t.Errorf("want name %q, got %q", "caption", hdr.Name)
|
||||
}
|
||||
if hdr.Filename != nil {
|
||||
t.Errorf("want nil filename for a non file part, got %q", hdr.Filename)
|
||||
}
|
||||
if !strings.HasPrefix(string(rest), "hi there\r\n") {
|
||||
t.Errorf("want rest at part body, got %q", rest)
|
||||
@@ -79,14 +92,15 @@ func TestNextPartHeader(t *testing.T) {
|
||||
|
||||
// Incomplete data must ask for more, never guess.
|
||||
func TestNextPartHeaderNeedMore(t *testing.T) {
|
||||
boundary := []byte(multiBoundary)
|
||||
m := Multipart{Boundary: []byte(multiBoundary)}
|
||||
for _, data := range []string{
|
||||
"",
|
||||
"------abc", // Delimiter cut short.
|
||||
"------abc123\r\n", // No header block yet.
|
||||
"------abc123\r\nContent-Disposition: form-", // Header block unterminated.
|
||||
} {
|
||||
if _, _, err := NextPartHeader([]byte(data), boundary); err != ErrNeedMoreData {
|
||||
var hdr MultipartHeader
|
||||
if _, err := m.NextHeader(&hdr, []byte(data)); err != ErrNeedMoreData {
|
||||
t.Errorf("%q: want ErrNeedMoreData, got %v", data, err)
|
||||
}
|
||||
}
|
||||
@@ -94,19 +108,21 @@ func TestNextPartHeaderNeedMore(t *testing.T) {
|
||||
|
||||
// The closing delimiter ends iteration.
|
||||
func TestNextPartHeaderEnd(t *testing.T) {
|
||||
boundary := []byte(multiBoundary)
|
||||
if _, _, err := NextPartHeader([]byte("------abc123--\r\n"), boundary); err != ErrEndOfParts {
|
||||
t.Errorf("want ErrEndOfParts, got %v", err)
|
||||
m := Multipart{Boundary: []byte(multiBoundary)}
|
||||
var hdr MultipartHeader
|
||||
if _, err := m.NextHeader(&hdr, []byte("------abc123--\r\n")); err != io.EOF {
|
||||
t.Errorf("want io.EOF, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNextPartBody(t *testing.T) {
|
||||
boundary := []byte(multiBoundary)
|
||||
_, rest, err := NextPartHeader([]byte(multiBody), boundary)
|
||||
m := Multipart{Boundary: []byte(multiBoundary)}
|
||||
var hdr MultipartHeader
|
||||
rest, err := m.NextHeader(&hdr, []byte(multiBody))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
body, rest, done := NextPartBody(rest, boundary)
|
||||
body, rest, done := m.NextBody(rest)
|
||||
if !done {
|
||||
t.Fatal("want the part to end within the buffer")
|
||||
}
|
||||
@@ -120,18 +136,19 @@ func TestNextPartBody(t *testing.T) {
|
||||
|
||||
// A part whose bytes contain CRLFs and boundary-like text must survive intact.
|
||||
func TestNextPartBodyBinary(t *testing.T) {
|
||||
boundary := []byte(multiBoundary)
|
||||
m := Multipart{Boundary: []byte(multiBoundary)}
|
||||
data := []byte(multiBody)
|
||||
_, rest, err := NextPartHeader(data, boundary) // caption part.
|
||||
var hdr MultipartHeader
|
||||
rest, err := m.NextHeader(&hdr, data) // caption part.
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, rest, _ = NextPartBody(rest, boundary)
|
||||
_, rest, err = NextPartHeader(rest, boundary) // photo part.
|
||||
_, rest, _ = m.NextBody(rest)
|
||||
rest, err = m.NextHeader(&hdr, rest) // photo part.
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
body, rest, done := NextPartBody(rest, boundary)
|
||||
body, rest, done := m.NextBody(rest)
|
||||
if !done {
|
||||
t.Fatal("want the part to end within the buffer")
|
||||
}
|
||||
@@ -139,19 +156,19 @@ func TestNextPartBodyBinary(t *testing.T) {
|
||||
if string(body) != want {
|
||||
t.Errorf("want body %q, got %q", want, body)
|
||||
}
|
||||
if _, _, err = NextPartHeader(rest, boundary); err != ErrEndOfParts {
|
||||
t.Errorf("want ErrEndOfParts after last part, got %v", err)
|
||||
if _, err = m.NextHeader(&hdr, rest); err != io.EOF {
|
||||
t.Errorf("want io.EOF after last part, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// A delimiter split across two reads must not be mistaken for part data: the
|
||||
// tail is held back until proven not to be a delimiter.
|
||||
func TestNextPartBodySplitDelimiter(t *testing.T) {
|
||||
boundary := []byte(multiBoundary)
|
||||
m := Multipart{Boundary: []byte(multiBoundary)}
|
||||
const part = "hi there"
|
||||
full := part + "\r\n------abc123\r\n"
|
||||
for split := 1; split < len(full); split++ {
|
||||
body, rest, done := NextPartBody([]byte(full[:split]), boundary)
|
||||
body, rest, done := m.NextBody([]byte(full[:split]))
|
||||
if done {
|
||||
continue // Whole delimiter already present, nothing to prove.
|
||||
}
|
||||
@@ -164,41 +181,62 @@ func TestNextPartBodySplitDelimiter(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestPartNameFileName(t *testing.T) {
|
||||
const photo = "Content-Disposition: form-data; name=\"photo\"; filename=\"beach.png\"\r\n" +
|
||||
"Content-Type: image/png\r\n"
|
||||
if got := string(PartName([]byte(photo))); got != "photo" {
|
||||
// A file part carries both parameters, and the raw block stays available.
|
||||
func TestNextHeaderFilePart(t *testing.T) {
|
||||
m := Multipart{Boundary: []byte(multiBoundary)}
|
||||
var hdr MultipartHeader
|
||||
rest, err := m.NextHeader(&hdr, []byte(multiBody)) // caption part.
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, rest, _ = m.NextBody(rest)
|
||||
if _, err = m.NextHeader(&hdr, rest); err != nil { // photo part.
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := string(hdr.Name); got != "photo" {
|
||||
t.Errorf("want name %q, got %q", "photo", got)
|
||||
}
|
||||
if got := string(PartFileName([]byte(photo))); got != "beach.png" {
|
||||
if got := string(hdr.Filename); got != "beach.png" {
|
||||
t.Errorf("want filename %q, got %q", "beach.png", got)
|
||||
}
|
||||
const caption = "Content-Disposition: form-data; name=\"caption\"\r\n"
|
||||
if got := string(PartName([]byte(caption))); got != "caption" {
|
||||
t.Errorf("want name %q, got %q", "caption", got)
|
||||
if !strings.Contains(string(hdr.Part), "Content-Type: image/png") {
|
||||
t.Errorf("want the raw block to hold every field, got %q", hdr.Part)
|
||||
}
|
||||
if got := PartFileName([]byte(caption)); got != nil {
|
||||
t.Errorf("want nil filename for a non file part, got %q", got)
|
||||
}
|
||||
|
||||
// A failed call must not leave the previous part's fields behind.
|
||||
func TestNextHeaderZeroesOnError(t *testing.T) {
|
||||
m := Multipart{Boundary: []byte(multiBoundary)}
|
||||
var hdr MultipartHeader
|
||||
if _, err := m.NextHeader(&hdr, []byte(multiBody)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := m.NextHeader(&hdr, []byte("------abc123--\r\n")); err != io.EOF {
|
||||
t.Fatalf("want io.EOF, got %v", err)
|
||||
}
|
||||
if hdr.Part != nil || hdr.Name != nil || hdr.Filename != nil {
|
||||
t.Errorf("want zeroed header on error, got %+v", hdr)
|
||||
}
|
||||
}
|
||||
|
||||
// The whole loop, as a caller writes it.
|
||||
func TestMultipartLoop(t *testing.T) {
|
||||
boundary := []byte(multiBoundary)
|
||||
m := Multipart{Boundary: []byte(multiBoundary)}
|
||||
rest := []byte(multiBody)
|
||||
var got []string
|
||||
var hdr MultipartHeader
|
||||
for {
|
||||
hdr, next, err := NextPartHeader(rest, boundary)
|
||||
if err == ErrEndOfParts {
|
||||
next, err := m.NextHeader(&hdr, rest)
|
||||
if err == io.EOF {
|
||||
break
|
||||
} else if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
name := string(PartName(hdr))
|
||||
name := string(hdr.Name)
|
||||
total := 0
|
||||
rest = next
|
||||
for {
|
||||
body, next, done := NextPartBody(rest, boundary)
|
||||
body, next, done := m.NextBody(rest)
|
||||
total += len(body)
|
||||
rest = next
|
||||
if done {
|
||||
@@ -208,7 +246,7 @@ func TestMultipartLoop(t *testing.T) {
|
||||
}
|
||||
got = append(got, name+":"+strconv.Itoa(total))
|
||||
}
|
||||
want := "caption:8|photo:29"
|
||||
want := "caption:8|photo:28"
|
||||
if strings.Join(got, "|") != want {
|
||||
t.Errorf("want %q, got %q", want, strings.Join(got, "|"))
|
||||
}
|
||||
|
||||
+17
-14
@@ -11,12 +11,15 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
errNoProto = errors.New("missing protocol, HTTP/0.9 unsupported")
|
||||
errNeedMore = errors.New("need more data: cannot find trailing lf")
|
||||
errUnparsed = errors.New("need to finish parsing")
|
||||
errInvalidName = errors.New("invalid header name")
|
||||
errSmallBuffer = errors.New("small read buffer. Increase ReadBufferSize")
|
||||
errOOM = errors.New("httpraw: buffer out of memory")
|
||||
errNoProto = errors.New("missing protocol, HTTP/0.9 unsupported")
|
||||
// ErrNeedMoreData signals a parser was handed an incomplete buffer: append
|
||||
// more data to it and call again.
|
||||
ErrNeedMoreData = errors.New("need more data: cannot find trailing lf/delimiter")
|
||||
errNoBoundary = errors.New("httpraw: multipart boundary not set")
|
||||
errUnparsed = errors.New("need to finish parsing")
|
||||
errInvalidName = errors.New("invalid header name")
|
||||
errSmallBuffer = errors.New("small read buffer. Increase ReadBufferSize")
|
||||
errOOM = errors.New("httpraw: buffer out of memory")
|
||||
// Header.Set and Header.Add mangles the buffer.
|
||||
// Call them after retrieving the Body. Do not call them before parsing the header (why would you even do that?).
|
||||
errMangledBuffer = errors.New("httpraw: mangled buffer")
|
||||
@@ -179,11 +182,11 @@ func (hb *headerBuf) parseFirstLineRequest(initFlags Flags) (method, uri, proto
|
||||
hb.skipLeadingCRLF()
|
||||
flags = initFlags
|
||||
if bytes.IndexByte(hb.offBuf(), '\n') < 0 {
|
||||
return method, uri, proto, flags, errNeedMore // Incomplete line.
|
||||
return method, uri, proto, flags, ErrNeedMoreData // Incomplete line.
|
||||
}
|
||||
b := hb.scanLine()
|
||||
if len(b) < 5 {
|
||||
return method, uri, proto, flags, errNeedMore
|
||||
return method, uri, proto, flags, ErrNeedMoreData
|
||||
}
|
||||
debuglog("http:req:parse")
|
||||
|
||||
@@ -213,18 +216,18 @@ func (hb *headerBuf) parseFirstLineResponse(initFlags Flags) (statusCode, status
|
||||
hb.skipLeadingCRLF()
|
||||
flags = initFlags
|
||||
if bytes.IndexByte(hb.offBuf(), '\n') < 0 {
|
||||
return statusCode, statusText, flags, errNeedMore // Incomplete line.
|
||||
return statusCode, statusText, flags, ErrNeedMoreData // Incomplete line.
|
||||
}
|
||||
b := hb.scanLine()
|
||||
if len(b) < 5 {
|
||||
return statusCode, statusText, flags, errNeedMore
|
||||
return statusCode, statusText, flags, ErrNeedMoreData
|
||||
}
|
||||
debuglog("http:resp:parse")
|
||||
|
||||
// Parse protocol (e.g. "HTTP/1.1"), then status code, then status text.
|
||||
protoEnd := bytes.IndexByte(b, ' ')
|
||||
if protoEnd < 0 {
|
||||
return statusCode, statusText, flags, errNeedMore
|
||||
return statusCode, statusText, flags, ErrNeedMoreData
|
||||
}
|
||||
if b2s(b[:protoEnd]) != strHTTP11 {
|
||||
flags |= flagNoHTTP11
|
||||
@@ -444,7 +447,7 @@ func (hb *headerBuf) next(ss *scannerState) argsKV {
|
||||
if x < 0 {
|
||||
// A header name should always at some point be followed by a \n
|
||||
// even if it's the one that terminates the header block.
|
||||
ss.err = errNeedMore
|
||||
ss.err = ErrNeedMoreData
|
||||
return hb.noKV()
|
||||
} else if x < n {
|
||||
// There was a \n before the colon! This is invalid.
|
||||
@@ -454,7 +457,7 @@ func (hb *headerBuf) next(ss *scannerState) argsKV {
|
||||
// A newline is present (x>=0 reached here) but the line has no
|
||||
// colon: malformed, not incomplete. A split arriving before the
|
||||
// colon has no newline yet and is caught by the x<0 branch above,
|
||||
// so it still returns errNeedMore.
|
||||
// so it still returns ErrNeedMoreData.
|
||||
ss.err = errInvalidName
|
||||
return hb.noKV()
|
||||
}
|
||||
@@ -482,7 +485,7 @@ func (hb *headerBuf) next(ss *scannerState) argsKV {
|
||||
nl := bytes.IndexByte(buf[n:], '\n')
|
||||
if nl < 0 || nl+n+1 == len(buf) {
|
||||
// No newline or newline is last character and can't know if is multiline.
|
||||
ss.err = errNeedMore
|
||||
ss.err = ErrNeedMoreData
|
||||
return hb.noKV()
|
||||
}
|
||||
n += nl + 1 // Index of the newly found newline.
|
||||
|
||||
Reference in New Issue
Block a user