4 * Handles GroupDAV PUT requests.
14 #include <sys/types.h>
16 #include <sys/socket.h>
25 #include "webserver.h"
30 * The pathname is always going to be /groupdav/room_name/euid
32 void groupdav_put(char *dav_pathname, char *dav_ifmatch,
33 char *supplied_content_type, char *dav_content
35 char dav_roomname[SIZ];
37 char dav_content_type[SIZ];
38 long new_msgnum = (-2L);
39 long old_msgnum = (-1L);
43 /* First, break off the "/groupdav/" prefix */
44 remove_token(dav_pathname, 0, '/');
45 remove_token(dav_pathname, 0, '/');
47 /* Now extract the message euid */
48 n = num_tokens(dav_pathname, '/');
49 extract_token(dav_uid, dav_pathname, n-1, '/');
50 remove_token(dav_pathname, n-1, '/');
52 /* What's left is the room name. Remove trailing slashes. */
53 if (dav_pathname[strlen(dav_pathname)-1] == '/') {
54 dav_pathname[strlen(dav_pathname)-1] = 0;
56 strcpy(dav_roomname, dav_pathname);
58 /* Go to the correct room. */
59 if (strcasecmp(WC->wc_roomname, dav_roomname)) {
60 gotoroom(dav_roomname);
62 if (strcasecmp(WC->wc_roomname, dav_roomname)) {
63 wprintf("HTTP/1.1 404 not found\n");
64 groupdav_common_headers();
66 "Content-Type: text/plain\n"
68 "There is no folder called \"%s\" on this server.\n",
74 /* Ugly hack to mess with the content type. KOrganizer is either
75 * not supplying one, or supplying the wrong one. FIXME remove this
76 * after getting clarification or a fix from Reinhold.
78 strcpy(dav_content_type, supplied_content_type);
79 switch (WC->wc_view) {
80 case VIEW_ADDRESSBOOK:
81 strcpy(dav_content_type, "text/x-vcard");
84 strcpy(dav_content_type, "text/calendar");
87 strcpy(dav_content_type, "text/calendar");
94 * If an HTTP If-Match: header is present, the client is attempting
95 * to replace an existing item. We have to check to see if the
96 * message number associated with the supplied uid matches what the
97 * client is expecting. If not, the server probably contains a newer
98 * version, so we fail...
100 if (strlen(dav_ifmatch) > 0) {
101 lprintf(9, "* ifmatch failed, bailing out\n");
102 old_msgnum = locate_message_by_uid(dav_uid);
103 if (atol(dav_ifmatch) != old_msgnum) {
104 wprintf("HTTP/1.1 412 Precondition Failed\n");
105 groupdav_common_headers();
106 wprintf("Content-Length: 0\n\n");
112 * We are cleared for upload! We use the new calling syntax for ENT0
113 * which allows a confirmation to be sent back to us. That's how we
114 * extract the message ID.
116 lprintf(9, "* I am %s/%s/%s\n",
121 lprintf(9, "* allowing upload (old_msgnum=%ld, ifmatch=%s)\n", old_msgnum, dav_ifmatch);
122 serv_puts("ENT0 1|||4|||1|");
125 wprintf("HTTP/1.1 502 Bad Gateway\n");
126 groupdav_common_headers();
127 wprintf("Content-type: text/plain\n"
134 /* Send the content to the Citadel server */
135 serv_printf("Content-type: %s\n\n", dav_content_type);
136 serv_puts(dav_content);
139 /* Fetch the reply from the Citadel server */
142 while (serv_gets(buf), strcmp(buf, "000")) {
145 new_msgnum = atol(buf);
148 strcpy(dav_uid, buf);
154 /* Tell the client what happened. */
155 lprintf(9, "* new_msgnum = %ld\n", new_msgnum);
157 /* Citadel failed in some way? */
158 if (new_msgnum < 0L) {
159 wprintf("HTTP/1.1 502 Bad Gateway\n");
160 groupdav_common_headers();
161 wprintf("Content-type: text/plain\n"
163 "new_msgnum is %ld\n"
169 /* We created this item for the first time. */
170 if (old_msgnum < 0L) {
171 lprintf(9, "* new item created\n");
172 wprintf("HTTP/1.1 201 Created\n");
173 groupdav_common_headers();
174 wprintf("Content-Length: 0\n");
175 wprintf("Location: ");
176 if (strlen(WC->http_host) > 0) {
178 (is_https ? "https" : "http"),
181 wprintf("/groupdav/");
182 urlescputs(dav_roomname);
183 wprintf("/%s\n", dav_uid);
188 /* We modified an existing item. */
189 lprintf(9, "* existing item replaced\n");
190 wprintf("HTTP/1.1 204 No Content\n");
191 groupdav_common_headers();
192 wprintf("Content-Length: 0\n\n");
194 /* The item we replaced has probably already been deleted by
195 * the Citadel server, but we'll do this anyway, just in case.
197 serv_printf("DELE %ld", old_msgnum);