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;
122 char *bstr(char *key) {
123 struct urlcontent *u;
125 for (u = urlstrings; u != NULL; u = u->next) {
126 if (!strcasecmp(u->url_key, key)) return(u->url_data);
132 void wprintf(const char *format, ...) {
134 struct webcontent *wptr;
136 wptr = (struct webcontent *)malloc(sizeof(struct webcontent));
147 va_start(arg_ptr, format);
148 vsprintf(wptr->w_data, format, arg_ptr);
153 int wContentLength(void) {
154 struct webcontent *wptr;
157 for (wptr = wlist; wptr != NULL; wptr = wptr->next) {
158 len = len + strlen(wptr->w_data);
164 void wDumpContent(void) {
165 struct webcontent *wptr;
167 printf("Content-type: text/html\n");
168 printf("Content-length: %d\n", wContentLength());
171 while (wlist != NULL) {
172 fwrite(wlist->w_data, strlen(wlist->w_data), 1, stdout);
181 void escputs1(char *strbuf, int nbsp)
185 for (a=0; a<strlen(strbuf); ++a) {
186 if (strbuf[a]=='<') wprintf("<");
187 else if (strbuf[a]=='>') wprintf(">");
188 else if (strbuf[a]=='&') wprintf("&");
189 else if (strbuf[a]==34) wprintf(""");
190 else if (strbuf[a]==LB) wprintf("<");
191 else if (strbuf[a]==RB) wprintf(">");
192 else if (strbuf[a]==QU) wprintf("\"");
193 else if ((strbuf[a]==32)&&(nbsp==1)) {
197 wprintf("%c", strbuf[a]);
202 void escputs(char *strbuf)
209 char *urlesc(char *strbuf)
212 char *ec = " #&;`'|*?-~<>^()[]{}$\\";
213 static char outbuf[512];
217 for (a=0; a<strlen(strbuf); ++a) {
219 for (b=0; b<strlen(ec); ++b) {
220 if (strbuf[a]==ec[b]) c=1;
223 if (c==1) sprintf(&outbuf[b],"%%%02x",strbuf[a]);
224 else sprintf(&outbuf[b],"%c",strbuf[a]);
229 void urlescputs(char *strbuf)
231 wprintf("%s",urlesc(strbuf));
236 * Look for URL's embedded in a buffer and make them linkable. We use a
237 * target window in order to keep the BBS session in its own window.
252 for (pos=0; pos<strlen(buf); ++pos) {
253 if (!strncasecmp(&buf[pos],"http://",7)) start = pos;
254 if (!strncasecmp(&buf[pos],"ftp://",6)) start = pos;
259 if ((start>0)&&(buf[start-1]=='<')) ench = '>';
260 if ((start>0)&&(buf[start-1]=='[')) ench = ']';
261 if ((start>0)&&(buf[start-1]=='(')) ench = ')';
262 if ((start>0)&&(buf[start-1]=='{')) ench = '}';
264 for (pos=strlen(buf); pos>start; --pos) {
265 if ((buf[pos]==' ')||(buf[pos]==ench)) end = pos;
268 strncpy(urlbuf,&buf[start],end-start);
269 urlbuf[end-start] = 0;
272 strncpy(outbuf,buf,start);
273 sprintf(&outbuf[start],"%cA HREF=%c%s%c TARGET=%c%s%c%c%s%c/A%c",
274 LB,QU,urlbuf,QU,QU,TARGET,QU,RB,urlbuf,LB,RB);
275 strcat(outbuf,&buf[end]);
282 void getz(char *buf) {
283 if (fgets(buf, 256, stdin) == NULL) strcpy(buf, "");
285 while ((strlen(buf)>0)&&(!isprint(buf[strlen(buf)-1])))
286 buf[strlen(buf)-1] = 0;
291 * Output all that important stuff that the browser will want to see
293 void output_headers(void) {
294 printf("Server: %s\n", SERVER);
295 printf("Connection: close\n");
296 printf("Set-cookie: wc_session=%d\n", wc_session);
297 if (strlen(wc_host)>0) printf("Set-cookie: wc_host=%s\n", wc_host);
298 if (strlen(wc_port)>0) printf("Set-cookie: wc_port=%s\n", wc_port);
299 if (strlen(wc_username)>0) printf("Set-cookie: wc_username=%s\n",
301 if (strlen(wc_password)>0) printf("Set-cookie: wc_password=%s\n",
303 if (strlen(wc_roomname)>0) printf("Set-cookie: wc_roomname=%s\n",
307 void output_static(char *what) {
313 sprintf(buf, "static/%s", what);
314 fp = fopen(buf, "rb");
316 printf("HTTP/1.0 404 %s\n", strerror(errno));
318 printf("Content-Type: text/plain\n");
319 sprintf(buf, "%s: %s\n", what, strerror(errno));
320 printf("Content-length: %d\n", strlen(buf));
322 fwrite(buf, strlen(buf), 1, stdout);
325 printf("HTTP/1.0 200 OK\n");
328 if (!strncasecmp(&what[strlen(what)-4], ".gif", 4))
329 printf("Content-type: image/gif\n");
330 else if (!strncasecmp(&what[strlen(what)-5], ".html", 5))
331 printf("Content-type: text/html\n");
333 printf("Content-type: application/octet-stream\n");
335 fstat(fileno(fp), &statbuf);
336 bytes = statbuf.st_size;
337 printf("Content-length: %ld\n", (long)bytes);
340 putc(getc(fp), stdout);
346 static const char *defaulthost = DEFAULT_HOST;
347 static const char *defaultport = DEFAULT_PORT;
349 void session_loop(void) {
353 int ContentLength = 0;
356 /* We stuff these with the values coming from the client cookies,
357 * so we can use them to reconnect a timed out session if we have to.
361 char c_username[256];
362 char c_password[256];
363 char c_roomname[256];
365 strcpy(c_host, defaulthost);
366 strcpy(c_port, defaultport);
367 strcpy(c_username, "");
368 strcpy(c_password, "");
369 strcpy(c_roomname, "");
376 if (!strncasecmp(buf, "Cookie: wc_host=", 16))
377 strcpy(c_host, &buf[16]);
378 if (!strncasecmp(buf, "Cookie: wc_port=", 16))
379 strcpy(c_port, &buf[16]);
380 if (!strncasecmp(buf, "Cookie: wc_username=", 20))
381 strcpy(c_username, &buf[20]);
382 if (!strncasecmp(buf, "Cookie: wc_password=", 20))
383 strcpy(c_password, &buf[20]);
384 if (!strncasecmp(buf, "Cookie: wc_roomname=", 20))
385 strcpy(c_roomname, &buf[20]);
387 if (!strncasecmp(buf, "Content-length: ", 16)) {
388 ContentLength = atoi(&buf[16]);
391 } while(strlen(buf)>0);
395 if (ContentLength > 0) {
396 content = malloc(ContentLength+1);
397 fread(content, ContentLength, 1, stdin);
398 content[ContentLength] = 0;
406 * If we're not connected to a Citadel server, try to hook up the
407 * connection now. Preference is given to the host and port specified
408 * by browser cookies, if cookies have been supplied.
411 serv_sock = connectsock(c_host, c_port, "tcp");
413 serv_gets(buf); /* get the server welcome message */
414 strcpy(wc_host, c_host);
415 strcpy(wc_port, c_port);
421 * If we're not logged in, but we have username and password cookies
422 * supplied by the browser, try using them to log in.
424 if ((!logged_in)&&(strlen(c_username)>0)&&(strlen(c_password)>0)) {
425 serv_printf("USER %s", c_username);
428 serv_printf("PASS %s", c_password);
431 become_logged_in(c_username, c_password, buf);
437 * If we don't have a current room, but a cookie specifying the
438 * current room is supplied, make an effort to go there.
440 if ((strlen(wc_roomname)==0) && (strlen(c_roomname)>0) ) {
441 serv_printf("GOTO %s", c_roomname);
444 strcpy(wc_roomname, c_roomname);
448 /* If there are variables in the URL, we must grab them now */
449 for (a=0; a<strlen(cmd); ++a) if (cmd[a]=='?') {
450 for (b=a; b<strlen(cmd); ++b) if (isspace(cmd[b])) cmd[b]=0;
455 if (!strncasecmp(cmd, "GET /static/", 12)) {
456 strcpy(buf, &cmd[12]);
457 for (a=0; a<strlen(buf); ++a) if (isspace(buf[a])) buf[a]=0;
462 else if ((!logged_in)&&(!strncasecmp(cmd, "POST /login", 11))) {
466 else if (!logged_in) {
470 /* Various commands... */
472 else if (!strncasecmp(cmd, "GET /do_welcome", 15)) {
476 else if (!strncasecmp(cmd, "GET /display_main_menu", 22)) {
480 else if (!strncasecmp(cmd, "GET /advanced", 13)) {
481 display_advanced_menu();
484 else if (!strncasecmp(cmd, "GET /whobbs", 11)) {
488 else if (!strncasecmp(cmd, "GET /knrooms", 12)) {
489 list_all_rooms_by_floor();
492 else if (!strncasecmp(cmd, "GET /gotonext", 13)) {
497 else if (!strncasecmp(cmd, "GET /skip", 9)) {
501 else if (!strncasecmp(cmd, "GET /ungoto", 11)) {
505 else if (!strncasecmp(cmd, "GET /dotgoto", 12)) {
510 else if (!strncasecmp(cmd, "GET /termquit", 13)) {
514 /* When all else fails... */
516 printf("HTTP/1.0 200 OK\n");
519 wprintf("<HTML><HEAD><TITLE>WebCit</TITLE></HEAD><BODY>\n");
520 wprintf("TransactionCount is %d<HR>\n", TransactionCount);
521 wprintf("You're in session %d<BR>\n", wc_session);
522 wprintf("</BODY></HTML>\n");
527 if (content != NULL) {
534 int main(int argc, char *argv[]) {
536 if (argc < 2 || argc > 4) {
538 "webcit: usage: webcit <session_id> [host [port]]\n");
542 wc_session = atoi(argv[1]);
545 defaulthost = argv[2];
547 defaultport = argv[3];
552 strcpy(wc_username, "");
553 strcpy(wc_password, "");
554 strcpy(wc_roomname, "");