Skip to content

Commit

Permalink
switch to plack!
Browse files Browse the repository at this point in the history
  • Loading branch information
tokuhirom committed Jan 11, 2010
1 parent edb4a67 commit 7e4a680
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 165 deletions.
3 changes: 2 additions & 1 deletion Makefile.PL
Expand Up @@ -17,7 +17,6 @@ requires 'Encode::JP::Mobile' => 0.25;
requires 'HTML::ReplacePictogramMobileJp' => 0.06;
requires 'HTML::Tree' => 3.23;
requires 'HTML::TreeBuilder::XPath' => 0.09;
requires 'HTTP::Engine' => '0.03001';
requires 'Params::Validate' => 0.91;
requires 'Template' => 2.19;
requires 'UNIVERSAL::require' => '0.11';
Expand All @@ -27,8 +26,10 @@ requires 'HTTP::MobileAttribute' => 0.13;
requires 'Path::Class';
requires 'HTTP::Session' => 0.29;
requires 'HTTP::Cookies'; # part of LWP
requires 'Test::More' => 0.88;

test_requires('Test::More');
test_requires('Test::Requires');

features(
'Better Encoding detection' => [
Expand Down
53 changes: 37 additions & 16 deletions lib/Moxy.pm
Expand Up @@ -29,6 +29,9 @@ use URI::Heuristic qw(uf_uristr);
use URI;
use YAML;
use Time::HiRes ();
use Plack::Response;
use Moxy::Request;
use HTTP::Message::PSGI;
use HTTP::MobileAttribute plugins => [
qw/CarrierLetter IS/,
{
Expand Down Expand Up @@ -70,6 +73,18 @@ sub assets_path {
};
}

sub res {
Plack::Response->new(@_);
}
sub HTTP::Response::to_plack_response {
my $self = shift;
return res(
$self->code,
$self->headers,
$self->content,
);
}

# -------------------------------------------------------------------------

sub run_hook_and_get_response {
Expand Down Expand Up @@ -154,6 +169,17 @@ sub rewrite_html {
return $result;
}

sub to_app {
my ($self) = @_;
sub {
my $env = shift;
my $req = Moxy::Request->new($env);
my $res = $self->handle_request($req);
$res->content_length( length($res->content) ); # adjust content-length.
$res->finalize();
};
}

sub handle_request {
my ($self, $req) = @_;

Expand Down Expand Up @@ -184,12 +210,12 @@ sub handle_request {
my $auth = join(',', $req->headers->authorization_basic);
if ($state->isa('Moxy::Session::State::BasicAuth') && !$auth) {
$self->log(debug => 'basicauth');
return HTTP::Engine::Response->new(
status => 401,
headers => {
return res(
401,
[
WWW_Authenticate => qq{Basic realm="Moxy needs basic auth.Only for identification.Password is dummy."},
},
body => 'authentication required',
],
'authentication required',
);
} else {
$self->log(debug => "session: state: $state, store: $store");
Expand All @@ -214,7 +240,7 @@ sub _make_response {
my $self = shift;
my %args = validate(
@_ => +{
req => { isa => 'HTTP::Engine::Request', },
req => { isa => 'Moxy::Request', },
session => { type => OBJECT },
}
);
Expand Down Expand Up @@ -260,11 +286,10 @@ sub _make_response {
}
my $redirect = $base . '/' . uri_escape($location);
$self->log(debug => "redirect to $redirect");
return HTTP::Engine::Response->new(
status => 302,
headers => {
return res(
302, [
Location => $redirect,
},
],
);
} else {
my $content_type = $res->header('Content-Type');
Expand All @@ -275,9 +300,7 @@ sub _make_response {
$res->content( encode($res->charset, rewrite_css($base, decode($res->charset, $res->content), $url), Encode::FB_HTMLCREF) );
}

my $response = HTTP::Engine::Response->new();
$response->set_http_response($res);
return $response;
return $res->to_plack_response();
}
} else {
# please input url.
Expand Down Expand Up @@ -305,9 +328,7 @@ sub _make_response {
mobile_attribute => HTTP::MobileAttribute->new('KDDI-KC26 UP.Browser/6.2.0.7.3.129 (GUI) MMP/2.0'),
session => $args{session},
);
my $res = HTTP::Engine::Response->new;
$res->set_http_response($response);
$res;
return $response->to_plack_response();
}
}

Expand Down
16 changes: 16 additions & 0 deletions lib/Moxy/Request.pm
@@ -0,0 +1,16 @@
package Moxy::Request;
use strict;
use warnings;
use base qw/Plack::Request/;

sub as_http_request {
my $self = shift;
return HTTP::Request->new(
$self->method,
$self->uri,
$self->headers,
$self->raw_body,
);
}

1;
126 changes: 0 additions & 126 deletions moxy.pl

This file was deleted.

12 changes: 2 additions & 10 deletions moxy.psgi
Expand Up @@ -6,7 +6,6 @@ use lib File::Spec->catfile( dirname(__FILE__), 'lib' );
use File::Temp;
use Plack::Builder;

use HTTP::Engine;
use Moxy;

# preload
Expand All @@ -31,17 +30,10 @@ my $config = +{
},
};
my $moxy = Moxy->new($config);
my $engine = HTTP::Engine->new(
interface => {
module => 'PSGI',
request_handler => sub {
$moxy->handle_request( @_ );
},
}
);
builder {
enable_if { $_[0]->{REMOTE_ADDR} eq '127.0.0.1' }
"Plack::Middleware::ReverseProxy";
sub { $engine->run(@_) };

$moxy->to_app();
};

16 changes: 4 additions & 12 deletions t/Plugins/UserAgentSwitcher.t
Expand Up @@ -7,14 +7,14 @@ use FindBin;
use File::Spec::Functions;
use HTTP::Response;
use HTTP::Request;
use HTTP::Engine;
use HTTP::Message::PSGI;

my $moxy = Moxy->new(
{
global => {
assets_path => catfile( $FindBin::Bin, '..', '..', 'assets' ),
'log' => {
level => 'debug',
level => 'info',
},
session => {
store => {
Expand All @@ -35,16 +35,8 @@ sub test {
GET => $input
);
$req->authorization_basic('foobar');
my $res = HTTP::Engine->new(
interface => {
module => 'Test',
args => {},
request_handler => sub {
my $req = shift;
$moxy->handle_request($req);
},
}
)->run($req);
my $app = $moxy->to_app();
my $res = res_from_psgi($app->($req->to_psgi));
is $res->header('Location'), $expected;
}

Expand Down
43 changes: 43 additions & 0 deletions t/mech.t
@@ -0,0 +1,43 @@
use strict;
use warnings;
use utf8;
use Test::Requires 'Test::WWW::Mechanize::PSGI';
use Test::More;
use Moxy;
use FindBin;
use File::Spec::Functions qw/catfile/;

binmode Test::More->builder->$_, ":utf8" for qw/output failure_output todo_output/;

my $moxy = Moxy->new(
{
global => {
assets_path => catfile( $FindBin::Bin, '..', 'assets' ),
'log' => {
level => 'info',
},
session => {
store => {
module => 'Test',
config => {},
},
}
},
plugins => [
{ module => 'UserAgentSwitcher' },
],
}
);
my $app = $moxy->to_app();

# -------------------------------------------------------------------------

my $mech = Test::WWW::Mechanize::PSGI->new(app => $app);
$mech->get('/');
is $mech->res->code(), 401;
$mech->credentials('oh', 'my god');
$mech->get_ok('/');
$mech->get_ok('/http://wassr.jp/');
$mech->content_contains('お気軽');

done_testing;

0 comments on commit 7e4a680

Please sign in to comment.