File: group__apreq__xs__request.html

package info (click to toggle)
libapreq2 2.17-3~bpo12%2B1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-backports
  • size: 5,108 kB
  • sloc: ansic: 8,263; perl: 5,451; sh: 4,627; cpp: 380; makefile: 270; javascript: 186
file content (349 lines) | stat: -rw-r--r-- 16,392 bytes parent folder | download | duplicates (2)
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
<html>
<head>
  <link rel="stylesheet" type="text/css" href="dox.css" />
  <title>libapreq2-2.17: Apache2::Request</title> 
</head>
<body>
<div id="page-header">
<p class="menu">
   <a href="http://www.apache.org/">Apache Software Foundation</a> &gt; <a href="http://httpd.apache.org">HTTP Server Project</a> &gt;
<a href="http://httpd.apache.org/apreq/">Request Library Subproject</a></p>
<p class="apache">Apache HTTP Server Request Library</p>
<img alt="" src="feather.gif" /></div>
<!-- Generated by Doxygen 1.9.1 -->
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(function() {
  initMenu('',false,false,'search.php','Search');
});
/* @license-end */</script>
<div id="main-nav"></div>
</div><!-- top -->
<div class="header">
  <div class="headertitle">
<div class="title">Apache2::Request<div class="ingroups"><a class="el" href="group__apreq__lang.html">Language Bindings</a> &raquo; <a class="el" href="group__apreq__xs.html">Perl</a></div></div>  </div>
</div><!--header-->
<div class="contents">




<ul id="index">
  <li><a href="#NAME">NAME</a></li>
  <li><a href="#SYNOPSIS">SYNOPSIS</a></li>
  <li><a href="#DESCRIPTION">DESCRIPTION</a></li>
  <li><a href="#Apache2::Request">Apache2::Request</a>
    <ul>
      <li><a href="#new">new</a></li>
      <li><a href="#instance">instance</a></li>
      <li><a href="#param">param</a></li>
      <li><a href="#parms-params">parms, params</a></li>
      <li><a href="#body">body</a></li>
      <li><a href="#upload">upload</a></li>
      <li><a href="#args_status">args_status</a></li>
      <li><a href="#body_status">body_status</a></li>
      <li><a href="#param_status">param_status</a></li>
      <li><a href="#parse">parse</a></li>
    </ul>
  </li>
  <li><a href="#SUBCLASSING-Apache2::Request">SUBCLASSING Apache2::Request</a></li>
  <li><a href="#PORTING-from-1.X">PORTING from 1.X</a></li>
  <li><a href="#SEE-ALSO">SEE ALSO</a></li>
  <li><a href="#COPYRIGHT">COPYRIGHT</a></li>
</ul>

<h1 id="NAME">NAME</h1>

<p>Apache2::Request - Methods for dealing with client request data</p>

<h1 id="SYNOPSIS">SYNOPSIS</h1>

<pre><code>use Apache2::Request;
$req = Apache2::Request-&gt;new($r);
@foo = $req-&gt;param(&quot;foo&quot;);
$bar = $req-&gt;args(&quot;bar&quot;);</code></pre>

<h1 id="DESCRIPTION">DESCRIPTION</h1>

<p>The Apache2::Request module provides methods for parsing GET and POST parameters encoded with either <i>application/x-www-form-urlencoded</i> or <i>multipart/form-data</i>. Although Apache2::Request provides a few new APIs for accessing the parsed data, it remains largely backwards-compatible with the original 1.X API. See the <a href="#PORTING-from-1.X">&quot;PORTING from 1.X&quot;</a> section below for a list of known issues.</p>

<p>This manpage documents the Apache2::Request package.</p>

<h1 id="Apache2::Request">Apache2::Request</h1>

<p>The interface is designed to mimic the CGI.pm routines for parsing query parameters. The main differences are</p>

<ul>

<li><p><code>Apache2::Request::new</code> takes an environment-specific object <code>$r</code> as (second) argument. Newer versions of CGI.pm also accept this syntax within modperl.</p>

</li>
<li><p>The query parameters are stored in APR::Table derived objects, and are therefore retrieved from the table by using case-insensitive keys.</p>

</li>
<li><p>The query string is always parsed immediately, even for POST requests.</p>

