ObjFW  Check-in [c8877fd278]

Overview
Comment:OFHTTPRequest: Rename entity to body
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: c8877fd278e34062394ea3fd8ffd87d8bd64b5c333bc6675200afd3b743fe533
User & Date: js on 2015-05-23 11:21:43
Other Links: manifest | tags
Context
2015-05-24
10:58
utils/ofhttp: Add verbose mode check-in: 33760dee1a user: js tags: trunk
2015-05-23
11:21
OFHTTPRequest: Rename entity to body check-in: c8877fd278 user: js tags: trunk
2015-05-21
21:10
OFHTTPClient: Better keep-alive detection logic check-in: 03f10291e6 user: js tags: trunk
Changes

Modified src/OFHTTPClient.m from [e745036a39] to [d946419431].

327
328
329
330
331
332
333
334

335
336
337
338
339
340
341
327
328
329
330
331
332
333

334
335
336
337
338
339
340
341







-
+







	void *pool = objc_autoreleasePoolPush();
	OFURL *URL = [request URL];
	OFString *scheme = [URL scheme];
	of_http_request_method_t method = [request method];
	OFMutableString *requestString;
	OFString *user, *password;
	OFDictionary *headers = [request headers];
	OFDataArray *entity = [request entity];
	OFDataArray *body = [request body];
	OFTCPSocket *socket;
	OFHTTPClientResponse *response;
	OFString *line, *version, *redirect, *connectionHeader;
	bool keepAlive;
	OFMutableDictionary *serverHeaders;
	OFEnumerator *keyEnumerator, *objectEnumerator;
	OFString *key, *object;
420
421
422
423
424
425
426
427

428
429
430
431

432
433
434
435
436
437
438
420
421
422
423
424
425
426

427
428
429
430

431
432
433
434
435
436
437
438







-
+



-
+







	}

	if ([headers objectForKey: @"User-Agent"] == nil)
		[requestString appendString:
		    @"User-Agent: Something using ObjFW "
		    @"<https://webkeks.org/objfw>\r\n"];

	if (entity != nil) {
	if (body != nil) {
		if ([headers objectForKey: @"Content-Length"] == nil)
			[requestString appendFormat:
			    @"Content-Length: %zd\r\n",
			    [entity itemSize] * [entity count]];
			    [body itemSize] * [body count]];

		if ([headers objectForKey: @"Content-Type"] == nil)
			[requestString appendString:
			    @"Content-Type: application/x-www-form-urlencoded; "
			    @"charset=UTF-8\r\n"];
	}

456
457
458
459
460
461
462
463
464
465



466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485




486
487
488
489
490
491
492
456
457
458
459
460
461
462



463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481




482
483
484
485
486
487
488
489
490
491
492







-
-
-
+
+
+
















-
-
-
-
+
+
+
+







			@throw e;

		/* Reconnect in case a keep-alive connection timed out */
		socket = [self OF_closeAndCreateSocketForRequest: request];
		[socket writeString: requestString];
	}

	if (entity != nil)
		[socket writeBuffer: [entity items]
			     length: [entity count] * [entity itemSize]];
	if (body != nil)
		[socket writeBuffer: [body items]
			     length: [body count] * [body itemSize]];

	@try {
		line = [socket readLine];
	} @catch (OFInvalidEncodingException *e) {
		@throw [OFInvalidServerReplyException exception];
	}

	/*
	 * It's possible that the write succeeds on a connection that is
	 * keep-alive, but the connection has already been closed by the remote
	 * end due to a timeout. In this case, we need to reconnect.
	 */
	if (line == nil) {
		socket = [self OF_closeAndCreateSocketForRequest: request];
		[socket writeString: requestString];

		if (entity != nil)
			[socket writeBuffer: [entity items]
				     length: [entity count] *
					     [entity itemSize]];
		if (body != nil)
			[socket writeBuffer: [body items]
				     length: [body count] *
					     [body itemSize]];

		@try {
			line = [socket readLine];
		} @catch (OFInvalidEncodingException *e) {
			@throw [OFInvalidServerReplyException exception];
		}
	}
