Skip to content

Commit aa79555

Browse files
committed
chore: change port on configuration file
1 parent dfa82a7 commit aa79555

File tree

2 files changed

+53
-6
lines changed

2 files changed

+53
-6
lines changed

config/default.conf

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
server {
22
host 127.0.0.1;
3-
port 8080;
3+
port 8042;
44
server_names example.com www.example.com;
55
client_max_size 10M;
66

src/main.cpp

+52-5
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,31 @@
88
#include <map>
99
#include <string>
1010
#include <sstream>
11+
#include <unistd.h>
12+
#include <sys/socket.h>
1113

1214
#include "common.hpp"
1315
#include "network/NetworkSetup.hpp"
1416
#include "parser_config_file/CheckFile.hpp"
1517
#include "parser_config_file/ConfigParser.hpp"
1618

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\nContent-Length: 13\r\n\r\nHello, world!";
32+
send(client_fd, response.c_str(), response.size(), 0);
33+
close(client_fd);
34+
}
35+
1736
int main(int argc, char* argv[])
1837
{
1938
std::string configuration_file;
@@ -30,18 +49,46 @@ int main(int argc, char* argv[])
3049
ConfigParser parser;
3150
std::vector<Server> servers = parser.parse(configuration_file);
3251

52+
//setup network
3353
NetworkSetup networkSetup;
3454
for (std::vector<Server>::const_iterator it = servers.begin(); it != servers.end(); ++it)
3555
{
56+
std::stringstream port;
57+
port << it->port;
3658
if (!networkSetup.setupServer(it->host, it->port))
37-
{
38-
std::stringstream port;
39-
port << it->port;
4059
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+
}
4190
}
4291
}
43-
// for (std::vector<Server>::const_iterator it = servers.begin(); it != servers.end(); ++it)
44-
// std::cout << *it << std::endl;
4592
}
4693
catch (const ConfigParseError& e)
4794
{

0 commit comments

Comments
 (0)