5 * \defgroup GroupdavPut Handles GroupDAV PUT requests.
6 * \ingroup WebcitHttpServerGDav
11 #include "webserver.h"
16 * \brief GroupDAV PUT an item to the server
17 * \param dav_pathname The pathname is always going to be /groupdav/room_name/euid
18 * \param dav_ifmatch ETag of the item we think we're replacing
19 * \param dav_content_type the mime type
20 * \param dav_content the actual data
22 void groupdav_put(char *dav_pathname, char *dav_ifmatch,
23 char *dav_content_type, char *dav_content
25 char dav_roomname[SIZ];
27 long new_msgnum = (-2L);
28 long old_msgnum = (-1L);
32 /** First, break off the "/groupdav/" prefix */
33 remove_token(dav_pathname, 0, '/');
34 remove_token(dav_pathname, 0, '/');
36 /** Now extract the message euid */
37 n = num_tokens(dav_pathname, '/');
38 extract_token(dav_uid, dav_pathname, n-1, '/', sizeof dav_uid);
39 remove_token(dav_pathname, n-1, '/');
41 /** What's left is the room name. Remove trailing slashes. */
42 if (dav_pathname[strlen(dav_pathname)-1] == '/') {
43 dav_pathname[strlen(dav_pathname)-1] = 0;
45 strcpy(dav_roomname, dav_pathname);
47 /** Go to the correct room. */
48 if (strcasecmp(WC->wc_roomname, dav_roomname)) {
49 gotoroom(dav_roomname);
51 if (strcasecmp(WC->wc_roomname, dav_roomname)) {
52 wprintf("HTTP/1.1 404 not found\r\n");
53 groupdav_common_headers();
55 "Content-Type: text/plain\r\n"
57 "There is no folder called \"%s\" on this server.\r\n",
64 * If an HTTP If-Match: header is present, the client is attempting
65 * to replace an existing item. We have to check to see if the
66 * message number associated with the supplied uid matches what the
67 * client is expecting. If not, the server probably contains a newer
68 * version, so we fail...
70 if (strlen(dav_ifmatch) > 0) {
71 old_msgnum = locate_message_by_uid(dav_uid);
72 if (atol(dav_ifmatch) != old_msgnum) {
73 wprintf("HTTP/1.1 412 Precondition Failed\r\n");
74 lprintf(9, "HTTP/1.1 412 Precondition Failed (ifmatch=%ld, old_msgnum=%ld)\r\n",
75 atol(dav_ifmatch), old_msgnum);
76 groupdav_common_headers();
77 wprintf("Content-Length: 0\r\n\r\n");
83 * We are cleared for upload! We use the new calling syntax for ENT0
84 * which allows a confirmation to be sent back to us. That's how we
85 * extract the message ID.
87 serv_puts("ENT0 1|||4|||1|");
88 serv_getln(buf, sizeof buf);
90 wprintf("HTTP/1.1 502 Bad Gateway\r\n");
91 groupdav_common_headers();
92 wprintf("Content-type: text/plain\r\n"
99 /** Send the content to the Citadel server */
100 serv_printf("Content-type: %s\n\n", dav_content_type);
101 serv_puts(dav_content);
104 /** Fetch the reply from the Citadel server */
107 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
109 case 0: new_msgnum = atol(buf);
111 case 1: lprintf(9, "new_msgnum=%ld (%s)\n", new_msgnum, buf);
113 case 2: strcpy(dav_uid, buf);
120 /** Tell the client what happened. */
122 /** Citadel failed in some way? */
123 if (new_msgnum < 0L) {
124 wprintf("HTTP/1.1 502 Bad Gateway\r\n");
125 groupdav_common_headers();
126 wprintf("Content-type: text/plain\r\n"
128 "new_msgnum is %ld\r\n"
134 /** We created this item for the first time. */
135 if (old_msgnum < 0L) {
136 wprintf("HTTP/1.1 201 Created\r\n");
137 lprintf(9, "HTTP/1.1 201 Created\r\n");
138 groupdav_common_headers();
139 wprintf("etag: \"%ld\"\r\n", new_msgnum);
140 wprintf("Content-Length: 0\r\n");
141 wprintf("Location: ");
143 if (strlen(WC->http_host) > 0) {
145 (is_https ? "https" : "http"),
149 wprintf("/groupdav/");
150 urlescputs(dav_roomname);
151 wprintf("/%s\r\n", dav_uid);
156 /** We modified an existing item. */
157 wprintf("HTTP/1.1 204 No Content\r\n");
158 lprintf(9, "HTTP/1.1 204 No Content\r\n");
159 groupdav_common_headers();
160 wprintf("etag: \"%ld\"\r\n", new_msgnum);
161 wprintf("Content-Length: 0\r\n\r\n");
164 * The item we replaced has probably already been deleted by
165 * the Citadel server, but we'll do this anyway, just in case.
167 serv_printf("DELE %ld", old_msgnum);
168 serv_getln(buf, sizeof buf);