removed a bunch of blank comment lines
[citadel.git] / webcit / paramhandling.c
index 8ab501bea0586984bbfe2d4a8c137df8a0b088dd..a2dcca2f368f9efe1d0e8629c9b19cc58f5764ec 100644 (file)
@@ -1,7 +1,15 @@
 /*
- * $Id: paramhandling.c 6808 2008-12-11 00:00:36Z dothebart $
- *
  * parse urlparts and post data
+ *
+ * Copyright (c) 1996-2012 by the citadel.org team
+ *
+ * This program is open source software.  You can redistribute it and/or
+ * modify it under the terms of the GNU General Public License, version 3.
+ *
+ * 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.
  */
 
 #include "webcit.h"
@@ -24,8 +32,8 @@ void ParseURLParams(StrBuf *url)
        urlcontent *u;
        wcsession *WCC = WC;
 
-       if (WCC->urlstrings == NULL)
-               WCC->urlstrings = NewHash(1, NULL);
+       if (WCC->Hdr->urlstrings == NULL)
+               WCC->Hdr->urlstrings = NewHash(1, NULL);
        eptr = ChrPtr(url) + StrLength(url);
        up = ChrPtr(url);
        while ((up < eptr) && (!IsEmptyStr(up))) {
@@ -43,7 +51,7 @@ void ParseURLParams(StrBuf *url)
                }
                keylen = aptr - up - 1; /* -1 -> '=' */
                if(keylen > sizeof(u->url_key)) {
-                       lprintf(1, "URLkey to long! [%s]", up);
+                       syslog(1, "URLkey to long! [%s]", up);
                        continue;
                }
 
@@ -51,24 +59,37 @@ void ParseURLParams(StrBuf *url)
                memcpy(u->url_key, up, keylen);
                u->url_key[keylen] = '\0';
                if (keylen < 0) {
-                       lprintf(1, "URLkey to long! [%s]", up);
+                       syslog(1, "URLkey to long! [%s]", up);
                        free(u);
                        continue;
                }
-
-               Put(WCC->urlstrings, u->url_key, keylen, u, free_url);
-               len = bptr - aptr;
-               u->url_data = NewStrBufPlain(aptr, len);
-               StrBufUnescape(u->url_data, 1);
-            
-               up = bptr;
-               ++up;
+               
+               if (strncmp(u->url_key, "__", 2) != 0)
+               {
+                       Put(WCC->Hdr->urlstrings, u->url_key, keylen, u, free_url);
+                       len = bptr - aptr;
+                       u->url_data = NewStrBufPlain(aptr, len);
+                       StrBufUnescape(u->url_data, 1);
 #ifdef DEBUG_URLSTRINGS
-               lprintf(9, "%s = [%ld]  %s\n", 
-                       u->url_key, 
-                       StrLength(u->url_data), 
-                       ChrPtr(u->url_data)); 
+                       syslog(9, "%s = [%d]  %s\n", 
+                               u->url_key, 
+                               StrLength(u->url_data), 
+                               ChrPtr(u->url_data)); 
 #endif
+               }
+               else {
+                       len = bptr - aptr;
+                       u->url_data = NewStrBufPlain(aptr, len);
+                       StrBufUnescape(u->url_data, 1);
+                       syslog(1, "REJECTED because of __ is internal only: %s = [%d]  %s\n", 
+                               u->url_key, 
+                               StrLength(u->url_data), 
+                               ChrPtr(u->url_data)); 
+                       
+                       free_url(u);
+               }
+               up = bptr;
+               ++up;
        }
 }
 
