Add associated object from request URL to baseURL in order to workaround issues with NSURLRequest clobbering the baseURL

This commit is contained in:
Blake Watters
2012-09-26 22:46:32 -04:00
parent 098edd1fec
commit ee9ae20823
2 changed files with 36 additions and 1 deletions

View File

@@ -18,6 +18,7 @@
// limitations under the License.
//
#import <objc/runtime.h>
#import "RKObjectManager.h"
#import "RKObjectParameterization.h"
#import "RKManagedObjectStore.h"
@@ -88,6 +89,24 @@ static BOOL RKDoesArrayOfResponseDescriptorsContainEntityMapping(NSArray *respon
return NO;
}
void RKAssociateBaseURLWithURL(NSURL *baseURL, NSURL *URL);
NSURL *RKBaseURLAssociatedWithURL(NSURL *URL);
static char kRKBaseURLAssociatedObjectKey;
void RKAssociateBaseURLWithURL(NSURL *baseURL, NSURL *URL)
{
objc_setAssociatedObject(URL,
&kRKBaseURLAssociatedObjectKey,
baseURL,
OBJC_ASSOCIATION_COPY_NONATOMIC);
}
NSURL *RKBaseURLAssociatedWithURL(NSURL *URL)
{
return objc_getAssociatedObject(URL, &kRKBaseURLAssociatedObjectKey);
}
///////////////////////////////////
@interface RKObjectManager ()
@@ -171,6 +190,11 @@ static BOOL RKDoesArrayOfResponseDescriptorsContainEntityMapping(NSArray *respon
[request setHTTPMethod:method];
[request setAllHTTPHeaderFields:self.HTTPClient.defaultHeaders];
if (self.acceptHeaderValue) [request setValue:self.acceptHeaderValue forHTTPHeaderField:@"Accept"];
/**
Associate our baseURL with the URL of the `NSURLRequest` object. This enables us to match response descriptors by path.
*/
RKAssociateBaseURLWithURL(request.URL, self.HTTPClient.baseURL);
if (parameters) {
if ([method isEqualToString:@"GET"] || [method isEqualToString:@"HEAD"] || [method isEqualToString:@"DELETE"]) {

View File

@@ -31,6 +31,9 @@
#undef RKLogComponent
#define RKLogComponent lcl_cRestKitNetwork
// Defined in RKObjectManager.h
NSURL *RKBaseURLAssociatedWithURL(NSURL *URL);
NSError *RKErrorFromMappingResult(RKMappingResult *mappingResult)
{
NSArray *collection = [mappingResult array];
@@ -54,6 +57,7 @@ NSError *RKErrorFromMappingResult(RKMappingResult *mappingResult)
@property (nonatomic, strong, readwrite) RKMappingResult *mappingResult;
@property (nonatomic, strong, readwrite) NSError *error;
@property (nonatomic, strong, readwrite) NSDictionary *responseMappingsDictionary;
@property (nonatomic, strong) NSString *relativeResponsePath;
@end
@interface RKResponseMapperOperation (ForSubclassEyesOnly)
@@ -76,11 +80,18 @@ NSError *RKErrorFromMappingResult(RKMappingResult *mappingResult)
self.responseDescriptors = responseDescriptors;
self.responseMappingsDictionary = [self buildResponseMappingsDictionary];
self.treatsEmptyResponseAsSuccess = YES;
self.relativeResponsePath = [self buildRelativeResponsePath];
}
return self;
}
- (NSString *)buildRelativeResponsePath
{
NSURL *baseURL = RKBaseURLAssociatedWithURL(self.response.URL);
return [[self.response.URL absoluteString] substringFromIndex:[[baseURL absoluteString] length]];
}
- (id)parseResponseData:(NSError **)error
{
NSString *MIMEType = [self.response MIMEType];
@@ -105,7 +116,7 @@ NSError *RKErrorFromMappingResult(RKMappingResult *mappingResult)
{
if (mappingDescriptor.pathPattern) {
RKPathMatcher *pathMatcher = [RKPathMatcher matcherWithPattern:mappingDescriptor.pathPattern];
if (! [pathMatcher matchesPath:[self.response.URL relativePath] tokenizeQueryStrings:NO parsedArguments:nil]) {
if (! [pathMatcher matchesPath:self.relativeResponsePath tokenizeQueryStrings:NO parsedArguments:nil]) {
return NO;
}
}