(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler)
| 61 | } |
| 62 | |
| 63 | func (h LogAppend) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error { |
| 64 | // Determine if we need to add the log field early. |
| 65 | // We do if the Early flag is set, or for convenience, |
| 66 | // if the value is a special placeholder for the request body. |
| 67 | needsEarly := h.Early || h.Value == placeholderRequestBody || h.Value == placeholderRequestBodyBase64 |
| 68 | |
| 69 | // Check if we need to buffer the response for special placeholders |
| 70 | needsResponseBody := h.Value == placeholderResponseBody || h.Value == placeholderResponseBodyBase64 |
| 71 | |
| 72 | if needsEarly && !needsResponseBody { |
| 73 | // Add the log field before calling the next handler |
| 74 | // (but not if we need the response body, which isn't available yet) |
| 75 | h.addLogField(r, nil) |
| 76 | } |
| 77 | |
| 78 | var rec caddyhttp.ResponseRecorder |
| 79 | var buf *bytes.Buffer |
| 80 | |
| 81 | if needsResponseBody { |
| 82 | // Wrap the response writer with a recorder to capture the response body |
| 83 | buf = new(bytes.Buffer) |
| 84 | rec = caddyhttp.NewResponseRecorder(w, buf, func(status int, header http.Header) bool { |
| 85 | // Always buffer the response when we need to log the body |
| 86 | return true |
| 87 | }) |
| 88 | w = rec |
| 89 | } |
| 90 | |
| 91 | // Run the next handler in the chain. |
| 92 | // If an error occurs, we still want to add |
| 93 | // any extra log fields that we can, so we |
| 94 | // hold onto the error and return it later. |
| 95 | handlerErr := next.ServeHTTP(w, r) |
| 96 | |
| 97 | if needsResponseBody { |
| 98 | // Write the buffered response to the client |
| 99 | if rec.Buffered() { |
| 100 | h.addLogField(r, buf) |
| 101 | err := rec.WriteResponse() |
| 102 | if err != nil { |
| 103 | return err |
| 104 | } |
| 105 | } |
| 106 | return handlerErr |
| 107 | } |
| 108 | |
| 109 | if !h.Early { |
| 110 | // Add the log field after the handler completes |
| 111 | h.addLogField(r, buf) |
| 112 | } |
| 113 | |
| 114 | return handlerErr |
| 115 | } |
| 116 | |
| 117 | // addLogField adds the log field to the request's extra log fields. |
| 118 | // If buf is not nil, it contains the buffered response body for special |
nothing calls this directly
no test coverage detected