From 86b473e2247aef1f32b12cdac0108ad8526e121c Mon Sep 17 00:00:00 2001 From: Lorenzo Fontana Date: Sun, 17 May 2020 01:58:00 +0200 Subject: [PATCH] update(userspace/falco): utilities to detect unix socket prefix in string Signed-off-by: Lorenzo Fontana --- userspace/falco/utils.cpp | 14 ++++++++++++++ userspace/falco/utils.h | 7 +++++++ 2 files changed, 21 insertions(+) diff --git a/userspace/falco/utils.cpp b/userspace/falco/utils.cpp index 12900799..76d20468 100644 --- a/userspace/falco/utils.cpp +++ b/userspace/falco/utils.cpp @@ -14,6 +14,8 @@ See the License for the specific language governing permissions and limitations under the License. */ +#include + #include "utils.h" #include "banned.h" // This raises a compilation error when certain functions are used @@ -33,3 +35,15 @@ void falco::utils::read(const std::string& filename, std::string& data) return; } + +bool falco::utils::starts_with(const std::string& text, const std::string& prefix) +{ + return prefix.empty() || + (text.size() >= prefix.size() && + std::memcmp(text.data(), prefix.data(), prefix.size()) == 0); +} + +bool falco::utils::network::url_is_unix_scheme(const std::string& url) +{ + return starts_with(url, UNIX_SCHEME); +} diff --git a/userspace/falco/utils.h b/userspace/falco/utils.h index 30d6bd15..a91ea978 100644 --- a/userspace/falco/utils.h +++ b/userspace/falco/utils.h @@ -26,5 +26,12 @@ namespace falco namespace utils { void read(const std::string& filename, std::string& data); +bool starts_with(const std::string& text, const std::string& prefix); + +namespace network +{ +static const std::string UNIX_SCHEME{"unix://"}; +bool url_is_unix_scheme(const std::string& url); +} // namespace network } // namespace utils } // namespace falco