diff --git a/Server/Http/BasicDatastructures.h b/Server/Http/BasicDatastructures.h new file mode 100644 index 000000000..f92cdc57a --- /dev/null +++ b/Server/Http/BasicDatastructures.h @@ -0,0 +1,41 @@ +/* + +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 BASIC_DATASTRUCTURES_H +#define BASIC_DATASTRUCTURES_H + +namespace http { + +enum CompressionType { + noCompression, + gzipRFC1952, + deflateRFC1951 +}; + +} // namespace http + +#endif //BASIC_DATASTRUCTURES_H diff --git a/Server/Http/Header.h b/Server/Http/Header.h new file mode 100644 index 000000000..833af27d3 --- /dev/null +++ b/Server/Http/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/Http/Reply.cpp b/Server/Http/Reply.cpp new file mode 100644 index 000000000..1bad643a1 --- /dev/null +++ b/Server/Http/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/Http/Reply.h b/Server/Http/Reply.h new file mode 100644 index 000000000..7757729e0 --- /dev/null +++ b/Server/Http/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