mirror of
https://github.com/zhigang1992/RestKit.git
synced 2026-04-21 19:42:59 +08:00
Fix for issue where authentication challenges without credentials would cause stream errors when coupled with an RKParams (NSInputStream) based params payload
This commit is contained in:
@@ -83,8 +83,19 @@ extern NSString* cacheURLKey;
|
||||
[super dealloc];
|
||||
}
|
||||
|
||||
- (BOOL)hasCredentials {
|
||||
return _request.username && _request.password;
|
||||
}
|
||||
|
||||
// Handle basic auth
|
||||
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
|
||||
RKLogDebug(@"Received authentication challenge");
|
||||
if (! [self hasCredentials]) {
|
||||
RKLogWarning(@"Received an authentication challenge without any credentials to satify the request.");
|
||||
[[challenge sender] cancelAuthenticationChallenge:challenge];
|
||||
return;
|
||||
}
|
||||
|
||||
if ([challenge previousFailureCount] == 0) {
|
||||
NSURLCredential *newCredential;
|
||||
newCredential = [NSURLCredential credentialWithUser:[NSString stringWithFormat:@"%@", _request.username]
|
||||
@@ -93,6 +104,7 @@ extern NSString* cacheURLKey;
|
||||
[[challenge sender] useCredential:newCredential
|
||||
forAuthenticationChallenge:challenge];
|
||||
} else {
|
||||
RKLogWarning(@"Failed authentication challenge after %d failures", [challenge previousFailureCount]);
|
||||
[[challenge sender] cancelAuthenticationChallenge:challenge];
|
||||
}
|
||||
}
|
||||
@@ -117,6 +129,11 @@ extern NSString* cacheURLKey;
|
||||
[_request didFailLoadWithError:_failureError];
|
||||
}
|
||||
|
||||
- (NSInputStream *)connection:(NSURLConnection *)connection needNewBodyStream:(NSURLRequest *)request {
|
||||
RKLogWarning(@"RestKit was asked to retransmit a new body stream for a request. Possible connection error or authentication challenge?");
|
||||
return [self.request.params HTTPBodyStream];
|
||||
}
|
||||
|
||||
// In the event that the url request is a post, this delegate method will be called before
|
||||
// either connection:didReceiveData: or connection:didReceiveResponse:
|
||||
// However this method is only called if there is payload data to be sent.
|
||||
|
||||
@@ -72,4 +72,19 @@
|
||||
assertThatBool([RKRequestQueue sharedQueue].suspended, is(equalToBool(NO)));
|
||||
}
|
||||
|
||||
- (void)itShouldPerformAPUtWithParams {
|
||||
RKClient* client = [RKClient clientWithBaseURL:@"http://ohblockhero.appspot.com/api/v1"];
|
||||
client.cachePolicy = RKRequestCachePolicyNone;
|
||||
RKParams *params=[RKParams params];
|
||||
[params setValue:@"username" forParam:@"username"];
|
||||
[params setValue:@"Dear Daniel" forParam:@"fullName"];
|
||||
[params setValue:@"aa@aa.com" forParam:@"email"];
|
||||
RKLogConfigureByName("RestKit/Network*", RKLogLevelTrace);
|
||||
RKSpecResponseLoader* loader = [RKSpecResponseLoader responseLoader];
|
||||
loader.timeout = 15;
|
||||
[client put:@"/userprofile" params:params delegate:loader];
|
||||
[loader waitForResponse];
|
||||
assertThatBool(loader.success, is(equalToBool(NO)));
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
Reference in New Issue
Block a user