X-Git-Url: https://code.citadel.org/?a=blobdiff_plain;f=webcit%2Fparamhandling.c;h=f7d1a325d21fd85b83c02b83449628411b2814ec;hb=c610deab77612b51cb238f62acc87fb17726cbff;hp=ed1a1a5879aa1adc1d7d24fda3342d7809f7e089;hpb=2e4f1412f9b10153eb01586c3de7fb7dc26af947;p=citadel.git diff --git a/webcit/paramhandling.c b/webcit/paramhandling.c index ed1a1a587..f7d1a325d 100644 --- a/webcit/paramhandling.c +++ b/webcit/paramhandling.c @@ -1,26 +1,24 @@ /* * parse urlparts and post data * - * Copyright (c) 1996-2011 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 as - * published by the Free Software Foundation; either version 3 of the - * License, or (at your option) any later version. + * 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. - * - * 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" +/* uncomment to see all parameters sent to the server by the browser. */ +/* #define DEBUG_URLSTRINGS */ + + void free_url(void *U) { urlcontent *u = (urlcontent*) U; @@ -33,19 +31,21 @@ void free_url(void *U) */ 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))) { aptr = up; - while ((aptr < eptr) && (*aptr != '\0') && (*aptr != '=')) + while ((aptr < eptr) && (*aptr != '\0') && (*aptr != '=')) { aptr++; + } if (*aptr != '=') { return; } @@ -56,18 +56,19 @@ void ParseURLParams(StrBuf *url) bptr++; } keylen = aptr - up - 1; /* -1 -> '=' */ - if(keylen > sizeof(u->url_key)) { - syslog(1, "URLkey to long! [%s]", up); - continue; + if (keylen > sizeof(u->url_key)) { + syslog(LOG_WARNING, "%s:%d: invalid url_key of size %d in string size %d", + __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(1, "URLkey to long! [%s]", up); + syslog(LOG_WARNING, "%s:%d: invalid url_key of size %d", __FILE__, __LINE__, keylen); free(u); - continue; + return; } if (strncmp(u->url_key, "__", 2) != 0) @@ -77,7 +78,7 @@ void ParseURLParams(StrBuf *url) u->url_data = NewStrBufPlain(aptr, len); StrBufUnescape(u->url_data, 1); #ifdef DEBUG_URLSTRINGS - syslog(9, "%s = [%d] %s\n", + syslog(LOG_DEBUG, "%s = [%d] %s\n", u->url_key, StrLength(u->url_data), ChrPtr(u->url_data)); @@ -87,7 +88,7 @@ void ParseURLParams(StrBuf *url) 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", + syslog(LOG_WARNING, "REJECTED because of __ is internal only: %s = [%d] %s\n", u->url_key, StrLength(u->url_data), ChrPtr(u->url_data)); @@ -324,7 +325,7 @@ void upload_handler(char *name, char *filename, char *partnum, char *disp, long keylen; #ifdef DEBUG_URLSTRINGS - syslog(9, "upload_handler() name=%s, type=%s, len=%d", name, cbtype, length); + syslog(LOG_DEBUG, "upload_handler() name=%s, type=%s, len="SIZE_T_FMT, name, cbtype, length); #endif if (WCC->Hdr->urlstrings == NULL) WCC->Hdr->urlstrings = NewHash(1, NULL); @@ -341,7 +342,7 @@ void upload_handler(char *name, char *filename, char *partnum, char *disp, Put(WCC->Hdr->urlstrings, u->url_key, keylen, u, free_url); } else { - syslog(1, "REJECTED because of __ is internal only: %s = [%d] %s\n", + syslog(LOG_INFO, "REJECTED because of __ is internal only: %s = [%d] %s\n", u->url_key, StrLength(u->url_data), ChrPtr(u->url_data)); @@ -349,7 +350,7 @@ void upload_handler(char *name, char *filename, char *partnum, char *disp, free_url(u); } #ifdef DEBUG_URLSTRINGS - syslog(9, "Key: <%s> len: [%d] Data: <%s>", + syslog(LOG_DEBUG, "Key: <%s> len: [%d] Data: <%s>", u->url_key, StrLength(u->url_data), ChrPtr(u->url_data)); @@ -363,21 +364,19 @@ 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(9, "File: <%s> len: [%ld]", filename, length); + syslog(LOG_DEBUG, "File: <%s> len: [%ld]", filename, (long int)length); #endif } } - - void PutBstr(const char *key, long keylen, StrBuf *Value) { urlcontent *u; - if(keylen > sizeof(u->url_key)) { - syslog(1, "URLkey to long! [%s]", key); + if(keylen >= sizeof(u->url_key)) { + syslog(LOG_WARNING, "%s:%d: invalid url_key of size %ld", __FILE__, __LINE__, keylen); FreeStrBuf(&Value); return; } @@ -386,6 +385,14 @@ void PutBstr(const char *key, long keylen, StrBuf *Value) u->url_data = Value; Put(WC->Hdr->urlstrings, u->url_key, keylen, u, free_url); } +void PutlBstr(const char *key, long keylen, long Value) +{ + StrBuf *Buf; + + Buf = NewStrBufPlain(NULL, sizeof(long) * 16); + StrBufPrintf(Buf, "%ld", Value); + PutBstr(key, keylen, Buf); +} @@ -437,11 +444,11 @@ void diagnostics(void) wc_printf("Session: %d
\n", WC->wc_session); wc_printf("Command:
\n");
 /*	
-StrEscPuts(WC->UrlFragment1);
+StrEscAppend(WC->WBuf, NULL, WC->UrlFragment1, 0, 0);
 	wc_printf("
\n"); - StrEscPuts(WC->UrlFragment2); +StrEscAppend(WC->WBuf, NULL, WC->UrlFragment12 0, 0); wc_printf("
\n"); - StrEscPuts(WC->UrlFragment3); +StrEscAppend(WC->WBuf, NULL, WC->UrlFragment3, 0, 0); */ wc_printf("

\n"); wc_printf("Variables:
\n");
@@ -482,14 +489,88 @@ void tmplput_url_part(StrBuf *Target, WCTemplputParams *TP)
 	}
 }
 
