SDWebImageDownloaderResponseModifier.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. * This file is part of the SDWebImage package.
  3. * (c) Olivier Poitrey <rs@dailymotion.com>
  4. *
  5. * For the full copyright and license information, please view the LICENSE
  6. * file that was distributed with this source code.
  7. */
  8. #import <Foundation/Foundation.h>
  9. #import "SDWebImageCompat.h"
  10. typedef NSURLResponse * _Nullable (^SDWebImageDownloaderResponseModifierBlock)(NSURLResponse * _Nonnull response);
  11. /**
  12. This is the protocol for downloader response modifier.
  13. We can use a block to specify the downloader response modifier. But Using protocol can make this extensible, and allow Swift user to use it easily instead of using `@convention(block)` to store a block into context options.
  14. */
  15. @protocol SDWebImageDownloaderResponseModifier <NSObject>
  16. /// Modify the original URL response and return a new response. You can use this to check MIME-Type, mock server response, etc.
  17. /// @param response The original URL response, note for HTTP request it's actually a `NSHTTPURLResponse` instance
  18. /// @note If nil is returned, the image download will marked as cancelled with error `SDWebImageErrorInvalidDownloadResponse`
  19. - (nullable NSURLResponse *)modifiedResponseWithResponse:(nonnull NSURLResponse *)response;
  20. @end
  21. /**
  22. A downloader response modifier class with block.
  23. */
  24. @interface SDWebImageDownloaderResponseModifier : NSObject <SDWebImageDownloaderResponseModifier>
  25. /// Create the response modifier with block
  26. /// @param block A block to control modifier logic
  27. - (nonnull instancetype)initWithBlock:(nonnull SDWebImageDownloaderResponseModifierBlock)block;
  28. /// Create the response modifier with block
  29. /// @param block A block to control modifier logic
  30. + (nonnull instancetype)responseModifierWithBlock:(nonnull SDWebImageDownloaderResponseModifierBlock)block;
  31. @end
  32. /**
  33. A convenient response modifier to provide the HTTP response including HTTP Status Code, Version and Headers.
  34. */
  35. @interface SDWebImageDownloaderResponseModifier (Conveniences)
  36. /// Create the response modifier with HTTP Status code.
  37. /// @param statusCode HTTP Status Code.
  38. /// @note This is for convenience, if you need code to control the logic, use block API instead.
  39. - (nonnull instancetype)initWithStatusCode:(NSInteger)statusCode;
  40. /// Create the response modifier with HTTP Version. Status code defaults to 200.
  41. /// @param version HTTP Version, nil means "HTTP/1.1".
  42. /// @note This is for convenience, if you need code to control the logic, use block API instead.
  43. - (nonnull instancetype)initWithVersion:(nullable NSString *)version;
  44. /// Create the response modifier with HTTP Headers. Status code defaults to 200.
  45. /// @param headers HTTP Headers. Case insensitive according to HTTP/1.1(HTTP/2) standard. The headers will override the same fields from original response.
  46. /// @note This is for convenience, if you need code to control the logic, use block API instead.
  47. - (nonnull instancetype)initWithHeaders:(nullable NSDictionary<NSString *, NSString *> *)headers;
  48. /// Create the response modifier with HTTP Status Code, Version and Headers.
  49. /// @param statusCode HTTP Status Code.
  50. /// @param version HTTP Version, nil means "HTTP/1.1".
  51. /// @param headers HTTP Headers. Case insensitive according to HTTP/1.1(HTTP/2) standard. The headers will override the same fields from original response.
  52. /// @note This is for convenience, if you need code to control the logic, use block API instead.
  53. - (nonnull instancetype)initWithStatusCode:(NSInteger)statusCode version:(nullable NSString *)version headers:(nullable NSDictionary<NSString *, NSString *> *)headers;
  54. @end