* move some more vars from the session context to strbuf (the use of StrBufAppendTemp...
[citadel.git] / webcit / groupdav_options.c
1 /*
2  * $Id$
3  *
4  * Handles DAV OPTIONS requests (experimental -- not required by GroupDAV)
5  *
6  */
7
8 #include "webcit.h"
9 #include "webserver.h"
10 #include "groupdav.h"
11
12 /*
13  * The pathname is always going to be /groupdav/room_name/msg_num
14  */
15 void groupdav_options(StrBuf *dav_pathname) {
16         StrBuf *dav_roomname;
17         StrBuf *dav_uid;
18         long dav_msgnum = (-1);
19         char datestring[256];
20         time_t now;
21
22         now = time(NULL);
23         http_datestring(datestring, sizeof datestring, now);
24
25         dav_roomname = NewStrBuf();
26         dav_uid = NewStrBuf();
27         StrBufExtract_token(dav_roomname, dav_pathname, 2, '/');
28         StrBufExtract_token(dav_uid, dav_pathname, 3, '/');
29
30         /*
31          * If the room name is blank, the client is doing a top-level OPTIONS.
32          */
33         if (StrLength(dav_roomname) == 0) {
34                 hprintf("HTTP/1.1 200 OK\r\n");
35                 groupdav_common_headers();
36                 hprintf("Date: %s\r\n", datestring);
37                 hprintf("DAV: 1\r\n");
38                 hprintf("Allow: OPTIONS, PROPFIND\r\n");
39                 hprintf("\r\n");
40                 begin_burst();
41                 end_burst();
42                 FreeStrBuf(&dav_roomname);
43                 FreeStrBuf(&dav_uid);
44                 return;
45         }
46
47         /* Go to the correct room. */
48         if (strcasecmp(ChrPtr(WC->wc_roomname), ChrPtr(dav_roomname))) {
49                 gotoroom(dav_roomname);
50         }
51
52         if (strcasecmp(ChrPtr(WC->wc_roomname), ChrPtr(dav_roomname))) {
53                 hprintf("HTTP/1.1 404 not found\r\n");
54                 groupdav_common_headers();
55                 hprintf("Date: %s\r\n", datestring);
56                 hprintf(
57                         "Content-Type: text/plain\r\n"
58                         "\r\n"
59                         "There is no folder called \"%s\" on this server.\r\n",
60                         ChrPtr(dav_roomname)
61                 );
62                 begin_burst();
63                 end_burst();
64                 FreeStrBuf(&dav_roomname);
65                 FreeStrBuf(&dav_uid);
66                 return;
67         }
68
69         /* If dav_uid is non-empty, client is requesting an OPTIONS on
70          * a specific item in the room.
71          */
72         if (StrLength(dav_uid) != 0) {
73
74                 dav_msgnum = locate_message_by_uid(ChrPtr(dav_uid));
75                 if (dav_msgnum < 0) {
76                         hprintf("HTTP/1.1 404 not found\r\n");
77                         groupdav_common_headers();
78                         hprintf(
79                                 "Content-Type: text/plain\r\n"
80                                 "\r\n"
81                                 "Object \"%s\" was not found in the \"%s\" folder.\r\n",
82                                 ChrPtr(dav_uid),
83                                 ChrPtr(dav_roomname)
84                         );
85                         FreeStrBuf(&dav_roomname);
86                         FreeStrBuf(&dav_uid);
87                         begin_burst();end_burst();return;
88                 }
89
90                 hprintf("HTTP/1.1 200 OK\r\n");
91                 groupdav_common_headers();
92                 hprintf("Date: %s\r\n", datestring);
93                 hprintf("DAV: 1\r\n");
94                 hprintf("Allow: OPTIONS, PROPFIND, GET, PUT, DELETE\r\n");
95                 hprintf("\r\n");
96                 begin_burst();
97                 end_burst();
98                 FreeStrBuf(&dav_roomname);
99                 FreeStrBuf(&dav_uid);
100                 return;
101         }
102
103         FreeStrBuf(&dav_roomname);
104         FreeStrBuf(&dav_uid);
105
106         /*
107          * We got to this point, which means that the client is requesting
108          * an OPTIONS on the room itself.
109          */
110         hprintf("HTTP/1.1 200 OK\r\n");
111         groupdav_common_headers();
112         hprintf("Date: %s\r\n", datestring);
113         hprintf("DAV: 1\r\n");
114         hprintf("Allow: OPTIONS, PROPFIND, GET, PUT\r\n");
115         hprintf("\r\n");
116         begin_burst();
117         end_burst();
118 }