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) ||
170 syslog(LOG_DEBUG, "5\n");
174 Dir = (StrBuf*) vDir;
175 if (strcmp(ChrPtr(ThisFolder->name),
181 syslog(LOG_DEBUG, "5\n");
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) ||
386 (view == VIEW_WIKI) ||
387 (view == VIEW_WIKIMD)
389 is_groupware_collection = 1;
392 is_groupware_collection = 0;
395 if ( (is_groupware_collection) && ((starting_point + WCC->Hdr->HR.dav_depth) >= 2) ) {
396 wc_printf("<response>");
400 wc_printf("/groupdav/");
401 urlescputs(roomname);
402 wc_printf("/</href>");
404 wc_printf("<propstat>");
405 wc_printf("<status>HTTP/1.1 200 OK</status>");
407 wc_printf("<displayname>");
409 wc_printf("</displayname>");
410 wc_printf("<resourcetype><collection/>");
414 wc_printf("<G:vevent-collection />");
417 wc_printf("<G:vtodo-collection />");
419 case VIEW_ADDRESSBOOK:
420 wc_printf("<G:vcard-collection />");
423 wc_printf("<G:vnotes-collection />");
426 wc_printf("<G:vjournal-collection />");
430 wc_printf("<G:wiki-collection />");
434 wc_printf("</resourcetype>");
435 wc_printf("<getlastmodified>");
437 wc_printf("</getlastmodified>");
438 wc_printf("</prop>");
439 wc_printf("</propstat>");
440 wc_printf("</response>");
443 wc_printf("</multistatus>\n");
449 void propfind_xml_start(void *data, const char *supplied_el, const char **attr) {
450 // syslog(LOG_DEBUG, "<%s>", supplied_el);
453 void propfind_xml_end(void *data, const char *supplied_el) {
454 // syslog(LOG_DEBUG, "</%s>", supplied_el);
460 * The pathname is always going to be /groupdav/room_name/msg_num
462 void dav_propfind(void)
465 StrBuf *dav_roomname;
469 long dav_msgnum = (-1);
471 char encoded_uid[256];
475 char datestring[256];
479 http_datestring(datestring, sizeof datestring, now);
481 int parse_success = 0;
482 XML_Parser xp = XML_ParserCreateNS(NULL, '|');
484 // XML_SetUserData(xp, XXX);
485 XML_SetElementHandler(xp, propfind_xml_start, propfind_xml_end);
486 // XML_SetCharacterDataHandler(xp, xrds_xml_chardata);
488 const char *req = ChrPtr(WCC->upload);
490 req = strchr(req, '<'); /* hunt for the first tag */
493 req = "ERROR"; /* force it to barf */
496 i = XML_Parse(xp, req, strlen(req), 1);
498 syslog(LOG_DEBUG, "XML_Parse() failed: %s", XML_ErrorString(XML_GetErrorCode(xp)));
507 if (!parse_success) {
508 hprintf("HTTP/1.1 500 Internal Server Error\r\n");
509 dav_common_headers();
510 hprintf("Date: %s\r\n", datestring);
511 hprintf("Content-Type: text/plain\r\n");
512 wc_printf("An internal error has occurred at %s:%d.\r\n", __FILE__ , __LINE__ );
517 dav_roomname = NewStrBuf();
518 dav_uid = NewStrBuf();
519 StrBufExtract_token(dav_roomname, WCC->Hdr->HR.ReqLine, 0, '/');
520 StrBufExtract_token(dav_uid, WCC->Hdr->HR.ReqLine, 1, '/');
522 syslog(LOG_DEBUG, "PROPFIND requested for '%s' at depth %d",
523 ChrPtr(dav_roomname), WCC->Hdr->HR.dav_depth
527 * If the room name is blank, the client is requesting a folder list.
529 if (StrLength(dav_roomname) == 0) {
530 dav_collection_list();
531 FreeStrBuf(&dav_roomname);
532 FreeStrBuf(&dav_uid);
536 /* Go to the correct room. */
537 if (strcasecmp(ChrPtr(WCC->CurRoom.name), ChrPtr(dav_roomname))) {
538 gotoroom(dav_roomname);
540 if (strcasecmp(ChrPtr(WCC->CurRoom.name), ChrPtr(dav_roomname))) {
541 hprintf("HTTP/1.1 404 not found\r\n");
542 dav_common_headers();
543 hprintf("Date: %s\r\n", datestring);
544 hprintf("Content-Type: text/plain\r\n");
545 wc_printf("There is no folder called \"%s\" on this server.\r\n", ChrPtr(dav_roomname));
547 FreeStrBuf(&dav_roomname);
548 FreeStrBuf(&dav_uid);
552 /* If dav_uid is non-empty, client is requesting a PROPFIND on
553 * a specific item in the room. This is not valid GroupDAV, but
554 * it is valid WebDAV (and probably CalDAV too).
556 if (StrLength(dav_uid) != 0) {
558 dav_msgnum = locate_message_by_uid(ChrPtr(dav_uid));
559 if (dav_msgnum < 0) {
560 hprintf("HTTP/1.1 404 not found\r\n");
561 dav_common_headers();
562 hprintf("Content-Type: text/plain\r\n");
563 wc_printf("Object \"%s\" was not found in the \"%s\" folder.\r\n",
568 FreeStrBuf(&dav_roomname);
569 FreeStrBuf(&dav_uid);
573 /* Be rude. Completely ignore the XML request and simply send them
574 * everything we know about (which is going to simply be the ETag and
575 * nothing else). Let the client-side parser sort it out.
577 hprintf("HTTP/1.0 207 Multi-Status\r\n");
578 dav_common_headers();
579 hprintf("Date: %s\r\n", datestring);
580 hprintf("Content-type: text/xml\r\n");
581 if (DisableGzip || (!WCC->Hdr->HR.gzip_ok))
582 hprintf("Content-encoding: identity\r\n");
586 wc_printf("<?xml version=\"1.0\" encoding=\"utf-8\"?>"
587 "<multistatus xmlns=\"DAV:\">"
590 wc_printf("<response>");
594 wc_printf("/groupdav/");
595 urlescputs(ChrPtr(WCC->CurRoom.name));
596 euid_escapize(encoded_uid, ChrPtr(dav_uid));
597 wc_printf("/%s", encoded_uid);
598 wc_printf("</href>");
599 wc_printf("<propstat>");
600 wc_printf("<status>HTTP/1.1 200 OK</status>");
602 wc_printf("<getetag>\"%ld\"</getetag>", dav_msgnum);
603 wc_printf("<getlastmodified>");
605 wc_printf("</getlastmodified>");
606 wc_printf("</prop>");
607 wc_printf("</propstat>");
609 wc_printf("</response>\n");
610 wc_printf("</multistatus>\n");
612 FreeStrBuf(&dav_roomname);
613 FreeStrBuf(&dav_uid);
616 FreeStrBuf(&dav_roomname);
617 FreeStrBuf(&dav_uid);
621 * If we get to this point the client is performing a PROPFIND on the room itself.
623 * We call it a room; DAV calls it a "collection." We have to give it some properties
624 * of the room itself and then offer a list of all items contained therein.
626 * Be rude. Completely ignore the XML request and simply send them
627 * everything we know about (which is going to simply be the ETag and
628 * nothing else). Let the client-side parser sort it out.
630 //syslog(LOG_DEBUG, "BE RUDE AND IGNORE: \033[31m%s\033[0m", ChrPtr(WC->upload) );
631 hprintf("HTTP/1.0 207 Multi-Status\r\n");
632 dav_common_headers();
633 hprintf("Date: %s\r\n", datestring);
634 hprintf("Content-type: text/xml\r\n");
635 if (DisableGzip || (!WCC->Hdr->HR.gzip_ok)) {
636 hprintf("Content-encoding: identity\r\n");
640 wc_printf("<?xml version=\"1.0\" encoding=\"utf-8\"?>"
643 "xmlns:G=\"http://groupdav.org/\" "
644 "xmlns:C=\"urn:ietf:params:xml:ns:caldav\""
648 /* Transmit the collection resource */
649 wc_printf("<D:response>");
651 wc_printf("<D:href>");
653 wc_printf("/groupdav/");
654 urlescputs(ChrPtr(WCC->CurRoom.name));
655 wc_printf("</D:href>");
657 wc_printf("<D:propstat>");
658 wc_printf("<D:status>HTTP/1.1 200 OK</D:status>");
659 wc_printf("<D:prop>");
660 wc_printf("<D:displayname>");
661 escputs(ChrPtr(WCC->CurRoom.name));
662 wc_printf("</D:displayname>");
664 wc_printf("<D:owner/>"); /* empty owner ought to be legal; see rfc3744 section 5.1 */
666 wc_printf("<D:resourcetype><D:collection/>");
667 switch(WCC->CurRoom.defview) {
669 wc_printf("<G:vevent-collection />");
670 wc_printf("<C:calendar />");
673 wc_printf("<G:vtodo-collection />");
675 case VIEW_ADDRESSBOOK:
676 wc_printf("<G:vcard-collection />");
679 wc_printf("</D:resourcetype>");
681 /* FIXME get the mtime
682 wc_printf("<D:getlastmodified>");
684 wc_printf("</D:getlastmodified>");
686 wc_printf("</D:prop>");
687 wc_printf("</D:propstat>");
688 wc_printf("</D:response>");
690 /* If a depth greater than zero was specified, transmit the collection listing */
692 if (WCC->Hdr->HR.dav_depth > 0) {
693 MsgNum = NewStrBuf();
694 serv_puts("MSGS ALL");
696 StrBuf_ServGetln(MsgNum);
697 if (GetServerStatus(MsgNum, NULL) == 1)
698 while (BufLen = StrBuf_ServGetln(MsgNum),
700 ((BufLen != 3) || strcmp(ChrPtr(MsgNum), "000")) ))
702 msgs = realloc(msgs, ++num_msgs * sizeof(long));
703 msgs[num_msgs-1] = StrTol(MsgNum);
706 if (num_msgs > 0) for (i=0; i<num_msgs; ++i) {
708 syslog(LOG_DEBUG, "PROPFIND enumerating message # %ld", msgs[i]);
711 serv_printf("MSG0 %ld|3", msgs[i]);
712 StrBuf_ServGetln(MsgNum);
713 if (GetServerStatus(MsgNum, NULL) == 1)
714 while (BufLen = StrBuf_ServGetln(MsgNum),
716 ((BufLen != 3) || strcmp(ChrPtr(MsgNum), "000")) ))
718 if (!strncasecmp(ChrPtr(MsgNum), "exti=", 5)) {
719 strcpy(uid, &ChrPtr(MsgNum)[5]);
721 else if (!strncasecmp(ChrPtr(MsgNum), "time=", 5)) {
722 now = atol(&ChrPtr(MsgNum)[5]);
726 if (!IsEmptyStr(uid)) {
727 wc_printf("<D:response>");
728 wc_printf("<D:href>");
730 wc_printf("/groupdav/");
731 urlescputs(ChrPtr(WCC->CurRoom.name));
732 euid_escapize(encoded_uid, uid);
733 wc_printf("/%s", encoded_uid);
734 wc_printf("</D:href>");
735 switch(WCC->CurRoom.defview) {
737 wc_printf("<D:getcontenttype>text/x-ical</D:getcontenttype>");
740 wc_printf("<D:getcontenttype>text/x-ical</D:getcontenttype>");
742 case VIEW_ADDRESSBOOK:
743 wc_printf("<D:getcontenttype>text/x-vcard</D:getcontenttype>");
746 wc_printf("<D:propstat>");
747 wc_printf("<D:status>HTTP/1.1 200 OK</D:status>");
748 wc_printf("<D:prop>");
749 wc_printf("<D:getetag>\"%ld\"</D:getetag>", msgs[i]);
751 http_datestring(datestring, sizeof datestring, now);
752 wc_printf("<D:getlastmodified>");
754 wc_printf("</D:getlastmodified>");
756 wc_printf("</D:prop>");
757 wc_printf("</D:propstat>");
758 wc_printf("</D:response>");
764 wc_printf("</D:multistatus>\n");
774 int ParseMessageListHeaders_EUID(StrBuf *Line,
776 message_summary *Msg,
777 StrBuf *ConversionBuffer)
779 Msg->euid = NewStrBuf();
780 StrBufExtract_NextToken(Msg->euid, Line, pos, '|');
781 Msg->date = StrBufExtractNext_long(Line, pos, '|');
783 return StrLength(Msg->euid) > 0;
786 int DavUIDL_GetParamsGetServerCall(SharedMessageStatus *Stat,
794 Stat->defaultsortorder = 0;
797 Stat->maxmsgs = 9999999;
799 snprintf(cmd, len, "MSGS ALL|||2");
803 int DavUIDL_RenderView_or_Tail(SharedMessageStatus *Stat,
808 DoTemplate(HKEY("msg_listview"),NULL,&NoCtx);
813 int DavUIDL_Cleanup(void **ViewSpecific)
815 /* Note: wDumpContent() will output one additional </div> tag. */
816 /* We ought to move this out into template */
829 RegisterReadLoopHandlerset(
831 DavUIDL_GetParamsGetServerCall,
833 NULL, /// TODO: is this right?
834 ParseMessageListHeaders_EUID,
836 DavUIDL_RenderView_or_Tail,