1
0
Fork 0
mirror of https://github.com/KingDuckZ/kamokan.git synced 2024-11-23 00:33:44 +00:00

Wrap environment in a class.

This commit is contained in:
King_DuckZ 2017-04-05 08:41:49 +01:00
parent fea5b738df
commit f8d6796fc1
6 changed files with 137 additions and 17 deletions

View file

@ -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

82
src/cgi_env.cpp Normal file
View file

@ -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<std::size_t>(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<uint16_t>(value));
}
VersionInfo CGIEnv::server_protocol() const {
}
const std::string& CGIEnv::server_software() const {
return m_cgi_env[CGIVars::SERVER_SOFTWARE];
}
} //namespace tawashi

42
src/cgi_env.hpp Normal file
View file

@ -0,0 +1,42 @@
#pragma once
#include "split_get_vars.hpp"
#include <vector>
#include <string>
#include <boost/utility/string_ref.hpp>
#include <cstdint>
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<std::string> m_cgi_env;
};
} //namespace tawashi

View file

@ -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 <iostream>
#include <string>
#include <algorithm>
@ -14,31 +12,26 @@ namespace {
} //unnamed namespace
int main() {
using boost::string_ref;
using dhandy::lexical_cast;
std::vector<std::string> 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<std::size_t>(env[tawashi::CGIVars::CONTENT_LENGTH]);
const std::size_t in_len = cgi_env.content_length();
std::cout << "\n<br>\n";
std::cout << "Content length: \"" << in_len << "\"\n<br>\n";
for (std::size_t z = 0; z < env.size(); ++z) {
std::cout << tawashi::CGIVars::_from_integral(z) << " = \"" << env[z] << "\"<br>\n";
}
//for (std::size_t z = 0; z < env.size(); ++z) {
// std::cout << tawashi::CGIVars::_from_integral(z) << " = \"" << env[z] << "\"<br>\n";
//}
std::string input;
if (in_len > 0)
std::copy_n(std::istream_iterator<char>(std::cin), in_len, std::back_inserter(input));

View file

@ -9,7 +9,7 @@
#include <boost/range/algorithm/find.hpp>
namespace tawashi {
std::vector<std::pair<boost::string_ref, boost::string_ref>> split_env_vars (const std::string& parList) {
KeyValueList split_env_vars (const std::string& parList) {
using MatchRange = boost::iterator_range<std::string::const_iterator>;
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<std::vector<std::pair<string_ref, string_ref>>>(
return boost::copy_range<KeyValueList>(
make_iterator_range(
split_iterator<std::string::const_iterator>(parList, token_finder([](char c){return '&'==c;})),
split_iterator<std::string::const_iterator>()

View file

@ -6,5 +6,7 @@
#include <utility>
namespace tawashi {
std::vector<std::pair<boost::string_ref, boost::string_ref>> split_env_vars ( const std::string& parCommaSeparatedList ) a_pure;
typedef std::vector<std::pair<boost::string_ref, boost::string_ref>> KeyValueList;
KeyValueList split_env_vars ( const std::string& parCommaSeparatedList ) a_pure;
} //namespace tawashi