]> code.citadel.org Git - citadel.git/blob - webcit/webcit.c
got a basic framework for content working
[citadel.git] / webcit / webcit.c
1 /*
2  * webcit.c
3  *
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.
7  */
8
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <stdio.h>
12 #include <ctype.h>
13 #include <string.h>
14 #include <errno.h>
15 #include <sys/stat.h>
16 #include <stdarg.h>
17 #include "webcit.h"
18
19 int wc_session;
20 char wc_host[256];
21 int wc_port;
22 char wc_username[256];
23 char wc_password[256];
24 char wc_roomname[256];
25 int TransactionCount = 0;
26 int logged_in = 0;
27
28 struct webcontent *wlist = NULL;
29 struct webcontent *wlast = NULL;
30
31
32 void wprintf(const char *format, ...) {   
33         va_list arg_ptr;   
34         struct webcontent *wptr;
35
36         wptr = (struct webcontent *)malloc(sizeof(struct webcontent));
37         wptr->next = NULL;
38         if (wlist == NULL) {
39                 wlist = wptr;
40                 wlast = wptr;
41                 }
42         else {
43                 wlast->next = wptr;
44                 wlast = wptr;
45                 }
46   
47         va_start(arg_ptr, format);   
48         vsprintf(wptr->w_data, format, arg_ptr);   
49         va_end(arg_ptr);   
50   
51         }
52
53 int wContentLength() {
54         struct webcontent *wptr;
55         int len = 0;
56
57         for (wptr = wlist; wptr != NULL; wptr = wptr->next) {
58                 len = len + strlen(wptr->w_data);
59                 }
60
61         return(len);
62         }
63
64 void wDumpContent() {
65         struct webcontent *wptr;
66
67         printf("Content-type: text/html\n");
68         printf("Content-length: %d\n", wContentLength());
69         printf("\n");
70
71         while (wlist != NULL) {
72                 fwrite(wlist->w_data, strlen(wlist->w_data), 1, stdout);
73                 wptr = wlist->next;
74                 free(wlist);
75                 wlist = wptr;
76                 }
77         wlast = NULL;
78         }
79
80 void getz(char *buf) {
81         if (fgets(buf, 256, stdin) == NULL) strcpy(buf, "");
82         else {
83                 while ((strlen(buf)>0)&&(!isprint(buf[strlen(buf)-1])))
84                         buf[strlen(buf)-1] = 0;
85                 }
86         }
87
88 /*
89  * Output all that important stuff that the browser will want to see
90  */
91 void output_headers() {
92         printf("Server: %s\n", SERVER);
93         printf("Connection: close\n");
94         printf("Set-cookie: wc_session=%d\n", wc_session);
95         if (strlen(wc_host)>0) printf("Set-cookie: wc_host=%s\n", wc_host);
96         if (wc_port != 0) printf("Set-cookie: wc_port=%d\n", wc_port);
97         if (strlen(wc_username)>0) printf("Set-cookie: wc_username=%s\n",
98                 wc_username);
99         if (strlen(wc_password)>0) printf("Set-cookie: wc_password=%s\n",
100                 wc_password);
101         if (strlen(wc_roomname)>0) printf("Set-cookie: wc_roomname=%s\n",
102                 wc_roomname);
103         }
104
105 void output_static(char *what) {
106         char buf[256];
107         FILE *fp;
108         struct stat statbuf;
109         off_t bytes;
110
111         sprintf(buf, "static/%s", what);
112         fp = fopen(buf, "rb");
113         if (fp == NULL) {
114                 printf("HTTP/1.0 404 %s\n", strerror(errno));
115                 output_headers();
116                 printf("Content-Type: text/plain\n");
117                 sprintf(buf, "%s: %s\n", what, strerror(errno));
118                 printf("Content-length: %d\n", strlen(buf));
119                 printf("\n");
120                 fwrite(buf, strlen(buf), 1, stdout);
121                 }
122         else {
123                 printf("HTTP/1.0 200 OK\n");
124                 output_headers();
125
126                 if (!strncasecmp(&what[strlen(what)-4], ".gif", 4))
127                         printf("Content-type: image/gif\n");
128                 else
129                         printf("Content-type: junk/data\n");
130
131                 fstat(fileno(fp), &statbuf);
132                 bytes = statbuf.st_size;
133                 printf("Content-length: %d\n", bytes);
134                 printf("\n");
135                 while (bytes--) {
136                         putc(getc(fp), stdout);
137                         }
138                 fclose(fp);
139                 }
140         }
141
142
143 void session_loop() {
144         char cmd[256];
145         char buf[256];
146         int a;
147
148         getz(cmd);
149         fprintf(stderr, "Cmd: %s\n", cmd);
150         fflush(stderr);
151
152         do {
153                 getz(buf);
154                 } while(strlen(buf)>0);
155
156         ++TransactionCount;
157
158         if (!strncasecmp(cmd, "GET /static/", 12)) {
159                 strcpy(buf, &cmd[12]);
160                 for (a=0; a<strlen(buf); ++a) if (isspace(buf[a])) buf[a]=0;
161                 output_static(buf);
162                 }
163
164         else if (!strncasecmp(cmd, "GET /demographics", 17)) {
165                 printf("HTTP/1.0 200 OK\n");
166                 output_headers();
167
168                 wprintf("<HTML><HEAD><TITLE>Stuff</TITLE></HEAD><BODY>\n");
169                 wprintf("It's time to include an image...\n");
170                 wprintf("<IMG SRC=\"/static/netscape.gif\">\n");
171                 wprintf("...in the page.</BODY></HTML>\n");
172                 wDumpContent();
173                 }
174
175         else {
176                 printf("HTTP/1.0 200 OK\n");
177                 output_headers();
178         
179                 wprintf("<HTML><HEAD><TITLE>WebCit</TITLE></HEAD><BODY>\n");
180                 wprintf("TransactionCount is %d<HR>\n", TransactionCount);
181                 wprintf("You're in session %d<BR>\n", wc_session);
182                 wprintf("</BODY></HTML>\n");
183                 wDumpContent();
184                 }
185
186         fflush(stdout);
187         }
188
189
190
191 int main(int argc, char *argv[]) {
192
193         if (argc != 2) {
194                 printf("%s: usage: %s <session_id>\n", argv[0], argv[0]);
195                 exit(1);
196                 }
197
198         wc_session = atoi(argv[1]);
199         strcpy(wc_host, "");
200         wc_port = 0;
201         strcpy(wc_username, "");
202         strcpy(wc_password, "");
203         strcpy(wc_roomname, "");
204
205         while (1) {
206                 session_loop();
207                 }
208
209         exit(0);
210         }