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