commit 0c022e5: [Project] Skeleton of the css library

Vsevolod Stakhov vsevolod at highsecure.ru
Fri Jan 22 16:00:14 UTC 2021


Author: Vsevolod Stakhov
Date: 2021-01-14 19:04:52 +0000
URL: https://github.com/rspamd/rspamd/commit/0c022e5276b526a98417b969410e9e553e8c9c1d

[Project] Skeleton of the css library

---
 src/libserver/CMakeLists.txt                       |   4 +-
 src/libserver/css/CMakeLists.txt                   |   4 +
 .../base64/base64.h => libserver/css/css.cxx}      |  19 +---
 .../base64/base64.h => libserver/css/css.h}        |  19 +---
 .../base64/base64.h => libserver/css/css.hxx}      |  24 ++---
 .../base64.h => libserver/css/css_property.cxx}    |  21 +++--
 src/libserver/css/css_property.hxx                 |  53 +++++++++++
 .../base64.h => libserver/css/css_value.cxx}       |  21 +++--
 src/libserver/css/css_value.hxx                    | 100 +++++++++++++++++++++
 src/libserver/css/parse_error.hxx                  |  51 +++++++++++
 10 files changed, 251 insertions(+), 65 deletions(-)

diff --git a/src/libserver/CMakeLists.txt b/src/libserver/CMakeLists.txt
index 635c65b13..c6d4034ed 100644
--- a/src/libserver/CMakeLists.txt
+++ b/src/libserver/CMakeLists.txt
@@ -1,4 +1,5 @@
 # Librspamdserver
+ADD_SUBDIRECTORY(css)
 SET(LIBRSPAMDSERVERSRC
 				${CMAKE_CURRENT_SOURCE_DIR}/cfg_utils.c
 				${CMAKE_CURRENT_SOURCE_DIR}/cfg_rcl.c
@@ -33,7 +34,8 @@ SET(LIBRSPAMDSERVERSRC
 				${CMAKE_CURRENT_SOURCE_DIR}/http/http_router.c
 				${CMAKE_CURRENT_SOURCE_DIR}/http/http_context.c
 				${CMAKE_CURRENT_SOURCE_DIR}/maps/map.c
-				${CMAKE_CURRENT_SOURCE_DIR}/maps/map_helpers.c)
+				${CMAKE_CURRENT_SOURCE_DIR}/maps/map_helpers.c
+				${LIBCSSSRC})
 
 # Librspamd-server
 SET(RSPAMD_SERVER ${LIBRSPAMDSERVERSRC} PARENT_SCOPE)
