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 * If you would like to rewrite this using libxml2, code it up and submit
10 * a patch. Whining will be summarily ignored.
12 * --> XML is deliberately output with no whitespace/newlines between tags.
13 * This makes it difficult to read, but we have discovered clients which
14 * crash when you try to pretty it up.
19 #include "webserver.h"
23 * Given an encoded UID, translate that to an unencoded Citadel EUID and
24 * then search for it in the current room. Return a message number or -1
28 long locate_message_by_uid(const char *uid
) {
30 char decoded_uid
[1024];
34 euid_unescapize(decoded_uid
, uid
);
36 /************** THE NEW WAY ***********************/
37 serv_printf("EUID %s", decoded_uid
);
38 serv_getln(buf
, sizeof buf
);
40 retval
= atol(&buf
[4]);
42 /***************************************************/
44 /************** THE OLD WAY ***********************
45 serv_puts("MSGS ALL|0|1");
46 serv_getln(buf, sizeof buf);
48 serv_printf("exti|%s", decoded_uid);
50 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
54 ***************************************************/
62 * List rooms (or "collections" in DAV terminology) which contain
63 * interesting groupware objects.
65 void groupdav_collection_list(const char *dav_pathname
, int dav_depth
)
73 int is_groupware_collection
= 0;
74 int starting_point
= 1; /**< 0 for /, 1 for /groupdav/ */
76 if (!strcmp(dav_pathname
, "/")) {
79 else if (!strcasecmp(dav_pathname
, "/groupdav")) {
82 else if (!strcasecmp(dav_pathname
, "/groupdav/")) {
85 else if ( (!strncasecmp(dav_pathname
, "/groupdav/", 10)) && (strlen(dav_pathname
) > 10) ) {
90 http_datestring(datestring
, sizeof datestring
, now
);
93 * Be rude. Completely ignore the XML request and simply send them
94 * everything we know about. Let the client sort it out.
96 hprintf("HTTP/1.0 207 Multi-Status\r\n");
97 groupdav_common_headers();
98 hprintf("Date: %s\r\n", datestring
);
99 hprintf("Content-type: text/xml\r\n");
100 hprintf("Content-encoding: identity\r\n");
104 wprintf("<?xml version=\"1.0\" encoding=\"utf-8\"?>"
105 "<multistatus xmlns=\"DAV:\" xmlns:G=\"http://groupdav.org/\">"
109 * If the client is requesting the root, show a root node.
111 if (starting_point
== 0) {
112 wprintf("<response>");
114 groupdav_identify_host();
117 wprintf("<propstat>");
118 wprintf("<status>HTTP/1.1 200 OK</status>");
120 wprintf("<displayname>/</displayname>");
121 wprintf("<resourcetype><collection/></resourcetype>");
122 wprintf("<getlastmodified>");
124 wprintf("</getlastmodified>");
126 wprintf("</propstat>");
127 wprintf("</response>");
131 * If the client is requesting "/groupdav", show a /groupdav subdirectory.
133 if ((starting_point
+ dav_depth
) >= 1) {
134 wprintf("<response>");
136 groupdav_identify_host();
137 wprintf("/groupdav");
139 wprintf("<propstat>");
140 wprintf("<status>HTTP/1.1 200 OK</status>");
142 wprintf("<displayname>GroupDAV</displayname>");
143 wprintf("<resourcetype><collection/></resourcetype>");
144 wprintf("<getlastmodified>");
146 wprintf("</getlastmodified>");
148 wprintf("</propstat>");
149 wprintf("</response>");
153 * Now go through the list and make it look like a DAV collection
156 serv_getln(buf
, sizeof buf
);
157 if (buf
[0] == '1') while (serv_getln(buf
, sizeof buf
), strcmp(buf
, "000")) {
159 extract_token(roomname
, buf
, 0, '|', sizeof roomname
);
160 view
= extract_int(buf
, 7);
161 mtime
= extract_long(buf
, 8);
162 http_datestring(datestring
, sizeof datestring
, mtime
);
165 * For now, only list rooms that we know a GroupDAV client
166 * might be interested in. In the future we may add
169 * We determine the type of objects which are stored in each
170 * room by looking at the *default* view for the room. This
171 * allows, for example, a Calendar room to appear as a
172 * GroupDAV calendar even if the user has switched it to a
173 * Calendar List view.
175 if ((view
== VIEW_CALENDAR
) || (view
== VIEW_TASKS
) || (view
== VIEW_ADDRESSBOOK
) ) {
176 is_groupware_collection
= 1;
179 is_groupware_collection
= 0;
182 if ( (is_groupware_collection
) && ((starting_point
+ dav_depth
) >= 2) ) {
183 wprintf("<response>");
186 groupdav_identify_host();
187 wprintf("/groupdav/");
188 urlescputs(roomname
);
191 wprintf("<propstat>");
192 wprintf("<status>HTTP/1.1 200 OK</status>");
194 wprintf("<displayname>");
196 wprintf("</displayname>");
197 wprintf("<resourcetype><collection/>");
201 wprintf("<G:vevent-collection />");
204 wprintf("<G:vtodo-collection />");
206 case VIEW_ADDRESSBOOK
:
207 wprintf("<G:vcard-collection />");
211 wprintf("</resourcetype>");
212 wprintf("<getlastmodified>");
214 wprintf("</getlastmodified>");
216 wprintf("</propstat>");
217 wprintf("</response>");
220 wprintf("</multistatus>\n");
228 * The pathname is always going to be /groupdav/room_name/msg_num
230 void groupdav_propfind(StrBuf
*dav_pathname
, int dav_depth
, StrBuf
*dav_content_type
, StrBuf
*dav_content
, int offset
) {
231 StrBuf
*dav_roomname
;
234 long dav_msgnum
= (-1);
237 char encoded_uid
[256];
241 char datestring
[256];
245 http_datestring(datestring
, sizeof datestring
, now
);
247 dav_roomname
= NewStrBuf();
248 dav_uid
= NewStrBuf();
249 StrBufExtract_token(dav_roomname
, dav_pathname
, 2, '/');
250 StrBufExtract_token(dav_uid
, dav_pathname
, 3, '/');
253 * If the room name is blank, the client is requesting a
256 if (StrLength(dav_roomname
) == 0) {
257 groupdav_collection_list(ChrPtr(dav_pathname
), dav_depth
);
258 FreeStrBuf(&dav_roomname
);
259 FreeStrBuf(&dav_uid
);
263 /* Go to the correct room. */
264 if (strcasecmp(ChrPtr(WC
->wc_roomname
), ChrPtr(dav_roomname
))) {
265 gotoroom(dav_roomname
);
267 if (strcasecmp(ChrPtr(WC
->wc_roomname
), ChrPtr(dav_roomname
))) {
268 hprintf("HTTP/1.1 404 not found\r\n");
269 groupdav_common_headers();
270 hprintf("Date: %s\r\n", datestring
);
271 hprintf("Content-Type: text/plain\r\n");
272 wprintf("There is no folder called \"%s\" on this server.\r\n",
276 FreeStrBuf(&dav_roomname
);
277 FreeStrBuf(&dav_uid
);
281 /* If dav_uid is non-empty, client is requesting a PROPFIND on
282 * a specific item in the room. This is not valid GroupDAV, but
283 * it is valid WebDAV.
285 if (StrLength(dav_uid
) != 0) {
287 dav_msgnum
= locate_message_by_uid(ChrPtr(dav_uid
));
288 if (dav_msgnum
< 0) {
289 hprintf("HTTP/1.1 404 not found\r\n");
290 groupdav_common_headers();
291 hprintf("Content-Type: text/plain\r\n");
292 wprintf("Object \"%s\" was not found in the \"%s\" folder.\r\n",
297 FreeStrBuf(&dav_roomname
);
298 FreeStrBuf(&dav_uid
);
302 /* Be rude. Completely ignore the XML request and simply send them
303 * everything we know about (which is going to simply be the ETag and
304 * nothing else). Let the client-side parser sort it out.
306 hprintf("HTTP/1.0 207 Multi-Status\r\n");
307 groupdav_common_headers();
308 hprintf("Date: %s\r\n", datestring
);
309 hprintf("Content-type: text/xml\r\n");
310 hprintf("Content-encoding: identity\r\n");
314 wprintf("<?xml version=\"1.0\" encoding=\"utf-8\"?>"
315 "<multistatus xmlns=\"DAV:\">"
318 wprintf("<response>");
321 groupdav_identify_host();
322 wprintf("/groupdav/");
323 urlescputs(ChrPtr(WC
->wc_roomname
));
324 euid_escapize(encoded_uid
, ChrPtr(dav_uid
));
325 wprintf("/%s", encoded_uid
);
327 wprintf("<propstat>");
328 wprintf("<status>HTTP/1.1 200 OK</status>");
330 wprintf("<getetag>\"%ld\"</getetag>", dav_msgnum
);
331 wprintf("<getlastmodified>");
333 wprintf("</getlastmodified>");
335 wprintf("</propstat>");
337 wprintf("</response>\n");
338 wprintf("</multistatus>\n");
340 FreeStrBuf(&dav_roomname
);
341 FreeStrBuf(&dav_uid
);
344 FreeStrBuf(&dav_roomname
);
345 FreeStrBuf(&dav_uid
);
349 * We got to this point, which means that the client is requesting
350 * a 'collection' (i.e. a list of all items in the room).
352 * Be rude. Completely ignore the XML request and simply send them
353 * everything we know about (which is going to simply be the ETag and
354 * nothing else). Let the client-side parser sort it out.
356 hprintf("HTTP/1.0 207 Multi-Status\r\n");
357 groupdav_common_headers();
358 hprintf("Date: %s\r\n", datestring
);
359 hprintf("Content-type: text/xml\r\n");
360 hprintf("Content-encoding: identity\r\n");
364 wprintf("<?xml version=\"1.0\" encoding=\"utf-8\"?>"
365 "<multistatus xmlns=\"DAV:\" xmlns:G=\"http://groupdav.org/\">"
369 /** Transmit the collection resource (FIXME check depth and starting point) */
370 wprintf("<response>");
373 groupdav_identify_host();
374 wprintf("/groupdav/");
375 urlescputs(ChrPtr(WC
->wc_roomname
));
378 wprintf("<propstat>");
379 wprintf("<status>HTTP/1.1 200 OK</status>");
381 wprintf("<displayname>");
382 escputs(ChrPtr(WC
->wc_roomname
));
383 wprintf("</displayname>");
384 wprintf("<resourcetype><collection/>");
386 switch(WC
->wc_default_view
) {
388 wprintf("<G:vevent-collection />");
391 wprintf("<G:vtodo-collection />");
393 case VIEW_ADDRESSBOOK
:
394 wprintf("<G:vcard-collection />");
398 wprintf("</resourcetype>");
399 /* FIXME get the mtime
400 wprintf("<getlastmodified>");
402 wprintf("</getlastmodified>");
405 wprintf("</propstat>");
406 wprintf("</response>");
408 /** Transmit the collection listing (FIXME check depth and starting point) */
410 serv_puts("MSGS ALL");
411 serv_getln(buf
, sizeof buf
);
412 if (buf
[0] == '1') while (serv_getln(msgnum
, sizeof msgnum
), strcmp(msgnum
, "000")) {
413 msgs
= realloc(msgs
, ++num_msgs
* sizeof(long));
414 msgs
[num_msgs
-1] = atol(msgnum
);
417 if (num_msgs
> 0) for (i
=0; i
<num_msgs
; ++i
) {
421 serv_printf("MSG0 %ld|3", msgs
[i
]);
422 serv_getln(buf
, sizeof buf
);
423 if (buf
[0] == '1') while (serv_getln(buf
, sizeof buf
), strcmp(buf
, "000")) {
424 if (!strncasecmp(buf
, "exti=", 5)) {
425 strcpy(uid
, &buf
[5]);
427 else if (!strncasecmp(buf
, "time=", 5)) {
432 if (!IsEmptyStr(uid
)) {
433 wprintf("<response>");
435 groupdav_identify_host();
436 wprintf("/groupdav/");
437 urlescputs(ChrPtr(WC
->wc_roomname
));
438 euid_escapize(encoded_uid
, uid
);
439 wprintf("/%s", encoded_uid
);
441 switch(WC
->wc_default_view
) {
443 wprintf("<getcontenttype>text/x-ical</getcontenttype>");
446 wprintf("<getcontenttype>text/x-ical</getcontenttype>");
448 case VIEW_ADDRESSBOOK
:
449 wprintf("<getcontenttype>text/x-vcard</getcontenttype>");
452 wprintf("<propstat>");
453 wprintf("<status>HTTP/1.1 200 OK</status>");
455 wprintf("<getetag>\"%ld\"</getetag>", msgs
[i
]);
457 http_datestring(datestring
, sizeof datestring
, now
);
458 wprintf("<getlastmodified>");
460 wprintf("</getlastmodified>");
463 wprintf("</propstat>");
464 wprintf("</response>");
468 wprintf("</multistatus>\n");