Skip to content

Commit

Permalink
Updated to new version.
Browse files Browse the repository at this point in the history
  • Loading branch information
dfirsht committed Apr 11, 2016
1 parent c73f3d3 commit 6ac5cfc
Show file tree
Hide file tree
Showing 13 changed files with 273 additions and 120 deletions.
33 changes: 33 additions & 0 deletions .github/CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Contributing to Kitura

We welcome contributions, and request you follow these guidelines.

- [Raising issues](#raising-issues)
- [Contributor License Agreement](#contributor-license-agreement)
- [Coding Standards](#coding-standards)


## Raising issues

Please raise any bug reports on the issue tracker. Be sure to
search the list to see if your issue has already been raised.

A good bug report is one that make it easy for us to understand what you were
trying to do and what went wrong. Provide as much context as possible so we can try to recreate the issue.

### Contributor License Agreement

In order for us to accept pull-requests, the contributor must first complete
a Contributor License Agreement (CLA). Please see our [CLA repo](http://github.com/IBM-Swift/CLA) for more information.

This clarifies the intellectual property license granted with any contribution. It is for your protection as a
Contributor as well as the protection of IBM and its customers; it does not
change your rights to use your own Contributions for any other purpose.

### Coding standards

Please ensure you follow the coding standards used throughout the existing
code base. Some basic rules include:

- all files must have the Apache license in the header.
- all PRs must have passing builds for all operating systems.
20 changes: 20 additions & 0 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!--- Provide a general summary of your changes in the Title above -->

## Description
<!--- Describe your changes in detail -->

## Motivation and Context
<!--- Why is this change required? What problem does it solve? -->
<!--- If it fixes an open issue, please link to the issue here. -->

## How Has This Been Tested?
<!--- Please describe in detail how you tested your changes. -->
<!--- Include details of your testing environment, and the tests you ran to -->
<!--- see how your change affects other areas of the code, etc. -->

## Checklist:
<!--- Go over all the following points, and put an `x` in all the boxes that apply. -->
<!--- If you're unsure about any of these, don't hesitate to ask. We're here to help! -->
- [ ] I have submitted a [CLA form](https://github.com/IBM-Swift/CLA)
- [ ] If applicable, I have updated the documentation accordingly.
- [ ] If applicable, I have added tests to cover my changes.
4 changes: 2 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import PackageDescription
let package = Package(
name: "Kitura-net",
dependencies: [
.Package(url: "https://github.com/IBM-Swift/Kitura-sys.git", majorVersion: 0, minor: 6),
.Package(url: "https://github.com/IBM-Swift/BlueSocket.git", majorVersion: 0, minor: 2),
.Package(url: "https://github.com/IBM-Swift/Kitura-sys.git", majorVersion: 0, minor: 8),
.Package(url: "https://github.com/IBM-Swift/BlueSocket.git", majorVersion: 0, minor: 3),
.Package(url: "https://github.com/IBM-Swift/LoggerAPI.git", majorVersion: 0, minor: 4),
.Package(url: "https://github.com/IBM-Swift/CCurl.git", majorVersion: 0, minor: 0),
.Package(url: "https://github.com/IBM-Swift/CHttpParser.git", majorVersion: 0, minor: 0),
Expand Down
48 changes: 25 additions & 23 deletions Sources/KituraNet/ClientRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@

import KituraSys
import CCurl
import BlueSocket
import Socket

import Foundation

// MARK: ClientRequest

public class ClientRequest: BlueSocketWriter {
public class ClientRequest: SocketWriter {

///
/// Internal lock to the request
Expand Down Expand Up @@ -106,17 +106,17 @@ public class ClientRequest: BlueSocketWriter {
/// - Returns: a ClientRequest instance
///
init(options: [ClientRequestOptions], callback: ClientRequestCallback) {

self.callback = callback

var theSchema = "http://"
var hostName = "localhost"
var path = "/"
var port:Int16 = 80
var port: Int16? = nil

for option in options {
switch(option) {

case .Method(let method):
self.method = method
case .Schema(let schema):
Expand Down Expand Up @@ -145,40 +145,42 @@ public class ClientRequest: BlueSocketWriter {
let pwd = self.password ?? ""
var authenticationClause = ""
if (!user.isEmpty && !pwd.isEmpty) {

authenticationClause = "\(user):\(pwd)@"

}

let portNumber = String(port)
url = "\(theSchema)\(authenticationClause)\(hostName):\(portNumber)\(path)"


var portClause = ""
if let port = port {
portClause = ":\(String(port))"
}

url = "\(theSchema)\(authenticationClause)\(hostName)\(portClause)\(path)"

}

///
/// Instance destruction
///
deinit {

if let handle = handle {
curl_easy_cleanup(handle)
}

if headersList != nil {
curl_slist_free_all(headersList)
}

}

///
/// Writes a string to the response
///
/// - Parameter str: String to be written
///
public func writeString(str: String) {
public func write(from string: String) {

if let data = StringUtils.toUtf8String(str) {
writeData(data)
if let data = StringUtils.toUtf8String(string) {
write(from: data)
}

}
Expand All @@ -188,7 +190,7 @@ public class ClientRequest: BlueSocketWriter {
///
/// - Parameter data: NSData to be written
///
public func writeData(data: NSData) {
public func write(from data: NSData) {

writeBuffers.appendData(data)

Expand All @@ -201,7 +203,7 @@ public class ClientRequest: BlueSocketWriter {
///
public func end(data: String) {

writeString(data)
write(from: data)
end()

}
Expand All @@ -213,7 +215,7 @@ public class ClientRequest: BlueSocketWriter {
///
public func end(data: NSData) {

writeData(data)
write(from: data)
end()

}
Expand Down Expand Up @@ -294,7 +296,7 @@ public class ClientRequest: BlueSocketWriter {
///
private func setMethod() {

let methodUpperCase = method.uppercaseString
let methodUpperCase = method.uppercased()
switch(methodUpperCase) {
case "GET":
curlHelperSetOptBool(handle!, CURLOPT_HTTPGET, CURL_TRUE)
Expand Down Expand Up @@ -450,13 +452,13 @@ private class CurlInvoker {
curlHelperSetOptReadFunc(handle, ptr) { (buf: UnsafeMutablePointer<Int8>, size: Int, nMemb: Int, privateData: UnsafeMutablePointer<Void>) -> Int in

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

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

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

Expand Down
10 changes: 10 additions & 0 deletions Sources/KituraNet/Http.swift
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,11 @@ public class Http {
///
/// A set of characters that are valid in requests
///
#if os(Linux)
private static let allowedCharacterSet = NSCharacterSet(charactersInString:"\"#%/<>?@\\^`{|}").invertedSet
#else
private static let allowedCharacterSet = NSCharacterSet(charactersIn:"\"#%/<>?@\\^`{|}").inverted
#endif

///
/// Transform the URL into escaped characters
Expand All @@ -112,9 +116,15 @@ public class Http {
///
public static func escapeUrl(url: String) -> String {

#if os(Linux)
if let escaped = url.bridge().stringByAddingPercentEncodingWithAllowedCharacters(allowedCharacterSet) {
return escaped
}
#else
if let escaped = url.addingPercentEncoding(withAllowedCharacters: allowedCharacterSet) {
return escaped
}
#endif

return url
}
Expand Down
36 changes: 18 additions & 18 deletions Sources/KituraNet/HttpParser/HttpParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,65 +66,65 @@ class HttpParser {
settings = http_parser_settings()

settings.on_url = { (parser, chunk, length) -> Int32 in
let p = UnsafePointer<HttpParserDelegate?>(parser.memory.data)
let p = UnsafePointer<HttpParserDelegate?>(parser.pointee.data)
let data = NSData(bytes: chunk, length: length)
p.memory?.onUrl(data)
p.pointee?.onUrl(data)
return 0
}

settings.on_header_field = { (parser, chunk, length) -> Int32 in
let data = NSData(bytes: chunk, length: length)
let p = UnsafePointer<HttpParserDelegate?>(parser.memory.data)
p.memory?.onHeaderField(data)
let p = UnsafePointer<HttpParserDelegate?>(parser.pointee.data)
p.pointee?.onHeaderField(data)
return 0
}

settings.on_header_value = { (parser, chunk, length) -> Int32 in
let data = NSData(bytes: chunk, length: length)
let p = UnsafePointer<HttpParserDelegate?>(parser.memory.data)
p.memory?.onHeaderValue(data)
let p = UnsafePointer<HttpParserDelegate?>(parser.pointee.data)
p.pointee?.onHeaderValue(data)
return 0
}

settings.on_body = { (parser, chunk, length) -> Int32 in
let p = UnsafePointer<HttpParserDelegate?>(parser.memory.data)
let p = UnsafePointer<HttpParserDelegate?>(parser.pointee.data)
let data = NSData(bytes: chunk, length: length)
p.memory?.onBody(data)
p.pointee?.onBody(data)

return 0
}

settings.on_headers_complete = { (parser) -> Int32 in
let p = UnsafePointer<HttpParserDelegate?>(parser.memory.data)
let p = UnsafePointer<HttpParserDelegate?>(parser.pointee.data)
// TODO: Clean and refactor
//let method = String( get_method(parser))
let po = get_method(parser)
var message = ""
var i = 0
while((po+i).memory != Int8(0)) {
message += String(UnicodeScalar(UInt8((po+i).memory)))
while((po+i).pointee != Int8(0)) {
message += String(UnicodeScalar(UInt8((po+i).pointee)))
i += 1
}
p.memory?.onHeadersComplete(message, versionMajor: parser.memory.http_major,
versionMinor: parser.memory.http_minor)
p.pointee?.onHeadersComplete(message, versionMajor: parser.pointee.http_major,
versionMinor: parser.pointee.http_minor)

return 0
}

settings.on_message_begin = { (parser) -> Int32 in
let p = UnsafePointer<HttpParserDelegate?>(parser.memory.data)
p.memory?.onMessageBegin()
let p = UnsafePointer<HttpParserDelegate?>(parser.pointee.data)
p.pointee?.onMessageBegin()

return 0
}

settings.on_message_complete = { (parser) -> Int32 in
let p = UnsafePointer<HttpParserDelegate?>(parser.memory.data)
let p = UnsafePointer<HttpParserDelegate?>(parser.pointee.data)
if get_status_code(parser) == 100 {
p.memory?.reset()
p.pointee?.reset()
}
else {
p.memory?.onMessageComplete()
p.pointee?.onMessageComplete()
}

return 0
Expand Down
8 changes: 8 additions & 0 deletions Sources/KituraNet/HttpParser/UrlParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,18 @@ public class UrlParser : CustomStringConvertible {

if let query = query {

#if os(Linux)
let pairs = query.bridge().componentsSeparatedByString("&")
#else
let pairs = query.componentsSeparated(by: "&")
#endif
for pair in pairs {

#if os(Linux)
let pairArr = pair.bridge().componentsSeparatedByString("=")
#else
let pairArr = pair.componentsSeparated(by: "=")
#endif
if pairArr.count == 2 {
queryParams[pairArr[0]] = pairArr[1]
}
Expand Down
10 changes: 5 additions & 5 deletions Sources/KituraNet/HttpServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
**/

import KituraSys
import BlueSocket
import Socket

// MARK: HttpServer

Expand Down Expand Up @@ -52,7 +52,7 @@ public class HttpServer {
///
/// TCP socket used for listening for new connections
///
private var listenSocket: BlueSocket?
private var listenSocket: Socket?

///
/// Initializes an HttpServer instance
Expand All @@ -78,9 +78,9 @@ public class HttpServer {

do {

self.listenSocket = try BlueSocket.defaultConfigured()
self.listenSocket = try Socket.makeDefault()

} catch let error as BlueSocket.Error {
} catch let error as Socket.Error {
print("Error reported:\n \(error.description)")
} catch {
print("Unexpected error...")
Expand Down Expand Up @@ -139,7 +139,7 @@ extension HttpServer : HttpServerSpiDelegate {
///
/// - Parameter clientSocket: the socket used for connecting
///
func handleClientRequest(clientSocket: BlueSocket) {
func handleClientRequest(clientSocket: Socket) {

guard let delegate = delegate else {
return
Expand Down
Loading

0 comments on commit 6ac5cfc

Please sign in to comment.