Skip to content

Commit

Permalink
Support Swift 4
Browse files Browse the repository at this point in the history
  • Loading branch information
youming-lin committed Sep 13, 2017
1 parent ab37545 commit 8e0c79c
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 33 deletions.
74 changes: 74 additions & 0 deletions Package@swift-4.0.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// swift-tools-version:4.0
// 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", .upToNextMinor(from: "1.7.0")),
.package(url: "https://github.com/IBM-Swift/BlueSocket.git", .upToNextMinor(from: "0.12.0")),
.package(url: "https://github.com/IBM-Swift/CCurl.git", .upToNextMinor(from: "0.4.0")),
.package(url: "https://github.com/IBM-Swift/BlueSSLService.git", .upToNextMinor(from: "0.12.0"))
]

var kituraNetDependencies: [Target.Dependency] = [
.byNameItem(name: "CHTTPParser"),
.byNameItem(name: "LoggerAPI"),
.byNameItem(name: "Socket"),
.byNameItem(name: "CCurl"),
.byNameItem(name: "SSLService")
]

#if os(Linux)
dependencies.append(contentsOf: [
.package(url: "https://github.com/IBM-Swift/CEpoll.git", .upToNextMinor(from: "0.1.0")),
.package(url: "https://github.com/IBM-Swift/BlueSignals.git", .upToNextMinor(from: "0.9.0"))
])

kituraNetDependencies.append(contentsOf: [
.byNameItem(name: "CEpoll"),
.byNameItem(name: "Signals")
])
#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 are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
.target(
name: "CHTTPParser"
),
.target(
name: "KituraNet",
dependencies: kituraNetDependencies
),
.testTarget(
name: "KituraNetTests",
dependencies: ["KituraNet"]
)
]
)
49 changes: 31 additions & 18 deletions Sources/KituraNet/FastCGI/FastCGIServerRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public class FastCGIServerRequest : ServerRequest {
/// This contains just the path and query parameters starting with '/'
/// Use 'urlURL' for the full URL
@available(*, deprecated, message:
"This contains just the path and query parameters starting with '/'. use 'urlURL' instead")
"This contains just the path and query parameters starting with '/'. use 'urlURL' instead")
public var urlString : String { return requestUri ?? "" }

/// The URL from the request in UTF-8 form
Expand All @@ -64,10 +64,10 @@ public class FastCGIServerRequest : ServerRequest {
/// The URL from the request as URLComponents
/// URLComponents has a memory leak on linux as of swift 3.0.1. Use 'urlURL' instead
@available(*, deprecated, message:
"URLComponents has a memory leak on linux as of swift 3.0.1. use 'urlURL' instead")
"URLComponents has a memory leak on linux as of swift 3.0.1. use 'urlURL' instead")
public lazy var urlComponents: URLComponents = { [unowned self] () in
return URLComponents(url: self.urlURL, resolvingAgainstBaseURL: false) ?? URLComponents()
}()
}()

