Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Class Members | File Members

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  * $Log: httpsvc.h,v $
00027  * Revision 1.44  2003/07/15 11:17:45  csoutheren
00028  * Added additional constructor for PServiceHTTPString,
00029  *   Thanks to Federico Pinna
00030  *
00031  * Revision 1.43  2003/02/19 07:23:17  robertj
00032  * Changes to allow for single threaded HTTP service processes.
00033  *
00034  * Revision 1.42  2002/11/06 22:47:24  robertj
00035  * Fixed header comment (copyright etc)
00036  *
00037  * Revision 1.41  2002/07/30 03:47:13  craigs
00038  * Added PHTTPServiceString with content type
00039  *
00040  * Revision 1.40  2002/07/17 08:02:53  robertj
00041  * Allowed for adjustable copyright holder
00042  *
00043  * Revision 1.39  2002/02/05 22:53:14  robertj
00044  * Changed default for HTTP listener to reuse addresses.
00045  *
00046  * Revision 1.38  2001/10/10 08:06:34  robertj
00047  * Fixed problem with not shutting down threads when closing listener.
00048  *
00049  * Revision 1.37  2001/08/28 06:44:30  craigs
00050  * Added ability to override PHTTPServer creation
00051  *
00052  * Revision 1.36  2001/06/23 00:32:15  robertj
00053  * Added parameter to be able to set REUSEADDR on listener socket.
00054  *
00055  * Revision 1.35  2001/03/04 02:24:44  robertj
00056  * Removed default OnControl() from http service as cannot get port number.
00057  *
00058  * Revision 1.34  2001/02/20 02:32:41  robertj
00059  * Added PServiceMacro version that can do substitutions on blocks of HTML.
00060  *
00061  * Revision 1.33  2001/02/15 01:12:15  robertj
00062  * Moved some often repeated HTTP service code into PHTTPServiceProcess.
00063  *
00064  * Revision 1.32  2001/02/14 02:30:59  robertj
00065  * Moved HTTP Service Macro facility to public API so can be used by apps.
00066  * Added ability to specify the service macro keyword, defaults to "macro".
00067  * Added machine macro to get the OS version and hardware.
00068  *
00069  * Revision 1.31  2000/12/14 08:09:41  robertj
00070  * Fixed missing immediate expiry date on string and file service HTTP resourcer.
00071  *
00072  * Revision 1.30  2000/12/11 13:15:17  robertj
00073  * Added macro to include signed or unsigned chunks of HTML.
00074  * Added flag to globally ignore HTML signatures (useful for develeopment).
00075  *
00076  * Revision 1.29  2000/11/02 21:57:00  craigs
00077  * Added extra constructor
00078  *
00079  * Revision 1.28  2000/08/04 12:48:13  robertj
00080  * Added mechanism by which a service can get at new HTTP connections, eg to add SSL.
00081  *
00082  * Revision 1.27  1999/02/16 08:07:10  robertj
00083  * MSVC 6.0 compatibility changes.
00084  *
00085  * Revision 1.26  1998/11/30 02:50:50  robertj
00086  * New directory structure
00087  *
00088  * Revision 1.25  1998/10/29 11:58:51  robertj
00089  * Added ability to configure the HTTP threads stack size.
00090  *
00091  * Revision 1.24  1998/09/23 06:19:34  robertj
00092  * Added open source copyright license.
00093  *
00094  * Revision 1.23  1998/04/01 01:56:47  robertj
00095  * Added PServiceHTTPFile constructor so file path and URL can be different.
00096  *
00097  * Revision 1.22  1998/03/20 03:16:09  robertj
00098  * Added special classes for specific sepahores, PMutex and PSyncPoint.
00099  *
00100  * Revision 1.21  1998/03/17 10:16:00  robertj
00101  * Allowed registration page to have HTML override.
00102  *
00103  * Revision 1.20  1998/02/16 00:15:13  robertj
00104  * Major rewrite of application info passed in PHTTPServiceProcess constructor.
00105  *
00106  * Revision 1.19  1998/01/26 00:28:32  robertj
00107  * Removed POrderPage completely from httpsvc.
00108  * Added PHTTPAuthority to PHTTPServiceString constructor.
00109  * Added option flags to ProcessMacros to automatically load from file etc.
00110  *
00111  * Revision 1.18  1997/11/10 12:40:05  robertj
00112  * Changed SustituteEquivalSequence so can override standard macros.
00113  *
00114  * Revision 1.17  1997/11/04 06:02:56  robertj
00115  * Allowed help gif file name to overridable in PServiceHTML, so can be in subdirectory.
00116  *
00117  * Revision 1.16  1997/10/30 10:22:35  robertj
00118  * Added ability to customise regisration text by application.
00119  *
00120  * Revision 1.15  1997/08/28 13:56:34  robertj
00121  * Fixed bug where HTTP directory was not processed for macros.
00122  *
00123  * Revision 1.13  1997/08/20 08:48:18  craigs
00124  * Added PHTTPServiceDirectory & PHTTPServiceString
00125  *
00126  * Revision 1.12  1997/07/26 11:38:18  robertj
00127  * Support for overridable pages in HTTP service applications.
00128  *
00129  * Revision 1.11  1997/06/16 14:12:55  robertj
00130  * Changed private to protected.
00131  *
00132  * Revision 1.10  1997/06/16 13:20:14  robertj
00133  * Fixed bug where PHTTPThread crashes on exit.
00134  *
00135  * Revision 1.9  1997/03/02 03:42:19  robertj
00136  * Added error logging to standard HTTP Service HTTP Server.
00137  * Removed extraneous variables that set GIF file size to zero.
00138  *
00139  * Revision 1.8  1997/02/05 11:54:52  robertj
00140  * Added support for order form page overridiing.
00141  *
00142  * Revision 1.7  1997/01/27 10:22:33  robertj
00143  * Numerous changes to support OEM versions of products.
00144  *
00145  * Revision 1.6  1996/11/04 03:55:20  robertj
00146  * Changed to accept separate copyright and manufacturer strings.
00147  *
00148  * Revision 1.5  1996/09/14 13:09:12  robertj
00149  * Major upgrade:
00150  *   rearranged sockets to help support IPX.
00151  *   added indirect channel class and moved all protocols to descend from it,
00152  *   separating the protocol from the low level byte transport.
00153  *
00154  * Revision 1.4  1996/08/19 13:43:46  robertj
00155  * Fixed race condition in system restart logic.
00156  *
00157  * Revision 1.3  1996/08/08 13:36:38  robertj
00158  * Fixed Registation page so no longer has static link, ie can be DLLed.
00159  *
00160  * Revision 1.2  1996/06/28 13:15:39  robertj
00161  * Moved HTTP form resource to another compilation module.
00162  *
00163  * Revision 1.1  1996/06/13 13:33:14  robertj
00164  * Initial revision
00165  *
00166  */
00167 
00168 #ifndef APPCOMM_H
00169 #define APPCOMM_H
00170 
00171 #include <ptlib/svcproc.h>
00172 #include <ptlib/sockets.h>
00173 #include <ptclib/httpform.h>
00174 #include <ptclib/cypher.h>
00175 
00176 
00177 class PHTTPServiceProcess;
00178 
00179 
00181 
00182 class PHTTPServiceThread : public PThread
00183 {
00184   PCLASSINFO(PHTTPServiceThread, PThread)
00185   public:
00186     PHTTPServiceThread(PINDEX stackSize,
00187                        PHTTPServiceProcess & app);
00188     ~PHTTPServiceThread();
00189 
00190     void Main();
00191     void Close();
00192 
00193   protected:
00194     PINDEX                myStackSize;
00195     PHTTPServiceProcess & process;
00196     PTCPSocket          * socket;
00197 };
00198 
00199 
00201 
00202 class PHTTPServiceProcess : public PServiceProcess
00203 {
00204   PCLASSINFO(PHTTPServiceProcess, PServiceProcess)
00205 
00206   public:
00207     enum {
00208       MaxSecuredKeys = 10
00209     };
00210     struct Info {
00211       const char * productName;
00212       const char * manufacturerName;
00213 
00214       WORD majorVersion;
00215       WORD minorVersion;
00216       CodeStatus buildStatus;    // AlphaCode, BetaCode or ReleaseCode
00217       WORD buildNumber;
00218       const char * compilationDate;
00219 
00220       PTEACypher::Key productKey;  // Poduct key for registration
00221       const char * securedKeys[MaxSecuredKeys]; // Product secured keys for registration
00222       PINDEX securedKeyCount;
00223 
00224       PTEACypher::Key signatureKey;   // Signature key for encryption of HTML files
00225 
00226       const char * manufHomePage; // WWW address of manufacturers home page
00227       const char * email;         // contact email for manufacturer
00228       const char * productHTML;   // HTML for the product name, if NULL defaults to
00229                                   //   the productName variable.
00230       const char * gifHTML;       // HTML to show GIF image in page headers, if NULL
00231                                   //   then the following are used to build one
00232       const char * gifFilename;   // File name for the products GIF file
00233       int gifWidth;               // Size of GIF image, if zero then none is used
00234       int gifHeight;              //   in the generated HTML.
00235 
00236       const char * copyrightHolder;   // Name of copyright holder
00237       const char * copyrightHomePage; // Home page for copyright holder
00238       const char * copyrightEmail;    // E-Mail address for copyright holder
00239     };
00240 
00241     PHTTPServiceProcess(const Info & inf);
00242     ~PHTTPServiceProcess();
00243 
00244     BOOL OnStart();
00245     void OnStop();
00246     BOOL OnPause();
00247     void OnContinue();
00248     const char * GetServiceDependencies() const;
00249 
00250     virtual void OnConfigChanged() = 0;
00251     virtual BOOL Initialise(const char * initMsg) = 0;
00252 
00253     BOOL ListenForHTTP(
00254       WORD port,
00255       PSocket::Reusability reuse = PSocket::CanReuseAddress,
00256       PINDEX stackSize = 0x4000
00257     );
00258     BOOL ListenForHTTP(
00259       PSocket * listener,
00260       PSocket::Reusability reuse = PSocket::CanReuseAddress,
00261       PINDEX stackSize = 0x4000
00262     );
00263 
00264     virtual PString GetPageGraphic();
00265     void GetPageHeader(PHTML &);
00266     void GetPageHeader(PHTML &, const PString & title);
00267 
00268     virtual PString GetCopyrightText();
00269 
00270     const PString & GetMacroKeyword() const { return macroKeyword; }
00271     const PTime & GetCompilationDate() const { return compilationDate; }
00272     const PString & GetHomePage() const { return manufacturersHomePage; }
00273     const PString & GetEMailAddress() const { return manufacturersEmail; }
00274     const PString & GetProductName() const { return productNameHTML; }
00275     const PTEACypher::Key & GetProductKey() const { return productKey; }
00276     const PStringArray & GetSecuredKeys() const { return securedKeys; }
00277     const PTEACypher::Key & GetSignatureKey() const { return signatureKey; }
00278     BOOL ShouldIgnoreSignatures() const { return ignoreSignatures; }
00279     void SetIgnoreSignatures(BOOL ig) { ignoreSignatures = ig; }
00280 
00281     static PHTTPServiceProcess & Current();
00282 
00283     virtual void AddRegisteredText(PHTML & html);
00284     virtual void AddUnregisteredText(PHTML & html);
00285     virtual BOOL SubstituteEquivalSequence(PHTTPRequest & request, const PString &, PString &);
00286     virtual PHTTPServer * CreateHTTPServer(PTCPSocket & socket);
00287     virtual PHTTPServer * OnCreateHTTPServer(const PHTTPSpace & urlSpace);
00288     PTCPSocket * AcceptHTTP();
00289     BOOL ProcessHTTP(PTCPSocket & socket);
00290 
00291   protected:
00292     PSocket  * httpListeningSocket;
00293     PHTTPSpace httpNameSpace;
00294     PString    macroKeyword;
00295 
00296     PTEACypher::Key productKey;
00297     PStringArray    securedKeys;
00298     PTEACypher::Key signatureKey;
00299     BOOL            ignoreSignatures;
00300 
00301     PTime      compilationDate;
00302     PString    manufacturersHomePage;
00303     PString    manufacturersEmail;
00304     PString    productNameHTML;
00305     PString    gifHTML;
00306     PString    copyrightHolder;
00307     PString    copyrightHomePage;
00308     PString    copyrightEmail;
00309 
00310     void ShutdownListener();
00311     void BeginRestartSystem();
00312     void CompleteRestartSystem();
00313 
00314     PThread *  restartThread;
00315 
00316     PLIST(ThreadList, PHTTPServiceThread);
00317     ThreadList httpThreads;
00318     PMutex     httpThreadsMutex;
00319 
00320   friend class PConfigPage;
00321   friend class PConfigSectionsPage;
00322   friend class PHTTPServiceThread;
00323 };
00324 
00325 
00327 
00328 class PConfigPage : public PHTTPConfig
00329 {
00330   PCLASSINFO(PConfigPage, PHTTPConfig)
00331   public:
00332     PConfigPage(
00333       PHTTPServiceProcess & app,
00334       const PString & section,
00335       const PHTTPAuthority & auth
00336     );
00337     PConfigPage(
00338       PHTTPServiceProcess & app,
00339       const PString & title,
00340       const PString & section,
00341       const PHTTPAuthority & auth
00342     );
00343 
00344     void OnLoadedText(PHTTPRequest &, PString & text);
00345 
00346     BOOL OnPOST(
00347       PHTTPServer & server,
00348       const PURL & url,
00349       const PMIMEInfo & info,
00350       const PStringToString & data,
00351       const PHTTPConnectionInfo & connectInfo
00352     );
00353 
00354     virtual BOOL Post(
00355       PHTTPRequest & request,       // Information on this request.
00356       const PStringToString & data, // Variables in the POST data.
00357       PHTML & replyMessage          // Reply message for post.
00358     );
00359 
00360   protected:
00361     virtual BOOL GetExpirationDate(
00362       PTime & when          // Time that the resource expires
00363     );
00364 
00365     PHTTPServiceProcess & process;
00366 };
00367 
00368 
00370 
00371 class PConfigSectionsPage : public PHTTPConfigSectionList
00372 {
00373   PCLASSINFO(PConfigSectionsPage, PHTTPConfigSectionList)
00374   public:
00375     PConfigSectionsPage(
00376       PHTTPServiceProcess & app,
00377       const PURL & url,
00378       const PHTTPAuthority & auth,
00379       const PString & prefix,
00380       const PString & valueName,
00381       const PURL & editSection,
00382       const PURL & newSection,
00383       const PString & newTitle,
00384       PHTML & heading
00385     );
00386 
00387     void OnLoadedText(PHTTPRequest &, PString & text);
00388 
00389     BOOL OnPOST(
00390       PHTTPServer & server,
00391       const PURL & url,
00392       const PMIMEInfo & info,
00393       const PStringToString & data,
00394       const PHTTPConnectionInfo & connectInfo
00395     );
00396 
00397     virtual BOOL Post(
00398       PHTTPRequest & request,       // Information on this request.
00399       const PStringToString & data, // Variables in the POST data.
00400       PHTML & replyMessage          // Reply message for post.
00401     );
00402 
00403   protected:
00404     virtual BOOL GetExpirationDate(
00405       PTime & when          // Time that the resource expires
00406     );
00407 
00408     PHTTPServiceProcess & process;
00409 };
00410 
00411 
00413 
00414 class PRegisterPage : public PConfigPage
00415 {
00416   PCLASSINFO(PRegisterPage, PConfigPage)
00417   public:
00418     PRegisterPage(
00419       PHTTPServiceProcess & app,
00420       const PHTTPAuthority & auth
00421     );
00422 
00423     PString LoadText(
00424       PHTTPRequest & request        // Information on this request.
00425     );
00426     void OnLoadedText(PHTTPRequest & request, PString & text);
00427 
00428     virtual BOOL Post(
00429       PHTTPRequest & request,       // Information on this request.
00430       const PStringToString & data, // Variables in the POST data.
00431       PHTML & replyMessage          // Reply message for post.
00432     );
00433 
00434     virtual void AddFields(
00435       const PString & prefix        // Prefix on field names
00436     ) = 0;
00437 
00438   protected:
00439     PHTTPServiceProcess & process;
00440 };
00441 
00442 
00444 
00445 class PServiceHTML : public PHTML
00446 {
00447   PCLASSINFO(PServiceHTML, PHTML)
00448   public:
00449     PServiceHTML(const char * title,
00450                  const char * help = NULL,
00451                  const char * helpGif = "help.gif");
00452 
00453     PString ExtractSignature(PString & out);
00454     static PString ExtractSignature(const PString & html,
00455                                     PString & out,
00456                                     const char * keyword = "#equival");
00457 
00458     PString CalculateSignature();
00459     static PString CalculateSignature(const PString & out);
00460     static PString CalculateSignature(const PString & out, const PTEACypher::Key & sig);
00461 
00462     BOOL CheckSignature();
00463     static BOOL CheckSignature(const PString & html);
00464 
00465     enum MacroOptions {
00466       NoOptions           = 0,
00467       NeedSignature       = 1,
00468       LoadFromFile        = 2,
00469       NoURLOverride       = 4,
00470       NoSignatureForFile  = 8
00471     };
00472     static BOOL ProcessMacros(PHTTPRequest & request,
00473                               PString & text,
00474                               const PString & filename,
00475                               unsigned options);
00476 };
00477 
00478 
00480 
00481 class PServiceMacro : public PObject
00482 {
00483   public:
00484     PServiceMacro(const char * name, BOOL isBlock);
00485     PServiceMacro(const PCaselessString & name, BOOL isBlock);
00486     Comparison Compare(const PObject & obj) const;
00487     virtual PString Translate(
00488       PHTTPRequest & request,
00489       const PString & args,
00490       const PString & block
00491     ) const;
00492   protected:
00493     const char * macroName;
00494     BOOL isMacroBlock;
00495     PServiceMacro * link;
00496     static PServiceMacro * list;
00497   friend class PServiceMacros_list;
00498 };
00499 
00500 
00501 #define P_EMPTY
00502 
00503 #define PCREATE_SERVICE_MACRO(name, request, args) \
00504   class PServiceMacro_##name : public PServiceMacro { \
00505     public: \
00506       PServiceMacro_##name() : PServiceMacro(#name, FALSE) { } \
00507       PString Translate(PHTTPRequest &, const PString &, const PString &) const; \
00508   }; \
00509   static const PServiceMacro_##name serviceMacro_##name; \
00510   PString PServiceMacro_##name::Translate(PHTTPRequest & request, const PString & args, const PString &) const
00511 
00512 
00513 
00514 #define PCREATE_SERVICE_MACRO_BLOCK(name, request, args, block) \
00515   class PServiceMacro_##name : public PServiceMacro { \
00516     public: \
00517       PServiceMacro_##name() : PServiceMacro(#name, TRUE) { } \
00518       PString Translate(PHTTPRequest &, const PString &, const PString &) const; \
00519   }; \
00520   static const PServiceMacro_##name serviceMacro_##name; \
00521   PString PServiceMacro_##name::Translate(PHTTPRequest & request, const PString & args, const PString & block) const
00522 
00523 
00524 
00526 
00527 class PServiceHTTPString : public PHTTPString
00528 {
00529   PCLASSINFO(PServiceHTTPString, PHTTPString)
00530   public:
00531     PServiceHTTPString(const PURL & url, const PString & string)
00532       : PHTTPString(url, string) { }
00533 
00534     PServiceHTTPString(const PURL & url, const PHTTPAuthority & auth)
00535       : PHTTPString(url, auth) { }
00536 
00537     PServiceHTTPString(const PURL & url, const PString & string, const PHTTPAuthority & auth)
00538       : PHTTPString(url, string, auth) { }
00539 
00540     PServiceHTTPString(const PURL & url, const PString & string, const PString & contentType)
00541       : PHTTPString(url, string, contentType) { }
00542 
00543     PServiceHTTPString(const PURL & url, const PString & string, const PString & contentType, const PHTTPAuthority & auth)
00544       : PHTTPString(url, string, contentType, auth) { }
00545 
00546     PString LoadText(PHTTPRequest &);
00547 
00548   protected:
00549     virtual BOOL GetExpirationDate(
00550       PTime & when          // Time that the resource expires
00551     );
00552 };
00553 
00554 
00555 class PServiceHTTPFile : public PHTTPFile
00556 {
00557   PCLASSINFO(PServiceHTTPFile, PHTTPFile)
00558   public:
00559     PServiceHTTPFile(const PString & filename, BOOL needSig = FALSE)
00560       : PHTTPFile(filename) { needSignature = needSig; }
00561     PServiceHTTPFile(const PString & filename, const PFilePath & file, BOOL needSig = FALSE)
00562       : PHTTPFile(filename, file) { needSignature = needSig; }
00563     PServiceHTTPFile(const PString & filename, const PHTTPAuthority & auth, BOOL needSig = FALSE)
00564       : PHTTPFile(filename, auth) { needSignature = needSig; }
00565     PServiceHTTPFile(const PString & filename, const PFilePath & file, const PHTTPAuthority & auth, BOOL needSig = FALSE)
00566       : PHTTPFile(filename, file, auth) { needSignature = needSig; }
00567 
00568     void OnLoadedText(PHTTPRequest &, PString & text);
00569 
00570   protected:
00571     virtual BOOL GetExpirationDate(
00572       PTime & when          // Time that the resource expires
00573     );
00574 
00575     BOOL needSignature;
00576 };
00577 
00578 class PServiceHTTPDirectory : public PHTTPDirectory
00579 {
00580   PCLASSINFO(PServiceHTTPDirectory, PHTTPDirectory)
00581   public:
00582     PServiceHTTPDirectory(const PURL & url, const PDirectory & dirname, BOOL needSig = FALSE)
00583       : PHTTPDirectory(url, dirname) { needSignature = needSig; }
00584 
00585     PServiceHTTPDirectory(const PURL & url, const PDirectory & dirname, const PHTTPAuthority & auth, BOOL needSig = FALSE)
00586       : PHTTPDirectory(url, dirname, auth) { needSignature = needSig; }
00587 
00588     void OnLoadedText(PHTTPRequest &, PString & text);
00589 
00590   protected:
00591     virtual BOOL GetExpirationDate(
00592       PTime & when          // Time that the resource expires
00593     );
00594 
00595     BOOL needSignature;
00596 };
00597 
00598 
00599 #endif

Generated on Sat Dec 22 16:46:51 2007 for PWLib by  doxygen 1.4.2