Altered the URL format for PUT an entire calendar.
[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
76         /* Go to the correct room. */
77         if (strcasecmp(WC->wc_roomname, dav_roomname)) {
78                 gotoroom(dav_roomname);
79         }
80         if (strcasecmp(WC->wc_roomname, dav_roomname)) {
81                 wprintf("HTTP/1.1 404 not found\r\n");
82                 groupdav_common_headers();
83                 wprintf(
84                         "Content-Type: text/plain\r\n"
85                         "\r\n"
86                         "There is no folder called \"%s\" on this server.\r\n",
87                         dav_roomname
88                 );
89                 return;
90         }
91
92         /*
93          * If an HTTP If-Match: header is present, the client is attempting
94          * to replace an existing item.  We have to check to see if the
95          * message number associated with the supplied uid matches what the
96          * client is expecting.  If not, the server probably contains a newer
97          * version, so we fail...
98          */
99         if (strlen(dav_ifmatch) > 0) {
100                 lprintf(9, "dav_ifmatch: %s\n", dav_ifmatch);
101                 old_msgnum = locate_message_by_uid(dav_uid);
102                 lprintf(9, "old_msgnum:  %ld\n", old_msgnum);
103                 if (atol(dav_ifmatch) != old_msgnum) {
104                         wprintf("HTTP/1.1 412 Precondition Failed\r\n");
105                         lprintf(9, "HTTP/1.1 412 Precondition Failed (ifmatch=%ld, old_msgnum=%ld)\r\n",
106                                 atol(dav_ifmatch), old_msgnum);
107                         groupdav_common_headers();
108                         wprintf("Content-Length: 0\r\n\r\n");
109                         return;
110                 }
111         }
112
113         /** PUT on the collection itself uploads an ICS of the entire collection.
114          */
115         if (!strcasecmp(dav_uid, "")) {
116                 groupdav_put_bigics(dav_content, dav_content_length);
117                 return;
118         }
119
120         /*
121          * We are cleared for upload!  We use the new calling syntax for ENT0
122          * which allows a confirmation to be sent back to us.  That's how we
123          * extract the message ID.
124          */
125         serv_puts("ENT0 1|||4|||1|");
126         serv_getln(buf, sizeof buf);
127         if (buf[0] != '8') {
128                 wprintf("HTTP/1.1 502 Bad Gateway\r\n");
129                 groupdav_common_headers();
130                 wprintf("Content-type: text/plain\r\n"
131                         "\r\n"
132                         "%s\r\n", &buf[4]
133                 );
134                 return;
135         }
136
137         /* Send the content to the Citadel server */
138         serv_printf("Content-type: %s\n\n", dav_content_type);
139         serv_puts(dav_content);
140         serv_puts("\n000");
141
142         /* Fetch the reply from the Citadel server */
143         n = 0;
144         strcpy(dav_uid, "");
145         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
146                 switch(n++) {
147                         case 0: new_msgnum = atol(buf);
148                                 break;
149                         case 1: lprintf(9, "new_msgnum=%ld (%s)\n", new_msgnum, buf);
150                                 break;
151                         case 2: strcpy(dav_uid, buf);
152                                 break;
153                         default:
154                                 break;
155                 }
156         }
157
158         /* Tell the client what happened. */
159
160         /* Citadel failed in some way? */
161         if (new_msgnum < 0L) {
162                 wprintf("HTTP/1.1 502 Bad Gateway\r\n");
163                 groupdav_common_headers();
164                 wprintf("Content-type: text/plain\r\n"
165                         "\r\n"
166                         "new_msgnum is %ld\r\n"
167                         "\r\n", new_msgnum
168                 );
169                 return;
170         }
171
172         /* We created this item for the first time. */
173         if (old_msgnum < 0L) {
174                 wprintf("HTTP/1.1 201 Created\r\n");
175                 lprintf(9, "HTTP/1.1 201 Created\r\n");
176                 groupdav_common_headers();
177                 wprintf("etag: \"%ld\"\r\n", new_msgnum);
178                 wprintf("Content-Length: 0\r\n");
179                 wprintf("Location: ");
180                 groupdav_identify_host();
181                 wprintf("/groupdav/");
182                 urlescputs(dav_roomname);
183                 wprintf("/%s\r\n", dav_uid);
184                 wprintf("\r\n");
185                 return;
186         }
187
188         /* We modified an existing item. */
189         wprintf("HTTP/1.1 204 No Content\r\n");
190         lprintf(9, "HTTP/1.1 204 No Content\r\n");
191         groupdav_common_headers();
192         wprintf("etag: \"%ld\"\r\n", new_msgnum);
193         wprintf("Content-Length: 0\r\n\r\n");
194
195         /* The item we replaced has probably already been deleted by
196          * the Citadel server, but we'll do this anyway, just in case.
197          */
198         serv_printf("DELE %ld", old_msgnum);
199         serv_getln(buf, sizeof buf);
200
201         return;
202 }