4 * Handles GroupDAV PUT requests.
14 * This function is for uploading an ENTIRE calendar, not just one
15 * component. This would be for webcal:// 'publish' operations, not
18 void groupdav_put_bigics(char *dav_content, int dav_content_length)
23 * Tell the server that when we save a calendar event, we
24 * do *not* want the server to generate invitations.
26 serv_puts("ICAL sgi|0");
27 serv_getln(buf, sizeof buf);
29 serv_puts("ICAL putics");
30 serv_getln(buf, sizeof buf);
32 wprintf("HTTP/1.1 502 Bad Gateway\r\n");
33 groupdav_common_headers();
34 wprintf("Content-type: text/plain\r\n"
41 serv_write(dav_content, dav_content_length);
44 /* Report success and not much else. */
45 wprintf("HTTP/1.1 204 No Content\r\n");
46 lprintf(9, "HTTP/1.1 204 No Content\r\n");
47 groupdav_common_headers();
48 wprintf("Content-Length: 0\r\n\r\n");
54 * The pathname is always going to take one of two formats:
55 * /groupdav/room_name/euid (GroupDAV)
56 * /groupdav/room_name (webcal)
58 void groupdav_put(char *dav_pathname, char *dav_ifmatch,
59 char *dav_content_type, char *dav_content,
60 int dav_content_length
62 char dav_roomname[1024];
64 long new_msgnum = (-2L);
65 long old_msgnum = (-1L);
69 if (num_tokens(dav_pathname, '/') < 3) {
70 wprintf("HTTP/1.1 404 not found\r\n");
71 groupdav_common_headers();
73 "Content-Type: text/plain\r\n"
75 "The object you requested was not found.\r\n"
80 extract_token(dav_roomname, dav_pathname, 2, '/', sizeof dav_roomname);
81 extract_token(dav_uid, dav_pathname, 3, '/', sizeof dav_uid);
82 if ((!strcasecmp(dav_uid, "ics")) || (!strcasecmp(dav_uid, "calendar.ics"))) {
86 /* Go to the correct room. */
87 if (strcasecmp(WC->wc_roomname, dav_roomname)) {
88 gotoroom(dav_roomname);
90 if (strcasecmp(WC->wc_roomname, dav_roomname)) {
91 wprintf("HTTP/1.1 404 not found\r\n");
92 groupdav_common_headers();
94 "Content-Type: text/plain\r\n"
96 "There is no folder called \"%s\" on this server.\r\n",
103 * If an HTTP If-Match: header is present, the client is attempting
104 * to replace an existing item. We have to check to see if the
105 * message number associated with the supplied uid matches what the
106 * client is expecting. If not, the server probably contains a newer
107 * version, so we fail...
109 if (!IsEmptyStr(dav_ifmatch)) {
110 lprintf(9, "dav_ifmatch: %s\n", dav_ifmatch);
111 old_msgnum = locate_message_by_uid(dav_uid);
112 lprintf(9, "old_msgnum: %ld\n", old_msgnum);
113 if (atol(dav_ifmatch) != old_msgnum) {
114 wprintf("HTTP/1.1 412 Precondition Failed\r\n");
115 lprintf(9, "HTTP/1.1 412 Precondition Failed (ifmatch=%ld, old_msgnum=%ld)\r\n",
116 atol(dav_ifmatch), old_msgnum);
117 groupdav_common_headers();
118 wprintf("Content-Length: 0\r\n\r\n");
123 /** PUT on the collection itself uploads an ICS of the entire collection.
125 if (!strcasecmp(dav_uid, "")) {
126 groupdav_put_bigics(dav_content, dav_content_length);
131 * We are cleared for upload! We use the new calling syntax for ENT0
132 * which allows a confirmation to be sent back to us. That's how we
133 * extract the message ID.
135 serv_puts("ENT0 1|||4|||1|");
136 serv_getln(buf, sizeof buf);
138 wprintf("HTTP/1.1 502 Bad Gateway\r\n");
139 groupdav_common_headers();
140 wprintf("Content-type: text/plain\r\n"
147 /* Send the content to the Citadel server */
148 serv_printf("Content-type: %s\n\n", dav_content_type);
149 serv_puts(dav_content);
152 /* Fetch the reply from the Citadel server */
155 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
157 case 0: new_msgnum = atol(buf);
159 case 1: lprintf(9, "new_msgnum=%ld (%s)\n", new_msgnum, buf);
161 case 2: strcpy(dav_uid, buf);
168 /* Tell the client what happened. */
170 /* Citadel failed in some way? */
171 if (new_msgnum < 0L) {
172 wprintf("HTTP/1.1 502 Bad Gateway\r\n");
173 groupdav_common_headers();
174 wprintf("Content-type: text/plain\r\n"
176 "new_msgnum is %ld\r\n"
182 /* We created this item for the first time. */
183 if (old_msgnum < 0L) {
184 wprintf("HTTP/1.1 201 Created\r\n");
185 lprintf(9, "HTTP/1.1 201 Created\r\n");
186 groupdav_common_headers();
187 wprintf("etag: \"%ld\"\r\n", new_msgnum);
188 wprintf("Content-Length: 0\r\n");
189 wprintf("Location: ");
190 groupdav_identify_host();
191 wprintf("/groupdav/");
192 urlescputs(dav_roomname);
193 char escaped_uid[1024];
194 euid_escapize(escaped_uid, dav_uid);
195 wprintf("/%s\r\n", escaped_uid);
200 /* We modified an existing item. */
201 wprintf("HTTP/1.1 204 No Content\r\n");
202 lprintf(9, "HTTP/1.1 204 No Content\r\n");
203 groupdav_common_headers();
204 wprintf("etag: \"%ld\"\r\n", new_msgnum);
205 wprintf("Content-Length: 0\r\n\r\n");
207 /* The item we replaced has probably already been deleted by
208 * the Citadel server, but we'll do this anyway, just in case.
210 serv_printf("DELE %ld", old_msgnum);
211 serv_getln(buf, sizeof buf);