~ubuntu-branches/ubuntu/feisty/libapache2-mod-perl2/feisty-security

« back to all changes in this revision

Viewing changes to docs/user/handlers/protocols.pod

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2004-08-19 06:23:48 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20040819062348-jxl4koqbtvgm8v2t
Tags: 1.99.14-4
Remove the LFS CFLAGS, and build-dep against apache2-*-dev (>= 2.0.50-10)
as we're backing out of the apache2/apr ABI transition.

Show diffs side-by-side

added added

removed removed

Lines of Context:
161
161
 
162
162
A I<process_connection> handler accepts a connection record object as
163
163
its only argument, a socket object can be retrieved from the
164
 
connection record object.
 
164
connection record object. Here is a simplified handler skeleton:
165
165
 
166
166
  sub handler {
167
167
      my ($c) = @_;
168
 
      my $socket = $c->client_socket;
 
168
      my $sock = $c->client_socket;
 
169
      $sock->opt_set(APR::SO_NONBLOCK, 0);
169
170
      # ...
170
171
      return Apache::OK;
171
172
  }
172
173
 
173
 
 
 
174
Most likely you'll need to set the socket to perform blocking IO. On
 
175
some platforms (e.g. Linux) Apache gives us a socket which is set for
 
176
blocking, on other platforms (.e.g. Solaris) it doesn't. Unless you
 
177
know which platforms your application will be running on, always
 
178
explicitly set it to the blocking IO mode as in the example
 
179
above. Alternatively, you could query whether the socket is already
 
180
set to a blocking IO mode with help of C<L<the opt_get()
 
181
method|docs::2.0::api::APR::Socket/C_opt_get_>>.
174
182
 
175
183
Now let's look at the following two examples of connection
176
184
handlers. The first using the connection socket to read and write the
228
236
  use APR::Socket ();
229
237
  
230
238
  use Apache::Const -compile => 'OK';
 
239
  use APR::Const    -compile => 'SO_NONBLOCK';
231
240
  
232
241
  use constant BUFF_LEN => 1024;
233
242
  
234
243
  sub handler {
235
244
      my $c = shift;
236
 
      my $socket = $c->client_socket;
237
 
  
238
 
      my $buff;
 
245
      my $sock = $c->client_socket;
 
246
  
 
247
      # set the socket to the blocking mode
 
248
      $sock->opt_set(APR::SO_NONBLOCK => 0);
 
249
  
239
250
      while (1) {
240
 
          my $rlen = BUFF_LEN;
241
 
          $socket->recv($buff, $rlen);
242
 
  
243
 
          last if $rlen <= 0 or $buff =~ /^[\r\n]+$/;
244
 
  
245
 
          my $wlen = $rlen;
246
 
          $socket->send($buff, $wlen);
247
 
  
248
 
          last if $wlen != $rlen;
 
251
          # read from the socket
 
252
          my $buff = $sock->recv(BUFF_LEN);
 
253
          my $rlen = length $buff;
 
254
          last if $rlen == 0; # EOF
 
255
          last if $buff =~ /^[\r\n]+$/;
 
256
  
 
257
          # write to the socket
 
258
          my $wlen = $sock->send($buff);
 
259
          last if $wlen != $rlen; # shouldn't happen
249
260
      }
250
261
  
251
262
      Apache::OK;
258
269
protocol handler, the first argument is not a C<request_rec>, but a
259
270
C<conn_rec> blessed into the C<Apache::Connection> class.  We have
260
271
direct access to the client socket via C<Apache::Connection>'s
261
 
I<client_socket> method.  This returns an object blessed into the
262
 
C<APR::Socket> class.
 
272
I<client_socket> method.  This returns an object, blessed into the
 
273
C<APR::Socket> class. Before using the socket, we make sure that it's
 
274
set to perform blocking IO, by using the C<APR::SO_NONBLOCK> constant,
 
275
compiled earlier.
263
276
 
264
 
Inside the read/send loop, the handler attempts to read C<BUFF_LEN>
265
 
bytes from the client socket into the C<$buff> buffer.  The C<$rlen>
266
 
parameter will be set to the number of bytes actually read.  The
267
 
C<APR::Socket::recv()> method returns an APR status value, but we need
268
 
only to check the read length to break out of the loop if it is less
269
 
than or equal to C<0> bytes. The handler also breaks the loop after
270
 
processing an input including nothing but new lines characters, which
271
 
is how we abort the connection in the interactive mode.
 
277
Inside the recv/send loop, the handler attempts to read C<BUFF_LEN>
 
278
bytes from the client socket into the C<$buff> buffer. The handler
 
279
breaks the loop if the returned buffer is empty, (there was nothing
 
280
more to read (EOF)) or if the buffer contains nothing but new line
 
281
character(s), which is how we know to abort the connection in the
 
282
interactive mode.
272
283
 
273
284
If the handler receives some data, it sends it unmodified back to the
274
285
client with the C<APR::Socket::send()> method. When the loop is
344
355
      my $last = 0;
345
356
  
346
357
      while (1) {
347
 
          my $rv = $c->input_filters->get_brigade($bb_in, Apache::MODE_GETLINE);
 
358
          my $rv = $c->input_filters->get_brigade($bb_in,
 
359
                                                  Apache::MODE_GETLINE);
348
360
          if ($rv != APR::SUCCESS && $rv != APR::EOF) {
349
361
              my $error = APR::strerror($rv);
350
362
              warn __PACKAGE__ . ": get_brigade: $error\n";
454
466
 
455
467
 
456
468
 
 
469
=head1 CPAN Modules
 
470
 
 
471
Some of the CPAN modules that implement mod_perl 2.0 protocols:
 
472
 
 
473
=over
 
474
 
 
475
=item C<Apache::SMTP> - An SMTP server
 
476
 
 
477
http://search.cpan.org/dist/Apache-SMTP/
 
478
 
 
479
=back
 
480
 
 
481
 
 
482
 
 
483
 
 
484
 
 
485
 
457
486
 
458
487
=head1 Maintainers
459
488