Repository: go
Issue: net/http: Request.ParseMultipartForm doesn't read all the body
Labels: NeedsInvestigation

<!-- Please answer these questions before submitting your issue. Thanks! -->

### What version of Go are you using (`go version`)?

<pre>
$ go version
tested in go1.12.6 and go1.13beta1
</pre>

### Does this issue reproduce with the latest release?
yes


### What operating system and processor architecture are you using (`go env`)?
Ubuntu 18.04.2 LTS
<details><summary><code>go env</code> Output</summary><br><pre>
$ go env
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/tevic/.cache/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/tevic/Work/WorkSpace/Code/GoProjs"
GOPROXY=""
GORACE=""
GOROOT="/home/tevic/Work/RunTime/Go"
GOTMPDIR=""
GOTOOLDIR="/home/tevic/Work/RunTime/Go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build281284214=/tmp/go-build -gno-record-gcc-switches"
</pre></details>

### What did you do?
I send a multipart form request with a trailer, and the server side will parse the form use ParseMultipartForm,after that i get trailer from request. But sometimes it doesn't show up.
This is my example. Two test servers are for comparison. srvReadBody works like a charm while srvParseForm don't.
```
package main

import (
	"bytes"
	"errors"
	"fmt"
	"hash"
	"io"
	"io/ioutil"
	"log"
	"mime/multipart"
	"net/http"
	"net/http/httptest"
)

type eofReaderFunc func()

func (f eofReaderFunc) Read(p []byte) (n int, err error) {
	f()
	return 0, io.EOF
}

func main() {
	var testTrailer = "TestTrailer"
	srvReadBody := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		_, err := io.Copy(ioutil.Discard, r.Body)
		if err != nil {
			log.Println(err)
			return
		}
		if r.Trailer.Get(testTrailer) == "" {
			fmt.Println("srvReadBody: Trailer is empty.")
		}
	}))

	srvParseForm := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		err := r.ParseMultipartForm(1024 * 1024 * 16)
		if err != nil {
			log.Println(err)
			return
		}
		if r.Trailer.Get(testTrailer) == "" {
			fmt.Println("srvParseForm: Trailer is empty.")
		}
	}))

	var sendRequestWithTrailer = func(addr string) {

		buf := make([]byte, 1024)
		body := &bytes.Buffer{}
		writer := multipart.NewWriter(body)
		part, err := writer.CreateFormFile("file", "testFile")
		if err != nil {
			log.Println(err)
			return
		}
		_, err = io.Copy(part, bytes.NewReader(buf))
		if err != nil {
			log.Fatal(err)
		}
		err = writer.Close()
		if err != nil {
			log.Println(err)
			return
		}

		var req *http.Request
		req, err = http.NewRequest("POST", addr, io.MultiReader(body, eofReaderFunc(func() {
			req.Trailer.Set(testTrailer, testTrailer)
		})))
		if err != nil {
			log.Println(err)
			return
		}
		req.Header.Set("Content-Type", writer.FormDataContentType())
		req.Trailer = http.Header{testTrailer: nil}
		resp, err := http.DefaultClient.Do(req)
		if err != nil {
			log.Println(err)
			return
		}
		defer resp.Body.Close()
	}

	for i := 0; i < 100; i++ {
		sendRequestWithTrailer(srvReadBody.URL)
		sendRequestWithTrailer(srvParseForm.URL)
	}
}

```



### What did you expect to see?
`r.Trailer.Get(testTrailer) != ""`


### What did you see instead?
Sometimes `r.Trailer.Get(testTrailer) == ""`
The code output like:
```
srvParseForm: Trailer is empty.
srvParseForm: Trailer is empty.
srvParseForm: Trailer is empty.
srvParseForm: Trailer is empty.
srvParseForm: Trailer is empty.
srvParseForm: Trailer is empty.
srvParseForm: Trailer is empty.
srvParseForm: Trailer is empty.
srvParseForm: Trailer is empty.
srvParseForm: Trailer is empty.
srvParseForm: Trailer is empty.
srvParseForm: Trailer is empty.
srvParseForm: Trailer is empty.
srvParseForm: Trailer is empty.
srvParseForm: Trailer is empty.
```

I've trace the code and the problem is due to this line. When isFinalBoundary match the condition the body may have not been completely read.
https://github.com/golang/go/blob/39b533ed6634079a5c4f6a3488f4b01cdab2d833/src/mime/multipart/multipart.go#L336