GCDWebServerStreamedResponse.h 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. Copyright (c) 2012-2015, 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 "GCDWebServerResponse.h"
  26. /**
  27. * The GCDWebServerStreamBlock is called to stream the data for the HTTP body.
  28. * The block must return either a chunk of data, an empty NSData when done, or
  29. * nil on error and set the "error" argument which is guaranteed to be non-NULL.
  30. */
  31. typedef NSData* (^GCDWebServerStreamBlock)(NSError** error);
  32. /**
  33. * The GCDWebServerAsyncStreamBlock works like the GCDWebServerStreamBlock
  34. * except the streamed data can be returned at a later time allowing for
  35. * truly asynchronous generation of the data.
  36. *
  37. * The block must call "completionBlock" passing the new chunk of data when ready,
  38. * an empty NSData when done, or nil on error and pass a NSError.
  39. *
  40. * The block cannot call "completionBlock" more than once per invocation.
  41. */
  42. typedef void (^GCDWebServerAsyncStreamBlock)(GCDWebServerBodyReaderCompletionBlock completionBlock);
  43. /**
  44. * The GCDWebServerStreamedResponse subclass of GCDWebServerResponse streams
  45. * the body of the HTTP response using a GCD block.
  46. */
  47. @interface GCDWebServerStreamedResponse : GCDWebServerResponse
  48. /**
  49. * Creates a response with streamed data and a given content type.
  50. */
  51. + (instancetype)responseWithContentType:(NSString*)type streamBlock:(GCDWebServerStreamBlock)block;
  52. /**
  53. * Creates a response with async streamed data and a given content type.
  54. */
  55. + (instancetype)responseWithContentType:(NSString*)type asyncStreamBlock:(GCDWebServerAsyncStreamBlock)block;
  56. /**
  57. * Initializes a response with streamed data and a given content type.
  58. */
  59. - (instancetype)initWithContentType:(NSString*)type streamBlock:(GCDWebServerStreamBlock)block;
  60. /**
  61. * This method is the designated initializer for the class.
  62. */
  63. - (instancetype)initWithContentType:(NSString*)type asyncStreamBlock:(GCDWebServerAsyncStreamBlock)block;
  64. @end