--- libaitwww/src/tools.c 2012/03/08 23:40:21 1.1 +++ libaitwww/src/tools.c 2013/05/26 20:30:43 1.4.4.2 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: tools.c,v 1.1 2012/03/08 23:40:21 misho Exp $ +* $Id: tools.c,v 1.4.4.2 2013/05/26 20:30:43 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -12,7 +12,7 @@ terms: All of the documentation and software included in the ELWIX and AITNET Releases is copyrighted by ELWIX - Sofia/Bulgaria -Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 +Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 by Michael Pounov . All rights reserved. Redistribution and use in source and binary forms, with or without @@ -44,9 +44,15 @@ OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF TH SUCH DAMAGE. */ #include "global.h" -#include "tools.h" +/* + * www_cmp() - Compare two string + * + * @ct = content text from www + * @s = string + * return: 0 are equal or !0 are different + */ int www_cmp(const char *ct, const char *s) { @@ -54,12 +60,12 @@ www_cmp(const char *ct, const char *s) assert(ct && s); - while (isspace(*ct)) + while (isspace((int) *ct)) ct++; if (!(sc = strchr(ct, ';'))) sc = strchr(ct, '\x0'); - while (isspace(*(sc - 1))) + while (isspace((int) *(sc - 1))) sc--; if (strlen(s) != sc - ct) @@ -67,6 +73,13 @@ www_cmp(const char *ct, const char *s) return strncasecmp(ct, s, sc - ct); } +/* + * www_cmptype() - Compare context type + * + * @ct = content text from www + * @type = content type + * return: 0 are equal or !0 are different + */ int www_cmptype(const char *ct, const char *type) { @@ -74,7 +87,7 @@ www_cmptype(const char *ct, const char *type) assert(ct && type); - while (isspace(*ct)) + while (isspace((int) *ct)) ct++; if (!(sl = strchr(ct, '/'))) @@ -85,34 +98,46 @@ www_cmptype(const char *ct, const char *type) return strncasecmp(ct, type, sl - ct); } -char * +/* + * www_getpair() - Get AV pair from WWW query string + * + * @str = query string + * @delim = delimiter + * return: NULL error or AV pair, must be e_free() after use! + */ +ait_val_t * www_getpair(char ** __restrict str, const char *delim) { - char *tr, *s = NULL; + char *tr; int cx; + ait_val_t *s; assert(str && *str && delim); - cx = strcspn(*str, delim); - tr = *str + cx; - - s = malloc(cx + 1); + s = ait_allocVar(); if (!s) { - LOGERR; + www_SetErr(elwix_GetErrno(), "%s", elwix_GetError()); return NULL; - } else { - strncpy(s, *str, cx); - s[cx] = 0; } - *str = tr; - if (*str) - (*str)++; + cx = strcspn(*str, delim); + tr = *str + cx; + if (*tr) + *tr++ = 0; + AIT_SET_STR(s, *str); + + *str = tr; return s; } -inline char +/* + * www_x2c() - Hex from string to digit + * + * @str = string + * return: digit + */ +char www_x2c(const char *str) { register char digit; @@ -126,12 +151,19 @@ www_x2c(const char *str) return digit; } -inline void +/* + * www_unescape() - Unescape/decode WWW query string to host string + * + * @str = string + * return: none + */ +void www_unescape(char * __restrict str) { register int i, j; - assert(str); + if (!str) + return; for (i = j = 0; str[j]; i++, j++) { str[i] = str[j]; @@ -145,4 +177,81 @@ www_unescape(char * __restrict str) } str[i] = 0; +} + +/* + * www_undot() - Undotted and clean WWW query filename + * + * @pname = query filename + * return: =NULL error or !=NULL allocated valid filename, after use you must call ait_freeVar() + */ +ait_val_t * +www_undot(const char * __restrict pname) +{ + char *s, *s2, *fname; + int l; + ait_val_t *v; + + if (!pname) + return NULL; + /* check for valid query filename */ + if (*pname != '/') + return NULL; + + v = ait_allocVar(); + if (!v) { + www_SetErr(elwix_GetErrno(), "%s", elwix_GetError()); + return NULL; + } else { + AIT_SET_STR(v, pname + 1); + fname = AIT_GET_STR(v); + } + + /* collapse / sequences */ + if ((s = strstr(fname, "//"))) { + s2 = s + 1; + for (s2 = ++s; *s2 == '/'; s2++); + memmove(s, s2, strlen(s2) + 1); + } + + /* escaped ./ and /./ sequences */ + while (!strncmp(fname, "./", 2)) + memmove(fname, fname + 2, strlen(fname + 1)); + while ((s = strstr(fname, "/./"))) + memmove(s, s + 2, strlen(s + 1)); + + /* alternate between removing leading ../ and removing xxx/../ */ + while (42) { + while (!strncmp(fname, "../", 3)) + memmove(fname, fname + 3, strlen(fname + 2)); + if (!(s = strstr(fname, "/../"))) + break; + for (s2 = s - 1; s2 >= fname && *s2 != '/'; --s2); + memmove(s2 + 1, s + 4, strlen(s + 3)); + } + + /* elide any /.. at the end */ + while ((l = strlen(fname)) > 3 && + !strcmp((s = fname + l - 3), "/..")) { + for (s2 = s - 1; s2 >= fname && *s2 != '/'; --s2); + if (s2 < fname) + break; + *s2 = 0; + } + + /* if filename is empry add current dir */ + if (!*fname) { + AIT_FREE_VAL(v); + AIT_SET_STR(v, "./"); + fname = AIT_GET_STR(v); + } + + /* check for valid filename */ + if (*fname == '/' || (fname[0] == '.' && fname[1] == '.' && + (!fname[2] || fname[2] == '/'))) { + ait_freeVar(&v); + return NULL; + } + + return v; }