API  0.9.8
 All Classes Files Functions Variables Typedefs Macros Groups Pages
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 
31 @implementation CPURLRequest : CPObject
32 {
33  CPURL _URL;
34 
35  // FIXME: this should be CPData
36  CPString _HTTPBody;
37  CPString _HTTPMethod;
38  BOOL _withCredentials;
39 
40  CPDictionary _HTTPHeaderFields;
41 }
42 
48 + (id)requestWithURL:(CPURL)aURL
49 {
50  return [[CPURLRequest alloc] initWithURL:aURL];
51 }
52 
56 - (id)init
57 {
58  return [self initWithURL:nil];
59 }
60 
67 - (id)initWithURL:(CPURL)aURL
68 {
69  self = [super init];
70 
71  if (self)
72  {
73  [self setURL:aURL];
74 
75  _HTTPBody = @"";
76  _HTTPMethod = @"GET";
77  _HTTPHeaderFields = @{};
78  _withCredentials = NO;
79 
80  [self setValue:"Thu, 01 Jan 1970 00:00:00 GMT" forHTTPHeaderField:"If-Modified-Since"];
81  [self setValue:"no-cache" forHTTPHeaderField:"Cache-Control"];
82  [self setValue:"XMLHttpRequest" forHTTPHeaderField:"X-Requested-With"];
83  }
84 
85  return self;
86 }
87 
92 - (void)setURL:(CPURL)aURL
93 {
94  // Lenient and accept strings.
95  _URL = new CFURL(aURL);
96 }
97 
102 - (CPString)valueForHTTPHeaderField:(CPString)aField
103 {
104  return [_HTTPHeaderFields objectForKey:aField];
105 }
106 
112 - (void)setValue:(CPString)aValue forHTTPHeaderField:(CPString)aField
113 {
114  [_HTTPHeaderFields setObject:aValue forKey:aField];
115 }
116 
117 @end
118 
119 /*
120  Implements the CPCopying Protocol for a CPURLRequest to provide deep copying for CPURLRequests
121 */
123 {
124 }
125 
126 - (id)copy
127 {
128  var request = [[CPURLRequest alloc] initWithURL:[self URL]];
129  [request setHTTPBody:[self HTTPBody]];
130  [request setHTTPMethod:[self HTTPMethod]];
131  [request setWithCredentials:[self withCredentials]];
132  request._HTTPHeaderFields = [self allHTTPHeaderFields];
133 
134  return request;
135 }
136 
137 @end
138 
140 
144 - (CPURL)URL
145 {
146  return _URL;
147 }
148 
152 - (void)setURL:(CPURL)aValue
153 {
154  _URL = aValue;
155 }
156 
160 - (CPString)HTTPBody
161 {
162  return _HTTPBody;
163 }
164 
168 - (void)setHTTPBody:(CPString)aValue
169 {
170  _HTTPBody = aValue;
171 }
172 
176 - (CPString)HTTPMethod
177 {
178  return _HTTPMethod;
179 }
180 
184 - (void)setHTTPMethod:(CPString)aValue
185 {
186  _HTTPMethod = aValue;
187 }
188 
192 - (BOOL)withCredentials
193 {
194  return _withCredentials;
195 }
196 
200 - (void)setWithCredentials:(BOOL)aValue
201 {
202  _withCredentials = aValue;
203 }
204 
208 - (CPDictionary)allHTTPHeaderFields
209 {
210  return _HTTPHeaderFields;
211 }
212 
213 @end