Annotation of libaitwww/src/url.c, revision 1.1.2.1
1.1.2.1 ! misho 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.6 2011/04/20 22:56:32 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_Path2File() - Parse and make path/filename pair
! 158: *
! 159: * @csArgs = Input argument line
! 160: * @psPath = Output Path, if ==NULL path not returned
! 161: * @pathLen = Size of path array
! 162: * @psFile = Output File
! 163: * @fileLen = Size of file array
! 164: * return: 0 error format; -1 error:: can`t read; >0 ok, number of readed items
! 165: */
! 166: inline int
! 167: www_Path2File(const char * __restrict csArgs, char * __restrict psPath,
! 168: int pathLen, char * __restrict psFile, int fileLen)
! 169: {
! 170: char *pos, *psBuf;
! 171:
! 172: if (!csArgs || !psFile || !fileLen)
! 173: return -1;
! 174: if (psPath && !pathLen)
! 175: return -1;
! 176: else
! 177: memset(psPath, 0, pathLen);
! 178: psBuf = strdup(csArgs);
! 179: if (!psBuf) {
! 180: LOGERR;
! 181: return -1;
! 182: }
! 183:
! 184: pos = strrchr(psBuf, '/');
! 185: if (!pos) {
! 186: strlcpy(psFile, psBuf, fileLen);
! 187:
! 188: free(psBuf);
! 189: return 1;
! 190: } else
! 191: *pos++ = 0;
! 192:
! 193: strlcpy(psFile, pos, fileLen);
! 194: if (psPath)
! 195: strlcpy(psPath, psBuf, pathLen);
! 196:
! 197: free(psBuf);
! 198: return 2;
! 199: }
! 200:
! 201: /*
! 202: * www_URLGetFile() - Get file from parsed URL
! 203: *
! 204: * @url = Input parsed URL
! 205: * @psValue = Return filename, if not specified file in url path, replace with /
! 206: * @valLen = Size of psValue array
! 207: * return: -1 error:: can`t read; 0 ok
! 208: */
! 209: int
! 210: www_URLGetFile(struct tagIOURL *url, char * __restrict psValue, int valLen)
! 211: {
! 212: if (!url || !psValue || !valLen)
! 213: return -1;
! 214:
! 215: if (www_Path2File(url->url_path.value, NULL, 0, psValue, valLen) < 1)
! 216: return -1;
! 217:
! 218: /* If not specified file in path, default replace to / */
! 219: if (!*psValue)
! 220: strlcpy(psValue, "/", valLen);
! 221: return 0;
! 222: }
! 223:
! 224: /*
! 225: * www_XMLGet() - Parse and get data from input XML request string
! 226: * [ns:]container[|attribute[=value]][?data]
! 227: *
! 228: * @csXML = Input XML request line
! 229: * @xml = Output parsed XML request
! 230: * return: 0 error format incorrect, -1 error:: can`t read; >0 ok readed elements bits
! 231: */
! 232: int
! 233: www_XMLGet(const char *csXML, struct tagReqXML *xml)
! 234: {
! 235: char *pos, *p, *end;
! 236: int ret = 0;
! 237:
! 238: if (!csXML || !xml)
! 239: return -1;
! 240: else
! 241: memset(xml, 0, sizeof *xml);
! 242:
! 243: strlcpy((char*) xml->xml_line, csXML, BUFSIZ);
! 244: /* if namespace present */
! 245: if ((pos = strchr((char*) xml->xml_line, ':'))) {
! 246: xml->xml_namespace.value = (char*) xml->xml_line;
! 247: xml->xml_namespace.vallen = pos - (char*) xml->xml_line;
! 248: if (xml->xml_namespace.vallen)
! 249: ret |= 1;
! 250: *pos++ = 0;
! 251: } else
! 252: pos = (char*) xml->xml_line;
! 253: /* if container is path */
! 254: if (*pos == '/') {
! 255: xml->xml_node.path.value = pos;
! 256: xml->xml_node.path.vallen = strlen(pos);
! 257: if (!xml->xml_node.path.vallen)
! 258: ret = 0;
! 259: else
! 260: ret |= 32;
! 261: return ret;
! 262: } else {
! 263: /* container */
! 264: xml->xml_node.container.value = pos;
! 265: xml->xml_node.container.vallen = strlen(pos);
! 266: if (!xml->xml_node.container.vallen)
! 267: return 0;
! 268: else
! 269: ret |= 2;
! 270: }
! 271: end = strchr(pos, '?');
! 272: /* if attribute present */
! 273: if (pos && (p = strchr(pos, '|')) && (!end || end > p)) {
! 274: pos = p;
! 275: *pos++ = 0;
! 276: xml->xml_node.container.vallen = strlen(xml->xml_node.container.value);
! 277: if (!xml->xml_node.container.vallen)
! 278: return 0;
! 279:
! 280: xml->xml_attribute.value = pos;
! 281: xml->xml_attribute.vallen = strlen(pos);
! 282: if (xml->xml_attribute.vallen)
! 283: ret |= 4;
! 284: }
! 285: /* if value present */
! 286: if (pos && (p = strchr(pos, '=')) && (!end || end > p)) {
! 287: if (!(ret & 4))
! 288: return 0;
! 289: else
! 290: pos = p;
! 291: *pos++ = 0;
! 292: xml->xml_attribute.vallen = strlen(xml->xml_attribute.value);
! 293: if (!xml->xml_attribute.vallen)
! 294: return 0;
! 295:
! 296: xml->xml_value.value = pos;
! 297: xml->xml_value.vallen = strlen(pos);
! 298: if (xml->xml_value.vallen)
! 299: ret |= 8;
! 300: }
! 301: /* if data present */
! 302: if (pos && end) {
! 303: if (ret < 2)
! 304: return 0;
! 305: else
! 306: pos = end;
! 307: *pos++ = 0;
! 308: if (ret & 8) {
! 309: xml->xml_value.vallen = strlen(xml->xml_value.value);
! 310: if (!xml->xml_value.vallen)
! 311: return 0;
! 312: } else if (ret & 4) {
! 313: xml->xml_attribute.vallen = strlen(xml->xml_attribute.value);
! 314: if (!xml->xml_attribute.vallen)
! 315: return 0;
! 316: } else if (ret & 2) {
! 317: xml->xml_node.container.vallen = strlen(xml->xml_node.container.value);
! 318: if (!xml->xml_node.container.vallen)
! 319: return 0;
! 320: } else
! 321: return 0;
! 322:
! 323: xml->xml_data.value = pos;
! 324: xml->xml_data.vallen = strlen(pos);
! 325: if (xml->xml_data.vallen)
! 326: ret |= 16;
! 327: }
! 328:
! 329: return ret;
! 330: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>