-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a78ec41
commit 7a2e084
Showing
6 changed files
with
179 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
# include <ft_p.h> | ||
|
||
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"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# include <ft_p.h> | ||
|
||
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters