Skip to content

Commit

Permalink
Migration 0823 (Kitura#84)
Browse files Browse the repository at this point in the history
* Kitura/Kitura#675 Test module name changed

* Kitura/Kitura#675 Updated BlueSocket dependency

* Kitura/Kitura#675 Various changes to get code to compile on macOS

* Kitura/Kitura#675 Migration from KituraSys to Dispatch

* Kitura/Kitura#675 Refactoring and changes to compile of macOS

* Kitura/Kitura#675 Dispatch changes on Linux

* Kitura/Kitura#675 More Dispatch changes for Linux

* IBM-SwiftKitura#675 More Dispatch changes on Linux

* Kitura/Kitura#675 Even more Dispatch changes for Linux

* Kitura/Kitura#675 Changes to get tests to compile of macOS

* Kitura/Kitura#675 Updated swift version

* Kitura/Kitura#675 Fix test compilation on Linux

* Kitura/Kitura#675 Updated test module name

* Kitura/Kitura#675 Work around for SPM bug

* Kitura/Kitura#675 Made HTTPParserErrorType a CustomStringConvertible

* Kitura/Kitura#675 Added printing of the parsing error cause to the message

* Use 0.27 of Kitura-sys

* Migrate macOS to 0818

* Migrate Linux to 0818

* Kitura/Kitura#697 Updated Swift version
  • Loading branch information
shmuelk authored and Sergey Minakov committed Aug 30, 2016
1 parent cea87f6 commit 1210521
Show file tree
Hide file tree
Showing 30 changed files with 161 additions and 255 deletions.
2 changes: 1 addition & 1 deletion .swift-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
DEVELOPMENT-SNAPSHOT-2016-07-25-a
DEVELOPMENT-SNAPSHOT-2016-08-23-a
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ let package = Package(
name: "Kitura-net",
dependencies: [
.Package(url: "https://github.com/IBM-Swift/Kitura-sys.git", majorVersion: 0, minor: 27),
.Package(url: "https://github.com/IBM-Swift/BlueSocket.git", majorVersion: 0, minor: 7),
.Package(url: "https://github.com/IBM-Swift/BlueSocket.git", majorVersion: 0, minor: 10),
.Package(url: "https://github.com/IBM-Swift/CCurl.git", majorVersion: 0, minor: 2),
.Package(url: "https://github.com/IBM-Swift/CHTTPParser.git", majorVersion: 0, minor: 3),
]
Expand Down
8 changes: 2 additions & 6 deletions Sources/KituraNet/BufferList.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,7 @@ public class BufferList {
///
/// Internal storage buffer
///
#if os(Linux)
private var localData = Data(capacity: 4096)!
#else
private var localData = Data(capacity: 4096)
#endif
private var localData = Data(capacity: 4096)

///
/// Byte offset inside of internal storage buffer
Expand Down Expand Up @@ -92,7 +88,7 @@ public class BufferList {
public func fill(array: inout [UInt8]) -> Int {

let result = min(array.count, localData.count-byteIndex)
localData.copyBytes(to: UnsafeMutablePointer<UInt8>(array), from: byteIndex..<byteIndex+result)
localData.copyBytes(to: UnsafeMutablePointer<UInt8>(mutating: array), from: byteIndex..<byteIndex+result)
byteIndex += result

return result
Expand Down
79 changes: 34 additions & 45 deletions Sources/KituraNet/ClientRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,16 @@ public class ClientRequest {
public private(set) var closeConnection = false

/// Handle for working with libCurl
private var handle: UnsafeMutablePointer<Void>?
private var handle: UnsafeMutableRawPointer?

/// List of header information
private var headersList: UnsafeMutablePointer<curl_slist>?

/// BufferList to store bytes to be written
private var writeBuffers = BufferList()
fileprivate var writeBuffers = BufferList()

/// Response instance for communicating with client
private var response = ClientResponse()
fileprivate var response = ClientResponse()

/// The callback to receive the response
private var callback: Callback
Expand All @@ -77,15 +77,15 @@ public class ClientRequest {
}

/// Response callback closure type
public typealias Callback = (response: ClientResponse?) -> Void
public typealias Callback = (ClientResponse?) -> Void

/// Initializes a ClientRequest instance
///
/// - Parameter url: url for the request
/// - Parameter callback:
///
/// - Returns: a ClientRequest instance
init(url: String, callback: Callback) {
init(url: String, callback: @escaping Callback) {

self.url = url
self.callback = callback
Expand All @@ -98,7 +98,7 @@ public class ClientRequest {
/// - Parameter callback:
///
/// - Returns: a ClientRequest instance
init(options: [Options], callback: Callback) {
init(options: [Options], callback: @escaping Callback) {

self.callback = callback

Expand Down Expand Up @@ -196,24 +196,13 @@ public class ClientRequest {
if let host = url.host {
options.append(.hostname(host))
}
#if os(Linux)
if var fullPath = url.path {
// query strings and parameters need to be appended here
if let query = url.query {
fullPath += "?"
fullPath += query
}
options.append(.path(fullPath))
}
#else
var fullPath = url.path
// query strings and parameters need to be appended here
if let query = url.query {
fullPath += "?"
fullPath += query
}
options.append(.path(fullPath))
#endif
var fullPath = url.path
// query strings and parameters need to be appended here
if let query = url.query {
fullPath += "?"
fullPath += query
}
options.append(.path(fullPath))
if let port = url.port {
options.append(.port(Int16(port)))
}
Expand Down Expand Up @@ -289,7 +278,7 @@ public class ClientRequest {
closeConnection = close

guard let urlBuffer = StringUtils.toNullTerminatedUtf8String(url) else {
callback(response: nil)
callback(nil)
return
}

Expand All @@ -301,25 +290,25 @@ public class ClientRequest {
var code = invoker.invoke()
guard code == CURLE_OK else {
Log.error("ClientRequest Error, Failed to invoke HTTP request. CURL Return code=\(code)")
callback(response: nil)
callback(nil)
return
}

code = curlHelperGetInfoLong(handle!, CURLINFO_RESPONSE_CODE, &response.status)
guard code == CURLE_OK else {
Log.error("ClientRequest Error. Failed to get response code. CURL Return code=\(code)")
callback(response: nil)
callback(nil)
return
}

let parseStatus = response.parse()
guard parseStatus.error == nil else {
Log.error("ClientRequest error. Failed to parse response. status=\(parseStatus.error!)")
callback(response: nil)
callback(nil)
return
}

self.callback(response: self.response)
self.callback(self.response)
}

/// Prepare the handle
Expand Down Expand Up @@ -390,23 +379,23 @@ public class ClientRequest {
extension ClientRequest: CurlInvokerDelegate {

/// libCurl callback to recieve data sent by the server
private func curlWriteCallback(_ buf: UnsafeMutablePointer<Int8>, size: Int) -> Int {
fileprivate func curlWriteCallback(_ buf: UnsafeMutablePointer<Int8>, size: Int) -> Int {

response.responseBuffers.append(bytes: UnsafePointer<UInt8>(buf), length: size)
response.responseBuffers.append(bytes: UnsafeRawPointer(buf).assumingMemoryBound(to: UInt8.self), length: size)
return size

}

/// libCurl callback to provide the data to send to the server
private func curlReadCallback(_ buf: UnsafeMutablePointer<Int8>, size: Int) -> Int {
fileprivate func curlReadCallback(_ buf: UnsafeMutablePointer<Int8>, size: Int) -> Int {

let count = writeBuffers.fill(buffer: UnsafeMutablePointer<UInt8>(buf), length: size)
let count = writeBuffers.fill(buffer: UnsafeMutableRawPointer(buf).assumingMemoryBound(to: UInt8.self), length: size)
return count

}

/// libCurl callback invoked when a redirect is about to be done
private func prepareForRedirect() {
fileprivate func prepareForRedirect() {

response.responseBuffers.reset()
writeBuffers.rewind()
Expand All @@ -418,16 +407,16 @@ extension ClientRequest: CurlInvokerDelegate {
private class CurlInvoker {

/// Pointer to the libCurl handle
private var handle: UnsafeMutablePointer<Void>
private var handle: UnsafeMutableRawPointer

/// Delegate that can have a read or write callback
private weak var delegate: CurlInvokerDelegate?
fileprivate weak var delegate: CurlInvokerDelegate?

/// Maximum number of redirects
private let maxRedirects: Int

/// Initializes a new CurlInvoker instance
private init(handle: UnsafeMutablePointer<Void>, maxRedirects: Int) {
fileprivate init(handle: UnsafeMutableRawPointer, maxRedirects: Int) {

self.handle = handle
self.maxRedirects = maxRedirects
Expand All @@ -437,14 +426,14 @@ private class CurlInvoker {
/// Run the HTTP method through the libCurl library
///
/// - Returns: a status code for the success of the operation
private func invoke() -> CURLcode {
fileprivate func invoke() -> CURLcode {

var rc: CURLcode = CURLE_FAILED_INIT
if delegate == nil {
return rc
}

withUnsafeMutablePointer(&delegate) {ptr in
withUnsafeMutablePointer(to: &delegate) {ptr in
self.prepareHandle(ptr)

var redirected = false
Expand Down Expand Up @@ -479,16 +468,16 @@ private class CurlInvoker {
/// - Parameter ptr: pointer to the CurlInvokerDelegat
private func prepareHandle(_ ptr: UnsafeMutablePointer<CurlInvokerDelegate?>) {

curlHelperSetOptReadFunc(handle, ptr) { (buf: UnsafeMutablePointer<Int8>?, size: Int, nMemb: Int, privateData: UnsafeMutablePointer<Void>?) -> Int in
curlHelperSetOptReadFunc(handle, ptr) { (buf: UnsafeMutablePointer<Int8>?, size: Int, nMemb: Int, privateData: UnsafeMutableRawPointer?) -> Int in

let p = UnsafePointer<CurlInvokerDelegate?>(privateData)
return (p?.pointee?.curlReadCallback(buf!, size: size*nMemb))!
let p = privateData?.assumingMemoryBound(to: CurlInvokerDelegate.self).pointee
return (p?.curlReadCallback(buf!, size: size*nMemb))!
}

curlHelperSetOptWriteFunc(handle, ptr) { (buf: UnsafeMutablePointer<Int8>?, size: Int, nMemb: Int, privateData: UnsafeMutablePointer<Void>?) -> Int in
curlHelperSetOptWriteFunc(handle, ptr) { (buf: UnsafeMutablePointer<Int8>?, size: Int, nMemb: Int, privateData: UnsafeMutableRawPointer?) -> Int in

let p = UnsafePointer<CurlInvokerDelegate?>(privateData)
return (p?.pointee?.curlWriteCallback(buf!, size: size*nMemb))!
let p = privateData?.assumingMemoryBound(to: CurlInvokerDelegate.self).pointee
return (p?.curlWriteCallback(buf!, size: size*nMemb))!
}
}

Expand Down
4 changes: 2 additions & 2 deletions Sources/KituraNet/FastCGI/FastCGIRecordCreate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ class FastCGIRecordCreate {
}

// A helper method to append various types to a Data object
private func appendBytes(to: inout Data, bytes: UnsafePointer<Void>, count: Int) {
to.append(UnsafePointer<UInt8>(bytes), count: count)
private func appendBytes(to: inout Data, bytes: UnsafeRawPointer, count: Int) {
to.append(UnsafeRawPointer(bytes).assumingMemoryBound(to: UInt8.self), count: count)
}

//
Expand Down
38 changes: 21 additions & 17 deletions Sources/KituraNet/FastCGI/FastCGIRecordParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class FastCGIRecordParser {
var requestId : UInt16 = 0
var role : UInt16 = 0
var flags : UInt8 = 0
var headers : [Dictionary] = Array<Dictionary<String,String>>()
var headers : Array<Dictionary<String,String>> = Array<Dictionary<String,String>>()
var data : Data? = nil
var appStatus : UInt32 = 0
var protocolStatus : UInt8 = 0
Expand Down Expand Up @@ -88,23 +88,27 @@ class FastCGIRecordParser {
// Helper to turn UInt16 from network byte order to local byte order,
// which is typically Big to Little.
//
private static func getLocalByteOrderSmall(from networkOrderedBytes: UInt16) -> UInt16 {
private static func getLocalByteOrderSmall(from networkOrderedBytes: [UInt8]) -> UInt16 {
let networkOrderedUInt16 = UnsafeRawPointer(networkOrderedBytes).assumingMemoryBound(to: UInt16.self)[0]

#if os(Linux)
return Glibc.ntohs(networkOrderedBytes)
return Glibc.ntohs(networkOrderedUInt16)
#else
return CFSwapInt16BigToHost(networkOrderedBytes)
return CFSwapInt16BigToHost(networkOrderedUInt16)
#endif
}

//
// Helper to turn UInt32 from network byte order to local byte order,
// which is typically Big to Little.
//
private static func getLocalByteOrderLarge(from networkOrderedBytes: UInt32) -> UInt32 {
private static func getLocalByteOrderLarge(from networkOrderedBytes: [UInt8]) -> UInt32 {
let networkOrderedUInt32 = UnsafeRawPointer(networkOrderedBytes).assumingMemoryBound(to: UInt32.self)[0]

#if os(Linux)
return Glibc.ntohl(networkOrderedBytes)
return Glibc.ntohl(networkOrderedUInt32)
#else
return CFSwapInt32BigToHost(networkOrderedBytes)
return CFSwapInt32BigToHost(networkOrderedUInt32)
#endif
}

Expand Down Expand Up @@ -151,9 +155,9 @@ class FastCGIRecordParser {

let requestIdBytes1 = buffer[try advance()]
let requestIdBytes0 = buffer[try advance()]
let requestIdBytes : [UInt8] = [ requestIdBytes1, requestIdBytes0 ]
let requestIdBytes = [ requestIdBytes1, requestIdBytes0 ]

requestId = FastCGIRecordParser.getLocalByteOrderSmall(from: UnsafePointer<UInt16>(requestIdBytes).pointee)
requestId = FastCGIRecordParser.getLocalByteOrderSmall(from: requestIdBytes)

}

Expand All @@ -163,9 +167,9 @@ class FastCGIRecordParser {

let contentLengthBytes1 = buffer[try advance()]
let contentLengthBytes0 = buffer[try advance()]
let contentLengthBytes : [UInt8] = [ contentLengthBytes1, contentLengthBytes0 ]
let contentLengthBytes = [ contentLengthBytes1, contentLengthBytes0 ]

contentLength = FastCGIRecordParser.getLocalByteOrderSmall(from: UnsafePointer<UInt16>(contentLengthBytes).pointee)
contentLength = FastCGIRecordParser.getLocalByteOrderSmall(from: contentLengthBytes)

}

Expand All @@ -181,9 +185,9 @@ class FastCGIRecordParser {

let roleByte1 = buffer[try advance()]
let roleByte0 = buffer[try advance()]
let roleBytes : [UInt8] = [ roleByte1, roleByte0 ]
let roleBytes = [ roleByte1, roleByte0 ]

role = FastCGIRecordParser.getLocalByteOrderSmall(from: UnsafePointer<UInt16>(roleBytes).pointee)
role = FastCGIRecordParser.getLocalByteOrderSmall(from: roleBytes)
flags = buffer[try advance()]

guard role == FastCGI.Constants.FCGI_RESPONDER else {
Expand All @@ -200,9 +204,9 @@ class FastCGIRecordParser {
let appStatusByte2 = buffer[try advance()]
let appStatusByte1 = buffer[try advance()]
let appStatusByte0 = buffer[try advance()]
let appStatusBytes : [UInt8] = [ appStatusByte3, appStatusByte2, appStatusByte1, appStatusByte0 ]
let appStatusBytes = [ appStatusByte3, appStatusByte2, appStatusByte1, appStatusByte0 ]

appStatus = FastCGIRecordParser.getLocalByteOrderLarge(from: UnsafePointer<UInt32>(appStatusBytes).pointee)
appStatus = FastCGIRecordParser.getLocalByteOrderLarge(from: appStatusBytes)

}

Expand Down Expand Up @@ -265,9 +269,9 @@ class FastCGIRecordParser {
let lengthByteB2 : UInt8 = buffer[try advance()]
let lengthByteB1 : UInt8 = buffer[try advance()]
let lengthByteB0 : UInt8 = buffer[try advance()]
let lengthBytes : [UInt8] = [ lengthByteB3 & 0x7f, lengthByteB2, lengthByteB1, lengthByteB0 ]
let lengthBytes = [ lengthByteB3 & 0x7f, lengthByteB2, lengthByteB1, lengthByteB0 ]

return Int(FastCGIRecordParser.getLocalByteOrderLarge(from: UnsafePointer<UInt32>(lengthBytes).pointee))
return Int(FastCGIRecordParser.getLocalByteOrderLarge(from: lengthBytes))
}

}
Expand Down
Loading

0 comments on commit 1210521

Please sign in to comment.