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