Bug fixes and select on FIFOs in wcgi - doesn't really work!
[egate.git] / www / wproto.c
1 /*
2  * Copyright (c) 1995, the EUROPAGATE consortium (see below).
3  *
4  * The EUROPAGATE consortium members are:
5  *
6  *    University College Dublin
7  *    Danmarks Teknologiske Videnscenter
8  *    An Chomhairle Leabharlanna
9  *    Consejo Superior de Investigaciones Cientificas
10  *
11  * Permission to use, copy, modify, distribute, and sell this software and
12  * its documentation, in whole or in part, for any purpose, is hereby granted,
13  * provided that:
14  *
15  * 1. This copyright and permission notice appear in all copies of the
16  * software and its documentation. Notices of copyright or attribution
17  * which appear at the beginning of any file must remain unchanged.
18  *
19  * 2. The names of EUROPAGATE or the project partners may not be used to
20  * endorse or promote products derived from this software without specific
21  * prior written permission.
22  *
23  * 3. Users of this software (implementors and gateway operators) agree to
24  * inform the EUROPAGATE consortium of their use of the software. This
25  * information will be used to evaluate the EUROPAGATE project and the
26  * software, and to plan further developments. The consortium may use
27  * the information in later publications.
28  * 
29  * 4. Users of this software agree to make their best efforts, when
30  * documenting their use of the software, to acknowledge the EUROPAGATE
31  * consortium, and the role played by the software in their work.
32  *
33  * THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTY OF ANY KIND,
34  * EXPRESS, IMPLIED, OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
35  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
36  * IN NO EVENT SHALL THE EUROPAGATE CONSORTIUM OR ITS MEMBERS BE LIABLE
37  * FOR ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF
38  * ANY KIND, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA
39  * OR PROFITS, WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND
40  * ON ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
41  * USE OR PERFORMANCE OF THIS SOFTWARE.
42  *
43  * $Log: wproto.c,v $
44  * Revision 1.5  1995/11/02 16:35:37  adam
45  * Bug fixes and select on FIFOs in wcgi - doesn't really work!
46  *
47  * Revision 1.4  1995/10/31  16:56:25  adam
48  * Record presentation.
49  *
50  * Revision 1.3  1995/10/27  15:12:10  adam
51  * IrTcl incorporated in the gateway.
52  * Better separation of script types.
53  * Z39.50 gateway scripts entered.
54  *
55  * Revision 1.2  1995/10/23  16:55:39  adam
56  * A lot of changes - really.
57  *
58  * Revision 1.1  1995/10/20  11:49:26  adam
59  * First version of www gateway.
60  *
61  */
62
63 #include <stdio.h>
64 #include <stdlib.h>
65 #include <sys/time.h>
66 #include <sys/types.h>
67 #include <sys/stat.h>
68 #include <fcntl.h>
69 #include <unistd.h>
70 #include <stdarg.h>
71 #include <ctype.h>
72 #include <errno.h>
73
74 #include "wproto.h"
75
76 static int wproto_dumpcache(WCLIENT wc, int level);
77 static int wproto_findcache(WCLIENT wc, char *name);
78 static void wproto_uncache(WCLIENT wc, int level);
79
80 static char *mod = "wproto";
81
82 void wo_puts(WCLIENT wc, char *s)
83 {
84     int len;
85
86     if (wc->outbuffer_offset + (len = strlen(s)) + 1 > wc->outbuffer_size)
87         wc->outbuffer = realloc(wc->outbuffer, wc->outbuffer_size +=
88         OUTBUFFER_CHUNK);
89     memcpy(wc->outbuffer + wc->outbuffer_offset, s, len + 1);
90     wc->outbuffer_offset += len;
91 }
92
93 void wo_printf(WCLIENT wc, const char *fmt, ...)
94 {
95     va_list ap;
96     char tmpbuf[4048];
97
98     va_start(ap, fmt);
99     vsprintf(tmpbuf, fmt, ap);
100     wo_puts(wc, tmpbuf);
101     va_end(ap);
102 }
103
104 void wo_clear(WCLIENT wc, char *type)
105 {
106     if (!wc->outbuffer)
107         wc->outbuffer = malloc(wc->outbuffer_size = OUTBUFFER_CHUNK);
108     wc->outbuffer_offset = 0;
109     wo_printf(wc, "Content-type: %s\n\n", type);
110 }
111
112 int wo_puthtml(WCLIENT wc, char *name)
113 {
114     FILE *f; 
115     char ch;
116
117     wo_clear(wc, "text/html");
118     if (!(f = fopen(name, "r")))
119     {
120         wo_printf(wc, "<BR>Failed to open file: %s<BR>", name);
121         return 0;
122     }
123     while (ch = getc(f), !feof(f))
124     {
125         if (wo_putc(wc, ch) < 0)
126         {
127             fclose(f);
128             return -1;
129         }
130     }
131     fclose(f);
132     return 0;
133 }
134
135 int wo_flush(WCLIENT wc)
136 {
137     int wrote, towrite;
138
139     if (!(wc->outbuffer_offset))
140         return 0;
141     towrite = wc->outbuffer_offset;
142     wc->outbuffer_offset = 0;
143     for (;;)
144     {
145         wrote = write(wc->lineout, wc->outbuffer + wc->outbuffer_offset,
146             towrite);
147         if (wrote <= 0)
148         {
149             gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "write response");
150             return -1;
151         }
152         gw_log (GW_LOG_DEBUG, mod, "wrote %d bytes", wrote);
153         if (wc->cache_fd >= 0)
154             if (write(wc->cache_fd, wc->outbuffer + wc->outbuffer_offset,
155                 towrite) < 0)
156             {   
157                 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "write cache");
158                 return -1;
159             }
160         towrite -= wrote;
161         if (!towrite)
162             break;
163         wc->outbuffer_offset += wrote;
164     }
165     wc->outbuffer_offset = 0;
166     return 0;
167 }
168
169 int wo_overflow(WCLIENT wc, char ch)
170 {
171     gw_log (GW_LOG_DEBUG, mod, "wo_overflow");
172     if (wo_flush(wc) < 0)
173         return -1;
174     return wo_putc(wc, ch);
175 }
176
177 int wo_finish(WCLIENT wc)
178 {
179     gw_log (GW_LOG_DEBUG, mod, "wo_finish");
180     if (wo_flush(wc) < 0)
181         return -1;
182     close(wc->lineout);
183     wc->lineout = -1;
184     if (wc->cache_fd >= 0)
185     {
186         close(wc->cache_fd);
187         wc->cache_fd = -1;
188     }
189     return 0;
190 }
191
192 static void descramble(char *t, const char *o)
193 {
194     unsigned int v;
195
196     while (*o)
197     {
198         if (*o == '%' && isxdigit(*(o + 1)) && isxdigit(*(o + 2)))
199         {
200             sscanf(o + 1, "%2x", &v);
201             o += 3;
202             *(t++) = (char) v;
203         }
204         else
205             *(t++) = *(o++);
206     }
207     *t = '\0';
208 }
209
210 static void decode_form(wform_data *form, char *buf)
211 {
212     int i = 0;
213     char *p;
214     char tmp[512];
215
216     while (*buf)
217     {
218         for (p = form[i].name; *buf && *buf != '='; buf++)
219             *(p++) = *buf;
220         *p = '\0';
221         if (*buf)
222             buf++;
223         for (p = tmp; *buf && *buf != '&'; buf++)
224             *(p++) = *buf;
225         *p = '\0';
226         descramble(form[i].value, tmp);
227         if (*buf)
228             buf++;
229         i++;
230     }
231     *form[i].name = '\0';
232 }
233
234 char *wgetval(WCLIENT wc, char *name)
235 {
236     int i;
237
238     for (i = 0; *wc->wf_data[i].name; i++)
239         if (!strcmp(name, wc->wf_data[i].name))
240             return wc->wf_data[i].value;
241     return 0;
242 }
243
244 int wproto_process(WCLIENT wc, int timeout)
245 {
246     int toread, rs, level;
247     char combuf[COMBUF], *p,*t;
248     fd_set input;
249     struct timeval to, *top;
250
251     for (;;)
252     {
253         gw_log (GW_LOG_DEBUG, mod, "process waiting for input.");
254         if (timeout > 0)
255         {
256             to.tv_usec = 0;
257             to.tv_sec = timeout;
258             top = &to;
259         }
260         else
261             top = 0;
262         FD_ZERO(&input);
263         FD_SET(wc->linein, &input);
264         /* go through select handle list */
265         while ((rs = select(wc->linein + 1, &input, 0, 0, top)) < 0 &&
266             errno == EINTR)
267             ;
268         if (rs < 0)
269         {
270             gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "select");
271             return -1;
272         }
273         if (rs == 0)
274         {
275             gw_log (GW_LOG_STAT, mod, 
276                     "wproto_process returning 0 after %d second timeout.",
277                     timeout);
278             unlink (wc->wf_serverp);
279             return 0;
280         }
281         /* determine handle (fifo or user) */
282         if (read(wc->linein, &toread, sizeof(toread)) < sizeof(toread))
283         {
284             gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "wp_proc:len read failed");
285             exit(1);
286         }
287         toread -= sizeof(toread);
288         if (read(wc->linein, combuf, toread) < toread)
289         {
290             gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "wp_proc: data read failed");
291             exit(1);
292         }
293         p = combuf;
294         for (t = wc->wf_serverp; (*t = *p); t++, p++);
295         p++;
296         for (t = wc->wf_parms; (*t = *p); t++, p++);
297         p++;
298         p++;         /* we don't deal with envvars yet */
299         decode_form(wc->wf_data, p);
300         if (wc->lineout < 0 && (wc->lineout = open(wc->wf_serverp, O_WRONLY))
301             < 0)
302         {
303             gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "open %s", wc->wf_serverp);
304             exit(1);
305         }
306         /* look in cache only if request carries no forms data. */
307         if (!*wc->wf_data[0].name && (level = wproto_findcache(wc,
308             wc->wf_parms)) >= 0)
309         {
310             wproto_dumpcache(wc, level);
311             wo_finish(wc);
312         }
313         else
314         {
315             unlink (wc->wf_serverp);
316             return 1;
317         }
318     }
319 }
320
321 WCLIENT wproto_init(void)
322 {
323     char path2[256];
324     wclient_data *new;
325
326     gw_log (GW_LOG_DEBUG, mod, "wproto_init");
327     close(1);    /* release us from the wserver */
328     new = malloc(sizeof(*new));
329     new->id = getpid();
330     sprintf(new->path, "%s/%s/clt%d", FIFOROOT, FIFODIR, new->id);
331     if (mkfifo(new->path, 0666 | S_IFIFO) < 0)
332     {
333         gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "mkfifo(%s)", new->path);
334         exit(1);
335     }
336     gw_log (GW_LOG_DEBUG, mod, "Synchronizing with server.");
337     sprintf(path2, "%s/%s/srv%d", FIFOROOT, FIFODIR, getppid());
338     if ((new->lineout = open(path2, O_WRONLY)) < 0)
339     {
340         gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "open out %s", path2);
341         exit(1);
342     }
343     if (write(new->lineout, "OK", 2) < 2)
344     {
345         gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "write");
346         exit(1);
347     }
348     gw_log (GW_LOG_DEBUG, mod, "Synchronized.");
349     if ((new->linein = open(new->path, O_RDONLY)) < 0)
350     {
351         gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "open input %s", new->path);
352         exit(1);
353     }
354     gw_log (GW_LOG_DEBUG, mod, "init. linein=%d lineout=%d",
355             new->linein, new->lineout);
356     /* we put a handle on this so we get a blocking read when no peer */
357     if (open(new->path, O_WRONLY | O_NDELAY) < 0)
358     {
359         gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "open dummy %s", new->path);
360         exit(1);
361     }
362     new->outbuffer = 0;
363     new->cache_level = -1;
364     new->cache_fd = -1;
365     return new;
366 }
367
368 static void wproto_uncache(WCLIENT wc, int level)
369 {
370     for (;wc->cache_level >= level; wc->cache_level--)
371         unlink(wc->cache[wc->cache_level].path);
372 }
373
374 void wproto_terminate(WCLIENT wc)
375 {
376     close(wc->linein);
377     unlink(wc->path);
378     wproto_uncache(wc, 0);
379     free(wc);
380 }
381
382 int wproto_cache(WCLIENT wc, int level)
383 {
384     cache_data *p;
385
386     if (level > wc->cache_level + 1)
387     {
388         gw_log (GW_LOG_FATAL, mod, "Illegal cache level increment.");
389         exit(1);
390     }
391     wproto_uncache(wc, level);
392     p = &wc->cache[++wc->cache_level];
393     sprintf(p->path, "%s/%s/csh%d.%d", FIFOROOT, FIFODIR, wc->id, level);
394     if ((wc->cache_fd = open(p->path, O_WRONLY|O_CREAT|O_TRUNC, 0600)) < 0)
395     {
396         gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "open %s", p->path);
397         return -1;
398     }
399     strcpy(p->name, wc->wf_parms);
400     return 0;
401 }
402
403 static int wproto_findcache(WCLIENT wc, char *name)
404 {
405     int i;
406
407     for (i = 0; i <= wc->cache_level; i++)
408         if (!strcmp(wc->cache[i].name, name))
409             return i;
410     return -1;
411 }
412
413 static int wproto_dumpcache(WCLIENT wc, int level)
414 {
415     int fd, rd;
416
417     gw_log (GW_LOG_STAT, mod, "Using Cache: %s", wc->cache[level].name);
418     if ((fd = open(wc->cache[level].path, O_RDONLY)) < 0)
419     {
420         gw_log (GW_LOG_FATAL, mod, "open (R) %s", wc->cache[level].path);
421         return -1;
422     }
423     while ((rd = read(fd, wc->outbuffer, OUTBUFFER_CHUNK)) > 0)
424         if (write(wc->lineout, wc->outbuffer, rd) < rd)
425         {
426             gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "write toline");
427             return -1;
428         }
429     if (rd < 0)
430     {
431         gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "read");
432         return -1;
433     }
434     wproto_uncache(wc, level + 1);
435     return 0;
436 }