summaryrefslogtreecommitdiff
path: root/src/cmd/gofix/httpheaders.go
blob: 8a9080e8e372eb03c3b08b63c414b0c89155c9fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Copyright 2011 The Go Authors.  All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package main

import (
	"go/ast"
)

var httpHeadersFix = fix{
	"httpheaders",
	httpheaders,
	`Rename http Referer, UserAgent, Cookie, SetCookie, which are now methods.

http://codereview.appspot.com/4620049/
`,
}

func init() {
	register(httpHeadersFix)
}

func httpheaders(f *ast.File) bool {
	if !imports(f, "http") {
		return false
	}

	called := make(map[ast.Node]bool)
	walk(f, func(ni interface{}) {
		switch n := ni.(type) {
		case *ast.CallExpr:
			called[n.Fun] = true
		}
	})

	fixed := false
	typeof := typecheck(headerTypeConfig, f)
	walk(f, func(ni interface{}) {
		switch n := ni.(type) {
		case *ast.SelectorExpr:
			if called[n] {
				break
			}
			if t := typeof[n.X]; t != "*http.Request" && t != "*http.Response" {
				break
			}
			switch n.Sel.Name {
			case "Referer", "UserAgent":
				n.Sel.Name += "()"
				fixed = true
			case "Cookie":
				n.Sel.Name = "Cookies()"
				fixed = true
			}
		}
	})
	return fixed
}

var headerTypeConfig = &TypeConfig{
	Type: map[string]*Type{
		"*http.Request":  &Type{},
		"*http.Response": &Type{},
	},
}