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() {
148 int ContentLength = 0;
151 /* We stuff these with the values coming from the client cookies,
152 * so we can use them to reconnect a timed out session if we have to.
156 char c_username[256];
157 char c_password[256];
158 char c_roomname[256];
160 strcpy(c_host, DEFAULT_HOST);
161 strcpy(c_port, DEFAULT_PORT);
162 strcpy(c_username, "");
163 strcpy(c_password, "");
164 strcpy(c_roomname, "");
167 fprintf(stderr, "\nCmd: %s\n", cmd);
172 fprintf(stderr, "Buf: %s\n", buf);
175 if (!strncasecmp(buf, "Cookie: wc_host=", 16))
176 strcpy(c_host, &buf[16]);
177 if (!strncasecmp(buf, "Cookie: wc_port=", 16))
178 strcpy(c_port, &buf[16]);
179 if (!strncasecmp(buf, "Cookie: wc_username=", 20))
180 strcpy(c_username, &buf[20]);
181 if (!strncasecmp(buf, "Cookie: wc_password=", 20))
182 strcpy(c_password, &buf[20]);
183 if (!strncasecmp(buf, "Cookie: wc_roomname=", 20))
184 strcpy(c_roomname, &buf[20]);
186 if (!strncasecmp(buf, "Content-length: ", 16)) {
187 ContentLength = atoi(&buf[16]);
190 } while(strlen(buf)>0);
194 if (ContentLength > 0) {
195 content = malloc(ContentLength+1);
196 fread(content, ContentLength, 1, stdin);
197 content[ContentLength] = 0;
198 fprintf(stderr, "CONTENT:\n%s\n", content);
205 * If we're not connected to a Citadel server, try to hook up the
206 * connection now. Preference is given to the host and port specified
207 * by browser cookies, if cookies have been supplied.
210 serv_sock = connectsock(c_host, c_port, "tcp");
212 strcpy(wc_host, c_host);
213 strcpy(wc_port, c_port);
214 serv_printf("IDEN %d|%d|%d|%s|%s",
221 serv_gets(buf); /* FIX find out where the user is */
226 * If we're not logged in, but we have username and password cookies
227 * supplied by the browser, try using them to log in.
229 if ((!logged_in)&&(strlen(c_username)>0)&&(strlen(c_password)>0)) {
230 serv_printf("USER %s", c_username);
233 serv_printf("PASS %s", c_password);
237 strcpy(wc_username, c_username);
238 strcpy(wc_password, c_password);
244 * If we don't have a current room, but a cookie specifying the
245 * current room is supplied, make an effort to go there.
247 if ((strlen(wc_roomname)==0) && (strlen(c_roomname)>0) ) {
248 serv_printf("GOTO %s", c_roomname);
251 strcpy(wc_roomname, c_roomname);
255 if (!strncasecmp(cmd, "GET /static/", 12)) {
256 strcpy(buf, &cmd[12]);
257 for (a=0; a<strlen(buf); ++a) if (isspace(buf[a])) buf[a]=0;
261 else if (!logged_in) {
262 display_login_page();
266 printf("HTTP/1.0 200 OK\n");
269 wprintf("<HTML><HEAD><TITLE>WebCit</TITLE></HEAD><BODY>\n");
270 wprintf("TransactionCount is %d<HR>\n", TransactionCount);
271 wprintf("You're in session %d<BR>\n", wc_session);
272 wprintf("</BODY></HTML>\n");
277 if (content != NULL) {
285 int main(int argc, char *argv[]) {
288 printf("%s: usage: %s <session_id>\n", argv[0], argv[0]);
292 wc_session = atoi(argv[1]);
295 strcpy(wc_username, "");
296 strcpy(wc_password, "");
297 strcpy(wc_roomname, "");