remove number for source
This commit is contained in:
39
src/canonical-hosts/canonical-hosts.go
Normal file
39
src/canonical-hosts/canonical-hosts.go
Normal file
@@ -0,0 +1,39 @@
|
||||
// ## Canonical Hosts
|
||||
|
||||
package main
|
||||
|
||||
import "net/http"
|
||||
import "strings"
|
||||
import "fmt"
|
||||
|
||||
type handler http.HandlerFunc
|
||||
|
||||
func hello(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "text/plain")
|
||||
fmt.Fprintln(w, "Hello canonical world")
|
||||
}
|
||||
|
||||
func wrapCanonicalHost(f handler, chost string) handler {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
hostPort := strings.Split(r.Host, ":")
|
||||
host := hostPort[0]
|
||||
if host != chost {
|
||||
fmt.Println("redirect to", chost)
|
||||
hostPort[0] = chost
|
||||
url := "http://" +
|
||||
strings.Join(hostPort, ":") +
|
||||
r.URL.String()
|
||||
http.Redirect(w, r, url, 301)
|
||||
return
|
||||
}
|
||||
f(w, r)
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
handler := wrapCanonicalHost(hello, "localhost")
|
||||
http.HandleFunc("/", handler)
|
||||
http.ListenAndServe(":5000", nil)
|
||||
}
|
||||
|
||||
// todo: comment about https canonicalization
|
||||
4
src/canonical-hosts/canonical-hosts.sh
Normal file
4
src/canonical-hosts/canonical-hosts.sh
Normal file
@@ -0,0 +1,4 @@
|
||||
$ go run canonical-hosts.go
|
||||
|
||||
$ curl -i -L http://127.0.0.1:5000/go
|
||||
$ curl -i -L http://127.0.0.1:5000/go
|
||||
Reference in New Issue
Block a user