Skip to content

Commit fe472cd

Browse files
committed
Update README.md
1 parent 9c33c83 commit fe472cd

File tree

1 file changed

+60
-60
lines changed

1 file changed

+60
-60
lines changed

README.md

Lines changed: 60 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -150,66 +150,6 @@ println("Visit \(webServer.serverURL) in your web browser")
150150
#import "GCDWebServerDataResponse.h"
151151
```
152152

153-
Asynchronous HTTP Responses
154-
===========================
155-
156-
New in GCDWebServer 3.0 is the ability to process HTTP requests aysnchronously i.e. add handlers to the server which generate their ```GCDWebServerResponse``` asynchronously. This is achieved by adding handlers that use a ```GCDWebServerAsyncProcessBlock``` instead of a ```GCDWebServerProcessBlock```. Here's an example:
157-
158-
**(Synchronous version)** The handler blocks while generating the HTTP response:
159-
```objectivec
160-
[webServer addDefaultHandlerForMethod:@"GET"
161-
requestClass:[GCDWebServerRequest class]
162-
processBlock:^GCDWebServerResponse *(GCDWebServerRequest* request) {
163-
164-
GCDWebServerDataResponse* response = [GCDWebServerDataResponse responseWithHTML:@"<html><body><p>Hello World</p></body></html>"];
165-
return response;
166-
167-
}];
168-
```
169-
170-
**(Asynchronous version)** The handler returns immediately and calls back GCDWebServer later with the generated HTTP response:
171-
```objectivec
172-
[webServer addDefaultHandlerForMethod:@"GET"
173-
requestClass:[GCDWebServerRequest class]
174-
asyncProcessBlock:^(GCDWebServerRequest* request, GCDWebServerCompletionBlock completionBlock) {
175-
176-
// Do some async operation like network access or file I/O (simulated here using dispatch_after())
177-
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
178-
GCDWebServerDataResponse* response = [GCDWebServerDataResponse responseWithHTML:@"<html><body><p>Hello World</p></body></html>"];
179-
completionBlock(response);
180-
});
181-
182-
}];
183-
```
184-
185-
**(Advanced asynchronous version)** The handler returns immediately a streamed HTTP response which itself generates its contents asynchronously:
186-
```objectivec
187-
[webServer addDefaultHandlerForMethod:@"GET"
188-
requestClass:[GCDWebServerRequest class]
189-
processBlock:^GCDWebServerResponse *(GCDWebServerRequest* request) {
190-
191-
NSMutableArray* contents = [NSMutableArray arrayWithObjects:@"<html><body><p>\n", @"Hello World!\n", @"</p></body></html>\n", nil]; // Fake data source we are reading from
192-
GCDWebServerStreamedResponse* response = [GCDWebServerStreamedResponse responseWithContentType:@"text/html" asyncStreamBlock:^(GCDWebServerBodyReaderCompletionBlock completionBlock) {
193-
194-
// Simulate a delay reading from the fake data source
195-
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
196-
NSString* string = contents.firstObject;
197-
if (string) {
198-
[contents removeObjectAtIndex:0];
199-
completionBlock([string dataUsingEncoding:NSUTF8StringEncoding], nil); // Generate the 2nd part of the stream data
200-
} else {
201-
completionBlock([NSData data], nil); // Must pass an empty NSData to signal the end of the stream
202-
}
203-
});
204-
205-
}];
206-
return response;
207-
208-
}];
209-
```
210-
211-
*Note that you can even combine both the asynchronous and advanced asynchronous versions to return asynchronously an asynchronous HTTP response!*
212-
213153
Web Based Uploads in iOS Apps
214154
=============================
215155

@@ -317,6 +257,66 @@ Handlers require 2 GCD blocks:
317257

318258
Note that most methods on ```GCDWebServer``` to add handlers only require the ```GCDWebServerProcessBlock``` or ```GCDWebServerAsyncProcessBlock``` as they already provide a built-in ```GCDWebServerMatchBlock``` e.g. to match a URL path with a Regex.
319259

260+
Asynchronous HTTP Responses
261+
===========================
262+
263+
New in GCDWebServer 3.0 is the ability to process HTTP requests aysnchronously i.e. add handlers to the server which generate their ```GCDWebServerResponse``` asynchronously. This is achieved by adding handlers that use a ```GCDWebServerAsyncProcessBlock``` instead of a ```GCDWebServerProcessBlock```. Here's an example:
264+
265+
**(Synchronous version)** The handler blocks while generating the HTTP response:
266+
```objectivec
267+
[webServer addDefaultHandlerForMethod:@"GET"
268+
requestClass:[GCDWebServerRequest class]
269+
processBlock:^GCDWebServerResponse *(GCDWebServerRequest* request) {
270+
271+
GCDWebServerDataResponse* response = [GCDWebServerDataResponse responseWithHTML:@"<html><body><p>Hello World</p></body></html>"];
272+
return response;
273+
274+
}];
275+
```
276+
277+
**(Asynchronous version)** The handler returns immediately and calls back GCDWebServer later with the generated HTTP response:
278+
```objectivec
279+
[webServer addDefaultHandlerForMethod:@"GET"
280+
requestClass:[GCDWebServerRequest class]
281+
asyncProcessBlock:^(GCDWebServerRequest* request, GCDWebServerCompletionBlock completionBlock) {
282+
283+
// Do some async operation like network access or file I/O (simulated here using dispatch_after())
284+
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
285+
GCDWebServerDataResponse* response = [GCDWebServerDataResponse responseWithHTML:@"<html><body><p>Hello World</p></body></html>"];
286+
completionBlock(response);
287+
});
288+
289+
}];
290+
```
291+
292+
**(Advanced asynchronous version)** The handler returns immediately a streamed HTTP response which itself generates its contents asynchronously:
293+
```objectivec
294+
[webServer addDefaultHandlerForMethod:@"GET"
295+
requestClass:[GCDWebServerRequest class]
296+
processBlock:^GCDWebServerResponse *(GCDWebServerRequest* request) {
297+
298+
NSMutableArray* contents = [NSMutableArray arrayWithObjects:@"<html><body><p>\n", @"Hello World!\n", @"</p></body></html>\n", nil]; // Fake data source we are reading from
299+
GCDWebServerStreamedResponse* response = [GCDWebServerStreamedResponse responseWithContentType:@"text/html" asyncStreamBlock:^(GCDWebServerBodyReaderCompletionBlock completionBlock) {
300+
301+
// Simulate a delay reading from the fake data source
302+
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
303+
NSString* string = contents.firstObject;
304+
if (string) {
305+
[contents removeObjectAtIndex:0];
306+
completionBlock([string dataUsingEncoding:NSUTF8StringEncoding], nil); // Generate the 2nd part of the stream data
307+
} else {
308+
completionBlock([NSData data], nil); // Must pass an empty NSData to signal the end of the stream
309+
}
310+
});
311+
312+
}];
313+
return response;
314+
315+
}];
316+
```
317+
318+
*Note that you can even combine both the asynchronous and advanced asynchronous versions to return asynchronously an asynchronous HTTP response!*
319+
320320
GCDWebServer & Background Mode for iOS Apps
321321
===========================================
322322

0 commit comments

Comments
 (0)