Package: apt-cacher
Version: 1.6.5
Severity: normal
Tags: patch

I'm seeing the same exact bug as this one:

  https://bugs.launchpad.net/ubuntu/+source/apt-cacher/+bug/219095

Basically, I get a lot of "400 No Request Recieved", on a
semi-consistent basis.

The reporter for the Ubuntu bug described the cause of the problem as:


> The bug is in the getRequestLine routine in
>  /usr/share/apt-cacher/apt-cacher. The problematic lines are:
>
>   # after every read at least one line MUST have been found. Read length
>   # is large enough.
>    my $n=sysread($source, $buf, 1024);
>
> Apparently from the comments, it's assumed that sysread will read
>  enough bytes to have at least one complete line. Unfortunately this
>  is not true. Given the unblocking nature of sysread, it can read
>  anything from 0 bytes to the buffer length of 1024.
>
> My investigation shows that sysread indeed only reads half lines
>  (such as "GET http://";) when the error happens.

I agree with his conclusions, and his fix works for me too, so I've
made it into a "proper" patch; well, "proper" except for the fact I
had to uncomment "use strict;" :>

Cheers,

--Seb

-- System Information:
Debian Release: lenny/sid
  APT prefers unstable
  APT policy: (500, 'unstable'), (500, 'testing'), (500, 'stable'), (1, 
'experimental')
Architecture: i386 (i686)

Kernel: Linux 2.6.26-1-686 (SMP w/2 CPU cores)
Locale: LANG=en_US.ISO-8859-15, LC_CTYPE=en_US.ISO-8859-15 (charmap=ISO-8859-15)
Shell: /bin/sh linked to /bin/bash

Versions of packages apt-cacher depends on:
ii  bzip2                         1.0.5-0.1  high-quality block-sorting file co
ii  ed                            0.7-1      The classic unix line editor
ii  libdigest-sha1-perl           2.11-2+b1  NIST SHA-1 message digest algorith
ii  libfreezethaw-perl            0.43-4     converting Perl structures to stri
ii  libwww-curl-perl              4.05-1     Perl bindings to libcurl
ii  libwww-perl                   5.812-1    WWW client/server library for Perl
ii  perl                          5.10.0-13  Larry Wall's Practical Extraction 

Versions of packages apt-cacher recommends:
pn  libberkeleydb-perl            <none>     (no description available)

-- no debconf information
--- /usr/sbin/apt-cacher        2008-09-01 04:32:49.000000000 -0700
+++ apt-cacher  2008-10-16 11:23:58.000000000 -0700
@@ -47,7 +47,7 @@
 =cut
 # ----------------------------------------------------------------------------
 
-use strict;
+#use strict;
 use lib '/usr/share/apt-cacher/';
 
 ## Just for testing!
@@ -1632,8 +1632,37 @@
        # after every read at least one line MUST have been found. Read length
        # is large enough.
 
-       my $n=sysread($source, $buf, 1024);
-       $buf=$reqTail.$buf if(defined($reqTail));
+       my $n, $tmp;
+        $buf=$reqTail.$buf if(defined($reqTail));
+        
+      RETRY_READ_SOCKET:
+        $n=sysread($source, $tmp, 1024);
+        if( defined($n) )
+        {
+                if( $n != 0 )
+                {
+                   # we read something: keep reading until \r\n
+                   $buf .= $tmp;
+                        
+                   if( $buf !~ /\r\n/s ) # cannot be $tmp here
+                   {
+                       $tmp = "";
+                       goto RETRY_READ_SOCKET;
+                   }
+                }
+       }
+        elsif($! == EWOULDBLOCK)
+        {
+           # we read nothing: no data from the other end of socket yet
+           sleep(1);
+           goto RETRY_READ_SOCKET;
+        }
+        else
+        {
+           # we read nothing: error
+           return undef;
+        }
+
        undef $reqTail;
 
        # pushes the lines found into the buffer. The last one may be 
incomplete,

Reply via email to