ObjFW  Diff

Differences From Artifact [c73164e676]:

To Artifact [44128a59e9]:


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
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







-
+


-
+

-
+

-
+

-
+

-
+

-
+

-
+

-
+






-
-
+
+


-
+

-
+

-
+

-
+

-
+

-
+

-
+

-
+








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

const char *
of_http_request_method_to_string(of_http_request_method_t method)
OFHTTPRequestMethodName(OFHTTPRequestMethod method)
{
	switch (method) {
	case OF_HTTP_REQUEST_METHOD_OPTIONS:
	case OFHTTPRequestMethodOptions:
		return "OPTIONS";
	case OF_HTTP_REQUEST_METHOD_GET:
	case OFHTTPRequestMethodGet:
		return "GET";
	case OF_HTTP_REQUEST_METHOD_HEAD:
	case OFHTTPRequestMethodHead:
		return "HEAD";
	case OF_HTTP_REQUEST_METHOD_POST:
	case OFHTTPRequestMethodPost:
		return "POST";
	case OF_HTTP_REQUEST_METHOD_PUT:
	case OFHTTPRequestMethodPut:
		return "PUT";
	case OF_HTTP_REQUEST_METHOD_DELETE:
	case OFHTTPRequestMethodDelete:
		return "DELETE";
	case OF_HTTP_REQUEST_METHOD_TRACE:
	case OFHTTPRequestMethodTrace:
		return "TRACE";
	case OF_HTTP_REQUEST_METHOD_CONNECT:
	case OFHTTPRequestMethodConnect:
		return "CONNECT";
	}

	return NULL;
}

of_http_request_method_t
of_http_request_method_from_string(OFString *string)
OFHTTPRequestMethod
OFHTTPRequestMethodParseName(OFString *string)
{
	if ([string isEqual: @"OPTIONS"])
		return OF_HTTP_REQUEST_METHOD_OPTIONS;
		return OFHTTPRequestMethodOptions;
	if ([string isEqual: @"GET"])
		return OF_HTTP_REQUEST_METHOD_GET;
		return OFHTTPRequestMethodGet;
	if ([string isEqual: @"HEAD"])
		return OF_HTTP_REQUEST_METHOD_HEAD;
		return OFHTTPRequestMethodHead;
	if ([string isEqual: @"POST"])
		return OF_HTTP_REQUEST_METHOD_POST;
		return OFHTTPRequestMethodPost;
	if ([string isEqual: @"PUT"])
		return OF_HTTP_REQUEST_METHOD_PUT;
		return OFHTTPRequestMethodPut;
	if ([string isEqual: @"DELETE"])
		return OF_HTTP_REQUEST_METHOD_DELETE;
		return OFHTTPRequestMethodDelete;
	if ([string isEqual: @"TRACE"])
		return OF_HTTP_REQUEST_METHOD_TRACE;
		return OFHTTPRequestMethodTrace;
	if ([string isEqual: @"CONNECT"])
		return OF_HTTP_REQUEST_METHOD_CONNECT;
		return OFHTTPRequestMethodConnect;

	@throw [OFInvalidArgumentException exception];
}

@implementation OFHTTPRequest
@synthesize URL = _URL, method = _method, headers = _headers;

90
91
92
93
94
95
96
97

98
99
100
101
102
103
104
90
91
92
93
94
95
96

97
98
99
100
101
102
103
104







-
+







	return [[[self alloc] initWithURL: URL] autorelease];
}

- (instancetype)init
{
	self = [super init];

	_method = OF_HTTP_REQUEST_METHOD_GET;
	_method = OFHTTPRequestMethodGet;
	_protocolVersion.major = 1;
	_protocolVersion.minor = 1;

	return self;
}

- (instancetype)initWithURL: (OFURL *)URL
119
120
121
122
123
124
125
126

127
128
129
130
131
132
133
134

135
136
137
138
139
140
141
119
120
121
122
123
124
125

126
127
128
129
130
131
132
133

134
135
136
137
138
139
140
141







-
+







-
+







{
	[_URL release];
	[_headers release];

	[super dealloc];
}

- (void)setRemoteAddress: (const of_socket_address_t *)remoteAddress
- (void)setRemoteAddress: (const OFSocketAddress *)remoteAddress
{
	_hasRemoteAddress = (remoteAddress != NULL);

	if (_hasRemoteAddress)
		_remoteAddress = *remoteAddress;
}

