| 1 | package QBit::WebInterface::PSGI; | ||
| 2 | $QBit::WebInterface::PSGI::VERSION = '0.004'; | ||
| 3 | use qbit; | ||
| 4 | |||
| 5 | use base qw(QBit::WebInterface); | ||
| 6 | |||
| 7 | use QBit::WebInterface::PSGI::Request; | ||
| 8 | |||
| 9 | use URI::Escape qw(uri_escape_utf8); | ||
| 10 | |||
| 11 | sub run { | ||
| 12 | my ($self) = @_; | ||
| 13 | |||
| 14 | $self = $self->new() unless blessed($self); | ||
| 15 | |||
| 16 | return sub { | ||
| 17 | my ($env) = @_; | ||
| 18 | |||
| 19 | $self->request(QBit::WebInterface::PSGI::Request->new(ENV => $env)); | ||
| 20 | |||
| 21 | $self->build_response(); | ||
| 22 | |||
| 23 | my $data_ref = \$self->response->data; | ||
| 24 | if (defined($data_ref)) { | ||
| 25 | $data_ref = $$data_ref if ref($$data_ref); | ||
| 26 | utf8::encode($$data_ref) if defined($$data_ref) && utf8::is_utf8($$data_ref); | ||
| 27 | } | ||
| 28 | $data_ref = \'' unless defined($$data_ref); | ||
| 29 | |||
| 30 | my $status = $self->response->status || 200; | ||
| 31 | |||
| 32 | my @headers = (); | ||
| 33 | my @data = (); | ||
| 34 | |||
| 35 | push( | ||
| 36 | @headers, | ||
| 37 | 'Status' => $status | ||
| 38 | . ( | ||
| 39 | exists($QBit::WebInterface::HTTP_STATUSES{$status}) | ||
| 40 | ? " $QBit::WebInterface::HTTP_STATUSES{$status}" | ||
| 41 | : '' | ||
| 42 | ) | ||
| 43 | ); | ||
| 44 | |||
| 45 | push(@headers, 'Set-Cookie' => $_->as_string()) foreach values(%{$self->response->cookies}); | ||
| 46 | |||
| 47 | while (my ($key, $value) = each(%{$self->response->headers})) { | ||
| 48 | push(@headers, $key => $value); | ||
| 49 | } | ||
| 50 | |||
| 51 | if (!$self->response->status || $self->response->status == 200) { | ||
| 52 | push(@headers, 'Content-Type' => $self->response->content_type); | ||
| 53 | |||
| 54 | my $filename = $self->response->filename; | ||
| 55 | if (defined($filename)) { | ||
| 56 | utf8::encode($filename) if utf8::is_utf8($filename); | ||
| 57 | |||
| 58 | push(@headers, | ||
| 59 | 'Content-Disposition' => 'attachment; filename="' . $self->_escape_filename($filename) . '"'); | ||
| 60 | } | ||
| 61 | |||
| 62 | push(@data, $$data_ref); | ||
| 63 | } elsif ($self->response->status == 301 || $self->response->status == 302) { | ||
| 64 | push(@headers, 'Location' => $self->response->location); | ||
| 65 | } | ||
| 66 | |||
| 67 | return [$status, \@headers, \@data]; | ||
| 68 | }; | ||
| 69 | } | ||
| 70 | |||
| 71 | TRUE; | ||
| 72 | |||
| 73 | __END__ |