#include #include #include char *mntwebfs = "/mnt/web"; Webfs* webfs(char *url, ...) { int ctlfd, n; char buf[1024+1]; va_list arg; Webfs *w; snprint(buf, sizeof(buf), "%s/clone", mntwebfs); if((ctlfd = open(buf, ORDWR)) < 0) return nil; if((n = read(ctlfd, buf, sizeof(buf-1))) <= 0){ close(ctlfd); return nil; } buf[n] = 0; w = mallocz(sizeof(*w), 1); if(w == nil){ close(ctlfd); return nil; } w->conn = atoi(buf); w->ctlfd = ctlfd; w->bodyfd = -1; va_start(arg, url); vsnprint(w->url, sizeof(w->url), url, arg); va_end(arg); return w; } int webfshdr(Webfs *w, char *key, char *value) { int n; char hdr[256]; n = snprint(hdr, sizeof(hdr), "headers %s: %s", key, value); if(write(w->ctlfd, hdr, n) != n) return -1; return 0; } static int doreq(Webfs *w) { int fd, n; char buf[1024]; n = snprint(buf, sizeof(buf), "url %s", w->url); if(write(w->ctlfd, buf, n) != n) return -1; if(w->useragent[0]){ n = snprint(buf, sizeof(buf), "useragent %s", w->useragent); if(write(w->ctlfd, buf, n) != n) return -1; } if(w->method[0]){ n = snprint(buf, sizeof(buf), "request %s", w->method); if(write(w->ctlfd, buf, n) != n) return -1; } if(w->postbody != nil && w->npostbody > 0){ snprint(buf, sizeof(buf), "%s/%d/postbody", mntwebfs, w->conn); fd = open(buf, ORDWR); if(fd < 0) return -1; n = write(fd, w->postbody, w->npostbody); close(fd); if(n != w->npostbody){ return -1; } } snprint(buf, sizeof(buf), "%s/%d/body", mntwebfs, w->conn); fd = open(buf, OREAD); if(fd < 0) return -1; w->bodyfd = fd; return 0; } int webfsget(Webfs *w, char *buf, int len) { int n, r; if(doreq(w) < 0) return -1; n = 0; while(n < len){ r = read(w->bodyfd, buf+n, len-n); if(r <= 0) break; n += r; } return n; } int webfspost(Webfs *w, char *body, char *data, int len) { w->postbody = strdup(body); w->npostbody = strlen(body); return webfsget(w, data, len); } void webfsfree(Webfs *w) { if(w->ctlfd >= 0) close(w->ctlfd); if(w->bodyfd >= 0) close(w->bodyfd); if(w->postbody != nil) free(w->postbody); free(w); }