/* invoked from /netlib/pub/search.html */ #include #include #include #include "httpd.h" #include "httpsrv.h" void bib_fmt(char*,char*); void index_fmt(char*,char*); void no_fmt(char*,char*); int send(HConnect*); Hio *hout; /********** table of databases ************/ typedef struct DB DB; struct DB { int SELECT; /* value from search.html */ char *log; /* abbreviation for logfile */ int maxhit; /* maximum number of hits to return */ char *file; /* searchfs database */ void (*fmt)(char*,char*); /* convert one record to HTML */ char *postlude; /* trailer text */ }; DB db[] = { {0, "netlib", 250, "/srv/netlib_DEFAULT", index_fmt, "
browse netlib\r\n"}, {1, "BibNet", 250, "/srv/netlib_bibnet", bib_fmt, "
browse BibNet\r\n"}, {2, "compgeom", 250, "/srv/netlib_compgeom", no_fmt, "\r\n"}, {3, "approx", 250, "/srv/netlib_approximation", no_fmt, "
hierarchical catalog\r\n"}, {4, "siam", 50, "/srv/netlib_siam-Secret", no_fmt, "\r\n"}, {-1,"",0,"",no_fmt,""} }; /********** reformat database record as HTML ************/ void /* tr '\015' '\012' ("uncombline") */ no_fmt(char*s,char*e) { /* s = start, e = (one past) end of database record */ char *p; for(p = s; p%s\r\n",filename); for(p = s; p%s\r\n",filename); for(p = s; p%s", filename,filename); } } } /********** main() calls httpheadget() calls send() ************/ void main(int argc, char **argv) { HConnect *c; c = init(argc, argv); hout = &c->hout; if(hparseheaders(c, HSTIMEOUT) >= 0) send(c); exits(nil); } Biobuf Blist; Biobuf* init800fs(char*name,char*pat) { int fd800fs, n; char*search; fd800fs = open(name, ORDWR); if(fd800fs < 0) exits("can't connect to 800fs server"); if(mount(fd800fs, -1, "/mnt", MREPL, "") < 0) exits("can't mount /mnt"); fd800fs = open("/mnt/search", ORDWR); n = strlen("search=")+strlen(pat)+1; search = ezalloc(n); strcpy(search,"search="); strcat(search,pat); write(fd800fs,search,n); free(search); Binit(&Blist, fd800fs,OREAD); return(&Blist); } static char * hq(char *text) { int textlen = strlen(text), escapedlen = textlen; char *escaped, *s, *w; for(s = text; *s; s++) if(*s=='<' || *s=='>' || *s=='&') escapedlen += 4; escaped = ezalloc(escapedlen+1); for(s = text, w = escaped; *s; s++){ if(*s == '<'){ strcpy(w, "<"); w += 4; }else if(*s == '>'){ strcpy(w, ">"); w += 4; }else if(*s == '&'){ strcpy(w, "&"); w += 5; }else{ *w++ = *s; } } return escaped; } int send(HConnect *c) { Biobuf*blist; int m, n, dbi, nmatch; char *pat, *s, *e; HSPairs *q; 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); if(c->req.search == nil || !*c->req.search) return hfail(c, HNoData, "netlib_find"); s = c->req.search; while((s = strchr(s, '+')) != nil) *s++ = ' '; dbi = -1; pat = nil; for(q = hparsequery(c, hstrdup(c, c->req.search)); q; q = q->next){ if(strcmp(q->s, "db") == 0){ m = atoi(q->t); for(dbi = 0; m!=db[dbi].SELECT; dbi++) if(db[dbi].SELECT<0) exits("unrecognized db"); }else if(strcmp(q->s, "pat") == 0){ pat = q->t; } } if(dbi < 0) exits("missing db field in query"); if(pat == nil) exits("missing pat field in query"); logit(c, "netlib_find %s %s", db[dbi].log,pat); blist = init800fs(db[dbi].file,pat); if(c->req.vermaj){ hokheaders(c); hprint(hout, "Content-type: text/html\r\n"); hprint(hout, "\r\n"); } if(strcmp(c->req.meth, "HEAD") == 0){ writelog(c, "Reply: 200 netlib_find 0\n"); hflush(hout); exits(nil); } hprint(hout, "%s/%s\r\n\r\n", db[dbi].log,hq(pat)); nmatch = 0; while(s = Brdline(blist, '\n')){ /* get next database record */ n = Blinelen(blist); e = s+n; hprint(hout, "
");
		(*db[dbi].fmt)(s,e);
		hprint(hout, "
\r\n"); if(nmatch++>=db[dbi].maxhit){ hprint(hout, "

reached limit at %d hits

\n\r",nmatch); break; } } if(nmatch==0) hprint(hout, "

Nothing Found.

\r\n"); hprint(hout, db[dbi].postlude); hflush(hout); writelog(c, "Reply: 200 netlib_find %ld %ld\n", hout->seek, hout->seek); return 1; }