630
631
632
633
634
635
636
637

638
639
640
641
642
643
644
630
631
632
633
634
635
636

637
638
639
640
641
642
643
644







-
+








		if (follow) {
			OFHTTPRequest *newRequest;

			newRequest = [OFHTTPRequest requestWithURL: newURL];
			[newRequest setMethod: method];
			[newRequest setHeaders: headers];
			[newRequest setEntity: entity];
			[newRequest setBody: body];

			/*
			 * 303 means the request should be converted to a GET
			 * request before redirection. This also means stripping
			 * the entity of the request.
			 */
			if (status == 303) {
656
657
658
659
660
661
662
663

664
665
666
667
668
669
670
656
657
658
659
660
661
662

663
664
665
666
667
668
669
670







-
+







					if (![key hasPrefix: @"Content-"])
						[newHeaders setObject: object
							       forKey: key];

				[newRequest
				    setMethod: OF_HTTP_REQUEST_METHOD_GET];
				[newRequest setHeaders: newHeaders];
				[newRequest setEntity: nil];
				[newRequest setBody: nil];
			}

			[newRequest retain];
			objc_autoreleasePoolPop(pool);
			[newRequest autorelease];

			return [self performRequest: newRequest

Modified src/OFHTTPRequest.h from [9935b8277c] to [2ca5d029e3].

70
71
72
73
74
75
76
77

78
79
80
81
82
83
84
85
86

87
88
89
90
91
92
93
70
71
72
73
74
75
76

77
78
79
80
81
82
83
84
85

86
87
88
89
90
91
92
93







-
+








-
+







 */
@interface OFHTTPRequest: OFObject <OFCopying>
{
	OFURL *_URL;
	of_http_request_method_t _method;
	of_http_request_protocol_version_t _protocolVersion;
	OFDictionary *_headers;
	OFDataArray *_entity;
	OFDataArray *_body;
	OFString *_remoteAddress;
}

#ifdef OF_HAVE_PROPERTIES
@property (copy) OFURL *URL;
@property of_http_request_method_t method;
@property of_http_request_protocol_version_t protocolVersion;
@property (copy) OFDictionary *headers;
@property (retain) OFDataArray *entity;
@property (retain) OFDataArray *body;
@property (copy) OFString *remoteAddress;
#endif

/*!
 * @brief Creates a new OFHTTPRequest.
 *
 * @return A new, autoreleased OFHTTPRequest
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
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







-
+

-
+







-
+








-
-
+
+






-
+







 * @return A dictionary with headers for the HTTP request.
 */
- (OFDictionary*)headers;

/*!
 * @brief Sets the entity body of the HTTP request.
 *
 * @param entity The entity body of the HTTP request
 * @param body The entity body of the HTTP request
 */
- (void)setEntity: (OFDataArray*)entity;
- (void)setBody: (OFDataArray*)body;

/*!
 * @brief Sets the entity body of the HTTP request to the specified string
 *	  encoded in UTF-8.
 *
 * @param string The string to use for the entity body
 */
- (void)setEntityFromString: (OFString*)string;
- (void)setBodyFromString: (OFString*)string;

/*!
 * @brief Sets the entity body of the HTTP request to the specified string
 *	  encoded in the specified encoding.
 *
 * @param string The string to use for the entity body
 * @param encoding The encoding to encode the string with
 */
- (void)setEntityFromString: (OFString*)string
		   encoding: (of_string_encoding_t)encoding;
- (void)setBodyFromString: (OFString*)string
		 encoding: (of_string_encoding_t)encoding;

/*!
 * @brief Returns the entity body of the HTTP request.
 *
 * @return The entity body of the HTTP request
 */
- (OFDataArray*)entity;
- (OFDataArray*)body;

/*!
 * @brief Sets the remote address from which the request originates.
 *
 * @param remoteAddress The remote address from which the request originates
 */
- (void)setRemoteAddress: (OFString*)remoteAddress;

Modified src/OFHTTPRequest.m from [b4e7571ab2] to [e92c282719].

113
114
115
116
117
118
119
120

121
122
123
124
125
126
127
128
129
130
131
132
133
134
135

136
137
138
139
140
141
142
113
114
115
116
117
118
119

120
121
122
123
124
125
126
127
128
129
130
131
132
133
134

135
136
137
138
139
140
141
142







-
+














-
+







	return self;
}

- (void)dealloc
{
	[_URL release];
	[_headers release];
	[_entity release];
	[_body release];
	[_remoteAddress release];

	[super dealloc];
}

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

	@try {
		copy->_method = _method;
		copy->_protocolVersion = _protocolVersion;
		[copy setURL: _URL];
		[copy setHeaders: _headers];
		[copy setEntity: _entity];
		[copy setBody: _body];
		[copy setRemoteAddress: _remoteAddress];
	} @catch (id e) {
		[copy release];
		@throw e;
	}

	return copy;
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
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







-
+

















-
+







	request = object;

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

	return true;
}

