blob: 6087046e35b703d2e41cd2da45ad33aec1aa02b1 [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"
31#include <fstream>
32#include <sstream>
33#include <string>
34#include <boost/lexical_cast.hpp>
35#include "mime_types.hpp"
36#include "reply.hpp"
37#include "request.hpp"
Zhenkai Zhud9429222013-02-25 22:34:09 -080038#include <QIODevice>
39#include <QFile>
40#include <QDataStream>
41#include <QString>
42#include "logging.h"
43
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
54void request_handler::handle_request(const request& req, reply& rep)
55{
56 // Decode url to path.
57 std::string request_path;
58 if (!url_decode(req.uri, request_path))
59 {
60 rep = reply::stock_reply(reply::bad_request);
61 return;
62 }
63
64 // Request path must be absolute and not contain "..".
65 if (request_path.empty() || request_path[0] != '/'
66 || request_path.find("..") != std::string::npos)
67 {
68 rep = reply::stock_reply(reply::bad_request);
69 return;
70 }
71
72 // If path ends in slash (i.e. is a directory) then add "index.html".
73 if (request_path[request_path.size() - 1] == '/')
74 {
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;
82 if (last_dot_pos != std::string::npos && last_dot_pos > last_slash_pos)
83 {
84 extension = request_path.substr(last_dot_pos + 1);
85 }
86
87 // Open the file to send back.
Zhenkai Zhud9429222013-02-25 22:34:09 -080088 // The following is a hack to make the server understands Qt's
89 // resource system, so that the html resources can be managed using Qt's
90 // resource system (e.g. no need to worry about the location of html)
91 // in Mac OS, it will be inside the bundle, in Linux, perhaps somewhere
92 // in /usr/local/share
93 QString full_path = doc_root_.absolutePath() + QString(request_path.c_str());
94 QFile file(full_path);
95 if (!file.exists() || !file.open(QIODevice::ReadOnly))
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -080096 {
97 rep = reply::stock_reply(reply::not_found);
98 return;
99 }
100
Zhenkai Zhud9429222013-02-25 22:34:09 -0800101 _LOG_DEBUG("Serving file: " << request_path);
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -0800102 // Fill out the reply to be sent to the client.
103 rep.status = reply::ok;
104 char buf[512];
Zhenkai Zhud9429222013-02-25 22:34:09 -0800105 QDataStream in(&file);
106 while (true)
107 {
108 int bytes = in.readRawData(buf, sizeof(buf));
109 if (bytes > 0)
110 {
111 rep.content.append(buf, bytes);
112 }
113 else
114 {
115 break;
116 }
117 }
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -0800118 rep.headers.resize(2);
119 rep.headers[0].name = "Content-Length";
120 rep.headers[0].value = boost::lexical_cast<std::string>(rep.content.size());
121 rep.headers[1].name = "Content-Type";
122 rep.headers[1].value = mime_types::extension_to_type(extension);
123}
124
125bool request_handler::url_decode(const std::string& in, std::string& out)
126{
127 out.clear();
128 out.reserve(in.size());
129 for (std::size_t i = 0; i < in.size(); ++i)
130 {
131 if (in[i] == '%')
132 {
133 if (i + 3 <= in.size())
134 {
135 int value = 0;
136 std::istringstream is(in.substr(i + 1, 2));
137 if (is >> std::hex >> value)
138 {
139 out += static_cast<char>(value);
140 i += 2;
141 }
142 else
143 {
144 return false;
145 }
146 }
147 else
148 {
149 return false;
150 }
151 }
152 else if (in[i] == '+')
153 {
154 out += ' ';
155 }
156 else
157 {
158 out += in[i];
159 }
160 }
161 return true;
162}
163
164} // namespace server
165} // namespace http