home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2005 March / PCWELT_3_2005.ISO / pcwsoft / framework-2.2.exe / ssl_read_until.al < prev    next >
Encoding:
Text File  |  2004-02-17  |  3.1 KB  |  96 lines

  1. # NOTE: Derived from blib/lib/Net/SSLeay.pm.
  2. # Changes made here will be lost when autosplit is run again.
  3. # See AutoSplit.pm.
  4. package Net::SSLeay;
  5.  
  6. #line 1560 "blib/lib/Net/SSLeay.pm (autosplit into blib/lib/auto/Net/SSLeay/ssl_read_until.al)"
  7. ### from patch by Clinton Wong <clintdw@netcom.com>
  8.  
  9. # ssl_read_until($ssl [, $delimit [, $max_length]])
  10. #  if $delimit missing, use $/ if it exists, otherwise use \n
  11. #  read until delimiter reached, up to $max_length chars if defined
  12.  
  13. sub ssl_read_until ($;$$) {
  14.     my ($ssl,$delim, $max_length) = @_;
  15.     local $[;
  16.  
  17.     # guess the delim string if missing
  18.     if ( ! defined $delim ) {           
  19.       if ( defined $/ && length $/  ) { $delim = $/ }
  20.       else { $delim = "\n" }      # Note: \n,$/ value depends on the platform
  21.     }
  22.     my $len_delim = length $delim;
  23.  
  24.     my ($got);
  25.     my $reply = '';
  26.     
  27.     # If we have OpenSSL 0.9.6a or later, we can use SSL_peek to
  28.     # speed things up.
  29.     # N.B. 0.9.6a has security problems, so the support for
  30.     #      anything earlier than 0.9.6e will be dropped soon.
  31.     if (&Net::SSLeay::OPENSSL_VERSION_NUMBER >= 0x0090601f) {
  32.     $max_length = 2000000000 unless (defined $max_length);
  33.     my ($pending, $peek_length, $found, $done);
  34.     while (blength($reply) < $max_length and !$done) {
  35.         #Block if necessary until we get some data
  36.         $got = Net::SSLeay::peek($ssl,1);
  37.         last if print_errs('SSL_peek');
  38.  
  39.         $pending = Net::SSLeay::pending($ssl) + blength($reply);
  40.         $peek_length = ($pending > $max_length) ? $max_length : $pending;
  41.         $peek_length -= blength($reply);
  42.         $got = Net::SSLeay::peek($ssl, $peek_length);
  43.         last if print_errs('SSL_peek');
  44.         $peek_length = blength($got);
  45.         
  46.         #$found = index($got, $delim);  # Old and broken
  47.         
  48.         # the delimiter may be split across two gets, so we prepend
  49.         # a little from the last get onto this one before we check
  50.         # for a match
  51.         my $match;
  52.         if(blength($reply) >= blength($delim) - 1) {
  53.         #if what we've read so far is greater or equal
  54.         #in length of what we need to prepatch
  55.         $match = substr $reply, blength($reply) - blength($delim) + 1;
  56.         } else {
  57.         $match = $reply;
  58.         }
  59.  
  60.         $match .= $got;
  61.         $found = index($match, $delim);
  62.  
  63.         if ($found > -1) {
  64.         #$got = Net::SSLeay::read($ssl, $found+$len_delim);
  65.         #read up to the end of the delimiter
  66.         $got = Net::SSLeay::read($ssl,
  67.                      $found + $len_delim
  68.                      - ((blength $match) - (blength $got)));
  69.         $done = 1;
  70.         } else {
  71.         $got = Net::SSLeay::read($ssl, $peek_length);
  72.         $done = 1 if ($peek_length == $max_length - blength($reply));
  73.         } 
  74.  
  75.         last if print_errs('SSL_read');
  76.         debug_read(\$reply, \$got) if $trace>1;
  77.         last if $got eq '';
  78.         $reply .= $got;
  79.     }
  80.     } else {
  81.     while (!defined $max_length || length $reply < $max_length) {
  82.         $got = Net::SSLeay::read($ssl,1);  # one by one
  83.         last if print_errs('SSL_read');
  84.         debug_read(\$reply, \$got) if $trace>1;
  85.         last if $got eq '';
  86.         $reply .= $got;
  87.         last if $len_delim
  88.         && substr($reply, blength($reply)-$len_delim) eq $delim;
  89.     }
  90.     }
  91.     return $reply;
  92. }
  93.  
  94. # end of Net::SSLeay::ssl_read_until
  95. 1;
  96.