Began making changes to do better handling of character sets.
[citadel.git] / webcit / groupdav_main.c
1 /*
2  * $Id$
3  *
4  * Entry point for GroupDAV functions
5  *
6  */
7
8 #include "webcit.h"
9 #include "webserver.h"
10 #include "groupdav.h"
11
12
13 /*
14  * Output HTTP headers which are common to all requests.
15  *
16  * Please observe that we don't use the usual output_headers()
17  * and wDumpContent() functions in the GroupDAV subsystem, so we
18  * do our own header stuff here.
19  *
20  */
21 void groupdav_common_headers(void) {
22         wprintf(
23                 "Server: %s / %s\r\n"
24                 "Connection: close\r\n",
25                 SERVER, serv_info.serv_software
26         );
27 }
28
29
30
31 /*
32  * string conversion function
33  */
34 void euid_escapize(char *target, char *source) {
35         int i;
36         int target_length = 0;
37
38         strcpy(target, "");
39         for (i=0; i<strlen(source); ++i) {
40                 if ( (isalnum(source[i])) || (source[i]=='-') || (source[i]=='_') ) {
41                         target[target_length] = source[i];
42                         target[++target_length] = 0;
43                 }
44                 else {
45                         sprintf(&target[target_length], "=%02X", source[i]);
46                         target_length += 3;
47                 }
48         }
49 }
50
51 /*
52  * string conversion function
53  */
54 void euid_unescapize(char *target, char *source) {
55         int a, b;
56         char hex[3];
57         int target_length = 0;
58
59         strcpy(target, "");
60
61         for (a = 0; a < strlen(source); ++a) {
62                 if (source[a] == '=') {
63                         hex[0] = source[a + 1];
64                         hex[1] = source[a + 2];
65                         hex[2] = 0;
66                         b = 0;
67                         sscanf(hex, "%02x", &b);
68                         target[target_length] = b;
69                         target[++target_length] = 0;
70                         a += 2;
71                 }
72                 else {
73                         target[target_length] = source[a];
74                         target[++target_length] = 0;
75                 }
76         }
77 }
78
79
80
81
82 /*
83  * Main entry point for GroupDAV requests
84  */
85 void groupdav_main(struct httprequest *req,
86                         char *dav_content_type,
87                         int dav_content_length,
88                         char *dav_content
89 ) {
90         struct httprequest *rptr;
91         char dav_method[256];
92         char dav_pathname[256];
93         char dav_ifmatch[256];
94         int dav_depth;
95         char *ds;
96         int i;
97
98         strcpy(dav_method, "");
99         strcpy(dav_pathname, "");
100         strcpy(dav_ifmatch, "");
101         dav_depth = 0;
102
103         for (rptr=req; rptr!=NULL; rptr=rptr->next) {
104                 if (!strncasecmp(rptr->line, "Host: ", 6)) {
105                         if (strlen(WC->http_host) == 0) {
106                                 safestrncpy(WC->http_host, &rptr->line[6],
107                                         sizeof WC->http_host);
108                         }
109                 }
110                 if (!strncasecmp(rptr->line, "If-Match: ", 10)) {
111                         safestrncpy(dav_ifmatch, &rptr->line[10],
112                                 sizeof dav_ifmatch);
113                 }
114                 if (!strncasecmp(rptr->line, "Depth: ", 7)) {
115                         if (!strcasecmp(&rptr->line[7], "infinity")) {
116                                 dav_depth = 32767;
117                         }
118                         else if (!strcmp(&rptr->line[7], "0")) {
119                                 dav_depth = 0;
120                         }
121                         else if (!strcmp(&rptr->line[7], "1")) {
122                                 dav_depth = 1;
123                         }
124                 }
125         }
126
127         if (!WC->logged_in) {
128                 wprintf("HTTP/1.1 401 Unauthorized\r\n");
129                 groupdav_common_headers();
130                 wprintf("WWW-Authenticate: Basic realm=\"%s\"\r\n",
131                         serv_info.serv_humannode);
132                 wprintf("Content-Length: 0\r\n\r\n");
133                 return;
134         }
135
136         extract_token(dav_method, req->line, 0, ' ', sizeof dav_method);
137         extract_token(dav_pathname, req->line, 1, ' ', sizeof dav_pathname);
138         unescape_input(dav_pathname);
139
140         /* If the request does not begin with "/groupdav", prepend it.  If
141          * we happen to introduce a double-slash, that's ok; we'll strip it
142          * in the next step.
143          * 
144          * (THIS IS DISABLED BECAUSE WE ARE NOW TRYING TO DO REAL DAV.)
145          *
146         if (strncasecmp(dav_pathname, "/groupdav", 9)) {
147                 char buf[512];
148                 snprintf(buf, sizeof buf, "/groupdav/%s", dav_pathname);
149                 safestrncpy(dav_pathname, buf, sizeof dav_pathname);
150         }
151          *
152          */
153         
154         /* Remove any stray double-slashes in pathname */
155         while (ds=strstr(dav_pathname, "//"), ds != NULL) {
156                 strcpy(ds, ds+1);
157         }
158
159         /*
160          * If there's an If-Match: header, strip out the quotes if present, and
161          * then if all that's left is an asterisk, make it go away entirely.
162          */
163         if (strlen(dav_ifmatch) > 0) {
164                 striplt(dav_ifmatch);
165                 if (dav_ifmatch[0] == '\"') {
166                         strcpy(dav_ifmatch, &dav_ifmatch[1]);
167                         for (i=0; i<strlen(dav_ifmatch); ++i) {
168                                 if (dav_ifmatch[i] == '\"') {
169                                         dav_ifmatch[i] = 0;
170                                 }
171                         }
172                 }
173                 if (!strcmp(dav_ifmatch, "*")) {
174                         strcpy(dav_ifmatch, "");
175                 }
176         }
177
178         /*
179          * The OPTIONS method is not required by GroupDAV.  This is an
180          * experiment to determine what might be involved in supporting
181          * other variants of DAV in the future.
182          */
183         if (!strcasecmp(dav_method, "OPTIONS")) {
184                 groupdav_options(dav_pathname);
185                 return;
186         }
187
188         /*
189          * The PROPFIND method is basically used to list all objects in a
190          * room, or to list all relevant rooms on the server.
191          */
192         if (!strcasecmp(dav_method, "PROPFIND")) {
193                 groupdav_propfind(dav_pathname, dav_depth,
194                                 dav_content_type, dav_content);
195                 return;
196         }
197
198         /*
199          * The GET method is used for fetching individual items.
200          */
201         if (!strcasecmp(dav_method, "GET")) {
202                 groupdav_get(dav_pathname);
203                 return;
204         }
205
206         /*
207          * The PUT method is used to add or modify items.
208          */
209         if (!strcasecmp(dav_method, "PUT")) {
210                 groupdav_put(dav_pathname, dav_ifmatch,
211                                 dav_content_type, dav_content,
212                                 dav_content_length);
213                 return;
214         }
215
216         /*
217          * The DELETE method kills, maims, and destroys.
218          */
219         if (!strcasecmp(dav_method, "DELETE")) {
220                 groupdav_delete(dav_pathname, dav_ifmatch);
221                 return;
222         }
223
224         /*
225          * Couldn't find what we were looking for.  Die in a car fire.
226          */
227         wprintf("HTTP/1.1 501 Method not implemented\r\n");
228         groupdav_common_headers();
229         wprintf("Content-Type: text/plain\r\n"
230                 "\r\n"
231                 "GroupDAV method \"%s\" is not implemented.\r\n",
232                 dav_method
233         );
234 }
235
236
237 /*
238  * Output our host prefix for globally absolute URL's.
239  */  
240 void groupdav_identify_host(void) {
241         if (strlen(WC->http_host) > 0) {
242                 wprintf("%s://%s",
243                         (is_https ? "https" : "http"),
244                         WC->http_host);
245         }
246 }