r250 - in /apt-transport-debtorrent/trunk: debtorrent.cc debtorrent.h

camrdale-guest at users.alioth.debian.org camrdale-guest at users.alioth.debian.org
Wed Aug 15 05:51:40 UTC 2007


Author: camrdale-guest
Date: Wed Aug 15 05:51:40 2007
New Revision: 250

URL: http://svn.debian.org/wsvn/debtorrent/?sc=1&rev=250
Log:
Allow requests to be returned in any order.

Modified:
    apt-transport-debtorrent/trunk/debtorrent.cc
    apt-transport-debtorrent/trunk/debtorrent.h

Modified: apt-transport-debtorrent/trunk/debtorrent.cc
URL: http://svn.debian.org/wsvn/debtorrent/apt-transport-debtorrent/trunk/debtorrent.cc?rev=250&op=diff
==============================================================================
--- apt-transport-debtorrent/trunk/debtorrent.cc (original)
+++ apt-transport-debtorrent/trunk/debtorrent.cc Wed Aug 15 05:51:40 2007
@@ -521,32 +521,12 @@
    
    if (stringcasecmp(Tag.c_str(),Tag.c_str()+10,"DEBTORRENT") == 0)
    {
-      // Evil servers return no version
-      if (Line[10] == '/')
-      {
-	 if (sscanf(Line.c_str(),"DEBTORRENT/%u.%u %u %[^\n]",&Major,&Minor,
-		    &Result,Code) != 4)
-	    return _error->Error(_("The HTTP server sent an invalid reply header"));
-      }
-      else
-      {
-	 Major = 0;
-	 Minor = 1;
-	 if (sscanf(Line.c_str(),"DEBTORRENT %u %[^\n]",&Result,Code) != 2)
-	    return _error->Error(_("The HTTP server sent an invalid reply header"));
-      }
-
-      /* Check the HTTP response header to get the default persistance
-         state. */
-      if (Major < 0)
-	 Persistent = false;
-      else
-      {
-	 if (Major == 0 && Minor <= 0)
-	    Persistent = false;
-	 else
-	    Persistent = true;
-      }
+      if (sscanf(Line.c_str(),"DEBTORRENT/%u.%u %s %u %[^\n]",&Major,&Minor,
+		 ResultPath,&Result,Code) != 5)
+	 return _error->Error(_("The HTTP server sent an invalid reply header"));
+
+      // DEBTORRENT connections are always persistent
+      Persistent = true;
 
       return true;
    }      
@@ -835,6 +815,55 @@
       return true;
    }
    
+   return false;
+}
+									/*}}}*/
+// HttpMethod::RearrangeQueue - Move the item returned to the top	/*{{{*/
+// ---------------------------------------------------------------------
+/* Look at the ResultPath we got back from the server and find the
+   item to move to the top. Returns false if the item is not in the
+   queue.  */
+bool HttpMethod::RearrangeQueue(ServerState *Srv)
+{
+   URI Uri;
+   FetchItem *PrevI;
+   PrevI = 0;
+
+   // Search the queue for the returned item
+   for (FetchItem *I = Queue; I != 0; I = I->Next)
+   {
+      Uri = I->Uri;
+
+      // If this is the end of the queue (QueueBack always points to the
+      // next item to request), then the item is not found.
+      if (I == QueueBack || I == 0)
+      {
+	 break;
+      }
+
+      // Compare this item's path with the returned path
+      if (stringcmp(QuoteString(Uri.Path,"~"),Srv->ResultPath) == 0)
+      {
+      	 if (PrevI == 0)
+      	 {
+	    // The item is already at the head of the queue
+	    return true;
+      	 }
+
+	 // Pop the item out of the queue
+	 PrevI->Next = I->Next;
+	 
+	 // Add it to the front of the queue
+	 I->Next = Queue;
+	 Queue = I;
+	 
+	 return true;
+      }
+      
+      PrevI = I;
+   }
+   
+   // The item was not found
    return false;
 }
 									/*}}}*/
@@ -1072,106 +1101,118 @@
 
       // Decide what to do.
       FetchResult Res;
