Skip to content

Commit

Permalink
Refactoring (#12)
Browse files Browse the repository at this point in the history
* Inline Data extension

* Rename withMemory => withPointee

* Remove extra pointer, use guard for early return

* Remove deinitialize
  • Loading branch information
antonmes committed May 26, 2016
1 parent c3ed415 commit 422e8e2
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 41 deletions.
10 changes: 1 addition & 9 deletions Sources/Parser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,8 @@ extension Method {
}
}

extension Data {
public init(pointer: UnsafePointer<Int8>, length: Int) {
var bytes: [UInt8] = [UInt8](repeating: 0, count: length)
memcpy(&bytes, pointer, length)
self.bytes = bytes
}
}

extension UnsafeMutablePointer {
func withMemory<R>(_ body: @noescape (inout pointer: Pointee) throws -> R) rethrows -> R {
func withPointee<R>(_ body: @noescape (inout pointer: Pointee) throws -> R) rethrows -> R {
return try body(pointer: &pointee)
}
}
25 changes: 10 additions & 15 deletions Sources/RequestParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ public final class RequestParser: S4.RequestParser {
}

deinit {
context.deinitialize(count:1)
context.deallocateCapacity(1)
}

Expand All @@ -84,13 +83,8 @@ public final class RequestParser: S4.RequestParser {
public func parse(_ data: Data) throws -> Request? {
defer { request = nil }

let buffer = data.withUnsafeBufferPointer {
UnsafeMutablePointer<Int8>($0.baseAddress)
}

let bytesParsed = http_parser_execute(&parser, &requestSettings, buffer, data.count)

if bytesParsed != data.count {
let bytesParsed = http_parser_execute(&parser, &requestSettings, UnsafePointer(data.bytes), data.count)
guard bytesParsed == data.count else {
resetParser()
let errorName = http_errno_name(http_errno(parser.http_errno))!
let errorDescription = http_errno_description(http_errno(parser.http_errno))!
Expand All @@ -113,7 +107,7 @@ extension RequestParser {
}

func onRequestURL(_ parser: Parser?, data: UnsafePointer<Int8>?, length: Int) -> Int32 {
return RequestContext(parser!.pointee.data).withMemory {
return RequestContext(parser!.pointee.data).withPointee {
guard let uri = String(pointer: data!, length: length) else {
return 1
}
Expand All @@ -124,7 +118,7 @@ func onRequestURL(_ parser: Parser?, data: UnsafePointer<Int8>?, length: Int) ->
}

func onRequestHeaderField(_ parser: Parser?, data: UnsafePointer<Int8>?, length: Int) -> Int32 {
return RequestContext(parser!.pointee.data).withMemory {
return RequestContext(parser!.pointee.data).withPointee {
guard let headerName = String(pointer: data!, length: length) else {
return 1
}
Expand All @@ -139,7 +133,7 @@ func onRequestHeaderField(_ parser: Parser?, data: UnsafePointer<Int8>?, length:
}

func onRequestHeaderValue(_ parser: Parser?, data: UnsafePointer<Int8>?, length: Int) -> Int32 {
return RequestContext(parser!.pointee.data).withMemory {
return RequestContext(parser!.pointee.data).withPointee {
guard let headerValue = String(pointer: data!, length: length) else {
return 1
}
Expand All @@ -159,7 +153,7 @@ func onRequestHeaderValue(_ parser: Parser?, data: UnsafePointer<Int8>?, length:
}

func onRequestHeadersComplete(_ parser: Parser?) -> Int32 {
return RequestContext(parser!.pointee.data).withMemory {
return RequestContext(parser!.pointee.data).withPointee {
$0.method = Method(code: Int(parser!.pointee.method))
let major = Int(parser!.pointee.http_major)
let minor = Int(parser!.pointee.http_minor)
Expand All @@ -178,16 +172,17 @@ func onRequestHeadersComplete(_ parser: Parser?) -> Int32 {
}

func onRequestBody(_ parser: Parser?, data: UnsafePointer<Int8>?, length: Int) -> Int32 {
RequestContext(parser!.pointee.data).withMemory {
$0.body += Data(pointer: data!, length: length)
RequestContext(parser!.pointee.data).withPointee {
let buffer = UnsafeBufferPointer<UInt8>(start: UnsafePointer(data), count: length)
$0.body += Data(Array(buffer))
return
}

return 0
}

func onRequestMessageComplete(_ parser: Parser?) -> Int32 {
return RequestContext(parser!.pointee.data).withMemory {
return RequestContext(parser!.pointee.data).withPointee {
let request = Request(
method: $0.method,
uri: $0.uri,
Expand Down
27 changes: 10 additions & 17 deletions Sources/ResponseParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ public final class ResponseParser: S4.ResponseParser {
}

deinit {
context.deinitialize(count: 1)
context.deallocateCapacity(1)
}

Expand All @@ -83,15 +82,8 @@ public final class ResponseParser: S4.ResponseParser {
public func parse(_ data: Data) throws -> Response? {
defer { response = nil }

var data = data

let buffer = data.withUnsafeMutableBufferPointer {
UnsafeMutablePointer<Int8>($0.baseAddress)
}

let bytesParsed = http_parser_execute(&parser, &responseSettings, buffer, data.count)

if bytesParsed != data.count {
let bytesParsed = http_parser_execute(&parser, &responseSettings, UnsafePointer(data.bytes), data.count)
guard bytesParsed == data.count else {
resetParser()
let errorName = http_errno_name(http_errno(parser.http_errno))!
let errorDescription = http_errno_description(http_errno(parser.http_errno))!
Expand All @@ -114,7 +106,7 @@ extension ResponseParser {
}

func onResponseStatus(_ parser: Parser?, data: UnsafePointer<Int8>?, length: Int) -> Int32 {
return ResponseContext(parser!.pointee.data).withMemory {
return ResponseContext(parser!.pointee.data).withPointee {
guard let reasonPhrase = String(pointer: data!, length: length) else {
return 1
}
Expand All @@ -125,7 +117,7 @@ func onResponseStatus(_ parser: Parser?, data: UnsafePointer<Int8>?, length: Int
}

func onResponseHeaderField(_ parser: Parser?, data: UnsafePointer<Int8>?, length: Int) -> Int32 {
return ResponseContext(parser!.pointee.data).withMemory {
return ResponseContext(parser!.pointee.data).withPointee {
guard let headerName = String(pointer: data!, length: length) else {
return 1
}
Expand All @@ -140,7 +132,7 @@ func onResponseHeaderField(_ parser: Parser?, data: UnsafePointer<Int8>?, length
}

func onResponseHeaderValue(_ parser: Parser?, data: UnsafePointer<Int8>?, length: Int) -> Int32 {
return ResponseContext(parser!.pointee.data).withMemory {
return ResponseContext(parser!.pointee.data).withPointee {
guard let headerValue = String(pointer: data!, length: length) else {
return 1
}
Expand All @@ -160,7 +152,7 @@ func onResponseHeaderValue(_ parser: Parser?, data: UnsafePointer<Int8>?, length
}

func onResponseHeadersComplete(_ parser: Parser?) -> Int32 {
return ResponseContext(parser!.pointee.data).withMemory {
return ResponseContext(parser!.pointee.data).withPointee {
$0.buildingHeaderName = ""
$0.currentHeaderName = ""
$0.statusCode = Int(parser!.pointee.status_code)
Expand All @@ -172,14 +164,15 @@ func onResponseHeadersComplete(_ parser: Parser?) -> Int32 {
}

func onResponseBody(_ parser: Parser?, data: UnsafePointer<Int8>?, length: Int) -> Int32 {
return ResponseContext(parser!.pointee.data).withMemory {
$0.body += Data(pointer: data!, length: length)
return ResponseContext(parser!.pointee.data).withPointee {
let buffer = UnsafeBufferPointer<UInt8>(start: UnsafePointer(data), count: length)
$0.body += Data(Array(buffer))
return 0
}
}

func onResponseMessageComplete(_ parser: Parser?) -> Int32 {
return ResponseContext(parser!.pointee.data).withMemory {
return ResponseContext(parser!.pointee.data).withPointee {
let response = Response(
version: $0.version,
status: Status(statusCode: $0.statusCode, reasonPhrase: $0.reasonPhrase),
Expand Down

0 comments on commit 422e8e2

Please sign in to comment.