diff options
author | 2024-04-22 08:04:58 +1000 | |
---|---|---|
committer | 2024-04-22 08:04:58 +1000 | |
commit | 184292b730f4236bd4840e780fdad97ee060ec84 (patch) | |
tree | 67902161a465fe92aec78ca502de07092b8cde0f /3rdparty/asio/src/examples/cpp03/http/server3/request_handler.cpp | |
parent | 24154bc1f00790f344120b3a85175d6f616c5ad0 (diff) |
3rdparty/asio: Updated to 1.30.2
Diffstat (limited to '3rdparty/asio/src/examples/cpp03/http/server3/request_handler.cpp')
-rw-r--r-- | 3rdparty/asio/src/examples/cpp03/http/server3/request_handler.cpp | 122 |
1 files changed, 0 insertions, 122 deletions
diff --git a/3rdparty/asio/src/examples/cpp03/http/server3/request_handler.cpp b/3rdparty/asio/src/examples/cpp03/http/server3/request_handler.cpp deleted file mode 100644 index 64efd8d02a5..00000000000 --- a/3rdparty/asio/src/examples/cpp03/http/server3/request_handler.cpp +++ /dev/null @@ -1,122 +0,0 @@ -// -// request_handler.cpp -// ~~~~~~~~~~~~~~~~~~~ -// -// Copyright (c) 2003-2021 Christopher M. Kohlhoff (chris at kohlhoff dot com) -// -// Distributed under the Boost Software License, Version 1.0. (See accompanying -// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -// - -#include "request_handler.hpp" -#include <fstream> -#include <sstream> -#include <string> -#include <boost/lexical_cast.hpp> -#include "mime_types.hpp" -#include "reply.hpp" -#include "request.hpp" - -namespace http { -namespace server3 { - -request_handler::request_handler(const std::string& doc_root) - : doc_root_(doc_root) -{ -} - -void request_handler::handle_request(const request& req, reply& rep) -{ - // Decode url to path. - std::string request_path; - if (!url_decode(req.uri, request_path)) - { - rep = reply::stock_reply(reply::bad_request); - return; - } - - // Request path must be absolute and not contain "..". - if (request_path.empty() || request_path[0] != '/' - || request_path.find("..") != std::string::npos) - { - rep = reply::stock_reply(reply::bad_request); - return; - } - - // If path ends in slash (i.e. is a directory) then add "index.html". - if (request_path[request_path.size() - 1] == '/') - { - request_path += "index.html"; - } - - // Determine the file extension. - std::size_t last_slash_pos = request_path.find_last_of("/"); - std::size_t last_dot_pos = request_path.find_last_of("."); - std::string extension; - if (last_dot_pos != std::string::npos && last_dot_pos > last_slash_pos) - { - extension = request_path.substr(last_dot_pos + 1); - } - - // Open the file to send back. - std::string full_path = doc_root_ + request_path; - std::ifstream is(full_path.c_str(), std::ios::in | std::ios::binary); - if (!is) - { - rep = reply::stock_reply(reply::not_found); - return; - } - - // Fill out the reply to be sent to the client. - rep.status = reply::ok; - char buf[512]; - while (is.read(buf, sizeof(buf)).gcount() > 0) - rep.content.append(buf, is.gcount()); - rep.headers.resize(2); - rep.headers[0].name = "Content-Length"; - rep.headers[0].value = boost::lexical_cast<std::string>(rep.content.size()); - rep.headers[1].name = "Content-Type"; - rep.headers[1].value = mime_types::extension_to_type(extension); -} - -bool request_handler::url_decode(const std::string& in, std::string& out) -{ - out.clear(); - out.reserve(in.size()); - for (std::size_t i = 0; i < in.size(); ++i) - { - if (in[i] == '%') - { - if (i + 3 <= in.size()) - { - int value = 0; - std::istringstream is(in.substr(i + 1, 2)); - if (is >> std::hex >> value) - { - out += static_cast<char>(value); - i += 2; - } - else - { - return false; - } - } - else - { - return false; - } - } - else if (in[i] == '+') - { - out += ' '; - } - else - { - out += in[i]; - } - } - return true; -} - -} // namespace server3 -} // namespace http |