-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
145 lines (120 loc) · 3.04 KB
/
Copy pathmain.cpp
File metadata and controls
145 lines (120 loc) · 3.04 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
#include "http_server.h"
#include "http_headers.h"
#include "http_content_type.h"
#include <iostream>
#include <sstream>
#include <mutex>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <syslog.h>
static void skeleton_daemon()
{
pid_t pid;
/* Fork off the parent process */
pid = fork();
/* An error occurred */
if (pid < 0)
exit(EXIT_FAILURE);
/* Success: Let the parent terminate */
if (pid > 0)
exit(EXIT_SUCCESS);
/* On success: The child process becomes session leader */
if (setsid() < 0)
exit(EXIT_FAILURE);
/* Catch, ignore and handle signals */
//TODO: Implement a working signal handler */
signal(SIGCHLD, SIG_IGN);
signal(SIGHUP, SIG_IGN);
/* Fork off for the second time*/
pid = fork();
/* An error occurred */
if (pid < 0)
exit(EXIT_FAILURE);
/* Success: Let the parent terminate */
if (pid > 0)
exit(EXIT_SUCCESS);
/* Set new file permissions */
umask(0);
/* Change the working directory to the root directory */
/* or another appropriated directory */
chdir("/");
/* Close all open file descriptors */
int x;
for (x = sysconf(_SC_OPEN_MAX); x>0; x--)
{
close(x);
}
/* Open the log file */
openlog("firstdaemon", LOG_PID, LOG_DAEMON);
}
int main(int argc, char *argv[])
{
std::string SrvAddress;
std::string RootDir;
std::uint16_t SrvPort;
int opt = 0;
while ((opt = getopt(argc, argv, "h:p:d:")) != -1) {
switch (opt) {
case 'h':
SrvAddress.assign(optarg);
break;
case 'p':
SrvPort = atoi(optarg);
break;
case 'd':
RootDir.assign(optarg);
break;
default: /* '?' */
fprintf(stderr, "Usage: %s -h <ip> -p <port> -d <directory>\n",
argv[0]);
exit(EXIT_FAILURE);
}
}
skeleton_daemon();
while (1)
{
//TODO: Insert daemon code here.
syslog(LOG_NOTICE, "First daemon started.");
std::uint16_t SrvThreadCount = 4;
std::string const DefaultPage = "index.html";
std::mutex Mtx;
try
{
using namespace Network;
HttpServer Srv(SrvAddress, SrvPort, SrvThreadCount,
[&](IHttpRequestPtr req)
{
std::string Path = req->GetPath();
Path = RootDir + Path + (Path == "/" ? DefaultPage : std::string());
{
std::stringstream Io;
Io << "Path: " << Path << std::endl
<< Http::Request::Header::Host::Name << ": "
<< req->GetHeaderAttr(Http::Request::Header::Host::Value) << std::endl
<< Http::Request::Header::Referer::Name << ": "
<< req->GetHeaderAttr(Http::Request::Header::Referer::Value) << std::endl;
std::lock_guard<std::mutex> Lock(Mtx);
std::cout << Io.str() << std::endl;
}
req->SetResponseAttr(Http::Response::Header::Server::Value, "MyTestServer");
req->SetResponseAttr(Http::Response::Header::ContentType::Value,
Http::Content::TypeFromFileName(Path));
req->SetResponseFile(Path);
});
std::cin.get();
}
catch (std::exception const &e)
{
std::cout << e.what() << std::endl;
}
sleep(20);
break;
}
syslog(LOG_NOTICE, "First daemon terminated.");
closelog();
return EXIT_SUCCESS;
}