|
PTLib Version 2.10.2
|
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 * $Revision: 25319 $ 00027 * $Author: rjongbloed $ 00028 * $Date: 2011-03-14 20:33:53 -0500 (Mon, 14 Mar 2011) $ 00029 */ 00030 00031 #ifndef PTLIB_PURL_H 00032 #define PTLIB_PURL_H 00033 00034 #ifdef P_USE_PRAGMA 00035 #pragma interface 00036 #endif 00037 00038 00039 #if P_URL 00040 00041 #include <ptlib/pfactory.h> 00042 00043 00045 // PURL 00046 00047 class PURLLegacyScheme; 00048 00054 class PURL : public PObject 00055 { 00056 PCLASSINFO(PURL, PObject) 00057 public: 00059 PURL(); 00061 PURL( 00062 const char * cstr, 00063 const char * defaultScheme = "http" 00064 ); 00066 PURL( 00067 const PString & str, 00068 const char * defaultScheme = "http" 00069 ); 00071 PURL( 00072 const PFilePath & path 00073 ); 00074 00075 PURL(const PURL & other); 00076 PURL & operator=(const PURL & other); 00077 00086 virtual Comparison Compare( 00087 const PObject & obj 00088 ) const; 00089 00101 virtual PINDEX HashFunction() const; 00102 00105 virtual void PrintOn( 00106 ostream &strm 00107 ) const; 00108 00112 virtual void ReadFrom( 00113 istream &strm 00114 ); 00116 00120 inline PBoolean Parse( 00121 const char * cstr, 00122 const char * defaultScheme = NULL 00123 ) { return InternalParse(cstr, defaultScheme); } 00125 inline PBoolean Parse( 00126 const PString & str, 00127 const char * defaultScheme = NULL 00128 ) { return InternalParse((const char *)str, defaultScheme); } 00129 00131 enum UrlFormat { 00133 FullURL, 00135 PathOnly, 00137 URIOnly, 00139 HostPortOnly 00140 }; 00141 00148 PString AsString( 00149 UrlFormat fmt = FullURL 00150 ) const; 00151 operator PString() const { return AsString(); } 00152 00156 PFilePath AsFilePath() const; 00157 00159 enum TranslationType { 00161 LoginTranslation, 00163 PathTranslation, 00165 QueryTranslation, 00167 ParameterTranslation 00168 }; 00169 00177 static PString TranslateString( 00178 const PString & str, 00179 TranslationType type 00180 ); 00181 00189 static PString UntranslateString( 00190 const PString & str, 00191 TranslationType type 00192 ); 00193 00195 static void SplitVars( 00196 const PString & str, 00197 PStringToString & vars, 00198 char sep1 = ';', 00199 char sep2 = '=', 00200 TranslationType type = ParameterTranslation 00201 ); 00202 00204 static void SplitQueryVars( 00205 const PString & queryStr, 00206 PStringToString & queryVars 00207 ) { SplitVars(queryStr, queryVars, '&', '=', QueryTranslation); } 00208 00211 static void OutputVars( 00212 ostream & strm, 00213 const PStringToString & vars, 00214 char sep0 = ';', 00215 char sep1 = ';', 00216 char sep2 = '=', 00217 TranslationType type = ParameterTranslation 00218 ); 00219 00220 00222 const PCaselessString & GetScheme() const { return scheme; } 00223 00225 void SetScheme(const PString & scheme); 00226 00228 const PString & GetUserName() const { return username; } 00229 00231 void SetUserName(const PString & username); 00232 00234 const PString & GetPassword() const { return password; } 00235 00237 void SetPassword(const PString & password); 00238 00240 const PCaselessString & GetHostName() const { return hostname; } 00241 00243 void SetHostName(const PString & hostname); 00244 00246 WORD GetPort() const { return port; } 00247 00249 void SetPort(WORD newPort); 00250 00252 PBoolean GetPortSupplied() const { return portSupplied; } 00253 00255 PBoolean GetRelativePath() const { return relativePath; } 00256 00258 PString GetPathStr() const; 00259 00261 void SetPathStr(const PString & pathStr); 00262 00264 const PStringArray & GetPath() const { return path; } 00265 00267 void SetPath(const PStringArray & path); 00268 00270 void AppendPath(const PString & segment); 00271 00273 PString GetParameters() const; 00274 00276 void SetParameters(const PString & parameters); 00277 00280 const PStringToString & GetParamVars() const { return paramVars; } 00281 00284 void SetParamVars(const PStringToString & paramVars); 00285 00288 void SetParamVar( 00289 const PString & key, 00290 const PString & data, 00291 bool emptyDataDeletes = true 00292 ); 00293 00295 const PString & GetFragment() const { return fragment; } 00296 00298 PString GetQuery() const; 00299 00302 void SetQuery(const PString & query); 00303 00306 const PStringToString & GetQueryVars() const { return queryVars; } 00307 00310 void SetQueryVars(const PStringToString & queryVars); 00311 00314 void SetQueryVar(const PString & key, const PString & data); 00315 00317 const PString & GetContents() const { return m_contents; } 00318 00320 void SetContents(const PString & str); 00321 00323 PBoolean IsEmpty() const { return urlString.IsEmpty(); } 00324 00325 00330 bool LoadResource( 00331 PString & data, 00332 const PString & requiredContentType = PString::Empty() 00333 ); 00334 bool LoadResource( 00335 PBYTEArray & data, 00336 const PString & requiredContentType = PString::Empty() 00337 ); 00338 00345 bool OpenBrowser() const { return OpenBrowser(AsString()); } 00346 static bool OpenBrowser( 00347 const PString & url 00348 ); 00350 00351 PBoolean LegacyParse(const PString & url, const PURLLegacyScheme * schemeInfo); 00352 PString LegacyAsString(PURL::UrlFormat fmt, const PURLLegacyScheme * schemeInfo) const; 00353 00354 protected: 00355 void CopyContents(const PURL & other); 00356 virtual PBoolean InternalParse( 00357 const char * cstr, 00358 const char * defaultScheme 00359 ); 00360 void Recalculate(); 00361 PString urlString; 00362 00363 PCaselessString scheme; 00364 PString username; 00365 PString password; 00366 PCaselessString hostname; 00367 WORD port; 00368 PBoolean portSupplied; 00369 PBoolean relativePath; 00370 PStringArray path; 00371 PStringToString paramVars; 00372 PString fragment; 00373 PStringToString queryVars; 00374 PString m_contents; // Anything left after parsing other elements 00375 }; 00376 00377 00379 // PURLScheme 00380 00381 class PURLScheme : public PObject 00382 { 00383 PCLASSINFO(PURLScheme, PObject); 00384 public: 00385 virtual PString GetName() const = 0; 00386 virtual PBoolean Parse(const PString & url, PURL & purl) const = 0; 00387 virtual PString AsString(PURL::UrlFormat fmt, const PURL & purl) const = 0; 00388 }; 00389 00390 typedef PFactory<PURLScheme> PURLSchemeFactory; 00391 00392 00394 // PURLLegacyScheme 00395 00396 class PURLLegacyScheme : public PURLScheme 00397 { 00398 public: 00399 PURLLegacyScheme( 00400 const char * s, 00401 bool user = false, 00402 bool pass = false, 00403 bool host = false, 00404 bool def = false, 00405 bool defhost = false, 00406 bool query = false, 00407 bool params = false, 00408 bool frags = false, 00409 bool path = false, 00410 bool rel = false, 00411 WORD port = 0 00412 ) 00413 : scheme(s) 00414 , hasUsername (user) 00415 , hasPassword (pass) 00416 , hasHostPort (host) 00417 , defaultToUserIfNoAt (def) 00418 , defaultHostToLocal (defhost) 00419 , hasQuery (query) 00420 , hasParameters (params) 00421 , hasFragments (frags) 00422 , hasPath (path) 00423 , relativeImpliesScheme (rel) 00424 , defaultPort (port) 00425 { } 00426 00427 PBoolean Parse(const PString & url, PURL & purl) const 00428 { return purl.LegacyParse(url, this); } 00429 00430 PString AsString(PURL::UrlFormat fmt, const PURL & purl) const 00431 { return purl.LegacyAsString(fmt, this); } 00432 00433 PString GetName() const 00434 { return scheme; } 00435 00436 PString scheme; 00437 bool hasUsername; 00438 bool hasPassword; 00439 bool hasHostPort; 00440 bool defaultToUserIfNoAt; 00441 bool defaultHostToLocal; 00442 bool hasQuery; 00443 bool hasParameters; 00444 bool hasFragments; 00445 bool hasPath; 00446 bool relativeImpliesScheme; 00447 WORD defaultPort; 00448 }; 00449 00450 #define PURL_LEGACY_SCHEME(schemeName, user, pass, host, def, defhost, query, params, frags, path, rel, port) \ 00451 class PURLLegacyScheme_##schemeName : public PURLLegacyScheme \ 00452 { \ 00453 public: \ 00454 PURLLegacyScheme_##schemeName() \ 00455 : PURLLegacyScheme(#schemeName, user, pass, host, def, defhost, query, params, frags, path, rel, port) \ 00456 { } \ 00457 }; \ 00458 static PURLSchemeFactory::Worker<PURLLegacyScheme_##schemeName> schemeName##Factory(#schemeName, true); \ 00459 00460 00461 00463 // PURLLoader 00464 00465 class PURLLoader : public PObject 00466 { 00467 PCLASSINFO(PURLLoader, PObject); 00468 public: 00469 virtual bool Load(const PURL & url, PString & str, const PString & requiredContentType) = 0; 00470 virtual bool Load(const PURL & url, PBYTEArray & data, const PString & requiredContentType) = 0; 00471 }; 00472 00473 typedef PFactory<PURLLoader> PURLLoaderFactory; 00474 00475 00476 #endif // P_URL 00477 00478 #endif // PTLIB_PURL_H 00479 00480 00481 // End Of File ///////////////////////////////////////////////////////////////