Skip to content

Instantly share code, notes, and snippets.

@jacob-delgado
Created July 22, 2021 23:56
Show Gist options
  • Save jacob-delgado/20bdcbacb2978bda046f797954ba4ba8 to your computer and use it in GitHub Desktop.
Save jacob-delgado/20bdcbacb2978bda046f797954ba4ba8 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"net"
"net/http"
)
func headers(w http.ResponseWriter, req *http.Request) {
for name, headers := range req.Header {
for _, h := range headers {
fmt.Fprintf(w, "%v: %v\n", name, h)
}
}
fmt.Fprintf(w, "remote addr: %v\n", req.RemoteAddr)
fmt.Fprintf(w, "host addr: %v\n", req.Host)
}
func main() {
http.HandleFunc("/headers", headers)
go http.ListenAndServe("127.0.0.1:8090", nil)
ipv6 := ipv6OnlyHandler{
headersFunc: headers,
}
ListenAndServe(":8090", ipv6)
}
type ipv6OnlyHandler struct {
headersFunc func(w http.ResponseWriter, req *http.Request)
}
func (i ipv6OnlyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
i.headersFunc(w, r)
fmt.Fprintf(w, "ipv6 only server\n")
}
func ListenAndServe(addr string, handler http.Handler) error {
srv := &http.Server{Addr: addr, Handler: handler}
ln, err := net.Listen("tcp6", addr) // <--- tcp6 here
if err != nil {
return err
}
// return srv.Serve(tcpKeepAliveListener{ln.(*net.TCPListener)})
return srv.Serve(ln)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment