| 1 | package QBit::WebInterface::Apache2; | ||
| 2 | $QBit::WebInterface::Apache2::VERSION = '0.005'; | ||
| 3 | use qbit; | ||
| 4 | |||
| 5 | use base qw(QBit::WebInterface); | ||
| 6 | |||
| 7 | use Apache2::Connection; | ||
| 8 | use Apache2::RequestIO; | ||
| 9 | use Apache2::RequestRec; | ||
| 10 | use Apache2::RequestUtil; | ||
| 11 | use URI::Escape qw(uri_escape_utf8); | ||
| 12 | |||
| 13 | use QBit::WebInterface::Apache2::Request; | ||
| 14 | |||
| 15 | sub handler : method { | ||
| 16 | my ($self, $r) = @_; | ||
| 17 | |||
| 18 | $self = $self->new() unless blessed($self); | ||
| 19 | |||
| 20 | $self->request(QBit::WebInterface::Apache2::Request->new(r => $r)); | ||
| 21 | |||
| 22 | $self->build_response(); | ||
| 23 | |||
| 24 | $r->err_headers_out->add('Set-Cookie' => $_->as_string()) foreach values(%{$self->response->cookies}); | ||
| 25 | |||
| 26 | while (my ($key, $value) = each(%{$self->response->headers})) { | ||
| 27 | $r->headers_out->add($key => $value); | ||
| 28 | } | ||
| 29 | |||
| 30 | $r->content_type($self->response->content_type); | ||
| 31 | $r->headers_out->add( | ||
| 32 | 'Content-Disposition' => 'attachment; filename="' . $self->_escape_filename($self->response->filename) . '"') | ||
| 33 | if $self->response->filename; | ||
| 34 | |||
| 35 | $r->headers_out->add(Location => $self->response->location) | ||
| 36 | if $self->response->location; | ||
| 37 | |||
| 38 | eval { | ||
| 39 | $r->print( | ||
| 40 | ref($self->response->data) | ||
| 41 | ? ${$self->response->data} | ||
| 42 | : $self->response->data | ||
| 43 | ) if defined($self->response->data); | ||
| 44 | }; | ||
| 45 | |||
| 46 | my $status = $self->response->status; | ||
| 47 | $r->status($status) if defined($status); | ||
| 48 | |||
| 49 | $self->request(undef); | ||
| 50 | $self->response(undef); | ||
| 51 | |||
| 52 | return Apache2::Const::OK; | ||
| 53 | } | ||
| 54 | |||
| 55 | sub get_cmd { | ||
| 56 | my ($self) = @_; | ||
| 57 | |||
| 58 | my ($path, $cmd); | ||
| 59 | if ($self->request->uri() =~ /^\/([^?\/]+)(?:\/([^\/?#]+))?/) { | ||
| 60 | ($path, $cmd) = ($1, $2); | ||
| 61 | } else { | ||
| 62 | ($path, $cmd) = $self->default_cmd(); | ||
| 63 | } | ||
| 64 | |||
| 65 | $path = '' unless defined($path); | ||
| 66 | $cmd = '' unless defined($cmd); | ||
| 67 | |||
| 68 | return ($path, $cmd); | ||
| 69 | } | ||
| 70 | |||
| 71 | sub make_cmd { | ||
| 72 | my ($self, $new_cmd, $new_path, @params) = @_; | ||
| 73 | |||
| 74 | my %vars = defined($params[0]) | ||
| 75 | && ref($params[0]) eq 'HASH' ? %{$params[0]} : @params; | ||
| 76 | |||
| 77 | my ($path, $cmd) = $self->get_cmd(); | ||
| 78 | |||
| 79 | $path = uri_escape_utf8($self->_get_new_path($new_path, $path)); | ||
| 80 | $cmd = uri_escape_utf8($self->_get_new_cmd($new_cmd, $cmd)); | ||
| 81 | |||
| 82 | return "/$path/$cmd" | ||
| 83 | . ( | ||
| 84 | %vars | ||
| 85 | ? '?' | ||
| 86 | . join( | ||
| 87 | $self->get_option('link_param_separator', '&'), | ||
| 88 | map {uri_escape_utf8($_) . '=' . uri_escape_utf8($vars{$_})} keys(%vars) | ||
| 89 | ) | ||
| 90 | : '' | ||
| 91 | ); | ||
| 92 | } | ||
| 93 | |||
| 94 | TRUE; | ||
| 95 | |||
| 96 | __END__ |