indent -kr -i8 -brf -bbb -fnc -l132 -nce on all of webcit-classic
[citadel.git] / webcit / dav_options.c
1
2 /*
3  * Handles DAV OPTIONS requests (experimental -- not required by GroupDAV)
4  *
5  * Copyright (c) 2005-2012 by the citadel.org team
6  *
7  * This program is open source software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License, version 3.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  */
15
16 #include "webcit.h"
17 #include "webserver.h"
18 #include "dav.h"
19
20 /*
21  * The pathname is always going to be /groupdav/room_name/msg_num
22  */
23 void dav_options(void) {
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("Content-Type: text/plain\r\n");
69                 begin_burst();
70                 wc_printf("There is no folder called \"%s\" on this server.\r\n", ChrPtr(dav_roomname)
71                     );
72                 end_burst();
73                 FreeStrBuf(&dav_roomname);
74                 FreeStrBuf(&dav_uid);
75                 return;
76         }
77
78         /* If dav_uid is non-empty, client is requesting an OPTIONS on
79          * a specific item in the room.
80          */
81         if (StrLength(dav_uid) != 0) {
82                 syslog(LOG_DEBUG, "\033[36mOPTIONS requested for specific item\033[0m");
83                 dav_msgnum = locate_message_by_uid(ChrPtr(dav_uid));
84                 if (dav_msgnum < 0) {
85                         hprintf("HTTP/1.1 404 not found\r\n");
86                         dav_common_headers();
87                         hprintf("Content-Type: text/plain\r\n");
88                         begin_burst();
89                         wc_printf("Object \"%s\" was not found in the \"%s\" folder.\r\n", ChrPtr(dav_uid), ChrPtr(dav_roomname)
90                             );
91                         FreeStrBuf(&dav_roomname);
92                         FreeStrBuf(&dav_uid);
93                         end_burst();
94                         return;
95                 }
96
97                 hprintf("HTTP/1.1 200 OK\r\n");
98                 dav_common_headers();
99                 hprintf("Date: %s\r\n", datestring);
100                 hprintf("DAV: 1\r\n");
101                 hprintf("Allow: OPTIONS, PROPFIND, GET, PUT, DELETE\r\n");
102
103                 begin_burst();
104                 end_burst();
105                 FreeStrBuf(&dav_roomname);
106                 FreeStrBuf(&dav_uid);
107                 return;
108         }
109
110         FreeStrBuf(&dav_roomname);
111         FreeStrBuf(&dav_uid);
112
113         /*
114          * We got to this point, which means that the client is requesting
115          * an OPTIONS on the room itself.
116          */
117         syslog(LOG_DEBUG, "\033[36mOPTIONS requested for room '%s' (%slogged in)\033[0m",
118                ChrPtr(WC->CurRoom.name), ((WC->logged_in) ? "" : "not ")
119             );
120         hprintf("HTTP/1.1 200 OK\r\n");
121         dav_common_headers();
122         hprintf("Date: %s\r\n", datestring);
123
124         /*
125          * Offer CalDAV (RFC 4791) if this is a calendar room
126          */
127         if ((WC->CurRoom.view == VIEW_CALENDAR) || (WC->CurRoom.view == VIEW_CALBRIEF)) {
128                 hprintf("DAV: 1, calendar-access\r\n");
129                 syslog(LOG_DEBUG, "\033[36mDAV: 1, calendar-access\033[0m");
130         }
131         else {
132                 hprintf("DAV: 1\r\n");
133                 syslog(LOG_DEBUG, "\033[36mDAV: 1\033[0m");
134         }
135
136         hprintf("Allow: OPTIONS, PROPFIND, GET, PUT, REPORT\r\n");
137         begin_burst();
138         end_burst();
139 }