| 1 | package Browsermob::Server; | ||||||||||||
| 2 | 4 | $Browsermob::Server::VERSION = '0.16'; | |||||||||||
| 3 | # ABSTRACT: Perl client to control the Browsermob Proxy server | ||||||||||||
| 4 | 4 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 4 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 4 | ▶ | use Moo; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 4 | ▶ | use Carp; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 4 | ▶ | use JSON; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | 4 | ▶ | use LWP::UserAgent; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | 4 | ▶ | use IO::Socket::INET; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | 4 | ▶ | use Browsermob::Proxy; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 12 | |||||||||||||
| 13 | |||||||||||||
| 14 | 4 | has path => ( | |||||||||||
| 15 | is => 'rw', | ||||||||||||
| 16 | ); | ||||||||||||
| 17 | |||||||||||||
| 18 | |||||||||||||
| 19 | has server_addr => ( | ||||||||||||
| 20 | is => 'rw', | ||||||||||||
| 21 | 5 | ▶ | default => sub { 'localhost' } | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 22 | 4 | ); | |||||||||||
| 23 | |||||||||||||
| 24 | |||||||||||||
| 25 | has server_port => ( | ||||||||||||
| 26 | is => 'rw', | ||||||||||||
| 27 | init_arg => 'port', | ||||||||||||
| 28 | 5 | ▶ | default => sub { 8080 } | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 29 | 4 | ); | |||||||||||
| 30 | |||||||||||||
| 31 | has _pid => ( | ||||||||||||
| 32 | is => 'rw', | ||||||||||||
| 33 | init_arg => undef, | ||||||||||||
| 34 | 5 | ▶ | default => sub { '' } | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 35 | 4 | ); | |||||||||||
| 36 | |||||||||||||
| 37 | has ua => ( | ||||||||||||
| 38 | is => 'rw', | ||||||||||||
| 39 | lazy => 1, | ||||||||||||
| 40 | default => sub { | ||||||||||||
| 41 | 0 | ▶ | return LWP::UserAgent->new; | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 42 | } | ||||||||||||
| 43 | 4 | ); | |||||||||||
| 44 | |||||||||||||
| 45 | |||||||||||||
| 46 | sub start { | ||||||||||||
| 47 | 0 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
start: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||
| 48 | 0 | ▶ | die '"' . $self->path . '" is an invalid path' unless -f $self->path; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 49 | |||||||||||||
| 50 | 0 | ▶ | defined ($self->_pid(fork)) or die "Error starting server: $!"; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 51 | 0 | ▶ | if ($self->_pid) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 52 | # The parent knows about the child pid | ||||||||||||
| 53 | 0 | ▶ | die "Error starting server: $!" unless $self->_is_listening; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 54 | } | ||||||||||||
| 55 | else { | ||||||||||||
| 56 | # If I don't know the pid, then I'm the child and we should | ||||||||||||
| 57 | # exec to replace ourselves with the proxy | ||||||||||||
| 58 | 0 | my $cmd = 'sh ' . $self->path . ' -port ' . $self->server_port . ' 2>&1 > /dev/null'; | |||||||||||
| 59 | 0 | exec($cmd); | |||||||||||
| 60 | } | ||||||||||||
| 61 | } | ||||||||||||
| 62 | |||||||||||||
| 63 | |||||||||||||
| 64 | sub stop { | ||||||||||||
| 65 | 0 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
stop: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||
| 66 | 0 | ▶ | kill('SIGKILL', $self->_pid) and waitpid($self->_pid, 0); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 67 | } | ||||||||||||
| 68 | |||||||||||||
| 69 | |||||||||||||
| 70 | sub create_proxy { | ||||||||||||
| 71 | 0 | ▶ | my ($self, %args) = @_; | ||||||||||
|
Subroutine
create_proxy: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 72 | |||||||||||||
| 73 | 0 | my $proxy = Browsermob::Proxy->new( | |||||||||||
| 74 | server_addr => $self->server_addr, | ||||||||||||
| 75 | server_port => $self->server_port, | ||||||||||||
| 76 | %args | ||||||||||||
| 77 | ); | ||||||||||||
| 78 | |||||||||||||
| 79 | 0 | return $proxy; | |||||||||||
| 80 | } | ||||||||||||
| 81 | |||||||||||||
| 82 | |||||||||||||
| 83 | sub get_proxies { | ||||||||||||
| 84 | 3 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
get_proxies: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||
| 85 | 3 | my $ua = $self->ua; | |||||||||||
| 86 | |||||||||||||
| 87 | 3 | my $res = $ua->get('http://' . $self->server_addr . ':' . $self->server_port . '/proxy'); | |||||||||||
| 88 | 3 | ▶ | if ($res->is_success) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 89 | 3 | my $list = from_json($res->decoded_content)->{proxyList}; | |||||||||||
| 90 | |||||||||||||
| 91 | my @proxies = map { | ||||||||||||
| 92 | 3 | $_->{port}; | |||||||||||
| 93 | } @$list; | ||||||||||||
| 94 | |||||||||||||
| 95 | 3 | return \@proxies; | |||||||||||
| 96 | } | ||||||||||||
| 97 | |||||||||||||
| 98 | } | ||||||||||||
| 99 | |||||||||||||
| 100 | |||||||||||||
| 101 | sub find_open_port { | ||||||||||||
| 102 | 3 | ▶ | my ($self, @range) = @_; | ||||||||||
|
Subroutine
find_open_port: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||
| 103 | 3 | my $proxies = $self->get_proxies; | |||||||||||
| 104 | |||||||||||||
| 105 | 3 | my $count; | |||||||||||
| 106 | 3 | foreach (@range, @$proxies) { | |||||||||||
| 107 | 38 | $count->{$_}++; | |||||||||||
| 108 | } | ||||||||||||
| 109 | |||||||||||||
| 110 | 3 | foreach (sort @range) { | |||||||||||
| 111 | 4 | ▶ | if ($count->{$_} == 1) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 112 | 3 | return $_; | |||||||||||
| 113 | } | ||||||||||||
| 114 | } | ||||||||||||
| 115 | } | ||||||||||||
| 116 | |||||||||||||
| 117 | |||||||||||||
| 118 | sub _is_listening { | ||||||||||||
| 119 | 2 | ▶ | my ($self, $timeout) = @_; | ||||||||||
|
Subroutine
_is_listening: called CC=2 SCAR=7.0
| |||||||||||||
| 120 | 2 | ▶ | $timeout //= 30; | ||||||||||
|
Condition50%1/2
$timeout //= 30
MC/DC0%0/1
$timeout //= 30
$timeout
Truth table50%1/2
A
A= $timeout
| |||||||||||||
| 121 | |||||||||||||
| 122 | 2 | my $sock = IO::Socket::INET->new( | |||||||||||
| 123 | PeerAddr => $self->server_addr, | ||||||||||||
| 124 | PeerPort => $self->server_port, | ||||||||||||
| 125 | Timeout => $timeout | ||||||||||||
| 126 | ); | ||||||||||||
| 127 | |||||||||||||
| 128 | 2 | return $sock; | |||||||||||
| 129 | } | ||||||||||||
| 130 | |||||||||||||
| 131 | 4 | 1; | |||||||||||
| 132 | |||||||||||||
| 133 | __END__ | ||||||||||||
| 134 | |||||||||||||
| 135 | =pod | ||||||||||||
| 136 | |||||||||||||
| 137 | =encoding UTF-8 | ||||||||||||
| 138 | |||||||||||||
| 139 | =head1 NAME | ||||||||||||
| 140 | |||||||||||||
| 141 | Browsermob::Server - Perl client to control the Browsermob Proxy server | ||||||||||||
| 142 | |||||||||||||
| 143 | =head1 VERSION | ||||||||||||
| 144 | |||||||||||||
| 145 | version 0.16 | ||||||||||||
| 146 | |||||||||||||
| 147 | =head1 SYNOPSIS | ||||||||||||
| 148 | |||||||||||||
| 149 | my $server = Browsermob::Server->new( | ||||||||||||
| 150 | path => '/opt/browsermob-proxy-2.0-beta-9/bin/browsermob-proxy' | ||||||||||||
| 151 | ); | ||||||||||||
| 152 | $server->start; # ignore if your server is already started | ||||||||||||
| 153 | |||||||||||||
| 154 | my $proxy = $server->create_proxy; | ||||||||||||
| 155 | my $port = $proxy->port; | ||||||||||||
| 156 | |||||||||||||
| 157 | $proxy->new_har; | ||||||||||||
| 158 | |||||||||||||
| 159 | # generate traffic across your port | ||||||||||||
| 160 | `curl -x http://localhost:$port http://www.google.com > /dev/null 2>&1`; | ||||||||||||
| 161 | |||||||||||||
| 162 | print Dumper $proxy->har; | ||||||||||||
| 163 | |||||||||||||
| 164 | =head1 DESCRIPTION | ||||||||||||
| 165 | |||||||||||||
| 166 | This class provides a way to control the Browsermob Proxy server | ||||||||||||
| 167 | within Perl. There are only a few public methods for starting and | ||||||||||||
| 168 | stopping the server. You also have the option of instantiating a | ||||||||||||
| 169 | server object and pointing it towards an existing BMP server on | ||||||||||||
| 170 | localhost, and just using it to avoid having to pass the server_port | ||||||||||||
| 171 | arg when instantiating new proxies. | ||||||||||||
| 172 | |||||||||||||
| 173 | =head1 ATTRIBUTES | ||||||||||||
| 174 | |||||||||||||
| 175 | =head2 path | ||||||||||||
| 176 | |||||||||||||
| 177 | The path to the browsermob_proxy binary. If you aren't planning to | ||||||||||||
| 178 | call C<start>, this is optional. | ||||||||||||
| 179 | |||||||||||||
| 180 | =head2 server_addr | ||||||||||||
| 181 | |||||||||||||
| 182 | The address of the remote server where the Browsermob Proxy server is | ||||||||||||
| 183 | running. This defaults to localhost. | ||||||||||||
| 184 | |||||||||||||
| 185 | =head2 port | ||||||||||||
| 186 | |||||||||||||
| 187 | The port on which the proxy server should run. This is not the port | ||||||||||||
| 188 | that you should have other clients connect. | ||||||||||||
| 189 | |||||||||||||
| 190 | =head1 METHODS | ||||||||||||
| 191 | |||||||||||||
| 192 | =head2 start | ||||||||||||
| 193 | |||||||||||||
| 194 | Start a browsermob proxy on C<port>. Starting the server does not create | ||||||||||||
| 195 | any proxies. | ||||||||||||
| 196 | |||||||||||||
| 197 | =head2 stop | ||||||||||||
| 198 | |||||||||||||
| 199 | Stop the forked browsermob-proxy server. This does not work all the | ||||||||||||
| 200 | time, although the server seems to get GC'd all on its own, even after | ||||||||||||
| 201 | ignoring a C<SIGTERM>. | ||||||||||||
| 202 | |||||||||||||
| 203 | =head2 create_proxy | ||||||||||||
| 204 | |||||||||||||
| 205 | After starting the server, or connecting to an existing one, use | ||||||||||||
| 206 | C<create_proxy> to get a proxy that you can use with your tests. No | ||||||||||||
| 207 | proxies actually exist until you call create_proxy; starting the | ||||||||||||
| 208 | server does not create a proxy. | ||||||||||||
| 209 | |||||||||||||
| 210 | my $proxy = $bmp->create_proxy; # returns a Browsermob::Proxy object | ||||||||||||
| 211 | my $proxy = $bmp->create_proxy(port => 1337); | ||||||||||||
| 212 | |||||||||||||
| 213 | =head2 get_proxies | ||||||||||||
| 214 | |||||||||||||
| 215 | Get a list of currently registered proxies. | ||||||||||||
| 216 | |||||||||||||
| 217 | my $proxy_aref = $bmp->get_proxies->{proxyList}; | ||||||||||||
| 218 | print scalar @$proxy_aref; | ||||||||||||
| 219 | |||||||||||||
| 220 | =head2 find_open_port | ||||||||||||
| 221 | |||||||||||||
| 222 | Given a range of valid ports, finds the lowest unused port by | ||||||||||||
| 223 | searching the proxyList. | ||||||||||||
| 224 | |||||||||||||
| 225 | my $unused_port = $bmp->find_open_port; | ||||||||||||
| 226 | my $proxy = $bmp->create_proxy(port => $unused_port); | ||||||||||||
| 227 | |||||||||||||
| 228 | =head1 SEE ALSO | ||||||||||||
| 229 | |||||||||||||
| 230 | Please see those modules/websites for more information related to this module. | ||||||||||||
| 231 | |||||||||||||
| 232 | =over 4 | ||||||||||||
| 233 | |||||||||||||
| 234 | =item * | ||||||||||||
| 235 | |||||||||||||
| 236 | L<Browsermob::Proxy|Browsermob::Proxy> | ||||||||||||
| 237 | |||||||||||||
| 238 | =back | ||||||||||||
| 239 | |||||||||||||
| 240 | =head1 BUGS | ||||||||||||
| 241 | |||||||||||||
| 242 | Please report any bugs or feature requests on the bugtracker website | ||||||||||||
| 243 | https://github.com/gempesaw/Browsermob-Proxy/issues | ||||||||||||
| 244 | |||||||||||||
| 245 | When submitting a bug or request, please include a test-file or a | ||||||||||||
| 246 | patch to an existing test-file that illustrates the bug or desired | ||||||||||||
| 247 | feature. | ||||||||||||
| 248 | |||||||||||||
| 249 | =head1 AUTHOR | ||||||||||||
| 250 | |||||||||||||
| 251 | Daniel Gempesaw <gempesaw@gmail.com> | ||||||||||||
| 252 | |||||||||||||
| 253 | =head1 COPYRIGHT AND LICENSE | ||||||||||||
| 254 | |||||||||||||
| 255 | This software is copyright (c) 2014 by Daniel Gempesaw. | ||||||||||||
| 256 | |||||||||||||
| 257 | This is free software; you can redistribute it and/or modify it under | ||||||||||||
| 258 | the same terms as the Perl 5 programming language system itself. | ||||||||||||
| 259 | |||||||||||||
| 260 | =cut | ||||||||||||