PTLib  Version 2.18.8
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
url.h
Go to the documentation of this file.
1 /*
2  * url.h
3  *
4  * Universal Resource Locator (for HTTP/HTML) class.
5  *
6  * Portable Windows Library
7  *
8  * Copyright (c) 1993-2002 Equivalence Pty. Ltd.
9  *
10  * The contents of this file are subject to the Mozilla Public License
11  * Version 1.0 (the "License"); you may not use this file except in
12  * compliance with the License. You may obtain a copy of the License at
13  * http://www.mozilla.org/MPL/
14  *
15  * Software distributed under the License is distributed on an "AS IS"
16  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
17  * the License for the specific language governing rights and limitations
18  * under the License.
19  *
20  * The Original Code is Portable Windows Library.
21  *
22  * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
23  *
24  * Contributor(s): ______________________________________.
25  */
26 
27 #ifndef PTLIB_PURL_H
28 #define PTLIB_PURL_H
29 
30 #ifdef P_USE_PRAGMA
31 #pragma interface
32 #endif
33 
34 
35 #if P_URL
36 
37 #include <ptlib/pfactory.h>
38 
39 
40 class PURLScheme;
41 class PURLLegacyScheme;
42 
43 
45 // PURL
46 
56 class PURL : public PObject
57 {
59  public:
61  PURL();
63  PURL(
64  const char * cstr,
65  const char * defaultScheme = "http"
66  );
68  PURL(
69  const PString & str,
70  const char * defaultScheme = "http"
71  );
73  PURL(
74  const PFilePath & path
75  );
76 
77  PURL(const PURL & other);
78  PURL & operator=(const PURL & other);
79 
88  virtual Comparison Compare(
89  const PObject & obj
90  ) const;
91 
103  virtual PINDEX HashFunction() const;
104 
107  virtual void PrintOn(
108  ostream &strm
109  ) const;
110 
114  virtual void ReadFrom(
115  istream &strm
116  );
118 
122  inline PBoolean Parse(
123  const char * cstr,
124  const char * defaultScheme = NULL
125  ) { return InternalParse(cstr, defaultScheme); }
127  inline PBoolean Parse(
128  const PString & str,
129  const char * defaultScheme = NULL
130  ) { return InternalParse((const char *)str, defaultScheme); }
131 
133  enum UrlFormat {
138 
139  // for backward compatibility
142  };
143 
151  UrlFormat fmt = FullURL
152  ) const;
153  operator PString() const { return AsString(); }
154 
158  PFilePath AsFilePath() const;
159 
172  };
173 
181  static PString TranslateString(
182  const PString & str,
183  TranslationType type
184  );
185 
193  static PString UntranslateString(
194  const PString & str,
195  TranslationType type
196  );
197 
199  static void SplitVars(
200  const PString & str,
201  PStringToString & vars,
202  char sep1 = ';',
203  char sep2 = '=',
205  );
206 
208  static void SplitQueryVars(
209  const PString & queryStr,
210  PStringToString & queryVars
211  ) { SplitVars(queryStr, queryVars, '&', '=', QueryTranslation); }
212 
215  static void OutputVars(
216  ostream & strm,
217  const PStringToString & vars,
218  char sep0 = ';',
219  char sep1 = ';',
220  char sep2 = '=',
222  );
223 
224 
226  static PCaselessString ExtractScheme(const char * str);
227 
229  const PCaselessString & GetScheme() const { return m_scheme; }
230 
232  bool SetScheme(const PString & scheme);
233 
235  const PString & GetUserName() const { return m_username; }
236 
238  void SetUserName(const PString & username);
239 
241  const PString & GetPassword() const { return m_password; }
242 
244  void SetPassword(const PString & password);
245 
247  const PCaselessString & GetHostName() const { return m_hostname; }
248 
250  void SetHostName(const PString & hostname);
251 
253  WORD GetPort() const { return m_port; }
254 
256  void SetPort(WORD newPort);
257 
260 
262  PString GetHostPort() const;
263 
266 
268  PString GetPathStr() const;
269 
271  void SetPathStr(const PString & pathStr);
272 
274  const PStringArray & GetPath() const { return m_path; }
275 
277  void SetPath(const PStringArray & path);
278 
280  void AppendPath(const PString & segment);
281 
283  void ChangePath(
284  const PString & segment,
285  PINDEX idx = P_MAX_INDEX
286  );
287 
289  PString GetParameters() const;
290 
292  void SetParameters(const PString & parameters);
293 
296  const PStringOptions & GetParamVars() const { return m_paramVars; }
297 
300  void SetParamVars(
301  const PStringToString & paramVars,
302  bool merge = false
303  );
304 
307  void SetParamVar(
308  const PString & key,
309  const PString & data,
310  bool emptyDataDeletes = true
311  );
312 
314  const PString & GetFragment() const { return m_fragment; }
315 
317  PString GetQuery() const;
318 
321  void SetQuery(const PString & query);
322 
325  const PStringOptions & GetQueryVars() const { return m_queryVars; }
326 
329  void SetQueryVars(const PStringToString & queryVars);
330 
333  void SetQueryVar(const PString & key, const PString & data);
334 
336  const PString & GetContents() const { return m_contents; }
337 
339  void SetContents(const PString & str);
340 
342  PBoolean IsEmpty() const { return m_urlString.IsEmpty(); }
343 
344 
345  struct LoadParams {
347  const PString & requiredContentType = PString::Empty(),
348  const PTimeInterval & timeout = PMaxTimeInterval
349  ) : m_requiredContentType(requiredContentType)
350  , m_timeout(timeout)
351  {
352  }
353 
356 
357  PString m_username; // Basic authentication
359 #if P_SSL
360  PString m_authority; // Directory, file or data
361  PString m_certificate; // File or data
362  PString m_privateKey; // File or data
363 #endif
364  PStringOptions m_customOptions; // E.g. headers for http
365  };
370  bool LoadResource(
371  PString & data,
372  const LoadParams & params = LoadParams()
373  ) const;
374  bool LoadResource(
375  PBYTEArray & data,
376  const LoadParams & params = LoadParams()
377  ) const;
378 
379  // For backward compatibility
380  template <class T>
382  T & data,
383  const PString & requiredContentType = PString::Empty(),
384  const PTimeInterval & timeout = PMaxTimeInterval
385  ) const { return LoadResource(data, LoadParams(requiredContentType, timeout)); }
386 
393  bool OpenBrowser() const { return OpenBrowser(AsString()); }
394  static bool OpenBrowser(
395  const PString & url
396  );
398 
399  bool LegacyParse(const char * str, const PURLLegacyScheme * schemeInfo);
400  PString LegacyAsString(PURL::UrlFormat fmt, const PURLLegacyScheme * schemeInfo) const;
401 
402  protected:
403  void CopyContents(const PURL & other);
404  virtual PBoolean InternalParse(
405  const char * cstr,
406  const char * defaultScheme
407  );
408  void Recalculate();
409 
412 
417  WORD m_port;
424  PString m_contents; // Anything left after parsing other elements
425 };
426 
427 
429 // PURLScheme
430 
431 class PURLScheme : public PObject
432 {
433  PCLASSINFO(PURLScheme, PObject);
434  public:
435  virtual bool Parse(const char * cstr, PURL & url) const = 0;
436  virtual PString AsString(PURL::UrlFormat fmt, const PURL & purl) const = 0;
437  virtual WORD GetDefaultPort() const { return 0; }
438 };
439 
441 
442 
444 // PURLLegacyScheme
445 
447 {
448  public:
450  bool user = false,
451  bool pass = false,
452  bool host = false,
453  bool def = false,
454  bool defhost = false,
455  bool query = false,
456  bool params = false,
457  bool frags = false,
458  bool path = false,
459  bool rel = false,
460  WORD port = 0
461  )
462  : hasUsername (user)
463  , hasPassword (pass)
464  , hasHostPort (host)
465  , defaultToUserIfNoAt (def)
466  , defaultHostToLocal (defhost)
467  , hasQuery (query)
468  , hasParameters (params)
469  , hasFragments (frags)
470  , hasPath (path)
471  , relativeImpliesScheme (rel)
472  , defaultPort (port)
473  { }
474 
475  bool Parse(const char * cstr, PURL & url) const
476  {
477  return url.LegacyParse(cstr, this);
478  }
479 
480  PString AsString(PURL::UrlFormat fmt, const PURL & url) const
481  {
482  return url.LegacyAsString(fmt, this);
483  }
484 
485  virtual WORD GetDefaultPort() const { return defaultPort; }
486 
492  bool hasQuery;
495  bool hasPath;
498 };
499 
506 #define PURL_LEGACY_SCHEME(schemeName, \
507  hasUsername, /* URL scheme has a username */ \
508  hasPassword, /* URL scheme has a password */ \
509  hasHostPort, /* URL scheme has a host:port */ \
510  defaultToUserIfNoAt, /* URL scheme is username if no @, otherwise host:port */ \
511  defaultHostToLocal, /* URL scheme defaults to PIPSocket::GetHostName() if not present */ \
512  hasQuery, /* URL scheme has a query section */ \
513  hasParameters, /* URL scheme has a parameter section */ \
514  hasFragments, /* URL scheme has a fragment section */ \
515  hasPath, /* URL scheme has a path */ \
516  relativeImpliesScheme, /* URL scheme has relative path (no //) then scheme: is not output */ \
517  defaultPort) /* URL scheme default port if not specified in host:port */ \
518  class PURLLegacyScheme_##schemeName : public PURLLegacyScheme \
519  { \
520  public: \
521  PURLLegacyScheme_##schemeName() \
522  : PURLLegacyScheme(hasUsername, \
523  hasPassword, \
524  hasHostPort, \
525  defaultToUserIfNoAt, \
526  defaultHostToLocal, \
527  hasQuery, \
528  hasParameters, \
529  hasFragments, \
530  hasPath, \
531  relativeImpliesScheme, \
532  defaultPort) \
533  { } \
534  }; \
535  PFACTORY_CREATE(PURLSchemeFactory, PURLLegacyScheme_##schemeName, #schemeName, true)
536 
537 
538 
540 // PURLLoader
541 
542 class PURLLoader : public PObject
543 {
544  PCLASSINFO(PURLLoader, PObject);
545  public:
546  virtual bool Load(PString & str, const PURL & url, const PURL::LoadParams & params) const = 0;
547  virtual bool Load(PBYTEArray & data, const PURL & url, const PURL::LoadParams & params) const = 0;
548 };
549 
551 
552 #if P_HTTP
553  PFACTORY_LOAD(PURL_HttpLoader);
554 #endif // P_HTTP
555 #if P_FTP
556  PFACTORY_LOAD(PURL_FtpLoader);
557 #endif // P_HTTP
558 
559 
560 #endif // P_URL
561 
562 #endif // PTLIB_PURL_H
563 
564 
565 // End Of File ///////////////////////////////////////////////////////////////
PStringOptions m_queryVars
Definition: url.h:423
Translate the path field for a URL.
Definition: url.h:165
void SetParamVars(const PStringToString &paramVars, bool merge=false)
Set the parameter (;) field(s) of the URL as a string dictionary.
virtual PBoolean InternalParse(const char *cstr, const char *defaultScheme)
#define PMaxTimeInterval
Definition: timeint.h:31
WORD GetPort() const
Get the port field of the URL.
Definition: url.h:253
virtual void ReadFrom(istream &strm)
Input the contents of the URL from the stream.
bool defaultToUserIfNoAt
Definition: url.h:490
static void SplitQueryVars(const PString &queryStr, PStringToString &queryVars)
Split a string in &amp;= form to a dictionary of names and values.
Definition: url.h:208
const PStringOptions & GetQueryVars() const
Get the Query (?) field of the URL as a string dictionary.
Definition: url.h:325
PFactory< PURLLoader > PURLLoaderFactory
Definition: url.h:550
This class defines an arbitrary time interval to millisecond accuracy.
Definition: timeint.h:51
void SetPathStr(const PString &pathStr)
Set the path field of the URL as a string.
Definition: url.h:446
PBoolean IsEmpty() const
Return true if the URL is an empty string.
Definition: url.h:342
Output full URI.
Definition: url.h:134
PCaselessString m_scheme
Definition: url.h:413
PString m_contents
Definition: url.h:424
virtual Comparison Compare(const PObject &obj) const
Compare the two URLs and return their relative rank.
bool hasQuery
Definition: url.h:492
bool hasUsername
Definition: url.h:487
virtual bool Parse(const char *cstr, PURL &url) const =0
const PURLScheme * m_schemeInfo
Definition: url.h:410
void SetHostName(const PString &hostname)
Set the hostname field of the URL.
PURL & operator=(const PURL &other)
TranslationType
Type for translation of strings to URL format,.
Definition: url.h:161
static void OutputVars(ostream &strm, const PStringToString &vars, char sep0= ';', char sep1= ';', char sep2= '=', TranslationType type=ParameterTranslation)
Construct string from a dictionary using separators.
PBoolean Parse(const char *cstr, const char *defaultScheme=NULL)
Parse the URL string into the fields in the object instance.
Definition: url.h:122
bool hasHostPort
Definition: url.h:489
bool LoadResource(PString &data, const LoadParams &params=LoadParams()) const
Get the resource the URL is pointing at.
void ChangePath(const PString &segment, PINDEX idx=P_MAX_INDEX)
Change segment in the path field of the URL.
Translate the parameter variables field for a URL.
Definition: url.h:169
void Recalculate()
PString LegacyAsString(PURL::UrlFormat fmt, const PURLLegacyScheme *schemeInfo) const
This is a dictionary collection class of PString objects, keyed by another string.
Definition: pstring.h:3151
This class describes a full description for a file on the particular platform.
Definition: filepath.h:61
PString GetHostPort() const
Get the hostname and optional port fields of the URL.
void SetUserName(const PString &username)
Set the username field of the URL.
Comparison
Result of the comparison operation performed by the Compare() function.
Definition: object.h:2251
This class is a variation of a string that ignores case.
Definition: pstring.h:2012
PBoolean Parse(const PString &str, const char *defaultScheme=NULL)
Parse the URL string into the fields in the object instance.
Definition: url.h:127
void AppendPath(const PString &segment)
Append segment to the path field of the URL.
PURLLegacyScheme(bool user=false, bool pass=false, bool host=false, bool def=false, bool defhost=false, bool query=false, bool params=false, bool frags=false, bool path=false, bool rel=false, WORD port=0)
Definition: url.h:449
This is an array collection class of PString objects.
Definition: pstring.h:2365
void SetContents(const PString &str)
Set the contents of URL, data left after all elemetns are parsed out.
Translate the quoted parameter variables field for a URL.
Definition: url.h:171
PString m_password
Definition: url.h:415
static void SplitVars(const PString &str, PStringToString &vars, char sep1= ';', char sep2= '=', TranslationType type=ParameterTranslation)
Split a string to a dictionary of names and values.
Translate the query variable field for a URL.
Definition: url.h:167
void SetQueryVar(const PString &key, const PString &data)
Set the Query (?) field of the URL as a string dictionary.
const PStringOptions & GetParamVars() const
Get the parameter (;) field(s) of the URL as a string dictionary.
Definition: url.h:296
virtual PBoolean IsEmpty() const
Determine if the string is empty.
PStringArray m_path
Definition: url.h:420
PBoolean GetPortSupplied() const
Get if explicit port is specified.
Definition: url.h:259
static PString UntranslateString(const PString &str, TranslationType type)
Untranslate a string from a form that was included into a URL into a normal string.
PURL()
Construct a new URL object from the URL string.
void SetPath(const PStringArray &path)
Set the path field of the URL as a string array.
WORD m_port
Definition: url.h:417
virtual PString AsString(PURL::UrlFormat fmt, const PURL &purl) const =0
Definition: url.h:140
PCaselessString m_hostname
Definition: url.h:416
PString GetQuery() const
Get the Query (?) field of the URL as a string.
Array of unsigned characters.
Definition: array.h:605
PString GetPathStr() const
Get the path field of the URL as a string.
Specialised version of PStringToString to contain a dictionary of options/attributes.
Definition: pstring.h:3249
bool m_relativePath
port was supplied in string input
Definition: url.h:419
PTimeInterval m_timeout
Definition: url.h:355
const PString & GetPassword() const
Get the password field of the URL.
Definition: url.h:241
const PCaselessString & GetHostName() const
Get the hostname field of the URL.
Definition: url.h:247
bool hasParameters
Definition: url.h:493
Definition: url.h:542
virtual PINDEX HashFunction() const
This function yields a hash value required by the PDictionary class.
#define PFACTORY_LOAD(ConcreteType)
Definition: pfactory.h:557
virtual WORD GetDefaultPort() const
Definition: url.h:437
void SetPort(WORD newPort)
Set the port field in the URL. Zero resets to default.
Translate to a string as only path.
Definition: url.h:135
PString m_password
Definition: url.h:358
PStringOptions m_customOptions
Definition: url.h:364
static PString TranslateString(const PString &str, TranslationType type)
Translate a string from general form to one that can be included into a URL.
PFactory< PURLScheme > PURLSchemeFactory
Definition: url.h:440
void SetQuery(const PString &query)
Set the Query (?) field of the URL as a string.
bool PBoolean
Definition: object.h:174
const PStringArray & GetPath() const
Get the path field of the URL as a string array.
Definition: url.h:274
virtual WORD GetDefaultPort() const
Definition: url.h:485
static PCaselessString ExtractScheme(const char *str)
Extract scheme as per RFC2396.
#define P_MAX_INDEX
Definition: object.h:80
PStringOptions m_paramVars
Definition: url.h:421
The character string class.
Definition: pstring.h:108
PString m_urlString
Definition: url.h:411
bool Parse(const char *cstr, PURL &url) const
Definition: url.h:475
PFilePath AsFilePath() const
Get the &quot;file:&quot; URL as a file path.
Translate to a string with no scheme or host, just the relative part.
Definition: url.h:137
PString GetParameters() const
Get the parameter (;) field of the URL.
bool hasPassword
Definition: url.h:488
bool LegacyParse(const char *str, const PURLLegacyScheme *schemeInfo)
Definition: url.h:431
Translate to a string with the location (scheme and user/pass/host/port)
Definition: url.h:136
const PCaselessString & GetScheme() const
Get the scheme field of the URL.
Definition: url.h:229
UrlFormat
Print/String output representation formats.
Definition: url.h:133
static const PString & Empty()
Return an empty string.
const PString & GetFragment() const
Get the fragment (#) field of the URL.
Definition: url.h:314
PString m_username
Definition: url.h:357
Class for a factory to create concrete class instances without parameters during construction.
Definition: pfactory.h:396
bool OpenBrowser() const
Open the URL in a browser.
Definition: url.h:393
void SetParamVar(const PString &key, const PString &data, bool emptyDataDeletes=true)
Set the parameter (;) field of the URL as a string dictionary.
PString m_fragment
Definition: url.h:422
PString AsString(PURL::UrlFormat fmt, const PURL &url) const
Definition: url.h:480
bool m_portSupplied
Definition: url.h:418
Translate a username/password field for a URL.
Definition: url.h:163
bool hasFragments
Definition: url.h:494
const PString & GetUserName() const
Get the username field of the URL.
Definition: url.h:235
virtual void PrintOn(ostream &strm) const
Output the contents of the URL to the stream as a string.
bool hasPath
Definition: url.h:495
bool defaultHostToLocal
Definition: url.h:491
PString m_username
Definition: url.h:414
#define PCLASSINFO_WITH_CLONE(cls, par)
Declare all the standard PTLib class information, plus Clone().
Definition: object.h:2167
LoadParams(const PString &requiredContentType=PString::Empty(), const PTimeInterval &timeout=PMaxTimeInterval)
Definition: url.h:346
void SetPassword(const PString &password)
Set the password field of the URL.
PString m_requiredContentType
Definition: url.h:354
bool LoadResource(T &data, const PString &requiredContentType=PString::Empty(), const PTimeInterval &timeout=PMaxTimeInterval) const
Definition: url.h:381
const PString & GetContents() const
Get the contents of URL, data left after all elemetns are parsed out.
Definition: url.h:336
WORD defaultPort
Definition: url.h:497
void SetParameters(const PString &parameters)
Set the parameter (;) field of the URL.
bool relativeImpliesScheme
Definition: url.h:496
PString AsString(UrlFormat fmt=FullURL) const
Convert the URL object into its string representation.
Ultimate parent class for all objects in the class library.
Definition: object.h:2204
This class describes a Universal Resource Locator.
Definition: url.h:56
PBoolean GetRelativePath() const
Get if path is relative or absolute.
Definition: url.h:265
Definition: url.h:141
void SetQueryVars(const PStringToString &queryVars)
Set the Query (?) field(s) of the URL as a string dictionary.
Definition: url.h:345
bool SetScheme(const PString &scheme)
Set the scheme field of the URL.
virtual bool Load(PString &str, const PURL &url, const PURL::LoadParams &params) const =0
void CopyContents(const PURL &other)