From a9f54c44e9b8bd449d4189f8976e8e8a0d69f7d6 Mon Sep 17 00:00:00 2001 From: Dennis Luxen Date: Wed, 13 Nov 2013 18:42:42 -0500 Subject: [PATCH] move Reply into compile unit --- CMakeLists.txt | 5 +- Plugins/BasePlugin.h | 1 + Plugins/LocatePlugin.h | 4 +- Plugins/NearestPlugin.h | 4 +- Plugins/ViaRoutePlugin.h | 4 +- Server/BasicDatastructures.h | 113 ---------------------------------- Server/Connection.h | 2 +- Server/Header.h | 46 ++++++++++++++ Server/Reply.cpp | 116 +++++++++++++++++++++++++++++++++++ Server/Reply.h | 73 ++++++++++++++++++++++ Server/RequestHandler.h | 4 +- 11 files changed, 248 insertions(+), 124 deletions(-) create mode 100644 Server/Header.h create mode 100644 Server/Reply.cpp create mode 100644 Server/Reply.h diff --git a/CMakeLists.txt b/CMakeLists.txt index a8b8b258a..6f4f96fc4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -36,7 +36,7 @@ file(GLOB ExtractorGlob Extractor/*.cpp) set(ExtractorSources extractor.cpp ${ExtractorGlob}) add_executable(osrm-extract ${ExtractorSources} ) -file( GLOB PrepareGlob Contractor/*.cpp) +file( GLOB PrepareGlob Contractor/*.cpp ) set( PrepareSources prepare.cpp ${PrepareGlob} ) add_executable( osrm-prepare ${PrepareSources} ) @@ -44,12 +44,13 @@ add_executable(osrm-routed routed.cpp) set_target_properties(osrm-routed PROPERTIES COMPILE_FLAGS -DROUTED) add_executable(osrm-datastore datastore.cpp) +file(GLOB ServerGlob Server/*.cpp) file(GLOB DescriptorGlob Descriptors/*.cpp) file(GLOB DatastructureGlob DataStructures/*.cpp) file(GLOB AlgorithmGlob Algorithms/*.cpp) file(GLOB LibOSRMGlob Library/*.cpp) -set(OSRMSources ${LibOSRMGlob} ${DescriptorGlob} ${DatastructureGlob} ${AlgorithmGlob}) +set(OSRMSources ${LibOSRMGlob} ${DescriptorGlob} ${DatastructureGlob} ${AlgorithmGlob} ${ServerGlob}) add_library( OSRM SHARED ${OSRMSources} ) add_library( UUID STATIC Util/UUID.cpp ) add_library( GITDESCRIPTION STATIC Util/GitDescription.cpp ) diff --git a/Plugins/BasePlugin.h b/Plugins/BasePlugin.h index 265c8e87b..0823f658f 100644 --- a/Plugins/BasePlugin.h +++ b/Plugins/BasePlugin.h @@ -30,6 +30,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "../DataStructures/Coordinate.h" #include "../Server/BasicDatastructures.h" +#include "../Server/Reply.h" #include "../Server/DataStructures/RouteParameters.h" #include diff --git a/Plugins/LocatePlugin.h b/Plugins/LocatePlugin.h index 0eec321d5..561abc292 100644 --- a/Plugins/LocatePlugin.h +++ b/Plugins/LocatePlugin.h @@ -49,11 +49,11 @@ public: ) { //check number of parameters if(!routeParameters.coordinates.size()) { - reply = http::Reply::stockReply(http::Reply::badRequest); + reply = http::Reply::StockReply(http::Reply::badRequest); return; } if(false == checkCoord(routeParameters.coordinates[0])) { - reply = http::Reply::stockReply(http::Reply::badRequest); + reply = http::Reply::StockReply(http::Reply::badRequest); return; } diff --git a/Plugins/NearestPlugin.h b/Plugins/NearestPlugin.h index def61142a..c715a98ac 100644 --- a/Plugins/NearestPlugin.h +++ b/Plugins/NearestPlugin.h @@ -51,11 +51,11 @@ public: void HandleRequest(const RouteParameters & routeParameters, http::Reply& reply) { //check number of parameters if(!routeParameters.coordinates.size()) { - reply = http::Reply::stockReply(http::Reply::badRequest); + reply = http::Reply::StockReply(http::Reply::badRequest); return; } if( !checkCoord(routeParameters.coordinates[0]) ) { - reply = http::Reply::stockReply(http::Reply::badRequest); + reply = http::Reply::StockReply(http::Reply::badRequest); return; } diff --git a/Plugins/ViaRoutePlugin.h b/Plugins/ViaRoutePlugin.h index 057c2f56c..7c46034f5 100644 --- a/Plugins/ViaRoutePlugin.h +++ b/Plugins/ViaRoutePlugin.h @@ -77,7 +77,7 @@ public: ) { //check number of parameters if( 2 > routeParameters.coordinates.size() ) { - reply = http::Reply::stockReply(http::Reply::badRequest); + reply = http::Reply::StockReply(http::Reply::badRequest); return; } @@ -87,7 +87,7 @@ public: std::vector textCoord; for(unsigned i = 0; i < routeParameters.coordinates.size(); ++i) { if( !checkCoord(routeParameters.coordinates[i]) ) { - reply = http::Reply::stockReply(http::Reply::badRequest); + reply = http::Reply::StockReply(http::Reply::badRequest); return; } rawRoute.rawViaNodeCoordinates.push_back(routeParameters.coordinates[i]); diff --git a/Server/BasicDatastructures.h b/Server/BasicDatastructures.h index 90dad8c3f..e8a729687 100644 --- a/Server/BasicDatastructures.h +++ b/Server/BasicDatastructures.h @@ -31,33 +31,11 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "../Util/StringUtil.h" #include -#include #include -#include -#include namespace http { -const std::string okString = "HTTP/1.0 200 OK\r\n"; -const std::string badRequestString = "HTTP/1.0 400 Bad Request\r\n"; -const std::string internalServerErrorString = "HTTP/1.0 500 Internal Server Error\r\n"; - -const char okHTML[] = ""; -const char badRequestHTML[] = "Bad Request

400 Bad Request

"; -const char internalServerErrorHTML[] = "Internal Server Error

500 Internal Server Error

"; -const char seperators[] = { ':', ' ' }; -const char crlf[] = { '\r', '\n' }; - -struct Header { - std::string name; - std::string value; - void Clear() { - name.clear(); - value.clear(); - } -}; - enum CompressionType { noCompression, gzipRFC1952, @@ -71,97 +49,6 @@ struct Request { boost::asio::ip::address endpoint; }; -struct Reply { - Reply() : status(ok) { content.reserve(2 << 20); } - enum status_type { - ok = 200, - badRequest = 400, - internalServerError = 500 - } status; - - std::vector
headers; - std::vector toBuffers(); - std::vector HeaderstoBuffers(); - std::string content; - static Reply stockReply(status_type status); - void setSize(const unsigned size) { - BOOST_FOREACH ( Header& h, headers) { - if("Content-Length" == h.name) { - std::string sizeString; - intToString(size,h.value); - } - } - } -}; - -boost::asio::const_buffer ToBuffer(Reply::status_type status) { - switch (status) { - case Reply::ok: - return boost::asio::buffer(okString); - case Reply::internalServerError: - return boost::asio::buffer(internalServerErrorString); - default: - return boost::asio::buffer(badRequestString); - } -} - -std::string ToString(Reply::status_type status) { - switch (status) { - case Reply::ok: - return okHTML; - case Reply::badRequest: - return badRequestHTML; - default: - return internalServerErrorHTML; - } -} - -std::vector Reply::toBuffers(){ - std::vector buffers; - buffers.push_back(ToBuffer(status)); - for (std::size_t i = 0; i < headers.size(); ++i) { - Header& h = headers[i]; - buffers.push_back(boost::asio::buffer(h.name)); - buffers.push_back(boost::asio::buffer(seperators)); - buffers.push_back(boost::asio::buffer(h.value)); - buffers.push_back(boost::asio::buffer(crlf)); - } - buffers.push_back(boost::asio::buffer(crlf)); - buffers.push_back(boost::asio::buffer(content)); - return buffers; -} - -std::vector Reply::HeaderstoBuffers(){ - std::vector buffers; - buffers.push_back(ToBuffer(status)); - for (std::size_t i = 0; i < headers.size(); ++i) { - Header& h = headers[i]; - buffers.push_back(boost::asio::buffer(h.name)); - buffers.push_back(boost::asio::buffer(seperators)); - buffers.push_back(boost::asio::buffer(h.value)); - buffers.push_back(boost::asio::buffer(crlf)); - } - buffers.push_back(boost::asio::buffer(crlf)); - return buffers; -} - -Reply Reply::stockReply(Reply::status_type status) { - Reply rep; - rep.status = status; - rep.content = ToString(status); - rep.headers.resize(3); - rep.headers[0].name = "Access-Control-Allow-Origin"; - rep.headers[0].value = "*"; - rep.headers[1].name = "Content-Length"; - - std::string s; - intToString(rep.content.size(), s); - - rep.headers[1].value = s; - rep.headers[2].name = "Content-Type"; - rep.headers[2].value = "text/html"; - return rep; -} } // namespace http #endif //BASIC_DATASTRUCTURES_H diff --git a/Server/Connection.h b/Server/Connection.h index d5a3cfa5c..d5ed6cd97 100644 --- a/Server/Connection.h +++ b/Server/Connection.h @@ -170,7 +170,7 @@ private: break; } } else if (!result) { - reply = Reply::stockReply(Reply::badRequest); + reply = Reply::StockReply(Reply::badRequest); boost::asio::async_write( TCP_socket, reply.toBuffers(), diff --git a/Server/Header.h b/Server/Header.h new file mode 100644 index 000000000..833af27d3 --- /dev/null +++ b/Server/Header.h @@ -0,0 +1,46 @@ +/* + +Copyright (c) 2013, Project OSRM, Dennis Luxen, others +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, +are permitted provided that the following conditions are met: + +Redistributions of source code must retain the above copyright notice, this list +of conditions and the following disclaimer. +Redistributions in binary form must reproduce the above copyright notice, this +list of conditions and the following disclaimer in the documentation and/or +other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +*/ + +#ifndef HTTP_HEADER_H +#define HTTP_HEADER_H + +#include + +namespace http { + + struct Header { + std::string name; + std::string value; + void Clear() { + name.clear(); + value.clear(); + } + }; +} + +#endif //HTTP_HEADER_H + diff --git a/Server/Reply.cpp b/Server/Reply.cpp new file mode 100644 index 000000000..1bad643a1 --- /dev/null +++ b/Server/Reply.cpp @@ -0,0 +1,116 @@ +/* + +Copyright (c) 2013, Project OSRM, Dennis Luxen, others +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, +are permitted provided that the following conditions are met: + +Redistributions of source code must retain the above copyright notice, this list +of conditions and the following disclaimer. +Redistributions in binary form must reproduce the above copyright notice, this +list of conditions and the following disclaimer in the documentation and/or +other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +*/ + +#include "Reply.h" + +namespace http { + +void Reply::setSize(const unsigned size) { + BOOST_FOREACH ( Header& h, headers) { + if("Content-Length" == h.name) { + std::string sizeString; + intToString(size,h.value); + } + } +} + +std::vector Reply::toBuffers(){ + std::vector buffers; + buffers.push_back(ToBuffer(status)); + for (std::size_t i = 0; i < headers.size(); ++i) { + Header& h = headers[i]; + buffers.push_back(boost::asio::buffer(h.name)); + buffers.push_back(boost::asio::buffer(seperators)); + buffers.push_back(boost::asio::buffer(h.value)); + buffers.push_back(boost::asio::buffer(crlf)); + } + buffers.push_back(boost::asio::buffer(crlf)); + buffers.push_back(boost::asio::buffer(content)); + return buffers; +} + +std::vector Reply::HeaderstoBuffers(){ + std::vector buffers; + buffers.push_back(ToBuffer(status)); + for (std::size_t i = 0; i < headers.size(); ++i) { + Header& h = headers[i]; + buffers.push_back(boost::asio::buffer(h.name)); + buffers.push_back(boost::asio::buffer(seperators)); + buffers.push_back(boost::asio::buffer(h.value)); + buffers.push_back(boost::asio::buffer(crlf)); + } + buffers.push_back(boost::asio::buffer(crlf)); + return buffers; +} + +Reply Reply::StockReply(Reply::status_type status) { + Reply rep; + rep.status = status; + rep.content = ToString(status); + rep.headers.resize(3); + rep.headers[0].name = "Access-Control-Allow-Origin"; + rep.headers[0].value = "*"; + rep.headers[1].name = "Content-Length"; + + std::string s; + intToString(rep.content.size(), s); + + rep.headers[1].value = s; + rep.headers[2].name = "Content-Type"; + rep.headers[2].value = "text/html"; + return rep; +} + + +std::string Reply::ToString(Reply::status_type status) { + switch (status) { + case Reply::ok: + return okHTML; + case Reply::badRequest: + return badRequestHTML; + default: + return internalServerErrorHTML; + } +} + +boost::asio::const_buffer Reply::ToBuffer(Reply::status_type status) { + switch (status) { + case Reply::ok: + return boost::asio::buffer(okString); + case Reply::internalServerError: + return boost::asio::buffer(internalServerErrorString); + default: + return boost::asio::buffer(badRequestString); + } +} + + +Reply::Reply() : status(ok) { + content.reserve(2 << 20); +} + +} diff --git a/Server/Reply.h b/Server/Reply.h new file mode 100644 index 000000000..63f29d881 --- /dev/null +++ b/Server/Reply.h @@ -0,0 +1,73 @@ +/* + +Copyright (c) 2013, Project OSRM, Dennis Luxen, others +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, +are permitted provided that the following conditions are met: + +Redistributions of source code must retain the above copyright notice, this list +of conditions and the following disclaimer. +Redistributions in binary form must reproduce the above copyright notice, this +list of conditions and the following disclaimer in the documentation and/or +other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +*/ + +#ifndef REPLY_H +#define REPLY_H + +#include "Header.h" +#include "../Util/StringUtil.h" + +#include +#include + +#include + +namespace http { + +const char okHTML[] = ""; +const char badRequestHTML[] = "Bad Request

400 Bad Request

"; +const char internalServerErrorHTML[] = "Internal Server Error

500 Internal Server Error

"; +const char seperators[] = { ':', ' ' }; +const char crlf[] = { '\r', '\n' }; +const std::string okString = "HTTP/1.0 200 OK\r\n"; +const std::string badRequestString = "HTTP/1.0 400 Bad Request\r\n"; +const std::string internalServerErrorString = "HTTP/1.0 500 Internal Server Error\r\n"; + + class Reply { + public: + enum status_type { + ok = 200, + badRequest = 400, + internalServerError = 500 + } status; + + + std::vector
headers; + std::vector toBuffers(); + std::vector HeaderstoBuffers(); + std::string content; + static Reply StockReply(status_type status); + void setSize(const unsigned size); + Reply(); + private: + static std::string ToString(Reply::status_type status); + boost::asio::const_buffer ToBuffer(Reply::status_type status); +}; + +} + +#endif //REPLY_H diff --git a/Server/RequestHandler.h b/Server/RequestHandler.h index 3f82988d9..e275bf0c5 100644 --- a/Server/RequestHandler.h +++ b/Server/RequestHandler.h @@ -81,7 +81,7 @@ public: ); if ( !result || (it != request.end()) ) { - rep = http::Reply::stockReply(http::Reply::badRequest); + rep = http::Reply::StockReply(http::Reply::badRequest); const int position = std::distance(request.begin(), it); std::string tmp_position_string; intToString(position, tmp_position_string); @@ -105,7 +105,7 @@ public: return; } } catch(std::exception& e) { - rep = http::Reply::stockReply(http::Reply::internalServerError); + rep = http::Reply::StockReply(http::Reply::internalServerError); SimpleLogger().Write(logWARNING) << "[server error] code: " << e.what() << ", uri: " << req.uri; return;