HTTPAsyncFileResponse.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #import <Foundation/Foundation.h>
  2. #import "HTTPResponse.h"
  3. @class HTTPConnection;
  4. /**
  5. * This is an asynchronous version of HTTPFileResponse.
  6. * It reads data from the given file asynchronously via GCD.
  7. *
  8. * It may be overriden to allow custom post-processing of the data that has been read from the file.
  9. * An example of this is the HTTPDynamicFileResponse class.
  10. **/
  11. @interface HTTPAsyncFileResponse : NSObject <HTTPResponse>
  12. {
  13. HTTPConnection *connection;
  14. NSString *filePath;
  15. UInt64 fileLength;
  16. UInt64 fileOffset; // File offset as pertains to data given to connection
  17. UInt64 readOffset; // File offset as pertains to data read from file (but maybe not returned to connection)
  18. BOOL aborted;
  19. NSData *data;
  20. int fileFD;
  21. void *readBuffer;
  22. NSUInteger readBufferSize; // Malloced size of readBuffer
  23. NSUInteger readBufferOffset; // Offset within readBuffer where the end of existing data is
  24. NSUInteger readRequestLength;
  25. dispatch_queue_t readQueue;
  26. dispatch_source_t readSource;
  27. BOOL readSourceSuspended;
  28. }
  29. - (id)initWithFilePath:(NSString *)filePath forConnection:(HTTPConnection *)connection;
  30. - (NSString *)filePath;
  31. @end
  32. /**
  33. * Explanation of Variables (excluding those that are obvious)
  34. *
  35. * fileOffset
  36. * This is the number of bytes that have been returned to the connection via the readDataOfLength method.
  37. * If 1KB of data has been read from the file, but none of that data has yet been returned to the connection,
  38. * then the fileOffset variable remains at zero.
  39. * This variable is used in the calculation of the isDone method.
  40. * Only after all data has been returned to the connection are we actually done.
  41. *
  42. * readOffset
  43. * Represents the offset of the file descriptor.
  44. * In other words, the file position indidcator for our read stream.
  45. * It might be easy to think of it as the total number of bytes that have been read from the file.
  46. * However, this isn't entirely accurate, as the setOffset: method may have caused us to
  47. * jump ahead in the file (lseek).
  48. *
  49. * readBuffer
  50. * Malloc'd buffer to hold data read from the file.
  51. *
  52. * readBufferSize
  53. * Total allocation size of malloc'd buffer.
  54. *
  55. * readBufferOffset
  56. * Represents the position in the readBuffer where we should store new bytes.
  57. *
  58. * readRequestLength
  59. * The total number of bytes that were requested from the connection.
  60. * It's OK if we return a lesser number of bytes to the connection.
  61. * It's NOT OK if we return a greater number of bytes to the connection.
  62. * Doing so would disrupt proper support for range requests.
  63. * If, however, the response is chunked then we don't need to worry about this.
  64. * Chunked responses inheritly don't support range requests.
  65. **/