NAME

Aion::Format::Url - utilities for encoding and decoding URLs

SYNOPSIS

use Aion::Format::Url;

to_url_params {a => 1, b => [[1,2],3,{x=>10}]} # => a&b[][]&b[][1]=2&b[1]=3&b[2][x]=10

normalize_url "?x", "http://load.er/fix/mix?y=6"  # => http://load.er/fix/mix?x

DESCRIPTION

Utilities for encoding and decoding URLs.

SUBROUTINES

to_url_param (;$scalar)

Escapes $scalar for the search part of the URL.

to_url_param "a b" # => a+b

[map to_url_param, "a b", "🦁"] # --> [qw/a+b %F0%9F%A6%81/]

to_url_params (;$hash_ref)

Generates the search portion of the URL.

local $_ = {a => 1, b => [[1,2],3,{x=>10}]};
to_url_params  # => a&b[][]&b[][1]=2&b[1]=3&b[2][x]=10
1. Keys with undef values are discarded.
2. The value 1 is used for a key without a value.
3. Keys are converted in alphabetical order.
to_url_params {k => "", n => undef, f => 1}  # => f&k=

from_url_params (;$scalar)

Parses the search part of the URL.

local $_ = 'a&b[][]&b[][1]=2&b[1]=3&b[2][x]=10';
from_url_params  # --> {a => 1, b => [[1,2],3,{x=>10}]}

from_url_param (;$scalar)

Used to parse keys and values in a URL parameter.

Reverse to to_url_param.

local $_ = to_url_param '↬';
from_url_param  # => ↬

parse_url ($url, $onpage, $dir)

Parses and normalizes URLs.

  • $url - URL or part of it to be parsed.

  • $onpage is the URL of the page with $url. If $url is not complete, then it is completed from here. Optional. By default it uses the $onpage = 'off://off' configuration.

  • $dir (bool): 1 - normalize the URL path with a "/" at the end if it is a directory. 0 - without "/".

my $res = {
    proto  => "off",
    dom    => "off",
    domain => "off",
    link   => "off://off",
    orig   => "",
    onpage => "off://off",
};

parse_url "" # --> $res

$res = {
    proto  => "https",
    dom    => "main.com",
    domain => "www.main.com",
    path   => "/page",
    dir    => "/page/",
    link   => "https://main.com/page",
    orig   => "/page",
    onpage => "https://www.main.com/pager/mix",
};

parse_url "/page", "https://www.main.com/pager/mix"   # --> $res

$res = {
    proto  => "https",
    user   => "user",
    pass   => "pass",
    dom    => "x.test",
    domain => "www.x.test",
    path   => "/path",
    dir    => "/path/",
    query  => "x=10&y=20",
    hash   => "hash",
    link   => 'https://user:pass@x.test/path?x=10&y=20#hash',
    orig   => 'https://user:pass@www.x.test/path?x=10&y=20#hash',
    onpage => "off://off",
};
parse_url 'https://user:pass@www.x.test/path?x=10&y=20#hash'  # --> $res

normalize_url ($url, $onpage, $dir)

Normalizes the URL.

Uses parse_url and returns a link.

normalize_url ""   # => off://off
normalize_url "www.fix.com"  # => off://off/www.fix.com
normalize_url ":"  # => off://off/:
normalize_url '@'  # => off://off/@
normalize_url "/"  # => off://off
normalize_url "//" # => off://
normalize_url "?"  # => off://off
normalize_url "#"  # => off://off

normalize_url "/dir/file", "http://www.load.er/fix/mix"  # => http://load.er/dir/file
normalize_url "dir/file", "http://www.load.er/fix/mix"  # => http://load.er/fix/mix/dir/file
normalize_url "?x", "http://load.er/fix/mix?y=6"  # => http://load.er/fix/mix?x

SEE ALSO

AUTHOR

Yaroslav O. Kosmina mailto:darviarush@mail.ru

LICENSE

GPLv3

COPYRIGHT

The Aion::Format::Url module is copyright © 2023 Yaroslav O. Kosmina. Rusland. All rights reserved.