summary refs log tree commit diff
diff options
context:
space:
mode:
authorLeah Neukirchen <leah@vuxu.org>2020-05-08 20:01:35 +0200
committerLeah Neukirchen <leah@vuxu.org>2020-05-08 20:01:35 +0200
commit8c03a1cb66412ba184c221192c8ac32f4865c262 (patch)
tree669b3447448a6745a0f4c5e1a171d9ce540d1ad6
parent800bcd41f5b38853c98bfdf48c5a71dcb33fe54f (diff)
downloadhittpd-8c03a1cb66412ba184c221192c8ac32f4865c262.tar.gz
hittpd-8c03a1cb66412ba184c221192c8ac32f4865c262.tar.xz
hittpd-8c03a1cb66412ba184c221192c8ac32f4865c262.zip
send_dir_redirect: increase response buffer, detect truncation
Thanks @duncaen.
-rw-r--r--hittpd.c65
1 files changed, 35 insertions, 30 deletions
diff --git a/hittpd.c b/hittpd.c
index c4a9f91..1cc12c0 100644
--- a/hittpd.c
+++ b/hittpd.c
@@ -233,7 +233,7 @@ accesslog(http_parser *p, int status)
 }
 
 void
-send_dir_redirect(http_parser *p)
+send_error(http_parser *p, int status, const char *msg)
 {
 	struct conn_data *data = p->data;
 	char buf[512];
@@ -242,6 +242,35 @@ send_dir_redirect(http_parser *p)
 	httpdate(time(0), now);
 
 	int len = snprintf(buf, sizeof buf,
+	    "HTTP/%d.%d %d %s\r\n"
+	    "Content-Length: %ld\r\n"
+	    "Date: %s\r\n"
+	    "\r\n",
+	    p->http_major,
+	    p->http_minor,
+	    status, msg,
+	    (data->last = 4 + strlen(msg) + 2),
+	    now);
+
+	if (p->method != HTTP_HEAD)
+		len += snprintf(buf + len, sizeof buf - len,
+		    "%03d %s\r\n",
+		    status, msg);
+
+	write(data->fd, buf, len);
+	accesslog(p, status);
+}
+
+void
+send_dir_redirect(http_parser *p)
+{
+	struct conn_data *data = p->data;
+	char buf[2048];
+
+	char now[64];
+	httpdate(time(0), now);
+
+	int len = snprintf(buf, sizeof buf,
 	    "HTTP/1.%d 301 Moved Permanently\r\n"
 	    "Content-Length: 0\r\n"
 	    "Date: %s\r\n"
@@ -251,6 +280,11 @@ send_dir_redirect(http_parser *p)
 	    now,
 	    data->path);
 
+	if (len >= (int)sizeof buf) {
+		send_error(p, 413, "Payload Too Large");
+		return;
+	}
+
 	// XXX include redirect link?
 
 	data->last = data->first = 0;
@@ -283,35 +317,6 @@ send_not_modified(http_parser *p, time_t modified)
 }
 
 void
-send_error(http_parser *p, int status, const char *msg)
-{
-	struct conn_data *data = p->data;
-	char buf[512];
-
-	char now[64];
-	httpdate(time(0), now);
-
-	int len = snprintf(buf, sizeof buf,
-	    "HTTP/%d.%d %d %s\r\n"
-	    "Content-Length: %ld\r\n"
-	    "Date: %s\r\n"
-	    "\r\n",
-	    p->http_major,
-	    p->http_minor,
-	    status, msg,
-	    (data->last = 4 + strlen(msg) + 2),
-	    now);
-
-	if (p->method != HTTP_HEAD)
-		len += snprintf(buf + len, sizeof buf - len,
-		    "%03d %s\r\n",
-		    status, msg);
-
-	write(data->fd, buf, len);
-	accesslog(p, status);
-}
-
-void
 send_rns(http_parser *p, off_t filesize)
 {
 	struct conn_data *data = p->data;