New function zwait that waits for a variable change - due to i/o events
[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.3  1995/10/27 15:12:10  adam
45  * IrTcl incorporated in the gateway.
46  * Better separation of script types.
47  * Z39.50 gateway scripts entered.
48  *
49  * Revision 1.2  1995/10/23  16:55:39  adam
50  * A lot of changes - really.
51  *
52  * Revision 1.1  1995/10/20  11:49:26  adam
53  * First version of www gateway.
54  *
55  */
56
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <sys/time.h>
60 #include <sys/types.h>
61 #include <sys/stat.h>
62 #include <fcntl.h>
63 #include <unistd.h>
64 #include <stdarg.h>
65 #include <ctype.h>
66 #include <errno.h>
67
68 #include "wproto.h"
69
70 static int wproto_dumpcache(WCLIENT wc, int level);
71 static int wproto_findcache(WCLIENT wc, char *name);
72 static void wproto_uncache(WCLIENT wc, int level);
73
74 static char *mod = "wproto";
75
76 void wo_puts(WCLIENT wc, char *s)
77 {
78     int len;
79
80     if (wc->outbuffer_offset + (len = strlen(s)) + 1 > wc->outbuffer_size)
81         wc->outbuffer = realloc(wc->outbuffer, wc->outbuffer_size +=
82         OUTBUFFER_CHUNK);
83     memcpy(wc->outbuffer + wc->outbuffer_offset, s, len + 1);
84     wc->outbuffer_offset += len;
85 }
86
87 void wo_printf(WCLIENT wc, const char *fmt, ...)
88 {
89     va_list ap;
90     char tmpbuf[4048];
91
92     va_start(ap, fmt);
93     vsprintf(tmpbuf, fmt, ap);
94     wo_puts(wc, tmpbuf);
95     va_end(ap);
96 }
97
98 void wo_clear(WCLIENT wc, char *type)
99 {
100     if (!wc->outbuffer)
101         wc->outbuffer = malloc(wc->outbuffer_size = OUTBUFFER_CHUNK);
102     wc->outbuffer_offset = 0;
103     wo_printf(wc, "Content-type: %s\n\n", type);
104 }
105
106 int wo_puthtml(WCLIENT wc, char *name)
107 {
108     FILE *f; 
109     char ch;
110
111     wo_clear(wc, "text/html");
112     if (!(f = fopen(name, "r")))
113     {
114         wo_printf(wc, "<BR>Failed to open file: %s<BR>", name);
115         return 0;
116     }
117     while (ch = getc(f), !feof(f))
118     {
119         if (wo_putc(wc, ch) < 0)
120         {
121             fclose(f);
122             return -1;
123         }
124     }
125     fclose(f);
126     return 0;
127 }
128
129 int wo_flush(WCLIENT wc)
130 {
131     int wrote, towrite;
132
133     if (!(wc->outbuffer_offset))
134         return 0;
135     towrite = wc->outbuffer_offset;
136     wc->outbuffer_offset = 0;
137     for (;;)
138     {
139         wrote = write(wc->lineout, wc->outbuffer + wc->outbuffer_offset,
140             towrite);
141         if (wrote <= 0)
142         {
143             gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "write response");
144             return -1;
145         }
146         if (wc->cache_fd >= 0)
147             if (write(wc->cache_fd, wc->outbuffer + wc->outbuffer_offset,
148                 towrite) < 0)
149             {   
150                 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "write cache");
151                 return -1;
152             }
153         towrite -= wrote;
154         if (!towrite)
155             break;
156         wc->outbuffer_offset += wrote;
157     }
158     wc->outbuffer_offset = 0;
159     return 0;
160 }
161
162 int wo_overflow(WCLIENT wc, char ch)
163 {
164     gw_log (GW_LOG_DEBUG, mod, "wo_overflow");
165     if (wo_flush(wc) < 0)
166         return -1;
167     return wo_putc(wc, ch);
168 }
169
170 int wo_finish(WCLIENT wc)
171 {
172     gw_log (GW_LOG_DEBUG, mod, "wo_finish");
173     if (wo_flush(wc) < 0)
174         return -1;
175     close(wc->lineout);
176     wc->lineout = -1;
177     if (wc->cache_fd >= 0)
178     {
179         close(wc->cache_fd);
180         wc->cache_fd = -1;
181     }
182     return 0;
183 }
184
185 static void descramble(char *t, const char *o)
186 {
187     unsigned int v;
188
189     while (*o)
190     {
191         if (*o == '%' && isxdigit(*(o + 1)) && isxdigit(*(o + 2)))
192         {
193             sscanf(o + 1, "%2x", &v);
194             o += 3;
195             *(t++) = (char) v;
196         }
197         else
198             *(t++) = *(o++);
199     }
200     *t = '\0';
201 }
202
203 static void decode_form(wform_data *form, char *buf)
204 {
205     int i = 0;
206     char *p;
207     char tmp[512];
208
209     while (*buf)
210     {
211         for (p = form[i].name; *buf && *buf != '='; buf++)
212             *(p++) = *buf;
213         *p = '\0';
214         if (*buf)
215             buf++;
216         for (p = tmp; *buf && *buf != '&'; buf++)
217             *(p++) = *buf;
218         *p = '\0';
219         descramble(form[i].value, tmp);
220         if (*buf)
221             buf++;
222         i++;
223     }
224     *form[i].name = '\0';
225 }
226
227 char *wgetval(WCLIENT wc, char *name)
228 {
229     int i;
230
231     for (i = 0; *wc->wf_data[i].name; i++)
232         if (!strcmp(name, wc->wf_data[i].name))
233             return wc->wf_data[i].value;
234     return 0;
235 }
236
237 int wproto_process(WCLIENT wc, int timeout)
238 {
239     int toread, rs, level;
240     char combuf[COMBUF], *p,*t;
241     fd_set input;
242     struct timeval to, *top;
243
244     for (;;)
245     {
246         gw_log (GW_LOG_DEBUG, mod, "process waiting for input.");
247         if (timeout > 0)
248         {
249             to.tv_usec = 0;
250             to.tv_sec = timeout;
251             top = &to;
252         }
253         else
254             top = 0;
255         FD_ZERO(&input);
256         FD_SET(wc->linein, &input);
257         /* go through select handle list */
258         while ((rs = select(wc->linein + 1, &input, 0, 0, top)) < 0 &&
259             errno == EINTR)
260             ;
261         if (rs < 0)
262         {
263             gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "select");
264             return -1;
265         }
266         if (rs == 0)
267         {
268             gw_log (GW_LOG_STAT, mod, 
269                     "wproto_process returning 0 after %d second timeout.",
270                     timeout);
271             return 0;
272         }
273         /* determine handle (fifo or user) */
274         if (read(wc->linein, &toread, sizeof(toread)) < sizeof(toread))
275         {
276             gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "wp_proc:len read failed");
277             exit(1);
278         }
279         toread -= sizeof(toread);
280         if (read(wc->linein, combuf, toread) < toread)
281         {
282             gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "wp_proc: data read failed");
283             exit(1);
284         }
285         p = combuf;
286         for (t = wc->wf_serverp; (*t = *p); t++, p++);
287         p++;
288         for (t = wc->wf_parms; (*t = *p); t++, p++);
289         p++;
290         p++;         /* we don't deal with envvars yet */
291         decode_form(wc->wf_data, p);
292         if (wc->lineout < 0 && (wc->lineout = open(wc->wf_serverp, O_WRONLY))
293             < 0)
294         {
295             gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "open %s", wc->wf_serverp);
296             exit(1);
297         }
298         /* look in cache only if request carries no forms data. */
299         if (!*wc->wf_data[0].name && (level = wproto_findcache(wc,
300             wc->wf_parms)) >= 0)
301         {
302             wproto_dumpcache(wc, level);
303             wo_finish(wc);
304         }
305         else
306             return 1;
307     }
308 }
309
310 WCLIENT wproto_init(void)
311 {
312     char path2[256];
313     wclient_data *new;
314
315     gw_log (GW_LOG_DEBUG, mod, "wproto_init");
316     close(1);    /* release us from the wserver */
317     new = malloc(sizeof(*new));
318     new->id = getpid();
319     sprintf(new->path, "%s/%s/clt%d", FIFOROOT, FIFODIR, new->id);
320     if (mkfifo(new->path, 0666 | S_IFIFO) < 0)
321     {
322         gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "mkfifo(%s)", new->path);
323         exit(1);
324     }
325     gw_log (GW_LOG_DEBUG, mod, "Synchronizing with server.");
326     sprintf(path2, "%s/%s/srv%d", FIFOROOT, FIFODIR, getppid());
327     if ((new->lineout = open(path2, O_WRONLY)) < 0)
328     {
329         gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "open out %s", path2);
330         exit(1);
331     }
332     if (write(new->lineout, "OK", 2) < 2)
333     {
334         gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "write");
335         exit(1);
336     }
337     gw_log (GW_LOG_DEBUG, mod, "Synchronized.");
338     if ((new->linein = open(new->path, O_RDONLY)) < 0)
339     {
340         gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "open input %s", new->path);
341         exit(1);
342     }
343     /* we put a handle on this so we get a blocking read when no peer */
344     if (open(new->path, O_WRONLY | O_NDELAY) < 0)
345     {
346         gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "open dummy %s", new->path);
347         exit(1);
348     }
349     new->outbuffer = 0;
350     new->cache_level = -1;
351     new->cache_fd = -1;
352     return new;
353 }
354
355 static void wproto_uncache(WCLIENT wc, int level)
356 {
357     for (;wc->cache_level >= level; wc->cache_level--)
358         unlink(wc->cache[wc->cache_level].path);
359 }
360
361 void wproto_terminate(WCLIENT wc)
362 {
363     close(wc->linein);
364     unlink(wc->path);
365     wproto_uncache(wc, 0);
366     free(wc);
367 }
368
369 int wproto_cache(WCLIENT wc, int level)
370 {
371     cache_data *p;
372
373     if (level > wc->cache_level + 1)
374     {
375         gw_log (GW_LOG_FATAL, mod, "Illegal cache level increment.");
376         exit(1);
377     }
378     wproto_uncache(wc, level);
379     p = &wc->cache[++wc->cache_level];
380     sprintf(p->path, "%s/%s/csh%d.%d", FIFOROOT, FIFODIR, wc->id, level);
381     if ((wc->cache_fd = open(p->path, O_WRONLY|O_CREAT|O_TRUNC, 0600)) < 0)
382     {
383         gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "open %s", p->path);
384         return -1;
385     }
386     strcpy(p->name, wc->wf_parms);
387     return 0;
388 }
389
390 static int wproto_findcache(WCLIENT wc, char *name)
391 {
392     int i;
393
394     for (i = 0; i <= wc->cache_level; i++)
395         if (!strcmp(wc->cache[i].name, name))
396             return i;
397     return -1;
398 }
399
400 static int wproto_dumpcache(WCLIENT wc, int level)
401 {
402     int fd, rd;
403
404     gw_log (GW_LOG_STAT, mod, "Using Cache: %s", wc->cache[level].name);
405     if ((fd = open(wc->cache[level].path, O_RDONLY)) < 0)
406     {
407         gw_log (GW_LOG_FATAL, mod, "open (R) %s", wc->cache[level].path);
408         return -1;
409     }
410     while ((rd = read(fd, wc->outbuffer, OUTBUFFER_CHUNK)) > 0)
411         if (write(wc->lineout, wc->outbuffer, rd) < rd)
412         {
413             gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "write toline");
414             return -1;
415         }
416     if (rd < 0)
417     {
418         gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "read");
419         return -1;
420     }
421     wproto_uncache(wc, level + 1);
422     return 0;
423 }