https: handle TLS_WANT_POLLIN and TLS_WANT_POLLOUT - frontends - front-ends for some sites (experiment)
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit f2a7bc67a3a4b1fae29f35192fc6ad566041bfc3
 (DIR) parent edb55dfa30e8bca53391c23e22fbe0207ae78645
 (HTM) Author: Hiltjo Posthuma <hiltjo@codemadness.org>
       Date:   Tue, 19 May 2020 15:39:50 +0200
       
       https: handle TLS_WANT_POLLIN and TLS_WANT_POLLOUT
       
       Handle this for tls_read() and tls_write() like the idiom suggests in the man
       page.
       
       Diffstat:
         M https.c                             |      22 ++++++++++++++++++----
       
       1 file changed, 18 insertions(+), 4 deletions(-)
       ---
 (DIR) diff --git a/https.c b/https.c
       @@ -56,14 +56,18 @@ readtls(struct tls *t)
                                if (!(buf = realloc(buf, size + 1)))
                                        die("realloc: %s\n", strerror(errno));
                        }
       -                if ((r = tls_read(t, &buf[len], READ_BUF_SIZ)) <= 0)
       +                r = tls_read(t, &buf[len], READ_BUF_SIZ);
       +                if (r == TLS_WANT_POLLIN || r == TLS_WANT_POLLOUT) {
       +                        continue;
       +                } else if (r <= 0) {
                                break;
       +                }
                        len += r;
                        buf[len] = '\0';
                        if (len > MAX_RESPONSESIZ)
                                die("response is too big: > %zu bytes\n", MAX_RESPONSESIZ);
                }
       -        if (r < 0)
       +        if (r == -1)
                        die("tls_read: %s\n", tls_error(t));
        
                return buf;
       @@ -125,6 +129,7 @@ request(const char *host, const char *path, const char *headers)
                struct tls *t;
                char request[4096];
                char *data;
       +        size_t len;
                ssize_t w;
                int fd;
        
       @@ -148,8 +153,17 @@ request(const char *host, const char *path, const char *headers)
                if (tls_connect_socket(t, fd, host) == -1)
                        die("tls_connect: %s\n", tls_error(t));
        
       -        if ((w = tls_write(t, request, strlen(request))) < 0)
       -                die("tls_write: %s\n", tls_error(t));
       +        data = request;
       +        len = strlen(data);
       +        while (len > 0) {
       +                w = tls_write(t, data, len);
       +                if (w == TLS_WANT_POLLIN || w == TLS_WANT_POLLOUT)
       +                        continue;
       +                else if (w == -1)
       +                        die("tls_write: %s\n", tls_error(t));
       +                data += w;
       +                len -= w;
       +        }
        
                data = readtls(t);