2 * Copyright (c) 1995, the EUROPAGATE consortium (see below).
4 * The EUROPAGATE consortium members are:
6 * University College Dublin
7 * Danmarks Teknologiske Videnscenter
8 * An Chomhairle Leabharlanna
9 * Consejo Superior de Investigaciones Cientificas
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,
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.
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.
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.
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.
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.
44 * Revision 1.26 1997/12/15 15:05:02 adam
45 * Enhanced the code that deals with FORM variables.
47 * Revision 1.25 1997/02/10 10:15:38 adam
48 * Bug fix: egw_source <file> <var> didn't work properly.
50 * Revision 1.24 1997/01/31 11:16:00 adam
51 * Enhanced the egw_source command. An optional parameter specifies the
52 * name of a variable in which the HTML output is stored.
54 * Revision 1.23 1997/01/27 11:27:14 adam
55 * Implemented a new command, egw_clear, to clear http output cache.
56 * Changed prototype for function wo_clear.
58 * Revision 1.22 1997/01/24 13:13:11 adam
59 * Implemnted egw_source and added a "raw" option to the URL.
60 * Fixed a bug in the buffering system of wproto; the macro wo_putc could
61 * override memory if it was the first HTML generating function called.
63 * Revision 1.21 1996/05/23 15:53:11 adam
64 * Bug fix: egw_enc failed on 8-bit chars.
65 * New command: egw_parms.
67 * Revision 1.20 1996/05/21 14:53:04 adam
68 * Tcl command wform extented; options -raw and -exists added.
70 * Revision 1.19 1996/03/14 11:48:39 adam
71 * New function egw_prog that returns name of shell.
73 * Revision 1.18 1996/02/12 10:09:23 adam
74 * New parameter to wproto_init: directory root for the FIFOs (instead
75 * of using preprocessor defines FIFODIR/FIFOROOT).
77 * Revision 1.17 1996/01/26 09:02:22 adam
78 * Open of client FIFO called with O_NDELAY when reconnecting to shell
79 * in order to prevent serious lock if previous shell died without
80 * unlinking client FIFO.
82 * Revision 1.16 1996/01/24 10:13:56 adam
83 * Bug fix: in function wo_write realloc is used only when memory is already
84 * allocated with malloc.
86 * Revision 1.15 1996/01/24 08:25:32 adam
87 * Buf fix: Uninitialized outbuffer_offset member.
89 * Revision 1.14 1996/01/12 13:08:07 adam
90 * CGI script passes name of lock file to the shell. The server will not close
91 * the response FIFO until this file becomes unlocked. This method handles
92 * cancel operations much better.
94 * Revision 1.13 1996/01/12 10:05:20 adam
95 * If script name ends with ';' HTTP/GET/Expires will be defined.
96 * The cgi interface only reads final handshake if response from
97 * server (shell) was zero-terminated [If it isn't it probably died].
99 * Revision 1.12 1996/01/05 16:35:02 adam
102 * Revision 1.11 1996/01/05 16:21:21 adam
103 * Bug fix: shell (wproto) sometimes closed server FIFO before cgi
104 * program opened it - solution: cgi sends OK when response has been read.
106 * Revision 1.10 1995/12/22 14:21:16 adam
107 * More work on scan. The search.egw script takes care of cached
108 * query page (doesn't always increment nextSetNo). To make new search set
109 * either 'New query' must be selected or the query page must be reloaded.
110 * The msearch script doesn't do this yet, however.
112 * Revision 1.9 1995/11/14 16:31:36 adam
113 * Temporary remove of ccl entry.
115 * Revision 1.8 1995/11/13 15:41:45 adam
117 * Gateway uses record element set names B(rief) and F(ull).
118 * Bug fix. Didn't save idAuthentication correctly.
120 * Revision 1.7 1995/11/10 14:47:32 adam
121 * Plus (+) characters automatically converted to space in forms.
122 * Work on search in multiple targets. Doesn't work well - yet.
123 * Presentation formats enhanced.
125 * Revision 1.6 1995/11/06 10:51:17 adam
126 * End of response marker in response from wsh/wproto to wcgi.
127 * Shells are respawned when necessary.
129 * Revision 1.5 1995/11/02 16:35:37 adam
130 * Bug fixes and select on FIFOs in wcgi - doesn't really work!
132 * Revision 1.4 1995/10/31 16:56:25 adam
133 * Record presentation.
135 * Revision 1.3 1995/10/27 15:12:10 adam
136 * IrTcl incorporated in the gateway.
137 * Better separation of script types.
138 * Z39.50 gateway scripts entered.
140 * Revision 1.2 1995/10/23 16:55:39 adam
141 * A lot of changes - really.
143 * Revision 1.1 1995/10/20 11:49:26 adam
144 * First version of www gateway.
151 #include <sys/time.h>
152 #include <sys/types.h>
153 #include <sys/stat.h>
163 static int wproto_dumpcache(WCLIENT wc, int level);
164 static int wproto_findcache(WCLIENT wc, char *name);
165 static void wproto_uncache(WCLIENT wc, int level);
167 static char *mod = "wproto";
169 void wo_expand (WCLIENT wc, size_t len)
171 assert (wc->outbuffer);
172 wc->outbuffer = realloc(wc->outbuffer, wc->outbuffer_size +=
173 ((len >= OUTBUFFER_CHUNK) ? len*2 : OUTBUFFER_CHUNK));
176 void wo_write (WCLIENT wc, const char *s, size_t len)
178 if (wc->outbuffer_offset + len >= wc->outbuffer_size)
180 memcpy(wc->outbuffer + wc->outbuffer_offset, s, len);
181 wc->outbuffer_offset += len;
184 void wo_puts (WCLIENT wc, const char *s)
186 wo_write (wc, s, strlen(s));
189 void wo_printf (WCLIENT wc, const char *fmt, ...)
195 vsprintf(tmpbuf, fmt, ap);
200 void wo_clear (WCLIENT wc)
203 wc->outbuffer = malloc (wc->outbuffer_size = OUTBUFFER_CHUNK);
204 wc->outbuffer_offset = 0;
207 int wo_flush(WCLIENT wc)
211 if (!(wc->outbuffer_offset))
213 towrite = wc->outbuffer_offset;
214 wc->outbuffer_offset = 0;
220 wrote = write(wc->lineout, wc->outbuffer + wc->outbuffer_offset,
224 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "write response");
227 gw_log (GW_LOG_DEBUG, mod, "wrote %d bytes", wrote);
228 if (wc->cache_fd >= 0)
229 if (write(wc->cache_fd, wc->outbuffer + wc->outbuffer_offset,
232 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "write cache");
238 wc->outbuffer_offset += wrote;
240 wc->outbuffer_offset = 0;
244 int wo_overflow(WCLIENT wc, char ch)
246 gw_log (GW_LOG_DEBUG, mod, "wo_overflow");
249 else if (wo_flush(wc) < 0)
251 return wo_putc(wc, ch);
254 void destroy_form (wform_data *p)
258 wform_data *p_next = p->next;
268 int wo_finish(WCLIENT wc)
271 gw_log (GW_LOG_DEBUG, mod, "wo_finish");
274 if (wo_flush(wc) < 0)
275 gw_log (GW_LOG_WARN, mod, "wo_flush failed in wo_finish");
276 fd = open (wc->wf_serverf, O_RDONLY);
280 area.l_type = F_RDLCK;
281 area.l_whence = SEEK_SET;
284 fcntl (fd, F_SETLKW, &area);
289 if (wc->cache_fd >= 0)
295 destroy_form (wc->wf_data);
296 destroy_form (wc->wf_parms_var);
300 static void descramble(char *t, const char *o)
306 if (*o == '%' && isxdigit(*(o + 1)) && isxdigit(*(o + 2)))
308 sscanf(o + 1, "%2x", &v);
329 wform_data *decode_form(char *buf)
331 wform_data *p, **pp = &p;
338 *pp = malloc (sizeof(**pp));
339 for (l = 0; *buf && *buf != '='; buf++)
340 if (l < (sizeof(tmp)-1))
343 (*pp)->name = malloc (strlen(tmp)+1);
344 strcpy ((*pp)->name, tmp);
347 for (l = 0; *buf && *buf != '&'; buf++)
348 if (l < (sizeof(tmp)-1))
351 (*pp)->value = malloc (strlen(tmp)+1);
352 descramble((*pp)->value, tmp);
361 char *wgetval(WCLIENT wc, char *name)
365 for (p = wc->wf_data; p; p = p->next)
366 if (!strcmp(name, p->name))
371 int wproto_process(WCLIENT wc, int timeout)
373 int toread, rs, level;
374 char combuf[COMBUF], *p,*t;
376 struct timeval to, *top;
380 gw_log (GW_LOG_DEBUG, mod, "process waiting for input.");
390 FD_SET(wc->linein, &input);
391 /* go through select handle list */
392 while ((rs = select(wc->linein + 1, &input, 0, 0, top)) < 0 &&
397 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "select");
402 gw_log (GW_LOG_STAT, mod,
403 "select %d second timeout.",
405 unlink (wc->wf_serverp);
408 if (read(wc->linein, &toread, sizeof(toread)) < sizeof(toread))
410 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "wp_proc:len read failed");
413 toread -= sizeof(toread);
414 if (read(wc->linein, combuf, toread) < toread)
416 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "wp_proc: data read failed");
420 for (t = wc->wf_serverp; (*t = *p); t++, p++);
422 for (t = wc->wf_serverf; (*t = *p); t++, p++);
424 for (t = wc->wf_script; *p && *p != '/'; t++, p++)
429 wc->wf_parms_var = decode_form (p);
430 wc->wf_parms = malloc (strlen(p)+1);
431 for (t = wc->wf_parms; (*t = *p); t++, p++)
434 p++; /* we don't deal with envvars yet */
436 wc->wf_data = decode_form (p);
439 gw_log (GW_LOG_DEBUG, mod, "open %s", wc->wf_serverp);
440 if ((wc->lineout = open(wc->wf_serverp, O_WRONLY)) < 0)
442 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "open %s",
447 /* look in cache only if request carries no forms data. */
448 if (!wc->wf_data && (level = wproto_findcache(wc,
451 gw_log (GW_LOG_DEBUG, mod, "wproto_dumpcache");
452 wproto_dumpcache(wc, level);
463 WCLIENT wproto_init (const char *fifoDir, const char *prog)
465 char *val, path2[256];
468 gw_log (GW_LOG_DEBUG, mod, "wproto_init");
469 close(1); /* release us from the wserver */
470 if (!(newp = malloc(sizeof(*newp))))
472 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "malloc");
475 if (!(val = getenv ("GWID")))
477 gw_log (GW_LOG_FATAL, mod, "GWID not set");
480 if (!(newp->prog = malloc (strlen(prog)+1)))
482 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "malloc");
485 strcpy (newp->prog, prog);
486 newp->fifoDir = fifoDir;
487 newp->id = atoi (val);
488 sprintf(newp->path, "%s/clt%d", newp->fifoDir, newp->id);
489 if (mkfifo(newp->path, 0666 | S_IFIFO) < 0)
490 gw_log (GW_LOG_WARN|GW_LOG_ERRNO, mod, "mkfifo(%s)", newp->path);
491 gw_log (GW_LOG_DEBUG, mod, "Synchronizing with server.");
492 sprintf(path2, "%s/srv%d", newp->fifoDir, getppid());
493 if ((newp->lineout = open(path2, O_WRONLY)) < 0)
495 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "open out %s", path2);
498 if (write(newp->lineout, "OK", 2) < 2)
500 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "write");
503 gw_log (GW_LOG_DEBUG, mod, "Synchronized.");
504 if ((newp->linein = open(newp->path, O_RDONLY)) < 0)
506 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "open input %s", newp->path);
509 gw_log (GW_LOG_DEBUG, mod, "init. linein=%d lineout=%d",
510 newp->linein, newp->lineout);
511 /* we put a handle on this so we get a blocking read when no peer */
512 if (open(newp->path, O_WRONLY | O_NDELAY) < 0)
514 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "open dummy %s", newp->path);
518 newp->outbuffer_size = 0;
519 newp->outbuffer_offset = 0;
520 newp->cache_level = -1;
522 newp->save_level = 0;
526 static void wproto_uncache(WCLIENT wc, int level)
528 for (;wc->cache_level >= level; wc->cache_level--)
529 unlink(wc->cache[wc->cache_level].path);
532 void wproto_terminate(WCLIENT wc)
537 wproto_uncache(wc, 0);
541 int wproto_cache(WCLIENT wc, int level)
545 if (level > wc->cache_level + 1)
547 gw_log (GW_LOG_FATAL, mod, "Illegal cache level increment.");
550 wproto_uncache(wc, level);
551 p = &wc->cache[++wc->cache_level];
552 sprintf(p->path, "%s/csh%d.%d", wc->fifoDir, wc->id, level);
553 if ((wc->cache_fd = open(p->path, O_WRONLY|O_CREAT|O_TRUNC, 0600)) < 0)
555 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "open %s", p->path);
558 strcpy(p->name, wc->wf_parms);
562 static int wproto_findcache(WCLIENT wc, char *name)
566 for (i = 0; i <= wc->cache_level; i++)
567 if (!strcmp(wc->cache[i].name, name))
572 int wproto_save_push (WCLIENT wc)
575 return wc->outbuffer_offset;
578 char *wproto_save_pop (WCLIENT wc, int offset)
584 assert (offset <= wc->outbuffer_offset);
585 cp = wc->outbuffer + offset;
586 wc->outbuffer[wc->outbuffer_offset] = '\0';
587 wc->outbuffer_offset = offset;
591 static int wproto_dumpcache(WCLIENT wc, int level)
595 gw_log (GW_LOG_STAT, mod, "Using Cache: %s", wc->cache[level].name);
596 if ((fd = open(wc->cache[level].path, O_RDONLY)) < 0)
598 gw_log (GW_LOG_FATAL, mod, "open (R) %s", wc->cache[level].path);
601 while ((rd = read(fd, wc->outbuffer, OUTBUFFER_CHUNK)) > 0)
602 if (write(wc->lineout, wc->outbuffer, rd) < rd)
604 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "write toline");
609 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "read");
612 wproto_uncache(wc, level + 1);