diff options
author | Leah Neukirchen <leah@vuxu.org> | 2020-05-09 17:42:54 +0200 |
---|---|---|
committer | Leah Neukirchen <leah@vuxu.org> | 2020-05-09 17:42:54 +0200 |
commit | d6e080f59b3763cf432fbfcd18a68b6c9c0a6514 (patch) | |
tree | 8fd4028317977c4c8bbe0f2830f1df1740f76d5c | |
parent | 905eea31d98d0a9143315fe3dff6a37b07940c30 (diff) | |
download | hittpd-d6e080f59b3763cf432fbfcd18a68b6c9c0a6514.tar.gz hittpd-d6e080f59b3763cf432fbfcd18a68b6c9c0a6514.tar.xz hittpd-d6e080f59b3763cf432fbfcd18a68b6c9c0a6514.zip |
send_error: refactor
-rw-r--r-- | hittpd.c | 22 |
1 files 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); |