Skip to content

Commit 1dbff28

Browse files
authored
Merge pull request #406 from nichbar/fix-content-length-issue
Add header item "Content-Length"
2 parents 3367b63 + d7e3759 commit 1dbff28

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ All notable changes to this project will be documented in this file. Changes not
2828

2929
- 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)
3030
- Fixes `HttpRequest.path` value to be parsed without query parameters [#404](https://github.com/httpswift/swifter/pull/404)) by [@mazyod](https://github.com/mazyod)
31+
- 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)
3132

3233
## Changed
3334
- 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)

XCode/Sources/Files.swift

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,18 @@ public func shareFilesFromDirectory(_ directoryPath: String, defaults: [String]
3434
}
3535
}
3636
}
37-
if let file = try? (directoryPath + String.pathSeparator + fileRelativePath.value).openForReading() {
37+
let filePath = directoryPath + String.pathSeparator + fileRelativePath.value
38+
39+
if let file = try? filePath.openForReading() {
3840
let mimeType = fileRelativePath.value.mimeType()
41+
var responseHeader: [String: String] = ["Content-Type": mimeType]
3942

40-
return .raw(200, "OK", ["Content-Type": mimeType], { writer in
43+
if let attr = try? FileManager.default.attributesOfItem(atPath: filePath),
44+
let fileSize = attr[FileAttributeKey.size] as? UInt64 {
45+
responseHeader["Content-Length"] = String(fileSize)
46+
}
47+
48+
return .raw(200, "OK", responseHeader, { writer in
4149
try? writer.write(file)
4250
file.close()
4351
})

0 commit comments

Comments
 (0)