Commit 52798024 authored by Brad Fitzpatrick's avatar Brad Fitzpatrick

http2/h2demo: allow alternate hostnames for http and https links

Fixes golang/go#12905

Change-Id: I73b3041f06ca93ad0e75251ca32f077fa9eafe8d
Reviewed-on: https://go-review.googlesource.com/15826Reviewed-by: 's avatarArnout Engelen <arnout@bzzt.net>
Reviewed-by: 's avatarBrad Fitzpatrick <bradfitz@golang.org>
parent 6d10a0c3
......@@ -19,7 +19,6 @@ import (
"log"
"net"
"net/http"
"os/exec"
"path"
"regexp"
"runtime"
......@@ -34,10 +33,13 @@ import (
)
var (
openFirefox = flag.Bool("openff", false, "Open Firefox")
addr = flag.String("addr", "localhost:4430", "TLS address to listen on")
httpAddr = flag.String("httpaddr", "", "If non-empty, address to listen for regular HTTP on")
prod = flag.Bool("prod", false, "Whether to configure itself to be the production http2.golang.org server.")
prod = flag.Bool("prod", false, "Whether to configure itself to be the production http2.golang.org server.")
httpsAddr = flag.String("https_addr", "localhost:4430", "TLS address to listen on ('host:port' or ':port'). Required.")
httpAddr = flag.String("http_addr", "", "Plain HTTP address to listen on ('host:port', or ':port'). Empty means no HTTP.")
hostHTTP = flag.String("http_host", "", "Optional host or host:port to use for http:// links to this service. By default, this is implied from -http_addr.")
hostHTTPS = flag.String("https_host", "", "Optional host or host:port to use for http:// links to this service. By default, this is implied from -https_addr.")
)
func homeOldHTTP(w http.ResponseWriter, r *http.Request) {
......@@ -197,7 +199,7 @@ func registerHandlers() {
tiles.ServeHTTP(w, r)
return
}
http.Redirect(w, r, "https://http2.golang.org/", http.StatusFound)
http.Redirect(w, r, "https://"+httpsHost()+"/", http.StatusFound)
return
}
if r.ProtoMajor == 1 {
......@@ -318,10 +320,10 @@ function showtimes() {
}
func httpsHost() string {
if *prod {
return "http2.golang.org"
if *hostHTTPS != "" {
return *hostHTTPS
}
if v := *addr; strings.HasPrefix(v, ":") {
if v := *httpsAddr; strings.HasPrefix(v, ":") {
return "localhost" + v
} else {
return v
......@@ -329,8 +331,8 @@ func httpsHost() string {
}
func httpHost() string {
if *prod {
return "http2.golang.org"
if *hostHTTP != "" {
return *hostHTTP
}
if v := *httpAddr; strings.HasPrefix(v, ":") {
return "localhost" + v
......@@ -406,29 +408,29 @@ func main() {
var srv http.Server
flag.BoolVar(&http2.VerboseLogs, "verbose", false, "Verbose HTTP/2 debugging.")
flag.Parse()
srv.Addr = *addr
srv.Addr = *httpsAddr
registerHandlers()
if *prod {
*httpAddr = "http2.golang.org"
*hostHTTP = "http2.golang.org"
*hostHTTPS = "http2.golang.org"
log.Fatal(serveProd())
}
url := "https://" + *addr + "/"
url := "https://" + httpsHost() + "/"
log.Printf("Listening on " + url)
http2.ConfigureServer(&srv, &http2.Server{})
if *httpAddr != "" {
go func() { log.Fatal(http.ListenAndServe(*httpAddr, nil)) }()
go func() {
log.Printf("Listening on http://" + httpHost() + "/ (for unencrypted HTTP/1)")
log.Fatal(http.ListenAndServe(*httpAddr, nil))
}()
}
go func() {
log.Fatal(srv.ListenAndServeTLS("server.crt", "server.key"))
}()
if *openFirefox && runtime.GOOS == "darwin" {
time.Sleep(250 * time.Millisecond)
exec.Command("open", "-b", "org.mozilla.nightly", "https://localhost:4430/").Run()
}
select {}
}
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