« blib/lib/auto/share/dist/Cmd-Dwarf/app/lib/Dwarf/Module/SocialMedia/Weibo.pm
↑ Summary
blib/lib/auto/share/dist/Cmd-Dwarf/app/lib/Dwarf/Pragma.pm »
| 1 | package Dwarf::Module::APIBase; | ||
| 2 | use Dwarf::Pragma; | ||
| 3 | use parent 'Dwarf::Module'; | ||
| 4 | use Dwarf::Validator; | ||
| 5 | use Dwarf::Validator::Constraint; | ||
| 6 | use Dwarf::Util qw/safe_decode_json encode_utf8/; | ||
| 7 | use Carp qw/croak/; | ||
| 8 | use HTTP::Date; | ||
| 9 | |||
| 10 | use Dwarf::Accessor { | ||
| 11 | ro => [qw/autoflush_validation_error/], | ||
| 12 | }; | ||
| 13 | |||
| 14 | # バリデーションエラー時に直ちにエラーを送出するかどうか | ||
| 15 | sub _build_autoflush_validation_error { 1 } | ||
| 16 | |||
| 17 | |||
| 18 | sub init { | ||
| 19 | my ($self, $c) = @_; | ||
| 20 | |||
| 21 | $c->error->autoflush(1) if $self->autoflush_validation_error; | ||
| 22 | |||
| 23 | $c->add_trigger(BEFORE_RENDER => $self->can('will_render')); | ||
| 24 | $c->add_trigger(AFTER_RENDER => $self->can('did_render')); | ||
| 25 | $c->add_trigger(ERROR => $self->can('receive_error')); | ||
| 26 | $c->add_trigger(SERVER_ERROR => $self->can('receive_server_error')); | ||
| 27 | |||
| 28 | $self->header('Pragma' => 'no-cache'); | ||
| 29 | $self->header('Cache-Control' => 'no-cache'); | ||
| 30 | $self->header('Expires' => HTTP::Date::time2str(time - 24 * 60 * 60)); | ||
| 31 | $self->header('X-Content-Type-Options' => 'nosniff'); # http://blogs.msdn.com/b/ie/archive/2008/07/02/ie8-security-part-v-comprehensive-protection.aspx | ||
| 32 | $self->header('X-Frame-Options' => 'DENY'); # http://blog.mozilla.com/security/2010/09/08/x-frame-options/ | ||
| 33 | |||
| 34 | $self->init_plugins($c); | ||
| 35 | |||
| 36 | $self->type('application/json; charset=UTF-8'); | ||
| 37 | |||
| 38 | # defense from JSON hijacking | ||
| 39 | my $user_agent = $c->req->user_agent || ''; | ||
| 40 | my $request_method = $c->req->method || 'GET'; | ||
| 41 | if ((!$c->req->header('X-Requested-With')) && $user_agent =~ /android/i && defined $c->req->header('Cookie') && $request_method eq 'GET') { | ||
| 42 | $c->{response} = $c->req->new_response; | ||
| 43 | $c->res->status(403); | ||
| 44 | $c->res->content_type('text/html; charset=utf-8'); | ||
| 45 | $c->finish("Your request may be JSON hijacking.\nIf you are not an attacker, please add 'X-Requested-With' header to each request."); | ||
| 46 | } | ||
| 47 | |||
| 48 | if (defined $c->ext and $c->ext eq 'xml' and $c->can('encode_xml')) { | ||
| 49 | $self->type('application/xml; charset=UTF-8'); | ||
| 50 | } | ||
| 51 | |||
| 52 | $self->will_dispatch($c); | ||
| 53 | $self->error->flush; | ||
| 54 | $self->error->autoflush(1); | ||
| 55 | } | ||
| 56 | |||
| 57 | sub init_plugins { | ||
| 58 | my ($self, $c) = @_; | ||
| 59 | |||
| 60 | $c->load_plugins( | ||
| 61 | 'Error' => { | ||
| 62 | LACK_OF_PARAM => sub { shift->throw(1001, sprintf("missing mandatory parameters: %s", $_[0] || "")) }, | ||
| 63 | INVALID_PARAM => sub { shift->throw(1002, sprintf("illegal parameter: %s", $_[0] || "")) }, | ||
| 64 | NEED_TO_LOGIN => sub { shift->throw(1003, sprintf("You must login.")) }, | ||
| 65 | SNS_LIMIT_ERROR => sub { shift->throw(2001, sprintf("SNS Limit Error: reset at %s", $_[0] || "")) }, | ||
| 66 | SNS_ERROR => sub { shift->throw(2002, sprintf("SNS Error: %s", $_[0] || "SNS Error.")) }, | ||
| 67 | ERROR => sub { shift->throw(400, sprintf("%s", $_[0] || "Unknown Error.")) }, | ||
| 68 | }, | ||
| 69 | 'JSON' => { pretty => 1 }, | ||
| 70 | 'XML::Simple' => { | ||
| 71 | NoAttr => 1, | ||
| 72 | KeyAttr => [], | ||
| 73 | SuppressEmpty => '' | ||
| 74 | }, | ||
| 75 | ); | ||
| 76 | } | ||
| 77 | |||
| 78 | sub will_dispatch {} | ||
| 79 | sub did_dispatch {} | ||
| 80 | |||
| 81 | sub validate { | ||
| 82 | my ($self, @rules) = @_; | ||
| 83 | return unless @rules; | ||
| 84 | |||
| 85 | my $validator = Dwarf::Validator->new($self->c->req)->check(@rules); | ||
| 86 | if ($validator->has_error) { | ||
| 87 | while (my ($param, $detail) = each %{ $validator->errors }) { | ||
| 88 | $self->c->error->LACK_OF_PARAM($param) if $detail->{NOT_NULL} || $detail->{NOT_BLANK}; | ||
| 89 | $self->c->error->LACK_OF_PARAM($param) if $detail->{FILE_NOT_NULL}; | ||
| 90 | $self->c->error->INVALID_PARAM($param); | ||
| 91 | } | ||
| 92 | } | ||
| 93 | } | ||
| 94 | |||
| 95 | sub validate_json_body { | ||
| 96 | my ($self, @rules) = @_; | ||
| 97 | my $json = $self->c->req->content; | ||
| 98 | |||
| 99 | $json = eval { safe_decode_json(encode_utf8 $json) }; | ||
| 100 | if ($@) { | ||
| 101 | $self->c->error->ERROR('JSON decode error: ' . $@); | ||
| 102 | } | ||
| 103 | |||
| 104 | $json = [ $json ] unless ref $json eq 'ARRAY'; | ||
| 105 | |||
| 106 | eval { $self->args({ @rules }, $self, $_) for @$json }; | ||
| 107 | if ($@) { | ||
| 108 | $self->c->error->ERROR($@); | ||
| 109 | } | ||
| 110 | } | ||
| 111 | |||
| 112 | sub validate_response { | ||
| 113 | my ($self, @rules) = @_; | ||
| 114 | return if $self->c->is_production; | ||
| 115 | |||
| 116 | my $res = $self->c->response->body; | ||
| 117 | |||
| 118 | eval { $self->args({ @rules }, $self, $res) }; | ||
| 119 | if ($@) { | ||
| 120 | $self->c->error->ERROR($@); | ||
| 121 | } | ||
| 122 | } | ||
| 123 | |||
| 124 | # レンダリング前の共通処理 | ||
| 125 | sub will_render { | ||
| 126 | my ($self, $c, $data) = @_; | ||
| 127 | $self->response_http_status($data); | ||
| 128 | } | ||
| 129 | |||
| 130 | # レンダリング後の共通処理 | ||
| 131 | sub did_render { | ||
| 132 | my ($self, $c, $data) = @_; | ||
| 133 | } | ||
| 134 | |||
| 135 | # 400 系のエラー | ||
| 136 | sub receive_error { | ||
| 137 | my ($self, $c, $error) = @_; | ||
| 138 | my (@codes, @messages); | ||
| 139 | |||
| 140 | for my $m (@{ $error->messages }) { | ||
| 141 | print STDERR sprintf "[API Error] code = %s, message = %s\n", $m->data->[0], $m->data->[1]; | ||
| 142 | push @codes, $m->data->[0]; | ||
| 143 | push @messages, $m->data->[1]; | ||
| 144 | } | ||
| 145 | |||
| 146 | my $data = { | ||
| 147 | error_code => @codes == 1 ? $codes[0] : \@codes, | ||
| 148 | error_message => @messages == 1 ? $messages[0] : \@messages, | ||
| 149 | }; | ||
| 150 | |||
| 151 | return $data; | ||
| 152 | } | ||
| 153 | |||
| 154 | # 500 系のエラー | ||
| 155 | sub receive_server_error { | ||
| 156 | my ($self, $c, $error) = @_; | ||
| 157 | |||
| 158 | $error ||= 'Internal Server Error'; | ||
| 159 | print STDERR sprintf "[Server Error] %s\n", $error; | ||
| 160 | |||
| 161 | load_plugins('Devel::StackTrace' => {}); | ||
| 162 | |||
| 163 | my $data = { | ||
| 164 | error_code => 500, | ||
| 165 | error_message => $c->stacktrace($error), | ||
| 166 | }; | ||
| 167 | |||
| 168 | return $data; | ||
| 169 | } | ||
| 170 | |||
| 171 | # HTTP ステータスの調整 | ||
| 172 | sub response_http_status { | ||
| 173 | my ($self, $data) = @_; | ||
| 174 | $data ||= {}; | ||
| 175 | return unless ref $data eq 'HASH'; | ||
| 176 | |||
| 177 | my $status = 200; | ||
| 178 | if ($data->{error_code}) { | ||
| 179 | $status = $data->{error_code} == 500 ? 500 : 400; | ||
| 180 | } | ||
| 181 | |||
| 182 | if (defined $self->param('response_http_status')) { | ||
| 183 | $self->status(scalar $self->param('response_http_status')); | ||
| 184 | $data->{http_status} ||= $status; | ||
| 185 | $status = 200; | ||
| 186 | } | ||
| 187 | |||
| 188 | $self->res->status($status); | ||
| 189 | } | ||
| 190 | |||
| 191 | 1; |