diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..d066651 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,19 @@ +cmake_minimum_required(VERSION 3.10) + +project( + proxy-network + LANGUAGES CXX) + +set(CMAKE_CXX_STANDARD 11 CACHE STRING "The C++ standard to use") +set(CMAKE_CXX_STANDARD_REQUIRED ON) +set(CMAKE_CXX_EXTENSIONS OFF) +set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin) + +include_directories(${proxy-network_SOURCE_DIR}/src) + +add_subdirectory(src) + +if("${CMAKE_BUILD_TYPE}" STREQUAL "Debug") + add_subdirectory(test) +endif() + diff --git a/README.md b/README.md index e1aaea8..1b63240 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,18 @@ # proxy-network +## Compiling the project + +Prerequisites + - C++11 + +In order to compile the project, simply run these two commands: + + cmake -B build -S . + cmake --build build + +## Running the Project +The program should now be compiled at ./build/bin/proxy-network + +Simply run the program using: + + build/bin/proxy-network diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 0000000..2b33f04 --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1,6 @@ +add_executable(proxy-network + ./proxy-network.cpp +) + +target_include_directories(proxy-network PUBLIC ${CMAKE_CURRENT_LIST_DIR}) + diff --git a/src/proxy-network.cpp b/src/proxy-network.cpp new file mode 100644 index 0000000..9c6f43b --- /dev/null +++ b/src/proxy-network.cpp @@ -0,0 +1,6 @@ +#include + +int main() { + std::cout << "Hello world" << std::endl; + return 0; +} diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt new file mode 100644 index 0000000..e69de29 diff --git a/test/mt_web_server.cpp b/test/mt_web_server.cpp new file mode 100644 index 0000000..de95e9e --- /dev/null +++ b/test/mt_web_server.cpp @@ -0,0 +1,304 @@ +//====================================================== file = weblite.c ===== +//= A super light weight HTTP server = +//============================================================================= +//= Notes: = +//= 1) Compiles for Winsock only since uses Windows threads. Generates = +//= one warning about unreachable code in main. Ignore this warning. = +//= 2) Serves HTML and GIF only. = +//= 3) Sometimes the browser drops a connection when doing a refresh. = +//= This is handled by checking the recv() return code in the function = +//= that handles GETs. = +//= 4) The 404 HTML message does not always display in Explorer. = +//=---------------------------------------------------------------------------= +//= Execution notes: = +//= 1) Execute this program in the directory which will be the root for = +//= all file references (i.e., the directory that is considered at = +//= "public.html"). = +//= 2) Open a Web browser and surf to http://xxx.xxx.xxx.xxx/yyy where = +//= xxx.xxx.xxx.xxx is the IP address or hostname of the machine that = +//= weblite is executing on and yyy is the requested object. = +//= 3) The only output (to stdout) from weblite is a message with the = +//= of the file currently being sent = +//=---------------------------------------------------------------------------= +//= Build: bcc32 weblite.c, cl weblite.c wsock32.lib (or ws2_32.lib) = +//= gcc weblite.c -lsocket -lnsl for BSD = +//=---------------------------------------------------------------------------= +//= Execute: weblite = +//=---------------------------------------------------------------------------= +//= History: KJC (12/29/00) - Genesis (from server.c) = +//= HF (01/25/01) - Ported to multi-platform environment = +//============================================================================= +#define WIN // WIN for Windows environment, UNIX for BSD or LINUX env. + +//----- Include files --------------------------------------------------------- +#include // Needed for printf() +#include // Needed for exit() +#include // Needed for strcpy() and strlen() +#include // Needed for file i/o constants +#include // Needed for file i/o constants + +/* FOR BSD UNIX/LINUX ---------------------------------------------------- */ +#ifdef UNIX +#include // +#include // +#include // +#include // +#endif +/* ------------------------------------------------------------------------ */ + +/* FOR WIN ---------------------------------------------------------------- */ +#ifdef WIN +#include // Needed for _threadid +#include // Needed for _beginthread() and _endthread() +#include // Needed for open(), close(), and eof() +#include // Needed for all Winsock stuff +#endif +/* ------------------------------------------------------------------------ */ + +//----- 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 "

FILE NOT FOUND

