/************************************************************************* * (C) 2012 AITNET ltd - Sofia/Bulgaria - * by Michael Pounov * * $Author: misho $ * $Id: tools.c,v 1.4 2012/09/20 14:19:45 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following 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 by Michael Pounov . All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. All advertising materials mentioning features or use of this software must display the following acknowledgement: This product includes software developed by Michael Pounov ELWIX - Embedded LightWeight unIX and its contributors. 4. Neither the name of AITNET nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include "global.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) { char *sc; assert(ct && s); while (isspace((int) *ct)) ct++; if (!(sc = strchr(ct, ';'))) sc = strchr(ct, '\x0'); while (isspace((int) *(sc - 1))) sc--; if (strlen(s) != sc - ct) return -1; 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) { char *sl; assert(ct && type); while (isspace((int) *ct)) ct++; if (!(sl = strchr(ct, '/'))) return -1; if (strlen(type) != sl - ct) return 1; return strncasecmp(ct, type, sl - ct); } /* * www_getpair() - Get AV pair from WWW query string * * @str = query string * @delim = delimiter * return: NULL error or AV pair, must be io_free() after use! */ ait_val_t * www_getpair(char ** __restrict str, const char *delim) { char *tr; int cx; ait_val_t *s; assert(str && *str && delim); s = io_allocVar(); if (!s) { www_SetErr(io_GetErrno(), "%s", io_GetError()); return NULL; } cx = strcspn(*str, delim); tr = *str + cx; if (*tr) *tr++ = 0; AIT_SET_STR(s, *str); *str = tr; return s; } /* * www_x2c() - Hex from string to digit * * @str = string * return: digit */ inline char www_x2c(const char *str) { register char digit; assert(str); digit = (str[0] >= 'A' ? ((str[0] & 0xdf) - 'A') + 10 : (str[0] - '0')); digit *= 16; digit += (str[1] >= 'A' ? ((str[1] & 0xdf) - 'A') + 10 : (str[1] - '0')); return digit; } /* * www_unescape() - Unescape/decode WWW query string to host string * * @str = string * return: none */ inline void www_unescape(char * __restrict str) { register int i, j; if (!str) return; for (i = j = 0; str[j]; i++, j++) { str[i] = str[j]; if (str[j] == '+') str[i] = ' '; else if (str[j] == '%') { str[i] = www_x2c(&str[j + 1]); j += 2; } } 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 io_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 = io_allocVar(); if (!v) { www_SetErr(io_GetErrno(), "%s", io_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] == '/'))) { io_freeVar(&v); return NULL; } return v; }