| 1 | ###################################################################### | ||||||||||||||||||||||
| 2 | # Management connection from a client | ||||||||||||||||||||||
| 3 | ###################################################################### | ||||||||||||||||||||||
| 4 | |||||||||||||||||||||||
| 5 | package Perlbal::ClientManage; | ||||||||||||||||||||||
| 6 | 22 | ▶ | use strict; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 7 | 22 | ▶ | use warnings; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 8 | 22 | ▶ | no warnings qw(deprecated); | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 9 | |||||||||||||||||||||||
| 10 | 22 | ▶ | use base "Perlbal::Socket"; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 11 | 22 | ▶ | use fields ('service', | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 12 | 'buf', | ||||||||||||||||||||||
| 13 | 'is_http', # bool: is an HTTP request? | ||||||||||||||||||||||
| 14 | 'ctx', # command context | ||||||||||||||||||||||
| 15 | 22 | ); | |||||||||||||||||||||
| 16 | |||||||||||||||||||||||
| 17 | # ClientManage | ||||||||||||||||||||||
| 18 | 18 | ▶ | sub new { | ||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||
| 19 | 18 | my Perlbal::ClientManage $self = shift; | |||||||||||||||||||||
| 20 | 18 | my ($service, $sock) = @_; | |||||||||||||||||||||
| 21 | 18 | ▶ | $self = fields::new($self) unless ref $self; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 22 | 18 | $self->SUPER::new($sock); | |||||||||||||||||||||
| 23 | 18 | $self->{service} = $service; | |||||||||||||||||||||
| 24 | 18 | $self->{buf} = ""; # what we've read so far, not forming a complete line | |||||||||||||||||||||
| 25 | |||||||||||||||||||||||
| 26 | 18 | $self->{ctx} = Perlbal::CommandContext->new; | |||||||||||||||||||||
| 27 | 18 | $self->{ctx}->verbose(1); | |||||||||||||||||||||
| 28 | |||||||||||||||||||||||
| 29 | 18 | $self->watch_read(1); | |||||||||||||||||||||
| 30 | 18 | return $self; | |||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||
| 32 | |||||||||||||||||||||||
| 33 | # ClientManage | ||||||||||||||||||||||
| 34 | 124 | ▶ | sub event_read { | ||||||||||||||||||||
|
Subroutine
event_read: called CC=8 SCAR=25.4
Pod
documented
| |||||||||||||||||||||||
| 35 | 124 | my Perlbal::ClientManage $self = shift; | |||||||||||||||||||||
| 36 | |||||||||||||||||||||||
| 37 | 124 | my $bref; | |||||||||||||||||||||
| 38 | 124 | ▶ | unless ($self->{is_http}) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 39 | 124 | $bref = $self->read(1024); | |||||||||||||||||||||
| 40 | 124 | ▶ | return $self->close() unless defined $bref; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 41 | 124 | $self->{buf} .= $$bref; | |||||||||||||||||||||
| 42 | |||||||||||||||||||||||
| 43 | 124 | ▶ | if ($self->{buf} =~ /^(?:HEAD|GET|POST) /) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 44 | 0 | $self->{is_http} = 1; | |||||||||||||||||||||
| 45 | 0 | $self->{headers_string} .= $$bref; | |||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||
| 48 | |||||||||||||||||||||||
| 49 | 124 | ▶ | if ($self->{is_http}) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 50 | 0 | my $hd = $self->read_request_headers; | |||||||||||||||||||||
| 51 | 0 | ▶ | return unless $hd; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 52 | 0 | $self->handle_http(); | |||||||||||||||||||||
| 53 | 0 | return; | |||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||
| 55 | |||||||||||||||||||||||
| 56 | 124 | ▶ | while ($self->{buf} =~ s/^(.+?)\r?\n//) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 57 | 124 | my $line = $1; | |||||||||||||||||||||
| 58 | |||||||||||||||||||||||
| 59 | 124 | ▶ | if ($line =~ /^quit|exit$/) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 60 | 0 | $self->close('user_requested_quit'); | |||||||||||||||||||||
| 61 | 0 | return; | |||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||
| 63 | |||||||||||||||||||||||
| 64 | 278 | ▶ | my $out = sub { | ||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 65 | 278 | $self->write("$_[0]\r\n"); | |||||||||||||||||||||
| 66 | 124 | }; | |||||||||||||||||||||
| 67 | |||||||||||||||||||||||
| 68 | 124 | Perlbal::run_manage_command($line, $out, $self->{ctx}); | |||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||
| 71 | |||||||||||||||||||||||
| 72 | 0 | ▶ | sub event_write { | ||||||||||||||||||||
|
Subroutine
event_write: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||
| 73 | 0 | my $self = shift; | |||||||||||||||||||||
| 74 | 0 | ▶ | $self->watch_write(0) if $self->write(undef); | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||
| 76 | |||||||||||||||||||||||
| 77 | # ClientManage | ||||||||||||||||||||||
| 78 | 0 | ▶ | sub event_err { my $self = shift; $self->close; } | ||||||||||||||||||||
|
Subroutine
event_err: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||
| 79 | 0 | ▶ | sub event_hup { my $self = shift; $self->close; } | ||||||||||||||||||||
|
Subroutine
event_hup: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||
| 80 | |||||||||||||||||||||||
| 81 | # HTTP management support | ||||||||||||||||||||||
| 82 | 0 | ▶ | sub handle_http { | ||||||||||||||||||||
|
Subroutine
handle_http: not called CC=8 SCAR=42.8
Pod
undocumented
| |||||||||||||||||||||||
| 83 | 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 | 0 | ▶ | my $prebox = sub { | ||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||
| 91 | 0 | my $cmd = shift; | |||||||||||||||||||||
| 92 | 0 | my $alt = shift; | |||||||||||||||||||||
| 93 | 0 | $body .= "<pre><div style='margin-bottom: 5px; background: #ddd'><b>$cmd</b></div>"; | |||||||||||||||||||||
| 94 | 0 | ▶ | Perlbal::run_manage_command($cmd, sub { | ||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||
| 95 | 0 | ▶ | my $line = $_[0] || ""; | ||||||||||||||||||||
|
Condition0%0/2
$_[0] || ''
MC/DC0%0/1
$_[0] || ''
$_[0]
Truth table0%0/2
A
A= $_[0]
| |||||||||||||||||||||||
| 96 | 0 | ▶ | $alt->(\$line) if $alt; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 97 | 0 | $body .= "$line\n"; | |||||||||||||||||||||
| 98 | 0 | }); | |||||||||||||||||||||
| 99 | 0 | $body .= "</pre>\n"; | |||||||||||||||||||||
| 100 | |||||||||||||||||||||||
| 101 | 0 | }; | |||||||||||||||||||||
| 102 | |||||||||||||||||||||||
| 103 | 0 | $body .= "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n"; | |||||||||||||||||||||
| 104 | 0 | $body .= "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en-ZA\">\n"; | |||||||||||||||||||||
| 105 | 0 | $body .= "<head><title>perlbal management interface</title><meta name=\"generator\" content=\"perlbal\" /></head><body>"; | |||||||||||||||||||||
| 106 | |||||||||||||||||||||||
| 107 | 0 | ▶ | if ($uri eq "/") { | ||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||
| 108 | 0 | $body .= "<h1>perlbal management interface</h1><ul>"; | |||||||||||||||||||||
| 109 | 0 | $body .= "<li><a href='/socks'>Sockets</a></li>"; | |||||||||||||||||||||
| 110 | 0 | $body .= "<li><a href='/obj'>Perl Objects in use</a></li>"; | |||||||||||||||||||||
| 111 | 0 | $body .= "<li>Service Details<ul>"; | |||||||||||||||||||||
| 112 | 0 | foreach my $sname (Perlbal->service_names) { | |||||||||||||||||||||
| 113 | 0 | my Perlbal::Service $svc = Perlbal->service($sname); | |||||||||||||||||||||
| 114 | 0 | ▶ | next unless $svc; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 115 | 0 | ▶ | my $listen = $svc->{listen} ? " ($svc->{listen})" : ""; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 116 | 0 | $body .= "<li><a href='/service?$sname'>$sname</a> - $svc->{role}$listen</li>\n"; | |||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||
| 118 | 0 | $body .= "</ul></li>"; | |||||||||||||||||||||
| 119 | 0 | $body .= "</ul>"; | |||||||||||||||||||||
| 120 | } elsif ($uri eq "/socks") { | ||||||||||||||||||||||
| 121 | 0 | $prebox->('socks summary'); | |||||||||||||||||||||
| 122 | |||||||||||||||||||||||
| 123 | 0 | ▶ | $prebox->('socks', sub { | ||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||
| 124 | 0 | ${$_[0]} =~ s!service \'(\w+)\'!<a href=\"/service?$1\">$1</a>!; | |||||||||||||||||||||
| 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 .= "<h1>$code</h1>"; | |||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||
| 135 | |||||||||||||||||||||||
| 136 | 0 | $body .= "<hr style='margin-top: 10px' /><p><a href='/'>Perlbal management</a>.</p></body></html>\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 | ▶ | $self->write(sub { $self->close; }); | ||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||
| 140 | 0 | return; | |||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||
| 142 | |||||||||||||||||||||||
| 143 | 22 | 1; | |||||||||||||||||||||
| 144 | |||||||||||||||||||||||
| 145 | |||||||||||||||||||||||
| 146 | # Local Variables: | ||||||||||||||||||||||
| 147 | # mode: perl | ||||||||||||||||||||||
| 148 | # c-basic-indent: 4 | ||||||||||||||||||||||
| 149 | # indent-tabs-mode: nil | ||||||||||||||||||||||
| 150 | # End: | ||||||||||||||||||||||