Created
December 19, 2015 16:05
-
-
Save odeke-em/5b1fdb40c1ad576300b5 to your computer and use it in GitHub Desktop.
http*-default-user-agent checks while fixing bug https://github.com/golang/go/issues/13685
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"crypto/tls" | |
"io/ioutil" | |
"log" | |
"net/http" | |
) | |
func main() { | |
url := "http://localhost:8080" | |
client := http.DefaultClient | |
if os.Getenv("http1") == "" { | |
url = "https://localhost:8080" | |
tr := &http.Transport{ | |
TLSClientConfig: &tls.Config{ | |
InsecureSkipVerify: true, | |
}, | |
} | |
client = &http.Client{Transport: tr} | |
} | |
res, err := client.Get(url) | |
if err != nil { | |
log.Printf("get: %v\n", err) | |
return | |
} | |
data, _ := ioutil.ReadAll(res.Body) | |
res.Body.Close() | |
log.Printf("data %s\n", data) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
// sample run: | |
// $ http1=1 go run server.go # To run in http1 mode | |
// $ go run server.go # Default in http2 mode | |
import ( | |
"fmt" | |
"log" | |
"net/http" | |
"os" | |
"time" | |
) | |
func timeNow(w http.ResponseWriter, r *http.Request) { | |
log.Println("got a request from ", r) | |
fmt.Fprintf(w, "the time now is: %q", time.Now()) | |
fmt.Fprintf(w, "User-Agent: %q", r.UserAgent()) | |
} | |
func main() { | |
var server http.Server | |
server.Addr = ":8080" | |
http.HandleFunc("/", timeNow) | |
http.HandleFunc("/time", timeNow) | |
var err error | |
if os.Getenv("http1") != "" { | |
log.Println("http1 mode running") | |
err = server.ListenAndServe() | |
} else { | |
log.Println("http2 mode running") | |
err = server.ListenAndServeTLS("rosedn.cert", "rosedn.key") | |
} | |
if err != nil { | |
log.Printf("servingTLS: %v\n", err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment