Remove APPDIR support in database_cleanup.sh
[citadel] / webcit / dav_options.c
1 /*
2  * Handles DAV OPTIONS requests (experimental -- not required by GroupDAV)
3  *
4  * Copyright (c) 2005-2012 by the citadel.org team
5  *
6  * This program is open source software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License, version 3.
8  *
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.
13  */
14
15 #include "webcit.h"
16 #include "webserver.h"
17 #include "dav.h"
18
19 /*
20  * The pathname is always going to be /groupdav/room_name/msg_num
21  */
22 void dav_options(void)
23 {
24         wcsession *WCC = WC;
25         StrBuf *dav_roomname;
26         StrBuf *dav_uid;
27         long dav_msgnum = (-1);
28         char datestring[256];
29         time_t now;
30
31         now = time(NULL);
32         http_datestring(datestring, sizeof datestring, now);
33
34         dav_roomname = NewStrBuf();
35         dav_uid = NewStrBuf();
36         StrBufExtract_token(dav_roomname, WCC->Hdr->HR.ReqLine, 0, '/');
37         StrBufExtract_token(dav_uid, WCC->Hdr->HR.ReqLine, 1, '/');
38
39         syslog(LOG_DEBUG, "\033[35m%s (logged_in=%d)\033[0m", ChrPtr(WCC->Hdr->HR.ReqLine), WC->logged_in);
40         /*
41          * If the room name is blank, the client is doing an OPTIONS on the root.
42          */
43         if (StrLength(dav_roomname) == 0) {
44                 syslog(LOG_DEBUG, "\033[36mOPTIONS requested for root\033[0m");
45                 hprintf("HTTP/1.1 200 OK\r\n");
46                 dav_common_headers();
47                 hprintf("Date: %s\r\n", datestring);
48                 hprintf("DAV: 1\r\n");
49                 hprintf("Allow: OPTIONS, PROPFIND\r\n");
50                 hprintf("\r\n");
51                 begin_burst();
52                 end_burst();
53                 FreeStrBuf(&dav_roomname);
54                 FreeStrBuf(&dav_uid);
55                 return;
56         }
57
58         /* Go to the correct room. */
59         if (strcasecmp(ChrPtr(WC->CurRoom.name), ChrPtr(dav_roomname))) {
60                 gotoroom(dav_roomname);
61         }
62
63         if (strcasecmp(ChrPtr(WC->CurRoom.name), ChrPtr(dav_roomname))) {
64                 syslog(LOG_DEBUG, "\033[36mOPTIONS requested for invalid item\033[0m");
65                 hprintf("HTTP/1.1 404 not found\r\n");
66                 dav_common_headers();
67                 hprintf("Date: %s\r\n", datestring);
68                 hprintf(
69                         "Content-Type: text/plain\r\n");
70                 begin_burst();
71                 wc_printf(
72                         "There is no folder called \"%s\" on this server.\r\n",
73                         ChrPtr(dav_roomname)
74                 );
75                 end_burst();
76                 FreeStrBuf(&dav_roomname);
77                 FreeStrBuf(&dav_uid);
78                 return;
79         }
80
81         /* If dav_uid is non-empty, client is requesting an OPTIONS on
82          * a specific item in the room.
83          */
84         if (StrLength(dav_uid) != 0) {
85                 syslog(LOG_DEBUG, "\033[36mOPTIONS requested for specific item\033[0m");
86                 dav_msgnum = locate_message_by_uid(ChrPtr(dav_uid));
87                 if (dav_msgnum < 0) {
88                         hprintf("HTTP/1.1 404 not found\r\n");
89                         dav_common_headers();
90                         hprintf("Content-Type: text/plain\r\n");
91                         begin_burst();
92                         wc_printf(
93                                 "Object \"%s\" was not found in the \"%s\" folder.\r\n",
94                                 ChrPtr(dav_uid),
95                                 ChrPtr(dav_roomname)
96                         );
97                         FreeStrBuf(&dav_roomname);
98                         FreeStrBuf(&dav_uid);
99                         end_burst();return;
100                 }
101
102                 hprintf("HTTP/1.1 200 OK\r\n");
103                 dav_common_headers();
104                 hprintf("Date: %s\r\n", datestring);
105                 hprintf("DAV: 1\r\n");
106                 hprintf("Allow: OPTIONS, PROPFIND, GET, PUT, DELETE\r\n");
107                 
108                 begin_burst();
109                 end_burst();
110                 FreeStrBuf(&dav_roomname);
111                 FreeStrBuf(&dav_uid);
112                 return;
113         }
114
115         FreeStrBuf(&dav_roomname);
116         FreeStrBuf(&dav_uid);
117
118         /*
119          * We got to this point, which means that the client is requesting
120          * an OPTIONS on the room itself.
121          */
122         syslog(LOG_DEBUG, "\033[36mOPTIONS requested for room '%s' (%slogged in)\033[0m",
123                 ChrPtr(WC->CurRoom.name),
124                 ((WC->logged_in) ? "" : "not ")
125         );
126         hprintf("HTTP/1.1 200 OK\r\n");
127         dav_common_headers();
128         hprintf("Date: %s\r\n", datestring);
129
130         /*
131          * Offer CalDAV (RFC 4791) if this is a calendar room
132          */
133         if ( (WC->CurRoom.view == VIEW_CALENDAR) || (WC->CurRoom.view == VIEW_CALBRIEF) ) {
134                 hprintf("DAV: 1, calendar-access\r\n");
135                 syslog(LOG_DEBUG, "\033[36mDAV: 1, calendar-access\033[0m");
136         }
137         else {
138                 hprintf("DAV: 1\r\n");
139                 syslog(LOG_DEBUG, "\033[36mDAV: 1\033[0m");
140         }
141
142         hprintf("Allow: OPTIONS, PROPFIND, GET, PUT, REPORT\r\n");
143         begin_burst();
144         end_burst();
145 }