Skip to content

Commit

Permalink
Update to Swift 5 (#297)
Browse files Browse the repository at this point in the history
  • Loading branch information
djones6 authored Apr 4, 2019
1 parent 6c9ad87 commit e050157
Show file tree
Hide file tree
Showing 9 changed files with 134 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .swift-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4.2.3
5.0
23 changes: 15 additions & 8 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,38 +25,45 @@ 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
env: SWIFT_SNAPSHOT=4.0.3
- 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
Expand Down
4 changes: 2 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
81 changes: 81 additions & 0 deletions [email protected]
Original file line number Diff line number Diff line change
@@ -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
)
15 changes: 15 additions & 0 deletions Sources/KituraNet/ClientRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,20 @@ extension ClientRequest: CurlInvokerDelegate {
fileprivate func curlHeaderCallback(_ buf: UnsafeMutablePointer<Int8>, 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<UInt8>) -> Void in
if memcmp(ptr, buf, ClientRequest.Http2StatusLineVersion.count) == 0 {
ClientRequest.Http2StatusLineVersionWithMinor.withUnsafeBytes() { (p: UnsafePointer<UInt8>) -> Void in
Expand All @@ -656,6 +670,7 @@ extension ClientRequest: CurlInvokerDelegate {
response?.responseBuffers.append(bytes: UnsafeRawPointer(buf).assumingMemoryBound(to: UInt8.self), length: size)
}
}
#endif

return size

Expand Down
6 changes: 6 additions & 0 deletions Sources/KituraNet/HTTPParser/URLParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,15 @@ public class URLParser : CustomStringConvertible {
memset(&parsedURL, 0, MemoryLayout<http_parser_url>.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<Int8>) -> Int32 in
return http_parser_parse_url_url(bytes, url.count, cIsConnect, &parsedURL)
}
#endif

guard returnCode == 0 else { return }

Expand Down
6 changes: 5 additions & 1 deletion Tests/KituraNetTests/FastCGIProtocolTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 8 additions & 0 deletions Tests/KituraNetTests/FastCGIRequestTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 {
Expand Down
4 changes: 1 addition & 3 deletions Tests/KituraNetTests/RegressionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)")
}
Expand Down

0 comments on commit e050157

Please sign in to comment.