Cleaned up web server and makes it use network.cpp
This commit is contained in:
parent
5e0069871e
commit
0ea28af44d
@ -5,5 +5,6 @@ add_executable(proxy
|
||||
|
||||
add_executable(web_server
|
||||
./web_server.cpp
|
||||
./network.cpp
|
||||
)
|
||||
|
||||
|
@ -4,32 +4,43 @@
|
||||
#include <string.h> // Needed for strcpy() and strlen()
|
||||
#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 out_buf[BUF_SIZE]; // Output buffer for HTML response
|
||||
ssize_t buf_len; // Buffer length for file reads
|
||||
|
||||
//
|
||||
// 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 {
|
||||
buf_len = recv(browser_s, in_buf, BUF_SIZE, 0);
|
||||
buf_len = recv(sender_s, in_buf, BUF_SIZE, 0);
|
||||
if (buf_len == -1) {
|
||||
std::cout << "ERROR - recv returned -1" << std::endl;
|
||||
std::cout << "ERROR (info) - recv" << std::endl;
|
||||
std::terminate();
|
||||
}
|
||||
send(server_s, in_buf, buf_len, 0);
|
||||
} while (buf_len != 0);
|
||||
std::cout << "Sent to server" << std::endl;
|
||||
send(receiver_s, in_buf, buf_len, 0);
|
||||
std::cout << "LOG (info) - pipe packet size: " << buf_len << 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
|
||||
std::cout << "Attempting to send to browser" << std::endl;
|
||||
do {
|
||||
buf_len = recv(server_s, out_buf, BUF_SIZE, 0);
|
||||
send(browser_s, out_buf, buf_len, 0);
|
||||
} while (buf_len != 0);
|
||||
buf_len = recv(sender_s, out_buf, BUF_SIZE, 0);
|
||||
if (buf_len == -1) {
|
||||
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;
|
||||
close(server_s);
|
||||
close(browser_s);
|
||||
close(sender_s);
|
||||
close(receiver_s);
|
||||
}
|
||||
|
||||
Client::Client(void) {
|
||||
@ -37,8 +48,8 @@ Client::Client(void) {
|
||||
address.sin_family = AF_INET;
|
||||
}
|
||||
|
||||
bool Client::ConnectFrom(int socketFD) {
|
||||
socketFD = accept(socketFD, (struct sockaddr *)&address, &addressLength);
|
||||
bool Client::ConnectFrom(int serverFD) {
|
||||
socketFD = accept(serverFD, (struct sockaddr *)&address, &addressLength);
|
||||
if (socketFD == -1) { return 1; }
|
||||
return 0;
|
||||
}
|
||||
@ -60,20 +71,17 @@ bool Client::ConnectTo(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;
|
||||
// Create a socket, fill-in address information, and then bind it
|
||||
socketFD = socket(address.sin_family, SOCK_STREAM, 0);
|
||||
socketFD = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (socketFD == -1)
|
||||
{
|
||||
std::cerr << "ERROR - Unable to create socket on server" << std::endl;
|
||||
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)
|
||||
{
|
||||
std::cerr << "ERROR - Unable to bind socket" << std::endl;
|
||||
@ -81,6 +89,7 @@ void Server::Open(void) {
|
||||
}
|
||||
// Listen for connections and then accept
|
||||
listen(socketFD, 100);
|
||||
std::cout << "Server is now open" << std::endl;
|
||||
}
|
||||
|
||||
void Server::Close(void) {
|
||||
|
@ -10,14 +10,15 @@
|
||||
#define kWebserverIP "127.0.0.1"
|
||||
#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 {
|
||||
unsigned int socketFD; // Client socket descriptor
|
||||
sockaddr_in address; // Client IP address
|
||||
socklen_t addressLength; // Internet address length
|
||||
Client(void);
|
||||
bool ConnectFrom(int socketFD);
|
||||
bool ConnectFrom(int serverFD);
|
||||
bool ConnectTo(int portNumber);
|
||||
};
|
||||
|
||||
@ -25,7 +26,6 @@ struct Server {
|
||||
unsigned int socketFD; // Server socket descriptor
|
||||
sockaddr_in address; // Server Internet address
|
||||
Server(int portNumber);
|
||||
void Open(void);
|
||||
void Close(void);
|
||||
};
|
||||
|
||||
|
@ -31,12 +31,13 @@
|
||||
#define UNIX // WIN for Windows environment, UNIX for BSD or LINUX env.
|
||||
|
||||
//----- Include files ---------------------------------------------------------
|
||||
#include <stdio.h> // Needed for printf()
|
||||
#include <stdlib.h> // Needed for exit()
|
||||
#include <string.h> // Needed for strcpy() and strlen()
|
||||
#include <fcntl.h> // Needed for file i/o constants
|
||||
#include <sys/stat.h> // Needed for file i/o constants
|
||||
#include <future>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include "network.hpp"
|
||||
|
||||
/* FOR BSD UNIX/LINUX ---------------------------------------------------- */
|
||||
#ifdef UNIX
|
||||
@ -84,19 +85,9 @@ int main(void)
|
||||
#endif
|
||||
/* --------------------------------------------------------------------- */
|
||||
|
||||
unsigned int server_s; // Server socket descriptor
|
||||
struct sockaddr_in server_addr; // Server Internet address
|
||||
unsigned int client_s; // Client socket descriptor
|
||||
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
|
||||
/* --------------------------------------------------------------------- */
|
||||
|
||||
std::vector<std::future<void>> pending_futures;
|
||||
Server webserver(kWebserverPort);
|
||||
Client proxy;
|
||||
/* FOR WIN ------------------------------------------------------------- */
|
||||
#ifdef WIN
|
||||
// Initialize winsock
|
||||
@ -104,41 +95,19 @@ int main(void)
|
||||
#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
|
||||
while (1)
|
||||
{
|
||||
addr_len = sizeof(client_addr);
|
||||
|
||||
client_s = accept(server_s, (struct sockaddr *)&client_addr, &addr_len);
|
||||
if (client_s == -1)
|
||||
if (proxy.ConnectFrom(webserver.socketFD) != 0)
|
||||
{
|
||||
perror("ERROR - Unable to create socket to client\n");
|
||||
std::cerr << "ERROR - Unable to create socket to client" << std::endl;
|
||||
continue;
|
||||
}
|
||||
|
||||
std::async(std::launch::async, ClientRequest, client_s);
|
||||
auto newThreadRequest = std::async(std::launch::async, ClientRequest, proxy.socketFD);
|
||||
pending_futures.push_back(std::move(newThreadRequest));
|
||||
}
|
||||
close(server_s);
|
||||
return (1);
|
||||
webserver.Close();
|
||||
return 0;
|
||||
}
|
||||
|
||||
void ClientRequest(int client_s) {
|
||||
@ -188,7 +157,7 @@ void ClientRequest(int client_s) {
|
||||
do {
|
||||
buf_len = read(fh, out_buf, BUF_SIZE);
|
||||
send(client_s, out_buf, buf_len, 0);
|
||||
} while (buf_len != 0);
|
||||
} while (buf_len > 0);
|
||||
close(fh);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user