Message:
Hello r (cc: golan...@googlegroups.com),
I'd like you to review this change.
Description:
http: handle HEAD requests correctly
Please review this at http://codereview.appspot.com/3939042/
Affected files:
M src/pkg/http/fs.go
M src/pkg/http/server.go
Index: src/pkg/http/fs.go
===================================================================
--- a/src/pkg/http/fs.go
+++ b/src/pkg/http/fs.go
@@ -173,8 +173,10 @@
w.SetHeader("Content-Length", strconv.Itoa64(size))
w.WriteHeader(code)
-
- io.Copyn(w, f, size)
+
+ if r.Method != "HEAD" {
+ io.Copyn(w, f, size)
+ }
}
// ServeFile replies to the request with the contents of the named file or
directory.
Index: src/pkg/http/server.go
===================================================================
--- a/src/pkg/http/server.go
+++ b/src/pkg/http/server.go
@@ -181,7 +181,9 @@
w.SetHeader("Content-Type", "text/html; charset=utf-8")
w.SetHeader("Date", time.UTC().Format(TimeFormat))
- if req.ProtoAtLeast(1, 1) {
+ if req.Method == "HEAD" {
+ // do nothing
+ } else if req.ProtoAtLeast(1, 1) {
// HTTP/1.1 or greater: use chunked transfer encoding
// to avoid closing the connection at EOF.
w.chunking = true
@@ -268,7 +270,7 @@
return 0, nil
}
- if w.status == StatusNotModified {
+ if w.status == StatusNotModified || w.req.Method == "HEAD" {
// Must not have body.
return 0, ErrBodyNotAllowed
}
@@ -495,11 +497,11 @@
// RFC2616 recommends that a short note "SHOULD" be included in the
// response because older user agents may not understand 301/307.
- note := "<a href=\"" + htmlEscape(url) + "\">" + statusText[code]
+ "</a>.\n"
- if r.Method == "POST" {
- note = ""
+ // Shouldn't send the response for POST or HEAD; that leaves GET.
+ if r.Method == "GET" {
+ note := "<a href=\"" + htmlEscape(url) + "\">" + statusText[code]
+ "</a>.\n"
+ fmt.Fprintln(w, note)
}
- fmt.Fprintln(w, note)
}
func htmlEscape(s string) string {
http: handle HEAD requests correctly
R=r, r2
CC=golang-dev
http://codereview.appspot.com/3939042