Loading file.c +7 −1 Original line number Diff line number Diff line Loading @@ -7,13 +7,16 @@ #include <memory.h> #include <fcntl.h> #include "file.h" /// Serves the given file to the client /// \param clientfd client to serve the file to /// \param path path of the file to serve void serveFile(int clientfd, const char *path) { struct _IO_FILE *fp; unsigned long numBytesReadFile; fp = fopen(path, "r"); char fileBuff[1024]; if (fp) { printf("\nGot a fp\n"); while ((numBytesReadFile = fread(fileBuff, 1023, 1, fp)) > 0) { write(clientfd, fileBuff, strlen(fileBuff)); } Loading @@ -21,6 +24,9 @@ void serveFile(int clientfd, const char *path) { fclose(fp); } /// Checks if a file exists /// \param fileName file name to check /// \return Will return -1 if doesn't exist, otherwise 0 int checkFile(char *fileName) { return access(fileName, R_OK); } No newline at end of file http.c +1 −1 Original line number Diff line number Diff line Loading @@ -30,7 +30,7 @@ void get_http_path(char *request, char *path) { } /// /// Will parse a header /// \param header /// \return int parseHeader(char *header) { Loading http.h +1 −0 Original line number Diff line number Diff line Loading @@ -4,6 +4,7 @@ #ifndef COMS352_PROJECT2_PART2_HTTP_H #define COMS352_PROJECT2_PART2_HTTP_H // Struct of methods used on this project typedef enum { HTTP_GET, HTTP_HEAD, Loading network.c +20 −0 Original line number Diff line number Diff line Loading @@ -33,35 +33,45 @@ int accept_connection(int socketfd) { /// \param clientfd file descriptor of the client /// \return int handle_client(int clientfd) { /// Fork int pid = fork(); if (pid == 0) { // Setup memory GrowthBuffer request; getRequest(clientfd, &request); // Get verb HTTP_VERB verb = get_http_verb(request.array); // We don't how to handle stuff other than HEAD and GET if (verb == HTTP_UNKNOWN) { char *basicHeader = "HTTP/1.1 400 Bad Request\r\n\r\n"; write(clientfd, basicHeader, strlen(basicHeader)); shutdown(clientfd, SHUT_RDWR); exit(EXIT_SUCCESS); } // Get the path char *path = malloc(sizeof(char) * 1024); get_http_path(request.array, path); fprintf(stdout, "Verb:%d Path:%s\n", verb, path); // Setup for getting the response GrowthBuffer response; initBuffer(&response, 5000); // If the exsits return 200 if (checkFile(path) != -1) { char *basicHeader = "HTTP/1.1 200 OK\r\n\r\n"; appendBuffer(&response, basicHeader, strlen(basicHeader)); write(clientfd, response.array, strlen(response.array)); // If it is a get request return the contents of the file if (verb == HTTP_GET) { serveFile(clientfd, path); } } else { // Didn't find the file serve 4040 char *basicHeader = "HTTP/1.1 404 Not Found\r\n\r\n"; appendBuffer(&response, basicHeader, strlen(basicHeader)); write(clientfd, response.array, strlen(response.array)); } // Clean up and exit shutdown(clientfd, SHUT_RDWR); free(path); freeBuffer(&request); Loading Loading @@ -98,22 +108,32 @@ int bind_socket() { return socketfd; } /// Get request from client /// /// \param clientfd client to recive from /// \param request buffer to send request back in void getRequest(int clientfd, GrowthBuffer *request) { struct pollfd pollfd = { .fd = clientfd, .events = POLLIN }; ssize_t readCount; // Setup memory initBuffer(request, 500); char *buff = malloc(1024); // Poll poll(&pollfd, 1, 0); while (pollfd.revents & POLLIN) { // Read readCount = read(clientfd, buff, 1024); // Append bytes read to the buffer appendBuffer(request, buff, readCount); // Didn't read anything if (readCount <= 0) { break; } // Poll again poll(&pollfd, 1, 0); } } No newline at end of file Loading
file.c +7 −1 Original line number Diff line number Diff line Loading @@ -7,13 +7,16 @@ #include <memory.h> #include <fcntl.h> #include "file.h" /// Serves the given file to the client /// \param clientfd client to serve the file to /// \param path path of the file to serve void serveFile(int clientfd, const char *path) { struct _IO_FILE *fp; unsigned long numBytesReadFile; fp = fopen(path, "r"); char fileBuff[1024]; if (fp) { printf("\nGot a fp\n"); while ((numBytesReadFile = fread(fileBuff, 1023, 1, fp)) > 0) { write(clientfd, fileBuff, strlen(fileBuff)); } Loading @@ -21,6 +24,9 @@ void serveFile(int clientfd, const char *path) { fclose(fp); } /// Checks if a file exists /// \param fileName file name to check /// \return Will return -1 if doesn't exist, otherwise 0 int checkFile(char *fileName) { return access(fileName, R_OK); } No newline at end of file
http.c +1 −1 Original line number Diff line number Diff line Loading @@ -30,7 +30,7 @@ void get_http_path(char *request, char *path) { } /// /// Will parse a header /// \param header /// \return int parseHeader(char *header) { Loading
http.h +1 −0 Original line number Diff line number Diff line Loading @@ -4,6 +4,7 @@ #ifndef COMS352_PROJECT2_PART2_HTTP_H #define COMS352_PROJECT2_PART2_HTTP_H // Struct of methods used on this project typedef enum { HTTP_GET, HTTP_HEAD, Loading
network.c +20 −0 Original line number Diff line number Diff line Loading @@ -33,35 +33,45 @@ int accept_connection(int socketfd) { /// \param clientfd file descriptor of the client /// \return int handle_client(int clientfd) { /// Fork int pid = fork(); if (pid == 0) { // Setup memory GrowthBuffer request; getRequest(clientfd, &request); // Get verb HTTP_VERB verb = get_http_verb(request.array); // We don't how to handle stuff other than HEAD and GET if (verb == HTTP_UNKNOWN) { char *basicHeader = "HTTP/1.1 400 Bad Request\r\n\r\n"; write(clientfd, basicHeader, strlen(basicHeader)); shutdown(clientfd, SHUT_RDWR); exit(EXIT_SUCCESS); } // Get the path char *path = malloc(sizeof(char) * 1024); get_http_path(request.array, path); fprintf(stdout, "Verb:%d Path:%s\n", verb, path); // Setup for getting the response GrowthBuffer response; initBuffer(&response, 5000); // If the exsits return 200 if (checkFile(path) != -1) { char *basicHeader = "HTTP/1.1 200 OK\r\n\r\n"; appendBuffer(&response, basicHeader, strlen(basicHeader)); write(clientfd, response.array, strlen(response.array)); // If it is a get request return the contents of the file if (verb == HTTP_GET) { serveFile(clientfd, path); } } else { // Didn't find the file serve 4040 char *basicHeader = "HTTP/1.1 404 Not Found\r\n\r\n"; appendBuffer(&response, basicHeader, strlen(basicHeader)); write(clientfd, response.array, strlen(response.array)); } // Clean up and exit shutdown(clientfd, SHUT_RDWR); free(path); freeBuffer(&request); Loading Loading @@ -98,22 +108,32 @@ int bind_socket() { return socketfd; } /// Get request from client /// /// \param clientfd client to recive from /// \param request buffer to send request back in void getRequest(int clientfd, GrowthBuffer *request) { struct pollfd pollfd = { .fd = clientfd, .events = POLLIN }; ssize_t readCount; // Setup memory initBuffer(request, 500); char *buff = malloc(1024); // Poll poll(&pollfd, 1, 0); while (pollfd.revents & POLLIN) { // Read readCount = read(clientfd, buff, 1024); // Append bytes read to the buffer appendBuffer(request, buff, readCount); // Didn't read anything if (readCount <= 0) { break; } // Poll again poll(&pollfd, 1, 0); } } No newline at end of file