Quellcode durchsuchen

Swifter compiles on Ubuntu.

Damian Kołakowski vor 10 Jahren
Ursprung
Commit
90261ad9ba
3 geänderte Dateien mit 24 neuen und 31 gelöschten Zeilen
  1. 19 26
      Sources/Swifter/HttpResponse.swift
  2. 3 3
      Sources/Swifter/HttpServer.swift
  3. 2 2
      Sources/Swifter/Socket.swift

+ 19 - 26
Sources/Swifter/HttpResponse.swift

@@ -24,11 +24,7 @@ public class JSONSerializer: Serializer {
         
         let json = try NSJSONSerialization.dataWithJSONObject(obj, options: NSJSONWritingOptions.PrettyPrinted)
         
-        guard let string = String(data: json, encoding: NSUTF8StringEncoding) else {
-            throw SerializationError.EncodingError
-        }
-        
-        return string
+        return String(json)
     }
     
     private static func serialize(object: Any) throws -> String {
@@ -58,11 +54,9 @@ public class PLISTSerializer: Serializer {
         
         let plist = try NSPropertyListSerialization.dataWithPropertyList(obj, format: format, options: 0)
         
-        guard let string = String(data: plist, encoding: NSUTF8StringEncoding) else {
-            throw SerializationError.EncodingError
-        }
         
-        return string
+        
+        return String(plist)
     }
     
     private static func serialize(object: Any) throws -> String {
@@ -150,18 +144,18 @@ public enum HttpResponse {
         var headers = [String:String]()
         headers["Server"] = "Swifter \(HttpServer.VERSION)"
         switch self {
-		case .OK(let body):
+        case .OK(let body):
             switch body {
-                case .Json(_)   : headers["Content-Type"] = "application/json"
-                case .Plist(_)  : headers["Content-Type"] = "application/xml"
-                case .Xml(_)    : headers["Content-Type"] = "application/xml"
+            case .Json(_)   : headers["Content-Type"] = "application/json"
+            case .Plist(_)  : headers["Content-Type"] = "application/xml"
+            case .Xml(_)    : headers["Content-Type"] = "application/xml"
                 // 'application/xml' or 'text/xml' ?
-                // From RFC: http://www.rfc-editor.org/rfc/rfc3023.txt - "If an XML document -- that is, the unprocessed, 
-                // source XML document -- is readable by casual users, text/xml is preferable to application/xml. 
-                // MIME user agents (and web user agents) that do not have explicit support for text/xml will treat it as text/plain, 
-                // for example, by displaying the XML MIME entity as plain text.
-                case .Html(_)   : headers["Content-Type"] = "text/html"
-                default:break
+                // From RFC: http://www.rfc-editor.org/rfc/rfc3023.txt - "If an XML document -- that is, the unprocessed,
+                // source XML document -- is readable by casual users, text/xml is preferable to application/xml.
+                // MIME user agents (and web user agents) that do not have explicit support for text/xml will treat it as text/plain,
+            // for example, by displaying the XML MIME entity as plain text.
+            case .Html(_)   : headers["Content-Type"] = "text/html"
+            default:break
             }
         case .MovedPermanently(let location): headers["Location"] = location
         case .RAW(_, _, let rawHeaders, _):
@@ -189,14 +183,13 @@ public enum HttpResponse {
 	ignores any associated values. This should generally be what
 	you want. E.g.:
 	
-		let resp = handler(updatedRequest)
-		if resp == .NotFound {
-			print("Client requested not found: \(request.url)")
-		}
-*/
+ let resp = handler(updatedRequest)
+ if resp == .NotFound {
+ print("Client requested not found: \(request.url)")
+ }
+ */
 
 func ==(inLeft: HttpResponse, inRight: HttpResponse) -> Bool {
-	return inLeft.statusCode() == inRight.statusCode()
+    return inLeft.statusCode() == inRight.statusCode()
 }
 
-

+ 3 - 3
Sources/Swifter/HttpServer.swift

@@ -42,7 +42,7 @@ public class HttpServer {
         return router.routes()
     }
     
-    public func start(listenPort: in_port_t = 8080) throws {
+    public func start(listenPort: UInt16 = 8080) throws {
         stop()
         listenSocket = try Socket.tcpSocketForListen(listenPort)
         dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0)) {
@@ -79,7 +79,7 @@ public class HttpServer {
             self.stop()
         }
     }
-
+    
     public func stop() {
         listenSocket.release()
         HttpServer.lock(self.clientSocketsLock) {
@@ -113,4 +113,4 @@ public class HttpServer {
             try socket.writeUInt8(body)
         }
     }
-}
+}

+ 2 - 2
Sources/Swifter/Socket.swift

@@ -27,7 +27,7 @@ enum SocketError: ErrorType {
 
 public class Socket: Hashable, Equatable {
     
-    public class func tcpSocketForListen(port: in_port_t = 8080, maxPendingConnection: Int32 = SOMAXCONN) throws -> Socket {
+    public class func tcpSocketForListen(port: UInt16 = 8080, maxPendingConnection: Int32 = SOMAXCONN) throws -> Socket {
         
         #if os(Linux)
             let socketFileDescriptor = socket(AF_INET, Int32(SOCK_STREAM.rawValue), 0)
@@ -214,4 +214,4 @@ public class Socket: Hashable, Equatable {
 
 public func ==(socket1: Socket, socket2: Socket) -> Bool {
     return socket1.socketFileDescriptor == socket2.socketFileDescriptor
-}
+}