1/* 2 Copyright (c) 2012-2019, Pierre-Olivier Latour 3 All rights reserved. 4 5 Redistribution and use in source and binary forms, with or without 6 modification, are permitted provided that the following conditions are met: 7 * Redistributions of source code must retain the above copyright 8 notice, this list of conditions and the following disclaimer. 9 * Redistributions in binary form must reproduce the above copyright 10 notice, this list of conditions and the following disclaimer in the 11 documentation and/or other materials provided with the distribution. 12 * The name of Pierre-Olivier Latour may not be used to endorse 13 or promote products derived from this software without specific 14 prior written permission. 15 16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 DISCLAIMED. IN NO EVENT SHALL PIERRE-OLIVIER LATOUR BE LIABLE FOR ANY 20 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28#if !__has_feature(objc_arc) 29#error GCDWebServer requires ARC 30#endif 31 32#import "GCDWebServerPrivate.h" 33 34@implementation GCDWebServerStreamedResponse { 35 GCDWebServerAsyncStreamBlock _block; 36} 37 38@dynamic contentType; 39 40+ (instancetype)responseWithContentType:(NSString*)type streamBlock:(GCDWebServerStreamBlock)block { 41 return [(GCDWebServerStreamedResponse*)[[self class] alloc] initWithContentType:type streamBlock:block]; 42} 43 44+ (instancetype)responseWithContentType:(NSString*)type asyncStreamBlock:(GCDWebServerAsyncStreamBlock)block { 45 return [(GCDWebServerStreamedResponse*)[[self class] alloc] initWithContentType:type asyncStreamBlock:block]; 46} 47 48- (instancetype)initWithContentType:(NSString*)type streamBlock:(GCDWebServerStreamBlock)block { 49 return [self initWithContentType:type 50 asyncStreamBlock:^(GCDWebServerBodyReaderCompletionBlock completionBlock) { 51 NSError* error = nil; 52 NSData* data = block(&error); 53 completionBlock(data, error); 54 }]; 55} 56 57- (instancetype)initWithContentType:(NSString*)type asyncStreamBlock:(GCDWebServerAsyncStreamBlock)block { 58 if ((self = [super init])) { 59 _block = [block copy]; 60 61 self.contentType = type; 62 } 63 return self; 64} 65 66- (void)asyncReadDataWithCompletion:(GCDWebServerBodyReaderCompletionBlock)block { 67 _block(block); 68} 69 70- (NSString*)description { 71 NSMutableString* description = [NSMutableString stringWithString:[super description]]; 72 [description appendString:@"\n\n<STREAM>"]; 73 return description; 74} 75 76@end 77