]> code.citadel.org Git - citadel.git/blob - webcit/groupdav_main.c
* Completed GroupDAV PUT. Untested.
[citadel.git] / webcit / groupdav_main.c
1 /*
2  * $Id$
3  *
4  * Entry point for GroupDAV functions
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 /*
30  * Output HTTP headers which are common to all requests.
31  */
32 void groupdav_common_headers(void) {
33         wprintf(
34                 "Server: %s / %s\n"
35                 "Connection: close\n",
36                 SERVER, serv_info.serv_software
37         );
38 }
39
40
41 /*
42  * Main entry point for GroupDAV requests
43  */
44 void groupdav_main(struct httprequest *req,
45                         char *dav_content_type,
46                         int dav_content_length,
47                         char *dav_content
48 ) {
49         struct httprequest *rptr;
50         char dav_method[SIZ];
51         char dav_pathname[SIZ];
52         char dav_ifmatch[SIZ];
53
54         strcpy(dav_method, "");
55         strcpy(dav_pathname, "");
56         strcpy(dav_ifmatch, "");
57
58         for (rptr=req; rptr!=NULL; rptr=rptr->next) {
59                 if (!strncasecmp(rptr->line, "Host: ", 6)) {
60                         safestrncpy(WC->http_host, &rptr->line[6],
61                                 sizeof WC->http_host);
62                 }
63                 if (!strncasecmp(rptr->line, "If-Match: ", 10)) {
64                         safestrncpy(dav_ifmatch, &rptr->line[10],
65                                 sizeof dav_ifmatch);
66                 }
67         }
68
69         if (!WC->logged_in) {
70                 wprintf("HTTP/1.1 401 Unauthorized\n");
71                 groupdav_common_headers();
72                 wprintf("WWW-Authenticate: Basic realm=\"%s\"\n",
73                         serv_info.serv_humannode);
74                 wprintf("Content-Type: text/plain\n");
75                 wprintf("\n");
76                 wprintf("GroupDAV sessions require HTTP authentication.\n");
77                 return;
78         }
79
80         extract_token(dav_method, req->line, 0, ' ');
81         extract_token(dav_pathname, req->line, 1, ' ');
82         unescape_input(dav_pathname);
83
84         /*
85          * If there's an If-Match: header, strip out the quotes if present, and
86          * then if all that's left is an asterisk, make it go away entirely.
87          */
88         if (strlen(dav_ifmatch) > 0) {
89                 if (dav_ifmatch[0] == '\"') {
90                         strcpy(dav_ifmatch, &dav_ifmatch[1]);
91                         if (strtok(dav_ifmatch, "\"") != NULL) {
92                                 strcpy(strtok(dav_ifmatch, "\""), "");
93                         }
94                 }
95                 if (!strcmp(dav_ifmatch, "*")) {
96                         strcpy(dav_ifmatch, "");
97                 }
98         }
99
100         /*
101          * The PROPFIND method is basically used to list all objects in a
102          * room, or to list all relevant rooms on the server.
103          */
104         if (!strcasecmp(dav_method, "PROPFIND")) {
105                 groupdav_propfind(dav_pathname);
106                 return;
107         }
108
109         /*
110          * The GET method is used for fetching individual items.
111          */
112         if (!strcasecmp(dav_method, "GET")) {
113                 groupdav_get(dav_pathname);
114                 return;
115         }
116
117         /*
118          * The PUT method is used to add or modify items.
119          */
120         if (!strcasecmp(dav_method, "PUT")) {
121                 groupdav_put(dav_pathname, dav_ifmatch,
122                                 dav_content_type, dav_content);
123                 return;
124         }
125
126         /*
127          * The DELETE method kills, maims, and destroys.
128          */
129         if (!strcasecmp(dav_method, "DELETE")) {
130                 groupdav_delete(dav_pathname, dav_ifmatch);
131                 return;
132         }
133
134         /*
135          * Couldn't find what we were looking for.  Die in a car fire.
136          */
137         wprintf("HTTP/1.1 501 Method not implemented\n");
138         groupdav_common_headers();
139         wprintf("Content-Type: text/plain\n"
140                 "\n"
141                 "GroupDAV method \"%s\" is not implemented.\n",
142                 dav_method
143         );
144 }