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

Delete get_env, which confused me a lot.

Get raw environment variables directly from main().
Add code to split on '=' in cgi_environment_vars and only pick
the ones that are interesting, just as before.
This commit is contained in:
King_DuckZ 2017-05-08 19:46:14 +01:00
parent 376b34c1d0
commit 07135f215c
9 changed files with 60 additions and 98 deletions

View file

@ -74,7 +74,7 @@ namespace {
}
} //unnamed namespace
int main() {
int main (int parArgc, char* parArgv[], char* parEnvp[]) {
using curry::SafeStackObject;
using tawashi::IndexResponse;
using tawashi::SubmitPasteResponse;
@ -101,7 +101,7 @@ int main() {
spdlog::set_level(static_cast<decltype(spdlog::level::trace)>(logging_level._to_integral()));
}
auto cgi_env = SafeStackObject<tawashi::cgi::Env>();
auto cgi_env = SafeStackObject<tawashi::cgi::Env>(parEnvp);
tawashi::ResponseFactory resp_factory(settings, cgi_env);
SPDLOG_TRACE(statuslog, "Registering makers in the response factory");
resp_factory.register_maker("index.cgi", &make_response<IndexResponse>);

View file

@ -10,7 +10,6 @@ add_library(${PROJECT_NAME} STATIC
split_get_vars.cpp
response.cpp
submit_paste_response.cpp
get_env.cpp
cgi_environment_vars.cpp
cgi_env.cpp
num_to_token.cpp

View file

@ -81,8 +81,8 @@ namespace cgi {
}
} //unnamed namespace
Env::Env() :
m_cgi_env(cgi_environment_vars())
Env::Env(const char* const* parEnvList) :
m_cgi_env(cgi_environment_vars(parEnvList))
{
}

View file

@ -42,7 +42,7 @@ namespace tawashi {
typedef boost::container::flat_map<std::string, std::string> GetMapType;
Env();
explicit Env (const char* const* parEnvList);
virtual_testing ~Env() noexcept;
virtual_testing const std::string& auth_type() const;

View file

@ -16,19 +16,64 @@
*/
#include "cgi_environment_vars.hpp"
#include "get_env.hpp"
#include "sanitized_utf8.hpp"
#include <utility>
#include <unordered_map>
#include <boost/utility/string_ref.hpp>
#include <cassert>
#include <cstring>
#include <boost/functional/hash.hpp>
namespace std {
template<>
struct hash<boost::string_ref> {
std::size_t operator() (boost::string_ref const& parStr) const {
return boost::hash_range(parStr.begin(), parStr.end());
}
};
} //namespace std
namespace tawashi {
std::vector<std::string> cgi_environment_vars() {
namespace {
std::unordered_map<boost::string_ref, boost::string_ref> get_unrefined_env_vars (const char* const* parEnvList) {
using boost::string_ref;
assert(parEnvList);
std::size_t count = 0;
while (*(parEnvList + count)) {
++count;
}
std::unordered_map<string_ref, string_ref> retval;
retval.reserve(count);
for (std::size_t z = 0; z < count; ++z) {
const char* const equal_sign = std::strchr(parEnvList[z], '=');
assert('=' == *equal_sign);
assert(equal_sign >= parEnvList[z]);
const std::size_t key_length = static_cast<std::size_t>(equal_sign - parEnvList[z]);
const std::size_t whole_length = std::strlen(parEnvList[z] + key_length) + key_length;
assert(std::strlen(parEnvList[z]) == whole_length);
assert(whole_length >= key_length + 1);
retval[string_ref(parEnvList[z], key_length)] = string_ref(parEnvList[z] + key_length + 1, whole_length - key_length - 1);
}
return retval;
}
} //unnamed namespace
std::vector<std::string> cgi_environment_vars (const char* const* parEnvList) {
using boost::string_ref;
std::vector<std::string> retlist;
retlist.reserve(CGIVars::_size());
auto unrefined_env_vars = get_unrefined_env_vars(parEnvList);
for (CGIVars var : CGIVars::_values()) {
auto value = get_env_as<std::string>(var._to_string(), "");
retlist.push_back(std::move(value));
auto it_found = unrefined_env_vars.find(var._to_string());
if (unrefined_env_vars.cend() != it_found)
retlist.push_back(sanitized_utf8(it_found->second));
else
retlist.push_back(std::string());
}
return retlist;
}

View file

@ -43,5 +43,5 @@ namespace tawashi {
SERVER_SOFTWARE
);
std::vector<std::string> cgi_environment_vars();
std::vector<std::string> cgi_environment_vars (const char* const* parEnvList);
} //namespace tawashi

View file

@ -1,48 +0,0 @@
/* Copyright 2017, Michele Santullo
* This file is part of "tawashi".
*
* "tawashi" is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* "tawashi" is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with "tawashi". If not, see <http://www.gnu.org/licenses/>.
*/
#include "get_env.hpp"
#include "duckhandy/lexical_cast.hpp"
#include "sanitized_utf8.hpp"
#include <cstdlib>
namespace tawashi {
boost::optional<std::string> get_env (const char* parName) {
using boost::string_ref;
using boost::make_optional;
using boost::optional;
const char* const raw_getvar = secure_getenv(parName);
if (raw_getvar)
return sanitized_utf8(boost::string_ref(raw_getvar));
else
return optional<std::string>();
}
template <>
std::string get_env_as (const char* parName, const std::string& parDefault) {
auto var = get_env(parName);
return (var ? *var : parDefault);
}
template <>
std::size_t get_env_as (const char* parName, const std::size_t& parDefault) {
using dhandy::lexical_cast;
auto var = get_env(parName);
return (var ? lexical_cast<std::size_t>(*var) : parDefault);
}
} //namespace tawashi

View file

@ -1,39 +0,0 @@
/* Copyright 2017, Michele Santullo
* This file is part of "tawashi".
*
* "tawashi" is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* "tawashi" is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with "tawashi". If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <boost/utility/string_ref.hpp>
#include <string>
#if !defined(_GNU_SOURCE)
# define _GNU_SOURCE
#endif
#include <cstddef>
#include <utility>
#include <boost/optional.hpp>
namespace tawashi {
boost::optional<std::string> get_env (const char* parName);
template <typename A>
A get_env_as (const char* parName, const A& parDefault);
template <>
std::string get_env_as (const char* parName, const std::string& parDefault);
template <>
std::size_t get_env_as (const char* parName, const std::size_t& parDefault);
} //namespace tawashi

View file

@ -19,9 +19,14 @@
#include "duckhandy/lexical_cast.hpp"
#include <cassert>
namespace {
const char* const g_dummy_env_list[] = { nullptr };
} //unnamed namespace
namespace tawashi {
namespace cgi {
FakeEnv::FakeEnv (std::string&& parVariablesIni) :
Env(g_dummy_env_list),
m_variables(std::move(parVariablesIni)),
m_auth_type(m_variables.parsed().at("fake_env").at("auth_type")),
m_content_type(m_variables.parsed().at("fake_env").at("content_type")),