forked from Ascoware/get-iplayer-automator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HTTPRequest.m
171 lines (139 loc) · 5.29 KB
/
HTTPRequest.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
//
// HTTPRequest.m
//
// Created by Samuel Colak on 11/4/11.
//
#import "HTTPRequest.h"
@implementation HTTPRequest
@synthesize delegate;
@synthesize headers=_headers;
@synthesize contentType=_contentType;
@synthesize password=_password;
@synthesize username=_username;
@synthesize bodyContent=_bodyContent;
#pragma mark - Instantiation
+ (HTTPRequest *) requestWithURL:(NSURL *)url
{
return [[HTTPRequest alloc] initWithURL:url];
}
- (id) initWithURL:(NSURL *)url
{
return [self initWithURL:url timeout:60.0 method:@"PUT"];
}
- (id) initWithURL:(NSURL *)url timeout:(float)timeout method:(NSString *)method
{
self = [super init];
if (self) {
_URL = url;
_responseCode = kHTTPCodeUndefined;
_responseData = nil;
_inProgress = NO;
_contentType = @"text/plain; charset=utf-8";
_headers = [[NSMutableDictionary alloc] init];
_request = [NSMutableURLRequest requestWithURL:_URL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:timeout];
[_request setHTTPMethod:method];
}
return self;
}
#pragma mark - Properties and getters
- (NSURL *) getURL
{
return _URL;
}
- (BOOL) getInProgress
{
return _inProgress;
}
- (NSData *) getResponseData
{
return _responseData;
}
- (NSInteger) getResponseStatusCode
{
return _responseCode;
}
#pragma mark - Functions
- (void) addRequestHeader:(NSString *)key value:(NSString *)data
{
[_headers setValue:data forKey:key];
}
- (void) start
{
if (_inProgress) return;
_inProgress = YES;
_responseCode = kHTTPCodeUndefined;
_responseData = nil;
[_request addValue:_contentType forHTTPHeaderField:@"Content-Type"];
if (_headers.count > 0) {
for (NSString *key in _headers.allKeys)
{
[_request addValue:[_headers valueForKey:key] forHTTPHeaderField:key];
}
}
if (_bodyContent != nil) {
[_request addValue:[NSString stringWithFormat:@"%d", _bodyContent.length] forHTTPHeaderField:@"Content-Length"];
[_request setHTTPBody: _bodyContent];
}
_connection = [[NSURLConnection alloc] initWithRequest:_request delegate:self];
if (_connection) {
if (delegate && [delegate respondsToSelector:@selector(request:initialized:)]) {
[delegate request:self initialized:_URL];
}
} else {
// connection failed ...
_responseCode = kHTTPCodeServerServiceUnavailable;
if (delegate && [delegate respondsToSelector:@selector(request:failed:)]) {
[delegate request:self failed:nil];
}
_inProgress = NO;
}
}
#pragma mark - Delegate related functions
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
// if you want to execute your own challenge functionality here....
if (delegate && [delegate respondsToSelector:@selector(request:receivedChallenge:)]) {
[delegate request:self receivedChallenge:challenge];
} else {
// automate the response using the username / password information..
if ([challenge previousFailureCount] == 0 && ![challenge proposedCredential]) {
NSURLCredential *_credentials = [NSURLCredential credentialWithUser:_username password:_password persistence:NSURLCredentialPersistenceNone];
[[challenge sender] useCredential:_credentials forAuthenticationChallenge:challenge];
} else {
if (delegate && [delegate respondsToSelector:@selector(request:authenticationFailed:)]) {
[delegate request:self authenticationFailed:challenge];
} else {
[[challenge sender] cancelAuthenticationChallenge:challenge];
}
}
}
}
- (void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[_responseData appendData:data];
}
- (void) connectionDidFinishLoading:(NSURLConnection *)connection
{
if (delegate && [delegate respondsToSelector:@selector(request:receivedData:)]) {
[delegate request:self receivedData:_responseData];
}
}
- (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
_responseData = [[NSMutableData alloc] init];
NSHTTPURLResponse *_httpResponse = (NSHTTPURLResponse *)response;
_responseCode = [_httpResponse statusCode];
if (delegate && [delegate respondsToSelector:@selector(request:connected:)]) {
[delegate request:self connected:response];
}
_inProgress = NO;
}
- (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
_responseCode = kHTTPCodeServerInternalServer;
if (delegate && [delegate respondsToSelector:@selector(request:failed:)]) {
[delegate request:self failed:error];
}
_inProgress = NO;
}
@end