Skip to content
This repository has been archived by the owner on Apr 7, 2022. It is now read-only.

Commit

Permalink
enable cert verification by default (#328)
Browse files Browse the repository at this point in the history
* enable cert validation by default

* add new HTTPScheme.customHTTPS option
  • Loading branch information
tanner0101 committed Jan 25, 2019
1 parent b034c9a commit 4c8c655
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
8 changes: 6 additions & 2 deletions Sources/HTTP/Responder/HTTPScheme.swift
Expand Up @@ -7,10 +7,14 @@ public struct HTTPScheme {

/// Enables TLS (SSL). Uses port `443` by default.
public static var https: HTTPScheme {
return self.customHTTPS(.forClient())
}

/// Enables TLS (SSL) with custom TLS configuration. Uses port `443` by default.
public static func customHTTPS(_ config: TLSConfiguration) -> HTTPScheme {
return .init(443) { channel, hostname in
return Future.flatMap(on: channel.eventLoop) {
let tlsConfiguration = TLSConfiguration.forClient(certificateVerification: .none)
let sslContext = try SSLContext(configuration: tlsConfiguration)
let sslContext = try SSLContext(configuration: config)
let sniName = hostname.isIPAddress() ? nil : hostname
let tlsHandler = try OpenSSLClientHandler(context: sslContext, serverHostname: sniName)
return channel.pipeline.add(handler: tlsHandler)
Expand Down
6 changes: 3 additions & 3 deletions Tests/HTTPTests/HTTPClientTests.swift
Expand Up @@ -26,8 +26,8 @@ class HTTPClientTests: XCTestCase {
try testURL("http://zombo.com", contains: "<title>ZOMBO</title>")
}

func testAmazonWithTLS() throws {
try testURL("https://www.amazon.com", contains: "Amazon.com, Inc.")
func testGoogleWithTLS() throws {
try testURL("https://www.google.com/search?q=vapor+swift", contains: "web framework")
}

func testSNIWebsite() throws {
Expand All @@ -45,7 +45,7 @@ class HTTPClientTests: XCTestCase {
("testGoogleAPIsFCM", testGoogleAPIsFCM),
("testExampleCom", testExampleCom),
("testZombo", testZombo),
("testAmazonWithTLS", testAmazonWithTLS),
("testGoogleWithTLS", testGoogleWithTLS),
("testSNIWebsite", testSNIWebsite),
("testQuery", testQuery),
]
Expand Down

0 comments on commit 4c8c655

Please sign in to comment.