1HTML::Mason::CGIHandlerU(s3e)r Contributed Perl DocumentaHtTiMoLn::Mason::CGIHandler(3)
2
3
4

NAME

6       HTML::Mason::CGIHandler - Use Mason in a CGI environment
7

SYNOPSIS

9       In httpd.conf or .htaccess:
10
11           <LocationMatch "\.html$">
12               Action html-mason /cgi-bin/mason_handler.cgi
13               AddHandler html-mason .html
14           </LocationMatch>
15           <LocationMatch "^/cgi-bin/">
16               RemoveHandler .html
17           </LocationMatch>
18           <FilesMatch "(autohandler|dhandler)$">
19               Order allow,deny
20               Deny from all
21           </FilesMatch>
22
23       A script at /cgi-bin/mason_handler.pl :
24
25          #!/usr/bin/perl
26          use HTML::Mason::CGIHandler;
27
28          my $h = HTML::Mason::CGIHandler->new
29           (
30            data_dir  => '/home/jethro/code/mason_data',
31            allow_globals => [qw(%session $u)],
32           );
33
34          $h->handle_request;
35
36       A .html component somewhere in the web server's document root:
37
38          <%args>
39           $mood => 'satisfied'
40          </%args>
41          % $r->err_header_out(Location => "http://blahblahblah.com/moodring/$mood.html");
42          ...
43

DESCRIPTION

