Skip to content

Commit

Permalink
Kitura/Kitura#644 NSData to Data migration and syntax changes
Browse files Browse the repository at this point in the history
  • Loading branch information
shmuelk committed Aug 4, 2016
1 parent ff21bc6 commit fafd06e
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 64 deletions.
36 changes: 14 additions & 22 deletions Sources/KituraNet/FastCGI/FastCGIServerRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public class FastCGIServerRequest : ServerRequest {
/// URL strings.
///
public var urlString : String {
guard url.length > 0 else {
guard url.count > 0 else {
return ""
}
return StringUtils.fromUtf8String(url)!
Expand All @@ -68,7 +68,7 @@ public class FastCGIServerRequest : ServerRequest {
///
/// Raw URL
///
public private(set) var url = NSMutableData()
public private(set) var url = Data()

///
/// Chunk of body read in by the http_parser, filled by callbacks to onBody
Expand Down Expand Up @@ -126,23 +126,23 @@ public class FastCGIServerRequest : ServerRequest {
//
// Read data received (perhaps from POST) into an NSData object
//
public func read(into data: NSMutableData) throws -> Int {
return bodyChunk.fill(data: data)
public func read(into data: inout Data) throws -> Int {
return bodyChunk.fill(data: &data)
}

//
// Read all data into the object.
//
public func readAllData(into data: NSMutableData) throws -> Int {
return bodyChunk.fill(data: data)
public func readAllData(into data: inout Data) throws -> Int {
return bodyChunk.fill(data: &data)
}

//
// Read data received (perhaps from POST) as a string
//
public func readString() throws -> String? {
let data : NSMutableData = NSMutableData()
let bytes : Int = bodyChunk.fill(data: data)
var data = Data()
let bytes : Int = bodyChunk.fill(data: &data)

if bytes > 0 {
return StringUtils.fromUtf8String(data)
Expand All @@ -158,25 +158,17 @@ public class FastCGIServerRequest : ServerRequest {

// reset the current url
//
url.length = 0
url.count = 0

// set the uri
//
if requestUri?.characters.count > 0 {
if let requestUri = requestUri, requestUri.characters.count > 0 {

// use the URI as received
#if os(Linux)
url.append(StringUtils.toUtf8String(requestUri!)!)
#else
url.append(StringUtils.toUtf8String(requestUri!)! as Data)
#endif
url.append(StringUtils.toUtf8String(requestUri)!)
}
else {
#if os(Linux)
url.append(StringUtils.toUtf8String("/")!)
#else
url.append(StringUtils.toUtf8String("/")! as Data)
#endif
url.append(StringUtils.toUtf8String("/")!)
}

}
Expand Down Expand Up @@ -418,10 +410,10 @@ public class FastCGIServerRequest : ServerRequest {
return
}

if record.data?.length > 0 {
if let data = record.data, data.length > 0 {
// we've received some request body data as part of the STDIN
//
bodyChunk.append(data: record.data!)
bodyChunk.append(bytes: UnsafePointer<UInt8>(data.bytes), length: data.length)
}
else {
// a zero length stdin means request is done
Expand Down
25 changes: 9 additions & 16 deletions Sources/KituraNet/FastCGI/FastCGIServerResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public class FastCGIServerResponse : ServerResponse {
return HTTPStatusCode(rawValue: status)
}
set (newValue) {
if let newValue = newValue where !startFlushed {
if let newValue = newValue, !startFlushed {
status = newValue.rawValue
}
}
Expand Down Expand Up @@ -96,19 +96,17 @@ public class FastCGIServerResponse : ServerResponse {
//
// Actual write methods
//
public func write(from data: NSData) throws {
public func write(from data: Data) throws {

try startResponse()

if (buffer.length + data.length) > FastCGIServerResponse.bufferSize {
if (buffer.length + data.count) > FastCGIServerResponse.bufferSize {
try flush()
}

#if os(Linux)
buffer.append(data)
#else
buffer.append(data as Data)
#endif
data.withUnsafeBytes() { [unowned self] (bytes: UnsafePointer<Int8>) in
self.buffer.append(bytes, length: data.count)
}
}

public func end() throws {
Expand Down Expand Up @@ -203,24 +201,19 @@ public class FastCGIServerResponse : ServerResponse {
guard let serverRequest = self.serverRequest else {
throw FastCGI.RecordErrors.internalError
}
guard let requestId : UInt16 = serverRequest.requestId else {
throw FastCGI.RecordErrors.internalError
}
guard requestId != FastCGI.Constants.FASTCGI_DEFAULT_REQUEST_ID else {
guard serverRequest.requestId != FastCGI.Constants.FASTCGI_DEFAULT_REQUEST_ID else {
throw FastCGI.RecordErrors.internalError
}

return try getEndRequestMessage(requestId: requestId, protocolStatus: FastCGI.Constants.FCGI_UNKNOWN_ROLE)
return try getEndRequestMessage(requestId: serverRequest.requestId, protocolStatus: FastCGI.Constants.FCGI_UNKNOWN_ROLE)

}

///
/// External message write for multiplex rejection
///
public func rejectMultiplexConnecton(requestId: UInt16) throws {
guard let message : NSData = try getNoMultiplexingMessage(requestId: requestId) else {
return
}
let message = try getNoMultiplexingMessage(requestId: requestId)
try writeToSocket(message, wrapAsMessage: false)
}

Expand Down
42 changes: 16 additions & 26 deletions Sources/KituraNet/HTTP/HTTPServerResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class HTTPServerResponse : ServerResponse {
private static let bufferSize = 2000

/// Buffer for HTTP response line, headers, and short bodies
private var buffer: NSMutableData
private var buffer: Data

/// Whether or not the HTTP response line and headers have been flushed.
private var startFlushed = false
Expand Down Expand Up @@ -54,7 +54,7 @@ public class HTTPServerResponse : ServerResponse {
return HTTPStatusCode(rawValue: status)
}
set (newValue) {
if let newValue = newValue where !startFlushed {
if let newValue = newValue, !startFlushed {
status = newValue.rawValue
}
}
Expand All @@ -66,7 +66,7 @@ public class HTTPServerResponse : ServerResponse {
init(processor: IncomingHTTPSocketProcessor) {

self.processor = processor
buffer = NSMutableData(capacity: HTTPServerResponse.bufferSize)!
buffer = Data(capacity: HTTPServerResponse.bufferSize)
headers["Date"] = [SPIUtils.httpDate()]
}

Expand All @@ -87,29 +87,23 @@ public class HTTPServerResponse : ServerResponse {
///
/// Write data as a response
///
/// - Parameter data: NSMutableData object to contain read data.
///
/// - Returns: Integer representing the number of bytes read.
/// - Parameter data: Data object that contains the data to be written.
///
/// - Throws: Socket.error if an error occurred while writing to a socket
///
public func write(from data: NSData) throws {
public func write(from data: Data) throws {

if let processor = processor {
try flushStart()
if buffer.length + data.length > HTTPServerResponse.bufferSize && buffer.length != 0 {
if buffer.count + data.count > HTTPServerResponse.bufferSize && buffer.count != 0 {
processor.write(from: buffer)
buffer.length = 0
buffer.count = 0
}
if data.length > HTTPServerResponse.bufferSize {
if data.count > HTTPServerResponse.bufferSize {
processor.write(from: data)
}
else {
#if os(Linux)
buffer.append(data)
#else
buffer.append(data as Data)
#endif
buffer.append(data)
}
}

Expand Down Expand Up @@ -138,12 +132,12 @@ public class HTTPServerResponse : ServerResponse {

try flushStart()

let keepAlive = processor.isKeepAlive ?? false
let keepAlive = processor.isKeepAlive
if keepAlive {
processor.keepAlive()
}

if buffer.length > 0 {
if buffer.count > 0 {
processor.write(from: buffer)
}

Expand Down Expand Up @@ -206,26 +200,22 @@ public class HTTPServerResponse : ServerResponse {
return
}

if buffer.length + utf8Data.length > HTTPServerResponse.bufferSize && buffer.length != 0 {
if buffer.count + utf8Data.count > HTTPServerResponse.bufferSize && buffer.count != 0 {
processor.write(from: buffer)
buffer.length = 0
buffer.count = 0
}
if utf8Data.length > HTTPServerResponse.bufferSize {
if utf8Data.count > HTTPServerResponse.bufferSize {
processor.write(from: utf8Data)
}
else {
#if os(Linux)
buffer.append(utf8Data)
#else
buffer.append(utf8Data as Data)
#endif
buffer.append(utf8Data)
}
}

/// Reset this response object back to it's initial state
public func reset() {
status = HTTPStatusCode.OK.rawValue
buffer.length = 0
buffer.count = 0
startFlushed = false
headers.removeAll()
headers["Date"] = [SPIUtils.httpDate()]
Expand Down

0 comments on commit fafd06e

Please sign in to comment.