-      Res.Filename = Queue->DestFile;
-      switch (DealWithHeaders(Res,Server))
-      {
-	 // Ok, the file is Open
-	 case 0:
+      if (RearrangeQueue(Server))
+      {
+	 Res.Filename = Queue->DestFile;
+	 switch (DealWithHeaders(Res,Server))
 	 {
-	    URIStart(Res);
-
-	    // Run the data
-	    bool Result =  Server->RunData();
-
-	    /* If the server is sending back sizeless responses then fill in
-	       the size now */
-	    if (Res.Size == 0)
-	       Res.Size = File->Size();
+	    // Ok, the file is Open
+	    case 0:
+	    {
+	       URIStart(Res);
+
+	       // Run the data
+	       bool Result =  Server->RunData();
+
+	       /* If the server is sending back sizeless responses then fill in
+		  the size now */
+	       if (Res.Size == 0)
+		  Res.Size = File->Size();
 	    
-	    // Close the file, destroy the FD object and timestamp it
-	    FailFd = -1;
-	    delete File;
-	    File = 0;
+	       // Close the file, destroy the FD object and timestamp it
+	       FailFd = -1;
+	       delete File;
+	       File = 0;
 	    
-	    // Timestamp
-	    struct utimbuf UBuf;
-	    time(&UBuf.actime);
-	    UBuf.actime = Server->Date;
-	    UBuf.modtime = Server->Date;
-	    utime(Queue->DestFile.c_str(),&UBuf);
-
-	    // Send status to APT
-	    if (Result == true)
-	    {
-	       Res.TakeHashes(*Server->In.Hash);
-	       URIDone(Res);
-	    }
-	    else
-	    {
-	       if (Server->ServerFd == -1)
+	       // Timestamp
+	       struct utimbuf UBuf;
+	       time(&UBuf.actime);
+	       UBuf.actime = Server->Date;
+	       UBuf.modtime = Server->Date;
+	       utime(Queue->DestFile.c_str(),&UBuf);
+
+	       // Send status to APT
+	       if (Result == true)
 	       {
-		  FailCounter++;
-		  _error->Discard();
-		  Server->Close();
-		  
-		  if (FailCounter >= 2)
-		  {
-		     Fail(_("Connection failed"),true);
-		     FailCounter = 0;
-		  }
-		  
-		  QueueBack = Queue;
+		  Res.TakeHashes(*Server->In.Hash);
+		  URIDone(Res);
 	       }
 	       else
-		  Fail(true);
+	       {
+		  if (Server->ServerFd == -1)
+		  {
+		     FailCounter++;
+		     _error->Discard();
+		     Server->Close();
+		  
+		     if (FailCounter >= 2)
+		     {
+		        Fail(_("Connection failed"),true);
+		        FailCounter = 0;
+		     }
+		  
+		     QueueBack = Queue;
+	          }
+	          else
+		     Fail(true);
+	       }
+	       break;
 	    }
+	 
+	    // IMS hit
+	    case 1:
+	    {
+	       URIDone(Res);
+	       break;
+	    }
+	 
+	    // Hard server error, not found or something
+	    case 3:
+	    {
+	       Fail();
+	       break;
+	    }
+	  
+	    // Hard internal error, kill the connection and fail
+	    case 5:
+	    {
+	       delete File;
+	       File = 0;
+
+	       Fail();
+	       RotateDNS();
+	       Server->Close();
+	       break;
+	    }
+
+	    // We need to flush the data, the header is like a 404 w/ error text
+	    case 4:
+	    {
+	       Fail();
+	    
+	       // Send to content to dev/null
+	       File = new FileFd("/dev/null",FileFd::WriteExists);
+	       Server->RunData();
+	       delete File;
+	       File = 0;
+	       break;
+	    }
+	 
+	    default:
+	    Fail(_("Internal error"));
 	    break;
 	 }
-	 
-	 // IMS hit
-	 case 1:
-	 {
-	    URIDone(Res);
-	    break;
-	 }
-	 
-	 // Hard server error, not found or something
-	 case 3:
-	 {
-	    Fail();
-	    break;
-	 }
-	  
-	 // Hard internal error, kill the connection and fail
-	 case 5:
-	 {
-	    delete File;
-	    File = 0;
-
-	    Fail();
-	    RotateDNS();
-	    Server->Close();
-	    break;
-	 }
-
-	 // We need to flush the data, the header is like a 404 w/ error text
-	 case 4:
-	 {
-	    Fail();
-	    
-	    // Send to content to dev/null
-	    File = new FileFd("/dev/null",FileFd::WriteExists);
-	    Server->RunData();
-	    delete File;
-	    File = 0;
-	    break;
-	 }
-	 
-	 default:
-	 Fail(_("Internal error"));
-	 break;
-      }
-      
+      }
+      else
+      {
+	 // The result could not be found in the requests
+	 delete File;
+	 File = 0;
+
+	 Fail(_("Data returned for an unrequested file"));
+	 RotateDNS();
+	 Server->Close();
+      }
       FailCounter = 0;
    }
    

Modified: apt-transport-debtorrent/trunk/debtorrent.h
URL: http://svn.debian.org/wsvn/debtorrent/apt-transport-debtorrent/trunk/debtorrent.h?rev=250&op=diff
==============================================================================
--- apt-transport-debtorrent/trunk/debtorrent.h (original)
+++ apt-transport-debtorrent/trunk/debtorrent.h Wed Aug 15 05:51:40 2007
@@ -89,6 +89,7 @@
    // This is the last parsed Header Line
    unsigned int Major;
    unsigned int Minor;
+   char ResultPath[MAXLEN];
    unsigned int Result;
    char Code[MAXLEN];
    
@@ -133,6 +134,7 @@
    bool Go(bool ToFile,ServerState *Srv);
    bool Flush(ServerState *Srv);
    bool ServerDie(ServerState *Srv);
+   bool RearrangeQueue(ServerState *Srv);
    int DealWithHeaders(FetchResult &Res,ServerState *Srv);
 
    virtual bool Fetch(FetchItem *);




More information about the Debtorrent-commits mailing list