| 1 | # ----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | =encoding utf8 | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | =head1 NAME | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | Quiq::UrlObj - URL Klasse | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | =head1 BASE CLASS | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | L<Quiq::Hash> | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | Ein Objekt der Klasse repräsentiert einen URL. Auf dessen Bestandteilen | ||||||||||||||||||||||||||||
| 16 | kann mit den Objektmethoden der Klasse operiert werden. Ferner enthält | ||||||||||||||||||||||||||||
| 17 | die Klasse allgemeine Methoden im Zusammenhang mit URLs, die als | ||||||||||||||||||||||||||||
| 18 | Klassenmethoden implementiert sind. | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | =cut | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | # ----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | package Quiq::UrlObj; | ||||||||||||||||||||||||||||
| 25 | 1 | ▶ | use base qw/Quiq::Hash/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 1 | ▶ | use v5.10; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 1 | our $VERSION = '1.240'; | |||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 1 | ▶ | use Quiq::Url; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 34 | 1 | ▶ | use Quiq::Hash::Ordered; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 35 | 1 | ▶ | use Scalar::Util (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | # ----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | =head1 METHODS | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | =head2 Konstruktor | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | =head3 new() - Instantiiere Objekt | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | =head4 Synopsis | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | $urlObj = $class->new; | ||||||||||||||||||||||||||||
| 48 | $urlObj = $class->new($url); | ||||||||||||||||||||||||||||
| 49 | $urlObj = $class->new(@keyVal); | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | =head4 Description | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | Instantiiere ein Objekt der Klasse und liefere eine Referenz auf | ||||||||||||||||||||||||||||
| 54 | dieses Objekt zurück. | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | =cut | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | # ----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 3 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 61 | 3 | my $class = shift; | |||||||||||||||||||||||||||
| 62 | # @_: $url -or- @keyVal | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 3 | my ($schema,$user,$password,$host,$port,$path,$fragment) = ('') x 8; | |||||||||||||||||||||||||||
| 65 | 3 | ▶ | if (@_ == 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | # $url | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 1 | my $url = shift; | |||||||||||||||||||||||||||
| 69 | 1 | ($schema,$user,$password,$host,$port,$path,my $query,$fragment) = | |||||||||||||||||||||||||||
| 70 | Quiq::Url->split($url); | ||||||||||||||||||||||||||||
| 71 | 1 | @_ = map {Quiq::Url->decode($_)} | |||||||||||||||||||||||||||
| 72 | Quiq::Url->queryDecode($query); | ||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 3 | my $self = $class->SUPER::new( | |||||||||||||||||||||||||||
| 76 | schema => $schema, | ||||||||||||||||||||||||||||
| 77 | user => $user, | ||||||||||||||||||||||||||||
| 78 | password => $password, | ||||||||||||||||||||||||||||
| 79 | host => $host, | ||||||||||||||||||||||||||||
| 80 | port => $port, | ||||||||||||||||||||||||||||
| 81 | path => $path, | ||||||||||||||||||||||||||||
| 82 | queryH => Quiq::Hash::Ordered->new, | ||||||||||||||||||||||||||||
| 83 | fragment => $fragment, | ||||||||||||||||||||||||||||
| 84 | ); | ||||||||||||||||||||||||||||
| 85 | 3 | $self->setQuery(@_); | |||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 3 | return $self; | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | # ----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | =head2 Objektmethoden | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | =head3 queryString() - liefere Querystring des URL-Objekts | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | =head4 Synopsis | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | $query = $urlObj->queryString; | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | =head4 Returns | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | (String) Querystring | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | =head4 Description | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | Erzeuge den Querystring des URL-Objekts und liefere diesen zurück. | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | =cut | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | # ----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 3 | ▶ | sub queryString { | ||||||||||||||||||||||||||
|
Subroutine
queryString: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 113 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 3 | my $queryH = $self->queryH; | |||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 3 | my @keyVal; | |||||||||||||||||||||||||||
| 118 | 3 | for my $key ($queryH->keys) { | |||||||||||||||||||||||||||
| 119 | 5 | my $arr = $queryH->get($key); | |||||||||||||||||||||||||||
| 120 | 5 | for my $val (@$arr) { | |||||||||||||||||||||||||||
| 121 | 7 | push @keyVal,$key=>$val; | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 3 | return Quiq::Url->queryEncode(@keyVal); | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | # ----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | =head3 setQuery() - Setze Querystring-Parameter des URL-Objekts | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | =head4 Synopsis | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | $urlObj = $urlObj->setQuery(@keyVal); | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | =head4 Arguments | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | =over 4 | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | =item @keyVal | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | Liste von Schlüssel-Wert-Paaren | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | =back | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | =head4 Returns | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | (Object) Das URl-Objekt (für Methodenverkettung) | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | =head4 Description | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | Setze die angegebenen Querystring-Parameter auf den jeweils angegebenen | ||||||||||||||||||||||||||||
| 153 | Wert. Existiert ein Parameter bereits, wird sein Wert überschrieben. | ||||||||||||||||||||||||||||
| 154 | Tritt derselbe Parameter mehrfach auf, werden die einzelnen Werte zu | ||||||||||||||||||||||||||||
| 155 | einem Array zusammengefasst. Ist der Wert eine Arrayreferenz, werden | ||||||||||||||||||||||||||||
| 156 | alle Werte des Arrays dem Parameter hinzugefügt. | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | =cut | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | # ----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 3 | ▶ | sub setQuery { | ||||||||||||||||||||||||||
|
Subroutine
setQuery: called CC=6 SCAR=18.7
Pod
documented
| |||||||||||||||||||||||||||||
| 163 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 164 | # @_ @keyVal | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 3 | my $queryH = $self->queryH; | |||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 3 | my %seen; | |||||||||||||||||||||||||||
| 169 | 3 | ▶ | while (@_) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 170 | 7 | my $key = shift; | |||||||||||||||||||||||||||
| 171 | 7 | my $val = shift; | |||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | 7 | ▶ | if (!defined $val) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 174 | # undef -> lösche Parameter | ||||||||||||||||||||||||||||
| 175 | 0 | delete $seen{$key}; | |||||||||||||||||||||||||||
| 176 | 0 | $queryH->delete($key); | |||||||||||||||||||||||||||
| 177 | 0 | next; | |||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||
| 179 | elsif (!$seen{$key}++) { | ||||||||||||||||||||||||||||
| 180 | # neuen Parameter hinzufügen | ||||||||||||||||||||||||||||
| 181 | 5 | $queryH->set($key=>[]); | |||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | # Wert(e) zur Liste der Parameterwerte hinzufügen | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | 7 | my $arr = $queryH->get($key); | |||||||||||||||||||||||||||
| 187 | 7 | ▶ | my $type = Scalar::Util::reftype($val) // ''; | ||||||||||||||||||||||||||
|
Condition50%1/2
builtin::reftype($val) // ""
MC/DC0%0/1
builtin::reftype($val) // ""
builtin::reftype($val)
Truth table50%1/2
A
A= builtin::reftype($val)
| |||||||||||||||||||||||||||||
| 188 | 7 | ▶ | push @$arr,$type eq 'ARRAY'? @$val: $val; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | 3 | return $self; | |||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | # ----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | =head3 url() - URL als Zeichenkette | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | =head4 Synopsis | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | $url = $urlObj->url; | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | =head4 Returns | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | (String) URL als Zeichenkette | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | =head4 Description | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | Erzeuge eine externe Repräsentation des URL-Objekts und liefere | ||||||||||||||||||||||||||||
| 209 | diese zurück. | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | =cut | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | # ----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | 3 | ▶ | sub url { | ||||||||||||||||||||||||||
|
Subroutine
url: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||||||||||||||||||
| 216 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | 3 | my $schema = $self->schema; | |||||||||||||||||||||||||||
| 219 | 3 | my $user = $self->user; | |||||||||||||||||||||||||||
| 220 | 3 | my $password = $self->password; | |||||||||||||||||||||||||||
| 221 | 3 | my $host = $self->host; | |||||||||||||||||||||||||||
| 222 | 3 | my $port = $self->port; | |||||||||||||||||||||||||||
| 223 | 3 | my $path = $self->path; | |||||||||||||||||||||||||||
| 224 | 3 | my $query = $self->queryString; | |||||||||||||||||||||||||||
| 225 | 3 | my $fragment = $self->fragment; | |||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | 3 | my $url = $schema; | |||||||||||||||||||||||||||
| 228 | 3 | ▶ | $url .= '://' if $url; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 229 | 3 | $url .= $user; | |||||||||||||||||||||||||||
| 230 | 3 | ▶ | $url .= ":$password" if $password; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 231 | 3 | ▶ | if ($user && $host) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$user and $host
MC/DC50%1/2
$user and $host
$user
$host
Truth table66%2/3
A and B
A= $userB= $host
| |||||||||||||||||||||||||||||
| 232 | 1 | $url .= '@'; | |||||||||||||||||||||||||||
| 233 | } | ||||||||||||||||||||||||||||
| 234 | 3 | $url .= $host; | |||||||||||||||||||||||||||
| 235 | 3 | ▶ | $url .= ":$port" if $port; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 236 | 3 | $url .= $path; | |||||||||||||||||||||||||||
| 237 | 3 | ▶ | $url .= "?$query" if $query; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 238 | 3 | ▶ | $url .= "#$fragment" if $fragment; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||
| 240 | 3 | return $url; | |||||||||||||||||||||||||||
| 241 | } | ||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | # ----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | =head1 VERSION | ||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||
| 247 | 1.240 | ||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||
| 251 | Frank Seitz, L<http://fseitz.de/> | ||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||
| 253 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||
| 255 | Copyright (C) 2026 Frank Seitz | ||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||
| 257 | =head1 LICENSE | ||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | This code is free software; you can redistribute it and/or modify | ||||||||||||||||||||||||||||
| 260 | it under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||
| 262 | =cut | ||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||
| 264 | # ----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||
| 266 | 1 | 1; | |||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||
| 268 | # eof | ||||||||||||||||||||||||||||