GCDWebServerResponse.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. Copyright (c) 2012-2015, 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 <Foundation/Foundation.h>
  26. /**
  27. * The GCDWebServerBodyReaderCompletionBlock is passed by GCDWebServer to the
  28. * GCDWebServerBodyReader object when reading data from it asynchronously.
  29. */
  30. typedef void (^GCDWebServerBodyReaderCompletionBlock)(NSData* data, NSError* error);
  31. /**
  32. * This protocol is used by the GCDWebServerConnection to communicate with
  33. * the GCDWebServerResponse and read the HTTP body data to send.
  34. *
  35. * Note that multiple GCDWebServerBodyReader objects can be chained together
  36. * internally e.g. to automatically apply gzip encoding to the content before
  37. * passing it on to the GCDWebServerResponse.
  38. *
  39. * @warning These methods can be called on any GCD thread.
  40. */
  41. @protocol GCDWebServerBodyReader <NSObject>
  42. @required
  43. /**
  44. * This method is called before any body data is sent.
  45. *
  46. * It should return YES on success or NO on failure and set the "error" argument
  47. * which is guaranteed to be non-NULL.
  48. */
  49. - (BOOL)open:(NSError**)error;
  50. /**
  51. * This method is called whenever body data is sent.
  52. *
  53. * It should return a non-empty NSData if there is body data available,
  54. * or an empty NSData there is no more body data, or nil on error and set
  55. * the "error" argument which is guaranteed to be non-NULL.
  56. */
  57. - (NSData*)readData:(NSError**)error;
  58. /**
  59. * This method is called after all body data has been sent.
  60. */
  61. - (void)close;
  62. @optional
  63. /**
  64. * If this method is implemented, it will be preferred over -readData:.
  65. *
  66. * It must call the passed block when data is available, passing a non-empty
  67. * NSData if there is body data available, or an empty NSData there is no more
  68. * body data, or nil on error and pass an NSError along.
  69. */
  70. - (void)asyncReadDataWithCompletion:(GCDWebServerBodyReaderCompletionBlock)block;
  71. @end
  72. /**
  73. * The GCDWebServerResponse class is used to wrap a single HTTP response.
  74. * It is instantiated by the handler of the GCDWebServer that handled the request.
  75. * If a body is present, the methods from the GCDWebServerBodyReader protocol
  76. * will be called by the GCDWebServerConnection to send it.
  77. *
  78. * The default implementation of the GCDWebServerBodyReader protocol
  79. * on the class simply returns an empty body.
  80. *
  81. * @warning GCDWebServerResponse instances can be created and used on any GCD thread.
  82. */
  83. @interface GCDWebServerResponse : NSObject <GCDWebServerBodyReader>
  84. /**
  85. * Sets the content type for the body of the response.
  86. *
  87. * The default value is nil i.e. the response has no body.
  88. *
  89. * @warning This property must be set if a body is present.
  90. */
  91. @property(nonatomic, copy) NSString* contentType;
  92. /**
  93. * Sets the content length for the body of the response. If a body is present
  94. * but this property is set to "NSUIntegerMax", this means the length of the body
  95. * cannot be known ahead of time. Chunked transfer encoding will be
  96. * automatically enabled by the GCDWebServerConnection to comply with HTTP/1.1
  97. * specifications.
  98. *
  99. * The default value is "NSUIntegerMax" i.e. the response has no body or its length
  100. * is undefined.
  101. */
  102. @property(nonatomic) NSUInteger contentLength;
  103. /**
  104. * Sets the HTTP status code for the response.
  105. *
  106. * The default value is 200 i.e. "OK".
  107. */
  108. @property(nonatomic) NSInteger statusCode;
  109. /**
  110. * Sets the caching hint for the response using the "Cache-Control" header.
  111. * This value is expressed in seconds.
  112. *
  113. * The default value is 0 i.e. "no-cache".
  114. */
  115. @property(nonatomic) NSUInteger cacheControlMaxAge;
  116. /**
  117. * Sets the last modified date for the response using the "Last-Modified" header.
  118. *
  119. * The default value is nil.
  120. */
  121. @property(nonatomic, retain) NSDate* lastModifiedDate;
  122. /**
  123. * Sets the ETag for the response using the "ETag" header.
  124. *
  125. * The default value is nil.
  126. */
  127. @property(nonatomic, copy) NSString* eTag;
  128. /**
  129. * Enables gzip encoding for the response body.
  130. *
  131. * The default value is NO.
  132. *
  133. * @warning Enabling gzip encoding will remove any "Content-Length" header
  134. * since the length of the body is not known anymore. The client will still
  135. * be able to determine the body length when connection is closed per
  136. * HTTP/1.1 specifications.
  137. */
  138. @property(nonatomic, getter=isGZipContentEncodingEnabled) BOOL gzipContentEncodingEnabled;
  139. /**
  140. * Creates an empty response.
  141. */
  142. + (instancetype)response;
  143. /**
  144. * This method is the designated initializer for the class.
  145. */
  146. - (instancetype)init;
  147. /**
  148. * Sets an additional HTTP header on the response.
  149. * Pass a nil value to remove an additional header.
  150. *
  151. * @warning Do not attempt to override the primary headers used
  152. * by GCDWebServerResponse like "Content-Type", "ETag", etc...
  153. */
  154. - (void)setValue:(NSString*)value forAdditionalHeader:(NSString*)header;
  155. /**
  156. * Convenience method that checks if the contentType property is defined.
  157. */
  158. - (BOOL)hasBody;
  159. @end
  160. @interface GCDWebServerResponse (Extensions)
  161. /**
  162. * Creates a empty response with a specific HTTP status code.
  163. */
  164. + (instancetype)responseWithStatusCode:(NSInteger)statusCode;
  165. /**
  166. * Creates an HTTP redirect response to a new URL.
  167. */
  168. + (instancetype)responseWithRedirect:(NSURL*)location permanent:(BOOL)permanent;
  169. /**
  170. * Initializes an empty response with a specific HTTP status code.
  171. */
  172. - (instancetype)initWithStatusCode:(NSInteger)statusCode;
  173. /**
  174. * Initializes an HTTP redirect response to a new URL.
  175. */
  176. - (instancetype)initWithRedirect:(NSURL*)location permanent:(BOOL)permanent;
  177. @end