00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
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;
00217 WORD buildNumber;
00218 const char * compilationDate;
00219
00220 PTEACypher::Key productKey;
00221 const char * securedKeys[MaxSecuredKeys];
00222 PINDEX securedKeyCount;
00223
00224 PTEACypher::Key signatureKey;
00225
00226 const char * manufHomePage;
00227 const char * email;
00228 const char * productHTML;
00229
00230 const char * gifHTML;
00231
00232 const char * gifFilename;
00233 int gifWidth;
00234 int gifHeight;
00235
00236 const char * copyrightHolder;
00237 const char * copyrightHomePage;
00238 const char * copyrightEmail;
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,
00356 const PStringToString & data,
00357 PHTML & replyMessage
00358 );
00359
00360 protected:
00361 virtual BOOL GetExpirationDate(
00362 PTime & when
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,
00399 const PStringToString & data,
00400 PHTML & replyMessage
00401 );
00402
00403 protected:
00404 virtual BOOL GetExpirationDate(
00405 PTime & when
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
00425 );
00426 void OnLoadedText(PHTTPRequest & request, PString & text);
00427
00428 virtual BOOL Post(
00429 PHTTPRequest & request,
00430 const PStringToString & data,
00431 PHTML & replyMessage
00432 );
00433
00434 virtual void AddFields(
00435 const PString & prefix
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
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
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
00593 );
00594
00595 BOOL needSignature;
00596 };
00597
00598
00599 #endif