~ft/gemnine

a811d49d0d0ae0ed433903f4a3337fc2f75a5260 — Sigrid Haflínudóttir 2 months ago 578fad9 master
fix response freeing urls that aren't supposed to be freed
3 files changed, 35 insertions(+), 29 deletions(-)

M gemnine.h
M main.c
M req.c
M gemnine.h => gemnine.h +1 -0
@@ 42,6 42,7 @@ struct Url {
	char *path;
	char *query;
	char *fragment;
	int free;
};

typedef struct {

M main.c => main.c +29 -22
@@ 109,10 109,10 @@ nextreq:
				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 @@ nextreq:
		}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)

M req.c => req.c +5 -7
@@ 105,20 105,17 @@ request(Url *url)
			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);
		goto err;
	}

	return r;

err:
	freeresponse(r);
	return nil;
	return r;
}

void


@@ 127,7 124,8 @@ freeresponse(Response *r)
	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);