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);
103 fprintf(stderr, "%s=%s\n", u->url_key, u->url_data);
109 struct urlcontent *u;
111 while (urlstrings != NULL) {
112 free(urlstrings->url_data);
113 u = urlstrings->next;
119 char *bstr(char *key) {
120 struct urlcontent *u;
122 for (u = urlstrings; u != NULL; u = u->next) {
123 if (!strcasecmp(u->url_key, key)) return(u->url_data);
129 void wprintf(const char *format, ...) {
131 struct webcontent *wptr;
133 wptr = (struct webcontent *)malloc(sizeof(struct webcontent));
144 va_start(arg_ptr, format);
145 vsprintf(wptr->w_data, format, arg_ptr);
150 int wContentLength() {
151 struct webcontent *wptr;
154 for (wptr = wlist; wptr != NULL; wptr = wptr->next) {
155 len = len + strlen(wptr->w_data);
161 void wDumpContent() {
162 struct webcontent *wptr;
164 printf("Content-type: text/html\n");
165 printf("Content-length: %d\n", wContentLength());
168 while (wlist != NULL) {
169 fwrite(wlist->w_data, strlen(wlist->w_data), 1, stdout);
177 void getz(char *buf) {
178 if (fgets(buf, 256, stdin) == NULL) strcpy(buf, "");
180 while ((strlen(buf)>0)&&(!isprint(buf[strlen(buf)-1])))
181 buf[strlen(buf)-1] = 0;
186 * Output all that important stuff that the browser will want to see
188 void output_headers() {
189 printf("Server: %s\n", SERVER);
190 printf("Connection: close\n");
191 printf("Set-cookie: wc_session=%d\n", wc_session);
192 if (strlen(wc_host)>0) printf("Set-cookie: wc_host=%s\n", wc_host);
193 if (strlen(wc_port)>0) printf("Set-cookie: wc_port=%s\n", wc_port);
194 if (strlen(wc_username)>0) printf("Set-cookie: wc_username=%s\n",
196 if (strlen(wc_password)>0) printf("Set-cookie: wc_password=%s\n",
198 if (strlen(wc_roomname)>0) printf("Set-cookie: wc_roomname=%s\n",
202 void output_static(char *what) {
208 sprintf(buf, "static/%s", what);
209 fp = fopen(buf, "rb");
211 printf("HTTP/1.0 404 %s\n", strerror(errno));
213 printf("Content-Type: text/plain\n");
214 sprintf(buf, "%s: %s\n", what, strerror(errno));
215 printf("Content-length: %d\n", strlen(buf));
217 fwrite(buf, strlen(buf), 1, stdout);
220 printf("HTTP/1.0 200 OK\n");
223 if (!strncasecmp(&what[strlen(what)-4], ".gif", 4))
224 printf("Content-type: image/gif\n");
226 printf("Content-type: application/octet-stream\n");
228 fstat(fileno(fp), &statbuf);
229 bytes = statbuf.st_size;
230 printf("Content-length: %d\n", bytes);
233 putc(getc(fp), stdout);
240 void session_loop() {
244 int ContentLength = 0;
247 /* We stuff these with the values coming from the client cookies,
248 * so we can use them to reconnect a timed out session if we have to.
252 char c_username[256];
253 char c_password[256];
254 char c_roomname[256];
256 strcpy(c_host, DEFAULT_HOST);
257 strcpy(c_port, DEFAULT_PORT);
258 strcpy(c_username, "");
259 strcpy(c_password, "");
260 strcpy(c_roomname, "");
263 fprintf(stderr, "\nCmd: %s\n", cmd);
268 fprintf(stderr, "Buf: %s\n", buf);
271 if (!strncasecmp(buf, "Cookie: wc_host=", 16))
272 strcpy(c_host, &buf[16]);
273 if (!strncasecmp(buf, "Cookie: wc_port=", 16))
274 strcpy(c_port, &buf[16]);
275 if (!strncasecmp(buf, "Cookie: wc_username=", 20))
276 strcpy(c_username, &buf[20]);
277 if (!strncasecmp(buf, "Cookie: wc_password=", 20))
278 strcpy(c_password, &buf[20]);
279 if (!strncasecmp(buf, "Cookie: wc_roomname=", 20))
280 strcpy(c_roomname, &buf[20]);
282 if (!strncasecmp(buf, "Content-length: ", 16)) {
283 ContentLength = atoi(&buf[16]);
286 } while(strlen(buf)>0);
290 if (ContentLength > 0) {
291 content = malloc(ContentLength+1);
292 fread(content, ContentLength, 1, stdin);
293 content[ContentLength] = 0;
294 fprintf(stderr, "CONTENT:\n%s\n", content);
302 * If we're not connected to a Citadel server, try to hook up the
303 * connection now. Preference is given to the host and port specified
304 * by browser cookies, if cookies have been supplied.
307 serv_sock = connectsock(c_host, c_port, "tcp");
309 serv_gets(buf); /* get the server welcome message */
310 strcpy(wc_host, c_host);
311 strcpy(wc_port, c_port);
312 serv_printf("IDEN %d|%d|%d|%s|%s",
320 /* FIX find out where the user is */
325 * If we're not logged in, but we have username and password cookies
326 * supplied by the browser, try using them to log in.
328 if ((!logged_in)&&(strlen(c_username)>0)&&(strlen(c_password)>0)) {
329 serv_printf("USER %s", c_username);
332 serv_printf("PASS %s", c_password);
335 become_logged_in(c_username, c_password, buf);
341 * If we don't have a current room, but a cookie specifying the
342 * current room is supplied, make an effort to go there.
344 if ((strlen(wc_roomname)==0) && (strlen(c_roomname)>0) ) {
345 serv_printf("GOTO %s", c_roomname);
348 strcpy(wc_roomname, c_roomname);
352 /* If there are variables in the URL, we must grab them now */
353 for (a=0; a<strlen(cmd); ++a) if (cmd[a]=='?') {
354 for (b=a; b<strlen(cmd); ++b) if (isspace(cmd[b])) cmd[b]=0;
359 if (!strncasecmp(cmd, "GET /static/", 12)) {
360 strcpy(buf, &cmd[12]);
361 for (a=0; a<strlen(buf); ++a) if (isspace(buf[a])) buf[a]=0;
366 else if ((!logged_in)&&(!strncasecmp(cmd, "POST /login", 11))) {
370 else if (!logged_in) {
371 display_login_page();
374 /* Various commands... */
376 else if (!strncasecmp(cmd, "GET /display_main_menu", 22)) {
380 /* When all else fails, display the oops page. */
382 printf("HTTP/1.0 200 OK\n");
385 wprintf("<HTML><HEAD><TITLE>WebCit</TITLE></HEAD><BODY>\n");
386 wprintf("TransactionCount is %d<HR>\n", TransactionCount);
387 wprintf("You're in session %d<BR>\n", wc_session);
388 wprintf("</BODY></HTML>\n");
393 if (content != NULL) {
402 int main(int argc, char *argv[]) {
405 printf("%s: usage: %s <session_id>\n", argv[0], argv[0]);
409 wc_session = atoi(argv[1]);
412 strcpy(wc_username, "");
413 strcpy(wc_password, "");
414 strcpy(wc_roomname, "");