mirror of
https://github.com/caddyserver/caddy.git
synced 2025-01-06 22:40:31 -05:00
caddyhttp: Log request body bytes read (#5461)
This commit is contained in:
parent
1aef807c71
commit
2b3046de36
1 changed files with 32 additions and 0 deletions
|
@ -19,6 +19,7 @@ import (
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
|
@ -259,6 +260,14 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
wrec := NewResponseRecorder(w, nil, nil)
|
wrec := NewResponseRecorder(w, nil, nil)
|
||||||
w = wrec
|
w = wrec
|
||||||
|
|
||||||
|
// wrap the request body in a LengthReader
|
||||||
|
// so we can track the number of bytes read from it
|
||||||
|
var bodyReader *lengthReader
|
||||||
|
if r.Body != nil {
|
||||||
|
bodyReader = &lengthReader{Source: r.Body}
|
||||||
|
r.Body = bodyReader
|
||||||
|
}
|
||||||
|
|
||||||
// capture the original version of the request
|
// capture the original version of the request
|
||||||
accLog := s.accessLogger.With(loggableReq)
|
accLog := s.accessLogger.With(loggableReq)
|
||||||
|
|
||||||
|
@ -285,7 +294,13 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
userID, _ := repl.GetString("http.auth.user.id")
|
userID, _ := repl.GetString("http.auth.user.id")
|
||||||
|
|
||||||
|
reqBodyLength := 0
|
||||||
|
if bodyReader != nil {
|
||||||
|
reqBodyLength = bodyReader.Length
|
||||||
|
}
|
||||||
|
|
||||||
log("handled request",
|
log("handled request",
|
||||||
|
zap.Int("bytes_read", reqBodyLength),
|
||||||
zap.String("user_id", userID),
|
zap.String("user_id", userID),
|
||||||
zap.Duration("duration", duration),
|
zap.Duration("duration", duration),
|
||||||
zap.Int("size", wrec.Size()),
|
zap.Int("size", wrec.Size()),
|
||||||
|
@ -826,6 +841,23 @@ func cloneURL(from, to *url.URL) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// lengthReader is an io.ReadCloser that keeps track of the
|
||||||
|
// number of bytes read from the request body.
|
||||||
|
type lengthReader struct {
|
||||||
|
Source io.ReadCloser
|
||||||
|
Length int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *lengthReader) Read(b []byte) (int, error) {
|
||||||
|
n, err := r.Source.Read(b)
|
||||||
|
r.Length += n
|
||||||
|
return n, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *lengthReader) Close() error {
|
||||||
|
return r.Source.Close()
|
||||||
|
}
|
||||||
|
|
||||||
// Context keys for HTTP request context values.
|
// Context keys for HTTP request context values.
|
||||||
const (
|
const (
|
||||||
// For referencing the server instance
|
// For referencing the server instance
|
||||||
|
|
Loading…
Reference in a new issue