-
Notifications
You must be signed in to change notification settings - Fork 1
/
UnREST.m
247 lines (189 loc) · 6.93 KB
/
UnREST.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
//
// UnREST.m
// UnREST
//
#import "UnREST.h"
#pragma mark -
#pragma mark Private declarations
@interface UnREST ()
@property (nonatomic, retain) NSURLConnection* connection;
@property (nonatomic, retain) NSMutableData* responseData;
@property (nonatomic, retain) NSMutableArray* multiparts;
@end
#pragma mark -
#pragma mark Implementation
@implementation UnREST
@synthesize urlString;
@synthesize connection;
@synthesize responseData;
@synthesize delegate;
@synthesize multiparts;
#pragma mark -
#pragma mark Initialization
+ (UnREST*) unrestWithURLString:(NSString*)urlString delegate:(id<UnRESTDelegate>)delegate {
return [[[self alloc] initWithURLString:urlString delegate:delegate] autorelease];
}
- (id) initWithURLString:(NSString*)urlStr delegate:(id<UnRESTDelegate>)del {
if ((self = [super init])) {
self.urlString = urlStr;
self.delegate = del;
}
return self;
}
#pragma mark -
#pragma mark Networking
/**
* Performs an HTTP GET request.
*/
- (void) get {
self.responseData = [NSMutableData data];
NSURLRequest* request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];
self.connection = [NSURLConnection connectionWithRequest:request delegate:self];
[connection start];
}
/**
* Helper method, sends the string using a given HTTP method.
*
* @param string request payload
* @param method HTTP method to use ("POST", "PUT")
*/
- (void) sendString:(NSString*)string withHttpMethod:(NSString*)method {
// Construct the URL request object
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:method];
[request setHTTPBody:[string dataUsingEncoding:NSUTF8StringEncoding]];
// Create our response placeholder
self.responseData = [NSMutableData data];
// Go
self.connection = [NSURLConnection connectionWithRequest:request delegate:self];
[connection start];
}
/**
* Performs an HTTP PUT request, using string as payload.
*
* @param string PUT request payload.
*/
- (void) putString:(NSString*)string {
[self sendString:string withHttpMethod:@"PUT"];
}
/**
* Performs an HTTP POST request, using string as payload.
*
* @param string POST request payload.
*/
- (void) postString:(NSString *)string {
[self sendString:string withHttpMethod:@"POST"];
}
/**
* Performs an HTTP POST multipart request. The parts array holds values that are to be
* inserted into the request as POST multipart sections. Each part in the parts array
* must be an NSDictionary, or the class explodes.
*
* Each part dictionary must have the following key->value mappings:
*
* - kUnRESTMultipartName -> the name for this multipart section. This is written to the
* "name" field in the "Content-Disposition" POST header, and is used by the server to
* differentiate the parts.
*
* - kUnRESTMultipartType -> a string to be put in the "Content-Type" POST header.
* @"text/plain" for strings, @"image/jpeg" for images, etc.
*
* - kUnRESTMultipartContent -> the actual content of this multipart section. The value for
* this key has to be an NSData object.
*
* - kUnRESTMultipartFilename (OPTIONAL) -> the filename to be put in the "Content-Disposition"
* POST header.
*
* @param parts An array holding data for the POST multipart request
*/
- (void) postMultipart:(NSArray*)parts {
// Construct the URL request object
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
// This is our boundary string
NSData* fullBoundary = [[NSString stringWithFormat:@"\r\n--%@\r\n", kUnRESTBoundary] dataUsingEncoding:NSUTF8StringEncoding];
// Set the Content-type to multipart/form-data
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data, boundary=%@", kUnRESTBoundary];
[request setValue:contentType forHTTPHeaderField:@"Content-type"];
// Start creating our POST body
NSMutableData* postBody = [NSMutableData data];
[postBody appendData:fullBoundary];
for (NSDictionary* part in parts) {
NSString* name = [part objectForKey:kUnRESTMultipartName];
NSString* type = [part objectForKey:kUnRESTMultipartType];
NSData* content = [part objectForKey:kUnRESTMultipartContent];
NSString* filename = [part objectForKey:kUnRESTMultipartFilename];
// Set the "Content-Disposition" header
NSString* contentDisposition;
if (filename) {
contentDisposition = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n", name, filename];
}
else {
contentDisposition = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n", name];
}
[postBody appendData:[contentDisposition dataUsingEncoding:NSUTF8StringEncoding]];
// Set content type
contentType = [NSString stringWithFormat:@"Content-Type: %@\r\n\r\n", type];
[postBody appendData:[contentType dataUsingEncoding:NSUTF8StringEncoding]];
// Set the actual content
[postBody appendData:content];
// On to the next one
[postBody appendData:fullBoundary];
}
// Set the body
[request setHTTPBody:postBody];
// Create our response placeholder
self.responseData = [NSMutableData data];
// Go
self.connection = [NSURLConnection connectionWithRequest:request delegate:self];
[connection start];
}
#pragma mark -
#pragma mark Multipart packing methods
- (void) addTextPart:(NSString*)text name:(NSString*)name {
if (!multiparts) self.multiparts = [NSMutableArray array];
NSDictionary* part = [NSDictionary dictionaryWithObjectsAndKeys:
name, kUnRESTMultipartName,
[text dataUsingEncoding:NSUTF8StringEncoding], kUnRESTMultipartContent,
kUnRESTContentTypePlain, kUnRESTMultipartType,
nil];
[multiparts addObject:part];
}
- (void) addImagePart:(UIImage*)image name:(NSString*)name {
if (!multiparts) self.multiparts = [NSMutableArray array];
NSData* jpegImage = UIImageJPEGRepresentation(image, kUnRESTJPEGQuality);
NSString* filename = [NSString stringWithFormat:@"%@.jpg", name];
NSDictionary* part = [NSDictionary dictionaryWithObjectsAndKeys:
name, kUnRESTMultipartName,
jpegImage, kUnRESTMultipartContent,
kUnRESTContentTypeImageJPEG, kUnRESTMultipartType,
filename, kUnRESTMultipartFilename,
nil];
[multiparts addObject:part];
}
- (void) postParts {
[self postMultipart:multiparts];
}
#pragma mark -
#pragma mark NSURLConnection delegate methods
- (void)connection:(NSURLConnection *)conn didReceiveData:(NSData *)data {
[responseData appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)conn {
self.connection = nil;
[delegate unrest:self didFinishWithResponse:responseData];
self.responseData = nil;
}
- (void)connection:(NSURLConnection *)conn didFailWithError:(NSError *)error {
self.responseData = nil;
self.connection = nil;
[delegate unrest:self didFailWithError:error];
}
#pragma mark -
#pragma mark Memory management
- (void) dealloc {
self.urlString = nil;
self.multiparts = nil;
[super dealloc];
}
@end