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