2015-02-11 16:18:18 +11:00
|
|
|
/*
|
2018-08-04 15:14:06 +10:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2015-02-11 16:18:18 +11:00
|
|
|
*
|
2017-12-15 18:57:10 +11:00
|
|
|
* Copyright 2015, 2017 Danny Robson <danny@nerdcruft.net>
|
2015-02-11 16:18:18 +11:00
|
|
|
*/
|
|
|
|
|
2017-12-15 18:57:10 +11:00
|
|
|
#ifndef CRUFT_UTIL_URI_HPP
|
|
|
|
#define CRUFT_UTIL_URI_HPP
|
2015-02-09 17:43:24 +11:00
|
|
|
|
2019-05-17 12:26:08 +10:00
|
|
|
#include "debug/assert.hpp"
|
2015-02-11 16:18:43 +11:00
|
|
|
#include "view.hpp"
|
|
|
|
|
2017-12-15 18:57:10 +11:00
|
|
|
#include <array>
|
2015-02-09 17:43:24 +11:00
|
|
|
#include <string>
|
|
|
|
#include <stdexcept>
|
|
|
|
|
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
namespace cruft {
|
2017-12-15 18:57:10 +11:00
|
|
|
// parsing of rfc3986 uniform resource identifiers
|
|
|
|
//
|
|
|
|
// does not currently perform normalisation (scheme or protocol),
|
|
|
|
// comparison, or other associated operations. though these should be
|
|
|
|
// added in the future.
|
|
|
|
//
|
|
|
|
// note that the parsed results may not always conform to expectations
|
|
|
|
// for some protocols. eg, mailto identifiers are complex to parse
|
|
|
|
// reliably and would require a specialised parser to be reliable.
|
|
|
|
//
|
|
|
|
// not all fields will be present for all protocols (or all instances of
|
|
|
|
// any given protocol). eg, the "tel" is unlikely to have port numbers.
|
2015-02-09 17:43:24 +11:00
|
|
|
class uri {
|
|
|
|
public:
|
2020-01-02 11:19:09 +11:00
|
|
|
uri (std::string &&);
|
|
|
|
uri (const std::string&);
|
|
|
|
uri (const char *);
|
|
|
|
uri (view<const char *>);
|
2015-02-09 17:43:24 +11:00
|
|
|
|
|
|
|
class parse_error : public std::runtime_error
|
|
|
|
{ using runtime_error::runtime_error; };
|
|
|
|
|
2017-12-15 18:57:10 +11:00
|
|
|
|
|
|
|
// URI: 'https://user:password@example.com:80/path/to?foo=bar#fragment'
|
|
|
|
//
|
|
|
|
// SCHEME: 'https'
|
|
|
|
// HIERARCHICAL: 'user:password@example.com:80/path/to'
|
|
|
|
// AUTHORITY: 'user:password@example.com:80'
|
|
|
|
// USER: 'user:password'
|
|
|
|
// HOST: 'example.com'
|
|
|
|
// PORT: '80'
|
|
|
|
// PATH: '/path/to'
|
|
|
|
// QUERY: 'foo=bar'
|
|
|
|
// FRAGMENT: 'fragment'
|
2016-05-12 17:39:33 +10:00
|
|
|
enum component {
|
2015-02-09 17:43:24 +11:00
|
|
|
SCHEME,
|
2017-12-15 18:57:10 +11:00
|
|
|
HIERARCHICAL,
|
|
|
|
AUTHORITY,
|
|
|
|
USER,
|
|
|
|
HOST,
|
|
|
|
PORT,
|
|
|
|
PATH,
|
2015-02-09 17:43:24 +11:00
|
|
|
QUERY,
|
|
|
|
FRAGMENT,
|
|
|
|
|
|
|
|
NUM_COMPONENTS
|
|
|
|
};
|
|
|
|
|
2017-12-26 17:33:24 +11:00
|
|
|
view<const char*>
|
|
|
|
get (component c) const&
|
|
|
|
{
|
2020-09-24 08:03:41 +10:00
|
|
|
CHECK_INDEX (c, NUM_COMPONENTS);
|
2017-12-26 17:33:24 +11:00
|
|
|
return m_views[c];
|
|
|
|
}
|
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::view<const char*> all (void) const& { return cruft::make_view (m_value); }
|
2021-11-07 12:13:15 +11:00
|
|
|
std::string const& value (void) const& { return m_value; }
|
2018-08-05 14:42:02 +10:00
|
|
|
|
|
|
|
cruft::view<const char*> scheme (void) const& { return get (SCHEME); }
|
|
|
|
cruft::view<const char*> heirarchical (void) const& { return get (HIERARCHICAL); }
|
|
|
|
cruft::view<const char*> authority (void) const& { return get (AUTHORITY); }
|
|
|
|
cruft::view<const char*> user (void) const& { return get (USER); }
|
|
|
|
cruft::view<const char*> host (void) const& { return get (HOST); }
|
|
|
|
cruft::view<const char*> port (void) const& { return get (PORT); }
|
|
|
|
cruft::view<const char*> path (void) const& { return get (PATH); }
|
|
|
|
cruft::view<const char*> query (void) const& { return get (QUERY); }
|
|
|
|
cruft::view<const char*> fragment (void) const& { return get (FRAGMENT); }
|
2017-12-26 17:33:24 +11:00
|
|
|
|
|
|
|
auto components (void) const& noexcept { return m_views; }
|
2015-02-09 17:43:24 +11:00
|
|
|
|
2015-09-21 15:36:05 +10:00
|
|
|
static std::string percent_decode (view<const char*>);
|
2015-02-09 17:43:24 +11:00
|
|
|
|
|
|
|
private:
|
2017-12-15 18:57:10 +11:00
|
|
|
std::array<view<const char*>, NUM_COMPONENTS> m_views;
|
2015-02-09 17:43:24 +11:00
|
|
|
std::string m_value;
|
|
|
|
};
|
|
|
|
|
|
|
|
std::ostream& operator<< (std::ostream&, uri::component);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|