* merge from dav_rework:
[citadel.git] / webcit / webcit.c
index 427cb50ad6ae2875c168f58e3d759ded95736c78..e88ebf0063a72ecf8782189dfd368cf4d6442067 100644 (file)
 #include "webserver.h"
 
 
-/*
- * String to unset the cookie.
- * Any date "in the past" will work, so I chose my birthday, right down to
- * the exact minute.  :)
- */
-static char *unset = "; expires=28-May-1971 18:10:00 GMT";
 StrBuf *csslocal = NULL;
 HashList *HandlerHash = NULL;
 
@@ -27,8 +21,8 @@ void DeleteWebcitHandler(void *vHandler)
 {
        WebcitHandler *Handler = (WebcitHandler*) vHandler;
        FreeStrBuf(&Handler->Name);
+       FreeStrBuf(&Handler->DisplayName);
        free (Handler);
-
 }
 
 void WebcitAddUrlHandler(const char * UrlString, long UrlSLen, 
@@ -47,7 +41,12 @@ void WebcitAddUrlHandler(const char * UrlString, long UrlSLen,
        Put(HandlerHash, UrlString, UrlSLen, NewHandler, DeleteWebcitHandler);
 }
 
-
+void tmplput_HANDLER_DISPLAYNAME(StrBuf *Target, WCTemplputParams *TP) 
+{
+       wcsession *WCC = WC;
+       if (WCC->Hdr->HR.Handler != NULL)
+               StrBufAppendTemplate(Target, TP, WCC->Hdr->HR.Handler->DisplayName, 0);
+}
 /*
  * web-printing funcion. uses our vsnprintf wrapper
  */
@@ -118,7 +117,6 @@ void output_headers(        int do_httpheaders,     /* 1 = output HTTP headers
                        int cache               /* 1 = allow browser to cache this page             */
 ) {
        wcsession *WCC = WC;
-       char cookie[1024];
        char httpnow[128];
 
        hprintf("HTTP/1.1 200 OK\n");
@@ -159,25 +157,7 @@ void output_headers(       int do_httpheaders,     /* 1 = output HTTP headers
                );
        }
 
-       if (cache < 2) {
-
-               stuff_to_cookie(cookie, 1024, 
-                               WCC->wc_session,
-                               WCC->wc_username,
-                               WCC->wc_password,
-                               WCC->wc_roomname,
-                               get_selected_language()
-                       );
-               
-               if (unset_cookies) {
-                       hprintf("Set-cookie: webcit=%s; path=/\r\n", unset);
-               } else {
-                       hprintf("Set-cookie: webcit=%s; path=/\r\n", cookie);
-                       if (server_cookie != NULL) {
-                               hprintf("%s\n", server_cookie);
-                       }
-               }
-       }
+       if (cache < 2) stuff_to_cookie(unset_cookies);
 
        if (do_htmlhead) {
                begin_burst();
@@ -540,32 +520,57 @@ void ParseREST_URL(void)
        HashList *Floors;
        void *vFloor;
 
+       lprintf(1, "parsing rest URL: %s\n", ChrPtr(WCC->Hdr->HR.ReqLine));
+
        WCC->Directory = NewHash(1, Flathash);
+       WCC->CurrentFloor = NULL;
 
        Buf = NewStrBuf();
-       while (StrBufExtract_NextToken(WCC->Hdr->HR.ReqLine, 
-                                      Buf, &pCh,  '/') >= 0)
+       while (StrBufExtract_NextToken(Buf, WCC->Hdr->HR.ReqLine, &pCh,  '/') >= 0)
        {
-               Put(WCC->Directory, IKEY(i), Buf, HFreeStrBuf);
-               if (i==0)
-                       pFloor = Buf;
+               if (StrLength(Buf) != 0) {
+                       /* ignore empty path segments */
+                       StrBufUnescape(Buf, 1);
+                       Put(WCC->Directory, IKEY(i), Buf, HFreeStrBuf);
+                       if (i==0)
+                               pFloor = Buf;
+                       Buf = NewStrBuf();
+               }
                i++;
-               Buf = NewStrBuf();
        }
-       if (i == 0)
-               FreeStrBuf(&Buf);
-       else if (pFloor != NULL)
+
+       FreeStrBuf(&Buf);
+       if (pFloor != NULL)
        {
                Floors = GetFloorListHash(NULL, NULL);
                
                if (Floors != NULL)
                {
-                       if (GetHash(Floors, SKEY(pFloor), &vFloor))
-                               WCC->CurrentFloor = (floor*) vFloor;
+                       if (GetHash(WCC->FloorsByName, SKEY(pFloor), &vFloor))
+                               WCC->CurrentFloor = (Floor*) vFloor;
                }
        }
 }
 
