]> git.decadent.org.uk Git - maypole.git/blob - lib/CGI/Maypole.pm
fixed pod for warn method
[maypole.git] / lib / CGI / Maypole.pm
1 package CGI::Maypole;
2 use base 'Maypole';
3
4 use strict;
5 use warnings;
6 use CGI::Simple;
7 use Maypole::Headers;
8 use Maypole::Constants;
9
10 our $VERSION = '2.11';
11
12 __PACKAGE__->mk_accessors( qw/cgi/ );
13
14 =head1 NAME
15
16 CGI::Maypole - CGI-based front-end to Maypole
17
18 =head1 SYNOPSIS
19
20      package BeerDB;
21      use Maypole::Application;
22
23      ## example beer.cgi:
24
25      #!/usr/bin/perl -w
26      use strict;
27      use BeerDB;
28      BeerDB->run();
29
30 Now to access the beer database, type this URL into your browser:
31 http://your.site/cgi-bin/beer.cgi/frontpage
32
33 NOTE: this Maypole frontend requires additional modules that won't be installed
34 or included with Maypole. Please see below.
35
36 =head1 DESCRIPTION
37
38 This is a CGI platform driver for Maypole. Your application can inherit from
39 CGI::Maypole directly, but it is recommended that you use
40 L<Maypole::Application>.
41
42 This module requires CGI::Simple which you will have to install yourself via
43 CPAN or manually.
44
45 =head1 METHODS
46
47 =over
48
49 =item run
50
51 Call this from your CGI script to start the Maypole application.
52
53 =back
54
55 =cut
56
57 sub run 
58 {
59     my $self = shift;
60     return $self->handler;
61 }
62
63 =head1 Implementation
64
65 This class overrides a set of methods in the base Maypole class to provide it's
66 functionality. See L<Maypole> for these:
67
68 =over
69
70 =item get_request
71
72 =cut
73
74 sub get_request 
75 {
76     shift->cgi( CGI::Simple->new );
77 }
78
79 =item parse_location
80
81 =cut
82
83 sub parse_location 
84 {
85     my $r = shift;
86     my $cgi = $r->cgi;
87
88     # Reconstruct the request headers (as far as this is possible)
89     $r->headers_in(Maypole::Headers->new);
90     for my $http_header ($cgi->http) {
91         (my $field_name = $http_header) =~ s/^HTTPS?_//;
92         $r->headers_in->set($field_name => $cgi->http($http_header));
93     }
94
95     my $path = $cgi->url( -absolute => 1, -path_info => 1 );
96     my $loc = $cgi->url( -absolute => 1 );
97     {
98         no warnings 'uninitialized';
99         $path .= '/' if $path eq $loc;
100         $path =~ s/^($loc)?\///;
101     }
102     $r->path($path);
103     
104     $r->parse_path;
105     $r->parse_args;
106 }
107
108 =item warn
109
110 =cut
111
112 sub warn {
113     my ($self,@args) = @_;
114     my ($package, $line) = (caller)[0,2];
115     warn "[$package line $line] ", @args ;
116     return;
117 }
118
119 =item parse_args
120
121 =cut
122
123 sub parse_args 
124 {
125     my $r = shift;
126     my (%vars) = $r->cgi->Vars;
127     while ( my ( $key, $value ) = each %vars ) {
128         my @values = split "\0", $value;
129         $vars{$key} = @values <= 1 ? $values[0] : \@values;
130     }
131     $r->params( {%vars} );
132     $r->query( $r->params );
133 }
134
135 =item redirect_request
136
137 =cut
138
139 # FIXME: use headers_in to gather host and other information?
140 sub redirect_request 
141 {
142   my $r = shift;
143   my $redirect_url = $_[0];
144   my $status = "302";
145   if ($_[1]) {
146     my %args = @_;
147     if ($args{url}) {
148       $redirect_url = $args{url};
149     } else {
150       my $path = $args{path} || $r->cgi->url(-absolute => 1, -query=>1);
151       my $host = $args{domain};
152       ($host = $r->cgi->url(-base => 1)) =~ s/^https?:\/\///i unless ($host);
153       my $protocol = $args{protocol} || $r->get_protocol;
154       $redirect_url = "${protocol}://${host}/${path}";
155     }
156     $status = $args{status} if ($args{status});
157   }
158
159   $r->headers_out->set('Status' => $status);
160   $r->headers_out->set('Location' => $redirect_url);
161
162   return;
163 }
164
165 =item get_protocol
166
167 =cut
168
169 sub get_protocol 
170 {
171   my $self = shift;
172   my $protocol = ($self->cgi->https) ? 'https' : 'http';
173   return $protocol;
174 }
175
176 =item send_output
177
178 Generates output (using C<collect_output>) and prints it. 
179
180 =cut
181
182 sub send_output 
183 {
184     my $r = shift;
185     print $r->collect_output;
186 }
187
188 =item collect_output
189
190 Gathers headers and output together into a string and returns it.
191
192 Splitting this code out of C<send_output> supports L<Maypole::HTTPD::Frontend>.
193
194 =cut
195
196 sub collect_output
197 {
198     my $r = shift;
199     
200     # Collect HTTP headers
201     my %headers = (
202         -type            => $r->content_type,
203         -charset         => $r->document_encoding,
204         -content_length  => do { use bytes; length $r->output },
205     );
206     foreach ($r->headers_out->field_names) {
207         next if /^Content-(Type|Length)/;
208         $headers{"-$_"} = $r->headers_out->get($_);
209     }
210
211     return $r->cgi->header(%headers) . $r->output;
212 }
213
214 =item get_template_root
215
216 =cut
217
218 sub get_template_root {
219     my $r = shift;
220     $r->cgi->document_root . "/" . $r->cgi->url( -relative => 1 );
221 }
222
223 1;
224
225
226 =back
227
228 =head1 DEPENDANCIES
229
230 CGI::Simple
231
232 =head1 AUTHORS
233
234 Dave Ranney C<dave@sialia.com>
235
236 Simon Cozens C<simon@cpan.org>
237
238 =cut