Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
Update libfetch to the latest FreeBSD version
Baptiste Daroussin committed 10 years ago
commit 6ef952ecbde441a16e1ccaa54678fa95ca4f0ac6
parent ebf67bb
3 files changed +50 -15
modified external/libfetch/common.c
@@ -502,7 +502,8 @@ fetch_ssl_get_numeric_addrinfo(const char *hostname, size_t len)
	hints.ai_protocol = 0;
	hints.ai_flags = AI_NUMERICHOST;
	/* port is not relevant for this purpose */
-
	getaddrinfo(host, "443", &hints, &res);
+
	if (getaddrinfo(host, "443", &hints, &res) != 0)
+
		res = NULL;
	free(host);
	return res;
}
modified external/libfetch/file.c
@@ -49,7 +49,7 @@ fetchXGetFile(struct url *u, struct url_stat *us, const char *flags)
	if (us && fetchStatFile(u, us, flags) == -1)
		return (NULL);

-
	f = fopen(u->doc, "r");
+
	f = fopen(u->doc, "re");

	if (f == NULL) {
		fetch_syserr();
@@ -62,7 +62,6 @@ fetchXGetFile(struct url *u, struct url_stat *us, const char *flags)
		return (NULL);
	}

-
	fcntl(fileno(f), F_SETFD, FD_CLOEXEC);
	return (f);
}

@@ -78,9 +77,9 @@ fetchPutFile(struct url *u, const char *flags)
	FILE *f;

	if (CHECK_FLAG('a'))
-
		f = fopen(u->doc, "a");
+
		f = fopen(u->doc, "ae");
	else
-
		f = fopen(u->doc, "w+");
+
		f = fopen(u->doc, "w+e");

	if (f == NULL) {
		fetch_syserr();
@@ -93,7 +92,6 @@ fetchPutFile(struct url *u, const char *flags)
		return (NULL);
	}

-
	fcntl(fileno(f), F_SETFD, FD_CLOEXEC);
	return (f);
}

modified external/libfetch/http.c
@@ -130,8 +130,8 @@ struct httpio
	int		 chunked;	/* chunked mode */
	char		*buf;		/* chunk buffer */
	size_t		 bufsize;	/* size of chunk buffer */
-
	ssize_t		 buflen;	/* amount of data currently in buffer */
-
	int		 bufpos;	/* current read offset in buffer */
+
	size_t		 buflen;	/* amount of data currently in buffer */
+
	size_t		 bufpos;	/* current read offset in buffer */
	int		 eof;		/* end-of-file flag */
	int		 error;		/* error flag */
	size_t		 chunksize;	/* remaining size of current chunk */
@@ -215,6 +215,7 @@ http_fillbuf(struct httpio *io, size_t len)
	if (io->eof)
		return (0);

+
	/* not chunked: just fetch the requested amount */
	if (io->chunked == 0) {
		if (http_growbuf(io, len) == -1)
			return (-1);
@@ -227,6 +228,7 @@ http_fillbuf(struct httpio *io, size_t len)
		return (io->buflen);
	}

+
	/* chunked, but we ran out: get the next chunk header */
	if (io->chunksize == 0) {
		switch (http_new_chunk(io)) {
		case -1:
@@ -238,6 +240,7 @@ http_fillbuf(struct httpio *io, size_t len)
		}
	}

+
	/* fetch the requested amount, but no more than the current chunk */
	if (len > io->chunksize)
		len = io->chunksize;
	if (http_growbuf(io, len) == -1)
@@ -246,8 +249,9 @@ http_fillbuf(struct httpio *io, size_t len)
		io->error = errno;
		return (-1);
	}
+
	io->bufpos = 0;
	io->buflen = nbytes;
-
	io->chunksize -= io->buflen;
