[Rt-commit] rt branch, 4.0/migrate-cgi-env, created. rt-4.0.22-20-g96fee9d

? sunnavy sunnavy at bestpractical.com
Wed Oct 15 13:14:01 EDT 2014


The branch, 4.0/migrate-cgi-env has been created
        at  96fee9df1c053c544b0f4bf7db04e7fee4f4483c (commit)

- Log -----------------------------------------------------------------
commit a21a65ae959a0278911400ae8eec5fe5fc20a44a
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 3d8cd99..61c14d4 100644
--- a/lib/RT/Interface/Web.pm
+++ b/lib/RT/Interface/Web.pm
@@ -185,7 +185,7 @@ just downcase $ENV{'REMOTE_USER'}
 =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');
 }
 
 
@@ -477,8 +477,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
@@ -748,13 +748,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
@@ -788,7 +789,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;
 }
 
@@ -862,15 +863,15 @@ sub Redirect {
         && $uri->host eq $server_uri->host
         && $uri->port eq $server_uri->port )
     {
-        if ( defined $ENV{HTTPS} and $ENV{'HTTPS'} eq 'on' ) {
+        if ( defined ENV('HTTPS') and ENV('HTTPS') eq 'on' ) {
             $uri->scheme('https');
         } else {
             $uri->scheme('http');
         }
 
         # [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') );
     }
 
     # not sure why, but on some systems without this call mason doesn't
@@ -1060,7 +1061,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;
@@ -1215,12 +1216,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.");
@@ -1235,10 +1236,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.");
     }
 }
@@ -1414,9 +1415,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 ) {
@@ -1539,6 +1540,11 @@ sub PotentialPageAction {
     return "";
 }
 
+sub ENV {
+    my $name = shift;
+    return $name ? $ENV{$name} : \%ENV;
+}
+
 package HTML::Mason::Commands;
 
 use vars qw/$r $m %session/;
@@ -1681,7 +1687,7 @@ sub MaybeRedirectToApproval {
         @_
     );
 
-    return unless $ENV{REQUEST_METHOD} eq 'GET';
+    return unless ENV('REQUEST_METHOD') eq 'GET';
 
     my $id = $args{ARGSRef}->{id};
 

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

    pickuse psgi env directly
    
    sadly that we need to keep REQUEST_METHOD and HTTP_ACCEPT_LANGUAGE to make
    i18n(actually I18N::LangTags::Detect) happy.
    
    Fixes: #30427

diff --git a/lib/RT/Interface/Web.pm b/lib/RT/Interface/Web.pm
index 61c14d4..efe18f0 100644
--- a/lib/RT/Interface/Web.pm
+++ b/lib/RT/Interface/Web.pm
@@ -795,7 +795,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;
@@ -863,7 +863,7 @@ sub Redirect {
         && $uri->host eq $server_uri->host
         && $uri->port eq $server_uri->port )
     {
-        if ( defined ENV('HTTPS') and ENV('HTTPS') eq 'on' ) {
+        if ( ENV('psgi.url_scheme') && ENV('psgi.url_scheme') eq 'https' ) {
             $uri->scheme('https');
         } else {
             $uri->scheme('http');
@@ -1542,7 +1542,8 @@ sub PotentialPageAction {
 
 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 7cf18d1..d7d9408 100644
--- a/lib/RT/Interface/Web/Handler.pm
+++ b/lib/RT/Interface/Web/Handler.pm
@@ -286,9 +286,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->{$_} } qw/HTTP_ACCEPT_LANGUAGE REQUEST_METHOD/);
             $ret = $h->handle_psgi($env);
         }
 
@@ -298,7 +296,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->{$_} } qw/HTTP_ACCEPT_LANGUAGE REQUEST_METHOD/);
                 $orig_ret->($respond);
             };
         }

commit 96fee9df1c053c544b0f4bf7db04e7fee4f4483c
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