Filename | /var/www/foswikidev/core/lib/Foswiki/Contrib/JsonRpcContrib/Response.pm |
Statements | Executed 11 statements in 571µs |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
---|---|---|---|---|---|
1 | 1 | 1 | 16µs | 29µs | BEGIN@19 | Foswiki::Contrib::JsonRpcContrib::Response::
1 | 1 | 1 | 9µs | 36µs | BEGIN@22 | Foswiki::Contrib::JsonRpcContrib::Response::
1 | 1 | 1 | 9µs | 14µs | BEGIN@20 | Foswiki::Contrib::JsonRpcContrib::Response::
1 | 1 | 1 | 8µs | 37µs | BEGIN@25 | Foswiki::Contrib::JsonRpcContrib::Response::
1 | 1 | 1 | 4µs | 4µs | BEGIN@24 | Foswiki::Contrib::JsonRpcContrib::Response::
0 | 0 | 0 | 0s | 0s | code | Foswiki::Contrib::JsonRpcContrib::Response::
0 | 0 | 0 | 0s | 0s | encode | Foswiki::Contrib::JsonRpcContrib::Response::
0 | 0 | 0 | 0s | 0s | id | Foswiki::Contrib::JsonRpcContrib::Response::
0 | 0 | 0 | 0s | 0s | isError | Foswiki::Contrib::JsonRpcContrib::Response::
0 | 0 | 0 | 0s | 0s | json | Foswiki::Contrib::JsonRpcContrib::Response::
0 | 0 | 0 | 0s | 0s | message | Foswiki::Contrib::JsonRpcContrib::Response::
0 | 0 | 0 | 0s | 0s | new | Foswiki::Contrib::JsonRpcContrib::Response::
0 | 0 | 0 | 0s | 0s |
Line | State ments |
Time on line |
Calls | Time in subs |
Code |
---|---|---|---|---|---|
1 | # JSON-RPC for Foswiki | ||||
2 | # | ||||
3 | # Copyright (C) 2011-2015 Michael Daum http://michaeldaumconsulting.com | ||||
4 | # | ||||
5 | # This program is free software; you can redistribute it and/or | ||||
6 | # modify it under the terms of the GNU General Public License | ||||
7 | # as published by the Free Software Foundation; either version 2 | ||||
8 | # of the License, or (at your option) any later version. For | ||||
9 | # more details read LICENSE in the root of this distribution. | ||||
10 | # | ||||
11 | # This program is distributed in the hope that it will be useful, | ||||
12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||||
14 | # | ||||
15 | # As per the GPL, removal of this notice is prohibited. | ||||
16 | |||||
17 | package Foswiki::Contrib::JsonRpcContrib::Response; | ||||
18 | |||||
19 | 2 | 28µs | 2 | 42µs | # spent 29µs (16+13) within Foswiki::Contrib::JsonRpcContrib::Response::BEGIN@19 which was called:
# once (16µs+13µs) by Foswiki::Contrib::JsonRpcContrib::Server::BEGIN@26 at line 19 # spent 29µs making 1 call to Foswiki::Contrib::JsonRpcContrib::Response::BEGIN@19
# spent 13µs making 1 call to strict::import |
20 | 2 | 25µs | 2 | 18µs | # spent 14µs (9+4) within Foswiki::Contrib::JsonRpcContrib::Response::BEGIN@20 which was called:
# once (9µs+4µs) by Foswiki::Contrib::JsonRpcContrib::Server::BEGIN@26 at line 20 # spent 14µs making 1 call to Foswiki::Contrib::JsonRpcContrib::Response::BEGIN@20
# spent 4µs making 1 call to warnings::import |
21 | |||||
22 | 2 | 25µs | 2 | 63µs | # spent 36µs (9+27) within Foswiki::Contrib::JsonRpcContrib::Response::BEGIN@22 which was called:
# once (9µs+27µs) by Foswiki::Contrib::JsonRpcContrib::Server::BEGIN@26 at line 22 # spent 36µs making 1 call to Foswiki::Contrib::JsonRpcContrib::Response::BEGIN@22
# spent 27µs making 1 call to Exporter::import |
23 | |||||
24 | 2 | 22µs | 1 | 4µs | # spent 4µs within Foswiki::Contrib::JsonRpcContrib::Response::BEGIN@24 which was called:
# once (4µs+0s) by Foswiki::Contrib::JsonRpcContrib::Server::BEGIN@26 at line 24 # spent 4µs making 1 call to Foswiki::Contrib::JsonRpcContrib::Response::BEGIN@24 |
25 | 2 | 469µs | 2 | 66µs | # spent 37µs (8+29) within Foswiki::Contrib::JsonRpcContrib::Response::BEGIN@25 which was called:
# once (8µs+29µs) by Foswiki::Contrib::JsonRpcContrib::Server::BEGIN@26 at line 25 # spent 37µs making 1 call to Foswiki::Contrib::JsonRpcContrib::Response::BEGIN@25
# spent 29µs making 1 call to constant::import |
26 | |||||
27 | ################################################################################ | ||||
28 | sub new { | ||||
29 | my $class = shift; | ||||
30 | my $session = shift; | ||||
31 | |||||
32 | my $this = { | ||||
33 | session => $session, | ||||
34 | @_ | ||||
35 | }; | ||||
36 | |||||
37 | return bless( $this, $class ); | ||||
38 | } | ||||
39 | |||||
40 | ############################################################################## | ||||
41 | # static constructor | ||||
42 | sub print { | ||||
43 | my $class = shift; | ||||
44 | my $session = shift; | ||||
45 | |||||
46 | my $this = $class->new( $session, @_ ); | ||||
47 | my $response = $this->{session}->{response}; | ||||
48 | my $text = $this->encode(); | ||||
49 | my $hopts = { | ||||
50 | 'status' => $this->code() ? 500 : 200, | ||||
51 | 'Content-Type' => 'application/json', | ||||
52 | }; | ||||
53 | |||||
54 | # SMELL: code duplication with core ($Foswiki::UNICODE only) | ||||
55 | my $encoding = $ENV{'HTTP_ACCEPT_ENCODING'} || 'gzip'; | ||||
56 | $encoding =~ s/^.*(x-gzip|gzip).*/$1/g; | ||||
57 | my $compressed = 0; | ||||
58 | |||||
59 | if ( $Foswiki::cfg{HttpCompress} || $ENV{'SPDY'} ) { | ||||
60 | $hopts->{'Content-Encoding'} = $encoding; | ||||
61 | $hopts->{'Vary'} = 'Accept-Encoding'; | ||||
62 | require Compress::Zlib; | ||||
63 | $text = Encode::encode_utf8($text); | ||||
64 | $text = Compress::Zlib::memGzip($text); | ||||
65 | $compressed = 1; | ||||
66 | } | ||||
67 | |||||
68 | $response->setDefaultHeaders($hopts); | ||||
69 | |||||
70 | if ($compressed) { | ||||
71 | $response->body($text); | ||||
72 | } | ||||
73 | else { | ||||
74 | $response->print($text); | ||||
75 | } | ||||
76 | } | ||||
77 | |||||
78 | ############################################################################## | ||||
79 | sub id { | ||||
80 | my ( $this, $value ) = @_; | ||||
81 | |||||
82 | $this->{id} = $value if defined $value; | ||||
83 | return $this->{id}; | ||||
84 | } | ||||
85 | |||||
86 | ############################################################################## | ||||
87 | sub code { | ||||
88 | my ( $this, $value ) = @_; | ||||
89 | |||||
90 | $this->{code} = $value if defined $value; | ||||
91 | return ( $this->{code} || 0 ); | ||||
92 | } | ||||
93 | |||||
94 | ############################################################################## | ||||
95 | sub message { | ||||
96 | my ( $this, $value ) = @_; | ||||
97 | |||||
98 | $this->{message} = $value if defined $value; | ||||
99 | return $this->{message}; | ||||
100 | } | ||||
101 | |||||
102 | ################################################################################ | ||||
103 | sub isError { | ||||
104 | my $this = shift; | ||||
105 | |||||
106 | return ( $this->code() == 0 ) ? 0 : 1; | ||||
107 | } | ||||
108 | |||||
109 | ################################################################################ | ||||
110 | sub encode { | ||||
111 | my $this = shift; | ||||
112 | |||||
113 | my $code = $this->code(); | ||||
114 | my $message = $this->message(); | ||||
115 | |||||
116 | if ( $this->isError() ) { | ||||
117 | $message = { | ||||
118 | jsonrpc => "2.0", | ||||
119 | error => { | ||||
120 | code => $code, | ||||
121 | message => $message, | ||||
122 | }, | ||||
123 | }; | ||||
124 | } | ||||
125 | else { | ||||
126 | $message = { | ||||
127 | jsonrpc => "2.0", | ||||
128 | result => $message, | ||||
129 | }; | ||||
130 | } | ||||
131 | |||||
132 | my $id = $this->id(); | ||||
133 | $message->{id} = $id if defined $id; | ||||
134 | |||||
135 | return $this->json->encode($message); | ||||
136 | } | ||||
137 | |||||
138 | ################################################################################ | ||||
139 | sub json { | ||||
140 | my $this = shift; | ||||
141 | |||||
142 | unless ( defined $this->{json} ) { | ||||
143 | $this->{json} = JSON->new->pretty(DEBUG)->convert_blessed(1); | ||||
144 | } | ||||
145 | |||||
146 | return $this->{json}; | ||||
147 | } | ||||
148 | |||||
149 | 1 | 2µs | 1; |