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.
13 * Copyright (c) 2005-2012 by the citadel.org team
15 * This program is open source software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License version 3.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
25 #include "webserver.h"
29 * Given an encoded UID, translate that to an unencoded Citadel EUID and
30 * then search for it in the current room. Return a message number or -1
34 long locate_message_by_uid(const char *uid) {
36 char decoded_uid[1024];
40 euid_unescapize(decoded_uid, uid);
42 /* ask Citadel if we have this one */
43 serv_printf("EUID %s", decoded_uid);
44 serv_getln(buf, sizeof buf);
46 retval = atol(&buf[4]);
54 * IgnoreFloor: set to 0 or 1 _nothing else_
55 * Subfolders: direct child floors will be put here.
57 const folder *GetRESTFolder(int IgnoreFloor, HashList *Subfolders)
61 const folder *ThisFolder = NULL;
62 const folder *FoundFolder = NULL;
63 const folder *BestGuess = NULL;
70 int iRoom, jURL, urlp;
74 * Guess room: if the full URL matches a room, list thats it. We also need to remember direct sub rooms.
75 * if the URL is longer, we need to find the "best guess" so we can find the room we're in, and the rest
76 * of the URL will be uids and so on.
78 itfl = GetNewHashPos(WCC->Floors, 0);
79 urlp = GetCount(WCC->Directory);
81 while (GetNextHashPos(WCC->Floors, itfl, &len, &Key, &vFolder) &&
85 if (!IgnoreFloor && /* so we can handle legacy URLS... */
86 (ThisFolder->Floor != WCC->CurrentFloor))
89 if (ThisFolder->nRoomNameParts > 1)
91 /*TODO: is that number all right? */
92 // if (urlp - ThisFolder->nRoomNameParts != 2) {
93 // if (BestGuess != NULL)
96 // itd = GetNewHashPos(WCC->Directory, 0);
97 // GetNextHashPos(WCC->Directory, itd, &len, &Key, &vDir); //TODO: how many to fast forward?
99 itd = GetNewHashPos(WCC->Directory, 0);
100 GetNextHashPos(WCC->Directory, itd, &len, &Key, &vDir); //TODO: how many to fast forward?
102 for (iRoom = 0, /* Fast forward the floorname as we checked it above: */ jURL = IgnoreFloor;
104 (iRoom <= ThisFolder->nRoomNameParts) && (jURL <= urlp);
106 iRoom++, jURL++, GetNextHashPos(WCC->Directory, itd, &len, &Key, &vDir))
109 if (strcmp(ChrPtr(ThisFolder->RoomNameParts[iRoom]),
118 if ((iRoom == ThisFolder->nRoomNameParts) && (jURL == urlp))
120 FoundFolder = ThisFolder;
122 /* URL got more parts then this room, so we remember it for the best guess*/
123 else if ((jURL <= urlp) &&
124 (ThisFolder->nRoomNameParts <= nBestGuess))
126 BestGuess = ThisFolder;
127 nBestGuess = jURL - 1;
129 /* Room has more parts than the URL, it might be a sub-room? */
130 else if (iRoom <ThisFolder->nRoomNameParts)
131 {//// TODO: ThisFolder->nRoomNameParts == urlp - IgnoreFloor???
132 Put(Subfolders, SKEY(ThisFolder->name),
133 /* Cast away const, its a reference. */
134 (void*)ThisFolder, reference_free_handler);
138 delta = GetCount(WCC->Directory) - ThisFolder->nRoomNameParts;
139 if ((delta != 2) && (nBestGuess > 1))
142 itd = GetNewHashPos(WCC->Directory, 0);
144 if (!GetNextHashPos(WCC->Directory,
145 itd, &len, &Key, &vDir) ||
154 Dir = (StrBuf*) vDir;
155 if (strcmp(ChrPtr(ThisFolder->name),
164 DeleteHashPos(&itfl);
168 BestGuess = ThisFolder;
171 FoundFolder = ThisFolder;
175 /* TODO: Subfolders: remove patterns not matching the best guess or thisfolder */
176 DeleteHashPos(&itfl);
177 if (FoundFolder != NULL)
186 long GotoRestRoom(HashList *SubRooms)
188 int IgnoreFloor = 0; /* deprecated... */
192 const folder *ThisFolder;
194 State = REST_TOPLEVEL;
196 if (WCC->Hdr->HR.Handler != NULL)
197 State |= REST_IN_NAMESPACE;
199 Count = GetCount(WCC->Directory);
201 if (Count == 0) return State;
203 if (Count >= 1) State |=REST_IN_FLOOR;
204 if (Count == 1) return State;
207 * More than 3 params and no floor found?
208 * -> fall back to old non-floored notation
210 if ((Count >= 3) && (WCC->CurrentFloor == NULL))
215 State |= REST_IN_FLOOR;
217 ThisFolder = GetRESTFolder(IgnoreFloor, SubRooms);
218 if (ThisFolder != NULL)
220 if (WCC->ThisRoom != NULL)
221 if (CompareRooms(WCC->ThisRoom, ThisFolder) != 0)
222 gotoroom(ThisFolder->name);
223 State |= REST_IN_ROOM;
226 if (GetCount(SubRooms) > 0)
227 State |= REST_HAVE_SUB_ROOMS;
229 if ((WCC->ThisRoom != NULL) &&
230 (Count + IgnoreFloor > 3))
232 if (WCC->Hdr->HR.Handler->RID(ExistsID, IgnoreFloor))
234 State |= REST_GOT_LOCAL_PART;
237 /// WHOOPS, not there???
238 State |= REST_NONEXIST;
249 * List rooms (or "collections" in DAV terminology) which contain
250 * interesting groupware objects.
252 void dav_collection_list(void)
258 char datestring[256];
261 int is_groupware_collection = 0;
262 int starting_point = 1; /**< 0 for /, 1 for /groupdav/ */
264 if (WCC->Hdr->HR.Handler == NULL) {
267 else if (StrLength(WCC->Hdr->HR.ReqLine) == 0) {
275 http_datestring(datestring, sizeof datestring, now);
278 * Be rude. Completely ignore the XML request and simply send them
279 * everything we know about. Let the client sort it out.
281 hprintf("HTTP/1.0 207 Multi-Status\r\n");
282 dav_common_headers();
283 hprintf("Date: %s\r\n", datestring);
284 hprintf("Content-type: text/xml\r\n");
285 if (DisableGzip || (!WCC->Hdr->HR.gzip_ok))
286 hprintf("Content-encoding: identity\r\n");
290 wc_printf("<?xml version=\"1.0\" encoding=\"utf-8\"?>"
291 "<multistatus xmlns=\"DAV:\" xmlns:G=\"http://groupdav.org/\">"
295 * If the client is requesting the root, show a root node.
297 if (starting_point == 0) {
298 wc_printf("<response>");
302 wc_printf("</href>");
303 wc_printf("<propstat>");
304 wc_printf("<status>HTTP/1.1 200 OK</status>");
306 wc_printf("<displayname>/</displayname>");
307 wc_printf("<resourcetype><collection/></resourcetype>");
308 wc_printf("<getlastmodified>");
310 wc_printf("</getlastmodified>");
311 wc_printf("</prop>");
312 wc_printf("</propstat>");
313 wc_printf("</response>");
317 * If the client is requesting "/groupdav", show a /groupdav subdirectory.
319 if ((starting_point + WCC->Hdr->HR.dav_depth) >= 1) {
320 wc_printf("<response>");
323 wc_printf("/groupdav");
324 wc_printf("</href>");
325 wc_printf("<propstat>");
326 wc_printf("<status>HTTP/1.1 200 OK</status>");
328 wc_printf("<displayname>GroupDAV</displayname>");
329 wc_printf("<resourcetype><collection/></resourcetype>");
330 wc_printf("<getlastmodified>");
332 wc_printf("</getlastmodified>");
333 wc_printf("</prop>");
334 wc_printf("</propstat>");
335 wc_printf("</response>");
339 * Now go through the list and make it look like a DAV collection
342 serv_getln(buf, sizeof buf);
343 if (buf[0] == '1') while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
345 extract_token(roomname, buf, 0, '|', sizeof roomname);
346 view = extract_int(buf, 7);
347 mtime = extract_long(buf, 8);
348 http_datestring(datestring, sizeof datestring, mtime);
351 * For now, only list rooms that we know a GroupDAV client
352 * might be interested in. In the future we may add
355 * We determine the type of objects which are stored in each
356 * room by looking at the *default* view for the room. This
357 * allows, for example, a Calendar room to appear as a
358 * GroupDAV calendar even if the user has switched it to a
359 * Calendar List view.
361 if ( (view == VIEW_CALENDAR) ||
362 (view == VIEW_TASKS) ||
363 (view == VIEW_ADDRESSBOOK) ||
364 (view == VIEW_NOTES) ||
365 (view == VIEW_JOURNAL) ||
368 is_groupware_collection = 1;
371 is_groupware_collection = 0;
374 if ( (is_groupware_collection) && ((starting_point + WCC->Hdr->HR.dav_depth) >= 2) ) {
375 wc_printf("<response>");
379 wc_printf("/groupdav/");
380 urlescputs(roomname);
381 wc_printf("/</href>");
383 wc_printf("<propstat>");
384 wc_printf("<status>HTTP/1.1 200 OK</status>");
386 wc_printf("<displayname>");
388 wc_printf("</displayname>");
389 wc_printf("<resourcetype><collection/>");
393 wc_printf("<G:vevent-collection />");
396 wc_printf("<G:vtodo-collection />");
398 case VIEW_ADDRESSBOOK:
399 wc_printf("<G:vcard-collection />");
402 wc_printf("<G:vnotes-collection />");
405 wc_printf("<G:vjournal-collection />");
408 wc_printf("<G:wiki-collection />");
412 wc_printf("</resourcetype>");
413 wc_printf("<getlastmodified>");
415 wc_printf("</getlastmodified>");
416 wc_printf("</prop>");
417 wc_printf("</propstat>");
418 wc_printf("</response>");
421 wc_printf("</multistatus>\n");
427 void propfind_xml_start(void *data, const char *supplied_el, const char **attr) {
428 syslog(LOG_DEBUG, "<%s>", supplied_el);
431 void propfind_xml_end(void *data, const char *supplied_el) {
432 syslog(LOG_DEBUG, "</%s>", supplied_el);
438 * The pathname is always going to be /groupdav/room_name/msg_num
440 void dav_propfind(void)
443 StrBuf *dav_roomname;
447 long dav_msgnum = (-1);
449 char encoded_uid[256];
453 char datestring[256];
457 http_datestring(datestring, sizeof datestring, now);
459 int parse_success = 0;
460 XML_Parser xp = XML_ParserCreateNS(NULL, '|');
462 // XML_SetUserData(xp, XXX);
463 XML_SetElementHandler(xp, propfind_xml_start, propfind_xml_end);
464 // XML_SetCharacterDataHandler(xp, xrds_xml_chardata);
466 const char *req = ChrPtr(WCC->upload);
468 req = strchr(req, '<'); /* hunt for the first tag */
471 req = "ERROR"; /* force it to barf */
474 i = XML_Parse(xp, req, strlen(req), 1);
476 syslog(LOG_DEBUG, "XML_Parse() failed: %s", XML_ErrorString(XML_GetErrorCode(xp)));
485 if (!parse_success) {
486 hprintf("HTTP/1.1 500 Internal Server Error\r\n");
487 dav_common_headers();
488 hprintf("Date: %s\r\n", datestring);
489 hprintf("Content-Type: text/plain\r\n");
490 wc_printf("An internal error has occurred at %s:%d.\r\n", __FILE__ , __LINE__ );
495 dav_roomname = NewStrBuf();
496 dav_uid = NewStrBuf();
497 StrBufExtract_token(dav_roomname, WCC->Hdr->HR.ReqLine, 0, '/');
498 StrBufExtract_token(dav_uid, WCC->Hdr->HR.ReqLine, 1, '/');
501 * If the room name is blank, the client is requesting a folder list.
503 if (StrLength(dav_roomname) == 0) {
504 dav_collection_list();
505 FreeStrBuf(&dav_roomname);
506 FreeStrBuf(&dav_uid);
510 /* Go to the correct room. */
511 if (strcasecmp(ChrPtr(WCC->CurRoom.name), ChrPtr(dav_roomname))) {
512 gotoroom(dav_roomname);
514 if (strcasecmp(ChrPtr(WCC->CurRoom.name), ChrPtr(dav_roomname))) {
515 hprintf("HTTP/1.1 404 not found\r\n");
516 dav_common_headers();
517 hprintf("Date: %s\r\n", datestring);
518 hprintf("Content-Type: text/plain\r\n");
519 wc_printf("There is no folder called \"%s\" on this server.\r\n", ChrPtr(dav_roomname));
521 FreeStrBuf(&dav_roomname);
522 FreeStrBuf(&dav_uid);
526 /* If dav_uid is non-empty, client is requesting a PROPFIND on
527 * a specific item in the room. This is not valid GroupDAV, but
528 * it is valid WebDAV (and probably CalDAV too).
530 if (StrLength(dav_uid) != 0) {
532 dav_msgnum = locate_message_by_uid(ChrPtr(dav_uid));
533 if (dav_msgnum < 0) {
534 hprintf("HTTP/1.1 404 not found\r\n");
535 dav_common_headers();
536 hprintf("Content-Type: text/plain\r\n");
537 wc_printf("Object \"%s\" was not found in the \"%s\" folder.\r\n",
542 FreeStrBuf(&dav_roomname);
543 FreeStrBuf(&dav_uid);
547 /* Be rude. Completely ignore the XML request and simply send them
548 * everything we know about (which is going to simply be the ETag and
549 * nothing else). Let the client-side parser sort it out.
551 hprintf("HTTP/1.0 207 Multi-Status\r\n");
552 dav_common_headers();
553 hprintf("Date: %s\r\n", datestring);
554 hprintf("Content-type: text/xml\r\n");
555 if (DisableGzip || (!WCC->Hdr->HR.gzip_ok))
556 hprintf("Content-encoding: identity\r\n");
560 wc_printf("<?xml version=\"1.0\" encoding=\"utf-8\"?>"
561 "<multistatus xmlns=\"DAV:\">"
564 wc_printf("<response>");
568 wc_printf("/groupdav/");
569 urlescputs(ChrPtr(WCC->CurRoom.name));
570 euid_escapize(encoded_uid, ChrPtr(dav_uid));
571 wc_printf("/%s", encoded_uid);
572 wc_printf("</href>");
573 wc_printf("<propstat>");
574 wc_printf("<status>HTTP/1.1 200 OK</status>");
576 wc_printf("<getetag>\"%ld\"</getetag>", dav_msgnum);
577 wc_printf("<getlastmodified>");
579 wc_printf("</getlastmodified>");
580 wc_printf("</prop>");
581 wc_printf("</propstat>");
583 wc_printf("</response>\n");
584 wc_printf("</multistatus>\n");
586 FreeStrBuf(&dav_roomname);
587 FreeStrBuf(&dav_uid);
590 FreeStrBuf(&dav_roomname);
591 FreeStrBuf(&dav_uid);
595 * We got to this point, which means that the client is requesting
596 * a 'collection' (i.e. a list of all items in the room).
598 * Be rude. Completely ignore the XML request and simply send them
599 * everything we know about (which is going to simply be the ETag and
600 * nothing else). Let the client-side parser sort it out.
602 hprintf("HTTP/1.0 207 Multi-Status\r\n");
603 dav_common_headers();
604 hprintf("Date: %s\r\n", datestring);
605 hprintf("Content-type: text/xml\r\n");
606 if (DisableGzip || (!WCC->Hdr->HR.gzip_ok)) {
607 hprintf("Content-encoding: identity\r\n");
611 wc_printf("<?xml version=\"1.0\" encoding=\"utf-8\"?>"
614 "xmlns:G=\"http://groupdav.org/\" "
615 "xmlns:CALDAV=\"urn:ietf:params:xml:ns:caldav\""
619 /* Transmit the collection resource (FIXME check depth and starting point) */
620 wc_printf("<response>");
624 wc_printf("/groupdav/");
625 urlescputs(ChrPtr(WCC->CurRoom.name));
626 wc_printf("</href>");
628 wc_printf("<propstat>");
629 wc_printf("<status>HTTP/1.1 200 OK</status>");
631 wc_printf("<displayname>");
632 escputs(ChrPtr(WCC->CurRoom.name));
633 wc_printf("</displayname>");
635 wc_printf("<owner/>"); /* empty owner ought to be legal; see rfc3744 section 5.1 */
637 wc_printf("<resourcetype><collection/>");
638 switch(WCC->CurRoom.defview) {
640 wc_printf("<G:vevent-collection />");
641 wc_printf("<CALDAV:calendar />");
644 wc_printf("<G:vtodo-collection />");
646 case VIEW_ADDRESSBOOK:
647 wc_printf("<G:vcard-collection />");
650 wc_printf("</resourcetype>");
652 /* FIXME get the mtime
653 wc_printf("<getlastmodified>");
655 wc_printf("</getlastmodified>");
657 wc_printf("</prop>");
658 wc_printf("</propstat>");
659 wc_printf("</response>");
661 /* Transmit the collection listing (FIXME check depth and starting point) */
663 MsgNum = NewStrBuf();
664 serv_puts("MSGS ALL");
666 StrBuf_ServGetln(MsgNum);
667 if (GetServerStatus(MsgNum, NULL) == 1)
668 while (BufLen = StrBuf_ServGetln(MsgNum),
670 ((BufLen != 3) || strcmp(ChrPtr(MsgNum), "000")) ))
672 msgs = realloc(msgs, ++num_msgs * sizeof(long));
673 msgs[num_msgs-1] = StrTol(MsgNum);
676 if (num_msgs > 0) for (i=0; i<num_msgs; ++i) {
678 syslog(LOG_DEBUG, "PROPFIND enumerating message # %ld", msgs[i]);
681 serv_printf("MSG0 %ld|3", msgs[i]);
682 StrBuf_ServGetln(MsgNum);
683 if (GetServerStatus(MsgNum, NULL) == 1)
684 while (BufLen = StrBuf_ServGetln(MsgNum),
686 ((BufLen != 3) || strcmp(ChrPtr(MsgNum), "000")) ))
688 if (!strncasecmp(ChrPtr(MsgNum), "exti=", 5)) {
689 strcpy(uid, &ChrPtr(MsgNum)[5]);
691 else if (!strncasecmp(ChrPtr(MsgNum), "time=", 5)) {
692 now = atol(&ChrPtr(MsgNum)[5]);
696 if (!IsEmptyStr(uid)) {
697 wc_printf("<response>");
700 wc_printf("/groupdav/");
701 urlescputs(ChrPtr(WCC->CurRoom.name));
702 euid_escapize(encoded_uid, uid);
703 wc_printf("/%s", encoded_uid);
704 wc_printf("</href>");
705 switch(WCC->CurRoom.defview) {
707 wc_printf("<getcontenttype>text/x-ical</getcontenttype>");
710 wc_printf("<getcontenttype>text/x-ical</getcontenttype>");
712 case VIEW_ADDRESSBOOK:
713 wc_printf("<getcontenttype>text/x-vcard</getcontenttype>");
716 wc_printf("<propstat>");
717 wc_printf("<status>HTTP/1.1 200 OK</status>");
719 wc_printf("<getetag>\"%ld\"</getetag>", msgs[i]);
721 http_datestring(datestring, sizeof datestring, now);
722 wc_printf("<getlastmodified>");
724 wc_printf("</getlastmodified>");
726 wc_printf("</prop>");
727 wc_printf("</propstat>");
728 wc_printf("</response>");
733 wc_printf("</multistatus>\n");
743 int ParseMessageListHeaders_EUID(StrBuf *Line,
745 message_summary *Msg,
746 StrBuf *ConversionBuffer)
748 Msg->euid = NewStrBuf();
749 StrBufExtract_NextToken(Msg->euid, Line, pos, '|');
750 Msg->date = StrBufExtractNext_long(Line, pos, '|');
752 return StrLength(Msg->euid) > 0;
755 int DavUIDL_GetParamsGetServerCall(SharedMessageStatus *Stat,
761 Stat->defaultsortorder = 0;
764 Stat->maxmsgs = 9999999;
766 snprintf(cmd, len, "MSGS ALL|||2");
770 int DavUIDL_RenderView_or_Tail(SharedMessageStatus *Stat,
775 DoTemplate(HKEY("msg_listview"),NULL,&NoCtx);
780 int DavUIDL_Cleanup(void **ViewSpecific)
782 /* Note: wDumpContent() will output one additional </div> tag. */
783 /* We ought to move this out into template */
796 RegisterReadLoopHandlerset(
798 DavUIDL_GetParamsGetServerCall,
799 NULL, /// TODO: is this right?
800 ParseMessageListHeaders_EUID,
802 DavUIDL_RenderView_or_Tail,