ObjFW  Diff

Differences From Artifact [5824c67d6a]:

To Artifact [e321453562]:


11
12
13
14
15
16
17


18
19
20
21
22
23
24
25
26
27
28
29
30
31
















































32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48

49
50
51
52
53
54
55
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97

98
99
100
101
102
103
104
105







+
+














+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
















-
+







 * Alternatively, it may be distributed under the terms of the GNU General
 * Public License, either version 2 or 3, which can be found in the file
 * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
 * file.
 */

#include "config.h"

#include <string.h>

#import "OFHTTPRequest.h"
#import "OFString.h"
#import "OFURL.h"
#import "OFDictionary.h"
#import "OFDataArray.h"
#import "OFArray.h"

#import "autorelease.h"
#import "macros.h"

#import "OFInvalidFormatException.h"
#import "OFOutOfRangeException.h"
#import "OFUnsupportedVersionException.h"

const char*
of_http_request_method_to_string(of_http_request_method_t method)
{
	switch (method) {
	case OF_HTTP_REQUEST_METHOD_OPTIONS:
		return "OPTIONS";
	case OF_HTTP_REQUEST_METHOD_GET:
		return "GET";
	case OF_HTTP_REQUEST_METHOD_HEAD:
		return "HEAD";
	case OF_HTTP_REQUEST_METHOD_POST:
		return "POST";
	case OF_HTTP_REQUEST_METHOD_PUT:
		return "PUT";
	case OF_HTTP_REQUEST_METHOD_DELETE:
		return "DELETE";
	case OF_HTTP_REQUEST_METHOD_TRACE:
		return "TRACE";
	case OF_HTTP_REQUEST_METHOD_CONNECT:
		return "CONNECT";
	}

	return NULL;
}

of_http_request_method_t
of_http_request_method_from_string(const char *string)
{
	if (!strcmp(string, "OPTIONS"))
		return OF_HTTP_REQUEST_METHOD_OPTIONS;
	if (!strcmp(string, "GET"))
		return OF_HTTP_REQUEST_METHOD_GET;
	if (!strcmp(string, "HEAD"))
		return OF_HTTP_REQUEST_METHOD_HEAD;
	if (!strcmp(string, "POST"))
		return OF_HTTP_REQUEST_METHOD_POST;
	if (!strcmp(string, "PUT"))
		return OF_HTTP_REQUEST_METHOD_PUT;
	if (!strcmp(string, "DELETE"))
		return OF_HTTP_REQUEST_METHOD_DELETE;
	if (!strcmp(string, "TRACE"))
		return OF_HTTP_REQUEST_METHOD_TRACE;
	if (!strcmp(string, "CONNECT"))
		return OF_HTTP_REQUEST_METHOD_CONNECT;

	@throw [OFInvalidFormatException exception];
}

@implementation OFHTTPRequest
+ (instancetype)request
{
	return [[[self alloc] init] autorelease];
}

+ (instancetype)requestWithURL: (OFURL*)URL
{
	return [[[self alloc] initWithURL: URL] autorelease];
}

- init
{
	self = [super init];

	_requestType = OF_HTTP_REQUEST_TYPE_GET;
	_method = OF_HTTP_REQUEST_METHOD_GET;
	_protocolVersion.major = 1;
	_protocolVersion.minor = 1;

	return self;
}

- initWithURL: (OFURL*)URL
78
79
80
81
82
83
84
85

86
87
88
89
90
91
92
128
129
130
131
132
133
134

135
136
137
138
139
140
141
142







-
+







}

