File tree Expand file tree Collapse file tree 2 files changed +46
-0
lines changed Expand file tree Collapse file tree 2 files changed +46
-0
lines changed Original file line number Diff line number Diff line change 3131
3232import FlyingSocks
3333import Foundation
34+ #if canImport(UniformTypeIdentifiers)
3435import UniformTypeIdentifiers
36+ #endif
3537
3638public struct FileHTTPHandler : HTTPHandler {
3739
@@ -50,6 +52,7 @@ public struct FileHTTPHandler: HTTPHandler {
5052
5153 static func makeContentType( for filename: String ) -> String {
5254 let pathExtension = ( filename. lowercased ( ) as NSString ) . pathExtension
55+ #if canImport(UniformTypeIdentifiers)
5356 switch pathExtension {
5457 case " wasm " :
5558 return " application/wasm "
@@ -58,6 +61,46 @@ public struct FileHTTPHandler: HTTPHandler {
5861 default :
5962 return UTType ( filenameExtension: pathExtension) ? . preferredMIMEType ?? " application/octet-stream "
6063 }
64+ #else
65+ switch pathExtension {
66+ case " json " :
67+ return " application/json "
68+ case " html " , " htm " :
69+ return " text/html "
70+ case " css " :
71+ return " text/css "
72+ case " js " , " javascript " :
73+ return " text/javascript "
74+ case " png " :
75+ return " image/png "
76+ case " jpeg " , " jpg " :
77+ return " image/jpeg "
78+ case " mp4 " :
79+ return " video/mp4 "
80+ case " m4v " :
81+ return " video/x-m4v "
82+ case " pdf " :
83+ return " application/pdf "
84+ case " svg " :
85+ return " image/svg+xml "
86+ case " txt " :
87+ return " text/plain "
88+ case " ico " :
89+ return " image/vnd.microsoft.icon "
90+ case " wasm " :
91+ return " application/wasm "
92+ case " webp " :
93+ return " image/webp "
94+ case " jp2 " :
95+ return " image/jp2 "
96+ case " properties " :
97+ return " text/plain "
98+ case " xml " :
99+ return " application/xml "
100+ default :
101+ return " application/octet-stream "
102+ }
103+ #endif
61104 }
62105
63106 public func handleRequest( _ request: HTTPRequest ) async throws -> HTTPResponse {
Original file line number Diff line number Diff line change @@ -179,6 +179,9 @@ struct HTTPHandlerTests {
179179 #expect(
180180 FileHTTPHandler . makeContentType ( for: " fish.properties " ) == " text/plain "
181181 )
182+ #expect(
183+ FileHTTPHandler . makeContentType ( for: " fish.xml " ) == " application/xml "
184+ )
182185 #expect(
183186 FileHTTPHandler . makeContentType ( for: " fish.somefile " ) == " application/octet-stream "
184187 )
You can’t perform that action at this time.
0 commit comments