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

changes required by new pistache version (commit d50d1c2 on 2022-07-28) #20

Draft
wants to merge 2 commits into
base: develop
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ find_package(nlohmann_json REQUIRED)
find_package(Pistache REQUIRED)
find_package(ers REQUIRED)

set(RESTCMD_DEPENDENCIES ${CETLIB} ${CETLIB_EXCEPT} ers::ers logging::logging cmdlib::cmdlib nlohmann_json::nlohmann_json pistache_shared)
set(RESTCMD_DEPENDENCIES ${CETLIB} ${CETLIB_EXCEPT} ers::ers logging::logging cmdlib::cmdlib nlohmann_json::nlohmann_json pistache)

##############################################################################
# Main library
Expand Down
6 changes: 3 additions & 3 deletions include/RestEndpoint.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class RestEndpoint {
, http_endpoint_{ std::make_shared<Pistache::Http::Endpoint>( address_ ) }
, description_{ "DUNE DAQ cmdlib API", "0.1" }
, accepted_mime_{ MIME(Application, Json) }
, http_client_{ std::make_shared<Pistache::Http::Client>() }
, http_client_{ std::make_shared<Pistache::Http::Experimental::Client>() }
, command_callback_{ callback }
{ }

Expand Down Expand Up @@ -73,8 +73,8 @@ class RestEndpoint {
Pistache::Http::Mime::MediaType accepted_mime_;

// CLIENT
std::shared_ptr<Pistache::Http::Client> http_client_;
Pistache::Http::Client::Options http_client_options_;
std::shared_ptr<Pistache::Http::Experimental::Client> http_client_;
Pistache::Http::Experimental::Client::Options http_client_options_;
std::vector<Pistache::Async::Promise<Pistache::Http::Response>> http_client_responses_;

// Function to call with received POST bodies
Expand Down
15 changes: 8 additions & 7 deletions src/RestEndpoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ void RestEndpoint::init(size_t threads)

http_endpoint_->init(opts);
createRouting();
http_client_options_ = Http::Client::options().threads(static_cast<int>(threads));
http_client_options_ = Http::Experimental::Client::options().threads(static_cast<int>(threads));
http_client_->init(http_client_options_);
}

Expand Down Expand Up @@ -66,10 +66,9 @@ inline void extendHeader(Http::Header::Collection& headers)
inline
std::string
getClientAddress(const Pistache::Rest::Request &request) {
const auto xff = request.headers().tryGetRaw("X-Forwarded-For");
if (!xff.isEmpty()) {
if (const auto xff = request.headers().tryGetRaw("X-Forwarded-For")){
//TODO: Strip of after first comma (to handle chained proxies).
return xff.get().value();
return xff->value();
}
return request.address().host();
}
Expand All @@ -84,9 +83,11 @@ void RestEndpoint::handle_route_command(const Rest::Request& request, Http::Resp
auto res = response.send(Http::Code::Not_Acceptable, "Not a JSON command!\n");
} else {
auto ansport = headers.getRaw("X-Answer-Port"); // RS: FIXME reply using headers
auto anshost = headers.tryGetRaw("X-Answer-Host"); // RS: FIXME reply using headers
meta.data["ans-port"] = ansport.value();
meta.data["ans-host"] = ( !anshost.isEmpty() ? anshost.get().value() : addr.host() );
if (auto anshost = headers.tryGetRaw("X-Answer-Host")) // RS: FIXME reply using headers
meta.data["ans-host"] = anshost->value();
else
meta.data["ans-host"] = addr.host();
command_callback_(nlohmann::json::parse(request.body()), meta); // RS: FIXME parse errors
auto res = response.send(Http::Code::Accepted, "Command received\n");
}
Expand All @@ -113,7 +114,7 @@ void RestEndpoint::handleResponseCommand(const cmdobj_t& cmd, dunedaq::cmdlib::c
std::rethrow_exception(exc);
}
catch (const std::exception &e) {
TLOG() << "Exception thrown by Http::Client::post() call: \"" << e.what() << "\"; errno = " << errno;
TLOG() << "Exception thrown by Http::Experimental::Client::post() call: \"" << e.what() << "\"; errno = " << errno;
}
}
);
Expand Down