Mark Slee | 2ac60ed | 2007-09-19 21:10:18 +0000 | [diff] [blame] | 1 | #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 Slee | 77575e6 | 2007-09-24 19:24:53 +0000 | [diff] [blame] | 42 | [mResponseData release]; |
Mark Slee | 2ac60ed | 2007-09-19 21:10:18 +0000 | [diff] [blame] | 43 | [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 Slee | 77575e6 | 2007-09-24 19:24:53 +0000 | [diff] [blame] | 79 | @throw [TTransportException exceptionWithName: @"TTransportException" |
| 80 | reason: @"Could not make HTTP request" |
Mark Slee | 2ac60ed | 2007-09-19 21:10:18 +0000 | [diff] [blame] | 81 | error: error]; |
| 82 | } |
| 83 | if (![response isKindOfClass: [NSHTTPURLResponse class]]) { |
Mark Slee | 77575e6 | 2007-09-24 19:24:53 +0000 | [diff] [blame] | 84 | @throw [TTransportException exceptionWithName: @"TTransportException" |
| 85 | reason: @"Unexpected NSURLResponse type"]; |
Mark Slee | 2ac60ed | 2007-09-19 21:10:18 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | NSHTTPURLResponse * httpResponse = (NSHTTPURLResponse *) response; |
| 89 | if ([httpResponse statusCode] != 200) { |
Mark Slee | 77575e6 | 2007-09-24 19:24:53 +0000 | [diff] [blame] | 90 | @throw [TTransportException exceptionWithName: @"TTransportException" |
| 91 | reason: [NSString stringWithFormat: @"Bad response from HTTP server: %d", |
| 92 | [httpResponse statusCode]]]; |
Mark Slee | 2ac60ed | 2007-09-19 21:10:18 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | // phew! |
| 96 | [mResponseData release]; |
| 97 | mResponseData = [responseData retain]; |
| 98 | mResponseDataOffset = 0; |
| 99 | } |
| 100 | |
| 101 | |
| 102 | @end |