From: Ammar Faizi <[email protected]>
To: GNU/Weeb Mailing List <[email protected]>
Cc: Ammar Faizi <[email protected]>,
Alviro Iskandar Setiawan <[email protected]>,
Arthur Lapz <[email protected]>,
Fernanda Ma'rouf <[email protected]>,
Sprite <[email protected]>, Yonle <[email protected]>
Subject: [PATCH gwhttpd 03/14] gwhttpd: Replace `send_error_and_close()` with `send_http_error()`
Date: Fri, 8 Jul 2022 19:10:14 +0700 [thread overview]
Message-ID: <[email protected]> (raw)
In-Reply-To: <[email protected]>
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 <[email protected]>
---
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
next prev parent reply other threads:[~2022-07-08 12:10 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-07-08 12:10 [PATCH gwhttpd 00/14] gwhttpd updates Ammar Faizi
2022-07-08 12:10 ` [PATCH gwhttpd 01/14] gwhttpd: Do an early return when `parse_http_header()` fails Ammar Faizi
2022-07-08 12:10 ` [PATCH gwhttpd 02/14] gwhttpd: Don't print any error when mlock fails Ammar Faizi
2022-07-08 12:10 ` Ammar Faizi [this message]
2022-07-08 12:10 ` [PATCH gwhttpd 04/14] gwhttpd: Add log in the interrupt handler Ammar Faizi
2022-07-08 12:10 ` [PATCH gwhttpd 05/14] gwhttpd: Refactor HTTP header parser Ammar Faizi
2022-07-08 12:10 ` [PATCH gwhttpd 06/14] gwhttpd: Avoid endless busy spinning on `send()` Ammar Faizi
2022-07-08 12:10 ` [PATCH gwhttpd 07/14] Makefile: Add "make clean" command Ammar Faizi
2022-07-08 12:10 ` [PATCH gwhttpd 08/14] gwhttpd: Skip interrupt error from `epoll_wait()` Ammar Faizi
2022-07-08 12:10 ` [PATCH gwhttpd 09/14] gwhttpd: Add directory listing and download file support Ammar Faizi
2022-07-08 12:10 ` [PATCH gwhttpd 10/14] gwhttpd: Add command line options Ammar Faizi
2022-07-08 12:10 ` [PATCH gwhttpd 11/14] gwhttpd: Add SLC support Ammar Faizi
2022-07-08 12:10 ` [PATCH gwhttpd 12/14] gwhttpd: slc: Shut the SLC log up Ammar Faizi
2022-07-08 12:10 ` [PATCH gwhttpd 13/14] gwhttpd: Fix 403 HTTP error when accessing an empty file Ammar Faizi
2022-07-08 12:10 ` [PATCH gwhttpd 14/14] gwhttpd: Add connecting log for SLC Ammar Faizi
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
[email protected] \
[email protected] \
[email protected] \
[email protected] \
[email protected] \
[email protected] \
[email protected] \
[email protected] \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox