Annotation of libaitio/src/url.c, revision 1.1.2.3
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.3 ! misho 6: * $Id: url.c,v 1.1.2.2 2010/03/02 16:45:23 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: */
125: int io_MakeArray(char * __restrict psArgs, const char *csDelim, char *** __restrict args, int nargs)
126: {
127: char **app;
128: register int i;
129:
130: if (!psArgs || !args || !nargs)
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: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>