4 * This is the actual program called by the webserver. It maintains a
5 * persistent session to the Citadel server, handling HTTP WebCit requests as
6 * they arrive and presenting a user interface.
27 char wc_username[256];
28 char wc_password[256];
29 char wc_roomname[256];
30 int TransactionCount = 0;
35 struct webcontent *wlist = NULL;
36 struct webcontent *wlast = NULL;
38 struct urlcontent *urlstrings = NULL;
40 static const char *defaulthost = DEFAULT_HOST;
41 static const char *defaultport = DEFAULT_PORT;
44 void unescape_input(char *buf)
49 while ((isspace(buf[strlen(buf)-1]))&&(strlen(buf)>0))
50 buf[strlen(buf)-1] = 0;
52 for (a=0; a<strlen(buf); ++a) {
53 if (buf[a]=='+') buf[a]=' ';
58 sscanf(hex,"%02x",&b);
60 strcpy(&buf[a+1],&buf[a+3]);
67 void addurls(char *url) {
74 while (strlen(up)>0) {
76 /* locate the = sign */
79 for (a=255; a>=0; --a) if (buf[a]=='=') b=a;
83 u = (struct urlcontent *)malloc(sizeof(struct urlcontent));
86 strcpy(u->url_key, buf);
88 /* now chop that part off */
89 for (a=0; a<=b; ++a) ++up;
91 /* locate the & sign */
94 for (a=0; a<strlen(up); ++a) {
95 if (!strncmp(ptr,"&",1)) {
102 for (a=0; a<b; ++a) ++ptr;
105 u->url_data = malloc(strlen(up));
106 strcpy(u->url_data, up);
107 unescape_input(u->url_data);
114 void free_urls(void) {
115 struct urlcontent *u;
117 while (urlstrings != NULL) {
118 free(urlstrings->url_data);
119 u = urlstrings->next;
126 * Diagnostic function to display the contents of all variables
128 void dump_vars(void) {
129 struct urlcontent *u;
131 for (u = urlstrings; u != NULL; u = u->next) {
132 wprintf("%38s = %s\n", u->url_key, u->url_data);
136 char *bstr(char *key) {
137 struct urlcontent *u;
139 for (u = urlstrings; u != NULL; u = u->next) {
140 if (!strcasecmp(u->url_key, key)) return(u->url_data);
146 void wprintf(const char *format, ...) {
148 struct webcontent *wptr;
150 wptr = (struct webcontent *)malloc(sizeof(struct webcontent));
161 va_start(arg_ptr, format);
162 vsprintf(wptr->w_data, format, arg_ptr);
166 int wContentLength(void) {
167 struct webcontent *wptr;
170 for (wptr = wlist; wptr != NULL; wptr = wptr->next) {
171 len = len + strlen(wptr->w_data);
177 void wDumpContent(void) {
178 struct webcontent *wptr;
180 printf("Content-type: text/html\n");
181 printf("Content-length: %d\n", wContentLength());
184 while (wlist != NULL) {
185 fwrite(wlist->w_data, strlen(wlist->w_data), 1, stdout);
194 void escputs1(char *strbuf, int nbsp)
198 for (a=0; a<strlen(strbuf); ++a) {
199 if (strbuf[a]=='<') wprintf("<");
200 else if (strbuf[a]=='>') wprintf(">");
201 else if (strbuf[a]=='&') wprintf("&");
202 else if (strbuf[a]==34) wprintf(""");
203 else if (strbuf[a]==LB) wprintf("<");
204 else if (strbuf[a]==RB) wprintf(">");
205 else if (strbuf[a]==QU) wprintf("\"");
206 else if ((strbuf[a]==32)&&(nbsp==1)) {
210 wprintf("%c", strbuf[a]);
215 void escputs(char *strbuf)
222 char *urlesc(char *strbuf)
225 char *ec = " #&;`'|*?-~<>^()[]{}$\\";
226 static char outbuf[512];
230 for (a=0; a<strlen(strbuf); ++a) {
232 for (b=0; b<strlen(ec); ++b) {
233 if (strbuf[a]==ec[b]) c=1;
236 if (c==1) sprintf(&outbuf[b],"%%%02x",strbuf[a]);
237 else sprintf(&outbuf[b],"%c",strbuf[a]);
242 void urlescputs(char *strbuf)
244 wprintf("%s",urlesc(strbuf));
248 void getz(char *buf) {
249 if (fgets(buf, 256, stdin) == NULL) strcpy(buf, "");
251 while ((strlen(buf)>0)&&(!isprint(buf[strlen(buf)-1])))
252 buf[strlen(buf)-1] = 0;
257 * Output all that important stuff that the browser will want to see
259 void output_headers(void) {
261 static char *unset = "; expires=28-May-1971 18:10:00 GMT";
263 printf("Server: %s\n", SERVER);
264 printf("Connection: close\n");
265 printf("Set-cookie: wc_session=%d\n", wc_session);
267 if (strlen(wc_host)>0) printf("Set-cookie: wc_host=%s\n", wc_host);
268 else printf("Set-cookie: wc_host=%s\n", unset);
270 if (strlen(wc_port)>0) printf("Set-cookie: wc_port=%s\n", wc_port);
271 else printf("Set-cookie: wc_port=%s\n", unset);
273 if (strlen(wc_username)>0) printf("Set-cookie: wc_username=%s\n",
275 else printf("Set-cookie: wc_username=%s\n", unset);
277 if (strlen(wc_password)>0) printf("Set-cookie: wc_password=%s\n",
279 else printf("Set-cookie: wc_password=%s\n", unset);
281 if (strlen(wc_roomname)>0) printf("Set-cookie: wc_roomname=%s\n",
283 else printf("Set-cookie: wc_roomname=%s\n", unset);
286 void output_static(char *what) {
292 sprintf(buf, "static/%s", what);
293 fp = fopen(buf, "rb");
295 printf("HTTP/1.0 404 %s\n", strerror(errno));
297 printf("Content-Type: text/plain\n");
298 sprintf(buf, "%s: %s\n", what, strerror(errno));
299 printf("Content-length: %d\n", strlen(buf));
301 fwrite(buf, strlen(buf), 1, stdout);
304 printf("HTTP/1.0 200 OK\n");
307 if (!strncasecmp(&what[strlen(what)-4], ".gif", 4))
308 printf("Content-type: image/gif\n");
309 else if (!strncasecmp(&what[strlen(what)-5], ".html", 5))
310 printf("Content-type: text/html\n");
312 printf("Content-type: application/octet-stream\n");
314 fstat(fileno(fp), &statbuf);
315 bytes = statbuf.st_size;
316 printf("Content-length: %ld\n", (long)bytes);
319 putc(getc(fp), stdout);
326 void output_image() {
331 off_t accomplished = 0L;
334 serv_printf("OIMG %s|%s", bstr("name"), bstr("parm"));
337 bytes = extract_long(&buf[4], 0);
338 printf("HTTP/1.0 200 OK\n");
340 printf("Content-type: image/gif\n");
341 printf("Content-length: %ld\n", bytes);
344 while (bytes > (off_t)0) {
345 thisblock = (off_t)sizeof(xferbuf);
346 if (thisblock > bytes) thisblock = bytes;
347 serv_printf("READ %ld|%ld", accomplished, thisblock);
349 if (buf[0]=='6') thisblock = extract_long(&buf[4],0);
350 serv_read(xferbuf, (int)thisblock);
351 fwrite(xferbuf, thisblock, 1, stdout);
352 bytes = bytes - thisblock;
353 accomplished = accomplished + thisblock;
360 printf("HTTP/1.0 404 %s\n", strerror(errno));
362 printf("Content-Type: text/plain\n");
363 sprintf(buf, "Error retrieving image\n");
364 printf("Content-length: %d\n", strlen(buf));
366 fwrite(buf, strlen(buf), 1, stdout);
371 void extract_action(char *actbuf, char *cmdbuf) {
374 strcpy(actbuf, cmdbuf);
375 if (!strncasecmp(actbuf, "GET /", 5)) strcpy(actbuf, &actbuf[5]);
376 if (!strncasecmp(actbuf, "PUT /", 5)) strcpy(actbuf, &actbuf[5]);
377 if (!strncasecmp(actbuf, "POST /", 6)) strcpy(actbuf, &actbuf[6]);
379 for (i=0; i<strlen(actbuf); ++i) {
380 if (actbuf[i]==' ') { actbuf[i]=0; i=0; }
381 if (actbuf[i]=='/') { actbuf[i]=0; i=0; }
382 if (actbuf[i]=='?') { actbuf[i]=0; i=0; }
383 if (actbuf[i]=='&') { actbuf[i]=0; i=0; }
388 void session_loop(void) {
393 int ContentLength = 0;
397 /* We stuff these with the values coming from the client cookies,
398 * so we can use them to reconnect a timed out session if we have to.
402 char c_username[256];
403 char c_password[256];
404 char c_roomname[256];
406 strcpy(c_host, defaulthost);
407 strcpy(c_port, defaultport);
408 strcpy(c_username, "");
409 strcpy(c_password, "");
410 strcpy(c_roomname, "");
413 extract_action(action, cmd);
418 if (!strncasecmp(buf, "Cookie: wc_host=", 16))
419 strcpy(c_host, &buf[16]);
420 if (!strncasecmp(buf, "Cookie: wc_port=", 16))
421 strcpy(c_port, &buf[16]);
422 if (!strncasecmp(buf, "Cookie: wc_username=", 20))
423 strcpy(c_username, &buf[20]);
424 if (!strncasecmp(buf, "Cookie: wc_password=", 20))
425 strcpy(c_password, &buf[20]);
426 if (!strncasecmp(buf, "Cookie: wc_roomname=", 20))
427 strcpy(c_roomname, &buf[20]);
429 if (!strncasecmp(buf, "Content-length: ", 16)) {
430 ContentLength = atoi(&buf[16]);
433 } while(strlen(buf)>0);
437 if (ContentLength > 0) {
438 content = malloc(ContentLength+1);
439 fread(content, ContentLength, 1, stdin);
440 fp = fopen("content", "wb");
441 fwrite(content, ContentLength, 1, fp);
443 content[ContentLength] = 0;
451 * If we're not connected to a Citadel server, try to hook up the
452 * connection now. Preference is given to the host and port specified
453 * by browser cookies, if cookies have been supplied.
456 serv_sock = connectsock(c_host, c_port, "tcp");
458 serv_gets(buf); /* get the server welcome message */
459 strcpy(wc_host, c_host);
460 strcpy(wc_port, c_port);
466 * If we're not logged in, but we have username and password cookies
467 * supplied by the browser, try using them to log in.
469 if ((!logged_in)&&(strlen(c_username)>0)&&(strlen(c_password)>0)) {
470 serv_printf("USER %s", c_username);
473 serv_printf("PASS %s", c_password);
476 become_logged_in(c_username, c_password, buf);
482 * If we don't have a current room, but a cookie specifying the
483 * current room is supplied, make an effort to go there.
485 if ((strlen(wc_roomname)==0) && (strlen(c_roomname)>0) ) {
486 serv_printf("GOTO %s", c_roomname);
489 strcpy(wc_roomname, c_roomname);
493 /* If there are variables in the URL, we must grab them now */
494 for (a=0; a<strlen(cmd); ++a) if ((cmd[a]=='?')||(cmd[a]=='&')) {
495 for (b=a; b<strlen(cmd); ++b) if (isspace(cmd[b])) cmd[b]=0;
500 if (!strcasecmp(action, "static")) {
501 strcpy(buf, &cmd[12]);
502 for (a=0; a<strlen(buf); ++a) if (isspace(buf[a])) buf[a]=0;
506 else if (!strcasecmp(action, "image")) {
510 else if ((!logged_in)&&(!strcasecmp(action, "login"))) {
514 else if (!logged_in) {
518 /* Various commands... */
520 else if (!strcasecmp(action, "do_welcome")) {
524 else if (!strcasecmp(action, "display_main_menu")) {
528 else if (!strcasecmp(action, "advanced")) {
529 display_advanced_menu();
532 else if (!strcasecmp(action, "whobbs")) {
536 else if (!strcasecmp(action, "knrooms")) {
537 list_all_rooms_by_floor();
540 else if (!strcasecmp(action, "gotonext")) {
545 else if (!strcasecmp(action, "skip")) {
549 else if (!strcasecmp(action, "ungoto")) {
553 else if (!strcasecmp(action, "dotgoto")) {
558 else if (!strcasecmp(action, "termquit")) {
562 else if (!strcasecmp(action, "readnew")) {
566 else if (!strcasecmp(action, "readold")) {
570 else if (!strcasecmp(action, "readfwd")) {
574 /* When all else fails... */
576 printf("HTTP/1.0 200 OK\n");
579 wprintf("<HTML><HEAD><TITLE>WebCit</TITLE></HEAD><BODY BACKGROUND=\"/image&name=background\" TEXT=\"#000000\" LINK=\"#004400\">\n");
580 wprintf("TransactionCount is %d<BR>\n", TransactionCount);
581 wprintf("You're in session %d<HR>\n", wc_session);
582 wprintf("Command: <BR><PRE>\n");
584 wprintf("</PRE><HR>\n");
585 wprintf("Variables: <BR><PRE>\n");
587 wprintf("</PRE><HR>\n");
588 wprintf("</BODY></HTML>\n");
593 if (content != NULL) {
600 int main(int argc, char *argv[]) {
602 if (argc < 2 || argc > 4) {
604 "webcit: usage: webcit <session_id> [host [port]]\n");
608 wc_session = atoi(argv[1]);
611 defaulthost = argv[2];
613 defaultport = argv[3];
618 strcpy(wc_username, "");
619 strcpy(wc_password, "");
620 strcpy(wc_roomname, "");