aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2018-05-11 15:28:43 +0200
committerDavid Robillard <d@drobilla.net>2018-06-10 19:57:44 +0000
commitd208424fdb4e8b516aff2a5a0d03fc7411909471 (patch)
treeb293d21b26329c6522bd6cb769f30917c14f1bf1
parent480744aab736a669c548cb5ced68866c3ebffb4e (diff)
downloadserd-d208424fdb4e8b516aff2a5a0d03fc7411909471.tar.gz
serd-d208424fdb4e8b516aff2a5a0d03fc7411909471.tar.bz2
serd-d208424fdb4e8b516aff2a5a0d03fc7411909471.zip
Fix relative URI serialisation
-rw-r--r--NEWS1
-rw-r--r--src/serd_internal.h11
-rw-r--r--src/uri.c12
-rw-r--r--tests/serd_test.c12
4 files changed, 30 insertions, 6 deletions
diff --git a/NEWS b/NEWS
index b54e8bd4..3752c90f 100644
--- a/NEWS
+++ b/NEWS
@@ -3,6 +3,7 @@ serd (0.29.3) unstable;
* Clarify errors returned by serd_env_expand()
* Fix reported error when reading statements with only a blank node
* Fix parsing local names that end with escaped dots
+ * Improve relative URI serialisation
* Add serdi option to write ASCII output
* Make serdi guess input syntax from extension if unspecified
* Make serdi syntax options case-insensitive
diff --git a/src/serd_internal.h b/src/serd_internal.h
index cfcdc82c..52dd31ed 100644
--- a/src/serd_internal.h
+++ b/src/serd_internal.h
@@ -431,9 +431,9 @@ uri_path_at(const SerdURI* uri, size_t i)
}
}
-/** Return true iff `uri` is within the base of `root` */
+/** Return true iff `uri` shares path components with `root` */
static inline bool
-uri_is_under(const SerdURI* uri, const SerdURI* root)
+uri_is_related(const SerdURI* uri, const SerdURI* root)
{
if (!root || !root->scheme.len ||
!chunk_equals(&root->scheme, &uri->scheme) ||
@@ -456,6 +456,13 @@ uri_is_under(const SerdURI* uri, const SerdURI* root)
return true;
}
+/** Return true iff `uri` is within the base of `root` */
+static inline bool
+uri_is_under(const SerdURI* uri, const SerdURI* root)
+{
+ return uri->path.len >= root->path.len && uri_is_related(uri, root);
+}
+
static inline bool
is_uri_scheme_char(const uint8_t c)
{
diff --git a/src/uri.c b/src/uri.c
index 7b172e42..e6f03547 100644
--- a/src/uri.c
+++ b/src/uri.c
@@ -433,8 +433,6 @@ write_rel_path(SerdSink sink,
if (i == path_len && i == base_len) { // Paths are identical
return 0;
- } else if (last_shared_sep == 0) { // No common components
- return write_path_tail(sink, stream, uri, 0);
}
// Find the number of up references ("..") required
@@ -451,6 +449,10 @@ write_rel_path(SerdSink sink,
len += sink("../", 3, stream);
}
+ if (last_shared_sep == 0 && up == 0) {
+ len += sink("/", 1, stream);
+ }
+
// Write suffix
return len += write_path_tail(sink, stream, uri, last_shared_sep + 1);
}
@@ -464,8 +466,10 @@ serd_uri_serialise_relative(const SerdURI* uri,
SerdSink sink,
void* stream)
{
- size_t len = 0;
- const bool relative = uri_is_under(uri, root ? root : base);
+ size_t len = 0;
+ const bool relative =
+ root ? uri_is_under(uri, root) : uri_is_related(uri, base);
+
if (relative) {
len = write_rel_path(sink, stream, uri, base);
}
diff --git a/tests/serd_test.c b/tests/serd_test.c
index b80c8b82..945fefe3 100644
--- a/tests/serd_test.c
+++ b/tests/serd_test.c
@@ -408,6 +408,18 @@ main(void)
FAILF("Bad relative URI %s (expected '/foo/bar')\n", rel.buf);
}
+ SerdNode up = serd_node_new_relative_uri(&base_uri, &abs_uri, NULL, NULL);
+ if (strcmp((const char*)up.buf, "../")) {
+ FAILF("Bad relative URI %s (expected '../')\n", up.buf);
+ }
+
+ SerdNode noup = serd_node_new_relative_uri(&base_uri, &abs_uri, &abs_uri, NULL);
+ if (strcmp((const char*)noup.buf, "http://example.org/")) {
+ FAILF("Bad relative URI %s (expected 'http://example.org/')\n", noup.buf);
+ }
+
+ serd_node_free(&noup);
+ serd_node_free(&up);
serd_node_free(&rel);
serd_node_free(&base);