diff --git a/.gitignore b/.gitignore index 333348960c8b29853633da2f1500aadb28cef6ec..17dcca995a7ef3b6f5d695c79ed15eca64a95c80 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,2 @@ -/.vscode -client -server -debug \ No newline at end of file +client.c +test/* \ No newline at end of file diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json new file mode 100644 index 0000000000000000000000000000000000000000..f73ff122ac5c5329ee0bf1794f97aaf777f306bc --- /dev/null +++ b/.vscode/c_cpp_properties.json @@ -0,0 +1,80 @@ +{ + "configurations": [ + { + "name": "Mac", + "includePath": [ + "/usr/include", + "/usr/local/include", + "${workspaceFolder}" + ], + "defines": [], + "intelliSenseMode": "clang-x64", + "browse": { + "path": [ + "/usr/include", + "/usr/local/include", + "${workspaceFolder}" + ], + "limitSymbolsToIncludedHeaders": true, + "databaseFilename": "" + }, + "macFrameworkPath": [ + "/System/Library/Frameworks", + "/Library/Frameworks" + ] + }, + { + "name": "Linux", + "includePath": [ + "/usr/include", + "/usr/local/include", + "${workspaceFolder}" + ], + "defines": [], + "intelliSenseMode": "clang-x64", + "browse": { + "path": [ + "/usr/include", + "/usr/local/include", + "${workspaceFolder}" + ], + "limitSymbolsToIncludedHeaders": true, + "databaseFilename": "" + } + }, + { + "name": "Win32", + "includePath": [ + "${workspaceFolder}", + "C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.12.25827/include/*", + "C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.12.25827/atlmfc/include/*", + "C:/Program Files (x86)/Windows Kits/10/Include/10.0.16299.0/um", + "C:/Program Files (x86)/Windows Kits/10/Include/10.0.16299.0/ucrt", + "C:/Program Files (x86)/Windows Kits/10/Include/10.0.16299.0/shared", + "C:/Program Files (x86)/Windows Kits/10/Include/10.0.16299.0/winrt" + ], + "defines": [ + "_DEBUG", + "UNICODE", + "_UNICODE" + ], + "intelliSenseMode": "msvc-x64", + "browse": { + "path": [ + "${workspaceFolder}", + "C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.12.25827/include/*", + "C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.12.25827/atlmfc/include/*", + "C:/Program Files (x86)/Windows Kits/10/Include/10.0.16299.0/um", + "C:/Program Files (x86)/Windows Kits/10/Include/10.0.16299.0/ucrt", + "C:/Program Files (x86)/Windows Kits/10/Include/10.0.16299.0/shared", + "C:/Program Files (x86)/Windows Kits/10/Include/10.0.16299.0/winrt" + ], + "limitSymbolsToIncludedHeaders": true, + "databaseFilename": "" + }, + "cStandard": "c11", + "cppStandard": "c++17" + } + ], + "version": 3 +} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000000000000000000000000000000000000..82a41b8159af0f0ad9deef625482dcc714e35b7c --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,10 @@ +{ + "C_Cpp.errorSquiggles": "Enabled", + "C_Cpp.intelliSenseEngineFallback": "Enabled", + "files.associations": { + "iostream": "c", + "get.h": "c", + "system_error": "c", + "ostream": "c" + } +} \ No newline at end of file diff --git a/client.c b/client.c index 50b076d3bf4b6e089c73fd540ad044caa5da7f00..7ed00887fc1d2fafbedb6649e8b5a60f70ab479e 100644 --- a/client.c +++ b/client.c @@ -67,7 +67,7 @@ int main(int argc, char* argv[]) if (connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0) { - perror("ERROR connecting"); + perror("ERROR connecting-"); exit(0); } diff --git a/get.c b/get.c index 9ee9056c16ef60894b3d75e763fb3037ee864730..c9137a4b27950f7e2a0d6364a037155247af2ddf 100644 --- a/get.c +++ b/get.c @@ -2,38 +2,40 @@ #include <stdio.h> #include <string.h> +#define FOUND 0 +#define NOT_FOUND -1 -void valid_get(char* buffer) { // checks if the request is valid - // const char* path_start = strchr(buffer, ' '); - // const char* path_end = strrchr(req, ' '); - - // printf("gucci\n"); -} - -void parse(char* req, char* root) { - // Extract just the file path from the request - // message into the char array 'path'. - const char* path_start = strchr(req, ' ') + 1; - const char* path_end = strrchr(req, ' '); +void get(char* req, char* root, FILE** content) { + // Extract just the file path from the request message into the char array 'path'. + const char* path_start = strstr(req, "GET") + 4; + const char* path_end = strstr(req, "HTTP")-1; char path[path_end - path_start]; + // int l = (path_end-path_start); + // printf("%c", l); strncpy(path, path_start, path_end - path_start); path[sizeof(path)] = '\0'; // null terminator - // printf("root %s\n", root); char full_path[sizeof(path) + sizeof(root)]; strcpy(full_path, root); strcat(full_path, path); - find_file(full_path); + printf("%s\n _____ \n", full_path); + if (find_file(full_path) == FOUND) open_file(content, full_path); + else *content = NULL; + } -void find_file(char* path_to_file) { +int find_file(char* path_to_file) { FILE *fp; - fp = fopen(path_to_file, "r"); if (fp != NULL) { - printf("success...\n"); fclose(fp); + printf("FOUND\n"); + return FOUND; } else { - printf("fail\n"); - } - + printf("NOT FOUND\n"); + return NOT_FOUND; + } +} + +void open_file(FILE** file, char* path_to_file) { + *file = fopen(path_to_file, "r"); } \ No newline at end of file diff --git a/get.h b/get.h index 4fe299626dadfc6fc990850bd8b1d1b17a922b22..9fa75eae1170ea17e1719e7806fdc894830d3e43 100644 --- a/get.h +++ b/get.h @@ -2,9 +2,11 @@ #ifndef GET_H_INCLUDED #define GET_H_INCLUDED +#include <stdio.h> + /* Prototypes for the functions */ -void valid_get(char* buffer); -void parse(char* req, char* root); -void find_file(char* path_to_file); +void get(char* req, char* root, FILE** content); // means everything incl header +int find_file(char* path_to_file); +void open_file(FILE** file, char* path_to_file); #endif \ No newline at end of file diff --git a/home/file.css b/home/file.css deleted file mode 100644 index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..0000000000000000000000000000000000000000 diff --git a/home/file.txt b/home/file.txt new file mode 100644 index 0000000000000000000000000000000000000000..60948a22a53227894fd089b16e0d54a05fdb94e2 --- /dev/null +++ b/home/file.txt @@ -0,0 +1,4 @@ +Line 1 +Line 2 +ok +. \ No newline at end of file diff --git a/makefile b/makefile index 30c7d65205f99434dd643ff2408b7dc3d2fa0583..08090383a27e1469d329d926e43f140507eef6d2 100644 --- a/makefile +++ b/makefile @@ -1,4 +1,4 @@ -server: server.c +server: server.c get.c gcc -o server server.c get.c client: client.c diff --git a/server.c b/server.c index 74c0557e5a416e7111e57b29508a9c045b0e67d9..e6a0c871e01ea281b384ec4ddb3877bf0cc7a2dc 100644 --- a/server.c +++ b/server.c @@ -2,6 +2,10 @@ * Server program based on sample code */ +#define FOUND_RESPONSE "HTTP/1.0 200 OK\n noicee\r\n" +#define NOT_FOUND_RESPONSE "HTTP/1.0 404 File Not Found noob feeder enjoy 4x report\r\n" +#define BUF 100000 + #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -9,13 +13,14 @@ #include <sys/socket.h> #include <netinet/in.h> #include <unistd.h> +#include <sys/sendfile.h> #include "get.h" int main(int argc, char *argv[]) { int sockfd, newsockfd, portno; - char buffer[256]; + char buffer[BUF]; struct sockaddr_in serv_addr, cli_addr; socklen_t clilen; int n; @@ -65,10 +70,10 @@ int main(int argc, char *argv[]) exit(1); } - bzero(buffer,256); + bzero(buffer,BUF); /* Read characters from the connection, then process */ - n = read(newsockfd,buffer,255); + n = read(newsockfd,buffer,BUF-1); if (n < 0) { perror("ERROR reading from socket"); @@ -77,11 +82,36 @@ int main(int argc, char *argv[]) /* using the get.c functionality here: */ char* root = argv[2]; - parse(buffer, root); + FILE* content = NULL; + ssize_t read; + get(buffer, root, &content); + if (content) { + // n = write(newsockfd, FOUND_RESPONSE, strlen(FOUND_RESPONSE)); + // if (n < 0) { + // perror("ERROR writing to socket"); + // exit(1); + // } + write(newsockfd, content, sizeof(content)); + // sendfile(newsockfd, fileno(content), NULL, 5000); + // close(fileno(content)); + // char* line = NULL; + // size_t len = 0; + // while ((read = getline(&line, &len, content)) != -1) { + // printf("%s", line); + // n = write(newsockfd,"I got your message",18); + // } + // n = write(newsockfd,"I got your mess3434age",18); + } else { + n = write(newsockfd, NOT_FOUND_RESPONSE, strlen(NOT_FOUND_RESPONSE)); + if (n < 0) { + perror("ERROR writing to socket"); + exit(1); + } + } printf("Here is the message: %s\n",buffer); - n = write(newsockfd,"I got your message",18); + // n = write(newsockfd,"I got your message",18); if (n < 0) { perror("ERROR writing to socket");