Packages

Join URL path segments and normalize the result.

Current section

Files

Jump to
url_join src url_join@protocol.erl
Raw

src/url_join@protocol.erl

-module(url_join@protocol).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/url_join/protocol.gleam").
-export([is_plain_protocol/1, normalize/1]).
-file("src/url_join/protocol.gleam", 44).
-spec only_slashes_or_empty(binary()) -> boolean().
only_slashes_or_empty(S) ->
_pipe = gleam@string:replace(S, <<"/"/utf8>>, <<""/utf8>>),
gleam@string:is_empty(_pipe).
-file("src/url_join/protocol.gleam", 33).
-spec is_plain_protocol(binary()) -> boolean().
is_plain_protocol(S) ->
case gleam@string:split_once(S, <<":"/utf8>>) of
{ok, {Before, After}} ->
(not gleam_stdlib:contains_string(Before, <<"/"/utf8>>) andalso not gleam_stdlib:contains_string(
Before,
<<":"/utf8>>
))
andalso only_slashes_or_empty(After);
{error, _} ->
false
end.
-file("src/url_join/protocol.gleam", 48).
-spec is_ipv6_host(binary()) -> boolean().
is_ipv6_host(S) ->
gleam_stdlib:string_starts_with(S, <<"["/utf8>>) andalso gleam_stdlib:contains_string(
S,
<<"]"/utf8>>
).
-file("src/url_join/protocol.gleam", 4).
-spec normalize(binary()) -> binary().
normalize(S) ->
case gleam_stdlib:string_starts_with(S, <<"file:///"/utf8>>) of
true ->
S;
false ->
case gleam_stdlib:string_starts_with(S, <<"file:"/utf8>>) of
true ->
<<"file:///"/utf8,
(url_join@slashes:trim_leading(
gleam@string:drop_start(S, 5)
))/binary>>;
false ->
case is_ipv6_host(S) of
true ->
S;
false ->
case gleam@string:split_once(S, <<"://"/utf8>>) of
{ok, _} ->
S;
{error, _} ->
case gleam@string:split_once(
S,
<<":"/utf8>>
) of
{ok, {Protocol, Rest}} ->
Rest_trimmed = url_join@slashes:trim_leading(
Rest
),
<<<<Protocol/binary, "://"/utf8>>/binary,
Rest_trimmed/binary>>;
{error, _} ->
S
end
end
end
end
end.