se JSON::MaybeXS;
my $json = JSON::MaybeXS->new->allow_nonref;
sub errpage {
my ($env, $rres) = @_;
my $fmt = $env->{'riap.request'}{fmt} //
$env->{"periahs.default_fmt"} // 'json';
else {
$pres = [
200,
["Content-Type" => "application/json"],
[$json->encode($rres)]
];
}
log_trace("Returning error page: %s", $pres);
unction>) as an error
page PSGI response, either in HTML/JSON/plaintext (according to C<<
$env->{"riap.request"}{fmt} >>). Will default to JSON if C<fmt> is unsupported.
$env is PSGI environment.
=h
max_args_len
max_resp_len
);
use JSON::MaybeXS;
use Plack::Util;
use POSIX;
use Scalar::Util qw(blessed);
use Time::HiRes qw(gettimeofd
r = "tcp:$env->{SERVER_PORT}";
}
state $json = JSON::MaybeXS->new->allow_nonref->allow_blessed->convert_blessed;
local *UNIVERSAL::TO_JSON = sub { "$_[0]" };
my $rreq = $env->{'riap.
y ($args_s, $args_len, $args_partial);
if ($rreq->{args} && !$skip_args) {
$args_s = $json->encode($rreq->{args});
$args_len = length($args_s);
$args_partial = $args_len >
s)
sub __parse_json {
require Data::Clean::FromJSON;
require JSON::MaybeXS;
my $str = shift;
state $json = JSON::MaybeXS->new->allow_nonref;
# to rid of those JSON::XS::Boolean
, or we use a fork of JSON::XS which doesn't
# produce those in the first place (probably only when performance is
# critical).
state $cleanser = Data::Clean::FromJSON->get_cleanser;
my $res;
eval { $res = $json->decode($str); $cleanser->clean_in_place($res) };
my $e = $@;
return (!$e, $e, $res);
}
sub __parse_yaml {
require YAML::Syck;
my $str = shift;
);
use Perinci::AccessUtil qw(insert_riap_stuffs_to_res);
use Data::Clean::JSON;
use Log::ger::Output;
use Log::ger::Util;
use Perinci::Result::Format 0.31;
use Scalar::Util qw(
sewhere (e.g.
# when doing access logging).
my $cleanser = Data::Clean::JSON->get_cleanser;
# to avoid sending colored YAML/JSON output
$Perinci::Result::Format::Enable_Decoration = 0;
sub prepare_a
my $fmt = $rreq->{fmt} // $env->{'periahs.default_fmt'} // 'json';
my $formatter;
for ($fmt, "json") { # fallback to json if unknown format
$formatter = $Perinci::Result::Format:
!^/api/(?<f>json|yaml|j|y)/
(?<uri>[^?/]+(?:/[^?/]+)?)!x,
sub {
my ($env, $m) = @_;
$env->{"riap.request"}{fmt} = $m->{f} =~ /j/ ? 'json' : 'yaml';
allowing format by sticking C<.json> or C<.yaml> at the end of
Riap URI:
enable "PeriAHS::ParseRequest"
match_uri => qr!^(?<uri>[^?/]+(?:/[^?/]+)?)(?:\.(?<fmt>json|yaml))!x;
=head2 I need even