RouteResponse.m 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #import "RouteResponse.h"
  2. #import "HTTPConnection.h"
  3. #import "HTTPDataResponse.h"
  4. #import "HTTPResponseProxy.h"
  5. #pragma clang diagnostic ignored "-Wdirect-ivar-access"
  6. #pragma clang diagnostic ignored "-Widiomatic-parentheses"
  7. @implementation RouteResponse {
  8. NSMutableDictionary *headers;
  9. HTTPResponseProxy *proxy;
  10. }
  11. @synthesize connection;
  12. @synthesize headers;
  13. - (id)initWithConnection:(HTTPConnection *)theConnection {
  14. if (self = [super init]) {
  15. connection = theConnection;
  16. headers = [[NSMutableDictionary alloc] init];
  17. proxy = [[HTTPResponseProxy alloc] init];
  18. }
  19. return self;
  20. }
  21. - (NSObject <HTTPResponse>*)response {
  22. return proxy.response;
  23. }
  24. - (void)setResponse:(NSObject <HTTPResponse>*)response {
  25. proxy.response = response;
  26. }
  27. - (NSObject <HTTPResponse>*)proxiedResponse {
  28. if (proxy.response != nil || proxy.customStatus != 0 || [headers count] > 0) {
  29. return proxy;
  30. }
  31. return nil;
  32. }
  33. - (NSInteger)statusCode {
  34. return proxy.status;
  35. }
  36. - (void)setStatusCode:(NSInteger)status {
  37. proxy.status = status;
  38. }
  39. - (void)setHeader:(NSString *)field value:(NSString *)value {
  40. [headers setObject:value forKey:field];
  41. }
  42. - (void)respondWithString:(NSString *)string {
  43. [self respondWithString:string encoding:NSUTF8StringEncoding];
  44. }
  45. - (void)respondWithString:(NSString *)string encoding:(NSStringEncoding)encoding {
  46. [self respondWithData:[string dataUsingEncoding:encoding]];
  47. }
  48. - (void)respondWithData:(NSData *)data {
  49. self.response = [[HTTPDataResponse alloc] initWithData:data];
  50. }
  51. @end