File 0918-stdlib-Fix-path-handling-in-uri_string-recompose-1.patch of Package erlang

From 40efd733e0fc643668a368e3f2860583d7ceb521 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?P=C3=A9ter=20Dimitrov?= <peterdmv@erlang.org>
Date: Tue, 30 Jun 2020 12:49:12 +0200
Subject: [PATCH] stdlib: Fix path handling in uri_string:recompose/1

This change fixes the handling of the path URI component during
recomposition.

Calling uri_string:recompose/1 with an input map that contains a host
component and a path component that does not start with a slash ("/"),
will prefix the path with a slash, turning it into an absolute path.
---
 lib/stdlib/src/uri_string.erl        | 28 ++++++++++++++++++++++++++++
 lib/stdlib/test/uri_string_SUITE.erl | 14 ++++++++++++--
 2 files changed, 40 insertions(+), 2 deletions(-)

diff --git a/lib/stdlib/src/uri_string.erl b/lib/stdlib/src/uri_string.erl
index 2ee0ba53b9..0b84a8a91d 100644
--- a/lib/stdlib/src/uri_string.erl
+++ b/lib/stdlib/src/uri_string.erl
@@ -1640,6 +1640,10 @@ update_port(#{}, URI) ->
 
 update_path(#{path := Path}, empty) ->
     encode_path(Path);
+update_path(#{host := _, path := Path0}, URI) ->
+    %% When host is present in a URI the path must begin with "/" or be empty.
+    Path = make_path_absolute(Path0),
+    concat(URI,encode_path(Path));
 update_path(#{path := Path}, URI) ->
     concat(URI,encode_path(Path));
 update_path(#{}, empty) ->
@@ -1717,6 +1721,31 @@ maybe_to_list(Comp) -> Comp.
 encode_port(Port) ->
     integer_to_binary(Port).
 
+%% URI           = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
+%%
+%% hier-part     = "//" authority path-abempty
+%%               / path-absolute
+%%               / path-rootless
+%%               / path-empty
+%%
+%% path          = path-abempty    ; begins with "/" or is empty
+%%               / path-absolute   ; begins with "/" but not "//"
+%%               / path-noscheme   ; begins with a non-colon segment
+%%               / path-rootless   ; begins with a segment
+%%               / path-empty      ; zero characters
+make_path_absolute(<<>>) ->
+    <<>>;
+make_path_absolute("") ->
+    "";
+make_path_absolute(<<"/",_/binary>> = Path) ->
+    Path;
+make_path_absolute([$/|_] = Path) ->
+    Path;
+make_path_absolute(Path) when is_binary(Path) ->
+    concat(<<$/>>, Path);
+make_path_absolute(Path) when is_list(Path) ->
+    concat("/", Path).
+
 %%-------------------------------------------------------------------------
 %% Helper functions for transcode
 %%-------------------------------------------------------------------------
diff --git a/lib/stdlib/test/uri_string_SUITE.erl b/lib/stdlib/test/uri_string_SUITE.erl
index eb59ea9b0c..51f6aac7ad 100644
--- a/lib/stdlib/test/uri_string_SUITE.erl
+++ b/lib/stdlib/test/uri_string_SUITE.erl
@@ -51,7 +51,8 @@
          compose_query/1, compose_query_latin1/1, compose_query_negative/1,
          dissect_query/1, dissect_query_negative/1,
          interop_query_latin1/1, interop_query_utf8/1,
-         regression_parse/1, regression_recompose/1, regression_normalize/1
+         regression_parse/1, regression_recompose/1, regression_normalize/1,
+         recompose_host_relative_path/1
         ]).
 
 
@@ -144,7 +145,8 @@ all() ->
      interop_query_utf8,
      regression_parse,
      regression_recompose,
-     regression_normalize
+     regression_normalize,
+     recompose_host_relative_path
     ].
 
 groups() ->
@@ -1246,3 +1248,11 @@ regression_normalize(_Config) ->
         uri_string:normalize(#{host => "ö",path => [],scheme => "FOo"}),
     #{host := "ö",path := [],scheme := "foo"} =
         uri_string:normalize(#{host => "ö",path => [],scheme => "FOo"}, [return_map]).
+
+recompose_host_relative_path(_Config) ->
+    "//example.com/.foo" =
+        uri_string:recompose(#{host => "example.com", path => ".foo"}),
+    <<"//example.com/foo">> =
+        uri_string:recompose(#{host => <<"example.com">>, path => <<"foo">>}),
+    ok.
+
-- 
2.26.2

openSUSE Build Service is sponsored by