VideoTools
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
gurl.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2017 Graeme Walker
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 // ===
17 ///
18 /// \file gurl.h
19 ///
20 
21 #ifndef G_URL_H
22 #define G_URL_H
23 
24 #include "gdef.h"
25 #include "gstr.h"
26 #include <map>
27 #include <string>
28 
29 namespace G
30 {
31  class Url ;
32 }
33 
34 /// \class G::Url
35 /// A simple parser for URLs.
36 /// Eg:
37 /// \code
38 /// "protocol://user:pwd@host:port/path1/path2.ext?key1=/value%31&key2#anchor"
39 /// \endcode
40 /// \see rfc3986
41 ///
42 class G::Url
43 {
44 public:
45  typedef std::multimap<std::string,std::string> Map ;
46 
47  Url() ;
48  ///< Default constructor for a url with a path of "/".
49 
50  explicit Url( const std::string & ) ;
51  ///< Constructor.
52 
53  std::string protocol() const ;
54  ///< Returns the protocol part eg. "http".
55 
56  std::string address() const ;
57  ///< Returns the address part, which might include the port,
58  ///< and which might use ipv6 square brackets.
59 
60  std::string address( const std::string & default_port ) const ;
61  ///< Returns the transport address formed from host() and
62  ///< port(default_port). This never has ipv6 square brackets.
63  ///< Precondition: !default_port.empty()
64 
65  std::string host() const ;
66  ///< Returns the hostname or network address part. Any square
67  ///< brackets used to delimit an ipv6 network address are
68  ///< not returned.
69 
70  std::string port( const std::string & default_ = std::string() ) const ;
71  ///< Returns the port or service-name, or the specified default
72  ///< if none. It is often appropriate to pass protocol() as the
73  ///< default.
74 
75  std::string path() const ;
76  ///< Returns the path part, including the leading slash.
77 
78  std::string request() const ;
79  ///< Returns the path and parameters, suitable for a GET request.
80 
81  std::string parameters() const ;
82  ///< Returns the parameters string.
83 
84  Map pmap() const ;
85  ///< Returns the decode()d parameters as a multimap.
86 
87  bool has( const std::string & key ) const ;
88  ///< Returns true if the named parameter is present.
89 
90  std::string parameter( std::string key , std::string default_ = std::string() ) const ;
91  ///< Returns the decode()d value of the named parameter,
92  ///< or a default value.
93 
94  std::string authorisation() const ;
95  ///< Returns the "user:pwd" part.
96 
97  std::string anchor() const ;
98  ///< Returns the "#anchor" part.
99 
100  std::string str() const ;
101  ///< Returns the string representation. Returns "/" if default constructed.
102 
103  std::string summary() const ;
104  ///< Returns a summary of the url for logging purposes, specifically
105  ///< excluding username/password but also excluding url parameters
106  ///< in case they also contains authentication secrets or tokens.
107 
108  static std::string decode( const std::string & ) ;
109  ///< Does url-decoding (rfc3986 2.1).
110 
111  static std::string encode( const std::string & , bool plus_for_space = true ) ;
112  ///< Does url-encoding.
113 
114  static std::string join( const std::string & protocol , const std::string & authorisation ,
115  const std::string & address , const std::string & path , const std::string & params ,
116  const std::string & anchor = std::string() ) ;
117  ///< Returns a concatenation of the given url parts, with
118  ///< the correct separators inserted.
119 
120 private:
121  void init( std::string ) ;
122  static size_t colonpos( const std::string & ) ;
123  static bool ipv6( const std::string & ) ;
124 
125 private:
126  std::string m_protocol ;
127  std::string m_authorisation ;
128  std::string m_address ;
129  std::string m_host ;
130  std::string m_port ;
131  std::string m_path ;
132  std::string m_params ;
133  std::string m_anchor ;
134  Map m_params_map ;
135 } ;
136 
137 #endif
std::string port(const std::string &default_=std::string()) const
Returns the port or service-name, or the specified default if none.
Definition: gurl.cpp:191
std::string host() const
Returns the hostname or network address part.
Definition: gurl.cpp:186
static std::string decode(const std::string &)
Does url-decoding (rfc3986 2.1).
Definition: gurl.cpp:248
A simple parser for URLs.
Definition: gurl.h:42
std::string parameter(std::string key, std::string default_=std::string()) const
Returns the decode()d value of the named parameter, or a default value.
Definition: gurl.cpp:213
std::string request() const
Returns the path and parameters, suitable for a GET request.
Definition: gurl.cpp:166
std::string path() const
Returns the path part, including the leading slash.
Definition: gurl.cpp:222
bool has(const std::string &key) const
Returns true if the named parameter is present.
Definition: gurl.cpp:208
std::string parameters() const
Returns the parameters string.
Definition: gurl.cpp:171
std::string str() const
Returns the string representation. Returns "/" if default constructed.
Definition: gurl.cpp:38
Url()
Default constructor for a url with a path of "/".
Definition: gurl.cpp:28
Map pmap() const
Returns the decode()d parameters as a multimap.
Definition: gurl.cpp:176
std::string protocol() const
Returns the protocol part eg. "http".
Definition: gurl.cpp:181
std::string authorisation() const
Returns the "user:pwd" part.
Definition: gurl.cpp:227
static std::string encode(const std::string &, bool plus_for_space=true)
Does url-encoding.
Definition: gurl.cpp:279
std::string summary() const
Returns a summary of the url for logging purposes, specifically excluding username/password but also ...
Definition: gurl.cpp:73
std::string anchor() const
Returns the "#anchor" part.
Definition: gurl.cpp:232
std::string address() const
Returns the address part, which might include the port, and which might use ipv6 square brackets...
Definition: gurl.cpp:196
static std::string join(const std::string &protocol, const std::string &authorisation, const std::string &address, const std::string &path, const std::string &params, const std::string &anchor=std::string())
Returns a concatenation of the given url parts, with the correct separators inserted.
Definition: gurl.cpp:43