gemfetch

simple gemini fetcher for plan9
git clone git://nsmpr.xyz/gemfetch.git
Log | Files | Refs

commit 0da64e5161ebe55fccfd1716a81e21e7ed4ef8ac
Author: Pavel Renev <an2qzavok@gmail.com>
Date:   Wed,  5 May 2021 20:57:34 +0000

initial commit

Diffstat:
Agemfetch.c | 81+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Amkfile | 7+++++++
2 files changed, 88 insertions(+), 0 deletions(-)

diff --git a/gemfetch.c b/gemfetch.c @@ -0,0 +1,81 @@ +#include <u.h> +#include <libc.h> +#include <mp.h> +#include <libsec.h> + +#define BSIZE 4096 + +char * +gethost(char *uri) +{ + char *host, *sp, *ep; + long length; + + sp = strstr(uri, "://"); + if (sp == 0) sp = uri; + else sp += 3; + + ep = strchr(sp, '/'); + if (ep == 0) ep = sp + strlen(sp); + length = ep - sp; + host = mallocz(sizeof(char) * (length+1), 1); + memcpy(host, sp, length); + return host; +} + +void +usage(void) +{ + fprint(2, "usage: %s uri\n", argv0); + exits("usage"); +} + +void +main(int argc, char **argv) +{ + char buf[BSIZE], *uri, *host; + long n, t; + int fd, tlsfd; + TLSconn conn; + + argv0 = argv[0]; + argv++, argc--; + if (argc != 1) usage(); + + uri = argv[0]; + host = gethost(uri); + fprint(2, "fetching from %s\n", host); + fd = dial(netmkaddr(host, "tcp", "1965"), nil, nil, nil); + if (fd <=0) sysfatal("dial: %r"); + + memset(&conn, 0, sizeof(conn)); + + tlsfd = tlsClient(fd, &conn); + if (tlsfd <=0) sysfatal("tlsClient: %r"); + + close(fd); + + fprint(tlsfd, "%s\r\n", uri); + + 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 >= BSIZE - 2) sysfatal("responce header too long"); + } + write(2, buf, t-1); + write(2, "\n", 1); + + while ((n = read(tlsfd, buf, BSIZE)) > 0) { + if (n < 0) sysfatal("read: %r"); + t+=n; + write(1, buf, n); + } + + fprint(2, "total: %ld\n", t); + + close(tlsfd); +} diff --git a/mkfile b/mkfile @@ -0,0 +1,7 @@ +</$objtype/mkfile + +TARG=gemfetch +OFILES=gemfetch.$O +BIN=$home/bin/$objtype + +</sys/src/cmd/mkone