125
127
+---------------------------------------------------------------------*/
126
128
class NPT_Url : public NPT_Uri {
128
// constructors and destructor
131
* Default constructor. This does not construct a valid URL, but an
132
* uninitialized one that can later be initialized to a valid URL by
133
* parsing or setting some of its fields.
130
NPT_Url(const char* url,
131
SchemeId expected_scheme = SCHEME_ID_UNKNOWN,
132
NPT_UInt16 default_port = NPT_URL_INVALID_PORT);
138
* Construct a URL by parsing an input string in its fully encoded form.
139
* If an error occurs during parsing (such as an invalid syntax), the
140
* URL will be in an invalid state (a call to IsValid() will return false).
142
* @param url The URL string in its encoded form
143
* @param default_port The default port number, or 0 if not specified
145
NPT_Url(const char* url, NPT_UInt16 default_port = 0);
148
* Construct a URL from its components. When constructing a URL from
149
* components, the components are assumed to be passed in their non-encoded
150
* form, and will thus be encoded automatically.
152
* @param scheme The URL scheme
153
* @param port The port number
154
* @param path The path
155
* @param query The query, if any, or NULL
156
* @param fragment The fragment, if any, or NULL
133
158
NPT_Url(const char* scheme,
134
159
const char* host,
137
162
const char* query = NULL,
138
163
const char* fragment = NULL);
141
const NPT_String& GetHost() const { return m_Host; }
142
NPT_UInt16 GetPort() const { return m_Port; }
143
const NPT_String& GetPath() const { return m_Path; }
144
const NPT_String& GetQuery() const { return m_Query; }
145
const NPT_String& GetFragment() const { return m_Fragment; }
146
virtual bool IsValid() const;
147
bool HasQuery() const { return m_HasQuery; }
148
bool HasFragment() const { return m_HasFragment; }
149
NPT_Result SetHost(const char* host);
150
NPT_Result SetPort(NPT_UInt16 port);
151
NPT_Result SetPath(const char* path);
152
NPT_Result SetPathPlus(const char* path_plus);
153
NPT_Result SetQuery(const char* query);
154
NPT_Result SetFragment(const char* fragment);
166
* Parse a URL from its fully encoded form.
168
* @param url The URL string in its encoded form
169
* @param default port The defautl port number, or 0 if not specified
171
NPT_Result Parse(const char* url, NPT_UInt16 default_port = 0);
174
* Parse just the path plus optional query and fragment from a fully encoded form.
176
* @param path_plus The URL path plus optional query and fragment
178
NPT_Result ParsePathPlus(const char* path_plus);
181
* Returns the host part of the URL, in its encoded form
183
const NPT_String& GetHost() const { return m_Host; }
186
* Returns the port number of the URL.
188
NPT_UInt16 GetPort() const { return m_Port; }
191
* Returns the path part of the URL, in its encoded form
193
const NPT_String& GetPath() const { return m_Path; }
196
* Returns the path part of the URL, in its encoded or decoded form
198
NPT_String GetPath(bool decoded) const { return decoded?NPT_Uri::PercentDecode(m_Path):m_Path;}
201
* Returns the query part of the URL, in its encoded form
203
const NPT_String& GetQuery() const { return m_Query; }
206
* Returns the fragment part of the URL, in its encoded form
208
const NPT_String& GetFragment() const { return m_Fragment; }
211
* Returns whether the URL is valid or not. Invalid URLs are uninitialized or
212
* not fully initialized URLs.
214
* @return true if the URL is valid, false if it is not.
216
virtual bool IsValid() const;
219
* Resets a URL to an uninitialized state.
224
* Returns whether the URL has a query part or not.
226
* @return true if the URL has a query part, false if it does not.
228
bool HasQuery() const { return m_HasQuery; }
231
* Returns whether the URL has a fragment part or not.
233
* @return true if the URL has a fragment part, false if it does not.
235
bool HasFragment() const { return m_HasFragment; }
238
* Sets the host part of the URL.
240
* @param host The host part of the URL
242
NPT_Result SetHost(const char* host);
245
* Sets the port number of the URL.
247
* @param port The port number of the URL
249
NPT_Result SetPort(NPT_UInt16 port);
252
* Sets the path part of the URL.
254
* @param path The path part of the URL
255
* @param encoded Boolean flag indicating whether the path parameter is
256
* already encoded or not. If it is not already encoded, it will be
257
* automatically encoded.
259
NPT_Result SetPath(const char* path, bool encoded=false);
262
* Sets the query part of the URL.
264
* @param query The query part of the URL
265
* @param encoded Boolean flag indicating whether the query parameter is
266
* already encoded or not. If it is not already encoded, it will be
267
* automatically encoded.
269
NPT_Result SetQuery(const char* query, bool encoded=false);
272
* Sets the fragment part of the URL.
274
* @param query The fragment part of the URL
275
* @param encoded Boolean flag indicating whether the fragment parameter is
276
* already encoded or not. If it is not already encoded, it will be
277
* automatically encoded.
279
NPT_Result SetFragment(const char* fragment, bool encoded=false);
282
* Return the string representation of the URL in a way that can be used in
283
* an HTTP request (i.e just the portion of the URL starting with the path)
285
* @param with_fragment Boolean flag specifiying whether the fragment part of
286
* the URL should be included in the returned string or not.
155
288
virtual NPT_String ToRequestString(bool with_fragment = false) const;
291
* Return the string representation of the URL.
293
* @param default_port default port number for the scheme. If the port number of
294
* the URL is not equal to the default port, then port number is explicitely
295
* included in the string representation of the URL.
296
* @param with_fragment Boolean flag specifiying whether the fragment part of
297
* the URL should be included in the returned string or not.
156
299
virtual NPT_String ToStringWithDefaultPort(NPT_UInt16 default_port, bool with_fragment = true) const;
302
* Return the string representation of the URL.
304
* @param with_fragment Boolean flag specifiying whether the fragment part of
305
* the URL should be included in the returned string or not.
157
307
virtual NPT_String ToString(bool with_fragment = true) const;