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.2 1995/10/23 16:55:39 adam
45 * A lot of changes - really.
47 * Revision 1.1 1995/10/20 11:49:26 adam
48 * First version of www gateway.
55 #include <sys/types.h>
65 static int wproto_dumpcache(WCLIENT wc, int level);
66 static int wproto_findcache(WCLIENT wc, char *name);
67 static void wproto_uncache(WCLIENT wc, int level);
69 static char *mod = "wproto";
71 void wo_puts(WCLIENT wc, char *s)
75 if (wc->outbuffer_offset + (len = strlen(s)) + 1 > wc->outbuffer_size)
76 wc->outbuffer = realloc(wc->outbuffer, wc->outbuffer_size +=
78 memcpy(wc->outbuffer + wc->outbuffer_offset, s, len + 1);
79 wc->outbuffer_offset += len;
82 void wo_printf(WCLIENT wc, const char *fmt, ...)
88 vsprintf(tmpbuf, fmt, ap);
93 void wo_clear(WCLIENT wc, char *type)
96 wc->outbuffer = malloc(wc->outbuffer_size = OUTBUFFER_CHUNK);
97 wc->outbuffer_offset = 0;
98 wo_printf(wc, "Content-type: %s\n\n", type);
101 int wo_puthtml(WCLIENT wc, char *name)
106 wo_clear(wc, "text/html");
107 if (!(f = fopen(name, "r")))
109 wo_printf(wc, "<BR>Failed to open file: %s<BR>", name);
112 while (ch = getc(f), !feof(f))
114 if (wo_putc(wc, ch) < 0)
124 int wo_flush(WCLIENT wc)
128 if (!(wc->outbuffer_offset))
130 towrite = wc->outbuffer_offset;
131 wc->outbuffer_offset = 0;
134 wrote = write(wc->lineout, wc->outbuffer + wc->outbuffer_offset,
138 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "write response");
141 if (wc->cache_fd >= 0)
142 if (write(wc->cache_fd, wc->outbuffer + wc->outbuffer_offset,
145 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "write cache");
151 wc->outbuffer_offset += wrote;
153 wc->outbuffer_offset = 0;
157 int wo_overflow(WCLIENT wc, char ch)
159 gw_log (GW_LOG_DEBUG, mod, "wo_overflow");
160 if (wo_flush(wc) < 0)
162 return wo_putc(wc, ch);
165 int wo_finish(WCLIENT wc)
167 gw_log (GW_LOG_DEBUG, mod, "wo_finish");
168 if (wo_flush(wc) < 0)
172 if (wc->cache_fd >= 0)
180 static void descramble(char *t, const char *o)
186 if (*o == '%' && isxdigit(*(o + 1)) && isxdigit(*(o + 2)))
188 sscanf(o + 1, "%2x", &v);
198 static void decode_form(wform_data *form, char *buf)
206 for (p = form[i].name; *buf && *buf != '='; buf++)
211 for (p = tmp; *buf && *buf != '&'; buf++)
214 descramble(form[i].value, tmp);
219 *form[i].name = '\0';
222 char *wgetval(WCLIENT wc, char *name)
226 for (i = 0; *wc->wf_data[i].name; i++)
227 if (!strcmp(name, wc->wf_data[i].name))
228 return wc->wf_data[i].value;
232 int wproto_process(WCLIENT wc, int timeout)
234 int toread, rs, level;
235 char combuf[COMBUF], *p,*t;
237 struct timeval to, *top;
241 gw_log (GW_LOG_DEBUG, mod, "process waiting for input.");
251 FD_SET(wc->linein, &input);
252 while ((rs = select(wc->linein + 1, &input, 0, 0, top)) < 0 &&
257 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "select");
262 gw_log (GW_LOG_STAT, mod,
263 "wproto_process returning 0 after %d second timeout.",
267 if (read(wc->linein, &toread, sizeof(toread)) < sizeof(toread))
269 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "wp_proc:len read failed");
272 toread -= sizeof(toread);
273 if (read(wc->linein, combuf, toread) < toread)
275 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "wp_proc: data read failed");
279 for (t = wc->wf_serverp; (*t = *p); t++, p++);
281 for (t = wc->wf_parms; (*t = *p); t++, p++);
283 p++; /* we don't deal with envvars yet */
284 decode_form(wc->wf_data, p);
285 if (wc->lineout < 0 && (wc->lineout = open(wc->wf_serverp, O_WRONLY))
288 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "open %s", wc->wf_serverp);
291 /* look in cache only if request carries no forms data. */
292 if (!*wc->wf_data[0].name && (level = wproto_findcache(wc,
295 wproto_dumpcache(wc, level);
303 WCLIENT wproto_init(void)
308 gw_log (GW_LOG_DEBUG, mod, "wproto_init");
309 close(1); /* release us from the wserver */
310 new = malloc(sizeof(*new));
312 sprintf(new->path, "%s/%s/clt%d", FIFOROOT, FIFODIR, new->id);
313 if (mkfifo(new->path, 0666 | S_IFIFO) < 0)
315 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "mkfifo(%s)", new->path);
318 gw_log (GW_LOG_DEBUG, mod, "Synchronizing with server.");
319 sprintf(path2, "%s/%s/srv%d", FIFOROOT, FIFODIR, getppid());
320 if ((new->lineout = open(path2, O_WRONLY)) < 0)
322 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "open out %s", path2);
325 if (write(new->lineout, "OK", 2) < 2)
327 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "write");
330 gw_log (GW_LOG_DEBUG, mod, "Synchronized.");
331 if ((new->linein = open(new->path, O_RDONLY)) < 0)
333 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "open input %s", new->path);
336 /* we put a handle on this so we get a blocking read when no peer */
337 if (open(new->path, O_WRONLY | O_NDELAY) < 0)
339 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "open dummy %s", new->path);
343 new->cache_level = -1;
348 static void wproto_uncache(WCLIENT wc, int level)
350 for (;wc->cache_level >= level; wc->cache_level--)
351 unlink(wc->cache[wc->cache_level].path);
354 void wproto_terminate(WCLIENT wc)
358 wproto_uncache(wc, 0);
362 int wproto_cache(WCLIENT wc, int level)
366 if (level > wc->cache_level + 1)
368 gw_log (GW_LOG_FATAL, mod, "Illegal cache level increment.");
371 wproto_uncache(wc, level);
372 p = &wc->cache[++wc->cache_level];
373 sprintf(p->path, "%s/%s/csh%d.%d", FIFOROOT, FIFODIR, wc->id, level);
374 if ((wc->cache_fd = open(p->path, O_WRONLY|O_CREAT|O_TRUNC, 0600)) < 0)
376 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "open %s", p->path);
379 strcpy(p->name, wc->wf_parms);
383 static int wproto_findcache(WCLIENT wc, char *name)
387 for (i = 0; i <= wc->cache_level; i++)
388 if (!strcmp(wc->cache[i].name, name))
393 static int wproto_dumpcache(WCLIENT wc, int level)
397 gw_log (GW_LOG_STAT, mod, "Using Cache: %s", wc->cache[level].name);
398 if ((fd = open(wc->cache[level].path, O_RDONLY)) < 0)
400 gw_log (GW_LOG_FATAL, mod, "open (R) %s", wc->cache[level].path);
403 while ((rd = read(fd, wc->outbuffer, OUTBUFFER_CHUNK)) > 0)
404 if (write(wc->lineout, wc->outbuffer, rd) < rd)
406 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "write toline");
411 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "read");
414 wproto_uncache(wc, level + 1);