From 674091424e715fddd8fbfe8146f351da5bf84974 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Sat, 1 Nov 2014 12:45:47 +0100 Subject: util/UriUtil: add uri_get_suffix() overload that ignores query string --- src/util/UriUtil.cxx | 17 +++++++++++++++++ src/util/UriUtil.hxx | 11 +++++++++++ 2 files changed, 28 insertions(+) (limited to 'src') diff --git a/src/util/UriUtil.cxx b/src/util/UriUtil.cxx index 2609db2cf..1783fbca5 100644 --- a/src/util/UriUtil.cxx +++ b/src/util/UriUtil.cxx @@ -44,6 +44,23 @@ uri_get_suffix(const char *uri) return suffix; } +const char * +uri_get_suffix(const char *uri, UriSuffixBuffer &buffer) +{ + const char *suffix = uri_get_suffix(uri); + if (suffix == nullptr) + return nullptr; + + const char *q = strchr(suffix, '?'); + if (q != nullptr && size_t(q - suffix) < sizeof(buffer.data)) { + memcpy(buffer.data, suffix, q - suffix); + buffer.data[q - suffix] = 0; + suffix = buffer.data; + } + + return suffix; +} + static const char * verify_uri_segment(const char *p) { diff --git a/src/util/UriUtil.hxx b/src/util/UriUtil.hxx index 78d0a6bff..1c6bce3ff 100644 --- a/src/util/UriUtil.hxx +++ b/src/util/UriUtil.hxx @@ -35,6 +35,17 @@ gcc_pure const char * uri_get_suffix(const char *uri); +struct UriSuffixBuffer { + char data[8]; +}; + +/** + * Returns the file name suffix, ignoring the query string. + */ +gcc_pure +const char * +uri_get_suffix(const char *uri, UriSuffixBuffer &buffer); + /** * Returns true if this is a safe "local" URI: * -- cgit v1.2.3