0

In request handler, processing e.g. 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 respectively?

Currently I'm constructing it like so

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

where httpPrefix is:

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

But it feels like there's a better way...

Dannie P
  • 4,464
  • 3
  • 29
  • 48

1 Answers1

0

I've discovered that host header would contain either example.com or 0.0.0.0:8080 depending on the server.

So the following produces just the result I need. httpScheme is still hardcoded (which is something I don't like, but don't see other options yet).

httpScheme + request.header(.host)!

I'm force-unwrapping, as I observe host header is always there.

Dannie P
  • 4,464
  • 3
  • 29
  • 48