Skip to content

net/http: MaxBytesReader's behavior does not match the comments #10884

Closed
@chanxuehong

Description

@chanxuehong
package main

import (
    "fmt"
    "io"
    "io/ioutil"
    "net/http"
    "strings"
)

func test1(w http.ResponseWriter, r *http.Request) {
    rd := strings.NewReader("12345")
    rc := ioutil.NopCloser(rd)

    mr := http.MaxBytesReader(w, rc, 5)
    fmt.Println(io.Copy(ioutil.Discard, mr))
}

func test2(w http.ResponseWriter, r *http.Request) {
    r.Body = http.MaxBytesReader(w, r.Body, 5)
    fmt.Println(io.Copy(ioutil.Discard, r.Body))
}

func main() {
    http.HandleFunc("/test1", test1)
    http.HandleFunc("/test2", test2)
    http.ListenAndServe(":80", nil)
}

for test1:
the reader rd 's content does not beyond the http.MaxBytesReader mr 's limit, but we got

5 http: request body too large

for test2(use postman, post 12345, use raw, not form-data, x-www-form-urlencoded):

we got

5 <nil>

see
https://github.com/golang/go/blob/master/src/net/http/request.go#L730

if n==l.n and err==nil and next call return n==0 and err==io.EOF, The program does not consider this

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions