]> code.citadel.org Git - citadel.git/blobdiff - webcit/groupdav_propfind.c
if StrBuf_ServGetln() is called in a loop, its return value has to be checked for...
[citadel.git] / webcit / groupdav_propfind.c
index 5973bf2401ddfbe062802556c1f6c39d08325fd2..08dd90ad28377867fc10605df5d1b79df2c1c3a2 100644 (file)
  *     This makes it difficult to read, but we have discovered clients which
  *     crash when you try to pretty it up.
  *
+ * Copyright (c) 2005-2010 by the citadel.org team
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 
 #include "webcit.h"
 #include "webserver.h"
 #include "groupdav.h"
 
+extern int DisableGzip;
+
 /*
  * Given an encoded UID, translate that to an unencoded Citadel EUID and
  * then search for it in the current room.  Return a message number or -1
@@ -42,22 +59,33 @@ long locate_message_by_uid(const char *uid) {
        return(retval);
 }
 
-const folder *GetRESTFolder(int IgnoreFloor)
+
+/*
+ * IgnoreFloor: set to 0 or 1 _nothing else_
+ * Subfolders: direct child floors will be put here.
+ */
+const folder *GetRESTFolder(int IgnoreFloor, HashList *Subfolders)
 {
        wcsession  *WCC = WC;
        void *vFolder;
        const folder *ThisFolder = NULL;
+       const folder *FoundFolder = NULL;
+       const folder *BestGuess = NULL;
+       int nBestGuess = 0;
        HashPos    *itd, *itfl;
        StrBuf     * Dir;
        void       *vDir;
        long        len;
         const char *Key;
-       int i, j, urlp;
+       int iRoom, jURL, urlp;
        int delta;
 
-
-
+/*
+ * Guess room: if the full URL matches a room, list thats it. We also need to remember direct sub rooms.
+ * if the URL is longer, we need to find the "best guess" so we can find the room we're in, and the rest of the URL will be uids and so on.
+ */
        itfl = GetNewHashPos(WCC->Floors, 0);
+       urlp = GetCount(WCC->Directory);
 
        while (GetNextHashPos(WCC->Floors, itfl, &len, &Key, &vFolder) && 
               (ThisFolder == NULL))
@@ -71,18 +99,24 @@ const folder *GetRESTFolder(int IgnoreFloor)
                if (ThisFolder->nRoomNameParts > 1) 
                {
                        /*TODO: is that number all right? */
-                       if (GetCount(WCC->Directory) - ThisFolder->nRoomNameParts != 2)
-                               continue;
-
+//                     if (urlp - ThisFolder->nRoomNameParts != 2) {
+//                             if (BestGuess != NULL)
+//                                     continue;
+//ThisFolder->name
+//                             itd  = GetNewHashPos(WCC->Directory, 0);
+//                             GetNextHashPos(WCC->Directory, itd, &len, &Key, &vDir); //TODO: how many to fast forward?
+//                     }
                        itd  = GetNewHashPos(WCC->Directory, 0);
                        GetNextHashPos(WCC->Directory, itd, &len, &Key, &vDir); //TODO: how many to fast forward?
-       /* Fast forward the floorname we checked above... */
-                       for (i = 0, j = 1; 
-                            (i > ThisFolder->nRoomNameParts) && (j > urlp); 
-                            i++, j++, GetNextHashPos(WCC->Directory, itd, &len, &Key, &vDir))
+       
+                       for (iRoom = 0, /* Fast forward the floorname as we checked it above: */ jURL = IgnoreFloor; 
+
+                            (iRoom <= ThisFolder->nRoomNameParts) && (jURL <= urlp); 
+
+                            iRoom++, jURL++, GetNextHashPos(WCC->Directory, itd, &len, &Key, &vDir))
                        {
                                Dir = (StrBuf*)vDir;
-                               if (strcmp(ChrPtr(ThisFolder->RoomNameParts[i]), 
+                               if (strcmp(ChrPtr(ThisFolder->RoomNameParts[iRoom]), 
                                           ChrPtr(Dir)) != 0)
                                {
                                        DeleteHashPos(&itd);
@@ -90,16 +124,33 @@ const folder *GetRESTFolder(int IgnoreFloor)
                                }
                        }
                        DeleteHashPos(&itd);
-                       DeleteHashPos(&itfl);
-                       return ThisFolder;
+                       /* Gotcha? */
+                       if ((iRoom == ThisFolder->nRoomNameParts) && (jURL == urlp))
+                       {
+                               FoundFolder = ThisFolder;
+                       }
+                       /* URL got more parts then this room, so we remember it for the best guess*/
+                       else if ((jURL <= urlp) &&
+                                (ThisFolder->nRoomNameParts <= nBestGuess))
+                       {
+                               BestGuess = ThisFolder;
+                               nBestGuess = jURL - 1;
+                       }
+                       /* Room has more parts than the URL, it might be a sub-room? */
+                       else if (iRoom <ThisFolder->nRoomNameParts) 
+                       {//// TODO: ThisFolder->nRoomNameParts == urlp - IgnoreFloor???
+                               Put(Subfolders, SKEY(ThisFolder->name), 
+                                   /* Cast away const, its a reference. */
+                                   (void*)ThisFolder, reference_free_handler);
+                       }
                }
                else {
+                       delta = GetCount(WCC->Directory) - ThisFolder->nRoomNameParts;
+                       if ((delta != 2) && (nBestGuess > 1))
+                           continue;
                        
-                       if (GetCount(WCC->Directory) - ThisFolder->nRoomNameParts != 2)
-                               continue;
                        itd  = GetNewHashPos(WCC->Directory, 0);
-                       
-                       
+                                               
                        if (!GetNextHashPos(WCC->Directory, 
                                            itd, &len, &Key, &vDir) ||
                            (vDir == NULL))
@@ -120,22 +171,31 @@ const folder *GetRESTFolder(int IgnoreFloor)
                                lprintf(0, "5\n");
                                continue;
                        }
-                       
                        DeleteHashPos(&itfl);
                        DeleteHashPos(&itd);
-                       
-                       return ThisFolder;;
+                       if (delta != 2) {
+                               nBestGuess = 1;
+                               BestGuess = ThisFolder;
+                       }
+                       else 
+                               FoundFolder = ThisFolder;
                }
        }
+
+/* TODO: Subfolders: remove patterns not matching the best guess or thisfolder */
        DeleteHashPos(&itfl);
-       return NULL;
+       if (FoundFolder != NULL)
+               return FoundFolder;
+       else
+               return BestGuess;
 }
 
 
 
 
