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