8
8
#include < map>
9
9
#include < string>
10
10
#include < sstream>
11
+ #include < unistd.h>
12
+ #include < sys/socket.h>
11
13
12
14
#include " common.hpp"
13
15
#include " network/NetworkSetup.hpp"
14
16
#include " parser_config_file/CheckFile.hpp"
15
17
#include " parser_config_file/ConfigParser.hpp"
16
18
19
+ // Function to handle incoming requests (simplified version)
20
+ void handleClientRequest (int client_fd, const Location& location) {
21
+ // Example of processing a CGI request (placeholder)
22
+ if (location.cgi .enabled ) {
23
+ std::cout << " Executing CGI script: " << location.cgi .path << " \n " ;
24
+ // Here, execute the CGI script using `execve` or similar.
25
+ } else {
26
+ std::cout << " Handling non-CGI request for client " << client_fd << " \n " ;
27
+ // Handle static files, directory listing, etc.
28
+ }
29
+
30
+ // Respond to the client (simplified)
31
+ std::string response = " HTTP/1.1 200 OK\r\n Content-Length: 13\r\n\r\n Hello, world!" ;
32
+ send (client_fd, response.c_str (), response.size (), 0 );
33
+ close (client_fd);
34
+ }
35
+
17
36
int main (int argc, char * argv[])
18
37
{
19
38
std::string configuration_file;
@@ -30,18 +49,46 @@ int main(int argc, char* argv[])
30
49
ConfigParser parser;
31
50
std::vector<Server> servers = parser.parse (configuration_file);
32
51
52
+ // setup network
33
53
NetworkSetup networkSetup;
34
54
for (std::vector<Server>::const_iterator it = servers.begin (); it != servers.end (); ++it)
35
55
{
56
+ std::stringstream port;
57
+ port << it->port ;
36
58
if (!networkSetup.setupServer (it->host , it->port ))
37
- {
38
- std::stringstream port;
39
- port << it->port ;
40
59
throw std::runtime_error (" Failed to setup server on host: " + it->host + " port: " + port.str ());
60
+ std::cout << " Starting server on host: " << it->host << " port: " << it->port << " \n " ;
61
+ }
62
+
63
+ // main server loop
64
+ while (42 )
65
+ {
66
+ // accept new connections
67
+ std::vector<int > serverSockets = networkSetup.getServerSockets ();
68
+ for (size_t i = 0 ; i < serverSockets.size (); i++)
69
+ {
70
+ int socket = serverSockets[i];
71
+ int client_fd = NetworkSetup::acceptConnection (socket);
72
+ if (client_fd < 0 )
73
+ continue ;
74
+
75
+ std::cout << " Accepted new connection on socket " << client_fd << std::endl;
76
+
77
+ // route the request to the appropriate location (simple)
78
+ for (size_t j = 0 ; j < servers.size (); j++)
79
+ {
80
+ const Server& server = servers[j];
81
+ for (size_t k = 0 ; k < server.locations .size (); k++)
82
+ {
83
+ const Location& location = server.locations [k];
84
+ // Here, you would normally parse the HTTP request and match against `location.path`
85
+ // For simplicity, we handle the first location
86
+ handleClientRequest (client_fd, location);
87
+ break ;
88
+ }
89
+ }
41
90
}
42
91
}
43
- // for (std::vector<Server>::const_iterator it = servers.begin(); it != servers.end(); ++it)
44
- // std::cout << *it << std::endl;
45
92
}
46
93
catch (const ConfigParseError& e)
47
94
{
0 commit comments