GCDWebServerResponse.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. Copyright (c) 2012-2013, 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. @interface GCDWebServerResponse : NSObject {
  27. @private
  28. NSString* _type;
  29. NSUInteger _length;
  30. NSInteger _status;
  31. NSUInteger _maxAge;
  32. NSMutableDictionary* _headers;
  33. }
  34. @property(nonatomic, readonly) NSString* contentType;
  35. @property(nonatomic, readonly) NSUInteger contentLength;
  36. @property(nonatomic) NSInteger statusCode; // Default is 200
  37. @property(nonatomic) NSUInteger cacheControlMaxAge; // Default is 0 seconds i.e. "no-cache"
  38. @property(nonatomic, readonly) NSDictionary* additionalHeaders;
  39. + (GCDWebServerResponse*) response;
  40. - (id)init;
  41. - (id)initWithContentType:(NSString*)type contentLength:(NSUInteger)length; // Pass nil contentType to indicate empty body
  42. - (void)setValue:(NSString*)value forAdditionalHeader:(NSString*)header;
  43. - (BOOL)hasBody; // Convenience method
  44. @end
  45. @interface GCDWebServerResponse (Subclassing)
  46. - (BOOL)open; // Implementation required
  47. - (NSInteger)read:(void*)buffer maxLength:(NSUInteger)length; // Implementation required
  48. - (BOOL)close; // Implementation required
  49. @end
  50. @interface GCDWebServerResponse (Extensions)
  51. + (GCDWebServerResponse*)responseWithStatusCode:(NSInteger)statusCode;
  52. + (GCDWebServerResponse*)responseWithRedirect:(NSURL*)location permanent:(BOOL)permanent;
  53. - (id)initWithStatusCode:(NSInteger)statusCode;
  54. - (id)initWithRedirect:(NSURL*)location permanent:(BOOL)permanent;
  55. @end
  56. @interface GCDWebServerDataResponse : GCDWebServerResponse {
  57. @private
  58. NSData* _data;
  59. NSInteger _offset;
  60. }
  61. + (GCDWebServerDataResponse*)responseWithData:(NSData*)data contentType:(NSString*)type;
  62. - (id)initWithData:(NSData*)data contentType:(NSString*)type;
  63. @end
  64. @interface GCDWebServerDataResponse (Extensions)
  65. + (GCDWebServerDataResponse*)responseWithText:(NSString*)text;
  66. + (GCDWebServerDataResponse*)responseWithHTML:(NSString*)html;
  67. + (GCDWebServerDataResponse*)responseWithHTMLTemplate:(NSString*)path variables:(NSDictionary*)variables;
  68. - (id)initWithText:(NSString*)text; // Encodes using UTF-8
  69. - (id)initWithHTML:(NSString*)html; // Encodes using UTF-8
  70. - (id)initWithHTMLTemplate:(NSString*)path variables:(NSDictionary*)variables; // Simple template system that replaces all occurences of "%variable%" with corresponding value (encodes using UTF-8)
  71. @end
  72. @interface GCDWebServerFileResponse : GCDWebServerResponse {
  73. @private
  74. NSString* _path;
  75. int _file;
  76. }
  77. + (GCDWebServerFileResponse*)responseWithFile:(NSString*)path;
  78. + (GCDWebServerFileResponse*)responseWithFile:(NSString*)path isAttachment:(BOOL)attachment;
  79. - (id)initWithFile:(NSString*)path;
  80. - (id)initWithFile:(NSString*)path isAttachment:(BOOL)attachment;
  81. @end