| 83 | } |
| 84 | |
| 85 | void RequestHandler::HandleRequest(const Request ¤t_request, |
| 86 | Response ¤t_reply, |
| 87 | const boost::asio::ip::address &remote_address) |
| 88 | { |
| 89 | // Defensive reset: Connection also resets before calling us. |
| 90 | current_reply = {}; |
| 91 | |
| 92 | if (!service_handler) |
| 93 | { |
| 94 | SetInternalServerError(current_reply); |
| 95 | util::Log(logWARNING) << "No service handler registered." << std::endl; |
| 96 | return; |
| 97 | } |
| 98 | |
| 99 | const auto tid = std::this_thread::get_id(); |
| 100 | |
| 101 | // parse command |
| 102 | try |
| 103 | { |
| 104 | TIMER_START(request_duration); |
| 105 | std::string request_string; |
| 106 | util::URIDecode(std::string(current_request.target()), request_string); |
| 107 | |
| 108 | util::Log(logDEBUG) << "[req][" << tid << "] " << request_string; |
| 109 | |
| 110 | auto api_iterator = request_string.begin(); |
| 111 | auto maybe_parsed_url = api::parseURL(api_iterator, request_string.end()); |
| 112 | ServiceHandler::ResultT result; |
| 113 | bhttp::status response_status = bhttp::status::ok; |
| 114 | |
| 115 | // check if there was an error with the request |
| 116 | if (maybe_parsed_url && api_iterator == request_string.end()) |
| 117 | { |
| 118 | |
| 119 | const engine::Status status = |
| 120 | service_handler->RunQuery(*std::move(maybe_parsed_url), result); |
| 121 | if (status != engine::Status::Ok) |
| 122 | { |
| 123 | // 4xx bad request return code |
| 124 | response_status = bhttp::status::bad_request; |
| 125 | } |
| 126 | else |
| 127 | { |
| 128 | BOOST_ASSERT(status == engine::Status::Ok); |
| 129 | } |
| 130 | } |
| 131 | else |
| 132 | { |
| 133 | const auto position = std::distance(request_string.begin(), api_iterator); |
| 134 | BOOST_ASSERT(position >= 0); |
| 135 | const auto context_begin = |
| 136 | request_string.begin() + ((position < 3) ? 0 : (position - 3UL)); |
| 137 | BOOST_ASSERT(context_begin >= request_string.begin()); |
| 138 | const auto context_end = request_string.begin() + |
| 139 | std::min<std::size_t>(position + 3UL, request_string.size()); |
| 140 | BOOST_ASSERT(context_end <= request_string.end()); |
| 141 | std::string context(context_begin, context_end); |
| 142 |
no test coverage detected