" + +//----- Defines ------------------------------------------------------------- +#define BUF_SIZE 4096 // Buffer size (big enough for a GET) +#define PORT_NUM 9080 // Port number for a Web server + +//----- Function prototypes ------------------------------------------------- +/* FOR WIN --------------------------------------------------------------- */ +#ifdef WIN +void handle_get(void *in_arg); // Thread function to handle GET +#endif +/* ----------------------------------------------------------------------- */ + +/* FOR UNIX/LINUX -------------------------------------------------------- */ +#ifdef UNIX +void child_proc(int server_s, int client_s); // Fork function for GET +#endif +/* ----------------------------------------------------------------------- */ + +//===== modeule main ======================================================== +int main(void) +{ + /* FOR WIN ------------------------------------------------------------- */ +#ifdef WIN + WORD wVersionRequested = MAKEWORD(1, 1); // Stuff for WSA functions + WSADATA wsaData; // Stuff for WSA functions +#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 + int addr_len; // Internet address length + + /* FOR UNIX/LINUX ------------------------------------------------------ */ +#ifdef UNIX + int nChild_proc_id; // New child process ID. +#endif + /* --------------------------------------------------------------------- */ + + /* FOR WIN ------------------------------------------------------------- */ +#ifdef WIN + // Initialize winsock + WSAStartup(wVersionRequested, &wsaData); +#endif + /* --------------------------------------------------------------------- */ + + // Create a socket, fill-in address information, and then bind it + server_s = socket(AF_INET, SOCK_STREAM, 0); + server_addr.sin_family = AF_INET; + server_addr.sin_port = htons(PORT_NUM); + server_addr.sin_addr.s_addr = htonl(INADDR_ANY); + bind(server_s, (struct sockaddr *)&server_addr, sizeof(server_addr)); + + // Main loop to listen, accept, and then spin-off a thread to handle the GET + while (1) + { + // Listen for connections and then accept + listen(server_s, 100); + addr_len = sizeof(client_addr); + + client_s = accept(server_s, (struct sockaddr *)&client_addr, &addr_len); + if (client_s == 0) + { + printf("ERROR - Unable to create socket \n"); + exit(1); + } + + /* FOR UNIX/LINUX ---------------------------------------------------- */ +#ifdef UNIX + // Spin-off a child process by fork + nChild_proc_id = fork(); + + // Separate the parent and child process here ... + if (nChild_proc_id == -1) // if I am a new child, go to the child module. + { + child_proc(server_s, client_s); + } +#endif + /* ------------------------------------------------------------------- */ + + /* FOR WIN ----------------------------------------------------------- */ +#ifdef WIN + // Spin-off a thread to handle this request (pass only client_s) + if (_beginthread(handle_get, 4096, (void *)client_s) < 0) + { + printf("ERROR - Unable to create thread \n"); + exit(1); + } +#endif + /* ------------------------------------------------------------------- */ + } + + /* FOR UNIX/LINUX ------------------------------------------------------ */ +#ifdef UNIX + // Close the server socket + close(server_s); +#endif + /* --------------------------------------------------------------------- */ + + /* FOR WIN ------------------------------------------------------------- */ +#ifdef WIN + // Close the server socket and clean-up winsock + printf("this web server is shutting down .....\a\n"); + + closesocket(server_s); + WSACleanup(); +#endif + /* --------------------------------------------------------------------- */ + + // To make sure this "main" returns an integer. + return (1); +} + +/* FOR WIN --------------------------------------------------------------- */ +#ifdef WIN +//=========================================================================== +//= This is is the thread function to handle the GET = +//= - It is assumed that the request is a GET = +//=========================================================================== +void handle_get(void *in_arg) +{ + unsigned int client_s; // Client socket descriptor + char in_buf[BUF_SIZE]; // Input buffer for GET resquest + char out_buf[BUF_SIZE]; // Output buffer for HTML response + char *file_name; // File name + unsigned int fh; // File handle + unsigned int buf_len; // Buffer length for file reads + unsigned int retcode; // Return code + + // Set client_s to in_arg + client_s = (unsigned int)in_arg; + + // Receive the GET request from the Web browser + retcode = recv(client_s, in_buf, BUF_SIZE, 0); + + // Handle the GET if there is one (see note #3 in the header) + if (retcode != -1) + { + // Parse out the filename from the GET request + strtok(in_buf, " "); + file_name = strtok(NULL, " "); + + // Open the requested file + // - Start at 2nd char to get rid of leading "\" + fh = open(&file_name[1], O_RDONLY | O_BINARY, S_IREAD | S_IWRITE); + + // Generate and send the response (404 if could not open the file) + if (fh == -1) + { + printf("File %s not found - sending an HTTP 404 \n", &file_name[1]); + strcpy(out_buf, NOTOK_404); + send(client_s, out_buf, strlen(out_buf), 0); + strcpy(out_buf, MESS_404); + send(client_s, out_buf, strlen(out_buf), 0); + } + else + { + printf("File %s is being sent \n", &file_name[1]); + if (strstr(file_name, ".gif") != NULL) + strcpy(out_buf, OK_IMAGE); + else + strcpy(out_buf, OK_TEXT); + send(client_s, out_buf, strlen(out_buf), 0); + while (!eof(fh)) + { + buf_len = read(fh, out_buf, BUF_SIZE); + send(client_s, out_buf, buf_len, 0); + } + close(fh); + } + } + + // Close the client socket and end the thread + closesocket(client_s); + _endthread(); +} +#endif +/* ----------------------------------------------------------------------- */ + +/* FOR UNIX/LINUX -------------------------------------------------------- */ +#ifdef UNIX +void child_proc(int server_s, int client_s) +{ + char in_buf[BUF_SIZE]; // Input buffer for GET resquest + char out_buf[BUF_SIZE]; // Output buffer for HTML response + char *file_name; // File name + unsigned int fh; // File handle + unsigned int buf_len; // Buffer length for file reads + unsigned int retcode; // Return code + + // Shut down the parent pipe + close(server_s); + + // Receive the GET request from the Web browser + retcode = recv(client_s, in_buf, BUF_SIZE, 0); + + // Handle the GET if there is one (see note #3 in the header) + if (retcode != -1) + { + // Parse out the filename from the GET request + strtok(in_buf, " "); + file_name = strtok(NULL, " "); + + // Open the requested file + // - Start at 2nd char to get rid of leading "\" + // fh = open(&file_name[1], O_RDONLY | O_BINARY, S_IREAD | S_IWRITE); + fh = open(&file_name[1], O_RDONLY, S_IREAD | S_IWRITE); + + // Generate and send the response (404 if could not open the file) + if (fh == -1) + { + printf("File %s not found - sending an HTTP 404 \n", &file_name[1]); + strcpy(out_buf, NOTOK_404); + send(client_s, out_buf, strlen(out_buf), 0); + strcpy(out_buf, MESS_404); + send(client_s, out_buf, strlen(out_buf), 0); + } + else + { + printf("File %s is being sent \n", &file_name[1]); + if (strstr(file_name, ".gif") != NULL) + strcpy(out_buf, OK_IMAGE); + else + strcpy(out_buf, OK_TEXT); + send(client_s, out_buf, strlen(out_buf), 0); + + while (fh != EOF) + { + buf_len = read(fh, out_buf, BUF_SIZE); + send(client_s, out_buf, buf_len, 0); + } + close(fh); + } + } + + // Shut down my (the child) pipe + close(client_s); +} +#endif +/* ----------------------------------------------------------------------- */ \ No newline at end of file diff --git a/test/multi_threads.cpp b/test/multi_threads.cpp new file mode 100644 index 0000000..b3b7190 --- /dev/null +++ b/test/multi_threads.cpp @@ -0,0 +1,109 @@ +/* ************************************************************************* * + * * + * Multi.cpp: * + * This is a sample program for multi-threaded applications. * + * * + * As soon as this program starts, the main thread generates two child * + * threads. The two child threads wait for 10 seconds and terminate. * + * * + * While the two child threads are running, the main thread waits. The * + * main thread waits until both threads finish. * + * * + * Compile: * + * In Project->Setting->C/C++->CodeGenartion(in Category) * + * ->Select Multi-threaded for runtime library * + * * + * Coded by: H. Fujinoki * + * September 12, 11:00 AM at Edwardsville, IL * + * * + * ************************************************************************* */ +#include // for thread system calls (_beginthread, etc.) +#include // for TRUE, FALSE labels +#include // for printf +#include // for clock() and CLK_TCK + +/* Global label defenition ------------------------------------------------ */ +#define INTERVAL 1 // Transmission interval in seconds +#define REPEATS 50 // Number of child thread's repeats + +/* Global Variables ------------------------------------------------------- */ +int nChild1_status; // Child thread #1 status +int nChild2_status; // Child thread #2 status + +/* Prototypes ------------------------------------------------------------- */ +void ChildThread1(void *dummy); // The child thread #1 +void ChildThread2(void *dummy); // The child thread #2 + +/* The MAIN --------------------------------------------------------------- */ +void main (void) +{ + /* Set the child thread status (TRUE = RUNNING) --- */ + nChild1_status = TRUE; + nChild2_status = TRUE; + + /* Create and start the two threads --- */ + _beginthread (ChildThread1, 0, NULL ); // Start child process #1 + _beginthread (ChildThread2, 0, NULL ); // Start child process #2 + + /* Spin-loop until both threads finish --- */ + while ((nChild1_status == TRUE)||(nChild2_status == TRUE)) + { ; } + + /* Two threads are now finished --- */ + printf("Both child threads are finished ... \n"); + printf("The main thread is finishing ... \n"); +} + +// The Child-Thread #1 /////////////////////////////////////////////////////// +void ChildThread1(void *dummy) +{ + /* Child #1 local variable(s) --- */ + int i; // Loop counter + + /* This thread is started --- */ + printf("Child Thread #1 has started ... \n"); + + /* wait for 10 seconds w/ count down --- */ + for (i = 0; i < REPEATS ; i++) + { + /* Wait for 10 seconds --- */ + Sleep((clock_t)INTERVAL * CLOCKS_PER_SEC); + + /* Display count down --- */ + printf("Child #1: %d more second(s) to finish ...\n", REPEATS -i); + } + + /* Reset the status flag --- */ + nChild1_status = FALSE; + + /* Terminate this thread --- */ + _endthread(); +} + +// The Child-Thread #2 /////////////////////////////////////////////////////// +void ChildThread2(void *dummy) +{ + /* Child #2 local variable(s) --- */ + int i; // Loop counter + + /* This thread is started --- */ + printf("Child Thread #2 has started ... \n"); + + /* wait for 10 seconds w/ count down --- */ + for (i = 0; i < REPEATS ; i++) + { + /* Wait for 10 seconds --- */ + Sleep((clock_t)INTERVAL * CLOCKS_PER_SEC); + + /* Display count down --- */ + printf("Child #2: %d more second(s) to finish ...\n", REPEATS -i); + } + + /* Reset the status flag --- */ + nChild2_status = FALSE; + + /* Terminate this thread --- */ + _endthread(); +} + +// THE END OF LINES ////////////////////////////////////////////////////////// \ No newline at end of file diff --git a/test/page/almost_done.gif b/test/page/almost_done.gif new file mode 100644 index 0000000..bc2d181 Binary files /dev/null and b/test/page/almost_done.gif differ diff --git a/test/page/index.html b/test/page/index.html new file mode 100644 index 0000000..c6fd54b --- /dev/null +++ b/test/page/index.html @@ -0,0 +1,196 @@ + + + +Southern Illinois University Edwardsville + + + + + + +

+ +

+ +
+ +
+ + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + +
+ +Urban opportunities at a suburban university: a 2,660-acre
campus only 25 minutes from downtown St. Louis
+ +

+Watch Us Grow + + +

+

+Southern Illinois University Edwardsville
+Edwardsville, Illinois 62026
+618-650-2000
+
+ +

+ + + +

+ + + + + diff --git a/test/page/rule1.gif b/test/page/rule1.gif new file mode 100644 index 0000000..5d5ebf4 Binary files /dev/null and b/test/page/rule1.gif differ diff --git a/test/page/siuebdy1.jpg b/test/page/siuebdy1.jpg new file mode 100644 index 0000000..5a6ecfd Binary files /dev/null and b/test/page/siuebdy1.jpg differ diff --git a/test/page/siuebdy4.jpg b/test/page/siuebdy4.jpg new file mode 100644 index 0000000..234f145 Binary files /dev/null and b/test/page/siuebdy4.jpg differ diff --git a/test/page/siuehdr2.jpg b/test/page/siuehdr2.jpg new file mode 100644 index 0000000..b1b5f90 Binary files /dev/null and b/test/page/siuehdr2.jpg differ