48f0b4a8534fd90f74693504fe13210d11123d22
[citadel.git] / webcit / groupdav_put.c
1 /*
2  * $Id$
3  *
4  * Handles GroupDAV PUT requests.
5  *
6  */
7
8 #include "webcit.h"
9 #include "webserver.h"
10 #include "groupdav.h"
11
12
13 /*
14  * This function is for uploading an ENTIRE calendar, not just one
15  * component.  This would be for webcal:// 'publish' operations, not
16  * for GroupDAV.
17  */
18 void groupdav_put_bigics(void)
19 {
20         wcsession *WCC = WC;
21         char buf[1024];
22
23         /*
24          * Tell the server that when we save a calendar event, we
25          * do *not* want the server to generate invitations. 
26          */
27         serv_puts("ICAL sgi|0");
28         serv_getln(buf, sizeof buf);
29
30         serv_puts("ICAL putics");
31         serv_getln(buf, sizeof buf);
32         if (buf[0] != '4') {
33                 hprintf("HTTP/1.1 502 Bad Gateway\r\n");
34                 groupdav_common_headers();
35                 hprintf("Content-type: text/plain\r\n");
36                 begin_burst();
37                 wprintf("%s\r\n", &buf[4]);
38                 end_burst();
39                 return;
40         }
41
42         serv_putbuf(WCC->upload);
43         serv_printf("\n000");
44
45         /* Report success and not much else. */
46         hprintf("HTTP/1.1 204 No Content\r\n");
47         lprintf(9, "HTTP/1.1 204 No Content\r\n");
48         groupdav_common_headers();
49         begin_burst();
50         end_burst();
51 }
52
53
54
55 /*
56  * The pathname is always going to take one of two formats:
57  * [/groupdav/]room_name/euid   (GroupDAV)
58  * [/groupdav/]room_name                (webcal)
59  */
60 void groupdav_put(void) 
61 {
62         wcsession *WCC = WC;
63         StrBuf *dav_roomname;
64         StrBuf *dav_uid;
65         long new_msgnum = (-2L);
66         long old_msgnum = (-1L);
67         char buf[SIZ];
68         int n = 0;
69
70         if (StrBufNum_tokens(WCC->Hdr->HR.ReqLine, '/') < 2) {
71                 hprintf("HTTP/1.1 404 not found\r\n");
72                 groupdav_common_headers();
73                 hprintf("Content-Type: text/plain\r\n");
74                 begin_burst();
75                 wprintf("The object you requested was not found.\r\n");
76                 end_burst();
77                 return;
78         }
79
80         dav_roomname = NewStrBuf();;
81         dav_uid = NewStrBuf();;
82         StrBufExtract_token(dav_roomname, WCC->Hdr->HR.ReqLine, 0, '/');
83         StrBufExtract_token(dav_uid, WCC->Hdr->HR.ReqLine, 1, '/');
84         if ((!strcasecmp(ChrPtr(dav_uid), "ics")) || 
85             (!strcasecmp(ChrPtr(dav_uid), "calendar.ics"))) {
86                 FlushStrBuf(dav_uid);
87         }
88
89         /* Go to the correct room. */
90         if (strcasecmp(ChrPtr(WC->wc_roomname), ChrPtr(dav_roomname))) {
91                 gotoroom(dav_roomname);
92         }
93         if (strcasecmp(ChrPtr(WC->wc_roomname), ChrPtr(dav_roomname))) {
94                 hprintf("HTTP/1.1 404 not found\r\n");
95                 groupdav_common_headers();
96                 hprintf("Content-Type: text/plain\r\n");
97                 begin_burst();
98                 wprintf("There is no folder called \"%s\" on this server.\r\n",
99                         ChrPtr(dav_roomname));
100                 end_burst();
101                 FreeStrBuf(&dav_roomname);
102                 FreeStrBuf(&dav_uid);           
103                 return;
104         }
105
106         /*
107          * If an HTTP If-Match: header is present, the client is attempting
108          * to replace an existing item.  We have to check to see if the
109          * message number associated with the supplied uid matches what the
110          * client is expecting.  If not, the server probably contains a newer
111          * version, so we fail...
112          */
113         if (StrLength(WCC->Hdr->HR.dav_ifmatch) > 0) {
114                 lprintf(9, "dav_ifmatch: %s\n", WCC->Hdr->HR.dav_ifmatch);
115                 old_msgnum = locate_message_by_uid(ChrPtr(dav_uid));
116                 lprintf(9, "old_msgnum:  %ld\n", old_msgnum);
117                 if (StrTol(WCC->Hdr->HR.dav_ifmatch) != old_msgnum) {
118                         hprintf("HTTP/1.1 412 Precondition Failed\r\n");
119                         lprintf(9, "HTTP/1.1 412 Precondition Failed (ifmatch=%ld, old_msgnum=%ld)\r\n",
120                                 StrTol(WCC->Hdr->HR.dav_ifmatch), old_msgnum);
121                         groupdav_common_headers();
122                         
123                         end_burst();
124                         FreeStrBuf(&dav_roomname);
125                         FreeStrBuf(&dav_uid);
126                         return;
127                 }
128         }
129
130         /** PUT on the collection itself uploads an ICS of the entire collection.
131          */
132         if (StrLength(dav_uid) == 0) {
133                 groupdav_put_bigics();
134                 FreeStrBuf(&dav_roomname);
135                 FreeStrBuf(&dav_uid);
136                 return;
137         }
138
139         /*
140          * We are cleared for upload!  We use the new calling syntax for ENT0
141          * which allows a confirmation to be sent back to us.  That's how we
142          * extract the message ID.
143          */
144         serv_puts("ENT0 1|||4|||1|");
145         serv_getln(buf, sizeof buf);
146         if (buf[0] != '8') {
147                 hprintf("HTTP/1.1 502 Bad Gateway\r\n");
148                 groupdav_common_headers();
149                 hprintf("Content-type: text/plain\r\n");
150                 begin_burst();
151                 wprintf("%s\r\n", &buf[4]);
152                 end_burst();
153                 return;
154         }
155
156         /* Send the content to the Citadel server */
157         //serv_printf("Content-type: %s\n\n", WCC->upload_content_type);
158         serv_putbuf(WCC->upload);
159         serv_puts("\n000");
160
161         /* Fetch the reply from the Citadel server */
162         n = 0;
163         FlushStrBuf(dav_uid);
164         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
165                 switch(n++) {
166                 case 0: 
167                         new_msgnum = atol(buf);
168                         break;
169                 case 1: 
170                         lprintf(9, "new_msgnum=%ld (%s)\n", new_msgnum, buf);
171                         break;
172                 case 2: 
173                         StrBufAppendBufPlain(dav_uid, buf, -1, 0);
174                         break;
175                 default:
176                         break;
177                 }
178         }
179
180         /* Tell the client what happened. */
181
182         /* Citadel failed in some way? */
183         if (new_msgnum < 0L) {
184                 hprintf("HTTP/1.1 502 Bad Gateway\r\n");
185                 groupdav_common_headers();
186                 hprintf("Content-type: text/plain\r\n");
187                 begin_burst();
188                 wprintf("new_msgnum is %ld\r\n"
189                         "\r\n", new_msgnum);
190                 end_burst();
191                 FreeStrBuf(&dav_roomname);
192                 FreeStrBuf(&dav_uid);
193                 return;
194         }
195
196         /* We created this item for the first time. */
197         if (old_msgnum < 0L) {
198                 char escaped_uid[1024];
199                 hprintf("HTTP/1.1 201 Created\r\n");
200                 lprintf(9, "HTTP/1.1 201 Created\r\n");
201                 groupdav_common_headers();
202                 hprintf("etag: \"%ld\"\r\n", new_msgnum);
203                 hprintf("Location: ");
204                 groupdav_identify_hosthdr();
205                 hprintf("/groupdav/");/* TODO */
206                 hurlescputs(ChrPtr(dav_roomname));
207                 euid_escapize(escaped_uid, ChrPtr(dav_uid));
208                 hprintf("/%s\r\n", escaped_uid);
209                 end_burst();
210                 FreeStrBuf(&dav_roomname);
211                 FreeStrBuf(&dav_uid);
212                 return;
213         }
214
215         /* We modified an existing item. */
216         hprintf("HTTP/1.1 204 No Content\r\n");
217         lprintf(9, "HTTP/1.1 204 No Content\r\n");
218         groupdav_common_headers();
219         hprintf("Etag: \"%ld\"\r\n", new_msgnum);
220         /* The item we replaced has probably already been deleted by
221          * the Citadel server, but we'll do this anyway, just in case.
222          */
223         serv_printf("DELE %ld", old_msgnum);
224         serv_getln(buf, sizeof buf);
225         begin_burst();
226         end_burst();
227         FreeStrBuf(&dav_roomname);
228         FreeStrBuf(&dav_uid);
229         return;
230 }