-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathressourcemapper.cpp
More file actions
40 lines (38 loc) · 1.39 KB
/
Copy pathressourcemapper.cpp
File metadata and controls
40 lines (38 loc) · 1.39 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
#include "ressourcemapper.hpp"
using namespace std;
Response* RessourceMapper::get(Request &req){
Response * rep;
cmtx.lock();
rep = cache[req];
cmtx.unlock();
if(rep) return rep;
Ressource * rec;
mtx.lock();
rec = rec_mapping[req.ressource];
mtx.unlock();
if (rec != NULL) return rec->buildResp(req);
std::filesystem::path rec_path(ressource_path + req.ressource);
if(std::filesystem::is_regular_file(rec_path) || filesystem::is_symlink(rec_path)){
Ressource *rec= new StaticRessource(ressource_path + req.ressource);
add_mapping(req.ressource, rec);
if (rec->cacheable){
cache.insert(std::make_pair(Request(req), rec->buildResp(req)));
return cache[req];
}
return rec->buildResp(req);
}
else return new NotFound();
}
void RessourceMapper::add_mapping(const string &identifier, Ressource * rec){
lock_guard<mutex> l(mtx);
rec_mapping[identifier] = rec;
}
RessourceMapper::RessourceMapper(std::string ressourcepath){
ressource_path = ressourcepath;
std::filesystem::path indexpath(ressource_path + "/index.html");
if(std::filesystem::is_regular_file(indexpath) || filesystem::is_symlink(indexpath)){
StaticRessource *index = new StaticRessource(ressource_path + "/index.html");
add_mapping("/", index);
add_mapping("/index.html", index);
}
}