-long GotoRestRoom()
+long GotoRestRoom(HashList *SubRooms)
 {
+       int IgnoreFloor = 0; /* deprecated... */
        wcsession *WCC = WC;
        long Count;
        long State;
@@ -153,46 +213,43 @@ long GotoRestRoom()
        if (Count >= 1) State |=REST_IN_FLOOR;
        if (Count == 1) return State;
        
-       if (Count >= 3) {
-               State |= REST_IN_FLOOR;
-               ThisFolder = GetRESTFolder(0);
-               WCC->ThisRoom = ThisFolder;
-               if (ThisFolder != NULL)
-               {
-                       gotoroom(ThisFolder->name);
-                       State |= REST_IN_ROOM;
-                       return State;
-               }
-               
-       }
-
-
        /* 
         * More than 3 params and no floor found? 
         * -> fall back to old non-floored notation
         */
-
        if ((Count >= 3) && (WCC->CurrentFloor == NULL))
+               IgnoreFloor = 1;
+       if (Count >= 3)
        {
-               ThisFolder = GetRESTFolder(1);
-               WCC->ThisRoom = ThisFolder;
+               IgnoreFloor = 0;
+               State |= REST_IN_FLOOR;
+
+               ThisFolder = GetRESTFolder(IgnoreFloor, SubRooms);
                if (ThisFolder != NULL)
                {
-                       gotoroom(ThisFolder->name);
+                       if (WCC->ThisRoom != NULL)
+                               if (CompareRooms(WCC->ThisRoom, ThisFolder) != 0)
+                                       gotoroom(ThisFolder->name);
                        State |= REST_IN_ROOM;
-                       return State;
+                       
                }
-
-
+               if (GetCount(SubRooms) > 0)
+                       State |= REST_HAVE_SUB_ROOMS;
        }
+       if ((WCC->ThisRoom != NULL) && 
+           (Count + IgnoreFloor > 3))
+       {
+               if (WCC->Hdr->HR.Handler->RID(ExistsID, IgnoreFloor))
+               {
+                       State |= REST_GOT_LOCAL_PART;
+               }
+               else {
+                       /// WHOOPS, not there???
+                       State |= REST_NONEXIST;
+               }
 
 
-       if (Count == 3) return State;
-
-       /// TODO: ID detection
-       /// TODO: File detection
-
-
+       }
        return State;
 }
 
@@ -235,7 +292,8 @@ void groupdav_collection_list(void)
        groupdav_common_headers();
        hprintf("Date: %s\r\n", datestring);
        hprintf("Content-type: text/xml\r\n");
-       hprintf("Content-encoding: identity\r\n");
+       if (DisableGzip || (!WCC->Hdr->HR.gzip_ok))     
+               hprintf("Content-encoding: identity\r\n");
 
        begin_burst();
 
