blob: 5617d452eb598a9bde895aa86e0ef6a6cd37b62e [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +00001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
Mark Slee2ac60ed2007-09-19 21:10:18 +000020#import "THTTPClient.h"
21#import "TTransportException.h"
Jake Farrell9689d892011-12-06 01:07:17 +000022#import "TObjective-C.h"
Mark Slee2ac60ed2007-09-19 21:10:18 +000023
24@implementation THTTPClient
25
Mark Slee2ac60ed2007-09-19 21:10:18 +000026
Mark Slee84406052007-11-20 01:39:25 +000027- (void) setupRequest
28{
29 if (mRequest != nil) {
Jake Farrell9689d892011-12-06 01:07:17 +000030 [mRequest release_stub];
Mark Slee84406052007-11-20 01:39:25 +000031 }
David Reiss0c90f6f2008-02-06 22:18:40 +000032
Mark Slee2ac60ed2007-09-19 21:10:18 +000033 // set up our request object that we'll use for each request
34 mRequest = [[NSMutableURLRequest alloc] initWithURL: mURL];
35 [mRequest setHTTPMethod: @"POST"];
36 [mRequest setValue: @"application/x-thrift" forHTTPHeaderField: @"Content-Type"];
37 [mRequest setValue: @"application/x-thrift" forHTTPHeaderField: @"Accept"];
David Reissb0232b32008-01-23 20:35:39 +000038
39 NSString * userAgent = mUserAgent;
40 if (!userAgent) {
41 userAgent = @"Cocoa/THTTPClient";
42 }
43 [mRequest setValue: userAgent forHTTPHeaderField: @"User-Agent"];
44
Mark Slee2ac60ed2007-09-19 21:10:18 +000045 [mRequest setCachePolicy: NSURLRequestReloadIgnoringCacheData];
David Reissb0232b32008-01-23 20:35:39 +000046 if (mTimeout) {
47 [mRequest setTimeoutInterval: mTimeout];
48 }
Mark Slee84406052007-11-20 01:39:25 +000049}
50
51
52- (id) initWithURL: (NSURL *) aURL
53{
David Reissb0232b32008-01-23 20:35:39 +000054 return [self initWithURL: aURL
55 userAgent: nil
56 timeout: 0];
57}
58
59
David Reiss0c90f6f2008-02-06 22:18:40 +000060- (id) initWithURL: (NSURL *) aURL
David Reissb0232b32008-01-23 20:35:39 +000061 userAgent: (NSString *) userAgent
62 timeout: (int) timeout
63{
Mark Slee84406052007-11-20 01:39:25 +000064 self = [super init];
David Reissb0232b32008-01-23 20:35:39 +000065 if (!self) {
66 return nil;
67 }
David Reiss0c90f6f2008-02-06 22:18:40 +000068
David Reissb0232b32008-01-23 20:35:39 +000069 mTimeout = timeout;
70 if (userAgent) {
Jake Farrell9689d892011-12-06 01:07:17 +000071 mUserAgent = [userAgent retain_stub];
David Reissb0232b32008-01-23 20:35:39 +000072 }
Jake Farrell9689d892011-12-06 01:07:17 +000073 mURL = [aURL retain_stub];
Mark Slee84406052007-11-20 01:39:25 +000074
75 [self setupRequest];
Mark Slee2ac60ed2007-09-19 21:10:18 +000076
77 // create our request data buffer
78 mRequestData = [[NSMutableData alloc] initWithCapacity: 1024];
David Reiss0c90f6f2008-02-06 22:18:40 +000079
Mark Slee2ac60ed2007-09-19 21:10:18 +000080 return self;
81}
82
83
Mark Slee84406052007-11-20 01:39:25 +000084- (void) setURL: (NSURL *) aURL
85{
Jake Farrell9689d892011-12-06 01:07:17 +000086 [aURL retain_stub];
87 [mURL release_stub];
Mark Slee84406052007-11-20 01:39:25 +000088 mURL = aURL;
David Reiss0c90f6f2008-02-06 22:18:40 +000089
Mark Slee84406052007-11-20 01:39:25 +000090 [self setupRequest];
91}
92
93
Mark Slee2ac60ed2007-09-19 21:10:18 +000094- (void) dealloc
95{
Jake Farrell9689d892011-12-06 01:07:17 +000096 [mURL release_stub];
97 [mUserAgent release_stub];
98 [mRequest release_stub];
99 [mRequestData release_stub];
100 [mResponseData release_stub];
101 [super dealloc_stub];
Mark Slee2ac60ed2007-09-19 21:10:18 +0000102}
103
104
105- (int) readAll: (uint8_t *) buf offset: (int) off length: (int) len
106{
107 NSRange r;
108 r.location = mResponseDataOffset;
109 r.length = len;
110
111 [mResponseData getBytes: buf+off range: r];
112 mResponseDataOffset += len;
113
114 return len;
115}
116
117
118- (void) write: (const uint8_t *) data offset: (unsigned int) offset length: (unsigned int) length
119{
120 [mRequestData appendBytes: data+offset length: length];
121}
122
123
124- (void) flush
125{
126 [mRequest setHTTPBody: mRequestData]; // not sure if it copies the data
127
128 // make the HTTP request
129 NSURLResponse * response;
130 NSError * error;
David Reiss0c90f6f2008-02-06 22:18:40 +0000131 NSData * responseData =
Mark Slee2ac60ed2007-09-19 21:10:18 +0000132 [NSURLConnection sendSynchronousRequest: mRequest returningResponse: &response error: &error];
133
134 [mRequestData setLength: 0];
135
136 if (responseData == nil) {
Mark Slee77575e62007-09-24 19:24:53 +0000137 @throw [TTransportException exceptionWithName: @"TTransportException"
138 reason: @"Could not make HTTP request"
Mark Slee2ac60ed2007-09-19 21:10:18 +0000139 error: error];
140 }
141 if (![response isKindOfClass: [NSHTTPURLResponse class]]) {
Mark Slee77575e62007-09-24 19:24:53 +0000142 @throw [TTransportException exceptionWithName: @"TTransportException"
Andrew McGeachie73380092009-10-27 20:27:49 +0000143 reason: [NSString stringWithFormat: @"Unexpected NSURLResponse type: %@",
Andrew McGeachie7ceba942010-05-19 15:00:26 +0000144 NSStringFromClass([response class])]];
Mark Slee2ac60ed2007-09-19 21:10:18 +0000145 }
146
147 NSHTTPURLResponse * httpResponse = (NSHTTPURLResponse *) response;
148 if ([httpResponse statusCode] != 200) {
Mark Slee77575e62007-09-24 19:24:53 +0000149 @throw [TTransportException exceptionWithName: @"TTransportException"
David Reiss0c90f6f2008-02-06 22:18:40 +0000150 reason: [NSString stringWithFormat: @"Bad response from HTTP server: %d",
Mark Slee77575e62007-09-24 19:24:53 +0000151 [httpResponse statusCode]]];
Mark Slee2ac60ed2007-09-19 21:10:18 +0000152 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000153
Mark Slee2ac60ed2007-09-19 21:10:18 +0000154 // phew!
Jake Farrell9689d892011-12-06 01:07:17 +0000155 [mResponseData release_stub];
156 mResponseData = [responseData retain_stub];
Mark Slee2ac60ed2007-09-19 21:10:18 +0000157 mResponseDataOffset = 0;
158}
159
160
161@end