Skip to content

Commit 33645d3

Browse files
committed
Fixed incorrect documentation for GCDWebServerAsyncStreamBlock
1 parent 3618dca commit 33645d3

3 files changed

Lines changed: 18 additions & 30 deletions

File tree

GCDWebServer/Responses/GCDWebServerStreamedResponse.h

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929

3030
/**
3131
* The GCDWebServerStreamBlock is called to stream the data for the HTTP body.
32-
* The block must return empty NSData when done or nil on error and set the
33-
* "error" argument which is guaranteed to be non-NULL.
32+
* The block must return either a chunk of data, an empty NSData when done, or
33+
* nil on error and set the "error" argument which is guaranteed to be non-NULL.
3434
*/
3535
typedef NSData* (^GCDWebServerStreamBlock)(NSError** error);
3636

@@ -39,13 +39,10 @@ typedef NSData* (^GCDWebServerStreamBlock)(NSError** error);
3939
* except the streamed data can be returned at a later time allowing for
4040
* truly asynchronous generation of the data.
4141
*
42-
* The block must return empty NSData when done or nil on error and set the
43-
* "error" argument which is guaranteed to be non-NULL.
42+
* The block must call "completionBlock" passing the new chunk of data when ready,
43+
* an empty NSData when done, or nil on error and pass a NSError.
4444
*
45-
* The block must regularly call "completionBlock" passing new streamed data.
46-
* Eventually it must call "completionBlock" passing an empty NSData indicating
47-
* the end of the stream has been reached, or pass nil and an NSError in case of
48-
* error.
45+
* The block cannot call "completionBlock" more than once per invocation.
4946
*/
5047
typedef void (^GCDWebServerAsyncStreamBlock)(GCDWebServerBodyReaderCompletionBlock completionBlock);
5148

Mac/main.m

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@
5252
kMode_WebDAV,
5353
kMode_WebUploader,
5454
kMode_StreamingResponse,
55-
kMode_AsyncResponse,
56-
kMode_AsyncResponse2
55+
kMode_AsyncResponse
5756
} Mode;
5857

5958
@interface Delegate : NSObject <GCDWebServerDelegate, GCDWebDAVServerDelegate, GCDWebUploaderDelegate>
@@ -145,7 +144,7 @@ int main(int argc, const char* argv[]) {
145144
BOOL bindToLocalhost = NO;
146145

147146
if (argc == 1) {
148-
fprintf(stdout, "Usage: %s [-mode webServer | htmlPage | htmlForm | htmlFileUpload | webDAV | webUploader | streamingResponse | asyncResponse | asyncResponse2] [-record] [-root directory] [-tests directory] [-authenticationMethod Basic | Digest] [-authenticationRealm realm] [-authenticationUser user] [-authenticationPassword password] [--localhost]\n\n", basename((char*)argv[0]));
147+
fprintf(stdout, "Usage: %s [-mode webServer | htmlPage | htmlForm | htmlFileUpload | webDAV | webUploader | streamingResponse | asyncResponse] [-record] [-root directory] [-tests directory] [-authenticationMethod Basic | Digest] [-authenticationRealm realm] [-authenticationUser user] [-authenticationPassword password] [--localhost]\n\n", basename((char*)argv[0]));
149148
} else {
150149
for (int i = 1; i < argc; ++i) {
151150
if (argv[i][0] != '-') {
@@ -169,8 +168,6 @@ int main(int argc, const char* argv[]) {
169168
mode = kMode_StreamingResponse;
170169
} else if (!strcmp(argv[i], "asyncResponse")) {
171170
mode = kMode_AsyncResponse;
172-
} else if (!strcmp(argv[i], "asyncResponse2")) {
173-
mode = kMode_AsyncResponse2;
174171
}
175172
} else if (!strcmp(argv[i], "-record")) {
176173
recording = YES;
@@ -360,7 +357,7 @@ int main(int argc, const char* argv[]) {
360357
fprintf(stdout, "Running in Async Response mode");
361358
webServer = [[GCDWebServer alloc] init];
362359
[webServer addHandlerForMethod:@"GET"
363-
path:@"/"
360+
path:@"/async"
364361
requestClass:[GCDWebServerRequest class]
365362
asyncProcessBlock:^(GCDWebServerRequest* request, GCDWebServerCompletionBlock completionBlock) {
366363

@@ -370,15 +367,8 @@ int main(int argc, const char* argv[]) {
370367
});
371368

372369
}];
373-
break;
374-
}
375-
376-
// Test async responses 2
377-
case kMode_AsyncResponse2: {
378-
fprintf(stdout, "Running in Async Response 2 mode");
379-
webServer = [[GCDWebServer alloc] init];
380370
[webServer addHandlerForMethod:@"GET"
381-
path:@"/"
371+
path:@"/async2"
382372
requestClass:[GCDWebServerRequest class]
383373
asyncProcessBlock:^(GCDWebServerRequest* request, GCDWebServerCompletionBlock handlerCompletionBlock) {
384374

README.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -188,22 +188,23 @@ New in GCDWebServer 3.0 is the ability to process HTTP requests aysnchronously i
188188
requestClass:[GCDWebServerRequest class]
189189
processBlock:^GCDWebServerResponse *(GCDWebServerRequest* request) {
190190

191+
NSMutableArray* contents = [NSMutableArray arrayWithObjects:@"<html><body><p>\n", @"Hello World!\n", @"</p></body></html>\n", nil]; // Fake data source we are reading from
191192
GCDWebServerStreamedResponse* response = [GCDWebServerStreamedResponse responseWithContentType:@"text/html" asyncStreamBlock:^(GCDWebServerBodyReaderCompletionBlock completionBlock) {
192193

194+
// Simulate a delay reading from the fake data source
193195
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
194-
completionBlock([@"<html><body><p>Hello" dataUsingEncoding:NSUTF8StringEncoding], nil); // Generate the 1st part of the stream data
195-
196-
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
197-
completionBlock([@"World</p></body></html>" dataUsingEncoding:NSUTF8StringEncoding], nil); // Generate the 2nd part of the stream data
198-
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 {
199201
completionBlock([NSData data], nil); // Must pass an empty NSData to signal the end of the stream
200-
});
201-
202+
}
202203
});
203204

204205
}];
205206
return response;
206-
207+
207208
}];
208209
```
209210

0 commit comments

Comments
 (0)