diff --git a/inc/ft_p.h b/inc/ft_p.h index 24e5ec7..966527c 100644 --- a/inc/ft_p.h +++ b/inc/ft_p.h @@ -30,6 +30,7 @@ */ void ft_server_put(int fd, char **argv); +void ft_server_get(int fd, char **argv); void ft_cd(int fd, char **pptr); void ft_invalid(int fd); void ft_handle_request(char *line, int fd); @@ -39,6 +40,7 @@ void ft_handle_request(char *line, int fd); */ void ft_client_put(int fd, char **argv); +void ft_client_get(int fd, char **argv); void ft_lcd(int fd, char **pptr); int ft_lhandle_request(char *line, int fd); int ft_handle_special(char *line, int connfd); diff --git a/src/client/get.c b/src/client/get.c new file mode 100644 index 0000000..b3acb83 --- /dev/null +++ b/src/client/get.c @@ -0,0 +1,116 @@ +# include + +static int ft_wfile_exists(char *res) +{ + char **argv; + + if ((argv = ft_get_argv(res))) + { + if (argv[1]) + { + return (ft_file_exists(argv[1])); + } + } + return (TRUE); +} + +void ft_save_file(int fd, char *res) +{ + char **argv; + char *buff; + int local_fd; + + if ((argv = ft_get_argv(res))) + { + if (argv[1] && argv[2]) + { + if ((buff = ft_strnew(ft_atoi(argv[2])))) + { + /* + ** read file contents + */ + ft_wreadn(fd, buff, ft_atoi(argv[2])); + /* + ** create file locally + */ + if ((local_fd = open(argv[1], O_WRONLY | O_APPEND | O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO)) != -1) + { + /* + ** write to file + */ + ft_wwriten(local_fd, buff, ft_atoi(argv[2])); + ft_putendl("ft_p: download complete"); + close(local_fd); + } + else + ft_putendl("ft_p: failed to open a file for writing locally"); + ft_strdel(&buff); + } + } + } +} + +void ft_client_get(int fd, char **argv) +{ + char *req; + char *res; + + /* + ** check that a file was specified + */ + if (argv[1]) + { + /* + ** build server request + */ + if ((req = ft_strdup("get "))) + { + req = ft_wstrjoin(req, argv[1]); + req = ft_wstrjoin(req, "\n"); + /* + ** send request to server + */ + ft_send_response(req, fd); + /* + ** read server response into buff + */ + if ((res = ft_wreadline(fd))) + { + /* + ** server response should be success or error msg + ** followed by filename and size + */ + if (ft_strnequ(res, "OK", 1)) + { + /* + ** check if the filename exists locally + */ + if (!ft_wfile_exists(res)) + { + /* + ** send server request to begin send + */ + ft_send_response("OK\n", fd); + /* + ** save file locally + */ + ft_save_file(fd, res); + } + else + { + ft_send_response("KO", fd); + ft_putendl("ft_p: filename is taken locally"); + } + } + else + ft_putendl("ft_p: file specified does not exist"); + } + else + ft_putendl("ft_p: error recieving file length from server"); + } + else + ft_putendl("ft_p: call to malloc failed"); + } + else + ft_putendl("ft_p: file not specified"); +} \ No newline at end of file diff --git a/src/client/handle_special.c b/src/client/handle_special.c index b2c1c59..22036df 100644 --- a/src/client/handle_special.c +++ b/src/client/handle_special.c @@ -17,7 +17,7 @@ int ft_handle_special(char *line, int connfd) if (ft_strequ(argv[0], "put")) ft_client_put(connfd, argv); else if (ft_strequ(argv[0], "get")) - ft_client_put(connfd, argv); + ft_client_get(connfd, argv); /* ** tell client if the command hs been handled here */ diff --git a/src/server/get.c b/src/server/get.c new file mode 100644 index 0000000..f63ac75 --- /dev/null +++ b/src/server/get.c @@ -0,0 +1,56 @@ +# include + +void ft_server_get(int connfd, char **argv) +{ + // int fd; + t_buffer *buffer; + char *req; + char *res; + + if (argv[1]) + { + /* + ** check if the file requested exists on the server + */ + if (ft_file_exists(argv[1])) + { + if ((buffer = ft_set_buffer(argv[1]))) + { + req = ft_strjoin("OK ", buffer->_filename); + req = ft_wstrjoin(req, " "); + req = ft_wstrjoin(req, ft_itoa(buffer->_size)); //mem leak here + req = ft_wstrjoin(req, "\n"); + /* + ** send response (success and filename) + */ + ft_send_response(req, connfd); + /* + ** read from client res on whether to continue + */ + if ((res = ft_wreadline(connfd))) + { + /* + ** client is waiting for bytes + */ + if (ft_strequ(res, "OK")) + { + /* + ** send all bytes + */ + ft_wwriten(connfd, ft_get_chunk(buffer, buffer->_size), buffer->_size); + } + } + } + else + ft_send_response("KKKO\n", connfd); + } + else + { + ft_send_response("KO\n", connfd); + } + } + else + { + ft_send_response("KKO\n", connfd); + } +} \ No newline at end of file diff --git a/src/server/handle_request.c b/src/server/handle_request.c index 3f3d5d5..62f79cc 100644 --- a/src/server/handle_request.c +++ b/src/server/handle_request.c @@ -20,6 +20,8 @@ void ft_handle_request(char *line, int connfd) ft_ls(connfd); else if (ft_strequ(argv[0], "put")) ft_server_put(connfd, argv); + else if (ft_strequ(argv[0], "get")) + ft_server_get(connfd, argv); else ft_send_response("ft_p: \033[0;31minvalid command\n\033[0m", connfd); /* diff --git a/src/server/put.c b/src/server/put.c index e788a58..69e6dc6 100644 --- a/src/server/put.c +++ b/src/server/put.c @@ -33,9 +33,11 @@ void ft_server_put(int connfd, char **argv) { ft_wwriten(fd, buff, ft_atoi(argv[2])); ft_send_response("OK\n", connfd); + close(fd); } else ft_send_response("KO\n", connfd); + ft_strdel(&buff); } else ft_send_response("KO\n", connfd);