blob: 45c0b80ec81f2707fed432015d20206538a8c3a0 [file] [log] [blame]
Mark Slee2ac60ed2007-09-19 21:10:18 +00001#import "THTTPClient.h"
2#import "TTransportException.h"
3
4@implementation THTTPClient
5
6- (id) initWithURL: (NSURL *) aURL
7{
8 self = [super init];
9 mURL = [aURL retain];
10
11 // set up our request object that we'll use for each request
12 mRequest = [[NSMutableURLRequest alloc] initWithURL: mURL];
13 [mRequest setHTTPMethod: @"POST"];
14 [mRequest setValue: @"application/x-thrift" forHTTPHeaderField: @"Content-Type"];
15 [mRequest setValue: @"application/x-thrift" forHTTPHeaderField: @"Accept"];
16 [mRequest setValue: @"Cocoa/THTTPClient" forHTTPHeaderField: @"User-Agent"];
17 [mRequest setCachePolicy: NSURLRequestReloadIgnoringCacheData];
18
19 // create our request data buffer
20 mRequestData = [[NSMutableData alloc] initWithCapacity: 1024];
21
22 return self;
23}
24
25
26- (id) initWithURL: (NSURL *) aURL
27 timeout: (int) timeout
28{
29 self = [self initWithURL: aURL];
30
31 [mRequest setTimeoutInterval: timeout];
32
33 return self;
34}
35
36
37- (void) dealloc
38{
39 [mURL release];
40 [mRequest release];
41 [mRequestData release];
Mark Slee77575e62007-09-24 19:24:53 +000042 [mResponseData release];
Mark Slee2ac60ed2007-09-19 21:10:18 +000043 [super dealloc];
44}
45
46
47- (int) readAll: (uint8_t *) buf offset: (int) off length: (int) len
48{
49 NSRange r;
50 r.location = mResponseDataOffset;
51 r.length = len;
52
53 [mResponseData getBytes: buf+off range: r];
54 mResponseDataOffset += len;
55
56 return len;
57}
58
59
60- (void) write: (const uint8_t *) data offset: (unsigned int) offset length: (unsigned int) length
61{
62 [mRequestData appendBytes: data+offset length: length];
63}
64
65
66- (void) flush
67{
68 [mRequest setHTTPBody: mRequestData]; // not sure if it copies the data
69
70 // make the HTTP request
71 NSURLResponse * response;
72 NSError * error;
73 NSData * responseData =
74 [NSURLConnection sendSynchronousRequest: mRequest returningResponse: &response error: &error];
75
76 [mRequestData setLength: 0];
77
78 if (responseData == nil) {
Mark Slee77575e62007-09-24 19:24:53 +000079 @throw [TTransportException exceptionWithName: @"TTransportException"
80 reason: @"Could not make HTTP request"
Mark Slee2ac60ed2007-09-19 21:10:18 +000081 error: error];
82 }
83 if (![response isKindOfClass: [NSHTTPURLResponse class]]) {
Mark Slee77575e62007-09-24 19:24:53 +000084 @throw [TTransportException exceptionWithName: @"TTransportException"
85 reason: @"Unexpected NSURLResponse type"];
Mark Slee2ac60ed2007-09-19 21:10:18 +000086 }
87
88 NSHTTPURLResponse * httpResponse = (NSHTTPURLResponse *) response;
89 if ([httpResponse statusCode] != 200) {
Mark Slee77575e62007-09-24 19:24:53 +000090 @throw [TTransportException exceptionWithName: @"TTransportException"
91 reason: [NSString stringWithFormat: @"Bad response from HTTP server: %d",
92 [httpResponse statusCode]]];
Mark Slee2ac60ed2007-09-19 21:10:18 +000093 }
94
95 // phew!
96 [mResponseData release];
97 mResponseData = [responseData retain];
98 mResponseDataOffset = 0;
99}
100
101
102@end