Skip to content

Commit

Permalink
make ParseURL getQuery helper static
Browse files Browse the repository at this point in the history
Summary:
1. The only callsite of this seemed to migrate to `HTTPMessage::getQueryParam`
2. I was looking to reuse this method, but it requires me to have a `ParsedURL` which is only buildable via URL parsing. This method doesn't really use internal state, so maybe it can just be a static helper.

See next diff for usage.

Reviewed By: lnicco

Differential Revision: D68833844

fbshipit-source-id: 990ff1b621e9b505a387b02c2d1a5669aa09625b
  • Loading branch information
pfarcasanu authored and facebook-github-bot committed Jan 30, 2025
1 parent 8d1533b commit 88adfe4
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
7 changes: 3 additions & 4 deletions proxygen/lib/utils/ParseURL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,10 +255,9 @@ void ParseURL::stripBrackets() noexcept {
}

folly::Optional<folly::StringPiece> ParseURL::getQueryParam(
folly::StringPiece name) const noexcept {
auto params = query_;
while (!params.empty()) {
auto param = params.split_step('&');
folly::StringPiece query, const folly::StringPiece name) noexcept {
while (!query.empty()) {
auto param = query.split_step('&');
if (!param.removePrefix(name)) {
continue;
}
Expand Down
4 changes: 2 additions & 2 deletions proxygen/lib/utils/ParseURL.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,8 @@ class ParseURL {

FB_EXPORT void stripBrackets() noexcept;

FOLLY_NODISCARD folly::Optional<folly::StringPiece> getQueryParam(
folly::StringPiece name) const noexcept;
FOLLY_NODISCARD static folly::Optional<folly::StringPiece> getQueryParam(
folly::StringPiece query, const folly::StringPiece name) noexcept;

private:
void moveHostAndAuthority(ParseURL&& goner) {
Expand Down
13 changes: 7 additions & 6 deletions proxygen/lib/utils/test/ParseURLTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -268,11 +268,12 @@ TEST(ParseURL, PortOverflow) {

TEST(ParseURL, GetQueryParam) {
auto u = ParseURL::parseURL("localhost/?foo=1&bar=2&baz&bazz=3&bak=");
auto q = u->query();
ASSERT_TRUE(u.hasValue());
EXPECT_EQ(u->getQueryParam("foo"), "1");
EXPECT_EQ(u->getQueryParam("bar"), "2");
EXPECT_EQ(u->getQueryParam("baz"), "");
EXPECT_EQ(u->getQueryParam("bazz"), "3");
EXPECT_EQ(u->getQueryParam("bak"), "");
EXPECT_FALSE(u->getQueryParam("fooo").has_value());
EXPECT_EQ(ParseURL::getQueryParam(q, "foo"), "1");
EXPECT_EQ(ParseURL::getQueryParam(q, "bar"), "2");
EXPECT_EQ(ParseURL::getQueryParam(q, "baz"), "");
EXPECT_EQ(ParseURL::getQueryParam(q, "bazz"), "3");
EXPECT_EQ(ParseURL::getQueryParam(q, "bak"), "");
EXPECT_FALSE(ParseURL::getQueryParam(q, "fooo").has_value());
}

0 comments on commit 88adfe4

Please sign in to comment.