commit 144acb7: [Minor] Add functions to deal with url protocols

Vsevolod Stakhov vsevolod at highsecure.ru
Sat Feb 16 14:28:04 UTC 2019


Author: Vsevolod Stakhov
Date: 2019-02-16 13:52:55 +0000
URL: https://github.com/rspamd/rspamd/commit/144acb70eb098e1d88bbc0d270a3e115f8b6aa27

[Minor] Add functions to deal with url protocols

---
 src/libserver/url.c | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/libserver/url.h | 28 +++++++++++++++++++-------
 2 files changed, 79 insertions(+), 7 deletions(-)

diff --git a/src/libserver/url.c b/src/libserver/url.c
index 461c232af..ae2f84300 100644
--- a/src/libserver/url.c
+++ b/src/libserver/url.c
@@ -3330,3 +3330,61 @@ rspamd_url_is_domain (int c)
 {
 	return is_domain ((guchar)c);
 }
+
+const gchar*
+rspamd_url_protocol_name (enum rspamd_url_protocol proto)
+{
+	const gchar *ret = "unknown";
+
+	switch (proto) {
+	case PROTOCOL_HTTP:
+		ret = "http";
+		break;
+	case PROTOCOL_HTTPS:
+		ret = "https";
+		break;
+	case PROTOCOL_FTP:
+		ret = "ftp";
+		break;
+	case PROTOCOL_FILE:
+		ret = "file";
+		break;
+	case PROTOCOL_MAILTO:
+		ret = "mailto";
+		break;
+	case PROTOCOL_TELEPHONE:
+		ret = "telephone";
+		break;
+	default:
+		break;
+	}
+
+	return ret;
+}
+
+enum rspamd_url_protocol
+rspamd_url_protocol_from_string (const gchar *str)
+{
+	enum rspamd_url_protocol ret = PROTOCOL_UNKNOWN;
+
+	if (strcmp (str, "http") == 0) {
+		ret = PROTOCOL_HTTP;
+	}
+	else if (strcmp (str, "https") == 0) {
+		ret = PROTOCOL_HTTPS;
+	}
+	else if (strcmp (str, "mailto") == 0) {
+		ret = PROTOCOL_MAILTO;
+	}
+	else if (strcmp (str, "ftp") == 0) {
+		ret = PROTOCOL_FTP;
+	}
+	else if (strcmp (str, "file") == 0) {
+		ret = PROTOCOL_FILE;
+	}
+	else if (strcmp (str, "telephone") == 0) {
+		ret = PROTOCOL_TELEPHONE;
+	}
+
+	return ret;
+}
diff --git a/src/libserver/url.h b/src/libserver/url.h
index 4c34be9e7..4d1948921 100644
--- a/src/libserver/url.h
+++ b/src/libserver/url.h
@@ -79,13 +79,13 @@ enum uri_errno {
 };
 
 enum rspamd_url_protocol {
-	PROTOCOL_FILE = 0,
-	PROTOCOL_FTP,
-	PROTOCOL_HTTP,
-	PROTOCOL_HTTPS,
-	PROTOCOL_MAILTO,
-	PROTOCOL_TELEPHONE,
-	PROTOCOL_UNKNOWN
+	PROTOCOL_FILE = 1u << 0,
+	PROTOCOL_FTP = 1u << 1,
+	PROTOCOL_HTTP = 1u << 2,
+	PROTOCOL_HTTPS = 1u << 3,
+	PROTOCOL_MAILTO = 1u << 4,
+	PROTOCOL_TELEPHONE = 1u << 5,
+	PROTOCOL_UNKNOWN = -1,
 };
 
 /**
@@ -240,4 +240,18 @@ const gchar * rspamd_url_encode (struct rspamd_url *url, gsize *dlen,
  */
 gboolean rspamd_url_is_domain (int c);
 
+/**
+ * Returns symbolic name for protocol
+ * @param proto
+ * @return
+ */
+const gchar* rspamd_url_protocol_name (enum rspamd_url_protocol proto);
+
+
+/**
+ * Converts string to a numeric protocol
+ * @param str
+ * @return
+ */
+enum rspamd_url_protocol rspamd_url_protocol_from_string (const gchar *str);
 #endif


More information about the Commits mailing list