GuacLite/Client/Web.pm untested

filter: Statementstmt 0.0% 0 / 59 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 109 CC 13 SCAR 41.8
CC
13
Cov
0%
_tunnel
132.0
run
12.0
startup
2.0
CRAP 65.5
1 package GuacLite::Client::Web;
2  
3 use Mojo::Base 'Mojolicious';
4  
5 use Mojo::File;
6 use Mojo::JSON;
7 use Mojo::URL;
8  
9 use GuacLite::Client::Guacd;
10  
11 use File::ShareDir;
12  
13 has dist_dir => sub {
14 my $cpanfile = Mojo::File::curfile->dirname->dirname->dirname->sibling('cpanfile');
15 return $cpanfile->sibling('share') if -e $cpanfile;
16 return Mojo::File->new(File::ShareDir::dist_dir('GuacLite'));
17 };
18  
19 sub startup {
20 my $app = shift;
21  
22 $app->commands->namespaces([
23 'Mojolicious::Command',
24 'GuacLite::Client::Web::Command',
25 ]);
26  
27 $app->renderer->paths([
28 $app->dist_dir->child('templates'),
29 ]);
30 $app->static->paths([
31 $app->dist_dir->child('public'),
32 ]);
33  
34 $app->plugin('GuacLite::Plugin::Guacd');
35  
36 my $r = $app->routes;
37 $r->get('/')->to(template => 'index');
38 $r->websocket('/tunnel')->to(cb => \&_tunnel)->name('tunnel');
39 }
40  
41 sub _tunnel {
42 my $c = shift;
43  
44 my $json = $c->req->params->pairs->[0];
45 my $params = Mojo::JSON::from_json($json);
46 my $target = Mojo::URL->new($params->{target});
47  
48 my $connection = $target->query->to_hash;
49 $connection->{hostname} ||= $target->host if $target->host;
50 $connection->{port} ||= $target->port if $target->port;
51  
52 my $client = GuacLite::Client::Guacd->new(
53 $params->{client}{audio} ? (audio_mimetypes => $params->{client}{audio} ) : (),
54 $params->{client}{video} ? (video_mimetypes => $params->{client}{video} ) : (),
55 $params->{client}{image} ? (image_mimetypes => $params->{client}{image} ) : (),
56 $params->{client}{timezone} ? (timezone => $params->{client}{timezone}) : (),
57 $params->{client}{width} ? (width => $params->{client}{width} ) : (),
58 $params->{client}{height} ? (height => $params->{client}{height} ) : (),
59 $params->{client}{dpi} ? (dpi => $params->{client}{dpi} ) : (),
60 $target->protocol ? (protocol => $target->protocol ) : (),
61 connection_args => $connection,
62 );
63  
64 return $c->guacd->tunnel($client);
65 }
66  
67 {
68 package GuacLite::Client::Web::Command::pack;
69  
70 use Mojo::Base 'Mojolicious::Command';
71  
72 has description => 'Bundle guacamole-common-js assets';
73 has usage => <<"END";
74 Usage: $0 pack path/to/guacamole-client/guacamole-common-js/src/main/webapp/modules/
75  
76 Bundle the assets in a given directory and write it to STDOUT.
77 The resulting javascript file exposes a Guacamole global with an "initialize" method used to load the rest of the library.
78 This "initialize" allows deferring loading the audio system until after a user action which is required by modern browsers.
79 END
80  
81 sub run {
82 my ($command, $path) = @_;
83 warn $path;
84  
85 die "a path is required" unless $path;
86 die "$path does not exist or is not a directory" unless -d $path;
87  
88 require GuacLite::Util;
89 print GuacLite::Util::pack_js($path);
90 }
91 }
92  
93 1;
94