Servant中如何去掉"Server"header?

How to remove "Server" header in Servant?

我正在使用仆人库。我尝试修改一些博客 post 的方法,但在删除 "Server" header 的情况下它没有做任何事情。 (它仅适用于添加自定义 headers。)

removeServerHeaderMiddleware :: Middleware
removeServerHeaderMiddleware baseApp = \req responseFunc -> baseApp req (newResponseFunc responseFunc)
  where
    newResponseFunc :: (Response -> IO ResponseReceived) -> Response -> IO ResponseReceived
    newResponseFunc responseFunc = removeServerHeader >>> responseFunc
    removeServerHeader :: Response -> Response
    removeServerHeader r = trace (show $ responseHeaders r) $ mapResponseHeaders (filter (\(k, _) -> k /= "Server")) r

app1 :: Application
app1 = serve imageAPI1 server1

main :: IO ()
main = do
  let port = 8080
  putStrLn $ "Starting server on port " <> show port <> "."
  run port $ removeServerHeaderMiddleware app1

回复:

$ http :8080/images
HTTP/1.1 200 OK
Content-Type: application/json;charset=utf-8
Date: Sat, 15 Jun 2019 06:12:58 GMT
Server: Warp/3.2.27
Transfer-Encoding: chunked

查看跟踪,也许 header 是稍后添加的?

[("Content-Type","application/json;charset=utf-8")]

我在 warp 中找到 setServerName,但我不知道如何将它与 servant 合并。

setServerName 的文档指出:

Default server name to be sent as the "Server:" header if an application does not set one. If an empty string is set, the "Server:" header is not sent. This is true even if an application set one.

来自http://hackage.haskell.org/package/warp-3.2.27/docs/Network-Wai-Handler-Warp.html

因此要禁用服务器 header 一个 setServerName 空字符串。

要使用这个,请将 run port 替换为

runSettings (setPort port (setServerName "" defaultSettings))