/// Chunk of body read in by the http_parser, filled by callbacks to onBody
private var bodyChunk = BufferList()
Expand Down Expand Up @@ -200,14 +200,16 @@ public class FastCGIServerRequest : ServerRequest {
/// with "HTTP_" prefixed. We want to normalize these out to remove HTTP_
/// and correct the capitilization (first letter of each word capitilized).
private func processHttpHeader(_ name: String, value: String, remove: String) {

var processedName : String = name.substring(from:
name.index(name.startIndex, offsetBy: remove.characters.count))
#if swift(>=3.2)
var processedName = String(name[name.index(name.startIndex, offsetBy: remove.count)...])
#else
var processedName = name.substring(from: name.index(name.startIndex, offsetBy: remove.characters.count))
#endif

processedName = processedName.replacingOccurrences(of: "_", with: "-")
processedName = processedName.capitalized

headers.append(processedName as String, value: value)
headers.append(processedName, value: value)
}

/// Parse the server protocol into a major and minor version
Expand All @@ -219,11 +221,15 @@ public class FastCGIServerRequest : ServerRequest {

guard protocolString.lowercased().hasPrefix("http/") &&
protocolString.characters.count > "http/".characters.count else {
return
return
}

let versionPortion : String = protocolString.substring(from:
protocolString.index(protocolString.startIndex, offsetBy: "http/".characters.count))

#if swift(>=3.2)
let versionPortion = String(protocolString[protocolString.index(protocolString.startIndex, offsetBy: "http/".count)...])
#else
let versionPortion = protocolString.substring(from: protocolString.index(protocolString.startIndex, offsetBy: "http/".characters.count))
#endif

var decimalPosition : Int = 0

for i in versionPortion.characters {
Expand All @@ -239,15 +245,23 @@ public class FastCGIServerRequest : ServerRequest {

// get major version
if decimalPosition > 0 {
let majorPortion : String = versionPortion.substring(to:
versionPortion.index(versionPortion.startIndex, offsetBy: decimalPosition))
#if swift(>=3.2)
let majorPortion = String(versionPortion[..<versionPortion.index(versionPortion.startIndex, offsetBy: decimalPosition)])
#else
let majorPortion = versionPortion.substring(to: versionPortion.index(versionPortion.startIndex, offsetBy: decimalPosition))
#endif

majorVersion = UInt16(majorPortion)
}

// get minor version
if protocolString.characters.count > decimalPosition {
let minorPortion : String = versionPortion.substring(from:
versionPortion.index(versionPortion.startIndex, offsetBy: decimalPosition + 1))
#if swift(>=3.2)
let minorPortion = String(versionPortion[versionPortion.index(versionPortion.startIndex, offsetBy: decimalPosition + 1)...])
#else
let minorPortion = versionPortion.substring(from:versionPortion.index(versionPortion.startIndex, offsetBy: decimalPosition + 1))
#endif

minorVersion = UInt16(minorPortion)
}

Expand All @@ -256,7 +270,7 @@ public class FastCGIServerRequest : ServerRequest {
httpVersionMajor = majorVersion!
httpVersionMinor = minorVersion!
}

}


Expand Down Expand Up @@ -486,8 +500,7 @@ public class FastCGIServerRequest : ServerRequest {
return
}

}
while true
} while true


} catch FastCGI.RecordErrors.invalidVersion {
Expand Down
19 changes: 6 additions & 13 deletions Tests/KituraNetTests/KituraNetTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,28 +31,21 @@ class KituraNetTest: XCTestCase {
var port = portDefault

static let sslConfig: SSLService.Configuration = {
let path = #file
let sslConfigDir: String
if let range = path.range(of: "/", options: .backwards) {
sslConfigDir = path.substring(to: range.lowerBound) + "/SSLConfig/"
} else {
sslConfigDir = "./SSLConfig/"
}
let sslConfigDir = URL(fileURLWithPath: #file).appendingPathComponent("../SSLConfig")

#if os(Linux)
let certificatePath = sslConfigDir + "certificate.pem"
let keyPath = sslConfigDir + "key.pem"
let certificatePath = sslConfigDir.appendingPathComponent("certificate.pem").standardized.path
let keyPath = sslConfigDir.appendingPathComponent("key.pem").standardized.path
return SSLService.Configuration(withCACertificateDirectory: nil, usingCertificateFile: certificatePath,
withKeyFile: keyPath, usingSelfSignedCerts: true, cipherSuite: nil)
#else
let chainFilePath = sslConfigDir + "certificateChain.pfx"
let chainFilePath = sslConfigDir.appendingPathComponent("certificateChain.pfx").standardized.path
return SSLService.Configuration(withChainFilePath: chainFilePath, withPassword: "kitura",
usingSelfSignedCerts: true, cipherSuite: nil)
#endif
}()

private static let initOnce: () = {
PrintLogger.use(colored: true)
}()
private static let initOnce: () = PrintLogger.use(colored: true)

func doSetUp() {
KituraNetTest.initOnce
Expand Down
7 changes: 6 additions & 1 deletion Tests/KituraNetTests/PrintLogger.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ public class PrintLogger: Logger {
guard let range = path.range(of: "/", options: .backwards) else {
return path
}
return path.substring(from: range.upperBound)

#if swift(>=3.2)
return String(path[range.upperBound...])
#else
return path.substring(from: range.upperBound)
#endif
}
}
5 changes: 4 additions & 1 deletion Tests/KituraNetTests/VerifyLinuxTestCount.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@
* limitations under the License.
**/

#if os(OSX)
// Test disabled on Swift 4 for now due to
// https://bugs.swift.org/browse/SR-5684

#if os(OSX) && !swift(>=3.2)
import XCTest

class VerifyLinuxTestCount: XCTestCase {
Expand Down

0 comments on commit 8e0c79c

Please sign in to comment.