</li>
</ul>

<h2 id="new">new</h2>

<pre><code>Apache2::Request-&gt;new($r, %args)</code></pre>

<p>Creates a new Apache2::Request object.</p>

<pre><code>my $req = Apache2::Request-&gt;new($r, POST_MAX =&gt; &quot;1M&quot;);</code></pre>

<p>With mod_perl2, the environment object $r must be an Apache2::RequestRec object. In that case, all methods from Apache2::RequestRec are inherited. In the (default) CGI environment, $r must be an APR::Pool object.</p>

<p>The following args are optional:</p>

<ul>

<li><p><code>POST_MAX</code>, <code>MAX_BODY</code></p>

<p>Limit the size of POST data (in bytes).</p>

</li>
<li><p><code>DISABLE_UPLOADS</code></p>

<p>Disable file uploads.</p>

</li>
<li><p><code>TEMP_DIR</code></p>

<p>Sets the directory where upload files are spooled. On a *nix-like that supports <i>link(2)</i>, the TEMP_DIR should be located on the same file system as the final destination file:</p>

<pre><code>use Apache2::Upload;
my $req = Apache2::Request-&gt;new($r, TEMP_DIR =&gt; &quot;/home/httpd/tmp&quot;);
my $upload = $req-&gt;upload(&#39;file&#39;);
$upload-&gt;link(&quot;/home/user/myfile&quot;);</code></pre>

<p>For more details on <code>link</code>, see <a>Apache2::Upload</a>.</p>

</li>
<li><p><code>HOOK_DATA</code></p>

<p>Extra configuration info passed as the fourth argument to an upload hook. See the description for the next item, <code>UPLOAD_HOOK</code>.</p>

</li>
<li><p><code>UPLOAD_HOOK</code></p>

<p>Sets up a callback to run whenever file upload data is read. This can be used to provide an upload progress meter during file uploads. Apache will automatically continue writing the original data to $upload-&gt;fh after the hook exits.</p>

<pre><code>my $transparent_hook = sub {
  my ($upload, $data, $data_len, $hook_data) = @_;
  warn &quot;$hook_data: got $data_len bytes for &quot; . $upload-&gt;name;
};

my $req = Apache2::Request-&gt;new($r,
                                HOOK_DATA =&gt; &quot;Note&quot;,
                                UPLOAD_HOOK =&gt; $transparent_hook,
                               );</code></pre>

</li>
</ul>

<h2 id="instance">instance</h2>

<pre><code>Apache2::Request-&gt;instance($r)</code></pre>

<p>The default (and only) behavior of <i>Apache2::Request</i> is to intelligently cache <b>POST</b> data for the duration of the request. Thus there is no longer the need for a separate <code>instance()</code> method as existed in <i>Apache2::Request</i> for Apache 1.3 - all <b>POST</b> data is always available from each and every <i>Apache2::Request</i> object created during the request&#39;s lifetime.</p>

<p>However an <code>instance()</code> method is aliased to <code>new()</code> in this release to ease the pain of porting from 1.X to 2.X.</p>

<h2 id="param">param</h2>

<pre><code>$req-&gt;param()
$req-&gt;param($name)</code></pre>

<p>Get the request parameters (using case-insensitive keys) by mimicking the OO interface of <code>CGI::param</code>.</p>

<pre><code># similar to CGI.pm

my $foo_value   = $req-&gt;param(&#39;foo&#39;);
my @foo_values  = $req-&gt;param(&#39;foo&#39;);
my @param_names = $req-&gt;param;

# the following differ slightly from CGI.pm

# returns ref to APR::Request::Param::Table object representing
# all (args + body) params
my $table = $req-&gt;param;
@table_keys = keys %$table;</code></pre>

<p>In list context, or when invoked with no arguments as <code>$req-&gt;param()</code>, <code>param</code> induces libapreq2 to read and parse all remaining data in the request body. However, <code>scalar $req-&gt;param(&quot;foo&quot;)</code> is lazy: libapreq2 will only read and parse more data if</p>

<pre><code>1) no &quot;foo&quot; param appears in the query string arguments, AND
2) no &quot;foo&quot; param appears in the previously parsed POST data.</code></pre>

<p>In this circumstance libapreq2 will read and parse additional blocks of the incoming request body until either</p>

<pre><code>1) it has found the the &quot;foo&quot; param, or
2) parsing is completed.</code></pre>

