httpsvc.h

Go to the documentation of this file.
00001 /*
00002  * httpsvc.h
00003  *
00004  * Common classes for service applications using HTTP as the user interface.
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: 21788 $
00027  * $Author: rjongbloed $
00028  * $Date: 2008-12-11 23:42:13 -0600 (Thu, 11 Dec 2008) $
00029  */
00030 
00031 #ifndef PTLIB_HTTPSVC_H
00032 #define PTLIB_HTTPSVC_H
00033 
00034 #include <ptlib/svcproc.h>
00035 #include <ptlib/sockets.h>
00036 #include <ptclib/httpform.h>
00037 #include <ptclib/cypher.h>
00038 
00039 
00040 class PHTTPServiceProcess;
00041 
00042 
00044 
00045 class PHTTPServiceThread : public PThread
00046 {
00047   PCLASSINFO(PHTTPServiceThread, PThread)
00048   public:
00049     PHTTPServiceThread(PINDEX stackSize,
00050                        PHTTPServiceProcess & app);
00051     ~PHTTPServiceThread();
00052 
00053     void Main();
00054     void Close();
00055 
00056   protected:
00057     PINDEX                myStackSize;
00058     PHTTPServiceProcess & process;
00059     PTCPSocket          * socket;
00060 };
00061 
00062 
00064 
00065 class PHTTPServiceProcess : public PServiceProcess
00066 {
00067   PCLASSINFO(PHTTPServiceProcess, PServiceProcess)
00068 
00069   public:
00070     enum {
00071       MaxSecuredKeys = 10
00072     };
00073     struct Info {
00074       const char * productName;
00075       const char * manufacturerName;
00076 
00077       WORD majorVersion;
00078       WORD minorVersion;
00079       CodeStatus buildStatus;    
00080       WORD buildNumber;
00081       const char * compilationDate;
00082 
00083       PTEACypher::Key productKey;  
00084       const char * securedKeys[MaxSecuredKeys]; 
00085       PINDEX securedKeyCount;
00086 
00087       PTEACypher::Key signatureKey;   
00088 
00089       const char * manufHomePage; 
00090       const char * email;         
00091       const char * productHTML;   
00092 
00093       const char * gifHTML;       
00094 
00095       const char * gifFilename;   
00096       int gifWidth;               
00097       int gifHeight;              
00098 
00099       const char * copyrightHolder;   
00100       const char * copyrightHomePage; 
00101       const char * copyrightEmail;    
00102     };
00103 
00104     PHTTPServiceProcess(const Info & inf);
00105     ~PHTTPServiceProcess();
00106 
00107     PBoolean OnStart();
00108     void OnStop();
00109     PBoolean OnPause();
00110     void OnContinue();
00111     const char * GetServiceDependencies() const;
00112 
00113     virtual void OnConfigChanged() = 0;
00114     virtual PBoolean Initialise(const char * initMsg) = 0;
00115 
00116     PBoolean ListenForHTTP(
00117       WORD port,
00118       PSocket::Reusability reuse = PSocket::CanReuseAddress,
00119       PINDEX stackSize = 0x4000
00120     );
00121     PBoolean ListenForHTTP(
00122       PSocket * listener,
00123       PSocket::Reusability reuse = PSocket::CanReuseAddress,
00124       PINDEX stackSize = 0x4000
00125     );
00126 
00127     virtual PString GetPageGraphic();
00128     void GetPageHeader(PHTML &);
00129     void GetPageHeader(PHTML &, const PString & title);
00130 
00131     virtual PString GetCopyrightText();
00132 
00133     const PString & GetMacroKeyword() const { return macroKeyword; }
00134     const PTime & GetCompilationDate() const { return compilationDate; }
00135     const PString & GetHomePage() const { return manufacturersHomePage; }
00136     const PString & GetEMailAddress() const { return manufacturersEmail; }
00137     const PString & GetProductName() const { return productNameHTML; }
00138     const PTEACypher::Key & GetProductKey() const { return productKey; }
00139     const PStringArray & GetSecuredKeys() const { return securedKeys; }
00140     const PTEACypher::Key & GetSignatureKey() const { return signatureKey; }
00141     PBoolean ShouldIgnoreSignatures() const { return ignoreSignatures; }
00142     void SetIgnoreSignatures(PBoolean ig) { ignoreSignatures = ig; }
00143 
00144     static PHTTPServiceProcess & Current();
00145 
00146     virtual void AddRegisteredText(PHTML & html);
00147     virtual void AddUnregisteredText(PHTML & html);
00148     virtual PBoolean SubstituteEquivalSequence(PHTTPRequest & request, const PString &, PString &);
00149     virtual PHTTPServer * CreateHTTPServer(PTCPSocket & socket);
00150     virtual PHTTPServer * OnCreateHTTPServer(const PHTTPSpace & urlSpace);
00151     PTCPSocket * AcceptHTTP();
00152     PBoolean ProcessHTTP(PTCPSocket & socket);
00153 
00154   protected:
00155     PSocket  * httpListeningSocket;
00156     PHTTPSpace httpNameSpace;
00157     PString    macroKeyword;
00158 
00159     PTEACypher::Key productKey;
00160     PStringArray    securedKeys;
00161     PTEACypher::Key signatureKey;
00162     PBoolean            ignoreSignatures;
00163 
00164     PTime      compilationDate;
00165     PString    manufacturersHomePage;
00166     PString    manufacturersEmail;
00167     PString    productNameHTML;
00168     PString    gifHTML;
00169     PString    copyrightHolder;
00170     PString    copyrightHomePage;
00171     PString    copyrightEmail;
00172 
00173     void ShutdownListener();
00174     void BeginRestartSystem();
00175     void CompleteRestartSystem();
00176 
00177     PThread *  restartThread;
00178 
00179     PLIST(ThreadList, PHTTPServiceThread);
00180     ThreadList httpThreads;
00181     PMutex     httpThreadsMutex;
00182 
00183   friend class PConfigPage;
00184   friend class PConfigSectionsPage;
00185   friend class PHTTPServiceThread;
00186 };
00187 
00188 
00190 
00191 class PConfigPage : public PHTTPConfig
00192 {
00193   PCLASSINFO(PConfigPage, PHTTPConfig)
00194   public:
00195     PConfigPage(
00196       PHTTPServiceProcess & app,
00197       const PString & section,
00198       const PHTTPAuthority & auth
00199     );
00200     PConfigPage(
00201       PHTTPServiceProcess & app,
00202       const PString & title,
00203       const PString & section,
00204       const PHTTPAuthority & auth
00205     );
00206 
00207     void OnLoadedText(PHTTPRequest &, PString & text);
00208 
00209     PBoolean OnPOST(
00210       PHTTPServer & server,
00211       const PURL & url,
00212       const PMIMEInfo & info,
00213       const PStringToString & data,
00214       const PHTTPConnectionInfo & connectInfo
00215     );
00216 
00217     virtual PBoolean Post(
00218       PHTTPRequest & request,       
00219       const PStringToString & data, 
00220       PHTML & replyMessage          
00221     );
00222 
00223   protected:
00224     virtual PBoolean GetExpirationDate(
00225       PTime & when          
00226     );
00227 
00228     PHTTPServiceProcess & process;
00229 };
00230 
00231 
00233 
00234 class PConfigSectionsPage : public PHTTPConfigSectionList
00235 {
00236   PCLASSINFO(PConfigSectionsPage, PHTTPConfigSectionList)
00237   public:
00238     PConfigSectionsPage(
00239       PHTTPServiceProcess & app,
00240       const PURL & url,
00241       const PHTTPAuthority & auth,
00242       const PString & prefix,
00243       const PString & valueName,
00244       const PURL & editSection,
00245       const PURL & newSection,
00246       const PString & newTitle,
00247       PHTML & heading
00248     );
00249 
00250     void OnLoadedText(PHTTPRequest &, PString & text);
00251 
00252     PBoolean OnPOST(
00253       PHTTPServer & server,
00254       const PURL & url,
00255       const PMIMEInfo & info,
00256       const PStringToString & data,
00257       const PHTTPConnectionInfo & connectInfo
00258     );
00259 
00260     virtual PBoolean Post(
00261       PHTTPRequest & request,       
00262       const PStringToString & data, 
00263       PHTML & replyMessage          
00264     );
00265 
00266   protected:
00267     virtual PBoolean GetExpirationDate(
00268       PTime & when          
00269     );
00270 
00271     PHTTPServiceProcess & process;
00272 };
00273 
00274 
00276 
00277 class PRegisterPage : public PConfigPage
00278 {
00279   PCLASSINFO(PRegisterPage, PConfigPage)
00280   public:
00281     PRegisterPage(
00282       PHTTPServiceProcess & app,
00283       const PHTTPAuthority & auth
00284     );
00285 
00286     PString LoadText(
00287       PHTTPRequest & request        
00288     );
00289     void OnLoadedText(PHTTPRequest & request, PString & text);
00290 
00291     virtual PBoolean Post(
00292       PHTTPRequest & request,       
00293       const PStringToString & data, 
00294       PHTML & replyMessage          
00295     );
00296 
00297     virtual void AddFields(
00298       const PString & prefix        
00299     ) = 0;
00300 
00301   protected:
00302     PHTTPServiceProcess & process;
00303 };
00304 
00305 
00307 
00308 class PServiceHTML : public PHTML
00309 {
00310   PCLASSINFO(PServiceHTML, PHTML)
00311   public:
00312     PServiceHTML(const char * title,
00313                  const char * help = NULL,
00314                  const char * helpGif = "help.gif");
00315 
00316     PString ExtractSignature(PString & out);
00317     static PString ExtractSignature(const PString & html,
00318                                     PString & out,
00319                                     const char * keyword = "#equival");
00320 
00321     PString CalculateSignature();
00322     static PString CalculateSignature(const PString & out);
00323     static PString CalculateSignature(const PString & out, const PTEACypher::Key & sig);
00324 
00325     PBoolean CheckSignature();
00326     static PBoolean CheckSignature(const PString & html);
00327 
00328     enum MacroOptions {
00329       NoOptions           = 0,
00330       NeedSignature       = 1,
00331       LoadFromFile        = 2,
00332       NoURLOverride       = 4,
00333       NoSignatureForFile  = 8
00334     };
00335     static PBoolean ProcessMacros(PHTTPRequest & request,
00336                               PString & text,
00337                               const PString & filename,
00338                               unsigned options);
00339 };
00340 
00341 
00343 
00344 class PServiceMacro : public PObject
00345 {
00346   public:
00347     PServiceMacro(const char * name, PBoolean isBlock);
00348     PServiceMacro(const PCaselessString & name, PBoolean isBlock);
00349     Comparison Compare(const PObject & obj) const;
00350     virtual PString Translate(
00351       PHTTPRequest & request,
00352       const PString & args,
00353       const PString & block
00354     ) const;
00355   protected:
00356     const char * macroName;
00357     PBoolean isMacroBlock;
00358     PServiceMacro * link;
00359     static PServiceMacro * list;
00360   friend class PServiceMacros_list;
00361 };
00362 
00363 
00364 #define P_EMPTY
00365 
00366 #define PCREATE_SERVICE_MACRO(name, request, args) \
00367   class PServiceMacro_##name : public PServiceMacro { \
00368     public: \
00369       PServiceMacro_##name() : PServiceMacro(#name, PFalse) { } \
00370       PString Translate(PHTTPRequest &, const PString &, const PString &) const; \
00371   }; \
00372   static const PServiceMacro_##name serviceMacro_##name; \
00373   PString PServiceMacro_##name::Translate(PHTTPRequest & request, const PString & args, const PString &) const
00374 
00375 
00376 
00377 #define PCREATE_SERVICE_MACRO_BLOCK(name, request, args, block) \
00378   class PServiceMacro_##name : public PServiceMacro { \
00379     public: \
00380       PServiceMacro_##name() : PServiceMacro(#name, PTrue) { } \
00381       PString Translate(PHTTPRequest &, const PString &, const PString &) const; \
00382   }; \
00383   static const PServiceMacro_##name serviceMacro_##name; \
00384   PString PServiceMacro_##name::Translate(PHTTPRequest & request, const PString & args, const PString & block) const
00385 
00386 
00387 
00389 
00390 class PServiceHTTPString : public PHTTPString
00391 {
00392   PCLASSINFO(PServiceHTTPString, PHTTPString)
00393   public:
00394     PServiceHTTPString(const PURL & url, const PString & string)
00395       : PHTTPString(url, string) { }
00396 
00397     PServiceHTTPString(const PURL & url, const PHTTPAuthority & auth)
00398       : PHTTPString(url, auth) { }
00399 
00400     PServiceHTTPString(const PURL & url, const PString & string, const PHTTPAuthority & auth)
00401       : PHTTPString(url, string, auth) { }
00402 
00403     PServiceHTTPString(const PURL & url, const PString & string, const PString & contentType)
00404       : PHTTPString(url, string, contentType) { }
00405 
00406     PServiceHTTPString(const PURL & url, const PString & string, const PString & contentType, const PHTTPAuthority & auth)
00407       : PHTTPString(url, string, contentType, auth) { }
00408 
00409     PString LoadText(PHTTPRequest &);
00410 
00411   protected:
00412     virtual PBoolean GetExpirationDate(
00413       PTime & when          
00414     );
00415 };
00416 
00417 
00418 class PServiceHTTPFile : public PHTTPFile
00419 {
00420   PCLASSINFO(PServiceHTTPFile, PHTTPFile)
00421   public:
00422     PServiceHTTPFile(const PString & filename, PBoolean needSig = PFalse)
00423       : PHTTPFile(filename) { needSignature = needSig; }
00424     PServiceHTTPFile(const PString & filename, const PFilePath & file, PBoolean needSig = PFalse)
00425       : PHTTPFile(filename, file) { needSignature = needSig; }
00426     PServiceHTTPFile(const PString & filename, const PString & file, PBoolean needSig = PFalse)
00427       : PHTTPFile(filename, file) { needSignature = needSig; }
00428     PServiceHTTPFile(const PString & filename, const PHTTPAuthority & auth, PBoolean needSig = PFalse)
00429       : PHTTPFile(filename, auth) { needSignature = needSig; }
00430     PServiceHTTPFile(const PString & filename, const PFilePath & file, const PHTTPAuthority & auth, PBoolean needSig = PFalse)
00431       : PHTTPFile(filename, file, auth) { needSignature = needSig; }
00432 
00433     void OnLoadedText(PHTTPRequest &, PString & text);
00434 
00435   protected:
00436     virtual PBoolean GetExpirationDate(
00437       PTime & when          
00438     );
00439 
00440     PBoolean needSignature;
00441 };
00442 
00443 class PServiceHTTPDirectory : public PHTTPDirectory
00444 {
00445   PCLASSINFO(PServiceHTTPDirectory, PHTTPDirectory)
00446   public:
00447     PServiceHTTPDirectory(const PURL & url, const PDirectory & dirname, PBoolean needSig = PFalse)
00448       : PHTTPDirectory(url, dirname) { needSignature = needSig; }
00449 
00450     PServiceHTTPDirectory(const PURL & url, const PDirectory & dirname, const PHTTPAuthority & auth, PBoolean needSig = PFalse)
00451       : PHTTPDirectory(url, dirname, auth) { needSignature = needSig; }
00452 
00453     void OnLoadedText(PHTTPRequest &, PString & text);
00454 
00455   protected:
00456     virtual PBoolean GetExpirationDate(
00457       PTime & when          
00458     );
00459 
00460     PBoolean needSignature;
00461 };
00462 
00463 
00464 #endif // PTLIB_HTTPSVC_H
00465 
00466 
00467 // End Of File ///////////////////////////////////////////////////////////////

Generated on Thu May 27 01:36:47 2010 for PTLib by  doxygen 1.4.7