@@ -382,6 +440,10 @@ void groupdav_collection_list(void)
  */
 void groupdav_propfind(void) 
 {
+#ifdef DEV_RESTDAV
+       HashList *SubRooms = NULL;
+       long State;
+#endif
        wcsession *WCC = WC;
        StrBuf *dav_roomname;
        StrBuf *dav_uid;
@@ -395,7 +457,6 @@ void groupdav_propfind(void)
        int i;
        char datestring[256];
        time_t now;
-       long State;
 
        now = time(NULL);
        http_datestring(datestring, sizeof datestring, now);
@@ -409,9 +470,10 @@ void groupdav_propfind(void)
         * If the room name is blank, the client is requesting a
         * folder list.
         */
-       State = GotoRestRoom();
+       SubRooms = NewHash(1, Flathash);
+       State = GotoRestRoom(SubRooms);
        if (((State & REST_IN_ROOM) == 0) ||
-           (((State & (REST_GOT_EUID|REST_GOT_ID|REST_GOT_FILENAME)) == 0) &&
+           (((State & (REST_GOT_LOCAL_PART)) == 0) &&
             (WCC->Hdr->HR.dav_depth == 0)))
        {
                now = time(NULL);
@@ -425,7 +487,8 @@ void groupdav_propfind(void)
                groupdav_common_headers();
                hprintf("Date: %s\r\n", datestring);
                hprintf("Content-type: text/xml\r\n");
-               hprintf("Content-encoding: identity\r\n");
+               if (DisableGzip || (!WCC->Hdr->HR.gzip_ok))     
+                       hprintf("Content-encoding: identity\r\n");
 
                begin_burst();
 
@@ -437,14 +500,21 @@ void groupdav_propfind(void)
                end_burst();
                FreeStrBuf(&dav_roomname);
                FreeStrBuf(&dav_uid);
+               FreeHashList(&SubRooms);
                return;
        }
 
-       if ((State & (REST_GOT_EUID|REST_GOT_ID|REST_GOT_FILENAME)) == 0) {
+       if ((State & (REST_GOT_LOCAL_PART)) == 0) {
                readloop(headers, eReadEUIDS);
+               FreeHashList(&SubRooms);
                return;
 
        }
+
+
+       
+       FreeHashList(&SubRooms);
+
 #endif
 
        /*
@@ -505,7 +575,8 @@ void groupdav_propfind(void)
                groupdav_common_headers();
                hprintf("Date: %s\r\n", datestring);
                hprintf("Content-type: text/xml\r\n");
-               hprintf("Content-encoding: identity\r\n");
+               if (DisableGzip || (!WCC->Hdr->HR.gzip_ok))     
+                       hprintf("Content-encoding: identity\r\n");
        
                begin_burst();
        
@@ -555,7 +626,8 @@ void groupdav_propfind(void)
        groupdav_common_headers();
        hprintf("Date: %s\r\n", datestring);
        hprintf("Content-type: text/xml\r\n");
-       hprintf("Content-encoding: identity\r\n");
+       if (DisableGzip || (!WCC->Hdr->HR.gzip_ok))     
+               hprintf("Content-encoding: identity\r\n");
 
        begin_burst();
 
@@ -610,7 +682,10 @@ void groupdav_propfind(void)
 
        StrBuf_ServGetln(MsgNum);
        if (GetServerStatus(MsgNum, NULL) == 1)
-               while (BufLen = StrBuf_ServGetln(MsgNum), strcmp(ChrPtr(MsgNum), "000"))  {
+               while (BufLen = StrBuf_ServGetln(MsgNum), 
+                      ((BufLen >= 0) && 
+                       ((BufLen != 3) || strcmp(ChrPtr(MsgNum), "000"))  ))
+               {
                        msgs = realloc(msgs, ++num_msgs * sizeof(long));
                        msgs[num_msgs-1] = StrTol(MsgNum);
                }
@@ -622,7 +697,9 @@ void groupdav_propfind(void)
                serv_printf("MSG0 %ld|3", msgs[i]);
                StrBuf_ServGetln(MsgNum);
                if (GetServerStatus(MsgNum, NULL) == 1)
-                       while (BufLen = StrBuf_ServGetln(MsgNum), strcmp(ChrPtr(MsgNum), "000")) 
+                       while (BufLen = StrBuf_ServGetln(MsgNum), 
+                              ((BufLen >= 0) && 
+                               ((BufLen != 3) || strcmp(ChrPtr(MsgNum), "000")) ))
                        {
                                if (!strncasecmp(ChrPtr(MsgNum), "exti=", 5)) {
                                        strcpy(uid, &ChrPtr(MsgNum)[5]);
@@ -686,6 +763,8 @@ int ParseMessageListHeaders_EUID(StrBuf *Line,
 {
        Msg->euid = NewStrBuf();
        StrBufExtract_NextToken(Msg->euid,  Line, pos, '|');
+       Msg->date = StrBufExtractNext_long(Line, pos, '|');
+       
        return StrLength(Msg->euid) > 0;
 }