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 #include "UT_ORMColumnType.h"
20 
21 #include <iostream>
22 
23 class UT_SqlStatement;
24 
26 {
27 public:
28  enum Protocol
29  {
37  PROT_LDAPS
38  };
39 
40  enum FormattingOptions : unsigned
41  {
42  None = 0x0,
43  RemoveProtocol = 1 << 0,
44  RemovePassword = 1 << 1,
45  RemoveUserInfo = RemovePassword | 1 << 2,
46  RemovePort = 1 << 3,
47  RemovePath = 1 << 4,
48  RemoveQuery = 1 << 5
49  };
50 
51  UT_Url();
52  explicit UT_Url(const UT_StringRef &url_string,
53  Protocol force_protocol = PROT_UNKNOWN);
54  UT_Url(Protocol prot,
55  const UT_StringHolder &host,
56  int port,
57  const UT_StringHolder &path);
58  UT_Url(Protocol prot,
59  const UT_StringHolder &user,
60  const UT_StringHolder &password,
61  const UT_StringHolder &host,
62  int port,
63  const UT_StringHolder &path,
64  const UT_StringHolder &query_string);
65 
66  UT_Url(const UT_Url &url) = default;
67  UT_Url &operator=(const UT_Url &url) = default;
68  UT_Url(UT_Url &&url) = default;
69  UT_Url &operator=(UT_Url &&url) = default;
70 
71  UT_Url& operator=(const UT_StringRef& url_str)
72  {
73  parse(url_str);
74  return *this;
75  }
76 
77  // This clears the entire url and parses the url string passed in.
78  // Returns true if the url was successfully parsed.
79  bool parse(const UT_StringRef &str_url,
80  Protocol force_protocol = PROT_UNKNOWN);
81 
82  void clear();
83 
84  // The only part of the url that is actually required is the host.
85  bool isUsable() const { return !myHost.isEmpty(); }
86 
87  explicit operator bool() const { return isUsable(); }
88  bool operator==(const UT_Url &url) const;
89  bool operator!=(const UT_Url &url) const;
90 
91  // Use FormattingOptions to specify which elements of the url you would like
92  // to strip. The default is to include all elements when necessary
93  UT_StringHolder toString(unsigned format = FormattingOptions::None) const;
94  // Helper if only the host and port are wanted.
96  {
97  UT_StringHolder str;
98  str.format("{}:{}", myHost, port());
99  return str;
100  }
101  UT_StringHolder protocolToString() const;
102 
103  bool usingDefaultPortForProtocol() const;
104  static bool isDefaultPortForProtocol(Protocol prot, int port);
105 
106  inline bool hasMatchingPath(const UT_Url &url) const;
107  inline bool hasMatchingPath(const UT_StringHolder &path) const;
108 
109  // This is the relaxed method for checking hosts.
110  bool hasMatchingHost(const UT_Url &url) const;
111  // This is the relaxed method for checking hosts.
112  bool hasMatchingHost(const UT_StringHolder &host) const;
113 
114  Protocol protocol() const { return myProtocol; }
115  void setProtocol(Protocol protocol) { myProtocol = protocol; }
116 
117  const UT_StringHolder &user() const { return myUser; }
118  void setUser(const UT_StringHolder &user) { myUser = user; }
119 
120  const UT_StringHolder &password() const { return myPassword; }
121  void setPassword(const UT_StringHolder &pass) { myPassword = pass; }
122 
123  const UT_StringHolder &host() const { return myHost; }
124  void setHost(const UT_StringHolder &host) { myHost = host; }
125 
126  // Get a port that is usable for network calls.
127  int port() const;
128  int rawPort() const { return myPort; }
129  void setPort(int port) { myPort = port; }
130 
131  const UT_StringHolder &path() const { return myPath; }
132  void setPath(const UT_StringHolder &path) { myPath = path; }
133 
134  const UT_StringHolder &queryString() const { return myQueryString; }
135  void setQueryString(const UT_StringHolder &query) { myQueryString = query; }
136 
137  // In some cases we want to return a const reference to a url or we
138  // want to pass an invalid url to signal not using it.
139  static const UT_Url theInvalidUrl;
140 private:
141  Protocol myProtocol;
142  UT_StringHolder myCustomProtocol;
143  UT_StringHolder myUser;
144  UT_StringHolder myPassword;
145  UT_StringHolder myHost;
146  int myPort;
147  UT_StringHolder myPath;
148  UT_StringHolder myQueryString;
149 };
150 
151 inline bool
152 UT_Url::operator==(const UT_Url &url) const
153 {
154  return myProtocol == url.myProtocol && myUser == url.myUser &&
155  myPassword == url.myPassword && hasMatchingHost(url) &&
156  port() == url.port() && hasMatchingPath(url) &&
157  myQueryString == url.myQueryString;
158 }
159 
160 inline bool
161 UT_Url::operator!=(const UT_Url &url) const
162 {
163  return !(*this == url);
164 }
165 
166 inline bool
168 {
169  return hasMatchingPath(url.path());
170 }
171 
172 inline bool
174 {
175  UT_StringView path_view(myPath.begin(), myPath.end());
176  UT_StringView other_path_view(path.begin(), path.end());
177 
178  // Trim off trailing and leading '/'
179  path_view = path_view.trim("/");
180  other_path_view = other_path_view.trim("/");
181 
182  return path_view == other_path_view;
183 }
184 
185 UT_API bool UTsqlBind(UT_SqlStatement& stmt, int idx, const UT_Url& url);
186 UT_API bool UTsqlGet(const UT_SqlStatement& stmt, int idx , UT_Url& url);
187 
188 template <>
189 inline UT_ORMColumnType
191 {
193 }
194 
195 #endif // __UT_URL_H__
196 
bool hasMatchingPath(const UT_Url &url) const
Definition: UT_Url.h:167
GLenum query
Definition: glad.h:2772
void setQueryString(const UT_StringHolder &query)
Definition: UT_Url.h:135
void setProtocol(Protocol protocol)
Definition: UT_Url.h:115
SYS_FORCE_INLINE const_iterator begin() const
UT_API bool UTsqlBind(UT_SqlStatement &stmt, int idx, const UT_Url &url)
Protocol
Definition: UT_Url.h:28
bool operator!=(const UT_Url &url) const
Definition: UT_Url.h:161
const UT_StringHolder & path() const
Definition: UT_Url.h:131
bool hasMatchingHost(const UT_Url &url) const
const UT_StringHolder & password() const
Definition: UT_Url.h:120
GLsizei const GLchar *const * path
Definition: glcorearb.h:3341
UT_ORMColumnType
#define UT_API
Definition: UT_API.h:14
const UT_StringHolder & user() const
Definition: UT_Url.h:117
OutGridT const XformOp bool bool
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:85
void setPath(const UT_StringHolder &path)
Definition: UT_Url.h:132
UT_ORMColumnType UTsqlOrmColumnType< UT_Url >()
Definition: UT_Url.h:190
GLint GLint GLsizei GLint GLenum format
Definition: glcorearb.h:108
Protocol protocol() const
Definition: UT_Url.h:114
int port() const
Definition: UT_Url.h:25
void setHost(const UT_StringHolder &host)
Definition: UT_Url.h:124
size_t format(const char *fmt, const Args &...args)
Format a string using the same formatting codes as UTformat.
UT_API bool UTsqlGet(const UT_SqlStatement &stmt, int idx, UT_Url &url)
UT_StringHolder toHostPort() const
Definition: UT_Url.h:95
bool operator==(const UT_Url &url) const
Definition: UT_Url.h:152
static const UT_Url theInvalidUrl
Definition: UT_Url.h:139
const UT_StringHolder & host() const
Definition: UT_Url.h:123
int rawPort() const
Definition: UT_Url.h:128
LeafData & operator=(const LeafData &)=delete
void setPort(int port)
Definition: UT_Url.h:129
const UT_StringHolder & queryString() const
Definition: UT_Url.h:134
UT_Url & operator=(const UT_StringRef &url_str)
Definition: UT_Url.h:71
void setPassword(const UT_StringHolder &pass)
Definition: UT_Url.h:121
bool operator!=(const BaseDimensions< T > &a, const BaseDimensions< Y > &b)
Definition: Dimensions.h:165
void setUser(const UT_StringHolder &user)
Definition: UT_Url.h:118
FormattingOptions
Definition: UT_Url.h:40
SYS_NO_DISCARD_RESULT SYS_FORCE_INLINE UT_StringView trim(const char *c=" \t\n\r") const