1
0
Fork 0
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

128 lines
3.0 KiB

//
// WRTRequest.m
// DDWRT-Monitor
//
// Created by Matthieu Lalonde on 11-05-29.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import "WRTRequest.h"
@implementation WRTRequest
@synthesize username;
@synthesize password;
@synthesize receivedData;
@synthesize delegate;
@synthesize callback;
@synthesize errorCallback;
- (id)init
{
self = [super init];
if (self) {
// Initialization code here.
wrtReachable = false;
}
return self;
}
- (void)dealloc
{
[super dealloc];
}
-(void)doRequest:(id)requestDelegate requestSelector:(SEL)requestSelector uri:(NSString *)uri
{
self.delegate = requestDelegate;
self.callback = requestSelector;
NSString *urlAddress = [NSString stringWithFormat:@"https://%@:%d/%@",
[[NSUserDefaults standardUserDefaults] stringForKey:@"hostname"],
[[NSUserDefaults standardUserDefaults] integerForKey:@"port"],
uri];
NSURL *url = [NSURL URLWithString:urlAddress];
[self request:url];
}
-(void)request:(NSURL *) url
{
theRequest = [[NSMutableURLRequest alloc] initWithURL:url];
theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (theConnection) {
receivedData = [[NSMutableData data] retain];
} else {
// TODO return error
}
}
-(void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
NSLog(@"1");
if ([challenge previousFailureCount] == 0) {
NSLog(@"1.1.1");
NSURLCredential *newCredential;
newCredential = [NSURLCredential credentialWithUser:@"user" password:@"password" persistence:NSURLCredentialPersistenceForSession];
NSLog(@"1.1.2");
[[challenge sender] useCredential:newCredential forAuthenticationChallenge:challenge];
} else {
NSLog(@"1.2");
[[challenge sender] cancelAuthenticationChallenge:challenge];
// TODO Pop up authentication error
NSLog(@"Invalid Username or Password");
}
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSLog(@"2");
[receivedData setLength:0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
NSLog(@"3");
[receivedData appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"4");
[connection release];
[receivedData release];
[theRequest release];
//TODO Return alert
NSLog(@"Connection failed! Error - %@ %@", [error localizedDescription], [[error userInfo] objectForKey:NSErrorFailingURLStringKey]);
if (errorCallback) {
[delegate performSelector:errorCallback withObject:error];
}
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"5");
// TODO: Return the data
if (delegate && callback) {
if ([delegate respondsToSelector:self.callback]) {
[delegate performSelector:self.callback withObject:receivedData];
} else {
// TODO: Return no data
NSLog(@"No response data from delegate");
}
}
[theConnection release];
[receivedData release];
[theRequest release];
}
@end