4 * Handles GroupDAV GET requests.
7 * Copyright (c) 2005-2010 by the citadel.org team
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "webserver.h"
30 * Fetch the entire contents of the room as one big ics file.
31 * This is for "webcal://" type access.
33 void groupdav_get_big_ics(void) {
36 serv_puts("ICAL getics");
37 serv_getln(buf, sizeof buf);
39 hprintf("HTTP/1.1 404 not found\r\n");
40 groupdav_common_headers();
41 hprintf("Content-Type: text/plain\r\n");
50 hprintf("HTTP/1.1 200 OK\r\n");
51 groupdav_common_headers();
52 hprintf("Content-type: text/calendar; charset=UTF-8\r\n");
54 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
55 wc_printf("%s\r\n", buf);
62 * MIME parser callback function for groupdav_get()
63 * Helps identify the relevant section of a multipart message
65 void extract_preferred(char *name, char *filename, char *partnum, char *disp,
66 void *content, char *cbtype, char *cbcharset,
67 size_t length, char *encoding, char *cbid, void *userdata)
69 struct epdata *epdata = (struct epdata *)userdata;
72 /* We only want the first one that we found */
73 if (!IsEmptyStr(epdata->found_section)) return;
75 /* Check for a content type match */
76 if (strlen(epdata->desired_content_type_1) > 0) {
77 if (!strcasecmp(epdata->desired_content_type_1, cbtype)) {
81 if (!IsEmptyStr(epdata->desired_content_type_2)) {
82 if (!strcasecmp(epdata->desired_content_type_2, cbtype)) {
87 /* Is this the one? If so, output it. */
89 safestrncpy(epdata->found_section, partnum, sizeof epdata->found_section);
90 if (!IsEmptyStr(cbcharset)) {
91 safestrncpy(epdata->charset, cbcharset, sizeof epdata->charset);
93 hprintf("Content-type: %s; charset=%s\r\n", cbtype, epdata->charset);
95 StrBufAppendBufPlain(WC->WBuf, content, length, 0);
103 * The pathname is always going to take one of two formats:
104 * /groupdav/room_name/euid (GroupDAV)
105 * /groupdav/room_name (webcal)
107 void groupdav_get(void)
110 StrBuf *dav_roomname;
112 long dav_msgnum = (-1);
115 int found_content_type = 0;
118 char *msgtext = NULL;
122 char content_type[128];
125 struct epdata epdata;
127 if (StrBufNum_tokens(WCC->Hdr->HR.ReqLine, '/') < 2) {
128 hprintf("HTTP/1.1 404 not found\r\n");
129 groupdav_common_headers();
130 hprintf("Content-Type: text/plain\r\n");
131 wc_printf("The object you requested was not found.\r\n");
136 dav_roomname = NewStrBuf();;
137 dav_uid = NewStrBuf();;
138 StrBufExtract_token(dav_roomname, WCC->Hdr->HR.ReqLine, 0, '/');
139 StrBufExtract_token(dav_uid, WCC->Hdr->HR.ReqLine, 1, '/');
140 if ((!strcasecmp(ChrPtr(dav_uid), "ics")) ||
141 (!strcasecmp(ChrPtr(dav_uid), "calendar.ics"))) {
142 FlushStrBuf(dav_uid);
145 /* Go to the correct room. */
146 if (strcasecmp(ChrPtr(WCC->CurRoom.name), ChrPtr(dav_roomname))) {
147 gotoroom(dav_roomname);
149 if (strcasecmp(ChrPtr(WCC->CurRoom.name), ChrPtr(dav_roomname))) {
150 hprintf("HTTP/1.1 404 not found\r\n");
151 groupdav_common_headers();
152 hprintf("Content-Type: text/plain\r\n");
153 wc_printf("There is no folder called \"%s\" on this server.\r\n",
154 ChrPtr(dav_roomname));
156 FreeStrBuf(&dav_roomname);
157 FreeStrBuf(&dav_uid);
161 /** GET on the collection itself returns an ICS of the entire collection.
163 if (StrLength(dav_uid) == 0) {
164 groupdav_get_big_ics();
165 FreeStrBuf(&dav_roomname);
166 FreeStrBuf(&dav_uid);
170 dav_msgnum = locate_message_by_uid(ChrPtr(dav_uid));
171 serv_printf("MSG2 %ld", dav_msgnum);
172 serv_getln(buf, sizeof buf);
174 hprintf("HTTP/1.1 404 not found\r\n");
175 groupdav_common_headers();
176 hprintf("Content-Type: text/plain\r\n");
177 wc_printf("Object \"%s\" was not found in the \"%s\" folder.\r\n",
179 ChrPtr(dav_roomname));
181 FreeStrBuf(&dav_roomname);
182 FreeStrBuf(&dav_uid);
185 FreeStrBuf(&dav_roomname);
186 FreeStrBuf(&dav_uid);
188 /* We got it; a message is now arriving from the server. Read it in. */
191 found_content_type = 0;
192 strcpy(charset, "UTF-8");
193 strcpy(content_type, "text/plain");
195 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
196 linelen = strlen(buf);
198 /* Append it to the buffer */
199 if ((msglen + linelen + 3) > msgalloc) {
200 msgalloc = ( (msgalloc > 0) ? (msgalloc * 2) : 1024 );
201 msgtext = realloc(msgtext, msgalloc);
203 strcpy(&msgtext[msglen], buf);
205 strcpy(&msgtext[msglen], "\n");
208 /* Also learn some things about the message */
213 if (!strncasecmp(buf, "Date:", 5)) {
214 safestrncpy(date, &buf[5], sizeof date);
217 if (!strncasecmp(buf, "Content-type:", 13)) {
218 safestrncpy(content_type, &buf[13], sizeof content_type);
219 striplt(content_type);
220 ptr = bmstrcasestr(&buf[13], "charset=");
222 safestrncpy(charset, ptr+8, sizeof charset);
224 endptr = strchr(charset, ';');
225 if (endptr != NULL) strcpy(endptr, "");
227 endptr = strchr(content_type, ';');
228 if (endptr != NULL) strcpy(endptr, "");
234 /* Output headers common to single or multi part messages */
236 hprintf("HTTP/1.1 200 OK\r\n");
237 groupdav_common_headers();
238 hprintf("etag: \"%ld\"\r\n", dav_msgnum);
239 hprintf("Date: %s\r\n", date);
241 memset(&epdata, 0, sizeof(struct epdata));
242 safestrncpy(epdata.charset, charset, sizeof epdata.charset);
244 /* If we have a multipart message on our hands, and we are in a groupware room,
245 * strip it down to only the relevant part.
247 if (!strncasecmp(content_type, "multipart/", 10)) {
249 if ( (WCC->CurRoom.defview == VIEW_CALENDAR) || (WCC->CurRoom.defview == VIEW_TASKS) ) {
250 strcpy(epdata.desired_content_type_1, "text/calendar");
253 else if (WCC->CurRoom.defview == VIEW_ADDRESSBOOK) {
254 strcpy(epdata.desired_content_type_1, "text/vcard");
255 strcpy(epdata.desired_content_type_2, "text/x-vcard");
258 mime_parser(msgtext, &msgtext[msglen], extract_preferred, NULL, NULL, (void *)&epdata, 0);
261 /* If epdata.found_section is empty, we haven't output anything yet, so output the whole thing */
263 if (IsEmptyStr(epdata.found_section)) {
265 endptr = &msgtext[msglen];
267 hprintf("Content-type: %s; charset=%s\r\n", content_type, charset);
271 ptr = memreadline(ptr, buf, sizeof buf);
274 wc_printf("%s\r\n", buf);
276 else if ((buf[0] == 0) && (in_body == 0)) {
280 } while (ptr < endptr);