| 1 | package App::Mobirc::Web::Handler; |
|---|
| 2 | use Moose; |
|---|
| 3 | use Scalar::Util qw/blessed/; |
|---|
| 4 | use Data::Visitor::Encode; |
|---|
| 5 | use HTTP::MobileAgent; |
|---|
| 6 | use HTTP::MobileAgent::Plugin::Charset; |
|---|
| 7 | use Module::Find; |
|---|
| 8 | |
|---|
| 9 | use App::Mobirc; |
|---|
| 10 | use App::Mobirc::Util; |
|---|
| 11 | use App::Mobirc::Web::Router; |
|---|
| 12 | useall 'App::Mobirc::Web::C'; |
|---|
| 13 | |
|---|
| 14 | my $dve = Data::Visitor::Encode->new; |
|---|
| 15 | |
|---|
| 16 | sub context () { App::Mobirc->context } ## no critic |
|---|
| 17 | |
|---|
| 18 | sub handler { |
|---|
| 19 | my $req = shift; |
|---|
| 20 | |
|---|
| 21 | my $res = _handler($req); |
|---|
| 22 | context->run_hook('response_filter', $res); |
|---|
| 23 | $res; |
|---|
| 24 | } |
|---|
| 25 | |
|---|
| 26 | sub _handler { |
|---|
| 27 | my $req = shift; |
|---|
| 28 | |
|---|
| 29 | context->run_hook('request_filter', $req); |
|---|
| 30 | |
|---|
| 31 | if (authorize($req)) { |
|---|
| 32 | return process_request($req); |
|---|
| 33 | } else { |
|---|
| 34 | HTTP::Engine::Response->new( |
|---|
| 35 | status => 401, |
|---|
| 36 | headers => HTTP::Headers->new( |
|---|
| 37 | 'WWW-Authenticate' => qq{Basic Realm="mobirc"} |
|---|
| 38 | ), |
|---|
| 39 | ); |
|---|
| 40 | } |
|---|
| 41 | } |
|---|
| 42 | |
|---|
| 43 | sub authorize { |
|---|
| 44 | my $req = shift; |
|---|
| 45 | |
|---|
| 46 | if (context->run_hook_first('authorize', $req)) { |
|---|
| 47 | DEBUG "AUTHORIZATION SUCCEEDED"; |
|---|
| 48 | return 1; # authorization succeeded. |
|---|
| 49 | } else { |
|---|
| 50 | return 0; # authorization failed |
|---|
| 51 | } |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | sub process_request { |
|---|
| 55 | my ($req, ) = @_; |
|---|
| 56 | |
|---|
| 57 | my $rule = App::Mobirc::Web::Router->match($req); |
|---|
| 58 | |
|---|
| 59 | unless ($rule) { |
|---|
| 60 | # hook by plugins |
|---|
| 61 | if (my $res = context->run_hook_first( 'httpd', $req )) { |
|---|
| 62 | # XXX we should use html filter? |
|---|
| 63 | return $res; |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | # doesn't match. |
|---|
| 67 | do { |
|---|
| 68 | my $uri = $req->uri->path; |
|---|
| 69 | warn "dan the 404 not found: $uri" if $uri ne '/favicon.ico'; |
|---|
| 70 | |
|---|
| 71 | return HTTP::Engine::Response->new( |
|---|
| 72 | status => 404, |
|---|
| 73 | body => "404 not found: $uri", |
|---|
| 74 | ); |
|---|
| 75 | }; |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | my $controller = "App::Mobirc::Web::C::$rule->{controller}"; |
|---|
| 79 | |
|---|
| 80 | my $meth = $rule->{action}; |
|---|
| 81 | my $post_meth = "post_dispatch_$meth"; |
|---|
| 82 | my $get_meth = "dispatch_$meth"; |
|---|
| 83 | my $args = $dve->decode( $req->mobile_agent->encoding, $rule->{args} ); |
|---|
| 84 | if ( $req->method =~ /POST/i && $controller->can($post_meth)) { |
|---|
| 85 | return $controller->$post_meth($req, $args); |
|---|
| 86 | } else { |
|---|
| 87 | return $controller->$get_meth($req, $args); |
|---|
| 88 | } |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | no Moose;__PACKAGE__->meta->make_immutable; |
|---|
| 92 | 1; |
|---|
| 93 | |
|---|