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