Skip to content

Commit

Permalink
Support multiple values for query params
Browse files Browse the repository at this point in the history
  • Loading branch information
skwijeratne committed Apr 25, 2023
1 parent 1c9d71a commit 0c3bc1f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ if(CMAKE_BUILD_TYPE MATCHES "Debug")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -O0 -fprofile-arcs -ftest-coverage")
endif()

option(PISTACHE_BUILD_TESTS "build tests alongside the project" OFF)
option(PISTACHE_BUILD_TESTS "build tests alongside the project" ON)
option(PISTACHE_ENABLE_NETWORK_TESTS "if tests are built, run ones needing network access" ON)
option(PISTACHE_BUILD_EXAMPLES "build examples alongside the project" OFF)
option(PISTACHE_BUILD_DOCS "build docs alongside the project" OFF)
Expand Down
3 changes: 3 additions & 0 deletions include/pistache/http.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ namespace Uri {
Query(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);
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 @@ -163,6 +164,7 @@ class Request : public Message {

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

const CookieJar& cookies() const;

Expand Down Expand Up @@ -198,6 +200,7 @@ class Request : public Message {
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
21 changes: 20 additions & 1 deletion src/common/http.cc
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,10 @@ namespace Private {

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 @@ -179,7 +181,14 @@ namespace Private {
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 {
std::string newValue = request->query_.get(key).get();
newValue += "," + value;
request->query_.update(key, std::move(newValue));
}
if (cursor.current() == '&') {
if (!cursor.advance(1)) return State::Again;
}
Expand Down Expand Up @@ -499,6 +508,11 @@ namespace Uri {
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);
}

Optional<std::string>
Query::get(const std::string& name) const {
auto it = params.find(name);
Expand Down Expand Up @@ -559,6 +573,11 @@ Request::query() const {
return query_;
}

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

const CookieJar&
Request::cookies() const {
return cookies_;
Expand Down

0 comments on commit 0c3bc1f

Please sign in to comment.