Adding AFContentTypesFromHTTPHeader()

This commit is contained in:
Mattt Thompson 2012-03-02 11:18:20 -08:00
parent 01572eed3d
commit 68eb634027
2 changed files with 30 additions and 0 deletions

View file

@ -23,6 +23,11 @@
#import <Foundation/Foundation.h>
#import "AFURLConnectionOperation.h"
/**
Returns a set of MIME types detected in an HTTP `Accept` or `Content-Type` header.
*/
extern NSSet * AFContentTypesFromHTTPHeader(NSString *string);
/**
`AFHTTPRequestOperation` is a subclass of `AFURLConnectionOperation` for requests using the HTTP or HTTPS protocols. It encapsulates the concept of acceptable status codes and content types, which determine the success or failure of a request.
*/

View file

@ -24,6 +24,31 @@
#import <objc/runtime.h>
NSSet * AFContentTypesFromHTTPHeader(NSString *string) {
static NSCharacterSet *_skippedCharacterSet = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_skippedCharacterSet = [NSCharacterSet characterSetWithCharactersInString:@" ,"];
});
NSScanner *scanner = [NSScanner scannerWithString:string];
scanner.charactersToBeSkipped = _skippedCharacterSet;
NSMutableSet *mutableContentTypes = [NSMutableSet set];
while (![scanner isAtEnd]) {
NSString *contentType = nil;
if ([scanner scanUpToString:@";" intoString:&contentType]) {
[scanner scanUpToString:@"," intoString:nil];
}
if (contentType) {
[mutableContentTypes addObject:contentType];
}
}
return [NSSet setWithSet:mutableContentTypes];
}
static void AFSwizzleClassMethodWithClassAndSelectorUsingBlock(Class klass, SEL selector, void *block) {
Method originalMethod = class_getClassMethod(klass, selector);
IMP implementation = imp_implementationWithBlock(block);