<p>Observe that <code>scalar $req-&gt;param(&quot;foo&quot;)</code> will not raise an exception if it can locate &quot;foo&quot; in the existing body or args tables, even if the query-string parser or the body parser has failed. In all other circumstances <code>param</code> will throw an Apache2::Request::Error object into $@ should either parser fail.</p>

<pre><code>$req-&gt;args_status(1); # set error state for query-string parser
ok $req-&gt;param_status == 1;

$foo = $req-&gt;param(&quot;foo&quot;);
ok $foo == 1;
eval { @foo = $req-&gt;param(&quot;foo&quot;) };
ok $@-&gt;isa(&quot;Apache2::Request::Error&quot;);
undef $@;
eval { my $not_found = $req-&gt;param(&quot;non-existent-param&quot;) };
ok $@-&gt;isa(&quot;Apache2::Request::Error&quot;);

$req-&gt;args_status(0); # reset query-string parser state to &quot;success&quot;</code></pre>

<p>Note: modifications to the <code>scalar $req-&gt;param()</code> table only affect the returned table object (the underlying C apr_table_t is <i>generated</i> from the parse data by apreq_params()). Modifications do not affect the actual request data, and will not be seen by other libapreq2 applications.</p>

<h2 id="parms-params">parms, params</h2>

<p>The functionality of these functions is assumed by <code>param</code>, so they are no longer necessary. Aliases to <code>param</code> are provided in this release for backwards compatibility, however they are deprecated and may be removed from a future release.</p>

<h2 id="body">body</h2>

<pre><code>$req-&gt;body()
$req-&gt;body($name)</code></pre>

<p>Returns an <i>APR::Request::Param::Table</i> object containing the POST data parameters of the <i>Apache2::Request</i> object.</p>

<pre><code>my $body = $req-&gt;body;</code></pre>

<p>An optional name parameter can be passed to return the POST data parameter associated with the given name:</p>

<pre><code>my $foo_body = $req-&gt;body(&quot;foo&quot;);</code></pre>

<p>More generally, <code>body()</code> follows the same pattern as <code>param()</code> with respect to its return values and argument list. The main difference is that modifications to the <code>scalar $req-&gt;body()</code> table affect the underlying apr_table_t attribute in apreq_request_t, so their impact will be noticed by all libapreq2 applications during this request.</p>

<h2 id="upload">upload</h2>

<pre><code>$req-&gt;upload()
$req-&gt;upload($name)</code></pre>

<p>Requires <code>Apache2::Upload</code>. With no arguments, this method returns an <i>APR::Request::Param::Table</i> object in scalar context, or the names of all <i>Apache2::Upload</i> objects in list context.</p>

<p>An optional name parameter can be passed to return the <i>Apache2::Upload</i> object associated with the given name:</p>

<pre><code>my $upload = $req-&gt;upload($name);</code></pre>

<p>More generally, <code>upload()</code> follows the same pattern as <code>param()</code> with respect to its return values and argument list. The main difference is that its returned values are Apache2::Upload object refs, not simple scalars.</p>

<p>Note: modifications to the <code>scalar $req-&gt;upload()</code> table only affect the returned table object (the underlying C apr_table_t is <i>generated</i> by apreq_uploads()). They do not affect the actual request data, and will not be seen by other libapreq2 applications.</p>

<h2 id="args_status">args_status</h2>

<pre><code>$req-&gt;args_status()</code></pre>

<p>Get the <i>APR</i> status code of the query-string parser. APR_SUCCESS on success, error otherwise.</p>

<h2 id="body_status">body_status</h2>

<pre><code>$req-&gt;body_status()</code></pre>

<p>Get the current <i>APR</i> status code of the parsed POST data. APR_SUCCESS when parser has completed, APR_INCOMPLETE if parser has more data to parse, APR_EINIT if no post data has been parsed, error otherwise.</p>

<h2 id="param_status">param_status</h2>

<pre><code>$req-&gt;param_status()</code></pre>

<p>In scalar context, this returns <code>args_status</code> if there was an error during the query-string parse, otherwise this returns <code>body_status</code>, ie</p>

