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