blob: c7b8898f537bba70ac29db869919efcf2b1cf826 [file] [log] [blame]
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev1cf5c432017-01-13 23:22:15 -08003 * Copyright (c) 2013-2017, Regents of the University of California.
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08004 *
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 Zhua9a7e1d2013-02-25 18:29:07 -080020//
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 Zhua9a7e1d2013-02-25 18:29:07 -080031#include "mime_types.hpp"
32#include "reply.hpp"
33#include "request.hpp"
Alexander Afanasyevcaf61b12016-12-25 14:18:52 -080034#include "core/logging.hpp"
35
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080036#include <fstream>
37#include <sstream>
38#include <string>
Zhenkai Zhud9429222013-02-25 22:34:09 -080039
Alexander Afanasyevcaf61b12016-12-25 14:18:52 -080040#include <boost/lexical_cast.hpp>
41
42#include <QDataStream>
43#include <QFile>
44#include <QIODevice>
45#include <QString>
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -080046
47namespace http {
48namespace server {
49
Alexander Afanasyev1cf5c432017-01-13 23:22:15 -080050_LOG_INIT(HttpServer);
Alexander Afanasyevcaf61b12016-12-25 14:18:52 -080051
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -080052request_handler::request_handler(const std::string& doc_root)
Zhenkai Zhud9429222013-02-25 22:34:09 -080053 : doc_root_(doc_root.c_str())
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -080054{
55}
56
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080057void
58request_handler::handle_request(const request& req, reply& rep)
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -080059{
60 // Decode url to path.
61 std::string request_path;
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080062 if (!url_decode(req.uri, request_path)) {
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -080063 rep = reply::stock_reply(reply::bad_request);
64 return;
65 }
66
67 // Request path must be absolute and not contain "..".
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080068 if (request_path.empty() || request_path[0] != '/' || request_path.find("..") != std::string::npos) {
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -080069 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 Afanasyeveda3b7a2016-12-25 11:26:40 -080074 if (request_path[request_path.size() - 1] == '/') {
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -080075 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 Afanasyeveda3b7a2016-12-25 11:26:40 -080082 if (last_dot_pos != std::string::npos && last_dot_pos > last_slash_pos) {
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -080083 extension = request_path.substr(last_dot_pos + 1);
84 }
85
86 // Open the file to send back.
Zhenkai Zhud9429222013-02-25 22:34:09 -080087 // 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 Afanasyeveda3b7a2016-12-25 11:26:40 -080094 if (!file.exists() || !file.open(QIODevice::ReadOnly)) {
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -080095 rep = reply::stock_reply(reply::not_found);
96 return;
97 }
98
Zhenkai Zhud9429222013-02-25 22:34:09 -080099 _LOG_DEBUG("Serving file: " << request_path);
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -0800100 // Fill out the reply to be sent to the client.
101 rep.status = reply::ok;
102 char buf[512];
Zhenkai Zhud9429222013-02-25 22:34:09 -0800103 QDataStream in(&file);
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800104 while (true) {
Zhenkai Zhud9429222013-02-25 22:34:09 -0800105 int bytes = in.readRawData(buf, sizeof(buf));
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800106 if (bytes > 0) {
Zhenkai Zhud9429222013-02-25 22:34:09 -0800107 rep.content.append(buf, bytes);
108 }
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800109 else {
Zhenkai Zhud9429222013-02-25 22:34:09 -0800110 break;
111 }
112 }
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -0800113 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 Afanasyeveda3b7a2016-12-25 11:26:40 -0800120bool
121request_handler::url_decode(const std::string& in, std::string& out)
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -0800122{
123 out.clear();
124 out.reserve(in.size());
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800125 for (std::size_t i = 0; i < in.size(); ++i) {
126 if (in[i] == '%') {
127 if (i + 3 <= in.size()) {
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -0800128 int value = 0;
129 std::istringstream is(in.substr(i + 1, 2));
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800130 if (is >> std::hex >> value) {
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -0800131 out += static_cast<char>(value);
132 i += 2;
133 }
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800134 else {
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -0800135 return false;
136 }
137 }
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800138 else {
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -0800139 return false;
140 }
141 }
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800142 else if (in[i] == '+') {
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -0800143 out += ' ';
144 }
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800145 else {
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -0800146 out += in[i];
147 }
148 }
149 return true;
150}
151
152} // namespace server
153} // namespace http