* When saving a Task, if the status is COMPLETED then also set PERCENT-COMPLETE:100...
[citadel.git] / webcit / groupdav_main.c
blob19b060c13eca53c369e064eac0a20f9c148a4343
1 /*
2 * $Id$
4 * Entry point for GroupDAV functions
6 */
8 #include "webcit.h"
9 #include "webserver.h"
10 #include "groupdav.h"
14 * Output HTTP headers which are common to all requests.
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.
21 void groupdav_common_headers(void) {
22 hprintf(
23 "Server: %s / %s\r\n"
24 "Connection: close\r\n",
25 PACKAGE_STRING, ChrPtr(serv_info.serv_software)
32 * string conversion function
34 void euid_escapize(char *target, const char *source) {
35 int i, len;
36 int target_length = 0;
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;
45 else {
46 sprintf(&target[target_length], "=%02X", (0xFF & source[i]));
47 target_length += 3;
53 * string conversion function
55 void euid_unescapize(char *target, const char *source) {
56 int a, b, len;
57 char hex[3];
58 int target_length = 0;
60 strcpy(target, "");
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 sscanf(hex, "%02x", &b);
70 target[target_length] = b;
71 target[++target_length] = 0;
72 a += 2;
74 else {
75 target[target_length] = source[a];
76 target[++target_length] = 0;
85 * Main entry point for GroupDAV requests
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 void *vLine;
96 char dav_ifmatch[256];
97 int dav_depth;
98 char *ds;
99 int i, len;
101 strcpy(dav_ifmatch, "");
102 dav_depth = 0;
104 if (IsEmptyStr(WC->http_host) &&
105 GetHash(HTTPHeaders, HKEY("HOST"), &vLine) &&
106 (vLine != NULL)) {
107 safestrncpy(WC->http_host, ChrPtr((StrBuf*)vLine),
108 sizeof WC->http_host);
110 if (GetHash(HTTPHeaders, HKEY("IF-MATCH"), &vLine) &&
111 (vLine != NULL)) {
112 safestrncpy(dav_ifmatch, ChrPtr((StrBuf*)vLine),
113 sizeof dav_ifmatch);
115 if (GetHash(HTTPHeaders, HKEY("DEPTH"), &vLine) &&
116 (vLine != NULL)) {
117 if (!strcasecmp(ChrPtr((StrBuf*)vLine), "infinity")) {
118 dav_depth = 32767;
120 else if (strcmp(ChrPtr((StrBuf*)vLine), "0") == 0) {
121 dav_depth = 0;
123 else if (strcmp(ChrPtr((StrBuf*)vLine), "1") == 0) {
124 dav_depth = 1;
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(serv_info.serv_humannode));
133 hprintf("Content-Length: 0\r\n");
134 end_burst();
135 return;
138 // extract_token(dav_method, req->line, 0, ' ', sizeof dav_method);
139 // extract_token(dav_pathname, req->line, 1, ' ', sizeof dav_pathname);
140 //// TODO unescape_input(dav_pathname);
142 /* If the request does not begin with "/groupdav", prepend it. If
143 * we happen to introduce a double-slash, that's ok; we'll strip it
144 * in the next step.
146 * (THIS IS DISABLED BECAUSE WE ARE NOW TRYING TO DO REAL DAV.)
148 if (strncasecmp(dav_pathname, "/groupdav", 9)) {
149 char buf[512];
150 snprintf(buf, sizeof buf, "/groupdav/%s", dav_pathname);
151 safestrncpy(dav_pathname, buf, sizeof dav_pathname);
156 /* Remove any stray double-slashes in pathname */
157 while (ds=strstr(ChrPtr(DavPathname), "//"), ds != NULL) {
158 strcpy(ds, ds+1);
162 * If there's an If-Match: header, strip out the quotes if present, and
163 * then if all that's left is an asterisk, make it go away entirely.
165 len = strlen(dav_ifmatch);
166 if (len > 0) {
167 stripltlen(dav_ifmatch, &len);
168 if (dav_ifmatch[0] == '\"') {
169 memmove (dav_ifmatch, &dav_ifmatch[1], len);
170 len --;
171 for (i=0; i<len; ++i) {
172 if (dav_ifmatch[i] == '\"') {
173 dav_ifmatch[i] = 0;
174 len = i - 1;
178 if (!strcmp(dav_ifmatch, "*")) {
179 strcpy(dav_ifmatch, "");
184 * The OPTIONS method is not required by GroupDAV. This is an
185 * experiment to determine what might be involved in supporting
186 * other variants of DAV in the future.
188 if (!strcasecmp(ChrPtr(DavMethod), "OPTIONS")) {
189 groupdav_options(DavPathname);
190 return;
194 * The PROPFIND method is basically used to list all objects in a
195 * room, or to list all relevant rooms on the server.
197 if (!strcasecmp(ChrPtr(DavMethod), "PROPFIND")) {
198 groupdav_propfind(DavPathname, dav_depth,
199 dav_content_type, dav_content,
200 Offset);
201 return;
205 * The GET method is used for fetching individual items.
207 if (!strcasecmp(ChrPtr(DavMethod), "GET")) {
208 groupdav_get(DavPathname);
209 return;
213 * The PUT method is used to add or modify items.
215 if (!strcasecmp(ChrPtr(DavMethod), "PUT")) {
216 groupdav_put(DavPathname, dav_ifmatch,
217 ChrPtr(dav_content_type), dav_content,
218 Offset);
219 return;
223 * The DELETE method kills, maims, and destroys.
225 if (!strcasecmp(ChrPtr(DavMethod), "DELETE")) {
226 groupdav_delete(DavPathname, dav_ifmatch);
227 return;
231 * Couldn't find what we were looking for. Die in a car fire.
233 hprintf("HTTP/1.1 501 Method not implemented\r\n");
234 groupdav_common_headers();
235 hprintf("Content-Type: text/plain\r\n");
236 wprintf("GroupDAV method \"%s\" is not implemented.\r\n",
237 ChrPtr(DavMethod));
238 end_burst();
243 * Output our host prefix for globally absolute URL's.
245 void groupdav_identify_host(void) {
246 if (!IsEmptyStr(WC->http_host)) {
247 wprintf("%s://%s",
248 (is_https ? "https" : "http"),
249 WC->http_host);