shithub: tcp80

Download patch

ref: be84ca6fe75bf5b400d1247a57edf0390a7c19e1
parent: 02f0cea426cd1eb2d9f676c131f6ecace60f761c
author: grobe0ba <grobe0ba@tcp80.org>
date: Mon Aug 1 16:10:12 EDT 2022

fix error page redirection

--- a/tcp80.c
+++ b/tcp80.c
@@ -399,6 +399,34 @@
 }
 
 int
+redirerr(char *status, int istatus, Dir *d)
+{
+	static char buf[8192], tmp[1024];
+	char *p, *s;
+	int i;
+	Pair *h;
+
+	for(i = 0; i < nelem(redir_errno); i++){
+		if(istatus == redir_errno[i]){
+			status = "301 Moved Permanently";
+			respond(status);
+			headers(buf, d);
+
+			snprint(buf, sizeof(buf), "/%d", istatus);
+			strncat(buf, location, sizeof(buf));
+
+			h = findhdr(nil, "Host");
+			p = strchr(location, '?');
+			s = fullurl(h ? h->val : nil, urlenc(tmp, buf, sizeof(tmp)), "/",
+				    p ? p + 1 : nil);
+			print("Location: %s\r\n\r\n", buf);
+			return 1;
+		}
+	}
+	return 0;
+}
+
+int
 dispatch(void)
 {
 	static char buf[8192], tmp[1024];
@@ -415,24 +443,8 @@
 		istatus = 405;
 		status	= "405 Method Not Allowed";
 	Error:
-		for(i = 0; i < nelem(redir_errno); i++){
-			if(istatus == redir_errno[i]){
-				status = "301 Moved Permanently";
-				respond(status);
-				headers(buf, d);
-
-				strncpy(buf, "/404", sizeof(buf));
-				strncat(buf, location, sizeof(buf));
-
-				h = findhdr(nil, "Host");
-				p = strchr(location, '?');
-				s = fullurl(h ? h->val : nil, urlenc(tmp, buf, sizeof(tmp)), "/",
-					    p ? p + 1 : nil);
-				print("Location: %s\r\nContent-Length: %d\r\n\r\n%*s", s, n, n,
-				      buf);
-				goto Out;
-			}
-		}
+		if(redirerr(status, istatus, d))
+			goto Out;
 		if(!nobody)
 			n = snprint(buf, sizeof(buf),
 				    "<html><head><title>%s</title></head>\n"
@@ -798,11 +810,6 @@
 		}
 		c = findrule(r, loc);
 		if(c){
-			if(cistrcmp(method, "GET") != 0){
-				respond("405 Method Not Allowed");
-				return;
-			}
-
 			Dir fakedir  = {0};
 			fakedir.mode = 0644;
 			fakedir.name = "unrealfile";
@@ -809,9 +816,13 @@
 			fakedir.uid  = "none";
 			fakedir.gid  = "none";
 			fakedir.muid = "none";
+			if(cistrcmp(method, "GET") != 0)
+				if(redirerr("405 Method Not Allowed", 405, &fakedir))
+					goto rOut;
 			respond("200 OK");
 			headers(loc, &fakedir);
 			dispatchrule(c);
+		rOut:
 			free(c);
 			return;
 		}