From b0d0e894a4394eed25dbb5cead12edd08a510a92 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Sun, 24 Aug 2025 14:13:34 +0200 Subject: feat! use `chunk_string` in the CGI module. Add additional constructors for `chunk_string` Signed-off-by: Amlal El Mahrouss --- dev/lib/core/includes.hpp | 4 ---- dev/lib/except/error.hpp | 4 ++-- dev/lib/fix/parser.hpp | 2 ++ dev/lib/io/print.hpp | 1 - dev/lib/logic/math.hpp | 4 ++-- dev/lib/memory/tracked_ptr.hpp | 30 +++++++++++++++++++++--------- dev/lib/utility/cgi_writer.hpp | 36 ++++++++++++++++++++---------------- dev/lib/utility/chunk_string.hpp | 17 ++++++++++++++--- 8 files changed, 61 insertions(+), 37 deletions(-) (limited to 'dev/lib') diff --git a/dev/lib/core/includes.hpp b/dev/lib/core/includes.hpp index 4beec1c..3238342 100644 --- a/dev/lib/core/includes.hpp +++ b/dev/lib/core/includes.hpp @@ -12,7 +12,3 @@ #include #include #include - -#include -#include -#include diff --git a/dev/lib/except/error.hpp b/dev/lib/except/error.hpp index 486d2e6..862dc3e 100644 --- a/dev/lib/except/error.hpp +++ b/dev/lib/except/error.hpp @@ -9,12 +9,12 @@ #include -namespace snu::except +namespace snu { using runtime_error = std::runtime_error; using fix_error = runtime_error; using math_error = runtime_error; using cgi_error = runtime_error; -} // namespace snu::except +} // namespace snu #endif // _SNU_ERR_HPP \ No newline at end of file diff --git a/dev/lib/fix/parser.hpp b/dev/lib/fix/parser.hpp index 8181359..1ace160 100644 --- a/dev/lib/fix/parser.hpp +++ b/dev/lib/fix/parser.hpp @@ -209,6 +209,8 @@ namespace snu::fix ::kill(::getpid(), SIGTRAP); } } + + using fix_tag_type = std::uint32_t; } // namespace snu::fix #endif // ifndef _SNU_FIX_PARSER_HPP diff --git a/dev/lib/io/print.hpp b/dev/lib/io/print.hpp index eb425c1..840b373 100644 --- a/dev/lib/io/print.hpp +++ b/dev/lib/io/print.hpp @@ -8,7 +8,6 @@ #ifndef _SNU_PRINT_HPP #define _SNU_PRINT_HPP -#include #include #include diff --git a/dev/lib/logic/math.hpp b/dev/lib/logic/math.hpp index cab995a..155251b 100644 --- a/dev/lib/logic/math.hpp +++ b/dev/lib/logic/math.hpp @@ -9,7 +9,7 @@ #include -namespace snu::math +namespace snu { template struct is_non_boolean_integer final @@ -32,4 +32,4 @@ namespace snu::math constexpr inline auto not_a_number = NAN; constexpr inline auto positive_infinity = INFINITY; constexpr inline auto negative_infinity = -positive_infinity; -} // namespace snu::math \ No newline at end of file +} // namespace snu \ No newline at end of file diff --git a/dev/lib/memory/tracked_ptr.hpp b/dev/lib/memory/tracked_ptr.hpp index da7e46c..7030f40 100644 --- a/dev/lib/memory/tracked_ptr.hpp +++ b/dev/lib/memory/tracked_ptr.hpp @@ -9,7 +9,7 @@ #include #include -#include +#include #include #include @@ -45,19 +45,24 @@ namespace snu::memory template void retain(T*& ptr, U&&... args) { - ptr = new T(args...); + ptr = new T(std::forward(args)...); if (ptr) { ++allocated_count_; + return; } - else - { - throw std::bad_alloc(); - } + + throw std::bad_alloc(); + } + + template + void must_retain(T*& ptr, U&&... args) noexcept + { + this->retain(ptr, args...); } - void dispose(T*& ptr) + void dispose(T*& ptr) noexcept { if (ptr) { @@ -96,10 +101,17 @@ namespace snu::memory { T* ptr = nullptr; allocator_.retain(ptr, std::forward(args)...); + return ptr; } - void dispose(T*& ptr) + template + T* must_retain(U&&... args) noexcept + { + return this->retain(std::forward(args)...); + } + + void dispose(T*& ptr) noexcept { allocator_.dispose(ptr); } @@ -190,7 +202,7 @@ namespace snu::memory } private: - T* ptr_ = nullptr; + T* ptr_{nullptr}; }; template diff --git a/dev/lib/utility/cgi_writer.hpp b/dev/lib/utility/cgi_writer.hpp index 69d0170..70b32cf 100644 --- a/dev/lib/utility/cgi_writer.hpp +++ b/dev/lib/utility/cgi_writer.hpp @@ -7,9 +7,10 @@ #ifndef _SNU_CGI_WRITER_HPP #define _SNU_CGI_WRITER_HPP -#include -#include +#include +#include #include +#include namespace snu { @@ -20,18 +21,16 @@ namespace snu class basic_writer { private: - basic_writer& eval_(const std::basic_string& mime, const std::basic_stringstream& ss) noexcept + basic_writer& eval_(const snu::basic_chunk_string& mime, const snu::basic_chunk_string& ss) noexcept { - std::printf("Content-Type: %s\r\n", mime.c_str()); - std::printf("Server: %s\r\n", "socl-cgi-system"); - std::printf("Content-Length: %ld\r\n\r\n", ss.str().size()); + std::basic_stringstream ss_out; - auto ss_cc = ss.str(); + ss_out << std::format("Content-Type: {}\r\n", mime.str()); + ss_out << std::format("Server: {}\r\n", "SOCL-CGI/1.0"); + ss_out << std::format("Content-Length: {}\r\n\r\n", ss.str().size()); + ss_out << ss.str(); - for (auto& ch : ss_cc) - { - std::printf("%c", ch); - } + snu::io::print(ss_out.str()); return *this; } @@ -44,27 +43,32 @@ namespace snu basic_writer(const basic_writer&) = default; public: - basic_writer& binary(const std::basic_stringstream& ss_in) + friend void operator<<(basic_writer& self, const snu::basic_chunk_string& ss_in) + { + self = self.eval_("text/plain", ss_in); + } + + basic_writer& binary(const snu::basic_chunk_string& ss_in) { return this->eval_("application/octet-stream", ss_in); } - basic_writer& html(const std::basic_stringstream& ss_in) + basic_writer& html(const snu::basic_chunk_string& ss_in) { return this->eval_("text/html", ss_in); } - basic_writer& xml(const std::basic_stringstream& ss_in) + basic_writer& xml(const snu::basic_chunk_string& ss_in) { return this->eval_("application/xml", ss_in); } - basic_writer& json(const std::basic_stringstream& ss_in) + basic_writer& json(const snu::basic_chunk_string& ss_in) { return this->eval_("application/json", ss_in); } - basic_writer& js(const std::basic_stringstream& ss_in) + basic_writer& js(const snu::basic_chunk_string& ss_in) { return this->eval_("text/javascript", ss_in); } diff --git a/dev/lib/utility/chunk_string.hpp b/dev/lib/utility/chunk_string.hpp index 6b060ae..79e0dd6 100644 --- a/dev/lib/utility/chunk_string.hpp +++ b/dev/lib/utility/chunk_string.hpp @@ -29,8 +29,19 @@ namespace snu constexpr const static auto max_chunk_size = 4096; public: - explicit basic_chunk_string() = default; - virtual ~basic_chunk_string() = default; + basic_chunk_string() = default; + + basic_chunk_string(const char_type* in) + { + this->operator+=(in); + } + + basic_chunk_string(const std::basic_string& in) + { + this->operator+=(in); + } + + ~basic_chunk_string() = default; basic_chunk_string& operator=(const basic_chunk_string&) = default; basic_chunk_string(const basic_chunk_string&) = default; @@ -56,7 +67,7 @@ namespace snu return *this; } - const std::basic_string& str() noexcept + const std::basic_string& str() const noexcept { return packed_chunks_; } -- cgit v1.2.3