@@ -77,7 +98,7 @@ void ParseURLParams(StrBuf *url)
  */
 void free_urls(void)
 {
-       DeleteHash(&WC->urlstrings);
+       DeleteHash(&WC->Hdr->urlstrings);
 }
 
 /*
@@ -93,10 +114,10 @@ void dump_vars(void)
        const char *HKey;
        HashPos *Cursor;
        
-       Cursor = GetNewHashPos (WCC->urlstrings, 0);
-       while (GetNextHashPos(WCC->urlstrings, Cursor, &HKLen, &HKey, &U)) {
+       Cursor = GetNewHashPos (WCC->Hdr->urlstrings, 0);
+       while (GetNextHashPos(WCC->Hdr->urlstrings, Cursor, &HKLen, &HKey, &U)) {
                u = (urlcontent*) U;
-               wprintf("%38s = %s\n", u->url_key, ChrPtr(u->url_data));
+               wc_printf("%38s = %s\n", u->url_key, ChrPtr(u->url_data));
        }
 }
 
@@ -108,8 +129,8 @@ const char *XBstr(const char *key, size_t keylen, size_t *len)
 {
        void *U;
 
-       if ((WC->urlstrings != NULL) && 
-           GetHash(WC->urlstrings, key, keylen, &U)) {
+       if ((WC->Hdr->urlstrings != NULL) && 
+           GetHash(WC->Hdr->urlstrings, key, keylen, &U)) {
                *len = StrLength(((urlcontent *)U)->url_data);
                return ChrPtr(((urlcontent *)U)->url_data);
        }
@@ -123,8 +144,8 @@ const char *XBSTR(const char *key, size_t *len)
 {
        void *U;
 
-       if ((WC->urlstrings != NULL) &&
-           GetHash(WC->urlstrings, key, strlen (key), &U)){
+       if ((WC->Hdr->urlstrings != NULL) &&
+           GetHash(WC->Hdr->urlstrings, key, strlen (key), &U)){
                *len = StrLength(((urlcontent *)U)->url_data);
                return ChrPtr(((urlcontent *)U)->url_data);
        }
@@ -139,8 +160,8 @@ const char *BSTR(const char *key)
 {
        void *U;
 
-       if ((WC->urlstrings != NULL) &&
-           GetHash(WC->urlstrings, key, strlen (key), &U))
+       if ((WC->Hdr->urlstrings != NULL) &&
+           GetHash(WC->Hdr->urlstrings, key, strlen (key), &U))
                return ChrPtr(((urlcontent *)U)->url_data);
        else    
                return ("");
@@ -150,8 +171,8 @@ const char *Bstr(const char *key, size_t keylen)
 {
        void *U;
 
-       if ((WC->urlstrings != NULL) && 
-           GetHash(WC->urlstrings, key, keylen, &U))
+       if ((WC->Hdr->urlstrings != NULL) && 
+           GetHash(WC->Hdr->urlstrings, key, keylen, &U))
                return ChrPtr(((urlcontent *)U)->url_data);
        else    
                return ("");
@@ -161,8 +182,8 @@ const StrBuf *SBSTR(const char *key)
 {
        void *U;
 
-       if ((WC->urlstrings != NULL) &&
-           GetHash(WC->urlstrings, key, strlen (key), &U))
+       if ((WC->Hdr->urlstrings != NULL) &&
+           GetHash(WC->Hdr->urlstrings, key, strlen (key), &U))
                return ((urlcontent *)U)->url_data;
        else    
                return NULL;
@@ -172,8 +193,8 @@ const StrBuf *SBstr(const char *key, size_t keylen)
 {
        void *U;
 
-       if ((WC->urlstrings != NULL) && 
-           GetHash(WC->urlstrings, key, keylen, &U))
+       if ((WC->Hdr->urlstrings != NULL) && 
+           GetHash(WC->Hdr->urlstrings, key, keylen, &U))
                return ((urlcontent *)U)->url_data;
        else    
                return NULL;
@@ -183,8 +204,8 @@ long LBstr(const char *key, size_t keylen)
 {
        void *U;
 
-       if ((WC->urlstrings != NULL) && 
-           GetHash(WC->urlstrings, key, keylen, &U))
+       if ((WC->Hdr->urlstrings != NULL) && 
+           GetHash(WC->Hdr->urlstrings, key, keylen, &U))
                return StrTol(((urlcontent *)U)->url_data);
        else    
                return (0);
@@ -194,8 +215,8 @@ long LBSTR(const char *key)
 {
        void *U;
 
-       if ((WC->urlstrings != NULL) && 
-           GetHash(WC->urlstrings, key, strlen(key), &U))
+       if ((WC->Hdr->urlstrings != NULL) && 
+           GetHash(WC->Hdr->urlstrings, key, strlen(key), &U))
                return StrTol(((urlcontent *)U)->url_data);
        else    
                return (0);
@@ -205,8 +226,8 @@ int IBstr(const char *key, size_t keylen)
 {
        void *U;
 
-       if ((WC->urlstrings != NULL) && 
-           GetHash(WC->urlstrings, key, keylen, &U))
+       if ((WC->Hdr->urlstrings != NULL) && 
+           GetHash(WC->Hdr->urlstrings, key, keylen, &U))
                return StrTol(((urlcontent *)U)->url_data);
        else    
                return (0);
@@ -216,8 +237,8 @@ int IBSTR(const char *key)
 {
        void *U;
 
-       if ((WC->urlstrings != NULL) && 
-           GetHash(WC->urlstrings, key, strlen(key), &U))
+       if ((WC->Hdr->urlstrings != NULL) && 
+           GetHash(WC->Hdr->urlstrings, key, strlen(key), &U))
                return StrToi(((urlcontent *)U)->url_data);
        else    
                return (0);
@@ -227,8 +248,8 @@ int HaveBstr(const char *key, size_t keylen)
 {
        void *U;
 
-       if ((WC->urlstrings != NULL) && 
-           GetHash(WC->urlstrings, key, keylen, &U))
+       if ((WC->Hdr->urlstrings != NULL) && 
+           GetHash(WC->Hdr->urlstrings, key, keylen, &U))
                return (StrLength(((urlcontent *)U)->url_data) != 0);
        else    
                return (0);
@@ -238,8 +259,8 @@ int HAVEBSTR(const char *key)
 {
        void *U;
 
-       if ((WC->urlstrings != NULL) && 
-           GetHash(WC->urlstrings, key, strlen(key), &U))
+       if ((WC->Hdr->urlstrings != NULL) && 
+           GetHash(WC->Hdr->urlstrings, key, strlen(key), &U))
                return (StrLength(((urlcontent *)U)->url_data) != 0);
        else    
                return (0);
@@ -250,8 +271,8 @@ int YesBstr(const char *key, size_t keylen)
 {
        void *U;
 
-       if ((WC->urlstrings != NULL) && 
-           GetHash(WC->urlstrings, key, keylen, &U))
+       if ((WC->Hdr->urlstrings != NULL) && 
+           GetHash(WC->Hdr->urlstrings, key, keylen, &U))
                return strcmp( ChrPtr(((urlcontent *)U)->url_data), "yes") == 0;
        else    
                return (0);
@@ -261,8 +282,8 @@ int YESBSTR(const char *key)
 {
        void *U;
 
-       if ((WC->urlstrings != NULL) && 
-           GetHash(WC->urlstrings, key, strlen(key), &U))
+       if ((WC->Hdr->urlstrings != NULL) && 
+           GetHash(WC->Hdr->urlstrings, key, strlen(key), &U))
                return strcmp( ChrPtr(((urlcontent *)U)->url_data), "yes") == 0;
        else    
                return (0);
@@ -292,43 +313,53 @@ void upload_handler(char *name, char *filename, char *partnum, char *disp,
                        void *content, char *cbtype, char *cbcharset,
                        size_t length, char *encoding, char *cbid, void *userdata)
 {
+       wcsession *WCC = WC;
        urlcontent *u;
+       long keylen;
+
 #ifdef DEBUG_URLSTRINGS
-       lprintf(9, "upload_handler() name=%s, type=%s, len=%d\n", name, cbtype, length);
+       syslog(9, "upload_handler() name=%s, type=%s, len=%d", name, cbtype, length);
 #endif
-       if (WC->urlstrings == NULL)
-               WC->urlstrings = NewHash(1, NULL);
+       if (WCC->Hdr->urlstrings == NULL)
+               WCC->Hdr->urlstrings = NewHash(1, NULL);
 
        /* Form fields */
        if ( (length > 0) && (IsEmptyStr(cbtype)) ) {
                u = (urlcontent *) malloc(sizeof(urlcontent));
                
-               safestrncpy(u->url_key, name, sizeof(u->url_key));
+               keylen = safestrncpy(u->url_key, name, sizeof(u->url_key));
                u->url_data = NewStrBufPlain(content, length);
                
-               Put(WC->urlstrings, u->url_key, strlen(u->url_key), u, free_url);
+               if (strncmp(u->url_key, "__", 2) != 0)
+               {
+                       Put(WCC->Hdr->urlstrings, u->url_key, keylen, u, free_url);
+               }
+               else {
+                       syslog(1, "REJECTED because of __ is internal only: %s = [%d]  %s\n", 
+                               u->url_key, 
+                               StrLength(u->url_data), 
+                               ChrPtr(u->url_data)); 
+                       
+                       free_url(u);
+               }
 #ifdef DEBUG_URLSTRINGS
-               lprintf(9, "Key: <%s> len: [%ld] Data: <%s>\n", 
+               syslog(9, "Key: <%s> len: [%d] Data: <%s>", 
                        u->url_key, 
                        StrLength(u->url_data), 
                        ChrPtr(u->url_data));
 #endif
        }
 
-       /** Uploaded files */
+       /* Uploaded files */
        if ( (length > 0) && (!IsEmptyStr(cbtype)) ) {
-               WC->upload = malloc(length);
-               if (WC->upload != NULL) {
-                       WC->upload_length = length;
-                       safestrncpy(WC->upload_filename, filename,
-                                       sizeof(WC->upload_filename));
-                       safestrncpy(WC->upload_content_type, cbtype,
-                                       sizeof(WC->upload_content_type));
-                       memcpy(WC->upload, content, length);
-               }
-               else {
-                       lprintf(3, "malloc() failed: %s\n", strerror(errno));
-               }
+               WCC->upload = NewStrBufPlain(content, length);
+               WCC->upload_length = length;
+               WCC->upload_filename = NewStrBufPlain(filename, -1);
+               safestrncpy(WCC->upload_content_type, cbtype, sizeof(WC->upload_content_type));
+#ifdef DEBUG_URLSTRINGS
+               syslog(9, "File: <%s> len: [%ld]", filename, length);
+#endif
+               
        }
 
 }
