version 1.1, 2010/03/02 15:10:19
|
version 1.2, 2010/03/09 09:26:27
|
Line 0
|
Line 1
|
|
/************************************************************************* |
|
* (C) 2010 AITNET ltd - Sofia/Bulgaria - <misho@aitbg.com> |
|
* by Michael Pounov <misho@openbsd-bg.org> |
|
* |
|
* $Author$ |
|
* $Id$ |
|
* |
|
*************************************************************************/ |
|
#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_MakeArray() Parse and make array of arguments values |
|
* @psArgs = Input arguments line |
|
* @csDelim = Delimiter(s) for separate |
|
* @args = Output array of arguments ... (must be free() after procced function!) |
|
* @nargs = Requested count of arguments |
|
* return: 0 error format; -1 error:: can`t read; >0 ok, number of readed items |
|
*/ |
|
inline int io_MakeArray(char * __restrict psArgs, const char *csDelim, char *** __restrict args, int nargs) |
|
{ |
|
char **app; |
|
register int i; |
|
|
|
if (!psArgs || !csDelim || !args || !nargs) |
|
return -1; |
|
if (!(*args = malloc(sizeof(char*) * nargs))) { |
|
LOGERR; |
|
return -1; |
|
} else |
|
memset(*args, 0, sizeof(char*) * nargs); |
|
|
|
for (i = 0, app = *args; app < *args + nargs && (*app = strsep(&psArgs, csDelim)); |
|
**app ? i++ : i, **app ? app++ : app); |
|
return i; |
|
} |
|
/* |
|
* io_SizeArray() Parse and calculate size of array |
|
* @csArgs = Input arguments line |
|
* @csDelim = Delimiter(s) for separate |
|
* return: 0 error format; -1 error:: can`t read; >0 ok, number of items |
|
*/ |
|
inline int io_SizeArray(const char *csArgs, const char *csDelim) |
|
{ |
|
register int res; |
|
char *pos; |
|
|
|
if (!csArgs || !csDelim) |
|
return -1; |
|
|
|
for (res = 1, pos = (char*) csArgs; (pos = strpbrk(pos, csDelim)); res++, pos++); |
|
return res; |
|
} |
|
/* |
|
* io_MakeAV() Parse and make attribute/value pair |
|
* @csArgs = Input argument line |
|
* @csDelim = Delimiter for separate |
|
* @psAttr = Output Attribute |
|
* @attrLen = Size of attribute array |
|
* @psValue = Output Value, if ==NULL this element not present value or not wanted for return |
|
* @valLen = Size of value array |
|
* return: 0 error format; -1 error:: can`t read; >0 ok, number of readed items |
|
*/ |
|
inline int io_MakeAV(const char * __restrict csArgs, const char *csDelim, |
|
char * __restrict psAttr, int attrLen, char * __restrict psValue, int valLen) |
|
{ |
|
register int ret = 0; |
|
char *pos, *psBuf; |
|
|
|
if (!csArgs || !csDelim || !psAttr || !attrLen) |
|
return -1; |
|
if (psValue && !valLen) |
|
return -1; |
|
else |
|
memset(psValue, 0, valLen); |
|
psBuf = strdup(csArgs); |
|
if (!psBuf) { |
|
LOGERR; |
|
return -1; |
|
} |
|
|
|
pos = strpbrk(psBuf, csDelim); |
|
if (pos) |
|
*pos++ = 0; |
|
ret++; |
|
strlcpy(psAttr, psBuf, attrLen); |
|
|
|
if (pos && *pos) { |
|
ret++; |
|
if (psValue) |
|
strlcpy(psValue, pos, valLen); |
|
} |
|
|
|
free(psBuf); |
|
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], **items, szElem[2][BUFSIZ]; |
|
int len; |
|
|
|
if (!url || !csAttr) |
|
return -1; |
|
|
|
strlcpy(szBuf, url->url_args.value, BUFSIZ); |
|
if (io_MakeArray(szBuf, "&", &items, (len = io_SizeArray(szBuf, "&"))) < 1) |
|
return ret; |
|
|
|
for (i = 0; i < len && items[i]; i++) { |
|
if (io_MakeAV(items[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; |
|
} |
|
} |
|
|
|
free(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; |
|
} |