diff --git a/src/libserver/css/CMakeLists.txt b/src/libserver/css/CMakeLists.txt
new file mode 100644
index 000000000..429a7373f
--- /dev/null
+++ b/src/libserver/css/CMakeLists.txt
@@ -0,0 +1,4 @@
+SET(LIBCSSSRC    "${CMAKE_CURRENT_SOURCE_DIR}/css.cxx"
+                "${CMAKE_CURRENT_SOURCE_DIR}/css_property.cxx"
+                "${CMAKE_CURRENT_SOURCE_DIR}/css_value.cxx"
+                PARENT_SCOPE)
\ No newline at end of file
diff --git a/src/libcryptobox/base64/base64.h b/src/libserver/css/css.cxx
similarity index 66%
copy from src/libcryptobox/base64/base64.h
copy to src/libserver/css/css.cxx
index e2be379b5..09388e739 100644
--- a/src/libcryptobox/base64/base64.h
+++ b/src/libserver/css/css.cxx
@@ -1,5 +1,5 @@
 /*-
- * Copyright 2016 Vsevolod Stakhov
+ * 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.
@@ -13,19 +13,6 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-#ifndef SRC_LIBCRYPTOBOX_BASE64_BASE64_H_
-#define SRC_LIBCRYPTOBOX_BASE64_BASE64_H_
 
-#include "config.h"
-
-#ifdef  __cplusplus
-extern "C" {
-#endif
-
-const char *base64_load (void);
-
-#ifdef  __cplusplus
-}
-#endif
-
-#endif /* SRC_LIBCRYPTOBOX_BASE64_BASE64_H_ */
+#include "css.h"
+#include "css.hxx"
diff --git a/src/libcryptobox/base64/base64.h b/src/libserver/css/css.h
similarity index 66%
copy from src/libcryptobox/base64/base64.h
copy to src/libserver/css/css.h
index e2be379b5..b2c820616 100644
--- a/src/libcryptobox/base64/base64.h
+++ b/src/libserver/css/css.h
@@ -1,5 +1,5 @@
 /*-
- * Copyright 2016 Vsevolod Stakhov
+ * 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.
@@ -13,19 +13,8 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-#ifndef SRC_LIBCRYPTOBOX_BASE64_BASE64_H_
-#define SRC_LIBCRYPTOBOX_BASE64_BASE64_H_
 
-#include "config.h"
+#ifndef RSPAMD_CSS_H
+#define RSPAMD_CSS_H
 
-#ifdef  __cplusplus
-extern "C" {
-#endif
-
-const char *base64_load (void);
-
-#ifdef  __cplusplus
-}
-#endif
-
-#endif /* SRC_LIBCRYPTOBOX_BASE64_BASE64_H_ */
+#endif //RSPAMD_CSS_H
diff --git a/src/libcryptobox/base64/base64.h b/src/libserver/css/css.hxx
similarity index 66%
copy from src/libcryptobox/base64/base64.h
copy to src/libserver/css/css.hxx
index e2be379b5..86a8457ca 100644
--- a/src/libcryptobox/base64/base64.h
+++ b/src/libserver/css/css.hxx
@@ -1,5 +1,5 @@
 /*-
- * Copyright 2016 Vsevolod Stakhov
+ * 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.
@@ -13,19 +13,21 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-#ifndef SRC_LIBCRYPTOBOX_BASE64_BASE64_H_
-#define SRC_LIBCRYPTOBOX_BASE64_BASE64_H_
+#ifndef RSPAMD_CSS_HXX
+#define RSPAMD_CSS_HXX
 
-#include "config.h"
+#include <string>
 
-#ifdef  __cplusplus
-extern "C" {
-#endif
+namespace rspamd {
 
-const char *base64_load (void);
+namespace css {
+
+struct css_element {
+
+};
+
+}
 
-#ifdef  __cplusplus
 }
-#endif
 
-#endif /* SRC_LIBCRYPTOBOX_BASE64_BASE64_H_ */
+#endif //RSPAMD_CSS_H
\ No newline at end of file
diff --git a/src/libcryptobox/base64/base64.h b/src/libserver/css/css_property.cxx
similarity index 66%
copy from src/libcryptobox/base64/base64.h
copy to src/libserver/css/css_property.cxx
index e2be379b5..483856a84 100644
--- a/src/libcryptobox/base64/base64.h
+++ b/src/libserver/css/css_property.cxx
@@ -1,5 +1,5 @@
 /*-
- * Copyright 2016 Vsevolod Stakhov
+ * 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.
@@ -13,19 +13,18 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-#ifndef SRC_LIBCRYPTOBOX_BASE64_BASE64_H_
-#define SRC_LIBCRYPTOBOX_BASE64_BASE64_H_
 
-#include "config.h"
+#include "css_property.hxx"
 
-#ifdef  __cplusplus
-extern "C" {
-#endif
 
-const char *base64_load (void);
+namespace rspamd {
 
-#ifdef  __cplusplus
+namespace css {
+
+auto css_property::from_bytes (const char *input, size_t inlen) -> tl::expected<css_property,css_parse_error>
+{
+	return tl::unexpected{css_parse_error(css_parse_error_type::PARSE_ERROR_NYI)};
 }
-#endif
 
-#endif /* SRC_LIBCRYPTOBOX_BASE64_BASE64_H_ */
+}
+}
\ No newline at end of file
diff --git a/src/libserver/css/css_property.hxx b/src/libserver/css/css_property.hxx
new file mode 100644
index 000000000..36a3fee07
--- /dev/null
+++ b/src/libserver/css/css_property.hxx
@@ -0,0 +1,53 @@
+/*-
+ * 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_PROPERTY_HXX
+#define RSPAMD_CSS_PROPERTY_HXX
+
+#include <string>
+#include "parse_error.hxx"
+#include "contrib/expected/expected.hpp"
+
+namespace rspamd {
+
+namespace css {
+
+/*
+ * To be extended with properties that are interesting from the email
+ * point of view
+ */
+enum class css_property_type {
+	PROPERTY_FONT,
+	PROPERTY_COLOR,
+	PROPERTY_BGCOLOR,
+	PROPERTY_BACKGROUND,
+	PROPERTY_HEIGHT,
+	PROPERTY_WIDTH,
+	PROPERTY_DISPLAY,
+	PROPERTY_VISIBILITY,
+};
+
+struct css_property {
+	css_property_type type;
+	static tl::expected<css_property,css_parse_error> from_bytes (const char *input,
+																 size_t inlen);
+};
+
+}
+
+}
+
+#endif //RSPAMD_CSS_PROPERTY_HXX
diff --git a/src/libcryptobox/base64/base64.h b/src/libserver/css/css_value.cxx
similarity index 66%
copy from src/libcryptobox/base64/base64.h
copy to src/libserver/css/css_value.cxx
index e2be379b5..29eb4aed2 100644
--- a/src/libcryptobox/base64/base64.h
+++ b/src/libserver/css/css_value.cxx
@@ -1,5 +1,5 @@
 /*-
- * Copyright 2016 Vsevolod Stakhov
+ * 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.
@@ -13,19 +13,18 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-#ifndef SRC_LIBCRYPTOBOX_BASE64_BASE64_H_
-#define SRC_LIBCRYPTOBOX_BASE64_BASE64_H_
 
-#include "config.h"
+#include "css_value.hxx"
 
-#ifdef  __cplusplus
-extern "C" {
-#endif
+namespace rspamd {
 
-const char *base64_load (void);
+namespace css {
 
-#ifdef  __cplusplus
+tl::expected<css_value,css_parse_error> from_bytes (const char *input,
+													size_t inlen)
+{
+	return tl::unexpected{css_parse_error(css_parse_error_type::PARSE_ERROR_NYI)};
 }
-#endif
 
-#endif /* SRC_LIBCRYPTOBOX_BASE64_BASE64_H_ */
+}
+}
diff --git a/src/libserver/css/css_value.hxx b/src/libserver/css/css_value.hxx
new file mode 100644
index 000000000..0af6a1091
--- /dev/null
+++ b/src/libserver/css/css_value.hxx
@@ -0,0 +1,100 @@
+/*-
+ * 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_VALUE_HXX
+#define RSPAMD_CSS_VALUE_HXX
+
+#include "libserver/html.h"
+#include <string>
+#include <variant>
+#include <optional>
+#include "parse_error.hxx"
+#include "contrib/expected/expected.hpp"
+
+namespace rspamd {
+namespace css {
+
+enum class css_display_value {
+	DISPLAY_NORMAL,
+	DISPLAY_
+};
+
+enum class css_flag_value {
+	FLAG_INHERIT,
+	FLAG_IMPORTANT,
+	FLAG_NOTIMPORTANT
+};
+
+struct css_value {
+	enum class css_value_type {
+		CSS_VALUE_COLOR,
+		CSS_VALUE_SIZE,
+		CSS_VALUE_STRING,
+		CSS_VALUE_DISPLAY,
+		CSS_VALUE_FLAG,
+		CSS_VALUE_NYI,
+	} type;
+
+	std::variant<struct html_color,
+			double,
+			std::string,
+			css_display_value,
+			css_flag_value> value;
+
+	constexpr std::optional<struct html_color> to_color (void) const {
+		if (type == css_value_type::CSS_VALUE_COLOR) {
+			return std::get<struct html_color>(value);
+		}
+
+		return std::nullopt;
+	}
+
+	constexpr std::optional<double> to_size (void) const {
+		if (type == css_value_type::CSS_VALUE_SIZE) {
+			return std::get<double>(value);
+		}
+
+		return std::nullopt;
+	}
+
+	constexpr std::optional<css_display_value> to_display (void) const {
+		if (type == css_value_type::CSS_VALUE_DISPLAY) {
+			return std::get<css_display_value>(value);
+		}
+
+		return std::nullopt;
+	}
+
+	constexpr std::optional<css_flag_value> to_flag (void) const {
+		if (type == css_value_type::CSS_VALUE_FLAG) {
+			return std::get<css_flag_value>(value);
+		}
+
+		return std::nullopt;
+	}
+
+	constexpr bool is_valid (void) const {
+		return (type != css_value_type::CSS_VALUE_NYI);
+	}
+
+	static tl::expected<css_value,css_parse_error> from_bytes (const char *input,
+																  size_t inlen);
+};
+
+}
+}
+
+#endif //RSPAMD_CSS_VALUE_HXX
diff --git a/src/libserver/css/parse_error.hxx b/src/libserver/css/parse_error.hxx
new file mode 100644
index 000000000..d5629fa26
--- /dev/null
+++ b/src/libserver/css/parse_error.hxx
@@ -0,0 +1,51 @@
+/*-
+ * 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_PARSE_ERROR_HXX
+#define RSPAMD_PARSE_ERROR_HXX
+
+#include <string>
+#include <optional>
+
+namespace rspamd {
+
+namespace css {
+
+/*
+ * Generic parser errors
+ */
+enum class css_parse_error_type {
+	PARSE_ERROR_UNKNOWN_OPTION,
+	PARSE_ERROR_INVALID_SYNTAX,
+	PARSE_ERROR_NYI,
+	PARSE_ERROR_UNKNOWN_ERROR,
+};
+
+struct css_parse_error {
+	css_parse_error_type type;
+	std::optional<std::string> description;
+
+	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) :
+			type(type) {}
+};
+
+}
+
+}
+#endif //RSPAMD_PARSE_ERROR_HXX


More information about the Commits mailing list