/************************************************************************* * (C) 2012 AITNET ltd - Sofia/Bulgaria - * by Michael Pounov * * $Author: misho $ * $Id: aitwww.c,v 1.1 2012/03/08 23:40:21 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" #include "tools.h" #include "mime.h" #pragma GCC visibility push(hidden) int www_Errno; char www_Error[STRSIZ]; #pragma GCC visibility pop // www_GetErrno() Get error code of last operation inline int www_GetErrno() { return www_Errno; } // www_GetError() Get error text of last operation inline const char * www_GetError() { return www_Error; } // www_SetErr() Set error to variables for internal use!!! inline void www_SetErr(int eno, char *estr, ...) { va_list lst; www_Errno = eno; memset(www_Error, 0, STRSIZ); va_start(lst, estr); vsnprintf(www_Error, STRSIZ, estr, lst); va_end(lst); } /* -------------------------------------------------------------- */ /* * www_initCGI() - Init CGI program * * return: NULL error or allocated cgi session */ cgi_t * www_initCGI(void) { char *s, *str; int ctlen, rlen; register int i; cgi_t *cgi = NULL; str = getenv("REQUEST_METHOD"); if (!str) { www_SetErr(EBADMSG, "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"); return NULL; } cgi = www_parseQuery(str); } else if (!strcmp(str, "POST")) { /* POST */ str = getenv("CONTENT_LENGTH"); if (!str) { www_SetErr(EBADMSG, "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"); return NULL; } if (www_cmp(s, "multipart/form-data") && www_cmp(s, "application/x-www-form-urlencoded")) { www_SetErr(EBADMSG, "MIME parts are broken"); return NULL; } /* allocated space for post data */ str = malloc(ctlen + 1); if (!str) { LOGERR; return NULL; } for (i = 0; i < ctlen && (rlen = read(STDIN_FILENO, (void*) str + i, ctlen - i)) > 0; i += rlen); str[ctlen] = 0; if (!www_cmp(s, "application/x-www-form-urlencoded")) cgi = www_parseQuery(str); else if (!www_cmp(s, "multipart/form-data")) cgi = www_parseMultiPart(str, ctlen, s); free(str); } else { /* Unknown method */ www_SetErr(EBADMSG, "Unknown request method"); return NULL; } return cgi; } /* * www_closeCGI() - Close and free all CGI resources * * @cgi = Inited cgi session * return: none */ void www_closeCGI(cgi_t ** __restrict cgi) { struct tagCGI *t; if (!cgi || !*cgi) return; while ((t = SLIST_FIRST(*cgi))) { if (t->cgi_name) free(t->cgi_name); if (t->cgi_value) free(t->cgi_value); SLIST_REMOVE_HEAD(*cgi, cgi_node); free(t); } free(*cgi); *cgi = NULL; } /* * www_parseQuery() - Parse CGI query string * * @str = String * return: NULL error or allocated cgi session */ cgi_t * www_parseQuery(const char *str) { char *base, *wrk; cgi_t *cgi; struct tagCGI *t, *old = NULL; if (!str) { www_SetErr(EINVAL, "String is NULL"); return NULL; } cgi = malloc(sizeof(cgi_t)); if (!cgi) { LOGERR; return NULL; } else { memset(cgi, 0, sizeof(cgi_t)); SLIST_INIT(cgi); } base = wrk = strdup(str); while (*wrk) { t = malloc(sizeof(struct tagCGI)); if (!t) { LOGERR; www_closeCGI(&cgi); return NULL; } else memset(t, 0, sizeof(struct tagCGI)); t->cgi_name = www_getpair(&wrk, "="); www_unescape(t->cgi_name); t->cgi_value = www_getpair(&wrk, "&;"); www_unescape(t->cgi_value); if (!old) SLIST_INSERT_HEAD(cgi, t, cgi_node); else SLIST_INSERT_AFTER(old, t, cgi_node); old = t; } free(base); return 0; } /* * www_getValue() - Get Value from CGI session * * @cgi = Inited cgi session * @name = Name of cgi variable * return: NULL not found or !=NULL value */ inline const char * www_getValue(cgi_t * __restrict cgi, const char *name) { struct tagCGI *t; if (!cgi || !name) { www_SetErr(EINVAL, "Invalid argument(s)"); return NULL; } SLIST_FOREACH(t, cgi, cgi_node) if (t->cgi_name && !strcmp(name, t->cgi_name)) break; return t->cgi_value; } /* * www_addValue() - Add new or update if exists CGI variable * * @cgi = Inited cgi session * @name = Name of cgi variable * @value = Value of cgi variable * return: -1 error, 0 add new one or 1 updated variable */ int www_addValue(cgi_t * __restrict cgi, const char *name, const char *value) { struct tagCGI *t, *tmp; if (!cgi || !name) { www_SetErr(EINVAL, "Invalid argument(s)"); return -1; } /* 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); /* update */ return 1; } /* save last cgi pair */ if (!tmp) break; } /* add new one */ tmp = malloc(sizeof(struct tagCGI)); if (!tmp) { LOGERR; return -1; } else memset(tmp, 0, sizeof(struct tagCGI)); tmp->cgi_name = strdup(name); if (value) tmp->cgi_value = strdup(value); SLIST_INSERT_AFTER(t, tmp, cgi_node); return 0; } /* * www_delPair() - Delete CGI variable from session * * @cgi = Inited cgi session * @name = Name of cgi variable * return: -1 error, 0 not found or 1 deleted ok */ int www_delPair(cgi_t * __restrict cgi, const char *name) { struct tagCGI *t; if (!cgi || !name) { www_SetErr(EINVAL, "Invalid argument(s)"); return -1; } /* search for delete */ SLIST_FOREACH(t, cgi, cgi_node) if (t->cgi_name && !strcmp(name, t->cgi_name)) { SLIST_REMOVE(cgi, t, tagCGI, cgi_node); return 1; } return 0; } /* * www_header() - Output initial html header * * @output = file handle * return: <1 error or >0 writed bytes */ inline int www_header(FILE *output) { FILE *f = output ? output : stdout; return fputs("Content-type: text/html\n\n", f); } static char * quotStr(const char *str, const char **end) { char *s, *e; int n, len = 0; register int i; /* get str w/o " */ if (*str != '"') { n = strspn(str, "!#$%&'*+-.0123456789?ABCDEFGHIJKLMNOPQRSTUVWXYZ" "^_`abcdefghijklmnopqrstuvwxyz{|}~"); s = malloc(n + 1); if (!s) { LOGERR; return NULL; } else { strncpy(s, str, n); s[n] = 0; *end = str + n; return s; } } else str++; /* get quoted string */ if (!(e = strchr(str, '"'))) return NULL; else len = e - str; s = malloc(len + 1); if (!s) { LOGERR; return NULL; } for (i = 0; i < len; i++, str++) { if (*str == '\\' || *str == '\n') s[i] = *++str; else if (*str == '"') break; else s[i] = *str; } s[i] = 0; *end = ++str; return s; } static struct tagCGI * addAttr(const char **ct) { struct tagCGI *a; const char *c, *eq; char *name, *value; if (!*ct || !(c = strchr(*ct, ';'))) return NULL; else c++; while (isspace(*c)) c++; if (!(eq = strchr(c, '='))) return NULL; /* parse name */ name = malloc(eq - c + 1); if (!name) { LOGERR; return NULL; } else { strncpy(name, c, eq - c); name[eq - c] = 0; } /* parse value */ value = quotStr(++eq, &c); if (!value) { free(name); return NULL; } /* fill tagCGI */ a = malloc(sizeof(struct tagCGI)); if (!a) { LOGERR; return NULL; } else { a->cgi_name = name; a->cgi_value = value; *ct = c; } return a; } /* * www_parseMultiPart() - Parse Multi part POST CGI query string * * @str = String * @ctlen = Content length * @ct = Content type * return: NULL error or allocated cgi session */ cgi_t * www_parseMultiPart(const char *str, int ctlen, const char *ct) { cgi_t *cgi, *attr; mime_t *mime = NULL; struct tagMIME *m; struct tagCGI *t, *old = NULL; const char *s; int len; if (!str) { www_SetErr(EINVAL, "String is NULL"); return NULL; } cgi = malloc(sizeof(cgi_t)); if (!cgi) { LOGERR; return NULL; } else { memset(cgi, 0, sizeof(cgi_t)); SLIST_INIT(cgi); } /* parse MIME messages */ attr = www_parseAttributes(&ct); if (!attr) { www_closeCGI(&cgi); return NULL; } mime = mime_parseMultiPart(str, ctlen, www_getAttribute(attr, "boundary"), NULL); www_freeAttributes(&attr); SLIST_FOREACH(m, mime, mime_node) { s = mime_getValue(m, "content-disposition"); attr = www_parseAttributes(&s); t = malloc(sizeof(struct tagCGI)); if (!t) { LOGERR; mime_close(&mime); www_closeCGI(&cgi); return NULL; } else memset(t, 0, sizeof(struct tagCGI)); t->cgi_name = strdup(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; } www_freeAttributes(&attr); if (!old) SLIST_INSERT_HEAD(cgi, t, cgi_node); else SLIST_INSERT_AFTER(old, t, cgi_node); old = t; } mime_close(&mime); return cgi; } /* * www_parseAttributes() - Parse attributes * * @ct = Content type * return: NULL error or !=NULL attributes */ inline cgi_t * www_parseAttributes(const char **ct) { struct tagCGI *t, *old = NULL; cgi_t *attr = NULL; if (!ct) { www_SetErr(EINVAL, "String is NULL"); return NULL; } attr = malloc(sizeof(cgi_t)); if (!attr) { LOGERR; return NULL; } else { memset(attr, 0, sizeof(cgi_t)); SLIST_INIT(attr); } /* get mime attributes */ while ((t = addAttr(ct))) { if (!old) SLIST_INSERT_HEAD(attr, t, cgi_node); else SLIST_INSERT_AFTER(old, t, cgi_node); old = t; } return attr; } /* * www_freeAttributes() - Free attributes * * @attr = Attributes * return: none */ inline void www_freeAttributes(cgi_t ** __restrict attr) { 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); } 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)) return t->cgi_value; return NULL; }