--- libaitwww/src/url.c 2012/03/15 01:59:37 1.2 +++ libaitwww/src/url.c 2012/09/20 14:19:45 1.3 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: url.c,v 1.2 2012/03/15 01:59:37 misho Exp $ +* $Id: url.c,v 1.3 2012/09/20 14:19:45 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -47,6 +47,30 @@ SUCH DAMAGE. /* + * www_URLInit() - Init URL structure and free old one + * + * @url = Input URL + * return: -1 error or 0 ok + */ +inline int +www_URLInit(struct tagIOURL * __restrict url) +{ + if (!url) + return -1; + else + memset(url, 0, sizeof(struct tagIOURL)); + + AIT_INIT_VAL2(&url->url_tech, string); + AIT_INIT_VAL2(&url->url_user, string); + AIT_INIT_VAL2(&url->url_pass, string); + AIT_INIT_VAL2(&url->url_host, string); + AIT_INIT_VAL2(&url->url_port, string); + AIT_INIT_VAL2(&url->url_path, string); + AIT_INIT_VAL2(&url->url_args, string); + return 0; +} + +/* * www_URLGet() - Parse and get data from input URL * * @csURL = Input URL line @@ -55,7 +79,7 @@ SUCH DAMAGE. * -1 error:: can`t read; >0 ok, up bits for known elements */ int -www_URLGet(const char *csURL, struct tagIOURL *url) +www_URLGet(const char *csURL, struct tagIOURL * __restrict url) { char *pos, *at, *cl, *sl; int ret = 0; @@ -63,22 +87,24 @@ www_URLGet(const char *csURL, struct tagIOURL *url) if (!url) return -1; else - memset(url, 0, sizeof(*url)); + www_URLInit(url); - strlcpy((char*) url->url_line, csURL, BUFSIZ); + AIT_SET_STR(&url->url_line, csURL); + /* unescape URL */ + www_unescape(AIT_GET_STR(&url->url_line)); /* 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; + if (!(pos = strstr(AIT_GET_STR(&url->url_line), "://"))) { + AIT_SET_LIKE(&url->url_path, string, + AIT_LEN(&url->url_line), AIT_ADDR(&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) + AIT_SET_LIKE(&url->url_tech, string, + pos - AIT_GET_STR(&url->url_line), AIT_ADDR(&url->url_line)); + if (AIT_LEN(&url->url_tech)) ret |= 1; *pos = 0; - pos += 3; + pos += 3; /* :// */ } /* User */ @@ -88,16 +114,14 @@ www_URLGet(const char *csURL, struct tagIOURL *url) if ((cl = strchr(pos, ':'))) { *cl++ = 0; - url->url_pass.value = cl; - url->url_pass.vallen = at - cl - 1; - if (url->url_pass.vallen) + AIT_SET_LIKE(&url->url_pass, string, at - cl - 1, cl); + if (AIT_LEN(&url->url_pass)) ret |= 4; } else cl = at; - url->url_user.value = pos; - url->url_user.vallen = cl - pos - 1; - if (url->url_user.vallen) + AIT_SET_LIKE(&url->url_user, string, cl - pos - 1, pos); + if (AIT_LEN(&url->url_user)) ret |= 2; pos = at; @@ -112,16 +136,14 @@ www_URLGet(const char *csURL, struct tagIOURL *url) if ((cl = strchr(pos, ':'))) { *cl++ = 0; - url->url_port.value = cl; - url->url_port.vallen = sl - cl - 1; - if (url->url_port.vallen) + AIT_SET_LIKE(&url->url_port, string, sl - cl - 1, cl); + if (AIT_LEN(&url->url_port)) ret |= 16; } else cl = sl; - url->url_host.value = pos; - url->url_host.vallen = cl - pos - 1; - if (url->url_host.vallen) + AIT_SET_LIKE(&url->url_host, string, cl - pos - 1, pos); + if (AIT_LEN(&url->url_host)) ret |= 8; pos = sl; @@ -130,23 +152,21 @@ www_URLGet(const char *csURL, struct tagIOURL *url) if ((at = strchr(pos, '?'))) { *at++ = 0; - url->url_args.value = at; - url->url_args.vallen = strlen(at); - if (url->url_args.vallen) + AIT_SET_LIKE(&url->url_args, string, strlen(at), at); + if (AIT_LEN(&url->url_args)) 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) + AIT_SET_LIKE(&url->url_path, string, at - pos - 1, pos); + if (AIT_LEN(&url->url_path)) ret |= 32; pos = at + strlen(at); /* Reserved */ - url->url_reserved = pos; + url->url_reserved = (u_char*) pos; if (*pos) ret |= 128; @@ -157,42 +177,62 @@ www_URLGet(const char *csURL, struct tagIOURL *url) * www_URLGetFile() - 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 + * @value = Return filename, if not specified file in url path, replace with / + * return: -1 error, 0 filename from path, 1 filename or 2 not specified filename */ int -www_URLGetFile(struct tagIOURL *url, char * __restrict psValue, int valLen) +www_URLGetFile(struct tagIOURL * __restrict url, ait_val_t * __restrict value) { char *pos, *psBuf; + int ret = 0; - if (!url || !psValue || !valLen) + if (!url || !value) return -1; - psBuf = strdup(url->url_path.value); + psBuf = io_strdup(AIT_GET_STR(&url->url_path)); if (!psBuf) { - LOGERR; + www_SetErr(io_GetErrno(), "%s", io_GetError()); return -1; - } + } else + AIT_FREE_VAL(value); pos = strrchr(psBuf, '/'); if (!pos) { /* whole string is filename */ - strlcpy(psValue, psBuf, valLen); - - free(psBuf); - return 1; - } else { + pos = psBuf; + ret = 1; + } else *pos++ = 0; - - strlcpy(psValue, pos, valLen); - free(psBuf); + /* If not specified file in path, default replace to / */ + if (!*pos) { + pos = "/"; + ret = 2; } - /* If not specified file in path, default replace to / */ - if (!*psValue) - strlcpy(psValue, "/", valLen); - return 0; + AIT_SET_STR(value, pos); + io_free(psBuf); + return ret; +} + +/* + * www_URLFree() - URL free structure + * + * @url = Input parsed URL + * return: none + */ +inline void +www_URLFree(struct tagIOURL * __restrict url) +{ + AIT_FREE_VAL(&url->url_tech); + AIT_FREE_VAL(&url->url_user); + AIT_FREE_VAL(&url->url_pass); + AIT_FREE_VAL(&url->url_host); + AIT_FREE_VAL(&url->url_port); + AIT_FREE_VAL(&url->url_path); + AIT_FREE_VAL(&url->url_args); + url->url_reserved = NULL; + + AIT_FREE_VAL(&url->url_line); } /*