* BIG rewrite of header handling and slimming of context_loop and session_loop; shuff...
[citadel.git] / webcit / groupdav_main.c
index b761875ef4fad701ed15e5ebc3af28161a93e591..2e33e26a539fb63ce9995af0fbee0112ebdb0693 100644 (file)
@@ -22,7 +22,7 @@ void groupdav_common_headers(void) {
        hprintf(
                "Server: %s / %s\r\n"
                "Connection: close\r\n",
-               PACKAGE_STRING, serv_info.serv_software
+               PACKAGE_STRING, ChrPtr(WC->serv_info->serv_software)
        );
 }
 
@@ -31,7 +31,7 @@ void groupdav_common_headers(void) {
 /*
  * string conversion function
  */
-void euid_escapize(char *target, char *source) {
+void euid_escapize(char *target, const char *source) {
        int i, len;
        int target_length = 0;
 
@@ -52,7 +52,7 @@ void euid_escapize(char *target, char *source) {
 /*
  * string conversion function
  */
-void euid_unescapize(char *target, char *source) {
+void euid_unescapize(char *target, const char *source) {
        int a, b, len;
        char hex[3];
        int target_length = 0;
@@ -66,7 +66,7 @@ void euid_unescapize(char *target, char *source) {
                        hex[1] = source[a + 2];
                        hex[2] = 0;
                        b = 0;
-                       sscanf(hex, "%02x", &b);
+                       b = decode_hex(hex);
                        target[target_length] = b;
                        target[++target_length] = 0;
                        a += 2;
@@ -86,12 +86,12 @@ void euid_unescapize(char *target, char *source) {
  */
 void groupdav_main(HashList *HTTPHeaders,
                   StrBuf *DavPathname,
-                  StrBuf *DavMethod,
                   StrBuf *dav_content_type,
                   int dav_content_length,
                   StrBuf *dav_content,
                   int Offset
 ) {
+       wcsession *WCC = WC;
        void *vLine;
        char dav_ifmatch[256];
        int dav_depth;
@@ -101,11 +101,10 @@ void groupdav_main(HashList *HTTPHeaders,
        strcpy(dav_ifmatch, "");
        dav_depth = 0;
 
-       if (IsEmptyStr(WC->http_host) &&
+       if ((StrLength(WCC->Hdr->http_host) == 0) &&
            GetHash(HTTPHeaders, HKEY("HOST"), &vLine) && 
            (vLine != NULL)) {
-               safestrncpy(WC->http_host, ChrPtr((StrBuf*)vLine),
-                           sizeof WC->http_host);
+               WCC->Hdr->http_host = (StrBuf*)vLine;
        }
        if (GetHash(HTTPHeaders, HKEY("IF-MATCH"), &vLine) && 
            (vLine != NULL)) {
@@ -129,30 +128,14 @@ void groupdav_main(HashList *HTTPHeaders,
                hprintf("HTTP/1.1 401 Unauthorized\r\n");
                groupdav_common_headers();
                hprintf("WWW-Authenticate: Basic realm=\"%s\"\r\n",
-                       serv_info.serv_humannode);
+                       ChrPtr(WCC->serv_info->serv_humannode));
                hprintf("Content-Length: 0\r\n");
                end_burst();
                return;
        }
 
-//     extract_token(dav_method, req->line, 0, ' ', sizeof dav_method);
-//     extract_token(dav_pathname, req->line, 1, ' ', sizeof dav_pathname);
-       //// TODO unescape_input(dav_pathname);
-
-       /* If the request does not begin with "/groupdav", prepend it.  If
-        * we happen to introduce a double-slash, that's ok; we'll strip it
-        * in the next step.
-        * 
-        * (THIS IS DISABLED BECAUSE WE ARE NOW TRYING TO DO REAL DAV.)
-        *
-       if (strncasecmp(dav_pathname, "/groupdav", 9)) {
-               char buf[512];
-               snprintf(buf, sizeof buf, "/groupdav/%s", dav_pathname);
-               safestrncpy(dav_pathname, buf, sizeof dav_pathname);
-       }
-        *
-        */
-       
+       StrBufUnescape(DavPathname, 0);
+
        /* Remove any stray double-slashes in pathname */
        while (ds=strstr(ChrPtr(DavPathname), "//"), ds != NULL) {
                strcpy(ds, ds+1);
@@ -180,62 +163,62 @@ void groupdav_main(HashList *HTTPHeaders,
                }
        }
 
+       switch (WCC->Hdr->eReqType)
+       {
        /*
         * The OPTIONS method is not required by GroupDAV.  This is an
         * experiment to determine what might be involved in supporting
         * other variants of DAV in the future.
         */
-       if (!strcasecmp(ChrPtr(DavMethod), "OPTIONS")) {
-               groupdav_options(ChrPtr(DavPathname));
-               return;
-       }
+       case eOPTIONS:
+               groupdav_options(DavPathname);
+               break;
+
 
        /*
         * The PROPFIND method is basically used to list all objects in a
         * room, or to list all relevant rooms on the server.
         */
-       if (!strcasecmp(ChrPtr(DavMethod), "PROPFIND")) {
-               groupdav_propfind(ChrPtr(DavPathname), dav_depth,
+       case ePROPFIND:
+               groupdav_propfind(DavPathname, dav_depth,
                                  dav_content_type, dav_content, 
                                  Offset);
-               return;
-       }
+               break;
 
        /*
         * The GET method is used for fetching individual items.
         */
-       if (!strcasecmp(ChrPtr(DavMethod), "GET")) {
-               groupdav_get(ChrPtr(DavPathname));
-               return;
-       }
-
+       case eGET:
+               groupdav_get(DavPathname);
+               break;
+       
        /*
         * The PUT method is used to add or modify items.
         */
-       if (!strcasecmp(ChrPtr(DavMethod), "PUT")) {
-               groupdav_put(ChrPtr(DavPathname), dav_ifmatch,
+       case ePUT:
+               groupdav_put(DavPathname, dav_ifmatch,
                             ChrPtr(dav_content_type), dav_content, 
                             Offset);
-               return;
-       }
-
+               break;
+       
        /*
         * The DELETE method kills, maims, and destroys.
         */
-       if (!strcasecmp(ChrPtr(DavMethod), "DELETE")) {
+       case eDELETE:
                groupdav_delete(DavPathname, dav_ifmatch);
-               return;
-       }
+               break;
+       default:
 
        /*
         * Couldn't find what we were looking for.  Die in a car fire.
         */
-       hprintf("HTTP/1.1 501 Method not implemented\r\n");
-       groupdav_common_headers();
-       hprintf("Content-Type: text/plain\r\n");
-       wprintf("GroupDAV method \"%s\" is not implemented.\r\n",
-               ChrPtr(DavMethod));
-       end_burst();
+               hprintf("HTTP/1.1 501 Method not implemented\r\n");
+               groupdav_common_headers();
+               hprintf("Content-Type: text/plain\r\n");
+               wprintf("GroupDAV method \"%s\" is not implemented.\r\n",
+                       ReqStrs[WCC->Hdr->eReqType]);
+               end_burst();
+       }
 }
 
 
@@ -243,9 +226,21 @@ void groupdav_main(HashList *HTTPHeaders,
  * Output our host prefix for globally absolute URL's.
  */  
 void groupdav_identify_host(void) {
-       if (!IsEmptyStr(WC->http_host)) {
+       wcsession *WCC = WC;
+
+       if (StrLength(WCC->Hdr->http_host)!=0) {
                wprintf("%s://%s",
                        (is_https ? "https" : "http"),
-                       WC->http_host);
+                       ChrPtr(WCC->Hdr->http_host));
        }
 }
+
+
+void 
+InitModule_GROUPDAV
+(void)
+{
+
+       WebcitAddUrlHandler(HKEY("groupdav"), do_logout, XHTTP_COMMANDS|COOKIEUNNEEDED|FORCE_SESSIONCLOSE);
+
+}