Annotation of libaitio/src/url.c, revision 1.1.2.4
1.1.2.1 misho 1: /*************************************************************************
2: * (C) 2010 AITNET ltd - Sofia/Bulgaria - <misho@aitbg.com>
3: * by Michael Pounov <misho@openbsd-bg.org>
4: *
5: * $Author: misho $
1.1.2.4 ! misho 6: * $Id: url.c,v 1.1.2.3 2010/03/04 08:10:57 misho Exp $
1.1.2.1 misho 7: *
8: *************************************************************************/
9: #include "global.h"
10:
11:
12: /*
13: * ioURLGet() Parse and get data from input URL
14: * @csURL = Input URL line
15: * @url = Output parsed URL
16: * return: 0 error format not find tech:// and return URL like path;
17: -1 error:: can`t read; >0 ok, up bits for known elements
18: */
19: int ioURLGet(const char *csURL, struct tagIOURL *url)
20: {
21: char *pos, *at, *cl, *sl;
22: int ret = 0;
23:
24: if (!url)
25: return -1;
26: else
27: memset(url, 0, sizeof(*url));
28:
29: strlcpy((char*) url->url_line, csURL, BUFSIZ);
30: // Tech
31: if (!(pos = strstr((char*) url->url_line, "://"))) {
32: url->url_path.vallen = strlen((char*) url->url_line);
33: url->url_path.value = (char*) url->url_line;
34: return ret;
35: } else {
36: url->url_tech.value = (char*) url->url_line;
37: url->url_tech.vallen = pos - (char*) url->url_line;
38: if (url->url_tech.vallen)
39: ret |= 1;
40:
41: *pos = 0;
42: pos += 3;
43: }
44:
45: // User
46: if ((at = strchr(pos, '@'))) {
47: *at++ = 0;
48: // Pass
49: if ((cl = strchr(pos, ':'))) {
50: *cl++ = 0;
51:
52: url->url_pass.value = cl;
53: url->url_pass.vallen = at - cl - 1;
54: if (url->url_pass.vallen)
55: ret |= 4;
56: } else
57: cl = at;
58:
59: url->url_user.value = pos;
60: url->url_user.vallen = cl - pos - 1;
61: if (url->url_user.vallen)
62: ret |= 2;
63:
64: pos = at;
65: }
66:
67: // Host
68: if ((sl = strchr(pos, '/')))
69: *sl++ = 0;
70: else
71: sl = pos + strlen(pos) + 1;
72: // Port
73: if ((cl = strchr(pos, ':'))) {
74: *cl++ = 0;
75:
76: url->url_port.value = cl;
77: url->url_port.vallen = sl - cl - 1;
78: if (url->url_port.vallen)
79: ret |= 16;
80: } else
81: cl = sl;
82:
83: url->url_host.value = pos;
84: url->url_host.vallen = cl - pos - 1;
85: if (url->url_host.vallen)
86: ret |= 8;
87:
88: pos = sl;
89:
90: // Args
91: if ((at = strchr(pos, '?'))) {
92: *at++ = 0;
93:
94: url->url_args.value = at;
95: url->url_args.vallen = strlen(at);
96: if (url->url_args.vallen)
97: ret |= 64;
98: } else
99: at = pos + strlen(pos) + 1;
100:
101: // Path
102: url->url_path.value = pos;
103: url->url_path.vallen = at - pos - 1;
104: if (url->url_path.vallen)
105: ret |= 32;
106:
107: pos = at + strlen(at);
108:
109: // Reserved
110: url->url_reserved = pos;
111: if (*pos)
112: ret |= 128;
113:
114: return ret;
115: }
1.1.2.2 misho 116:
117: /*
118: * io_MakeArray() Parse and make array of arguments values
1.1.2.3 misho 119: * @psArgs = Input arguments line
1.1.2.2 misho 120: * @csDelim = Delimiter(s) for separate
1.1.2.3 misho 121: * @args = Output array of arguments ... (must be free() after procced function!)
1.1.2.2 misho 122: * @nargs = Requested count of arguments
123: * return: 0 error format; -1 error:: can`t read; >0 ok, number of readed items
124: */
1.1.2.4 ! misho 125: inline int io_MakeArray(char * __restrict psArgs, const char *csDelim, char *** __restrict args, int nargs)
1.1.2.2 misho 126: {
127: char **app;
128: register int i;
129:
1.1.2.4 ! misho 130: if (!psArgs || !csDelim || !args || !nargs)
1.1.2.2 misho 131: return -1;
132: if (!(*args = malloc(sizeof(char*) * nargs))) {
133: LOGERR;
134: return -1;
135: } else
136: memset(*args, 0, sizeof(char*) * nargs);
137:
138: for (i = 0, app = *args; app < *args + nargs && (*app = strsep(&psArgs, csDelim));
139: **app ? i++ : i, **app ? app++ : app);
140: return i;
141: }
1.1.2.4 ! misho 142: /*
! 143: * io_SizeArray() Parse and calculate size of array
! 144: * @csArgs = Input arguments line
! 145: * @csDelim = Delimiter(s) for separate
! 146: * return: 0 error format; -1 error:: can`t read; >0 ok, number of items
! 147: */
! 148: inline int io_SizeArray(const char *csArgs, const char *csDelim)
! 149: {
! 150: register int res;
! 151: char *pos;
! 152:
! 153: if (!csArgs || !csDelim)
! 154: return -1;
! 155:
! 156: for (res = 1, pos = (char*) csArgs; (pos = strpbrk(pos, csDelim)); res++, pos++);
! 157: return res;
! 158: }
! 159: /*
! 160: * io_MakeAV() Parse and make attribute/value pair
! 161: * @csArgs = Input argument line
! 162: * @csDelim = Delimiter for separate
! 163: * @psAttr = Output Attribute
! 164: * @psValue = Output Value, if ==NULL this element not present value or not wanted for return
! 165: * return: 0 error format; -1 error:: can`t read; >0 ok, number of readed items
! 166: */
! 167: inline int io_MakeAV(const char * __restrict csArgs, const char *csDelim,
! 168: char * __restrict psAttr, int attrLen, char * __restrict psValue, int valLen)
! 169: {
! 170: register int ret = 0;
! 171: char *pos;
! 172:
! 173: if (!csArgs || !csDelim || !psAttr || !attrLen)
! 174: return -1;
! 175: if (psValue && !valLen)
! 176: return -1;
! 177: else
! 178: memset(psValue, 0, valLen);
! 179:
! 180: pos = strpbrk(csArgs, csDelim);
! 181: if (pos)
! 182: *pos++ = 0;
! 183: ret++;
! 184: strlcpy(psAttr, csArgs, attrLen);
! 185:
! 186: if (pos && *pos) {
! 187: ret++;
! 188: if (psValue)
! 189: strlcpy(psValue, pos, valLen);
! 190: }
! 191:
! 192: return ret;
! 193: }
! 194:
! 195: /*
! 196: * ioURLGetValue() Get value from parsed URL
! 197: * @url = Input parsed URL
! 198: * @csAttr = Attribute for search
! 199: * @psValue = Return value of attribute, if ==NULL only check for existence of attribute
! 200: * @valLen = Size of psValue array
! 201: * return: 0 error attribute not find; -1 error:: can`t read; >0 ok, find at position
! 202: */
! 203: int ioURLGetValue(struct tagIOURL *url, const char *csAttr, char * __restrict psValue, int valLen)
! 204: {
! 205: register int i, ret = 0;
! 206: char szBuf[BUFSIZ], **items, szElem[2][BUFSIZ];
! 207:
! 208: if (!url || !csAttr)
! 209: return -1;
! 210:
! 211: strlcpy(szBuf, url->url_args.value, BUFSIZ);
! 212: if (io_MakeArray(szBuf, "&", &items, io_SizeArray(szBuf, "&")) < 1)
! 213: return ret;
! 214:
! 215: for (i = 0; items[i]; i++) {
! 216: if (io_MakeAV(items[i], "=", szElem[0], BUFSIZ, szElem[1], BUFSIZ) < 1)
! 217: continue;
! 218:
! 219: if (!strcmp(szElem[0], csAttr)) {
! 220: ret = i + 1;
! 221: if (psValue && valLen)
! 222: strlcpy(psValue, szElem[1], valLen);
! 223: break;
! 224: }
! 225: }
! 226:
! 227: free(items);
! 228: return ret;
! 229: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>