@@ -37,6 +37,7 @@ type Module struct {
37
37
inspector Inspector
38
38
inspInit InspectorInitFunc
39
39
inspFini InspectorFiniFunc
40
+ headerExtractor func (* http.Request ) (http.Header , error )
40
41
}
41
42
42
43
// ModuleConfigOption is a functional config option for configuring the module
@@ -206,6 +207,16 @@ func CustomInspector(insp Inspector, init InspectorInitFunc, fini InspectorFiniF
206
207
}
207
208
}
208
209
210
+ // CustomHeaderExtractor is a function argument that sets a function to extract
211
+ // an alternative header object from the request. It is primarily intended only
212
+ // for internal use.
213
+ func CustomHeaderExtractor (fn func (r * http.Request ) (http.Header , error )) ModuleConfigOption {
214
+ return func (m * Module ) error {
215
+ m .headerExtractor = fn
216
+ return nil
217
+ }
218
+ }
219
+
209
220
// ServeHTTP satisfies the http.Handler interface
210
221
func (m * Module ) ServeHTTP (w http.ResponseWriter , req * http.Request ) {
211
222
start := time .Now ().UTC ()
@@ -334,6 +345,18 @@ func (m *Module) inspectorPreRequest(req *http.Request) (inspin2 RPCMsgIn2, out
334
345
335
346
inspin := NewRPCMsgIn (req , reqbody , - 1 , - 1 , - 1 , m .moduleVersion , m .serverVersion )
336
347
348
+ // If the user supplied a custom header extractor, use it to unpack the
349
+ // headers. If there no custom header extractor or it returns an error,
350
+ // fallback to the native headers on the request.
351
+ if m .headerExtractor != nil {
352
+ hin , err := m .headerExtractor (req )
353
+ if err == nil {
354
+ inspin .HeadersIn = convertHeaders (hin )
355
+ } else if m .debug {
356
+ log .Printf ("DEBUG: Error extracting custom headers, using native headers: %s" , err )
357
+ }
358
+ }
359
+
337
360
if m .debug {
338
361
log .Printf ("DEBUG: Making PreRequest call to inspector: %s %s" , inspin .Method , inspin .URI )
339
362
}
0 commit comments