r1327@opsdev009 (orig r70766): mcslee | 2007-11-19 17:39:25 -0800
[amiethrift.git] / lib / cocoa / src / transport / THTTPClient.m
blob2e31a984ab05912c338670c95db6e14680c8461e
1 #import "THTTPClient.h"
2 #import "TTransportException.h"
4 @implementation THTTPClient
7 - (void) setupRequest
9   if (mRequest != nil) {
10     [mRequest release];
11   }
12   
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
25   self = [super init];
26   mURL = [aURL retain];
28   [self setupRequest];
30   // create our request data buffer
31   mRequestData = [[NSMutableData alloc] initWithCapacity: 1024];
33   return self;
37 - (id) initWithURL: (NSURL *) aURL 
38            timeout: (int) timeout
40   self = [self initWithURL: aURL];
42   [mRequest setTimeoutInterval: timeout];
44   return self;
48 - (void) setURL: (NSURL *) aURL
50   [aURL retain];
51   [mURL release];
52   mURL = aURL;
53   
54   [self setupRequest];
58 - (void) dealloc
60   [mURL release];
61   [mRequest release];
62   [mRequestData release];
63   [mResponseData release];
64   [super dealloc];
68 - (int) readAll: (uint8_t *) buf offset: (int) off length: (int) len
70   NSRange r;
71   r.location = mResponseDataOffset;
72   r.length = len;
74   [mResponseData getBytes: buf+off range: r];
75   mResponseDataOffset += len;
77   return len;
81 - (void) write: (const uint8_t *) data offset: (unsigned int) offset length: (unsigned int) length
83   [mRequestData appendBytes: data+offset length: length];
87 - (void) flush
89   [mRequest setHTTPBody: mRequestData]; // not sure if it copies the data
91   // make the HTTP request
92   NSURLResponse * response;
93   NSError * error;
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"
102                                 error: error];
103   }
104   if (![response isKindOfClass: [NSHTTPURLResponse class]]) {
105     @throw [TTransportException exceptionWithName: @"TTransportException"
106                                            reason: @"Unexpected NSURLResponse type"];
107   }
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]]];
114   }
115                                 
116   // phew!
117   [mResponseData release];
118   mResponseData = [responseData retain];
119   mResponseDataOffset = 0;
123 @end