1
0
Fork 0
mirror of https://github.com/KingDuckZ/kamokan.git synced 2024-12-27 21:35:41 +00:00

Allow users to specify min/max valid pastie sizes.

This commit is contained in:
King_DuckZ 2017-04-24 19:09:43 +01:00
parent 79e82e2489
commit 3c10d624e3
6 changed files with 31 additions and 7 deletions

View file

@ -65,6 +65,9 @@ namespace {
parSettings.add_default("base_uri", "http://127.0.0.1");
parSettings.add_default("website_root", "");
parSettings.add_default("langmap_dir", "/usr/share/source-highlight");
parSettings.add_default("min_pastie_size", "10");
parSettings.add_default("max_pastie_size", "10000");
parSettings.add_default("truncate_long_pasties", "false");
}
} //unnamed namespace

View file

@ -28,6 +28,7 @@
#include <sstream>
#include <functional>
#include <boost/optional.hpp>
#include <cstdint>
namespace tawashi {
namespace {
@ -135,7 +136,7 @@ namespace tawashi {
if (m_redis) {
m_redis->wait_for_connect();
auto batch = m_redis->make_batch();
batch.select(m_settings->as<int>("redis_db"));
batch.select(m_settings->as<uint32_t>("redis_db"));
batch.client_setname("tawashi_v" STRINGIZE(VERSION_MAJOR) "." STRINGIZE(VERSION_MINOR) "." STRINGIZE(VERSION_PATCH));
batch.throw_if_failed();
}
@ -198,4 +199,9 @@ namespace tawashi {
assert(m_redis);
return *m_redis;
}
const SettingsBag& Response::settings() const {
assert(m_settings);
return *m_settings;
}
} //namespace tawashi

View file

@ -51,6 +51,7 @@ namespace tawashi {
const std::string& page_basename() const;
std::string load_mustache() const;
redis::IncRedis& redis() const;
const SettingsBag& settings() const;
private:
virtual void on_process();

View file

@ -73,12 +73,12 @@ namespace tawashi {
}
template <>
int SettingsBag::as (boost::string_ref parIndex) const {
return dhandy::lexical_cast<int>(this->at(parIndex));
uint32_t SettingsBag::as (boost::string_ref parIndex) const {
return dhandy::lexical_cast<uint32_t>(this->at(parIndex));
}
template std::string SettingsBag::as<std::string> (boost::string_ref parIndex) const;
template bool SettingsBag::as<bool> (boost::string_ref parIndex) const;
template uint16_t SettingsBag::as<uint16_t> (boost::string_ref parIndex) const;
template int SettingsBag::as<int> (boost::string_ref parIndex) const;
template uint32_t SettingsBag::as<uint32_t> (boost::string_ref parIndex) const;
} //namespace tawashi

View file

@ -19,6 +19,7 @@
#include "incredis/incredis.hpp"
#include "cgi_post.hpp"
#include "num_to_token.hpp"
#include "settings_bag.hpp"
#include <ciso646>
#include <sstream>
@ -40,7 +41,19 @@ namespace tawashi {
return;
}
boost::optional<std::string> token = submit_to_redis(post_data_it->second);
const SettingsBag& settings = this->settings();
const auto max_sz = settings.as<uint32_t>("max_pastie_size");
boost::string_ref pastie(post_data_it->second);
if (post_data_it->second.size() < settings.as<uint32_t>("min_pastie_size"))
return;
if (max_sz and post_data_it->second.size() > max_sz) {
if (settings.as<bool>("truncate_long_pasties"))
pastie = pastie.substr(0, max_sz);
else
return;
}
boost::optional<std::string> token = submit_to_redis(pastie);
if (token) {
std::ostringstream oss;
oss << base_uri() << '/' << *token;
@ -54,7 +67,7 @@ namespace tawashi {
m_error_message << '\n';
}
boost::optional<std::string> SubmitPasteResponse::submit_to_redis (const std::string& parText) const {
boost::optional<std::string> SubmitPasteResponse::submit_to_redis (boost::string_ref parText) const {
auto& redis = this->redis();
if (not redis.is_connected())
return boost::optional<std::string>();

View file

@ -20,6 +20,7 @@
#include "response.hpp"
#include <string>
#include <boost/optional.hpp>
#include <boost/utility/string_ref.hpp>
namespace tawashi {
class SubmitPasteResponse : public Response {
@ -29,7 +30,7 @@ namespace tawashi {
private:
virtual void on_process() override;
virtual void on_send (std::ostream& parStream) override;
boost::optional<std::string> submit_to_redis (const std::string& parText) const;
boost::optional<std::string> submit_to_redis (boost::string_ref parText) const;
std::string m_error_message;
};