File size: 3,304 Bytes
ea775bd | 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 153 154 155 156 157 | package proxy
import (
"bytes"
"io"
"net/http"
)
// captureRequestHeaders serializes headers to canonical wire format for
// request-log payload capture.
func captureRequestHeaders(header http.Header) []byte {
if header == nil {
return nil
}
var buf bytes.Buffer
_ = header.Clone().Write(&buf)
return buf.Bytes()
}
// headerWireLen returns canonical wire-format header bytes length.
func headerWireLen(header http.Header) int64 {
if header == nil || len(header) == 0 {
return 0
}
var buf bytes.Buffer
_ = header.Write(&buf)
return int64(buf.Len())
}
func captureHeadersWithLimit(header http.Header, maxBytes int) ([]byte, int, bool) {
payload := captureRequestHeaders(header)
totalLen := len(payload)
if totalLen == 0 {
return nil, 0, false
}
if maxBytes >= 0 && totalLen > maxBytes {
return payload[:maxBytes], totalLen, true
}
return payload, totalLen, false
}
type payloadCaptureReadCloser struct {
rc io.ReadCloser
maxBytes int
payload bytes.Buffer
totalLen int
}
func newPayloadCaptureReadCloser(rc io.ReadCloser, maxBytes int) *payloadCaptureReadCloser {
return &payloadCaptureReadCloser{
rc: rc,
maxBytes: maxBytes,
}
}
func (c *payloadCaptureReadCloser) Read(p []byte) (int, error) {
n, err := c.rc.Read(p)
if n > 0 {
c.totalLen += n
if c.maxBytes < 0 {
_, _ = c.payload.Write(p[:n])
} else {
remaining := c.maxBytes - c.payload.Len()
if remaining > 0 {
if n <= remaining {
_, _ = c.payload.Write(p[:n])
} else {
_, _ = c.payload.Write(p[:remaining])
}
}
}
}
return n, err
}
func (c *payloadCaptureReadCloser) Close() error {
return c.rc.Close()
}
func (c *payloadCaptureReadCloser) Payload() []byte {
return c.payload.Bytes()
}
func (c *payloadCaptureReadCloser) TotalLen() int {
return c.totalLen
}
func (c *payloadCaptureReadCloser) Truncated() bool {
return c.totalLen > c.payload.Len()
}
// countingReadCloser wraps a body stream and records total read bytes.
type countingReadCloser struct {
rc io.ReadCloser
total int64
}
func newCountingReadCloser(rc io.ReadCloser) *countingReadCloser {
return &countingReadCloser{rc: rc}
}
func (c *countingReadCloser) Read(p []byte) (int, error) {
n, err := c.rc.Read(p)
if n > 0 {
c.total += int64(n)
}
return n, err
}
func (c *countingReadCloser) Close() error {
return c.rc.Close()
}
func (c *countingReadCloser) Total() int64 {
return c.total
}
// countingReadWriteCloser wraps a bidirectional stream and records
// bytes read/written independently.
type countingReadWriteCloser struct {
rwc io.ReadWriteCloser
totalRead int64
totalWrite int64
}
func newCountingReadWriteCloser(rwc io.ReadWriteCloser) *countingReadWriteCloser {
return &countingReadWriteCloser{rwc: rwc}
}
func (c *countingReadWriteCloser) Read(p []byte) (int, error) {
n, err := c.rwc.Read(p)
if n > 0 {
c.totalRead += int64(n)
}
return n, err
}
func (c *countingReadWriteCloser) Write(p []byte) (int, error) {
n, err := c.rwc.Write(p)
if n > 0 {
c.totalWrite += int64(n)
}
return n, err
}
func (c *countingReadWriteCloser) Close() error {
return c.rwc.Close()
}
func (c *countingReadWriteCloser) TotalRead() int64 {
return c.totalRead
}
func (c *countingReadWriteCloser) TotalWrite() int64 {
return c.totalWrite
}
|