📄 httpsvc.cxx
字号:
Sleep(1); httpThreadsMutex.Wait(); } httpThreadsMutex.Signal(); delete httpListeningSocket; httpListeningSocket = NULL;}PString PHTTPServiceProcess::GetCopyrightText(){ PHTML html(PHTML::InBody); html << "Copyright ©" << compilationDate.AsString("yyyy") << " by " << PHTML::HotLink(copyrightHomePage) << copyrightHolder << PHTML::HotLink() << ", " << PHTML::HotLink("mailto:" + copyrightEmail) << copyrightEmail << PHTML::HotLink(); return html;}PString PHTTPServiceProcess::GetPageGraphic(){ PFile header; if (header.Open("header.html", PFile::ReadOnly)) return header.ReadString(header.GetLength()); PHTML html(PHTML::InBody); html << PHTML::TableStart() << PHTML::TableRow() << PHTML::TableData(); if (gifHTML.IsEmpty()) html << PHTML::Heading(1) << productNameHTML << " " << PHTML::Heading(1); else html << gifHTML; html << PHTML::TableData() << GetOSClass() << ' ' << GetOSName() << " Version " << GetVersion(TRUE) << PHTML::BreakLine() << ' ' << GetCompilationDate().AsString("d MMMM yyyy") << PHTML::BreakLine() << "By " << PHTML::HotLink(manufacturersHomePage) << GetManufacturer() << PHTML::HotLink() << ", " << PHTML::HotLink("mailto:" + manufacturersEmail) << manufacturersEmail << PHTML::HotLink() << PHTML::TableEnd() << PHTML::HRule(); return html;}void PHTTPServiceProcess::GetPageHeader(PHTML & html){ GetPageHeader(html, GetName());}void PHTTPServiceProcess::GetPageHeader(PHTML & html, const PString & title){ html << PHTML::Title(title) << PHTML::Body() << GetPageGraphic();}PTCPSocket * PHTTPServiceProcess::AcceptHTTP(){ if (httpListeningSocket == NULL) return NULL; if (!httpListeningSocket->IsOpen()) return NULL; // get a socket when a client connects PTCPSocket * socket = new PTCPSocket; if (socket->Accept(*httpListeningSocket)) return socket; if (socket->GetErrorCode() != PChannel::Interrupted) PSYSTEMLOG(Error, "Accept failed for HTTP: " << socket->GetErrorText()); if (httpListeningSocket != NULL && httpListeningSocket->IsOpen()) return socket; delete socket; return NULL;}BOOL PHTTPServiceProcess::ProcessHTTP(PTCPSocket & socket){ if (!socket.IsOpen()) return TRUE; PHTTPServer * server = CreateHTTPServer(socket); if (server == NULL) { PSYSTEMLOG(Error, "HTTP server creation/open failed."); return TRUE; } // process requests while (server->ProcessCommand()) ; // always close after the response has been sent delete server; // if a restart was requested, then do it, but only if we are not shutting down if (httpListeningSocket->IsOpen()) CompleteRestartSystem(); return TRUE;}void PHTTPServiceProcess::BeginRestartSystem(){ if (restartThread == NULL) { restartThread = PThread::Current(); OnConfigChanged(); }}void PHTTPServiceProcess::CompleteRestartSystem(){ if (restartThread == NULL) return; if (restartThread != PThread::Current()) return; httpNameSpace.StartWrite(); if (Initialise("Restart\tInitialisation")) restartThread = NULL; httpNameSpace.EndWrite(); if (restartThread != NULL) Terminate();}void PHTTPServiceProcess::AddRegisteredText(PHTML &){}void PHTTPServiceProcess::AddUnregisteredText(PHTML &){}BOOL PHTTPServiceProcess::SubstituteEquivalSequence(PHTTPRequest &, const PString &, PString &){ return FALSE;}PHTTPServer * PHTTPServiceProcess::CreateHTTPServer(PTCPSocket & socket){#ifdef SO_LINGER const linger ling = { 1, 5 }; socket.SetOption(SO_LINGER, &ling, sizeof(ling));#endif PHTTPServer * server = OnCreateHTTPServer(httpNameSpace); if (server->Open(socket)) return server; delete server; return NULL;}PHTTPServer * PHTTPServiceProcess::OnCreateHTTPServer(const PHTTPSpace & httpNameSpace){ return new PHTTPServer(httpNameSpace);}//////////////////////////////////////////////////////////////PHTTPServiceThread::PHTTPServiceThread(PINDEX stackSize, PHTTPServiceProcess & app) : PThread(stackSize, AutoDeleteThread, NormalPriority, "HTTP Service:%x"), process(app){ process.httpThreadsMutex.Wait(); process.httpThreads.Append(this); process.httpThreadsMutex.Signal(); myStackSize = stackSize; socket = NULL; Resume();}PHTTPServiceThread::~PHTTPServiceThread(){ process.httpThreadsMutex.Wait(); process.httpThreads.Remove(this); process.httpThreadsMutex.Signal(); delete socket;}void PHTTPServiceThread::Close(){ if (socket != NULL) socket->Close();}void PHTTPServiceThread::Main(){ PTCPSocket * socket = process.AcceptHTTP(); if (socket != NULL) { new PHTTPServiceThread(myStackSize, process); process.ProcessHTTP(*socket); }}//////////////////////////////////////////////////////////////PConfigPage::PConfigPage(PHTTPServiceProcess & app, const PString & title, const PString & section, const PHTTPAuthority & auth) : PHTTPConfig(title, section, auth), process(app){}PConfigPage::PConfigPage(PHTTPServiceProcess & app, const PString & section, const PHTTPAuthority & auth) : PHTTPConfig(section.ToLower() + ".html", section, auth), process(app){}void PConfigPage::OnLoadedText(PHTTPRequest & request, PString & text){ PServiceHTML::ProcessMacros(request, text, GetURL().AsString(PURL::PathOnly).Mid(1), PServiceHTML::LoadFromFile); PHTTPConfig::OnLoadedText(request, text); PServiceHTML::ProcessMacros(request, text, "", PServiceHTML::NoOptions);}BOOL PConfigPage::OnPOST(PHTTPServer & server, const PURL & url, const PMIMEInfo & info, const PStringToString & data, const PHTTPConnectionInfo & connectInfo){ PHTTPConfig::OnPOST(server, url, info, data, connectInfo); return FALSE; // Make sure we break any persistent connections}BOOL PConfigPage::Post(PHTTPRequest & request, const PStringToString & data, PHTML & reply){ PSYSTEMLOG(Debug3, "Post to " << request.url << '\n' << data); BOOL retval = PHTTPConfig::Post(request, data, reply); if (request.code == PHTTP::RequestOK) process.BeginRestartSystem(); PServiceHTML::ProcessMacros(request, reply, GetURL().AsString(PURL::PathOnly).Mid(1), PServiceHTML::LoadFromFile); OnLoadedText(request, reply); return retval;}BOOL PConfigPage::GetExpirationDate(PTime & when){ // Well and truly before now.... when = ImmediateExpiryTime; return TRUE;}//////////////////////////////////////////////////////////////PConfigSectionsPage::PConfigSectionsPage(PHTTPServiceProcess & app, const PURL & url, const PHTTPAuthority & auth, const PString & prefix, const PString & valueName, const PURL & editSection, const PURL & newSection, const PString & newTitle, PHTML & heading) : PHTTPConfigSectionList(url, auth, prefix, valueName, editSection, newSection, newTitle, heading), process(app){}void PConfigSectionsPage::OnLoadedText(PHTTPRequest & request, PString & text){ PServiceHTML::ProcessMacros(request, text, GetURL().AsString(PURL::PathOnly).Mid(1), PServiceHTML::LoadFromFile); PHTTPConfigSectionList::OnLoadedText(request, text);}BOOL PConfigSectionsPage::OnPOST(PHTTPServer & server, const PURL & url, const PMIMEInfo & info, const PStringToString & data, const PHTTPConnectionInfo & connectInfo){ PHTTPConfigSectionList::OnPOST(server, url, info, data, connectInfo); return FALSE; // Make sure we break any persistent connections}BOOL PConfigSectionsPage::Post(PHTTPRequest & request, const PStringToString & data, PHTML & reply){ BOOL retval = PHTTPConfigSectionList::Post(request, data, reply); if (request.code == PHTTP::RequestOK) process.BeginRestartSystem(); return retval;}BOOL PConfigSectionsPage::GetExpirationDate(PTime & when){ // Well and truly before now.... when = ImmediateExpiryTime; return TRUE;}//////////////////////////////////////////////////////////////PRegisterPage::PRegisterPage(PHTTPServiceProcess & app, const PHTTPAuthority & auth) : PConfigPage(app, "register.html", "Secured Options", auth), process(app){}PString PRegisterPage::LoadText(PHTTPRequest & request){ if (fields.GetSize() > 0) return PConfigPage::LoadText(request); PString mailURL = "mailto:" + process.GetEMailAddress(); PString orderURL = mailURL; PString tempURL = mailURL; if (process.GetHomePage() == HOME_PAGE) { orderURL = "https://home.equival.com.au/purchase.html"; tempURL = "http://www.equival.com/" + process.GetName().ToLower() + "/register.html"; tempURL.Replace(" ", "", TRUE); } PServiceHTML regPage(process.GetName() & "Registration", NULL); regPage << "<!--#registration start Permanent-->" "Your registration key is permanent.<p>" "Do not change your registration details or your key will not " "operate correctly.<p>" "If you need to " << PHTML::HotLink(orderURL) << "upgrade" << PHTML::HotLink() << " or " << PHTML::HotLink(mailURL) << "change" << PHTML::HotLink() << " your registration, then you may enter the new values sent " << " to you from " << process.GetManufacturer() << " into the fields " "below, and then press the Accept button.<p>" << PHTML::HRule() << "<!--#registration end Permanent-->" "<!--#registration start Temporary-->" "Your registration key is temporary and will expire on " "<!--#registration ExpiryDate-->.<p>" "Do not change your registration details or your key will not " "operate correctly.<p>" "You may " << PHTML::HotLink(orderURL) << "order a permanent key" << PHTML::HotLink() << " and enter the new values sent to you from " << process.GetManufacturer() << " into the fields below, and then press the Accept button.<p>" << PHTML::HRule() << "<!--#registration end Temporary-->" "<!--#registration start Expired-->" "Your temporary registration key has expired.<p>" "You may " << PHTML::HotLink(orderURL) << "order a permanent key" << PHTML::HotLink() << " and enter the new values sent to you from " << process.GetManufacturer() << " into the fields below, and then press the Accept button.<P>" << PHTML::HRule() << "<!--#registration end Expired-->"; PSecureConfig securedConf(process.GetProductKey(), process.GetSecuredKeys()); PString prefix; if (securedConf.GetValidation() != PSecureConfig::IsValid) prefix = securedConf.GetPendingPrefix(); AddFields(prefix); Add(new PHTTPStringField("Validation", 40)); BuildHTML(regPage, InsertIntoHTML); regPage << "<!--#registration start Invalid-->" "You have entered the values sent to you from " << process.GetManufacturer() << " incorrectly. Please enter them again. Note, " << PHTML::Emphasis() << PHTML::Strong() << "all" << PHTML::Strong() << PHTML::Emphasis() << "the fields must be entered " << PHTML::Emphasis() << PHTML::Strong() << "exactly" << PHTML::Strong() << PHTML::Emphasis() << " as they appear in the e-mail from " << process.GetManufacturer() << ". We strongly recommend using copy and paste of all the fields, and then " "press the Accept button." "<!--#registration end Invalid-->" "<!--#registration start Default-->" "You may " << PHTML::HotLink(orderURL) << "order a permanent key" << PHTML::HotLink() << " or " << PHTML::HotLink(tempURL) << "obtain a temporary key" << PHTML::HotLink() << " and enter the values sent to you from " << process.GetManufacturer() << " into the fields above, and then press the Accept button.<p>" "<!--#registration end Default-->" << PHTML::HRule() << PHTML::Heading(3) << "Disclaimer" << PHTML::Heading(3) << PHTML::Paragraph() << PHTML::Bold() << "The information and code herein is provided \"as is\" " "without warranty of any kind, either expressed or implied, " "including but not limited to the implied warrenties of " "merchantability and fitness for a particular purpose. In " "no event shall " << process.GetManufacturer() << " be liable " "for any damages whatsoever including direct, indirect, " "incidental, consequential, loss of business profits or special " "damages, even if " << process.GetManufacturer() << " has been " "advised of the possibility of such damages." << PHTML::Bold() << PHTML::Paragraph() << process.GetCopyrightText() << PHTML::Body(); SetString(regPage); return PConfigPage::LoadText(request);}static BOOL FindSpliceBlock(const PRegularExpression & regex, const PString & text, PINDEX & pos, PINDEX & len, PINDEX & start, PINDEX & finish){ if (!text.FindRegEx(regex, pos, len, 0)) return FALSE; PINDEX endpos, endlen; static PRegularExpression EndBlock("<?!--#registration[ \t\n]*end[ \t\n]*[a-z]*[ \t\n]*-->?", PRegularExpression::Extended|PRegularExpression::IgnoreCase); if (text.FindRegEx(EndBlock, endpos, endlen, pos)) { start = pos+len; finish = endpos-1; len = endpos - pos + endlen; } return TRUE;}void PRegisterPage::OnLoadedText(PHTTPRequest & request, PString & text){ PString block;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -