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