Skip to content

Commit

Permalink
Merge pull request #406 from nichbar/fix-content-length-issue
Browse files Browse the repository at this point in the history
Add header item "Content-Length"
  • Loading branch information
Vkt0r authored May 9, 2019
2 parents 3367b63 + d7e3759 commit 1dbff28
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 10 additions & 2 deletions XCode/Sources/Files.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
Expand Down

0 comments on commit 1dbff28

Please sign in to comment.