Parcourir la source

Added support for ArraySlice to HttpResponseBodyWriter.

Damian Kołakowski il y a 10 ans
Parent
commit
8c55199529

+ 1 - 1
Sources/HttpHandlers+Files.swift

@@ -21,7 +21,7 @@ extension HttpHandlers {
             return .RAW(200, "OK", [:], { writer in
                 var buffer = [UInt8](count: 64, repeatedValue: 0)
                 while let count = try? file.read(&buffer) where count > 0 {
-                    writer.write(Array(buffer[0..<count]))
+                    writer.write(buffer[0..<count])
                 }
                 file.close()
             })

+ 1 - 0
Sources/HttpResponse.swift

@@ -14,6 +14,7 @@ public enum SerializationError: ErrorType {
 
 public protocol HttpResponseBodyWriter {
     func write(data: [UInt8])
+    func write(data: ArraySlice<UInt8>)
 }
 
 public enum HttpResponseBody {

+ 3 - 0
Sources/HttpServerIO.swift

@@ -85,6 +85,9 @@ public class HttpServerIO {
     private struct InnerWriteContext: HttpResponseBodyWriter {
         let socket: Socket
         func write(data: [UInt8]) {
+            write(ArraySlice(data))
+        }
+        func write(data: ArraySlice<UInt8>) {
             do {
                 try socket.writeUInt8(data)
             } catch {

+ 5 - 1
Sources/Socket.swift

@@ -108,10 +108,14 @@ public class Socket: Hashable, Equatable {
     }
     
     public func writeUTF8(string: String) throws {
-        try writeUInt8([UInt8](string.utf8))
+        try writeUInt8(ArraySlice(string.utf8))
     }
     
     public func writeUInt8(data: [UInt8]) throws {
+        try writeUInt8(ArraySlice(data))
+    }
+    
+    public func writeUInt8(data: ArraySlice<UInt8>) throws {
         try data.withUnsafeBufferPointer {
             var sent = 0
             while sent < data.count {