@@ -340,14 +371,14 @@ void PutBstr(const char *key, long keylen, StrBuf *Value)
        urlcontent *u;
 
        if(keylen > sizeof(u->url_key)) {
-               lprintf(1, "URLkey to long! [%s]", key);
+               syslog(1, "URLkey to long! [%s]", key);
                FreeStrBuf(&Value);
                return;
        }
        u = (urlcontent*)malloc(sizeof(urlcontent));
        memcpy(u->url_key, key, keylen + 1);
        u->url_data = Value;
-       Put(WC->urlstrings, u->url_key, keylen, u, free_url);
+       Put(WC->Hdr->urlstrings, u->url_key, keylen, u, free_url);
 }
 
 
@@ -357,11 +388,21 @@ int ConditionalBstr(StrBuf *Target, WCTemplputParams *TP)
        if(TP->Tokens->nParameters == 3)
                return HaveBstr(TKEY(2));
        else {
-               if (TP->Tokens->Params[3]->Type == TYPE_LONG)
-                       return LBstr(TKEY(2)) == TP->Tokens->Params[3]->lvalue;
-               else 
-                       return strcmp(Bstr(TKEY(2)),
-                                     TP->Tokens->Params[3]->Start) == 0;
+               if (IS_NUMBER(TP->Tokens->Params[3]->Type))
+               {
+                       return LBstr(TKEY(2)) == 
+                               GetTemplateTokenNumber(Target, 
+                                                      TP, 
+                                                      3, 
+                                                      0);
+               }
+               else {
+                       const char *pch;
+                       long len;
+
+                       GetTemplateTokenString (Target, TP, 3, &pch, &len);
+                       return strcmp(Bstr(TKEY(2)), pch) == 0;
+               }
        }
 }
 
