Damian Kołakowski пре 10 година
родитељ
комит
dcb5e059ff

+ 0 - 22
Sources/Const.swift

@@ -1,22 +0,0 @@
-//
-//  Const.swift
-//  Swifter
-//
-//  Created by Damian Kolakowski on 17/12/15.
-//  Copyright © 2015 Damian Kołakowski. All rights reserved.
-//
-
-import Foundation
-#if os(Linux)
-    import Glibc
-    import NSLinux
-#endif
-
-struct Constants {
-    
-    static let VERSION      = "1.0.6"
-    static let DEFAULT_PORT = in_port_t(8080)
-    
-    static let CR           = UInt8(13)
-    static let NL           = UInt8(10)
-}

+ 1 - 1
Sources/DemoServer.swift

@@ -57,7 +57,7 @@ public func demoServer(publicDir: String?) -> HttpServer {
     
     server.POST["/upload"] = { r in
         let formFields = r.parseMultiPartFormData()
-        return HttpResponse.OK(.Html(formFields.map({ UInt8ArrayToUTF8String($0.body) }).joinWithSeparator("<br>")))
+        return HttpResponse.OK(.Html(formFields.map({ String.fromUInt8($0.body) }).joinWithSeparator("<br>")))
     }
     
     server.GET["/login"] = { r in

+ 8 - 5
Sources/HttpRequest.swift

@@ -24,7 +24,7 @@ public struct HttpRequest {
         guard let contentType = contentTypeHeaderTokens.first where contentType == "application/x-www-form-urlencoded" else {
             return []
         }
-        return UInt8ArrayToUTF8String(body).split("&").map { (param: String) -> (String, String) in
+        return String.fromUInt8(body).split("&").map { (param: String) -> (String, String) in
             let tokens = param.split("=")
             if let name = tokens.first, value = tokens.last where tokens.count == 2 {
                 return (name.replace("+", new: " ").removePercentEncoding(),
@@ -93,16 +93,19 @@ public struct HttpRequest {
     private func nextMultiPartLine(inout generator: IndexingGenerator<[UInt8]>) -> String? {
         var result = String()
         while let value = generator.next() {
-            if value > Constants.CR {
+            if value > HttpRequest.CR {
                 result.append(Character(UnicodeScalar(value)))
             }
-            if value == Constants.NL {
+            if value == HttpRequest.NL {
                 break
             }
         }
         return result
     }
     
+    static let CR = UInt8(13)
+    static let NL = UInt8(10)
+    
     private func nextMultiPartBody(inout generator: IndexingGenerator<[UInt8]>, boundary: String) -> [UInt8]? {
         var body = [UInt8]()
         let boundaryArray = [UInt8](boundary.utf8)
@@ -112,9 +115,9 @@ public struct HttpRequest {
             body.append(x)
             if matchOffset == boundaryArray.count {
                 body.removeRange(Range<Int>(start: body.count-matchOffset, end: body.count))
-                if body.last == Constants.NL {
+                if body.last == HttpRequest.NL {
                     body.removeLast()
-                    if body.last == Constants.CR {
+                    if body.last == HttpRequest.CR {
                         body.removeLast()
                     }
                 }

+ 1 - 1
Sources/HttpResponse.swift

@@ -82,7 +82,7 @@ public enum HttpResponse {
     }
     
     func headers() -> [String: String] {
-        var headers = ["Server" : "Swifter \(Constants.VERSION)"]
+        var headers = ["Server" : "Swifter \(HttpServer.VERSION)"]
         switch self {
         case .OK(let body):
             switch body {

+ 3 - 1
Sources/HttpServer.swift

@@ -10,6 +10,8 @@ import Foundation
 
 public class HttpServer: HttpServerIO {
     
+    public static let VERSION = "1.0.6"
+    
     public override init() { }
     
     private let router = HttpRouter()
@@ -55,7 +57,7 @@ public class HttpServer: HttpServerIO {
         return Route(method: method, router: self.router)
     }
     
-    override public func select(method: String, url: String) -> ([String : String], HttpRequest -> HttpResponse) {
+    override public func select(method: String, url: String) -> ([String:String], HttpRequest -> HttpResponse) {
         if let handler = router.select(method, url: url) {
             return handler
         }

+ 1 - 1
Sources/HttpServerIO.swift

@@ -17,7 +17,7 @@ public class HttpServerIO {
     private var clientSockets: Set<Socket> = []
     private let clientSocketsLock = NSLock()
     
-    public func start(listenPort: in_port_t = Constants.DEFAULT_PORT) throws {
+    public func start(listenPort: in_port_t = 8080) throws {
         stop()
         listenSocket = try Socket.tcpSocketForListen(listenPort)
         dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0)) {

+ 5 - 2
Sources/Socket.swift

@@ -136,13 +136,16 @@ public class Socket: Hashable, Equatable {
         return buffer[0]
     }
     
+    private static let CR = UInt8(13)
+    private static let NL = UInt8(10)
+    
     public func readLine() throws -> String {
         var characters: String = ""
         var n: UInt8 = 0
         repeat {
             n = try self.read()
-            if n > Constants.CR { characters.append(Character(UnicodeScalar(n))) }
-        } while n != Constants.NL
+            if n > Socket.CR { characters.append(Character(UnicodeScalar(n))) }
+        } while n != Socket.NL
         return characters
     }
     

+ 14 - 14
Sources/String+Misc.swift

@@ -25,6 +25,17 @@ extension String {
         return String(scalars)
     }
     
+    public static func fromUInt8(array: [UInt8]) -> String {
+        #if os(Linux)
+            return String(data: NSData(bytes: array, length: array.count), encoding: NSUTF8StringEncoding)
+        #else
+            if let s = String(data: NSData(bytes: array, length: array.count), encoding: NSUTF8StringEncoding) {
+                return s
+            }
+            return ""
+        #endif
+    }
+    
     public func removePercentEncoding() -> String {
         var scalars = self.unicodeScalars
         var output = ""
@@ -37,7 +48,7 @@ extension String {
                     bytesBuffer.append(first*16+secon)
                 } else {
                     if !bytesBuffer.isEmpty {
-                        output.appendContentsOf(UInt8ArrayToUTF8String(bytesBuffer))
+                        output.appendContentsOf(String.fromUInt8(bytesBuffer))
                         bytesBuffer.removeAll()
                     }
                     if let first = first { output.append(Character(first)) }
@@ -45,14 +56,14 @@ extension String {
                 }
             } else {
                 if !bytesBuffer.isEmpty {
-                    output.appendContentsOf(UInt8ArrayToUTF8String(bytesBuffer))
+                    output.appendContentsOf(String.fromUInt8(bytesBuffer))
                     bytesBuffer.removeAll()
                 }
                 output.append(Character(scalar))
             }
         }
         if !bytesBuffer.isEmpty {
-            output.appendContentsOf(UInt8ArrayToUTF8String(bytesBuffer))
+            output.appendContentsOf(String.fromUInt8(bytesBuffer))
             bytesBuffer.removeAll()
         }
         return output
@@ -85,14 +96,3 @@ extension String {
         return nil
     }
 }
-
-public func UInt8ArrayToUTF8String(array: [UInt8]) -> String {
-    #if os(Linux)
-        return String(data: NSData(bytes: array, length: array.count), encoding: NSUTF8StringEncoding)
-    #else
-        if let s = String(data: NSData(bytes: array, length: array.count), encoding: NSUTF8StringEncoding) {
-            return s
-        }
-        return ""
-    #endif
-}

+ 0 - 6
Swifter.xcodeproj/project.pbxproj

@@ -14,7 +14,6 @@
 		7C71C5B11A1EC49B00682BF0 /* logo.png in CopyFiles */ = {isa = PBXBuildFile; fileRef = 7CB102DF1A17381D00CBA3B4 /* logo.png */; };
 		7C73C6911C2615FE00AEF6CA /* SwiftyJSON.swift in Sources */ = {isa = PBXBuildFile; fileRef = 18E610A51BD6397D00B7D17A /* SwiftyJSON.swift */; };
 		7C73C6921C26179C00AEF6CA /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7CDAB80C1BE2A1D400C8A977 /* AppDelegate.swift */; };
-		7C73C6A91C261A2100AEF6CA /* Const.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C73C6931C2619E100AEF6CA /* Const.swift */; };
 		7C73C6AA1C261A2100AEF6CA /* DemoServer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C73C6941C2619E100AEF6CA /* DemoServer.swift */; };
 		7C73C6AB1C261A2100AEF6CA /* HttpHandlers.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C73C6951C2619E100AEF6CA /* HttpHandlers.swift */; };
 		7C73C6AC1C261A2100AEF6CA /* HttpParser.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C73C6961C2619E100AEF6CA /* HttpParser.swift */; };
@@ -25,7 +24,6 @@
 		7C73C6B11C261A2100AEF6CA /* HttpServerIO.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C73C69B1C2619E100AEF6CA /* HttpServerIO.swift */; };
 		7C73C6B21C261A2100AEF6CA /* Socket.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C73C69C1C2619E100AEF6CA /* Socket.swift */; };
 		7C73C6B31C261A2100AEF6CA /* String+Misc.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C73C69D1C2619E100AEF6CA /* String+Misc.swift */; };
-		7C73C6B41C261A2600AEF6CA /* Const.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C73C6931C2619E100AEF6CA /* Const.swift */; };
 		7C73C6B51C261A2600AEF6CA /* DemoServer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C73C6941C2619E100AEF6CA /* DemoServer.swift */; };
 		7C73C6B61C261A2600AEF6CA /* HttpHandlers.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C73C6951C2619E100AEF6CA /* HttpHandlers.swift */; };
 		7C73C6B71C261A2600AEF6CA /* HttpParser.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C73C6961C2619E100AEF6CA /* HttpParser.swift */; };
@@ -90,7 +88,6 @@
 		7AE893FD1C0512C400A29F63 /* SwifterMac.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SwifterMac.h; sourceTree = "<group>"; };
 		7AE893FF1C0512C400A29F63 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
 		7AE8940C1C05151100A29F63 /* Launch Screen.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; path = "Launch Screen.storyboard"; sourceTree = "<group>"; };
-		7C73C6931C2619E100AEF6CA /* Const.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Const.swift; sourceTree = "<group>"; };
 		7C73C6941C2619E100AEF6CA /* DemoServer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DemoServer.swift; sourceTree = "<group>"; };
 		7C73C6951C2619E100AEF6CA /* HttpHandlers.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HttpHandlers.swift; sourceTree = "<group>"; };
 		7C73C6961C2619E100AEF6CA /* HttpParser.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HttpParser.swift; sourceTree = "<group>"; };
@@ -227,7 +224,6 @@
 		7CEAF84A1C14B29B003252DE /* Sources */ = {
 			isa = PBXGroup;
 			children = (
-				7C73C6931C2619E100AEF6CA /* Const.swift */,
 				7C73C6941C2619E100AEF6CA /* DemoServer.swift */,
 				7C73C6951C2619E100AEF6CA /* HttpHandlers.swift */,
 				7C73C6961C2619E100AEF6CA /* HttpParser.swift */,
@@ -417,7 +413,6 @@
 			isa = PBXSourcesBuildPhase;
 			buildActionMask = 2147483647;
 			files = (
-				7C73C6A91C261A2100AEF6CA /* Const.swift in Sources */,
 				7C73C6AA1C261A2100AEF6CA /* DemoServer.swift in Sources */,
 				7C73C6AB1C261A2100AEF6CA /* HttpHandlers.swift in Sources */,
 				7C73C6AC1C261A2100AEF6CA /* HttpParser.swift in Sources */,
@@ -435,7 +430,6 @@
 			isa = PBXSourcesBuildPhase;
 			buildActionMask = 2147483647;
 			files = (
-				7C73C6B41C261A2600AEF6CA /* Const.swift in Sources */,
 				7C73C6B51C261A2600AEF6CA /* DemoServer.swift in Sources */,
 				7C73C6B61C261A2600AEF6CA /* HttpHandlers.swift in Sources */,
 				7C73C6B71C261A2600AEF6CA /* HttpParser.swift in Sources */,

BIN
Swifter.xcodeproj/project.xcworkspace/xcuserdata/damiankolakowski.xcuserdatad/UserInterfaceState.xcuserstate