--- libaitwww/src/url.c 2012/03/10 15:00:45 1.1.2.1 +++ libaitwww/src/url.c 2013/05/26 20:30:43 1.3.4.2 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: url.c,v 1.1.2.1 2012/03/10 15:00:45 misho Exp $ +* $Id: url.c,v 1.3.4.2 2013/05/26 20:30:43 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -12,7 +12,7 @@ terms: All of the documentation and software included in the ELWIX and AITNET Releases is copyrighted by ELWIX - Sofia/Bulgaria -Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 +Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 by Michael Pounov . All rights reserved. Redistribution and use in source and binary forms, with or without @@ -47,6 +47,30 @@ SUCH DAMAGE. /* + * www_URLInit() - Init URL structure and free old one + * + * @url = Input URL + * return: -1 error or 0 ok + */ +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; @@ -154,71 +174,65 @@ www_URLGet(const char *csURL, struct tagIOURL *url) } /* - * www_Path2File() - Parse and make path/filename pair + * www_URLGetFile() - Get file from parsed URL * - * @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 + * @url = Input parsed URL + * @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 */ -inline int -www_Path2File(const char * __restrict csArgs, char * __restrict psPath, - int pathLen, char * __restrict psFile, int fileLen) +int +www_URLGetFile(struct tagIOURL * __restrict url, ait_val_t * __restrict value) { char *pos, *psBuf; + int ret = 0; - if (!csArgs || !psFile || !fileLen) + if (!url || !value) return -1; - if (psPath && !pathLen) - return -1; - else - memset(psPath, 0, pathLen); - psBuf = strdup(csArgs); + + psBuf = e_strdup(AIT_GET_STR(&url->url_path)); if (!psBuf) { - LOGERR; + www_SetErr(elwix_GetErrno(), "%s", elwix_GetError()); return -1; - } + } else + AIT_FREE_VAL(value); pos = strrchr(psBuf, '/'); if (!pos) { - strlcpy(psFile, psBuf, fileLen); - - free(psBuf); - return 1; + /* whole string is filename */ + pos = psBuf; + ret = 1; } else *pos++ = 0; + /* If not specified file in path, default replace to / */ + if (!*pos) { + pos = "/"; + ret = 2; + } - strlcpy(psFile, pos, fileLen); - if (psPath) - strlcpy(psPath, psBuf, pathLen); - - free(psBuf); - return 2; + AIT_SET_STR(value, pos); + e_free(psBuf); + return ret; } /* - * www_URLGetFile() - Get file from parsed URL + * www_URLFree() - URL free structure * * @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 + * return: none */ -int -www_URLGetFile(struct tagIOURL *url, char * __restrict psValue, int valLen) +void +www_URLFree(struct tagIOURL * __restrict url) { - if (!url || !psValue || !valLen) - return -1; + 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; - if (www_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; + AIT_FREE_VAL(&url->url_line); } /*