line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
###################################################################### |
2
|
|
|
|
|
|
|
# Management connection from a client |
3
|
|
|
|
|
|
|
###################################################################### |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
package Perlbal::ClientManage; |
6
|
22
|
|
|
22
|
|
129
|
use strict; |
|
22
|
|
|
|
|
49
|
|
|
22
|
|
|
|
|
808
|
|
7
|
22
|
|
|
22
|
|
151
|
use warnings; |
|
22
|
|
|
|
|
43
|
|
|
22
|
|
|
|
|
638
|
|
8
|
22
|
|
|
22
|
|
136
|
no warnings qw(deprecated); |
|
22
|
|
|
|
|
42
|
|
|
22
|
|
|
|
|
1079
|
|
9
|
|
|
|
|
|
|
|
10
|
22
|
|
|
22
|
|
112
|
use base "Perlbal::Socket"; |
|
22
|
|
|
|
|
43
|
|
|
22
|
|
|
|
|
5505
|
|
11
|
22
|
|
|
|
|
148
|
use fields ('service', |
12
|
|
|
|
|
|
|
'buf', |
13
|
|
|
|
|
|
|
'is_http', # bool: is an HTTP request? |
14
|
|
|
|
|
|
|
'ctx', # command context |
15
|
22
|
|
|
22
|
|
133
|
); |
|
22
|
|
|
|
|
46
|
|
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
# ClientManage |
18
|
|
|
|
|
|
|
sub new { |
19
|
17
|
|
|
17
|
1
|
55
|
my Perlbal::ClientManage $self = shift; |
20
|
17
|
|
|
|
|
281
|
my ($service, $sock) = @_; |
21
|
17
|
50
|
|
|
|
272
|
$self = fields::new($self) unless ref $self; |
22
|
17
|
|
|
|
|
8952
|
$self->SUPER::new($sock); |
23
|
17
|
|
|
|
|
49
|
$self->{service} = $service; |
24
|
17
|
|
|
|
|
56
|
$self->{buf} = ""; # what we've read so far, not forming a complete line |
25
|
|
|
|
|
|
|
|
26
|
17
|
|
|
|
|
318
|
$self->{ctx} = Perlbal::CommandContext->new; |
27
|
17
|
|
|
|
|
284
|
$self->{ctx}->verbose(1); |
28
|
|
|
|
|
|
|
|
29
|
17
|
|
|
|
|
1585
|
$self->watch_read(1); |
30
|
17
|
|
|
|
|
1575
|
return $self; |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
# ClientManage |
34
|
|
|
|
|
|
|
sub event_read { |
35
|
117
|
|
|
117
|
1
|
2650737
|
my Perlbal::ClientManage $self = shift; |
36
|
|
|
|
|
|
|
|
37
|
117
|
|
|
|
|
610
|
my $bref; |
38
|
117
|
50
|
|
|
|
782
|
unless ($self->{is_http}) { |
39
|
117
|
|
|
|
|
945
|
$bref = $self->read(1024); |
40
|
117
|
50
|
|
|
|
6485
|
return $self->close() unless defined $bref; |
41
|
117
|
|
|
|
|
473
|
$self->{buf} .= $$bref; |
42
|
|
|
|
|
|
|
|
43
|
117
|
50
|
|
|
|
1037
|
if ($self->{buf} =~ /^(?:HEAD|GET|POST) /) { |
44
|
0
|
|
|
|
|
0
|
$self->{is_http} = 1; |
45
|
0
|
|
|
|
|
0
|
$self->{headers_string} .= $$bref; |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
117
|
50
|
|
|
|
791
|
if ($self->{is_http}) { |
50
|
0
|
|
|
|
|
0
|
my $hd = $self->read_request_headers; |
51
|
0
|
0
|
|
|
|
0
|
return unless $hd; |
52
|
0
|
|
|
|
|
0
|
$self->handle_http(); |
53
|
0
|
|
|
|
|
0
|
return; |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
117
|
|
|
|
|
2567
|
while ($self->{buf} =~ s/^(.+?)\r?\n//) { |
57
|
117
|
|
|
|
|
941
|
my $line = $1; |
58
|
|
|
|
|
|
|
|
59
|
117
|
50
|
|
|
|
2459
|
if ($line =~ /^quit|exit$/) { |
60
|
0
|
|
|
|
|
0
|
$self->close('user_requested_quit'); |
61
|
0
|
|
|
|
|
0
|
return; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
my $out = sub { |
65
|
274
|
|
|
274
|
|
1705
|
$self->write("$_[0]\r\n"); |
66
|
117
|
|
|
|
|
959
|
}; |
67
|
|
|
|
|
|
|
|
68
|
117
|
|
|
|
|
1129
|
Perlbal::run_manage_command($line, $out, $self->{ctx}); |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
sub event_write { |
73
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
74
|
0
|
0
|
|
|
|
|
$self->watch_write(0) if $self->write(undef); |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
# ClientManage |
78
|
0
|
|
|
0
|
1
|
|
sub event_err { my $self = shift; $self->close; } |
|
0
|
|
|
|
|
|
|
79
|
0
|
|
|
0
|
1
|
|
sub event_hup { my $self = shift; $self->close; } |
|
0
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
# HTTP management support |
82
|
|
|
|
|
|
|
sub handle_http { |
83
|
0
|
|
|
0
|
0
|
|
my Perlbal::ClientManage $self = shift; |
84
|
|
|
|
|
|
|
|
85
|
0
|
|
|
|
|
|
my $uri = $self->{req_headers}->request_uri; |
86
|
|
|
|
|
|
|
|
87
|
0
|
|
|
|
|
|
my $body; |
88
|
0
|
|
|
|
|
|
my $code = "200 OK"; |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
my $prebox = sub { |
91
|
0
|
|
|
0
|
|
|
my $cmd = shift; |
92
|
0
|
|
|
|
|
|
my $alt = shift; |
93
|
0
|
|
|
|
|
|
$body .= "$cmd ";
|
94
|
|
|
|
|
|
|
Perlbal::run_manage_command($cmd, sub { |
95
|
0
|
|
0
|
|
|
|
my $line = $_[0] || ""; |
96
|
0
|
0
|
|
|
|
|
$alt->(\$line) if $alt; |
97
|
0
|
|
|
|
|
|
$body .= "$line\n"; |
98
|
0
|
|
|
|
|
|
}); |
99
|
0
|
|
|
|
|
|
$body .= "\n"; |
100
|
|
|
|
|
|
|
|
101
|
0
|
|
|
|
|
|
}; |
102
|
|
|
|
|
|
|
|
103
|
0
|
|
|
|
|
|
$body .= "\n"; |
104
|
0
|
|
|
|
|
|
$body .= "\n"; |
105
|
0
|
|
|
|
|
|
$body .= "perlbal management interface"; |
106
|
|
|
|
|
|
|
|
107
|
0
|
0
|
|
|
|
|
if ($uri eq "/") { |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
108
|
0
|
|
|
|
|
|
$body .= "perlbal management interface |
109
|
0
|
|
|
|
|
|
$body .= "Sockets"; |
110
|
0
|
|
|
|
|
|
$body .= "Perl Objects in use"; |
111
|
0
|
|
|
|
|
|
$body .= "Service Details |
112
|
0
|
|
|
|
|
|
foreach my $sname (Perlbal->service_names) { |
113
|
0
|
|
|
|
|
|
my Perlbal::Service $svc = Perlbal->service($sname); |
114
|
0
|
0
|
|
|
|
|
next unless $svc; |
115
|
0
|
0
|
|
|
|
|
my $listen = $svc->{listen} ? " ($svc->{listen})" : ""; |
116
|
0
|
|
|
|
|
|
$body .= "$sname - $svc->{role}$listen\n"; |
117
|
|
|
|
|
|
|
} |
118
|
0
|
|
|
|
|
|
$body .= ""; |
119
|
0
|
|
|
|
|
|
$body .= ""; |
120
|
|
|
|
|
|
|
} elsif ($uri eq "/socks") { |
121
|
0
|
|
|
|
|
|
$prebox->('socks summary'); |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
$prebox->('socks', sub { |
124
|
0
|
|
|
0
|
|
|
${$_[0]} =~ s!service \'(\w+)\'!$1!; |
|
0
|
|
|
|
|
|
|
125
|
0
|
|
|
|
|
|
}); |
126
|
|
|
|
|
|
|
} elsif ($uri eq "/obj") { |
127
|
0
|
|
|
|
|
|
$prebox->('obj'); |
128
|
|
|
|
|
|
|
} elsif ($uri =~ m!^/service\?(\w+)$!) { |
129
|
0
|
|
|
|
|
|
my $service = $1; |
130
|
0
|
|
|
|
|
|
$prebox->("show service $service"); |
131
|
|
|
|
|
|
|
} else { |
132
|
0
|
|
|
|
|
|
$code = "404 Not found"; |
133
|
0
|
|
|
|
|
|
$body .= "$code"; |
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
|
136
|
0
|
|
|
|
|
|
$body .= "
Perlbal management. \n"; |
137
|
0
|
|
|
|
|
|
$self->write("HTTP/1.0 $code\r\nContent-type: text/html\r\nContent-Length: " . length($body) . |
138
|
|
|
|
|
|
|
"\r\n\r\n$body"); |
139
|
0
|
|
|
0
|
|
|
$self->write(sub { $self->close; }); |
|
0
|
|
|
|
|
|
|
140
|
0
|
|
|
|
|
|
return; |
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
1; |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
# Local Variables: |
147
|
|
|
|
|
|
|
# mode: perl |
148
|
|
|
|
|
|
|
# c-basic-indent: 4 |
149
|
|
|
|
|
|
|
# indent-tabs-mode: nil |
150
|
|
|
|
|
|
|
# End: |