- (uint32_t)hash
{
	uint32_t hash;

	OF_HASH_INIT(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]);
	OF_HASH_ADD_HASH(hash, [_entity hash]);
	OF_HASH_ADD_HASH(hash, [_body hash]);
	OF_HASH_ADD_HASH(hash, [_remoteAddress hash]);

	OF_HASH_FINALIZE(hash);

	return hash;
}

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
289
290
291
292
293
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
327
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
289
290
291
292
293
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
327







-
+

-
+


-
+

-
-
+
+


-
-
+
+


-
+

-
-
-
+
+
+




-
+

-
+
















-
+




-
+







-
+


-
+







}

- (OFDictionary*)headers
{
	OF_GETTER(_headers, true)
}

- (void)setEntity: (OFDataArray*)entity
- (void)setBody: (OFDataArray*)body
{
	OF_SETTER(_entity, entity, true, 0)
	OF_SETTER(_body, body, true, 0)
}

- (void)setEntityFromString: (OFString*)string
- (void)setBodyFromString: (OFString*)string
{
	[self setEntityFromString: string
			 encoding: OF_STRING_ENCODING_UTF_8];
	[self setBodyFromString: string
		       encoding: OF_STRING_ENCODING_UTF_8];
}

- (void)setEntityFromString: (OFString*)string
		   encoding: (of_string_encoding_t)encoding
- (void)setBodyFromString: (OFString*)string
		 encoding: (of_string_encoding_t)encoding
{
	void *pool = objc_autoreleasePoolPush();
	OFDataArray *entity = [OFDataArray dataArray];
	OFDataArray *body = [OFDataArray dataArray];

	[entity addItems: [string cStringWithEncoding: encoding]
		   count: [string cStringLengthWithEncoding: encoding]];
	[self setEntity: entity];
	[body addItems: [string cStringWithEncoding: encoding]
		 count: [string cStringLengthWithEncoding: encoding]];
	[self setBody: body];

	objc_autoreleasePoolPop(pool);
}

- (OFDataArray*)entity
- (OFDataArray*)body
{
	OF_GETTER(_entity, true)
	OF_GETTER(_body, true)
}

- (void)setRemoteAddress: (OFString*)remoteAddress
{
	OF_SETTER(_remoteAddress, remoteAddress, true, 1)
}

- (OFString*)remoteAddress
{
	OF_GETTER(_remoteAddress, true)
}

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

	indentedHeaders = [[_headers description]
	    stringByReplacingOccurrencesOfString: @"\n"
				      withString: @"\n\t"];
	indentedEntity = [[_entity description]
	indentedBody = [[_body description]
	    stringByReplacingOccurrencesOfString: @"\n"
				      withString: @"\n\t"];

	ret = [[OFString alloc] initWithFormat:
	    @"<%@:\n\tURL = %@\n"
	    @"\tMethod = %s\n"
	    @"\tHeaders = %@\n"
	    @"\tEntity = %@\n"
	    @"\tBody = %@\n"
	    @"\tRemote address = %@\n"
	    @">",
	    [self class], _URL, method, indentedHeaders, indentedEntity,
	    [self class], _URL, method, indentedHeaders, indentedBody,
	    _remoteAddress];

	objc_autoreleasePoolPop(pool);

	return [ret autorelease];
}
@end

