summaryrefslogtreecommitdiff
path: root/src/cmd/fix/httpheaders.go
blob: 15c21ac8675fd957279636f5bbaf6a508b75fcf0 (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
67
// 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"
)

func init() {
	register(httpHeadersFix)
}

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

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

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":  {},
		"*http.Response": {},
	},
}