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;
41 void unescape_input(char *buf)
46 while ((isspace(buf[strlen(buf)-1]))&&(strlen(buf)>0))
47 buf[strlen(buf)-1] = 0;
49 for (a=0; a<strlen(buf); ++a) {
50 if (buf[a]=='+') buf[a]=' ';
55 sscanf(hex,"%02x",&b);
57 strcpy(&buf[a+1],&buf[a+3]);
64 void addurls(char *url) {
71 while (strlen(up)>0) {
73 /* locate the = sign */
76 for (a=255; a>=0; --a) if (buf[a]=='=') b=a;
80 u = (struct urlcontent *)malloc(sizeof(struct urlcontent));
83 strcpy(u->url_key, buf);
85 /* now chop that part off */
86 for (a=0; a<=b; ++a) ++up;
88 /* locate the & sign */
91 for (a=0; a<strlen(up); ++a) {
92 if (!strncmp(ptr,"&",1)) {
99 for (a=0; a<b; ++a) ++ptr;
102 u->url_data = malloc(strlen(up));
103 strcpy(u->url_data, up);
104 unescape_input(u->url_data);
111 void free_urls(void) {
112 struct urlcontent *u;
114 while (urlstrings != NULL) {
115 free(urlstrings->url_data);
116 u = urlstrings->next;
123 * Diagnostic function to display the contents of all variables
126 struct urlcontent *u;
128 for (u = urlstrings; u != NULL; u = u->next) {
129 fprintf(stderr, "%38s = %s\n", u->url_key, u->url_data);
133 char *bstr(char *key) {
134 struct urlcontent *u;
136 for (u = urlstrings; u != NULL; u = u->next) {
137 if (!strcasecmp(u->url_key, key)) return(u->url_data);
143 void wprintf(const char *format, ...) {
145 struct webcontent *wptr;
147 wptr = (struct webcontent *)malloc(sizeof(struct webcontent));
158 va_start(arg_ptr, format);
159 vsprintf(wptr->w_data, format, arg_ptr);
164 int wContentLength(void) {
165 struct webcontent *wptr;
168 for (wptr = wlist; wptr != NULL; wptr = wptr->next) {
169 len = len + strlen(wptr->w_data);
175 void wDumpContent(void) {
176 struct webcontent *wptr;
178 printf("Content-type: text/html\n");
179 printf("Content-length: %d\n", wContentLength());
182 while (wlist != NULL) {
183 fwrite(wlist->w_data, strlen(wlist->w_data), 1, stdout);
192 void escputs1(char *strbuf, int nbsp)
196 for (a=0; a<strlen(strbuf); ++a) {
197 if (strbuf[a]=='<') wprintf("<");
198 else if (strbuf[a]=='>') wprintf(">");
199 else if (strbuf[a]=='&') wprintf("&");
200 else if (strbuf[a]==34) wprintf(""");
201 else if (strbuf[a]==LB) wprintf("<");
202 else if (strbuf[a]==RB) wprintf(">");
203 else if (strbuf[a]==QU) wprintf("\"");
204 else if ((strbuf[a]==32)&&(nbsp==1)) {
208 wprintf("%c", strbuf[a]);
213 void escputs(char *strbuf)
220 char *urlesc(char *strbuf)
223 char *ec = " #&;`'|*?-~<>^()[]{}$\\";
224 static char outbuf[512];
228 for (a=0; a<strlen(strbuf); ++a) {
230 for (b=0; b<strlen(ec); ++b) {
231 if (strbuf[a]==ec[b]) c=1;
234 if (c==1) sprintf(&outbuf[b],"%%%02x",strbuf[a]);
235 else sprintf(&outbuf[b],"%c",strbuf[a]);
240 void urlescputs(char *strbuf)
242 wprintf("%s",urlesc(strbuf));
247 * Look for URL's embedded in a buffer and make them linkable. We use a
248 * target window in order to keep the BBS session in its own window.
263 for (pos=0; pos<strlen(buf); ++pos) {
264 if (!strncasecmp(&buf[pos],"http://",7)) start = pos;
265 if (!strncasecmp(&buf[pos],"ftp://",6)) start = pos;
270 if ((start>0)&&(buf[start-1]=='<')) ench = '>';
271 if ((start>0)&&(buf[start-1]=='[')) ench = ']';
272 if ((start>0)&&(buf[start-1]=='(')) ench = ')';
273 if ((start>0)&&(buf[start-1]=='{')) ench = '}';
275 for (pos=strlen(buf); pos>start; --pos) {
276 if ((buf[pos]==' ')||(buf[pos]==ench)) end = pos;
279 strncpy(urlbuf,&buf[start],end-start);
280 urlbuf[end-start] = 0;
283 strncpy(outbuf,buf,start);
284 sprintf(&outbuf[start],"%cA HREF=%c%s%c TARGET=%c%s%c%c%s%c/A%c",
285 LB,QU,urlbuf,QU,QU,TARGET,QU,RB,urlbuf,LB,RB);
286 strcat(outbuf,&buf[end]);
293 void getz(char *buf) {
294 if (fgets(buf, 256, stdin) == NULL) strcpy(buf, "");
296 while ((strlen(buf)>0)&&(!isprint(buf[strlen(buf)-1])))
297 buf[strlen(buf)-1] = 0;
302 * Output all that important stuff that the browser will want to see
304 void output_headers(void) {
305 printf("Server: %s\n", SERVER);
306 printf("Connection: close\n");
307 printf("Set-cookie: wc_session=%d\n", wc_session);
308 if (strlen(wc_host)>0) printf("Set-cookie: wc_host=%s\n", wc_host);
309 if (strlen(wc_port)>0) printf("Set-cookie: wc_port=%s\n", wc_port);
310 if (strlen(wc_username)>0) printf("Set-cookie: wc_username=%s\n",
312 if (strlen(wc_password)>0) printf("Set-cookie: wc_password=%s\n",
314 if (strlen(wc_roomname)>0) printf("Set-cookie: wc_roomname=%s\n",
318 void output_static(char *what) {
324 sprintf(buf, "static/%s", what);
325 fp = fopen(buf, "rb");
327 printf("HTTP/1.0 404 %s\n", strerror(errno));
329 printf("Content-Type: text/plain\n");
330 sprintf(buf, "%s: %s\n", what, strerror(errno));
331 printf("Content-length: %d\n", strlen(buf));
333 fwrite(buf, strlen(buf), 1, stdout);
336 printf("HTTP/1.0 200 OK\n");
339 if (!strncasecmp(&what[strlen(what)-4], ".gif", 4))
340 printf("Content-type: image/gif\n");
341 else if (!strncasecmp(&what[strlen(what)-5], ".html", 5))
342 printf("Content-type: text/html\n");
344 printf("Content-type: application/octet-stream\n");
346 fstat(fileno(fp), &statbuf);
347 bytes = statbuf.st_size;
348 printf("Content-length: %ld\n", (long)bytes);
351 putc(getc(fp), stdout);
357 static const char *defaulthost = DEFAULT_HOST;
358 static const char *defaultport = DEFAULT_PORT;
360 void session_loop(void) {
364 int ContentLength = 0;
367 /* We stuff these with the values coming from the client cookies,
368 * so we can use them to reconnect a timed out session if we have to.
372 char c_username[256];
373 char c_password[256];
374 char c_roomname[256];
376 strcpy(c_host, defaulthost);
377 strcpy(c_port, defaultport);
378 strcpy(c_username, "");
379 strcpy(c_password, "");
380 strcpy(c_roomname, "");
387 if (!strncasecmp(buf, "Cookie: wc_host=", 16))
388 strcpy(c_host, &buf[16]);
389 if (!strncasecmp(buf, "Cookie: wc_port=", 16))
390 strcpy(c_port, &buf[16]);
391 if (!strncasecmp(buf, "Cookie: wc_username=", 20))
392 strcpy(c_username, &buf[20]);
393 if (!strncasecmp(buf, "Cookie: wc_password=", 20))
394 strcpy(c_password, &buf[20]);
395 if (!strncasecmp(buf, "Cookie: wc_roomname=", 20))
396 strcpy(c_roomname, &buf[20]);
398 if (!strncasecmp(buf, "Content-length: ", 16)) {
399 ContentLength = atoi(&buf[16]);
402 } while(strlen(buf)>0);
406 if (ContentLength > 0) {
407 content = malloc(ContentLength+1);
408 fread(content, ContentLength, 1, stdin);
409 content[ContentLength] = 0;
417 * If we're not connected to a Citadel server, try to hook up the
418 * connection now. Preference is given to the host and port specified
419 * by browser cookies, if cookies have been supplied.
422 serv_sock = connectsock(c_host, c_port, "tcp");
424 serv_gets(buf); /* get the server welcome message */
425 strcpy(wc_host, c_host);
426 strcpy(wc_port, c_port);
432 * If we're not logged in, but we have username and password cookies
433 * supplied by the browser, try using them to log in.
435 if ((!logged_in)&&(strlen(c_username)>0)&&(strlen(c_password)>0)) {
436 serv_printf("USER %s", c_username);
439 serv_printf("PASS %s", c_password);
442 become_logged_in(c_username, c_password, buf);
448 * If we don't have a current room, but a cookie specifying the
449 * current room is supplied, make an effort to go there.
451 if ((strlen(wc_roomname)==0) && (strlen(c_roomname)>0) ) {
452 serv_printf("GOTO %s", c_roomname);
455 strcpy(wc_roomname, c_roomname);
459 /* If there are variables in the URL, we must grab them now */
460 for (a=0; a<strlen(cmd); ++a) if ((cmd[a]=='?')||(cmd[a]=='&')) {
461 for (b=a; b<strlen(cmd); ++b) if (isspace(cmd[b])) cmd[b]=0;
466 /* Verbose but informative; uncomment if you want to trace variables */
469 if (!strncasecmp(cmd, "GET /static/", 12)) {
470 strcpy(buf, &cmd[12]);
471 for (a=0; a<strlen(buf); ++a) if (isspace(buf[a])) buf[a]=0;
475 else if ((!logged_in)&&(!strncasecmp(cmd, "POST /login", 11))) {
479 else if (!logged_in) {
483 /* Various commands... */
485 else if (!strncasecmp(cmd, "GET /do_welcome", 15)) {
489 else if (!strncasecmp(cmd, "GET /display_main_menu", 22)) {
493 else if (!strncasecmp(cmd, "GET /advanced", 13)) {
494 display_advanced_menu();
497 else if (!strncasecmp(cmd, "GET /whobbs", 11)) {
501 else if (!strncasecmp(cmd, "GET /knrooms", 12)) {
502 list_all_rooms_by_floor();
505 else if (!strncasecmp(cmd, "GET /gotonext", 13)) {
510 else if (!strncasecmp(cmd, "GET /skip", 9)) {
514 else if (!strncasecmp(cmd, "GET /ungoto", 11)) {
518 else if (!strncasecmp(cmd, "GET /dotgoto", 12)) {
523 else if (!strncasecmp(cmd, "GET /termquit", 13)) {
527 /* When all else fails... */
529 printf("HTTP/1.0 200 OK\n");
532 wprintf("<HTML><HEAD><TITLE>WebCit</TITLE></HEAD><BODY>\n");
533 wprintf("TransactionCount is %d<HR>\n", TransactionCount);
534 wprintf("You're in session %d<BR>\n", wc_session);
535 wprintf("</BODY></HTML>\n");
540 if (content != NULL) {
547 int main(int argc, char *argv[]) {
549 if (argc < 2 || argc > 4) {
551 "webcit: usage: webcit <session_id> [host [port]]\n");
555 wc_session = atoi(argv[1]);
558 defaulthost = argv[2];
560 defaultport = argv[3];
565 strcpy(wc_username, "");
566 strcpy(wc_password, "");
567 strcpy(wc_roomname, "");