/
main.cpp
313 lines (251 loc) · 7.51 KB
/
main.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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/* Simple Web Server */
/* Sonny Lazuardi Hermawan - 13511029 */
/* Muhammad Harits SAHE - 13511046 */
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <sstream>
#include <stdlib.h>
#include <ev++.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <resolv.h>
#include <errno.h>
#include <list>
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
/* Global variable */
string file_content;
bool file_status;
/* Variable konfigurasi (default) */
string file_conf = "config.txt";
int conf_port = 8200;
string conf_file_dir = "/home/sonny/Documents/PAT/";
/* method untuk membaca file, konten dari file akan masuk di global var file_content */
void read_file (const string filename) {
string line;
ifstream myfile (filename.c_str());
file_content = "";
file_status = false;
if (myfile.is_open())
{
file_status = true;
while ( getline (myfile,line) )
{
file_content += line;
file_content += "\n";
}
myfile.close();
}
else cout << "Unable to open file : " << filename << endl;
}
/* string tokenizer, pisahkan dengan karakter tertentu kedalam vector of string */
vector<string> split(const char *str, char c = ' ')
{
vector<string> result;
do
{
const char *begin = str;
while(*str != c && *str)
str++;
result.push_back(string(begin, str));
} while (0 != *str++);
return result;
}
void read_conf() {
read_file(file_conf);
vector<string> conf_line = split(file_content.c_str(), '\n');
conf_file_dir = conf_line[0];
conf_port = atoi(conf_line[1].c_str());
}
/* Kelas Buffer, untuk buffer output dan ditulis secara asinkronus */
struct Buffer {
char *data;
ssize_t len;
ssize_t pos;
Buffer(const char *bytes, ssize_t nbytes) {
pos = 0;
len = nbytes;
data = new char[nbytes];
memcpy(data, bytes, nbytes);
}
virtual ~Buffer() {
delete [] data;
}
char *dpos() {
return data + pos;
}
ssize_t nbytes() {
return len - pos;
}
};
/* Sebuah single instance untuk non-blocking echo handler */
class EchoInstance {
private:
ev::io io;
int sfd;
/* Buffer yang dipending untuk ditulis */
std::list<Buffer*> write_queue;
/* Callback umum */
void callback(ev::io &watcher, int revents) {
if (EV_ERROR & revents) {
perror("got invalid event");
return;
}
if (revents & EV_READ)
read_cb(watcher);
if (revents & EV_WRITE)
write_cb(watcher);
if (write_queue.empty()) {
io.set(ev::READ);
} else {
io.set(ev::READ|ev::WRITE);
}
}
/* Socket dapat ditulis */
void write_cb(ev::io &watcher) {
if (write_queue.empty()) {
io.set(ev::READ);
return;
}
Buffer* buffer = write_queue.front();
ssize_t written = write(watcher.fd, buffer->dpos(), buffer->nbytes());
if (written < 0) {
perror("read error");
return;
}
buffer->pos += written;
if (buffer->nbytes() == 0) {
write_queue.pop_front();
delete buffer;
}
/* Tutup dan bebaskan watcher saat selesai mengirim response (stateless) */
io.stop();
close(sfd);
}
/* Menerima pesan dari socket client */
void read_cb(ev::io &watcher) {
char buffer[1024];
ssize_t nread = recv(watcher.fd, buffer, sizeof(buffer), 0);
if (nread < 0) {
perror("read error");
return;
}
if (nread == 0) {
delete this;
} else {
vector<string> pisah = split(buffer, '\n');
vector<string> pisah2 = split(pisah[0].c_str(), ' ');
string current_url = pisah2[1];
current_url.erase(current_url.begin());
if (current_url == "") {
current_url = "index.html";
}
string file_path = conf_file_dir + current_url;
read_file(file_path);
string content = file_content;
string header = "";
/* Mengirim pesan berdasarkan status file */
if (file_status) {
header += "HTTP/1.1 200 OK\r\n";
header += "Content-Type: text/html\r\n";
header += "Accept-Ranges: bytes\r\n";
header += "Server: Apache/2.4.7 (Ubuntu)\r\n";
header += "Vary: Accept-Encoding\r\n";
header += "Content-Type: text/html\r\n";
header += "Content-Length: ";
stringstream ss;
ss << content.length()+1;
header += ss.str();
header += "\r\n";
header += "\r\n";
header += "\r\n";
} else {
content = "<h1>Not Found</h1>";
header += "HTTP/1.1 404 Not Found\r\n";
header += "Content-Type: text/html\r\n";
header += "Content-Length: ";
stringstream ss;
ss << content.length()+1;
header += ss.str();
header += "\r\n";
header += "\r\n";
header += "\r\n";
}
string balik = header + content;
write_queue.push_back(new Buffer(balik.c_str(), balik.length()));
}
}
/* Hancurkan dan tutup */
virtual ~EchoInstance() {
/* Tutup dan bebaskan watcher saat socket ditutup */
io.stop();
close(sfd);
}
public:
EchoInstance(int s) : sfd(s) {
fcntl(s, F_SETFL, fcntl(s, F_GETFL, 0) | O_NONBLOCK);
printf("Client terkoneksi\n");
io.set<EchoInstance, &EchoInstance::callback>(this);
io.start(s, ev::READ);
}
};
class EchoServer {
private:
ev::io io;
ev::sig sio;
int s;
public:
void io_accept(ev::io &watcher, int revents) {
if (EV_ERROR & revents) {
perror("event tidak valid");
return;
}
struct sockaddr_in client_addr;
socklen_t client_len = sizeof(client_addr);
int client_sd = accept(watcher.fd, (struct sockaddr *)&client_addr, &client_len);
if (client_sd < 0) {
perror("mendapatkan error");
return;
}
EchoInstance *client = new EchoInstance(client_sd);
}
static void signal_cb(ev::sig &signal, int revents) {
signal.loop.break_loop();
}
EchoServer(int port) {
printf("Mendengarkan pada port %d\n", port);
struct sockaddr_in addr;
s = socket(PF_INET, SOCK_STREAM, 0);
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = INADDR_ANY;
if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) != 0) {
perror("bind");
}
fcntl(s, F_SETFL, fcntl(s, F_GETFL, 0) | O_NONBLOCK);
listen(s, 5);
io.set<EchoServer, &EchoServer::io_accept>(this);
io.start(s, ev::READ);
sio.set<&EchoServer::signal_cb>();
sio.start(SIGINT);
}
virtual ~EchoServer() {
shutdown(s, SHUT_RDWR);
close(s);
}
};
/* Program Utama */
int main(int argc, char **argv)
{
read_conf();
if (argc > 1)
conf_port = atoi(argv[1]);
ev::default_loop loop;
EchoServer echo(conf_port);
/* Menjalankan loop server */
loop.run(0);
return 0;
}