Commit fdad616d authored by Matt Holt's avatar Matt Holt Committed by GitHub

Merge pull request #1049 from tw4452852/log_body

capture request body normally
parents 1d3212a5 590862a9
...@@ -40,10 +40,40 @@ type Replacer interface { ...@@ -40,10 +40,40 @@ type Replacer interface {
// they will be used to overwrite other replacements // they will be used to overwrite other replacements
// if there is a name conflict. // if there is a name conflict.
type replacer struct { type replacer struct {
replacements map[string]func() string customReplacements map[string]string
customReplacements map[string]func() string
emptyValue string emptyValue string
responseRecorder *ResponseRecorder responseRecorder *ResponseRecorder
request *http.Request
requestBody *limitWriter
}
type limitWriter struct {
w bytes.Buffer
remain int
}
func newLimitWriter(max int) *limitWriter {
return &limitWriter{
w: bytes.Buffer{},
remain: max,
}
}
func (lw *limitWriter) Write(p []byte) (int, error) {
// skip if we are full
if lw.remain <= 0 {
return len(p), nil
}
if n := len(p); n > lw.remain {
p = p[:lw.remain]
}
n, err := lw.w.Write(p)
lw.remain -= n
return n, err
}
func (lw *limitWriter) String() string {
return lw.w.String()
} }
// NewReplacer makes a new replacer based on r and rr which // NewReplacer makes a new replacer based on r and rr which
...@@ -54,91 +84,24 @@ type replacer struct { ...@@ -54,91 +84,24 @@ type replacer struct {
// emptyValue should be the string that is used in place // emptyValue should be the string that is used in place
// of empty string (can still be empty string). // of empty string (can still be empty string).
func NewReplacer(r *http.Request, rr *ResponseRecorder, emptyValue string) Replacer { func NewReplacer(r *http.Request, rr *ResponseRecorder, emptyValue string) Replacer {
rb := newLimitWriter(MaxLogBodySize)
if r.Body != nil {
r.Body = struct {
io.Reader
io.Closer
}{io.TeeReader(r.Body, rb), io.Closer(r.Body)}
}
rep := &replacer{ rep := &replacer{
request: r,
requestBody: rb,
responseRecorder: rr, responseRecorder: rr,
customReplacements: make(map[string]func() string), customReplacements: make(map[string]string),
replacements: map[string]func() string{ emptyValue: emptyValue,
"{method}": func() string { return r.Method },
"{scheme}": func() string {
if r.TLS != nil {
return "https"
}
return "http"
},
"{hostname}": func() string {
name, err := os.Hostname()
if err != nil {
return ""
}
return name
},
"{host}": func() string { return r.Host },
"{hostonly}": func() string {
host, _, err := net.SplitHostPort(r.Host)
if err != nil {
return r.Host
}
return host
},
"{path}": func() string { return r.URL.Path },
"{path_escaped}": func() string { return url.QueryEscape(r.URL.Path) },
"{query}": func() string { return r.URL.RawQuery },
"{query_escaped}": func() string { return url.QueryEscape(r.URL.RawQuery) },
"{fragment}": func() string { return r.URL.Fragment },
"{proto}": func() string { return r.Proto },
"{remote}": func() string {
host, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
return r.RemoteAddr
}
return host
},
"{port}": func() string {
_, port, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
return ""
}
return port
},
"{uri}": func() string { return r.URL.RequestURI() },
"{uri_escaped}": func() string { return url.QueryEscape(r.URL.RequestURI()) },
"{when}": func() string { return time.Now().Format(timeFormat) },
"{file}": func() string {
_, file := path.Split(r.URL.Path)
return file
},
"{dir}": func() string {
dir, _ := path.Split(r.URL.Path)
return dir
},
"{request}": func() string {
dump, err := httputil.DumpRequest(r, false)
if err != nil {
return ""
}
return requestReplacer.Replace(string(dump))
},
"{request_body}": func() string {
if !canLogRequest(r) {
return ""
}
body, err := readRequestBody(r, maxLogBodySize)
if err != nil {
return ""
}
return requestReplacer.Replace(string(body))
},
},
emptyValue: emptyValue,
} }
// Header placeholders (case-insensitive) // Header placeholders (case-insensitive)
for header, values := range r.Header { for header, values := range r.Header {
values := values rep.customReplacements["{>"+strings.ToLower(header)+"}"] = strings.Join(values, ",")
rep.replacements[headerReplacer+strings.ToLower(header)+"}"] = func() string { return strings.Join(values, ",") }
} }
return rep return rep
...@@ -156,27 +119,6 @@ func canLogRequest(r *http.Request) bool { ...@@ -156,27 +119,6 @@ func canLogRequest(r *http.Request) bool {
return false return false
} }
// readRequestBody reads the request body and sets a
// new io.ReadCloser that has not yet been read.
func readRequestBody(r *http.Request, n int64) ([]byte, error) {
defer r.Body.Close()
body, err := ioutil.ReadAll(io.LimitReader(r.Body, n))
if err != nil {
return nil, err
}
// Read the remaining bytes
remaining, err := ioutil.ReadAll(r.Body)
if err != nil {
return nil, err
}
buf := bytes.NewBuffer(append(body, remaining...))
r.Body = ioutil.NopCloser(buf)
return body, nil
}
// Replace performs a replacement of values on s and returns // Replace performs a replacement of values on s and returns
// the string with the replaced values. // the string with the replaced values.
func (r *replacer) Replace(s string) string { func (r *replacer) Replace(s string) string {
...@@ -185,54 +127,37 @@ func (r *replacer) Replace(s string) string { ...@@ -185,54 +127,37 @@ func (r *replacer) Replace(s string) string {
return s return s
} }
// Make response placeholders now result := ""
if r.responseRecorder != nil { for {
r.replacements["{status}"] = func() string { return strconv.Itoa(r.responseRecorder.status) } idxStart := strings.Index(s, "{")
r.replacements["{size}"] = func() string { return strconv.Itoa(r.responseRecorder.size) } if idxStart == -1 {
r.replacements["{latency}"] = func() string { // no placeholder anymore
dur := time.Since(r.responseRecorder.start) break
return roundDuration(dur).String()
} }
} idxEnd := strings.Index(s[idxStart:], "}")
if idxEnd == -1 {
// Include custom placeholders, overwriting existing ones if necessary // unpaired placeholder
for key, val := range r.customReplacements {
r.replacements[key] = val
}
// Header replacements - these are case-insensitive, so we can't just use strings.Replace()
for strings.Contains(s, headerReplacer) {
idxStart := strings.Index(s, headerReplacer)
endOffset := idxStart + len(headerReplacer)
idxEnd := strings.Index(s[endOffset:], "}")
if idxEnd > -1 {
placeholder := strings.ToLower(s[idxStart : endOffset+idxEnd+1])
replacement := ""
if getReplacement, ok := r.replacements[placeholder]; ok {
replacement = getReplacement()
}
if replacement == "" {
replacement = r.emptyValue
}
s = s[:idxStart] + replacement + s[endOffset+idxEnd+1:]
} else {
break break
} }
} idxEnd += idxStart
// Regular replacements - these are easier because they're case-sensitive // get a replacement
for placeholder, getReplacement := range r.replacements { placeholder := s[idxStart : idxEnd+1]
if !strings.Contains(s, placeholder) { // Header replacements - they are case-insensitive
continue if placeholder[1] == '>' {
} placeholder = strings.ToLower(placeholder)
replacement := getReplacement()
if replacement == "" {
replacement = r.emptyValue
} }
s = strings.Replace(s, placeholder, replacement, -1) replacement := r.getSubstitution(placeholder)
// append prefix + replacement
result += s[:idxStart] + replacement
// strip out scanned parts
s = s[idxEnd+1:]
} }
return s // append unscanned parts
return result + s
} }
func roundDuration(d time.Duration) time.Duration { func roundDuration(d time.Duration) time.Duration {
...@@ -265,16 +190,117 @@ func round(d, r time.Duration) time.Duration { ...@@ -265,16 +190,117 @@ func round(d, r time.Duration) time.Duration {
return d return d
} }
// getSubstitution retrieves value from corresponding key
func (r *replacer) getSubstitution(key string) string {
// search custom replacements first
if value, ok := r.customReplacements[key]; ok {
return value
}
// search default replacements then
switch key {
case "{method}":
return r.request.Method
case "{scheme}":
if r.request.TLS != nil {
return "https"
}
return "http"
case "{hostname}":
name, err := os.Hostname()
if err != nil {
return r.emptyValue
}
return name
case "{host}":
return r.request.Host
case "{hostonly}":
host, _, err := net.SplitHostPort(r.request.Host)
if err != nil {
return r.request.Host
}
return host
case "{path}":
return r.request.URL.Path
case "{path_escaped}":
return url.QueryEscape(r.request.URL.Path)
case "{query}":
return r.request.URL.RawQuery
case "{query_escaped}":
return url.QueryEscape(r.request.URL.RawQuery)
case "{fragment}":
return r.request.URL.Fragment
case "{proto}":
return r.request.Proto
case "{remote}":
host, _, err := net.SplitHostPort(r.request.RemoteAddr)
if err != nil {
return r.request.RemoteAddr
}
return host
case "{port}":
_, port, err := net.SplitHostPort(r.request.RemoteAddr)
if err != nil {
return r.emptyValue
}
return port
case "{uri}":
return r.request.URL.RequestURI()
case "{uri_escaped}":
return url.QueryEscape(r.request.URL.RequestURI())
case "{when}":
return time.Now().Format(timeFormat)
case "{file}":
_, file := path.Split(r.request.URL.Path)
return file
case "{dir}":
dir, _ := path.Split(r.request.URL.Path)
return dir
case "{request}":
dump, err := httputil.DumpRequest(r.request, false)
if err != nil {
return r.emptyValue
}
return requestReplacer.Replace(string(dump))
case "{request_body}":
if !canLogRequest(r.request) {
return r.emptyValue
}
_, err := ioutil.ReadAll(r.request.Body)
if err != nil {
return r.emptyValue
}
return requestReplacer.Replace(r.requestBody.String())
case "{status}":
if r.responseRecorder == nil {
return r.emptyValue
}
return strconv.Itoa(r.responseRecorder.status)
case "{size}":
if r.responseRecorder == nil {
return r.emptyValue
}
return strconv.Itoa(r.responseRecorder.size)
case "{latency}":
if r.responseRecorder == nil {
return r.emptyValue
}
return roundDuration(time.Since(r.responseRecorder.start)).String()
}
return r.emptyValue
}
// Set sets key to value in the r.customReplacements map. // Set sets key to value in the r.customReplacements map.
func (r *replacer) Set(key, value string) { func (r *replacer) Set(key, value string) {
r.customReplacements["{"+key+"}"] = func() string { return value } r.customReplacements["{"+key+"}"] = value
} }
const ( const (
timeFormat = "02/Jan/2006:15:04:05 -0700" timeFormat = "02/Jan/2006:15:04:05 -0700"
headerReplacer = "{>"
headerContentType = "Content-Type" headerContentType = "Content-Type"
contentTypeJSON = "application/json" contentTypeJSON = "application/json"
contentTypeXML = "application/xml" contentTypeXML = "application/xml"
maxLogBodySize = 100 * 1024 // MaxLogBodySize limits the size of logged request's body
MaxLogBodySize = 100 * 1024
) )
package httpserver package httpserver
import ( import (
"bytes"
"io/ioutil"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"os" "os"
...@@ -24,28 +22,12 @@ func TestNewReplacer(t *testing.T) { ...@@ -24,28 +22,12 @@ func TestNewReplacer(t *testing.T) {
switch v := rep.(type) { switch v := rep.(type) {
case *replacer: case *replacer:
if v.replacements["{host}"]() != "localhost" { if v.getSubstitution("{host}") != "localhost" {
t.Error("Expected host to be localhost") t.Error("Expected host to be localhost")
} }
if v.replacements["{method}"]() != "POST" { if v.getSubstitution("{method}") != "POST" {
t.Error("Expected request method to be POST") t.Error("Expected request method to be POST")
} }
// Response placeholders should only be set after call to Replace()
got, want := "", ""
if getReplacement, ok := v.replacements["{status}"]; ok {
got = getReplacement()
}
if want := ""; got != want {
t.Errorf("Expected status to NOT be set before Replace() is called; was: %s", got)
}
rep.Replace("{foobar}")
if getReplacement, ok := v.replacements["{status}"]; ok {
got = getReplacement()
}
if want = "200"; got != want {
t.Errorf("Expected status to be %s, was: %s", want, got)
}
default: default:
t.Fatalf("Expected *replacer underlying Replacer type, got: %#v", rep) t.Fatalf("Expected *replacer underlying Replacer type, got: %#v", rep)
} }
...@@ -94,19 +76,21 @@ func TestReplace(t *testing.T) { ...@@ -94,19 +76,21 @@ func TestReplace(t *testing.T) {
complexCases := []struct { complexCases := []struct {
template string template string
replacements map[string]func() string replacements map[string]string
expect string expect string
}{ }{
{"/a{1}/{2}", {
map[string]func() string{ "/a{1}/{2}",
"{1}": func() string { return "12" }, map[string]string{
"{2}": func() string { return "" }}, "{1}": "12",
"{2}": "",
},
"/a12/"}, "/a12/"},
} }
for _, c := range complexCases { for _, c := range complexCases {
repl := &replacer{ repl := &replacer{
replacements: c.replacements, customReplacements: c.replacements,
} }
if expected, actual := c.expect, repl.Replace(c.template); expected != actual { if expected, actual := c.expect, repl.Replace(c.template); expected != actual {
t.Errorf("for template '%s', expected '%s', got '%s'", c.template, expected, actual) t.Errorf("for template '%s', expected '%s', got '%s'", c.template, expected, actual)
...@@ -163,28 +147,3 @@ func TestRound(t *testing.T) { ...@@ -163,28 +147,3 @@ func TestRound(t *testing.T) {
} }
} }
} }
func TestReadRequestBody(t *testing.T) {
payload := []byte(`{ "foo": "bar" }`)
var readSize int64 = 5
r, err := http.NewRequest("POST", "/", bytes.NewReader(payload))
if err != nil {
t.Error(err)
}
defer r.Body.Close()
logBody, err := readRequestBody(r, readSize)
if err != nil {
t.Error("readRequestBody failed", err)
} else if !bytes.EqualFold(payload[0:readSize], logBody) {
t.Error("Expected log comparison failed")
}
// Ensure the Request body is the same as the original.
reqBody, err := ioutil.ReadAll(r.Body)
if err != nil {
t.Error("Unable to read request body", err)
} else if !bytes.EqualFold(payload, reqBody) {
t.Error("Expected request body comparison failed")
}
}
...@@ -2,6 +2,7 @@ package log ...@@ -2,6 +2,7 @@ package log
import ( import (
"bytes" "bytes"
"io/ioutil"
"log" "log"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
...@@ -60,3 +61,59 @@ func TestLoggedStatus(t *testing.T) { ...@@ -60,3 +61,59 @@ func TestLoggedStatus(t *testing.T) {
t.Errorf("Expected the log entry to contain 'foobar' (custom placeholder), but it didn't: %s", logged) t.Errorf("Expected the log entry to contain 'foobar' (custom placeholder), but it didn't: %s", logged)
} }
} }
func TestLogRequestBody(t *testing.T) {
var got bytes.Buffer
logger := Logger{
Rules: []Rule{{
PathScope: "/",
Format: "{request_body}",
Log: log.New(&got, "", 0),
}},
Next: httpserver.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) {
// drain up body
ioutil.ReadAll(r.Body)
return 0, nil
}),
}
for i, c := range []struct {
body string
expect string
}{
{"", "\n"},
{"{hello} world!", "{hello} world!\n"},
{func() string {
length := httpserver.MaxLogBodySize + 100
b := make([]byte, length)
for i := 0; i < length; i++ {
b[i] = 0xab
}
return string(b)
}(), func() string {
b := make([]byte, httpserver.MaxLogBodySize)
for i := 0; i < httpserver.MaxLogBodySize; i++ {
b[i] = 0xab
}
return string(b) + "\n"
}(),
},
} {
got.Reset()
r, err := http.NewRequest("POST", "/", bytes.NewBufferString(c.body))
if err != nil {
t.Fatal(err)
}
r.Header.Set("Content-Type", "application/json")
status, err := logger.ServeHTTP(httptest.NewRecorder(), r)
if status != 0 {
t.Errorf("case %d: Expected status to be 0, but was %d", i, status)
}
if err != nil {
t.Errorf("case %d: Expected error to be nil, instead got: %v", i, err)
}
if got.String() != c.expect {
t.Errorf("case %d: Expected body %q, but got %q", i, c.expect, got.String())
}
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment