<commit_before>//
//  ImageLoader.swift
//  ImageLoader
//
//  Created by <NAME> on 10/16/14.
//  Copyright © 2014 <NAME>. All rights reserved.
//

import Foundation
import UIKit

public struct ImageLoader {

    @discardableResult
    public static func request(with url: URLLiteralConvertible, onCompletion: @escaping (UIImage?, Error?, FetchOperation) -> Void) -> Loader {
        let task = Task(nil, onCompletion: onCompletion)
        let loader = ImageLoader.loaderManager.getLoader(with: url.imageLoaderURL, task: task)
        loader.resume()

        return loader
    }
}

extension ImageLoader {

    static let loaderManager = LoaderManager()
    static let sessionManager = SessionManager()

    class SessionManager: NSObject, URLSessionDataDelegate {

        func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) {
            guard let loader = getLoaderFromLoaderManager(with: dataTask) else { return }
            loader.operative.receiveData.append(data)
        }

        func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
            guard let loader = getLoaderFromLoaderManager(with: task) else { return }

            loader.complete(with: error)
        }

        func getLoaderFromLoaderManager(with dataTask: URLSessionTask) -> Loader? {
            guard let url = dataTask.currentRequest?.url else { return nil }

            return loaderManager.storage[url]
        }
    }

    class LoaderManager {

        let session: URLSession
        let storage = HashStorage<URL, Loader>()
        var disk = Disk()

        init(configuration: URLSessionConfiguration = .default) {
            self.session = URLSession(configuration: configuration, delegate: sessionManager, delegateQueue: nil)
        }<commit_msg>Bugfix: - don't allow on redirecting - dont't use original request on finding the loader from storage<commit_after>//
//  ImageLoader.swift
//  ImageLoader
//
//  Created by <NAME> on 10/16/14.
//  Copyright © 2014 <NAME>. All rights reserved.
//

import Foundation
import UIKit

public struct ImageLoader {

    @discardableResult
    public static func request(with url: URLLiteralConvertible, onCompletion: @escaping (UIImage?, Error?, FetchOperation) -> Void) -> Loader {
        let task = Task(nil, onCompletion: onCompletion)
        let loader = ImageLoader.loaderManager.getLoader(with: url.imageLoaderURL, task: task)
        loader.resume()

        return loader
    }
}

extension ImageLoader {

    static let loaderManager = LoaderManager()
    static let sessionManager = SessionManager()

    class SessionManager: NSObject, URLSessionDataDelegate {

        func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) {
            guard let loader = getLoaderFromLoaderManager(with: dataTask) else { return }
            loader.operative.receiveData.append(data)
        }

        func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive response: URLResponse, completionHandler: @escaping (URLSession.ResponseDisposition) -> Void) {
            completionHandler(.allow)
        }

        func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
            guard let loader = getLoaderFromLoaderManager(with: task) else { return }
            loader.complete(with: error)
        }

        func getLoaderFromLoaderManager(with dataTask: URLSessionTask) -> Loader? {
            guard let url = dataTask.originalRequest?.url else { return nil }
            return loaderManager.storage[url]
        }
    }

    class LoaderManager {

        let session: URLSession
        let storage = HashStorage<URL, Loader>()
        var disk = Disk()

        init(configuration: URLSessionConfiguration = .default) {
            self.session = URLSession(configuration: configuration, delegate: sessionManager, delegateQueue: nil)
        }
{'fraction_non_alphanumeric': 0.08709839357429719, 'fraction_numerical': 0.0050200803212851405, 'mean_word_length': 2.985, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7228825', 'n_tokens_mistral': 1064, 'n_tokens_neox': 1005, 'n_words': 298}