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