+int Conditional_REST_DEPTH(StrBuf *Target, WCTemplputParams *TP)
+{
+       long Depth, IsDepth;
+       long offset = 0;
+       wcsession *WCC = WC;
+
+       if (WCC->Hdr->HR.Handler != NULL)
+               offset ++;
+       Depth = GetTemplateTokenNumber(Target, TP, 2, 0);
+       IsDepth = GetCount(WCC->Directory) + offset;
+
+//     LogTemplateError(Target, "bla", 1, TP, "REST_DEPTH: %ld : %ld\n", Depth, IsDepth);
+       if (Depth < 0) {
+               Depth = -Depth;
+               return IsDepth > Depth;
+       }
+       else 
+               return Depth == IsDepth;
+}
 
 
 
@@ -612,7 +617,7 @@ void session_loop(void)
                if (ibstr("nonce") != WCC->nonce) {
                        lprintf(9, "Ignoring request with mismatched nonce.\n");
                        hprintf("HTTP/1.1 404 Security check failed\r\n");
-                       hprintf("Content-Type: text/plain\r\n\r\n");
+                       hprintf("Content-Type: text/plain\r\n");
                        begin_burst();
                        wc_printf("Security check failed.\r\n");
                        end_burst();
@@ -678,11 +683,11 @@ void session_loop(void)
         * If we aren't in any room yet, but we have cookie data telling us where we're
         * supposed to be, and 'gotofirst' was not specified, then go there.
         */
-       else if ( (StrLength(WCC->wc_roomname) == 0) && ( (StrLength(WCC->Hdr->c_roomname) > 0) )) {
+       else if ( (StrLength(WCC->CurRoom.name) == 0) && ( (StrLength(WCC->Hdr->c_roomname) > 0) )) {
                int ret;
 
                lprintf(9, "We are in '%s' but cookie indicates '%s', going there...\n",
-                       ChrPtr(WCC->wc_roomname),
+                       ChrPtr(WCC->CurRoom.name),
                        ChrPtr(WCC->Hdr->c_roomname)
                );
                ret = gotoroom(WCC->Hdr->c_roomname);   /* do quietly to avoid session output! */
@@ -806,9 +811,12 @@ InitModule_WEBCIT
 
        WebcitAddUrlHandler(HKEY("401"), "", 0, authorization_required, ANONYMOUS|COOKIEUNNEEDED);
        RegisterConditional(HKEY("COND:IMPMSG"), 0, ConditionalImportantMesage, CTX_NONE);
+       RegisterConditional(HKEY("COND:REST:DEPTH"), 0, Conditional_REST_DEPTH, CTX_NONE);
+
        RegisterNamespace("CSSLOCAL", 0, 0, tmplput_csslocal, NULL, CTX_NONE);
        RegisterNamespace("IMPORTANTMESSAGE", 0, 0, tmplput_importantmessage, NULL, CTX_NONE);
        RegisterNamespace("TRAILING_JAVASCRIPT", 0, 0, tmplput_trailing_javascript, NULL, CTX_NONE);
+       RegisterNamespace("URL:DISPLAYNAME", 0, 1, tmplput_HANDLER_DISPLAYNAME, NULL, CTX_NONE);
 
        snprintf(dir, SIZ, "%s/webcit.css", static_local_dir);
        if (!access(dir, R_OK)) {