Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: SwiftLint errors and warnings #122

Merged
merged 1 commit into from
Dec 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ let package = Package(
// Products define the executables and libraries produced by a package, and make them visible to other packages.
.library(
name: "KituraNet",
targets: ["KituraNet"]),
targets: ["KituraNet"])
],
dependencies: [
// Dependencies declare other packages that this package depends on.
.package(url: "https://github.com/apple/swift-nio.git", from: "1.0.0"),
.package(url: "https://github.com/apple/swift-nio-ssl.git", from: "1.0.1"),
.package(url: "https://github.com/IBM-Swift/BlueSSLService.git", from: "1.0.0"),
.package(url: "https://github.com/IBM-Swift/LoggerAPI.git", from: "1.7.3"),
.package(url: "https://github.com/IBM-Swift/LoggerAPI.git", from: "1.7.3")
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
Expand All @@ -44,6 +44,6 @@ let package = Package(
dependencies: ["NIO", "NIOFoundationCompat", "NIOHTTP1", "NIOOpenSSL", "SSLService", "LoggerAPI", "NIOWebSocket", "CLinuxHelpers"]),
.testTarget(
name: "KituraNetTests",
dependencies: ["KituraNet"]),
dependencies: ["KituraNet"])
]
)
2 changes: 1 addition & 1 deletion Sources/KituraNet/BufferList.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public class BufferList {
///Fill a `NSMutableData` with data from the buffer.
public func fill(data: NSMutableData) -> Int {
let length = byteBuffer.readableBytes
let result = byteBuffer.readWithUnsafeReadableBytes() { body in
let result = byteBuffer.readWithUnsafeReadableBytes { body in
data.append(body.baseAddress!, length: length)
return length
}
Expand Down
62 changes: 31 additions & 31 deletions Sources/KituraNet/ClientRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public class ClientRequest {
///
/// - Parameter option: an `Options` instance describing the change to be made to the request
public func set(_ option: Options) {
switch(option) {
switch option {
case .schema, .hostname, .port, .path, .username, .password:
Log.error("Must use ClientRequest.init() to set URL components")
case .method(let method):
Expand Down Expand Up @@ -188,33 +188,33 @@ public class ClientRequest {
var path = ""
var port = ""

for option in options {
switch(option) {
for option in options {
switch option {

case .method, .headers, .maxRedirects, .disableSSLVerification, .useHTTP2:
// call set() for Options that do not construct the URL
set(option)
case .schema(var schema):
if !schema.contains("://") && !schema.isEmpty {
schema += "://"
}
theSchema = schema
case .hostname(let host):
hostName = host
self.hostName = host
case .port(let thePort):
port = ":\(thePort)"
self.port = Int(thePort)
case .path(var thePath):
if thePath.first != "/" {
thePath = "/" + thePath
}
path = thePath
self.path = path
case .username(let userName):
self.userName = userName
case .password(let password):
self.password = password
case .method, .headers, .maxRedirects, .disableSSLVerification, .useHTTP2:
// call set() for Options that do not construct the URL
set(option)
case .schema(var schema):
if !schema.contains("://") && !schema.isEmpty {
schema += "://"
}
theSchema = schema
case .hostname(let host):
hostName = host
self.hostName = host
case .port(let thePort):
port = ":\(thePort)"
self.port = Int(thePort)
case .path(var thePath):
if thePath.first != "/" {
thePath = "/" + thePath
}
path = thePath
self.path = path
case .username(let userName):
self.userName = userName
case .password(let password):
self.password = password
}
}

Expand All @@ -223,7 +223,7 @@ public class ClientRequest {
let pwd = self.password ?? ""
var authenticationClause = ""
// If either the userName or password are non-empty, add the authenticationClause
if (!user.isEmpty || !pwd.isEmpty) {
if !user.isEmpty || !pwd.isEmpty {
authenticationClause = "\(user):\(pwd)@"
}

Expand Down Expand Up @@ -392,7 +392,7 @@ public class ClientRequest {
return
}

var request = HTTPRequestHead(version: HTTPVersion(major: 1, minor:1), method: HTTPMethod.method(from: self.method), uri: path)
var request = HTTPRequestHead(version: HTTPVersion(major: 1, minor: 1), method: HTTPMethod.method(from: self.method), uri: path)
request.headers = HTTPHeaders.from(dictionary: self.headers)

// Make the HTTP request, the response callbacks will be received on the HTTPClientHandler.
Expand Down Expand Up @@ -578,7 +578,7 @@ class HTTPClientHandler: ChannelInboundHandler {
let response = self.unwrapInboundIn(data)
switch response {
case .head(let header):
clientResponse._headers = header.headers
clientResponse.httpHeaders = header.headers
clientResponse.httpVersionMajor = header.version.major
clientResponse.httpVersionMinor = header.version.minor
clientResponse.statusCode = HTTPStatusCode(rawValue: Int(header.status.code))!
Expand All @@ -588,7 +588,7 @@ class HTTPClientHandler: ChannelInboundHandler {
} else {
clientResponse.buffer!.byteBuffer.write(buffer: &buffer)
}
case .end(_):
case .end:
// Handle redirection
if clientResponse.statusCode == .movedTemporarily || clientResponse.statusCode == .movedPermanently {
self.clientRequest.redirectCount += 1
Expand Down
6 changes: 3 additions & 3 deletions Sources/KituraNet/ClientResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,19 @@ public class ClientResponse {
/// Minor version of HTTP of the response
public var httpVersionMinor: UInt16?

internal var _headers: HTTPHeaders?
internal var httpHeaders: HTTPHeaders?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you mean to rename this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ianpartridge Yes, by rules it can contain only alphanumeric characters.


/// Set of HTTP headers of the response.
public var headers: HeadersContainer {
get {
guard let httpHeaders = _headers else {
guard let httpHeaders = httpHeaders else {
return HeadersContainer()
}
return HeadersContainer.create(from: httpHeaders)
}

set {
_headers = newValue.httpHeaders()
httpHeaders = newValue.httpHeaders()
}
}
/// The HTTP Status code, as an Int, sent in the response by the remote server.
Expand Down
40 changes: 20 additions & 20 deletions Sources/KituraNet/FastCGI/FastCGI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,47 +24,47 @@ public class FastCGI {

// general
//
static let FASTCGI_PROTOCOL_VERSION : UInt8 = 1
static let FASTCGI_DEFAULT_REQUEST_ID : UInt16 = 0
static let FASTCGI_PROTOCOL_VERSION: UInt8 = 1
static let FASTCGI_DEFAULT_REQUEST_ID: UInt16 = 0

// FastCGI record types
//
static let FCGI_NO_TYPE : UInt8 = 0
static let FCGI_BEGIN_REQUEST : UInt8 = 1
static let FCGI_END_REQUEST : UInt8 = 3
static let FCGI_PARAMS : UInt8 = 4
static let FCGI_STDIN : UInt8 = 5
static let FCGI_STDOUT : UInt8 = 6
static let FCGI_NO_TYPE: UInt8 = 0
static let FCGI_BEGIN_REQUEST: UInt8 = 1
static let FCGI_END_REQUEST: UInt8 = 3
static let FCGI_PARAMS: UInt8 = 4
static let FCGI_STDIN: UInt8 = 5
static let FCGI_STDOUT: UInt8 = 6

// sub types
//
static let FCGI_SUBTYPE_NO_TYPE : UInt8 = 99
static let FCGI_REQUEST_COMPLETE : UInt8 = 0
static let FCGI_CANT_MPX_CONN : UInt8 = 1
static let FCGI_UNKNOWN_ROLE : UInt8 = 3
static let FCGI_SUBTYPE_NO_TYPE: UInt8 = 99
static let FCGI_REQUEST_COMPLETE: UInt8 = 0
static let FCGI_CANT_MPX_CONN: UInt8 = 1
static let FCGI_UNKNOWN_ROLE: UInt8 = 3

// roles
//
static let FCGI_NO_ROLE : UInt16 = 99
static let FCGI_RESPONDER : UInt16 = 1
static let FCGI_NO_ROLE: UInt16 = 99
static let FCGI_RESPONDER: UInt16 = 1

// flags
//
static let FCGI_KEEP_CONN : UInt8 = 1
static let FCGI_KEEP_CONN: UInt8 = 1

// request headers of note
// we translate these into internal variables
//
static let HEADER_REQUEST_METHOD : String = "REQUEST_METHOD";
static let HEADER_REQUEST_SCHEME : String = "REQUEST_SCHEME";
static let HEADER_HTTP_HOST : String = "HTTP_HOST";
static let HEADER_REQUEST_URI : String = "REQUEST_URI";
static let HEADER_REQUEST_METHOD: String = "REQUEST_METHOD";
static let HEADER_REQUEST_SCHEME: String = "REQUEST_SCHEME";
static let HEADER_HTTP_HOST: String = "HTTP_HOST";
static let HEADER_REQUEST_URI: String = "REQUEST_URI";
}

//
// Exceptions
//
enum RecordErrors : Swift.Error {
enum RecordErrors: Swift.Error {

case invalidType
case invalidSubType
Expand Down
14 changes: 7 additions & 7 deletions Sources/KituraNet/FastCGI/FastCGIServerRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import LoggerAPI

/// The FastCGIServerRequest class implements the `ServerRequest` protocol
/// for incoming HTTP requests that come in over a FastCGI connection.
public class FastCGIServerRequest : ServerRequest {
public class FastCGIServerRequest: ServerRequest {

/// The IP address of the client
public private(set) var remoteAddress: String = ""
Expand All @@ -41,7 +41,7 @@ public class FastCGIServerRequest : ServerRequest {
public private(set) var method: String = ""

/// URI Component received from FastCGI
private var requestUri : String? = nil
private var requestUri: String?

public private(set) var urlURL = URL(string: "http://not_available/")!

Expand All @@ -50,12 +50,12 @@ public class FastCGIServerRequest : ServerRequest {
/// Use 'urlURL' for the full URL
@available(*, deprecated, message:
"This contains just the path and query parameters starting with '/'. use 'urlURL' instead")
public var urlString : String { return requestUri ?? "" }
public var urlString: String { return requestUri ?? "" }

/// The URL from the request in UTF-8 form
/// This contains just the path and query parameters starting with '/'
/// Use 'urlURL' for the full URL
public var url : Data { return requestUri?.data(using: .utf8) ?? Data() }
public var url: Data { return requestUri?.data(using: .utf8) ?? Data() }

/// The URL from the request as URLComponents
/// URLComponents has a memory leak on linux as of swift 3.0.1. Use 'urlURL' instead
Expand All @@ -72,15 +72,15 @@ public class FastCGIServerRequest : ServerRequest {
private var status = Status.initial

/// The request ID established by the FastCGI client.
public private(set) var requestId : UInt16 = 0
public private(set) var requestId: UInt16 = 0

/// An array of request ID's that are not our primary one.
/// When the main request is done, the FastCGIServer can reject the
/// extra requests as being unusable.
public private(set) var extraRequestIds : [UInt16] = []
public private(set) var extraRequestIds: [UInt16] = []

/// Some defaults
private static let defaultMethod : String = "GET"
private static let defaultMethod: String = "GET"

/// List of status states
private enum Status {
Expand Down
8 changes: 4 additions & 4 deletions Sources/KituraNet/FastCGI/FastCGIServerResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ import Foundation
#endif

extension Range where Bound: BinaryInteger {
func iterate(by delta: Bound, action: (Range<Bound>) throws -> ()) throws {
func iterate(by delta: Bound, action: (Range<Bound>) throws -> Void) throws {

var base = self.lowerBound

while (base < self.upperBound) {
while base < self.upperBound {
let subRange = (base ..< (base + delta)).clamped(to: self)
try action(subRange)

Expand All @@ -38,7 +38,7 @@ extension Range where Bound: BinaryInteger {

/// The FastCGIServerRequest class implements the `ServerResponse` protocol
/// for incoming HTTP requests that come in over a FastCGI connection.
public class FastCGIServerResponse : ServerResponse {
public class FastCGIServerResponse: ServerResponse {

/// Size of buffers (64 * 1024 is the max size for a FastCGI outbound record)
/// Which also gives a bit more internal buffer room.
Expand All @@ -57,7 +57,7 @@ public class FastCGIServerResponse : ServerResponse {
private var status = HTTPStatusCode.OK.rawValue

/// Corresponding server request
private weak var serverRequest : FastCGIServerRequest?
private weak var serverRequest: FastCGIServerRequest?

/// The status code to send in the HTTP response.
public var statusCode: HTTPStatusCode? {
Expand Down
16 changes: 8 additions & 8 deletions Sources/KituraNet/HTTP/HTTP.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public class HTTP {
return req
}

private static let allowedCharacterSet = NSCharacterSet(charactersIn:"\"#%/<>?@\\^`{|} ").inverted
private static let allowedCharacterSet = NSCharacterSet(charactersIn: "\"#%/<>?@\\^`{|} ").inverted

public static func escape(url: String) -> String {
if let escaped = url.addingPercentEncoding(withAllowedCharacters: allowedCharacterSet) {
Expand All @@ -68,7 +68,7 @@ public class HTTP {
}
}

// MARK HTTPStatusCode
// MARK: HTTPStatusCode

/// HTTP status codes and numbers.
public enum HTTPStatusCode: Int {
Expand Down Expand Up @@ -109,12 +109,12 @@ extension HTTPStatusCode {

init(code: Int) {
switch code {
case 100..<200: self = .informational
case 200..<300: self = .successful
case 300..<400: self = .redirection
case 400..<500: self = .clientError
case 500..<600: self = .serverError
default: self = .invalidStatus
case 100..<200: self = .informational
case 200..<300: self = .successful
case 300..<400: self = .redirection
case 400..<500: self = .clientError
case 500..<600: self = .serverError
default: self = .invalidStatus
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/KituraNet/HTTP/HTTPRequestHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ internal class HTTPRequestHandler: ChannelInboundHandler {
public init(for server: HTTPServer) {
self.server = server
self.keepAliveState = server.keepAliveState
if let _ = server.sslConfig {
if server.sslConfig != nil {
self.enableSSLVerification = true
}
}
Expand Down Expand Up @@ -92,7 +92,7 @@ internal class HTTPRequestHandler: ChannelInboundHandler {
} else {
serverRequest.buffer!.byteBuffer.write(buffer: &buffer)
}
case .end(_):
case .end:
serverResponse = HTTPServerResponse(channel: ctx.channel, handler: self)
//Make sure we use the latest delegate registered with the server
DispatchQueue.global().async {
Expand Down
5 changes: 2 additions & 3 deletions Sources/KituraNet/HTTP/HTTPServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import NIOWebSocket
import CLinuxHelpers

/// An HTTP server that listens for connections on a socket.
public class HTTPServer : Server {
public class HTTPServer: Server {

public typealias ServerType = HTTPServer

Expand Down Expand Up @@ -341,8 +341,7 @@ class HTTPDummyServerDelegate: ServerDelegate {
response.headers["Content-Length"] = [String(theBody.utf8.count)]
try response.write(from: theBody)
try response.end()
}
catch {
} catch {
Log.error("Failed to send the response. Error = \(error)")
}
}
Expand Down
Loading