Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2013-2016, Regents of the University of California. |
| 4 | * |
| 5 | * This file is part of ChronoShare, a decentralized file sharing application over NDN. |
| 6 | * |
| 7 | * ChronoShare is free software: you can redistribute it and/or modify it under the terms |
| 8 | * of the GNU General Public License as published by the Free Software Foundation, either |
| 9 | * version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ChronoShare is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License along with |
| 16 | * ChronoShare, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 17 | * |
| 18 | * See AUTHORS.md for complete list of ChronoShare authors and contributors. |
| 19 | */ |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 20 | // |
| 21 | // request_handler.cpp |
| 22 | // ~~~~~~~~~~~~~~~~~~~ |
| 23 | // |
| 24 | // Copyright (c) 2003-2012 Christopher M. Kohlhoff (chris at kohlhoff dot com) |
| 25 | // |
| 26 | // Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 27 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 28 | // |
| 29 | |
| 30 | #include "request_handler.hpp" |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 31 | #include "logging.h" |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 32 | #include "mime_types.hpp" |
| 33 | #include "reply.hpp" |
| 34 | #include "request.hpp" |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 35 | #include <boost/lexical_cast.hpp> |
Zhenkai Zhu | d942922 | 2013-02-25 22:34:09 -0800 | [diff] [blame] | 36 | #include <QDataStream> |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 37 | #include <QFile> |
| 38 | #include <QIODevice> |
Zhenkai Zhu | d942922 | 2013-02-25 22:34:09 -0800 | [diff] [blame] | 39 | #include <QString> |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 40 | #include <fstream> |
| 41 | #include <sstream> |
| 42 | #include <string> |
Zhenkai Zhu | d942922 | 2013-02-25 22:34:09 -0800 | [diff] [blame] | 43 | |
| 44 | INIT_LOGGER("HttpServer") |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 45 | |
| 46 | namespace http { |
| 47 | namespace server { |
| 48 | |
| 49 | request_handler::request_handler(const std::string& doc_root) |
Zhenkai Zhu | d942922 | 2013-02-25 22:34:09 -0800 | [diff] [blame] | 50 | : doc_root_(doc_root.c_str()) |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 51 | { |
| 52 | } |
| 53 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 54 | void |
| 55 | request_handler::handle_request(const request& req, reply& rep) |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 56 | { |
| 57 | // Decode url to path. |
| 58 | std::string request_path; |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 59 | if (!url_decode(req.uri, request_path)) { |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 60 | rep = reply::stock_reply(reply::bad_request); |
| 61 | return; |
| 62 | } |
| 63 | |
| 64 | // Request path must be absolute and not contain "..". |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 65 | if (request_path.empty() || request_path[0] != '/' || request_path.find("..") != std::string::npos) { |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 66 | rep = reply::stock_reply(reply::bad_request); |
| 67 | return; |
| 68 | } |
| 69 | |
| 70 | // If path ends in slash (i.e. is a directory) then add "index.html". |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 71 | if (request_path[request_path.size() - 1] == '/') { |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 72 | request_path += "index.html"; |
| 73 | } |
| 74 | |
| 75 | // Determine the file extension. |
| 76 | std::size_t last_slash_pos = request_path.find_last_of("/"); |
| 77 | std::size_t last_dot_pos = request_path.find_last_of("."); |
| 78 | std::string extension; |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 79 | if (last_dot_pos != std::string::npos && last_dot_pos > last_slash_pos) { |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 80 | extension = request_path.substr(last_dot_pos + 1); |
| 81 | } |
| 82 | |
| 83 | // Open the file to send back. |
Zhenkai Zhu | d942922 | 2013-02-25 22:34:09 -0800 | [diff] [blame] | 84 | // The following is a hack to make the server understands Qt's |
| 85 | // resource system, so that the html resources can be managed using Qt's |
| 86 | // resource system (e.g. no need to worry about the location of html) |
| 87 | // in Mac OS, it will be inside the bundle, in Linux, perhaps somewhere |
| 88 | // in /usr/local/share |
| 89 | QString full_path = doc_root_.absolutePath() + QString(request_path.c_str()); |
| 90 | QFile file(full_path); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 91 | if (!file.exists() || !file.open(QIODevice::ReadOnly)) { |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 92 | rep = reply::stock_reply(reply::not_found); |
| 93 | return; |
| 94 | } |
| 95 | |
Zhenkai Zhu | d942922 | 2013-02-25 22:34:09 -0800 | [diff] [blame] | 96 | _LOG_DEBUG("Serving file: " << request_path); |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 97 | // Fill out the reply to be sent to the client. |
| 98 | rep.status = reply::ok; |
| 99 | char buf[512]; |
Zhenkai Zhu | d942922 | 2013-02-25 22:34:09 -0800 | [diff] [blame] | 100 | QDataStream in(&file); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 101 | while (true) { |
Zhenkai Zhu | d942922 | 2013-02-25 22:34:09 -0800 | [diff] [blame] | 102 | int bytes = in.readRawData(buf, sizeof(buf)); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 103 | if (bytes > 0) { |
Zhenkai Zhu | d942922 | 2013-02-25 22:34:09 -0800 | [diff] [blame] | 104 | rep.content.append(buf, bytes); |
| 105 | } |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 106 | else { |
Zhenkai Zhu | d942922 | 2013-02-25 22:34:09 -0800 | [diff] [blame] | 107 | break; |
| 108 | } |
| 109 | } |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 110 | rep.headers.resize(2); |
| 111 | rep.headers[0].name = "Content-Length"; |
| 112 | rep.headers[0].value = boost::lexical_cast<std::string>(rep.content.size()); |
| 113 | rep.headers[1].name = "Content-Type"; |
| 114 | rep.headers[1].value = mime_types::extension_to_type(extension); |
| 115 | } |
| 116 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 117 | bool |
| 118 | request_handler::url_decode(const std::string& in, std::string& out) |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 119 | { |
| 120 | out.clear(); |
| 121 | out.reserve(in.size()); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 122 | for (std::size_t i = 0; i < in.size(); ++i) { |
| 123 | if (in[i] == '%') { |
| 124 | if (i + 3 <= in.size()) { |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 125 | int value = 0; |
| 126 | std::istringstream is(in.substr(i + 1, 2)); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 127 | if (is >> std::hex >> value) { |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 128 | out += static_cast<char>(value); |
| 129 | i += 2; |
| 130 | } |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 131 | else { |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 132 | return false; |
| 133 | } |
| 134 | } |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 135 | else { |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 136 | return false; |
| 137 | } |
| 138 | } |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 139 | else if (in[i] == '+') { |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 140 | out += ' '; |
| 141 | } |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 142 | else { |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 143 | out += in[i]; |
| 144 | } |
| 145 | } |
| 146 | return true; |
| 147 | } |
| 148 | |
| 149 | } // namespace server |
| 150 | } // namespace http |