version 0.3.7
[fms.git] / src / http / fmshttprequesthandlerfactory.cpp
1 #include "../../include/http/fmshttprequesthandlerfactory.h"\r
2 #include "../../include/option.h"\r
3 #include "../../include/stringfunctions.h"\r
4 #include "../../include/http/pages/homepage.h"\r
5 #include "../../include/http/pages/optionspage.h"\r
6 #include "../../include/http/pages/createidentitypage.h"\r
7 #include "../../include/http/pages/localidentitiespage.h"\r
8 #include "../../include/http/pages/confirmpage.h"\r
9 #include "../../include/http/pages/showcaptchapage.h"\r
10 #include "../../include/http/pages/announceidentitypage.h"\r
11 #include "../../include/http/pages/execquerypage.h"\r
12 #include "../../include/http/pages/boardspage.h"\r
13 #include "../../include/http/pages/insertedfilespage.h"\r
14 #include "../../include/http/pages/addpeerpage.h"\r
15 #include "../../include/http/pages/peerdetailspage.h"\r
16 #include "../../include/http/pages/controlboardpage.h"\r
17 #include "../../include/http/pages/peermaintenancepage.h"\r
18 #include "../../include/http/pages/peertrustpage.h"\r
19 #include "../../include/http/pages/versioninfopage.h"\r
20 \r
21 FMSHTTPRequestHandlerFactory::FMSHTTPRequestHandlerFactory()\r
22 {\r
23         // set template\r
24         std::string templatestr="<html><head></head><body><a href=\"home.htm\">Home</a><br><h1>Could not open template.htm!  Place in program directory and restart!</h1><br>[CONTENT]</body></html>";\r
25         FILE *infile=fopen("template.htm","r+b");\r
26         if(infile)\r
27         {\r
28                 fseek(infile,0,SEEK_END);\r
29                 long len=ftell(infile);\r
30                 std::vector<char> data(len,0);\r
31                 fseek(infile,0,SEEK_SET);\r
32                 fread(&data[0],1,len,infile);\r
33                 fclose(infile);\r
34                 templatestr.assign(data.begin(),data.end());\r
35         }\r
36         else\r
37         {\r
38                 m_log->error("HTTPThread::HTTPThread could not open template.htm");\r
39         }\r
40 \r
41         // push back page handlers\r
42         m_pagehandlers.push_back(new OptionsPage(templatestr));\r
43         m_pagehandlers.push_back(new CreateIdentityPage(templatestr));\r
44         m_pagehandlers.push_back(new LocalIdentitiesPage(templatestr));\r
45         m_pagehandlers.push_back(new ConfirmPage(templatestr));\r
46         m_pagehandlers.push_back(new ShowCaptchaPage());\r
47         m_pagehandlers.push_back(new AnnounceIdentityPage(templatestr));\r
48         m_pagehandlers.push_back(new ExecQueryPage(templatestr));\r
49         m_pagehandlers.push_back(new BoardsPage(templatestr));\r
50         m_pagehandlers.push_back(new InsertedFilesPage(templatestr));\r
51         m_pagehandlers.push_back(new AddPeerPage(templatestr));\r
52         m_pagehandlers.push_back(new PeerDetailsPage(templatestr));\r
53         m_pagehandlers.push_back(new ControlBoardPage(templatestr));\r
54         m_pagehandlers.push_back(new PeerMaintenancePage(templatestr));\r
55         m_pagehandlers.push_back(new PeerTrustPage(templatestr));\r
56         m_pagehandlers.push_back(new VersionInfoPage(templatestr));\r
57         // homepage must be last - catch all page handler\r
58         m_pagehandlers.push_back(new HomePage(templatestr));\r
59 \r
60         // initialize the access control list\r
61         std::string aclstr;\r
62         std::vector<std::string> aclparts;\r
63         Option::Instance()->Get("HTTPAccessControl",aclstr);\r
64         StringFunctions::Split(aclstr,",",aclparts);\r
65         for(std::vector<std::string>::iterator i=aclparts.begin(); i!=aclparts.end(); i++)\r
66         {\r
67                 m_acl.Add((*i));\r
68         }\r
69 }\r
70 \r
71 FMSHTTPRequestHandlerFactory::~FMSHTTPRequestHandlerFactory()\r
72 {\r
73 \r
74         for(std::vector<IPageHandler *>::iterator i=m_pagehandlers.begin(); i!=m_pagehandlers.end(); i++)\r
75         {\r
76                 delete (*i);\r
77         }\r
78 \r
79 }\r
80 \r
81 Poco::Net::HTTPRequestHandler *FMSHTTPRequestHandlerFactory::createRequestHandler(const Poco::Net::HTTPServerRequest &request)\r
82 {\r
83         if(m_acl.IsAllowed(request.clientAddress().host()))\r
84         {\r
85                 for(std::vector<IPageHandler *>::iterator i=m_pagehandlers.begin(); i!=m_pagehandlers.end(); i++)\r
86                 {\r
87                         if((*i)->WillHandleURI(request.getURI()))\r
88                         {\r
89                                 // we need to return a new object because the HTTPServer will destory it when it's done.\r
90                                 return (*i)->New();\r
91                         }\r
92                 }\r
93         }\r
94         else\r
95         {\r
96                 m_log->debug("FMSHTTPRequestHandlerFactory::createRequestHandler host denied access "+request.clientAddress().host().toString());\r
97         }\r
98         return 0;\r
99 }\r