2009-02-25 16:44:06 +01:00
|
|
|
/*
|
2019-08-09 15:53:06 +02:00
|
|
|
* Copyright 2008-2019 Max Kellermann <max.kellermann@gmail.com>
|
2009-02-25 16:44:06 +01:00
|
|
|
*
|
2019-08-09 15:53:06 +02:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
2009-02-25 16:44:06 +01:00
|
|
|
*
|
2019-08-09 15:53:06 +02:00
|
|
|
* - Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
2009-03-13 18:43:16 +01:00
|
|
|
*
|
2019-08-09 15:53:06 +02:00
|
|
|
* - Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
|
|
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
|
|
|
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
2009-02-25 16:44:06 +01:00
|
|
|
*/
|
|
|
|
|
2013-04-08 23:30:21 +02:00
|
|
|
#include "UriUtil.hxx"
|
2018-08-02 10:38:20 +02:00
|
|
|
#include "ASCII.hxx"
|
2009-02-25 16:44:06 +01:00
|
|
|
|
2020-03-12 23:20:59 +01:00
|
|
|
#include <cassert>
|
|
|
|
|
2009-02-25 16:44:06 +01:00
|
|
|
#include <string.h>
|
|
|
|
|
2009-12-26 02:07:44 +01:00
|
|
|
static const char *
|
2017-05-08 14:44:49 +02:00
|
|
|
verify_uri_segment(const char *p) noexcept
|
2009-12-26 02:07:44 +01:00
|
|
|
{
|
2010-02-27 18:56:47 +01:00
|
|
|
unsigned dots = 0;
|
2010-06-25 21:54:45 +02:00
|
|
|
while (*p == '.') {
|
2010-02-27 18:56:47 +01:00
|
|
|
++p;
|
2010-06-25 21:54:45 +02:00
|
|
|
++dots;
|
|
|
|
}
|
|
|
|
|
2010-02-27 18:56:47 +01:00
|
|
|
if (dots <= 2 && (*p == 0 || *p == '/'))
|
2013-04-08 23:30:21 +02:00
|
|
|
return nullptr;
|
2009-12-26 02:07:44 +01:00
|
|
|
|
2015-11-06 09:43:44 +01:00
|
|
|
const char *q = strchr(p + 1, '/');
|
2013-04-08 23:30:21 +02:00
|
|
|
return q != nullptr ? q : "";
|
2009-12-26 02:07:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2017-05-08 14:44:49 +02:00
|
|
|
uri_safe_local(const char *uri) noexcept
|
2009-12-26 02:07:44 +01:00
|
|
|
{
|
|
|
|
while (true) {
|
|
|
|
uri = verify_uri_segment(uri);
|
2013-04-08 23:30:21 +02:00
|
|
|
if (uri == nullptr)
|
2009-12-26 02:07:44 +01:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if (*uri == 0)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
assert(*uri == '/');
|
|
|
|
|
|
|
|
++uri;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-06 09:43:37 +01:00
|
|
|
gcc_pure
|
|
|
|
static const char *
|
2017-05-08 14:44:49 +02:00
|
|
|
SkipUriScheme(const char *uri) noexcept
|
2009-02-27 19:20:11 +01:00
|
|
|
{
|
2015-11-06 09:47:28 +01:00
|
|
|
const char *const schemes[] = { "http://", "https://", "ftp://" };
|
|
|
|
for (auto scheme : schemes) {
|
2018-08-02 10:38:20 +02:00
|
|
|
auto result = StringAfterPrefixCaseASCII(uri, scheme);
|
2015-11-06 09:47:28 +01:00
|
|
|
if (result != nullptr)
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
2015-11-06 09:43:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
2017-05-08 14:44:49 +02:00
|
|
|
uri_remove_auth(const char *uri) noexcept
|
2015-11-06 09:43:37 +01:00
|
|
|
{
|
|
|
|
const char *auth = SkipUriScheme(uri);
|
|
|
|
if (auth == nullptr)
|
2009-02-27 19:20:11 +01:00
|
|
|
/* unrecognized URI */
|
2013-10-23 21:38:07 +02:00
|
|
|
return std::string();
|
2009-02-27 19:20:11 +01:00
|
|
|
|
2015-11-06 09:43:44 +01:00
|
|
|
const char *slash = strchr(auth, '/');
|
2013-04-08 23:30:21 +02:00
|
|
|
if (slash == nullptr)
|
2009-02-27 19:20:11 +01:00
|
|
|
slash = auth + strlen(auth);
|
|
|
|
|
2015-11-06 09:43:44 +01:00
|
|
|
const char *at = (const char *)memchr(auth, '@', slash - auth);
|
2013-04-08 23:30:21 +02:00
|
|
|
if (at == nullptr)
|
2009-02-27 19:20:11 +01:00
|
|
|
/* no auth info present, do nothing */
|
2013-10-23 21:38:07 +02:00
|
|
|
return std::string();
|
2009-02-27 19:20:11 +01:00
|
|
|
|
|
|
|
/* duplicate the full URI and then delete the auth
|
|
|
|
information */
|
2013-10-23 21:38:07 +02:00
|
|
|
std::string result(uri);
|
|
|
|
result.erase(auth - uri, at + 1 - auth);
|
|
|
|
return result;
|
2009-02-27 19:20:11 +01:00
|
|
|
}
|