From 8e9c14455c7b9a92403e9081c9a014ed871135a5 Mon Sep 17 00:00:00 2001 From: Petros Date: Wed, 30 Sep 2020 18:44:29 +0300 Subject: [PATCH 1/4] Regex ignore url implementation --- netfox/Core/NFX.swift | 9 +++++++++ netfox/Core/NFXProtocol.swift | 19 +++++++++++++++++++ netfox_ios_demo/AppDelegate.swift | 2 +- 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/netfox/Core/NFX.swift b/netfox/Core/NFX.swift index e02c65b2..64ee0629 100755 --- a/netfox/Core/NFX.swift +++ b/netfox/Core/NFX.swift @@ -48,6 +48,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() @@ -166,6 +167,10 @@ open class NFX: NSObject { ignoredURLs.append(url) } + @objc open func ignoreURLsWithRegex(_ regex: String) { + ignoredURLsRegex = NSRegularExpression(regex) + } + internal func getLastVisitDate() -> Date { return lastVisitDate } @@ -215,6 +220,10 @@ open class NFX: NSObject { return ignoredURLs } + func getIgnoredURLsRegex() -> NSRegularExpression { + return ignoredURLsRegex + } + func getSelectedGesture() -> ENFXGesture { return selectedGesture } diff --git a/netfox/Core/NFXProtocol.swift b/netfox/Core/NFXProtocol.swift index 55ff91ec..574c965e 100755 --- a/netfox/Core/NFXProtocol.swift +++ b/netfox/Core/NFXProtocol.swift @@ -44,6 +44,10 @@ open class NFXProtocol: URLProtocol { return false } + guard !NFX.sharedInstance().getIgnoredURLsRegex().matches(url.absoluteString) else { + return false + } + return true } @@ -66,6 +70,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.utf16.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) diff --git a/netfox_ios_demo/AppDelegate.swift b/netfox_ios_demo/AppDelegate.swift index cd494638..2b705bfc 100644 --- a/netfox_ios_demo/AppDelegate.swift +++ b/netfox_ios_demo/AppDelegate.swift @@ -16,7 +16,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate { func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { // Override point for customization after application launch. - + // NFX.sharedInstance().ignoreURLsWithRegex(".io") NFX.sharedInstance().start() return true From 52e5f79a6c0afb8c810c3983343294fd1f2ad2bd Mon Sep 17 00:00:00 2001 From: Petros Date: Wed, 30 Sep 2020 18:55:25 +0300 Subject: [PATCH 2/4] fixed matches function in regex --- netfox/Core/NFXProtocol.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/netfox/Core/NFXProtocol.swift b/netfox/Core/NFXProtocol.swift index 5588ade8..5e28799d 100755 --- a/netfox/Core/NFXProtocol.swift +++ b/netfox/Core/NFXProtocol.swift @@ -80,7 +80,7 @@ extension NSRegularExpression { } func matches(_ string: String) -> Bool { - let range = NSRange(location: 0, length: string.utf16.count) + let range = NSRange(location: 0, length: string.count) return firstMatch(in: string, options: [], range: range) != nil } } From 010c63c15882d1e91c20682150d671e20bf48912 Mon Sep 17 00:00:00 2001 From: Petros Date: Thu, 1 Oct 2020 13:44:35 +0300 Subject: [PATCH 3/4] - Added array for regex ignore urls --- netfox/Core/NFX.swift | 6 +++--- netfox/Core/NFXProtocol.swift | 4 +++- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/netfox/Core/NFX.swift b/netfox/Core/NFX.swift index 94936ce7..bb3b0b40 100755 --- a/netfox/Core/NFX.swift +++ b/netfox/Core/NFX.swift @@ -49,7 +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 ignoredURLsRegex = [NSRegularExpression()] fileprivate var filters = [Bool]() fileprivate var lastVisitDate: Date = Date() @@ -175,7 +175,7 @@ open class NFX: NSObject { } @objc open func ignoreURLsWithRegex(_ regex: String) { - ignoredURLsRegex = NSRegularExpression(regex) + ignoredURLsRegex.append(NSRegularExpression(regex)) } internal func getLastVisitDate() -> Date { @@ -227,7 +227,7 @@ open class NFX: NSObject { return ignoredURLs } - func getIgnoredURLsRegex() -> NSRegularExpression { + func getIgnoredURLsRegexes() -> [NSRegularExpression] { return ignoredURLsRegex } diff --git a/netfox/Core/NFXProtocol.swift b/netfox/Core/NFXProtocol.swift index 5e28799d..1c6f9e1e 100755 --- a/netfox/Core/NFXProtocol.swift +++ b/netfox/Core/NFXProtocol.swift @@ -44,7 +44,9 @@ open class NFXProtocol: URLProtocol { return false } - guard !NFX.sharedInstance().getIgnoredURLsRegex().matches(url.absoluteString) else { + let regexMatches = NFX.sharedInstance().getIgnoredURLsRegexes().map({return $0.matches(url.absoluteString)}).reduce(false) {$0 || $1} + + guard !regexMatches else { return false } From be4fb5b0097ff660a22f2f4b3ee9e349bba39811 Mon Sep 17 00:00:00 2001 From: Viktor Gubriienko <814362+vGubriienko@users.noreply.github.com> Date: Mon, 22 Nov 2021 12:13:12 +0200 Subject: [PATCH 4/4] Added different URL ignore methods --- netfox/Core/NFX.swift | 10 +++++++++- netfox/Core/NFXProtocol.swift | 6 ++++-- netfox_ios_demo/AppDelegate.swift | 3 +-- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/netfox/Core/NFX.swift b/netfox/Core/NFX.swift index bb3b0b40..10d97d95 100755 --- a/netfox/Core/NFX.swift +++ b/netfox/Core/NFX.swift @@ -49,7 +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 ignoredURLsRegex = [NSRegularExpression]() fileprivate var filters = [Bool]() fileprivate var lastVisitDate: Date = Date() @@ -174,10 +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 } diff --git a/netfox/Core/NFXProtocol.swift b/netfox/Core/NFXProtocol.swift index 1c6f9e1e..0b96793a 100755 --- a/netfox/Core/NFXProtocol.swift +++ b/netfox/Core/NFXProtocol.swift @@ -44,9 +44,11 @@ open class NFXProtocol: URLProtocol { return false } - let regexMatches = NFX.sharedInstance().getIgnoredURLsRegexes().map({return $0.matches(url.absoluteString)}).reduce(false) {$0 || $1} + let regexMatches = NFX.sharedInstance().getIgnoredURLsRegexes() + .map({return $0.matches(url.absoluteString)}) + .reduce(false) {$0 || $1} - guard !regexMatches else { + guard regexMatches == false else { return false } diff --git a/netfox_ios_demo/AppDelegate.swift b/netfox_ios_demo/AppDelegate.swift index 2b705bfc..d6eb8231 100644 --- a/netfox_ios_demo/AppDelegate.swift +++ b/netfox_ios_demo/AppDelegate.swift @@ -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().ignoreURLsWithRegex(".io") + NFX.sharedInstance().start() return true