Trimmed web_server

This commit is contained in:
TriantaTV 2023-09-30 17:10:42 -05:00
parent c4cd468480
commit 98af3a223d

View File

@ -72,18 +72,7 @@ typedef int32_t socklen_t;
#define PORT_NUM 7080 // Port number for a Web server #define PORT_NUM 7080 // Port number for a Web server
//----- Function prototypes ------------------------------------------------- //----- Function prototypes -------------------------------------------------
void ClientRequest(int server_s, int client_s); void ClientRequest(int client_s);
/* 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 ======================================================== //===== modeule main ========================================================
int main(void) int main(void)
@ -146,59 +135,13 @@ int main(void)
continue; continue;
} }
std::async(std::launch::async, ClientRequest, server_s, client_s); std::async(std::launch::async, ClientRequest, client_s);
}
/* 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
/* ------------------------------------------------------------------- */
}
close(server_s); close(server_s);
/* 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); return (1);
} }
void ClientRequest(int server_s, int client_s) { void ClientRequest(int client_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 char out_buf[BUF_SIZE]; // Output buffer for HTML response
char *file_name; // File name char *file_name; // File name
@ -253,130 +196,3 @@ void ClientRequest(int server_s, int client_s) {
close(client_s); close(client_s);
} }
/* 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
ssize_t 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);
do {
buf_len = read(fh, out_buf, BUF_SIZE);
send(client_s, out_buf, buf_len, 0);
} while (buf_len != 0);
close(fh);
}
}
// Shut down my (the child) pipe
close(client_s);
exit(1);
}
#endif
/* ----------------------------------------------------------------------- */