HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_Url.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be rurlroduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: UT_Url.h
7  *
8  * COMMENTS:
9  *
10  */
11 
12 #ifndef __UT_URL_H__
13 #define __UT_URL_H__
14 
15 #include "UT_API.h"
16 
17 #include "UT_StringHolder.h"
18 #include "UT_StringView.h"
19 
20 #include <iostream>
21 
23 {
24 public:
25  enum Protocol
26  {
31  PROT_WSS
32  };
33 
34  enum FormattingOptions : unsigned
35  {
36  None = 0x0,
37  RemoveProtocol = 1 << 0,
38  RemovePassword = 1 << 1,
39  RemoveUserInfo = RemovePassword | 1 << 2,
40  RemovePort = 1 << 3,
41  RemovePath = 1 << 4,
42  RemoveQuery = 1 << 5
43  };
44 
45  UT_Url();
46  UT_Url(const UT_StringHolder &url_string,
47  Protocol force_protocol = PROT_UNKNOWN);
48  UT_Url(Protocol prot,
49  const UT_StringHolder &host,
50  int port,
51  const UT_StringHolder &path);
52  UT_Url(Protocol prot,
53  const UT_StringHolder &user,
54  const UT_StringHolder &password,
55  const UT_StringHolder &host,
56  int port,
57  const UT_StringHolder &path,
58  const UT_StringHolder &query_string);
59 
60  UT_Url(const UT_Url &url) = default;
61  UT_Url &operator=(const UT_Url &url) = default;
62  UT_Url(UT_Url &&url) = default;
63  UT_Url &operator=(UT_Url &&url) = default;
64 
65  // This clears the entire url and parses the url string passed in.
66  // Returns true if the url was successfully parsed.
67  bool parse(const UT_StringRef &str_url,
68  Protocol force_protocol = PROT_UNKNOWN);
69 
70  void clear();
71 
72  // The only part of the url that is actually required is the host.
73  bool isUsable() const { return !myHost.isEmpty(); }
74 
75  explicit operator bool() const { return isUsable(); }
76  bool operator==(const UT_Url &url) const;
77  bool operator!=(const UT_Url &url) const;
78 
79  // Use FormattingOptions to specify which elements of the url you would like
80  // to strip. The default is to include all elements when necessary
81  UT_StringHolder toString(unsigned format = FormattingOptions::None) const;
82  UT_StringHolder protocolToString() const;
83 
84  bool usingDefaultPortForProtocol() const;
85  static bool isDefaultPortForProtocol(Protocol prot, int port);
86 
87  inline bool hasMatchingPath(const UT_Url &url) const;
88  inline bool hasMatchingPath(const UT_StringHolder &path) const;
89 
90  // This is the relaxed method for checking hosts.
91  bool hasMatchingHost(const UT_Url &url) const;
92  // This is the relaxed method for checking hosts.
93  bool hasMatchingHost(const UT_StringHolder &host) const;
94 
95  Protocol protocol() const { return myProtocol; }
96  void setProtocol(Protocol protocol) { myProtocol = protocol; }
97 
98  const UT_StringHolder &user() const { return myUser; }
99  void setUser(const UT_StringHolder &user) { myUser = user; }
100 
101  const UT_StringHolder &password() const { return myPassword; }
102  void setPassword(const UT_StringHolder &pass) { myPassword = pass; }
103 
104  const UT_StringHolder &host() const { return myHost; }
105  void setHost(const UT_StringHolder &host) { myHost = host; }
106 
107  // Get a port that is usable for network calls.
108  int port() const;
109  int rawPort() const { return myPort; }
110  void setPort(int port) { myPort = port; }
111 
112  const UT_StringHolder &path() const { return myPath; }
113  void setPath(const UT_StringHolder &path) { myPath = path; }
114 
115  const UT_StringHolder &queryString() const { return myQueryString; }
116  void setQueryString(const UT_StringHolder &query) { myQueryString = query; }
117 
118  // In some cases we want to return a const reference to a url or we
119  // want to pass an invalid url to signal not using it.
120  static const UT_Url theInvalidUrl;
121 private:
122  Protocol myProtocol;
123  UT_StringHolder myUser;
124  UT_StringHolder myPassword;
125  UT_StringHolder myHost;
126  int myPort;
127  UT_StringHolder myPath;
128  UT_StringHolder myQueryString;
129 };
130 
131 inline bool
132 UT_Url::operator==(const UT_Url &url) const
133 {
134  return myProtocol == url.myProtocol && myUser == url.myUser &&
135  myPassword == url.myPassword && hasMatchingHost(url) &&
136  port() == url.port() && hasMatchingPath(url) &&
137  myQueryString == url.myQueryString;
138 }
139 
140 inline bool
141 UT_Url::operator!=(const UT_Url &url) const
142 {
143  return !(*this == url);
144 }
145 
146 inline bool
148 {
149  return hasMatchingPath(url.path());
150 }
151 
152 inline bool
154 {
155  UT_StringView path_view(myPath.begin(), myPath.end());
156  UT_StringView other_path_view(path.begin(), path.end());
157 
158  // Trim off trailing and leading '/'
159  path_view = path_view.trim("/");
160  other_path_view = other_path_view.trim("/");
161 
162  return path_view == other_path_view;
163 }
164 
165 #endif // __UT_URL_H__
166 
bool hasMatchingPath(const UT_Url &url) const
Definition: UT_Url.h:147
void setQueryString(const UT_StringHolder &query)
Definition: UT_Url.h:116
void setProtocol(Protocol protocol)
Definition: UT_Url.h:96
SYS_FORCE_INLINE const_iterator begin() const
Protocol
Definition: UT_Url.h:25
bool operator!=(const UT_Url &url) const
Definition: UT_Url.h:141
const UT_StringHolder & path() const
Definition: UT_Url.h:112
bool hasMatchingHost(const UT_Url &url) const
const UT_StringHolder & password() const
Definition: UT_Url.h:101
GLenum query
Definition: glew.h:5704
GLint GLint GLsizei GLsizei GLsizei GLint GLenum format
Definition: glew.h:1254
#define UT_API
Definition: UT_API.h:13
const UT_StringHolder & user() const
Definition: UT_Url.h:98
SYS_FORCE_INLINE const_iterator end() const
bool operator==(const BaseDimensions< T > &a, const BaseDimensions< Y > &b)
Definition: Dimensions.h:137
A utility class to do read-only operations on a subset of an existing string.
Definition: UT_StringView.h:40
bool isUsable() const
Definition: UT_Url.h:73
void setPath(const UT_StringHolder &path)
Definition: UT_Url.h:113
Protocol protocol() const
Definition: UT_Url.h:95
int port() const
Definition: UT_Url.h:22
void setHost(const UT_StringHolder &host)
Definition: UT_Url.h:105
bool operator==(const UT_Url &url) const
Definition: UT_Url.h:132
GLsizei const GLchar *const * path
Definition: glew.h:6461
static const UT_Url theInvalidUrl
Definition: UT_Url.h:120
const UT_StringHolder & host() const
Definition: UT_Url.h:104
int rawPort() const
Definition: UT_Url.h:109
void setPort(int port)
Definition: UT_Url.h:110
const UT_StringHolder & queryString() const
Definition: UT_Url.h:115
void setPassword(const UT_StringHolder &pass)
Definition: UT_Url.h:102
bool operator!=(const BaseDimensions< T > &a, const BaseDimensions< Y > &b)
Definition: Dimensions.h:165
void setUser(const UT_StringHolder &user)
Definition: UT_Url.h:99
FormattingOptions
Definition: UT_Url.h:34
SYS_NO_DISCARD_RESULT SYS_FORCE_INLINE UT_StringView trim(const char *c=" \t\n\r") const