github.com/minio/minio@v0.0.0-20240328213742-3f72439b8a27/internal/http/request-recorder.go (about) 1 // Copyright (c) 2015-2022 MinIO, Inc. 2 // 3 // This file is part of MinIO Object Storage stack 4 // 5 // This program is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Affero General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // This program is distributed in the hope that it will be useful 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Affero General Public License for more details. 14 // 15 // You should have received a copy of the GNU Affero General Public License 16 // along with this program. If not, see <http://www.gnu.org/licenses/>. 17 18 package http 19 20 import ( 21 "bytes" 22 "io" 23 ) 24 25 // RequestRecorder - records the 26 // of a given io.Reader 27 type RequestRecorder struct { 28 // Data source to record 29 io.Reader 30 // Response body should be logged 31 LogBody bool 32 33 // internal recording buffer 34 buf bytes.Buffer 35 // total bytes read including header size 36 bytesRead int 37 } 38 39 // Close is a no operation closer 40 func (r *RequestRecorder) Close() error { 41 // no-op 42 return nil 43 } 44 45 // Read reads from the internal reader and counts/save the body in the memory 46 func (r *RequestRecorder) Read(p []byte) (n int, err error) { 47 n, err = r.Reader.Read(p) 48 r.bytesRead += n 49 50 if r.LogBody { 51 r.buf.Write(p[:n]) 52 } 53 if err != nil { 54 return n, err 55 } 56 return n, err 57 } 58 59 // Size returns the body size if the currently read object 60 func (r *RequestRecorder) Size() int { 61 return r.bytesRead 62 } 63 64 // Data returns the bytes that were recorded. 65 func (r *RequestRecorder) Data() []byte { 66 // If body logging is enabled then we return the actual body 67 if r.LogBody { 68 return r.buf.Bytes() 69 } 70 // ... otherwise we return <BLOB> placeholder 71 return blobBody 72 }