Modified src/OFHTTPServer.m from [c3a8a62614] to [0ddfcecf31].

299
300
301
302
303
304
305
306

307
308
309
310
311
312
313
299
300
301
302
303
304
305

306
307
308
309
310
311
312
313







-
+







	} _state;
	uint8_t _HTTPMinorVersion;
	of_http_request_method_t _method;
	OFString *_host, *_path;
	uint16_t _port;
	OFMutableDictionary *_headers;
	size_t _contentLength;
	OFDataArray *_entity;
	OFDataArray *_body;
}

- initWithSocket: (OFTCPSocket*)socket
	  server: (OFHTTPServer*)server;
- (bool)socket: (OFTCPSocket*)socket
   didReadLine: (OFString*)line
     exception: (OFException*)exception;
352
353
354
355
356
357
358
359

360
361
362
363
364
365
366
352
353
354
355
356
357
358

359
360
361
362
363
364
365
366







-
+








	[_timer invalidate];
	[_timer release];

	[_host release];
	[_path release];
	[_headers release];
	[_entity release];
	[_body release];

	[super dealloc];
}

- (bool)socket: (OFTCPSocket*)socket
   didReadLine: (OFString*)line
     exception: (OFException*)exception
466
467
468
469
470
471
472
473

474
475
476
477
478
479
480
466
467
468
469
470
471
472

473
474
475
476
477
478
479
480







