Use houdini escaping instead of easy_curl.

Drop dependency on easy_curl.
This commit is contained in:
King_DuckZ 2017-04-25 22:07:02 +01:00
parent c75c23fa36
commit 85784d231d
10 changed files with 132 additions and 15 deletions

View File

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

View File

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

View File

@ -19,7 +19,7 @@
#include "split_get_vars.hpp"
#include "duckhandy/compatibility.h"
#include "curl_wrapper.hpp"
#include "escapist.hpp"
#include <vector>
#include <string>
#include <boost/utility/string_ref.hpp>
@ -67,7 +67,7 @@ namespace tawashi {
private:
std::vector<std::string> m_cgi_env;
CurlWrapper m_curl;
Escapist m_houdini;
};
} //namespace cgi
} //namespace tawashi

View File

@ -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 <iostream>
#include <iterator>
#include <algorithm>
@ -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);
}
}

74
src/escapist.cpp Normal file
View File

@ -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 <http://www.gnu.org/licenses/>.
*/
#include "escapist.hpp"
#include "houdini.h"
#include <cstddef>
#include <cassert>
namespace tawashi {
Escapist::Escapist() :
m_gh_buf(new(&m_gh_buf_mem) gh_buf GH_BUF_INIT)
{
assert(reinterpret_cast<uintptr_t>(&m_gh_buf_mem) == reinterpret_cast<uintptr_t>(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<gh_buf*>(m_gh_buf));
static_cast<gh_buf*>(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<gh_buf*>(m_gh_buf);
const int escaped = houdini_unescape_url(
buf,
reinterpret_cast<const uint8_t*>(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<gh_buf*>(m_gh_buf);
const int escaped = houdini_escape_html0(
buf,
reinterpret_cast<const uint8_t*>(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

46
src/escapist.hpp Normal file
View File

@ -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 <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <boost/utility/string_ref.hpp>
#include <string>
#include <memory>
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<sizeof(implem::DummyGHBuf), alignof(implem::DummyGHBuf)>::type m_gh_buf_mem;
void* m_gh_buf;
};
} //namespace tawashi

View File

@ -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 <ciso646>
@ -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<uint32_t>(duration)), 2628000U), 1U);
CurlWrapper curl;
boost::optional<std::string> token = submit_to_redis(curl.escape(pastie), duration_int, lang);
boost::optional<std::string> 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<std::string> SubmitPasteResponse::submit_to_redis (const std::string& 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) const {
auto& redis = this->redis();
if (not redis.is_connected())
return boost::optional<std::string>();

View File

@ -30,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, 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) const;
std::string m_error_message;
};