neoGFX
Cross-platform C++ app/game engine
Loading...
Searching...
No Matches
uri.hpp
Go to the documentation of this file.
1// uri.hpp
2/*
3 * Copyright (c) 2007 Leigh Johnston.
4 *
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * * Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * * Neither the name of Leigh Johnston nor the names of any
19 * other contributors to this software may be used to endorse or
20 * promote products derived from this software without specific prior
21 * written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
24 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
25 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
27 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34*/
35
36#pragma once
37
38#include <neolib/neolib.hpp>
39#include <unordered_set>
40#include <optional>
41
42namespace neolib
43{
44 class NEOLIB_EXPORT uri_authority
45 {
46 public:
47 typedef std::string user_information_type;
48 typedef std::string host_type;
49 typedef uint16_t port_type;
50 typedef std::optional<user_information_type> optional_user_information;
51 typedef std::optional<host_type> optional_host;
52 typedef std::optional<port_type> optional_port;
53 public:
55 uri_authority(const std::string& aAuthority);
56 public:
58 const optional_host& host() const;
59 const optional_port& port() const;
60 private:
61 std::string parse_user_information(const std::string& aRest);
62 std::string parse_host(const std::string& aRest);
63 void parse_port(const std::string& aRest);
64 private:
65 optional_user_information iUserInformation;
66 optional_host iHost;
67 optional_port iPort;
68 };
69
70 class uri
71 {
72 public:
73 uri();
74 uri(const std::string& aUri);
75 public:
76 std::string to_string() const;
77 const std::string& scheme() const;
78 const uri_authority& authority() const;
79 const std::string& path() const;
80 const std::string& query() const;
81 const std::string& fragment() const;
82 void set_scheme(const std::string& aScheme);
83 void set_authority(const uri_authority& aAuthority);
84 void set_path(const std::string& aPath);
85 void set_query(const std::string& aQuery);
86 void set_fragment(const std::string& aFragment);
87 public:
88 static std::string escaped(const std::string& aString);
89 static std::string unescaped(const std::string& String);
90 private:
91 void parse_authority(const std::string& aRest);
92 std::string parse_path(const std::string& aRest);
93 std::string parse_query(const std::string& aRest);
94 std::string parse_fragment(const std::string& aRest);
95 std::string parse_scheme(const std::string& aRest);
96 private:
97 std::string iScheme;
98 uri_authority iAuthority;
99 std::string iPath;
100 std::string iQuery;
101 std::string iFragment;
102 };
103
104 template <typename Elem, typename Traits>
105 inline std::basic_ostream<Elem, Traits>& operator<<(std::basic_ostream<Elem, Traits>& aStream, const uri_authority& aUriAuthority)
106 {
107 if (aUriAuthority.user_information() != std::nullopt)
108 aStream << uri::escaped(*aUriAuthority.user_information()) << "@";
109 if (aUriAuthority.host() != std::nullopt)
110 aStream << uri::escaped(*aUriAuthority.host());
111 if (aUriAuthority.port() != std::nullopt)
112 aStream << ":" << *aUriAuthority.port();
113 return aStream;
114 }
115
116 template <typename Elem, typename Traits>
117 inline std::basic_ostream<Elem, Traits>& operator<<(std::basic_ostream<Elem, Traits>& aStream, const uri& aUri)
118 {
119 aStream << uri::escaped(aUri.scheme()) << "://" << aUri.authority() << "/" << uri::escaped(aUri.path());
120 if (!aUri.query().empty())
121 aStream << "?" << uri::escaped(aUri.query());
122 if (!aUri.fragment().empty())
123 aStream << "#" << uri::escaped(aUri.fragment());
124 return aStream;
125 }
126}
std::optional< host_type > optional_host
Definition uri.hpp:51
uri_authority(const std::string &aAuthority)
std::string user_information_type
Definition uri.hpp:47
std::string host_type
Definition uri.hpp:48
const optional_host & host() const
std::optional< port_type > optional_port
Definition uri.hpp:52
const optional_user_information & user_information() const
const optional_port & port() const
uint16_t port_type
Definition uri.hpp:49
std::optional< user_information_type > optional_user_information
Definition uri.hpp:50
static std::string escaped(const std::string &aString)
const uri_authority & authority() const
void set_fragment(const std::string &aFragment)
void set_path(const std::string &aPath)
const std::string & fragment() const
std::string to_string() const
const std::string & query() const
const std::string & path() const
void set_query(const std::string &aQuery)
static std::string unescaped(const std::string &String)
void set_scheme(const std::string &aScheme)
const std::string & scheme() const
void set_authority(const uri_authority &aAuthority)
uri(const std::string &aUri)