diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..333348960c8b29853633da2f1500aadb28cef6ec --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +/.vscode +client +server +debug \ No newline at end of file diff --git a/get.c b/get.c new file mode 100644 index 0000000000000000000000000000000000000000..7d4b488f94a65cc5d907e503b26ca9d3c68cabb1 --- /dev/null +++ b/get.c @@ -0,0 +1,45 @@ +#include "get.h" +#include <stdio.h> +#include <string.h> + + +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'. + // printf("%s\n", req); + const char* path_start = strchr(req, ' ') + 1; + // printf("%c start\n", *path_start); + const char* path_end = strrchr(req, ' '); + // printf("%c final\n", *path_end); + // int path_len = path_start - path_end; + char path[path_end - path_start]; + strncpy(path, path_start, path_end - path_start); + path[sizeof(path)] = '\0'; // null terminator + // printf("root %s\n", root); + // printf("path %s\n", path); + char full_path[sizeof(path) + sizeof(root)]; + strncpy(full_path, root, sizeof(root)); + strcat(full_path, path); + // full_path[sizeof(full_path)-1] = 9; + printf("waiittt %s end\n", full_path); + find_file(full_path); +} + +void find_file(char* path_to_file) { + FILE *fp; + + fp = fopen(path_to_file, "r"); + if (fp != NULL) { + printf("success...\n");fclose(fp); + } else { + printf("fail\n"); + } + +} \ No newline at end of file diff --git a/get.h b/get.h new file mode 100644 index 0000000000000000000000000000000000000000..4fe299626dadfc6fc990850bd8b1d1b17a922b22 --- /dev/null +++ b/get.h @@ -0,0 +1,10 @@ +/* the include guards */ +#ifndef GET_H_INCLUDED +#define GET_H_INCLUDED + +/* Prototypes for the functions */ +void valid_get(char* buffer); +void parse(char* req, char* root); +void find_file(char* path_to_file); + +#endif \ No newline at end of file diff --git a/home/file.css b/home/file.css new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/makefile b/makefile index 922856f99cdf16c87bad40e259dd2aff75a765c8..30c7d65205f99434dd643ff2408b7dc3d2fa0583 100644 --- a/makefile +++ b/makefile @@ -1,5 +1,5 @@ server: server.c - gcc -o server server.c + gcc -o server server.c get.c client: client.c gcc -o client client.c diff --git a/server.c b/server.c index 35a9984f5f03e0e1e57c76d76ce67f874b381f88..74c0557e5a416e7111e57b29508a9c045b0e67d9 100644 --- a/server.c +++ b/server.c @@ -10,6 +10,7 @@ #include <netinet/in.h> #include <unistd.h> +#include "get.h" int main(int argc, char *argv[]) { @@ -19,18 +20,14 @@ int main(int argc, char *argv[]) socklen_t clilen; int n; - if (argc < 2) - { + if (argc < 2) { fprintf(stderr,"ERROR, no port provided\n"); exit(1); } /* Create TCP socket */ - sockfd = socket(AF_INET, SOCK_STREAM, 0); - - if (sockfd < 0) - { + if (sockfd < 0) { perror("ERROR opening socket"); exit(1); } @@ -43,64 +40,55 @@ int main(int argc, char *argv[]) /* Create address we're going to listen on (given port number) - converted to network byte order & any IP address for this machine */ - serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = INADDR_ANY; serv_addr.sin_port = htons(portno); // store in machine-neutral format /* Bind address to the socket */ - - if (bind(sockfd, (struct sockaddr *) &serv_addr, - sizeof(serv_addr)) < 0) - { + if (bind(sockfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) { perror("ERROR on binding"); exit(1); } /* Listen on socket - means we're ready to accept connections - incoming connection requests will be queued */ - listen(sockfd,5); - clilen = sizeof(cli_addr); /* 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) - { + if (newsockfd < 0) { perror("ERROR on accept"); exit(1); } bzero(buffer,256); - /* Read characters from the connection, then process */ - n = read(newsockfd,buffer,255); - if (n < 0) - { + if (n < 0) { perror("ERROR reading from socket"); exit(1); } + /* using the get.c functionality here: */ + char* root = argv[2]; + parse(buffer, root); + printf("Here is the message: %s\n",buffer); n = write(newsockfd,"I got your message",18); - if (n < 0) - { + if (n < 0) { perror("ERROR writing to socket"); exit(1); } /* close socket */ - close(sockfd); return 0;