Skip to content

Commit cb95968

Browse files
committed
feat: enchanced exceptions - unix socket
1 parent e107f39 commit cb95968

File tree

3 files changed

+100
-159
lines changed

3 files changed

+100
-159
lines changed

include/base_exceptions.hpp

+66-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace tpt
99
public:
1010
const char *what() const throw()
1111
{
12-
return "Error - file not found";
12+
return "File not found";
1313
}
1414
};
1515

@@ -18,7 +18,7 @@ namespace tpt
1818
public:
1919
const char *what() const throw()
2020
{
21-
return "Error - JSON parsing error";
21+
return "JSON parsing error";
2222
}
2323
};
2424

@@ -27,7 +27,70 @@ namespace tpt
2727
public:
2828
const char *what() const throw()
2929
{
30-
return "Error - IP is blacklisted";
30+
return "IP is blacklisted";
31+
}
32+
};
33+
34+
class SocketCreationException : public std::exception
35+
{
36+
public:
37+
const char *what() const throw()
38+
{
39+
return "Error when creating socket";
40+
}
41+
};
42+
43+
class SocketCloseException : public std::exception
44+
{
45+
public:
46+
const char *what() const throw()
47+
{
48+
return "Error closing socket";
49+
}
50+
};
51+
52+
class SocketBindingException : public std::exception
53+
{
54+
public:
55+
const char *what() const throw()
56+
{
57+
return "Error binding socket";
58+
}
59+
};
60+
61+
class SocketListenException : public std::exception
62+
{
63+
public:
64+
const char *what() const throw()
65+
{
66+
return "Error listening to connections";
67+
}
68+
};
69+
70+
class SocketAcceptException : public std::exception
71+
{
72+
public:
73+
const char *what() const throw()
74+
{
75+
return "Error accepting connections";
76+
}
77+
};
78+
79+
class SocketReceiveException : public std::exception
80+
{
81+
public:
82+
const char *what() const throw()
83+
{
84+
return "Error receiving data";
85+
}
86+
};
87+
88+
class SocketSendException : public std::exception
89+
{
90+
public:
91+
const char *what() const throw()
92+
{
93+
return "Error sending data";
3194
}
3295
};
3396
}

src/teapot.cpp

+16-37
Original file line numberDiff line numberDiff line change
@@ -180,40 +180,9 @@ void Teapot::requestHandler(SOCKET client_socket)
180180
this->socket.closeSocket(client_socket);
181181
}
182182

183-
Teapot::Teapot()
184-
{
185-
this->ip_address = "127.0.0.1";
186-
this->port = 8000;
187-
this->max_connections = 10;
188-
this->logging_type = DEFAULT;
189-
this->static_files_dir = "static";
190-
this->sanitizer_middleware = SanitizerMiddleware();
191-
this->cors_middleware = CORSMiddleware();
192-
this->security_middleware = SecurityMiddleware();
193-
this->logger = ConsoleLogger();
194-
#ifdef __linux__
195-
this->socket = tpt::UnixSocket(this->logger, this->port);
196-
#endif
197-
#ifdef _WIN32
198-
this->socket = tpt::WinSocket(this->port);
199-
#endif
200-
}
183+
Teapot::Teapot() : Teapot("127.0.0.1", 8000, 10, DEFAULT, "static") {}
201184

202-
Teapot::Teapot(unsigned int port)
203-
{
204-
this->ip_address = "127.0.0.1";
205-
this->port = port;
206-
this->max_connections = 10;
207-
this->logging_type = DEFAULT;
208-
this->static_files_dir = "static";
209-
this->logger = ConsoleLogger();
210-
#ifdef __linux__
211-
this->socket = tpt::UnixSocket(this->logger, this->port);
212-
#endif
213-
#ifdef _WIN32
214-
this->socket = tpt::WinSocket(this->port);
215-
#endif
216-
}
185+
Teapot::Teapot(unsigned int port) : Teapot("127.0.0.1", port, 10, DEFAULT, "static") {}
217186

218187
Teapot::Teapot(std::string ip_address, unsigned int port, unsigned int max_connections, logging logging_type, std::string static_files_dir)
219188
{
@@ -222,12 +191,16 @@ Teapot::Teapot(std::string ip_address, unsigned int port, unsigned int max_conne
222191
this->max_connections = max_connections;
223192
this->logging_type = logging_type;
224193
this->static_files_dir = static_files_dir;
194+
this->sanitizer_middleware = SanitizerMiddleware();
195+
this->cors_middleware = CORSMiddleware();
196+
this->security_middleware = SecurityMiddleware();
225197
this->logger = ConsoleLogger();
198+
// Conditional compilation based on the operating system
226199
#ifdef __linux__
227-
this->socket = tpt::UnixSocket(logger, ip_address, port, max_connections);
200+
this->socket = tpt::UnixSocket(this->logger, this->ip_address, this->port, this->max_connections);
228201
#endif
229202
#ifdef _WIN32
230-
this->socket = tpt::WinSocket(ip_address, port, max_connections);
203+
this->socket = tpt::WinSocket(this->ip_address, this->port, this->max_connections);
231204
#endif
232205
}
233206

@@ -265,11 +238,17 @@ void Teapot::run()
265238
std::cout << e.what();
266239
this->socket.closeSocket(client_socket);
267240
}
241+
catch (SocketAcceptException &)
242+
{
243+
continue;
244+
}
245+
catch (SocketCloseException &)
246+
{
247+
exit(EXIT_FAILURE);
248+
}
268249
}
269250

