From 85784d231dbf18988bdb563c9b62449da33ab4dc Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Tue, 25 Apr 2017 22:07:02 +0100 Subject: [PATCH] Use houdini escaping instead of easy_curl. Drop dependency on easy_curl. --- {src => oldcode}/curl_wrapper.cpp | 0 {src => oldcode}/curl_wrapper.hpp | 0 src/CMakeLists.txt | 5 +-- src/cgi_env.cpp | 2 +- src/cgi_env.hpp | 4 +- src/cgi_post.cpp | 8 ++-- src/escapist.cpp | 74 +++++++++++++++++++++++++++++++ src/escapist.hpp | 46 +++++++++++++++++++ src/submit_paste_response.cpp | 6 +-- src/submit_paste_response.hpp | 2 +- 10 files changed, 132 insertions(+), 15 deletions(-) rename {src => oldcode}/curl_wrapper.cpp (100%) rename {src => oldcode}/curl_wrapper.hpp (100%) create mode 100644 src/escapist.cpp create mode 100644 src/escapist.hpp diff --git a/src/curl_wrapper.cpp b/oldcode/curl_wrapper.cpp similarity index 100% rename from src/curl_wrapper.cpp rename to oldcode/curl_wrapper.cpp diff --git a/src/curl_wrapper.hpp b/oldcode/curl_wrapper.hpp similarity index 100% rename from src/curl_wrapper.hpp rename to oldcode/curl_wrapper.hpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index a784187..cb59189 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,7 +1,6 @@ project(tawashi VERSION 0.1.1 LANGUAGES CXX) find_package(Boost 1.53.0 REQUIRED COMPONENTS program_options filesystem system) -find_package(CURL REQUIRED) find_package(SourceHighlight REQUIRED) set(CMAKE_CXX_STANDARD 14) @@ -20,7 +19,7 @@ add_executable(${PROJECT_NAME} cgi_env.cpp num_to_token.cpp cgi_post.cpp - curl_wrapper.cpp + escapist.cpp index_response.cpp pastie_response.cpp ini_file.cpp @@ -43,13 +42,11 @@ target_include_directories(${PROJECT_NAME} target_include_directories(${PROJECT_NAME} SYSTEM PRIVATE ${Boost_INCLUDE_DIRS} PRIVATE ${TAWASHI_SOURCE_ROOT}/lib/better-enums - PRIVATE ${CURL_INCLUDE_DIR} PRIVATE ${SourceHighlight_INCLUDE_DIR} ) target_link_libraries(${PROJECT_NAME} PRIVATE ${Boost_LIBRARIES} PRIVATE incredis - PRIVATE ${CURL_LIBRARIES} PRIVATE ${SourceHighlight_LIBRARIES} PRIVATE mstch PRIVATE houdini diff --git a/src/cgi_env.cpp b/src/cgi_env.cpp index 7c82fb9..40bbd03 100644 --- a/src/cgi_env.cpp +++ b/src/cgi_env.cpp @@ -165,7 +165,7 @@ namespace cgi { const auto urlencoded_values = split_env_vars(m_cgi_env[CGIVars::QUERY_STRING]); retval.reserve(urlencoded_values.size()); for (auto& itm : urlencoded_values) { - retval[unescape_string(m_curl, itm.first)] = unescape_string(m_curl, itm.second); + retval[m_houdini.unescape_url(itm.first)] = m_houdini.unescape_url(itm.second); } return retval; } diff --git a/src/cgi_env.hpp b/src/cgi_env.hpp index f2bed1e..9ed7bf1 100644 --- a/src/cgi_env.hpp +++ b/src/cgi_env.hpp @@ -19,7 +19,7 @@ #include "split_get_vars.hpp" #include "duckhandy/compatibility.h" -#include "curl_wrapper.hpp" +#include "escapist.hpp" #include #include #include @@ -67,7 +67,7 @@ namespace tawashi { private: std::vector m_cgi_env; - CurlWrapper m_curl; + Escapist m_houdini; }; } //namespace cgi } //namespace tawashi diff --git a/src/cgi_post.cpp b/src/cgi_post.cpp index 4b04482..d9ab676 100644 --- a/src/cgi_post.cpp +++ b/src/cgi_post.cpp @@ -18,7 +18,7 @@ #include "cgi_post.hpp" #include "cgi_env.hpp" #include "split_get_vars.hpp" -#include "curl_wrapper.hpp" +#include "escapist.hpp" #include #include #include @@ -49,9 +49,11 @@ namespace tawashi { std::back_inserter(original_data) ); - CurlWrapper curl; + Escapist houdini; for (auto& itm : split_env_vars(original_data)) { - map[unescape_string(curl, itm.first)] = unescape_string(curl, itm.second); + std::string key(houdini.unescape_url(itm.first)); + std::string val(houdini.unescape_url(itm.second)); + map[std::move(key)] = std::move(val); } } diff --git a/src/escapist.cpp b/src/escapist.cpp new file mode 100644 index 0000000..d295ec7 --- /dev/null +++ b/src/escapist.cpp @@ -0,0 +1,74 @@ +/* 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 . + */ + +#include "escapist.hpp" +#include "houdini.h" +#include +#include + +namespace tawashi { + Escapist::Escapist() : + m_gh_buf(new(&m_gh_buf_mem) gh_buf GH_BUF_INIT) + { + assert(reinterpret_cast(&m_gh_buf_mem) == reinterpret_cast(m_gh_buf)); + static_assert(sizeof(implem::DummyGHBuf) == sizeof(gh_buf), "Dummy struct has the wrong size"); + static_assert(sizeof(gh_buf) == sizeof(m_gh_buf_mem), "Static memory for gh_buf has the wrong size"); + static_assert(alignof(gh_buf) == alignof(m_gh_buf_mem), "Static memory for gh_buf has the wrong alignment"); + } + + Escapist::~Escapist() noexcept { + gh_buf_free(static_cast(m_gh_buf)); + static_cast(m_gh_buf)->~gh_buf(); + } + + std::string Escapist::unescape_url (const boost::string_ref& parURL) const { + if (parURL.empty()) + return std::string(); + + assert(m_gh_buf); + gh_buf* const buf = static_cast(m_gh_buf); + + const int escaped = houdini_unescape_url( + buf, + reinterpret_cast(parURL.data()), + parURL.size() + ); + if (0 == escaped) + return std::string(parURL.data(), parURL.size()); + else + return std::string(buf->ptr, buf->size); + } + + std::string Escapist::escape_html (const boost::string_ref& parHtml) const { + if (parHtml.empty()) + return std::string(); + + assert(m_gh_buf); + gh_buf* const buf = static_cast(m_gh_buf); + + const int escaped = houdini_escape_html0( + buf, + reinterpret_cast(parHtml.data()), + parHtml.size(), + 1 + ); + if (0 == escaped) + return std::string(parHtml.data(), parHtml.size()); + else + return std::string(buf->ptr, buf->size); + } +} //namespace tawashi diff --git a/src/escapist.hpp b/src/escapist.hpp new file mode 100644 index 0000000..252b732 --- /dev/null +++ b/src/escapist.hpp @@ -0,0 +1,46 @@ +/* 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 . + */ + +#pragma once + +#include +#include +#include + +namespace tawashi { + namespace implem { + //not actually used, only needed to get the size of the actual gh_buf + //without including its full header file + struct DummyGHBuf{ + char *ptr; + size_t asize, size; + }; + } //namespace implem + + class Escapist { + public: + Escapist(); + ~Escapist() noexcept; + + std::string unescape_url (const boost::string_ref& parURL) const; + std::string escape_html (const boost::string_ref& parHtml) const; + + private: + std::aligned_storage::type m_gh_buf_mem; + void* m_gh_buf; + }; +} //namespace tawashi diff --git a/src/submit_paste_response.cpp b/src/submit_paste_response.cpp index d218988..0ff85a9 100644 --- a/src/submit_paste_response.cpp +++ b/src/submit_paste_response.cpp @@ -20,7 +20,6 @@ #include "cgi_post.hpp" #include "num_to_token.hpp" #include "settings_bag.hpp" -#include "curl_wrapper.hpp" #include "duckhandy/compatibility.h" #include "duckhandy/lexical_cast.hpp" #include @@ -99,8 +98,7 @@ namespace tawashi { //TODO: replace boost's lexical_cast with mine when I have some checks //oven invalid inputs const uint32_t duration_int = std::max(std::min((duration.empty() ? 86400U : boost::lexical_cast(duration)), 2628000U), 1U); - CurlWrapper curl; - boost::optional token = submit_to_redis(curl.escape(pastie), duration_int, lang); + boost::optional token = submit_to_redis(pastie, duration_int, lang); if (token) { std::ostringstream oss; oss << base_uri() << '/' << *token; @@ -116,7 +114,7 @@ namespace tawashi { m_error_message << '\n'; } - boost::optional SubmitPasteResponse::submit_to_redis (const std::string& parText, uint32_t parExpiry, const boost::string_ref& parLang) const { + boost::optional SubmitPasteResponse::submit_to_redis (const boost::string_ref& parText, uint32_t parExpiry, const boost::string_ref& parLang) const { auto& redis = this->redis(); if (not redis.is_connected()) return boost::optional(); diff --git a/src/submit_paste_response.hpp b/src/submit_paste_response.hpp index 0ae81e3..9641277 100644 --- a/src/submit_paste_response.hpp +++ b/src/submit_paste_response.hpp @@ -30,7 +30,7 @@ namespace tawashi { private: virtual void on_process() override; virtual void on_send (std::ostream& parStream) override; - boost::optional submit_to_redis (const std::string& parText, uint32_t parExpiry, const boost::string_ref& parLang) const; + boost::optional submit_to_redis (const boost::string_ref& parText, uint32_t parExpiry, const boost::string_ref& parLang) const; std::string m_error_message; };