+
	io->chunksize -= nbytes;

	if (io->chunksize == 0) {
		if (fetch_read(io->conn, &ch, 1) != 1 || ch != '\r' ||
@@ -255,8 +259,6 @@ http_fillbuf(struct httpio *io, size_t len)
			return (-1);
	}

-
	io->bufpos = 0;
-

	return (io->buflen);
}

@@ -1376,8 +1378,12 @@ http_connect(struct url *URL, struct url *purl, const char *flags)
{
	struct url *curl;
	conn_t *conn;
+
	hdr_t h;
+
	http_headerbuf_t headerbuf;
+
	const char *p;
	int verbose;
	int af, val;
+
	int serrno;

#ifdef INET6
	af = AF_UNSPEC;
@@ -1398,6 +1404,7 @@ http_connect(struct url *URL, struct url *purl, const char *flags)
	if ((conn = fetch_connect(curl->host, curl->port, af, verbose)) == NULL)
		/* fetch_connect() has already set an error code */
		return (NULL);
+
	init_http_headerbuf(&headerbuf);
	if (strcasecmp(URL->scheme, SCHEME_HTTPS) == 0 && purl) {
		http_cmd(conn, "CONNECT %s:%d HTTP/1.1",
		    URL->host, URL->port);
@@ -1405,10 +1412,26 @@ http_connect(struct url *URL, struct url *purl, const char *flags)
		    URL->host, URL->port);
		http_cmd(conn, "");
		if (http_get_reply(conn) != HTTP_OK) {
-
			fetch_close(conn);
-
			return (NULL);
+
			http_seterr(conn->err);
+
			goto ouch;
+
		}
+
		/* Read and discard the rest of the proxy response */
+
		if (fetch_getln(conn) < 0) {
+
			fetch_syserr();
+
			goto ouch;
		}
-
		http_get_reply(conn);
+
		do {
+
			switch ((h = http_next_header(conn, &headerbuf, &p))) {
+
			case hdr_syserror:
+
				fetch_syserr();
+
				goto ouch;
+
			case hdr_error:
+
				http_seterr(HTTP_PROTOCOL_ERROR);
+
				goto ouch;
+
			default:
+
				/* ignore */ ;
+
			}
+
		} while (h < hdr_end);
	}
	if (strcasecmp(URL->scheme, SCHEME_HTTPS) == 0 &&
	    fetch_ssl(conn, URL, verbose) == -1) {
@@ -1416,14 +1439,21 @@ http_connect(struct url *URL, struct url *purl, const char *flags)
		/* grrr */
		errno = EAUTH;
		fetch_syserr();
-
		return (NULL);
+
		goto ouch;
	}
#ifdef TCP_NOPUSH
	val = 1;
	setsockopt(conn->sd, IPPROTO_TCP, TCP_NOPUSH, &val, sizeof(val));
#endif

+
	clean_http_headerbuf(&headerbuf);
	return (conn);
+
ouch:
+
	serrno = errno;
+
	clean_http_headerbuf(&headerbuf);
+
	fetch_close(conn);
+
	errno = serrno;
+
	return (NULL);
}

static struct url *
@@ -1631,6 +1661,9 @@ http_request_body(struct url *URL, const char *op, struct url_stat *us,
					http_seterr(HTTP_NEED_PROXY_AUTH);
					goto ouch;
				}
+
			} else if (fetch_netrc_auth(purl) == 0) {
+
				aparams.user = strdup(purl->user);
+
				aparams.password = strdup(purl->pwd);
			}
			http_authorize(conn, "Proxy-Authorization",
				       &proxy_challenges, &aparams, url);
@@ -1658,6 +1691,9 @@ http_request_body(struct url *URL, const char *op, struct url_stat *us,
					http_seterr(HTTP_NEED_AUTH);
					goto ouch;
				}
+
			} else if (fetch_netrc_auth(url) == 0) {
+
				aparams.user = strdup(url->user);
+
				aparams.password = strdup(url->pwd);
			} else if (fetchAuthMethod &&
				   fetchAuthMethod(url) == 0) {
				aparams.user = strdup(url->user);