diff options
| author | Amlal El Mahrouss <amlal@nekernel.org> | 2025-09-04 11:25:04 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-09-04 11:25:04 +0200 |
| commit | 6789dd7d88a192e3f55b95798cb393e7d12f368a (patch) | |
| tree | 7f04815ad5214f97d0fb2becceceed7ce8089b3d /dev/lib/utility/cgi_writer.hpp | |
| parent | 443588a42fe9cf48b5f63184b94afe483cb0e761 (diff) | |
| parent | fda7082c54ad46a56ac885d4686b82bad8dbc7c9 (diff) | |
Merge pull request #4 from amlel-el-mahrouss/devv1.0.43
OCL — v1.0.43
Diffstat (limited to 'dev/lib/utility/cgi_writer.hpp')
| -rw-r--r-- | dev/lib/utility/cgi_writer.hpp | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/dev/lib/utility/cgi_writer.hpp b/dev/lib/utility/cgi_writer.hpp new file mode 100644 index 0000000..126b299 --- /dev/null +++ b/dev/lib/utility/cgi_writer.hpp @@ -0,0 +1,79 @@ +/* + * File: cgi_writer.hpp + * Author: Amlal El Mahrouss, + * Copyright 2023-2025, Amlal El Mahrouss. + */ + +#ifndef _OCL_CGI_WRITER_HPP +#define _OCL_CGI_WRITER_HPP + +#include <lib/io/print.hpp> +#include <lib/utility/chunk_string.hpp> +#include <sstream> +#include <format> + +namespace ocl +{ + namespace cgi + { + /// @brief CGI Writer class, writes to stdout; as CGI expects. + template <typename char_type = char> + class basic_writer + { + private: + basic_writer& eval_(const ocl::basic_chunk_string<char_type>& mime, const ocl::basic_chunk_string<char_type>& ss) noexcept + { + std::basic_stringstream<char_type> ss_out; + + ss_out << std::format("Content-Type: {}\r\n", mime.str()); + ss_out << std::format("Server: {}\r\n", "OCL-CGI/1.0"); + ss_out << std::format("Content-Length: {}\r\n\r\n", ss.str().size()); + ss_out << ss.str(); + + ocl::io::print(ss_out.str()); + + return *this; + } + + public: + explicit basic_writer() = default; + ~basic_writer() = default; + + basic_writer& operator=(const basic_writer&) = default; + basic_writer(const basic_writer&) = default; + + public: + friend void operator<<(basic_writer& self, const ocl::basic_chunk_string<char_type>& ss_in) + { + self = self.eval_("text/plain", ss_in); + } + + basic_writer& binary(const ocl::basic_chunk_string<char_type>& ss_in) + { + return this->eval_("application/octet-stream", ss_in); + } + + basic_writer& html(const ocl::basic_chunk_string<char_type>& ss_in) + { + return this->eval_("text/html", ss_in); + } + + basic_writer& xml(const ocl::basic_chunk_string<char_type>& ss_in) + { + return this->eval_("application/xml", ss_in); + } + + basic_writer& json(const ocl::basic_chunk_string<char_type>& ss_in) + { + return this->eval_("application/json", ss_in); + } + + basic_writer& js(const ocl::basic_chunk_string<char_type>& ss_in) + { + return this->eval_("text/javascript", ss_in); + } + }; + } // namespace cgi +} // namespace ocl + +#endif // ifndef _OCL_CGI_WRITER_HPP |
