Jelajahi Sumber

'unicodeScalarTo___' methods moved to a new UInt8 extension.

Damian Kołakowski 10 tahun lalu
induk
melakukan
a713cccc27

+ 27 - 27
Sources/HttpHandlers+WebSockets.swift

@@ -9,8 +9,9 @@ import Foundation
 
 extension HttpHandlers {
     
-    public class func websocket(text:((WebSocketSession, String) -> Void)?, _ binary:
-        ((WebSocketSession, [UInt8]) -> Void)?) -> (HttpRequest -> HttpResponse) {
+    public class func websocket(
+            text: ((WebSocketSession, String) -> Void)?,
+        _ binary: ((WebSocketSession, [UInt8]) -> Void)?) -> (HttpRequest -> HttpResponse) {
         return { r in
             guard r.headers["upgrade"] == "websocket" else {
                 return .BadRequest(.Text("Invalid value of 'Upgrade' header: \(r.headers["upgrade"])"))
@@ -25,11 +26,11 @@ extension HttpHandlers {
                 let session = WebSocketSession(socket)
                 while let frame = try? session.readFrame(socket) {
                     switch frame.opcode {
-                    case .TEXT:
+                    case .Text:
                         if let handleText = text {
                             handleText(session, String.fromUInt8(frame.payload))
                         }
-                    case .BINARY:
+                    case .Binary:
                         if let handleBinary = binary {
                             handleBinary(session, frame.payload)
                         }
@@ -37,7 +38,7 @@ extension HttpHandlers {
                     }
                 }
             }
-            let secWebSocketAccept = String.encodeToBase64((secWebSocketKey + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11").SHA1())
+            let secWebSocketAccept = String.toBase64((secWebSocketKey + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11").SHA1())
             let headers = [ "Upgrade": "WebSocket", "Connection": "Upgrade", "Sec-WebSocket-Accept": secWebSocketAccept]
             return HttpResponse.SwitchProtocols(headers, protocolSessionClosure)
         }
@@ -46,11 +47,10 @@ extension HttpHandlers {
     public class WebSocketSession {
         
         public enum Error: ErrorType { case UnknownOpCode(String), UnMaskedFrame }
-        
-        public enum OpCode { case CONTINUE, CLOSE, PING, PONG, TEXT, BINARY }
+        public enum OpCode { case Continue, Close, Ping, Pong, Text, Binary }
         
         public class Frame {
-            public var opcode = OpCode.CLOSE
+            public var opcode = OpCode.Close
             public var fin = false
             public var payload = [UInt8]()
         }
@@ -62,11 +62,11 @@ extension HttpHandlers {
         }
         
         public func writeText(text: String) -> Void {
-            self.writeFrame([UInt8](text.utf8), OpCode.TEXT)
+            self.writeFrame([UInt8](text.utf8), OpCode.Text)
         }
     
         public func writeBinary(binary: [UInt8]) -> Void {
-            self.writeFrame(binary, OpCode.BINARY)
+            self.writeFrame(binary, OpCode.Binary)
         }
         
         private func writeFrame(data: [UInt8], _ op: OpCode, _ fin: Bool = true) {
@@ -84,12 +84,12 @@ extension HttpHandlers {
         private func encodeFinAndOpCode(fin: Bool, op: OpCode) -> UInt8 {
             var encodedByte = UInt8(fin ? 0x80 : 0x00);
             switch op {
-            case .CONTINUE : encodedByte |= 0x00 & 0x0F;
-            case .TEXT     : encodedByte |= 0x01 & 0x0F;
-            case .BINARY   : encodedByte |= 0x02 & 0x0F;
-            case .CLOSE    : encodedByte |= 0x08 & 0x0F;
-            case .PING     : encodedByte |= 0x09 & 0x0F;
-            case .PONG     : encodedByte |= 0x0A & 0x0F;
+            case .Continue : encodedByte |= 0x00 & 0x0F;
+            case .Text     : encodedByte |= 0x01 & 0x0F;
+            case .Binary   : encodedByte |= 0x02 & 0x0F;
+            case .Close    : encodedByte |= 0x08 & 0x0F;
+            case .Ping     : encodedByte |= 0x09 & 0x0F;
+            case .Pong     : encodedByte |= 0x0A & 0x0F;
             }
             return encodedByte
         }
@@ -98,13 +98,13 @@ extension HttpHandlers {
             let encodedLngth = UInt8(masked ? 0x80 : 0x00)
             var encodedBytes = [UInt8]()
             switch len {
-            case 0...0x7D:
+            case 0...125:
                 encodedBytes.append(encodedLngth | UInt8(len));
-            case 0x7E...0xFF_FF:
+            case 126...UInt64(UINT16_MAX):
                 encodedBytes.append(encodedLngth | 0x7E);
                 encodedBytes.append(UInt8(len >> 8));
                 encodedBytes.append(UInt8(len & 0xFF));
-            default:
+            case UInt64(UINT16_MAX)+1...UINT64_MAX:
                 encodedBytes.append(encodedLngth | 0x7F);
                 encodedBytes.append(UInt8(len >> 56) & 0xFF);
                 encodedBytes.append(UInt8(len >> 48) & 0xFF);
@@ -112,8 +112,8 @@ extension HttpHandlers {
                 encodedBytes.append(UInt8(len >> 32) & 0xFF);
                 encodedBytes.append(UInt8(len >> 24) & 0xFF);
                 encodedBytes.append(UInt8(len >> 16) & 0xFF);
-                encodedBytes.append(UInt8(len >> 8 ) & 0xFF);
-                encodedBytes.append(UInt8(len & 0xFF));
+                encodedBytes.append(UInt8(len >> 08) & 0xFF);
+                encodedBytes.append(UInt8(len >> 00) & 0xFF);
             }
             return encodedBytes
         }
@@ -124,12 +124,12 @@ extension HttpHandlers {
             frm.fin = fst & 0x80 != 0
             let opc = fst & 0x0F
             switch opc {
-                case 0x00: frm.opcode = OpCode.CONTINUE
-                case 0x01: frm.opcode = OpCode.TEXT
-                case 0x02: frm.opcode = OpCode.BINARY
-                case 0x08: frm.opcode = OpCode.CLOSE
-                case 0x09: frm.opcode = OpCode.PING
-                case 0x0A: frm.opcode = OpCode.PONG
+                case 0x00: frm.opcode = OpCode.Continue
+                case 0x01: frm.opcode = OpCode.Text
+                case 0x02: frm.opcode = OpCode.Binary
+                case 0x08: frm.opcode = OpCode.Close
+                case 0x09: frm.opcode = OpCode.Ping
+                case 0x0A: frm.opcode = OpCode.Pong
                 // "If an unknown opcode is received, the receiving endpoint MUST _Fail the WebSocket Connection_."
                 // http://tools.ietf.org/html/rfc6455#section-5.2 ( Page 29 )
                 default  : throw Error.UnknownOpCode("\(opc)")

+ 8 - 0
Sources/HttpServer.swift

@@ -20,9 +20,17 @@ public class HttpServer: HttpServerIO {
         self.POST   = MethodRoute(method: "POST", router: router)
         self.GET    = MethodRoute(method: "GET", router: router)
         self.PUT    = MethodRoute(method: "PUT", router: router)
+        
+        self.delete = MethodRoute(method: "DELETE", router: router)
+        self.update = MethodRoute(method: "UPDATE", router: router)
+        self.head   = MethodRoute(method: "HEAD", router: router)
+        self.post   = MethodRoute(method: "POST", router: router)
+        self.get    = MethodRoute(method: "GET", router: router)
+        self.put    = MethodRoute(method: "PUT", router: router)
     }
     
     public var DELETE, UPDATE, HEAD, POST, GET, PUT : MethodRoute;
+    public var delete, update, head, post, get, put : MethodRoute;
 
     public subscript(path: String) -> (HttpRequest -> HttpResponse)? {
         set {

+ 1 - 1
Sources/String+BASE64.swift

@@ -16,7 +16,7 @@ extension String {
     
     private static let CODES = [UInt8]("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=".utf8)
     
-    public static func encodeToBase64(data: [UInt8]) -> String {
+    public static func toBase64(data: [UInt8]) -> String {
         
         // Based on: https://en.wikipedia.org/wiki/Base64#Sample_Implementation_in_Java
         

+ 23 - 23
Sources/String+Misc.swift

@@ -39,13 +39,14 @@ extension String {
     
     public func trim() -> String {
         var scalars = self.unicodeScalars
-        while let _ = unicodeScalarToUInt32Whitespace(scalars.first) { scalars.removeFirst() }
-        while let _ = unicodeScalarToUInt32Whitespace(scalars.last) { scalars.removeLast() }
+        while let _ = scalars.first?.asWhitespace() { scalars.removeFirst() }
+        while let _ = scalars.last?.asWhitespace() { scalars.removeLast() }
         return String(scalars)
     }
     
     public static func fromUInt8(array: [UInt8]) -> String {
-	return String(array)
+        // Apple changes the definition of String(data: .... ) every realse so let's stay with 'fromUInt8(...)' wrapper.
+        return String(array) // It works for 2.2 and 3.0-dev (25 Jan 2016).
     }
     
     public func removePercentEncoding() -> String {
@@ -56,7 +57,7 @@ extension String {
             if scalar == "%" {
                 let first = scalars.popFirst()
                 let secon = scalars.popFirst()
-                if let first = unicodeScalarToUInt32Hex(first), secon = unicodeScalarToUInt32Hex(secon) {
+                if let first = first?.asAlpha(), secon = secon?.asAlpha() {
                     bytesBuffer.append(first*16+secon)
                 } else {
                     if !bytesBuffer.isEmpty {
@@ -80,30 +81,29 @@ extension String {
         }
         return output
     }
+}
+
+extension UnicodeScalar {
     
-    private func unicodeScalarToUInt32Whitespace(x: UnicodeScalar?) -> UInt8? {
-        if let x = x {
-            if x.value >= 9 && x.value <= 13 {
-                return UInt8(x.value)
-            }
-            if x.value == 32 {
-                return UInt8(x.value)
-            }
+    public func asWhitespace() -> UInt8? {
+        if self.value >= 9 && self.value <= 13 {
+            return UInt8(self.value)
+        }
+        if self.value == 32 {
+            return UInt8(self.value)
         }
         return nil
     }
     
-    private func unicodeScalarToUInt32Hex(x: UnicodeScalar?) -> UInt8? {
-        if let x = x {
-            if x.value >= 48 && x.value <= 57 {
-                return UInt8(x.value) - 48
-            }
-            if x.value >= 97 && x.value <= 102 {
-                return UInt8(x.value) - 87
-            }
-            if x.value >= 65 && x.value <= 70 {
-                return UInt8(x.value) - 55
-            }
+    public func asAlpha() -> UInt8? {
+        if self.value >= 48 && self.value <= 57 {
+            return UInt8(self.value) - 48
+        }
+        if self.value >= 97 && self.value <= 102 {
+            return UInt8(self.value) - 87
+        }
+        if self.value >= 65 && self.value <= 70 {
+            return UInt8(self.value) - 55
         }
         return nil
     }

TEMPAT SAMPAH
Swifter.xcodeproj/project.xcworkspace/xcuserdata/damiankolakowski.xcuserdatad/UserInterfaceState.xcuserstate


+ 119 - 23
Swifter.xcodeproj/xcuserdata/damiankolakowski.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist

@@ -1653,11 +1653,11 @@
             ignoreCount = "0"
             continueAfterRunningActions = "No"
             filePath = "SwifterSampleOSX/main.swift"
-            timestampString = "475884681.54644"
+            timestampString = "475886051.685009"
             startingColumnNumber = "9223372036854775807"
             endingColumnNumber = "9223372036854775807"
-            startingLineNumber = "21"
-            endingLineNumber = "21">
+            startingLineNumber = "22"
+            endingLineNumber = "22">
          </BreakpointContent>
       </BreakpointProxy>
       <BreakpointProxy
@@ -1699,11 +1699,11 @@
             ignoreCount = "0"
             continueAfterRunningActions = "No"
             filePath = "Sources/HttpHandlers+WebSockets.swift"
-            timestampString = "475885394.646408"
+            timestampString = "476047570.714406"
             startingColumnNumber = "9223372036854775807"
             endingColumnNumber = "9223372036854775807"
-            startingLineNumber = "155"
-            endingLineNumber = "155"
+            startingLineNumber = "154"
+            endingLineNumber = "154"
             landmarkName = "readFrame(_:)"
             landmarkType = "5">
             <Locations>
@@ -1747,11 +1747,11 @@
             ignoreCount = "0"
             continueAfterRunningActions = "No"
             filePath = "Sources/HttpHandlers+WebSockets.swift"
-            timestampString = "475885394.646408"
+            timestampString = "476047570.714406"
             startingColumnNumber = "9223372036854775807"
             endingColumnNumber = "9223372036854775807"
-            startingLineNumber = "161"
-            endingLineNumber = "161"
+            startingLineNumber = "160"
+            endingLineNumber = "160"
             landmarkName = "readFrame(_:)"
             landmarkType = "5">
          </BreakpointContent>
@@ -1763,11 +1763,11 @@
             ignoreCount = "0"
             continueAfterRunningActions = "No"
             filePath = "Sources/HttpHandlers+WebSockets.swift"
-            timestampString = "475885394.646408"
+            timestampString = "476047570.714406"
             startingColumnNumber = "9223372036854775807"
             endingColumnNumber = "9223372036854775807"
-            startingLineNumber = "154"
-            endingLineNumber = "154"
+            startingLineNumber = "153"
+            endingLineNumber = "153"
             landmarkName = "readFrame(_:)"
             landmarkType = "5">
          </BreakpointContent>
@@ -1811,12 +1811,12 @@
             ignoreCount = "0"
             continueAfterRunningActions = "No"
             filePath = "Sources/String+Misc.swift"
-            timestampString = "475104585.734509"
+            timestampString = "476044129.590475"
             startingColumnNumber = "9223372036854775807"
             endingColumnNumber = "9223372036854775807"
             startingLineNumber = "37"
             endingLineNumber = "37"
-            landmarkName = "trim()"
+            landmarkName = "unquote()"
             landmarkType = "5">
          </BreakpointContent>
       </BreakpointProxy>
@@ -1939,11 +1939,11 @@
             ignoreCount = "0"
             continueAfterRunningActions = "No"
             filePath = "Sources/HttpHandlers+WebSockets.swift"
-            timestampString = "475885394.646408"
+            timestampString = "476047570.714406"
             startingColumnNumber = "9223372036854775807"
             endingColumnNumber = "9223372036854775807"
-            startingLineNumber = "153"
-            endingLineNumber = "153"
+            startingLineNumber = "152"
+            endingLineNumber = "152"
             landmarkName = "readFrame(_:)"
             landmarkType = "5">
          </BreakpointContent>
@@ -2193,11 +2193,11 @@
             ignoreCount = "0"
             continueAfterRunningActions = "No"
             filePath = "SwifterSampleOSX/main.swift"
-            timestampString = "475884681.54644"
+            timestampString = "475886051.685009"
             startingColumnNumber = "9223372036854775807"
             endingColumnNumber = "9223372036854775807"
-            startingLineNumber = "14"
-            endingLineNumber = "14">
+            startingLineNumber = "15"
+            endingLineNumber = "15">
          </BreakpointContent>
       </BreakpointProxy>
       <BreakpointProxy
@@ -2308,8 +2308,8 @@
             endingColumnNumber = "9223372036854775807"
             startingLineNumber = "16"
             endingLineNumber = "16"
-            landmarkName = "encodeToBase64(_:)"
-            landmarkType = "5">
+            landmarkName = "String"
+            landmarkType = "3">
          </BreakpointContent>
       </BreakpointProxy>
       <BreakpointProxy
@@ -2324,7 +2324,7 @@
             endingColumnNumber = "9223372036854775807"
             startingLineNumber = "40"
             endingLineNumber = "40"
-            landmarkName = "encodeToBase64(_:)"
+            landmarkName = "toBase64(_:)"
             landmarkType = "5">
          </BreakpointContent>
       </BreakpointProxy>
@@ -2360,5 +2360,101 @@
             landmarkType = "5">
          </BreakpointContent>
       </BreakpointProxy>
+      <BreakpointProxy
+         BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
+         <BreakpointContent
+            shouldBeEnabled = "No"
+            ignoreCount = "0"
+            continueAfterRunningActions = "No"
+            filePath = "Sources/HttpHandlers+WebSockets.swift"
+            timestampString = "476047570.714406"
+            startingColumnNumber = "9223372036854775807"
+            endingColumnNumber = "9223372036854775807"
+            startingLineNumber = "16"
+            endingLineNumber = "16"
+            landmarkName = "websocket(_:_:)"
+            landmarkType = "5">
+         </BreakpointContent>
+      </BreakpointProxy>
+      <BreakpointProxy
+         BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
+         <BreakpointContent
+            shouldBeEnabled = "No"
+            ignoreCount = "0"
+            continueAfterRunningActions = "No"
+            filePath = "Sources/HttpParser.swift"
+            timestampString = "476043885.290607"
+            startingColumnNumber = "9223372036854775807"
+            endingColumnNumber = "9223372036854775807"
+            startingLineNumber = "26"
+            endingLineNumber = "26"
+            landmarkName = "readHttpRequest(_:)"
+            landmarkType = "5">
+         </BreakpointContent>
+      </BreakpointProxy>
+      <BreakpointProxy
+         BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
+         <BreakpointContent
+            shouldBeEnabled = "No"
+            ignoreCount = "0"
+            continueAfterRunningActions = "No"
+            filePath = "Sources/HttpHandlers+WebSockets.swift"
+            timestampString = "476047570.714406"
+            startingColumnNumber = "9223372036854775807"
+            endingColumnNumber = "9223372036854775807"
+            startingLineNumber = "25"
+            endingLineNumber = "25"
+            landmarkName = "websocket(_:_:)"
+            landmarkType = "5">
+         </BreakpointContent>
+      </BreakpointProxy>
+      <BreakpointProxy
+         BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
+         <BreakpointContent
+            shouldBeEnabled = "No"
+            ignoreCount = "0"
+            continueAfterRunningActions = "No"
+            filePath = "Sources/HttpHandlers+WebSockets.swift"
+            timestampString = "476047570.714406"
+            startingColumnNumber = "9223372036854775807"
+            endingColumnNumber = "9223372036854775807"
+            startingLineNumber = "122"
+            endingLineNumber = "122"
+            landmarkName = "readFrame(_:)"
+            landmarkType = "5">
+         </BreakpointContent>
+      </BreakpointProxy>
+      <BreakpointProxy
+         BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
+         <BreakpointContent
+            shouldBeEnabled = "No"
+            ignoreCount = "0"
+            continueAfterRunningActions = "No"
+            filePath = "Sources/HttpHandlers+WebSockets.swift"
+            timestampString = "476047570.714406"
+            startingColumnNumber = "9223372036854775807"
+            endingColumnNumber = "9223372036854775807"
+            startingLineNumber = "125"
+            endingLineNumber = "125"
+            landmarkName = "readFrame(_:)"
+            landmarkType = "5">
+         </BreakpointContent>
+      </BreakpointProxy>
+      <BreakpointProxy
+         BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
+         <BreakpointContent
+            shouldBeEnabled = "No"
+            ignoreCount = "0"
+            continueAfterRunningActions = "No"
+            filePath = "Sources/Socket.swift"
+            timestampString = "476044365.050784"
+            startingColumnNumber = "9223372036854775807"
+            endingColumnNumber = "9223372036854775807"
+            startingLineNumber = "132"
+            endingLineNumber = "132"
+            landmarkName = "read()"
+            landmarkType = "5">
+         </BreakpointContent>
+      </BreakpointProxy>
    </Breakpoints>
 </Bucket>

+ 0 - 1
SwifterSampleOSX/main.swift

@@ -7,7 +7,6 @@
 import Foundation
 import Swifter
 
-
 let server = demoServer(NSBundle.mainBundle().resourcePath!)
 
 do {