Skip to content

Commit 8e7ce8e

Browse files
ozangoktandbrattli
andauthored
Add case-insensitive fallback for response header logging (#150)
* add case-insensitive fallback for response header logging * formatting * Update src/Logging.fs Co-authored-by: Dag Brattli <[email protected]> * Simplify header logging --------- Co-authored-by: Dag Brattli <[email protected]>
1 parent 44def6b commit 8e7ce8e

File tree

6 files changed

+539
-506
lines changed

6 files changed

+539
-506
lines changed

.paket/Paket.Restore.targets

Lines changed: 500 additions & 497 deletions
Large diffs are not rendered by default.

examples/github/GitHub.fsproj.user

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<ActiveDebugProfile>IIS Express</ActiveDebugProfile>
5+
</PropertyGroup>
6+
</Project>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<ActiveDebugProfile>IIS Express</ActiveDebugProfile>
5+
</PropertyGroup>
6+
</Project>

src/Logging.fs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,23 @@ module Logging =
2424
let private replacer (_: Match) : string =
2525
$"{{{PlaceHolder.ResponseHeader}__{Interlocked.Increment(&placeholderCounter)}}}"
2626

27+
let private lowerCaseHeaders (headers: Map<string, seq<string>>) : Map<string, seq<string>> =
28+
headers |> Seq.map (fun kv -> kv.Key.ToLowerInvariant(), kv.Value) |> Map.ofSeq
29+
2730
let private getHeaderValue (headers: Map<string, seq<string>>) (key: string) : string =
28-
match headers.TryGetValue(key) with
29-
| true, v ->
30-
match Seq.tryHead v with
31-
| first -> if first.IsSome then first.Value else String.Empty
32-
| false, _ -> String.Empty
31+
headers
32+
|> Map.tryFind key
33+
|> Option.defaultValue Seq.empty
34+
|> Seq.tryHead
35+
|> Option.defaultValue String.Empty
3336

3437
let private log' logLevel ctx content =
3538
match ctx.Request.Logger with
3639
| Some logger ->
3740
let format = ctx.Request.LogFormat
3841
let request = ctx.Request
3942
let matches = reqex.Matches format
43+
let lowerCaseHeaders = lazy (lowerCaseHeaders ctx.Response.Headers)
4044

4145
// Create an array with values in the same order as in the format string. Important to be lazy and not
4246
// stringify any values here. Only pass references to the objects themselves so the logger can stringify
@@ -56,7 +60,7 @@ module Logging =
5660
| PlaceHolder.ResponseHeader ->
5761
// GroupCollection returns empty string values for indexes beyond what was captured, therefore
5862
// we don't cause an exception here if the optional second group was not captured
59-
getHeaderValue ctx.Response.Headers match'.Groups[3].Value :> _
63+
getHeaderValue lowerCaseHeaders.Value (match'.Groups[3].Value.ToLowerInvariant()) :> _
6064
| key ->
6165
// Look for the key in the extra info. This also enables custom HTTP handlers to add custom
6266
// placeholders to the format string.

test/Fetch.fs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ let ``Get with logging is OK`` () =
132132
"Message"
133133
"ResponseHeader[missing-key]"
134134
"ResponseHeader[X-Request-ID]"
135+
"ResponseHeader[x-request-id]"
135136
"ResponseHeader"
136137
"ResponseHeader["
137138
"ResponseHeader]"
@@ -163,7 +164,13 @@ let ``Get with logging is OK`` () =
163164
// Assert
164165
test <@ logger.Output.Contains "42" @>
165166
test <@ logger.Output.Contains "http://test.org" @>
166-
test <@ logger.Output.Contains $"test-value\n← {msg}\n\n← test-request-id\n\n\n\n\n← end" @>
167+
168+
test
169+
<@
170+
logger.Output.Contains
171+
$"test-value\n← {msg}\n\n← test-request-id\n← test-request-id\n\n\n\n\n← end"
172+
@>
173+
167174
test <@ logger.Output.Contains "not-included-in-log" = false @>
168175
test <@ Result.isOk result @>
169176
test <@ metrics.Retries = 0L @>
@@ -204,6 +211,7 @@ let ``Post with logging is OK`` () =
204211
"Message"
205212
"ResponseHeader[missing-key]"
206213
"ResponseHeader[X-Request-ID]"
214+
"ResponseHeader[x-request-id]"
207215
"ResponseHeader"
208216
"ResponseHeader["
209217
"ResponseHeader]"
@@ -230,7 +238,13 @@ let ``Post with logging is OK`` () =
230238
test <@ logger.Output.Contains json @>
231239
test <@ logger.Output.Contains msg @>
232240
test <@ logger.Output.Contains "http://testing.org" @>
233-
test <@ logger.Output.Contains $"test-value\n← {msg}\n\n← test-request-id\n\n\n\n\n← end" @>
241+
242+
test
243+
<@
244+
logger.Output.Contains
245+
$"test-value\n← {msg}\n\n← test-request-id\n← test-request-id\n\n\n\n\n← end"
246+
@>
247+
234248
test <@ logger.Output.Contains "not-included-in-log" = false @>
235249
test <@ Result.isOk result @>
236250
test <@ retries' = 1 @>

version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
6.0.1
1+
6.0.2

0 commit comments

Comments
 (0)