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

Swiftlint #72

Merged
merged 2 commits into from
Dec 11, 2015
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
5 changes: 5 additions & 0 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
disabled_rules:
- line_length
- statement_position
- trailing_whitespace
- variable_name_min_length
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ import PackageDescription

let package = Package(
name: "Swifter"
)
)
4 changes: 2 additions & 2 deletions Sources/Swifter/DemoServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public func demoServer(publicDir: String?) -> HttpServer {
}

server["/json"] = { request in
return .OK(.Json(["posts" : [[ "id" : 1, "message" : "hello world"],[ "id" : 2, "message" : "sample message"]], "new_updates" : false]))
return .OK(.Json(["posts": [["id": 1, "message": "hello world"], ["id": 2, "message": "sample message"]], "new_updates": false]))
}

server["/redirect"] = { request in
Expand All @@ -62,4 +62,4 @@ public func demoServer(publicDir: String?) -> HttpServer {
}

return server
}
}
10 changes: 5 additions & 5 deletions Sources/Swifter/HttpHandlers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class HttpHandlers {

private static let cache = NSCache()

public class func directory(dir: String) -> ( HttpRequest -> HttpResponse ) {
public class func directory(dir: String) -> (HttpRequest -> HttpResponse) {
return { request in

guard let localPath = request.params.first else {
Expand Down Expand Up @@ -78,16 +78,16 @@ public class HttpHandlers {
if let (_, value) = r.params.first {
let filePath = dir + "/" + value
let fileManager = NSFileManager.defaultManager()
var isDir: ObjCBool = false;
if ( fileManager.fileExistsAtPath(filePath, isDirectory: &isDir) ) {
if ( isDir ) {
var isDir: ObjCBool = false
if fileManager.fileExistsAtPath(filePath, isDirectory: &isDir) {
if isDir {
do {
let files = try fileManager.contentsOfDirectoryAtPath(filePath)
var response = "<h3>\(filePath)</h3></br><table>"
response += files.map({ "<tr><td><a href=\"\(r.url)/\($0)\">\($0)</a></td></tr>"}).joinWithSeparator("")
response += "</table>"
return HttpResponse.OK(.Html(response))
} catch {
} catch {
return HttpResponse.NotFound
}
} else {
Expand Down
6 changes: 3 additions & 3 deletions Sources/Swifter/HttpParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#endif


enum HttpParserError : ErrorType {
enum HttpParserError: ErrorType {
case ReadBodyFailed(String)
case InvalidStatusLine(String)
}
Expand Down Expand Up @@ -51,14 +51,14 @@ class HttpParser {

private func readBody(socket: Socket, size: Int) throws -> String {
var body = ""
var counter = 0;
var counter = 0
while counter < size {
let c = socket.read()
if c < 0 {
throw HttpParserError.ReadBodyFailed(String.fromCString(UnsafePointer(strerror(errno))) ?? "Error: \(errno)")
}
body.append(UnicodeScalar(c))
counter++;
counter++
}
return body
}
Expand Down
10 changes: 5 additions & 5 deletions Sources/Swifter/HttpResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public protocol Serializer {
func serialize(object: Any) throws -> String
}

public class JSONSerializer : Serializer {
public class JSONSerializer: Serializer {
public func serialize(object: Any) throws -> String {
guard let obj = object as? AnyObject where NSJSONSerialization.isValidJSONObject(obj) else {
throw SerializationError.InvalidObject
Expand Down Expand Up @@ -127,7 +127,7 @@ public enum HttpResponse {
case .Forbidden : return 403
case .NotFound : return 404
case .InternalServerError : return 500
case .RAW(let code,_,_,_) : return code
case .RAW(let code, _ , _, _) : return code
}
}

Expand All @@ -142,7 +142,7 @@ public enum HttpResponse {
case .Forbidden : return "Forbidden"
case .NotFound : return "Not Found"
case .InternalServerError : return "Internal Server Error"
case .RAW(_,let pharse,_,_) : return pharse
case .RAW(_, let phrase, _, _) : return phrase
}
}

Expand All @@ -164,7 +164,7 @@ public enum HttpResponse {
default:break
}
case .MovedPermanently(let location): headers["Location"] = location
case .RAW(_,_, let rawHeaders,_):
case .RAW(_, _, let rawHeaders, _):
if let rawHeaders = rawHeaders {
for (k, v) in rawHeaders {
headers.updateValue(v, forKey: k)
Expand All @@ -178,7 +178,7 @@ public enum HttpResponse {
func body() -> [UInt8]? {
switch self {
case .OK(let body) : return body.data()
case .RAW(_,_,_, let data) : return data
case .RAW(_, _, _, let data) : return data
default : return nil
}
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/Swifter/HttpRouter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,4 @@ public class HttpRouter {
}
return params
}
}
}
1 change: 0 additions & 1 deletion Sources/Swifter/HttpServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,3 @@ public class HttpServer {
}
}
}

10 changes: 5 additions & 5 deletions Sources/Swifter/Socket.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ enum SocketError: ErrorType {
case RecvFailed(String)
}

public class Socket : Hashable, Equatable {
public class Socket: Hashable, Equatable {

public class func tcpSocketForListen(port: in_port_t = 8080, maxPendingConnection: Int32 = SOMAXCONN) throws -> Socket {

Expand Down Expand Up @@ -133,7 +133,7 @@ public class Socket : Hashable, Equatable {
}

public func read() -> Int {
var buffer = [UInt8](count: 1, repeatedValue: 0);
var buffer = [UInt8](count: 1, repeatedValue: 0)
let next = recv(self.socketFileDescriptor as Int32, &buffer, Int(buffer.count), 0)
if next <= 0 {
return next
Expand All @@ -146,7 +146,7 @@ public class Socket : Hashable, Equatable {
var n = 0
repeat {
n = self.read()
if ( n > 13 /* CR */ ) { characters.append(Character(UnicodeScalar(n))) }
if n > 13 /* CR */ { characters.append(Character(UnicodeScalar(n))) }
} while n > 0 && n != 10 /* NL */
if n == -1 {
throw SocketError.RecvFailed(Socket.descriptionOfLastError())
Expand Down Expand Up @@ -179,8 +179,8 @@ public class Socket : Hashable, Equatable {
// or use signal(SIGPIPE, SIG_IGN) to make your entire application ignore SIGPIPE.
#else
// Prevents crashes when blocking calls are pending and the app is paused ( via Home button ).
var no_sig_pipe: Int32 = 1;
setsockopt(socket, SOL_SOCKET, SO_NOSIGPIPE, &no_sig_pipe, socklen_t(sizeof(Int32)));
var no_sig_pipe: Int32 = 1
setsockopt(socket, SOL_SOCKET, SO_NOSIGPIPE, &no_sig_pipe, socklen_t(sizeof(Int32)))
#endif
}

Expand Down
18 changes: 9 additions & 9 deletions Sources/Swifter/String+Misc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ extension String {
buffer.append(Character(scalar))
continue
}
if ( scalar == "%" ) {
if scalar == "%" {
let first = scalars.popFirst()
let secon = scalars.popFirst()
if let first = unicodeScalarToUInt32Hex(first), secon = unicodeScalarToUInt32Hex(secon) {
Expand All @@ -53,28 +53,28 @@ extension String {

private func unicodeScalarToUInt32Whitespace(x: UnicodeScalar?) -> UInt32? {
if let x = x {
if (x.value >= 9 && x.value <= 13) {
return x.value;
if x.value >= 9 && x.value <= 13 {
return x.value
}
if (x.value == 32) {
return x.value;
if x.value == 32 {
return x.value
}
}
return nil
}

private func unicodeScalarToUInt32Hex(x: UnicodeScalar?) -> UInt32? {
if let x = x {
if (x.value >= 48 && x.value <= 57) {
if x.value >= 48 && x.value <= 57 {
return x.value - 48
}
if (x.value >= 97 && x.value <= 102) {
if x.value >= 97 && x.value <= 102 {
return x.value - 97
}
if (x.value >= 65 && x.value <= 70) {
if x.value >= 65 && x.value <= 70 {
return x.value - 65
}
}
return nil
}
}
}
5 changes: 2 additions & 3 deletions SwifterSampleiOS/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ class ViewController: UIViewController {
}

@IBAction func likedThis(sender: UIButton) {
self.server?.stop();
self.server = nil;
self.server?.stop()
self.server = nil
}
}