|
| 1 | +package WebGUI::Session::Plack; |
| 2 | + |
| 3 | +use strict; |
| 4 | +use warnings; |
| 5 | +use Carp; |
| 6 | + |
| 7 | +=head1 DESCRIPTION |
| 8 | +
|
| 9 | +This class is used instead of WebGUI::Session::Request when wg is started via plackup |
| 10 | +
|
| 11 | +=cut |
| 12 | + |
| 13 | +sub new { |
| 14 | + my ( $class, %p ) = @_; |
| 15 | + |
| 16 | + # 'require' rather than 'use' so that non-plebgui doesn't freak out |
| 17 | + require Plack::Request; |
| 18 | + my $request = Plack::Request->new( $p{env} ); |
| 19 | + my $response = $request->new_response(200); |
| 20 | + |
| 21 | + bless { |
| 22 | + %p, |
| 23 | + pnotes => {}, |
| 24 | + request => $request, |
| 25 | + response => $response, |
| 26 | + server => WebGUI::Session::Plack::Server->new( env => $p{env} ), |
| 27 | + headers_out => Plack::Util::headers( [] ), # use Plack::Util to manage response headers |
| 28 | + body => [], |
| 29 | + sendfile => undef, |
| 30 | + }, $class; |
| 31 | +} |
| 32 | + |
| 33 | +our $AUTOLOAD; |
| 34 | + |
| 35 | +sub AUTOLOAD { |
| 36 | + my $what = $AUTOLOAD; |
| 37 | + $what =~ s/.*:://; |
| 38 | + carp "!!plack->$what(@_)" unless $what eq 'DESTROY'; |
| 39 | +} |
| 40 | + |
| 41 | +# Emulate/delegate/fake Apache2::* subs |
| 42 | +sub uri { shift->{request}->path_info } |
| 43 | +sub param { shift->{request}->param(@_) } |
| 44 | +sub params { shift->{request}->prameters->mixed(@_) } |
| 45 | +sub headers_in { shift->{request}->headers(@_) } |
| 46 | +sub headers_out { shift->{headers_out} } |
| 47 | +sub protocol { shift->{request}->protocol(@_) } |
| 48 | +sub status { shift->{response}->status(@_) } |
| 49 | +sub sendfile { $_[0]->{sendfile} = $_[1] } |
| 50 | +sub server { shift->{server} } |
| 51 | +sub method { shift->{request}->method } |
| 52 | +sub upload { shift->{request}->upload(@_) } |
| 53 | +sub dir_config { shift->{server}->dir_config(@_) } |
| 54 | +sub status_line { } |
| 55 | +sub auth_type { } # should we support this? |
| 56 | +sub handler {'perl-script'} # or not..? |
| 57 | + |
| 58 | +sub content_type { |
| 59 | + my ( $self, $ct ) = @_; |
| 60 | + $self->{headers_out}->set( 'Content-Type' => $ct ); |
| 61 | +} |
| 62 | + |
| 63 | +# TODO: I suppose this should do some sort of IO::Handle thing |
| 64 | +sub print { |
| 65 | + my $self = shift; |
| 66 | + push @{ $self->{body} }, @_; |
| 67 | +} |
| 68 | + |
| 69 | +sub pnotes { |
| 70 | + my ( $self, $key ) = ( shift, shift ); |
| 71 | + return wantarray ? %{ $self->{pnotes} } : $self->{pnotes} unless defined $key; |
| 72 | + return $self->{pnotes}{$key} = $_[0] if @_; |
| 73 | + return $self->{pnotes}{$key}; |
| 74 | +} |
| 75 | + |
| 76 | +sub user { |
| 77 | + my ( $self, $user ) = @_; |
| 78 | + if ( defined $user ) { |
| 79 | + $self->{user} = $user; |
| 80 | + } |
| 81 | + $self->{user}; |
| 82 | +} |
| 83 | + |
| 84 | +sub push_handlers { |
| 85 | + my $self = shift; |
| 86 | + my ( $x, $sub ) = @_; |
| 87 | + |
| 88 | + # log it |
| 89 | + # carp "push_handlers($x)"; |
| 90 | + |
| 91 | + # run it |
| 92 | + # returns something like Apache2::Const::OK, which we just ignore because we're not modperl |
| 93 | + my $ret = $sub->($self); |
| 94 | + |
| 95 | + return; |
| 96 | +} |
| 97 | + |
| 98 | +sub finalize { |
| 99 | + my $self = shift; |
| 100 | + my $response = $self->{response}; |
| 101 | + if ( $self->{sendfile} && open my $fh, '<', $self->{sendfile} ) { |
| 102 | + $response->body($fh); |
| 103 | + } |
| 104 | + else { |
| 105 | + $response->body( $self->{body} ); |
| 106 | + } |
| 107 | + $response->headers( $self->{headers_out}->headers ); |
| 108 | + return $response->finalize; |
| 109 | +} |
| 110 | + |
| 111 | +sub no_cache { |
| 112 | + my ( $self, $doit ) = @_; |
| 113 | + if ($doit) { |
| 114 | + $self->{headers_out}->set( 'Pragma' => 'no-cache', 'Cache-control' => 'no-cache' ); |
| 115 | + } |
| 116 | + else { |
| 117 | + $self->{headers_out}->remove( 'Pragma', 'Cache-control' ); |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +################################################ |
| 122 | + |
| 123 | +package WebGUI::Session::Plack::Server; |
| 124 | + |
| 125 | +use strict; |
| 126 | +use warnings; |
| 127 | +use Carp; |
| 128 | + |
| 129 | +sub new { |
| 130 | + my $class = shift; |
| 131 | + bless {@_}, $class; |
| 132 | +} |
| 133 | + |
| 134 | +our $AUTOLOAD; |
| 135 | + |
| 136 | +sub AUTOLOAD { |
| 137 | + my $what = $AUTOLOAD; |
| 138 | + $what =~ s/.*:://; |
| 139 | + carp "!!server->$what(@_)" unless $what eq 'DESTROY'; |
| 140 | +} |
| 141 | + |
| 142 | +sub dir_config { |
| 143 | + my ( $self, $c ) = @_; |
| 144 | + |
| 145 | + # Translate the legacy WebguiRoot and WebguiConfig PerlSetVar's into known values |
| 146 | + return WebGUI->root if $c eq 'WebguiRoot'; |
| 147 | + return WebGUI->config_file if $c eq 'WebguiConfig'; |
| 148 | + |
| 149 | + # Otherwise, we might want to provide some sort of support (which Apache is still around) |
| 150 | + return $self->{env}->{"wg.DIR_CONFIG.$c"}; |
| 151 | +} |
| 152 | + |
| 153 | +################################################ |
| 154 | + |
| 155 | +package Plack::Request::Upload; |
| 156 | + |
| 157 | +sub link { shift->link_to(@_) } |
| 158 | + |
| 159 | +1; |
0 commit comments