1
0
mirror of https://github.com/golang/go synced 2024-11-17 23:04:56 -07:00

net/http: check If-Range header when request method is HEAD

When If-Range does not match and the requested resource is
available, server should return a "200 OK" response to client.
Currently server returns "200 OK" when the request method is
GET, but "206 Partial Content" when method is HEAD.
This change fixed this inconsistency.

Change-Id: I5ad979919f4f089baba54a4445b70ca38471a906
Reviewed-on: https://go-review.googlesource.com/54110
Run-TryBot: Tom Bergan <tombergan@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Tom Bergan <tombergan@google.com>
This commit is contained in:
Joe Kyo 2017-08-09 03:26:45 +01:00 committed by Tom Bergan
parent ff560ee950
commit 6a7c4d69cb
2 changed files with 50 additions and 33 deletions

View File

@ -445,7 +445,7 @@ func checkIfModifiedSince(r *Request, modtime time.Time) condResult {
}
func checkIfRange(w ResponseWriter, r *Request, modtime time.Time) condResult {
if r.Method != "GET" {
if r.Method != "GET" && r.Method != "HEAD" {
return condNone
}
ir := r.Header.get("If-Range")

View File

@ -895,6 +895,17 @@ func TestServeContent(t *testing.T) {
wantContentRange: "bytes 0-4/8",
wantLastMod: "Wed, 25 Jun 2014 17:12:18 GMT",
},
"range_with_modtime_mismatch": {
file: "testdata/style.css",
modtime: time.Date(2014, 6, 25, 17, 12, 18, 0 /* nanos */, time.UTC),
reqHeader: map[string]string{
"Range": "bytes=0-4",
"If-Range": "Wed, 25 Jun 2014 17:12:19 GMT",
},
wantStatus: StatusOK,
wantContentType: "text/css; charset=utf-8",
wantLastMod: "Wed, 25 Jun 2014 17:12:18 GMT",
},
"range_with_modtime_nanos": {
file: "testdata/style.css",
modtime: time.Date(2014, 6, 25, 17, 12, 18, 123 /* nanos */, time.UTC),
@ -982,6 +993,11 @@ func TestServeContent(t *testing.T) {
} else {
content = tt.content
}
for _, method := range []string{"GET", "HEAD"} {
//restore content in case it is consumed by previous method
if content, ok := content.(*strings.Reader); ok {
content.Seek(io.SeekStart, 0)
}
servec <- serveParam{
name: filepath.Base(tt.file),
@ -990,7 +1006,7 @@ func TestServeContent(t *testing.T) {
etag: tt.serveETag,
contentType: tt.serveContentType,
}
req, err := NewRequest("GET", ts.URL, nil)
req, err := NewRequest(method, ts.URL, nil)
if err != nil {
t.Fatal(err)
}
@ -1006,16 +1022,17 @@ func TestServeContent(t *testing.T) {
io.Copy(ioutil.Discard, res.Body)
res.Body.Close()
if res.StatusCode != tt.wantStatus {
t.Errorf("test %q: status = %d; want %d", testName, res.StatusCode, tt.wantStatus)
t.Errorf("test %q using %q: got status = %d; want %d", testName, method, res.StatusCode, tt.wantStatus)
}
if g, e := res.Header.Get("Content-Type"), tt.wantContentType; g != e {
t.Errorf("test %q: content-type = %q, want %q", testName, g, e)
t.Errorf("test %q using %q: got content-type = %q, want %q", testName, method, g, e)
}
if g, e := res.Header.Get("Content-Range"), tt.wantContentRange; g != e {
t.Errorf("test %q: content-range = %q, want %q", testName, g, e)
t.Errorf("test %q using %q: got content-range = %q, want %q", testName, method, g, e)
}
if g, e := res.Header.Get("Last-Modified"), tt.wantLastMod; g != e {
t.Errorf("test %q: last-modified = %q, want %q", testName, g, e)
t.Errorf("test %q using %q: got last-modified = %q, want %q", testName, method, g, e)
}
}
}
}