Cleaned up web server and makes it use network.cpp

This commit is contained in:
TriantaTV 2023-10-02 16:47:48 -05:00
parent 5e0069871e
commit 0ea28af44d
4 changed files with 53 additions and 74 deletions

View File

@ -5,5 +5,6 @@ add_executable(proxy
add_executable(web_server add_executable(web_server
./web_server.cpp ./web_server.cpp
./network.cpp
) )

View File

@ -4,32 +4,43 @@
#include <string.h> // Needed for strcpy() and strlen() #include <string.h> // Needed for strcpy() and strlen()
#include <iostream> #include <iostream>
void HandleClient(int browser_s, int server_s) { void PipeSockets(int sender_s, int receiver_s) {
char in_buf[BUF_SIZE]; // Input buffer for GET resquest char in_buf[BUF_SIZE]; // Input buffer for GET resquest
char out_buf[BUF_SIZE]; // Output buffer for HTML response
ssize_t buf_len; // Buffer length for file reads ssize_t buf_len; // Buffer length for file reads
//
// Pass GET along from browser to server // Pass GET along from browser to server
std::cout << "Attempting to send to server" << std::endl; std::cout << "Attempting to send data to receiver" << std::endl;
do { do {
buf_len = recv(browser_s, in_buf, BUF_SIZE, 0); buf_len = recv(sender_s, in_buf, BUF_SIZE, 0);
if (buf_len == -1) { if (buf_len == -1) {
std::cout << "ERROR - recv returned -1" << std::endl; std::cout << "ERROR (info) - recv" << std::endl;
std::terminate(); std::terminate();
} }
send(server_s, in_buf, buf_len, 0); send(receiver_s, in_buf, buf_len, 0);
} while (buf_len != 0); std::cout << "LOG (info) - pipe packet size: " << buf_len << std::endl;
std::cout << "Sent to server" << std::endl; } while (buf_len > 0);
std::cout << "Sent data to receiver" << std::endl;
}
void ProxySockets(int sender_s, int receiver_s) {
char out_buf[BUF_SIZE]; // Output buffer for HTML response
ssize_t buf_len; // Buffer length for file reads
// Pass response along from server to browser // Pass response along from server to browser
std::cout << "Attempting to send to browser" << std::endl; std::cout << "Attempting to send to browser" << std::endl;
do { do {
buf_len = recv(server_s, out_buf, BUF_SIZE, 0); buf_len = recv(sender_s, out_buf, BUF_SIZE, 0);
send(browser_s, out_buf, buf_len, 0); if (buf_len == -1) {
} while (buf_len != 0); std::cout << "ERROR (info) - recv" << std::endl;
std::terminate();
}
send(receiver_s, out_buf, buf_len, 0);
std::cout << "LOG (info) - proxy packet size: " << buf_len << std::endl;
} while (buf_len > 0);
std::cout << "Sent to browser" << std::endl; std::cout << "Sent to browser" << std::endl;
close(server_s); close(sender_s);
close(browser_s); close(receiver_s);
} }
Client::Client(void) { Client::Client(void) {
@ -37,8 +48,8 @@ Client::Client(void) {
address.sin_family = AF_INET; address.sin_family = AF_INET;
} }
bool Client::ConnectFrom(int socketFD) { bool Client::ConnectFrom(int serverFD) {
socketFD = accept(socketFD, (struct sockaddr *)&address, &addressLength); socketFD = accept(serverFD, (struct sockaddr *)&address, &addressLength);
if (socketFD == -1) { return 1; } if (socketFD == -1) { return 1; }
return 0; return 0;
} }
@ -60,20 +71,17 @@ bool Client::ConnectTo(int portNumber) {
} }
Server::Server(int portNumber) { Server::Server(int portNumber) {
address.sin_family = AF_INET;
address.sin_port = htons(portNumber);
address.sin_addr.s_addr = htonl(INADDR_ANY);
}
void Server::Open(void) {
std::cout << "Opening the server" << std::endl; std::cout << "Opening the server" << std::endl;
// Create a socket, fill-in address information, and then bind it socketFD = socket(AF_INET, SOCK_STREAM, 0);
socketFD = socket(address.sin_family, SOCK_STREAM, 0);
if (socketFD == -1) if (socketFD == -1)
{ {
std::cerr << "ERROR - Unable to create socket on server" << std::endl; std::cerr << "ERROR - Unable to create socket on server" << std::endl;
exit(1); exit(1);
} }
address.sin_family = AF_INET;
address.sin_port = htons(portNumber);
address.sin_addr.s_addr = htonl(INADDR_ANY);
// Create a socket, fill-in address information, and then bind it
if (bind(socketFD, (struct sockaddr *)&address, sizeof(address)) == -1) if (bind(socketFD, (struct sockaddr *)&address, sizeof(address)) == -1)
{ {
std::cerr << "ERROR - Unable to bind socket" << std::endl; std::cerr << "ERROR - Unable to bind socket" << std::endl;
@ -81,6 +89,7 @@ void Server::Open(void) {
} }
// Listen for connections and then accept // Listen for connections and then accept
listen(socketFD, 100); listen(socketFD, 100);
std::cout << "Server is now open" << std::endl;
} }
void Server::Close(void) { void Server::Close(void) {

View File

@ -10,14 +10,15 @@
#define kWebserverIP "127.0.0.1" #define kWebserverIP "127.0.0.1"
#define kWebserverPort 7080 #define kWebserverPort 7080
void HandleClient(int browser_s, int server_s); void PipeSockets(int sender_s, int receiver_s);
void ProxySockets(int sender_s, int receiver_s);
struct Client { struct Client {
unsigned int socketFD; // Client socket descriptor unsigned int socketFD; // Client socket descriptor
sockaddr_in address; // Client IP address sockaddr_in address; // Client IP address
socklen_t addressLength; // Internet address length socklen_t addressLength; // Internet address length
Client(void); Client(void);
bool ConnectFrom(int socketFD); bool ConnectFrom(int serverFD);
bool ConnectTo(int portNumber); bool ConnectTo(int portNumber);
}; };
@ -25,7 +26,6 @@ struct Server {
unsigned int socketFD; // Server socket descriptor unsigned int socketFD; // Server socket descriptor
sockaddr_in address; // Server Internet address sockaddr_in address; // Server Internet address
Server(int portNumber); Server(int portNumber);
void Open(void);
void Close(void); void Close(void);
}; };

View File

@ -31,12 +31,13 @@
#define UNIX // WIN for Windows environment, UNIX for BSD or LINUX env. #define UNIX // WIN for Windows environment, UNIX for BSD or LINUX env.
//----- Include files --------------------------------------------------------- //----- Include files ---------------------------------------------------------
#include <stdio.h> // Needed for printf()
#include <stdlib.h> // Needed for exit()
#include <string.h> // Needed for strcpy() and strlen() #include <string.h> // Needed for strcpy() and strlen()
#include <fcntl.h> // Needed for file i/o constants #include <fcntl.h> // Needed for file i/o constants
#include <sys/stat.h> // Needed for file i/o constants #include <sys/stat.h> // Needed for file i/o constants
#include <future> #include <future>
#include <iostream>
#include <vector>
#include "network.hpp"
/* FOR BSD UNIX/LINUX ---------------------------------------------------- */ /* FOR BSD UNIX/LINUX ---------------------------------------------------- */
#ifdef UNIX #ifdef UNIX
@ -84,19 +85,9 @@ int main(void)
#endif #endif
/* --------------------------------------------------------------------- */ /* --------------------------------------------------------------------- */
unsigned int server_s; // Server socket descriptor std::vector<std::future<void>> pending_futures;
struct sockaddr_in server_addr; // Server Internet address Server webserver(kWebserverPort);
unsigned int client_s; // Client socket descriptor Client proxy;
struct sockaddr_in client_addr; // Client Internet address
struct in_addr client_ip_addr; // Client IP address
socklen_t addr_len; // Internet address length
/* FOR UNIX/LINUX ------------------------------------------------------ */
#ifdef UNIX
int nChild_proc_id; // New child process ID.
#endif
/* --------------------------------------------------------------------- */
/* FOR WIN ------------------------------------------------------------- */ /* FOR WIN ------------------------------------------------------------- */
#ifdef WIN #ifdef WIN
// Initialize winsock // Initialize winsock
@ -104,41 +95,19 @@ int main(void)
#endif #endif
/* --------------------------------------------------------------------- */ /* --------------------------------------------------------------------- */
// Create a socket, fill-in address information, and then bind it
server_s = socket(AF_INET, SOCK_STREAM, 0);
if (server_s == -1)
{
printf("ERROR - Unable to create socket on server\n");
exit(1);
}
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(PORT_NUM);
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
int bindRet = bind(server_s, (struct sockaddr *)&server_addr, sizeof(server_addr));
if (bindRet == -1)
{
printf("ERROR - Unable to bind socket\n");
exit(1);
}
// Listen for connections and then accept
listen(server_s, 100);
// Main loop to listen, accept, and then spin-off a thread to handle the GET // Main loop to listen, accept, and then spin-off a thread to handle the GET
while (1) while (1)
{ {
addr_len = sizeof(client_addr); if (proxy.ConnectFrom(webserver.socketFD) != 0)
client_s = accept(server_s, (struct sockaddr *)&client_addr, &addr_len);
if (client_s == -1)
{ {
perror("ERROR - Unable to create socket to client\n"); std::cerr << "ERROR - Unable to create socket to client" << std::endl;
continue; continue;
} }
auto newThreadRequest = std::async(std::launch::async, ClientRequest, proxy.socketFD);
std::async(std::launch::async, ClientRequest, client_s); pending_futures.push_back(std::move(newThreadRequest));
} }
close(server_s); webserver.Close();
return (1); return 0;
} }
void ClientRequest(int client_s) { void ClientRequest(int client_s) {
@ -188,7 +157,7 @@ void ClientRequest(int client_s) {
do { do {
buf_len = read(fh, out_buf, BUF_SIZE); buf_len = read(fh, out_buf, BUF_SIZE);
send(client_s, out_buf, buf_len, 0); send(client_s, out_buf, buf_len, 0);
} while (buf_len != 0); } while (buf_len > 0);
close(fh); close(fh);
} }
} }