@@ -73,68 +73,76 @@ void Teapot::mainEventLoop(SOCKET client_socket)
73
73
std::string content_type;
74
74
unsigned int status_code = 500 ; // Default to internal server error in case of early failure
75
75
76
- if (request)
76
+ try
77
77
{
78
- context->request = &(*request);
79
- this ->sanitizer_middleware .handle (context.get ());
80
- std::string uri = request->getUri ();
81
- if (uri == " /" )
82
- uri = " /index.html" ; // Normalize root access to a specific file, e.g., index.html
83
- std::string method = request->getMethod ();
84
- content_type = determineContentType (uri); // Determine content type early based on URI
85
-
86
- std::cout << " [" << request->getDate () << " ] " << this ->socket .getClientIp () + " " << method << " " << uri << " HTTP/1.1 " ;
87
-
88
- if (method == " GET" )
78
+ if (request)
89
79
{
90
- // Check for predefined routes or responses before attempting to read a file
91
- auto routeIt = this ->routes .find (uri);
92
- auto jsonIt = this ->json_responses .find (uri);
93
- auto htmlIt = this ->html_responses .find (uri);
80
+ context->request = &(*request);
81
+ this ->sanitizer_middleware .handle (context.get ());
82
+ std::string uri = request->getUri ();
83
+ if (uri == " /" )
84
+ uri = " /index.html" ; // Normalize root access to a specific file, e.g., index.html
85
+ std::string method = request->getMethod ();
86
+ content_type = determineContentType (uri); // Determine content type early based on URI
94
87
95
- if (routeIt != this ->routes .end ())
96
- {
97
- body = Utils::readFileToBuffer (this ->static_files_dir + routeIt->second );
98
- status_code = 200 ;
99
- }
100
- else if (jsonIt != this ->json_responses .end ())
101
- {
102
- body = jsonIt->second ;
103
- status_code = 200 ;
104
- content_type = " application/json" ;
105
- }
106
- else if (htmlIt != this ->html_responses .end ())
107
- {
108
- body = htmlIt->second ;
109
- status_code = 200 ;
110
- content_type = " text/html" ;
111
- }
112
- else
88
+ std::cout << " [" << request->getDate () << " ] " << this ->socket .getClientIp () + " " << method << " " << uri << " HTTP/1.1 " ;
89
+
90
+ if (method == " GET" )
113
91
{
114
- try
92
+ // Check for predefined routes or responses before attempting to read a file
93
+ auto routeIt = this ->routes .find (uri);
94
+ auto jsonIt = this ->json_responses .find (uri);
95
+ auto htmlIt = this ->html_responses .find (uri);
96
+
97
+ if (routeIt != this ->routes .end ())
115
98
{
116
- body = Utils::readFileToBuffer (this ->static_files_dir + uri );
99
+ body = Utils::readFileToBuffer (this ->static_files_dir + routeIt-> second );
117
100
status_code = 200 ;
118
101
}
119
- catch (FileNotFoundException & )
102
+ else if (jsonIt != this -> json_responses . end () )
120
103
{
121
- body = Utils::readFileToBuffer (this ->static_files_dir + " /404.html" );
104
+ body = jsonIt->second ;
105
+ status_code = 200 ;
106
+ content_type = " application/json" ;
107
+ }
108
+ else if (htmlIt != this ->html_responses .end ())
109
+ {
110
+ body = htmlIt->second ;
111
+ status_code = 200 ;
122
112
content_type = " text/html" ;
123
- status_code = 404 ;
113
+ }
114
+ else
115
+ {
116
+ try
117
+ {
118
+ body = Utils::readFileToBuffer (this ->static_files_dir + uri);
119
+ status_code = 200 ;
120
+ }
121
+ catch (FileNotFoundException &)
122
+ {
123
+ body = Utils::readFileToBuffer (this ->static_files_dir + " /404.html" );
124
+ content_type = " text/html" ;
125
+ status_code = 404 ;
126
+ }
124
127
}
125
128
}
129
+ else
130
+ {
131
+ // If not GET, assume method not supported
132
+ body = Utils::readFileToBuffer (this ->static_files_dir + " /405.html" );
133
+ content_type = " text/html" ;
134
+ status_code = 405 ;
135
+ }
126
136
}
127
137
else
128
138
{
129
- // If not GET, assume method not supported
130
- body = Utils::readFileToBuffer (this ->static_files_dir + " /405 .html" );
139
+ // Handle parsing failure by responding with 500 Internal Server Error
140
+ body = Utils::readFileToBuffer (this ->static_files_dir + " /500 .html" );
131
141
content_type = " text/html" ;
132
- status_code = 405 ;
133
142
}
134
143
}
135
- else
144
+ catch (...)
136
145
{
137
- // Handle parsing failure by responding with 500 Internal Server Error
138
146
body = Utils::readFileToBuffer (this ->static_files_dir + " /500.html" );
139
147
content_type = " text/html" ;
140
148
}
@@ -210,9 +218,16 @@ void Teapot::run()
210
218
SOCKET client_socket;
211
219
void *client_addr = nullptr ;
212
220
213
- socket.acceptConnection (client_socket, client_addr);
214
-
215
- std::jthread th (&Teapot::mainEventLoop, this , client_socket);
221
+ try
222
+ {
223
+ socket.acceptConnection (client_socket, client_addr);
224
+ std::jthread th (&Teapot::mainEventLoop, this , client_socket);
225
+ }
226
+ catch (IPBlackListedException &e)
227
+ {
228
+ std::cout << e.what ();
229
+ this ->socket .closeSocket (client_socket);
230
+ }
216
231
}
217
232
}
218
233
0 commit comments