소스 검색

Merge pull request #124 from trozware/serve-index-files

Serve index files if no file name is specified
Damian Kołakowski 10 년 전
부모
커밋
c88641d01b
2개의 변경된 파일34개의 추가작업 그리고 7개의 파일을 삭제
  1. 3 2
      Sources/DemoServer.swift
  2. 31 5
      Sources/HttpHandlers+Files.swift

+ 3 - 2
Sources/DemoServer.swift

@@ -14,7 +14,8 @@ public func demoServer(publicDir: String) -> HttpServer {
     let server = HttpServer()
     
     server["/public/:path"] = HttpHandlers.shareFilesFromDirectory(publicDir)
-    
+    server["/public/"] = HttpHandlers.shareFilesFromDirectory(publicDir)    // needed to serve index file at root level
+
     server["/files/:path"] = HttpHandlers.directoryBrowser("/")
 
     server["/"] = { r in
@@ -109,7 +110,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));
+                w.write([UInt8]("[chunk \(i)]".utf8))
             }
         })
     }

+ 31 - 5
Sources/HttpHandlers+Files.swift

@@ -11,23 +11,49 @@ extension HttpHandlers {
     
     public class func shareFilesFromDirectory(directoryPath: String) -> (HttpRequest -> HttpResponse) {
         return { r in
-            guard let fileRelativePath = r.params.first else {
+            guard let absolutePath = self.fileNameToShare(directoryPath, request: r) else {
                 return .NotFound
             }
-            let absolutePath = directoryPath + "/" + fileRelativePath.1
+
             guard let file = try? File.openForReading(absolutePath) else {
                 return .NotFound
             }
             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(buffer[0..<count])
+                    writer.write(buffer[0 ..< count])
                 }
                 file.close()
             })
         }
     }
-    
+
+    private class 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
+    }
+
     private static let rangePrefix = "bytes="
     
     public class func directory(dir: String) -> (HttpRequest -> HttpResponse) {
@@ -123,4 +149,4 @@ extension HttpHandlers {
             }
         }
     }
-}
+}