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(StrBuf
*dav_content
, int offset
)
23 * Tell the server that when we save a calendar event, we
24 * do *not* want the server to generate invitations.
26 serv_puts("ICAL sgi|0");
27 serv_getln(buf
, sizeof buf
);
29 serv_puts("ICAL putics");
30 serv_getln(buf
, sizeof buf
);
32 hprintf("HTTP/1.1 502 Bad Gateway\r\n");
33 groupdav_common_headers();
34 hprintf("Content-type: text/plain\r\n");
35 wprintf("%s\r\n", &buf
[4]);
40 serv_write(ChrPtr(dav_content
) + offset
, StrLength(dav_content
) - offset
);
43 /* Report success and not much else. */
44 hprintf("HTTP/1.1 204 No Content\r\n");
45 lprintf(9, "HTTP/1.1 204 No Content\r\n");
46 groupdav_common_headers();
54 * The pathname is always going to take one of two formats:
55 * /groupdav/room_name/euid (GroupDAV)
56 * /groupdav/room_name (webcal)
58 void groupdav_put(StrBuf
*dav_pathname
, char *dav_ifmatch
,
59 const char *dav_content_type
, StrBuf
*dav_content
,
64 long new_msgnum
= (-2L);
65 long old_msgnum
= (-1L);
69 if (StrBufNum_tokens(dav_pathname
, '/') < 3) {
70 hprintf("HTTP/1.1 404 not found\r\n");
71 groupdav_common_headers();
72 hprintf("Content-Type: text/plain\r\n");
73 wprintf("The object you requested was not found.\r\n");
78 dav_roomname
= NewStrBuf();;
79 dav_uid
= NewStrBuf();;
80 StrBufExtract_token(dav_roomname
, dav_pathname
, 2, '/');
81 StrBufExtract_token(dav_uid
, dav_pathname
, 3, '/');
82 if ((!strcasecmp(ChrPtr(dav_uid
), "ics")) ||
83 (!strcasecmp(ChrPtr(dav_uid
), "calendar.ics"))) {
87 /* Go to the correct room. */
88 if (strcasecmp(ChrPtr(WC
->wc_roomname
), ChrPtr(dav_roomname
))) {
89 gotoroom(dav_roomname
);
91 if (strcasecmp(ChrPtr(WC
->wc_roomname
), ChrPtr(dav_roomname
))) {
92 hprintf("HTTP/1.1 404 not found\r\n");
93 groupdav_common_headers();
94 hprintf("Content-Type: text/plain\r\n");
95 wprintf("There is no folder called \"%s\" on this server.\r\n",
96 ChrPtr(dav_roomname
));
98 FreeStrBuf(&dav_roomname
);
104 * If an HTTP If-Match: header is present, the client is attempting
105 * to replace an existing item. We have to check to see if the
106 * message number associated with the supplied uid matches what the
107 * client is expecting. If not, the server probably contains a newer
108 * version, so we fail...
110 if (!IsEmptyStr(dav_ifmatch
)) {
111 lprintf(9, "dav_ifmatch: %s\n", dav_ifmatch
);
112 old_msgnum
= locate_message_by_uid(ChrPtr(dav_uid
));
113 lprintf(9, "old_msgnum: %ld\n", old_msgnum
);
114 if (atol(dav_ifmatch
) != old_msgnum
) {
115 hprintf("HTTP/1.1 412 Precondition Failed\r\n");
116 lprintf(9, "HTTP/1.1 412 Precondition Failed (ifmatch=%ld, old_msgnum=%ld)\r\n",
117 atol(dav_ifmatch
), old_msgnum
);
118 groupdav_common_headers();
119 hprintf("Content-Length: 0\r\n");
121 FreeStrBuf(&dav_roomname
);
122 FreeStrBuf(&dav_uid
);
127 /** PUT on the collection itself uploads an ICS of the entire collection.
129 if (StrLength(dav_uid
) == 0) {
130 groupdav_put_bigics(dav_content
, offset
);
131 FreeStrBuf(&dav_roomname
);
132 FreeStrBuf(&dav_uid
);
137 * We are cleared for upload! We use the new calling syntax for ENT0
138 * which allows a confirmation to be sent back to us. That's how we
139 * extract the message ID.
141 serv_puts("ENT0 1|||4|||1|");
142 serv_getln(buf
, sizeof buf
);
144 hprintf("HTTP/1.1 502 Bad Gateway\r\n");
145 groupdav_common_headers();
146 hprintf("Content-type: text/plain\r\n");
148 wprintf("%s\r\n", &buf
[4]);
153 /* Send the content to the Citadel server */
154 serv_printf("Content-type: %s\n\n", dav_content_type
);
155 serv_puts(ChrPtr(dav_content
) + offset
);
158 /* Fetch the reply from the Citadel server */
160 FlushStrBuf(dav_uid
);
161 while (serv_getln(buf
, sizeof buf
), strcmp(buf
, "000")) {
164 new_msgnum
= atol(buf
);
167 lprintf(9, "new_msgnum=%ld (%s)\n", new_msgnum
, buf
);
170 StrBufAppendBufPlain(dav_uid
, buf
, -1, 0);
177 /* Tell the client what happened. */
179 /* Citadel failed in some way? */
180 if (new_msgnum
< 0L) {
181 hprintf("HTTP/1.1 502 Bad Gateway\r\n");
182 groupdav_common_headers();
183 hprintf("Content-type: text/plain\r\n");
184 wprintf("new_msgnum is %ld\r\n"
187 FreeStrBuf(&dav_roomname
);
188 FreeStrBuf(&dav_uid
);
192 /* We created this item for the first time. */
193 if (old_msgnum
< 0L) {
194 char escaped_uid
[1024];
195 hprintf("HTTP/1.1 201 Created\r\n");
196 lprintf(9, "HTTP/1.1 201 Created\r\n");
197 groupdav_common_headers();
198 hprintf("etag: \"%ld\"\r\n", new_msgnum
);
199 hprintf("Location: ");
200 groupdav_identify_host();
201 hprintf("/groupdav/");/* TODO */
202 hurlescputs(ChrPtr(dav_roomname
));
203 euid_escapize(escaped_uid
, ChrPtr(dav_uid
));
204 hprintf("/%s\r\n", escaped_uid
);
206 FreeStrBuf(&dav_roomname
);
207 FreeStrBuf(&dav_uid
);
211 /* We modified an existing item. */
212 hprintf("HTTP/1.1 204 No Content\r\n");
213 lprintf(9, "HTTP/1.1 204 No Content\r\n");
214 groupdav_common_headers();
215 hprintf("Etag: \"%ld\"\r\n", new_msgnum
);
216 /* The item we replaced has probably already been deleted by
217 * the Citadel server, but we'll do this anyway, just in case.
219 serv_printf("DELE %ld", old_msgnum
);
220 serv_getln(buf
, sizeof buf
);
223 FreeStrBuf(&dav_roomname
);
224 FreeStrBuf(&dav_uid
);