Add CgIApplication class to main() function & fix bugs.
Some checks failed
buildbot/mdml-cgi-freebsd-jail-builder Build done.
buildbot/mdml-cgi-darwin-macos-builder Build done.
buildbot/mdml-cgi-linux-podman-builder Build done.

Close #22 - Fix mdml-cgi build so it uses the correct .cpp
implementation.
Close #21 - Fix issue where std::exception is used to create
mdml::exception in CgiApplication class when REQUEST_URI is missing
Close #20 - Implement CgiApplicaiton CGI request handler in mdml-cgi
This commit is contained in:
S David 2023-11-24 11:42:30 -05:00
parent 0b9fc10c6a
commit 4835883587
7 changed files with 61 additions and 46 deletions

View File

@ -1,5 +1,5 @@
set(MDML_CGI_SOURCES main.cpp)
set(MDML_CGI_SOURCES mdml.cpp)
add_executable(mdml-cgi ${MDML_CGI_SOURCES})
target_link_libraries(mdml-cgi PRIVATE mdml_static)

View File

@ -1,29 +0,0 @@
/* main.cpp
* Copyright © 2023 Saul D. Beniquez
* License: Mozilla Public License v. 2.0
*
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v.2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at https://mozilla.org/MPL/2.0/.
*/
#include "Application.hpp"
#include "types.hpp"
#include <iomanip>
#include <iostream>
#include <map>
#include <string>
int
main(int argc, const char** argv, const char** envp)
{
for (int i = 0; i < argc; ++i) {
std::cout << argv[i] << std::endl;
}
return 0;
}
// clang-format off
// vim: set foldmethod=marker foldmarker=#region,#endregion textwidth=80 ts=8 sts=0 sw=8 noexpandtab ft=cpp.doxygen :

View File

@ -7,14 +7,55 @@
* obtain one at https://mozilla.org/MPL/2.0/.
*/
#include "CgiApplication.hpp"
#include "libmdml.hpp"
#include "types.hpp"
int main(int argc, char *argv[])
#include <functional>
#include <iostream>
#include <optional>
#include <string>
namespace {
using string_result = mdml::Result<std::string>;
}
int
handle_error(
const string_result* error_data = nullptr,
const std::exception* except = nullptr
)
{
auto result = 0;
std::cerr << "An error occurred" << std::endl;
if (error_data && error_data->IsError) {
std::cerr << error_data->ErrorData << std::endl;
result |= 0x001;
}
if (except) {
std::cerr << except->what() << std::endl;
result |= 0x010;
}
return result;
}
int
main(int argc, const char* argv[], const char* envp[])
{
auto app = mdml::CgiApplication(argc, argv, envp);
try {
auto result = app.ProcessRequest();
if (result.IsError) {
return handle_error(&result);
}
} catch (std::exception& except) {
return handle_error(nullptr, &except);
}
return 0;
}
// clang-format off
// vim: set foldmethod=marker foldmarker=#region,#endregion textwidth=80 ts=8 sts=0 sw=8 noexpandtab ft=cpp.doxygen :

View File

@ -26,6 +26,8 @@ class exception : public std::exception {
public:
exception(c::const_string _message = default_error);
exception(const std::string& message);
exception(const mdml::exception& other) = default;
exception(const std::exception& inner);
exception& operator=(const exception&) = delete;

View File

@ -83,6 +83,8 @@ CgiApplication::ProcessRequest()
return { ERROR, buffer.str() };
// throw mdml::exception(buffer.str().c_str());
}
} catch (const mdml::exception& except) {
throw except;
} catch (const std::exception& except) {
throw mdml::exception(except);
}

View File

@ -116,9 +116,9 @@ generate_stacktrace(unsigned short framesToRemove)
buffer << std::endl;
}
std::free(strs);
#else
#else /*@{*/
buffer << boost::stacktrace::stacktrace() << std::flush;
#endif
#endif /*@}*/
return buffer.str();
}

View File

@ -22,7 +22,7 @@
using mdml::exception;
constexpr unsigned DEFAULT_STACKFRAMES_TO_STRIP = 2;
constexpr unsigned DEFAULT_STACKFRAMES_TO_STRIP = 3;
// Helper classes and functions. #region
@ -91,20 +91,19 @@ void
exception::build_what_message()
{
std::stringstream buffer;
this->what_message = error_message;
std::string indented_stacktrace =
prepend_tabs_to_lines(this->stack_trace);
buffer << "mdml::exception::what(): { " << std::endl;
buffer << "mdml::exception::what(): { " << std::endl
<< "\terror: " << error_message.c_str() << std::endl
<< "\tstack_trace: " << std::endl
<< indented_stacktrace << std::endl
<< "};" << std::endl;
buffer << " error: " << error_message << std::endl;
buffer << std::endl;
buffer << " stack_trace: " << std::endl
<< indented_stacktrace << std::endl;
buffer << "}; " << std::endl;
what_message = buffer.str();
this->what_message = buffer.str().c_str();
}
// clang-format off