2 * Handles GroupDAV GET requests.
4 * Copyright (c) 2005-2012 by the citadel.org team
6 * This program is open source software. You can redistribute it and/or
7 * modify it under the terms of the GNU General Public License, version 3.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
16 #include "webserver.h"
21 * Fetch the entire contents of the room as one big ics file.
22 * This is for "webcal://" type access.
24 void dav_get_big_ics(void) {
27 serv_puts("ICAL getics");
28 serv_getln(buf, sizeof buf);
30 hprintf("HTTP/1.1 404 not found\r\n");
32 hprintf("Content-Type: text/plain\r\n");
41 hprintf("HTTP/1.1 200 OK\r\n");
43 hprintf("Content-type: text/calendar; charset=UTF-8\r\n");
45 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
46 wc_printf("%s\r\n", buf);
53 * MIME parser callback function for dav_get()
54 * Helps identify the relevant section of a multipart message
56 void extract_preferred(char *name, char *filename, char *partnum, char *disp,
57 void *content, char *cbtype, char *cbcharset,
58 size_t length, char *encoding, char *cbid, void *userdata)
60 struct epdata *epdata = (struct epdata *)userdata;
63 /* We only want the first one that we found */
64 if (!IsEmptyStr(epdata->found_section)) return;
66 /* Check for a content type match */
67 if (strlen(epdata->desired_content_type_1) > 0) {
68 if (!strcasecmp(epdata->desired_content_type_1, cbtype)) {
72 if (!IsEmptyStr(epdata->desired_content_type_2)) {
73 if (!strcasecmp(epdata->desired_content_type_2, cbtype)) {
78 /* Is this the one? If so, output it. */
80 safestrncpy(epdata->found_section, partnum, sizeof epdata->found_section);
81 if (!IsEmptyStr(cbcharset)) {
82 safestrncpy(epdata->charset, cbcharset, sizeof epdata->charset);
84 hprintf("Content-type: %s; charset=%s\r\n", cbtype, epdata->charset);
86 StrBufAppendBufPlain(WC->WBuf, content, length, 0);
94 * The pathname is always going to take one of two formats:
95 * /groupdav/room_name/euid (GroupDAV)
96 * /groupdav/room_name (webcal)
101 StrBuf *dav_roomname;
103 long dav_msgnum = (-1);
108 char *msgtext = NULL;
112 char content_type[128];
115 struct epdata epdata;
117 if (StrBufNum_tokens(WCC->Hdr->HR.ReqLine, '/') < 2) {
118 hprintf("HTTP/1.1 404 not found\r\n");
119 dav_common_headers();
120 hprintf("Content-Type: text/plain\r\n");
121 wc_printf("The object you requested was not found.\r\n");
126 dav_roomname = NewStrBuf();;
127 dav_uid = NewStrBuf();;
128 StrBufExtract_token(dav_roomname, WCC->Hdr->HR.ReqLine, 0, '/');
129 StrBufExtract_token(dav_uid, WCC->Hdr->HR.ReqLine, 1, '/');
130 if ((!strcasecmp(ChrPtr(dav_uid), "ics")) ||
131 (!strcasecmp(ChrPtr(dav_uid), "calendar.ics"))) {
132 FlushStrBuf(dav_uid);
135 /* Go to the correct room. */
136 if (strcasecmp(ChrPtr(WCC->CurRoom.name), ChrPtr(dav_roomname))) {
137 gotoroom(dav_roomname);
139 if (strcasecmp(ChrPtr(WCC->CurRoom.name), ChrPtr(dav_roomname))) {
140 hprintf("HTTP/1.1 404 not found\r\n");
141 dav_common_headers();
142 hprintf("Content-Type: text/plain\r\n");
143 wc_printf("There is no folder called \"%s\" on this server.\r\n",
144 ChrPtr(dav_roomname));
146 FreeStrBuf(&dav_roomname);
147 FreeStrBuf(&dav_uid);
151 /** GET on the collection itself returns an ICS of the entire collection.
153 if (StrLength(dav_uid) == 0) {
155 FreeStrBuf(&dav_roomname);
156 FreeStrBuf(&dav_uid);
160 dav_msgnum = locate_message_by_uid(ChrPtr(dav_uid));
161 serv_printf("MSG2 %ld", dav_msgnum);
162 serv_getln(buf, sizeof buf);
164 hprintf("HTTP/1.1 404 not found\r\n");
165 dav_common_headers();
166 hprintf("Content-Type: text/plain\r\n");
167 wc_printf("Object \"%s\" was not found in the \"%s\" folder.\r\n",
169 ChrPtr(dav_roomname));
171 FreeStrBuf(&dav_roomname);
172 FreeStrBuf(&dav_uid);
175 FreeStrBuf(&dav_roomname);
176 FreeStrBuf(&dav_uid);
178 /* We got it; a message is now arriving from the server. Read it in. */
181 strcpy(charset, "UTF-8");
182 strcpy(content_type, "text/plain");
184 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
185 linelen = strlen(buf);
187 /* Append it to the buffer */
188 if ((msglen + linelen + 3) > msgalloc) {
189 msgalloc = ( (msgalloc > 0) ? (msgalloc * 2) : 1024 );
190 msgtext = realloc(msgtext, msgalloc);
192 strcpy(&msgtext[msglen], buf);
194 strcpy(&msgtext[msglen], "\n");
197 /* Also learn some things about the message */
202 if (!strncasecmp(buf, "Date:", 5)) {
203 safestrncpy(date, &buf[5], sizeof date);
206 if (!strncasecmp(buf, "Content-type:", 13)) {
207 safestrncpy(content_type, &buf[13], sizeof content_type);
208 string_trim(content_type);
209 ptr = bmstrcasestr(&buf[13], "charset=");
211 safestrncpy(charset, ptr+8, sizeof charset);
212 string_trim(charset);
213 endptr = strchr(charset, ';');
214 if (endptr != NULL) strcpy(endptr, "");
216 endptr = strchr(content_type, ';');
217 if (endptr != NULL) strcpy(endptr, "");
223 /* Output headers common to single or multi part messages */
225 hprintf("HTTP/1.1 200 OK\r\n");
226 dav_common_headers();
227 hprintf("etag: \"%ld\"\r\n", dav_msgnum);
228 hprintf("Date: %s\r\n", date);
230 memset(&epdata, 0, sizeof(struct epdata));
231 safestrncpy(epdata.charset, charset, sizeof epdata.charset);
233 /* If we have a multipart message on our hands, and we are in a groupware room,
234 * strip it down to only the relevant part.
236 if (!strncasecmp(content_type, "multipart/", 10)) {
238 if ( (WCC->CurRoom.defview == VIEW_CALENDAR) || (WCC->CurRoom.defview == VIEW_TASKS) ) {
239 strcpy(epdata.desired_content_type_1, "text/calendar");
242 else if (WCC->CurRoom.defview == VIEW_ADDRESSBOOK) {
243 strcpy(epdata.desired_content_type_1, "text/vcard");
244 strcpy(epdata.desired_content_type_2, "text/x-vcard");
247 mime_parser(msgtext, &msgtext[msglen], extract_preferred, NULL, NULL, (void *)&epdata, 0);
250 /* If epdata.found_section is empty, we haven't output anything yet, so output the whole thing */
252 if (IsEmptyStr(epdata.found_section)) {
254 endptr = &msgtext[msglen];
256 hprintf("Content-type: %s; charset=%s\r\n", content_type, charset);
260 ptr = memreadline(ptr, buf, sizeof buf);
263 wc_printf("%s\r\n", buf);
265 else if ((buf[0] == 0) && (in_body == 0)) {
269 } while (ptr < endptr);