File:  [ELWIX - Embedded LightWeight unIX -] / libaitio / src / Attic / url.c
Revision 1.6: download - view: text, annotated - select for diffs - revision graph
Wed Apr 20 22:56:32 2011 UTC (13 years, 2 months ago) by misho
Branches: MAIN
CVS tags: io2_4, io2_3, io2_2, io2_1, io2_0, io1_9, io1_8, IO2_3, IO2_2, IO2_1, IO2_0, IO1_9, IO1_8, IO1_7, HEAD
Release 1.7

/*************************************************************************
* (C) 2010 AITNET ltd - Sofia/Bulgaria - <misho@aitnet.org>
*  by Michael Pounov <misho@elwix.org>
*
* $Author: misho $
* $Id: url.c,v 1.6 2011/04/20 22:56:32 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 <info@elwix.org>

Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
	by Michael Pounov <misho@elwix.org>.  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 <misho@elwix.org>
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"


/*
 * ioURLGet() 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
ioURLGet(const char *csURL, struct tagIOURL *url)
{
	char *pos, *at, *cl, *sl;
	int ret = 0;

	if (!url)
		return -1;
	else
		memset(url, 0, sizeof(*url));

	strlcpy((char*) url->url_line, csURL, BUFSIZ);
	// Tech
	if (!(pos = strstr((char*) url->url_line, "://"))) {
		url->url_path.vallen = strlen((char*) url->url_line);
		url->url_path.value = (char*) url->url_line;
		return ret;
	} else {
		url->url_tech.value = (char*) url->url_line;
		url->url_tech.vallen = pos - (char*) url->url_line;
		if (url->url_tech.vallen)
			ret |= 1;

		*pos = 0;
		pos += 3;
	}

	// User
	if ((at = strchr(pos, '@'))) {
		*at++ = 0;
		// Pass
		if ((cl = strchr(pos, ':'))) {
			*cl++ = 0;

			url->url_pass.value = cl;
			url->url_pass.vallen = at - cl - 1;
			if (url->url_pass.vallen)
				ret |= 4;
		} else
			cl = at;

		url->url_user.value = pos;
		url->url_user.vallen = cl - pos - 1;
		if (url->url_user.vallen)
			ret |= 2;

		pos = at;
	}

	// Host
	if ((sl = strchr(pos, '/')))
		*sl++ = 0;
	else
		sl = pos + strlen(pos) + 1;
	// Port
	if ((cl = strchr(pos, ':'))) {
		*cl++ = 0;

		url->url_port.value = cl;
		url->url_port.vallen = sl - cl - 1;
		if (url->url_port.vallen)
			ret |= 16;
	} else
		cl = sl;

	url->url_host.value = pos;
	url->url_host.vallen = cl - pos - 1;
	if (url->url_host.vallen)
		ret |= 8;

	pos = sl;

	// Args
	if ((at = strchr(pos, '?'))) {
		*at++ = 0;

		url->url_args.value = at;
		url->url_args.vallen = strlen(at);
		if (url->url_args.vallen)
			ret |= 64;
	} else
		at = pos + strlen(pos) + 1;

	// Path
	url->url_path.value = pos;
	url->url_path.vallen = at - pos - 1;
	if (url->url_path.vallen)
		ret |= 32;

	pos = at + strlen(at);

	// Reserved
	url->url_reserved = pos;
	if (*pos)
		ret |= 128;

	return ret;
}

/*
 * io_Path2File() Parse and make path/filename pair
 * @csArgs = Input argument line
 * @psPath = Output Path, if ==NULL path not returned
 * @pathLen = Size of path array
 * @psFile = Output File
 * @fileLen = Size of file array
 * return: 0 error format; -1 error:: can`t read; >0 ok, number of readed items
*/
inline int
io_Path2File(const char * __restrict csArgs, char * __restrict psPath, int pathLen, 
		char * __restrict psFile, int fileLen)
{
	char *pos, *psBuf;

	if (!csArgs || !psFile || !fileLen)
		return -1;
	if (psPath && !pathLen)
		return -1;
	else
		memset(psPath, 0, pathLen);
	psBuf = strdup(csArgs);
	if (!psBuf) {
		LOGERR;
		return -1;
	}

	pos = strrchr(psBuf, '/');
	if (!pos) {
		strlcpy(psFile, psBuf, fileLen);

		free(psBuf);
		return 1;
	} else
		*pos++ = 0;

	strlcpy(psFile, pos, fileLen);
	if (psPath)
		strlcpy(psPath, psBuf, pathLen);

	free(psBuf);
	return 2;
}

/*
 * ioURLGetValue() Get value from parsed URL
 * @url = Input parsed URL
 * @csAttr = Attribute for search
 * @psValue = Return value of attribute, if ==NULL only check for existence of attribute
 * @valLen = Size of psValue array
 * return: 0 error attribute not find; -1 error:: can`t read; >0 ok, find at position
*/
int
ioURLGetValue(struct tagIOURL *url, const char *csAttr, char * __restrict psValue, int valLen)
{
	register int i, ret = 0;
	char szBuf[BUFSIZ], szElem[2][BUFSIZ];
	int len;
	array_t *items;

	if (!url || !csAttr)
		return -1;

	strlcpy(szBuf, url->url_args.value, BUFSIZ);
	if ((len = io_arrayMake(szBuf, 0, "&", &items)) < 1)
		return ret;

	for (i = 0; i < len && items->arr_data[i]; i++) {
		if (io_MakeAV(items->arr_data[i], "=", szElem[0], BUFSIZ, szElem[1], BUFSIZ) < 1)
			continue;

		if (!strcmp(szElem[0], csAttr)) {
			ret = i + 1;
			if (psValue && valLen)
				strlcpy(psValue, szElem[1], valLen);
			break;
		}
	}

	io_arrayDestroy(&items);
	return ret;
}

/*
 * ioURLGetFile() Get file from parsed URL
 * @url = Input parsed URL
 * @psValue = Return filename, if not specified file in url path, replace with /
 * @valLen = Size of psValue array
 * return: -1 error:: can`t read; 0 ok
*/
int
ioURLGetFile(struct tagIOURL *url, char * __restrict psValue, int valLen)
{
	if (!url || !psValue || !valLen)
		return -1;

	if (io_Path2File(url->url_path.value, NULL, 0, psValue, valLen) < 1)
		return -1;

	// If not specified file in path, default replace to /
	if (!*psValue)
		strlcpy(psValue, "/", valLen);
	return 0;
}

// ------------------------------------------

/*
 * ioXMLGet() 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
ioXMLGet(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;
}

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