forked from scylladb/scylladb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_handler.cc
129 lines (115 loc) · 4.52 KB
/
file_handler.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
* This file is open source software, licensed to you under the terms
* of the Apache License, Version 2.0 (the "License"). See the NOTICE file
* distributed with this work for additional information regarding copyright
* ownership. You may not use this file except in compliance with the License.
*
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
/*
* Copyright 2015 Cloudius Systems
*/
#include "file_handler.hh"
#include <algorithm>
#include <iostream>
#include "core/reactor.hh"
#include "core/fstream.hh"
#include "core/shared_ptr.hh"
#include "core/app-template.hh"
#include "exception.hh"
namespace httpd {
directory_handler::directory_handler(const sstring& doc_root,
file_transformer* transformer)
: file_interaction_handler(transformer), doc_root(doc_root) {
}
future<std::unique_ptr<reply>> directory_handler::handle(const sstring& path,
std::unique_ptr<request> req, std::unique_ptr<reply> rep) {
sstring full_path = doc_root + req->param["path"];
auto h = this;
return engine().file_type(full_path).then(
[h, full_path, req = std::move(req), rep = std::move(rep)](auto val) mutable {
if (val) {
if (val.value() == directory_entry_type::directory) {
if (h->redirect_if_needed(*req.get(), *rep.get())) {
return make_ready_future<std::unique_ptr<reply>>(std::move(rep));
}
full_path += "/index.html";
}
return h->read(full_path, std::move(req), std::move(rep));
}
rep->set_status(reply::status_type::not_found).done();
return make_ready_future<std::unique_ptr<reply>>(std::move(rep));
});
}
file_interaction_handler::~file_interaction_handler() {
delete transformer;
}
sstring file_interaction_handler::get_extension(const sstring& file) {
size_t last_slash_pos = file.find_last_of('/');
size_t last_dot_pos = file.find_last_of('.');
sstring extension;
if (last_dot_pos != sstring::npos && last_dot_pos > last_slash_pos) {
extension = file.substr(last_dot_pos + 1);
}
return extension;
}
struct reader {
reader(file f, std::unique_ptr<reply> rep)
: is(
make_file_input_stream(make_lw_shared<file>(std::move(f)),
0, 4096)), _rep(std::move(rep)) {
}
input_stream<char> is;
std::unique_ptr<reply> _rep;
// for input_stream::consume():
template<typename Done>
void operator()(temporary_buffer<char> data, Done&& done) {
if (data.empty()) {
done(std::move(data));
_rep->done();
} else {
_rep->_content.append(data.get(), data.size());
}
}
};
future<std::unique_ptr<reply>> file_interaction_handler::read(
const sstring& file_name, std::unique_ptr<request> req,
std::unique_ptr<reply> rep) {
sstring extension = get_extension(file_name);
rep->set_content_type(extension);
return engine().open_file_dma(file_name, open_flags::ro).then(
[rep = std::move(rep)](file f) mutable {
std::shared_ptr<reader> r = std::make_shared<reader>(std::move(f), std::move(rep));
return r->is.consume(*r).then([r]() {
r->_rep->done();
return make_ready_future<std::unique_ptr<reply>>(std::move(r->_rep));
});
});
}
bool file_interaction_handler::redirect_if_needed(const request& req,
reply& rep) const {
if (req._url.length() == 0 || req._url.back() != '/') {
rep.set_status(reply::status_type::moved_permanently);
rep._headers["Location"] = req.get_url() + "/";
rep.done();
return true;
}
return false;
}
future<std::unique_ptr<reply>> file_handler::handle(const sstring& path,
std::unique_ptr<request> req, std::unique_ptr<reply> rep) {
if (force_path && redirect_if_needed(*req.get(), *rep.get())) {
return make_ready_future<std::unique_ptr<reply>>(std::move(rep));
}
return read(file, std::move(req), std::move(rep));
}
}