Commit ffcf1bed authored by Jaana Burcu Dogan's avatar Jaana Burcu Dogan

http2/h2demo: don't auto redirect the HTTP-only serverpush demo to HTTPS

Change-Id: Ib30bbe789718bf1e6d455c0011f27c3db6f7cba5
Reviewed-on: https://go-review.googlesource.com/38783Reviewed-by: 's avatarTom Bergan <tombergan@google.com>
Reviewed-by: 's avatarBrad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Tom Bergan <tombergan@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent 05d32051
...@@ -231,14 +231,18 @@ func clockStreamHandler(w http.ResponseWriter, r *http.Request) { ...@@ -231,14 +231,18 @@ func clockStreamHandler(w http.ResponseWriter, r *http.Request) {
func registerHandlers() { func registerHandlers() {
tiles := newGopherTilesHandler() tiles := newGopherTilesHandler()
push := newPushHandler()
mux2 := http.NewServeMux() mux2 := http.NewServeMux()
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if r.TLS == nil { switch {
if r.URL.Path == "/gophertiles" { case r.URL.Path == "/gophertiles":
tiles.ServeHTTP(w, r) tiles.ServeHTTP(w, r) // allow HTTP/2 + HTTP/1.x
return return
} case strings.HasPrefix(r.URL.Path, "/serverpush"):
push.ServeHTTP(w, r) // allow HTTP/2 + HTTP/1.x
return
case r.TLS == nil: // do not allow HTTP/1.x for anything else
http.Redirect(w, r, "https://"+httpsHost()+"/", http.StatusFound) http.Redirect(w, r, "https://"+httpsHost()+"/", http.StatusFound)
return return
} }
...@@ -259,11 +263,6 @@ func registerHandlers() { ...@@ -259,11 +263,6 @@ func registerHandlers() {
mux2.HandleFunc("/crc32", crcHandler) mux2.HandleFunc("/crc32", crcHandler)
mux2.HandleFunc("/ECHO", echoCapitalHandler) mux2.HandleFunc("/ECHO", echoCapitalHandler)
mux2.HandleFunc("/clockstream", clockStreamHandler) mux2.HandleFunc("/clockstream", clockStreamHandler)
mux2.HandleFunc("/serverpush", pushHandler)
mux2.Handle("/serverpush/static/jquery.min.js", fileServer("https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js", 100*time.Millisecond))
mux2.Handle("/serverpush/static/godocs.js", fileServer("https://golang.org/lib/godoc/godocs.js", 100*time.Millisecond))
mux2.Handle("/serverpush/static/playground.js", fileServer("https://golang.org/lib/godoc/playground.js", 100*time.Millisecond))
mux2.Handle("/serverpush/static/style.css", fileServer("https://golang.org/lib/godoc/style.css", 100*time.Millisecond))
mux2.Handle("/gophertiles", tiles) mux2.Handle("/gophertiles", tiles)
mux2.HandleFunc("/redirect", func(w http.ResponseWriter, r *http.Request) { mux2.HandleFunc("/redirect", func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/", http.StatusFound) http.Redirect(w, r, "/", http.StatusFound)
...@@ -276,35 +275,44 @@ func registerHandlers() { ...@@ -276,35 +275,44 @@ func registerHandlers() {
}) })
} }
var pushResources = []string{ var pushResources = map[string]http.Handler{
"/serverpush/static/jquery.min.js", "/serverpush/static/jquery.min.js": fileServer("https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js", 100*time.Millisecond),
"/serverpush/static/godocs.js", "/serverpush/static/godocs.js": fileServer("https://golang.org/lib/godoc/godocs.js", 100*time.Millisecond),
"/serverpush/static/playground.js", "/serverpush/static/playground.js": fileServer("https://golang.org/lib/godoc/playground.js", 100*time.Millisecond),
"/serverpush/static/style.css", "/serverpush/static/style.css": fileServer("https://golang.org/lib/godoc/style.css", 100*time.Millisecond),
} }
func pushHandler(w http.ResponseWriter, r *http.Request) { func newPushHandler() http.Handler {
cacheBust := time.Now().UnixNano() return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if pusher, ok := w.(http.Pusher); ok { for path, handler := range pushResources {
for _, resource := range pushResources { if r.URL.Path == path {
url := fmt.Sprintf("%s?%d", resource, cacheBust) handler.ServeHTTP(w, r)
if err := pusher.Push(url, nil); err != nil { return
log.Printf("Failed to push %v: %v", resource, err)
} }
} }
}
time.Sleep(100 * time.Millisecond) // fake network latency + parsing time cacheBust := time.Now().UnixNano()
if err := pushTmpl.Execute(w, struct { if pusher, ok := w.(http.Pusher); ok {
CacheBust int64 for path := range pushResources {
HTTPSHost string url := fmt.Sprintf("%s?%d", path, cacheBust)
HTTPHost string if err := pusher.Push(url, nil); err != nil {
}{ log.Printf("Failed to push %v: %v", path, err)
CacheBust: cacheBust, }
HTTPSHost: httpsHost(), }
HTTPHost: httpHost(), }
}); err != nil { time.Sleep(100 * time.Millisecond) // fake network latency + parsing time
log.Printf("Executing server push template: %v", err) if err := pushTmpl.Execute(w, struct {
} CacheBust int64
HTTPSHost string
HTTPHost string
}{
CacheBust: cacheBust,
HTTPSHost: httpsHost(),
HTTPHost: httpHost(),
}); err != nil {
log.Printf("Executing server push template: %v", err)
}
})
} }
func newGopherTilesHandler() http.Handler { func newGopherTilesHandler() http.Handler {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment