Guest User

Untitled

a guest
May 3rd, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. + (Response *)sendRequest:(NSMutableURLRequest *)request withUser:(NSString *)user andPassword:(NSString *)password {
  2.  
  3. //lots of servers fail to implement http basic authentication correctly, so we pass the credentials even if they are not asked for
  4. //TODO make this configurable?
  5. NSURL *url = [request URL];
  6. if(user && password) {
  7. NSString *authString = [[[NSString stringWithFormat:@"%@:%@",user, password] dataUsingEncoding:NSUTF8StringEncoding] base64Encoding];
  8. [request addValue:[NSString stringWithFormat:@"Basic %@", authString] forHTTPHeaderField:@"Authorization"];
  9. NSString *escapedUser = (NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
  10. (CFStringRef)user, NULL, (CFStringRef)@"@.:", kCFStringEncodingUTF8);
  11. NSString *escapedPassword = (NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
  12. (CFStringRef)password, NULL, (CFStringRef)@"@.:", kCFStringEncodingUTF8);
  13. NSMutableString *urlString = [NSMutableString stringWithFormat:@"%@://%@:%@@%@",[url scheme],escapedUser,escapedPassword,[url host],nil];
  14. if([url port]) {
  15. [urlString appendFormat:@":%@",[url port],nil];
  16. }
  17. [urlString appendString:[url path]];
  18. if([url query]){
  19. [urlString appendFormat:@"?%@",[url query],nil];
  20. }
  21. [request setURL:[NSURL URLWithString:urlString]];
  22. [escapedUser release];
  23. [escapedPassword release];
  24. }
  25.  
  26.  
  27. [self logRequest:request to:[url absoluteString]];
  28.  
  29. ConnectionDelegate *connectionDelegate = [[[ConnectionDelegate alloc] init] autorelease];
  30.  
  31. [[self activeDelegates] addObject:connectionDelegate];
  32. NSURLConnection *connection = [[[NSURLConnection alloc] initWithRequest:request delegate:connectionDelegate startImmediately:NO] autorelease];
  33. connectionDelegate.connection = connection;
  34.  
  35.  
  36. //use a custom runloop
  37. static NSString *runLoopMode = @"com.yfactorial.objectiveresource.connectionLoop";
  38. [connection scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:runLoopMode];
  39. [connection start];
  40. while (![connectionDelegate isDone]) {
  41. [[NSRunLoop currentRunLoop] runMode:runLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:.3]];
  42. }
  43. Response *resp = [Response responseFrom:(NSHTTPURLResponse *)connectionDelegate.response
  44. withBody:connectionDelegate.data
  45. andError:connectionDelegate.error];
  46. [resp log];
  47.  
  48. [activeDelegates removeObject:connectionDelegate];
  49.  
  50. //if there are no more active delegates release the array
  51. if (0 == [activeDelegates count]) {
  52. NSMutableArray *tempDelegates = activeDelegates;
  53. activeDelegates = nil;
  54. [tempDelegates release];
  55. }
  56.  
  57. return resp;
  58. }
Add Comment
Please, Sign In to add comment