mirror of
https://github.com/KingDuckZ/kamokan.git
synced 2024-12-27 21:35:41 +00:00
Add more logging and error checking.
This commit is contained in:
parent
60d9641538
commit
e2437a6b12
4 changed files with 53 additions and 22 deletions
|
@ -146,20 +146,27 @@ int main (int parArgc, char* parArgv[], char* parEnvp[]) {
|
|||
|
||||
auto statuslog = setup_logging(*settings);
|
||||
SPDLOG_DEBUG(statuslog, "tawashi started");
|
||||
statuslog->info("Loaded config: \"{}\"", config_file_path());
|
||||
|
||||
auto cgi_env = SafeStackObject<tawashi::cgi::Env>(parEnvp, settings->at("host_path"));
|
||||
tawashi::ResponseFactory resp_factory(settings, cgi_env);
|
||||
SPDLOG_TRACE(statuslog, "Registering makers in the response factory");
|
||||
resp_factory.register_maker("index.cgi", &make_response<IndexResponse>);
|
||||
resp_factory.register_maker("", &make_response<IndexResponse>);
|
||||
resp_factory.register_maker("paste.cgi", &make_response<SubmitPasteResponse>);
|
||||
resp_factory.register_maker("error.cgi", &make_response<ErrorResponse>);
|
||||
resp_factory.register_jolly_maker(&make_response<PastieResponse>);
|
||||
|
||||
std::unique_ptr<Response> response = resp_factory.make_response(cgi_env->path_info());
|
||||
response->send();
|
||||
|
||||
SPDLOG_DEBUG(statuslog, "tawashi done, quitting");
|
||||
return 0;
|
||||
int retval = 0;
|
||||
try {
|
||||
statuslog->info("Loaded config: \"{}\"", config_file_path());
|
||||
|
||||
auto cgi_env = SafeStackObject<tawashi::cgi::Env>(parEnvp, settings->at("host_path"));
|
||||
tawashi::ResponseFactory resp_factory(settings, cgi_env);
|
||||
SPDLOG_TRACE(statuslog, "Registering makers in the response factory");
|
||||
resp_factory.register_maker("index.cgi", &make_response<IndexResponse>);
|
||||
resp_factory.register_maker("", &make_response<IndexResponse>);
|
||||
resp_factory.register_maker("paste.cgi", &make_response<SubmitPasteResponse>);
|
||||
resp_factory.register_maker("error.cgi", &make_response<ErrorResponse>);
|
||||
resp_factory.register_jolly_maker(&make_response<PastieResponse>);
|
||||
|
||||
std::unique_ptr<Response> response = resp_factory.make_response(cgi_env->path_info());
|
||||
response->send();
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
statuslog->critical("Uncaught exception in main(): \"{}\"", e.what());
|
||||
retval = 1;
|
||||
}
|
||||
|
||||
SPDLOG_DEBUG(statuslog, "tawashi done, quitting with {}", retval);
|
||||
return retval;
|
||||
}
|
||||
|
|
|
@ -88,17 +88,36 @@ namespace cgi {
|
|||
m_skip_path_info(0)
|
||||
{
|
||||
const std::string& path = m_cgi_env[CGIVars::PATH_INFO];
|
||||
assert(parBasePath.size() <= path.size());
|
||||
if (parBasePath.empty()) {
|
||||
spdlog::get("statuslog")->error("Received base path is empty - this will likely cause Tawashi to malfunction");
|
||||
}
|
||||
else if (parBasePath.size() > path.size()) {
|
||||
spdlog::get("statuslog")->error(
|
||||
"Received base path \"{}\" has size {}, which is longer than PATH_INFO \"{}\" size {}",
|
||||
std::string(parBasePath.begin(), parBasePath.end()),
|
||||
parBasePath.size(),
|
||||
path,
|
||||
path.size()
|
||||
);
|
||||
}
|
||||
else if (parBasePath[parBasePath.size() - 1] != '/') {
|
||||
spdlog::get("statuslog")->warn(
|
||||
"Received base path \"{}\" doesn't end with a '/' character",
|
||||
std::string(parBasePath.begin(), parBasePath.end())
|
||||
);
|
||||
}
|
||||
|
||||
if (boost::starts_with(path, parBasePath)) {
|
||||
m_skip_path_info = parBasePath.size();
|
||||
}
|
||||
else {
|
||||
auto statuslog = spdlog::get("statuslog");
|
||||
assert(statuslog);
|
||||
std::string str_base_path(parBasePath.begin(), parBasePath.end());
|
||||
std::string str_path(path.begin(), path.end());
|
||||
statuslog->error("base path is not a prefix of PATH_INFO: base path={}, PATH_INFO={}, tawashi will most likely malfunction", str_base_path, str_path);
|
||||
spdlog::get("statuslog")->error(
|
||||
"base path is not a prefix of PATH_INFO: base path={}, PATH_INFO={}, tawashi will most likely malfunction",
|
||||
str_base_path,
|
||||
str_path
|
||||
);
|
||||
m_skip_path_info = 1; //try with the default, maybe the user is lucky and it will work (ie base path = /)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -80,6 +80,7 @@ namespace tawashi {
|
|||
|
||||
std::string processed_pastie;
|
||||
if (m_syntax_highlight) {
|
||||
//TODO: redirect to "pastie not found" if !pastie
|
||||
processed_pastie = std::move(*pastie);
|
||||
}
|
||||
else {
|
||||
|
|
|
@ -20,6 +20,7 @@
|
|||
#include "cgi_env.hpp"
|
||||
#include <functional>
|
||||
#include <boost/container/flat_map.hpp>
|
||||
#include <spdlog/spdlog.h>
|
||||
|
||||
namespace tawashi {
|
||||
namespace {
|
||||
|
@ -42,17 +43,20 @@ namespace tawashi {
|
|||
ResponseFactory::~ResponseFactory() noexcept = default;
|
||||
|
||||
std::unique_ptr<Response> ResponseFactory::make_response (const boost::string_ref& parName) {
|
||||
//spdlog::get("statuslog")->info("making response object for \"{}\"", parName);
|
||||
std::string name(parName.data(), parName.size());
|
||||
spdlog::get("statuslog")->info("making response object for \"{}\"", name);
|
||||
|
||||
auto maker_it = m_local_data->makers.find(std::string(parName.data(), parName.size()));
|
||||
auto maker_it = m_local_data->makers.find(name);
|
||||
if (m_local_data->makers.end() != maker_it) {
|
||||
return maker_it->second(m_local_data->settings, m_local_data->cgi_env);
|
||||
}
|
||||
else if (m_local_data->jolly_maker) {
|
||||
spdlog::get("statuslog")->info("no exact match found for \"{}\", assuming it's a pastie's token", name);
|
||||
return m_local_data->jolly_maker(m_local_data->settings, m_local_data->cgi_env);
|
||||
}
|
||||
else {
|
||||
assert(false);
|
||||
spdlog::get("statuslog")->critical("no exact match found for \"{}\" and no jolly maker given, this should not happen", name);
|
||||
return std::unique_ptr<Response>();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue