From 79c2769714d1b41416360cbb23a64f5cf68abc1f Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Sat, 29 Mar 2025 20:27:48 +0100 Subject: module: add: CGI header. Signed-off-by: Amlal El Mahrouss --- ReadMe.md | 22 ++++++++++++++ Readme.md | 18 ------------ examples/cgi/CMakeLists.txt | 12 ++++++++ examples/cgi/cgi.cc | 33 +++++++++++++++++++++ examples/must_pass/CMakeLists.txt | 2 +- lib/cgi.cc | 8 +++++ lib/stdx/cgi.hpp | 62 +++++++++++++++++++++++++++++++++++++++ lib/stdx/opt.hpp | 8 ++--- 8 files changed, 142 insertions(+), 23 deletions(-) create mode 100644 ReadMe.md delete mode 100644 Readme.md create mode 100644 examples/cgi/CMakeLists.txt create mode 100644 examples/cgi/cgi.cc create mode 100644 lib/cgi.cc create mode 100644 lib/stdx/cgi.hpp diff --git a/ReadMe.md b/ReadMe.md new file mode 100644 index 0000000..b3481ff --- /dev/null +++ b/ReadMe.md @@ -0,0 +1,22 @@ +# STDX (Standard eXtended) + +[![License: GPL-2.0](https://img.shields.io/badge/license-GPL--2.0-blue.svg)](LICENSE) + +## Getting Started: + +Here is an example of how stdx::opt works. + +```cpp +#include + +int main(int argc, char** argv) +{ + auto opt = stdx::opt(stdx::eval_eq(50, 50)).expect("stdex::eval_eq, does not match!"); + opt = stdx::opt(stdx::eval_eq(50, 40)); + opt.expect("this time it doesn't."); + + return 0; +} +``` + +# Licensed under GPL-2 diff --git a/Readme.md b/Readme.md deleted file mode 100644 index b2d7f77..0000000 --- a/Readme.md +++ /dev/null @@ -1,18 +0,0 @@ -# STDX (Standard eXtended) - -Here is an example of how stdx::opt works. - -```cpp -#include - -int main(int argc, char** argv) -{ - auto opt = stdx::opt(stdx::eval_eq(50, 50)).expect("stdex::eval_eq, does not match!"); - opt = stdx::opt(stdx::eval_eq(50, 40)); - opt.expect("this time it doesn't."); - - return 0; -} -``` - -# Licensed under GPL-2 diff --git a/examples/cgi/CMakeLists.txt b/examples/cgi/CMakeLists.txt new file mode 100644 index 0000000..6a93acf --- /dev/null +++ b/examples/cgi/CMakeLists.txt @@ -0,0 +1,12 @@ + +cmake_minimum_required(VERSION 3.15...3.31) + +project( + CGI + VERSION 1.0 + LANGUAGES CXX) + +add_executable(CGI cgi.cc) + +set_property(TARGET CGI PROPERTY CXX_STANDARD 20) +target_include_directories(CGI PUBLIC ../../lib) diff --git a/examples/cgi/cgi.cc b/examples/cgi/cgi.cc new file mode 100644 index 0000000..c2b4267 --- /dev/null +++ b/examples/cgi/cgi.cc @@ -0,0 +1,33 @@ +/* + cgi example + written by Amlal El Mahrouss. + licensed under GPL-2 license + */ + +#include +#include +#include +#include +#include + +/* finally test it */ +int main(int argc, char** argv) +{ + // ... let's assume we serve data. + + stdx::web::cgi_writer writer; + std::stringstream ss_file; + + std::ifstream fp("index.html"); + + if (fp.good()) + ss_file << fp.rdbuf(); + else + ss_file << "\n"; + + fp.close(); + + writer.eval_html(ss_file); + + return 0; +} diff --git a/examples/must_pass/CMakeLists.txt b/examples/must_pass/CMakeLists.txt index 0075ad5..6fcd411 100644 --- a/examples/must_pass/CMakeLists.txt +++ b/examples/must_pass/CMakeLists.txt @@ -9,4 +9,4 @@ project( add_executable(MustPass must_pass.cc) set_property(TARGET MustPass PROPERTY CXX_STANDARD 20) -target_include_directories(MustPass PUBLIC ../lib) +target_include_directories(MustPass PUBLIC ../../lib) diff --git a/lib/cgi.cc b/lib/cgi.cc new file mode 100644 index 0000000..d92a455 --- /dev/null +++ b/lib/cgi.cc @@ -0,0 +1,8 @@ +/* + * File: cgi.cc + * Author: Amlal El Mahrouss, + * Copyright 2023-2025, Amlal El Mahrouss all rights reserved. + */ + +#include + diff --git a/lib/stdx/cgi.hpp b/lib/stdx/cgi.hpp new file mode 100644 index 0000000..317ee6c --- /dev/null +++ b/lib/stdx/cgi.hpp @@ -0,0 +1,62 @@ +/* + * File: cgi.hpp + * Author: Amlal El Mahrouss, + * Copyright 2023-2025, Amlal El Mahrouss all rights reserved. + */ + +#ifndef _STDX_CGI_HPP +#define _STDX_CGI_HPP + +#include +#include +#include + +namespace stdx +{ + namespace web + { + class cgi_writer final + { + cgi_writer& eval_(const std::string& mime, const std::stringstream& ss) noexcept + { + std::printf("Content-Type: %s\r\n", mime.c_str()); + std::printf("Server: %s\r\n", "STDX CGI"); + std::printf("Content-Length: %ld\r\n\r\n", ss.str().size()); + std::printf("%s", ss.str().c_str()); + + return *this; + } + + public: + explicit cgi_writer() + { + } + + cgi_writer& operator=(const cgi_writer&) = default; + cgi_writer(const cgi_writer&) = default; + + public: + cgi_writer& eval_html(const std::stringstream& ss_html) + { + return this->eval_("text/html", ss_html); + } + + cgi_writer& eval_xml(const std::stringstream& ss_html) + { + return this->eval_("application/xml", ss_html); + } + + cgi_writer& eval_json(const std::stringstream& ss_html) + { + return this->eval_("application/json", ss_html); + } + + cgi_writer& eval_js(const std::stringstream& ss_html) + { + return this->eval_("text/javascript", ss_html); + } + }; + } // namespace details +} // namespace stdx + +#endif // ifndef _STDX_CGI_HPP \ No newline at end of file diff --git a/lib/stdx/opt.hpp b/lib/stdx/opt.hpp index 66ef4a5..5624c0a 100644 --- a/lib/stdx/opt.hpp +++ b/lib/stdx/opt.hpp @@ -1,11 +1,11 @@ /* - * File: stdx.hpp + * File: opt.hpp * Author: Amlal El Mahrouss, * Copyright 2023-2025, Amlal El Mahrouss all rights reserved. */ -#ifndef _STDX_HPP -#define _STDX_HPP +#ifndef _STDX_OPT_HPP +#define _STDX_OPT_HPP #include #include @@ -107,4 +107,4 @@ namespace stdx } /* namespace stdx */ -#endif /* ifndef _STDX_HPP */ +#endif /* ifndef _STDX_OPT_HPP */ -- cgit v1.2.3