Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions FlyingFox/Sources/Handlers/FileHTTPHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@

import FlyingSocks
import Foundation
#if canImport(UniformTypeIdentifiers)
import UniformTypeIdentifiers
#endif

public struct FileHTTPHandler: HTTPHandler {

Expand All @@ -48,8 +51,17 @@ public struct FileHTTPHandler: HTTPHandler {
}

static func makeContentType(for filename: String) -> String {
// TODO: UTTypeCreatePreferredIdentifierForTag / UTTypeCopyPreferredTagWithClass
let pathExtension = (filename.lowercased() as NSString).pathExtension
#if canImport(UniformTypeIdentifiers)
switch pathExtension {
case "wasm":
return "application/wasm"
case "properties":
return "text/plain"
default:
return UTType(filenameExtension: pathExtension)?.preferredMIMEType ?? "application/octet-stream"
}
#else
switch pathExtension {
case "json":
return "application/json"
Expand All @@ -58,30 +70,37 @@ public struct FileHTTPHandler: HTTPHandler {
case "css":
return "text/css"
case "js", "javascript":
return "application/javascript"
return "text/javascript"
case "png":
return "image/png"
case "jpeg", "jpg":
return "image/jpeg"
case "m4v", "mp4":
case "mp4":
return "video/mp4"
case "m4v":
return "video/x-m4v"
case "pdf":
return "application/pdf"
case "svg":
return "image/svg+xml"
case "txt":
return "text/plain"
case "ico":
return "image/x-icon"
return "image/vnd.microsoft.icon"
case "wasm":
return "application/wasm"
case "webp":
return "image/webp"
case "jp2":
return "image/jp2"
case "properties":
return "text/plain"
case "xml":
return "application/xml"
default:
return "application/octet-stream"
}
#endif
}

public func handleRequest(_ request: HTTPRequest) async throws -> HTTPResponse {
Expand Down
14 changes: 10 additions & 4 deletions FlyingFox/Tests/Handlers/HTTPHandlerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,10 @@ struct HTTPHandlerTests {
FileHTTPHandler.makeContentType(for: "fish.css") == "text/css"
)
#expect(
FileHTTPHandler.makeContentType(for: "fish.js") == "application/javascript"
FileHTTPHandler.makeContentType(for: "fish.js") == "text/javascript"
)
#expect(
FileHTTPHandler.makeContentType(for: "fish.javascript") == "application/javascript"
FileHTTPHandler.makeContentType(for: "fish.javascript") == "text/javascript"
)
#expect(
FileHTTPHandler.makeContentType(for: "fish.png") == "image/png"
Expand All @@ -162,10 +162,10 @@ struct HTTPHandlerTests {
FileHTTPHandler.makeContentType(for: "fish.mp4") == "video/mp4"
)
#expect(
FileHTTPHandler.makeContentType(for: "fish.m4v") == "video/mp4"
FileHTTPHandler.makeContentType(for: "fish.m4v") == "video/x-m4v"
)
#expect(
FileHTTPHandler.makeContentType(for: "fish.ico") == "image/x-icon"
FileHTTPHandler.makeContentType(for: "fish.ico") == "image/vnd.microsoft.icon"
)
#expect(
FileHTTPHandler.makeContentType(for: "fish.wasm") == "application/wasm"
Expand All @@ -176,6 +176,12 @@ struct HTTPHandlerTests {
#expect(
FileHTTPHandler.makeContentType(for: "fish.jp2") == "image/jp2"
)
#expect(
FileHTTPHandler.makeContentType(for: "fish.properties") == "text/plain"
)
#expect(
FileHTTPHandler.makeContentType(for: "fish.xml") == "application/xml"
)
#expect(
FileHTTPHandler.makeContentType(for: "fish.somefile") == "application/octet-stream"
)
Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import PackageDescription
let package = Package(
name: "FlyingFox",
platforms: [
.macOS(.v10_15), .iOS(.v13), .tvOS(.v13), .watchOS(.v8)
.macOS(.v11), .iOS(.v14), .tvOS(.v14), .watchOS(.v8)
],
products: [
.library(
Expand Down
2 changes: 1 addition & 1 deletion [email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import PackageDescription
let package = Package(
name: "FlyingFox",
platforms: [
.macOS(.v10_15), .iOS(.v13), .tvOS(.v13), .watchOS(.v8)
.macOS(.v11), .iOS(.v14), .tvOS(.v14), .watchOS(.v8)
],
products: [
.library(
Expand Down
Loading