@@ -372,40 +413,66 @@ void tmplput_bstr(StrBuf *Target, WCTemplputParams *TP)
                StrBufAppendTemplate(Target, TP, Buf, 1);
 }
 
+
+void tmplput_bstrforward(StrBuf *Target, WCTemplputParams *TP)
+{
+       const StrBuf *Buf = SBstr(TKEY(0));
+       if (Buf != NULL) {
+               StrBufAppendBufPlain(Target, HKEY("?"), 0);             
+               StrBufAppendBufPlain(Target, TKEY(0), 0);
+               StrBufAppendBufPlain(Target, HKEY("="), 0);             
+               StrBufAppendTemplate(Target, TP, Buf, 1);
+       }
+}
+
 void diagnostics(void)
 {
        output_headers(1, 1, 1, 0, 0, 0);
-       wprintf("Session: %d<hr />\n", WC->wc_session);
-       wprintf("Command: <br /><PRE>\n");
-       StrEscPuts(WC->UrlFragment1);
-       wprintf("<br />\n");
-       StrEscPuts(WC->UrlFragment2);
-       wprintf("<br />\n");
-       StrEscPuts(WC->UrlFragment3);
-       wprintf("</PRE><hr />\n");
-       wprintf("Variables: <br /><PRE>\n");
+       wc_printf("Session: %d<hr />\n", WC->wc_session);
+       wc_printf("Command: <br><PRE>\n");
+/*     
+StrEscAppend(WC->WBuf, NULL, WC->UrlFragment1, 0, 0);
+       wc_printf("<br>\n");
+StrEscAppend(WC->WBuf, NULL, WC->UrlFragment12 0, 0);
+       wc_printf("<br>\n");
+StrEscAppend(WC->WBuf, NULL, WC->UrlFragment3, 0, 0);
+*/
+       wc_printf("</PRE><hr />\n");
+       wc_printf("Variables: <br><PRE>\n");
        dump_vars();
-       wprintf("</PRE><hr />\n");
+       wc_printf("</PRE><hr />\n");
        wDumpContent(1);
 }
 
 
 void tmplput_url_part(StrBuf *Target, WCTemplputParams *TP)
 {
-       StrBuf *UrlBuf;
+       StrBuf *Name = NULL;
+       StrBuf *UrlBuf = NULL;
        wcsession *WCC = WC;
        
        if (WCC != NULL) {
-               if (TP->Tokens->Params[0]->lvalue == 0)
-                       UrlBuf = WCC->UrlFragment1;
-               else if (TP->Tokens->Params[0]->lvalue == 1)
-                       UrlBuf = WCC->UrlFragment2;
-               else
-                       UrlBuf = WCC->UrlFragment3;
+               long n;
+
+               n = GetTemplateTokenNumber(Target, TP, 0, 0);
+               if (n == 0) {
+                       if (WCC->Hdr->HR.Handler != NULL)
+                               UrlBuf = Name = WCC->Hdr->HR.Handler->Name;
+               }
+               else if (n == 1) {
+                       UrlBuf = NewStrBuf();
+                       StrBufExtract_token(UrlBuf, WCC->Hdr->HR.ReqLine, 0, '/');
+               }
+               else {
+                       UrlBuf = NewStrBuf();
+                       StrBufExtract_token(UrlBuf, WCC->Hdr->HR.ReqLine, 1, '/');
+               }
+
                if (UrlBuf == NULL)  {
                        LogTemplateError(Target, "urlbuf", ERR_PARM1, TP, "not set.");
                }
                StrBufAppendTemplate(Target, TP, UrlBuf, 2);
+               if (Name == NULL) FreeStrBuf(&UrlBuf);
        }
 }
 
