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 old_msgnum = locate_message_by_uid(dav_uid);
66 if (atol(dav_ifmatch) != old_msgnum) {
67 wprintf("HTTP/1.1 412 Precondition Failed\r\n");
68 lprintf(9, "HTTP/1.1 412 Precondition Failed (ifmatch=%ld, old_msgnum=%ld)\r\n",
69 atol(dav_ifmatch), old_msgnum);
70 groupdav_common_headers();
71 wprintf("Content-Length: 0\r\n\r\n");
77 * We are cleared for upload! We use the new calling syntax for ENT0
78 * which allows a confirmation to be sent back to us. That's how we
79 * extract the message ID.
81 serv_puts("ENT0 1|||4|||1|");
82 serv_getln(buf, sizeof buf);
84 wprintf("HTTP/1.1 502 Bad Gateway\r\n");
85 groupdav_common_headers();
86 wprintf("Content-type: text/plain\r\n"
93 /* Send the content to the Citadel server */
94 serv_printf("Content-type: %s\n\n", dav_content_type);
95 serv_puts(dav_content);
98 /* Fetch the reply from the Citadel server */
101 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
103 case 0: new_msgnum = atol(buf);
105 case 1: lprintf(9, "new_msgnum=%ld (%s)\n", new_msgnum, buf);
107 case 2: strcpy(dav_uid, buf);
114 /* Tell the client what happened. */
116 /* Citadel failed in some way? */
117 if (new_msgnum < 0L) {
118 wprintf("HTTP/1.1 502 Bad Gateway\r\n");
119 groupdav_common_headers();
120 wprintf("Content-type: text/plain\r\n"
122 "new_msgnum is %ld\r\n"
128 /* We created this item for the first time. */
129 if (old_msgnum < 0L) {
130 wprintf("HTTP/1.1 201 Created\r\n");
131 lprintf(9, "HTTP/1.1 201 Created\r\n");
132 groupdav_common_headers();
133 wprintf("etag: \"%ld\"\r\n", new_msgnum);
134 wprintf("Content-Length: 0\r\n");
135 wprintf("Location: ");
136 if (strlen(WC->http_host) > 0) {
138 (is_https ? "https" : "http"),
141 wprintf("/groupdav/");
142 urlescputs(dav_roomname);
143 wprintf("/%s\r\n", dav_uid);
148 /* We modified an existing item. */
149 wprintf("HTTP/1.1 204 No Content\r\n");
150 lprintf(9, "HTTP/1.1 204 No Content\r\n");
151 groupdav_common_headers();
152 wprintf("etag: \"%ld\"\r\n", new_msgnum);
153 wprintf("Content-Length: 0\r\n\r\n");
155 /* The item we replaced has probably already been deleted by
156 * the Citadel server, but we'll do this anyway, just in case.
158 serv_printf("DELE %ld", old_msgnum);
159 serv_getln(buf, sizeof buf);