blob: f5186e20548c019c24182a599fb88db9fc22e120 [file] [log] [blame]
Mark Slee2ac60ed2007-09-19 21:10:18 +00001#import "THTTPClient.h"
2#import "TTransportException.h"
3
4@implementation THTTPClient
5
Mark Slee2ac60ed2007-09-19 21:10:18 +00006
Mark Slee84406052007-11-20 01:39:25 +00007- (void) setupRequest
8{
9 if (mRequest != nil) {
10 [mRequest release];
11 }
12
Mark Slee2ac60ed2007-09-19 21:10:18 +000013 // 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 Reissb0232b32008-01-23 20:35:39 +000018
19 NSString * userAgent = mUserAgent;
20 if (!userAgent) {
21 userAgent = @"Cocoa/THTTPClient";
22 }
23 [mRequest setValue: userAgent forHTTPHeaderField: @"User-Agent"];
24
Mark Slee2ac60ed2007-09-19 21:10:18 +000025 [mRequest setCachePolicy: NSURLRequestReloadIgnoringCacheData];
David Reissb0232b32008-01-23 20:35:39 +000026 if (mTimeout) {
27 [mRequest setTimeoutInterval: mTimeout];
28 }
Mark Slee84406052007-11-20 01:39:25 +000029}
30
31
32- (id) initWithURL: (NSURL *) aURL
33{
David Reissb0232b32008-01-23 20:35:39 +000034 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 Slee84406052007-11-20 01:39:25 +000044 self = [super init];
David Reissb0232b32008-01-23 20:35:39 +000045 if (!self) {
46 return nil;
47 }
48
49 mTimeout = timeout;
50 if (userAgent) {
51 mUserAgent = [userAgent retain];
52 }
Mark Slee84406052007-11-20 01:39:25 +000053 mURL = [aURL retain];
54
55 [self setupRequest];
Mark Slee2ac60ed2007-09-19 21:10:18 +000056
57 // create our request data buffer
58 mRequestData = [[NSMutableData alloc] initWithCapacity: 1024];
David Reissb0232b32008-01-23 20:35:39 +000059
Mark Slee2ac60ed2007-09-19 21:10:18 +000060 return self;
61}
62
63
Mark Slee84406052007-11-20 01:39:25 +000064- (void) setURL: (NSURL *) aURL
65{
66 [aURL retain];
67 [mURL release];
68 mURL = aURL;
69
70 [self setupRequest];
71}
72
73
Mark Slee2ac60ed2007-09-19 21:10:18 +000074- (void) dealloc
75{
76 [mURL release];
David Reissb0232b32008-01-23 20:35:39 +000077 [mUserAgent release];
Mark Slee2ac60ed2007-09-19 21:10:18 +000078 [mRequest release];
79 [mRequestData release];
Mark Slee77575e62007-09-24 19:24:53 +000080 [mResponseData release];
Mark Slee2ac60ed2007-09-19 21:10:18 +000081 [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 Slee77575e62007-09-24 19:24:53 +0000117 @throw [TTransportException exceptionWithName: @"TTransportException"
118 reason: @"Could not make HTTP request"
Mark Slee2ac60ed2007-09-19 21:10:18 +0000119 error: error];
120 }
121 if (![response isKindOfClass: [NSHTTPURLResponse class]]) {
Mark Slee77575e62007-09-24 19:24:53 +0000122 @throw [TTransportException exceptionWithName: @"TTransportException"
123 reason: @"Unexpected NSURLResponse type"];
Mark Slee2ac60ed2007-09-19 21:10:18 +0000124 }
125
126 NSHTTPURLResponse * httpResponse = (NSHTTPURLResponse *) response;
127 if ([httpResponse statusCode] != 200) {
Mark Slee77575e62007-09-24 19:24:53 +0000128 @throw [TTransportException exceptionWithName: @"TTransportException"
129 reason: [NSString stringWithFormat: @"Bad response from HTTP server: %d",
130 [httpResponse statusCode]]];
Mark Slee2ac60ed2007-09-19 21:10:18 +0000131 }
132
133 // phew!
134 [mResponseData release];
135 mResponseData = [responseData retain];
136 mResponseDataOffset = 0;
137}
138
139
140@end