diff --git a/src/cgi_env.cpp b/src/cgi_env.cpp index 32179ca..c923480 100644 --- a/src/cgi_env.cpp +++ b/src/cgi_env.cpp @@ -15,15 +15,16 @@ #include BOOST_FUSION_ADAPT_STRUCT( - tawashi::CGIEnv::VersionInfo, + tawashi::cgi::Env::VersionInfo, (boost::string_ref, name) (uint16_t, major) (uint16_t, minor) ); namespace tawashi { +namespace cgi { namespace { - boost::optional split_version (const std::string& parString) { + boost::optional split_version (const std::string& parString) { namespace px = boost::phoenix; using boost::spirit::ascii::space; @@ -47,7 +48,7 @@ namespace tawashi { VerNum ver_num; auto it_curr = parString.cbegin(); - CGIEnv::VersionInfo retval; + Env::VersionInfo retval; const bool parse_ret = boost::spirit::qi::phrase_parse( it_curr, parString.end(), @@ -59,90 +60,90 @@ namespace tawashi { if (parse_ret and parString.end() == it_curr) return make_optional(retval); else - return optional(); + return optional(); } } //unnamed namespace - CGIEnv::CGIEnv() : + Env::Env() : m_cgi_env(get_cgi_environment_vars()) { } - CGIEnv::~CGIEnv() noexcept = default; + Env::~Env() noexcept = default; - const std::string& CGIEnv::auth_type() const { + const std::string& Env::auth_type() const { return m_cgi_env[CGIVars::AUTH_TYPE]; } - std::size_t CGIEnv::content_length() const { + std::size_t Env::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 { + const std::string& Env::content_type() const { return m_cgi_env[CGIVars::CONTENT_TYPE]; } - auto CGIEnv::gateway_interface() const -> boost::optional { + auto Env::gateway_interface() const -> boost::optional { return split_version(m_cgi_env[CGIVars::GATEWAY_INTERFACE]); } - const std::string& CGIEnv::path_info() const { + const std::string& Env::path_info() const { return m_cgi_env[CGIVars::PATH_INFO]; } - const std::string& CGIEnv::path_translated() const { + const std::string& Env::path_translated() const { return m_cgi_env[CGIVars::PATH_TRANSLATED]; } - const std::string& CGIEnv::query_string() const { + const std::string& Env::query_string() const { return m_cgi_env[CGIVars::QUERY_STRING]; } - const std::string& CGIEnv::remote_addr() const { + const std::string& Env::remote_addr() const { return m_cgi_env[CGIVars::REMOTE_ADDR]; } - const std::string& CGIEnv::remote_host() const { + const std::string& Env::remote_host() const { return m_cgi_env[CGIVars::REMOTE_HOST]; } - const std::string& CGIEnv::remote_ident() const { + const std::string& Env::remote_ident() const { return m_cgi_env[CGIVars::REMOTE_IDENT]; } - const std::string& CGIEnv::remote_user() const { + const std::string& Env::remote_user() const { return m_cgi_env[CGIVars::REMOTE_USER]; } - const std::string& CGIEnv::request_method() const { + const std::string& Env::request_method() const { return m_cgi_env[CGIVars::REQUEST_METHOD]; } - const std::string& CGIEnv::script_name() const { + const std::string& Env::script_name() const { return m_cgi_env[CGIVars::SCRIPT_NAME]; } - const std::string& CGIEnv::server_name() const { + const std::string& Env::server_name() const { return m_cgi_env[CGIVars::SERVER_NAME]; } - uint16_t CGIEnv::server_port() const { + uint16_t Env::server_port() const { using dhandy::lexical_cast; const std::string& value = m_cgi_env[CGIVars::SERVER_PORT]; return (value.empty() ? 0U : lexical_cast(value)); } - auto CGIEnv::server_protocol() const -> boost::optional { + auto Env::server_protocol() const -> boost::optional { return split_version(m_cgi_env[CGIVars::SERVER_PROTOCOL]); } - const std::string& CGIEnv::server_software() const { + const std::string& Env::server_software() const { return m_cgi_env[CGIVars::SERVER_SOFTWARE]; } - CGIEnv::GetMapType CGIEnv::query_string_split() const { + Env::GetMapType Env::query_string_split() const { GetMapType retval; const auto urlencoded_values = split_env_vars(m_cgi_env[CGIVars::QUERY_STRING]); retval.reserve(urlencoded_values.size()); @@ -152,11 +153,13 @@ namespace tawashi { return retval; } - std::ostream& CGIEnv::print_all (std::ostream& parStream, const char* parNewline) const { + std::ostream& Env::print_all (std::ostream& parStream, const char* parNewline) const { for (std::size_t z = 0; z < m_cgi_env.size(); ++z) { parStream << CGIVars::_from_integral(z) << " = \"" << m_cgi_env[z] << '"' << parNewline; } return parStream; } + +} //namespace cgi } //namespace tawashi diff --git a/src/cgi_env.hpp b/src/cgi_env.hpp index 1a8cd23..4c51217 100644 --- a/src/cgi_env.hpp +++ b/src/cgi_env.hpp @@ -12,43 +12,45 @@ #include namespace tawashi { - class CGIEnv { - public: - struct VersionInfo { - boost::string_ref name; - uint16_t major; - uint16_t minor; + namespace cgi { + class Env { + public: + struct VersionInfo { + boost::string_ref name; + uint16_t major; + uint16_t minor; + }; + + typedef boost::container::flat_map GetMapType; + + Env(); + ~Env() noexcept; + + const std::string& auth_type() const; + std::size_t content_length() const; + const std::string& content_type() const; + boost::optional gateway_interface() const a_pure; + const std::string& path_info() const; + const std::string& path_translated() const; + const std::string& 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 a_pure; + boost::optional server_protocol() const a_pure; + const std::string& server_software() const; + + GetMapType query_string_split() const a_pure; + + std::ostream& print_all (std::ostream& parStream, const char* parNewline) const; + + private: + std::vector m_cgi_env; + CurlWrapper m_curl; }; - - typedef boost::container::flat_map GetMapType; - - CGIEnv(); - ~CGIEnv() noexcept; - - const std::string& auth_type() const; - std::size_t content_length() const; - const std::string& content_type() const; - boost::optional gateway_interface() const a_pure; - const std::string& path_info() const; - const std::string& path_translated() const; - const std::string& 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 a_pure; - boost::optional server_protocol() const a_pure; - const std::string& server_software() const; - - GetMapType query_string_split() const a_pure; - - std::ostream& print_all (std::ostream& parStream, const char* parNewline) const; - - private: - std::vector m_cgi_env; - CurlWrapper m_curl; - }; + } //namespace cgi } //namespace tawashi diff --git a/src/cgi_post.cpp b/src/cgi_post.cpp index 175d9fd..51734e2 100644 --- a/src/cgi_post.cpp +++ b/src/cgi_post.cpp @@ -14,7 +14,7 @@ namespace tawashi { namespace { } //unnamed namespace - const PostMapType& read_post (const CGIEnv& parEnv) { + const PostMapType& read_post (const Env& parEnv) { static bool already_read = false; static PostMapType map; static std::string original_data; diff --git a/src/cgi_post.hpp b/src/cgi_post.hpp index cf9f268..ac00a8d 100644 --- a/src/cgi_post.hpp +++ b/src/cgi_post.hpp @@ -4,11 +4,12 @@ #include namespace tawashi { - class CGIEnv; namespace cgi { + class Env; + typedef boost::container::flat_map PostMapType; - const PostMapType& read_post (const CGIEnv& parEnv); + const PostMapType& read_post (const Env& parEnv); } //namespace cgi } //namespace tawashi diff --git a/src/main.cpp b/src/main.cpp index ce0d19b..d3b568f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -16,7 +16,7 @@ int main() { redis::IncRedis incredis("127.0.0.1", 6379); incredis.connect(); - tawashi::CGIEnv cgi_env; + tawashi::cgi::Env cgi_env; if (cgi_env.path_info() == "/index.cgi") { tawashi::IndexResponse resp; resp.send(); diff --git a/src/response.cpp b/src/response.cpp index b48c0c8..7e20552 100644 --- a/src/response.cpp +++ b/src/response.cpp @@ -15,7 +15,7 @@ namespace tawashi { std::cout.flush(); } - const CGIEnv& Response::cgi_env() const { + const cgi::Env& Response::cgi_env() const { return m_cgi_env; } } //namespace tawashi diff --git a/src/response.hpp b/src/response.hpp index 9446e9c..14a54b2 100644 --- a/src/response.hpp +++ b/src/response.hpp @@ -13,12 +13,12 @@ namespace tawashi { protected: Response (std::string&& parType); - const CGIEnv& cgi_env() const; + const cgi::Env& cgi_env() const; private: virtual void on_send (std::ostream& parStream) = 0; - CGIEnv m_cgi_env; + cgi::Env m_cgi_env; std::string m_content_type; }; } //namespace tawashi