netlib_find.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /* invoked from /netlib/pub/search.html */
  2. #include <u.h>
  3. #include <libc.h>
  4. #include <bio.h>
  5. #include "httpd.h"
  6. #include "httpsrv.h"
  7. void bib_fmt(char*,char*);
  8. void index_fmt(char*,char*);
  9. void no_fmt(char*,char*);
  10. int send(HConnect*);
  11. Hio *hout;
  12. /********** table of databases ************/
  13. typedef struct DB DB;
  14. struct DB
  15. {
  16. int SELECT; /* value from search.html */
  17. char *log; /* abbreviation for logfile */
  18. int maxhit; /* maximum number of hits to return */
  19. char *file; /* searchfs database */
  20. void (*fmt)(char*,char*); /* convert one record to HTML */
  21. char *postlude; /* trailer text */
  22. };
  23. DB db[] =
  24. {
  25. {0, "netlib", 250, "/srv/netlib_DEFAULT", index_fmt,
  26. "<HR><A HREF=\"/netlib/master\">browse netlib</A></BODY>\r\n"},
  27. {1, "BibNet", 250, "/srv/netlib_bibnet", bib_fmt,
  28. "<HR><A HREF=\"/netlib/bibnet\">browse BibNet</A></BODY>\r\n"},
  29. {2, "compgeom", 250, "/srv/netlib_compgeom", no_fmt, "</BODY>\r\n"},
  30. {3, "approx", 250, "/srv/netlib_approximation", no_fmt,
  31. "<HR><A HREF=\"/netlib/a/catalog.html.gz\">hierarchical catalog</A></BODY>\r\n"},
  32. {4, "siam", 50, "/srv/netlib_siam-Secret", no_fmt, "</BODY>\r\n"},
  33. {-1,"",0,"",no_fmt,""}
  34. };
  35. /********** reformat database record as HTML ************/
  36. void /* tr '\015' '\012' ("uncombline") */
  37. no_fmt(char*s,char*e)
  38. {
  39. /* s = start, e = (one past) end of database record */
  40. char *p;
  41. for(p = s; p<e; p++)
  42. if(*p=='\r'){
  43. hwrite(hout, s,p-s);
  44. hprint(hout, "\n");
  45. s = p+1;
  46. }
  47. }
  48. int /* should the filename have .gz appended? */
  49. suffix(char*filename)
  50. {
  51. int n;
  52. char *z;
  53. if(!filename || *filename==0)
  54. return(0);
  55. n = strlen(filename);
  56. if(strncmp(".html",filename+n-5,5)==0)
  57. return(0);
  58. z = malloc(n+50);
  59. if(z == nil)
  60. return(0);
  61. strcpy(z,"/netlib/pub/");
  62. strcat(z,filename);
  63. strcat(z,".gz");
  64. if(access(z,4)==0){
  65. free(z);
  66. return(1);
  67. }
  68. free(z);
  69. return(0);
  70. }
  71. void /* add HREF to "file:" lines */
  72. index_fmt(char*s,char*e)
  73. {
  74. char *p, *filename;
  75. if(strncmp(s,"file",4)==0 && (s[4]==' '||s[4]=='\t')){
  76. for(filename = s+4; strchr(" \t",*filename); filename++){}
  77. for(s = filename; *s && strchr("\r\n",*s)==nil; s++){}
  78. *s++ = '\0';
  79. if(*s=='\n') s++;
  80. hprint(hout, "file: <A HREF=\"/netlib/%s",filename);
  81. if(suffix(filename))
  82. hprint(hout, ".gz");
  83. hprint(hout, "\">%s</A>\r\n",filename);
  84. for(p = s; p<e; p++)
  85. if(*p=='\r'){
  86. hwrite(hout, s,p-s);
  87. hprint(hout, "\n");
  88. s = p+1;
  89. }
  90. }else if(strncmp(s,"lib",3)==0 && (s[3]==' '||s[3]=='\t')){
  91. for(filename = s+3; strchr(" \t",*filename); filename++){}
  92. for(s = filename; *s && strchr("\r\n",*s)==nil; s++){}
  93. *s++ = '\0';
  94. if(*s=='\n') s++;
  95. hprint(hout, "lib: <A HREF=\"/netlib/%s",filename);
  96. hprint(hout, "\">%s</A>\r\n",filename);
  97. for(p = s; p<e; p++)
  98. if(*p=='\r'){
  99. hwrite(hout, s,p-s);
  100. hprint(hout, "\n");
  101. s = p+1;
  102. }
  103. }else{
  104. no_fmt(s,e);
  105. }
  106. }
  107. void /* add HREF to "URL" lines */
  108. bib_fmt(char*s,char*e)
  109. {
  110. char *p, *filename;
  111. for(p = s; p<e; p++)
  112. if(*p=='\r'){
  113. hwrite(hout, s,p-s);
  114. hprint(hout, "\n");
  115. s = p+1;
  116. if(strncmp(s," URL =",6)==0 &&
  117. (filename = strchr(s+6,'"'))!=nil){
  118. filename++;
  119. for(s = filename; *s && strchr("\"\r\n",*s)==nil; s++){}
  120. *s++ = '\0';
  121. p = s;
  122. hprint(hout, " URL =<A HREF=\"%s\">%s</A>",
  123. filename,filename);
  124. }
  125. }
  126. }
  127. /********** main() calls httpheadget() calls send() ************/
  128. void
  129. main(int argc, char **argv)
  130. {
  131. HConnect *c;
  132. c = init(argc, argv);
  133. hout = &c->hout;
  134. if(hparseheaders(c, HSTIMEOUT) >= 0)
  135. send(c);
  136. exits(nil);
  137. }
  138. Biobuf Blist;
  139. Biobuf*
  140. init800fs(char*name,char*pat)
  141. {
  142. int fd800fs, n;
  143. char*search;
  144. fd800fs = open(name, ORDWR);
  145. if(fd800fs < 0)
  146. exits("can't connect to 800fs server");
  147. if(mount(fd800fs, -1, "/mnt", MREPL, "") < 0)
  148. exits("can't mount /mnt");
  149. fd800fs = open("/mnt/search", ORDWR);
  150. n = strlen("search=")+strlen(pat)+1;
  151. search = ezalloc(n);
  152. strcpy(search,"search=");
  153. strcat(search,pat);
  154. write(fd800fs,search,n);
  155. free(search);
  156. Binit(&Blist, fd800fs,OREAD);
  157. return(&Blist);
  158. }
  159. static char *
  160. hq(char *text)
  161. {
  162. int textlen = strlen(text), escapedlen = textlen;
  163. char *escaped, *s, *w;
  164. for(s = text; *s; s++)
  165. if(*s=='<' || *s=='>' || *s=='&')
  166. escapedlen += 4;
  167. escaped = ezalloc(escapedlen+1);
  168. for(s = text, w = escaped; *s; s++){
  169. if(*s == '<'){
  170. strcpy(w, "&lt;");
  171. w += 4;
  172. }else if(*s == '>'){
  173. strcpy(w, "&gt;");
  174. w += 4;
  175. }else if(*s == '&'){
  176. strcpy(w, "&amp;");
  177. w += 5;
  178. }else{
  179. *w++ = *s;
  180. }
  181. }
  182. return escaped;
  183. }
  184. int
  185. send(HConnect *c)
  186. {
  187. Biobuf*blist;
  188. int m, n, dbi, nmatch;
  189. char *pat, *s, *e;
  190. HSPairs *q;
  191. if(strcmp(c->req.meth, "GET") != 0 && strcmp(c->req.meth, "HEAD") != 0)
  192. return hunallowed(c, "GET, HEAD");
  193. if(c->head.expectother || c->head.expectcont)
  194. return hfail(c, HExpectFail, nil);
  195. if(c->req.search == nil || !*c->req.search)
  196. return hfail(c, HNoData, "netlib_find");
  197. s = c->req.search;
  198. while((s = strchr(s, '+')) != nil)
  199. *s++ = ' ';
  200. dbi = -1;
  201. pat = nil;
  202. for(q = hparsequery(c, hstrdup(c, c->req.search)); q; q = q->next){
  203. if(strcmp(q->s, "db") == 0){
  204. m = atoi(q->t);
  205. for(dbi = 0; m!=db[dbi].SELECT; dbi++)
  206. if(db[dbi].SELECT<0)
  207. exits("unrecognized db");
  208. }else if(strcmp(q->s, "pat") == 0){
  209. pat = q->t;
  210. }
  211. }
  212. if(dbi < 0)
  213. exits("missing db field in query");
  214. if(pat == nil)
  215. exits("missing pat field in query");
  216. logit(c, "netlib_find %s %s", db[dbi].log,pat);
  217. blist = init800fs(db[dbi].file,pat);
  218. if(c->req.vermaj){
  219. hokheaders(c);
  220. hprint(hout, "Content-type: text/html\r\n");
  221. hprint(hout, "\r\n");
  222. }
  223. if(strcmp(c->req.meth, "HEAD") == 0){
  224. writelog(c, "Reply: 200 netlib_find 0\n");
  225. hflush(hout);
  226. exits(nil);
  227. }
  228. hprint(hout, "<HEAD><TITLE>%s/%s</TITLE></HEAD>\r\n<BODY>\r\n",
  229. db[dbi].log,hq(pat));
  230. nmatch = 0;
  231. while(s = Brdline(blist, '\n')){ /* get next database record */
  232. n = Blinelen(blist);
  233. e = s+n;
  234. hprint(hout, "<PRE>");
  235. (*db[dbi].fmt)(s,e);
  236. hprint(hout, "</PRE>\r\n");
  237. if(nmatch++>=db[dbi].maxhit){
  238. hprint(hout, "<H4>reached limit at %d hits</H4>\n\r",nmatch);
  239. break;
  240. }
  241. }
  242. if(nmatch==0)
  243. hprint(hout, "<H4>Nothing Found.</H4>\r\n");
  244. hprint(hout, db[dbi].postlude);
  245. hflush(hout);
  246. writelog(c, "Reply: 200 netlib_find %ld %ld\n", hout->seek, hout->seek);
  247. return 1;
  248. }