📄 uri.c
字号:
default: ret[len++] = '0' + (val / 0x10); } switch (val % 0x10) { case 0xF: ret[len++] = 'F'; break; case 0xE: ret[len++] = 'E'; break; case 0xD: ret[len++] = 'D'; break; case 0xC: ret[len++] = 'C'; break; case 0xB: ret[len++] = 'B'; break; case 0xA: ret[len++] = 'A'; break; default: ret[len++] = '0' + (val % 0x10); } } } } if (len >= max) { max *= 2; ret = xmlRealloc(ret, (max + 1) * sizeof(xmlChar)); if (ret == NULL) { fprintf(stderr, "xmlSaveUri: out of memory\n"); return(NULL); } } ret[len++] = 0; } return(ret);}/** * xmlPrintURI: * @stream: a FILE* for the output * @uri: pointer to an xmlURI * * Prints the URI in the stream @steam. */voidxmlPrintURI(FILE *stream, xmlURIPtr uri) { xmlChar *out; out = xmlSaveUri(uri); if (out != NULL) { fprintf(stream, "%s", out); xmlFree(out); }}/** * xmlCleanURI: * @uri: pointer to an xmlURI * * Make sure the xmlURI struct is free of content */voidxmlCleanURI(xmlURIPtr uri) { if (uri == NULL) return; if (uri->scheme != NULL) xmlFree(uri->scheme); uri->scheme = NULL; if (uri->server != NULL) xmlFree(uri->server); uri->server = NULL; if (uri->user != NULL) xmlFree(uri->user); uri->user = NULL; if (uri->path != NULL) xmlFree(uri->path); uri->path = NULL; if (uri->fragment != NULL) xmlFree(uri->fragment); uri->fragment = NULL; if (uri->opaque != NULL) xmlFree(uri->opaque); uri->opaque = NULL; if (uri->authority != NULL) xmlFree(uri->authority); uri->authority = NULL; if (uri->query != NULL) xmlFree(uri->query); uri->query = NULL;}/** * xmlFreeURI: * @uri: pointer to an xmlURI * * Free up the xmlURI struct */voidxmlFreeURI(xmlURIPtr uri) { if (uri == NULL) return; if (uri->scheme != NULL) xmlFree(uri->scheme); if (uri->server != NULL) xmlFree(uri->server); if (uri->user != NULL) xmlFree(uri->user); if (uri->path != NULL) xmlFree(uri->path); if (uri->fragment != NULL) xmlFree(uri->fragment); if (uri->opaque != NULL) xmlFree(uri->opaque); if (uri->authority != NULL) xmlFree(uri->authority); if (uri->query != NULL) xmlFree(uri->query); memset(uri, -1, sizeof(xmlURI)); xmlFree(uri);}/** * xmlURIUnescapeString: * @str: the string to unescape * @len: the lenght in bytes to unescape (or <= 0 to indicate full string) * @target: optionnal destination buffer * * Unescaping routine, does not do validity checks ! * Output is direct unsigned char translation of %XX values (no encoding) * * Returns an copy of the string, but unescaped */char *xmlURIUnescapeString(const char *str, int len, char *target) { char *ret, *out; const char *in; if (str == NULL) return(NULL); if (len <= 0) len = strlen(str); if (len <= 0) return(NULL); if (target == NULL) { ret = (char *) xmlMalloc(len + 1); if (ret == NULL) { fprintf(stderr, "xmlURIUnescapeString: out of memory\n"); return(NULL); } } else ret = target; in = str; out = ret; while(len > 0) { if (*in == '%') { in++; if ((*in >= '0') && (*in <= '9')) *out = (*in - '0'); else if ((*in >= 'a') && (*in <= 'f')) *out = (*in - 'a') + 10; else if ((*in >= 'A') && (*in <= 'F')) *out = (*in - 'A') + 10; in++; if ((*in >= '0') && (*in <= '9')) *out = *out * 16 + (*in - '0'); else if ((*in >= 'a') && (*in <= 'f')) *out = *out * 16 + (*in - 'a') + 10; else if ((*in >= 'A') && (*in <= 'F')) *out = *out * 16 + (*in - 'A') + 10; in++; len -= 3; out++; } else { *out++ = *in++; len--; } } *out = 0; return(ret);}/** * 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 */intxmlParseURIFragment(xmlURIPtr uri, const char **str) { const char *cur = *str; if (str == NULL) return(-1); while (IS_URIC(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 */intxmlParseURIQuery(xmlURIPtr uri, const char **str) { const char *cur = *str; if (str == NULL) return(-1); while (IS_URIC(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 */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 = xmlURIUnescapeString(*str, cur - *str, NULL); /* !!! strndup */ } *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 */intxmlParseURIOpaquePart(xmlURIPtr uri, const char **str) { const char *cur; if (str == NULL) return(-1); cur = *str; if (!IS_URIC_NO_SLASH(cur)) { return(3); } NEXT(cur); while (IS_URIC(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 */intxmlParseURIServer(xmlURIPtr uri, const char **str) { const char *cur; const char *host, *tmp; 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; } /* * 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 */ host = cur; if (IS_DIGIT(*cur)) { while(IS_DIGIT(*cur)) cur++; if (*cur != '.') goto host_name; cur++; if (!IS_DIGIT(*cur)) goto host_name; while(IS_DIGIT(*cur)) cur++; if (*cur != '.') goto host_name; cur++; if (!IS_DIGIT(*cur)) goto host_name; while(IS_DIGIT(*cur)) cur++; if (*cur != '.') goto host_name; cur++; if (!IS_DIGIT(*cur)) goto host_name; while(IS_DIGIT(*cur)) cur++; 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); } goto host_done; }host_name: /* * the hostname production as-is is a parser nightmare. * simplify it to * hostname = *( domainlabel "." ) domainlabel [ "." ] * and just make sure the last label starts with a non numeric char. */ if (!IS_ALPHANUM(*cur)) return(6); while (IS_ALPHANUM(*cur)) { while ((IS_ALPHANUM(*cur)) || (*cur == '-')) cur++; if (*cur == '.') cur++; } tmp = cur; tmp--; while (IS_ALPHANUM(*tmp) && (*tmp != '.') && (tmp >= host)) tmp--; tmp++; if (!IS_ALPHA(*tmp)) return(7); 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); }host_done: /* * 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 */intxmlParseURIRelSegment(xmlURIPtr uri, const char **str) { const char *cur; if (str == NULL) return(-1); cur = *str; if (!IS_SEGMENT(cur)) { return(3); } NEXT(cur); while (IS_SEGMENT(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 */intxmlParseURIPathSegments(xmlURIPtr uri, const char **str, int slash) { const char *cur; if (str == NULL) return(-1); cur = *str; do { while (IS_PCHAR(cur)) NEXT(cur); if (*cur == ';') { cur++; while (IS_PCHAR(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 *) xmlMalloc(len + 1); if (path == NULL) { fprintf(stderr, "xmlParseURIPathSegments: out of memory\n"); *str = cur; return(-1); } if (uri->path != NULL) memcpy(path, uri->path, len2); if (slash) { path[len2] = '/'; len2++; } 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 */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)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -