Renamed all groupdav_() functions to dav_()
[citadel.git] / webcit / dav_options.c
1 /*
2  * Handles DAV OPTIONS requests (experimental -- not required by GroupDAV)
3  *
4  * Copyright (c) 2005-2010 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 as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "webcit.h"
22 #include "webserver.h"
23 #include "dav.h"
24
25 /*
26  * The pathname is always going to be /groupdav/room_name/msg_num
27  */
28 void dav_options(void)
29 {
30         wcsession *WCC = WC;
31         StrBuf *dav_roomname;
32         StrBuf *dav_uid;
33         long dav_msgnum = (-1);
34         char datestring[256];
35         time_t now;
36
37         now = time(NULL);
38         http_datestring(datestring, sizeof datestring, now);
39
40         dav_roomname = NewStrBuf();
41         dav_uid = NewStrBuf();
42         StrBufExtract_token(dav_roomname, WCC->Hdr->HR.ReqLine, 0, '/');
43         StrBufExtract_token(dav_uid, WCC->Hdr->HR.ReqLine, 1, '/');
44
45         /*
46          * If the room name is blank, the client is doing a top-level OPTIONS.
47          */
48         if (StrLength(dav_roomname) == 0) {
49                 hprintf("HTTP/1.1 200 OK\r\n");
50                 dav_common_headers();
51                 hprintf("Date: %s\r\n", datestring);
52                 hprintf("DAV: 1\r\n");
53                 hprintf("Allow: OPTIONS, PROPFIND\r\n");
54                 hprintf("\r\n");
55                 begin_burst();
56                 end_burst();
57                 FreeStrBuf(&dav_roomname);
58                 FreeStrBuf(&dav_uid);
59                 return;
60         }
61
62         /* Go to the correct room. */
63         if (strcasecmp(ChrPtr(WC->CurRoom.name), ChrPtr(dav_roomname))) {
64                 gotoroom(dav_roomname);
65         }
66
67         if (strcasecmp(ChrPtr(WC->CurRoom.name), ChrPtr(dav_roomname))) {
68                 hprintf("HTTP/1.1 404 not found\r\n");
69                 dav_common_headers();
70                 hprintf("Date: %s\r\n", datestring);
71                 hprintf(
72                         "Content-Type: text/plain\r\n");
73                 begin_burst();
74                 wc_printf(
75                         "There is no folder called \"%s\" on this server.\r\n",
76                         ChrPtr(dav_roomname)
77                 );
78                 end_burst();
79                 FreeStrBuf(&dav_roomname);
80                 FreeStrBuf(&dav_uid);
81                 return;
82         }
83
84         /* If dav_uid is non-empty, client is requesting an OPTIONS on
85          * a specific item in the room.
86          */
87         if (StrLength(dav_uid) != 0) {
88
89                 dav_msgnum = locate_message_by_uid(ChrPtr(dav_uid));
90                 if (dav_msgnum < 0) {
91                         hprintf("HTTP/1.1 404 not found\r\n");
92                         dav_common_headers();
93                         hprintf("Content-Type: text/plain\r\n");
94                         begin_burst();
95                         wc_printf(
96                                 "Object \"%s\" was not found in the \"%s\" folder.\r\n",
97                                 ChrPtr(dav_uid),
98                                 ChrPtr(dav_roomname)
99                         );
100                         FreeStrBuf(&dav_roomname);
101                         FreeStrBuf(&dav_uid);
102                         end_burst();return;
103                 }
104
105                 hprintf("HTTP/1.1 200 OK\r\n");
106                 dav_common_headers();
107                 hprintf("Date: %s\r\n", datestring);
108                 hprintf("DAV: 1\r\n");
109                 hprintf("Allow: OPTIONS, PROPFIND, GET, PUT, DELETE\r\n");
110                 
111                 begin_burst();
112                 end_burst();
113                 FreeStrBuf(&dav_roomname);
114                 FreeStrBuf(&dav_uid);
115                 return;
116         }
117
118         FreeStrBuf(&dav_roomname);
119         FreeStrBuf(&dav_uid);
120
121         /*
122          * We got to this point, which means that the client is requesting
123          * an OPTIONS on the room itself.
124          */
125         hprintf("HTTP/1.1 200 OK\r\n");
126         dav_common_headers();
127         hprintf("Date: %s\r\n", datestring);
128         hprintf("DAV: 1\r\n");
129         hprintf("Allow: OPTIONS, PROPFIND, GET, PUT\r\n");
130         begin_burst();
131         wc_printf("\r\n");
132         end_burst();
133 }