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