0

In my iOS app I'm trying to add a simple GCDWebServer redirect handler like so:

self.webServer!.addHandlerForMethod("GET", 
    pathRegex: "/api/.*",
    requestClass: GCDWebServerRequest.self,
    processBlock: { request in
        let url = NSURL(string: "http://external.host\(request.URL!.absoluteString)")
        return GCDWebServerResponse(redirect: url, permanent: true)
    }
)

but my url is malformed. How do I properly construct an NSURL from a given host and GCDWebServerRequest.URL ?

Robert Carter Mills
  • 793
  • 1
  • 9
  • 19
  • What is the value of `url`? It seems like your current construction might end up with two schemes, which would be invalid. – Aaron Brager Oct 04 '15 at 23:40
  • Ah, it is `nil` ! It appears my `url` is indeed malformed. If I hardcode an external url such as `let url = NSURL(string: "http://external.host/api/playthrough_ids")` then it works. So I'll edit the question, to rephrase 'how to form a proper `NSURL` with host + `request.URL` – Robert Carter Mills Oct 05 '15 at 00:10

1 Answers1

0

request.URL is an NSURL object and as such, using it to form another url with a different host necessitates splitting it by accessing its path like so:

let url = NSURL(string: "http://host.com\(request.URL!.path)")
Aaron Brager
  • 65,323
  • 19
  • 161
  • 287
Robert Carter Mills
  • 793
  • 1
  • 9
  • 19