Remove $Id$ tags from most of webcit
[citadel.git] / webcit / groupdav_main.c
1 /*
2  * Entry point for GroupDAV functions
3  *
4  * Copyright (c) 2005-2010 by the citadel.org team
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "webcit.h"
22 #include "webserver.h"
23 #include "groupdav.h"
24
25 extern HashList *HandlerHash;
26
27 HashList *DavNamespaces = NULL;
28
29 /*
30  * Output HTTP headers which are common to all requests.
31  *
32  * Please observe that we don't use the usual output_headers()
33  * and wDumpContent() functions in the GroupDAV subsystem, so we
34  * do our own header stuff here.
35  *
36  */
37 void groupdav_common_headers(void) {
38         hprintf(
39                 "Server: %s / %s\r\n"
40                 "Connection: close\r\n",
41                 PACKAGE_STRING, ChrPtr(WC->serv_info->serv_software)
42         );
43 }
44
45
46
47 /*
48  * string conversion function
49  */
50 void euid_escapize(char *target, const char *source) {
51         int i, len;
52         int target_length = 0;
53
54         strcpy(target, "");
55         len = strlen(source);
56         for (i=0; i<len; ++i) {
57                 if ( (isalnum(source[i])) || (source[i]=='-') || (source[i]=='_') ) {
58                         target[target_length] = source[i];
59                         target[++target_length] = 0;
60                 }
61                 else {
62                         sprintf(&target[target_length], "=%02X", (0xFF & source[i]));
63                         target_length += 3;
64                 }
65         }
66 }
67
68 /*
69  * string conversion function
70  */
71 void euid_unescapize(char *target, const char *source) {
72         int a, b, len;
73         char hex[3];
74         int target_length = 0;
75
76         strcpy(target, "");
77
78         len = strlen(source);
79         for (a = 0; a < len; ++a) {
80                 if (source[a] == '=') {
81                         hex[0] = source[a + 1];
82                         hex[1] = source[a + 2];
83                         hex[2] = 0;
84                         b = 0;
85                         b = decode_hex(hex);
86                         target[target_length] = b;
87                         target[++target_length] = 0;
88                         a += 2;
89                 }
90                 else {
91                         target[target_length] = source[a];
92                         target[++target_length] = 0;
93                 }
94         }
95 }
96
97
98
99
100 /*
101  * Main entry point for GroupDAV requests
102  */
103 void groupdav_main(void)
104 {
105         wcsession *WCC = WC;
106         int i, len;
107
108         StrBufUnescape(WCC->Hdr->HR.ReqLine, 0);
109
110         StrBufStripSlashes(WCC->Hdr->HR.ReqLine, 0);
111
112         /*
113          * If there's an If-Match: header, strip out the quotes if present, and
114          * then if all that's left is an asterisk, make it go away entirely.
115          */
116         len = StrLength(WCC->Hdr->HR.dav_ifmatch);
117         if (len > 0) {
118                 StrBufTrim(WCC->Hdr->HR.dav_ifmatch);
119                 if (ChrPtr(WCC->Hdr->HR.dav_ifmatch)[0] == '\"') {
120                         StrBufCutLeft(WCC->Hdr->HR.dav_ifmatch, 1);
121                         len --;
122                         for (i=0; i<len; ++i) {
123                                 if (ChrPtr(WCC->Hdr->HR.dav_ifmatch)[i] == '\"') {
124                                         StrBufCutAt(WCC->Hdr->HR.dav_ifmatch, i, NULL);
125                                         len = StrLength(WCC->Hdr->HR.dav_ifmatch);
126                                 }
127                         }
128                 }
129                 if (!strcmp(ChrPtr(WCC->Hdr->HR.dav_ifmatch), "*")) {
130                         FlushStrBuf(WCC->Hdr->HR.dav_ifmatch);
131                 }
132         }
133
134         switch (WCC->Hdr->HR.eReqType)
135         {
136         /*
137          * The OPTIONS method is not required by GroupDAV.  This is an
138          * experiment to determine what might be involved in supporting
139          * other variants of DAV in the future.
140          */
141         case eOPTIONS:
142                 groupdav_options();
143                 break;
144
145
146         /*
147          * The PROPFIND method is basically used to list all objects in a
148          * room, or to list all relevant rooms on the server.
149          */
150         case ePROPFIND:
151                 groupdav_propfind();
152                 break;
153
154         /*
155          * The GET method is used for fetching individual items.
156          */
157         case eGET:
158                 groupdav_get();
159                 break;
160         
161         /*
162          * The PUT method is used to add or modify items.
163          */
164         case ePUT:
165                 groupdav_put();
166                 break;
167         
168         /*
169          * The DELETE method kills, maims, and destroys.
170          */
171         case eDELETE:
172                 groupdav_delete();
173                 break;
174         default:
175
176         /*
177          * Couldn't find what we were looking for.  Die in a car fire.
178          */
179                 hprintf("HTTP/1.1 501 Method not implemented\r\n");
180                 groupdav_common_headers();
181                 hprintf("Content-Type: text/plain\r\n");
182                 wc_printf("GroupDAV method \"%s\" is not implemented.\r\n",
183                         ReqStrs[WCC->Hdr->HR.eReqType]);
184                 end_burst();
185         }
186 }
187
188
189 /*
190  * Output our host prefix for globally absolute URL's.
191  */  
192 void groupdav_identify_host(void) {
193         wcsession *WCC = WC;
194
195         if (StrLength(WCC->Hdr->HR.http_host)!=0) {
196                 wc_printf("%s://%s",
197                         (is_https ? "https" : "http"),
198                         ChrPtr(WCC->Hdr->HR.http_host));
199         }
200 }
201
202
203 void tmplput_GROUPDAV_HOSTNAME(StrBuf *Target, WCTemplputParams *TP) 
204 {
205         wcsession *WCC = WC;
206
207         if (StrLength(WCC->Hdr->HR.http_host)!=0) {
208                 StrBufAppendPrintf(Target, 
209                                    "%s://%s",
210                                    (is_https ? "https" : "http"),
211                                    ChrPtr(WCC->Hdr->HR.http_host));
212         }
213 }
214
215 /*
216  * Output our host prefix for globally absolute URL's.
217  */  
218 void groupdav_identify_hosthdr(void) {
219         wcsession *WCC = WC;
220
221         if (StrLength(WCC->Hdr->HR.http_host)!=0) {
222                 hprintf("%s://%s",
223                         (is_https ? "https" : "http"),
224                         ChrPtr(WCC->Hdr->HR.http_host));
225         }
226 }
227
228
229 void Header_HandleIfMatch(StrBuf *Line, ParsedHttpHdrs *hdr)
230 {
231         hdr->HR.dav_ifmatch = Line;
232 }
233         
234 void Header_HandleDepth(StrBuf *Line, ParsedHttpHdrs *hdr)
235 {
236         if (!strcasecmp(ChrPtr(Line), "infinity")) {
237                 hdr->HR.dav_depth = 32767;
238         }
239         else if (strcmp(ChrPtr(Line), "0") == 0) {
240                 hdr->HR.dav_depth = 0;
241         }
242         else if (strcmp(ChrPtr(Line), "1") == 0) {
243                 hdr->HR.dav_depth = 1;
244         }
245 }
246 int Conditional_DAV_DEPTH(StrBuf *Target, WCTemplputParams *TP)
247 {
248         return WC->Hdr->HR.dav_depth == GetTemplateTokenNumber(Target, TP, 2, 0);
249 }
250
251
252 void RegisterDAVNamespace(const char * UrlString, 
253                           long UrlSLen, 
254                           const char *DisplayName, 
255                           long dslen, 
256                           WebcitHandlerFunc F, 
257                           WebcitRESTDispatchID RID,
258                           long Flags)
259 {
260         void *vHandler;
261
262         /* first put it in... */
263         WebcitAddUrlHandler(UrlString, UrlSLen, DisplayName, dslen, F, Flags|PARSE_REST_URL);
264         /* get it out again... */
265         GetHash(HandlerHash, UrlString, UrlSLen, &vHandler);
266         ((WebcitHandler*)vHandler)->RID = RID;
267         /* and keep a copy of it, so we can compare it later */
268         Put(DavNamespaces, UrlString, UrlSLen, vHandler, reference_free_handler);
269 }
270
271 int Conditional_DAV_NS(StrBuf *Target, WCTemplputParams *TP)
272 {
273         wcsession *WCC = WC;
274         void *vHandler;
275         const char *NS;
276         long NSLen;
277
278         GetTemplateTokenString(NULL, TP, 2, &NS, &NSLen);
279         GetHash(HandlerHash, NS, NSLen, &vHandler);
280         return WCC->Hdr->HR.Handler == vHandler;
281 }
282
283
284 int Conditional_DAV_NSCURRENT(StrBuf *Target, WCTemplputParams *TP)
285 {
286         wcsession *WCC = WC;
287         void *vHandler;
288
289         vHandler = CTX;
290         return WCC->Hdr->HR.Handler == vHandler;
291 }
292
293 void tmplput_DAV_NAMESPACE(StrBuf *Target, WCTemplputParams *TP)
294 {
295         wcsession *WCC = WC;
296
297         if (TP->Filter.ContextType == CTX_DAVNS) {
298                 WebcitHandler *H;
299                 H = (WebcitHandler*) CTX;
300                 StrBufAppendTemplate(Target, TP, H->Name, 0);
301         }
302         else if (WCC->Hdr->HR.Handler != NULL) {
303                 StrBufAppendTemplate(Target, TP, WCC->Hdr->HR.Handler->Name, 0);
304         }
305 }
306
307 int GroupdavDispatchREST(RESTDispatchID WhichAction, int IgnoreFloor)
308 {
309         wcsession *WCC = WC;
310         void *vDir;
311         
312         switch(WhichAction){
313         case ExistsID:
314                 GetHash(WCC->Directory, IKEY(WCC->ThisRoom->nRoomNameParts + 1), &vDir);
315                 return locate_message_by_uid(ChrPtr((StrBuf*)vDir)) != -1;
316                 /* TODO: remember euid */
317         case PutID:
318         case DeleteID:
319                 break;
320
321
322         }
323         return 0;
324 }
325
326
327 void
328 ServerStartModule_DAV
329 (void)
330 {
331
332         DavNamespaces = NewHash(1, NULL);
333
334 }
335
336 void 
337 ServerShutdownModule_DAV
338 (void)
339 {
340         DeleteHash(&DavNamespaces);
341 }
342
343
344
345
346 void 
347 InitModule_GROUPDAV
348 (void)
349 {
350 //      WebcitAddUrlHandler(HKEY("groupdav"), "", 0, groupdav_main, XHTTP_COMMANDS|COOKIEUNNEEDED|FORCE_SESSIONCLOSE);
351         RegisterDAVNamespace(HKEY("groupdav"), HKEY("GroupDAV"), 
352                              groupdav_main, GroupdavDispatchREST, 
353                              XHTTP_COMMANDS|COOKIEUNNEEDED|FORCE_SESSIONCLOSE);
354
355         RegisterNamespace("DAV:HOSTNAME", 0, 0, tmplput_GROUPDAV_HOSTNAME, NULL, CTX_NONE);
356
357         RegisterConditional(HKEY("COND:DAV:NS"), 0, Conditional_DAV_NS,  CTX_NONE);
358
359         RegisterIterator("DAV:NS", 0, DavNamespaces, NULL, 
360                          NULL, NULL, CTX_DAVNS, CTX_NONE, IT_NOFLAG);
361
362
363         RegisterConditional(HKEY("COND:DAV:NSCURRENT"), 0, Conditional_DAV_NSCURRENT,  CTX_DAVNS);
364         RegisterNamespace("DAV:NAMESPACE", 0, 1, tmplput_DAV_NAMESPACE, NULL, CTX_NONE);
365
366         RegisterHeaderHandler(HKEY("IF-MATCH"), Header_HandleIfMatch);
367         RegisterHeaderHandler(HKEY("DEPTH"), Header_HandleDepth);
368         RegisterConditional(HKEY("COND:DAV:DEPTH"), 1, Conditional_DAV_DEPTH,  CTX_NONE);
369
370 }