proxy-network/src/proxy.cpp

43 lines
1.3 KiB
C++
Raw Normal View History

2023-09-30 14:28:48 -05:00
//----- Include files ---------------------------------------------------------
2023-10-01 18:24:47 -05:00
#include <algorithm>
2023-09-30 14:28:48 -05:00
#include <future>
2023-10-01 16:10:03 -05:00
#include <iostream>
2023-10-01 18:24:47 -05:00
#include <vector>
#include "network.hpp"
2023-09-30 14:28:48 -05:00
//----- HTTP response messages ----------------------------------------------
#define OK_IMAGE "HTTP/1.0 200 OK\nContent-Type:image/gif\n\n"
#define OK_TEXT "HTTP/1.0 200 OK\nContent-Type:text/html\n\n"
#define NOTOK_404 "HTTP/1.0 404 Not Found\nContent-Type:text/html\n\n"
#define MESS_404 "<html><body><h1>FILE NOT FOUND</h1></body></html>"
int main(void)
{
2023-10-01 18:24:47 -05:00
std::vector<std::future<void>> pending_futures;
Server proxy(kProxyPort);
Client browser;
Client webserver;
2023-09-30 14:28:48 -05:00
2023-10-01 18:24:47 -05:00
proxy.Open();
2023-09-30 14:28:48 -05:00
// Main loop to listen, accept, and then spin-off a thread to handle the GET
while (1)
{
2023-10-01 18:24:47 -05:00
if (browser.ConnectFrom(proxy.socketFD) != 0)
{
2023-10-01 16:10:03 -05:00
std::cerr << "ERROR - Unable to create socket to client" << std::endl;
2023-09-30 14:28:48 -05:00
continue;
2023-10-01 18:24:47 -05:00
}
if (webserver.ConnectTo(kWebserverPort) != 0)
{
std::cerr << "ERROR - Unable to connect to webserver" << std::endl;
continue;
}
auto newThreadRequest = std::async(std::launch::async, HandleClient, browser.socketFD, webserver.socketFD);
pending_futures.push_back(std::move(newThreadRequest));
2023-09-30 14:28:48 -05:00
}
2023-10-01 18:24:47 -05:00
proxy.Close();
return 0;
2023-09-30 14:28:48 -05:00
}