diff options
Diffstat (limited to 'src/cmd/fix/httpfs.go')
-rw-r--r-- | src/cmd/fix/httpfs.go | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/src/cmd/fix/httpfs.go b/src/cmd/fix/httpfs.go new file mode 100644 index 000000000..d87b30f9d --- /dev/null +++ b/src/cmd/fix/httpfs.go @@ -0,0 +1,70 @@ +// 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" + "go/token" +) + +func init() { + register(httpFileSystemFix) +} + +var httpFileSystemFix = fix{ + "httpfs", + "2011-06-27", + httpfs, + `Adapt http FileServer to take a FileSystem. + +http://codereview.appspot.com/4629047 http FileSystem interface +`, +} + +func httpfs(f *ast.File) bool { + if !imports(f, "http") { + return false + } + + fixed := false + walk(f, func(n interface{}) { + call, ok := n.(*ast.CallExpr) + if !ok || !isPkgDot(call.Fun, "http", "FileServer") { + return + } + if len(call.Args) != 2 { + return + } + dir, prefix := call.Args[0], call.Args[1] + call.Args = []ast.Expr{&ast.CallExpr{ + Fun: &ast.SelectorExpr{ + X: ast.NewIdent("http"), + Sel: ast.NewIdent("Dir"), + }, + Args: []ast.Expr{dir}, + }} + wrapInStripHandler := true + if prefixLit, ok := prefix.(*ast.BasicLit); ok { + if prefixLit.Kind == token.STRING && (prefixLit.Value == `"/"` || prefixLit.Value == `""`) { + wrapInStripHandler = false + } + } + if wrapInStripHandler { + call.Fun.(*ast.SelectorExpr).Sel = ast.NewIdent("StripPrefix") + call.Args = []ast.Expr{ + prefix, + &ast.CallExpr{ + Fun: &ast.SelectorExpr{ + X: ast.NewIdent("http"), + Sel: ast.NewIdent("FileServer"), + }, + Args: call.Args, + }, + } + } + fixed = true + }) + return fixed +} |