<pre><code>$req-&gt;args_status || $req-&gt;body_status</code></pre>

<p>In list context <code>param_status</code> returns the list <code>(args_status, body_status)</code>.</p>

<h2 id="parse">parse</h2>

<pre><code>$req-&gt;parse()</code></pre>

<p>Forces the request to be parsed immediately. In void context, this will throw an APR::Request::Error should the either the query-string or body parser fail. In all other contexts it will return the two parsers&#39; combined <i>APR</i> status code</p>

<pre><code>$req-&gt;body_status || $req-&gt;args_status</code></pre>

<p>However <code>parse</code> should be avoided in most normal situations. For example, in a mod_perl content handler it is more efficient to write</p>

<pre><code>sub handler {
    my $r = shift;
    my $req = Apache2::Request-&gt;new($r);
    $r-&gt;discard_request_body;   # efficiently parses the request body
    my $parser_status = $req-&gt;body_status;

    #...
}</code></pre>

<p>Calling <code>$r-&gt;discard_request_body</code> outside the content handler is generally a mistake, so use <code>$req-&gt;parse</code> there, but <b>only as a last resort</b>. The Apache2::Request API is <b>designed</b> around a lazy-parsing scheme, so calling <code>parse</code> should not affect the behavior of any other methods.</p>

<h1 id="SUBCLASSING-Apache2::Request">SUBCLASSING Apache2::Request</h1>

<p>If the instances of your subclass are hash references then you can actually inherit from Apache2::Request as long as the Apache2::Request object is stored in an attribute called &quot;r&quot; or &quot;_r&quot;. (The Apache2::Request class effectively does the delegation for you automagically, as long as it knows where to find the Apache2::Request object to delegate to.) For example:</p>

<pre><code>package MySubClass;
use Apache2::Request;
our @ISA = qw(Apache2::Request);
sub new {
        my($class, @args) = @_;
        return bless { r =&gt; Apache2::Request-&gt;new(@args) }, $class;
}</code></pre>

<h1 id="PORTING-from-1.X">PORTING from 1.X</h1>

<p>This is the complete list of changes to existing methods from Apache2::Request 1.X. These issues need to be addressed when porting 1.X apps to the new 2.X API.</p>

<ul>

<li><p>Apache2::Upload is now a separate module. Applications requiring the upload API must <code>use Apache2::Upload</code> in 2.X. This is easily addressed by preloading the modules during server startup.</p>

</li>
<li><p>You can no longer add (or set or delete) parameters in the <code>scalar $req-&gt;param</code>, <code>scalar $req-&gt;args</code> or <code>scalar $req-&gt;body</code> tables. Nor can you add (or set or delete) cookies in the <code>scalar $req-&gt;jar</code> table.</p>

</li>
<li><p><code>instance()</code> is now identical to <code>new()</code>, and is now deprecated. It may be removed from a future 2.X release.</p>

</li>
<li><p><code>param</code> includes the functionality of <code>parms()</code> and <code>params()</code>, so they are now deprecated and may be removed from a future 2.X release.</p>

</li>
<li><p><code>param</code> called in a list context no longer returns a unique list of parameters. The returned list contains multiple instances of the parameter name for multivalued fields.</p>

</li>
</ul>

<h1 id="SEE-ALSO">SEE ALSO</h1>

<p><a>APR::Request::Param</a>, <a>APR::Request::Error</a>, <a>Apache2::Upload</a>, <a>Apache2::Cookie</a>, APR::Table(3).</p>

<h1 id="COPYRIGHT">COPYRIGHT</h1>

<pre><code>Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements.  See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the &quot;License&quot;); you may not use this file except in compliance with
the License.  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an &quot;AS IS&quot; BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.</code></pre>


 </div><!-- contents -->
<div id="footer">
<p class="apache">
Copyright &copy; 2003-2006 <a href="http://www.apache.org">The Apache Software Foundation</a>.<br/>
See <a href="apreq_license.html">LICENSE</a>.</p>
<p class="menu">
<span style="color:#aaa">page generated by <a href="http://www.doxygen.org/"><code>doxygen</code></a>
version 1.9.1 on Thu Aug 18 2022</span>
</p>
</div>
</body>
</html>