[Rt-commit] rt branch, 4.4/migrate-cgi-env, created. rt-4.2.5-216-gaaa16b8

? sunnavy sunnavy at bestpractical.com
Wed Nov 26 11:53:48 EST 2014


The branch, 4.4/migrate-cgi-env has been created
        at  aaa16b8503e2803b99332279aef9ff056a061362 (commit)

- Log -----------------------------------------------------------------
commit ece2945aafb5fb12b9557facbe58b50efbe7e753
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Thu Oct 9 01:50:57 2014 +0800

    abstract usage of cgi env variables

diff --git a/lib/RT/Interface/Web.pm b/lib/RT/Interface/Web.pm
index 3782a62..77b3387 100644
--- a/lib/RT/Interface/Web.pm
+++ b/lib/RT/Interface/Web.pm
@@ -223,12 +223,12 @@ sub EscapeJS {
 
 Different web servers set different environmental varibles. This
 function must return something suitable for REMOTE_USER. By default,
-just downcase $ENV{'REMOTE_USER'}
+just downcase REMOTE_USER env
 
 =cut
 
 sub WebCanonicalizeInfo {
-    return $ENV{'REMOTE_USER'} ? lc $ENV{'REMOTE_USER'} : $ENV{'REMOTE_USER'};
+    return ENV('REMOTE_USER') ? lc ENV('REMOTE_USER') : ENV('REMOTE_USER');
 }
 
 
@@ -525,8 +525,8 @@ sub IntuitNextPage {
 
     # This includes any query parameters.  Redirect will take care of making
     # it an absolute URL.
-    if ($ENV{'REQUEST_URI'}) {
-        $req_uri = $ENV{'REQUEST_URI'};
+    if (ENV('REQUEST_URI')) {
+        $req_uri = ENV('REQUEST_URI');
 
         # collapse multiple leading slashes so the first part doesn't look like
         # a hostname of a schema-less URI
@@ -807,13 +807,14 @@ sub AttemptPasswordAuthentication {
 
     my $m = $HTML::Mason::Commands::m;
 
+    my $remote_addr = ENV('REMOTE_ADDR');
     unless ( $user_obj->id && $user_obj->IsPassword( $ARGS->{pass} ) ) {
-        $RT::Logger->error("FAILED LOGIN for @{[$ARGS->{user}]} from $ENV{'REMOTE_ADDR'}");
+        $RT::Logger->error("FAILED LOGIN for @{[$ARGS->{user}]} from $remote_addr");
         $m->callback( %$ARGS, CallbackName => 'FailedLogin', CallbackPage => '/autohandler' );
         return (0, HTML::Mason::Commands::loc('Your username or password is incorrect'));
     }
     else {
-        $RT::Logger->info("Successful login for @{[$ARGS->{user}]} from $ENV{'REMOTE_ADDR'}");
+        $RT::Logger->info("Successful login for @{[$ARGS->{user}]} from $remote_addr");
 
         # It's important to nab the next page from the session before we blow
         # the session away
@@ -847,7 +848,7 @@ Load or setup a session cookie for the current user.
 
 sub _SessionCookieName {
     my $cookiename = "RT_SID_" . RT->Config->Get('rtname');
-    $cookiename .= "." . $ENV{'SERVER_PORT'} if $ENV{'SERVER_PORT'};
+    $cookiename .= "." . ENV('SERVER_PORT') if ENV('SERVER_PORT');
     return $cookiename;
 }
 
@@ -902,7 +903,7 @@ sub GetWebURLFromRequest {
 
     my $uri = URI->new( RT->Config->Get('WebURL') );
 
-    if ( defined $ENV{HTTPS} and $ENV{'HTTPS'} eq 'on' ) {
+    if ( defined ENV('HTTPS') and ENV('HTTPS') eq 'on' ) {
         $uri->scheme('https');
     }
     else {
@@ -910,8 +911,8 @@ sub GetWebURLFromRequest {
     }
 
     # [rt3.fsck.com #12716] Apache recommends use of $SERVER_HOST
-    $uri->host( $ENV{'SERVER_HOST'} || $ENV{'HTTP_HOST'} || $ENV{'SERVER_NAME'} );
-    $uri->port( $ENV{'SERVER_PORT'} );
+    $uri->host( ENV('SERVER_HOST') || ENV('HTTP_HOST') || ENV('SERVER_NAME') );
+    $uri->port( ENV('SERVER_PORT') );
     return "$uri"; # stringify to be consistent with WebURL in config
 }
 
@@ -1145,7 +1146,7 @@ sub MobileClient {
     my $self = shift;
 
 
-if (($ENV{'HTTP_USER_AGENT'} || '') =~ /(?:hiptop|Blazer|Novarra|Vagabond|SonyEricsson|Symbian|NetFront|UP.Browser|UP.Link|Windows CE|MIDP|J2ME|DoCoMo|J-PHONE|PalmOS|PalmSource|iPhone|iPod|AvantGo|Nokia|Android|WebOS|S60|Mobile)/io && !$HTML::Mason::Commands::session{'NotMobile'})  {
+if ((ENV('HTTP_USER_AGENT') || '') =~ /(?:hiptop|Blazer|Novarra|Vagabond|SonyEricsson|Symbian|NetFront|UP.Browser|UP.Link|Windows CE|MIDP|J2ME|DoCoMo|J-PHONE|PalmOS|PalmSource|iPhone|iPod|AvantGo|Nokia|Android|WebOS|S60|Mobile)/io && !$HTML::Mason::Commands::session{'NotMobile'})  {
     return 1;
 } else {
     return undef;
@@ -1307,12 +1308,12 @@ sub ValidateWebConfig {
     return if $_has_validated_web_config;
     $_has_validated_web_config = 1;
 
-    my $port = $ENV{SERVER_PORT};
-    my $host = $ENV{HTTP_X_FORWARDED_HOST} || $ENV{HTTP_X_FORWARDED_SERVER}
-            || $ENV{HTTP_HOST}             || $ENV{SERVER_NAME};
+    my $port = ENV('SERVER_PORT');
+    my $host = ENV('HTTP_X_FORWARDED_HOST') || ENV('HTTP_X_FORWARDED_SERVER')
+            || ENV('HTTP_HOST')             || ENV('SERVER_NAME');
     ($host, $port) = ($1, $2) if $host =~ /^(.*?):(\d+)$/;
 
-    if ( $port != RT->Config->Get('WebPort') and not $ENV{'rt.explicit_port'}) {
+    if ( $port != RT->Config->Get('WebPort') and not ENV('rt.explicit_port')) {
         $RT::Logger->warn("The requested port ($port) does NOT match the configured WebPort ($RT::WebPort).  "
                          ."Perhaps you should Set(\$WebPort, $port); in RT_SiteConfig.pm, "
                          ."otherwise your internal links may be broken.");
@@ -1327,10 +1328,10 @@ sub ValidateWebConfig {
     # Unfortunately, there is no reliable way to get the _path_ that was
     # requested at the proxy level; simply disable this warning if we're
     # proxied and there's a mismatch.
-    my $proxied = $ENV{HTTP_X_FORWARDED_HOST} || $ENV{HTTP_X_FORWARDED_SERVER};
-    if ($ENV{SCRIPT_NAME} ne RT->Config->Get('WebPath') and not $proxied) {
-        $RT::Logger->warn("The requested path ($ENV{SCRIPT_NAME}) does NOT match the configured WebPath ($RT::WebPath).  "
-                         ."Perhaps you should Set(\$WebPath, '$ENV{SCRIPT_NAME}'); in RT_SiteConfig.pm, "
+    my $proxied = ENV('HTTP_X_FORWARDED_HOST') || ENV('HTTP_X_FORWARDED_SERVER');
+    if (ENV('SCRIPT_NAME') ne RT->Config->Get('WebPath') and not $proxied) {
+        $RT::Logger->warn("The requested path ('" . ENV('SCRIPT_NAME') . "') does NOT match the configured WebPath ($RT::WebPath).  "
+                         ."Perhaps you should Set(\$WebPath, '" .  ENV('SCRIPT_NAME') . "' in RT_SiteConfig.pm, "
                          ."otherwise your internal links may be broken.");
     }
 }
@@ -1517,9 +1518,9 @@ EOT
     # if there is no Referer header then assume the worst
     return (1,
             "your browser did not supply a Referrer header", # loc
-        ) if !$ENV{HTTP_REFERER};
+        ) if !ENV('HTTP_REFERER');
 
-    my ($whitelisted, $browser, $configs) = IsRefererCSRFWhitelisted($ENV{HTTP_REFERER});
+    my ($whitelisted, $browser, $configs) = IsRefererCSRFWhitelisted(ENV('HTTP_REFERER'));
     return 0 if $whitelisted;
 
     if ( @$configs > 1 ) {
@@ -1804,6 +1805,11 @@ sub GetCustomFieldInputNamePrefix {
     return $prefix;
 }
 
+sub ENV {
+    my $name = shift;
+    return $name ? $ENV{$name} : \%ENV;
+}
+
 package HTML::Mason::Commands;
 
 use vars qw/$r $m %session/;
@@ -2037,7 +2043,7 @@ sub MaybeRedirectToApproval {
         @_
     );
 
-    return unless $ENV{REQUEST_METHOD} eq 'GET';
+    return unless ENV('REQUEST_METHOD') eq 'GET';
 
     my $id = $args{ARGSRef}->{id};
 

commit 22990c9ba342644bda6f96f27eccc40025c8e045
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Thu Oct 16 00:04:46 2014 +0800

    use psgi env directly
    
    sadly we still need to keep several env vars:
    
    * PATH
    
      with mod_fastcgi(at least), we need this to find commands like "dot"
    
    * REQUEST_METHOD and HTTP_ACCEPT_LANGUAGE
    
      they are used in I18N::LangTags::Detect(which is also called by Locale::Maketext)
    
    * QUERY_STRING
    
      HTML::Mason::Utils::cgi_request_args uses it to determine if to call
      url_param or not for a weird case(see inline comments there)
    
    Fixes: #30427

diff --git a/lib/RT/Interface/Web.pm b/lib/RT/Interface/Web.pm
index 77b3387..535b34a 100644
--- a/lib/RT/Interface/Web.pm
+++ b/lib/RT/Interface/Web.pm
@@ -854,7 +854,7 @@ sub _SessionCookieName {
 
 sub LoadSessionFromCookie {
 
-    my %cookies       = CGI::Cookie->fetch;
+    my %cookies       = CGI::Cookie->parse(ENV('HTTP_COOKIE'));
     my $cookiename    = _SessionCookieName();
     my $SessionCookie = ( $cookies{$cookiename} ? $cookies{$cookiename}->value : undef );
     tie %HTML::Mason::Commands::session, 'RT::Interface::Web::Session', $SessionCookie;
@@ -903,12 +903,7 @@ sub GetWebURLFromRequest {
 
     my $uri = URI->new( RT->Config->Get('WebURL') );
 
-    if ( defined ENV('HTTPS') and ENV('HTTPS') eq 'on' ) {
-        $uri->scheme('https');
-    }
-    else {
-        $uri->scheme('http');
-    }
+    $uri->scheme(ENV('psgi.url_scheme') || 'http');
 
     # [rt3.fsck.com #12716] Apache recommends use of $SERVER_HOST
     $uri->host( ENV('SERVER_HOST') || ENV('HTTP_HOST') || ENV('SERVER_NAME') );
@@ -1807,7 +1802,8 @@ sub GetCustomFieldInputNamePrefix {
 
 sub ENV {
     my $name = shift;
-    return $name ? $ENV{$name} : \%ENV;
+    my $env = $HTML::Mason::Commands::m->cgi_object->env;
+    return $name ? $env->{$name} : $env;
 }
 
 package HTML::Mason::Commands;
diff --git a/lib/RT/Interface/Web/Handler.pm b/lib/RT/Interface/Web/Handler.pm
index cf94610..02d9357 100644
--- a/lib/RT/Interface/Web/Handler.pm
+++ b/lib/RT/Interface/Web/Handler.pm
@@ -269,6 +269,10 @@ sub PSGIApp {
     my $mason = sub {
         my $env = shift;
 
+        # mod_fastcgi starts with an empty %ENV, but provides it on each
+        # request.  Pick it up and cache it during the first request.
+        $ENV{PATH} //= $env->{PATH};
+
         {
             my $res = $self->CheckModPerlHandler($env);
             return $self->_psgi_response_cb( $res->finalize ) if $res;
@@ -297,9 +301,7 @@ sub PSGIApp {
 
         my $ret;
         {
-            # XXX: until we get rid of all $ENV stuff.
-            local %ENV = (%ENV, CGI::Emulate::PSGI->emulate_environment($env));
-
+            local %ENV = (%ENV, map { $_ => $env->{$_} } grep { exists $env->{$_} } qw/HTTP_ACCEPT_LANGUAGE REQUEST_METHOD QUERY_STRING/);
             $ret = $h->handle_psgi($env);
         }
 
@@ -309,7 +311,7 @@ sub PSGIApp {
             my $orig_ret = $ret;
             $ret = sub {
                 my $respond = shift;
-                local %ENV = (%ENV, CGI::Emulate::PSGI->emulate_environment($env));
+                local %ENV = (%ENV, map { $_ => $env->{$_} } grep { exists $env->{$_} } qw/HTTP_ACCEPT_LANGUAGE REQUEST_METHOD QUERY_STRING/);
                 $orig_ret->($respond);
             };
         }

commit aaa16b8503e2803b99332279aef9ff056a061362
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Thu Oct 16 00:05:41 2014 +0800

    update the mock of $m to reflect the cgi env migration

diff --git a/t/web/redirect.t b/t/web/redirect.t
index d909d8d..4edf785 100644
--- a/t/web/redirect.t
+++ b/t/web/redirect.t
@@ -2,9 +2,9 @@ use strict;
 use warnings;
 
 use RT::Test tests => 13;
-
+use CGI::PSGI;
 my $r = $HTML::Mason::Commands::r = bless {}, 'R';
-my $m = $HTML::Mason::Commands::m = bless {}, 'M';
+my $m = $HTML::Mason::Commands::m = bless { cgi_object => CGI::PSGI->new( {} ) }, 'M';
 
 set_config(
     CanonicalizeRedirectURLs => 0,
@@ -39,11 +39,11 @@ is( RT->Config->Get('WebURL'), 'https://localhost/' );
 
 redirect_ok(
     'https://localhost/Ticket/', 'https://localhost/Ticket/',
-    { SERVER_NAME => 'localhost', SERVER_PORT => 443, HTTPS => 'on' },
+    { SERVER_NAME => 'localhost', SERVER_PORT => 443, 'psgi.url_scheme' => 'https' },
 );
 redirect_ok(
     '/Ticket/', 'https://localhost/Ticket/',
-    { SERVER_NAME => 'localhost', SERVER_PORT => 443, HTTPS => 'on' },
+    { SERVER_NAME => 'localhost', SERVER_PORT => 443, 'psgi.url_scheme' => 'https' },
 );
 redirect_ok(
     'https://localhost/Ticket/', 'http://localhost/Ticket/',
@@ -59,7 +59,7 @@ redirect_ok(
 );
 redirect_ok(
     'https://localhost/Ticket/', 'https://example.com/Ticket/',
-    { SERVER_NAME => 'example.com', SERVER_PORT => 443, HTTPS => 'on' },
+    { SERVER_NAME => 'example.com', SERVER_PORT => 443, 'psgi.url_scheme' => 'https' },
 );
 
 sub set_config {
@@ -87,10 +87,7 @@ sub set_config {
 sub redirect_ok {
     my ($to, $expected, $env, $details) = @_;
 
-    local %ENV = %ENV;
-    while ( my ($k, $v) = each %{ $env || {} } ) {
-        $ENV{ $k } = $v;
-    }
+    %{$m->cgi_object->env} = %$env;
     RT::Interface::Web::Redirect( $to );
     is($m->redirect, $expected, $details || "correct for '$to'");
 }
@@ -101,4 +98,5 @@ sub status {};
 package M;
 sub redirect { $_[0]{'last'} = $_[1] if @_ > 1; return $_[0]{'last'} }
 sub abort {}
+sub cgi_object { $_[0]{'cgi_object'} }
 

-----------------------------------------------------------------------


More information about the rt-commit mailing list