Skip to content

Commit

Permalink
Fix brave/brave-ios#7873: Skip old reddit redirect when loading new r…
Browse files Browse the repository at this point in the history
…eddit media page (brave/brave-ios#7895)
  • Loading branch information
kylehickinson authored Aug 17, 2023
1 parent 1a51772 commit e206929
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
9 changes: 9 additions & 0 deletions Sources/Brave/WebFilters/WebsiteRedirects.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ struct WebsiteRedirects {
let hostToRedirectTo: String
/// What hosts should be redirected. Due to compat reasons not every host may be easily replaced.
let eligibleHosts: Set<String>
/// Any additional predicates on whether or not the URL should redirect (requires all to be satified)
///
/// It can be assumed that any URL passed into these predicates will be a eligible host
var additionalPredicates: [(URL) -> Bool] = []
/// What hosts should not be redirected. It's either due to web compat reasons or to let user explicitely type a url to not override it.
/// Reddit is good example, regular reddit.com and new.reddit.com point to the same new user interface.
/// So we redirect all regular reddit.com link, but the user may explicitely go to new.reddit.com without having to disable the reddit redirect toggle.
Expand All @@ -22,6 +26,10 @@ struct WebsiteRedirects {
private static let reddit = Rule(
hostToRedirectTo: "old.reddit.com",
eligibleHosts: ["reddit.com", "www.reddit.com", "np.reddit.com", "amp.reddit.com", "i.reddit.com"],
additionalPredicates: [ { url in
!url.path.hasPrefix("/media")
}
],
excludedHosts: ["new.reddit.com"])

private static let npr = Rule(
Expand Down Expand Up @@ -53,6 +61,7 @@ struct WebsiteRedirects {
.first(where: { $0.eligibleHosts.contains(host) })

guard let redirect = foundMatch,
redirect.additionalPredicates.allSatisfy({ $0(url) }),
var components = URLComponents(url: url, resolvingAgainstBaseURL: false) else { return nil }

// For privacy reasons we do not redirect websites if username or password are present.
Expand Down
5 changes: 5 additions & 0 deletions Tests/ClientTests/Web Filters/WebsiteRedirectsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ class WebsiteRedirectsTests: XCTestCase {

XCTAssertEqual(WebsiteRedirects.redirect(
for: try url("https://www.reddit.com/r/brave")), try url("https://old.reddit.com/r/brave"))

// Skip media redirect

XCTAssertNil(WebsiteRedirects.redirect(
for: try url("https://reddit.com/media?url=https%3A%2F%2Fi.redd.it%2Fimageid.jpg")))
}

func testNpr() throws {
Expand Down

0 comments on commit e206929

Please sign in to comment.