API calls now get parsed through a grammar using boost::spirit::qi
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
open source routing machine
|
||||
Copyright (C) Dennis Luxen, others 2010
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU AFFERO General Public License as published by
|
||||
the Free Software Foundation; either version 3 of the License, or
|
||||
any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program; if not, write to the Free Software
|
||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
or see http://www.gnu.org/licenses/agpl.txt.
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#ifndef APIGRAMMAR_H_
|
||||
#define APIGRAMMAR_H_
|
||||
|
||||
#include <boost/bind.hpp>
|
||||
#include <boost/spirit/include/qi.hpp>
|
||||
#include <boost/spirit/include/qi_action.hpp>
|
||||
|
||||
namespace qi = boost::spirit::qi;
|
||||
|
||||
template <typename Iterator, class HandlerT>
|
||||
struct APIGrammar : qi::grammar<Iterator> {
|
||||
APIGrammar(HandlerT * h) : APIGrammar::base_type(api_call), handler(h) {
|
||||
api_call = qi::lit('/') >> string[boost::bind(&HandlerT::printService, handler, ::_1)] >> ('?') >> query;
|
||||
query = (*(zoom | output | jsonp | checksum | location | hint | compressed_geometry | language | instruction) ) ;
|
||||
|
||||
zoom = (-qi::lit('&')) >> qi::lit('z') >> '=' >> qi::short_[boost::bind(&HandlerT::setZoomLevel, handler, ::_1)];
|
||||
output = (-qi::lit('&')) >> qi::lit("output") >> '=' >> string[boost::bind(&HandlerT::setOutputFormat, handler, ::_1)];
|
||||
jsonp = (-qi::lit('&')) >> qi::lit("jsonp") >> '=' >> stringwithDot[boost::bind(&HandlerT::setJSONpParameter, handler, ::_1)];
|
||||
checksum = (-qi::lit('&')) >> qi::lit("checksum") >> '=' >> qi::int_[boost::bind(&HandlerT::setChecksum, handler, ::_1)];
|
||||
instruction = (-qi::lit('&')) >> qi::lit("instructions") >> '=' >> qi::bool_[boost::bind(&HandlerT::setInstructionFlag, handler, ::_1)];
|
||||
geometry = (-qi::lit('&')) >> qi::lit("geometry") >> '=' >> qi::bool_[boost::bind(&HandlerT::setGeometryFlag, handler, ::_1)];
|
||||
cmp = (-qi::lit('&')) >> qi::lit("compression") >> '=' >> qi::bool_[boost::bind(&HandlerT::setCompressionFlag, handler, ::_1)];
|
||||
location = (-qi::lit('&')) >> qi::lit("loc") >> '=' >> (qi::double_ >> qi::lit(',') >> qi::double_)[boost::bind(&HandlerT::addCoordinate, handler, ::_1)];
|
||||
hint = (-qi::lit('&')) >> qi::lit("hint") >> '=' >> stringwithDot[boost::bind(&HandlerT::addHint, handler, ::_1)];
|
||||
language = (-qi::lit('&')) >> qi::lit("hl") >> '=' >> string[boost::bind(&HandlerT::setLanguage, handler, ::_1)];
|
||||
|
||||
string = +(qi::char_("a-zA-Z"));
|
||||
stringwithDot = +(qi::char_("a-zA-Z0-9_.-"));
|
||||
}
|
||||
qi::rule<Iterator> api_call, query;
|
||||
qi::rule<Iterator, std::string()> service, zoom, output, string, jsonp, checksum, location, hint, compressed_geometry, stringwithDot, language, instruction, geometry, cmp;
|
||||
|
||||
HandlerT * handler;
|
||||
};
|
||||
|
||||
|
||||
#endif /* APIGRAMMAR_H_ */
|
||||
+35
-45
@@ -28,6 +28,7 @@ or see http://www.gnu.org/licenses/agpl.txt.
|
||||
#include <boost/lexical_cast.hpp>
|
||||
#include <boost/noncopyable.hpp>
|
||||
|
||||
#include "APIGrammar.h"
|
||||
#include "BasicDatastructures.h"
|
||||
#include "../DataStructures/HashTable.h"
|
||||
#include "../Plugins/BasePlugin.h"
|
||||
@@ -51,56 +52,45 @@ public:
|
||||
void handle_request(const Request& req, Reply& rep){
|
||||
//parse command
|
||||
std::string request(req.uri);
|
||||
time_t ltime;
|
||||
struct tm *Tm;
|
||||
|
||||
ltime=time(NULL);
|
||||
Tm=localtime(<ime);
|
||||
{ //This block logs the current request to std out. should be moved to a logging component
|
||||
time_t ltime;
|
||||
struct tm *Tm;
|
||||
|
||||
INFO((Tm->tm_mday < 10 ? "0" : "" ) << Tm->tm_mday << "-" << (Tm->tm_mon+1 < 10 ? "0" : "" ) << (Tm->tm_mon+1) << "-" << 1900+Tm->tm_year << " " << (Tm->tm_hour < 10 ? "0" : "" ) << Tm->tm_hour << ":" << (Tm->tm_min < 10 ? "0" : "" ) << Tm->tm_min << ":" << (Tm->tm_sec < 10 ? "0" : "" ) << Tm->tm_sec << " " <<
|
||||
req.endpoint.to_string() << " " << req.referrer << ( 0 == req.referrer.length() ? "- " :" ") << req.agent << ( 0 == req.agent.length() ? "- " :" ") << request );
|
||||
std::size_t firstAmpPosition = request.find_first_of("?");
|
||||
//DEBUG("[debug] looking for handler for command: " << command);
|
||||
ltime=time(NULL);
|
||||
Tm=localtime(<ime);
|
||||
|
||||
INFO((Tm->tm_mday < 10 ? "0" : "" ) << Tm->tm_mday << "-" << (Tm->tm_mon+1 < 10 ? "0" : "" ) << (Tm->tm_mon+1) << "-" << 1900+Tm->tm_year << " " << (Tm->tm_hour < 10 ? "0" : "" ) << Tm->tm_hour << ":" << (Tm->tm_min < 10 ? "0" : "" ) << Tm->tm_min << ":" << (Tm->tm_sec < 10 ? "0" : "" ) << Tm->tm_sec << " " <<
|
||||
req.endpoint.to_string() << " " << req.referrer << ( 0 == req.referrer.length() ? "- " :" ") << req.agent << ( 0 == req.agent.length() ? "- " :" ") << req.uri );
|
||||
}
|
||||
try {
|
||||
std::string command = request.substr(1,firstAmpPosition-1);
|
||||
if(pluginMap.Holds(command)) {
|
||||
RouteParameters routeParameters;
|
||||
APIGrammar<std::string::iterator, RouteParameters> apiParser(&routeParameters);
|
||||
|
||||
RouteParameters routeParameters;
|
||||
std::stringstream ss(( firstAmpPosition == std::string::npos ? "" : request.substr(firstAmpPosition+1) ));
|
||||
std::string item;
|
||||
while(std::getline(ss, item, '&')) {
|
||||
size_t found = item.find('=');
|
||||
if(found == std::string::npos) {
|
||||
routeParameters.parameters.push_back(item);
|
||||
} else {
|
||||
std::string p = item.substr(0, found);
|
||||
std::transform(p.begin(), p.end(), p.begin(), (int(*)(int)) std::tolower);
|
||||
std::string o = item.substr(found+1);
|
||||
if("jsonp" != p && "hint" != p)
|
||||
std::transform(o.begin(), o.end(), o.begin(), (int(*)(int)) std::tolower);
|
||||
if("loc" == p) {
|
||||
if(25 >= routeParameters.viaPoints.size()) {
|
||||
routeParameters.viaPoints.push_back(o);
|
||||
}
|
||||
} else if("hint" == p) {
|
||||
routeParameters.hints.resize(routeParameters.viaPoints.size());
|
||||
if(routeParameters.viaPoints.size())
|
||||
routeParameters.hints.back() = o;
|
||||
} else {
|
||||
routeParameters.options.Set(p, o);
|
||||
}
|
||||
}
|
||||
}
|
||||
// std::cout << "[debug] found handler for '" << command << "' at version: " << pluginMap.Find(command)->GetVersionString() << std::endl;
|
||||
// std::cout << "[debug] remaining parameters: " << parameters.size() << std::endl;
|
||||
rep.status = Reply::ok;
|
||||
_pluginVector[pluginMap.Find(command)]->HandleRequest(routeParameters, rep );
|
||||
|
||||
// std::cout << rep.content << std::endl;
|
||||
std::string::iterator it = request.begin();
|
||||
bool result = boost::spirit::qi::parse(it, request.end(), apiParser); // returns true if successful
|
||||
if (!result || (it != request.end()) ) {
|
||||
rep = http::Reply::stockReply(http::Reply::badRequest);
|
||||
std::stringstream content;
|
||||
int position = std::distance(request.begin(), it);
|
||||
content << "Input seems to be malformed close to position " << position << "<br>";
|
||||
content << "<pre>";
|
||||
content << req.uri << "<br>";
|
||||
for(unsigned i = 0, end = std::distance(request.begin(), it); i < end; ++i)
|
||||
content << " ";
|
||||
content << "^" << "<br>";
|
||||
content << "</pre>";
|
||||
rep.content += content.str();
|
||||
} else {
|
||||
rep = Reply::stockReply(Reply::badRequest);
|
||||
//Finished parsing, lets call the right plugin to handle the request
|
||||
if(pluginMap.Holds(routeParameters.service)) {
|
||||
rep.status = Reply::ok;
|
||||
_pluginVector[pluginMap.Find(routeParameters.service)]->HandleRequest(routeParameters, rep );
|
||||
} else {
|
||||
rep = Reply::stockReply(Reply::badRequest);
|
||||
}
|
||||
return;
|
||||
}
|
||||
return;
|
||||
} catch(std::exception& e) {
|
||||
rep = Reply::stockReply(Reply::internalServerError);
|
||||
std::cerr << "[server error] code: " << e.what() << ", uri: " << req.uri << std::endl;
|
||||
@@ -112,7 +102,7 @@ public:
|
||||
std::cout << "[handler] registering plugin " << plugin->GetDescriptor() << std::endl;
|
||||
pluginMap.Add(plugin->GetDescriptor(), _pluginCount);
|
||||
_pluginVector.push_back(plugin);
|
||||
_pluginCount++;
|
||||
++_pluginCount;
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
Reference in New Issue
Block a user