-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added an intermediate base class for the CPP-HTTPLIB-based REST services
- Loading branch information
1 parent
8274c8c
commit 45a19e4
Showing
3 changed files
with
150 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* 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 { | ||
unordered_map<string, string> result; | ||
for (auto const& [key, value] : _req.params) result[key] = value; | ||
return result; | ||
} | ||
|
||
RequestQuery ChttpModule::query() const { return RequestQuery(_req.path_params); } | ||
|
||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |