1 #import "THTTPClient.h"
2 #import "TTransportException.h"
4 @implementation THTTPClient
13 // 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];
23 - (id) initWithURL: (NSURL *) aURL
30 // create our request data buffer
31 mRequestData = [[NSMutableData alloc] initWithCapacity: 1024];
37 - (id) initWithURL: (NSURL *) aURL
38 timeout: (int) timeout
40 self = [self initWithURL: aURL];
42 [mRequest setTimeoutInterval: timeout];
48 - (void) setURL: (NSURL *) aURL
62 [mRequestData release];
63 [mResponseData release];
68 - (int) readAll: (uint8_t *) buf offset: (int) off length: (int) len
71 r.location = mResponseDataOffset;
74 [mResponseData getBytes: buf+off range: r];
75 mResponseDataOffset += len;
81 - (void) write: (const uint8_t *) data offset: (unsigned int) offset length: (unsigned int) length
83 [mRequestData appendBytes: data+offset length: length];
89 [mRequest setHTTPBody: mRequestData]; // not sure if it copies the data
91 // make the HTTP request
92 NSURLResponse * response;
94 NSData * responseData =
95 [NSURLConnection sendSynchronousRequest: mRequest returningResponse: &response error: &error];
97 [mRequestData setLength: 0];
99 if (responseData == nil) {
100 @throw [TTransportException exceptionWithName: @"TTransportException"
101 reason: @"Could not make HTTP request"
104 if (![response isKindOfClass: [NSHTTPURLResponse class]]) {
105 @throw [TTransportException exceptionWithName: @"TTransportException"
106 reason: @"Unexpected NSURLResponse type"];
109 NSHTTPURLResponse * httpResponse = (NSHTTPURLResponse *) response;
110 if ([httpResponse statusCode] != 200) {
111 @throw [TTransportException exceptionWithName: @"TTransportException"
112 reason: [NSString stringWithFormat: @"Bad response from HTTP server: %d",
113 [httpResponse statusCode]]];
117 [mResponseData release];
118 mResponseData = [responseData retain];
119 mResponseDataOffset = 0;