2 * Handles GroupDAV PUT requests.
4 * Copyright (c) 2005-2010 by the citadel.org team
6 * This program is open source software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "webserver.h"
27 * This function is for uploading an ENTIRE calendar, not just one
28 * component. This would be for webcal:// 'publish' operations, not
31 void dav_put_bigics(void)
37 * Tell the server that when we save a calendar event, we
38 * do *not* want the server to generate invitations.
40 serv_puts("ICAL sgi|0");
41 serv_getln(buf, sizeof buf);
43 serv_puts("ICAL putics");
44 serv_getln(buf, sizeof buf);
46 hprintf("HTTP/1.1 502 Bad Gateway\r\n");
48 hprintf("Content-type: text/plain\r\n");
50 wc_printf("%s\r\n", &buf[4]);
55 serv_putbuf(WCC->upload);
58 /* Report success and not much else. */
59 hprintf("HTTP/1.1 204 No Content\r\n");
60 syslog(9, "HTTP/1.1 204 No Content\r\n");
69 * The pathname is always going to take one of two formats:
70 * [/groupdav/]room_name/euid (GroupDAV)
71 * [/groupdav/]room_name (webcal)
78 long new_msgnum = (-2L);
79 long old_msgnum = (-1L);
83 if (StrBufNum_tokens(WCC->Hdr->HR.ReqLine, '/') < 2) {
84 hprintf("HTTP/1.1 404 not found\r\n");
86 hprintf("Content-Type: text/plain\r\n");
88 wc_printf("The object you requested was not found.\r\n");
93 dav_roomname = NewStrBuf();;
94 dav_uid = NewStrBuf();;
95 StrBufExtract_token(dav_roomname, WCC->Hdr->HR.ReqLine, 0, '/');
96 StrBufExtract_token(dav_uid, WCC->Hdr->HR.ReqLine, 1, '/');
97 if ((!strcasecmp(ChrPtr(dav_uid), "ics")) ||
98 (!strcasecmp(ChrPtr(dav_uid), "calendar.ics"))) {
102 /* Go to the correct room. */
103 if (strcasecmp(ChrPtr(WC->CurRoom.name), ChrPtr(dav_roomname))) {
104 gotoroom(dav_roomname);
106 if (strcasecmp(ChrPtr(WC->CurRoom.name), ChrPtr(dav_roomname))) {
107 hprintf("HTTP/1.1 404 not found\r\n");
108 dav_common_headers();
109 hprintf("Content-Type: text/plain\r\n");
111 wc_printf("There is no folder called \"%s\" on this server.\r\n",
112 ChrPtr(dav_roomname));
114 FreeStrBuf(&dav_roomname);
115 FreeStrBuf(&dav_uid);
120 * If an HTTP If-Match: header is present, the client is attempting
121 * to replace an existing item. We have to check to see if the
122 * message number associated with the supplied uid matches what the
123 * client is expecting. If not, the server probably contains a newer
124 * version, so we fail...
126 if (StrLength(WCC->Hdr->HR.dav_ifmatch) > 0) {
127 syslog(9, "dav_ifmatch: %s\n", ChrPtr(WCC->Hdr->HR.dav_ifmatch));
128 old_msgnum = locate_message_by_uid(ChrPtr(dav_uid));
129 syslog(9, "old_msgnum: %ld\n", old_msgnum);
130 if (StrTol(WCC->Hdr->HR.dav_ifmatch) != old_msgnum) {
131 hprintf("HTTP/1.1 412 Precondition Failed\r\n");
132 syslog(9, "HTTP/1.1 412 Precondition Failed (ifmatch=%ld, old_msgnum=%ld)\r\n",
133 StrTol(WCC->Hdr->HR.dav_ifmatch), old_msgnum);
134 dav_common_headers();
137 FreeStrBuf(&dav_roomname);
138 FreeStrBuf(&dav_uid);
143 /** PUT on the collection itself uploads an ICS of the entire collection.
145 if (StrLength(dav_uid) == 0) {
147 FreeStrBuf(&dav_roomname);
148 FreeStrBuf(&dav_uid);
153 * We are cleared for upload! We use the new calling syntax for ENT0
154 * which allows a confirmation to be sent back to us. That's how we
155 * extract the message ID.
157 serv_puts("ENT0 1|||4|||1|");
158 serv_getln(buf, sizeof buf);
160 hprintf("HTTP/1.1 502 Bad Gateway\r\n");
161 dav_common_headers();
162 hprintf("Content-type: text/plain\r\n");
164 wc_printf("%s\r\n", &buf[4]);
169 /* Send the content to the Citadel server */
170 //serv_printf("Content-type: %s\n\n", WCC->upload_content_type);
171 serv_putbuf(WCC->upload);
174 /* Fetch the reply from the Citadel server */
176 FlushStrBuf(dav_uid);
177 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
180 new_msgnum = atol(buf);
183 syslog(9, "new_msgnum=%ld (%s)\n", new_msgnum, buf);
186 StrBufAppendBufPlain(dav_uid, buf, -1, 0);
193 /* Tell the client what happened. */
195 /* Citadel failed in some way? */
196 if (new_msgnum < 0L) {
197 hprintf("HTTP/1.1 502 Bad Gateway\r\n");
198 dav_common_headers();
199 hprintf("Content-type: text/plain\r\n");
201 wc_printf("new_msgnum is %ld\r\n"
204 FreeStrBuf(&dav_roomname);
205 FreeStrBuf(&dav_uid);
209 /* We created this item for the first time. */
210 if (old_msgnum < 0L) {
211 char escaped_uid[1024];
212 hprintf("HTTP/1.1 201 Created\r\n");
213 syslog(9, "HTTP/1.1 201 Created\r\n");
214 dav_common_headers();
215 hprintf("etag: \"%ld\"\r\n", new_msgnum);
216 hprintf("Location: ");
217 dav_identify_hosthdr();
218 hprintf("/groupdav/");/* TODO */
219 hurlescputs(ChrPtr(dav_roomname));
220 euid_escapize(escaped_uid, ChrPtr(dav_uid));
221 hprintf("/%s\r\n", escaped_uid);
223 FreeStrBuf(&dav_roomname);
224 FreeStrBuf(&dav_uid);
228 /* We modified an existing item. */
229 hprintf("HTTP/1.1 204 No Content\r\n");
230 syslog(9, "HTTP/1.1 204 No Content\r\n");
231 dav_common_headers();
232 hprintf("Etag: \"%ld\"\r\n", new_msgnum);
233 /* The item we replaced has probably already been deleted by
234 * the Citadel server, but we'll do this anyway, just in case.
236 serv_printf("DELE %ld", old_msgnum);
237 serv_getln(buf, sizeof buf);
240 FreeStrBuf(&dav_roomname);
241 FreeStrBuf(&dav_uid);