-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathKStaticFileCache.cpp
More file actions
157 lines (133 loc) · 3.96 KB
/
Copy pathKStaticFileCache.cpp
File metadata and controls
157 lines (133 loc) · 3.96 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
#include "KStaticFileCache.h"
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
namespace kback {
namespace {
std::string detectContentType(const std::string &path) {
const std::string::size_type dot = path.find_last_of('.');
if (dot == std::string::npos) {
return "application/octet-stream";
}
const std::string extension = path.substr(dot);
if (extension == ".html" || extension == ".htm") {
return "text/html; charset=utf-8";
}
if (extension == ".css") {
return "text/css; charset=utf-8";
}
if (extension == ".js") {
return "application/javascript; charset=utf-8";
}
if (extension == ".json") {
return "application/json; charset=utf-8";
}
if (extension == ".txt") {
return "text/plain; charset=utf-8";
}
if (extension == ".svg") {
return "image/svg+xml";
}
if (extension == ".png") {
return "image/png";
}
if (extension == ".jpg" || extension == ".jpeg") {
return "image/jpeg";
}
if (extension == ".gif") {
return "image/gif";
}
if (extension == ".ico") {
return "image/x-icon";
}
return "application/octet-stream";
}
} // namespace
StaticFileCache::Entry::Entry(std::string fullPath, std::string contentType,
size_t fileSize, int fileFd)
: fullPath(std::move(fullPath)), contentType(std::move(contentType)),
fileSize(fileSize), fileFd(fileFd) {}
StaticFileCache::Entry::~Entry() {
if (fileFd >= 0) {
::close(fileFd);
}
}
StaticFileCache::StaticFileCache() : root_(".") {}
void StaticFileCache::setRoot(std::string root) {
std::unique_lock<std::shared_mutex> lock(mutex_);
root_ = root.empty() ? "." : std::move(root);
entries_.clear();
}
std::shared_ptr<const StaticFileCache::Entry>
StaticFileCache::find(const std::string &requestPath) {
const std::string normalizedPath = normalizeRequestPath(requestPath);
if (normalizedPath.empty()) {
return nullptr;
}
{
std::shared_lock<std::shared_mutex> lock(mutex_);
const auto it = entries_.find(normalizedPath);
if (it != entries_.end()) {
return it->second;
}
}
std::shared_ptr<Entry> loaded = loadEntry(normalizedPath);
if (loaded == nullptr) {
return nullptr;
}
std::unique_lock<std::shared_mutex> lock(mutex_);
const auto [it, inserted] =
entries_.emplace(normalizedPath, std::move(loaded));
return it->second;
}
std::string
StaticFileCache::normalizeRequestPath(const std::string &requestPath) const {
std::string normalizedPath;
size_t segmentStart = requestPath.empty() ? 0 : 1;
for (;;) {
const size_t segmentEnd = requestPath.find('/', segmentStart);
const size_t length =
(segmentEnd == std::string::npos ? requestPath.size() : segmentEnd) -
segmentStart;
const std::string_view segment(requestPath.data() + segmentStart, length);
if (!segment.empty() && segment != ".") {
if (segment == "..") {
return {};
}
if (!normalizedPath.empty()) {
normalizedPath.push_back('/');
}
normalizedPath.append(segment.data(), segment.size());
}
if (segmentEnd == std::string::npos) {
break;
}
segmentStart = segmentEnd + 1;
}
if (normalizedPath.empty()) {
normalizedPath = "index.html";
}
return normalizedPath;
}
std::shared_ptr<StaticFileCache::Entry>
StaticFileCache::loadEntry(const std::string &normalizedPath) {
std::string root;
{
std::shared_lock<std::shared_mutex> lock(mutex_);
root = root_;
}
const std::string fullPath = root + "/" + normalizedPath;
const int fileFd = ::open(fullPath.c_str(), O_RDONLY | O_CLOEXEC);
if (fileFd < 0) {
return nullptr;
}
struct stat statBuffer;
if (::fstat(fileFd, &statBuffer) < 0 || !S_ISREG(statBuffer.st_mode)) {
::close(fileFd);
return nullptr;
}
return std::make_shared<Entry>(fullPath, detectContentType(fullPath),
static_cast<size_t>(statBuffer.st_size),
fileFd);
}
} // namespace kback