-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
187 lines (153 loc) · 4.09 KB
/
main.cpp
File metadata and controls
187 lines (153 loc) · 4.09 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include <boost/asio.hpp>
#include <boost/filesystem.hpp>
#include <boost/filesystem/fstream.hpp>
#include <cstdlib>
#include <getopt.h>
#include <iostream>
#include <memory>
#include <sstream>
#include <thread>
#include <unistd.h>
#include <signal.h>
namespace asio = boost::asio;
namespace fs = boost::filesystem;
using boost::asio::ip::tcp;
namespace
{
const int msgLength = 1024;
typedef std::shared_ptr<tcp::socket> SocketPtr;
std::string getPath(const std::string& request)
{
std::istringstream sIn(request);
std::string getWord, body;
sIn >> getWord >> body;
size_t paramsPos = body.find_first_of("?");
std::string dir = body.substr(0, paramsPos);
dir.erase(dir.begin());
return dir;
}
std::string readFile(const fs::path& p)
{
size_t len = fs::file_size(p);
std::string res;
res.resize(len, ' ');
fs::ifstream fIn(p);
fIn.read(&res[0], len);
return res;
}
struct Session
{
Session(SocketPtr s)
: s(s)
{}
void operator()()
{
try
{
char buf[msgLength];
for (;;) {
boost::system::error_code err;
size_t len = s->read_some(asio::buffer(buf), err);
if (err == asio::error::eof)
break;
else if (err)
throw boost::system::system_error(err);
std::string filePath = getPath(buf);
std::string response = getResponse(filePath);
//std::cerr << response << std::endl;
asio::write(*s, asio::buffer(response.c_str(), response.size()));
}
}
catch (std::exception& e)
{
}
}
static std::string getResponse(const fs::path& filePath)
{
if (!fs::exists(filePath) || !fs::is_regular_file(filePath))
return notFound();
std::string html = readFile(filePath);
std::ostringstream sOut;
sOut << "HTTP/1.0 200 OK" << std::endl
<< "Content-length:" << html.size() << std::endl
<< "Content-Type: text/html" << std::endl
<< std::endl << html;
return sOut.str();
}
static std::string notFound()
{
std::ostringstream sOut;
sOut << "HTTP/1.0 404 NOT FOUND" << std::endl
<< "Content-length:" << 0 << std::endl
<< "Content-Type: text/html" << std::endl << std::endl;
return sOut.str();
}
private :
SocketPtr s;
};
struct Server
{
void run(asio::io_service& io_service, const std::string& ip, unsigned short port)
{
tcp::acceptor a(io_service, tcp::endpoint(/*tcp::v4()*/asio::ip::address::from_string(ip), port));
for (;;) {
SocketPtr s(new tcp::socket(io_service));
a.accept(*s);
Session session(s);
std::thread t(session);
t.detach();
}
}
};
void summonDaemon()
{
pid_t pid = fork();
if (pid < 0)
exit(1);
if (pid > 0)
exit(0);
if (setsid() < 0)
exit(1);
signal(SIGCHLD, SIG_IGN);
signal(SIGHUP, SIG_IGN);
pid = fork();
if (pid < 0)
exit(1);
if (pid > 0)
exit(0);
umask(0);
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
}
} // namespace
int main(int argc, char **argv)
{
summonDaemon();
unsigned short port = 5001;
std::string ip("127.0.0.1");
std::string dir("1");
int switchName;
while ((switchName = getopt(argc, argv, "h:p:d:")) != -1) {
switch(switchName) {
case 'h':
ip = std::string(optarg);
break;
case 'p':
port = std::atoi(optarg);
break;
case 'd':
dir = std::string(optarg);
break;
default: {
std::cerr << "Unknown cmd arg" << std::endl;
}
}
}
fs::path cur_path(dir);
fs::current_path(cur_path);
asio::io_service service;
Server server;
server.run(service, ip, port);
return 0;
}