3 * Handles GroupDAV PUT requests.
5 * Copyright (c) 2005-2012 by the citadel.org team
7 * This program is open source software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License, version 3.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
17 #include "webserver.h"
22 * This function is for uploading an ENTIRE calendar, not just one
23 * component. This would be for webcal:// 'publish' operations, not
26 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(LOG_DEBUG, "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)
71 long new_msgnum = (-2L);
72 long old_msgnum = (-1L);
76 if (StrBufNum_tokens(WCC->Hdr->HR.ReqLine, '/') < 2) {
77 hprintf("HTTP/1.1 404 not found\r\n");
79 hprintf("Content-Type: text/plain\r\n");
81 wc_printf("The object you requested was not found.\r\n");
86 dav_roomname = NewStrBuf();;
87 dav_uid = NewStrBuf();;
88 StrBufExtract_token(dav_roomname, WCC->Hdr->HR.ReqLine, 0, '/');
89 StrBufExtract_token(dav_uid, WCC->Hdr->HR.ReqLine, 1, '/');
90 if ((!strcasecmp(ChrPtr(dav_uid), "ics")) || (!strcasecmp(ChrPtr(dav_uid), "calendar.ics"))) {
94 /* Go to the correct room. */
95 if (strcasecmp(ChrPtr(WC->CurRoom.name), ChrPtr(dav_roomname))) {
96 gotoroom(dav_roomname);
98 if (strcasecmp(ChrPtr(WC->CurRoom.name), ChrPtr(dav_roomname))) {
99 hprintf("HTTP/1.1 404 not found\r\n");
100 dav_common_headers();
101 hprintf("Content-Type: text/plain\r\n");
103 wc_printf("There is no folder called \"%s\" on this server.\r\n", ChrPtr(dav_roomname));
105 FreeStrBuf(&dav_roomname);
106 FreeStrBuf(&dav_uid);
111 * If an HTTP If-Match: header is present, the client is attempting
112 * to replace an existing item. We have to check to see if the
113 * message number associated with the supplied uid matches what the
114 * client is expecting. If not, the server probably contains a newer
115 * version, so we fail...
117 if (StrLength(WCC->Hdr->HR.dav_ifmatch) > 0) {
118 syslog(LOG_DEBUG, "dav_ifmatch: %s\n", ChrPtr(WCC->Hdr->HR.dav_ifmatch));
119 old_msgnum = locate_message_by_uid(ChrPtr(dav_uid));
120 syslog(LOG_DEBUG, "old_msgnum: %ld\n", old_msgnum);
121 if (StrTol(WCC->Hdr->HR.dav_ifmatch) != old_msgnum) {
122 hprintf("HTTP/1.1 412 Precondition Failed\r\n");
123 syslog(LOG_INFO, "HTTP/1.1 412 Precondition Failed (ifmatch=%ld, old_msgnum=%ld)\r\n",
124 StrTol(WCC->Hdr->HR.dav_ifmatch), old_msgnum);
125 dav_common_headers();
128 FreeStrBuf(&dav_roomname);
129 FreeStrBuf(&dav_uid);
134 /** PUT on the collection itself uploads an ICS of the entire collection.
136 if (StrLength(dav_uid) == 0) {
138 FreeStrBuf(&dav_roomname);
139 FreeStrBuf(&dav_uid);
144 * We are cleared for upload! We use the new calling syntax for ENT0
145 * which allows a confirmation to be sent back to us. That's how we
146 * extract the message ID.
148 serv_puts("ENT0 1|||4|||1|");
149 serv_getln(buf, sizeof buf);
151 hprintf("HTTP/1.1 502 Bad Gateway\r\n");
152 dav_common_headers();
153 hprintf("Content-type: text/plain\r\n");
155 wc_printf("%s\r\n", &buf[4]);
160 /* Send the content to the Citadel server */
161 //serv_printf("Content-type: %s\n\n", WCC->upload_content_type);
162 serv_putbuf(WCC->upload);
165 /* Fetch the reply from the Citadel server */
167 FlushStrBuf(dav_uid);
168 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
171 new_msgnum = atol(buf);
174 syslog(LOG_DEBUG, "new_msgnum=%ld (%s)\n", new_msgnum, buf);
177 StrBufAppendBufPlain(dav_uid, buf, -1, 0);
184 /* Tell the client what happened. */
186 /* Citadel failed in some way? */
187 if (new_msgnum < 0L) {
188 hprintf("HTTP/1.1 502 Bad Gateway\r\n");
189 dav_common_headers();
190 hprintf("Content-type: text/plain\r\n");
192 wc_printf("new_msgnum is %ld\r\n" "\r\n", new_msgnum);
194 FreeStrBuf(&dav_roomname);
195 FreeStrBuf(&dav_uid);
199 /* We created this item for the first time. */
200 if (old_msgnum < 0L) {
201 char escaped_uid[1024];
202 hprintf("HTTP/1.1 201 Created\r\n");
203 syslog(LOG_DEBUG, "HTTP/1.1 201 Created\r\n");
204 dav_common_headers();
205 hprintf("etag: \"%ld\"\r\n", new_msgnum);
206 hprintf("Location: ");
207 dav_identify_hosthdr();
208 hprintf("/groupdav/"); /* TODO */
209 hurlescputs(ChrPtr(dav_roomname));
210 euid_escapize(escaped_uid, ChrPtr(dav_uid));
211 hprintf("/%s\r\n", escaped_uid);
213 FreeStrBuf(&dav_roomname);
214 FreeStrBuf(&dav_uid);
218 /* We modified an existing item. */
219 hprintf("HTTP/1.1 204 No Content\r\n");
220 syslog(LOG_DEBUG, "HTTP/1.1 204 No Content\r\n");
221 dav_common_headers();
222 hprintf("Etag: \"%ld\"\r\n", new_msgnum);
223 /* The item we replaced has probably already been deleted by
224 * the Citadel server, but we'll do this anyway, just in case.
226 serv_printf("DELE %ld", old_msgnum);
227 serv_getln(buf, sizeof buf);
230 FreeStrBuf(&dav_roomname);
231 FreeStrBuf(&dav_uid);