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"
24 * Given an encoded UID, translate that to an unencoded Citadel EUID and
25 * then search for it in the current room. Return a message number or -1
28 * NOTE: this function relies on the Citadel server's brute-force search.
29 * There's got to be a way to optimize this better.
31 long locate_message_by_uid(char *uid) {
33 char decoded_uid[SIZ];
37 euid_unescapize(decoded_uid, uid);
39 serv_printf("EUID %s", decoded_uid);
40 serv_getln(buf, sizeof buf);
42 retval = extract_long(&buf[4], 0);
49 * List folders containing interesting groupware objects
51 void groupdav_folder_list(void) {
59 http_datestring(datestring, sizeof datestring, now);
62 * Be rude. Completely ignore the XML request and simply send them
63 * everything we know about. Let the client sort it out.
65 wprintf("HTTP/1.1 207 Multi-Status\r\n");
66 groupdav_common_headers();
67 wprintf("Date: %s\r\n", datestring);
68 wprintf("Content-type: text/xml\r\n");
69 wprintf("Content-encoding: identity\r\n");
73 wprintf("<?xml version=\"1.0\" encoding=\"utf-8\"?>"
74 "<D:multistatus xmlns:D=\"DAV:\" xmlns:G=\"http://groupdav.org/\">"
78 serv_getln(buf, sizeof buf);
79 if (buf[0] == '1') while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
81 extract_token(roomname, buf, 0, '|', sizeof roomname);
82 view = extract_int(buf, 6);
85 * For now, only list rooms that we know a GroupDAV client
86 * might be interested in. In the future we may add
89 if ((view == VIEW_CALENDAR)
90 || (view == VIEW_TASKS)
91 || (view == VIEW_ADDRESSBOOK) ) {
93 wprintf("<D:response>");
96 if (strlen(WC->http_host) > 0) {
98 (is_https ? "https" : "http"),
101 wprintf("/groupdav/");
102 urlescputs(roomname);
103 wprintf("/</D:href>");
105 wprintf("<D:propstat>");
106 wprintf("<D:status>HTTP/1.1 200 OK</D:status>");
108 wprintf("<D:displayname>");
110 wprintf("</D:displayname>");
111 wprintf("<D:resourcetype><D:collection/>");
115 wprintf("<G:vevent-collection />");
118 wprintf("<G:vtodo-collection />");
120 case VIEW_ADDRESSBOOK:
121 wprintf("<G:vcard-collection />");
125 wprintf("</D:resourcetype>");
126 wprintf("</D:prop>");
127 wprintf("</D:propstat>");
128 wprintf("</D:response>");
131 wprintf("</D:multistatus>\n");
139 * The pathname is always going to be /groupdav/room_name/msg_num
141 void groupdav_propfind(char *dav_pathname) {
142 char dav_roomname[256];
145 long dav_msgnum = (-1);
148 char encoded_uid[256];
152 char datestring[256];
156 http_datestring(datestring, sizeof datestring, now);
158 extract_token(dav_roomname, dav_pathname, 2, '/', sizeof dav_roomname);
159 extract_token(dav_uid, dav_pathname, 3, '/', sizeof dav_uid);
161 lprintf(9, "dav_pathname: %s\n", dav_pathname);
162 lprintf(9, "dav_roomname: %s\n", dav_roomname);
163 lprintf(9, " dav_uid: %s\n", dav_uid);
166 * If the room name is blank, the client is requesting a
169 if (strlen(dav_roomname) == 0) {
170 groupdav_folder_list();
174 /* Go to the correct room. */
175 if (strcasecmp(WC->wc_roomname, dav_roomname)) {
176 gotoroom(dav_roomname);
178 if (strcasecmp(WC->wc_roomname, dav_roomname)) {
179 wprintf("HTTP/1.1 404 not found\r\n");
180 groupdav_common_headers();
181 wprintf("Date: %s\r\n", datestring);
183 "Content-Type: text/plain\r\n"
185 "There is no folder called \"%s\" on this server.\r\n",
191 /* If dav_uid is non-empty, client is requesting a PROPFIND on
192 * a specific item in the room. This is not valid GroupDAV, but
193 * we try to honor it anyway because some clients are expecting
196 if (strlen(dav_uid) > 0) {
198 dav_msgnum = locate_message_by_uid(dav_uid);
199 if (dav_msgnum < 0) {
200 wprintf("HTTP/1.1 404 not found\r\n");
201 groupdav_common_headers();
203 "Content-Type: text/plain\r\n"
205 "Object \"%s\" was not found in the \"%s\" folder.\r\n",
212 /* Be rude. Completely ignore the XML request and simply send them
213 * everything we know about (which is going to simply be the ETag and
214 * nothing else). Let the client-side parser sort it out.
216 wprintf("HTTP/1.1 207 Multi-Status\r\n");
217 groupdav_common_headers();
218 wprintf("Date: %s\r\n", datestring);
219 wprintf("Content-type: text/xml\r\n");
220 wprintf("Content-encoding: identity\r\n");
224 wprintf("<?xml version=\"1.0\" encoding=\"utf-8\"?>"
225 "<D:multistatus xmlns:D=\"DAV:\" xmlns:G=\"http://groupdav.org/\">"
228 wprintf("<D:response>");
231 if (strlen(WC->http_host) > 0) {
233 (is_https ? "https" : "http"),
236 wprintf("/groupdav/");
237 urlescputs(WC->wc_roomname);
238 euid_escapize(encoded_uid, dav_uid);
239 wprintf("/%s", encoded_uid);
240 wprintf("</D:href>");
241 wprintf("<D:propstat>");
242 wprintf("<D:status>HTTP/1.1 200 OK</D:status>");
243 wprintf("<D:prop><D:getetag>\"%ld\"</D:getetag></D:prop>", dav_msgnum);
244 wprintf("</D:propstat>");
246 wprintf("</D:response>\n");
247 wprintf("</D:multistatus>\n");
254 * We got to this point, which means that the client is requesting
255 * a 'collection' (i.e. a list of all items in the room).
257 * Be rude. Completely ignore the XML request and simply send them
258 * everything we know about (which is going to simply be the ETag and
259 * nothing else). Let the client-side parser sort it out.
261 wprintf("HTTP/1.1 207 Multi-Status\r\n");
262 groupdav_common_headers();
263 wprintf("Date: %s\r\n", datestring);
264 wprintf("Content-type: text/xml\r\n");
265 wprintf("Content-encoding: identity\r\n");
269 wprintf("<?xml version=\"1.0\" encoding=\"utf-8\"?>"
270 "<D:multistatus xmlns:D=\"DAV:\" xmlns:G=\"http://groupdav.org/\">"
273 serv_puts("MSGS ALL");
274 serv_getln(buf, sizeof buf);
275 if (buf[0] == '1') while (serv_getln(msgnum, sizeof msgnum), strcmp(msgnum, "000")) {
276 msgs = realloc(msgs, ++num_msgs * sizeof(long));
277 msgs[num_msgs-1] = atol(msgnum);
280 if (num_msgs > 0) for (i=0; i<num_msgs; ++i) {
283 serv_printf("MSG0 %ld|3", msgs[i]);
284 serv_getln(buf, sizeof buf);
285 if (buf[0] == '1') while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
286 if (!strncasecmp(buf, "exti=", 5)) {
287 strcpy(uid, &buf[5]);
291 if (strlen(uid) > 0) {
292 wprintf("<D:response>");
294 if (strlen(WC->http_host) > 0) {
296 (is_https ? "https" : "http"),
299 wprintf("/groupdav/");
300 urlescputs(WC->wc_roomname);
301 euid_escapize(encoded_uid, uid);
302 wprintf("/%s", encoded_uid);
303 wprintf("</D:href>");
304 wprintf("<D:propstat>");
305 wprintf("<D:status>HTTP/1.1 200 OK</D:status>");
306 wprintf("<D:prop><D:getetag>\"%ld\"</D:getetag></D:prop>", msgs[i]);
307 wprintf("</D:propstat>");
308 wprintf("</D:response>");
312 wprintf("</D:multistatus>\n");