| 1 | package QBit::WebInterface::PSGI; | ||
| 2 | $QBit::WebInterface::PSGI::VERSION = '0.003'; | ||
| 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 | my %RESPONSE_TEXT = ( | ||
| 12 | 200 => 'OK', | ||
| 13 | 201 => 'CREATED', | ||
| 14 | 202 => 'Accepted', | ||
| 15 | 203 => 'Partial Information', | ||
| 16 | 204 => 'No Response', | ||
| 17 | 301 => 'Moved', | ||
| 18 | 302 => 'Found', | ||
| 19 | 303 => 'Method', | ||
| 20 | 304 => 'Not Modified', | ||
| 21 | 400 => 'Bad request', | ||
| 22 | 401 => 'Unauthorized', | ||
| 23 | 402 => 'PaymentRequired', | ||
| 24 | 403 => 'Forbidden', | ||
| 25 | 404 => 'Not found', | ||
| 26 | 500 => 'Internal Error', | ||
| 27 | 501 => 'Not implemented', | ||
| 28 | 502 => 'Service temporarily overloaded', | ||
| 29 | 503 => 'Gateway timeout', | ||
| 30 | ); | ||
| 31 | |||
| 32 | sub run { | ||
| 33 | my ($self) = @_; | ||
| 34 | |||
| 35 | $self = $self->new() unless blessed($self); | ||
| 36 | |||
| 37 | return sub { | ||
| 38 | my ($env) = @_; | ||
| 39 | |||
| 40 | $self->request(QBit::WebInterface::PSGI::Request->new(ENV => $env)); | ||
| 41 | |||
| 42 | $self->build_response(); | ||
| 43 | |||
| 44 | my $data_ref = \$self->response->data; | ||
| 45 | if (defined($data_ref)) { | ||
| 46 | $data_ref = $$data_ref if ref($$data_ref); | ||
| 47 | utf8::encode($$data_ref) if defined($$data_ref) && utf8::is_utf8($$data_ref); | ||
| 48 | } | ||
| 49 | $data_ref = \'' unless defined($$data_ref); | ||
| 50 | |||
| 51 | my $status = $self->response->status || 200; | ||
| 52 | |||
| 53 | my @headers = (); | ||
| 54 | my @data = (); | ||
| 55 | |||
| 56 | push(@headers, 'Status' => $status . (exists($RESPONSE_TEXT{$status}) ? " $RESPONSE_TEXT{$status}" : '')); | ||
| 57 | |||
| 58 | push(@headers, 'Set-Cookie' => $_->as_string()) foreach values(%{$self->response->cookies}); | ||
| 59 | |||
| 60 | while (my ($key, $value) = each(%{$self->response->headers})) { | ||
| 61 | push(@headers, $key => $value); | ||
| 62 | } | ||
| 63 | |||
| 64 | if (!$self->response->status || $self->response->status == 200) { | ||
| 65 | push(@headers, 'Content-Type' => $self->response->content_type); | ||
| 66 | |||
| 67 | my $filename = $self->response->filename; | ||
| 68 | if (defined($filename)) { | ||
| 69 | utf8::encode($filename) if utf8::is_utf8($filename); | ||
| 70 | |||
| 71 | push(@headers, 'Content-Disposition' => 'attachment; filename="' . $self->_escape_filename($filename) . '"'); | ||
| 72 | } | ||
| 73 | |||
| 74 | push(@data, $$data_ref); | ||
| 75 | } elsif ($self->response->status == 301 || $self->response->status == 302) { | ||
| 76 | push(@headers, 'Location' => $self->response->location); | ||
| 77 | } | ||
| 78 | |||
| 79 | return [ | ||
| 80 | $status, \@headers, \@data | ||
| 81 | ]; | ||
| 82 | }; | ||
| 83 | } | ||
| 84 | |||
| 85 | sub get_cmd { | ||
| 86 | my ($self) = @_; | ||
| 87 | |||
| 88 | my ($path, $cmd); | ||
| 89 | if ($self->request->uri() =~ /^\/([^?\/]+)(?:\/([^\/?#]+))?/) { | ||
| 90 | ($path, $cmd) = ($1, $2); | ||
| 91 | } else { | ||
| 92 | ($path, $cmd) = $self->default_cmd(); | ||
| 93 | } | ||
| 94 | |||
| 95 | $path = '' unless defined($path); | ||
| 96 | $cmd = '' unless defined($cmd); | ||
| 97 | |||
| 98 | return ($path, $cmd); | ||
| 99 | } | ||
| 100 | |||
| 101 | sub make_cmd { | ||
| 102 | my ($self, $new_cmd, $new_path, @params) = @_; | ||
| 103 | |||
| 104 | my %vars = defined($params[0]) | ||
| 105 | && ref($params[0]) eq 'HASH' ? %{$params[0]} : @params; | ||
| 106 | |||
| 107 | my ($path, $cmd) = $self->get_cmd(); | ||
| 108 | |||
| 109 | $path = uri_escape_utf8($self->_get_new_path($new_path, $path)); | ||
| 110 | $cmd = uri_escape_utf8($self->_get_new_cmd($new_cmd, $cmd)); | ||
| 111 | |||
| 112 | return "/$path/$cmd" | ||
| 113 | . ( | ||
| 114 | %vars | ||
| 115 | ? '?' | ||
| 116 | . join( | ||
| 117 | $self->get_option('link_param_separator', '&'), | ||
| 118 | map {uri_escape_utf8($_) . '=' . uri_escape_utf8($vars{$_})} keys(%vars) | ||
| 119 | ) | ||
| 120 | : '' | ||
| 121 | ); | ||
| 122 | } | ||
| 123 | |||
| 124 | TRUE; | ||
| 125 | |||
| 126 | __END__ |