forked from samber/slog-http
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdump.go
More file actions
152 lines (129 loc) · 2.96 KB
/
dump.go
File metadata and controls
152 lines (129 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package sloghttp
import (
"bufio"
"bytes"
"errors"
"io"
"net"
"net/http"
)
var _ WrapResponseWriter = (*bodyWriter)(nil)
type WrapResponseWriter interface {
http.ResponseWriter
http.Flusher
http.Hijacker
Status() int
BytesWritten() int
Body() []byte
}
var _ http.ResponseWriter = (*bodyWriter)(nil)
var _ http.Flusher = (*bodyWriter)(nil)
var _ http.Hijacker = (*bodyWriter)(nil)
var _ io.ReaderFrom = (*bodyWriter)(nil)
type bodyWriter struct {
http.ResponseWriter
body *bytes.Buffer
maxSize int
bytes int
status int
}
// implements http.ResponseWriter
func (w *bodyWriter) Write(b []byte) (int, error) {
length := len(b)
if w.body != nil {
if w.body.Len()+length > w.maxSize {
w.body.Truncate(min(w.maxSize, length, w.body.Len()))
w.body.Write(b[:min(w.maxSize-w.body.Len(), length)])
} else {
w.body.Write(b)
}
}
w.bytes += length //nolint:staticcheck
return w.ResponseWriter.Write(b)
}
// implements http.ResponseWriter
func (r *bodyWriter) WriteHeader(code int) {
r.status = code
r.ResponseWriter.WriteHeader(code)
}
// implements http.Flusher
func (w *bodyWriter) Flush() {
if f, ok := w.ResponseWriter.(http.Flusher); ok {
f.Flush()
}
}
// implements http.Hijacker
func (w *bodyWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
if hi, ok := w.ResponseWriter.(http.Hijacker); ok {
return hi.Hijack()
}
return nil, nil, errors.New("Hijack not supported")
}
// implements io.ReaderFrom
func (w *bodyWriter) ReadFrom(r io.Reader) (int64, error) {
if w.body == nil {
if rf, ok := w.ResponseWriter.(io.ReaderFrom); ok {
n, err := rf.ReadFrom(r)
w.bytes += int(n)
return n, err
}
}
return io.Copy(w, r)
}
// Unwrap implements the ability to use underlying http.ResponseController
func (w *bodyWriter) Unwrap() http.ResponseWriter {
return w.ResponseWriter
}
func (w *bodyWriter) Status() int {
return w.status
}
func (w *bodyWriter) BytesWritten() int {
return w.bytes
}
func (w *bodyWriter) Body() []byte {
return w.body.Bytes()
}
func newBodyWriter(writer http.ResponseWriter, maxSize int, recordBody bool) *bodyWriter {
var body *bytes.Buffer
if recordBody {
body = bytes.NewBufferString("")
}
return &bodyWriter{
ResponseWriter: writer,
body: body,
maxSize: maxSize,
bytes: 0,
status: http.StatusOK,
}
}
type bodyReader struct {
io.ReadCloser
body *bytes.Buffer
maxSize int
bytes int
}
// implements io.Reader
func (r *bodyReader) Read(b []byte) (int, error) {
n, err := r.ReadCloser.Read(b)
if r.body != nil && r.body.Len() < r.maxSize {
if r.body.Len()+n > r.maxSize {
r.body.Write(b[:min(r.maxSize-r.body.Len(), n)])
} else {
r.body.Write(b[:n])
}
}
r.bytes += n
return n, err
}
func newBodyReader(reader io.ReadCloser, maxSize int, recordBody bool) *bodyReader {
var body *bytes.Buffer
if recordBody {
body = new(bytes.Buffer)
}
return &bodyReader{
ReadCloser: reader,
body: body,
maxSize: maxSize,
bytes: 0,
}
}