4 * Handles GroupDAV PROPFIND requests.
6 * A few notes about our XML output:
8 * --> Yes, we are spewing tags directly instead of using an XML library.
9 * Whining about it will be summarily ignored.
11 * --> XML is deliberately output with no whitespace/newlines between tags.
12 * This makes it difficult to read, but we have discovered clients which
13 * crash when you try to pretty it up.
18 #include "webserver.h"
22 * Given an encoded UID, translate that to an unencoded Citadel EUID and
23 * then search for it in the current room. Return a message number or -1
27 long locate_message_by_uid(const char *uid) {
29 char decoded_uid[1024];
33 euid_unescapize(decoded_uid, uid);
35 /* ask Citadel if we have this one */
36 serv_printf("EUID %s", decoded_uid);
37 serv_getln(buf, sizeof buf);
39 retval = atol(&buf[4]);
45 const folder *GetRESTFolder(int IgnoreFloor)
49 const folder *ThisFolder = NULL;
60 itfl = GetNewHashPos(WCC->Floors, 0);
62 while (GetNextHashPos(WCC->Floors, itfl, &len, &Key, &vFolder) &&
66 if (!IgnoreFloor && /* so we can handle legacy URLS... */
67 (ThisFolder->Floor != WCC->CurrentFloor))
71 if (ThisFolder->nRoomNameParts > 1)
73 /*TODO: is that number all right? */
74 if (GetCount(WCC->Directory) - ThisFolder->nRoomNameParts != 2)
77 itd = GetNewHashPos(WCC->Directory, 0);
78 GetNextHashPos(WCC->Directory, itd, &len, &Key, &vDir); //TODO: how many to fast forward?
79 /* Fast forward the floorname we checked above... */
81 (i > ThisFolder->nRoomNameParts) && (j > urlp);
82 i++, j++, GetNextHashPos(WCC->Directory, itd, &len, &Key, &vDir))
85 if (strcmp(ChrPtr(ThisFolder->RoomNameParts[i]),
98 if (GetCount(WCC->Directory) - ThisFolder->nRoomNameParts != 2)
100 itd = GetNewHashPos(WCC->Directory, 0);
103 if (!GetNextHashPos(WCC->Directory,
104 itd, &len, &Key, &vDir) ||
113 Dir = (StrBuf*) vDir;
114 if (strcmp(ChrPtr(ThisFolder->name),
124 DeleteHashPos(&itfl);
130 DeleteHashPos(&itfl);
142 const folder *ThisFolder;
144 State = REST_TOPLEVEL;
146 if (WCC->Hdr->HR.Handler != NULL)
147 State |= REST_IN_NAMESPACE;
149 Count = GetCount(WCC->Directory);
151 if (Count == 0) return State;
153 if (Count >= 1) State |=REST_IN_FLOOR;
154 if (Count == 1) return State;
157 State |= REST_IN_FLOOR;
158 ThisFolder = GetRESTFolder(0);
159 WCC->ThisRoom = ThisFolder;
160 if (ThisFolder != NULL)
162 gotoroom(ThisFolder->name);
163 State |= REST_IN_ROOM;
171 * More than 3 params and no floor found?
172 * -> fall back to old non-floored notation
175 if ((Count >= 3) && (WCC->CurrentFloor == NULL))
177 ThisFolder = GetRESTFolder(1);
178 WCC->ThisRoom = ThisFolder;
179 if (ThisFolder != NULL)
181 gotoroom(ThisFolder->name);
182 State |= REST_IN_ROOM;
190 if (Count == 3) return State;
192 /// TODO: ID detection
193 /// TODO: File detection
202 * List rooms (or "collections" in DAV terminology) which contain
203 * interesting groupware objects.
205 void groupdav_collection_list(void)
211 char datestring[256];
214 int is_groupware_collection = 0;
215 int starting_point = 1; /**< 0 for /, 1 for /groupdav/ */
217 if (WCC->Hdr->HR.Handler == NULL) {
220 else if (StrLength(WCC->Hdr->HR.ReqLine) == 0) {
228 http_datestring(datestring, sizeof datestring, now);
231 * Be rude. Completely ignore the XML request and simply send them
232 * everything we know about. Let the client sort it out.
234 hprintf("HTTP/1.0 207 Multi-Status\r\n");
235 groupdav_common_headers();
236 hprintf("Date: %s\r\n", datestring);
237 hprintf("Content-type: text/xml\r\n");
238 hprintf("Content-encoding: identity\r\n");
242 wc_printf("<?xml version=\"1.0\" encoding=\"utf-8\"?>"
243 "<multistatus xmlns=\"DAV:\" xmlns:G=\"http://groupdav.org/\">"
247 * If the client is requesting the root, show a root node.
249 if (starting_point == 0) {
250 wc_printf("<response>");
252 groupdav_identify_host();
254 wc_printf("</href>");
255 wc_printf("<propstat>");
256 wc_printf("<status>HTTP/1.1 200 OK</status>");
258 wc_printf("<displayname>/</displayname>");
259 wc_printf("<resourcetype><collection/></resourcetype>");
260 wc_printf("<getlastmodified>");
262 wc_printf("</getlastmodified>");
263 wc_printf("</prop>");
264 wc_printf("</propstat>");
265 wc_printf("</response>");
269 * If the client is requesting "/groupdav", show a /groupdav subdirectory.
271 if ((starting_point + WCC->Hdr->HR.dav_depth) >= 1) {
272 wc_printf("<response>");
274 groupdav_identify_host();
275 wc_printf("/groupdav");
276 wc_printf("</href>");
277 wc_printf("<propstat>");
278 wc_printf("<status>HTTP/1.1 200 OK</status>");
280 wc_printf("<displayname>GroupDAV</displayname>");
281 wc_printf("<resourcetype><collection/></resourcetype>");
282 wc_printf("<getlastmodified>");
284 wc_printf("</getlastmodified>");
285 wc_printf("</prop>");
286 wc_printf("</propstat>");
287 wc_printf("</response>");
291 * Now go through the list and make it look like a DAV collection
294 serv_getln(buf, sizeof buf);
295 if (buf[0] == '1') while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
297 extract_token(roomname, buf, 0, '|', sizeof roomname);
298 view = extract_int(buf, 7);
299 mtime = extract_long(buf, 8);
300 http_datestring(datestring, sizeof datestring, mtime);
303 * For now, only list rooms that we know a GroupDAV client
304 * might be interested in. In the future we may add
307 * We determine the type of objects which are stored in each
308 * room by looking at the *default* view for the room. This
309 * allows, for example, a Calendar room to appear as a
310 * GroupDAV calendar even if the user has switched it to a
311 * Calendar List view.
313 if ( (view == VIEW_CALENDAR) ||
314 (view == VIEW_TASKS) ||
315 (view == VIEW_ADDRESSBOOK) ||
316 (view == VIEW_NOTES) ||
317 (view == VIEW_JOURNAL) ||
320 is_groupware_collection = 1;
323 is_groupware_collection = 0;
326 if ( (is_groupware_collection) && ((starting_point + WCC->Hdr->HR.dav_depth) >= 2) ) {
327 wc_printf("<response>");
330 groupdav_identify_host();
331 wc_printf("/groupdav/");
332 urlescputs(roomname);
333 wc_printf("/</href>");
335 wc_printf("<propstat>");
336 wc_printf("<status>HTTP/1.1 200 OK</status>");
338 wc_printf("<displayname>");
340 wc_printf("</displayname>");
341 wc_printf("<resourcetype><collection/>");
345 wc_printf("<G:vevent-collection />");
348 wc_printf("<G:vtodo-collection />");
350 case VIEW_ADDRESSBOOK:
351 wc_printf("<G:vcard-collection />");
354 wc_printf("<G:vnotes-collection />");
357 wc_printf("<G:vjournal-collection />");
360 wc_printf("<G:wiki-collection />");
364 wc_printf("</resourcetype>");
365 wc_printf("<getlastmodified>");
367 wc_printf("</getlastmodified>");
368 wc_printf("</prop>");
369 wc_printf("</propstat>");
370 wc_printf("</response>");
373 wc_printf("</multistatus>\n");
381 * The pathname is always going to be /groupdav/room_name/msg_num
383 void groupdav_propfind(void)
386 StrBuf *dav_roomname;
390 long dav_msgnum = (-1);
392 char encoded_uid[256];
396 char datestring[256];
401 http_datestring(datestring, sizeof datestring, now);
403 dav_roomname = NewStrBuf();
404 dav_uid = NewStrBuf();
405 StrBufExtract_token(dav_roomname, WCC->Hdr->HR.ReqLine, 0, '/');
406 StrBufExtract_token(dav_uid, WCC->Hdr->HR.ReqLine, 1, '/');
409 * If the room name is blank, the client is requesting a
412 State = GotoRestRoom();
413 if (((State & REST_IN_ROOM) == 0) ||
414 (((State & (REST_GOT_EUID|REST_GOT_ID|REST_GOT_FILENAME)) == 0) &&
415 (WCC->Hdr->HR.dav_depth == 0)))
418 http_datestring(datestring, sizeof datestring, now);
421 * Be rude. Completely ignore the XML request and simply send them
422 * everything we know about. Let the client sort it out.
424 hprintf("HTTP/1.0 207 Multi-Status\r\n");
425 groupdav_common_headers();
426 hprintf("Date: %s\r\n", datestring);
427 hprintf("Content-type: text/xml\r\n");
428 hprintf("Content-encoding: identity\r\n");
434 * If the client is requesting the root, show a root node.
436 do_template("dav_propfind_top", NULL);
438 FreeStrBuf(&dav_roomname);
439 FreeStrBuf(&dav_uid);
443 if ((State & (REST_GOT_EUID|REST_GOT_ID|REST_GOT_FILENAME)) == 0) {
444 readloop(headers, eReadEUIDS);
451 * If the room name is blank, the client is requesting a
454 if (StrLength(dav_roomname) == 0) {
455 groupdav_collection_list();
456 FreeStrBuf(&dav_roomname);
457 FreeStrBuf(&dav_uid);
461 /* Go to the correct room. */
462 if (strcasecmp(ChrPtr(WCC->CurRoom.name), ChrPtr(dav_roomname))) {
463 gotoroom(dav_roomname);
465 if (strcasecmp(ChrPtr(WCC->CurRoom.name), ChrPtr(dav_roomname))) {
466 hprintf("HTTP/1.1 404 not found\r\n");
467 groupdav_common_headers();
468 hprintf("Date: %s\r\n", datestring);
469 hprintf("Content-Type: text/plain\r\n");
470 wc_printf("There is no folder called \"%s\" on this server.\r\n",
474 FreeStrBuf(&dav_roomname);
475 FreeStrBuf(&dav_uid);
479 /* If dav_uid is non-empty, client is requesting a PROPFIND on
480 * a specific item in the room. This is not valid GroupDAV, but
481 * it is valid WebDAV.
483 if (StrLength(dav_uid) != 0) {
485 dav_msgnum = locate_message_by_uid(ChrPtr(dav_uid));
486 if (dav_msgnum < 0) {
487 hprintf("HTTP/1.1 404 not found\r\n");
488 groupdav_common_headers();
489 hprintf("Content-Type: text/plain\r\n");
490 wc_printf("Object \"%s\" was not found in the \"%s\" folder.\r\n",
495 FreeStrBuf(&dav_roomname);
496 FreeStrBuf(&dav_uid);
500 /* Be rude. Completely ignore the XML request and simply send them
501 * everything we know about (which is going to simply be the ETag and
502 * nothing else). Let the client-side parser sort it out.
504 hprintf("HTTP/1.0 207 Multi-Status\r\n");
505 groupdav_common_headers();
506 hprintf("Date: %s\r\n", datestring);
507 hprintf("Content-type: text/xml\r\n");
508 hprintf("Content-encoding: identity\r\n");
512 wc_printf("<?xml version=\"1.0\" encoding=\"utf-8\"?>"
513 "<multistatus xmlns=\"DAV:\">"
516 wc_printf("<response>");
519 groupdav_identify_host();
520 wc_printf("/groupdav/");
521 urlescputs(ChrPtr(WCC->CurRoom.name));
522 euid_escapize(encoded_uid, ChrPtr(dav_uid));
523 wc_printf("/%s", encoded_uid);
524 wc_printf("</href>");
525 wc_printf("<propstat>");
526 wc_printf("<status>HTTP/1.1 200 OK</status>");
528 wc_printf("<getetag>\"%ld\"</getetag>", dav_msgnum);
529 wc_printf("<getlastmodified>");
531 wc_printf("</getlastmodified>");
532 wc_printf("</prop>");
533 wc_printf("</propstat>");
535 wc_printf("</response>\n");
536 wc_printf("</multistatus>\n");
538 FreeStrBuf(&dav_roomname);
539 FreeStrBuf(&dav_uid);
542 FreeStrBuf(&dav_roomname);
543 FreeStrBuf(&dav_uid);
547 * We got to this point, which means that the client is requesting
548 * a 'collection' (i.e. a list of all items in the room).
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 groupdav_common_headers();
556 hprintf("Date: %s\r\n", datestring);
557 hprintf("Content-type: text/xml\r\n");
558 hprintf("Content-encoding: identity\r\n");
562 wc_printf("<?xml version=\"1.0\" encoding=\"utf-8\"?>"
563 "<multistatus xmlns=\"DAV:\" xmlns:G=\"http://groupdav.org/\">"
567 /* Transmit the collection resource (FIXME check depth and starting point) */
568 wc_printf("<response>");
571 groupdav_identify_host();
572 wc_printf("/groupdav/");
573 urlescputs(ChrPtr(WCC->CurRoom.name));
574 wc_printf("</href>");
576 wc_printf("<propstat>");
577 wc_printf("<status>HTTP/1.1 200 OK</status>");
579 wc_printf("<displayname>");
580 escputs(ChrPtr(WCC->CurRoom.name));
581 wc_printf("</displayname>");
582 wc_printf("<resourcetype><collection/>");
584 switch(WCC->CurRoom.defview) {
586 wc_printf("<G:vevent-collection />");
589 wc_printf("<G:vtodo-collection />");
591 case VIEW_ADDRESSBOOK:
592 wc_printf("<G:vcard-collection />");
596 wc_printf("</resourcetype>");
597 /* FIXME get the mtime
598 wc_printf("<getlastmodified>");
600 wc_printf("</getlastmodified>");
602 wc_printf("</prop>");
603 wc_printf("</propstat>");
604 wc_printf("</response>");
606 /* Transmit the collection listing (FIXME check depth and starting point) */
608 MsgNum = NewStrBuf();
609 serv_puts("MSGS ALL");
611 StrBuf_ServGetln(MsgNum);
612 if (GetServerStatus(MsgNum, NULL) == 1)
613 while (BufLen = StrBuf_ServGetln(MsgNum), strcmp(ChrPtr(MsgNum), "000")) {
614 msgs = realloc(msgs, ++num_msgs * sizeof(long));
615 msgs[num_msgs-1] = StrTol(MsgNum);
618 if (num_msgs > 0) for (i=0; i<num_msgs; ++i) {
622 serv_printf("MSG0 %ld|3", msgs[i]);
623 StrBuf_ServGetln(MsgNum);
624 if (GetServerStatus(MsgNum, NULL) == 1)
625 while (BufLen = StrBuf_ServGetln(MsgNum), strcmp(ChrPtr(MsgNum), "000"))
627 if (!strncasecmp(ChrPtr(MsgNum), "exti=", 5)) {
628 strcpy(uid, &ChrPtr(MsgNum)[5]);
630 else if (!strncasecmp(ChrPtr(MsgNum), "time=", 5)) {
631 now = atol(&ChrPtr(MsgNum)[5]);
635 if (!IsEmptyStr(uid)) {
636 wc_printf("<response>");
638 groupdav_identify_host();
639 wc_printf("/groupdav/");
640 urlescputs(ChrPtr(WCC->CurRoom.name));
641 euid_escapize(encoded_uid, uid);
642 wc_printf("/%s", encoded_uid);
643 wc_printf("</href>");
644 switch(WCC->CurRoom.defview) {
646 wc_printf("<getcontenttype>text/x-ical</getcontenttype>");
649 wc_printf("<getcontenttype>text/x-ical</getcontenttype>");
651 case VIEW_ADDRESSBOOK:
652 wc_printf("<getcontenttype>text/x-vcard</getcontenttype>");
655 wc_printf("<propstat>");
656 wc_printf("<status>HTTP/1.1 200 OK</status>");
658 wc_printf("<getetag>\"%ld\"</getetag>", msgs[i]);
660 http_datestring(datestring, sizeof datestring, now);
661 wc_printf("<getlastmodified>");
663 wc_printf("</getlastmodified>");
665 wc_printf("</prop>");
666 wc_printf("</propstat>");
667 wc_printf("</response>");
672 wc_printf("</multistatus>\n");
682 int ParseMessageListHeaders_EUID(StrBuf *Line,
684 message_summary *Msg,
685 StrBuf *ConversionBuffer)
687 Msg->euid = NewStrBuf();
688 StrBufExtract_NextToken(Msg->euid, Line, pos, '|');
689 return StrLength(Msg->euid) > 0;
692 int DavUIDL_GetParamsGetServerCall(SharedMessageStatus *Stat,
698 Stat->defaultsortorder = 0;
701 Stat->maxmsgs = 9999999;
703 snprintf(cmd, len, "MSGS ALL|||2");
707 int DavUIDL_RenderView_or_Tail(SharedMessageStatus *Stat,
712 DoTemplate(HKEY("msg_listview"),NULL,&NoCtx);
717 int DavUIDL_Cleanup(void **ViewSpecific)
719 /* Note: wDumpContent() will output one additional </div> tag. */
720 /* We ought to move this out into template */
733 RegisterReadLoopHandlerset(
735 DavUIDL_GetParamsGetServerCall,
736 NULL, /// TODO: is this right?
737 ParseMessageListHeaders_EUID,
739 DavUIDL_RenderView_or_Tail,