David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 1 | /* |
| 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 Slee | 2ac60ed | 2007-09-19 21:10:18 +0000 | [diff] [blame] | 20 | #import "THTTPClient.h" |
| 21 | #import "TTransportException.h" |
Jake Farrell | 9689d89 | 2011-12-06 01:07:17 +0000 | [diff] [blame] | 22 | #import "TObjective-C.h" |
Mark Slee | 2ac60ed | 2007-09-19 21:10:18 +0000 | [diff] [blame] | 23 | |
| 24 | @implementation THTTPClient |
| 25 | |
Mark Slee | 2ac60ed | 2007-09-19 21:10:18 +0000 | [diff] [blame] | 26 | |
Mark Slee | 8440605 | 2007-11-20 01:39:25 +0000 | [diff] [blame] | 27 | - (void) setupRequest |
| 28 | { |
| 29 | if (mRequest != nil) { |
Jake Farrell | 9689d89 | 2011-12-06 01:07:17 +0000 | [diff] [blame] | 30 | [mRequest release_stub]; |
Mark Slee | 8440605 | 2007-11-20 01:39:25 +0000 | [diff] [blame] | 31 | } |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 32 | |
Mark Slee | 2ac60ed | 2007-09-19 21:10:18 +0000 | [diff] [blame] | 33 | // 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 Reiss | b0232b3 | 2008-01-23 20:35:39 +0000 | [diff] [blame] | 38 | |
| 39 | NSString * userAgent = mUserAgent; |
| 40 | if (!userAgent) { |
| 41 | userAgent = @"Cocoa/THTTPClient"; |
| 42 | } |
| 43 | [mRequest setValue: userAgent forHTTPHeaderField: @"User-Agent"]; |
| 44 | |
Mark Slee | 2ac60ed | 2007-09-19 21:10:18 +0000 | [diff] [blame] | 45 | [mRequest setCachePolicy: NSURLRequestReloadIgnoringCacheData]; |
David Reiss | b0232b3 | 2008-01-23 20:35:39 +0000 | [diff] [blame] | 46 | if (mTimeout) { |
| 47 | [mRequest setTimeoutInterval: mTimeout]; |
| 48 | } |
Mark Slee | 8440605 | 2007-11-20 01:39:25 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | |
| 52 | - (id) initWithURL: (NSURL *) aURL |
| 53 | { |
David Reiss | b0232b3 | 2008-01-23 20:35:39 +0000 | [diff] [blame] | 54 | return [self initWithURL: aURL |
| 55 | userAgent: nil |
| 56 | timeout: 0]; |
| 57 | } |
| 58 | |
| 59 | |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 60 | - (id) initWithURL: (NSURL *) aURL |
David Reiss | b0232b3 | 2008-01-23 20:35:39 +0000 | [diff] [blame] | 61 | userAgent: (NSString *) userAgent |
| 62 | timeout: (int) timeout |
| 63 | { |
Mark Slee | 8440605 | 2007-11-20 01:39:25 +0000 | [diff] [blame] | 64 | self = [super init]; |
David Reiss | b0232b3 | 2008-01-23 20:35:39 +0000 | [diff] [blame] | 65 | if (!self) { |
| 66 | return nil; |
| 67 | } |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 68 | |
David Reiss | b0232b3 | 2008-01-23 20:35:39 +0000 | [diff] [blame] | 69 | mTimeout = timeout; |
| 70 | if (userAgent) { |
Jake Farrell | 9689d89 | 2011-12-06 01:07:17 +0000 | [diff] [blame] | 71 | mUserAgent = [userAgent retain_stub]; |
David Reiss | b0232b3 | 2008-01-23 20:35:39 +0000 | [diff] [blame] | 72 | } |
Jake Farrell | 9689d89 | 2011-12-06 01:07:17 +0000 | [diff] [blame] | 73 | mURL = [aURL retain_stub]; |
Mark Slee | 8440605 | 2007-11-20 01:39:25 +0000 | [diff] [blame] | 74 | |
| 75 | [self setupRequest]; |
Mark Slee | 2ac60ed | 2007-09-19 21:10:18 +0000 | [diff] [blame] | 76 | |
| 77 | // create our request data buffer |
| 78 | mRequestData = [[NSMutableData alloc] initWithCapacity: 1024]; |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 79 | |
Mark Slee | 2ac60ed | 2007-09-19 21:10:18 +0000 | [diff] [blame] | 80 | return self; |
| 81 | } |
| 82 | |
| 83 | |
Mark Slee | 8440605 | 2007-11-20 01:39:25 +0000 | [diff] [blame] | 84 | - (void) setURL: (NSURL *) aURL |
| 85 | { |
Jake Farrell | 9689d89 | 2011-12-06 01:07:17 +0000 | [diff] [blame] | 86 | [aURL retain_stub]; |
| 87 | [mURL release_stub]; |
Mark Slee | 8440605 | 2007-11-20 01:39:25 +0000 | [diff] [blame] | 88 | mURL = aURL; |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 89 | |
Mark Slee | 8440605 | 2007-11-20 01:39:25 +0000 | [diff] [blame] | 90 | [self setupRequest]; |
| 91 | } |
| 92 | |
| 93 | |
Mark Slee | 2ac60ed | 2007-09-19 21:10:18 +0000 | [diff] [blame] | 94 | - (void) dealloc |
| 95 | { |
Jake Farrell | 9689d89 | 2011-12-06 01:07:17 +0000 | [diff] [blame] | 96 | [mURL release_stub]; |
| 97 | [mUserAgent release_stub]; |
| 98 | [mRequest release_stub]; |
| 99 | [mRequestData release_stub]; |
| 100 | [mResponseData release_stub]; |
| 101 | [super dealloc_stub]; |
Mark Slee | 2ac60ed | 2007-09-19 21:10:18 +0000 | [diff] [blame] | 102 | } |
| 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 Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 131 | NSData * responseData = |
Mark Slee | 2ac60ed | 2007-09-19 21:10:18 +0000 | [diff] [blame] | 132 | [NSURLConnection sendSynchronousRequest: mRequest returningResponse: &response error: &error]; |
| 133 | |
| 134 | [mRequestData setLength: 0]; |
| 135 | |
| 136 | if (responseData == nil) { |
Mark Slee | 77575e6 | 2007-09-24 19:24:53 +0000 | [diff] [blame] | 137 | @throw [TTransportException exceptionWithName: @"TTransportException" |
| 138 | reason: @"Could not make HTTP request" |
Mark Slee | 2ac60ed | 2007-09-19 21:10:18 +0000 | [diff] [blame] | 139 | error: error]; |
| 140 | } |
| 141 | if (![response isKindOfClass: [NSHTTPURLResponse class]]) { |
Mark Slee | 77575e6 | 2007-09-24 19:24:53 +0000 | [diff] [blame] | 142 | @throw [TTransportException exceptionWithName: @"TTransportException" |
Andrew McGeachie | 7338009 | 2009-10-27 20:27:49 +0000 | [diff] [blame] | 143 | reason: [NSString stringWithFormat: @"Unexpected NSURLResponse type: %@", |
Andrew McGeachie | 7ceba94 | 2010-05-19 15:00:26 +0000 | [diff] [blame] | 144 | NSStringFromClass([response class])]]; |
Mark Slee | 2ac60ed | 2007-09-19 21:10:18 +0000 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | NSHTTPURLResponse * httpResponse = (NSHTTPURLResponse *) response; |
| 148 | if ([httpResponse statusCode] != 200) { |
Mark Slee | 77575e6 | 2007-09-24 19:24:53 +0000 | [diff] [blame] | 149 | @throw [TTransportException exceptionWithName: @"TTransportException" |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 150 | reason: [NSString stringWithFormat: @"Bad response from HTTP server: %d", |
Mark Slee | 77575e6 | 2007-09-24 19:24:53 +0000 | [diff] [blame] | 151 | [httpResponse statusCode]]]; |
Mark Slee | 2ac60ed | 2007-09-19 21:10:18 +0000 | [diff] [blame] | 152 | } |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 153 | |
Mark Slee | 2ac60ed | 2007-09-19 21:10:18 +0000 | [diff] [blame] | 154 | // phew! |
Jake Farrell | 9689d89 | 2011-12-06 01:07:17 +0000 | [diff] [blame] | 155 | [mResponseData release_stub]; |
| 156 | mResponseData = [responseData retain_stub]; |
Mark Slee | 2ac60ed | 2007-09-19 21:10:18 +0000 | [diff] [blame] | 157 | mResponseDataOffset = 0; |
| 158 | } |
| 159 | |
| 160 | |
| 161 | @end |