270251
LOG_INFO(logger, "Shutting down...");
271-
this->socket.closeSocket();
272-
sleep(3);
273252
}
274253

275254
void Teapot::serveFile(std::string url, std::string file_path)

src/unix_socket.cpp

+18-119
Original file line numberDiff line numberDiff line change
@@ -3,101 +3,13 @@
33

44
using namespace tpt;
55

6-
UnixSocket::UnixSocket()
7-
{
8-
this->ip_address = "127.0.0.1";
9-
this->port = 8000;
10-
this->max_connections = 10;
11-
this->logger = ConsoleLogger();
12-
13-
LOG_INFO(logger, "Creating socket ...");
14-
this->server_socket = socket(AF_INET, SOCK_STREAM, 0);
15-
if (this->server_socket < 0)
16-
{
17-
perror("Socket failed");
18-
std::cout << "Error code: " + errno << std::endl;
19-
exit(EXIT_FAILURE);
20-
}
21-
LOG_INFO(logger, "Socket created!");
22-
23-
this->server_address.sin_family = AF_INET;
24-
this->server_address.sin_port = htons(this->port);
25-
this->server_address.sin_addr.s_addr = inet_addr(this->ip_address.c_str());
26-
27-
Utils::fillIPBlacklist(this->ip_blacklist);
28-
}
29-
30-
UnixSocket::UnixSocket(ConsoleLogger logger)
31-
{
32-
this->ip_address = "127.0.0.1";
33-
this->port = 8000;
34-
this->max_connections = 10;
35-
this->logger = logger;
36-
37-
LOG_INFO(logger, "Creating socket ...");
38-
this->server_socket = socket(AF_INET, SOCK_STREAM, 0);
39-
if (this->server_socket < 0)
40-
{
41-
perror("Socket failed");
42-
std::cout << "Error code: " + errno << std::endl;
43-
exit(EXIT_FAILURE);
44-
}
45-
LOG_INFO(logger, "Socket created!");
46-
47-
this->server_address.sin_family = AF_INET;
48-
this->server_address.sin_port = htons(this->port);
49-
this->server_address.sin_addr.s_addr = inet_addr(this->ip_address.c_str());
50-
51-
Utils::fillIPBlacklist(this->ip_blacklist);
52-
}
53-
54-
UnixSocket::UnixSocket(ConsoleLogger logger, unsigned int port)
55-
{
56-
this->ip_address = "127.0.0.1";
57-
this->port = port;
58-
this->max_connections = 10;
59-
this->logger = logger;
6+
UnixSocket::UnixSocket() : UnixSocket(ConsoleLogger()) {}
607

61-
LOG_INFO(logger, "Creating socket ...");
62-
this->server_socket = socket(AF_INET, SOCK_STREAM, 0);
63-
if (this->server_socket < 0)
64-
{
65-
perror("Socket failed");
66-
std::cout << "Error code: " + errno << std::endl;
67-
exit(EXIT_FAILURE);
68-
}
69-
LOG_INFO(logger, "Socket created!");
70-
71-
this->server_address.sin_family = AF_INET;
72-
this->server_address.sin_port = htons(this->port);
73-
this->server_address.sin_addr.s_addr = inet_addr(this->ip_address.c_str());
8+
UnixSocket::UnixSocket(ConsoleLogger logger) : UnixSocket(logger, "127.0.0.1", 8000, 10) {}
749

75-
Utils::fillIPBlacklist(this->ip_blacklist);
76-
}
10+
UnixSocket::UnixSocket(ConsoleLogger logger, unsigned int port) : UnixSocket(logger, "127.0.0.1", port, 10) {}
7711

78-
UnixSocket::UnixSocket(ConsoleLogger logger, std::string ip_address, unsigned int port)
79-
{
80-
this->ip_address = ip_address;
81-
this->port = port;
82-
this->max_connections = 10;
83-
this->logger = logger;
84-
85-
LOG_INFO(logger, "Creating socket ...");
86-
this->server_socket = socket(AF_INET, SOCK_STREAM, 0);
87-
if (this->server_socket < 0)
88-
{
89-
perror("Socket failed");
90-
std::cout << "Error code: " + errno << std::endl;
91-
exit(EXIT_FAILURE);
92-
}
93-
LOG_INFO(logger, "Socket created!");
94-
95-
this->server_address.sin_family = AF_INET;
96-
this->server_address.sin_port = htons(this->port);
97-
this->server_address.sin_addr.s_addr = inet_addr(this->ip_address.c_str());
98-
99-
Utils::fillIPBlacklist(this->ip_blacklist);
100-
}
12+
UnixSocket::UnixSocket(ConsoleLogger logger, std::string ip_address, unsigned int port) : UnixSocket(logger, ip_address, port, 10) {}
10113

