Skip to content

Commit

Permalink
Merge pull request #217 from kasketis/regex_ignore_url_support
Browse files Browse the repository at this point in the history
Regex ignore url support
  • Loading branch information
vGubriienko authored Dec 6, 2021
2 parents 9ad20cd + be4fb5b commit cd73205
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
17 changes: 17 additions & 0 deletions netfox/Core/NFX.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ open class NFX: NSObject {
fileprivate var enabled: Bool = false
fileprivate var selectedGesture: ENFXGesture = .shake
fileprivate var ignoredURLs = [String]()
fileprivate var ignoredURLsRegex = [NSRegularExpression]()
fileprivate var filters = [Bool]()
fileprivate var lastVisitDate: Date = Date()

Expand Down Expand Up @@ -173,6 +174,18 @@ open class NFX: NSObject {
ignoredURLs.append(url)
}

@objc open func ignoreURLs(_ urls: [String]) {
ignoredURLs.append(contentsOf: urls)
}

@objc open func ignoreURLsWithRegex(_ regex: String) {
ignoredURLsRegex.append(NSRegularExpression(regex))
}

@objc open func ignoreURLsWithRegexes(_ regexes: [String]) {
ignoredURLsRegex.append(contentsOf: regexes.map { NSRegularExpression($0) })
}

internal func getLastVisitDate() -> Date {
return lastVisitDate
}
Expand Down Expand Up @@ -222,6 +235,10 @@ open class NFX: NSObject {
return ignoredURLs
}

func getIgnoredURLsRegexes() -> [NSRegularExpression] {
return ignoredURLsRegex
}

func getSelectedGesture() -> ENFXGesture {
return selectedGesture
}
Expand Down
23 changes: 23 additions & 0 deletions netfox/Core/NFXProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ open class NFXProtocol: URLProtocol {
return false
}

let regexMatches = NFX.sharedInstance().getIgnoredURLsRegexes()
.map({return $0.matches(url.absoluteString)})
.reduce(false) {$0 || $1}

guard regexMatches == false else {
return false
}

return true
}

Expand All @@ -66,6 +74,21 @@ open class NFXProtocol: URLProtocol {
}
}

extension NSRegularExpression {
convenience init(_ pattern: String) {
do {
try self.init(pattern: pattern)
} catch {
preconditionFailure("Illegal regular expression: \(pattern).")
}
}

func matches(_ string: String) -> Bool {
let range = NSRange(location: 0, length: string.count)
return firstMatch(in: string, options: [], range: range) != nil
}
}

extension NFXProtocol: URLSessionDataDelegate {
public func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) {
responseData?.append(data)
Expand Down
3 changes: 1 addition & 2 deletions netfox_ios_demo/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.


NFX.sharedInstance().start()

return true
Expand Down

0 comments on commit cd73205

Please sign in to comment.