Skip to content

Commit

Permalink
Add the trailing whitespace rule in Swiftlint
Browse files Browse the repository at this point in the history
* Autocorrect the new rule in Swiftlint to remove all the trailing whitespaces
  • Loading branch information
Vkt0r committed Jun 11, 2019
1 parent 8b5afb4 commit 27b6901
Show file tree
Hide file tree
Showing 31 changed files with 349 additions and 351 deletions.
1 change: 0 additions & 1 deletion .swiftlint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ identifier_name:
disabled_rules:
- line_length
- statement_position
- trailing_whitespace

excluded: # paths to ignore during linting. Takes precedence over `included`.
- LinuxMain.swift
Expand Down
61 changes: 30 additions & 31 deletions XCode/Sources/DemoServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import Foundation

// swiftlint:disable function_body_length
public func demoServer(_ publicDir: String) -> HttpServer {

print(publicDir)

let server = HttpServer()

server["/public/:path"] = shareFilesFromDirectory(publicDir)

server["/files/:path"] = directoryBrowser("/")
Expand All @@ -29,37 +29,37 @@ public func demoServer(_ publicDir: String) -> HttpServer {
}
}
}

server["/magic"] = { .ok(.htmlBody("You asked for " + $0.path)) }

server["/test/:param1/:param2"] = { request in
scopes {
html {
body {
h3 { inner = "Address: \(request.address ?? "unknown")" }
h3 { inner = "Url: \(request.path)" }
h3 { inner = "Method: \(request.method)" }

h3 { inner = "Query:" }

table(request.queryParams) { param in
tr {
td { inner = param.0 }
td { inner = param.1 }
}
}

h3 { inner = "Headers:" }

table(request.headers) { header in
tr {
td { inner = header.0 }
td { inner = header.1 }
}
}

h3 { inner = "Route params:" }

table(request.params) { param in
tr {
td { inner = param.0 }
Expand All @@ -70,19 +70,19 @@ public func demoServer(_ publicDir: String) -> HttpServer {
}
}(request)
}

server.GET["/upload"] = scopes {
html {
body {
form {
method = "POST"
action = "/upload"
enctype = "multipart/form-data"

input { name = "my_file1"; type = "file" }
input { name = "my_file2"; type = "file" }
input { name = "my_file3"; type = "file" }

button {
type = "submit"
inner = "Upload"
Expand All @@ -91,7 +91,7 @@ public func demoServer(_ publicDir: String) -> HttpServer {
}
}
}

server.POST["/upload"] = { request in
var response = ""
for multipart in request.parseMultiPartFormData() {
Expand All @@ -100,7 +100,7 @@ public func demoServer(_ publicDir: String) -> HttpServer {
}
return HttpResponse.ok(.htmlBody(response))
}

server.GET["/login"] = scopes {
html {
head {
Expand All @@ -109,11 +109,11 @@ public func demoServer(_ publicDir: String) -> HttpServer {
}
body {
h3 { inner = "Sign In" }

form {
method = "POST"
action = "/login"

fieldset {
input { placeholder = "E-mail"; name = "email"; type = "email"; autofocus = "" }
input { placeholder = "Password"; name = "password"; type = "password"; autofocus = "" }
Expand All @@ -125,20 +125,20 @@ public func demoServer(_ publicDir: String) -> HttpServer {
}
}
}

}
javascript {
src = "http://cdn.staticfile.org/twitter-bootstrap/3.3.0/js/bootstrap.min.js"
}
}
}
}

server.POST["/login"] = { request in
let formFields = request.parseUrlencodedForm()
return HttpResponse.ok(.htmlBody(formFields.map({ "\($0.0) = \($0.1)" }).joined(separator: "<br>")))
}

server["/demo"] = scopes {
html {
body {
Expand All @@ -149,15 +149,15 @@ public func demoServer(_ publicDir: String) -> HttpServer {
}
}
}

server["/raw"] = { _ in
return HttpResponse.raw(200, "OK", ["XXX-Custom-Header": "value"], { try $0.write([UInt8]("test".utf8)) })
}

server["/redirect/permanently"] = { _ in
return .movedPermanently("http://www.google.com")
}

server["/redirect/temporarily"] = { _ in
return .movedTemporarily("http://www.google.com")
}
Expand All @@ -167,19 +167,19 @@ public func demoServer(_ publicDir: String) -> HttpServer {
for index in 0..<1000 { longResponse += "(\(index)),->" }
return .ok(.htmlBody(longResponse))
}

server["/wildcard/*/test/*/:param"] = { request in
return .ok(.htmlBody(request.path))
}

server["/stream"] = { _ in
return HttpResponse.raw(200, "OK", nil, { writer in
for index in 0...100 {
try writer.write([UInt8]("[chunk \(index)]".utf8))
}
})
}

server["/websocket-echo"] = websocket(text: { (session, text) in
session.writeText(text)
}, binary: { (session, binary) in
Expand All @@ -191,16 +191,15 @@ public func demoServer(_ publicDir: String) -> HttpServer {
}, disconnected: { _ in
// Client disconnected
})

server.notFoundHandler = { _ in
return .movedPermanently("https://github.com/404")
}

server.middleware.append { request in
print("Middleware: \(request.address ?? "unknown address") -> \(request.method) -> \(request.path)")
return nil
}

return server
}

2 changes: 1 addition & 1 deletion XCode/Sources/Errno.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import Foundation

public class Errno {

public class func description() -> String {
// https://forums.developer.apple.com/thread/113919
return String(cString: strerror(errno))
Expand Down
6 changes: 3 additions & 3 deletions XCode/Sources/Files.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,16 @@ public func shareFilesFromDirectory(_ directoryPath: String, defaults: [String]
}
}
let filePath = directoryPath + String.pathSeparator + fileRelativePath.value

if let file = try? filePath.openForReading() {
let mimeType = fileRelativePath.value.mimeType()
var responseHeader: [String: String] = ["Content-Type": mimeType]

if let attr = try? FileManager.default.attributesOfItem(atPath: filePath),
let fileSize = attr[FileAttributeKey.size] as? UInt64 {
responseHeader["Content-Length"] = String(fileSize)
}

return .raw(200, "OK", responseHeader, { writer in
try? writer.write(file)
file.close()
Expand Down
8 changes: 4 additions & 4 deletions XCode/Sources/HttpParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ enum HttpParserError: Error {
}

public class HttpParser {

public init() { }

public func readHttpRequest(_ socket: Socket) throws -> HttpRequest {
let statusLine = try socket.readLine()
let statusLineTokens = statusLine.components(separatedBy: " ")
Expand All @@ -36,7 +36,7 @@ public class HttpParser {
private func readBody(_ socket: Socket, size: Int) throws -> [UInt8] {
return try socket.read(length: size)
}

private func readHeaders(_ socket: Socket) throws -> [String: String] {
var headers = [String: String]()
while case let headerLine = try socket.readLine(), !headerLine.isEmpty {
Expand All @@ -47,7 +47,7 @@ public class HttpParser {
}
return headers
}

func supportsKeepAlive(_ headers: [String: String]) -> Bool {
if let value = headers["connection"] {
return "keep-alive" == value.trimmingCharacters(in: .whitespaces)
Expand Down
30 changes: 15 additions & 15 deletions XCode/Sources/HttpRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,24 @@
import Foundation

public class HttpRequest {

public var path: String = ""
public var queryParams: [(String, String)] = []
public var method: String = ""
public var headers: [String: String] = [:]
public var body: [UInt8] = []
public var address: String? = ""
public var params: [String: String] = [:]

public init() {}

public func hasTokenForHeader(_ headerName: String, token: String) -> Bool {
guard let headerValue = headers[headerName] else {
return false
}
return headerValue.components(separatedBy: ",").filter({ $0.trimmingCharacters(in: .whitespaces).lowercased() == token }).count > 0
}

public func parseUrlencodedForm() -> [(String, String)] {
guard let contentTypeHeader = headers["content-type"] else {
return []
Expand All @@ -47,20 +47,20 @@ public class HttpRequest {
return ("", "")
}
}

public struct MultiPart {

public let headers: [String: String]
public let body: [UInt8]

public var name: String? {
return valueFor("content-disposition", parameter: "name")?.unquote()
}

public var fileName: String? {
return valueFor("content-disposition", parameter: "filename")?.unquote()
}

private func valueFor(_ headerName: String, parameter: String) -> String? {
return headers.reduce([String]()) { (combined, header: (key: String, value: String)) -> [String] in
guard header.key == headerName else {
Expand All @@ -77,7 +77,7 @@ public class HttpRequest {
}.first
}
}

public func parseMultiPartFormData() -> [MultiPart] {
guard let contentTypeHeader = headers["content-type"] else {
return []
Expand All @@ -98,7 +98,7 @@ public class HttpRequest {
}
return []
}

private func parseMultiPartFormData(_ data: [UInt8], boundary: String) -> [MultiPart] {
var generator = data.makeIterator()
var result = [MultiPart]()
Expand All @@ -107,7 +107,7 @@ public class HttpRequest {
}
return result
}

private func nextMultiPart(_ generator: inout IndexingIterator<[UInt8]>, boundary: String, isFirst: Bool) -> MultiPart? {
if isFirst {
guard nextUTF8MultiPartLine(&generator) == boundary else {
Expand All @@ -128,7 +128,7 @@ public class HttpRequest {
}
return MultiPart(headers: headers, body: body)
}

private func nextUTF8MultiPartLine(_ generator: inout IndexingIterator<[UInt8]>) -> String? {
var temp = [UInt8]()
while let value = generator.next() {
Expand All @@ -141,11 +141,11 @@ public class HttpRequest {
}
return String(bytes: temp, encoding: String.Encoding.utf8)
}

// swiftlint:disable identifier_name
static let CR = UInt8(13)
static let NL = UInt8(10)

private func nextMultiPartBody(_ generator: inout IndexingIterator<[UInt8]>, boundary: String) -> [UInt8]? {
var body = [UInt8]()
let boundaryArray = [UInt8](boundary.utf8)
Expand Down
2 changes: 1 addition & 1 deletion XCode/Sources/HttpResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public protocol HttpResponseBodyWriter {
}

public enum HttpResponseBody {

case json(Any)
case html(String)
case htmlBody(String)
Expand Down
Loading

0 comments on commit 27b6901

Please sign in to comment.