10214
UnixSocket::UnixSocket(ConsoleLogger logger, std::string ip_address, unsigned int port, unsigned int max_connections)
10315
{
@@ -110,9 +22,7 @@ UnixSocket::UnixSocket(ConsoleLogger logger, std::string ip_address, unsigned in
11022
this->server_socket = socket(AF_INET, SOCK_STREAM, 0);
11123
if (this->server_socket < 0)
11224
{
113-
perror("Socket failed");
114-
std::cout << "Error code: " + errno << std::endl;
115-
exit(EXIT_FAILURE);
25+
throw SocketCreationException();
11626
}
11727
LOG_INFO(logger, "Socket created!");
11828

@@ -129,21 +39,16 @@ void UnixSocket::bindSocket()
12939
LOG_INFO(logger, "Binding socket ...");
13040
if ((bind(this->server_socket, (struct sockaddr *)&this->server_address, sizeof(this->server_address))) < 0)
13141
{
132-
perror("Bind failed");
133-
std::cout << "Error code: " + errno << std::endl;
134-
exit(EXIT_FAILURE);
42+
throw SocketBindingException();
13543
}
136-
this->setSocketTimeout(this->server_socket, 5);
13744
LOG_INFO(logger, "Binding done! Listening to connections ...");
13845
}
13946

14047
void UnixSocket::listenToConnections()
14148
{
14249
if ((listen(this->server_socket, this->max_connections)) < 0)
14350
{
144-
perror("Listen failed");
145-
std::cout << "Error code: " + errno << std::endl;
146-
exit(1);
51+
throw SocketListenException();
14752
}
14853
}
14954

@@ -152,12 +57,11 @@ void UnixSocket::acceptConnection(SOCKET &client_socket, void *client_address)
15257
struct sockaddr_storage client_addr_storage;
15358
socklen_t client_addr_size = sizeof(client_addr_storage);
15459

60+
this->setSocketTimeout(this->server_socket, 5);
15561
client_socket = accept(this->server_socket, (struct sockaddr *)&client_addr_storage, &client_addr_size);
15662
if (client_socket < 0)
15763
{
158-
perror("Accept failed");
159-
std::cout << "Error code: " << std::to_string(errno) << std::endl;
160-
exit(EXIT_FAILURE);
64+
throw SocketAcceptException();
16165
}
16266

16367
// Assuming client_address is meant to store the result
@@ -198,16 +102,17 @@ ssize_t UnixSocket::receiveData(SOCKET client_socket, char *buffer, unsigned int
198102
ssize_t data = recv(client_socket, (void *)buffer, buffer_size, 0);
199103
if (data < 0)
200104
{
201-
perror("Receive error");
202-
std::cout << "Error code: " + errno << std::endl;
203-
exit(1);
105+
throw SocketReceiveException();
204106
}
205107
return data;
206108
}
207109

208110
void UnixSocket::sendData(SOCKET client_socket, const void *buffer, unsigned int buffer_size, int flags)
209111
{
210-
send(client_socket, buffer, buffer_size, flags);
112+
if (send(client_socket, buffer, buffer_size, flags) == -1)
113+
{
114+
throw SocketSendException();
115+
}
211116
}
212117

213118
void UnixSocket::closeSocket()
@@ -221,30 +126,24 @@ void UnixSocket::closeSocket()
221126
LOG_INFO(logger, "Closing socket ...");
222127
if (shutdown(this->server_socket, SHUT_RDWR) == -1)
223128
{
224-
perror("An error occurred while shutting down the socket: ");
225-
std::cout << "Error code: " + errno << std::endl;
226-
exit(EXIT_FAILURE);
129+
throw SocketCloseException();
227130
}
228131
if (close(this->server_socket) == 0)
229132
{
230133
LOG_INFO(logger, "Socket closed!");
231-
exit(EXIT_SUCCESS);
134+
return;
232135
}
233136
else
234137
{
235-
perror("An error occurred while closing the socket: ");
236-
std::cout << "Error code: " + errno << std::endl;
237-
exit(EXIT_FAILURE);
138+
throw SocketCloseException();
238139
}
239140
}
240141

241142
void UnixSocket::closeSocket(SOCKET client_socket)
242143
{
243144
if (shutdown(client_socket, SHUT_RDWR) == -1)
244145
{
245-
perror("An error occurred while shutting down the socket: ");
246-
std::cout << "Error code: " + errno << std::endl;
247-
exit(EXIT_FAILURE);
146+
throw SocketCloseException();
248147
}
249148
close(client_socket);
250149

0 commit comments

Comments
 (0)