API  0.9.9
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 
97  return self;
98 }
99 
106 - (id)initWithURL:(CPURL)aURL
107 {
108  if (self = [super init])
109  {
110  [self setURL:aURL];
111 
112  _HTTPBody = @"";
113  _HTTPMethod = @"GET";
114  _HTTPHeaderFields = @{};
115  _withCredentials = NO;
116  _timeoutInterval = 60.0;
117  _cachePolicy = CPURLRequestUseProtocolCachePolicy;
118 
119  [self setValue:"Thu, 01 Jan 1970 00:00:00 GMT" forHTTPHeaderField:"If-Modified-Since"];
120 
121  switch (_cachePolicy)
122  {
124  // TODO: implement everything about cache...
125  [self setValue:"no-cache" forHTTPHeaderField:"Cache-Control"];
126  break;
127 
129  [self setValue:"max-stale=31536000" forHTTPHeaderField:"Cache-Control"];
130  break;
131 
133  [self setValue:"only-if-cached" forHTTPHeaderField:"Cache-Control"];
134  break;
135 
137  [self setValue:"no-cache" forHTTPHeaderField:"Cache-Control"];
138  break;
139 
140  default:
141  [self setValue:"no-cache" forHTTPHeaderField:"Cache-Control"];
142  }
143 
144  [self setValue:"XMLHttpRequest" forHTTPHeaderField:"X-Requested-With"];
145  }
146 
147  return self;
148 }
149 
154 - (void)setURL:(CPURL)aURL
155 {
156  // Lenient and accept strings.
157  _URL = new CFURL(aURL);
158 }
159 
164 - (CPString)valueForHTTPHeaderField:(CPString)aField
165 {
166  return [_HTTPHeaderFields objectForKey:aField];
167 }
168 
174 - (void)setValue:(CPString)aValue forHTTPHeaderField:(CPString)aField
175 {
176  [_HTTPHeaderFields setObject:aValue forKey:aField];
177 }
178 
179 @end
180 
181 /*
182  Implements the CPCopying Protocol for a CPURLRequest to provide deep copying for CPURLRequests
183 */
185 {
186 }
187 
188 - (id)copy
189 {
190  var request = [[CPURLRequest alloc] initWithURL:[self URL]];
191  [request setHTTPBody:[self HTTPBody]];
192  [request setHTTPMethod:[self HTTPMethod]];
193  [request setWithCredentials:[self withCredentials]];
194  request._HTTPHeaderFields = [self allHTTPHeaderFields];
195 
196  return request;
197 }
198 
199 @end
200 
202 
207 {
208  return _URL;
209 }
210 
214 - (void)setURL:(CPURL)aValue
215 {
216  _URL = aValue;
217 }
218 
223 {
224  return _HTTPBody;
225 }
226 
230 - (void)setHTTPBody:(CPString)aValue
231 {
232  _HTTPBody = aValue;
233 }
234 
239 {
240  return _HTTPMethod;
241 }
242 
246 - (void)setHTTPMethod:(CPString)aValue
247 {
248  _HTTPMethod = aValue;
249 }
250 
255 {
256  return _withCredentials;
257 }
258 
262 - (void)setWithCredentials:(BOOL)aValue
263 {
264  _withCredentials = aValue;
265 }
266 
271 {
272  return _HTTPHeaderFields;
273 }
274 
278 - (CPTimeInterval)timeoutInterval
279 {
280  return _timeoutInterval;
281 }
282 
286 - (CPURLRequestCachePolicy)cachePolicy
287 {
288  return _cachePolicy;
289 }
290 
291 @end
id initWithURL:(CPURL aURL)
Definition: CPURLRequest.j:106
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:238
An immutable string (collection of characters).
Definition: CPString.h:2
CPURLRequestReturnCacheDataElseLoad
Definition: CPURLRequest.j:27
CPURLRequestCachePolicy cachePolicy()
Definition: CPURLRequest.j:286
void setValue:forHTTPHeaderField:(CPString aValue, [forHTTPHeaderField] CPString aField)
Definition: CPURLRequest.j:174
CPURLRequestReturnCacheDataDontLoad
Definition: CPURLRequest.j:28
CPDictionary allHTTPHeaderFields()
Definition: CPURLRequest.j:270
void setURL:(CPURL aURL)
Definition: CPURLRequest.j:154
BOOL withCredentials()
Definition: CPURLRequest.j:254
Contains data obtained during a request made with CPURLConnection.
Definition: CPURLRequest.h:2
CPURLRequestReloadIgnoringLocalCacheData
Definition: CPURLRequest.j:26
CPString HTTPBody()
Definition: CPURLRequest.j:222
Definition: CPURL.h:2
id alloc()
Definition: CPObject.j:130