--- libaitwww/src/aitwww.c 2012/03/09 13:24:58 1.1.1.1.2.5 +++ libaitwww/src/aitwww.c 2012/07/31 23:08:40 1.3.4.3 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: aitwww.c,v 1.1.1.1.2.5 2012/03/09 13:24:58 misho Exp $ +* $Id: aitwww.c,v 1.3.4.3 2012/07/31 23:08:40 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,26 +113,26 @@ 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; } /* allocated space for post data */ - str = malloc(ctlen + 1); + str = io_malloc(ctlen + 1); if (!str) { - LOGERR; + www_SetErr(io_GetErrno(), "%s", io_GetError()); return NULL; } else memset(str, 0, ctlen + 1); @@ -146,10 +145,10 @@ www_initCGI(void) else if (!www_cmp(s, "multipart/form-data")) cgi = www_parseMultiPart(str, ctlen, s); - free(str); + io_free(str); } else { /* Unknown method */ - www_SetErr(EBADMSG, "Unknown request method"); + www_SetErr(EFAULT, "Unknown request method"); return NULL; } @@ -171,16 +170,14 @@ www_closeCGI(cgi_t ** __restrict cgi) return; while ((t = SLIST_FIRST(*cgi))) { - if (t->cgi_name) - free(t->cgi_name); - if (t->cgi_value) - free(t->cgi_value); + io_freeVar(&t->cgi_name); + io_freeVar(&t->cgi_value); SLIST_REMOVE_HEAD(*cgi, cgi_node); - free(t); + io_free(t); } - free(*cgi); + io_free(*cgi); *cgi = NULL; } @@ -202,31 +199,31 @@ www_parseQuery(const char *str) return NULL; } - cgi = malloc(sizeof(cgi_t)); + cgi = io_malloc(sizeof(cgi_t)); if (!cgi) { - LOGERR; + www_SetErr(io_GetErrno(), "%s", io_GetError()); return NULL; } else { memset(cgi, 0, sizeof(cgi_t)); SLIST_INIT(cgi); } - base = wrk = strdup(str); + base = wrk = io_strdup(str); while (*wrk) { - t = malloc(sizeof(struct tagCGI)); + t = io_malloc(sizeof(struct tagCGI)); if (!t) { - LOGERR; + www_SetErr(io_GetErrno(), "%s", io_GetError()); www_closeCGI(&cgi); return NULL; } else memset(t, 0, sizeof(struct tagCGI)); t->cgi_name = www_getpair(&wrk, "="); - www_unescape(t->cgi_name); + www_unescape(AIT_GET_STR(t->cgi_name)); t->cgi_value = www_getpair(&wrk, "&;"); - www_unescape(t->cgi_value); + www_unescape(AIT_GET_STR(t->cgi_value)); if (!old) SLIST_INSERT_HEAD(cgi, t, cgi_node); @@ -235,7 +232,7 @@ www_parseQuery(const char *str) old = t; } - free(base); + io_free(base); return cgi; } @@ -257,8 +254,8 @@ www_getValue(cgi_t * __restrict cgi, const char *name) } SLIST_FOREACH(t, cgi, cgi_node) - if (t->cgi_name && !strcmp(name, t->cgi_name)) - return t->cgi_value; + if (t->cgi_name && !strcmp(name, AIT_GET_STR(t->cgi_name))) + return AIT_GET_STR(t->cgi_value); return NULL; } @@ -283,11 +280,9 @@ www_addValue(cgi_t * __restrict cgi, const char *name, /* search for update */ SLIST_FOREACH_SAFE(t, cgi, cgi_node, tmp) { - if (t->cgi_name && !strcmp(name, t->cgi_name)) { - if (t->cgi_value) - free(t->cgi_value); - if (value) - t->cgi_value = strdup(value); + if (t->cgi_name && !strcmp(name, AIT_GET_STR(t->cgi_name))) { + AIT_FREE_VAL(t->cgi_value); + AIT_SET_STR(t->cgi_value, value); /* update */ return 1; } @@ -297,16 +292,28 @@ www_addValue(cgi_t * __restrict cgi, const char *name, } /* add new one */ - tmp = malloc(sizeof(struct tagCGI)); + tmp = io_malloc(sizeof(struct tagCGI)); if (!tmp) { - LOGERR; + www_SetErr(io_GetErrno(), "%s", io_GetError()); return -1; } else memset(tmp, 0, sizeof(struct tagCGI)); - tmp->cgi_name = strdup(name); - if (value) - tmp->cgi_value = strdup(value); + tmp->cgi_name = io_allocVar(); + if (!tmp->cgi_name) { + www_SetErr(io_GetErrno(), "%s", io_GetError()); + io_free(tmp); + return -1; + } else + AIT_SET_STR(tmp->cgi_name, name); + tmp->cgi_value = io_allocVar(); + if (!tmp->cgi_name) { + www_SetErr(io_GetErrno(), "%s", io_GetError()); + io_freeVar(&tmp->cgi_name); + io_free(tmp); + return -1; + } else + AIT_SET_STR(tmp->cgi_value, value); if (!t) SLIST_INSERT_HEAD(cgi, tmp, cgi_node); @@ -334,14 +341,12 @@ www_delPair(cgi_t * __restrict cgi, const char *name) /* search for delete */ SLIST_FOREACH_SAFE(t, cgi, cgi_node, tmp) - if (t->cgi_name && !strcmp(name, t->cgi_name)) { + if (t->cgi_name && !strcmp(name, AIT_GET_STR(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); + io_freeVar(&t->cgi_name); + io_freeVar(&t->cgi_value); + io_free(t); return 1; } @@ -391,24 +396,25 @@ www_header(FILE *output) return fputs("Content-type: text/html\n\n", f); } -static char * +static ait_val_t * quotStr(const char *str, const char **end) { - char *s, *e; + char *e; int n, len = 0; register int i; + ait_val_t *s; /* get str w/o " */ if (*str != '"') { n = strspn(str, "!#$%&'*+-.0123456789?ABCDEFGHIJKLMNOPQRSTUVWXYZ" "^_`abcdefghijklmnopqrstuvwxyz{|}~"); - s = malloc(n + 1); + s = io_allocVar(); if (!s) { - LOGERR; + www_SetErr(io_GetErrno(), "%s", io_GetError()); return NULL; } else { - strncpy(s, str, n); - s[n] = 0; + AIT_SET_STRSIZ(s, n + 1); + strlcpy(AIT_GET_STR(s), str, AIT_LEN(s)); *end = str + n; return s; } @@ -419,21 +425,25 @@ quotStr(const char *str, const char **end) return NULL; else len = e - str; - s = malloc(len + 1); + + s = io_allocVar(); if (!s) { - LOGERR; + www_SetErr(io_GetErrno(), "%s", io_GetError()); return NULL; + } else { + AIT_SET_STRSIZ(s, len + 1); + e = AIT_GET_STR(s); } for (i = 0; i < len; i++, str++) { if (*str == '\\' || *str == '\n') - s[i] = *++str; + e[i] = *++str; else if (*str == '"') break; else - s[i] = *str; + e[i] = *str; } - s[i] = 0; + e[i] = 0; *end = ++str; return s; @@ -443,45 +453,41 @@ static struct tagCGI * addAttr(const char **ct) { struct tagCGI *a; - const char *c, *eq; - char *name, *value; + const char *c; + char *eq; if (!*ct || !(c = strchr(*ct, ';'))) return NULL; else c++; - while (isspace(*c)) + while (isspace((int) *c)) c++; if (!(eq = strchr(c, '='))) return NULL; - /* parse name */ - name = malloc(eq - c + 1); - if (!name) { - LOGERR; + a = io_malloc(sizeof(struct tagCGI)); + if (!a) { + www_SetErr(io_GetErrno(), "%s", io_GetError()); return NULL; - } else { - strncpy(name, c, eq - c); - name[eq - c] = 0; } - /* parse value */ - value = quotStr(++eq, &c); - if (!value) { - free(name); + a->cgi_name = io_allocVar(); + if (!a->cgi_name) { + www_SetErr(io_GetErrno(), "%s", io_GetError()); + io_free(a); return NULL; } - /* fill tagCGI */ - a = malloc(sizeof(struct tagCGI)); - if (!a) { - LOGERR; + *eq++ = 0; + AIT_SET_STR(a->cgi_name, c); + a->cgi_value = quotStr(eq, &c); + if (!a->cgi_value) { + io_freeVar(&a->cgi_name); + io_free(a); return NULL; - } else { - a->cgi_name = name; - a->cgi_value = value; - *ct = c; } + + *ct = c; return a; } @@ -502,15 +508,16 @@ www_parseMultiPart(const char *str, int ctlen, const c struct tagCGI *t, *old = NULL; const char *s; int len; + ait_val_t *v; if (!str) { www_SetErr(EINVAL, "String is NULL"); return NULL; } - cgi = malloc(sizeof(cgi_t)); + cgi = io_malloc(sizeof(cgi_t)); if (!cgi) { - LOGERR; + www_SetErr(io_GetErrno(), "%s", io_GetError()); return NULL; } else { memset(cgi, 0, sizeof(cgi_t)); @@ -523,7 +530,8 @@ www_parseMultiPart(const char *str, int ctlen, const c www_closeCGI(&cgi); return NULL; } - mime = mime_parseMultiPart(str, ctlen, www_getAttribute(attr, "boundary"), NULL); + v = www_getAttribute(attr, "boundary"); + mime = mime_parseMultiPart(str, ctlen, AIT_GET_STR(v), NULL); www_freeAttributes(&attr); if (!mime) { www_closeCGI(&cgi); @@ -533,22 +541,33 @@ www_parseMultiPart(const char *str, int ctlen, const c 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)); + t = io_malloc(sizeof(struct tagCGI)); if (!t) { - LOGERR; + www_SetErr(io_GetErrno(), "%s", io_GetError()); mime_close(&mime); www_closeCGI(&cgi); return NULL; } else memset(t, 0, sizeof(struct tagCGI)); - t->cgi_name = strdup(www_getAttribute(attr, "name")); + AIT_COPY_VAL(t->cgi_name, www_getAttribute(attr, "name")); len = mime_calcRawSize(m); - t->cgi_value = malloc(len + 1); - if (t->cgi_value) { - len = mime_getRawData(m, t->cgi_value, len + 1); - t->cgi_value[len] = 0; + t->cgi_value = io_allocVar(); + if (!t->cgi_value) { + www_SetErr(io_GetErrno(), "%s", io_GetError()); + io_freeVar(&t->cgi_name); + io_free(t); + mime_close(&mime); + www_closeCGI(&cgi); + return NULL; + } else { + AIT_SET_STRSIZ(t->cgi_value, len + 1); + len = mime_getRawData(m, AIT_GET_STR(t->cgi_value), AIT_LEN(t->cgi_value)); } www_freeAttributes(&attr); @@ -581,9 +600,9 @@ www_parseAttributes(const char **ct) return NULL; } - attr = malloc(sizeof(cgi_t)); + attr = io_malloc(sizeof(cgi_t)); if (!attr) { - LOGERR; + www_SetErr(io_GetErrno(), "%s", io_GetError()); return NULL; } else { memset(attr, 0, sizeof(cgi_t)); @@ -603,48 +622,26 @@ www_parseAttributes(const char **ct) } /* - * www_freeAttributes() - Free attributes + * www_getAttribute() - Get Attribute from attribute session * - * @attr = Attributes - * return: none + * @cgi = Inited attribute session + * @name = Name of attribute variable + * return: NULL not found or !=NULL value */ -inline void -www_freeAttributes(cgi_t ** __restrict attr) +inline ait_val_t * +www_getAttribute(cgi_t * __restrict cgi, const char *name) { struct tagCGI *t; - if (!attr || !*attr) - return; - - /* free mime attributes */ - while ((t = SLIST_FIRST(*attr))) { - if (t->cgi_name) - free(t->cgi_name); - if (t->cgi_value) - free(t->cgi_value); - SLIST_REMOVE_HEAD(*attr, cgi_node); - free(t); + if (!cgi || !name) { + www_SetErr(EINVAL, "Invalid argument(s)"); + return NULL; } - free(*attr); - *attr = NULL; -} - -/* - * www_getAttribute() - Get attribute by name - * - * @attr = Attributes - * @name = Name of attribute - * return: NULL not found or !=NULL attribute value - */ -inline const char * -www_getAttribute(cgi_t * __restrict attr, const char *name) -{ - struct tagCGI *t; - - SLIST_FOREACH(t, attr, cgi_node) - if (!strcasecmp(t->cgi_name, name)) + SLIST_FOREACH(t, cgi, cgi_node) + if (t->cgi_name && !strcmp(name, AIT_GET_STR(t->cgi_name))) return t->cgi_value; return NULL; } +