Skip to content

Commit

Permalink
Added an intermediate base class for the CPP-HTTPLIB-based REST services
Browse files Browse the repository at this point in the history
  • Loading branch information
iagaponenko committed Jul 5, 2024
1 parent 3102395 commit d5d6f66
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/http/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ add_library(http SHARED)
target_sources(http PRIVATE
AsyncReq.cc
BinaryEncoding.cc
ChttpModule.cc
Client.cc
ClientConnPool.cc
ClientConfig.cc
Expand All @@ -25,6 +26,7 @@ target_link_libraries(http PUBLIC
Boost::filesystem
Boost::regex
Boost::system
cpp-httplib
)

install(TARGETS http)
Expand Down
69 changes: 69 additions & 0 deletions src/http/ChttpModule.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* LSST Data Management System
*
* This product includes software developed by the
* LSST Project (http://www.lsst.org/).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the LSST License Statement and
* the GNU General Public License along with this program. If not,
* see <http://www.lsstcorp.org/LegalNotices/>.
*/

// Class header
#include "http/ChttpModule.h"

// System headers
#include <iterator>

// Third-party headers
#include <httplib.h>

// Qserv headers
#include "http/RequestQuery.h"

using namespace std;

namespace lsst::qserv::http {

ChttpModule::ChttpModule(string const& authKey, string const& adminAuthKey, httplib::Request const& req,
httplib::Response& resp)
: Module(authKey, adminAuthKey), _req(req), _resp(resp) {}

string ChttpModule::method() const { return _req.method; }

unordered_map<string, string> ChttpModule::params() const { return _req.path_params; }

RequestQuery ChttpModule::query() const {
// TODO: The query parameters in CPP-HTTPLIB are stored in the std::multimap
// container to allow accumulating values of non-unique keys. For now we need
// to convert the multimap to the std::unordered_map container. This may result
// in losing some query parameters if they have the same key but different values.
// Though, the correct solution is to fix the QHTTP library to support
// the std::multimap container for query parameters.
unordered_map<string, string> queryParams;
for (auto const& [key, value] : _req.params) queryParams[key] = value;
return RequestQuery(queryParams);
}

void ChttpModule::getRequestBody(string& content, string const& requiredContentType) {
auto itr = _req.headers.find("Content-Type");
if (itr != _req.headers.end() && itr->second == requiredContentType) {
content = _req.body;
}
}

void ChttpModule::sendResponse(string const& content, string const& contentType) {
_resp.set_content(content, contentType);
}

} // namespace lsst::qserv::http
85 changes: 85 additions & 0 deletions src/http/ChttpModule.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@

/*
* LSST Data Management System
*
* This product includes software developed by the
* LSST Project (http://www.lsst.org/).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the LSST License Statement and
* the GNU General Public License along with this program. If not,
* see <http://www.lsstcorp.org/LegalNotices/>.
*/
#ifndef LSST_QSERV_HTTP_CHTTPMODULE_H
#define LSST_QSERV_HTTP_CHTTPMODULE_H

// System headers
#include <string>
#include <unordered_map>

// Qserv headers
#include "http/Module.h"

// Forward declarations

namespace httplib {
class Request;
class Response;
} // namespace httplib

namespace lsst::qserv::http {
class RequestBodyJSON;
class RequestQuery;
} // namespace lsst::qserv::http

// This header declarations
namespace lsst::qserv::http {

/**
* Class ChttpModule is an extended base class specialized for constructing
* the CPP-HTTPLIB request processing modules.
*/
class ChttpModule : public Module {
public:
ChttpModule() = delete;
ChttpModule(ChttpModule const&) = delete;
ChttpModule& operator=(ChttpModule const&) = delete;

virtual ~ChttpModule() = default;

protected:
/**
* @param authKey An authorization key for operations which require extra security.
* @param adminAuthKey An administrator-level authorization key.
* @param req The HTTP request.
* @param resp The HTTP response channel.
*/
ChttpModule(std::string const& authKey, std::string const& adminAuthKey, httplib::Request const& req,
httplib::Response& resp);

httplib::Request const& req() { return _req; }
httplib::Response& resp() { return _resp; }

virtual std::string method() const;
virtual std::unordered_map<std::string, std::string> params() const;
virtual RequestQuery query() const;
virtual void getRequestBody(std::string& content, std::string const& requiredContentType);
virtual void sendResponse(std::string const& content, std::string const& contentType);

private:
httplib::Request const& _req;
httplib::Response& _resp;
};

} // namespace lsst::qserv::http

#endif // LSST_QSERV_HTTP_CHTTPMODULE_H

0 comments on commit d5d6f66

Please sign in to comment.