Skip to content

Commit

Permalink
Merge pull request #156 from nethraravindran/url
Browse files Browse the repository at this point in the history
Build urlURL using URLComponents
  • Loading branch information
Pushkar N Kulkarni authored Jan 26, 2019
2 parents c02779e + 9b6a8b8 commit fe5db77
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions Sources/KituraNet/HTTP/HTTPServerRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,15 @@ public class HTTPServerRequest: ServerRequest {

private var _url: URL?

private var _urlComponents: URLComponents?

public var urlURL: URL {
if let _url = _url {
return _url
}
var url = ""

self.enableSSL ? url.append("https://") : url.append("http://")
_urlComponents = URLComponents()
_urlComponents?.scheme = self.enableSSL ? "https" : "http"

var localAddress = ""
var localAddressPort = UInt16(0)
Expand All @@ -67,22 +69,25 @@ public class HTTPServerRequest: ServerRequest {
}

if let hostname = headers["Host"]?.first {
url.append(hostname)
if !hostname.contains(":") {
url.append(":")
url.append(String(describing: localAddressPort))
}
_urlComponents?.host = hostname
} else {
Log.error("Host header not received")
let hostname = localAddress
url.append(hostname == "127.0.0.1" ? "localhost" : hostname)
url.append(":")
url.append(String(describing: localAddressPort))
_urlComponents?.host = hostname == "127.0.0.1" ? "localhost" : hostname
}

_urlComponents?.port = Int(localAddressPort)

let uriComponents = _urlString.split(separator: "?")
if uriComponents.count > 0 {
_urlComponents?.path = String(uriComponents[0])
}

url.append(_urlString)
if uriComponents.count > 1 {
_urlComponents?.percentEncodedQuery = String(uriComponents[1])
}

if let urlURL = URL(string: url) {
if let urlURL = _urlComponents?.url {
self._url = urlURL
} else {
Log.error("URL init failed from: \(url)")
Expand Down

0 comments on commit fe5db77

Please sign in to comment.