/************************************************************************* * (C) 2010 AITNET ltd - Sofia/Bulgaria - * by Michael Pounov * * $Author: misho $ * $Id: url.c,v 1.4 2013/05/30 09:25:35 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, 2013 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_URLInit() - Init URL structure and free old one * * @url = Input URL * return: -1 error or 0 ok */ int www_URLInit(struct tagIOURL * __restrict url) { if (!url) return -1; else memset(url, 0, sizeof(struct tagIOURL)); AIT_INIT_VAL2(&url->url_tech, string); AIT_INIT_VAL2(&url->url_user, string); AIT_INIT_VAL2(&url->url_pass, string); AIT_INIT_VAL2(&url->url_host, string); AIT_INIT_VAL2(&url->url_port, string); AIT_INIT_VAL2(&url->url_path, string); AIT_INIT_VAL2(&url->url_args, string); return 0; } /* * www_URLGet() - Parse and get data from input URL * * @csURL = Input URL line * @url = Output parsed URL * return: 0 error format not find tech:// and return URL like path; * -1 error:: can`t read; >0 ok, up bits for known elements */ int www_URLGet(const char *csURL, struct tagIOURL * __restrict url) { char *pos, *at, *cl, *sl; int ret = 0; if (!url) return -1; else www_URLInit(url); AIT_SET_STR(&url->url_line, csURL); /* unescape URL */ www_unescape(AIT_GET_STR(&url->url_line)); /* Tech */ if (!(pos = strstr(AIT_GET_STR(&url->url_line), "://"))) { AIT_SET_LIKE(&url->url_path, string, AIT_LEN(&url->url_line), AIT_ADDR(&url->url_line)); return ret; } else { AIT_SET_LIKE(&url->url_tech, string, pos - AIT_GET_STR(&url->url_line), AIT_ADDR(&url->url_line)); if (AIT_LEN(&url->url_tech)) ret |= 1; *pos = 0; pos += 3; /* :// */ } /* User */ if ((at = strchr(pos, '@'))) { *at++ = 0; /* Pass */ if ((cl = strchr(pos, ':'))) { *cl++ = 0; AIT_SET_LIKE(&url->url_pass, string, at - cl - 1, cl); if (AIT_LEN(&url->url_pass)) ret |= 4; } else cl = at; AIT_SET_LIKE(&url->url_user, string, cl - pos - 1, pos); if (AIT_LEN(&url->url_user)) ret |= 2; pos = at; } /* Host */ if ((sl = strchr(pos, '/'))) *sl++ = 0; else sl = pos + strlen(pos) + 1; /* Port */ if ((cl = strchr(pos, ':'))) { *cl++ = 0; AIT_SET_LIKE(&url->url_port, string, sl - cl - 1, cl); if (AIT_LEN(&url->url_port)) ret |= 16; } else cl = sl; AIT_SET_LIKE(&url->url_host, string, cl - pos - 1, pos); if (AIT_LEN(&url->url_host)) ret |= 8; pos = sl; /* Args */ if ((at = strchr(pos, '?'))) { *at++ = 0; AIT_SET_LIKE(&url->url_args, string, strlen(at), at); if (AIT_LEN(&url->url_args)) ret |= 64; } else at = pos + strlen(pos) + 1; /* Path */ AIT_SET_LIKE(&url->url_path, string, at - pos - 1, pos); if (AIT_LEN(&url->url_path)) ret |= 32; pos = at + strlen(at); /* Reserved */ url->url_reserved = (u_char*) pos; if (*pos) ret |= 128; return ret; } /* * www_URLGetFile() - Get file from parsed URL * * @url = Input parsed URL * @value = Return filename, if not specified file in url path, replace with / * return: -1 error, 0 filename from path, 1 filename or 2 not specified filename */ int www_URLGetFile(struct tagIOURL * __restrict url, ait_val_t * __restrict value) { char *pos, *psBuf; int ret = 0; if (!url || !value) return -1; psBuf = e_strdup(AIT_GET_STR(&url->url_path)); if (!psBuf) { www_SetErr(elwix_GetErrno(), "%s", elwix_GetError()); return -1; } else AIT_FREE_VAL(value); pos = strrchr(psBuf, '/'); if (!pos) { /* whole string is filename */ pos = psBuf; ret = 1; } else *pos++ = 0; /* If not specified file in path, default replace to / */ if (!*pos) { pos = "/"; ret = 2; } AIT_SET_STR(value, pos); e_free(psBuf); return ret; } /* * www_URLFree() - URL free structure * * @url = Input parsed URL * return: none */ void www_URLFree(struct tagIOURL * __restrict url) { AIT_FREE_VAL(&url->url_tech); AIT_FREE_VAL(&url->url_user); AIT_FREE_VAL(&url->url_pass); AIT_FREE_VAL(&url->url_host); AIT_FREE_VAL(&url->url_port); AIT_FREE_VAL(&url->url_path); AIT_FREE_VAL(&url->url_args); url->url_reserved = NULL; AIT_FREE_VAL(&url->url_line); } /* * www_XMLGet() - Parse and get data from input XML request string * [ns:]container[|attribute[=value]][?data] * * @csXML = Input XML request line * @xml = Output parsed XML request * return: 0 error format incorrect, -1 error:: can`t read; >0 ok readed elements bits */ int www_XMLGet(const char *csXML, struct tagReqXML *xml) { char *pos, *p, *end; int ret = 0; if (!csXML || !xml) return -1; else memset(xml, 0, sizeof *xml); strlcpy((char*) xml->xml_line, csXML, BUFSIZ); /* if namespace present */ if ((pos = strchr((char*) xml->xml_line, ':'))) { xml->xml_namespace.value = (char*) xml->xml_line; xml->xml_namespace.vallen = pos - (char*) xml->xml_line; if (xml->xml_namespace.vallen) ret |= 1; *pos++ = 0; } else pos = (char*) xml->xml_line; /* if container is path */ if (*pos == '/') { xml->xml_node.path.value = pos; xml->xml_node.path.vallen = strlen(pos); if (!xml->xml_node.path.vallen) ret = 0; else ret |= 32; return ret; } else { /* container */ xml->xml_node.container.value = pos; xml->xml_node.container.vallen = strlen(pos); if (!xml->xml_node.container.vallen) return 0; else ret |= 2; } end = strchr(pos, '?'); /* if attribute present */ if (pos && (p = strchr(pos, '|')) && (!end || end > p)) { pos = p; *pos++ = 0; xml->xml_node.container.vallen = strlen(xml->xml_node.container.value); if (!xml->xml_node.container.vallen) return 0; xml->xml_attribute.value = pos; xml->xml_attribute.vallen = strlen(pos); if (xml->xml_attribute.vallen) ret |= 4; } /* if value present */ if (pos && (p = strchr(pos, '=')) && (!end || end > p)) { if (!(ret & 4)) return 0; else pos = p; *pos++ = 0; xml->xml_attribute.vallen = strlen(xml->xml_attribute.value); if (!xml->xml_attribute.vallen) return 0; xml->xml_value.value = pos; xml->xml_value.vallen = strlen(pos); if (xml->xml_value.vallen) ret |= 8; } /* if data present */ if (pos && end) { if (ret < 2) return 0; else pos = end; *pos++ = 0; if (ret & 8) { xml->xml_value.vallen = strlen(xml->xml_value.value); if (!xml->xml_value.vallen) return 0; } else if (ret & 4) { xml->xml_attribute.vallen = strlen(xml->xml_attribute.value); if (!xml->xml_attribute.vallen) return 0; } else if (ret & 2) { xml->xml_node.container.vallen = strlen(xml->xml_node.container.value); if (!xml->xml_node.container.vallen) return 0; } else return 0; xml->xml_data.value = pos; xml->xml_data.vallen = strlen(pos); if (xml->xml_data.vallen) ret |= 16; } return ret; }