~john-koepi/ubuntu/trusty/golang/default

« back to all changes in this revision

Viewing changes to src/cmd/gofix/httpheaders.go

  • Committer: Bazaar Package Importer
  • Author(s): Ondřej Surý
  • Date: 2011-08-03 17:04:59 UTC
  • mfrom: (14.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20110803170459-wzd99m3567y80ila
Tags: 1:59-1
* Imported Upstream version 59
* Refresh patches to a new release
* Fix FTBFS on ARM (Closes: #634270)
* Update version.bash to work with Debian packaging and not hg
  repository

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2011 The Go Authors.  All rights reserved.
 
2
// Use of this source code is governed by a BSD-style
 
3
// license that can be found in the LICENSE file.
 
4
 
 
5
package main
 
6
 
 
7
import (
 
8
        "go/ast"
 
9
)
 
10
 
 
11
var httpHeadersFix = fix{
 
12
        "httpheaders",
 
13
        httpheaders,
 
14
        `Rename http Referer, UserAgent, Cookie, SetCookie, which are now methods.
 
15
 
 
16
http://codereview.appspot.com/4620049/
 
17
`,
 
18
}
 
19
 
 
20
func init() {
 
21
        register(httpHeadersFix)
 
22
}
 
23
 
 
24
func httpheaders(f *ast.File) bool {
 
25
        if !imports(f, "http") {
 
26
                return false
 
27
        }
 
28
 
 
29
        called := make(map[ast.Node]bool)
 
30
        walk(f, func(ni interface{}) {
 
31
                switch n := ni.(type) {
 
32
                case *ast.CallExpr:
 
33
                        called[n.Fun] = true
 
34
                }
 
35
        })
 
36
 
 
37
        fixed := false
 
38
        typeof := typecheck(headerTypeConfig, f)
 
39
        walk(f, func(ni interface{}) {
 
40
                switch n := ni.(type) {
 
41
                case *ast.SelectorExpr:
 
42
                        if called[n] {
 
43
                                break
 
44
                        }
 
45
                        if t := typeof[n.X]; t != "*http.Request" && t != "*http.Response" {
 
46
                                break
 
47
                        }
 
48
                        switch n.Sel.Name {
 
49
                        case "Referer", "UserAgent":
 
50
                                n.Sel.Name += "()"
 
51
                                fixed = true
 
52
                        case "Cookie":
 
53
                                n.Sel.Name = "Cookies()"
 
54
                                fixed = true
 
55
                        }
 
56
                }
 
57
        })
 
58
        return fixed
 
59
}
 
60
 
 
61
var headerTypeConfig = &TypeConfig{
 
62
        Type: map[string]*Type{
 
63
                "*http.Request":  &Type{},
 
64
                "*http.Response": &Type{},
 
65
        },
 
66
}