45       This module lets you execute Mason components in a CGI environment.  It
46       lets you keep your top-level components in the web server's document
47       root, using regular component syntax and without worrying about the
48       particular details of invoking Mason on each request.
49
50       If you want to use Mason components from within a regular CGI script
51       (or any other Perl program, for that matter), then you don't need this
52       module.  You can simply follow the directions in the Using Mason from a
53       standalone script section of the administrator's manual.
54
55       This module also provides an $r request object for use inside
56       components, similar to the Apache request object under
57       "HTML::Mason::ApacheHandler", but limited in functionality.  Please
58       note that we aim to replicate the "mod_perl" functionality as closely
59       as possible - if you find differences, do not depend on them to stay
60       different.  We may fix them in a future release.  Also, if you need
61       some missing functionality in $r, let us know, we might be able to
62       provide it.
63
64       Finally, this module alters the "HTML::Mason::Request" object $m to
65       provide direct access to the CGI query, should such access be
66       necessary.
67
68   "HTML::Mason::CGIHandler" Methods
69new()
70
71           Creates a new handler.  Accepts any parameter that the Interpreter
72           accepts.
73
74           If no "comp_root" parameter is passed to new(), the component root
75           will be $ENV{DOCUMENT_ROOT}.
76
77handle_request()
78
79           Handles the current request, reading input from $ENV{QUERY_STRING}
80           or "STDIN" and sending headers and component output to "STDOUT".
81           This method doesn't accept any parameters.  The initial component
82           will be the one specified in $ENV{PATH_INFO}.
83
84handle_comp()
85
86           Like handle_request(), but the first (only) parameter is a
87           component path or component object.  This is useful within a
88           traditional CGI environment, in which you're essentially using
89           Mason as a templating language but not an application server.
90
91           handle_component() will create a CGI query object, parse the query
92           parameters, and send the HTTP header and component output to
93           STDOUT.  If you want to handle those parts yourself, see the Using
94           Mason from a standalone script section of the administrator's
95           manual.
96
97handle_cgi_object()
98
99           Also like handle_request(), but this method takes only a CGI object
100           as its parameter.  This can be quite useful if you want to use this
101           module with CGI::Fast.
102
103           The component path will be the value of the CGI object's
104           path_info() method.
105
106request_args()
107
108           Given an "HTML::Mason::FakeApache" object, this method is expected
109           to return a hash containing the arguments to be passed to the
110           component.  It is a separate method in order to make it easily
111           overrideable in a subclass.
112
113interp()
114
115           Returns the Mason Interpreter associated with this handler.  The
116           Interpreter lasts for the entire lifetime of the handler.
117
118   $r Methods
119headers_in()
120
121           This works much like the "Apache" method of the same name. In an
122           array context, it will return a %hash of response headers. In a
123           scalar context, it will return a reference to the case-insensitive
124           hash blessed into the "HTML::Mason::FakeTable" class. The values
125           initially populated in this hash are extracted from the CGI
126           environment variables as best as possible. The pattern is to merely
127           reverse the conversion from HTTP headers to CGI variables as
128           documented here:
129           <http://cgi-spec.golux.com/draft-coar-cgi-v11-03-clean.html#6.1>.
130
131header_in()
132
133           This works much like the "Apache" method of the same name. When
134           passed the name of a header, returns the value of the given
135           incoming header. When passed a name and a value, sets the value of
136           the header. Setting the header to "undef" will actually unset the
137           header (instead of setting its value to "undef"), removing it from
138           the table of headers returned from future calls to headers_in() or
139           header_in().
140
141headers_out()
142
143           This works much like the "Apache" method of the same name. In an
144           array context, it will return a %hash of response headers. In a
145           scalar context, it will return a reference to the case-insensitive
146           hash blessed into the "HTML::Mason::FakeTable" class. Changes made
147           to this hash will be made to the headers that will eventually be
148           passed to the "CGI" module's header() method.
149
150header_out()
151
152           This works much like the "Apache" method of the same name.  When
153           passed the name of a header, returns the value of the given
154           outgoing header.  When passed a name and a value, sets the value of
155           the header.  Setting the header to "undef" will actually unset the
156           header (instead of setting its value to "undef"), removing it from
157           the table of headers that will be sent to the client.
158
159           The headers are eventually passed to the "CGI" module's header()
160           method.
161
162err_headers_out()
163
164           This works much like the "Apache" method of the same name. In an
165           array context, it will return a %hash of error response headers. In
166           a scalar context, it will return a reference to the case-
167           insensitive hash blessed into the "HTML::Mason::FakeTable" class.
168           Changes made to this hash will be made to the error headers that
169           will eventually be passed to the "CGI" module's header() method.
170
171err_header_out()
172
173           This works much like the "Apache" method of the same name. When
174           passed the name of a header, returns the value of the given
175           outgoing error header. When passed a name and a value, sets the
176           value of the error header. Setting the header to "undef" will
177           actually unset the header (instead of setting its value to
178           "undef"), removing it from the table of headers that will be sent
179           to the client.
180
181           The headers are eventually passed to the "CGI" module's header()
182           method.
183
184           One header currently gets special treatment - if you set a
185           "Location" header, you'll cause the "CGI" module's redirect()
186           method to be used instead of the header() method.  This means that
187           in order to do a redirect, all you need to do is:
188
189            $r->err_header_out(Location => 'http://redirect.to/here');
190
191           You may be happier using the "$m->redirect" method, though, because
192           it hides most of the complexities of sending headers and getting
193           the status code right.
194
195content_type()
196
197           When passed an argument, sets the content type of the current
198           request to the value of the argument.  Use this method instead of
199           setting a "Content-Type" header directly with header_out().  Like
200           header_out(), setting the content type to "undef" will remove any
201           content type set previously.
202
203           When called without arguments, returns the value set by a previous
204           call to content_type().  The behavior when content_type() hasn't
205           already been set is undefined - currently it returns "undef".
206
207           If no content type is set during the request, the default MIME type
208           "text/html" will be used.
209
210method()
211
212           Returns the request method used for the current request, e.g.,
213           "GET", "POST", etc.
214
215http_header()
216
217           This method returns the outgoing headers as a string, suitable for
218           sending to the client.
219
220send_http_header()
221
222           Sends the outgoing headers to the client.
223
224notes()
225
226           This works much like the "Apache" method of the same name. When
227           passed a $key argument, it returns the value of the note for that
228           key. When passed a $value argument, it stores that value under the
229           key. Keys are case-insensitive, and both the key and the value must
230           be strings. When called in a scalar context with no $key argument,
231           it returns a hash reference blessed into the
232           "HTML::Mason::FakeTable" class.
233
234pnotes()
235
236           Like notes(), but takes any scalar as an value, and stores the
237           values in a case-sensitive hash.
238
239subprocess_env()
240
241           Works like the "Apache" method of the same name, but is simply
242           populated with the current values of the environment. Still, it's
243           useful, because values can be changed and then seen by later
244           components, but the environment itself remains unchanged. Like the
245           "Apache" method, it will reset all of its values to the current
246           environment again if it's called without a $key argument.
247
248params()
249
250           This method returns a hash containing the parameters sent by the
251           client.  Multiple parameters of the same name are represented by
252           array references.  If both POST and query string arguments were
253           submitted, these will be merged together.
254
255   Added $m methods
256       The $m object provided in components has all the functionality of the
257       regular "HTML::Mason::Request" object $m, and the following:
258
259cgi_object()
260
261           Returns the current "CGI" request object.  This is handy for
262           processing cookies or perhaps even doing HTML generation (but is
263           that really what you want to do?).  If you pass an argument to this
264           method, you can set the request object to the argument passed.  Use
265           this with care, as it may affect components called after the
266           current one (they may check the content length of the request, for
267           example).
268
269           Note that the ApacheHandler class (for using Mason under mod_perl)
270           also provides a cgi_object() method that does the same thing as
271           this one.  This makes it easier to write components that function
272           equally well under CGIHandler and ApacheHandler.
273
274cgi_request()
275
276           Returns the object that is used to emulate Apache's request object.
277           In other words, this is the object that $r is set to when you use
278           this class.
279
280   "HTML::Mason::FakeTable" Methods
281       This class emulates the behavior of the "Apache::Table" class, and is
282       used to store manage the tables of values for the following attributes
283       of <$r>:
284
285       headers_in
286       headers_out
287       err_headers_out
288       notes
289       subprocess_env
290
291       "HTML::Mason::FakeTable" is designed to behave exactly like
292       "Apache::Table", and differs in only one respect. When a given key has
293       multiple values in an "Apache::Table" object, one can fetch each of the
294       values for that key using Perl's "each" operator:
295
296         while (my ($k, $v) = each %{$r->headers_out}) {
297             push @cookies, $v if lc $k eq 'set-cookie';
298         }
299
300       If anyone knows how Apache::Table does this, let us know! In the
301       meantime, use get() or do() to get at all of the values for a given key
302       (get() is much more efficient, anyway).
303
304       Since the methods named for these attributes return an
305       "HTML::Mason::FakeTable" object hash in a scalar reference, it seemed
306       only fair to document its interface.
307
308new()
309
310           Returns a new "HTML::Mason::FakeTable" object. Any parameters
311           passed to new() will be added to the table as initial values.
312
313add()
314
315           Adds a new value to the table. If the value did not previously
316           exist under the given key, it will be created. Otherwise, it will
317           be added as a new value to the key.
318
319clear()
320
321           Clears the table of all values.
322
323do()
324
325           Pass a code reference to this method to have it iterate over all of
326           the key/value pairs in the table. Keys will multiple values will
327           trigger the execution of the code reference multiple times for each
328           value. The code reference should expect two arguments: a key and a
329           value. Iteration terminates when the code reference returns false,
330           to be sure to have it return a true value if you wan it to iterate
331           over every value in the table.
332
333get()
334
335           Gets the value stored for a given key in the table. If a key has
336           multiple values, all will be returned when get() is called in an
337           array context, and only the first value when it is called in a
338           scalar context.
339
340merge()
341
342           Merges a new value with an existing value by concatenating the new
343           value onto the existing. The result is a comma-separated list of
344           all of the values merged for a given key.
345
346set()
347
348           Takes key and value arguments and sets the value for that key.
349           Previous values for that key will be discarded. The value must be a
350           string, or set() will turn it into one. A value of "undef" will
351           have the same behavior as unset().
352
353unset()
354
355           Takes a single key argument and deletes that key from the table, so
356           that none of its values will be in the table any longer.
357
358
359
360perl v5.36.0                      2023-01-20        HTML::Mason::CGIHandler(3)
Impressum