6e7323c50f10a0c8ffe526c8b30cb7fb54503130
[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 <ctype.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <stdio.h>
12 #include <fcntl.h>
13 #include <signal.h>
14 #include <sys/types.h>
15 #include <sys/wait.h>
16 #include <sys/socket.h>
17 #include <limits.h>
18 #include <string.h>
19 #include <pwd.h>
20 #include <errno.h>
21 #include <stdarg.h>
22 #include <time.h>
23 #include <pthread.h>
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(char *dav_pathname) {
32         char dav_roomname[256];
33         char dav_uid[256];
34         long dav_msgnum = (-1);
35         char datestring[256];
36         time_t now;
37
38         now = time(NULL);
39         http_datestring(datestring, sizeof datestring, now);
40
41         extract_token(dav_roomname, dav_pathname, 2, '/', sizeof dav_roomname);
42         extract_token(dav_uid, dav_pathname, 3, '/', sizeof dav_uid);
43
44         /*
45          * If the room name is blank, the client is doing a top-level OPTIONS.
46          */
47         if (strlen(dav_roomname) == 0) {
48                 wprintf("HTTP/1.1 200 OK\r\n");
49                 groupdav_common_headers();
50                 wprintf("Date: %s\r\n", datestring);
51                 wprintf("Allow: OPTIONS, PROPFIND\r\n");
52                 wprintf("\r\n");
53                 return;
54         }
55
56         /* Go to the correct room. */
57         if (strcasecmp(WC->wc_roomname, dav_roomname)) {
58                 gotoroom(dav_roomname);
59         }
60         if (strcasecmp(WC->wc_roomname, dav_roomname)) {
61                 wprintf("HTTP/1.1 404 not found\r\n");
62                 groupdav_common_headers();
63                 wprintf("Date: %s\r\n", datestring);
64                 wprintf(
65                         "Content-Type: text/plain\r\n"
66                         "\r\n"
67                         "There is no folder called \"%s\" on this server.\r\n",
68                         dav_roomname
69                 );
70                 return;
71         }
72
73         /* If dav_uid is non-empty, client is requesting an OPTIONS on
74          * a specific item in the room.
75          */
76         if (strlen(dav_uid) > 0) {
77
78                 dav_msgnum = locate_message_by_uid(dav_uid);
79                 if (dav_msgnum < 0) {
80                         wprintf("HTTP/1.1 404 not found\r\n");
81                         groupdav_common_headers();
82                         wprintf(
83                                 "Content-Type: text/plain\r\n"
84                                 "\r\n"
85                                 "Object \"%s\" was not found in the \"%s\" folder.\r\n",
86                                 dav_uid,
87                                 dav_roomname
88                         );
89                         return;
90                 }
91
92                 wprintf("HTTP/1.1 200 OK\r\n");
93                 groupdav_common_headers();
94                 wprintf("Date: %s\r\n", datestring);
95                 wprintf("Allow: OPTIONS, PROPFIND, GET, PUT, DELETE\r\n");
96                 wprintf("\r\n");
97                 return;
98         }
99
100
101         /*
102          * We got to this point, which means that the client is requesting
103          * an OPTIONS on the room itself.
104          */
105         wprintf("HTTP/1.1 200 OK\r\n");
106         groupdav_common_headers();
107         wprintf("Date: %s\r\n", datestring);
108         wprintf("Allow: OPTIONS, PROPFIND, GET, PUT\r\n");
109         wprintf("\r\n");
110 }