shithub: gemnine

Download patch

ref: a811d49d0d0ae0ed433903f4a3337fc2f75a5260
parent: 578fad97c2a3599aec2ac4085fed2430e3c2bbc8
author: Sigrid Haflínudóttir <[email protected]>
date: Mon Aug 24 10:06:31 EDT 2020

fix response freeing urls that aren't supposed to be freed

--- a/gemnine.h
+++ b/gemnine.h
@@ -42,6 +42,7 @@
 	char *path;
 	char *query;
 	char *fragment;
+	int free;
 };
 
 typedef struct {
--- a/main.c
+++ b/main.c
@@ -109,10 +109,10 @@
 				Bterm(&body);
 				free(s);
 				if(t != nil){
+					freeresponse(r);
 					freeurl(url);
 					url = urlparse(nil, t);
 					free(t);
-					freeresponse(r);
 				}else{
 					fprint(2, "%r\n");
 				}
@@ -124,31 +124,38 @@
 		}else{
 			if(wait)
 				close(open("/dev/text", OWRITE|OTRUNC));
-			Binit(&body, r->fd, OREAD);
-			while((s = Brdstr(&body, '\n', 1)) != nil){
-				if((len = Blinelen(&body)) > 0)
-					s[len] = 0;
-				for(len--; len >= 0 && (s[len] == '\r' || s[len] == '\n'); len--)
-					s[len] = 0;
-				if(s[0] == '=' && s[1] == '>'){
-					u = s + 2;
-					while(isspace(*u))
-						u++;
-					if((t = strpbrk(u, " \t")) != nil)
-						*t++ = 0;
-					else
-						t = "";
-					x = urlparse(r->url, u);
-					Bprint(&out, "→ %U %s\n", x, t);
-					freeurl(x);
-				}else{
-					Bprint(&out, "%s\n", s);
+			if(r->code < 20 || r->code >= 30){
+				fprint(2, "%U: %d %s\n", url, r->code, r->meta[0] ? r->meta : r->status);
+				if(!wait)
+					exits("failed");
+			}else{
+				Binit(&body, r->fd, OREAD);
+				while((s = Brdstr(&body, '\n', 1)) != nil){
+					if((len = Blinelen(&body)) > 0)
+						s[len] = 0;
+					for(len--; len >= 0 && (s[len] == '\r' || s[len] == '\n'); len--)
+						s[len] = 0;
+					if(s[0] == '=' && s[1] == '>'){
+						u = s + 2;
+						while(isspace(*u))
+							u++;
+						if((t = strpbrk(u, " \t")) != nil)
+							*t++ = 0;
+						else
+							t = "";
+						x = urlparse(r->url, u);
+						Bprint(&out, "→ %U %s\n", x, t);
+						freeurl(x);
+					}else{
+						Bprint(&out, "%s\n", s);
+					}
+					free(s);
 				}
-				free(s);
+				Bterm(&body);
 			}
-			Bterm(&body);
 		}
 		freeresponse(r);
+		freeurl(url);
 	}else{
 		fprint(2, "%U: %r\n", url);
 		if(!wait)
--- a/req.c
+++ b/req.c
@@ -105,9 +105,9 @@
 			werrstr("invalid redirect");
 			goto err;
 		}
+		u->free = 1;
 		freeresponse(r);
-		if((r = request(u)) == nil)
-			freeurl(u);
+		r = request(u);
 	}else{
 		r->meta = estrdup(s);
 		werrstr(r->status);
@@ -114,11 +114,8 @@
 		goto err;
 	}
 
-	return r;
-
 err:
-	freeresponse(r);
-	return nil;
+	return r;
 }
 
 void
@@ -127,7 +124,8 @@
 	if(r == nil)
 		return;
 	close(r->fd);
-	freeurl(r->url);
+	if(r->url->free)
+		freeurl(r->url);
 	free(r->mime);
 	free(r->prompt);
 	free(r->meta);