gofix: httpserver - rewrite rw.SetHeader to rw.Header.Set

R=rsc
CC=golang-dev
https://golang.org/cl/4271048
This commit is contained in:
Andrew Gerrand 2011-03-17 09:59:18 +11:00
parent 5dd0869bf5
commit 4bd0a54435
2 changed files with 20 additions and 1 deletions

View File

@ -51,7 +51,7 @@ func httpserver(f *ast.File) bool {
// Look for w.UsingTLS() and w.Remoteaddr().
call, ok := n.(*ast.CallExpr)
if !ok || len(call.Args) != 0 {
if !ok || (len(call.Args) != 0 && len(call.Args) != 2) {
return
}
sel, ok := call.Fun.(*ast.SelectorExpr)
@ -102,6 +102,21 @@ func httpserver(f *ast.File) bool {
Sel: ast.NewIdent("RemoteAddr"),
}
fixed = true
case "SetHeader":
// replace w.SetHeader with w.Header().Set
// or w.Header().Del if second argument is ""
sel.X = &ast.CallExpr{
Fun: &ast.SelectorExpr{
X: ast.NewIdent(w.String()),
Sel: ast.NewIdent("Header"),
},
}
sel.Sel = ast.NewIdent("Set")
if len(call.Args) == 2 && isEmptyString(call.Args[1]) {
sel.Sel = ast.NewIdent("Del")
call.Args = call.Args[:1]
}
fixed = true
}
})
}

View File

@ -16,6 +16,8 @@ var httpserverTests = []testCase{
import "http"
func f(xyz http.ResponseWriter, abc *http.Request, b string) {
xyz.SetHeader("foo", "bar")
xyz.SetHeader("baz", "")
xyz.Hijack()
xyz.Flush()
go xyz.Hijack()
@ -33,6 +35,8 @@ func f(xyz http.ResponseWriter, abc *http.Request, b string) {
import "http"
func f(xyz http.ResponseWriter, abc *http.Request, b string) {
xyz.Header().Set("foo", "bar")
xyz.Header().Del("baz")
xyz.(http.Hijacker).Hijack()
xyz.(http.Flusher).Flush()
go xyz.(http.Hijacker).Hijack()