fix: Misuse of sendfile when SSL is enabled

Signed-off-by: Jianhui Zhao <zhaojh329@gmail.com>
main^2
Jianhui Zhao 2021-03-01 17:10:09 +08:00
parent e281f44bde
commit c35cee771c
1 changed files with 29 additions and 12 deletions

View File

@ -107,6 +107,10 @@ static void conn_send_file(struct uh_connection *conn, const char *path, off_t o
} else { } else {
conni->file.size = len; conni->file.size = len;
conni->file.fd = fd; conni->file.fd = fd;
#if UHTTPD_SSL_SUPPORT
if (conni->ssl)
fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) | O_NONBLOCK);
#endif
} }
ev_io_start(conni->srv->loop, &conni->iow); ev_io_start(conni->srv->loop, &conni->iow);
@ -634,22 +638,35 @@ static void conn_write_cb(struct ev_loop *loop, struct ev_io *w, int revents)
if (buffer_length(&conn->wb) == 0) { if (buffer_length(&conn->wb) == 0) {
if (conn->file.fd > 0) { if (conn->file.fd > 0) {
ret = sendfile(w->fd, conn->file.fd, NULL, conn->file.size); #if UHTTPD_SSL_SUPPORT
if (ret < 0) { if (conn->ssl) {
if (errno != EAGAIN) { bool eof;
uh_log_err("write error: %s\n", strerror(errno)); if (buffer_put_fd(&conn->wb, conn->file.fd, 8192, &eof) < 0 || eof) {
conn_free(conn); close(conn->file.fd);
conn->file.fd = -1;
} }
return; return;
} } else {
#endif
ret = sendfile(w->fd, conn->file.fd, NULL, conn->file.size);
if (ret < 0) {
if (errno != EAGAIN) {
uh_log_err("write error: %s\n", strerror(errno));
conn_free(conn);
}
return;
}
if (ret < conn->file.size) { if (ret < conn->file.size) {
conn->file.size -= ret; conn->file.size -= ret;
return; return;
} }
close(conn->file.fd); close(conn->file.fd);
conn->file.fd = -1; conn->file.fd = -1;
#if UHTTPD_SSL_SUPPORT
}
#endif
} }
if (conn->flags & CONN_F_SEND_AND_CLOSE) { if (conn->flags & CONN_F_SEND_AND_CLOSE) {