line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package OneTool::Daemon; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
=head1 NAME |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
OneTool::Daemon - OneTool Daemon module |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
=cut |
8
|
|
|
|
|
|
|
|
9
|
1
|
|
|
1
|
|
62466
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
31
|
|
10
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
27
|
|
11
|
|
|
|
|
|
|
|
12
|
1
|
|
|
1
|
|
6
|
use FindBin; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
45
|
|
13
|
1
|
|
|
1
|
|
1028
|
use HTTP::Daemon; |
|
1
|
|
|
|
|
95143
|
|
|
1
|
|
|
|
|
17
|
|
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
#use HTTP::Daemon::SSL; |
16
|
1
|
|
|
1
|
|
857
|
use HTTP::Headers; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
31
|
|
17
|
1
|
|
|
1
|
|
7
|
use HTTP::Response; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
26
|
|
18
|
1
|
|
|
1
|
|
7
|
use HTTP::Status; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
377
|
|
19
|
1
|
|
|
1
|
|
1568
|
use IO::Socket::SSL; |
|
1
|
|
|
|
|
68555
|
|
|
1
|
|
|
|
|
8
|
|
20
|
1
|
|
|
1
|
|
207
|
use Log::Log4perl; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
10
|
|
21
|
1
|
|
|
1
|
|
42
|
use Log::Log4perl::Level; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
9
|
|
22
|
1
|
|
|
1
|
|
574
|
use Moose; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
use URI; |
24
|
|
|
|
|
|
|
use URI::QueryParam; |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
=head1 MOOSE OBJECT |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
=cut |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
has 'ip' => ( |
31
|
|
|
|
|
|
|
is => 'rw', |
32
|
|
|
|
|
|
|
isa => 'Str', |
33
|
|
|
|
|
|
|
required => 1, |
34
|
|
|
|
|
|
|
); |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
has 'port' => ( |
37
|
|
|
|
|
|
|
is => 'rw', |
38
|
|
|
|
|
|
|
isa => 'Int', |
39
|
|
|
|
|
|
|
required => 1, |
40
|
|
|
|
|
|
|
); |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
has 'api' => ( |
43
|
|
|
|
|
|
|
is => 'rw', |
44
|
|
|
|
|
|
|
isa => 'HashRef', |
45
|
|
|
|
|
|
|
required => 1, |
46
|
|
|
|
|
|
|
); |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
has 'logger' => ( |
49
|
|
|
|
|
|
|
is => 'rw', |
50
|
|
|
|
|
|
|
isa => 'Log::Log4perl::Logger', |
51
|
|
|
|
|
|
|
required => 1, |
52
|
|
|
|
|
|
|
); |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
=head1 SUBROUTINES/METHODS |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
=head2 Listener() |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
Listener for HTTP/HTTPS API requests |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
=cut |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
sub Listener |
63
|
|
|
|
|
|
|
{ |
64
|
|
|
|
|
|
|
my $self = shift; |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
my $daemon = HTTP::Daemon->new( |
67
|
|
|
|
|
|
|
ReuseAddr => 1, |
68
|
|
|
|
|
|
|
LocalAddr => $self->{ip}, |
69
|
|
|
|
|
|
|
LocalPort => $self->{port} |
70
|
|
|
|
|
|
|
); |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
=head2 comment |
73
|
|
|
|
|
|
|
my $daemon = HTTP::Daemon::SSL->new( |
74
|
|
|
|
|
|
|
ReuseAddr => 1, LocalAddr => $self->{ip}, LocalPort => $self->{port}, |
75
|
|
|
|
|
|
|
SSL_cert_file => "$FindBin::Bin/../conf/certs/server-cert.pem", |
76
|
|
|
|
|
|
|
SSL_key_file => "$FindBin::Bin/../conf/certs/server-key.pem" |
77
|
|
|
|
|
|
|
) |
78
|
|
|
|
|
|
|
|| die IO::Socket::SSL::errstr(); |
79
|
|
|
|
|
|
|
=cut |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
my $json_header = HTTP::Headers->new('Content-Type' => 'application/json'); |
82
|
|
|
|
|
|
|
$self->Log('info', 'OneTool Daemon API listening on ' . $daemon->url); |
83
|
|
|
|
|
|
|
while (my $connection = $daemon->accept) |
84
|
|
|
|
|
|
|
{ |
85
|
|
|
|
|
|
|
while (my $request = $connection->get_request) |
86
|
|
|
|
|
|
|
{ |
87
|
|
|
|
|
|
|
my ($method, $path, $params, $content) = ( |
88
|
|
|
|
|
|
|
$request->method, $request->uri->path, |
89
|
|
|
|
|
|
|
$request->uri->query_form_hash, |
90
|
|
|
|
|
|
|
$request->content |
91
|
|
|
|
|
|
|
); |
92
|
|
|
|
|
|
|
if ( (defined $self->{api}->{$path}) |
93
|
|
|
|
|
|
|
&& ($method eq $self->{api}->{$path}->{method})) |
94
|
|
|
|
|
|
|
{ |
95
|
|
|
|
|
|
|
my $resp_content = |
96
|
|
|
|
|
|
|
$self->{api}->{$path}->{action}($self, $params, $content); |
97
|
|
|
|
|
|
|
my $resp = |
98
|
|
|
|
|
|
|
HTTP::Response->new(200, 'OK', $json_header, $resp_content); |
99
|
|
|
|
|
|
|
$connection->send_response($resp); |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
else |
102
|
|
|
|
|
|
|
{ |
103
|
|
|
|
|
|
|
$connection->send_error(RC_FORBIDDEN); |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
$connection->close; |
107
|
|
|
|
|
|
|
undef($connection); |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
return (1); |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
=head2 Log($str_level, $msg) |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
Logs message $msg with loglevel $str_level |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
=cut |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
sub Log |
120
|
|
|
|
|
|
|
{ |
121
|
|
|
|
|
|
|
my ($self, $str_level, $msg) = @_; |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
return (undef) if ($str_level !~ /^(?:debug|info|warn|error)$/i); |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
my $level = Log::Log4perl::Level::to_priority(uc($str_level)); |
126
|
|
|
|
|
|
|
$self->{logger}->log($level, $msg); |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
return ($msg); |
129
|
|
|
|
|
|
|
} |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
no Moose; |
132
|
|
|
|
|
|
|
__PACKAGE__->meta->make_immutable; |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
1; |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
=head1 AUTHOR |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
Sebastien Thebert <contact@onetool.pm> |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
=cut |