Skip to content

Commit

Permalink
fix #755 Allow to set custom Server header
Browse files Browse the repository at this point in the history
  • Loading branch information
ademar committed May 14, 2021
1 parent 48b34bd commit ffe9d3f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
11 changes: 11 additions & 0 deletions src/Suave.Tests/HttpWriters.fs
Original file line number Diff line number Diff line change
Expand Up @@ -189,4 +189,15 @@ let headers cfg =
],
hdrs |> getRespHeaders "Vary"))
ctx

testCase "setHeader adds Server header with hideHeader = true" <| fun _ ->
let ctx = runWith { cfg with hideHeader = true } (Writers.setHeader "Server" "My custom value" >=> OK "test")

withContext (fun _ ->
let hdrs = requestHeaders ()
Assert.Equal(
"expecting Server header value",
[ "Server", "My custom value" ],
hdrs |> getRespHeaders "Server"))
ctx
]
11 changes: 7 additions & 4 deletions src/Suave/HttpOutput.fs
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,19 @@ module HttpOutput =
do! asyncWriteLn (String.Concat [| x; ": "; y |])
}

let inline writePreamble exclusions (context: HttpContext) = withConnection {
let inline writePreamble (context: HttpContext) = withConnection {

let r = context.response

do! asyncWriteBufferedArrayBytes [| ByteConstants.httpVersionBytes; ASCII.bytes (r.status.code.ToString());
ByteConstants.spaceBytes; ASCII.bytes (r.status.reason); ByteConstants.dateBytes; ASCII.bytes (Globals.utcNow().ToString("R")); ByteConstants.EOL |]
if not context.runtime.hideHeader then

if context.runtime.hideHeader then
do! writeHeaders ["date";"content-length"] r.headers
else
do! asyncWriteBufferedBytes ByteConstants.serverHeaderBytes
do! writeHeaders ["server";"date";"content-length"] r.headers

do! writeHeaders exclusions r.headers
do! writeContentType r.headers
}

Expand Down Expand Up @@ -134,7 +137,7 @@ module HttpOutput =
let writeResponse (newCtx:HttpContext) =
socket{
if newCtx.response.writePreamble then
let! (_, connection) = writePreamble ["server";"date";"content-length"] newCtx newCtx.connection
let! (_, connection) = writePreamble newCtx newCtx.connection
let! connection = writeContent true { newCtx with connection = connection } newCtx.response.content
return { newCtx with connection = connection }
else
Expand Down

0 comments on commit ffe9d3f

Please sign in to comment.