X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fhttp%2Fhttpthread.cpp;h=89edc931cfb884ddaf85e585738a08d1fae6abef;hb=023b2219068d481cd6a300982427c99bacd88a12;hp=827ce289446e8ce953f39edf624744d95f4efc4e;hpb=f208e33c29132aacaec448e74341edea1b925a2a;p=fms.git diff --git a/src/http/httpthread.cpp b/src/http/httpthread.cpp index 827ce28..89edc93 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,40 @@ HTTPThread::HTTPThread() { - std::string templatestr; - int port; + m_listenport=8080; 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); - -} - -HTTPThread::~HTTPThread() -{ - shttpd_fini(m_ctx); - - for(std::vector::iterator i=m_pagehandlers.begin(); i!=m_pagehandlers.end(); i++) - { - delete (*i); - } - + StringFunctions::Convert(portstr,m_listenport); } -void HTTPThread::PageCallback(shttpd_arg *arg) +void HTTPThread::run() { + m_log->debug("HTTPThread::run thread started."); - HTTPThread *thread=(HTTPThread *)arg->user_data; + 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,sock,pParams); - for(std::vector::iterator i=thread->m_pagehandlers.begin(); i!=thread->m_pagehandlers.end(); ) - { - if((*i)->Handle(arg)==true) - { - i=thread->m_pagehandlers.end(); - } - else - { - i++; - } - } - -} - -void HTTPThread::Run() -{ - m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"HTTPThread::run thread started."); + srv.start(); + m_log->trace("Started HTTPServer"); do { - shttpd_poll(m_ctx,1000); + Poco::Thread::sleep(1000); }while(!IsCancelled()); - m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"HTTPThread::run thread exiting."); + 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) + { + Poco::Thread::sleep(500); + } + + m_log->debug("HTTPThread::run thread exiting."); }