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_StringRef &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  // Helper if only the host and port are wanted.
84  {
85  UT_StringHolder str;
86  str.format("{}:{}", myHost, port());
87  return str;
88  }
89  UT_StringHolder protocolToString() const;
90 
91  bool usingDefaultPortForProtocol() const;
92  static bool isDefaultPortForProtocol(Protocol prot, int port);
93 
94  inline bool hasMatchingPath(const UT_Url &url) const;
95  inline bool hasMatchingPath(const UT_StringHolder &path) const;
96 
97  // This is the relaxed method for checking hosts.
98  bool hasMatchingHost(const UT_Url &url) const;
99  // This is the relaxed method for checking hosts.
100  bool hasMatchingHost(const UT_StringHolder &host) const;
101 
102  Protocol protocol() const { return myProtocol; }
103  void setProtocol(Protocol protocol) { myProtocol = protocol; }
104 
105  const UT_StringHolder &user() const { return myUser; }
106  void setUser(const UT_StringHolder &user) { myUser = user; }
107 
108  const UT_StringHolder &password() const { return myPassword; }
109  void setPassword(const UT_StringHolder &pass) { myPassword = pass; }
110 
111  const UT_StringHolder &host() const { return myHost; }
112  void setHost(const UT_StringHolder &host) { myHost = host; }
113 
114  // Get a port that is usable for network calls.
115  int port() const;
116  int rawPort() const { return myPort; }
117  void setPort(int port) { myPort = port; }
118 
119  const UT_StringHolder &path() const { return myPath; }
120  void setPath(const UT_StringHolder &path) { myPath = path; }
121 
122  const UT_StringHolder &queryString() const { return myQueryString; }
123  void setQueryString(const UT_StringHolder &query) { myQueryString = query; }
124 
125  // In some cases we want to return a const reference to a url or we
126  // want to pass an invalid url to signal not using it.
127  static const UT_Url theInvalidUrl;
128 private:
129  Protocol myProtocol;
130  UT_StringHolder myUser;
131  UT_StringHolder myPassword;
132  UT_StringHolder myHost;
133  int myPort;
134  UT_StringHolder myPath;
135  UT_StringHolder myQueryString;
136 };
137 
138 inline bool
139 UT_Url::operator==(const UT_Url &url) const
140 {
141  return myProtocol == url.myProtocol && myUser == url.myUser &&
142  myPassword == url.myPassword && hasMatchingHost(url) &&
143  port() == url.port() && hasMatchingPath(url) &&
144  myQueryString == url.myQueryString;
145 }
146 
147 inline bool
148 UT_Url::operator!=(const UT_Url &url) const
149 {
150  return !(*this == url);
151 }
152 
153 inline bool
155 {
156  return hasMatchingPath(url.path());
157 }
158 
159 inline bool
161 {
162  UT_StringView path_view(myPath.begin(), myPath.end());
163  UT_StringView other_path_view(path.begin(), path.end());
164 
165  // Trim off trailing and leading '/'
166  path_view = path_view.trim("/");
167  other_path_view = other_path_view.trim("/");
168 
169  return path_view == other_path_view;
170 }
171 
172 #endif // __UT_URL_H__
173 
bool hasMatchingPath(const UT_Url &url) const
Definition: UT_Url.h:154
void setQueryString(const UT_StringHolder &query)
Definition: UT_Url.h:123
void setProtocol(Protocol protocol)
Definition: UT_Url.h:103
SYS_FORCE_INLINE const_iterator begin() const
Protocol
Definition: UT_Url.h:25
bool operator!=(const UT_Url &url) const
Definition: UT_Url.h:148
const UT_StringHolder & path() const
Definition: UT_Url.h:119
bool hasMatchingHost(const UT_Url &url) const
const UT_StringHolder & password() const
Definition: UT_Url.h:108
GLsizei const GLchar *const * path
Definition: glcorearb.h:3341
#define UT_API
Definition: UT_API.h:14
const UT_StringHolder & user() const
Definition: UT_Url.h:105
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:120
Protocol protocol() const
Definition: UT_Url.h:102
int port() const
Definition: UT_Url.h:22
void setHost(const UT_StringHolder &host)
Definition: UT_Url.h:112
size_t format(const char *fmt, const Args &...args)
Format a string using the same formatting codes as UTformat.
UT_StringHolder toHostPort() const
Definition: UT_Url.h:83
bool operator==(const UT_Url &url) const
Definition: UT_Url.h:139
static const UT_Url theInvalidUrl
Definition: UT_Url.h:127
GLint GLint GLsizei GLint GLenum format
Definition: glcorearb.h:108
const UT_StringHolder & host() const
Definition: UT_Url.h:111
int rawPort() const
Definition: UT_Url.h:116
void setPort(int port)
Definition: UT_Url.h:117
const UT_StringHolder & queryString() const
Definition: UT_Url.h:122
void setPassword(const UT_StringHolder &pass)
Definition: UT_Url.h:109
bool operator!=(const BaseDimensions< T > &a, const BaseDimensions< Y > &b)
Definition: Dimensions.h:165
void setUser(const UT_StringHolder &user)
Definition: UT_Url.h:106
FormattingOptions
Definition: UT_Url.h:34
GLenum query
Definition: glew.h:5734
SYS_NO_DISCARD_RESULT SYS_FORCE_INLINE UT_StringView trim(const char *c=" \t\n\r") const