Add way to have structured URL-parametrs using : in their names
[citadel.git] / webcit / paramhandling.c
index a317ad39daff72f72d330d5652071913ed978c7d..bfc8ac7ff1dfdead5a1132718425a9415e68e746 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * parse urlparts and post data
  *
- * Copyright (c) 1996-2012 by the citadel.org team
+ * Copyright (c) 1996-2013 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.
 #include "webcit.h"
 #include "webserver.h"
 
+/* uncomment to see all parameters sent to the server by the browser. */
+/* #define DEBUG_URLSTRINGS */
+
+
 void free_url(void *U)
 {
        urlcontent *u = (urlcontent*) U;
        FreeStrBuf(&u->url_data);
+       if (u->sub != NULL) {
+               DeleteHash(&u->sub);
+       }
        free(u);
 }
 
+void PutSubstructUrlKey(HashList *list, urlcontent *u, char **keys, long *lengths, int max, int which){
+       void *vUrl;
+       urlcontent *subu;
+       HashList *thisList = list;
+       if (GetHash(list, keys[which], lengths[which], &vUrl) &&
+           (vUrl != NULL))
+       {
+               subu = (urlcontent*) vUrl;
+               if (subu->sub == NULL) {
+                       subu->sub = NewHash(1, NULL);
+               }
+               thisList = subu->sub;
+       } 
+       else if (which < max) {
+               subu = (urlcontent *) malloc(sizeof(urlcontent));
+               
+               memcpy(subu->url_key, keys[which], lengths[which]);
+               subu->klen = lengths[which];
+               subu->url_data = NULL;
+               subu->sub = NewHash(1, NULL);
+               
+               Put(list, subu->url_key, subu->klen, subu, free_url);
+               thisList = subu->sub;
+       }
+       if (which >= max) {
+               Put(thisList, keys[which], lengths[which], u, free_url);
+       }
+       else {
+               PutSubstructUrlKey(subu->sub, u, keys, lengths, max, which + 1);
+       }
+}
+
+void PutUrlKey(HashList *urlstrings, urlcontent *u, int have_colons) {
+       if (!have_colons) {
+               Put(urlstrings, u->url_key, u->klen, u, free_url);
+       }
+       else {
+               char *keys[10];
+               long lengths[10];
+               int i = 0;
+               char *pch;
+               char *pchs;
+               char *pche;
+
+               memset(&keys, 0, sizeof(keys));
+               memset(&lengths, 0, sizeof(lengths));
+               pchs = pch = u->url_key;
+               pche = u->url_key + u->klen;
+               while ((i < 10) && (pch <= pche)) {
+                       if ((*pch != ':') && (pch != pche)){
+                               pch ++;
+                               continue;
+                       }
+                       *pch = '\0';
+                       if (i == 0) {
+                               /* Separate the toplevel key : */
+                               u->klen = pch - pchs;
+                       }
+                       /* sub-section: */
+                       keys[i] = pchs;
+                       lengths[i] = pch - pchs;
+                       
+                       pch++;
+                       pchs = pch;
+                       i++;
+               }
+               
+               PutSubstructUrlKey(urlstrings, u, keys, lengths, i - 1, 0);
+       }
+}
+
 /*
  * Extract variables from the URL.
  */
 void ParseURLParams(StrBuf *url)
 {
-       const char *aptr, *bptr, *eptr, *up;
-       int len, keylen;
-       urlcontent *u;
+       const char *aptr, *bptr, *eptr, *up = NULL;
+       int len, keylen = 0;
+       urlcontent *u = NULL;
        wcsession *WCC = WC;
 
-       if (WCC->Hdr->urlstrings == 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))) {
+               int have_colon = 0;
                aptr = up;
-               while ((aptr < eptr) && (*aptr != '\0') && (*aptr != '='))
+               while ((aptr < eptr) && (*aptr != '\0') && (*aptr != '=')) {
+                       if (*aptr == ':') {
+                               have_colon = 1;
+                       }
                        aptr++;
+               }
                if (*aptr != '=') {
                        return;
                }
@@ -50,23 +134,26 @@ void ParseURLParams(StrBuf *url)
                        bptr++;
                }
                keylen = aptr - up - 1; /* -1 -> '=' */
