Skip to content

Commit 238fa31

Browse files
Merge pull request #7 from signalsciences/feature/custom-header-fn
Adds support for a custom header extractor fn
2 parents 18c1077 + d7af9bd commit 238fa31

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

module.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ type Module struct {
3737
inspector Inspector
3838
inspInit InspectorInitFunc
3939
inspFini InspectorFiniFunc
40+
headerExtractor func(*http.Request) (http.Header, error)
4041
}
4142

4243
// ModuleConfigOption is a functional config option for configuring the module
@@ -206,6 +207,16 @@ func CustomInspector(insp Inspector, init InspectorInitFunc, fini InspectorFiniF
206207
}
207208
}
208209

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+
209220
// ServeHTTP satisfies the http.Handler interface
210221
func (m *Module) ServeHTTP(w http.ResponseWriter, req *http.Request) {
211222
start := time.Now().UTC()
@@ -334,6 +345,18 @@ func (m *Module) inspectorPreRequest(req *http.Request) (inspin2 RPCMsgIn2, out
334345

335346
inspin := NewRPCMsgIn(req, reqbody, -1, -1, -1, m.moduleVersion, m.serverVersion)
336347

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+
337360
if m.debug {
338361
log.Printf("DEBUG: Making PreRequest call to inspector: %s %s", inspin.Method, inspin.URI)
339362
}

0 commit comments

Comments
 (0)