|
@@ -900,13 +900,14 @@ static inline NSString* _EncodeBase64(NSString* string) {
|
|
|
}
|
|
|
|
|
|
- (void)addDefaultHandlerForMethod:(NSString*)method requestClass:(Class)aClass asyncProcessBlock:(GCDWebServerAsyncProcessBlock)block {
|
|
|
- [self addHandlerWithMatchBlock:^GCDWebServerRequest*(NSString* requestMethod, NSURL* requestURL, NSDictionary<NSString*, NSString*>* requestHeaders, NSString* urlPath, NSDictionary<NSString*, NSString*>* urlQuery) {
|
|
|
- if (![requestMethod isEqualToString:method]) {
|
|
|
- return nil;
|
|
|
- }
|
|
|
- return [(GCDWebServerRequest*)[aClass alloc] initWithMethod:requestMethod url:requestURL headers:requestHeaders path:urlPath query:urlQuery];
|
|
|
- }
|
|
|
- asyncProcessBlock:block];
|
|
|
+ [self
|
|
|
+ addHandlerWithMatchBlock:^GCDWebServerRequest*(NSString* requestMethod, NSURL* requestURL, NSDictionary<NSString*, NSString*>* requestHeaders, NSString* urlPath, NSDictionary<NSString*, NSString*>* urlQuery) {
|
|
|
+ if (![requestMethod isEqualToString:method]) {
|
|
|
+ return nil;
|
|
|
+ }
|
|
|
+ return [(GCDWebServerRequest*)[aClass alloc] initWithMethod:requestMethod url:requestURL headers:requestHeaders path:urlPath query:urlQuery];
|
|
|
+ }
|
|
|
+ asyncProcessBlock:block];
|
|
|
}
|
|
|
|
|
|
- (void)addHandlerForMethod:(NSString*)method path:(NSString*)path requestClass:(Class)aClass processBlock:(GCDWebServerProcessBlock)block {
|
|
@@ -920,16 +921,17 @@ static inline NSString* _EncodeBase64(NSString* string) {
|
|
|
|
|
|
- (void)addHandlerForMethod:(NSString*)method path:(NSString*)path requestClass:(Class)aClass asyncProcessBlock:(GCDWebServerAsyncProcessBlock)block {
|
|
|
if ([path hasPrefix:@"/"] && [aClass isSubclassOfClass:[GCDWebServerRequest class]]) {
|
|
|
- [self addHandlerWithMatchBlock:^GCDWebServerRequest*(NSString* requestMethod, NSURL* requestURL, NSDictionary<NSString*, NSString*>* requestHeaders, NSString* urlPath, NSDictionary<NSString*, NSString*>* urlQuery) {
|
|
|
- if (![requestMethod isEqualToString:method]) {
|
|
|
- return nil;
|
|
|
- }
|
|
|
- if ([urlPath caseInsensitiveCompare:path] != NSOrderedSame) {
|
|
|
- return nil;
|
|
|
- }
|
|
|
- return [(GCDWebServerRequest*)[aClass alloc] initWithMethod:requestMethod url:requestURL headers:requestHeaders path:urlPath query:urlQuery];
|
|
|
- }
|
|
|
- asyncProcessBlock:block];
|
|
|
+ [self
|
|
|
+ addHandlerWithMatchBlock:^GCDWebServerRequest*(NSString* requestMethod, NSURL* requestURL, NSDictionary<NSString*, NSString*>* requestHeaders, NSString* urlPath, NSDictionary<NSString*, NSString*>* urlQuery) {
|
|
|
+ if (![requestMethod isEqualToString:method]) {
|
|
|
+ return nil;
|
|
|
+ }
|
|
|
+ if ([urlPath caseInsensitiveCompare:path] != NSOrderedSame) {
|
|
|
+ return nil;
|
|
|
+ }
|
|
|
+ return [(GCDWebServerRequest*)[aClass alloc] initWithMethod:requestMethod url:requestURL headers:requestHeaders path:urlPath query:urlQuery];
|
|
|
+ }
|
|
|
+ asyncProcessBlock:block];
|
|
|
} else {
|
|
|
GWS_DNOT_REACHED();
|
|
|
}
|
|
@@ -947,34 +949,35 @@ static inline NSString* _EncodeBase64(NSString* string) {
|
|
|
- (void)addHandlerForMethod:(NSString*)method pathRegex:(NSString*)regex requestClass:(Class)aClass asyncProcessBlock:(GCDWebServerAsyncProcessBlock)block {
|
|
|
NSRegularExpression* expression = [NSRegularExpression regularExpressionWithPattern:regex options:NSRegularExpressionCaseInsensitive error:NULL];
|
|
|
if (expression && [aClass isSubclassOfClass:[GCDWebServerRequest class]]) {
|
|
|
- [self addHandlerWithMatchBlock:^GCDWebServerRequest*(NSString* requestMethod, NSURL* requestURL, NSDictionary<NSString*, NSString*>* requestHeaders, NSString* urlPath, NSDictionary<NSString*, NSString*>* urlQuery) {
|
|
|
- if (![requestMethod isEqualToString:method]) {
|
|
|
- return nil;
|
|
|
- }
|
|
|
+ [self
|
|
|
+ addHandlerWithMatchBlock:^GCDWebServerRequest*(NSString* requestMethod, NSURL* requestURL, NSDictionary<NSString*, NSString*>* requestHeaders, NSString* urlPath, NSDictionary<NSString*, NSString*>* urlQuery) {
|
|
|
+ if (![requestMethod isEqualToString:method]) {
|
|
|
+ return nil;
|
|
|
+ }
|
|
|
|
|
|
- NSArray* matches = [expression matchesInString:urlPath options:0 range:NSMakeRange(0, urlPath.length)];
|
|
|
- if (matches.count == 0) {
|
|
|
- return nil;
|
|
|
- }
|
|
|
+ NSArray* matches = [expression matchesInString:urlPath options:0 range:NSMakeRange(0, urlPath.length)];
|
|
|
+ if (matches.count == 0) {
|
|
|
+ return nil;
|
|
|
+ }
|
|
|
|
|
|
- NSMutableArray* captures = [NSMutableArray array];
|
|
|
- for (NSTextCheckingResult* result in matches) {
|
|
|
- // Start at 1; index 0 is the whole string
|
|
|
- for (NSUInteger i = 1; i < result.numberOfRanges; i++) {
|
|
|
- NSRange range = [result rangeAtIndex:i];
|
|
|
- // range is {NSNotFound, 0} "if one of the capture groups did not participate in this particular match"
|
|
|
- // see discussion in -[NSRegularExpression firstMatchInString:options:range:]
|
|
|
- if (range.location != NSNotFound) {
|
|
|
- [captures addObject:[urlPath substringWithRange:range]];
|
|
|
+ NSMutableArray* captures = [NSMutableArray array];
|
|
|
+ for (NSTextCheckingResult* result in matches) {
|
|
|
+ // Start at 1; index 0 is the whole string
|
|
|
+ for (NSUInteger i = 1; i < result.numberOfRanges; i++) {
|
|
|
+ NSRange range = [result rangeAtIndex:i];
|
|
|
+ // range is {NSNotFound, 0} "if one of the capture groups did not participate in this particular match"
|
|
|
+ // see discussion in -[NSRegularExpression firstMatchInString:options:range:]
|
|
|
+ if (range.location != NSNotFound) {
|
|
|
+ [captures addObject:[urlPath substringWithRange:range]];
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
- }
|
|
|
- }
|
|
|
|
|
|
- GCDWebServerRequest* request = [(GCDWebServerRequest*)[aClass alloc] initWithMethod:requestMethod url:requestURL headers:requestHeaders path:urlPath query:urlQuery];
|
|
|
- [request setAttribute:captures forKey:GCDWebServerRequestAttribute_RegexCaptures];
|
|
|
- return request;
|
|
|
- }
|
|
|
- asyncProcessBlock:block];
|
|
|
+ GCDWebServerRequest* request = [(GCDWebServerRequest*)[aClass alloc] initWithMethod:requestMethod url:requestURL headers:requestHeaders path:urlPath query:urlQuery];
|
|
|
+ [request setAttribute:captures forKey:GCDWebServerRequestAttribute_RegexCaptures];
|
|
|
+ return request;
|
|
|
+ }
|
|
|
+ asyncProcessBlock:block];
|
|
|
} else {
|
|
|
GWS_DNOT_REACHED();
|
|
|
}
|
|
@@ -1045,15 +1048,16 @@ static inline NSString* _EncodeBase64(NSString* string) {
|
|
|
- (void)addGETHandlerForBasePath:(NSString*)basePath directoryPath:(NSString*)directoryPath indexFilename:(NSString*)indexFilename cacheAge:(NSUInteger)cacheAge allowRangeRequests:(BOOL)allowRangeRequests {
|
|
|
if ([basePath hasPrefix:@"/"] && [basePath hasSuffix:@"/"]) {
|
|
|
GCDWebServer* __unsafe_unretained server = self;
|
|
|
- [self addHandlerWithMatchBlock:^GCDWebServerRequest*(NSString* requestMethod, NSURL* requestURL, NSDictionary<NSString*, NSString*>* requestHeaders, NSString* urlPath, NSDictionary<NSString*, NSString*>* urlQuery) {
|
|
|
- if (![requestMethod isEqualToString:@"GET"]) {
|
|
|
- return nil;
|
|
|
- }
|
|
|
- if (![urlPath hasPrefix:basePath]) {
|
|
|
- return nil;
|
|
|
- }
|
|
|
- return [[GCDWebServerRequest alloc] initWithMethod:requestMethod url:requestURL headers:requestHeaders path:urlPath query:urlQuery];
|
|
|
- }
|
|
|
+ [self
|
|
|
+ addHandlerWithMatchBlock:^GCDWebServerRequest*(NSString* requestMethod, NSURL* requestURL, NSDictionary<NSString*, NSString*>* requestHeaders, NSString* urlPath, NSDictionary<NSString*, NSString*>* urlQuery) {
|
|
|
+ if (![requestMethod isEqualToString:@"GET"]) {
|
|
|
+ return nil;
|
|
|
+ }
|
|
|
+ if (![urlPath hasPrefix:basePath]) {
|
|
|
+ return nil;
|
|
|
+ }
|
|
|
+ return [[GCDWebServerRequest alloc] initWithMethod:requestMethod url:requestURL headers:requestHeaders path:urlPath query:urlQuery];
|
|
|
+ }
|
|
|
processBlock:^GCDWebServerResponse*(GCDWebServerRequest* request) {
|
|
|
GCDWebServerResponse* response = nil;
|
|
|
NSString* filePath = [directoryPath stringByAppendingPathComponent:GCDWebServerNormalizePath([request.path substringFromIndex:basePath.length])];
|