refactor RequestHandler to remove code duplication, nested blocks and object copies
This commit is contained in:
		
							parent
							
								
									971c557d85
								
							
						
					
					
						commit
						f75fcb3041
					
				| @ -66,6 +66,7 @@ void RequestHandler::handle_request(const http::Request &req, http::Reply &reply | |||||||
|         ltime = time(nullptr); |         ltime = time(nullptr); | ||||||
|         Tm = localtime(<ime); |         Tm = localtime(<ime); | ||||||
| 
 | 
 | ||||||
|  |         // log timestamp
 | ||||||
|         SimpleLogger().Write() << (Tm->tm_mday < 10 ? "0" : "") << Tm->tm_mday << "-" |         SimpleLogger().Write() << (Tm->tm_mday < 10 ? "0" : "") << Tm->tm_mday << "-" | ||||||
|                                << (Tm->tm_mon + 1 < 10 ? "0" : "") << (Tm->tm_mon + 1) << "-" |                                << (Tm->tm_mon + 1 < 10 ? "0" : "") << (Tm->tm_mon + 1) << "-" | ||||||
|                                << 1900 + Tm->tm_year << " " << (Tm->tm_hour < 10 ? "0" : "") |                                << 1900 + Tm->tm_year << " " << (Tm->tm_hour < 10 ? "0" : "") | ||||||
| @ -81,6 +82,7 @@ void RequestHandler::handle_request(const http::Request &req, http::Reply &reply | |||||||
|         auto it = request.begin(); |         auto it = request.begin(); | ||||||
|         const bool result = boost::spirit::qi::parse(it, request.end(), api_parser); |         const bool result = boost::spirit::qi::parse(it, request.end(), api_parser); | ||||||
| 
 | 
 | ||||||
|  |         // check if the was an error with the request
 | ||||||
|         if (!result || (it != request.end())) |         if (!result || (it != request.end())) | ||||||
|         { |         { | ||||||
|             reply = http::Reply::StockReply(http::Reply::badRequest); |             reply = http::Reply::StockReply(http::Reply::badRequest); | ||||||
| @ -91,46 +93,39 @@ void RequestHandler::handle_request(const http::Request &req, http::Reply &reply | |||||||
|             std::string message = ("Query string malformed close to position " + IntToString(position)); |             std::string message = ("Query string malformed close to position " + IntToString(position)); | ||||||
|             json_result.values["status_message"] = message; |             json_result.values["status_message"] = message; | ||||||
|             JSON::render(reply.content, json_result); |             JSON::render(reply.content, json_result); | ||||||
|  |             return; | ||||||
|         } |         } | ||||||
|         else | 
 | ||||||
|         { |  | ||||||
|         // parsing done, lets call the right plugin to handle the request
 |         // parsing done, lets call the right plugin to handle the request
 | ||||||
|         BOOST_ASSERT_MSG(routing_machine != nullptr, "pointer not init'ed"); |         BOOST_ASSERT_MSG(routing_machine != nullptr, "pointer not init'ed"); | ||||||
| 
 | 
 | ||||||
|         if (!route_parameters.jsonp_parameter.empty()) |         if (!route_parameters.jsonp_parameter.empty()) | ||||||
|             { |         {   // prepend response with jsonp parameter
 | ||||||
|             const std::string json_p = (route_parameters.jsonp_parameter + "("); |             const std::string json_p = (route_parameters.jsonp_parameter + "("); | ||||||
|             reply.content.insert(reply.content.end(), json_p.begin(), json_p.end()); |             reply.content.insert(reply.content.end(), json_p.begin(), json_p.end()); | ||||||
|         } |         } | ||||||
|         routing_machine->RunQuery(route_parameters, reply); |         routing_machine->RunQuery(route_parameters, reply); | ||||||
|  |         if (!route_parameters.jsonp_parameter.empty()) | ||||||
|  |         {   // append brace to jsonp response
 | ||||||
|  |             reply.content.push_back(')'); | ||||||
|  |         } | ||||||
| 
 | 
 | ||||||
|             // set headers, still ugly and should be reworked
 |         // set headers
 | ||||||
|             reply.headers.resize(3); |         reply.headers.emplace_back("Content-Length", UintToString(reply.content.size())); | ||||||
|         if ("gpx" == route_parameters.output_format) |         if ("gpx" == route_parameters.output_format) | ||||||
|             { |         {   // gpx file
 | ||||||
|                 reply.headers[1].name = "Content-Type"; |             reply.headers.emplace_back("Content-Type", "application/gpx+xml; charset=UTF-8"); | ||||||
|                 reply.headers[1].value = "application/gpx+xml; charset=UTF-8"; |             reply.headers.emplace_back("Content-Disposition", "attachment; filename=\"route.gpx\""); | ||||||
|                 reply.headers[2].name = "Content-Disposition"; |  | ||||||
|                 reply.headers[2].value = "attachment; filename=\"route.gpx\""; |  | ||||||
|         } |         } | ||||||
|         else if (route_parameters.jsonp_parameter.empty()) |         else if (route_parameters.jsonp_parameter.empty()) | ||||||
|             { |         {   // json file
 | ||||||
|                 reply.headers[1].name = "Content-Type"; |             reply.headers.emplace_back("Content-Type", "application/x-javascript; charset=UTF-8"); | ||||||
|                 reply.headers[1].value = "application/x-javascript; charset=UTF-8"; |             reply.headers.emplace_back("Content-Disposition", "inline; filename=\"response.json\""); | ||||||
|                 reply.headers[2].name = "Content-Disposition"; |  | ||||||
|                 reply.headers[2].value = "inline; filename=\"response.json\""; |  | ||||||
|         } |         } | ||||||
|         else |         else | ||||||
|             { |         {   // jsonp
 | ||||||
|                 reply.content.push_back(')'); |             reply.headers.emplace_back("Content-Type", "text/javascript; charset=UTF-8"); | ||||||
|                 reply.headers[1].name = "Content-Type"; |             reply.headers.emplace_back("Content-Disposition", "inline; filename=\"response.js\""); | ||||||
|                 reply.headers[1].value = "text/javascript; charset=UTF-8"; |  | ||||||
|                 reply.headers[2].name = "Content-Disposition"; |  | ||||||
|                 reply.headers[2].value = "inline; filename=\"response.js\""; |  | ||||||
|             } |  | ||||||
|             reply.headers[0].name = "Content-Length"; |  | ||||||
|             reply.headers[0].value = IntToString(reply.content.size()); |  | ||||||
|             return; |  | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|     catch (const std::exception &e) |     catch (const std::exception &e) | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user