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;
33 void wprintf(const char *format, ...) {
35 struct webcontent *wptr;
37 wptr = (struct webcontent *)malloc(sizeof(struct webcontent));
48 va_start(arg_ptr, format);
49 vsprintf(wptr->w_data, format, arg_ptr);
54 int wContentLength() {
55 struct webcontent *wptr;
58 for (wptr = wlist; wptr != NULL; wptr = wptr->next) {
59 len = len + strlen(wptr->w_data);
66 struct webcontent *wptr;
68 printf("Content-type: text/html\n");
69 printf("Content-length: %d\n", wContentLength());
72 while (wlist != NULL) {
73 fwrite(wlist->w_data, strlen(wlist->w_data), 1, stdout);
81 void getz(char *buf) {
82 if (fgets(buf, 256, stdin) == NULL) strcpy(buf, "");
84 while ((strlen(buf)>0)&&(!isprint(buf[strlen(buf)-1])))
85 buf[strlen(buf)-1] = 0;
90 * Output all that important stuff that the browser will want to see
92 void output_headers() {
93 printf("Server: %s\n", SERVER);
94 printf("Connection: close\n");
95 printf("Set-cookie: wc_session=%d\n", wc_session);
96 if (strlen(wc_host)>0) printf("Set-cookie: wc_host=%s\n", wc_host);
97 if (strlen(wc_port)>0) printf("Set-cookie: wc_port=%s\n", wc_port);
98 if (strlen(wc_username)>0) printf("Set-cookie: wc_username=%s\n",
100 if (strlen(wc_password)>0) printf("Set-cookie: wc_password=%s\n",
102 if (strlen(wc_roomname)>0) printf("Set-cookie: wc_roomname=%s\n",
106 void output_static(char *what) {
112 sprintf(buf, "static/%s", what);
113 fp = fopen(buf, "rb");
115 printf("HTTP/1.0 404 %s\n", strerror(errno));
117 printf("Content-Type: text/plain\n");
118 sprintf(buf, "%s: %s\n", what, strerror(errno));
119 printf("Content-length: %d\n", strlen(buf));
121 fwrite(buf, strlen(buf), 1, stdout);
124 printf("HTTP/1.0 200 OK\n");
127 if (!strncasecmp(&what[strlen(what)-4], ".gif", 4))
128 printf("Content-type: image/gif\n");
130 printf("Content-type: junk/data\n");
132 fstat(fileno(fp), &statbuf);
133 bytes = statbuf.st_size;
134 printf("Content-length: %d\n", bytes);
137 putc(getc(fp), stdout);
144 void session_loop() {
149 /* We stuff these with the values coming from the client cookies,
150 * so we can use them to reconnect a timed out session if we have to.
154 char c_username[256];
155 char c_password[256];
156 char c_roomname[256];
158 strcpy(c_host, DEFAULT_HOST);
159 strcpy(c_port, DEFAULT_PORT);
160 strcpy(c_username, "");
161 strcpy(c_password, "");
162 strcpy(c_roomname, "");
165 fprintf(stderr, "Cmd: %s\n", cmd);
171 if (!strncasecmp(buf, "Cookie: wc_host=", 16))
172 strcpy(c_host, &buf[16]);
173 if (!strncasecmp(buf, "Cookie: wc_port=", 16))
174 strcpy(c_port, &buf[16]);
175 if (!strncasecmp(buf, "Cookie: wc_username=", 20))
176 strcpy(c_username, &buf[20]);
177 if (!strncasecmp(buf, "Cookie: wc_password=", 20))
178 strcpy(c_password, &buf[20]);
179 if (!strncasecmp(buf, "Cookie: wc_roomname=", 20))
180 strcpy(c_roomname, &buf[20]);
182 } while(strlen(buf)>0);
187 * If we're not connected to a Citadel server, try to hook up the
188 * connection now. Preference is given to the host and port specified
189 * by browser cookies, if cookies have been supplied.
192 serv_sock = connectsock(c_host, c_port, "tcp");
194 strcpy(wc_host, c_host);
195 strcpy(wc_port, c_port);
199 * If we're not logged in, but we have username and password cookies
200 * supplied by the browser, try using them to log in.
202 if ((!logged_in)&&(strlen(c_username)>0)&&(strlen(c_password)>0)) {
203 serv_printf("USER %s", c_username);
206 serv_printf("PASS %s", c_password);
210 strcpy(wc_username, c_username);
211 strcpy(wc_password, c_password);
217 * If we don't have a current room, but a cookie specifying the
218 * current room is supplied, make an effort to go there.
220 if ((strlen(wc_roomname)==0) && (strlen(c_roomname)>0) ) {
221 serv_printf("GOTO %s", c_roomname);
224 strcpy(wc_roomname, c_roomname);
228 if (!strncasecmp(cmd, "GET /static/", 12)) {
229 strcpy(buf, &cmd[12]);
230 for (a=0; a<strlen(buf); ++a) if (isspace(buf[a])) buf[a]=0;
234 else if (!logged_in) {
235 display_login_page();
239 printf("HTTP/1.0 200 OK\n");
242 wprintf("<HTML><HEAD><TITLE>WebCit</TITLE></HEAD><BODY>\n");
243 wprintf("TransactionCount is %d<HR>\n", TransactionCount);
244 wprintf("You're in session %d<BR>\n", wc_session);
245 wprintf("</BODY></HTML>\n");
254 int main(int argc, char *argv[]) {
257 printf("%s: usage: %s <session_id>\n", argv[0], argv[0]);
261 wc_session = atoi(argv[1]);
264 strcpy(wc_username, "");
265 strcpy(wc_password, "");
266 strcpy(wc_roomname, "");