793f9651071c3728c9831ef59455354e08e322a2
[citadel.git] / webcit / groupdav_main.c
1 /*
2  * $Id$
3  */
4 /**
5  * \defgroup GroupdavMain Entry point for GroupDAV functions
6  *
7  */
8 /*@{*/
9 #include "webcit.h"
10 #include "webserver.h"
11 #include "groupdav.h"
12
13
14 /**
15  * \brief Output HTTP headers which are common to all requests.
16  *
17  * Please observe that we don't use the usual output_headers()
18  * and wDumpContent() functions in the GroupDAV subsystem, so we
19  * do our own header stuff here.
20  *
21  */
22 void groupdav_common_headers(void) {
23         wprintf(
24                 "Server: %s / %s\r\n"
25                 "Connection: close\r\n",
26                 SERVER, serv_info.serv_software
27         );
28 }
29
30
31
32 /**
33  * \brief string conversion function
34  * \param target output string
35  * \param source string to process
36  */
37 void euid_escapize(char *target, char *source) {
38         int i;
39         int target_length = 0;
40
41         strcpy(target, "");
42         for (i=0; i<strlen(source); ++i) {
43                 if (isalnum(source[i])) {
44                         target[target_length] = source[i];
45                         target[++target_length] = 0;
46                 }
47                 else if (source[i] == ' ') {
48                         target[target_length] = '_';
49                         target[++target_length] = 0;
50                 }
51                 else if (source[i] == '-') {
52                         target[target_length] = '-';
53                         target[++target_length] = 0;
54                 }
55                 else {
56                         sprintf(&target[target_length], "%%%02X", source[i]);
57                         target_length += 3;
58                 }
59         }
60 }
61
62 /**
63  * \brief string conversion function
64  * \param target output string
65  * \param source string to process
66  */
67 void euid_unescapize(char *target, char *source) {
68         int a, b;
69         char hex[3];
70         int target_length = 0;
71
72         strcpy(target, "");
73
74         for (a = 0; a < strlen(source); ++a) {
75                 if (source[a] == '%') {
76                         hex[0] = source[a + 1];
77                         hex[1] = source[a + 2];
78                         hex[2] = 0;
79                         b = 0;
80                         sscanf(hex, "%02x", &b);
81                         target[target_length] = b;
82                         target[++target_length] = 0;
83                         a += 2;
84                 }
85                 else if (source[a] == '_') {
86                         target[target_length] = ' ';
87                         target[++target_length] = 0;
88                 }
89                 else if (source[a] == '-') {
90                         target[target_length] = '-';
91                         target[++target_length] = 0;
92                 }
93                 else {
94                         target[target_length] = source[a];
95                         target[++target_length] = 0;
96                 }
97         }
98 }
99
100
101
102
103 /**
104  * \brief Main entry point for GroupDAV requests
105  * \param req Request header
106  * \param dav_content_type the kind of dav elemet to represent??
107  * \param dav_content_length the length of our response
108  * \param dav_content the actual content to give back
109  */
110 void groupdav_main(struct httprequest *req,
111                         char *dav_content_type,
112                         int dav_content_length,
113                         char *dav_content
114 ) {
115         struct httprequest *rptr;
116         char dav_method[256];
117         char dav_pathname[256];
118         char dav_ifmatch[256];
119         char buf[256];
120         char *ds;
121         int i;
122
123         strcpy(dav_method, "");
124         strcpy(dav_pathname, "");
125         strcpy(dav_ifmatch, "");
126
127         for (rptr=req; rptr!=NULL; rptr=rptr->next) {
128                 /* lprintf(9, "< %s\n", rptr->line); */
129                 if (!strncasecmp(rptr->line, "Host: ", 6)) {
130                         safestrncpy(WC->http_host, &rptr->line[6],
131                                 sizeof WC->http_host);
132                 }
133                 if (!strncasecmp(rptr->line, "If-Match: ", 10)) {
134                         safestrncpy(dav_ifmatch, &rptr->line[10],
135                                 sizeof dav_ifmatch);
136                 }
137         }
138
139         if (!WC->logged_in) {
140                 wprintf("HTTP/1.1 401 Unauthorized\r\n");
141                 groupdav_common_headers();
142                 wprintf("WWW-Authenticate: Basic realm=\"%s\"\r\n",
143                         serv_info.serv_humannode);
144                 wprintf("Content-Length: 0\r\n\r\n");
145                 return;
146         }
147
148         extract_token(dav_method, req->line, 0, ' ', sizeof dav_method);
149         extract_token(dav_pathname, req->line, 1, ' ', sizeof dav_pathname);
150         unescape_input(dav_pathname);
151
152         /**
153          * If the request does not begin with "/groupdav", prepend it.  If
154          * we happen to introduce a double-slash, that's ok; we'll strip it
155          * in the next step.
156          */
157         if (strncasecmp(dav_pathname, "/groupdav", 9)) {
158                 snprintf(buf, sizeof buf, "/groupdav/%s", dav_pathname);
159                 safestrncpy(dav_pathname, buf, sizeof dav_pathname);
160         }
161         
162         /** Remove any stray double-slashes in pathname */
163         while (ds=strstr(dav_pathname, "//"), ds != NULL) {
164                 strcpy(ds, ds+1);
165         }
166
167         /**
168          * If there's an If-Match: header, strip out the quotes if present, and
169          * then if all that's left is an asterisk, make it go away entirely.
170          */
171         if (strlen(dav_ifmatch) > 0) {
172                 striplt(dav_ifmatch);
173                 if (dav_ifmatch[0] == '\"') {
174                         strcpy(dav_ifmatch, &dav_ifmatch[1]);
175                         for (i=0; i<strlen(dav_ifmatch); ++i) {
176                                 if (dav_ifmatch[i] == '\"') {
177                                         dav_ifmatch[i] = 0;
178                                 }
179                         }
180                 }
181                 if (!strcmp(dav_ifmatch, "*")) {
182                         strcpy(dav_ifmatch, "");
183                 }
184         }
185
186         /**
187          * The OPTIONS method is not required by GroupDAV.  This is an
188          * experiment to determine what might be involved in supporting
189          * other variants of DAV in the future.
190          */
191         if (!strcasecmp(dav_method, "OPTIONS")) {
192                 groupdav_options(dav_pathname);
193                 return;
194         }
195
196         /**
197          * The PROPFIND method is basically used to list all objects in a
198          * room, or to list all relevant rooms on the server.
199          */
200         if (!strcasecmp(dav_method, "PROPFIND")) {
201                 groupdav_propfind(dav_pathname);
202                 return;
203         }
204
205         /**
206          * The GET method is used for fetching individual items.
207          */
208         if (!strcasecmp(dav_method, "GET")) {
209                 groupdav_get(dav_pathname);
210                 return;
211         }
212
213         /**
214          * The PUT method is used to add or modify items.
215          */
216         if (!strcasecmp(dav_method, "PUT")) {
217                 groupdav_put(dav_pathname, dav_ifmatch,
218                                 dav_content_type, dav_content);
219                 return;
220         }
221
222         /**
223          * The DELETE method kills, maims, and destroys.
224          */
225         if (!strcasecmp(dav_method, "DELETE")) {
226                 groupdav_delete(dav_pathname, dav_ifmatch);
227                 return;
228         }
229
230         /**
231          * Couldn't find what we were looking for.  Die in a car fire.
232          */
233         wprintf("HTTP/1.1 501 Method not implemented\r\n");
234         groupdav_common_headers();
235         wprintf("Content-Type: text/plain\r\n"
236                 "\r\n"
237                 "GroupDAV method \"%s\" is not implemented.\r\n",
238                 dav_method
239         );
240 }
241
242
243 /*@}*/