forked from Project-OSRM/osrm-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest_handler.cpp
More file actions
159 lines (135 loc) · 6.45 KB
/
request_handler.cpp
File metadata and controls
159 lines (135 loc) · 6.45 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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include "server/request_handler.hpp"
#include "server/api_grammar.hpp"
#include "server/http/reply.hpp"
#include "server/http/request.hpp"
#include "util/json_renderer.hpp"
#include "util/simple_logger.hpp"
#include "util/string_util.hpp"
#include "util/typedefs.hpp"
#include "engine/route_parameters.hpp"
#include "util/json_container.hpp"
#include "osrm/osrm.hpp"
#include <boost/iostreams/filtering_streambuf.hpp>
#include <boost/iostreams/copy.hpp>
#include <boost/iostreams/filter/gzip.hpp>
#include <ctime>
#include <algorithm>
#include <iostream>
#include <string>
namespace osrm
{
namespace server
{
RequestHandler::RequestHandler() : routing_machine(nullptr) {}
void RequestHandler::handle_request(const http::request ¤t_request,
http::reply ¤t_reply)
{
util::json::Object json_result;
// parse command
try
{
std::string request_string;
util::URIDecode(current_request.uri, request_string);
// deactivated as GCC apparently does not implement that, not even in 4.9
// std::time_t t = std::time(nullptr);
// util::SimpleLogger().Write() << std::put_time(std::localtime(&t), "%m-%d-%Y %H:%M:%S") <<
// " " << current_request.endpoint.to_string() << " " <<
// current_request.referrer << ( 0 == current_request.referrer.length() ? "- " :" ") <<
// current_request.agent << ( 0 == current_request.agent.length() ? "- " :" ") <<
// request;
time_t ltime;
struct tm *time_stamp;
ltime = time(nullptr);
time_stamp = localtime(<ime);
// log timestamp
util::SimpleLogger().Write()
<< (time_stamp->tm_mday < 10 ? "0" : "") << time_stamp->tm_mday << "-"
<< (time_stamp->tm_mon + 1 < 10 ? "0" : "") << (time_stamp->tm_mon + 1) << "-"
<< 1900 + time_stamp->tm_year << " " << (time_stamp->tm_hour < 10 ? "0" : "")
<< time_stamp->tm_hour << ":" << (time_stamp->tm_min < 10 ? "0" : "")
<< time_stamp->tm_min << ":" << (time_stamp->tm_sec < 10 ? "0" : "")
<< time_stamp->tm_sec << " " << current_request.endpoint.to_string() << " "
<< current_request.referrer << (0 == current_request.referrer.length() ? "- " : " ")
<< current_request.agent << (0 == current_request.agent.length() ? "- " : " ")
<< request_string;
engine::RouteParameters route_parameters;
APIGrammarParser api_parser(&route_parameters);
auto api_iterator = request_string.begin();
const bool result =
boost::spirit::qi::parse(api_iterator, request_string.end(), api_parser);
// check if the was an error with the request
if (result && api_iterator == request_string.end())
{
// parsing done, lets call the right plugin to handle the request
BOOST_ASSERT_MSG(routing_machine != nullptr, "pointer not init'ed");
if (!route_parameters.jsonp_parameter.empty())
{ // prepend response with jsonp parameter
const std::string json_p = (route_parameters.jsonp_parameter + "(");
current_reply.content.insert(current_reply.content.end(), json_p.begin(),
json_p.end());
}
const int return_code = routing_machine->RunQuery(route_parameters, json_result);
json_result.values["status"] = return_code;
// 4xx bad request return code
if (return_code / 100 == 4)
{
current_reply.status = http::reply::bad_request;
current_reply.content.clear();
route_parameters.output_format.clear();
}
else
{
// 2xx valid request
BOOST_ASSERT(return_code / 100 == 2);
}
}
else
{
const auto position = std::distance(request_string.begin(), api_iterator);
current_reply.status = http::reply::bad_request;
json_result.values["status"] = http::reply::bad_request;
json_result.values["status_message"] =
"Query string malformed close to position " + std::to_string(position);
}
current_reply.headers.emplace_back("Access-Control-Allow-Origin", "*");
current_reply.headers.emplace_back("Access-Control-Allow-Methods", "GET");
current_reply.headers.emplace_back("Access-Control-Allow-Headers",
"X-Requested-With, Content-Type");
if (route_parameters.service == "tile" && json_result.values.find("pbf") != json_result.values.end())
{
std::copy(json_result.values["pbf"].get<osrm::util::json::Buffer>().value.cbegin(),
json_result.values["pbf"].get<osrm::util::json::Buffer>().value.cend(),
std::back_inserter(current_reply.content));
current_reply.headers.emplace_back("Content-Type", "application/x-protobuf");
}
else if (route_parameters.jsonp_parameter.empty())
{ // json file
util::json::render(current_reply.content, json_result);
current_reply.headers.emplace_back("Content-Type", "application/json; charset=UTF-8");
current_reply.headers.emplace_back("Content-Disposition",
"inline; filename=\"response.json\"");
}
else
{ // jsonp
util::json::render(current_reply.content, json_result);
current_reply.headers.emplace_back("Content-Type", "text/javascript; charset=UTF-8");
current_reply.headers.emplace_back("Content-Disposition",
"inline; filename=\"response.js\"");
}
current_reply.headers.emplace_back("Content-Length",
std::to_string(current_reply.content.size()));
if (!route_parameters.jsonp_parameter.empty())
{ // append brace to jsonp response
current_reply.content.push_back(')');
}
}
catch (const std::exception &e)
{
current_reply = http::reply::stock_reply(http::reply::internal_server_error);
util::SimpleLogger().Write(logWARNING) << "[server error] code: " << e.what()
<< ", uri: " << current_request.uri;
}
}
void RequestHandler::RegisterRoutingMachine(OSRM *osrm) { routing_machine = osrm; }
}
}