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