2 * Entry point for GroupDAV functions
4 * Copyright (c) 2005-2011 by the citadel.org team
6 * This program is open source 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.
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.
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
22 #include "webserver.h"
25 extern HashList *HandlerHash;
27 HashList *DavNamespaces = NULL;
30 * Output HTTP headers which are common to all requests.
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.
37 void dav_common_headers(void) {
40 "Connection: close\r\n",
41 PACKAGE_STRING, ChrPtr(WC->serv_info->serv_software)
48 * string conversion function
50 void euid_escapize(char *target, const char *source) {
52 int target_length = 0;
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;
62 sprintf(&target[target_length], "=%02X", (0xFF & source[i]));
69 * string conversion function
71 void euid_unescapize(char *target, const char *source) {
74 int target_length = 0;
79 for (a = 0; a < len; ++a) {
80 if (source[a] == '=') {
81 hex[0] = source[a + 1];
82 hex[1] = source[a + 2];
86 target[target_length] = b;
87 target[++target_length] = 0;
91 target[target_length] = source[a];
92 target[++target_length] = 0;
101 * Main entry point for GroupDAV requests
108 syslog(LOG_DEBUG, "dav_main() called, logged_in=%d", WCC->logged_in );
110 StrBufUnescape(WCC->Hdr->HR.ReqLine, 0);
111 StrBufStripSlashes(WCC->Hdr->HR.ReqLine, 0);
114 * If there's an If-Match: header, strip out the quotes if present, and
115 * then if all that's left is an asterisk, make it go away entirely.
117 len = StrLength(WCC->Hdr->HR.dav_ifmatch);
119 StrBufTrim(WCC->Hdr->HR.dav_ifmatch);
120 if (ChrPtr(WCC->Hdr->HR.dav_ifmatch)[0] == '\"') {
121 StrBufCutLeft(WCC->Hdr->HR.dav_ifmatch, 1);
123 for (i=0; i<len; ++i) {
124 if (ChrPtr(WCC->Hdr->HR.dav_ifmatch)[i] == '\"') {
125 StrBufCutAt(WCC->Hdr->HR.dav_ifmatch, i, NULL);
126 len = StrLength(WCC->Hdr->HR.dav_ifmatch);
130 if (!strcmp(ChrPtr(WCC->Hdr->HR.dav_ifmatch), "*")) {
131 FlushStrBuf(WCC->Hdr->HR.dav_ifmatch);
135 switch (WCC->Hdr->HR.eReqType)
138 * The OPTIONS method is not required by GroupDAV but it will be
139 * needed for future implementations of other DAV-based protocols.
146 * The PROPFIND method is basically used to list all objects in a
147 * room, or to list all relevant rooms on the server.
154 * The GET method is used for fetching individual items.
161 * The PUT method is used to add or modify items.
168 * The DELETE method kills, maims, and destroys.
176 * Couldn't find what we were looking for. Die in a car fire.
178 hprintf("HTTP/1.1 501 Method not implemented\r\n");
179 dav_common_headers();
180 hprintf("Content-Type: text/plain\r\n");
181 wc_printf("GroupDAV method \"%s\" is not implemented.\r\n",
182 ReqStrs[WCC->Hdr->HR.eReqType]);
189 * Output our host prefix for globally absolute URL's.
191 void dav_identify_host(void) {
192 wc_printf("%s", ChrPtr(site_prefix));
196 void tmplput_dav_HOSTNAME(StrBuf *Target, WCTemplputParams *TP)
198 StrBufAppendPrintf(Target, "%s", ChrPtr(site_prefix));
202 * Output our host prefix for globally absolute URL's.
204 void dav_identify_hosthdr(void) {
205 hprintf("%s", ChrPtr(site_prefix));
209 void Header_HandleIfMatch(StrBuf *Line, ParsedHttpHdrs *hdr)
211 hdr->HR.dav_ifmatch = Line;
215 void Header_HandleDepth(StrBuf *Line, ParsedHttpHdrs *hdr)
217 if (!strcasecmp(ChrPtr(Line), "infinity")) {
218 hdr->HR.dav_depth = 32767;
220 else if (strcmp(ChrPtr(Line), "0") == 0) {
221 hdr->HR.dav_depth = 0;
223 else if (strcmp(ChrPtr(Line), "1") == 0) {
224 hdr->HR.dav_depth = 1;
229 int Conditional_DAV_DEPTH(StrBuf *Target, WCTemplputParams *TP)
231 return WC->Hdr->HR.dav_depth == GetTemplateTokenNumber(Target, TP, 2, 0);
235 void RegisterDAVNamespace(const char * UrlString,
237 const char *DisplayName,
240 WebcitRESTDispatchID RID,
245 /* first put it in... */
246 WebcitAddUrlHandler(UrlString, UrlSLen, DisplayName, dslen, F, Flags|PARSE_REST_URL);
247 /* get it out again... */
248 GetHash(HandlerHash, UrlString, UrlSLen, &vHandler);
249 ((WebcitHandler*)vHandler)->RID = RID;
250 /* and keep a copy of it, so we can compare it later */
251 Put(DavNamespaces, UrlString, UrlSLen, vHandler, reference_free_handler);
255 int Conditional_DAV_NS(StrBuf *Target, WCTemplputParams *TP)
262 GetTemplateTokenString(NULL, TP, 2, &NS, &NSLen);
263 GetHash(HandlerHash, NS, NSLen, &vHandler);
264 return WCC->Hdr->HR.Handler == vHandler;
268 int Conditional_DAV_NSCURRENT(StrBuf *Target, WCTemplputParams *TP)
274 return WCC->Hdr->HR.Handler == vHandler;
278 void tmplput_DAV_NAMESPACE(StrBuf *Target, WCTemplputParams *TP)
282 if (TP->Filter.ContextType == CTX_DAVNS) {
284 H = (WebcitHandler*) CTX;
285 StrBufAppendTemplate(Target, TP, H->Name, 0);
287 else if (WCC->Hdr->HR.Handler != NULL) {
288 StrBufAppendTemplate(Target, TP, WCC->Hdr->HR.Handler->Name, 0);
293 int GroupdavDispatchREST(RESTDispatchID WhichAction, int IgnoreFloor)
300 GetHash(WCC->Directory, IKEY(WCC->ThisRoom->nRoomNameParts + 1), &vDir);
301 return locate_message_by_uid(ChrPtr((StrBuf*)vDir)) != -1;
302 /* TODO: remember euid */
314 ServerStartModule_DAV
318 DavNamespaces = NewHash(1, NULL);
323 ServerShutdownModule_DAV
326 DeleteHash(&DavNamespaces);
334 RegisterDAVNamespace(HKEY("groupdav"), HKEY("GroupDAV"),
335 dav_main, GroupdavDispatchREST,
336 XHTTP_COMMANDS|COOKIEUNNEEDED|FORCE_SESSIONCLOSE
339 RegisterNamespace("DAV:HOSTNAME", 0, 0, tmplput_dav_HOSTNAME, NULL, CTX_NONE);
341 RegisterConditional(HKEY("COND:DAV:NS"), 0, Conditional_DAV_NS, CTX_NONE);
343 RegisterIterator("DAV:NS", 0, DavNamespaces, NULL,
344 NULL, NULL, CTX_DAVNS, CTX_NONE, IT_NOFLAG
347 RegisterConditional(HKEY("COND:DAV:NSCURRENT"), 0, Conditional_DAV_NSCURRENT, CTX_DAVNS);
348 RegisterNamespace("DAV:NAMESPACE", 0, 1, tmplput_DAV_NAMESPACE, NULL, CTX_NONE);
350 RegisterHeaderHandler(HKEY("IF-MATCH"), Header_HandleIfMatch);
351 RegisterHeaderHandler(HKEY("DEPTH"), Header_HandleDepth);
352 RegisterConditional(HKEY("COND:DAV:DEPTH"), 1, Conditional_DAV_DEPTH, CTX_NONE);