Kaynağa Gözat

Fixed linux build for swift-2.2.1-RELEASE-ubuntu15.10

kolakowski 10 yıl önce
ebeveyn
işleme
2152f1e335

+ 6 - 7
Sources/DemoServer.swift

@@ -5,7 +5,11 @@
 //  Copyright (c) 2014-2016 Damian Kołakowski. All rights reserved.
 //
 
-import Foundation
+#if os(Linux)
+    import Glibc
+#else
+    import Foundation
+#endif
 
 public func demoServer(publicDir: String) -> HttpServer {
     
@@ -153,11 +157,6 @@ public func demoServer(publicDir: String) -> HttpServer {
         return HttpResponse.RAW(200, "OK", ["XXX-Custom-Header": "value"], { $0.write([UInt8]("test".utf8)) })
     }
     
-    server["/json"] = { r in
-        let jsonObject: NSDictionary = [NSString(string: "foo"): NSNumber(int: 3), NSString(string: "bar"): NSString(string: "baz")] 
-        return .OK(.Json(jsonObject))
-    }
-    
     server["/redirect"] = { r in
         return .MovedPermanently("http://www.google.com")
     }
@@ -197,4 +196,4 @@ public func demoServer(publicDir: String) -> HttpServer {
     
     return server
 }
-    
+    

+ 7 - 2
Sources/File.swift

@@ -79,10 +79,15 @@ public class File {
             if ent == nil {
                 break
             }
-            let fileName = withUnsafePointer(&ent.memory.d_name) { (ptr) -> String? in
-                var buffer = [CChar](UnsafeBufferPointer(start: unsafeBitCast(ptr, UnsafePointer<CChar>.self), count: Int(ent.memory.d_namlen)))
+	    var name = ent.memory.d_name
+            let fileName = withUnsafePointer(&name) { (ptr) -> String? in
+                #if os(Linux)
+		return String.fromCString([CChar](UnsafeBufferPointer<CChar>(start: UnsafePointer(unsafeBitCast(ptr, UnsafePointer<CChar>.self)), count: 256)))
+		#else
+		var buffer = [CChar](UnsafeBufferPointer(start: unsafeBitCast(ptr, UnsafePointer<CChar>.self), count: Int(ent.memory.d_namlen)))
                 buffer.append(0)
                 return String.fromCString(buffer)
+		#endif
             }
             if let fileName = fileName {
                 results.append(fileName)

+ 5 - 91
Sources/Files.swift

@@ -5,7 +5,11 @@
 //  Copyright (c) 2014-2016 Damian Kołakowski. All rights reserved.
 //
 
-import Foundation
+#if os(Linux)
+    import Glibc
+#else
+    import Foundation
+#endif
 
 public func shareFilesFromDirectory(directoryPath: String) -> (HttpRequest -> HttpResponse) {
     return { r in
@@ -23,96 +27,6 @@ public func shareFilesFromDirectory(directoryPath: String) -> (HttpRequest -> Ht
     }
 }
 
-private func fileNameToShare(directoryPath: String, request: HttpRequest) -> String? {
-    let path = request.path
-    let fileRelativePath = request.params.first
-
-    if !path.hasSuffix("/"), let fileRelativePath = fileRelativePath {
-        let absolutePath = directoryPath + "/" + fileRelativePath.1
-        return absolutePath
-    }
-
-    let fm = NSFileManager.defaultManager()
-    let possibleIndexFiles = ["index.html", "index.htm"] // add any other files you want to check for here
-    var folderPath = directoryPath
-    if let fileRelativePath = fileRelativePath {
-        folderPath += "/\(fileRelativePath.1)"
-    }
-
-    for indexFile in possibleIndexFiles {
-        let indexPath = "\(folderPath)/\(indexFile)"
-        if fm.fileExistsAtPath(indexPath) {
-            return indexPath
-        }
-    }
-    
-    return nil
-}
-
-let rangePrefix = "bytes="
-
-public func directory(dir: String) -> (HttpRequest -> HttpResponse) {
-    return { r in
-        
-        guard let localPath = r.params.first else {
-            return HttpResponse.NotFound
-        }
-        
-        let filesPath = dir + "/" + localPath.1
-        
-        guard let fileBody = NSData(contentsOfFile: filesPath) else {
-            return HttpResponse.NotFound
-        }
-        
-        if let rangeHeader = r.headers["range"] {
-            
-            guard rangeHeader.hasPrefix(rangePrefix) else {
-                return .BadRequest(.Text("Invalid value of 'Range' header: \(r.headers["range"])"))
-            }
-            
-            #if os(Linux)
-                let rangeString = rangeHeader.substringFromIndex(HttpHandlers.rangePrefix.characters.count)
-            #else
-                let rangeString = rangeHeader.substringFromIndex(rangeHeader.startIndex.advancedBy(rangePrefix.characters.count))
-            #endif
-            
-            let rangeStringExploded = rangeString.split("-")
-            
-            guard rangeStringExploded.count == 2 else {
-                return .BadRequest(.Text("Invalid value of 'Range' header: \(r.headers["range"])"))
-            }
-            
-            let startStr = rangeStringExploded[0]
-            let endStr   = rangeStringExploded[1]
-            
-            guard let start = Int(startStr), end = Int(endStr) else {
-                var array = [UInt8](count: fileBody.length, repeatedValue: 0)
-                fileBody.getBytes(&array, length: fileBody.length)
-                return HttpResponse.RAW(200, "OK", nil, { $0.write(array) })
-            }
-            
-            let chunkLength = end - start
-            let chunkRange = NSRange(location: start, length: chunkLength + 1)
-            
-            guard chunkRange.location + chunkRange.length <= fileBody.length else {
-                return HttpResponse.RAW(416, "Requested range not satisfiable", nil, nil)
-            }
-            
-            let chunk = fileBody.subdataWithRange(chunkRange)
-            
-            let headers = [ "Content-Range" : "bytes \(startStr)-\(endStr)/\(fileBody.length)" ]
-            
-            var content = [UInt8](count: chunk.length, repeatedValue: 0)
-            chunk.getBytes(&content, length: chunk.length)
-            return HttpResponse.RAW(206, "Partial Content", headers, { $0.write(content) })
-        } else {
-            var content = [UInt8](count: fileBody.length, repeatedValue: 0)
-            fileBody.getBytes(&content, length: fileBody.length)
-            return HttpResponse.RAW(200, "OK", nil, { $0.write(content) })
-        }
-    }
-}
-
 public func directoryBrowser(dir: String) -> (HttpRequest -> HttpResponse) {
     return { r in
         guard let (_, value) = r.params.first else {

+ 2 - 2
Sources/HttpServerIO.swift

@@ -136,8 +136,8 @@ public class HttpServerIO {
     
     struct sf_hdtr { }
     
-    func sendfile(source: Int32, _ target: Int32, _: off_t, _: UnsafeMutablePointer<off_t>!, _: UnsafeMutablePointer<sf_hdtr>!, _: Int32) -> Int32 {
-        var buffer = [UInt8](repeating: 0, count: 1024)
+    func sendfile(source: Int32, _ target: Int32, _: off_t, _: UnsafeMutablePointer<off_t>, _: UnsafeMutablePointer<sf_hdtr>, _: Int32) -> Int32 {
+        var buffer = [UInt8](count: 1024, repeatedValue: 0)
         while true {
             let readResult = read(source, &buffer, buffer.count)
             guard readResult > 0 else {

+ 9 - 1
Sources/Process.swift

@@ -5,7 +5,11 @@
 //  Copyright (c) 2014-2016 Damian Kołakowski. All rights reserved.
 //
 
-import Foundation
+#if os(Linux)
+    import Glibc
+#else
+    import Foundation
+#endif
 
 public class Process {
     
@@ -14,9 +18,13 @@ public class Process {
     }
     
     public static var TID: UInt64 {
+	#if os(Linux)
+        return UInt64(pthread_self())
+	#else
         var tid: __uint64_t = 0
         pthread_threadid_np(nil, &tid);
         return UInt64(tid)
+        #endif
     }
     
     private static var signalsWatchers = Array<(Int32) -> Void>()

+ 5 - 1
Sources/Scopes.swift

@@ -5,7 +5,11 @@
 //  Copyright © 2014-2016 Damian Kołakowski. All rights reserved.
 //
 
-import Foundation
+#if os(Linux)
+    import Glibc
+#else
+    import Foundation
+#endif
 
 public func scopes(scope: Closure) -> ((HttpRequest) -> HttpResponse) {
     return { r in

+ 6 - 2
Sources/WebSockets.swift

@@ -5,7 +5,11 @@
 //  Copyright © 2014-2016 Damian Kołakowski. All rights reserved.
 //
 
-import Foundation
+#if os(Linux)
+    import Glibc
+#else
+    import Foundation
+#endif
 
 public func websocket(
         text: ((WebSocketSession, String) -> Void)?,
@@ -165,4 +169,4 @@ public class WebSocketSession {
         }
         return frm
     }
-}
+}