Refactoring +canProcessRequest: and content type acceptability

This commit is contained in:
Mattt Thompson 2012-03-03 13:19:53 -08:00
parent 68eb634027
commit 8bfee9e910
5 changed files with 47 additions and 50 deletions

View file

@ -31,6 +31,10 @@ NSSet * AFContentTypesFromHTTPHeader(NSString *string) {
_skippedCharacterSet = [NSCharacterSet characterSetWithCharactersInString:@" ,"];
});
if (!string) {
return nil;
}
NSScanner *scanner = [NSScanner scannerWithString:string];
scanner.charactersToBeSkipped = _skippedCharacterSet;
@ -226,7 +230,11 @@ static NSString * AFStringFromIndexSet(NSIndexSet *indexSet) {
}
+ (BOOL)canProcessRequest:(NSURLRequest *)request {
return YES;
if (![[self class] isEqual:[AFHTTPRequestOperation class]]) {
return YES;
}
return [[self acceptableContentTypes] intersectsSet:AFContentTypesFromHTTPHeader([request valueForHTTPHeaderField:@"Accept"])];
}
@end

View file

@ -37,8 +37,6 @@ static dispatch_queue_t image_request_operation_processing_queue() {
#elif __MAC_OS_X_VERSION_MIN_REQUIRED
@property (readwrite, nonatomic, retain) NSImage *responseImage;
#endif
+ (NSSet *)defaultAcceptablePathExtensions;
@end
@implementation AFImageRequestOperation
@ -125,14 +123,6 @@ static dispatch_queue_t image_request_operation_processing_queue() {
}
#endif
+ (NSSet *)acceptableContentTypes {
return [NSSet setWithObjects:@"image/tiff", @"image/jpeg", @"image/gif", @"image/png", @"image/ico", @"image/x-icon", @"image/bmp", @"image/x-bmp", @"image/x-xbitmap", @"image/x-win-bitmap", nil];
}
+ (NSSet *)defaultAcceptablePathExtensions {
return [NSSet setWithObjects:@"tif", @"tiff", @"jpg", @"jpeg", @"gif", @"png", @"ico", @"bmp", @"cur", nil];
}
- (id)initWithRequest:(NSURLRequest *)urlRequest {
self = [super initWithRequest:urlRequest];
if (!self) {
@ -189,9 +179,6 @@ static dispatch_queue_t image_request_operation_processing_queue() {
#pragma mark - AFHTTPClientOperation
+ (BOOL)canProcessRequest:(NSURLRequest *)request {
return [[[self class] acceptableContentTypes] containsObject:[request valueForHTTPHeaderField:@"Accept"]] || [[self defaultAcceptablePathExtensions] containsObject:[[request URL] pathExtension]];
}
#if __IPHONE_OS_VERSION_MIN_REQUIRED
+ (AFHTTPRequestOperation *)HTTPRequestOperationWithRequest:(NSURLRequest *)urlRequest
@ -217,6 +204,20 @@ static dispatch_queue_t image_request_operation_processing_queue() {
}
#endif
+ (NSSet *)acceptableContentTypes {
return [NSSet setWithObjects:@"image/tiff", @"image/jpeg", @"image/gif", @"image/png", @"image/ico", @"image/x-icon", @"image/bmp", @"image/x-bmp", @"image/x-xbitmap", @"image/x-win-bitmap", nil];
}
+ (BOOL)canProcessRequest:(NSURLRequest *)request {
static NSSet * _acceptablePathExtension = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_acceptablePathExtension = [[NSSet alloc] initWithObjects:@"tif", @"tiff", @"jpg", @"jpeg", @"gif", @"png", @"ico", @"bmp", @"cur", nil];
});
return [_acceptablePathExtension containsObject:[[request URL] pathExtension]] || [super canProcessRequest:request];
}
- (void)setCompletionBlockWithSuccess:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
{

View file

@ -35,8 +35,6 @@ static dispatch_queue_t json_request_operation_processing_queue() {
@interface AFJSONRequestOperation ()
@property (readwrite, nonatomic, retain) id responseJSON;
@property (readwrite, nonatomic, retain) NSError *JSONError;
+ (NSSet *)defaultAcceptablePathExtensions;
@end
@implementation AFJSONRequestOperation
@ -61,18 +59,6 @@ static dispatch_queue_t json_request_operation_processing_queue() {
return requestOperation;
}
+ (NSSet *)acceptableContentTypes {
return [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", nil];
}
+ (NSSet *)defaultAcceptablePathExtensions {
return [NSSet setWithObjects:@"json", nil];
}
+ (BOOL)canProcessRequest:(NSURLRequest *)request {
return [[self acceptableContentTypes] containsObject:[request valueForHTTPHeaderField:@"Accept"]] || [[self defaultAcceptablePathExtensions] containsObject:[[request URL] pathExtension]];
}
- (void)dealloc {
[_responseJSON release];
[_JSONError release];
@ -103,6 +89,16 @@ static dispatch_queue_t json_request_operation_processing_queue() {
}
}
#pragma mark - AFHTTPRequestOperation
+ (NSSet *)acceptableContentTypes {
return [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", nil];
}
+ (BOOL)canProcessRequest:(NSURLRequest *)request {
return [[[request URL] pathExtension] isEqualToString:@"json"] || [super canProcessRequest:request];
}
- (void)setCompletionBlockWithSuccess:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
{

View file

@ -35,8 +35,6 @@ static dispatch_queue_t property_list_request_operation_processing_queue() {
@property (readwrite, nonatomic, retain) id responsePropertyList;
@property (readwrite, nonatomic, assign) NSPropertyListFormat propertyListFormat;
@property (readwrite, nonatomic, retain) NSError *propertyListError;
+ (NSSet *)defaultAcceptablePathExtensions;
@end
@implementation AFPropertyListRequestOperation
@ -63,14 +61,6 @@ static dispatch_queue_t property_list_request_operation_processing_queue() {
return requestOperation;
}
+ (NSSet *)acceptableContentTypes {
return [NSSet setWithObjects:@"application/x-plist", nil];
}
+ (NSSet *)defaultAcceptablePathExtensions {
return [NSSet setWithObjects:@"plist", nil];
}
- (id)initWithRequest:(NSURLRequest *)urlRequest {
self = [super initWithRequest:urlRequest];
if (!self) {
@ -108,8 +98,14 @@ static dispatch_queue_t property_list_request_operation_processing_queue() {
}
}
#pragma mark - AFHTTPRequestOperation
+ (NSSet *)acceptableContentTypes {
return [NSSet setWithObjects:@"application/x-plist", nil];
}
+ (BOOL)canProcessRequest:(NSURLRequest *)request {
return [[[self class] acceptableContentTypes] containsObject:[request valueForHTTPHeaderField:@"Accept"]] || [[self defaultAcceptablePathExtensions] containsObject:[[request URL] pathExtension]];
return [[[request URL] pathExtension] isEqualToString:@"plist"] || [super canProcessRequest:request];
}
- (void)setCompletionBlockWithSuccess:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success

View file

@ -39,8 +39,6 @@ static dispatch_queue_t xml_request_operation_processing_queue() {
@property (readwrite, nonatomic, retain) NSXMLDocument *responseXMLDocument;
#endif
@property (readwrite, nonatomic, retain) NSError *XMLError;
+ (NSSet *)defaultAcceptablePathExtensions;
@end
@implementation AFXMLRequestOperation
@ -90,14 +88,6 @@ static dispatch_queue_t xml_request_operation_processing_queue() {
}
#endif
+ (NSSet *)acceptableContentTypes {
return [NSSet setWithObjects:@"application/xml", @"text/xml", nil];
}
+ (NSSet *)defaultAcceptablePathExtensions {
return [NSSet setWithObjects:@"xml", nil];
}
- (void)dealloc {
[_responseXMLParser release];
@ -146,8 +136,14 @@ static dispatch_queue_t xml_request_operation_processing_queue() {
self.responseXMLParser.delegate = nil;
}
#pragma mark - AFHTTPRequestOperation
+ (NSSet *)acceptableContentTypes {
return [NSSet setWithObjects:@"application/xml", @"text/xml", nil];
}
+ (BOOL)canProcessRequest:(NSURLRequest *)request {
return [[[self class] acceptableContentTypes] containsObject:[request valueForHTTPHeaderField:@"Accept"]] || [[self defaultAcceptablePathExtensions] containsObject:[[request URL] pathExtension]];
return [[[request URL] pathExtension] isEqualToString:@"xml"] || [super canProcessRequest:request];
}
- (void)setCompletionBlockWithSuccess:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success