Debian Codegraph @

modules/caddyhttp/logging/logappend.go

raw symbols SCIP
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 loggingscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/logging`/
16
17 import (
18 "bytesscip-go gomod github.com/golang/go/src go1.25.1 bytes/"
19 "encoding/base64scip-go gomod github.com/golang/go/src go1.25.1 `encoding/base64`/"
20 "net/httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/"
21 "stringsscip-go gomod github.com/golang/go/src go1.25.1 strings/"
22
23 "go.uber.org/zapscip-go gomod go.uber.org/zap v1.28.0 `go.uber.org/zap`/"
24
25 "github.com/caddyserver/caddy/v2scip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/"
26 "github.com/caddyserver/caddy/v2/modules/caddyhttpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/"
27 )
28
29 func initscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/logging`/init().() {
30 caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.RegisterModulescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/RegisterModule().(LogAppendscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/logging`/LogAppend#{})
31 }
32
33 // LogAppend implements a middleware that takes a key and value, where
34 // the key is the name of a log field and the value is a placeholder,
35 // or variable key, or constant value to use for that field.
36 type LogAppendscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/logging`/LogAppend# struct {
37 // Key is the name of the log field.
38 Keyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/logging`/LogAppend#Key. string `json:"key,omitempty"`
39
40 // Value is the value to use for the log field.
41 // If it is a placeholder (with surrounding `{}`),
42 // it will be evaluated when the log is written.
43 // If the value is a key that exists in the `vars`
44 // map, the value of that key will be used. Otherwise
45 // the value will be used as-is as a constant string.
46 Valuescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/logging`/LogAppend#Value. string `json:"value,omitempty"`
47
48 // Early, if true, adds the log field before calling
49 // the next handler in the chain. By default, the log
50 // field is added on the way back up the middleware chain,
51 // after all subsequent handlers have completed.
52 Earlyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/logging`/LogAppend#Early. bool `json:"early,omitempty"`
53 }
54
55 // CaddyModule returns the Caddy module information.
56 func (LogAppendscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/logging`/LogAppend#) CaddyModulescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/logging`/LogAppend#CaddyModule().() caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.ModuleInfoscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/ModuleInfo# {
57 return caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.ModuleInfoscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/ModuleInfo#{
58 IDscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/ModuleInfo#ID.: "http.handlers.log_append",
59 Newscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/ModuleInfo#New.: func() caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.Modulescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Module# { return new(LogAppendscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/logging`/LogAppend#) },
60 }
61 }
62
63 func (h LogAppendscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/logging`/LogAppend#) ServeHTTPscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/logging`/LogAppend#ServeHTTP().(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#, r *httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.Requestscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#, next caddyhttpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/.Handlerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/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.Earlyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/logging`/LogAppend#Early. || h.Valuescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/logging`/LogAppend#Value. == placeholderRequestBodyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/logging`/placeholderRequestBody. || h.Valuescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/logging`/LogAppend#Value. == placeholderRequestBodyBase64scip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/logging`/placeholderRequestBodyBase64.
68
69 // Check if we need to buffer the response for special placeholders
70 needsResponseBody := h.Valuescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/logging`/LogAppend#Value. == placeholderResponseBodyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/logging`/placeholderResponseBody. || h.Valuescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/logging`/LogAppend#Value. == placeholderResponseBodyBase64scip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/logging`/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.addLogFieldscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/logging`/LogAppend#addLogField().(r, nil)
76 }
77
78 var rec caddyhttpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/.ResponseRecorderscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/ResponseRecorder#
79 var 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#
80
81 if needsResponseBody {
82 // Wrap the response writer with a recorder to capture the response body
83 buf = new(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#)
84 rec = caddyhttpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/.NewResponseRecorderscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/NewResponseRecorder().(w, buf, 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 {
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.ServeHTTPscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Handler#ServeHTTP.(w, r)
96
97 if needsResponseBody {
98 // Write the buffered response to the client
99 if rec.Bufferedscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/ResponseRecorder#Buffered.() {
100 h.addLogFieldscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/logging`/LogAppend#addLogField().(r, buf)
101 err := rec.WriteResponsescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/ResponseRecorder#WriteResponse.()
102 if err != nil {
103 return err
104 }
105 }
106 return handlerErr
107 }
108
109 if !h.Earlyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/logging`/LogAppend#Early. {
110 // Add the log field after the handler completes
111 h.addLogFieldscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/logging`/LogAppend#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
119 // response body placeholders.
120 func (h LogAppendscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/logging`/LogAppend#) addLogFieldscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/logging`/LogAppend#addLogField().(r *httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.Requestscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#, 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#) {
121 ctx := r.Contextscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#Context().()
122
123 vars := ctx.Valuescip-go gomod github.com/golang/go/src go1.25.1 context/Context#Value().(caddyhttpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/.VarsCtxKeyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/VarsCtxKey.).(map[string]any)
124 repl := ctx.Valuescip-go gomod github.com/golang/go/src go1.25.1 context/Context#Value().(caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.ReplacerCtxKeyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/ReplacerCtxKey.).(*caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.Replacerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Replacer#)
125 extra := ctx.Valuescip-go gomod github.com/golang/go/src go1.25.1 context/Context#Value().(caddyhttpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/.ExtraLogFieldsCtxKeyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/ExtraLogFieldsCtxKey.).(*caddyhttpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/.ExtraLogFieldsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/ExtraLogFields#)
126
127 var varValue any
128
129 // Handle special case placeholders for response body
130 if h.Valuescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/logging`/LogAppend#Value. == placeholderResponseBodyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/logging`/placeholderResponseBody. {
131 if buf != nil {
132 varValue = buf.Stringscip-go gomod github.com/golang/go/src go1.25.1 bytes/Buffer#String().()
133 } else {
134 varValue = ""
135 }
136 } else if h.Valuescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/logging`/LogAppend#Value. == placeholderResponseBodyBase64scip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/logging`/placeholderResponseBodyBase64. {
137 if buf != nil {
138 varValue = base64scip-go gomod github.com/golang/go/src go1.25.1 `encoding/base64`/.StdEncodingscip-go gomod github.com/golang/go/src go1.25.1 `encoding/base64`/StdEncoding..EncodeToStringscip-go gomod github.com/golang/go/src go1.25.1 `encoding/base64`/Encoding#EncodeToString().(buf.Bytesscip-go gomod github.com/golang/go/src go1.25.1 bytes/Buffer#Bytes().())
139 } else {
140 varValue = ""
141 }
142 } else if stringsscip-go gomod github.com/golang/go/src go1.25.1 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.25.1 strings/HasPrefix().(h.Valuescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/logging`/LogAppend#Value., "{") &&
143 stringsscip-go gomod github.com/golang/go/src go1.25.1 strings/.HasSuffixscip-go gomod github.com/golang/go/src go1.25.1 strings/HasSuffix().(h.Valuescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/logging`/LogAppend#Value., "}") &&
144 stringsscip-go gomod github.com/golang/go/src go1.25.1 strings/.Countscip-go gomod github.com/golang/go/src go1.25.1 strings/Count().(h.Valuescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/logging`/LogAppend#Value., "{") == 1 {
145 // the value looks like a placeholder, so get its value
146 varValue, _ = repl.Getscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Replacer#Get().(stringsscip-go gomod github.com/golang/go/src go1.25.1 strings/.Trimscip-go gomod github.com/golang/go/src go1.25.1 strings/Trim().(h.Valuescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/logging`/LogAppend#Value., "{}"))
147 } else if val, ok := vars[h.Valuescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/logging`/LogAppend#Value.]; ok {
148 // the value is a key in the vars map
149 varValue = val
150 } else {
151 // the value is a constant string
152 varValue = h.Valuescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/logging`/LogAppend#Value.
153 }
154
155 // Add the field to the extra log fields.
156 // We use zap.Any because it will reflect
157 // to the correct type for us.
158 extra.Addscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/ExtraLogFields#Add().(zapscip-go gomod go.uber.org/zap v1.28.0 `go.uber.org/zap`/.Anyscip-go gomod go.uber.org/zap v1.28.0 `go.uber.org/zap`/Any().(h.Keyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/logging`/LogAppend#Key., varValue))
159 }
160
161 const (
162 // Special placeholder values that are handled by log_append
163 // rather than by the replacer.
164 placeholderRequestBodyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/logging`/placeholderRequestBody. = "{http.request.body}"
165 placeholderRequestBodyBase64scip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/logging`/placeholderRequestBodyBase64. = "{http.request.body_base64}"
166 placeholderResponseBodyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/logging`/placeholderResponseBody. = "{http.response.body}"
167 placeholderResponseBodyBase64scip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/logging`/placeholderResponseBodyBase64. = "{http.response.body_base64}"
168 )
169
170 // Interface guards
171 var (
172 _ caddyhttpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/.MiddlewareHandlerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/MiddlewareHandler# = (*LogAppendscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/logging`/LogAppend#)(nil)
173 )