From aa77d07f75d04a9635ae5db353ac94e0de7c7af4 Mon Sep 17 00:00:00 2001 From: Sean McBride Date: Tue, 4 May 2021 23:03:57 -0400 Subject: [PATCH] refactor: client socket --- runtime/include/client_socket.h | 80 ++------------------------------- runtime/src/client_socket.c | 78 ++++++++++++++++++++++++++++++++ runtime/src/sandbox.c | 1 + 3 files changed, 82 insertions(+), 77 deletions(-) create mode 100644 runtime/src/client_socket.c diff --git a/runtime/include/client_socket.h b/runtime/include/client_socket.h index bab817a..974d04e 100644 --- a/runtime/include/client_socket.h +++ b/runtime/include/client_socket.h @@ -1,81 +1,7 @@ #pragma once -#include -#include -#include -#include +#include -#include "panic.h" -#include "debuglog.h" -#include "http_response.h" -#include "http_total.h" -#include "runtime.h" -#include "worker_thread.h" +void client_socket_close(int client_socket, struct sockaddr *client_address); - -static inline void -client_socket_close(int client_socket, struct sockaddr *client_address) -{ - /* Should never close 0, 1, or 2 */ - assert(client_socket != STDIN_FILENO); - assert(client_socket != STDOUT_FILENO); - assert(client_socket != STDERR_FILENO); - - - if (unlikely(close(client_socket) < 0)) { - char client_address_text[INET6_ADDRSTRLEN] = {}; - if (unlikely(inet_ntop(AF_INET, &client_address, client_address_text, INET6_ADDRSTRLEN) == NULL)) { - debuglog("Failed to log client_address: %s", strerror(errno)); - } - debuglog("Error closing client socket %d associated with %s - %s", client_socket, client_address_text, - strerror(errno)); - } -} - - -/** - * Rejects request due to admission control or error - * @param client_socket - the client we are rejecting - * @param status_code - either 503 or 400 - */ -static inline int -client_socket_send(int client_socket, int status_code) -{ - const char *response; - int rc; - switch (status_code) { - case 503: - response = HTTP_RESPONSE_503_SERVICE_UNAVAILABLE; - http_total_increment_5XX(); - break; - case 400: - response = HTTP_RESPONSE_400_BAD_REQUEST; - http_total_increment_4XX(); - break; - default: - panic("%d is not a valid status code\n", status_code); - } - - int sent = 0; - int to_send = strlen(response); - - while (sent < to_send) { - rc = write(client_socket, &response[sent], to_send - sent); - if (rc < 0) { - if (errno == EAGAIN) { debuglog("Unexpectedly blocking on write of %s\n", response); } - - debuglog("Error with %s\n", strerror(errno)); - - goto send_err; - } - sent += rc; - }; - - rc = 0; -done: - return rc; -send_err: - debuglog("Error sending to client: %s", strerror(errno)); - rc = -1; - goto done; -} +int client_socket_send(int client_socket, int status_code); diff --git a/runtime/src/client_socket.c b/runtime/src/client_socket.c new file mode 100644 index 0000000..f545533 --- /dev/null +++ b/runtime/src/client_socket.c @@ -0,0 +1,78 @@ +#include +#include +#include +#include +#include + +#include "client_socket.h" +#include "debuglog.h" +#include "http_response.h" +#include "http_total.h" +#include "likely.h" +#include "panic.h" +#include "worker_thread.h" + +void +client_socket_close(int client_socket, struct sockaddr *client_address) +{ + /* Should never close 0, 1, or 2 */ + assert(client_socket != STDIN_FILENO); + assert(client_socket != STDOUT_FILENO); + assert(client_socket != STDERR_FILENO); + + if (unlikely(close(client_socket) < 0)) { + char client_address_text[INET6_ADDRSTRLEN] = {}; + if (unlikely(inet_ntop(AF_INET, &client_address, client_address_text, INET6_ADDRSTRLEN) == NULL)) { + debuglog("Failed to log client_address: %s", strerror(errno)); + } + debuglog("Error closing client socket %d associated with %s - %s", client_socket, client_address_text, + strerror(errno)); + } +} + +/** + * Rejects request due to admission control or error + * @param client_socket - the client we are rejecting + * @param status_code - either 503 or 400 + */ +int +client_socket_send(int client_socket, int status_code) +{ + const char *response; + int rc; + switch (status_code) { + case 503: + response = HTTP_RESPONSE_503_SERVICE_UNAVAILABLE; + http_total_increment_5XX(); + break; + case 400: + response = HTTP_RESPONSE_400_BAD_REQUEST; + http_total_increment_4XX(); + break; + default: + panic("%d is not a valid status code\n", status_code); + } + + int sent = 0; + int to_send = strlen(response); + + while (sent < to_send) { + rc = write(client_socket, &response[sent], to_send - sent); + if (rc < 0) { + if (errno == EAGAIN) { debuglog("Unexpectedly blocking on write of %s\n", response); } + + debuglog("Error with %s\n", strerror(errno)); + + goto send_err; + } + sent += rc; + }; + + rc = 0; +done: + return rc; +send_err: + debuglog("Error sending to client: %s", strerror(errno)); + rc = -1; + goto done; +} diff --git a/runtime/src/sandbox.c b/runtime/src/sandbox.c index 236e785..2ab114c 100644 --- a/runtime/src/sandbox.c +++ b/runtime/src/sandbox.c @@ -1,3 +1,4 @@ +#include #include #include #include