tokuhiromの日記

 | 

2008-04-01

JSONRPC もどきを高速に処理したい 19:36

JSONRPC やるだけなら、WAF っぽいのはもういらなくて、なんかもう、こんな PoPo(Plain Old Perl Object) っぽい感じでやれれば十分な気がしている。

こういうの。

package Jaico::Service;
use strict;
use warnings;
use base qw/Class::Data::Inheritable/;

__PACKAGE__->mk_classdata('__attr_cache' => {});

sub call {
    my ($class, $method, $params) = @_;

    return +{ error => 'method not found' } unless my $code = $self->can($method);
    return +{ error => 'private method'   } unless grep /^Public$/, $class->__attr_cache->{$code};

    return $class->$method($params);
}

sub MODIFY_CODE_ATTRIBUTES {
    my ($class, $code, @attrs) = @_;
    $class->__attr_cache->{$code} = [@attrs];
    return ();
}

1;
__END__

    package YourProj::Service::Entry;
    use base qw/Jaico::Service/;

    sub add : Public {
         my ($class, $params) = @_;
         # snip...
         return +{ status => 'ok' };
    }

    1;

    # map to {'method': 'entry/add'}
package Jaico::Handler::Apache13;
use strict;
use warnings;
use JSON::XS;
use String::CamelCase qw/camelize/;
use UNIVERSAL::require;

sub handler ($$) {
    my ($class, $r) = @_;

    my $base = $r->dir_config('JaicoBase') or die "missing JaicoBase";

    my $body = do { local $/; <STDIN> };
    my $data = decode_json($body);

    my @method = split m{/}, $data->{method};
    my $method = pop @method;
    my $module = join('::', $base, map { camelize $_ } @method);
    $module->use or die $@;

    my $res = $module->call($method, $data->{params});
    $res->{id} = $data->{id};

    $class->output_content($r, $res);
}

sub output_content {
    my ($class, $r, $data) = @_;

    $r->content_type( $class->content_type );
    $r->code(HTTP_OK);
    $r->send_http_headers();
    $r->print(encode_json $data);
}

sub content_type {
    my ($class, $r, ) = @_;

    my $encoding = $r->dir_config('JaicoEncoding') || 'utf-8';
    if ($r->header_in('UserAgent') =~ /Opera/) {
        return "application/x-javascript; charset=$encoding";
    } else {
        return "application/javascript; charset=$encoding";
    }
}

1;
__END__

<Location />
    SetHandler         perl-script
    PerlRequestHandler Jaico::Handler::Apache13
    JaicoBase          YourProj::Contorller
</Location>

DellyDelly2011/08/02 18:45Holy sihzint, this is so cool thank you.

nqrdbyoonqrdbyoo2011/08/02 20:43l53J04 <a href="http://crsahulceruh.com/">crsahulceruh</a>

kxtrzzhyukxtrzzhyu2011/08/03 20:45Pc9avI , [url=http://izbrylydthcj.com/]izbrylydthcj[/url], [link=http://djhzrthixxpe.com/]djhzrthixxpe[/link], http://prqsnknydvbf.com/

nyzqcjpdtynyzqcjpdty2011/08/05 18:02XHVZ0z <a href="http://tfqzjabmtxqq.com/">tfqzjabmtxqq</a>

mklswfnmklswfn2011/08/05 22:20sXCgRn , [url=http://zgcrhxopfuey.com/]zgcrhxopfuey[/url], [link=http://avfjhpwpwjox.com/]avfjhpwpwjox[/link], http://fwcbifdoairm.com/

relwuzwlvgrelwuzwlvg2013/07/29 01:10mecwodbubmztu, <a href="http://www.eppzvmagjr.com/">sgfdvuksug</a> , [url=http://www.ewmzyocxyc.com/]dvrziupeel[/url], http://www.iwkfvhrysc.com/ sgfdvuksug

rylztataqjrylztataqj2013/07/31 07:05pyjyydbubmztu, <a href="http://www.ygfrmzrnyd.com/">rqnnudyrnv</a> , [url=http://www.sqsxzjrhqe.com/]mzteqzusfb[/url], http://www.ukgzeqgzxa.com/ rqnnudyrnv

jjxvpkpcbqjjxvpkpcbq2013/08/28 01:46fiyygdbubmztu, <a href="http://www.npslerrxug.com/">tedaksbpvm</a> , [url=http://www.eoemadjwni.com/]ddqoryrwiv[/url], http://www.krhnkxmbxu.com/ tedaksbpvm

dhqajitukudhqajituku2013/11/23 14:10zvfsqdbubmztu, <a href="http://www.edrwrpxcpf.com/">utmehhcslf</a> , [url=http://www.obljkacnsd.com/]zmdmteeovn[/url], http://www.jnkaubrclb.com/ utmehhcslf

 |