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.
22 char wc_username[256];
23 char wc_password[256];
24 char wc_roomname[256];
25 int TransactionCount = 0;
30 struct webcontent *wlist = NULL;
31 struct webcontent *wlast = NULL;
33 struct urlcontent *urlstrings = NULL;
36 void unescape_input(buf)
41 while ((isspace(buf[strlen(buf)-1]))&&(strlen(buf)>0))
42 buf[strlen(buf)-1] = 0;
44 for (a=0; a<strlen(buf); ++a) {
45 if (buf[a]=='+') buf[a]=' ';
50 sscanf(hex,"%02x",&b);
52 strcpy(&buf[a+1],&buf[a+3]);
59 void addurls(char *url) {
66 while (strlen(up)>0) {
68 /* locate the = sign */
71 for (a=255; a>=0; --a) if (buf[a]=='=') b=a;
75 u = (struct urlcontent *)malloc(sizeof(struct urlcontent));
78 strcpy(u->url_key, buf);
80 /* now chop that part off */
81 for (a=0; a<=b; ++a) ++up;
83 /* locate the & sign */
86 for (a=0; a<strlen(up); ++a) {
87 if (!strncmp(ptr,"&",1)) {
94 for (a=0; a<b; ++a) ++ptr;
97 u->url_data = malloc(strlen(up));
98 strcpy(u->url_data, up);
99 unescape_input(u->url_data);
108 struct urlcontent *u;
110 while (urlstrings != NULL) {
111 free(urlstrings->url_data);
112 u = urlstrings->next;
118 char *bstr(char *key) {
119 struct urlcontent *u;
121 for (u = urlstrings; u != NULL; u = u->next) {
122 if (!strcasecmp(u->url_key, key)) return(u->url_data);
128 void wprintf(const char *format, ...) {
130 struct webcontent *wptr;
132 wptr = (struct webcontent *)malloc(sizeof(struct webcontent));
143 va_start(arg_ptr, format);
144 vsprintf(wptr->w_data, format, arg_ptr);
149 int wContentLength() {
150 struct webcontent *wptr;
153 for (wptr = wlist; wptr != NULL; wptr = wptr->next) {
154 len = len + strlen(wptr->w_data);
160 void wDumpContent() {
161 struct webcontent *wptr;
163 printf("Content-type: text/html\n");
164 printf("Content-length: %d\n", wContentLength());
167 while (wlist != NULL) {
168 fwrite(wlist->w_data, strlen(wlist->w_data), 1, stdout);
177 void escputs1(strbuf,nbsp)
182 for (a=0; a<strlen(strbuf); ++a) {
183 if (strbuf[a]=='<') wprintf("<");
184 else if (strbuf[a]=='>') wprintf(">");
185 else if (strbuf[a]=='&') wprintf("&");
186 else if (strbuf[a]==34) wprintf(""");
187 else if (strbuf[a]==LB) wprintf("<");
188 else if (strbuf[a]==RB) wprintf(">");
189 else if (strbuf[a]==QU) wprintf("\"");
190 else if ((strbuf[a]==32)&&(nbsp==1)) {
194 wprintf("%c", strbuf[a]);
209 char *ec = " #&;`'|*?-~<>^()[]{}$\\";
210 static char outbuf[512];
214 for (a=0; a<strlen(strbuf); ++a) {
216 for (b=0; b<strlen(ec); ++b) {
217 if (strbuf[a]==ec[b]) c=1;
220 if (c==1) sprintf(&outbuf[b],"%%%02x",strbuf[a]);
221 else sprintf(&outbuf[b],"%c",strbuf[a]);
226 void urlescputs(strbuf)
228 wprintf("%s",urlesc(strbuf));
233 * Look for URL's embedded in a buffer and make them linkable. We use a
234 * target window in order to keep the BBS session in its own window.
249 for (pos=0; pos<strlen(buf); ++pos) {
250 if (!strncasecmp(&buf[pos],"http://",7)) start = pos;
251 if (!strncasecmp(&buf[pos],"ftp://",6)) start = pos;
256 if ((start>0)&&(buf[start-1]=='<')) ench = '>';
257 if ((start>0)&&(buf[start-1]=='[')) ench = ']';
258 if ((start>0)&&(buf[start-1]=='(')) ench = ')';
259 if ((start>0)&&(buf[start-1]=='{')) ench = '}';
261 for (pos=strlen(buf); pos>start; --pos) {
262 if ((buf[pos]==' ')||(buf[pos]==ench)) end = pos;
265 strncpy(urlbuf,&buf[start],end-start);
266 urlbuf[end-start] = 0;
269 strncpy(outbuf,buf,start);
270 sprintf(&outbuf[start],"%cA HREF=%c%s%c TARGET=%c%s%c%c%s%c/A%c",
271 LB,QU,urlbuf,QU,QU,TARGET,QU,RB,urlbuf,LB,RB);
272 strcat(outbuf,&buf[end]);
279 void getz(char *buf) {
280 if (fgets(buf, 256, stdin) == NULL) strcpy(buf, "");
282 while ((strlen(buf)>0)&&(!isprint(buf[strlen(buf)-1])))
283 buf[strlen(buf)-1] = 0;
288 * Output all that important stuff that the browser will want to see
290 void output_headers() {
291 printf("Server: %s\n", SERVER);
292 printf("Connection: close\n");
293 printf("Set-cookie: wc_session=%d\n", wc_session);
294 if (strlen(wc_host)>0) printf("Set-cookie: wc_host=%s\n", wc_host);
295 if (strlen(wc_port)>0) printf("Set-cookie: wc_port=%s\n", wc_port);
296 if (strlen(wc_username)>0) printf("Set-cookie: wc_username=%s\n",
298 if (strlen(wc_password)>0) printf("Set-cookie: wc_password=%s\n",
300 if (strlen(wc_roomname)>0) printf("Set-cookie: wc_roomname=%s\n",
304 void output_static(char *what) {
310 sprintf(buf, "static/%s", what);
311 fp = fopen(buf, "rb");
313 printf("HTTP/1.0 404 %s\n", strerror(errno));
315 printf("Content-Type: text/plain\n");
316 sprintf(buf, "%s: %s\n", what, strerror(errno));
317 printf("Content-length: %d\n", strlen(buf));
319 fwrite(buf, strlen(buf), 1, stdout);
322 printf("HTTP/1.0 200 OK\n");
325 if (!strncasecmp(&what[strlen(what)-4], ".gif", 4))
326 printf("Content-type: image/gif\n");
327 else if (!strncasecmp(&what[strlen(what)-5], ".html", 5))
328 printf("Content-type: text/html\n");
330 printf("Content-type: application/octet-stream\n");
332 fstat(fileno(fp), &statbuf);
333 bytes = statbuf.st_size;
334 printf("Content-length: %d\n", bytes);
337 putc(getc(fp), stdout);
344 void session_loop() {
348 int ContentLength = 0;
351 /* We stuff these with the values coming from the client cookies,
352 * so we can use them to reconnect a timed out session if we have to.
356 char c_username[256];
357 char c_password[256];
358 char c_roomname[256];
360 strcpy(c_host, DEFAULT_HOST);
361 strcpy(c_port, DEFAULT_PORT);
362 strcpy(c_username, "");
363 strcpy(c_password, "");
364 strcpy(c_roomname, "");
371 if (!strncasecmp(buf, "Cookie: wc_host=", 16))
372 strcpy(c_host, &buf[16]);
373 if (!strncasecmp(buf, "Cookie: wc_port=", 16))
374 strcpy(c_port, &buf[16]);
375 if (!strncasecmp(buf, "Cookie: wc_username=", 20))
376 strcpy(c_username, &buf[20]);
377 if (!strncasecmp(buf, "Cookie: wc_password=", 20))
378 strcpy(c_password, &buf[20]);
379 if (!strncasecmp(buf, "Cookie: wc_roomname=", 20))
380 strcpy(c_roomname, &buf[20]);
382 if (!strncasecmp(buf, "Content-length: ", 16)) {
383 ContentLength = atoi(&buf[16]);
386 } while(strlen(buf)>0);
390 if (ContentLength > 0) {
391 content = malloc(ContentLength+1);
392 fread(content, ContentLength, 1, stdin);
393 content[ContentLength] = 0;
401 * If we're not connected to a Citadel server, try to hook up the
402 * connection now. Preference is given to the host and port specified
403 * by browser cookies, if cookies have been supplied.
406 serv_sock = connectsock(c_host, c_port, "tcp");
408 serv_gets(buf); /* get the server welcome message */
409 strcpy(wc_host, c_host);
410 strcpy(wc_port, c_port);
416 * If we're not logged in, but we have username and password cookies
417 * supplied by the browser, try using them to log in.
419 if ((!logged_in)&&(strlen(c_username)>0)&&(strlen(c_password)>0)) {
420 serv_printf("USER %s", c_username);
423 serv_printf("PASS %s", c_password);
426 become_logged_in(c_username, c_password, buf);
432 * If we don't have a current room, but a cookie specifying the
433 * current room is supplied, make an effort to go there.
435 if ((strlen(wc_roomname)==0) && (strlen(c_roomname)>0) ) {
436 serv_printf("GOTO %s", c_roomname);
439 strcpy(wc_roomname, c_roomname);
443 /* If there are variables in the URL, we must grab them now */
444 for (a=0; a<strlen(cmd); ++a) if (cmd[a]=='?') {
445 for (b=a; b<strlen(cmd); ++b) if (isspace(cmd[b])) cmd[b]=0;
450 if (!strncasecmp(cmd, "GET /static/", 12)) {
451 strcpy(buf, &cmd[12]);
452 for (a=0; a<strlen(buf); ++a) if (isspace(buf[a])) buf[a]=0;
457 else if ((!logged_in)&&(!strncasecmp(cmd, "POST /login", 11))) {
461 else if (!logged_in) {
465 /* Various commands... */
467 else if (!strncasecmp(cmd, "GET /do_welcome", 15)) {
471 else if (!strncasecmp(cmd, "GET /display_main_menu", 22)) {
475 else if (!strncasecmp(cmd, "GET /advanced", 13)) {
476 display_advanced_menu();
479 else if (!strncasecmp(cmd, "GET /whobbs", 11)) {
483 else if (!strncasecmp(cmd, "GET /knrooms", 12)) {
484 list_all_rooms_by_floor();
487 else if (!strncasecmp(cmd, "GET /gotonext", 13)) {
492 else if (!strncasecmp(cmd, "GET /skip", 9)) {
496 else if (!strncasecmp(cmd, "GET /ungoto", 11)) {
500 else if (!strncasecmp(cmd, "GET /dotgoto", 12)) {
505 else if (!strncasecmp(cmd, "GET /termquit", 13)) {
509 /* When all else fails... */
511 printf("HTTP/1.0 200 OK\n");
514 wprintf("<HTML><HEAD><TITLE>WebCit</TITLE></HEAD><BODY>\n");
515 wprintf("TransactionCount is %d<HR>\n", TransactionCount);
516 wprintf("You're in session %d<BR>\n", wc_session);
517 wprintf("</BODY></HTML>\n");
522 if (content != NULL) {
531 int main(int argc, char *argv[]) {
534 printf("%s: usage: %s <session_id>\n", argv[0], argv[0]);
538 wc_session = atoi(argv[1]);
541 strcpy(wc_username, "");
542 strcpy(wc_password, "");
543 strcpy(wc_roomname, "");