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.
24 char wc_username[256];
25 char wc_password[256];
26 char wc_roomname[256];
27 int TransactionCount = 0;
32 struct webcontent *wlist = NULL;
33 struct webcontent *wlast = NULL;
35 struct urlcontent *urlstrings = NULL;
38 void unescape_input(buf)
43 while ((isspace(buf[strlen(buf)-1]))&&(strlen(buf)>0))
44 buf[strlen(buf)-1] = 0;
46 for (a=0; a<strlen(buf); ++a) {
47 if (buf[a]=='+') buf[a]=' ';
52 sscanf(hex,"%02x",&b);
54 strcpy(&buf[a+1],&buf[a+3]);
61 void addurls(char *url) {
68 while (strlen(up)>0) {
70 /* locate the = sign */
73 for (a=255; a>=0; --a) if (buf[a]=='=') b=a;
77 u = (struct urlcontent *)malloc(sizeof(struct urlcontent));
80 strcpy(u->url_key, buf);
82 /* now chop that part off */
83 for (a=0; a<=b; ++a) ++up;
85 /* locate the & sign */
88 for (a=0; a<strlen(up); ++a) {
89 if (!strncmp(ptr,"&",1)) {
96 for (a=0; a<b; ++a) ++ptr;
99 u->url_data = malloc(strlen(up));
100 strcpy(u->url_data, up);
101 unescape_input(u->url_data);
110 struct urlcontent *u;
112 while (urlstrings != NULL) {
113 free(urlstrings->url_data);
114 u = urlstrings->next;
120 char *bstr(char *key) {
121 struct urlcontent *u;
123 for (u = urlstrings; u != NULL; u = u->next) {
124 if (!strcasecmp(u->url_key, key)) return(u->url_data);
130 void wprintf(const char *format, ...) {
132 struct webcontent *wptr;
134 wptr = (struct webcontent *)malloc(sizeof(struct webcontent));
145 va_start(arg_ptr, format);
146 vsprintf(wptr->w_data, format, arg_ptr);
151 int wContentLength() {
152 struct webcontent *wptr;
155 for (wptr = wlist; wptr != NULL; wptr = wptr->next) {
156 len = len + strlen(wptr->w_data);
162 void wDumpContent() {
163 struct webcontent *wptr;
165 printf("Content-type: text/html\n");
166 printf("Content-length: %d\n", wContentLength());
169 while (wlist != NULL) {
170 fwrite(wlist->w_data, strlen(wlist->w_data), 1, stdout);
179 void escputs1(strbuf,nbsp)
184 for (a=0; a<strlen(strbuf); ++a) {
185 if (strbuf[a]=='<') wprintf("<");
186 else if (strbuf[a]=='>') wprintf(">");
187 else if (strbuf[a]=='&') wprintf("&");
188 else if (strbuf[a]==34) wprintf(""");
189 else if (strbuf[a]==LB) wprintf("<");
190 else if (strbuf[a]==RB) wprintf(">");
191 else if (strbuf[a]==QU) wprintf("\"");
192 else if ((strbuf[a]==32)&&(nbsp==1)) {
196 wprintf("%c", strbuf[a]);
211 char *ec = " #&;`'|*?-~<>^()[]{}$\\";
212 static char outbuf[512];
216 for (a=0; a<strlen(strbuf); ++a) {
218 for (b=0; b<strlen(ec); ++b) {
219 if (strbuf[a]==ec[b]) c=1;
222 if (c==1) sprintf(&outbuf[b],"%%%02x",strbuf[a]);
223 else sprintf(&outbuf[b],"%c",strbuf[a]);
228 void urlescputs(strbuf)
230 wprintf("%s",urlesc(strbuf));
235 * Look for URL's embedded in a buffer and make them linkable. We use a
236 * target window in order to keep the BBS session in its own window.
251 for (pos=0; pos<strlen(buf); ++pos) {
252 if (!strncasecmp(&buf[pos],"http://",7)) start = pos;
253 if (!strncasecmp(&buf[pos],"ftp://",6)) start = pos;
258 if ((start>0)&&(buf[start-1]=='<')) ench = '>';
259 if ((start>0)&&(buf[start-1]=='[')) ench = ']';
260 if ((start>0)&&(buf[start-1]=='(')) ench = ')';
261 if ((start>0)&&(buf[start-1]=='{')) ench = '}';
263 for (pos=strlen(buf); pos>start; --pos) {
264 if ((buf[pos]==' ')||(buf[pos]==ench)) end = pos;
267 strncpy(urlbuf,&buf[start],end-start);
268 urlbuf[end-start] = 0;
271 strncpy(outbuf,buf,start);
272 sprintf(&outbuf[start],"%cA HREF=%c%s%c TARGET=%c%s%c%c%s%c/A%c",
273 LB,QU,urlbuf,QU,QU,TARGET,QU,RB,urlbuf,LB,RB);
274 strcat(outbuf,&buf[end]);
281 void getz(char *buf) {
282 if (fgets(buf, 256, stdin) == NULL) strcpy(buf, "");
284 while ((strlen(buf)>0)&&(!isprint(buf[strlen(buf)-1])))
285 buf[strlen(buf)-1] = 0;
290 * Output all that important stuff that the browser will want to see
292 void output_headers() {
293 printf("Server: %s\n", SERVER);
294 printf("Connection: close\n");
295 printf("Set-cookie: wc_session=%d\n", wc_session);
296 if (strlen(wc_host)>0) printf("Set-cookie: wc_host=%s\n", wc_host);
297 if (strlen(wc_port)>0) printf("Set-cookie: wc_port=%s\n", wc_port);
298 if (strlen(wc_username)>0) printf("Set-cookie: wc_username=%s\n",
300 if (strlen(wc_password)>0) printf("Set-cookie: wc_password=%s\n",
302 if (strlen(wc_roomname)>0) printf("Set-cookie: wc_roomname=%s\n",
306 void output_static(char *what) {
312 sprintf(buf, "static/%s", what);
313 fp = fopen(buf, "rb");
315 printf("HTTP/1.0 404 %s\n", strerror(errno));
317 printf("Content-Type: text/plain\n");
318 sprintf(buf, "%s: %s\n", what, strerror(errno));
319 printf("Content-length: %d\n", strlen(buf));
321 fwrite(buf, strlen(buf), 1, stdout);
324 printf("HTTP/1.0 200 OK\n");
327 if (!strncasecmp(&what[strlen(what)-4], ".gif", 4))
328 printf("Content-type: image/gif\n");
329 else if (!strncasecmp(&what[strlen(what)-5], ".html", 5))
330 printf("Content-type: text/html\n");
332 printf("Content-type: application/octet-stream\n");
334 fstat(fileno(fp), &statbuf);
335 bytes = statbuf.st_size;
336 printf("Content-length: %d\n", bytes);
339 putc(getc(fp), stdout);
345 static const char *defaulthost = DEFAULT_HOST;
346 static const char *defaultport = DEFAULT_PORT;
348 void session_loop() {
352 int ContentLength = 0;
355 /* We stuff these with the values coming from the client cookies,
356 * so we can use them to reconnect a timed out session if we have to.
360 char c_username[256];
361 char c_password[256];
362 char c_roomname[256];
364 strcpy(c_host, defaulthost);
365 strcpy(c_port, defaultport);
366 strcpy(c_username, "");
367 strcpy(c_password, "");
368 strcpy(c_roomname, "");
375 if (!strncasecmp(buf, "Cookie: wc_host=", 16))
376 strcpy(c_host, &buf[16]);
377 if (!strncasecmp(buf, "Cookie: wc_port=", 16))
378 strcpy(c_port, &buf[16]);
379 if (!strncasecmp(buf, "Cookie: wc_username=", 20))
380 strcpy(c_username, &buf[20]);
381 if (!strncasecmp(buf, "Cookie: wc_password=", 20))
382 strcpy(c_password, &buf[20]);
383 if (!strncasecmp(buf, "Cookie: wc_roomname=", 20))
384 strcpy(c_roomname, &buf[20]);
386 if (!strncasecmp(buf, "Content-length: ", 16)) {
387 ContentLength = atoi(&buf[16]);
390 } while(strlen(buf)>0);
394 if (ContentLength > 0) {
395 content = malloc(ContentLength+1);
396 fread(content, ContentLength, 1, stdin);
397 content[ContentLength] = 0;
405 * If we're not connected to a Citadel server, try to hook up the
406 * connection now. Preference is given to the host and port specified
407 * by browser cookies, if cookies have been supplied.
410 serv_sock = connectsock(c_host, c_port, "tcp");
412 serv_gets(buf); /* get the server welcome message */
413 strcpy(wc_host, c_host);
414 strcpy(wc_port, c_port);
420 * If we're not logged in, but we have username and password cookies
421 * supplied by the browser, try using them to log in.
423 if ((!logged_in)&&(strlen(c_username)>0)&&(strlen(c_password)>0)) {
424 serv_printf("USER %s", c_username);
427 serv_printf("PASS %s", c_password);
430 become_logged_in(c_username, c_password, buf);
436 * If we don't have a current room, but a cookie specifying the
437 * current room is supplied, make an effort to go there.
439 if ((strlen(wc_roomname)==0) && (strlen(c_roomname)>0) ) {
440 serv_printf("GOTO %s", c_roomname);
443 strcpy(wc_roomname, c_roomname);
447 /* If there are variables in the URL, we must grab them now */
448 for (a=0; a<strlen(cmd); ++a) if (cmd[a]=='?') {
449 for (b=a; b<strlen(cmd); ++b) if (isspace(cmd[b])) cmd[b]=0;
454 if (!strncasecmp(cmd, "GET /static/", 12)) {
455 strcpy(buf, &cmd[12]);
456 for (a=0; a<strlen(buf); ++a) if (isspace(buf[a])) buf[a]=0;
461 else if ((!logged_in)&&(!strncasecmp(cmd, "POST /login", 11))) {
465 else if (!logged_in) {
469 /* Various commands... */
471 else if (!strncasecmp(cmd, "GET /do_welcome", 15)) {
475 else if (!strncasecmp(cmd, "GET /display_main_menu", 22)) {
479 else if (!strncasecmp(cmd, "GET /advanced", 13)) {
480 display_advanced_menu();
483 else if (!strncasecmp(cmd, "GET /whobbs", 11)) {
487 else if (!strncasecmp(cmd, "GET /knrooms", 12)) {
488 list_all_rooms_by_floor();
491 else if (!strncasecmp(cmd, "GET /gotonext", 13)) {
496 else if (!strncasecmp(cmd, "GET /skip", 9)) {
500 else if (!strncasecmp(cmd, "GET /ungoto", 11)) {
504 else if (!strncasecmp(cmd, "GET /dotgoto", 12)) {
509 else if (!strncasecmp(cmd, "GET /termquit", 13)) {
513 /* When all else fails... */
515 printf("HTTP/1.0 200 OK\n");
518 wprintf("<HTML><HEAD><TITLE>WebCit</TITLE></HEAD><BODY>\n");
519 wprintf("TransactionCount is %d<HR>\n", TransactionCount);
520 wprintf("You're in session %d<BR>\n", wc_session);
521 wprintf("</BODY></HTML>\n");
526 if (content != NULL) {
533 int main(int argc, char *argv[]) {
535 if (argc < 2 || argc > 4) {
537 "webcit: usage: webcit <session_id> [host [port]]\n");
541 wc_session = atoi(argv[1]);
544 defaulthost = argv[2];
546 defaultport = argv[3];
551 strcpy(wc_username, "");
552 strcpy(wc_password, "");
553 strcpy(wc_roomname, "");