]> code.citadel.org Git - citadel.git/blob - webcit/groupdav_get.c
Altered the URL format for GET an entire calendar.
[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
58         if (num_tokens(dav_pathname, '/') < 3) {
59                 wprintf("HTTP/1.1 404 not found\r\n");
60                 groupdav_common_headers();
61                 wprintf(
62                         "Content-Type: text/plain\r\n"
63                         "\r\n"
64                         "The object you requested was not found.\r\n"
65                 );
66                 return;
67         }
68
69         extract_token(dav_roomname, dav_pathname, 2, '/', sizeof dav_roomname);
70         extract_token(dav_uid, dav_pathname, 3, '/', sizeof dav_uid);
71
72         /* Go to the correct room. */
73         if (strcasecmp(WC->wc_roomname, dav_roomname)) {
74                 gotoroom(dav_roomname);
75         }
76         if (strcasecmp(WC->wc_roomname, dav_roomname)) {
77                 wprintf("HTTP/1.1 404 not found\r\n");
78                 groupdav_common_headers();
79                 wprintf(
80                         "Content-Type: text/plain\r\n"
81                         "\r\n"
82                         "There is no folder called \"%s\" on this server.\r\n",
83                         dav_roomname
84                 );
85                 return;
86         }
87
88         /** GET on the collection itself returns an ICS of the entire collection.
89          */
90         if (!strcasecmp(dav_uid, "")) {
91                 groupdav_get_big_ics();
92                 return;
93         }
94
95         dav_msgnum = locate_message_by_uid(dav_uid);
96         serv_printf("MSG2 %ld", dav_msgnum);
97         serv_getln(buf, sizeof buf);
98         if (buf[0] != '1') {
99                 wprintf("HTTP/1.1 404 not found\r\n");
100                 groupdav_common_headers();
101                 wprintf(
102                         "Content-Type: text/plain\r\n"
103                         "\r\n"
104                         "Object \"%s\" was not found in the \"%s\" folder.\r\n",
105                         dav_uid,
106                         dav_roomname
107                 );
108                 return;
109         }
110
111         wprintf("HTTP/1.1 200 OK\r\n");
112         groupdav_common_headers();
113         wprintf("etag: \"%ld\"\r\n", dav_msgnum);
114         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
115                 if (in_body) {
116                         wprintf("%s\r\n", buf);
117                 }
118                 else if (!strncasecmp(buf, "Date: ", 6)) {
119                         wprintf("%s\r\n", buf);
120                 }
121                 else if (!strncasecmp(buf, "Content-type: ", 14)) {
122                         wprintf("%s\r\n", buf);
123                         found_content_type = 1;
124                 }
125                 else if ((strlen(buf) == 0) && (in_body == 0)) {
126                         if (!found_content_type) {
127                                 wprintf("Content-type: text/plain\r\n");
128                         }
129                         in_body = 1;
130                         begin_burst();
131                 }
132         }
133         end_burst();
134 }