Converted to using Data instead of [UInt8]

This commit is contained in:
Tyler Cloutier 2017-11-20 14:59:03 -08:00
parent 5f4cec1c6b
commit dbf5ad750a
15 changed files with 79 additions and 79 deletions

View File

@ -43,7 +43,7 @@ public final class ClientConnection {
}
}
public func write(_ response: Response) -> Source<[UInt8], SystemError> {
public func write(_ response: Response) -> Source<Data, SystemError> {
return socket.write(buffer: response.serialized)
}

View File

@ -13,7 +13,7 @@ public protocol HTTPMessage {
var rawHeaders: [String] { get set }
var headers: [String:String] { get }
var cookies: [String] { get }
var body: [UInt8] { get set }
var body: Data { get set }
}
public extension HTTPMessage {

View File

@ -63,7 +63,7 @@ public class RequestParser {
}
}
public func parse(_ data: [UInt8]) throws {
public func parse(_ data: Data) throws {
try parser.parse(data)
}
@ -87,7 +87,7 @@ public class ResponseParser {
}
}
public func parse(_ data: [UInt8]) throws {
public func parse(_ data: Data) throws {
try parser.parse(data)
}
@ -99,7 +99,7 @@ private final class FullMessageParser {
var version: (major: Int, minor: Int) = (0, 0)
var rawHeaders: [String] = []
var body: [UInt8] = []
var body: Data = Data()
// Response
var statusCode: Int! = nil
@ -121,7 +121,7 @@ private final class FullMessageParser {
self.rawParser.delegate = self
}
func parse(_ data: [UInt8]) throws {
func parse(_ data: Data) throws {
try rawParser.parse(data)
}
@ -254,9 +254,8 @@ public final class RawParser {
parserPointer.pointee.data = bridge(self)
}
public func parse(_ data: [UInt8]) throws {
try UnsafePointer(data).withMemoryRebound(to: Int8.self,
capacity: data.count) { convertedPointer in
public func parse(_ data: Data) throws {
try data.withUnsafeBytes { (convertedPointer: UnsafePointer<Int8>) in
let bytesParsed = http_parser_execute(
parserPointer,
&requestSettings,

View File

@ -13,10 +13,10 @@ public struct Request: Serializable, HTTPMessage {
public var uri: URL
public var version: Version
public var rawHeaders: [String]
public var body: [UInt8]
public var body: Data
public var storage: [String: Any]
public var serialized: [UInt8] {
public var serialized: Data {
var headerString = ""
headerString += "\(method) \(uri.absoluteString) HTTP/\(version.major).\(version.minor)"
headerString += "\r\n"
@ -41,7 +41,7 @@ public struct Request: Serializable, HTTPMessage {
uri: URL,
version: Version = Version(major: 1, minor: 1),
rawHeaders: [String] = [],
body: [UInt8] = []
body: Data = Data()
) {
self.method = method
self.uri = uri

View File

@ -13,10 +13,10 @@ public struct Response: Serializable, HTTPMessage {
public var version: Version
public var status: Status
public var rawHeaders: [String]
public var body: [UInt8]
public var body: Data
public var storage: [String: Any] = [:]
public var serialized: [UInt8] {
public var serialized: Data {
var headerString = ""
headerString += "HTTP/\(version.major).\(version.minor)"
headerString += " \(status.code) \(status.reasonPhrase)"
@ -41,7 +41,7 @@ public struct Response: Serializable, HTTPMessage {
version: Version = Version(major: 1, minor: 1),
status: Status,
rawHeaders: [String],
body: [UInt8] = []
body: Data = Data()
) {
self.version = version
self.status = status
@ -65,7 +65,7 @@ public struct Response: Serializable, HTTPMessage {
rawHeaders: [String] = [],
json: Any
) throws {
let body = Array(try JSONSerialization.data(withJSONObject: json))
let body = try JSONSerialization.data(withJSONObject: json)
let rawHeaders = Array([
rawHeaders,
[

View File

@ -5,7 +5,8 @@
// Created by Tyler Fleming Cloutier on 7/1/16.
//
//
import Foundation
public protocol Serializable {
var serialized: [UInt8] { get }
var serialized: Data { get }
}

View File

@ -5,7 +5,7 @@
// Created by Tyler Fleming Cloutier on 5/1/16.
//
//
import Foundation
import Dispatch
import StreamKit
import POSIX
@ -27,13 +27,13 @@ public protocol WritableIOStream: class {
var channel: DispatchIO { get }
func write(buffer: [UInt8]) -> Source<[UInt8], SystemError>
func write(buffer: Data) -> Source<Data, SystemError>
}
public extension WritableIOStream {
func write(buffer: [UInt8]) -> Source<[UInt8], SystemError> {
func write(buffer: Data) -> Source<Data, SystemError> {
return Source { observer in
let writeChannel = DispatchIO(
type: .stream,
@ -45,15 +45,15 @@ public extension WritableIOStream {
}
}
buffer.withUnsafeBufferPointer { buffer in
// Allocate dispatch data
// TODO: This does not seem right.
// Work around crash for now.
let dispatchData = DispatchData(
bytesNoCopy: UnsafeRawBufferPointer(buffer),
let dispatchData = buffer.withUnsafeBytes {
return DispatchData(
bytesNoCopy: UnsafeRawBufferPointer(start: $0, count: buffer.count),
deallocator: .custom(nil, { })
)
}
// Schedule write operation
writeChannel.write(
@ -70,7 +70,7 @@ public extension WritableIOStream {
if let data = data, !data.isEmpty {
// Get unwritten data
data.enumerateBytes { (buffer, byteIndex, stop) in
observer.sendNext(Array(buffer))
observer.sendNext(Data(buffer))
}
}
@ -88,7 +88,7 @@ public extension WritableIOStream {
}
}
}
}
return ActionDisposable {
writeChannel.close()
}
@ -102,13 +102,13 @@ public protocol ReadableIOStream: class {
var channel: DispatchIO { get }
func read(minBytes: Int) -> Source<[UInt8], SystemError>
func read(minBytes: Int) -> Source<Data, SystemError>
}
public extension ReadableIOStream {
func read(minBytes: Int = 1) -> Source<[UInt8], SystemError> {
func read(minBytes: Int = 1) -> Source<Data, SystemError> {
return Source { observer in
@ -133,7 +133,7 @@ public extension ReadableIOStream {
// Deliver data if it is non-empty
if let data = data, !data.isEmpty {
data.enumerateBytes { (buffer, byteIndex, stop) in
observer.sendNext(Array(buffer))
observer.sendNext(Data(buffer))
}
}

View File

@ -20,7 +20,7 @@ class HTTPMessageTests: XCTestCase {
key == "set-cookie"
}.map { $0.1 }
}
var body: [UInt8] = []
var body: Data = Data()
}
func testHeaders() {

View File

@ -8,7 +8,7 @@ class RequestParserTests: XCTestCase {
let parser = RequestParser()
let data = ("INVALID / HTTP/1.1\r\n" + "\r\n")
do {
try parser.parse(Array(data.utf8))
try parser.parse(Data(data.utf8))
} catch {
expectParseError.fulfill()
}
@ -63,7 +63,7 @@ class RequestParserTests: XCTestCase {
XCTAssert(request.rawHeaders.count == 0)
}
do {
try parser.parse(Array(data.utf8))
try parser.parse(Data(data.utf8))
} catch {
XCTFail("Parsing error \(error) for method \(method)")
}
@ -92,7 +92,7 @@ class RequestParserTests: XCTestCase {
]
do {
for data in dataArray {
try parser.parse(Array(data.utf8))
try parser.parse(Data(data.utf8))
}
} catch {
XCTFail("Parsing error \(error).")
@ -115,7 +115,7 @@ class RequestParserTests: XCTestCase {
XCTAssert(request.rawHeaders[1] == "swift.org")
}
do {
try parser.parse(Array(data.utf8))
try parser.parse(Data(data.utf8))
} catch {
XCTFail("Parsing error \(error).")
}
@ -147,7 +147,7 @@ class RequestParserTests: XCTestCase {
]
do {
for data in dataArray {
try parser.parse(Array(data.utf8))
try parser.parse(Data(data.utf8))
}
} catch {
XCTFail("Parsing error \(error).")
@ -187,7 +187,7 @@ class RequestParserTests: XCTestCase {
]
do {
for data in dataArray {
try parser.parse(Array(data.utf8))
try parser.parse(Data(data.utf8))
}
} catch {
XCTFail("Parsing error \(error).")
@ -211,7 +211,7 @@ class RequestParserTests: XCTestCase {
"\r\n" +
"Swift")
do {
try parser.parse(Array(data.utf8))
try parser.parse(Data(data.utf8))
} catch {
XCTFail("Parsing error \(error).")
}
@ -244,7 +244,7 @@ class RequestParserTests: XCTestCase {
]
do {
for data in dataArray {
try parser.parse(Array(data.utf8))
try parser.parse(Data(data.utf8))
}
} catch {
XCTFail("Parsing error \(error).")
@ -282,7 +282,7 @@ class RequestParserTests: XCTestCase {
]
do {
for data in dataArray {
try parser.parse(Array(data.utf8))
try parser.parse(Data(data.utf8))
}
} catch {
XCTFail("Parsing error \(error).")
@ -310,7 +310,7 @@ class RequestParserTests: XCTestCase {
}
let data = "GET / HTTP/1.1\r\n\r\nHEAD /profile HTTP/1.1\r\n\r\n"
do {
try parser.parse(Array(data.utf8))
try parser.parse(Data(data.utf8))
} catch {
XCTFail("Parsing error \(error).")
}
@ -336,7 +336,7 @@ class RequestParserTests: XCTestCase {
}
for _ in 0 ..< messageNumber {
do {
try parser.parse(Array(data.utf8))
try parser.parse(Data(data.utf8))
} catch {
XCTFail("Parsing error \(error).")
}

View File

@ -19,7 +19,7 @@ class RequestSerializationTests: XCTestCase {
uri: URL(string: "/")!,
version: Version(major: 1, minor: 1),
rawHeaders: [],
body: []
body: Data()
)
let actual = String(bytes: request.serialized, encoding: .utf8)!
XCTAssert(expected == actual, "Actual request, \(actual), did not match expected.")
@ -33,7 +33,7 @@ class RequestSerializationTests: XCTestCase {
uri: URL(string: "/")!,
version: Version(major: 1, minor: 1),
rawHeaders: ["Accept", "*/*", "Host", "www.google.com", "Connection", "Keep-Alive"],
body: []
body: Data()
)
let actual = String(bytes: request.serialized, encoding: .utf8)!
XCTAssert(expected == actual, "Actual request, \(actual), did not match expected.")

View File

@ -79,7 +79,7 @@ class ResponseParserTests: XCTestCase {
let parser = ResponseParser()
let data = ("FTP/1.1 200 OK\r\n\r\n")
XCTAssertThrowsError(
try parser.parse(Array(data.utf8)),
try parser.parse(Data(data.utf8)),
"Invalid request did not throw an error."
)
}
@ -102,7 +102,7 @@ class ResponseParserTests: XCTestCase {
XCTAssert(response.headers.count == (contentLength == "" ? 0 : 1))
}
do {
try parser.parse(Array(data.utf8))
try parser.parse(Data(data.utf8))
} catch {
XCTFail("Parsing error \(error) for \(data)")
}

View File

@ -18,7 +18,7 @@ class ResponseSerializationTests: XCTestCase {
version: Version(major: 1, minor: 1),
status: .ok,
rawHeaders: [],
body: []
body: Data()
)
let actual = String(bytes: response.serialized, encoding: .utf8)!
XCTAssert(expected == actual, "Actual response, \(actual), did not match expected.")
@ -37,7 +37,7 @@ class ResponseSerializationTests: XCTestCase {
"Date", "Sun, 30 Oct 2016 09:06:40 GMT",
"Content-Type", "text/html; charset=ISO-8859-1"
],
body: []
body: Data()
)
let actual = String(bytes: response.serialized, encoding: .utf8)!
XCTAssert(expected == actual, "Actual request, \(actual), did not match expected.")

View File

@ -10,7 +10,7 @@ class PipeTests: XCTestCase {
let stdout = Pipe(fd: .stdout)
// Write to stdout
let outStream = stdout.write(buffer: Array("Send it in!\n".utf8))
let outStream = stdout.write(buffer: Data("Send it in!\n".utf8))
outStream.onFailed { err in
XCTFail(String(describing: err))
}

View File

@ -141,7 +141,7 @@ class RouterTests: XCTestCase {
uri: request.uri,
version: request.version,
rawHeaders: request.rawHeaders,
body: Array("Hehe, changin' the body.".utf8)
body: Data("Hehe, changin' the body.".utf8)
)
}

View File

@ -44,7 +44,7 @@ class ConnectionTests: XCTestCase {
let socket = try Socket()
let connect = socket.connect(host: "localhost", port: 50000)
connect.onCompleted {
let buffer = Array("This is a test".utf8)
let buffer = Data("This is a test".utf8)
let write = socket.write(buffer: buffer)
write.onCompleted {
completeWriteExpectation.fulfill()