]> code.citadel.org Git - citadel.git/blob - webcit/groupdav_get.c
More changes to GroupDAV GET to prepare for
[citadel.git] / webcit / groupdav_get.c
1 /*
2  * $Id$
3  *
4  * Handles GroupDAV GET requests.
5  *
6  */
7
8 #include "webcit.h"
9 #include "webserver.h"
10 #include "groupdav.h"
11
12
13 /*
14  * Fetch the entire contents of the room as one big ics file.
15  * This is for "webcal://" type access.
16  */     
17 void groupdav_get_big_ics(void) {
18         char buf[1024];
19
20         serv_puts("ICAL getics");
21         serv_getln(buf, sizeof buf);
22         if (buf[0] != '1') {
23                 wprintf("HTTP/1.1 404 not found\r\n");
24                 groupdav_common_headers();
25                 wprintf(
26                         "Content-Type: text/plain\r\n"
27                         "\r\n"
28                         "%s\r\n",
29                         &buf[4]
30                 );
31                 return;
32         }
33
34         wprintf("HTTP/1.1 200 OK\r\n");
35         groupdav_common_headers();
36         wprintf("Content-type: text/calendar; charset=UTF-8\r\n");
37         begin_burst();
38         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
39                 wprintf("%s\r\n", buf);
40         }
41         end_burst();
42 }
43
44
45 /*
46  * The pathname is always going to take one of two formats:
47  * /groupdav/room_name/euid     (GroupDAV)
48  * /groupdav/room_name          (webcal)
49  */
50 void groupdav_get(char *dav_pathname) {
51         char dav_roomname[1024];
52         char dav_uid[1024];
53         long dav_msgnum = (-1);
54         char buf[1024];
55         int in_body = 0;
56         int found_content_type = 0;
57         char *ptr;
58         char *endptr;
59         char *msgtext = NULL;
60         size_t msglen = 0;
61         size_t msgalloc = 0;
62         int linelen;
63         char content_type[128];
64         char charset[128];
65         char date[128];
66
67         if (num_tokens(dav_pathname, '/') < 3) {
68                 wprintf("HTTP/1.1 404 not found\r\n");
69                 groupdav_common_headers();
70                 wprintf(
71                         "Content-Type: text/plain\r\n"
72                         "\r\n"
73                         "The object you requested was not found.\r\n"
74                 );
75                 return;
76         }
77
78         extract_token(dav_roomname, dav_pathname, 2, '/', sizeof dav_roomname);
79         extract_token(dav_uid, dav_pathname, 3, '/', sizeof dav_uid);
80         if ((!strcasecmp(dav_uid, "ics")) || (!strcasecmp(dav_uid, "calendar.ics"))) {
81                 strcpy(dav_uid, "");
82         }
83
84         /* Go to the correct room. */
85         if (strcasecmp(WC->wc_roomname, dav_roomname)) {
86                 gotoroom(dav_roomname);
87         }
88         if (strcasecmp(WC->wc_roomname, dav_roomname)) {
89                 wprintf("HTTP/1.1 404 not found\r\n");
90                 groupdav_common_headers();
91                 wprintf(
92                         "Content-Type: text/plain\r\n"
93                         "\r\n"
94                         "There is no folder called \"%s\" on this server.\r\n",
95                         dav_roomname
96                 );
97                 return;
98         }
99
100         /** GET on the collection itself returns an ICS of the entire collection.
101          */
102         if (!strcasecmp(dav_uid, "")) {
103                 groupdav_get_big_ics();
104                 return;
105         }
106
107         dav_msgnum = locate_message_by_uid(dav_uid);
108         serv_printf("MSG2 %ld", dav_msgnum);
109         serv_getln(buf, sizeof buf);
110         if (buf[0] != '1') {
111                 wprintf("HTTP/1.1 404 not found\r\n");
112                 groupdav_common_headers();
113                 wprintf(
114                         "Content-Type: text/plain\r\n"
115                         "\r\n"
116                         "Object \"%s\" was not found in the \"%s\" folder.\r\n",
117                         dav_uid,
118                         dav_roomname
119                 );
120                 return;
121         }
122
123         /* We got it; a message is now arriving from the server.  Read it in. */
124
125         in_body = 0;
126         found_content_type = 0;
127         strcpy(charset, "UTF-8");
128         strcpy(content_type, "text/plain");
129         strcpy(date, "");
130         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
131                 linelen = strlen(buf);
132
133                 /* Append it to the buffer */
134                 if ((msglen + linelen + 3) > msgalloc) {
135                         msgalloc = ( (msgalloc > 0) ? (msgalloc * 2) : 1024 );
136                         msgtext = realloc(msgtext, msgalloc);
137                 }
138                 strcpy(&msgtext[msglen], buf);
139                 msglen += linelen;
140                 strcpy(&msgtext[msglen], "\n");
141                 msglen += 1;
142
143                 /* Also learn some things about the message */
144                 if (linelen == 0) {
145                         in_body = 1;
146                 }
147                 if (!in_body) {
148                         if (!strncasecmp(buf, "Date:", 5)) {
149                                 safestrncpy(date, &buf[5], sizeof date);
150                                 striplt(date);
151                         }
152                         if (!strncasecmp(buf, "Content-type:", 13)) {
153                                 safestrncpy(content_type, &buf[13], sizeof content_type);
154                                 striplt(content_type);
155                                 ptr = bmstrcasestr(&buf[13], "charset=");
156                                 if (ptr) {
157                                         safestrncpy(charset, ptr+8, sizeof charset);
158                                         striplt(charset);
159                                         endptr = strchr(charset, ';');
160                                         if (endptr != NULL) strcpy(endptr, "");
161                                 }
162                                 endptr = strchr(content_type, ';');
163                                 if (endptr != NULL) strcpy(endptr, "");
164                         }
165                 }
166         }
167         msgtext[msglen] = 0;
168         lprintf(9, "CONTENT TYPE: '%s'\n", content_type);
169         lprintf(9, "CHARSET: '%s'\n", charset);
170         lprintf(9, "DATE: '%s'\n", date);
171
172         /* Now do something with it.  FIXME boil it down to only the part we need */
173
174         ptr = msgtext;
175         endptr = &msgtext[msglen];
176
177         wprintf("HTTP/1.1 200 OK\r\n");
178         groupdav_common_headers();
179         wprintf("etag: \"%ld\"\r\n", dav_msgnum);
180         wprintf("Content-type: %s; charset=%s\r\n", content_type, charset);
181         wprintf("Date: %s\r\n", date);
182
183         in_body = 0;
184         do {
185                 ptr = memreadline(ptr, buf, sizeof buf);
186
187                 if (in_body) {
188                         wprintf("%s\r\n", buf);
189                 }
190                 else if ((strlen(buf) == 0) && (in_body == 0)) {
191                         in_body = 1;
192                         begin_burst();
193                 }
194         } while (ptr < endptr);
195
196         end_burst();
197
198         free(msgtext);
199 }