2 * Handles GroupDAV 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-2011 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.
26 #include "webserver.h"
29 extern int DisableGzip;
32 * Given an encoded UID, translate that to an unencoded Citadel EUID and
33 * then search for it in the current room. Return a message number or -1
37 long locate_message_by_uid(const char *uid) {
39 char decoded_uid[1024];
43 euid_unescapize(decoded_uid, uid);
45 /* ask Citadel if we have this one */
46 serv_printf("EUID %s", decoded_uid);
47 serv_getln(buf, sizeof buf);
49 retval = atol(&buf[4]);
57 * IgnoreFloor: set to 0 or 1 _nothing else_
58 * Subfolders: direct child floors will be put here.
60 const folder *GetRESTFolder(int IgnoreFloor, HashList *Subfolders)
64 const folder *ThisFolder = NULL;
65 const folder *FoundFolder = NULL;
66 const folder *BestGuess = NULL;
73 int iRoom, jURL, urlp;
77 * Guess room: if the full URL matches a room, list thats it. We also need to remember direct sub rooms.
78 * if the URL is longer, we need to find the "best guess" so we can find the room we're in, and the rest
79 * of the URL will be uids and so on.
81 itfl = GetNewHashPos(WCC->Floors, 0);
82 urlp = GetCount(WCC->Directory);
84 while (GetNextHashPos(WCC->Floors, itfl, &len, &Key, &vFolder) &&
88 if (!IgnoreFloor && /* so we can handle legacy URLS... */
89 (ThisFolder->Floor != WCC->CurrentFloor))
92 if (ThisFolder->nRoomNameParts > 1)
94 /*TODO: is that number all right? */
95 // if (urlp - ThisFolder->nRoomNameParts != 2) {
96 // if (BestGuess != NULL)
99 // itd = GetNewHashPos(WCC->Directory, 0);
100 // GetNextHashPos(WCC->Directory, itd, &len, &Key, &vDir); //TODO: how many to fast forward?
102 itd = GetNewHashPos(WCC->Directory, 0);
103 GetNextHashPos(WCC->Directory, itd, &len, &Key, &vDir); //TODO: how many to fast forward?
105 for (iRoom = 0, /* Fast forward the floorname as we checked it above: */ jURL = IgnoreFloor;
107 (iRoom <= ThisFolder->nRoomNameParts) && (jURL <= urlp);
109 iRoom++, jURL++, GetNextHashPos(WCC->Directory, itd, &len, &Key, &vDir))
112 if (strcmp(ChrPtr(ThisFolder->RoomNameParts[iRoom]),
121 if ((iRoom == ThisFolder->nRoomNameParts) && (jURL == urlp))
123 FoundFolder = ThisFolder;
125 /* URL got more parts then this room, so we remember it for the best guess*/
126 else if ((jURL <= urlp) &&
127 (ThisFolder->nRoomNameParts <= nBestGuess))
129 BestGuess = ThisFolder;
130 nBestGuess = jURL - 1;
132 /* Room has more parts than the URL, it might be a sub-room? */
133 else if (iRoom <ThisFolder->nRoomNameParts)
134 {//// TODO: ThisFolder->nRoomNameParts == urlp - IgnoreFloor???
135 Put(Subfolders, SKEY(ThisFolder->name),
136 /* Cast away const, its a reference. */
137 (void*)ThisFolder, reference_free_handler);
141 delta = GetCount(WCC->Directory) - ThisFolder->nRoomNameParts;
142 if ((delta != 2) && (nBestGuess > 1))
145 itd = GetNewHashPos(WCC->Directory, 0);
147 if (!GetNextHashPos(WCC->Directory,
148 itd, &len, &Key, &vDir) ||
157 Dir = (StrBuf*) vDir;
158 if (strcmp(ChrPtr(ThisFolder->name),
167 DeleteHashPos(&itfl);
171 BestGuess = ThisFolder;
174 FoundFolder = ThisFolder;
178 /* TODO: Subfolders: remove patterns not matching the best guess or thisfolder */
179 DeleteHashPos(&itfl);
180 if (FoundFolder != NULL)
189 long GotoRestRoom(HashList *SubRooms)
191 int IgnoreFloor = 0; /* deprecated... */
195 const folder *ThisFolder;
197 State = REST_TOPLEVEL;
199 if (WCC->Hdr->HR.Handler != NULL)
200 State |= REST_IN_NAMESPACE;
202 Count = GetCount(WCC->Directory);
204 if (Count == 0) return State;
206 if (Count >= 1) State |=REST_IN_FLOOR;
207 if (Count == 1) return State;
210 * More than 3 params and no floor found?
211 * -> fall back to old non-floored notation
213 if ((Count >= 3) && (WCC->CurrentFloor == NULL))
218 State |= REST_IN_FLOOR;
220 ThisFolder = GetRESTFolder(IgnoreFloor, SubRooms);
221 if (ThisFolder != NULL)
223 if (WCC->ThisRoom != NULL)
224 if (CompareRooms(WCC->ThisRoom, ThisFolder) != 0)
225 gotoroom(ThisFolder->name);
226 State |= REST_IN_ROOM;
229 if (GetCount(SubRooms) > 0)
230 State |= REST_HAVE_SUB_ROOMS;
232 if ((WCC->ThisRoom != NULL) &&
233 (Count + IgnoreFloor > 3))
235 if (WCC->Hdr->HR.Handler->RID(ExistsID, IgnoreFloor))
237 State |= REST_GOT_LOCAL_PART;
240 /// WHOOPS, not there???
241 State |= REST_NONEXIST;
252 * List rooms (or "collections" in DAV terminology) which contain
253 * interesting groupware objects.
255 void dav_collection_list(void)
261 char datestring[256];
264 int is_groupware_collection = 0;
265 int starting_point = 1; /**< 0 for /, 1 for /groupdav/ */
267 if (WCC->Hdr->HR.Handler == NULL) {
270 else if (StrLength(WCC->Hdr->HR.ReqLine) == 0) {
278 http_datestring(datestring, sizeof datestring, now);
281 * Be rude. Completely ignore the XML request and simply send them
282 * everything we know about. Let the client sort it out.
284 hprintf("HTTP/1.0 207 Multi-Status\r\n");
285 dav_common_headers();
286 hprintf("Date: %s\r\n", datestring);
287 hprintf("Content-type: text/xml\r\n");
288 if (DisableGzip || (!WCC->Hdr->HR.gzip_ok))
289 hprintf("Content-encoding: identity\r\n");
293 wc_printf("<?xml version=\"1.0\" encoding=\"utf-8\"?>"
294 "<multistatus xmlns=\"DAV:\" xmlns:G=\"http://groupdav.org/\">"
298 * If the client is requesting the root, show a root node.
300 if (starting_point == 0) {
301 wc_printf("<response>");
305 wc_printf("</href>");
306 wc_printf("<propstat>");
307 wc_printf("<status>HTTP/1.1 200 OK</status>");
309 wc_printf("<displayname>/</displayname>");
310 wc_printf("<resourcetype><collection/></resourcetype>");
311 wc_printf("<getlastmodified>");
313 wc_printf("</getlastmodified>");
314 wc_printf("</prop>");
315 wc_printf("</propstat>");
316 wc_printf("</response>");
320 * If the client is requesting "/groupdav", show a /groupdav subdirectory.
322 if ((starting_point + WCC->Hdr->HR.dav_depth) >= 1) {
323 wc_printf("<response>");
326 wc_printf("/groupdav");
327 wc_printf("</href>");
328 wc_printf("<propstat>");
329 wc_printf("<status>HTTP/1.1 200 OK</status>");
331 wc_printf("<displayname>GroupDAV</displayname>");
332 wc_printf("<resourcetype><collection/></resourcetype>");
333 wc_printf("<getlastmodified>");
335 wc_printf("</getlastmodified>");
336 wc_printf("</prop>");
337 wc_printf("</propstat>");
338 wc_printf("</response>");
342 * Now go through the list and make it look like a DAV collection
345 serv_getln(buf, sizeof buf);
346 if (buf[0] == '1') while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
348 extract_token(roomname, buf, 0, '|', sizeof roomname);
349 view = extract_int(buf, 7);
350 mtime = extract_long(buf, 8);
351 http_datestring(datestring, sizeof datestring, mtime);
354 * For now, only list rooms that we know a GroupDAV client
355 * might be interested in. In the future we may add
358 * We determine the type of objects which are stored in each
359 * room by looking at the *default* view for the room. This
360 * allows, for example, a Calendar room to appear as a
361 * GroupDAV calendar even if the user has switched it to a
362 * Calendar List view.
364 if ( (view == VIEW_CALENDAR) ||
365 (view == VIEW_TASKS) ||
366 (view == VIEW_ADDRESSBOOK) ||
367 (view == VIEW_NOTES) ||
368 (view == VIEW_JOURNAL) ||
371 is_groupware_collection = 1;
374 is_groupware_collection = 0;
377 if ( (is_groupware_collection) && ((starting_point + WCC->Hdr->HR.dav_depth) >= 2) ) {
378 wc_printf("<response>");
382 wc_printf("/groupdav/");
383 urlescputs(roomname);
384 wc_printf("/</href>");
386 wc_printf("<propstat>");
387 wc_printf("<status>HTTP/1.1 200 OK</status>");
389 wc_printf("<displayname>");
391 wc_printf("</displayname>");
392 wc_printf("<resourcetype><collection/>");
396 wc_printf("<G:vevent-collection />");
399 wc_printf("<G:vtodo-collection />");
401 case VIEW_ADDRESSBOOK:
402 wc_printf("<G:vcard-collection />");
405 wc_printf("<G:vnotes-collection />");
408 wc_printf("<G:vjournal-collection />");
411 wc_printf("<G:wiki-collection />");
415 wc_printf("</resourcetype>");
416 wc_printf("<getlastmodified>");
418 wc_printf("</getlastmodified>");
419 wc_printf("</prop>");
420 wc_printf("</propstat>");
421 wc_printf("</response>");
424 wc_printf("</multistatus>\n");
430 void propfind_xml_start(void *data, const char *supplied_el, const char **attr) {
431 syslog(LOG_DEBUG, "<%s>", supplied_el);
434 void propfind_xml_end(void *data, const char *supplied_el) {
435 syslog(LOG_DEBUG, "</%s>", supplied_el);
441 * The pathname is always going to be /groupdav/room_name/msg_num
443 void dav_propfind(void)
446 StrBuf *dav_roomname;
450 long dav_msgnum = (-1);
452 char encoded_uid[256];
456 char datestring[256];
460 http_datestring(datestring, sizeof datestring, now);
462 int parse_success = 0;
463 XML_Parser xp = XML_ParserCreateNS(NULL, '|');
465 // XML_SetUserData(xp, XXX);
466 XML_SetElementHandler(xp, propfind_xml_start, propfind_xml_end);
467 // XML_SetCharacterDataHandler(xp, xrds_xml_chardata);
469 const char *req = ChrPtr(WCC->upload);
471 req = strchr(req, '<'); /* hunt for the first tag */
474 req = "ERROR"; /* force it to barf */
477 i = XML_Parse(xp, req, strlen(req), 1);
479 syslog(LOG_DEBUG, "XML_Parse() failed: %s", XML_ErrorString(XML_GetErrorCode(xp)));
488 if (!parse_success) {
489 hprintf("HTTP/1.1 500 Internal Server Error\r\n");
490 dav_common_headers();
491 hprintf("Date: %s\r\n", datestring);
492 hprintf("Content-Type: text/plain\r\n");
493 wc_printf("An internal error has occurred at %s:%d.\r\n", __FILE__ , __LINE__ );
498 dav_roomname = NewStrBuf();
499 dav_uid = NewStrBuf();
500 StrBufExtract_token(dav_roomname, WCC->Hdr->HR.ReqLine, 0, '/');
501 StrBufExtract_token(dav_uid, WCC->Hdr->HR.ReqLine, 1, '/');
504 * If the room name is blank, the client is requesting a folder list.
506 if (StrLength(dav_roomname) == 0) {
507 dav_collection_list();
508 FreeStrBuf(&dav_roomname);
509 FreeStrBuf(&dav_uid);
513 /* Go to the correct room. */
514 if (strcasecmp(ChrPtr(WCC->CurRoom.name), ChrPtr(dav_roomname))) {
515 gotoroom(dav_roomname);
517 if (strcasecmp(ChrPtr(WCC->CurRoom.name), ChrPtr(dav_roomname))) {
518 hprintf("HTTP/1.1 404 not found\r\n");
519 dav_common_headers();
520 hprintf("Date: %s\r\n", datestring);
521 hprintf("Content-Type: text/plain\r\n");
522 wc_printf("There is no folder called \"%s\" on this server.\r\n", ChrPtr(dav_roomname));
524 FreeStrBuf(&dav_roomname);
525 FreeStrBuf(&dav_uid);
529 /* If dav_uid is non-empty, client is requesting a PROPFIND on
530 * a specific item in the room. This is not valid GroupDAV, but
531 * it is valid WebDAV (and probably CalDAV too).
533 if (StrLength(dav_uid) != 0) {
535 dav_msgnum = locate_message_by_uid(ChrPtr(dav_uid));
536 if (dav_msgnum < 0) {
537 hprintf("HTTP/1.1 404 not found\r\n");
538 dav_common_headers();
539 hprintf("Content-Type: text/plain\r\n");
540 wc_printf("Object \"%s\" was not found in the \"%s\" folder.\r\n",
545 FreeStrBuf(&dav_roomname);
546 FreeStrBuf(&dav_uid);
550 /* Be rude. Completely ignore the XML request and simply send them
551 * everything we know about (which is going to simply be the ETag and
552 * nothing else). Let the client-side parser sort it out.
554 hprintf("HTTP/1.0 207 Multi-Status\r\n");
555 dav_common_headers();
556 hprintf("Date: %s\r\n", datestring);
557 hprintf("Content-type: text/xml\r\n");
558 if (DisableGzip || (!WCC->Hdr->HR.gzip_ok))
559 hprintf("Content-encoding: identity\r\n");
563 wc_printf("<?xml version=\"1.0\" encoding=\"utf-8\"?>"
564 "<multistatus xmlns=\"DAV:\">"
567 wc_printf("<response>");
571 wc_printf("/groupdav/");
572 urlescputs(ChrPtr(WCC->CurRoom.name));
573 euid_escapize(encoded_uid, ChrPtr(dav_uid));
574 wc_printf("/%s", encoded_uid);
575 wc_printf("</href>");
576 wc_printf("<propstat>");
577 wc_printf("<status>HTTP/1.1 200 OK</status>");
579 wc_printf("<getetag>\"%ld\"</getetag>", dav_msgnum);
580 wc_printf("<getlastmodified>");
582 wc_printf("</getlastmodified>");
583 wc_printf("</prop>");
584 wc_printf("</propstat>");
586 wc_printf("</response>\n");
587 wc_printf("</multistatus>\n");
589 FreeStrBuf(&dav_roomname);
590 FreeStrBuf(&dav_uid);
593 FreeStrBuf(&dav_roomname);
594 FreeStrBuf(&dav_uid);
598 * We got to this point, which means that the client is requesting
599 * a 'collection' (i.e. a list of all items in the room).
601 * Be rude. Completely ignore the XML request and simply send them
602 * everything we know about (which is going to simply be the ETag and
603 * nothing else). Let the client-side parser sort it out.
605 hprintf("HTTP/1.0 207 Multi-Status\r\n");
606 dav_common_headers();
607 hprintf("Date: %s\r\n", datestring);
608 hprintf("Content-type: text/xml\r\n");
609 if (DisableGzip || (!WCC->Hdr->HR.gzip_ok)) {
610 hprintf("Content-encoding: identity\r\n");
614 wc_printf("<?xml version=\"1.0\" encoding=\"utf-8\"?>"
617 "xmlns:G=\"http://groupdav.org/\" "
618 "xmlns:CALDAV=\"urn:ietf:params:xml:ns:caldav\""
622 /* Transmit the collection resource (FIXME check depth and starting point) */
623 wc_printf("<response>");
627 wc_printf("/groupdav/");
628 urlescputs(ChrPtr(WCC->CurRoom.name));
629 wc_printf("</href>");
631 wc_printf("<propstat>");
632 wc_printf("<status>HTTP/1.1 200 OK</status>");
634 wc_printf("<displayname>");
635 escputs(ChrPtr(WCC->CurRoom.name));
636 wc_printf("</displayname>");
638 wc_printf("<owner/>"); /* empty owner ought to be legal; see rfc3744 section 5.1 */
640 wc_printf("<resourcetype><collection/>");
641 switch(WCC->CurRoom.defview) {
643 wc_printf("<G:vevent-collection />");
644 wc_printf("<CALDAV:calendar />");
647 wc_printf("<G:vtodo-collection />");
649 case VIEW_ADDRESSBOOK:
650 wc_printf("<G:vcard-collection />");
653 wc_printf("</resourcetype>");
655 /* FIXME get the mtime
656 wc_printf("<getlastmodified>");
658 wc_printf("</getlastmodified>");
660 wc_printf("</prop>");
661 wc_printf("</propstat>");
662 wc_printf("</response>");
664 /* Transmit the collection listing (FIXME check depth and starting point) */
666 MsgNum = NewStrBuf();
667 serv_puts("MSGS ALL");
669 StrBuf_ServGetln(MsgNum);
670 if (GetServerStatus(MsgNum, NULL) == 1)
671 while (BufLen = StrBuf_ServGetln(MsgNum),
673 ((BufLen != 3) || strcmp(ChrPtr(MsgNum), "000")) ))
675 msgs = realloc(msgs, ++num_msgs * sizeof(long));
676 msgs[num_msgs-1] = StrTol(MsgNum);
679 if (num_msgs > 0) for (i=0; i<num_msgs; ++i) {
681 syslog(LOG_DEBUG, "PROPFIND enumerating message # %ld", msgs[i]);
684 serv_printf("MSG0 %ld|3", msgs[i]);
685 StrBuf_ServGetln(MsgNum);
686 if (GetServerStatus(MsgNum, NULL) == 1)
687 while (BufLen = StrBuf_ServGetln(MsgNum),
689 ((BufLen != 3) || strcmp(ChrPtr(MsgNum), "000")) ))
691 if (!strncasecmp(ChrPtr(MsgNum), "exti=", 5)) {
692 strcpy(uid, &ChrPtr(MsgNum)[5]);
694 else if (!strncasecmp(ChrPtr(MsgNum), "time=", 5)) {
695 now = atol(&ChrPtr(MsgNum)[5]);
699 if (!IsEmptyStr(uid)) {
700 wc_printf("<response>");
703 wc_printf("/groupdav/");
704 urlescputs(ChrPtr(WCC->CurRoom.name));
705 euid_escapize(encoded_uid, uid);
706 wc_printf("/%s", encoded_uid);
707 wc_printf("</href>");
708 switch(WCC->CurRoom.defview) {
710 wc_printf("<getcontenttype>text/x-ical</getcontenttype>");
713 wc_printf("<getcontenttype>text/x-ical</getcontenttype>");
715 case VIEW_ADDRESSBOOK:
716 wc_printf("<getcontenttype>text/x-vcard</getcontenttype>");
719 wc_printf("<propstat>");
720 wc_printf("<status>HTTP/1.1 200 OK</status>");
722 wc_printf("<getetag>\"%ld\"</getetag>", msgs[i]);
724 http_datestring(datestring, sizeof datestring, now);
725 wc_printf("<getlastmodified>");
727 wc_printf("</getlastmodified>");
729 wc_printf("</prop>");
730 wc_printf("</propstat>");
731 wc_printf("</response>");
736 wc_printf("</multistatus>\n");
746 int ParseMessageListHeaders_EUID(StrBuf *Line,
748 message_summary *Msg,
749 StrBuf *ConversionBuffer)
751 Msg->euid = NewStrBuf();
752 StrBufExtract_NextToken(Msg->euid, Line, pos, '|');
753 Msg->date = StrBufExtractNext_long(Line, pos, '|');
755 return StrLength(Msg->euid) > 0;
758 int DavUIDL_GetParamsGetServerCall(SharedMessageStatus *Stat,
764 Stat->defaultsortorder = 0;
767 Stat->maxmsgs = 9999999;
769 snprintf(cmd, len, "MSGS ALL|||2");
773 int DavUIDL_RenderView_or_Tail(SharedMessageStatus *Stat,
778 DoTemplate(HKEY("msg_listview"),NULL,&NoCtx);
783 int DavUIDL_Cleanup(void **ViewSpecific)
785 /* Note: wDumpContent() will output one additional </div> tag. */
786 /* We ought to move this out into template */
799 RegisterReadLoopHandlerset(
801 DavUIDL_GetParamsGetServerCall,
802 NULL, /// TODO: is this right?
803 ParseMessageListHeaders_EUID,
805 DavUIDL_RenderView_or_Tail,