| 1 | package PEF::Front::Response; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use Time::Duration::Parse; | ||
| 5 | use Scalar::Util qw(blessed); | ||
| 6 | use Encode; | ||
| 7 | use utf8; | ||
| 8 | use URI::Escape; | ||
| 9 | use URI; | ||
| 10 | use PEF::Front::Config; | ||
| 11 | use PEF::Front::Headers; | ||
| 12 | |||
| 13 | sub new { | ||
| 14 | my ($class, %args) = @_; | ||
| 15 | my $status = delete $args{status} || 200; | ||
| 16 | my $body = delete $args{body} || []; | ||
| 17 | my $base_url = delete $args{base} || ''; | ||
| 18 | my $href = delete $args{headers} || []; | ||
| 19 | my $cref = delete $args{cookies} || []; | ||
| 20 | my @headers; | ||
| 21 | my @cookies; | ||
| 22 | if (ref($href) eq 'HASH') { | ||
| 23 | @headers = %$href; | ||
| 24 | } elsif (ref($href) eq 'ARRAY') { | ||
| 25 | @headers = @$href; | ||
| 26 | } else { | ||
| 27 | @headers = ($href); | ||
| 28 | } | ||
| 29 | if (ref($cref) eq 'HASH') { | ||
| 30 | @cookies = %$cref; | ||
| 31 | } elsif (ref($cref) eq 'ARRAY') { | ||
| 32 | @cookies = @$cref; | ||
| 33 | } else { | ||
| 34 | @cookies = ($cref); | ||
| 35 | } | ||
| 36 | my $request = delete $args{request}; | ||
| 37 | if ($request && blessed($request) && $request->isa("PEF::Front::Request")) { | ||
| 38 | $base_url ||= $request->base; | ||
| 39 | if (not $request->out_headers->is_empty) { | ||
| 40 | unshift @headers, $request->out_headers; | ||
| 41 | } | ||
| 42 | if (not $request->out_cookies->is_empty) { | ||
| 43 | unshift @cookies, $request->out_cookies; | ||
| 44 | } | ||
| 45 | } | ||
| 46 | $body = [$body] if not ref $body; | ||
| 47 | my $self = bless { | ||
| 48 | status => $status, | ||
| 49 | headers => PEF::Front::HTTPHeaders->new(@headers), | ||
| 50 | cookies => PEF::Front::Headers->new(@cookies), | ||
| 51 | body => $body, | ||
| 52 | base_url => $base_url | ||
| 53 | }, $class; | ||
| 54 | $self; | ||
| 55 | } | ||
| 56 | |||
| 57 | sub add_header { | ||
| 58 | my ($self, $key, $value) = @_; | ||
| 59 | $self->{headers}->add_header($key, $value); | ||
| 60 | } | ||
| 61 | |||
| 62 | sub set_header { | ||
| 63 | my ($self, $key, $value) = @_; | ||
| 64 | $self->{headers}->set_header($key, $value); | ||
| 65 | } | ||
| 66 | |||
| 67 | sub remove_header { | ||
| 68 | my ($self, $key, $value) = @_; | ||
| 69 | $self->{headers}->remove_header($key, $value); | ||
| 70 | } | ||
| 71 | |||
| 72 | sub get_header { | ||
| 73 | my ($self, $key) = @_; | ||
| 74 | $self->{headers}->get_header($key); | ||
| 75 | } | ||
| 76 | |||
| 77 | sub set_cookie { | ||
| 78 | my ($self, $key, $value) = @_; | ||
| 79 | $self->{cookies}->set_header($key, $value); | ||
| 80 | } | ||
| 81 | |||
| 82 | sub remove_cookie { | ||
| 83 | my ($self, $key) = @_; | ||
| 84 | $self->{cookies}->remove_header($key); | ||
| 85 | } | ||
| 86 | |||
| 87 | sub get_cookie { | ||
| 88 | my ($self, $key) = @_; | ||
| 89 | $self->{cookies}->get_header($key); | ||
| 90 | } | ||
| 91 | |||
| 92 | sub set_body { | ||
| 93 | my ($self, $body) = @_; | ||
| 94 | $self->{body} = ref($body) ? $body : [$body]; | ||
| 95 | } | ||
| 96 | |||
| 97 | sub add_body { | ||
| 98 | my ($self, $body) = @_; | ||
| 99 | if (ref($self->{body}) eq 'ARRAY') { | ||
| 100 | push @{$self->{body}}, $body; | ||
| 101 | } else { | ||
| 102 | $self->set_body($body); | ||
| 103 | } | ||
| 104 | } | ||
| 105 | |||
| 106 | sub get_body {$_[0]->{body}} | ||
| 107 | |||
| 108 | sub status { | ||
| 109 | my ($self, $status) = @_; | ||
| 110 | $self->{status} = $status if defined $status; | ||
| 111 | $self->{status}; | ||
| 112 | } | ||
| 113 | |||
| 114 | sub redirect { | ||
| 115 | my ($self, $url, $status) = @_; | ||
| 116 | if ($url) { | ||
| 117 | if ($self->{base_url} ne '') { | ||
| 118 | my $nuri = URI->new_abs($url, $self->{base_url}); | ||
| 119 | $url = $nuri->as_string; | ||
| 120 | } | ||
| 121 | $self->set_header(Location => $url); | ||
| 122 | if (!defined $status) { | ||
| 123 | $status ||= $self->status; | ||
| 124 | $self->status(302) if $status < 300 || $status > 399; | ||
| 125 | } else { | ||
| 126 | $self->status($status); | ||
| 127 | } | ||
| 128 | $self->remove_header('Content-Type'); | ||
| 129 | } | ||
| 130 | } | ||
| 131 | |||
| 132 | sub content_type { | ||
| 133 | my ($self, $type) = @_; | ||
| 134 | $self->set_header('Content-Type' => $type) if $type; | ||
| 135 | return $self->get_header('Content-Type') if defined wantarray; | ||
| 136 | } | ||
| 137 | |||
| 138 | my @dow = qw(Sun Mon Tue Wed Thu Fri Sat); | ||
| 139 | my @mon = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec); | ||
| 140 | |||
| 141 | sub expires { | ||
| 142 | my $expires = eval {parse_duration($_[0])} || 0; | ||
| 143 | my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = gmtime(time + $expires); | ||
| 144 | sprintf "%s, %02d-%s-%04d %02d:%02d:%02d GMT", $dow[$wday], $mday, $mon[$mon], 1900 + $year, $hour, $min, $sec; | ||
| 145 | } | ||
| 146 | |||
| 147 | sub _safe_encode_utf8 { | ||
| 148 | return encode_utf8($_[0]) if not ref($_[0]) && utf8::is_utf8($_[0]); | ||
| 149 | $_[0]; | ||
| 150 | } | ||
| 151 | |||
| 152 | sub make_headers { | ||
| 153 | my ($self) = @_; | ||
| 154 | my $headers = $self->{headers}->get_all_headers; | ||
| 155 | for (@$headers) { | ||
| 156 | $_ = encode_utf8($_) if utf8::is_utf8($_); | ||
| 157 | } | ||
| 158 | my $cookies = $self->{cookies}->get_all_headers; | ||
| 159 | for (my $i = 0; $i < @$cookies; $i += 2) { | ||
| 160 | my $name = _safe_encode_utf8($cookies->[$i]); | ||
| 161 | my $value = $cookies->[$i + 1]; | ||
| 162 | $value = '' if not defined $value; | ||
| 163 | $value = {value => $value} unless ref($value) eq 'HASH'; | ||
| 164 | no utf8; | ||
| 165 | my @cookie = (URI::Escape::uri_escape($name) . "=" . URI::Escape::uri_escape(_safe_encode_utf8($value->{value}))); | ||
| 166 | push @cookie, "domain=" . $value->{domain} if $value->{domain}; | ||
| 167 | push @cookie, "path=" . $value->{path} if $value->{path}; | ||
| 168 | push @cookie, "expires=" . expires($value->{expires}) if $value->{expires}; | ||
| 169 | push @cookie, "max-age=" . $value->{"max-age"} if $value->{"max-age"}; | ||
| 170 | push @cookie, "secure" if $value->{secure}; | ||
| 171 | push @cookie, "HttpOnly" if $value->{httponly}; | ||
| 172 | push @$headers, ('Set-Cookie' => join "; ", @cookie); | ||
| 173 | } | ||
| 174 | return [$self->status, $headers]; | ||
| 175 | } | ||
| 176 | |||
| 177 | sub response { | ||
| 178 | my ($self) = @_; | ||
| 179 | my $out = $self->make_headers; | ||
| 180 | if ('ARRAY' eq ref $self->{body}) { | ||
| 181 | for (@{$self->{body}}) { | ||
| 182 | $_ = encode_utf8($_) if utf8::is_utf8($_); | ||
| 183 | } | ||
| 184 | } | ||
| 185 | push @$out, $self->{body}; | ||
| 186 | return $out; | ||
| 187 | |||
| 188 | } | ||
| 189 | |||
| 190 | 1; | ||
| 191 | |||
| 192 | __END__ |