-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.cpp
395 lines (331 loc) · 13.4 KB
/
server.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
#include "server.h"
#include <iostream>
#include <unistd.h>
#include <sstream>
#include "threadPool.h"
#include "utils.h"
#include <cstring>
#include <sys/stat.h>
#include <fstream>
#include <algorithm>
namespace http
{
Server::Server(std::vector<std::string> ips, std::string servIp, int port, ThreadPool *pool) : servPort(port),
servSockAddrLen(sizeof(servSockAddr)), allyServers(ips), pool(pool)
{
servSockAddr.sin_family = AF_INET;
servSockAddr.sin_port = htons(port);
servSockAddr.sin_addr.s_addr = inet_addr(servIp.c_str());
memset(servSockAddr.sin_zero, '\0', sizeof servSockAddr.sin_zero);
servSockAddrLen = sizeof(servSockAddr);
std::vector<sockaddr_in> sockAddrs;
for (std::string &ip : ips)
{
std::cout << "Friend server ip " << ip << std::endl;
sockaddr_in sockaddr;
sockaddr.sin_family = AF_INET;
sockaddr.sin_port = htons(8080);
sockaddr.sin_addr.s_addr = inet_addr(ip.c_str());
memset(sockaddr.sin_zero, '\0', sizeof sockaddr.sin_zero);
sockAddrs.push_back(sockaddr);
int sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0)
erroredExit("Couldn't open a socket");
const int enable = 1;
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int)) < 0)
erroredExit("Couldn't set SO_REUSEADDR");
if (setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, &enable, sizeof(int)) < 0)
erroredExit("Couldn't set SO_REUSEPORT");
int res = connect(sock, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
if (res == 0)
{
std::cout << "Server connected successfully " << std::endl;
interserverSockets.push_back(sock);
}
else
{
std::cout << "Error: " << errno << std::endl;
}
}
}
Server::~Server()
{
close(servSock);
exit(0);
}
std::string Server::sendMessageAndGetResponce(std::string ip, SubServerMessage message, std::string resourceName)
{
// std::ostream msg;
return "";
}
int Server::runSocket(std::string servIp, int port, sockaddr_in &servSockAddr, unsigned int &servSockAddrLen, __socket_type sockType)
{
memset(&servSockAddr, '\0', sizeof(sockaddr_in));
servSockAddr.sin_family = AF_INET;
servSockAddr.sin_port = htons(port);
servSockAddr.sin_addr.s_addr = inet_addr(servIp.c_str());
int servSock = socket(AF_INET, sockType, 0);
if (servSock < 0)
erroredExit("Couldn't open a socket");
const int enable = 1;
if (setsockopt(servSock, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int)) < 0)
erroredExit("Couldn't set SO_REUSEADDR");
if (setsockopt(servSock, SOL_SOCKET, SO_REUSEPORT, &enable, sizeof(int)) < 0)
erroredExit("Couldn't set SO_REUSEPORT");
if (bind(servSock, (sockaddr *)&servSockAddr, servSockAddrLen) < 0)
erroredExit("Couldn't bind the socket to address");
return servSock;
}
void Server::startListen()
{
if (listen(servSock, 20) < 0)
erroredExit("Could not listen on the socket");
log("Listening...");
while (true)
{
int connSock = acceptConnection();
pool->enqueueTask([this, &connSock]
{
std::cout << "Task enqueued" << std::endl;
std::thread thr(&Server::handleUserConnection, this, std::ref(connSock));
thr.join();
close(connSock);
std::cout << "Task completed" << std::endl; });
}
}
void Server::handleUserConnection(int &connSock)
{
fd_set sock;
FD_ZERO(&sock);
FD_SET(connSock, &sock);
while (true)
{
fd_set s = sock;
int sel = select(connSock + 1, &s, 0, 0, 0);
if (sel < 0)
erroredExit("Select broke");
int bytesRecieved;
char buffer[BUFFER_SIZE] = {'\0'};
bytesRecieved = read(connSock, buffer, BUFFER_SIZE);
if (bytesRecieved < 0)
erroredExit("Could not recieve bytes from a remote socket");
else if (bytesRecieved == 0)
{
log("Client disconnected");
close(connSock);
break;
}
else // bytesRecieved > 0
{
std::string str(buffer);
auto tokens = getReqTokens(str);
std::string method;
std::string url;
for (int i = 0; i < tokens.size(); i++)
{
std::string s = tokens[i];
if (s == "GET" || s == "POST" || s == "DELETE" || s == "UPDATE")
{
method = s;
url = tokens[i + 1];
if (url[url.length() - 1] == '/')
url.erase(url.length() - 1);
}
}
log("Recieved content:");
log(str);
std::cout << "METHOD AND URL" << method << url;
sendResponse(connSock, method, url, tokens);
}
}
}
int Server::acceptConnection()
{
int connSock = accept(servSock, (sockaddr *)&servSockAddr, &servSockAddrLen);
if (connSock < 0)
erroredExit("Could not accept connection");
return connSock;
}
void Server::sendResponse(int &connSock, const std::string &requestMethod, const std::string &requestURI, std::vector<std::string> tokens)
{
long bytesSent;
std::ostringstream response;
if (requestMethod == "GET")
{
getFile(response, requestURI, tokens);
}
else if (requestMethod == "POST")
{
std::string body = getReqBody(tokens);
postFile(response, requestURI, body);
}
else if (requestMethod == "DELETE")
{
}
else if (requestMethod == "UPDATE")
{
}
else
{
response << "HTTP/1.1 405 Method Not Allowed\r\n"
<< "Content-Length: 0\r\n"
<< "Allow: GET, POST, DELETE, UPDATE\r\n"
<< "Connection: close\r\n"
<< "\r\n";
}
bytesSent = write(connSock, response.str().c_str(), response.str().size());
if (bytesSent == response.str().size())
log("Sent the response successfully");
else
log("Error sending response to client");
}
void Server::getFile(std::ostringstream &response, const std::string &requestURI, std::vector<std::string> tokens)
{
std::string filePath = "." + requestURI;
struct stat fileStat;
if (stat(filePath.c_str(), &fileStat) == 0 && S_ISREG(fileStat.st_mode))
{
std::ifstream file(filePath, std::ios::binary);
if (file)
{
std::ostringstream fileContent;
fileContent << file.rdbuf();
std::string content = fileContent.str();
response << "HTTP/1.1 200 OK\r\n"
<< "Content-Type: " << getContentType(filePath) << "\r\n"
<< "Content-Length: " << content.size() << "\r\n"
<< "Last-Modified: " << getLastModifiedTime(filePath) << "\r\n"
<< "Connection: close\r\n"
<< "\r\n"
<< content;
}
}
else
{
if (std::find(tokens.begin(), tokens.end(), "Internal") != tokens.end())
{
std::string errorMessage = "File not found: " + requestURI;
response << "HTTP/1.1 404 Not Found\r\n"
<< "Content-Type: text/plain\r\n"
<< "Content-Length: " << errorMessage.size() << "\r\n"
<< "Connection: close\r\n"
<< "Internal: " << requestURI << "\r\n"
<< "\r\n"
<< errorMessage;
}
else
{
std::string content = "";
for (int i = 0; i < interserverSockets.size(); i++)
{
std::ostringstream interserverReq;
interserverReq << "GET " << requestURI << " HTTP/1.1" << "\r\n"
<< "Content-Type: text/plain" << "\r\n"
<< "Accept: */*" << "\r\n"
<< "Host : " << allyServers[i] << ":8080" << "\r\n"
<< "Connection: keep-alive" << "\r\n"
<< "Internal : hi" << "\r\n\r\n";
int bytesSent = write(interserverSockets[i], interserverReq.str().c_str(), interserverReq.str().size());
if (bytesSent == interserverReq.str().size())
log("Sent the response successfully");
else
log("Error sending response to client");
char buffer[BUFFER_SIZE] = {'\0'};
int bytesRecieved = read(interserverSockets[i], buffer, BUFFER_SIZE);
if (bytesRecieved > 0)
{
std::string str(buffer);
auto tokens = getReqTokens(str);
auto body = getReqBody(tokens);
if (body != "")
{
content = body;
}
}
}
if (content != "")
{
response << "HTTP/1.1 200 OK\r\n"
<< "Content-Type: " << getContentType(filePath) << "\r\n"
<< "Content-Length: " << content.size() << "\r\n"
<< "Last-Modified: " << getLastModifiedTime(filePath) << "\r\n"
<< "Connection: close\r\n"
<< "\r\n"
<< content;
}
else
{
std::string errorMessage = "File not found: " + requestURI;
response << "HTTP/1.1 404 Not Found\r\n"
<< "Content-Type: text/plain\r\n"
<< "Content-Length: " << errorMessage.size() << "\r\n"
<< "Connection: close\r\n"
<< "Internal: " << requestURI << "\r\n"
<< "\r\n"
<< errorMessage;
}
}
}
}
std::string Server::getContentType(const std::string &fileName)
{
std::string extension = fileName.substr(fileName.find_last_of('.') + 1);
if (extension == "html" || extension == "htm")
return "text/html";
else if (extension == "txt")
return "text/plain";
else if (extension == "jpg" || extension == "jpeg")
return "image/jpeg";
else if (extension == "png")
return "image/png";
else if (extension == "pdf")
return "application/pdf";
else
return "application/octet-stream";
}
std::string Server::getLastModifiedTime(const std::string &fileName)
{
struct stat fileStat;
if (stat(fileName.c_str(), &fileStat) != 0)
{
return "";
}
std::time_t lastModifiedTime = fileStat.st_mtime;
std::tm *tmPtr = std::gmtime(&lastModifiedTime);
char buf[80];
std::strftime(buf, 80, "%a, %d %b %Y %H:%M:%S GMT", tmPtr);
return buf;
}
void Server::postFile(std::ostringstream &response, const std::string &requestURI, const std::string &body)
{
std::string filePath = "." + requestURI;
if (filePath.empty())
{
formErrorResponse(400, "Bad Request", "Filename not found in URI", response);
return;
}
std::ofstream outFile(filePath);
if (!outFile.is_open())
{
formErrorResponse(500, "Internal Server Error", "Error opening an output file", response);
return;
}
outFile << body;
outFile.close();
response << "HTTP/1.1 200 OK\r\n"
<< "Content-Type: " << getContentType(filePath) << "\r\n"
<< "Content-Length: " << 0 << "\r\n"
<< "Last-Modified: " << getLastModifiedTime(filePath) << "\r\n"
<< "Connection: close\r\n"
<< "\r\n";
};
void Server::formErrorResponse(int statusCode, const std::string &message, const std::string &statusText, std::ostringstream &response)
{
response << "HTTP/1.1 " << statusCode << " " << statusText << "\r\n"
<< "Content-Type: text/plain\r\n"
<< "Content-Length: " << message.size() << "\r\n"
<< "Connection: close\r\n"
<< "\r\n"
<< message;
}
}