"GOT", but the "O" is a cute, smiling pufferfish. Index | Thread | Search

From:
Omar Polo <op@omarpolo.com>
Subject:
add bufio_flush() to tidy got-fetch-http a bit
To:
gameoftrees@openbsd.org
Date:
Thu, 25 Apr 2024 17:58:32 +0200

Download raw body.

Thread
  • Omar Polo:

    add bufio_flush() to tidy got-fetch-http a bit

Seems useful to have a function to flush the write buffer instead of
rolling a loop in several places.  ok?

diff /home/op/w/got
commit - 9e2d05155fd66ac528765af1d592aea55ba4fe78
path + /home/op/w/got
blob - 8c41efaa709011edca44b1445296029d36c0644f
file + lib/bufio.c
--- lib/bufio.c
+++ lib/bufio.c
@@ -27,6 +27,7 @@
 
 #include <assert.h>
 #include <errno.h>
+#include <poll.h>
 #include <stdarg.h>
 #include <stdint.h>
 #include <stdio.h>
@@ -348,6 +349,39 @@ bufio_write(struct bufio *bio)
 	return (w);
 }
 
+int
+bufio_flush(struct bufio *bio)
+{
+	struct pollfd	 pfd;
+
+	if (bio->wbuf.len == 0)
+		return (0);
+
+	if (bufio_write(bio) == -1 && errno != EAGAIN)
+		return (-1);
+
+	while (bio->wbuf.len > 0) {
+		memset(&pfd, 0, sizeof(pfd));
+		pfd.fd = bio->fd;
+		if (bio->ctx) {
+			if (bio->wantev & BUFIO_WANT_READ)
+				pfd.events |= POLLIN;
+			if (bio->wantev & BUFIO_WANT_WRITE)
+				pfd.events |= POLLOUT;
+		}
+		if (pfd.events == 0)
+			pfd.events = POLLOUT;
+
+		if (poll(&pfd, 1, INFTIM) == -1)
+			return (-1);
+
+		if (bufio_write(bio) == -1 && errno != EAGAIN)
+			return (-1);
+	}
+
+	return (0);
+}
+
 const char *
 bufio_io_err(struct bufio *bio)
 {
blob - fbef23e4160ecd417a5885fd1cbae17390b17c29
file + lib/bufio.h
--- lib/bufio.h
+++ lib/bufio.h
@@ -65,6 +65,7 @@ int		 bufio_handshake(struct bufio *);
 ssize_t		 bufio_read(struct bufio *);
 size_t		 bufio_drain(struct bufio *, void *, size_t);
 ssize_t		 bufio_write(struct bufio *);
+int		 bufio_flush(struct bufio *);
 const char	*bufio_io_err(struct bufio *);
 int		 bufio_compose(struct bufio *, const void *, size_t);
 int		 bufio_compose_str(struct bufio *, const char *);
blob - b9e126395c419d439c968c0b1b113ab6e1a26b37
file + libexec/got-fetch-http/got-fetch-http.c
--- libexec/got-fetch-http/got-fetch-http.c
+++ libexec/got-fetch-http/got-fetch-http.c
@@ -158,7 +158,7 @@ http_open(struct bufio *bio, int https, const char *me
 	const char	*chdr = NULL, *te = "";
 	char		*p, *req;
 	int		 r;
-	
+
 	if (path_sufx != NULL && *path && path[strlen(path) - 1] == '/')
 		path_sufx++; /* skip the slash */
 
@@ -186,20 +186,13 @@ http_open(struct bufio *bio, int https, const char *me
 
 	if (verbose > 0)
 		fprintf(stderr, "%s: request: %s\n", getprogname(), req);
-	
 
 	r = bufio_compose(bio, req, r);
 	if (r == -1)
 		err(1, "bufio_compose_fmt");
 	free(req);
 
-	do {
-		r = bufio_write(bio);
-		if (r == -1 && errno != EAGAIN)
-			errx(1, "bufio_read: %s", bufio_io_err(bio));
-	} while (bio->wbuf.len != 0);
-
-	return 0;
+	return bufio_flush(bio);
 }
 
 static int
@@ -328,20 +321,12 @@ http_read(struct bufio *bio, int chunked, size_t *chun
 static int
 http_chunk(struct bufio *bio, const void *buf, size_t len)
 {
-	int r;
-
 	if (bufio_compose_fmt(bio, "%zx\r\n", len) ||
 	    bufio_compose(bio, buf, len) ||
 	    bufio_compose(bio, "\r\n", 2))
 		return 1;
 
-	do {
-		r = bufio_write(bio);
-		if (r == -1 && errno != EAGAIN)
-			errx(1, "bufio_read: %s", bufio_io_err(bio));
-	} while (bio->wbuf.len != 0);
-
-	return 0;
+	return bufio_flush(bio);
 }
 
 static int