Переглянути джерело

Merge branch 'stable' of github.com:glock45/swifter into stable

Damian Kołakowski 10 роки тому
батько
коміт
e509da0276

+ 2 - 2
Sources/DemoServer.swift

@@ -154,7 +154,7 @@ public func demoServer(publicDir: String) -> HttpServer {
     }
     
     server["/raw"] = { r in
-        return HttpResponse.RAW(200, "OK", ["XXX-Custom-Header": "value"], { $0.write([UInt8]("test".utf8)) })
+        return HttpResponse.RAW(200, "OK", ["XXX-Custom-Header": "value"], { try $0.write([UInt8]("test".utf8)) })
     }
     
     server["/redirect"] = { r in
@@ -174,7 +174,7 @@ public func demoServer(publicDir: String) -> HttpServer {
     server["/stream"] = { r in
         return HttpResponse.RAW(200, "OK", nil, { w in
             for i in 0...100 {
-                w.write([UInt8]("[chunk \(i)]".utf8))
+                try w.write([UInt8]("[chunk \(i)]".utf8))
             }
         })
     }

+ 4 - 4
Sources/Files.swift

@@ -21,8 +21,8 @@ public func shareFilesFromDirectory(directoryPath: String) -> (HttpRequest -> Ht
             return .NotFound
         }
         return .RAW(200, "OK", [:], { writer in
-            writer.write(file)
-            file.close()
+            defer { file.close() }
+            try writer.write(file)
         })
     }
 }
@@ -60,8 +60,8 @@ public func directoryBrowser(dir: String) -> (HttpRequest -> HttpResponse) {
                     return .NotFound
                 }
                 return .RAW(200, "OK", [:], { writer in
-                    writer.write(file)
-                    file.close()
+                    defer { file.close() }
+                    try writer.write(file)
                 })
             }
         } catch {

+ 11 - 11
Sources/HttpResponse.swift

@@ -17,9 +17,9 @@ public enum SerializationError: ErrorType {
 }
 
 public protocol HttpResponseBodyWriter {
-    func write(file: File)
-    func write(data: [UInt8])
-    func write(data: ArraySlice<UInt8>)
+    func write(file: File) throws
+    func write(data: [UInt8]) throws
+    func write(data: ArraySlice<UInt8>) throws
 }
 
 public enum HttpResponseBody {
@@ -36,7 +36,7 @@ public enum HttpResponseBody {
                 #if os(Linux)
                     let data = [UInt8]("Not ready for Linux.".utf8)
                     return (data.count, {
-                        $0.write(data)
+                        try $0.write(data)
                     })
                 #else
                     guard NSJSONSerialization.isValidJSONObject(object) else {
@@ -45,31 +45,31 @@ public enum HttpResponseBody {
                     let json = try NSJSONSerialization.dataWithJSONObject(object, options: NSJSONWritingOptions.PrettyPrinted)
                     let data = Array(UnsafeBufferPointer(start: UnsafePointer<UInt8>(json.bytes), count: json.length))
                     return (data.count, {
-                        $0.write(data)
+                        try $0.write(data)
                     })
                 #endif
             case .Text(let body):
                 let data = [UInt8](body.utf8)
                 return (data.count, {
-                    $0.write(data)
+                    try $0.write(data)
                 })
             case .Html(let body):
                 let serialised = "<html><meta charset=\"UTF-8\"><body>\(body)</body></html>"
                 let data = [UInt8](serialised.utf8)
                 return (data.count, {
-                    $0.write(data)
+                    try $0.write(data)
                 })
             case .Custom(let object, let closure):
                 let serialised = try closure(object)
                 let data = [UInt8](serialised.utf8)
                 return (data.count, {
-                    $0.write(data)
+                    try $0.write(data)
                 })
             }
         } catch {
             let data = [UInt8]("Serialisation error: \(error)".utf8)
             return (data.count, {
-                $0.write(data)
+                try $0.write(data)
             })
         }
     }
@@ -82,8 +82,8 @@ public enum HttpResponse {
     case MovedPermanently(String)
     case BadRequest(HttpResponseBody?), Unauthorized, Forbidden, NotFound
     case InternalServerError
-    case RAW(Int, String, [String:String]?, (HttpResponseBodyWriter -> Void)? )
-    
+    case RAW(Int, String, [String:String]?, (HttpResponseBodyWriter throws -> Void)? )
+
     func statusCode() -> Int {
         switch self {
         case .SwitchProtocols(_, _)   : return 101

+ 13 - 14
Sources/HttpServerIO.swift

@@ -5,12 +5,12 @@
 //  Copyright (c) 2014-2016 Damian Kołakowski. All rights reserved.
 //
 
+import Foundation
 #if os(Linux)
     import Glibc
-#else
-    import Foundation
 #endif
 
+
 public class HttpServerIO {
     
     private var listenSocket: Socket = Socket(socketFileDescriptor: -1)
@@ -83,22 +83,21 @@ public class HttpServerIO {
     
     private struct InnerWriteContext: HttpResponseBodyWriter {
         let socket: Socket
-        
-        func write(file: File) {
+
+        func write(file: File) throws {
             var offset: off_t = 0
-            let _ = sendfile(fileno(file.pointer), socket.socketFileDescriptor, 0, &offset, nil, 0)
+            let result = sendfile(fileno(file.pointer), socket.socketFileDescriptor, 0, &offset, nil, 0)
+            if result == -1 {
+              throw NSError(domain: NSPOSIXErrorDomain, code: Int(errno), userInfo: nil)
+            }
         }
 
-        func write(data: [UInt8]) {
-            write(ArraySlice(data))
+        func write(data: [UInt8]) throws {
+            try write(ArraySlice(data))
         }
-        
-        func write(data: ArraySlice<UInt8>) {
-            do {
-                try socket.writeUInt8(data)
-            } catch {
-                print("\(error)")
-            }
+
+        func write(data: ArraySlice<UInt8>) throws {
+            try socket.writeUInt8(data)
         }
     }
     

+ 1 - 2
Sources/Scopes.swift

@@ -15,8 +15,7 @@ public func scopes(scope: Closure) -> ((HttpRequest) -> HttpResponse) {
     return { r in
         ScopesBuffer[Process.TID] = ""
         scope()
-        return .RAW(200, "OK", ["Content-Type": "text/html"],
-                    { $0.write([UInt8](("<!DOCTYPE html>"  + (ScopesBuffer[Process.TID] ?? "")).utf8)) })
+        return .RAW(200, "OK", ["Content-Type": "text/html"], { try $0.write([UInt8](("<!DOCTYPE html>"  + (ScopesBuffer[Process.TID] ?? "")).utf8)) })
     }
 }