RedirectHandler.swift 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. //
  2. // RedirectHandler.swift
  3. // Kingfisher
  4. //
  5. // Created by Roman Maidanovych on 2018/12/10.
  6. //
  7. // Copyright (c) 2019 Wei Wang <onevcat@gmail.com>
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining a copy
  10. // of this software and associated documentation files (the "Software"), to deal
  11. // in the Software without restriction, including without limitation the rights
  12. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. // copies of the Software, and to permit persons to whom the Software is
  14. // furnished to do so, subject to the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be included in
  17. // all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. // THE SOFTWARE.
  26. import Foundation
  27. /// Represents and wraps a method for modifying request during an image download request redirection.
  28. public protocol ImageDownloadRedirectHandler {
  29. /// The `ImageDownloadRedirectHandler` contained will be used to change the request before redirection.
  30. /// This is the posibility you can modify the image download request during redirection. You can modify the
  31. /// request for some customizing purpose, such as adding auth token to the header, do basic HTTP auth or
  32. /// something like url mapping.
  33. ///
  34. /// Usually, you pass an `ImageDownloadRedirectHandler` as the associated value of
  35. /// `KingfisherOptionsInfoItem.redirectHandler` and use it as the `options` parameter in related methods.
  36. ///
  37. /// If you do nothing with the input `request` and return it as is, a downloading process will redirect with it.
  38. ///
  39. /// - Parameters:
  40. /// - task: The current `SessionDataTask` which triggers this redirect.
  41. /// - response: The response received during redirection.
  42. /// - newRequest: The request for redirection which can be modified.
  43. /// - completionHandler: A closure for being called with modified request.
  44. func handleHTTPRedirection(
  45. for task: SessionDataTask,
  46. response: HTTPURLResponse,
  47. newRequest: URLRequest,
  48. completionHandler: @escaping (URLRequest?) -> Void)
  49. }
  50. /// A wrapper for creating an `ImageDownloadRedirectHandler` easier.
  51. /// This type conforms to `ImageDownloadRedirectHandler` and wraps an redirect request modify block.
  52. public struct AnyRedirectHandler: ImageDownloadRedirectHandler {
  53. let block: (SessionDataTask, HTTPURLResponse, URLRequest, (URLRequest?) -> Void) -> Void
  54. public func handleHTTPRedirection(
  55. for task: SessionDataTask,
  56. response: HTTPURLResponse,
  57. newRequest: URLRequest,
  58. completionHandler: @escaping (URLRequest?) -> Void)
  59. {
  60. block(task, response, newRequest, completionHandler)
  61. }
  62. /// Creates a value of `ImageDownloadRedirectHandler` which runs `modify` block.
  63. ///
  64. /// - Parameter modify: The request modifying block runs when a request modifying task comes.
  65. ///
  66. public init(handle: @escaping (SessionDataTask, HTTPURLResponse, URLRequest, (URLRequest?) -> Void) -> Void) {
  67. block = handle
  68. }
  69. }