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 | |
Mark Slee | 2ac60ed | 2007-09-19 21:10:18 +0000 | [diff] [blame] | 6 | |
Mark Slee | 8440605 | 2007-11-20 01:39:25 +0000 | [diff] [blame] | 7 | - (void) setupRequest |
| 8 | { |
| 9 | if (mRequest != nil) { |
| 10 | [mRequest release]; |
| 11 | } |
| 12 | |
Mark Slee | 2ac60ed | 2007-09-19 21:10:18 +0000 | [diff] [blame] | 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]; |
Mark Slee | 8440605 | 2007-11-20 01:39:25 +0000 | [diff] [blame] | 20 | } |
| 21 | |
| 22 | |
| 23 | - (id) initWithURL: (NSURL *) aURL |
| 24 | { |
| 25 | self = [super init]; |
| 26 | mURL = [aURL retain]; |
| 27 | |
| 28 | [self setupRequest]; |
Mark Slee | 2ac60ed | 2007-09-19 21:10:18 +0000 | [diff] [blame] | 29 | |
| 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 Slee | 8440605 | 2007-11-20 01:39:25 +0000 | [diff] [blame] | 48 | - (void) setURL: (NSURL *) aURL |
| 49 | { |
| 50 | [aURL retain]; |
| 51 | [mURL release]; |
| 52 | mURL = aURL; |
| 53 | |
| 54 | [self setupRequest]; |
| 55 | } |
| 56 | |
| 57 | |
Mark Slee | 2ac60ed | 2007-09-19 21:10:18 +0000 | [diff] [blame] | 58 | - (void) dealloc |
| 59 | { |
| 60 | [mURL release]; |
| 61 | [mRequest release]; |
| 62 | [mRequestData release]; |
Mark Slee | 77575e6 | 2007-09-24 19:24:53 +0000 | [diff] [blame] | 63 | [mResponseData release]; |
Mark Slee | 2ac60ed | 2007-09-19 21:10:18 +0000 | [diff] [blame] | 64 | [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 Slee | 77575e6 | 2007-09-24 19:24:53 +0000 | [diff] [blame] | 100 | @throw [TTransportException exceptionWithName: @"TTransportException" |
| 101 | reason: @"Could not make HTTP request" |
Mark Slee | 2ac60ed | 2007-09-19 21:10:18 +0000 | [diff] [blame] | 102 | error: error]; |
| 103 | } |
| 104 | if (![response isKindOfClass: [NSHTTPURLResponse class]]) { |
Mark Slee | 77575e6 | 2007-09-24 19:24:53 +0000 | [diff] [blame] | 105 | @throw [TTransportException exceptionWithName: @"TTransportException" |
| 106 | reason: @"Unexpected NSURLResponse type"]; |
Mark Slee | 2ac60ed | 2007-09-19 21:10:18 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | NSHTTPURLResponse * httpResponse = (NSHTTPURLResponse *) response; |
| 110 | if ([httpResponse statusCode] != 200) { |
Mark Slee | 77575e6 | 2007-09-24 19:24:53 +0000 | [diff] [blame] | 111 | @throw [TTransportException exceptionWithName: @"TTransportException" |
| 112 | reason: [NSString stringWithFormat: @"Bad response from HTTP server: %d", |
| 113 | [httpResponse statusCode]]]; |
Mark Slee | 2ac60ed | 2007-09-19 21:10:18 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | // phew! |
| 117 | [mResponseData release]; |
| 118 | mResponseData = [responseData retain]; |
| 119 | mResponseDataOffset = 0; |
| 120 | } |
| 121 | |
| 122 | |
| 123 | @end |