From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on gnuweeb.org X-Spam-Level: X-Spam-Status: No, score=-0.8 required=5.0 tests=ALL_TRUSTED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NO_DNS_FOR_FROM,URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.6 Received: from integral2.. (unknown [125.160.108.65]) by gnuweeb.org (Postfix) with ESMTPSA id 775187F948; Sun, 26 Jun 2022 05:56:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1656223014; bh=BAbY8SEx03bZVBm46BOEZfmgI34m6VnvN5S7m7WXvq0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=BnNtjvsTX81nC+uB+Lzi2egRv5oIoldZ0r8m6CEjfNkswM3w+gimhUxbd362SF5eH yzPtYbSrhqNkzCvjrhYvapvmgzqdD2sZYe/MWHB/bKq2FLlo42y5jwI3F/eceogdMs d4s2zimxvJFUoQpoTB3hMrOq1DsZoX5vOSrhlJPDJsBmPvrteqbPWERVY2DtAY/Pb+ mVFuyR68pXp36JQPm9apSOTNYl7hN0BcmI1BdVlxReCKPEDN80E0vQtIi9DqP5sgrk HectZ+nQfQre7HXtwDe2gaepjRi8COFhvDcx6BQR8Kx8JCODtOJ+2PZH7alzwGw94D E3Aou4JQw3pBA== From: Ammar Faizi To: GNU/Weeb Mailing List Cc: Ammar Faizi , Arthur Lapz , Sprite , Yonle Subject: [PATCH gwhttpd 3/7] gwhttpd: Replace `send_error_and_close` with `send_http_error()` Date: Sun, 26 Jun 2022 12:56:30 +0700 Message-Id: <20220626055634.676175-4-ammarfaizi2@gnuweeb.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220626055634.676175-1-ammarfaizi2@gnuweeb.org> References: <20220626055634.676175-1-ammarfaizi2@gnuweeb.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: Let the caller close the connection, we need to have more decisions in the caller rather than closing it directly. Signed-off-by: Ammar Faizi --- gwhttpd.cpp | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/gwhttpd.cpp b/gwhttpd.cpp index b01d5f1..afe4c3a 100644 --- a/gwhttpd.cpp +++ b/gwhttpd.cpp @@ -369,22 +369,17 @@ repeat: return ret; } -static void send_error_and_close(int code, struct client_sess *sess, - struct server_state *state) +static void send_http_error(int code, struct client_sess *sess) { char buf[128]; - ssize_t ret; int tmp; tmp = snprintf(buf, sizeof(buf), "HTTP/1.1 %d\r\n" "Content-Type: text/plain\r\n\r\n" - "Error %d", + "HTTP Error %d", code, code); - - ret = send_to_client(sess, buf, (size_t)tmp); - if (ret < 0) - close_sess(sess, state); + send_to_client(sess, buf, (size_t)tmp); } #define HTTP_200_HTML "HTTP/1.1 200\r\nContent-Type: text/html\r\n\r\n" @@ -434,7 +429,8 @@ static int handle_route_get(struct client_sess *sess, if (!strcmp(uri, "/hello")) return route_show_hello(sess, state); - send_error_and_close(404, sess, state); + send_http_error(404, sess); + close_sess(sess, state); return 0; } @@ -465,7 +461,8 @@ static int handle_route_post(struct client_sess *sess, if (!strcmp(uri, "/echo")) return route_show_echo(sess, state); - send_error_and_close(404, sess, state); + send_http_error(404, sess); + close_sess(sess, state); return 0; } @@ -481,14 +478,12 @@ static int handle_route(struct client_sess *sess, struct server_state *state) ret = handle_route_post(sess, state); break; default: - send_error_and_close(405, sess, state); + send_http_error(405, sess); + close_sess(sess, state); return 0; } - if (ret) - close_sess(sess, state); - - return 0; + return ret; } static int _handle_client(struct client_sess *sess, struct server_state *state) @@ -499,14 +494,20 @@ static int _handle_client(struct client_sess *sess, struct server_state *state) if (!sess->got_http_header) { ret = parse_http_header(sess); if (ret) { - send_error_and_close(400, sess, state); + send_http_error(400, sess); return 0; } if (!sess->got_http_header) return 0; } - return handle_route(sess, state); + ret = handle_route(sess, state); + if (ret) { + close_sess(sess, state); + if (likely(ret == -EBADMSG || ret == -ENETDOWN)) + ret = 0; + } + return ret; } static int handle_client(struct client_sess *sess, struct server_state *state) -- Ammar Faizi