diff --git a/.swift-version b/.swift-version index f2c6cb6..819e07a 100644 --- a/.swift-version +++ b/.swift-version @@ -1 +1 @@ -4.2.3 +5.0 diff --git a/.travis.yml b/.travis.yml index 3e167f0..2a9ef1f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -25,23 +25,24 @@ matrix: dist: xenial sudo: required services: docker - env: DOCKER_IMAGE=swift:4.2.3 + env: DOCKER_IMAGE=swift:4.2.4 SWIFT_SNAPSHOT=4.2.4 - os: linux dist: xenial sudo: required services: docker - env: DOCKER_IMAGE=swift:4.2.3 SWIFT_SNAPSHOT=$SWIFT_DEVELOPMENT_SNAPSHOT CUSTOM_TEST_SCRIPT=.kitura-test.sh DOCKER_ENVIRONMENT=CUSTOM_TEST_SCRIPT - # Test GCD_ASYNCH codepath on Linux + # Run Kitura tests in addition to Kitura-net tests + env: DOCKER_IMAGE=swift:5.0-xenial CUSTOM_TEST_SCRIPT=.kitura-test.sh DOCKER_ENVIRONMENT=CUSTOM_TEST_SCRIPT - os: linux dist: xenial sudo: required services: docker - env: DOCKER_IMAGE=swift:4.2.3 SWIFT_SNAPSHOT=$SWIFT_DEVELOPMENT_SNAPSHOT CUSTOM_TEST_SCRIPT=testWithGCD.sh DOCKER_ENVIRONMENT=CUSTOM_TEST_SCRIPT + # Test GCD_ASYNCH codepath on Linux + env: DOCKER_IMAGE=swift:5.0-xenial CUSTOM_TEST_SCRIPT=testWithGCD.sh DOCKER_ENVIRONMENT=CUSTOM_TEST_SCRIPT - os: linux dist: xenial sudo: required services: docker - env: DOCKER_IMAGE=ubuntu:18.04 + env: DOCKER_IMAGE=swift:5.0 SWIFT_SNAPSHOT=$SWIFT_DEVELOPMENT_SNAPSHOT - os: osx osx_image: xcode9.2 sudo: required @@ -49,14 +50,20 @@ matrix: - os: osx osx_image: xcode9.4 sudo: required - env: SWIFT_SNAPSHOT=4.1.2 JAZZY_ELIGIBLE=true + env: SWIFT_SNAPSHOT=4.1.2 - os: osx osx_image: xcode10.1 sudo: required + env: SWIFT_SNAPSHOT=4.2.1 JAZZY_ELIGIBLE=true - os: osx - osx_image: xcode10.1 + osx_image: xcode10.2 + sudo: required + # Run Kitura tests in addition to Kitura-net tests + env: CUSTOM_TEST_SCRIPT=.kitura-test.sh + - os: osx + osx_image: xcode10.2 sudo: required - env: SWIFT_SNAPSHOT=$SWIFT_DEVELOPMENT_SNAPSHOT CUSTOM_TEST_SCRIPT=.kitura-test.sh + env: SWIFT_SNAPSHOT=$SWIFT_DEVELOPMENT_SNAPSHOT before_install: - git clone https://github.com/IBM-Swift/Package-Builder.git diff --git a/Package.swift b/Package.swift index 2843421..27fc348 100644 --- a/Package.swift +++ b/Package.swift @@ -1,8 +1,8 @@ -// swift-tools-version:4.2 +// swift-tools-version:5.0 // The swift-tools-version declares the minimum version of Swift required to build this package. /** - * Copyright IBM Corporation 2016, 2017 + * Copyright IBM Corporation 2016-2019 * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/Package@swift-4.2.swift b/Package@swift-4.2.swift new file mode 100644 index 0000000..2843421 --- /dev/null +++ b/Package@swift-4.2.swift @@ -0,0 +1,81 @@ +// swift-tools-version:4.2 +// The swift-tools-version declares the minimum version of Swift required to build this package. + +/** + * Copyright IBM Corporation 2016, 2017 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + **/ + +import PackageDescription + +var dependencies: [Package.Dependency] = [ + .package(url: "https://github.com/IBM-Swift/LoggerAPI.git", from: "1.7.3"), + .package(url: "https://github.com/IBM-Swift/BlueSocket.git", from: "1.0.0"), + .package(url: "https://github.com/IBM-Swift/BlueSSLService.git", from: "1.0.0") +] + +var kituraNetDependencies: [Target.Dependency] = [ + .byName(name: "CHTTPParser"), + .byName(name: "LoggerAPI"), + .byName(name: "Socket"), + .target(name: "CCurl"), + .byName(name: "SSLService") +] + +#if os(Linux) +dependencies.append(contentsOf: [ + .package(url: "https://github.com/IBM-Swift/BlueSignals.git", from: "1.0.0") + ]) + +kituraNetDependencies.append(contentsOf: [ + .target(name: "CEpoll"), + .byName(name: "Signals") + ]) +#endif + +var targets: [Target] = [ + .target( + name: "CHTTPParser" + ), + .systemLibrary( + name: "CCurl" + ), + .target( + name: "KituraNet", + dependencies: kituraNetDependencies + ), + .testTarget( + name: "KituraNetTests", + dependencies: ["KituraNet"] + ) +] + +#if os(Linux) +targets.append( + .systemLibrary(name: "CEpoll") +) +#endif + +let package = Package( + name: "Kitura-net", + products: [ + // Products define the executables and libraries produced by a package, and make them visible to other packages. + .library( + name: "KituraNet", + targets: ["KituraNet"] + ) + ], + dependencies: dependencies, + targets: targets +) diff --git a/Sources/KituraNet/ClientRequest.swift b/Sources/KituraNet/ClientRequest.swift index f20d66f..23eef26 100644 --- a/Sources/KituraNet/ClientRequest.swift +++ b/Sources/KituraNet/ClientRequest.swift @@ -644,6 +644,20 @@ extension ClientRequest: CurlInvokerDelegate { fileprivate func curlHeaderCallback(_ buf: UnsafeMutablePointer, size: Int) -> Int { // If the header status line begins with 'HTTP/2 ' we replace it with 'HTTP/2.0' because // otherwise the CHTTPParser will parse this line incorrectly and won't extract the status code +#if swift(>=5.0) + ClientRequest.Http2StatusLineVersion.withUnsafeBytes() { (rawPtr: UnsafeRawBufferPointer) -> Void in + if memcmp(rawPtr.baseAddress!, buf, ClientRequest.Http2StatusLineVersion.count) == 0 { + ClientRequest.Http2StatusLineVersionWithMinor.withUnsafeBytes() { (p: UnsafeRawBufferPointer) -> Void in + response?.responseBuffers.append(bytes: p.bindMemory(to: UInt8.self).baseAddress!, length: ClientRequest.Http2StatusLineVersionWithMinor.count) + response?.responseBuffers.append(bytes: UnsafeRawPointer(buf).assumingMemoryBound(to: UInt8.self) + ClientRequest.Http2StatusLineVersion.count, + length: size - ClientRequest.Http2StatusLineVersion.count) + } + } + else { + response?.responseBuffers.append(bytes: UnsafeRawPointer(buf).assumingMemoryBound(to: UInt8.self), length: size) + } + } +#else ClientRequest.Http2StatusLineVersion.withUnsafeBytes() { (ptr: UnsafePointer) -> Void in if memcmp(ptr, buf, ClientRequest.Http2StatusLineVersion.count) == 0 { ClientRequest.Http2StatusLineVersionWithMinor.withUnsafeBytes() { (p: UnsafePointer) -> Void in @@ -656,6 +670,7 @@ extension ClientRequest: CurlInvokerDelegate { response?.responseBuffers.append(bytes: UnsafeRawPointer(buf).assumingMemoryBound(to: UInt8.self), length: size) } } +#endif return size diff --git a/Sources/KituraNet/HTTPParser/URLParser.swift b/Sources/KituraNet/HTTPParser/URLParser.swift index 3b98c73..3453cd8 100644 --- a/Sources/KituraNet/HTTPParser/URLParser.swift +++ b/Sources/KituraNet/HTTPParser/URLParser.swift @@ -125,9 +125,15 @@ public class URLParser : CustomStringConvertible { memset(&parsedURL, 0, MemoryLayout.size) let cIsConnect: Int32 = (isConnect ? 1 : 0) +#if swift(>=5.0) + let returnCode = url.withUnsafeBytes() { (bytes: UnsafeRawBufferPointer) -> Int32 in + return http_parser_parse_url_url(bytes.bindMemory(to: Int8.self).baseAddress, url.count, cIsConnect, &parsedURL) + } +#else let returnCode = url.withUnsafeBytes() { (bytes: UnsafePointer) -> Int32 in return http_parser_parse_url_url(bytes, url.count, cIsConnect, &parsedURL) } +#endif guard returnCode == 0 else { return } diff --git a/Tests/KituraNetTests/FastCGIProtocolTests.swift b/Tests/KituraNetTests/FastCGIProtocolTests.swift index 5efda5e..bd76305 100644 --- a/Tests/KituraNetTests/FastCGIProtocolTests.swift +++ b/Tests/KituraNetTests/FastCGIProtocolTests.swift @@ -107,8 +107,12 @@ class FastCGIProtocolTests: KituraNetTest { #else Darwin.arc4random_buf(&bytes, numberOfBytes) #endif - + +#if swift(>=5.0) + return Data(bytes) +#else return Data(bytes: bytes) +#endif } // Test an FCGI_STDOUT or FCGI_STDIN exchange with overly large bundle. diff --git a/Tests/KituraNetTests/FastCGIRequestTests.swift b/Tests/KituraNetTests/FastCGIRequestTests.swift index 09b2b3d..cbc941c 100644 --- a/Tests/KituraNetTests/FastCGIRequestTests.swift +++ b/Tests/KituraNetTests/FastCGIRequestTests.swift @@ -294,7 +294,11 @@ class FastCGIRequestTests: KituraNetTest { valueNetworkByteOrder = CFSwapInt16HostToBig(value) #endif let asBytes = UnsafeMutablePointer(&valueNetworkByteOrder) +#if swift(>=4.2) + (UnsafeMutableRawPointer(mutating: bytes)+offset).copyMemory(from: asBytes, byteCount: 2) +#else (UnsafeMutableRawPointer(mutating: bytes)+offset).copyBytes(from: asBytes, count: 2) +#endif } private func copyUInt32IntoBuffer(_ bytes: inout [UInt8], offset: Int, value: UInt32) { @@ -305,7 +309,11 @@ class FastCGIRequestTests: KituraNetTest { valueNetworkByteOrder = CFSwapInt32HostToBig(value) #endif let asBytes = UnsafeMutablePointer(&valueNetworkByteOrder) +#if swift(>=4.2) + (UnsafeMutableRawPointer(mutating: bytes)+offset).copyMemory(from: asBytes, byteCount: 4) +#else (UnsafeMutableRawPointer(mutating: bytes)+offset).copyBytes(from: asBytes, count: 4) +#endif } class TestDelegate : ServerDelegate { diff --git a/Tests/KituraNetTests/RegressionTests.swift b/Tests/KituraNetTests/RegressionTests.swift index 7e353c1..513f8a3 100644 --- a/Tests/KituraNetTests/RegressionTests.swift +++ b/Tests/KituraNetTests/RegressionTests.swift @@ -191,9 +191,7 @@ class RegressionTests: KituraNetTest { do { let sharingServer: HTTPServer = try startServer(nil, port: serverPort, useSSL: false, allowPortReuse: true) - defer { - sharingServer.stop() - } + sharingServer.stop() } catch { XCTFail("Second server could not share listener port, received: \(error)") }