commit 4b4e3310b4b159b241d85146d3e6ef4cfec0de58
parent 34b926de8c9d1dac1890690f52765f55a741e92b
Author: glenda <glenda@9front.local>
Date: Sat, 14 Aug 2021 02:19:03 +0000
swtich tio bio.h
Diffstat:
M | gemfetch.c | | | 39 | ++++++++++++++++++++++----------------- |
1 file changed, 22 insertions(+), 17 deletions(-)
diff --git a/gemfetch.c b/gemfetch.c
@@ -2,6 +2,7 @@
#include <libc.h>
#include <mp.h>
#include <libsec.h>
+#include <bio.h>
#define BSIZE 4096
@@ -36,9 +37,10 @@ usage(void)
void
main(int argc, char **argv)
{
- char buf[BSIZE], *uri, *host;
+ char *buf, *uri, *host;
long n, t;
int fd, tlsfd;
+ Biobuf bfd;
TLSconn conn;
host = nil;
@@ -55,7 +57,7 @@ main(int argc, char **argv)
uri = argv[0];
if (host == nil) host = gethost(uri);
- if (host == nil) sysfatal("%r");
+ if (host == nil) sysfatal("gethost: %r");
fprint(2, "fetching from %s\n", host);
fd = dial(netmkaddr(host, "tcp", "1965"), nil, nil, nil);
@@ -64,31 +66,34 @@ main(int argc, char **argv)
memset(&conn, 0, sizeof(conn));
tlsfd = tlsClient(fd, &conn);
- if (tlsfd <=0) sysfatal("tlsClient: %r");
+ if (tlsfd < 0) sysfatal("tlsClient: %r");
- close(fd);
+ if (Binit(&bfd, tlsfd, OREAD) < 0)
+ sysfatal("Binit: %r");
+
+ n = fprint(tlsfd, "%s\r\n", uri);
+ if (n < 0) sysfatal("fprint: %r");
- fprint(tlsfd, "%s\r\n", uri);
+ buf = Brdstr(&bfd, '\n', 1);
+ n = Blinelen(&bfd);
+ if (n > 1024 + 2)
+ fprint(2, "warning: header too long\n");
+ buf[n-1] = '\0';
+ fprint(2, "%s\n", buf);
t = 0;
- while ((n = read(tlsfd, buf + t, 1)) > 0) {
- if (n == 0) break;
- if (buf[t] == '\n') break;
- if (n < 0) sysfatal("read: %r");
- t += n;
- if (t >= 1024 + 2) sysfatal("responce header too long");
- }
- write(2, buf, t-1);
- write(2, "\n", 1);
+ buf = realloc(buf, BSIZE);
- while ((n = read(tlsfd, buf, BSIZE)) > 0) {
- if (n < 0) sysfatal("read: %r");
- t+=n;
+ while ((n = Bread(&bfd, buf, BSIZE)) > 0) {
+ t += n;
write(1, buf, n);
}
+ if (n < 0) fprint(2, "read: %r\n");
fprint(2, "total: %ld\n", t);
+ Bterm(&bfd);
close(tlsfd);
+ close(fd);
}