HTTPConnection.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #import <Foundation/Foundation.h>
  2. @class GCDAsyncSocket;
  3. @class HTTPMessage;
  4. @class HTTPServer;
  5. @class WebSocket;
  6. @protocol HTTPResponse;
  7. #define HTTPConnectionDidDieNotification @"HTTPConnectionDidDie"
  8. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  9. #pragma mark -
  10. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  11. @interface HTTPConfig : NSObject
  12. {
  13. HTTPServer __unsafe_unretained *server;
  14. NSString __strong *documentRoot;
  15. dispatch_queue_t queue;
  16. }
  17. - (id)initWithServer:(HTTPServer *)server documentRoot:(NSString *)documentRoot;
  18. - (id)initWithServer:(HTTPServer *)server documentRoot:(NSString *)documentRoot queue:(dispatch_queue_t)q;
  19. @property (nonatomic, unsafe_unretained, readonly) HTTPServer *server;
  20. @property (nonatomic, strong, readonly) NSString *documentRoot;
  21. @property (nonatomic, readonly) dispatch_queue_t queue;
  22. @end
  23. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  24. #pragma mark -
  25. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  26. @interface HTTPConnection : NSObject
  27. {
  28. dispatch_queue_t connectionQueue;
  29. GCDAsyncSocket *asyncSocket;
  30. HTTPConfig *config;
  31. BOOL started;
  32. HTTPMessage *request;
  33. unsigned int numHeaderLines;
  34. BOOL sentResponseHeaders;
  35. NSString *nonce;
  36. long lastNC;
  37. NSObject<HTTPResponse> *httpResponse;
  38. NSMutableArray *ranges;
  39. NSMutableArray *ranges_headers;
  40. NSString *ranges_boundry;
  41. int rangeIndex;
  42. UInt64 requestContentLength;
  43. UInt64 requestContentLengthReceived;
  44. UInt64 requestChunkSize;
  45. UInt64 requestChunkSizeReceived;
  46. NSMutableArray *responseDataSizes;
  47. }
  48. - (id)initWithAsyncSocket:(GCDAsyncSocket *)newSocket configuration:(HTTPConfig *)aConfig;
  49. - (void)start;
  50. - (void)stop;
  51. - (void)startConnection;
  52. - (BOOL)supportsMethod:(NSString *)method atPath:(NSString *)path;
  53. - (BOOL)expectsRequestBodyFromMethod:(NSString *)method atPath:(NSString *)path;
  54. - (BOOL)isSecureServer;
  55. - (NSArray *)sslIdentityAndCertificates;
  56. - (BOOL)isPasswordProtected:(NSString *)path;
  57. - (BOOL)useDigestAccessAuthentication;
  58. - (NSString *)realm;
  59. - (NSString *)passwordForUser:(NSString *)username;
  60. - (NSDictionary *)parseParams:(NSString *)query;
  61. - (NSDictionary *)parseGetParams;
  62. - (NSString *)requestURI;
  63. - (NSArray *)directoryIndexFileNames;
  64. - (NSString *)filePathForURI:(NSString *)path;
  65. - (NSString *)filePathForURI:(NSString *)path allowDirectory:(BOOL)allowDirectory;
  66. - (NSObject<HTTPResponse> *)httpResponseForMethod:(NSString *)method URI:(NSString *)path;
  67. - (WebSocket *)webSocketForURI:(NSString *)path;
  68. - (void)prepareForBodyWithSize:(UInt64)contentLength;
  69. - (void)processBodyData:(NSData *)postDataChunk;
  70. - (void)finishBody;
  71. - (void)handleVersionNotSupported:(NSString *)version;
  72. - (void)handleAuthenticationFailed;
  73. - (void)handleResourceNotFound;
  74. - (void)handleInvalidRequest:(NSData *)data;
  75. - (void)handleUnknownMethod:(NSString *)method;
  76. - (NSData *)preprocessResponse:(HTTPMessage *)response;
  77. - (NSData *)preprocessErrorResponse:(HTTPMessage *)response;
  78. - (void)finishResponse;
  79. - (BOOL)shouldDie;
  80. - (void)die;
  81. @end
  82. @interface HTTPConnection (AsynchronousHTTPResponse)
  83. - (void)responseHasAvailableData:(NSObject<HTTPResponse> *)sender;
  84. - (void)responseDidAbort:(NSObject<HTTPResponse> *)sender;
  85. @end