blob: 2e31a984ab05912c338670c95db6e14680c8461e [file] [log] [blame]
Mark Slee2ac60ed2007-09-19 21:10:18 +00001#import "THTTPClient.h"
2#import "TTransportException.h"
3
4@implementation THTTPClient
5
Mark Slee2ac60ed2007-09-19 21:10:18 +00006
Mark Slee84406052007-11-20 01:39:25 +00007- (void) setupRequest
8{
9 if (mRequest != nil) {
10 [mRequest release];
11 }
12
Mark Slee2ac60ed2007-09-19 21:10:18 +000013 // set up our request object that we'll use for each request
14 mRequest = [[NSMutableURLRequest alloc] initWithURL: mURL];
15 [mRequest setHTTPMethod: @"POST"];
16 [mRequest setValue: @"application/x-thrift" forHTTPHeaderField: @"Content-Type"];
17 [mRequest setValue: @"application/x-thrift" forHTTPHeaderField: @"Accept"];
18 [mRequest setValue: @"Cocoa/THTTPClient" forHTTPHeaderField: @"User-Agent"];
19 [mRequest setCachePolicy: NSURLRequestReloadIgnoringCacheData];
Mark Slee84406052007-11-20 01:39:25 +000020}
21
22
23- (id) initWithURL: (NSURL *) aURL
24{
25 self = [super init];
26 mURL = [aURL retain];
27
28 [self setupRequest];
Mark Slee2ac60ed2007-09-19 21:10:18 +000029
30 // create our request data buffer
31 mRequestData = [[NSMutableData alloc] initWithCapacity: 1024];
32
33 return self;
34}
35
36
37- (id) initWithURL: (NSURL *) aURL
38 timeout: (int) timeout
39{
40 self = [self initWithURL: aURL];
41
42 [mRequest setTimeoutInterval: timeout];
43
44 return self;
45}
46
47
Mark Slee84406052007-11-20 01:39:25 +000048- (void) setURL: (NSURL *) aURL
49{
50 [aURL retain];
51 [mURL release];
52 mURL = aURL;
53
54 [self setupRequest];
55}
56
57
Mark Slee2ac60ed2007-09-19 21:10:18 +000058- (void) dealloc
59{
60 [mURL release];
61 [mRequest release];
62 [mRequestData release];
Mark Slee77575e62007-09-24 19:24:53 +000063 [mResponseData release];
Mark Slee2ac60ed2007-09-19 21:10:18 +000064 [super dealloc];
65}
66
67
68- (int) readAll: (uint8_t *) buf offset: (int) off length: (int) len
69{
70 NSRange r;
71 r.location = mResponseDataOffset;
72 r.length = len;
73
74 [mResponseData getBytes: buf+off range: r];
75 mResponseDataOffset += len;
76
77 return len;
78}
79
80
81- (void) write: (const uint8_t *) data offset: (unsigned int) offset length: (unsigned int) length
82{
83 [mRequestData appendBytes: data+offset length: length];
84}
85
86
87- (void) flush
88{
89 [mRequest setHTTPBody: mRequestData]; // not sure if it copies the data
90
91 // make the HTTP request
92 NSURLResponse * response;
93 NSError * error;
94 NSData * responseData =
95 [NSURLConnection sendSynchronousRequest: mRequest returningResponse: &response error: &error];
96
97 [mRequestData setLength: 0];
98
99 if (responseData == nil) {
Mark Slee77575e62007-09-24 19:24:53 +0000100 @throw [TTransportException exceptionWithName: @"TTransportException"
101 reason: @"Could not make HTTP request"
Mark Slee2ac60ed2007-09-19 21:10:18 +0000102 error: error];
103 }
104 if (![response isKindOfClass: [NSHTTPURLResponse class]]) {
Mark Slee77575e62007-09-24 19:24:53 +0000105 @throw [TTransportException exceptionWithName: @"TTransportException"
106 reason: @"Unexpected NSURLResponse type"];
Mark Slee2ac60ed2007-09-19 21:10:18 +0000107 }
108
109 NSHTTPURLResponse * httpResponse = (NSHTTPURLResponse *) response;
110 if ([httpResponse statusCode] != 200) {
Mark Slee77575e62007-09-24 19:24:53 +0000111 @throw [TTransportException exceptionWithName: @"TTransportException"
112 reason: [NSString stringWithFormat: @"Bad response from HTTP server: %d",
113 [httpResponse statusCode]]];
Mark Slee2ac60ed2007-09-19 21:10:18 +0000114 }
115
116 // phew!
117 [mResponseData release];
118 mResponseData = [responseData retain];
119 mResponseDataOffset = 0;
120}
121
122
123@end