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]);
188 fprintf(stderr, "ContentLength is %d\n", ContentLength);
191 } while(strlen(buf)>0);
195 if (ContentLength > 0) {
196 content = malloc(ContentLength+1);
197 fread(content, ContentLength, 1, stdin);
198 content[ContentLength] = 0;
199 fprintf(stderr, "CONTENT:\n%s\n", content);
206 * If we're not connected to a Citadel server, try to hook up the
207 * connection now. Preference is given to the host and port specified
208 * by browser cookies, if cookies have been supplied.
211 serv_sock = connectsock(c_host, c_port, "tcp");
213 strcpy(wc_host, c_host);
214 strcpy(wc_port, c_port);
218 * If we're not logged in, but we have username and password cookies
219 * supplied by the browser, try using them to log in.
221 if ((!logged_in)&&(strlen(c_username)>0)&&(strlen(c_password)>0)) {
222 serv_printf("USER %s", c_username);
225 serv_printf("PASS %s", c_password);
229 strcpy(wc_username, c_username);
230 strcpy(wc_password, c_password);
236 * If we don't have a current room, but a cookie specifying the
237 * current room is supplied, make an effort to go there.
239 if ((strlen(wc_roomname)==0) && (strlen(c_roomname)>0) ) {
240 serv_printf("GOTO %s", c_roomname);
243 strcpy(wc_roomname, c_roomname);
247 if (!strncasecmp(cmd, "GET /static/", 12)) {
248 strcpy(buf, &cmd[12]);
249 for (a=0; a<strlen(buf); ++a) if (isspace(buf[a])) buf[a]=0;
253 else if (!logged_in) {
254 display_login_page();
258 printf("HTTP/1.0 200 OK\n");
261 wprintf("<HTML><HEAD><TITLE>WebCit</TITLE></HEAD><BODY>\n");
262 wprintf("TransactionCount is %d<HR>\n", TransactionCount);
263 wprintf("You're in session %d<BR>\n", wc_session);
264 wprintf("</BODY></HTML>\n");
269 if (content != NULL) {
277 int main(int argc, char *argv[]) {
280 printf("%s: usage: %s <session_id>\n", argv[0], argv[0]);
284 wc_session = atoi(argv[1]);
287 strcpy(wc_username, "");
288 strcpy(wc_password, "");
289 strcpy(wc_roomname, "");