RedirectHandler.swift 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. /// The ``ImageDownloadRedirectHandler`` is used to modify the request before redirection.
  28. ///
  29. /// This allows you to customize the image download request during redirection. You can make modifications for
  30. /// purposes such as adding an authentication token to the header, performing basic HTTP authentication, or URL
  31. /// mapping.
  32. ///
  33. /// Typically, you pass an ``ImageDownloadRedirectHandler`` as the associated value of
  34. /// ``KingfisherOptionsInfoItem/redirectHandler(_:)`` and use it as the `options` parameter in relevant methods.
  35. ///
  36. /// If you do not make any changes to the input `request` and return it as is, the downloading process will redirect
  37. /// using it.
  38. ///
  39. public protocol ImageDownloadRedirectHandler: Sendable {
  40. /// Called when a redirect is received and the downloader waiting for the request to continue the download task.
  41. ///
  42. /// - Parameters:
  43. /// - task: The current ``SessionDataTask`` that triggers this redirect.
  44. /// - response: The response received during redirection.
  45. /// - newRequest: The new request received from the URL session for redirection that can be modified.
  46. /// - Returns: The modified request.
  47. func handleHTTPRedirection(
  48. for task: SessionDataTask,
  49. response: HTTPURLResponse,
  50. newRequest: URLRequest
  51. ) async -> URLRequest?
  52. }
  53. /// A wrapper for creating an ``ImageDownloadRedirectHandler`` instance more easily.
  54. ///
  55. /// This type conforms to ``ImageDownloadRedirectHandler`` and wraps an image modification block.
  56. public struct AnyRedirectHandler: ImageDownloadRedirectHandler {
  57. let block: @Sendable (SessionDataTask, HTTPURLResponse, URLRequest, @escaping (URLRequest?) -> Void) -> Void
  58. public func handleHTTPRedirection(
  59. for task: SessionDataTask, response: HTTPURLResponse, newRequest: URLRequest
  60. ) async -> URLRequest? {
  61. return await withCheckedContinuation { continuation in
  62. block(task, response, newRequest, { urlRequest in
  63. continuation.resume(returning: urlRequest)
  64. })
  65. }
  66. }
  67. /// Creates a value of ``ImageDownloadRedirectHandler`` that executes the `modify` block.
  68. ///
  69. /// - Parameter handle: The block that modifies the request when a request modification task is triggered.
  70. public init(handle: @escaping @Sendable (SessionDataTask, HTTPURLResponse, URLRequest, @escaping (URLRequest?) -> Void) -> Void) {
  71. block = handle
  72. }
  73. }