shithub: castor9

Download patch

ref: 27cba0188575b3663d7e4873c99adfa489213d5a
parent: 894d1d534e687fac3dae6cc64db07ad07f55a891
author: Julien Blanchard <julien@typed-hole.org>
date: Thu Dec 10 12:03:44 EST 2020

Better status codes handling

--- a/castor.c
+++ b/castor.c
@@ -190,16 +190,25 @@
 }
 
 void
-handle_status(char *status, Response *r)
+parse_status(char *status, Response *r)
 {
 	int code;
-	char *meta;
-	code = atoi(strtok(status, " \t"));
-	if(code == 0)
-		message("STATUS: %s\n", status);
-	meta = strtok(NULL, "\n");
-	r->status = code;
-	r->meta = cleanup(meta);
+	char *meta, *s;
+
+	if(status == nil){
+		message("Failed to read response (missing crlf?)", status);
+		return;
+	}
+	if((s = strtok(status, " \t")) != nil){
+		code = atoi(s);
+		if(code == 0){
+			message("Invalid status received: %s", status);
+			return;
+		}
+		meta = strtok(NULL, "\r\n");
+		r->status = code;
+		r->meta = cleanup(meta);
+	}
 }
 
 void
@@ -230,7 +239,7 @@
 		}
 		/* Headers */
 		if(strbeg(line, "#") == 0){
-			plrtstr(&c->text, 1000000, 8, 0, font, strdup(cleanup(base)), PL_SEL, 0);
+			plrtstr(&c->text, 1000000, 8, 0, font, strdup(cleanup(base)), PL_HEAD, 0);
 			break;
         } /* Small lines */		
 		if((length <= width)){
@@ -363,10 +372,17 @@
 	fprint(fd, "%s\r\n", url->raw);
 	Binit(&body, fd, OREAD);
 
-	char *status = Brdstr(&body, '\n', '0');
-	handle_status(status, r);
+	char *status = Brdstr(&body, '\n', 0);
+	parse_status(status, r);
 
-	if(r->status == 20){
+	switch(r->status){
+	case 10:
+		message("Input! %s", r->meta);
+		break;
+	case 11:
+		message("Sensitive input! %s", r->meta);
+		break;
+	case 20:
 		c->url = url;
 		set_current_base_url(base_url(url));
 
@@ -396,9 +412,56 @@
 
 			show(c);
 		}
-	}else if(r->status == 31){
-		Url *redirect = urlparse(nil, r->meta);
-		gemini_get(redirect);
+
+		break;
+	case 30:
+		gemini_get(urlparse(nil, r->meta));
+		break;
+	case 31:
+		gemini_get(urlparse(nil, r->meta));
+		break;
+	case 40:
+		message("Temporary failure, please try again later!");
+		break;
+	case 41:
+		message("Server unavailable!");
+		break;
+	case 42:
+		message("CGI error!");
+		break;
+	case 43:
+		message("Proxy error!");
+		break;
+	case 44:
+		message("Slow down!");
+		break;
+	case 50:
+		message("Permanent failure!");
+		break;
+	case 51:
+		message("Not found!");
+		break;
+	case 52:
+		message("Gone!");
+		break;
+	case 53:
+		message("Proxy request refused!");
+		break;
+	case 59:
+		message("Bad request!");
+		break;
+	case 60:
+		message("Client certificate required!");
+		break;
+	case 61:
+		message("Certificate not authorised!");
+		break;
+	case 62:
+		message("Certificate not valid!");
+		break;
+	//default:
+	//	message("Unknown status code %d!", status);
+	//	break;
 	}
 }