version 0.3.0
[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 \r
20 FMSHTTPRequestHandlerFactory::FMSHTTPRequestHandlerFactory()\r
21 {\r
22         // set template\r
23         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
24         FILE *infile=fopen("template.htm","r+b");\r
25         if(infile)\r
26         {\r
27                 fseek(infile,0,SEEK_END);\r
28                 long len=ftell(infile);\r
29                 std::vector<char> data(len,0);\r
30                 fseek(infile,0,SEEK_SET);\r
31                 fread(&data[0],1,len,infile);\r
32                 fclose(infile);\r
33                 templatestr.assign(data.begin(),data.end());\r
34         }\r
35         else\r
36         {\r
37                 m_log->error("HTTPThread::HTTPThread could not open template.htm");\r
38         }\r
39 \r
40         // push back page handlers\r
41         m_pagehandlers.push_back(new OptionsPage(templatestr));\r
42         m_pagehandlers.push_back(new CreateIdentityPage(templatestr));\r
43         m_pagehandlers.push_back(new LocalIdentitiesPage(templatestr));\r
44         m_pagehandlers.push_back(new ConfirmPage(templatestr));\r
45         m_pagehandlers.push_back(new ShowCaptchaPage());\r
46         m_pagehandlers.push_back(new AnnounceIdentityPage(templatestr));\r
47         m_pagehandlers.push_back(new ExecQueryPage(templatestr));\r
48         m_pagehandlers.push_back(new BoardsPage(templatestr));\r
49         m_pagehandlers.push_back(new InsertedFilesPage(templatestr));\r
50         m_pagehandlers.push_back(new AddPeerPage(templatestr));\r
51         m_pagehandlers.push_back(new PeerDetailsPage(templatestr));\r
52         m_pagehandlers.push_back(new ControlBoardPage(templatestr));\r
53         m_pagehandlers.push_back(new PeerMaintenancePage(templatestr));\r
54         m_pagehandlers.push_back(new PeerTrustPage(templatestr));\r
55         // homepage must be last - catch all page handler\r
56         m_pagehandlers.push_back(new HomePage(templatestr));\r
57 \r
58         // initialize the access control list\r
59         std::string aclstr;\r
60         std::vector<std::string> aclparts;\r
61         Option::Instance()->Get("HTTPAccessControl",aclstr);\r
62         StringFunctions::Split(aclstr,",",aclparts);\r
63         for(std::vector<std::string>::iterator i=aclparts.begin(); i!=aclparts.end(); i++)\r
64         {\r
65                 m_acl.Add((*i));\r
66         }\r
67 }\r
68 \r
69 FMSHTTPRequestHandlerFactory::~FMSHTTPRequestHandlerFactory()\r
70 {\r
71 \r
72         for(std::vector<IPageHandler *>::iterator i=m_pagehandlers.begin(); i!=m_pagehandlers.end(); i++)\r
73         {\r
74                 delete (*i);\r
75         }\r
76 \r
77 }\r
78 \r
79 Poco::Net::HTTPRequestHandler *FMSHTTPRequestHandlerFactory::createRequestHandler(const Poco::Net::HTTPServerRequest &request)\r
80 {\r
81         if(m_acl.IsAllowed(request.clientAddress().host()))\r
82         {\r
83                 for(std::vector<IPageHandler *>::iterator i=m_pagehandlers.begin(); i!=m_pagehandlers.end(); i++)\r
84                 {\r
85                         if((*i)->WillHandleURI(request.getURI()))\r
86                         {\r
87                                 // we need to return a new object because the HTTPServer will destory it when it's done.\r
88                                 return (*i)->New();\r
89                         }\r
90                 }\r
91         }\r
92         return 0;\r
93 }\r