From f8d6796fc1c3db70e6a08bdf6aa7f13c94a74849 Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Wed, 5 Apr 2017 08:41:49 +0100 Subject: [PATCH] Wrap environment in a class. --- src/CMakeLists.txt | 1 + src/cgi_env.cpp | 82 ++++++++++++++++++++++++++++++++++++++++++ src/cgi_env.hpp | 42 ++++++++++++++++++++++ src/main.cpp | 21 ++++------- src/split_get_vars.cpp | 4 +-- src/split_get_vars.hpp | 4 ++- 6 files changed, 137 insertions(+), 17 deletions(-) create mode 100644 src/cgi_env.cpp create mode 100644 src/cgi_env.hpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index d1b2167..b4b19d5 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -12,6 +12,7 @@ add_executable(${PROJECT_NAME} submit_form_response.cpp get_env.cpp envy.cpp + cgi_env.cpp ) target_include_directories(${PROJECT_NAME} SYSTEM diff --git a/src/cgi_env.cpp b/src/cgi_env.cpp new file mode 100644 index 0000000..374d63b --- /dev/null +++ b/src/cgi_env.cpp @@ -0,0 +1,82 @@ +#include "cgi_env.hpp" +#include "envy.hpp" +#include "duckhandy/lexical_cast.hpp" + +namespace tawashi { + CGIEnv::CGIEnv() : + m_cgi_env(get_cgi_vars()) + { + } + + CGIEnv::~CGIEnv() noexcept = default; + + const std::string& CGIEnv::auth_type() const { + return m_cgi_env[CGIVars::AUTH_TYPE]; + } + + std::size_t CGIEnv::content_length() const { + using dhandy::lexical_cast; + const std::string& value = m_cgi_env[CGIVars::CONTENT_LENGTH]; + return (value.empty() ? 0U : lexical_cast(value)); + } + + const std::string& CGIEnv::content_type() const { + return m_cgi_env[CGIVars::CONTENT_TYPE]; + } + + VersionInfo CGIEnv::gateway_interface() const { + } + + const std::string& CGIEnv::path_info() const { + return m_cgi_env[CGIVars::PATH_INFO]; + } + + const std::string& CGIEnv::path_translated() const { + return m_cgi_env[CGIVars::PATH_TRANSLATED]; + } + + KeyValueList CGIEnv::query_string() const { + return split_env_vars(m_cgi_env[CGIVars::QUERY_STRING]); + } + + const std::string& CGIEnv::remote_addr() const { + return m_cgi_env[CGIVars::REMOTE_ADDR]; + } + + const std::string& CGIEnv::remote_host() const { + return m_cgi_env[CGIVars::REMOTE_HOST]; + } + + const std::string& CGIEnv::remote_ident() const { + return m_cgi_env[CGIVars::REMOTE_IDENT]; + } + + const std::string& CGIEnv::remote_user() const { + return m_cgi_env[CGIVars::REMOTE_USER]; + } + + const std::string& CGIEnv::request_method() const { + return m_cgi_env[CGIVars::REQUEST_METHOD]; + } + + const std::string& CGIEnv::script_name() const { + return m_cgi_env[CGIVars::SCRIPT_NAME]; + } + + const std::string& CGIEnv::server_name() const { + return m_cgi_env[CGIVars::SERVER_NAME]; + } + + uint16_t CGIEnv::server_port() const { + using dhandy::lexical_cast; + const std::string& value = m_cgi_env[CGIVars::SERVER_PORT]; + return (value.empty() ? 0U : lexical_cast(value)); + } + + VersionInfo CGIEnv::server_protocol() const { + } + + const std::string& CGIEnv::server_software() const { + return m_cgi_env[CGIVars::SERVER_SOFTWARE]; + } +} //namespace tawashi diff --git a/src/cgi_env.hpp b/src/cgi_env.hpp new file mode 100644 index 0000000..250fd94 --- /dev/null +++ b/src/cgi_env.hpp @@ -0,0 +1,42 @@ +#pragma once + +#include "split_get_vars.hpp" +#include +#include +#include +#include + +namespace tawashi { + struct VersionInfo { + boost::string_ref name; + uint16_t major; + uint16_t minor; + }; + + class CGIEnv { + public: + CGIEnv(); + ~CGIEnv() noexcept; + + const std::string& auth_type() const; + std::size_t content_length() const; + const std::string& content_type() const; + VersionInfo gateway_interface() const; + const std::string& path_info() const; + const std::string& path_translated() const; + KeyValueList query_string() const; + const std::string& remote_addr() const; + const std::string& remote_host() const; + const std::string& remote_ident() const; + const std::string& remote_user() const; + const std::string& request_method() const; + const std::string& script_name() const; + const std::string& server_name() const; + uint16_t server_port() const; + VersionInfo server_protocol() const; + const std::string& server_software() const; + + private: + std::vector m_cgi_env; + }; +} //namespace tawashi diff --git a/src/main.cpp b/src/main.cpp index 3f6bad3..c430ee2 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,8 +1,6 @@ #include "incredis/incredis.hpp" -#include "split_get_vars.hpp" #include "submit_form_response.hpp" -#include "envy.hpp" -#include "duckhandy/lexical_cast.hpp" +#include "cgi_env.hpp" #include #include #include @@ -14,31 +12,26 @@ namespace { } //unnamed namespace int main() { - using boost::string_ref; - using dhandy::lexical_cast; - - std::vector env = tawashi::get_cgi_vars(); //std::cout << "Content-type:text/plain\n\n"; - const std::string& getvar = env[tawashi::CGIVars::QUERY_STRING]; - //std::cout << "QUERY_STRING = \"" << getvar << "\"\n"; redis::IncRedis incredis("127.0.0.1", 6379); tawashi::SubmitFormResponse resp; resp.send(); - for (auto& pair : tawashi::split_env_vars(getvar)) { + tawashi::CGIEnv cgi_env; + for (auto& pair : cgi_env.query_string()) { std::cout << "first:\t\"" << pair.first << "\"\tsecond:\t\"" << pair.second << "\"\n"; } - const std::size_t in_len = lexical_cast(env[tawashi::CGIVars::CONTENT_LENGTH]); + const std::size_t in_len = cgi_env.content_length(); std::cout << "\n
\n"; std::cout << "Content length: \"" << in_len << "\"\n
\n"; - for (std::size_t z = 0; z < env.size(); ++z) { - std::cout << tawashi::CGIVars::_from_integral(z) << " = \"" << env[z] << "\"
\n"; - } + //for (std::size_t z = 0; z < env.size(); ++z) { + // std::cout << tawashi::CGIVars::_from_integral(z) << " = \"" << env[z] << "\"
\n"; + //} std::string input; if (in_len > 0) std::copy_n(std::istream_iterator(std::cin), in_len, std::back_inserter(input)); diff --git a/src/split_get_vars.cpp b/src/split_get_vars.cpp index 176f25a..9509af4 100644 --- a/src/split_get_vars.cpp +++ b/src/split_get_vars.cpp @@ -9,7 +9,7 @@ #include namespace tawashi { - std::vector> split_env_vars (const std::string& parList) { + KeyValueList split_env_vars (const std::string& parList) { using MatchRange = boost::iterator_range; using boost::token_finder; using boost::adaptors::transformed; @@ -23,7 +23,7 @@ namespace tawashi { //https://stackoverflow.com/questions/27999941/how-to-use-boostsplit-with-booststring-ref-in-boost-1-55 //http://www.boost.org/doc/libs/1_60_0/doc/html/boost/algorithm/token_finder.html //https://stackoverflow.com/questions/20781090/difference-between-boostsplit-vs-boostiter-split - return boost::copy_range>>( + return boost::copy_range( make_iterator_range( split_iterator(parList, token_finder([](char c){return '&'==c;})), split_iterator() diff --git a/src/split_get_vars.hpp b/src/split_get_vars.hpp index 7f5cd40..52b8b34 100644 --- a/src/split_get_vars.hpp +++ b/src/split_get_vars.hpp @@ -6,5 +6,7 @@ #include namespace tawashi { - std::vector> split_env_vars ( const std::string& parCommaSeparatedList ) a_pure; + typedef std::vector> KeyValueList; + + KeyValueList split_env_vars ( const std::string& parCommaSeparatedList ) a_pure; } //namespace tawashi