@@ -414,11 +481,12 @@ void
 InitModule_PARAMHANDLING
 (void)
 {
-       WebcitAddUrlHandler(HKEY("diagnostics"), diagnostics, NEED_URL);
+       WebcitAddUrlHandler(HKEY("diagnostics"), "", 0, diagnostics, NEED_URL);
 
        RegisterConditional(HKEY("COND:BSTR"), 1, ConditionalBstr, CTX_NONE);
-       RegisterNamespace("BSTR", 1, 2, tmplput_bstr, CTX_NONE);
-       RegisterNamespace("URLPART", 1, 2, tmplput_url_part, CTX_NONE);
+       RegisterNamespace("BSTR", 1, 2, tmplput_bstr, NULL, CTX_NONE);
+       RegisterNamespace("BSTR:FORWARD", 1, 2, tmplput_bstrforward, NULL, CTX_NONE);
+       RegisterNamespace("URLPART", 1, 2, tmplput_url_part, NULL, CTX_NONE);
 }
 
 
@@ -426,12 +494,13 @@ void
 SessionAttachModule_PARAMHANDLING
 (wcsession *sess)
 {
-       sess->urlstrings = NewHash(1,NULL);
+       sess->Hdr->urlstrings = NewHash(1,NULL);
 }
 
 void
 SessionDetachModule_PARAMHANDLING
 (wcsession *sess)
 {
-       DeleteHash(&sess->urlstrings);
+       DeleteHash(&sess->Hdr->urlstrings);
+       FreeStrBuf(&sess->upload_filename);
 }