完美,swift,从处理程序中的请求获取服务器地址

Perfect, swift, getting server address from request in handler

在请求处理程序中,处理例如GET https://example.com/collections/1 or POSThttp://0.0.0.0:8080/collections how do I get server address https://example.com and http://0.0.0.0:8080分别?

目前我是这样构建的

 var url = "\(httpPrefix)\(server.serverAddress)"
 if server.serverPort != 443 { url += ":\(server.serverPort)" }

其中 httpPrefix 是:

let httpPrefix = isLinux ? "https://" : "http://"

不过感觉还有更好的方法...

我发现 host header 会包含 example.com0.0.0.0:8080,具体取决于服务器。

所以下面的结果正好是我需要的结果。 httpScheme 仍然是硬编码的(这是我不喜欢的,但还没有看到其他选项)。

httpScheme + request.header(.host)!

我是 force-unwrapping,因为我观察到主持人 header 总是在那里。