Mailing list header changes (fuck you Google)
[citadel.git] / webcit / dav_put.c
1 /*
2  * Handles GroupDAV PUT requests.
3  *
4  * Copyright (c) 2005-2012 by the citadel.org team
5  *
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.
8  *
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.
13  */
14
15 #include "webcit.h"
16 #include "webserver.h"
17 #include "dav.h"
18
19
20 /*
21  * This function is for uploading an ENTIRE calendar, not just one
22  * component.  This would be for webcal:// 'publish' operations, not
23  * for GroupDAV.
24  */
25 void dav_put_bigics(void)
26 {
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 {
69         wcsession *WCC = WC;
70         StrBuf *dav_roomname;
71         StrBuf *dav_uid;
72         long new_msgnum = (-2L);
73         long old_msgnum = (-1L);
74         char buf[SIZ];
75         int n = 0;
76
77         if (StrBufNum_tokens(WCC->Hdr->HR.ReqLine, '/') < 2) {
78                 hprintf("HTTP/1.1 404 not found\r\n");
79                 dav_common_headers();
80                 hprintf("Content-Type: text/plain\r\n");
81                 begin_burst();
82                 wc_printf("The object you requested was not found.\r\n");
83                 end_burst();
84                 return;
85         }
86
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"))) {
93                 FlushStrBuf(dav_uid);
94         }
95
96         /* Go to the correct room. */
97         if (strcasecmp(ChrPtr(WC->CurRoom.name), ChrPtr(dav_roomname))) {
98                 gotoroom(dav_roomname);
99         }
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");
104                 begin_burst();
105                 wc_printf("There is no folder called \"%s\" on this server.\r\n",
106                         ChrPtr(dav_roomname));
107                 end_burst();
108                 FreeStrBuf(&dav_roomname);
109                 FreeStrBuf(&dav_uid);           
110                 return;
111         }
112
113         /*
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...
119          */
120         if (StrLength(WCC->Hdr->HR.dav_ifmatch) > 0) {
121                 syslog(LOG_DEBUG, "dav_ifmatch: %s\n", ChrPtr(WCC->Hdr->HR.dav_ifmatch));
122                 old_msgnum = locate_message_by_uid(ChrPtr(dav_uid));
123                 syslog(LOG_DEBUG, "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(LOG_INFO, "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();
129                         
130                         end_burst();
131                         FreeStrBuf(&dav_roomname);
132                         FreeStrBuf(&dav_uid);
133                         return;
134                 }
135         }
136
137         /** PUT on the collection itself uploads an ICS of the entire collection.
138          */
139         if (StrLength(dav_uid) == 0) {
140                 dav_put_bigics();
141                 FreeStrBuf(&dav_roomname);
142                 FreeStrBuf(&dav_uid);
143                 return;
144         }
145
146         /*
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.
150          */
151         serv_puts("ENT0 1|||4|||1|");
152         serv_getln(buf, sizeof buf);
153         if (buf[0] != '8') {
154                 hprintf("HTTP/1.1 502 Bad Gateway\r\n");
155                 dav_common_headers();
156                 hprintf("Content-type: text/plain\r\n");
157                 begin_burst();
158                 wc_printf("%s\r\n", &buf[4]);
159                 end_burst();
160                 return;
161         }
162
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);
166         serv_puts("\n000");
167
168         /* Fetch the reply from the Citadel server */
169         n = 0;
170         FlushStrBuf(dav_uid);
171         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
172                 switch(n++) {
173                 case 0: 
174                         new_msgnum = atol(buf);
175                         break;
176                 case 1: 
177                         syslog(LOG_DEBUG, "new_msgnum=%ld (%s)\n", new_msgnum, buf);
178                         break;
179                 case 2: 
180                         StrBufAppendBufPlain(dav_uid, buf, -1, 0);
181                         break;
182                 default:
183                         break;
184                 }
185         }
186
187         /* Tell the client what happened. */
188
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");
194                 begin_burst();
195                 wc_printf("new_msgnum is %ld\r\n"
196                         "\r\n", new_msgnum);
197                 end_burst();
198                 FreeStrBuf(&dav_roomname);
199                 FreeStrBuf(&dav_uid);
200                 return;
201         }
202
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(LOG_DEBUG, "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);
216                 end_burst();
217                 FreeStrBuf(&dav_roomname);
218                 FreeStrBuf(&dav_uid);
219                 return;
220         }
221
222         /* We modified an existing item. */
223         hprintf("HTTP/1.1 204 No Content\r\n");
224         syslog(LOG_DEBUG, "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.
229          */
230         serv_printf("DELE %ld", old_msgnum);
231         serv_getln(buf, sizeof buf);
232         begin_burst();
233         end_burst();
234         FreeStrBuf(&dav_roomname);
235         FreeStrBuf(&dav_uid);
236         return;
237 }