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.
24 #include <sys/types.h>
26 #include <sys/socket.h>
35 #include "webserver.h"
40 * Given an encoded UID, translate that to an unencoded Citadel EUID and
41 * then search for it in the current room. Return a message number or -1
44 * NOTE: this function relies on the Citadel server's brute-force search.
45 * There's got to be a way to optimize this better.
47 long locate_message_by_uid(char *uid) {
49 char decoded_uid[SIZ];
53 euid_unescapize(decoded_uid, uid);
55 serv_puts("MSGS ALL|0|1");
56 serv_getln(buf, sizeof buf);
58 serv_printf("exti|%s", decoded_uid);
60 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
69 * List folders containing interesting groupware objects
71 void groupdav_folder_list(void) {
79 http_datestring(datestring, sizeof datestring, now);
82 * Be rude. Completely ignore the XML request and simply send them
83 * everything we know about. Let the client sort it out.
85 wprintf("HTTP/1.0 207 Multi-Status\r\n");
86 groupdav_common_headers();
87 wprintf("Date: %s\r\n", datestring);
88 wprintf("Content-type: text/xml\r\n");
89 wprintf("Content-encoding: identity\r\n");
93 wprintf("<?xml version=\"1.0\" encoding=\"utf-8\"?>"
94 "<D:multistatus xmlns:D=\"DAV:\" xmlns:G=\"http://groupdav.org/\">"
98 serv_getln(buf, sizeof buf);
99 if (buf[0] == '1') while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
101 extract_token(roomname, buf, 0, '|', sizeof roomname);
102 view = extract_int(buf, 6);
105 * For now, only list rooms that we know a GroupDAV client
106 * might be interested in. In the future we may add
109 if ((view == VIEW_CALENDAR)
110 || (view == VIEW_TASKS)
111 || (view == VIEW_ADDRESSBOOK) ) {
113 wprintf("<D:response>");
116 if (strlen(WC->http_host) > 0) {
118 (is_https ? "https" : "http"),
121 wprintf("/groupdav/");
122 urlescputs(roomname);
123 wprintf("/</D:href>");
125 wprintf("<D:propstat>");
126 wprintf("<D:status>HTTP/1.1 200 OK</D:status>");
128 wprintf("<D:displayname>");
130 wprintf("</D:displayname>");
131 wprintf("<D:resourcetype><D:collection/>");
135 wprintf("<G:vevent-collection />");
138 wprintf("<G:vtodo-collection />");
140 case VIEW_ADDRESSBOOK:
141 wprintf("<G:vcard-collection />");
145 wprintf("</D:resourcetype>");
146 wprintf("</D:prop>");
147 wprintf("</D:propstat>");
148 wprintf("</D:response>");
151 wprintf("</D:multistatus>\n");
159 * The pathname is always going to be /groupdav/room_name/msg_num
161 void groupdav_propfind(char *dav_pathname) {
162 char dav_roomname[256];
165 long dav_msgnum = (-1);
168 char encoded_uid[256];
172 char datestring[256];
176 http_datestring(datestring, sizeof datestring, now);
178 extract_token(dav_roomname, dav_pathname, 2, '/', sizeof dav_roomname);
179 extract_token(dav_uid, dav_pathname, 3, '/', sizeof dav_uid);
181 lprintf(9, "dav_pathname: %s\n", dav_pathname);
182 lprintf(9, "dav_roomname: %s\n", dav_roomname);
183 lprintf(9, " dav_uid: %s\n", dav_uid);
186 * If the room name is blank, the client is requesting a
189 if (strlen(dav_roomname) == 0) {
190 groupdav_folder_list();
194 /* Go to the correct room. */
195 if (strcasecmp(WC->wc_roomname, dav_roomname)) {
196 gotoroom(dav_roomname);
198 if (strcasecmp(WC->wc_roomname, dav_roomname)) {
199 wprintf("HTTP/1.1 404 not found\r\n");
200 groupdav_common_headers();
201 wprintf("Date: %s\r\n", datestring);
203 "Content-Type: text/plain\r\n"
205 "There is no folder called \"%s\" on this server.\r\n",
211 /* If dav_uid is non-empty, client is requesting a PROPFIND on
212 * a specific item in the room. This is not valid GroupDAV, but
213 * we try to honor it anyway because some clients are expecting
216 if (strlen(dav_uid) > 0) {
218 dav_msgnum = locate_message_by_uid(dav_uid);
219 if (dav_msgnum < 0) {
220 wprintf("HTTP/1.1 404 not found\r\n");
221 groupdav_common_headers();
223 "Content-Type: text/plain\r\n"
225 "Object \"%s\" was not found in the \"%s\" folder.\r\n",
232 /* Be rude. Completely ignore the XML request and simply send them
233 * everything we know about (which is going to simply be the ETag and
234 * nothing else). Let the client-side parser sort it out.
236 wprintf("HTTP/1.0 207 Multi-Status\r\n");
237 groupdav_common_headers();
238 wprintf("Date: %s\r\n", datestring);
239 wprintf("Content-type: text/xml\r\n");
240 wprintf("Content-encoding: identity\r\n");
244 wprintf("<?xml version=\"1.0\" encoding=\"utf-8\"?>"
245 "<D:multistatus xmlns:D=\"DAV:\" xmlns:G=\"http://groupdav.org/\">"
248 wprintf("<D:response>");
251 if (strlen(WC->http_host) > 0) {
253 (is_https ? "https" : "http"),
256 wprintf("/groupdav/");
257 urlescputs(WC->wc_roomname);
258 euid_escapize(encoded_uid, dav_uid);
259 wprintf("/%s", encoded_uid);
260 wprintf("</D:href>");
261 wprintf("<D:propstat>");
262 wprintf("<D:status>HTTP/1.1 200 OK</D:status>");
263 wprintf("<D:prop><D:getetag>\"%ld\"</D:getetag></D:prop>", dav_msgnum);
264 wprintf("</D:propstat>");
266 wprintf("</D:response>\n");
267 wprintf("</D:multistatus>\n");
274 * We got to this point, which means that the client is requesting
275 * a 'collection' (i.e. a list of all items in the room).
277 * Be rude. Completely ignore the XML request and simply send them
278 * everything we know about (which is going to simply be the ETag and
279 * nothing else). Let the client-side parser sort it out.
281 wprintf("HTTP/1.0 207 Multi-Status\r\n");
282 groupdav_common_headers();
283 wprintf("Date: %s\r\n", datestring);
284 wprintf("Content-type: text/xml\r\n");
285 wprintf("Content-encoding: identity\r\n");
289 wprintf("<?xml version=\"1.0\" encoding=\"utf-8\"?>"
290 "<D:multistatus xmlns:D=\"DAV:\" xmlns:G=\"http://groupdav.org/\">"
293 serv_puts("MSGS ALL");
294 serv_getln(buf, sizeof buf);
295 if (buf[0] == '1') while (serv_getln(msgnum, sizeof msgnum), strcmp(msgnum, "000")) {
296 msgs = realloc(msgs, ++num_msgs * sizeof(long));
297 msgs[num_msgs-1] = atol(msgnum);
300 if (num_msgs > 0) for (i=0; i<num_msgs; ++i) {
303 serv_printf("MSG0 %ld|3", msgs[i]);
304 serv_getln(buf, sizeof buf);
305 if (buf[0] == '1') while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
306 if (!strncasecmp(buf, "exti=", 5)) {
307 strcpy(uid, &buf[5]);
311 if (strlen(uid) > 0) {
312 wprintf("<D:response>");
314 if (strlen(WC->http_host) > 0) {
316 (is_https ? "https" : "http"),
319 wprintf("/groupdav/");
320 urlescputs(WC->wc_roomname);
321 euid_escapize(encoded_uid, uid);
322 wprintf("/%s", encoded_uid);
323 wprintf("</D:href>");
324 wprintf("<D:propstat>");
325 wprintf("<D:status>HTTP/1.1 200 OK</D:status>");
326 wprintf("<D:prop><D:getetag>\"%ld\"</D:getetag></D:prop>", msgs[i]);
327 wprintf("</D:propstat>");
328 wprintf("</D:response>");
332 wprintf("</D:multistatus>\n");