summaryrefslogtreecommitdiffhomepage
path: root/include/ocl/utility/cgi.hpp
diff options
context:
space:
mode:
authorAmlal El Mahrouss <amlal@nekernel.org>2025-11-23 20:16:02 -0500
committerGitHub <noreply@github.com>2025-11-23 20:16:02 -0500
commit85f89ee4bb137100cbeffcbc10168eb8ea52e6cc (patch)
treef6e2063319ceaaa02f523fb5c289e4f37411a2df /include/ocl/utility/cgi.hpp
parent9a70f32ddaec0eef99efbf7ff5597c2adf08f45a (diff)
parent65a8349aa5526d071b18cd4d42586c46faaa3823 (diff)
Merge pull request #18 from amlel-el-mahrouss/developv1.0.48
OCL v1.0.48
Diffstat (limited to 'include/ocl/utility/cgi.hpp')
-rw-r--r--include/ocl/utility/cgi.hpp79
1 files changed, 79 insertions, 0 deletions
diff --git a/include/ocl/utility/cgi.hpp b/include/ocl/utility/cgi.hpp
new file mode 100644
index 0000000..8849c2d
--- /dev/null
+++ b/include/ocl/utility/cgi.hpp
@@ -0,0 +1,79 @@
+/*
+ * File: cgi.hpp
+ * Author: Amlal El Mahrouss,
+ * Copyright 2023-2025, Amlal El Mahrouss, Licensed under the Boost Software License.
+ */
+
+#ifndef _OCL_CGI_HPP
+#define _OCL_CGI_HPP
+
+#include <io/print.hpp>
+#include <core/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 basic_chunk_string<char_type>& mime, const 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/1.0");
+ ss_out << std::format("Content-Length: {}\r\n\r\n", ss.str().size());
+ ss_out << ss.str();
+
+ io::print(ss_out.str());
+
+ return *this;
+ }
+
+ public:
+ explicit basic_writer() = default;
+ virtual ~basic_writer() = default;
+
+ basic_writer& operator=(const basic_writer&) = default;
+ basic_writer(const basic_writer&) = default;
+
+ public:
+ friend void operator<<(basic_writer& self, const basic_chunk_string<char_type>& ss_in)
+ {
+ self = self.eval_("text/plain", ss_in);
+ }
+
+ basic_writer& binary(const basic_chunk_string<char_type>& ss_in)
+ {
+ return this->eval_("application/octet-stream", ss_in);
+ }
+
+ basic_writer& html(const basic_chunk_string<char_type>& ss_in)
+ {
+ return this->eval_("text/html", ss_in);
+ }
+
+ basic_writer& xml(const basic_chunk_string<char_type>& ss_in)
+ {
+ return this->eval_("application/xml", ss_in);
+ }
+
+ basic_writer& json(const basic_chunk_string<char_type>& ss_in)
+ {
+ return this->eval_("application/json", ss_in);
+ }
+
+ basic_writer& js(const basic_chunk_string<char_type>& ss_in)
+ {
+ return this->eval_("text/javascript", ss_in);
+ }
+ };
+ } // namespace cgi
+} // namespace ocl
+
+#endif // ifndef _OCL_CGI_HPP