QUOTE: Kindness is always fashionable.

fix: Guarded http_read() from blocking with poll() - libhttp - A basic HTTP Framework

libhttp

A basic HTTP Framework
git clone git://192.168.2.2/libhttp
Log | Files | Refs | README

commit 5220ab46e9cafb10f14b233b25ed0c4941fbe7ec
parent 2e7bac568e85445338d346402027e5ec7edd3a94
Author: Sophie <sophie@aest.me>
Date:   Thu, 31 Oct 2024 13:46:12 +0100

fix: Guarded http_read() from blocking with poll()

Diffstat:
Msrc/libhttp.h | 12+++++++++---
1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/src/libhttp.h b/src/libhttp.h @@ -332,10 +332,16 @@ const char *http_status_str(http_status_t status) { } ssize_t http_read(http_conn_t *conn, char *buffer, size_t len) { - if (conn->ssl != NULL) { - return SSL_read(conn->ssl, buffer, len); + struct pollfd fds[1]; + fds[0].fd = conn->sockfd; + fds[0].events = POLLIN | POLLPRI; + if (poll(fds, 1, 100) && fds[0].revents & POLLIN) { + if (conn->ssl != NULL) { + return SSL_read(conn->ssl, buffer, len); + } + return read(conn->sockfd, buffer, len); } - return read(conn->sockfd, buffer, len); + return 0; } ssize_t http_write(http_conn_t *conn, char *buffer, size_t len) {