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