add httpcat utility

otelq
William Perron 1 year ago
parent d4d9394a1f
commit e99d5309e7
No known key found for this signature in database
GPG Key ID: D1815C43C9BA3DE1

@ -0,0 +1,5 @@
# HTTP Cat
An HTTP server that returns the HTTP request as text in the response body.
Useful for debugging proxy configurations.

@ -0,0 +1,59 @@
package main
import (
"flag"
"fmt"
"io"
"log"
"net/http"
"strings"
)
var (
ports = flag.String("ports", "", "comma-separated list of ports to listen on")
)
type CatHandler struct{}
var _ http.Handler = &CatHandler{}
func (cs *CatHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
log.Printf("[%s] %s %s", req.Host, req.Method, req.URL.Path)
w.Write([]byte(fmt.Sprintf("HTTP1.1 %s %s\n", req.Method, req.URL.Path)))
for k, v := range req.Header {
w.Write([]byte(fmt.Sprintf("%s: %s\n", k, strings.Join(v, ", "))))
}
w.Write([]byte{'\n'})
if req.Body != nil {
io.Copy(w, req.Body)
}
}
func main() {
flag.Parse()
errch := make(chan error)
done := make(chan struct{})
for _, p := range strings.Split(*ports, ",") {
port := strings.TrimSpace(p)
if port[0] != ':' {
port = ":" + port
}
go func() {
log.Printf("listening on %s\n", port)
if err := http.ListenAndServe(port, new(CatHandler)); err != nil {
log.Println(err)
errch <- err
}
}()
}
go func() {
<-errch
done <- struct{}{}
}()
<-done
}
Loading…
Cancel
Save