Quiq/Http/Client.pm

filter: Statementstmt 37.8% 28 / 74 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 75.0% 9 / 12 Podpod 100% 3 / 3 totaltotal 34.4% 40 / 116 CC 12 SCAR 44.1
CC
12
Cov
29%
sendReceive
156.0
get
2.0
post
2.0
CRAP 82.0
« Quiq/Sql.pm ↑ Summary Quiq/Cascm.pm »
1 # -----------------------------------------------------------------------------
2  
3 =encoding utf8
4  
5 =head1 NAME
6  
7 Quiq::Http::Client - HTTP-Client
8  
9 =head1 BASE CLASS
10  
11 L<Quiq::Object>
12  
13 =head1 DESCRIPTION
14  
15 Die Klasse implementiert einen HTTP-Client für GET- und POST-Requests.
16  
17 Die zentrale Methode ist $class->L<sendReceive|"sendReceive() - Führe HTTP-Request aus">(). Diese sendet eine
18 HTTP-Request-Message, die der Aufrufer konfiguriert hat, an einen
19 Server (URL), und gibt die HTTP-Response-Message des Servers zurück.
20 Die Methode beherrscht GET- und POSTS-Requests. Auf ihrer Basis
21 sind die spezielleren Methoden $class->L<get|"get() - Führe GET-Request aus">() und $class->L<post|"post() - Führe POST-Request aus">()
22 implementiert. Die HTTP-Messages, sowohl gesendete als auch empfangene,
23 sind Instanzen der Klasse Quiq::HTTP::Message.
24  
25 =head1 EXAMPLE
26  
27 =head2 Universeller Client
28  
29 Siehe quiq-http-client
30  
31 =head2 GET-Request
32  
33 my $msg = Quiq::Http::Client->get($url);
34 print $msg->asString;
35  
36 =head2 POST-Request
37  
38 my $sMsg = Quiq::Http::Message->new(
39 contentType => 'text/plain',
40 contentLength => -1,
41 body => 'Dies ist ein Test',
42 );
43 my $rMsg = Quiq::Http::Client->put($url,$sMsg);
44 print $rMsg->asString;
45  
46 =cut
47  
48 # -----------------------------------------------------------------------------
49  
50 package Quiq::Http::Client;
51 1 ▶use base qw/Quiq::Object/;
Subroutine BEGIN: called CC=1 SCAR=0.0
52  
53 1 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
54 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
55 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
56  
57 1 our $VERSION = '1.240';
58  
59 1 ▶use Quiq::Option;
Subroutine BEGIN: called CC=1 SCAR=0.0
60 1 ▶use Quiq::Url;
Subroutine BEGIN: called CC=1 SCAR=0.0
61 1 ▶use Quiq::Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
62 1 ▶use Quiq::Http::Message;
Subroutine BEGIN: called CC=1 SCAR=0.0
63 1 ▶use Time::HiRes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
64  
65 # -----------------------------------------------------------------------------
66  
67 =head1 METHODS
68  
69 =head2 Klassenmethoden
70  
71 =head3 sendReceive() - Führe HTTP-Request aus
72  
73 =head4 Synopsis
74  
75 $rMsg = $class->sendReceive($op,$url,$sMsg,@opt);
76  
77 =head4 Arguments
78  
79 =over 4
80  
81 =item $op
82  
83 Die HTTP-Operation: 'post' oder 'get'.
84  
85 =item $url
86  
87 Der URL, gegen den die Operation ausgeführt wird.
88  
89 =item $sMsg
90  
91 Die HTTP-Nachricht, die gesendet wird. Dies ist eine Instanz der Klasse
92 Quiq::Http::Message.
93  
94 =back
95  
96 =head4 Options
97  
98 =over 4
99  
100 =item -debug => $bool (Default: 0)
101  
102 Gib die kommunizierten Daten (Request, Response) und Metainformation
103 (Zeitmessung) auf STDOUT aus.
104  
105 =item -redirect => $bool (Default: 1)
106  
107 Führe Redirects automatisch aus.
108  
109 =back
110  
111 =head4 Description
112  
113 Führe HTTP Request vom Typ $op gegen URL $url mit HTTP-Nachricht $sMsg aus
114 und liefere die vom Server gelieferte Antwort $rMsg zurück.
115  
116 =cut
117  
118 # -----------------------------------------------------------------------------
119  
120 0 ▶sub sendReceive {
Subroutine sendReceive: not called CC=12 SCAR=50.5
Pod documented
121 0 my $class = shift;
122 0 my $op = shift;
123 0 my $url = shift;
124 0 my $sMsg = shift;
125 # @_: @opt
126  
127 # Optionen
128  
129 0 my $debug = 0;
130 0 my $redirect = 1;
131  
132 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
133 0 Quiq::Option->extract([@_],
134 -debug => \$debug,
135 -redirect => \$redirect,
136 );
137 }
138  
139 # Zerlege URL
140  
141 0 my ($schema,$user,$passw,$host,$port,$path,$query,$fragment) =
142 Quiq::Url->split($url,-defaultSchema=>'http://');
143 0 ▶ $port ||= 80;
Condition0%0/2
$port ||= 80
l!l
00
MC/DC0%0/1
$port ||= 80
$port
Truth table0%0/2
A
A= $port
Aresult
11
00
144 0 ▶ $path ||= '/';
Condition0%0/2
$path ||= "/"
l!l
00
MC/DC0%0/1
$path ||= "/"
$path
Truth table0%0/2
A
A= $path
Aresult
11
00
145  
146 # Ermittele Host und Resource
147  
148 0 my $resource = $path;
149 0 ▶ if ($query ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($query ne "") 0 0 0
150 0 $resource .= "?$query";
151 }
152  
153 0 my $hostPort = $host;
154 0 ▶ if ($port ne '80') {
Branch: 1 branch
BranchTrueFalseTotal
if ($port ne 80) 0 0 0
155 0 $hostPort .= ":$port";
156 }
157  
158 # Setze HTTP/1.1 Pflich-Header Host:, UserAgent:
159  
160 0 my $ua =
161 $sMsg->set(
162 host => $hostPort,
163 userAgent => 'Perl HTTP Client',
164 connection => 'close',
165 );
166  
167 0 ▶ if ($user) {
Branch: 1 branch
BranchTrueFalseTotal
if ($user) 0 0 0
168 0 $sMsg->set(authorization=>"$user:$passw");
169 }
170  
171 # Erzeuge Request
172  
173 0 my $request = sprintf "%s %s HTTP/1.1\n%s",uc($op),$resource,
174 $sMsg->asString;
175  
176 0 my $t0;
177 0 my $w = 79;
178 0 ▶ if ($debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 0 0 0
179 0 printf "%s\n$request%s\n",'='x$w,'-'x$w;
180 0 $t0 = Time::HiRes::gettimeofday;
181 }
182  
183 # Baue Verbindung auf
184 0 my $sock = Quiq::Socket->new($host,$port);
185  
186 # Sende Request
187 0 print $sock $request;
188  
189 # Lies die Response
190 0 my $str = $sock->slurp;
191  
192 # Schließe Verbindung
193 0 $sock->close;
194  
195 0 ▶ if ($debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 0 0 0
196 0 printf "%s%s\n%.3f sec\n",$str,'~'x$w,
197 Time::HiRes::gettimeofday-$t0;
198 }
199  
200 # Instantiiere das Message-Objekt der Response
201 0 my $msg = Quiq::Http::Message->new(received=>1,\$str);
202  
203 # Führe Redirect aus
204  
205 0 ▶ if ($redirect && $msg->status =~ /^30[123]$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($redirect and $msg->status =~ /^30[123]$/) 0 0 0
Condition0%0/3
$redirect and $msg->status =~ /^30[123]$/
!ll&&!rl&&r
000
MC/DC0%0/2
$redirect and $msg->status =~ /^30[123]$/
$redirect $msg->status =~ /^30[123]$/
Truth table0%0/3
A and B
A= $redirectB= $msg->status =~ /^30[123]$/
ABresult
0X0
100
111
206 0 return $class->sendReceive($op,$msg->location,$sMsg,@_);
207 }
208  
209 0 ▶ if ($debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 0 0 0
210 0 printf "%s\n",'#'x$w;
211 }
212  
213 0 return $msg;
214 }
215  
216 # -----------------------------------------------------------------------------
217  
218 =head3 get() - Führe GET-Request aus
219  
220 =head4 Synopsis
221  
222 $msg = $class->get($url,@opt);
223  
224 =head4 Arguments
225  
226 =over 4
227  
228 =item $url
229  
230 Der URL, gegen den der GET-Request ausgeführt wird.
231  
232 =item $sMsg
233  
234 Die HTTP-Nachricht, die gesendet wird. Dies ist eine Instanz der Klasse
235 Quiq::Http::Message.
236  
237 =back
238  
239 =head4 Options
240  
241 Siehe Methode L<sendReceive|"sendReceive() - Führe HTTP-Request aus">().
242  
243 =head4 Description
244  
245 Führe HTTP POST-Request mit URL $url aus und liefere die vom Server
246 gelieferte Antwort zurück.
247  
248 =cut
249  
250 # -----------------------------------------------------------------------------
251  
252 0 ▶sub get {
Subroutine get: not called CC=1 SCAR=6.9
Pod documented
253 0 my $class = shift;
254 0 my $url = shift;
255 # @_: @opt
256  
257 # Bei einem GET-Request hat HTTP Request-Message
258 # keinen besonderen Inhalt.
259  
260 0 my $msg = Quiq::Http::Message->new;
261 0 return $class->sendReceive('get',$url,$msg,@_);
262 }
263  
264 # -----------------------------------------------------------------------------
265  
266 =head3 post() - Führe POST-Request aus
267  
268 =head4 Synopsis
269  
270 $rMsg = $class->post($url,$sMsg,@opt);
271  
272 =head4 Arguments
273  
274 =over 4
275  
276 =item $url
277  
278 Der URL, gegen den der GET-Request ausgeführt wird.
279  
280 =back
281  
282 =head4 Options
283  
284 Siehe Methode L<sendReceive|"sendReceive() - Führe HTTP-Request aus">().
285  
286 =head4 Description
287  
288 Führe HTTP POST-Request gegen URL $url und mit HTTP-Nachricht $sMsg aus
289 und liefere die vom Server gelieferte Antwort zurück.
290  
291 =cut
292  
293 # -----------------------------------------------------------------------------
294  
295 0 ▶sub post {
Subroutine post: not called CC=1 SCAR=6.9
Pod documented
296 0 my $class = shift;
297 0 my $url = shift;
298 0 my $msg = shift;
299 # @_: @opt
300  
301 0 return $class->sendReceive('post',$url,$msg,@_);
302 }
303  
304 # -----------------------------------------------------------------------------
305  
306 =head1 VERSION
307  
308 1.240
309  
310 =head1 AUTHOR
311  
312 Frank Seitz, L<http://fseitz.de/>
313  
314 =head1 COPYRIGHT
315  
316 Copyright (C) 2026 Frank Seitz
317  
318 =head1 LICENSE
319  
320 This code is free software; you can redistribute it and/or modify
321 it under the same terms as Perl itself.
322  
323 =cut
324  
325 # -----------------------------------------------------------------------------
326  
327 1 1;
328  
329 # eof
« Quiq/Sql.pm ↑ Summary Quiq/Cascm.pm »