- (const of_socket_address_t *)remoteAddress
- (const OFSocketAddress *)remoteAddress
{
	if (_hasRemoteAddress)
		return &_remoteAddress;

	return NULL;
}

173
174
175
176
177
178
179
180

181
182
183
184
185
186
187
188

189
190

191
192
193
194
195
196





197
198

199
200

201
202
203
204
205

206
207
208
209
210
211
212
213
214
215
216

217
218
219
220
221
222
223
224
225
226

227
228
229
230
231
232
233
173
174
175
176
177
178
179

180
181
182
183
184
185
186
187

188
189

190
191





192
193
194
195
196
197

198
199

200
201
202
203
204

205
206
207
208
209
210
211
212
213
214
215

216
217
218
219
220
221
222
223
224
225

226
227
228
229
230
231
232
233







-
+







-
+

-
+

-
-
-
-
-
+
+
+
+
+

-
+

-
+




-
+










-
+









-
+







	    request->_protocolVersion.major != _protocolVersion.major ||
	    request->_protocolVersion.minor != _protocolVersion.minor ||
	    ![request->_URL isEqual: _URL] ||
	    ![request->_headers isEqual: _headers])
		return false;

	if (request.remoteAddress != self.remoteAddress &&
	    !of_socket_address_equal(request.remoteAddress, self.remoteAddress))
	    !OFSocketAddressEqual(request.remoteAddress, self.remoteAddress))
		return false;

	return true;
}

- (unsigned long)hash
{
	uint32_t hash;
	unsigned long hash;

	OF_HASH_INIT(hash);
	OFHashInit(&hash);

	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);
	OFHashAdd(&hash, _method);
	OFHashAdd(&hash, _protocolVersion.major);
	OFHashAdd(&hash, _protocolVersion.minor);
	OFHashAddHash(&hash, _URL.hash);
	OFHashAddHash(&hash, _headers.hash);
	if (_hasRemoteAddress)
		OF_HASH_ADD_HASH(hash, of_socket_address_hash(&_remoteAddress));
		OFHashAddHash(&hash, OFSocketAddressHash(&_remoteAddress));

	OF_HASH_FINALIZE(hash);
	OFHashFinalize(&hash);

	return hash;
}

- (void)setProtocolVersion: (of_http_request_protocol_version_t)protocolVersion
- (void)setProtocolVersion: (OFHTTPRequestProtocolVersion)protocolVersion
{
	if (protocolVersion.major != 1 || protocolVersion.minor > 1)
		@throw [OFUnsupportedVersionException exceptionWithVersion:
		    [OFString stringWithFormat: @"%hhu.%hhu",
						protocolVersion.major,
						protocolVersion.minor]];

	_protocolVersion = protocolVersion;
}

- (of_http_request_protocol_version_t)protocolVersion
- (OFHTTPRequestProtocolVersion)protocolVersion
{
	return _protocolVersion;
}

- (void)setProtocolVersionString: (OFString *)string
{
	void *pool = objc_autoreleasePoolPush();
	OFArray *components = [string componentsSeparatedByString: @"."];
	unsigned long long major, minor;
	of_http_request_protocol_version_t protocolVersion;
	OFHTTPRequestProtocolVersion protocolVersion;

	if (components.count != 2)
		@throw [OFInvalidFormatException exception];

	major = [components.firstObject unsignedLongLongValue];
	minor = [components.lastObject unsignedLongLongValue];

248
249
250
251
252
253
254
255

256
257
258
259
260
261
262
263

264
265
266
267
268
269
270
271
248
249
250
251
252
253
254

255
256
257
258
259
260
261
262

263

264
265
266
267
268
269
270







-
+







-
+
-







					   _protocolVersion.major,
					   _protocolVersion.minor];
}

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

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

	if (_hasRemoteAddress)
		remoteAddress =
		remoteAddress = OFSocketAddressString(&_remoteAddress);
		    of_socket_address_ip_string(&_remoteAddress, NULL);
	else
		remoteAddress = nil;

	ret = [[OFString alloc] initWithFormat:
	    @"<%@:\n\tURL = %@\n"
	    @"\tMethod = %s\n"
	    @"\tHeaders = %@\n"