📄 uri.c.svn-base
字号:
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 = *str; if (str == NULL) return (-1); while (IS_URIC(cur) || IS_UNWISE(cur)) NEXT(cur); if (uri != NULL) { if (uri->fragment != NULL) xmlFree(uri->fragment); 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 = *str; if (str == NULL) return (-1); while (IS_URIC(cur) || ((uri != NULL) && (uri->cleanup) && (IS_UNWISE(cur)))) NEXT(cur); if (uri != NULL) { if (uri->query != NULL) xmlFree(uri->query); uri->query = xmlURIUnescapeString(*str, cur - *str, NULL); } *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); /* !!! strndup */ uri->scheme = xmlURIUnescapeString(*str, cur - *str, NULL); } *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) && (IS_UNWISE(cur))))) { return (3); } NEXT(cur); while (IS_URIC(cur) || ((uri != NULL) && (uri->cleanup) && (IS_UNWISE(cur)))) NEXT(cur); if (uri != NULL) { if (uri->opaque != NULL) xmlFree(uri->opaque); 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 * hostname = *( domainlabel "." ) toplabel [ "." ] * domainlabel = alphanum | alphanum *( alphanum | "-" ) alphanum * toplabel = alpha | alpha *( alphanum | "-" ) alphanum * IPv4address = 1*digit "." 1*digit "." 1*digit "." 1*digit * port = *digit * * Returns 0 or the error code */static intxmlParseURIServer(xmlURIPtr uri, const char **str) { const char *cur; const char *host, *tmp; const int IPmax = 4; int oct; if (str == NULL) return(-1); cur = *str; /* * is there an userinfo ? */ while (IS_USERINFO(cur)) NEXT(cur); if (*cur == '@') { if (uri != NULL) { if (uri->user != NULL) xmlFree(uri->user); 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 derive either an IPV4 address * or an unresolved name. Check the IP first, it easier to detect * errors if wrong one */ for (oct = 0; oct < IPmax; ++oct) { if (*cur == '.') return(3); /* e.g. http://.xml/ or http://18.29..30/ */ while(IS_DIGIT(*cur)) cur++; if (oct == (IPmax-1)) continue; if (*cur != '.') break; cur++; } if (oct < IPmax || (*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 (uri->server != NULL) xmlFree(uri->server); 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) && (IS_UNWISE(cur))))) { return (3); } NEXT(cur); while (IS_SEGMENT(cur) || ((uri != NULL) && (uri->cleanup) && (IS_UNWISE(cur)))) NEXT(cur); if (uri != NULL) { if (uri->path != NULL) xmlFree(uri->path); 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) && (IS_UNWISE(cur)))) NEXT(cur); while (*cur == ';') { cur++; while (IS_PCHAR(cur) || ((uri != NULL) && (uri->cleanup) && (IS_UNWISE(cur)))) NEXT(cur); } if (*cur != '/') break; cur++; } while (1); if (uri != NULL) { int len, len2 = 0; char *path; /* * Concat the set of path segments to the current path */ len = cur - *str; if (slash) len++; if (uri->path != NULL) { len2 = strlen(uri->path); len += len2; } path = (char *) xmlMallocAtomic(len + 1); if (path == NULL) { xmlGenericError(xmlGenericErrorContext, "xmlParseURIPathSegments: out of memory\n"); *str = cur; return (-1); } if (uri->path != NULL) memcpy(path, uri->path, len2); if (slash) { path[len2] = '/'; len2++; } path[len2] = 0; if (cur - *str > 0) xmlURIUnescapeString(*str, cur - *str, &path[len2]); if (uri->path != NULL) xmlFree(uri->path); uri->path = path; } *str = cur; return (0);}/** * xmlParseURIAuthority: * @uri: pointer to an URI structure * @str: pointer to the string to analyze * * Parse the authority part of an URI. * * authority = server | reg_name * server = [ [ userinfo "@" ] hostport ] * reg_name = 1*( unreserved | escaped | "$" | "," | ";" | ":" | * "@" | "&" | "=" | "+" ) * * Note : this is completely ambiguous since reg_name is allowed to * use the full set of chars in use by server: * * 3.2.1. Registry-based Naming Authority * * The structure of a registry-based naming authority is specific * to the URI scheme, but constrained to the allowed characters * for an authority component. * * Returns 0 or the error code */static intxmlParseURIAuthority(xmlURIPtr uri, const char **str) { const char *cur; int ret; if (str == NULL) return(-1); cur = *str; /* * try first to parse it as a server string. */ ret = xmlParseURIServer(uri, str); if ((ret == 0) && (*str != NULL) && ((**str == 0) || (**str == '/') || (**str == '?'))) return(0); *str = cur; /* * failed, fallback to reg_name */ if (!IS_REG_NAME(cur)) { return(5); } NEXT(cur); while (IS_REG_NAME(cur)) NEXT(cur); if (uri != NULL) { if (uri->server != NULL) xmlFree(uri->server); uri->server = NULL; if (uri->user != NULL) xmlFree(uri->user); uri->user = NULL; if (uri->authority != NULL) xmlFree(uri->authority); uri->authority = xmlURIUnescapeString(*str, cur - *str, NULL); } *str = cur; return(0);}/** * xmlParseURIHierPart: * @uri: pointer to an URI structure * @str: pointer to the string to analyze * * Parse an URI hierarchical part * * hier_part = ( net_path | abs_path ) [ "?" query ] * abs_path = "/" path_segments * net_path = "//" authority [ abs_path ] * * Returns 0 or the error code */static intxmlParseURIHierPart(xmlURIPtr uri, const char **str) { int ret; const char *cur; if (str == NULL) return(-1); cur = *str; if ((cur[0] == '/') && (cur[1] == '/')) { cur += 2; ret = xmlParseURIAuthority(uri, &cur); if (ret != 0) return(ret); if (cur[0] == '/') { cur++; ret = xmlParseURIPathSegments(uri, &cur, 1); } } else if (cur[0] == '/') { cur++; ret = xmlParseURIPathSegments(uri, &cur, 1); } else { return(4);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -