diff options
author | cflip <cflip@cflip.net> | 2023-07-09 11:57:13 -0600 |
---|---|---|
committer | cflip <cflip@cflip.net> | 2023-07-09 11:57:13 -0600 |
commit | c446378817e4d215ffd69f452f846fc5278f7943 (patch) | |
tree | da4b68ef6f1fabf532100b6d23509881008e1bd3 /net.c | |
parent | b67db2af4c3fd5bc8c612c6348c78323f5bf4b48 (diff) |
Gracefully shut down server when interrupted with Ctrl+C
Diffstat (limited to 'net.c')
-rw-r--r-- | net.c | 16 |
1 files changed, 12 insertions, 4 deletions
@@ -1,6 +1,7 @@ #include "net.h" #include <arpa/inet.h> +#include <errno.h> #include <netdb.h> #include <stdio.h> #include <stdlib.h> @@ -50,19 +51,26 @@ int net_init_server(int port) return sockfd; } -struct http_request net_next_request(int serverfd, int *clientfd) +int net_next_request(int serverfd, int *clientfd, struct http_request *req) { int connfd; char readbuf[CFWS_NET_MAXREAD]; - struct http_request req; connfd = accept(serverfd, NULL, NULL); + if (connfd == -1) { + /* EINVAL most likely indicates that the server socket was + * closed due to the user pressing Ctrl+C. */ + if (errno == EINVAL) + return 1; + perror("cfws: accept"); + return -1; + } /* Read and parse the HTTP request */ memset(readbuf, 0, CFWS_NET_MAXREAD); read(connfd, readbuf, CFWS_NET_MAXREAD - 1); - req = http_parse_request(readbuf); + *req = http_parse_request(readbuf); *clientfd = connfd; - return req; + return 0; } |