GCDWebServerDataResponse.m 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. Copyright (c) 2012-2014, Pierre-Olivier Latour
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are met:
  6. * Redistributions of source code must retain the above copyright
  7. notice, this list of conditions and the following disclaimer.
  8. * Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in the
  10. documentation and/or other materials provided with the distribution.
  11. * The name of Pierre-Olivier Latour may not be used to endorse
  12. or promote products derived from this software without specific
  13. prior written permission.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  15. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  16. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  17. DISCLAIMED. IN NO EVENT SHALL PIERRE-OLIVIER LATOUR BE LIABLE FOR ANY
  18. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  19. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  20. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  21. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  23. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #import "GCDWebServerPrivate.h"
  26. @interface GCDWebServerDataResponse () {
  27. @private
  28. NSData* _data;
  29. BOOL _done;
  30. }
  31. @end
  32. @implementation GCDWebServerDataResponse
  33. + (instancetype)responseWithData:(NSData*)data contentType:(NSString*)type {
  34. return ARC_AUTORELEASE([[[self class] alloc] initWithData:data contentType:type]);
  35. }
  36. - (instancetype)initWithData:(NSData*)data contentType:(NSString*)type {
  37. if (data == nil) {
  38. DNOT_REACHED();
  39. ARC_RELEASE(self);
  40. return nil;
  41. }
  42. if ((self = [super init])) {
  43. _data = ARC_RETAIN(data);
  44. self.contentType = type;
  45. self.contentLength = data.length;
  46. }
  47. return self;
  48. }
  49. - (void)dealloc {
  50. ARC_RELEASE(_data);
  51. ARC_DEALLOC(super);
  52. }
  53. - (NSData*)readData:(NSError**)error {
  54. NSData* data;
  55. if (_done) {
  56. data = [NSData data];
  57. } else {
  58. data = _data;
  59. _done = YES;
  60. }
  61. return data;
  62. }
  63. - (NSString*)description {
  64. NSMutableString* description = [NSMutableString stringWithString:[super description]];
  65. [description appendString:@"\n\n"];
  66. [description appendString:GCDWebServerDescribeData(_data, self.contentType)];
  67. return description;
  68. }
  69. @end
  70. @implementation GCDWebServerDataResponse (Extensions)
  71. + (instancetype)responseWithText:(NSString*)text {
  72. return ARC_AUTORELEASE([[self alloc] initWithText:text]);
  73. }
  74. + (instancetype)responseWithHTML:(NSString*)html {
  75. return ARC_AUTORELEASE([[self alloc] initWithHTML:html]);
  76. }
  77. + (instancetype)responseWithHTMLTemplate:(NSString*)path variables:(NSDictionary*)variables {
  78. return ARC_AUTORELEASE([[self alloc] initWithHTMLTemplate:path variables:variables]);
  79. }
  80. + (instancetype)responseWithJSONObject:(id)object {
  81. return ARC_AUTORELEASE([[self alloc] initWithJSONObject:object]);
  82. }
  83. + (instancetype)responseWithJSONObject:(id)object contentType:(NSString*)type {
  84. return ARC_AUTORELEASE([[self alloc] initWithJSONObject:object contentType:type]);
  85. }
  86. - (instancetype)initWithText:(NSString*)text {
  87. NSData* data = [text dataUsingEncoding:NSUTF8StringEncoding];
  88. if (data == nil) {
  89. DNOT_REACHED();
  90. ARC_RELEASE(self);
  91. return nil;
  92. }
  93. return [self initWithData:data contentType:@"text/plain; charset=utf-8"];
  94. }
  95. - (instancetype)initWithHTML:(NSString*)html {
  96. NSData* data = [html dataUsingEncoding:NSUTF8StringEncoding];
  97. if (data == nil) {
  98. DNOT_REACHED();
  99. ARC_RELEASE(self);
  100. return nil;
  101. }
  102. return [self initWithData:data contentType:@"text/html; charset=utf-8"];
  103. }
  104. - (instancetype)initWithHTMLTemplate:(NSString*)path variables:(NSDictionary*)variables {
  105. NSMutableString* html = [[NSMutableString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:NULL];
  106. [variables enumerateKeysAndObjectsUsingBlock:^(NSString* key, NSString* value, BOOL* stop) {
  107. [html replaceOccurrencesOfString:[NSString stringWithFormat:@"%%%@%%", key] withString:value options:0 range:NSMakeRange(0, html.length)];
  108. }];
  109. id response = [self initWithHTML:html];
  110. ARC_RELEASE(html);
  111. return response;
  112. }
  113. - (instancetype)initWithJSONObject:(id)object {
  114. return [self initWithJSONObject:object contentType:@"application/json"];
  115. }
  116. - (instancetype)initWithJSONObject:(id)object contentType:(NSString*)type {
  117. NSData* data = [NSJSONSerialization dataWithJSONObject:object options:0 error:NULL];
  118. if (data == nil) {
  119. ARC_RELEASE(self);
  120. return nil;
  121. }
  122. return [self initWithData:data contentType:type];
  123. }
  124. @end