Explorar o código

Merge pull request #406 from nichbar/fix-content-length-issue

Add header item "Content-Length"
Victor Sigler %!s(int64=7) %!d(string=hai) anos
pai
achega
1dbff28c17
Modificáronse 2 ficheiros con 11 adicións e 2 borrados
  1. 1 0
      CHANGELOG.md
  2. 10 2
      XCode/Sources/Files.swift

+ 1 - 0
CHANGELOG.md

@@ -28,6 +28,7 @@ All notable changes to this project will be documented in this file. Changes not
 
 - Fixes build errors by excluding XC(UI)Test files from regular targets [#397](https://github.com/httpswift/swifter/pull/397)) by [@ChristianSteffens](https://github.com/ChristianSteffens)
 - Fixes `HttpRequest.path` value to be parsed without query parameters [#404](https://github.com/httpswift/swifter/pull/404)) by [@mazyod](https://github.com/mazyod)
+- Fixes the issue of missing `Content-Length` header item when `shareFilesFromDirectory` is being used to share files [#406](https://github.com/httpswift/swifter/pull/406) by [@nichbar](https://github.com/nichbar)
 
 ## Changed
 - Performance: Batch reads of websocket payloads rather than reading byte-by-byte. ([#387](https://github.com/httpswift/swifter/pull/387)) by [@lynaghk](https://github.com/lynaghk)

+ 10 - 2
XCode/Sources/Files.swift

@@ -34,10 +34,18 @@ public func shareFilesFromDirectory(_ directoryPath: String, defaults: [String]
                 }
             }
         }
-        if let file = try? (directoryPath + String.pathSeparator + fileRelativePath.value).openForReading() {
+        let filePath = directoryPath + String.pathSeparator + fileRelativePath.value
+        
+        if let file = try? filePath.openForReading() {
             let mimeType = fileRelativePath.value.mimeType()
+            var responseHeader: [String: String] = ["Content-Type": mimeType]
             
-            return .raw(200, "OK", ["Content-Type": mimeType], { writer in
+            if let attr = try? FileManager.default.attributesOfItem(atPath: filePath),
+                let fileSize = attr[FileAttributeKey.size] as? UInt64 {
+                responseHeader["Content-Length"] = String(fileSize)
+            }
+            
+            return .raw(200, "OK", responseHeader, { writer in
                 try? writer.write(file)
                 file.close()
             })