Merge branch 'master' of ssh://git.citadel.org/appl/gitroot/citadel
[citadel] / 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  * 
10  *
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.
15  *
16  * 
17  * 
18  * 
19  */
20
21 #include "webcit.h"
22 #include "webserver.h"
23 #include "dav.h"
24
25
26 /*
27  * This function is for uploading an ENTIRE calendar, not just one
28  * component.  This would be for webcal:// 'publish' operations, not
29  * for GroupDAV.
30  */
31 void dav_put_bigics(void)
32 {
33         wcsession *WCC = WC;
34         char buf[1024];
35
36         /*
37          * Tell the server that when we save a calendar event, we
38          * do *not* want the server to generate invitations. 
39          */
40         serv_puts("ICAL sgi|0");
41         serv_getln(buf, sizeof buf);
42
43         serv_puts("ICAL putics");
44         serv_getln(buf, sizeof buf);
45         if (buf[0] != '4') {
46                 hprintf("HTTP/1.1 502 Bad Gateway\r\n");
47                 dav_common_headers();
48                 hprintf("Content-type: text/plain\r\n");
49                 begin_burst();
50                 wc_printf("%s\r\n", &buf[4]);
51                 end_burst();
52                 return;
53         }
54
55         serv_putbuf(WCC->upload);
56         serv_printf("\n000");
57
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");
61         dav_common_headers();
62         begin_burst();
63         end_burst();
64 }
65
66
67
68 /*
69  * The pathname is always going to take one of two formats:
70  * [/groupdav/]room_name/euid   (GroupDAV)
71  * [/groupdav/]room_name                (webcal)
72  */
73 void dav_put(void) 
74 {
75         wcsession *WCC = WC;
76         StrBuf *dav_roomname;
77         StrBuf *dav_uid;
78         long new_msgnum = (-2L);
79         long old_msgnum = (-1L);
80         char buf[SIZ];
81         int n = 0;
82
83         if (StrBufNum_tokens(WCC->Hdr->HR.ReqLine, '/') < 2) {
84                 hprintf("HTTP/1.1 404 not found\r\n");
85                 dav_common_headers();
86                 hprintf("Content-Type: text/plain\r\n");
87                 begin_burst();
88                 wc_printf("The object you requested was not found.\r\n");
89                 end_burst();
90                 return;
91         }
92
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"))) {
99                 FlushStrBuf(dav_uid);
100         }
101
102         /* Go to the correct room. */
103         if (strcasecmp(ChrPtr(WC->CurRoom.name), ChrPtr(dav_roomname))) {
104                 gotoroom(dav_roomname);
105         }
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");
110                 begin_burst();
111                 wc_printf("There is no folder called \"%s\" on this server.\r\n",
112                         ChrPtr(dav_roomname));
113                 end_burst();
114                 FreeStrBuf(&dav_roomname);
115                 FreeStrBuf(&dav_uid);           
116                 return;
117         }
118
119         /*
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...
125          */
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();
135                         
136                         end_burst();
137                         FreeStrBuf(&dav_roomname);
138                         FreeStrBuf(&dav_uid);
139                         return;
140                 }
141         }
142
143         /** PUT on the collection itself uploads an ICS of the entire collection.
144          */
145         if (StrLength(dav_uid) == 0) {
146                 dav_put_bigics();
147                 FreeStrBuf(&dav_roomname);
148                 FreeStrBuf(&dav_uid);
149                 return;
150         }
151
152         /*
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.
156          */
157         serv_puts("ENT0 1|||4|||1|");
158         serv_getln(buf, sizeof buf);
159         if (buf[0] != '8') {
160                 hprintf("HTTP/1.1 502 Bad Gateway\r\n");
161                 dav_common_headers();
162                 hprintf("Content-type: text/plain\r\n");
163                 begin_burst();
164                 wc_printf("%s\r\n", &buf[4]);
165                 end_burst();
166                 return;
167         }
168
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);
172         serv_puts("\n000");
173
174         /* Fetch the reply from the Citadel server */
175         n = 0;
176         FlushStrBuf(dav_uid);
177         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
178                 switch(n++) {
179                 case 0: 
180                         new_msgnum = atol(buf);
181                         break;
182                 case 1: 
183                         syslog(9, "new_msgnum=%ld (%s)\n", new_msgnum, buf);
184                         break;
185                 case 2: 
186                         StrBufAppendBufPlain(dav_uid, buf, -1, 0);
187                         break;
188                 default:
189                         break;
190                 }
191         }
192
193         /* Tell the client what happened. */
194
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");
200                 begin_burst();
201                 wc_printf("new_msgnum is %ld\r\n"
202                         "\r\n", new_msgnum);
203                 end_burst();
204                 FreeStrBuf(&dav_roomname);
205                 FreeStrBuf(&dav_uid);
206                 return;
207         }
208
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);
222                 end_burst();
223                 FreeStrBuf(&dav_roomname);
224                 FreeStrBuf(&dav_uid);
225                 return;
226         }
227
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.
235          */
236         serv_printf("DELE %ld", old_msgnum);
237         serv_getln(buf, sizeof buf);
238         begin_burst();
239         end_burst();
240         FreeStrBuf(&dav_roomname);
241         FreeStrBuf(&dav_uid);
242         return;
243 }