ObjFW  Check-in [0319fe1eb9]

Overview
Comment:OFHTTPClient: Rename to -[asyncPerformRequest:]

This makes it clear that the request is handled asynchronously and makes
it possible to reintroduce a synchronous version later.

Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 0319fe1eb973f289a45d83f0a8db15145ba982dbfb8786db2ecf8976df048497
User & Date: js on 2017-09-24 21:00:09
Other Links: manifest | tags
Context
2017-09-25
00:02
OFObject: Add -[performSelector*] with 4 objects check-in: f714793eb4 user: js tags: trunk
2017-09-24
21:00
OFHTTPClient: Rename to -[asyncPerformRequest:] check-in: 0319fe1eb9 user: js tags: trunk
17:35
OFUDPSocket: Add support for async sending check-in: 842c55dd83 user: js tags: trunk
Changes

Modified src/OFHTTPClient.h from [cce5af40ca] to [e0785471fb].

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
 * @return A new, autoreleased OFHTTPClient
 */
+ (instancetype)client;

/*!
 * @brief Asynchronously performs the specified HTTP request.
 */
- (void)performRequest: (OFHTTPRequest *)request;

/*!
 * @brief Asynchronously performs the specified HTTP request.
 *
 * @param request The request to perform
 * @param redirects The maximum number of redirects after which no further
 *		    attempt is done to follow the redirect, but instead the
 *		    redirect is treated as an OFHTTPResponse
 */
- (void)performRequest: (OFHTTPRequest *)request
	     redirects: (unsigned int)redirects;

/*!
 * @brief Closes connections that are still open due to keep-alive.
 */
- (void)close;
@end

OF_ASSUME_NONNULL_END







|









|
|








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
 * @return A new, autoreleased OFHTTPClient
 */
+ (instancetype)client;

/*!
 * @brief Asynchronously performs the specified HTTP request.
 */
- (void)asyncPerformRequest: (OFHTTPRequest *)request;

/*!
 * @brief Asynchronously performs the specified HTTP request.
 *
 * @param request The request to perform
 * @param redirects The maximum number of redirects after which no further
 *		    attempt is done to follow the redirect, but instead the
 *		    redirect is treated as an OFHTTPResponse
 */
- (void)asyncPerformRequest: (OFHTTPRequest *)request
		  redirects: (unsigned int)redirects;

/*!
 * @brief Closes connections that are still open due to keep-alive.
 */
- (void)close;
@end

OF_ASSUME_NONNULL_END

Modified src/OFHTTPClient.m from [390eee68b1] to [3d75f15cfb].

374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
			}

			[newRequest setURL: newURL];
			[newRequest setHeaders: newHeaders];

			_client->_inProgress = false;

			[_client performRequest: newRequest
				      redirects: _redirects - 1];
			return;
		}
	}

	if (_status / 100 != 2)
		@throw [OFHTTPRequestFailedException
		    exceptionWithRequest: _request







|
|







374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
			}

			[newRequest setURL: newURL];
			[newRequest setHeaders: newHeaders];

			_client->_inProgress = false;

			[_client asyncPerformRequest: newRequest
					   redirects: _redirects - 1];
			return;
		}
	}

	if (_status / 100 != 2)
		@throw [OFHTTPRequestFailedException
		    exceptionWithRequest: _request
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
- (void)dealloc
{
	[self close];

	[super dealloc];
}

- (void)performRequest: (OFHTTPRequest *)request
{
	[self performRequest: request
		   redirects: 10];
}

- (void)performRequest: (OFHTTPRequest *)request
	     redirects: (unsigned int)redirects
{
	void *pool = objc_autoreleasePoolPush();
	OFURL *URL = [request URL];
	OFString *scheme = [URL scheme];

	if (![scheme isEqual: @"http"] && ![scheme isEqual: @"https"])
		@throw [OFUnsupportedProtocolException exceptionWithURL: URL];







|

|
|


|
|







911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
- (void)dealloc
{
	[self close];

	[super dealloc];
}

- (void)asyncPerformRequest: (OFHTTPRequest *)request
{
	[self asyncPerformRequest: request
			redirects: 10];
}

- (void)asyncPerformRequest: (OFHTTPRequest *)request
		  redirects: (unsigned int)redirects
{
	void *pool = objc_autoreleasePoolPush();
	OFURL *URL = [request URL];
	OFString *scheme = [URL scheme];

	if (![scheme isEqual: @"http"] && ![scheme isEqual: @"https"])
		@throw [OFUnsupportedProtocolException exceptionWithURL: URL];

Modified tests/OFHTTPClientTests.m from [a00a732f44] to [1220c3f5ba].

118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
	[cond wait];
	[cond unlock];

	URL = [OFURL URLWithString:
	    [OFString stringWithFormat: @"http://127.0.0.1:%" @PRIu16 "/foo",
					server->_port]];

	TEST(@"-[performRequest:]",
	    (client = [OFHTTPClient client]) && R([client setDelegate: self]) &&
	    R(request = [OFHTTPRequest requestWithURL: URL]) &&
	    R([client performRequest: request]))

	[[OFRunLoop mainRunLoop] runUntilDate:
	    [OFDate dateWithTimeIntervalSinceNow: 2]];
	[response autorelease];

	TEST(@"Asynchronous handling of requests", response != nil)








|


|







118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
	[cond wait];
	[cond unlock];

	URL = [OFURL URLWithString:
	    [OFString stringWithFormat: @"http://127.0.0.1:%" @PRIu16 "/foo",
					server->_port]];

	TEST(@"-[asyncPerformRequest:]",
	    (client = [OFHTTPClient client]) && R([client setDelegate: self]) &&
	    R(request = [OFHTTPRequest requestWithURL: URL]) &&
	    R([client asyncPerformRequest: request]))

	[[OFRunLoop mainRunLoop] runUntilDate:
	    [OFDate dateWithTimeIntervalSinceNow: 2]];
	[response autorelease];

	TEST(@"Asynchronous handling of requests", response != nil)

Modified utils/ofhttp/OFHTTP.m from [725d764e14] to [7ea7ca97c5].

904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
		if (!_quiet)
			[of_stdout writeFormat: @"⠒ %@", [URL string]];

		request = [OFHTTPRequest requestWithURL: URL];
		[request setHeaders: clientHeaders];
		[request setMethod: OF_HTTP_REQUEST_METHOD_HEAD];

		[_HTTPClient performRequest: request];
		return;
	}

	_detectedFileName = false;

	if (!_quiet)
		[of_stdout writeFormat: @"⇣ %@", [URL string]];







|







904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
		if (!_quiet)
			[of_stdout writeFormat: @"⠒ %@", [URL string]];

		request = [OFHTTPRequest requestWithURL: URL];
		[request setHeaders: clientHeaders];
		[request setMethod: OF_HTTP_REQUEST_METHOD_HEAD];

		[_HTTPClient asyncPerformRequest: request];
		return;
	}

	_detectedFileName = false;

	if (!_quiet)
		[of_stdout writeFormat: @"⇣ %@", [URL string]];
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
	}

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

	[_HTTPClient performRequest: request];
	return;

next:
	[self performSelector: @selector(downloadNextURL)
		   afterDelay: 0];
}
@end







|







945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
	}

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

	[_HTTPClient asyncPerformRequest: request];
	return;

next:
	[self performSelector: @selector(downloadNextURL)
		   afterDelay: 0];
}
@end