2 * Handles GroupDAV PUT requests.
4 * Copyright (c) 2005-2012 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, version 3.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
16 #include "webserver.h"
21 * This function is for uploading an ENTIRE calendar, not just one
22 * component. This would be for webcal:// 'publish' operations, not
25 void dav_put_bigics(void)
31 * Tell the server that when we save a calendar event, we
32 * do *not* want the server to generate invitations.
34 serv_puts("ICAL sgi|0");
35 serv_getln(buf, sizeof buf);
37 serv_puts("ICAL putics");
38 serv_getln(buf, sizeof buf);
40 hprintf("HTTP/1.1 502 Bad Gateway\r\n");
42 hprintf("Content-type: text/plain\r\n");
44 wc_printf("%s\r\n", &buf[4]);
49 serv_putbuf(WCC->upload);
52 /* Report success and not much else. */
53 hprintf("HTTP/1.1 204 No Content\r\n");
54 syslog(9, "HTTP/1.1 204 No Content\r\n");
63 * The pathname is always going to take one of two formats:
64 * [/groupdav/]room_name/euid (GroupDAV)
65 * [/groupdav/]room_name (webcal)
72 long new_msgnum = (-2L);
73 long old_msgnum = (-1L);
77 if (StrBufNum_tokens(WCC->Hdr->HR.ReqLine, '/') < 2) {
78 hprintf("HTTP/1.1 404 not found\r\n");
80 hprintf("Content-Type: text/plain\r\n");
82 wc_printf("The object you requested was not found.\r\n");
87 dav_roomname = NewStrBuf();;
88 dav_uid = NewStrBuf();;
89 StrBufExtract_token(dav_roomname, WCC->Hdr->HR.ReqLine, 0, '/');
90 StrBufExtract_token(dav_uid, WCC->Hdr->HR.ReqLine, 1, '/');
91 if ((!strcasecmp(ChrPtr(dav_uid), "ics")) ||
92 (!strcasecmp(ChrPtr(dav_uid), "calendar.ics"))) {
96 /* Go to the correct room. */
97 if (strcasecmp(ChrPtr(WC->CurRoom.name), ChrPtr(dav_roomname))) {
98 gotoroom(dav_roomname);
100 if (strcasecmp(ChrPtr(WC->CurRoom.name), ChrPtr(dav_roomname))) {
101 hprintf("HTTP/1.1 404 not found\r\n");
102 dav_common_headers();
103 hprintf("Content-Type: text/plain\r\n");
105 wc_printf("There is no folder called \"%s\" on this server.\r\n",
106 ChrPtr(dav_roomname));
108 FreeStrBuf(&dav_roomname);
109 FreeStrBuf(&dav_uid);
114 * If an HTTP If-Match: header is present, the client is attempting
115 * to replace an existing item. We have to check to see if the
116 * message number associated with the supplied uid matches what the
117 * client is expecting. If not, the server probably contains a newer
118 * version, so we fail...
120 if (StrLength(WCC->Hdr->HR.dav_ifmatch) > 0) {
121 syslog(9, "dav_ifmatch: %s\n", ChrPtr(WCC->Hdr->HR.dav_ifmatch));
122 old_msgnum = locate_message_by_uid(ChrPtr(dav_uid));
123 syslog(9, "old_msgnum: %ld\n", old_msgnum);
124 if (StrTol(WCC->Hdr->HR.dav_ifmatch) != old_msgnum) {
125 hprintf("HTTP/1.1 412 Precondition Failed\r\n");
126 syslog(9, "HTTP/1.1 412 Precondition Failed (ifmatch=%ld, old_msgnum=%ld)\r\n",
127 StrTol(WCC->Hdr->HR.dav_ifmatch), old_msgnum);
128 dav_common_headers();
131 FreeStrBuf(&dav_roomname);
132 FreeStrBuf(&dav_uid);
137 /** PUT on the collection itself uploads an ICS of the entire collection.
139 if (StrLength(dav_uid) == 0) {
141 FreeStrBuf(&dav_roomname);
142 FreeStrBuf(&dav_uid);
147 * We are cleared for upload! We use the new calling syntax for ENT0
148 * which allows a confirmation to be sent back to us. That's how we
149 * extract the message ID.
151 serv_puts("ENT0 1|||4|||1|");
152 serv_getln(buf, sizeof buf);
154 hprintf("HTTP/1.1 502 Bad Gateway\r\n");
155 dav_common_headers();
156 hprintf("Content-type: text/plain\r\n");
158 wc_printf("%s\r\n", &buf[4]);
163 /* Send the content to the Citadel server */
164 //serv_printf("Content-type: %s\n\n", WCC->upload_content_type);
165 serv_putbuf(WCC->upload);
168 /* Fetch the reply from the Citadel server */
170 FlushStrBuf(dav_uid);
171 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
174 new_msgnum = atol(buf);
177 syslog(9, "new_msgnum=%ld (%s)\n", new_msgnum, buf);
180 StrBufAppendBufPlain(dav_uid, buf, -1, 0);
187 /* Tell the client what happened. */
189 /* Citadel failed in some way? */
190 if (new_msgnum < 0L) {
191 hprintf("HTTP/1.1 502 Bad Gateway\r\n");
192 dav_common_headers();
193 hprintf("Content-type: text/plain\r\n");
195 wc_printf("new_msgnum is %ld\r\n"
198 FreeStrBuf(&dav_roomname);
199 FreeStrBuf(&dav_uid);
203 /* We created this item for the first time. */
204 if (old_msgnum < 0L) {
205 char escaped_uid[1024];
206 hprintf("HTTP/1.1 201 Created\r\n");
207 syslog(9, "HTTP/1.1 201 Created\r\n");
208 dav_common_headers();
209 hprintf("etag: \"%ld\"\r\n", new_msgnum);
210 hprintf("Location: ");
211 dav_identify_hosthdr();
212 hprintf("/groupdav/");/* TODO */
213 hurlescputs(ChrPtr(dav_roomname));
214 euid_escapize(escaped_uid, ChrPtr(dav_uid));
215 hprintf("/%s\r\n", escaped_uid);
217 FreeStrBuf(&dav_roomname);
218 FreeStrBuf(&dav_uid);
222 /* We modified an existing item. */
223 hprintf("HTTP/1.1 204 No Content\r\n");
224 syslog(9, "HTTP/1.1 204 No Content\r\n");
225 dav_common_headers();
226 hprintf("Etag: \"%ld\"\r\n", new_msgnum);
227 /* The item we replaced has probably already been deleted by
228 * the Citadel server, but we'll do this anyway, just in case.
230 serv_printf("DELE %ld", old_msgnum);
231 serv_getln(buf, sizeof buf);
234 FreeStrBuf(&dav_roomname);
235 FreeStrBuf(&dav_uid);