static int preq(HConnect *c) { if(hparseheaders(c, 0) < 0) return -1; if(strcmp(c->req.meth, "GET") != 0 && strcmp(c->req.meth, "HEAD") != 0) return hunallowed(c, "GET, HEAD"); if(c->head.expectother || c->head.expectcont) return hfail(c, HExpectFail, nil); return 0; }
void main(int argc, char **argv) { fmtinstall('H', httpfmt); fmtinstall('U', hurlfmt); fmtinstall('M', dirmodefmt); aio = Bopen("/sys/lib/webls.allowed", OREAD); dio = Bopen("/sys/lib/webls.denied", OREAD); if(argc == 2){ hinit(&houtb, 1, Hwrite); hout = &houtb; dols(argv[1]); exits(nil); } close(2); connect = init(argc, argv); hout = &connect->hout; vermaj = connect->req.vermaj; if(hparseheaders(connect, HSTIMEOUT) < 0) exits("failed"); if(strcmp(connect->req.meth, "GET") != 0 && strcmp(connect->req.meth, "HEAD") != 0){ hunallowed(connect, "GET, HEAD"); exits("not allowed"); } if(connect->head.expectother || connect->head.expectcont){ hfail(connect, HExpectFail, nil); exits("failed"); } bind("/usr/web", "/", MREPL); if(connect->req.search != nil) dosearch(connect->req.search); else error("Bad argument", "<p>Need a search argument</p>"); hflush(hout); writelog(connect, "200 webls %ld %ld\n", hout->seek, hout->seek); exits(nil); }
void main(int argc, char **argv) { fmtinstall('H', httpfmt); fmtinstall('U', hurlfmt); if(argc == 2){ hinit(&houtb, 1, Hwrite); hout = &houtb; doconvert(argv[1], 0); exits(nil); } close(2); connect = init(argc, argv); hout = &connect->hout; if(hparseheaders(connect, HSTIMEOUT) < 0) exits("failed"); if(strcmp(connect->req.meth, "GET") != 0 && strcmp(connect->req.meth, "HEAD") != 0){ hunallowed(connect, "GET, HEAD"); exits("not allowed"); } if(connect->head.expectother || connect->head.expectcont){ hfail(connect, HExpectFail, nil); exits("failed"); } bind("/usr/web/sys/man", "/sys/man", MREPL); if(connect->req.search != nil) dosearch(connect->req.vermaj, connect->req.search); else doconvert(connect->req.uri, connect->req.vermaj); hflush(hout); writelog(connect, "200 man2html %ld %ld\n", hout->seek, hout->seek); exits(nil); }
void main(int argc, char **argv) { HConnect *c; Dir *dir; Hio *hin, *hout; char *s, *t, *fn; int n, nfn, datafd, htmlfd; c = init(argc, argv); if(dangerous(c->req.uri)){ hfail(c, HSyntax); exits("failed"); } if(hparseheaders(c, HSTIMEOUT) < 0) exits("failed"); hout = &c->hout; if(c->head.expectother){ hfail(c, HExpectFail, nil); exits("failed"); } if(c->head.expectcont){ hprint(hout, "100 Continue\r\n"); hprint(hout, "\r\n"); hflush(hout); } s = nil; if(strcmp(c->req.meth, "POST") == 0){ hin = hbodypush(&c->hin, c->head.contlen, c->head.transenc); if(hin != nil){ alarm(HSTIMEOUT); s = hreadbuf(hin, hin->pos); alarm(0); } if(s == nil){ hfail(c, HBadReq, nil); exits("failed"); } t = strchr(s, '\n'); if(t != nil) *t = '\0'; }else if(strcmp(c->req.meth, "GET") != 0 && strcmp(c->req.meth, "HEAD") != 0){ hunallowed(c, "GET, HEAD, PUT"); exits("unallowed"); }else s = c->req.search; if(s == nil){ hfail(c, HNoData, "save"); exits("failed"); } if(strlen(s) > MaxLog) s[MaxLog] = '\0'; n = snprint(c->xferbuf, HBufSize, "at %ld %s\n", time(0), s); nfn = strlen(c->req.uri) + 64; fn = halloc(c, nfn); /* * open file descriptors & write log line */ snprint(fn, nfn, "/usr/web/save/%s.html", c->req.uri); htmlfd = open(fn, OREAD); if(htmlfd < 0 || (dir = dirfstat(htmlfd)) == nil){ hfail(c, HNotFound, c->req.uri); exits("failed"); return; } snprint(fn, nfn, "/usr/web/save/%s.data", c->req.uri); datafd = openLocked(fn, OWRITE); if(datafd < 0){ errstr(c->xferbuf, sizeof c->xferbuf); if(strstr(c->xferbuf, "locked") != nil) hfail(c, HTempFail, c->req.uri); else hfail(c, HNotFound, c->req.uri); exits("failed"); } seek(datafd, 0, 2); write(datafd, c->xferbuf, n); close(datafd); sendfd(c, htmlfd, dir, hmkcontent(c, "text", "html", nil), nil); exits(nil); }