Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support multiple values for query params #1130

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions include/pistache/http.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ namespace Pistache
std::initializer_list<std::pair<const std::string, std::string>> params);

void add(std::string name, std::string value);
void update(std::string& name, std::string value);
std::optional<std::string> get(const std::string& name) const;
bool has(const std::string& name) const;
// Return empty string or "?key1=value1&key2=value2" if query exist
Expand Down Expand Up @@ -191,6 +192,7 @@ namespace Pistache
const std::string& resource() const;

const Uri::Query& query() const;
const std::vector<std::pair<std::string,std::string>>& rawParams() const;

/* @Investigate: this is disabled because of a lock in the shared_ptr /
weak_ptr implementation of libstdc++. Under contention, we experience a
Expand Down Expand Up @@ -224,6 +226,7 @@ namespace Pistache
Method method_;
std::string resource_;
Uri::Query query_;
std::vector<std::pair<std::string,std::string>> raw_params_;

#ifdef LIBSTDCPP_SMARTPTR_LOCK_FIXME
std::weak_ptr<Tcp::Peer> peer_;
Expand Down
23 changes: 22 additions & 1 deletion src/common/http.cc
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,12 @@ namespace Pistache::Http
auto c = cursor.current();
if (c == ' ')
{
request->raw_params_.emplace_back(std::make_pair<std::string,std::string>(std::string(key), std::string()));
request->query_.add(std::move(key), "");
}
else if (c == '&')
{
request->raw_params_.emplace_back(std::make_pair<std::string,std::string>(std::string(key), std::string()));
request->query_.add(std::move(key), "");
if (!cursor.advance(1))
return State::Again;
Expand All @@ -212,7 +214,16 @@ namespace Pistache::Http
return State::Again;

std::string value = valueToken.text();
request->query_.add(std::move(key), std::move(value));
request->raw_params_.emplace_back(std::make_pair<std::string,std::string>(std::string(key), std::string(value)));
if (!request->query_.has(key)) {
request->query_.add(std::move(key), std::move(value));
} else {
// If an existing param, append it to the current value seperated by ','
// so that down stream the values will be converted to std::vector of multiple values.
std::string newValue = request->query_.get(key).value();
newValue += "," + value;
request->query_.update(key, std::move(newValue));
}
if (cursor.current() == '&')
{
if (!cursor.advance(1))
Expand Down Expand Up @@ -588,6 +599,11 @@ namespace Pistache::Http
{
params.insert(std::make_pair(std::move(name), std::move(value)));
}

void
Query::update(std::string& name, std::string value) {
params[name] = std::move(value);
}

std::optional<std::string> Query::get(const std::string& name) const
{
Expand Down Expand Up @@ -644,6 +660,11 @@ namespace Pistache::Http
const std::string& Request::resource() const { return resource_; }

const Uri::Query& Request::query() const { return query_; }

const std::vector<std::pair<std::string,std::string>>&
Request::rawParams() const {
return raw_params_;
}

const Address& Request::address() const { return address_; }

Expand Down