Skip to content

Instantly share code, notes, and snippets.

@alibo
Created July 20, 2020 18:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alibo/b7f03f6f7f5363446aa3f67fee2577a2 to your computer and use it in GitHub Desktop.
Save alibo/b7f03f6f7f5363446aa3f67fee2577a2 to your computer and use it in GitHub Desktop.
Simple HTTP2 echo headers
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