| 1 | # Copyrights 2007-2021 by [Mark Overmeer <markov@cpan.org>]. | ||
| 2 | # For other contributors see ChangeLog. | ||
| 3 | # See the manual pages for details on the licensing terms. | ||
| 4 | # Pod stripped from pm file by OODoc 2.02. | ||
| 5 | # This code is part of distribution XML-Compile-SOAP. Meta-POD processed | ||
| 6 | # with OODoc into POD and HTML manual-pages. See README.md | ||
| 7 | # Copyright Mark Overmeer. Licensed under the same terms as Perl itself. | ||
| 8 | |||
| 9 | package XML::Compile::SOAP::Trace; | ||
| 10 | use vars '$VERSION'; | ||
| 11 | $VERSION = '3.27'; | ||
| 12 | |||
| 13 | |||
| 14 | use warnings; | ||
| 15 | use strict; | ||
| 16 | |||
| 17 | use Log::Report 'xml-compile-soap', syntax => 'REPORT'; | ||
| 18 | # no syntax SHORT, because we have own error() | ||
| 19 | |||
| 20 | use IO::Handle; | ||
| 21 | |||
| 22 | my @xml_parse_opts = (load_ext_dtd => 0, recover => 1, no_network => 1); | ||
| 23 | |||
| 24 | |||
| 25 | sub new($) | ||
| 26 | { my ($class, $data) = @_; | ||
| 27 | bless $data, $class; | ||
| 28 | } | ||
| 29 | |||
| 30 | |||
| 31 | sub start() {shift->{start}} | ||
| 32 | |||
| 33 | |||
| 34 | sub date() {scalar localtime shift->start} | ||
| 35 | |||
| 36 | |||
| 37 | sub error(;$) | ||
| 38 | { my $self = shift; | ||
| 39 | my $errors = $self->{errors} ||= []; | ||
| 40 | |||
| 41 | foreach my $err (@_) | ||
| 42 | { $err = __$err unless ref $err; | ||
| 43 | $err = Log::Report::Exception->new(reason => 'ERROR', message => $err) | ||
| 44 | unless $err->isa('Log::Report::Exception'); | ||
| 45 | push @$errors, $err; | ||
| 46 | } | ||
| 47 | |||
| 48 | wantarray ? @$errors : $errors->[0]; | ||
| 49 | } | ||
| 50 | |||
| 51 | |||
| 52 | sub errors() { @{shift->{errors} || []} } | ||
| 53 | |||
| 54 | |||
| 55 | sub elapse($) | ||
| 56 | { my ($self, $kind) = @_; | ||
| 57 | defined $kind ? $self->{$kind.'_elapse'} : $self->{elapse}; | ||
| 58 | } | ||
| 59 | |||
| 60 | |||
| 61 | sub request() {shift->{http_request}} | ||
| 62 | |||
| 63 | |||
| 64 | sub response() {shift->{http_response}} | ||
| 65 | |||
| 66 | |||
| 67 | sub responseDOM() {shift->{response_dom}} | ||
| 68 | |||
| 69 | |||
| 70 | sub printTimings(;$) | ||
| 71 | { my ($self, $fh) = @_; | ||
| 72 | my $oldfh = $fh ? (select $fh) : undef; | ||
| 73 | print "Call initiated at: ",$self->date, "\n"; | ||
| 74 | print "SOAP call timing:\n"; | ||
| 75 | printf " encoding: %7.2f ms\n", $self->elapse('encode') *1000; | ||
| 76 | printf " stringify: %7.2f ms\n", $self->elapse('stringify') *1000; | ||
| 77 | printf " connection: %7.2f ms\n", $self->elapse('connect') *1000; | ||
| 78 | |||
| 79 | my $dp = $self->elapse('parse'); | ||
| 80 | if(defined $dp) {printf " parsing: %7.2f ms\n", $dp *1000 } | ||
| 81 | else {printf " parsing: - (no xml to parse)\n" } | ||
| 82 | |||
| 83 | my $dt = $self->elapse('decode'); | ||
| 84 | if(defined $dt) {printf " decoding: %7.2f ms\n", $dt *1000 } | ||
| 85 | else {print " decoding: - (no xml to convert)\n"} | ||
| 86 | |||
| 87 | my $el = $self->elapse; | ||
| 88 | printf " total time: %7.2f ms = %.3f seconds\n\n", $el*1000, $el | ||
| 89 | if defined $el; | ||
| 90 | |||
| 91 | select $oldfh if $oldfh; | ||
| 92 | } | ||
| 93 | |||
| 94 | |||
| 95 | sub printRequest(;$%) | ||
| 96 | { my $self = shift; | ||
| 97 | my $request = $self->request or return; | ||
| 98 | |||
| 99 | my $fh = @_%2 ? shift : *STDOUT; | ||
| 100 | my %args = @_; | ||
| 101 | |||
| 102 | my $format = $args{pretty_print} || 0; | ||
| 103 | if($format && $request->content_type =~ m/xml/i) | ||
| 104 | { $fh->print("\n", $request->headers->as_string, "\n"); | ||
| 105 | XML::LibXML | ||
| 106 | ->load_xml(string => $request->content, @xml_parse_opts) | ||
| 107 | ->toFH($fh, $format); | ||
| 108 | } | ||
| 109 | else | ||
| 110 | { my $req = $request->as_string; | ||
| 111 | $req =~ s/^/ /gm; | ||
| 112 | $fh->print("Request:\n$req\n"); | ||
| 113 | } | ||
| 114 | } | ||
| 115 | |||
| 116 | |||
| 117 | sub printResponse(;$%) | ||
| 118 | { my $self = shift; | ||
| 119 | my $resp = $self->response or return; | ||
| 120 | |||
| 121 | my $fh = @_%2 ? shift : *STDOUT; | ||
| 122 | my %args = @_; | ||
| 123 | |||
| 124 | my $format = $args{pretty_print} || 0; | ||
| 125 | if($format && $resp->content_type =~ m/xml/i) | ||
| 126 | { $fh->print("\n", $resp->headers->as_string, "\n"); | ||
| 127 | XML::LibXML->load_xml | ||
| 128 | ( string => ($resp->decoded_content || $resp->content) | ||
| 129 | , @xml_parse_opts | ||
| 130 | )->toFH($fh, $format); | ||
| 131 | } | ||
| 132 | else | ||
| 133 | { my $resp = $resp->as_string; | ||
| 134 | $resp =~ s/^/ /gm; | ||
| 135 | $fh->print("Response:\n$resp\n"); | ||
| 136 | } | ||
| 137 | } | ||
| 138 | |||
| 139 | |||
| 140 | sub printErrors(;$) | ||
| 141 | { my ($self, $fh) = @_; | ||
| 142 | $fh ||= *STDERR; | ||
| 143 | |||
| 144 | print $fh $_->toString for $self->errors; | ||
| 145 | |||
| 146 | if(my $d = $self->{decode_errors}) # Log::Report::Dispatcher::Try object | ||
| 147 | { print $fh "Errors while decoding:\n"; | ||
| 148 | foreach my $e ($d->exceptions) | ||
| 149 | { print $fh " ", $e->toString; | ||
| 150 | } | ||
| 151 | } | ||
| 152 | } | ||
| 153 | |||
| 154 | 1; |