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 [36.81.65.188]) by gnuweeb.org (Postfix) with ESMTPSA id 705107E34F; Fri, 8 Jul 2022 12:10:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1657282244; bh=BAbY8SEx03bZVBm46BOEZfmgI34m6VnvN5S7m7WXvq0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=sWbleo9x0jxU+zRuaTMDNklzPyq1q0qdz4RlC+rqSA1Vw0nkUhcA13xqVG3zNzH3E p+fztxOKqmT1ph5Im8WSBAu058aNum/aBg7PV/UyVLQMkI/urkfXNoVYo+742lu24A BKrFam5jeex86OZtZFA9adrs3Mw7HHgobZVZnMrwO4IaSJBsqoII8/6IQT6+Nv9sfS 5RYmkHs+7NgnegWg5Xikm1fVMKgAFRdR+SsOwIFC/s3zG4yG6Npl6wo4YemtoMMT7k 9kZfyHuNuKjV6qKHPPBF2QoJclrz5RfAfDralqy9dIt4wzMyMECFp2tMWiC21oUBcC yuKQdAqY6yebA== From: Ammar Faizi To: GNU/Weeb Mailing List Cc: Ammar Faizi , Alviro Iskandar Setiawan , Arthur Lapz , Fernanda Ma'rouf , Sprite , Yonle Subject: [PATCH gwhttpd 03/14] gwhttpd: Replace `send_error_and_close()` with `send_http_error()` Date: Fri, 8 Jul 2022 19:10:14 +0700 Message-Id: <20220708121025.926162-4-ammarfaizi2@gnuweeb.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220708121025.926162-1-ammarfaizi2@gnuweeb.org> References: <20220708121025.926162-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