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