Finished cleanup of HTTP AUTH and XML Support. Ready to tag 0.9.1

This commit is contained in:
Blake Watters
2011-03-28 23:29:06 -04:00
parent aea1df21e3
commit c90ec2770d
18 changed files with 151 additions and 68 deletions

View File

@@ -6,10 +6,11 @@
// Copyright 2009 Two Toasters. All rights reserved.
//
#import <SystemConfiguration/SCNetworkReachability.h>
#import "RKClient.h"
#import "RKObjectLoader.h"
#import "RKURL.h"
#import <SystemConfiguration/SCNetworkReachability.h>
#import "RKNotifications.h"
///////////////////////////////////////////////////////////////////////////////////////////////////
// Global
@@ -104,7 +105,8 @@ NSString* RKMakePathWithObject(NSString* path, id object) {
}
- (id)init {
if (self = [super init]) {
self = [super init];
if (self) {
_HTTPHeaders = [[NSMutableDictionary alloc] init];
self.serviceUnavailableAlertEnabled = NO;
self.serviceUnavailableAlertTitle = NSLocalizedString(@"Service Unavailable", nil);
@@ -121,6 +123,7 @@ NSString* RKMakePathWithObject(NSString* path, id object) {
self.serviceUnavailableAlertTitle = nil;
self.serviceUnavailableAlertMessage = nil;
[_HTTPHeaders release];
[super dealloc];
}

20
Code/Network/RKNetwork.h Normal file
View File

@@ -0,0 +1,20 @@
//
// RKNetwork.h
// RestKit
//
// Created by Blake Watters on 3/28/11.
// Copyright 2011 Two Toasters. All rights reserved.
//
#import <Foundation/Foundation.h>
/**
* Returns the global value for credential persistence to use during HTTP AUTH
* Defaults to NSURLCredentialPersistenceForSession
*/
NSURLCredentialPersistence RKNetworkGetGlobalCredentialPersistence();
/**
* Set the global value for credential persistence to use during HTTP AUTH
*/
void RKNetworkSetGlobalCredentialPersistence(NSURLCredentialPersistence persistence);

20
Code/Network/RKNetwork.m Normal file
View File

@@ -0,0 +1,20 @@
//
// MyClass.m
// RestKit
//
// Created by Blake Watters on 3/28/11.
// Copyright 2011 Two Toasters. All rights reserved.
//
#import "RKNetwork.h"
// Global credential persistence value.
static NSURLCredentialPersistence gCredentialPersistence = NSURLCredentialPersistenceForSession;
NSURLCredentialPersistence RKNetworkGetGlobalCredentialPersistence() {
return gCredentialPersistence;
}
void RKNetworkSetGlobalCredentialPersistence(NSURLCredentialPersistence persistence) {
gCredentialPersistence = persistence;
}

View File

@@ -16,6 +16,7 @@
* This is useful for doing things like generating automatic logging
* for all your requests or sending the response times
*/
extern NSString* const kRKRequestSentNotification;
extern NSString* const kRKResponseReceivedNotification;
extern NSString* const kRKRequestFailedWithErrorNotification;
extern NSString* const RKRequestSentNotification;
extern NSString* const RKRequestDidLoadResponseNotification;
extern NSString* const RKResponseReceivedNotification;
extern NSString* const RKRequestFailedWithErrorNotification;

View File

@@ -8,6 +8,7 @@
#import "RKNotifications.h"
NSString* const kRKRequestSentNotification = @"kRKRequestSentNotification";
NSString* const kRKResponseReceivedNotification = @"kRKRespongReceivedNotification";
NSString* const kRKRequestFailedWithErrorNotification = @"kRKRequestFailedWithErrorNotification";
NSString* const RKRequestSentNotification = @"RKRequestSentNotification";
NSString* const RKRequestDidLoadResponseNotification = @"RKRequestDidLoadResponseNotification";
NSString* const RKRequestFailedWithErrorNotification = @"RKRequestFailedWithErrorNotification";
NSString* const RKResponseReceivedNotification = @"RKRespongReceivedNotification";

View File

@@ -32,7 +32,6 @@ typedef enum RKRequestMethod {
NSObject<RKRequestSerializable>* _params;
NSObject<RKRequestDelegate>* _delegate;
id _userData;
NSString* _authenticationScheme;
NSString* _username;
NSString* _password;
RKRequestMethod _method;
@@ -86,7 +85,6 @@ typedef enum RKRequestMethod {
// The authentication scheme to use. When set to kCFHTTPAuthenticationSchemeBasic, authentication will
// be setup before a challenge occurs
@property(nonatomic, retain) NSString* authenticationScheme;
@property(nonatomic, retain) NSString* username;
@property(nonatomic, retain) NSString* password;

View File

@@ -19,8 +19,7 @@
@implementation RKRequest
@synthesize URL = _URL, URLRequest = _URLRequest, delegate = _delegate, additionalHTTPHeaders = _additionalHTTPHeaders,
params = _params, userData = _userData, username = _username, password = _password, method = _method,
authenticationScheme = _authenticationScheme;
params = _params, userData = _userData, username = _username, password = _password, method = _method;
+ (RKRequest*)requestWithURL:(NSURL*)URL delegate:(id)delegate {
return [[[RKRequest alloc] initWithURL:URL delegate:delegate] autorelease];
@@ -61,7 +60,6 @@
_params = nil;
[_additionalHTTPHeaders release];
_additionalHTTPHeaders = nil;
[_authenticationScheme release];
[_username release];
_username = nil;
[_password release];
@@ -97,20 +95,6 @@
[_URLRequest setValue:[NSString stringWithFormat:@"%d", [_params HTTPHeaderValueForContentLength]] forHTTPHeaderField:@"Content-Length"];
}
}
if (_username != nil && [_authenticationScheme isEqualToString:(NSString*)kCFHTTPAuthenticationSchemeBasic]) {
// Add authentication headers so we don't have to deal with an extra cycle for each message requiring basic auth.
CFHTTPMessageRef dummyRequest = CFHTTPMessageCreateRequest(kCFAllocatorDefault, (CFStringRef)[self HTTPMethod], (CFURLRef)[self URL], kCFHTTPVersion1_1);
CFHTTPMessageAddAuthentication(dummyRequest, nil, (CFStringRef)_username, (CFStringRef)_password, kCFHTTPAuthenticationSchemeBasic, FALSE);
CFStringRef authorizationString = CFHTTPMessageCopyHeaderFieldValue(dummyRequest, CFSTR("Authorization"));
[_URLRequest setValue:(NSString *)authorizationString forHTTPHeaderField:@"Authorization"];
CFRelease(dummyRequest);
CFRelease(authorizationString);
}
NSLog(@"Headers: %@", [_URLRequest allHTTPHeaderFields]);
}
// Setup the NSURLRequest. The request must be prepared right before dispatching
@@ -152,7 +136,7 @@
[body release];
NSDate* sentAt = [NSDate date];
NSDictionary* userInfo = [NSDictionary dictionaryWithObjectsAndKeys:[self HTTPMethod], @"HTTPMethod", [self URL], @"URL", sentAt, @"sentAt", nil];
[[NSNotificationCenter defaultCenter] postNotificationName:kRKRequestSentNotification object:self userInfo:userInfo];
[[NSNotificationCenter defaultCenter] postNotificationName:RKRequestSentNotification object:self userInfo:userInfo];
_isLoading = YES;
RKResponse* response = [[[RKResponse alloc] initWithRequest:self] autorelease];
@@ -180,7 +164,7 @@
[body release];
NSDate* sentAt = [NSDate date];
NSDictionary* userInfo = [NSDictionary dictionaryWithObjectsAndKeys:[self HTTPMethod], @"HTTPMethod", [self URL], @"URL", sentAt, @"sentAt", nil];
[[NSNotificationCenter defaultCenter] postNotificationName:kRKRequestSentNotification object:self userInfo:userInfo];
[[NSNotificationCenter defaultCenter] postNotificationName:RKRequestSentNotification object:self userInfo:userInfo];
_isLoading = YES;
payload = [NSURLConnection sendSynchronousRequest:_URLRequest returningResponse:&URLResponse error:&error];
@@ -223,7 +207,7 @@
NSDate* receivedAt = [NSDate date];
NSDictionary* userInfo = [NSDictionary dictionaryWithObjectsAndKeys:[self HTTPMethod], @"HTTPMethod",
[self URL], @"URL", receivedAt, @"receivedAt", error, @"error", nil];
[[NSNotificationCenter defaultCenter] postNotificationName:kRKRequestFailedWithErrorNotification object:self userInfo:userInfo];
[[NSNotificationCenter defaultCenter] postNotificationName:RKRequestFailedWithErrorNotification object:self userInfo:userInfo];
}
- (void)didFinishLoad:(RKResponse*)response {
@@ -234,9 +218,9 @@
[_delegate request:self didLoadResponse:response];
}
NSDate* receivedAt = [NSDate date];
NSDictionary* userInfo = [NSDictionary dictionaryWithObjectsAndKeys:[self HTTPMethod], @"HTTPMethod", [self URL], @"URL", receivedAt, @"receivedAt", nil];
[[NSNotificationCenter defaultCenter] postNotificationName:kRKResponseReceivedNotification object:response userInfo:userInfo];
NSDictionary* userInfo = [NSDictionary dictionaryWithObject:response forKey:@"response"];
[[NSNotificationCenter defaultCenter] postNotificationName:RKRequestDidLoadResponseNotification object:self userInfo:userInfo];
[[NSNotificationCenter defaultCenter] postNotificationName:RKResponseReceivedNotification object:response userInfo:nil];
if ([response isServiceUnavailable] && [[RKClient sharedClient] serviceUnavailableAlertEnabled]) {
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:[[RKClient sharedClient] serviceUnavailableAlertTitle]

View File

@@ -42,11 +42,11 @@ static const NSInteger kMaxConcurrentLoads = 5;
_totalLoading = 0;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(responseDidLoad:)
name:kRKResponseReceivedNotification
name:RKResponseReceivedNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(responseDidLoad:)
name:kRKRequestFailedWithErrorNotification
name:RKRequestFailedWithErrorNotification
object:nil];
}
return self;

View File

@@ -9,6 +9,7 @@
#import "RKResponse.h"
#import "RKNotifications.h"
#import "RKJSONParser.h"
#import "RKNetwork.h"
@implementation RKResponse
@@ -62,9 +63,9 @@
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
if ([challenge previousFailureCount] == 0) {
NSURLCredential *newCredential;
newCredential=[NSURLCredential credentialWithUser:[NSString stringWithFormat:@"%@", _request.username]
password:[NSString stringWithFormat:@"%@", _request.password]
persistence:NSURLCredentialPersistenceNone];
newCredential = [NSURLCredential credentialWithUser:[NSString stringWithFormat:@"%@", _request.username]
password:[NSString stringWithFormat:@"%@", _request.password]
persistence:RKNetworkGetGlobalCredentialPersistence()];
[[challenge sender] useCredential:newCredential
forAuthenticationChallenge:challenge];
} else {