url.h

Go to the documentation of this file.
00001 /*
00002  * url.h
00003  *
00004  * Universal Resource Locator (for HTTP/HTML) class.
00005  *
00006  * Portable Windows Library
00007  *
00008  * Copyright (c) 1993-2002 Equivalence Pty. Ltd.
00009  *
00010  * The contents of this file are subject to the Mozilla Public License
00011  * Version 1.0 (the "License"); you may not use this file except in
00012  * compliance with the License. You may obtain a copy of the License at
00013  * http://www.mozilla.org/MPL/
00014  *
00015  * Software distributed under the License is distributed on an "AS IS"
00016  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
00017  * the License for the specific language governing rights and limitations
00018  * under the License.
00019  *
00020  * The Original Code is Portable Windows Library.
00021  *
00022  * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
00023  *
00024  * Contributor(s): ______________________________________.
00025  *
00026  * $Log: url.h,v $
00027  * Revision 1.36  2007/09/11 10:34:14  hfriederich
00028  * Get access to portSupplied variable
00029  *
00030  * Revision 1.35  2007/04/18 23:49:50  csoutheren
00031  * Add usage of precompiled headers
00032  *
00033  * Revision 1.34  2005/11/30 12:47:37  csoutheren
00034  * Removed tabs, reformatted some code, and changed tags for Doxygen
00035  *
00036  * Revision 1.33  2005/04/20 05:19:48  csoutheren
00037  * Patch 1185334. Ensure SIP URLs correctly store status of port
00038  * Thanks to Ted Szoczei
00039  *
00040  * Revision 1.32  2005/01/04 07:44:02  csoutheren
00041  * More changes to implement the new configuration methodology, and also to
00042  * attack the global static problem
00043  *
00044  * Revision 1.31  2004/12/08 00:51:11  csoutheren
00045  * Move PURLLegacyScheme to header file to allow external usage
00046  *
00047  * Revision 1.30  2004/11/11 07:34:50  csoutheren
00048  * Added #include <ptlib.h>
00049  *
00050  * Revision 1.29  2004/07/07 07:18:43  csoutheren
00051  * Removed warnings on Linux from Windows static global hacks
00052  *
00053  * Revision 1.28  2004/07/06 10:12:51  csoutheren
00054  * Added static integer o factory template to assist in ensuring factories are instantiated
00055  *
00056  * Revision 1.27  2004/06/01 07:32:45  csoutheren
00057  * Removed warning on Linux
00058  *
00059  * Revision 1.26  2004/06/01 07:28:44  csoutheren
00060  * Changed URL parsing to use abstract factory code
00061  *
00062  * Revision 1.25  2004/03/13 06:30:52  rjongbloed
00063  * Virtualised parse function.
00064  *
00065  * Revision 1.24  2003/04/04 05:18:08  robertj
00066  * Added "callto", "tel" and fixed "h323" URL types.
00067  *
00068  * Revision 1.23  2002/12/10 04:40:34  robertj
00069  * Added test function for URL being empty.
00070  *
00071  * Revision 1.22  2002/11/20 00:50:09  robertj
00072  * Fixed correct interpretation of url re double slashes as per latest RFC,
00073  *   including file: mapping and relative paths. Probably still more to do.
00074  *
00075  * Revision 1.21  2002/11/19 10:36:08  robertj
00076  * Added functions to set anf get "file:" URL. as PFilePath and do the right
00077  *   things with platform dependent directory components.
00078  *
00079  * Revision 1.20  2002/11/06 22:47:24  robertj
00080  * Fixed header comment (copyright etc)
00081  *
00082  * Revision 1.19  2002/09/16 01:08:59  robertj
00083  * Added #define so can select if #pragma interface/implementation is used on
00084  *   platform basis (eg MacOS) rather than compiler, thanks Robert Monaghan.
00085  *
00086  * Revision 1.18  2002/03/18 05:01:54  robertj
00087  * Added functions to set component parts of URL.
00088  *
00089  * Revision 1.17  2001/11/08 00:32:49  robertj
00090  * Added parsing of ';' based parameter fields into string dictionary if there are multiple parameters, with '=' values.
00091  *
00092  * Revision 1.16  2001/09/28 00:32:24  robertj
00093  * Broke out internal static function for unstranslating URL strings.
00094  *
00095  * Revision 1.15  1999/03/09 08:01:47  robertj
00096  * Changed comments for doc++ support (more to come).
00097  *
00098  * Revision 1.14  1999/02/16 08:07:10  robertj
00099  * MSVC 6.0 compatibility changes.
00100  *
00101  * Revision 1.13  1998/09/23 06:20:11  robertj
00102  * Added open source copyright license.
00103  *
00104  * Revision 1.12  1998/02/16 00:12:53  robertj
00105  * Added function to open a URL in a browser.
00106  *
00107  * Revision 1.11  1998/02/03 10:02:35  robertj
00108  * Added ability to get scheme, host and port from URL as a string.
00109  *
00110  * Revision 1.10  1998/02/03 06:18:49  robertj
00111  * Fixed URL encoding to be closer to RFC
00112  *
00113  * Revision 1.9  1997/01/12 04:22:54  robertj
00114  * Added has function so URL can be dictionary key.
00115  *
00116  * Revision 1.8  1996/08/19 13:37:28  robertj
00117  * Fixed URL parsing and definition (cannot have relative paths).
00118  *
00119  * Revision 1.7  1996/06/10 09:55:44  robertj
00120  * Added global function for query parameters parsing.
00121  *
00122  * Revision 1.6  1996/03/31 08:53:13  robertj
00123  * Added string representation for URI part only.
00124  *
00125  * Revision 1.5  1996/03/16 04:46:02  robertj
00126  * Added translation type to TranslateString() to accommodate query variables.
00127  *
00128  * Revision 1.4  1996/03/02 03:12:13  robertj
00129  * Added function to translate a string to a form suitable for inclusion in a URL.
00130  *
00131  * Revision 1.3  1996/02/03 11:06:27  robertj
00132  * Added splitting of query field into variables dictionary.
00133  *
00134  * Revision 1.2  1996/01/26 02:24:32  robertj
00135  * Further implemetation.
00136  *
00137  * Revision 1.1  1996/01/23 13:04:20  robertj
00138  * Initial revision
00139  *
00140  */
00141 
00142 #ifndef _PURL
00143 #define _PURL
00144 
00145 #ifdef P_USE_PRAGMA
00146 #pragma interface
00147 #endif
00148 
00149 #ifndef _PTLIB_H
00150 #include <ptlib.h>
00151 #endif
00152 
00153 
00155 // PURL
00156 
00157 class PURLLegacyScheme;
00158 
00164 class PURL : public PObject
00165 {
00166   PCLASSINFO(PURL, PObject)
00167   public:
00169     PURL();
00171     PURL(
00172       const char * cstr,    
00173       const char * defaultScheme = NULL 
00174     );
00176     PURL(
00177       const PString & str,  
00178       const char * defaultScheme = NULL 
00179     );
00181     PURL(
00182       const PFilePath & path   
00183     );
00184 
00193     virtual Comparison Compare(
00194       const PObject & obj   
00195     ) const;
00196 
00208     virtual PINDEX HashFunction() const;
00209 
00212     virtual void PrintOn(
00213       ostream &strm   
00214     ) const;
00215 
00219     virtual void ReadFrom(
00220       istream &strm   
00221     );
00223  
00227     inline BOOL Parse(
00228       const char * cstr,   
00229       const char * defaultScheme = NULL 
00230     ) { return InternalParse(cstr, defaultScheme); }
00232     inline BOOL Parse(
00233       const PString & str, 
00234       const char * defaultScheme = NULL 
00235     ) { return InternalParse((const char *)str, defaultScheme); }
00236 
00238     enum UrlFormat {
00240       FullURL,      
00242       PathOnly,     
00244       URIOnly,      
00246       HostPortOnly  
00247     };
00248 
00255     PString AsString(
00256       UrlFormat fmt = FullURL   
00257     ) const;
00258 
00262     PFilePath AsFilePath() const;
00263 
00265     enum TranslationType {
00267       LoginTranslation,
00269       PathTranslation,
00271       QueryTranslation
00272     };
00273 
00281     static PString TranslateString(
00282       const PString & str,    
00283       TranslationType type    
00284     );
00285 
00293     static PString UntranslateString(
00294       const PString & str,    
00295       TranslationType type    
00296     );
00297 
00299     static void SplitQueryVars(
00300       const PString & queryStr,   
00301       PStringToString & queryVars 
00302     );
00303 
00304 
00306     const PCaselessString & GetScheme() const { return scheme; }
00307 
00309     void SetScheme(const PString & scheme);
00310 
00312     const PString & GetUserName() const { return username; }
00313 
00315     void SetUserName(const PString & username);
00316 
00318     const PString & GetPassword() const { return password; }
00319 
00321     void SetPassword(const PString & password);
00322 
00324     const PCaselessString & GetHostName() const { return hostname; }
00325 
00327     void SetHostName(const PString & hostname);
00328 
00330     WORD GetPort() const { return port; }
00331 
00333     void SetPort(WORD newPort);
00334     
00336     BOOL GetPortSupplied() const { return portSupplied; }
00337 
00339     BOOL GetRelativePath() const { return relativePath; }
00340 
00342     const PString & GetPathStr() const { return pathStr; }
00343 
00345     void SetPathStr(const PString & pathStr);
00346 
00348     const PStringArray & GetPath() const { return path; }
00349 
00351     void SetPath(const PStringArray & path);
00352 
00354     PString GetParameters() const;
00355 
00357     void SetParameters(const PString & parameters);
00358 
00360     const PStringToString & GetParamVars() const { return paramVars; }
00361 
00363     void SetParamVars(const PStringToString & paramVars);
00364 
00366     void SetParamVar(const PString & key, const PString & data);
00367 
00369     const PString & GetFragment() const { return fragment; }
00370 
00372     PString GetQuery() const;
00373 
00375     void SetQuery(const PString & query);
00376 
00378     const PStringToString & GetQueryVars() const { return queryVars; }
00379 
00381     void SetQueryVars(const PStringToString & queryVars);
00382 
00384     void SetQueryVar(const PString & key, const PString & data);
00385 
00387     BOOL IsEmpty() const { return urlString.IsEmpty(); }
00388 
00389 
00396     static BOOL OpenBrowser(
00397       const PString & url   
00398     );
00400 
00401     BOOL LegacyParse(const PString & _url, const PURLLegacyScheme * schemeInfo);
00402     PString LegacyAsString(PURL::UrlFormat fmt, const PURLLegacyScheme * schemeInfo) const;
00403 
00404   protected:
00405     virtual BOOL InternalParse(
00406       const char * cstr,         
00407       const char * defaultScheme 
00408     );
00409     void Recalculate();
00410     PString urlString;
00411 
00412     PCaselessString scheme;
00413     PString username;
00414     PString password;
00415     PCaselessString hostname;
00416     WORD port;
00417     BOOL portSupplied;          
00418     BOOL relativePath;
00419     PString pathStr;
00420     PStringArray path;
00421     PStringToString paramVars;
00422     PString fragment;
00423     PStringToString queryVars;
00424 };
00425 
00426 
00428 // PURLScheme
00429 
00430 class PURLScheme : public PObject
00431 {
00432   PCLASSINFO(PURLScheme, PObject);
00433   public:
00434     virtual PString GetName() const = 0;
00435     virtual BOOL Parse(const PString & url, PURL & purl) const = 0;
00436     virtual PString AsString(PURL::UrlFormat fmt, const PURL & purl) const = 0;
00437 };
00438 
00440 // PURLLegacyScheme
00441 
00442 class PURLLegacyScheme : public PURLScheme
00443 {
00444   public:
00445     PURLLegacyScheme(const char * _scheme)
00446       : scheme(_scheme) { }
00447 
00448     BOOL Parse(const PString & url, PURL & purl) const
00449     { return purl.LegacyParse(url, this); }
00450 
00451     PString AsString(PURL::UrlFormat fmt, const PURL & purl) const
00452     { return purl.LegacyAsString(fmt, this); }
00453 
00454     PString GetName() const     
00455     { return scheme; }
00456 
00457     PString scheme;
00458     BOOL hasUsername;
00459     BOOL hasPassword;
00460     BOOL hasHostPort;
00461     BOOL defaultToUserIfNoAt;
00462     BOOL defaultHostToLocal;
00463     BOOL hasQuery;
00464     BOOL hasParameters;
00465     BOOL hasFragments;
00466     BOOL hasPath;
00467     BOOL relativeImpliesScheme;
00468     WORD defaultPort;
00469 };
00470 
00471 #endif
00472 
00473 // End Of File ///////////////////////////////////////////////////////////////

Generated on Fri Mar 7 06:25:03 2008 for PTLib by  doxygen 1.5.1