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