-
+







			return [self sendErrorAndClose: 400];
		}

		if (contentLength > 0) {
			char *buffer;

			buffer = [self allocMemoryWithSize: BUFFER_SIZE];
			_entity = [[OFDataArray alloc] init];
			_body = [[OFDataArray alloc] init];

			[_socket asyncReadIntoBuffer: buffer
					      length: BUFFER_SIZE
					      target: self
					    selector: @selector(socket:
							  didReadIntoBuffer:
							  length:exception:)];
539
540
541
542
543
544
545
546
547


548
549

550
551
552
553
554
555
556
539
540
541
542
543
544
545


546
547
548

549
550
551
552
553
554
555
556







-
-
+
+

-
+







  didReadIntoBuffer: (char*)buffer
	     length: (size_t)length
	  exception: (OFException*)exception
{
	if ([socket isAtEndOfStream] || exception != nil)
		return false;

	[_entity addItems: buffer
		    count: length];
	[_body addItems: buffer
		  count: length];

	if ([_entity count] >= _contentLength) {
	if ([_body count] >= _contentLength) {
		/*
		 * Manually free the buffer here. While this is not required
		 * now as the async read is the only thing referencing self and
		 * the buffer is allocated on self, it is required once
		 * Connection: keep-alive is implemented.
		 */
		[self freeMemory: buffer];
624
625
626
627
628
629
630
631

632
633
634
635
636
637
638
624
625
626
627
628
629
630

631
632
633
634
635
636
637
638







-
+







		[URL setPath: _path];

	request = [OFHTTPRequest requestWithURL: URL];
	[request setMethod: _method];
	[request setProtocolVersion:
	    (of_http_request_protocol_version_t){ 1, _HTTPMinorVersion }];
	[request setHeaders: _headers];
	[request setEntity: _entity];
	[request setBody: _body];
	[request setRemoteAddress: [_socket remoteAddress]];

	response = [[[OFHTTPServerResponse alloc]
	    initWithSocket: _socket
		    server: _server] autorelease];

	[[_server delegate] server: _server

Modified utils/ofhttp/OFHTTP.m from [d09ed2ac2f] to [84d9105a37].

51
52
53
54
55
56
57
58

59
60
61
62
63
64
65
51
52
53
54
55
56
57

58
59
60
61
62
63
64
65







-
+







@interface OFHTTP: OFObject
{
	OFArray *_URLs;
	size_t _URLIndex;
	int _errorCode;
	OFString *_outputPath;
	bool _continue, _detectFileName, _quiet;
	OFDataArray *_entity;
	OFDataArray *_body;
	of_http_request_method_t _method;
	OFMutableDictionary *_clientHeaders;
	OFHTTPClient *_HTTPClient;
	char *_buffer;
	OFStream *_output;
	intmax_t _received, _length, _resumedFrom;
	ProgressBar *_progressBar;
74
75
76
77
78
79
80

81
82
83
84
85
86
87
88
89
74
75
76
77
78
79
80
81
82

83
84
85
86
87
88
89







+

-







	[of_stderr writeFormat:
	    @"Usage: %@ -[cehHmoOPq] url1 [url2 ...]\n",
	    [OFApplication programName]];

	if (full)
		[stream writeString:
		    @"\nOptions:\n"
		    @"    -b  Specify the file to send as body\n"
		    @"    -c  Continue download of existing file\n"
		    @"    -e  Specify the file to send as entity\n"
		    @"    -h  Show this help\n"
		    @"    -H  Add a header (e.g. X-Foo:Bar)\n"
		    @"    -m  Set the method of the HTTP request\n"
		    @"    -o  Specify output file name\n"
		    @"    -O  Do a HEAD request to detect file name\n"
		    @"    -P  Specify SOCKS5 proxy\n"
		    @"    -q  Quiet mode (no output, except errors)\n"];
134
135
136
137
138
139
140
141

142
143
144


145
146
147
148
149
150
151
134
135
136
137
138
139
140

141
142


143
144
145
146
147
148
149
150
151







-
+

-
-
+
+







	    of_range(pos + 1, [header length] - pos - 1)];
	value = [value stringByDeletingEnclosingWhitespaces];

	[_clientHeaders setObject: value
			   forKey: name];
}

- (void)setEntity: (OFString*)entity
- (void)setBody: (OFString*)body
{
	[_entity release];
	_entity = [[OFDataArray alloc] initWithContentsOfFile: entity];
	[_body release];
	_body = [[OFDataArray alloc] initWithContentsOfFile: body];
}

- (void)setMethod: (OFString*)method
{
	void *pool = objc_autoreleasePoolPush();

	method = [method uppercaseString];
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
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







-
+




+
+
+



-
-
-







		[OFApplication terminateWithStatus: 1];
	}
}

- (void)applicationDidFinishLaunching
{
	OFOptionsParser *optionsParser =
	    [OFOptionsParser parserWithOptions: @"ce:hH:m:o:OP:q"];
	    [OFOptionsParser parserWithOptions: @"bc:hH:m:o:OP:q"];
	of_unichar_t option;

	while ((option = [optionsParser nextOption]) != '\0') {
		switch (option) {
		case 'b':
			[self setBody: [optionsParser argument]];
			break;
		case 'c':
			_continue = true;
			break;
		case 'e':
			[self setEntity: [optionsParser argument]];
			break;
		case 'h':
			help(of_stdout, true, 0);
			break;
		case 'H':
			[self addHeader: [optionsParser argument]];
			break;
		case 'm':
608
609
610
611
612
613
614
615

616
617
618
619
620
621
622
608
609
610
611
612
613
614

615
616
617
618
619
620
621
622







-
+







		} @catch (OFStatItemFailedException *e) {
		}
	}

	request = [OFHTTPRequest requestWithURL: URL];
	[request setHeaders: clientHeaders];
	[request setMethod: _method];
	[request setEntity: _entity];
	[request setBody: _body];

	if ((response = [self performRequest: request]) == nil) {
		_errorCode = 1;
		goto next;
	}

	headers = [response headers];