diff --git a/.vscode/settings.json b/.vscode/settings.json index 654eb2c0f8b9510552852d113b8e022f1ee3257a..4f9b610c76728c5e56eb95b4aa04d568b7657f62 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -11,6 +11,8 @@ "xstring": "c", "xutility": "c", "stdio.h": "c", - "stat.h": "c" + "stat.h": "c", + "server.h": "c", + "types.h": "c" } } \ No newline at end of file diff --git a/get.c b/get.c index c9e9bd0fe8a879d897ebe7c07db006b0074844cb..863588f81f2a1ed69964bac3d835fc959fa687dd 100644 --- a/get.c +++ b/get.c @@ -1,21 +1,6 @@ -#define FOUND_RESPONSE "HTTP/1.0 200 OK\r\n" /* add \n*/ -#define NOT_FOUND_RESPONSE "HTTP/1.0 404\r\n\r\n" - #include "get.h" -#include <stdio.h> -#include <string.h> -#include <pthread.h> -#include <sys/types.h> -#include <sys/socket.h> -#include <netinet/in.h> -#include <unistd.h> -#include <sys/sendfile.h> -#include <sys/types.h> -#include <sys/stat.h> -#include <fcntl.h> -#define FOUND 1 -#define NOT_FOUND 0 + void parse_request_and_send_response(char* root, char* buffer, int newsockfd) { const char* path_start = strstr(buffer, "GET") + 4; diff --git a/get.h b/get.h index 8e62fe04853a1602aa9db5d2d212b778fd77bd7f..d37a54cecd56ac73cdb6dbdf3a182e6fac992a7e 100644 --- a/get.h +++ b/get.h @@ -1,8 +1,18 @@ -/* the include guards */ +/* Include guards */ #ifndef GET_H_INCLUDED #define GET_H_INCLUDED -#include <stdio.h> +/* Hash defines */ +#define FOUND_RESPONSE "HTTP/1.0 200 OK\r\n" /* add \n*/ +#define NOT_FOUND_RESPONSE "HTTP/1.0 404\r\n\r\n" +#define FOUND 1 +#define NOT_FOUND 0 +#define BUF 100000 +#define INFINITE_LOOP 1 + +/* Libraries used */ +#include "server.h" + /* Prototypes for the functions */ void parse_request_and_send_response(char* root, char* buffer, int newsockfd); diff --git a/server.c b/server.c index 28ebbf64c0709366bbd54f9b415cf1a663684105..2d8c27edfa1a0b218d8eb2513ef7750beb66a17c 100644 --- a/server.c +++ b/server.c @@ -1,36 +1,30 @@ -/* - * Server program based on sample code +/* + |**************************************************| + |*| Computer Systems (COMP20023) 2018 Semester 1 |*| + |*| Assignment 1 - Multithreaded HTTP/1.0 Server |*| + |**************************************************| + |**| Student ID: |******| Name: |*******| + |**| 798839 |******| Saleh Ahmed Khan |*******| + |**************************************************| + |* Credit: Used Sample code from LMS for sockets *| */ -#define FOUND_RESPONSE "HTTP/1.0 200 OK\r\n" /* add \n*/ -#define NOT_FOUND_RESPONSE "HTTP/1.0 404\r\n\r\n" -#define FOUND 0 -#define BUF 100000 -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <pthread.h> -#include <sys/types.h> -#include <sys/socket.h> -#include <netinet/in.h> -#include <unistd.h> -#include <sys/sendfile.h> -#include <sys/types.h> -#include <sys/stat.h> -#include <fcntl.h> -#include "get.h" +#include "server.h" -int main(int argc, char *argv[]) -{ +int main(int argc, char *argv[]) { + /* Sample code starts here, not changed + or commented as per clarification + with the tutor via email */ + int sockfd, newsockfd, portno; char buffer[BUF]; struct sockaddr_in serv_addr, cli_addr; socklen_t clilen; int n; - if (argc < 2) { // change this - fprintf(stderr,"ERROR, no port provided\n"); + if (argc < 2) { + fprintf(stderr, "Usage \"./name <port> ./<root>\"\n"); exit(1); } @@ -61,37 +55,35 @@ int main(int argc, char *argv[]) incoming connection requests will be queued */ listen(sockfd,5); clilen = sizeof(cli_addr); + + /* End of sample code. Most of my own code starts here: */ + char* root = argv[2]; - while(1) { + while (INFINITE_LOOP) { /* Accept a connection - block until a connection is ready to be accepted. Get back a new file descriptor to communicate on. */ newsockfd = accept( sockfd, (struct sockaddr *) &cli_addr, &clilen); if (newsockfd < 0) { - perror("ERROR on accept"); - // exit(1); continue; } - if (!fork()) { + /* Multithreading using fork(), based on clarification + by Professor this is a valid alternative to pthreads. + Based on comparative testing vs pthreads this works + efficiently and makes code shorter and more readable. + */ + if (!fork()) { close(sockfd); - printf("closed sockfd\n"); - // bzero(buffer,BUF); /* Read characters from the connection, then process */ - n = read(newsockfd,buffer,BUF-1); + read(newsockfd, buffer, BUF-1); parse_request_and_send_response(root, buffer, newsockfd); close(newsockfd); - printf("closed newsockfd\n"); } close(newsockfd); - printf("closed sockfd in parent\n"); } - if (n < 0) { - perror("ERROR writing to socket"); - exit(1); - } /* close socket */ - // close(sockfd); + close(sockfd); return 0; }