From b8b244679f637df1056b5ffb9400c6f765e033d3 Mon Sep 17 00:00:00 2001 From: Nick Krichevsky Date: Mon, 10 Sep 2018 00:09:28 -0400 Subject: [PATCH] Fix issues with premature socket closing --- common/socket_helper.c | 6 ++++++ server/http_server.c | 8 +++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/common/socket_helper.c b/common/socket_helper.c index 9f07886..ba80e37 100644 --- a/common/socket_helper.c +++ b/common/socket_helper.c @@ -233,6 +233,12 @@ enum socket_result get_all_remote_parts(int socket_fd, enum socket_type type, st } write_offset = current_result_size; } + // If we never got a blank line, the message never finished. + if (!have_blank_line) { + free(buffer); + free(result); + return RESULT_MALFORMED; + } struct socket_read_info read_info = get_read_info(message->headers); char *body_result; if (read_info.strategy == STRATEGY_UNDEFINED) { diff --git a/server/http_server.c b/server/http_server.c index 765da0f..322c843 100644 --- a/server/http_server.c +++ b/server/http_server.c @@ -13,7 +13,12 @@ bool running = true; void handle_signal(int signal) { - running = false; + if (signal == SIGPIPE) { + return; + } + if (signal == SIGINT || signal == SIGTERM) { + running = false; + } } int main(int argc, char* argv[]) { @@ -30,6 +35,7 @@ int main(int argc, char* argv[]) { sigemptyset(&sa.sa_mask); sigaction(SIGINT, &sa, NULL); sigaction(SIGTERM, &sa, NULL); + sigaction(SIGPIPE, &sa, NULL); struct server_info info = setup(port_num); if (info.status == STATUS_ERROR) {