util/UriUtil: add uri_get_suffix() overload that ignores query string
This commit is contained in:
parent
6ad336743d
commit
674091424e
@ -44,6 +44,23 @@ uri_get_suffix(const char *uri)
|
|||||||
return suffix;
|
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 *
|
static const char *
|
||||||
verify_uri_segment(const char *p)
|
verify_uri_segment(const char *p)
|
||||||
{
|
{
|
||||||
|
@ -35,6 +35,17 @@ gcc_pure
|
|||||||
const char *
|
const char *
|
||||||
uri_get_suffix(const char *uri);
|
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:
|
* Returns true if this is a safe "local" URI:
|
||||||
*
|
*
|
||||||
|
@ -33,6 +33,25 @@ public:
|
|||||||
uri_get_suffix(".jpg"));
|
uri_get_suffix(".jpg"));
|
||||||
CPPUNIT_ASSERT_EQUAL((const char *)nullptr,
|
CPPUNIT_ASSERT_EQUAL((const char *)nullptr,
|
||||||
uri_get_suffix("/foo/.jpg"));
|
uri_get_suffix("/foo/.jpg"));
|
||||||
|
|
||||||
|
/* the first overload does not eliminate the query
|
||||||
|
string */
|
||||||
|
CPPUNIT_ASSERT_EQUAL(0, strcmp(uri_get_suffix("/foo/bar.jpg?query_string"),
|
||||||
|
"jpg?query_string"));
|
||||||
|
|
||||||
|
/* ... but the second one does */
|
||||||
|
UriSuffixBuffer buffer;
|
||||||
|
CPPUNIT_ASSERT_EQUAL(0, strcmp(uri_get_suffix("/foo/bar.jpg?query_string",
|
||||||
|
buffer),
|
||||||
|
"jpg"));
|
||||||
|
|
||||||
|
/* repeat some of the above tests with the second overload */
|
||||||
|
CPPUNIT_ASSERT_EQUAL((const char *)nullptr,
|
||||||
|
uri_get_suffix("/foo/bar", buffer));
|
||||||
|
CPPUNIT_ASSERT_EQUAL((const char *)nullptr,
|
||||||
|
uri_get_suffix("/foo.jpg/bar", buffer));
|
||||||
|
CPPUNIT_ASSERT_EQUAL(0, strcmp(uri_get_suffix("/foo/bar.jpg", buffer),
|
||||||
|
"jpg"));
|
||||||
}
|
}
|
||||||
|
|
||||||
void TestRemoveAuth() {
|
void TestRemoveAuth() {
|
||||||
|
Loading…
Reference in New Issue
Block a user