-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
204 lines (182 loc) · 5.35 KB
/
Copy pathmain.cpp
File metadata and controls
204 lines (182 loc) · 5.35 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include <iostream>
#include <map>
#include <list>
#include <semaphore.h>
#include <sys/types.h>
#include <sys/sendfile.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <ev.h>
#include <thread>
#include <mutex>
int set_nonblock(int fd) {
int flags;
#if defined(O_NONBLOCK)
if (-1 == (flags = fcntl(fd, F_GETFL, 0)))
flags = 0;
return fcntl(fd, F_SETFL, flags | O_NONBLOCK);
#else
flags = 1;
return ioctl(fd, FIONBIO, &flags);
#endif
}
std::mutex m;
void log_error(const char *place) {
m.lock();
FILE *log = fopen("/tmp/log", "a");
fprintf(log, "%s\n", strerror(errno));
fclose(log);
m.unlock();
}
void log(const char *msg) {
m.lock();
FILE *log = fopen("/tmp/log", "a");
fprintf(log, "%s\n", msg);
fclose(log);
m.unlock();
}
// Arguments
char *host = 0, *port = 0, *dir = 0;
void extract_path_from_http_get_request(std::string& path, const char* buf, ssize_t len)
{
std::string request(buf, len);
std::string s1(" ");
std::string s2("?");
// "GET "
std::size_t pos1 = 4;
std::size_t pos2 = request.find(s2, 4);
if (pos2 == std::string::npos)
{
pos2 = request.find(s1, 4);
}
path = request.substr(4, pos2 - 4);
}
void process_slave_socket(int slave_socket) {
char buf[1024];
ssize_t recv_ret = recv(slave_socket, buf, sizeof(buf), MSG_NOSIGNAL);
if (recv_ret < 0) {
log_error("recv");
close(slave_socket);
return;
}
if (recv_ret == 0) {
close(slave_socket);
return;
}
buf[recv_ret]=0;
// process http request, extract file path
std::string path;
extract_path_from_http_get_request(path, buf, recv_ret);
// if path exists open and read file
std::string full_path = std::string(dir) + path;
char reply[1024];
log(full_path.c_str());
if (path.length() && path != "/" && access(full_path.c_str(), F_OK) != -1) {
// file exists, get its size
int fd = open(full_path.c_str(), O_RDONLY);
int sz = lseek(fd, 0, SEEK_END);
sprintf(reply, "HTTP/1.1 200 OK\r\n"
"Content-Type: text/html\r\n"
"Content-length: %d\r\n"
"Connection: close\r\n"
"\r\n", sz);
ssize_t send_ret = send(slave_socket, reply, strlen(reply), MSG_NOSIGNAL);
off_t offset = 0;
while (offset < sz) {
int s = sendfile(slave_socket, fd, &offset, sz - offset);
}
close(fd);
} else {
strcpy(reply, "HTTP/1.1 404 Not Found\r\n"
"Content-Type: text/html\r\n"
"Content-length: 0\r\n"
"Connection: close\r\n"
"\r\n");
ssize_t send_ret = send(slave_socket, reply, strlen(reply), MSG_NOSIGNAL);
}
shutdown(slave_socket, SHUT_WR);
while(recv(slave_socket, buf, sizeof(buf), MSG_NOSIGNAL) > 0);
close(slave_socket);
}
void slave_send_to_worker(struct ev_loop *loop, struct ev_io *w, int revents) {
int slave_socket = w->fd;
ev_io_stop(loop, w);
std::thread t(process_slave_socket, slave_socket);
t.detach();
}
void master_accept_connection(struct ev_loop *loop, struct ev_io *w, int revents) {
// create slave socket
int slave_socket = accept(w->fd, 0, 0);
if (slave_socket == -1) {
log_error("accept");
exit(3);
}
set_nonblock(slave_socket);
struct ev_io *slave_watcher = new ev_io;
ev_init(slave_watcher, slave_send_to_worker);
ev_io_set(slave_watcher, slave_socket, EV_READ);
ev_io_start(loop, slave_watcher);
}
int main(int argc, char* argv[]) {
log("-----------------------------------------");
if (daemon(0, 0) == -1) {
log_error("daemon");
exit(1);
}
int opt;
while ((opt = getopt(argc, argv, "h:p:d:")) != -1) {
switch(opt) {
case 'h':
host = optarg;
break;
case 'p':
port = optarg;
break;
case 'd':
dir = optarg;
break;
default:
printf("Usage: %s -h <host> -p <port> -d <folder>\n", argv[0]);
exit(1);
}
}
if (host == 0 || port == 0 || dir == 0) {
printf("Usage: %s -h <host> -p <port> -d <folder>\n", argv[0]);
exit(1);
}
struct ev_loop *loop = ev_default_loop(EVFLAG_FORKCHECK);
int master_socket = socket(AF_INET, SOCK_STREAM, 0);
if (master_socket == -1) {
log_error("master_socket");
exit(1);
}
set_nonblock(master_socket);
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(atoi(port));
if (inet_pton(AF_INET, host, &(addr.sin_addr.s_addr)) != 1) {
log_error("inet_aton");
exit(2);
}
if (bind(master_socket, (struct sockaddr* )&addr, sizeof(addr)) == -1) {
log_error("bind");
exit(3);
}
listen(master_socket, SOMAXCONN);
struct ev_io master_watcher;
ev_init (&master_watcher, master_accept_connection);
ev_io_set(&master_watcher, master_socket, EV_READ);
ev_io_start(loop, &master_watcher);
ev_loop(loop, 0);
close(master_socket);
return 0;
}