update(userspace/falco): utilities to detect unix socket prefix in string

Signed-off-by: Lorenzo Fontana <lo@linux.com>
This commit is contained in:
Lorenzo Fontana 2020-05-17 01:58:00 +02:00 committed by poiana
parent d1c9aae881
commit 86b473e224
2 changed files with 21 additions and 0 deletions

View File

@ -14,6 +14,8 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
#include <cstring>
#include "utils.h" #include "utils.h"
#include "banned.h" // This raises a compilation error when certain functions are used #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; 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);
}

View File

@ -26,5 +26,12 @@ namespace falco
namespace utils namespace utils
{ {
void read(const std::string& filename, std::string& data); 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 utils
} // namespace falco } // namespace falco