From 54c6c7e4169a1d5caa9ab5f13842f47bec6a50d3 Mon Sep 17 00:00:00 2001 From: Nick Wellnhofer Date: Sun, 23 Jun 2024 21:51:52 +0200 Subject: [PATCH] uri: Only set file scheme for special Windows paths Fixes 2ce70cde. Also fix a test case. --- testparser.c | 12 ++++++------ uri.c | 40 +++++++++++++++++++++++++++------------- 2 files changed, 33 insertions(+), 19 deletions(-) diff --git a/testparser.c b/testparser.c index 0e9ee5a3..68ab9444 100644 --- a/testparser.c +++ b/testparser.c @@ -416,11 +416,11 @@ testBuildRelativeUri(void) { }, { "file:///a/b1/c1", "/a/b2/c2", - "../b1/c1" + NULL }, { "/a/b1/c1", "file:///a/b2/c2", - "../b1/c1" + NULL }, { "a/b1/c1", "/a/b2/c2", @@ -428,7 +428,7 @@ testBuildRelativeUri(void) { }, { "/a/b1/c1", "a/b2/c2", - "file:///a/b1/c1" + NULL }, { "http://example.org/a/b1/c1", "http://example.org/a/b2/c2", @@ -490,11 +490,11 @@ testBuildRelativeUri(void) { }, { "/a/b1/c1", "y:/a/b2/c2", - "file:///a/b1/c1" + NULL }, { - "\\server\\a\\b1\\c1", + "\\\\server\\a\\b1\\c1", "a/b2/c2", - "file:///server/a/b1/c1" + "file://server/a/b1/c1" } #endif }; diff --git a/uri.c b/uri.c index d5302785..3b986532 100644 --- a/uri.c +++ b/uri.c @@ -2397,21 +2397,19 @@ xmlParseUriOrPath(const char *str, xmlURIPtr *out, int *drive) { if (xmlIsAbsolutePath(BAD_CAST buf)) { #if defined(_WIN32) || defined(__CYGWIN__) const char *server = NULL; + int isFileScheme = 0; #endif - uri->scheme = (char *) xmlStrdup(BAD_CAST "file"); - if (uri->scheme == NULL) { - ret = -1; - goto done; - } - #if defined(_WIN32) || defined(__CYGWIN__) if (strncmp(buf, "//?/UNC/", 8) == 0) { server = buf + 8; + isFileScheme = 1; } else if (strncmp(buf, "//?/", 4) == 0) { path = buf + 3; + isFileScheme = 1; } else if (strncmp(buf, "//", 2) == 0) { server = buf + 2; + isFileScheme = 1; } if (server != NULL) { @@ -2433,12 +2431,22 @@ xmlParseUriOrPath(const char *str, xmlURIPtr *out, int *drive) { if ((((path[0] >= 'A') && (path[0] <= 'Z')) || ((path[0] >= 'a') && (path[0] <= 'z'))) && - (path[1] == ':')) + (path[1] == ':')) { prependSlash = 1; -#endif + isFileScheme = 1; + } - if (uri->server == NULL) - uri->port = PORT_EMPTY_SERVER; + if (isFileScheme) { + uri->scheme = xmlMemStrdup("file"); + if (uri->scheme == NULL) { + ret = -1; + goto done; + } + + if (uri->server == NULL) + uri->port = PORT_EMPTY_SERVER; + } +#endif } pathSize = strlen(path); @@ -2591,15 +2599,21 @@ xmlBuildRelativeURISafe(const xmlChar * URI, const xmlChar * base, remove_path = 1; } + bptr = (xmlChar *) bas->path; + rptr = (xmlChar *) ref->path; + + /* + * Return URI if URI and base aren't both absolute or relative. + */ + if ((bptr[0] == '/') != (rptr[0] == '/')) + goto done; + /* * At this point we can compare the two paths */ { int pos = 0; - bptr = (xmlChar *) bas->path; - rptr = (xmlChar *) ref->path; - /* * Next we compare the two strings and find where they first differ */