From 541e2b41af8cc44ad3eb0638618bc457c666d612 Mon Sep 17 00:00:00 2001 From: Dylan William Hardison Date: Sat, 7 Apr 2018 19:20:00 -0400 Subject: a bit of a quantum leap It's now possible to load the CGIs into a mojolicious controller. Compatibility isn't 100% yet, but it should give a migration path for any random CGI to become a proper controller. --- Bugzilla/Quantum/Plugin/Glue.pm | 61 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 Bugzilla/Quantum/Plugin/Glue.pm (limited to 'Bugzilla/Quantum/Plugin') diff --git a/Bugzilla/Quantum/Plugin/Glue.pm b/Bugzilla/Quantum/Plugin/Glue.pm new file mode 100644 index 000000000..d689f598a --- /dev/null +++ b/Bugzilla/Quantum/Plugin/Glue.pm @@ -0,0 +1,61 @@ +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. +# +# This Source Code Form is "Incompatible With Secondary Licenses", as +# defined by the Mozilla Public License, v. 2.0. + +package Bugzilla::Quantum::Plugin::Glue; +use Mojo::Base 'Mojolicious::Plugin'; + +use Try::Tiny; +use Bugzilla::Constants; +use Bugzilla::Quantum::CGI; +use Bugzilla::Quantum::Template; + +sub register { + my ( $self, $app, $conf ) = @_; + + my $template = Bugzilla::Template->create; + $template->{_is_main} = 1; + + $app->renderer->add_handler( + 'bugzilla' => sub { + my ( $renderer, $c, $output, $options ) = @_; + my %params; + + # Helpers + foreach my $method ( grep {m/^\w+\z/} keys %{ $renderer->helpers } ) { + my $sub = $renderer->helpers->{$method}; + $params{$method} = sub { $c->$sub(@_) }; + } + + # Stash values + $params{$_} = $c->stash->{$_} for grep {m/^\w+\z/} keys %{ $c->stash }; + $params{self} = $params{c} = $c; + my $name = $options->{template}; + unless ($name =~ /\./) { + $name = sprintf '%s.%s.tmpl', $options->{template}, $options->{format}; + } + $template->process( $name, \%params, $output ) + or die $template->error; + } + ); + + $app->hook( + around_dispatch => sub { + my ($next, $c) = @_; + try { + local %{ Bugzilla->request_cache } = (); + Bugzilla->usage_mode(USAGE_MODE_QUANTUM); + Bugzilla->cgi( Bugzilla::Quantum::CGI->new(controller => $c) ); + Bugzilla->template( Bugzilla::Quantum::Template->new( controller => $c, template => $template ) ); + $next->(); + } catch { + die $_ unless /\bModPerl::Util::exit\b/; + }; + } + ); +} + +1; \ No newline at end of file -- cgit v1.2.3-24-g4f1b From 10918f3336863623020a6d73e63a0f0a5eebb306 Mon Sep 17 00:00:00 2001 From: Dylan William Hardison Date: Sat, 19 May 2018 00:34:01 -0400 Subject: mojo all the things --- Bugzilla/Quantum/Plugin/Glue.pm | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'Bugzilla/Quantum/Plugin') diff --git a/Bugzilla/Quantum/Plugin/Glue.pm b/Bugzilla/Quantum/Plugin/Glue.pm index d689f598a..6c20d87f7 100644 --- a/Bugzilla/Quantum/Plugin/Glue.pm +++ b/Bugzilla/Quantum/Plugin/Glue.pm @@ -47,12 +47,13 @@ sub register { my ($next, $c) = @_; try { local %{ Bugzilla->request_cache } = (); + local $CGI::Compile::USE_REAL_EXIT = 0; Bugzilla->usage_mode(USAGE_MODE_QUANTUM); Bugzilla->cgi( Bugzilla::Quantum::CGI->new(controller => $c) ); Bugzilla->template( Bugzilla::Quantum::Template->new( controller => $c, template => $template ) ); $next->(); } catch { - die $_ unless /\bModPerl::Util::exit\b/; + die $_ unless ref $_ eq 'ARRAY' && $_->[0] eq "EXIT\n" || /\bModPerl::Util::exit\b/; }; } ); -- cgit v1.2.3-24-g4f1b From 216af94b979206ee77d3f3b783648a959b120547 Mon Sep 17 00:00:00 2001 From: Dylan William Hardison Date: Sat, 19 May 2018 11:33:46 -0400 Subject: more things --- Bugzilla/Quantum/Plugin/Glue.pm | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'Bugzilla/Quantum/Plugin') diff --git a/Bugzilla/Quantum/Plugin/Glue.pm b/Bugzilla/Quantum/Plugin/Glue.pm index 6c20d87f7..a28c2598d 100644 --- a/Bugzilla/Quantum/Plugin/Glue.pm +++ b/Bugzilla/Quantum/Plugin/Glue.pm @@ -48,7 +48,8 @@ sub register { try { local %{ Bugzilla->request_cache } = (); local $CGI::Compile::USE_REAL_EXIT = 0; - Bugzilla->usage_mode(USAGE_MODE_QUANTUM); + # HACK, should just make i_am_cgi smarter. + local $ENV{'SERVER_SOFTWARE'} = 1; Bugzilla->cgi( Bugzilla::Quantum::CGI->new(controller => $c) ); Bugzilla->template( Bugzilla::Quantum::Template->new( controller => $c, template => $template ) ); $next->(); -- cgit v1.2.3-24-g4f1b From 889d8ad22855191773dd45e67272650d0c00f502 Mon Sep 17 00:00:00 2001 From: Dylan William Hardison Date: Sun, 20 May 2018 22:00:08 -0400 Subject: cool stuff --- Bugzilla/Quantum/Plugin/Glue.pm | 52 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 47 insertions(+), 5 deletions(-) (limited to 'Bugzilla/Quantum/Plugin') diff --git a/Bugzilla/Quantum/Plugin/Glue.pm b/Bugzilla/Quantum/Plugin/Glue.pm index a28c2598d..abada14f8 100644 --- a/Bugzilla/Quantum/Plugin/Glue.pm +++ b/Bugzilla/Quantum/Plugin/Glue.pm @@ -10,8 +10,9 @@ use Mojo::Base 'Mojolicious::Plugin'; use Try::Tiny; use Bugzilla::Constants; -use Bugzilla::Quantum::CGI; use Bugzilla::Quantum::Template; +use Socket qw(AF_INET inet_aton); +use Sys::Hostname; sub register { my ( $self, $app, $conf ) = @_; @@ -46,13 +47,13 @@ sub register { around_dispatch => sub { my ($next, $c) = @_; try { - local %{ Bugzilla->request_cache } = (); local $CGI::Compile::USE_REAL_EXIT = 0; - # HACK, should just make i_am_cgi smarter. - local $ENV{'SERVER_SOFTWARE'} = 1; - Bugzilla->cgi( Bugzilla::Quantum::CGI->new(controller => $c) ); + local %ENV = _ENV($c); + Bugzilla::init_page(); + Bugzilla::request_cache->{mojo_controller} = $c; Bugzilla->template( Bugzilla::Quantum::Template->new( controller => $c, template => $template ) ); $next->(); + Bugzilla::_cleanup; ## no critic (private) } catch { die $_ unless ref $_ eq 'ARRAY' && $_->[0] eq "EXIT\n" || /\bModPerl::Util::exit\b/; }; @@ -60,4 +61,45 @@ sub register { ); } +sub _ENV { + my ($c) = @_; + my $tx = $c->tx; + my $req = $tx->req; + my $headers = $req->headers; + my $content_length = $req->content->is_multipart ? $req->body_size : $headers->content_length; + my %env_headers = ( HTTP_COOKIE => '', HTTP_REFERER => '' ); + + for my $name ( @{ $headers->names } ) { + my $key = uc "http_$name"; + $key =~ s!\W!_!g; + $env_headers{$key} = $headers->header($name); + } + + if ( my $userinfo = $c->req->url->to_abs->userinfo ) { + $remote_user = $userinfo =~ /([^:]+)/ ? $1 : ''; + } + elsif ( my $authenticate = $headers->authorization ) { + $remote_user = $authenticate =~ /Basic\s+(.*)/ ? b64_decode $1 : ''; + $remote_user = $remote_user =~ /([^:]+)/ ? $1 : ''; + } + + return ( + CONTENT_LENGTH => $content_length || 0, + CONTENT_TYPE => $headers->content_type || '', + GATEWAY_INTERFACE => 'CGI/1.1', + HTTPS => $req->is_secure ? 'YES' : 'NO', + %env_headers, + QUERY_STRING => $c->stash('cgi.query_string') || $req->url->query->to_string, + REMOTE_ADDR => $tx->remote_address, + REMOTE_HOST => gethostbyaddr( inet_aton( $tx->remote_address || '127.0.0.1' ), AF_INET ) || '', + REMOTE_PORT => $tx->remote_port, + REMOTE_USER => $remote_user || '', + REQUEST_METHOD => $req->method, + SCRIPT_NAME => $req->env->{SCRIPT_NAME} SERVER_NAME => hostname, + SERVER_PORT => $tx->local_port, + SERVER_PROTOCOL => $req->is_secure ? 'HTTPS' : 'HTTP', # TODO: Version is missing + SERVER_SOFTWARE => __PACKAGE__, + ); +} + 1; \ No newline at end of file -- cgit v1.2.3-24-g4f1b From 873074873fe5e27e853e5ce3d83b078280a29c2a Mon Sep 17 00:00:00 2001 From: Dylan William Hardison Date: Sun, 20 May 2018 23:16:37 -0400 Subject: more things work --- Bugzilla/Quantum/Plugin/Glue.pm | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) (limited to 'Bugzilla/Quantum/Plugin') diff --git a/Bugzilla/Quantum/Plugin/Glue.pm b/Bugzilla/Quantum/Plugin/Glue.pm index abada14f8..2e3ecf4f0 100644 --- a/Bugzilla/Quantum/Plugin/Glue.pm +++ b/Bugzilla/Quantum/Plugin/Glue.pm @@ -26,10 +26,12 @@ sub register { my %params; # Helpers + my %helper; foreach my $method ( grep {m/^\w+\z/} keys %{ $renderer->helpers } ) { my $sub = $renderer->helpers->{$method}; - $params{$method} = sub { $c->$sub(@_) }; + $helper{$method} = sub { $c->$sub(@_) }; } + $params{helper} = \%helper; # Stash values $params{$_} = $c->stash->{$_} for grep {m/^\w+\z/} keys %{ $c->stash }; @@ -49,11 +51,16 @@ sub register { try { local $CGI::Compile::USE_REAL_EXIT = 0; local %ENV = _ENV($c); + my $stdin = _STDIN($c); + warn "stdin path ", $stdin->path, "\n"; + local *STDIN; + open STDIN, '<', $stdin->path or die "STDIN @{[$stdin->path]}: $!" if -s $stdin->path; Bugzilla::init_page(); - Bugzilla::request_cache->{mojo_controller} = $c; + Bugzilla->request_cache->{mojo_controller} = $c; Bugzilla->template( Bugzilla::Quantum::Template->new( controller => $c, template => $template ) ); $next->(); Bugzilla::_cleanup; ## no critic (private) + CGI::initialize_globals(); } catch { die $_ unless ref $_ eq 'ARRAY' && $_->[0] eq "EXIT\n" || /\bModPerl::Util::exit\b/; }; @@ -75,6 +82,7 @@ sub _ENV { $env_headers{$key} = $headers->header($name); } + my $remote_user; if ( my $userinfo = $c->req->url->to_abs->userinfo ) { $remote_user = $userinfo =~ /([^:]+)/ ? $1 : ''; } @@ -95,11 +103,29 @@ sub _ENV { REMOTE_PORT => $tx->remote_port, REMOTE_USER => $remote_user || '', REQUEST_METHOD => $req->method, - SCRIPT_NAME => $req->env->{SCRIPT_NAME} SERVER_NAME => hostname, + SCRIPT_NAME => $req->env->{SCRIPT_NAME}, + SERVER_NAME => hostname, SERVER_PORT => $tx->local_port, SERVER_PROTOCOL => $req->is_secure ? 'HTTPS' : 'HTTP', # TODO: Version is missing SERVER_SOFTWARE => __PACKAGE__, ); } +sub _STDIN { + my $c = shift; + my $stdin; + + if ( $c->req->content->is_multipart ) { + $stdin = Mojo::Asset::File->new; + $stdin->add_chunk( $c->req->build_body ); + } + else { + $stdin = $c->req->content->asset; + } + + return $stdin if $stdin->isa('Mojo::Asset::File'); + return Mojo::Asset::File->new->add_chunk( $stdin->slurp ); +} + + 1; \ No newline at end of file -- cgit v1.2.3-24-g4f1b From 3517d8a6687cd37cb8c9009a78f16071d652254a Mon Sep 17 00:00:00 2001 From: Dylan William Hardison Date: Mon, 21 May 2018 11:38:41 -0400 Subject: almost working --- Bugzilla/Quantum/Plugin/Glue.pm | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) (limited to 'Bugzilla/Quantum/Plugin') diff --git a/Bugzilla/Quantum/Plugin/Glue.pm b/Bugzilla/Quantum/Plugin/Glue.pm index 2e3ecf4f0..17dc443c6 100644 --- a/Bugzilla/Quantum/Plugin/Glue.pm +++ b/Bugzilla/Quantum/Plugin/Glue.pm @@ -13,6 +13,7 @@ use Bugzilla::Constants; use Bugzilla::Quantum::Template; use Socket qw(AF_INET inet_aton); use Sys::Hostname; +use IO::String; sub register { my ( $self, $app, $conf ) = @_; @@ -23,7 +24,7 @@ sub register { $app->renderer->add_handler( 'bugzilla' => sub { my ( $renderer, $c, $output, $options ) = @_; - my %params; + my $vars = delete $c->stash->{vars}; # Helpers my %helper; @@ -31,16 +32,15 @@ sub register { my $sub = $renderer->helpers->{$method}; $helper{$method} = sub { $c->$sub(@_) }; } - $params{helper} = \%helper; + $vars->{helper} = \%helper; - # Stash values - $params{$_} = $c->stash->{$_} for grep {m/^\w+\z/} keys %{ $c->stash }; - $params{self} = $params{c} = $c; + # The controller + $vars->{c} = $c; my $name = $options->{template}; unless ($name =~ /\./) { $name = sprintf '%s.%s.tmpl', $options->{template}, $options->{format}; } - $template->process( $name, \%params, $output ) + $template->process( $name, $vars, $output ) or die $template->error; } ); @@ -48,21 +48,32 @@ sub register { $app->hook( around_dispatch => sub { my ($next, $c) = @_; + local %ENV = _ENV($c); + my $stdin = _STDIN($c); + my $stdout = ''; try { local $CGI::Compile::USE_REAL_EXIT = 0; - local %ENV = _ENV($c); - my $stdin = _STDIN($c); - warn "stdin path ", $stdin->path, "\n"; - local *STDIN; + local *STDIN; ## no critic (local) + local *STDOUT; open STDIN, '<', $stdin->path or die "STDIN @{[$stdin->path]}: $!" if -s $stdin->path; + open STDOUT, '>', \$stdout or die "STDOUT capture: $!"; + Bugzilla::init_page(); Bugzilla->request_cache->{mojo_controller} = $c; Bugzilla->template( Bugzilla::Quantum::Template->new( controller => $c, template => $template ) ); $next->(); + } + catch { + die $_ unless ref $_ eq 'ARRAY' && $_->[0] eq "EXIT\n" || /\bModPerl::Util::exit\b/; + } + finally { + if (length $stdout) { + warn "setting body\n"; + $c->res->body($stdout); + $c->rendered; + } Bugzilla::_cleanup; ## no critic (private) CGI::initialize_globals(); - } catch { - die $_ unless ref $_ eq 'ARRAY' && $_->[0] eq "EXIT\n" || /\bModPerl::Util::exit\b/; }; } ); -- cgit v1.2.3-24-g4f1b From 3d8dc32afa0131e2ac0cd121b492aed8ad59fc11 Mon Sep 17 00:00:00 2001 From: Dylan William Hardison Date: Mon, 21 May 2018 14:14:53 -0400 Subject: working more --- Bugzilla/Quantum/Plugin/Glue.pm | 93 ----------------------------------------- 1 file changed, 93 deletions(-) (limited to 'Bugzilla/Quantum/Plugin') diff --git a/Bugzilla/Quantum/Plugin/Glue.pm b/Bugzilla/Quantum/Plugin/Glue.pm index 17dc443c6..822987ad8 100644 --- a/Bugzilla/Quantum/Plugin/Glue.pm +++ b/Bugzilla/Quantum/Plugin/Glue.pm @@ -11,9 +11,6 @@ use Mojo::Base 'Mojolicious::Plugin'; use Try::Tiny; use Bugzilla::Constants; use Bugzilla::Quantum::Template; -use Socket qw(AF_INET inet_aton); -use Sys::Hostname; -use IO::String; sub register { my ( $self, $app, $conf ) = @_; @@ -44,99 +41,9 @@ sub register { or die $template->error; } ); - - $app->hook( - around_dispatch => sub { - my ($next, $c) = @_; - local %ENV = _ENV($c); - my $stdin = _STDIN($c); - my $stdout = ''; - try { - local $CGI::Compile::USE_REAL_EXIT = 0; - local *STDIN; ## no critic (local) - local *STDOUT; - open STDIN, '<', $stdin->path or die "STDIN @{[$stdin->path]}: $!" if -s $stdin->path; - open STDOUT, '>', \$stdout or die "STDOUT capture: $!"; - - Bugzilla::init_page(); - Bugzilla->request_cache->{mojo_controller} = $c; - Bugzilla->template( Bugzilla::Quantum::Template->new( controller => $c, template => $template ) ); - $next->(); - } - catch { - die $_ unless ref $_ eq 'ARRAY' && $_->[0] eq "EXIT\n" || /\bModPerl::Util::exit\b/; - } - finally { - if (length $stdout) { - warn "setting body\n"; - $c->res->body($stdout); - $c->rendered; - } - Bugzilla::_cleanup; ## no critic (private) - CGI::initialize_globals(); - }; - } - ); -} - -sub _ENV { - my ($c) = @_; - my $tx = $c->tx; - my $req = $tx->req; - my $headers = $req->headers; - my $content_length = $req->content->is_multipart ? $req->body_size : $headers->content_length; - my %env_headers = ( HTTP_COOKIE => '', HTTP_REFERER => '' ); - - for my $name ( @{ $headers->names } ) { - my $key = uc "http_$name"; - $key =~ s!\W!_!g; - $env_headers{$key} = $headers->header($name); - } - - my $remote_user; - if ( my $userinfo = $c->req->url->to_abs->userinfo ) { - $remote_user = $userinfo =~ /([^:]+)/ ? $1 : ''; - } - elsif ( my $authenticate = $headers->authorization ) { - $remote_user = $authenticate =~ /Basic\s+(.*)/ ? b64_decode $1 : ''; - $remote_user = $remote_user =~ /([^:]+)/ ? $1 : ''; - } - - return ( - CONTENT_LENGTH => $content_length || 0, - CONTENT_TYPE => $headers->content_type || '', - GATEWAY_INTERFACE => 'CGI/1.1', - HTTPS => $req->is_secure ? 'YES' : 'NO', - %env_headers, - QUERY_STRING => $c->stash('cgi.query_string') || $req->url->query->to_string, - REMOTE_ADDR => $tx->remote_address, - REMOTE_HOST => gethostbyaddr( inet_aton( $tx->remote_address || '127.0.0.1' ), AF_INET ) || '', - REMOTE_PORT => $tx->remote_port, - REMOTE_USER => $remote_user || '', - REQUEST_METHOD => $req->method, - SCRIPT_NAME => $req->env->{SCRIPT_NAME}, - SERVER_NAME => hostname, - SERVER_PORT => $tx->local_port, - SERVER_PROTOCOL => $req->is_secure ? 'HTTPS' : 'HTTP', # TODO: Version is missing - SERVER_SOFTWARE => __PACKAGE__, - ); } -sub _STDIN { - my $c = shift; - my $stdin; - - if ( $c->req->content->is_multipart ) { - $stdin = Mojo::Asset::File->new; - $stdin->add_chunk( $c->req->build_body ); - } - else { - $stdin = $c->req->content->asset; - } - return $stdin if $stdin->isa('Mojo::Asset::File'); - return Mojo::Asset::File->new->add_chunk( $stdin->slurp ); -} 1; \ No newline at end of file -- cgit v1.2.3-24-g4f1b From e1b48df3b5cdd81920782a8585864af3b294e919 Mon Sep 17 00:00:00 2001 From: Dylan William Hardison Date: Tue, 19 Jun 2018 21:28:50 -0400 Subject: lots of hacking --- Bugzilla/Quantum/Plugin/Glue.pm | 44 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 2 deletions(-) (limited to 'Bugzilla/Quantum/Plugin') diff --git a/Bugzilla/Quantum/Plugin/Glue.pm b/Bugzilla/Quantum/Plugin/Glue.pm index 822987ad8..b46ffb1e1 100644 --- a/Bugzilla/Quantum/Plugin/Glue.pm +++ b/Bugzilla/Quantum/Plugin/Glue.pm @@ -11,12 +11,45 @@ use Mojo::Base 'Mojolicious::Plugin'; use Try::Tiny; use Bugzilla::Constants; use Bugzilla::Quantum::Template; +use Bugzilla::Logging; sub register { my ( $self, $app, $conf ) = @_; - my $template = Bugzilla::Template->create; - $template->{_is_main} = 1; + my %D; + if ($ENV{BUGZILLA_HTTPD_ARGS}) { + my $args = decode_json($ENV{BUGZILLA_HTTPD_ARGS}); + foreach my $arg (@$args) { + if ($arg =~ /^-D(\w+)$/) { + $D{$1} = 1; + } + else { + die "Unknown httpd arg: $arg"; + } + } + } + + $app->hook( + around_dispatch => sub { + my ($next, $c) = @_; + + if ($D{HTTPD_IN_SUBDIR}) { + my $path = $c->req->url->path; + $path =~ s{^/bmo}{}s; + $c->req->url->path($path); + } + $next->(); + } + ); + + Bugzilla::Extension->load_all(); + if ($app->mode ne 'development') { + Bugzilla->preload_features(); + DEBUG("preloading templates"); + Bugzilla->preload_templates(); + DEBUG("done preloading templates"); + } + $app->secrets([Bugzilla->localconfig->{side_wide_secret}]); $app->renderer->add_handler( 'bugzilla' => sub { @@ -37,10 +70,17 @@ sub register { unless ($name =~ /\./) { $name = sprintf '%s.%s.tmpl', $options->{template}, $options->{format}; } + my $template = Bugzilla->template; $template->process( $name, $vars, $output ) or die $template->error; } ); + + $app->log( + MojoX::Log::Log4perl::Tiny->new( + logger => Log::Log4perl->get_logger(ref $app) + ) + ); } -- cgit v1.2.3-24-g4f1b From 5ac448e57e94bb8f286df0cfcebb7dfed066ed40 Mon Sep 17 00:00:00 2001 From: Dylan William Hardison Date: Wed, 20 Jun 2018 17:10:32 -0400 Subject: add hypnotoad to vagrant --- Bugzilla/Quantum/Plugin/Glue.pm | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'Bugzilla/Quantum/Plugin') diff --git a/Bugzilla/Quantum/Plugin/Glue.pm b/Bugzilla/Quantum/Plugin/Glue.pm index b46ffb1e1..19896817c 100644 --- a/Bugzilla/Quantum/Plugin/Glue.pm +++ b/Bugzilla/Quantum/Plugin/Glue.pm @@ -29,6 +29,13 @@ sub register { } } + # hypnotoad is weird and doesn't look for MOJO_LISTEN itself. + $app->config( + hypnotoad => { + listen => [ $ENV{MOJO_LISTEN} ], + }, + ); + $app->hook( around_dispatch => sub { my ($next, $c) = @_; @@ -86,4 +93,4 @@ sub register { -1; \ No newline at end of file +1; -- cgit v1.2.3-24-g4f1b From 64830ef3c076b72d9da3eb590f22bc4d722c8d4c Mon Sep 17 00:00:00 2001 From: Dylan William Hardison Date: Thu, 21 Jun 2018 11:05:53 -0400 Subject: more fixes --- Bugzilla/Quantum/Plugin/Glue.pm | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'Bugzilla/Quantum/Plugin') diff --git a/Bugzilla/Quantum/Plugin/Glue.pm b/Bugzilla/Quantum/Plugin/Glue.pm index 19896817c..07ac10c49 100644 --- a/Bugzilla/Quantum/Plugin/Glue.pm +++ b/Bugzilla/Quantum/Plugin/Glue.pm @@ -12,6 +12,7 @@ use Try::Tiny; use Bugzilla::Constants; use Bugzilla::Quantum::Template; use Bugzilla::Logging; +use JSON::MaybeXS qw(decode_json); sub register { my ( $self, $app, $conf ) = @_; @@ -36,6 +37,12 @@ sub register { }, ); + Mojo::IOLoop->next_tick( + sub { + + } + ); + $app->hook( around_dispatch => sub { my ($next, $c) = @_; -- cgit v1.2.3-24-g4f1b From 3e390abcc1781fc64bc55e7a36a175d0fd9deeb4 Mon Sep 17 00:00:00 2001 From: Dylan William Hardison Date: Thu, 21 Jun 2018 11:21:55 -0400 Subject: move PerlInitChildHandler to mojolicious --- Bugzilla/Quantum/Plugin/Glue.pm | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'Bugzilla/Quantum/Plugin') diff --git a/Bugzilla/Quantum/Plugin/Glue.pm b/Bugzilla/Quantum/Plugin/Glue.pm index 07ac10c49..ef4a291f8 100644 --- a/Bugzilla/Quantum/Plugin/Glue.pm +++ b/Bugzilla/Quantum/Plugin/Glue.pm @@ -12,6 +12,7 @@ use Try::Tiny; use Bugzilla::Constants; use Bugzilla::Quantum::Template; use Bugzilla::Logging; +use Bugzilla::RNG (); use JSON::MaybeXS qw(decode_json); sub register { @@ -39,7 +40,9 @@ sub register { Mojo::IOLoop->next_tick( sub { - + Bugzilla::RNG::srand(); + srand(); + eval { Bugzilla->dbh->ping }; } ); -- cgit v1.2.3-24-g4f1b From 4431e5bf74ffe47358e713340f8c47747fec0d1d Mon Sep 17 00:00:00 2001 From: Dylan William Hardison Date: Thu, 21 Jun 2018 11:25:21 -0400 Subject: add comment --- Bugzilla/Quantum/Plugin/Glue.pm | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'Bugzilla/Quantum/Plugin') diff --git a/Bugzilla/Quantum/Plugin/Glue.pm b/Bugzilla/Quantum/Plugin/Glue.pm index ef4a291f8..e1c78f07b 100644 --- a/Bugzilla/Quantum/Plugin/Glue.pm +++ b/Bugzilla/Quantum/Plugin/Glue.pm @@ -38,6 +38,12 @@ sub register { }, ); + # Make sure each httpd child receives a different random seed (bug 476622). + # Bugzilla::RNG has one srand that needs to be called for + # every process, and Perl has another. (Various Perl modules still use + # the built-in rand(), even though we never use it in Bugzilla itself, + # so we need to srand() both of them.) + # Also, ping the dbh to force a reconnection. Mojo::IOLoop->next_tick( sub { Bugzilla::RNG::srand(); -- cgit v1.2.3-24-g4f1b From 11037be43cd0d059f2c66ba6f3e6fd9bdc5a455f Mon Sep 17 00:00:00 2001 From: Dylan William Hardison Date: Thu, 21 Jun 2018 14:25:25 -0400 Subject: rewrite Hostage to use mojolicious --- Bugzilla/Quantum/Plugin/Hostage.pm | 80 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 Bugzilla/Quantum/Plugin/Hostage.pm (limited to 'Bugzilla/Quantum/Plugin') diff --git a/Bugzilla/Quantum/Plugin/Hostage.pm b/Bugzilla/Quantum/Plugin/Hostage.pm new file mode 100644 index 000000000..42a05a910 --- /dev/null +++ b/Bugzilla/Quantum/Plugin/Hostage.pm @@ -0,0 +1,80 @@ +package Bugzilla::Quantum::Plugin::Hostage; +use 5.10.1; +use Mojo::Base 'Mojolicious::Plugin'; + +sub _attachment_root { + my ($base) = @_; + return undef unless $base; + return $base =~ m{^https?://(?:bug)?\%bugid\%\.([a-zA-Z\.-]+)} + ? $1 + : undef; +} + +sub _attachment_host_regex { + my ($base) = @_; + return undef unless $base; + my $val = $base; + $val =~ s{^https?://}{}s; + $val =~ s{/$}{}s; + my $regex = quotemeta $val; + $regex =~ s/\\\%bugid\\\%/\\d+/g; + return qr/^$regex$/s; +} + +sub register { + my ( $self, $app, $conf ) = @_; + + $app->hook(before_routes => \&_before_routes); +} + +sub _before_routes { + my ( $c ) = @_; + state $urlbase = Bugzilla->localconfig->{urlbase}; + state $urlbase_uri = URI->new($urlbase); + state $urlbase_host = $urlbase_uri->host; + state $urlbase_host_regex = qr/^bug(\d+)\.\Q$urlbase_host\E$/; + state $attachment_base = Bugzilla->localconfig->{attachment_base}; + state $attachment_root = _attachment_root($attachment_base); + state $attachment_host_regex = _attachment_host_regex($attachment_base); + + my $stash = $c->stash; + my $req = $c->req; + my $url = $req->url->to_abs; + + return if $stash->{'mojo.static'}; + + my $hostname = $url->host; + return if $hostname eq $urlbase_host; + + my $path = $url->path; + return if $path eq '/__lbheartbeat__'; + + if ($attachment_base && $hostname eq $attachment_root) { + $c->redirect_to($urlbase); + return; + } + elsif ($attachment_base && $hostname =~ $attachment_host_regex) { + if ($path =~ m{^/attachment\.cgi}s) { + return; + } else { + my $new_uri = $url->clone; + $new_uri->scheme($urlbase_uri->scheme); + $new_uri->host($urlbase_host); + $c->redirect_to($new_uri); + return; + } + } + elsif (my ($id) = $hostname =~ $urlbase_host_regex) { + my $new_uri = $urlbase_uri->clone; + $new_uri->path('/show_bug.cgi'); + $new_uri->query_form(id => $id); + $c->redirect_to($new_uri); + return; + } + else { + $c->redirect_to($urlbase); + return; + } +} + +1; \ No newline at end of file -- cgit v1.2.3-24-g4f1b From 0245042a5337d07748ba4b48c7c56a6fa91bbfbe Mon Sep 17 00:00:00 2001 From: Dylan William Hardison Date: Thu, 21 Jun 2018 14:41:22 -0400 Subject: optimize the /bmo/ path fix to not impact runtime performance. --- Bugzilla/Quantum/Plugin/Glue.pm | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) (limited to 'Bugzilla/Quantum/Plugin') diff --git a/Bugzilla/Quantum/Plugin/Glue.pm b/Bugzilla/Quantum/Plugin/Glue.pm index e1c78f07b..112514b63 100644 --- a/Bugzilla/Quantum/Plugin/Glue.pm +++ b/Bugzilla/Quantum/Plugin/Glue.pm @@ -6,6 +6,7 @@ # defined by the Mozilla Public License, v. 2.0. package Bugzilla::Quantum::Plugin::Glue; +use 5.10.1; use Mojo::Base 'Mojolicious::Plugin'; use Try::Tiny; @@ -52,18 +53,16 @@ sub register { } ); - $app->hook( - around_dispatch => sub { - my ($next, $c) = @_; - - if ($D{HTTPD_IN_SUBDIR}) { + if ($D{HTTPD_IN_SUBDIR}) { + $app->hook( + before_dispatch => sub { + my ($c) = @_; my $path = $c->req->url->path; $path =~ s{^/bmo}{}s; $c->req->url->path($path); - } - $next->(); - } - ); + } + ); + } Bugzilla::Extension->load_all(); if ($app->mode ne 'development') { -- cgit v1.2.3-24-g4f1b From e7b05770d892573ec47e70a764545ecf950fe343 Mon Sep 17 00:00:00 2001 From: Dylan William Hardison Date: Thu, 21 Jun 2018 15:41:05 -0400 Subject: port BlockIP to mojolicious --- Bugzilla/Quantum/Plugin/BlockIP.pm | 44 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 Bugzilla/Quantum/Plugin/BlockIP.pm (limited to 'Bugzilla/Quantum/Plugin') diff --git a/Bugzilla/Quantum/Plugin/BlockIP.pm b/Bugzilla/Quantum/Plugin/BlockIP.pm new file mode 100644 index 000000000..57f61c71f --- /dev/null +++ b/Bugzilla/Quantum/Plugin/BlockIP.pm @@ -0,0 +1,44 @@ +package Bugzilla::Quantum::Plugin::BlockIP; +use 5.10.1; +use Mojo::Base 'Mojolicious::Plugin'; + +use Bugzilla::Memcached; + +use constant BLOCK_TIMEOUT => 60*60; + +my $MEMCACHED = Bugzilla::Memcached->_new()->{memcached}; + +sub register { + my ( $self, $app, $conf ) = @_; + + $app->hook(before_routes => \&_before_routes) + $app->helper(block_ip => \&_block_ip); + $app->helper(unblock_ip => \&_unblock_ip); +} + +sub _block_ip { + my ($class, $ip) = @_; + $MEMCACHED->set("block_ip:$ip" => 1, BLOCK_TIMEOUT) if $MEMCACHED; +} + +sub _unblock_ip { + my ($class, $ip) = @_; + $MEMCACHED->delete("block_ip:$ip") if $MEMCACHED; +} + +sub _before_routes { + my ( $c ) = @_; + return if $c->stash->{'mojo.static'}; + + my $ip = $c->tx->remote_address; + $c->app->log->debug("remote ip: $ip"); + if ($MEMCACHED && $MEMCACHED->get("block_ip:$ip")) { + $c->block_ip($ip); + $c->res->code(429); + $c->res->message("Too Many Requests"); + $c->res->body("Too Many Requests"); + $c->finish; + } +} + +1; -- cgit v1.2.3-24-g4f1b From a5f6efe3e79df8918292498ab042c3baf419466d Mon Sep 17 00:00:00 2001 From: Dylan William Hardison Date: Thu, 21 Jun 2018 16:00:20 -0400 Subject: fix CI again --- Bugzilla/Quantum/Plugin/BlockIP.pm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'Bugzilla/Quantum/Plugin') diff --git a/Bugzilla/Quantum/Plugin/BlockIP.pm b/Bugzilla/Quantum/Plugin/BlockIP.pm index 57f61c71f..512575b41 100644 --- a/Bugzilla/Quantum/Plugin/BlockIP.pm +++ b/Bugzilla/Quantum/Plugin/BlockIP.pm @@ -11,7 +11,7 @@ my $MEMCACHED = Bugzilla::Memcached->_new()->{memcached}; sub register { my ( $self, $app, $conf ) = @_; - $app->hook(before_routes => \&_before_routes) + $app->hook(before_routes => \&_before_routes); $app->helper(block_ip => \&_block_ip); $app->helper(unblock_ip => \&_unblock_ip); } -- cgit v1.2.3-24-g4f1b From ee7ec91ac4306f255005f814250d97204afc0351 Mon Sep 17 00:00:00 2001 From: Dylan William Hardison Date: Thu, 21 Jun 2018 16:06:36 -0400 Subject: remove debugging --- Bugzilla/Quantum/Plugin/BlockIP.pm | 1 - 1 file changed, 1 deletion(-) (limited to 'Bugzilla/Quantum/Plugin') diff --git a/Bugzilla/Quantum/Plugin/BlockIP.pm b/Bugzilla/Quantum/Plugin/BlockIP.pm index 512575b41..fbfffad66 100644 --- a/Bugzilla/Quantum/Plugin/BlockIP.pm +++ b/Bugzilla/Quantum/Plugin/BlockIP.pm @@ -31,7 +31,6 @@ sub _before_routes { return if $c->stash->{'mojo.static'}; my $ip = $c->tx->remote_address; - $c->app->log->debug("remote ip: $ip"); if ($MEMCACHED && $MEMCACHED->get("block_ip:$ip")) { $c->block_ip($ip); $c->res->code(429); -- cgit v1.2.3-24-g4f1b From 4a29d896ea90e8174ca10a8fd696e4c0767d3350 Mon Sep 17 00:00:00 2001 From: Dylan William Hardison Date: Thu, 21 Jun 2018 17:17:21 -0400 Subject: add request id to logs --- Bugzilla/Quantum/Plugin/Glue.pm | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'Bugzilla/Quantum/Plugin') diff --git a/Bugzilla/Quantum/Plugin/Glue.pm b/Bugzilla/Quantum/Plugin/Glue.pm index 112514b63..301003740 100644 --- a/Bugzilla/Quantum/Plugin/Glue.pm +++ b/Bugzilla/Quantum/Plugin/Glue.pm @@ -53,16 +53,17 @@ sub register { } ); - if ($D{HTTPD_IN_SUBDIR}) { - $app->hook( - before_dispatch => sub { + $app->hook( + before_dispatch => sub { + if ($D{HTTPD_IN_SUBDIR}) { my ($c) = @_; my $path = $c->req->url->path; $path =~ s{^/bmo}{}s; $c->req->url->path($path); } - ); - } + Log::Log4perl::MDC->put(request_id => $c->req->request_id); + } + ); Bugzilla::Extension->load_all(); if ($app->mode ne 'development') { -- cgit v1.2.3-24-g4f1b From 46288f62b71eb569379ccbb7e8ee68bb05220ac2 Mon Sep 17 00:00:00 2001 From: Dylan William Hardison Date: Thu, 21 Jun 2018 17:25:40 -0400 Subject: fix mistake --- Bugzilla/Quantum/Plugin/Glue.pm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'Bugzilla/Quantum/Plugin') diff --git a/Bugzilla/Quantum/Plugin/Glue.pm b/Bugzilla/Quantum/Plugin/Glue.pm index 301003740..54a360003 100644 --- a/Bugzilla/Quantum/Plugin/Glue.pm +++ b/Bugzilla/Quantum/Plugin/Glue.pm @@ -55,8 +55,8 @@ sub register { $app->hook( before_dispatch => sub { + my ($c) = @_; if ($D{HTTPD_IN_SUBDIR}) { - my ($c) = @_; my $path = $c->req->url->path; $path =~ s{^/bmo}{}s; $c->req->url->path($path); -- cgit v1.2.3-24-g4f1b