Skip to content

Commit 45f5574

Browse files
committed
Get latest page always from wayback machine
By deleting timestamp value from wayback query, we can get latest saved page. fix brave/brave-browser#14843
1 parent f1aa58e commit 45f5574

4 files changed

+35
-6
lines changed

components/brave_wayback_machine/brave_wayback_machine_utils.cc

+12
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,15 @@ bool IsWaybackMachineDisabledFor(const GURL& url) {
2828

2929
return false;
3030
}
31+
32+
GURL FixupWaybackQueryURL(const GURL& url) {
33+
// Get latest page always from wayback machine by invalidating timestamp
34+
// value in query string.
35+
GURL fixed_url = url;
36+
std::string unused;
37+
if (net::GetValueForKeyInQuery(fixed_url, "timestamp", &unused)) {
38+
fixed_url = net::AppendOrReplaceQueryParameter(fixed_url, "timestamp", "");
39+
}
40+
41+
return fixed_url;
42+
}

components/brave_wayback_machine/brave_wayback_machine_utils.h

+1
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@
99
class GURL;
1010

1111
bool IsWaybackMachineDisabledFor(const GURL& url);
12+
GURL FixupWaybackQueryURL(const GURL& url);
1213

1314
#endif // BRAVE_COMPONENTS_BRAVE_WAYBACK_MACHINE_BRAVE_WAYBACK_MACHINE_UTILS_H_

components/brave_wayback_machine/brave_wayback_machine_utils_unittest.cc

+17
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
* You can obtain one at http://mozilla.org/MPL/2.0/. */
55

66
#include "brave/components/brave_wayback_machine/brave_wayback_machine_utils.h"
7+
#include "brave/components/brave_wayback_machine/url_constants.h"
8+
#include "net/base/url_util.h"
79
#include "testing/gtest/include/gtest/gtest.h"
810
#include "url/gurl.h"
911

@@ -23,3 +25,18 @@ TEST(BraveWaybackMachineUtilsTest, LocalHostDisabledTest) {
2325
EXPECT_FALSE(
2426
IsWaybackMachineDisabledFor(GURL("https://archive.org/foobar.html")));
2527
}
28+
29+
TEST(BraveWaybackMachineUtilsTest, FixupQueryURLTest) {
30+
GURL wayback_fetch_url(std::string(kWaybackQueryURL) +
31+
"https://www.example.com?&timestamp=20160101");
32+
std::string timestamp_value;
33+
EXPECT_TRUE(net::GetValueForKeyInQuery(wayback_fetch_url, "timestamp",
34+
&timestamp_value));
35+
EXPECT_EQ("20160101", timestamp_value);
36+
37+
wayback_fetch_url = FixupWaybackQueryURL(wayback_fetch_url);
38+
EXPECT_TRUE(net::GetValueForKeyInQuery(wayback_fetch_url, "timestamp",
39+
&timestamp_value));
40+
// Check value is empty.
41+
EXPECT_EQ("", timestamp_value);
42+
}

components/brave_wayback_machine/wayback_machine_url_fetcher.cc

+5-6
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@
99

1010
#include "base/bind.h"
1111
#include "base/json/json_reader.h"
12+
#include "brave/components/brave_wayback_machine/brave_wayback_machine_utils.h"
1213
#include "brave/components/brave_wayback_machine/url_constants.h"
13-
#include "net/traffic_annotation/network_traffic_annotation.h"
1414
#include "net/base/load_flags.h"
15+
#include "net/traffic_annotation/network_traffic_annotation.h"
1516
#include "services/network/public/cpp/resource_request.h"
1617
#include "services/network/public/cpp/shared_url_loader_factory.h"
1718
#include "services/network/public/cpp/simple_url_loader.h"
@@ -52,14 +53,12 @@ WaybackMachineURLFetcher::WaybackMachineURLFetcher(
5253
url_loader_factory_(std::move(url_loader_factory)) {
5354
}
5455

55-
WaybackMachineURLFetcher::~WaybackMachineURLFetcher() {
56-
}
56+
WaybackMachineURLFetcher::~WaybackMachineURLFetcher() = default;
5757

5858
void WaybackMachineURLFetcher::Fetch(const GURL& url) {
5959
auto request = std::make_unique<network::ResourceRequest>();
60-
std::string wayback_fetch_url =
61-
std::string(kWaybackQueryURL) + url.spec();
62-
request->url = GURL(wayback_fetch_url);
60+
const GURL wayback_fetch_url(std::string(kWaybackQueryURL) + url.spec());
61+
request->url = FixupWaybackQueryURL(wayback_fetch_url);
6362
request->credentials_mode = network::mojom::CredentialsMode::kOmit;
6463
request->load_flags = net::LOAD_DO_NOT_SAVE_COOKIES;
6564
wayback_url_loader_ = network::SimpleURLLoader::Create(

0 commit comments

Comments
 (0)