Replace http_send_status() with http_prepare_error_response()

This approach fits better in line of first initializing the response
struct and then sending the header with http_send_header() later.

Signed-off-by: Laslo Hunhold <dev@frign.de>
This commit is contained in:
Laslo Hunhold 2020-08-28 22:32:47 +02:00
parent 601b56d270
commit 123f168a3b
No known key found for this signature in database
GPG key ID: 69576BD24CFCB980
3 changed files with 49 additions and 43 deletions

62
http.c
View file

@ -99,42 +99,18 @@ http_send_header(int fd, const struct response *res)
esc) < 0) { esc) < 0) {
return S_REQUEST_TIMEOUT; return S_REQUEST_TIMEOUT;
} }
} } else if (res->type == RESTYPE_ERROR) {
return res->status;
}
enum status
http_send_status(int fd, enum status s)
{
enum status sendstatus;
struct response res = {
.status = s,
.field[RES_CONTENT_TYPE] = "text/html; charset=utf-8",
};
if (s == S_METHOD_NOT_ALLOWED) {
if (esnprintf(res.field[RES_ALLOW],
sizeof(res.field[RES_ALLOW]), "%s",
"Allow: GET, HEAD")) {
return S_INTERNAL_SERVER_ERROR;
}
}
if ((sendstatus = http_send_header(fd, &res)) != s) {
return sendstatus;
}
if (dprintf(fd, if (dprintf(fd,
"<!DOCTYPE html>\n<html>\n\t<head>\n" "<!DOCTYPE html>\n<html>\n\t<head>\n"
"\t\t<title>%d %s</title>\n\t</head>\n\t<body>\n" "\t\t<title>%d %s</title>\n\t</head>\n\t<body>\n"
"\t\t<h1>%d %s</h1>\n\t</body>\n</html>\n", "\t\t<h1>%d %s</h1>\n\t</body>\n</html>\n",
s, status_str[s], s, status_str[s]) < 0) { res->status, status_str[res->status],
res->status, status_str[res->status]) < 0) {
return S_REQUEST_TIMEOUT; return S_REQUEST_TIMEOUT;
} }
}
return s; return res->status;
} }
static void static void
@ -827,3 +803,31 @@ http_prepare_response(const struct request *req, struct response *res,
return 0; return 0;
} }
void
http_prepare_error_response(const struct request *req,
struct response *res, enum status s)
{
/* used later */
(void)req;
/* empty all response fields */
memset(res, 0, sizeof(*res));
res->type = RESTYPE_ERROR;
res->status = s;
if (esnprintf(res->field[RES_CONTENT_TYPE],
sizeof(res->field[RES_CONTENT_TYPE]),
"text/html; charset=utf-8")) {
res->status = S_INTERNAL_SERVER_ERROR;
}
if (res->status == S_METHOD_NOT_ALLOWED) {
if (esnprintf(res->field[RES_ALLOW],
sizeof(res->field[RES_ALLOW]),
"Allow: GET, HEAD")) {
res->status = S_INTERNAL_SERVER_ERROR;
}
}
}

2
http.h
View file

@ -105,5 +105,7 @@ enum status http_recv_header(int, char *, size_t, size_t *);
enum status http_parse_header(const char *, struct request *); enum status http_parse_header(const char *, struct request *);
enum status http_prepare_response(const struct request *, struct response *, enum status http_prepare_response(const struct request *, struct response *,
const struct server *); const struct server *);
void http_prepare_error_response(const struct request *,
struct response *, enum status);
#endif /* HTTP_H */ #endif /* HTTP_H */

8
main.c
View file

@ -41,8 +41,9 @@ serve(int infd, const struct sockaddr_storage *in_sa, const struct server *srv)
if ((status = http_recv_header(c.fd, c.header, LEN(c.header), &c.off)) || if ((status = http_recv_header(c.fd, c.header, LEN(c.header), &c.off)) ||
(status = http_parse_header(c.header, &c.req)) || (status = http_parse_header(c.header, &c.req)) ||
(status = http_prepare_response(&c.req, &c.res, srv))) { (status = http_prepare_response(&c.req, &c.res, srv))) {
status = http_send_status(c.fd, status); http_prepare_error_response(&c.req, &c.res, status);
} else { }
status = http_send_header(c.fd, &c.res); status = http_send_header(c.fd, &c.res);
/* send data */ /* send data */
@ -51,7 +52,6 @@ serve(int infd, const struct sockaddr_storage *in_sa, const struct server *srv)
} else if (c.res.type == RESTYPE_DIRLISTING) { } else if (c.res.type == RESTYPE_DIRLISTING) {
resp_dir(c.fd, &c.res); resp_dir(c.fd, &c.res);
} }
}
/* write output to log */ /* write output to log */
t = time(NULL); t = time(NULL);
@ -63,7 +63,7 @@ serve(int infd, const struct sockaddr_storage *in_sa, const struct server *srv)
if (sock_get_inaddr_str(in_sa, inaddr, LEN(inaddr))) { if (sock_get_inaddr_str(in_sa, inaddr, LEN(inaddr))) {
goto cleanup; goto cleanup;
} }
printf("%s\t%s\t%d\t%s\t%s\n", tstmp, inaddr, status, printf("%s\t%s\t%d\t%s\t%s\n", tstmp, inaddr, c.res.status,
c.req.field[REQ_HOST], c.req.uri); c.req.field[REQ_HOST], c.req.uri);
cleanup: cleanup:
/* clean up and finish */ /* clean up and finish */