|
| 1 | +/* |
| 2 | + Copyright (c) 2012-2014, 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 | +#import "GCDWebServerPrivate.h" |
| 29 | + |
| 30 | +@interface GCDWebServerDataResponse () { |
| 31 | +@private |
| 32 | + NSData* _data; |
| 33 | + BOOL _done; |
| 34 | +} |
| 35 | +@end |
| 36 | + |
| 37 | +@implementation GCDWebServerDataResponse |
| 38 | + |
| 39 | ++ (GCDWebServerDataResponse*)responseWithData:(NSData*)data contentType:(NSString*)type { |
| 40 | + return ARC_AUTORELEASE([[[self class] alloc] initWithData:data contentType:type]); |
| 41 | +} |
| 42 | + |
| 43 | +- (id)initWithData:(NSData*)data contentType:(NSString*)type { |
| 44 | + if (data == nil) { |
| 45 | + DNOT_REACHED(); |
| 46 | + ARC_RELEASE(self); |
| 47 | + return nil; |
| 48 | + } |
| 49 | + |
| 50 | + if ((self = [super init])) { |
| 51 | + _data = ARC_RETAIN(data); |
| 52 | + |
| 53 | + self.contentType = type; |
| 54 | + self.contentLength = data.length; |
| 55 | + } |
| 56 | + return self; |
| 57 | +} |
| 58 | + |
| 59 | +- (void)dealloc { |
| 60 | + ARC_RELEASE(_data); |
| 61 | + |
| 62 | + ARC_DEALLOC(super); |
| 63 | +} |
| 64 | + |
| 65 | +- (NSData*)readData:(NSError**)error { |
| 66 | + NSData* data; |
| 67 | + if (_done) { |
| 68 | + data = [NSData data]; |
| 69 | + } else { |
| 70 | + data = _data; |
| 71 | + _done = YES; |
| 72 | + } |
| 73 | + return data; |
| 74 | +} |
| 75 | + |
| 76 | +@end |
| 77 | + |
| 78 | +@implementation GCDWebServerDataResponse (Extensions) |
| 79 | + |
| 80 | ++ (GCDWebServerDataResponse*)responseWithText:(NSString*)text { |
| 81 | + return ARC_AUTORELEASE([[self alloc] initWithText:text]); |
| 82 | +} |
| 83 | + |
| 84 | ++ (GCDWebServerDataResponse*)responseWithHTML:(NSString*)html { |
| 85 | + return ARC_AUTORELEASE([[self alloc] initWithHTML:html]); |
| 86 | +} |
| 87 | + |
| 88 | ++ (GCDWebServerDataResponse*)responseWithHTMLTemplate:(NSString*)path variables:(NSDictionary*)variables { |
| 89 | + return ARC_AUTORELEASE([[self alloc] initWithHTMLTemplate:path variables:variables]); |
| 90 | +} |
| 91 | + |
| 92 | ++ (GCDWebServerDataResponse*)responseWithJSONObject:(id)object { |
| 93 | + return ARC_AUTORELEASE([[self alloc] initWithJSONObject:object]); |
| 94 | +} |
| 95 | + |
| 96 | ++ (GCDWebServerDataResponse*)responseWithJSONObject:(id)object contentType:(NSString*)type { |
| 97 | + return ARC_AUTORELEASE([[self alloc] initWithJSONObject:object contentType:type]); |
| 98 | +} |
| 99 | + |
| 100 | +- (id)initWithText:(NSString*)text { |
| 101 | + NSData* data = [text dataUsingEncoding:NSUTF8StringEncoding]; |
| 102 | + if (data == nil) { |
| 103 | + DNOT_REACHED(); |
| 104 | + ARC_RELEASE(self); |
| 105 | + return nil; |
| 106 | + } |
| 107 | + return [self initWithData:data contentType:@"text/plain; charset=utf-8"]; |
| 108 | +} |
| 109 | + |
| 110 | +- (id)initWithHTML:(NSString*)html { |
| 111 | + NSData* data = [html dataUsingEncoding:NSUTF8StringEncoding]; |
| 112 | + if (data == nil) { |
| 113 | + DNOT_REACHED(); |
| 114 | + ARC_RELEASE(self); |
| 115 | + return nil; |
| 116 | + } |
| 117 | + return [self initWithData:data contentType:@"text/html; charset=utf-8"]; |
| 118 | +} |
| 119 | + |
| 120 | +- (id)initWithHTMLTemplate:(NSString*)path variables:(NSDictionary*)variables { |
| 121 | + NSMutableString* html = [[NSMutableString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:NULL]; |
| 122 | + [variables enumerateKeysAndObjectsUsingBlock:^(NSString* key, NSString* value, BOOL* stop) { |
| 123 | + [html replaceOccurrencesOfString:[NSString stringWithFormat:@"%%%@%%", key] withString:value options:0 range:NSMakeRange(0, html.length)]; |
| 124 | + }]; |
| 125 | + id response = [self initWithHTML:html]; |
| 126 | + ARC_RELEASE(html); |
| 127 | + return response; |
| 128 | +} |
| 129 | + |
| 130 | +- (id)initWithJSONObject:(id)object { |
| 131 | + return [self initWithJSONObject:object contentType:@"application/json"]; |
| 132 | +} |
| 133 | + |
| 134 | +- (id)initWithJSONObject:(id)object contentType:(NSString*)type { |
| 135 | + NSData* data = [NSJSONSerialization dataWithJSONObject:object options:0 error:NULL]; |
| 136 | + if (data == nil) { |
| 137 | + ARC_RELEASE(self); |
| 138 | + return nil; |
| 139 | + } |
| 140 | + return [self initWithData:data contentType:type]; |
| 141 | +} |
| 142 | + |
| 143 | +@end |
0 commit comments