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.1 1995/10/20 11:49:26 adam
45 * First version of www gateway.
52 #include <sys/types.h>
62 static int wproto_dumpcache(WCLIENT wc, int level);
63 static int wproto_findcache(WCLIENT wc, char *name);
64 static void wproto_uncache(WCLIENT wc, int level);
66 static char *mod = "wproto";
68 void wo_puts(WCLIENT wc, char *s)
72 if (wc->outbuffer_offset + (len = strlen(s)) + 1 > wc->outbuffer_size)
73 wc->outbuffer = realloc(wc->outbuffer, wc->outbuffer_size +=
75 memcpy(wc->outbuffer + wc->outbuffer_offset, s, len + 1);
76 wc->outbuffer_offset += len;
79 void wo_printf(WCLIENT wc, const char *fmt, ...)
85 vsprintf(tmpbuf, fmt, ap);
90 void wo_clear(WCLIENT wc, char *type)
93 wc->outbuffer = malloc(wc->outbuffer_size = OUTBUFFER_CHUNK);
94 wc->outbuffer_offset = 0;
95 wo_printf(wc, "Content-type: %s\n\n", type);
98 int wo_puthtml(WCLIENT wc, char *name)
103 wo_clear(wc, "text/html");
104 if (!(f = fopen(name, "r")))
106 wo_printf(wc, "<BR>Failed to open file: %s<BR>", name);
109 while (ch = getc(f), !feof(f))
111 if (wo_putc(wc, ch) < 0)
121 int wo_flush(WCLIENT wc)
125 if (!(wc->outbuffer_offset))
127 towrite = wc->outbuffer_offset;
128 wc->outbuffer_offset = 0;
131 wrote = write(wc->lineout, wc->outbuffer + wc->outbuffer_offset,
135 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "write response");
138 if (wc->cache_fd >= 0)
139 if (write(wc->cache_fd, wc->outbuffer + wc->outbuffer_offset,
142 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "write cache");
148 wc->outbuffer_offset += wrote;
150 wc->outbuffer_offset = 0;
154 int wo_overflow(WCLIENT wc, char ch)
156 gw_log (GW_LOG_DEBUG, mod, "wo_overflow");
157 if (wo_flush(wc) < 0)
159 return wo_putc(wc, ch);
162 int wo_finish(WCLIENT wc)
164 gw_log (GW_LOG_DEBUG, mod, "wo_finish");
165 if (wo_flush(wc) < 0)
169 if (wc->cache_fd >= 0)
177 static void descramble(char *t, const char *o)
183 if (*o == '%' && isxdigit(*(o + 1)) && isxdigit(*(o + 2)))
185 sscanf(o + 1, "%2x", &v);
195 static void decode_form(wform_data *form, char *buf)
203 for (p = form[i].name; *buf && *buf != '='; buf++)
208 for (p = tmp; *buf && *buf != '&'; buf++)
211 descramble(form[i].value, tmp);
216 *form[i].name = '\0';
219 char *wgetval(WCLIENT wc, char *name)
223 for (i = 0; *wc->wf_data[i].name; i++)
224 if (!strcmp(name, wc->wf_data[i].name))
225 return wc->wf_data[i].value;
229 int wproto_process(WCLIENT wc, int timeout)
231 int toread, rs, level;
232 char combuf[COMBUF], *p,*t;
234 struct timeval to, *top;
238 gw_log (GW_LOG_DEBUG, mod, "process waiting for input.");
248 FD_SET(wc->linein, &input);
249 while ((rs = select(wc->linein + 1, &input, 0, 0, top)) < 0 &&
254 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "select");
259 gw_log (GW_LOG_STAT, mod,
260 "wproto_process returning 0 after %d second timeout.",
264 if (read(wc->linein, &toread, sizeof(toread)) < sizeof(toread))
266 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "wp_proc:len read failed");
269 toread -= sizeof(toread);
270 if (read(wc->linein, combuf, toread) < toread)
272 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "wp_proc: data read failed");
276 for (t = wc->wf_serverp; (*t = *p); t++, p++);
278 for (t = wc->wf_parms; (*t = *p); t++, p++);
280 p++; /* we don't deal with envvars yet */
281 decode_form(wc->wf_data, p);
282 if (wc->lineout < 0 && (wc->lineout = open(wc->wf_serverp, O_WRONLY))
285 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "open %s", wc->wf_serverp);
288 /* look in cache only if request carries no forms data. */
289 if (!*wc->wf_data[0].name && (level = wproto_findcache(wc,
292 wproto_dumpcache(wc, level);
300 WCLIENT wproto_init(void)
305 gw_log (GW_LOG_DEBUG, mod, "wproto_init");
306 close(1); /* release us from the wserver */
307 new = malloc(sizeof(*new));
308 sprintf(new->path, "%s/%s/clt%d", FIFOROOT, FIFODIR, getpid());
309 if (mkfifo(new->path, 0666 | S_IFIFO) < 0)
311 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "mkfifo(%s)", new->path);
314 gw_log (GW_LOG_DEBUG, mod, "Synchronizing with server.");
315 sprintf(path2, "%s/%s/srv%d", FIFOROOT, FIFODIR, getppid());
316 if ((new->lineout = open(path2, O_WRONLY)) < 0)
318 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "open %s", path2);
321 if (write(new->lineout, "OK", 2) < 2)
323 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "write");
326 gw_log (GW_LOG_DEBUG, mod, "Synchronized.");
327 if ((new->linein = open(new->path, O_RDONLY)) < 0)
329 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "open input handle %s", new->path);
332 /* we put a handle on this so we get a blocking read when no peer */
333 if (open(new->path, O_WRONLY | O_NDELAY) < 0)
335 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "open dummy %s", new->path);
339 new->cache_level = -1;
344 static void wproto_uncache(WCLIENT wc, int level)
346 for (;wc->cache_level >= level; wc->cache_level--)
347 unlink(wc->cache[wc->cache_level].path);
350 void wproto_terminate(WCLIENT wc)
354 wproto_uncache(wc, 0);
358 int wproto_cache(WCLIENT wc, int level)
362 if (level > wc->cache_level + 1)
364 gw_log (GW_LOG_FATAL, mod, "Illegal cache level increment.");
367 wproto_uncache(wc, level);
368 p = &wc->cache[++wc->cache_level];
369 sprintf(p->path, "%s/%s/csh%d.%d", FIFOROOT, FIFODIR, getpid(), level);
370 if ((wc->cache_fd = open(p->path, O_WRONLY|O_CREAT|O_TRUNC, 0600)) < 0)
372 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "open %s", p->path);
375 strcpy(p->name, wc->wf_parms);
379 static int wproto_findcache(WCLIENT wc, char *name)
383 for (i = 0; i <= wc->cache_level; i++)
384 if (!strcmp(wc->cache[i].name, name))
389 static int wproto_dumpcache(WCLIENT wc, int level)
393 gw_log (GW_LOG_STAT, mod, "Using Cache: %s", wc->cache[level].name);
394 if ((fd = open(wc->cache[level].path, O_RDONLY)) < 0)
396 gw_log (GW_LOG_FATAL, mod, "open (R) %s", wc->cache[level].path);
399 while ((rd = read(fd, wc->outbuffer, OUTBUFFER_CHUNK)) > 0)
400 if (write(wc->lineout, wc->outbuffer, rd) < rd)
402 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "write toline");
407 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "read");
410 wproto_uncache(wc, level + 1);