-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathcpp.cpp
97 lines (87 loc) · 2.27 KB
/
cpp.cpp
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
#include <unistd.h>
#include <dlfcn.h>
#include "utils.hpp"
#include "cpp.hpp"
namespace hi
{
/*
cpp::cb_t::cb_t(const std::string &path) : m(0), create(0), destroy(0)
{
this->m = dlopen(path.c_str(), RTLD_NOW);
if (this->m)
{
this->create = (create_t *)dlsym(m, "create");
this->destroy = (destroy_t *)dlsym(m, "destroy");
}
}
cpp::cb_t::~cb_t()
{
if (this->m)
{
dlclose(this->m);
}
}
servlet *cpp::cb_t::make_obj()
{
return this->create ? this->create() : NULL;
}
void cpp::cb_t::free(servlet *obj)
{
this->destroy(obj);
}
void cpp::cb_t::main(const request &req, response &res)
{
servlet *obj = this->make_obj();
if (obj)
{
obj->handler(req, res);
this->free(obj);
}
}
*/
cpp::cpp() : path(), modules() {}
cpp::cpp(const std::vector<std::string> &p) : path(), modules()
{
for (const auto &item : p)
{
this->path.push_back(item);
}
}
cpp::~cpp() {}
void cpp::main(const request &req, response &res)
{
std::string key = std::move(hi::md5(req.uri));
if (this->modules.find(key) == this->modules.end())
{
size_t p = req.uri.find_last_of('.');
std::string m_str;
if (p != std::string::npos)
{
m_str = std::move(req.uri.substr(0, p));
}
else
{
m_str = req.uri;
}
for (auto &item : this->path)
{
std::string module_file = std::move(item + "/" + m_str + ".so");
if (is_file(module_file))
{
std::shared_ptr<module<servlet>> cb = std::make_shared<module<servlet>>(module_file);
if (cb)
{
cb->main(req, res);
this->modules[key] = std::move(cb);
}
break;
}
}
}
else
{
std::shared_ptr<module<servlet>> cb = this->modules[key];
cb->main(req, res);
}
}
} // namespace anybyte