Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Provide initial API for socket TLS session
  • Loading branch information
viktorasl committed Aug 1, 2019
commit 8265d2c1915207de648ff850aab67ad9d18310be
19 changes: 18 additions & 1 deletion XCode/Sources/HttpServerIO.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ public class HttpServerIO {
/// Otherwise, `listenAddressIPv4` will be used.
public var listenAddressIPv6: String?

#if !os(Linux)
/// SSL certificate to use in TLS session
public var sslCertificate: CFArray?
#endif

private let queue = DispatchQueue(label: "swifter.httpserverio.clientsockets")

public func port() throws -> Int {
Expand Down Expand Up @@ -116,6 +121,19 @@ public class HttpServerIO {
}

private func handleConnection(_ socket: Socket) {
defer {
socket.close()
}
#if !os(Linux)
if let cert = sslCertificate {
do {
try socket.startTlsSession(with: cert)
} catch {
print("Failed to start TLS session: \(error)")
return
}
}
#endif
let parser = HttpParser()
while self.operating, let request = try? parser.readHttpRequest(socket) {
let request = request
Expand All @@ -139,7 +157,6 @@ public class HttpServerIO {
}
if !keepConnection { break }
}
socket.close()
}

private struct InnerWriteContext: HttpResponseBodyWriter {
Expand Down
6 changes: 6 additions & 0 deletions XCode/Sources/Socket.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ open class Socket: Hashable, Equatable {
Socket.close(self.socketFileDescriptor)
}

#if !os(Linux)
public func startTlsSession(with certificate: CFArray) {
// TODO: TLS session init and setup
}
#endif

public func port() throws -> in_port_t {
var addr = sockaddr_in()
return try withUnsafePointer(to: &addr) { pointer in
Expand Down