mirror of
https://github.com/KingDuckZ/kamokan.git
synced 2025-02-09 09:23:56 +00:00
Redirect to error.cgi when saving fails.
Not working yet, still getting there.
This commit is contained in:
parent
73707c2ad0
commit
4d31323bb1
9 changed files with 147 additions and 7 deletions
13
html/error.html.mstch
Normal file
13
html/error.html.mstch
Normal file
|
@ -0,0 +1,13 @@
|
|||
{{> head}}
|
||||
<body>
|
||||
<form action="{{base_uri}}/paste.cgi" method="post" accept-charset="UTF-8">
|
||||
|
||||
{{> topbar}}
|
||||
|
||||
<p>An error occurred: {{error_message}}</p>
|
||||
|
||||
</form>
|
||||
|
||||
|
||||
</body>
|
||||
</html>
|
|
@ -19,6 +19,7 @@
|
|||
#include "submit_paste_response.hpp"
|
||||
#include "pastie_response.hpp"
|
||||
#include "index_response.hpp"
|
||||
#include "error_response.hpp"
|
||||
#include "response_factory.hpp"
|
||||
#include "cgi_env.hpp"
|
||||
#include "ini_file.hpp"
|
||||
|
@ -54,8 +55,13 @@ namespace {
|
|||
}
|
||||
|
||||
template <typename T>
|
||||
std::unique_ptr<tawashi::Response> make_response (const Kakoune::SafePtr<tawashi::SettingsBag>& parSettings, const Kakoune::SafePtr<tawashi::cgi::Env>& parCgiEnv) {
|
||||
return static_cast<std::unique_ptr<tawashi::Response>>(std::make_unique<T>(parSettings, &std::cout, parCgiEnv));
|
||||
std::unique_ptr<tawashi::Response> make_response (
|
||||
const Kakoune::SafePtr<tawashi::SettingsBag>& parSettings,
|
||||
const Kakoune::SafePtr<tawashi::cgi::Env>& parCgiEnv
|
||||
) {
|
||||
return static_cast<std::unique_ptr<tawashi::Response>>(
|
||||
std::make_unique<T>(parSettings, &std::cout, parCgiEnv)
|
||||
);
|
||||
}
|
||||
|
||||
void fill_defaults (tawashi::SettingsBag& parSettings) {
|
||||
|
@ -80,6 +86,7 @@ int main (int parArgc, char* parArgv[], char* parEnvp[]) {
|
|||
using tawashi::IndexResponse;
|
||||
using tawashi::SubmitPasteResponse;
|
||||
using tawashi::PastieResponse;
|
||||
using tawashi::ErrorResponse;
|
||||
using tawashi::Response;
|
||||
|
||||
//Prepare the logger
|
||||
|
@ -108,6 +115,7 @@ int main (int parArgc, char* parArgv[], char* parEnvp[]) {
|
|||
resp_factory.register_maker("index.cgi", &make_response<IndexResponse>);
|
||||
resp_factory.register_maker("", &make_response<IndexResponse>);
|
||||
resp_factory.register_maker("paste.cgi", &make_response<SubmitPasteResponse>);
|
||||
resp_factory.register_maker("error.cgi", &make_response<ErrorResponse>);
|
||||
resp_factory.register_jolly_maker(&make_response<PastieResponse>);
|
||||
|
||||
std::unique_ptr<Response> response = resp_factory.make_response(cgi_env->path_info().substr(1));
|
||||
|
|
|
@ -24,6 +24,7 @@ add_library(${PROJECT_NAME} STATIC
|
|||
settings_bag.cpp
|
||||
sanitized_utf8.cpp
|
||||
tiger.c
|
||||
error_response.cpp
|
||||
)
|
||||
|
||||
target_include_directories(${PROJECT_NAME}
|
||||
|
|
26
src/tawashi_implem/error_reasons.hpp
Normal file
26
src/tawashi_implem/error_reasons.hpp
Normal file
|
@ -0,0 +1,26 @@
|
|||
/* 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
|
||||
|
||||
namespace tawashi {
|
||||
enum ErrorReasons : int {
|
||||
PostLengthNotInRange,
|
||||
PastieNotSaved,
|
||||
UserFlooding
|
||||
};
|
||||
} //namespace tawashi
|
34
src/tawashi_implem/error_response.cpp
Normal file
34
src/tawashi_implem/error_response.cpp
Normal file
|
@ -0,0 +1,34 @@
|
|||
/* 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 "error_response.hpp"
|
||||
#include <boost/algorithm/string/replace.hpp>
|
||||
|
||||
namespace tawashi {
|
||||
ErrorResponse::ErrorResponse (
|
||||
const Kakoune::SafePtr<SettingsBag>& parSettings,
|
||||
std::ostream* parStreamOut,
|
||||
const Kakoune::SafePtr<cgi::Env>& parCgiEnv
|
||||
) :
|
||||
Response(parSettings, parStreamOut, parCgiEnv, false)
|
||||
{
|
||||
}
|
||||
|
||||
void ErrorResponse::on_mustache_prepare (mstch::map& parContext) {
|
||||
parContext["error_message"] = "No error";
|
||||
}
|
||||
} //namespace tawashi
|
39
src/tawashi_implem/error_response.hpp
Normal file
39
src/tawashi_implem/error_response.hpp
Normal file
|
@ -0,0 +1,39 @@
|
|||
/* 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 "response.hpp"
|
||||
#include <boost/utility/string_ref.hpp>
|
||||
|
||||
namespace tawashi {
|
||||
class ErrorResponse : public Response {
|
||||
public:
|
||||
ErrorResponse (
|
||||
const Kakoune::SafePtr<SettingsBag>& parSettings,
|
||||
std::ostream* parStreamOut,
|
||||
const Kakoune::SafePtr<cgi::Env>& parCgiEnv
|
||||
);
|
||||
|
||||
protected:
|
||||
virtual boost::string_ref page_basename() const override { return boost::string_ref("error"); }
|
||||
|
||||
private:
|
||||
virtual void on_mustache_prepare (mstch::map& parContext) override;
|
||||
};
|
||||
} //namespace tawashi
|
||||
|
|
@ -53,8 +53,10 @@ namespace tawashi {
|
|||
const Kakoune::SafePtr<cgi::Env>& parCgiEnv,
|
||||
bool parWantRedis
|
||||
);
|
||||
const cgi::Env& cgi_env() const;
|
||||
|
||||
void change_type (Types parRespType, std::string&& parValue);
|
||||
|
||||
const cgi::Env& cgi_env() const;
|
||||
const boost::string_ref& base_uri() const;
|
||||
virtual boost::string_ref page_basename() const = 0;
|
||||
redis::IncRedis& redis() const;
|
||||
|
|
|
@ -123,16 +123,20 @@ namespace tawashi {
|
|||
if (pastie.size() < settings.as<uint32_t>("min_pastie_size"))
|
||||
return;
|
||||
if (max_sz and pastie.size() > max_sz) {
|
||||
if (settings.as<bool>("truncate_long_pasties"))
|
||||
if (settings.as<bool>("truncate_long_pasties")) {
|
||||
pastie = pastie.substr(0, max_sz);
|
||||
else
|
||||
}
|
||||
else {
|
||||
error_redirect(1, ErrorReasons::PostLengthNotInRange);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
//TODO: replace boost's lexical_cast with mine when I have some checks
|
||||
//over invalid inputs
|
||||
const uint32_t duration_int = std::max(std::min((duration.empty() ? 86400U : boost::lexical_cast<uint32_t>(duration)), 2628000U), 1U);
|
||||
boost::optional<std::string> token = submit_to_redis(pastie, duration_int, lang);
|
||||
|
||||
if (token) {
|
||||
std::ostringstream oss;
|
||||
oss << base_uri() << '/' << *token;
|
||||
|
@ -140,9 +144,12 @@ namespace tawashi {
|
|||
oss << '?' << lang;
|
||||
this->change_type(Response::Location, oss.str());
|
||||
}
|
||||
else {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
boost::optional<std::string> SubmitPasteResponse::submit_to_redis (const boost::string_ref& parText, uint32_t parExpiry, const boost::string_ref& parLang) const {
|
||||
boost::optional<std::string> SubmitPasteResponse::submit_to_redis (const boost::string_ref& parText, uint32_t parExpiry, const boost::string_ref& parLang) {
|
||||
auto& redis = this->redis();
|
||||
if (not redis.is_connected())
|
||||
return boost::optional<std::string>();
|
||||
|
@ -150,6 +157,7 @@ namespace tawashi {
|
|||
std::string ip_hash = hashed_ip(cgi_env().remote_addr());
|
||||
if (redis.get(ip_hash)) {
|
||||
//please wait and submit again
|
||||
error_redirect(1, ErrorReasons::UserFlooding);
|
||||
return boost::optional<std::string>();
|
||||
}
|
||||
|
||||
|
@ -167,6 +175,13 @@ namespace tawashi {
|
|||
return boost::make_optional(token);
|
||||
}
|
||||
|
||||
error_redirect(1, ErrorReasons::PastieNotSaved);
|
||||
return boost::optional<std::string>();
|
||||
}
|
||||
|
||||
void SubmitPasteResponse::error_redirect (int parCode, ErrorReasons parReason) {
|
||||
std::ostringstream oss;
|
||||
oss << base_uri() << "/error.cgi?code=" << parCode << "&reason=" << parReason;
|
||||
this->change_type(Response::Location, oss.str());
|
||||
}
|
||||
} //namespace tawashi
|
||||
|
|
|
@ -18,6 +18,7 @@
|
|||
#pragma once
|
||||
|
||||
#include "response.hpp"
|
||||
#include "error_reasons.hpp"
|
||||
#include <string>
|
||||
#include <boost/optional.hpp>
|
||||
#include <boost/utility/string_ref.hpp>
|
||||
|
@ -36,7 +37,8 @@ namespace tawashi {
|
|||
|
||||
private:
|
||||
virtual void on_process() override;
|
||||
boost::optional<std::string> submit_to_redis (const boost::string_ref& parText, uint32_t parExpiry, const boost::string_ref& parLang) const;
|
||||
boost::optional<std::string> submit_to_redis (const boost::string_ref& parText, uint32_t parExpiry, const boost::string_ref& parLang);
|
||||
void error_redirect (int parCode, ErrorReasons parReason);
|
||||
|
||||
std::string m_error_message;
|
||||
};
|
||||
|
|
Loading…
Add table
Reference in a new issue