-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebserv.cpp
More file actions
106 lines (90 loc) · 2.74 KB
/
webserv.cpp
File metadata and controls
106 lines (90 loc) · 2.74 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
#include "src/Multiplexing.hpp"
#include "src/Connection.hpp"
#include "src/EventLoop.hpp"
#include "src/Config.hpp"
#include "src/Logger.hpp"
#include "src/Utils.hpp"
#include "src/Date.hpp"
#include <Regex.hpp>
#include <fcntl.h>
static void check_necessary_configuration()
{
DIR *dir = NULL;
dir = opendir("./tmp");
if (dir == NULL)
{
if (mkdir("./tmp", 0755) < 0)
throw std::runtime_error("mkdir: failed to create 'tmp' directory");
}
else
closedir(dir);
dir = opendir("./log");
if (dir == NULL)
{
if (mkdir("./log", 0755) < 0)
throw std::runtime_error("mkdir: failed to create 'log' directory");
}
else
closedir(dir);
we::Logger::get_instance().setAccessLog("./log/access.log");
we::Logger::get_instance().setErrorLog("./log/error.log");
}
int main(int ac, char **av)
{
signal(SIGPIPE, SIG_IGN);
we::Config config;
we::EventLoop eventLoop;
we::AMultiplexing *multiplexer = NULL;
try
{
if (ac > 2)
throw std::runtime_error("Usage: " + std::string(av[0]) + " [config_file]");
if (ac == 2)
we::load_config(av[1], config);
else
we::load_config("./conf/default.conf", config);
check_necessary_configuration();
multiplexer = we::get_instance(config.multiplex_type);
if (multiplexer == NULL)
throw std::runtime_error("Could not create multiplexer");
we::Config::server_block_const_iterator it = config.server_blocks.begin();
while (it != config.server_blocks.end())
{
multiplexer->add(it->first, NULL, we::AMultiplexing::Read);
++it;
}
while (1)
{
multiplexer->wait(eventLoop.get_next_activation_time());
int fd = -1;
while ((fd = multiplexer->get_next_fd()) != -1)
{
we::BaseConnection *connection = multiplexer->get_connection(fd);
try
{
if (connection == NULL)
new we::Connection(fd, eventLoop, config, *multiplexer);
else
connection->handle_connection();
}
catch (...)
{
if (connection)
delete connection;
connection = NULL;
}
}
eventLoop.run();
}
delete multiplexer;
return 0;
}
catch (std::exception const &e)
{
if (multiplexer)
delete multiplexer;
std::cerr << e.what() << std::endl;
we::Logger::get_instance().error(NULL, "critical", e.what());
return 1;
}
}