-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapplication_server.cpp
More file actions
98 lines (86 loc) · 3.67 KB
/
application_server.cpp
File metadata and controls
98 lines (86 loc) · 3.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/***************************************************************************
* Copyright (C) 2014 by Terraneo Federico *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* As a special exception, if other files instantiate templates or use *
* macros or inline functions from this file, or you compile this file *
* and link it with other works to produce a work based on this file, *
* this file does not by itself cause the resulting work to be covered *
* by the GNU General Public License. However the source code for this *
* file must still be made available in accordance with the GNU General *
* Public License. This exception does not invalidate any other reasons *
* why a work based on this file might be covered by the GNU General *
* Public License. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, see <http://www.gnu.org/licenses/> *
***************************************************************************/
#include <iostream>
#include <stdexcept>
#include "application_server.h"
#include "Poco/Net/HTTPRequestHandler.h"
#include "Poco/Net/HTTPServerResponse.h"
using namespace std;
using namespace Poco::Net;
/**
* Class that returns an HTTP 404
*/
class NotFound : public HTTPRequestHandler
{
public:
void handleRequest(HTTPServerRequest& request, HTTPServerResponse& response)
{
response.setStatus(HTTPResponse::HTTP_NOT_FOUND);
response.setReason("Not found");
response.setChunkedTransferEncoding(true);
response.setContentType("text/html");
std::ostream& responseStream = response.send();
responseStream << "Not Found\n";
}
};
//
// class PageFactoryBase
//
PageFactoryBase::~PageFactoryBase() {}
//
// class ApplicationServer
//
ApplicationServer& ApplicationServer::instance()
{
static ApplicationServer singleton;
return singleton;
}
HTTPRequestHandler* ApplicationServer::createRequestHandler(const HTTPServerRequest& request)
{
string uri=request.getURI();
for(;;)
{
if(uri.empty() || uri.at(0)!='/') break;
uri.erase(0,1); //Strip beginning /
}
size_t param=uri.find_first_of('?');
if(param!=string::npos) uri=uri.substr(0,param);
if(uri.empty()) uri="Index";
auto it=pages.find(uri);
if(it==pages.end()) return new NotFound;
return it->second->generate();
}
void ApplicationServer::registerPage(const std::string& name, PageFactoryBase *factory)
{
if(pages.insert(make_pair(name,factory)).second==false)
throw logic_error(string("attempting to register twice ")+name);
}
ApplicationServer::ApplicationServer() {}
ApplicationServer::~ApplicationServer()
{
for(auto p : pages) delete p.second;
}