-               if(keylen >= sizeof(u->url_key)) {
-                       syslog(LOG_WARNING, "invalid url_key from %s", ChrPtr(WCC->Hdr->HR.browser_host));
-                       return;
+               if (keylen > sizeof(u->url_key)) {
+                       syslog(LOG_WARNING, "%s:%d: invalid url_key of size %d in string size %ld",
+                               __FILE__, __LINE__, keylen, sizeof(u->url_key)
+                       );
                }
 
-               u = (urlcontent *) malloc(sizeof(urlcontent));
-               memcpy(u->url_key, up, keylen);
-               u->url_key[keylen] = '\0';
                if (keylen < 0) {
-                       syslog(LOG_WARNING, "invalid url_key from %s", ChrPtr(WCC->Hdr->HR.browser_host));
+                       syslog(LOG_WARNING, "%s:%d: invalid url_key of size %d", __FILE__, __LINE__, keylen);
                        free(u);
                        return;
                }
                
+               u = (urlcontent *) malloc(sizeof(urlcontent));
+               memcpy(u->url_key, up, keylen);
+               u->url_key[keylen] = '\0';
+               u->klen = keylen;
+               u->sub = NULL;
+
                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);
@@ -76,6 +163,7 @@ void ParseURLParams(StrBuf *url)
                                StrLength(u->url_data), 
                                ChrPtr(u->url_data)); 
 #endif
+                       PutUrlKey(WCC->Hdr->urlstrings, u, have_colon);
                }
                else {
                        len = bptr - aptr;
@@ -329,10 +417,12 @@ void upload_handler(char *name, char *filename, char *partnum, char *disp,
                
                keylen = safestrncpy(u->url_key, name, sizeof(u->url_key));
                u->url_data = NewStrBufPlain(content, length);
+               u->klen = keylen;
+               u->sub = NULL;
                
                if (strncmp(u->url_key, "__", 2) != 0)
                {
-                       Put(WCC->Hdr->urlstrings, u->url_key, keylen, u, free_url);
+                       PutUrlKey(WCC->Hdr->urlstrings, u, (strchr(u->url_key, ':') != NULL));
                }
                else {
                        syslog(LOG_INFO, "REJECTED because of __ is internal only: %s = [%d]  %s\n", 
@@ -357,7 +447,7 @@ void upload_handler(char *name, char *filename, char *partnum, char *disp,
                WCC->upload_filename = NewStrBufPlain(filename, -1);
                safestrncpy(WCC->upload_content_type, cbtype, sizeof(WC->upload_content_type));
 #ifdef DEBUG_URLSTRINGS
-               syslog(LOG_DEBUG, "File: <%s> len: [%ld]", filename, length);
+               syslog(LOG_DEBUG, "File: <%s> len: [%ld]", filename, (long int)length);
 #endif
                
        }
@@ -367,16 +457,17 @@ void upload_handler(char *name, char *filename, char *partnum, char *disp,
 void PutBstr(const char *key, long keylen, StrBuf *Value)
 {
        urlcontent *u;
-       wcsession *WCC = WC;
 
        if(keylen >= sizeof(u->url_key)) {
-               syslog(LOG_WARNING, "invalid url_key from %s", ChrPtr(WCC->Hdr->HR.browser_host));
+               syslog(LOG_WARNING, "%s:%d: invalid url_key of size %ld", __FILE__, __LINE__, keylen);
                FreeStrBuf(&Value);
                return;
        }
        u = (urlcontent*)malloc(sizeof(urlcontent));
        memcpy(u->url_key, key, keylen + 1);
+       u->klen = keylen;
        u->url_data = Value;
+       u->sub = NULL;
        Put(WC->Hdr->urlstrings, u->url_key, keylen, u, free_url);
 }
 void PutlBstr(const char *key, long keylen, long Value)