API  0.9.10
CPURLRequest.j
Go to the documentation of this file.
1 /*
2  * CPURLRequest.j
3  * Foundation
4  *
5  * Created by Francisco Tolmasky.
6  * Copyright 2008, 280 North, Inc.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 
24 @typedef CPURLRequestCachePolicy
29 
37 @implementation CPURLRequest : CPObject
38 {
39  CPURL _URL;
40 
41  // FIXME: this should be CPData
42  CPString _HTTPBody;
43  CPString _HTTPMethod;
44  BOOL _withCredentials;
45 
46  CPDictionary _HTTPHeaderFields;
47  CPTimeInterval _timeoutInterval;
48  CPURLRequestCachePolicy _cachePolicy;
49 }
50 
56 + (id)requestWithURL:(CPURL)aURL
57 {
58  return [[CPURLRequest alloc] initWithURL:aURL];
59 }
60 
68 + (id)requestWithURL:(CPURL)anURL cachePolicy:(CPURLRequestCachePolicy)aCachePolicy timeoutInterval:(CPTimeInterval)aTimeoutInterval
69 {
70  return [[CPURLRequest alloc] initWithURL:anURL cachePolicy:aCachePolicy timeoutInterval:aTimeoutInterval];
71 }
72 
76 - (id)init
77 {
78  return [self initWithURL:nil];
79 }
80 
89 - (id)initWithURL:(CPURL)anURL cachePolicy:(CPURLRequestCachePolicy)aCachePolicy timeoutInterval:(CPTimeInterval)aTimeoutInterval
90 {
91  if (self = [self initWithURL:anURL])
92  {
93  _cachePolicy = aCachePolicy;
94  _timeoutInterval = aTimeoutInterval;
95 
96  [self _updateCacheControlHeader];
97  }
98 
99  return self;
100 }
101 
108 - (id)initWithURL:(CPURL)aURL
109 {
110  if (self = [super init])
111  {
112  [self setURL:aURL];
113 
114  _HTTPBody = @"";
115  _HTTPMethod = @"GET";
116  _HTTPHeaderFields = @{};
117  _withCredentials = NO;
118  _timeoutInterval = 60.0;
119  _cachePolicy = CPURLRequestUseProtocolCachePolicy;
120 
121  [self setValue:"Thu, 01 Jan 1970 00:00:00 GMT" forHTTPHeaderField:"If-Modified-Since"];
122  [self setValue:"XMLHttpRequest" forHTTPHeaderField:"X-Requested-With"];
123  [self _updateCacheControlHeader];
124  }
125 
126  return self;
127 }
128 
133 - (void)setURL:(CPURL)aURL
134 {
135  // Lenient and accept strings.
136  _URL = new CFURL(aURL);
137 }
138 
143 - (CPString)valueForHTTPHeaderField:(CPString)aField
144 {
145  return [_HTTPHeaderFields objectForKey:aField];
146 }
147 
153 - (void)setValue:(CPString)aValue forHTTPHeaderField:(CPString)aField
154 {
155  [_HTTPHeaderFields setObject:aValue forKey:aField];
156 }
157 
158 /*
159  @ignore
160 */
161 - (void)_updateCacheControlHeader
162 {
163  switch (_cachePolicy)
164  {
166  // TODO: implement everything about cache...
167  [self setValue:"no-cache" forHTTPHeaderField:"Cache-Control"];
168  break;
169 
171  [self setValue:"max-stale=31536000" forHTTPHeaderField:"Cache-Control"];
172  break;
173 
175  [self setValue:"only-if-cached" forHTTPHeaderField:"Cache-Control"];
176  break;
177 
179  [self setValue:"no-cache" forHTTPHeaderField:"Cache-Control"];
180  break;
181 
182  default:
183  [self setValue:"no-cache" forHTTPHeaderField:"Cache-Control"];
184  }
185 }
186 
187 @end
188 
189 /*
190  Implements the CPCopying Protocol for a CPURLRequest to provide deep copying for CPURLRequests
191 */
193 {
194 }
195 
196 - (id)copy
197 {
198  var request = [[CPURLRequest alloc] initWithURL:[self URL]];
199  [request setHTTPBody:[self HTTPBody]];
200  [request setHTTPMethod:[self HTTPMethod]];
201  [request setWithCredentials:[self withCredentials]];
202  request._HTTPHeaderFields = [self allHTTPHeaderFields];
203 
204  return request;
205 }
206 
207 @end
208 
210 
215 {
216  return _URL;
217 }
218 
222 - (void)setURL:(CPURL)aValue
223 {
224  _URL = aValue;
225 }
226 
231 {
232  return _HTTPBody;
233 }
234 
238 - (void)setHTTPBody:(CPString)aValue
239 {
240  _HTTPBody = aValue;
241 }
242 
247 {
248  return _HTTPMethod;
249 }
250 
254 - (void)setHTTPMethod:(CPString)aValue
255 {
256  _HTTPMethod = aValue;
257 }
258 
263 {
264  return _withCredentials;
265 }
266 
270 - (void)setWithCredentials:(BOOL)aValue
271 {
272  _withCredentials = aValue;
273 }
274 
279 {
280  return _HTTPHeaderFields;
281 }
282 
286 - (CPTimeInterval)timeoutInterval
287 {
288  return _timeoutInterval;
289 }
290 
294 - (CPURLRequestCachePolicy)cachePolicy
295 {
296  return _cachePolicy;
297 }
298 
299 @end
id initWithURL:(CPURL aURL)
Definition: CPURLRequest.j:108
CPURLRequestCachePolicy CPURLRequestUseProtocolCachePolicy
Definition: CPURLRequest.j:25
A mutable key-value pair collection.
Definition: CPDictionary.h:2
id initWithURL:cachePolicy:timeoutInterval:(CPURL anURL, [cachePolicy] CPURLRequestCachePolicy aCachePolicy, [timeoutInterval] CPTimeInterval aTimeoutInterval)
Definition: CPURLRequest.j:89
CPString HTTPMethod()
Definition: CPURLRequest.j:246
An immutable string (collection of characters).
Definition: CPString.h:2
CPURLRequestReturnCacheDataElseLoad
Definition: CPURLRequest.j:27
CPURLRequestCachePolicy cachePolicy()
Definition: CPURLRequest.j:294
void setValue:forHTTPHeaderField:(CPString aValue, [forHTTPHeaderField] CPString aField)
Definition: CPURLRequest.j:153
CPURLRequestReturnCacheDataDontLoad
Definition: CPURLRequest.j:28
CPDictionary allHTTPHeaderFields()
Definition: CPURLRequest.j:278
void setURL:(CPURL aURL)
Definition: CPURLRequest.j:133
BOOL withCredentials()
Definition: CPURLRequest.j:262
Contains data obtained during a request made with CPURLConnection.
Definition: CPURLRequest.h:2
CPURLRequestReloadIgnoringLocalCacheData
Definition: CPURLRequest.j:26
CPString HTTPBody()
Definition: CPURLRequest.j:230
Definition: CPURL.h:2
id alloc()
Definition: CPObject.j:130