2 * Handles GroupDAV and CalDAV PROPFIND requests.
4 * A few notes about our XML output:
6 * --> Yes, we are spewing tags directly instead of using an XML library.
7 * Whining about it will be summarily ignored.
9 * --> XML is deliberately output with no whitespace/newlines between tags.
10 * This makes it difficult to read, but we have discovered clients which
11 * crash when you try to pretty it up.
14 * http://www.ietf.org/rfc/rfc4791.txt
15 * http://blogs.nologin.es/rickyepoderi/index.php?/archives/14-Introducing-CalDAV-Part-I.html
19 PROPFIND /groupdav/calendar/ HTTP/1.1
20 Content-type: text/xml; charset=utf-8
23 <?xml version="1.0" encoding="UTF-8"?>
24 <D:propfind xmlns:D="DAV:">
33 * Copyright (c) 2005-2012 by the citadel.org team
35 * This program is open source software; you can redistribute it and/or
36 * modify it under the terms of the GNU General Public License version 3.
38 * This program is distributed in the hope that it will be useful,
39 * but WITHOUT ANY WARRANTY; without even the implied warranty of
40 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
41 * GNU General Public License for more details.
45 #include "webserver.h"
49 * Given an encoded UID, translate that to an unencoded Citadel EUID and
50 * then search for it in the current room. Return a message number or -1
54 long locate_message_by_uid(const char *uid) {
56 char decoded_uid[1024];
60 euid_unescapize(decoded_uid, uid);
62 /* ask Citadel if we have this one */
63 serv_printf("EUID %s", decoded_uid);
64 serv_getln(buf, sizeof buf);
66 retval = atol(&buf[4]);
74 * IgnoreFloor: set to 0 or 1 _nothing else_
75 * Subfolders: direct child floors will be put here.
77 const folder *GetRESTFolder(int IgnoreFloor, HashList *Subfolders)
81 const folder *ThisFolder = NULL;
82 const folder *FoundFolder = NULL;
83 const folder *BestGuess = NULL;
90 int iRoom, jURL, urlp;
94 * Guess room: if the full URL matches a room, list thats it. We also need to remember direct sub rooms.
95 * if the URL is longer, we need to find the "best guess" so we can find the room we're in, and the rest
96 * of the URL will be uids and so on.
98 itfl = GetNewHashPos(WCC->Floors, 0);
99 urlp = GetCount(WCC->Directory);
101 while (GetNextHashPos(WCC->Floors, itfl, &len, &Key, &vFolder) &&
102 (ThisFolder == NULL))
104 ThisFolder = vFolder;
105 if (!IgnoreFloor && /* so we can handle legacy URLS... */
106 (ThisFolder->Floor != WCC->CurrentFloor))
109 if (ThisFolder->nRoomNameParts > 1)
111 /*TODO: is that number all right? */
112 // if (urlp - ThisFolder->nRoomNameParts != 2) {
113 // if (BestGuess != NULL)
116 // itd = GetNewHashPos(WCC->Directory, 0);
117 // GetNextHashPos(WCC->Directory, itd, &len, &Key, &vDir); //TODO: how many to fast forward?
119 itd = GetNewHashPos(WCC->Directory, 0);
120 GetNextHashPos(WCC->Directory, itd, &len, &Key, &vDir); //TODO: how many to fast forward?
122 for (iRoom = 0, /* Fast forward the floorname as we checked it above: */ jURL = IgnoreFloor;
124 (iRoom <= ThisFolder->nRoomNameParts) && (jURL <= urlp);
126 iRoom++, jURL++, GetNextHashPos(WCC->Directory, itd, &len, &Key, &vDir))
129 if (strcmp(ChrPtr(ThisFolder->RoomNameParts[iRoom]),
138 if ((iRoom == ThisFolder->nRoomNameParts) && (jURL == urlp))
140 FoundFolder = ThisFolder;
142 /* URL got more parts then this room, so we remember it for the best guess*/
143 else if ((jURL <= urlp) &&
144 (ThisFolder->nRoomNameParts <= nBestGuess))
146 BestGuess = ThisFolder;
147 nBestGuess = jURL - 1;
149 /* Room has more parts than the URL, it might be a sub-room? */
150 else if (iRoom <ThisFolder->nRoomNameParts)
151 {//// TODO: ThisFolder->nRoomNameParts == urlp - IgnoreFloor???
152 Put(Subfolders, SKEY(ThisFolder->name),
153 /* Cast away const, its a reference. */
154 (void*)ThisFolder, reference_free_handler);
158 delta = GetCount(WCC->Directory) - ThisFolder->nRoomNameParts;
159 if ((delta != 2) && (nBestGuess > 1))
162 itd = GetNewHashPos(WCC->Directory, 0);
164 if (!GetNextHashPos(WCC->Directory,
165 itd, &len, &Key, &vDir) ||
174 Dir = (StrBuf*) vDir;
175 if (strcmp(ChrPtr(ThisFolder->name),
184 DeleteHashPos(&itfl);
188 BestGuess = ThisFolder;
191 FoundFolder = ThisFolder;
195 /* TODO: Subfolders: remove patterns not matching the best guess or thisfolder */
196 DeleteHashPos(&itfl);
197 if (FoundFolder != NULL)
206 long GotoRestRoom(HashList *SubRooms)
208 int IgnoreFloor = 0; /* deprecated... */
212 const folder *ThisFolder;
214 State = REST_TOPLEVEL;
216 if (WCC->Hdr->HR.Handler != NULL)
217 State |= REST_IN_NAMESPACE;
219 Count = GetCount(WCC->Directory);
221 if (Count == 0) return State;
223 if (Count >= 1) State |=REST_IN_FLOOR;
224 if (Count == 1) return State;
227 * More than 3 params and no floor found?
228 * -> fall back to old non-floored notation
230 if ((Count >= 3) && (WCC->CurrentFloor == NULL))
235 State |= REST_IN_FLOOR;
237 ThisFolder = GetRESTFolder(IgnoreFloor, SubRooms);
238 if (ThisFolder != NULL)
240 if (WCC->ThisRoom != NULL)
241 if (CompareRooms(WCC->ThisRoom, ThisFolder) != 0)
242 gotoroom(ThisFolder->name);
243 State |= REST_IN_ROOM;
246 if (GetCount(SubRooms) > 0)
247 State |= REST_HAVE_SUB_ROOMS;
249 if ((WCC->ThisRoom != NULL) &&
250 (Count + IgnoreFloor > 3))
252 if (WCC->Hdr->HR.Handler->RID(ExistsID, IgnoreFloor))
254 State |= REST_GOT_LOCAL_PART;
257 /// WHOOPS, not there???
258 State |= REST_NONEXIST;
269 * List rooms (or "collections" in DAV terminology) which contain
270 * interesting groupware objects.
272 void dav_collection_list(void)
278 char datestring[256];
281 int is_groupware_collection = 0;
282 int starting_point = 1; /**< 0 for /, 1 for /groupdav/ */
284 if (WCC->Hdr->HR.Handler == NULL) {
287 else if (StrLength(WCC->Hdr->HR.ReqLine) == 0) {
295 http_datestring(datestring, sizeof datestring, now);
298 * Be rude. Completely ignore the XML request and simply send them
299 * everything we know about. Let the client sort it out.
301 hprintf("HTTP/1.0 207 Multi-Status\r\n");
302 dav_common_headers();
303 hprintf("Date: %s\r\n", datestring);
304 hprintf("Content-type: text/xml\r\n");
305 if (DisableGzip || (!WCC->Hdr->HR.gzip_ok))
306 hprintf("Content-encoding: identity\r\n");
310 wc_printf("<?xml version=\"1.0\" encoding=\"utf-8\"?>"
311 "<multistatus xmlns=\"DAV:\" xmlns:G=\"http://groupdav.org/\">"
315 * If the client is requesting the root, show a root node.
317 if (starting_point == 0) {
318 wc_printf("<response>");
322 wc_printf("</href>");
323 wc_printf("<propstat>");
324 wc_printf("<status>HTTP/1.1 200 OK</status>");
326 wc_printf("<displayname>/</displayname>");
327 wc_printf("<resourcetype><collection/></resourcetype>");
328 wc_printf("<getlastmodified>");
330 wc_printf("</getlastmodified>");
331 wc_printf("</prop>");
332 wc_printf("</propstat>");
333 wc_printf("</response>");
337 * If the client is requesting "/groupdav", show a /groupdav subdirectory.
339 if ((starting_point + WCC->Hdr->HR.dav_depth) >= 1) {
340 wc_printf("<response>");
343 wc_printf("/groupdav");
344 wc_printf("</href>");
345 wc_printf("<propstat>");
346 wc_printf("<status>HTTP/1.1 200 OK</status>");
348 wc_printf("<displayname>GroupDAV</displayname>");
349 wc_printf("<resourcetype><collection/></resourcetype>");
350 wc_printf("<getlastmodified>");
352 wc_printf("</getlastmodified>");
353 wc_printf("</prop>");
354 wc_printf("</propstat>");
355 wc_printf("</response>");
359 * Now go through the list and make it look like a DAV collection
362 serv_getln(buf, sizeof buf);
363 if (buf[0] == '1') while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
365 extract_token(roomname, buf, 0, '|', sizeof roomname);
366 view = extract_int(buf, 7);
367 mtime = extract_long(buf, 8);
368 http_datestring(datestring, sizeof datestring, mtime);
371 * For now, only list rooms that we know a GroupDAV client
372 * might be interested in. In the future we may add
375 * We determine the type of objects which are stored in each
376 * room by looking at the *default* view for the room. This
377 * allows, for example, a Calendar room to appear as a
378 * GroupDAV calendar even if the user has switched it to a
379 * Calendar List view.
381 if ( (view == VIEW_CALENDAR) ||
382 (view == VIEW_TASKS) ||
383 (view == VIEW_ADDRESSBOOK) ||
384 (view == VIEW_NOTES) ||
385 (view == VIEW_JOURNAL) ||
388 is_groupware_collection = 1;
391 is_groupware_collection = 0;
394 if ( (is_groupware_collection) && ((starting_point + WCC->Hdr->HR.dav_depth) >= 2) ) {
395 wc_printf("<response>");
399 wc_printf("/groupdav/");
400 urlescputs(roomname);
401 wc_printf("/</href>");
403 wc_printf("<propstat>");
404 wc_printf("<status>HTTP/1.1 200 OK</status>");
406 wc_printf("<displayname>");
408 wc_printf("</displayname>");
409 wc_printf("<resourcetype><collection/>");
413 wc_printf("<G:vevent-collection />");
416 wc_printf("<G:vtodo-collection />");
418 case VIEW_ADDRESSBOOK:
419 wc_printf("<G:vcard-collection />");
422 wc_printf("<G:vnotes-collection />");
425 wc_printf("<G:vjournal-collection />");
428 wc_printf("<G:wiki-collection />");
432 wc_printf("</resourcetype>");
433 wc_printf("<getlastmodified>");
435 wc_printf("</getlastmodified>");
436 wc_printf("</prop>");
437 wc_printf("</propstat>");
438 wc_printf("</response>");
441 wc_printf("</multistatus>\n");
447 void propfind_xml_start(void *data, const char *supplied_el, const char **attr) {
448 // syslog(LOG_DEBUG, "<%s>", supplied_el);
451 void propfind_xml_end(void *data, const char *supplied_el) {
452 // syslog(LOG_DEBUG, "</%s>", supplied_el);
458 * The pathname is always going to be /groupdav/room_name/msg_num
460 void dav_propfind(void)
463 StrBuf *dav_roomname;
467 long dav_msgnum = (-1);
469 char encoded_uid[256];
473 char datestring[256];
477 http_datestring(datestring, sizeof datestring, now);
479 int parse_success = 0;
480 XML_Parser xp = XML_ParserCreateNS(NULL, '|');
482 // XML_SetUserData(xp, XXX);
483 XML_SetElementHandler(xp, propfind_xml_start, propfind_xml_end);
484 // XML_SetCharacterDataHandler(xp, xrds_xml_chardata);
486 const char *req = ChrPtr(WCC->upload);
488 req = strchr(req, '<'); /* hunt for the first tag */
491 req = "ERROR"; /* force it to barf */
494 i = XML_Parse(xp, req, strlen(req), 1);
496 syslog(LOG_DEBUG, "XML_Parse() failed: %s", XML_ErrorString(XML_GetErrorCode(xp)));
505 if (!parse_success) {
506 hprintf("HTTP/1.1 500 Internal Server Error\r\n");
507 dav_common_headers();
508 hprintf("Date: %s\r\n", datestring);
509 hprintf("Content-Type: text/plain\r\n");
510 wc_printf("An internal error has occurred at %s:%d.\r\n", __FILE__ , __LINE__ );
515 dav_roomname = NewStrBuf();
516 dav_uid = NewStrBuf();
517 StrBufExtract_token(dav_roomname, WCC->Hdr->HR.ReqLine, 0, '/');
518 StrBufExtract_token(dav_uid, WCC->Hdr->HR.ReqLine, 1, '/');
520 syslog(LOG_DEBUG, "PROPFIND requested for '%s' at depth %d",
521 ChrPtr(dav_roomname), WCC->Hdr->HR.dav_depth
525 * If the room name is blank, the client is requesting a folder list.
527 if (StrLength(dav_roomname) == 0) {
528 dav_collection_list();
529 FreeStrBuf(&dav_roomname);
530 FreeStrBuf(&dav_uid);
534 /* Go to the correct room. */
535 if (strcasecmp(ChrPtr(WCC->CurRoom.name), ChrPtr(dav_roomname))) {
536 gotoroom(dav_roomname);
538 if (strcasecmp(ChrPtr(WCC->CurRoom.name), ChrPtr(dav_roomname))) {
539 hprintf("HTTP/1.1 404 not found\r\n");
540 dav_common_headers();
541 hprintf("Date: %s\r\n", datestring);
542 hprintf("Content-Type: text/plain\r\n");
543 wc_printf("There is no folder called \"%s\" on this server.\r\n", ChrPtr(dav_roomname));
545 FreeStrBuf(&dav_roomname);
546 FreeStrBuf(&dav_uid);
550 /* If dav_uid is non-empty, client is requesting a PROPFIND on
551 * a specific item in the room. This is not valid GroupDAV, but
552 * it is valid WebDAV (and probably CalDAV too).
554 if (StrLength(dav_uid) != 0) {
556 dav_msgnum = locate_message_by_uid(ChrPtr(dav_uid));
557 if (dav_msgnum < 0) {
558 hprintf("HTTP/1.1 404 not found\r\n");
559 dav_common_headers();
560 hprintf("Content-Type: text/plain\r\n");
561 wc_printf("Object \"%s\" was not found in the \"%s\" folder.\r\n",
566 FreeStrBuf(&dav_roomname);
567 FreeStrBuf(&dav_uid);
571 /* Be rude. Completely ignore the XML request and simply send them
572 * everything we know about (which is going to simply be the ETag and
573 * nothing else). Let the client-side parser sort it out.
575 hprintf("HTTP/1.0 207 Multi-Status\r\n");
576 dav_common_headers();
577 hprintf("Date: %s\r\n", datestring);
578 hprintf("Content-type: text/xml\r\n");
579 if (DisableGzip || (!WCC->Hdr->HR.gzip_ok))
580 hprintf("Content-encoding: identity\r\n");
584 wc_printf("<?xml version=\"1.0\" encoding=\"utf-8\"?>"
585 "<multistatus xmlns=\"DAV:\">"
588 wc_printf("<response>");
592 wc_printf("/groupdav/");
593 urlescputs(ChrPtr(WCC->CurRoom.name));
594 euid_escapize(encoded_uid, ChrPtr(dav_uid));
595 wc_printf("/%s", encoded_uid);
596 wc_printf("</href>");
597 wc_printf("<propstat>");
598 wc_printf("<status>HTTP/1.1 200 OK</status>");
600 wc_printf("<getetag>\"%ld\"</getetag>", dav_msgnum);
601 wc_printf("<getlastmodified>");
603 wc_printf("</getlastmodified>");
604 wc_printf("</prop>");
605 wc_printf("</propstat>");
607 wc_printf("</response>\n");
608 wc_printf("</multistatus>\n");
610 FreeStrBuf(&dav_roomname);
611 FreeStrBuf(&dav_uid);
614 FreeStrBuf(&dav_roomname);
615 FreeStrBuf(&dav_uid);
619 * If we get to this point the client is performing a PROPFIND on the room itself.
621 * We call it a room; DAV calls it a "collection." We have to give it some properties
622 * of the room itself and then offer a list of all items contained therein.
624 * Be rude. Completely ignore the XML request and simply send them
625 * everything we know about (which is going to simply be the ETag and
626 * nothing else). Let the client-side parser sort it out.
628 //syslog(LOG_DEBUG, "BE RUDE AND IGNORE: \033[31m%s\033[0m", ChrPtr(WC->upload) );
629 hprintf("HTTP/1.0 207 Multi-Status\r\n");
630 dav_common_headers();
631 hprintf("Date: %s\r\n", datestring);
632 hprintf("Content-type: text/xml\r\n");
633 if (DisableGzip || (!WCC->Hdr->HR.gzip_ok)) {
634 hprintf("Content-encoding: identity\r\n");
638 wc_printf("<?xml version=\"1.0\" encoding=\"utf-8\"?>"
641 "xmlns:G=\"http://groupdav.org/\" "
642 "xmlns:C=\"urn:ietf:params:xml:ns:caldav\""
646 /* Transmit the collection resource */
647 wc_printf("<D:response>");
649 wc_printf("<D:href>");
651 wc_printf("/groupdav/");
652 urlescputs(ChrPtr(WCC->CurRoom.name));
653 wc_printf("</D:href>");
655 wc_printf("<D:propstat>");
656 wc_printf("<D:status>HTTP/1.1 200 OK</D:status>");
657 wc_printf("<D:prop>");
658 wc_printf("<D:displayname>");
659 escputs(ChrPtr(WCC->CurRoom.name));
660 wc_printf("</D:displayname>");
662 wc_printf("<D:owner/>"); /* empty owner ought to be legal; see rfc3744 section 5.1 */
664 wc_printf("<D:resourcetype><D:collection/>");
665 switch(WCC->CurRoom.defview) {
667 wc_printf("<G:vevent-collection />");
668 wc_printf("<C:calendar />");
671 wc_printf("<G:vtodo-collection />");
673 case VIEW_ADDRESSBOOK:
674 wc_printf("<G:vcard-collection />");
677 wc_printf("</D:resourcetype>");
679 /* FIXME get the mtime
680 wc_printf("<D:getlastmodified>");
682 wc_printf("</D:getlastmodified>");
684 wc_printf("</D:prop>");
685 wc_printf("</D:propstat>");
686 wc_printf("</D:response>");
688 /* If a depth greater than zero was specified, transmit the collection listing */
690 if (WCC->Hdr->HR.dav_depth > 0) {
691 MsgNum = NewStrBuf();
692 serv_puts("MSGS ALL");
694 StrBuf_ServGetln(MsgNum);
695 if (GetServerStatus(MsgNum, NULL) == 1)
696 while (BufLen = StrBuf_ServGetln(MsgNum),
698 ((BufLen != 3) || strcmp(ChrPtr(MsgNum), "000")) ))
700 msgs = realloc(msgs, ++num_msgs * sizeof(long));
701 msgs[num_msgs-1] = StrTol(MsgNum);
704 if (num_msgs > 0) for (i=0; i<num_msgs; ++i) {
706 syslog(LOG_DEBUG, "PROPFIND enumerating message # %ld", msgs[i]);
709 serv_printf("MSG0 %ld|3", msgs[i]);
710 StrBuf_ServGetln(MsgNum);
711 if (GetServerStatus(MsgNum, NULL) == 1)
712 while (BufLen = StrBuf_ServGetln(MsgNum),
714 ((BufLen != 3) || strcmp(ChrPtr(MsgNum), "000")) ))
716 if (!strncasecmp(ChrPtr(MsgNum), "exti=", 5)) {
717 strcpy(uid, &ChrPtr(MsgNum)[5]);
719 else if (!strncasecmp(ChrPtr(MsgNum), "time=", 5)) {
720 now = atol(&ChrPtr(MsgNum)[5]);
724 if (!IsEmptyStr(uid)) {
725 wc_printf("<D:response>");
726 wc_printf("<D:href>");
728 wc_printf("/groupdav/");
729 urlescputs(ChrPtr(WCC->CurRoom.name));
730 euid_escapize(encoded_uid, uid);
731 wc_printf("/%s", encoded_uid);
732 wc_printf("</D:href>");
733 switch(WCC->CurRoom.defview) {
735 wc_printf("<D:getcontenttype>text/x-ical</D:getcontenttype>");
738 wc_printf("<D:getcontenttype>text/x-ical</D:getcontenttype>");
740 case VIEW_ADDRESSBOOK:
741 wc_printf("<D:getcontenttype>text/x-vcard</D:getcontenttype>");
744 wc_printf("<D:propstat>");
745 wc_printf("<D:status>HTTP/1.1 200 OK</D:status>");
746 wc_printf("<D:prop>");
747 wc_printf("<D:getetag>\"%ld\"</D:getetag>", msgs[i]);
749 http_datestring(datestring, sizeof datestring, now);
750 wc_printf("<D:getlastmodified>");
752 wc_printf("</D:getlastmodified>");
754 wc_printf("</D:prop>");
755 wc_printf("</D:propstat>");
756 wc_printf("</D:response>");
762 wc_printf("</D:multistatus>\n");
772 int ParseMessageListHeaders_EUID(StrBuf *Line,
774 message_summary *Msg,
775 StrBuf *ConversionBuffer)
777 Msg->euid = NewStrBuf();
778 StrBufExtract_NextToken(Msg->euid, Line, pos, '|');
779 Msg->date = StrBufExtractNext_long(Line, pos, '|');
781 return StrLength(Msg->euid) > 0;
784 int DavUIDL_GetParamsGetServerCall(SharedMessageStatus *Stat,
792 Stat->defaultsortorder = 0;
795 Stat->maxmsgs = 9999999;
797 snprintf(cmd, len, "MSGS ALL|||2");
801 int DavUIDL_RenderView_or_Tail(SharedMessageStatus *Stat,
806 DoTemplate(HKEY("msg_listview"),NULL,&NoCtx);
811 int DavUIDL_Cleanup(void **ViewSpecific)
813 /* Note: wDumpContent() will output one additional </div> tag. */
814 /* We ought to move this out into template */
827 RegisterReadLoopHandlerset(
829 DavUIDL_GetParamsGetServerCall,
831 NULL, /// TODO: is this right?
832 ParseMessageListHeaders_EUID,
834 DavUIDL_RenderView_or_Tail,