1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
|
/* invoked from /netlib/pub/search.html */
#include <u.h>
#include <libc.h>
#include <bio.h>
#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,
"<HR><A HREF=\"/netlib/master\">browse netlib</A></BODY>\r\n"},
{1, "BibNet", 250, "/srv/netlib_bibnet", bib_fmt,
"<HR><A HREF=\"/netlib/bibnet\">browse BibNet</A></BODY>\r\n"},
{2, "compgeom", 250, "/srv/netlib_compgeom", no_fmt, "</BODY>\r\n"},
{3, "approx", 250, "/srv/netlib_approximation", no_fmt,
"<HR><A HREF=\"/netlib/a/catalog.html.gz\">hierarchical catalog</A></BODY>\r\n"},
{4, "siam", 50, "/srv/netlib_siam-Secret", no_fmt, "</BODY>\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<e; p++)
if(*p=='\r'){
hwrite(hout, s,p-s);
hprint(hout, "\n");
s = p+1;
}
}
int /* should the filename have .gz appended? */
suffix(char*filename)
{
int n;
char *z;
if(!filename || *filename==0)
return(0);
n = strlen(filename);
if(strncmp(".html",filename+n-5,5)==0)
return(0);
z = malloc(n+50);
if(z == nil)
return(0);
strcpy(z,"/netlib/pub/");
strcat(z,filename);
strcat(z,".gz");
if(access(z,4)==0){
free(z);
return(1);
}
free(z);
return(0);
}
void /* add HREF to "file:" lines */
index_fmt(char*s,char*e)
{
char *p, *filename;
if(strncmp(s,"file",4)==0 && (s[4]==' '||s[4]=='\t')){
for(filename = s+4; strchr(" \t",*filename); filename++){}
for(s = filename; *s && strchr("\r\n",*s)==nil; s++){}
*s++ = '\0';
if(*s=='\n') s++;
hprint(hout, "file: <A HREF=\"/netlib/%s",filename);
if(suffix(filename))
hprint(hout, ".gz");
hprint(hout, "\">%s</A>\r\n",filename);
for(p = s; p<e; p++)
if(*p=='\r'){
hwrite(hout, s,p-s);
hprint(hout, "\n");
s = p+1;
}
}else if(strncmp(s,"lib",3)==0 && (s[3]==' '||s[3]=='\t')){
for(filename = s+3; strchr(" \t",*filename); filename++){}
for(s = filename; *s && strchr("\r\n",*s)==nil; s++){}
*s++ = '\0';
if(*s=='\n') s++;
hprint(hout, "lib: <A HREF=\"/netlib/%s",filename);
hprint(hout, "\">%s</A>\r\n",filename);
for(p = s; p<e; p++)
if(*p=='\r'){
hwrite(hout, s,p-s);
hprint(hout, "\n");
s = p+1;
}
}else{
no_fmt(s,e);
}
}
void /* add HREF to "URL" lines */
bib_fmt(char*s,char*e)
{
char *p, *filename;
for(p = s; p<e; p++)
if(*p=='\r'){
hwrite(hout, s,p-s);
hprint(hout, "\n");
s = p+1;
if(strncmp(s," URL =",6)==0 &&
(filename = strchr(s+6,'"'))!=nil){
filename++;
for(s = filename; *s && strchr("\"\r\n",*s)==nil; s++){}
*s++ = '\0';
p = s;
hprint(hout, " URL =<A HREF=\"%s\">%s</A>",
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, "") == -1)
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, "<HEAD><TITLE>%s/%s</TITLE></HEAD>\r\n<BODY>\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, "<PRE>");
(*db[dbi].fmt)(s,e);
hprint(hout, "</PRE>\r\n");
if(nmatch++>=db[dbi].maxhit){
hprint(hout, "<H4>reached limit at %d hits</H4>\n\r",nmatch);
break;
}
}
if(nmatch==0)
hprint(hout, "<H4>Nothing Found.</H4>\r\n");
hprint(hout, db[dbi].postlude);
hflush(hout);
writelog(c, "Reply: 200 netlib_find %ld %ld\n", hout->seek, hout->seek);
return 1;
}
|