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;
29 struct webcontent *wlist = NULL;
30 struct webcontent *wlast = NULL;
32 struct urlcontent *urlstrings = NULL;
35 void unescape_input(buf)
40 while ((isspace(buf[strlen(buf)-1]))&&(strlen(buf)>0))
41 buf[strlen(buf)-1] = 0;
43 for (a=0; a<strlen(buf); ++a) {
44 if (buf[a]=='+') buf[a]=' ';
49 sscanf(hex,"%02x",&b);
51 strcpy(&buf[a+1],&buf[a+3]);
58 void addurls(char *url) {
65 while (strlen(up)>0) {
67 /* locate the = sign */
70 for (a=255; a>=0; --a) if (buf[a]=='=') b=a;
74 u = (struct urlcontent *)malloc(sizeof(struct urlcontent));
77 strcpy(u->url_key, buf);
79 /* now chop that part off */
80 for (a=0; a<=b; ++a) ++up;
82 /* locate the & sign */
85 for (a=0; a<strlen(up); ++a) {
86 if (!strncmp(ptr,"&",1)) {
93 for (a=0; a<b; ++a) ++ptr;
96 u->url_data = malloc(strlen(up));
97 strcpy(u->url_data, up);
98 unescape_input(u->url_data);
107 struct urlcontent *u;
109 while (urlstrings != NULL) {
110 free(urlstrings->url_data);
111 u = urlstrings->next;
117 char *bstr(char *key) {
118 struct urlcontent *u;
120 for (u = urlstrings; u != NULL; u = u->next) {
121 if (!strcasecmp(u->url_key, key)) return(u->url_data);
127 void wprintf(const char *format, ...) {
129 struct webcontent *wptr;
131 wptr = (struct webcontent *)malloc(sizeof(struct webcontent));
142 va_start(arg_ptr, format);
143 vsprintf(wptr->w_data, format, arg_ptr);
148 int wContentLength() {
149 struct webcontent *wptr;
152 for (wptr = wlist; wptr != NULL; wptr = wptr->next) {
153 len = len + strlen(wptr->w_data);
159 void wDumpContent() {
160 struct webcontent *wptr;
162 printf("Content-type: text/html\n");
163 printf("Content-length: %d\n", wContentLength());
166 while (wlist != NULL) {
167 fwrite(wlist->w_data, strlen(wlist->w_data), 1, stdout);
176 void escputs1(strbuf,nbsp)
181 for (a=0; a<strlen(strbuf); ++a) {
182 if (strbuf[a]=='<') wprintf("<");
183 else if (strbuf[a]=='>') wprintf(">");
184 else if (strbuf[a]=='&') wprintf("&");
185 else if (strbuf[a]==34) wprintf(""");
186 else if (strbuf[a]==LB) wprintf("<");
187 else if (strbuf[a]==RB) wprintf(">");
188 else if (strbuf[a]==QU) wprintf("\"");
189 else if ((strbuf[a]==32)&&(nbsp==1)) {
193 wprintf("%c", strbuf[a]);
208 char *ec = " #&;`'|*?-~<>^()[]{}$\\";
209 static char outbuf[512];
213 for (a=0; a<strlen(strbuf); ++a) {
215 for (b=0; b<strlen(ec); ++b) {
216 if (strbuf[a]==ec[b]) c=1;
219 if (c==1) sprintf(&outbuf[b],"%%%02x",strbuf[a]);
220 else sprintf(&outbuf[b],"%c",strbuf[a]);
225 void urlescputs(strbuf)
227 wprintf("%s",urlesc(strbuf));
232 * Look for URL's embedded in a buffer and make them linkable. We use a
233 * target window in order to keep the BBS session in its own window.
248 for (pos=0; pos<strlen(buf); ++pos) {
249 if (!strncasecmp(&buf[pos],"http://",7)) start = pos;
250 if (!strncasecmp(&buf[pos],"ftp://",6)) start = pos;
255 if ((start>0)&&(buf[start-1]=='<')) ench = '>';
256 if ((start>0)&&(buf[start-1]=='[')) ench = ']';
257 if ((start>0)&&(buf[start-1]=='(')) ench = ')';
258 if ((start>0)&&(buf[start-1]=='{')) ench = '}';
260 for (pos=strlen(buf); pos>start; --pos) {
261 if ((buf[pos]==' ')||(buf[pos]==ench)) end = pos;
264 strncpy(urlbuf,&buf[start],end-start);
265 urlbuf[end-start] = 0;
268 strncpy(outbuf,buf,start);
269 sprintf(&outbuf[start],"%cA HREF=%c%s%c TARGET=%c%s%c%c%s%c/A%c",
270 LB,QU,urlbuf,QU,QU,TARGET,QU,RB,urlbuf,LB,RB);
271 strcat(outbuf,&buf[end]);
278 void getz(char *buf) {
279 if (fgets(buf, 256, stdin) == NULL) strcpy(buf, "");
281 while ((strlen(buf)>0)&&(!isprint(buf[strlen(buf)-1])))
282 buf[strlen(buf)-1] = 0;
287 * Output all that important stuff that the browser will want to see
289 void output_headers() {
290 printf("Server: %s\n", SERVER);
291 printf("Connection: close\n");
292 printf("Set-cookie: wc_session=%d\n", wc_session);
293 if (strlen(wc_host)>0) printf("Set-cookie: wc_host=%s\n", wc_host);
294 if (strlen(wc_port)>0) printf("Set-cookie: wc_port=%s\n", wc_port);
295 if (strlen(wc_username)>0) printf("Set-cookie: wc_username=%s\n",
297 if (strlen(wc_password)>0) printf("Set-cookie: wc_password=%s\n",
299 if (strlen(wc_roomname)>0) printf("Set-cookie: wc_roomname=%s\n",
303 void output_static(char *what) {
309 sprintf(buf, "static/%s", what);
310 fp = fopen(buf, "rb");
312 printf("HTTP/1.0 404 %s\n", strerror(errno));
314 printf("Content-Type: text/plain\n");
315 sprintf(buf, "%s: %s\n", what, strerror(errno));
316 printf("Content-length: %d\n", strlen(buf));
318 fwrite(buf, strlen(buf), 1, stdout);
321 printf("HTTP/1.0 200 OK\n");
324 if (!strncasecmp(&what[strlen(what)-4], ".gif", 4))
325 printf("Content-type: image/gif\n");
326 else if (!strncasecmp(&what[strlen(what)-5], ".html", 5))
327 printf("Content-type: text/html\n");
329 printf("Content-type: application/octet-stream\n");
331 fstat(fileno(fp), &statbuf);
332 bytes = statbuf.st_size;
333 printf("Content-length: %d\n", bytes);
336 putc(getc(fp), stdout);
343 void session_loop() {
347 int ContentLength = 0;
350 /* We stuff these with the values coming from the client cookies,
351 * so we can use them to reconnect a timed out session if we have to.
355 char c_username[256];
356 char c_password[256];
357 char c_roomname[256];
359 strcpy(c_host, DEFAULT_HOST);
360 strcpy(c_port, DEFAULT_PORT);
361 strcpy(c_username, "");
362 strcpy(c_password, "");
363 strcpy(c_roomname, "");
370 if (!strncasecmp(buf, "Cookie: wc_host=", 16))
371 strcpy(c_host, &buf[16]);
372 if (!strncasecmp(buf, "Cookie: wc_port=", 16))
373 strcpy(c_port, &buf[16]);
374 if (!strncasecmp(buf, "Cookie: wc_username=", 20))
375 strcpy(c_username, &buf[20]);
376 if (!strncasecmp(buf, "Cookie: wc_password=", 20))
377 strcpy(c_password, &buf[20]);
378 if (!strncasecmp(buf, "Cookie: wc_roomname=", 20))
379 strcpy(c_roomname, &buf[20]);
381 if (!strncasecmp(buf, "Content-length: ", 16)) {
382 ContentLength = atoi(&buf[16]);
385 } while(strlen(buf)>0);
389 if (ContentLength > 0) {
390 content = malloc(ContentLength+1);
391 fread(content, ContentLength, 1, stdin);
392 content[ContentLength] = 0;
400 * If we're not connected to a Citadel server, try to hook up the
401 * connection now. Preference is given to the host and port specified
402 * by browser cookies, if cookies have been supplied.
405 serv_sock = connectsock(c_host, c_port, "tcp");
407 serv_gets(buf); /* get the server welcome message */
408 strcpy(wc_host, c_host);
409 strcpy(wc_port, c_port);
415 * If we're not logged in, but we have username and password cookies
416 * supplied by the browser, try using them to log in.
418 if ((!logged_in)&&(strlen(c_username)>0)&&(strlen(c_password)>0)) {
419 serv_printf("USER %s", c_username);
422 serv_printf("PASS %s", c_password);
425 become_logged_in(c_username, c_password, buf);
431 * If we don't have a current room, but a cookie specifying the
432 * current room is supplied, make an effort to go there.
434 if ((strlen(wc_roomname)==0) && (strlen(c_roomname)>0) ) {
435 serv_printf("GOTO %s", c_roomname);
438 strcpy(wc_roomname, c_roomname);
442 /* If there are variables in the URL, we must grab them now */
443 for (a=0; a<strlen(cmd); ++a) if (cmd[a]=='?') {
444 for (b=a; b<strlen(cmd); ++b) if (isspace(cmd[b])) cmd[b]=0;
449 if (!strncasecmp(cmd, "GET /static/", 12)) {
450 strcpy(buf, &cmd[12]);
451 for (a=0; a<strlen(buf); ++a) if (isspace(buf[a])) buf[a]=0;
456 else if ((!logged_in)&&(!strncasecmp(cmd, "POST /login", 11))) {
460 else if (!logged_in) {
461 display_login_page();
464 /* Various commands... */
466 else if (!strncasecmp(cmd, "GET /display_main_menu", 22)) {
470 else if (!strncasecmp(cmd, "GET /whobbs", 11)) {
474 else if (!strncasecmp(cmd, "GET /knrooms", 12)) {
475 list_all_rooms_by_floor();
478 else if (!strncasecmp(cmd, "GET /test", 9)) {
479 printf("HTTP/1.0 200 OK\n");
482 wprintf("<HTML><HEAD><TITLE>WebCit</TITLE></HEAD><BODY>\n");
483 wprintf("TransactionCount is %d<HR>\n", TransactionCount);
484 wprintf("You're in session %d<BR>\n", wc_session);
485 wprintf("</BODY></HTML>\n");
489 /* When all else fails... */
491 printf("HTTP/1.0 200 OK\n");
494 wprintf("<HTML><HEAD><TITLE>WebCit</TITLE></HEAD><BODY>\n");
495 wprintf("TransactionCount is %d<HR>\n", TransactionCount);
496 wprintf("You're in session %d<BR>\n", wc_session);
497 wprintf("</BODY></HTML>\n");
502 if (content != NULL) {
511 int main(int argc, char *argv[]) {
514 printf("%s: usage: %s <session_id>\n", argv[0], argv[0]);
518 wc_session = atoi(argv[1]);
521 strcpy(wc_username, "");
522 strcpy(wc_password, "");
523 strcpy(wc_roomname, "");