forked from jgarzik/rpcsrv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest_handler.cc
133 lines (118 loc) · 3.1 KB
/
request_handler.cc
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
//
// request_handler.cpp
// ~~~~~~~~~~~~~~~~~~~
//
// Copyright 2012 Red Hat, Inc.
// Copyright (c) 2003-2012 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 "rpcsrv-config.h"
#include <fstream>
#include <sstream>
#include <string>
#include <boost/lexical_cast.hpp>
#include "mime_types.h"
#include "reply.h"
#include "request.h"
#include "request_handler.h"
#include "json/json_spirit_reader.h"
#include "json/json_spirit_writer.h"
#include "json/json_spirit_utils.h"
#include "rpcs.h"
namespace http {
namespace server3 {
extern std::string FormatTime(std::string fmt, boost::posix_time::ptime now);
request_handler::request_handler(const std::string& doc_root)
: doc_root_(doc_root)
{
}
void request_handler::handle_request(const request& req, reply& rep,
bool keepalive)
{
// Decode url to path.
std::string request_path;
if (!url_decode(req.uri, request_path)) {
rep = reply::stock_reply(reply::bad_request);
return;
}
if (req.method != "GET" && req.method != "POST") {
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;
}
if (request_path != "/v1/api") {
rep = reply::stock_reply(reply::not_found);
return;
}
json_spirit::Value jv;
if (!json_spirit::read(req.content, jv) ||
(jv.type() != json_spirit::obj_type)) {
rep = reply::stock_reply(reply::bad_request);
return;
}
json_spirit::Object result;
json_spirit::Object query = jv.get_obj();
json_rpc::exec(query, result);
// Fill out the reply to be sent to the client.
rep.status = reply::ok;
rep.content = json_spirit::write(result);
rep.content += "\r\n";
rep.headers.push_back(header("Date",
FormatTime("%a, %d %b %Y %H:%M:%S GMT", req.tstamp)));
rep.headers.push_back(header(
"Server", PACKAGE_NAME "/" PACKAGE_VERSION));
rep.headers.push_back(header("Content-Length",
boost::lexical_cast<std::string>(rep.content.size())));
rep.headers.push_back(header("Content-Type", "application/json"));
if (keepalive)
rep.headers.push_back(header("Connection", "Keep-Alive"));
else
rep.headers.push_back(header("Connection", "close"));
}
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