Simple HTTP2 echo headers
This file contains 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 ( | |
"flag" | |
"fmt" | |
"log" | |
"net/http" | |
"time" | |
) | |
var keyPath string | |
var crtPath string | |
func init() { | |
flag.StringVar(&keyPath, "tls-key", "server.key", "Path of TLS private key") | |
flag.StringVar(&crtPath, "tls-crt", "server.crt", "Path of TLS certificate / public key") | |
flag.Parse() | |
} | |
func main() { | |
srv := &http.Server{Addr: ":8000", Handler: http.HandlerFunc(handle)} | |
log.Printf("Serving on https://0.0.0.0:8000") | |
// to generate keys: openssl req -newkey rsa:2048 -nodes -keyout server.key -x509 -days 365 -out server.crt | |
log.Fatal(srv.ListenAndServeTLS(crtPath, keyPath)) | |
} | |
func handle(w http.ResponseWriter, r *http.Request) { | |
delay, err := time.ParseDuration(r.URL.Query().Get("delay")) | |
if err == nil { | |
time.Sleep(delay) | |
} | |
fmt.Fprintf(w, "%s %s %s\n", r.Method, r.RequestURI, r.Proto) | |
log.Printf("%s %s %s - (remoteAddr: %s, delay: %v)\n", r.Method, r.RequestURI, r.Proto, r.RemoteAddr, delay) | |
for name, headers := range r.Header { | |
for _, h := range headers { | |
fmt.Fprintf(w, "%v: %v\n", name, h) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment