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