| 1 | // Copyright 2015 Matthew Holt and The Caddy Authors |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package caddyhttpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/ |
| 16 | |
| 17 | import ( |
| 18 | "bufioscip-go gomod github.com/golang/go/src go1.25.1 bufio/" |
| 19 | "bytesscip-go gomod github.com/golang/go/src go1.25.1 bytes/" |
| 20 | "fmtscip-go gomod github.com/golang/go/src go1.25.1 fmt/" |
| 21 | "ioscip-go gomod github.com/golang/go/src go1.25.1 io/" |
| 22 | "netscip-go gomod github.com/golang/go/src go1.25.1 net/" |
| 23 | "net/httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/" |
| 24 | ) |
| 25 | |
| 26 | // ResponseWriterWrapper wraps an underlying ResponseWriter and |
| 27 | // promotes its Pusher method as well. To use this type, embed |
| 28 | // a pointer to it within your own struct type that implements |
| 29 | // the http.ResponseWriter interface, then call methods on the |
| 30 | // embedded value. |
| 31 | type ResponseWriterWrapperscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/ResponseWriterWrapper# struct { |
| 32 | httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.ResponseWriterscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/ResponseWriterWrapper#ResponseWriter. |
| 33 | } |
| 34 | |
| 35 | // Push implements http.Pusher. It simply calls the underlying |
| 36 | // ResponseWriter's Push method if there is one, or returns |
| 37 | // ErrNotImplemented otherwise. |
| 38 | func (rww *ResponseWriterWrapperscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/ResponseWriterWrapper#) Pushscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/ResponseWriterWrapper#Push().(target string, opts *httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.PushOptionsscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/PushOptions#) error { |
| 39 | if pusher, ok := rww.ResponseWriterscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/ResponseWriterWrapper#ResponseWriter..(httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.Pusherscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Pusher#); ok { |
| 40 | return pusher.Pushscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Pusher#Push().(target, opts) |
| 41 | } |
| 42 | return ErrNotImplementedscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/ErrNotImplemented. |
| 43 | } |
| 44 | |
| 45 | // ReadFrom implements io.ReaderFrom. It retries to use io.ReaderFrom if available, |
| 46 | // then fallback to io.Copy. |
| 47 | // see: https://github.com/caddyserver/caddy/issues/6546 |
| 48 | func (rww *ResponseWriterWrapperscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/ResponseWriterWrapper#) ReadFromscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/ResponseWriterWrapper#ReadFrom().(r ioscip-go gomod github.com/golang/go/src go1.25.1 io/.Readerscip-go gomod github.com/golang/go/src go1.25.1 io/Reader#) (n int64, err error) { |
| 49 | if rf, ok := rww.ResponseWriterscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/ResponseWriterWrapper#ResponseWriter..(ioscip-go gomod github.com/golang/go/src go1.25.1 io/.ReaderFromscip-go gomod github.com/golang/go/src go1.25.1 io/ReaderFrom#); ok { |
| 50 | return rf.ReadFromscip-go gomod github.com/golang/go/src go1.25.1 io/ReaderFrom#ReadFrom().(r) |
| 51 | } |
| 52 | return ioscip-go gomod github.com/golang/go/src go1.25.1 io/.Copyscip-go gomod github.com/golang/go/src go1.25.1 io/Copy().(rww.ResponseWriterscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/ResponseWriterWrapper#ResponseWriter., r) |
| 53 | } |
| 54 | |
| 55 | // Unwrap returns the underlying ResponseWriter, necessary for |
| 56 | // http.ResponseController to work correctly. |
| 57 | func (rww *ResponseWriterWrapperscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/ResponseWriterWrapper#) Unwrapscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/ResponseWriterWrapper#Unwrap().() httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.ResponseWriterscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/ResponseWriter# { |
| 58 | return rww.ResponseWriterscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/ResponseWriterWrapper#ResponseWriter. |
| 59 | } |
| 60 | |
| 61 | // ErrNotImplemented is returned when an underlying |
| 62 | // ResponseWriter does not implement the required method. |
| 63 | var ErrNotImplementedscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/ErrNotImplemented. = fmtscip-go gomod github.com/golang/go/src go1.25.1 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.1 fmt/Errorf().("method not implemented") |
| 64 | |
| 65 | type responseRecorderscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/responseRecorder# struct { |
| 66 | *ResponseWriterWrapperscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/responseRecorder#ResponseWriterWrapper. |
| 67 | statusCodescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/responseRecorder#statusCode. int |
| 68 | bufscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/responseRecorder#buf. *bytesscip-go gomod github.com/golang/go/src go1.25.1 bytes/.Bufferscip-go gomod github.com/golang/go/src go1.25.1 bytes/Buffer# |
| 69 | shouldBufferscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/responseRecorder#shouldBuffer. ShouldBufferFuncscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/ShouldBufferFunc# |
| 70 | sizescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/responseRecorder#size. int |
| 71 | wroteHeaderscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/responseRecorder#wroteHeader. bool |
| 72 | streamscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/responseRecorder#stream. bool |
| 73 | |
| 74 | readSizescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/responseRecorder#readSize. *int |
| 75 | } |
| 76 | |
| 77 | // NewResponseRecorder returns a new ResponseRecorder that can be |
| 78 | // used instead of a standard http.ResponseWriter. The recorder is |
| 79 | // useful for middlewares which need to buffer a response and |
| 80 | // potentially process its entire body before actually writing the |
| 81 | // response to the underlying writer. Of course, buffering the entire |
| 82 | // body has a memory overhead, but sometimes there is no way to avoid |
| 83 | // buffering the whole response, hence the existence of this type. |
| 84 | // Still, if at all practical, handlers should strive to stream |
| 85 | // responses by wrapping Write and WriteHeader methods instead of |
| 86 | // buffering whole response bodies. |
| 87 | // |
| 88 | // Buffering is actually optional. The shouldBuffer function will |
| 89 | // be called just before the headers are written. If it returns |
| 90 | // true, the headers and body will be buffered by this recorder |
| 91 | // and not written to the underlying writer; if false, the headers |
| 92 | // will be written immediately and the body will be streamed out |
| 93 | // directly to the underlying writer. If shouldBuffer is nil, |
| 94 | // the response will never be buffered and will always be streamed |
| 95 | // directly to the writer. |
| 96 | // |
| 97 | // You can know if shouldBuffer returned true by calling Buffered(). |
| 98 | // |
| 99 | // The provided buffer buf should be obtained from a pool for best |
| 100 | // performance (see the sync.Pool type). |
| 101 | // |
| 102 | // Proper usage of a recorder looks like this: |
| 103 | // |
| 104 | // rec := caddyhttp.NewResponseRecorder(w, buf, shouldBuffer) |
| 105 | // err := next.ServeHTTP(rec, req) |
| 106 | // if err != nil { |
| 107 | // return err |
| 108 | // } |
| 109 | // if !rec.Buffered() { |
| 110 | // return nil |
| 111 | // } |
| 112 | // // process the buffered response here |
| 113 | // |
| 114 | // The header map is not buffered; i.e. the ResponseRecorder's Header() |
| 115 | // method returns the same header map of the underlying ResponseWriter. |
| 116 | // This is a crucial design decision to allow HTTP trailers to be |
| 117 | // flushed properly (https://github.com/caddyserver/caddy/issues/3236). |
| 118 | // |
| 119 | // Once you are ready to write the response, there are two ways you can |
| 120 | // do it. The easier way is to have the recorder do it: |
| 121 | // |
| 122 | // rec.WriteResponse() |
| 123 | // |
| 124 | // This writes the recorded response headers as well as the buffered body. |
| 125 | // Or, you may wish to do it yourself, especially if you manipulated the |
| 126 | // buffered body. First you will need to write the headers with the |
| 127 | // recorded status code, then write the body (this example writes the |
| 128 | // recorder's body buffer, but you might have your own body to write |
| 129 | // instead): |
| 130 | // |
| 131 | // w.WriteHeader(rec.Status()) |
| 132 | // io.Copy(w, rec.Buffer()) |
| 133 | // |
| 134 | // As a special case, 1xx responses are not buffered nor recorded |
| 135 | // because they are not the final response; they are passed through |
| 136 | // directly to the underlying ResponseWriter. |
| 137 | func NewResponseRecorderscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/NewResponseRecorder().(w httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.ResponseWriterscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/ResponseWriter#, buf *bytesscip-go gomod github.com/golang/go/src go1.25.1 bytes/.Bufferscip-go gomod github.com/golang/go/src go1.25.1 bytes/Buffer#, shouldBuffer ShouldBufferFuncscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/ShouldBufferFunc#) ResponseRecorderscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/ResponseRecorder# { |
| 138 | return &responseRecorderscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/responseRecorder#{ |
| 139 | ResponseWriterWrapperscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/responseRecorder#ResponseWriterWrapper.: &ResponseWriterWrapperscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/ResponseWriterWrapper#{ResponseWriterscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/ResponseWriterWrapper#ResponseWriter.: w}, |
| 140 | bufscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/responseRecorder#buf.: buf, |
| 141 | shouldBufferscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/responseRecorder#shouldBuffer.: shouldBuffer, |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | // WriteHeader writes the headers with statusCode to the wrapped |
| 146 | // ResponseWriter unless the response is to be buffered instead. |
| 147 | // 1xx responses are never buffered. |
| 148 | func (rr *responseRecorderscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/responseRecorder#) WriteHeaderscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/responseRecorder#WriteHeader().(statusCode int) { |
| 149 | if rr.wroteHeaderscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/responseRecorder#wroteHeader. { |
| 150 | return |
| 151 | } |
| 152 | |
| 153 | // save statusCode always, in case HTTP middleware upgrades websocket |
| 154 | // connections by manually setting headers and writing status 101 |
| 155 | rr.statusCodescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/responseRecorder#statusCode. = statusCode |
| 156 | |
| 157 | // decide whether we should buffer the response |
| 158 | if rr.shouldBufferscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/responseRecorder#shouldBuffer. == nil { |
| 159 | rr.streamscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/responseRecorder#stream. = true |
| 160 | } else { |
| 161 | rr.streamscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/responseRecorder#stream. = !rr.shouldBufferscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/responseRecorder#shouldBuffer.(rr.statusCodescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/responseRecorder#statusCode., rr.ResponseWriterWrapperscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/responseRecorder#ResponseWriterWrapper..Headerscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/ResponseWriter#Header().()) |
| 162 | } |
| 163 | |
| 164 | // 1xx responses aren't final; just informational |
| 165 | if statusCode < 100 || statusCode > 199 { |
| 166 | rr.wroteHeaderscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/responseRecorder#wroteHeader. = true |
| 167 | } |
| 168 | |
| 169 | // if informational or not buffered, immediately write header |
| 170 | if rr.streamscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/responseRecorder#stream. || (100 <= statusCode && statusCode <= 199) { |
| 171 | rr.ResponseWriterWrapperscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/responseRecorder#ResponseWriterWrapper..WriteHeaderscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/ResponseWriter#WriteHeader().(statusCode) |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | func (rr *responseRecorderscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/responseRecorder#) Writescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/responseRecorder#Write().(data []byte) (int, error) { |
| 176 | rr.WriteHeaderscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/responseRecorder#WriteHeader().(httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.StatusOKscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/StatusOK.) |
| 177 | var n int |
| 178 | var err error |
| 179 | if rr.streamscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/responseRecorder#stream. { |
| 180 | n, err = rr.ResponseWriterWrapperscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/responseRecorder#ResponseWriterWrapper..Writescip-go gomod github.com/golang/go/src go1.25.1 `net/http`/ResponseWriter#Write().(data) |
| 181 | } else { |
| 182 | n, err = rr.bufscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/responseRecorder#buf..Writescip-go gomod github.com/golang/go/src go1.25.1 bytes/Buffer#Write().(data) |
| 183 | } |
| 184 | |
| 185 | rr.sizescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/responseRecorder#size. += n |
| 186 | return n, err |
| 187 | } |
| 188 | |
| 189 | func (rr *responseRecorderscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/responseRecorder#) ReadFromscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/responseRecorder#ReadFrom().(r ioscip-go gomod github.com/golang/go/src go1.25.1 io/.Readerscip-go gomod github.com/golang/go/src go1.25.1 io/Reader#) (int64, error) { |
| 190 | rr.WriteHeaderscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/responseRecorder#WriteHeader().(httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.StatusOKscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/StatusOK.) |
| 191 | var n int64 |
| 192 | var err error |
| 193 | if rr.streamscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/responseRecorder#stream. { |
| 194 | n, err = rr.ResponseWriterWrapperscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/responseRecorder#ResponseWriterWrapper..ReadFromscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/ResponseWriterWrapper#ReadFrom().(r) |
| 195 | } else { |
| 196 | n, err = rr.bufscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/responseRecorder#buf..ReadFromscip-go gomod github.com/golang/go/src go1.25.1 bytes/Buffer#ReadFrom().(r) |
| 197 | } |
| 198 | |
| 199 | rr.sizescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/responseRecorder#size. += int(n) |
| 200 | return n, err |
| 201 | } |
| 202 | |
| 203 | // Status returns the status code that was written, if any. |
| 204 | func (rr *responseRecorderscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/responseRecorder#) Statusscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/responseRecorder#Status().() int { |
| 205 | return rr.statusCodescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/responseRecorder#statusCode. |
| 206 | } |
| 207 | |
| 208 | // Size returns the number of bytes written, |
| 209 | // not including the response headers. |
| 210 | func (rr *responseRecorderscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/responseRecorder#) Sizescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/responseRecorder#Size().() int { |
| 211 | return rr.sizescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/responseRecorder#size. |
| 212 | } |
| 213 | |
| 214 | // Buffer returns the body buffer that rr was created with. |
| 215 | // You should still have your original pointer, though. |
| 216 | func (rr *responseRecorderscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/responseRecorder#) Bufferscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/responseRecorder#Buffer().() *bytesscip-go gomod github.com/golang/go/src go1.25.1 bytes/.Bufferscip-go gomod github.com/golang/go/src go1.25.1 bytes/Buffer# { |
| 217 | return rr.bufscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/responseRecorder#buf. |
| 218 | } |
| 219 | |
| 220 | // Buffered returns whether rr has decided to buffer the response. |
| 221 | func (rr *responseRecorderscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/responseRecorder#) Bufferedscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/responseRecorder#Buffered().() bool { |
| 222 | return !rr.streamscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/responseRecorder#stream. |
| 223 | } |
| 224 | |
| 225 | func (rr *responseRecorderscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/responseRecorder#) WriteResponsescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/responseRecorder#WriteResponse().() error { |
| 226 | if rr.statusCodescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/responseRecorder#statusCode. == 0 { |
| 227 | // could happen if no handlers actually wrote anything, |
| 228 | // and this prevents a panic; status must be > 0 |
| 229 | rr.WriteHeaderscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/responseRecorder#WriteHeader().(httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.StatusOKscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/StatusOK.) |
| 230 | } |
| 231 | if rr.streamscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/responseRecorder#stream. { |
| 232 | return nil |
| 233 | } |
| 234 | rr.ResponseWriterWrapperscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/responseRecorder#ResponseWriterWrapper..WriteHeaderscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/ResponseWriter#WriteHeader().(rr.statusCodescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/responseRecorder#statusCode.) |
| 235 | _, err := ioscip-go gomod github.com/golang/go/src go1.25.1 io/.Copyscip-go gomod github.com/golang/go/src go1.25.1 io/Copy().(rr.ResponseWriterWrapperscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/responseRecorder#ResponseWriterWrapper., rr.bufscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/responseRecorder#buf.) |
| 236 | return err |
| 237 | } |
| 238 | |
| 239 | // FlushError will suppress actual flushing if the response is buffered. See: |
| 240 | // https://github.com/caddyserver/caddy/issues/6144 |
| 241 | func (rr *responseRecorderscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/responseRecorder#) FlushErrorscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/responseRecorder#FlushError().() error { |
| 242 | if rr.streamscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/responseRecorder#stream. { |
| 243 | //nolint:bodyclose |
| 244 | return httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.NewResponseControllerscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/NewResponseController().(rr.ResponseWriterWrapperscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/responseRecorder#ResponseWriterWrapper.).Flushscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/ResponseController#Flush().() |
| 245 | } |
| 246 | return nil |
| 247 | } |
| 248 | |
| 249 | // Private interface so it can only be used in this package |
| 250 | // #TODO: maybe export it later |
| 251 | func (rr *responseRecorderscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/responseRecorder#) setReadSizescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/responseRecorder#setReadSize().(size *int) { |
| 252 | rr.readSizescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/responseRecorder#readSize. = size |
| 253 | } |
| 254 | |
| 255 | func (rr *responseRecorderscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/responseRecorder#) Hijackscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/responseRecorder#Hijack().() (netscip-go gomod github.com/golang/go/src go1.25.1 net/.Connscip-go gomod github.com/golang/go/src go1.25.1 net/Conn#, *bufioscip-go gomod github.com/golang/go/src go1.25.1 bufio/.ReadWriterscip-go gomod github.com/golang/go/src go1.25.1 bufio/ReadWriter#, error) { |
| 256 | //nolint:bodyclose |
| 257 | conn, brw, err := httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.NewResponseControllerscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/NewResponseController().(rr.ResponseWriterWrapperscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/responseRecorder#ResponseWriterWrapper.).Hijackscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/ResponseController#Hijack().() |
| 258 | if err != nil { |
| 259 | return nil, nil, err |
| 260 | } |
| 261 | // Per http documentation, returned bufio.Writer is empty, but bufio.Read maybe not |
| 262 | conn = &hijackedConnscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/hijackedConn#{conn, rr} |
| 263 | brw.Writerscip-go gomod github.com/golang/go/src go1.25.1 bufio/ReadWriter#Writer..Resetscip-go gomod github.com/golang/go/src go1.25.1 bufio/Writer#Reset().(conn) |
| 264 | |
| 265 | buffered := brw.Readerscip-go gomod github.com/golang/go/src go1.25.1 bufio/ReadWriter#Reader..Bufferedscip-go gomod github.com/golang/go/src go1.25.1 bufio/Reader#Buffered().() |
| 266 | if buffered != 0 { |
| 267 | conn.(*hijackedConnscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/hijackedConn#).updateReadSizescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/hijackedConn#updateReadSize().(buffered) |
| 268 | data, _ := brw.Peekscip-go gomod github.com/golang/go/src go1.25.1 bufio/Reader#Peek().(buffered) |
| 269 | brw.Readerscip-go gomod github.com/golang/go/src go1.25.1 bufio/ReadWriter#Reader..Resetscip-go gomod github.com/golang/go/src go1.25.1 bufio/Reader#Reset().(ioscip-go gomod github.com/golang/go/src go1.25.1 io/.MultiReaderscip-go gomod github.com/golang/go/src go1.25.1 io/MultiReader().(bytesscip-go gomod github.com/golang/go/src go1.25.1 bytes/.NewReaderscip-go gomod github.com/golang/go/src go1.25.1 bytes/NewReader().(data), conn)) |
| 270 | // peek to make buffered data appear, as Reset will make it 0 |
| 271 | _, _ = brw.Peekscip-go gomod github.com/golang/go/src go1.25.1 bufio/Reader#Peek().(buffered) |
| 272 | } else { |
| 273 | brw.Readerscip-go gomod github.com/golang/go/src go1.25.1 bufio/ReadWriter#Reader..Resetscip-go gomod github.com/golang/go/src go1.25.1 bufio/Reader#Reset().(conn) |
| 274 | } |
| 275 | return conn, brw, nil |
| 276 | } |
| 277 | |
| 278 | // used to track the size of hijacked response writers |
| 279 | type hijackedConnscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/hijackedConn# struct { |
| 280 | netscip-go gomod github.com/golang/go/src go1.25.1 net/.Connscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/hijackedConn#Conn. |
| 281 | rrscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/hijackedConn#rr. *responseRecorderscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/responseRecorder# |
| 282 | } |
| 283 | |
| 284 | func (hc *hijackedConnscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/hijackedConn#) updateReadSizescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/hijackedConn#updateReadSize().(n int) { |
| 285 | if hc.rrscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/hijackedConn#rr..readSizescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/responseRecorder#readSize. != nil { |
| 286 | *hc.rrscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/hijackedConn#rr..readSizescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/responseRecorder#readSize. += n |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | func (hc *hijackedConnscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/hijackedConn#) Readscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/hijackedConn#Read().(p []byte) (int, error) { |
| 291 | n, err := hc.Connscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/hijackedConn#Conn..Readscip-go gomod github.com/golang/go/src go1.25.1 net/Conn#Read().(p) |
| 292 | hc.updateReadSizescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/hijackedConn#updateReadSize().(n) |
| 293 | return n, err |
| 294 | } |
| 295 | |
| 296 | func (hc *hijackedConnscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/hijackedConn#) WriteToscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/hijackedConn#WriteTo().(w ioscip-go gomod github.com/golang/go/src go1.25.1 io/.Writerscip-go gomod github.com/golang/go/src go1.25.1 io/Writer#) (int64, error) { |
| 297 | n, err := ioscip-go gomod github.com/golang/go/src go1.25.1 io/.Copyscip-go gomod github.com/golang/go/src go1.25.1 io/Copy().(w, hc.Connscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/hijackedConn#Conn.) |
| 298 | hc.updateReadSizescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/hijackedConn#updateReadSize().(int(n)) |
| 299 | return n, err |
| 300 | } |
| 301 | |
| 302 | func (hc *hijackedConnscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/hijackedConn#) Writescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/hijackedConn#Write().(p []byte) (int, error) { |
| 303 | n, err := hc.Connscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/hijackedConn#Conn..Writescip-go gomod github.com/golang/go/src go1.25.1 net/Conn#Write().(p) |
| 304 | hc.rrscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/hijackedConn#rr..sizescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/responseRecorder#size. += n |
| 305 | return n, err |
| 306 | } |
| 307 | |
| 308 | func (hc *hijackedConnscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/hijackedConn#) ReadFromscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/hijackedConn#ReadFrom().(r ioscip-go gomod github.com/golang/go/src go1.25.1 io/.Readerscip-go gomod github.com/golang/go/src go1.25.1 io/Reader#) (int64, error) { |
| 309 | n, err := ioscip-go gomod github.com/golang/go/src go1.25.1 io/.Copyscip-go gomod github.com/golang/go/src go1.25.1 io/Copy().(hc.Connscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/hijackedConn#Conn., r) |
| 310 | hc.rrscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/hijackedConn#rr..sizescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/responseRecorder#size. += int(n) |
| 311 | return n, err |
| 312 | } |
| 313 | |
| 314 | // ResponseRecorder is a http.ResponseWriter that records |
| 315 | // responses instead of writing them to the client. See |
| 316 | // docs for NewResponseRecorder for proper usage. |
| 317 | type ResponseRecorderscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/ResponseRecorder# interface { |
| 318 | httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.ResponseWriterscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/ResponseWriter# |
| 319 | Statusscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/ResponseRecorder#Status.() int |
| 320 | Bufferscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/ResponseRecorder#Buffer.() *bytesscip-go gomod github.com/golang/go/src go1.25.1 bytes/.Bufferscip-go gomod github.com/golang/go/src go1.25.1 bytes/Buffer# |
| 321 | Bufferedscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/ResponseRecorder#Buffered.() bool |
| 322 | Sizescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/ResponseRecorder#Size.() int |
| 323 | WriteResponsescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/ResponseRecorder#WriteResponse.() error |
| 324 | } |
| 325 | |
| 326 | // ShouldBufferFunc is a function that returns true if the |
| 327 | // response should be buffered, given the pending HTTP status |
| 328 | // code and response headers. |
| 329 | type ShouldBufferFuncscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/ShouldBufferFunc# func(status int, header httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.Headerscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Header#) bool |
| 330 | |
| 331 | // Interface guards |
| 332 | var ( |
| 333 | _ httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.ResponseWriterscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/ResponseWriter# = (*ResponseWriterWrapperscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/ResponseWriterWrapper#)(nil) |
| 334 | _ ResponseRecorderscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/ResponseRecorder# = (*responseRecorderscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/responseRecorder#)(nil) |
| 335 | |
| 336 | // Implementing ReaderFrom can be such a significant |
| 337 | // optimization that it should probably be required! |
| 338 | // see PR #5022 (25%-50% speedup) |
| 339 | _ ioscip-go gomod github.com/golang/go/src go1.25.1 io/.ReaderFromscip-go gomod github.com/golang/go/src go1.25.1 io/ReaderFrom# = (*ResponseWriterWrapperscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/ResponseWriterWrapper#)(nil) |
| 340 | _ ioscip-go gomod github.com/golang/go/src go1.25.1 io/.ReaderFromscip-go gomod github.com/golang/go/src go1.25.1 io/ReaderFrom# = (*responseRecorderscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/responseRecorder#)(nil) |
| 341 | _ ioscip-go gomod github.com/golang/go/src go1.25.1 io/.ReaderFromscip-go gomod github.com/golang/go/src go1.25.1 io/ReaderFrom# = (*hijackedConnscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/hijackedConn#)(nil) |
| 342 | |
| 343 | _ ioscip-go gomod github.com/golang/go/src go1.25.1 io/.WriterToscip-go gomod github.com/golang/go/src go1.25.1 io/WriterTo# = (*hijackedConnscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/hijackedConn#)(nil) |
| 344 | ) |