4 * Handles GroupDAV PUT requests.
14 * The pathname is always going to be /groupdav/room_name/euid
16 void groupdav_put(char *dav_pathname, char *dav_ifmatch,
17 char *dav_content_type, char *dav_content
19 char dav_roomname[SIZ];
21 long new_msgnum = (-2L);
22 long old_msgnum = (-1L);
26 /* First, break off the "/groupdav/" prefix */
27 remove_token(dav_pathname, 0, '/');
28 remove_token(dav_pathname, 0, '/');
30 /* Now extract the message euid */
31 n = num_tokens(dav_pathname, '/');
32 extract_token(dav_uid, dav_pathname, n-1, '/', sizeof dav_uid);
33 remove_token(dav_pathname, n-1, '/');
35 /* What's left is the room name. Remove trailing slashes. */
36 if (dav_pathname[strlen(dav_pathname)-1] == '/') {
37 dav_pathname[strlen(dav_pathname)-1] = 0;
39 strcpy(dav_roomname, dav_pathname);
41 /* Go to the correct room. */
42 if (strcasecmp(WC->wc_roomname, dav_roomname)) {
43 gotoroom(dav_roomname);
45 if (strcasecmp(WC->wc_roomname, dav_roomname)) {
46 wprintf("HTTP/1.1 404 not found\r\n");
47 groupdav_common_headers();
49 "Content-Type: text/plain\r\n"
51 "There is no folder called \"%s\" on this server.\r\n",
58 * If an HTTP If-Match: header is present, the client is attempting
59 * to replace an existing item. We have to check to see if the
60 * message number associated with the supplied uid matches what the
61 * client is expecting. If not, the server probably contains a newer
62 * version, so we fail...
64 if (strlen(dav_ifmatch) > 0) {
65 lprintf(9, "dav_ifmatch: %s\n", dav_ifmatch);
66 old_msgnum = locate_message_by_uid(dav_uid);
67 lprintf(9, "old_msgnum: %ld\n", old_msgnum);
68 if (atol(dav_ifmatch) != old_msgnum) {
69 wprintf("HTTP/1.1 412 Precondition Failed\r\n");
70 lprintf(9, "HTTP/1.1 412 Precondition Failed (ifmatch=%ld, old_msgnum=%ld)\r\n",
71 atol(dav_ifmatch), old_msgnum);
72 groupdav_common_headers();
73 wprintf("Content-Length: 0\r\n\r\n");
79 * We are cleared for upload! We use the new calling syntax for ENT0
80 * which allows a confirmation to be sent back to us. That's how we
81 * extract the message ID.
83 serv_puts("ENT0 1|||4|||1|");
84 serv_getln(buf, sizeof buf);
86 wprintf("HTTP/1.1 502 Bad Gateway\r\n");
87 groupdav_common_headers();
88 wprintf("Content-type: text/plain\r\n"
95 /* Send the content to the Citadel server */
96 serv_printf("Content-type: %s\n\n", dav_content_type);
97 serv_puts(dav_content);
100 /* Fetch the reply from the Citadel server */
103 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
105 case 0: new_msgnum = atol(buf);
107 case 1: lprintf(9, "new_msgnum=%ld (%s)\n", new_msgnum, buf);
109 case 2: strcpy(dav_uid, buf);
116 /* Tell the client what happened. */
118 /* Citadel failed in some way? */
119 if (new_msgnum < 0L) {
120 wprintf("HTTP/1.1 502 Bad Gateway\r\n");
121 groupdav_common_headers();
122 wprintf("Content-type: text/plain\r\n"
124 "new_msgnum is %ld\r\n"
130 /* We created this item for the first time. */
131 if (old_msgnum < 0L) {
132 wprintf("HTTP/1.1 201 Created\r\n");
133 lprintf(9, "HTTP/1.1 201 Created\r\n");
134 groupdav_common_headers();
135 wprintf("etag: \"%ld\"\r\n", new_msgnum);
136 wprintf("Content-Length: 0\r\n");
137 wprintf("Location: ");
138 if (strlen(WC->http_host) > 0) {
140 (is_https ? "https" : "http"),
143 wprintf("/groupdav/");
144 urlescputs(dav_roomname);
145 wprintf("/%s\r\n", dav_uid);
150 /* We modified an existing item. */
151 wprintf("HTTP/1.1 204 No Content\r\n");
152 lprintf(9, "HTTP/1.1 204 No Content\r\n");
153 groupdav_common_headers();
154 wprintf("etag: \"%ld\"\r\n", new_msgnum);
155 wprintf("Content-Length: 0\r\n\r\n");
157 /* The item we replaced has probably already been deleted by
158 * the Citadel server, but we'll do this anyway, just in case.
160 serv_printf("DELE %ld", old_msgnum);
161 serv_getln(buf, sizeof buf);