Implemented a new command, egw_clear, to clear http output cache.
[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.23  1997/01/27 11:27:14  adam
45  * Implemented a new command, egw_clear, to clear http output cache.
46  * Changed prototype for function wo_clear.
47  *
48  * Revision 1.22  1997/01/24 13:13:11  adam
49  * Implemnted egw_source and added a "raw" option to the URL.
50  * Fixed a bug in the buffering system of wproto; the macro wo_putc could
51  * override memory if it was the first HTML generating function called.
52  *
53  * Revision 1.21  1996/05/23 15:53:11  adam
54  * Bug fix: egw_enc failed on 8-bit chars.
55  * New command: egw_parms.
56  *
57  * Revision 1.20  1996/05/21  14:53:04  adam
58  * Tcl command wform extented; options -raw and -exists added.
59  *
60  * Revision 1.19  1996/03/14  11:48:39  adam
61  * New function egw_prog that returns name of shell.
62  *
63  * Revision 1.18  1996/02/12  10:09:23  adam
64  * New parameter to wproto_init: directory root for the FIFOs (instead
65  * of using preprocessor defines FIFODIR/FIFOROOT).
66  *
67  * Revision 1.17  1996/01/26  09:02:22  adam
68  * Open of client FIFO called with O_NDELAY when reconnecting to shell
69  * in order to prevent serious lock if previous shell died without
70  * unlinking client FIFO.
71  *
72  * Revision 1.16  1996/01/24  10:13:56  adam
73  * Bug fix: in function wo_write realloc is used only when memory is already
74  * allocated with malloc.
75  *
76  * Revision 1.15  1996/01/24  08:25:32  adam
77  * Buf fix: Uninitialized outbuffer_offset member.
78  *
79  * Revision 1.14  1996/01/12  13:08:07  adam
80  * CGI script passes name of lock file to the shell. The server will not close
81  * the response FIFO until this file becomes unlocked. This method handles
82  * cancel operations much better.
83  *
84  * Revision 1.13  1996/01/12  10:05:20  adam
85  * If script name ends with ';' HTTP/GET/Expires will be defined.
86  * The cgi interface only reads final handshake if response from
87  * server (shell) was zero-terminated [If it isn't it probably died].
88  *
89  * Revision 1.12  1996/01/05  16:35:02  adam
90  * Minor changes.
91  *
92  * Revision 1.11  1996/01/05  16:21:21  adam
93  * Bug fix: shell (wproto) sometimes closed server FIFO before cgi
94  * program opened it - solution: cgi sends OK when response has been read.
95  *
96  * Revision 1.10  1995/12/22  14:21:16  adam
97  * More work on scan. The search.egw script takes care of cached
98  * query page (doesn't always increment nextSetNo). To make new search set
99  * either 'New query' must be selected or the query page must be reloaded.
100  * The msearch script doesn't do this yet, however.
101  *
102  * Revision 1.9  1995/11/14  16:31:36  adam
103  * Temporary remove of ccl entry.
104  *
105  * Revision 1.8  1995/11/13  15:41:45  adam
106  * Arrow gifs.
107  * Gateway uses record element set names B(rief) and F(ull).
108  * Bug fix. Didn't save idAuthentication correctly.
109  *
110  * Revision 1.7  1995/11/10  14:47:32  adam
111  * Plus (+) characters automatically converted to space in forms.
112  * Work on search in multiple targets. Doesn't work well - yet.
113  * Presentation formats enhanced.
114  *
115  * Revision 1.6  1995/11/06  10:51:17  adam
116  * End of response marker in response from wsh/wproto to wcgi.
117  * Shells are respawned when necessary.
118  *
119  * Revision 1.5  1995/11/02  16:35:37  adam
120  * Bug fixes and select on FIFOs in wcgi - doesn't really work!
121  *
122  * Revision 1.4  1995/10/31  16:56:25  adam
123  * Record presentation.
124  *
125  * Revision 1.3  1995/10/27  15:12:10  adam
126  * IrTcl incorporated in the gateway.
127  * Better separation of script types.
128  * Z39.50 gateway scripts entered.
129  *
130  * Revision 1.2  1995/10/23  16:55:39  adam
131  * A lot of changes - really.
132  *
133  * Revision 1.1  1995/10/20  11:49:26  adam
134  * First version of www gateway.
135  *
136  */
137
138 #include <stdio.h>
139 #include <string.h>
140 #include <stdlib.h>
141 #include <sys/time.h>
142 #include <sys/types.h>
143 #include <sys/stat.h>
144 #include <fcntl.h>
145 #include <unistd.h>
146 #include <stdarg.h>
147 #include <ctype.h>
148 #include <errno.h>
149 #include <assert.h>
150
151 #include "wproto.h"
152
153 static int wproto_dumpcache(WCLIENT wc, int level);
154 static int wproto_findcache(WCLIENT wc, char *name);
155 static void wproto_uncache(WCLIENT wc, int level);
156
157 static char *mod = "wproto";
158
159 void wo_write (WCLIENT wc, const char *s, size_t len)
160 {
161     if (wc->outbuffer_offset + len >= wc->outbuffer_size)
162     {
163         assert (wc->outbuffer);
164         wc->outbuffer = realloc(wc->outbuffer, wc->outbuffer_size +=
165                 ((len >= OUTBUFFER_CHUNK) ? len*2 : OUTBUFFER_CHUNK));
166     }
167     memcpy(wc->outbuffer + wc->outbuffer_offset, s, len);
168     wc->outbuffer_offset += len;
169 }
170
171 void wo_puts (WCLIENT wc, const char *s)
172 {
173     wo_write (wc, s, strlen(s));
174 }
175
176 void wo_printf (WCLIENT wc, const char *fmt, ...)
177 {
178     va_list ap;
179     char tmpbuf[4048];
180
181     va_start(ap, fmt);
182     vsprintf(tmpbuf, fmt, ap);
183     wo_puts(wc, tmpbuf);
184     va_end(ap);
185 }
186
187 void wo_clear (WCLIENT wc)
188 {
189     if (!wc->outbuffer)
190         wc->outbuffer = malloc (wc->outbuffer_size = OUTBUFFER_CHUNK);
191     wc->outbuffer_offset = 0;
192 }
193
194 int wo_flush(WCLIENT wc)
195 {
196     int wrote, towrite;
197
198     if (!(wc->outbuffer_offset))
199         return 0;
200     towrite = wc->outbuffer_offset;
201     wc->outbuffer_offset = 0;
202     for (;;)
203     {
204         int w_chunk;
205
206         w_chunk = towrite;
207         wrote = write(wc->lineout, wc->outbuffer + wc->outbuffer_offset,
208             w_chunk);
209         if (wrote <= 0)
210         {
211             gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "write response");
212             return -1;
213         }
214         gw_log (GW_LOG_DEBUG, mod, "wrote %d bytes", wrote);
215         if (wc->cache_fd >= 0)
216             if (write(wc->cache_fd, wc->outbuffer + wc->outbuffer_offset,
217                 towrite) < 0)
218             {   
219                 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "write cache");
220                 return -1;
221             }
222         towrite -= wrote;
223         if (!towrite)
224             break;
225         wc->outbuffer_offset += wrote;
226     }
227     wc->outbuffer_offset = 0;
228     return 0;
229 }
230
231 int wo_overflow(WCLIENT wc, char ch)
232 {
233     gw_log (GW_LOG_DEBUG, mod, "wo_overflow");
234     if (wo_flush(wc) < 0)
235         return -1;
236     return wo_putc(wc, ch);
237 }
238
239 int wo_finish(WCLIENT wc)
240 {
241     int fd;
242     gw_log (GW_LOG_DEBUG, mod, "wo_finish");
243
244     wo_putc (wc, 0);
245     if (wo_flush(wc) < 0)
246         return -1;
247
248     fd = open (wc->wf_serverf, O_RDONLY);
249     if (fd != -1)
250     {
251         struct flock area;
252         area.l_type = F_RDLCK;
253         area.l_whence = SEEK_SET;
254         area.l_start = 0L;
255         area.l_len = 0L;
256         fcntl (fd, F_SETLKW, &area);
257         close (fd);
258     }
259     close(wc->lineout);
260     wc->lineout = -1;
261     if (wc->cache_fd >= 0)
262     {
263         close(wc->cache_fd);
264         wc->cache_fd = -1;
265     }
266     return 0;
267 }
268
269 static void descramble(char *t, const char *o)
270 {
271     unsigned int v;
272
273     while (*o)
274     {
275         if (*o == '%' && isxdigit(*(o + 1)) && isxdigit(*(o + 2)))
276         {
277             sscanf(o + 1, "%2x", &v);
278             o += 3;
279             if (v == '+')
280                 *t = ' ';
281             else
282                 *t = (char) v;
283             t++;
284         }
285         else
286         {
287             if (*o == '+')
288                 *t = ' ';
289             else
290                 *t = *o;
291             t++;
292             o++;
293         }
294     }
295     *t = '\0';
296 }
297
298 static void decode_form(wform_data *form, char *buf)
299 {
300     int i = 0;
301     char *p;
302     char tmp[512];
303
304     while (*buf)
305     {
306         for (p = form[i].name; *buf && *buf != '='; buf++)
307             *(p++) = *buf;
308         *p = '\0';
309         if (*buf)
310             buf++;
311         for (p = tmp; *buf && *buf != '&'; buf++)
312             *(p++) = *buf;
313         *p = '\0';
314         descramble(form[i].value, tmp);
315         if (*buf)
316             buf++;
317         i++;
318     }
319     *form[i].name = '\0';
320 }
321
322 char *wgetval(WCLIENT wc, char *name)
323 {
324     int i;
325
326     for (i = 0; *wc->wf_data[i].name; i++)
327         if (!strcmp(name, wc->wf_data[i].name))
328             return wc->wf_data[i].value;
329     return 0;
330 }
331
332 int wproto_process(WCLIENT wc, int timeout)
333 {
334     int toread, rs, level;
335     char combuf[COMBUF], *p,*t;
336     fd_set input;
337     struct timeval to, *top;
338
339     for (;;)
340     {
341         gw_log (GW_LOG_DEBUG, mod, "process waiting for input.");
342         if (timeout > 0)
343         {
344             to.tv_usec = 0;
345             to.tv_sec = timeout;
346             top = &to;
347         }
348         else
349             top = 0;
350         FD_ZERO(&input);
351         FD_SET(wc->linein, &input);
352         /* go through select handle list */
353         while ((rs = select(wc->linein + 1, &input, 0, 0, top)) < 0 &&
354             errno == EINTR)
355             ;
356         if (rs < 0)
357         {
358             gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "select");
359             return -1;
360         }
361         if (rs == 0)
362         {
363             gw_log (GW_LOG_STAT, mod, 
364                     "select %d second timeout.",
365                     timeout);
366             unlink (wc->wf_serverp);
367             return 0;
368         }
369         if (read(wc->linein, &toread, sizeof(toread)) < sizeof(toread))
370         {
371             gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "wp_proc:len read failed");
372             exit(1);
373         }
374         toread -= sizeof(toread);
375         if (read(wc->linein, combuf, toread) < toread)
376         {
377             gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "wp_proc: data read failed");
378             exit(1);
379         }
380         p = combuf;
381         for (t = wc->wf_serverp; (*t = *p); t++, p++);
382         p++;
383         for (t = wc->wf_serverf; (*t = *p); t++, p++);
384         p++;
385         for (t = wc->wf_script; *p && *p != '/'; t++, p++)
386             *t = *p;
387         *t = '\0';
388         if (*p)
389             p++;
390         decode_form (wc->wf_parms_var, p);
391         for (t = wc->wf_parms; (*t = *p); t++, p++);
392         p++;
393         p++;         /* we don't deal with envvars yet */
394         wc->raw_data = p;
395         decode_form(wc->wf_data, p);
396         if (wc->lineout < 0)
397         {
398             gw_log (GW_LOG_DEBUG, mod, "open %s", wc->wf_serverp);
399             if ((wc->lineout = open(wc->wf_serverp, O_WRONLY)) < 0)
400             {
401                 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "open %s", 
402                         wc->wf_serverp);
403                 exit(1);
404             }
405         }
406         /* look in cache only if request carries no forms data. */
407         if (!*wc->wf_data[0].name && (level = wproto_findcache(wc,
408             wc->wf_parms)) >= 0)
409         {
410             gw_log (GW_LOG_DEBUG, mod, "wproto_dumpcache");
411             wproto_dumpcache(wc, level);
412             wo_finish(wc);
413             
414         }
415         else
416         {
417             return 1;
418         }
419     }
420 }
421
422 WCLIENT wproto_init (const char *fifoDir, const char *prog)
423 {
424     char *val, path2[256];
425     wclient_data *newp;
426
427     gw_log (GW_LOG_DEBUG, mod, "wproto_init");
428     close(1);    /* release us from the wserver */
429     if (!(newp = malloc(sizeof(*newp))))
430     {
431         gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "malloc");
432         exit (1);
433     }
434     if (!(val = getenv ("GWID")))
435     {
436         gw_log (GW_LOG_FATAL, mod, "GWID not set");
437         exit (1);
438     }
439     if (!(newp->prog = malloc (strlen(prog)+1)))
440     {
441         gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "malloc");
442         exit (1);
443     }
444     strcpy (newp->prog, prog);
445     newp->fifoDir = fifoDir;
446     newp->id = atoi (val);
447     sprintf(newp->path, "%s/clt%d", newp->fifoDir, newp->id);
448     if (mkfifo(newp->path, 0666 | S_IFIFO) < 0)
449         gw_log (GW_LOG_WARN|GW_LOG_ERRNO, mod, "mkfifo(%s)", newp->path);
450     gw_log (GW_LOG_DEBUG, mod, "Synchronizing with server.");
451     sprintf(path2, "%s/srv%d", newp->fifoDir, getppid());
452     if ((newp->lineout = open(path2, O_WRONLY)) < 0)
453     {
454         gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "open out %s", path2);
455         exit(1);
456     }
457     if (write(newp->lineout, "OK", 2) < 2)
458     {
459         gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "write");
460         exit(1);
461     }
462     gw_log (GW_LOG_DEBUG, mod, "Synchronized.");
463     if ((newp->linein = open(newp->path, O_RDONLY)) < 0)
464     {
465         gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "open input %s", newp->path);
466         exit(1);
467     }
468     gw_log (GW_LOG_DEBUG, mod, "init. linein=%d lineout=%d",
469             newp->linein, newp->lineout);
470     /* we put a handle on this so we get a blocking read when no peer */
471     if (open(newp->path, O_WRONLY | O_NDELAY) < 0)
472     {
473         gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "open dummy %s", newp->path);
474         exit(1);
475     }
476     newp->outbuffer = 0;
477     newp->outbuffer_size = 0;
478     newp->outbuffer_offset = 0;
479     newp->cache_level = -1;
480     newp->cache_fd = -1;
481     return newp;
482 }
483
484 static void wproto_uncache(WCLIENT wc, int level)
485 {
486     for (;wc->cache_level >= level; wc->cache_level--)
487         unlink(wc->cache[wc->cache_level].path);
488 }
489
490 void wproto_terminate(WCLIENT wc)
491 {
492     free (wc->prog);
493     close(wc->linein);
494     unlink(wc->path);
495     wproto_uncache(wc, 0);
496     free(wc);
497 }
498
499 int wproto_cache(WCLIENT wc, int level)
500 {
501     cache_data *p;
502
503     if (level > wc->cache_level + 1)
504     {
505         gw_log (GW_LOG_FATAL, mod, "Illegal cache level increment.");
506         exit(1);
507     }
508     wproto_uncache(wc, level);
509     p = &wc->cache[++wc->cache_level];
510     sprintf(p->path, "%s/csh%d.%d", wc->fifoDir, wc->id, level);
511     if ((wc->cache_fd = open(p->path, O_WRONLY|O_CREAT|O_TRUNC, 0600)) < 0)
512     {
513         gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "open %s", p->path);
514         return -1;
515     }
516     strcpy(p->name, wc->wf_parms);
517     return 0;
518 }
519
520 static int wproto_findcache(WCLIENT wc, char *name)
521 {
522     int i;
523
524     for (i = 0; i <= wc->cache_level; i++)
525         if (!strcmp(wc->cache[i].name, name))
526             return i;
527     return -1;
528 }
529
530 static int wproto_dumpcache(WCLIENT wc, int level)
531 {
532     int fd, rd;
533
534     gw_log (GW_LOG_STAT, mod, "Using Cache: %s", wc->cache[level].name);
535     if ((fd = open(wc->cache[level].path, O_RDONLY)) < 0)
536     {
537         gw_log (GW_LOG_FATAL, mod, "open (R) %s", wc->cache[level].path);
538         return -1;
539     }
540     while ((rd = read(fd, wc->outbuffer, OUTBUFFER_CHUNK)) > 0)
541         if (write(wc->lineout, wc->outbuffer, rd) < rd)
542         {
543             gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "write toline");
544             return -1;
545         }
546     if (rd < 0)
547     {
548         gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "read");
549         return -1;
550     }
551     wproto_uncache(wc, level + 1);
552     return 0;
553 }