4 * Handles GroupDAV PUT requests.
14 * This function is for uploading an ENTIRE calendar, not just one
15 * component. This would be for webcal:// 'publish' operations, not
18 void groupdav_put_bigics(char *dav_content, int dav_content_length)
22 serv_puts("ICAL putics");
23 serv_getln(buf, sizeof buf);
25 wprintf("HTTP/1.1 502 Bad Gateway\r\n");
26 groupdav_common_headers();
27 wprintf("Content-type: text/plain\r\n"
34 client_write(dav_content, dav_content_length);
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");
47 * The pathname is always going to be /groupdav/room_name/euid
49 void groupdav_put(char *dav_pathname, char *dav_ifmatch,
50 char *dav_content_type, char *dav_content,
51 int dav_content_length
53 char dav_roomname[1024];
55 long new_msgnum = (-2L);
56 long old_msgnum = (-1L);
59 int is_bigics = 0; /* nonzero == doing a webcal publish */
61 /* First, break off the "/groupdav/" prefix */
62 remove_token(dav_pathname, 0, '/');
63 remove_token(dav_pathname, 0, '/');
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, '/');
70 /* What's left is the room name. Check for webcal publish syntax... */
72 if (!strcasecmp(&dav_pathname[strlen(dav_pathname)-4], "/ics")) {
73 dav_pathname[strlen(dav_pathname)-4] = 0;
76 for (n=0; n<strlen(dav_pathname); ++n) {
77 if (!strncasecmp(&dav_pathname[n], "/ics/", 5)) {
83 /* ...now remove trailing slashes. */
84 if (dav_pathname[strlen(dav_pathname)-1] == '/') {
85 dav_pathname[strlen(dav_pathname)-1] = 0;
87 strcpy(dav_roomname, dav_pathname);
89 /* Go to the correct room. */
90 if (strcasecmp(WC->wc_roomname, dav_roomname)) {
91 gotoroom(dav_roomname);
93 if (strcasecmp(WC->wc_roomname, dav_roomname)) {
94 wprintf("HTTP/1.1 404 not found\r\n");
95 groupdav_common_headers();
97 "Content-Type: text/plain\r\n"
99 "There is no folder called \"%s\" on this server.\r\n",
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...
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");
127 groupdav_put_bigics(dav_content, dav_content_length);
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.
136 serv_puts("ENT0 1|||4|||1|");
137 serv_getln(buf, sizeof buf);
139 wprintf("HTTP/1.1 502 Bad Gateway\r\n");
140 groupdav_common_headers();
141 wprintf("Content-type: text/plain\r\n"
148 /* Send the content to the Citadel server */
149 serv_printf("Content-type: %s\n\n", dav_content_type);
150 serv_puts(dav_content);
153 /* Fetch the reply from the Citadel server */
156 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
158 case 0: new_msgnum = atol(buf);
160 case 1: lprintf(9, "new_msgnum=%ld (%s)\n", new_msgnum, buf);
162 case 2: strcpy(dav_uid, buf);
169 /* Tell the client what happened. */
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"
177 "new_msgnum is %ld\r\n"
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);
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");
206 /* The item we replaced has probably already been deleted by
207 * the Citadel server, but we'll do this anyway, just in case.
209 serv_printf("DELE %ld", old_msgnum);
210 serv_getln(buf, sizeof buf);