--- libaitwww/src/aitwww.c 2012/03/09 09:38:55 1.1.1.1.2.1 +++ libaitwww/src/aitwww.c 2012/07/31 11:56:16 1.3.4.1 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: aitwww.c,v 1.1.1.1.2.1 2012/03/09 09:38:55 misho Exp $ +* $Id: aitwww.c,v 1.3.4.1 2012/07/31 11:56:16 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -44,7 +44,6 @@ OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF TH SUCH DAMAGE. */ #include "global.h" -#include "tools.h" #include "mime.h" @@ -76,9 +75,9 @@ www_SetErr(int eno, char *estr, ...) va_list lst; www_Errno = eno; - memset(www_Error, 0, STRSIZ); + memset(www_Error, 0, sizeof www_Errno); va_start(lst, estr); - vsnprintf(www_Error, STRSIZ, estr, lst); + vsnprintf(www_Error, sizeof www_Errno, estr, lst); va_end(lst); } @@ -99,14 +98,14 @@ www_initCGI(void) str = getenv("REQUEST_METHOD"); if (!str) { - www_SetErr(EBADMSG, "Request method not found"); + www_SetErr(EFAULT, "Request method not found"); return NULL; } if (!strcmp(str, "GET") || !strcmp(str, "HEAD")) { /* GET | HEAD */ str = getenv("QUERY_STRING"); if (!str) { - www_SetErr(EBADMSG, "Query string not found"); + www_SetErr(EFAULT, "Query string not found"); return NULL; } cgi = www_parseQuery(str); @@ -114,19 +113,19 @@ www_initCGI(void) /* POST */ str = getenv("CONTENT_LENGTH"); if (!str) { - www_SetErr(EBADMSG, "Content length not found"); + www_SetErr(EFAULT, "Content length not found"); return NULL; } else ctlen = strtol(str, NULL, 0); s = getenv("CONTENT_TYPE"); if (!s) { - www_SetErr(EBADMSG, "Content type not found"); + www_SetErr(EFAULT, "Content type not found"); return NULL; } if (www_cmp(s, "multipart/form-data") && www_cmp(s, "application/x-www-form-urlencoded")) { - www_SetErr(EBADMSG, "MIME parts are broken"); + www_SetErr(EFAULT, "MIME parts are broken"); return NULL; } @@ -135,7 +134,8 @@ www_initCGI(void) if (!str) { LOGERR; return NULL; - } + } else + memset(str, 0, ctlen + 1); for (i = 0; i < ctlen && (rlen = read(STDIN_FILENO, (void*) str + i, ctlen - i)) > 0; i += rlen); str[ctlen] = 0; @@ -148,7 +148,7 @@ www_initCGI(void) free(str); } else { /* Unknown method */ - www_SetErr(EBADMSG, "Unknown request method"); + www_SetErr(EFAULT, "Unknown request method"); return NULL; } @@ -324,7 +324,7 @@ www_addValue(cgi_t * __restrict cgi, const char *name, int www_delPair(cgi_t * __restrict cgi, const char *name) { - struct tagCGI *t; + struct tagCGI *t, *tmp; if (!cgi || !name) { www_SetErr(EINVAL, "Invalid argument(s)"); @@ -332,9 +332,15 @@ www_delPair(cgi_t * __restrict cgi, const char *name) } /* search for delete */ - SLIST_FOREACH(t, cgi, cgi_node) + SLIST_FOREACH_SAFE(t, cgi, cgi_node, tmp) if (t->cgi_name && !strcmp(name, t->cgi_name)) { SLIST_REMOVE(cgi, t, tagCGI, cgi_node); + + if (t->cgi_name) + free(t->cgi_name); + if (t->cgi_value) + free(t->cgi_value); + free(t); return 1; } @@ -342,6 +348,35 @@ www_delPair(cgi_t * __restrict cgi, const char *name) } /* + * www_listPairs() - Walk over CGI session variables + * + * @cgi = Cgi session + * @func = If !=NULL call function for each element + * @arg = Optional argument pass through callback + * return: -1 error or >-1 number of elements + */ +inline int +www_listPairs(cgi_t * __restrict cgi, list_cb_t func, void *arg) +{ + register int ret = 0; + struct tagCGI *t; + + if (!cgi) { + www_SetErr(EINVAL, "Invalid CGI session argument"); + return -1; + } + + SLIST_FOREACH(t, cgi, cgi_node) { + ret++; + + if (func) + func(t, arg); + } + + return ret; +} + +/* * www_header() - Output initial html header * * @output = file handle @@ -489,10 +524,18 @@ www_parseMultiPart(const char *str, int ctlen, const c } mime = mime_parseMultiPart(str, ctlen, www_getAttribute(attr, "boundary"), NULL); www_freeAttributes(&attr); + if (!mime) { + www_closeCGI(&cgi); + return NULL; + } SLIST_FOREACH(m, mime, mime_node) { s = mime_getValue(m, "content-disposition"); attr = www_parseAttributes(&s); + if (!www_getAttribute(attr, "name")) { + www_freeAttributes(&attr); + continue; + } t = malloc(sizeof(struct tagCGI)); if (!t) {