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(void)
24 * Tell the server that when we save a calendar event, we
25 * do *not* want the server to generate invitations.
27 serv_puts("ICAL sgi|0");
28 serv_getln(buf, sizeof buf);
30 serv_puts("ICAL putics");
31 serv_getln(buf, sizeof buf);
33 hprintf("HTTP/1.1 502 Bad Gateway\r\n");
34 groupdav_common_headers();
35 hprintf("Content-type: text/plain\r\n");
36 wprintf("%s\r\n", &buf[4]);
41 serv_write(WCC->upload, WCC->upload_length);
44 /* Report success and not much else. */
45 hprintf("HTTP/1.1 204 No Content\r\n");
46 lprintf(9, "HTTP/1.1 204 No Content\r\n");
47 groupdav_common_headers();
55 * The pathname is always going to take one of two formats:
56 * [/groupdav/]room_name/euid (GroupDAV)
57 * [/groupdav/]room_name (webcal)
59 void groupdav_put(void)
64 long new_msgnum = (-2L);
65 long old_msgnum = (-1L);
69 if (StrBufNum_tokens(WCC->Hdr->HR.ReqLine, '/') < 2) {
70 hprintf("HTTP/1.1 404 not found\r\n");
71 groupdav_common_headers();
72 hprintf("Content-Type: text/plain\r\n");
73 wprintf("The object you requested was not found.\r\n");
78 dav_roomname = NewStrBuf();;
79 dav_uid = NewStrBuf();;
80 StrBufExtract_token(dav_roomname, WCC->Hdr->HR.ReqLine, 0, '/');
81 StrBufExtract_token(dav_uid, WCC->Hdr->HR.ReqLine, 1, '/');
82 if ((!strcasecmp(ChrPtr(dav_uid), "ics")) ||
83 (!strcasecmp(ChrPtr(dav_uid), "calendar.ics"))) {
87 /* Go to the correct room. */
88 if (strcasecmp(ChrPtr(WC->wc_roomname), ChrPtr(dav_roomname))) {
89 gotoroom(dav_roomname);
91 if (strcasecmp(ChrPtr(WC->wc_roomname), ChrPtr(dav_roomname))) {
92 hprintf("HTTP/1.1 404 not found\r\n");
93 groupdav_common_headers();
94 hprintf("Content-Type: text/plain\r\n");
95 wprintf("There is no folder called \"%s\" on this server.\r\n",
96 ChrPtr(dav_roomname));
98 FreeStrBuf(&dav_roomname);
104 * If an HTTP If-Match: header is present, the client is attempting
105 * to replace an existing item. We have to check to see if the
106 * message number associated with the supplied uid matches what the
107 * client is expecting. If not, the server probably contains a newer
108 * version, so we fail...
110 if (StrLength(WCC->Hdr->HR.dav_ifmatch) > 0) {
111 lprintf(9, "dav_ifmatch: %s\n", WCC->Hdr->HR.dav_ifmatch);
112 old_msgnum = locate_message_by_uid(ChrPtr(dav_uid));
113 lprintf(9, "old_msgnum: %ld\n", old_msgnum);
114 if (StrTol(WCC->Hdr->HR.dav_ifmatch) != old_msgnum) {
115 hprintf("HTTP/1.1 412 Precondition Failed\r\n");
116 lprintf(9, "HTTP/1.1 412 Precondition Failed (ifmatch=%ld, old_msgnum=%ld)\r\n",
117 StrTol(WCC->Hdr->HR.dav_ifmatch), old_msgnum);
118 groupdav_common_headers();
119 hprintf("Content-Length: 0\r\n");
121 FreeStrBuf(&dav_roomname);
122 FreeStrBuf(&dav_uid);
127 /** PUT on the collection itself uploads an ICS of the entire collection.
129 if (StrLength(dav_uid) == 0) {
130 groupdav_put_bigics();
131 FreeStrBuf(&dav_roomname);
132 FreeStrBuf(&dav_uid);
137 * We are cleared for upload! We use the new calling syntax for ENT0
138 * which allows a confirmation to be sent back to us. That's how we
139 * extract the message ID.
141 serv_puts("ENT0 1|||4|||1|");
142 serv_getln(buf, sizeof buf);
144 hprintf("HTTP/1.1 502 Bad Gateway\r\n");
145 groupdav_common_headers();
146 hprintf("Content-type: text/plain\r\n");
148 wprintf("%s\r\n", &buf[4]);
153 /* Send the content to the Citadel server */
154 serv_printf("Content-type: %s\n\n", WCC->upload_content_type);
155 serv_puts(WCC->upload);
158 /* Fetch the reply from the Citadel server */
160 FlushStrBuf(dav_uid);
161 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
164 new_msgnum = atol(buf);
167 lprintf(9, "new_msgnum=%ld (%s)\n", new_msgnum, buf);
170 StrBufAppendBufPlain(dav_uid, buf, -1, 0);
177 /* Tell the client what happened. */
179 /* Citadel failed in some way? */
180 if (new_msgnum < 0L) {
181 hprintf("HTTP/1.1 502 Bad Gateway\r\n");
182 groupdav_common_headers();
183 hprintf("Content-type: text/plain\r\n");
184 wprintf("new_msgnum is %ld\r\n"
187 FreeStrBuf(&dav_roomname);
188 FreeStrBuf(&dav_uid);
192 /* We created this item for the first time. */
193 if (old_msgnum < 0L) {
194 char escaped_uid[1024];
195 hprintf("HTTP/1.1 201 Created\r\n");
196 lprintf(9, "HTTP/1.1 201 Created\r\n");
197 groupdav_common_headers();
198 hprintf("etag: \"%ld\"\r\n", new_msgnum);
199 hprintf("Location: ");
200 groupdav_identify_host();
201 hprintf("/groupdav/");/* TODO */
202 hurlescputs(ChrPtr(dav_roomname));
203 euid_escapize(escaped_uid, ChrPtr(dav_uid));
204 hprintf("/%s\r\n", escaped_uid);
206 FreeStrBuf(&dav_roomname);
207 FreeStrBuf(&dav_uid);
211 /* We modified an existing item. */
212 hprintf("HTTP/1.1 204 No Content\r\n");
213 lprintf(9, "HTTP/1.1 204 No Content\r\n");
214 groupdav_common_headers();
215 hprintf("Etag: \"%ld\"\r\n", new_msgnum);
216 /* The item we replaced has probably already been deleted by
217 * the Citadel server, but we'll do this anyway, just in case.
219 serv_printf("DELE %ld", old_msgnum);
220 serv_getln(buf, sizeof buf);
223 FreeStrBuf(&dav_roomname);
224 FreeStrBuf(&dav_uid);