From d6e080f59b3763cf432fbfcd18a68b6c9c0a6514 Mon Sep 17 00:00:00 2001 From: Leah Neukirchen Date: Sat, 9 May 2020 17:42:54 +0200 Subject: send_error: refactor --- hittpd.c | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/hittpd.c b/hittpd.c index 2d75f49..fe6b045 100644 --- a/hittpd.c +++ b/hittpd.c @@ -276,24 +276,32 @@ send_error(http_parser *p, int status, const char *msg) char now[64]; httpdate(time(0), now); + char content[512]; + if (p->method == HTTP_HEAD) + *content = 0; + else + snprintf(content, sizeof content, "%03d %s\r\n", status, msg); + data->first = 0; - data->last = 4 + strlen(msg) + 2; + data->last = strlen(content); int len = snprintf(buf, sizeof buf, "HTTP/%d.%d %d %s\r\n" "Content-Length: %jd\r\n" "Date: %s\r\n" - "\r\n", + "\r\n" + "%s", p->http_major, p->http_minor, status, msg, content_length(data), - now); + now, + content); - if (p->method != HTTP_HEAD) - len += snprintf(buf + len, sizeof buf - len, - "%03d %s\r\n", - status, msg); + if (len >= (int)sizeof buf) { + send_error(p, 413, "Payload Too Large"); + return 0; + } write(data->fd, buf, len); accesslog(p, status); -- cgit 1.4.1