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"]; |
David Reiss | b0232b3 | 2008-01-23 20:35:39 +0000 | [diff] [blame^] | 18 | |
| 19 | NSString * userAgent = mUserAgent; |
| 20 | if (!userAgent) { |
| 21 | userAgent = @"Cocoa/THTTPClient"; |
| 22 | } |
| 23 | [mRequest setValue: userAgent forHTTPHeaderField: @"User-Agent"]; |
| 24 | |
Mark Slee | 2ac60ed | 2007-09-19 21:10:18 +0000 | [diff] [blame] | 25 | [mRequest setCachePolicy: NSURLRequestReloadIgnoringCacheData]; |
David Reiss | b0232b3 | 2008-01-23 20:35:39 +0000 | [diff] [blame^] | 26 | if (mTimeout) { |
| 27 | [mRequest setTimeoutInterval: mTimeout]; |
| 28 | } |
Mark Slee | 8440605 | 2007-11-20 01:39:25 +0000 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | |
| 32 | - (id) initWithURL: (NSURL *) aURL |
| 33 | { |
David Reiss | b0232b3 | 2008-01-23 20:35:39 +0000 | [diff] [blame^] | 34 | return [self initWithURL: aURL |
| 35 | userAgent: nil |
| 36 | timeout: 0]; |
| 37 | } |
| 38 | |
| 39 | |
| 40 | - (id) initWithURL: (NSURL *) aURL |
| 41 | userAgent: (NSString *) userAgent |
| 42 | timeout: (int) timeout |
| 43 | { |
Mark Slee | 8440605 | 2007-11-20 01:39:25 +0000 | [diff] [blame] | 44 | self = [super init]; |
David Reiss | b0232b3 | 2008-01-23 20:35:39 +0000 | [diff] [blame^] | 45 | if (!self) { |
| 46 | return nil; |
| 47 | } |
| 48 | |
| 49 | mTimeout = timeout; |
| 50 | if (userAgent) { |
| 51 | mUserAgent = [userAgent retain]; |
| 52 | } |
Mark Slee | 8440605 | 2007-11-20 01:39:25 +0000 | [diff] [blame] | 53 | mURL = [aURL retain]; |
| 54 | |
| 55 | [self setupRequest]; |
Mark Slee | 2ac60ed | 2007-09-19 21:10:18 +0000 | [diff] [blame] | 56 | |
| 57 | // create our request data buffer |
| 58 | mRequestData = [[NSMutableData alloc] initWithCapacity: 1024]; |
David Reiss | b0232b3 | 2008-01-23 20:35:39 +0000 | [diff] [blame^] | 59 | |
Mark Slee | 2ac60ed | 2007-09-19 21:10:18 +0000 | [diff] [blame] | 60 | return self; |
| 61 | } |
| 62 | |
| 63 | |
Mark Slee | 8440605 | 2007-11-20 01:39:25 +0000 | [diff] [blame] | 64 | - (void) setURL: (NSURL *) aURL |
| 65 | { |
| 66 | [aURL retain]; |
| 67 | [mURL release]; |
| 68 | mURL = aURL; |
| 69 | |
| 70 | [self setupRequest]; |
| 71 | } |
| 72 | |
| 73 | |
Mark Slee | 2ac60ed | 2007-09-19 21:10:18 +0000 | [diff] [blame] | 74 | - (void) dealloc |
| 75 | { |
| 76 | [mURL release]; |
David Reiss | b0232b3 | 2008-01-23 20:35:39 +0000 | [diff] [blame^] | 77 | [mUserAgent release]; |
Mark Slee | 2ac60ed | 2007-09-19 21:10:18 +0000 | [diff] [blame] | 78 | [mRequest release]; |
| 79 | [mRequestData release]; |
Mark Slee | 77575e6 | 2007-09-24 19:24:53 +0000 | [diff] [blame] | 80 | [mResponseData release]; |
Mark Slee | 2ac60ed | 2007-09-19 21:10:18 +0000 | [diff] [blame] | 81 | [super dealloc]; |
| 82 | } |
| 83 | |
| 84 | |
| 85 | - (int) readAll: (uint8_t *) buf offset: (int) off length: (int) len |
| 86 | { |
| 87 | NSRange r; |
| 88 | r.location = mResponseDataOffset; |
| 89 | r.length = len; |
| 90 | |
| 91 | [mResponseData getBytes: buf+off range: r]; |
| 92 | mResponseDataOffset += len; |
| 93 | |
| 94 | return len; |
| 95 | } |
| 96 | |
| 97 | |
| 98 | - (void) write: (const uint8_t *) data offset: (unsigned int) offset length: (unsigned int) length |
| 99 | { |
| 100 | [mRequestData appendBytes: data+offset length: length]; |
| 101 | } |
| 102 | |
| 103 | |
| 104 | - (void) flush |
| 105 | { |
| 106 | [mRequest setHTTPBody: mRequestData]; // not sure if it copies the data |
| 107 | |
| 108 | // make the HTTP request |
| 109 | NSURLResponse * response; |
| 110 | NSError * error; |
| 111 | NSData * responseData = |
| 112 | [NSURLConnection sendSynchronousRequest: mRequest returningResponse: &response error: &error]; |
| 113 | |
| 114 | [mRequestData setLength: 0]; |
| 115 | |
| 116 | if (responseData == nil) { |
Mark Slee | 77575e6 | 2007-09-24 19:24:53 +0000 | [diff] [blame] | 117 | @throw [TTransportException exceptionWithName: @"TTransportException" |
| 118 | reason: @"Could not make HTTP request" |
Mark Slee | 2ac60ed | 2007-09-19 21:10:18 +0000 | [diff] [blame] | 119 | error: error]; |
| 120 | } |
| 121 | if (![response isKindOfClass: [NSHTTPURLResponse class]]) { |
Mark Slee | 77575e6 | 2007-09-24 19:24:53 +0000 | [diff] [blame] | 122 | @throw [TTransportException exceptionWithName: @"TTransportException" |
| 123 | reason: @"Unexpected NSURLResponse type"]; |
Mark Slee | 2ac60ed | 2007-09-19 21:10:18 +0000 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | NSHTTPURLResponse * httpResponse = (NSHTTPURLResponse *) response; |
| 127 | if ([httpResponse statusCode] != 200) { |
Mark Slee | 77575e6 | 2007-09-24 19:24:53 +0000 | [diff] [blame] | 128 | @throw [TTransportException exceptionWithName: @"TTransportException" |
| 129 | reason: [NSString stringWithFormat: @"Bad response from HTTP server: %d", |
| 130 | [httpResponse statusCode]]]; |
Mark Slee | 2ac60ed | 2007-09-19 21:10:18 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | // phew! |
| 134 | [mResponseData release]; |
| 135 | mResponseData = [responseData retain]; |
| 136 | mResponseDataOffset = 0; |
| 137 | } |
| 138 | |
| 139 | |
| 140 | @end |