← Index
NYTProf Performance Profile   « line view »
For ./view
  Run on Fri Jul 31 18:42:36 2015
Reported on Fri Jul 31 18:48:15 2015

Filename/var/www/foswikidev/core/lib/Foswiki/Contrib/JsonRpcContrib/Response.pm
StatementsExecuted 11 statements in 571µs
Subroutines
Calls P F Exclusive
Time
Inclusive
Time
Subroutine
11116µs29µsFoswiki::Contrib::JsonRpcContrib::Response::::BEGIN@19Foswiki::Contrib::JsonRpcContrib::Response::BEGIN@19
1119µs36µsFoswiki::Contrib::JsonRpcContrib::Response::::BEGIN@22Foswiki::Contrib::JsonRpcContrib::Response::BEGIN@22
1119µs14µsFoswiki::Contrib::JsonRpcContrib::Response::::BEGIN@20Foswiki::Contrib::JsonRpcContrib::Response::BEGIN@20
1118µs37µsFoswiki::Contrib::JsonRpcContrib::Response::::BEGIN@25Foswiki::Contrib::JsonRpcContrib::Response::BEGIN@25
1114µs4µsFoswiki::Contrib::JsonRpcContrib::Response::::BEGIN@24Foswiki::Contrib::JsonRpcContrib::Response::BEGIN@24
0000s0sFoswiki::Contrib::JsonRpcContrib::Response::::codeFoswiki::Contrib::JsonRpcContrib::Response::code
0000s0sFoswiki::Contrib::JsonRpcContrib::Response::::encodeFoswiki::Contrib::JsonRpcContrib::Response::encode
0000s0sFoswiki::Contrib::JsonRpcContrib::Response::::idFoswiki::Contrib::JsonRpcContrib::Response::id
0000s0sFoswiki::Contrib::JsonRpcContrib::Response::::isErrorFoswiki::Contrib::JsonRpcContrib::Response::isError
0000s0sFoswiki::Contrib::JsonRpcContrib::Response::::jsonFoswiki::Contrib::JsonRpcContrib::Response::json
0000s0sFoswiki::Contrib::JsonRpcContrib::Response::::messageFoswiki::Contrib::JsonRpcContrib::Response::message
0000s0sFoswiki::Contrib::JsonRpcContrib::Response::::newFoswiki::Contrib::JsonRpcContrib::Response::new
0000s0sFoswiki::Contrib::JsonRpcContrib::Response::::printFoswiki::Contrib::JsonRpcContrib::Response::print
Call graph for these subroutines as a Graphviz dot language file.
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
17package Foswiki::Contrib::JsonRpcContrib::Response;
18
19228µs242µ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
use strict;
# spent 29µs making 1 call to Foswiki::Contrib::JsonRpcContrib::Response::BEGIN@19 # spent 13µs making 1 call to strict::import
20225µs218µ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
use warnings;
# spent 14µs making 1 call to Foswiki::Contrib::JsonRpcContrib::Response::BEGIN@20 # spent 4µs making 1 call to warnings::import
21
22225µs263µ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
use Assert;
# spent 36µs making 1 call to Foswiki::Contrib::JsonRpcContrib::Response::BEGIN@22 # spent 27µs making 1 call to Exporter::import
23
24222µs14µ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
use JSON ();
252469µs266µ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
use constant TRACE => 0; # toggle me
# spent 37µs making 1 call to Foswiki::Contrib::JsonRpcContrib::Response::BEGIN@25 # spent 29µs making 1 call to constant::import
26
27################################################################################
28sub 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
42sub 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##############################################################################
79sub id {
80 my ( $this, $value ) = @_;
81
82 $this->{id} = $value if defined $value;
83 return $this->{id};
84}
85
86##############################################################################
87sub code {
88 my ( $this, $value ) = @_;
89
90 $this->{code} = $value if defined $value;
91 return ( $this->{code} || 0 );
92}
93
94##############################################################################
95sub message {
96 my ( $this, $value ) = @_;
97
98 $this->{message} = $value if defined $value;
99 return $this->{message};
100}
101
102################################################################################
103sub isError {
104 my $this = shift;
105
106 return ( $this->code() == 0 ) ? 0 : 1;
107}
108
109################################################################################
110sub 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################################################################################
139sub 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
14912µs1;