You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
toolkit/cmd/httpcat/main.go

60 lines
1.0 KiB

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
}