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