+typedef struct __BstrPair {
+	StrBuf *x;
+	StrBuf *y;
+}BstrPair;
+CtxType CTX_BSTRPAIRS = CTX_NONE;
+void HFreeBstrPair(void *pv)
+{
+	BstrPair *p = (BstrPair*) pv;
+	FreeStrBuf(&p->x);
+	FreeStrBuf(&p->y);
+	free(pv);
+}
+
+HashList *iterate_GetBstrPairs(StrBuf *Target, WCTemplputParams *TP)
+{
+	StrBuf *X, *Y;
+        const char *ch = NULL;
+        long len;
+	const StrBuf *TheBStr;
+	BstrPair *OnePair;
+        HashList *List;
+	const char *Pos = NULL;
+	int i = 0;
+
+	if (HaveTemplateTokenString(NULL, TP, 2, &ch, &len))
+        {
+                GetTemplateTokenString(Target, TP, 2, &ch, &len);
+        }
+	else 
+	{
+		return NULL;
+	}
+
+	TheBStr = SBstr(ch, len);
+	if ((TheBStr == NULL) || (StrLength(TheBStr) == 0))
+		return NULL;
+	List = NewHash(1, NULL);
+	while (Pos != StrBufNOTNULL)
+	{
+		X = NewStrBufPlain(NULL, StrLength(TheBStr));
+		StrBufExtract_NextToken(X, TheBStr, &Pos, '|');
+		if (Pos == StrBufNOTNULL) {
+			FreeStrBuf(&X);
+			DeleteHash(&List);
+			return NULL;
+		}
+		Y = NewStrBufPlain(NULL, StrLength(TheBStr));
+		StrBufExtract_NextToken(Y, TheBStr, &Pos, '|');
+		OnePair = (BstrPair*)malloc(sizeof(BstrPair));
+		OnePair->x = X;
+		OnePair->y = Y;
+		Put(List, IKEY(i), OnePair, HFreeBstrPair);
+		i++;
+	}
+	return List;
+}
+
+
+void tmplput_bstr_pair(StrBuf *Target, WCTemplputParams *TP, int XY)
+{
+	BstrPair *Pair = (BstrPair*) CTX(CTX_BSTRPAIRS);
+
+	StrBufAppendTemplate(Target, TP, (XY)?Pair->y:Pair->x, 0);
+}
+
+void tmplput_bstr_pair_x(StrBuf *Target, WCTemplputParams *TP)
+{	tmplput_bstr_pair(Target, TP, 0); }
+void tmplput_bstr_pair_y(StrBuf *Target, WCTemplputParams *TP)
+{	tmplput_bstr_pair(Target, TP, 1); }
 
 void 
 InitModule_PARAMHANDLING
 (void)
 {
+	RegisterCTX(CTX_BSTRPAIRS);
 	WebcitAddUrlHandler(HKEY("diagnostics"), "", 0, diagnostics, NEED_URL);
 
-	RegisterConditional(HKEY("COND:BSTR"), 1, ConditionalBstr, CTX_NONE);
+	RegisterIterator("ITERATE:BSTR:PAIR", 1, NULL, iterate_GetBstrPairs, NULL, DeleteHash, CTX_BSTRPAIRS, CTX_NONE, IT_NOFLAG);
+	RegisterNamespace("BSTR:PAIR:X", 1, 2, tmplput_bstr_pair_x, NULL, CTX_BSTRPAIRS);
+	RegisterNamespace("BSTR:PAIR:Y", 1, 2, tmplput_bstr_pair_y, NULL, CTX_BSTRPAIRS);
+
+	RegisterConditional("COND:BSTR", 1, ConditionalBstr, 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);