X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fhttp%2Fhttpthread.cpp;h=23154392a47108c244e2a5ff4259c8317a628822;hb=59a5414ec47a2932a7802fcd1d98c4d80166564f;hp=827ce289446e8ce953f39edf624744d95f4efc4e;hpb=f208e33c29132aacaec448e74341edea1b925a2a;p=fms.git diff --git a/src/http/httpthread.cpp b/src/http/httpthread.cpp index 827ce28..2315439 100644 --- a/src/http/httpthread.cpp +++ b/src/http/httpthread.cpp @@ -1,14 +1,11 @@ #include "../../include/http/httpthread.h" +#include "../../include/http/fmshttprequesthandlerfactory.h" #include "../../include/option.h" #include "../../include/stringfunctions.h" -#include "../../include/http/pages/homepage.h" -#include "../../include/http/pages/optionspage.h" -#include "../../include/http/pages/showcaptchapage.h" -#include "../../include/http/pages/createidentitypage.h" -#include "../../include/http/pages/localidentitiespage.h" -#include "../../include/http/pages/announceidentitypage.h" -#include "../../include/http/pages/addpeerpage.h" -#include "../../include/http/pages/peertrustpage.h" + +#include +#include +#include #include @@ -18,88 +15,55 @@ HTTPThread::HTTPThread() { - std::string templatestr; - int port; - std::string portstr; - Option::Instance()->Get("HTTPListenPort",portstr); - StringFunctions::Convert(portstr,port); - - // set template - templatestr="Home
[CONTENT]"; - FILE *infile=fopen("template.htm","r+b"); - if(infile) - { - fseek(infile,0,SEEK_END); - long len=ftell(infile); - std::vector data(len,0); - fseek(infile,0,SEEK_SET); - fread(&data[0],1,len,infile); - fclose(infile); - templatestr.assign(data.begin(),data.end()); - } - else - { - m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"HTTPThread::HTTPThread could not open template.htm"); - } - - // push back page handlers - m_pagehandlers.push_back(new ShowCaptchaPage()); - m_pagehandlers.push_back(new OptionsPage(templatestr)); - m_pagehandlers.push_back(new LocalIdentitiesPage(templatestr)); - m_pagehandlers.push_back(new CreateIdentityPage(templatestr)); - m_pagehandlers.push_back(new AnnounceIdentityPage(templatestr)); - m_pagehandlers.push_back(new AddPeerPage(templatestr)); - m_pagehandlers.push_back(new PeerTrustPage(templatestr)); - // homepage must be last - catch all page handler - m_pagehandlers.push_back(new HomePage(templatestr)); - - m_ctx=0; - m_ctx=shttpd_init(NULL,"listen_ports",portstr.c_str(),NULL); - shttpd_listen(m_ctx,port,false); - - shttpd_register_uri(m_ctx,"*",HTTPThread::PageCallback,this); - + m_listenport=8080; } -HTTPThread::~HTTPThread() +void HTTPThread::run() { - shttpd_fini(m_ctx); + m_log->debug("HTTPThread::run thread started."); - for(std::vector::iterator i=m_pagehandlers.begin(); i!=m_pagehandlers.end(); i++) - { - delete (*i); - } + LoadDatabase(); + Option option(m_db); -} + std::string portstr("8080"); + option.Get("HTTPListenPort",portstr); + StringFunctions::Convert(portstr,m_listenport); -void HTTPThread::PageCallback(shttpd_arg *arg) -{ + try + { + Poco::Net::ServerSocket sock(m_listenport); + Poco::Net::HTTPServerParams* pParams = new Poco::Net::HTTPServerParams; + pParams->setMaxQueued(30); + pParams->setMaxThreads(5); + Poco::Net::HTTPServer srv(new FMSHTTPRequestHandlerFactory(m_db),sock,pParams); - HTTPThread *thread=(HTTPThread *)arg->user_data; + srv.start(); + m_log->trace("Started HTTPServer"); - for(std::vector::iterator i=thread->m_pagehandlers.begin(); i!=thread->m_pagehandlers.end(); ) - { - if((*i)->Handle(arg)==true) + do { - i=thread->m_pagehandlers.end(); - } - else + Poco::Thread::sleep(1000); + }while(!IsCancelled()); + + m_log->trace("Trying to stop HTTPServer"); + srv.stop(); + m_log->trace("Stopped HTTPServer"); + + m_log->trace("Waiting for current HTTP requests to finish"); + while(srv.currentConnections()>0) { - i++; + Poco::Thread::sleep(500); } } - -} - -void HTTPThread::Run() -{ - m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"HTTPThread::run thread started."); - - do + catch(Poco::Exception &e) { - shttpd_poll(m_ctx,1000); - }while(!IsCancelled()); + m_log->fatal("HTTPThread::run caught "+e.displayText()); + } + catch(...) + { + m_log->fatal("HTTPThread::run caught unknown exception"); + } - m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"HTTPThread::run thread exiting."); + m_log->debug("HTTPThread::run thread exiting."); }