File:  [ELWIX - Embedded LightWeight unIX -] / libaitwww / src / url.c
Revision 1.2: download - view: text, annotated - select for diffs - revision graph
Thu Mar 15 01:59:37 2012 UTC (12 years, 3 months ago) by misho
Branches: MAIN
CVS tags: www1_4, www1_3, www1_2, WWW1_3, WWW1_2, WWW1_1, HEAD
version 1.1

    1: /*************************************************************************
    2: * (C) 2010 AITNET ltd - Sofia/Bulgaria - <misho@aitnet.org>
    3: *  by Michael Pounov <misho@elwix.org>
    4: *
    5: * $Author: misho $
    6: * $Id: url.c,v 1.2 2012/03/15 01:59:37 misho Exp $
    7: *
    8: **************************************************************************
    9: The ELWIX and AITNET software is distributed under the following
   10: terms:
   11: 
   12: All of the documentation and software included in the ELWIX and AITNET
   13: Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>
   14: 
   15: Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
   16: 	by Michael Pounov <misho@elwix.org>.  All rights reserved.
   17: 
   18: Redistribution and use in source and binary forms, with or without
   19: modification, are permitted provided that the following conditions
   20: are met:
   21: 1. Redistributions of source code must retain the above copyright
   22:    notice, this list of conditions and the following disclaimer.
   23: 2. Redistributions in binary form must reproduce the above copyright
   24:    notice, this list of conditions and the following disclaimer in the
   25:    documentation and/or other materials provided with the distribution.
   26: 3. All advertising materials mentioning features or use of this software
   27:    must display the following acknowledgement:
   28: This product includes software developed by Michael Pounov <misho@elwix.org>
   29: ELWIX - Embedded LightWeight unIX and its contributors.
   30: 4. Neither the name of AITNET nor the names of its contributors
   31:    may be used to endorse or promote products derived from this software
   32:    without specific prior written permission.
   33: 
   34: THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND
   35: ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   36: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   37: ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   38: FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   39: DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   40: OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   41: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   42: LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   43: OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   44: SUCH DAMAGE.
   45: */
   46: #include "global.h"
   47: 
   48: 
   49: /*
   50:  * www_URLGet() - Parse and get data from input URL
   51:  *
   52:  * @csURL = Input URL line
   53:  * @url = Output parsed URL
   54:  * return: 0 error format not find tech:// and return URL like path; 
   55:  * 		-1 error:: can`t read; >0 ok, up bits for known elements
   56:  */
   57: int
   58: www_URLGet(const char *csURL, struct tagIOURL *url)
   59: {
   60: 	char *pos, *at, *cl, *sl;
   61: 	int ret = 0;
   62: 
   63: 	if (!url)
   64: 		return -1;
   65: 	else
   66: 		memset(url, 0, sizeof(*url));
   67: 
   68: 	strlcpy((char*) url->url_line, csURL, BUFSIZ);
   69: 	/* Tech */
   70: 	if (!(pos = strstr((char*) url->url_line, "://"))) {
   71: 		url->url_path.vallen = strlen((char*) url->url_line);
   72: 		url->url_path.value = (char*) url->url_line;
   73: 		return ret;
   74: 	} else {
   75: 		url->url_tech.value = (char*) url->url_line;
   76: 		url->url_tech.vallen = pos - (char*) url->url_line;
   77: 		if (url->url_tech.vallen)
   78: 			ret |= 1;
   79: 
   80: 		*pos = 0;
   81: 		pos += 3;
   82: 	}
   83: 
   84: 	/* User */
   85: 	if ((at = strchr(pos, '@'))) {
   86: 		*at++ = 0;
   87: 		/* Pass */
   88: 		if ((cl = strchr(pos, ':'))) {
   89: 			*cl++ = 0;
   90: 
   91: 			url->url_pass.value = cl;
   92: 			url->url_pass.vallen = at - cl - 1;
   93: 			if (url->url_pass.vallen)
   94: 				ret |= 4;
   95: 		} else
   96: 			cl = at;
   97: 
   98: 		url->url_user.value = pos;
   99: 		url->url_user.vallen = cl - pos - 1;
  100: 		if (url->url_user.vallen)
  101: 			ret |= 2;
  102: 
  103: 		pos = at;
  104: 	}
  105: 
  106: 	/* Host */
  107: 	if ((sl = strchr(pos, '/')))
  108: 		*sl++ = 0;
  109: 	else
  110: 		sl = pos + strlen(pos) + 1;
  111: 	/* Port */
  112: 	if ((cl = strchr(pos, ':'))) {
  113: 		*cl++ = 0;
  114: 
  115: 		url->url_port.value = cl;
  116: 		url->url_port.vallen = sl - cl - 1;
  117: 		if (url->url_port.vallen)
  118: 			ret |= 16;
  119: 	} else
  120: 		cl = sl;
  121: 
  122: 	url->url_host.value = pos;
  123: 	url->url_host.vallen = cl - pos - 1;
  124: 	if (url->url_host.vallen)
  125: 		ret |= 8;
  126: 
  127: 	pos = sl;
  128: 
  129: 	/* Args */
  130: 	if ((at = strchr(pos, '?'))) {
  131: 		*at++ = 0;
  132: 
  133: 		url->url_args.value = at;
  134: 		url->url_args.vallen = strlen(at);
  135: 		if (url->url_args.vallen)
  136: 			ret |= 64;
  137: 	} else
  138: 		at = pos + strlen(pos) + 1;
  139: 
  140: 	/* Path */
  141: 	url->url_path.value = pos;
  142: 	url->url_path.vallen = at - pos - 1;
  143: 	if (url->url_path.vallen)
  144: 		ret |= 32;
  145: 
  146: 	pos = at + strlen(at);
  147: 
  148: 	/* Reserved */
  149: 	url->url_reserved = pos;
  150: 	if (*pos)
  151: 		ret |= 128;
  152: 
  153: 	return ret;
  154: }
  155: 
  156: /*
  157:  * www_URLGetFile() - Get file from parsed URL
  158:  *
  159:  * @url = Input parsed URL
  160:  * @psValue = Return filename, if not specified file in url path, replace with /
  161:  * @valLen = Size of psValue array
  162:  * return: -1 error:: can`t read; 0 ok
  163:  */
  164: int
  165: www_URLGetFile(struct tagIOURL *url, char * __restrict psValue, int valLen)
  166: {
  167: 	char *pos, *psBuf;
  168: 
  169: 	if (!url || !psValue || !valLen)
  170: 		return -1;
  171: 
  172: 	psBuf = strdup(url->url_path.value);
  173: 	if (!psBuf) {
  174: 		LOGERR;
  175: 		return -1;
  176: 	}
  177: 
  178: 	pos = strrchr(psBuf, '/');
  179: 	if (!pos) {
  180: 		/* whole string is filename */
  181: 		strlcpy(psValue, psBuf, valLen);
  182: 
  183: 		free(psBuf);
  184: 		return 1;
  185: 	} else {
  186: 		*pos++ = 0;
  187: 
  188: 		strlcpy(psValue, pos, valLen);
  189: 		free(psBuf);
  190: 	}
  191: 
  192: 	/* If not specified file in path, default replace to / */
  193: 	if (!*psValue)
  194: 		strlcpy(psValue, "/", valLen);
  195: 	return 0;
  196: }
  197: 
  198: /*
  199:  * www_XMLGet() - Parse and get data from input XML request string
  200:  * 				[ns:]container[|attribute[=value]][?data]
  201:  *
  202:  * @csXML = Input XML request line
  203:  * @xml = Output parsed XML request
  204:  * return: 0 error format incorrect, -1 error:: can`t read; >0 ok readed elements bits
  205:  */
  206: int
  207: www_XMLGet(const char *csXML, struct tagReqXML *xml)
  208: {
  209: 	char *pos, *p, *end;
  210: 	int ret = 0;
  211: 
  212: 	if (!csXML || !xml)
  213: 		return -1;
  214: 	else
  215: 		memset(xml, 0, sizeof *xml);
  216: 
  217: 	strlcpy((char*) xml->xml_line, csXML, BUFSIZ);
  218: 	/* if namespace present */
  219: 	if ((pos = strchr((char*) xml->xml_line, ':'))) {
  220: 		xml->xml_namespace.value = (char*) xml->xml_line;
  221: 		xml->xml_namespace.vallen = pos - (char*) xml->xml_line;
  222: 		if (xml->xml_namespace.vallen)
  223: 			ret |= 1;
  224: 		*pos++ = 0;
  225: 	} else
  226: 		pos = (char*) xml->xml_line;
  227: 	/* if container is path */
  228: 	if (*pos == '/') {
  229: 		xml->xml_node.path.value = pos;
  230: 		xml->xml_node.path.vallen = strlen(pos);
  231: 		if (!xml->xml_node.path.vallen)
  232: 			ret = 0;
  233: 		else
  234: 			ret |= 32;
  235: 		return ret;
  236: 	} else {
  237: 		/* container */
  238: 		xml->xml_node.container.value = pos;
  239: 		xml->xml_node.container.vallen = strlen(pos);
  240: 		if (!xml->xml_node.container.vallen)
  241: 			return 0;
  242: 		else
  243: 			ret |= 2;
  244: 	}
  245: 	end = strchr(pos, '?');
  246: 	/* if attribute present */
  247: 	if (pos && (p = strchr(pos, '|')) && (!end || end > p)) {
  248: 		pos = p;
  249: 		*pos++ = 0;
  250: 		xml->xml_node.container.vallen = strlen(xml->xml_node.container.value);
  251: 		if (!xml->xml_node.container.vallen)
  252: 			return 0;
  253: 
  254: 		xml->xml_attribute.value = pos;
  255: 		xml->xml_attribute.vallen = strlen(pos);
  256: 		if (xml->xml_attribute.vallen)
  257: 			ret |= 4;
  258: 	}
  259: 	/* if value present */
  260: 	if (pos && (p = strchr(pos, '=')) && (!end || end > p)) {
  261: 		if (!(ret & 4))
  262: 			return 0;
  263: 		else
  264: 			pos = p;
  265: 		*pos++ = 0;
  266: 		xml->xml_attribute.vallen = strlen(xml->xml_attribute.value);
  267: 		if (!xml->xml_attribute.vallen)
  268: 			return 0;
  269: 
  270: 		xml->xml_value.value = pos;
  271: 		xml->xml_value.vallen = strlen(pos);
  272: 		if (xml->xml_value.vallen)
  273: 			ret |= 8;
  274: 	}
  275: 	/* if data present */
  276: 	if (pos && end) {
  277: 		if (ret < 2)
  278: 			return 0;
  279: 		else
  280: 			pos = end;
  281: 		*pos++ = 0;
  282: 		if (ret & 8) {
  283: 			xml->xml_value.vallen = strlen(xml->xml_value.value);
  284: 			if (!xml->xml_value.vallen)
  285: 				return 0;
  286: 		} else if (ret & 4) {
  287: 			xml->xml_attribute.vallen = strlen(xml->xml_attribute.value);
  288: 			if (!xml->xml_attribute.vallen)
  289: 				return 0;
  290: 		} else if (ret & 2) {
  291: 			xml->xml_node.container.vallen = strlen(xml->xml_node.container.value);
  292: 			if (!xml->xml_node.container.vallen)
  293: 				return 0;
  294: 		} else
  295: 			return 0;
  296: 
  297: 		xml->xml_data.value = pos;
  298: 		xml->xml_data.vallen = strlen(pos);
  299: 		if (xml->xml_data.vallen)
  300: 			ret |= 16;
  301: 	}
  302: 
  303: 	return ret;
  304: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>