blob: d2b339e94460635fddfe9ffda059a2baab1c5b7e [file] [log] [blame]
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08001/* -*- 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 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"
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080031#include "logging.h"
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -080032#include "mime_types.hpp"
33#include "reply.hpp"
34#include "request.hpp"
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080035#include <boost/lexical_cast.hpp>
Zhenkai Zhud9429222013-02-25 22:34:09 -080036#include <QDataStream>
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080037#include <QFile>
38#include <QIODevice>
Zhenkai Zhud9429222013-02-25 22:34:09 -080039#include <QString>
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080040#include <fstream>
41#include <sstream>
42#include <string>
Zhenkai Zhud9429222013-02-25 22:34:09 -080043
44INIT_LOGGER("HttpServer")
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -080045
46namespace http {
47namespace server {
48
49request_handler::request_handler(const std::string& doc_root)
Zhenkai Zhud9429222013-02-25 22:34:09 -080050 : doc_root_(doc_root.c_str())
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -080051{
52}
53
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080054void
55request_handler::handle_request(const request& req, reply& rep)
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -080056{
57 // Decode url to path.
58 std::string request_path;
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080059 if (!url_decode(req.uri, request_path)) {
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -080060 rep = reply::stock_reply(reply::bad_request);
61 return;
62 }
63
64 // Request path must be absolute and not contain "..".
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080065 if (request_path.empty() || request_path[0] != '/' || request_path.find("..") != std::string::npos) {
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -080066 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 Afanasyeveda3b7a2016-12-25 11:26:40 -080071 if (request_path[request_path.size() - 1] == '/') {
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -080072 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 Afanasyeveda3b7a2016-12-25 11:26:40 -080079 if (last_dot_pos != std::string::npos && last_dot_pos > last_slash_pos) {
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -080080 extension = request_path.substr(last_dot_pos + 1);
81 }
82
83 // Open the file to send back.
Zhenkai Zhud9429222013-02-25 22:34:09 -080084 // 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 Afanasyeveda3b7a2016-12-25 11:26:40 -080091 if (!file.exists() || !file.open(QIODevice::ReadOnly)) {
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -080092 rep = reply::stock_reply(reply::not_found);
93 return;
94 }
95
Zhenkai Zhud9429222013-02-25 22:34:09 -080096 _LOG_DEBUG("Serving file: " << request_path);
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -080097 // Fill out the reply to be sent to the client.
98 rep.status = reply::ok;
99 char buf[512];
Zhenkai Zhud9429222013-02-25 22:34:09 -0800100 QDataStream in(&file);
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800101 while (true) {
Zhenkai Zhud9429222013-02-25 22:34:09 -0800102 int bytes = in.readRawData(buf, sizeof(buf));
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800103 if (bytes > 0) {
Zhenkai Zhud9429222013-02-25 22:34:09 -0800104 rep.content.append(buf, bytes);
105 }
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800106 else {
Zhenkai Zhud9429222013-02-25 22:34:09 -0800107 break;
108 }
109 }
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -0800110 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 Afanasyeveda3b7a2016-12-25 11:26:40 -0800117bool
118request_handler::url_decode(const std::string& in, std::string& out)
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -0800119{
120 out.clear();
121 out.reserve(in.size());
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800122 for (std::size_t i = 0; i < in.size(); ++i) {
123 if (in[i] == '%') {
124 if (i + 3 <= in.size()) {
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -0800125 int value = 0;
126 std::istringstream is(in.substr(i + 1, 2));
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800127 if (is >> std::hex >> value) {
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -0800128 out += static_cast<char>(value);
129 i += 2;
130 }
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800131 else {
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -0800132 return false;
133 }
134 }
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800135 else {
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -0800136 return false;
137 }
138 }
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800139 else if (in[i] == '+') {
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -0800140 out += ' ';
141 }
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800142 else {
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -0800143 out += in[i];
144 }
145 }
146 return true;
147}
148
149} // namespace server
150} // namespace http