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