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

Merge branch 'mime_karma'

This commit is contained in:
King_DuckZ 2017-05-31 19:54:47 +01:00
commit 22a4503e0e
3 changed files with 165 additions and 14 deletions

View file

@ -28,15 +28,33 @@
#include <boost/spirit/include/qi_char_class.hpp>
#include <boost/spirit/include/qi_kleene.hpp>
#include <boost/spirit/include/qi_alternative.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/spirit/include/qi_difference.hpp>
#include <boost/spirit/include/karma_char.hpp>
#include <boost/spirit/include/karma_generate.hpp>
#include <boost/spirit/include/karma_operator.hpp>
#include <boost/spirit/include/karma_kleene.hpp>
#include <boost/spirit/include/karma_stream.hpp>
#include <boost/spirit/include/karma_string.hpp>
#include <boost/spirit/include/karma_alternative.hpp>
#include <boost/spirit/include/karma_rule.hpp>
#include <boost/spirit/include/karma_eps.hpp>
#include <boost/fusion/include/std_pair.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/phoenix/function/lazy_prelude.hpp>
#include <boost/phoenix/core.hpp>
#include <boost/phoenix/object/construct.hpp>
#include <boost/phoenix/stl/container.hpp>
#include <boost/phoenix/bind/bind_member_function.hpp>
#include <boost/phoenix/operator.hpp>
#include <boost/functional/hash.hpp>
#include <boost/phoenix/operator/arithmetic.hpp>
#include <boost/phoenix/operator/self.hpp>
#include <boost/phoenix/stl/algorithm/transformation.hpp>
#include <cassert>
#include <iterator>
#include <sstream>
// The Internet Media Type [9 <#ref-9>] of the attached entity. The syntax is
// the same as the HTTP Content-Type header.
@ -93,17 +111,16 @@ namespace tawashi {
m_master_string(parString),
m_begin(m_master_string->cbegin())
{
namespace px = boost::phoenix;
using boost::spirit::ascii::space;
using boost::spirit::qi::char_;
using boost::spirit::qi::lit;
using boost::spirit::qi::alnum;
using boost::spirit::qi::raw;
using boost::spirit::qi::_val;
using boost::spirit::qi::lexeme;
using boost::string_ref;
using boost::spirit::_1;
using boost::phoenix::begin;
using boost::phoenix::size;
namespace px = boost::phoenix;
content_type = -media_type;
media_type = type >> "/" >> subtype >> *(lit(";") >> parameter);
@ -113,12 +130,45 @@ namespace tawashi {
attribute = token.alias();
value = token | quoted_string;
token = raw[+(char_ - ';' - '/' - '=')][_val = px::bind(&string_ref::substr, px::construct<string_ref>(px::ref(*m_master_string)), begin(_1) - px::ref(m_begin), size(_1))];
quoted_string = raw[lexeme['"' >> +(char_ - '"') >> '"']][_val = px::bind(&string_ref::substr, px::construct<string_ref>(px::ref(*m_master_string)), begin(_1) - px::ref(m_begin), size(_1))];
token = raw[+(alnum | char_("_.-"))][
_val = px::bind(
&string_ref::substr, px::construct<string_ref>(px::ref(*m_master_string)),
px::begin(_1) - px::ref(m_begin),
px::size(_1)
)
];
quoted_string = raw[
lexeme[
lit('"') >>
*(char_ - '"') >>
'"'
]
][_val = px::bind(
&string_ref::substr, px::construct<string_ref>(px::ref(*m_master_string)),
px::begin(_1) + 1 - px::ref(m_begin),
px::size(_1) - 2
)];
}
struct simple_token_checker {
typedef bool result_type;
template <typename V>
bool operator() (const V& parIn) const {
std::cout << "simple_token_checker returning ";
if (std::find(std::begin(parIn), std::end(parIn), ' ') == std::end(parIn)) {
std::cout << "true\n";
return true;
}
else {
std::cout << "false\n";
return false;
}
}
};
} //unnamed namespace
SplitMime split_mime (const std::string* parMime, bool& parParseOk, int& parParsedCharCount) {
SplitMime string_to_mime (const std::string* parMime, bool& parParseOk, int& parParsedCharCount) {
using boost::spirit::qi::blank;
using boost::spirit::qi::blank_type;
@ -142,4 +192,41 @@ namespace tawashi {
assert(parParsedCharCount >= 0);
return result;
}
std::string mime_to_string (const SplitMime& parMime, bool& parWriteOk) {
namespace px = boost::phoenix;
using boost::string_ref;
using boost::spirit::karma::generate;
using boost::spirit::karma::char_;
using boost::spirit::karma::string;
using boost::spirit::karma::rule;
using boost::spirit::karma::alnum;
using boost::spirit::karma::eps;
using boost::spirit::_val;
if (parMime.type.empty() or parMime.subtype.empty()) {
parWriteOk = false;
return std::string();
}
px::function<simple_token_checker> is_simple_token;
std::string retval;
std::back_insert_iterator<std::string> out_iter(retval);
rule<std::back_insert_iterator<std::string>, string_ref()> token;
rule<std::back_insert_iterator<std::string>, string_ref()> quoted_string;
rule<std::back_insert_iterator<std::string>, string_ref()> param_value;
token %= eps(is_simple_token(_val)) << *(alnum | char_("._-"));
quoted_string %= '"' << string << '"';
param_value %= token | quoted_string;
parWriteOk = generate(
out_iter,
string << "/" << string << *(
"; " << string << "=" << param_value
),
parMime
);
return retval;
}
} //namespace tawashi

View file

@ -30,5 +30,6 @@ namespace tawashi {
MimeParametersMapType parameters;
};
SplitMime split_mime (const std::string* parMime, bool& parParseOk, int& parParsedCharCount);
SplitMime string_to_mime (const std::string* parMime, bool& parParseOk, int& parParsedCharCount);
std::string mime_to_string (const SplitMime& parMime, bool& parWriteOk);
} //namespace tawashi

View file

@ -18,25 +18,88 @@
#include "catch.hpp"
#include "mime_split.hpp"
TEST_CASE ("Test the Mime-type splitter", "[mime][parser]") {
namespace {
std::string to_string (const boost::string_ref& parRef) {
return std::string(parRef.data(), parRef.size());
}
} //unnamed namespace
TEST_CASE ("Test the string_to_mime parser", "[mime][parser]") {
using tawashi::SplitMime;
using tawashi::split_mime;
using tawashi::string_to_mime;
bool ok;
int parsed_count;
{
std::string test("application/x-javascript; charset=UTF-8");
SplitMime split = split_mime(&test, ok, parsed_count);
std::string curr_val;
SplitMime split = string_to_mime(&test, ok, parsed_count);
REQUIRE(ok);
CHECK(test.size() == parsed_count);
CHECK(split.type == "application");
CHECK(split.subtype == "x-javascript");
REQUIRE(split.parameters.size() == 1);
CHECK(split.parameters.find("charset") != split.parameters.end());
CHECK(split.parameters.at("charset") == "UTF-8");
curr_val = to_string(split.parameters.at("charset"));
CHECK(curr_val == "UTF-8");
}
{
std::string test("image/jpeg; filename=genome.jpeg; modification-date=\"Wed, 12 Feb 1997 16:29:51 -0500\"");
std::string curr_val;
SplitMime split = string_to_mime(&test, ok, parsed_count);
REQUIRE(ok);
CHECK(test.size() == parsed_count);
CHECK(split.type == "image");
CHECK(split.subtype == "jpeg");
REQUIRE(split.parameters.size() == 2);
CHECK(split.parameters.find("filename") != split.parameters.end());
curr_val = to_string(split.parameters.at("filename"));
CHECK(curr_val == "genome.jpeg");
CHECK(split.parameters.find("modification-date") != split.parameters.end());
curr_val = to_string(split.parameters.at("modification-date"));
CHECK(curr_val == "Wed, 12 Feb 1997 16:29:51 -0500");
}
}
TEST_CASE ("Test the mime_to_string parser", "[mime][parser]") {
using tawashi::SplitMime;
using tawashi::mime_to_string;
std::string type = "image";
std::string subtype = "jpeg";
bool ok;
{
SplitMime test;
test.type = type;
test.subtype = subtype;
std::string result = mime_to_string(test, ok);
REQUIRE(ok);
CHECK(result == "image/jpeg");
}
{
SplitMime test;
test.type = type;
test.subtype = subtype;
test.parameters["filename"] = "genome.jpeg";
std::string result = mime_to_string(test, ok);
REQUIRE(ok);
CHECK(result == "image/jpeg; filename=genome.jpeg");
}
{
SplitMime test;
test.type = type;
test.subtype = subtype;
test.parameters["modification-date"] = "Wed, 12 Feb 1997 16:29:51 -0500";
test.parameters["filename"] = "genome.jpeg";
std::string result = mime_to_string(test, ok);
REQUIRE(ok);
CHECK(result == "image/jpeg; filename=genome.jpeg; modification-date=\"Wed, 12 Feb 1997 16:29:51 -0500\"");
}
}