⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 uri.c

📁 xml开源解析代码.版本为libxml2-2.6.29,可支持GB3212.网络消息发送XML时很有用.
💻 C
📖 第 1 页 / 共 5 页
字号:
        ret = xmlStrcat(ret, BAD_CAST ":");        xmlFree(segment);    }    if (uri->authority) {        segment =            xmlURIEscapeStr(BAD_CAST uri->authority, BAD_CAST "/?;:@");        NULLCHK(segment)        ret = xmlStrcat(ret, BAD_CAST "//");        ret = xmlStrcat(ret, segment);        xmlFree(segment);    }    if (uri->user) {        segment = xmlURIEscapeStr(BAD_CAST uri->user, BAD_CAST ";:&=+$,");        NULLCHK(segment)		ret = xmlStrcat(ret,BAD_CAST "//");	        ret = xmlStrcat(ret, segment);        ret = xmlStrcat(ret, BAD_CAST "@");        xmlFree(segment);    }    if (uri->server) {        segment = xmlURIEscapeStr(BAD_CAST uri->server, BAD_CAST "/?;:@");        NULLCHK(segment)		if (uri->user == NULL)       		ret = xmlStrcat(ret, BAD_CAST "//");        ret = xmlStrcat(ret, segment);        xmlFree(segment);    }    if (uri->port) {        xmlChar port[10];        snprintf((char *) port, 10, "%d", uri->port);        ret = xmlStrcat(ret, BAD_CAST ":");        ret = xmlStrcat(ret, port);    }    if (uri->path) {        segment =            xmlURIEscapeStr(BAD_CAST uri->path, BAD_CAST ":@&=+$,/?;");        NULLCHK(segment)        ret = xmlStrcat(ret, segment);        xmlFree(segment);    }    if (uri->query_raw) {        ret = xmlStrcat(ret, BAD_CAST "?");        ret = xmlStrcat(ret, BAD_CAST uri->query_raw);    }    else if (uri->query) {        segment =            xmlURIEscapeStr(BAD_CAST uri->query, BAD_CAST ";/?:@&=+,$");        NULLCHK(segment)        ret = xmlStrcat(ret, BAD_CAST "?");        ret = xmlStrcat(ret, segment);        xmlFree(segment);    }    if (uri->opaque) {        segment = xmlURIEscapeStr(BAD_CAST uri->opaque, BAD_CAST "");        NULLCHK(segment)        ret = xmlStrcat(ret, segment);        xmlFree(segment);    }    if (uri->fragment) {        segment = xmlURIEscapeStr(BAD_CAST uri->fragment, BAD_CAST "#");        NULLCHK(segment)        ret = xmlStrcat(ret, BAD_CAST "#");        ret = xmlStrcat(ret, segment);        xmlFree(segment);    }    xmlFreeURI(uri);#undef NULLCHK    return (ret);}/************************************************************************ *									* *			Escaped URI parsing				* *									* ************************************************************************//** * xmlParseURIFragment: * @uri:  pointer to an URI structure * @str:  pointer to the string to analyze * * Parse an URI fragment string and fills in the appropriate fields * of the @uri structure. *  * fragment = *uric * * Returns 0 or the error code */static intxmlParseURIFragment(xmlURIPtr uri, const char **str){    const char *cur;        if (str == NULL)        return (-1);    cur = *str;    while (IS_URIC(cur) || IS_UNWISE(cur))        NEXT(cur);    if (uri != NULL) {        if (uri->fragment != NULL)            xmlFree(uri->fragment);	if (uri->cleanup & 2)	    uri->fragment = STRNDUP(*str, cur - *str);	else	    uri->fragment = xmlURIUnescapeString(*str, cur - *str, NULL);    }    *str = cur;    return (0);}/** * xmlParseURIQuery: * @uri:  pointer to an URI structure * @str:  pointer to the string to analyze * * Parse the query part of an URI *  * query = *uric * * Returns 0 or the error code */static intxmlParseURIQuery(xmlURIPtr uri, const char **str){    const char *cur;    if (str == NULL)        return (-1);    cur = *str;    while ((IS_URIC(cur)) ||           ((uri != NULL) && (uri->cleanup & 1) && (IS_UNWISE(cur))))        NEXT(cur);    if (uri != NULL) {        if (uri->query != NULL)            xmlFree(uri->query);	if (uri->cleanup & 2)	    uri->query = STRNDUP(*str, cur - *str);	else	    uri->query = xmlURIUnescapeString(*str, cur - *str, NULL);	/* Save the raw bytes of the query as well.	 * See: http://mail.gnome.org/archives/xml/2007-April/thread.html#00114	 */	if (uri->query_raw != NULL)	    xmlFree (uri->query_raw);	uri->query_raw = STRNDUP (*str, cur - *str);    }    *str = cur;    return (0);}/** * xmlParseURIScheme: * @uri:  pointer to an URI structure * @str:  pointer to the string to analyze * * Parse an URI scheme *  * scheme = alpha *( alpha | digit | "+" | "-" | "." ) * * Returns 0 or the error code */static intxmlParseURIScheme(xmlURIPtr uri, const char **str) {    const char *cur;    if (str == NULL)	return(-1);        cur = *str;    if (!IS_ALPHA(*cur))	return(2);    cur++;    while (IS_SCHEME(*cur)) cur++;    if (uri != NULL) {	if (uri->scheme != NULL) xmlFree(uri->scheme);	uri->scheme = STRNDUP(*str, cur - *str);    }    *str = cur;    return(0);}/** * xmlParseURIOpaquePart: * @uri:  pointer to an URI structure * @str:  pointer to the string to analyze * * Parse an URI opaque part *  * opaque_part = uric_no_slash *uric * * Returns 0 or the error code */static intxmlParseURIOpaquePart(xmlURIPtr uri, const char **str){    const char *cur;    if (str == NULL)        return (-1);    cur = *str;    if (!((IS_URIC_NO_SLASH(cur)) ||          ((uri != NULL) && (uri->cleanup & 1) && (IS_UNWISE(cur))))) {        return (3);    }    NEXT(cur);    while ((IS_URIC(cur)) ||           ((uri != NULL) && (uri->cleanup & 1) && (IS_UNWISE(cur))))        NEXT(cur);    if (uri != NULL) {        if (uri->opaque != NULL)            xmlFree(uri->opaque);	if (uri->cleanup & 2)	    uri->opaque = STRNDUP(*str, cur - *str);	else	    uri->opaque = xmlURIUnescapeString(*str, cur - *str, NULL);    }    *str = cur;    return (0);}/** * xmlParseURIServer: * @uri:  pointer to an URI structure * @str:  pointer to the string to analyze * * Parse a server subpart of an URI, it's a finer grain analysis * of the authority part. *  * server        = [ [ userinfo "@" ] hostport ] * userinfo      = *( unreserved | escaped | *                       ";" | ":" | "&" | "=" | "+" | "$" | "," ) * hostport      = host [ ":" port ] * host          = hostname | IPv4address | IPv6reference * hostname      = *( domainlabel "." ) toplabel [ "." ] * domainlabel   = alphanum | alphanum *( alphanum | "-" ) alphanum * toplabel      = alpha | alpha *( alphanum | "-" ) alphanum * IPv6reference = "[" IPv6address "]" * IPv6address   = hexpart [ ":" IPv4address ] * IPv4address   = 1*3digit "." 1*3digit "." 1*3digit "." 1*3digit * hexpart       = hexseq | hexseq "::" [ hexseq ]| "::" [ hexseq ] * hexseq        = hex4 *( ":" hex4) * hex4          = 1*4hexdig * port          = *digit * * Returns 0 or the error code */static intxmlParseURIServer(xmlURIPtr uri, const char **str) {    const char *cur;    const char *host, *tmp;    const int IPV4max = 4;    const int IPV6max = 8;    int oct;    if (str == NULL)	return(-1);        cur = *str;    /*     * is there a userinfo ?     */    while (IS_USERINFO(cur)) NEXT(cur);    if (*cur == '@') {	if (uri != NULL) {	    if (uri->user != NULL) xmlFree(uri->user);	    if (uri->cleanup & 2)		uri->user = STRNDUP(*str, cur - *str);	    else		uri->user = xmlURIUnescapeString(*str, cur - *str, NULL);	}	cur++;    } else {	if (uri != NULL) {	    if (uri->user != NULL) xmlFree(uri->user);	    uri->user = NULL;	}        cur = *str;    }    /*     * This can be empty in the case where there is no server     */    host = cur;    if (*cur == '/') {	if (uri != NULL) {	    if (uri->authority != NULL) xmlFree(uri->authority);	    uri->authority = NULL;	    if (uri->server != NULL) xmlFree(uri->server);	    uri->server = NULL;	    uri->port = 0;	}	return(0);    }    /*     * host part of hostport can denote an IPV4 address, an IPV6 address     * or an unresolved name. Check the IP first, its easier to detect     * errors if wrong one.     * An IPV6 address must start with a '[' and end with a ']'.     */    if (*cur == '[') {	int compress=0;	cur++;	for (oct = 0; oct < IPV6max; ++oct) {	    if (*cur == ':') {		if (compress)		    return(3);	/* multiple compression attempted */		if (!oct) { 	/* initial char is compression */		    if (*++cur != ':')			return(3);		}		compress = 1;	/* set compression-encountered flag */		cur++;		/* skip over the second ':' */		continue;	    }	    while(IS_HEX(*cur)) cur++;	    if (oct == (IPV6max-1))		continue;	    if (*cur != ':')		break;	    cur++;	}	if ((!compress) && (oct != IPV6max))	    return(3);	if (*cur != ']')	    return(3);	if (uri != NULL) {	    if (uri->server != NULL) xmlFree(uri->server);	    uri->server = (char *)xmlStrndup((xmlChar *)host+1,			(cur-host)-1);	}	cur++;    } else {	/*	 * Not IPV6, maybe IPV4	 */	for (oct = 0; oct < IPV4max; ++oct) {            if (*cur == '.')                 return(3); /* e.g. http://.xml/ or http://18.29..30/ */            while(IS_DIGIT(*cur)) cur++;            if (oct == (IPV4max-1))                continue;            if (*cur != '.')	        break;            cur++;	}    }    if ((host[0] != '[') && (oct < IPV4max || (*cur == '.' && cur++) ||			     IS_ALPHA(*cur))) {        /* maybe host_name */        if (!IS_ALPHANUM(*cur))            return(4); /* e.g. http://xml.$oft */        do {            do ++cur; while (IS_ALPHANUM(*cur));            if (*cur == '-') {	        --cur;                if (*cur == '.')                    return(5); /* e.g. http://xml.-soft */	        ++cur;		continue;            }    	    if (*cur == '.') {	        --cur;                if (*cur == '-')                    return(6); /* e.g. http://xml-.soft */                if (*cur == '.')                    return(7); /* e.g. http://xml..soft */	        ++cur;		continue;            }	    break;        } while (1);        tmp = cur;        if (tmp[-1] == '.')            --tmp; /* e.g. http://xml.$Oft/ */        do --tmp; while (tmp >= host && IS_ALPHANUM(*tmp));        if ((++tmp == host || tmp[-1] == '.') && !IS_ALPHA(*tmp))            return(8); /* e.g. http://xmlsOft.0rg/ */    }    if (uri != NULL) {	if (uri->authority != NULL) xmlFree(uri->authority);	uri->authority = NULL;	if (host[0] != '[') {	/* it's not an IPV6 addr */	    if (uri->server != NULL) xmlFree(uri->server);	    if (uri->cleanup & 2)		uri->server = STRNDUP(host, cur - host);	    else		uri->server = xmlURIUnescapeString(host, cur - host, NULL);	}    }    /*     * finish by checking for a port presence.     */    if (*cur == ':') {        cur++;	if (IS_DIGIT(*cur)) {	    if (uri != NULL)	        uri->port = 0;	    while (IS_DIGIT(*cur)) {	        if (uri != NULL)		    uri->port = uri->port * 10 + (*cur - '0');		cur++;	    }	}    }    *str = cur;    return(0);}	/** * xmlParseURIRelSegment: * @uri:  pointer to an URI structure * @str:  pointer to the string to analyze * * Parse an URI relative segment *  * rel_segment = 1*( unreserved | escaped | ";" | "@" | "&" | "=" | *                          "+" | "$" | "," ) * * Returns 0 or the error code */static intxmlParseURIRelSegment(xmlURIPtr uri, const char **str){    const char *cur;    if (str == NULL)        return (-1);    cur = *str;    if (!((IS_SEGMENT(cur)) ||          ((uri != NULL) && (uri->cleanup & 1) && (IS_UNWISE(cur))))) {        return (3);    }    NEXT(cur);    while ((IS_SEGMENT(cur)) ||           ((uri != NULL) && (uri->cleanup & 1) && (IS_UNWISE(cur))))        NEXT(cur);    if (uri != NULL) {        if (uri->path != NULL)            xmlFree(uri->path);	if (uri->cleanup & 2)	    uri->path = STRNDUP(*str, cur - *str);	else	    uri->path = xmlURIUnescapeString(*str, cur - *str, NULL);    }    *str = cur;    return (0);}/** * xmlParseURIPathSegments: * @uri:  pointer to an URI structure * @str:  pointer to the string to analyze * @slash:  should we add a leading slash * * Parse an URI set of path segments *  * path_segments = segment *( "/" segment ) * segment       = *pchar *( ";" param ) * param         = *pchar * * Returns 0 or the error code */static intxmlParseURIPathSegments(xmlURIPtr uri, const char **str, int slash){    const char *cur;    if (str == NULL)        return (-1);    cur = *str;    do {        while ((IS_PCHAR(cur)) ||	       ((uri != NULL) && (uri->cleanup & 1) && (IS_UNWISE(cur))))            NEXT(cur);        while (*cur == ';') {            cur++;            while ((IS_PCHAR(cur)) ||	           ((uri != NULL) && (uri->cleanup & 1) && (IS_UNWISE(cur))))                NEXT(cur);        }        if (*cur != '/')            break;        cur++;    } while (1);    if (uri != NULL) {        int len, len2 = 0;        char *path;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -