improve conn_send_file

Signed-off-by: Jianhui Zhao <zhaojh329@gmail.com>
main^2
Jianhui Zhao 2021-01-06 01:02:59 +08:00
parent b0bed67434
commit de6bd1bf92
2 changed files with 6 additions and 3 deletions

View File

@ -62,13 +62,16 @@ static void conn_send(struct uh_connection *conn, const void *data, ssize_t len)
ev_io_start(conni->srv->loop, &conni->iow); ev_io_start(conni->srv->loop, &conni->iow);
} }
static void conn_send_file(struct uh_connection *conn, const char *path, size_t offset, size_t len) static void conn_send_file(struct uh_connection *conn, const char *path, size_t offset, ssize_t len)
{ {
struct uh_connection_internal *conni = (struct uh_connection_internal *)conn; struct uh_connection_internal *conni = (struct uh_connection_internal *)conn;
size_t min = 8192; size_t min = 8192;
struct stat st; struct stat st;
int fd; int fd;
if (len == 0)
return;
fd = open(path, O_RDONLY); fd = open(path, O_RDONLY);
if (fd < 0) { if (fd < 0) {
uh_log_err("open: %s\n", strerror(errno)); uh_log_err("open: %s\n", strerror(errno));
@ -85,7 +88,7 @@ static void conn_send_file(struct uh_connection *conn, const char *path, size_t
lseek(fd, offset, SEEK_SET); lseek(fd, offset, SEEK_SET);
st.st_size -= offset; st.st_size -= offset;
if (len == 0 || len > st.st_size) if (len < 0 || len > st.st_size)
len = st.st_size; len = st.st_size;
/* If the file is not greater than 8K, then append it to the HTTP head, send once */ /* If the file is not greater than 8K, then append it to the HTTP head, send once */

View File

@ -56,7 +56,7 @@ struct uh_connection {
*/ */
void (*done)(struct uh_connection *conn); void (*done)(struct uh_connection *conn);
void (*send)(struct uh_connection *conn, const void *data, ssize_t len); void (*send)(struct uh_connection *conn, const void *data, ssize_t len);
void (*send_file)(struct uh_connection *conn, const char *path, size_t offset, size_t len); void (*send_file)(struct uh_connection *conn, const char *path, size_t offset, ssize_t len);
void (*printf)(struct uh_connection *conn, const char *format, ...); void (*printf)(struct uh_connection *conn, const char *format, ...);
void (*vprintf)(struct uh_connection *conn, const char *format, va_list arg); void (*vprintf)(struct uh_connection *conn, const char *format, va_list arg);
void (*send_status_line)(struct uh_connection *conn, int code, const char *extra_headers); void (*send_status_line)(struct uh_connection *conn, int code, const char *extra_headers);