#27 Updated API to expose range requests support

This commit is contained in:
Pierre-Olivier Latour 2014-03-19 21:15:25 -07:00
parent eb6589e27a
commit 1b163b1b8b
3 changed files with 7 additions and 5 deletions

View File

@ -54,7 +54,7 @@ typedef GCDWebServerResponse* (^GCDWebServerProcessBlock)(GCDWebServerRequest* r
@interface GCDWebServer (Handlers) @interface GCDWebServer (Handlers)
- (void)addDefaultHandlerForMethod:(NSString*)method requestClass:(Class)aClass processBlock:(GCDWebServerProcessBlock)block; - (void)addDefaultHandlerForMethod:(NSString*)method requestClass:(Class)aClass processBlock:(GCDWebServerProcessBlock)block;
- (void)addHandlerForBasePath:(NSString*)basePath localPath:(NSString*)localPath indexFilename:(NSString*)indexFilename cacheAge:(NSUInteger)age; // Base path is recursive and case-sensitive - (void)addHandlerForBasePath:(NSString*)basePath localPath:(NSString*)localPath indexFilename:(NSString*)indexFilename cacheAge:(NSUInteger)cacheAge allowRangeRequests:(BOOL)allowRangeRequests; // Base path is recursive and case-sensitive
- (void)addHandlerForMethod:(NSString*)method path:(NSString*)path requestClass:(Class)aClass processBlock:(GCDWebServerProcessBlock)block; // Path is case-insensitive - (void)addHandlerForMethod:(NSString*)method path:(NSString*)path requestClass:(Class)aClass processBlock:(GCDWebServerProcessBlock)block; // Path is case-insensitive
- (void)addHandlerForMethod:(NSString*)method pathRegex:(NSString*)regex requestClass:(Class)aClass processBlock:(GCDWebServerProcessBlock)block; // Regular expression is case-insensitive - (void)addHandlerForMethod:(NSString*)method pathRegex:(NSString*)regex requestClass:(Class)aClass processBlock:(GCDWebServerProcessBlock)block; // Regular expression is case-insensitive
@end @end

View File

@ -395,7 +395,7 @@ static void _NetServiceClientCallBack(CFNetServiceRef service, CFStreamError* er
return [GCDWebServerDataResponse responseWithHTML:html]; return [GCDWebServerDataResponse responseWithHTML:html];
} }
- (void)addHandlerForBasePath:(NSString*)basePath localPath:(NSString*)localPath indexFilename:(NSString*)indexFilename cacheAge:(NSUInteger)age { - (void)addHandlerForBasePath:(NSString*)basePath localPath:(NSString*)localPath indexFilename:(NSString*)indexFilename cacheAge:(NSUInteger)cacheAge allowRangeRequests:(BOOL)allowRangeRequests {
if ([basePath hasPrefix:@"/"] && [basePath hasSuffix:@"/"]) { if ([basePath hasPrefix:@"/"] && [basePath hasSuffix:@"/"]) {
#if __has_feature(objc_arc) #if __has_feature(objc_arc)
__unsafe_unretained GCDWebServer* server = self; __unsafe_unretained GCDWebServer* server = self;
@ -426,7 +426,7 @@ static void _NetServiceClientCallBack(CFNetServiceRef service, CFStreamError* er
} }
} }
response = [server _responseWithContentsOfDirectory:filePath]; response = [server _responseWithContentsOfDirectory:filePath];
} else { } else if (allowRangeRequests) {
NSRange range = request.byteRange; NSRange range = request.byteRange;
if ((range.location != NSNotFound) || (range.length > 0)) { if ((range.location != NSNotFound) || (range.length > 0)) {
response = [server _responseWithPartialContentsOfFile:filePath byteRange:range]; response = [server _responseWithPartialContentsOfFile:filePath byteRange:range];
@ -434,10 +434,12 @@ static void _NetServiceClientCallBack(CFNetServiceRef service, CFStreamError* er
response = [server _responseWithContentsOfFile:filePath]; response = [server _responseWithContentsOfFile:filePath];
} }
[response setValue:@"bytes" forAdditionalHeader:@"Accept-Ranges"]; [response setValue:@"bytes" forAdditionalHeader:@"Accept-Ranges"];
} else {
response = [server _responseWithContentsOfFile:filePath];
} }
} }
if (response) { if (response) {
response.cacheControlMaxAge = age; response.cacheControlMaxAge = cacheAge;
} else { } else {
response = [GCDWebServerResponse responseWithStatusCode:404]; response = [GCDWebServerResponse responseWithStatusCode:404];
} }

View File

@ -34,7 +34,7 @@ int main(int argc, const char* argv[]) {
switch (0) { switch (0) {
case 0: { case 0: {
[webServer addHandlerForBasePath:@"/" localPath:NSHomeDirectory() indexFilename:nil cacheAge:0]; [webServer addHandlerForBasePath:@"/" localPath:NSHomeDirectory() indexFilename:nil cacheAge:0 allowRangeRequests:YES];
break; break;
} }