| 1 | package QBit::WebInterface::FastCGI; | ||
| 2 | $QBit::WebInterface::FastCGI::VERSION = '0.005'; | ||
| 3 | use qbit; | ||
| 4 | |||
| 5 | use base qw(QBit::WebInterface); | ||
| 6 | |||
| 7 | use QBit::WebInterface::FastCGI::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, $r) = @_; | ||
| 34 | |||
| 35 | $self = $self->new() unless blessed($self); | ||
| 36 | |||
| 37 | $self->request(QBit::WebInterface::FastCGI::Request->new(request => $r)); | ||
| 38 | |||
| 39 | $self->build_response(); | ||
| 40 | |||
| 41 | my $data_ref = \$self->response->data; | ||
| 42 | if (defined($data_ref)) { | ||
| 43 | $data_ref = $$data_ref if ref($$data_ref); | ||
| 44 | utf8::encode($$data_ref) if defined($$data_ref) && utf8::is_utf8($$data_ref); | ||
| 45 | } | ||
| 46 | $data_ref = \'' unless defined($$data_ref); | ||
| 47 | |||
| 48 | binmode(STDOUT); | ||
| 49 | |||
| 50 | my $status = $self->response->status || 200; | ||
| 51 | print "Status: $status" . (exists($RESPONSE_TEXT{$status}) ? " $RESPONSE_TEXT{$status}" : '') . "\n"; | ||
| 52 | print 'Set-Cookie: ' . $_->as_string() . "\n" foreach values(%{$self->response->cookies}); | ||
| 53 | |||
| 54 | while (my ($key, $value) = each(%{$self->response->headers})) { | ||
| 55 | print "$key: $value\n"; | ||
| 56 | } | ||
| 57 | |||
| 58 | if (!$self->response->status || $self->response->status == 200) { | ||
| 59 | print 'Content-Type: ' . $self->response->content_type . "\n"; | ||
| 60 | |||
| 61 | my $filename = $self->response->filename; | ||
| 62 | if (defined($filename)) { | ||
| 63 | utf8::encode($filename) if utf8::is_utf8($filename); | ||
| 64 | |||
| 65 | print 'Content-Disposition: attachment; filename="' . $self->_escape_filename($filename) . "\"\n"; | ||
| 66 | } | ||
| 67 | |||
| 68 | print "\n", $$data_ref; | ||
| 69 | } elsif ($self->response->status == 301 || $self->response->status == 302) { | ||
| 70 | print 'Location: ' . $self->response->location . "\n\n"; | ||
| 71 | } else { | ||
| 72 | print "\n"; | ||
| 73 | } | ||
| 74 | } | ||
| 75 | |||
| 76 | sub get_cmd { | ||
| 77 | my ($self) = @_; | ||
| 78 | |||
| 79 | my ($path, $cmd); | ||
| 80 | if ($self->request->uri() =~ /^\/([^?\/]+)(?:\/([^\/?#]+))?/) { | ||
| 81 | ($path, $cmd) = ($1, $2); | ||
| 82 | } else { | ||
| 83 | ($path, $cmd) = $self->default_cmd(); | ||
| 84 | } | ||
| 85 | |||
| 86 | $path = '' unless defined($path); | ||
| 87 | $cmd = '' unless defined($cmd); | ||
| 88 | |||
| 89 | return ($path, $cmd); | ||
| 90 | } | ||
| 91 | |||
| 92 | sub make_cmd { | ||
| 93 | my ($self, $new_cmd, $new_path, @params) = @_; | ||
| 94 | |||
| 95 | my %vars = defined($params[0]) | ||
| 96 | && ref($params[0]) eq 'HASH' ? %{$params[0]} : @params; | ||
| 97 | |||
| 98 | my ($path, $cmd) = $self->get_cmd(); | ||
| 99 | |||
| 100 | $path = uri_escape_utf8($self->_get_new_path($new_path, $path)); | ||
| 101 | $cmd = uri_escape_utf8($self->_get_new_cmd($new_cmd, $cmd)); | ||
| 102 | |||
| 103 | return "/$path/$cmd" | ||
| 104 | . ( | ||
| 105 | %vars | ||
| 106 | ? '?' | ||
| 107 | . join( | ||
| 108 | $self->get_option('link_param_separator', '&'), | ||
| 109 | map {uri_escape_utf8($_) . '=' . uri_escape_utf8($vars{$_})} keys(%vars) | ||
| 110 | ) | ||
| 111 | : '' | ||
| 112 | ); | ||
| 113 | } | ||
| 114 | |||
| 115 | TRUE; | ||
| 116 | |||
| 117 | __END__ |