PTLib  Version 2.14.3
 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  * $Revision: 32250 $
27  * $Author: rjongbloed $
28  * $Date: 2014-06-28 14:55:01 +1000 (Sat, 28 Jun 2014) $
29  */
30 
31 #ifndef PTLIB_PURL_H
32 #define PTLIB_PURL_H
33 
34 #ifdef P_USE_PRAGMA
35 #pragma interface
36 #endif
37 
38 
39 #if P_URL
40 
41 #include <ptlib/pfactory.h>
42 
43 
44 class PURLScheme;
45 class PURLLegacyScheme;
46 
47 
49 // PURL
50 
60 class PURL : public PObject
61 {
63  public:
65  PURL();
67  PURL(
68  const char * cstr,
69  const char * defaultScheme = "http"
70  );
72  PURL(
73  const PString & str,
74  const char * defaultScheme = "http"
75  );
77  PURL(
78  const PFilePath & path
79  );
80 
81  PURL(const PURL & other);
82  PURL & operator=(const PURL & other);
83 
92  virtual Comparison Compare(
93  const PObject & obj
94  ) const;
95 
107  virtual PINDEX HashFunction() const;
108 
111  virtual void PrintOn(
112  ostream &strm
113  ) const;
114 
118  virtual void ReadFrom(
119  istream &strm
120  );
122 
126  inline PBoolean Parse(
127  const char * cstr,
128  const char * defaultScheme = NULL
129  ) { return InternalParse(cstr, defaultScheme); }
131  inline PBoolean Parse(
132  const PString & str,
133  const char * defaultScheme = NULL
134  ) { return InternalParse((const char *)str, defaultScheme); }
135 
137  enum UrlFormat {
142 
143  // for backward compatibility
146  };
147 
155  UrlFormat fmt = FullURL
156  ) const;
157  operator PString() const { return AsString(); }
158 
162  PFilePath AsFilePath() const;
163 
176  };
177 
185  static PString TranslateString(
186  const PString & str,
187  TranslationType type
188  );
189 
197  static PString UntranslateString(
198  const PString & str,
199  TranslationType type
200  );
201 
203  static void SplitVars(
204  const PString & str,
205  PStringToString & vars,
206  char sep1 = ';',
207  char sep2 = '=',
209  );
210 
212  static void SplitQueryVars(
213  const PString & queryStr,
215  ) { SplitVars(queryStr, queryVars, '&', '=', QueryTranslation); }
216 
219  static void OutputVars(
220  ostream & strm,
221  const PStringToString & vars,
222  char sep0 = ';',
223  char sep1 = ';',
224  char sep2 = '=',
226  );
227 
228 
230  static PCaselessString ExtractScheme(const char * str);
231 
233  const PCaselessString & GetScheme() const { return scheme; }
234 
236  bool SetScheme(const PString & scheme);
237 
239  const PString & GetUserName() const { return username; }
240 
242  void SetUserName(const PString & username);
243 
245  const PString & GetPassword() const { return password; }
246 
248  void SetPassword(const PString & password);
249 
251  const PCaselessString & GetHostName() const { return hostname; }
252 
254  void SetHostName(const PString & hostname);
255 
257  WORD GetPort() const { return port; }
258 
260  void SetPort(WORD newPort);
261 
264 
266  PString GetHostPort() const;
267 
270 
272  PString GetPathStr() const;
273 
275  void SetPathStr(const PString & pathStr);
276 
278  const PStringArray & GetPath() const { return path; }
279 
281  void SetPath(const PStringArray & path);
282 
284  void AppendPath(const PString & segment);
285 
287  void ChangePath(
288  const PString & segment,
289  PINDEX idx = P_MAX_INDEX
290  );
291 
293  PString GetParameters() const;
294 
296  void SetParameters(const PString & parameters);
297 
300  const PStringOptions & GetParamVars() const { return paramVars; }
301 
304  void SetParamVars(const PStringToString & paramVars);
305 
308  void SetParamVar(
309  const PString & key,
310  const PString & data,
311  bool emptyDataDeletes = true
312  );
313 
315  const PString & GetFragment() const { return fragment; }
316 
318  PString GetQuery() const;
319 
322  void SetQuery(const PString & query);
323 
326  const PStringOptions & GetQueryVars() const { return queryVars; }
327 
330  void SetQueryVars(const PStringToString & queryVars);
331 
334  void SetQueryVar(const PString & key, const PString & data);
335 
337  const PString & GetContents() const { return m_contents; }
338 
340  void SetContents(const PString & str);
341 
343  PBoolean IsEmpty() const { return urlString.IsEmpty(); }
344 
345 
346  struct LoadParams {
348  const PString & requiredContentType = PString::Empty(),
349  const PTimeInterval & timeout = PMaxTimeInterval
350  ) : m_requiredContentType(requiredContentType)
351  , m_timeout(timeout)
352  {
353  }
354 
357 
358  PString m_username; // Basic authentication
360 #if P_SSL
361  PString m_authority; // Directory, file or data
362  PString m_certificate; // File or data
363  PString m_privateKey; // File or data
364 #endif
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);
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 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 ysername */ \
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 secton */ \
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 ///////////////////////////////////////////////////////////////