106 lines
3.5 KiB
C++
106 lines
3.5 KiB
C++
/*
|
|
* 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/.
|
|
*
|
|
* Copyright 2015, 2017 Danny Robson <danny@nerdcruft.net>
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include "debug/assert.hpp"
|
|
#include "view.hpp"
|
|
|
|
#include <array>
|
|
#include <string>
|
|
#include <stdexcept>
|
|
#include <iosfwd>
|
|
|
|
|
|
namespace cruft {
|
|
// 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.
|
|
class uri {
|
|
public:
|
|
uri (std::string &&);
|
|
uri (const std::string&);
|
|
uri (const char *);
|
|
uri (view<const char *>);
|
|
|
|
class parse_error : public std::runtime_error
|
|
{ using runtime_error::runtime_error; };
|
|
|
|
|
|
// 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'
|
|
enum component {
|
|
SCHEME,
|
|
HIERARCHICAL,
|
|
AUTHORITY,
|
|
USER,
|
|
HOST,
|
|
PORT,
|
|
PATH,
|
|
QUERY,
|
|
FRAGMENT,
|
|
|
|
NUM_COMPONENTS
|
|
};
|
|
|
|
std::string_view
|
|
get (component c) const&
|
|
{
|
|
CHECK_INDEX (c, NUM_COMPONENTS);
|
|
return { m_views[c].data (), m_views[c].size () };
|
|
}
|
|
|
|
std::string_view all (void) const& { return m_value; }
|
|
std::string const& value (void) const& { return m_value; }
|
|
|
|
std::string_view scheme (void) const& { return get (SCHEME); }
|
|
std::string_view heirarchical (void) const& { return get (HIERARCHICAL); }
|
|
std::string_view authority (void) const& { return get (AUTHORITY); }
|
|
std::string_view user (void) const& { return get (USER); }
|
|
std::string_view host (void) const& { return get (HOST); }
|
|
std::string_view port (void) const& { return get (PORT); }
|
|
std::string_view path (void) const& { return get (PATH); }
|
|
std::string_view query (void) const& { return get (QUERY); }
|
|
std::string_view fragment (void) const& { return get (FRAGMENT); }
|
|
|
|
void clear (component);
|
|
|
|
auto components (void) const& noexcept { return m_views; }
|
|
|
|
static std::string percent_decode (view<const char*>);
|
|
|
|
private:
|
|
std::array<view<const char*>, NUM_COMPONENTS> m_views;
|
|
std::string m_value;
|
|
};
|
|
|
|
cruft::uri resolve (cruft::uri base, cruft::uri child);
|
|
cruft::uri normalise (cruft::uri);
|
|
|
|
std::ostream& operator<< (std::ostream&, uri const&);
|
|
std::ostream& operator<< (std::ostream&, uri::component);
|
|
}
|