indent -kr -i8 -brf -bbb -fnc -l132 -nce on all of webcit-classic
[citadel.git] / webcit / dav_put.c
1
2 /*
3  * Handles GroupDAV PUT requests.
4  *
5  * Copyright (c) 2005-2012 by the citadel.org team
6  *
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.
9  *
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.
14  */
15
16 #include "webcit.h"
17 #include "webserver.h"
18 #include "dav.h"
19
20
21 /*
22  * This function is for uploading an ENTIRE calendar, not just one
23  * component.  This would be for webcal:// 'publish' operations, not
24  * for GroupDAV.
25  */
26 void dav_put_bigics(void) {
27         wcsession *WCC = WC;
28         char buf[1024];
29
30         /*
31          * Tell the server that when we save a calendar event, we
32          * do *not* want the server to generate invitations. 
33          */
34         serv_puts("ICAL sgi|0");
35         serv_getln(buf, sizeof buf);
36
37         serv_puts("ICAL putics");
38         serv_getln(buf, sizeof buf);
39         if (buf[0] != '4') {
40                 hprintf("HTTP/1.1 502 Bad Gateway\r\n");
41                 dav_common_headers();
42                 hprintf("Content-type: text/plain\r\n");
43                 begin_burst();
44                 wc_printf("%s\r\n", &buf[4]);
45                 end_burst();
46                 return;
47         }
48
49         serv_putbuf(WCC->upload);
50         serv_printf("\n000");
51
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");
55         dav_common_headers();
56         begin_burst();
57         end_burst();
58 }
59
60
61
62 /*
63  * The pathname is always going to take one of two formats:
64  * [/groupdav/]room_name/euid   (GroupDAV)
65  * [/groupdav/]room_name                (webcal)
66  */
67 void dav_put(void) {
68         wcsession *WCC = WC;
69         StrBuf *dav_roomname;
70         StrBuf *dav_uid;
71         long new_msgnum = (-2L);
72         long old_msgnum = (-1L);
73         char buf[SIZ];
74         int n = 0;
75
76         if (StrBufNum_tokens(WCC->Hdr->HR.ReqLine, '/') < 2) {
77                 hprintf("HTTP/1.1 404 not found\r\n");
78                 dav_common_headers();
79                 hprintf("Content-Type: text/plain\r\n");
80                 begin_burst();
81                 wc_printf("The object you requested was not found.\r\n");
82                 end_burst();
83                 return;
84         }
85
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"))) {
91                 FlushStrBuf(dav_uid);
92         }
93
94         /* Go to the correct room. */
95         if (strcasecmp(ChrPtr(WC->CurRoom.name), ChrPtr(dav_roomname))) {
96                 gotoroom(dav_roomname);
97         }
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");
102                 begin_burst();
103                 wc_printf("There is no folder called \"%s\" on this server.\r\n", ChrPtr(dav_roomname));
104                 end_burst();
105                 FreeStrBuf(&dav_roomname);
106                 FreeStrBuf(&dav_uid);
107                 return;
108         }
109
110         /*
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...
116          */
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();
126
127                         end_burst();
128                         FreeStrBuf(&dav_roomname);
129                         FreeStrBuf(&dav_uid);
130                         return;
131                 }
132         }
133
134         /** PUT on the collection itself uploads an ICS of the entire collection.
135          */
136         if (StrLength(dav_uid) == 0) {
137                 dav_put_bigics();
138                 FreeStrBuf(&dav_roomname);
139                 FreeStrBuf(&dav_uid);
140                 return;
141         }
142
143         /*
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.
147          */
148         serv_puts("ENT0 1|||4|||1|");
149         serv_getln(buf, sizeof buf);
150         if (buf[0] != '8') {
151                 hprintf("HTTP/1.1 502 Bad Gateway\r\n");
152                 dav_common_headers();
153                 hprintf("Content-type: text/plain\r\n");
154                 begin_burst();
155                 wc_printf("%s\r\n", &buf[4]);
156                 end_burst();
157                 return;
158         }
159
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);
163         serv_puts("\n000");
164
165         /* Fetch the reply from the Citadel server */
166         n = 0;
167         FlushStrBuf(dav_uid);
168         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
169                 switch (n++) {
170                 case 0:
171                         new_msgnum = atol(buf);
172                         break;
173                 case 1:
174                         syslog(LOG_DEBUG, "new_msgnum=%ld (%s)\n", new_msgnum, buf);
175                         break;
176                 case 2:
177                         StrBufAppendBufPlain(dav_uid, buf, -1, 0);
178                         break;
179                 default:
180                         break;
181                 }
182         }
183
184         /* Tell the client what happened. */
185
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");
191                 begin_burst();
192                 wc_printf("new_msgnum is %ld\r\n" "\r\n", new_msgnum);
193                 end_burst();
194                 FreeStrBuf(&dav_roomname);
195                 FreeStrBuf(&dav_uid);
196                 return;
197         }
198
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);
212                 end_burst();
213                 FreeStrBuf(&dav_roomname);
214                 FreeStrBuf(&dav_uid);
215                 return;
216         }
217
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.
225          */
226         serv_printf("DELE %ld", old_msgnum);
227         serv_getln(buf, sizeof buf);
228         begin_burst();
229         end_burst();
230         FreeStrBuf(&dav_roomname);
231         FreeStrBuf(&dav_uid);
232         return;
233 }