swps

Static Web Page Server
git clone git://git.noxz.tech/swps
Log | Files | Refs | LICENSE

commit d539de6ee304b869bcfe4126aaec022c7a200b39
parent 279c8148e8c823d4798c059230fdb96f1f694caf
Author: Chris Noxz <chris@noxz.tech>
Date:   Sun,  4 Aug 2019 23:09:25 +0200

Handle broken pipe with SIG_IGN for now. Maybe use fork() in future

Diffstat:
Mswps.c | 23++++++++++++++++++-----
1 file changed, 18 insertions(+), 5 deletions(-)

diff --git a/swps.c b/swps.c @@ -44,6 +44,7 @@ #define REDIRECT 301 #define FORBIDDEN 403 #define NOTFOUND 404 +#define BROKEN_PIPE 1001 /* functions */ static void die(int, const char *, ...); @@ -131,6 +132,8 @@ die(int n, const char *msg, ...) exit(n); } +#include <errno.h> + int respond(int type, int fd, unsigned char *ip, const char *pt) { @@ -150,22 +153,26 @@ respond(int type, int fd, unsigned char *ip, const char *pt) l = (long)lseek(filefd, (off_t)0, SEEK_END); lseek(filefd, (off_t)0, SEEK_SET); sprintf(buffer, response_200, date, server, l); - write(fd, buffer, strlen(buffer)); + if (write(fd, buffer, strlen(buffer)) == -1) + return BROKEN_PIPE; while ((l = read(filefd, buffer, BUFSIZE)) > 0) write(fd, buffer, l); close(filefd); break; case REDIRECT: sprintf(buffer, response_301, date, server); - write(fd, buffer, strlen(buffer)); + if (write(fd, buffer, strlen(buffer)) == -1) + return BROKEN_PIPE; break; case FORBIDDEN: sprintf(buffer, response_403, date, server); - write(fd, buffer, strlen(buffer)); + if (write(fd, buffer, strlen(buffer)) == -1) + return BROKEN_PIPE; break; case NOTFOUND: sprintf(buffer, response_404, date, server); - write(fd, buffer, strlen(buffer)); + if (write(fd, buffer, strlen(buffer)) == -1) + return BROKEN_PIPE; break; } @@ -228,6 +235,8 @@ serve(int serverfd) die(1, "Error: system call 'signal'\n"); if (signal(SIGTERM, &stopserver) == SIG_ERR) die(1, "Error: system call 'signal'\n"); + if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) + die(1, "Error: system call 'signal'\n"); /* ...and server forever */ for (;;) { @@ -235,7 +244,11 @@ serve(int serverfd) if ((clientfd = accept(serverfd, (struct sockaddr *)&c_addr, &l)) < 0) die(1, "Error: system call 'accept'\n"); ip = (unsigned char *)(&c_addr.sin_addr.s_addr); - handle(clientfd, ip); + switch (handle(clientfd, ip)) { + case BROKEN_PIPE: + die(-1, "Error: handling request 'broken pipe'\n"); + break; + } close(clientfd); } }