Skip to content
This repository was archived by the owner on Apr 25, 2024. It is now read-only.

Commit 3a4a3c8

Browse files
committed
Add option to dump request headers to stdout
1 parent 1222154 commit 3a4a3c8

2 files changed

Lines changed: 21 additions & 4 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ $(go env GOPATH)/bin/dirhttps
1515

1616
## Setup
1717

18-
_dirhttps_ needs a certificate and corresponding key to operate. Easiest option is to use the excellent [mkcert](https://github.com/FiloSottile/mkcert) tool which creates locally trusted, self-signed development certificates.
18+
_dirhttps_ needs a certificate and corresponding key to operate. Easiest option is to use the excellent [mkcert](https://github.com/FiloSottile/mkcert) tool which creates locally trusted, self-signed development certificates.
1919

2020
* Install and setup mkcert CA
2121
* Create dirhttps certificate
@@ -39,6 +39,7 @@ Usage:
3939
Flags:
4040
--cache Enable client side caching
4141
-c, --cert string Certificate file (default "/home/maetthu/.config/dirhttps/cert.pem")
42+
-d, --dump Dump client request headers to STDOUT
4243
-h, --help help for dirhttps
4344
-k, --key string Key file (default "/home/maetthu/.config/dirhttps/key.pem")
4445
-l, --listen string Listen address (default ":8443")
@@ -63,4 +64,3 @@ $ dirhttps
6364
$ dirhttps -l :1234
6465
$ dirhttps -l 127.0.0.2:8443
6566
```
66-

cmd/root.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/spf13/cobra"
99
"log"
1010
"net/http"
11+
"net/http/httputil"
1112
"os"
1213
"path/filepath"
1314
)
@@ -85,6 +86,11 @@ var rootCmd = &cobra.Command{
8586
handler = nocache(handler)
8687
}
8788

89+
// Dump request headers?
90+
if d, _ := cmd.Flags().GetBool("dump"); d {
91+
handler = dump(handler)
92+
}
93+
8894
log.Fatal(http.ListenAndServeTLS(addr, certFile, keyFile, handler))
8995
},
9096
}
@@ -109,11 +115,13 @@ func init(){
109115
certFile := filepath.Join(configDir, certFilename)
110116
keyFile := filepath.Join(configDir, keyFilename)
111117

112-
rootCmd.Flags().StringP("listen", "l", ":8443", "Listen address")
113118
rootCmd.Flags().StringP("cert", "c", certFile, "Certificate file")
114119
rootCmd.Flags().StringP("key", "k", keyFile, "Key file")
115-
rootCmd.Flags().Bool("no-cors", false, "Disable CORS handling")
120+
rootCmd.Flags().StringP("listen", "l", ":8443", "Listen address")
121+
116122
rootCmd.Flags().Bool("cache", false, "Enable client side caching")
123+
rootCmd.Flags().BoolP("dump", "d",false, "Dump client request headers to STDOUT")
124+
rootCmd.Flags().Bool("no-cors", false, "Disable CORS handling")
117125
}
118126

119127
func logger(handler http.Handler) http.Handler {
@@ -130,3 +138,12 @@ func nocache(handler http.Handler) http.Handler {
130138
})
131139
}
132140

141+
func dump(handler http.Handler) http.Handler {
142+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
143+
if dump, err := httputil.DumpRequest(r, true); err == nil {
144+
fmt.Printf("---\n%s---\n", dump)
145+
}
146+
147+
handler.ServeHTTP(w, r)
148+
})
149+
}

0 commit comments

Comments
 (0)