Skip to content

Commit

Permalink
Merge pull request #7693 from brave/ipfs_keep_query_and_hash_param
Browse files Browse the repository at this point in the history
Only replace scheme, host, port, path when translating IPFS URI
  • Loading branch information
yrliou authored Jan 25, 2021
2 parents 3dc841e + 7a750e4 commit 28463f8
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 14 deletions.
14 changes: 7 additions & 7 deletions components/ipfs/translate_ipfs_uri.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ bool TranslateIPFSURI(const GURL& url, GURL* new_url, const GURL& gateway_url) {
// new_url would be:
// https://dweb.link/ipfs/[cid]//wiki/Vincent_van_Gogh.html
if (new_url) {
std::string gateway_empty_path = gateway_url.spec();
if (gateway_empty_path.size() > 0 &&
gateway_empty_path[gateway_empty_path.size() - 1] == '/') {
gateway_empty_path.pop_back();
}
*new_url = GURL(gateway_empty_path +
(ipfs_scheme ? "/ipfs/" : "/ipns/") + cid + path);
GURL::Replacements replacements;
replacements.SetSchemeStr(gateway_url.scheme_piece());
replacements.SetHostStr(gateway_url.host_piece());
replacements.SetPortStr(gateway_url.port_piece());
std::string new_path = (ipfs_scheme ? "ipfs/" : "ipns/") + cid + path;
replacements.SetPathStr(new_path);
*new_url = url.ReplaceComponents(replacements);
VLOG(1) << "[IPFS] " << __func__ << " new URL: " << *new_url;
}

Expand Down
86 changes: 79 additions & 7 deletions components/ipfs/translate_ipfs_uri_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

namespace ipfs {

class IPFSBraveContentBrowserClientTest : public testing::Test {
class TranslateIPFSURITest : public testing::Test {
protected:
void SetUp() override {
public_gateway_ = GURL(kDefaultIPFSGateway);
Expand All @@ -40,45 +40,45 @@ class IPFSBraveContentBrowserClientTest : public testing::Test {
GURL public_gateway_;
};

TEST_F(IPFSBraveContentBrowserClientTest, TranslateIPFSURINotIPFSScheme) {
TEST_F(TranslateIPFSURITest, TranslateIPFSURINotIPFSScheme) {
GURL url("http://a.com/ipfs/QmfM2r8seH2GiRaC4esTjeraXEachRt8ZsSeGaWTPLyMoG");
GURL new_url;
ASSERT_FALSE(ipfs::TranslateIPFSURI(url, &new_url, public_gateway()));
}

TEST_F(IPFSBraveContentBrowserClientTest, TranslateIPFSURIIPFSScheme) {
TEST_F(TranslateIPFSURITest, TranslateIPFSURIIPFSScheme) {
GURL url("ipfs://QmfM2r8seH2GiRaC4esTjeraXEachRt8ZsSeGaWTPLyMoG");
GURL new_url;
ASSERT_TRUE(ipfs::TranslateIPFSURI(url, &new_url, public_gateway()));
EXPECT_EQ(new_url, GURL("https://dweb.link/ipfs/"
"QmfM2r8seH2GiRaC4esTjeraXEachRt8ZsSeGaWTPLyMoG"));
}

TEST_F(IPFSBraveContentBrowserClientTest, TranslateIPFSURIIPNSScheme) {
TEST_F(TranslateIPFSURITest, TranslateIPFSURIIPNSScheme) {
GURL url("ipns://QmSrPmbaUKA3ZodhzPWZnpFgcPMFWF4QsxXbkWfEptTBJd");
GURL new_url;
ASSERT_TRUE(ipfs::TranslateIPFSURI(url, &new_url, public_gateway()));
EXPECT_EQ(new_url, GURL("https://dweb.link/ipns/"
"QmSrPmbaUKA3ZodhzPWZnpFgcPMFWF4QsxXbkWfEptTBJd"));
}

TEST_F(IPFSBraveContentBrowserClientTest, TranslateIPFSURIIPFSSchemeLocal) {
TEST_F(TranslateIPFSURITest, TranslateIPFSURIIPFSSchemeLocal) {
GURL url("ipfs://QmfM2r8seH2GiRaC4esTjeraXEachRt8ZsSeGaWTPLyMoG");
GURL new_url;
ASSERT_TRUE(ipfs::TranslateIPFSURI(url, &new_url, local_gateway()));
EXPECT_EQ(new_url, GURL("http://localhost:48080/ipfs/"
"QmfM2r8seH2GiRaC4esTjeraXEachRt8ZsSeGaWTPLyMoG"));
}

TEST_F(IPFSBraveContentBrowserClientTest, TranslateIPFSURIIPNSSchemeLocal) {
TEST_F(TranslateIPFSURITest, TranslateIPFSURIIPNSSchemeLocal) {
GURL url("ipns://QmSrPmbaUKA3ZodhzPWZnpFgcPMFWF4QsxXbkWfEptTBJd");
GURL new_url;
ASSERT_TRUE(ipfs::TranslateIPFSURI(url, &new_url, local_gateway()));
EXPECT_EQ(new_url, GURL("http://localhost:48080/ipns/"
"QmSrPmbaUKA3ZodhzPWZnpFgcPMFWF4QsxXbkWfEptTBJd"));
}

TEST_F(IPFSBraveContentBrowserClientTest, TranslateIPFSURIIPFSSchemeWithPath) {
TEST_F(TranslateIPFSURITest, TranslateIPFSURIIPFSSchemeWithPath) {
GURL url(
"ipfs://bafybeiemxf5abjwjbikoz4mc3a3dla6ual3jsgpdr4cjr3oz3evfyavhwq"
"/wiki/Vincent_van_Gogh.html");
Expand All @@ -90,4 +90,76 @@ TEST_F(IPFSBraveContentBrowserClientTest, TranslateIPFSURIIPFSSchemeWithPath) {
"/wiki/Vincent_van_Gogh.html"));
}

TEST_F(TranslateIPFSURITest, TranslateIPFSURIIPFSSchemeWithPathAndHash) {
GURL url(
"ipfs://bafybeiemxf5abjwjbikoz4mc3a3dla6ual3jsgpdr4cjr3oz3evfyavhwq"
"/wiki/Vincent_van_Gogh.html#Emerging_artist");
GURL new_url;
ASSERT_TRUE(ipfs::TranslateIPFSURI(url, &new_url, public_gateway()));
EXPECT_EQ(new_url,
GURL("https://dweb.link/ipfs/"
"bafybeiemxf5abjwjbikoz4mc3a3dla6ual3jsgpdr4cjr3oz3evfyavhwq"
"/wiki/Vincent_van_Gogh.html#Emerging_artist"));
}

TEST_F(TranslateIPFSURITest, TranslateIPFSURIIPFSSchemeLocalWithPathAndHash) {
GURL url(
"ipfs://bafybeiemxf5abjwjbikoz4mc3a3dla6ual3jsgpdr4cjr3oz3evfyavhwq"
"/wiki/Vincent_van_Gogh.html#Emerging_artist");
GURL new_url;
ASSERT_TRUE(ipfs::TranslateIPFSURI(url, &new_url, local_gateway()));
EXPECT_EQ(new_url,
GURL("http://localhost:48080/ipfs/"
"bafybeiemxf5abjwjbikoz4mc3a3dla6ual3jsgpdr4cjr3oz3evfyavhwq"
"/wiki/Vincent_van_Gogh.html#Emerging_artist"));
}

TEST_F(TranslateIPFSURITest, TranslateIPFSURIIPFSSchemeWithPathAndQuery) {
GURL url(
"ipfs://bafybeiemxf5abjwjbikoz4mc3a3dla6ual3jsgpdr4cjr3oz3evfyavhwq"
"/wiki/Vincent_van_Gogh.html?test=true");
GURL new_url;
ASSERT_TRUE(ipfs::TranslateIPFSURI(url, &new_url, public_gateway()));
EXPECT_EQ(new_url,
GURL("https://dweb.link/ipfs/"
"bafybeiemxf5abjwjbikoz4mc3a3dla6ual3jsgpdr4cjr3oz3evfyavhwq"
"/wiki/Vincent_van_Gogh.html?test=true"));
}

TEST_F(TranslateIPFSURITest, TranslateIPFSURIIPFSSchemeLocalWithPathAndQuery) {
GURL url(
"ipfs://bafybeiemxf5abjwjbikoz4mc3a3dla6ual3jsgpdr4cjr3oz3evfyavhwq"
"/wiki/Vincent_van_Gogh.html?test=true");
GURL new_url;
ASSERT_TRUE(ipfs::TranslateIPFSURI(url, &new_url, local_gateway()));
EXPECT_EQ(new_url,
GURL("http://localhost:48080/ipfs/"
"bafybeiemxf5abjwjbikoz4mc3a3dla6ual3jsgpdr4cjr3oz3evfyavhwq"
"/wiki/Vincent_van_Gogh.html?test=true"));
}

TEST_F(TranslateIPFSURITest, TranslateIPFSURIIPFSSchemeWithPathQueryHash) {
GURL url(
"ipfs://bafybeiemxf5abjwjbikoz4mc3a3dla6ual3jsgpdr4cjr3oz3evfyavhwq"
"/wiki/Vincent_van_Gogh.html?test=true#test");
GURL new_url;
ASSERT_TRUE(ipfs::TranslateIPFSURI(url, &new_url, public_gateway()));
EXPECT_EQ(new_url,
GURL("https://dweb.link/ipfs/"
"bafybeiemxf5abjwjbikoz4mc3a3dla6ual3jsgpdr4cjr3oz3evfyavhwq"
"/wiki/Vincent_van_Gogh.html?test=true#test"));
}

TEST_F(TranslateIPFSURITest, TranslateIPFSURIIPFSSchemeLocalWithPathQueryHash) {
GURL url(
"ipfs://bafybeiemxf5abjwjbikoz4mc3a3dla6ual3jsgpdr4cjr3oz3evfyavhwq"
"/wiki/Vincent_van_Gogh.html?test=true#test");
GURL new_url;
ASSERT_TRUE(ipfs::TranslateIPFSURI(url, &new_url, local_gateway()));
EXPECT_EQ(new_url,
GURL("http://localhost:48080/ipfs/"
"bafybeiemxf5abjwjbikoz4mc3a3dla6ual3jsgpdr4cjr3oz3evfyavhwq"
"/wiki/Vincent_van_Gogh.html?test=true#test"));
}

} // namespace ipfs

0 comments on commit 28463f8

Please sign in to comment.