HTTPMessage.m 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #import "HTTPMessage.h"
  2. #if ! __has_feature(objc_arc)
  3. #warning This file must be compiled with ARC. Use -fobjc-arc flag (or convert project to ARC).
  4. #endif
  5. @implementation HTTPMessage
  6. - (id)initEmptyRequest
  7. {
  8. if ((self = [super init]))
  9. {
  10. message = CFHTTPMessageCreateEmpty(NULL, YES);
  11. }
  12. return self;
  13. }
  14. - (id)initRequestWithMethod:(NSString *)method URL:(NSURL *)url version:(NSString *)version
  15. {
  16. if ((self = [super init]))
  17. {
  18. message = CFHTTPMessageCreateRequest(NULL,
  19. (__bridge CFStringRef)method,
  20. (__bridge CFURLRef)url,
  21. (__bridge CFStringRef)version);
  22. }
  23. return self;
  24. }
  25. - (id)initResponseWithStatusCode:(NSInteger)code description:(NSString *)description version:(NSString *)version
  26. {
  27. if ((self = [super init]))
  28. {
  29. message = CFHTTPMessageCreateResponse(NULL,
  30. (CFIndex)code,
  31. (__bridge CFStringRef)description,
  32. (__bridge CFStringRef)version);
  33. }
  34. return self;
  35. }
  36. - (void)dealloc
  37. {
  38. if (message)
  39. {
  40. CFRelease(message);
  41. }
  42. }
  43. - (BOOL)appendData:(NSData *)data
  44. {
  45. return CFHTTPMessageAppendBytes(message, [data bytes], [data length]);
  46. }
  47. - (BOOL)isHeaderComplete
  48. {
  49. return CFHTTPMessageIsHeaderComplete(message);
  50. }
  51. - (NSString *)version
  52. {
  53. return (__bridge_transfer NSString *)CFHTTPMessageCopyVersion(message);
  54. }
  55. - (NSString *)method
  56. {
  57. return (__bridge_transfer NSString *)CFHTTPMessageCopyRequestMethod(message);
  58. }
  59. - (NSURL *)url
  60. {
  61. return (__bridge_transfer NSURL *)CFHTTPMessageCopyRequestURL(message);
  62. }
  63. - (NSInteger)statusCode
  64. {
  65. return (NSInteger)CFHTTPMessageGetResponseStatusCode(message);
  66. }
  67. - (NSDictionary *)allHeaderFields
  68. {
  69. return (__bridge_transfer NSDictionary *)CFHTTPMessageCopyAllHeaderFields(message);
  70. }
  71. - (NSString *)headerField:(NSString *)headerField
  72. {
  73. return (__bridge_transfer NSString *)CFHTTPMessageCopyHeaderFieldValue(message, (__bridge CFStringRef)headerField);
  74. }
  75. - (void)setHeaderField:(NSString *)headerField value:(NSString *)headerFieldValue
  76. {
  77. CFHTTPMessageSetHeaderFieldValue(message,
  78. (__bridge CFStringRef)headerField,
  79. (__bridge CFStringRef)headerFieldValue);
  80. }
  81. - (NSData *)messageData
  82. {
  83. return (__bridge_transfer NSData *)CFHTTPMessageCopySerializedMessage(message);
  84. }
  85. - (NSData *)body
  86. {
  87. return (__bridge_transfer NSData *)CFHTTPMessageCopyBody(message);
  88. }
  89. - (void)setBody:(NSData *)body
  90. {
  91. CFHTTPMessageSetBody(message, (__bridge CFDataRef)body);
  92. }
  93. @end