- copy
{
	OFHTTPRequest *copy = [[OFHTTPRequest alloc] init];

	@try {
		copy->_requestType = _requestType;
		copy->_method = _method;
		copy->_protocolVersion = _protocolVersion;
		[copy setURL: _URL];
		[copy setHeaders: _headers];
		[copy setPOSTData: _POSTData];
		[copy setMIMEType: _MIMEType];
		[copy setRemoteAddress: _remoteAddress];
	} @catch (id e) {
102
103
104
105
106
107
108
109

110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128

129
130
131
132
133
134
135
152
153
154
155
156
157
158

159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177

178
179
180
181
182
183
184
185







-
+


















-
+







	OFHTTPRequest *request;

	if (![object isKindOfClass: [OFHTTPRequest class]])
		return false;

	request = object;

	if (request->_requestType != _requestType ||
	if (request->_method != _method ||
	    request->_protocolVersion.major != _protocolVersion.major ||
	    request->_protocolVersion.minor != _protocolVersion.minor ||
	    ![request->_URL isEqual: _URL] ||
	    ![request->_headers isEqual: _headers] ||
	    ![request->_POSTData isEqual: _POSTData] ||
	    ![request->_MIMEType isEqual: _MIMEType] ||
	    ![request->_remoteAddress isEqual: _remoteAddress])
		return false;

	return true;
}

- (uint32_t)hash
{
	uint32_t hash;

	OF_HASH_INIT(hash);

	OF_HASH_ADD(hash, _requestType);
	OF_HASH_ADD(hash, _method);
	OF_HASH_ADD(hash, _protocolVersion.major);
	OF_HASH_ADD(hash, _protocolVersion.minor);
	OF_HASH_ADD_HASH(hash, [_URL hash]);
	OF_HASH_ADD_HASH(hash, [_headers hash]);
	OF_HASH_ADD_HASH(hash, [_POSTData hash]);
	OF_HASH_ADD_HASH(hash, [_MIMEType hash]);
	OF_HASH_ADD_HASH(hash, [_remoteAddress hash]);
145
146
147
148
149
150
151
152

153
154

155
156
157

158
159

160
161
162
163
164
165
166
195
196
197
198
199
200
201

202
203

204
205
206

207
208

209
210
211
212
213
214
215
216







-
+

-
+


-
+

-
+







}

- (OFURL*)URL
{
	OF_GETTER(_URL, true)
}

- (void)setRequestType: (of_http_request_type_t)requestType
- (void)setMethod: (of_http_request_method_t)method
{
	_requestType = requestType;
	_method = method;
}

- (of_http_request_type_t)requestType
- (of_http_request_method_t)method
{
	return _requestType;
	return _method;
}

- (void)setProtocolVersion: (of_http_request_protocol_version_t)protocolVersion
{
	if (protocolVersion.major != 1 || protocolVersion.minor > 1)
		@throw [OFUnsupportedVersionException
		    exceptionWithVersion: [OFString stringWithFormat: @"%u.%u",
244
245
246
247
248
249
250
251

252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275

276
277
278
279
280
281
282


283
284
285
286
287
288
294
295
296
297
298
299
300

301
302
303












304
305
306
307
308
309
310
311
312

313
314
315
316
317
318


319
320
321
322
323
324
325
326







-
+


-
-
-
-
-
-
-
-
-
-
-
-









-
+





-
-
+
+






{
	OF_GETTER(_remoteAddress, true)
}

- (OFString*)description
{
	void *pool = objc_autoreleasePoolPush();
	const char *requestTypeStr = NULL;
	const char *method = of_http_request_method_to_string(_method);
	OFString *indentedHeaders, *indentedPOSTData, *ret;

	switch (_requestType) {
	case OF_HTTP_REQUEST_TYPE_GET:
		requestTypeStr = "GET";
		break;
	case OF_HTTP_REQUEST_TYPE_POST:
		requestTypeStr = "POST";
		break;
	case OF_HTTP_REQUEST_TYPE_HEAD:
		requestTypeStr = "HEAD";
		break;
	}

	indentedHeaders = [[_headers description]
	    stringByReplacingOccurrencesOfString: @"\n"
				      withString: @"\n\t"];
	indentedPOSTData = [[_POSTData description]
	    stringByReplacingOccurrencesOfString: @"\n"
				      withString: @"\n\t"];

	ret = [[OFString alloc] initWithFormat:
	    @"<%@:\n\tURL = %@\n"
	    @"\tRequest type = %s\n"
	    @"\tMethod = %s\n"
	    @"\tHeaders = %@\n"
	    @"\tPOST data = %@\n"
	    @"\tPOST data MIME type = %@\n"
	    @"\tRemote address = %@\n"
	    @">",
	    [self class], _URL, requestTypeStr, indentedHeaders,
	    indentedPOSTData, _MIMEType, _remoteAddress];
	    [self class], _URL, method, indentedHeaders, indentedPOSTData,
	    _MIMEType, _remoteAddress];

	objc_autoreleasePoolPop(pool);

	return [ret autorelease];
}
@end