-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest_handler.cpp
105 lines (95 loc) · 3.11 KB
/
request_handler.cpp
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
99
100
101
102
103
104
105
//
// request_handler.cpp
// ~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2010 Ivan Ribeiro Rocha (ivanribeiro at gmail dot com)
// 2003-2008 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#include <fstream>
#include <sstream>
#include <iostream>
#include <string>
#include <boost/lexical_cast.hpp>
#include "request_handler.hpp"
#include "reply.hpp"
#include "request.hpp"
#include "router.hpp"
namespace http {
namespace server3 {
request_handler::request_handler(std::size_t thread_pool_size, const std::string& database)
: database_pool_(new soci::connection_pool(thread_pool_size))
{
// Create database connection pool.
for (std::size_t i = 0; i < thread_pool_size; ++i)
{
soci::session& sql = (*database_pool_).at(i);
sql.open(soci::mysql, database);
}
}
void request_handler::handle_request(request& req, reply& rep)
{
// Decode url to path.
std::string request_path;
if (!url_decode(req.uri, request_path))
{
rep = reply::stock_reply(reply::bad_request);
return;
}
// Request path must be absolute and not contain "..".
if (request_path.empty() || request_path[0] != '/'
|| request_path.find("..") != std::string::npos)
{
rep = reply::stock_reply(reply::bad_request);
return;
}
// Router request based upon a REST API
req.database_pool = &(*database_pool_);
router r(req, rep);
if (r.exec() == declined)
{
rep = reply::stock_reply(reply::not_implemented);
}
}
bool request_handler::url_decode(const std::string& in, std::string& out)
{
out.clear();
out.reserve(in.size());
for (std::size_t i = 0; i < in.size(); ++i)
{
if (in[i] == '%')
{
if (i + 3 <= in.size())
{
int value = 0;
std::istringstream is(in.substr(i + 1, 2));
if (is >> std::hex >> value)
{
out += static_cast<char>(value);
i += 2;
}
else
{
return false;
}
}
else
{
return false;
}
}
else if (in[i] == '+')
{
out += ' ';
}
else
{
out += in[i];
}
}
return true;
}
} // namespace server3
} // namespace http