-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_handler.cpp
More file actions
209 lines (172 loc) · 7.03 KB
/
Copy pathfile_handler.cpp
File metadata and controls
209 lines (172 loc) · 7.03 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
* Copyright 2011 Iouri Khramtsov.
*
* This software is available under Apache License, Version
* 2.0 (the "License"); 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.
*/
#include <assert.h>
#include <stdio.h>
#include <errno.h>
#include <sys/stat.h>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
#include <boost/shared_ptr.hpp>
#include <boost/shared_array.hpp>
#include <boost/regex.hpp>
#include <event2/event.h>
#include <event2/http.h>
#include <event2/http_struct.h>
#include <event2/buffer.h>
#include <event2/util.h>
#include <event2/keyvalq_struct.h>
#include "file_handler.h"
#include "http_server.h"
#include "http_utils.h"
#include "file_cache.h"
using boost::shared_ptr;
using boost::shared_array;
namespace isaword {
/**
* Set the directory to serve the files from.
*/
FileHandler::FileRootStatusCode FileHandler::initialize(const std::string& file_root) {
//Attempt to look up the file root.
struct stat stat_buffer;
int status = stat(file_root.c_str(), &stat_buffer);
if (status != 0) {
return FileHandler::FILE_ROOT_NOT_FOUND;
}
//Check whether it's a directory.
if (!S_ISDIR(stat_buffer.st_mode)) {
return FileHandler::FILE_ROOT_NOT_A_DIRECTORY;
}
file_root_ = file_root;
//Make sure that the file root ends with '/'.
if (file_root_[file_root_.size() - 1] != '/') {
file_root_ += '/';
}
allowed_path_pattern_ =
boost::regex("^[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]*)*(/[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]*)*)*$");
// Initialize the file cache.
file_cache_ = shared_ptr<FileCache>(new FileCache(file_root));
file_cache_->set_expiration_period(cache_period_sec_);
std::stringstream cache_control;
cache_control << "public, max-age=" << cache_period_sec_;
cache_control_ = cache_control.str();
return FileHandler::FILE_ROOT_OK;
}
/**
* Add the file handler to the HTTP server, setting it to handle files
* from a certain url root (e.g. www.someplace.com/url_root/).
* Should do proper error checking; for now, it doesn't.
*/
FileHandler::ServerAttachStatusCode
FileHandler::attach_to_server(boost::shared_ptr<HttpServer> server,
const std::string& url_root) {
//Do a bit of error checking.
if (is_attached_) {
return FileHandler::ALREADY_ATTACHED;
} else if (file_root_ == "") {
return FileHandler::NO_FILE_ROOT_SET;
}
url_root_ = url_root;
//Make sure that the URL root ends with "/".
if (url_root_[url_root_.size() - 1] != '/') {
url_root_ += '/';
}
//Attach the handler to the server.
std::string url_pattern = url_root_ + ".*";
server->add_url_handler(url_pattern, &FileHandler::handler_callback, (void*) this);
is_attached_ = true;
server_ = server;
return FileHandler::ATTACHED_OK;
}
/**
* Handle a file request.
*/
void FileHandler::handle_request(struct evhttp_request* request) {
//Figure out which file to get.
std::string uri(request_uri_path(request));
std::string relative_file_path(uri.substr(url_root_.size()));
//Check whether the user entered a valid file path.
if (!this->is_permitted_file_path(relative_file_path)) {
// Invalid request. Cannot find the file.
server_->send_response(request, std::string(""), HTTP_NOTFOUND);
return;
}
//Attempt to load the file.
//std::string full_path = file_root_ + relative_file_path;
CachedFilePtr cached_file;
const bool has_loaded = file_cache_->get_cached_object(relative_file_path, cached_file);
if (!has_loaded) {
// No such file.
server_->send_response(request, std::string(""), HTTP_NOTFOUND);
return;
}
// Start writing the response.
// Add Last-Modified response header.
const time_t last_modified = cached_file->last_modified();
struct evkeyvalq* response_headers = evhttp_request_get_output_headers(request);
shared_array<char> sz_last_modified(time_to_string(last_modified));
evhttp_add_header(response_headers, "Last-Modified", sz_last_modified.get());
// Add Cache Control response header.
if (!cache_control_.empty()) {
evhttp_add_header(response_headers, "Cache-Control", cache_control_.c_str());
}
// Set the Content-Type header.
std::string extension;
size_t extension_start = relative_file_path.find_last_of(".");
const char* content_type = NULL;
if (extension_start != std::string::npos) {
extension = relative_file_path.substr(extension_start + 1);
}
if (extension == "css") { content_type = "text/css";}
else if (extension == "js") { content_type = "text/javascript"; }
else if (extension == "png") { content_type = "image/png"; }
else if (extension == "jpeg") { content_type = "image/jpeg"; }
else if (extension == "jpg") { content_type = "image/jpeg"; }
else if (extension == "gif") { content_type = "image/gif"; }
else if (extension == "ico") { content_type = "image/vnd.microsoft.icon"; }
else if (extension == "txt") { content_type = "text/plain"; }
else if (extension == "h") { content_type = "text/plain"; }
else if (extension == "cpp") { content_type = "text/plain"; }
else if (extension == "hpp") { content_type = "text/plain"; }
else if (extension == "html") { content_type = "text/html"; }
else { content_type = "text/html"; }
evhttp_add_header(response_headers, "Content-Type", content_type);
// Check whether the user agent already has the right version of the file.
// TODO: need to implement handling "If-None-Match" header.
struct evkeyvalq* request_headers = evhttp_request_get_input_headers(request);
const char* if_modified_since = evhttp_find_header(request_headers, "If-Modified-Since");
const time_t t_if_modified_since = string_to_time(if_modified_since);
if (t_if_modified_since >= cached_file->last_modified()) {
server_->send_response(request, std::string(""), HTTP_NOTMODIFIED);
return;
}
// Respond with the file data..
shared_array<char> file_data = cached_file->data();
size_t data_size = cached_file->data_size();
server_->send_response_data(request, file_data.get(), data_size, HTTP_OK);
}
/**
* Check whether the file path is a permitted path (e.g. not
* an absolute path, not a path leading up in the directory tree,
* etc).
*/
bool FileHandler::is_permitted_file_path(const std::string& file_path) const {
return regex_match(file_path, allowed_path_pattern_);
}
}