mirror of
https://github.com/KingDuckZ/kamokan.git
synced 2025-06-07 00:51:41 +00:00
Rename CGIEnv to cgi::Env
This commit is contained in:
parent
fa33944919
commit
81cf36de22
7 changed files with 76 additions and 70 deletions
|
@ -15,15 +15,16 @@
|
||||||
#include <boost/phoenix/stl/container.hpp>
|
#include <boost/phoenix/stl/container.hpp>
|
||||||
|
|
||||||
BOOST_FUSION_ADAPT_STRUCT(
|
BOOST_FUSION_ADAPT_STRUCT(
|
||||||
tawashi::CGIEnv::VersionInfo,
|
tawashi::cgi::Env::VersionInfo,
|
||||||
(boost::string_ref, name)
|
(boost::string_ref, name)
|
||||||
(uint16_t, major)
|
(uint16_t, major)
|
||||||
(uint16_t, minor)
|
(uint16_t, minor)
|
||||||
);
|
);
|
||||||
|
|
||||||
namespace tawashi {
|
namespace tawashi {
|
||||||
|
namespace cgi {
|
||||||
namespace {
|
namespace {
|
||||||
boost::optional<CGIEnv::VersionInfo> split_version (const std::string& parString) {
|
boost::optional<Env::VersionInfo> split_version (const std::string& parString) {
|
||||||
namespace px = boost::phoenix;
|
namespace px = boost::phoenix;
|
||||||
|
|
||||||
using boost::spirit::ascii::space;
|
using boost::spirit::ascii::space;
|
||||||
|
@ -47,7 +48,7 @@ namespace tawashi {
|
||||||
VerNum ver_num;
|
VerNum ver_num;
|
||||||
|
|
||||||
auto it_curr = parString.cbegin();
|
auto it_curr = parString.cbegin();
|
||||||
CGIEnv::VersionInfo retval;
|
Env::VersionInfo retval;
|
||||||
const bool parse_ret = boost::spirit::qi::phrase_parse(
|
const bool parse_ret = boost::spirit::qi::phrase_parse(
|
||||||
it_curr,
|
it_curr,
|
||||||
parString.end(),
|
parString.end(),
|
||||||
|
@ -59,90 +60,90 @@ namespace tawashi {
|
||||||
if (parse_ret and parString.end() == it_curr)
|
if (parse_ret and parString.end() == it_curr)
|
||||||
return make_optional(retval);
|
return make_optional(retval);
|
||||||
else
|
else
|
||||||
return optional<CGIEnv::VersionInfo>();
|
return optional<Env::VersionInfo>();
|
||||||
}
|
}
|
||||||
} //unnamed namespace
|
} //unnamed namespace
|
||||||
|
|
||||||
CGIEnv::CGIEnv() :
|
Env::Env() :
|
||||||
m_cgi_env(get_cgi_environment_vars())
|
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];
|
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;
|
using dhandy::lexical_cast;
|
||||||
const std::string& value = m_cgi_env[CGIVars::CONTENT_LENGTH];
|
const std::string& value = m_cgi_env[CGIVars::CONTENT_LENGTH];
|
||||||
return (value.empty() ? 0U : lexical_cast<std::size_t>(value));
|
return (value.empty() ? 0U : lexical_cast<std::size_t>(value));
|
||||||
}
|
}
|
||||||
|
|
||||||
const std::string& CGIEnv::content_type() const {
|
const std::string& Env::content_type() const {
|
||||||
return m_cgi_env[CGIVars::CONTENT_TYPE];
|
return m_cgi_env[CGIVars::CONTENT_TYPE];
|
||||||
}
|
}
|
||||||
|
|
||||||
auto CGIEnv::gateway_interface() const -> boost::optional<VersionInfo> {
|
auto Env::gateway_interface() const -> boost::optional<VersionInfo> {
|
||||||
return split_version(m_cgi_env[CGIVars::GATEWAY_INTERFACE]);
|
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];
|
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];
|
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];
|
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];
|
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];
|
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];
|
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];
|
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];
|
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];
|
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];
|
return m_cgi_env[CGIVars::SERVER_NAME];
|
||||||
}
|
}
|
||||||
|
|
||||||
uint16_t CGIEnv::server_port() const {
|
uint16_t Env::server_port() const {
|
||||||
using dhandy::lexical_cast;
|
using dhandy::lexical_cast;
|
||||||
const std::string& value = m_cgi_env[CGIVars::SERVER_PORT];
|
const std::string& value = m_cgi_env[CGIVars::SERVER_PORT];
|
||||||
return (value.empty() ? 0U : lexical_cast<uint16_t>(value));
|
return (value.empty() ? 0U : lexical_cast<uint16_t>(value));
|
||||||
}
|
}
|
||||||
|
|
||||||
auto CGIEnv::server_protocol() const -> boost::optional<VersionInfo> {
|
auto Env::server_protocol() const -> boost::optional<VersionInfo> {
|
||||||
return split_version(m_cgi_env[CGIVars::SERVER_PROTOCOL]);
|
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];
|
return m_cgi_env[CGIVars::SERVER_SOFTWARE];
|
||||||
}
|
}
|
||||||
|
|
||||||
CGIEnv::GetMapType CGIEnv::query_string_split() const {
|
Env::GetMapType Env::query_string_split() const {
|
||||||
GetMapType retval;
|
GetMapType retval;
|
||||||
const auto urlencoded_values = split_env_vars(m_cgi_env[CGIVars::QUERY_STRING]);
|
const auto urlencoded_values = split_env_vars(m_cgi_env[CGIVars::QUERY_STRING]);
|
||||||
retval.reserve(urlencoded_values.size());
|
retval.reserve(urlencoded_values.size());
|
||||||
|
@ -152,11 +153,13 @@ namespace tawashi {
|
||||||
return retval;
|
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) {
|
for (std::size_t z = 0; z < m_cgi_env.size(); ++z) {
|
||||||
parStream << CGIVars::_from_integral(z) <<
|
parStream << CGIVars::_from_integral(z) <<
|
||||||
" = \"" << m_cgi_env[z] << '"' << parNewline;
|
" = \"" << m_cgi_env[z] << '"' << parNewline;
|
||||||
}
|
}
|
||||||
return parStream;
|
return parStream;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} //namespace cgi
|
||||||
} //namespace tawashi
|
} //namespace tawashi
|
||||||
|
|
|
@ -12,43 +12,45 @@
|
||||||
#include <boost/container/flat_map.hpp>
|
#include <boost/container/flat_map.hpp>
|
||||||
|
|
||||||
namespace tawashi {
|
namespace tawashi {
|
||||||
class CGIEnv {
|
namespace cgi {
|
||||||
public:
|
class Env {
|
||||||
struct VersionInfo {
|
public:
|
||||||
boost::string_ref name;
|
struct VersionInfo {
|
||||||
uint16_t major;
|
boost::string_ref name;
|
||||||
uint16_t minor;
|
uint16_t major;
|
||||||
|
uint16_t minor;
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef boost::container::flat_map<std::string, std::string> GetMapType;
|
||||||
|
|
||||||
|
Env();
|
||||||
|
~Env() noexcept;
|
||||||
|
|
||||||
|
const std::string& auth_type() const;
|
||||||
|
std::size_t content_length() const;
|
||||||
|
const std::string& content_type() const;
|
||||||
|
boost::optional<VersionInfo> 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<VersionInfo> 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<std::string> m_cgi_env;
|
||||||
|
CurlWrapper m_curl;
|
||||||
};
|
};
|
||||||
|
} //namespace cgi
|
||||||
typedef boost::container::flat_map<std::string, std::string> GetMapType;
|
|
||||||
|
|
||||||
CGIEnv();
|
|
||||||
~CGIEnv() noexcept;
|
|
||||||
|
|
||||||
const std::string& auth_type() const;
|
|
||||||
std::size_t content_length() const;
|
|
||||||
const std::string& content_type() const;
|
|
||||||
boost::optional<VersionInfo> 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<VersionInfo> 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<std::string> m_cgi_env;
|
|
||||||
CurlWrapper m_curl;
|
|
||||||
};
|
|
||||||
} //namespace tawashi
|
} //namespace tawashi
|
||||||
|
|
|
@ -14,7 +14,7 @@ namespace tawashi {
|
||||||
namespace {
|
namespace {
|
||||||
} //unnamed namespace
|
} //unnamed namespace
|
||||||
|
|
||||||
const PostMapType& read_post (const CGIEnv& parEnv) {
|
const PostMapType& read_post (const Env& parEnv) {
|
||||||
static bool already_read = false;
|
static bool already_read = false;
|
||||||
static PostMapType map;
|
static PostMapType map;
|
||||||
static std::string original_data;
|
static std::string original_data;
|
||||||
|
|
|
@ -4,11 +4,12 @@
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
namespace tawashi {
|
namespace tawashi {
|
||||||
class CGIEnv;
|
|
||||||
|
|
||||||
namespace cgi {
|
namespace cgi {
|
||||||
|
class Env;
|
||||||
|
|
||||||
typedef boost::container::flat_map<std::string, std::string> PostMapType;
|
typedef boost::container::flat_map<std::string, std::string> PostMapType;
|
||||||
|
|
||||||
const PostMapType& read_post (const CGIEnv& parEnv);
|
const PostMapType& read_post (const Env& parEnv);
|
||||||
} //namespace cgi
|
} //namespace cgi
|
||||||
} //namespace tawashi
|
} //namespace tawashi
|
||||||
|
|
|
@ -16,7 +16,7 @@ int main() {
|
||||||
redis::IncRedis incredis("127.0.0.1", 6379);
|
redis::IncRedis incredis("127.0.0.1", 6379);
|
||||||
incredis.connect();
|
incredis.connect();
|
||||||
|
|
||||||
tawashi::CGIEnv cgi_env;
|
tawashi::cgi::Env cgi_env;
|
||||||
if (cgi_env.path_info() == "/index.cgi") {
|
if (cgi_env.path_info() == "/index.cgi") {
|
||||||
tawashi::IndexResponse resp;
|
tawashi::IndexResponse resp;
|
||||||
resp.send();
|
resp.send();
|
||||||
|
|
|
@ -15,7 +15,7 @@ namespace tawashi {
|
||||||
std::cout.flush();
|
std::cout.flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
const CGIEnv& Response::cgi_env() const {
|
const cgi::Env& Response::cgi_env() const {
|
||||||
return m_cgi_env;
|
return m_cgi_env;
|
||||||
}
|
}
|
||||||
} //namespace tawashi
|
} //namespace tawashi
|
||||||
|
|
|
@ -13,12 +13,12 @@ namespace tawashi {
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
Response (std::string&& parType);
|
Response (std::string&& parType);
|
||||||
const CGIEnv& cgi_env() const;
|
const cgi::Env& cgi_env() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
virtual void on_send (std::ostream& parStream) = 0;
|
virtual void on_send (std::ostream& parStream) = 0;
|
||||||
|
|
||||||
CGIEnv m_cgi_env;
|
cgi::Env m_cgi_env;
|
||||||
std::string m_content_type;
|
std::string m_content_type;
|
||||||
};
|
};
|
||||||
} //namespace tawashi
|
} //namespace tawashi
|
||||||
|
|
Loading…
Add table
Reference in a new issue