Skip to content

Commit c35d514

Browse files
committed
Allow to customiz mime types used by GCDWebServerFileResponse
1 parent a013f9c commit c35d514

File tree

4 files changed

+22
-10
lines changed

4 files changed

+22
-10
lines changed

GCDWebServer/Core/GCDWebServerFunctions.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,12 @@ extern "C" {
3636
/**
3737
* Converts a file extension to the corresponding MIME type.
3838
* If there is no match, "application/octet-stream" is returned.
39+
*
40+
* Overrides allow to customize the built-in mapping from extensions to MIME
41+
* types. Keys of the dictionary must be lowercased file extensions without
42+
* the period, and the values must be the corresponding MIME types.
3943
*/
40-
NSString* GCDWebServerGetMimeTypeForExtension(NSString* extension);
44+
NSString* GCDWebServerGetMimeTypeForExtension(NSString* extension, NSDictionary* _Nullable overrides);
4145

4246
/**
4347
* Add percent-escapes to a string so it can be used in a URL.

GCDWebServer/Core/GCDWebServerFunctions.m

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,12 +166,15 @@ BOOL GCDWebServerIsTextContentType(NSString* type) {
166166
return [NSString stringWithFormat:@"<%lu bytes>", (unsigned long)data.length];
167167
}
168168

169-
NSString* GCDWebServerGetMimeTypeForExtension(NSString* extension) {
170-
NSDictionary* overrides = @{@"css": @"text/css"};
169+
NSString* GCDWebServerGetMimeTypeForExtension(NSString* extension, NSDictionary* overrides) {
170+
NSDictionary* builtInOverrides = @{@"css": @"text/css"};
171171
NSString* mimeType = nil;
172172
extension = [extension lowercaseString];
173173
if (extension.length) {
174174
mimeType = [overrides objectForKey:extension];
175+
if (mimeType == nil) {
176+
mimeType = [builtInOverrides objectForKey:extension];
177+
}
175178
if (mimeType == nil) {
176179
CFStringRef uti = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, (__bridge CFStringRef)extension, NULL);
177180
if (uti) {

GCDWebServer/Responses/GCDWebServerFileResponse.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,13 @@ NS_ASSUME_NONNULL_BEGIN
9595

9696
/**
9797
* This method is the designated initializer for the class.
98+
*
99+
* If MIME type overrides are specified, they allow to customize the built-in
100+
* mapping from extensions to MIME types. Keys of the dictionary must be lowercased
101+
* file extensions without the period, and the values must be the corresponding
102+
* MIME types.
98103
*/
99-
- (nullable instancetype)initWithFile:(NSString*)path byteRange:(NSRange)range isAttachment:(BOOL)attachment;
104+
- (nullable instancetype)initWithFile:(NSString*)path byteRange:(NSRange)range isAttachment:(BOOL)attachment mimeTypeOverrides:(nullable NSDictionary*)overrides;
100105

101106
@end
102107

GCDWebServer/Responses/GCDWebServerFileResponse.m

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,26 +57,26 @@ + (instancetype)responseWithFile:(NSString*)path byteRange:(NSRange)range {
5757
}
5858

5959
+ (instancetype)responseWithFile:(NSString*)path byteRange:(NSRange)range isAttachment:(BOOL)attachment {
60-
return [[[self class] alloc] initWithFile:path byteRange:range isAttachment:attachment];
60+
return [[[self class] alloc] initWithFile:path byteRange:range isAttachment:attachment mimeTypeOverrides:nil];
6161
}
6262

6363
- (instancetype)initWithFile:(NSString*)path {
64-
return [self initWithFile:path byteRange:NSMakeRange(NSUIntegerMax, 0) isAttachment:NO];
64+
return [self initWithFile:path byteRange:NSMakeRange(NSUIntegerMax, 0) isAttachment:NO mimeTypeOverrides:nil];
6565
}
6666

6767
- (instancetype)initWithFile:(NSString*)path isAttachment:(BOOL)attachment {
68-
return [self initWithFile:path byteRange:NSMakeRange(NSUIntegerMax, 0) isAttachment:attachment];
68+
return [self initWithFile:path byteRange:NSMakeRange(NSUIntegerMax, 0) isAttachment:attachment mimeTypeOverrides:nil];
6969
}
7070

7171
- (instancetype)initWithFile:(NSString*)path byteRange:(NSRange)range {
72-
return [self initWithFile:path byteRange:range isAttachment:NO];
72+
return [self initWithFile:path byteRange:range isAttachment:NO mimeTypeOverrides:nil];
7373
}
7474

7575
static inline NSDate* _NSDateFromTimeSpec(const struct timespec* t) {
7676
return [NSDate dateWithTimeIntervalSince1970:((NSTimeInterval)t->tv_sec + (NSTimeInterval)t->tv_nsec / 1000000000.0)];
7777
}
7878

79-
- (instancetype)initWithFile:(NSString*)path byteRange:(NSRange)range isAttachment:(BOOL)attachment {
79+
- (instancetype)initWithFile:(NSString*)path byteRange:(NSRange)range isAttachment:(BOOL)attachment mimeTypeOverrides:(NSDictionary*)overrides {
8080
struct stat info;
8181
if (lstat([path fileSystemRepresentation], &info) || !(info.st_mode & S_IFREG)) {
8282
GWS_DNOT_REACHED();
@@ -129,7 +129,7 @@ - (instancetype)initWithFile:(NSString*)path byteRange:(NSRange)range isAttachme
129129
}
130130
}
131131

132-
self.contentType = GCDWebServerGetMimeTypeForExtension([_path pathExtension]);
132+
self.contentType = GCDWebServerGetMimeTypeForExtension([_path pathExtension], overrides);
133133
self.contentLength = _size;
134134
self.lastModifiedDate = _NSDateFromTimeSpec(&info.st_mtimespec);
135135
self.eTag = [NSString stringWithFormat:@"%llu/%li/%li", info.st_ino, info.st_mtimespec.tv_sec, info.st_mtimespec.tv_nsec];

0 commit comments

Comments
 (0)