github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/tools/godoc/redirect/redirect.go (about) 1 // Copyright 2013 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 redirect provides hooks to register HTTP handlers that redirect old 6 // godoc paths to their new equivalents and assist in accessing the issue 7 // tracker, wiki, code review system, etc. 8 package redirect // import "golang.org/x/tools/godoc/redirect" 9 10 import ( 11 "fmt" 12 "net/http" 13 "os" 14 "regexp" 15 "strconv" 16 "strings" 17 ) 18 19 // Register registers HTTP handlers that redirect old godoc paths to their new 20 // equivalents and assist in accessing the issue tracker, wiki, code review 21 // system, etc. If mux is nil it uses http.DefaultServeMux. 22 func Register(mux *http.ServeMux) { 23 if mux == nil { 24 mux = http.DefaultServeMux 25 } 26 handlePathRedirects(mux, pkgRedirects, "/pkg/") 27 handlePathRedirects(mux, cmdRedirects, "/cmd/") 28 for prefix, redirect := range prefixHelpers { 29 p := "/" + prefix + "/" 30 mux.Handle(p, PrefixHandler(p, redirect)) 31 } 32 for path, redirect := range redirects { 33 mux.Handle(path, Handler(redirect)) 34 } 35 // NB: /src/pkg (sans trailing slash) is the index of packages. 36 mux.HandleFunc("/src/pkg/", srcPkgHandler) 37 mux.HandleFunc("/cl/", clHandler) 38 mux.HandleFunc("/change/", changeHandler) 39 mux.HandleFunc("/design/", designHandler) 40 } 41 42 func handlePathRedirects(mux *http.ServeMux, redirects map[string]string, prefix string) { 43 for source, target := range redirects { 44 h := Handler(prefix + target + "/") 45 p := prefix + source 46 mux.Handle(p, h) 47 mux.Handle(p+"/", h) 48 } 49 } 50 51 // Packages that were renamed between r60 and go1. 52 var pkgRedirects = map[string]string{ 53 "asn1": "encoding/asn1", 54 "big": "math/big", 55 "cmath": "math/cmplx", 56 "csv": "encoding/csv", 57 "exec": "os/exec", 58 "exp/template/html": "html/template", 59 "gob": "encoding/gob", 60 "http": "net/http", 61 "http/cgi": "net/http/cgi", 62 "http/fcgi": "net/http/fcgi", 63 "http/httptest": "net/http/httptest", 64 "http/pprof": "net/http/pprof", 65 "json": "encoding/json", 66 "mail": "net/mail", 67 "rand": "math/rand", 68 "rpc": "net/rpc", 69 "rpc/jsonrpc": "net/rpc/jsonrpc", 70 "scanner": "text/scanner", 71 "smtp": "net/smtp", 72 "tabwriter": "text/tabwriter", 73 "template": "text/template", 74 "template/parse": "text/template/parse", 75 "url": "net/url", 76 "utf16": "unicode/utf16", 77 "utf8": "unicode/utf8", 78 "xml": "encoding/xml", 79 } 80 81 // Commands that were renamed between r60 and go1. 82 var cmdRedirects = map[string]string{ 83 "gofix": "fix", 84 "goinstall": "go", 85 "gopack": "pack", 86 "gotest": "go", 87 "govet": "vet", 88 "goyacc": "yacc", 89 } 90 91 var redirects = map[string]string{ 92 "/blog": "/blog/", 93 "/build": "http://build.golang.org", 94 "/change": "https://go.googlesource.com/go", 95 "/cl": "https://go-review.googlesource.com", 96 "/cmd/godoc/": "http://godoc.org/golang.org/x/tools/cmd/godoc/", 97 "/issue": "https://github.com/golang/go/issues", 98 "/issue/new": "https://github.com/golang/go/issues/new", 99 "/issues": "https://github.com/golang/go/issues", 100 "/issues/new": "https://github.com/golang/go/issues/new", 101 "/play": "http://play.golang.org", 102 "/design": "https://github.com/golang/proposal/tree/master/design", 103 104 // In Go 1.2 the references page is part of /doc/. 105 "/ref": "/doc/#references", 106 // This next rule clobbers /ref/spec and /ref/mem. 107 // TODO(adg): figure out what to do here, if anything. 108 // "/ref/": "/doc/#references", 109 110 // Be nice to people who are looking in the wrong place. 111 "/doc/mem": "/ref/mem", 112 "/doc/spec": "/ref/spec", 113 114 "/talks": "http://talks.golang.org", 115 "/tour": "http://tour.golang.org", 116 "/wiki": "https://github.com/golang/go/wiki", 117 118 "/doc/articles/c_go_cgo.html": "/blog/c-go-cgo", 119 "/doc/articles/concurrency_patterns.html": "/blog/go-concurrency-patterns-timing-out-and", 120 "/doc/articles/defer_panic_recover.html": "/blog/defer-panic-and-recover", 121 "/doc/articles/error_handling.html": "/blog/error-handling-and-go", 122 "/doc/articles/gobs_of_data.html": "/blog/gobs-of-data", 123 "/doc/articles/godoc_documenting_go_code.html": "/blog/godoc-documenting-go-code", 124 "/doc/articles/gos_declaration_syntax.html": "/blog/gos-declaration-syntax", 125 "/doc/articles/image_draw.html": "/blog/go-imagedraw-package", 126 "/doc/articles/image_package.html": "/blog/go-image-package", 127 "/doc/articles/json_and_go.html": "/blog/json-and-go", 128 "/doc/articles/json_rpc_tale_of_interfaces.html": "/blog/json-rpc-tale-of-interfaces", 129 "/doc/articles/laws_of_reflection.html": "/blog/laws-of-reflection", 130 "/doc/articles/slices_usage_and_internals.html": "/blog/go-slices-usage-and-internals", 131 "/doc/go_for_cpp_programmers.html": "/wiki/GoForCPPProgrammers", 132 "/doc/go_tutorial.html": "http://tour.golang.org/", 133 } 134 135 var prefixHelpers = map[string]string{ 136 "issue": "https://github.com/golang/go/issues/", 137 "issues": "https://github.com/golang/go/issues/", 138 "play": "http://play.golang.org/", 139 "talks": "http://talks.golang.org/", 140 "wiki": "https://github.com/golang/go/wiki/", 141 } 142 143 func Handler(target string) http.Handler { 144 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 145 url := target 146 if qs := r.URL.RawQuery; qs != "" { 147 url += "?" + qs 148 } 149 http.Redirect(w, r, url, http.StatusMovedPermanently) 150 }) 151 } 152 153 var validId = regexp.MustCompile(`^[A-Za-z0-9-]*$`) 154 155 func PrefixHandler(prefix, baseURL string) http.Handler { 156 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 157 if p := r.URL.Path; p == prefix { 158 // redirect /prefix/ to /prefix 159 http.Redirect(w, r, p[:len(p)-1], http.StatusFound) 160 return 161 } 162 id := r.URL.Path[len(prefix):] 163 if !validId.MatchString(id) { 164 http.Error(w, "Not found", http.StatusNotFound) 165 return 166 } 167 target := baseURL + id 168 http.Redirect(w, r, target, http.StatusFound) 169 }) 170 } 171 172 // Redirect requests from the old "/src/pkg/foo" to the new "/src/foo". 173 // See http://golang.org/s/go14nopkg 174 func srcPkgHandler(w http.ResponseWriter, r *http.Request) { 175 r.URL.Path = "/src/" + r.URL.Path[len("/src/pkg/"):] 176 http.Redirect(w, r, r.URL.String(), http.StatusMovedPermanently) 177 } 178 179 func clHandler(w http.ResponseWriter, r *http.Request) { 180 const prefix = "/cl/" 181 if p := r.URL.Path; p == prefix { 182 // redirect /prefix/ to /prefix 183 http.Redirect(w, r, p[:len(p)-1], http.StatusFound) 184 return 185 } 186 id := r.URL.Path[len(prefix):] 187 // support /cl/152700045/, which is used in commit 0edafefc36. 188 id = strings.TrimSuffix(id, "/") 189 if !validId.MatchString(id) { 190 http.Error(w, "Not found", http.StatusNotFound) 191 return 192 } 193 target := "" 194 // the first CL in rietveld is about 152046, so only treat the id as 195 // a rietveld CL if it is larger than 150000. 196 if n, err := strconv.Atoi(id); err == nil && n > 150000 { 197 target = "https://codereview.appspot.com/" + id 198 } else { 199 target = "https://go-review.googlesource.com/r/" + id 200 } 201 http.Redirect(w, r, target, http.StatusFound) 202 } 203 204 var changeMap *hashMap 205 206 // LoadChangeMap loads the specified map of Mercurial to Git revisions, 207 // which is used by the /change/ handler to intelligently map old hg 208 // revisions to their new git equivalents. 209 // It should be called before calling Register. 210 // The file should remain open as long as the process is running. 211 // See the implementation of this package for details. 212 func LoadChangeMap(filename string) error { 213 f, err := os.Open(filename) 214 if err != nil { 215 return err 216 } 217 m, err := newHashMap(f) 218 if err != nil { 219 return err 220 } 221 changeMap = m 222 return nil 223 } 224 225 func changeHandler(w http.ResponseWriter, r *http.Request) { 226 const prefix = "/change/" 227 if p := r.URL.Path; p == prefix { 228 // redirect /prefix/ to /prefix 229 http.Redirect(w, r, p[:len(p)-1], http.StatusFound) 230 return 231 } 232 hash := r.URL.Path[len(prefix):] 233 target := "https://go.googlesource.com/go/+/" + hash 234 if git := changeMap.Lookup(hash); git > 0 { 235 target = fmt.Sprintf("https://go.googlesource.com/%v/+/%v", git.Repo(), git.Hash()) 236 } 237 http.Redirect(w, r, target, http.StatusFound) 238 } 239 240 func designHandler(w http.ResponseWriter, r *http.Request) { 241 const prefix = "/design/" 242 if p := r.URL.Path; p == prefix { 243 // redirect /prefix/ to /prefix 244 http.Redirect(w, r, p[:len(p)-1], http.StatusFound) 245 return 246 } 247 name := r.URL.Path[len(prefix):] 248 target := "https://github.com/golang/proposal/blob/master/design/" + name + ".md" 249 http.Redirect(w, r, target, http.StatusFound) 250 }