ref: 45930f4521a779916b87d051065a8d2b751a7c12
dir: /http.c/
#include <u.h> #include <libc.h> #include <stdio.h> #include "masto9.h" char * httpget(char *token, char *url) { int ctlfd, bodyfd, conn, n; char buf[1024]; char *body; char *bearer_token; ctlfd = open("/mnt/web/clone", ORDWR); if (ctlfd < 0) sysfatal("open: %r"); n = read(ctlfd, buf, sizeof(buf)); if (n < 0) sysfatal("read: %r"); buf[n] = 0; conn = atoi(buf); /* the write(2) syscall (used by fprint) is considered * failed if it returns -1 or N != Nwritten. to check for * error here you'd have to snprint the url to a temporary * buffer, get its length, then write it out and check the * amount written against the length */ if (fprint(ctlfd, "url %s", url) <= 0) sysfatal("write ctl failed 'url %s': %r", url); bearer_token = concat("Authorization: Bearer ", token); if (fprint(ctlfd, "headers %s", bearer_token) <= 0) sysfatal("write ctl failed 'headers'"); snprint(buf, sizeof(buf), "/mnt/web/%d/body", conn); bodyfd = open(buf, OREAD); if (bodyfd < 0) sysfatal("open %s: %r", buf); body = emalloc(TLBUFSIZE * sizeof(char)); if (readn(bodyfd, body, TLBUFSIZE) <= 0) sysfatal("readn: %r"); close(bodyfd); close(ctlfd); return body; } char * httppost(char *token, char *url, char *text) { int ctlfd, bodyfd, conn; char *buf; char *bearer_token; buf = emalloc(TOOTBUFSIZE * sizeof(char)); ctlfd = open("/mnt/web/clone", ORDWR); if (ctlfd < 0) sysfatal("open ctlfd: %r"); /* n = write(ctlfd, Contenttype, sizeof(Contenttype)); */ /* if (n < 0) */ /* sysfatal("write: %r"); */ /* buf[n] = 0; */ conn = atoi(buf); /* the write(2) syscall (used by fprint) is considered * failed if it returns -1 or N != Nwritten. to check for * error here you'd have to snprint the url to a temporary * buffer, get its length, then write it out and check the * amount written against the length */ if (fprint(ctlfd, "url %s", url) <= 0) sysfatal("write ctl failed 'url %s': %r", url); bearer_token = concat("Authorization: Bearer ", token); if (fprint(ctlfd, "headers %s", bearer_token) <= 0) sysfatal("write ctl failed 'headers'"); snprint(buf, TOOTBUFSIZE, "/mnt/web/%d/postbody", conn); bodyfd = open(buf, OWRITE); if (bodyfd < 0) sysfatal("open bodyfd %s: %r", buf); if (write(bodyfd, text, strlen(text)) < 0) sysfatal("write: %r"); close(bodyfd); snprint(buf, TOOTBUFSIZE, "/mnt/web/%d/body", conn); bodyfd = open(buf, OREAD); if (bodyfd < 0) sysfatal("open %s: %r", buf); if (readn(bodyfd, buf, TOOTBUFSIZE) <= 0) sysfatal("readn: %r"); /* print("BUF %s", buf); */ close(bodyfd); close(ctlfd); return buf; } /* static void* */ /* slurp(int fd, int *n) */ /* { */ /* char *b; */ /* int r, sz; */ /* *n = 0; */ /* sz = 32; */ /* if((b = malloc(sz)) == nil) */ /* abort(); */ /* while(1){ */ /* if(*n + 1 == sz){ */ /* sz *= 2; */ /* if((b = realloc(b, sz)) == nil) */ /* abort(); */ /* } */ /* r = read(fd, b + *n, sz - *n - 1); */ /* if(r == 0) */ /* break; */ /* if(r == -1){ */ /* free(b); */ /* return nil; */ /* } */ /* *n += r; */ /* } */ /* b[*n] = 0; */ /* return b; */ /* } */ /* static int */ /* webopen(char *url, char *token, char *dir, int ndir) */ /* { */ /* char buf[16]; */ /* int n, cfd, conn; */ /* if((cfd = open("/mnt/web/clone", ORDWR)) == -1) */ /* return -1; */ /* if((n = read(cfd, buf, sizeof(buf)-1)) == -1) */ /* return -1; */ /* buf[n] = 0; */ /* conn = atoi(buf); */ /* bearer_token = concat("Authorization: Bearer ", token); */ /* if(fprint(cfd, "headers %s", bearer_token) <= 0) */ /* goto Error; */ /* if(fprint(cfd, "url %s", url) == -1) */ /* goto Error; */ /* snprint(dir, ndir, "/mnt/web/%d", conn); */ /* return cfd; */ /* Error: */ /* close(cfd); */ /* return -1; */ /* } */ /* static char* */ /* get(char *url, char *token, int *n) */ /* { */ /* char *r, dir[64], path[80]; */ /* int cfd, dfd; */ /* r = nil; */ /* dfd = -1; */ /* if((cfd = webopen(url, token, dir, sizeof(dir))) == -1) */ /* goto Error; */ /* snprint(path, sizeof(path), "%s/%s", dir, "body"); */ /* if((dfd = open(path, OREAD)) == -1) */ /* goto Error; */ /* r = slurp(dfd, n); */ /* Error: */ /* if(dfd != -1) close(dfd); */ /* if(cfd != -1) close(cfd); */ /* return r; */ /* } */ /* static char* */ /* post(char *url, char *buf, int nbuf, int *nret, Hdr *h) */ /* { */ /* char *r, dir[64], path[80]; */ /* int cfd, dfd, hfd, ok; */ /* r = nil; */ /* ok = 0; */ /* dfd = -1; */ /* if((cfd = webopen(url, dir, sizeof(dir))) == -1) */ /* goto Error; */ /* if(write(cfd, Contenttype, strlen(Contenttype)) == -1) */ /* goto Error; */ /* snprint(path, sizeof(path), "%s/%s", dir, "postbody"); */ /* if((dfd = open(path, OWRITE)) == -1) */ /* goto Error; */ /* if(write(dfd, buf, nbuf) != nbuf) */ /* goto Error; */ /* close(dfd); */ /* snprint(path, sizeof(path), "%s/%s", dir, "body"); */ /* if((dfd = open(path, OREAD)) == -1) */ /* goto Error; */ /* if((r = slurp(dfd, nret)) == nil) */ /* goto Error; */ /* if(h != nil){ */ /* snprint(path, sizeof(path), "%s/%s", dir, h->name); */ /* if((hfd = open(path, OREAD)) == -1) */ /* goto Error; */ /* if((h->val = slurp(hfd, &h->nval)) == nil) */ /* goto Error; */ /* close(hfd); */ /* } */ /* ok = 1; */ /* Error: */ /* if(dfd != -1) close(dfd); */ /* if(cfd != -1) close(cfd); */ /* if(!ok && h != nil) */ /* free(h->val); */ /* return r; */ /* } */