Skip to content

Commit

Permalink
Item13897: PSGIfication is pretty much complete.
Browse files Browse the repository at this point in the history
Foswiki has been configured and started using only PSGI+nginx interface.
This is the point, where 3.0 countdown could be started.

- Added serverName key on engine's connectionData.

- Added serverName attribute on Foswiki::Request.

- Converting double_word attribute names to doubleWord format.

- Foswiki::Request::JSON web and topic attributes are back to the standard
Foswiki::Request behaviour; i.e., if not defined then initialized from
defaults. So far it doesn't affect neither test nor site functionaity.
  • Loading branch information
vrurg committed Jul 5, 2016
1 parent 8d82ef1 commit d1e0cad
Show file tree
Hide file tree
Showing 18 changed files with 92 additions and 56 deletions.
4 changes: 3 additions & 1 deletion JsonRpcContrib/lib/Foswiki/Contrib/JsonRpcContrib/Error.pm
Expand Up @@ -17,11 +17,13 @@
package Foswiki::Contrib::JsonRpcContrib::Error;
use v5.14;

use Assert;

use Moo;
use namespace::clean;
extends qw(Foswiki::Exception);

has code => ( is => 'rw', );
has code => ( is => 'rw', required => 1, );

around BUILDARGS => sub {
my $orig = shift;
Expand Down
11 changes: 10 additions & 1 deletion JsonRpcContrib/lib/Foswiki/Contrib/JsonRpcContrib/Response.pm
Expand Up @@ -31,7 +31,16 @@ use constant TRACE => 0; # toggle me

has id => ( is => 'rw', );
has message => ( is => 'rw', );
has code => ( is => 'rw', lazy => 1, default => 0, );
has code => (
is => 'rw',
lazy => 1,
default => 0,
isa => sub {
Foswiki::Exception::Fatal->throw(
text => __PACKAGE__ . "::code object attribute cannot be undef" )
unless defined $_[0];
},
);
has json => (
is => 'ro',
lazy => 1,
Expand Down
42 changes: 24 additions & 18 deletions JsonRpcContrib/lib/Foswiki/Request/JSON.pm
Expand Up @@ -176,23 +176,26 @@ sub parseJSON {
if ( ( $jsondata->{jsonrpc} || '' ) ne "2.0" ) {
$this->jsonerror(
new Foswiki::Contrib::JsonRpcContrib::Error(
-32600, "Invalid JSON-RPC request - must be jsonrpc: '2.0'"
code => -32600,
text => "Invalid JSON-RPC request - must be jsonrpc: '2.0'",
)
);
}

# must have a json method
$this->jsonerror(
new Foswiki::Contrib::JsonRpcContrib::Error(
-32600, "Invalid JSON-RPC request - no method"
code => -32600,
text => "Invalid JSON-RPC request - no method"
)
) unless defined $jsondata->{method};

# must not have any other keys other than these
foreach my $key ( keys %{$jsondata} ) {
$this->jsonerror(
new Foswiki::Contrib::JsonRpcContrib::Error(
-32600, "Invalid JSON-RPC request - unknown key $key"
code => -32600,
text => "Invalid JSON-RPC request - unknown key $key"
)
) unless $key =~ /^(jsonrpc|method|params|id)$/;
}
Expand Down Expand Up @@ -222,7 +225,8 @@ sub initFromString {
$error =~ s/,? +at.*$//s;
$this->_jsonerror(
new Foswiki::Contrib::JsonRpcContrib::Error(
-32700, "Parse error - invalid json-rpc request: $error"
code => -32700,
text => "Parse error - invalid json-rpc request: $error"
)
);
};
Expand Down Expand Up @@ -334,12 +338,12 @@ This is read only.
=cut

around _establishWeb => sub {
my $orig = shift;
my $this = shift;

return $this->_pathParsed->{web};
};
#around _establishWeb => sub {
# my $orig = shift;
# my $this = shift;
#
# return $this->_pathParsed->{web};
#};

=begin TML
Expand All @@ -355,12 +359,12 @@ This is read only.
=cut

around _establishTopic => sub {
my $orig = shift;
my $this = shift;

return $this->_pathParsed->{topic};
};
#around _establishTopic => sub {
# my $orig = shift;
# my $this = shift;
#
# return $this->_pathParsed->{topic};
#};

=begin TML
Expand Down Expand Up @@ -394,7 +398,8 @@ around _trigger_method => sub {
if ( defined $value && $this->_has_jsondata && lc($value) ne 'post' ) {
$this->jsonerror(
new Foswiki::Contrib::JsonRpcContrib::Error(
-32600, "Method must be POST, not " . $value
code => -32600,
text => "Method must be POST, not " . $value
)
);
}
Expand Down Expand Up @@ -426,7 +431,8 @@ sub _establishNamespace {
unless ( $decodedInfo =~ /^\/?([^\/\.]+)(?:[\/\.](.*))?$/ ) {
$this->jsonerror(
new Foswiki::Contrib::JsonRpcContrib::Error(
-32600, "Invalid Namespace / method"
code => -32600,
text => "Invalid Namespace / method"
)
);
return;
Expand Down
2 changes: 1 addition & 1 deletion UnitTestContrib/test/unit/PluginHandlerTests.pm
Expand Up @@ -424,7 +424,7 @@ sub initializeUserHandler {
my $url = $_[1] || 'undef';
die "URL $url" unless $url eq (Foswiki::Func::getCgiQuery()->url() || undef);
my $path = $_[2] || 'undef';
die "PATH $path" unless $path eq (Foswiki::Func::getCgiQuery->path_info() || 'undef');
die "PATH $path" unless $path eq (Foswiki::Func::getCgiQuery->pathInfo() || 'undef');
}
HERE
$this->checkCalls( 1, 'earlyInitPlugin' );
Expand Down
8 changes: 4 additions & 4 deletions UnitTestContrib/test/unit/RequestCacheTests.pm
Expand Up @@ -34,7 +34,7 @@ sub test_simpleparams {
);
my $req = $this->create( 'Foswiki::Request', initializer => \%init );
$req->method("BURP");
$req->path_info("/bad/wolf");
$req->pathInfo("/bad/wolf");
$req->action("puke");
my $cache = $this->create('Foswiki::Request::Cache');
my $uid = $cache->save($req);
Expand All @@ -55,7 +55,7 @@ sub test_simpleparams {
@values = $req->multi_param('multi_undef');
$this->assert_str_equals( 0, scalar @values, 'Wrong parameter value' );
$this->assert_str_equals( "BURP", $req->method() );
$this->assert_str_equals( "/bad/wolf", $req->path_info() );
$this->assert_str_equals( "/bad/wolf", $req->pathInfo() );
$this->assert_str_equals( "puke", $req->action() );
}

Expand All @@ -74,7 +74,7 @@ sub test_simpleparams_utf8 {
my $pathinfo = Encode::encode_utf8("/vústění/posvětit");
my $req = $this->create( 'Foswiki::Request', initializer => \%init );
$req->method("BURP");
$req->path_info($pathinfo);
$req->pathInfo($pathinfo);
$req->action("puke");
my $cache = $this->create('Foswiki::Request::Cache');
my $uid = $cache->save($req);
Expand All @@ -95,7 +95,7 @@ sub test_simpleparams_utf8 {
@values = $req->multi_param('multi_undef');
$this->assert_str_equals( 0, scalar @values, 'Wrong parameter value' );
$this->assert_str_equals( "BURP", $req->method() );
$this->assert_str_equals( $pathinfo, $req->path_info() );
$this->assert_str_equals( $pathinfo, $req->pathInfo() );
$this->assert_str_equals( "puke", $req->action() );
}

Expand Down
2 changes: 1 addition & 1 deletion UnitTestContrib/test/unit/ViewParamSectionTests.pm
Expand Up @@ -28,7 +28,7 @@ sub _viewSection {
$this->app->request->web('TestCases');
$this->app->request->topic('IncludeFixtures');
$this->app->request->param( '-name' => 'skin', '-value' => 'text' );
$this->app->request->path_info('TestCases/IncludeFixtures');
$this->app->request->pathInfo('TestCases/IncludeFixtures');

$this->app->request->param( '-name' => 'section', '-value' => $section );
my ($text) = $this->capture( sub { $this->app->handleRequest } );
Expand Down
19 changes: 11 additions & 8 deletions core/lib/Foswiki/Config.pm
Expand Up @@ -128,12 +128,14 @@ sub BUILD {
my $this = shift;
my ($params) = @_;

$this->_workOutOS;
$this->_populatePresets;
$this->_guessDefaults;

$this->data->{isVALID} =
$this->readConfig( $this->noExpand, $this->noSpec, $this->configSpec,
$this->noLocal, );

$this->_populatePresets;
$this->_guessDefaults;
$this->_setupGlobals;
}

Expand Down Expand Up @@ -187,7 +189,7 @@ See also: =Foswiki::Aux::Localize=
=cut

sub setLocalizableAttributes { return qw(data files urlHost); }
sub setLocalizableAttributes { return qw(data files); }

around localize => sub {
my $orig = shift;
Expand Down Expand Up @@ -438,7 +440,7 @@ sub bootstrapSystemSettings {
$this->clear_data;

# Restore system-default state.
$this->_workOutOS();
$this->_workOutOS;
$this->_populatePresets;
$this->_guessDefaults;

Expand Down Expand Up @@ -736,20 +738,22 @@ sub bootstrapWebSettings {

# Work out the URL path for Short and standard URLs
if ( $request_uri =~ m{^(.*?)/$script(\b|$)} ) {
my $spfx = $1;
print STDERR
"AUTOCONFIG: SCRIPT $script fully contained in REQUEST_URI "
. $request_uri
. ", Not short URLs\n"
if (TRAUTO);

# Conventional URLs with path and script
$this->data->{ScriptUrlPath} = $1;
$this->data->{ScriptUrlPath} = $spfx;
$this->data->{ScriptUrlPaths}{view} =
$1 . '/view' . $this->data->{ScriptSuffix};
$spfx . '/view' . $this->data->{ScriptSuffix};

# This might not work, depending on the websrver config,
# but it's the best we can do
$this->data->{PubUrlPath} = "$1/../pub";
$this->data->{PubUrlPath} =
( length($spfx) ? "$spfx/.." : "" ) . "/pub";
}
else {
print STDERR "AUTOCONFIG: Building Short URL paths using prefix $pfx \n"
Expand Down Expand Up @@ -1270,7 +1274,6 @@ sub _populatePresets {
package => 'Foswiki::Contrib::JsonRpcContrib',
request => 'Foswiki::Request::JSON',
method => 'dispatch',
request => 'Foswiki::Request',
context => { jsonrpc => 1 },
};
$cfgData->{SwitchBoard}{login} = {
Expand Down
6 changes: 4 additions & 2 deletions core/lib/Foswiki/Configure/Checkers/ScriptDir.pm
Expand Up @@ -32,15 +32,17 @@ HERE
if ( !$ext
&& $script =~ m/(\..*)$/
&& $script !~ /\.cfg$/
&& $script !~ /\.fcgi$/ )
&& $script !~ /\.(?:fcgi|psgi)$/ )
{
$err .=
" * has a suffix ($1), but no script suffix is configured.\n";
}

# Verify that scripts are executable
# Verify that scripts are executable
# PSGI doesn't require the executable bit.
if ( $^O ne 'MSWin32'
&& $script !~ /\.cfg$/
&& !$Foswiki::app->engine->isa('Foswiki::Engine::PSGI')
&& !-x "$dir/$script" )
{
$err .=
Expand Down
4 changes: 3 additions & 1 deletion core/lib/Foswiki/Configure/Checkers/ScriptSuffix.pm
Expand Up @@ -12,7 +12,9 @@ sub check_current_value {
my ( $this, $reporter ) = @_;

my $currentSuffix = ( fileparse( $0, qr/\.[^.]*/ ) )[2];
$currentSuffix = '' if ( $currentSuffix eq '.fcgi' );
$currentSuffix = ''
if $Foswiki::app->engine->isa('Foswiki::Engine::FastCGO')
|| $Foswiki::app->engine->isa('Foswiki::Engine::PSGI');
my $expectedSuffix = $Foswiki::cfg{ScriptSuffix} || '';

if ( $currentSuffix ne $expectedSuffix ) {
Expand Down
2 changes: 1 addition & 1 deletion core/lib/Foswiki/Configure/Wizards/SendTestEmail.pm
Expand Up @@ -294,7 +294,7 @@ U4kyGRD1h/t/K/8VYAAe30lsvO6PVgAAAABJRU5ErkJggg==
--=_=0i0k0i0w0s0o0fXuOi0E0A--
MAILTEST

my $net = Foswiki::Net->new();
my $net = $Foswiki::app->create('Foswiki::Net');

# Redirect streams so we can capture errors from the sendmail
# program (which does not use Foswiki::Sandbox)
Expand Down
1 change: 1 addition & 0 deletions core/lib/Foswiki/Engine.pm
Expand Up @@ -69,6 +69,7 @@ has pathData => ( is => 'rw', lazy => 1, builder => '_preparePath', );
connectionData attribute is a hash with the following keys:
* =remoteAddress=
* =serverName=
* =serverPort=
* =method=
* =secure=
Expand Down
1 change: 1 addition & 0 deletions core/lib/Foswiki/Engine/CGI.pm
Expand Up @@ -187,6 +187,7 @@ around _prepareConnection => sub {
method => $this->env->{REQUEST_METHOD},
secure => $secure,
serverPort => $this->env->{SERVER_PORT},
serverName => $this->env->{SERVER_NAME},
};
};

Expand Down
2 changes: 2 additions & 0 deletions core/lib/Foswiki/Engine/PSGI.pm
Expand Up @@ -15,6 +15,7 @@ use v5.14;

use Assert;
use Plack::Request;
use Unicode::Normalize;

use Moo;
use namespace::clean;
Expand Down Expand Up @@ -49,6 +50,7 @@ around _prepareConnection => sub {
method => $psgi->method,
secure => $psgi->secure,
serverPort => $this->env->{SERVER_PORT},
serverName => $this->env->{SERVER_NAME},
};
};

Expand Down
4 changes: 2 additions & 2 deletions core/lib/Foswiki/PageCache.pm
Expand Up @@ -128,8 +128,8 @@ sub genVariationKey {
my $app = $this->app;
my $request = $app->request;
my $action = substr( ( $request->action || 'view' ), 0, 4 );
my $serverName = $request->server_name || $Foswiki::cfg{DefaultUrlHost};
my $serverPort = $request->server_port || 80;
my $serverName = $request->serverName || $Foswiki::cfg{DefaultUrlHost};
my $serverPort = $request->serverPort || 80;
$variationKey = '::' . $serverName . '::' . $serverPort . '::' . $action;

# add a flag to distinguish compressed from uncompressed cache entries
Expand Down

0 comments on commit d1e0cad

Please sign in to comment.