Skip to content

Commit

Permalink
Merge pull request #65 from colinhumber/host-blacklist
Browse files Browse the repository at this point in the history
Added ability to blacklist hosts from being recorded
  • Loading branch information
pmusolino authored Sep 18, 2019
2 parents a40712c + 73592fe commit 315adb5
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
14 changes: 13 additions & 1 deletion Sources/CustomHTTPProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
import Foundation

public class CustomHTTPProtocol: URLProtocol {

static var blacklistedHosts = [String]()

struct Constants {
static let RequestHandledKey = "URLProtocolRequestHandled"
}
Expand All @@ -27,6 +28,8 @@ public class CustomHTTPProtocol: URLProtocol {
}

override public class func canInit(with request: URLRequest) -> Bool {
guard CustomHTTPProtocol.shouldHandleRequest(request) else { return false }

if CustomHTTPProtocol.property(forKey: Constants.RequestHandledKey, in: request) != nil {
return false
}
Expand All @@ -53,6 +56,7 @@ public class CustomHTTPProtocol: URLProtocol {
if let startDate = currentRequest?.date{
currentRequest?.duration = fabs(startDate.timeIntervalSinceNow) * 1000 //Find elapsed time and convert to milliseconds
}

Storage.shared.saveRequest(request: currentRequest)
session?.invalidateAndCancel()
}
Expand All @@ -70,6 +74,14 @@ public class CustomHTTPProtocol: URLProtocol {
return data as Data
}
}

/// Inspects the request to see if the host has not been blacklisted and can be handled by this URL protocol.
/// - Parameter request: The request being processed.
private class func shouldHandleRequest(_ request: URLRequest) -> Bool {
guard let host = request.url?.host else { return false }

return CustomHTTPProtocol.blacklistedHosts.filter({ host.hasSuffix($0) }).isEmpty
}

deinit {
session = nil
Expand Down
5 changes: 5 additions & 0 deletions Sources/Wormholy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ import UIKit

public class Wormholy: NSObject
{
@objc public static var blacklistedHosts: [String] {
get { return CustomHTTPProtocol.blacklistedHosts }
set { CustomHTTPProtocol.blacklistedHosts = newValue }
}

@objc public static func swiftyLoad() {
NotificationCenter.default.addObserver(forName: fireWormholy, object: nil, queue: nil) { (notification) in
Wormholy.presentWormholyFlow()
Expand Down
3 changes: 2 additions & 1 deletion WormholyDemo/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@

import UIKit
import Foundation

import Wormholy
class ViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()

if #available(iOS 10.0, *) {
let timer = Timer.scheduledTimer(withTimeInterval: 8, repeats: true) { (timer) in
DataFetcher.sharedInstance.getPost(id: Utils.random(max: 128), completion: {
Expand Down

0 comments on commit 315adb5

Please sign in to comment.