| 1 | package JRPC::Nginx; | ||
| 2 | use JRPC; | ||
| 3 | use JSON::XS; | ||
| 4 | # require - to avoid symbol resolution problems ? | ||
| 5 | require nginx; # nginx / Nginx ? | ||
| 6 | use strict; | ||
| 7 | use warnings; | ||
| 8 | |||
| 9 | our $VERSION = "0.9"; | ||
| 10 | |||
| 11 | # See Perldoc at the end... | ||
| 12 | sub handler { | ||
| 13 | my ($r) = @_; | ||
| 14 | no strict 'subs'; | ||
| 15 | # NGinx: | ||
| 16 | if ($r->request_method() ne 'POST') {return DECLINED;} | ||
| 17 | # NGinx $r->header_in(header) | ||
| 18 | # Nginx: The body must be under | ||
| 19 | #my $buffer = $r->request_body(); | ||
| 20 | # Nginx: $r->header_out('Content-type', 'text/plain'); | ||
| 21 | # OR: | ||
| 22 | $r->send_http_header('text/plain'); | ||
| 23 | #my $hasbody = | ||
| 24 | $r->has_request_body(\&handle_2nd_stage); | ||
| 25 | ##################### | ||
| 26 | return OK; | ||
| 27 | } | ||
| 28 | #=head2 handle_2nd_stage($r); | ||
| 29 | # Internal method to handle Nginx Asynchronous Post-request POST-Body stage. | ||
| 30 | #This encapsulates the | ||
| 31 | #=cut | ||
| 32 | sub handle_2nd_stage { | ||
| 33 | my ($r) = @_; | ||
| 34 | my $jr = {'id' => $$, 'jsonrpc' => '2.0', }; | ||
| 35 | my $buffer = $r->request_body(); | ||
| 36 | |||
| 37 | eval { | ||
| 38 | # Parse Request | ||
| 39 | my $j = eval { JRPC::parse($buffer); }; | ||
| 40 | if ($@) {die("Error Parsing Request: $@");} | ||
| 41 | if (defined($JRPC::prelogger) && (ref($JRPC::prelogger) eq 'CODE')) {$JRPC::prelogger->($j);} | ||
| 42 | my $p = $j->{'params'}; | ||
| 43 | my $m = $j->{'method'}; | ||
| 44 | |||
| 45 | my $uri = $r->uri(); | ||
| 46 | #my $res = {}; # Result | ||
| 47 | my $f = eval {JRPC::methresolve($r, $m);}; | ||
| 48 | if (!$f || $@) {die("method '$m' not resolved (-32601) URI: $uri: $@");} | ||
| 49 | ##### reqinit | ||
| 50 | #if (my $f = $pkg->can('reqinit')) {$f->($p, $j);} | ||
| 51 | ##### Execute, store result | ||
| 52 | my $res = eval { $f->($p); }; # Dispatch (catching any exceptions) | ||
| 53 | if ($@) {die("Error in processing JRPC method '$m' (-32603): $@");} | ||
| 54 | # Definite Success - serialize response ? | ||
| 55 | $jr->{'result'} = $res; | ||
| 56 | #my $clen = $hdrs_in->get('content-length'); # Double verify read() | ||
| 57 | # 'clen' => $clen, 'postread' => $cnt, | ||
| 58 | # There could be blessed nodes in $res ('result' now) that do not serialize | ||
| 59 | # well. Be ready to encounter exceptions here. | ||
| 60 | my $out = JSON::XS::encode_json($jr); # Serialize as a separate step to know length | ||
| 61 | |||
| 62 | ################# Nginx Output Response ################ | ||
| 63 | $r->header_out('Content-length', length($out)); | ||
| 64 | $r->print($out); | ||
| 65 | }; # eval for full processing | ||
| 66 | if ($@) { | ||
| 67 | $r->print( JRPC::createfault($jr, $@, 500) ); | ||
| 68 | } | ||
| 69 | # Nginx: | ||
| 70 | $r->rflush(); | ||
| 71 | no strict 'subs'; | ||
| 72 | return OK; | ||
| 73 | } | ||
| 74 | |||
| 75 | 1; | ||
| 76 | __END__ |