From ce1399d1610771c5ecca49586dccd89e00427515 Mon Sep 17 00:00:00 2001 From: Karen Arutyunov Date: Wed, 29 May 2019 19:02:06 +0300 Subject: Inherit url type from butl::url --- libbpkg/manifest.cxx | 39 ++++++++++++++++++++++++++++++++----- libbpkg/manifest.hxx | 19 +++++++++++++++--- tests/manifest/testscript | 49 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 99 insertions(+), 8 deletions(-) diff --git a/libbpkg/manifest.cxx b/libbpkg/manifest.cxx index 139b7a8..55fe18f 100644 --- a/libbpkg/manifest.cxx +++ b/libbpkg/manifest.cxx @@ -598,6 +598,21 @@ namespace bpkg return *this; } + // url + // + url:: + url (const std::string& u, std::string c): butl::url (u), comment (move (c)) + { + if (rootless) + throw invalid_argument ("rootless URL"); + + if (casecmp (scheme, "file") == 0) + throw invalid_argument ("local URL"); + + if (!authority || authority->empty ()) + throw invalid_argument ("no authority"); + } + // depends // dependency_constraint:: @@ -1631,7 +1646,18 @@ namespace bpkg if (v.empty ()) bad_value (string ("empty ") + what + " url"); - return url (move (p.first), move (p.second)); + url r; + + try + { + r = url (p.first, move (p.second)); + } + catch (const invalid_argument& e) + { + bad_value (string ("invalid ") + what + " url: " + e.what ()); + } + + return r; }; auto flag = [fl] (package_manifest_flags f) @@ -2514,19 +2540,22 @@ namespace bpkg } if (m.url) - s.next ("url", serializer::merge_comment (*m.url, m.url->comment)); + s.next ("url", + serializer::merge_comment (m.url->string (), m.url->comment)); if (m.doc_url) s.next ("doc-url", - serializer::merge_comment (*m.doc_url, m.doc_url->comment)); + serializer::merge_comment (m.doc_url->string (), + m.doc_url->comment)); if (m.src_url) s.next ("src-url", - serializer::merge_comment (*m.src_url, m.src_url->comment)); + serializer::merge_comment (m.src_url->string (), + m.src_url->comment)); if (m.package_url) s.next ("package-url", - serializer::merge_comment (*m.package_url, + serializer::merge_comment (m.package_url->string (), m.package_url->comment)); if (m.email) diff --git a/libbpkg/manifest.hxx b/libbpkg/manifest.hxx index 59c0ba0..01ffa49 100644 --- a/libbpkg/manifest.hxx +++ b/libbpkg/manifest.hxx @@ -250,16 +250,29 @@ namespace bpkg }; // url + // doc-url + // src-url // package-url // - class url: public std::string + // URL that has the following constraints: + // + // - is not rootless + // - is not local (the scheme is not `file`) + // - authority is present and is not empty + // + // See libbutl/url.mxx for details. + // + class url: public butl::url { public: std::string comment; + // Throw invalid_argument on parsing or constraints checking error. + // explicit - url (std::string u = "", std::string c = "") - : std::string (std::move (u)), comment (std::move (c)) {} + url (const std::string& u, std::string c = ""); + + url () = default; }; // email diff --git a/tests/manifest/testscript b/tests/manifest/testscript index 96f8f31..e7a6af6 100644 --- a/tests/manifest/testscript +++ b/tests/manifest/testscript @@ -138,6 +138,55 @@ stdin:6:19: error: invalid package description file: invalid filesystem path %) EOE + + : url + : + { + : no-scheme + : + $* <>EOE != 0 + : 1 + src-url: libfoo + EOI + stdin:2:10: error: invalid src url: no scheme + EOE + + : rootless + : + $* <>EOE != 0 + : 1 + src-url: pkcs11:libfoo + EOI + stdin:2:10: error: invalid src url: rootless URL + EOE + + : local + : + $* <>EOE != 0 + : 1 + src-url: file:/libfoo/bar + EOI + stdin:2:10: error: invalid src url: local URL + EOE + + : authority-absent + : + $* <>EOE != 0 + : 1 + src-url: http:/libfoo/bar + EOI + stdin:2:10: error: invalid src url: no authority + EOE + + : authority-empty + : + $* <>EOE != 0 + : 1 + src-url: http:///libfoo/bar + EOI + stdin:2:10: error: invalid src url: no authority + EOE + } } : complete -- cgit v1.1