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.
25 char wc_username[256];
26 char wc_password[256];
27 char wc_roomname[256];
28 int TransactionCount = 0;
33 struct webcontent *wlist = NULL;
34 struct webcontent *wlast = NULL;
36 struct urlcontent *urlstrings = NULL;
39 void unescape_input(char *buf)
44 while ((isspace(buf[strlen(buf)-1]))&&(strlen(buf)>0))
45 buf[strlen(buf)-1] = 0;
47 for (a=0; a<strlen(buf); ++a) {
48 if (buf[a]=='+') buf[a]=' ';
53 sscanf(hex,"%02x",&b);
55 strcpy(&buf[a+1],&buf[a+3]);
62 void addurls(char *url) {
69 while (strlen(up)>0) {
71 /* locate the = sign */
74 for (a=255; a>=0; --a) if (buf[a]=='=') b=a;
78 u = (struct urlcontent *)malloc(sizeof(struct urlcontent));
81 strcpy(u->url_key, buf);
83 /* now chop that part off */
84 for (a=0; a<=b; ++a) ++up;
86 /* locate the & sign */
89 for (a=0; a<strlen(up); ++a) {
90 if (!strncmp(ptr,"&",1)) {
97 for (a=0; a<b; ++a) ++ptr;
100 u->url_data = malloc(strlen(up));
101 strcpy(u->url_data, up);
102 unescape_input(u->url_data);
109 void free_urls(void) {
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(void) {
152 struct webcontent *wptr;
155 for (wptr = wlist; wptr != NULL; wptr = wptr->next) {
156 len = len + strlen(wptr->w_data);
162 void wDumpContent(void) {
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(char *strbuf, int nbsp)
183 for (a=0; a<strlen(strbuf); ++a) {
184 if (strbuf[a]=='<') wprintf("<");
185 else if (strbuf[a]=='>') wprintf(">");
186 else if (strbuf[a]=='&') wprintf("&");
187 else if (strbuf[a]==34) wprintf(""");
188 else if (strbuf[a]==LB) wprintf("<");
189 else if (strbuf[a]==RB) wprintf(">");
190 else if (strbuf[a]==QU) wprintf("\"");
191 else if ((strbuf[a]==32)&&(nbsp==1)) {
195 wprintf("%c", strbuf[a]);
200 void escputs(char *strbuf)
207 char *urlesc(char *strbuf)
210 char *ec = " #&;`'|*?-~<>^()[]{}$\\";
211 static char outbuf[512];
215 for (a=0; a<strlen(strbuf); ++a) {
217 for (b=0; b<strlen(ec); ++b) {
218 if (strbuf[a]==ec[b]) c=1;
221 if (c==1) sprintf(&outbuf[b],"%%%02x",strbuf[a]);
222 else sprintf(&outbuf[b],"%c",strbuf[a]);
227 void urlescputs(char *strbuf)
229 wprintf("%s",urlesc(strbuf));
234 * Look for URL's embedded in a buffer and make them linkable. We use a
235 * target window in order to keep the BBS session in its own window.
250 for (pos=0; pos<strlen(buf); ++pos) {
251 if (!strncasecmp(&buf[pos],"http://",7)) start = pos;
252 if (!strncasecmp(&buf[pos],"ftp://",6)) start = pos;
257 if ((start>0)&&(buf[start-1]=='<')) ench = '>';
258 if ((start>0)&&(buf[start-1]=='[')) ench = ']';
259 if ((start>0)&&(buf[start-1]=='(')) ench = ')';
260 if ((start>0)&&(buf[start-1]=='{')) ench = '}';
262 for (pos=strlen(buf); pos>start; --pos) {
263 if ((buf[pos]==' ')||(buf[pos]==ench)) end = pos;
266 strncpy(urlbuf,&buf[start],end-start);
267 urlbuf[end-start] = 0;
270 strncpy(outbuf,buf,start);
271 sprintf(&outbuf[start],"%cA HREF=%c%s%c TARGET=%c%s%c%c%s%c/A%c",
272 LB,QU,urlbuf,QU,QU,TARGET,QU,RB,urlbuf,LB,RB);
273 strcat(outbuf,&buf[end]);
280 void getz(char *buf) {
281 if (fgets(buf, 256, stdin) == NULL) strcpy(buf, "");
283 while ((strlen(buf)>0)&&(!isprint(buf[strlen(buf)-1])))
284 buf[strlen(buf)-1] = 0;
289 * Output all that important stuff that the browser will want to see
291 void output_headers(void) {
292 printf("Server: %s\n", SERVER);
293 printf("Connection: close\n");
294 printf("Set-cookie: wc_session=%d\n", wc_session);
295 if (strlen(wc_host)>0) printf("Set-cookie: wc_host=%s\n", wc_host);
296 if (strlen(wc_port)>0) printf("Set-cookie: wc_port=%s\n", wc_port);
297 if (strlen(wc_username)>0) printf("Set-cookie: wc_username=%s\n",
299 if (strlen(wc_password)>0) printf("Set-cookie: wc_password=%s\n",
301 if (strlen(wc_roomname)>0) printf("Set-cookie: wc_roomname=%s\n",
305 void output_static(char *what) {
311 sprintf(buf, "static/%s", what);
312 fp = fopen(buf, "rb");
314 printf("HTTP/1.0 404 %s\n", strerror(errno));
316 printf("Content-Type: text/plain\n");
317 sprintf(buf, "%s: %s\n", what, strerror(errno));
318 printf("Content-length: %d\n", strlen(buf));
320 fwrite(buf, strlen(buf), 1, stdout);
323 printf("HTTP/1.0 200 OK\n");
326 if (!strncasecmp(&what[strlen(what)-4], ".gif", 4))
327 printf("Content-type: image/gif\n");
328 else if (!strncasecmp(&what[strlen(what)-5], ".html", 5))
329 printf("Content-type: text/html\n");
331 printf("Content-type: application/octet-stream\n");
333 fstat(fileno(fp), &statbuf);
334 bytes = statbuf.st_size;
335 printf("Content-length: %ld\n", (long)bytes);
338 putc(getc(fp), stdout);
344 static const char *defaulthost = DEFAULT_HOST;
345 static const char *defaultport = DEFAULT_PORT;
347 void session_loop(void) {
351 int ContentLength = 0;
354 /* We stuff these with the values coming from the client cookies,
355 * so we can use them to reconnect a timed out session if we have to.
359 char c_username[256];
360 char c_password[256];
361 char c_roomname[256];
363 strcpy(c_host, defaulthost);
364 strcpy(c_port, defaultport);
365 strcpy(c_username, "");
366 strcpy(c_password, "");
367 strcpy(c_roomname, "");
374 if (!strncasecmp(buf, "Cookie: wc_host=", 16))
375 strcpy(c_host, &buf[16]);
376 if (!strncasecmp(buf, "Cookie: wc_port=", 16))
377 strcpy(c_port, &buf[16]);
378 if (!strncasecmp(buf, "Cookie: wc_username=", 20))
379 strcpy(c_username, &buf[20]);
380 if (!strncasecmp(buf, "Cookie: wc_password=", 20))
381 strcpy(c_password, &buf[20]);
382 if (!strncasecmp(buf, "Cookie: wc_roomname=", 20))
383 strcpy(c_roomname, &buf[20]);
385 if (!strncasecmp(buf, "Content-length: ", 16)) {
386 ContentLength = atoi(&buf[16]);
389 } while(strlen(buf)>0);
393 if (ContentLength > 0) {
394 content = malloc(ContentLength+1);
395 fread(content, ContentLength, 1, stdin);
396 content[ContentLength] = 0;
404 * If we're not connected to a Citadel server, try to hook up the
405 * connection now. Preference is given to the host and port specified
406 * by browser cookies, if cookies have been supplied.
409 serv_sock = connectsock(c_host, c_port, "tcp");
411 serv_gets(buf); /* get the server welcome message */
412 strcpy(wc_host, c_host);
413 strcpy(wc_port, c_port);
419 * If we're not logged in, but we have username and password cookies
420 * supplied by the browser, try using them to log in.
422 if ((!logged_in)&&(strlen(c_username)>0)&&(strlen(c_password)>0)) {
423 serv_printf("USER %s", c_username);
426 serv_printf("PASS %s", c_password);
429 become_logged_in(c_username, c_password, buf);
435 * If we don't have a current room, but a cookie specifying the
436 * current room is supplied, make an effort to go there.
438 if ((strlen(wc_roomname)==0) && (strlen(c_roomname)>0) ) {
439 serv_printf("GOTO %s", c_roomname);
442 strcpy(wc_roomname, c_roomname);
446 /* If there are variables in the URL, we must grab them now */
447 for (a=0; a<strlen(cmd); ++a) if (cmd[a]=='?') {
448 for (b=a; b<strlen(cmd); ++b) if (isspace(cmd[b])) cmd[b]=0;
453 if (!strncasecmp(cmd, "GET /static/", 12)) {
454 strcpy(buf, &cmd[12]);
455 for (a=0; a<strlen(buf); ++a) if (isspace(buf[a])) buf[a]=0;
460 else if ((!logged_in)&&(!strncasecmp(cmd, "POST /login", 11))) {
464 else if (!logged_in) {
468 /* Various commands... */
470 else if (!strncasecmp(cmd, "GET /do_welcome", 15)) {
474 else if (!strncasecmp(cmd, "GET /display_main_menu", 22)) {
478 else if (!strncasecmp(cmd, "GET /advanced", 13)) {
479 display_advanced_menu();
482 else if (!strncasecmp(cmd, "GET /whobbs", 11)) {
486 else if (!strncasecmp(cmd, "GET /knrooms", 12)) {
487 list_all_rooms_by_floor();
490 else if (!strncasecmp(cmd, "GET /gotonext", 13)) {
495 else if (!strncasecmp(cmd, "GET /skip", 9)) {
499 else if (!strncasecmp(cmd, "GET /ungoto", 11)) {
503 else if (!strncasecmp(cmd, "GET /dotgoto", 12)) {
508 else if (!strncasecmp(cmd, "GET /termquit", 13)) {
512 /* When all else fails... */
514 printf("HTTP/1.0 200 OK\n");
517 wprintf("<HTML><HEAD><TITLE>WebCit</TITLE></HEAD><BODY>\n");
518 wprintf("TransactionCount is %d<HR>\n", TransactionCount);
519 wprintf("You're in session %d<BR>\n", wc_session);
520 wprintf("</BODY></HTML>\n");
525 if (content != NULL) {
532 int main(int argc, char *argv[]) {
534 if (argc < 2 || argc > 4) {
536 "webcit: usage: webcit <session_id> [host [port]]\n");
540 wc_session = atoi(argv[1]);
543 defaulthost = argv[2];
545 defaultport = argv[3];
550 strcpy(wc_username, "");
551 strcpy(wc_password, "");
552 strcpy(wc_roomname, "");