Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

net/http: server not setting Content-Length in certain cases #44953

Open
muirdm opened this issue Mar 11, 2021 · 6 comments
Open

net/http: server not setting Content-Length in certain cases #44953

muirdm opened this issue Mar 11, 2021 · 6 comments
Assignees
Labels
Milestone

Comments

@muirdm
Copy link

@muirdm muirdm commented Mar 11, 2021

What version of Go are you using (go version)?

$ go version
go version go1.16.2 linux/amd64

Does this issue reproduce with the latest release?

Yes.

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/muir/.cache/go-build"
GOENV="/Users/muir/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/Users/muir/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/Users/muir/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.16.2"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="[redacted]"
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-build1046132156=/tmp/go-build -gno-record-gcc-switches"

What did you do?

https://play.golang.org/p/w-t_Fq5N2JD (not runnable ATM due to playground issue).

What did you expect to see?

"Content-Length" set to 1 in the response, as in Go 1.15.

What did you see instead?

"Content-Length" not set in the response.

@toothrot toothrot added this to the Backlog milestone Mar 11, 2021
@toothrot
Copy link
Contributor

@toothrot toothrot commented Mar 11, 2021

/cc @neild

I'll look into the playground timeouts separately.

@muirdm
Copy link
Author

@muirdm muirdm commented Mar 12, 2021

This seems to be incorrect behavior in Go 1.16 based on the documented http.ResponseWriter.Write behavior: if the total size of all written data is under a few KB and there are no Flush calls, the Content-Length header is added automatically..

	// If WriteHeader has not yet been called, Write calls
	// WriteHeader(http.StatusOK) before writing the data. If the Header
	// does not contain a Content-Type line, Write adds a Content-Type set
	// to the result of passing the initial 512 bytes of written data to
	// DetectContentType. Additionally, if the total size of all written
	// data is under a few KB and there are no Flush calls, the
	// Content-Length header is added automatically.
@neild
Copy link
Contributor

@neild neild commented Mar 12, 2021

This is caused by CL 249238, which changes ResponseWriter.ReadFrom to issue a single 512 byte read for Content-Type detection, where previously it used io.Copy with a 512-byte io.LimitReader.

I think this is a bug in that CL; content-detection should read up to 512 bytes, issuing multiple reads if necessary.

@gopherbot
Copy link

@gopherbot gopherbot commented Mar 12, 2021

Change https://golang.org/cl/301449 mentions this issue: net/http: fix ResponseWriter.ReadFrom with short reads

@networkimprov
Copy link

@networkimprov networkimprov commented Mar 13, 2021

@gopherbot please backport to 1.16; this is a regression.

@gopherbot
Copy link

@gopherbot gopherbot commented Mar 13, 2021

Backport issue(s) opened: #44984 (for 1.16).

Remember to create the cherry-pick CL(s) as soon as the patch is submitted to master, according to https://golang.org/wiki/MinorReleases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
6 participants