Skip to content

Commit 8e9fe4c

Browse files
committed
Update README.md
1 parent 95bccff commit 8e9fe4c

1 file changed

Lines changed: 37 additions & 5 deletions

File tree

README.md

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,15 @@ Download or check out the [latest release](https://github.com/swisspol/GCDWebSer
4242

4343
Alternatively, you can install GCDWebServer using [CocoaPods](http://cocoapods.org/) by simply adding this line to your Xcode project's Podfile:
4444
```
45-
pod "GCDWebServer", "~> 2.0"
45+
pod "GCDWebServer", "~> 3.0"
4646
```
4747
If you want to use GCDWebUploader, use this line instead:
4848
```
49-
pod "GCDWebServer/WebUploader", "~> 2.0"
49+
pod "GCDWebServer/WebUploader", "~> 3.0"
5050
```
5151
Or this line for GCDWebDAVServer:
5252
```
53-
pod "GCDWebServer/WebDAV", "~> 2.0"
53+
pod "GCDWebServer/WebDAV", "~> 3.0"
5454
```
5555

5656
Hello World
@@ -147,6 +147,38 @@ println("Visit \(webServer.serverURL) in your web browser")
147147
#import "GCDWebServerDataResponse.h"
148148
```
149149

150+
Asynchronous HTTP Responses
151+
===========================
152+
153+
New in GCDWebServer 3.0 is the ability to process HTTP requests aysnchronously i.e. add handlers to the server that generate their ```GCDWebServerResponse``` asynchronously. This is achieved by adding handlers that use a ```GCDWebServerAsyncProcessBlock``` instead of a ```GCDWebServerProcessBlock```. Here's an example:
154+
155+
***Synchronous version***
156+
```objectivec
157+
[webServer addDefaultHandlerForMethod:@"GET"
158+
requestClass:[GCDWebServerRequest class]
159+
processBlock:^GCDWebServerResponse *(GCDWebServerRequest* request) {
160+
161+
GCDWebServerDataResponse* response = [GCDWebServerDataResponse responseWithHTML:@"<html><body><p>Hello World</p></body></html>"];
162+
return response;
163+
164+
}];
165+
```
166+
167+
***Asynchronous version***
168+
```objectivec
169+
[webServer addDefaultHandlerForMethod:@"GET"
170+
requestClass:[GCDWebServerRequest class]
171+
asyncProcessBlock:^(GCDWebServerRequest* request, GCDWebServerCompletionBlock completionBlock) {
172+
173+
// Do some async operation like network access or file I/O (simulated here using dispatch_after())
174+
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
175+
GCDWebServerDataResponse* response = [GCDWebServerDataResponse responseWithHTML:@"<html><body><p>Hello World</p></body></html>"];
176+
completionBlock(response);
177+
});
178+
179+
}];
180+
```
181+
150182
Web Based Uploads in iOS Apps
151183
=============================
152184

@@ -250,9 +282,9 @@ GCDWebServer relies on "handlers" to process incoming web requests and generatin
250282
251283
Handlers require 2 GCD blocks:
252284
* The ```GCDWebServerMatchBlock``` is called on every handler added to the ```GCDWebServer``` instance whenever a web request has started (i.e. HTTP headers have been received). It is passed the basic info for the web request (HTTP method, URL, headers...) and must decide if it wants to handle it or not. If yes, it must return a new ```GCDWebServerRequest``` instance (see above) created with this info. Otherwise, it simply returns nil.
253-
* The ```GCDWebServerProcessBlock``` is called after the web request has been fully received and is passed the ```GCDWebServerRequest``` instance created at the previous step. It must return a ```GCDWebServerResponse``` instance (see above) or nil on error, which will result in a 500 HTTP status code returned to the client. It's however recommended to return an instance of [GCDWebServerErrorResponse](GCDWebServer/Responses/GCDWebServerErrorResponse.h) on error so more useful information can be returned to the client.
285+
* The ```GCDWebServerProcessBlock``` or ```GCDWebServerAsyncProcessBlock``` is called after the web request has been fully received and is passed the ```GCDWebServerRequest``` instance created at the previous step. It must return synchronously (if using ```GCDWebServerProcessBlock```) or asynchronously (if using ```GCDWebServerAsyncProcessBlock```) a ```GCDWebServerResponse``` instance (see above) or nil on error, which will result in a 500 HTTP status code returned to the client. It's however recommended to return an instance of [GCDWebServerErrorResponse](GCDWebServer/Responses/GCDWebServerErrorResponse.h) on error so more useful information can be returned to the client.
254286
255-
Note that most methods on ```GCDWebServer``` to add handlers only require the ```GCDWebServerProcessBlock``` as they already provide a built-in ```GCDWebServerMatchBlock``` e.g. to match a URL path with a Regex.
287+
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.
256288
257289
GCDWebServer & Background Mode for iOS Apps
258290
===========================================

0 commit comments

Comments
 (0)