cancel download [needs testing]
This commit is contained in:
+15
-1
@@ -293,7 +293,21 @@ void file_download(session *sess) {
|
||||
int source_d = sess->fd;
|
||||
int dest_d = sess->sd;
|
||||
char buf[INBUFSIZE];
|
||||
int rlen = read(source_d, buf, INBUFSIZE);
|
||||
int rlen = read(sess->sd, buf, INBUFSIZE);
|
||||
if (rlen > 0) {
|
||||
char *line;
|
||||
query_extract_from_buf_2(buf, &rlen, &line);
|
||||
if (!strcmp(line, "cancel\n")) {
|
||||
free(line);
|
||||
/* replace session buffer with the rest of the local buffer */
|
||||
memcpy(sess->buf, buf, rlen);
|
||||
sess->buf_used = rlen;
|
||||
clear_file_from_sess(sess);
|
||||
sess->state = OP_WAIT;
|
||||
return;
|
||||
}
|
||||
}
|
||||
rlen = read(source_d, buf, INBUFSIZE);
|
||||
if (rlen == 0) {
|
||||
if (sess->file->rest) {
|
||||
print_log(stdout, pl_error, "Error downloading file %s!\n",
|
||||
|
||||
@@ -5,7 +5,6 @@
|
||||
#include "server.h"
|
||||
#include "utils.h"
|
||||
#include <db.h>
|
||||
#include <stdio.h>
|
||||
|
||||
/* *** MAIN *** */
|
||||
int main(int argc, char *argv[]) {
|
||||
|
||||
@@ -31,6 +31,11 @@ int32_t accept_client(int ls, session *connections[], char *wm) {
|
||||
perror("accept");
|
||||
return -1;
|
||||
}
|
||||
/* set non-blocking behaviour */
|
||||
int flags = fcntl(sd, F_GETFL, 0);
|
||||
flags = flags | O_NONBLOCK;
|
||||
fcntl(sd, F_SETFL, flags);
|
||||
|
||||
session *sess = make_new_session(sd, &addr, wm);
|
||||
print_log(stdout, pl_info, "New connection: %s:%u\n",
|
||||
inet_ntoa(addr.sin_addr), ntohs(addr.sin_port));
|
||||
@@ -135,6 +140,49 @@ int query_extract_from_buf(session *sess, char **output_line) {
|
||||
return pos + 1;
|
||||
}
|
||||
|
||||
int query_extract_from_buf_2(char *buf, int *buf_used, char **output_line) {
|
||||
char *line;
|
||||
int pos = -1;
|
||||
int _buf_used = *buf_used;
|
||||
|
||||
if (_buf_used > 0) {
|
||||
char *cptr = strchr(buf, '\n');
|
||||
if (cptr != NULL)
|
||||
pos = cptr - buf;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (pos == -1) {
|
||||
int b_used = _buf_used;
|
||||
*buf_used = 0;
|
||||
line = malloc(b_used + 1);
|
||||
strncpy(line, buf, b_used);
|
||||
|
||||
line[b_used] = 0;
|
||||
|
||||
*output_line = line;
|
||||
return b_used;
|
||||
} else {
|
||||
line = malloc(pos + 2);
|
||||
strncpy(line, buf, pos + 1);
|
||||
line[++pos] = 0;
|
||||
_buf_used -= (pos);
|
||||
if (!pos)
|
||||
pos++;
|
||||
memmove(buf, buf + pos, _buf_used);
|
||||
buf[_buf_used] = 0;
|
||||
if (line[pos - 2] == '\r') {
|
||||
line[pos - 2] = line[pos - 1];
|
||||
pos--;
|
||||
line[pos - 1] = 0;
|
||||
}
|
||||
*output_line = line;
|
||||
*buf_used = _buf_used;
|
||||
}
|
||||
return pos + 1;
|
||||
}
|
||||
|
||||
void perform_session_action(session *sess, char *line, server_data_t *s_d) {
|
||||
int state = sess->state;
|
||||
int res;
|
||||
|
||||
@@ -10,6 +10,7 @@ int query_extract_from_buf(session *sess, char **output_line);
|
||||
int session_do_read(session *sess, char **read_str);
|
||||
void perform_session_action(session *sess, char *line, server_data_t *s_d);
|
||||
void close_session(session *connections[], int sd);
|
||||
int query_extract_from_buf_2(char *buf, int *buf_used, char **output_line);
|
||||
/* session *make_new_session(int fd, struct sockaddr_in *from, char *wm); */
|
||||
/* int query_extract_from_sess(session *sess, char **output_line); */
|
||||
#endif
|
||||
Reference in New Issue
Block a user