--- libaitwww/src/aitwww.c 2012/03/15 01:59:37 1.3 +++ libaitwww/src/aitwww.c 2012/08/01 00:40:41 1.4 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: aitwww.c,v 1.3 2012/03/15 01:59:37 misho Exp $ +* $Id: aitwww.c,v 1.4 2012/08/01 00:40:41 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); } @@ -131,9 +130,9 @@ www_initCGI(void) } /* 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,7 +145,7 @@ 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(EFAULT, "Unknown request method"); @@ -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); @@ -538,21 +546,28 @@ www_parseMultiPart(const char *str, int ctlen, const c 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); @@ -585,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)); @@ -607,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; } +