diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2021-06-15 12:54:47 +0100 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2021-06-15 12:54:47 +0100 |
commit | daeba37c5557e5c33d6e752a204b723e657532a4 (patch) | |
tree | 2bb6a9fde586901c32b66f94356505ff5a599696 /src/libserver/css | |
parent | f1f2f27585576a2b37e0c1d7440f4dece0b58a59 (diff) | |
download | rspamd-daeba37c5557e5c33d6e752a204b723e657532a4.tar.gz rspamd-daeba37c5557e5c33d6e752a204b723e657532a4.zip |
[Rework] Html/CSS: Remove css C bindings as they are useless now
Diffstat (limited to 'src/libserver/css')
-rw-r--r-- | src/libserver/css/css.cxx | 56 | ||||
-rw-r--r-- | src/libserver/css/css.h | 45 | ||||
-rw-r--r-- | src/libserver/css/css.hxx | 7 | ||||
-rw-r--r-- | src/libserver/css/css_parser.cxx | 36 | ||||
-rw-r--r-- | src/libserver/css/css_parser.hxx | 4 | ||||
-rw-r--r-- | src/libserver/css/parse_error.hxx | 4 |
6 files changed, 39 insertions, 113 deletions
diff --git a/src/libserver/css/css.cxx b/src/libserver/css/css.cxx index 9b0e02230..12f7753c7 100644 --- a/src/libserver/css/css.cxx +++ b/src/libserver/css/css.cxx @@ -14,7 +14,6 @@ * limitations under the License. */ -#include "css.h" #include "css.hxx" #include "contrib/robin-hood/robin_hood.h" #include "css_parser.hxx" @@ -23,45 +22,6 @@ #define DOCTEST_CONFIG_IMPLEMENT #include "doctest/doctest.h" -static void -rspamd_css_dtor(void *p) -{ - rspamd::css::css_style_sheet *style = - reinterpret_cast<rspamd::css::css_style_sheet *>(p); - - delete style; -} - -rspamd_css_ptr -rspamd_css_parse_style(rspamd_mempool_t *pool, const gchar *begin, gsize len, - rspamd_css_ptr existing_style, - GError **err) -{ - auto parse_res = rspamd::css::parse_css(pool, {(const char* )begin, len}, - reinterpret_cast<rspamd::css::css_style_sheet*>(existing_style)); - - if (parse_res.has_value()) { - /* - * Detach style pointer from the unique_ptr as it will be managed by - * C memory pool - */ - auto *detached_style = reinterpret_cast<rspamd_css_ptr>(parse_res.value().release()); - - /* We attach dtor merely if the existing style is not null */ - if (!existing_style) { - rspamd_mempool_add_destructor(pool, rspamd_css_dtor, (void *) detached_style); - } - - return detached_style; - } - else { - g_set_error(err, g_quark_from_static_string("css"), - static_cast<int>(parse_res.error().type), - "parse error"); - return nullptr; - } -} - namespace rspamd::css { INIT_LOG_MODULE_PUBLIC(css); @@ -136,4 +96,20 @@ css_style_sheet::add_selector_rule(std::unique_ptr<css_selector> &&selector, } } +auto +css_parse_style(rspamd_mempool_t *pool, + std::string_view input, + std::shared_ptr<css_style_sheet> &&existing) + -> css_return_pair +{ + auto parse_res = rspamd::css::parse_css(pool, input, + std::forward<std::shared_ptr<css_style_sheet>>(existing)); + + if (parse_res.has_value()) { + return std::make_pair(parse_res.value(), css_parse_error()); + } + + return std::make_pair(nullptr, parse_res.error()); +} + }
\ No newline at end of file diff --git a/src/libserver/css/css.h b/src/libserver/css/css.h deleted file mode 100644 index 607f1fa2c..000000000 --- a/src/libserver/css/css.h +++ /dev/null @@ -1,45 +0,0 @@ -/*- - * Copyright 2021 Vsevolod Stakhov - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef RSPAMD_CSS_H -#define RSPAMD_CSS_H - -#include "config.h" -#include "mem_pool.h" - -#ifdef __cplusplus -extern "C" { -#endif -typedef void * rspamd_css_ptr; - -rspamd_css_ptr rspamd_css_parse_style (rspamd_mempool_t *pool, - const gchar *begin, - gsize len, - rspamd_css_ptr existing_style, - GError **err); - -/* - * Unescape css - */ -const gchar *rspamd_css_unescape (rspamd_mempool_t *pool, - const guchar *begin, - gsize len, - gsize *outlen); -#ifdef __cplusplus -} -#endif - -#endif //RSPAMD_CSS_H diff --git a/src/libserver/css/css.hxx b/src/libserver/css/css.hxx index 739ad3251..a169a1052 100644 --- a/src/libserver/css/css.hxx +++ b/src/libserver/css/css.hxx @@ -21,7 +21,6 @@ #include <string> #include <memory> #include "logger.h" -#include "css.h" #include "css_rule.hxx" #include "css_selector.hxx" @@ -50,6 +49,12 @@ private: std::unique_ptr<impl> pimpl; }; +using css_return_pair = std::pair<std::shared_ptr<css_style_sheet>, css_parse_error>; +auto css_parse_style(rspamd_mempool_t *pool, + std::string_view input, + std::shared_ptr<css_style_sheet> &&existing) -> + css_return_pair; + } #endif //RSPAMD_CSS_H
\ No newline at end of file diff --git a/src/libserver/css/css_parser.cxx b/src/libserver/css/css_parser.cxx index d40520381..6526ebc57 100644 --- a/src/libserver/css/css_parser.cxx +++ b/src/libserver/css/css_parser.cxx @@ -148,15 +148,14 @@ class css_parser { public: css_parser(void) = delete; /* Require mempool to be set for logging */ explicit css_parser(rspamd_mempool_t *pool) : pool (pool) { - /* Int this case we need to remove style in case of parser error */ - owned_style = true; + style_object.reset(); } /* * This constructor captures existing via unique_ptr, but it does not * destruct it on errors (we assume that it is owned somewhere else) */ - explicit css_parser(css_style_sheet *existing, rspamd_mempool_t *pool) : + explicit css_parser(std::shared_ptr<css_style_sheet> &&existing, rspamd_mempool_t *pool) : style_object(existing), pool(pool) {} /* @@ -169,9 +168,9 @@ public: std::unique_ptr<css_consumed_block> consume_css_rule(const std::string_view &sv); bool consume_input(const std::string_view &sv); - auto get_object_maybe(void) -> tl::expected<std::unique_ptr<css_style_sheet>, css_parse_error> { + auto get_object_maybe(void) -> tl::expected<std::shared_ptr<css_style_sheet>, css_parse_error> { if (style_object) { - return std::move(style_object); + return style_object; } return tl::make_unexpected(error); @@ -180,15 +179,8 @@ public: /* Helper parser methods */ static bool need_unescape(const std::string_view &sv); - ~css_parser() { - if (!owned_style && style_object) { - /* Avoid double free */ - (void)style_object.release(); - } - } - private: - std::unique_ptr<css_style_sheet> style_object; + std::shared_ptr<css_style_sheet> style_object; std::unique_ptr<css_tokeniser> tokeniser; css_parse_error error; @@ -197,7 +189,6 @@ private: int rec_level = 0; const int max_rec = 20; bool eof = false; - bool owned_style = false; /* Consumers */ auto component_value_consumer(std::unique_ptr<css_consumed_block> &top) -> bool; @@ -618,7 +609,7 @@ bool css_parser::consume_input(const std::string_view &sv) } if (!style_object) { - style_object = std::make_unique<css_style_sheet>(pool); + style_object = std::make_shared<css_style_sheet>(pool); } for (auto &&rule : rules) { @@ -772,10 +763,10 @@ get_rules_parser_functor(rspamd_mempool_t *pool, * Wrapper for the parser */ auto parse_css(rspamd_mempool_t *pool, const std::string_view &st, - css_style_sheet *other) - -> tl::expected<std::unique_ptr<css_style_sheet>, css_parse_error> + std::shared_ptr<css_style_sheet> &&other) + -> tl::expected<std::shared_ptr<css_style_sheet>, css_parse_error> { - css_parser parser(other, pool); + css_parser parser(std::forward<std::shared_ptr<css_style_sheet>>(other), pool); std::string_view processed_input; if (css_parser::need_unescape(st)) { @@ -878,14 +869,13 @@ TEST_SUITE("css parser") { } /* We now merge all styles together */ - css_style_sheet *merged = nullptr; + std::shared_ptr<css_style_sheet> merged; for (const auto &c : cases) { - auto ret = parse_css(pool, c, merged); - /* Avoid destruction as we are using this to interoperate with C */ - merged = ret->release(); + auto ret = parse_css(pool, c, std::move(merged)); + merged.swap(ret.value()); } - CHECK(merged != nullptr); + CHECK(merged.get() != nullptr); rspamd_mempool_delete(pool); } diff --git a/src/libserver/css/css_parser.hxx b/src/libserver/css/css_parser.hxx index 1e0762d78..35b51ed23 100644 --- a/src/libserver/css/css_parser.hxx +++ b/src/libserver/css/css_parser.hxx @@ -193,8 +193,8 @@ class css_style_sheet; * Update the existing stylesheet with another stylesheet */ auto parse_css(rspamd_mempool_t *pool, const std::string_view &st, - css_style_sheet *other) - -> tl::expected<std::unique_ptr<css_style_sheet>, css_parse_error>; + std::shared_ptr<css_style_sheet> &&other) + -> tl::expected<std::shared_ptr<css_style_sheet>, css_parse_error>; /* * Creates a functor to consume css selectors sequence diff --git a/src/libserver/css/parse_error.hxx b/src/libserver/css/parse_error.hxx index 458469afc..450c49d68 100644 --- a/src/libserver/css/parse_error.hxx +++ b/src/libserver/css/parse_error.hxx @@ -33,6 +33,7 @@ enum class css_parse_error_type { PARSE_ERROR_BAD_NESTING, PARSE_ERROR_NYI, PARSE_ERROR_UNKNOWN_ERROR, + PARSE_ERROR_NO_ERROR, }; struct css_parse_error { @@ -41,9 +42,8 @@ struct css_parse_error { explicit css_parse_error (css_parse_error_type type, const std::string &description) : type(type), description(description) {} - explicit css_parse_error (css_parse_error_type type) : + explicit css_parse_error (css_parse_error_type type = css_parse_error_type::PARSE_ERROR_NO_ERROR) : type(type) {} - css_parse_error() = default; }; } |