1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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 <lib/io/print.hpp>
#include <lib/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
|