mirror of
https://github.com/caddyserver/caddy.git
synced 2024-11-29 12:16:16 +08:00
New {request} placeholder to log entire requests (sans body) (#871)
Add a {request} placeholder to the replacer. Closes #858.
This commit is contained in:
parent
01e05afa0c
commit
6c847d0723
|
@ -3,6 +3,7 @@ package httpserver
|
||||||
import (
|
import (
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/http/httputil"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
@ -11,6 +12,14 @@ import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// requestReplacer is a strings.Replacer which is used to
|
||||||
|
// encode literal \r and \n characters and keep everything
|
||||||
|
// on one line
|
||||||
|
var requestReplacer = strings.NewReplacer(
|
||||||
|
"\r", "\\r",
|
||||||
|
"\n", "\\n",
|
||||||
|
)
|
||||||
|
|
||||||
// Replacer is a type which can replace placeholder
|
// Replacer is a type which can replace placeholder
|
||||||
// substrings in a string with actual values from a
|
// substrings in a string with actual values from a
|
||||||
// http.Request and ResponseRecorder. Always use
|
// http.Request and ResponseRecorder. Always use
|
||||||
|
@ -95,6 +104,14 @@ func NewReplacer(r *http.Request, rr *ResponseRecorder, emptyValue string) Repla
|
||||||
dir, _ := path.Split(r.URL.Path)
|
dir, _ := path.Split(r.URL.Path)
|
||||||
return dir
|
return dir
|
||||||
}(),
|
}(),
|
||||||
|
"{request}": func() string {
|
||||||
|
dump, err := httputil.DumpRequest(r, false)
|
||||||
|
if err != nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
return requestReplacer.Replace(string(dump))
|
||||||
|
}(),
|
||||||
},
|
},
|
||||||
emptyValue: emptyValue,
|
emptyValue: emptyValue,
|
||||||
}
|
}
|
||||||
|
|
|
@ -74,6 +74,9 @@ func TestReplace(t *testing.T) {
|
||||||
if expected, actual := "The Custom header is foobarbaz.", repl.Replace("The Custom header is {>Custom}."); expected != actual {
|
if expected, actual := "The Custom header is foobarbaz.", repl.Replace("The Custom header is {>Custom}."); expected != actual {
|
||||||
t.Errorf("{>Custom} replacement: expected '%s', got '%s'", expected, actual)
|
t.Errorf("{>Custom} replacement: expected '%s', got '%s'", expected, actual)
|
||||||
}
|
}
|
||||||
|
if expected, actual := "The request is POST / HTTP/1.1\\r\\nHost: localhost\\r\\nCustom: foobarbaz\\r\\nShorterval: 1\\r\\n\\r\\n.", repl.Replace("The request is {request}."); expected != actual {
|
||||||
|
t.Errorf("{request} replacement: expected '%s', got '%s'", expected, actual)
|
||||||
|
}
|
||||||
|
|
||||||
// Test header case-insensitivity
|
// Test header case-insensitivity
|
||||||
if expected, actual := "The cUsToM header is foobarbaz...", repl.Replace("The cUsToM header is {>cUsToM}..."); expected != actual {
|
if expected, actual := "The cUsToM header is foobarbaz...", repl.Replace("The cUsToM header is {>cUsToM}..."); expected != actual {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user