X-Git-Url: https://code.citadel.org/?p=citadel.git;a=blobdiff_plain;f=webcit%2Fparamhandling.c;h=15905caa8b7caa0d30483c0f49247a665b290224;hp=46ac87280e970bb90976bd4dbd4932511c929194;hb=b98d9f087b3a24a9549470ab066c2aea187ecfba;hpb=b39d860713ddd390339211be8d398c4d4b6680ba diff --git a/webcit/paramhandling.c b/webcit/paramhandling.c index 46ac87280..15905caa8 100644 --- a/webcit/paramhandling.c +++ b/webcit/paramhandling.c @@ -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. @@ -15,31 +15,155 @@ #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 == 0) { + 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 ((have_colons == 2) && + (*pch == '%') && + (*(pch + 1) == '3') && + ((*(pch + 2) == 'A') || + (*(pch + 1) == 'a') + )) + { + *pch = '\0'; + + if (i == 0) { + /* Separate the toplevel key : */ + u->klen = pch - pchs; + } + + /* sub-section: */ + keys[i] = pchs; + lengths[i] = pch - pchs; + + pch += 3; + + pchs = pch; + i++; + } + else if ((have_colons == 1) && + (*pch == ':')) { + *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++; + } + else if (pch == pche){ + /* sub-section: */ + keys[i] = pchs; + lengths[i] = pch - pchs; + i++; + break; + } + else { + pch ++; + } + } + + 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; + } + else if ((*aptr == '%') && + (*(aptr + 1) == '3') && + ((*(aptr + 2) == 'A') || + (*(aptr + 1) == 'a') + )) + { + have_colon = 2; + } aptr++; + } if (*aptr != '=') { return; } @@ -50,38 +174,42 @@ void ParseURLParams(StrBuf *url) bptr++; } keylen = aptr - up - 1; /* -1 -> '=' */ - if(keylen > sizeof(u->url_key)) { - syslog(1, "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(1, "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); #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)); #endif + PutUrlKey(WCC->Hdr->urlstrings, u, have_colon); } 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", + syslog(LOG_WARNING, "REJECTED because of __ is internal only: %s = [%d] %s\n", u->url_key, StrLength(u->url_data), ChrPtr(u->url_data)); @@ -211,17 +339,6 @@ long LBstr(const char *key, size_t keylen) return (0); } -long LBSTR(const char *key) -{ - void *U; - - if ((WC->Hdr->urlstrings != NULL) && - GetHash(WC->Hdr->urlstrings, key, strlen(key), &U)) - return StrTol(((urlcontent *)U)->url_data); - else - return (0); -} - int IBstr(const char *key, size_t keylen) { void *U; @@ -255,41 +372,133 @@ int HaveBstr(const char *key, size_t keylen) return (0); } -int HAVEBSTR(const char *key) +int YesBstr(const char *key, size_t keylen) { void *U; if ((WC->Hdr->urlstrings != NULL) && - GetHash(WC->Hdr->urlstrings, key, strlen(key), &U)) - return (StrLength(((urlcontent *)U)->url_data) != 0); + GetHash(WC->Hdr->urlstrings, key, keylen, &U)) + return strcmp( ChrPtr(((urlcontent *)U)->url_data), "yes") == 0; else return (0); } - -int YesBstr(const char *key, size_t keylen) +int YESBSTR(const char *key) { void *U; if ((WC->Hdr->urlstrings != NULL) && - GetHash(WC->Hdr->urlstrings, key, keylen, &U)) + GetHash(WC->Hdr->urlstrings, key, strlen(key), &U)) return strcmp( ChrPtr(((urlcontent *)U)->url_data), "yes") == 0; else return (0); } -int YESBSTR(const char *key) + +/* + * Return a sub array that was separated by a colon: + */ +HashList* getSubStruct(const char *key, size_t keylen) { void *U; if ((WC->Hdr->urlstrings != NULL) && GetHash(WC->Hdr->urlstrings, key, strlen(key), &U)) - return strcmp( ChrPtr(((urlcontent *)U)->url_data), "yes") == 0; + return ((urlcontent *)U)->sub; + else + return NULL; +} + + +/* + * Return the value of a variable of a substruct provided by getSubStruct + */ +const char *XSubBstr(HashList *sub, const char *key, size_t keylen, size_t *len) +{ + void *U; + + if ((sub != NULL) && + GetHash(sub, key, keylen, &U)) { + *len = StrLength(((urlcontent *)U)->url_data); + return ChrPtr(((urlcontent *)U)->url_data); + } + else { + *len = 0; + return (""); + } +} + +const char *SubBstr(HashList *sub, const char *key, size_t keylen) +{ + void *U; + + if ((sub != NULL) && + GetHash(sub, key, keylen, &U)) { + return ChrPtr(((urlcontent *)U)->url_data); + } + else + return (""); +} + +const StrBuf *SSubBstr(HashList *sub, const char *key, size_t keylen) +{ + void *U; + + if ((sub != NULL) && + GetHash(sub, key, keylen, &U)) { + return ((urlcontent *)U)->url_data; + } + else + return NULL; +} + +long LSubBstr(HashList *sub, const char *key, size_t keylen) +{ + void *U; + + if ((sub != NULL) && + GetHash(sub, key, keylen, &U)) { + return StrTol(((urlcontent *)U)->url_data); + } + else + return (0); +} + +int ISubBstr(HashList *sub, const char *key, size_t keylen) +{ + void *U; + + if ((sub != NULL) && + GetHash(sub, key, keylen, &U)) { + return StrTol(((urlcontent *)U)->url_data); + } + else + return (0); +} + +int HaveSubBstr(HashList *sub, const char *key, size_t keylen) +{ + void *U; + + if ((sub != NULL) && + GetHash(sub, key, keylen, &U)) { + return (StrLength(((urlcontent *)U)->url_data) != 0); + } else return (0); } +int YesSubBstr(HashList *sub, const char *key, size_t keylen) +{ + void *U; + if ((sub != NULL) && + GetHash(sub, key, keylen, &U)) { + return strcmp( ChrPtr(((urlcontent *)U)->url_data), "yes") == 0; + } + else + return (0); +} /* @@ -318,7 +527,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); @@ -329,13 +538,15 @@ 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(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)); @@ -343,7 +554,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)); @@ -357,30 +568,37 @@ 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; - wcsession *WCC = WC; - if(keylen > sizeof(u->url_key)) { - syslog(1, "invalid url_key from %s", ChrPtr(WCC->Hdr->HR.browser_host)); + if(keylen >= sizeof(u->url_key)) { + 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) +{ + StrBuf *Buf; + + Buf = NewStrBufPlain(NULL, sizeof(long) * 16); + StrBufPrintf(Buf, "%ld", Value); + PutBstr(key, keylen, Buf); +}