Quiq/Http/Message.pm

filter: Statementstmt 18.8% 32 / 170 Branchbran 0.0% 0 / 78 Conditioncond 0.0% 0 / 10 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 29.4% 10 / 34 Podpod 100% 23 / 23 totaltotal 20.2% 65 / 321 CC 45 SCAR 51.8
CC
45
Cov
12%
fromString
506.0
asString
182.0
setCookie
30.0
CRAP 177.3
« Quiq/LaTeX/Document.pm ↑ Summary Quiq/TimeLapse/RangeDef.pm »
1 # -----------------------------------------------------------------------------
2  
3 =encoding utf8
4  
5 =head1 NAME
6  
7 Quiq::Http::Message - HTTP-Nachricht
8  
9 =head1 BASE CLASS
10  
11 L<Quiq::Hash>
12  
13 =head1 DESCRIPTION
14  
15 Ein Objekt dieser Klasse repräsentiert eine HTTP-Nachricht. Eine
16 HTTP-Nachricht besteht aus einer oder mehreren Kopfzeilen (Header) und
17 einem optionalen Rumpf (Body). Kopfzeilen und Rumpf werden von der Klasse
18 als Attribute implementiert, die durch Methoden der Klasse manipuliert
19 werden können. Eine HTTP-Nachricht, die versendet werden kann,
20 entsteht durch Erzeugen einer Stringrepräsentation des Objekts.
21  
22 Die Klasse kann für HTTP-Requests als auch für HTTP-Responses
23 verwendet werden. Ggf. müssen weitere Header eingeführt werden.
24  
25 MEMO: Die Klasse ist nach dem Vorbild von R1::HttpResponse entstanden.
26 Diese sollte so angepasst werden, dass sie Quiq::Http::Message
27 als Basisklasse verwendet.
28  
29 =head1 EXAMPLES
30  
31 =head2 Einfache HTTP-Nachricht
32  
33 my $msg = Quiq::Http::Message->new(
34 contentType => 'text/plain',
35 body => "Hello world\n"
36 );
37 print $msg->asString;
38  
39 generiert auf STDOUT
40  
41 Content-Type: text/plain
42 Content-Length: 12
43
44 Hello world
45  
46 =head2 HTTP-Nachricht über Socket schicken (siehe auch Quiq::Http::Client)
47  
48 my $sock = Quiq::Socket->new($host,$port);
49
50 my $msg = Quiq::Http::Message->new(
51 contentType => 'text/plain',
52 body => "Hello world\n"
53 );
54
55 print $sock $msg->asString;
56  
57 =head2 HTTP-Nachricht vom Server empfangen
58  
59 my $msg = Quiq::Http::Message->new(received=>1,$socket);
60 print $msg->asString;
61  
62 Die Setzung received=>1 bewirkt, dass wir bei der Auswertung der
63 Headerzeilen nicht strikt sind, d.h. bei unbekannten Headern wird
64 keine Exception geworfen, und die Methode $msg->L<asString|"asString() - Liefere HTTP-Nachricht als Zeichenkette">()
65 liefert die Headerinformation exakt so wie sie empfangen wurde, d.h.
66 sie wird nicht aus den Attributen gewonnen.
67  
68 =head2 HTTP-Nachricht aus Datei
69  
70 my $msg = Quiq::Http::Message->new('http/message01.txt');
71 print $msg->asString;
72  
73 =cut
74  
75 # -----------------------------------------------------------------------------
76  
77 package Quiq::Http::Message;
78 2 ▶use base qw/Quiq::Hash/;
Subroutine BEGIN: called CC=1 SCAR=0.0
79  
80 2 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
81 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
82 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
83 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
84  
85 2 our $VERSION = '1.240';
86  
87 2 ▶use Quiq::Reference;
Subroutine BEGIN: called CC=1 SCAR=0.0
88 2 ▶use Quiq::Http::Cookie;
Subroutine BEGIN: called CC=1 SCAR=0.0
89 2 ▶use Quiq::FileHandle;
Subroutine BEGIN: called CC=1 SCAR=0.0
90 2 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
91 2 ▶use MIME::Base64 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
92  
93 # -----------------------------------------------------------------------------
94  
95 =head1 METHODS
96  
97 =head2 Konstruktor
98  
99 =cut
100  
101 # -----------------------------------------------------------------------------
102  
103 # Private Methode zum Ermitteln/Setzen eines Attributs
104  
105 0 ▶my $GetSet = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
106 0 my $self = shift;
107 0 my $key = shift;
108 # @_: $val
109  
110 0 my $ref = \$self->{$key};
111 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
112 0 $$ref = shift;
113 }
114  
115 0 return $$ref;
116 2 };
117  
118 # -----------------------------------------------------------------------------
119  
120 =head3 new() - Instantiiere ein HTTP Nachrichten-Objekt
121  
122 =head4 Synopsis
123  
124 $http = $class->new(@keyVal);
125 $http = $class->new(@keyVal,$fh);
126 $http = $class->new(@keyVal,$file);
127 $http = $class->new(@keyVal,\$str);
128  
129 =head4 Returns
130  
131 Referenz auf HTTP-Objekt.
132  
133 =head4 Description
134  
135 Instantiiere ein HTTP Nachrichten-Objekt mit den Eigenschaften @keyVal.
136  
137 Folgende Eigenschaften können (u.a.) gesetzt werden:
138  
139 =over 2
140  
141 =item *
142  
143 contentType => $type
144  
145 =item *
146  
147 charset => $charset
148  
149 =item *
150  
151 contentLength => $n | -1
152  
153 =item *
154  
155 expires => $date | 'now' | 0
156  
157 =item *
158  
159 location => $url
160  
161 =item *
162  
163 setCookie => [$name=>$value,@keyVal]
164  
165 =item *
166  
167 refresh => [$n,$url]
168  
169 =item *
170  
171 body => $data
172  
173 =back
174  
175 Zum Setzen von Eigenschaften siehe auch die Methoden $msg->L<set|"set() - Setze Objekteigenschaften">()
176 und $msg->L<fromString|"fromString() - Setze Objektattribute aus Zeichenkette">().
177  
178 Ist eine ungerade Anzahl an Parametern angegeben, wird zunächst
179 die (ggf. leere) Liste von Attribut/Wert-Paaren @keyVal zugewiesen.
180 Alle weiteren Eigenschaften werden via Handle $fh, Datei $file
181 oder String $str gewonnen.
182  
183 =cut
184  
185 # -----------------------------------------------------------------------------
186  
187 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
188 0 my $class = shift;
189 # @_: @keyVal -or- @keyVal,$source -or- $source
190  
191 0 my $self = $class->SUPER::new(
192 received => 0,
193 protocol => undef,
194 status => undef,
195 statusText => undef,
196 contentType => undef,
197 transferEncoding => undef,
198 charset => undef,
199 contentLength => undef,
200 expires => undef,
201 location => undef,
202 refresh => [undef,undef],
203 setCookie => [],
204 host => undef,
205 connection => undef,
206 #wwwAuthenticate => undef,
207 authorization => undef,
208 userAgent => undef,
209 body => '',
210 );
211  
212 0 ▶ if (@_%2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ % 2) { } 0 0 0
213 # Ungerade Anzahl an Parametern. Letztes Argument ist die Quelle,
214 # aus der weitere Eigenschaften des Objekts gelesen werden.
215  
216 0 my $source = pop;
217 0 $self->set(@_);
218 0 $self->fromString($source);
219 }
220 else {
221 0 $self->set(@_);
222 }
223  
224 0 return $self;
225 }
226  
227 # -----------------------------------------------------------------------------
228  
229 =head2 Attribute
230  
231 =head3 set() - Setze Objekteigenschaften
232  
233 =head4 Synopsis
234  
235 $http->set(@keyVal);
236  
237 =head4 Returns
238  
239 Die Methode liefert keinen Wert zurück.
240  
241 =head4 Description
242  
243 Setze die Objekteigenschaften @keyVal. Für die Liste der
244 Eigenschaften siehe L<new|"new() - Instantiiere ein HTTP Nachrichten-Objekt">().
245  
246 =head4 Examples
247  
248 Ein HTTP-Request ohne Inhalt:
249  
250 $http->set(
251 host => $host,
252 connection => 'close',
253 );
254  
255 Eine HTTP-Response:
256  
257 $http->set(
258 contentType => 'text/html',
259 charset => 'utf-8',
260 setCookie => [id=>4711],
261 setCookie => [user=>'seitzf'],
262 body => "Test\n",
263 );
264  
265 =cut
266  
267 # -----------------------------------------------------------------------------
268  
269 0 ▶sub set {
Subroutine set: not called CC=3 SCAR=24.8
Pod documented
270 0 my $self = shift;
271 # @_: @keyVal
272  
273 0 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
274 0 my $key = shift;
275 0 my $val = shift;
276 0 ▶ $self->$key(Quiq::Reference->isArrayRef($val)? @$val: $val);
Branch: 1 branch
BranchTrueFalseTotal
'Quiq::Reference'->isArrayRef($val) ? : 0 0 0
277 }
278  
279 0 return;
280 }
281  
282 # -----------------------------------------------------------------------------
283  
284 =head2 Kopfzeilen (Header)
285  
286 Dieser Abschnitt beschreibt die Methoden zum Setzen und Abfragen
287 von Kopfzeilen. Generell gilt: Ist ein Argument angegeben, wird die
288 betreffende Kopfzeile gesetzt. Ist kein Argument angegeben, wird der
289 Wert der Kopfzeile geliefert.
290  
291 Der Name der Methode entspricht dem Namen der HTTP-Kopfzeile unter
292 Anwendung folgender Transformationsregeln:
293  
294 =over 2
295  
296 =item *
297  
298 der erste Buchstabe ist klein geschrieben
299  
300 =item *
301  
302 Bindestriche sind entfernt
303  
304 =item *
305  
306 mehrere Worte sind in "camel case" zusammengesetzt
307  
308 =back
309  
310 Beispiel: Die Kopfzeile "Content-Type" wird von der
311 Methode contentType() verwaltet.
312  
313 Der Wert einer Kopfzeilen-Methode ist nicht immer eine Zeichenkette.
314 Er kann auch eine Datenstruktur sein. Dies hängt von der jeweiligen
315 Kopfzeile ab. Im Skalarkontext wird eine Referenz auf die Datenstruktur
316 geliefert, im Array-Kontext die Liste der Elemente (siehe L<contentType|"contentType() - Setze/Liefere Content-Type Header">()
317 oder L<setCookie|"setCookie() - Setze/Liefere Set-Cookie Header">()).
318  
319 =head3 received() - Setze/Liefere received-Eigenschaft
320  
321 =head4 Synopsis
322  
323 $bool = $http->received($bool);
324 $bool = $http->received;
325  
326 =cut
327  
328 # -----------------------------------------------------------------------------
329  
330 0 ▶sub received {
Subroutine received: not called CC=1 SCAR=6.9
Pod documented
331 0 return shift->$GetSet('received',@_);
332 }
333  
334 # -----------------------------------------------------------------------------
335  
336 =head3 protocol() - Setze/Liefere Protokoll-Bezeichnung
337  
338 =head4 Synopsis
339  
340 $protocol = $http->protocol($protocol);
341 $protocol = $http->protocol;
342  
343 =head4 Description
344  
345 Die Protokoll-Bezeichnung steht in der ersten Zeile einer Server-Antwort
346 und hat die Form "HTTP/X.Y".
347  
348 =cut
349  
350 # -----------------------------------------------------------------------------
351  
352 0 ▶sub protocol {
Subroutine protocol: not called CC=1 SCAR=6.9
Pod documented
353 0 return shift->$GetSet('protocol',@_);
354 }
355  
356 # -----------------------------------------------------------------------------
357  
358 =head3 status() - Setze/Liefere HTTP-Status
359  
360 =head4 Synopsis
361  
362 $status = $http->status($status);
363 $status = $http->status;
364  
365 =head4 Description
366  
367 Der Status steht in der ersten Zeile einer Server-Antwort
368 und ist ein dreistelliger Code in der Form NNN.
369  
370 =cut
371  
372 # -----------------------------------------------------------------------------
373  
374 0 ▶sub status {
Subroutine status: not called CC=1 SCAR=6.9
Pod documented
375 0 return shift->$GetSet('status',@_);
376 }
377  
378 # -----------------------------------------------------------------------------
379  
380 =head3 statusText() - Setze/Liefere HTTP-StatusText
381  
382 =head4 Synopsis
383  
384 $statusText = $http->statusText($statusText);
385 $statusText = $http->statusText;
386  
387 =head4 Description
388  
389 Der StatusText steht in der ersten Zeile einer Server-Antwort
390 und ist eine textuelle Beschreibung des Statuscode.
391  
392 =cut
393  
394 # -----------------------------------------------------------------------------
395  
396 0 ▶sub statusText {
Subroutine statusText: not called CC=1 SCAR=6.9
Pod documented
397 0 return shift->$GetSet('statusText',@_);
398 }
399  
400 # -----------------------------------------------------------------------------
401  
402 =head3 contentType() - Setze/Liefere Content-Type Header
403  
404 =head4 Synopsis
405  
406 $type = $http->contentType($type);
407 $type = $http->contentType;
408  
409 =cut
410  
411 # -----------------------------------------------------------------------------
412  
413 0 ▶sub contentType {
Subroutine contentType: not called CC=1 SCAR=6.9
Pod documented
414 0 return shift->$GetSet('contentType',@_);
415 }
416  
417 # -----------------------------------------------------------------------------
418  
419 =head3 charset() - Setze/Liefere Charset
420  
421 =head4 Synopsis
422  
423 $charset = $http->charset($charset);
424 $charset = $http->charset;
425  
426 =head4 Description
427  
428 Setze/Liefere den Zeichensatz, der ergänzend im Content-Type
429 Header angegeben wird.
430  
431 =cut
432  
433 # -----------------------------------------------------------------------------
434  
435 0 ▶sub charset {
Subroutine charset: not called CC=1 SCAR=6.9
Pod documented
436 0 return shift->$GetSet('charset',@_);
437 }
438  
439 # -----------------------------------------------------------------------------
440  
441 =head3 authorization() - Setze/Liefere Authorization-Information
442  
443 =head4 Synopsis
444  
445 $userPass = $http->authorization($userPass);
446 $userPass = $http->authorization;
447  
448 =cut
449  
450 # -----------------------------------------------------------------------------
451  
452 0 ▶sub authorization {
Subroutine authorization: not called CC=1 SCAR=6.9
Pod documented
453 0 return shift->$GetSet('authorization',@_);
454 }
455  
456 # -----------------------------------------------------------------------------
457  
458 =head3 transferEncoding() - Setze/Liefere Transfer-Encoding
459  
460 =head4 Synopsis
461  
462 $val = $http->transferEncoding($val);
463 $val = $http->transferEncoding;
464  
465 =cut
466  
467 # -----------------------------------------------------------------------------
468  
469 0 ▶sub transferEncoding {
Subroutine transferEncoding: not called CC=1 SCAR=6.9
Pod documented
470 0 return shift->$GetSet('transferEncoding',@_);
471 }
472  
473 # -----------------------------------------------------------------------------
474  
475 =head3 contentLength() - Setze/Liefere Content-Length Header
476  
477 =head4 Synopsis
478  
479 $n = $http->contentLength($n);
480 $n = $http->contentLength;
481  
482 =cut
483  
484 # -----------------------------------------------------------------------------
485  
486 0 ▶sub contentLength {
Subroutine contentLength: not called CC=1 SCAR=6.9
Pod documented
487 0 return shift->$GetSet('contentLength',@_);
488 }
489  
490 # -----------------------------------------------------------------------------
491  
492 =head3 expires() - Setze/Liefere Expires Header
493  
494 =head4 Synopsis
495  
496 $val = $http->expires($val);
497 $val = $http->expires;
498  
499 =cut
500  
501 # -----------------------------------------------------------------------------
502  
503 0 ▶sub expires {
Subroutine expires: not called CC=1 SCAR=6.9
Pod documented
504 0 return shift->$GetSet('expires',@_);
505 }
506  
507 # -----------------------------------------------------------------------------
508  
509 =head3 host() - Setze/Liefere Header Host:
510  
511 =head4 Synopsis
512  
513 $host = $http->host($host);
514 $host = $http->host;
515  
516 =cut
517  
518 # -----------------------------------------------------------------------------
519  
520 0 ▶sub host {
Subroutine host: not called CC=1 SCAR=6.9
Pod documented
521 0 return shift->$GetSet('host',@_);
522 }
523  
524 # -----------------------------------------------------------------------------
525  
526 =head3 userAgent() - Setze/Liefere Wert von Header UserAgent:
527  
528 =head4 Synopsis
529  
530 $userAgent = $http->userAgent($userAgent);
531 $userAgent = $http->userAgent;
532  
533 =cut
534  
535 # -----------------------------------------------------------------------------
536  
537 0 ▶sub userAgent {
Subroutine userAgent: not called CC=1 SCAR=6.9
Pod documented
538 0 return shift->$GetSet('userAgent',@_);
539 }
540  
541 # -----------------------------------------------------------------------------
542  
543 =head3 connection() - Setze/Liefere Header Connection:
544  
545 =head4 Synopsis
546  
547 $val = $http->connection($val);
548 $val = $http->connection;
549  
550 =cut
551  
552 # -----------------------------------------------------------------------------
553  
554 0 ▶sub connection {
Subroutine connection: not called CC=1 SCAR=6.9
Pod documented
555 0 return shift->$GetSet('connection',@_);
556 }
557  
558 # -----------------------------------------------------------------------------
559  
560 =head3 location() - Setze/Liefere Location: Header
561  
562 =head4 Synopsis
563  
564 $url = $http->location($val);
565 $url = $http->location;
566  
567 =cut
568  
569 # -----------------------------------------------------------------------------
570  
571 0 ▶sub location {
Subroutine location: not called CC=1 SCAR=6.9
Pod documented
572 0 return shift->$GetSet('location',@_);
573 }
574  
575 # -----------------------------------------------------------------------------
576  
577 =head3 refresh() - Setze/Liefere Refresh-Header
578  
579 =head4 Synopsis
580  
581 $http->refresh($n);
582 $http->refresh($n,$url);
583 ($n,$url) = $http->refresh;
584 $arr = $http->refresh;
585  
586 =cut
587  
588 # -----------------------------------------------------------------------------
589  
590 0 ▶sub refresh {
Subroutine refresh: not called CC=4 SCAR=30.0
Pod documented
591 0 my $self = shift;
592 # @_: $n,$url
593  
594 0 my $arr = $self->$GetSet('refresh');
595 0 ▶ if (@_ == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ == 0) { } 0 0 0
elsif (@_ == 1) { } 0 0 0
596 0 ▶ return wantarray? @$arr: $arr;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
597 }
598 elsif (@_ == 1) {
599 0 $arr->[0] = shift;
600 }
601 else {
602 0 @$arr = @_;
603 }
604  
605 0 return;
606 }
607  
608 # -----------------------------------------------------------------------------
609  
610 =head3 setCookie() - Setze/Liefere Set-Cookie Header
611  
612 =head4 Synopsis
613  
614 $http->setCookie($name=>$value,@options);
615 @cookies = $http->setCookie;
616 $cookieA = $http->setCookie;
617  
618 =head4 Description
619  
620 Definiere Cookie $name mit Wert $value und Optionen @options.
621 Existiert Cookie $name bereits, wird seine Definition überschrieben.
622 Die Methode liefert beim Setzen keinen Wert zurück.
623  
624 Ohne Parameter gerufen liefert die Methode die Liste der
625 Cookie-Objekte zurück. Im Skalarkontext wird eine Referenz auf
626 die Liste geliefert.
627  
628 =head4 Example
629  
630 Generiere Id und setze permanenten Cookie, der nach 5 Jahren abläuft:
631  
632 $id = Quiq::Converter->intToWord(time);
633 $http->setCookie(
634 id => $id,
635 expires => '+5y',
636 );
637  
638 =cut
639  
640 # -----------------------------------------------------------------------------
641  
642 0 ▶sub setCookie {
Subroutine setCookie: not called CC=5 SCAR=34.0
Pod documented
643 0 my $self = shift;
644 # @_: $name=>$value,@options
645  
646 0 my $arr = $self->$GetSet('setCookie');
647 0 ▶ if (@_ == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 0) 0 0 0
648 0 ▶ return wantarray? @$arr: $arr;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
649 }
650  
651 # Wir prüfen, ob ein Cookie des angegebenen Namens bereits existiert.
652 # Wenn ja, ersetzen wir ihn. Wenn nein, fügen wir ihn hinzu.
653  
654 0 my $cok = Quiq::Http::Cookie->new(@_);
655 0 ▶ for (my $i = 0; $i < @$arr; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$arr) 0 0 0
656 0 ▶ if ($arr->[$i]->get('name') eq $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arr->[$i]->get("name") eq $_[0]) 0 0 0
657 0 $arr->[$i] = $cok;
658 0 return;
659 }
660 }
661 0 push @$arr,$cok;
662  
663 0 return;
664 }
665  
666 # -----------------------------------------------------------------------------
667  
668 =head2 Rumpf (Body)
669  
670 Der Body der HTTP-Antwort ist per Default leer, d.h. sein Wert ist,
671 sofern beim Konstruktoraufruf nichts anderes angegeben wird,
672 ein Leerstring.
673  
674 Der Body kann gesetzt werden:
675  
676 $http->body($data);
677  
678 Oder er kann per Referenz "in place" manipuliert werden:
679  
680 $ref = $http->bodyRef;
681 $$ref =~ /__TIME__/strftime '%F %H:%M:%S %Z',localtime/eg;
682  
683 Sein Wert wird geliefert durch:
684  
685 $data = $http->body;
686  
687 =head3 body() - Setze/Liefere Body
688  
689 =head4 Synopsis
690  
691 $body = $http->body($body);
692 $body = $http->body;
693  
694 =cut
695  
696 # -----------------------------------------------------------------------------
697  
698 0 ▶sub body {
Subroutine body: not called CC=1 SCAR=6.9
Pod documented
699 0 return shift->$GetSet('body',@_);
700 }
701  
702 # -----------------------------------------------------------------------------
703  
704 =head3 bodyRef() - Liefere Referenz auf Body
705  
706 =head4 Synopsis
707  
708 $ref = $http->bodyRef;
709  
710 =cut
711  
712 # -----------------------------------------------------------------------------
713  
714 0 ▶sub bodyRef {
Subroutine bodyRef: not called CC=1 SCAR=6.9
Pod documented
715 0 return \shift->{'body'};
716 }
717  
718 # -----------------------------------------------------------------------------
719  
720 =head3 append() - Füge Daten zum Rumpf hinzu
721  
722 =head4 Synopsis
723  
724 $http->append($data);
725  
726 =cut
727  
728 # -----------------------------------------------------------------------------
729  
730 0 ▶sub append {
Subroutine append: not called CC=1 SCAR=6.9
Pod documented
731 0 my ($self,$data) = @_;
732  
733 0 my $ref = $self->bodyRef;
734 0 $$ref .= $data;
735  
736 0 return;
737 }
738  
739 # -----------------------------------------------------------------------------
740  
741 =head2 Externe Repräsentation
742  
743 =head3 fromString() - Setze Objektattribute aus Zeichenkette
744  
745 =head4 Synopsis
746  
747 $http->fromString($fh);
748 $http->fromString($file);
749 $http->fromString(\$str);
750  
751 =head4 Description
752  
753 Die Methode liest eine HTTP-Message als Zeichenkette ein, zerlegt sie
754 in ihre Bestandteile und weist die enthaltene Information den
755 Komponenten des Objektes zu.
756  
757 Als Quelle kann eine Handle (Filehandle, Socket) eine Datei (Dateiname)
758 oder eine Zeichenkette (Skalar-Referenz) angegeben werden.
759  
760 =cut
761  
762 # -----------------------------------------------------------------------------
763  
764 0 ▶sub fromString {
Subroutine fromString: not called CC=22 SCAR=62.3
Pod documented
765 0 my ($self,$fh) = @_;
766  
767 0 my $received = $self->received;
768  
769 0 ▶ my $refType = Scalar::Util::reftype($fh) || '';
Condition0%0/2
builtin::reftype($fh) || ''
l!l
00
MC/DC0%0/1
builtin::reftype($fh) || ''
builtin::reftype($fh)
Truth table0%0/2
A
A= builtin::reftype($fh)
Aresult
11
00
770 0 ▶ if ($refType ne 'GLOB') {
Branch: 1 branch
BranchTrueFalseTotal
if ($refType ne "GLOB") 0 0 0
771 # $fh ist Dateiname oder Stringreferenz
772 0 $fh = Quiq::FileHandle->new('<',$fh);
773 }
774  
775 0 my $transferEncoding = '';
776  
777 0 ▶ while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 0 0 0
778 0 s/\r?\n$//;
779 0 ▶ if (/^$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^$/) 0 0 0
780 0 last;
781 }
782  
783 # Headerzeilen an Attribute zuweisen
784  
785 0 ▶ if (/^HTTP/) {
Branch: 7 branches
BranchTrueFalseTotal
if (/^HTTP/) { } 0 0 0
elsif (/^Status: (\d+) (.*)/i) { } 0 0 0
elsif (/^Content-Type: (.*)/i) { } 0 0 0
elsif (/^Location: (.*)/i) { } 0 0 0
elsif (/^Content-Length: (.*)/i and not $received) { } 0 0 0
elsif (/^Transfer-Encoding: (.*)/i and $received) { } 0 0 0
elsif (not $received) { } 0 0 0
786 # erste Zeile: PROTOCOL STATUS STATUSTEXT
787  
788 0 my ($protocol,$status,$statusText) = split ' ';
789 0 $self->protocol($protocol);
790 0 $self->status($status);
791 0 $self->statusText($statusText);
792  
793 0 next;
794 }
795 elsif (/^Status: (\d+) (.*)/i) {
796 0 $self->status($1);
797 0 $self->statusText($2);
798 }
799 elsif (/^Content-Type: (.*)/i) {
800 0 my $str = $1;
801  
802 0 $str =~ /([^;\s]+)/;
803 0 $self->contentType($1);
804  
805 0 ▶ if ($str =~ /charset=([^;\s]+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /charset=([^;\s]+)/) 0 0 0
806 0 $self->charset($1);
807 }
808 }
809 elsif (/^Location: (.*)/i) {
810 0 $self->location($1);
811 }
812 ▶ elsif (/^Content-Length: (.*)/i && !$received) {
Condition0%0/3
/^Content-Length: (.*)/i and not $received
!ll&&!rl&&r
000
MC/DC0%0/2
/^Content-Length: (.*)/i and not $received
/^Content-Length: (.*)/i not $received
Truth table0%0/3
A and not(B)
A= /^Content-Length: (.*)/iB= not $received
ABresult
0X0
100
111
813 0 $self->contentLength($1);
814 }
815 ▶ elsif (/^Transfer-Encoding: (.*)/i && $received) {
Condition0%0/3
/^Transfer-Encoding: (.*)/i and $received
!ll&&!rl&&r
000
MC/DC0%0/2
/^Transfer-Encoding: (.*)/i and $received
/^Transfer-Encoding: (.*)/i $received
Truth table0%0/3
A and B
A= /^Transfer-Encoding: (.*)/iB= $received
ABresult
0X0
100
111
816 0 $transferEncoding = $1;
817 }
818 # Date:
819 # Pragma:
820 # Connection:
821 # Set-Cookie:
822 # Expires:
823 # Server:
824 # Cache-Control:
825 # Accept-Ranges:
826 # Alternate-Protocol:
827 # Vary:
828 # P3P:
829 # X-(.*?):
830 elsif (!$received) {
831 0 $self->throw(
832 'HTTP-00003: Unbekannte HTTP Headerzeile',
833 Line => $_,
834 );
835 }
836 }
837  
838 0 my $body = '';
839 0 ▶ if ($transferEncoding =~ /chunked/i) {
Branch: 2 branches
BranchTrueFalseTotal
if ($transferEncoding =~ /chunked/i) { } 0 0 0
elsif (not $transferEncoding) { } 0 0 0
840 0 ▶ while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 0 0 0
841 # Länge Chunk
842  
843 0 s/\r?\n$//;
844 0 my $n = hex;
845 0 ▶ if ($n == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n == 0) 0 0 0
846 0 scalar <$fh>; # \r\n am Schluss überlesen
847 0 last;
848 }
849  
850 # Chunk lesen
851  
852 0 my $r = read $fh,(my $data),$n;
853 0 ▶ if (!$r) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($r) 0 0 0
854 0 last;
855 }
856 0 $body .= $data;
857  
858 # \r\n nach Chunk überlesen
859 0 read $fh,$data,2;
860 }
861 }
862 elsif (!$transferEncoding) {
863 0 ▶ while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 0 0 0
864 0 $body .= $_;
865 }
866 }
867 else {
868 0 $self->throw(
869 'HTTP-00001: Transfer-Encoding nicht unterstützt',
870 Value => $transferEncoding,
871 );
872 }
873 0 $fh->close;
874  
875 # Dekodiere Body
876  
877 0 ▶ my $charset = $self->charset || 'ISO-8859-1';
Condition0%0/2
$self->charset || 'ISO-8859-1'
l!l
00
MC/DC0%0/1
$self->charset || 'ISO-8859-1'
$self->charset
Truth table0%0/2
A
A= $self->charset
Aresult
11
00
878 0 $self->body(Encode::decode($charset,$body));
879  
880 0 return;
881 }
882  
883 # -----------------------------------------------------------------------------
884  
885 =head3 asString() - Liefere HTTP-Nachricht als Zeichenkette
886  
887 =head4 Synopsis
888  
889 $str = $http->asString;
890  
891 =cut
892  
893 # -----------------------------------------------------------------------------
894  
895 0 ▶sub asString {
Subroutine asString: not called CC=13 SCAR=52.0
Pod documented
896 0 my $self = shift;
897  
898 0 my $str = '';
899 0 my $bodyRef = $self->bodyRef;
900  
901 # Host:
902  
903 0 ▶ if (my $host = $self->host) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $host = $self->host) 0 0 0
904 0 $str .= "Host: $host\n";
905 }
906  
907 # UserAgent:
908  
909 0 ▶ if (my $userAgent = $self->userAgent) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $userAgent = $self->userAgent) 0 0 0
910 0 $str .= "UserAgent: $userAgent\n";
911 }
912  
913 # Location:
914  
915 0 ▶ if (my $location = $self->location) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $location = $self->location) 0 0 0
916 0 $str .= "Location: $location\n";
917 }
918  
919 # Authorization:
920  
921 0 ▶ if (my $userPass = $self->authorization) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $userPass = $self->authorization) 0 0 0
922 0 $str .= sprintf "Authorization: Basic %s\n",
923 MIME::Base64::encode_base64($userPass,'');
924 }
925  
926 # Content-Type:
927  
928 0 ▶ if (my $type = $self->contentType) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $type = $self->contentType) 0 0 0
929 0 $str .= "Content-Type: $type";
930  
931 # charset=
932  
933 0 ▶ if (my $charset = $self->charset) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $charset = $self->charset) 0 0 0
934 0 $str .= "; charset=$charset";
935 }
936 0 $str .= "\n";
937 }
938  
939 # Content-Length: (bei -1 wird der Wert berechnet)
940  
941 0 my $len = $self->contentLength;
942 0 ▶ if (defined $len) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $len) 0 0 0
943 0 ▶ if ($len == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len == -1) 0 0 0
944 0 $len = bytes::length($$bodyRef);
945 }
946 0 $str .= sprintf "Content-Length: $len\n",;
947 }
948  
949 # Set-Cookie:
950  
951 0 for my $cok ($self->setCookie) {
952 0 $str .= sprintf "Set-Cookie: %s\n",$cok->asString;
953 }
954  
955 # Refresh:
956  
957 0 my ($n,$url) = $self->refresh;
958 0 ▶ if (defined $n) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $n) 0 0 0
959 # Frage: Ist $url erforderlich?
960 0 $str .= "Refresh: $n; url=$url\n";
961 }
962  
963 # Expires:
964  
965 0 ▶ if (my $expires = $self->expires) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $expires = $self->expires) 0 0 0
966 0 $str .= "Expires: $expires\n";
967 }
968  
969 # Connection:
970  
971 0 ▶ if (my $connection = $self->connection) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $connection = $self->connection) 0 0 0
972 0 $str .= "Connection: $connection\n";
973 }
974  
975 # Body
976  
977 0 $str .= "\n";
978 0 $str .= $$bodyRef;
979  
980 0 return $str;
981 }
982  
983 # -----------------------------------------------------------------------------
984  
985 =head1 VERSION
986  
987 1.240
988  
989 =head1 AUTHOR
990  
991 Frank Seitz, L<http://fseitz.de/>
992  
993 =head1 COPYRIGHT
994  
995 Copyright (C) 2026 Frank Seitz
996  
997 =head1 LICENSE
998  
999 This code is free software; you can redistribute it and/or modify
1000 it under the same terms as Perl itself.
1001  
1002 =cut
1003  
1004 # -----------------------------------------------------------------------------
1005  
1006 2 1;
1007  
1008 # eof
« Quiq/LaTeX/Document.pm ↑ Summary Quiq/TimeLapse/RangeDef.pm »