commit b5ec0c12b2b23718acb41bdc2af75829805b3411
parent 5220ab46e9cafb10f14b233b25ed0c4941fbe7ec
Author: Sophie <sophie@aest.me>
Date: Thu, 31 Oct 2024 22:43:15 +0100
fix: Made read() non-blocking
Diffstat:
1 file changed, 16 insertions(+), 9 deletions(-)
diff --git a/src/libhttp.h b/src/libhttp.h
@@ -165,6 +165,7 @@ void http_close(http_t *http);
#include <netdb.h>
#include <poll.h>
#include <signal.h>
+#include <fcntl.h>
#include <openssl/ssl.h>
#include <sys/socket.h>
@@ -332,16 +333,10 @@ const char *http_status_str(http_status_t status) {
}
ssize_t http_read(http_conn_t *conn, char *buffer, size_t 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);
+ if (conn->ssl != NULL) {
+ return SSL_read(conn->ssl, buffer, len);
}
- return 0;
+ return read(conn->sockfd, buffer, len);
}
ssize_t http_write(http_conn_t *conn, char *buffer, size_t len) {
@@ -610,6 +605,18 @@ void *http_handle_client(void *ptr) {
pthread_exit(0);
}
}
+ int flags = fcntl(conn->sockfd, F_GETFL, 0);
+ if (flags == -1) {
+ close(conn->sockfd);
+ free(conn);
+ pthread_exit(0);
+ }
+ flags |= O_NONBLOCK;
+ if (fcntl(conn->sockfd, F_SETFL, flags) == -1) {
+ close(conn->sockfd);
+ free(conn);
+ pthread_exit(0);
+ }
http_request_t request = http_read_request(conn);
if (request.method != NULL && request.url != NULL) {
if (conn->http->on_request_fn != NULL) {