shithub: paste

Download patch

ref: 8302ddbd15999e409ff342883ca1f82133877bcd
parent: 4e965b3e0c2d7138c632087e6bbb8bef9b9b8a6b
author: Alex Musolino <alex@musolino.id.au>
date: Tue May 27 05:12:37 EDT 2025

paste.c: refactor http error reporting

--- a/bin/paste.c
+++ b/bin/paste.c
@@ -25,6 +25,30 @@
 }
 
 static void
+error400(void)
+{
+	error(400, "Bad request");
+}
+
+static void
+error403(void)
+{
+	error(403, "Forbidden");
+}
+
+static void
+error404(void)
+{
+	error(404, "Not found");
+}
+
+static void
+error500(void)
+{
+		error(500, "Internal server error");
+}
+
+static void
 permredirect(char *location)
 {
 	fprint(1, "Status: 301 Moved Permanently\r\n");
@@ -364,18 +388,18 @@
 	ctype = getenv("CONTENT_TYPE");
 	if(ctype == nil){
 		fprint(2, "CGI variable CONTENT_TYPE not set!\n");
-		error(500, "Internal Server Error");
+		error500();
 		exits("missing CONTENT_TYPE");
 	}
 	if(strncmp(ctype, "multipart/form-data;", 20) != 0){
 		fprint(2, "expected multipart/form-data content-type; got %s\n", ctype);
-		error(400, "Bad Request");
+		error400();
 		exits("unexpected content-type");
 	}
 	boundary = findboundary(ctype);
 	if(boundary == nil){
 		fprint(2, "could not find  boundary in CONTENT_TYPE\n");
-		error(400, "Bad Request");
+		error400();
 		exits("missing boundary for multipart/form-data");
 	}
 	mark = smprint("--%s", boundary);
@@ -383,7 +407,7 @@
 	dir = smprint("%s/uploads/XXXXXXXXXXX", getenv("FS_ROOT"));
 	if(strcmp(nmktemp(dir), "/") == 0){
 		fprint(2, "could not create upload directory: mktemp: %r\n");
-		error(500, "Internal Server Error");
+		error500();
 		exits("mktemp");
 	}
 	target = nil;
@@ -391,7 +415,7 @@
 	nfiles = 0;
 	filename = nil;
 	if(skipto(mark) != 1){
-		error(400, "Bad Request");
+		error400();
 		exits(0);
 	}
 	for(;;){
@@ -399,7 +423,7 @@
 			break;
 		if(!crlf()){
 			fprint(2, "expected CRLF sequence after boundary marker\n");
-			error(400, "Bad Request");
+			error400();
 			exits(0);
 		}
 		free(filename);
@@ -447,12 +471,12 @@
 			out = Bopen(filename, OWRITE);
 			if(out == nil){
 				fprint(2, "could not open %s: %r\n", filename);
-				error(500, "Internal Server Error");
+				error500();
 				exits("open");
 			}
 			if(copydata(out, mark) < 0){
 				fprint(2, "error writing to %s: %r\n", filename);
-				error(500, "Internal Server Error");
+				error500();
 				exits("copydata");
 			}
 			Bterm(out);
@@ -466,7 +490,7 @@
 			}
 		}else if(skipto(mark) < 0){
 			fprint(2, "unexpected EOF\n");
-			error(400, "Bad Request");
+			error400();
 			exits(0);
 		}
 	}
@@ -485,7 +509,7 @@
 		return;
 	if(n < 0){
 		fprint(2, "paste: dirreadall: %r\n");
-		error(500, "Internal Server Error");
+		error500();
 		exits("dirreadall");
 	}
 
@@ -511,7 +535,7 @@
 	d = dirstat(path);
 	fd = open(path, OREAD);
 	if(fd < 0){
-		error(404, "Not Found");
+		error404();
 		exits(0);
 	}
 	if((d->mode&DMDIR) != 0){
@@ -559,13 +583,13 @@
 	requri = getenv("REQUEST_URI");
 	if(requri == nil){
 		fprint(2, "CGI variable REQUEST_URI not set!\n");
-		error(500, "Internal Server Error");
+		error500();
 		exits("missing REQUEST_URI");
 	}
 	if(strcmp(requri, "/") == 0)
 		permredirect("/index.html");
 	if(strcmp(requri, "/uploads/") == 0){
-		error(403, "Forbidden");
+		error403();
 		exits(0);
 	}
 	stdin = Bfdopen(0, OREAD);
@@ -575,5 +599,5 @@
 		serve(smprint("%s/%s", getenv("FS_ROOT"), requri));
 	if(strcmp(requri, "/post") == 0)
 		handlepost();
-	error(404, "Not Found");
+	error404();
 }
--