Skip to content

Commit

Permalink
fix form empty field error when used with pipe (#1417)
Browse files Browse the repository at this point in the history
Co-authored-by: root <[email protected]>
  • Loading branch information
nick9822 and root authored Nov 3, 2022
1 parent a468a7d commit c88dd5d
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 1 deletion.
2 changes: 1 addition & 1 deletion http.go
Original file line number Diff line number Diff line change
Expand Up @@ -2001,7 +2001,7 @@ func writeBodyChunked(w *bufio.Writer, r io.Reader) error {
n, err = r.Read(buf)
if n == 0 {
if err == nil {
panic("BUG: io.Reader returned 0, nil")
continue
}
if err == io.EOF {
if err = writeChunk(w, buf[:0]); err != nil {
Expand Down
84 changes: 84 additions & 0 deletions http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2949,3 +2949,87 @@ func TestResponseBodyStreamErrorOnPanicDuringClose(t *testing.T) {
t.Fatalf("unexpected error value, got: %+v.", e.Error())
}
}

func TestRequestMultipartFormPipeEmptyFormField(t *testing.T) {
t.Parallel()

pr, pw := io.Pipe()
mw := multipart.NewWriter(pw)

errs := make(chan error, 1)

go func() {
defer func() {
err := mw.Close()
if err != nil {
errs <- err
}
err = pw.Close()
if err != nil {
errs <- err
}
close(errs)
}()

if err := mw.WriteField("emptyField", ""); err != nil {
errs <- err
}
}()

var b bytes.Buffer
bw := bufio.NewWriter(&b)
err := writeBodyChunked(bw, pr)

if err != nil {
t.Fatalf("unexpected error: %v", err)
}

for e := range errs {
t.Fatalf("unexpected error in goroutine multiwriter: %v", e)
}

testRequestMultipartFormPipeEmptyFormField(t, mw.Boundary(), b.Bytes(), 1)
}

func testRequestMultipartFormPipeEmptyFormField(t *testing.T, boundary string, formData []byte, partsCount int) []byte {
s := fmt.Sprintf("POST / HTTP/1.1\r\nHost: aaa\r\nContent-Type: multipart/form-data; boundary=%s\r\nTransfer-Encoding: chunked\r\n\r\n%s",
boundary, formData)

var req Request

r := bytes.NewBufferString(s)
br := bufio.NewReader(r)
if err := req.Read(br); err != nil {
t.Fatalf("unexpected error: %v", err)
}

f, err := req.MultipartForm()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
defer req.RemoveMultipartFormFiles()

if len(f.File) > 0 {
t.Fatalf("unexpected files found in the multipart form: %d", len(f.File))
}

if len(f.Value) != partsCount {
t.Fatalf("unexpected number of values found: %d. Expecting %d", len(f.Value), partsCount)
}

for k, vv := range f.Value {
if len(vv) != 1 {
t.Fatalf("unexpected number of values found for key=%q: %d. Expecting 1", k, len(vv))
}
if k != "emptyField" {
t.Fatalf("unexpected key=%q. Expecting %q", k, "emptyField")
}

v := vv[0]
if v != "" {
t.Fatalf("unexpected value=%q. expecting %q", v, "")
}
}

return req.Body()
}

0 comments on commit c88dd5d

Please sign in to comment.