Commit 5f37444e authored by David Van Cleve's avatar David Van Cleve Committed by Commit Bot

url: Add a StringPiece variant to PathForRequest

Some of the GURL methods have StringPiece variant helpers. This patch
adds a helper to GURL::PathForRequest.

Test: expands unittest
Change-Id: I488f5cf0e36861b4f7202719f3ab8997ce77c74a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1900356Reviewed-by: default avatarCharlie Harrison <csharrison@chromium.org>
Commit-Queue: David Van Cleve <davidvc@chromium.org>
Cr-Commit-Position: refs/heads/master@{#713007}
parent 3f409f41
...@@ -396,14 +396,14 @@ std::string GURL::ExtractFileName() const { ...@@ -396,14 +396,14 @@ std::string GURL::ExtractFileName() const {
return ComponentString(file_component); return ComponentString(file_component);
} }
std::string GURL::PathForRequest() const { base::StringPiece GURL::PathForRequestPiece() const {
DCHECK(parsed_.path.len > 0) DCHECK(parsed_.path.len > 0)
<< "Canonical path for requests should be non-empty"; << "Canonical path for requests should be non-empty";
if (parsed_.ref.len >= 0) { if (parsed_.ref.len >= 0) {
// Clip off the reference when it exists. The reference starts after the // Clip off the reference when it exists. The reference starts after the
// #-sign, so we have to subtract one to also remove it. // #-sign, so we have to subtract one to also remove it.
return std::string(spec_, parsed_.path.begin, return base::StringPiece(&spec_[parsed_.path.begin],
parsed_.ref.begin - parsed_.path.begin - 1); parsed_.ref.begin - parsed_.path.begin - 1);
} }
// Compute the actual path length, rather than depending on the spec's // Compute the actual path length, rather than depending on the spec's
// terminator. If we're an inner_url, our spec continues on into our outer // terminator. If we're an inner_url, our spec continues on into our outer
...@@ -412,7 +412,11 @@ std::string GURL::PathForRequest() const { ...@@ -412,7 +412,11 @@ std::string GURL::PathForRequest() const {
if (parsed_.query.is_valid()) if (parsed_.query.is_valid())
path_len = parsed_.query.end() - parsed_.path.begin; path_len = parsed_.query.end() - parsed_.path.begin;
return std::string(spec_, parsed_.path.begin, path_len); return base::StringPiece(&spec_[parsed_.path.begin], path_len);
}
std::string GURL::PathForRequest() const {
return PathForRequestPiece().as_string();
} }
std::string GURL::HostNoBrackets() const { std::string GURL::HostNoBrackets() const {
......
...@@ -386,6 +386,9 @@ class COMPONENT_EXPORT(URL) GURL { ...@@ -386,6 +386,9 @@ class COMPONENT_EXPORT(URL) GURL {
// parameter, and query portions of the URL. It is guaranteed to be ASCII. // parameter, and query portions of the URL. It is guaranteed to be ASCII.
std::string PathForRequest() const; std::string PathForRequest() const;
// Returns the same characters as PathForRequest(), avoiding a copy.
base::StringPiece PathForRequestPiece() const;
// Returns the host, excluding the square brackets surrounding IPv6 address // Returns the host, excluding the square brackets surrounding IPv6 address
// literals. This can be useful for passing to getaddrinfo(). // literals. This can be useful for passing to getaddrinfo().
std::string HostNoBrackets() const; std::string HostNoBrackets() const;
......
...@@ -539,11 +539,14 @@ TEST(GURLTest, PathForRequest) { ...@@ -539,11 +539,14 @@ TEST(GURLTest, PathForRequest) {
for (size_t i = 0; i < base::size(cases); i++) { for (size_t i = 0; i < base::size(cases); i++) {
GURL url(cases[i].input); GURL url(cases[i].input);
std::string path_request = url.PathForRequest(); EXPECT_EQ(cases[i].expected, url.PathForRequest());
EXPECT_EQ(cases[i].expected, path_request); EXPECT_EQ(cases[i].expected, url.PathForRequestPiece());
EXPECT_EQ(cases[i].inner_expected == NULL, url.inner_url() == NULL); EXPECT_EQ(cases[i].inner_expected == NULL, url.inner_url() == NULL);
if (url.inner_url() && cases[i].inner_expected) if (url.inner_url() && cases[i].inner_expected) {
EXPECT_EQ(cases[i].inner_expected, url.inner_url()->PathForRequest()); EXPECT_EQ(cases[i].inner_expected, url.inner_url()->PathForRequest());
EXPECT_EQ(cases[i].inner_expected,
url.inner_url()->PathForRequestPiece());
}
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment