From 6df945da5900da86203e0527816690cb1d52c574 Mon Sep 17 00:00:00 2001 From: Dylan William Hardison Date: Tue, 3 Apr 2018 23:05:04 -0400 Subject: Bug 1455495 - Replace apache with Mojolicious --- Bugzilla/Quantum/CGI.pm | 162 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 162 insertions(+) create mode 100644 Bugzilla/Quantum/CGI.pm (limited to 'Bugzilla/Quantum/CGI.pm') diff --git a/Bugzilla/Quantum/CGI.pm b/Bugzilla/Quantum/CGI.pm new file mode 100644 index 000000000..85f14cf83 --- /dev/null +++ b/Bugzilla/Quantum/CGI.pm @@ -0,0 +1,162 @@ +# 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::CGI; +use Mojo::Base 'Mojolicious::Controller'; + +use CGI::Compile; +use Bugzilla::Constants qw(bz_locations); +use Bugzilla::Quantum::Stdout; +use File::Slurper qw(read_text); +use File::Spec::Functions qw(catfile); +use Sub::Name; +use Sub::Quote 2.005000; +use Try::Tiny; +use Taint::Util qw(untaint); +use Socket qw(AF_INET inet_aton); +use Sys::Hostname; +use English qw(-no_match_vars); + +our $C; +my %SEEN; + +sub load_all { + my ($class, $r) = @_; + + foreach my $file (glob '*.cgi') { + my $name = _file_to_method($file); + $class->load_one($name, $file); + $r->any("/$file")->to("CGI#$name"); + } +} + +sub load_one { + my ($class, $name, $file) = @_; + my $package = __PACKAGE__ . "::$name", + my $inner_name = "_$name"; + my $content = read_text( catfile( bz_locations->{cgi_path}, $file ) ); + $content = "package $package; $content"; + untaint($content); + my %options = ( + package => $package, + file => $file, + line => 1, + no_defer => 1, + ); + die "Tried to load $file more than once" if $SEEN{$file}++; + my $inner = quote_sub $inner_name, $content, {}, \%options; + my $wrapper = sub { + my ($c) = @_; + my $stdin = $c->_STDIN; + my $stdout = ''; + local $C = $c; + local %ENV = $c->_ENV($file); + local *STDIN; ## no critic (local) + local $CGI::Compile::USE_REAL_EXIT = 0; + local $PROGRAM_NAME = $file; + open STDIN, '<', $stdin->path or die "STDIN @{[$stdin->path]}: $!" if -s $stdin->path; + tie *STDOUT, 'Bugzilla::Quantum::Stdout', controller => $c; ## no critic (tie) + try { + Bugzilla->init_page(); + $inner->(); + } + catch { + die $_ unless ref $_ eq 'ARRAY' && $_->[0] eq "EXIT\n"; + } + finally { + untie *STDOUT; + $c->finish; + Bugzilla->_cleanup; ## no critic (private) + CGI::initialize_globals(); + }; + }; + + no strict 'refs'; ## no critic (strict) + *{$name} = subname($name, $wrapper); + return 1; +} + +sub _ENV { + my ($c, $script_name) = @_; + 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 : ''; + } + my $path_info = $c->param('PATH_INFO'); + my %captures = %{ $c->stash->{'mojo.captures'} // {} }; + foreach my $key (keys %captures) { + if ($key eq 'controller' || $key eq 'action' || $key eq 'PATH_INFO' || $key =~ /^REWRITE_/) { + delete $captures{$key}; + } + } + my $cgi_query = Mojo::Parameters->new(%captures); + $cgi_query->append($req->url->query); + my $prefix = $c->stash->{bmo_prefix} ? '/bmo/' : '/'; + + return ( + %ENV, + 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 => $cgi_query->to_string, + PATH_INFO => $path_info ? "/$path_info" : '', + REMOTE_ADDR => $tx->original_remote_address, + REMOTE_HOST => $tx->original_remote_address, + REMOTE_PORT => $tx->remote_port, + REMOTE_USER => $remote_user || '', + REQUEST_METHOD => $req->method, + SCRIPT_NAME => "$prefix$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 ); +} + +sub _file_to_method { + my ($name) = @_; + $name =~ s/\./_/s; + $name =~ s/\W+/_/gs; + return $name; +} + + +1; -- cgit v1.2.3-24-g4f1b From 5cf8f186b1f2dbda01cd63a0feb12ea0bdfa02af Mon Sep 17 00:00:00 2001 From: Dylan William Hardison Date: Thu, 19 Jul 2018 11:22:00 -0400 Subject: fix lint errors --- Bugzilla/Quantum/CGI.pm | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'Bugzilla/Quantum/CGI.pm') diff --git a/Bugzilla/Quantum/CGI.pm b/Bugzilla/Quantum/CGI.pm index 85f14cf83..0e09d795e 100644 --- a/Bugzilla/Quantum/CGI.pm +++ b/Bugzilla/Quantum/CGI.pm @@ -90,12 +90,12 @@ sub _ENV { for my $name ( @{ $headers->names } ) { my $key = uc "http_$name"; - $key =~ s!\W!_!g; + $key =~ s/\W/_/g; $env_headers{$key} = $headers->header($name); } my $remote_user; - if ( my $userinfo = $c->req->url->to_abs->userinfo ) { + if ( my $userinfo = $req->url->to_abs->userinfo ) { $remote_user = $userinfo =~ /([^:]+)/ ? $1 : ''; } elsif ( my $authenticate = $headers->authorization ) { -- cgit v1.2.3-24-g4f1b From f7c2f14988dc1638cb68f4a96cae4bc387688809 Mon Sep 17 00:00:00 2001 From: Dylan William Hardison Date: Thu, 19 Jul 2018 12:18:47 -0400 Subject: fix errors from group review --- Bugzilla/Quantum/CGI.pm | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'Bugzilla/Quantum/CGI.pm') diff --git a/Bugzilla/Quantum/CGI.pm b/Bugzilla/Quantum/CGI.pm index 0e09d795e..ba09dded6 100644 --- a/Bugzilla/Quantum/CGI.pm +++ b/Bugzilla/Quantum/CGI.pm @@ -52,7 +52,6 @@ sub load_one { my $wrapper = sub { my ($c) = @_; my $stdin = $c->_STDIN; - my $stdout = ''; local $C = $c; local %ENV = $c->_ENV($file); local *STDIN; ## no critic (local) @@ -102,7 +101,7 @@ sub _ENV { $remote_user = $authenticate =~ /Basic\s+(.*)/ ? b64_decode $1 : ''; $remote_user = $remote_user =~ /([^:]+)/ ? $1 : ''; } - my $path_info = $c->param('PATH_INFO'); + my $path_info = $c->stash->{'mojo.captures'}{'PATH_INFO'}; my %captures = %{ $c->stash->{'mojo.captures'} // {} }; foreach my $key (keys %captures) { if ($key eq 'controller' || $key eq 'action' || $key eq 'PATH_INFO' || $key =~ /^REWRITE_/) { -- cgit v1.2.3-24-g4f1b From 29c7ca6bb526ce2cc9eb99815b6fc1252b7870cf Mon Sep 17 00:00:00 2001 From: Dylan William Hardison Date: Mon, 23 Jul 2018 11:16:47 -0400 Subject: some cleanups --- Bugzilla/Quantum/CGI.pm | 71 ++++++++++++++++++++++++------------------------- 1 file changed, 35 insertions(+), 36 deletions(-) (limited to 'Bugzilla/Quantum/CGI.pm') diff --git a/Bugzilla/Quantum/CGI.pm b/Bugzilla/Quantum/CGI.pm index ba09dded6..ab092140c 100644 --- a/Bugzilla/Quantum/CGI.pm +++ b/Bugzilla/Quantum/CGI.pm @@ -25,40 +25,39 @@ our $C; my %SEEN; sub load_all { - my ($class, $r) = @_; + my ( $class, $r ) = @_; - foreach my $file (glob '*.cgi') { - my $name = _file_to_method($file); - $class->load_one($name, $file); + foreach my $file ( glob '*.cgi' ) { + my $name = _file_to_method($file); + $class->load_one( $name, $file ); $r->any("/$file")->to("CGI#$name"); } } sub load_one { - my ($class, $name, $file) = @_; - my $package = __PACKAGE__ . "::$name", - my $inner_name = "_$name"; - my $content = read_text( catfile( bz_locations->{cgi_path}, $file ) ); + my ( $class, $name, $file ) = @_; + my $package = __PACKAGE__ . "::$name", my $inner_name = "_$name"; + my $content = read_text( catfile( bz_locations->{cgi_path}, $file ) ); $content = "package $package; $content"; untaint($content); my %options = ( - package => $package, - file => $file, - line => 1, + package => $package, + file => $file, + line => 1, no_defer => 1, ); die "Tried to load $file more than once" if $SEEN{$file}++; my $inner = quote_sub $inner_name, $content, {}, \%options; my $wrapper = sub { my ($c) = @_; - my $stdin = $c->_STDIN; - local $C = $c; - local %ENV = $c->_ENV($file); - local *STDIN; ## no critic (local) + my $stdin = $c->_STDIN; + local $C = $c; + local %ENV = $c->_ENV($file); local $CGI::Compile::USE_REAL_EXIT = 0; - local $PROGRAM_NAME = $file; + local $PROGRAM_NAME = $file; + local *STDIN; ## no critic (local) open STDIN, '<', $stdin->path or die "STDIN @{[$stdin->path]}: $!" if -s $stdin->path; - tie *STDOUT, 'Bugzilla::Quantum::Stdout', controller => $c; ## no critic (tie) + tie *STDOUT, 'Bugzilla::Quantum::Stdout', controller => $c; ## no critic (tie) try { Bugzilla->init_page(); $inner->(); @@ -69,23 +68,24 @@ sub load_one { finally { untie *STDOUT; $c->finish; - Bugzilla->_cleanup; ## no critic (private) + Bugzilla->cleanup; CGI::initialize_globals(); }; }; - no strict 'refs'; ## no critic (strict) - *{$name} = subname($name, $wrapper); + no strict 'refs'; ## no critic (strict) + *{$name} = subname( $name, $wrapper ); return 1; } + sub _ENV { - my ($c, $script_name) = @_; - my $tx = $c->tx; - my $req = $tx->req; - my $headers = $req->headers; + my ( $c, $script_name ) = @_; + 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 => '' ); + my %env_headers = ( HTTP_COOKIE => '', HTTP_REFERER => '' ); for my $name ( @{ $headers->names } ) { my $key = uc "http_$name"; @@ -103,13 +103,13 @@ sub _ENV { } my $path_info = $c->stash->{'mojo.captures'}{'PATH_INFO'}; my %captures = %{ $c->stash->{'mojo.captures'} // {} }; - foreach my $key (keys %captures) { - if ($key eq 'controller' || $key eq 'action' || $key eq 'PATH_INFO' || $key =~ /^REWRITE_/) { + foreach my $key ( keys %captures ) { + if ( $key eq 'controller' || $key eq 'action' || $key eq 'PATH_INFO' || $key =~ /^REWRITE_/ ) { delete $captures{$key}; } } my $cgi_query = Mojo::Parameters->new(%captures); - $cgi_query->append($req->url->query); + $cgi_query->append( $req->url->query ); my $prefix = $c->stash->{bmo_prefix} ? '/bmo/' : '/'; return ( @@ -119,17 +119,17 @@ sub _ENV { GATEWAY_INTERFACE => 'CGI/1.1', HTTPS => $req->is_secure ? 'YES' : 'NO', %env_headers, - QUERY_STRING => $cgi_query->to_string, - PATH_INFO => $path_info ? "/$path_info" : '', - REMOTE_ADDR => $tx->original_remote_address, - REMOTE_HOST => $tx->original_remote_address, - REMOTE_PORT => $tx->remote_port, - REMOTE_USER => $remote_user || '', + QUERY_STRING => $cgi_query->to_string, + PATH_INFO => $path_info ? "/$path_info" : '', + REMOTE_ADDR => $tx->original_remote_address, + REMOTE_HOST => $tx->original_remote_address, + REMOTE_PORT => $tx->remote_port, + REMOTE_USER => $remote_user || '', REQUEST_METHOD => $req->method, SCRIPT_NAME => "$prefix$script_name", SERVER_NAME => hostname, SERVER_PORT => $tx->local_port, - SERVER_PROTOCOL => $req->is_secure ? 'HTTPS' : 'HTTP', # TODO: Version is missing + SERVER_PROTOCOL => $req->is_secure ? 'HTTPS' : 'HTTP', # TODO: Version is missing SERVER_SOFTWARE => __PACKAGE__, ); } @@ -157,5 +157,4 @@ sub _file_to_method { return $name; } - 1; -- cgit v1.2.3-24-g4f1b From a108778bc2784cdadfc6b2df4ec989b46dc162ca Mon Sep 17 00:00:00 2001 From: Dylan William Hardison Date: Mon, 23 Jul 2018 11:41:14 -0400 Subject: more tidy --- Bugzilla/Quantum/CGI.pm | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'Bugzilla/Quantum/CGI.pm') diff --git a/Bugzilla/Quantum/CGI.pm b/Bugzilla/Quantum/CGI.pm index ab092140c..7585d888a 100644 --- a/Bugzilla/Quantum/CGI.pm +++ b/Bugzilla/Quantum/CGI.pm @@ -9,17 +9,17 @@ package Bugzilla::Quantum::CGI; use Mojo::Base 'Mojolicious::Controller'; use CGI::Compile; -use Bugzilla::Constants qw(bz_locations); -use Bugzilla::Quantum::Stdout; -use File::Slurper qw(read_text); -use File::Spec::Functions qw(catfile); -use Sub::Name; -use Sub::Quote 2.005000; use Try::Tiny; use Taint::Util qw(untaint); -use Socket qw(AF_INET inet_aton); use Sys::Hostname; +use Sub::Quote 2.005000; +use Sub::Name; +use Socket qw(AF_INET inet_aton); +use File::Spec::Functions qw(catfile); +use File::Slurper qw(read_text); use English qw(-no_match_vars); +use Bugzilla::Quantum::Stdout; +use Bugzilla::Constants qw(bz_locations); our $C; my %SEEN; -- cgit v1.2.3-24-g4f1b From 01309f9f36d1767c8d6984364c47539fed3946e6 Mon Sep 17 00:00:00 2001 From: Dylan William Hardison Date: Tue, 31 Jul 2018 23:49:38 -0400 Subject: add more debugging but also probably fix redirect problem --- Bugzilla/Quantum/CGI.pm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'Bugzilla/Quantum/CGI.pm') diff --git a/Bugzilla/Quantum/CGI.pm b/Bugzilla/Quantum/CGI.pm index 7585d888a..0a74f1ee5 100644 --- a/Bugzilla/Quantum/CGI.pm +++ b/Bugzilla/Quantum/CGI.pm @@ -117,7 +117,7 @@ sub _ENV { CONTENT_LENGTH => $content_length || 0, CONTENT_TYPE => $headers->content_type || '', GATEWAY_INTERFACE => 'CGI/1.1', - HTTPS => $req->is_secure ? 'YES' : 'NO', + HTTPS => $req->is_secure ? 'on' : 'off', %env_headers, QUERY_STRING => $cgi_query->to_string, PATH_INFO => $path_info ? "/$path_info" : '', -- cgit v1.2.3-24-g4f1b