]> code.citadel.org Git - citadel.git/blob - webcit/webcit.c
started on the static loader stuff
[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
16 int wc_session;
17 char wc_host[256];
18 int wc_port;
19 char wc_username[256];
20 char wc_password[256];
21 char wc_roomname[256];
22
23 void getz(char *buf) {
24         if (fgets(buf, 256, stdin) == NULL) strcpy(buf, "");
25         else {
26                 while ((strlen(buf)>0)&&(!isprint(buf[strlen(buf)-1])))
27                         buf[strlen(buf)-1] = 0;
28                 }
29         }
30
31 void output_reconnect_cookies() {
32         printf("Set-cookie: wc_session=%d\n", wc_session);
33         if (strlen(wc_host)>0) printf("Set-cookie: wc_host=%s\n", wc_host);
34         if (wc_port != 0) printf("Set-cookie: wc_port=%d\n", wc_port);
35         if (strlen(wc_username)>0) printf("Set-cookie: wc_username=%s\n",
36                 wc_username);
37         if (strlen(wc_password)>0) printf("Set-cookie: wc_password=%s\n",
38                 wc_password);
39         if (strlen(wc_roomname)>0) printf("Set-cookie: wc_roomname=%s\n",
40                 wc_roomname);
41         }
42
43 void output_static(char *what) {
44         char buf[256];
45         FILE *fp;
46
47         sprintf(buf, "static/%s", what);
48         fp = fopen(buf, "rb");
49         if (fp == NULL) {
50                 printf("HTTP/1.0 404 %s\n", strerror(errno));
51                 printf("Server: WebCit v2 (Velma)\n");
52                 printf("Connection: close\n");
53                 output_reconnect_cookies();
54                 printf("Content-Type: text/plain\n");
55                 sprintf(buf, "%s: %s\n", what, strerror(errno));
56                 printf("Content-length: %d\n", strlen(buf));
57                 printf("\n");
58                 fwrite(buf, strlen(buf), 1, stdout);
59                 }
60         else {
61                 printf("HTTP/1.0 200 OK\n");
62                 printf("Server: WebCit v2 (Velma)\n");
63                 printf("Connection: close\n");
64                 output_reconnect_cookies();
65                 printf("Content-Type: text/plain\n");
66                 printf("Content-length: 11\n");
67                 printf("\n");
68                 printf("Hi from IG\n");
69                 fclose(fp);
70                 }
71         }
72
73
74 void session_loop() {
75         char cmd[256];
76         char buf[256];
77         char content[4096];
78         static int TransactionCount = 0;
79         int a;
80
81         getz(cmd);
82         do {
83                 getz(buf);
84                 } while(strlen(buf)>0);
85
86         fprintf(stderr, "Command: %s\n", cmd);
87         fflush(stderr);
88
89         if (!strncasecmp(cmd, "GET /static/", 12)) {
90                 strcpy(buf, &cmd[12]);
91                 for (a=0; a<strlen(buf); ++a) if (isspace(buf[a])) buf[a]=0;
92                 output_static(buf);
93                 }
94
95         else {
96                 printf("HTTP/1.0 200 OK\n");
97                 printf("Server: WebCit v2 (Velma)\n");
98                 printf("Connection: close\n");
99                 output_reconnect_cookies();
100                 printf("Content-Type: text/html\n");
101         
102                 strcpy(content, "");
103         
104                 sprintf(&content[strlen(content)],
105                         "<HTML><HEAD><TITLE>WebCit</TITLE></HEAD><BODY>\n");
106                 sprintf(&content[strlen(content)],
107                         "TransactionCount is %d<HR>\n", ++TransactionCount);
108                 sprintf(&content[strlen(content)],
109                         "You're in session %d<BR>\n", wc_session);
110                 sprintf(&content[strlen(content)],
111                         "</BODY></HTML>\n");
112         
113                 printf("Content-length: %d\n", strlen(content));
114                 printf("\n");
115                 fwrite(content, strlen(content), 1, stdout);
116                 }
117
118         fflush(stdout);
119         }
120
121
122
123 int main(int argc, char *argv[]) {
124
125         if (argc != 2) {
126                 printf("%s: usage: %s <session_id>\n", argv[0], argv[0]);
127                 exit(1);
128                 }
129
130         wc_session = atoi(argv[1]);
131         strcpy(wc_host, "");
132         wc_port = 0;
133         strcpy(wc_username, "");
134         strcpy(wc_password, "");
135         strcpy(wc_roomname, "");
136
137         while (1) {
138                 session_loop();
139                 }
140
141         exit(0);
142         }