File Coverage

blib/lib/POE/Component/Server/JSONRPC/Http.pm
Criterion Covered Total %
statement 15 15 100.0
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 20 20 100.0


line stmt bran cond sub pod time code
1             ##############################
2             # This code is part of FusionDirectory (http://www.fusiondirectory.org/)
3             # Copyright (C) 2011 FusionDirectory
4             #
5             # This program is free software; you can redistribute it and/or modify
6             # it under the terms of the GNU General Public License as published by
7             # the Free Software Foundation; either version 2 of the License, or
8             # (at your option) any later version.
9             #
10             # This program is distributed in the hope that it will be useful,
11             # but WITHOUT ANY WARRANTY; without even the implied warranty of
12             # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13             # GNU General Public License for more details.
14             #
15             # You should have received a copy of the GNU General Public License
16             # along with this program; if not, write to the Free Software
17             # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18             ##############################
19              
20             package POE::Component::Server::JSONRPC::Http;
21 1     1   7 use strict;
  1         2  
  1         48  
22 1     1   6 use warnings;
  1         2  
  1         34  
23 1     1   6 use POE::Component::Server::JSONRPC; # for old Perl 5.005
  1         2  
  1         7  
24 1     1   31 use base qw(POE::Component::Server::JSONRPC);
  1         1  
  1         114  
25              
26             our $VERSION = '0.03';
27              
28 1         9 use POE qw/
29             Component::Server::SimpleHTTP
30             Filter::Line
31 1     1   7 /;
  1         2  
32             use JSON::Any;
33              
34             use Data::Dumper;
35              
36             =head1 NAME
37              
38             POE::Component::Server::JSONRPC::Http - POE http based JSON-RPC server
39              
40             =head2 new
41              
42             constructor
43             =cut
44              
45             sub new {
46             my $class = shift;
47             return $class->SUPER::new(@_);
48             }
49              
50             =head2 poe_init_server
51              
52             Init HTTP Server.
53             =cut
54              
55             sub poe_init_server {
56             my ($self, $kernel, $session, $heap) = @_[OBJECT, KERNEL, SESSION, HEAP];
57              
58             $kernel->alias_set( 'JSONRPCHTTP' );
59              
60             if (defined($self->{Authenticate})) {
61             $kernel->state('http_input_handler' , $self, 'poe_http_authentication_input_handler');
62             } else {
63             $kernel->state('http_input_handler' , $self, 'poe_input_handler');
64             }
65              
66             $self->{http} = POE::Component::Server::SimpleHTTP->new(
67             'ALIAS' => 'HTTPD',
68             'PORT' => $self->{Port},
69             $self->{Address} ? ('ADDRESS' => $self->{Address} ) : (),
70             $self->{Hostname} ? ('HOSTNAME' => $self->{Hostname} ) : (),
71             $self->{SslKey} ? ('SSLKEYCERT' => [$self->{SslKey}, $self->{SslCert}]) : (),
72             $self->{SslCacert} ? ('SSLINTERMEDIATECACERT' => $self->{SslCacert} ) : (),
73             'HANDLERS' => [
74             {
75             'DIR' => '.*',
76             'SESSION' => 'JSONRPCHTTP',
77             'EVENT' => 'http_input_handler',
78             },
79             ],
80             );
81             }
82              
83             =head2 poe_http_authentication_input_handler
84              
85             This function is used to treat HTTP authentication if needed
86             =cut
87              
88             sub poe_http_authentication_input_handler {
89             my ($self, $kernel, $session, $heap, $request, $response, $dirmatch) = @_[OBJECT, KERNEL, SESSION, HEAP, ARG0..$#_ ];
90              
91             my ( $login, $password ) = $request->authorization_basic();
92             if (&{$self->{Authenticate}}($login,$password)) {
93             # Authentication worked
94             $kernel->post( 'JSONRPCHTTP', 'input_handler', $request, $response, $dirmatch);
95             } else {
96             # Set the authorization
97             $response->header( 'WWW-Authenticate' => 'Basic realm="JSONRPCRealm"' );
98             $response->code( 401 );
99             $response->content( 'FORBIDDEN.' );
100              
101             # Send it off!
102             $kernel->post( 'HTTPD', 'DONE', $response );
103             }
104             }
105              
106             =head2 poe_send
107              
108             Send HTTP response
109             =cut
110              
111             sub poe_send {
112             my ($kernel,$response, $content) = @_[KERNEL,ARG0..$#_];
113              
114             #HTTP
115             $response->code( 200 );
116             $response->content( $content );
117              
118             $kernel->post( 'HTTPD', 'DONE', $response );
119             }
120              
121             1;