forked from RipcordSoftware/libhttpserver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmime_types.cpp
More file actions
90 lines (77 loc) · 3.35 KB
/
Copy pathmime_types.cpp
File metadata and controls
90 lines (77 loc) · 3.35 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
#include "mime_types.h"
std::vector<rs::httpserver::MimeTypes::Item> rs::httpserver::MimeTypes::items_ = {
{ ".txt", "text/plain" },
{ ".htm", "text/html" },
{ ".html", "text/html" },
{ ".css", "text/css" },
{ ".js", "text/javascript" },
{ ".csv", "text/csv" },
{ ".rtf", "text/rtf" },
{ ".xml", "text/xml" },
{ ".png", "image/png", false },
{ ".jpg", "image/jpg", false },
{ ".jpeg", "image/jpg", false },
{ ".gif", "image/gif", false },
{ ".ico", "image/ico" },
{ ".woff", "application/font-woff", false },
{ ".woff2", "application/font-woff2", false },
{ ".eot", "application/vnd.ms-fontobject", false },
{ ".ttf", "application/x-font-ttf" },
{ ".otf", "application/x-font-opentype" },
{ ".svg", "image/svg+xml" },
{ ".json", "application/json" },
{ ".pdf", "application/pdf", false },
{ ".ps", "application/postscript" },
{ ".zip", "application/zip", false },
{ ".gz", "application/gzip", false }
};
rs::httpserver::MimeTypes::item_lookup_type rs::httpserver::MimeTypes::itemLookup_(rs::httpserver::MimeTypes::items_.cbegin(), rs::httpserver::MimeTypes::items_.cend());
rs::httpserver::MimeTypes::extension_lookup_type rs::httpserver::MimeTypes::extnLookup_(rs::httpserver::MimeTypes::items_.cbegin(), rs::httpserver::MimeTypes::items_.cend());
boost::optional<const rs::httpserver::MimeTypes::Item&> rs::httpserver::MimeTypes::GetExtensionType(const char* extn) {
return GetExtensionType(std::string(extn));
}
boost::optional<const rs::httpserver::MimeTypes::Item&> rs::httpserver::MimeTypes::GetContentType(const char* type) {
return GetContentType(std::string(type));
}
boost::optional<const rs::httpserver::MimeTypes::Item&> rs::httpserver::MimeTypes::GetExtensionType(const std::string& extn) {
if (extn.length() > 0) {
if (extn[0] != '.') {
auto index = extn.rfind(".");
if (index != std::string::npos) {
return GetExtensionType(std::string(std::string(&extn[index])));
}
}
auto iter = extnLookup_.find(Item{extn, ""});
if (iter != extnLookup_.cend()) {
return *iter;
}
}
return boost::optional<const Item&>();
}
boost::optional<const rs::httpserver::MimeTypes::Item&> rs::httpserver::MimeTypes::GetContentType(const std::string& type) {
auto iter = itemLookup_.find(Item{"", type});
if (iter != itemLookup_.cend()) {
return *iter;
}
return boost::optional<const Item&>();
}
boost::optional<const rs::httpserver::MimeTypes::Item&> rs::httpserver::MimeTypes::GetExtensionType(const boost::filesystem::path& path) {
auto extn = path.extension();
if (!extn.empty()) {
return GetExtensionType(std::string(path.c_str()));
} else {
return boost::optional<const Item&>();
}
}
bool rs::httpserver::MimeTypes::AddType(const std::string& extn, const std::string& type, bool compress) {
itemLookup_.erase(Item{"", type});
extnLookup_.erase(Item{extn, ""});
return extnLookup_.emplace(extn, type, compress).second && itemLookup_.emplace(extn, type, compress).second;
}
void rs::httpserver::MimeTypes::RemoveExtensionType(const std::string& extn) {
auto item = GetExtensionType(extn);
if (item.is_initialized()) {
itemLookup_.erase(item.get());
extnLookup_.erase(item.get());
}
}