Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 1cf5c43 | 2017-01-13 23:22:15 -0800 | [diff] [blame] | 3 | * Copyright (c) 2013-2017, Regents of the University of California. |
Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 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" |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 31 | #include "mime_types.hpp" |
| 32 | #include "reply.hpp" |
| 33 | #include "request.hpp" |
Alexander Afanasyev | caf61b1 | 2016-12-25 14:18:52 -0800 | [diff] [blame] | 34 | #include "core/logging.hpp" |
| 35 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 36 | #include <fstream> |
| 37 | #include <sstream> |
| 38 | #include <string> |
Zhenkai Zhu | d942922 | 2013-02-25 22:34:09 -0800 | [diff] [blame] | 39 | |
Alexander Afanasyev | caf61b1 | 2016-12-25 14:18:52 -0800 | [diff] [blame] | 40 | #include <boost/lexical_cast.hpp> |
| 41 | |
| 42 | #include <QDataStream> |
| 43 | #include <QFile> |
| 44 | #include <QIODevice> |
| 45 | #include <QString> |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 46 | |
| 47 | namespace http { |
| 48 | namespace server { |
| 49 | |
Alexander Afanasyev | 1cf5c43 | 2017-01-13 23:22:15 -0800 | [diff] [blame] | 50 | _LOG_INIT(HttpServer); |
Alexander Afanasyev | caf61b1 | 2016-12-25 14:18:52 -0800 | [diff] [blame] | 51 | |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 52 | request_handler::request_handler(const std::string& doc_root) |
Zhenkai Zhu | d942922 | 2013-02-25 22:34:09 -0800 | [diff] [blame] | 53 | : doc_root_(doc_root.c_str()) |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 54 | { |
| 55 | } |
| 56 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 57 | void |
| 58 | request_handler::handle_request(const request& req, reply& rep) |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 59 | { |
| 60 | // Decode url to path. |
| 61 | std::string request_path; |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 62 | if (!url_decode(req.uri, request_path)) { |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 63 | rep = reply::stock_reply(reply::bad_request); |
| 64 | return; |
| 65 | } |
| 66 | |
| 67 | // Request path must be absolute and not contain "..". |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 68 | 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] | 69 | rep = reply::stock_reply(reply::bad_request); |
| 70 | return; |
| 71 | } |
| 72 | |
| 73 | // 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] | 74 | if (request_path[request_path.size() - 1] == '/') { |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 75 | request_path += "index.html"; |
| 76 | } |
| 77 | |
| 78 | // Determine the file extension. |
| 79 | std::size_t last_slash_pos = request_path.find_last_of("/"); |
| 80 | std::size_t last_dot_pos = request_path.find_last_of("."); |
| 81 | std::string extension; |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 82 | 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] | 83 | extension = request_path.substr(last_dot_pos + 1); |
| 84 | } |
| 85 | |
| 86 | // Open the file to send back. |
Zhenkai Zhu | d942922 | 2013-02-25 22:34:09 -0800 | [diff] [blame] | 87 | // The following is a hack to make the server understands Qt's |
| 88 | // resource system, so that the html resources can be managed using Qt's |
| 89 | // resource system (e.g. no need to worry about the location of html) |
| 90 | // in Mac OS, it will be inside the bundle, in Linux, perhaps somewhere |
| 91 | // in /usr/local/share |
| 92 | QString full_path = doc_root_.absolutePath() + QString(request_path.c_str()); |
| 93 | QFile file(full_path); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 94 | if (!file.exists() || !file.open(QIODevice::ReadOnly)) { |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 95 | rep = reply::stock_reply(reply::not_found); |
| 96 | return; |
| 97 | } |
| 98 | |
Zhenkai Zhu | d942922 | 2013-02-25 22:34:09 -0800 | [diff] [blame] | 99 | _LOG_DEBUG("Serving file: " << request_path); |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 100 | // Fill out the reply to be sent to the client. |
| 101 | rep.status = reply::ok; |
| 102 | char buf[512]; |
Zhenkai Zhu | d942922 | 2013-02-25 22:34:09 -0800 | [diff] [blame] | 103 | QDataStream in(&file); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 104 | while (true) { |
Zhenkai Zhu | d942922 | 2013-02-25 22:34:09 -0800 | [diff] [blame] | 105 | int bytes = in.readRawData(buf, sizeof(buf)); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 106 | if (bytes > 0) { |
Zhenkai Zhu | d942922 | 2013-02-25 22:34:09 -0800 | [diff] [blame] | 107 | rep.content.append(buf, bytes); |
| 108 | } |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 109 | else { |
Zhenkai Zhu | d942922 | 2013-02-25 22:34:09 -0800 | [diff] [blame] | 110 | break; |
| 111 | } |
| 112 | } |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 113 | rep.headers.resize(2); |
| 114 | rep.headers[0].name = "Content-Length"; |
| 115 | rep.headers[0].value = boost::lexical_cast<std::string>(rep.content.size()); |
| 116 | rep.headers[1].name = "Content-Type"; |
| 117 | rep.headers[1].value = mime_types::extension_to_type(extension); |
| 118 | } |
| 119 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 120 | bool |
| 121 | request_handler::url_decode(const std::string& in, std::string& out) |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 122 | { |
| 123 | out.clear(); |
| 124 | out.reserve(in.size()); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 125 | for (std::size_t i = 0; i < in.size(); ++i) { |
| 126 | if (in[i] == '%') { |
| 127 | if (i + 3 <= in.size()) { |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 128 | int value = 0; |
| 129 | std::istringstream is(in.substr(i + 1, 2)); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 130 | if (is >> std::hex >> value) { |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 131 | out += static_cast<char>(value); |
| 132 | i += 2; |
| 133 | } |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 134 | else { |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 135 | return false; |
| 136 | } |
| 137 | } |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 138 | else { |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 139 | return false; |
| 140 | } |
| 141 | } |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 142 | else if (in[i] == '+') { |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 143 | out += ' '; |
| 144 | } |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 145 | else { |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 146 | out += in[i]; |
| 147 | } |
| 148 | } |
| 149 | return true; |
| 150 | } |
| 151 | |
| 152 | } // namespace server |
| 153 | } // namespace http |