Skip to content

Commit

Permalink
uds: create the ClientRequest for redirected URL right
Browse files Browse the repository at this point in the history
  • Loading branch information
Pushkar N Kulkarni committed Mar 31, 2019
1 parent 450882a commit 41062b6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
20 changes: 12 additions & 8 deletions Sources/KituraNet/ClientRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ public class ClientRequest {
let waitSemaphore = DispatchSemaphore(value: 0)

// Socket path for Unix domain sockets
private var unixDomainSocketPath: String?
var unixDomainSocketPath: String?

/**
Client request options enum. This allows the client to specify certain parameteres such as HTTP headers, HTTP methods, host names, and SSL credentials.
Expand Down Expand Up @@ -569,7 +569,7 @@ public class ClientRequest {
channel = try bootstrap.connect(host: hostName, port: Int(self.port!)).wait()
}
} catch let error {
Log.error("Connection to \(hostName):\(self.port ?? 80) failed with error: \(error)")
Log.error("Connection to \(hostName): self.unixDomainSocketPath ?? \(self.port ?? 80) failed with error: \(error)")
callback(nil)
return
}
Expand Down Expand Up @@ -768,13 +768,17 @@ class HTTPClientHandler: ChannelInboundHandler {
}
if url.starts(with: "/") {
let scheme = URL(string: clientRequest.url)?.scheme
let port = clientRequest.port.map { UInt16($0) }.map { $0.toInt16() }!
let request = ClientRequest(options: [.schema(scheme!),
.hostname(clientRequest.hostName!),
.port(port),
.path(url)],
callback: clientRequest.callback)
var options: [ClientRequest.Options] = [.schema(scheme!), .hostname(clientRequest.hostName!), .path(url)]
let request: ClientRequest
if let socketPath = self.clientRequest.unixDomainSocketPath {
request = ClientRequest(options: options, unixDomainSocketPath: socketPath, callback: clientRequest.callback)
} else {
let port = clientRequest.port.map { UInt16($0) }.map { $0.toInt16() }!
options.append(.port(port))
request = ClientRequest(options: options, callback: clientRequest.callback)
}
request.maxRedirects = self.clientRequest.maxRedirects - 1

// The next request can be asynchronously moved to a DispatchQueue.
// ClientRequest.end() calls connect().wait(), so we better move this to a dispatch queue.
// Because ClientRequest.end() is blocking, we mark the current task complete after the new task also completes.
Expand Down
2 changes: 1 addition & 1 deletion Sources/KituraNet/HTTP/HTTPServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ public class HTTPServer: Server {
case SocketType.tcp(let port):
serverChannel = try bootstrap.bind(host: "0.0.0.0", port: port).wait()
self.port = serverChannel?.localAddress?.port.map { Int($0) }
listenerDescription = "port \(self.port)"
listenerDescription = "port \(self.port ?? port)"
case SocketType.unix(let unixDomainSocketPath):
// Ensure the path doesn't exist...
#if os(Linux)
Expand Down

0 comments on commit 41062b6

Please sign in to comment.